2017-10-27 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / combine.c
blob93adfc11be749fba7fecc9b06cf72c57553dbdf2
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2017 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 use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "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 /* Basic block in which we are performing combines. */
306 static basic_block this_basic_block;
307 static bool optimize_this_for_speed_p;
310 /* Length of the currently allocated uid_insn_cost array. */
312 static int max_uid_known;
314 /* The following array records the insn_cost for every insn
315 in the instruction stream. */
317 static int *uid_insn_cost;
319 /* The following array records the LOG_LINKS for every insn in the
320 instruction stream as struct insn_link pointers. */
322 struct insn_link {
323 rtx_insn *insn;
324 unsigned int regno;
325 struct insn_link *next;
328 static struct insn_link **uid_log_links;
330 static inline int
331 insn_uid_check (const_rtx insn)
333 int uid = INSN_UID (insn);
334 gcc_checking_assert (uid <= max_uid_known);
335 return uid;
338 #define INSN_COST(INSN) (uid_insn_cost[insn_uid_check (INSN)])
339 #define LOG_LINKS(INSN) (uid_log_links[insn_uid_check (INSN)])
341 #define FOR_EACH_LOG_LINK(L, INSN) \
342 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
344 /* Links for LOG_LINKS are allocated from this obstack. */
346 static struct obstack insn_link_obstack;
348 /* Allocate a link. */
350 static inline struct insn_link *
351 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
353 struct insn_link *l
354 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
355 sizeof (struct insn_link));
356 l->insn = insn;
357 l->regno = regno;
358 l->next = next;
359 return l;
362 /* Incremented for each basic block. */
364 static int label_tick;
366 /* Reset to label_tick for each extended basic block in scanning order. */
368 static int label_tick_ebb_start;
370 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
371 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
373 static scalar_int_mode nonzero_bits_mode;
375 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
376 be safely used. It is zero while computing them and after combine has
377 completed. This former test prevents propagating values based on
378 previously set values, which can be incorrect if a variable is modified
379 in a loop. */
381 static int nonzero_sign_valid;
384 /* Record one modification to rtl structure
385 to be undone by storing old_contents into *where. */
387 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
389 struct undo
391 struct undo *next;
392 enum undo_kind kind;
393 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
394 union { rtx *r; int *i; struct insn_link **l; } where;
397 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
398 num_undo says how many are currently recorded.
400 other_insn is nonzero if we have modified some other insn in the process
401 of working on subst_insn. It must be verified too. */
403 struct undobuf
405 struct undo *undos;
406 struct undo *frees;
407 rtx_insn *other_insn;
410 static struct undobuf undobuf;
412 /* Number of times the pseudo being substituted for
413 was found and replaced. */
415 static int n_occurrences;
417 static rtx reg_nonzero_bits_for_combine (const_rtx, scalar_int_mode,
418 scalar_int_mode,
419 unsigned HOST_WIDE_INT *);
420 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, scalar_int_mode,
421 scalar_int_mode,
422 unsigned int *);
423 static void do_SUBST (rtx *, rtx);
424 static void do_SUBST_INT (int *, int);
425 static void init_reg_last (void);
426 static void setup_incoming_promotions (rtx_insn *);
427 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
428 static int cant_combine_insn_p (rtx_insn *);
429 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
430 rtx_insn *, rtx_insn *, rtx *, rtx *);
431 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
432 static int contains_muldiv (rtx);
433 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
434 int *, rtx_insn *);
435 static void undo_all (void);
436 static void undo_commit (void);
437 static rtx *find_split_point (rtx *, rtx_insn *, bool);
438 static rtx subst (rtx, rtx, rtx, int, int, int);
439 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
440 static rtx simplify_if_then_else (rtx);
441 static rtx simplify_set (rtx);
442 static rtx simplify_logical (rtx);
443 static rtx expand_compound_operation (rtx);
444 static const_rtx expand_field_assignment (const_rtx);
445 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
446 rtx, unsigned HOST_WIDE_INT, int, int, int);
447 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
448 unsigned HOST_WIDE_INT *);
449 static rtx canon_reg_for_combine (rtx, rtx);
450 static rtx force_int_to_mode (rtx, scalar_int_mode, scalar_int_mode,
451 scalar_int_mode, unsigned HOST_WIDE_INT, int);
452 static rtx force_to_mode (rtx, machine_mode,
453 unsigned HOST_WIDE_INT, int);
454 static rtx if_then_else_cond (rtx, rtx *, rtx *);
455 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
456 static int rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
457 static rtx make_field_assignment (rtx);
458 static rtx apply_distributive_law (rtx);
459 static rtx distribute_and_simplify_rtx (rtx, int);
460 static rtx simplify_and_const_int_1 (scalar_int_mode, rtx,
461 unsigned HOST_WIDE_INT);
462 static rtx simplify_and_const_int (rtx, scalar_int_mode, rtx,
463 unsigned HOST_WIDE_INT);
464 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
465 HOST_WIDE_INT, machine_mode, int *);
466 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
467 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
468 int);
469 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
470 static rtx gen_lowpart_for_combine (machine_mode, rtx);
471 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
472 rtx, rtx *);
473 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
474 static void update_table_tick (rtx);
475 static void record_value_for_reg (rtx, rtx_insn *, rtx);
476 static void check_promoted_subreg (rtx_insn *, rtx);
477 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
478 static void record_dead_and_set_regs (rtx_insn *);
479 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
480 static rtx get_last_value (const_rtx);
481 static int use_crosses_set_p (const_rtx, int);
482 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
483 static int reg_dead_at_p (rtx, rtx_insn *);
484 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
485 static int reg_bitfield_target_p (rtx, rtx);
486 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
487 static void distribute_links (struct insn_link *);
488 static void mark_used_regs_combine (rtx);
489 static void record_promoted_value (rtx_insn *, rtx);
490 static bool unmentioned_reg_p (rtx, rtx);
491 static void record_truncated_values (rtx *, void *);
492 static bool reg_truncated_to_mode (machine_mode, const_rtx);
493 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
496 /* It is not safe to use ordinary gen_lowpart in combine.
497 See comments in gen_lowpart_for_combine. */
498 #undef RTL_HOOKS_GEN_LOWPART
499 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
501 /* Our implementation of gen_lowpart never emits a new pseudo. */
502 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
503 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
505 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
506 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
508 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
509 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
511 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
512 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
514 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
517 /* Convenience wrapper for the canonicalize_comparison target hook.
518 Target hooks cannot use enum rtx_code. */
519 static inline void
520 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
521 bool op0_preserve_value)
523 int code_int = (int)*code;
524 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
525 *code = (enum rtx_code)code_int;
528 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
529 PATTERN can not be split. Otherwise, it returns an insn sequence.
530 This is a wrapper around split_insns which ensures that the
531 reg_stat vector is made larger if the splitter creates a new
532 register. */
534 static rtx_insn *
535 combine_split_insns (rtx pattern, rtx_insn *insn)
537 rtx_insn *ret;
538 unsigned int nregs;
540 ret = split_insns (pattern, insn);
541 nregs = max_reg_num ();
542 if (nregs > reg_stat.length ())
543 reg_stat.safe_grow_cleared (nregs);
544 return ret;
547 /* This is used by find_single_use to locate an rtx in LOC that
548 contains exactly one use of DEST, which is typically either a REG
549 or CC0. It returns a pointer to the innermost rtx expression
550 containing DEST. Appearances of DEST that are being used to
551 totally replace it are not counted. */
553 static rtx *
554 find_single_use_1 (rtx dest, rtx *loc)
556 rtx x = *loc;
557 enum rtx_code code = GET_CODE (x);
558 rtx *result = NULL;
559 rtx *this_result;
560 int i;
561 const char *fmt;
563 switch (code)
565 case CONST:
566 case LABEL_REF:
567 case SYMBOL_REF:
568 CASE_CONST_ANY:
569 case CLOBBER:
570 return 0;
572 case SET:
573 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
574 of a REG that occupies all of the REG, the insn uses DEST if
575 it is mentioned in the destination or the source. Otherwise, we
576 need just check the source. */
577 if (GET_CODE (SET_DEST (x)) != CC0
578 && GET_CODE (SET_DEST (x)) != PC
579 && !REG_P (SET_DEST (x))
580 && ! (GET_CODE (SET_DEST (x)) == SUBREG
581 && REG_P (SUBREG_REG (SET_DEST (x)))
582 && !read_modify_subreg_p (SET_DEST (x))))
583 break;
585 return find_single_use_1 (dest, &SET_SRC (x));
587 case MEM:
588 case SUBREG:
589 return find_single_use_1 (dest, &XEXP (x, 0));
591 default:
592 break;
595 /* If it wasn't one of the common cases above, check each expression and
596 vector of this code. Look for a unique usage of DEST. */
598 fmt = GET_RTX_FORMAT (code);
599 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
601 if (fmt[i] == 'e')
603 if (dest == XEXP (x, i)
604 || (REG_P (dest) && REG_P (XEXP (x, i))
605 && REGNO (dest) == REGNO (XEXP (x, i))))
606 this_result = loc;
607 else
608 this_result = find_single_use_1 (dest, &XEXP (x, i));
610 if (result == NULL)
611 result = this_result;
612 else if (this_result)
613 /* Duplicate usage. */
614 return NULL;
616 else if (fmt[i] == 'E')
618 int j;
620 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
622 if (XVECEXP (x, i, j) == dest
623 || (REG_P (dest)
624 && REG_P (XVECEXP (x, i, j))
625 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
626 this_result = loc;
627 else
628 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
630 if (result == NULL)
631 result = this_result;
632 else if (this_result)
633 return NULL;
638 return result;
642 /* See if DEST, produced in INSN, is used only a single time in the
643 sequel. If so, return a pointer to the innermost rtx expression in which
644 it is used.
646 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
648 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
649 care about REG_DEAD notes or LOG_LINKS.
651 Otherwise, we find the single use by finding an insn that has a
652 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
653 only referenced once in that insn, we know that it must be the first
654 and last insn referencing DEST. */
656 static rtx *
657 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
659 basic_block bb;
660 rtx_insn *next;
661 rtx *result;
662 struct insn_link *link;
664 if (dest == cc0_rtx)
666 next = NEXT_INSN (insn);
667 if (next == 0
668 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
669 return 0;
671 result = find_single_use_1 (dest, &PATTERN (next));
672 if (result && ploc)
673 *ploc = next;
674 return result;
677 if (!REG_P (dest))
678 return 0;
680 bb = BLOCK_FOR_INSN (insn);
681 for (next = NEXT_INSN (insn);
682 next && BLOCK_FOR_INSN (next) == bb;
683 next = NEXT_INSN (next))
684 if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
686 FOR_EACH_LOG_LINK (link, next)
687 if (link->insn == insn && link->regno == REGNO (dest))
688 break;
690 if (link)
692 result = find_single_use_1 (dest, &PATTERN (next));
693 if (ploc)
694 *ploc = next;
695 return result;
699 return 0;
702 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
703 insn. The substitution can be undone by undo_all. If INTO is already
704 set to NEWVAL, do not record this change. Because computing NEWVAL might
705 also call SUBST, we have to compute it before we put anything into
706 the undo table. */
708 static void
709 do_SUBST (rtx *into, rtx newval)
711 struct undo *buf;
712 rtx oldval = *into;
714 if (oldval == newval)
715 return;
717 /* We'd like to catch as many invalid transformations here as
718 possible. Unfortunately, there are way too many mode changes
719 that are perfectly valid, so we'd waste too much effort for
720 little gain doing the checks here. Focus on catching invalid
721 transformations involving integer constants. */
722 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
723 && CONST_INT_P (newval))
725 /* Sanity check that we're replacing oldval with a CONST_INT
726 that is a valid sign-extension for the original mode. */
727 gcc_assert (INTVAL (newval)
728 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
730 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
731 CONST_INT is not valid, because after the replacement, the
732 original mode would be gone. Unfortunately, we can't tell
733 when do_SUBST is called to replace the operand thereof, so we
734 perform this test on oldval instead, checking whether an
735 invalid replacement took place before we got here. */
736 gcc_assert (!(GET_CODE (oldval) == SUBREG
737 && CONST_INT_P (SUBREG_REG (oldval))));
738 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
739 && CONST_INT_P (XEXP (oldval, 0))));
742 if (undobuf.frees)
743 buf = undobuf.frees, undobuf.frees = buf->next;
744 else
745 buf = XNEW (struct undo);
747 buf->kind = UNDO_RTX;
748 buf->where.r = into;
749 buf->old_contents.r = oldval;
750 *into = newval;
752 buf->next = undobuf.undos, undobuf.undos = buf;
755 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
757 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
758 for the value of a HOST_WIDE_INT value (including CONST_INT) is
759 not safe. */
761 static void
762 do_SUBST_INT (int *into, int newval)
764 struct undo *buf;
765 int oldval = *into;
767 if (oldval == newval)
768 return;
770 if (undobuf.frees)
771 buf = undobuf.frees, undobuf.frees = buf->next;
772 else
773 buf = XNEW (struct undo);
775 buf->kind = UNDO_INT;
776 buf->where.i = into;
777 buf->old_contents.i = oldval;
778 *into = newval;
780 buf->next = undobuf.undos, undobuf.undos = buf;
783 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
785 /* Similar to SUBST, but just substitute the mode. This is used when
786 changing the mode of a pseudo-register, so that any other
787 references to the entry in the regno_reg_rtx array will change as
788 well. */
790 static void
791 do_SUBST_MODE (rtx *into, machine_mode newval)
793 struct undo *buf;
794 machine_mode oldval = GET_MODE (*into);
796 if (oldval == newval)
797 return;
799 if (undobuf.frees)
800 buf = undobuf.frees, undobuf.frees = buf->next;
801 else
802 buf = XNEW (struct undo);
804 buf->kind = UNDO_MODE;
805 buf->where.r = into;
806 buf->old_contents.m = oldval;
807 adjust_reg_mode (*into, newval);
809 buf->next = undobuf.undos, undobuf.undos = buf;
812 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
814 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
816 static void
817 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
819 struct undo *buf;
820 struct insn_link * oldval = *into;
822 if (oldval == newval)
823 return;
825 if (undobuf.frees)
826 buf = undobuf.frees, undobuf.frees = buf->next;
827 else
828 buf = XNEW (struct undo);
830 buf->kind = UNDO_LINKS;
831 buf->where.l = into;
832 buf->old_contents.l = oldval;
833 *into = newval;
835 buf->next = undobuf.undos, undobuf.undos = buf;
838 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
840 /* Subroutine of try_combine. Determine whether the replacement patterns
841 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_cost
842 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
843 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
844 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
845 of all the instructions can be estimated and the replacements are more
846 expensive than the original sequence. */
848 static bool
849 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
850 rtx newpat, rtx newi2pat, rtx newotherpat)
852 int i0_cost, i1_cost, i2_cost, i3_cost;
853 int new_i2_cost, new_i3_cost;
854 int old_cost, new_cost;
856 /* Lookup the original insn_costs. */
857 i2_cost = INSN_COST (i2);
858 i3_cost = INSN_COST (i3);
860 if (i1)
862 i1_cost = INSN_COST (i1);
863 if (i0)
865 i0_cost = INSN_COST (i0);
866 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
867 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
869 else
871 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
872 ? i1_cost + i2_cost + i3_cost : 0);
873 i0_cost = 0;
876 else
878 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
879 i1_cost = i0_cost = 0;
882 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
883 correct that. */
884 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
885 old_cost -= i1_cost;
888 /* Calculate the replacement insn_costs. */
889 rtx tmp = PATTERN (i3);
890 PATTERN (i3) = newpat;
891 int tmpi = INSN_CODE (i3);
892 INSN_CODE (i3) = -1;
893 new_i3_cost = insn_cost (i3, optimize_this_for_speed_p);
894 PATTERN (i3) = tmp;
895 INSN_CODE (i3) = tmpi;
896 if (newi2pat)
898 tmp = PATTERN (i2);
899 PATTERN (i2) = newi2pat;
900 tmpi = INSN_CODE (i2);
901 INSN_CODE (i2) = -1;
902 new_i2_cost = insn_cost (i2, optimize_this_for_speed_p);
903 PATTERN (i2) = tmp;
904 INSN_CODE (i2) = tmpi;
905 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
906 ? new_i2_cost + new_i3_cost : 0;
908 else
910 new_cost = new_i3_cost;
911 new_i2_cost = 0;
914 if (undobuf.other_insn)
916 int old_other_cost, new_other_cost;
918 old_other_cost = INSN_COST (undobuf.other_insn);
919 tmp = PATTERN (undobuf.other_insn);
920 PATTERN (undobuf.other_insn) = newotherpat;
921 tmpi = INSN_CODE (undobuf.other_insn);
922 INSN_CODE (undobuf.other_insn) = -1;
923 new_other_cost = insn_cost (undobuf.other_insn,
924 optimize_this_for_speed_p);
925 PATTERN (undobuf.other_insn) = tmp;
926 INSN_CODE (undobuf.other_insn) = tmpi;
927 if (old_other_cost > 0 && new_other_cost > 0)
929 old_cost += old_other_cost;
930 new_cost += new_other_cost;
932 else
933 old_cost = 0;
936 /* Disallow this combination if both new_cost and old_cost are greater than
937 zero, and new_cost is greater than old cost. */
938 int reject = old_cost > 0 && new_cost > old_cost;
940 if (dump_file)
942 fprintf (dump_file, "%s combination of insns ",
943 reject ? "rejecting" : "allowing");
944 if (i0)
945 fprintf (dump_file, "%d, ", INSN_UID (i0));
946 if (i1 && INSN_UID (i1) != INSN_UID (i2))
947 fprintf (dump_file, "%d, ", INSN_UID (i1));
948 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
950 fprintf (dump_file, "original costs ");
951 if (i0)
952 fprintf (dump_file, "%d + ", i0_cost);
953 if (i1 && INSN_UID (i1) != INSN_UID (i2))
954 fprintf (dump_file, "%d + ", i1_cost);
955 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
957 if (newi2pat)
958 fprintf (dump_file, "replacement costs %d + %d = %d\n",
959 new_i2_cost, new_i3_cost, new_cost);
960 else
961 fprintf (dump_file, "replacement cost %d\n", new_cost);
964 if (reject)
965 return false;
967 /* Update the uid_insn_cost array with the replacement costs. */
968 INSN_COST (i2) = new_i2_cost;
969 INSN_COST (i3) = new_i3_cost;
970 if (i1)
972 INSN_COST (i1) = 0;
973 if (i0)
974 INSN_COST (i0) = 0;
977 return true;
981 /* Delete any insns that copy a register to itself. */
983 static void
984 delete_noop_moves (void)
986 rtx_insn *insn, *next;
987 basic_block bb;
989 FOR_EACH_BB_FN (bb, cfun)
991 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
993 next = NEXT_INSN (insn);
994 if (INSN_P (insn) && noop_move_p (insn))
996 if (dump_file)
997 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
999 delete_insn_and_edges (insn);
1006 /* Return false if we do not want to (or cannot) combine DEF. */
1007 static bool
1008 can_combine_def_p (df_ref def)
1010 /* Do not consider if it is pre/post modification in MEM. */
1011 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1012 return false;
1014 unsigned int regno = DF_REF_REGNO (def);
1016 /* Do not combine frame pointer adjustments. */
1017 if ((regno == FRAME_POINTER_REGNUM
1018 && (!reload_completed || frame_pointer_needed))
1019 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1020 && regno == HARD_FRAME_POINTER_REGNUM
1021 && (!reload_completed || frame_pointer_needed))
1022 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1023 && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1024 return false;
1026 return true;
1029 /* Return false if we do not want to (or cannot) combine USE. */
1030 static bool
1031 can_combine_use_p (df_ref use)
1033 /* Do not consider the usage of the stack pointer by function call. */
1034 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1035 return false;
1037 return true;
1040 /* Fill in log links field for all insns. */
1042 static void
1043 create_log_links (void)
1045 basic_block bb;
1046 rtx_insn **next_use;
1047 rtx_insn *insn;
1048 df_ref def, use;
1050 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1052 /* Pass through each block from the end, recording the uses of each
1053 register and establishing log links when def is encountered.
1054 Note that we do not clear next_use array in order to save time,
1055 so we have to test whether the use is in the same basic block as def.
1057 There are a few cases below when we do not consider the definition or
1058 usage -- these are taken from original flow.c did. Don't ask me why it is
1059 done this way; I don't know and if it works, I don't want to know. */
1061 FOR_EACH_BB_FN (bb, cfun)
1063 FOR_BB_INSNS_REVERSE (bb, insn)
1065 if (!NONDEBUG_INSN_P (insn))
1066 continue;
1068 /* Log links are created only once. */
1069 gcc_assert (!LOG_LINKS (insn));
1071 FOR_EACH_INSN_DEF (def, insn)
1073 unsigned int regno = DF_REF_REGNO (def);
1074 rtx_insn *use_insn;
1076 if (!next_use[regno])
1077 continue;
1079 if (!can_combine_def_p (def))
1080 continue;
1082 use_insn = next_use[regno];
1083 next_use[regno] = NULL;
1085 if (BLOCK_FOR_INSN (use_insn) != bb)
1086 continue;
1088 /* flow.c claimed:
1090 We don't build a LOG_LINK for hard registers contained
1091 in ASM_OPERANDs. If these registers get replaced,
1092 we might wind up changing the semantics of the insn,
1093 even if reload can make what appear to be valid
1094 assignments later. */
1095 if (regno < FIRST_PSEUDO_REGISTER
1096 && asm_noperands (PATTERN (use_insn)) >= 0)
1097 continue;
1099 /* Don't add duplicate links between instructions. */
1100 struct insn_link *links;
1101 FOR_EACH_LOG_LINK (links, use_insn)
1102 if (insn == links->insn && regno == links->regno)
1103 break;
1105 if (!links)
1106 LOG_LINKS (use_insn)
1107 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1110 FOR_EACH_INSN_USE (use, insn)
1111 if (can_combine_use_p (use))
1112 next_use[DF_REF_REGNO (use)] = insn;
1116 free (next_use);
1119 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1120 true if we found a LOG_LINK that proves that A feeds B. This only works
1121 if there are no instructions between A and B which could have a link
1122 depending on A, since in that case we would not record a link for B.
1123 We also check the implicit dependency created by a cc0 setter/user
1124 pair. */
1126 static bool
1127 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1129 struct insn_link *links;
1130 FOR_EACH_LOG_LINK (links, b)
1131 if (links->insn == a)
1132 return true;
1133 if (HAVE_cc0 && sets_cc0_p (a))
1134 return true;
1135 return false;
1138 /* Main entry point for combiner. F is the first insn of the function.
1139 NREGS is the first unused pseudo-reg number.
1141 Return nonzero if the combiner has turned an indirect jump
1142 instruction into a direct jump. */
1143 static int
1144 combine_instructions (rtx_insn *f, unsigned int nregs)
1146 rtx_insn *insn, *next;
1147 rtx_insn *prev;
1148 struct insn_link *links, *nextlinks;
1149 rtx_insn *first;
1150 basic_block last_bb;
1152 int new_direct_jump_p = 0;
1154 for (first = f; first && !NONDEBUG_INSN_P (first); )
1155 first = NEXT_INSN (first);
1156 if (!first)
1157 return 0;
1159 combine_attempts = 0;
1160 combine_merges = 0;
1161 combine_extras = 0;
1162 combine_successes = 0;
1164 rtl_hooks = combine_rtl_hooks;
1166 reg_stat.safe_grow_cleared (nregs);
1168 init_recog_no_volatile ();
1170 /* Allocate array for insn info. */
1171 max_uid_known = get_max_uid ();
1172 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1173 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1174 gcc_obstack_init (&insn_link_obstack);
1176 nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1178 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1179 problems when, for example, we have j <<= 1 in a loop. */
1181 nonzero_sign_valid = 0;
1182 label_tick = label_tick_ebb_start = 1;
1184 /* Scan all SETs and see if we can deduce anything about what
1185 bits are known to be zero for some registers and how many copies
1186 of the sign bit are known to exist for those registers.
1188 Also set any known values so that we can use it while searching
1189 for what bits are known to be set. */
1191 setup_incoming_promotions (first);
1192 /* Allow the entry block and the first block to fall into the same EBB.
1193 Conceptually the incoming promotions are assigned to the entry block. */
1194 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1196 create_log_links ();
1197 FOR_EACH_BB_FN (this_basic_block, cfun)
1199 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1200 last_call_luid = 0;
1201 mem_last_set = -1;
1203 label_tick++;
1204 if (!single_pred_p (this_basic_block)
1205 || single_pred (this_basic_block) != last_bb)
1206 label_tick_ebb_start = label_tick;
1207 last_bb = this_basic_block;
1209 FOR_BB_INSNS (this_basic_block, insn)
1210 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1212 rtx links;
1214 subst_low_luid = DF_INSN_LUID (insn);
1215 subst_insn = insn;
1217 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1218 insn);
1219 record_dead_and_set_regs (insn);
1221 if (AUTO_INC_DEC)
1222 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1223 if (REG_NOTE_KIND (links) == REG_INC)
1224 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1225 insn);
1227 /* Record the current insn_cost of this instruction. */
1228 if (NONJUMP_INSN_P (insn))
1229 INSN_COST (insn) = insn_cost (insn, optimize_this_for_speed_p);
1230 if (dump_file)
1232 fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1233 dump_insn_slim (dump_file, insn);
1238 nonzero_sign_valid = 1;
1240 /* Now scan all the insns in forward order. */
1241 label_tick = label_tick_ebb_start = 1;
1242 init_reg_last ();
1243 setup_incoming_promotions (first);
1244 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1245 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1247 FOR_EACH_BB_FN (this_basic_block, cfun)
1249 rtx_insn *last_combined_insn = NULL;
1251 /* Ignore instruction combination in basic blocks that are going to
1252 be removed as unreachable anyway. See PR82386. */
1253 if (EDGE_COUNT (this_basic_block->preds) == 0)
1254 continue;
1256 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1257 last_call_luid = 0;
1258 mem_last_set = -1;
1260 label_tick++;
1261 if (!single_pred_p (this_basic_block)
1262 || single_pred (this_basic_block) != last_bb)
1263 label_tick_ebb_start = label_tick;
1264 last_bb = this_basic_block;
1266 rtl_profile_for_bb (this_basic_block);
1267 for (insn = BB_HEAD (this_basic_block);
1268 insn != NEXT_INSN (BB_END (this_basic_block));
1269 insn = next ? next : NEXT_INSN (insn))
1271 next = 0;
1272 if (!NONDEBUG_INSN_P (insn))
1273 continue;
1275 while (last_combined_insn
1276 && (!NONDEBUG_INSN_P (last_combined_insn)
1277 || last_combined_insn->deleted ()))
1278 last_combined_insn = PREV_INSN (last_combined_insn);
1279 if (last_combined_insn == NULL_RTX
1280 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1281 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1282 last_combined_insn = insn;
1284 /* See if we know about function return values before this
1285 insn based upon SUBREG flags. */
1286 check_promoted_subreg (insn, PATTERN (insn));
1288 /* See if we can find hardregs and subreg of pseudos in
1289 narrower modes. This could help turning TRUNCATEs
1290 into SUBREGs. */
1291 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1293 /* Try this insn with each insn it links back to. */
1295 FOR_EACH_LOG_LINK (links, insn)
1296 if ((next = try_combine (insn, links->insn, NULL,
1297 NULL, &new_direct_jump_p,
1298 last_combined_insn)) != 0)
1300 statistics_counter_event (cfun, "two-insn combine", 1);
1301 goto retry;
1304 /* Try each sequence of three linked insns ending with this one. */
1306 if (max_combine >= 3)
1307 FOR_EACH_LOG_LINK (links, insn)
1309 rtx_insn *link = links->insn;
1311 /* If the linked insn has been replaced by a note, then there
1312 is no point in pursuing this chain any further. */
1313 if (NOTE_P (link))
1314 continue;
1316 FOR_EACH_LOG_LINK (nextlinks, link)
1317 if ((next = try_combine (insn, link, nextlinks->insn,
1318 NULL, &new_direct_jump_p,
1319 last_combined_insn)) != 0)
1321 statistics_counter_event (cfun, "three-insn combine", 1);
1322 goto retry;
1326 /* Try to combine a jump insn that uses CC0
1327 with a preceding insn that sets CC0, and maybe with its
1328 logical predecessor as well.
1329 This is how we make decrement-and-branch insns.
1330 We need this special code because data flow connections
1331 via CC0 do not get entered in LOG_LINKS. */
1333 if (HAVE_cc0
1334 && JUMP_P (insn)
1335 && (prev = prev_nonnote_insn (insn)) != 0
1336 && NONJUMP_INSN_P (prev)
1337 && sets_cc0_p (PATTERN (prev)))
1339 if ((next = try_combine (insn, prev, NULL, NULL,
1340 &new_direct_jump_p,
1341 last_combined_insn)) != 0)
1342 goto retry;
1344 FOR_EACH_LOG_LINK (nextlinks, prev)
1345 if ((next = try_combine (insn, prev, nextlinks->insn,
1346 NULL, &new_direct_jump_p,
1347 last_combined_insn)) != 0)
1348 goto retry;
1351 /* Do the same for an insn that explicitly references CC0. */
1352 if (HAVE_cc0 && NONJUMP_INSN_P (insn)
1353 && (prev = prev_nonnote_insn (insn)) != 0
1354 && NONJUMP_INSN_P (prev)
1355 && sets_cc0_p (PATTERN (prev))
1356 && GET_CODE (PATTERN (insn)) == SET
1357 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1359 if ((next = try_combine (insn, prev, NULL, NULL,
1360 &new_direct_jump_p,
1361 last_combined_insn)) != 0)
1362 goto retry;
1364 FOR_EACH_LOG_LINK (nextlinks, prev)
1365 if ((next = try_combine (insn, prev, nextlinks->insn,
1366 NULL, &new_direct_jump_p,
1367 last_combined_insn)) != 0)
1368 goto retry;
1371 /* Finally, see if any of the insns that this insn links to
1372 explicitly references CC0. If so, try this insn, that insn,
1373 and its predecessor if it sets CC0. */
1374 if (HAVE_cc0)
1376 FOR_EACH_LOG_LINK (links, insn)
1377 if (NONJUMP_INSN_P (links->insn)
1378 && GET_CODE (PATTERN (links->insn)) == SET
1379 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1380 && (prev = prev_nonnote_insn (links->insn)) != 0
1381 && NONJUMP_INSN_P (prev)
1382 && sets_cc0_p (PATTERN (prev))
1383 && (next = try_combine (insn, links->insn,
1384 prev, NULL, &new_direct_jump_p,
1385 last_combined_insn)) != 0)
1386 goto retry;
1389 /* Try combining an insn with two different insns whose results it
1390 uses. */
1391 if (max_combine >= 3)
1392 FOR_EACH_LOG_LINK (links, insn)
1393 for (nextlinks = links->next; nextlinks;
1394 nextlinks = nextlinks->next)
1395 if ((next = try_combine (insn, links->insn,
1396 nextlinks->insn, NULL,
1397 &new_direct_jump_p,
1398 last_combined_insn)) != 0)
1401 statistics_counter_event (cfun, "three-insn combine", 1);
1402 goto retry;
1405 /* Try four-instruction combinations. */
1406 if (max_combine >= 4)
1407 FOR_EACH_LOG_LINK (links, insn)
1409 struct insn_link *next1;
1410 rtx_insn *link = links->insn;
1412 /* If the linked insn has been replaced by a note, then there
1413 is no point in pursuing this chain any further. */
1414 if (NOTE_P (link))
1415 continue;
1417 FOR_EACH_LOG_LINK (next1, link)
1419 rtx_insn *link1 = next1->insn;
1420 if (NOTE_P (link1))
1421 continue;
1422 /* I0 -> I1 -> I2 -> I3. */
1423 FOR_EACH_LOG_LINK (nextlinks, link1)
1424 if ((next = try_combine (insn, link, link1,
1425 nextlinks->insn,
1426 &new_direct_jump_p,
1427 last_combined_insn)) != 0)
1429 statistics_counter_event (cfun, "four-insn combine", 1);
1430 goto retry;
1432 /* I0, I1 -> I2, I2 -> I3. */
1433 for (nextlinks = next1->next; nextlinks;
1434 nextlinks = nextlinks->next)
1435 if ((next = try_combine (insn, link, link1,
1436 nextlinks->insn,
1437 &new_direct_jump_p,
1438 last_combined_insn)) != 0)
1440 statistics_counter_event (cfun, "four-insn combine", 1);
1441 goto retry;
1445 for (next1 = links->next; next1; next1 = next1->next)
1447 rtx_insn *link1 = next1->insn;
1448 if (NOTE_P (link1))
1449 continue;
1450 /* I0 -> I2; I1, I2 -> I3. */
1451 FOR_EACH_LOG_LINK (nextlinks, link)
1452 if ((next = try_combine (insn, link, link1,
1453 nextlinks->insn,
1454 &new_direct_jump_p,
1455 last_combined_insn)) != 0)
1457 statistics_counter_event (cfun, "four-insn combine", 1);
1458 goto retry;
1460 /* I0 -> I1; I1, I2 -> I3. */
1461 FOR_EACH_LOG_LINK (nextlinks, link1)
1462 if ((next = try_combine (insn, link, link1,
1463 nextlinks->insn,
1464 &new_direct_jump_p,
1465 last_combined_insn)) != 0)
1467 statistics_counter_event (cfun, "four-insn combine", 1);
1468 goto retry;
1473 /* Try this insn with each REG_EQUAL note it links back to. */
1474 FOR_EACH_LOG_LINK (links, insn)
1476 rtx set, note;
1477 rtx_insn *temp = links->insn;
1478 if ((set = single_set (temp)) != 0
1479 && (note = find_reg_equal_equiv_note (temp)) != 0
1480 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1481 /* Avoid using a register that may already been marked
1482 dead by an earlier instruction. */
1483 && ! unmentioned_reg_p (note, SET_SRC (set))
1484 && (GET_MODE (note) == VOIDmode
1485 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1486 : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1487 && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1488 || (GET_MODE (XEXP (SET_DEST (set), 0))
1489 == GET_MODE (note))))))
1491 /* Temporarily replace the set's source with the
1492 contents of the REG_EQUAL note. The insn will
1493 be deleted or recognized by try_combine. */
1494 rtx orig_src = SET_SRC (set);
1495 rtx orig_dest = SET_DEST (set);
1496 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1497 SET_DEST (set) = XEXP (SET_DEST (set), 0);
1498 SET_SRC (set) = note;
1499 i2mod = temp;
1500 i2mod_old_rhs = copy_rtx (orig_src);
1501 i2mod_new_rhs = copy_rtx (note);
1502 next = try_combine (insn, i2mod, NULL, NULL,
1503 &new_direct_jump_p,
1504 last_combined_insn);
1505 i2mod = NULL;
1506 if (next)
1508 statistics_counter_event (cfun, "insn-with-note combine", 1);
1509 goto retry;
1511 SET_SRC (set) = orig_src;
1512 SET_DEST (set) = orig_dest;
1516 if (!NOTE_P (insn))
1517 record_dead_and_set_regs (insn);
1519 retry:
1524 default_rtl_profile ();
1525 clear_bb_flags ();
1526 new_direct_jump_p |= purge_all_dead_edges ();
1527 delete_noop_moves ();
1529 /* Clean up. */
1530 obstack_free (&insn_link_obstack, NULL);
1531 free (uid_log_links);
1532 free (uid_insn_cost);
1533 reg_stat.release ();
1536 struct undo *undo, *next;
1537 for (undo = undobuf.frees; undo; undo = next)
1539 next = undo->next;
1540 free (undo);
1542 undobuf.frees = 0;
1545 total_attempts += combine_attempts;
1546 total_merges += combine_merges;
1547 total_extras += combine_extras;
1548 total_successes += combine_successes;
1550 nonzero_sign_valid = 0;
1551 rtl_hooks = general_rtl_hooks;
1553 /* Make recognizer allow volatile MEMs again. */
1554 init_recog ();
1556 return new_direct_jump_p;
1559 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1561 static void
1562 init_reg_last (void)
1564 unsigned int i;
1565 reg_stat_type *p;
1567 FOR_EACH_VEC_ELT (reg_stat, i, p)
1568 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1571 /* Set up any promoted values for incoming argument registers. */
1573 static void
1574 setup_incoming_promotions (rtx_insn *first)
1576 tree arg;
1577 bool strictly_local = false;
1579 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1580 arg = DECL_CHAIN (arg))
1582 rtx x, reg = DECL_INCOMING_RTL (arg);
1583 int uns1, uns3;
1584 machine_mode mode1, mode2, mode3, mode4;
1586 /* Only continue if the incoming argument is in a register. */
1587 if (!REG_P (reg))
1588 continue;
1590 /* Determine, if possible, whether all call sites of the current
1591 function lie within the current compilation unit. (This does
1592 take into account the exporting of a function via taking its
1593 address, and so forth.) */
1594 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1596 /* The mode and signedness of the argument before any promotions happen
1597 (equal to the mode of the pseudo holding it at that stage). */
1598 mode1 = TYPE_MODE (TREE_TYPE (arg));
1599 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1601 /* The mode and signedness of the argument after any source language and
1602 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1603 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1604 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1606 /* The mode and signedness of the argument as it is actually passed,
1607 see assign_parm_setup_reg in function.c. */
1608 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1609 TREE_TYPE (cfun->decl), 0);
1611 /* The mode of the register in which the argument is being passed. */
1612 mode4 = GET_MODE (reg);
1614 /* Eliminate sign extensions in the callee when:
1615 (a) A mode promotion has occurred; */
1616 if (mode1 == mode3)
1617 continue;
1618 /* (b) The mode of the register is the same as the mode of
1619 the argument as it is passed; */
1620 if (mode3 != mode4)
1621 continue;
1622 /* (c) There's no language level extension; */
1623 if (mode1 == mode2)
1625 /* (c.1) All callers are from the current compilation unit. If that's
1626 the case we don't have to rely on an ABI, we only have to know
1627 what we're generating right now, and we know that we will do the
1628 mode1 to mode2 promotion with the given sign. */
1629 else if (!strictly_local)
1630 continue;
1631 /* (c.2) The combination of the two promotions is useful. This is
1632 true when the signs match, or if the first promotion is unsigned.
1633 In the later case, (sign_extend (zero_extend x)) is the same as
1634 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1635 else if (uns1)
1636 uns3 = true;
1637 else if (uns3)
1638 continue;
1640 /* Record that the value was promoted from mode1 to mode3,
1641 so that any sign extension at the head of the current
1642 function may be eliminated. */
1643 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1644 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1645 record_value_for_reg (reg, first, x);
1649 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1650 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1651 because some machines (maybe most) will actually do the sign-extension and
1652 this is the conservative approach.
1654 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1655 kludge. */
1657 static rtx
1658 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1660 scalar_int_mode int_mode;
1661 if (CONST_INT_P (src)
1662 && is_a <scalar_int_mode> (mode, &int_mode)
1663 && GET_MODE_PRECISION (int_mode) < prec
1664 && INTVAL (src) > 0
1665 && val_signbit_known_set_p (int_mode, INTVAL (src)))
1666 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1668 return src;
1671 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1672 and SET. */
1674 static void
1675 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1676 rtx x)
1678 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1679 unsigned HOST_WIDE_INT bits = 0;
1680 rtx reg_equal = NULL, src = SET_SRC (set);
1681 unsigned int num = 0;
1683 if (reg_equal_note)
1684 reg_equal = XEXP (reg_equal_note, 0);
1686 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1688 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1689 if (reg_equal)
1690 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1693 /* Don't call nonzero_bits if it cannot change anything. */
1694 if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1696 bits = nonzero_bits (src, nonzero_bits_mode);
1697 if (reg_equal && bits)
1698 bits &= nonzero_bits (reg_equal, nonzero_bits_mode);
1699 rsp->nonzero_bits |= bits;
1702 /* Don't call num_sign_bit_copies if it cannot change anything. */
1703 if (rsp->sign_bit_copies != 1)
1705 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1706 if (reg_equal && num != GET_MODE_PRECISION (GET_MODE (x)))
1708 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1709 if (num == 0 || numeq > num)
1710 num = numeq;
1712 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1713 rsp->sign_bit_copies = num;
1717 /* Called via note_stores. If X is a pseudo that is narrower than
1718 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1720 If we are setting only a portion of X and we can't figure out what
1721 portion, assume all bits will be used since we don't know what will
1722 be happening.
1724 Similarly, set how many bits of X are known to be copies of the sign bit
1725 at all locations in the function. This is the smallest number implied
1726 by any set of X. */
1728 static void
1729 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1731 rtx_insn *insn = (rtx_insn *) data;
1732 scalar_int_mode mode;
1734 if (REG_P (x)
1735 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1736 /* If this register is undefined at the start of the file, we can't
1737 say what its contents were. */
1738 && ! REGNO_REG_SET_P
1739 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1740 && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1741 && HWI_COMPUTABLE_MODE_P (mode))
1743 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1745 if (set == 0 || GET_CODE (set) == CLOBBER)
1747 rsp->nonzero_bits = GET_MODE_MASK (mode);
1748 rsp->sign_bit_copies = 1;
1749 return;
1752 /* If this register is being initialized using itself, and the
1753 register is uninitialized in this basic block, and there are
1754 no LOG_LINKS which set the register, then part of the
1755 register is uninitialized. In that case we can't assume
1756 anything about the number of nonzero bits.
1758 ??? We could do better if we checked this in
1759 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1760 could avoid making assumptions about the insn which initially
1761 sets the register, while still using the information in other
1762 insns. We would have to be careful to check every insn
1763 involved in the combination. */
1765 if (insn
1766 && reg_referenced_p (x, PATTERN (insn))
1767 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1768 REGNO (x)))
1770 struct insn_link *link;
1772 FOR_EACH_LOG_LINK (link, insn)
1773 if (dead_or_set_p (link->insn, x))
1774 break;
1775 if (!link)
1777 rsp->nonzero_bits = GET_MODE_MASK (mode);
1778 rsp->sign_bit_copies = 1;
1779 return;
1783 /* If this is a complex assignment, see if we can convert it into a
1784 simple assignment. */
1785 set = expand_field_assignment (set);
1787 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1788 set what we know about X. */
1790 if (SET_DEST (set) == x
1791 || (paradoxical_subreg_p (SET_DEST (set))
1792 && SUBREG_REG (SET_DEST (set)) == x))
1793 update_rsp_from_reg_equal (rsp, insn, set, x);
1794 else
1796 rsp->nonzero_bits = GET_MODE_MASK (mode);
1797 rsp->sign_bit_copies = 1;
1802 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1803 optionally insns that were previously combined into I3 or that will be
1804 combined into the merger of INSN and I3. The order is PRED, PRED2,
1805 INSN, SUCC, SUCC2, I3.
1807 Return 0 if the combination is not allowed for any reason.
1809 If the combination is allowed, *PDEST will be set to the single
1810 destination of INSN and *PSRC to the single source, and this function
1811 will return 1. */
1813 static int
1814 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1815 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1816 rtx *pdest, rtx *psrc)
1818 int i;
1819 const_rtx set = 0;
1820 rtx src, dest;
1821 rtx_insn *p;
1822 rtx link;
1823 bool all_adjacent = true;
1824 int (*is_volatile_p) (const_rtx);
1826 if (succ)
1828 if (succ2)
1830 if (next_active_insn (succ2) != i3)
1831 all_adjacent = false;
1832 if (next_active_insn (succ) != succ2)
1833 all_adjacent = false;
1835 else if (next_active_insn (succ) != i3)
1836 all_adjacent = false;
1837 if (next_active_insn (insn) != succ)
1838 all_adjacent = false;
1840 else if (next_active_insn (insn) != i3)
1841 all_adjacent = false;
1843 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1844 or a PARALLEL consisting of such a SET and CLOBBERs.
1846 If INSN has CLOBBER parallel parts, ignore them for our processing.
1847 By definition, these happen during the execution of the insn. When it
1848 is merged with another insn, all bets are off. If they are, in fact,
1849 needed and aren't also supplied in I3, they may be added by
1850 recog_for_combine. Otherwise, it won't match.
1852 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1853 note.
1855 Get the source and destination of INSN. If more than one, can't
1856 combine. */
1858 if (GET_CODE (PATTERN (insn)) == SET)
1859 set = PATTERN (insn);
1860 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1861 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1863 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1865 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1867 switch (GET_CODE (elt))
1869 /* This is important to combine floating point insns
1870 for the SH4 port. */
1871 case USE:
1872 /* Combining an isolated USE doesn't make sense.
1873 We depend here on combinable_i3pat to reject them. */
1874 /* The code below this loop only verifies that the inputs of
1875 the SET in INSN do not change. We call reg_set_between_p
1876 to verify that the REG in the USE does not change between
1877 I3 and INSN.
1878 If the USE in INSN was for a pseudo register, the matching
1879 insn pattern will likely match any register; combining this
1880 with any other USE would only be safe if we knew that the
1881 used registers have identical values, or if there was
1882 something to tell them apart, e.g. different modes. For
1883 now, we forgo such complicated tests and simply disallow
1884 combining of USES of pseudo registers with any other USE. */
1885 if (REG_P (XEXP (elt, 0))
1886 && GET_CODE (PATTERN (i3)) == PARALLEL)
1888 rtx i3pat = PATTERN (i3);
1889 int i = XVECLEN (i3pat, 0) - 1;
1890 unsigned int regno = REGNO (XEXP (elt, 0));
1894 rtx i3elt = XVECEXP (i3pat, 0, i);
1896 if (GET_CODE (i3elt) == USE
1897 && REG_P (XEXP (i3elt, 0))
1898 && (REGNO (XEXP (i3elt, 0)) == regno
1899 ? reg_set_between_p (XEXP (elt, 0),
1900 PREV_INSN (insn), i3)
1901 : regno >= FIRST_PSEUDO_REGISTER))
1902 return 0;
1904 while (--i >= 0);
1906 break;
1908 /* We can ignore CLOBBERs. */
1909 case CLOBBER:
1910 break;
1912 case SET:
1913 /* Ignore SETs whose result isn't used but not those that
1914 have side-effects. */
1915 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1916 && insn_nothrow_p (insn)
1917 && !side_effects_p (elt))
1918 break;
1920 /* If we have already found a SET, this is a second one and
1921 so we cannot combine with this insn. */
1922 if (set)
1923 return 0;
1925 set = elt;
1926 break;
1928 default:
1929 /* Anything else means we can't combine. */
1930 return 0;
1934 if (set == 0
1935 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1936 so don't do anything with it. */
1937 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1938 return 0;
1940 else
1941 return 0;
1943 if (set == 0)
1944 return 0;
1946 /* The simplification in expand_field_assignment may call back to
1947 get_last_value, so set safe guard here. */
1948 subst_low_luid = DF_INSN_LUID (insn);
1950 set = expand_field_assignment (set);
1951 src = SET_SRC (set), dest = SET_DEST (set);
1953 /* Do not eliminate user-specified register if it is in an
1954 asm input because we may break the register asm usage defined
1955 in GCC manual if allow to do so.
1956 Be aware that this may cover more cases than we expect but this
1957 should be harmless. */
1958 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1959 && extract_asm_operands (PATTERN (i3)))
1960 return 0;
1962 /* Don't eliminate a store in the stack pointer. */
1963 if (dest == stack_pointer_rtx
1964 /* Don't combine with an insn that sets a register to itself if it has
1965 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1966 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1967 /* Can't merge an ASM_OPERANDS. */
1968 || GET_CODE (src) == ASM_OPERANDS
1969 /* Can't merge a function call. */
1970 || GET_CODE (src) == CALL
1971 /* Don't eliminate a function call argument. */
1972 || (CALL_P (i3)
1973 && (find_reg_fusage (i3, USE, dest)
1974 || (REG_P (dest)
1975 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1976 && global_regs[REGNO (dest)])))
1977 /* Don't substitute into an incremented register. */
1978 || FIND_REG_INC_NOTE (i3, dest)
1979 || (succ && FIND_REG_INC_NOTE (succ, dest))
1980 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1981 /* Don't substitute into a non-local goto, this confuses CFG. */
1982 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1983 /* Make sure that DEST is not used after INSN but before SUCC, or
1984 after SUCC and before SUCC2, or after SUCC2 but before I3. */
1985 || (!all_adjacent
1986 && ((succ2
1987 && (reg_used_between_p (dest, succ2, i3)
1988 || reg_used_between_p (dest, succ, succ2)))
1989 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1990 || (succ
1991 /* SUCC and SUCC2 can be split halves from a PARALLEL; in
1992 that case SUCC is not in the insn stream, so use SUCC2
1993 instead for this test. */
1994 && reg_used_between_p (dest, insn,
1995 succ2
1996 && INSN_UID (succ) == INSN_UID (succ2)
1997 ? succ2 : succ))))
1998 /* Make sure that the value that is to be substituted for the register
1999 does not use any registers whose values alter in between. However,
2000 If the insns are adjacent, a use can't cross a set even though we
2001 think it might (this can happen for a sequence of insns each setting
2002 the same destination; last_set of that register might point to
2003 a NOTE). If INSN has a REG_EQUIV note, the register is always
2004 equivalent to the memory so the substitution is valid even if there
2005 are intervening stores. Also, don't move a volatile asm or
2006 UNSPEC_VOLATILE across any other insns. */
2007 || (! all_adjacent
2008 && (((!MEM_P (src)
2009 || ! find_reg_note (insn, REG_EQUIV, src))
2010 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
2011 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
2012 || GET_CODE (src) == UNSPEC_VOLATILE))
2013 /* Don't combine across a CALL_INSN, because that would possibly
2014 change whether the life span of some REGs crosses calls or not,
2015 and it is a pain to update that information.
2016 Exception: if source is a constant, moving it later can't hurt.
2017 Accept that as a special case. */
2018 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
2019 return 0;
2021 /* DEST must either be a REG or CC0. */
2022 if (REG_P (dest))
2024 /* If register alignment is being enforced for multi-word items in all
2025 cases except for parameters, it is possible to have a register copy
2026 insn referencing a hard register that is not allowed to contain the
2027 mode being copied and which would not be valid as an operand of most
2028 insns. Eliminate this problem by not combining with such an insn.
2030 Also, on some machines we don't want to extend the life of a hard
2031 register. */
2033 if (REG_P (src)
2034 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
2035 && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
2036 /* Don't extend the life of a hard register unless it is
2037 user variable (if we have few registers) or it can't
2038 fit into the desired register (meaning something special
2039 is going on).
2040 Also avoid substituting a return register into I3, because
2041 reload can't handle a conflict with constraints of other
2042 inputs. */
2043 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2044 && !targetm.hard_regno_mode_ok (REGNO (src),
2045 GET_MODE (src)))))
2046 return 0;
2048 else if (GET_CODE (dest) != CC0)
2049 return 0;
2052 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2053 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2054 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2056 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2058 /* If the clobber represents an earlyclobber operand, we must not
2059 substitute an expression containing the clobbered register.
2060 As we do not analyze the constraint strings here, we have to
2061 make the conservative assumption. However, if the register is
2062 a fixed hard reg, the clobber cannot represent any operand;
2063 we leave it up to the machine description to either accept or
2064 reject use-and-clobber patterns. */
2065 if (!REG_P (reg)
2066 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2067 || !fixed_regs[REGNO (reg)])
2068 if (reg_overlap_mentioned_p (reg, src))
2069 return 0;
2072 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2073 or not), reject, unless nothing volatile comes between it and I3 */
2075 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2077 /* Make sure neither succ nor succ2 contains a volatile reference. */
2078 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2079 return 0;
2080 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2081 return 0;
2082 /* We'll check insns between INSN and I3 below. */
2085 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2086 to be an explicit register variable, and was chosen for a reason. */
2088 if (GET_CODE (src) == ASM_OPERANDS
2089 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2090 return 0;
2092 /* If INSN contains volatile references (specifically volatile MEMs),
2093 we cannot combine across any other volatile references.
2094 Even if INSN doesn't contain volatile references, any intervening
2095 volatile insn might affect machine state. */
2097 is_volatile_p = volatile_refs_p (PATTERN (insn))
2098 ? volatile_refs_p
2099 : volatile_insn_p;
2101 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2102 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2103 return 0;
2105 /* If INSN contains an autoincrement or autodecrement, make sure that
2106 register is not used between there and I3, and not already used in
2107 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2108 Also insist that I3 not be a jump; if it were one
2109 and the incremented register were spilled, we would lose. */
2111 if (AUTO_INC_DEC)
2112 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2113 if (REG_NOTE_KIND (link) == REG_INC
2114 && (JUMP_P (i3)
2115 || reg_used_between_p (XEXP (link, 0), insn, i3)
2116 || (pred != NULL_RTX
2117 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2118 || (pred2 != NULL_RTX
2119 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2120 || (succ != NULL_RTX
2121 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2122 || (succ2 != NULL_RTX
2123 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2124 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2125 return 0;
2127 /* Don't combine an insn that follows a CC0-setting insn.
2128 An insn that uses CC0 must not be separated from the one that sets it.
2129 We do, however, allow I2 to follow a CC0-setting insn if that insn
2130 is passed as I1; in that case it will be deleted also.
2131 We also allow combining in this case if all the insns are adjacent
2132 because that would leave the two CC0 insns adjacent as well.
2133 It would be more logical to test whether CC0 occurs inside I1 or I2,
2134 but that would be much slower, and this ought to be equivalent. */
2136 if (HAVE_cc0)
2138 p = prev_nonnote_insn (insn);
2139 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2140 && ! all_adjacent)
2141 return 0;
2144 /* If we get here, we have passed all the tests and the combination is
2145 to be allowed. */
2147 *pdest = dest;
2148 *psrc = src;
2150 return 1;
2153 /* LOC is the location within I3 that contains its pattern or the component
2154 of a PARALLEL of the pattern. We validate that it is valid for combining.
2156 One problem is if I3 modifies its output, as opposed to replacing it
2157 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2158 doing so would produce an insn that is not equivalent to the original insns.
2160 Consider:
2162 (set (reg:DI 101) (reg:DI 100))
2163 (set (subreg:SI (reg:DI 101) 0) <foo>)
2165 This is NOT equivalent to:
2167 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2168 (set (reg:DI 101) (reg:DI 100))])
2170 Not only does this modify 100 (in which case it might still be valid
2171 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2173 We can also run into a problem if I2 sets a register that I1
2174 uses and I1 gets directly substituted into I3 (not via I2). In that
2175 case, we would be getting the wrong value of I2DEST into I3, so we
2176 must reject the combination. This case occurs when I2 and I1 both
2177 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2178 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2179 of a SET must prevent combination from occurring. The same situation
2180 can occur for I0, in which case I0_NOT_IN_SRC is set.
2182 Before doing the above check, we first try to expand a field assignment
2183 into a set of logical operations.
2185 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2186 we place a register that is both set and used within I3. If more than one
2187 such register is detected, we fail.
2189 Return 1 if the combination is valid, zero otherwise. */
2191 static int
2192 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2193 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2195 rtx x = *loc;
2197 if (GET_CODE (x) == SET)
2199 rtx set = x ;
2200 rtx dest = SET_DEST (set);
2201 rtx src = SET_SRC (set);
2202 rtx inner_dest = dest;
2203 rtx subdest;
2205 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2206 || GET_CODE (inner_dest) == SUBREG
2207 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2208 inner_dest = XEXP (inner_dest, 0);
2210 /* Check for the case where I3 modifies its output, as discussed
2211 above. We don't want to prevent pseudos from being combined
2212 into the address of a MEM, so only prevent the combination if
2213 i1 or i2 set the same MEM. */
2214 if ((inner_dest != dest &&
2215 (!MEM_P (inner_dest)
2216 || rtx_equal_p (i2dest, inner_dest)
2217 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2218 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2219 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2220 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2221 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2223 /* This is the same test done in can_combine_p except we can't test
2224 all_adjacent; we don't have to, since this instruction will stay
2225 in place, thus we are not considering increasing the lifetime of
2226 INNER_DEST.
2228 Also, if this insn sets a function argument, combining it with
2229 something that might need a spill could clobber a previous
2230 function argument; the all_adjacent test in can_combine_p also
2231 checks this; here, we do a more specific test for this case. */
2233 || (REG_P (inner_dest)
2234 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2235 && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2236 GET_MODE (inner_dest)))
2237 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2238 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2239 return 0;
2241 /* If DEST is used in I3, it is being killed in this insn, so
2242 record that for later. We have to consider paradoxical
2243 subregs here, since they kill the whole register, but we
2244 ignore partial subregs, STRICT_LOW_PART, etc.
2245 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2246 STACK_POINTER_REGNUM, since these are always considered to be
2247 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2248 subdest = dest;
2249 if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2250 subdest = SUBREG_REG (subdest);
2251 if (pi3dest_killed
2252 && REG_P (subdest)
2253 && reg_referenced_p (subdest, PATTERN (i3))
2254 && REGNO (subdest) != FRAME_POINTER_REGNUM
2255 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2256 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2257 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2258 || (REGNO (subdest) != ARG_POINTER_REGNUM
2259 || ! fixed_regs [REGNO (subdest)]))
2260 && REGNO (subdest) != STACK_POINTER_REGNUM)
2262 if (*pi3dest_killed)
2263 return 0;
2265 *pi3dest_killed = subdest;
2269 else if (GET_CODE (x) == PARALLEL)
2271 int i;
2273 for (i = 0; i < XVECLEN (x, 0); i++)
2274 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2275 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2276 return 0;
2279 return 1;
2282 /* Return 1 if X is an arithmetic expression that contains a multiplication
2283 and division. We don't count multiplications by powers of two here. */
2285 static int
2286 contains_muldiv (rtx x)
2288 switch (GET_CODE (x))
2290 case MOD: case DIV: case UMOD: case UDIV:
2291 return 1;
2293 case MULT:
2294 return ! (CONST_INT_P (XEXP (x, 1))
2295 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2296 default:
2297 if (BINARY_P (x))
2298 return contains_muldiv (XEXP (x, 0))
2299 || contains_muldiv (XEXP (x, 1));
2301 if (UNARY_P (x))
2302 return contains_muldiv (XEXP (x, 0));
2304 return 0;
2308 /* Determine whether INSN can be used in a combination. Return nonzero if
2309 not. This is used in try_combine to detect early some cases where we
2310 can't perform combinations. */
2312 static int
2313 cant_combine_insn_p (rtx_insn *insn)
2315 rtx set;
2316 rtx src, dest;
2318 /* If this isn't really an insn, we can't do anything.
2319 This can occur when flow deletes an insn that it has merged into an
2320 auto-increment address. */
2321 if (!NONDEBUG_INSN_P (insn))
2322 return 1;
2324 /* Never combine loads and stores involving hard regs that are likely
2325 to be spilled. The register allocator can usually handle such
2326 reg-reg moves by tying. If we allow the combiner to make
2327 substitutions of likely-spilled regs, reload might die.
2328 As an exception, we allow combinations involving fixed regs; these are
2329 not available to the register allocator so there's no risk involved. */
2331 set = single_set (insn);
2332 if (! set)
2333 return 0;
2334 src = SET_SRC (set);
2335 dest = SET_DEST (set);
2336 if (GET_CODE (src) == SUBREG)
2337 src = SUBREG_REG (src);
2338 if (GET_CODE (dest) == SUBREG)
2339 dest = SUBREG_REG (dest);
2340 if (REG_P (src) && REG_P (dest)
2341 && ((HARD_REGISTER_P (src)
2342 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2343 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2344 || (HARD_REGISTER_P (dest)
2345 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2346 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2347 return 1;
2349 return 0;
2352 struct likely_spilled_retval_info
2354 unsigned regno, nregs;
2355 unsigned mask;
2358 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2359 hard registers that are known to be written to / clobbered in full. */
2360 static void
2361 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2363 struct likely_spilled_retval_info *const info =
2364 (struct likely_spilled_retval_info *) data;
2365 unsigned regno, nregs;
2366 unsigned new_mask;
2368 if (!REG_P (XEXP (set, 0)))
2369 return;
2370 regno = REGNO (x);
2371 if (regno >= info->regno + info->nregs)
2372 return;
2373 nregs = REG_NREGS (x);
2374 if (regno + nregs <= info->regno)
2375 return;
2376 new_mask = (2U << (nregs - 1)) - 1;
2377 if (regno < info->regno)
2378 new_mask >>= info->regno - regno;
2379 else
2380 new_mask <<= regno - info->regno;
2381 info->mask &= ~new_mask;
2384 /* Return nonzero iff part of the return value is live during INSN, and
2385 it is likely spilled. This can happen when more than one insn is needed
2386 to copy the return value, e.g. when we consider to combine into the
2387 second copy insn for a complex value. */
2389 static int
2390 likely_spilled_retval_p (rtx_insn *insn)
2392 rtx_insn *use = BB_END (this_basic_block);
2393 rtx reg;
2394 rtx_insn *p;
2395 unsigned regno, nregs;
2396 /* We assume here that no machine mode needs more than
2397 32 hard registers when the value overlaps with a register
2398 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2399 unsigned mask;
2400 struct likely_spilled_retval_info info;
2402 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2403 return 0;
2404 reg = XEXP (PATTERN (use), 0);
2405 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2406 return 0;
2407 regno = REGNO (reg);
2408 nregs = REG_NREGS (reg);
2409 if (nregs == 1)
2410 return 0;
2411 mask = (2U << (nregs - 1)) - 1;
2413 /* Disregard parts of the return value that are set later. */
2414 info.regno = regno;
2415 info.nregs = nregs;
2416 info.mask = mask;
2417 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2418 if (INSN_P (p))
2419 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2420 mask = info.mask;
2422 /* Check if any of the (probably) live return value registers is
2423 likely spilled. */
2424 nregs --;
2427 if ((mask & 1 << nregs)
2428 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2429 return 1;
2430 } while (nregs--);
2431 return 0;
2434 /* Adjust INSN after we made a change to its destination.
2436 Changing the destination can invalidate notes that say something about
2437 the results of the insn and a LOG_LINK pointing to the insn. */
2439 static void
2440 adjust_for_new_dest (rtx_insn *insn)
2442 /* For notes, be conservative and simply remove them. */
2443 remove_reg_equal_equiv_notes (insn);
2445 /* The new insn will have a destination that was previously the destination
2446 of an insn just above it. Call distribute_links to make a LOG_LINK from
2447 the next use of that destination. */
2449 rtx set = single_set (insn);
2450 gcc_assert (set);
2452 rtx reg = SET_DEST (set);
2454 while (GET_CODE (reg) == ZERO_EXTRACT
2455 || GET_CODE (reg) == STRICT_LOW_PART
2456 || GET_CODE (reg) == SUBREG)
2457 reg = XEXP (reg, 0);
2458 gcc_assert (REG_P (reg));
2460 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2462 df_insn_rescan (insn);
2465 /* Return TRUE if combine can reuse reg X in mode MODE.
2466 ADDED_SETS is nonzero if the original set is still required. */
2467 static bool
2468 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2470 unsigned int regno;
2472 if (!REG_P (x))
2473 return false;
2475 /* Don't change between modes with different underlying register sizes,
2476 since this could lead to invalid subregs. */
2477 if (REGMODE_NATURAL_SIZE (mode)
2478 != REGMODE_NATURAL_SIZE (GET_MODE (x)))
2479 return false;
2481 regno = REGNO (x);
2482 /* Allow hard registers if the new mode is legal, and occupies no more
2483 registers than the old mode. */
2484 if (regno < FIRST_PSEUDO_REGISTER)
2485 return (targetm.hard_regno_mode_ok (regno, mode)
2486 && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2488 /* Or a pseudo that is only used once. */
2489 return (regno < reg_n_sets_max
2490 && REG_N_SETS (regno) == 1
2491 && !added_sets
2492 && !REG_USERVAR_P (x));
2496 /* Check whether X, the destination of a set, refers to part of
2497 the register specified by REG. */
2499 static bool
2500 reg_subword_p (rtx x, rtx reg)
2502 /* Check that reg is an integer mode register. */
2503 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2504 return false;
2506 if (GET_CODE (x) == STRICT_LOW_PART
2507 || GET_CODE (x) == ZERO_EXTRACT)
2508 x = XEXP (x, 0);
2510 return GET_CODE (x) == SUBREG
2511 && SUBREG_REG (x) == reg
2512 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2515 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2516 Note that the INSN should be deleted *after* removing dead edges, so
2517 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2518 but not for a (set (pc) (label_ref FOO)). */
2520 static void
2521 update_cfg_for_uncondjump (rtx_insn *insn)
2523 basic_block bb = BLOCK_FOR_INSN (insn);
2524 gcc_assert (BB_END (bb) == insn);
2526 purge_dead_edges (bb);
2528 delete_insn (insn);
2529 if (EDGE_COUNT (bb->succs) == 1)
2531 rtx_insn *insn;
2533 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2535 /* Remove barriers from the footer if there are any. */
2536 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2537 if (BARRIER_P (insn))
2539 if (PREV_INSN (insn))
2540 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2541 else
2542 BB_FOOTER (bb) = NEXT_INSN (insn);
2543 if (NEXT_INSN (insn))
2544 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2546 else if (LABEL_P (insn))
2547 break;
2551 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2552 by an arbitrary number of CLOBBERs. */
2553 static bool
2554 is_parallel_of_n_reg_sets (rtx pat, int n)
2556 if (GET_CODE (pat) != PARALLEL)
2557 return false;
2559 int len = XVECLEN (pat, 0);
2560 if (len < n)
2561 return false;
2563 int i;
2564 for (i = 0; i < n; i++)
2565 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2566 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2567 return false;
2568 for ( ; i < len; i++)
2569 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER
2570 || XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2571 return false;
2573 return true;
2576 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2577 CLOBBERs), can be split into individual SETs in that order, without
2578 changing semantics. */
2579 static bool
2580 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2582 if (!insn_nothrow_p (insn))
2583 return false;
2585 rtx pat = PATTERN (insn);
2587 int i, j;
2588 for (i = 0; i < n; i++)
2590 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2591 return false;
2593 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2595 for (j = i + 1; j < n; j++)
2596 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2597 return false;
2600 return true;
2603 /* Try to combine the insns I0, I1 and I2 into I3.
2604 Here I0, I1 and I2 appear earlier than I3.
2605 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2608 If we are combining more than two insns and the resulting insn is not
2609 recognized, try splitting it into two insns. If that happens, I2 and I3
2610 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2611 Otherwise, I0, I1 and I2 are pseudo-deleted.
2613 Return 0 if the combination does not work. Then nothing is changed.
2614 If we did the combination, return the insn at which combine should
2615 resume scanning.
2617 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2618 new direct jump instruction.
2620 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2621 been I3 passed to an earlier try_combine within the same basic
2622 block. */
2624 static rtx_insn *
2625 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2626 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2628 /* New patterns for I3 and I2, respectively. */
2629 rtx newpat, newi2pat = 0;
2630 rtvec newpat_vec_with_clobbers = 0;
2631 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2632 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2633 dead. */
2634 int added_sets_0, added_sets_1, added_sets_2;
2635 /* Total number of SETs to put into I3. */
2636 int total_sets;
2637 /* Nonzero if I2's or I1's body now appears in I3. */
2638 int i2_is_used = 0, i1_is_used = 0;
2639 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2640 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2641 /* Contains I3 if the destination of I3 is used in its source, which means
2642 that the old life of I3 is being killed. If that usage is placed into
2643 I2 and not in I3, a REG_DEAD note must be made. */
2644 rtx i3dest_killed = 0;
2645 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2646 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2647 /* Copy of SET_SRC of I1 and I0, if needed. */
2648 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2649 /* Set if I2DEST was reused as a scratch register. */
2650 bool i2scratch = false;
2651 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2652 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2653 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2654 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2655 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2656 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2657 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2658 /* Notes that must be added to REG_NOTES in I3 and I2. */
2659 rtx new_i3_notes, new_i2_notes;
2660 /* Notes that we substituted I3 into I2 instead of the normal case. */
2661 int i3_subst_into_i2 = 0;
2662 /* Notes that I1, I2 or I3 is a MULT operation. */
2663 int have_mult = 0;
2664 int swap_i2i3 = 0;
2665 int changed_i3_dest = 0;
2667 int maxreg;
2668 rtx_insn *temp_insn;
2669 rtx temp_expr;
2670 struct insn_link *link;
2671 rtx other_pat = 0;
2672 rtx new_other_notes;
2673 int i;
2674 scalar_int_mode dest_mode, temp_mode;
2676 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2677 never be). */
2678 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2679 return 0;
2681 /* Only try four-insn combinations when there's high likelihood of
2682 success. Look for simple insns, such as loads of constants or
2683 binary operations involving a constant. */
2684 if (i0)
2686 int i;
2687 int ngood = 0;
2688 int nshift = 0;
2689 rtx set0, set3;
2691 if (!flag_expensive_optimizations)
2692 return 0;
2694 for (i = 0; i < 4; i++)
2696 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2697 rtx set = single_set (insn);
2698 rtx src;
2699 if (!set)
2700 continue;
2701 src = SET_SRC (set);
2702 if (CONSTANT_P (src))
2704 ngood += 2;
2705 break;
2707 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2708 ngood++;
2709 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2710 || GET_CODE (src) == LSHIFTRT)
2711 nshift++;
2714 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2715 are likely manipulating its value. Ideally we'll be able to combine
2716 all four insns into a bitfield insertion of some kind.
2718 Note the source in I0 might be inside a sign/zero extension and the
2719 memory modes in I0 and I3 might be different. So extract the address
2720 from the destination of I3 and search for it in the source of I0.
2722 In the event that there's a match but the source/dest do not actually
2723 refer to the same memory, the worst that happens is we try some
2724 combinations that we wouldn't have otherwise. */
2725 if ((set0 = single_set (i0))
2726 /* Ensure the source of SET0 is a MEM, possibly buried inside
2727 an extension. */
2728 && (GET_CODE (SET_SRC (set0)) == MEM
2729 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2730 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2731 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2732 && (set3 = single_set (i3))
2733 /* Ensure the destination of SET3 is a MEM. */
2734 && GET_CODE (SET_DEST (set3)) == MEM
2735 /* Would it be better to extract the base address for the MEM
2736 in SET3 and look for that? I don't have cases where it matters
2737 but I could envision such cases. */
2738 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2739 ngood += 2;
2741 if (ngood < 2 && nshift < 2)
2742 return 0;
2745 /* Exit early if one of the insns involved can't be used for
2746 combinations. */
2747 if (CALL_P (i2)
2748 || (i1 && CALL_P (i1))
2749 || (i0 && CALL_P (i0))
2750 || cant_combine_insn_p (i3)
2751 || cant_combine_insn_p (i2)
2752 || (i1 && cant_combine_insn_p (i1))
2753 || (i0 && cant_combine_insn_p (i0))
2754 || likely_spilled_retval_p (i3))
2755 return 0;
2757 combine_attempts++;
2758 undobuf.other_insn = 0;
2760 /* Reset the hard register usage information. */
2761 CLEAR_HARD_REG_SET (newpat_used_regs);
2763 if (dump_file && (dump_flags & TDF_DETAILS))
2765 if (i0)
2766 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2767 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2768 else if (i1)
2769 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2770 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2771 else
2772 fprintf (dump_file, "\nTrying %d -> %d:\n",
2773 INSN_UID (i2), INSN_UID (i3));
2776 /* If multiple insns feed into one of I2 or I3, they can be in any
2777 order. To simplify the code below, reorder them in sequence. */
2778 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2779 std::swap (i0, i2);
2780 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2781 std::swap (i0, i1);
2782 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2783 std::swap (i1, i2);
2785 added_links_insn = 0;
2787 /* First check for one important special case that the code below will
2788 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2789 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2790 we may be able to replace that destination with the destination of I3.
2791 This occurs in the common code where we compute both a quotient and
2792 remainder into a structure, in which case we want to do the computation
2793 directly into the structure to avoid register-register copies.
2795 Note that this case handles both multiple sets in I2 and also cases
2796 where I2 has a number of CLOBBERs inside the PARALLEL.
2798 We make very conservative checks below and only try to handle the
2799 most common cases of this. For example, we only handle the case
2800 where I2 and I3 are adjacent to avoid making difficult register
2801 usage tests. */
2803 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2804 && REG_P (SET_SRC (PATTERN (i3)))
2805 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2806 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2807 && GET_CODE (PATTERN (i2)) == PARALLEL
2808 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2809 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2810 below would need to check what is inside (and reg_overlap_mentioned_p
2811 doesn't support those codes anyway). Don't allow those destinations;
2812 the resulting insn isn't likely to be recognized anyway. */
2813 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2814 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2815 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2816 SET_DEST (PATTERN (i3)))
2817 && next_active_insn (i2) == i3)
2819 rtx p2 = PATTERN (i2);
2821 /* Make sure that the destination of I3,
2822 which we are going to substitute into one output of I2,
2823 is not used within another output of I2. We must avoid making this:
2824 (parallel [(set (mem (reg 69)) ...)
2825 (set (reg 69) ...)])
2826 which is not well-defined as to order of actions.
2827 (Besides, reload can't handle output reloads for this.)
2829 The problem can also happen if the dest of I3 is a memory ref,
2830 if another dest in I2 is an indirect memory ref.
2832 Neither can this PARALLEL be an asm. We do not allow combining
2833 that usually (see can_combine_p), so do not here either. */
2834 bool ok = true;
2835 for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2837 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2838 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2839 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2840 SET_DEST (XVECEXP (p2, 0, i))))
2841 ok = false;
2842 else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2843 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2844 ok = false;
2847 if (ok)
2848 for (i = 0; i < XVECLEN (p2, 0); i++)
2849 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2850 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2852 combine_merges++;
2854 subst_insn = i3;
2855 subst_low_luid = DF_INSN_LUID (i2);
2857 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2858 i2src = SET_SRC (XVECEXP (p2, 0, i));
2859 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2860 i2dest_killed = dead_or_set_p (i2, i2dest);
2862 /* Replace the dest in I2 with our dest and make the resulting
2863 insn the new pattern for I3. Then skip to where we validate
2864 the pattern. Everything was set up above. */
2865 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2866 newpat = p2;
2867 i3_subst_into_i2 = 1;
2868 goto validate_replacement;
2872 /* If I2 is setting a pseudo to a constant and I3 is setting some
2873 sub-part of it to another constant, merge them by making a new
2874 constant. */
2875 if (i1 == 0
2876 && (temp_expr = single_set (i2)) != 0
2877 && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2878 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2879 && GET_CODE (PATTERN (i3)) == SET
2880 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2881 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2883 rtx dest = SET_DEST (PATTERN (i3));
2884 rtx temp_dest = SET_DEST (temp_expr);
2885 int offset = -1;
2886 int width = 0;
2888 if (GET_CODE (dest) == ZERO_EXTRACT)
2890 if (CONST_INT_P (XEXP (dest, 1))
2891 && CONST_INT_P (XEXP (dest, 2))
2892 && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2893 &dest_mode))
2895 width = INTVAL (XEXP (dest, 1));
2896 offset = INTVAL (XEXP (dest, 2));
2897 dest = XEXP (dest, 0);
2898 if (BITS_BIG_ENDIAN)
2899 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2902 else
2904 if (GET_CODE (dest) == STRICT_LOW_PART)
2905 dest = XEXP (dest, 0);
2906 if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2908 width = GET_MODE_PRECISION (dest_mode);
2909 offset = 0;
2913 if (offset >= 0)
2915 /* If this is the low part, we're done. */
2916 if (subreg_lowpart_p (dest))
2918 /* Handle the case where inner is twice the size of outer. */
2919 else if (GET_MODE_PRECISION (temp_mode)
2920 == 2 * GET_MODE_PRECISION (dest_mode))
2921 offset += GET_MODE_PRECISION (dest_mode);
2922 /* Otherwise give up for now. */
2923 else
2924 offset = -1;
2927 if (offset >= 0)
2929 rtx inner = SET_SRC (PATTERN (i3));
2930 rtx outer = SET_SRC (temp_expr);
2932 wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2933 rtx_mode_t (inner, dest_mode),
2934 offset, width);
2936 combine_merges++;
2937 subst_insn = i3;
2938 subst_low_luid = DF_INSN_LUID (i2);
2939 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2940 i2dest = temp_dest;
2941 i2dest_killed = dead_or_set_p (i2, i2dest);
2943 /* Replace the source in I2 with the new constant and make the
2944 resulting insn the new pattern for I3. Then skip to where we
2945 validate the pattern. Everything was set up above. */
2946 SUBST (SET_SRC (temp_expr),
2947 immed_wide_int_const (o, temp_mode));
2949 newpat = PATTERN (i2);
2951 /* The dest of I3 has been replaced with the dest of I2. */
2952 changed_i3_dest = 1;
2953 goto validate_replacement;
2957 /* If we have no I1 and I2 looks like:
2958 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2959 (set Y OP)])
2960 make up a dummy I1 that is
2961 (set Y OP)
2962 and change I2 to be
2963 (set (reg:CC X) (compare:CC Y (const_int 0)))
2965 (We can ignore any trailing CLOBBERs.)
2967 This undoes a previous combination and allows us to match a branch-and-
2968 decrement insn. */
2970 if (!HAVE_cc0 && i1 == 0
2971 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2972 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2973 == MODE_CC)
2974 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2975 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2976 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2977 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2978 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2979 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2981 /* We make I1 with the same INSN_UID as I2. This gives it
2982 the same DF_INSN_LUID for value tracking. Our fake I1 will
2983 never appear in the insn stream so giving it the same INSN_UID
2984 as I2 will not cause a problem. */
2986 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2987 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2988 -1, NULL_RTX);
2989 INSN_UID (i1) = INSN_UID (i2);
2991 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2992 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2993 SET_DEST (PATTERN (i1)));
2994 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2995 SUBST_LINK (LOG_LINKS (i2),
2996 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2999 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
3000 make those two SETs separate I1 and I2 insns, and make an I0 that is
3001 the original I1. */
3002 if (!HAVE_cc0 && i0 == 0
3003 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3004 && can_split_parallel_of_n_reg_sets (i2, 2)
3005 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3006 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3008 /* If there is no I1, there is no I0 either. */
3009 i0 = i1;
3011 /* We make I1 with the same INSN_UID as I2. This gives it
3012 the same DF_INSN_LUID for value tracking. Our fake I1 will
3013 never appear in the insn stream so giving it the same INSN_UID
3014 as I2 will not cause a problem. */
3016 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3017 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
3018 -1, NULL_RTX);
3019 INSN_UID (i1) = INSN_UID (i2);
3021 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
3024 /* Verify that I2 and I1 are valid for combining. */
3025 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
3026 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
3027 &i1dest, &i1src))
3028 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
3029 &i0dest, &i0src)))
3031 undo_all ();
3032 return 0;
3035 /* Record whether I2DEST is used in I2SRC and similarly for the other
3036 cases. Knowing this will help in register status updating below. */
3037 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3038 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3039 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3040 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3041 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3042 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3043 i2dest_killed = dead_or_set_p (i2, i2dest);
3044 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3045 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3047 /* For the earlier insns, determine which of the subsequent ones they
3048 feed. */
3049 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3050 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3051 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3052 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3053 && reg_overlap_mentioned_p (i0dest, i2src))));
3055 /* Ensure that I3's pattern can be the destination of combines. */
3056 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3057 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3058 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3059 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3060 &i3dest_killed))
3062 undo_all ();
3063 return 0;
3066 /* See if any of the insns is a MULT operation. Unless one is, we will
3067 reject a combination that is, since it must be slower. Be conservative
3068 here. */
3069 if (GET_CODE (i2src) == MULT
3070 || (i1 != 0 && GET_CODE (i1src) == MULT)
3071 || (i0 != 0 && GET_CODE (i0src) == MULT)
3072 || (GET_CODE (PATTERN (i3)) == SET
3073 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3074 have_mult = 1;
3076 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3077 We used to do this EXCEPT in one case: I3 has a post-inc in an
3078 output operand. However, that exception can give rise to insns like
3079 mov r3,(r3)+
3080 which is a famous insn on the PDP-11 where the value of r3 used as the
3081 source was model-dependent. Avoid this sort of thing. */
3083 #if 0
3084 if (!(GET_CODE (PATTERN (i3)) == SET
3085 && REG_P (SET_SRC (PATTERN (i3)))
3086 && MEM_P (SET_DEST (PATTERN (i3)))
3087 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3088 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3089 /* It's not the exception. */
3090 #endif
3091 if (AUTO_INC_DEC)
3093 rtx link;
3094 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3095 if (REG_NOTE_KIND (link) == REG_INC
3096 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3097 || (i1 != 0
3098 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3100 undo_all ();
3101 return 0;
3105 /* See if the SETs in I1 or I2 need to be kept around in the merged
3106 instruction: whenever the value set there is still needed past I3.
3107 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3109 For the SET in I1, we have two cases: if I1 and I2 independently feed
3110 into I3, the set in I1 needs to be kept around unless I1DEST dies
3111 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3112 in I1 needs to be kept around unless I1DEST dies or is set in either
3113 I2 or I3. The same considerations apply to I0. */
3115 added_sets_2 = !dead_or_set_p (i3, i2dest);
3117 if (i1)
3118 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3119 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3120 else
3121 added_sets_1 = 0;
3123 if (i0)
3124 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3125 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3126 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3127 && dead_or_set_p (i2, i0dest)));
3128 else
3129 added_sets_0 = 0;
3131 /* We are about to copy insns for the case where they need to be kept
3132 around. Check that they can be copied in the merged instruction. */
3134 if (targetm.cannot_copy_insn_p
3135 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3136 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3137 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3139 undo_all ();
3140 return 0;
3143 /* If the set in I2 needs to be kept around, we must make a copy of
3144 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3145 PATTERN (I2), we are only substituting for the original I1DEST, not into
3146 an already-substituted copy. This also prevents making self-referential
3147 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3148 I2DEST. */
3150 if (added_sets_2)
3152 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3153 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3154 else
3155 i2pat = copy_rtx (PATTERN (i2));
3158 if (added_sets_1)
3160 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3161 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3162 else
3163 i1pat = copy_rtx (PATTERN (i1));
3166 if (added_sets_0)
3168 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3169 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3170 else
3171 i0pat = copy_rtx (PATTERN (i0));
3174 combine_merges++;
3176 /* Substitute in the latest insn for the regs set by the earlier ones. */
3178 maxreg = max_reg_num ();
3180 subst_insn = i3;
3182 /* Many machines that don't use CC0 have insns that can both perform an
3183 arithmetic operation and set the condition code. These operations will
3184 be represented as a PARALLEL with the first element of the vector
3185 being a COMPARE of an arithmetic operation with the constant zero.
3186 The second element of the vector will set some pseudo to the result
3187 of the same arithmetic operation. If we simplify the COMPARE, we won't
3188 match such a pattern and so will generate an extra insn. Here we test
3189 for this case, where both the comparison and the operation result are
3190 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3191 I2SRC. Later we will make the PARALLEL that contains I2. */
3193 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3194 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3195 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3196 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3198 rtx newpat_dest;
3199 rtx *cc_use_loc = NULL;
3200 rtx_insn *cc_use_insn = NULL;
3201 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3202 machine_mode compare_mode, orig_compare_mode;
3203 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3204 scalar_int_mode mode;
3206 newpat = PATTERN (i3);
3207 newpat_dest = SET_DEST (newpat);
3208 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3210 if (undobuf.other_insn == 0
3211 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3212 &cc_use_insn)))
3214 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3215 if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3216 compare_code = simplify_compare_const (compare_code, mode,
3217 op0, &op1);
3218 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3221 /* Do the rest only if op1 is const0_rtx, which may be the
3222 result of simplification. */
3223 if (op1 == const0_rtx)
3225 /* If a single use of the CC is found, prepare to modify it
3226 when SELECT_CC_MODE returns a new CC-class mode, or when
3227 the above simplify_compare_const() returned a new comparison
3228 operator. undobuf.other_insn is assigned the CC use insn
3229 when modifying it. */
3230 if (cc_use_loc)
3232 #ifdef SELECT_CC_MODE
3233 machine_mode new_mode
3234 = SELECT_CC_MODE (compare_code, op0, op1);
3235 if (new_mode != orig_compare_mode
3236 && can_change_dest_mode (SET_DEST (newpat),
3237 added_sets_2, new_mode))
3239 unsigned int regno = REGNO (newpat_dest);
3240 compare_mode = new_mode;
3241 if (regno < FIRST_PSEUDO_REGISTER)
3242 newpat_dest = gen_rtx_REG (compare_mode, regno);
3243 else
3245 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3246 newpat_dest = regno_reg_rtx[regno];
3249 #endif
3250 /* Cases for modifying the CC-using comparison. */
3251 if (compare_code != orig_compare_code
3252 /* ??? Do we need to verify the zero rtx? */
3253 && XEXP (*cc_use_loc, 1) == const0_rtx)
3255 /* Replace cc_use_loc with entire new RTX. */
3256 SUBST (*cc_use_loc,
3257 gen_rtx_fmt_ee (compare_code, compare_mode,
3258 newpat_dest, const0_rtx));
3259 undobuf.other_insn = cc_use_insn;
3261 else if (compare_mode != orig_compare_mode)
3263 /* Just replace the CC reg with a new mode. */
3264 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3265 undobuf.other_insn = cc_use_insn;
3269 /* Now we modify the current newpat:
3270 First, SET_DEST(newpat) is updated if the CC mode has been
3271 altered. For targets without SELECT_CC_MODE, this should be
3272 optimized away. */
3273 if (compare_mode != orig_compare_mode)
3274 SUBST (SET_DEST (newpat), newpat_dest);
3275 /* This is always done to propagate i2src into newpat. */
3276 SUBST (SET_SRC (newpat),
3277 gen_rtx_COMPARE (compare_mode, op0, op1));
3278 /* Create new version of i2pat if needed; the below PARALLEL
3279 creation needs this to work correctly. */
3280 if (! rtx_equal_p (i2src, op0))
3281 i2pat = gen_rtx_SET (i2dest, op0);
3282 i2_is_used = 1;
3286 if (i2_is_used == 0)
3288 /* It is possible that the source of I2 or I1 may be performing
3289 an unneeded operation, such as a ZERO_EXTEND of something
3290 that is known to have the high part zero. Handle that case
3291 by letting subst look at the inner insns.
3293 Another way to do this would be to have a function that tries
3294 to simplify a single insn instead of merging two or more
3295 insns. We don't do this because of the potential of infinite
3296 loops and because of the potential extra memory required.
3297 However, doing it the way we are is a bit of a kludge and
3298 doesn't catch all cases.
3300 But only do this if -fexpensive-optimizations since it slows
3301 things down and doesn't usually win.
3303 This is not done in the COMPARE case above because the
3304 unmodified I2PAT is used in the PARALLEL and so a pattern
3305 with a modified I2SRC would not match. */
3307 if (flag_expensive_optimizations)
3309 /* Pass pc_rtx so no substitutions are done, just
3310 simplifications. */
3311 if (i1)
3313 subst_low_luid = DF_INSN_LUID (i1);
3314 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3317 subst_low_luid = DF_INSN_LUID (i2);
3318 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3321 n_occurrences = 0; /* `subst' counts here */
3322 subst_low_luid = DF_INSN_LUID (i2);
3324 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3325 copy of I2SRC each time we substitute it, in order to avoid creating
3326 self-referential RTL when we will be substituting I1SRC for I1DEST
3327 later. Likewise if I0 feeds into I2, either directly or indirectly
3328 through I1, and I0DEST is in I0SRC. */
3329 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3330 (i1_feeds_i2_n && i1dest_in_i1src)
3331 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3332 && i0dest_in_i0src));
3333 substed_i2 = 1;
3335 /* Record whether I2's body now appears within I3's body. */
3336 i2_is_used = n_occurrences;
3339 /* If we already got a failure, don't try to do more. Otherwise, try to
3340 substitute I1 if we have it. */
3342 if (i1 && GET_CODE (newpat) != CLOBBER)
3344 /* Check that an autoincrement side-effect on I1 has not been lost.
3345 This happens if I1DEST is mentioned in I2 and dies there, and
3346 has disappeared from the new pattern. */
3347 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3348 && i1_feeds_i2_n
3349 && dead_or_set_p (i2, i1dest)
3350 && !reg_overlap_mentioned_p (i1dest, newpat))
3351 /* Before we can do this substitution, we must redo the test done
3352 above (see detailed comments there) that ensures I1DEST isn't
3353 mentioned in any SETs in NEWPAT that are field assignments. */
3354 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3355 0, 0, 0))
3357 undo_all ();
3358 return 0;
3361 n_occurrences = 0;
3362 subst_low_luid = DF_INSN_LUID (i1);
3364 /* If the following substitution will modify I1SRC, make a copy of it
3365 for the case where it is substituted for I1DEST in I2PAT later. */
3366 if (added_sets_2 && i1_feeds_i2_n)
3367 i1src_copy = copy_rtx (i1src);
3369 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3370 copy of I1SRC each time we substitute it, in order to avoid creating
3371 self-referential RTL when we will be substituting I0SRC for I0DEST
3372 later. */
3373 newpat = subst (newpat, i1dest, i1src, 0, 0,
3374 i0_feeds_i1_n && i0dest_in_i0src);
3375 substed_i1 = 1;
3377 /* Record whether I1's body now appears within I3's body. */
3378 i1_is_used = n_occurrences;
3381 /* Likewise for I0 if we have it. */
3383 if (i0 && GET_CODE (newpat) != CLOBBER)
3385 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3386 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3387 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3388 && !reg_overlap_mentioned_p (i0dest, newpat))
3389 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3390 0, 0, 0))
3392 undo_all ();
3393 return 0;
3396 /* If the following substitution will modify I0SRC, make a copy of it
3397 for the case where it is substituted for I0DEST in I1PAT later. */
3398 if (added_sets_1 && i0_feeds_i1_n)
3399 i0src_copy = copy_rtx (i0src);
3400 /* And a copy for I0DEST in I2PAT substitution. */
3401 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3402 || (i0_feeds_i2_n)))
3403 i0src_copy2 = copy_rtx (i0src);
3405 n_occurrences = 0;
3406 subst_low_luid = DF_INSN_LUID (i0);
3407 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3408 substed_i0 = 1;
3411 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3412 to count all the ways that I2SRC and I1SRC can be used. */
3413 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3414 && i2_is_used + added_sets_2 > 1)
3415 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3416 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3417 > 1))
3418 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3419 && (n_occurrences + added_sets_0
3420 + (added_sets_1 && i0_feeds_i1_n)
3421 + (added_sets_2 && i0_feeds_i2_n)
3422 > 1))
3423 /* Fail if we tried to make a new register. */
3424 || max_reg_num () != maxreg
3425 /* Fail if we couldn't do something and have a CLOBBER. */
3426 || GET_CODE (newpat) == CLOBBER
3427 /* Fail if this new pattern is a MULT and we didn't have one before
3428 at the outer level. */
3429 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3430 && ! have_mult))
3432 undo_all ();
3433 return 0;
3436 /* If the actions of the earlier insns must be kept
3437 in addition to substituting them into the latest one,
3438 we must make a new PARALLEL for the latest insn
3439 to hold additional the SETs. */
3441 if (added_sets_0 || added_sets_1 || added_sets_2)
3443 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3444 combine_extras++;
3446 if (GET_CODE (newpat) == PARALLEL)
3448 rtvec old = XVEC (newpat, 0);
3449 total_sets = XVECLEN (newpat, 0) + extra_sets;
3450 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3451 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3452 sizeof (old->elem[0]) * old->num_elem);
3454 else
3456 rtx old = newpat;
3457 total_sets = 1 + extra_sets;
3458 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3459 XVECEXP (newpat, 0, 0) = old;
3462 if (added_sets_0)
3463 XVECEXP (newpat, 0, --total_sets) = i0pat;
3465 if (added_sets_1)
3467 rtx t = i1pat;
3468 if (i0_feeds_i1_n)
3469 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3471 XVECEXP (newpat, 0, --total_sets) = t;
3473 if (added_sets_2)
3475 rtx t = i2pat;
3476 if (i1_feeds_i2_n)
3477 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3478 i0_feeds_i1_n && i0dest_in_i0src);
3479 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3480 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3482 XVECEXP (newpat, 0, --total_sets) = t;
3486 validate_replacement:
3488 /* Note which hard regs this insn has as inputs. */
3489 mark_used_regs_combine (newpat);
3491 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3492 consider splitting this pattern, we might need these clobbers. */
3493 if (i1 && GET_CODE (newpat) == PARALLEL
3494 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3496 int len = XVECLEN (newpat, 0);
3498 newpat_vec_with_clobbers = rtvec_alloc (len);
3499 for (i = 0; i < len; i++)
3500 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3503 /* We have recognized nothing yet. */
3504 insn_code_number = -1;
3506 /* See if this is a PARALLEL of two SETs where one SET's destination is
3507 a register that is unused and this isn't marked as an instruction that
3508 might trap in an EH region. In that case, we just need the other SET.
3509 We prefer this over the PARALLEL.
3511 This can occur when simplifying a divmod insn. We *must* test for this
3512 case here because the code below that splits two independent SETs doesn't
3513 handle this case correctly when it updates the register status.
3515 It's pointless doing this if we originally had two sets, one from
3516 i3, and one from i2. Combining then splitting the parallel results
3517 in the original i2 again plus an invalid insn (which we delete).
3518 The net effect is only to move instructions around, which makes
3519 debug info less accurate.
3521 If the remaining SET came from I2 its destination should not be used
3522 between I2 and I3. See PR82024. */
3524 if (!(added_sets_2 && i1 == 0)
3525 && is_parallel_of_n_reg_sets (newpat, 2)
3526 && asm_noperands (newpat) < 0)
3528 rtx set0 = XVECEXP (newpat, 0, 0);
3529 rtx set1 = XVECEXP (newpat, 0, 1);
3530 rtx oldpat = newpat;
3532 if (((REG_P (SET_DEST (set1))
3533 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3534 || (GET_CODE (SET_DEST (set1)) == SUBREG
3535 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3536 && insn_nothrow_p (i3)
3537 && !side_effects_p (SET_SRC (set1)))
3539 newpat = set0;
3540 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3543 else if (((REG_P (SET_DEST (set0))
3544 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3545 || (GET_CODE (SET_DEST (set0)) == SUBREG
3546 && find_reg_note (i3, REG_UNUSED,
3547 SUBREG_REG (SET_DEST (set0)))))
3548 && insn_nothrow_p (i3)
3549 && !side_effects_p (SET_SRC (set0)))
3551 rtx dest = SET_DEST (set1);
3552 if (GET_CODE (dest) == SUBREG)
3553 dest = SUBREG_REG (dest);
3554 if (!reg_used_between_p (dest, i2, i3))
3556 newpat = set1;
3557 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3559 if (insn_code_number >= 0)
3560 changed_i3_dest = 1;
3564 if (insn_code_number < 0)
3565 newpat = oldpat;
3568 /* Is the result of combination a valid instruction? */
3569 if (insn_code_number < 0)
3570 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3572 /* If we were combining three insns and the result is a simple SET
3573 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3574 insns. There are two ways to do this. It can be split using a
3575 machine-specific method (like when you have an addition of a large
3576 constant) or by combine in the function find_split_point. */
3578 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3579 && asm_noperands (newpat) < 0)
3581 rtx parallel, *split;
3582 rtx_insn *m_split_insn;
3584 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3585 use I2DEST as a scratch register will help. In the latter case,
3586 convert I2DEST to the mode of the source of NEWPAT if we can. */
3588 m_split_insn = combine_split_insns (newpat, i3);
3590 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3591 inputs of NEWPAT. */
3593 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3594 possible to try that as a scratch reg. This would require adding
3595 more code to make it work though. */
3597 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3599 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3601 /* ??? Reusing i2dest without resetting the reg_stat entry for it
3602 (temporarily, until we are committed to this instruction
3603 combination) does not work: for example, any call to nonzero_bits
3604 on the register (from a splitter in the MD file, for example)
3605 will get the old information, which is invalid.
3607 Since nowadays we can create registers during combine just fine,
3608 we should just create a new one here, not reuse i2dest. */
3610 /* First try to split using the original register as a
3611 scratch register. */
3612 parallel = gen_rtx_PARALLEL (VOIDmode,
3613 gen_rtvec (2, newpat,
3614 gen_rtx_CLOBBER (VOIDmode,
3615 i2dest)));
3616 m_split_insn = combine_split_insns (parallel, i3);
3618 /* If that didn't work, try changing the mode of I2DEST if
3619 we can. */
3620 if (m_split_insn == 0
3621 && new_mode != GET_MODE (i2dest)
3622 && new_mode != VOIDmode
3623 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3625 machine_mode old_mode = GET_MODE (i2dest);
3626 rtx ni2dest;
3628 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3629 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3630 else
3632 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3633 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3636 parallel = (gen_rtx_PARALLEL
3637 (VOIDmode,
3638 gen_rtvec (2, newpat,
3639 gen_rtx_CLOBBER (VOIDmode,
3640 ni2dest))));
3641 m_split_insn = combine_split_insns (parallel, i3);
3643 if (m_split_insn == 0
3644 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3646 struct undo *buf;
3648 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3649 buf = undobuf.undos;
3650 undobuf.undos = buf->next;
3651 buf->next = undobuf.frees;
3652 undobuf.frees = buf;
3656 i2scratch = m_split_insn != 0;
3659 /* If recog_for_combine has discarded clobbers, try to use them
3660 again for the split. */
3661 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3663 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3664 m_split_insn = combine_split_insns (parallel, i3);
3667 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3669 rtx m_split_pat = PATTERN (m_split_insn);
3670 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3671 if (insn_code_number >= 0)
3672 newpat = m_split_pat;
3674 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3675 && (next_nonnote_nondebug_insn (i2) == i3
3676 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3678 rtx i2set, i3set;
3679 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3680 newi2pat = PATTERN (m_split_insn);
3682 i3set = single_set (NEXT_INSN (m_split_insn));
3683 i2set = single_set (m_split_insn);
3685 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3687 /* If I2 or I3 has multiple SETs, we won't know how to track
3688 register status, so don't use these insns. If I2's destination
3689 is used between I2 and I3, we also can't use these insns. */
3691 if (i2_code_number >= 0 && i2set && i3set
3692 && (next_nonnote_nondebug_insn (i2) == i3
3693 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3694 insn_code_number = recog_for_combine (&newi3pat, i3,
3695 &new_i3_notes);
3696 if (insn_code_number >= 0)
3697 newpat = newi3pat;
3699 /* It is possible that both insns now set the destination of I3.
3700 If so, we must show an extra use of it. */
3702 if (insn_code_number >= 0)
3704 rtx new_i3_dest = SET_DEST (i3set);
3705 rtx new_i2_dest = SET_DEST (i2set);
3707 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3708 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3709 || GET_CODE (new_i3_dest) == SUBREG)
3710 new_i3_dest = XEXP (new_i3_dest, 0);
3712 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3713 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3714 || GET_CODE (new_i2_dest) == SUBREG)
3715 new_i2_dest = XEXP (new_i2_dest, 0);
3717 if (REG_P (new_i3_dest)
3718 && REG_P (new_i2_dest)
3719 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3720 && REGNO (new_i2_dest) < reg_n_sets_max)
3721 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3725 /* If we can split it and use I2DEST, go ahead and see if that
3726 helps things be recognized. Verify that none of the registers
3727 are set between I2 and I3. */
3728 if (insn_code_number < 0
3729 && (split = find_split_point (&newpat, i3, false)) != 0
3730 && (!HAVE_cc0 || REG_P (i2dest))
3731 /* We need I2DEST in the proper mode. If it is a hard register
3732 or the only use of a pseudo, we can change its mode.
3733 Make sure we don't change a hard register to have a mode that
3734 isn't valid for it, or change the number of registers. */
3735 && (GET_MODE (*split) == GET_MODE (i2dest)
3736 || GET_MODE (*split) == VOIDmode
3737 || can_change_dest_mode (i2dest, added_sets_2,
3738 GET_MODE (*split)))
3739 && (next_nonnote_nondebug_insn (i2) == i3
3740 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3741 /* We can't overwrite I2DEST if its value is still used by
3742 NEWPAT. */
3743 && ! reg_referenced_p (i2dest, newpat))
3745 rtx newdest = i2dest;
3746 enum rtx_code split_code = GET_CODE (*split);
3747 machine_mode split_mode = GET_MODE (*split);
3748 bool subst_done = false;
3749 newi2pat = NULL_RTX;
3751 i2scratch = true;
3753 /* *SPLIT may be part of I2SRC, so make sure we have the
3754 original expression around for later debug processing.
3755 We should not need I2SRC any more in other cases. */
3756 if (MAY_HAVE_DEBUG_INSNS)
3757 i2src = copy_rtx (i2src);
3758 else
3759 i2src = NULL;
3761 /* Get NEWDEST as a register in the proper mode. We have already
3762 validated that we can do this. */
3763 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3765 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3766 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3767 else
3769 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3770 newdest = regno_reg_rtx[REGNO (i2dest)];
3774 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3775 an ASHIFT. This can occur if it was inside a PLUS and hence
3776 appeared to be a memory address. This is a kludge. */
3777 if (split_code == MULT
3778 && CONST_INT_P (XEXP (*split, 1))
3779 && INTVAL (XEXP (*split, 1)) > 0
3780 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3782 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3783 XEXP (*split, 0), GEN_INT (i)));
3784 /* Update split_code because we may not have a multiply
3785 anymore. */
3786 split_code = GET_CODE (*split);
3789 /* Similarly for (plus (mult FOO (const_int pow2))). */
3790 if (split_code == PLUS
3791 && GET_CODE (XEXP (*split, 0)) == MULT
3792 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3793 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3794 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3796 rtx nsplit = XEXP (*split, 0);
3797 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3798 XEXP (nsplit, 0), GEN_INT (i)));
3799 /* Update split_code because we may not have a multiply
3800 anymore. */
3801 split_code = GET_CODE (*split);
3804 #ifdef INSN_SCHEDULING
3805 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3806 be written as a ZERO_EXTEND. */
3807 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3809 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3810 what it really is. */
3811 if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3812 == SIGN_EXTEND)
3813 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3814 SUBREG_REG (*split)));
3815 else
3816 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3817 SUBREG_REG (*split)));
3819 #endif
3821 /* Attempt to split binary operators using arithmetic identities. */
3822 if (BINARY_P (SET_SRC (newpat))
3823 && split_mode == GET_MODE (SET_SRC (newpat))
3824 && ! side_effects_p (SET_SRC (newpat)))
3826 rtx setsrc = SET_SRC (newpat);
3827 machine_mode mode = GET_MODE (setsrc);
3828 enum rtx_code code = GET_CODE (setsrc);
3829 rtx src_op0 = XEXP (setsrc, 0);
3830 rtx src_op1 = XEXP (setsrc, 1);
3832 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3833 if (rtx_equal_p (src_op0, src_op1))
3835 newi2pat = gen_rtx_SET (newdest, src_op0);
3836 SUBST (XEXP (setsrc, 0), newdest);
3837 SUBST (XEXP (setsrc, 1), newdest);
3838 subst_done = true;
3840 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3841 else if ((code == PLUS || code == MULT)
3842 && GET_CODE (src_op0) == code
3843 && GET_CODE (XEXP (src_op0, 0)) == code
3844 && (INTEGRAL_MODE_P (mode)
3845 || (FLOAT_MODE_P (mode)
3846 && flag_unsafe_math_optimizations)))
3848 rtx p = XEXP (XEXP (src_op0, 0), 0);
3849 rtx q = XEXP (XEXP (src_op0, 0), 1);
3850 rtx r = XEXP (src_op0, 1);
3851 rtx s = src_op1;
3853 /* Split both "((X op Y) op X) op Y" and
3854 "((X op Y) op Y) op X" as "T op T" where T is
3855 "X op Y". */
3856 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3857 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3859 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3860 SUBST (XEXP (setsrc, 0), newdest);
3861 SUBST (XEXP (setsrc, 1), newdest);
3862 subst_done = true;
3864 /* Split "((X op X) op Y) op Y)" as "T op T" where
3865 T is "X op Y". */
3866 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3868 rtx tmp = simplify_gen_binary (code, mode, p, r);
3869 newi2pat = gen_rtx_SET (newdest, tmp);
3870 SUBST (XEXP (setsrc, 0), newdest);
3871 SUBST (XEXP (setsrc, 1), newdest);
3872 subst_done = true;
3877 if (!subst_done)
3879 newi2pat = gen_rtx_SET (newdest, *split);
3880 SUBST (*split, newdest);
3883 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3885 /* recog_for_combine might have added CLOBBERs to newi2pat.
3886 Make sure NEWPAT does not depend on the clobbered regs. */
3887 if (GET_CODE (newi2pat) == PARALLEL)
3888 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3889 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3891 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3892 if (reg_overlap_mentioned_p (reg, newpat))
3894 undo_all ();
3895 return 0;
3899 /* If the split point was a MULT and we didn't have one before,
3900 don't use one now. */
3901 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3902 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3906 /* Check for a case where we loaded from memory in a narrow mode and
3907 then sign extended it, but we need both registers. In that case,
3908 we have a PARALLEL with both loads from the same memory location.
3909 We can split this into a load from memory followed by a register-register
3910 copy. This saves at least one insn, more if register allocation can
3911 eliminate the copy.
3913 We cannot do this if the destination of the first assignment is a
3914 condition code register or cc0. We eliminate this case by making sure
3915 the SET_DEST and SET_SRC have the same mode.
3917 We cannot do this if the destination of the second assignment is
3918 a register that we have already assumed is zero-extended. Similarly
3919 for a SUBREG of such a register. */
3921 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3922 && GET_CODE (newpat) == PARALLEL
3923 && XVECLEN (newpat, 0) == 2
3924 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3925 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3926 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3927 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3928 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3929 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3930 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3931 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3932 DF_INSN_LUID (i2))
3933 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3934 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3935 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3936 (REG_P (temp_expr)
3937 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3938 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3939 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3940 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3941 != GET_MODE_MASK (word_mode))))
3942 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3943 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3944 (REG_P (temp_expr)
3945 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3946 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3947 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3948 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3949 != GET_MODE_MASK (word_mode)))))
3950 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3951 SET_SRC (XVECEXP (newpat, 0, 1)))
3952 && ! find_reg_note (i3, REG_UNUSED,
3953 SET_DEST (XVECEXP (newpat, 0, 0))))
3955 rtx ni2dest;
3957 newi2pat = XVECEXP (newpat, 0, 0);
3958 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3959 newpat = XVECEXP (newpat, 0, 1);
3960 SUBST (SET_SRC (newpat),
3961 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3962 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3964 if (i2_code_number >= 0)
3965 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3967 if (insn_code_number >= 0)
3968 swap_i2i3 = 1;
3971 /* Similarly, check for a case where we have a PARALLEL of two independent
3972 SETs but we started with three insns. In this case, we can do the sets
3973 as two separate insns. This case occurs when some SET allows two
3974 other insns to combine, but the destination of that SET is still live.
3976 Also do this if we started with two insns and (at least) one of the
3977 resulting sets is a noop; this noop will be deleted later. */
3979 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3980 && GET_CODE (newpat) == PARALLEL
3981 && XVECLEN (newpat, 0) == 2
3982 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3983 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3984 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3985 || set_noop_p (XVECEXP (newpat, 0, 1)))
3986 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3987 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3988 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3989 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3990 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3991 XVECEXP (newpat, 0, 0))
3992 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3993 XVECEXP (newpat, 0, 1))
3994 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3995 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3997 rtx set0 = XVECEXP (newpat, 0, 0);
3998 rtx set1 = XVECEXP (newpat, 0, 1);
4000 /* Normally, it doesn't matter which of the two is done first,
4001 but the one that references cc0 can't be the second, and
4002 one which uses any regs/memory set in between i2 and i3 can't
4003 be first. The PARALLEL might also have been pre-existing in i3,
4004 so we need to make sure that we won't wrongly hoist a SET to i2
4005 that would conflict with a death note present in there. */
4006 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
4007 && !(REG_P (SET_DEST (set1))
4008 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4009 && !(GET_CODE (SET_DEST (set1)) == SUBREG
4010 && find_reg_note (i2, REG_DEAD,
4011 SUBREG_REG (SET_DEST (set1))))
4012 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
4013 /* If I3 is a jump, ensure that set0 is a jump so that
4014 we do not create invalid RTL. */
4015 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
4018 newi2pat = set1;
4019 newpat = set0;
4021 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
4022 && !(REG_P (SET_DEST (set0))
4023 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4024 && !(GET_CODE (SET_DEST (set0)) == SUBREG
4025 && find_reg_note (i2, REG_DEAD,
4026 SUBREG_REG (SET_DEST (set0))))
4027 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
4028 /* If I3 is a jump, ensure that set1 is a jump so that
4029 we do not create invalid RTL. */
4030 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4033 newi2pat = set0;
4034 newpat = set1;
4036 else
4038 undo_all ();
4039 return 0;
4042 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4044 if (i2_code_number >= 0)
4046 /* recog_for_combine might have added CLOBBERs to newi2pat.
4047 Make sure NEWPAT does not depend on the clobbered regs. */
4048 if (GET_CODE (newi2pat) == PARALLEL)
4050 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4051 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4053 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4054 if (reg_overlap_mentioned_p (reg, newpat))
4056 undo_all ();
4057 return 0;
4062 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4066 /* If it still isn't recognized, fail and change things back the way they
4067 were. */
4068 if ((insn_code_number < 0
4069 /* Is the result a reasonable ASM_OPERANDS? */
4070 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4072 undo_all ();
4073 return 0;
4076 /* If we had to change another insn, make sure it is valid also. */
4077 if (undobuf.other_insn)
4079 CLEAR_HARD_REG_SET (newpat_used_regs);
4081 other_pat = PATTERN (undobuf.other_insn);
4082 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4083 &new_other_notes);
4085 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4087 undo_all ();
4088 return 0;
4092 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4093 they are adjacent to each other or not. */
4094 if (HAVE_cc0)
4096 rtx_insn *p = prev_nonnote_insn (i3);
4097 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4098 && sets_cc0_p (newi2pat))
4100 undo_all ();
4101 return 0;
4105 /* Only allow this combination if insn_cost reports that the
4106 replacement instructions are cheaper than the originals. */
4107 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4109 undo_all ();
4110 return 0;
4113 if (MAY_HAVE_DEBUG_INSNS)
4115 struct undo *undo;
4117 for (undo = undobuf.undos; undo; undo = undo->next)
4118 if (undo->kind == UNDO_MODE)
4120 rtx reg = *undo->where.r;
4121 machine_mode new_mode = GET_MODE (reg);
4122 machine_mode old_mode = undo->old_contents.m;
4124 /* Temporarily revert mode back. */
4125 adjust_reg_mode (reg, old_mode);
4127 if (reg == i2dest && i2scratch)
4129 /* If we used i2dest as a scratch register with a
4130 different mode, substitute it for the original
4131 i2src while its original mode is temporarily
4132 restored, and then clear i2scratch so that we don't
4133 do it again later. */
4134 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4135 this_basic_block);
4136 i2scratch = false;
4137 /* Put back the new mode. */
4138 adjust_reg_mode (reg, new_mode);
4140 else
4142 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4143 rtx_insn *first, *last;
4145 if (reg == i2dest)
4147 first = i2;
4148 last = last_combined_insn;
4150 else
4152 first = i3;
4153 last = undobuf.other_insn;
4154 gcc_assert (last);
4155 if (DF_INSN_LUID (last)
4156 < DF_INSN_LUID (last_combined_insn))
4157 last = last_combined_insn;
4160 /* We're dealing with a reg that changed mode but not
4161 meaning, so we want to turn it into a subreg for
4162 the new mode. However, because of REG sharing and
4163 because its mode had already changed, we have to do
4164 it in two steps. First, replace any debug uses of
4165 reg, with its original mode temporarily restored,
4166 with this copy we have created; then, replace the
4167 copy with the SUBREG of the original shared reg,
4168 once again changed to the new mode. */
4169 propagate_for_debug (first, last, reg, tempreg,
4170 this_basic_block);
4171 adjust_reg_mode (reg, new_mode);
4172 propagate_for_debug (first, last, tempreg,
4173 lowpart_subreg (old_mode, reg, new_mode),
4174 this_basic_block);
4179 /* If we will be able to accept this, we have made a
4180 change to the destination of I3. This requires us to
4181 do a few adjustments. */
4183 if (changed_i3_dest)
4185 PATTERN (i3) = newpat;
4186 adjust_for_new_dest (i3);
4189 /* We now know that we can do this combination. Merge the insns and
4190 update the status of registers and LOG_LINKS. */
4192 if (undobuf.other_insn)
4194 rtx note, next;
4196 PATTERN (undobuf.other_insn) = other_pat;
4198 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4199 ensure that they are still valid. Then add any non-duplicate
4200 notes added by recog_for_combine. */
4201 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4203 next = XEXP (note, 1);
4205 if ((REG_NOTE_KIND (note) == REG_DEAD
4206 && !reg_referenced_p (XEXP (note, 0),
4207 PATTERN (undobuf.other_insn)))
4208 ||(REG_NOTE_KIND (note) == REG_UNUSED
4209 && !reg_set_p (XEXP (note, 0),
4210 PATTERN (undobuf.other_insn)))
4211 /* Simply drop equal note since it may be no longer valid
4212 for other_insn. It may be possible to record that CC
4213 register is changed and only discard those notes, but
4214 in practice it's unnecessary complication and doesn't
4215 give any meaningful improvement.
4217 See PR78559. */
4218 || REG_NOTE_KIND (note) == REG_EQUAL
4219 || REG_NOTE_KIND (note) == REG_EQUIV)
4220 remove_note (undobuf.other_insn, note);
4223 distribute_notes (new_other_notes, undobuf.other_insn,
4224 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4225 NULL_RTX);
4228 if (swap_i2i3)
4230 rtx_insn *insn;
4231 struct insn_link *link;
4232 rtx ni2dest;
4234 /* I3 now uses what used to be its destination and which is now
4235 I2's destination. This requires us to do a few adjustments. */
4236 PATTERN (i3) = newpat;
4237 adjust_for_new_dest (i3);
4239 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4240 so we still will.
4242 However, some later insn might be using I2's dest and have
4243 a LOG_LINK pointing at I3. We must remove this link.
4244 The simplest way to remove the link is to point it at I1,
4245 which we know will be a NOTE. */
4247 /* newi2pat is usually a SET here; however, recog_for_combine might
4248 have added some clobbers. */
4249 if (GET_CODE (newi2pat) == PARALLEL)
4250 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4251 else
4252 ni2dest = SET_DEST (newi2pat);
4254 for (insn = NEXT_INSN (i3);
4255 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4256 || insn != BB_HEAD (this_basic_block->next_bb));
4257 insn = NEXT_INSN (insn))
4259 if (NONDEBUG_INSN_P (insn)
4260 && reg_referenced_p (ni2dest, PATTERN (insn)))
4262 FOR_EACH_LOG_LINK (link, insn)
4263 if (link->insn == i3)
4264 link->insn = i1;
4266 break;
4272 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4273 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4274 rtx midnotes = 0;
4275 int from_luid;
4276 /* Compute which registers we expect to eliminate. newi2pat may be setting
4277 either i3dest or i2dest, so we must check it. */
4278 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4279 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4280 || !i2dest_killed
4281 ? 0 : i2dest);
4282 /* For i1, we need to compute both local elimination and global
4283 elimination information with respect to newi2pat because i1dest
4284 may be the same as i3dest, in which case newi2pat may be setting
4285 i1dest. Global information is used when distributing REG_DEAD
4286 note for i2 and i3, in which case it does matter if newi2pat sets
4287 i1dest or not.
4289 Local information is used when distributing REG_DEAD note for i1,
4290 in which case it doesn't matter if newi2pat sets i1dest or not.
4291 See PR62151, if we have four insns combination:
4292 i0: r0 <- i0src
4293 i1: r1 <- i1src (using r0)
4294 REG_DEAD (r0)
4295 i2: r0 <- i2src (using r1)
4296 i3: r3 <- i3src (using r0)
4297 ix: using r0
4298 From i1's point of view, r0 is eliminated, no matter if it is set
4299 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4300 should be discarded.
4302 Note local information only affects cases in forms like "I1->I2->I3",
4303 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4304 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4305 i0dest anyway. */
4306 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4307 || !i1dest_killed
4308 ? 0 : i1dest);
4309 rtx elim_i1 = (local_elim_i1 == 0
4310 || (newi2pat && reg_set_p (i1dest, newi2pat))
4311 ? 0 : i1dest);
4312 /* Same case as i1. */
4313 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4314 ? 0 : i0dest);
4315 rtx elim_i0 = (local_elim_i0 == 0
4316 || (newi2pat && reg_set_p (i0dest, newi2pat))
4317 ? 0 : i0dest);
4319 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4320 clear them. */
4321 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4322 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4323 if (i1)
4324 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4325 if (i0)
4326 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4328 /* Ensure that we do not have something that should not be shared but
4329 occurs multiple times in the new insns. Check this by first
4330 resetting all the `used' flags and then copying anything is shared. */
4332 reset_used_flags (i3notes);
4333 reset_used_flags (i2notes);
4334 reset_used_flags (i1notes);
4335 reset_used_flags (i0notes);
4336 reset_used_flags (newpat);
4337 reset_used_flags (newi2pat);
4338 if (undobuf.other_insn)
4339 reset_used_flags (PATTERN (undobuf.other_insn));
4341 i3notes = copy_rtx_if_shared (i3notes);
4342 i2notes = copy_rtx_if_shared (i2notes);
4343 i1notes = copy_rtx_if_shared (i1notes);
4344 i0notes = copy_rtx_if_shared (i0notes);
4345 newpat = copy_rtx_if_shared (newpat);
4346 newi2pat = copy_rtx_if_shared (newi2pat);
4347 if (undobuf.other_insn)
4348 reset_used_flags (PATTERN (undobuf.other_insn));
4350 INSN_CODE (i3) = insn_code_number;
4351 PATTERN (i3) = newpat;
4353 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4355 for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4356 link = XEXP (link, 1))
4358 if (substed_i2)
4360 /* I2SRC must still be meaningful at this point. Some
4361 splitting operations can invalidate I2SRC, but those
4362 operations do not apply to calls. */
4363 gcc_assert (i2src);
4364 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4365 i2dest, i2src);
4367 if (substed_i1)
4368 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4369 i1dest, i1src);
4370 if (substed_i0)
4371 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4372 i0dest, i0src);
4376 if (undobuf.other_insn)
4377 INSN_CODE (undobuf.other_insn) = other_code_number;
4379 /* We had one special case above where I2 had more than one set and
4380 we replaced a destination of one of those sets with the destination
4381 of I3. In that case, we have to update LOG_LINKS of insns later
4382 in this basic block. Note that this (expensive) case is rare.
4384 Also, in this case, we must pretend that all REG_NOTEs for I2
4385 actually came from I3, so that REG_UNUSED notes from I2 will be
4386 properly handled. */
4388 if (i3_subst_into_i2)
4390 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4391 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4392 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4393 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4394 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4395 && ! find_reg_note (i2, REG_UNUSED,
4396 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4397 for (temp_insn = NEXT_INSN (i2);
4398 temp_insn
4399 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4400 || BB_HEAD (this_basic_block) != temp_insn);
4401 temp_insn = NEXT_INSN (temp_insn))
4402 if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4403 FOR_EACH_LOG_LINK (link, temp_insn)
4404 if (link->insn == i2)
4405 link->insn = i3;
4407 if (i3notes)
4409 rtx link = i3notes;
4410 while (XEXP (link, 1))
4411 link = XEXP (link, 1);
4412 XEXP (link, 1) = i2notes;
4414 else
4415 i3notes = i2notes;
4416 i2notes = 0;
4419 LOG_LINKS (i3) = NULL;
4420 REG_NOTES (i3) = 0;
4421 LOG_LINKS (i2) = NULL;
4422 REG_NOTES (i2) = 0;
4424 if (newi2pat)
4426 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4427 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4428 this_basic_block);
4429 INSN_CODE (i2) = i2_code_number;
4430 PATTERN (i2) = newi2pat;
4432 else
4434 if (MAY_HAVE_DEBUG_INSNS && i2src)
4435 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4436 this_basic_block);
4437 SET_INSN_DELETED (i2);
4440 if (i1)
4442 LOG_LINKS (i1) = NULL;
4443 REG_NOTES (i1) = 0;
4444 if (MAY_HAVE_DEBUG_INSNS)
4445 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4446 this_basic_block);
4447 SET_INSN_DELETED (i1);
4450 if (i0)
4452 LOG_LINKS (i0) = NULL;
4453 REG_NOTES (i0) = 0;
4454 if (MAY_HAVE_DEBUG_INSNS)
4455 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4456 this_basic_block);
4457 SET_INSN_DELETED (i0);
4460 /* Get death notes for everything that is now used in either I3 or
4461 I2 and used to die in a previous insn. If we built two new
4462 patterns, move from I1 to I2 then I2 to I3 so that we get the
4463 proper movement on registers that I2 modifies. */
4465 if (i0)
4466 from_luid = DF_INSN_LUID (i0);
4467 else if (i1)
4468 from_luid = DF_INSN_LUID (i1);
4469 else
4470 from_luid = DF_INSN_LUID (i2);
4471 if (newi2pat)
4472 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4473 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4475 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4476 if (i3notes)
4477 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4478 elim_i2, elim_i1, elim_i0);
4479 if (i2notes)
4480 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4481 elim_i2, elim_i1, elim_i0);
4482 if (i1notes)
4483 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4484 elim_i2, local_elim_i1, local_elim_i0);
4485 if (i0notes)
4486 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4487 elim_i2, elim_i1, local_elim_i0);
4488 if (midnotes)
4489 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4490 elim_i2, elim_i1, elim_i0);
4492 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4493 know these are REG_UNUSED and want them to go to the desired insn,
4494 so we always pass it as i3. */
4496 if (newi2pat && new_i2_notes)
4497 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4498 NULL_RTX);
4500 if (new_i3_notes)
4501 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4502 NULL_RTX);
4504 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4505 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4506 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4507 in that case, it might delete I2. Similarly for I2 and I1.
4508 Show an additional death due to the REG_DEAD note we make here. If
4509 we discard it in distribute_notes, we will decrement it again. */
4511 if (i3dest_killed)
4513 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4514 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4515 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4516 elim_i1, elim_i0);
4517 else
4518 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4519 elim_i2, elim_i1, elim_i0);
4522 if (i2dest_in_i2src)
4524 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4525 if (newi2pat && reg_set_p (i2dest, newi2pat))
4526 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4527 NULL_RTX, NULL_RTX);
4528 else
4529 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4530 NULL_RTX, NULL_RTX, NULL_RTX);
4533 if (i1dest_in_i1src)
4535 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4536 if (newi2pat && reg_set_p (i1dest, newi2pat))
4537 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4538 NULL_RTX, NULL_RTX);
4539 else
4540 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4541 NULL_RTX, NULL_RTX, NULL_RTX);
4544 if (i0dest_in_i0src)
4546 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4547 if (newi2pat && reg_set_p (i0dest, newi2pat))
4548 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4549 NULL_RTX, NULL_RTX);
4550 else
4551 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4552 NULL_RTX, NULL_RTX, NULL_RTX);
4555 distribute_links (i3links);
4556 distribute_links (i2links);
4557 distribute_links (i1links);
4558 distribute_links (i0links);
4560 if (REG_P (i2dest))
4562 struct insn_link *link;
4563 rtx_insn *i2_insn = 0;
4564 rtx i2_val = 0, set;
4566 /* The insn that used to set this register doesn't exist, and
4567 this life of the register may not exist either. See if one of
4568 I3's links points to an insn that sets I2DEST. If it does,
4569 that is now the last known value for I2DEST. If we don't update
4570 this and I2 set the register to a value that depended on its old
4571 contents, we will get confused. If this insn is used, thing
4572 will be set correctly in combine_instructions. */
4573 FOR_EACH_LOG_LINK (link, i3)
4574 if ((set = single_set (link->insn)) != 0
4575 && rtx_equal_p (i2dest, SET_DEST (set)))
4576 i2_insn = link->insn, i2_val = SET_SRC (set);
4578 record_value_for_reg (i2dest, i2_insn, i2_val);
4580 /* If the reg formerly set in I2 died only once and that was in I3,
4581 zero its use count so it won't make `reload' do any work. */
4582 if (! added_sets_2
4583 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4584 && ! i2dest_in_i2src
4585 && REGNO (i2dest) < reg_n_sets_max)
4586 INC_REG_N_SETS (REGNO (i2dest), -1);
4589 if (i1 && REG_P (i1dest))
4591 struct insn_link *link;
4592 rtx_insn *i1_insn = 0;
4593 rtx i1_val = 0, set;
4595 FOR_EACH_LOG_LINK (link, i3)
4596 if ((set = single_set (link->insn)) != 0
4597 && rtx_equal_p (i1dest, SET_DEST (set)))
4598 i1_insn = link->insn, i1_val = SET_SRC (set);
4600 record_value_for_reg (i1dest, i1_insn, i1_val);
4602 if (! added_sets_1
4603 && ! i1dest_in_i1src
4604 && REGNO (i1dest) < reg_n_sets_max)
4605 INC_REG_N_SETS (REGNO (i1dest), -1);
4608 if (i0 && REG_P (i0dest))
4610 struct insn_link *link;
4611 rtx_insn *i0_insn = 0;
4612 rtx i0_val = 0, set;
4614 FOR_EACH_LOG_LINK (link, i3)
4615 if ((set = single_set (link->insn)) != 0
4616 && rtx_equal_p (i0dest, SET_DEST (set)))
4617 i0_insn = link->insn, i0_val = SET_SRC (set);
4619 record_value_for_reg (i0dest, i0_insn, i0_val);
4621 if (! added_sets_0
4622 && ! i0dest_in_i0src
4623 && REGNO (i0dest) < reg_n_sets_max)
4624 INC_REG_N_SETS (REGNO (i0dest), -1);
4627 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4628 been made to this insn. The order is important, because newi2pat
4629 can affect nonzero_bits of newpat. */
4630 if (newi2pat)
4631 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4632 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4635 if (undobuf.other_insn != NULL_RTX)
4637 if (dump_file)
4639 fprintf (dump_file, "modifying other_insn ");
4640 dump_insn_slim (dump_file, undobuf.other_insn);
4642 df_insn_rescan (undobuf.other_insn);
4645 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4647 if (dump_file)
4649 fprintf (dump_file, "modifying insn i0 ");
4650 dump_insn_slim (dump_file, i0);
4652 df_insn_rescan (i0);
4655 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4657 if (dump_file)
4659 fprintf (dump_file, "modifying insn i1 ");
4660 dump_insn_slim (dump_file, i1);
4662 df_insn_rescan (i1);
4665 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4667 if (dump_file)
4669 fprintf (dump_file, "modifying insn i2 ");
4670 dump_insn_slim (dump_file, i2);
4672 df_insn_rescan (i2);
4675 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4677 if (dump_file)
4679 fprintf (dump_file, "modifying insn i3 ");
4680 dump_insn_slim (dump_file, i3);
4682 df_insn_rescan (i3);
4685 /* Set new_direct_jump_p if a new return or simple jump instruction
4686 has been created. Adjust the CFG accordingly. */
4687 if (returnjump_p (i3) || any_uncondjump_p (i3))
4689 *new_direct_jump_p = 1;
4690 mark_jump_label (PATTERN (i3), i3, 0);
4691 update_cfg_for_uncondjump (i3);
4694 if (undobuf.other_insn != NULL_RTX
4695 && (returnjump_p (undobuf.other_insn)
4696 || any_uncondjump_p (undobuf.other_insn)))
4698 *new_direct_jump_p = 1;
4699 update_cfg_for_uncondjump (undobuf.other_insn);
4702 if (GET_CODE (PATTERN (i3)) == TRAP_IF
4703 && XEXP (PATTERN (i3), 0) == const1_rtx)
4705 basic_block bb = BLOCK_FOR_INSN (i3);
4706 gcc_assert (bb);
4707 remove_edge (split_block (bb, i3));
4708 emit_barrier_after_bb (bb);
4709 *new_direct_jump_p = 1;
4712 if (undobuf.other_insn
4713 && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4714 && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4716 basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4717 gcc_assert (bb);
4718 remove_edge (split_block (bb, undobuf.other_insn));
4719 emit_barrier_after_bb (bb);
4720 *new_direct_jump_p = 1;
4723 /* A noop might also need cleaning up of CFG, if it comes from the
4724 simplification of a jump. */
4725 if (JUMP_P (i3)
4726 && GET_CODE (newpat) == SET
4727 && SET_SRC (newpat) == pc_rtx
4728 && SET_DEST (newpat) == pc_rtx)
4730 *new_direct_jump_p = 1;
4731 update_cfg_for_uncondjump (i3);
4734 if (undobuf.other_insn != NULL_RTX
4735 && JUMP_P (undobuf.other_insn)
4736 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4737 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4738 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4740 *new_direct_jump_p = 1;
4741 update_cfg_for_uncondjump (undobuf.other_insn);
4744 combine_successes++;
4745 undo_commit ();
4747 if (added_links_insn
4748 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4749 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4750 return added_links_insn;
4751 else
4752 return newi2pat ? i2 : i3;
4755 /* Get a marker for undoing to the current state. */
4757 static void *
4758 get_undo_marker (void)
4760 return undobuf.undos;
4763 /* Undo the modifications up to the marker. */
4765 static void
4766 undo_to_marker (void *marker)
4768 struct undo *undo, *next;
4770 for (undo = undobuf.undos; undo != marker; undo = next)
4772 gcc_assert (undo);
4774 next = undo->next;
4775 switch (undo->kind)
4777 case UNDO_RTX:
4778 *undo->where.r = undo->old_contents.r;
4779 break;
4780 case UNDO_INT:
4781 *undo->where.i = undo->old_contents.i;
4782 break;
4783 case UNDO_MODE:
4784 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4785 break;
4786 case UNDO_LINKS:
4787 *undo->where.l = undo->old_contents.l;
4788 break;
4789 default:
4790 gcc_unreachable ();
4793 undo->next = undobuf.frees;
4794 undobuf.frees = undo;
4797 undobuf.undos = (struct undo *) marker;
4800 /* Undo all the modifications recorded in undobuf. */
4802 static void
4803 undo_all (void)
4805 undo_to_marker (0);
4808 /* We've committed to accepting the changes we made. Move all
4809 of the undos to the free list. */
4811 static void
4812 undo_commit (void)
4814 struct undo *undo, *next;
4816 for (undo = undobuf.undos; undo; undo = next)
4818 next = undo->next;
4819 undo->next = undobuf.frees;
4820 undobuf.frees = undo;
4822 undobuf.undos = 0;
4825 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4826 where we have an arithmetic expression and return that point. LOC will
4827 be inside INSN.
4829 try_combine will call this function to see if an insn can be split into
4830 two insns. */
4832 static rtx *
4833 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4835 rtx x = *loc;
4836 enum rtx_code code = GET_CODE (x);
4837 rtx *split;
4838 unsigned HOST_WIDE_INT len = 0;
4839 HOST_WIDE_INT pos = 0;
4840 int unsignedp = 0;
4841 rtx inner = NULL_RTX;
4842 scalar_int_mode mode, inner_mode;
4844 /* First special-case some codes. */
4845 switch (code)
4847 case SUBREG:
4848 #ifdef INSN_SCHEDULING
4849 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4850 point. */
4851 if (MEM_P (SUBREG_REG (x)))
4852 return loc;
4853 #endif
4854 return find_split_point (&SUBREG_REG (x), insn, false);
4856 case MEM:
4857 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4858 using LO_SUM and HIGH. */
4859 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4860 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4862 machine_mode address_mode = get_address_mode (x);
4864 SUBST (XEXP (x, 0),
4865 gen_rtx_LO_SUM (address_mode,
4866 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4867 XEXP (x, 0)));
4868 return &XEXP (XEXP (x, 0), 0);
4871 /* If we have a PLUS whose second operand is a constant and the
4872 address is not valid, perhaps will can split it up using
4873 the machine-specific way to split large constants. We use
4874 the first pseudo-reg (one of the virtual regs) as a placeholder;
4875 it will not remain in the result. */
4876 if (GET_CODE (XEXP (x, 0)) == PLUS
4877 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4878 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4879 MEM_ADDR_SPACE (x)))
4881 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4882 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4883 subst_insn);
4885 /* This should have produced two insns, each of which sets our
4886 placeholder. If the source of the second is a valid address,
4887 we can make put both sources together and make a split point
4888 in the middle. */
4890 if (seq
4891 && NEXT_INSN (seq) != NULL_RTX
4892 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4893 && NONJUMP_INSN_P (seq)
4894 && GET_CODE (PATTERN (seq)) == SET
4895 && SET_DEST (PATTERN (seq)) == reg
4896 && ! reg_mentioned_p (reg,
4897 SET_SRC (PATTERN (seq)))
4898 && NONJUMP_INSN_P (NEXT_INSN (seq))
4899 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4900 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4901 && memory_address_addr_space_p
4902 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4903 MEM_ADDR_SPACE (x)))
4905 rtx src1 = SET_SRC (PATTERN (seq));
4906 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4908 /* Replace the placeholder in SRC2 with SRC1. If we can
4909 find where in SRC2 it was placed, that can become our
4910 split point and we can replace this address with SRC2.
4911 Just try two obvious places. */
4913 src2 = replace_rtx (src2, reg, src1);
4914 split = 0;
4915 if (XEXP (src2, 0) == src1)
4916 split = &XEXP (src2, 0);
4917 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4918 && XEXP (XEXP (src2, 0), 0) == src1)
4919 split = &XEXP (XEXP (src2, 0), 0);
4921 if (split)
4923 SUBST (XEXP (x, 0), src2);
4924 return split;
4928 /* If that didn't work, perhaps the first operand is complex and
4929 needs to be computed separately, so make a split point there.
4930 This will occur on machines that just support REG + CONST
4931 and have a constant moved through some previous computation. */
4933 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4934 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4935 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4936 return &XEXP (XEXP (x, 0), 0);
4939 /* If we have a PLUS whose first operand is complex, try computing it
4940 separately by making a split there. */
4941 if (GET_CODE (XEXP (x, 0)) == PLUS
4942 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4943 MEM_ADDR_SPACE (x))
4944 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4945 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4946 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4947 return &XEXP (XEXP (x, 0), 0);
4948 break;
4950 case SET:
4951 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4952 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4953 we need to put the operand into a register. So split at that
4954 point. */
4956 if (SET_DEST (x) == cc0_rtx
4957 && GET_CODE (SET_SRC (x)) != COMPARE
4958 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4959 && !OBJECT_P (SET_SRC (x))
4960 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4961 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4962 return &SET_SRC (x);
4964 /* See if we can split SET_SRC as it stands. */
4965 split = find_split_point (&SET_SRC (x), insn, true);
4966 if (split && split != &SET_SRC (x))
4967 return split;
4969 /* See if we can split SET_DEST as it stands. */
4970 split = find_split_point (&SET_DEST (x), insn, false);
4971 if (split && split != &SET_DEST (x))
4972 return split;
4974 /* See if this is a bitfield assignment with everything constant. If
4975 so, this is an IOR of an AND, so split it into that. */
4976 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4977 && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
4978 &inner_mode)
4979 && HWI_COMPUTABLE_MODE_P (inner_mode)
4980 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4981 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4982 && CONST_INT_P (SET_SRC (x))
4983 && ((INTVAL (XEXP (SET_DEST (x), 1))
4984 + INTVAL (XEXP (SET_DEST (x), 2)))
4985 <= GET_MODE_PRECISION (inner_mode))
4986 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4988 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4989 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4990 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4991 rtx dest = XEXP (SET_DEST (x), 0);
4992 unsigned HOST_WIDE_INT mask
4993 = (HOST_WIDE_INT_1U << len) - 1;
4994 rtx or_mask;
4996 if (BITS_BIG_ENDIAN)
4997 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
4999 or_mask = gen_int_mode (src << pos, inner_mode);
5000 if (src == mask)
5001 SUBST (SET_SRC (x),
5002 simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5003 else
5005 rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5006 SUBST (SET_SRC (x),
5007 simplify_gen_binary (IOR, inner_mode,
5008 simplify_gen_binary (AND, inner_mode,
5009 dest, negmask),
5010 or_mask));
5013 SUBST (SET_DEST (x), dest);
5015 split = find_split_point (&SET_SRC (x), insn, true);
5016 if (split && split != &SET_SRC (x))
5017 return split;
5020 /* Otherwise, see if this is an operation that we can split into two.
5021 If so, try to split that. */
5022 code = GET_CODE (SET_SRC (x));
5024 switch (code)
5026 case AND:
5027 /* If we are AND'ing with a large constant that is only a single
5028 bit and the result is only being used in a context where we
5029 need to know if it is zero or nonzero, replace it with a bit
5030 extraction. This will avoid the large constant, which might
5031 have taken more than one insn to make. If the constant were
5032 not a valid argument to the AND but took only one insn to make,
5033 this is no worse, but if it took more than one insn, it will
5034 be better. */
5036 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5037 && REG_P (XEXP (SET_SRC (x), 0))
5038 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5039 && REG_P (SET_DEST (x))
5040 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5041 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5042 && XEXP (*split, 0) == SET_DEST (x)
5043 && XEXP (*split, 1) == const0_rtx)
5045 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5046 XEXP (SET_SRC (x), 0),
5047 pos, NULL_RTX, 1, 1, 0, 0);
5048 if (extraction != 0)
5050 SUBST (SET_SRC (x), extraction);
5051 return find_split_point (loc, insn, false);
5054 break;
5056 case NE:
5057 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5058 is known to be on, this can be converted into a NEG of a shift. */
5059 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5060 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5061 && 1 <= (pos = exact_log2
5062 (nonzero_bits (XEXP (SET_SRC (x), 0),
5063 GET_MODE (XEXP (SET_SRC (x), 0))))))
5065 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5067 SUBST (SET_SRC (x),
5068 gen_rtx_NEG (mode,
5069 gen_rtx_LSHIFTRT (mode,
5070 XEXP (SET_SRC (x), 0),
5071 GEN_INT (pos))));
5073 split = find_split_point (&SET_SRC (x), insn, true);
5074 if (split && split != &SET_SRC (x))
5075 return split;
5077 break;
5079 case SIGN_EXTEND:
5080 inner = XEXP (SET_SRC (x), 0);
5082 /* We can't optimize if either mode is a partial integer
5083 mode as we don't know how many bits are significant
5084 in those modes. */
5085 if (!is_int_mode (GET_MODE (inner), &inner_mode)
5086 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5087 break;
5089 pos = 0;
5090 len = GET_MODE_PRECISION (inner_mode);
5091 unsignedp = 0;
5092 break;
5094 case SIGN_EXTRACT:
5095 case ZERO_EXTRACT:
5096 if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5097 &inner_mode)
5098 && CONST_INT_P (XEXP (SET_SRC (x), 1))
5099 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5101 inner = XEXP (SET_SRC (x), 0);
5102 len = INTVAL (XEXP (SET_SRC (x), 1));
5103 pos = INTVAL (XEXP (SET_SRC (x), 2));
5105 if (BITS_BIG_ENDIAN)
5106 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5107 unsignedp = (code == ZERO_EXTRACT);
5109 break;
5111 default:
5112 break;
5115 if (len && pos >= 0
5116 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner))
5117 && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5119 /* For unsigned, we have a choice of a shift followed by an
5120 AND or two shifts. Use two shifts for field sizes where the
5121 constant might be too large. We assume here that we can
5122 always at least get 8-bit constants in an AND insn, which is
5123 true for every current RISC. */
5125 if (unsignedp && len <= 8)
5127 unsigned HOST_WIDE_INT mask
5128 = (HOST_WIDE_INT_1U << len) - 1;
5129 SUBST (SET_SRC (x),
5130 gen_rtx_AND (mode,
5131 gen_rtx_LSHIFTRT
5132 (mode, gen_lowpart (mode, inner),
5133 GEN_INT (pos)),
5134 gen_int_mode (mask, mode)));
5136 split = find_split_point (&SET_SRC (x), insn, true);
5137 if (split && split != &SET_SRC (x))
5138 return split;
5140 else
5142 SUBST (SET_SRC (x),
5143 gen_rtx_fmt_ee
5144 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5145 gen_rtx_ASHIFT (mode,
5146 gen_lowpart (mode, inner),
5147 GEN_INT (GET_MODE_PRECISION (mode)
5148 - len - pos)),
5149 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5151 split = find_split_point (&SET_SRC (x), insn, true);
5152 if (split && split != &SET_SRC (x))
5153 return split;
5157 /* See if this is a simple operation with a constant as the second
5158 operand. It might be that this constant is out of range and hence
5159 could be used as a split point. */
5160 if (BINARY_P (SET_SRC (x))
5161 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5162 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5163 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5164 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5165 return &XEXP (SET_SRC (x), 1);
5167 /* Finally, see if this is a simple operation with its first operand
5168 not in a register. The operation might require this operand in a
5169 register, so return it as a split point. We can always do this
5170 because if the first operand were another operation, we would have
5171 already found it as a split point. */
5172 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5173 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5174 return &XEXP (SET_SRC (x), 0);
5176 return 0;
5178 case AND:
5179 case IOR:
5180 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5181 it is better to write this as (not (ior A B)) so we can split it.
5182 Similarly for IOR. */
5183 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5185 SUBST (*loc,
5186 gen_rtx_NOT (GET_MODE (x),
5187 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5188 GET_MODE (x),
5189 XEXP (XEXP (x, 0), 0),
5190 XEXP (XEXP (x, 1), 0))));
5191 return find_split_point (loc, insn, set_src);
5194 /* Many RISC machines have a large set of logical insns. If the
5195 second operand is a NOT, put it first so we will try to split the
5196 other operand first. */
5197 if (GET_CODE (XEXP (x, 1)) == NOT)
5199 rtx tem = XEXP (x, 0);
5200 SUBST (XEXP (x, 0), XEXP (x, 1));
5201 SUBST (XEXP (x, 1), tem);
5203 break;
5205 case PLUS:
5206 case MINUS:
5207 /* Canonicalization can produce (minus A (mult B C)), where C is a
5208 constant. It may be better to try splitting (plus (mult B -C) A)
5209 instead if this isn't a multiply by a power of two. */
5210 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5211 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5212 && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5214 machine_mode mode = GET_MODE (x);
5215 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5216 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5217 SUBST (*loc, gen_rtx_PLUS (mode,
5218 gen_rtx_MULT (mode,
5219 XEXP (XEXP (x, 1), 0),
5220 gen_int_mode (other_int,
5221 mode)),
5222 XEXP (x, 0)));
5223 return find_split_point (loc, insn, set_src);
5226 /* Split at a multiply-accumulate instruction. However if this is
5227 the SET_SRC, we likely do not have such an instruction and it's
5228 worthless to try this split. */
5229 if (!set_src
5230 && (GET_CODE (XEXP (x, 0)) == MULT
5231 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5232 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5233 return loc;
5235 default:
5236 break;
5239 /* Otherwise, select our actions depending on our rtx class. */
5240 switch (GET_RTX_CLASS (code))
5242 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5243 case RTX_TERNARY:
5244 split = find_split_point (&XEXP (x, 2), insn, false);
5245 if (split)
5246 return split;
5247 /* fall through */
5248 case RTX_BIN_ARITH:
5249 case RTX_COMM_ARITH:
5250 case RTX_COMPARE:
5251 case RTX_COMM_COMPARE:
5252 split = find_split_point (&XEXP (x, 1), insn, false);
5253 if (split)
5254 return split;
5255 /* fall through */
5256 case RTX_UNARY:
5257 /* Some machines have (and (shift ...) ...) insns. If X is not
5258 an AND, but XEXP (X, 0) is, use it as our split point. */
5259 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5260 return &XEXP (x, 0);
5262 split = find_split_point (&XEXP (x, 0), insn, false);
5263 if (split)
5264 return split;
5265 return loc;
5267 default:
5268 /* Otherwise, we don't have a split point. */
5269 return 0;
5273 /* Throughout X, replace FROM with TO, and return the result.
5274 The result is TO if X is FROM;
5275 otherwise the result is X, but its contents may have been modified.
5276 If they were modified, a record was made in undobuf so that
5277 undo_all will (among other things) return X to its original state.
5279 If the number of changes necessary is too much to record to undo,
5280 the excess changes are not made, so the result is invalid.
5281 The changes already made can still be undone.
5282 undobuf.num_undo is incremented for such changes, so by testing that
5283 the caller can tell whether the result is valid.
5285 `n_occurrences' is incremented each time FROM is replaced.
5287 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5289 IN_COND is nonzero if we are at the top level of a condition.
5291 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5292 by copying if `n_occurrences' is nonzero. */
5294 static rtx
5295 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5297 enum rtx_code code = GET_CODE (x);
5298 machine_mode op0_mode = VOIDmode;
5299 const char *fmt;
5300 int len, i;
5301 rtx new_rtx;
5303 /* Two expressions are equal if they are identical copies of a shared
5304 RTX or if they are both registers with the same register number
5305 and mode. */
5307 #define COMBINE_RTX_EQUAL_P(X,Y) \
5308 ((X) == (Y) \
5309 || (REG_P (X) && REG_P (Y) \
5310 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5312 /* Do not substitute into clobbers of regs -- this will never result in
5313 valid RTL. */
5314 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5315 return x;
5317 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5319 n_occurrences++;
5320 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5323 /* If X and FROM are the same register but different modes, they
5324 will not have been seen as equal above. However, the log links code
5325 will make a LOG_LINKS entry for that case. If we do nothing, we
5326 will try to rerecognize our original insn and, when it succeeds,
5327 we will delete the feeding insn, which is incorrect.
5329 So force this insn not to match in this (rare) case. */
5330 if (! in_dest && code == REG && REG_P (from)
5331 && reg_overlap_mentioned_p (x, from))
5332 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5334 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5335 of which may contain things that can be combined. */
5336 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5337 return x;
5339 /* It is possible to have a subexpression appear twice in the insn.
5340 Suppose that FROM is a register that appears within TO.
5341 Then, after that subexpression has been scanned once by `subst',
5342 the second time it is scanned, TO may be found. If we were
5343 to scan TO here, we would find FROM within it and create a
5344 self-referent rtl structure which is completely wrong. */
5345 if (COMBINE_RTX_EQUAL_P (x, to))
5346 return to;
5348 /* Parallel asm_operands need special attention because all of the
5349 inputs are shared across the arms. Furthermore, unsharing the
5350 rtl results in recognition failures. Failure to handle this case
5351 specially can result in circular rtl.
5353 Solve this by doing a normal pass across the first entry of the
5354 parallel, and only processing the SET_DESTs of the subsequent
5355 entries. Ug. */
5357 if (code == PARALLEL
5358 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5359 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5361 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5363 /* If this substitution failed, this whole thing fails. */
5364 if (GET_CODE (new_rtx) == CLOBBER
5365 && XEXP (new_rtx, 0) == const0_rtx)
5366 return new_rtx;
5368 SUBST (XVECEXP (x, 0, 0), new_rtx);
5370 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5372 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5374 if (!REG_P (dest)
5375 && GET_CODE (dest) != CC0
5376 && GET_CODE (dest) != PC)
5378 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5380 /* If this substitution failed, this whole thing fails. */
5381 if (GET_CODE (new_rtx) == CLOBBER
5382 && XEXP (new_rtx, 0) == const0_rtx)
5383 return new_rtx;
5385 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5389 else
5391 len = GET_RTX_LENGTH (code);
5392 fmt = GET_RTX_FORMAT (code);
5394 /* We don't need to process a SET_DEST that is a register, CC0,
5395 or PC, so set up to skip this common case. All other cases
5396 where we want to suppress replacing something inside a
5397 SET_SRC are handled via the IN_DEST operand. */
5398 if (code == SET
5399 && (REG_P (SET_DEST (x))
5400 || GET_CODE (SET_DEST (x)) == CC0
5401 || GET_CODE (SET_DEST (x)) == PC))
5402 fmt = "ie";
5404 /* Trying to simplify the operands of a widening MULT is not likely
5405 to create RTL matching a machine insn. */
5406 if (code == MULT
5407 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5408 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5409 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5410 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5411 && REG_P (XEXP (XEXP (x, 0), 0))
5412 && REG_P (XEXP (XEXP (x, 1), 0))
5413 && from == to)
5414 return x;
5417 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5418 constant. */
5419 if (fmt[0] == 'e')
5420 op0_mode = GET_MODE (XEXP (x, 0));
5422 for (i = 0; i < len; i++)
5424 if (fmt[i] == 'E')
5426 int j;
5427 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5429 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5431 new_rtx = (unique_copy && n_occurrences
5432 ? copy_rtx (to) : to);
5433 n_occurrences++;
5435 else
5437 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5438 unique_copy);
5440 /* If this substitution failed, this whole thing
5441 fails. */
5442 if (GET_CODE (new_rtx) == CLOBBER
5443 && XEXP (new_rtx, 0) == const0_rtx)
5444 return new_rtx;
5447 SUBST (XVECEXP (x, i, j), new_rtx);
5450 else if (fmt[i] == 'e')
5452 /* If this is a register being set, ignore it. */
5453 new_rtx = XEXP (x, i);
5454 if (in_dest
5455 && i == 0
5456 && (((code == SUBREG || code == ZERO_EXTRACT)
5457 && REG_P (new_rtx))
5458 || code == STRICT_LOW_PART))
5461 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5463 /* In general, don't install a subreg involving two
5464 modes not tieable. It can worsen register
5465 allocation, and can even make invalid reload
5466 insns, since the reg inside may need to be copied
5467 from in the outside mode, and that may be invalid
5468 if it is an fp reg copied in integer mode.
5470 We allow two exceptions to this: It is valid if
5471 it is inside another SUBREG and the mode of that
5472 SUBREG and the mode of the inside of TO is
5473 tieable and it is valid if X is a SET that copies
5474 FROM to CC0. */
5476 if (GET_CODE (to) == SUBREG
5477 && !targetm.modes_tieable_p (GET_MODE (to),
5478 GET_MODE (SUBREG_REG (to)))
5479 && ! (code == SUBREG
5480 && (targetm.modes_tieable_p
5481 (GET_MODE (x), GET_MODE (SUBREG_REG (to)))))
5482 && (!HAVE_cc0
5483 || (! (code == SET
5484 && i == 1
5485 && XEXP (x, 0) == cc0_rtx))))
5486 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5488 if (code == SUBREG
5489 && REG_P (to)
5490 && REGNO (to) < FIRST_PSEUDO_REGISTER
5491 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5492 SUBREG_BYTE (x),
5493 GET_MODE (x)) < 0)
5494 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5496 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5497 n_occurrences++;
5499 else
5500 /* If we are in a SET_DEST, suppress most cases unless we
5501 have gone inside a MEM, in which case we want to
5502 simplify the address. We assume here that things that
5503 are actually part of the destination have their inner
5504 parts in the first expression. This is true for SUBREG,
5505 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5506 things aside from REG and MEM that should appear in a
5507 SET_DEST. */
5508 new_rtx = subst (XEXP (x, i), from, to,
5509 (((in_dest
5510 && (code == SUBREG || code == STRICT_LOW_PART
5511 || code == ZERO_EXTRACT))
5512 || code == SET)
5513 && i == 0),
5514 code == IF_THEN_ELSE && i == 0,
5515 unique_copy);
5517 /* If we found that we will have to reject this combination,
5518 indicate that by returning the CLOBBER ourselves, rather than
5519 an expression containing it. This will speed things up as
5520 well as prevent accidents where two CLOBBERs are considered
5521 to be equal, thus producing an incorrect simplification. */
5523 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5524 return new_rtx;
5526 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5528 machine_mode mode = GET_MODE (x);
5530 x = simplify_subreg (GET_MODE (x), new_rtx,
5531 GET_MODE (SUBREG_REG (x)),
5532 SUBREG_BYTE (x));
5533 if (! x)
5534 x = gen_rtx_CLOBBER (mode, const0_rtx);
5536 else if (CONST_SCALAR_INT_P (new_rtx)
5537 && GET_CODE (x) == ZERO_EXTEND)
5539 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5540 new_rtx, GET_MODE (XEXP (x, 0)));
5541 gcc_assert (x);
5543 else
5544 SUBST (XEXP (x, i), new_rtx);
5549 /* Check if we are loading something from the constant pool via float
5550 extension; in this case we would undo compress_float_constant
5551 optimization and degenerate constant load to an immediate value. */
5552 if (GET_CODE (x) == FLOAT_EXTEND
5553 && MEM_P (XEXP (x, 0))
5554 && MEM_READONLY_P (XEXP (x, 0)))
5556 rtx tmp = avoid_constant_pool_reference (x);
5557 if (x != tmp)
5558 return x;
5561 /* Try to simplify X. If the simplification changed the code, it is likely
5562 that further simplification will help, so loop, but limit the number
5563 of repetitions that will be performed. */
5565 for (i = 0; i < 4; i++)
5567 /* If X is sufficiently simple, don't bother trying to do anything
5568 with it. */
5569 if (code != CONST_INT && code != REG && code != CLOBBER)
5570 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5572 if (GET_CODE (x) == code)
5573 break;
5575 code = GET_CODE (x);
5577 /* We no longer know the original mode of operand 0 since we
5578 have changed the form of X) */
5579 op0_mode = VOIDmode;
5582 return x;
5585 /* If X is a commutative operation whose operands are not in the canonical
5586 order, use substitutions to swap them. */
5588 static void
5589 maybe_swap_commutative_operands (rtx x)
5591 if (COMMUTATIVE_ARITH_P (x)
5592 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5594 rtx temp = XEXP (x, 0);
5595 SUBST (XEXP (x, 0), XEXP (x, 1));
5596 SUBST (XEXP (x, 1), temp);
5600 /* Simplify X, a piece of RTL. We just operate on the expression at the
5601 outer level; call `subst' to simplify recursively. Return the new
5602 expression.
5604 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5605 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5606 of a condition. */
5608 static rtx
5609 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5610 int in_cond)
5612 enum rtx_code code = GET_CODE (x);
5613 machine_mode mode = GET_MODE (x);
5614 scalar_int_mode int_mode;
5615 rtx temp;
5616 int i;
5618 /* If this is a commutative operation, put a constant last and a complex
5619 expression first. We don't need to do this for comparisons here. */
5620 maybe_swap_commutative_operands (x);
5622 /* Try to fold this expression in case we have constants that weren't
5623 present before. */
5624 temp = 0;
5625 switch (GET_RTX_CLASS (code))
5627 case RTX_UNARY:
5628 if (op0_mode == VOIDmode)
5629 op0_mode = GET_MODE (XEXP (x, 0));
5630 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5631 break;
5632 case RTX_COMPARE:
5633 case RTX_COMM_COMPARE:
5635 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5636 if (cmp_mode == VOIDmode)
5638 cmp_mode = GET_MODE (XEXP (x, 1));
5639 if (cmp_mode == VOIDmode)
5640 cmp_mode = op0_mode;
5642 temp = simplify_relational_operation (code, mode, cmp_mode,
5643 XEXP (x, 0), XEXP (x, 1));
5645 break;
5646 case RTX_COMM_ARITH:
5647 case RTX_BIN_ARITH:
5648 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5649 break;
5650 case RTX_BITFIELD_OPS:
5651 case RTX_TERNARY:
5652 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5653 XEXP (x, 1), XEXP (x, 2));
5654 break;
5655 default:
5656 break;
5659 if (temp)
5661 x = temp;
5662 code = GET_CODE (temp);
5663 op0_mode = VOIDmode;
5664 mode = GET_MODE (temp);
5667 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5668 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5669 things. Check for cases where both arms are testing the same
5670 condition.
5672 Don't do anything if all operands are very simple. */
5674 if ((BINARY_P (x)
5675 && ((!OBJECT_P (XEXP (x, 0))
5676 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5677 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5678 || (!OBJECT_P (XEXP (x, 1))
5679 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5680 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5681 || (UNARY_P (x)
5682 && (!OBJECT_P (XEXP (x, 0))
5683 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5684 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5686 rtx cond, true_rtx, false_rtx;
5688 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5689 if (cond != 0
5690 /* If everything is a comparison, what we have is highly unlikely
5691 to be simpler, so don't use it. */
5692 && ! (COMPARISON_P (x)
5693 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5695 rtx cop1 = const0_rtx;
5696 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5698 if (cond_code == NE && COMPARISON_P (cond))
5699 return x;
5701 /* Simplify the alternative arms; this may collapse the true and
5702 false arms to store-flag values. Be careful to use copy_rtx
5703 here since true_rtx or false_rtx might share RTL with x as a
5704 result of the if_then_else_cond call above. */
5705 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5706 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5708 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5709 is unlikely to be simpler. */
5710 if (general_operand (true_rtx, VOIDmode)
5711 && general_operand (false_rtx, VOIDmode))
5713 enum rtx_code reversed;
5715 /* Restarting if we generate a store-flag expression will cause
5716 us to loop. Just drop through in this case. */
5718 /* If the result values are STORE_FLAG_VALUE and zero, we can
5719 just make the comparison operation. */
5720 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5721 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5722 cond, cop1);
5723 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5724 && ((reversed = reversed_comparison_code_parts
5725 (cond_code, cond, cop1, NULL))
5726 != UNKNOWN))
5727 x = simplify_gen_relational (reversed, mode, VOIDmode,
5728 cond, cop1);
5730 /* Likewise, we can make the negate of a comparison operation
5731 if the result values are - STORE_FLAG_VALUE and zero. */
5732 else if (CONST_INT_P (true_rtx)
5733 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5734 && false_rtx == const0_rtx)
5735 x = simplify_gen_unary (NEG, mode,
5736 simplify_gen_relational (cond_code,
5737 mode, VOIDmode,
5738 cond, cop1),
5739 mode);
5740 else if (CONST_INT_P (false_rtx)
5741 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5742 && true_rtx == const0_rtx
5743 && ((reversed = reversed_comparison_code_parts
5744 (cond_code, cond, cop1, NULL))
5745 != UNKNOWN))
5746 x = simplify_gen_unary (NEG, mode,
5747 simplify_gen_relational (reversed,
5748 mode, VOIDmode,
5749 cond, cop1),
5750 mode);
5751 else
5752 return gen_rtx_IF_THEN_ELSE (mode,
5753 simplify_gen_relational (cond_code,
5754 mode,
5755 VOIDmode,
5756 cond,
5757 cop1),
5758 true_rtx, false_rtx);
5760 code = GET_CODE (x);
5761 op0_mode = VOIDmode;
5766 /* First see if we can apply the inverse distributive law. */
5767 if (code == PLUS || code == MINUS
5768 || code == AND || code == IOR || code == XOR)
5770 x = apply_distributive_law (x);
5771 code = GET_CODE (x);
5772 op0_mode = VOIDmode;
5775 /* If CODE is an associative operation not otherwise handled, see if we
5776 can associate some operands. This can win if they are constants or
5777 if they are logically related (i.e. (a & b) & a). */
5778 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5779 || code == AND || code == IOR || code == XOR
5780 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5781 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5782 || (flag_associative_math && FLOAT_MODE_P (mode))))
5784 if (GET_CODE (XEXP (x, 0)) == code)
5786 rtx other = XEXP (XEXP (x, 0), 0);
5787 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5788 rtx inner_op1 = XEXP (x, 1);
5789 rtx inner;
5791 /* Make sure we pass the constant operand if any as the second
5792 one if this is a commutative operation. */
5793 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5794 std::swap (inner_op0, inner_op1);
5795 inner = simplify_binary_operation (code == MINUS ? PLUS
5796 : code == DIV ? MULT
5797 : code,
5798 mode, inner_op0, inner_op1);
5800 /* For commutative operations, try the other pair if that one
5801 didn't simplify. */
5802 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5804 other = XEXP (XEXP (x, 0), 1);
5805 inner = simplify_binary_operation (code, mode,
5806 XEXP (XEXP (x, 0), 0),
5807 XEXP (x, 1));
5810 if (inner)
5811 return simplify_gen_binary (code, mode, other, inner);
5815 /* A little bit of algebraic simplification here. */
5816 switch (code)
5818 case MEM:
5819 /* Ensure that our address has any ASHIFTs converted to MULT in case
5820 address-recognizing predicates are called later. */
5821 temp = make_compound_operation (XEXP (x, 0), MEM);
5822 SUBST (XEXP (x, 0), temp);
5823 break;
5825 case SUBREG:
5826 if (op0_mode == VOIDmode)
5827 op0_mode = GET_MODE (SUBREG_REG (x));
5829 /* See if this can be moved to simplify_subreg. */
5830 if (CONSTANT_P (SUBREG_REG (x))
5831 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5832 /* Don't call gen_lowpart if the inner mode
5833 is VOIDmode and we cannot simplify it, as SUBREG without
5834 inner mode is invalid. */
5835 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5836 || gen_lowpart_common (mode, SUBREG_REG (x))))
5837 return gen_lowpart (mode, SUBREG_REG (x));
5839 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5840 break;
5842 rtx temp;
5843 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5844 SUBREG_BYTE (x));
5845 if (temp)
5846 return temp;
5848 /* If op is known to have all lower bits zero, the result is zero. */
5849 scalar_int_mode int_mode, int_op0_mode;
5850 if (!in_dest
5851 && is_a <scalar_int_mode> (mode, &int_mode)
5852 && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5853 && (GET_MODE_PRECISION (int_mode)
5854 < GET_MODE_PRECISION (int_op0_mode))
5855 && (subreg_lowpart_offset (int_mode, int_op0_mode)
5856 == SUBREG_BYTE (x))
5857 && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5858 && (nonzero_bits (SUBREG_REG (x), int_op0_mode)
5859 & GET_MODE_MASK (int_mode)) == 0)
5860 return CONST0_RTX (int_mode);
5863 /* Don't change the mode of the MEM if that would change the meaning
5864 of the address. */
5865 if (MEM_P (SUBREG_REG (x))
5866 && (MEM_VOLATILE_P (SUBREG_REG (x))
5867 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5868 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5869 return gen_rtx_CLOBBER (mode, const0_rtx);
5871 /* Note that we cannot do any narrowing for non-constants since
5872 we might have been counting on using the fact that some bits were
5873 zero. We now do this in the SET. */
5875 break;
5877 case NEG:
5878 temp = expand_compound_operation (XEXP (x, 0));
5880 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5881 replaced by (lshiftrt X C). This will convert
5882 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5884 if (GET_CODE (temp) == ASHIFTRT
5885 && CONST_INT_P (XEXP (temp, 1))
5886 && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
5887 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5888 INTVAL (XEXP (temp, 1)));
5890 /* If X has only a single bit that might be nonzero, say, bit I, convert
5891 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5892 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5893 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5894 or a SUBREG of one since we'd be making the expression more
5895 complex if it was just a register. */
5897 if (!REG_P (temp)
5898 && ! (GET_CODE (temp) == SUBREG
5899 && REG_P (SUBREG_REG (temp)))
5900 && is_a <scalar_int_mode> (mode, &int_mode)
5901 && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
5903 rtx temp1 = simplify_shift_const
5904 (NULL_RTX, ASHIFTRT, int_mode,
5905 simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
5906 GET_MODE_PRECISION (int_mode) - 1 - i),
5907 GET_MODE_PRECISION (int_mode) - 1 - i);
5909 /* If all we did was surround TEMP with the two shifts, we
5910 haven't improved anything, so don't use it. Otherwise,
5911 we are better off with TEMP1. */
5912 if (GET_CODE (temp1) != ASHIFTRT
5913 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5914 || XEXP (XEXP (temp1, 0), 0) != temp)
5915 return temp1;
5917 break;
5919 case TRUNCATE:
5920 /* We can't handle truncation to a partial integer mode here
5921 because we don't know the real bitsize of the partial
5922 integer mode. */
5923 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5924 break;
5926 if (HWI_COMPUTABLE_MODE_P (mode))
5927 SUBST (XEXP (x, 0),
5928 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5929 GET_MODE_MASK (mode), 0));
5931 /* We can truncate a constant value and return it. */
5932 if (CONST_INT_P (XEXP (x, 0)))
5933 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5935 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5936 whose value is a comparison can be replaced with a subreg if
5937 STORE_FLAG_VALUE permits. */
5938 if (HWI_COMPUTABLE_MODE_P (mode)
5939 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5940 && (temp = get_last_value (XEXP (x, 0)))
5941 && COMPARISON_P (temp))
5942 return gen_lowpart (mode, XEXP (x, 0));
5943 break;
5945 case CONST:
5946 /* (const (const X)) can become (const X). Do it this way rather than
5947 returning the inner CONST since CONST can be shared with a
5948 REG_EQUAL note. */
5949 if (GET_CODE (XEXP (x, 0)) == CONST)
5950 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5951 break;
5953 case LO_SUM:
5954 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5955 can add in an offset. find_split_point will split this address up
5956 again if it doesn't match. */
5957 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
5958 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5959 return XEXP (x, 1);
5960 break;
5962 case PLUS:
5963 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5964 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5965 bit-field and can be replaced by either a sign_extend or a
5966 sign_extract. The `and' may be a zero_extend and the two
5967 <c>, -<c> constants may be reversed. */
5968 if (GET_CODE (XEXP (x, 0)) == XOR
5969 && is_a <scalar_int_mode> (mode, &int_mode)
5970 && CONST_INT_P (XEXP (x, 1))
5971 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5972 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5973 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5974 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5975 && HWI_COMPUTABLE_MODE_P (int_mode)
5976 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5977 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5978 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5979 == (HOST_WIDE_INT_1U << (i + 1)) - 1))
5980 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5981 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5982 == (unsigned int) i + 1))))
5983 return simplify_shift_const
5984 (NULL_RTX, ASHIFTRT, int_mode,
5985 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
5986 XEXP (XEXP (XEXP (x, 0), 0), 0),
5987 GET_MODE_PRECISION (int_mode) - (i + 1)),
5988 GET_MODE_PRECISION (int_mode) - (i + 1));
5990 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5991 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5992 the bitsize of the mode - 1. This allows simplification of
5993 "a = (b & 8) == 0;" */
5994 if (XEXP (x, 1) == constm1_rtx
5995 && !REG_P (XEXP (x, 0))
5996 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5997 && REG_P (SUBREG_REG (XEXP (x, 0))))
5998 && is_a <scalar_int_mode> (mode, &int_mode)
5999 && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6000 return simplify_shift_const
6001 (NULL_RTX, ASHIFTRT, int_mode,
6002 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6003 gen_rtx_XOR (int_mode, XEXP (x, 0),
6004 const1_rtx),
6005 GET_MODE_PRECISION (int_mode) - 1),
6006 GET_MODE_PRECISION (int_mode) - 1);
6008 /* If we are adding two things that have no bits in common, convert
6009 the addition into an IOR. This will often be further simplified,
6010 for example in cases like ((a & 1) + (a & 2)), which can
6011 become a & 3. */
6013 if (HWI_COMPUTABLE_MODE_P (mode)
6014 && (nonzero_bits (XEXP (x, 0), mode)
6015 & nonzero_bits (XEXP (x, 1), mode)) == 0)
6017 /* Try to simplify the expression further. */
6018 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6019 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
6021 /* If we could, great. If not, do not go ahead with the IOR
6022 replacement, since PLUS appears in many special purpose
6023 address arithmetic instructions. */
6024 if (GET_CODE (temp) != CLOBBER
6025 && (GET_CODE (temp) != IOR
6026 || ((XEXP (temp, 0) != XEXP (x, 0)
6027 || XEXP (temp, 1) != XEXP (x, 1))
6028 && (XEXP (temp, 0) != XEXP (x, 1)
6029 || XEXP (temp, 1) != XEXP (x, 0)))))
6030 return temp;
6033 /* Canonicalize x + x into x << 1. */
6034 if (GET_MODE_CLASS (mode) == MODE_INT
6035 && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6036 && !side_effects_p (XEXP (x, 0)))
6037 return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6039 break;
6041 case MINUS:
6042 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6043 (and <foo> (const_int pow2-1)) */
6044 if (is_a <scalar_int_mode> (mode, &int_mode)
6045 && GET_CODE (XEXP (x, 1)) == AND
6046 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6047 && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6048 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6049 return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6050 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6051 break;
6053 case MULT:
6054 /* If we have (mult (plus A B) C), apply the distributive law and then
6055 the inverse distributive law to see if things simplify. This
6056 occurs mostly in addresses, often when unrolling loops. */
6058 if (GET_CODE (XEXP (x, 0)) == PLUS)
6060 rtx result = distribute_and_simplify_rtx (x, 0);
6061 if (result)
6062 return result;
6065 /* Try simplify a*(b/c) as (a*b)/c. */
6066 if (FLOAT_MODE_P (mode) && flag_associative_math
6067 && GET_CODE (XEXP (x, 0)) == DIV)
6069 rtx tem = simplify_binary_operation (MULT, mode,
6070 XEXP (XEXP (x, 0), 0),
6071 XEXP (x, 1));
6072 if (tem)
6073 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6075 break;
6077 case UDIV:
6078 /* If this is a divide by a power of two, treat it as a shift if
6079 its first operand is a shift. */
6080 if (is_a <scalar_int_mode> (mode, &int_mode)
6081 && CONST_INT_P (XEXP (x, 1))
6082 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6083 && (GET_CODE (XEXP (x, 0)) == ASHIFT
6084 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6085 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6086 || GET_CODE (XEXP (x, 0)) == ROTATE
6087 || GET_CODE (XEXP (x, 0)) == ROTATERT))
6088 return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6089 XEXP (x, 0), i);
6090 break;
6092 case EQ: case NE:
6093 case GT: case GTU: case GE: case GEU:
6094 case LT: case LTU: case LE: case LEU:
6095 case UNEQ: case LTGT:
6096 case UNGT: case UNGE:
6097 case UNLT: case UNLE:
6098 case UNORDERED: case ORDERED:
6099 /* If the first operand is a condition code, we can't do anything
6100 with it. */
6101 if (GET_CODE (XEXP (x, 0)) == COMPARE
6102 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
6103 && ! CC0_P (XEXP (x, 0))))
6105 rtx op0 = XEXP (x, 0);
6106 rtx op1 = XEXP (x, 1);
6107 enum rtx_code new_code;
6109 if (GET_CODE (op0) == COMPARE)
6110 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6112 /* Simplify our comparison, if possible. */
6113 new_code = simplify_comparison (code, &op0, &op1);
6115 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6116 if only the low-order bit is possibly nonzero in X (such as when
6117 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6118 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6119 known to be either 0 or -1, NE becomes a NEG and EQ becomes
6120 (plus X 1).
6122 Remove any ZERO_EXTRACT we made when thinking this was a
6123 comparison. It may now be simpler to use, e.g., an AND. If a
6124 ZERO_EXTRACT is indeed appropriate, it will be placed back by
6125 the call to make_compound_operation in the SET case.
6127 Don't apply these optimizations if the caller would
6128 prefer a comparison rather than a value.
6129 E.g., for the condition in an IF_THEN_ELSE most targets need
6130 an explicit comparison. */
6132 if (in_cond)
6135 else if (STORE_FLAG_VALUE == 1
6136 && new_code == NE
6137 && is_int_mode (mode, &int_mode)
6138 && op1 == const0_rtx
6139 && int_mode == GET_MODE (op0)
6140 && nonzero_bits (op0, int_mode) == 1)
6141 return gen_lowpart (int_mode,
6142 expand_compound_operation (op0));
6144 else if (STORE_FLAG_VALUE == 1
6145 && new_code == NE
6146 && is_int_mode (mode, &int_mode)
6147 && op1 == const0_rtx
6148 && int_mode == GET_MODE (op0)
6149 && (num_sign_bit_copies (op0, int_mode)
6150 == GET_MODE_PRECISION (int_mode)))
6152 op0 = expand_compound_operation (op0);
6153 return simplify_gen_unary (NEG, int_mode,
6154 gen_lowpart (int_mode, op0),
6155 int_mode);
6158 else if (STORE_FLAG_VALUE == 1
6159 && new_code == EQ
6160 && is_int_mode (mode, &int_mode)
6161 && op1 == const0_rtx
6162 && int_mode == GET_MODE (op0)
6163 && nonzero_bits (op0, int_mode) == 1)
6165 op0 = expand_compound_operation (op0);
6166 return simplify_gen_binary (XOR, int_mode,
6167 gen_lowpart (int_mode, op0),
6168 const1_rtx);
6171 else if (STORE_FLAG_VALUE == 1
6172 && new_code == EQ
6173 && is_int_mode (mode, &int_mode)
6174 && op1 == const0_rtx
6175 && int_mode == GET_MODE (op0)
6176 && (num_sign_bit_copies (op0, int_mode)
6177 == GET_MODE_PRECISION (int_mode)))
6179 op0 = expand_compound_operation (op0);
6180 return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6183 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6184 those above. */
6185 if (in_cond)
6188 else if (STORE_FLAG_VALUE == -1
6189 && new_code == NE
6190 && is_int_mode (mode, &int_mode)
6191 && op1 == const0_rtx
6192 && int_mode == GET_MODE (op0)
6193 && (num_sign_bit_copies (op0, int_mode)
6194 == GET_MODE_PRECISION (int_mode)))
6195 return gen_lowpart (int_mode, expand_compound_operation (op0));
6197 else if (STORE_FLAG_VALUE == -1
6198 && new_code == NE
6199 && is_int_mode (mode, &int_mode)
6200 && op1 == const0_rtx
6201 && int_mode == GET_MODE (op0)
6202 && nonzero_bits (op0, int_mode) == 1)
6204 op0 = expand_compound_operation (op0);
6205 return simplify_gen_unary (NEG, int_mode,
6206 gen_lowpart (int_mode, op0),
6207 int_mode);
6210 else if (STORE_FLAG_VALUE == -1
6211 && new_code == EQ
6212 && is_int_mode (mode, &int_mode)
6213 && op1 == const0_rtx
6214 && int_mode == GET_MODE (op0)
6215 && (num_sign_bit_copies (op0, int_mode)
6216 == GET_MODE_PRECISION (int_mode)))
6218 op0 = expand_compound_operation (op0);
6219 return simplify_gen_unary (NOT, int_mode,
6220 gen_lowpart (int_mode, op0),
6221 int_mode);
6224 /* If X is 0/1, (eq X 0) is X-1. */
6225 else if (STORE_FLAG_VALUE == -1
6226 && new_code == EQ
6227 && is_int_mode (mode, &int_mode)
6228 && op1 == const0_rtx
6229 && int_mode == GET_MODE (op0)
6230 && nonzero_bits (op0, int_mode) == 1)
6232 op0 = expand_compound_operation (op0);
6233 return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6236 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6237 one bit that might be nonzero, we can convert (ne x 0) to
6238 (ashift x c) where C puts the bit in the sign bit. Remove any
6239 AND with STORE_FLAG_VALUE when we are done, since we are only
6240 going to test the sign bit. */
6241 if (new_code == NE
6242 && is_int_mode (mode, &int_mode)
6243 && HWI_COMPUTABLE_MODE_P (int_mode)
6244 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6245 && op1 == const0_rtx
6246 && int_mode == GET_MODE (op0)
6247 && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6249 x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6250 expand_compound_operation (op0),
6251 GET_MODE_PRECISION (int_mode) - 1 - i);
6252 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6253 return XEXP (x, 0);
6254 else
6255 return x;
6258 /* If the code changed, return a whole new comparison.
6259 We also need to avoid using SUBST in cases where
6260 simplify_comparison has widened a comparison with a CONST_INT,
6261 since in that case the wider CONST_INT may fail the sanity
6262 checks in do_SUBST. */
6263 if (new_code != code
6264 || (CONST_INT_P (op1)
6265 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6266 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6267 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6269 /* Otherwise, keep this operation, but maybe change its operands.
6270 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6271 SUBST (XEXP (x, 0), op0);
6272 SUBST (XEXP (x, 1), op1);
6274 break;
6276 case IF_THEN_ELSE:
6277 return simplify_if_then_else (x);
6279 case ZERO_EXTRACT:
6280 case SIGN_EXTRACT:
6281 case ZERO_EXTEND:
6282 case SIGN_EXTEND:
6283 /* If we are processing SET_DEST, we are done. */
6284 if (in_dest)
6285 return x;
6287 return expand_compound_operation (x);
6289 case SET:
6290 return simplify_set (x);
6292 case AND:
6293 case IOR:
6294 return simplify_logical (x);
6296 case ASHIFT:
6297 case LSHIFTRT:
6298 case ASHIFTRT:
6299 case ROTATE:
6300 case ROTATERT:
6301 /* If this is a shift by a constant amount, simplify it. */
6302 if (CONST_INT_P (XEXP (x, 1)))
6303 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6304 INTVAL (XEXP (x, 1)));
6306 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6307 SUBST (XEXP (x, 1),
6308 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6309 (HOST_WIDE_INT_1U
6310 << exact_log2 (GET_MODE_UNIT_BITSIZE
6311 (GET_MODE (x))))
6312 - 1,
6313 0));
6314 break;
6316 default:
6317 break;
6320 return x;
6323 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6325 static rtx
6326 simplify_if_then_else (rtx x)
6328 machine_mode mode = GET_MODE (x);
6329 rtx cond = XEXP (x, 0);
6330 rtx true_rtx = XEXP (x, 1);
6331 rtx false_rtx = XEXP (x, 2);
6332 enum rtx_code true_code = GET_CODE (cond);
6333 int comparison_p = COMPARISON_P (cond);
6334 rtx temp;
6335 int i;
6336 enum rtx_code false_code;
6337 rtx reversed;
6338 scalar_int_mode int_mode, inner_mode;
6340 /* Simplify storing of the truth value. */
6341 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6342 return simplify_gen_relational (true_code, mode, VOIDmode,
6343 XEXP (cond, 0), XEXP (cond, 1));
6345 /* Also when the truth value has to be reversed. */
6346 if (comparison_p
6347 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6348 && (reversed = reversed_comparison (cond, mode)))
6349 return reversed;
6351 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6352 in it is being compared against certain values. Get the true and false
6353 comparisons and see if that says anything about the value of each arm. */
6355 if (comparison_p
6356 && ((false_code = reversed_comparison_code (cond, NULL))
6357 != UNKNOWN)
6358 && REG_P (XEXP (cond, 0)))
6360 HOST_WIDE_INT nzb;
6361 rtx from = XEXP (cond, 0);
6362 rtx true_val = XEXP (cond, 1);
6363 rtx false_val = true_val;
6364 int swapped = 0;
6366 /* If FALSE_CODE is EQ, swap the codes and arms. */
6368 if (false_code == EQ)
6370 swapped = 1, true_code = EQ, false_code = NE;
6371 std::swap (true_rtx, false_rtx);
6374 scalar_int_mode from_mode;
6375 if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6377 /* If we are comparing against zero and the expression being
6378 tested has only a single bit that might be nonzero, that is
6379 its value when it is not equal to zero. Similarly if it is
6380 known to be -1 or 0. */
6381 if (true_code == EQ
6382 && true_val == const0_rtx
6383 && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6385 false_code = EQ;
6386 false_val = gen_int_mode (nzb, from_mode);
6388 else if (true_code == EQ
6389 && true_val == const0_rtx
6390 && (num_sign_bit_copies (from, from_mode)
6391 == GET_MODE_PRECISION (from_mode)))
6393 false_code = EQ;
6394 false_val = constm1_rtx;
6398 /* Now simplify an arm if we know the value of the register in the
6399 branch and it is used in the arm. Be careful due to the potential
6400 of locally-shared RTL. */
6402 if (reg_mentioned_p (from, true_rtx))
6403 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6404 from, true_val),
6405 pc_rtx, pc_rtx, 0, 0, 0);
6406 if (reg_mentioned_p (from, false_rtx))
6407 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6408 from, false_val),
6409 pc_rtx, pc_rtx, 0, 0, 0);
6411 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6412 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6414 true_rtx = XEXP (x, 1);
6415 false_rtx = XEXP (x, 2);
6416 true_code = GET_CODE (cond);
6419 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6420 reversed, do so to avoid needing two sets of patterns for
6421 subtract-and-branch insns. Similarly if we have a constant in the true
6422 arm, the false arm is the same as the first operand of the comparison, or
6423 the false arm is more complicated than the true arm. */
6425 if (comparison_p
6426 && reversed_comparison_code (cond, NULL) != UNKNOWN
6427 && (true_rtx == pc_rtx
6428 || (CONSTANT_P (true_rtx)
6429 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6430 || true_rtx == const0_rtx
6431 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6432 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6433 && !OBJECT_P (false_rtx))
6434 || reg_mentioned_p (true_rtx, false_rtx)
6435 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6437 true_code = reversed_comparison_code (cond, NULL);
6438 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6439 SUBST (XEXP (x, 1), false_rtx);
6440 SUBST (XEXP (x, 2), true_rtx);
6442 std::swap (true_rtx, false_rtx);
6443 cond = XEXP (x, 0);
6445 /* It is possible that the conditional has been simplified out. */
6446 true_code = GET_CODE (cond);
6447 comparison_p = COMPARISON_P (cond);
6450 /* If the two arms are identical, we don't need the comparison. */
6452 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6453 return true_rtx;
6455 /* Convert a == b ? b : a to "a". */
6456 if (true_code == EQ && ! side_effects_p (cond)
6457 && !HONOR_NANS (mode)
6458 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6459 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6460 return false_rtx;
6461 else if (true_code == NE && ! side_effects_p (cond)
6462 && !HONOR_NANS (mode)
6463 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6464 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6465 return true_rtx;
6467 /* Look for cases where we have (abs x) or (neg (abs X)). */
6469 if (GET_MODE_CLASS (mode) == MODE_INT
6470 && comparison_p
6471 && XEXP (cond, 1) == const0_rtx
6472 && GET_CODE (false_rtx) == NEG
6473 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6474 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6475 && ! side_effects_p (true_rtx))
6476 switch (true_code)
6478 case GT:
6479 case GE:
6480 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6481 case LT:
6482 case LE:
6483 return
6484 simplify_gen_unary (NEG, mode,
6485 simplify_gen_unary (ABS, mode, true_rtx, mode),
6486 mode);
6487 default:
6488 break;
6491 /* Look for MIN or MAX. */
6493 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6494 && comparison_p
6495 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6496 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6497 && ! side_effects_p (cond))
6498 switch (true_code)
6500 case GE:
6501 case GT:
6502 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6503 case LE:
6504 case LT:
6505 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6506 case GEU:
6507 case GTU:
6508 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6509 case LEU:
6510 case LTU:
6511 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6512 default:
6513 break;
6516 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6517 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6518 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6519 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6520 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6521 neither 1 or -1, but it isn't worth checking for. */
6523 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6524 && comparison_p
6525 && is_int_mode (mode, &int_mode)
6526 && ! side_effects_p (x))
6528 rtx t = make_compound_operation (true_rtx, SET);
6529 rtx f = make_compound_operation (false_rtx, SET);
6530 rtx cond_op0 = XEXP (cond, 0);
6531 rtx cond_op1 = XEXP (cond, 1);
6532 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6533 scalar_int_mode m = int_mode;
6534 rtx z = 0, c1 = NULL_RTX;
6536 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6537 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6538 || GET_CODE (t) == ASHIFT
6539 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6540 && rtx_equal_p (XEXP (t, 0), f))
6541 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6543 /* If an identity-zero op is commutative, check whether there
6544 would be a match if we swapped the operands. */
6545 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6546 || GET_CODE (t) == XOR)
6547 && rtx_equal_p (XEXP (t, 1), f))
6548 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6549 else if (GET_CODE (t) == SIGN_EXTEND
6550 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6551 && (GET_CODE (XEXP (t, 0)) == PLUS
6552 || GET_CODE (XEXP (t, 0)) == MINUS
6553 || GET_CODE (XEXP (t, 0)) == IOR
6554 || GET_CODE (XEXP (t, 0)) == XOR
6555 || GET_CODE (XEXP (t, 0)) == ASHIFT
6556 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6557 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6558 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6559 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6560 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6561 && (num_sign_bit_copies (f, GET_MODE (f))
6562 > (unsigned int)
6563 (GET_MODE_PRECISION (int_mode)
6564 - GET_MODE_PRECISION (inner_mode))))
6566 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6567 extend_op = SIGN_EXTEND;
6568 m = inner_mode;
6570 else if (GET_CODE (t) == SIGN_EXTEND
6571 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6572 && (GET_CODE (XEXP (t, 0)) == PLUS
6573 || GET_CODE (XEXP (t, 0)) == IOR
6574 || GET_CODE (XEXP (t, 0)) == XOR)
6575 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6576 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6577 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6578 && (num_sign_bit_copies (f, GET_MODE (f))
6579 > (unsigned int)
6580 (GET_MODE_PRECISION (int_mode)
6581 - GET_MODE_PRECISION (inner_mode))))
6583 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6584 extend_op = SIGN_EXTEND;
6585 m = inner_mode;
6587 else if (GET_CODE (t) == ZERO_EXTEND
6588 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6589 && (GET_CODE (XEXP (t, 0)) == PLUS
6590 || GET_CODE (XEXP (t, 0)) == MINUS
6591 || GET_CODE (XEXP (t, 0)) == IOR
6592 || GET_CODE (XEXP (t, 0)) == XOR
6593 || GET_CODE (XEXP (t, 0)) == ASHIFT
6594 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6595 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6596 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6597 && HWI_COMPUTABLE_MODE_P (int_mode)
6598 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6599 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6600 && ((nonzero_bits (f, GET_MODE (f))
6601 & ~GET_MODE_MASK (inner_mode))
6602 == 0))
6604 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6605 extend_op = ZERO_EXTEND;
6606 m = inner_mode;
6608 else if (GET_CODE (t) == ZERO_EXTEND
6609 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6610 && (GET_CODE (XEXP (t, 0)) == PLUS
6611 || GET_CODE (XEXP (t, 0)) == IOR
6612 || GET_CODE (XEXP (t, 0)) == XOR)
6613 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6614 && HWI_COMPUTABLE_MODE_P (int_mode)
6615 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6616 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6617 && ((nonzero_bits (f, GET_MODE (f))
6618 & ~GET_MODE_MASK (inner_mode))
6619 == 0))
6621 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6622 extend_op = ZERO_EXTEND;
6623 m = inner_mode;
6626 if (z)
6628 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6629 cond_op0, cond_op1),
6630 pc_rtx, pc_rtx, 0, 0, 0);
6631 temp = simplify_gen_binary (MULT, m, temp,
6632 simplify_gen_binary (MULT, m, c1,
6633 const_true_rtx));
6634 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6635 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6637 if (extend_op != UNKNOWN)
6638 temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6640 return temp;
6644 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6645 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6646 negation of a single bit, we can convert this operation to a shift. We
6647 can actually do this more generally, but it doesn't seem worth it. */
6649 if (true_code == NE
6650 && is_a <scalar_int_mode> (mode, &int_mode)
6651 && XEXP (cond, 1) == const0_rtx
6652 && false_rtx == const0_rtx
6653 && CONST_INT_P (true_rtx)
6654 && ((1 == nonzero_bits (XEXP (cond, 0), int_mode)
6655 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6656 || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6657 == GET_MODE_PRECISION (int_mode))
6658 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6659 return
6660 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6661 gen_lowpart (int_mode, XEXP (cond, 0)), i);
6663 /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6664 non-zero bit in A is C1. */
6665 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6666 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6667 && is_a <scalar_int_mode> (mode, &int_mode)
6668 && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6669 && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6670 == nonzero_bits (XEXP (cond, 0), inner_mode)
6671 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6673 rtx val = XEXP (cond, 0);
6674 if (inner_mode == int_mode)
6675 return val;
6676 else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6677 return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6680 return x;
6683 /* Simplify X, a SET expression. Return the new expression. */
6685 static rtx
6686 simplify_set (rtx x)
6688 rtx src = SET_SRC (x);
6689 rtx dest = SET_DEST (x);
6690 machine_mode mode
6691 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6692 rtx_insn *other_insn;
6693 rtx *cc_use;
6694 scalar_int_mode int_mode;
6696 /* (set (pc) (return)) gets written as (return). */
6697 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6698 return src;
6700 /* Now that we know for sure which bits of SRC we are using, see if we can
6701 simplify the expression for the object knowing that we only need the
6702 low-order bits. */
6704 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6706 src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, 0);
6707 SUBST (SET_SRC (x), src);
6710 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6711 the comparison result and try to simplify it unless we already have used
6712 undobuf.other_insn. */
6713 if ((GET_MODE_CLASS (mode) == MODE_CC
6714 || GET_CODE (src) == COMPARE
6715 || CC0_P (dest))
6716 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6717 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6718 && COMPARISON_P (*cc_use)
6719 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6721 enum rtx_code old_code = GET_CODE (*cc_use);
6722 enum rtx_code new_code;
6723 rtx op0, op1, tmp;
6724 int other_changed = 0;
6725 rtx inner_compare = NULL_RTX;
6726 machine_mode compare_mode = GET_MODE (dest);
6728 if (GET_CODE (src) == COMPARE)
6730 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6731 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6733 inner_compare = op0;
6734 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6737 else
6738 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6740 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6741 op0, op1);
6742 if (!tmp)
6743 new_code = old_code;
6744 else if (!CONSTANT_P (tmp))
6746 new_code = GET_CODE (tmp);
6747 op0 = XEXP (tmp, 0);
6748 op1 = XEXP (tmp, 1);
6750 else
6752 rtx pat = PATTERN (other_insn);
6753 undobuf.other_insn = other_insn;
6754 SUBST (*cc_use, tmp);
6756 /* Attempt to simplify CC user. */
6757 if (GET_CODE (pat) == SET)
6759 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6760 if (new_rtx != NULL_RTX)
6761 SUBST (SET_SRC (pat), new_rtx);
6764 /* Convert X into a no-op move. */
6765 SUBST (SET_DEST (x), pc_rtx);
6766 SUBST (SET_SRC (x), pc_rtx);
6767 return x;
6770 /* Simplify our comparison, if possible. */
6771 new_code = simplify_comparison (new_code, &op0, &op1);
6773 #ifdef SELECT_CC_MODE
6774 /* If this machine has CC modes other than CCmode, check to see if we
6775 need to use a different CC mode here. */
6776 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6777 compare_mode = GET_MODE (op0);
6778 else if (inner_compare
6779 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6780 && new_code == old_code
6781 && op0 == XEXP (inner_compare, 0)
6782 && op1 == XEXP (inner_compare, 1))
6783 compare_mode = GET_MODE (inner_compare);
6784 else
6785 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6787 /* If the mode changed, we have to change SET_DEST, the mode in the
6788 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6789 a hard register, just build new versions with the proper mode. If it
6790 is a pseudo, we lose unless it is only time we set the pseudo, in
6791 which case we can safely change its mode. */
6792 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6794 if (can_change_dest_mode (dest, 0, compare_mode))
6796 unsigned int regno = REGNO (dest);
6797 rtx new_dest;
6799 if (regno < FIRST_PSEUDO_REGISTER)
6800 new_dest = gen_rtx_REG (compare_mode, regno);
6801 else
6803 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6804 new_dest = regno_reg_rtx[regno];
6807 SUBST (SET_DEST (x), new_dest);
6808 SUBST (XEXP (*cc_use, 0), new_dest);
6809 other_changed = 1;
6811 dest = new_dest;
6814 #endif /* SELECT_CC_MODE */
6816 /* If the code changed, we have to build a new comparison in
6817 undobuf.other_insn. */
6818 if (new_code != old_code)
6820 int other_changed_previously = other_changed;
6821 unsigned HOST_WIDE_INT mask;
6822 rtx old_cc_use = *cc_use;
6824 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6825 dest, const0_rtx));
6826 other_changed = 1;
6828 /* If the only change we made was to change an EQ into an NE or
6829 vice versa, OP0 has only one bit that might be nonzero, and OP1
6830 is zero, check if changing the user of the condition code will
6831 produce a valid insn. If it won't, we can keep the original code
6832 in that insn by surrounding our operation with an XOR. */
6834 if (((old_code == NE && new_code == EQ)
6835 || (old_code == EQ && new_code == NE))
6836 && ! other_changed_previously && op1 == const0_rtx
6837 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6838 && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6840 rtx pat = PATTERN (other_insn), note = 0;
6842 if ((recog_for_combine (&pat, other_insn, &note) < 0
6843 && ! check_asm_operands (pat)))
6845 *cc_use = old_cc_use;
6846 other_changed = 0;
6848 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6849 gen_int_mode (mask,
6850 GET_MODE (op0)));
6855 if (other_changed)
6856 undobuf.other_insn = other_insn;
6858 /* Don't generate a compare of a CC with 0, just use that CC. */
6859 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6861 SUBST (SET_SRC (x), op0);
6862 src = SET_SRC (x);
6864 /* Otherwise, if we didn't previously have the same COMPARE we
6865 want, create it from scratch. */
6866 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6867 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6869 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6870 src = SET_SRC (x);
6873 else
6875 /* Get SET_SRC in a form where we have placed back any
6876 compound expressions. Then do the checks below. */
6877 src = make_compound_operation (src, SET);
6878 SUBST (SET_SRC (x), src);
6881 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6882 and X being a REG or (subreg (reg)), we may be able to convert this to
6883 (set (subreg:m2 x) (op)).
6885 We can always do this if M1 is narrower than M2 because that means that
6886 we only care about the low bits of the result.
6888 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6889 perform a narrower operation than requested since the high-order bits will
6890 be undefined. On machine where it is defined, this transformation is safe
6891 as long as M1 and M2 have the same number of words. */
6893 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6894 && !OBJECT_P (SUBREG_REG (src))
6895 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6896 / UNITS_PER_WORD)
6897 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6898 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6899 && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
6900 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6901 && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
6902 GET_MODE (SUBREG_REG (src)),
6903 GET_MODE (src)))
6904 && (REG_P (dest)
6905 || (GET_CODE (dest) == SUBREG
6906 && REG_P (SUBREG_REG (dest)))))
6908 SUBST (SET_DEST (x),
6909 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6910 dest));
6911 SUBST (SET_SRC (x), SUBREG_REG (src));
6913 src = SET_SRC (x), dest = SET_DEST (x);
6916 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6917 in SRC. */
6918 if (dest == cc0_rtx
6919 && partial_subreg_p (src)
6920 && subreg_lowpart_p (src))
6922 rtx inner = SUBREG_REG (src);
6923 machine_mode inner_mode = GET_MODE (inner);
6925 /* Here we make sure that we don't have a sign bit on. */
6926 if (val_signbit_known_clear_p (GET_MODE (src),
6927 nonzero_bits (inner, inner_mode)))
6929 SUBST (SET_SRC (x), inner);
6930 src = SET_SRC (x);
6934 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6935 would require a paradoxical subreg. Replace the subreg with a
6936 zero_extend to avoid the reload that would otherwise be required. */
6938 enum rtx_code extend_op;
6939 if (paradoxical_subreg_p (src)
6940 && MEM_P (SUBREG_REG (src))
6941 && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
6943 SUBST (SET_SRC (x),
6944 gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
6946 src = SET_SRC (x);
6949 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6950 are comparing an item known to be 0 or -1 against 0, use a logical
6951 operation instead. Check for one of the arms being an IOR of the other
6952 arm with some value. We compute three terms to be IOR'ed together. In
6953 practice, at most two will be nonzero. Then we do the IOR's. */
6955 if (GET_CODE (dest) != PC
6956 && GET_CODE (src) == IF_THEN_ELSE
6957 && is_int_mode (GET_MODE (src), &int_mode)
6958 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6959 && XEXP (XEXP (src, 0), 1) == const0_rtx
6960 && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
6961 && (!HAVE_conditional_move
6962 || ! can_conditionally_move_p (int_mode))
6963 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
6964 == GET_MODE_PRECISION (int_mode))
6965 && ! side_effects_p (src))
6967 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6968 ? XEXP (src, 1) : XEXP (src, 2));
6969 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6970 ? XEXP (src, 2) : XEXP (src, 1));
6971 rtx term1 = const0_rtx, term2, term3;
6973 if (GET_CODE (true_rtx) == IOR
6974 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6975 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6976 else if (GET_CODE (true_rtx) == IOR
6977 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6978 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6979 else if (GET_CODE (false_rtx) == IOR
6980 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6981 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6982 else if (GET_CODE (false_rtx) == IOR
6983 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6984 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6986 term2 = simplify_gen_binary (AND, int_mode,
6987 XEXP (XEXP (src, 0), 0), true_rtx);
6988 term3 = simplify_gen_binary (AND, int_mode,
6989 simplify_gen_unary (NOT, int_mode,
6990 XEXP (XEXP (src, 0), 0),
6991 int_mode),
6992 false_rtx);
6994 SUBST (SET_SRC (x),
6995 simplify_gen_binary (IOR, int_mode,
6996 simplify_gen_binary (IOR, int_mode,
6997 term1, term2),
6998 term3));
7000 src = SET_SRC (x);
7003 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7004 whole thing fail. */
7005 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7006 return src;
7007 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7008 return dest;
7009 else
7010 /* Convert this into a field assignment operation, if possible. */
7011 return make_field_assignment (x);
7014 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7015 result. */
7017 static rtx
7018 simplify_logical (rtx x)
7020 rtx op0 = XEXP (x, 0);
7021 rtx op1 = XEXP (x, 1);
7022 scalar_int_mode mode;
7024 switch (GET_CODE (x))
7026 case AND:
7027 /* We can call simplify_and_const_int only if we don't lose
7028 any (sign) bits when converting INTVAL (op1) to
7029 "unsigned HOST_WIDE_INT". */
7030 if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7031 && CONST_INT_P (op1)
7032 && (HWI_COMPUTABLE_MODE_P (mode)
7033 || INTVAL (op1) > 0))
7035 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7036 if (GET_CODE (x) != AND)
7037 return x;
7039 op0 = XEXP (x, 0);
7040 op1 = XEXP (x, 1);
7043 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7044 apply the distributive law and then the inverse distributive
7045 law to see if things simplify. */
7046 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7048 rtx result = distribute_and_simplify_rtx (x, 0);
7049 if (result)
7050 return result;
7052 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7054 rtx result = distribute_and_simplify_rtx (x, 1);
7055 if (result)
7056 return result;
7058 break;
7060 case IOR:
7061 /* If we have (ior (and A B) C), apply the distributive law and then
7062 the inverse distributive law to see if things simplify. */
7064 if (GET_CODE (op0) == AND)
7066 rtx result = distribute_and_simplify_rtx (x, 0);
7067 if (result)
7068 return result;
7071 if (GET_CODE (op1) == AND)
7073 rtx result = distribute_and_simplify_rtx (x, 1);
7074 if (result)
7075 return result;
7077 break;
7079 default:
7080 gcc_unreachable ();
7083 return x;
7086 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7087 operations" because they can be replaced with two more basic operations.
7088 ZERO_EXTEND is also considered "compound" because it can be replaced with
7089 an AND operation, which is simpler, though only one operation.
7091 The function expand_compound_operation is called with an rtx expression
7092 and will convert it to the appropriate shifts and AND operations,
7093 simplifying at each stage.
7095 The function make_compound_operation is called to convert an expression
7096 consisting of shifts and ANDs into the equivalent compound expression.
7097 It is the inverse of this function, loosely speaking. */
7099 static rtx
7100 expand_compound_operation (rtx x)
7102 unsigned HOST_WIDE_INT pos = 0, len;
7103 int unsignedp = 0;
7104 unsigned int modewidth;
7105 rtx tem;
7106 scalar_int_mode inner_mode;
7108 switch (GET_CODE (x))
7110 case ZERO_EXTEND:
7111 unsignedp = 1;
7112 /* FALLTHRU */
7113 case SIGN_EXTEND:
7114 /* We can't necessarily use a const_int for a multiword mode;
7115 it depends on implicitly extending the value.
7116 Since we don't know the right way to extend it,
7117 we can't tell whether the implicit way is right.
7119 Even for a mode that is no wider than a const_int,
7120 we can't win, because we need to sign extend one of its bits through
7121 the rest of it, and we don't know which bit. */
7122 if (CONST_INT_P (XEXP (x, 0)))
7123 return x;
7125 /* Reject modes that aren't scalar integers because turning vector
7126 or complex modes into shifts causes problems. */
7127 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7128 return x;
7130 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7131 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7132 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7133 reloaded. If not for that, MEM's would very rarely be safe.
7135 Reject modes bigger than a word, because we might not be able
7136 to reference a two-register group starting with an arbitrary register
7137 (and currently gen_lowpart might crash for a SUBREG). */
7139 if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7140 return x;
7142 len = GET_MODE_PRECISION (inner_mode);
7143 /* If the inner object has VOIDmode (the only way this can happen
7144 is if it is an ASM_OPERANDS), we can't do anything since we don't
7145 know how much masking to do. */
7146 if (len == 0)
7147 return x;
7149 break;
7151 case ZERO_EXTRACT:
7152 unsignedp = 1;
7154 /* fall through */
7156 case SIGN_EXTRACT:
7157 /* If the operand is a CLOBBER, just return it. */
7158 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7159 return XEXP (x, 0);
7161 if (!CONST_INT_P (XEXP (x, 1))
7162 || !CONST_INT_P (XEXP (x, 2)))
7163 return x;
7165 /* Reject modes that aren't scalar integers because turning vector
7166 or complex modes into shifts causes problems. */
7167 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7168 return x;
7170 len = INTVAL (XEXP (x, 1));
7171 pos = INTVAL (XEXP (x, 2));
7173 /* This should stay within the object being extracted, fail otherwise. */
7174 if (len + pos > GET_MODE_PRECISION (inner_mode))
7175 return x;
7177 if (BITS_BIG_ENDIAN)
7178 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7180 break;
7182 default:
7183 return x;
7186 /* We've rejected non-scalar operations by now. */
7187 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7189 /* Convert sign extension to zero extension, if we know that the high
7190 bit is not set, as this is easier to optimize. It will be converted
7191 back to cheaper alternative in make_extraction. */
7192 if (GET_CODE (x) == SIGN_EXTEND
7193 && HWI_COMPUTABLE_MODE_P (mode)
7194 && ((nonzero_bits (XEXP (x, 0), inner_mode)
7195 & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7196 == 0))
7198 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7199 rtx temp2 = expand_compound_operation (temp);
7201 /* Make sure this is a profitable operation. */
7202 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7203 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7204 return temp2;
7205 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7206 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7207 return temp;
7208 else
7209 return x;
7212 /* We can optimize some special cases of ZERO_EXTEND. */
7213 if (GET_CODE (x) == ZERO_EXTEND)
7215 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7216 know that the last value didn't have any inappropriate bits
7217 set. */
7218 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7219 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7220 && HWI_COMPUTABLE_MODE_P (mode)
7221 && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7222 & ~GET_MODE_MASK (inner_mode)) == 0)
7223 return XEXP (XEXP (x, 0), 0);
7225 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7226 if (GET_CODE (XEXP (x, 0)) == SUBREG
7227 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7228 && subreg_lowpart_p (XEXP (x, 0))
7229 && HWI_COMPUTABLE_MODE_P (mode)
7230 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7231 & ~GET_MODE_MASK (inner_mode)) == 0)
7232 return SUBREG_REG (XEXP (x, 0));
7234 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7235 is a comparison and STORE_FLAG_VALUE permits. This is like
7236 the first case, but it works even when MODE is larger
7237 than HOST_WIDE_INT. */
7238 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7239 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7240 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7241 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7242 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7243 return XEXP (XEXP (x, 0), 0);
7245 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7246 if (GET_CODE (XEXP (x, 0)) == SUBREG
7247 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7248 && subreg_lowpart_p (XEXP (x, 0))
7249 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7250 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7251 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7252 return SUBREG_REG (XEXP (x, 0));
7256 /* If we reach here, we want to return a pair of shifts. The inner
7257 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7258 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7259 logical depending on the value of UNSIGNEDP.
7261 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7262 converted into an AND of a shift.
7264 We must check for the case where the left shift would have a negative
7265 count. This can happen in a case like (x >> 31) & 255 on machines
7266 that can't shift by a constant. On those machines, we would first
7267 combine the shift with the AND to produce a variable-position
7268 extraction. Then the constant of 31 would be substituted in
7269 to produce such a position. */
7271 modewidth = GET_MODE_PRECISION (mode);
7272 if (modewidth >= pos + len)
7274 tem = gen_lowpart (mode, XEXP (x, 0));
7275 if (!tem || GET_CODE (tem) == CLOBBER)
7276 return x;
7277 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7278 tem, modewidth - pos - len);
7279 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7280 mode, tem, modewidth - len);
7282 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7283 tem = simplify_and_const_int (NULL_RTX, mode,
7284 simplify_shift_const (NULL_RTX, LSHIFTRT,
7285 mode, XEXP (x, 0),
7286 pos),
7287 (HOST_WIDE_INT_1U << len) - 1);
7288 else
7289 /* Any other cases we can't handle. */
7290 return x;
7292 /* If we couldn't do this for some reason, return the original
7293 expression. */
7294 if (GET_CODE (tem) == CLOBBER)
7295 return x;
7297 return tem;
7300 /* X is a SET which contains an assignment of one object into
7301 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7302 or certain SUBREGS). If possible, convert it into a series of
7303 logical operations.
7305 We half-heartedly support variable positions, but do not at all
7306 support variable lengths. */
7308 static const_rtx
7309 expand_field_assignment (const_rtx x)
7311 rtx inner;
7312 rtx pos; /* Always counts from low bit. */
7313 int len;
7314 rtx mask, cleared, masked;
7315 scalar_int_mode compute_mode;
7317 /* Loop until we find something we can't simplify. */
7318 while (1)
7320 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7321 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7323 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7324 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7325 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7327 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7328 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7330 inner = XEXP (SET_DEST (x), 0);
7331 len = INTVAL (XEXP (SET_DEST (x), 1));
7332 pos = XEXP (SET_DEST (x), 2);
7334 /* A constant position should stay within the width of INNER. */
7335 if (CONST_INT_P (pos)
7336 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7337 break;
7339 if (BITS_BIG_ENDIAN)
7341 if (CONST_INT_P (pos))
7342 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7343 - INTVAL (pos));
7344 else if (GET_CODE (pos) == MINUS
7345 && CONST_INT_P (XEXP (pos, 1))
7346 && (INTVAL (XEXP (pos, 1))
7347 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7348 /* If position is ADJUST - X, new position is X. */
7349 pos = XEXP (pos, 0);
7350 else
7352 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7353 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7354 gen_int_mode (prec - len,
7355 GET_MODE (pos)),
7356 pos);
7361 /* If the destination is a subreg that overwrites the whole of the inner
7362 register, we can move the subreg to the source. */
7363 else if (GET_CODE (SET_DEST (x)) == SUBREG
7364 /* We need SUBREGs to compute nonzero_bits properly. */
7365 && nonzero_sign_valid
7366 && !read_modify_subreg_p (SET_DEST (x)))
7368 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7369 gen_lowpart
7370 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7371 SET_SRC (x)));
7372 continue;
7374 else
7375 break;
7377 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7378 inner = SUBREG_REG (inner);
7380 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7381 if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7383 /* Don't do anything for vector or complex integral types. */
7384 if (! FLOAT_MODE_P (GET_MODE (inner)))
7385 break;
7387 /* Try to find an integral mode to pun with. */
7388 if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7389 .exists (&compute_mode))
7390 break;
7392 inner = gen_lowpart (compute_mode, inner);
7395 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7396 if (len >= HOST_BITS_PER_WIDE_INT)
7397 break;
7399 /* Don't try to compute in too wide unsupported modes. */
7400 if (!targetm.scalar_mode_supported_p (compute_mode))
7401 break;
7403 /* Now compute the equivalent expression. Make a copy of INNER
7404 for the SET_DEST in case it is a MEM into which we will substitute;
7405 we don't want shared RTL in that case. */
7406 mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7407 compute_mode);
7408 cleared = simplify_gen_binary (AND, compute_mode,
7409 simplify_gen_unary (NOT, compute_mode,
7410 simplify_gen_binary (ASHIFT,
7411 compute_mode,
7412 mask, pos),
7413 compute_mode),
7414 inner);
7415 masked = simplify_gen_binary (ASHIFT, compute_mode,
7416 simplify_gen_binary (
7417 AND, compute_mode,
7418 gen_lowpart (compute_mode, SET_SRC (x)),
7419 mask),
7420 pos);
7422 x = gen_rtx_SET (copy_rtx (inner),
7423 simplify_gen_binary (IOR, compute_mode,
7424 cleared, masked));
7427 return x;
7430 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7431 it is an RTX that represents the (variable) starting position; otherwise,
7432 POS is the (constant) starting bit position. Both are counted from the LSB.
7434 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7436 IN_DEST is nonzero if this is a reference in the destination of a SET.
7437 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7438 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7439 be used.
7441 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7442 ZERO_EXTRACT should be built even for bits starting at bit 0.
7444 MODE is the desired mode of the result (if IN_DEST == 0).
7446 The result is an RTX for the extraction or NULL_RTX if the target
7447 can't handle it. */
7449 static rtx
7450 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7451 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7452 int in_dest, int in_compare)
7454 /* This mode describes the size of the storage area
7455 to fetch the overall value from. Within that, we
7456 ignore the POS lowest bits, etc. */
7457 machine_mode is_mode = GET_MODE (inner);
7458 machine_mode inner_mode;
7459 scalar_int_mode wanted_inner_mode;
7460 scalar_int_mode wanted_inner_reg_mode = word_mode;
7461 scalar_int_mode pos_mode = word_mode;
7462 machine_mode extraction_mode = word_mode;
7463 rtx new_rtx = 0;
7464 rtx orig_pos_rtx = pos_rtx;
7465 HOST_WIDE_INT orig_pos;
7467 if (pos_rtx && CONST_INT_P (pos_rtx))
7468 pos = INTVAL (pos_rtx), pos_rtx = 0;
7470 if (GET_CODE (inner) == SUBREG
7471 && subreg_lowpart_p (inner)
7472 && (paradoxical_subreg_p (inner)
7473 /* If trying or potentionally trying to extract
7474 bits outside of is_mode, don't look through
7475 non-paradoxical SUBREGs. See PR82192. */
7476 || (pos_rtx == NULL_RTX
7477 && pos + len <= GET_MODE_PRECISION (is_mode))))
7479 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7480 consider just the QI as the memory to extract from.
7481 The subreg adds or removes high bits; its mode is
7482 irrelevant to the meaning of this extraction,
7483 since POS and LEN count from the lsb. */
7484 if (MEM_P (SUBREG_REG (inner)))
7485 is_mode = GET_MODE (SUBREG_REG (inner));
7486 inner = SUBREG_REG (inner);
7488 else if (GET_CODE (inner) == ASHIFT
7489 && CONST_INT_P (XEXP (inner, 1))
7490 && pos_rtx == 0 && pos == 0
7491 && len > UINTVAL (XEXP (inner, 1)))
7493 /* We're extracting the least significant bits of an rtx
7494 (ashift X (const_int C)), where LEN > C. Extract the
7495 least significant (LEN - C) bits of X, giving an rtx
7496 whose mode is MODE, then shift it left C times. */
7497 new_rtx = make_extraction (mode, XEXP (inner, 0),
7498 0, 0, len - INTVAL (XEXP (inner, 1)),
7499 unsignedp, in_dest, in_compare);
7500 if (new_rtx != 0)
7501 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7503 else if (GET_CODE (inner) == TRUNCATE
7504 /* If trying or potentionally trying to extract
7505 bits outside of is_mode, don't look through
7506 TRUNCATE. See PR82192. */
7507 && pos_rtx == NULL_RTX
7508 && pos + len <= GET_MODE_PRECISION (is_mode))
7509 inner = XEXP (inner, 0);
7511 inner_mode = GET_MODE (inner);
7513 /* See if this can be done without an extraction. We never can if the
7514 width of the field is not the same as that of some integer mode. For
7515 registers, we can only avoid the extraction if the position is at the
7516 low-order bit and this is either not in the destination or we have the
7517 appropriate STRICT_LOW_PART operation available.
7519 For MEM, we can avoid an extract if the field starts on an appropriate
7520 boundary and we can change the mode of the memory reference. */
7522 scalar_int_mode tmode;
7523 if (int_mode_for_size (len, 1).exists (&tmode)
7524 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7525 && !MEM_P (inner)
7526 && (pos == 0 || REG_P (inner))
7527 && (inner_mode == tmode
7528 || !REG_P (inner)
7529 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7530 || reg_truncated_to_mode (tmode, inner))
7531 && (! in_dest
7532 || (REG_P (inner)
7533 && have_insn_for (STRICT_LOW_PART, tmode))))
7534 || (MEM_P (inner) && pos_rtx == 0
7535 && (pos
7536 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7537 : BITS_PER_UNIT)) == 0
7538 /* We can't do this if we are widening INNER_MODE (it
7539 may not be aligned, for one thing). */
7540 && !paradoxical_subreg_p (tmode, inner_mode)
7541 && (inner_mode == tmode
7542 || (! mode_dependent_address_p (XEXP (inner, 0),
7543 MEM_ADDR_SPACE (inner))
7544 && ! MEM_VOLATILE_P (inner))))))
7546 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7547 field. If the original and current mode are the same, we need not
7548 adjust the offset. Otherwise, we do if bytes big endian.
7550 If INNER is not a MEM, get a piece consisting of just the field
7551 of interest (in this case POS % BITS_PER_WORD must be 0). */
7553 if (MEM_P (inner))
7555 HOST_WIDE_INT offset;
7557 /* POS counts from lsb, but make OFFSET count in memory order. */
7558 if (BYTES_BIG_ENDIAN)
7559 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7560 else
7561 offset = pos / BITS_PER_UNIT;
7563 new_rtx = adjust_address_nv (inner, tmode, offset);
7565 else if (REG_P (inner))
7567 if (tmode != inner_mode)
7569 /* We can't call gen_lowpart in a DEST since we
7570 always want a SUBREG (see below) and it would sometimes
7571 return a new hard register. */
7572 if (pos || in_dest)
7574 unsigned int offset
7575 = subreg_offset_from_lsb (tmode, inner_mode, pos);
7577 /* Avoid creating invalid subregs, for example when
7578 simplifying (x>>32)&255. */
7579 if (!validate_subreg (tmode, inner_mode, inner, offset))
7580 return NULL_RTX;
7582 new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7584 else
7585 new_rtx = gen_lowpart (tmode, inner);
7587 else
7588 new_rtx = inner;
7590 else
7591 new_rtx = force_to_mode (inner, tmode,
7592 len >= HOST_BITS_PER_WIDE_INT
7593 ? HOST_WIDE_INT_M1U
7594 : (HOST_WIDE_INT_1U << len) - 1, 0);
7596 /* If this extraction is going into the destination of a SET,
7597 make a STRICT_LOW_PART unless we made a MEM. */
7599 if (in_dest)
7600 return (MEM_P (new_rtx) ? new_rtx
7601 : (GET_CODE (new_rtx) != SUBREG
7602 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7603 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7605 if (mode == tmode)
7606 return new_rtx;
7608 if (CONST_SCALAR_INT_P (new_rtx))
7609 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7610 mode, new_rtx, tmode);
7612 /* If we know that no extraneous bits are set, and that the high
7613 bit is not set, convert the extraction to the cheaper of
7614 sign and zero extension, that are equivalent in these cases. */
7615 if (flag_expensive_optimizations
7616 && (HWI_COMPUTABLE_MODE_P (tmode)
7617 && ((nonzero_bits (new_rtx, tmode)
7618 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7619 == 0)))
7621 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7622 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7624 /* Prefer ZERO_EXTENSION, since it gives more information to
7625 backends. */
7626 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7627 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7628 return temp;
7629 return temp1;
7632 /* Otherwise, sign- or zero-extend unless we already are in the
7633 proper mode. */
7635 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7636 mode, new_rtx));
7639 /* Unless this is a COMPARE or we have a funny memory reference,
7640 don't do anything with zero-extending field extracts starting at
7641 the low-order bit since they are simple AND operations. */
7642 if (pos_rtx == 0 && pos == 0 && ! in_dest
7643 && ! in_compare && unsignedp)
7644 return 0;
7646 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7647 if the position is not a constant and the length is not 1. In all
7648 other cases, we would only be going outside our object in cases when
7649 an original shift would have been undefined. */
7650 if (MEM_P (inner)
7651 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7652 || (pos_rtx != 0 && len != 1)))
7653 return 0;
7655 enum extraction_pattern pattern = (in_dest ? EP_insv
7656 : unsignedp ? EP_extzv : EP_extv);
7658 /* If INNER is not from memory, we want it to have the mode of a register
7659 extraction pattern's structure operand, or word_mode if there is no
7660 such pattern. The same applies to extraction_mode and pos_mode
7661 and their respective operands.
7663 For memory, assume that the desired extraction_mode and pos_mode
7664 are the same as for a register operation, since at present we don't
7665 have named patterns for aligned memory structures. */
7666 struct extraction_insn insn;
7667 if (get_best_reg_extraction_insn (&insn, pattern,
7668 GET_MODE_BITSIZE (inner_mode), mode))
7670 wanted_inner_reg_mode = insn.struct_mode.require ();
7671 pos_mode = insn.pos_mode;
7672 extraction_mode = insn.field_mode;
7675 /* Never narrow an object, since that might not be safe. */
7677 if (mode != VOIDmode
7678 && partial_subreg_p (extraction_mode, mode))
7679 extraction_mode = mode;
7681 if (!MEM_P (inner))
7682 wanted_inner_mode = wanted_inner_reg_mode;
7683 else
7685 /* Be careful not to go beyond the extracted object and maintain the
7686 natural alignment of the memory. */
7687 wanted_inner_mode = smallest_int_mode_for_size (len);
7688 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7689 > GET_MODE_BITSIZE (wanted_inner_mode))
7690 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7693 orig_pos = pos;
7695 if (BITS_BIG_ENDIAN)
7697 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7698 BITS_BIG_ENDIAN style. If position is constant, compute new
7699 position. Otherwise, build subtraction.
7700 Note that POS is relative to the mode of the original argument.
7701 If it's a MEM we need to recompute POS relative to that.
7702 However, if we're extracting from (or inserting into) a register,
7703 we want to recompute POS relative to wanted_inner_mode. */
7704 int width = (MEM_P (inner)
7705 ? GET_MODE_BITSIZE (is_mode)
7706 : GET_MODE_BITSIZE (wanted_inner_mode));
7708 if (pos_rtx == 0)
7709 pos = width - len - pos;
7710 else
7711 pos_rtx
7712 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7713 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7714 pos_rtx);
7715 /* POS may be less than 0 now, but we check for that below.
7716 Note that it can only be less than 0 if !MEM_P (inner). */
7719 /* If INNER has a wider mode, and this is a constant extraction, try to
7720 make it smaller and adjust the byte to point to the byte containing
7721 the value. */
7722 if (wanted_inner_mode != VOIDmode
7723 && inner_mode != wanted_inner_mode
7724 && ! pos_rtx
7725 && partial_subreg_p (wanted_inner_mode, is_mode)
7726 && MEM_P (inner)
7727 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7728 && ! MEM_VOLATILE_P (inner))
7730 int offset = 0;
7732 /* The computations below will be correct if the machine is big
7733 endian in both bits and bytes or little endian in bits and bytes.
7734 If it is mixed, we must adjust. */
7736 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7737 adjust OFFSET to compensate. */
7738 if (BYTES_BIG_ENDIAN
7739 && paradoxical_subreg_p (is_mode, inner_mode))
7740 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7742 /* We can now move to the desired byte. */
7743 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7744 * GET_MODE_SIZE (wanted_inner_mode);
7745 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7747 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7748 && is_mode != wanted_inner_mode)
7749 offset = (GET_MODE_SIZE (is_mode)
7750 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7752 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7755 /* If INNER is not memory, get it into the proper mode. If we are changing
7756 its mode, POS must be a constant and smaller than the size of the new
7757 mode. */
7758 else if (!MEM_P (inner))
7760 /* On the LHS, don't create paradoxical subregs implicitely truncating
7761 the register unless TARGET_TRULY_NOOP_TRUNCATION. */
7762 if (in_dest
7763 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7764 wanted_inner_mode))
7765 return NULL_RTX;
7767 if (GET_MODE (inner) != wanted_inner_mode
7768 && (pos_rtx != 0
7769 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7770 return NULL_RTX;
7772 if (orig_pos < 0)
7773 return NULL_RTX;
7775 inner = force_to_mode (inner, wanted_inner_mode,
7776 pos_rtx
7777 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7778 ? HOST_WIDE_INT_M1U
7779 : (((HOST_WIDE_INT_1U << len) - 1)
7780 << orig_pos),
7784 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7785 have to zero extend. Otherwise, we can just use a SUBREG.
7787 We dealt with constant rtxes earlier, so pos_rtx cannot
7788 have VOIDmode at this point. */
7789 if (pos_rtx != 0
7790 && (GET_MODE_SIZE (pos_mode)
7791 > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7793 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7794 GET_MODE (pos_rtx));
7796 /* If we know that no extraneous bits are set, and that the high
7797 bit is not set, convert extraction to cheaper one - either
7798 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7799 cases. */
7800 if (flag_expensive_optimizations
7801 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7802 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7803 & ~(((unsigned HOST_WIDE_INT)
7804 GET_MODE_MASK (GET_MODE (pos_rtx)))
7805 >> 1))
7806 == 0)))
7808 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7809 GET_MODE (pos_rtx));
7811 /* Prefer ZERO_EXTENSION, since it gives more information to
7812 backends. */
7813 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7814 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7815 temp = temp1;
7817 pos_rtx = temp;
7820 /* Make POS_RTX unless we already have it and it is correct. If we don't
7821 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7822 be a CONST_INT. */
7823 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7824 pos_rtx = orig_pos_rtx;
7826 else if (pos_rtx == 0)
7827 pos_rtx = GEN_INT (pos);
7829 /* Make the required operation. See if we can use existing rtx. */
7830 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7831 extraction_mode, inner, GEN_INT (len), pos_rtx);
7832 if (! in_dest)
7833 new_rtx = gen_lowpart (mode, new_rtx);
7835 return new_rtx;
7838 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7839 can be commuted with any other operations in X. Return X without
7840 that shift if so. */
7842 static rtx
7843 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7845 enum rtx_code code = GET_CODE (x);
7846 rtx tem;
7848 switch (code)
7850 case ASHIFT:
7851 /* This is the shift itself. If it is wide enough, we will return
7852 either the value being shifted if the shift count is equal to
7853 COUNT or a shift for the difference. */
7854 if (CONST_INT_P (XEXP (x, 1))
7855 && INTVAL (XEXP (x, 1)) >= count)
7856 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7857 INTVAL (XEXP (x, 1)) - count);
7858 break;
7860 case NEG: case NOT:
7861 if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7862 return simplify_gen_unary (code, mode, tem, mode);
7864 break;
7866 case PLUS: case IOR: case XOR: case AND:
7867 /* If we can safely shift this constant and we find the inner shift,
7868 make a new operation. */
7869 if (CONST_INT_P (XEXP (x, 1))
7870 && (UINTVAL (XEXP (x, 1))
7871 & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
7872 && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7874 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7875 return simplify_gen_binary (code, mode, tem,
7876 gen_int_mode (val, mode));
7878 break;
7880 default:
7881 break;
7884 return 0;
7887 /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
7888 level of the expression and MODE is its mode. IN_CODE is as for
7889 make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
7890 that should be used when recursing on operands of *X_PTR.
7892 There are two possible actions:
7894 - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
7895 equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
7897 - Return a new rtx, which the caller returns directly. */
7899 static rtx
7900 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
7901 enum rtx_code in_code,
7902 enum rtx_code *next_code_ptr)
7904 rtx x = *x_ptr;
7905 enum rtx_code next_code = *next_code_ptr;
7906 enum rtx_code code = GET_CODE (x);
7907 int mode_width = GET_MODE_PRECISION (mode);
7908 rtx rhs, lhs;
7909 rtx new_rtx = 0;
7910 int i;
7911 rtx tem;
7912 scalar_int_mode inner_mode;
7913 bool equality_comparison = false;
7915 if (in_code == EQ)
7917 equality_comparison = true;
7918 in_code = COMPARE;
7921 /* Process depending on the code of this operation. If NEW is set
7922 nonzero, it will be returned. */
7924 switch (code)
7926 case ASHIFT:
7927 /* Convert shifts by constants into multiplications if inside
7928 an address. */
7929 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7930 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7931 && INTVAL (XEXP (x, 1)) >= 0)
7933 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7934 HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
7936 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7937 if (GET_CODE (new_rtx) == NEG)
7939 new_rtx = XEXP (new_rtx, 0);
7940 multval = -multval;
7942 multval = trunc_int_for_mode (multval, mode);
7943 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7945 break;
7947 case PLUS:
7948 lhs = XEXP (x, 0);
7949 rhs = XEXP (x, 1);
7950 lhs = make_compound_operation (lhs, next_code);
7951 rhs = make_compound_operation (rhs, next_code);
7952 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
7954 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7955 XEXP (lhs, 1));
7956 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7958 else if (GET_CODE (lhs) == MULT
7959 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7961 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7962 simplify_gen_unary (NEG, mode,
7963 XEXP (lhs, 1),
7964 mode));
7965 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7967 else
7969 SUBST (XEXP (x, 0), lhs);
7970 SUBST (XEXP (x, 1), rhs);
7972 maybe_swap_commutative_operands (x);
7973 return x;
7975 case MINUS:
7976 lhs = XEXP (x, 0);
7977 rhs = XEXP (x, 1);
7978 lhs = make_compound_operation (lhs, next_code);
7979 rhs = make_compound_operation (rhs, next_code);
7980 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
7982 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7983 XEXP (rhs, 1));
7984 return simplify_gen_binary (PLUS, mode, tem, lhs);
7986 else if (GET_CODE (rhs) == MULT
7987 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7989 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7990 simplify_gen_unary (NEG, mode,
7991 XEXP (rhs, 1),
7992 mode));
7993 return simplify_gen_binary (PLUS, mode, tem, lhs);
7995 else
7997 SUBST (XEXP (x, 0), lhs);
7998 SUBST (XEXP (x, 1), rhs);
7999 return x;
8002 case AND:
8003 /* If the second operand is not a constant, we can't do anything
8004 with it. */
8005 if (!CONST_INT_P (XEXP (x, 1)))
8006 break;
8008 /* If the constant is a power of two minus one and the first operand
8009 is a logical right shift, make an extraction. */
8010 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8011 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8013 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8014 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8015 i, 1, 0, in_code == COMPARE);
8018 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
8019 else if (GET_CODE (XEXP (x, 0)) == SUBREG
8020 && subreg_lowpart_p (XEXP (x, 0))
8021 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8022 &inner_mode)
8023 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8024 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8026 rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8027 new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8028 new_rtx = make_extraction (inner_mode, new_rtx, 0,
8029 XEXP (inner_x0, 1),
8030 i, 1, 0, in_code == COMPARE);
8032 /* If we narrowed the mode when dropping the subreg, then we lose. */
8033 if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8034 new_rtx = NULL;
8036 /* If that didn't give anything, see if the AND simplifies on
8037 its own. */
8038 if (!new_rtx && i >= 0)
8040 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8041 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
8042 0, in_code == COMPARE);
8045 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8046 else if ((GET_CODE (XEXP (x, 0)) == XOR
8047 || GET_CODE (XEXP (x, 0)) == IOR)
8048 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8049 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8050 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8052 /* Apply the distributive law, and then try to make extractions. */
8053 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8054 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8055 XEXP (x, 1)),
8056 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8057 XEXP (x, 1)));
8058 new_rtx = make_compound_operation (new_rtx, in_code);
8061 /* If we are have (and (rotate X C) M) and C is larger than the number
8062 of bits in M, this is an extraction. */
8064 else if (GET_CODE (XEXP (x, 0)) == ROTATE
8065 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8066 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8067 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8069 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8070 new_rtx = make_extraction (mode, new_rtx,
8071 (GET_MODE_PRECISION (mode)
8072 - INTVAL (XEXP (XEXP (x, 0), 1))),
8073 NULL_RTX, i, 1, 0, in_code == COMPARE);
8076 /* On machines without logical shifts, if the operand of the AND is
8077 a logical shift and our mask turns off all the propagated sign
8078 bits, we can replace the logical shift with an arithmetic shift. */
8079 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8080 && !have_insn_for (LSHIFTRT, mode)
8081 && have_insn_for (ASHIFTRT, mode)
8082 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8083 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8084 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8085 && mode_width <= HOST_BITS_PER_WIDE_INT)
8087 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8089 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8090 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8091 SUBST (XEXP (x, 0),
8092 gen_rtx_ASHIFTRT (mode,
8093 make_compound_operation (XEXP (XEXP (x,
8096 next_code),
8097 XEXP (XEXP (x, 0), 1)));
8100 /* If the constant is one less than a power of two, this might be
8101 representable by an extraction even if no shift is present.
8102 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8103 we are in a COMPARE. */
8104 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8105 new_rtx = make_extraction (mode,
8106 make_compound_operation (XEXP (x, 0),
8107 next_code),
8108 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
8110 /* If we are in a comparison and this is an AND with a power of two,
8111 convert this into the appropriate bit extract. */
8112 else if (in_code == COMPARE
8113 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8114 && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8115 new_rtx = make_extraction (mode,
8116 make_compound_operation (XEXP (x, 0),
8117 next_code),
8118 i, NULL_RTX, 1, 1, 0, 1);
8120 /* If the one operand is a paradoxical subreg of a register or memory and
8121 the constant (limited to the smaller mode) has only zero bits where
8122 the sub expression has known zero bits, this can be expressed as
8123 a zero_extend. */
8124 else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8126 rtx sub;
8128 sub = XEXP (XEXP (x, 0), 0);
8129 machine_mode sub_mode = GET_MODE (sub);
8130 if ((REG_P (sub) || MEM_P (sub))
8131 && GET_MODE_PRECISION (sub_mode) < mode_width)
8133 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8134 unsigned HOST_WIDE_INT mask;
8136 /* original AND constant with all the known zero bits set */
8137 mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8138 if ((mask & mode_mask) == mode_mask)
8140 new_rtx = make_compound_operation (sub, next_code);
8141 new_rtx = make_extraction (mode, new_rtx, 0, 0,
8142 GET_MODE_PRECISION (sub_mode),
8143 1, 0, in_code == COMPARE);
8148 break;
8150 case LSHIFTRT:
8151 /* If the sign bit is known to be zero, replace this with an
8152 arithmetic shift. */
8153 if (have_insn_for (ASHIFTRT, mode)
8154 && ! have_insn_for (LSHIFTRT, mode)
8155 && mode_width <= HOST_BITS_PER_WIDE_INT
8156 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8158 new_rtx = gen_rtx_ASHIFTRT (mode,
8159 make_compound_operation (XEXP (x, 0),
8160 next_code),
8161 XEXP (x, 1));
8162 break;
8165 /* fall through */
8167 case ASHIFTRT:
8168 lhs = XEXP (x, 0);
8169 rhs = XEXP (x, 1);
8171 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8172 this is a SIGN_EXTRACT. */
8173 if (CONST_INT_P (rhs)
8174 && GET_CODE (lhs) == ASHIFT
8175 && CONST_INT_P (XEXP (lhs, 1))
8176 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8177 && INTVAL (XEXP (lhs, 1)) >= 0
8178 && INTVAL (rhs) < mode_width)
8180 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8181 new_rtx = make_extraction (mode, new_rtx,
8182 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8183 NULL_RTX, mode_width - INTVAL (rhs),
8184 code == LSHIFTRT, 0, in_code == COMPARE);
8185 break;
8188 /* See if we have operations between an ASHIFTRT and an ASHIFT.
8189 If so, try to merge the shifts into a SIGN_EXTEND. We could
8190 also do this for some cases of SIGN_EXTRACT, but it doesn't
8191 seem worth the effort; the case checked for occurs on Alpha. */
8193 if (!OBJECT_P (lhs)
8194 && ! (GET_CODE (lhs) == SUBREG
8195 && (OBJECT_P (SUBREG_REG (lhs))))
8196 && CONST_INT_P (rhs)
8197 && INTVAL (rhs) >= 0
8198 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8199 && INTVAL (rhs) < mode_width
8200 && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8201 new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8202 next_code),
8203 0, NULL_RTX, mode_width - INTVAL (rhs),
8204 code == LSHIFTRT, 0, in_code == COMPARE);
8206 break;
8208 case SUBREG:
8209 /* Call ourselves recursively on the inner expression. If we are
8210 narrowing the object and it has a different RTL code from
8211 what it originally did, do this SUBREG as a force_to_mode. */
8213 rtx inner = SUBREG_REG (x), simplified;
8214 enum rtx_code subreg_code = in_code;
8216 /* If the SUBREG is masking of a logical right shift,
8217 make an extraction. */
8218 if (GET_CODE (inner) == LSHIFTRT
8219 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8220 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8221 && CONST_INT_P (XEXP (inner, 1))
8222 && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8223 && subreg_lowpart_p (x))
8225 new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8226 int width = GET_MODE_PRECISION (inner_mode)
8227 - INTVAL (XEXP (inner, 1));
8228 if (width > mode_width)
8229 width = mode_width;
8230 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8231 width, 1, 0, in_code == COMPARE);
8232 break;
8235 /* If in_code is COMPARE, it isn't always safe to pass it through
8236 to the recursive make_compound_operation call. */
8237 if (subreg_code == COMPARE
8238 && (!subreg_lowpart_p (x)
8239 || GET_CODE (inner) == SUBREG
8240 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8241 is (const_int 0), rather than
8242 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8243 Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8244 for non-equality comparisons against 0 is not equivalent
8245 to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8246 || (GET_CODE (inner) == AND
8247 && CONST_INT_P (XEXP (inner, 1))
8248 && partial_subreg_p (x)
8249 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8250 >= GET_MODE_BITSIZE (mode) - 1)))
8251 subreg_code = SET;
8253 tem = make_compound_operation (inner, subreg_code);
8255 simplified
8256 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8257 if (simplified)
8258 tem = simplified;
8260 if (GET_CODE (tem) != GET_CODE (inner)
8261 && partial_subreg_p (x)
8262 && subreg_lowpart_p (x))
8264 rtx newer
8265 = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, 0);
8267 /* If we have something other than a SUBREG, we might have
8268 done an expansion, so rerun ourselves. */
8269 if (GET_CODE (newer) != SUBREG)
8270 newer = make_compound_operation (newer, in_code);
8272 /* force_to_mode can expand compounds. If it just re-expanded
8273 the compound, use gen_lowpart to convert to the desired
8274 mode. */
8275 if (rtx_equal_p (newer, x)
8276 /* Likewise if it re-expanded the compound only partially.
8277 This happens for SUBREG of ZERO_EXTRACT if they extract
8278 the same number of bits. */
8279 || (GET_CODE (newer) == SUBREG
8280 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8281 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8282 && GET_CODE (inner) == AND
8283 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8284 return gen_lowpart (GET_MODE (x), tem);
8286 return newer;
8289 if (simplified)
8290 return tem;
8292 break;
8294 default:
8295 break;
8298 if (new_rtx)
8299 *x_ptr = gen_lowpart (mode, new_rtx);
8300 *next_code_ptr = next_code;
8301 return NULL_RTX;
8304 /* Look at the expression rooted at X. Look for expressions
8305 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8306 Form these expressions.
8308 Return the new rtx, usually just X.
8310 Also, for machines like the VAX that don't have logical shift insns,
8311 try to convert logical to arithmetic shift operations in cases where
8312 they are equivalent. This undoes the canonicalizations to logical
8313 shifts done elsewhere.
8315 We try, as much as possible, to re-use rtl expressions to save memory.
8317 IN_CODE says what kind of expression we are processing. Normally, it is
8318 SET. In a memory address it is MEM. When processing the arguments of
8319 a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8320 precisely it is an equality comparison against zero. */
8323 make_compound_operation (rtx x, enum rtx_code in_code)
8325 enum rtx_code code = GET_CODE (x);
8326 const char *fmt;
8327 int i, j;
8328 enum rtx_code next_code;
8329 rtx new_rtx, tem;
8331 /* Select the code to be used in recursive calls. Once we are inside an
8332 address, we stay there. If we have a comparison, set to COMPARE,
8333 but once inside, go back to our default of SET. */
8335 next_code = (code == MEM ? MEM
8336 : ((code == COMPARE || COMPARISON_P (x))
8337 && XEXP (x, 1) == const0_rtx) ? COMPARE
8338 : in_code == COMPARE || in_code == EQ ? SET : in_code);
8340 scalar_int_mode mode;
8341 if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8343 rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8344 &next_code);
8345 if (new_rtx)
8346 return new_rtx;
8347 code = GET_CODE (x);
8350 /* Now recursively process each operand of this operation. We need to
8351 handle ZERO_EXTEND specially so that we don't lose track of the
8352 inner mode. */
8353 if (code == ZERO_EXTEND)
8355 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8356 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8357 new_rtx, GET_MODE (XEXP (x, 0)));
8358 if (tem)
8359 return tem;
8360 SUBST (XEXP (x, 0), new_rtx);
8361 return x;
8364 fmt = GET_RTX_FORMAT (code);
8365 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8366 if (fmt[i] == 'e')
8368 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8369 SUBST (XEXP (x, i), new_rtx);
8371 else if (fmt[i] == 'E')
8372 for (j = 0; j < XVECLEN (x, i); j++)
8374 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8375 SUBST (XVECEXP (x, i, j), new_rtx);
8378 maybe_swap_commutative_operands (x);
8379 return x;
8382 /* Given M see if it is a value that would select a field of bits
8383 within an item, but not the entire word. Return -1 if not.
8384 Otherwise, return the starting position of the field, where 0 is the
8385 low-order bit.
8387 *PLEN is set to the length of the field. */
8389 static int
8390 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8392 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8393 int pos = m ? ctz_hwi (m) : -1;
8394 int len = 0;
8396 if (pos >= 0)
8397 /* Now shift off the low-order zero bits and see if we have a
8398 power of two minus 1. */
8399 len = exact_log2 ((m >> pos) + 1);
8401 if (len <= 0)
8402 pos = -1;
8404 *plen = len;
8405 return pos;
8408 /* If X refers to a register that equals REG in value, replace these
8409 references with REG. */
8410 static rtx
8411 canon_reg_for_combine (rtx x, rtx reg)
8413 rtx op0, op1, op2;
8414 const char *fmt;
8415 int i;
8416 bool copied;
8418 enum rtx_code code = GET_CODE (x);
8419 switch (GET_RTX_CLASS (code))
8421 case RTX_UNARY:
8422 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8423 if (op0 != XEXP (x, 0))
8424 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8425 GET_MODE (reg));
8426 break;
8428 case RTX_BIN_ARITH:
8429 case RTX_COMM_ARITH:
8430 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8431 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8432 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8433 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8434 break;
8436 case RTX_COMPARE:
8437 case RTX_COMM_COMPARE:
8438 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8439 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8440 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8441 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8442 GET_MODE (op0), op0, op1);
8443 break;
8445 case RTX_TERNARY:
8446 case RTX_BITFIELD_OPS:
8447 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8448 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8449 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8450 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8451 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8452 GET_MODE (op0), op0, op1, op2);
8453 /* FALLTHRU */
8455 case RTX_OBJ:
8456 if (REG_P (x))
8458 if (rtx_equal_p (get_last_value (reg), x)
8459 || rtx_equal_p (reg, get_last_value (x)))
8460 return reg;
8461 else
8462 break;
8465 /* fall through */
8467 default:
8468 fmt = GET_RTX_FORMAT (code);
8469 copied = false;
8470 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8471 if (fmt[i] == 'e')
8473 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8474 if (op != XEXP (x, i))
8476 if (!copied)
8478 copied = true;
8479 x = copy_rtx (x);
8481 XEXP (x, i) = op;
8484 else if (fmt[i] == 'E')
8486 int j;
8487 for (j = 0; j < XVECLEN (x, i); j++)
8489 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8490 if (op != XVECEXP (x, i, j))
8492 if (!copied)
8494 copied = true;
8495 x = copy_rtx (x);
8497 XVECEXP (x, i, j) = op;
8502 break;
8505 return x;
8508 /* Return X converted to MODE. If the value is already truncated to
8509 MODE we can just return a subreg even though in the general case we
8510 would need an explicit truncation. */
8512 static rtx
8513 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8515 if (!CONST_INT_P (x)
8516 && partial_subreg_p (mode, GET_MODE (x))
8517 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8518 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8520 /* Bit-cast X into an integer mode. */
8521 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8522 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8523 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8524 x, GET_MODE (x));
8527 return gen_lowpart (mode, x);
8530 /* See if X can be simplified knowing that we will only refer to it in
8531 MODE and will only refer to those bits that are nonzero in MASK.
8532 If other bits are being computed or if masking operations are done
8533 that select a superset of the bits in MASK, they can sometimes be
8534 ignored.
8536 Return a possibly simplified expression, but always convert X to
8537 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8539 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8540 are all off in X. This is used when X will be complemented, by either
8541 NOT, NEG, or XOR. */
8543 static rtx
8544 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8545 int just_select)
8547 enum rtx_code code = GET_CODE (x);
8548 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8549 machine_mode op_mode;
8550 unsigned HOST_WIDE_INT nonzero;
8552 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8553 code below will do the wrong thing since the mode of such an
8554 expression is VOIDmode.
8556 Also do nothing if X is a CLOBBER; this can happen if X was
8557 the return value from a call to gen_lowpart. */
8558 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8559 return x;
8561 /* We want to perform the operation in its present mode unless we know
8562 that the operation is valid in MODE, in which case we do the operation
8563 in MODE. */
8564 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8565 && have_insn_for (code, mode))
8566 ? mode : GET_MODE (x));
8568 /* It is not valid to do a right-shift in a narrower mode
8569 than the one it came in with. */
8570 if ((code == LSHIFTRT || code == ASHIFTRT)
8571 && partial_subreg_p (mode, GET_MODE (x)))
8572 op_mode = GET_MODE (x);
8574 /* Truncate MASK to fit OP_MODE. */
8575 if (op_mode)
8576 mask &= GET_MODE_MASK (op_mode);
8578 /* Determine what bits of X are guaranteed to be (non)zero. */
8579 nonzero = nonzero_bits (x, mode);
8581 /* If none of the bits in X are needed, return a zero. */
8582 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8583 x = const0_rtx;
8585 /* If X is a CONST_INT, return a new one. Do this here since the
8586 test below will fail. */
8587 if (CONST_INT_P (x))
8589 if (SCALAR_INT_MODE_P (mode))
8590 return gen_int_mode (INTVAL (x) & mask, mode);
8591 else
8593 x = GEN_INT (INTVAL (x) & mask);
8594 return gen_lowpart_common (mode, x);
8598 /* If X is narrower than MODE and we want all the bits in X's mode, just
8599 get X in the proper mode. */
8600 if (paradoxical_subreg_p (mode, GET_MODE (x))
8601 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8602 return gen_lowpart (mode, x);
8604 /* We can ignore the effect of a SUBREG if it narrows the mode or
8605 if the constant masks to zero all the bits the mode doesn't have. */
8606 if (GET_CODE (x) == SUBREG
8607 && subreg_lowpart_p (x)
8608 && (partial_subreg_p (x)
8609 || (0 == (mask
8610 & GET_MODE_MASK (GET_MODE (x))
8611 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8612 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8614 scalar_int_mode int_mode, xmode;
8615 if (is_a <scalar_int_mode> (mode, &int_mode)
8616 && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8617 /* OP_MODE is either MODE or XMODE, so it must be a scalar
8618 integer too. */
8619 return force_int_to_mode (x, int_mode, xmode,
8620 as_a <scalar_int_mode> (op_mode),
8621 mask, just_select);
8623 return gen_lowpart_or_truncate (mode, x);
8626 /* Subroutine of force_to_mode that handles cases in which both X and
8627 the result are scalar integers. MODE is the mode of the result,
8628 XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8629 is preferred for simplified versions of X. The other arguments
8630 are as for force_to_mode. */
8632 static rtx
8633 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8634 scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8635 int just_select)
8637 enum rtx_code code = GET_CODE (x);
8638 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8639 unsigned HOST_WIDE_INT fuller_mask;
8640 rtx op0, op1, temp;
8642 /* When we have an arithmetic operation, or a shift whose count we
8643 do not know, we need to assume that all bits up to the highest-order
8644 bit in MASK will be needed. This is how we form such a mask. */
8645 if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8646 fuller_mask = HOST_WIDE_INT_M1U;
8647 else
8648 fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1))
8649 - 1);
8651 switch (code)
8653 case CLOBBER:
8654 /* If X is a (clobber (const_int)), return it since we know we are
8655 generating something that won't match. */
8656 return x;
8658 case SIGN_EXTEND:
8659 case ZERO_EXTEND:
8660 case ZERO_EXTRACT:
8661 case SIGN_EXTRACT:
8662 x = expand_compound_operation (x);
8663 if (GET_CODE (x) != code)
8664 return force_to_mode (x, mode, mask, next_select);
8665 break;
8667 case TRUNCATE:
8668 /* Similarly for a truncate. */
8669 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8671 case AND:
8672 /* If this is an AND with a constant, convert it into an AND
8673 whose constant is the AND of that constant with MASK. If it
8674 remains an AND of MASK, delete it since it is redundant. */
8676 if (CONST_INT_P (XEXP (x, 1)))
8678 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8679 mask & INTVAL (XEXP (x, 1)));
8680 xmode = op_mode;
8682 /* If X is still an AND, see if it is an AND with a mask that
8683 is just some low-order bits. If so, and it is MASK, we don't
8684 need it. */
8686 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8687 && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8688 x = XEXP (x, 0);
8690 /* If it remains an AND, try making another AND with the bits
8691 in the mode mask that aren't in MASK turned on. If the
8692 constant in the AND is wide enough, this might make a
8693 cheaper constant. */
8695 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8696 && GET_MODE_MASK (xmode) != mask
8697 && HWI_COMPUTABLE_MODE_P (xmode))
8699 unsigned HOST_WIDE_INT cval
8700 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8701 rtx y;
8703 y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8704 gen_int_mode (cval, xmode));
8705 if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8706 < set_src_cost (x, xmode, optimize_this_for_speed_p))
8707 x = y;
8710 break;
8713 goto binop;
8715 case PLUS:
8716 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8717 low-order bits (as in an alignment operation) and FOO is already
8718 aligned to that boundary, mask C1 to that boundary as well.
8719 This may eliminate that PLUS and, later, the AND. */
8722 unsigned int width = GET_MODE_PRECISION (mode);
8723 unsigned HOST_WIDE_INT smask = mask;
8725 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8726 number, sign extend it. */
8728 if (width < HOST_BITS_PER_WIDE_INT
8729 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8730 smask |= HOST_WIDE_INT_M1U << width;
8732 if (CONST_INT_P (XEXP (x, 1))
8733 && pow2p_hwi (- smask)
8734 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8735 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8736 return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8737 (INTVAL (XEXP (x, 1)) & smask)),
8738 mode, smask, next_select);
8741 /* fall through */
8743 case MULT:
8744 /* Substituting into the operands of a widening MULT is not likely to
8745 create RTL matching a machine insn. */
8746 if (code == MULT
8747 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8748 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8749 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8750 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8751 && REG_P (XEXP (XEXP (x, 0), 0))
8752 && REG_P (XEXP (XEXP (x, 1), 0)))
8753 return gen_lowpart_or_truncate (mode, x);
8755 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8756 most significant bit in MASK since carries from those bits will
8757 affect the bits we are interested in. */
8758 mask = fuller_mask;
8759 goto binop;
8761 case MINUS:
8762 /* If X is (minus C Y) where C's least set bit is larger than any bit
8763 in the mask, then we may replace with (neg Y). */
8764 if (CONST_INT_P (XEXP (x, 0))
8765 && least_bit_hwi (UINTVAL (XEXP (x, 0))) > mask)
8767 x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8768 return force_to_mode (x, mode, mask, next_select);
8771 /* Similarly, if C contains every bit in the fuller_mask, then we may
8772 replace with (not Y). */
8773 if (CONST_INT_P (XEXP (x, 0))
8774 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8776 x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8777 return force_to_mode (x, mode, mask, next_select);
8780 mask = fuller_mask;
8781 goto binop;
8783 case IOR:
8784 case XOR:
8785 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8786 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8787 operation which may be a bitfield extraction. Ensure that the
8788 constant we form is not wider than the mode of X. */
8790 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8791 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8792 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8793 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8794 && CONST_INT_P (XEXP (x, 1))
8795 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8796 + floor_log2 (INTVAL (XEXP (x, 1))))
8797 < GET_MODE_PRECISION (xmode))
8798 && (UINTVAL (XEXP (x, 1))
8799 & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8801 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8802 << INTVAL (XEXP (XEXP (x, 0), 1)),
8803 xmode);
8804 temp = simplify_gen_binary (GET_CODE (x), xmode,
8805 XEXP (XEXP (x, 0), 0), temp);
8806 x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8807 XEXP (XEXP (x, 0), 1));
8808 return force_to_mode (x, mode, mask, next_select);
8811 binop:
8812 /* For most binary operations, just propagate into the operation and
8813 change the mode if we have an operation of that mode. */
8815 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8816 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8818 /* If we ended up truncating both operands, truncate the result of the
8819 operation instead. */
8820 if (GET_CODE (op0) == TRUNCATE
8821 && GET_CODE (op1) == TRUNCATE)
8823 op0 = XEXP (op0, 0);
8824 op1 = XEXP (op1, 0);
8827 op0 = gen_lowpart_or_truncate (op_mode, op0);
8828 op1 = gen_lowpart_or_truncate (op_mode, op1);
8830 if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8832 x = simplify_gen_binary (code, op_mode, op0, op1);
8833 xmode = op_mode;
8835 break;
8837 case ASHIFT:
8838 /* For left shifts, do the same, but just for the first operand.
8839 However, we cannot do anything with shifts where we cannot
8840 guarantee that the counts are smaller than the size of the mode
8841 because such a count will have a different meaning in a
8842 wider mode. */
8844 if (! (CONST_INT_P (XEXP (x, 1))
8845 && INTVAL (XEXP (x, 1)) >= 0
8846 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8847 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8848 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8849 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8850 break;
8852 /* If the shift count is a constant and we can do arithmetic in
8853 the mode of the shift, refine which bits we need. Otherwise, use the
8854 conservative form of the mask. */
8855 if (CONST_INT_P (XEXP (x, 1))
8856 && INTVAL (XEXP (x, 1)) >= 0
8857 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8858 && HWI_COMPUTABLE_MODE_P (op_mode))
8859 mask >>= INTVAL (XEXP (x, 1));
8860 else
8861 mask = fuller_mask;
8863 op0 = gen_lowpart_or_truncate (op_mode,
8864 force_to_mode (XEXP (x, 0), op_mode,
8865 mask, next_select));
8867 if (op_mode != xmode || op0 != XEXP (x, 0))
8869 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8870 xmode = op_mode;
8872 break;
8874 case LSHIFTRT:
8875 /* Here we can only do something if the shift count is a constant,
8876 this shift constant is valid for the host, and we can do arithmetic
8877 in OP_MODE. */
8879 if (CONST_INT_P (XEXP (x, 1))
8880 && INTVAL (XEXP (x, 1)) >= 0
8881 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8882 && HWI_COMPUTABLE_MODE_P (op_mode))
8884 rtx inner = XEXP (x, 0);
8885 unsigned HOST_WIDE_INT inner_mask;
8887 /* Select the mask of the bits we need for the shift operand. */
8888 inner_mask = mask << INTVAL (XEXP (x, 1));
8890 /* We can only change the mode of the shift if we can do arithmetic
8891 in the mode of the shift and INNER_MASK is no wider than the
8892 width of X's mode. */
8893 if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
8894 op_mode = xmode;
8896 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8898 if (xmode != op_mode || inner != XEXP (x, 0))
8900 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8901 xmode = op_mode;
8905 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8906 shift and AND produces only copies of the sign bit (C2 is one less
8907 than a power of two), we can do this with just a shift. */
8909 if (GET_CODE (x) == LSHIFTRT
8910 && CONST_INT_P (XEXP (x, 1))
8911 /* The shift puts one of the sign bit copies in the least significant
8912 bit. */
8913 && ((INTVAL (XEXP (x, 1))
8914 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8915 >= GET_MODE_PRECISION (xmode))
8916 && pow2p_hwi (mask + 1)
8917 /* Number of bits left after the shift must be more than the mask
8918 needs. */
8919 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8920 <= GET_MODE_PRECISION (xmode))
8921 /* Must be more sign bit copies than the mask needs. */
8922 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8923 >= exact_log2 (mask + 1)))
8924 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
8925 GEN_INT (GET_MODE_PRECISION (xmode)
8926 - exact_log2 (mask + 1)));
8928 goto shiftrt;
8930 case ASHIFTRT:
8931 /* If we are just looking for the sign bit, we don't need this shift at
8932 all, even if it has a variable count. */
8933 if (val_signbit_p (xmode, mask))
8934 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8936 /* If this is a shift by a constant, get a mask that contains those bits
8937 that are not copies of the sign bit. We then have two cases: If
8938 MASK only includes those bits, this can be a logical shift, which may
8939 allow simplifications. If MASK is a single-bit field not within
8940 those bits, we are requesting a copy of the sign bit and hence can
8941 shift the sign bit to the appropriate location. */
8943 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8944 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8946 unsigned HOST_WIDE_INT nonzero;
8947 int i;
8949 /* If the considered data is wider than HOST_WIDE_INT, we can't
8950 represent a mask for all its bits in a single scalar.
8951 But we only care about the lower bits, so calculate these. */
8953 if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
8955 nonzero = HOST_WIDE_INT_M1U;
8957 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8958 is the number of bits a full-width mask would have set.
8959 We need only shift if these are fewer than nonzero can
8960 hold. If not, we must keep all bits set in nonzero. */
8962 if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
8963 < HOST_BITS_PER_WIDE_INT)
8964 nonzero >>= INTVAL (XEXP (x, 1))
8965 + HOST_BITS_PER_WIDE_INT
8966 - GET_MODE_PRECISION (xmode);
8968 else
8970 nonzero = GET_MODE_MASK (xmode);
8971 nonzero >>= INTVAL (XEXP (x, 1));
8974 if ((mask & ~nonzero) == 0)
8976 x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
8977 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8978 if (GET_CODE (x) != ASHIFTRT)
8979 return force_to_mode (x, mode, mask, next_select);
8982 else if ((i = exact_log2 (mask)) >= 0)
8984 x = simplify_shift_const
8985 (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
8986 GET_MODE_PRECISION (xmode) - 1 - i);
8988 if (GET_CODE (x) != ASHIFTRT)
8989 return force_to_mode (x, mode, mask, next_select);
8993 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8994 even if the shift count isn't a constant. */
8995 if (mask == 1)
8996 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
8998 shiftrt:
9000 /* If this is a zero- or sign-extension operation that just affects bits
9001 we don't care about, remove it. Be sure the call above returned
9002 something that is still a shift. */
9004 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9005 && CONST_INT_P (XEXP (x, 1))
9006 && INTVAL (XEXP (x, 1)) >= 0
9007 && (INTVAL (XEXP (x, 1))
9008 <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9009 && GET_CODE (XEXP (x, 0)) == ASHIFT
9010 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9011 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
9012 next_select);
9014 break;
9016 case ROTATE:
9017 case ROTATERT:
9018 /* If the shift count is constant and we can do computations
9019 in the mode of X, compute where the bits we care about are.
9020 Otherwise, we can't do anything. Don't change the mode of
9021 the shift or propagate MODE into the shift, though. */
9022 if (CONST_INT_P (XEXP (x, 1))
9023 && INTVAL (XEXP (x, 1)) >= 0)
9025 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9026 xmode, gen_int_mode (mask, xmode),
9027 XEXP (x, 1));
9028 if (temp && CONST_INT_P (temp))
9029 x = simplify_gen_binary (code, xmode,
9030 force_to_mode (XEXP (x, 0), xmode,
9031 INTVAL (temp), next_select),
9032 XEXP (x, 1));
9034 break;
9036 case NEG:
9037 /* If we just want the low-order bit, the NEG isn't needed since it
9038 won't change the low-order bit. */
9039 if (mask == 1)
9040 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9042 /* We need any bits less significant than the most significant bit in
9043 MASK since carries from those bits will affect the bits we are
9044 interested in. */
9045 mask = fuller_mask;
9046 goto unop;
9048 case NOT:
9049 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9050 same as the XOR case above. Ensure that the constant we form is not
9051 wider than the mode of X. */
9053 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9054 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9055 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9056 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9057 < GET_MODE_PRECISION (xmode))
9058 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9060 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9061 temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9062 x = simplify_gen_binary (LSHIFTRT, xmode,
9063 temp, XEXP (XEXP (x, 0), 1));
9065 return force_to_mode (x, mode, mask, next_select);
9068 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9069 use the full mask inside the NOT. */
9070 mask = fuller_mask;
9072 unop:
9073 op0 = gen_lowpart_or_truncate (op_mode,
9074 force_to_mode (XEXP (x, 0), mode, mask,
9075 next_select));
9076 if (op_mode != xmode || op0 != XEXP (x, 0))
9078 x = simplify_gen_unary (code, op_mode, op0, op_mode);
9079 xmode = op_mode;
9081 break;
9083 case NE:
9084 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9085 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9086 which is equal to STORE_FLAG_VALUE. */
9087 if ((mask & ~STORE_FLAG_VALUE) == 0
9088 && XEXP (x, 1) == const0_rtx
9089 && GET_MODE (XEXP (x, 0)) == mode
9090 && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9091 && (nonzero_bits (XEXP (x, 0), mode)
9092 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9093 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9095 break;
9097 case IF_THEN_ELSE:
9098 /* We have no way of knowing if the IF_THEN_ELSE can itself be
9099 written in a narrower mode. We play it safe and do not do so. */
9101 op0 = gen_lowpart_or_truncate (xmode,
9102 force_to_mode (XEXP (x, 1), mode,
9103 mask, next_select));
9104 op1 = gen_lowpart_or_truncate (xmode,
9105 force_to_mode (XEXP (x, 2), mode,
9106 mask, next_select));
9107 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9108 x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9109 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9110 op0, op1);
9111 break;
9113 default:
9114 break;
9117 /* Ensure we return a value of the proper mode. */
9118 return gen_lowpart_or_truncate (mode, x);
9121 /* Return nonzero if X is an expression that has one of two values depending on
9122 whether some other value is zero or nonzero. In that case, we return the
9123 value that is being tested, *PTRUE is set to the value if the rtx being
9124 returned has a nonzero value, and *PFALSE is set to the other alternative.
9126 If we return zero, we set *PTRUE and *PFALSE to X. */
9128 static rtx
9129 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9131 machine_mode mode = GET_MODE (x);
9132 enum rtx_code code = GET_CODE (x);
9133 rtx cond0, cond1, true0, true1, false0, false1;
9134 unsigned HOST_WIDE_INT nz;
9135 scalar_int_mode int_mode;
9137 /* If we are comparing a value against zero, we are done. */
9138 if ((code == NE || code == EQ)
9139 && XEXP (x, 1) == const0_rtx)
9141 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9142 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9143 return XEXP (x, 0);
9146 /* If this is a unary operation whose operand has one of two values, apply
9147 our opcode to compute those values. */
9148 else if (UNARY_P (x)
9149 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9151 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9152 *pfalse = simplify_gen_unary (code, mode, false0,
9153 GET_MODE (XEXP (x, 0)));
9154 return cond0;
9157 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9158 make can't possibly match and would suppress other optimizations. */
9159 else if (code == COMPARE)
9162 /* If this is a binary operation, see if either side has only one of two
9163 values. If either one does or if both do and they are conditional on
9164 the same value, compute the new true and false values. */
9165 else if (BINARY_P (x))
9167 rtx op0 = XEXP (x, 0);
9168 rtx op1 = XEXP (x, 1);
9169 cond0 = if_then_else_cond (op0, &true0, &false0);
9170 cond1 = if_then_else_cond (op1, &true1, &false1);
9172 if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9173 && (REG_P (op0) || REG_P (op1)))
9175 /* Try to enable a simplification by undoing work done by
9176 if_then_else_cond if it converted a REG into something more
9177 complex. */
9178 if (REG_P (op0))
9180 cond0 = 0;
9181 true0 = false0 = op0;
9183 else
9185 cond1 = 0;
9186 true1 = false1 = op1;
9190 if ((cond0 != 0 || cond1 != 0)
9191 && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9193 /* If if_then_else_cond returned zero, then true/false are the
9194 same rtl. We must copy one of them to prevent invalid rtl
9195 sharing. */
9196 if (cond0 == 0)
9197 true0 = copy_rtx (true0);
9198 else if (cond1 == 0)
9199 true1 = copy_rtx (true1);
9201 if (COMPARISON_P (x))
9203 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9204 true0, true1);
9205 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9206 false0, false1);
9208 else
9210 *ptrue = simplify_gen_binary (code, mode, true0, true1);
9211 *pfalse = simplify_gen_binary (code, mode, false0, false1);
9214 return cond0 ? cond0 : cond1;
9217 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9218 operands is zero when the other is nonzero, and vice-versa,
9219 and STORE_FLAG_VALUE is 1 or -1. */
9221 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9222 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9223 || code == UMAX)
9224 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9226 rtx op0 = XEXP (XEXP (x, 0), 1);
9227 rtx op1 = XEXP (XEXP (x, 1), 1);
9229 cond0 = XEXP (XEXP (x, 0), 0);
9230 cond1 = XEXP (XEXP (x, 1), 0);
9232 if (COMPARISON_P (cond0)
9233 && COMPARISON_P (cond1)
9234 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9235 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9236 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9237 || ((swap_condition (GET_CODE (cond0))
9238 == reversed_comparison_code (cond1, NULL))
9239 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9240 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9241 && ! side_effects_p (x))
9243 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9244 *pfalse = simplify_gen_binary (MULT, mode,
9245 (code == MINUS
9246 ? simplify_gen_unary (NEG, mode,
9247 op1, mode)
9248 : op1),
9249 const_true_rtx);
9250 return cond0;
9254 /* Similarly for MULT, AND and UMIN, except that for these the result
9255 is always zero. */
9256 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9257 && (code == MULT || code == AND || code == UMIN)
9258 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9260 cond0 = XEXP (XEXP (x, 0), 0);
9261 cond1 = XEXP (XEXP (x, 1), 0);
9263 if (COMPARISON_P (cond0)
9264 && COMPARISON_P (cond1)
9265 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9266 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9267 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9268 || ((swap_condition (GET_CODE (cond0))
9269 == reversed_comparison_code (cond1, NULL))
9270 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9271 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9272 && ! side_effects_p (x))
9274 *ptrue = *pfalse = const0_rtx;
9275 return cond0;
9280 else if (code == IF_THEN_ELSE)
9282 /* If we have IF_THEN_ELSE already, extract the condition and
9283 canonicalize it if it is NE or EQ. */
9284 cond0 = XEXP (x, 0);
9285 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9286 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9287 return XEXP (cond0, 0);
9288 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9290 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9291 return XEXP (cond0, 0);
9293 else
9294 return cond0;
9297 /* If X is a SUBREG, we can narrow both the true and false values
9298 if the inner expression, if there is a condition. */
9299 else if (code == SUBREG
9300 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
9301 &true0, &false0)))
9303 true0 = simplify_gen_subreg (mode, true0,
9304 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9305 false0 = simplify_gen_subreg (mode, false0,
9306 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9307 if (true0 && false0)
9309 *ptrue = true0;
9310 *pfalse = false0;
9311 return cond0;
9315 /* If X is a constant, this isn't special and will cause confusions
9316 if we treat it as such. Likewise if it is equivalent to a constant. */
9317 else if (CONSTANT_P (x)
9318 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9321 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9322 will be least confusing to the rest of the compiler. */
9323 else if (mode == BImode)
9325 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9326 return x;
9329 /* If X is known to be either 0 or -1, those are the true and
9330 false values when testing X. */
9331 else if (x == constm1_rtx || x == const0_rtx
9332 || (is_a <scalar_int_mode> (mode, &int_mode)
9333 && (num_sign_bit_copies (x, int_mode)
9334 == GET_MODE_PRECISION (int_mode))))
9336 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9337 return x;
9340 /* Likewise for 0 or a single bit. */
9341 else if (HWI_COMPUTABLE_MODE_P (mode)
9342 && pow2p_hwi (nz = nonzero_bits (x, mode)))
9344 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9345 return x;
9348 /* Otherwise fail; show no condition with true and false values the same. */
9349 *ptrue = *pfalse = x;
9350 return 0;
9353 /* Return the value of expression X given the fact that condition COND
9354 is known to be true when applied to REG as its first operand and VAL
9355 as its second. X is known to not be shared and so can be modified in
9356 place.
9358 We only handle the simplest cases, and specifically those cases that
9359 arise with IF_THEN_ELSE expressions. */
9361 static rtx
9362 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9364 enum rtx_code code = GET_CODE (x);
9365 const char *fmt;
9366 int i, j;
9368 if (side_effects_p (x))
9369 return x;
9371 /* If either operand of the condition is a floating point value,
9372 then we have to avoid collapsing an EQ comparison. */
9373 if (cond == EQ
9374 && rtx_equal_p (x, reg)
9375 && ! FLOAT_MODE_P (GET_MODE (x))
9376 && ! FLOAT_MODE_P (GET_MODE (val)))
9377 return val;
9379 if (cond == UNEQ && rtx_equal_p (x, reg))
9380 return val;
9382 /* If X is (abs REG) and we know something about REG's relationship
9383 with zero, we may be able to simplify this. */
9385 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9386 switch (cond)
9388 case GE: case GT: case EQ:
9389 return XEXP (x, 0);
9390 case LT: case LE:
9391 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9392 XEXP (x, 0),
9393 GET_MODE (XEXP (x, 0)));
9394 default:
9395 break;
9398 /* The only other cases we handle are MIN, MAX, and comparisons if the
9399 operands are the same as REG and VAL. */
9401 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9403 if (rtx_equal_p (XEXP (x, 0), val))
9405 std::swap (val, reg);
9406 cond = swap_condition (cond);
9409 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9411 if (COMPARISON_P (x))
9413 if (comparison_dominates_p (cond, code))
9414 return const_true_rtx;
9416 code = reversed_comparison_code (x, NULL);
9417 if (code != UNKNOWN
9418 && comparison_dominates_p (cond, code))
9419 return const0_rtx;
9420 else
9421 return x;
9423 else if (code == SMAX || code == SMIN
9424 || code == UMIN || code == UMAX)
9426 int unsignedp = (code == UMIN || code == UMAX);
9428 /* Do not reverse the condition when it is NE or EQ.
9429 This is because we cannot conclude anything about
9430 the value of 'SMAX (x, y)' when x is not equal to y,
9431 but we can when x equals y. */
9432 if ((code == SMAX || code == UMAX)
9433 && ! (cond == EQ || cond == NE))
9434 cond = reverse_condition (cond);
9436 switch (cond)
9438 case GE: case GT:
9439 return unsignedp ? x : XEXP (x, 1);
9440 case LE: case LT:
9441 return unsignedp ? x : XEXP (x, 0);
9442 case GEU: case GTU:
9443 return unsignedp ? XEXP (x, 1) : x;
9444 case LEU: case LTU:
9445 return unsignedp ? XEXP (x, 0) : x;
9446 default:
9447 break;
9452 else if (code == SUBREG)
9454 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9455 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9457 if (SUBREG_REG (x) != r)
9459 /* We must simplify subreg here, before we lose track of the
9460 original inner_mode. */
9461 new_rtx = simplify_subreg (GET_MODE (x), r,
9462 inner_mode, SUBREG_BYTE (x));
9463 if (new_rtx)
9464 return new_rtx;
9465 else
9466 SUBST (SUBREG_REG (x), r);
9469 return x;
9471 /* We don't have to handle SIGN_EXTEND here, because even in the
9472 case of replacing something with a modeless CONST_INT, a
9473 CONST_INT is already (supposed to be) a valid sign extension for
9474 its narrower mode, which implies it's already properly
9475 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9476 story is different. */
9477 else if (code == ZERO_EXTEND)
9479 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9480 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9482 if (XEXP (x, 0) != r)
9484 /* We must simplify the zero_extend here, before we lose
9485 track of the original inner_mode. */
9486 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9487 r, inner_mode);
9488 if (new_rtx)
9489 return new_rtx;
9490 else
9491 SUBST (XEXP (x, 0), r);
9494 return x;
9497 fmt = GET_RTX_FORMAT (code);
9498 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9500 if (fmt[i] == 'e')
9501 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9502 else if (fmt[i] == 'E')
9503 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9504 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9505 cond, reg, val));
9508 return x;
9511 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9512 assignment as a field assignment. */
9514 static int
9515 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9517 if (widen_x && GET_MODE (x) != GET_MODE (y))
9519 if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9520 return 0;
9521 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9522 return 0;
9523 x = adjust_address_nv (x, GET_MODE (y),
9524 byte_lowpart_offset (GET_MODE (y),
9525 GET_MODE (x)));
9528 if (x == y || rtx_equal_p (x, y))
9529 return 1;
9531 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9532 return 0;
9534 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9535 Note that all SUBREGs of MEM are paradoxical; otherwise they
9536 would have been rewritten. */
9537 if (MEM_P (x) && GET_CODE (y) == SUBREG
9538 && MEM_P (SUBREG_REG (y))
9539 && rtx_equal_p (SUBREG_REG (y),
9540 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9541 return 1;
9543 if (MEM_P (y) && GET_CODE (x) == SUBREG
9544 && MEM_P (SUBREG_REG (x))
9545 && rtx_equal_p (SUBREG_REG (x),
9546 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9547 return 1;
9549 /* We used to see if get_last_value of X and Y were the same but that's
9550 not correct. In one direction, we'll cause the assignment to have
9551 the wrong destination and in the case, we'll import a register into this
9552 insn that might have already have been dead. So fail if none of the
9553 above cases are true. */
9554 return 0;
9557 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9558 Return that assignment if so.
9560 We only handle the most common cases. */
9562 static rtx
9563 make_field_assignment (rtx x)
9565 rtx dest = SET_DEST (x);
9566 rtx src = SET_SRC (x);
9567 rtx assign;
9568 rtx rhs, lhs;
9569 HOST_WIDE_INT c1;
9570 HOST_WIDE_INT pos;
9571 unsigned HOST_WIDE_INT len;
9572 rtx other;
9574 /* All the rules in this function are specific to scalar integers. */
9575 scalar_int_mode mode;
9576 if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9577 return x;
9579 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9580 a clear of a one-bit field. We will have changed it to
9581 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9582 for a SUBREG. */
9584 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9585 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9586 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9587 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9589 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9590 1, 1, 1, 0);
9591 if (assign != 0)
9592 return gen_rtx_SET (assign, const0_rtx);
9593 return x;
9596 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9597 && subreg_lowpart_p (XEXP (src, 0))
9598 && partial_subreg_p (XEXP (src, 0))
9599 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9600 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9601 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9602 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9604 assign = make_extraction (VOIDmode, dest, 0,
9605 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9606 1, 1, 1, 0);
9607 if (assign != 0)
9608 return gen_rtx_SET (assign, const0_rtx);
9609 return x;
9612 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9613 one-bit field. */
9614 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9615 && XEXP (XEXP (src, 0), 0) == const1_rtx
9616 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9618 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9619 1, 1, 1, 0);
9620 if (assign != 0)
9621 return gen_rtx_SET (assign, const1_rtx);
9622 return x;
9625 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9626 SRC is an AND with all bits of that field set, then we can discard
9627 the AND. */
9628 if (GET_CODE (dest) == ZERO_EXTRACT
9629 && CONST_INT_P (XEXP (dest, 1))
9630 && GET_CODE (src) == AND
9631 && CONST_INT_P (XEXP (src, 1)))
9633 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9634 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9635 unsigned HOST_WIDE_INT ze_mask;
9637 if (width >= HOST_BITS_PER_WIDE_INT)
9638 ze_mask = -1;
9639 else
9640 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9642 /* Complete overlap. We can remove the source AND. */
9643 if ((and_mask & ze_mask) == ze_mask)
9644 return gen_rtx_SET (dest, XEXP (src, 0));
9646 /* Partial overlap. We can reduce the source AND. */
9647 if ((and_mask & ze_mask) != and_mask)
9649 src = gen_rtx_AND (mode, XEXP (src, 0),
9650 gen_int_mode (and_mask & ze_mask, mode));
9651 return gen_rtx_SET (dest, src);
9655 /* The other case we handle is assignments into a constant-position
9656 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9657 a mask that has all one bits except for a group of zero bits and
9658 OTHER is known to have zeros where C1 has ones, this is such an
9659 assignment. Compute the position and length from C1. Shift OTHER
9660 to the appropriate position, force it to the required mode, and
9661 make the extraction. Check for the AND in both operands. */
9663 /* One or more SUBREGs might obscure the constant-position field
9664 assignment. The first one we are likely to encounter is an outer
9665 narrowing SUBREG, which we can just strip for the purposes of
9666 identifying the constant-field assignment. */
9667 scalar_int_mode src_mode = mode;
9668 if (GET_CODE (src) == SUBREG
9669 && subreg_lowpart_p (src)
9670 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9671 src = SUBREG_REG (src);
9673 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9674 return x;
9676 rhs = expand_compound_operation (XEXP (src, 0));
9677 lhs = expand_compound_operation (XEXP (src, 1));
9679 if (GET_CODE (rhs) == AND
9680 && CONST_INT_P (XEXP (rhs, 1))
9681 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9682 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9683 /* The second SUBREG that might get in the way is a paradoxical
9684 SUBREG around the first operand of the AND. We want to
9685 pretend the operand is as wide as the destination here. We
9686 do this by adjusting the MEM to wider mode for the sole
9687 purpose of the call to rtx_equal_for_field_assignment_p. Also
9688 note this trick only works for MEMs. */
9689 else if (GET_CODE (rhs) == AND
9690 && paradoxical_subreg_p (XEXP (rhs, 0))
9691 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9692 && CONST_INT_P (XEXP (rhs, 1))
9693 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9694 dest, true))
9695 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9696 else if (GET_CODE (lhs) == AND
9697 && CONST_INT_P (XEXP (lhs, 1))
9698 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9699 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9700 /* The second SUBREG that might get in the way is a paradoxical
9701 SUBREG around the first operand of the AND. We want to
9702 pretend the operand is as wide as the destination here. We
9703 do this by adjusting the MEM to wider mode for the sole
9704 purpose of the call to rtx_equal_for_field_assignment_p. Also
9705 note this trick only works for MEMs. */
9706 else if (GET_CODE (lhs) == AND
9707 && paradoxical_subreg_p (XEXP (lhs, 0))
9708 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9709 && CONST_INT_P (XEXP (lhs, 1))
9710 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9711 dest, true))
9712 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9713 else
9714 return x;
9716 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9717 if (pos < 0
9718 || pos + len > GET_MODE_PRECISION (mode)
9719 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9720 || (c1 & nonzero_bits (other, mode)) != 0)
9721 return x;
9723 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9724 if (assign == 0)
9725 return x;
9727 /* The mode to use for the source is the mode of the assignment, or of
9728 what is inside a possible STRICT_LOW_PART. */
9729 machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9730 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9732 /* Shift OTHER right POS places and make it the source, restricting it
9733 to the proper length and mode. */
9735 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9736 src_mode, other, pos),
9737 dest);
9738 src = force_to_mode (src, new_mode,
9739 len >= HOST_BITS_PER_WIDE_INT
9740 ? HOST_WIDE_INT_M1U
9741 : (HOST_WIDE_INT_1U << len) - 1,
9744 /* If SRC is masked by an AND that does not make a difference in
9745 the value being stored, strip it. */
9746 if (GET_CODE (assign) == ZERO_EXTRACT
9747 && CONST_INT_P (XEXP (assign, 1))
9748 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9749 && GET_CODE (src) == AND
9750 && CONST_INT_P (XEXP (src, 1))
9751 && UINTVAL (XEXP (src, 1))
9752 == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9753 src = XEXP (src, 0);
9755 return gen_rtx_SET (assign, src);
9758 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9759 if so. */
9761 static rtx
9762 apply_distributive_law (rtx x)
9764 enum rtx_code code = GET_CODE (x);
9765 enum rtx_code inner_code;
9766 rtx lhs, rhs, other;
9767 rtx tem;
9769 /* Distributivity is not true for floating point as it can change the
9770 value. So we don't do it unless -funsafe-math-optimizations. */
9771 if (FLOAT_MODE_P (GET_MODE (x))
9772 && ! flag_unsafe_math_optimizations)
9773 return x;
9775 /* The outer operation can only be one of the following: */
9776 if (code != IOR && code != AND && code != XOR
9777 && code != PLUS && code != MINUS)
9778 return x;
9780 lhs = XEXP (x, 0);
9781 rhs = XEXP (x, 1);
9783 /* If either operand is a primitive we can't do anything, so get out
9784 fast. */
9785 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9786 return x;
9788 lhs = expand_compound_operation (lhs);
9789 rhs = expand_compound_operation (rhs);
9790 inner_code = GET_CODE (lhs);
9791 if (inner_code != GET_CODE (rhs))
9792 return x;
9794 /* See if the inner and outer operations distribute. */
9795 switch (inner_code)
9797 case LSHIFTRT:
9798 case ASHIFTRT:
9799 case AND:
9800 case IOR:
9801 /* These all distribute except over PLUS. */
9802 if (code == PLUS || code == MINUS)
9803 return x;
9804 break;
9806 case MULT:
9807 if (code != PLUS && code != MINUS)
9808 return x;
9809 break;
9811 case ASHIFT:
9812 /* This is also a multiply, so it distributes over everything. */
9813 break;
9815 /* This used to handle SUBREG, but this turned out to be counter-
9816 productive, since (subreg (op ...)) usually is not handled by
9817 insn patterns, and this "optimization" therefore transformed
9818 recognizable patterns into unrecognizable ones. Therefore the
9819 SUBREG case was removed from here.
9821 It is possible that distributing SUBREG over arithmetic operations
9822 leads to an intermediate result than can then be optimized further,
9823 e.g. by moving the outer SUBREG to the other side of a SET as done
9824 in simplify_set. This seems to have been the original intent of
9825 handling SUBREGs here.
9827 However, with current GCC this does not appear to actually happen,
9828 at least on major platforms. If some case is found where removing
9829 the SUBREG case here prevents follow-on optimizations, distributing
9830 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9832 default:
9833 return x;
9836 /* Set LHS and RHS to the inner operands (A and B in the example
9837 above) and set OTHER to the common operand (C in the example).
9838 There is only one way to do this unless the inner operation is
9839 commutative. */
9840 if (COMMUTATIVE_ARITH_P (lhs)
9841 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9842 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9843 else if (COMMUTATIVE_ARITH_P (lhs)
9844 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9845 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9846 else if (COMMUTATIVE_ARITH_P (lhs)
9847 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9848 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9849 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9850 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9851 else
9852 return x;
9854 /* Form the new inner operation, seeing if it simplifies first. */
9855 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9857 /* There is one exception to the general way of distributing:
9858 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9859 if (code == XOR && inner_code == IOR)
9861 inner_code = AND;
9862 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9865 /* We may be able to continuing distributing the result, so call
9866 ourselves recursively on the inner operation before forming the
9867 outer operation, which we return. */
9868 return simplify_gen_binary (inner_code, GET_MODE (x),
9869 apply_distributive_law (tem), other);
9872 /* See if X is of the form (* (+ A B) C), and if so convert to
9873 (+ (* A C) (* B C)) and try to simplify.
9875 Most of the time, this results in no change. However, if some of
9876 the operands are the same or inverses of each other, simplifications
9877 will result.
9879 For example, (and (ior A B) (not B)) can occur as the result of
9880 expanding a bit field assignment. When we apply the distributive
9881 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9882 which then simplifies to (and (A (not B))).
9884 Note that no checks happen on the validity of applying the inverse
9885 distributive law. This is pointless since we can do it in the
9886 few places where this routine is called.
9888 N is the index of the term that is decomposed (the arithmetic operation,
9889 i.e. (+ A B) in the first example above). !N is the index of the term that
9890 is distributed, i.e. of C in the first example above. */
9891 static rtx
9892 distribute_and_simplify_rtx (rtx x, int n)
9894 machine_mode mode;
9895 enum rtx_code outer_code, inner_code;
9896 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9898 /* Distributivity is not true for floating point as it can change the
9899 value. So we don't do it unless -funsafe-math-optimizations. */
9900 if (FLOAT_MODE_P (GET_MODE (x))
9901 && ! flag_unsafe_math_optimizations)
9902 return NULL_RTX;
9904 decomposed = XEXP (x, n);
9905 if (!ARITHMETIC_P (decomposed))
9906 return NULL_RTX;
9908 mode = GET_MODE (x);
9909 outer_code = GET_CODE (x);
9910 distributed = XEXP (x, !n);
9912 inner_code = GET_CODE (decomposed);
9913 inner_op0 = XEXP (decomposed, 0);
9914 inner_op1 = XEXP (decomposed, 1);
9916 /* Special case (and (xor B C) (not A)), which is equivalent to
9917 (xor (ior A B) (ior A C)) */
9918 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9920 distributed = XEXP (distributed, 0);
9921 outer_code = IOR;
9924 if (n == 0)
9926 /* Distribute the second term. */
9927 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9928 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9930 else
9932 /* Distribute the first term. */
9933 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9934 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9937 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9938 new_op0, new_op1));
9939 if (GET_CODE (tmp) != outer_code
9940 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
9941 < set_src_cost (x, mode, optimize_this_for_speed_p)))
9942 return tmp;
9944 return NULL_RTX;
9947 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9948 in MODE. Return an equivalent form, if different from (and VAROP
9949 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9951 static rtx
9952 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
9953 unsigned HOST_WIDE_INT constop)
9955 unsigned HOST_WIDE_INT nonzero;
9956 unsigned HOST_WIDE_INT orig_constop;
9957 rtx orig_varop;
9958 int i;
9960 orig_varop = varop;
9961 orig_constop = constop;
9962 if (GET_CODE (varop) == CLOBBER)
9963 return NULL_RTX;
9965 /* Simplify VAROP knowing that we will be only looking at some of the
9966 bits in it.
9968 Note by passing in CONSTOP, we guarantee that the bits not set in
9969 CONSTOP are not significant and will never be examined. We must
9970 ensure that is the case by explicitly masking out those bits
9971 before returning. */
9972 varop = force_to_mode (varop, mode, constop, 0);
9974 /* If VAROP is a CLOBBER, we will fail so return it. */
9975 if (GET_CODE (varop) == CLOBBER)
9976 return varop;
9978 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9979 to VAROP and return the new constant. */
9980 if (CONST_INT_P (varop))
9981 return gen_int_mode (INTVAL (varop) & constop, mode);
9983 /* See what bits may be nonzero in VAROP. Unlike the general case of
9984 a call to nonzero_bits, here we don't care about bits outside
9985 MODE. */
9987 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9989 /* Turn off all bits in the constant that are known to already be zero.
9990 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9991 which is tested below. */
9993 constop &= nonzero;
9995 /* If we don't have any bits left, return zero. */
9996 if (constop == 0)
9997 return const0_rtx;
9999 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
10000 a power of two, we can replace this with an ASHIFT. */
10001 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
10002 && (i = exact_log2 (constop)) >= 0)
10003 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
10005 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
10006 or XOR, then try to apply the distributive law. This may eliminate
10007 operations if either branch can be simplified because of the AND.
10008 It may also make some cases more complex, but those cases probably
10009 won't match a pattern either with or without this. */
10011 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10013 scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10014 return
10015 gen_lowpart
10016 (mode,
10017 apply_distributive_law
10018 (simplify_gen_binary (GET_CODE (varop), varop_mode,
10019 simplify_and_const_int (NULL_RTX, varop_mode,
10020 XEXP (varop, 0),
10021 constop),
10022 simplify_and_const_int (NULL_RTX, varop_mode,
10023 XEXP (varop, 1),
10024 constop))));
10027 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10028 the AND and see if one of the operands simplifies to zero. If so, we
10029 may eliminate it. */
10031 if (GET_CODE (varop) == PLUS
10032 && pow2p_hwi (constop + 1))
10034 rtx o0, o1;
10036 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10037 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10038 if (o0 == const0_rtx)
10039 return o1;
10040 if (o1 == const0_rtx)
10041 return o0;
10044 /* Make a SUBREG if necessary. If we can't make it, fail. */
10045 varop = gen_lowpart (mode, varop);
10046 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10047 return NULL_RTX;
10049 /* If we are only masking insignificant bits, return VAROP. */
10050 if (constop == nonzero)
10051 return varop;
10053 if (varop == orig_varop && constop == orig_constop)
10054 return NULL_RTX;
10056 /* Otherwise, return an AND. */
10057 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10061 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10062 in MODE.
10064 Return an equivalent form, if different from X. Otherwise, return X. If
10065 X is zero, we are to always construct the equivalent form. */
10067 static rtx
10068 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10069 unsigned HOST_WIDE_INT constop)
10071 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10072 if (tem)
10073 return tem;
10075 if (!x)
10076 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10077 gen_int_mode (constop, mode));
10078 if (GET_MODE (x) != mode)
10079 x = gen_lowpart (mode, x);
10080 return x;
10083 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10084 We don't care about bits outside of those defined in MODE.
10086 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10087 a shift, AND, or zero_extract, we can do better. */
10089 static rtx
10090 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10091 scalar_int_mode mode,
10092 unsigned HOST_WIDE_INT *nonzero)
10094 rtx tem;
10095 reg_stat_type *rsp;
10097 /* If X is a register whose nonzero bits value is current, use it.
10098 Otherwise, if X is a register whose value we can find, use that
10099 value. Otherwise, use the previously-computed global nonzero bits
10100 for this register. */
10102 rsp = &reg_stat[REGNO (x)];
10103 if (rsp->last_set_value != 0
10104 && (rsp->last_set_mode == mode
10105 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10106 && GET_MODE_CLASS (mode) == MODE_INT))
10107 && ((rsp->last_set_label >= label_tick_ebb_start
10108 && rsp->last_set_label < label_tick)
10109 || (rsp->last_set_label == label_tick
10110 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10111 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10112 && REGNO (x) < reg_n_sets_max
10113 && REG_N_SETS (REGNO (x)) == 1
10114 && !REGNO_REG_SET_P
10115 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10116 REGNO (x)))))
10118 /* Note that, even if the precision of last_set_mode is lower than that
10119 of mode, record_value_for_reg invoked nonzero_bits on the register
10120 with nonzero_bits_mode (because last_set_mode is necessarily integral
10121 and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10122 are all valid, hence in mode too since nonzero_bits_mode is defined
10123 to the largest HWI_COMPUTABLE_MODE_P mode. */
10124 *nonzero &= rsp->last_set_nonzero_bits;
10125 return NULL;
10128 tem = get_last_value (x);
10129 if (tem)
10131 if (SHORT_IMMEDIATES_SIGN_EXTEND)
10132 tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10134 return tem;
10137 if (nonzero_sign_valid && rsp->nonzero_bits)
10139 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10141 if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10142 /* We don't know anything about the upper bits. */
10143 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10145 *nonzero &= mask;
10148 return NULL;
10151 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10152 end of X that are known to be equal to the sign bit. X will be used
10153 in mode MODE; the returned value will always be between 1 and the
10154 number of bits in MODE. */
10156 static rtx
10157 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10158 scalar_int_mode mode,
10159 unsigned int *result)
10161 rtx tem;
10162 reg_stat_type *rsp;
10164 rsp = &reg_stat[REGNO (x)];
10165 if (rsp->last_set_value != 0
10166 && rsp->last_set_mode == mode
10167 && ((rsp->last_set_label >= label_tick_ebb_start
10168 && rsp->last_set_label < label_tick)
10169 || (rsp->last_set_label == label_tick
10170 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10171 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10172 && REGNO (x) < reg_n_sets_max
10173 && REG_N_SETS (REGNO (x)) == 1
10174 && !REGNO_REG_SET_P
10175 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10176 REGNO (x)))))
10178 *result = rsp->last_set_sign_bit_copies;
10179 return NULL;
10182 tem = get_last_value (x);
10183 if (tem != 0)
10184 return tem;
10186 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10187 && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10188 *result = rsp->sign_bit_copies;
10190 return NULL;
10193 /* Return the number of "extended" bits there are in X, when interpreted
10194 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
10195 unsigned quantities, this is the number of high-order zero bits.
10196 For signed quantities, this is the number of copies of the sign bit
10197 minus 1. In both case, this function returns the number of "spare"
10198 bits. For example, if two quantities for which this function returns
10199 at least 1 are added, the addition is known not to overflow.
10201 This function will always return 0 unless called during combine, which
10202 implies that it must be called from a define_split. */
10204 unsigned int
10205 extended_count (const_rtx x, machine_mode mode, int unsignedp)
10207 if (nonzero_sign_valid == 0)
10208 return 0;
10210 scalar_int_mode int_mode;
10211 return (unsignedp
10212 ? (is_a <scalar_int_mode> (mode, &int_mode)
10213 && HWI_COMPUTABLE_MODE_P (int_mode)
10214 ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10215 - floor_log2 (nonzero_bits (x, int_mode)))
10216 : 0)
10217 : num_sign_bit_copies (x, mode) - 1);
10220 /* This function is called from `simplify_shift_const' to merge two
10221 outer operations. Specifically, we have already found that we need
10222 to perform operation *POP0 with constant *PCONST0 at the outermost
10223 position. We would now like to also perform OP1 with constant CONST1
10224 (with *POP0 being done last).
10226 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
10227 the resulting operation. *PCOMP_P is set to 1 if we would need to
10228 complement the innermost operand, otherwise it is unchanged.
10230 MODE is the mode in which the operation will be done. No bits outside
10231 the width of this mode matter. It is assumed that the width of this mode
10232 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10234 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
10235 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
10236 result is simply *PCONST0.
10238 If the resulting operation cannot be expressed as one operation, we
10239 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
10241 static int
10242 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)
10244 enum rtx_code op0 = *pop0;
10245 HOST_WIDE_INT const0 = *pconst0;
10247 const0 &= GET_MODE_MASK (mode);
10248 const1 &= GET_MODE_MASK (mode);
10250 /* If OP0 is an AND, clear unimportant bits in CONST1. */
10251 if (op0 == AND)
10252 const1 &= const0;
10254 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
10255 if OP0 is SET. */
10257 if (op1 == UNKNOWN || op0 == SET)
10258 return 1;
10260 else if (op0 == UNKNOWN)
10261 op0 = op1, const0 = const1;
10263 else if (op0 == op1)
10265 switch (op0)
10267 case AND:
10268 const0 &= const1;
10269 break;
10270 case IOR:
10271 const0 |= const1;
10272 break;
10273 case XOR:
10274 const0 ^= const1;
10275 break;
10276 case PLUS:
10277 const0 += const1;
10278 break;
10279 case NEG:
10280 op0 = UNKNOWN;
10281 break;
10282 default:
10283 break;
10287 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
10288 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10289 return 0;
10291 /* If the two constants aren't the same, we can't do anything. The
10292 remaining six cases can all be done. */
10293 else if (const0 != const1)
10294 return 0;
10296 else
10297 switch (op0)
10299 case IOR:
10300 if (op1 == AND)
10301 /* (a & b) | b == b */
10302 op0 = SET;
10303 else /* op1 == XOR */
10304 /* (a ^ b) | b == a | b */
10306 break;
10308 case XOR:
10309 if (op1 == AND)
10310 /* (a & b) ^ b == (~a) & b */
10311 op0 = AND, *pcomp_p = 1;
10312 else /* op1 == IOR */
10313 /* (a | b) ^ b == a & ~b */
10314 op0 = AND, const0 = ~const0;
10315 break;
10317 case AND:
10318 if (op1 == IOR)
10319 /* (a | b) & b == b */
10320 op0 = SET;
10321 else /* op1 == XOR */
10322 /* (a ^ b) & b) == (~a) & b */
10323 *pcomp_p = 1;
10324 break;
10325 default:
10326 break;
10329 /* Check for NO-OP cases. */
10330 const0 &= GET_MODE_MASK (mode);
10331 if (const0 == 0
10332 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10333 op0 = UNKNOWN;
10334 else if (const0 == 0 && op0 == AND)
10335 op0 = SET;
10336 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10337 && op0 == AND)
10338 op0 = UNKNOWN;
10340 *pop0 = op0;
10342 /* ??? Slightly redundant with the above mask, but not entirely.
10343 Moving this above means we'd have to sign-extend the mode mask
10344 for the final test. */
10345 if (op0 != UNKNOWN && op0 != NEG)
10346 *pconst0 = trunc_int_for_mode (const0, mode);
10348 return 1;
10351 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10352 the shift in. The original shift operation CODE is performed on OP in
10353 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10354 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10355 result of the shift is subject to operation OUTER_CODE with operand
10356 OUTER_CONST. */
10358 static scalar_int_mode
10359 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10360 scalar_int_mode orig_mode, scalar_int_mode mode,
10361 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10363 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10365 /* In general we can't perform in wider mode for right shift and rotate. */
10366 switch (code)
10368 case ASHIFTRT:
10369 /* We can still widen if the bits brought in from the left are identical
10370 to the sign bit of ORIG_MODE. */
10371 if (num_sign_bit_copies (op, mode)
10372 > (unsigned) (GET_MODE_PRECISION (mode)
10373 - GET_MODE_PRECISION (orig_mode)))
10374 return mode;
10375 return orig_mode;
10377 case LSHIFTRT:
10378 /* Similarly here but with zero bits. */
10379 if (HWI_COMPUTABLE_MODE_P (mode)
10380 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10381 return mode;
10383 /* We can also widen if the bits brought in will be masked off. This
10384 operation is performed in ORIG_MODE. */
10385 if (outer_code == AND)
10387 int care_bits = low_bitmask_len (orig_mode, outer_const);
10389 if (care_bits >= 0
10390 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10391 return mode;
10393 /* fall through */
10395 case ROTATE:
10396 return orig_mode;
10398 case ROTATERT:
10399 gcc_unreachable ();
10401 default:
10402 return mode;
10406 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10407 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10408 if we cannot simplify it. Otherwise, return a simplified value.
10410 The shift is normally computed in the widest mode we find in VAROP, as
10411 long as it isn't a different number of words than RESULT_MODE. Exceptions
10412 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10414 static rtx
10415 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10416 rtx varop, int orig_count)
10418 enum rtx_code orig_code = code;
10419 rtx orig_varop = varop;
10420 int count;
10421 machine_mode mode = result_mode;
10422 machine_mode shift_mode;
10423 scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10424 unsigned int mode_words
10425 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10426 /* We form (outer_op (code varop count) (outer_const)). */
10427 enum rtx_code outer_op = UNKNOWN;
10428 HOST_WIDE_INT outer_const = 0;
10429 int complement_p = 0;
10430 rtx new_rtx, x;
10432 /* Make sure and truncate the "natural" shift on the way in. We don't
10433 want to do this inside the loop as it makes it more difficult to
10434 combine shifts. */
10435 if (SHIFT_COUNT_TRUNCATED)
10436 orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10438 /* If we were given an invalid count, don't do anything except exactly
10439 what was requested. */
10441 if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10442 return NULL_RTX;
10444 count = orig_count;
10446 /* Unless one of the branches of the `if' in this loop does a `continue',
10447 we will `break' the loop after the `if'. */
10449 while (count != 0)
10451 /* If we have an operand of (clobber (const_int 0)), fail. */
10452 if (GET_CODE (varop) == CLOBBER)
10453 return NULL_RTX;
10455 /* Convert ROTATERT to ROTATE. */
10456 if (code == ROTATERT)
10458 unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10459 code = ROTATE;
10460 count = bitsize - count;
10463 shift_mode = result_mode;
10464 if (shift_mode != mode)
10466 /* We only change the modes of scalar shifts. */
10467 int_mode = as_a <scalar_int_mode> (mode);
10468 int_result_mode = as_a <scalar_int_mode> (result_mode);
10469 shift_mode = try_widen_shift_mode (code, varop, count,
10470 int_result_mode, int_mode,
10471 outer_op, outer_const);
10474 scalar_int_mode shift_unit_mode
10475 = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10477 /* Handle cases where the count is greater than the size of the mode
10478 minus 1. For ASHIFT, use the size minus one as the count (this can
10479 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10480 take the count modulo the size. For other shifts, the result is
10481 zero.
10483 Since these shifts are being produced by the compiler by combining
10484 multiple operations, each of which are defined, we know what the
10485 result is supposed to be. */
10487 if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10489 if (code == ASHIFTRT)
10490 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10491 else if (code == ROTATE || code == ROTATERT)
10492 count %= GET_MODE_PRECISION (shift_unit_mode);
10493 else
10495 /* We can't simply return zero because there may be an
10496 outer op. */
10497 varop = const0_rtx;
10498 count = 0;
10499 break;
10503 /* If we discovered we had to complement VAROP, leave. Making a NOT
10504 here would cause an infinite loop. */
10505 if (complement_p)
10506 break;
10508 if (shift_mode == shift_unit_mode)
10510 /* An arithmetic right shift of a quantity known to be -1 or 0
10511 is a no-op. */
10512 if (code == ASHIFTRT
10513 && (num_sign_bit_copies (varop, shift_unit_mode)
10514 == GET_MODE_PRECISION (shift_unit_mode)))
10516 count = 0;
10517 break;
10520 /* If we are doing an arithmetic right shift and discarding all but
10521 the sign bit copies, this is equivalent to doing a shift by the
10522 bitsize minus one. Convert it into that shift because it will
10523 often allow other simplifications. */
10525 if (code == ASHIFTRT
10526 && (count + num_sign_bit_copies (varop, shift_unit_mode)
10527 >= GET_MODE_PRECISION (shift_unit_mode)))
10528 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10530 /* We simplify the tests below and elsewhere by converting
10531 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10532 `make_compound_operation' will convert it to an ASHIFTRT for
10533 those machines (such as VAX) that don't have an LSHIFTRT. */
10534 if (code == ASHIFTRT
10535 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10536 && val_signbit_known_clear_p (shift_unit_mode,
10537 nonzero_bits (varop,
10538 shift_unit_mode)))
10539 code = LSHIFTRT;
10541 if (((code == LSHIFTRT
10542 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10543 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10544 || (code == ASHIFT
10545 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10546 && !((nonzero_bits (varop, shift_unit_mode) << count)
10547 & GET_MODE_MASK (shift_unit_mode))))
10548 && !side_effects_p (varop))
10549 varop = const0_rtx;
10552 switch (GET_CODE (varop))
10554 case SIGN_EXTEND:
10555 case ZERO_EXTEND:
10556 case SIGN_EXTRACT:
10557 case ZERO_EXTRACT:
10558 new_rtx = expand_compound_operation (varop);
10559 if (new_rtx != varop)
10561 varop = new_rtx;
10562 continue;
10564 break;
10566 case MEM:
10567 /* The following rules apply only to scalars. */
10568 if (shift_mode != shift_unit_mode)
10569 break;
10570 int_mode = as_a <scalar_int_mode> (mode);
10572 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10573 minus the width of a smaller mode, we can do this with a
10574 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10575 if ((code == ASHIFTRT || code == LSHIFTRT)
10576 && ! mode_dependent_address_p (XEXP (varop, 0),
10577 MEM_ADDR_SPACE (varop))
10578 && ! MEM_VOLATILE_P (varop)
10579 && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10580 .exists (&tmode)))
10582 new_rtx = adjust_address_nv (varop, tmode,
10583 BYTES_BIG_ENDIAN ? 0
10584 : count / BITS_PER_UNIT);
10586 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10587 : ZERO_EXTEND, int_mode, new_rtx);
10588 count = 0;
10589 continue;
10591 break;
10593 case SUBREG:
10594 /* The following rules apply only to scalars. */
10595 if (shift_mode != shift_unit_mode)
10596 break;
10597 int_mode = as_a <scalar_int_mode> (mode);
10598 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10600 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10601 the same number of words as what we've seen so far. Then store
10602 the widest mode in MODE. */
10603 if (subreg_lowpart_p (varop)
10604 && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10605 && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10606 && (unsigned int) ((GET_MODE_SIZE (inner_mode)
10607 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10608 == mode_words
10609 && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10611 varop = SUBREG_REG (varop);
10612 if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10613 mode = inner_mode;
10614 continue;
10616 break;
10618 case MULT:
10619 /* Some machines use MULT instead of ASHIFT because MULT
10620 is cheaper. But it is still better on those machines to
10621 merge two shifts into one. */
10622 if (CONST_INT_P (XEXP (varop, 1))
10623 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10625 varop
10626 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10627 XEXP (varop, 0),
10628 GEN_INT (exact_log2 (
10629 UINTVAL (XEXP (varop, 1)))));
10630 continue;
10632 break;
10634 case UDIV:
10635 /* Similar, for when divides are cheaper. */
10636 if (CONST_INT_P (XEXP (varop, 1))
10637 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10639 varop
10640 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10641 XEXP (varop, 0),
10642 GEN_INT (exact_log2 (
10643 UINTVAL (XEXP (varop, 1)))));
10644 continue;
10646 break;
10648 case ASHIFTRT:
10649 /* If we are extracting just the sign bit of an arithmetic
10650 right shift, that shift is not needed. However, the sign
10651 bit of a wider mode may be different from what would be
10652 interpreted as the sign bit in a narrower mode, so, if
10653 the result is narrower, don't discard the shift. */
10654 if (code == LSHIFTRT
10655 && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10656 && (GET_MODE_UNIT_BITSIZE (result_mode)
10657 >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10659 varop = XEXP (varop, 0);
10660 continue;
10663 /* fall through */
10665 case LSHIFTRT:
10666 case ASHIFT:
10667 case ROTATE:
10668 /* The following rules apply only to scalars. */
10669 if (shift_mode != shift_unit_mode)
10670 break;
10671 int_mode = as_a <scalar_int_mode> (mode);
10672 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10673 int_result_mode = as_a <scalar_int_mode> (result_mode);
10675 /* Here we have two nested shifts. The result is usually the
10676 AND of a new shift with a mask. We compute the result below. */
10677 if (CONST_INT_P (XEXP (varop, 1))
10678 && INTVAL (XEXP (varop, 1)) >= 0
10679 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10680 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10681 && HWI_COMPUTABLE_MODE_P (int_mode))
10683 enum rtx_code first_code = GET_CODE (varop);
10684 unsigned int first_count = INTVAL (XEXP (varop, 1));
10685 unsigned HOST_WIDE_INT mask;
10686 rtx mask_rtx;
10688 /* We have one common special case. We can't do any merging if
10689 the inner code is an ASHIFTRT of a smaller mode. However, if
10690 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10691 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10692 we can convert it to
10693 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10694 This simplifies certain SIGN_EXTEND operations. */
10695 if (code == ASHIFT && first_code == ASHIFTRT
10696 && count == (GET_MODE_PRECISION (int_result_mode)
10697 - GET_MODE_PRECISION (int_varop_mode)))
10699 /* C3 has the low-order C1 bits zero. */
10701 mask = GET_MODE_MASK (int_mode)
10702 & ~((HOST_WIDE_INT_1U << first_count) - 1);
10704 varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10705 XEXP (varop, 0), mask);
10706 varop = simplify_shift_const (NULL_RTX, ASHIFT,
10707 int_result_mode, varop, count);
10708 count = first_count;
10709 code = ASHIFTRT;
10710 continue;
10713 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10714 than C1 high-order bits equal to the sign bit, we can convert
10715 this to either an ASHIFT or an ASHIFTRT depending on the
10716 two counts.
10718 We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10720 if (code == ASHIFTRT && first_code == ASHIFT
10721 && int_varop_mode == shift_unit_mode
10722 && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10723 > first_count))
10725 varop = XEXP (varop, 0);
10726 count -= first_count;
10727 if (count < 0)
10729 count = -count;
10730 code = ASHIFT;
10733 continue;
10736 /* There are some cases we can't do. If CODE is ASHIFTRT,
10737 we can only do this if FIRST_CODE is also ASHIFTRT.
10739 We can't do the case when CODE is ROTATE and FIRST_CODE is
10740 ASHIFTRT.
10742 If the mode of this shift is not the mode of the outer shift,
10743 we can't do this if either shift is a right shift or ROTATE.
10745 Finally, we can't do any of these if the mode is too wide
10746 unless the codes are the same.
10748 Handle the case where the shift codes are the same
10749 first. */
10751 if (code == first_code)
10753 if (int_varop_mode != int_result_mode
10754 && (code == ASHIFTRT || code == LSHIFTRT
10755 || code == ROTATE))
10756 break;
10758 count += first_count;
10759 varop = XEXP (varop, 0);
10760 continue;
10763 if (code == ASHIFTRT
10764 || (code == ROTATE && first_code == ASHIFTRT)
10765 || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10766 || (int_varop_mode != int_result_mode
10767 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10768 || first_code == ROTATE
10769 || code == ROTATE)))
10770 break;
10772 /* To compute the mask to apply after the shift, shift the
10773 nonzero bits of the inner shift the same way the
10774 outer shift will. */
10776 mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10777 int_result_mode);
10779 mask_rtx
10780 = simplify_const_binary_operation (code, int_result_mode,
10781 mask_rtx, GEN_INT (count));
10783 /* Give up if we can't compute an outer operation to use. */
10784 if (mask_rtx == 0
10785 || !CONST_INT_P (mask_rtx)
10786 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10787 INTVAL (mask_rtx),
10788 int_result_mode, &complement_p))
10789 break;
10791 /* If the shifts are in the same direction, we add the
10792 counts. Otherwise, we subtract them. */
10793 if ((code == ASHIFTRT || code == LSHIFTRT)
10794 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10795 count += first_count;
10796 else
10797 count -= first_count;
10799 /* If COUNT is positive, the new shift is usually CODE,
10800 except for the two exceptions below, in which case it is
10801 FIRST_CODE. If the count is negative, FIRST_CODE should
10802 always be used */
10803 if (count > 0
10804 && ((first_code == ROTATE && code == ASHIFT)
10805 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10806 code = first_code;
10807 else if (count < 0)
10808 code = first_code, count = -count;
10810 varop = XEXP (varop, 0);
10811 continue;
10814 /* If we have (A << B << C) for any shift, we can convert this to
10815 (A << C << B). This wins if A is a constant. Only try this if
10816 B is not a constant. */
10818 else if (GET_CODE (varop) == code
10819 && CONST_INT_P (XEXP (varop, 0))
10820 && !CONST_INT_P (XEXP (varop, 1)))
10822 /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10823 sure the result will be masked. See PR70222. */
10824 if (code == LSHIFTRT
10825 && int_mode != int_result_mode
10826 && !merge_outer_ops (&outer_op, &outer_const, AND,
10827 GET_MODE_MASK (int_result_mode)
10828 >> orig_count, int_result_mode,
10829 &complement_p))
10830 break;
10831 /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
10832 up outer sign extension (often left and right shift) is
10833 hardly more efficient than the original. See PR70429. */
10834 if (code == ASHIFTRT && int_mode != int_result_mode)
10835 break;
10837 rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10838 XEXP (varop, 0),
10839 GEN_INT (count));
10840 varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10841 count = 0;
10842 continue;
10844 break;
10846 case NOT:
10847 /* The following rules apply only to scalars. */
10848 if (shift_mode != shift_unit_mode)
10849 break;
10851 /* Make this fit the case below. */
10852 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10853 continue;
10855 case IOR:
10856 case AND:
10857 case XOR:
10858 /* The following rules apply only to scalars. */
10859 if (shift_mode != shift_unit_mode)
10860 break;
10861 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10862 int_result_mode = as_a <scalar_int_mode> (result_mode);
10864 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10865 with C the size of VAROP - 1 and the shift is logical if
10866 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10867 we have an (le X 0) operation. If we have an arithmetic shift
10868 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10869 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10871 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10872 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10873 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10874 && (code == LSHIFTRT || code == ASHIFTRT)
10875 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
10876 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10878 count = 0;
10879 varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
10880 const0_rtx);
10882 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10883 varop = gen_rtx_NEG (int_varop_mode, varop);
10885 continue;
10888 /* If we have (shift (logical)), move the logical to the outside
10889 to allow it to possibly combine with another logical and the
10890 shift to combine with another shift. This also canonicalizes to
10891 what a ZERO_EXTRACT looks like. Also, some machines have
10892 (and (shift)) insns. */
10894 if (CONST_INT_P (XEXP (varop, 1))
10895 /* We can't do this if we have (ashiftrt (xor)) and the
10896 constant has its sign bit set in shift_unit_mode with
10897 shift_unit_mode wider than result_mode. */
10898 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10899 && int_result_mode != shift_unit_mode
10900 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10901 shift_unit_mode))
10902 && (new_rtx = simplify_const_binary_operation
10903 (code, int_result_mode,
10904 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
10905 GEN_INT (count))) != 0
10906 && CONST_INT_P (new_rtx)
10907 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10908 INTVAL (new_rtx), int_result_mode,
10909 &complement_p))
10911 varop = XEXP (varop, 0);
10912 continue;
10915 /* If we can't do that, try to simplify the shift in each arm of the
10916 logical expression, make a new logical expression, and apply
10917 the inverse distributive law. This also can't be done for
10918 (ashiftrt (xor)) where we've widened the shift and the constant
10919 changes the sign bit. */
10920 if (CONST_INT_P (XEXP (varop, 1))
10921 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10922 && int_result_mode != shift_unit_mode
10923 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10924 shift_unit_mode)))
10926 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10927 XEXP (varop, 0), count);
10928 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10929 XEXP (varop, 1), count);
10931 varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
10932 lhs, rhs);
10933 varop = apply_distributive_law (varop);
10935 count = 0;
10936 continue;
10938 break;
10940 case EQ:
10941 /* The following rules apply only to scalars. */
10942 if (shift_mode != shift_unit_mode)
10943 break;
10944 int_result_mode = as_a <scalar_int_mode> (result_mode);
10946 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10947 says that the sign bit can be tested, FOO has mode MODE, C is
10948 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10949 that may be nonzero. */
10950 if (code == LSHIFTRT
10951 && XEXP (varop, 1) == const0_rtx
10952 && GET_MODE (XEXP (varop, 0)) == int_result_mode
10953 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10954 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10955 && STORE_FLAG_VALUE == -1
10956 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
10957 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
10958 int_result_mode, &complement_p))
10960 varop = XEXP (varop, 0);
10961 count = 0;
10962 continue;
10964 break;
10966 case NEG:
10967 /* The following rules apply only to scalars. */
10968 if (shift_mode != shift_unit_mode)
10969 break;
10970 int_result_mode = as_a <scalar_int_mode> (result_mode);
10972 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10973 than the number of bits in the mode is equivalent to A. */
10974 if (code == LSHIFTRT
10975 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10976 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
10978 varop = XEXP (varop, 0);
10979 count = 0;
10980 continue;
10983 /* NEG commutes with ASHIFT since it is multiplication. Move the
10984 NEG outside to allow shifts to combine. */
10985 if (code == ASHIFT
10986 && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
10987 int_result_mode, &complement_p))
10989 varop = XEXP (varop, 0);
10990 continue;
10992 break;
10994 case PLUS:
10995 /* The following rules apply only to scalars. */
10996 if (shift_mode != shift_unit_mode)
10997 break;
10998 int_result_mode = as_a <scalar_int_mode> (result_mode);
11000 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11001 is one less than the number of bits in the mode is
11002 equivalent to (xor A 1). */
11003 if (code == LSHIFTRT
11004 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11005 && XEXP (varop, 1) == constm1_rtx
11006 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11007 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11008 int_result_mode, &complement_p))
11010 count = 0;
11011 varop = XEXP (varop, 0);
11012 continue;
11015 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11016 that might be nonzero in BAR are those being shifted out and those
11017 bits are known zero in FOO, we can replace the PLUS with FOO.
11018 Similarly in the other operand order. This code occurs when
11019 we are computing the size of a variable-size array. */
11021 if ((code == ASHIFTRT || code == LSHIFTRT)
11022 && count < HOST_BITS_PER_WIDE_INT
11023 && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11024 && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11025 & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11027 varop = XEXP (varop, 0);
11028 continue;
11030 else if ((code == ASHIFTRT || code == LSHIFTRT)
11031 && count < HOST_BITS_PER_WIDE_INT
11032 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11033 && 0 == (nonzero_bits (XEXP (varop, 0), int_result_mode)
11034 >> count)
11035 && 0 == (nonzero_bits (XEXP (varop, 0), int_result_mode)
11036 & nonzero_bits (XEXP (varop, 1), int_result_mode)))
11038 varop = XEXP (varop, 1);
11039 continue;
11042 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11043 if (code == ASHIFT
11044 && CONST_INT_P (XEXP (varop, 1))
11045 && (new_rtx = simplify_const_binary_operation
11046 (ASHIFT, int_result_mode,
11047 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11048 GEN_INT (count))) != 0
11049 && CONST_INT_P (new_rtx)
11050 && merge_outer_ops (&outer_op, &outer_const, PLUS,
11051 INTVAL (new_rtx), int_result_mode,
11052 &complement_p))
11054 varop = XEXP (varop, 0);
11055 continue;
11058 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11059 signbit', and attempt to change the PLUS to an XOR and move it to
11060 the outer operation as is done above in the AND/IOR/XOR case
11061 leg for shift(logical). See details in logical handling above
11062 for reasoning in doing so. */
11063 if (code == LSHIFTRT
11064 && CONST_INT_P (XEXP (varop, 1))
11065 && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11066 && (new_rtx = simplify_const_binary_operation
11067 (code, int_result_mode,
11068 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11069 GEN_INT (count))) != 0
11070 && CONST_INT_P (new_rtx)
11071 && merge_outer_ops (&outer_op, &outer_const, XOR,
11072 INTVAL (new_rtx), int_result_mode,
11073 &complement_p))
11075 varop = XEXP (varop, 0);
11076 continue;
11079 break;
11081 case MINUS:
11082 /* The following rules apply only to scalars. */
11083 if (shift_mode != shift_unit_mode)
11084 break;
11085 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11087 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11088 with C the size of VAROP - 1 and the shift is logical if
11089 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11090 we have a (gt X 0) operation. If the shift is arithmetic with
11091 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11092 we have a (neg (gt X 0)) operation. */
11094 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11095 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11096 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11097 && (code == LSHIFTRT || code == ASHIFTRT)
11098 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11099 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11100 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11102 count = 0;
11103 varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11104 const0_rtx);
11106 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11107 varop = gen_rtx_NEG (int_varop_mode, varop);
11109 continue;
11111 break;
11113 case TRUNCATE:
11114 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11115 if the truncate does not affect the value. */
11116 if (code == LSHIFTRT
11117 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11118 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11119 && (INTVAL (XEXP (XEXP (varop, 0), 1))
11120 >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11121 - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11123 rtx varop_inner = XEXP (varop, 0);
11125 varop_inner
11126 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11127 XEXP (varop_inner, 0),
11128 GEN_INT
11129 (count + INTVAL (XEXP (varop_inner, 1))));
11130 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11131 count = 0;
11132 continue;
11134 break;
11136 default:
11137 break;
11140 break;
11143 shift_mode = result_mode;
11144 if (shift_mode != mode)
11146 /* We only change the modes of scalar shifts. */
11147 int_mode = as_a <scalar_int_mode> (mode);
11148 int_result_mode = as_a <scalar_int_mode> (result_mode);
11149 shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11150 int_mode, outer_op, outer_const);
11153 /* We have now finished analyzing the shift. The result should be
11154 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11155 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11156 to the result of the shift. OUTER_CONST is the relevant constant,
11157 but we must turn off all bits turned off in the shift. */
11159 if (outer_op == UNKNOWN
11160 && orig_code == code && orig_count == count
11161 && varop == orig_varop
11162 && shift_mode == GET_MODE (varop))
11163 return NULL_RTX;
11165 /* Make a SUBREG if necessary. If we can't make it, fail. */
11166 varop = gen_lowpart (shift_mode, varop);
11167 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11168 return NULL_RTX;
11170 /* If we have an outer operation and we just made a shift, it is
11171 possible that we could have simplified the shift were it not
11172 for the outer operation. So try to do the simplification
11173 recursively. */
11175 if (outer_op != UNKNOWN)
11176 x = simplify_shift_const_1 (code, shift_mode, varop, count);
11177 else
11178 x = NULL_RTX;
11180 if (x == NULL_RTX)
11181 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
11183 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11184 turn off all the bits that the shift would have turned off. */
11185 if (orig_code == LSHIFTRT && result_mode != shift_mode)
11186 /* We only change the modes of scalar shifts. */
11187 x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11188 x, GET_MODE_MASK (result_mode) >> orig_count);
11190 /* Do the remainder of the processing in RESULT_MODE. */
11191 x = gen_lowpart_or_truncate (result_mode, x);
11193 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11194 operation. */
11195 if (complement_p)
11196 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11198 if (outer_op != UNKNOWN)
11200 int_result_mode = as_a <scalar_int_mode> (result_mode);
11202 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11203 && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11204 outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11206 if (outer_op == AND)
11207 x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11208 else if (outer_op == SET)
11210 /* This means that we have determined that the result is
11211 equivalent to a constant. This should be rare. */
11212 if (!side_effects_p (x))
11213 x = GEN_INT (outer_const);
11215 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11216 x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11217 else
11218 x = simplify_gen_binary (outer_op, int_result_mode, x,
11219 GEN_INT (outer_const));
11222 return x;
11225 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11226 The result of the shift is RESULT_MODE. If we cannot simplify it,
11227 return X or, if it is NULL, synthesize the expression with
11228 simplify_gen_binary. Otherwise, return a simplified value.
11230 The shift is normally computed in the widest mode we find in VAROP, as
11231 long as it isn't a different number of words than RESULT_MODE. Exceptions
11232 are ASHIFTRT and ROTATE, which are always done in their original mode. */
11234 static rtx
11235 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11236 rtx varop, int count)
11238 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11239 if (tem)
11240 return tem;
11242 if (!x)
11243 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
11244 if (GET_MODE (x) != result_mode)
11245 x = gen_lowpart (result_mode, x);
11246 return x;
11250 /* A subroutine of recog_for_combine. See there for arguments and
11251 return value. */
11253 static int
11254 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11256 rtx pat = *pnewpat;
11257 rtx pat_without_clobbers;
11258 int insn_code_number;
11259 int num_clobbers_to_add = 0;
11260 int i;
11261 rtx notes = NULL_RTX;
11262 rtx old_notes, old_pat;
11263 int old_icode;
11265 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11266 we use to indicate that something didn't match. If we find such a
11267 thing, force rejection. */
11268 if (GET_CODE (pat) == PARALLEL)
11269 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11270 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11271 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11272 return -1;
11274 old_pat = PATTERN (insn);
11275 old_notes = REG_NOTES (insn);
11276 PATTERN (insn) = pat;
11277 REG_NOTES (insn) = NULL_RTX;
11279 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11280 if (dump_file && (dump_flags & TDF_DETAILS))
11282 if (insn_code_number < 0)
11283 fputs ("Failed to match this instruction:\n", dump_file);
11284 else
11285 fputs ("Successfully matched this instruction:\n", dump_file);
11286 print_rtl_single (dump_file, pat);
11289 /* If it isn't, there is the possibility that we previously had an insn
11290 that clobbered some register as a side effect, but the combined
11291 insn doesn't need to do that. So try once more without the clobbers
11292 unless this represents an ASM insn. */
11294 if (insn_code_number < 0 && ! check_asm_operands (pat)
11295 && GET_CODE (pat) == PARALLEL)
11297 int pos;
11299 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11300 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11302 if (i != pos)
11303 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11304 pos++;
11307 SUBST_INT (XVECLEN (pat, 0), pos);
11309 if (pos == 1)
11310 pat = XVECEXP (pat, 0, 0);
11312 PATTERN (insn) = pat;
11313 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11314 if (dump_file && (dump_flags & TDF_DETAILS))
11316 if (insn_code_number < 0)
11317 fputs ("Failed to match this instruction:\n", dump_file);
11318 else
11319 fputs ("Successfully matched this instruction:\n", dump_file);
11320 print_rtl_single (dump_file, pat);
11324 pat_without_clobbers = pat;
11326 PATTERN (insn) = old_pat;
11327 REG_NOTES (insn) = old_notes;
11329 /* Recognize all noop sets, these will be killed by followup pass. */
11330 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11331 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11333 /* If we had any clobbers to add, make a new pattern than contains
11334 them. Then check to make sure that all of them are dead. */
11335 if (num_clobbers_to_add)
11337 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11338 rtvec_alloc (GET_CODE (pat) == PARALLEL
11339 ? (XVECLEN (pat, 0)
11340 + num_clobbers_to_add)
11341 : num_clobbers_to_add + 1));
11343 if (GET_CODE (pat) == PARALLEL)
11344 for (i = 0; i < XVECLEN (pat, 0); i++)
11345 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11346 else
11347 XVECEXP (newpat, 0, 0) = pat;
11349 add_clobbers (newpat, insn_code_number);
11351 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11352 i < XVECLEN (newpat, 0); i++)
11354 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11355 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11356 return -1;
11357 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11359 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11360 notes = alloc_reg_note (REG_UNUSED,
11361 XEXP (XVECEXP (newpat, 0, i), 0), notes);
11364 pat = newpat;
11367 if (insn_code_number >= 0
11368 && insn_code_number != NOOP_MOVE_INSN_CODE)
11370 old_pat = PATTERN (insn);
11371 old_notes = REG_NOTES (insn);
11372 old_icode = INSN_CODE (insn);
11373 PATTERN (insn) = pat;
11374 REG_NOTES (insn) = notes;
11375 INSN_CODE (insn) = insn_code_number;
11377 /* Allow targets to reject combined insn. */
11378 if (!targetm.legitimate_combined_insn (insn))
11380 if (dump_file && (dump_flags & TDF_DETAILS))
11381 fputs ("Instruction not appropriate for target.",
11382 dump_file);
11384 /* Callers expect recog_for_combine to strip
11385 clobbers from the pattern on failure. */
11386 pat = pat_without_clobbers;
11387 notes = NULL_RTX;
11389 insn_code_number = -1;
11392 PATTERN (insn) = old_pat;
11393 REG_NOTES (insn) = old_notes;
11394 INSN_CODE (insn) = old_icode;
11397 *pnewpat = pat;
11398 *pnotes = notes;
11400 return insn_code_number;
11403 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11404 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11405 Return whether anything was so changed. */
11407 static bool
11408 change_zero_ext (rtx pat)
11410 bool changed = false;
11411 rtx *src = &SET_SRC (pat);
11413 subrtx_ptr_iterator::array_type array;
11414 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11416 rtx x = **iter;
11417 scalar_int_mode mode, inner_mode;
11418 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11419 continue;
11420 int size;
11422 if (GET_CODE (x) == ZERO_EXTRACT
11423 && CONST_INT_P (XEXP (x, 1))
11424 && CONST_INT_P (XEXP (x, 2))
11425 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11426 && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11428 size = INTVAL (XEXP (x, 1));
11430 int start = INTVAL (XEXP (x, 2));
11431 if (BITS_BIG_ENDIAN)
11432 start = GET_MODE_PRECISION (inner_mode) - size - start;
11434 if (start)
11435 x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0), GEN_INT (start));
11436 else
11437 x = XEXP (x, 0);
11438 if (mode != inner_mode)
11439 x = gen_lowpart_SUBREG (mode, x);
11441 else if (GET_CODE (x) == ZERO_EXTEND
11442 && GET_CODE (XEXP (x, 0)) == SUBREG
11443 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11444 && !paradoxical_subreg_p (XEXP (x, 0))
11445 && subreg_lowpart_p (XEXP (x, 0)))
11447 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11448 size = GET_MODE_PRECISION (inner_mode);
11449 x = SUBREG_REG (XEXP (x, 0));
11450 if (GET_MODE (x) != mode)
11451 x = gen_lowpart_SUBREG (mode, x);
11453 else if (GET_CODE (x) == ZERO_EXTEND
11454 && REG_P (XEXP (x, 0))
11455 && HARD_REGISTER_P (XEXP (x, 0))
11456 && can_change_dest_mode (XEXP (x, 0), 0, mode))
11458 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11459 size = GET_MODE_PRECISION (inner_mode);
11460 x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11462 else
11463 continue;
11465 if (!(GET_CODE (x) == LSHIFTRT
11466 && CONST_INT_P (XEXP (x, 1))
11467 && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11469 wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11470 x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11473 SUBST (**iter, x);
11474 changed = true;
11477 if (changed)
11478 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11479 maybe_swap_commutative_operands (**iter);
11481 rtx *dst = &SET_DEST (pat);
11482 scalar_int_mode mode;
11483 if (GET_CODE (*dst) == ZERO_EXTRACT
11484 && REG_P (XEXP (*dst, 0))
11485 && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11486 && CONST_INT_P (XEXP (*dst, 1))
11487 && CONST_INT_P (XEXP (*dst, 2)))
11489 rtx reg = XEXP (*dst, 0);
11490 int width = INTVAL (XEXP (*dst, 1));
11491 int offset = INTVAL (XEXP (*dst, 2));
11492 int reg_width = GET_MODE_PRECISION (mode);
11493 if (BITS_BIG_ENDIAN)
11494 offset = reg_width - width - offset;
11496 rtx x, y, z, w;
11497 wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11498 wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11499 x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11500 if (offset)
11501 y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11502 else
11503 y = SET_SRC (pat);
11504 z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11505 w = gen_rtx_IOR (mode, x, z);
11506 SUBST (SET_DEST (pat), reg);
11507 SUBST (SET_SRC (pat), w);
11509 changed = true;
11512 return changed;
11515 /* Like recog, but we receive the address of a pointer to a new pattern.
11516 We try to match the rtx that the pointer points to.
11517 If that fails, we may try to modify or replace the pattern,
11518 storing the replacement into the same pointer object.
11520 Modifications include deletion or addition of CLOBBERs. If the
11521 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11522 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11523 (and undo if that fails).
11525 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11526 the CLOBBERs are placed.
11528 The value is the final insn code from the pattern ultimately matched,
11529 or -1. */
11531 static int
11532 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11534 rtx pat = *pnewpat;
11535 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11536 if (insn_code_number >= 0 || check_asm_operands (pat))
11537 return insn_code_number;
11539 void *marker = get_undo_marker ();
11540 bool changed = false;
11542 if (GET_CODE (pat) == SET)
11543 changed = change_zero_ext (pat);
11544 else if (GET_CODE (pat) == PARALLEL)
11546 int i;
11547 for (i = 0; i < XVECLEN (pat, 0); i++)
11549 rtx set = XVECEXP (pat, 0, i);
11550 if (GET_CODE (set) == SET)
11551 changed |= change_zero_ext (set);
11555 if (changed)
11557 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11559 if (insn_code_number < 0)
11560 undo_to_marker (marker);
11563 return insn_code_number;
11566 /* Like gen_lowpart_general but for use by combine. In combine it
11567 is not possible to create any new pseudoregs. However, it is
11568 safe to create invalid memory addresses, because combine will
11569 try to recognize them and all they will do is make the combine
11570 attempt fail.
11572 If for some reason this cannot do its job, an rtx
11573 (clobber (const_int 0)) is returned.
11574 An insn containing that will not be recognized. */
11576 static rtx
11577 gen_lowpart_for_combine (machine_mode omode, rtx x)
11579 machine_mode imode = GET_MODE (x);
11580 unsigned int osize = GET_MODE_SIZE (omode);
11581 unsigned int isize = GET_MODE_SIZE (imode);
11582 rtx result;
11584 if (omode == imode)
11585 return x;
11587 /* We can only support MODE being wider than a word if X is a
11588 constant integer or has a mode the same size. */
11589 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11590 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11591 goto fail;
11593 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11594 won't know what to do. So we will strip off the SUBREG here and
11595 process normally. */
11596 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11598 x = SUBREG_REG (x);
11600 /* For use in case we fall down into the address adjustments
11601 further below, we need to adjust the known mode and size of
11602 x; imode and isize, since we just adjusted x. */
11603 imode = GET_MODE (x);
11605 if (imode == omode)
11606 return x;
11608 isize = GET_MODE_SIZE (imode);
11611 result = gen_lowpart_common (omode, x);
11613 if (result)
11614 return result;
11616 if (MEM_P (x))
11618 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11619 address. */
11620 if (MEM_VOLATILE_P (x)
11621 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11622 goto fail;
11624 /* If we want to refer to something bigger than the original memref,
11625 generate a paradoxical subreg instead. That will force a reload
11626 of the original memref X. */
11627 if (paradoxical_subreg_p (omode, imode))
11628 return gen_rtx_SUBREG (omode, x, 0);
11630 HOST_WIDE_INT offset = byte_lowpart_offset (omode, imode);
11631 return adjust_address_nv (x, omode, offset);
11634 /* If X is a comparison operator, rewrite it in a new mode. This
11635 probably won't match, but may allow further simplifications. */
11636 else if (COMPARISON_P (x))
11637 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11639 /* If we couldn't simplify X any other way, just enclose it in a
11640 SUBREG. Normally, this SUBREG won't match, but some patterns may
11641 include an explicit SUBREG or we may simplify it further in combine. */
11642 else
11644 rtx res;
11646 if (imode == VOIDmode)
11648 imode = int_mode_for_mode (omode).require ();
11649 x = gen_lowpart_common (imode, x);
11650 if (x == NULL)
11651 goto fail;
11653 res = lowpart_subreg (omode, x, imode);
11654 if (res)
11655 return res;
11658 fail:
11659 return gen_rtx_CLOBBER (omode, const0_rtx);
11662 /* Try to simplify a comparison between OP0 and a constant OP1,
11663 where CODE is the comparison code that will be tested, into a
11664 (CODE OP0 const0_rtx) form.
11666 The result is a possibly different comparison code to use.
11667 *POP1 may be updated. */
11669 static enum rtx_code
11670 simplify_compare_const (enum rtx_code code, machine_mode mode,
11671 rtx op0, rtx *pop1)
11673 scalar_int_mode int_mode;
11674 HOST_WIDE_INT const_op = INTVAL (*pop1);
11676 /* Get the constant we are comparing against and turn off all bits
11677 not on in our mode. */
11678 if (mode != VOIDmode)
11679 const_op = trunc_int_for_mode (const_op, mode);
11681 /* If we are comparing against a constant power of two and the value
11682 being compared can only have that single bit nonzero (e.g., it was
11683 `and'ed with that bit), we can replace this with a comparison
11684 with zero. */
11685 if (const_op
11686 && (code == EQ || code == NE || code == GE || code == GEU
11687 || code == LT || code == LTU)
11688 && is_a <scalar_int_mode> (mode, &int_mode)
11689 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11690 && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11691 && (nonzero_bits (op0, int_mode)
11692 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11694 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11695 const_op = 0;
11698 /* Similarly, if we are comparing a value known to be either -1 or
11699 0 with -1, change it to the opposite comparison against zero. */
11700 if (const_op == -1
11701 && (code == EQ || code == NE || code == GT || code == LE
11702 || code == GEU || code == LTU)
11703 && is_a <scalar_int_mode> (mode, &int_mode)
11704 && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11706 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11707 const_op = 0;
11710 /* Do some canonicalizations based on the comparison code. We prefer
11711 comparisons against zero and then prefer equality comparisons.
11712 If we can reduce the size of a constant, we will do that too. */
11713 switch (code)
11715 case LT:
11716 /* < C is equivalent to <= (C - 1) */
11717 if (const_op > 0)
11719 const_op -= 1;
11720 code = LE;
11721 /* ... fall through to LE case below. */
11722 gcc_fallthrough ();
11724 else
11725 break;
11727 case LE:
11728 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11729 if (const_op < 0)
11731 const_op += 1;
11732 code = LT;
11735 /* If we are doing a <= 0 comparison on a value known to have
11736 a zero sign bit, we can replace this with == 0. */
11737 else if (const_op == 0
11738 && is_a <scalar_int_mode> (mode, &int_mode)
11739 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11740 && (nonzero_bits (op0, int_mode)
11741 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11742 == 0)
11743 code = EQ;
11744 break;
11746 case GE:
11747 /* >= C is equivalent to > (C - 1). */
11748 if (const_op > 0)
11750 const_op -= 1;
11751 code = GT;
11752 /* ... fall through to GT below. */
11753 gcc_fallthrough ();
11755 else
11756 break;
11758 case GT:
11759 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11760 if (const_op < 0)
11762 const_op += 1;
11763 code = GE;
11766 /* If we are doing a > 0 comparison on a value known to have
11767 a zero sign bit, we can replace this with != 0. */
11768 else if (const_op == 0
11769 && is_a <scalar_int_mode> (mode, &int_mode)
11770 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11771 && (nonzero_bits (op0, int_mode)
11772 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11773 == 0)
11774 code = NE;
11775 break;
11777 case LTU:
11778 /* < C is equivalent to <= (C - 1). */
11779 if (const_op > 0)
11781 const_op -= 1;
11782 code = LEU;
11783 /* ... fall through ... */
11784 gcc_fallthrough ();
11786 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11787 else if (is_a <scalar_int_mode> (mode, &int_mode)
11788 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11789 && ((unsigned HOST_WIDE_INT) const_op
11790 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11792 const_op = 0;
11793 code = GE;
11794 break;
11796 else
11797 break;
11799 case LEU:
11800 /* unsigned <= 0 is equivalent to == 0 */
11801 if (const_op == 0)
11802 code = EQ;
11803 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11804 else if (is_a <scalar_int_mode> (mode, &int_mode)
11805 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11806 && ((unsigned HOST_WIDE_INT) const_op
11807 == ((HOST_WIDE_INT_1U
11808 << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11810 const_op = 0;
11811 code = GE;
11813 break;
11815 case GEU:
11816 /* >= C is equivalent to > (C - 1). */
11817 if (const_op > 1)
11819 const_op -= 1;
11820 code = GTU;
11821 /* ... fall through ... */
11822 gcc_fallthrough ();
11825 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11826 else if (is_a <scalar_int_mode> (mode, &int_mode)
11827 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11828 && ((unsigned HOST_WIDE_INT) const_op
11829 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11831 const_op = 0;
11832 code = LT;
11833 break;
11835 else
11836 break;
11838 case GTU:
11839 /* unsigned > 0 is equivalent to != 0 */
11840 if (const_op == 0)
11841 code = NE;
11842 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11843 else if (is_a <scalar_int_mode> (mode, &int_mode)
11844 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11845 && ((unsigned HOST_WIDE_INT) const_op
11846 == (HOST_WIDE_INT_1U
11847 << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
11849 const_op = 0;
11850 code = LT;
11852 break;
11854 default:
11855 break;
11858 *pop1 = GEN_INT (const_op);
11859 return code;
11862 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11863 comparison code that will be tested.
11865 The result is a possibly different comparison code to use. *POP0 and
11866 *POP1 may be updated.
11868 It is possible that we might detect that a comparison is either always
11869 true or always false. However, we do not perform general constant
11870 folding in combine, so this knowledge isn't useful. Such tautologies
11871 should have been detected earlier. Hence we ignore all such cases. */
11873 static enum rtx_code
11874 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11876 rtx op0 = *pop0;
11877 rtx op1 = *pop1;
11878 rtx tem, tem1;
11879 int i;
11880 scalar_int_mode mode, inner_mode, tmode;
11881 opt_scalar_int_mode tmode_iter;
11883 /* Try a few ways of applying the same transformation to both operands. */
11884 while (1)
11886 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11887 so check specially. */
11888 if (!WORD_REGISTER_OPERATIONS
11889 && code != GTU && code != GEU && code != LTU && code != LEU
11890 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11891 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11892 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11893 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11894 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11895 && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
11896 && (is_a <scalar_int_mode>
11897 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
11898 && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
11899 && CONST_INT_P (XEXP (op0, 1))
11900 && XEXP (op0, 1) == XEXP (op1, 1)
11901 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11902 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11903 && (INTVAL (XEXP (op0, 1))
11904 == (GET_MODE_PRECISION (mode)
11905 - GET_MODE_PRECISION (inner_mode))))
11907 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11908 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11911 /* If both operands are the same constant shift, see if we can ignore the
11912 shift. We can if the shift is a rotate or if the bits shifted out of
11913 this shift are known to be zero for both inputs and if the type of
11914 comparison is compatible with the shift. */
11915 if (GET_CODE (op0) == GET_CODE (op1)
11916 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11917 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11918 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11919 && (code != GT && code != LT && code != GE && code != LE))
11920 || (GET_CODE (op0) == ASHIFTRT
11921 && (code != GTU && code != LTU
11922 && code != GEU && code != LEU)))
11923 && CONST_INT_P (XEXP (op0, 1))
11924 && INTVAL (XEXP (op0, 1)) >= 0
11925 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11926 && XEXP (op0, 1) == XEXP (op1, 1))
11928 machine_mode mode = GET_MODE (op0);
11929 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11930 int shift_count = INTVAL (XEXP (op0, 1));
11932 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11933 mask &= (mask >> shift_count) << shift_count;
11934 else if (GET_CODE (op0) == ASHIFT)
11935 mask = (mask & (mask << shift_count)) >> shift_count;
11937 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11938 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11939 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11940 else
11941 break;
11944 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11945 SUBREGs are of the same mode, and, in both cases, the AND would
11946 be redundant if the comparison was done in the narrower mode,
11947 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11948 and the operand's possibly nonzero bits are 0xffffff01; in that case
11949 if we only care about QImode, we don't need the AND). This case
11950 occurs if the output mode of an scc insn is not SImode and
11951 STORE_FLAG_VALUE == 1 (e.g., the 386).
11953 Similarly, check for a case where the AND's are ZERO_EXTEND
11954 operations from some narrower mode even though a SUBREG is not
11955 present. */
11957 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11958 && CONST_INT_P (XEXP (op0, 1))
11959 && CONST_INT_P (XEXP (op1, 1)))
11961 rtx inner_op0 = XEXP (op0, 0);
11962 rtx inner_op1 = XEXP (op1, 0);
11963 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11964 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11965 int changed = 0;
11967 if (paradoxical_subreg_p (inner_op0)
11968 && GET_CODE (inner_op1) == SUBREG
11969 && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
11970 && (GET_MODE (SUBREG_REG (inner_op0))
11971 == GET_MODE (SUBREG_REG (inner_op1)))
11972 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11973 GET_MODE (SUBREG_REG (inner_op0)))))
11974 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11975 GET_MODE (SUBREG_REG (inner_op1))))))
11977 op0 = SUBREG_REG (inner_op0);
11978 op1 = SUBREG_REG (inner_op1);
11980 /* The resulting comparison is always unsigned since we masked
11981 off the original sign bit. */
11982 code = unsigned_condition (code);
11984 changed = 1;
11987 else if (c0 == c1)
11988 FOR_EACH_MODE_UNTIL (tmode,
11989 as_a <scalar_int_mode> (GET_MODE (op0)))
11990 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11992 op0 = gen_lowpart_or_truncate (tmode, inner_op0);
11993 op1 = gen_lowpart_or_truncate (tmode, inner_op1);
11994 code = unsigned_condition (code);
11995 changed = 1;
11996 break;
11999 if (! changed)
12000 break;
12003 /* If both operands are NOT, we can strip off the outer operation
12004 and adjust the comparison code for swapped operands; similarly for
12005 NEG, except that this must be an equality comparison. */
12006 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12007 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12008 && (code == EQ || code == NE)))
12009 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12011 else
12012 break;
12015 /* If the first operand is a constant, swap the operands and adjust the
12016 comparison code appropriately, but don't do this if the second operand
12017 is already a constant integer. */
12018 if (swap_commutative_operands_p (op0, op1))
12020 std::swap (op0, op1);
12021 code = swap_condition (code);
12024 /* We now enter a loop during which we will try to simplify the comparison.
12025 For the most part, we only are concerned with comparisons with zero,
12026 but some things may really be comparisons with zero but not start
12027 out looking that way. */
12029 while (CONST_INT_P (op1))
12031 machine_mode raw_mode = GET_MODE (op0);
12032 scalar_int_mode int_mode;
12033 int equality_comparison_p;
12034 int sign_bit_comparison_p;
12035 int unsigned_comparison_p;
12036 HOST_WIDE_INT const_op;
12038 /* We only want to handle integral modes. This catches VOIDmode,
12039 CCmode, and the floating-point modes. An exception is that we
12040 can handle VOIDmode if OP0 is a COMPARE or a comparison
12041 operation. */
12043 if (GET_MODE_CLASS (raw_mode) != MODE_INT
12044 && ! (raw_mode == VOIDmode
12045 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12046 break;
12048 /* Try to simplify the compare to constant, possibly changing the
12049 comparison op, and/or changing op1 to zero. */
12050 code = simplify_compare_const (code, raw_mode, op0, &op1);
12051 const_op = INTVAL (op1);
12053 /* Compute some predicates to simplify code below. */
12055 equality_comparison_p = (code == EQ || code == NE);
12056 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12057 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12058 || code == GEU);
12060 /* If this is a sign bit comparison and we can do arithmetic in
12061 MODE, say that we will only be needing the sign bit of OP0. */
12062 if (sign_bit_comparison_p
12063 && is_a <scalar_int_mode> (raw_mode, &int_mode)
12064 && HWI_COMPUTABLE_MODE_P (int_mode))
12065 op0 = force_to_mode (op0, int_mode,
12066 HOST_WIDE_INT_1U
12067 << (GET_MODE_PRECISION (int_mode) - 1),
12070 if (COMPARISON_P (op0))
12072 /* We can't do anything if OP0 is a condition code value, rather
12073 than an actual data value. */
12074 if (const_op != 0
12075 || CC0_P (XEXP (op0, 0))
12076 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12077 break;
12079 /* Get the two operands being compared. */
12080 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12081 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12082 else
12083 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12085 /* Check for the cases where we simply want the result of the
12086 earlier test or the opposite of that result. */
12087 if (code == NE || code == EQ
12088 || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12089 && (code == LT || code == GE)))
12091 enum rtx_code new_code;
12092 if (code == LT || code == NE)
12093 new_code = GET_CODE (op0);
12094 else
12095 new_code = reversed_comparison_code (op0, NULL);
12097 if (new_code != UNKNOWN)
12099 code = new_code;
12100 op0 = tem;
12101 op1 = tem1;
12102 continue;
12105 break;
12108 if (raw_mode == VOIDmode)
12109 break;
12110 scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12112 /* Now try cases based on the opcode of OP0. If none of the cases
12113 does a "continue", we exit this loop immediately after the
12114 switch. */
12116 unsigned int mode_width = GET_MODE_PRECISION (mode);
12117 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12118 switch (GET_CODE (op0))
12120 case ZERO_EXTRACT:
12121 /* If we are extracting a single bit from a variable position in
12122 a constant that has only a single bit set and are comparing it
12123 with zero, we can convert this into an equality comparison
12124 between the position and the location of the single bit. */
12125 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12126 have already reduced the shift count modulo the word size. */
12127 if (!SHIFT_COUNT_TRUNCATED
12128 && CONST_INT_P (XEXP (op0, 0))
12129 && XEXP (op0, 1) == const1_rtx
12130 && equality_comparison_p && const_op == 0
12131 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12133 if (BITS_BIG_ENDIAN)
12134 i = BITS_PER_WORD - 1 - i;
12136 op0 = XEXP (op0, 2);
12137 op1 = GEN_INT (i);
12138 const_op = i;
12140 /* Result is nonzero iff shift count is equal to I. */
12141 code = reverse_condition (code);
12142 continue;
12145 /* fall through */
12147 case SIGN_EXTRACT:
12148 tem = expand_compound_operation (op0);
12149 if (tem != op0)
12151 op0 = tem;
12152 continue;
12154 break;
12156 case NOT:
12157 /* If testing for equality, we can take the NOT of the constant. */
12158 if (equality_comparison_p
12159 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12161 op0 = XEXP (op0, 0);
12162 op1 = tem;
12163 continue;
12166 /* If just looking at the sign bit, reverse the sense of the
12167 comparison. */
12168 if (sign_bit_comparison_p)
12170 op0 = XEXP (op0, 0);
12171 code = (code == GE ? LT : GE);
12172 continue;
12174 break;
12176 case NEG:
12177 /* If testing for equality, we can take the NEG of the constant. */
12178 if (equality_comparison_p
12179 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12181 op0 = XEXP (op0, 0);
12182 op1 = tem;
12183 continue;
12186 /* The remaining cases only apply to comparisons with zero. */
12187 if (const_op != 0)
12188 break;
12190 /* When X is ABS or is known positive,
12191 (neg X) is < 0 if and only if X != 0. */
12193 if (sign_bit_comparison_p
12194 && (GET_CODE (XEXP (op0, 0)) == ABS
12195 || (mode_width <= HOST_BITS_PER_WIDE_INT
12196 && (nonzero_bits (XEXP (op0, 0), mode)
12197 & (HOST_WIDE_INT_1U << (mode_width - 1)))
12198 == 0)))
12200 op0 = XEXP (op0, 0);
12201 code = (code == LT ? NE : EQ);
12202 continue;
12205 /* If we have NEG of something whose two high-order bits are the
12206 same, we know that "(-a) < 0" is equivalent to "a > 0". */
12207 if (num_sign_bit_copies (op0, mode) >= 2)
12209 op0 = XEXP (op0, 0);
12210 code = swap_condition (code);
12211 continue;
12213 break;
12215 case ROTATE:
12216 /* If we are testing equality and our count is a constant, we
12217 can perform the inverse operation on our RHS. */
12218 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12219 && (tem = simplify_binary_operation (ROTATERT, mode,
12220 op1, XEXP (op0, 1))) != 0)
12222 op0 = XEXP (op0, 0);
12223 op1 = tem;
12224 continue;
12227 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12228 a particular bit. Convert it to an AND of a constant of that
12229 bit. This will be converted into a ZERO_EXTRACT. */
12230 if (const_op == 0 && sign_bit_comparison_p
12231 && CONST_INT_P (XEXP (op0, 1))
12232 && mode_width <= HOST_BITS_PER_WIDE_INT)
12234 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12235 (HOST_WIDE_INT_1U
12236 << (mode_width - 1
12237 - INTVAL (XEXP (op0, 1)))));
12238 code = (code == LT ? NE : EQ);
12239 continue;
12242 /* Fall through. */
12244 case ABS:
12245 /* ABS is ignorable inside an equality comparison with zero. */
12246 if (const_op == 0 && equality_comparison_p)
12248 op0 = XEXP (op0, 0);
12249 continue;
12251 break;
12253 case SIGN_EXTEND:
12254 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12255 (compare FOO CONST) if CONST fits in FOO's mode and we
12256 are either testing inequality or have an unsigned
12257 comparison with ZERO_EXTEND or a signed comparison with
12258 SIGN_EXTEND. But don't do it if we don't have a compare
12259 insn of the given mode, since we'd have to revert it
12260 later on, and then we wouldn't know whether to sign- or
12261 zero-extend. */
12262 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12263 && ! unsigned_comparison_p
12264 && HWI_COMPUTABLE_MODE_P (mode)
12265 && trunc_int_for_mode (const_op, mode) == const_op
12266 && have_insn_for (COMPARE, mode))
12268 op0 = XEXP (op0, 0);
12269 continue;
12271 break;
12273 case SUBREG:
12274 /* Check for the case where we are comparing A - C1 with C2, that is
12276 (subreg:MODE (plus (A) (-C1))) op (C2)
12278 with C1 a constant, and try to lift the SUBREG, i.e. to do the
12279 comparison in the wider mode. One of the following two conditions
12280 must be true in order for this to be valid:
12282 1. The mode extension results in the same bit pattern being added
12283 on both sides and the comparison is equality or unsigned. As
12284 C2 has been truncated to fit in MODE, the pattern can only be
12285 all 0s or all 1s.
12287 2. The mode extension results in the sign bit being copied on
12288 each side.
12290 The difficulty here is that we have predicates for A but not for
12291 (A - C1) so we need to check that C1 is within proper bounds so
12292 as to perturbate A as little as possible. */
12294 if (mode_width <= HOST_BITS_PER_WIDE_INT
12295 && subreg_lowpart_p (op0)
12296 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12297 &inner_mode)
12298 && GET_MODE_PRECISION (inner_mode) > mode_width
12299 && GET_CODE (SUBREG_REG (op0)) == PLUS
12300 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12302 rtx a = XEXP (SUBREG_REG (op0), 0);
12303 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12305 if ((c1 > 0
12306 && (unsigned HOST_WIDE_INT) c1
12307 < HOST_WIDE_INT_1U << (mode_width - 1)
12308 && (equality_comparison_p || unsigned_comparison_p)
12309 /* (A - C1) zero-extends if it is positive and sign-extends
12310 if it is negative, C2 both zero- and sign-extends. */
12311 && ((0 == (nonzero_bits (a, inner_mode)
12312 & ~GET_MODE_MASK (mode))
12313 && const_op >= 0)
12314 /* (A - C1) sign-extends if it is positive and 1-extends
12315 if it is negative, C2 both sign- and 1-extends. */
12316 || (num_sign_bit_copies (a, inner_mode)
12317 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12318 - mode_width)
12319 && const_op < 0)))
12320 || ((unsigned HOST_WIDE_INT) c1
12321 < HOST_WIDE_INT_1U << (mode_width - 2)
12322 /* (A - C1) always sign-extends, like C2. */
12323 && num_sign_bit_copies (a, inner_mode)
12324 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12325 - (mode_width - 1))))
12327 op0 = SUBREG_REG (op0);
12328 continue;
12332 /* If the inner mode is narrower and we are extracting the low part,
12333 we can treat the SUBREG as if it were a ZERO_EXTEND. */
12334 if (paradoxical_subreg_p (op0))
12336 else if (subreg_lowpart_p (op0)
12337 && GET_MODE_CLASS (mode) == MODE_INT
12338 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12339 && (code == NE || code == EQ)
12340 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12341 && !paradoxical_subreg_p (op0)
12342 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12343 & ~GET_MODE_MASK (mode)) == 0)
12345 /* Remove outer subregs that don't do anything. */
12346 tem = gen_lowpart (inner_mode, op1);
12348 if ((nonzero_bits (tem, inner_mode)
12349 & ~GET_MODE_MASK (mode)) == 0)
12351 op0 = SUBREG_REG (op0);
12352 op1 = tem;
12353 continue;
12355 break;
12357 else
12358 break;
12360 /* FALLTHROUGH */
12362 case ZERO_EXTEND:
12363 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12364 && (unsigned_comparison_p || equality_comparison_p)
12365 && HWI_COMPUTABLE_MODE_P (mode)
12366 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12367 && const_op >= 0
12368 && have_insn_for (COMPARE, mode))
12370 op0 = XEXP (op0, 0);
12371 continue;
12373 break;
12375 case PLUS:
12376 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12377 this for equality comparisons due to pathological cases involving
12378 overflows. */
12379 if (equality_comparison_p
12380 && 0 != (tem = simplify_binary_operation (MINUS, mode,
12381 op1, XEXP (op0, 1))))
12383 op0 = XEXP (op0, 0);
12384 op1 = tem;
12385 continue;
12388 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12389 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12390 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12392 op0 = XEXP (XEXP (op0, 0), 0);
12393 code = (code == LT ? EQ : NE);
12394 continue;
12396 break;
12398 case MINUS:
12399 /* We used to optimize signed comparisons against zero, but that
12400 was incorrect. Unsigned comparisons against zero (GTU, LEU)
12401 arrive here as equality comparisons, or (GEU, LTU) are
12402 optimized away. No need to special-case them. */
12404 /* (eq (minus A B) C) -> (eq A (plus B C)) or
12405 (eq B (minus A C)), whichever simplifies. We can only do
12406 this for equality comparisons due to pathological cases involving
12407 overflows. */
12408 if (equality_comparison_p
12409 && 0 != (tem = simplify_binary_operation (PLUS, mode,
12410 XEXP (op0, 1), op1)))
12412 op0 = XEXP (op0, 0);
12413 op1 = tem;
12414 continue;
12417 if (equality_comparison_p
12418 && 0 != (tem = simplify_binary_operation (MINUS, mode,
12419 XEXP (op0, 0), op1)))
12421 op0 = XEXP (op0, 1);
12422 op1 = tem;
12423 continue;
12426 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12427 of bits in X minus 1, is one iff X > 0. */
12428 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12429 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12430 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12431 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12433 op0 = XEXP (op0, 1);
12434 code = (code == GE ? LE : GT);
12435 continue;
12437 break;
12439 case XOR:
12440 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12441 if C is zero or B is a constant. */
12442 if (equality_comparison_p
12443 && 0 != (tem = simplify_binary_operation (XOR, mode,
12444 XEXP (op0, 1), op1)))
12446 op0 = XEXP (op0, 0);
12447 op1 = tem;
12448 continue;
12450 break;
12453 case IOR:
12454 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12455 iff X <= 0. */
12456 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12457 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12458 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12460 op0 = XEXP (op0, 1);
12461 code = (code == GE ? GT : LE);
12462 continue;
12464 break;
12466 case AND:
12467 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12468 will be converted to a ZERO_EXTRACT later. */
12469 if (const_op == 0 && equality_comparison_p
12470 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12471 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12473 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12474 XEXP (XEXP (op0, 0), 1));
12475 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12476 continue;
12479 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12480 zero and X is a comparison and C1 and C2 describe only bits set
12481 in STORE_FLAG_VALUE, we can compare with X. */
12482 if (const_op == 0 && equality_comparison_p
12483 && mode_width <= HOST_BITS_PER_WIDE_INT
12484 && CONST_INT_P (XEXP (op0, 1))
12485 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12486 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12487 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12488 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12490 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12491 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12492 if ((~STORE_FLAG_VALUE & mask) == 0
12493 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12494 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12495 && COMPARISON_P (tem))))
12497 op0 = XEXP (XEXP (op0, 0), 0);
12498 continue;
12502 /* If we are doing an equality comparison of an AND of a bit equal
12503 to the sign bit, replace this with a LT or GE comparison of
12504 the underlying value. */
12505 if (equality_comparison_p
12506 && const_op == 0
12507 && CONST_INT_P (XEXP (op0, 1))
12508 && mode_width <= HOST_BITS_PER_WIDE_INT
12509 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12510 == HOST_WIDE_INT_1U << (mode_width - 1)))
12512 op0 = XEXP (op0, 0);
12513 code = (code == EQ ? GE : LT);
12514 continue;
12517 /* If this AND operation is really a ZERO_EXTEND from a narrower
12518 mode, the constant fits within that mode, and this is either an
12519 equality or unsigned comparison, try to do this comparison in
12520 the narrower mode.
12522 Note that in:
12524 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12525 -> (ne:DI (reg:SI 4) (const_int 0))
12527 unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12528 known to hold a value of the required mode the
12529 transformation is invalid. */
12530 if ((equality_comparison_p || unsigned_comparison_p)
12531 && CONST_INT_P (XEXP (op0, 1))
12532 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12533 & GET_MODE_MASK (mode))
12534 + 1)) >= 0
12535 && const_op >> i == 0
12536 && int_mode_for_size (i, 1).exists (&tmode))
12538 op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12539 continue;
12542 /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12543 fits in both M1 and M2 and the SUBREG is either paradoxical
12544 or represents the low part, permute the SUBREG and the AND
12545 and try again. */
12546 if (GET_CODE (XEXP (op0, 0)) == SUBREG
12547 && CONST_INT_P (XEXP (op0, 1)))
12549 unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12550 /* Require an integral mode, to avoid creating something like
12551 (AND:SF ...). */
12552 if ((is_a <scalar_int_mode>
12553 (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12554 /* It is unsafe to commute the AND into the SUBREG if the
12555 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12556 not defined. As originally written the upper bits
12557 have a defined value due to the AND operation.
12558 However, if we commute the AND inside the SUBREG then
12559 they no longer have defined values and the meaning of
12560 the code has been changed.
12561 Also C1 should not change value in the smaller mode,
12562 see PR67028 (a positive C1 can become negative in the
12563 smaller mode, so that the AND does no longer mask the
12564 upper bits). */
12565 && ((WORD_REGISTER_OPERATIONS
12566 && mode_width > GET_MODE_PRECISION (tmode)
12567 && mode_width <= BITS_PER_WORD
12568 && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12569 || (mode_width <= GET_MODE_PRECISION (tmode)
12570 && subreg_lowpart_p (XEXP (op0, 0))))
12571 && mode_width <= HOST_BITS_PER_WIDE_INT
12572 && HWI_COMPUTABLE_MODE_P (tmode)
12573 && (c1 & ~mask) == 0
12574 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12575 && c1 != mask
12576 && c1 != GET_MODE_MASK (tmode))
12578 op0 = simplify_gen_binary (AND, tmode,
12579 SUBREG_REG (XEXP (op0, 0)),
12580 gen_int_mode (c1, tmode));
12581 op0 = gen_lowpart (mode, op0);
12582 continue;
12586 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12587 if (const_op == 0 && equality_comparison_p
12588 && XEXP (op0, 1) == const1_rtx
12589 && GET_CODE (XEXP (op0, 0)) == NOT)
12591 op0 = simplify_and_const_int (NULL_RTX, mode,
12592 XEXP (XEXP (op0, 0), 0), 1);
12593 code = (code == NE ? EQ : NE);
12594 continue;
12597 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12598 (eq (and (lshiftrt X) 1) 0).
12599 Also handle the case where (not X) is expressed using xor. */
12600 if (const_op == 0 && equality_comparison_p
12601 && XEXP (op0, 1) == const1_rtx
12602 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12604 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12605 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12607 if (GET_CODE (shift_op) == NOT
12608 || (GET_CODE (shift_op) == XOR
12609 && CONST_INT_P (XEXP (shift_op, 1))
12610 && CONST_INT_P (shift_count)
12611 && HWI_COMPUTABLE_MODE_P (mode)
12612 && (UINTVAL (XEXP (shift_op, 1))
12613 == HOST_WIDE_INT_1U
12614 << INTVAL (shift_count))))
12617 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12618 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12619 code = (code == NE ? EQ : NE);
12620 continue;
12623 break;
12625 case ASHIFT:
12626 /* If we have (compare (ashift FOO N) (const_int C)) and
12627 the high order N bits of FOO (N+1 if an inequality comparison)
12628 are known to be zero, we can do this by comparing FOO with C
12629 shifted right N bits so long as the low-order N bits of C are
12630 zero. */
12631 if (CONST_INT_P (XEXP (op0, 1))
12632 && INTVAL (XEXP (op0, 1)) >= 0
12633 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12634 < HOST_BITS_PER_WIDE_INT)
12635 && (((unsigned HOST_WIDE_INT) const_op
12636 & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12637 - 1)) == 0)
12638 && mode_width <= HOST_BITS_PER_WIDE_INT
12639 && (nonzero_bits (XEXP (op0, 0), mode)
12640 & ~(mask >> (INTVAL (XEXP (op0, 1))
12641 + ! equality_comparison_p))) == 0)
12643 /* We must perform a logical shift, not an arithmetic one,
12644 as we want the top N bits of C to be zero. */
12645 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12647 temp >>= INTVAL (XEXP (op0, 1));
12648 op1 = gen_int_mode (temp, mode);
12649 op0 = XEXP (op0, 0);
12650 continue;
12653 /* If we are doing a sign bit comparison, it means we are testing
12654 a particular bit. Convert it to the appropriate AND. */
12655 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12656 && mode_width <= HOST_BITS_PER_WIDE_INT)
12658 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12659 (HOST_WIDE_INT_1U
12660 << (mode_width - 1
12661 - INTVAL (XEXP (op0, 1)))));
12662 code = (code == LT ? NE : EQ);
12663 continue;
12666 /* If this an equality comparison with zero and we are shifting
12667 the low bit to the sign bit, we can convert this to an AND of the
12668 low-order bit. */
12669 if (const_op == 0 && equality_comparison_p
12670 && CONST_INT_P (XEXP (op0, 1))
12671 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12673 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12674 continue;
12676 break;
12678 case ASHIFTRT:
12679 /* If this is an equality comparison with zero, we can do this
12680 as a logical shift, which might be much simpler. */
12681 if (equality_comparison_p && const_op == 0
12682 && CONST_INT_P (XEXP (op0, 1)))
12684 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12685 XEXP (op0, 0),
12686 INTVAL (XEXP (op0, 1)));
12687 continue;
12690 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12691 do the comparison in a narrower mode. */
12692 if (! unsigned_comparison_p
12693 && CONST_INT_P (XEXP (op0, 1))
12694 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12695 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12696 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12697 .exists (&tmode))
12698 && (((unsigned HOST_WIDE_INT) const_op
12699 + (GET_MODE_MASK (tmode) >> 1) + 1)
12700 <= GET_MODE_MASK (tmode)))
12702 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12703 continue;
12706 /* Likewise if OP0 is a PLUS of a sign extension with a
12707 constant, which is usually represented with the PLUS
12708 between the shifts. */
12709 if (! unsigned_comparison_p
12710 && CONST_INT_P (XEXP (op0, 1))
12711 && GET_CODE (XEXP (op0, 0)) == PLUS
12712 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12713 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12714 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12715 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12716 .exists (&tmode))
12717 && (((unsigned HOST_WIDE_INT) const_op
12718 + (GET_MODE_MASK (tmode) >> 1) + 1)
12719 <= GET_MODE_MASK (tmode)))
12721 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12722 rtx add_const = XEXP (XEXP (op0, 0), 1);
12723 rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12724 add_const, XEXP (op0, 1));
12726 op0 = simplify_gen_binary (PLUS, tmode,
12727 gen_lowpart (tmode, inner),
12728 new_const);
12729 continue;
12732 /* FALLTHROUGH */
12733 case LSHIFTRT:
12734 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12735 the low order N bits of FOO are known to be zero, we can do this
12736 by comparing FOO with C shifted left N bits so long as no
12737 overflow occurs. Even if the low order N bits of FOO aren't known
12738 to be zero, if the comparison is >= or < we can use the same
12739 optimization and for > or <= by setting all the low
12740 order N bits in the comparison constant. */
12741 if (CONST_INT_P (XEXP (op0, 1))
12742 && INTVAL (XEXP (op0, 1)) > 0
12743 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12744 && mode_width <= HOST_BITS_PER_WIDE_INT
12745 && (((unsigned HOST_WIDE_INT) const_op
12746 + (GET_CODE (op0) != LSHIFTRT
12747 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12748 + 1)
12749 : 0))
12750 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12752 unsigned HOST_WIDE_INT low_bits
12753 = (nonzero_bits (XEXP (op0, 0), mode)
12754 & ((HOST_WIDE_INT_1U
12755 << INTVAL (XEXP (op0, 1))) - 1));
12756 if (low_bits == 0 || !equality_comparison_p)
12758 /* If the shift was logical, then we must make the condition
12759 unsigned. */
12760 if (GET_CODE (op0) == LSHIFTRT)
12761 code = unsigned_condition (code);
12763 const_op = (unsigned HOST_WIDE_INT) const_op
12764 << INTVAL (XEXP (op0, 1));
12765 if (low_bits != 0
12766 && (code == GT || code == GTU
12767 || code == LE || code == LEU))
12768 const_op
12769 |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12770 op1 = GEN_INT (const_op);
12771 op0 = XEXP (op0, 0);
12772 continue;
12776 /* If we are using this shift to extract just the sign bit, we
12777 can replace this with an LT or GE comparison. */
12778 if (const_op == 0
12779 && (equality_comparison_p || sign_bit_comparison_p)
12780 && CONST_INT_P (XEXP (op0, 1))
12781 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12783 op0 = XEXP (op0, 0);
12784 code = (code == NE || code == GT ? LT : GE);
12785 continue;
12787 break;
12789 default:
12790 break;
12793 break;
12796 /* Now make any compound operations involved in this comparison. Then,
12797 check for an outmost SUBREG on OP0 that is not doing anything or is
12798 paradoxical. The latter transformation must only be performed when
12799 it is known that the "extra" bits will be the same in op0 and op1 or
12800 that they don't matter. There are three cases to consider:
12802 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12803 care bits and we can assume they have any convenient value. So
12804 making the transformation is safe.
12806 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12807 In this case the upper bits of op0 are undefined. We should not make
12808 the simplification in that case as we do not know the contents of
12809 those bits.
12811 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12812 In that case we know those bits are zeros or ones. We must also be
12813 sure that they are the same as the upper bits of op1.
12815 We can never remove a SUBREG for a non-equality comparison because
12816 the sign bit is in a different place in the underlying object. */
12818 rtx_code op0_mco_code = SET;
12819 if (op1 == const0_rtx)
12820 op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12822 op0 = make_compound_operation (op0, op0_mco_code);
12823 op1 = make_compound_operation (op1, SET);
12825 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12826 && is_int_mode (GET_MODE (op0), &mode)
12827 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12828 && (code == NE || code == EQ))
12830 if (paradoxical_subreg_p (op0))
12832 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12833 implemented. */
12834 if (REG_P (SUBREG_REG (op0)))
12836 op0 = SUBREG_REG (op0);
12837 op1 = gen_lowpart (inner_mode, op1);
12840 else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12841 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12842 & ~GET_MODE_MASK (mode)) == 0)
12844 tem = gen_lowpart (inner_mode, op1);
12846 if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
12847 op0 = SUBREG_REG (op0), op1 = tem;
12851 /* We now do the opposite procedure: Some machines don't have compare
12852 insns in all modes. If OP0's mode is an integer mode smaller than a
12853 word and we can't do a compare in that mode, see if there is a larger
12854 mode for which we can do the compare. There are a number of cases in
12855 which we can use the wider mode. */
12857 if (is_int_mode (GET_MODE (op0), &mode)
12858 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12859 && ! have_insn_for (COMPARE, mode))
12860 FOR_EACH_WIDER_MODE (tmode_iter, mode)
12862 tmode = tmode_iter.require ();
12863 if (!HWI_COMPUTABLE_MODE_P (tmode))
12864 break;
12865 if (have_insn_for (COMPARE, tmode))
12867 int zero_extended;
12869 /* If this is a test for negative, we can make an explicit
12870 test of the sign bit. Test this first so we can use
12871 a paradoxical subreg to extend OP0. */
12873 if (op1 == const0_rtx && (code == LT || code == GE)
12874 && HWI_COMPUTABLE_MODE_P (mode))
12876 unsigned HOST_WIDE_INT sign
12877 = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
12878 op0 = simplify_gen_binary (AND, tmode,
12879 gen_lowpart (tmode, op0),
12880 gen_int_mode (sign, tmode));
12881 code = (code == LT) ? NE : EQ;
12882 break;
12885 /* If the only nonzero bits in OP0 and OP1 are those in the
12886 narrower mode and this is an equality or unsigned comparison,
12887 we can use the wider mode. Similarly for sign-extended
12888 values, in which case it is true for all comparisons. */
12889 zero_extended = ((code == EQ || code == NE
12890 || code == GEU || code == GTU
12891 || code == LEU || code == LTU)
12892 && (nonzero_bits (op0, tmode)
12893 & ~GET_MODE_MASK (mode)) == 0
12894 && ((CONST_INT_P (op1)
12895 || (nonzero_bits (op1, tmode)
12896 & ~GET_MODE_MASK (mode)) == 0)));
12898 if (zero_extended
12899 || ((num_sign_bit_copies (op0, tmode)
12900 > (unsigned int) (GET_MODE_PRECISION (tmode)
12901 - GET_MODE_PRECISION (mode)))
12902 && (num_sign_bit_copies (op1, tmode)
12903 > (unsigned int) (GET_MODE_PRECISION (tmode)
12904 - GET_MODE_PRECISION (mode)))))
12906 /* If OP0 is an AND and we don't have an AND in MODE either,
12907 make a new AND in the proper mode. */
12908 if (GET_CODE (op0) == AND
12909 && !have_insn_for (AND, mode))
12910 op0 = simplify_gen_binary (AND, tmode,
12911 gen_lowpart (tmode,
12912 XEXP (op0, 0)),
12913 gen_lowpart (tmode,
12914 XEXP (op0, 1)));
12915 else
12917 if (zero_extended)
12919 op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
12920 op0, mode);
12921 op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
12922 op1, mode);
12924 else
12926 op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
12927 op0, mode);
12928 op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
12929 op1, mode);
12931 break;
12937 /* We may have changed the comparison operands. Re-canonicalize. */
12938 if (swap_commutative_operands_p (op0, op1))
12940 std::swap (op0, op1);
12941 code = swap_condition (code);
12944 /* If this machine only supports a subset of valid comparisons, see if we
12945 can convert an unsupported one into a supported one. */
12946 target_canonicalize_comparison (&code, &op0, &op1, 0);
12948 *pop0 = op0;
12949 *pop1 = op1;
12951 return code;
12954 /* Utility function for record_value_for_reg. Count number of
12955 rtxs in X. */
12956 static int
12957 count_rtxs (rtx x)
12959 enum rtx_code code = GET_CODE (x);
12960 const char *fmt;
12961 int i, j, ret = 1;
12963 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12964 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12966 rtx x0 = XEXP (x, 0);
12967 rtx x1 = XEXP (x, 1);
12969 if (x0 == x1)
12970 return 1 + 2 * count_rtxs (x0);
12972 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12973 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12974 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12975 return 2 + 2 * count_rtxs (x0)
12976 + count_rtxs (x == XEXP (x1, 0)
12977 ? XEXP (x1, 1) : XEXP (x1, 0));
12979 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12980 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12981 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12982 return 2 + 2 * count_rtxs (x1)
12983 + count_rtxs (x == XEXP (x0, 0)
12984 ? XEXP (x0, 1) : XEXP (x0, 0));
12987 fmt = GET_RTX_FORMAT (code);
12988 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12989 if (fmt[i] == 'e')
12990 ret += count_rtxs (XEXP (x, i));
12991 else if (fmt[i] == 'E')
12992 for (j = 0; j < XVECLEN (x, i); j++)
12993 ret += count_rtxs (XVECEXP (x, i, j));
12995 return ret;
12998 /* Utility function for following routine. Called when X is part of a value
12999 being stored into last_set_value. Sets last_set_table_tick
13000 for each register mentioned. Similar to mention_regs in cse.c */
13002 static void
13003 update_table_tick (rtx x)
13005 enum rtx_code code = GET_CODE (x);
13006 const char *fmt = GET_RTX_FORMAT (code);
13007 int i, j;
13009 if (code == REG)
13011 unsigned int regno = REGNO (x);
13012 unsigned int endregno = END_REGNO (x);
13013 unsigned int r;
13015 for (r = regno; r < endregno; r++)
13017 reg_stat_type *rsp = &reg_stat[r];
13018 rsp->last_set_table_tick = label_tick;
13021 return;
13024 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13025 if (fmt[i] == 'e')
13027 /* Check for identical subexpressions. If x contains
13028 identical subexpression we only have to traverse one of
13029 them. */
13030 if (i == 0 && ARITHMETIC_P (x))
13032 /* Note that at this point x1 has already been
13033 processed. */
13034 rtx x0 = XEXP (x, 0);
13035 rtx x1 = XEXP (x, 1);
13037 /* If x0 and x1 are identical then there is no need to
13038 process x0. */
13039 if (x0 == x1)
13040 break;
13042 /* If x0 is identical to a subexpression of x1 then while
13043 processing x1, x0 has already been processed. Thus we
13044 are done with x. */
13045 if (ARITHMETIC_P (x1)
13046 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13047 break;
13049 /* If x1 is identical to a subexpression of x0 then we
13050 still have to process the rest of x0. */
13051 if (ARITHMETIC_P (x0)
13052 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13054 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13055 break;
13059 update_table_tick (XEXP (x, i));
13061 else if (fmt[i] == 'E')
13062 for (j = 0; j < XVECLEN (x, i); j++)
13063 update_table_tick (XVECEXP (x, i, j));
13066 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13067 are saying that the register is clobbered and we no longer know its
13068 value. If INSN is zero, don't update reg_stat[].last_set; this is
13069 only permitted with VALUE also zero and is used to invalidate the
13070 register. */
13072 static void
13073 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13075 unsigned int regno = REGNO (reg);
13076 unsigned int endregno = END_REGNO (reg);
13077 unsigned int i;
13078 reg_stat_type *rsp;
13080 /* If VALUE contains REG and we have a previous value for REG, substitute
13081 the previous value. */
13082 if (value && insn && reg_overlap_mentioned_p (reg, value))
13084 rtx tem;
13086 /* Set things up so get_last_value is allowed to see anything set up to
13087 our insn. */
13088 subst_low_luid = DF_INSN_LUID (insn);
13089 tem = get_last_value (reg);
13091 /* If TEM is simply a binary operation with two CLOBBERs as operands,
13092 it isn't going to be useful and will take a lot of time to process,
13093 so just use the CLOBBER. */
13095 if (tem)
13097 if (ARITHMETIC_P (tem)
13098 && GET_CODE (XEXP (tem, 0)) == CLOBBER
13099 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13100 tem = XEXP (tem, 0);
13101 else if (count_occurrences (value, reg, 1) >= 2)
13103 /* If there are two or more occurrences of REG in VALUE,
13104 prevent the value from growing too much. */
13105 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
13106 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13109 value = replace_rtx (copy_rtx (value), reg, tem);
13113 /* For each register modified, show we don't know its value, that
13114 we don't know about its bitwise content, that its value has been
13115 updated, and that we don't know the location of the death of the
13116 register. */
13117 for (i = regno; i < endregno; i++)
13119 rsp = &reg_stat[i];
13121 if (insn)
13122 rsp->last_set = insn;
13124 rsp->last_set_value = 0;
13125 rsp->last_set_mode = VOIDmode;
13126 rsp->last_set_nonzero_bits = 0;
13127 rsp->last_set_sign_bit_copies = 0;
13128 rsp->last_death = 0;
13129 rsp->truncated_to_mode = VOIDmode;
13132 /* Mark registers that are being referenced in this value. */
13133 if (value)
13134 update_table_tick (value);
13136 /* Now update the status of each register being set.
13137 If someone is using this register in this block, set this register
13138 to invalid since we will get confused between the two lives in this
13139 basic block. This makes using this register always invalid. In cse, we
13140 scan the table to invalidate all entries using this register, but this
13141 is too much work for us. */
13143 for (i = regno; i < endregno; i++)
13145 rsp = &reg_stat[i];
13146 rsp->last_set_label = label_tick;
13147 if (!insn
13148 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13149 rsp->last_set_invalid = 1;
13150 else
13151 rsp->last_set_invalid = 0;
13154 /* The value being assigned might refer to X (like in "x++;"). In that
13155 case, we must replace it with (clobber (const_int 0)) to prevent
13156 infinite loops. */
13157 rsp = &reg_stat[regno];
13158 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
13160 value = copy_rtx (value);
13161 if (!get_last_value_validate (&value, insn, label_tick, 1))
13162 value = 0;
13165 /* For the main register being modified, update the value, the mode, the
13166 nonzero bits, and the number of sign bit copies. */
13168 rsp->last_set_value = value;
13170 if (value)
13172 machine_mode mode = GET_MODE (reg);
13173 subst_low_luid = DF_INSN_LUID (insn);
13174 rsp->last_set_mode = mode;
13175 if (GET_MODE_CLASS (mode) == MODE_INT
13176 && HWI_COMPUTABLE_MODE_P (mode))
13177 mode = nonzero_bits_mode;
13178 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13179 rsp->last_set_sign_bit_copies
13180 = num_sign_bit_copies (value, GET_MODE (reg));
13184 /* Called via note_stores from record_dead_and_set_regs to handle one
13185 SET or CLOBBER in an insn. DATA is the instruction in which the
13186 set is occurring. */
13188 static void
13189 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13191 rtx_insn *record_dead_insn = (rtx_insn *) data;
13193 if (GET_CODE (dest) == SUBREG)
13194 dest = SUBREG_REG (dest);
13196 if (!record_dead_insn)
13198 if (REG_P (dest))
13199 record_value_for_reg (dest, NULL, NULL_RTX);
13200 return;
13203 if (REG_P (dest))
13205 /* If we are setting the whole register, we know its value. Otherwise
13206 show that we don't know the value. We can handle SUBREG in
13207 some cases. */
13208 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13209 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13210 else if (GET_CODE (setter) == SET
13211 && GET_CODE (SET_DEST (setter)) == SUBREG
13212 && SUBREG_REG (SET_DEST (setter)) == dest
13213 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
13214 && subreg_lowpart_p (SET_DEST (setter)))
13215 record_value_for_reg (dest, record_dead_insn,
13216 gen_lowpart (GET_MODE (dest),
13217 SET_SRC (setter)));
13218 else
13219 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13221 else if (MEM_P (dest)
13222 /* Ignore pushes, they clobber nothing. */
13223 && ! push_operand (dest, GET_MODE (dest)))
13224 mem_last_set = DF_INSN_LUID (record_dead_insn);
13227 /* Update the records of when each REG was most recently set or killed
13228 for the things done by INSN. This is the last thing done in processing
13229 INSN in the combiner loop.
13231 We update reg_stat[], in particular fields last_set, last_set_value,
13232 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13233 last_death, and also the similar information mem_last_set (which insn
13234 most recently modified memory) and last_call_luid (which insn was the
13235 most recent subroutine call). */
13237 static void
13238 record_dead_and_set_regs (rtx_insn *insn)
13240 rtx link;
13241 unsigned int i;
13243 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13245 if (REG_NOTE_KIND (link) == REG_DEAD
13246 && REG_P (XEXP (link, 0)))
13248 unsigned int regno = REGNO (XEXP (link, 0));
13249 unsigned int endregno = END_REGNO (XEXP (link, 0));
13251 for (i = regno; i < endregno; i++)
13253 reg_stat_type *rsp;
13255 rsp = &reg_stat[i];
13256 rsp->last_death = insn;
13259 else if (REG_NOTE_KIND (link) == REG_INC)
13260 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13263 if (CALL_P (insn))
13265 hard_reg_set_iterator hrsi;
13266 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
13268 reg_stat_type *rsp;
13270 rsp = &reg_stat[i];
13271 rsp->last_set_invalid = 1;
13272 rsp->last_set = insn;
13273 rsp->last_set_value = 0;
13274 rsp->last_set_mode = VOIDmode;
13275 rsp->last_set_nonzero_bits = 0;
13276 rsp->last_set_sign_bit_copies = 0;
13277 rsp->last_death = 0;
13278 rsp->truncated_to_mode = VOIDmode;
13281 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13283 /* We can't combine into a call pattern. Remember, though, that
13284 the return value register is set at this LUID. We could
13285 still replace a register with the return value from the
13286 wrong subroutine call! */
13287 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
13289 else
13290 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
13293 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13294 register present in the SUBREG, so for each such SUBREG go back and
13295 adjust nonzero and sign bit information of the registers that are
13296 known to have some zero/sign bits set.
13298 This is needed because when combine blows the SUBREGs away, the
13299 information on zero/sign bits is lost and further combines can be
13300 missed because of that. */
13302 static void
13303 record_promoted_value (rtx_insn *insn, rtx subreg)
13305 struct insn_link *links;
13306 rtx set;
13307 unsigned int regno = REGNO (SUBREG_REG (subreg));
13308 machine_mode mode = GET_MODE (subreg);
13310 if (!HWI_COMPUTABLE_MODE_P (mode))
13311 return;
13313 for (links = LOG_LINKS (insn); links;)
13315 reg_stat_type *rsp;
13317 insn = links->insn;
13318 set = single_set (insn);
13320 if (! set || !REG_P (SET_DEST (set))
13321 || REGNO (SET_DEST (set)) != regno
13322 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13324 links = links->next;
13325 continue;
13328 rsp = &reg_stat[regno];
13329 if (rsp->last_set == insn)
13331 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13332 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13335 if (REG_P (SET_SRC (set)))
13337 regno = REGNO (SET_SRC (set));
13338 links = LOG_LINKS (insn);
13340 else
13341 break;
13345 /* Check if X, a register, is known to contain a value already
13346 truncated to MODE. In this case we can use a subreg to refer to
13347 the truncated value even though in the generic case we would need
13348 an explicit truncation. */
13350 static bool
13351 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13353 reg_stat_type *rsp = &reg_stat[REGNO (x)];
13354 machine_mode truncated = rsp->truncated_to_mode;
13356 if (truncated == 0
13357 || rsp->truncation_label < label_tick_ebb_start)
13358 return false;
13359 if (!partial_subreg_p (mode, truncated))
13360 return true;
13361 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13362 return true;
13363 return false;
13366 /* If X is a hard reg or a subreg record the mode that the register is
13367 accessed in. For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13368 able to turn a truncate into a subreg using this information. Return true
13369 if traversing X is complete. */
13371 static bool
13372 record_truncated_value (rtx x)
13374 machine_mode truncated_mode;
13375 reg_stat_type *rsp;
13377 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13379 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13380 truncated_mode = GET_MODE (x);
13382 if (!partial_subreg_p (truncated_mode, original_mode))
13383 return true;
13385 truncated_mode = GET_MODE (x);
13386 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13387 return true;
13389 x = SUBREG_REG (x);
13391 /* ??? For hard-regs we now record everything. We might be able to
13392 optimize this using last_set_mode. */
13393 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13394 truncated_mode = GET_MODE (x);
13395 else
13396 return false;
13398 rsp = &reg_stat[REGNO (x)];
13399 if (rsp->truncated_to_mode == 0
13400 || rsp->truncation_label < label_tick_ebb_start
13401 || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13403 rsp->truncated_to_mode = truncated_mode;
13404 rsp->truncation_label = label_tick;
13407 return true;
13410 /* Callback for note_uses. Find hardregs and subregs of pseudos and
13411 the modes they are used in. This can help truning TRUNCATEs into
13412 SUBREGs. */
13414 static void
13415 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13417 subrtx_var_iterator::array_type array;
13418 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13419 if (record_truncated_value (*iter))
13420 iter.skip_subrtxes ();
13423 /* Scan X for promoted SUBREGs. For each one found,
13424 note what it implies to the registers used in it. */
13426 static void
13427 check_promoted_subreg (rtx_insn *insn, rtx x)
13429 if (GET_CODE (x) == SUBREG
13430 && SUBREG_PROMOTED_VAR_P (x)
13431 && REG_P (SUBREG_REG (x)))
13432 record_promoted_value (insn, x);
13433 else
13435 const char *format = GET_RTX_FORMAT (GET_CODE (x));
13436 int i, j;
13438 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13439 switch (format[i])
13441 case 'e':
13442 check_promoted_subreg (insn, XEXP (x, i));
13443 break;
13444 case 'V':
13445 case 'E':
13446 if (XVEC (x, i) != 0)
13447 for (j = 0; j < XVECLEN (x, i); j++)
13448 check_promoted_subreg (insn, XVECEXP (x, i, j));
13449 break;
13454 /* Verify that all the registers and memory references mentioned in *LOC are
13455 still valid. *LOC was part of a value set in INSN when label_tick was
13456 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
13457 the invalid references with (clobber (const_int 0)) and return 1. This
13458 replacement is useful because we often can get useful information about
13459 the form of a value (e.g., if it was produced by a shift that always
13460 produces -1 or 0) even though we don't know exactly what registers it
13461 was produced from. */
13463 static int
13464 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
13466 rtx x = *loc;
13467 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13468 int len = GET_RTX_LENGTH (GET_CODE (x));
13469 int i, j;
13471 if (REG_P (x))
13473 unsigned int regno = REGNO (x);
13474 unsigned int endregno = END_REGNO (x);
13475 unsigned int j;
13477 for (j = regno; j < endregno; j++)
13479 reg_stat_type *rsp = &reg_stat[j];
13480 if (rsp->last_set_invalid
13481 /* If this is a pseudo-register that was only set once and not
13482 live at the beginning of the function, it is always valid. */
13483 || (! (regno >= FIRST_PSEUDO_REGISTER
13484 && regno < reg_n_sets_max
13485 && REG_N_SETS (regno) == 1
13486 && (!REGNO_REG_SET_P
13487 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13488 regno)))
13489 && rsp->last_set_label > tick))
13491 if (replace)
13492 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13493 return replace;
13497 return 1;
13499 /* If this is a memory reference, make sure that there were no stores after
13500 it that might have clobbered the value. We don't have alias info, so we
13501 assume any store invalidates it. Moreover, we only have local UIDs, so
13502 we also assume that there were stores in the intervening basic blocks. */
13503 else if (MEM_P (x) && !MEM_READONLY_P (x)
13504 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13506 if (replace)
13507 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13508 return replace;
13511 for (i = 0; i < len; i++)
13513 if (fmt[i] == 'e')
13515 /* Check for identical subexpressions. If x contains
13516 identical subexpression we only have to traverse one of
13517 them. */
13518 if (i == 1 && ARITHMETIC_P (x))
13520 /* Note that at this point x0 has already been checked
13521 and found valid. */
13522 rtx x0 = XEXP (x, 0);
13523 rtx x1 = XEXP (x, 1);
13525 /* If x0 and x1 are identical then x is also valid. */
13526 if (x0 == x1)
13527 return 1;
13529 /* If x1 is identical to a subexpression of x0 then
13530 while checking x0, x1 has already been checked. Thus
13531 it is valid and so as x. */
13532 if (ARITHMETIC_P (x0)
13533 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13534 return 1;
13536 /* If x0 is identical to a subexpression of x1 then x is
13537 valid iff the rest of x1 is valid. */
13538 if (ARITHMETIC_P (x1)
13539 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13540 return
13541 get_last_value_validate (&XEXP (x1,
13542 x0 == XEXP (x1, 0) ? 1 : 0),
13543 insn, tick, replace);
13546 if (get_last_value_validate (&XEXP (x, i), insn, tick,
13547 replace) == 0)
13548 return 0;
13550 else if (fmt[i] == 'E')
13551 for (j = 0; j < XVECLEN (x, i); j++)
13552 if (get_last_value_validate (&XVECEXP (x, i, j),
13553 insn, tick, replace) == 0)
13554 return 0;
13557 /* If we haven't found a reason for it to be invalid, it is valid. */
13558 return 1;
13561 /* Get the last value assigned to X, if known. Some registers
13562 in the value may be replaced with (clobber (const_int 0)) if their value
13563 is known longer known reliably. */
13565 static rtx
13566 get_last_value (const_rtx x)
13568 unsigned int regno;
13569 rtx value;
13570 reg_stat_type *rsp;
13572 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13573 then convert it to the desired mode. If this is a paradoxical SUBREG,
13574 we cannot predict what values the "extra" bits might have. */
13575 if (GET_CODE (x) == SUBREG
13576 && subreg_lowpart_p (x)
13577 && !paradoxical_subreg_p (x)
13578 && (value = get_last_value (SUBREG_REG (x))) != 0)
13579 return gen_lowpart (GET_MODE (x), value);
13581 if (!REG_P (x))
13582 return 0;
13584 regno = REGNO (x);
13585 rsp = &reg_stat[regno];
13586 value = rsp->last_set_value;
13588 /* If we don't have a value, or if it isn't for this basic block and
13589 it's either a hard register, set more than once, or it's a live
13590 at the beginning of the function, return 0.
13592 Because if it's not live at the beginning of the function then the reg
13593 is always set before being used (is never used without being set).
13594 And, if it's set only once, and it's always set before use, then all
13595 uses must have the same last value, even if it's not from this basic
13596 block. */
13598 if (value == 0
13599 || (rsp->last_set_label < label_tick_ebb_start
13600 && (regno < FIRST_PSEUDO_REGISTER
13601 || regno >= reg_n_sets_max
13602 || REG_N_SETS (regno) != 1
13603 || REGNO_REG_SET_P
13604 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13605 return 0;
13607 /* If the value was set in a later insn than the ones we are processing,
13608 we can't use it even if the register was only set once. */
13609 if (rsp->last_set_label == label_tick
13610 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13611 return 0;
13613 /* If fewer bits were set than what we are asked for now, we cannot use
13614 the value. */
13615 if (GET_MODE_PRECISION (rsp->last_set_mode)
13616 < GET_MODE_PRECISION (GET_MODE (x)))
13617 return 0;
13619 /* If the value has all its registers valid, return it. */
13620 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13621 return value;
13623 /* Otherwise, make a copy and replace any invalid register with
13624 (clobber (const_int 0)). If that fails for some reason, return 0. */
13626 value = copy_rtx (value);
13627 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13628 return value;
13630 return 0;
13633 /* Return nonzero if expression X refers to a REG or to memory
13634 that is set in an instruction more recent than FROM_LUID. */
13636 static int
13637 use_crosses_set_p (const_rtx x, int from_luid)
13639 const char *fmt;
13640 int i;
13641 enum rtx_code code = GET_CODE (x);
13643 if (code == REG)
13645 unsigned int regno = REGNO (x);
13646 unsigned endreg = END_REGNO (x);
13648 #ifdef PUSH_ROUNDING
13649 /* Don't allow uses of the stack pointer to be moved,
13650 because we don't know whether the move crosses a push insn. */
13651 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13652 return 1;
13653 #endif
13654 for (; regno < endreg; regno++)
13656 reg_stat_type *rsp = &reg_stat[regno];
13657 if (rsp->last_set
13658 && rsp->last_set_label == label_tick
13659 && DF_INSN_LUID (rsp->last_set) > from_luid)
13660 return 1;
13662 return 0;
13665 if (code == MEM && mem_last_set > from_luid)
13666 return 1;
13668 fmt = GET_RTX_FORMAT (code);
13670 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13672 if (fmt[i] == 'E')
13674 int j;
13675 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13676 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13677 return 1;
13679 else if (fmt[i] == 'e'
13680 && use_crosses_set_p (XEXP (x, i), from_luid))
13681 return 1;
13683 return 0;
13686 /* Define three variables used for communication between the following
13687 routines. */
13689 static unsigned int reg_dead_regno, reg_dead_endregno;
13690 static int reg_dead_flag;
13692 /* Function called via note_stores from reg_dead_at_p.
13694 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13695 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13697 static void
13698 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13700 unsigned int regno, endregno;
13702 if (!REG_P (dest))
13703 return;
13705 regno = REGNO (dest);
13706 endregno = END_REGNO (dest);
13707 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13708 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13711 /* Return nonzero if REG is known to be dead at INSN.
13713 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13714 referencing REG, it is dead. If we hit a SET referencing REG, it is
13715 live. Otherwise, see if it is live or dead at the start of the basic
13716 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13717 must be assumed to be always live. */
13719 static int
13720 reg_dead_at_p (rtx reg, rtx_insn *insn)
13722 basic_block block;
13723 unsigned int i;
13725 /* Set variables for reg_dead_at_p_1. */
13726 reg_dead_regno = REGNO (reg);
13727 reg_dead_endregno = END_REGNO (reg);
13729 reg_dead_flag = 0;
13731 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13732 we allow the machine description to decide whether use-and-clobber
13733 patterns are OK. */
13734 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13736 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13737 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13738 return 0;
13741 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13742 beginning of basic block. */
13743 block = BLOCK_FOR_INSN (insn);
13744 for (;;)
13746 if (INSN_P (insn))
13748 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13749 return 1;
13751 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13752 if (reg_dead_flag)
13753 return reg_dead_flag == 1 ? 1 : 0;
13755 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13756 return 1;
13759 if (insn == BB_HEAD (block))
13760 break;
13762 insn = PREV_INSN (insn);
13765 /* Look at live-in sets for the basic block that we were in. */
13766 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13767 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13768 return 0;
13770 return 1;
13773 /* Note hard registers in X that are used. */
13775 static void
13776 mark_used_regs_combine (rtx x)
13778 RTX_CODE code = GET_CODE (x);
13779 unsigned int regno;
13780 int i;
13782 switch (code)
13784 case LABEL_REF:
13785 case SYMBOL_REF:
13786 case CONST:
13787 CASE_CONST_ANY:
13788 case PC:
13789 case ADDR_VEC:
13790 case ADDR_DIFF_VEC:
13791 case ASM_INPUT:
13792 /* CC0 must die in the insn after it is set, so we don't need to take
13793 special note of it here. */
13794 case CC0:
13795 return;
13797 case CLOBBER:
13798 /* If we are clobbering a MEM, mark any hard registers inside the
13799 address as used. */
13800 if (MEM_P (XEXP (x, 0)))
13801 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13802 return;
13804 case REG:
13805 regno = REGNO (x);
13806 /* A hard reg in a wide mode may really be multiple registers.
13807 If so, mark all of them just like the first. */
13808 if (regno < FIRST_PSEUDO_REGISTER)
13810 /* None of this applies to the stack, frame or arg pointers. */
13811 if (regno == STACK_POINTER_REGNUM
13812 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13813 && regno == HARD_FRAME_POINTER_REGNUM)
13814 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13815 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13816 || regno == FRAME_POINTER_REGNUM)
13817 return;
13819 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13821 return;
13823 case SET:
13825 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13826 the address. */
13827 rtx testreg = SET_DEST (x);
13829 while (GET_CODE (testreg) == SUBREG
13830 || GET_CODE (testreg) == ZERO_EXTRACT
13831 || GET_CODE (testreg) == STRICT_LOW_PART)
13832 testreg = XEXP (testreg, 0);
13834 if (MEM_P (testreg))
13835 mark_used_regs_combine (XEXP (testreg, 0));
13837 mark_used_regs_combine (SET_SRC (x));
13839 return;
13841 default:
13842 break;
13845 /* Recursively scan the operands of this expression. */
13848 const char *fmt = GET_RTX_FORMAT (code);
13850 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13852 if (fmt[i] == 'e')
13853 mark_used_regs_combine (XEXP (x, i));
13854 else if (fmt[i] == 'E')
13856 int j;
13858 for (j = 0; j < XVECLEN (x, i); j++)
13859 mark_used_regs_combine (XVECEXP (x, i, j));
13865 /* Remove register number REGNO from the dead registers list of INSN.
13867 Return the note used to record the death, if there was one. */
13870 remove_death (unsigned int regno, rtx_insn *insn)
13872 rtx note = find_regno_note (insn, REG_DEAD, regno);
13874 if (note)
13875 remove_note (insn, note);
13877 return note;
13880 /* For each register (hardware or pseudo) used within expression X, if its
13881 death is in an instruction with luid between FROM_LUID (inclusive) and
13882 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13883 list headed by PNOTES.
13885 That said, don't move registers killed by maybe_kill_insn.
13887 This is done when X is being merged by combination into TO_INSN. These
13888 notes will then be distributed as needed. */
13890 static void
13891 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13892 rtx *pnotes)
13894 const char *fmt;
13895 int len, i;
13896 enum rtx_code code = GET_CODE (x);
13898 if (code == REG)
13900 unsigned int regno = REGNO (x);
13901 rtx_insn *where_dead = reg_stat[regno].last_death;
13903 /* Don't move the register if it gets killed in between from and to. */
13904 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13905 && ! reg_referenced_p (x, maybe_kill_insn))
13906 return;
13908 if (where_dead
13909 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13910 && DF_INSN_LUID (where_dead) >= from_luid
13911 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13913 rtx note = remove_death (regno, where_dead);
13915 /* It is possible for the call above to return 0. This can occur
13916 when last_death points to I2 or I1 that we combined with.
13917 In that case make a new note.
13919 We must also check for the case where X is a hard register
13920 and NOTE is a death note for a range of hard registers
13921 including X. In that case, we must put REG_DEAD notes for
13922 the remaining registers in place of NOTE. */
13924 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13925 && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
13927 unsigned int deadregno = REGNO (XEXP (note, 0));
13928 unsigned int deadend = END_REGNO (XEXP (note, 0));
13929 unsigned int ourend = END_REGNO (x);
13930 unsigned int i;
13932 for (i = deadregno; i < deadend; i++)
13933 if (i < regno || i >= ourend)
13934 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13937 /* If we didn't find any note, or if we found a REG_DEAD note that
13938 covers only part of the given reg, and we have a multi-reg hard
13939 register, then to be safe we must check for REG_DEAD notes
13940 for each register other than the first. They could have
13941 their own REG_DEAD notes lying around. */
13942 else if ((note == 0
13943 || (note != 0
13944 && partial_subreg_p (GET_MODE (XEXP (note, 0)),
13945 GET_MODE (x))))
13946 && regno < FIRST_PSEUDO_REGISTER
13947 && REG_NREGS (x) > 1)
13949 unsigned int ourend = END_REGNO (x);
13950 unsigned int i, offset;
13951 rtx oldnotes = 0;
13953 if (note)
13954 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
13955 else
13956 offset = 1;
13958 for (i = regno + offset; i < ourend; i++)
13959 move_deaths (regno_reg_rtx[i],
13960 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13963 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13965 XEXP (note, 1) = *pnotes;
13966 *pnotes = note;
13968 else
13969 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13972 return;
13975 else if (GET_CODE (x) == SET)
13977 rtx dest = SET_DEST (x);
13979 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13981 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13982 that accesses one word of a multi-word item, some
13983 piece of everything register in the expression is used by
13984 this insn, so remove any old death. */
13985 /* ??? So why do we test for equality of the sizes? */
13987 if (GET_CODE (dest) == ZERO_EXTRACT
13988 || GET_CODE (dest) == STRICT_LOW_PART
13989 || (GET_CODE (dest) == SUBREG
13990 && !read_modify_subreg_p (dest)))
13992 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13993 return;
13996 /* If this is some other SUBREG, we know it replaces the entire
13997 value, so use that as the destination. */
13998 if (GET_CODE (dest) == SUBREG)
13999 dest = SUBREG_REG (dest);
14001 /* If this is a MEM, adjust deaths of anything used in the address.
14002 For a REG (the only other possibility), the entire value is
14003 being replaced so the old value is not used in this insn. */
14005 if (MEM_P (dest))
14006 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14007 to_insn, pnotes);
14008 return;
14011 else if (GET_CODE (x) == CLOBBER)
14012 return;
14014 len = GET_RTX_LENGTH (code);
14015 fmt = GET_RTX_FORMAT (code);
14017 for (i = 0; i < len; i++)
14019 if (fmt[i] == 'E')
14021 int j;
14022 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14023 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14024 to_insn, pnotes);
14026 else if (fmt[i] == 'e')
14027 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14031 /* Return 1 if X is the target of a bit-field assignment in BODY, the
14032 pattern of an insn. X must be a REG. */
14034 static int
14035 reg_bitfield_target_p (rtx x, rtx body)
14037 int i;
14039 if (GET_CODE (body) == SET)
14041 rtx dest = SET_DEST (body);
14042 rtx target;
14043 unsigned int regno, tregno, endregno, endtregno;
14045 if (GET_CODE (dest) == ZERO_EXTRACT)
14046 target = XEXP (dest, 0);
14047 else if (GET_CODE (dest) == STRICT_LOW_PART)
14048 target = SUBREG_REG (XEXP (dest, 0));
14049 else
14050 return 0;
14052 if (GET_CODE (target) == SUBREG)
14053 target = SUBREG_REG (target);
14055 if (!REG_P (target))
14056 return 0;
14058 tregno = REGNO (target), regno = REGNO (x);
14059 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14060 return target == x;
14062 endtregno = end_hard_regno (GET_MODE (target), tregno);
14063 endregno = end_hard_regno (GET_MODE (x), regno);
14065 return endregno > tregno && regno < endtregno;
14068 else if (GET_CODE (body) == PARALLEL)
14069 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14070 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14071 return 1;
14073 return 0;
14076 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14077 as appropriate. I3 and I2 are the insns resulting from the combination
14078 insns including FROM (I2 may be zero).
14080 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14081 not need REG_DEAD notes because they are being substituted for. This
14082 saves searching in the most common cases.
14084 Each note in the list is either ignored or placed on some insns, depending
14085 on the type of note. */
14087 static void
14088 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14089 rtx elim_i2, rtx elim_i1, rtx elim_i0)
14091 rtx note, next_note;
14092 rtx tem_note;
14093 rtx_insn *tem_insn;
14095 for (note = notes; note; note = next_note)
14097 rtx_insn *place = 0, *place2 = 0;
14099 next_note = XEXP (note, 1);
14100 switch (REG_NOTE_KIND (note))
14102 case REG_BR_PROB:
14103 case REG_BR_PRED:
14104 /* Doesn't matter much where we put this, as long as it's somewhere.
14105 It is preferable to keep these notes on branches, which is most
14106 likely to be i3. */
14107 place = i3;
14108 break;
14110 case REG_NON_LOCAL_GOTO:
14111 if (JUMP_P (i3))
14112 place = i3;
14113 else
14115 gcc_assert (i2 && JUMP_P (i2));
14116 place = i2;
14118 break;
14120 case REG_EH_REGION:
14121 /* These notes must remain with the call or trapping instruction. */
14122 if (CALL_P (i3))
14123 place = i3;
14124 else if (i2 && CALL_P (i2))
14125 place = i2;
14126 else
14128 gcc_assert (cfun->can_throw_non_call_exceptions);
14129 if (may_trap_p (i3))
14130 place = i3;
14131 else if (i2 && may_trap_p (i2))
14132 place = i2;
14133 /* ??? Otherwise assume we've combined things such that we
14134 can now prove that the instructions can't trap. Drop the
14135 note in this case. */
14137 break;
14139 case REG_ARGS_SIZE:
14140 /* ??? How to distribute between i3-i1. Assume i3 contains the
14141 entire adjustment. Assert i3 contains at least some adjust. */
14142 if (!noop_move_p (i3))
14144 int old_size, args_size = INTVAL (XEXP (note, 0));
14145 /* fixup_args_size_notes looks at REG_NORETURN note,
14146 so ensure the note is placed there first. */
14147 if (CALL_P (i3))
14149 rtx *np;
14150 for (np = &next_note; *np; np = &XEXP (*np, 1))
14151 if (REG_NOTE_KIND (*np) == REG_NORETURN)
14153 rtx n = *np;
14154 *np = XEXP (n, 1);
14155 XEXP (n, 1) = REG_NOTES (i3);
14156 REG_NOTES (i3) = n;
14157 break;
14160 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14161 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14162 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14163 gcc_assert (old_size != args_size
14164 || (CALL_P (i3)
14165 && !ACCUMULATE_OUTGOING_ARGS
14166 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14168 break;
14170 case REG_NORETURN:
14171 case REG_SETJMP:
14172 case REG_TM:
14173 case REG_CALL_DECL:
14174 case REG_CALL_NOCF_CHECK:
14175 /* These notes must remain with the call. It should not be
14176 possible for both I2 and I3 to be a call. */
14177 if (CALL_P (i3))
14178 place = i3;
14179 else
14181 gcc_assert (i2 && CALL_P (i2));
14182 place = i2;
14184 break;
14186 case REG_UNUSED:
14187 /* Any clobbers for i3 may still exist, and so we must process
14188 REG_UNUSED notes from that insn.
14190 Any clobbers from i2 or i1 can only exist if they were added by
14191 recog_for_combine. In that case, recog_for_combine created the
14192 necessary REG_UNUSED notes. Trying to keep any original
14193 REG_UNUSED notes from these insns can cause incorrect output
14194 if it is for the same register as the original i3 dest.
14195 In that case, we will notice that the register is set in i3,
14196 and then add a REG_UNUSED note for the destination of i3, which
14197 is wrong. However, it is possible to have REG_UNUSED notes from
14198 i2 or i1 for register which were both used and clobbered, so
14199 we keep notes from i2 or i1 if they will turn into REG_DEAD
14200 notes. */
14202 /* If this register is set or clobbered in I3, put the note there
14203 unless there is one already. */
14204 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14206 if (from_insn != i3)
14207 break;
14209 if (! (REG_P (XEXP (note, 0))
14210 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14211 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14212 place = i3;
14214 /* Otherwise, if this register is used by I3, then this register
14215 now dies here, so we must put a REG_DEAD note here unless there
14216 is one already. */
14217 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14218 && ! (REG_P (XEXP (note, 0))
14219 ? find_regno_note (i3, REG_DEAD,
14220 REGNO (XEXP (note, 0)))
14221 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14223 PUT_REG_NOTE_KIND (note, REG_DEAD);
14224 place = i3;
14226 break;
14228 case REG_EQUAL:
14229 case REG_EQUIV:
14230 case REG_NOALIAS:
14231 /* These notes say something about results of an insn. We can
14232 only support them if they used to be on I3 in which case they
14233 remain on I3. Otherwise they are ignored.
14235 If the note refers to an expression that is not a constant, we
14236 must also ignore the note since we cannot tell whether the
14237 equivalence is still true. It might be possible to do
14238 slightly better than this (we only have a problem if I2DEST
14239 or I1DEST is present in the expression), but it doesn't
14240 seem worth the trouble. */
14242 if (from_insn == i3
14243 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14244 place = i3;
14245 break;
14247 case REG_INC:
14248 /* These notes say something about how a register is used. They must
14249 be present on any use of the register in I2 or I3. */
14250 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14251 place = i3;
14253 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14255 if (place)
14256 place2 = i2;
14257 else
14258 place = i2;
14260 break;
14262 case REG_LABEL_TARGET:
14263 case REG_LABEL_OPERAND:
14264 /* This can show up in several ways -- either directly in the
14265 pattern, or hidden off in the constant pool with (or without?)
14266 a REG_EQUAL note. */
14267 /* ??? Ignore the without-reg_equal-note problem for now. */
14268 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14269 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14270 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14271 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14272 place = i3;
14274 if (i2
14275 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14276 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14277 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14278 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14280 if (place)
14281 place2 = i2;
14282 else
14283 place = i2;
14286 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14287 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14288 there. */
14289 if (place && JUMP_P (place)
14290 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14291 && (JUMP_LABEL (place) == NULL
14292 || JUMP_LABEL (place) == XEXP (note, 0)))
14294 rtx label = JUMP_LABEL (place);
14296 if (!label)
14297 JUMP_LABEL (place) = XEXP (note, 0);
14298 else if (LABEL_P (label))
14299 LABEL_NUSES (label)--;
14302 if (place2 && JUMP_P (place2)
14303 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14304 && (JUMP_LABEL (place2) == NULL
14305 || JUMP_LABEL (place2) == XEXP (note, 0)))
14307 rtx label = JUMP_LABEL (place2);
14309 if (!label)
14310 JUMP_LABEL (place2) = XEXP (note, 0);
14311 else if (LABEL_P (label))
14312 LABEL_NUSES (label)--;
14313 place2 = 0;
14315 break;
14317 case REG_NONNEG:
14318 /* This note says something about the value of a register prior
14319 to the execution of an insn. It is too much trouble to see
14320 if the note is still correct in all situations. It is better
14321 to simply delete it. */
14322 break;
14324 case REG_DEAD:
14325 /* If we replaced the right hand side of FROM_INSN with a
14326 REG_EQUAL note, the original use of the dying register
14327 will not have been combined into I3 and I2. In such cases,
14328 FROM_INSN is guaranteed to be the first of the combined
14329 instructions, so we simply need to search back before
14330 FROM_INSN for the previous use or set of this register,
14331 then alter the notes there appropriately.
14333 If the register is used as an input in I3, it dies there.
14334 Similarly for I2, if it is nonzero and adjacent to I3.
14336 If the register is not used as an input in either I3 or I2
14337 and it is not one of the registers we were supposed to eliminate,
14338 there are two possibilities. We might have a non-adjacent I2
14339 or we might have somehow eliminated an additional register
14340 from a computation. For example, we might have had A & B where
14341 we discover that B will always be zero. In this case we will
14342 eliminate the reference to A.
14344 In both cases, we must search to see if we can find a previous
14345 use of A and put the death note there. */
14347 if (from_insn
14348 && from_insn == i2mod
14349 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14350 tem_insn = from_insn;
14351 else
14353 if (from_insn
14354 && CALL_P (from_insn)
14355 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14356 place = from_insn;
14357 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14358 place = i3;
14359 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14360 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14361 place = i2;
14362 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14363 && !(i2mod
14364 && reg_overlap_mentioned_p (XEXP (note, 0),
14365 i2mod_old_rhs)))
14366 || rtx_equal_p (XEXP (note, 0), elim_i1)
14367 || rtx_equal_p (XEXP (note, 0), elim_i0))
14368 break;
14369 tem_insn = i3;
14370 /* If the new I2 sets the same register that is marked dead
14371 in the note, we do not know where to put the note.
14372 Give up. */
14373 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14374 break;
14377 if (place == 0)
14379 basic_block bb = this_basic_block;
14381 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14383 if (!NONDEBUG_INSN_P (tem_insn))
14385 if (tem_insn == BB_HEAD (bb))
14386 break;
14387 continue;
14390 /* If the register is being set at TEM_INSN, see if that is all
14391 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14392 into a REG_UNUSED note instead. Don't delete sets to
14393 global register vars. */
14394 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14395 || !global_regs[REGNO (XEXP (note, 0))])
14396 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14398 rtx set = single_set (tem_insn);
14399 rtx inner_dest = 0;
14400 rtx_insn *cc0_setter = NULL;
14402 if (set != 0)
14403 for (inner_dest = SET_DEST (set);
14404 (GET_CODE (inner_dest) == STRICT_LOW_PART
14405 || GET_CODE (inner_dest) == SUBREG
14406 || GET_CODE (inner_dest) == ZERO_EXTRACT);
14407 inner_dest = XEXP (inner_dest, 0))
14410 /* Verify that it was the set, and not a clobber that
14411 modified the register.
14413 CC0 targets must be careful to maintain setter/user
14414 pairs. If we cannot delete the setter due to side
14415 effects, mark the user with an UNUSED note instead
14416 of deleting it. */
14418 if (set != 0 && ! side_effects_p (SET_SRC (set))
14419 && rtx_equal_p (XEXP (note, 0), inner_dest)
14420 && (!HAVE_cc0
14421 || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
14422 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
14423 && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
14425 /* Move the notes and links of TEM_INSN elsewhere.
14426 This might delete other dead insns recursively.
14427 First set the pattern to something that won't use
14428 any register. */
14429 rtx old_notes = REG_NOTES (tem_insn);
14431 PATTERN (tem_insn) = pc_rtx;
14432 REG_NOTES (tem_insn) = NULL;
14434 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14435 NULL_RTX, NULL_RTX, NULL_RTX);
14436 distribute_links (LOG_LINKS (tem_insn));
14438 unsigned int regno = REGNO (XEXP (note, 0));
14439 reg_stat_type *rsp = &reg_stat[regno];
14440 if (rsp->last_set == tem_insn)
14441 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14443 SET_INSN_DELETED (tem_insn);
14444 if (tem_insn == i2)
14445 i2 = NULL;
14447 /* Delete the setter too. */
14448 if (cc0_setter)
14450 PATTERN (cc0_setter) = pc_rtx;
14451 old_notes = REG_NOTES (cc0_setter);
14452 REG_NOTES (cc0_setter) = NULL;
14454 distribute_notes (old_notes, cc0_setter,
14455 cc0_setter, NULL,
14456 NULL_RTX, NULL_RTX, NULL_RTX);
14457 distribute_links (LOG_LINKS (cc0_setter));
14459 SET_INSN_DELETED (cc0_setter);
14460 if (cc0_setter == i2)
14461 i2 = NULL;
14464 else
14466 PUT_REG_NOTE_KIND (note, REG_UNUSED);
14468 /* If there isn't already a REG_UNUSED note, put one
14469 here. Do not place a REG_DEAD note, even if
14470 the register is also used here; that would not
14471 match the algorithm used in lifetime analysis
14472 and can cause the consistency check in the
14473 scheduler to fail. */
14474 if (! find_regno_note (tem_insn, REG_UNUSED,
14475 REGNO (XEXP (note, 0))))
14476 place = tem_insn;
14477 break;
14480 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14481 || (CALL_P (tem_insn)
14482 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14484 place = tem_insn;
14486 /* If we are doing a 3->2 combination, and we have a
14487 register which formerly died in i3 and was not used
14488 by i2, which now no longer dies in i3 and is used in
14489 i2 but does not die in i2, and place is between i2
14490 and i3, then we may need to move a link from place to
14491 i2. */
14492 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14493 && from_insn
14494 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14495 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14497 struct insn_link *links = LOG_LINKS (place);
14498 LOG_LINKS (place) = NULL;
14499 distribute_links (links);
14501 break;
14504 if (tem_insn == BB_HEAD (bb))
14505 break;
14510 /* If the register is set or already dead at PLACE, we needn't do
14511 anything with this note if it is still a REG_DEAD note.
14512 We check here if it is set at all, not if is it totally replaced,
14513 which is what `dead_or_set_p' checks, so also check for it being
14514 set partially. */
14516 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14518 unsigned int regno = REGNO (XEXP (note, 0));
14519 reg_stat_type *rsp = &reg_stat[regno];
14521 if (dead_or_set_p (place, XEXP (note, 0))
14522 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14524 /* Unless the register previously died in PLACE, clear
14525 last_death. [I no longer understand why this is
14526 being done.] */
14527 if (rsp->last_death != place)
14528 rsp->last_death = 0;
14529 place = 0;
14531 else
14532 rsp->last_death = place;
14534 /* If this is a death note for a hard reg that is occupying
14535 multiple registers, ensure that we are still using all
14536 parts of the object. If we find a piece of the object
14537 that is unused, we must arrange for an appropriate REG_DEAD
14538 note to be added for it. However, we can't just emit a USE
14539 and tag the note to it, since the register might actually
14540 be dead; so we recourse, and the recursive call then finds
14541 the previous insn that used this register. */
14543 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14545 unsigned int endregno = END_REGNO (XEXP (note, 0));
14546 bool all_used = true;
14547 unsigned int i;
14549 for (i = regno; i < endregno; i++)
14550 if ((! refers_to_regno_p (i, PATTERN (place))
14551 && ! find_regno_fusage (place, USE, i))
14552 || dead_or_set_regno_p (place, i))
14554 all_used = false;
14555 break;
14558 if (! all_used)
14560 /* Put only REG_DEAD notes for pieces that are
14561 not already dead or set. */
14563 for (i = regno; i < endregno;
14564 i += hard_regno_nregs (i, reg_raw_mode[i]))
14566 rtx piece = regno_reg_rtx[i];
14567 basic_block bb = this_basic_block;
14569 if (! dead_or_set_p (place, piece)
14570 && ! reg_bitfield_target_p (piece,
14571 PATTERN (place)))
14573 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14574 NULL_RTX);
14576 distribute_notes (new_note, place, place,
14577 NULL, NULL_RTX, NULL_RTX,
14578 NULL_RTX);
14580 else if (! refers_to_regno_p (i, PATTERN (place))
14581 && ! find_regno_fusage (place, USE, i))
14582 for (tem_insn = PREV_INSN (place); ;
14583 tem_insn = PREV_INSN (tem_insn))
14585 if (!NONDEBUG_INSN_P (tem_insn))
14587 if (tem_insn == BB_HEAD (bb))
14588 break;
14589 continue;
14591 if (dead_or_set_p (tem_insn, piece)
14592 || reg_bitfield_target_p (piece,
14593 PATTERN (tem_insn)))
14595 add_reg_note (tem_insn, REG_UNUSED, piece);
14596 break;
14601 place = 0;
14605 break;
14607 default:
14608 /* Any other notes should not be present at this point in the
14609 compilation. */
14610 gcc_unreachable ();
14613 if (place)
14615 XEXP (note, 1) = REG_NOTES (place);
14616 REG_NOTES (place) = note;
14619 if (place2)
14620 add_shallow_copy_of_reg_note (place2, note);
14624 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14625 I3, I2, and I1 to new locations. This is also called to add a link
14626 pointing at I3 when I3's destination is changed. */
14628 static void
14629 distribute_links (struct insn_link *links)
14631 struct insn_link *link, *next_link;
14633 for (link = links; link; link = next_link)
14635 rtx_insn *place = 0;
14636 rtx_insn *insn;
14637 rtx set, reg;
14639 next_link = link->next;
14641 /* If the insn that this link points to is a NOTE, ignore it. */
14642 if (NOTE_P (link->insn))
14643 continue;
14645 set = 0;
14646 rtx pat = PATTERN (link->insn);
14647 if (GET_CODE (pat) == SET)
14648 set = pat;
14649 else if (GET_CODE (pat) == PARALLEL)
14651 int i;
14652 for (i = 0; i < XVECLEN (pat, 0); i++)
14654 set = XVECEXP (pat, 0, i);
14655 if (GET_CODE (set) != SET)
14656 continue;
14658 reg = SET_DEST (set);
14659 while (GET_CODE (reg) == ZERO_EXTRACT
14660 || GET_CODE (reg) == STRICT_LOW_PART
14661 || GET_CODE (reg) == SUBREG)
14662 reg = XEXP (reg, 0);
14664 if (!REG_P (reg))
14665 continue;
14667 if (REGNO (reg) == link->regno)
14668 break;
14670 if (i == XVECLEN (pat, 0))
14671 continue;
14673 else
14674 continue;
14676 reg = SET_DEST (set);
14678 while (GET_CODE (reg) == ZERO_EXTRACT
14679 || GET_CODE (reg) == STRICT_LOW_PART
14680 || GET_CODE (reg) == SUBREG)
14681 reg = XEXP (reg, 0);
14683 /* A LOG_LINK is defined as being placed on the first insn that uses
14684 a register and points to the insn that sets the register. Start
14685 searching at the next insn after the target of the link and stop
14686 when we reach a set of the register or the end of the basic block.
14688 Note that this correctly handles the link that used to point from
14689 I3 to I2. Also note that not much searching is typically done here
14690 since most links don't point very far away. */
14692 for (insn = NEXT_INSN (link->insn);
14693 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14694 || BB_HEAD (this_basic_block->next_bb) != insn));
14695 insn = NEXT_INSN (insn))
14696 if (DEBUG_INSN_P (insn))
14697 continue;
14698 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14700 if (reg_referenced_p (reg, PATTERN (insn)))
14701 place = insn;
14702 break;
14704 else if (CALL_P (insn)
14705 && find_reg_fusage (insn, USE, reg))
14707 place = insn;
14708 break;
14710 else if (INSN_P (insn) && reg_set_p (reg, insn))
14711 break;
14713 /* If we found a place to put the link, place it there unless there
14714 is already a link to the same insn as LINK at that point. */
14716 if (place)
14718 struct insn_link *link2;
14720 FOR_EACH_LOG_LINK (link2, place)
14721 if (link2->insn == link->insn && link2->regno == link->regno)
14722 break;
14724 if (link2 == NULL)
14726 link->next = LOG_LINKS (place);
14727 LOG_LINKS (place) = link;
14729 /* Set added_links_insn to the earliest insn we added a
14730 link to. */
14731 if (added_links_insn == 0
14732 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14733 added_links_insn = place;
14739 /* Check for any register or memory mentioned in EQUIV that is not
14740 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14741 of EXPR where some registers may have been replaced by constants. */
14743 static bool
14744 unmentioned_reg_p (rtx equiv, rtx expr)
14746 subrtx_iterator::array_type array;
14747 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14749 const_rtx x = *iter;
14750 if ((REG_P (x) || MEM_P (x))
14751 && !reg_mentioned_p (x, expr))
14752 return true;
14754 return false;
14757 DEBUG_FUNCTION void
14758 dump_combine_stats (FILE *file)
14760 fprintf
14761 (file,
14762 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14763 combine_attempts, combine_merges, combine_extras, combine_successes);
14766 void
14767 dump_combine_total_stats (FILE *file)
14769 fprintf
14770 (file,
14771 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14772 total_attempts, total_merges, total_extras, total_successes);
14775 /* Try combining insns through substitution. */
14776 static unsigned int
14777 rest_of_handle_combine (void)
14779 int rebuild_jump_labels_after_combine;
14781 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14782 df_note_add_problem ();
14783 df_analyze ();
14785 regstat_init_n_sets_and_refs ();
14786 reg_n_sets_max = max_reg_num ();
14788 rebuild_jump_labels_after_combine
14789 = combine_instructions (get_insns (), max_reg_num ());
14791 /* Combining insns may have turned an indirect jump into a
14792 direct jump. Rebuild the JUMP_LABEL fields of jumping
14793 instructions. */
14794 if (rebuild_jump_labels_after_combine)
14796 if (dom_info_available_p (CDI_DOMINATORS))
14797 free_dominance_info (CDI_DOMINATORS);
14798 timevar_push (TV_JUMP);
14799 rebuild_jump_labels (get_insns ());
14800 cleanup_cfg (0);
14801 timevar_pop (TV_JUMP);
14804 regstat_free_n_sets_and_refs ();
14805 return 0;
14808 namespace {
14810 const pass_data pass_data_combine =
14812 RTL_PASS, /* type */
14813 "combine", /* name */
14814 OPTGROUP_NONE, /* optinfo_flags */
14815 TV_COMBINE, /* tv_id */
14816 PROP_cfglayout, /* properties_required */
14817 0, /* properties_provided */
14818 0, /* properties_destroyed */
14819 0, /* todo_flags_start */
14820 TODO_df_finish, /* todo_flags_finish */
14823 class pass_combine : public rtl_opt_pass
14825 public:
14826 pass_combine (gcc::context *ctxt)
14827 : rtl_opt_pass (pass_data_combine, ctxt)
14830 /* opt_pass methods: */
14831 virtual bool gate (function *) { return (optimize > 0); }
14832 virtual unsigned int execute (function *)
14834 return rest_of_handle_combine ();
14837 }; // class pass_combine
14839 } // anon namespace
14841 rtl_opt_pass *
14842 make_pass_combine (gcc::context *ctxt)
14844 return new pass_combine (ctxt);