* tree-outof-ssa.h (ssaexpand): Add partitions_for_undefined_values.
[official-gcc.git] / gcc / combine.c
blob400cef3495a3faf08f7267765991bf278a1c5128
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_rtx_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 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
583 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
584 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
585 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
586 break;
588 return find_single_use_1 (dest, &SET_SRC (x));
590 case MEM:
591 case SUBREG:
592 return find_single_use_1 (dest, &XEXP (x, 0));
594 default:
595 break;
598 /* If it wasn't one of the common cases above, check each expression and
599 vector of this code. Look for a unique usage of DEST. */
601 fmt = GET_RTX_FORMAT (code);
602 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
604 if (fmt[i] == 'e')
606 if (dest == XEXP (x, i)
607 || (REG_P (dest) && REG_P (XEXP (x, i))
608 && REGNO (dest) == REGNO (XEXP (x, i))))
609 this_result = loc;
610 else
611 this_result = find_single_use_1 (dest, &XEXP (x, i));
613 if (result == NULL)
614 result = this_result;
615 else if (this_result)
616 /* Duplicate usage. */
617 return NULL;
619 else if (fmt[i] == 'E')
621 int j;
623 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
625 if (XVECEXP (x, i, j) == dest
626 || (REG_P (dest)
627 && REG_P (XVECEXP (x, i, j))
628 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
629 this_result = loc;
630 else
631 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
633 if (result == NULL)
634 result = this_result;
635 else if (this_result)
636 return NULL;
641 return result;
645 /* See if DEST, produced in INSN, is used only a single time in the
646 sequel. If so, return a pointer to the innermost rtx expression in which
647 it is used.
649 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
651 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
652 care about REG_DEAD notes or LOG_LINKS.
654 Otherwise, we find the single use by finding an insn that has a
655 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
656 only referenced once in that insn, we know that it must be the first
657 and last insn referencing DEST. */
659 static rtx *
660 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
662 basic_block bb;
663 rtx_insn *next;
664 rtx *result;
665 struct insn_link *link;
667 if (dest == cc0_rtx)
669 next = NEXT_INSN (insn);
670 if (next == 0
671 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
672 return 0;
674 result = find_single_use_1 (dest, &PATTERN (next));
675 if (result && ploc)
676 *ploc = next;
677 return result;
680 if (!REG_P (dest))
681 return 0;
683 bb = BLOCK_FOR_INSN (insn);
684 for (next = NEXT_INSN (insn);
685 next && BLOCK_FOR_INSN (next) == bb;
686 next = NEXT_INSN (next))
687 if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
689 FOR_EACH_LOG_LINK (link, next)
690 if (link->insn == insn && link->regno == REGNO (dest))
691 break;
693 if (link)
695 result = find_single_use_1 (dest, &PATTERN (next));
696 if (ploc)
697 *ploc = next;
698 return result;
702 return 0;
705 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
706 insn. The substitution can be undone by undo_all. If INTO is already
707 set to NEWVAL, do not record this change. Because computing NEWVAL might
708 also call SUBST, we have to compute it before we put anything into
709 the undo table. */
711 static void
712 do_SUBST (rtx *into, rtx newval)
714 struct undo *buf;
715 rtx oldval = *into;
717 if (oldval == newval)
718 return;
720 /* We'd like to catch as many invalid transformations here as
721 possible. Unfortunately, there are way too many mode changes
722 that are perfectly valid, so we'd waste too much effort for
723 little gain doing the checks here. Focus on catching invalid
724 transformations involving integer constants. */
725 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
726 && CONST_INT_P (newval))
728 /* Sanity check that we're replacing oldval with a CONST_INT
729 that is a valid sign-extension for the original mode. */
730 gcc_assert (INTVAL (newval)
731 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
733 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
734 CONST_INT is not valid, because after the replacement, the
735 original mode would be gone. Unfortunately, we can't tell
736 when do_SUBST is called to replace the operand thereof, so we
737 perform this test on oldval instead, checking whether an
738 invalid replacement took place before we got here. */
739 gcc_assert (!(GET_CODE (oldval) == SUBREG
740 && CONST_INT_P (SUBREG_REG (oldval))));
741 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
742 && CONST_INT_P (XEXP (oldval, 0))));
745 if (undobuf.frees)
746 buf = undobuf.frees, undobuf.frees = buf->next;
747 else
748 buf = XNEW (struct undo);
750 buf->kind = UNDO_RTX;
751 buf->where.r = into;
752 buf->old_contents.r = oldval;
753 *into = newval;
755 buf->next = undobuf.undos, undobuf.undos = buf;
758 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
760 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
761 for the value of a HOST_WIDE_INT value (including CONST_INT) is
762 not safe. */
764 static void
765 do_SUBST_INT (int *into, int newval)
767 struct undo *buf;
768 int oldval = *into;
770 if (oldval == newval)
771 return;
773 if (undobuf.frees)
774 buf = undobuf.frees, undobuf.frees = buf->next;
775 else
776 buf = XNEW (struct undo);
778 buf->kind = UNDO_INT;
779 buf->where.i = into;
780 buf->old_contents.i = oldval;
781 *into = newval;
783 buf->next = undobuf.undos, undobuf.undos = buf;
786 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
788 /* Similar to SUBST, but just substitute the mode. This is used when
789 changing the mode of a pseudo-register, so that any other
790 references to the entry in the regno_reg_rtx array will change as
791 well. */
793 static void
794 do_SUBST_MODE (rtx *into, machine_mode newval)
796 struct undo *buf;
797 machine_mode oldval = GET_MODE (*into);
799 if (oldval == newval)
800 return;
802 if (undobuf.frees)
803 buf = undobuf.frees, undobuf.frees = buf->next;
804 else
805 buf = XNEW (struct undo);
807 buf->kind = UNDO_MODE;
808 buf->where.r = into;
809 buf->old_contents.m = oldval;
810 adjust_reg_mode (*into, newval);
812 buf->next = undobuf.undos, undobuf.undos = buf;
815 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
817 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
819 static void
820 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
822 struct undo *buf;
823 struct insn_link * oldval = *into;
825 if (oldval == newval)
826 return;
828 if (undobuf.frees)
829 buf = undobuf.frees, undobuf.frees = buf->next;
830 else
831 buf = XNEW (struct undo);
833 buf->kind = UNDO_LINKS;
834 buf->where.l = into;
835 buf->old_contents.l = oldval;
836 *into = newval;
838 buf->next = undobuf.undos, undobuf.undos = buf;
841 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
843 /* Subroutine of try_combine. Determine whether the replacement patterns
844 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
845 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
846 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
847 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
848 of all the instructions can be estimated and the replacements are more
849 expensive than the original sequence. */
851 static bool
852 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
853 rtx newpat, rtx newi2pat, rtx newotherpat)
855 int i0_cost, i1_cost, i2_cost, i3_cost;
856 int new_i2_cost, new_i3_cost;
857 int old_cost, new_cost;
859 /* Lookup the original insn_rtx_costs. */
860 i2_cost = INSN_COST (i2);
861 i3_cost = INSN_COST (i3);
863 if (i1)
865 i1_cost = INSN_COST (i1);
866 if (i0)
868 i0_cost = INSN_COST (i0);
869 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
870 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
872 else
874 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
875 ? i1_cost + i2_cost + i3_cost : 0);
876 i0_cost = 0;
879 else
881 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
882 i1_cost = i0_cost = 0;
885 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
886 correct that. */
887 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
888 old_cost -= i1_cost;
891 /* Calculate the replacement insn_rtx_costs. */
892 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
893 if (newi2pat)
895 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
896 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
897 ? new_i2_cost + new_i3_cost : 0;
899 else
901 new_cost = new_i3_cost;
902 new_i2_cost = 0;
905 if (undobuf.other_insn)
907 int old_other_cost, new_other_cost;
909 old_other_cost = INSN_COST (undobuf.other_insn);
910 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
911 if (old_other_cost > 0 && new_other_cost > 0)
913 old_cost += old_other_cost;
914 new_cost += new_other_cost;
916 else
917 old_cost = 0;
920 /* Disallow this combination if both new_cost and old_cost are greater than
921 zero, and new_cost is greater than old cost. */
922 int reject = old_cost > 0 && new_cost > old_cost;
924 if (dump_file)
926 fprintf (dump_file, "%s combination of insns ",
927 reject ? "rejecting" : "allowing");
928 if (i0)
929 fprintf (dump_file, "%d, ", INSN_UID (i0));
930 if (i1 && INSN_UID (i1) != INSN_UID (i2))
931 fprintf (dump_file, "%d, ", INSN_UID (i1));
932 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
934 fprintf (dump_file, "original costs ");
935 if (i0)
936 fprintf (dump_file, "%d + ", i0_cost);
937 if (i1 && INSN_UID (i1) != INSN_UID (i2))
938 fprintf (dump_file, "%d + ", i1_cost);
939 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
941 if (newi2pat)
942 fprintf (dump_file, "replacement costs %d + %d = %d\n",
943 new_i2_cost, new_i3_cost, new_cost);
944 else
945 fprintf (dump_file, "replacement cost %d\n", new_cost);
948 if (reject)
949 return false;
951 /* Update the uid_insn_cost array with the replacement costs. */
952 INSN_COST (i2) = new_i2_cost;
953 INSN_COST (i3) = new_i3_cost;
954 if (i1)
956 INSN_COST (i1) = 0;
957 if (i0)
958 INSN_COST (i0) = 0;
961 return true;
965 /* Delete any insns that copy a register to itself. */
967 static void
968 delete_noop_moves (void)
970 rtx_insn *insn, *next;
971 basic_block bb;
973 FOR_EACH_BB_FN (bb, cfun)
975 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
977 next = NEXT_INSN (insn);
978 if (INSN_P (insn) && noop_move_p (insn))
980 if (dump_file)
981 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
983 delete_insn_and_edges (insn);
990 /* Return false if we do not want to (or cannot) combine DEF. */
991 static bool
992 can_combine_def_p (df_ref def)
994 /* Do not consider if it is pre/post modification in MEM. */
995 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
996 return false;
998 unsigned int regno = DF_REF_REGNO (def);
1000 /* Do not combine frame pointer adjustments. */
1001 if ((regno == FRAME_POINTER_REGNUM
1002 && (!reload_completed || frame_pointer_needed))
1003 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1004 && regno == HARD_FRAME_POINTER_REGNUM
1005 && (!reload_completed || frame_pointer_needed))
1006 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1007 && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1008 return false;
1010 return true;
1013 /* Return false if we do not want to (or cannot) combine USE. */
1014 static bool
1015 can_combine_use_p (df_ref use)
1017 /* Do not consider the usage of the stack pointer by function call. */
1018 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1019 return false;
1021 return true;
1024 /* Fill in log links field for all insns. */
1026 static void
1027 create_log_links (void)
1029 basic_block bb;
1030 rtx_insn **next_use;
1031 rtx_insn *insn;
1032 df_ref def, use;
1034 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1036 /* Pass through each block from the end, recording the uses of each
1037 register and establishing log links when def is encountered.
1038 Note that we do not clear next_use array in order to save time,
1039 so we have to test whether the use is in the same basic block as def.
1041 There are a few cases below when we do not consider the definition or
1042 usage -- these are taken from original flow.c did. Don't ask me why it is
1043 done this way; I don't know and if it works, I don't want to know. */
1045 FOR_EACH_BB_FN (bb, cfun)
1047 FOR_BB_INSNS_REVERSE (bb, insn)
1049 if (!NONDEBUG_INSN_P (insn))
1050 continue;
1052 /* Log links are created only once. */
1053 gcc_assert (!LOG_LINKS (insn));
1055 FOR_EACH_INSN_DEF (def, insn)
1057 unsigned int regno = DF_REF_REGNO (def);
1058 rtx_insn *use_insn;
1060 if (!next_use[regno])
1061 continue;
1063 if (!can_combine_def_p (def))
1064 continue;
1066 use_insn = next_use[regno];
1067 next_use[regno] = NULL;
1069 if (BLOCK_FOR_INSN (use_insn) != bb)
1070 continue;
1072 /* flow.c claimed:
1074 We don't build a LOG_LINK for hard registers contained
1075 in ASM_OPERANDs. If these registers get replaced,
1076 we might wind up changing the semantics of the insn,
1077 even if reload can make what appear to be valid
1078 assignments later. */
1079 if (regno < FIRST_PSEUDO_REGISTER
1080 && asm_noperands (PATTERN (use_insn)) >= 0)
1081 continue;
1083 /* Don't add duplicate links between instructions. */
1084 struct insn_link *links;
1085 FOR_EACH_LOG_LINK (links, use_insn)
1086 if (insn == links->insn && regno == links->regno)
1087 break;
1089 if (!links)
1090 LOG_LINKS (use_insn)
1091 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1094 FOR_EACH_INSN_USE (use, insn)
1095 if (can_combine_use_p (use))
1096 next_use[DF_REF_REGNO (use)] = insn;
1100 free (next_use);
1103 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1104 true if we found a LOG_LINK that proves that A feeds B. This only works
1105 if there are no instructions between A and B which could have a link
1106 depending on A, since in that case we would not record a link for B.
1107 We also check the implicit dependency created by a cc0 setter/user
1108 pair. */
1110 static bool
1111 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1113 struct insn_link *links;
1114 FOR_EACH_LOG_LINK (links, b)
1115 if (links->insn == a)
1116 return true;
1117 if (HAVE_cc0 && sets_cc0_p (a))
1118 return true;
1119 return false;
1122 /* Main entry point for combiner. F is the first insn of the function.
1123 NREGS is the first unused pseudo-reg number.
1125 Return nonzero if the combiner has turned an indirect jump
1126 instruction into a direct jump. */
1127 static int
1128 combine_instructions (rtx_insn *f, unsigned int nregs)
1130 rtx_insn *insn, *next;
1131 rtx_insn *prev;
1132 struct insn_link *links, *nextlinks;
1133 rtx_insn *first;
1134 basic_block last_bb;
1136 int new_direct_jump_p = 0;
1138 for (first = f; first && !NONDEBUG_INSN_P (first); )
1139 first = NEXT_INSN (first);
1140 if (!first)
1141 return 0;
1143 combine_attempts = 0;
1144 combine_merges = 0;
1145 combine_extras = 0;
1146 combine_successes = 0;
1148 rtl_hooks = combine_rtl_hooks;
1150 reg_stat.safe_grow_cleared (nregs);
1152 init_recog_no_volatile ();
1154 /* Allocate array for insn info. */
1155 max_uid_known = get_max_uid ();
1156 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1157 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1158 gcc_obstack_init (&insn_link_obstack);
1160 nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1162 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1163 problems when, for example, we have j <<= 1 in a loop. */
1165 nonzero_sign_valid = 0;
1166 label_tick = label_tick_ebb_start = 1;
1168 /* Scan all SETs and see if we can deduce anything about what
1169 bits are known to be zero for some registers and how many copies
1170 of the sign bit are known to exist for those registers.
1172 Also set any known values so that we can use it while searching
1173 for what bits are known to be set. */
1175 setup_incoming_promotions (first);
1176 /* Allow the entry block and the first block to fall into the same EBB.
1177 Conceptually the incoming promotions are assigned to the entry block. */
1178 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1180 create_log_links ();
1181 FOR_EACH_BB_FN (this_basic_block, cfun)
1183 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1184 last_call_luid = 0;
1185 mem_last_set = -1;
1187 label_tick++;
1188 if (!single_pred_p (this_basic_block)
1189 || single_pred (this_basic_block) != last_bb)
1190 label_tick_ebb_start = label_tick;
1191 last_bb = this_basic_block;
1193 FOR_BB_INSNS (this_basic_block, insn)
1194 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1196 rtx links;
1198 subst_low_luid = DF_INSN_LUID (insn);
1199 subst_insn = insn;
1201 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1202 insn);
1203 record_dead_and_set_regs (insn);
1205 if (AUTO_INC_DEC)
1206 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1207 if (REG_NOTE_KIND (links) == REG_INC)
1208 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1209 insn);
1211 /* Record the current insn_rtx_cost of this instruction. */
1212 if (NONJUMP_INSN_P (insn))
1213 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1214 optimize_this_for_speed_p);
1215 if (dump_file)
1217 fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1218 dump_insn_slim (dump_file, insn);
1223 nonzero_sign_valid = 1;
1225 /* Now scan all the insns in forward order. */
1226 label_tick = label_tick_ebb_start = 1;
1227 init_reg_last ();
1228 setup_incoming_promotions (first);
1229 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1230 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1232 FOR_EACH_BB_FN (this_basic_block, cfun)
1234 rtx_insn *last_combined_insn = NULL;
1236 /* Ignore instruction combination in basic blocks that are going to
1237 be removed as unreachable anyway. See PR82386. */
1238 if (EDGE_COUNT (this_basic_block->preds) == 0)
1239 continue;
1241 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1242 last_call_luid = 0;
1243 mem_last_set = -1;
1245 label_tick++;
1246 if (!single_pred_p (this_basic_block)
1247 || single_pred (this_basic_block) != last_bb)
1248 label_tick_ebb_start = label_tick;
1249 last_bb = this_basic_block;
1251 rtl_profile_for_bb (this_basic_block);
1252 for (insn = BB_HEAD (this_basic_block);
1253 insn != NEXT_INSN (BB_END (this_basic_block));
1254 insn = next ? next : NEXT_INSN (insn))
1256 next = 0;
1257 if (!NONDEBUG_INSN_P (insn))
1258 continue;
1260 while (last_combined_insn
1261 && (!NONDEBUG_INSN_P (last_combined_insn)
1262 || last_combined_insn->deleted ()))
1263 last_combined_insn = PREV_INSN (last_combined_insn);
1264 if (last_combined_insn == NULL_RTX
1265 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1266 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1267 last_combined_insn = insn;
1269 /* See if we know about function return values before this
1270 insn based upon SUBREG flags. */
1271 check_promoted_subreg (insn, PATTERN (insn));
1273 /* See if we can find hardregs and subreg of pseudos in
1274 narrower modes. This could help turning TRUNCATEs
1275 into SUBREGs. */
1276 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1278 /* Try this insn with each insn it links back to. */
1280 FOR_EACH_LOG_LINK (links, insn)
1281 if ((next = try_combine (insn, links->insn, NULL,
1282 NULL, &new_direct_jump_p,
1283 last_combined_insn)) != 0)
1285 statistics_counter_event (cfun, "two-insn combine", 1);
1286 goto retry;
1289 /* Try each sequence of three linked insns ending with this one. */
1291 if (max_combine >= 3)
1292 FOR_EACH_LOG_LINK (links, insn)
1294 rtx_insn *link = links->insn;
1296 /* If the linked insn has been replaced by a note, then there
1297 is no point in pursuing this chain any further. */
1298 if (NOTE_P (link))
1299 continue;
1301 FOR_EACH_LOG_LINK (nextlinks, link)
1302 if ((next = try_combine (insn, link, nextlinks->insn,
1303 NULL, &new_direct_jump_p,
1304 last_combined_insn)) != 0)
1306 statistics_counter_event (cfun, "three-insn combine", 1);
1307 goto retry;
1311 /* Try to combine a jump insn that uses CC0
1312 with a preceding insn that sets CC0, and maybe with its
1313 logical predecessor as well.
1314 This is how we make decrement-and-branch insns.
1315 We need this special code because data flow connections
1316 via CC0 do not get entered in LOG_LINKS. */
1318 if (HAVE_cc0
1319 && JUMP_P (insn)
1320 && (prev = prev_nonnote_insn (insn)) != 0
1321 && NONJUMP_INSN_P (prev)
1322 && sets_cc0_p (PATTERN (prev)))
1324 if ((next = try_combine (insn, prev, NULL, NULL,
1325 &new_direct_jump_p,
1326 last_combined_insn)) != 0)
1327 goto retry;
1329 FOR_EACH_LOG_LINK (nextlinks, prev)
1330 if ((next = try_combine (insn, prev, nextlinks->insn,
1331 NULL, &new_direct_jump_p,
1332 last_combined_insn)) != 0)
1333 goto retry;
1336 /* Do the same for an insn that explicitly references CC0. */
1337 if (HAVE_cc0 && NONJUMP_INSN_P (insn)
1338 && (prev = prev_nonnote_insn (insn)) != 0
1339 && NONJUMP_INSN_P (prev)
1340 && sets_cc0_p (PATTERN (prev))
1341 && GET_CODE (PATTERN (insn)) == SET
1342 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1344 if ((next = try_combine (insn, prev, NULL, NULL,
1345 &new_direct_jump_p,
1346 last_combined_insn)) != 0)
1347 goto retry;
1349 FOR_EACH_LOG_LINK (nextlinks, prev)
1350 if ((next = try_combine (insn, prev, nextlinks->insn,
1351 NULL, &new_direct_jump_p,
1352 last_combined_insn)) != 0)
1353 goto retry;
1356 /* Finally, see if any of the insns that this insn links to
1357 explicitly references CC0. If so, try this insn, that insn,
1358 and its predecessor if it sets CC0. */
1359 if (HAVE_cc0)
1361 FOR_EACH_LOG_LINK (links, insn)
1362 if (NONJUMP_INSN_P (links->insn)
1363 && GET_CODE (PATTERN (links->insn)) == SET
1364 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1365 && (prev = prev_nonnote_insn (links->insn)) != 0
1366 && NONJUMP_INSN_P (prev)
1367 && sets_cc0_p (PATTERN (prev))
1368 && (next = try_combine (insn, links->insn,
1369 prev, NULL, &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1374 /* Try combining an insn with two different insns whose results it
1375 uses. */
1376 if (max_combine >= 3)
1377 FOR_EACH_LOG_LINK (links, insn)
1378 for (nextlinks = links->next; nextlinks;
1379 nextlinks = nextlinks->next)
1380 if ((next = try_combine (insn, links->insn,
1381 nextlinks->insn, NULL,
1382 &new_direct_jump_p,
1383 last_combined_insn)) != 0)
1386 statistics_counter_event (cfun, "three-insn combine", 1);
1387 goto retry;
1390 /* Try four-instruction combinations. */
1391 if (max_combine >= 4)
1392 FOR_EACH_LOG_LINK (links, insn)
1394 struct insn_link *next1;
1395 rtx_insn *link = links->insn;
1397 /* If the linked insn has been replaced by a note, then there
1398 is no point in pursuing this chain any further. */
1399 if (NOTE_P (link))
1400 continue;
1402 FOR_EACH_LOG_LINK (next1, link)
1404 rtx_insn *link1 = next1->insn;
1405 if (NOTE_P (link1))
1406 continue;
1407 /* I0 -> I1 -> I2 -> I3. */
1408 FOR_EACH_LOG_LINK (nextlinks, link1)
1409 if ((next = try_combine (insn, link, link1,
1410 nextlinks->insn,
1411 &new_direct_jump_p,
1412 last_combined_insn)) != 0)
1414 statistics_counter_event (cfun, "four-insn combine", 1);
1415 goto retry;
1417 /* I0, I1 -> I2, I2 -> I3. */
1418 for (nextlinks = next1->next; nextlinks;
1419 nextlinks = nextlinks->next)
1420 if ((next = try_combine (insn, link, link1,
1421 nextlinks->insn,
1422 &new_direct_jump_p,
1423 last_combined_insn)) != 0)
1425 statistics_counter_event (cfun, "four-insn combine", 1);
1426 goto retry;
1430 for (next1 = links->next; next1; next1 = next1->next)
1432 rtx_insn *link1 = next1->insn;
1433 if (NOTE_P (link1))
1434 continue;
1435 /* I0 -> I2; I1, I2 -> I3. */
1436 FOR_EACH_LOG_LINK (nextlinks, link)
1437 if ((next = try_combine (insn, link, link1,
1438 nextlinks->insn,
1439 &new_direct_jump_p,
1440 last_combined_insn)) != 0)
1442 statistics_counter_event (cfun, "four-insn combine", 1);
1443 goto retry;
1445 /* I0 -> I1; I1, I2 -> I3. */
1446 FOR_EACH_LOG_LINK (nextlinks, link1)
1447 if ((next = try_combine (insn, link, link1,
1448 nextlinks->insn,
1449 &new_direct_jump_p,
1450 last_combined_insn)) != 0)
1452 statistics_counter_event (cfun, "four-insn combine", 1);
1453 goto retry;
1458 /* Try this insn with each REG_EQUAL note it links back to. */
1459 FOR_EACH_LOG_LINK (links, insn)
1461 rtx set, note;
1462 rtx_insn *temp = links->insn;
1463 if ((set = single_set (temp)) != 0
1464 && (note = find_reg_equal_equiv_note (temp)) != 0
1465 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1466 /* Avoid using a register that may already been marked
1467 dead by an earlier instruction. */
1468 && ! unmentioned_reg_p (note, SET_SRC (set))
1469 && (GET_MODE (note) == VOIDmode
1470 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1471 : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1472 && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1473 || (GET_MODE (XEXP (SET_DEST (set), 0))
1474 == GET_MODE (note))))))
1476 /* Temporarily replace the set's source with the
1477 contents of the REG_EQUAL note. The insn will
1478 be deleted or recognized by try_combine. */
1479 rtx orig_src = SET_SRC (set);
1480 rtx orig_dest = SET_DEST (set);
1481 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1482 SET_DEST (set) = XEXP (SET_DEST (set), 0);
1483 SET_SRC (set) = note;
1484 i2mod = temp;
1485 i2mod_old_rhs = copy_rtx (orig_src);
1486 i2mod_new_rhs = copy_rtx (note);
1487 next = try_combine (insn, i2mod, NULL, NULL,
1488 &new_direct_jump_p,
1489 last_combined_insn);
1490 i2mod = NULL;
1491 if (next)
1493 statistics_counter_event (cfun, "insn-with-note combine", 1);
1494 goto retry;
1496 SET_SRC (set) = orig_src;
1497 SET_DEST (set) = orig_dest;
1501 if (!NOTE_P (insn))
1502 record_dead_and_set_regs (insn);
1504 retry:
1509 default_rtl_profile ();
1510 clear_bb_flags ();
1511 new_direct_jump_p |= purge_all_dead_edges ();
1512 delete_noop_moves ();
1514 /* Clean up. */
1515 obstack_free (&insn_link_obstack, NULL);
1516 free (uid_log_links);
1517 free (uid_insn_cost);
1518 reg_stat.release ();
1521 struct undo *undo, *next;
1522 for (undo = undobuf.frees; undo; undo = next)
1524 next = undo->next;
1525 free (undo);
1527 undobuf.frees = 0;
1530 total_attempts += combine_attempts;
1531 total_merges += combine_merges;
1532 total_extras += combine_extras;
1533 total_successes += combine_successes;
1535 nonzero_sign_valid = 0;
1536 rtl_hooks = general_rtl_hooks;
1538 /* Make recognizer allow volatile MEMs again. */
1539 init_recog ();
1541 return new_direct_jump_p;
1544 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1546 static void
1547 init_reg_last (void)
1549 unsigned int i;
1550 reg_stat_type *p;
1552 FOR_EACH_VEC_ELT (reg_stat, i, p)
1553 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1556 /* Set up any promoted values for incoming argument registers. */
1558 static void
1559 setup_incoming_promotions (rtx_insn *first)
1561 tree arg;
1562 bool strictly_local = false;
1564 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1565 arg = DECL_CHAIN (arg))
1567 rtx x, reg = DECL_INCOMING_RTL (arg);
1568 int uns1, uns3;
1569 machine_mode mode1, mode2, mode3, mode4;
1571 /* Only continue if the incoming argument is in a register. */
1572 if (!REG_P (reg))
1573 continue;
1575 /* Determine, if possible, whether all call sites of the current
1576 function lie within the current compilation unit. (This does
1577 take into account the exporting of a function via taking its
1578 address, and so forth.) */
1579 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1581 /* The mode and signedness of the argument before any promotions happen
1582 (equal to the mode of the pseudo holding it at that stage). */
1583 mode1 = TYPE_MODE (TREE_TYPE (arg));
1584 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1586 /* The mode and signedness of the argument after any source language and
1587 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1588 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1589 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1591 /* The mode and signedness of the argument as it is actually passed,
1592 see assign_parm_setup_reg in function.c. */
1593 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1594 TREE_TYPE (cfun->decl), 0);
1596 /* The mode of the register in which the argument is being passed. */
1597 mode4 = GET_MODE (reg);
1599 /* Eliminate sign extensions in the callee when:
1600 (a) A mode promotion has occurred; */
1601 if (mode1 == mode3)
1602 continue;
1603 /* (b) The mode of the register is the same as the mode of
1604 the argument as it is passed; */
1605 if (mode3 != mode4)
1606 continue;
1607 /* (c) There's no language level extension; */
1608 if (mode1 == mode2)
1610 /* (c.1) All callers are from the current compilation unit. If that's
1611 the case we don't have to rely on an ABI, we only have to know
1612 what we're generating right now, and we know that we will do the
1613 mode1 to mode2 promotion with the given sign. */
1614 else if (!strictly_local)
1615 continue;
1616 /* (c.2) The combination of the two promotions is useful. This is
1617 true when the signs match, or if the first promotion is unsigned.
1618 In the later case, (sign_extend (zero_extend x)) is the same as
1619 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1620 else if (uns1)
1621 uns3 = true;
1622 else if (uns3)
1623 continue;
1625 /* Record that the value was promoted from mode1 to mode3,
1626 so that any sign extension at the head of the current
1627 function may be eliminated. */
1628 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1629 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1630 record_value_for_reg (reg, first, x);
1634 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1635 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1636 because some machines (maybe most) will actually do the sign-extension and
1637 this is the conservative approach.
1639 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1640 kludge. */
1642 static rtx
1643 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1645 scalar_int_mode int_mode;
1646 if (CONST_INT_P (src)
1647 && is_a <scalar_int_mode> (mode, &int_mode)
1648 && GET_MODE_PRECISION (int_mode) < prec
1649 && INTVAL (src) > 0
1650 && val_signbit_known_set_p (int_mode, INTVAL (src)))
1651 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1653 return src;
1656 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1657 and SET. */
1659 static void
1660 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1661 rtx x)
1663 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1664 unsigned HOST_WIDE_INT bits = 0;
1665 rtx reg_equal = NULL, src = SET_SRC (set);
1666 unsigned int num = 0;
1668 if (reg_equal_note)
1669 reg_equal = XEXP (reg_equal_note, 0);
1671 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1673 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1674 if (reg_equal)
1675 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1678 /* Don't call nonzero_bits if it cannot change anything. */
1679 if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1681 bits = nonzero_bits (src, nonzero_bits_mode);
1682 if (reg_equal && bits)
1683 bits &= nonzero_bits (reg_equal, nonzero_bits_mode);
1684 rsp->nonzero_bits |= bits;
1687 /* Don't call num_sign_bit_copies if it cannot change anything. */
1688 if (rsp->sign_bit_copies != 1)
1690 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1691 if (reg_equal && num != GET_MODE_PRECISION (GET_MODE (x)))
1693 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1694 if (num == 0 || numeq > num)
1695 num = numeq;
1697 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1698 rsp->sign_bit_copies = num;
1702 /* Called via note_stores. If X is a pseudo that is narrower than
1703 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1705 If we are setting only a portion of X and we can't figure out what
1706 portion, assume all bits will be used since we don't know what will
1707 be happening.
1709 Similarly, set how many bits of X are known to be copies of the sign bit
1710 at all locations in the function. This is the smallest number implied
1711 by any set of X. */
1713 static void
1714 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1716 rtx_insn *insn = (rtx_insn *) data;
1717 scalar_int_mode mode;
1719 if (REG_P (x)
1720 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1721 /* If this register is undefined at the start of the file, we can't
1722 say what its contents were. */
1723 && ! REGNO_REG_SET_P
1724 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1725 && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1726 && HWI_COMPUTABLE_MODE_P (mode))
1728 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1730 if (set == 0 || GET_CODE (set) == CLOBBER)
1732 rsp->nonzero_bits = GET_MODE_MASK (mode);
1733 rsp->sign_bit_copies = 1;
1734 return;
1737 /* If this register is being initialized using itself, and the
1738 register is uninitialized in this basic block, and there are
1739 no LOG_LINKS which set the register, then part of the
1740 register is uninitialized. In that case we can't assume
1741 anything about the number of nonzero bits.
1743 ??? We could do better if we checked this in
1744 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1745 could avoid making assumptions about the insn which initially
1746 sets the register, while still using the information in other
1747 insns. We would have to be careful to check every insn
1748 involved in the combination. */
1750 if (insn
1751 && reg_referenced_p (x, PATTERN (insn))
1752 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1753 REGNO (x)))
1755 struct insn_link *link;
1757 FOR_EACH_LOG_LINK (link, insn)
1758 if (dead_or_set_p (link->insn, x))
1759 break;
1760 if (!link)
1762 rsp->nonzero_bits = GET_MODE_MASK (mode);
1763 rsp->sign_bit_copies = 1;
1764 return;
1768 /* If this is a complex assignment, see if we can convert it into a
1769 simple assignment. */
1770 set = expand_field_assignment (set);
1772 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1773 set what we know about X. */
1775 if (SET_DEST (set) == x
1776 || (paradoxical_subreg_p (SET_DEST (set))
1777 && SUBREG_REG (SET_DEST (set)) == x))
1778 update_rsp_from_reg_equal (rsp, insn, set, x);
1779 else
1781 rsp->nonzero_bits = GET_MODE_MASK (mode);
1782 rsp->sign_bit_copies = 1;
1787 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1788 optionally insns that were previously combined into I3 or that will be
1789 combined into the merger of INSN and I3. The order is PRED, PRED2,
1790 INSN, SUCC, SUCC2, I3.
1792 Return 0 if the combination is not allowed for any reason.
1794 If the combination is allowed, *PDEST will be set to the single
1795 destination of INSN and *PSRC to the single source, and this function
1796 will return 1. */
1798 static int
1799 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1800 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1801 rtx *pdest, rtx *psrc)
1803 int i;
1804 const_rtx set = 0;
1805 rtx src, dest;
1806 rtx_insn *p;
1807 rtx link;
1808 bool all_adjacent = true;
1809 int (*is_volatile_p) (const_rtx);
1811 if (succ)
1813 if (succ2)
1815 if (next_active_insn (succ2) != i3)
1816 all_adjacent = false;
1817 if (next_active_insn (succ) != succ2)
1818 all_adjacent = false;
1820 else if (next_active_insn (succ) != i3)
1821 all_adjacent = false;
1822 if (next_active_insn (insn) != succ)
1823 all_adjacent = false;
1825 else if (next_active_insn (insn) != i3)
1826 all_adjacent = false;
1828 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1829 or a PARALLEL consisting of such a SET and CLOBBERs.
1831 If INSN has CLOBBER parallel parts, ignore them for our processing.
1832 By definition, these happen during the execution of the insn. When it
1833 is merged with another insn, all bets are off. If they are, in fact,
1834 needed and aren't also supplied in I3, they may be added by
1835 recog_for_combine. Otherwise, it won't match.
1837 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1838 note.
1840 Get the source and destination of INSN. If more than one, can't
1841 combine. */
1843 if (GET_CODE (PATTERN (insn)) == SET)
1844 set = PATTERN (insn);
1845 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1846 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1848 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1850 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1852 switch (GET_CODE (elt))
1854 /* This is important to combine floating point insns
1855 for the SH4 port. */
1856 case USE:
1857 /* Combining an isolated USE doesn't make sense.
1858 We depend here on combinable_i3pat to reject them. */
1859 /* The code below this loop only verifies that the inputs of
1860 the SET in INSN do not change. We call reg_set_between_p
1861 to verify that the REG in the USE does not change between
1862 I3 and INSN.
1863 If the USE in INSN was for a pseudo register, the matching
1864 insn pattern will likely match any register; combining this
1865 with any other USE would only be safe if we knew that the
1866 used registers have identical values, or if there was
1867 something to tell them apart, e.g. different modes. For
1868 now, we forgo such complicated tests and simply disallow
1869 combining of USES of pseudo registers with any other USE. */
1870 if (REG_P (XEXP (elt, 0))
1871 && GET_CODE (PATTERN (i3)) == PARALLEL)
1873 rtx i3pat = PATTERN (i3);
1874 int i = XVECLEN (i3pat, 0) - 1;
1875 unsigned int regno = REGNO (XEXP (elt, 0));
1879 rtx i3elt = XVECEXP (i3pat, 0, i);
1881 if (GET_CODE (i3elt) == USE
1882 && REG_P (XEXP (i3elt, 0))
1883 && (REGNO (XEXP (i3elt, 0)) == regno
1884 ? reg_set_between_p (XEXP (elt, 0),
1885 PREV_INSN (insn), i3)
1886 : regno >= FIRST_PSEUDO_REGISTER))
1887 return 0;
1889 while (--i >= 0);
1891 break;
1893 /* We can ignore CLOBBERs. */
1894 case CLOBBER:
1895 break;
1897 case SET:
1898 /* Ignore SETs whose result isn't used but not those that
1899 have side-effects. */
1900 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1901 && insn_nothrow_p (insn)
1902 && !side_effects_p (elt))
1903 break;
1905 /* If we have already found a SET, this is a second one and
1906 so we cannot combine with this insn. */
1907 if (set)
1908 return 0;
1910 set = elt;
1911 break;
1913 default:
1914 /* Anything else means we can't combine. */
1915 return 0;
1919 if (set == 0
1920 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1921 so don't do anything with it. */
1922 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1923 return 0;
1925 else
1926 return 0;
1928 if (set == 0)
1929 return 0;
1931 /* The simplification in expand_field_assignment may call back to
1932 get_last_value, so set safe guard here. */
1933 subst_low_luid = DF_INSN_LUID (insn);
1935 set = expand_field_assignment (set);
1936 src = SET_SRC (set), dest = SET_DEST (set);
1938 /* Do not eliminate user-specified register if it is in an
1939 asm input because we may break the register asm usage defined
1940 in GCC manual if allow to do so.
1941 Be aware that this may cover more cases than we expect but this
1942 should be harmless. */
1943 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1944 && extract_asm_operands (PATTERN (i3)))
1945 return 0;
1947 /* Don't eliminate a store in the stack pointer. */
1948 if (dest == stack_pointer_rtx
1949 /* Don't combine with an insn that sets a register to itself if it has
1950 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1951 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1952 /* Can't merge an ASM_OPERANDS. */
1953 || GET_CODE (src) == ASM_OPERANDS
1954 /* Can't merge a function call. */
1955 || GET_CODE (src) == CALL
1956 /* Don't eliminate a function call argument. */
1957 || (CALL_P (i3)
1958 && (find_reg_fusage (i3, USE, dest)
1959 || (REG_P (dest)
1960 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1961 && global_regs[REGNO (dest)])))
1962 /* Don't substitute into an incremented register. */
1963 || FIND_REG_INC_NOTE (i3, dest)
1964 || (succ && FIND_REG_INC_NOTE (succ, dest))
1965 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1966 /* Don't substitute into a non-local goto, this confuses CFG. */
1967 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1968 /* Make sure that DEST is not used after INSN but before SUCC, or
1969 after SUCC and before SUCC2, or after SUCC2 but before I3. */
1970 || (!all_adjacent
1971 && ((succ2
1972 && (reg_used_between_p (dest, succ2, i3)
1973 || reg_used_between_p (dest, succ, succ2)))
1974 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1975 || (succ
1976 /* SUCC and SUCC2 can be split halves from a PARALLEL; in
1977 that case SUCC is not in the insn stream, so use SUCC2
1978 instead for this test. */
1979 && reg_used_between_p (dest, insn,
1980 succ2
1981 && INSN_UID (succ) == INSN_UID (succ2)
1982 ? succ2 : succ))))
1983 /* Make sure that the value that is to be substituted for the register
1984 does not use any registers whose values alter in between. However,
1985 If the insns are adjacent, a use can't cross a set even though we
1986 think it might (this can happen for a sequence of insns each setting
1987 the same destination; last_set of that register might point to
1988 a NOTE). If INSN has a REG_EQUIV note, the register is always
1989 equivalent to the memory so the substitution is valid even if there
1990 are intervening stores. Also, don't move a volatile asm or
1991 UNSPEC_VOLATILE across any other insns. */
1992 || (! all_adjacent
1993 && (((!MEM_P (src)
1994 || ! find_reg_note (insn, REG_EQUIV, src))
1995 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1996 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1997 || GET_CODE (src) == UNSPEC_VOLATILE))
1998 /* Don't combine across a CALL_INSN, because that would possibly
1999 change whether the life span of some REGs crosses calls or not,
2000 and it is a pain to update that information.
2001 Exception: if source is a constant, moving it later can't hurt.
2002 Accept that as a special case. */
2003 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
2004 return 0;
2006 /* DEST must either be a REG or CC0. */
2007 if (REG_P (dest))
2009 /* If register alignment is being enforced for multi-word items in all
2010 cases except for parameters, it is possible to have a register copy
2011 insn referencing a hard register that is not allowed to contain the
2012 mode being copied and which would not be valid as an operand of most
2013 insns. Eliminate this problem by not combining with such an insn.
2015 Also, on some machines we don't want to extend the life of a hard
2016 register. */
2018 if (REG_P (src)
2019 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
2020 && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
2021 /* Don't extend the life of a hard register unless it is
2022 user variable (if we have few registers) or it can't
2023 fit into the desired register (meaning something special
2024 is going on).
2025 Also avoid substituting a return register into I3, because
2026 reload can't handle a conflict with constraints of other
2027 inputs. */
2028 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2029 && !targetm.hard_regno_mode_ok (REGNO (src),
2030 GET_MODE (src)))))
2031 return 0;
2033 else if (GET_CODE (dest) != CC0)
2034 return 0;
2037 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2038 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2039 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2041 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2043 /* If the clobber represents an earlyclobber operand, we must not
2044 substitute an expression containing the clobbered register.
2045 As we do not analyze the constraint strings here, we have to
2046 make the conservative assumption. However, if the register is
2047 a fixed hard reg, the clobber cannot represent any operand;
2048 we leave it up to the machine description to either accept or
2049 reject use-and-clobber patterns. */
2050 if (!REG_P (reg)
2051 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2052 || !fixed_regs[REGNO (reg)])
2053 if (reg_overlap_mentioned_p (reg, src))
2054 return 0;
2057 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2058 or not), reject, unless nothing volatile comes between it and I3 */
2060 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2062 /* Make sure neither succ nor succ2 contains a volatile reference. */
2063 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2064 return 0;
2065 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2066 return 0;
2067 /* We'll check insns between INSN and I3 below. */
2070 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2071 to be an explicit register variable, and was chosen for a reason. */
2073 if (GET_CODE (src) == ASM_OPERANDS
2074 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2075 return 0;
2077 /* If INSN contains volatile references (specifically volatile MEMs),
2078 we cannot combine across any other volatile references.
2079 Even if INSN doesn't contain volatile references, any intervening
2080 volatile insn might affect machine state. */
2082 is_volatile_p = volatile_refs_p (PATTERN (insn))
2083 ? volatile_refs_p
2084 : volatile_insn_p;
2086 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2087 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2088 return 0;
2090 /* If INSN contains an autoincrement or autodecrement, make sure that
2091 register is not used between there and I3, and not already used in
2092 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2093 Also insist that I3 not be a jump; if it were one
2094 and the incremented register were spilled, we would lose. */
2096 if (AUTO_INC_DEC)
2097 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2098 if (REG_NOTE_KIND (link) == REG_INC
2099 && (JUMP_P (i3)
2100 || reg_used_between_p (XEXP (link, 0), insn, i3)
2101 || (pred != NULL_RTX
2102 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2103 || (pred2 != NULL_RTX
2104 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2105 || (succ != NULL_RTX
2106 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2107 || (succ2 != NULL_RTX
2108 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2109 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2110 return 0;
2112 /* Don't combine an insn that follows a CC0-setting insn.
2113 An insn that uses CC0 must not be separated from the one that sets it.
2114 We do, however, allow I2 to follow a CC0-setting insn if that insn
2115 is passed as I1; in that case it will be deleted also.
2116 We also allow combining in this case if all the insns are adjacent
2117 because that would leave the two CC0 insns adjacent as well.
2118 It would be more logical to test whether CC0 occurs inside I1 or I2,
2119 but that would be much slower, and this ought to be equivalent. */
2121 if (HAVE_cc0)
2123 p = prev_nonnote_insn (insn);
2124 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2125 && ! all_adjacent)
2126 return 0;
2129 /* If we get here, we have passed all the tests and the combination is
2130 to be allowed. */
2132 *pdest = dest;
2133 *psrc = src;
2135 return 1;
2138 /* LOC is the location within I3 that contains its pattern or the component
2139 of a PARALLEL of the pattern. We validate that it is valid for combining.
2141 One problem is if I3 modifies its output, as opposed to replacing it
2142 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2143 doing so would produce an insn that is not equivalent to the original insns.
2145 Consider:
2147 (set (reg:DI 101) (reg:DI 100))
2148 (set (subreg:SI (reg:DI 101) 0) <foo>)
2150 This is NOT equivalent to:
2152 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2153 (set (reg:DI 101) (reg:DI 100))])
2155 Not only does this modify 100 (in which case it might still be valid
2156 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2158 We can also run into a problem if I2 sets a register that I1
2159 uses and I1 gets directly substituted into I3 (not via I2). In that
2160 case, we would be getting the wrong value of I2DEST into I3, so we
2161 must reject the combination. This case occurs when I2 and I1 both
2162 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2163 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2164 of a SET must prevent combination from occurring. The same situation
2165 can occur for I0, in which case I0_NOT_IN_SRC is set.
2167 Before doing the above check, we first try to expand a field assignment
2168 into a set of logical operations.
2170 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2171 we place a register that is both set and used within I3. If more than one
2172 such register is detected, we fail.
2174 Return 1 if the combination is valid, zero otherwise. */
2176 static int
2177 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2178 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2180 rtx x = *loc;
2182 if (GET_CODE (x) == SET)
2184 rtx set = x ;
2185 rtx dest = SET_DEST (set);
2186 rtx src = SET_SRC (set);
2187 rtx inner_dest = dest;
2188 rtx subdest;
2190 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2191 || GET_CODE (inner_dest) == SUBREG
2192 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2193 inner_dest = XEXP (inner_dest, 0);
2195 /* Check for the case where I3 modifies its output, as discussed
2196 above. We don't want to prevent pseudos from being combined
2197 into the address of a MEM, so only prevent the combination if
2198 i1 or i2 set the same MEM. */
2199 if ((inner_dest != dest &&
2200 (!MEM_P (inner_dest)
2201 || rtx_equal_p (i2dest, inner_dest)
2202 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2203 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2204 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2205 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2206 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2208 /* This is the same test done in can_combine_p except we can't test
2209 all_adjacent; we don't have to, since this instruction will stay
2210 in place, thus we are not considering increasing the lifetime of
2211 INNER_DEST.
2213 Also, if this insn sets a function argument, combining it with
2214 something that might need a spill could clobber a previous
2215 function argument; the all_adjacent test in can_combine_p also
2216 checks this; here, we do a more specific test for this case. */
2218 || (REG_P (inner_dest)
2219 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2220 && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2221 GET_MODE (inner_dest)))
2222 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2223 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2224 return 0;
2226 /* If DEST is used in I3, it is being killed in this insn, so
2227 record that for later. We have to consider paradoxical
2228 subregs here, since they kill the whole register, but we
2229 ignore partial subregs, STRICT_LOW_PART, etc.
2230 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2231 STACK_POINTER_REGNUM, since these are always considered to be
2232 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2233 subdest = dest;
2234 if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2235 subdest = SUBREG_REG (subdest);
2236 if (pi3dest_killed
2237 && REG_P (subdest)
2238 && reg_referenced_p (subdest, PATTERN (i3))
2239 && REGNO (subdest) != FRAME_POINTER_REGNUM
2240 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2241 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2242 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2243 || (REGNO (subdest) != ARG_POINTER_REGNUM
2244 || ! fixed_regs [REGNO (subdest)]))
2245 && REGNO (subdest) != STACK_POINTER_REGNUM)
2247 if (*pi3dest_killed)
2248 return 0;
2250 *pi3dest_killed = subdest;
2254 else if (GET_CODE (x) == PARALLEL)
2256 int i;
2258 for (i = 0; i < XVECLEN (x, 0); i++)
2259 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2260 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2261 return 0;
2264 return 1;
2267 /* Return 1 if X is an arithmetic expression that contains a multiplication
2268 and division. We don't count multiplications by powers of two here. */
2270 static int
2271 contains_muldiv (rtx x)
2273 switch (GET_CODE (x))
2275 case MOD: case DIV: case UMOD: case UDIV:
2276 return 1;
2278 case MULT:
2279 return ! (CONST_INT_P (XEXP (x, 1))
2280 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2281 default:
2282 if (BINARY_P (x))
2283 return contains_muldiv (XEXP (x, 0))
2284 || contains_muldiv (XEXP (x, 1));
2286 if (UNARY_P (x))
2287 return contains_muldiv (XEXP (x, 0));
2289 return 0;
2293 /* Determine whether INSN can be used in a combination. Return nonzero if
2294 not. This is used in try_combine to detect early some cases where we
2295 can't perform combinations. */
2297 static int
2298 cant_combine_insn_p (rtx_insn *insn)
2300 rtx set;
2301 rtx src, dest;
2303 /* If this isn't really an insn, we can't do anything.
2304 This can occur when flow deletes an insn that it has merged into an
2305 auto-increment address. */
2306 if (!NONDEBUG_INSN_P (insn))
2307 return 1;
2309 /* Never combine loads and stores involving hard regs that are likely
2310 to be spilled. The register allocator can usually handle such
2311 reg-reg moves by tying. If we allow the combiner to make
2312 substitutions of likely-spilled regs, reload might die.
2313 As an exception, we allow combinations involving fixed regs; these are
2314 not available to the register allocator so there's no risk involved. */
2316 set = single_set (insn);
2317 if (! set)
2318 return 0;
2319 src = SET_SRC (set);
2320 dest = SET_DEST (set);
2321 if (GET_CODE (src) == SUBREG)
2322 src = SUBREG_REG (src);
2323 if (GET_CODE (dest) == SUBREG)
2324 dest = SUBREG_REG (dest);
2325 if (REG_P (src) && REG_P (dest)
2326 && ((HARD_REGISTER_P (src)
2327 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2328 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2329 || (HARD_REGISTER_P (dest)
2330 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2331 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2332 return 1;
2334 return 0;
2337 struct likely_spilled_retval_info
2339 unsigned regno, nregs;
2340 unsigned mask;
2343 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2344 hard registers that are known to be written to / clobbered in full. */
2345 static void
2346 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2348 struct likely_spilled_retval_info *const info =
2349 (struct likely_spilled_retval_info *) data;
2350 unsigned regno, nregs;
2351 unsigned new_mask;
2353 if (!REG_P (XEXP (set, 0)))
2354 return;
2355 regno = REGNO (x);
2356 if (regno >= info->regno + info->nregs)
2357 return;
2358 nregs = REG_NREGS (x);
2359 if (regno + nregs <= info->regno)
2360 return;
2361 new_mask = (2U << (nregs - 1)) - 1;
2362 if (regno < info->regno)
2363 new_mask >>= info->regno - regno;
2364 else
2365 new_mask <<= regno - info->regno;
2366 info->mask &= ~new_mask;
2369 /* Return nonzero iff part of the return value is live during INSN, and
2370 it is likely spilled. This can happen when more than one insn is needed
2371 to copy the return value, e.g. when we consider to combine into the
2372 second copy insn for a complex value. */
2374 static int
2375 likely_spilled_retval_p (rtx_insn *insn)
2377 rtx_insn *use = BB_END (this_basic_block);
2378 rtx reg;
2379 rtx_insn *p;
2380 unsigned regno, nregs;
2381 /* We assume here that no machine mode needs more than
2382 32 hard registers when the value overlaps with a register
2383 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2384 unsigned mask;
2385 struct likely_spilled_retval_info info;
2387 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2388 return 0;
2389 reg = XEXP (PATTERN (use), 0);
2390 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2391 return 0;
2392 regno = REGNO (reg);
2393 nregs = REG_NREGS (reg);
2394 if (nregs == 1)
2395 return 0;
2396 mask = (2U << (nregs - 1)) - 1;
2398 /* Disregard parts of the return value that are set later. */
2399 info.regno = regno;
2400 info.nregs = nregs;
2401 info.mask = mask;
2402 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2403 if (INSN_P (p))
2404 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2405 mask = info.mask;
2407 /* Check if any of the (probably) live return value registers is
2408 likely spilled. */
2409 nregs --;
2412 if ((mask & 1 << nregs)
2413 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2414 return 1;
2415 } while (nregs--);
2416 return 0;
2419 /* Adjust INSN after we made a change to its destination.
2421 Changing the destination can invalidate notes that say something about
2422 the results of the insn and a LOG_LINK pointing to the insn. */
2424 static void
2425 adjust_for_new_dest (rtx_insn *insn)
2427 /* For notes, be conservative and simply remove them. */
2428 remove_reg_equal_equiv_notes (insn);
2430 /* The new insn will have a destination that was previously the destination
2431 of an insn just above it. Call distribute_links to make a LOG_LINK from
2432 the next use of that destination. */
2434 rtx set = single_set (insn);
2435 gcc_assert (set);
2437 rtx reg = SET_DEST (set);
2439 while (GET_CODE (reg) == ZERO_EXTRACT
2440 || GET_CODE (reg) == STRICT_LOW_PART
2441 || GET_CODE (reg) == SUBREG)
2442 reg = XEXP (reg, 0);
2443 gcc_assert (REG_P (reg));
2445 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2447 df_insn_rescan (insn);
2450 /* Return TRUE if combine can reuse reg X in mode MODE.
2451 ADDED_SETS is nonzero if the original set is still required. */
2452 static bool
2453 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2455 unsigned int regno;
2457 if (!REG_P (x))
2458 return false;
2460 regno = REGNO (x);
2461 /* Allow hard registers if the new mode is legal, and occupies no more
2462 registers than the old mode. */
2463 if (regno < FIRST_PSEUDO_REGISTER)
2464 return (targetm.hard_regno_mode_ok (regno, mode)
2465 && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2467 /* Or a pseudo that is only used once. */
2468 return (regno < reg_n_sets_max
2469 && REG_N_SETS (regno) == 1
2470 && !added_sets
2471 && !REG_USERVAR_P (x));
2475 /* Check whether X, the destination of a set, refers to part of
2476 the register specified by REG. */
2478 static bool
2479 reg_subword_p (rtx x, rtx reg)
2481 /* Check that reg is an integer mode register. */
2482 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2483 return false;
2485 if (GET_CODE (x) == STRICT_LOW_PART
2486 || GET_CODE (x) == ZERO_EXTRACT)
2487 x = XEXP (x, 0);
2489 return GET_CODE (x) == SUBREG
2490 && SUBREG_REG (x) == reg
2491 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2494 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2495 Note that the INSN should be deleted *after* removing dead edges, so
2496 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2497 but not for a (set (pc) (label_ref FOO)). */
2499 static void
2500 update_cfg_for_uncondjump (rtx_insn *insn)
2502 basic_block bb = BLOCK_FOR_INSN (insn);
2503 gcc_assert (BB_END (bb) == insn);
2505 purge_dead_edges (bb);
2507 delete_insn (insn);
2508 if (EDGE_COUNT (bb->succs) == 1)
2510 rtx_insn *insn;
2512 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2514 /* Remove barriers from the footer if there are any. */
2515 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2516 if (BARRIER_P (insn))
2518 if (PREV_INSN (insn))
2519 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2520 else
2521 BB_FOOTER (bb) = NEXT_INSN (insn);
2522 if (NEXT_INSN (insn))
2523 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2525 else if (LABEL_P (insn))
2526 break;
2530 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2531 by an arbitrary number of CLOBBERs. */
2532 static bool
2533 is_parallel_of_n_reg_sets (rtx pat, int n)
2535 if (GET_CODE (pat) != PARALLEL)
2536 return false;
2538 int len = XVECLEN (pat, 0);
2539 if (len < n)
2540 return false;
2542 int i;
2543 for (i = 0; i < n; i++)
2544 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2545 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2546 return false;
2547 for ( ; i < len; i++)
2548 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER
2549 || XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2550 return false;
2552 return true;
2555 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2556 CLOBBERs), can be split into individual SETs in that order, without
2557 changing semantics. */
2558 static bool
2559 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2561 if (!insn_nothrow_p (insn))
2562 return false;
2564 rtx pat = PATTERN (insn);
2566 int i, j;
2567 for (i = 0; i < n; i++)
2569 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2570 return false;
2572 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2574 for (j = i + 1; j < n; j++)
2575 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2576 return false;
2579 return true;
2582 /* Try to combine the insns I0, I1 and I2 into I3.
2583 Here I0, I1 and I2 appear earlier than I3.
2584 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2587 If we are combining more than two insns and the resulting insn is not
2588 recognized, try splitting it into two insns. If that happens, I2 and I3
2589 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2590 Otherwise, I0, I1 and I2 are pseudo-deleted.
2592 Return 0 if the combination does not work. Then nothing is changed.
2593 If we did the combination, return the insn at which combine should
2594 resume scanning.
2596 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2597 new direct jump instruction.
2599 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2600 been I3 passed to an earlier try_combine within the same basic
2601 block. */
2603 static rtx_insn *
2604 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2605 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2607 /* New patterns for I3 and I2, respectively. */
2608 rtx newpat, newi2pat = 0;
2609 rtvec newpat_vec_with_clobbers = 0;
2610 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2611 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2612 dead. */
2613 int added_sets_0, added_sets_1, added_sets_2;
2614 /* Total number of SETs to put into I3. */
2615 int total_sets;
2616 /* Nonzero if I2's or I1's body now appears in I3. */
2617 int i2_is_used = 0, i1_is_used = 0;
2618 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2619 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2620 /* Contains I3 if the destination of I3 is used in its source, which means
2621 that the old life of I3 is being killed. If that usage is placed into
2622 I2 and not in I3, a REG_DEAD note must be made. */
2623 rtx i3dest_killed = 0;
2624 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2625 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2626 /* Copy of SET_SRC of I1 and I0, if needed. */
2627 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2628 /* Set if I2DEST was reused as a scratch register. */
2629 bool i2scratch = false;
2630 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2631 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2632 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2633 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2634 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2635 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2636 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2637 /* Notes that must be added to REG_NOTES in I3 and I2. */
2638 rtx new_i3_notes, new_i2_notes;
2639 /* Notes that we substituted I3 into I2 instead of the normal case. */
2640 int i3_subst_into_i2 = 0;
2641 /* Notes that I1, I2 or I3 is a MULT operation. */
2642 int have_mult = 0;
2643 int swap_i2i3 = 0;
2644 int changed_i3_dest = 0;
2646 int maxreg;
2647 rtx_insn *temp_insn;
2648 rtx temp_expr;
2649 struct insn_link *link;
2650 rtx other_pat = 0;
2651 rtx new_other_notes;
2652 int i;
2653 scalar_int_mode dest_mode, temp_mode;
2655 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2656 never be). */
2657 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2658 return 0;
2660 /* Only try four-insn combinations when there's high likelihood of
2661 success. Look for simple insns, such as loads of constants or
2662 binary operations involving a constant. */
2663 if (i0)
2665 int i;
2666 int ngood = 0;
2667 int nshift = 0;
2668 rtx set0, set3;
2670 if (!flag_expensive_optimizations)
2671 return 0;
2673 for (i = 0; i < 4; i++)
2675 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2676 rtx set = single_set (insn);
2677 rtx src;
2678 if (!set)
2679 continue;
2680 src = SET_SRC (set);
2681 if (CONSTANT_P (src))
2683 ngood += 2;
2684 break;
2686 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2687 ngood++;
2688 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2689 || GET_CODE (src) == LSHIFTRT)
2690 nshift++;
2693 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2694 are likely manipulating its value. Ideally we'll be able to combine
2695 all four insns into a bitfield insertion of some kind.
2697 Note the source in I0 might be inside a sign/zero extension and the
2698 memory modes in I0 and I3 might be different. So extract the address
2699 from the destination of I3 and search for it in the source of I0.
2701 In the event that there's a match but the source/dest do not actually
2702 refer to the same memory, the worst that happens is we try some
2703 combinations that we wouldn't have otherwise. */
2704 if ((set0 = single_set (i0))
2705 /* Ensure the source of SET0 is a MEM, possibly buried inside
2706 an extension. */
2707 && (GET_CODE (SET_SRC (set0)) == MEM
2708 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2709 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2710 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2711 && (set3 = single_set (i3))
2712 /* Ensure the destination of SET3 is a MEM. */
2713 && GET_CODE (SET_DEST (set3)) == MEM
2714 /* Would it be better to extract the base address for the MEM
2715 in SET3 and look for that? I don't have cases where it matters
2716 but I could envision such cases. */
2717 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2718 ngood += 2;
2720 if (ngood < 2 && nshift < 2)
2721 return 0;
2724 /* Exit early if one of the insns involved can't be used for
2725 combinations. */
2726 if (CALL_P (i2)
2727 || (i1 && CALL_P (i1))
2728 || (i0 && CALL_P (i0))
2729 || cant_combine_insn_p (i3)
2730 || cant_combine_insn_p (i2)
2731 || (i1 && cant_combine_insn_p (i1))
2732 || (i0 && cant_combine_insn_p (i0))
2733 || likely_spilled_retval_p (i3))
2734 return 0;
2736 combine_attempts++;
2737 undobuf.other_insn = 0;
2739 /* Reset the hard register usage information. */
2740 CLEAR_HARD_REG_SET (newpat_used_regs);
2742 if (dump_file && (dump_flags & TDF_DETAILS))
2744 if (i0)
2745 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2746 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2747 else if (i1)
2748 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2749 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2750 else
2751 fprintf (dump_file, "\nTrying %d -> %d:\n",
2752 INSN_UID (i2), INSN_UID (i3));
2755 /* If multiple insns feed into one of I2 or I3, they can be in any
2756 order. To simplify the code below, reorder them in sequence. */
2757 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2758 std::swap (i0, i2);
2759 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2760 std::swap (i0, i1);
2761 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2762 std::swap (i1, i2);
2764 added_links_insn = 0;
2766 /* First check for one important special case that the code below will
2767 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2768 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2769 we may be able to replace that destination with the destination of I3.
2770 This occurs in the common code where we compute both a quotient and
2771 remainder into a structure, in which case we want to do the computation
2772 directly into the structure to avoid register-register copies.
2774 Note that this case handles both multiple sets in I2 and also cases
2775 where I2 has a number of CLOBBERs inside the PARALLEL.
2777 We make very conservative checks below and only try to handle the
2778 most common cases of this. For example, we only handle the case
2779 where I2 and I3 are adjacent to avoid making difficult register
2780 usage tests. */
2782 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2783 && REG_P (SET_SRC (PATTERN (i3)))
2784 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2785 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2786 && GET_CODE (PATTERN (i2)) == PARALLEL
2787 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2788 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2789 below would need to check what is inside (and reg_overlap_mentioned_p
2790 doesn't support those codes anyway). Don't allow those destinations;
2791 the resulting insn isn't likely to be recognized anyway. */
2792 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2793 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2794 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2795 SET_DEST (PATTERN (i3)))
2796 && next_active_insn (i2) == i3)
2798 rtx p2 = PATTERN (i2);
2800 /* Make sure that the destination of I3,
2801 which we are going to substitute into one output of I2,
2802 is not used within another output of I2. We must avoid making this:
2803 (parallel [(set (mem (reg 69)) ...)
2804 (set (reg 69) ...)])
2805 which is not well-defined as to order of actions.
2806 (Besides, reload can't handle output reloads for this.)
2808 The problem can also happen if the dest of I3 is a memory ref,
2809 if another dest in I2 is an indirect memory ref.
2811 Neither can this PARALLEL be an asm. We do not allow combining
2812 that usually (see can_combine_p), so do not here either. */
2813 bool ok = true;
2814 for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2816 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2817 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2818 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2819 SET_DEST (XVECEXP (p2, 0, i))))
2820 ok = false;
2821 else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2822 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2823 ok = false;
2826 if (ok)
2827 for (i = 0; i < XVECLEN (p2, 0); i++)
2828 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2829 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2831 combine_merges++;
2833 subst_insn = i3;
2834 subst_low_luid = DF_INSN_LUID (i2);
2836 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2837 i2src = SET_SRC (XVECEXP (p2, 0, i));
2838 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2839 i2dest_killed = dead_or_set_p (i2, i2dest);
2841 /* Replace the dest in I2 with our dest and make the resulting
2842 insn the new pattern for I3. Then skip to where we validate
2843 the pattern. Everything was set up above. */
2844 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2845 newpat = p2;
2846 i3_subst_into_i2 = 1;
2847 goto validate_replacement;
2851 /* If I2 is setting a pseudo to a constant and I3 is setting some
2852 sub-part of it to another constant, merge them by making a new
2853 constant. */
2854 if (i1 == 0
2855 && (temp_expr = single_set (i2)) != 0
2856 && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2857 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2858 && GET_CODE (PATTERN (i3)) == SET
2859 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2860 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2862 rtx dest = SET_DEST (PATTERN (i3));
2863 rtx temp_dest = SET_DEST (temp_expr);
2864 int offset = -1;
2865 int width = 0;
2867 if (GET_CODE (dest) == ZERO_EXTRACT)
2869 if (CONST_INT_P (XEXP (dest, 1))
2870 && CONST_INT_P (XEXP (dest, 2))
2871 && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2872 &dest_mode))
2874 width = INTVAL (XEXP (dest, 1));
2875 offset = INTVAL (XEXP (dest, 2));
2876 dest = XEXP (dest, 0);
2877 if (BITS_BIG_ENDIAN)
2878 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2881 else
2883 if (GET_CODE (dest) == STRICT_LOW_PART)
2884 dest = XEXP (dest, 0);
2885 if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2887 width = GET_MODE_PRECISION (dest_mode);
2888 offset = 0;
2892 if (offset >= 0)
2894 /* If this is the low part, we're done. */
2895 if (subreg_lowpart_p (dest))
2897 /* Handle the case where inner is twice the size of outer. */
2898 else if (GET_MODE_PRECISION (temp_mode)
2899 == 2 * GET_MODE_PRECISION (dest_mode))
2900 offset += GET_MODE_PRECISION (dest_mode);
2901 /* Otherwise give up for now. */
2902 else
2903 offset = -1;
2906 if (offset >= 0)
2908 rtx inner = SET_SRC (PATTERN (i3));
2909 rtx outer = SET_SRC (temp_expr);
2911 wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2912 rtx_mode_t (inner, dest_mode),
2913 offset, width);
2915 combine_merges++;
2916 subst_insn = i3;
2917 subst_low_luid = DF_INSN_LUID (i2);
2918 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2919 i2dest = temp_dest;
2920 i2dest_killed = dead_or_set_p (i2, i2dest);
2922 /* Replace the source in I2 with the new constant and make the
2923 resulting insn the new pattern for I3. Then skip to where we
2924 validate the pattern. Everything was set up above. */
2925 SUBST (SET_SRC (temp_expr),
2926 immed_wide_int_const (o, temp_mode));
2928 newpat = PATTERN (i2);
2930 /* The dest of I3 has been replaced with the dest of I2. */
2931 changed_i3_dest = 1;
2932 goto validate_replacement;
2936 /* If we have no I1 and I2 looks like:
2937 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2938 (set Y OP)])
2939 make up a dummy I1 that is
2940 (set Y OP)
2941 and change I2 to be
2942 (set (reg:CC X) (compare:CC Y (const_int 0)))
2944 (We can ignore any trailing CLOBBERs.)
2946 This undoes a previous combination and allows us to match a branch-and-
2947 decrement insn. */
2949 if (!HAVE_cc0 && i1 == 0
2950 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2951 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2952 == MODE_CC)
2953 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2954 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2955 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2956 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2957 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2958 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2960 /* We make I1 with the same INSN_UID as I2. This gives it
2961 the same DF_INSN_LUID for value tracking. Our fake I1 will
2962 never appear in the insn stream so giving it the same INSN_UID
2963 as I2 will not cause a problem. */
2965 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2966 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2967 -1, NULL_RTX);
2968 INSN_UID (i1) = INSN_UID (i2);
2970 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2971 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2972 SET_DEST (PATTERN (i1)));
2973 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2974 SUBST_LINK (LOG_LINKS (i2),
2975 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2978 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2979 make those two SETs separate I1 and I2 insns, and make an I0 that is
2980 the original I1. */
2981 if (!HAVE_cc0 && i0 == 0
2982 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2983 && can_split_parallel_of_n_reg_sets (i2, 2)
2984 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2985 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2987 /* If there is no I1, there is no I0 either. */
2988 i0 = i1;
2990 /* We make I1 with the same INSN_UID as I2. This gives it
2991 the same DF_INSN_LUID for value tracking. Our fake I1 will
2992 never appear in the insn stream so giving it the same INSN_UID
2993 as I2 will not cause a problem. */
2995 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2996 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2997 -1, NULL_RTX);
2998 INSN_UID (i1) = INSN_UID (i2);
3000 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
3003 /* Verify that I2 and I1 are valid for combining. */
3004 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
3005 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
3006 &i1dest, &i1src))
3007 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
3008 &i0dest, &i0src)))
3010 undo_all ();
3011 return 0;
3014 /* Record whether I2DEST is used in I2SRC and similarly for the other
3015 cases. Knowing this will help in register status updating below. */
3016 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3017 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3018 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3019 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3020 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3021 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3022 i2dest_killed = dead_or_set_p (i2, i2dest);
3023 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3024 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3026 /* For the earlier insns, determine which of the subsequent ones they
3027 feed. */
3028 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3029 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3030 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3031 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3032 && reg_overlap_mentioned_p (i0dest, i2src))));
3034 /* Ensure that I3's pattern can be the destination of combines. */
3035 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3036 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3037 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3038 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3039 &i3dest_killed))
3041 undo_all ();
3042 return 0;
3045 /* See if any of the insns is a MULT operation. Unless one is, we will
3046 reject a combination that is, since it must be slower. Be conservative
3047 here. */
3048 if (GET_CODE (i2src) == MULT
3049 || (i1 != 0 && GET_CODE (i1src) == MULT)
3050 || (i0 != 0 && GET_CODE (i0src) == MULT)
3051 || (GET_CODE (PATTERN (i3)) == SET
3052 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3053 have_mult = 1;
3055 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3056 We used to do this EXCEPT in one case: I3 has a post-inc in an
3057 output operand. However, that exception can give rise to insns like
3058 mov r3,(r3)+
3059 which is a famous insn on the PDP-11 where the value of r3 used as the
3060 source was model-dependent. Avoid this sort of thing. */
3062 #if 0
3063 if (!(GET_CODE (PATTERN (i3)) == SET
3064 && REG_P (SET_SRC (PATTERN (i3)))
3065 && MEM_P (SET_DEST (PATTERN (i3)))
3066 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3067 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3068 /* It's not the exception. */
3069 #endif
3070 if (AUTO_INC_DEC)
3072 rtx link;
3073 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3074 if (REG_NOTE_KIND (link) == REG_INC
3075 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3076 || (i1 != 0
3077 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3079 undo_all ();
3080 return 0;
3084 /* See if the SETs in I1 or I2 need to be kept around in the merged
3085 instruction: whenever the value set there is still needed past I3.
3086 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3088 For the SET in I1, we have two cases: if I1 and I2 independently feed
3089 into I3, the set in I1 needs to be kept around unless I1DEST dies
3090 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3091 in I1 needs to be kept around unless I1DEST dies or is set in either
3092 I2 or I3. The same considerations apply to I0. */
3094 added_sets_2 = !dead_or_set_p (i3, i2dest);
3096 if (i1)
3097 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3098 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3099 else
3100 added_sets_1 = 0;
3102 if (i0)
3103 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3104 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3105 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3106 && dead_or_set_p (i2, i0dest)));
3107 else
3108 added_sets_0 = 0;
3110 /* We are about to copy insns for the case where they need to be kept
3111 around. Check that they can be copied in the merged instruction. */
3113 if (targetm.cannot_copy_insn_p
3114 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3115 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3116 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3118 undo_all ();
3119 return 0;
3122 /* If the set in I2 needs to be kept around, we must make a copy of
3123 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3124 PATTERN (I2), we are only substituting for the original I1DEST, not into
3125 an already-substituted copy. This also prevents making self-referential
3126 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3127 I2DEST. */
3129 if (added_sets_2)
3131 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3132 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3133 else
3134 i2pat = copy_rtx (PATTERN (i2));
3137 if (added_sets_1)
3139 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3140 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3141 else
3142 i1pat = copy_rtx (PATTERN (i1));
3145 if (added_sets_0)
3147 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3148 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3149 else
3150 i0pat = copy_rtx (PATTERN (i0));
3153 combine_merges++;
3155 /* Substitute in the latest insn for the regs set by the earlier ones. */
3157 maxreg = max_reg_num ();
3159 subst_insn = i3;
3161 /* Many machines that don't use CC0 have insns that can both perform an
3162 arithmetic operation and set the condition code. These operations will
3163 be represented as a PARALLEL with the first element of the vector
3164 being a COMPARE of an arithmetic operation with the constant zero.
3165 The second element of the vector will set some pseudo to the result
3166 of the same arithmetic operation. If we simplify the COMPARE, we won't
3167 match such a pattern and so will generate an extra insn. Here we test
3168 for this case, where both the comparison and the operation result are
3169 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3170 I2SRC. Later we will make the PARALLEL that contains I2. */
3172 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3173 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3174 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3175 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3177 rtx newpat_dest;
3178 rtx *cc_use_loc = NULL;
3179 rtx_insn *cc_use_insn = NULL;
3180 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3181 machine_mode compare_mode, orig_compare_mode;
3182 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3183 scalar_int_mode mode;
3185 newpat = PATTERN (i3);
3186 newpat_dest = SET_DEST (newpat);
3187 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3189 if (undobuf.other_insn == 0
3190 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3191 &cc_use_insn)))
3193 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3194 if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3195 compare_code = simplify_compare_const (compare_code, mode,
3196 op0, &op1);
3197 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3200 /* Do the rest only if op1 is const0_rtx, which may be the
3201 result of simplification. */
3202 if (op1 == const0_rtx)
3204 /* If a single use of the CC is found, prepare to modify it
3205 when SELECT_CC_MODE returns a new CC-class mode, or when
3206 the above simplify_compare_const() returned a new comparison
3207 operator. undobuf.other_insn is assigned the CC use insn
3208 when modifying it. */
3209 if (cc_use_loc)
3211 #ifdef SELECT_CC_MODE
3212 machine_mode new_mode
3213 = SELECT_CC_MODE (compare_code, op0, op1);
3214 if (new_mode != orig_compare_mode
3215 && can_change_dest_mode (SET_DEST (newpat),
3216 added_sets_2, new_mode))
3218 unsigned int regno = REGNO (newpat_dest);
3219 compare_mode = new_mode;
3220 if (regno < FIRST_PSEUDO_REGISTER)
3221 newpat_dest = gen_rtx_REG (compare_mode, regno);
3222 else
3224 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3225 newpat_dest = regno_reg_rtx[regno];
3228 #endif
3229 /* Cases for modifying the CC-using comparison. */
3230 if (compare_code != orig_compare_code
3231 /* ??? Do we need to verify the zero rtx? */
3232 && XEXP (*cc_use_loc, 1) == const0_rtx)
3234 /* Replace cc_use_loc with entire new RTX. */
3235 SUBST (*cc_use_loc,
3236 gen_rtx_fmt_ee (compare_code, compare_mode,
3237 newpat_dest, const0_rtx));
3238 undobuf.other_insn = cc_use_insn;
3240 else if (compare_mode != orig_compare_mode)
3242 /* Just replace the CC reg with a new mode. */
3243 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3244 undobuf.other_insn = cc_use_insn;
3248 /* Now we modify the current newpat:
3249 First, SET_DEST(newpat) is updated if the CC mode has been
3250 altered. For targets without SELECT_CC_MODE, this should be
3251 optimized away. */
3252 if (compare_mode != orig_compare_mode)
3253 SUBST (SET_DEST (newpat), newpat_dest);
3254 /* This is always done to propagate i2src into newpat. */
3255 SUBST (SET_SRC (newpat),
3256 gen_rtx_COMPARE (compare_mode, op0, op1));
3257 /* Create new version of i2pat if needed; the below PARALLEL
3258 creation needs this to work correctly. */
3259 if (! rtx_equal_p (i2src, op0))
3260 i2pat = gen_rtx_SET (i2dest, op0);
3261 i2_is_used = 1;
3265 if (i2_is_used == 0)
3267 /* It is possible that the source of I2 or I1 may be performing
3268 an unneeded operation, such as a ZERO_EXTEND of something
3269 that is known to have the high part zero. Handle that case
3270 by letting subst look at the inner insns.
3272 Another way to do this would be to have a function that tries
3273 to simplify a single insn instead of merging two or more
3274 insns. We don't do this because of the potential of infinite
3275 loops and because of the potential extra memory required.
3276 However, doing it the way we are is a bit of a kludge and
3277 doesn't catch all cases.
3279 But only do this if -fexpensive-optimizations since it slows
3280 things down and doesn't usually win.
3282 This is not done in the COMPARE case above because the
3283 unmodified I2PAT is used in the PARALLEL and so a pattern
3284 with a modified I2SRC would not match. */
3286 if (flag_expensive_optimizations)
3288 /* Pass pc_rtx so no substitutions are done, just
3289 simplifications. */
3290 if (i1)
3292 subst_low_luid = DF_INSN_LUID (i1);
3293 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3296 subst_low_luid = DF_INSN_LUID (i2);
3297 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3300 n_occurrences = 0; /* `subst' counts here */
3301 subst_low_luid = DF_INSN_LUID (i2);
3303 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3304 copy of I2SRC each time we substitute it, in order to avoid creating
3305 self-referential RTL when we will be substituting I1SRC for I1DEST
3306 later. Likewise if I0 feeds into I2, either directly or indirectly
3307 through I1, and I0DEST is in I0SRC. */
3308 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3309 (i1_feeds_i2_n && i1dest_in_i1src)
3310 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3311 && i0dest_in_i0src));
3312 substed_i2 = 1;
3314 /* Record whether I2's body now appears within I3's body. */
3315 i2_is_used = n_occurrences;
3318 /* If we already got a failure, don't try to do more. Otherwise, try to
3319 substitute I1 if we have it. */
3321 if (i1 && GET_CODE (newpat) != CLOBBER)
3323 /* Check that an autoincrement side-effect on I1 has not been lost.
3324 This happens if I1DEST is mentioned in I2 and dies there, and
3325 has disappeared from the new pattern. */
3326 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3327 && i1_feeds_i2_n
3328 && dead_or_set_p (i2, i1dest)
3329 && !reg_overlap_mentioned_p (i1dest, newpat))
3330 /* Before we can do this substitution, we must redo the test done
3331 above (see detailed comments there) that ensures I1DEST isn't
3332 mentioned in any SETs in NEWPAT that are field assignments. */
3333 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3334 0, 0, 0))
3336 undo_all ();
3337 return 0;
3340 n_occurrences = 0;
3341 subst_low_luid = DF_INSN_LUID (i1);
3343 /* If the following substitution will modify I1SRC, make a copy of it
3344 for the case where it is substituted for I1DEST in I2PAT later. */
3345 if (added_sets_2 && i1_feeds_i2_n)
3346 i1src_copy = copy_rtx (i1src);
3348 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3349 copy of I1SRC each time we substitute it, in order to avoid creating
3350 self-referential RTL when we will be substituting I0SRC for I0DEST
3351 later. */
3352 newpat = subst (newpat, i1dest, i1src, 0, 0,
3353 i0_feeds_i1_n && i0dest_in_i0src);
3354 substed_i1 = 1;
3356 /* Record whether I1's body now appears within I3's body. */
3357 i1_is_used = n_occurrences;
3360 /* Likewise for I0 if we have it. */
3362 if (i0 && GET_CODE (newpat) != CLOBBER)
3364 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3365 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3366 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3367 && !reg_overlap_mentioned_p (i0dest, newpat))
3368 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3369 0, 0, 0))
3371 undo_all ();
3372 return 0;
3375 /* If the following substitution will modify I0SRC, make a copy of it
3376 for the case where it is substituted for I0DEST in I1PAT later. */
3377 if (added_sets_1 && i0_feeds_i1_n)
3378 i0src_copy = copy_rtx (i0src);
3379 /* And a copy for I0DEST in I2PAT substitution. */
3380 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3381 || (i0_feeds_i2_n)))
3382 i0src_copy2 = copy_rtx (i0src);
3384 n_occurrences = 0;
3385 subst_low_luid = DF_INSN_LUID (i0);
3386 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3387 substed_i0 = 1;
3390 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3391 to count all the ways that I2SRC and I1SRC can be used. */
3392 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3393 && i2_is_used + added_sets_2 > 1)
3394 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3395 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3396 > 1))
3397 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3398 && (n_occurrences + added_sets_0
3399 + (added_sets_1 && i0_feeds_i1_n)
3400 + (added_sets_2 && i0_feeds_i2_n)
3401 > 1))
3402 /* Fail if we tried to make a new register. */
3403 || max_reg_num () != maxreg
3404 /* Fail if we couldn't do something and have a CLOBBER. */
3405 || GET_CODE (newpat) == CLOBBER
3406 /* Fail if this new pattern is a MULT and we didn't have one before
3407 at the outer level. */
3408 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3409 && ! have_mult))
3411 undo_all ();
3412 return 0;
3415 /* If the actions of the earlier insns must be kept
3416 in addition to substituting them into the latest one,
3417 we must make a new PARALLEL for the latest insn
3418 to hold additional the SETs. */
3420 if (added_sets_0 || added_sets_1 || added_sets_2)
3422 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3423 combine_extras++;
3425 if (GET_CODE (newpat) == PARALLEL)
3427 rtvec old = XVEC (newpat, 0);
3428 total_sets = XVECLEN (newpat, 0) + extra_sets;
3429 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3430 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3431 sizeof (old->elem[0]) * old->num_elem);
3433 else
3435 rtx old = newpat;
3436 total_sets = 1 + extra_sets;
3437 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3438 XVECEXP (newpat, 0, 0) = old;
3441 if (added_sets_0)
3442 XVECEXP (newpat, 0, --total_sets) = i0pat;
3444 if (added_sets_1)
3446 rtx t = i1pat;
3447 if (i0_feeds_i1_n)
3448 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3450 XVECEXP (newpat, 0, --total_sets) = t;
3452 if (added_sets_2)
3454 rtx t = i2pat;
3455 if (i1_feeds_i2_n)
3456 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3457 i0_feeds_i1_n && i0dest_in_i0src);
3458 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3459 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3461 XVECEXP (newpat, 0, --total_sets) = t;
3465 validate_replacement:
3467 /* Note which hard regs this insn has as inputs. */
3468 mark_used_regs_combine (newpat);
3470 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3471 consider splitting this pattern, we might need these clobbers. */
3472 if (i1 && GET_CODE (newpat) == PARALLEL
3473 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3475 int len = XVECLEN (newpat, 0);
3477 newpat_vec_with_clobbers = rtvec_alloc (len);
3478 for (i = 0; i < len; i++)
3479 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3482 /* We have recognized nothing yet. */
3483 insn_code_number = -1;
3485 /* See if this is a PARALLEL of two SETs where one SET's destination is
3486 a register that is unused and this isn't marked as an instruction that
3487 might trap in an EH region. In that case, we just need the other SET.
3488 We prefer this over the PARALLEL.
3490 This can occur when simplifying a divmod insn. We *must* test for this
3491 case here because the code below that splits two independent SETs doesn't
3492 handle this case correctly when it updates the register status.
3494 It's pointless doing this if we originally had two sets, one from
3495 i3, and one from i2. Combining then splitting the parallel results
3496 in the original i2 again plus an invalid insn (which we delete).
3497 The net effect is only to move instructions around, which makes
3498 debug info less accurate.
3500 If the remaining SET came from I2 its destination should not be used
3501 between I2 and I3. See PR82024. */
3503 if (!(added_sets_2 && i1 == 0)
3504 && is_parallel_of_n_reg_sets (newpat, 2)
3505 && asm_noperands (newpat) < 0)
3507 rtx set0 = XVECEXP (newpat, 0, 0);
3508 rtx set1 = XVECEXP (newpat, 0, 1);
3509 rtx oldpat = newpat;
3511 if (((REG_P (SET_DEST (set1))
3512 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3513 || (GET_CODE (SET_DEST (set1)) == SUBREG
3514 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3515 && insn_nothrow_p (i3)
3516 && !side_effects_p (SET_SRC (set1)))
3518 newpat = set0;
3519 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3522 else if (((REG_P (SET_DEST (set0))
3523 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3524 || (GET_CODE (SET_DEST (set0)) == SUBREG
3525 && find_reg_note (i3, REG_UNUSED,
3526 SUBREG_REG (SET_DEST (set0)))))
3527 && insn_nothrow_p (i3)
3528 && !side_effects_p (SET_SRC (set0)))
3530 rtx dest = SET_DEST (set1);
3531 if (GET_CODE (dest) == SUBREG)
3532 dest = SUBREG_REG (dest);
3533 if (!reg_used_between_p (dest, i2, i3))
3535 newpat = set1;
3536 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3538 if (insn_code_number >= 0)
3539 changed_i3_dest = 1;
3543 if (insn_code_number < 0)
3544 newpat = oldpat;
3547 /* Is the result of combination a valid instruction? */
3548 if (insn_code_number < 0)
3549 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3551 /* If we were combining three insns and the result is a simple SET
3552 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3553 insns. There are two ways to do this. It can be split using a
3554 machine-specific method (like when you have an addition of a large
3555 constant) or by combine in the function find_split_point. */
3557 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3558 && asm_noperands (newpat) < 0)
3560 rtx parallel, *split;
3561 rtx_insn *m_split_insn;
3563 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3564 use I2DEST as a scratch register will help. In the latter case,
3565 convert I2DEST to the mode of the source of NEWPAT if we can. */
3567 m_split_insn = combine_split_insns (newpat, i3);
3569 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3570 inputs of NEWPAT. */
3572 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3573 possible to try that as a scratch reg. This would require adding
3574 more code to make it work though. */
3576 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3578 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3580 /* ??? Reusing i2dest without resetting the reg_stat entry for it
3581 (temporarily, until we are committed to this instruction
3582 combination) does not work: for example, any call to nonzero_bits
3583 on the register (from a splitter in the MD file, for example)
3584 will get the old information, which is invalid.
3586 Since nowadays we can create registers during combine just fine,
3587 we should just create a new one here, not reuse i2dest. */
3589 /* First try to split using the original register as a
3590 scratch register. */
3591 parallel = gen_rtx_PARALLEL (VOIDmode,
3592 gen_rtvec (2, newpat,
3593 gen_rtx_CLOBBER (VOIDmode,
3594 i2dest)));
3595 m_split_insn = combine_split_insns (parallel, i3);
3597 /* If that didn't work, try changing the mode of I2DEST if
3598 we can. */
3599 if (m_split_insn == 0
3600 && new_mode != GET_MODE (i2dest)
3601 && new_mode != VOIDmode
3602 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3604 machine_mode old_mode = GET_MODE (i2dest);
3605 rtx ni2dest;
3607 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3608 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3609 else
3611 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3612 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3615 parallel = (gen_rtx_PARALLEL
3616 (VOIDmode,
3617 gen_rtvec (2, newpat,
3618 gen_rtx_CLOBBER (VOIDmode,
3619 ni2dest))));
3620 m_split_insn = combine_split_insns (parallel, i3);
3622 if (m_split_insn == 0
3623 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3625 struct undo *buf;
3627 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3628 buf = undobuf.undos;
3629 undobuf.undos = buf->next;
3630 buf->next = undobuf.frees;
3631 undobuf.frees = buf;
3635 i2scratch = m_split_insn != 0;
3638 /* If recog_for_combine has discarded clobbers, try to use them
3639 again for the split. */
3640 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3642 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3643 m_split_insn = combine_split_insns (parallel, i3);
3646 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3648 rtx m_split_pat = PATTERN (m_split_insn);
3649 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3650 if (insn_code_number >= 0)
3651 newpat = m_split_pat;
3653 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3654 && (next_nonnote_nondebug_insn (i2) == i3
3655 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3657 rtx i2set, i3set;
3658 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3659 newi2pat = PATTERN (m_split_insn);
3661 i3set = single_set (NEXT_INSN (m_split_insn));
3662 i2set = single_set (m_split_insn);
3664 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3666 /* If I2 or I3 has multiple SETs, we won't know how to track
3667 register status, so don't use these insns. If I2's destination
3668 is used between I2 and I3, we also can't use these insns. */
3670 if (i2_code_number >= 0 && i2set && i3set
3671 && (next_nonnote_nondebug_insn (i2) == i3
3672 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3673 insn_code_number = recog_for_combine (&newi3pat, i3,
3674 &new_i3_notes);
3675 if (insn_code_number >= 0)
3676 newpat = newi3pat;
3678 /* It is possible that both insns now set the destination of I3.
3679 If so, we must show an extra use of it. */
3681 if (insn_code_number >= 0)
3683 rtx new_i3_dest = SET_DEST (i3set);
3684 rtx new_i2_dest = SET_DEST (i2set);
3686 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3687 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3688 || GET_CODE (new_i3_dest) == SUBREG)
3689 new_i3_dest = XEXP (new_i3_dest, 0);
3691 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3692 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3693 || GET_CODE (new_i2_dest) == SUBREG)
3694 new_i2_dest = XEXP (new_i2_dest, 0);
3696 if (REG_P (new_i3_dest)
3697 && REG_P (new_i2_dest)
3698 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3699 && REGNO (new_i2_dest) < reg_n_sets_max)
3700 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3704 /* If we can split it and use I2DEST, go ahead and see if that
3705 helps things be recognized. Verify that none of the registers
3706 are set between I2 and I3. */
3707 if (insn_code_number < 0
3708 && (split = find_split_point (&newpat, i3, false)) != 0
3709 && (!HAVE_cc0 || REG_P (i2dest))
3710 /* We need I2DEST in the proper mode. If it is a hard register
3711 or the only use of a pseudo, we can change its mode.
3712 Make sure we don't change a hard register to have a mode that
3713 isn't valid for it, or change the number of registers. */
3714 && (GET_MODE (*split) == GET_MODE (i2dest)
3715 || GET_MODE (*split) == VOIDmode
3716 || can_change_dest_mode (i2dest, added_sets_2,
3717 GET_MODE (*split)))
3718 && (next_nonnote_nondebug_insn (i2) == i3
3719 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3720 /* We can't overwrite I2DEST if its value is still used by
3721 NEWPAT. */
3722 && ! reg_referenced_p (i2dest, newpat))
3724 rtx newdest = i2dest;
3725 enum rtx_code split_code = GET_CODE (*split);
3726 machine_mode split_mode = GET_MODE (*split);
3727 bool subst_done = false;
3728 newi2pat = NULL_RTX;
3730 i2scratch = true;
3732 /* *SPLIT may be part of I2SRC, so make sure we have the
3733 original expression around for later debug processing.
3734 We should not need I2SRC any more in other cases. */
3735 if (MAY_HAVE_DEBUG_INSNS)
3736 i2src = copy_rtx (i2src);
3737 else
3738 i2src = NULL;
3740 /* Get NEWDEST as a register in the proper mode. We have already
3741 validated that we can do this. */
3742 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3744 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3745 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3746 else
3748 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3749 newdest = regno_reg_rtx[REGNO (i2dest)];
3753 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3754 an ASHIFT. This can occur if it was inside a PLUS and hence
3755 appeared to be a memory address. This is a kludge. */
3756 if (split_code == MULT
3757 && CONST_INT_P (XEXP (*split, 1))
3758 && INTVAL (XEXP (*split, 1)) > 0
3759 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3761 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3762 XEXP (*split, 0), GEN_INT (i)));
3763 /* Update split_code because we may not have a multiply
3764 anymore. */
3765 split_code = GET_CODE (*split);
3768 /* Similarly for (plus (mult FOO (const_int pow2))). */
3769 if (split_code == PLUS
3770 && GET_CODE (XEXP (*split, 0)) == MULT
3771 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3772 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3773 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3775 rtx nsplit = XEXP (*split, 0);
3776 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3777 XEXP (nsplit, 0), GEN_INT (i)));
3778 /* Update split_code because we may not have a multiply
3779 anymore. */
3780 split_code = GET_CODE (*split);
3783 #ifdef INSN_SCHEDULING
3784 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3785 be written as a ZERO_EXTEND. */
3786 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3788 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3789 what it really is. */
3790 if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3791 == SIGN_EXTEND)
3792 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3793 SUBREG_REG (*split)));
3794 else
3795 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3796 SUBREG_REG (*split)));
3798 #endif
3800 /* Attempt to split binary operators using arithmetic identities. */
3801 if (BINARY_P (SET_SRC (newpat))
3802 && split_mode == GET_MODE (SET_SRC (newpat))
3803 && ! side_effects_p (SET_SRC (newpat)))
3805 rtx setsrc = SET_SRC (newpat);
3806 machine_mode mode = GET_MODE (setsrc);
3807 enum rtx_code code = GET_CODE (setsrc);
3808 rtx src_op0 = XEXP (setsrc, 0);
3809 rtx src_op1 = XEXP (setsrc, 1);
3811 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3812 if (rtx_equal_p (src_op0, src_op1))
3814 newi2pat = gen_rtx_SET (newdest, src_op0);
3815 SUBST (XEXP (setsrc, 0), newdest);
3816 SUBST (XEXP (setsrc, 1), newdest);
3817 subst_done = true;
3819 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3820 else if ((code == PLUS || code == MULT)
3821 && GET_CODE (src_op0) == code
3822 && GET_CODE (XEXP (src_op0, 0)) == code
3823 && (INTEGRAL_MODE_P (mode)
3824 || (FLOAT_MODE_P (mode)
3825 && flag_unsafe_math_optimizations)))
3827 rtx p = XEXP (XEXP (src_op0, 0), 0);
3828 rtx q = XEXP (XEXP (src_op0, 0), 1);
3829 rtx r = XEXP (src_op0, 1);
3830 rtx s = src_op1;
3832 /* Split both "((X op Y) op X) op Y" and
3833 "((X op Y) op Y) op X" as "T op T" where T is
3834 "X op Y". */
3835 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3836 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3838 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3839 SUBST (XEXP (setsrc, 0), newdest);
3840 SUBST (XEXP (setsrc, 1), newdest);
3841 subst_done = true;
3843 /* Split "((X op X) op Y) op Y)" as "T op T" where
3844 T is "X op Y". */
3845 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3847 rtx tmp = simplify_gen_binary (code, mode, p, r);
3848 newi2pat = gen_rtx_SET (newdest, tmp);
3849 SUBST (XEXP (setsrc, 0), newdest);
3850 SUBST (XEXP (setsrc, 1), newdest);
3851 subst_done = true;
3856 if (!subst_done)
3858 newi2pat = gen_rtx_SET (newdest, *split);
3859 SUBST (*split, newdest);
3862 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3864 /* recog_for_combine might have added CLOBBERs to newi2pat.
3865 Make sure NEWPAT does not depend on the clobbered regs. */
3866 if (GET_CODE (newi2pat) == PARALLEL)
3867 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3868 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3870 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3871 if (reg_overlap_mentioned_p (reg, newpat))
3873 undo_all ();
3874 return 0;
3878 /* If the split point was a MULT and we didn't have one before,
3879 don't use one now. */
3880 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3881 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3885 /* Check for a case where we loaded from memory in a narrow mode and
3886 then sign extended it, but we need both registers. In that case,
3887 we have a PARALLEL with both loads from the same memory location.
3888 We can split this into a load from memory followed by a register-register
3889 copy. This saves at least one insn, more if register allocation can
3890 eliminate the copy.
3892 We cannot do this if the destination of the first assignment is a
3893 condition code register or cc0. We eliminate this case by making sure
3894 the SET_DEST and SET_SRC have the same mode.
3896 We cannot do this if the destination of the second assignment is
3897 a register that we have already assumed is zero-extended. Similarly
3898 for a SUBREG of such a register. */
3900 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3901 && GET_CODE (newpat) == PARALLEL
3902 && XVECLEN (newpat, 0) == 2
3903 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3904 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3905 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3906 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3907 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3908 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3909 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3910 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3911 DF_INSN_LUID (i2))
3912 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3913 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3914 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3915 (REG_P (temp_expr)
3916 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3917 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3918 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3919 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3920 != GET_MODE_MASK (word_mode))))
3921 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3922 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3923 (REG_P (temp_expr)
3924 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3925 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3926 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3927 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3928 != GET_MODE_MASK (word_mode)))))
3929 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3930 SET_SRC (XVECEXP (newpat, 0, 1)))
3931 && ! find_reg_note (i3, REG_UNUSED,
3932 SET_DEST (XVECEXP (newpat, 0, 0))))
3934 rtx ni2dest;
3936 newi2pat = XVECEXP (newpat, 0, 0);
3937 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3938 newpat = XVECEXP (newpat, 0, 1);
3939 SUBST (SET_SRC (newpat),
3940 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3941 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3943 if (i2_code_number >= 0)
3944 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3946 if (insn_code_number >= 0)
3947 swap_i2i3 = 1;
3950 /* Similarly, check for a case where we have a PARALLEL of two independent
3951 SETs but we started with three insns. In this case, we can do the sets
3952 as two separate insns. This case occurs when some SET allows two
3953 other insns to combine, but the destination of that SET is still live.
3955 Also do this if we started with two insns and (at least) one of the
3956 resulting sets is a noop; this noop will be deleted later. */
3958 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3959 && GET_CODE (newpat) == PARALLEL
3960 && XVECLEN (newpat, 0) == 2
3961 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3962 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3963 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3964 || set_noop_p (XVECEXP (newpat, 0, 1)))
3965 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3966 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3967 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3968 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3969 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3970 XVECEXP (newpat, 0, 0))
3971 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3972 XVECEXP (newpat, 0, 1))
3973 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3974 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3976 rtx set0 = XVECEXP (newpat, 0, 0);
3977 rtx set1 = XVECEXP (newpat, 0, 1);
3979 /* Normally, it doesn't matter which of the two is done first,
3980 but the one that references cc0 can't be the second, and
3981 one which uses any regs/memory set in between i2 and i3 can't
3982 be first. The PARALLEL might also have been pre-existing in i3,
3983 so we need to make sure that we won't wrongly hoist a SET to i2
3984 that would conflict with a death note present in there. */
3985 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3986 && !(REG_P (SET_DEST (set1))
3987 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3988 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3989 && find_reg_note (i2, REG_DEAD,
3990 SUBREG_REG (SET_DEST (set1))))
3991 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
3992 /* If I3 is a jump, ensure that set0 is a jump so that
3993 we do not create invalid RTL. */
3994 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3997 newi2pat = set1;
3998 newpat = set0;
4000 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
4001 && !(REG_P (SET_DEST (set0))
4002 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4003 && !(GET_CODE (SET_DEST (set0)) == SUBREG
4004 && find_reg_note (i2, REG_DEAD,
4005 SUBREG_REG (SET_DEST (set0))))
4006 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
4007 /* If I3 is a jump, ensure that set1 is a jump so that
4008 we do not create invalid RTL. */
4009 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4012 newi2pat = set0;
4013 newpat = set1;
4015 else
4017 undo_all ();
4018 return 0;
4021 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4023 if (i2_code_number >= 0)
4025 /* recog_for_combine might have added CLOBBERs to newi2pat.
4026 Make sure NEWPAT does not depend on the clobbered regs. */
4027 if (GET_CODE (newi2pat) == PARALLEL)
4029 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4030 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4032 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4033 if (reg_overlap_mentioned_p (reg, newpat))
4035 undo_all ();
4036 return 0;
4041 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4045 /* If it still isn't recognized, fail and change things back the way they
4046 were. */
4047 if ((insn_code_number < 0
4048 /* Is the result a reasonable ASM_OPERANDS? */
4049 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4051 undo_all ();
4052 return 0;
4055 /* If we had to change another insn, make sure it is valid also. */
4056 if (undobuf.other_insn)
4058 CLEAR_HARD_REG_SET (newpat_used_regs);
4060 other_pat = PATTERN (undobuf.other_insn);
4061 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4062 &new_other_notes);
4064 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4066 undo_all ();
4067 return 0;
4071 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4072 they are adjacent to each other or not. */
4073 if (HAVE_cc0)
4075 rtx_insn *p = prev_nonnote_insn (i3);
4076 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4077 && sets_cc0_p (newi2pat))
4079 undo_all ();
4080 return 0;
4084 /* Only allow this combination if insn_rtx_costs reports that the
4085 replacement instructions are cheaper than the originals. */
4086 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4088 undo_all ();
4089 return 0;
4092 if (MAY_HAVE_DEBUG_INSNS)
4094 struct undo *undo;
4096 for (undo = undobuf.undos; undo; undo = undo->next)
4097 if (undo->kind == UNDO_MODE)
4099 rtx reg = *undo->where.r;
4100 machine_mode new_mode = GET_MODE (reg);
4101 machine_mode old_mode = undo->old_contents.m;
4103 /* Temporarily revert mode back. */
4104 adjust_reg_mode (reg, old_mode);
4106 if (reg == i2dest && i2scratch)
4108 /* If we used i2dest as a scratch register with a
4109 different mode, substitute it for the original
4110 i2src while its original mode is temporarily
4111 restored, and then clear i2scratch so that we don't
4112 do it again later. */
4113 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4114 this_basic_block);
4115 i2scratch = false;
4116 /* Put back the new mode. */
4117 adjust_reg_mode (reg, new_mode);
4119 else
4121 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4122 rtx_insn *first, *last;
4124 if (reg == i2dest)
4126 first = i2;
4127 last = last_combined_insn;
4129 else
4131 first = i3;
4132 last = undobuf.other_insn;
4133 gcc_assert (last);
4134 if (DF_INSN_LUID (last)
4135 < DF_INSN_LUID (last_combined_insn))
4136 last = last_combined_insn;
4139 /* We're dealing with a reg that changed mode but not
4140 meaning, so we want to turn it into a subreg for
4141 the new mode. However, because of REG sharing and
4142 because its mode had already changed, we have to do
4143 it in two steps. First, replace any debug uses of
4144 reg, with its original mode temporarily restored,
4145 with this copy we have created; then, replace the
4146 copy with the SUBREG of the original shared reg,
4147 once again changed to the new mode. */
4148 propagate_for_debug (first, last, reg, tempreg,
4149 this_basic_block);
4150 adjust_reg_mode (reg, new_mode);
4151 propagate_for_debug (first, last, tempreg,
4152 lowpart_subreg (old_mode, reg, new_mode),
4153 this_basic_block);
4158 /* If we will be able to accept this, we have made a
4159 change to the destination of I3. This requires us to
4160 do a few adjustments. */
4162 if (changed_i3_dest)
4164 PATTERN (i3) = newpat;
4165 adjust_for_new_dest (i3);
4168 /* We now know that we can do this combination. Merge the insns and
4169 update the status of registers and LOG_LINKS. */
4171 if (undobuf.other_insn)
4173 rtx note, next;
4175 PATTERN (undobuf.other_insn) = other_pat;
4177 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4178 ensure that they are still valid. Then add any non-duplicate
4179 notes added by recog_for_combine. */
4180 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4182 next = XEXP (note, 1);
4184 if ((REG_NOTE_KIND (note) == REG_DEAD
4185 && !reg_referenced_p (XEXP (note, 0),
4186 PATTERN (undobuf.other_insn)))
4187 ||(REG_NOTE_KIND (note) == REG_UNUSED
4188 && !reg_set_p (XEXP (note, 0),
4189 PATTERN (undobuf.other_insn)))
4190 /* Simply drop equal note since it may be no longer valid
4191 for other_insn. It may be possible to record that CC
4192 register is changed and only discard those notes, but
4193 in practice it's unnecessary complication and doesn't
4194 give any meaningful improvement.
4196 See PR78559. */
4197 || REG_NOTE_KIND (note) == REG_EQUAL
4198 || REG_NOTE_KIND (note) == REG_EQUIV)
4199 remove_note (undobuf.other_insn, note);
4202 distribute_notes (new_other_notes, undobuf.other_insn,
4203 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4204 NULL_RTX);
4207 if (swap_i2i3)
4209 rtx_insn *insn;
4210 struct insn_link *link;
4211 rtx ni2dest;
4213 /* I3 now uses what used to be its destination and which is now
4214 I2's destination. This requires us to do a few adjustments. */
4215 PATTERN (i3) = newpat;
4216 adjust_for_new_dest (i3);
4218 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4219 so we still will.
4221 However, some later insn might be using I2's dest and have
4222 a LOG_LINK pointing at I3. We must remove this link.
4223 The simplest way to remove the link is to point it at I1,
4224 which we know will be a NOTE. */
4226 /* newi2pat is usually a SET here; however, recog_for_combine might
4227 have added some clobbers. */
4228 if (GET_CODE (newi2pat) == PARALLEL)
4229 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4230 else
4231 ni2dest = SET_DEST (newi2pat);
4233 for (insn = NEXT_INSN (i3);
4234 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4235 || insn != BB_HEAD (this_basic_block->next_bb));
4236 insn = NEXT_INSN (insn))
4238 if (NONDEBUG_INSN_P (insn)
4239 && reg_referenced_p (ni2dest, PATTERN (insn)))
4241 FOR_EACH_LOG_LINK (link, insn)
4242 if (link->insn == i3)
4243 link->insn = i1;
4245 break;
4251 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4252 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4253 rtx midnotes = 0;
4254 int from_luid;
4255 /* Compute which registers we expect to eliminate. newi2pat may be setting
4256 either i3dest or i2dest, so we must check it. */
4257 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4258 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4259 || !i2dest_killed
4260 ? 0 : i2dest);
4261 /* For i1, we need to compute both local elimination and global
4262 elimination information with respect to newi2pat because i1dest
4263 may be the same as i3dest, in which case newi2pat may be setting
4264 i1dest. Global information is used when distributing REG_DEAD
4265 note for i2 and i3, in which case it does matter if newi2pat sets
4266 i1dest or not.
4268 Local information is used when distributing REG_DEAD note for i1,
4269 in which case it doesn't matter if newi2pat sets i1dest or not.
4270 See PR62151, if we have four insns combination:
4271 i0: r0 <- i0src
4272 i1: r1 <- i1src (using r0)
4273 REG_DEAD (r0)
4274 i2: r0 <- i2src (using r1)
4275 i3: r3 <- i3src (using r0)
4276 ix: using r0
4277 From i1's point of view, r0 is eliminated, no matter if it is set
4278 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4279 should be discarded.
4281 Note local information only affects cases in forms like "I1->I2->I3",
4282 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4283 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4284 i0dest anyway. */
4285 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4286 || !i1dest_killed
4287 ? 0 : i1dest);
4288 rtx elim_i1 = (local_elim_i1 == 0
4289 || (newi2pat && reg_set_p (i1dest, newi2pat))
4290 ? 0 : i1dest);
4291 /* Same case as i1. */
4292 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4293 ? 0 : i0dest);
4294 rtx elim_i0 = (local_elim_i0 == 0
4295 || (newi2pat && reg_set_p (i0dest, newi2pat))
4296 ? 0 : i0dest);
4298 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4299 clear them. */
4300 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4301 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4302 if (i1)
4303 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4304 if (i0)
4305 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4307 /* Ensure that we do not have something that should not be shared but
4308 occurs multiple times in the new insns. Check this by first
4309 resetting all the `used' flags and then copying anything is shared. */
4311 reset_used_flags (i3notes);
4312 reset_used_flags (i2notes);
4313 reset_used_flags (i1notes);
4314 reset_used_flags (i0notes);
4315 reset_used_flags (newpat);
4316 reset_used_flags (newi2pat);
4317 if (undobuf.other_insn)
4318 reset_used_flags (PATTERN (undobuf.other_insn));
4320 i3notes = copy_rtx_if_shared (i3notes);
4321 i2notes = copy_rtx_if_shared (i2notes);
4322 i1notes = copy_rtx_if_shared (i1notes);
4323 i0notes = copy_rtx_if_shared (i0notes);
4324 newpat = copy_rtx_if_shared (newpat);
4325 newi2pat = copy_rtx_if_shared (newi2pat);
4326 if (undobuf.other_insn)
4327 reset_used_flags (PATTERN (undobuf.other_insn));
4329 INSN_CODE (i3) = insn_code_number;
4330 PATTERN (i3) = newpat;
4332 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4334 for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4335 link = XEXP (link, 1))
4337 if (substed_i2)
4339 /* I2SRC must still be meaningful at this point. Some
4340 splitting operations can invalidate I2SRC, but those
4341 operations do not apply to calls. */
4342 gcc_assert (i2src);
4343 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4344 i2dest, i2src);
4346 if (substed_i1)
4347 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4348 i1dest, i1src);
4349 if (substed_i0)
4350 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4351 i0dest, i0src);
4355 if (undobuf.other_insn)
4356 INSN_CODE (undobuf.other_insn) = other_code_number;
4358 /* We had one special case above where I2 had more than one set and
4359 we replaced a destination of one of those sets with the destination
4360 of I3. In that case, we have to update LOG_LINKS of insns later
4361 in this basic block. Note that this (expensive) case is rare.
4363 Also, in this case, we must pretend that all REG_NOTEs for I2
4364 actually came from I3, so that REG_UNUSED notes from I2 will be
4365 properly handled. */
4367 if (i3_subst_into_i2)
4369 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4370 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4371 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4372 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4373 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4374 && ! find_reg_note (i2, REG_UNUSED,
4375 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4376 for (temp_insn = NEXT_INSN (i2);
4377 temp_insn
4378 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4379 || BB_HEAD (this_basic_block) != temp_insn);
4380 temp_insn = NEXT_INSN (temp_insn))
4381 if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4382 FOR_EACH_LOG_LINK (link, temp_insn)
4383 if (link->insn == i2)
4384 link->insn = i3;
4386 if (i3notes)
4388 rtx link = i3notes;
4389 while (XEXP (link, 1))
4390 link = XEXP (link, 1);
4391 XEXP (link, 1) = i2notes;
4393 else
4394 i3notes = i2notes;
4395 i2notes = 0;
4398 LOG_LINKS (i3) = NULL;
4399 REG_NOTES (i3) = 0;
4400 LOG_LINKS (i2) = NULL;
4401 REG_NOTES (i2) = 0;
4403 if (newi2pat)
4405 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4406 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4407 this_basic_block);
4408 INSN_CODE (i2) = i2_code_number;
4409 PATTERN (i2) = newi2pat;
4411 else
4413 if (MAY_HAVE_DEBUG_INSNS && i2src)
4414 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4415 this_basic_block);
4416 SET_INSN_DELETED (i2);
4419 if (i1)
4421 LOG_LINKS (i1) = NULL;
4422 REG_NOTES (i1) = 0;
4423 if (MAY_HAVE_DEBUG_INSNS)
4424 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4425 this_basic_block);
4426 SET_INSN_DELETED (i1);
4429 if (i0)
4431 LOG_LINKS (i0) = NULL;
4432 REG_NOTES (i0) = 0;
4433 if (MAY_HAVE_DEBUG_INSNS)
4434 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4435 this_basic_block);
4436 SET_INSN_DELETED (i0);
4439 /* Get death notes for everything that is now used in either I3 or
4440 I2 and used to die in a previous insn. If we built two new
4441 patterns, move from I1 to I2 then I2 to I3 so that we get the
4442 proper movement on registers that I2 modifies. */
4444 if (i0)
4445 from_luid = DF_INSN_LUID (i0);
4446 else if (i1)
4447 from_luid = DF_INSN_LUID (i1);
4448 else
4449 from_luid = DF_INSN_LUID (i2);
4450 if (newi2pat)
4451 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4452 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4454 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4455 if (i3notes)
4456 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4457 elim_i2, elim_i1, elim_i0);
4458 if (i2notes)
4459 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4460 elim_i2, elim_i1, elim_i0);
4461 if (i1notes)
4462 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4463 elim_i2, local_elim_i1, local_elim_i0);
4464 if (i0notes)
4465 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4466 elim_i2, elim_i1, local_elim_i0);
4467 if (midnotes)
4468 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4469 elim_i2, elim_i1, elim_i0);
4471 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4472 know these are REG_UNUSED and want them to go to the desired insn,
4473 so we always pass it as i3. */
4475 if (newi2pat && new_i2_notes)
4476 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4477 NULL_RTX);
4479 if (new_i3_notes)
4480 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4481 NULL_RTX);
4483 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4484 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4485 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4486 in that case, it might delete I2. Similarly for I2 and I1.
4487 Show an additional death due to the REG_DEAD note we make here. If
4488 we discard it in distribute_notes, we will decrement it again. */
4490 if (i3dest_killed)
4492 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4493 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4494 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4495 elim_i1, elim_i0);
4496 else
4497 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4498 elim_i2, elim_i1, elim_i0);
4501 if (i2dest_in_i2src)
4503 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4504 if (newi2pat && reg_set_p (i2dest, newi2pat))
4505 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4506 NULL_RTX, NULL_RTX);
4507 else
4508 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4509 NULL_RTX, NULL_RTX, NULL_RTX);
4512 if (i1dest_in_i1src)
4514 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4515 if (newi2pat && reg_set_p (i1dest, newi2pat))
4516 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4517 NULL_RTX, NULL_RTX);
4518 else
4519 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4520 NULL_RTX, NULL_RTX, NULL_RTX);
4523 if (i0dest_in_i0src)
4525 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4526 if (newi2pat && reg_set_p (i0dest, newi2pat))
4527 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4528 NULL_RTX, NULL_RTX);
4529 else
4530 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4531 NULL_RTX, NULL_RTX, NULL_RTX);
4534 distribute_links (i3links);
4535 distribute_links (i2links);
4536 distribute_links (i1links);
4537 distribute_links (i0links);
4539 if (REG_P (i2dest))
4541 struct insn_link *link;
4542 rtx_insn *i2_insn = 0;
4543 rtx i2_val = 0, set;
4545 /* The insn that used to set this register doesn't exist, and
4546 this life of the register may not exist either. See if one of
4547 I3's links points to an insn that sets I2DEST. If it does,
4548 that is now the last known value for I2DEST. If we don't update
4549 this and I2 set the register to a value that depended on its old
4550 contents, we will get confused. If this insn is used, thing
4551 will be set correctly in combine_instructions. */
4552 FOR_EACH_LOG_LINK (link, i3)
4553 if ((set = single_set (link->insn)) != 0
4554 && rtx_equal_p (i2dest, SET_DEST (set)))
4555 i2_insn = link->insn, i2_val = SET_SRC (set);
4557 record_value_for_reg (i2dest, i2_insn, i2_val);
4559 /* If the reg formerly set in I2 died only once and that was in I3,
4560 zero its use count so it won't make `reload' do any work. */
4561 if (! added_sets_2
4562 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4563 && ! i2dest_in_i2src
4564 && REGNO (i2dest) < reg_n_sets_max)
4565 INC_REG_N_SETS (REGNO (i2dest), -1);
4568 if (i1 && REG_P (i1dest))
4570 struct insn_link *link;
4571 rtx_insn *i1_insn = 0;
4572 rtx i1_val = 0, set;
4574 FOR_EACH_LOG_LINK (link, i3)
4575 if ((set = single_set (link->insn)) != 0
4576 && rtx_equal_p (i1dest, SET_DEST (set)))
4577 i1_insn = link->insn, i1_val = SET_SRC (set);
4579 record_value_for_reg (i1dest, i1_insn, i1_val);
4581 if (! added_sets_1
4582 && ! i1dest_in_i1src
4583 && REGNO (i1dest) < reg_n_sets_max)
4584 INC_REG_N_SETS (REGNO (i1dest), -1);
4587 if (i0 && REG_P (i0dest))
4589 struct insn_link *link;
4590 rtx_insn *i0_insn = 0;
4591 rtx i0_val = 0, set;
4593 FOR_EACH_LOG_LINK (link, i3)
4594 if ((set = single_set (link->insn)) != 0
4595 && rtx_equal_p (i0dest, SET_DEST (set)))
4596 i0_insn = link->insn, i0_val = SET_SRC (set);
4598 record_value_for_reg (i0dest, i0_insn, i0_val);
4600 if (! added_sets_0
4601 && ! i0dest_in_i0src
4602 && REGNO (i0dest) < reg_n_sets_max)
4603 INC_REG_N_SETS (REGNO (i0dest), -1);
4606 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4607 been made to this insn. The order is important, because newi2pat
4608 can affect nonzero_bits of newpat. */
4609 if (newi2pat)
4610 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4611 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4614 if (undobuf.other_insn != NULL_RTX)
4616 if (dump_file)
4618 fprintf (dump_file, "modifying other_insn ");
4619 dump_insn_slim (dump_file, undobuf.other_insn);
4621 df_insn_rescan (undobuf.other_insn);
4624 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4626 if (dump_file)
4628 fprintf (dump_file, "modifying insn i0 ");
4629 dump_insn_slim (dump_file, i0);
4631 df_insn_rescan (i0);
4634 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4636 if (dump_file)
4638 fprintf (dump_file, "modifying insn i1 ");
4639 dump_insn_slim (dump_file, i1);
4641 df_insn_rescan (i1);
4644 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4646 if (dump_file)
4648 fprintf (dump_file, "modifying insn i2 ");
4649 dump_insn_slim (dump_file, i2);
4651 df_insn_rescan (i2);
4654 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4656 if (dump_file)
4658 fprintf (dump_file, "modifying insn i3 ");
4659 dump_insn_slim (dump_file, i3);
4661 df_insn_rescan (i3);
4664 /* Set new_direct_jump_p if a new return or simple jump instruction
4665 has been created. Adjust the CFG accordingly. */
4666 if (returnjump_p (i3) || any_uncondjump_p (i3))
4668 *new_direct_jump_p = 1;
4669 mark_jump_label (PATTERN (i3), i3, 0);
4670 update_cfg_for_uncondjump (i3);
4673 if (undobuf.other_insn != NULL_RTX
4674 && (returnjump_p (undobuf.other_insn)
4675 || any_uncondjump_p (undobuf.other_insn)))
4677 *new_direct_jump_p = 1;
4678 update_cfg_for_uncondjump (undobuf.other_insn);
4681 if (GET_CODE (PATTERN (i3)) == TRAP_IF
4682 && XEXP (PATTERN (i3), 0) == const1_rtx)
4684 basic_block bb = BLOCK_FOR_INSN (i3);
4685 gcc_assert (bb);
4686 remove_edge (split_block (bb, i3));
4687 emit_barrier_after_bb (bb);
4688 *new_direct_jump_p = 1;
4691 if (undobuf.other_insn
4692 && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4693 && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4695 basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4696 gcc_assert (bb);
4697 remove_edge (split_block (bb, undobuf.other_insn));
4698 emit_barrier_after_bb (bb);
4699 *new_direct_jump_p = 1;
4702 /* A noop might also need cleaning up of CFG, if it comes from the
4703 simplification of a jump. */
4704 if (JUMP_P (i3)
4705 && GET_CODE (newpat) == SET
4706 && SET_SRC (newpat) == pc_rtx
4707 && SET_DEST (newpat) == pc_rtx)
4709 *new_direct_jump_p = 1;
4710 update_cfg_for_uncondjump (i3);
4713 if (undobuf.other_insn != NULL_RTX
4714 && JUMP_P (undobuf.other_insn)
4715 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4716 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4717 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4719 *new_direct_jump_p = 1;
4720 update_cfg_for_uncondjump (undobuf.other_insn);
4723 combine_successes++;
4724 undo_commit ();
4726 if (added_links_insn
4727 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4728 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4729 return added_links_insn;
4730 else
4731 return newi2pat ? i2 : i3;
4734 /* Get a marker for undoing to the current state. */
4736 static void *
4737 get_undo_marker (void)
4739 return undobuf.undos;
4742 /* Undo the modifications up to the marker. */
4744 static void
4745 undo_to_marker (void *marker)
4747 struct undo *undo, *next;
4749 for (undo = undobuf.undos; undo != marker; undo = next)
4751 gcc_assert (undo);
4753 next = undo->next;
4754 switch (undo->kind)
4756 case UNDO_RTX:
4757 *undo->where.r = undo->old_contents.r;
4758 break;
4759 case UNDO_INT:
4760 *undo->where.i = undo->old_contents.i;
4761 break;
4762 case UNDO_MODE:
4763 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4764 break;
4765 case UNDO_LINKS:
4766 *undo->where.l = undo->old_contents.l;
4767 break;
4768 default:
4769 gcc_unreachable ();
4772 undo->next = undobuf.frees;
4773 undobuf.frees = undo;
4776 undobuf.undos = (struct undo *) marker;
4779 /* Undo all the modifications recorded in undobuf. */
4781 static void
4782 undo_all (void)
4784 undo_to_marker (0);
4787 /* We've committed to accepting the changes we made. Move all
4788 of the undos to the free list. */
4790 static void
4791 undo_commit (void)
4793 struct undo *undo, *next;
4795 for (undo = undobuf.undos; undo; undo = next)
4797 next = undo->next;
4798 undo->next = undobuf.frees;
4799 undobuf.frees = undo;
4801 undobuf.undos = 0;
4804 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4805 where we have an arithmetic expression and return that point. LOC will
4806 be inside INSN.
4808 try_combine will call this function to see if an insn can be split into
4809 two insns. */
4811 static rtx *
4812 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4814 rtx x = *loc;
4815 enum rtx_code code = GET_CODE (x);
4816 rtx *split;
4817 unsigned HOST_WIDE_INT len = 0;
4818 HOST_WIDE_INT pos = 0;
4819 int unsignedp = 0;
4820 rtx inner = NULL_RTX;
4821 scalar_int_mode mode, inner_mode;
4823 /* First special-case some codes. */
4824 switch (code)
4826 case SUBREG:
4827 #ifdef INSN_SCHEDULING
4828 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4829 point. */
4830 if (MEM_P (SUBREG_REG (x)))
4831 return loc;
4832 #endif
4833 return find_split_point (&SUBREG_REG (x), insn, false);
4835 case MEM:
4836 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4837 using LO_SUM and HIGH. */
4838 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4839 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4841 machine_mode address_mode = get_address_mode (x);
4843 SUBST (XEXP (x, 0),
4844 gen_rtx_LO_SUM (address_mode,
4845 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4846 XEXP (x, 0)));
4847 return &XEXP (XEXP (x, 0), 0);
4850 /* If we have a PLUS whose second operand is a constant and the
4851 address is not valid, perhaps will can split it up using
4852 the machine-specific way to split large constants. We use
4853 the first pseudo-reg (one of the virtual regs) as a placeholder;
4854 it will not remain in the result. */
4855 if (GET_CODE (XEXP (x, 0)) == PLUS
4856 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4857 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4858 MEM_ADDR_SPACE (x)))
4860 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4861 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4862 subst_insn);
4864 /* This should have produced two insns, each of which sets our
4865 placeholder. If the source of the second is a valid address,
4866 we can make put both sources together and make a split point
4867 in the middle. */
4869 if (seq
4870 && NEXT_INSN (seq) != NULL_RTX
4871 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4872 && NONJUMP_INSN_P (seq)
4873 && GET_CODE (PATTERN (seq)) == SET
4874 && SET_DEST (PATTERN (seq)) == reg
4875 && ! reg_mentioned_p (reg,
4876 SET_SRC (PATTERN (seq)))
4877 && NONJUMP_INSN_P (NEXT_INSN (seq))
4878 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4879 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4880 && memory_address_addr_space_p
4881 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4882 MEM_ADDR_SPACE (x)))
4884 rtx src1 = SET_SRC (PATTERN (seq));
4885 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4887 /* Replace the placeholder in SRC2 with SRC1. If we can
4888 find where in SRC2 it was placed, that can become our
4889 split point and we can replace this address with SRC2.
4890 Just try two obvious places. */
4892 src2 = replace_rtx (src2, reg, src1);
4893 split = 0;
4894 if (XEXP (src2, 0) == src1)
4895 split = &XEXP (src2, 0);
4896 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4897 && XEXP (XEXP (src2, 0), 0) == src1)
4898 split = &XEXP (XEXP (src2, 0), 0);
4900 if (split)
4902 SUBST (XEXP (x, 0), src2);
4903 return split;
4907 /* If that didn't work, perhaps the first operand is complex and
4908 needs to be computed separately, so make a split point there.
4909 This will occur on machines that just support REG + CONST
4910 and have a constant moved through some previous computation. */
4912 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4913 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4914 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4915 return &XEXP (XEXP (x, 0), 0);
4918 /* If we have a PLUS whose first operand is complex, try computing it
4919 separately by making a split there. */
4920 if (GET_CODE (XEXP (x, 0)) == PLUS
4921 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4922 MEM_ADDR_SPACE (x))
4923 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4924 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4925 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4926 return &XEXP (XEXP (x, 0), 0);
4927 break;
4929 case SET:
4930 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4931 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4932 we need to put the operand into a register. So split at that
4933 point. */
4935 if (SET_DEST (x) == cc0_rtx
4936 && GET_CODE (SET_SRC (x)) != COMPARE
4937 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4938 && !OBJECT_P (SET_SRC (x))
4939 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4940 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4941 return &SET_SRC (x);
4943 /* See if we can split SET_SRC as it stands. */
4944 split = find_split_point (&SET_SRC (x), insn, true);
4945 if (split && split != &SET_SRC (x))
4946 return split;
4948 /* See if we can split SET_DEST as it stands. */
4949 split = find_split_point (&SET_DEST (x), insn, false);
4950 if (split && split != &SET_DEST (x))
4951 return split;
4953 /* See if this is a bitfield assignment with everything constant. If
4954 so, this is an IOR of an AND, so split it into that. */
4955 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4956 && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
4957 &inner_mode)
4958 && HWI_COMPUTABLE_MODE_P (inner_mode)
4959 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4960 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4961 && CONST_INT_P (SET_SRC (x))
4962 && ((INTVAL (XEXP (SET_DEST (x), 1))
4963 + INTVAL (XEXP (SET_DEST (x), 2)))
4964 <= GET_MODE_PRECISION (inner_mode))
4965 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4967 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4968 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4969 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4970 rtx dest = XEXP (SET_DEST (x), 0);
4971 unsigned HOST_WIDE_INT mask
4972 = (HOST_WIDE_INT_1U << len) - 1;
4973 rtx or_mask;
4975 if (BITS_BIG_ENDIAN)
4976 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
4978 or_mask = gen_int_mode (src << pos, inner_mode);
4979 if (src == mask)
4980 SUBST (SET_SRC (x),
4981 simplify_gen_binary (IOR, inner_mode, dest, or_mask));
4982 else
4984 rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
4985 SUBST (SET_SRC (x),
4986 simplify_gen_binary (IOR, inner_mode,
4987 simplify_gen_binary (AND, inner_mode,
4988 dest, negmask),
4989 or_mask));
4992 SUBST (SET_DEST (x), dest);
4994 split = find_split_point (&SET_SRC (x), insn, true);
4995 if (split && split != &SET_SRC (x))
4996 return split;
4999 /* Otherwise, see if this is an operation that we can split into two.
5000 If so, try to split that. */
5001 code = GET_CODE (SET_SRC (x));
5003 switch (code)
5005 case AND:
5006 /* If we are AND'ing with a large constant that is only a single
5007 bit and the result is only being used in a context where we
5008 need to know if it is zero or nonzero, replace it with a bit
5009 extraction. This will avoid the large constant, which might
5010 have taken more than one insn to make. If the constant were
5011 not a valid argument to the AND but took only one insn to make,
5012 this is no worse, but if it took more than one insn, it will
5013 be better. */
5015 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5016 && REG_P (XEXP (SET_SRC (x), 0))
5017 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5018 && REG_P (SET_DEST (x))
5019 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5020 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5021 && XEXP (*split, 0) == SET_DEST (x)
5022 && XEXP (*split, 1) == const0_rtx)
5024 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5025 XEXP (SET_SRC (x), 0),
5026 pos, NULL_RTX, 1, 1, 0, 0);
5027 if (extraction != 0)
5029 SUBST (SET_SRC (x), extraction);
5030 return find_split_point (loc, insn, false);
5033 break;
5035 case NE:
5036 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5037 is known to be on, this can be converted into a NEG of a shift. */
5038 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5039 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5040 && 1 <= (pos = exact_log2
5041 (nonzero_bits (XEXP (SET_SRC (x), 0),
5042 GET_MODE (XEXP (SET_SRC (x), 0))))))
5044 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5046 SUBST (SET_SRC (x),
5047 gen_rtx_NEG (mode,
5048 gen_rtx_LSHIFTRT (mode,
5049 XEXP (SET_SRC (x), 0),
5050 GEN_INT (pos))));
5052 split = find_split_point (&SET_SRC (x), insn, true);
5053 if (split && split != &SET_SRC (x))
5054 return split;
5056 break;
5058 case SIGN_EXTEND:
5059 inner = XEXP (SET_SRC (x), 0);
5061 /* We can't optimize if either mode is a partial integer
5062 mode as we don't know how many bits are significant
5063 in those modes. */
5064 if (!is_int_mode (GET_MODE (inner), &inner_mode)
5065 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5066 break;
5068 pos = 0;
5069 len = GET_MODE_PRECISION (inner_mode);
5070 unsignedp = 0;
5071 break;
5073 case SIGN_EXTRACT:
5074 case ZERO_EXTRACT:
5075 if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5076 &inner_mode)
5077 && CONST_INT_P (XEXP (SET_SRC (x), 1))
5078 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5080 inner = XEXP (SET_SRC (x), 0);
5081 len = INTVAL (XEXP (SET_SRC (x), 1));
5082 pos = INTVAL (XEXP (SET_SRC (x), 2));
5084 if (BITS_BIG_ENDIAN)
5085 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5086 unsignedp = (code == ZERO_EXTRACT);
5088 break;
5090 default:
5091 break;
5094 if (len && pos >= 0
5095 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner))
5096 && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5098 /* For unsigned, we have a choice of a shift followed by an
5099 AND or two shifts. Use two shifts for field sizes where the
5100 constant might be too large. We assume here that we can
5101 always at least get 8-bit constants in an AND insn, which is
5102 true for every current RISC. */
5104 if (unsignedp && len <= 8)
5106 unsigned HOST_WIDE_INT mask
5107 = (HOST_WIDE_INT_1U << len) - 1;
5108 SUBST (SET_SRC (x),
5109 gen_rtx_AND (mode,
5110 gen_rtx_LSHIFTRT
5111 (mode, gen_lowpart (mode, inner),
5112 GEN_INT (pos)),
5113 gen_int_mode (mask, mode)));
5115 split = find_split_point (&SET_SRC (x), insn, true);
5116 if (split && split != &SET_SRC (x))
5117 return split;
5119 else
5121 SUBST (SET_SRC (x),
5122 gen_rtx_fmt_ee
5123 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5124 gen_rtx_ASHIFT (mode,
5125 gen_lowpart (mode, inner),
5126 GEN_INT (GET_MODE_PRECISION (mode)
5127 - len - pos)),
5128 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5130 split = find_split_point (&SET_SRC (x), insn, true);
5131 if (split && split != &SET_SRC (x))
5132 return split;
5136 /* See if this is a simple operation with a constant as the second
5137 operand. It might be that this constant is out of range and hence
5138 could be used as a split point. */
5139 if (BINARY_P (SET_SRC (x))
5140 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5141 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5142 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5143 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5144 return &XEXP (SET_SRC (x), 1);
5146 /* Finally, see if this is a simple operation with its first operand
5147 not in a register. The operation might require this operand in a
5148 register, so return it as a split point. We can always do this
5149 because if the first operand were another operation, we would have
5150 already found it as a split point. */
5151 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5152 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5153 return &XEXP (SET_SRC (x), 0);
5155 return 0;
5157 case AND:
5158 case IOR:
5159 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5160 it is better to write this as (not (ior A B)) so we can split it.
5161 Similarly for IOR. */
5162 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5164 SUBST (*loc,
5165 gen_rtx_NOT (GET_MODE (x),
5166 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5167 GET_MODE (x),
5168 XEXP (XEXP (x, 0), 0),
5169 XEXP (XEXP (x, 1), 0))));
5170 return find_split_point (loc, insn, set_src);
5173 /* Many RISC machines have a large set of logical insns. If the
5174 second operand is a NOT, put it first so we will try to split the
5175 other operand first. */
5176 if (GET_CODE (XEXP (x, 1)) == NOT)
5178 rtx tem = XEXP (x, 0);
5179 SUBST (XEXP (x, 0), XEXP (x, 1));
5180 SUBST (XEXP (x, 1), tem);
5182 break;
5184 case PLUS:
5185 case MINUS:
5186 /* Canonicalization can produce (minus A (mult B C)), where C is a
5187 constant. It may be better to try splitting (plus (mult B -C) A)
5188 instead if this isn't a multiply by a power of two. */
5189 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5190 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5191 && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5193 machine_mode mode = GET_MODE (x);
5194 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5195 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5196 SUBST (*loc, gen_rtx_PLUS (mode,
5197 gen_rtx_MULT (mode,
5198 XEXP (XEXP (x, 1), 0),
5199 gen_int_mode (other_int,
5200 mode)),
5201 XEXP (x, 0)));
5202 return find_split_point (loc, insn, set_src);
5205 /* Split at a multiply-accumulate instruction. However if this is
5206 the SET_SRC, we likely do not have such an instruction and it's
5207 worthless to try this split. */
5208 if (!set_src
5209 && (GET_CODE (XEXP (x, 0)) == MULT
5210 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5211 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5212 return loc;
5214 default:
5215 break;
5218 /* Otherwise, select our actions depending on our rtx class. */
5219 switch (GET_RTX_CLASS (code))
5221 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5222 case RTX_TERNARY:
5223 split = find_split_point (&XEXP (x, 2), insn, false);
5224 if (split)
5225 return split;
5226 /* fall through */
5227 case RTX_BIN_ARITH:
5228 case RTX_COMM_ARITH:
5229 case RTX_COMPARE:
5230 case RTX_COMM_COMPARE:
5231 split = find_split_point (&XEXP (x, 1), insn, false);
5232 if (split)
5233 return split;
5234 /* fall through */
5235 case RTX_UNARY:
5236 /* Some machines have (and (shift ...) ...) insns. If X is not
5237 an AND, but XEXP (X, 0) is, use it as our split point. */
5238 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5239 return &XEXP (x, 0);
5241 split = find_split_point (&XEXP (x, 0), insn, false);
5242 if (split)
5243 return split;
5244 return loc;
5246 default:
5247 /* Otherwise, we don't have a split point. */
5248 return 0;
5252 /* Throughout X, replace FROM with TO, and return the result.
5253 The result is TO if X is FROM;
5254 otherwise the result is X, but its contents may have been modified.
5255 If they were modified, a record was made in undobuf so that
5256 undo_all will (among other things) return X to its original state.
5258 If the number of changes necessary is too much to record to undo,
5259 the excess changes are not made, so the result is invalid.
5260 The changes already made can still be undone.
5261 undobuf.num_undo is incremented for such changes, so by testing that
5262 the caller can tell whether the result is valid.
5264 `n_occurrences' is incremented each time FROM is replaced.
5266 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5268 IN_COND is nonzero if we are at the top level of a condition.
5270 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5271 by copying if `n_occurrences' is nonzero. */
5273 static rtx
5274 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5276 enum rtx_code code = GET_CODE (x);
5277 machine_mode op0_mode = VOIDmode;
5278 const char *fmt;
5279 int len, i;
5280 rtx new_rtx;
5282 /* Two expressions are equal if they are identical copies of a shared
5283 RTX or if they are both registers with the same register number
5284 and mode. */
5286 #define COMBINE_RTX_EQUAL_P(X,Y) \
5287 ((X) == (Y) \
5288 || (REG_P (X) && REG_P (Y) \
5289 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5291 /* Do not substitute into clobbers of regs -- this will never result in
5292 valid RTL. */
5293 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5294 return x;
5296 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5298 n_occurrences++;
5299 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5302 /* If X and FROM are the same register but different modes, they
5303 will not have been seen as equal above. However, the log links code
5304 will make a LOG_LINKS entry for that case. If we do nothing, we
5305 will try to rerecognize our original insn and, when it succeeds,
5306 we will delete the feeding insn, which is incorrect.
5308 So force this insn not to match in this (rare) case. */
5309 if (! in_dest && code == REG && REG_P (from)
5310 && reg_overlap_mentioned_p (x, from))
5311 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5313 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5314 of which may contain things that can be combined. */
5315 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5316 return x;
5318 /* It is possible to have a subexpression appear twice in the insn.
5319 Suppose that FROM is a register that appears within TO.
5320 Then, after that subexpression has been scanned once by `subst',
5321 the second time it is scanned, TO may be found. If we were
5322 to scan TO here, we would find FROM within it and create a
5323 self-referent rtl structure which is completely wrong. */
5324 if (COMBINE_RTX_EQUAL_P (x, to))
5325 return to;
5327 /* Parallel asm_operands need special attention because all of the
5328 inputs are shared across the arms. Furthermore, unsharing the
5329 rtl results in recognition failures. Failure to handle this case
5330 specially can result in circular rtl.
5332 Solve this by doing a normal pass across the first entry of the
5333 parallel, and only processing the SET_DESTs of the subsequent
5334 entries. Ug. */
5336 if (code == PARALLEL
5337 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5338 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5340 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5342 /* If this substitution failed, this whole thing fails. */
5343 if (GET_CODE (new_rtx) == CLOBBER
5344 && XEXP (new_rtx, 0) == const0_rtx)
5345 return new_rtx;
5347 SUBST (XVECEXP (x, 0, 0), new_rtx);
5349 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5351 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5353 if (!REG_P (dest)
5354 && GET_CODE (dest) != CC0
5355 && GET_CODE (dest) != PC)
5357 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5359 /* If this substitution failed, this whole thing fails. */
5360 if (GET_CODE (new_rtx) == CLOBBER
5361 && XEXP (new_rtx, 0) == const0_rtx)
5362 return new_rtx;
5364 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5368 else
5370 len = GET_RTX_LENGTH (code);
5371 fmt = GET_RTX_FORMAT (code);
5373 /* We don't need to process a SET_DEST that is a register, CC0,
5374 or PC, so set up to skip this common case. All other cases
5375 where we want to suppress replacing something inside a
5376 SET_SRC are handled via the IN_DEST operand. */
5377 if (code == SET
5378 && (REG_P (SET_DEST (x))
5379 || GET_CODE (SET_DEST (x)) == CC0
5380 || GET_CODE (SET_DEST (x)) == PC))
5381 fmt = "ie";
5383 /* Trying to simplify the operands of a widening MULT is not likely
5384 to create RTL matching a machine insn. */
5385 if (code == MULT
5386 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5387 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5388 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5389 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5390 && REG_P (XEXP (XEXP (x, 0), 0))
5391 && REG_P (XEXP (XEXP (x, 1), 0))
5392 && from == to)
5393 return x;
5396 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5397 constant. */
5398 if (fmt[0] == 'e')
5399 op0_mode = GET_MODE (XEXP (x, 0));
5401 for (i = 0; i < len; i++)
5403 if (fmt[i] == 'E')
5405 int j;
5406 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5408 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5410 new_rtx = (unique_copy && n_occurrences
5411 ? copy_rtx (to) : to);
5412 n_occurrences++;
5414 else
5416 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5417 unique_copy);
5419 /* If this substitution failed, this whole thing
5420 fails. */
5421 if (GET_CODE (new_rtx) == CLOBBER
5422 && XEXP (new_rtx, 0) == const0_rtx)
5423 return new_rtx;
5426 SUBST (XVECEXP (x, i, j), new_rtx);
5429 else if (fmt[i] == 'e')
5431 /* If this is a register being set, ignore it. */
5432 new_rtx = XEXP (x, i);
5433 if (in_dest
5434 && i == 0
5435 && (((code == SUBREG || code == ZERO_EXTRACT)
5436 && REG_P (new_rtx))
5437 || code == STRICT_LOW_PART))
5440 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5442 /* In general, don't install a subreg involving two
5443 modes not tieable. It can worsen register
5444 allocation, and can even make invalid reload
5445 insns, since the reg inside may need to be copied
5446 from in the outside mode, and that may be invalid
5447 if it is an fp reg copied in integer mode.
5449 We allow two exceptions to this: It is valid if
5450 it is inside another SUBREG and the mode of that
5451 SUBREG and the mode of the inside of TO is
5452 tieable and it is valid if X is a SET that copies
5453 FROM to CC0. */
5455 if (GET_CODE (to) == SUBREG
5456 && !targetm.modes_tieable_p (GET_MODE (to),
5457 GET_MODE (SUBREG_REG (to)))
5458 && ! (code == SUBREG
5459 && (targetm.modes_tieable_p
5460 (GET_MODE (x), GET_MODE (SUBREG_REG (to)))))
5461 && (!HAVE_cc0
5462 || (! (code == SET
5463 && i == 1
5464 && XEXP (x, 0) == cc0_rtx))))
5465 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5467 if (code == SUBREG
5468 && REG_P (to)
5469 && REGNO (to) < FIRST_PSEUDO_REGISTER
5470 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5471 SUBREG_BYTE (x),
5472 GET_MODE (x)) < 0)
5473 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5475 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5476 n_occurrences++;
5478 else
5479 /* If we are in a SET_DEST, suppress most cases unless we
5480 have gone inside a MEM, in which case we want to
5481 simplify the address. We assume here that things that
5482 are actually part of the destination have their inner
5483 parts in the first expression. This is true for SUBREG,
5484 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5485 things aside from REG and MEM that should appear in a
5486 SET_DEST. */
5487 new_rtx = subst (XEXP (x, i), from, to,
5488 (((in_dest
5489 && (code == SUBREG || code == STRICT_LOW_PART
5490 || code == ZERO_EXTRACT))
5491 || code == SET)
5492 && i == 0),
5493 code == IF_THEN_ELSE && i == 0,
5494 unique_copy);
5496 /* If we found that we will have to reject this combination,
5497 indicate that by returning the CLOBBER ourselves, rather than
5498 an expression containing it. This will speed things up as
5499 well as prevent accidents where two CLOBBERs are considered
5500 to be equal, thus producing an incorrect simplification. */
5502 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5503 return new_rtx;
5505 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5507 machine_mode mode = GET_MODE (x);
5509 x = simplify_subreg (GET_MODE (x), new_rtx,
5510 GET_MODE (SUBREG_REG (x)),
5511 SUBREG_BYTE (x));
5512 if (! x)
5513 x = gen_rtx_CLOBBER (mode, const0_rtx);
5515 else if (CONST_SCALAR_INT_P (new_rtx)
5516 && GET_CODE (x) == ZERO_EXTEND)
5518 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5519 new_rtx, GET_MODE (XEXP (x, 0)));
5520 gcc_assert (x);
5522 else
5523 SUBST (XEXP (x, i), new_rtx);
5528 /* Check if we are loading something from the constant pool via float
5529 extension; in this case we would undo compress_float_constant
5530 optimization and degenerate constant load to an immediate value. */
5531 if (GET_CODE (x) == FLOAT_EXTEND
5532 && MEM_P (XEXP (x, 0))
5533 && MEM_READONLY_P (XEXP (x, 0)))
5535 rtx tmp = avoid_constant_pool_reference (x);
5536 if (x != tmp)
5537 return x;
5540 /* Try to simplify X. If the simplification changed the code, it is likely
5541 that further simplification will help, so loop, but limit the number
5542 of repetitions that will be performed. */
5544 for (i = 0; i < 4; i++)
5546 /* If X is sufficiently simple, don't bother trying to do anything
5547 with it. */
5548 if (code != CONST_INT && code != REG && code != CLOBBER)
5549 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5551 if (GET_CODE (x) == code)
5552 break;
5554 code = GET_CODE (x);
5556 /* We no longer know the original mode of operand 0 since we
5557 have changed the form of X) */
5558 op0_mode = VOIDmode;
5561 return x;
5564 /* If X is a commutative operation whose operands are not in the canonical
5565 order, use substitutions to swap them. */
5567 static void
5568 maybe_swap_commutative_operands (rtx x)
5570 if (COMMUTATIVE_ARITH_P (x)
5571 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5573 rtx temp = XEXP (x, 0);
5574 SUBST (XEXP (x, 0), XEXP (x, 1));
5575 SUBST (XEXP (x, 1), temp);
5579 /* Simplify X, a piece of RTL. We just operate on the expression at the
5580 outer level; call `subst' to simplify recursively. Return the new
5581 expression.
5583 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5584 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5585 of a condition. */
5587 static rtx
5588 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5589 int in_cond)
5591 enum rtx_code code = GET_CODE (x);
5592 machine_mode mode = GET_MODE (x);
5593 scalar_int_mode int_mode;
5594 rtx temp;
5595 int i;
5597 /* If this is a commutative operation, put a constant last and a complex
5598 expression first. We don't need to do this for comparisons here. */
5599 maybe_swap_commutative_operands (x);
5601 /* Try to fold this expression in case we have constants that weren't
5602 present before. */
5603 temp = 0;
5604 switch (GET_RTX_CLASS (code))
5606 case RTX_UNARY:
5607 if (op0_mode == VOIDmode)
5608 op0_mode = GET_MODE (XEXP (x, 0));
5609 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5610 break;
5611 case RTX_COMPARE:
5612 case RTX_COMM_COMPARE:
5614 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5615 if (cmp_mode == VOIDmode)
5617 cmp_mode = GET_MODE (XEXP (x, 1));
5618 if (cmp_mode == VOIDmode)
5619 cmp_mode = op0_mode;
5621 temp = simplify_relational_operation (code, mode, cmp_mode,
5622 XEXP (x, 0), XEXP (x, 1));
5624 break;
5625 case RTX_COMM_ARITH:
5626 case RTX_BIN_ARITH:
5627 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5628 break;
5629 case RTX_BITFIELD_OPS:
5630 case RTX_TERNARY:
5631 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5632 XEXP (x, 1), XEXP (x, 2));
5633 break;
5634 default:
5635 break;
5638 if (temp)
5640 x = temp;
5641 code = GET_CODE (temp);
5642 op0_mode = VOIDmode;
5643 mode = GET_MODE (temp);
5646 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5647 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5648 things. Check for cases where both arms are testing the same
5649 condition.
5651 Don't do anything if all operands are very simple. */
5653 if ((BINARY_P (x)
5654 && ((!OBJECT_P (XEXP (x, 0))
5655 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5656 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5657 || (!OBJECT_P (XEXP (x, 1))
5658 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5659 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5660 || (UNARY_P (x)
5661 && (!OBJECT_P (XEXP (x, 0))
5662 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5663 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5665 rtx cond, true_rtx, false_rtx;
5667 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5668 if (cond != 0
5669 /* If everything is a comparison, what we have is highly unlikely
5670 to be simpler, so don't use it. */
5671 && ! (COMPARISON_P (x)
5672 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5674 rtx cop1 = const0_rtx;
5675 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5677 if (cond_code == NE && COMPARISON_P (cond))
5678 return x;
5680 /* Simplify the alternative arms; this may collapse the true and
5681 false arms to store-flag values. Be careful to use copy_rtx
5682 here since true_rtx or false_rtx might share RTL with x as a
5683 result of the if_then_else_cond call above. */
5684 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5685 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5687 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5688 is unlikely to be simpler. */
5689 if (general_operand (true_rtx, VOIDmode)
5690 && general_operand (false_rtx, VOIDmode))
5692 enum rtx_code reversed;
5694 /* Restarting if we generate a store-flag expression will cause
5695 us to loop. Just drop through in this case. */
5697 /* If the result values are STORE_FLAG_VALUE and zero, we can
5698 just make the comparison operation. */
5699 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5700 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5701 cond, cop1);
5702 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5703 && ((reversed = reversed_comparison_code_parts
5704 (cond_code, cond, cop1, NULL))
5705 != UNKNOWN))
5706 x = simplify_gen_relational (reversed, mode, VOIDmode,
5707 cond, cop1);
5709 /* Likewise, we can make the negate of a comparison operation
5710 if the result values are - STORE_FLAG_VALUE and zero. */
5711 else if (CONST_INT_P (true_rtx)
5712 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5713 && false_rtx == const0_rtx)
5714 x = simplify_gen_unary (NEG, mode,
5715 simplify_gen_relational (cond_code,
5716 mode, VOIDmode,
5717 cond, cop1),
5718 mode);
5719 else if (CONST_INT_P (false_rtx)
5720 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5721 && true_rtx == const0_rtx
5722 && ((reversed = reversed_comparison_code_parts
5723 (cond_code, cond, cop1, NULL))
5724 != UNKNOWN))
5725 x = simplify_gen_unary (NEG, mode,
5726 simplify_gen_relational (reversed,
5727 mode, VOIDmode,
5728 cond, cop1),
5729 mode);
5730 else
5731 return gen_rtx_IF_THEN_ELSE (mode,
5732 simplify_gen_relational (cond_code,
5733 mode,
5734 VOIDmode,
5735 cond,
5736 cop1),
5737 true_rtx, false_rtx);
5739 code = GET_CODE (x);
5740 op0_mode = VOIDmode;
5745 /* First see if we can apply the inverse distributive law. */
5746 if (code == PLUS || code == MINUS
5747 || code == AND || code == IOR || code == XOR)
5749 x = apply_distributive_law (x);
5750 code = GET_CODE (x);
5751 op0_mode = VOIDmode;
5754 /* If CODE is an associative operation not otherwise handled, see if we
5755 can associate some operands. This can win if they are constants or
5756 if they are logically related (i.e. (a & b) & a). */
5757 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5758 || code == AND || code == IOR || code == XOR
5759 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5760 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5761 || (flag_associative_math && FLOAT_MODE_P (mode))))
5763 if (GET_CODE (XEXP (x, 0)) == code)
5765 rtx other = XEXP (XEXP (x, 0), 0);
5766 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5767 rtx inner_op1 = XEXP (x, 1);
5768 rtx inner;
5770 /* Make sure we pass the constant operand if any as the second
5771 one if this is a commutative operation. */
5772 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5773 std::swap (inner_op0, inner_op1);
5774 inner = simplify_binary_operation (code == MINUS ? PLUS
5775 : code == DIV ? MULT
5776 : code,
5777 mode, inner_op0, inner_op1);
5779 /* For commutative operations, try the other pair if that one
5780 didn't simplify. */
5781 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5783 other = XEXP (XEXP (x, 0), 1);
5784 inner = simplify_binary_operation (code, mode,
5785 XEXP (XEXP (x, 0), 0),
5786 XEXP (x, 1));
5789 if (inner)
5790 return simplify_gen_binary (code, mode, other, inner);
5794 /* A little bit of algebraic simplification here. */
5795 switch (code)
5797 case MEM:
5798 /* Ensure that our address has any ASHIFTs converted to MULT in case
5799 address-recognizing predicates are called later. */
5800 temp = make_compound_operation (XEXP (x, 0), MEM);
5801 SUBST (XEXP (x, 0), temp);
5802 break;
5804 case SUBREG:
5805 if (op0_mode == VOIDmode)
5806 op0_mode = GET_MODE (SUBREG_REG (x));
5808 /* See if this can be moved to simplify_subreg. */
5809 if (CONSTANT_P (SUBREG_REG (x))
5810 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5811 /* Don't call gen_lowpart if the inner mode
5812 is VOIDmode and we cannot simplify it, as SUBREG without
5813 inner mode is invalid. */
5814 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5815 || gen_lowpart_common (mode, SUBREG_REG (x))))
5816 return gen_lowpart (mode, SUBREG_REG (x));
5818 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5819 break;
5821 rtx temp;
5822 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5823 SUBREG_BYTE (x));
5824 if (temp)
5825 return temp;
5827 /* If op is known to have all lower bits zero, the result is zero. */
5828 scalar_int_mode int_mode, int_op0_mode;
5829 if (!in_dest
5830 && is_a <scalar_int_mode> (mode, &int_mode)
5831 && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5832 && (GET_MODE_PRECISION (int_mode)
5833 < GET_MODE_PRECISION (int_op0_mode))
5834 && (subreg_lowpart_offset (int_mode, int_op0_mode)
5835 == SUBREG_BYTE (x))
5836 && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5837 && (nonzero_bits (SUBREG_REG (x), int_op0_mode)
5838 & GET_MODE_MASK (int_mode)) == 0)
5839 return CONST0_RTX (int_mode);
5842 /* Don't change the mode of the MEM if that would change the meaning
5843 of the address. */
5844 if (MEM_P (SUBREG_REG (x))
5845 && (MEM_VOLATILE_P (SUBREG_REG (x))
5846 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5847 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5848 return gen_rtx_CLOBBER (mode, const0_rtx);
5850 /* Note that we cannot do any narrowing for non-constants since
5851 we might have been counting on using the fact that some bits were
5852 zero. We now do this in the SET. */
5854 break;
5856 case NEG:
5857 temp = expand_compound_operation (XEXP (x, 0));
5859 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5860 replaced by (lshiftrt X C). This will convert
5861 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5863 if (GET_CODE (temp) == ASHIFTRT
5864 && CONST_INT_P (XEXP (temp, 1))
5865 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5866 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5867 INTVAL (XEXP (temp, 1)));
5869 /* If X has only a single bit that might be nonzero, say, bit I, convert
5870 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5871 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5872 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5873 or a SUBREG of one since we'd be making the expression more
5874 complex if it was just a register. */
5876 if (!REG_P (temp)
5877 && ! (GET_CODE (temp) == SUBREG
5878 && REG_P (SUBREG_REG (temp)))
5879 && is_a <scalar_int_mode> (mode, &int_mode)
5880 && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
5882 rtx temp1 = simplify_shift_const
5883 (NULL_RTX, ASHIFTRT, int_mode,
5884 simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
5885 GET_MODE_PRECISION (int_mode) - 1 - i),
5886 GET_MODE_PRECISION (int_mode) - 1 - i);
5888 /* If all we did was surround TEMP with the two shifts, we
5889 haven't improved anything, so don't use it. Otherwise,
5890 we are better off with TEMP1. */
5891 if (GET_CODE (temp1) != ASHIFTRT
5892 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5893 || XEXP (XEXP (temp1, 0), 0) != temp)
5894 return temp1;
5896 break;
5898 case TRUNCATE:
5899 /* We can't handle truncation to a partial integer mode here
5900 because we don't know the real bitsize of the partial
5901 integer mode. */
5902 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5903 break;
5905 if (HWI_COMPUTABLE_MODE_P (mode))
5906 SUBST (XEXP (x, 0),
5907 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5908 GET_MODE_MASK (mode), 0));
5910 /* We can truncate a constant value and return it. */
5911 if (CONST_INT_P (XEXP (x, 0)))
5912 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5914 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5915 whose value is a comparison can be replaced with a subreg if
5916 STORE_FLAG_VALUE permits. */
5917 if (HWI_COMPUTABLE_MODE_P (mode)
5918 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5919 && (temp = get_last_value (XEXP (x, 0)))
5920 && COMPARISON_P (temp))
5921 return gen_lowpart (mode, XEXP (x, 0));
5922 break;
5924 case CONST:
5925 /* (const (const X)) can become (const X). Do it this way rather than
5926 returning the inner CONST since CONST can be shared with a
5927 REG_EQUAL note. */
5928 if (GET_CODE (XEXP (x, 0)) == CONST)
5929 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5930 break;
5932 case LO_SUM:
5933 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5934 can add in an offset. find_split_point will split this address up
5935 again if it doesn't match. */
5936 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
5937 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5938 return XEXP (x, 1);
5939 break;
5941 case PLUS:
5942 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5943 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5944 bit-field and can be replaced by either a sign_extend or a
5945 sign_extract. The `and' may be a zero_extend and the two
5946 <c>, -<c> constants may be reversed. */
5947 if (GET_CODE (XEXP (x, 0)) == XOR
5948 && is_a <scalar_int_mode> (mode, &int_mode)
5949 && CONST_INT_P (XEXP (x, 1))
5950 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5951 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5952 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5953 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5954 && HWI_COMPUTABLE_MODE_P (int_mode)
5955 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5956 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5957 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5958 == (HOST_WIDE_INT_1U << (i + 1)) - 1))
5959 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5960 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5961 == (unsigned int) i + 1))))
5962 return simplify_shift_const
5963 (NULL_RTX, ASHIFTRT, int_mode,
5964 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
5965 XEXP (XEXP (XEXP (x, 0), 0), 0),
5966 GET_MODE_PRECISION (int_mode) - (i + 1)),
5967 GET_MODE_PRECISION (int_mode) - (i + 1));
5969 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5970 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5971 the bitsize of the mode - 1. This allows simplification of
5972 "a = (b & 8) == 0;" */
5973 if (XEXP (x, 1) == constm1_rtx
5974 && !REG_P (XEXP (x, 0))
5975 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5976 && REG_P (SUBREG_REG (XEXP (x, 0))))
5977 && is_a <scalar_int_mode> (mode, &int_mode)
5978 && nonzero_bits (XEXP (x, 0), int_mode) == 1)
5979 return simplify_shift_const
5980 (NULL_RTX, ASHIFTRT, int_mode,
5981 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
5982 gen_rtx_XOR (int_mode, XEXP (x, 0),
5983 const1_rtx),
5984 GET_MODE_PRECISION (int_mode) - 1),
5985 GET_MODE_PRECISION (int_mode) - 1);
5987 /* If we are adding two things that have no bits in common, convert
5988 the addition into an IOR. This will often be further simplified,
5989 for example in cases like ((a & 1) + (a & 2)), which can
5990 become a & 3. */
5992 if (HWI_COMPUTABLE_MODE_P (mode)
5993 && (nonzero_bits (XEXP (x, 0), mode)
5994 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5996 /* Try to simplify the expression further. */
5997 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5998 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
6000 /* If we could, great. If not, do not go ahead with the IOR
6001 replacement, since PLUS appears in many special purpose
6002 address arithmetic instructions. */
6003 if (GET_CODE (temp) != CLOBBER
6004 && (GET_CODE (temp) != IOR
6005 || ((XEXP (temp, 0) != XEXP (x, 0)
6006 || XEXP (temp, 1) != XEXP (x, 1))
6007 && (XEXP (temp, 0) != XEXP (x, 1)
6008 || XEXP (temp, 1) != XEXP (x, 0)))))
6009 return temp;
6012 /* Canonicalize x + x into x << 1. */
6013 if (GET_MODE_CLASS (mode) == MODE_INT
6014 && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6015 && !side_effects_p (XEXP (x, 0)))
6016 return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6018 break;
6020 case MINUS:
6021 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6022 (and <foo> (const_int pow2-1)) */
6023 if (is_a <scalar_int_mode> (mode, &int_mode)
6024 && GET_CODE (XEXP (x, 1)) == AND
6025 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6026 && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6027 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6028 return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6029 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6030 break;
6032 case MULT:
6033 /* If we have (mult (plus A B) C), apply the distributive law and then
6034 the inverse distributive law to see if things simplify. This
6035 occurs mostly in addresses, often when unrolling loops. */
6037 if (GET_CODE (XEXP (x, 0)) == PLUS)
6039 rtx result = distribute_and_simplify_rtx (x, 0);
6040 if (result)
6041 return result;
6044 /* Try simplify a*(b/c) as (a*b)/c. */
6045 if (FLOAT_MODE_P (mode) && flag_associative_math
6046 && GET_CODE (XEXP (x, 0)) == DIV)
6048 rtx tem = simplify_binary_operation (MULT, mode,
6049 XEXP (XEXP (x, 0), 0),
6050 XEXP (x, 1));
6051 if (tem)
6052 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6054 break;
6056 case UDIV:
6057 /* If this is a divide by a power of two, treat it as a shift if
6058 its first operand is a shift. */
6059 if (is_a <scalar_int_mode> (mode, &int_mode)
6060 && CONST_INT_P (XEXP (x, 1))
6061 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6062 && (GET_CODE (XEXP (x, 0)) == ASHIFT
6063 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6064 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6065 || GET_CODE (XEXP (x, 0)) == ROTATE
6066 || GET_CODE (XEXP (x, 0)) == ROTATERT))
6067 return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6068 XEXP (x, 0), i);
6069 break;
6071 case EQ: case NE:
6072 case GT: case GTU: case GE: case GEU:
6073 case LT: case LTU: case LE: case LEU:
6074 case UNEQ: case LTGT:
6075 case UNGT: case UNGE:
6076 case UNLT: case UNLE:
6077 case UNORDERED: case ORDERED:
6078 /* If the first operand is a condition code, we can't do anything
6079 with it. */
6080 if (GET_CODE (XEXP (x, 0)) == COMPARE
6081 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
6082 && ! CC0_P (XEXP (x, 0))))
6084 rtx op0 = XEXP (x, 0);
6085 rtx op1 = XEXP (x, 1);
6086 enum rtx_code new_code;
6088 if (GET_CODE (op0) == COMPARE)
6089 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6091 /* Simplify our comparison, if possible. */
6092 new_code = simplify_comparison (code, &op0, &op1);
6094 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6095 if only the low-order bit is possibly nonzero in X (such as when
6096 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6097 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6098 known to be either 0 or -1, NE becomes a NEG and EQ becomes
6099 (plus X 1).
6101 Remove any ZERO_EXTRACT we made when thinking this was a
6102 comparison. It may now be simpler to use, e.g., an AND. If a
6103 ZERO_EXTRACT is indeed appropriate, it will be placed back by
6104 the call to make_compound_operation in the SET case.
6106 Don't apply these optimizations if the caller would
6107 prefer a comparison rather than a value.
6108 E.g., for the condition in an IF_THEN_ELSE most targets need
6109 an explicit comparison. */
6111 if (in_cond)
6114 else if (STORE_FLAG_VALUE == 1
6115 && new_code == NE
6116 && is_int_mode (mode, &int_mode)
6117 && op1 == const0_rtx
6118 && int_mode == GET_MODE (op0)
6119 && nonzero_bits (op0, int_mode) == 1)
6120 return gen_lowpart (int_mode,
6121 expand_compound_operation (op0));
6123 else if (STORE_FLAG_VALUE == 1
6124 && new_code == NE
6125 && is_int_mode (mode, &int_mode)
6126 && op1 == const0_rtx
6127 && int_mode == GET_MODE (op0)
6128 && (num_sign_bit_copies (op0, int_mode)
6129 == GET_MODE_PRECISION (int_mode)))
6131 op0 = expand_compound_operation (op0);
6132 return simplify_gen_unary (NEG, int_mode,
6133 gen_lowpart (int_mode, op0),
6134 int_mode);
6137 else if (STORE_FLAG_VALUE == 1
6138 && new_code == EQ
6139 && is_int_mode (mode, &int_mode)
6140 && op1 == const0_rtx
6141 && int_mode == GET_MODE (op0)
6142 && nonzero_bits (op0, int_mode) == 1)
6144 op0 = expand_compound_operation (op0);
6145 return simplify_gen_binary (XOR, int_mode,
6146 gen_lowpart (int_mode, op0),
6147 const1_rtx);
6150 else if (STORE_FLAG_VALUE == 1
6151 && new_code == EQ
6152 && is_int_mode (mode, &int_mode)
6153 && op1 == const0_rtx
6154 && int_mode == GET_MODE (op0)
6155 && (num_sign_bit_copies (op0, int_mode)
6156 == GET_MODE_PRECISION (int_mode)))
6158 op0 = expand_compound_operation (op0);
6159 return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6162 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6163 those above. */
6164 if (in_cond)
6167 else if (STORE_FLAG_VALUE == -1
6168 && new_code == NE
6169 && is_int_mode (mode, &int_mode)
6170 && op1 == const0_rtx
6171 && int_mode == GET_MODE (op0)
6172 && (num_sign_bit_copies (op0, int_mode)
6173 == GET_MODE_PRECISION (int_mode)))
6174 return gen_lowpart (int_mode, expand_compound_operation (op0));
6176 else if (STORE_FLAG_VALUE == -1
6177 && new_code == NE
6178 && is_int_mode (mode, &int_mode)
6179 && op1 == const0_rtx
6180 && int_mode == GET_MODE (op0)
6181 && nonzero_bits (op0, int_mode) == 1)
6183 op0 = expand_compound_operation (op0);
6184 return simplify_gen_unary (NEG, int_mode,
6185 gen_lowpart (int_mode, op0),
6186 int_mode);
6189 else if (STORE_FLAG_VALUE == -1
6190 && new_code == EQ
6191 && is_int_mode (mode, &int_mode)
6192 && op1 == const0_rtx
6193 && int_mode == GET_MODE (op0)
6194 && (num_sign_bit_copies (op0, int_mode)
6195 == GET_MODE_PRECISION (int_mode)))
6197 op0 = expand_compound_operation (op0);
6198 return simplify_gen_unary (NOT, int_mode,
6199 gen_lowpart (int_mode, op0),
6200 int_mode);
6203 /* If X is 0/1, (eq X 0) is X-1. */
6204 else if (STORE_FLAG_VALUE == -1
6205 && new_code == EQ
6206 && is_int_mode (mode, &int_mode)
6207 && op1 == const0_rtx
6208 && int_mode == GET_MODE (op0)
6209 && nonzero_bits (op0, int_mode) == 1)
6211 op0 = expand_compound_operation (op0);
6212 return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6215 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6216 one bit that might be nonzero, we can convert (ne x 0) to
6217 (ashift x c) where C puts the bit in the sign bit. Remove any
6218 AND with STORE_FLAG_VALUE when we are done, since we are only
6219 going to test the sign bit. */
6220 if (new_code == NE
6221 && is_int_mode (mode, &int_mode)
6222 && HWI_COMPUTABLE_MODE_P (int_mode)
6223 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6224 && op1 == const0_rtx
6225 && int_mode == GET_MODE (op0)
6226 && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6228 x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6229 expand_compound_operation (op0),
6230 GET_MODE_PRECISION (int_mode) - 1 - i);
6231 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6232 return XEXP (x, 0);
6233 else
6234 return x;
6237 /* If the code changed, return a whole new comparison.
6238 We also need to avoid using SUBST in cases where
6239 simplify_comparison has widened a comparison with a CONST_INT,
6240 since in that case the wider CONST_INT may fail the sanity
6241 checks in do_SUBST. */
6242 if (new_code != code
6243 || (CONST_INT_P (op1)
6244 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6245 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6246 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6248 /* Otherwise, keep this operation, but maybe change its operands.
6249 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6250 SUBST (XEXP (x, 0), op0);
6251 SUBST (XEXP (x, 1), op1);
6253 break;
6255 case IF_THEN_ELSE:
6256 return simplify_if_then_else (x);
6258 case ZERO_EXTRACT:
6259 case SIGN_EXTRACT:
6260 case ZERO_EXTEND:
6261 case SIGN_EXTEND:
6262 /* If we are processing SET_DEST, we are done. */
6263 if (in_dest)
6264 return x;
6266 return expand_compound_operation (x);
6268 case SET:
6269 return simplify_set (x);
6271 case AND:
6272 case IOR:
6273 return simplify_logical (x);
6275 case ASHIFT:
6276 case LSHIFTRT:
6277 case ASHIFTRT:
6278 case ROTATE:
6279 case ROTATERT:
6280 /* If this is a shift by a constant amount, simplify it. */
6281 if (CONST_INT_P (XEXP (x, 1)))
6282 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6283 INTVAL (XEXP (x, 1)));
6285 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6286 SUBST (XEXP (x, 1),
6287 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6288 (HOST_WIDE_INT_1U
6289 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6290 - 1,
6291 0));
6292 break;
6294 default:
6295 break;
6298 return x;
6301 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6303 static rtx
6304 simplify_if_then_else (rtx x)
6306 machine_mode mode = GET_MODE (x);
6307 rtx cond = XEXP (x, 0);
6308 rtx true_rtx = XEXP (x, 1);
6309 rtx false_rtx = XEXP (x, 2);
6310 enum rtx_code true_code = GET_CODE (cond);
6311 int comparison_p = COMPARISON_P (cond);
6312 rtx temp;
6313 int i;
6314 enum rtx_code false_code;
6315 rtx reversed;
6316 scalar_int_mode int_mode, inner_mode;
6318 /* Simplify storing of the truth value. */
6319 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6320 return simplify_gen_relational (true_code, mode, VOIDmode,
6321 XEXP (cond, 0), XEXP (cond, 1));
6323 /* Also when the truth value has to be reversed. */
6324 if (comparison_p
6325 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6326 && (reversed = reversed_comparison (cond, mode)))
6327 return reversed;
6329 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6330 in it is being compared against certain values. Get the true and false
6331 comparisons and see if that says anything about the value of each arm. */
6333 if (comparison_p
6334 && ((false_code = reversed_comparison_code (cond, NULL))
6335 != UNKNOWN)
6336 && REG_P (XEXP (cond, 0)))
6338 HOST_WIDE_INT nzb;
6339 rtx from = XEXP (cond, 0);
6340 rtx true_val = XEXP (cond, 1);
6341 rtx false_val = true_val;
6342 int swapped = 0;
6344 /* If FALSE_CODE is EQ, swap the codes and arms. */
6346 if (false_code == EQ)
6348 swapped = 1, true_code = EQ, false_code = NE;
6349 std::swap (true_rtx, false_rtx);
6352 scalar_int_mode from_mode;
6353 if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6355 /* If we are comparing against zero and the expression being
6356 tested has only a single bit that might be nonzero, that is
6357 its value when it is not equal to zero. Similarly if it is
6358 known to be -1 or 0. */
6359 if (true_code == EQ
6360 && true_val == const0_rtx
6361 && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6363 false_code = EQ;
6364 false_val = gen_int_mode (nzb, from_mode);
6366 else if (true_code == EQ
6367 && true_val == const0_rtx
6368 && (num_sign_bit_copies (from, from_mode)
6369 == GET_MODE_PRECISION (from_mode)))
6371 false_code = EQ;
6372 false_val = constm1_rtx;
6376 /* Now simplify an arm if we know the value of the register in the
6377 branch and it is used in the arm. Be careful due to the potential
6378 of locally-shared RTL. */
6380 if (reg_mentioned_p (from, true_rtx))
6381 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6382 from, true_val),
6383 pc_rtx, pc_rtx, 0, 0, 0);
6384 if (reg_mentioned_p (from, false_rtx))
6385 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6386 from, false_val),
6387 pc_rtx, pc_rtx, 0, 0, 0);
6389 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6390 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6392 true_rtx = XEXP (x, 1);
6393 false_rtx = XEXP (x, 2);
6394 true_code = GET_CODE (cond);
6397 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6398 reversed, do so to avoid needing two sets of patterns for
6399 subtract-and-branch insns. Similarly if we have a constant in the true
6400 arm, the false arm is the same as the first operand of the comparison, or
6401 the false arm is more complicated than the true arm. */
6403 if (comparison_p
6404 && reversed_comparison_code (cond, NULL) != UNKNOWN
6405 && (true_rtx == pc_rtx
6406 || (CONSTANT_P (true_rtx)
6407 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6408 || true_rtx == const0_rtx
6409 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6410 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6411 && !OBJECT_P (false_rtx))
6412 || reg_mentioned_p (true_rtx, false_rtx)
6413 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6415 true_code = reversed_comparison_code (cond, NULL);
6416 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6417 SUBST (XEXP (x, 1), false_rtx);
6418 SUBST (XEXP (x, 2), true_rtx);
6420 std::swap (true_rtx, false_rtx);
6421 cond = XEXP (x, 0);
6423 /* It is possible that the conditional has been simplified out. */
6424 true_code = GET_CODE (cond);
6425 comparison_p = COMPARISON_P (cond);
6428 /* If the two arms are identical, we don't need the comparison. */
6430 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6431 return true_rtx;
6433 /* Convert a == b ? b : a to "a". */
6434 if (true_code == EQ && ! side_effects_p (cond)
6435 && !HONOR_NANS (mode)
6436 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6437 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6438 return false_rtx;
6439 else if (true_code == NE && ! side_effects_p (cond)
6440 && !HONOR_NANS (mode)
6441 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6442 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6443 return true_rtx;
6445 /* Look for cases where we have (abs x) or (neg (abs X)). */
6447 if (GET_MODE_CLASS (mode) == MODE_INT
6448 && comparison_p
6449 && XEXP (cond, 1) == const0_rtx
6450 && GET_CODE (false_rtx) == NEG
6451 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6452 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6453 && ! side_effects_p (true_rtx))
6454 switch (true_code)
6456 case GT:
6457 case GE:
6458 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6459 case LT:
6460 case LE:
6461 return
6462 simplify_gen_unary (NEG, mode,
6463 simplify_gen_unary (ABS, mode, true_rtx, mode),
6464 mode);
6465 default:
6466 break;
6469 /* Look for MIN or MAX. */
6471 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6472 && comparison_p
6473 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6474 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6475 && ! side_effects_p (cond))
6476 switch (true_code)
6478 case GE:
6479 case GT:
6480 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6481 case LE:
6482 case LT:
6483 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6484 case GEU:
6485 case GTU:
6486 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6487 case LEU:
6488 case LTU:
6489 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6490 default:
6491 break;
6494 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6495 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6496 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6497 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6498 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6499 neither 1 or -1, but it isn't worth checking for. */
6501 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6502 && comparison_p
6503 && is_int_mode (mode, &int_mode)
6504 && ! side_effects_p (x))
6506 rtx t = make_compound_operation (true_rtx, SET);
6507 rtx f = make_compound_operation (false_rtx, SET);
6508 rtx cond_op0 = XEXP (cond, 0);
6509 rtx cond_op1 = XEXP (cond, 1);
6510 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6511 scalar_int_mode m = int_mode;
6512 rtx z = 0, c1 = NULL_RTX;
6514 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6515 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6516 || GET_CODE (t) == ASHIFT
6517 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6518 && rtx_equal_p (XEXP (t, 0), f))
6519 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6521 /* If an identity-zero op is commutative, check whether there
6522 would be a match if we swapped the operands. */
6523 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6524 || GET_CODE (t) == XOR)
6525 && rtx_equal_p (XEXP (t, 1), f))
6526 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6527 else if (GET_CODE (t) == SIGN_EXTEND
6528 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6529 && (GET_CODE (XEXP (t, 0)) == PLUS
6530 || GET_CODE (XEXP (t, 0)) == MINUS
6531 || GET_CODE (XEXP (t, 0)) == IOR
6532 || GET_CODE (XEXP (t, 0)) == XOR
6533 || GET_CODE (XEXP (t, 0)) == ASHIFT
6534 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6535 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6536 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6537 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6538 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6539 && (num_sign_bit_copies (f, GET_MODE (f))
6540 > (unsigned int)
6541 (GET_MODE_PRECISION (int_mode)
6542 - GET_MODE_PRECISION (inner_mode))))
6544 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6545 extend_op = SIGN_EXTEND;
6546 m = inner_mode;
6548 else if (GET_CODE (t) == SIGN_EXTEND
6549 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6550 && (GET_CODE (XEXP (t, 0)) == PLUS
6551 || GET_CODE (XEXP (t, 0)) == IOR
6552 || GET_CODE (XEXP (t, 0)) == XOR)
6553 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6554 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6555 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6556 && (num_sign_bit_copies (f, GET_MODE (f))
6557 > (unsigned int)
6558 (GET_MODE_PRECISION (int_mode)
6559 - GET_MODE_PRECISION (inner_mode))))
6561 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6562 extend_op = SIGN_EXTEND;
6563 m = inner_mode;
6565 else if (GET_CODE (t) == ZERO_EXTEND
6566 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6567 && (GET_CODE (XEXP (t, 0)) == PLUS
6568 || GET_CODE (XEXP (t, 0)) == MINUS
6569 || GET_CODE (XEXP (t, 0)) == IOR
6570 || GET_CODE (XEXP (t, 0)) == XOR
6571 || GET_CODE (XEXP (t, 0)) == ASHIFT
6572 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6573 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6574 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6575 && HWI_COMPUTABLE_MODE_P (int_mode)
6576 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6577 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6578 && ((nonzero_bits (f, GET_MODE (f))
6579 & ~GET_MODE_MASK (inner_mode))
6580 == 0))
6582 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6583 extend_op = ZERO_EXTEND;
6584 m = inner_mode;
6586 else if (GET_CODE (t) == ZERO_EXTEND
6587 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6588 && (GET_CODE (XEXP (t, 0)) == PLUS
6589 || GET_CODE (XEXP (t, 0)) == IOR
6590 || GET_CODE (XEXP (t, 0)) == XOR)
6591 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6592 && HWI_COMPUTABLE_MODE_P (int_mode)
6593 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6594 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6595 && ((nonzero_bits (f, GET_MODE (f))
6596 & ~GET_MODE_MASK (inner_mode))
6597 == 0))
6599 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6600 extend_op = ZERO_EXTEND;
6601 m = inner_mode;
6604 if (z)
6606 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6607 cond_op0, cond_op1),
6608 pc_rtx, pc_rtx, 0, 0, 0);
6609 temp = simplify_gen_binary (MULT, m, temp,
6610 simplify_gen_binary (MULT, m, c1,
6611 const_true_rtx));
6612 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6613 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6615 if (extend_op != UNKNOWN)
6616 temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6618 return temp;
6622 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6623 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6624 negation of a single bit, we can convert this operation to a shift. We
6625 can actually do this more generally, but it doesn't seem worth it. */
6627 if (true_code == NE
6628 && is_a <scalar_int_mode> (mode, &int_mode)
6629 && XEXP (cond, 1) == const0_rtx
6630 && false_rtx == const0_rtx
6631 && CONST_INT_P (true_rtx)
6632 && ((1 == nonzero_bits (XEXP (cond, 0), int_mode)
6633 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6634 || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6635 == GET_MODE_PRECISION (int_mode))
6636 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6637 return
6638 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6639 gen_lowpart (int_mode, XEXP (cond, 0)), i);
6641 /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6642 non-zero bit in A is C1. */
6643 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6644 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6645 && is_a <scalar_int_mode> (mode, &int_mode)
6646 && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6647 && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6648 == nonzero_bits (XEXP (cond, 0), inner_mode)
6649 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6651 rtx val = XEXP (cond, 0);
6652 if (inner_mode == int_mode)
6653 return val;
6654 else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6655 return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6658 return x;
6661 /* Simplify X, a SET expression. Return the new expression. */
6663 static rtx
6664 simplify_set (rtx x)
6666 rtx src = SET_SRC (x);
6667 rtx dest = SET_DEST (x);
6668 machine_mode mode
6669 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6670 rtx_insn *other_insn;
6671 rtx *cc_use;
6672 scalar_int_mode int_mode;
6674 /* (set (pc) (return)) gets written as (return). */
6675 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6676 return src;
6678 /* Now that we know for sure which bits of SRC we are using, see if we can
6679 simplify the expression for the object knowing that we only need the
6680 low-order bits. */
6682 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6684 src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, 0);
6685 SUBST (SET_SRC (x), src);
6688 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6689 the comparison result and try to simplify it unless we already have used
6690 undobuf.other_insn. */
6691 if ((GET_MODE_CLASS (mode) == MODE_CC
6692 || GET_CODE (src) == COMPARE
6693 || CC0_P (dest))
6694 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6695 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6696 && COMPARISON_P (*cc_use)
6697 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6699 enum rtx_code old_code = GET_CODE (*cc_use);
6700 enum rtx_code new_code;
6701 rtx op0, op1, tmp;
6702 int other_changed = 0;
6703 rtx inner_compare = NULL_RTX;
6704 machine_mode compare_mode = GET_MODE (dest);
6706 if (GET_CODE (src) == COMPARE)
6708 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6709 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6711 inner_compare = op0;
6712 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6715 else
6716 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6718 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6719 op0, op1);
6720 if (!tmp)
6721 new_code = old_code;
6722 else if (!CONSTANT_P (tmp))
6724 new_code = GET_CODE (tmp);
6725 op0 = XEXP (tmp, 0);
6726 op1 = XEXP (tmp, 1);
6728 else
6730 rtx pat = PATTERN (other_insn);
6731 undobuf.other_insn = other_insn;
6732 SUBST (*cc_use, tmp);
6734 /* Attempt to simplify CC user. */
6735 if (GET_CODE (pat) == SET)
6737 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6738 if (new_rtx != NULL_RTX)
6739 SUBST (SET_SRC (pat), new_rtx);
6742 /* Convert X into a no-op move. */
6743 SUBST (SET_DEST (x), pc_rtx);
6744 SUBST (SET_SRC (x), pc_rtx);
6745 return x;
6748 /* Simplify our comparison, if possible. */
6749 new_code = simplify_comparison (new_code, &op0, &op1);
6751 #ifdef SELECT_CC_MODE
6752 /* If this machine has CC modes other than CCmode, check to see if we
6753 need to use a different CC mode here. */
6754 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6755 compare_mode = GET_MODE (op0);
6756 else if (inner_compare
6757 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6758 && new_code == old_code
6759 && op0 == XEXP (inner_compare, 0)
6760 && op1 == XEXP (inner_compare, 1))
6761 compare_mode = GET_MODE (inner_compare);
6762 else
6763 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6765 /* If the mode changed, we have to change SET_DEST, the mode in the
6766 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6767 a hard register, just build new versions with the proper mode. If it
6768 is a pseudo, we lose unless it is only time we set the pseudo, in
6769 which case we can safely change its mode. */
6770 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6772 if (can_change_dest_mode (dest, 0, compare_mode))
6774 unsigned int regno = REGNO (dest);
6775 rtx new_dest;
6777 if (regno < FIRST_PSEUDO_REGISTER)
6778 new_dest = gen_rtx_REG (compare_mode, regno);
6779 else
6781 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6782 new_dest = regno_reg_rtx[regno];
6785 SUBST (SET_DEST (x), new_dest);
6786 SUBST (XEXP (*cc_use, 0), new_dest);
6787 other_changed = 1;
6789 dest = new_dest;
6792 #endif /* SELECT_CC_MODE */
6794 /* If the code changed, we have to build a new comparison in
6795 undobuf.other_insn. */
6796 if (new_code != old_code)
6798 int other_changed_previously = other_changed;
6799 unsigned HOST_WIDE_INT mask;
6800 rtx old_cc_use = *cc_use;
6802 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6803 dest, const0_rtx));
6804 other_changed = 1;
6806 /* If the only change we made was to change an EQ into an NE or
6807 vice versa, OP0 has only one bit that might be nonzero, and OP1
6808 is zero, check if changing the user of the condition code will
6809 produce a valid insn. If it won't, we can keep the original code
6810 in that insn by surrounding our operation with an XOR. */
6812 if (((old_code == NE && new_code == EQ)
6813 || (old_code == EQ && new_code == NE))
6814 && ! other_changed_previously && op1 == const0_rtx
6815 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6816 && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6818 rtx pat = PATTERN (other_insn), note = 0;
6820 if ((recog_for_combine (&pat, other_insn, &note) < 0
6821 && ! check_asm_operands (pat)))
6823 *cc_use = old_cc_use;
6824 other_changed = 0;
6826 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6827 gen_int_mode (mask,
6828 GET_MODE (op0)));
6833 if (other_changed)
6834 undobuf.other_insn = other_insn;
6836 /* Don't generate a compare of a CC with 0, just use that CC. */
6837 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6839 SUBST (SET_SRC (x), op0);
6840 src = SET_SRC (x);
6842 /* Otherwise, if we didn't previously have the same COMPARE we
6843 want, create it from scratch. */
6844 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6845 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6847 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6848 src = SET_SRC (x);
6851 else
6853 /* Get SET_SRC in a form where we have placed back any
6854 compound expressions. Then do the checks below. */
6855 src = make_compound_operation (src, SET);
6856 SUBST (SET_SRC (x), src);
6859 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6860 and X being a REG or (subreg (reg)), we may be able to convert this to
6861 (set (subreg:m2 x) (op)).
6863 We can always do this if M1 is narrower than M2 because that means that
6864 we only care about the low bits of the result.
6866 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6867 perform a narrower operation than requested since the high-order bits will
6868 be undefined. On machine where it is defined, this transformation is safe
6869 as long as M1 and M2 have the same number of words. */
6871 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6872 && !OBJECT_P (SUBREG_REG (src))
6873 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6874 / UNITS_PER_WORD)
6875 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6876 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6877 && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
6878 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6879 && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
6880 GET_MODE (SUBREG_REG (src)),
6881 GET_MODE (src)))
6882 && (REG_P (dest)
6883 || (GET_CODE (dest) == SUBREG
6884 && REG_P (SUBREG_REG (dest)))))
6886 SUBST (SET_DEST (x),
6887 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6888 dest));
6889 SUBST (SET_SRC (x), SUBREG_REG (src));
6891 src = SET_SRC (x), dest = SET_DEST (x);
6894 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6895 in SRC. */
6896 if (dest == cc0_rtx
6897 && partial_subreg_p (src)
6898 && subreg_lowpart_p (src))
6900 rtx inner = SUBREG_REG (src);
6901 machine_mode inner_mode = GET_MODE (inner);
6903 /* Here we make sure that we don't have a sign bit on. */
6904 if (val_signbit_known_clear_p (GET_MODE (src),
6905 nonzero_bits (inner, inner_mode)))
6907 SUBST (SET_SRC (x), inner);
6908 src = SET_SRC (x);
6912 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6913 would require a paradoxical subreg. Replace the subreg with a
6914 zero_extend to avoid the reload that would otherwise be required. */
6916 enum rtx_code extend_op;
6917 if (paradoxical_subreg_p (src)
6918 && MEM_P (SUBREG_REG (src))
6919 && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
6921 SUBST (SET_SRC (x),
6922 gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
6924 src = SET_SRC (x);
6927 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6928 are comparing an item known to be 0 or -1 against 0, use a logical
6929 operation instead. Check for one of the arms being an IOR of the other
6930 arm with some value. We compute three terms to be IOR'ed together. In
6931 practice, at most two will be nonzero. Then we do the IOR's. */
6933 if (GET_CODE (dest) != PC
6934 && GET_CODE (src) == IF_THEN_ELSE
6935 && is_int_mode (GET_MODE (src), &int_mode)
6936 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6937 && XEXP (XEXP (src, 0), 1) == const0_rtx
6938 && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
6939 && (!HAVE_conditional_move
6940 || ! can_conditionally_move_p (int_mode))
6941 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
6942 == GET_MODE_PRECISION (int_mode))
6943 && ! side_effects_p (src))
6945 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6946 ? XEXP (src, 1) : XEXP (src, 2));
6947 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6948 ? XEXP (src, 2) : XEXP (src, 1));
6949 rtx term1 = const0_rtx, term2, term3;
6951 if (GET_CODE (true_rtx) == IOR
6952 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6953 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6954 else if (GET_CODE (true_rtx) == IOR
6955 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6956 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6957 else if (GET_CODE (false_rtx) == IOR
6958 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6959 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6960 else if (GET_CODE (false_rtx) == IOR
6961 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6962 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6964 term2 = simplify_gen_binary (AND, int_mode,
6965 XEXP (XEXP (src, 0), 0), true_rtx);
6966 term3 = simplify_gen_binary (AND, int_mode,
6967 simplify_gen_unary (NOT, int_mode,
6968 XEXP (XEXP (src, 0), 0),
6969 int_mode),
6970 false_rtx);
6972 SUBST (SET_SRC (x),
6973 simplify_gen_binary (IOR, int_mode,
6974 simplify_gen_binary (IOR, int_mode,
6975 term1, term2),
6976 term3));
6978 src = SET_SRC (x);
6981 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6982 whole thing fail. */
6983 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6984 return src;
6985 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6986 return dest;
6987 else
6988 /* Convert this into a field assignment operation, if possible. */
6989 return make_field_assignment (x);
6992 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6993 result. */
6995 static rtx
6996 simplify_logical (rtx x)
6998 rtx op0 = XEXP (x, 0);
6999 rtx op1 = XEXP (x, 1);
7000 scalar_int_mode mode;
7002 switch (GET_CODE (x))
7004 case AND:
7005 /* We can call simplify_and_const_int only if we don't lose
7006 any (sign) bits when converting INTVAL (op1) to
7007 "unsigned HOST_WIDE_INT". */
7008 if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7009 && CONST_INT_P (op1)
7010 && (HWI_COMPUTABLE_MODE_P (mode)
7011 || INTVAL (op1) > 0))
7013 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7014 if (GET_CODE (x) != AND)
7015 return x;
7017 op0 = XEXP (x, 0);
7018 op1 = XEXP (x, 1);
7021 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7022 apply the distributive law and then the inverse distributive
7023 law to see if things simplify. */
7024 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7026 rtx result = distribute_and_simplify_rtx (x, 0);
7027 if (result)
7028 return result;
7030 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7032 rtx result = distribute_and_simplify_rtx (x, 1);
7033 if (result)
7034 return result;
7036 break;
7038 case IOR:
7039 /* If we have (ior (and A B) C), apply the distributive law and then
7040 the inverse distributive law to see if things simplify. */
7042 if (GET_CODE (op0) == AND)
7044 rtx result = distribute_and_simplify_rtx (x, 0);
7045 if (result)
7046 return result;
7049 if (GET_CODE (op1) == AND)
7051 rtx result = distribute_and_simplify_rtx (x, 1);
7052 if (result)
7053 return result;
7055 break;
7057 default:
7058 gcc_unreachable ();
7061 return x;
7064 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7065 operations" because they can be replaced with two more basic operations.
7066 ZERO_EXTEND is also considered "compound" because it can be replaced with
7067 an AND operation, which is simpler, though only one operation.
7069 The function expand_compound_operation is called with an rtx expression
7070 and will convert it to the appropriate shifts and AND operations,
7071 simplifying at each stage.
7073 The function make_compound_operation is called to convert an expression
7074 consisting of shifts and ANDs into the equivalent compound expression.
7075 It is the inverse of this function, loosely speaking. */
7077 static rtx
7078 expand_compound_operation (rtx x)
7080 unsigned HOST_WIDE_INT pos = 0, len;
7081 int unsignedp = 0;
7082 unsigned int modewidth;
7083 rtx tem;
7084 scalar_int_mode inner_mode;
7086 switch (GET_CODE (x))
7088 case ZERO_EXTEND:
7089 unsignedp = 1;
7090 /* FALLTHRU */
7091 case SIGN_EXTEND:
7092 /* We can't necessarily use a const_int for a multiword mode;
7093 it depends on implicitly extending the value.
7094 Since we don't know the right way to extend it,
7095 we can't tell whether the implicit way is right.
7097 Even for a mode that is no wider than a const_int,
7098 we can't win, because we need to sign extend one of its bits through
7099 the rest of it, and we don't know which bit. */
7100 if (CONST_INT_P (XEXP (x, 0)))
7101 return x;
7103 /* Reject modes that aren't scalar integers because turning vector
7104 or complex modes into shifts causes problems. */
7105 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7106 return x;
7108 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7109 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7110 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7111 reloaded. If not for that, MEM's would very rarely be safe.
7113 Reject modes bigger than a word, because we might not be able
7114 to reference a two-register group starting with an arbitrary register
7115 (and currently gen_lowpart might crash for a SUBREG). */
7117 if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7118 return x;
7120 len = GET_MODE_PRECISION (inner_mode);
7121 /* If the inner object has VOIDmode (the only way this can happen
7122 is if it is an ASM_OPERANDS), we can't do anything since we don't
7123 know how much masking to do. */
7124 if (len == 0)
7125 return x;
7127 break;
7129 case ZERO_EXTRACT:
7130 unsignedp = 1;
7132 /* fall through */
7134 case SIGN_EXTRACT:
7135 /* If the operand is a CLOBBER, just return it. */
7136 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7137 return XEXP (x, 0);
7139 if (!CONST_INT_P (XEXP (x, 1))
7140 || !CONST_INT_P (XEXP (x, 2)))
7141 return x;
7143 /* Reject modes that aren't scalar integers because turning vector
7144 or complex modes into shifts causes problems. */
7145 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7146 return x;
7148 len = INTVAL (XEXP (x, 1));
7149 pos = INTVAL (XEXP (x, 2));
7151 /* This should stay within the object being extracted, fail otherwise. */
7152 if (len + pos > GET_MODE_PRECISION (inner_mode))
7153 return x;
7155 if (BITS_BIG_ENDIAN)
7156 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7158 break;
7160 default:
7161 return x;
7164 /* We've rejected non-scalar operations by now. */
7165 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7167 /* Convert sign extension to zero extension, if we know that the high
7168 bit is not set, as this is easier to optimize. It will be converted
7169 back to cheaper alternative in make_extraction. */
7170 if (GET_CODE (x) == SIGN_EXTEND
7171 && HWI_COMPUTABLE_MODE_P (mode)
7172 && ((nonzero_bits (XEXP (x, 0), inner_mode)
7173 & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7174 == 0))
7176 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7177 rtx temp2 = expand_compound_operation (temp);
7179 /* Make sure this is a profitable operation. */
7180 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7181 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7182 return temp2;
7183 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7184 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7185 return temp;
7186 else
7187 return x;
7190 /* We can optimize some special cases of ZERO_EXTEND. */
7191 if (GET_CODE (x) == ZERO_EXTEND)
7193 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7194 know that the last value didn't have any inappropriate bits
7195 set. */
7196 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7197 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7198 && HWI_COMPUTABLE_MODE_P (mode)
7199 && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7200 & ~GET_MODE_MASK (inner_mode)) == 0)
7201 return XEXP (XEXP (x, 0), 0);
7203 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7204 if (GET_CODE (XEXP (x, 0)) == SUBREG
7205 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7206 && subreg_lowpart_p (XEXP (x, 0))
7207 && HWI_COMPUTABLE_MODE_P (mode)
7208 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7209 & ~GET_MODE_MASK (inner_mode)) == 0)
7210 return SUBREG_REG (XEXP (x, 0));
7212 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7213 is a comparison and STORE_FLAG_VALUE permits. This is like
7214 the first case, but it works even when MODE is larger
7215 than HOST_WIDE_INT. */
7216 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7217 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7218 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7219 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7220 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7221 return XEXP (XEXP (x, 0), 0);
7223 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7224 if (GET_CODE (XEXP (x, 0)) == SUBREG
7225 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7226 && subreg_lowpart_p (XEXP (x, 0))
7227 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7228 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7229 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7230 return SUBREG_REG (XEXP (x, 0));
7234 /* If we reach here, we want to return a pair of shifts. The inner
7235 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7236 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7237 logical depending on the value of UNSIGNEDP.
7239 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7240 converted into an AND of a shift.
7242 We must check for the case where the left shift would have a negative
7243 count. This can happen in a case like (x >> 31) & 255 on machines
7244 that can't shift by a constant. On those machines, we would first
7245 combine the shift with the AND to produce a variable-position
7246 extraction. Then the constant of 31 would be substituted in
7247 to produce such a position. */
7249 modewidth = GET_MODE_PRECISION (mode);
7250 if (modewidth >= pos + len)
7252 tem = gen_lowpart (mode, XEXP (x, 0));
7253 if (!tem || GET_CODE (tem) == CLOBBER)
7254 return x;
7255 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7256 tem, modewidth - pos - len);
7257 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7258 mode, tem, modewidth - len);
7260 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7261 tem = simplify_and_const_int (NULL_RTX, mode,
7262 simplify_shift_const (NULL_RTX, LSHIFTRT,
7263 mode, XEXP (x, 0),
7264 pos),
7265 (HOST_WIDE_INT_1U << len) - 1);
7266 else
7267 /* Any other cases we can't handle. */
7268 return x;
7270 /* If we couldn't do this for some reason, return the original
7271 expression. */
7272 if (GET_CODE (tem) == CLOBBER)
7273 return x;
7275 return tem;
7278 /* X is a SET which contains an assignment of one object into
7279 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7280 or certain SUBREGS). If possible, convert it into a series of
7281 logical operations.
7283 We half-heartedly support variable positions, but do not at all
7284 support variable lengths. */
7286 static const_rtx
7287 expand_field_assignment (const_rtx x)
7289 rtx inner;
7290 rtx pos; /* Always counts from low bit. */
7291 int len;
7292 rtx mask, cleared, masked;
7293 scalar_int_mode compute_mode;
7295 /* Loop until we find something we can't simplify. */
7296 while (1)
7298 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7299 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7301 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7302 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7303 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7305 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7306 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7308 inner = XEXP (SET_DEST (x), 0);
7309 len = INTVAL (XEXP (SET_DEST (x), 1));
7310 pos = XEXP (SET_DEST (x), 2);
7312 /* A constant position should stay within the width of INNER. */
7313 if (CONST_INT_P (pos)
7314 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7315 break;
7317 if (BITS_BIG_ENDIAN)
7319 if (CONST_INT_P (pos))
7320 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7321 - INTVAL (pos));
7322 else if (GET_CODE (pos) == MINUS
7323 && CONST_INT_P (XEXP (pos, 1))
7324 && (INTVAL (XEXP (pos, 1))
7325 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7326 /* If position is ADJUST - X, new position is X. */
7327 pos = XEXP (pos, 0);
7328 else
7330 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7331 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7332 gen_int_mode (prec - len,
7333 GET_MODE (pos)),
7334 pos);
7339 /* A SUBREG between two modes that occupy the same numbers of words
7340 can be done by moving the SUBREG to the source. */
7341 else if (GET_CODE (SET_DEST (x)) == SUBREG
7342 /* We need SUBREGs to compute nonzero_bits properly. */
7343 && nonzero_sign_valid
7344 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7345 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7346 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7347 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7349 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7350 gen_lowpart
7351 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7352 SET_SRC (x)));
7353 continue;
7355 else
7356 break;
7358 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7359 inner = SUBREG_REG (inner);
7361 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7362 if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7364 /* Don't do anything for vector or complex integral types. */
7365 if (! FLOAT_MODE_P (GET_MODE (inner)))
7366 break;
7368 /* Try to find an integral mode to pun with. */
7369 if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7370 .exists (&compute_mode))
7371 break;
7373 inner = gen_lowpart (compute_mode, inner);
7376 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7377 if (len >= HOST_BITS_PER_WIDE_INT)
7378 break;
7380 /* Don't try to compute in too wide unsupported modes. */
7381 if (!targetm.scalar_mode_supported_p (compute_mode))
7382 break;
7384 /* Now compute the equivalent expression. Make a copy of INNER
7385 for the SET_DEST in case it is a MEM into which we will substitute;
7386 we don't want shared RTL in that case. */
7387 mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7388 compute_mode);
7389 cleared = simplify_gen_binary (AND, compute_mode,
7390 simplify_gen_unary (NOT, compute_mode,
7391 simplify_gen_binary (ASHIFT,
7392 compute_mode,
7393 mask, pos),
7394 compute_mode),
7395 inner);
7396 masked = simplify_gen_binary (ASHIFT, compute_mode,
7397 simplify_gen_binary (
7398 AND, compute_mode,
7399 gen_lowpart (compute_mode, SET_SRC (x)),
7400 mask),
7401 pos);
7403 x = gen_rtx_SET (copy_rtx (inner),
7404 simplify_gen_binary (IOR, compute_mode,
7405 cleared, masked));
7408 return x;
7411 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7412 it is an RTX that represents the (variable) starting position; otherwise,
7413 POS is the (constant) starting bit position. Both are counted from the LSB.
7415 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7417 IN_DEST is nonzero if this is a reference in the destination of a SET.
7418 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7419 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7420 be used.
7422 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7423 ZERO_EXTRACT should be built even for bits starting at bit 0.
7425 MODE is the desired mode of the result (if IN_DEST == 0).
7427 The result is an RTX for the extraction or NULL_RTX if the target
7428 can't handle it. */
7430 static rtx
7431 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7432 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7433 int in_dest, int in_compare)
7435 /* This mode describes the size of the storage area
7436 to fetch the overall value from. Within that, we
7437 ignore the POS lowest bits, etc. */
7438 machine_mode is_mode = GET_MODE (inner);
7439 machine_mode inner_mode;
7440 scalar_int_mode wanted_inner_mode;
7441 scalar_int_mode wanted_inner_reg_mode = word_mode;
7442 scalar_int_mode pos_mode = word_mode;
7443 machine_mode extraction_mode = word_mode;
7444 rtx new_rtx = 0;
7445 rtx orig_pos_rtx = pos_rtx;
7446 HOST_WIDE_INT orig_pos;
7448 if (pos_rtx && CONST_INT_P (pos_rtx))
7449 pos = INTVAL (pos_rtx), pos_rtx = 0;
7451 if (GET_CODE (inner) == SUBREG
7452 && subreg_lowpart_p (inner)
7453 && (paradoxical_subreg_p (inner)
7454 /* If trying or potentionally trying to extract
7455 bits outside of is_mode, don't look through
7456 non-paradoxical SUBREGs. See PR82192. */
7457 || (pos_rtx == NULL_RTX
7458 && pos + len <= GET_MODE_PRECISION (is_mode))))
7460 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7461 consider just the QI as the memory to extract from.
7462 The subreg adds or removes high bits; its mode is
7463 irrelevant to the meaning of this extraction,
7464 since POS and LEN count from the lsb. */
7465 if (MEM_P (SUBREG_REG (inner)))
7466 is_mode = GET_MODE (SUBREG_REG (inner));
7467 inner = SUBREG_REG (inner);
7469 else if (GET_CODE (inner) == ASHIFT
7470 && CONST_INT_P (XEXP (inner, 1))
7471 && pos_rtx == 0 && pos == 0
7472 && len > UINTVAL (XEXP (inner, 1)))
7474 /* We're extracting the least significant bits of an rtx
7475 (ashift X (const_int C)), where LEN > C. Extract the
7476 least significant (LEN - C) bits of X, giving an rtx
7477 whose mode is MODE, then shift it left C times. */
7478 new_rtx = make_extraction (mode, XEXP (inner, 0),
7479 0, 0, len - INTVAL (XEXP (inner, 1)),
7480 unsignedp, in_dest, in_compare);
7481 if (new_rtx != 0)
7482 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7484 else if (GET_CODE (inner) == TRUNCATE
7485 /* If trying or potentionally trying to extract
7486 bits outside of is_mode, don't look through
7487 TRUNCATE. See PR82192. */
7488 && pos_rtx == NULL_RTX
7489 && pos + len <= GET_MODE_PRECISION (is_mode))
7490 inner = XEXP (inner, 0);
7492 inner_mode = GET_MODE (inner);
7494 /* See if this can be done without an extraction. We never can if the
7495 width of the field is not the same as that of some integer mode. For
7496 registers, we can only avoid the extraction if the position is at the
7497 low-order bit and this is either not in the destination or we have the
7498 appropriate STRICT_LOW_PART operation available.
7500 For MEM, we can avoid an extract if the field starts on an appropriate
7501 boundary and we can change the mode of the memory reference. */
7503 scalar_int_mode tmode;
7504 if (int_mode_for_size (len, 1).exists (&tmode)
7505 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7506 && !MEM_P (inner)
7507 && (pos == 0 || REG_P (inner))
7508 && (inner_mode == tmode
7509 || !REG_P (inner)
7510 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7511 || reg_truncated_to_mode (tmode, inner))
7512 && (! in_dest
7513 || (REG_P (inner)
7514 && have_insn_for (STRICT_LOW_PART, tmode))))
7515 || (MEM_P (inner) && pos_rtx == 0
7516 && (pos
7517 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7518 : BITS_PER_UNIT)) == 0
7519 /* We can't do this if we are widening INNER_MODE (it
7520 may not be aligned, for one thing). */
7521 && !paradoxical_subreg_p (tmode, inner_mode)
7522 && (inner_mode == tmode
7523 || (! mode_dependent_address_p (XEXP (inner, 0),
7524 MEM_ADDR_SPACE (inner))
7525 && ! MEM_VOLATILE_P (inner))))))
7527 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7528 field. If the original and current mode are the same, we need not
7529 adjust the offset. Otherwise, we do if bytes big endian.
7531 If INNER is not a MEM, get a piece consisting of just the field
7532 of interest (in this case POS % BITS_PER_WORD must be 0). */
7534 if (MEM_P (inner))
7536 HOST_WIDE_INT offset;
7538 /* POS counts from lsb, but make OFFSET count in memory order. */
7539 if (BYTES_BIG_ENDIAN)
7540 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7541 else
7542 offset = pos / BITS_PER_UNIT;
7544 new_rtx = adjust_address_nv (inner, tmode, offset);
7546 else if (REG_P (inner))
7548 if (tmode != inner_mode)
7550 /* We can't call gen_lowpart in a DEST since we
7551 always want a SUBREG (see below) and it would sometimes
7552 return a new hard register. */
7553 if (pos || in_dest)
7555 unsigned int offset
7556 = subreg_offset_from_lsb (tmode, inner_mode, pos);
7558 /* Avoid creating invalid subregs, for example when
7559 simplifying (x>>32)&255. */
7560 if (!validate_subreg (tmode, inner_mode, inner, offset))
7561 return NULL_RTX;
7563 new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7565 else
7566 new_rtx = gen_lowpart (tmode, inner);
7568 else
7569 new_rtx = inner;
7571 else
7572 new_rtx = force_to_mode (inner, tmode,
7573 len >= HOST_BITS_PER_WIDE_INT
7574 ? HOST_WIDE_INT_M1U
7575 : (HOST_WIDE_INT_1U << len) - 1, 0);
7577 /* If this extraction is going into the destination of a SET,
7578 make a STRICT_LOW_PART unless we made a MEM. */
7580 if (in_dest)
7581 return (MEM_P (new_rtx) ? new_rtx
7582 : (GET_CODE (new_rtx) != SUBREG
7583 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7584 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7586 if (mode == tmode)
7587 return new_rtx;
7589 if (CONST_SCALAR_INT_P (new_rtx))
7590 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7591 mode, new_rtx, tmode);
7593 /* If we know that no extraneous bits are set, and that the high
7594 bit is not set, convert the extraction to the cheaper of
7595 sign and zero extension, that are equivalent in these cases. */
7596 if (flag_expensive_optimizations
7597 && (HWI_COMPUTABLE_MODE_P (tmode)
7598 && ((nonzero_bits (new_rtx, tmode)
7599 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7600 == 0)))
7602 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7603 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7605 /* Prefer ZERO_EXTENSION, since it gives more information to
7606 backends. */
7607 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7608 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7609 return temp;
7610 return temp1;
7613 /* Otherwise, sign- or zero-extend unless we already are in the
7614 proper mode. */
7616 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7617 mode, new_rtx));
7620 /* Unless this is a COMPARE or we have a funny memory reference,
7621 don't do anything with zero-extending field extracts starting at
7622 the low-order bit since they are simple AND operations. */
7623 if (pos_rtx == 0 && pos == 0 && ! in_dest
7624 && ! in_compare && unsignedp)
7625 return 0;
7627 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7628 if the position is not a constant and the length is not 1. In all
7629 other cases, we would only be going outside our object in cases when
7630 an original shift would have been undefined. */
7631 if (MEM_P (inner)
7632 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7633 || (pos_rtx != 0 && len != 1)))
7634 return 0;
7636 enum extraction_pattern pattern = (in_dest ? EP_insv
7637 : unsignedp ? EP_extzv : EP_extv);
7639 /* If INNER is not from memory, we want it to have the mode of a register
7640 extraction pattern's structure operand, or word_mode if there is no
7641 such pattern. The same applies to extraction_mode and pos_mode
7642 and their respective operands.
7644 For memory, assume that the desired extraction_mode and pos_mode
7645 are the same as for a register operation, since at present we don't
7646 have named patterns for aligned memory structures. */
7647 struct extraction_insn insn;
7648 if (get_best_reg_extraction_insn (&insn, pattern,
7649 GET_MODE_BITSIZE (inner_mode), mode))
7651 wanted_inner_reg_mode = insn.struct_mode.require ();
7652 pos_mode = insn.pos_mode;
7653 extraction_mode = insn.field_mode;
7656 /* Never narrow an object, since that might not be safe. */
7658 if (mode != VOIDmode
7659 && partial_subreg_p (extraction_mode, mode))
7660 extraction_mode = mode;
7662 if (!MEM_P (inner))
7663 wanted_inner_mode = wanted_inner_reg_mode;
7664 else
7666 /* Be careful not to go beyond the extracted object and maintain the
7667 natural alignment of the memory. */
7668 wanted_inner_mode = smallest_int_mode_for_size (len);
7669 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7670 > GET_MODE_BITSIZE (wanted_inner_mode))
7671 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7674 orig_pos = pos;
7676 if (BITS_BIG_ENDIAN)
7678 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7679 BITS_BIG_ENDIAN style. If position is constant, compute new
7680 position. Otherwise, build subtraction.
7681 Note that POS is relative to the mode of the original argument.
7682 If it's a MEM we need to recompute POS relative to that.
7683 However, if we're extracting from (or inserting into) a register,
7684 we want to recompute POS relative to wanted_inner_mode. */
7685 int width = (MEM_P (inner)
7686 ? GET_MODE_BITSIZE (is_mode)
7687 : GET_MODE_BITSIZE (wanted_inner_mode));
7689 if (pos_rtx == 0)
7690 pos = width - len - pos;
7691 else
7692 pos_rtx
7693 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7694 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7695 pos_rtx);
7696 /* POS may be less than 0 now, but we check for that below.
7697 Note that it can only be less than 0 if !MEM_P (inner). */
7700 /* If INNER has a wider mode, and this is a constant extraction, try to
7701 make it smaller and adjust the byte to point to the byte containing
7702 the value. */
7703 if (wanted_inner_mode != VOIDmode
7704 && inner_mode != wanted_inner_mode
7705 && ! pos_rtx
7706 && partial_subreg_p (wanted_inner_mode, is_mode)
7707 && MEM_P (inner)
7708 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7709 && ! MEM_VOLATILE_P (inner))
7711 int offset = 0;
7713 /* The computations below will be correct if the machine is big
7714 endian in both bits and bytes or little endian in bits and bytes.
7715 If it is mixed, we must adjust. */
7717 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7718 adjust OFFSET to compensate. */
7719 if (BYTES_BIG_ENDIAN
7720 && paradoxical_subreg_p (is_mode, inner_mode))
7721 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7723 /* We can now move to the desired byte. */
7724 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7725 * GET_MODE_SIZE (wanted_inner_mode);
7726 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7728 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7729 && is_mode != wanted_inner_mode)
7730 offset = (GET_MODE_SIZE (is_mode)
7731 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7733 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7736 /* If INNER is not memory, get it into the proper mode. If we are changing
7737 its mode, POS must be a constant and smaller than the size of the new
7738 mode. */
7739 else if (!MEM_P (inner))
7741 /* On the LHS, don't create paradoxical subregs implicitely truncating
7742 the register unless TARGET_TRULY_NOOP_TRUNCATION. */
7743 if (in_dest
7744 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7745 wanted_inner_mode))
7746 return NULL_RTX;
7748 if (GET_MODE (inner) != wanted_inner_mode
7749 && (pos_rtx != 0
7750 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7751 return NULL_RTX;
7753 if (orig_pos < 0)
7754 return NULL_RTX;
7756 inner = force_to_mode (inner, wanted_inner_mode,
7757 pos_rtx
7758 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7759 ? HOST_WIDE_INT_M1U
7760 : (((HOST_WIDE_INT_1U << len) - 1)
7761 << orig_pos),
7765 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7766 have to zero extend. Otherwise, we can just use a SUBREG.
7768 We dealt with constant rtxes earlier, so pos_rtx cannot
7769 have VOIDmode at this point. */
7770 if (pos_rtx != 0
7771 && (GET_MODE_SIZE (pos_mode)
7772 > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7774 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7775 GET_MODE (pos_rtx));
7777 /* If we know that no extraneous bits are set, and that the high
7778 bit is not set, convert extraction to cheaper one - either
7779 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7780 cases. */
7781 if (flag_expensive_optimizations
7782 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7783 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7784 & ~(((unsigned HOST_WIDE_INT)
7785 GET_MODE_MASK (GET_MODE (pos_rtx)))
7786 >> 1))
7787 == 0)))
7789 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7790 GET_MODE (pos_rtx));
7792 /* Prefer ZERO_EXTENSION, since it gives more information to
7793 backends. */
7794 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7795 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7796 temp = temp1;
7798 pos_rtx = temp;
7801 /* Make POS_RTX unless we already have it and it is correct. If we don't
7802 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7803 be a CONST_INT. */
7804 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7805 pos_rtx = orig_pos_rtx;
7807 else if (pos_rtx == 0)
7808 pos_rtx = GEN_INT (pos);
7810 /* Make the required operation. See if we can use existing rtx. */
7811 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7812 extraction_mode, inner, GEN_INT (len), pos_rtx);
7813 if (! in_dest)
7814 new_rtx = gen_lowpart (mode, new_rtx);
7816 return new_rtx;
7819 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7820 can be commuted with any other operations in X. Return X without
7821 that shift if so. */
7823 static rtx
7824 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7826 enum rtx_code code = GET_CODE (x);
7827 rtx tem;
7829 switch (code)
7831 case ASHIFT:
7832 /* This is the shift itself. If it is wide enough, we will return
7833 either the value being shifted if the shift count is equal to
7834 COUNT or a shift for the difference. */
7835 if (CONST_INT_P (XEXP (x, 1))
7836 && INTVAL (XEXP (x, 1)) >= count)
7837 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7838 INTVAL (XEXP (x, 1)) - count);
7839 break;
7841 case NEG: case NOT:
7842 if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7843 return simplify_gen_unary (code, mode, tem, mode);
7845 break;
7847 case PLUS: case IOR: case XOR: case AND:
7848 /* If we can safely shift this constant and we find the inner shift,
7849 make a new operation. */
7850 if (CONST_INT_P (XEXP (x, 1))
7851 && (UINTVAL (XEXP (x, 1))
7852 & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
7853 && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7855 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7856 return simplify_gen_binary (code, mode, tem,
7857 gen_int_mode (val, mode));
7859 break;
7861 default:
7862 break;
7865 return 0;
7868 /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
7869 level of the expression and MODE is its mode. IN_CODE is as for
7870 make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
7871 that should be used when recursing on operands of *X_PTR.
7873 There are two possible actions:
7875 - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
7876 equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
7878 - Return a new rtx, which the caller returns directly. */
7880 static rtx
7881 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
7882 enum rtx_code in_code,
7883 enum rtx_code *next_code_ptr)
7885 rtx x = *x_ptr;
7886 enum rtx_code next_code = *next_code_ptr;
7887 enum rtx_code code = GET_CODE (x);
7888 int mode_width = GET_MODE_PRECISION (mode);
7889 rtx rhs, lhs;
7890 rtx new_rtx = 0;
7891 int i;
7892 rtx tem;
7893 scalar_int_mode inner_mode;
7894 bool equality_comparison = false;
7896 if (in_code == EQ)
7898 equality_comparison = true;
7899 in_code = COMPARE;
7902 /* Process depending on the code of this operation. If NEW is set
7903 nonzero, it will be returned. */
7905 switch (code)
7907 case ASHIFT:
7908 /* Convert shifts by constants into multiplications if inside
7909 an address. */
7910 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7911 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7912 && INTVAL (XEXP (x, 1)) >= 0)
7914 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7915 HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
7917 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7918 if (GET_CODE (new_rtx) == NEG)
7920 new_rtx = XEXP (new_rtx, 0);
7921 multval = -multval;
7923 multval = trunc_int_for_mode (multval, mode);
7924 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7926 break;
7928 case PLUS:
7929 lhs = XEXP (x, 0);
7930 rhs = XEXP (x, 1);
7931 lhs = make_compound_operation (lhs, next_code);
7932 rhs = make_compound_operation (rhs, next_code);
7933 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
7935 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7936 XEXP (lhs, 1));
7937 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7939 else if (GET_CODE (lhs) == MULT
7940 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7942 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7943 simplify_gen_unary (NEG, mode,
7944 XEXP (lhs, 1),
7945 mode));
7946 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7948 else
7950 SUBST (XEXP (x, 0), lhs);
7951 SUBST (XEXP (x, 1), rhs);
7953 maybe_swap_commutative_operands (x);
7954 return x;
7956 case MINUS:
7957 lhs = XEXP (x, 0);
7958 rhs = XEXP (x, 1);
7959 lhs = make_compound_operation (lhs, next_code);
7960 rhs = make_compound_operation (rhs, next_code);
7961 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
7963 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7964 XEXP (rhs, 1));
7965 return simplify_gen_binary (PLUS, mode, tem, lhs);
7967 else if (GET_CODE (rhs) == MULT
7968 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7970 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7971 simplify_gen_unary (NEG, mode,
7972 XEXP (rhs, 1),
7973 mode));
7974 return simplify_gen_binary (PLUS, mode, tem, lhs);
7976 else
7978 SUBST (XEXP (x, 0), lhs);
7979 SUBST (XEXP (x, 1), rhs);
7980 return x;
7983 case AND:
7984 /* If the second operand is not a constant, we can't do anything
7985 with it. */
7986 if (!CONST_INT_P (XEXP (x, 1)))
7987 break;
7989 /* If the constant is a power of two minus one and the first operand
7990 is a logical right shift, make an extraction. */
7991 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7992 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7994 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7995 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
7996 i, 1, 0, in_code == COMPARE);
7999 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
8000 else if (GET_CODE (XEXP (x, 0)) == SUBREG
8001 && subreg_lowpart_p (XEXP (x, 0))
8002 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8003 &inner_mode)
8004 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8005 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8007 rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8008 new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8009 new_rtx = make_extraction (inner_mode, new_rtx, 0,
8010 XEXP (inner_x0, 1),
8011 i, 1, 0, in_code == COMPARE);
8013 /* If we narrowed the mode when dropping the subreg, then we lose. */
8014 if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8015 new_rtx = NULL;
8017 /* If that didn't give anything, see if the AND simplifies on
8018 its own. */
8019 if (!new_rtx && i >= 0)
8021 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8022 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
8023 0, in_code == COMPARE);
8026 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8027 else if ((GET_CODE (XEXP (x, 0)) == XOR
8028 || GET_CODE (XEXP (x, 0)) == IOR)
8029 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8030 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8031 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8033 /* Apply the distributive law, and then try to make extractions. */
8034 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8035 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8036 XEXP (x, 1)),
8037 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8038 XEXP (x, 1)));
8039 new_rtx = make_compound_operation (new_rtx, in_code);
8042 /* If we are have (and (rotate X C) M) and C is larger than the number
8043 of bits in M, this is an extraction. */
8045 else if (GET_CODE (XEXP (x, 0)) == ROTATE
8046 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8047 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8048 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8050 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8051 new_rtx = make_extraction (mode, new_rtx,
8052 (GET_MODE_PRECISION (mode)
8053 - INTVAL (XEXP (XEXP (x, 0), 1))),
8054 NULL_RTX, i, 1, 0, in_code == COMPARE);
8057 /* On machines without logical shifts, if the operand of the AND is
8058 a logical shift and our mask turns off all the propagated sign
8059 bits, we can replace the logical shift with an arithmetic shift. */
8060 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8061 && !have_insn_for (LSHIFTRT, mode)
8062 && have_insn_for (ASHIFTRT, mode)
8063 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8064 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8065 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8066 && mode_width <= HOST_BITS_PER_WIDE_INT)
8068 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8070 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8071 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8072 SUBST (XEXP (x, 0),
8073 gen_rtx_ASHIFTRT (mode,
8074 make_compound_operation (XEXP (XEXP (x,
8077 next_code),
8078 XEXP (XEXP (x, 0), 1)));
8081 /* If the constant is one less than a power of two, this might be
8082 representable by an extraction even if no shift is present.
8083 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8084 we are in a COMPARE. */
8085 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8086 new_rtx = make_extraction (mode,
8087 make_compound_operation (XEXP (x, 0),
8088 next_code),
8089 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
8091 /* If we are in a comparison and this is an AND with a power of two,
8092 convert this into the appropriate bit extract. */
8093 else if (in_code == COMPARE
8094 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8095 && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8096 new_rtx = make_extraction (mode,
8097 make_compound_operation (XEXP (x, 0),
8098 next_code),
8099 i, NULL_RTX, 1, 1, 0, 1);
8101 /* If the one operand is a paradoxical subreg of a register or memory and
8102 the constant (limited to the smaller mode) has only zero bits where
8103 the sub expression has known zero bits, this can be expressed as
8104 a zero_extend. */
8105 else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8107 rtx sub;
8109 sub = XEXP (XEXP (x, 0), 0);
8110 machine_mode sub_mode = GET_MODE (sub);
8111 if ((REG_P (sub) || MEM_P (sub))
8112 && GET_MODE_PRECISION (sub_mode) < mode_width)
8114 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8115 unsigned HOST_WIDE_INT mask;
8117 /* original AND constant with all the known zero bits set */
8118 mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8119 if ((mask & mode_mask) == mode_mask)
8121 new_rtx = make_compound_operation (sub, next_code);
8122 new_rtx = make_extraction (mode, new_rtx, 0, 0,
8123 GET_MODE_PRECISION (sub_mode),
8124 1, 0, in_code == COMPARE);
8129 break;
8131 case LSHIFTRT:
8132 /* If the sign bit is known to be zero, replace this with an
8133 arithmetic shift. */
8134 if (have_insn_for (ASHIFTRT, mode)
8135 && ! have_insn_for (LSHIFTRT, mode)
8136 && mode_width <= HOST_BITS_PER_WIDE_INT
8137 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8139 new_rtx = gen_rtx_ASHIFTRT (mode,
8140 make_compound_operation (XEXP (x, 0),
8141 next_code),
8142 XEXP (x, 1));
8143 break;
8146 /* fall through */
8148 case ASHIFTRT:
8149 lhs = XEXP (x, 0);
8150 rhs = XEXP (x, 1);
8152 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8153 this is a SIGN_EXTRACT. */
8154 if (CONST_INT_P (rhs)
8155 && GET_CODE (lhs) == ASHIFT
8156 && CONST_INT_P (XEXP (lhs, 1))
8157 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8158 && INTVAL (XEXP (lhs, 1)) >= 0
8159 && INTVAL (rhs) < mode_width)
8161 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8162 new_rtx = make_extraction (mode, new_rtx,
8163 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8164 NULL_RTX, mode_width - INTVAL (rhs),
8165 code == LSHIFTRT, 0, in_code == COMPARE);
8166 break;
8169 /* See if we have operations between an ASHIFTRT and an ASHIFT.
8170 If so, try to merge the shifts into a SIGN_EXTEND. We could
8171 also do this for some cases of SIGN_EXTRACT, but it doesn't
8172 seem worth the effort; the case checked for occurs on Alpha. */
8174 if (!OBJECT_P (lhs)
8175 && ! (GET_CODE (lhs) == SUBREG
8176 && (OBJECT_P (SUBREG_REG (lhs))))
8177 && CONST_INT_P (rhs)
8178 && INTVAL (rhs) >= 0
8179 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8180 && INTVAL (rhs) < mode_width
8181 && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8182 new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8183 next_code),
8184 0, NULL_RTX, mode_width - INTVAL (rhs),
8185 code == LSHIFTRT, 0, in_code == COMPARE);
8187 break;
8189 case SUBREG:
8190 /* Call ourselves recursively on the inner expression. If we are
8191 narrowing the object and it has a different RTL code from
8192 what it originally did, do this SUBREG as a force_to_mode. */
8194 rtx inner = SUBREG_REG (x), simplified;
8195 enum rtx_code subreg_code = in_code;
8197 /* If the SUBREG is masking of a logical right shift,
8198 make an extraction. */
8199 if (GET_CODE (inner) == LSHIFTRT
8200 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8201 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8202 && CONST_INT_P (XEXP (inner, 1))
8203 && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8204 && subreg_lowpart_p (x))
8206 new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8207 int width = GET_MODE_PRECISION (inner_mode)
8208 - INTVAL (XEXP (inner, 1));
8209 if (width > mode_width)
8210 width = mode_width;
8211 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8212 width, 1, 0, in_code == COMPARE);
8213 break;
8216 /* If in_code is COMPARE, it isn't always safe to pass it through
8217 to the recursive make_compound_operation call. */
8218 if (subreg_code == COMPARE
8219 && (!subreg_lowpart_p (x)
8220 || GET_CODE (inner) == SUBREG
8221 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8222 is (const_int 0), rather than
8223 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8224 Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8225 for non-equality comparisons against 0 is not equivalent
8226 to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8227 || (GET_CODE (inner) == AND
8228 && CONST_INT_P (XEXP (inner, 1))
8229 && partial_subreg_p (x)
8230 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8231 >= GET_MODE_BITSIZE (mode) - 1)))
8232 subreg_code = SET;
8234 tem = make_compound_operation (inner, subreg_code);
8236 simplified
8237 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8238 if (simplified)
8239 tem = simplified;
8241 if (GET_CODE (tem) != GET_CODE (inner)
8242 && partial_subreg_p (x)
8243 && subreg_lowpart_p (x))
8245 rtx newer
8246 = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, 0);
8248 /* If we have something other than a SUBREG, we might have
8249 done an expansion, so rerun ourselves. */
8250 if (GET_CODE (newer) != SUBREG)
8251 newer = make_compound_operation (newer, in_code);
8253 /* force_to_mode can expand compounds. If it just re-expanded
8254 the compound, use gen_lowpart to convert to the desired
8255 mode. */
8256 if (rtx_equal_p (newer, x)
8257 /* Likewise if it re-expanded the compound only partially.
8258 This happens for SUBREG of ZERO_EXTRACT if they extract
8259 the same number of bits. */
8260 || (GET_CODE (newer) == SUBREG
8261 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8262 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8263 && GET_CODE (inner) == AND
8264 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8265 return gen_lowpart (GET_MODE (x), tem);
8267 return newer;
8270 if (simplified)
8271 return tem;
8273 break;
8275 default:
8276 break;
8279 if (new_rtx)
8280 *x_ptr = gen_lowpart (mode, new_rtx);
8281 *next_code_ptr = next_code;
8282 return NULL_RTX;
8285 /* Look at the expression rooted at X. Look for expressions
8286 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8287 Form these expressions.
8289 Return the new rtx, usually just X.
8291 Also, for machines like the VAX that don't have logical shift insns,
8292 try to convert logical to arithmetic shift operations in cases where
8293 they are equivalent. This undoes the canonicalizations to logical
8294 shifts done elsewhere.
8296 We try, as much as possible, to re-use rtl expressions to save memory.
8298 IN_CODE says what kind of expression we are processing. Normally, it is
8299 SET. In a memory address it is MEM. When processing the arguments of
8300 a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8301 precisely it is an equality comparison against zero. */
8304 make_compound_operation (rtx x, enum rtx_code in_code)
8306 enum rtx_code code = GET_CODE (x);
8307 const char *fmt;
8308 int i, j;
8309 enum rtx_code next_code;
8310 rtx new_rtx, tem;
8312 /* Select the code to be used in recursive calls. Once we are inside an
8313 address, we stay there. If we have a comparison, set to COMPARE,
8314 but once inside, go back to our default of SET. */
8316 next_code = (code == MEM ? MEM
8317 : ((code == COMPARE || COMPARISON_P (x))
8318 && XEXP (x, 1) == const0_rtx) ? COMPARE
8319 : in_code == COMPARE || in_code == EQ ? SET : in_code);
8321 scalar_int_mode mode;
8322 if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8324 rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8325 &next_code);
8326 if (new_rtx)
8327 return new_rtx;
8328 code = GET_CODE (x);
8331 /* Now recursively process each operand of this operation. We need to
8332 handle ZERO_EXTEND specially so that we don't lose track of the
8333 inner mode. */
8334 if (code == ZERO_EXTEND)
8336 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8337 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8338 new_rtx, GET_MODE (XEXP (x, 0)));
8339 if (tem)
8340 return tem;
8341 SUBST (XEXP (x, 0), new_rtx);
8342 return x;
8345 fmt = GET_RTX_FORMAT (code);
8346 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8347 if (fmt[i] == 'e')
8349 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8350 SUBST (XEXP (x, i), new_rtx);
8352 else if (fmt[i] == 'E')
8353 for (j = 0; j < XVECLEN (x, i); j++)
8355 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8356 SUBST (XVECEXP (x, i, j), new_rtx);
8359 maybe_swap_commutative_operands (x);
8360 return x;
8363 /* Given M see if it is a value that would select a field of bits
8364 within an item, but not the entire word. Return -1 if not.
8365 Otherwise, return the starting position of the field, where 0 is the
8366 low-order bit.
8368 *PLEN is set to the length of the field. */
8370 static int
8371 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8373 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8374 int pos = m ? ctz_hwi (m) : -1;
8375 int len = 0;
8377 if (pos >= 0)
8378 /* Now shift off the low-order zero bits and see if we have a
8379 power of two minus 1. */
8380 len = exact_log2 ((m >> pos) + 1);
8382 if (len <= 0)
8383 pos = -1;
8385 *plen = len;
8386 return pos;
8389 /* If X refers to a register that equals REG in value, replace these
8390 references with REG. */
8391 static rtx
8392 canon_reg_for_combine (rtx x, rtx reg)
8394 rtx op0, op1, op2;
8395 const char *fmt;
8396 int i;
8397 bool copied;
8399 enum rtx_code code = GET_CODE (x);
8400 switch (GET_RTX_CLASS (code))
8402 case RTX_UNARY:
8403 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8404 if (op0 != XEXP (x, 0))
8405 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8406 GET_MODE (reg));
8407 break;
8409 case RTX_BIN_ARITH:
8410 case RTX_COMM_ARITH:
8411 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8412 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8413 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8414 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8415 break;
8417 case RTX_COMPARE:
8418 case RTX_COMM_COMPARE:
8419 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8420 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8421 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8422 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8423 GET_MODE (op0), op0, op1);
8424 break;
8426 case RTX_TERNARY:
8427 case RTX_BITFIELD_OPS:
8428 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8429 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8430 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8431 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8432 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8433 GET_MODE (op0), op0, op1, op2);
8434 /* FALLTHRU */
8436 case RTX_OBJ:
8437 if (REG_P (x))
8439 if (rtx_equal_p (get_last_value (reg), x)
8440 || rtx_equal_p (reg, get_last_value (x)))
8441 return reg;
8442 else
8443 break;
8446 /* fall through */
8448 default:
8449 fmt = GET_RTX_FORMAT (code);
8450 copied = false;
8451 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8452 if (fmt[i] == 'e')
8454 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8455 if (op != XEXP (x, i))
8457 if (!copied)
8459 copied = true;
8460 x = copy_rtx (x);
8462 XEXP (x, i) = op;
8465 else if (fmt[i] == 'E')
8467 int j;
8468 for (j = 0; j < XVECLEN (x, i); j++)
8470 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8471 if (op != XVECEXP (x, i, j))
8473 if (!copied)
8475 copied = true;
8476 x = copy_rtx (x);
8478 XVECEXP (x, i, j) = op;
8483 break;
8486 return x;
8489 /* Return X converted to MODE. If the value is already truncated to
8490 MODE we can just return a subreg even though in the general case we
8491 would need an explicit truncation. */
8493 static rtx
8494 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8496 if (!CONST_INT_P (x)
8497 && partial_subreg_p (mode, GET_MODE (x))
8498 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8499 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8501 /* Bit-cast X into an integer mode. */
8502 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8503 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8504 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8505 x, GET_MODE (x));
8508 return gen_lowpart (mode, x);
8511 /* See if X can be simplified knowing that we will only refer to it in
8512 MODE and will only refer to those bits that are nonzero in MASK.
8513 If other bits are being computed or if masking operations are done
8514 that select a superset of the bits in MASK, they can sometimes be
8515 ignored.
8517 Return a possibly simplified expression, but always convert X to
8518 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8520 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8521 are all off in X. This is used when X will be complemented, by either
8522 NOT, NEG, or XOR. */
8524 static rtx
8525 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8526 int just_select)
8528 enum rtx_code code = GET_CODE (x);
8529 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8530 machine_mode op_mode;
8531 unsigned HOST_WIDE_INT nonzero;
8533 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8534 code below will do the wrong thing since the mode of such an
8535 expression is VOIDmode.
8537 Also do nothing if X is a CLOBBER; this can happen if X was
8538 the return value from a call to gen_lowpart. */
8539 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8540 return x;
8542 /* We want to perform the operation in its present mode unless we know
8543 that the operation is valid in MODE, in which case we do the operation
8544 in MODE. */
8545 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8546 && have_insn_for (code, mode))
8547 ? mode : GET_MODE (x));
8549 /* It is not valid to do a right-shift in a narrower mode
8550 than the one it came in with. */
8551 if ((code == LSHIFTRT || code == ASHIFTRT)
8552 && partial_subreg_p (mode, GET_MODE (x)))
8553 op_mode = GET_MODE (x);
8555 /* Truncate MASK to fit OP_MODE. */
8556 if (op_mode)
8557 mask &= GET_MODE_MASK (op_mode);
8559 /* Determine what bits of X are guaranteed to be (non)zero. */
8560 nonzero = nonzero_bits (x, mode);
8562 /* If none of the bits in X are needed, return a zero. */
8563 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8564 x = const0_rtx;
8566 /* If X is a CONST_INT, return a new one. Do this here since the
8567 test below will fail. */
8568 if (CONST_INT_P (x))
8570 if (SCALAR_INT_MODE_P (mode))
8571 return gen_int_mode (INTVAL (x) & mask, mode);
8572 else
8574 x = GEN_INT (INTVAL (x) & mask);
8575 return gen_lowpart_common (mode, x);
8579 /* If X is narrower than MODE and we want all the bits in X's mode, just
8580 get X in the proper mode. */
8581 if (paradoxical_subreg_p (mode, GET_MODE (x))
8582 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8583 return gen_lowpart (mode, x);
8585 /* We can ignore the effect of a SUBREG if it narrows the mode or
8586 if the constant masks to zero all the bits the mode doesn't have. */
8587 if (GET_CODE (x) == SUBREG
8588 && subreg_lowpart_p (x)
8589 && (partial_subreg_p (x)
8590 || (0 == (mask
8591 & GET_MODE_MASK (GET_MODE (x))
8592 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8593 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8595 scalar_int_mode int_mode, xmode;
8596 if (is_a <scalar_int_mode> (mode, &int_mode)
8597 && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8598 /* OP_MODE is either MODE or XMODE, so it must be a scalar
8599 integer too. */
8600 return force_int_to_mode (x, int_mode, xmode,
8601 as_a <scalar_int_mode> (op_mode),
8602 mask, just_select);
8604 return gen_lowpart_or_truncate (mode, x);
8607 /* Subroutine of force_to_mode that handles cases in which both X and
8608 the result are scalar integers. MODE is the mode of the result,
8609 XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8610 is preferred for simplified versions of X. The other arguments
8611 are as for force_to_mode. */
8613 static rtx
8614 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8615 scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8616 int just_select)
8618 enum rtx_code code = GET_CODE (x);
8619 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8620 unsigned HOST_WIDE_INT fuller_mask;
8621 rtx op0, op1, temp;
8623 /* When we have an arithmetic operation, or a shift whose count we
8624 do not know, we need to assume that all bits up to the highest-order
8625 bit in MASK will be needed. This is how we form such a mask. */
8626 if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8627 fuller_mask = HOST_WIDE_INT_M1U;
8628 else
8629 fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1))
8630 - 1);
8632 switch (code)
8634 case CLOBBER:
8635 /* If X is a (clobber (const_int)), return it since we know we are
8636 generating something that won't match. */
8637 return x;
8639 case SIGN_EXTEND:
8640 case ZERO_EXTEND:
8641 case ZERO_EXTRACT:
8642 case SIGN_EXTRACT:
8643 x = expand_compound_operation (x);
8644 if (GET_CODE (x) != code)
8645 return force_to_mode (x, mode, mask, next_select);
8646 break;
8648 case TRUNCATE:
8649 /* Similarly for a truncate. */
8650 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8652 case AND:
8653 /* If this is an AND with a constant, convert it into an AND
8654 whose constant is the AND of that constant with MASK. If it
8655 remains an AND of MASK, delete it since it is redundant. */
8657 if (CONST_INT_P (XEXP (x, 1)))
8659 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8660 mask & INTVAL (XEXP (x, 1)));
8661 xmode = op_mode;
8663 /* If X is still an AND, see if it is an AND with a mask that
8664 is just some low-order bits. If so, and it is MASK, we don't
8665 need it. */
8667 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8668 && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8669 x = XEXP (x, 0);
8671 /* If it remains an AND, try making another AND with the bits
8672 in the mode mask that aren't in MASK turned on. If the
8673 constant in the AND is wide enough, this might make a
8674 cheaper constant. */
8676 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8677 && GET_MODE_MASK (xmode) != mask
8678 && HWI_COMPUTABLE_MODE_P (xmode))
8680 unsigned HOST_WIDE_INT cval
8681 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8682 rtx y;
8684 y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8685 gen_int_mode (cval, xmode));
8686 if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8687 < set_src_cost (x, xmode, optimize_this_for_speed_p))
8688 x = y;
8691 break;
8694 goto binop;
8696 case PLUS:
8697 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8698 low-order bits (as in an alignment operation) and FOO is already
8699 aligned to that boundary, mask C1 to that boundary as well.
8700 This may eliminate that PLUS and, later, the AND. */
8703 unsigned int width = GET_MODE_PRECISION (mode);
8704 unsigned HOST_WIDE_INT smask = mask;
8706 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8707 number, sign extend it. */
8709 if (width < HOST_BITS_PER_WIDE_INT
8710 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8711 smask |= HOST_WIDE_INT_M1U << width;
8713 if (CONST_INT_P (XEXP (x, 1))
8714 && pow2p_hwi (- smask)
8715 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8716 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8717 return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8718 (INTVAL (XEXP (x, 1)) & smask)),
8719 mode, smask, next_select);
8722 /* fall through */
8724 case MULT:
8725 /* Substituting into the operands of a widening MULT is not likely to
8726 create RTL matching a machine insn. */
8727 if (code == MULT
8728 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8729 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8730 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8731 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8732 && REG_P (XEXP (XEXP (x, 0), 0))
8733 && REG_P (XEXP (XEXP (x, 1), 0)))
8734 return gen_lowpart_or_truncate (mode, x);
8736 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8737 most significant bit in MASK since carries from those bits will
8738 affect the bits we are interested in. */
8739 mask = fuller_mask;
8740 goto binop;
8742 case MINUS:
8743 /* If X is (minus C Y) where C's least set bit is larger than any bit
8744 in the mask, then we may replace with (neg Y). */
8745 if (CONST_INT_P (XEXP (x, 0))
8746 && least_bit_hwi (UINTVAL (XEXP (x, 0))) > mask)
8748 x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8749 return force_to_mode (x, mode, mask, next_select);
8752 /* Similarly, if C contains every bit in the fuller_mask, then we may
8753 replace with (not Y). */
8754 if (CONST_INT_P (XEXP (x, 0))
8755 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8757 x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8758 return force_to_mode (x, mode, mask, next_select);
8761 mask = fuller_mask;
8762 goto binop;
8764 case IOR:
8765 case XOR:
8766 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8767 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8768 operation which may be a bitfield extraction. Ensure that the
8769 constant we form is not wider than the mode of X. */
8771 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8772 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8773 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8774 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8775 && CONST_INT_P (XEXP (x, 1))
8776 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8777 + floor_log2 (INTVAL (XEXP (x, 1))))
8778 < GET_MODE_PRECISION (xmode))
8779 && (UINTVAL (XEXP (x, 1))
8780 & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8782 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8783 << INTVAL (XEXP (XEXP (x, 0), 1)),
8784 xmode);
8785 temp = simplify_gen_binary (GET_CODE (x), xmode,
8786 XEXP (XEXP (x, 0), 0), temp);
8787 x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8788 XEXP (XEXP (x, 0), 1));
8789 return force_to_mode (x, mode, mask, next_select);
8792 binop:
8793 /* For most binary operations, just propagate into the operation and
8794 change the mode if we have an operation of that mode. */
8796 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8797 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8799 /* If we ended up truncating both operands, truncate the result of the
8800 operation instead. */
8801 if (GET_CODE (op0) == TRUNCATE
8802 && GET_CODE (op1) == TRUNCATE)
8804 op0 = XEXP (op0, 0);
8805 op1 = XEXP (op1, 0);
8808 op0 = gen_lowpart_or_truncate (op_mode, op0);
8809 op1 = gen_lowpart_or_truncate (op_mode, op1);
8811 if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8813 x = simplify_gen_binary (code, op_mode, op0, op1);
8814 xmode = op_mode;
8816 break;
8818 case ASHIFT:
8819 /* For left shifts, do the same, but just for the first operand.
8820 However, we cannot do anything with shifts where we cannot
8821 guarantee that the counts are smaller than the size of the mode
8822 because such a count will have a different meaning in a
8823 wider mode. */
8825 if (! (CONST_INT_P (XEXP (x, 1))
8826 && INTVAL (XEXP (x, 1)) >= 0
8827 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8828 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8829 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8830 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8831 break;
8833 /* If the shift count is a constant and we can do arithmetic in
8834 the mode of the shift, refine which bits we need. Otherwise, use the
8835 conservative form of the mask. */
8836 if (CONST_INT_P (XEXP (x, 1))
8837 && INTVAL (XEXP (x, 1)) >= 0
8838 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8839 && HWI_COMPUTABLE_MODE_P (op_mode))
8840 mask >>= INTVAL (XEXP (x, 1));
8841 else
8842 mask = fuller_mask;
8844 op0 = gen_lowpart_or_truncate (op_mode,
8845 force_to_mode (XEXP (x, 0), op_mode,
8846 mask, next_select));
8848 if (op_mode != xmode || op0 != XEXP (x, 0))
8850 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8851 xmode = op_mode;
8853 break;
8855 case LSHIFTRT:
8856 /* Here we can only do something if the shift count is a constant,
8857 this shift constant is valid for the host, and we can do arithmetic
8858 in OP_MODE. */
8860 if (CONST_INT_P (XEXP (x, 1))
8861 && INTVAL (XEXP (x, 1)) >= 0
8862 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8863 && HWI_COMPUTABLE_MODE_P (op_mode))
8865 rtx inner = XEXP (x, 0);
8866 unsigned HOST_WIDE_INT inner_mask;
8868 /* Select the mask of the bits we need for the shift operand. */
8869 inner_mask = mask << INTVAL (XEXP (x, 1));
8871 /* We can only change the mode of the shift if we can do arithmetic
8872 in the mode of the shift and INNER_MASK is no wider than the
8873 width of X's mode. */
8874 if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
8875 op_mode = xmode;
8877 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8879 if (xmode != op_mode || inner != XEXP (x, 0))
8881 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8882 xmode = op_mode;
8886 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8887 shift and AND produces only copies of the sign bit (C2 is one less
8888 than a power of two), we can do this with just a shift. */
8890 if (GET_CODE (x) == LSHIFTRT
8891 && CONST_INT_P (XEXP (x, 1))
8892 /* The shift puts one of the sign bit copies in the least significant
8893 bit. */
8894 && ((INTVAL (XEXP (x, 1))
8895 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8896 >= GET_MODE_PRECISION (xmode))
8897 && pow2p_hwi (mask + 1)
8898 /* Number of bits left after the shift must be more than the mask
8899 needs. */
8900 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8901 <= GET_MODE_PRECISION (xmode))
8902 /* Must be more sign bit copies than the mask needs. */
8903 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8904 >= exact_log2 (mask + 1)))
8905 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
8906 GEN_INT (GET_MODE_PRECISION (xmode)
8907 - exact_log2 (mask + 1)));
8909 goto shiftrt;
8911 case ASHIFTRT:
8912 /* If we are just looking for the sign bit, we don't need this shift at
8913 all, even if it has a variable count. */
8914 if (val_signbit_p (xmode, mask))
8915 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8917 /* If this is a shift by a constant, get a mask that contains those bits
8918 that are not copies of the sign bit. We then have two cases: If
8919 MASK only includes those bits, this can be a logical shift, which may
8920 allow simplifications. If MASK is a single-bit field not within
8921 those bits, we are requesting a copy of the sign bit and hence can
8922 shift the sign bit to the appropriate location. */
8924 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8925 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8927 unsigned HOST_WIDE_INT nonzero;
8928 int i;
8930 /* If the considered data is wider than HOST_WIDE_INT, we can't
8931 represent a mask for all its bits in a single scalar.
8932 But we only care about the lower bits, so calculate these. */
8934 if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
8936 nonzero = HOST_WIDE_INT_M1U;
8938 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8939 is the number of bits a full-width mask would have set.
8940 We need only shift if these are fewer than nonzero can
8941 hold. If not, we must keep all bits set in nonzero. */
8943 if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
8944 < HOST_BITS_PER_WIDE_INT)
8945 nonzero >>= INTVAL (XEXP (x, 1))
8946 + HOST_BITS_PER_WIDE_INT
8947 - GET_MODE_PRECISION (xmode);
8949 else
8951 nonzero = GET_MODE_MASK (xmode);
8952 nonzero >>= INTVAL (XEXP (x, 1));
8955 if ((mask & ~nonzero) == 0)
8957 x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
8958 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8959 if (GET_CODE (x) != ASHIFTRT)
8960 return force_to_mode (x, mode, mask, next_select);
8963 else if ((i = exact_log2 (mask)) >= 0)
8965 x = simplify_shift_const
8966 (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
8967 GET_MODE_PRECISION (xmode) - 1 - i);
8969 if (GET_CODE (x) != ASHIFTRT)
8970 return force_to_mode (x, mode, mask, next_select);
8974 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8975 even if the shift count isn't a constant. */
8976 if (mask == 1)
8977 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
8979 shiftrt:
8981 /* If this is a zero- or sign-extension operation that just affects bits
8982 we don't care about, remove it. Be sure the call above returned
8983 something that is still a shift. */
8985 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8986 && CONST_INT_P (XEXP (x, 1))
8987 && INTVAL (XEXP (x, 1)) >= 0
8988 && (INTVAL (XEXP (x, 1))
8989 <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
8990 && GET_CODE (XEXP (x, 0)) == ASHIFT
8991 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8992 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8993 next_select);
8995 break;
8997 case ROTATE:
8998 case ROTATERT:
8999 /* If the shift count is constant and we can do computations
9000 in the mode of X, compute where the bits we care about are.
9001 Otherwise, we can't do anything. Don't change the mode of
9002 the shift or propagate MODE into the shift, though. */
9003 if (CONST_INT_P (XEXP (x, 1))
9004 && INTVAL (XEXP (x, 1)) >= 0)
9006 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9007 xmode, gen_int_mode (mask, xmode),
9008 XEXP (x, 1));
9009 if (temp && CONST_INT_P (temp))
9010 x = simplify_gen_binary (code, xmode,
9011 force_to_mode (XEXP (x, 0), xmode,
9012 INTVAL (temp), next_select),
9013 XEXP (x, 1));
9015 break;
9017 case NEG:
9018 /* If we just want the low-order bit, the NEG isn't needed since it
9019 won't change the low-order bit. */
9020 if (mask == 1)
9021 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9023 /* We need any bits less significant than the most significant bit in
9024 MASK since carries from those bits will affect the bits we are
9025 interested in. */
9026 mask = fuller_mask;
9027 goto unop;
9029 case NOT:
9030 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9031 same as the XOR case above. Ensure that the constant we form is not
9032 wider than the mode of X. */
9034 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9035 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9036 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9037 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9038 < GET_MODE_PRECISION (xmode))
9039 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9041 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9042 temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9043 x = simplify_gen_binary (LSHIFTRT, xmode,
9044 temp, XEXP (XEXP (x, 0), 1));
9046 return force_to_mode (x, mode, mask, next_select);
9049 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9050 use the full mask inside the NOT. */
9051 mask = fuller_mask;
9053 unop:
9054 op0 = gen_lowpart_or_truncate (op_mode,
9055 force_to_mode (XEXP (x, 0), mode, mask,
9056 next_select));
9057 if (op_mode != xmode || op0 != XEXP (x, 0))
9059 x = simplify_gen_unary (code, op_mode, op0, op_mode);
9060 xmode = op_mode;
9062 break;
9064 case NE:
9065 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9066 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9067 which is equal to STORE_FLAG_VALUE. */
9068 if ((mask & ~STORE_FLAG_VALUE) == 0
9069 && XEXP (x, 1) == const0_rtx
9070 && GET_MODE (XEXP (x, 0)) == mode
9071 && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9072 && (nonzero_bits (XEXP (x, 0), mode)
9073 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9074 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9076 break;
9078 case IF_THEN_ELSE:
9079 /* We have no way of knowing if the IF_THEN_ELSE can itself be
9080 written in a narrower mode. We play it safe and do not do so. */
9082 op0 = gen_lowpart_or_truncate (xmode,
9083 force_to_mode (XEXP (x, 1), mode,
9084 mask, next_select));
9085 op1 = gen_lowpart_or_truncate (xmode,
9086 force_to_mode (XEXP (x, 2), mode,
9087 mask, next_select));
9088 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9089 x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9090 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9091 op0, op1);
9092 break;
9094 default:
9095 break;
9098 /* Ensure we return a value of the proper mode. */
9099 return gen_lowpart_or_truncate (mode, x);
9102 /* Return nonzero if X is an expression that has one of two values depending on
9103 whether some other value is zero or nonzero. In that case, we return the
9104 value that is being tested, *PTRUE is set to the value if the rtx being
9105 returned has a nonzero value, and *PFALSE is set to the other alternative.
9107 If we return zero, we set *PTRUE and *PFALSE to X. */
9109 static rtx
9110 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9112 machine_mode mode = GET_MODE (x);
9113 enum rtx_code code = GET_CODE (x);
9114 rtx cond0, cond1, true0, true1, false0, false1;
9115 unsigned HOST_WIDE_INT nz;
9116 scalar_int_mode int_mode;
9118 /* If we are comparing a value against zero, we are done. */
9119 if ((code == NE || code == EQ)
9120 && XEXP (x, 1) == const0_rtx)
9122 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9123 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9124 return XEXP (x, 0);
9127 /* If this is a unary operation whose operand has one of two values, apply
9128 our opcode to compute those values. */
9129 else if (UNARY_P (x)
9130 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9132 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9133 *pfalse = simplify_gen_unary (code, mode, false0,
9134 GET_MODE (XEXP (x, 0)));
9135 return cond0;
9138 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9139 make can't possibly match and would suppress other optimizations. */
9140 else if (code == COMPARE)
9143 /* If this is a binary operation, see if either side has only one of two
9144 values. If either one does or if both do and they are conditional on
9145 the same value, compute the new true and false values. */
9146 else if (BINARY_P (x))
9148 rtx op0 = XEXP (x, 0);
9149 rtx op1 = XEXP (x, 1);
9150 cond0 = if_then_else_cond (op0, &true0, &false0);
9151 cond1 = if_then_else_cond (op1, &true1, &false1);
9153 if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9154 && (REG_P (op0) || REG_P (op1)))
9156 /* Try to enable a simplification by undoing work done by
9157 if_then_else_cond if it converted a REG into something more
9158 complex. */
9159 if (REG_P (op0))
9161 cond0 = 0;
9162 true0 = false0 = op0;
9164 else
9166 cond1 = 0;
9167 true1 = false1 = op1;
9171 if ((cond0 != 0 || cond1 != 0)
9172 && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9174 /* If if_then_else_cond returned zero, then true/false are the
9175 same rtl. We must copy one of them to prevent invalid rtl
9176 sharing. */
9177 if (cond0 == 0)
9178 true0 = copy_rtx (true0);
9179 else if (cond1 == 0)
9180 true1 = copy_rtx (true1);
9182 if (COMPARISON_P (x))
9184 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9185 true0, true1);
9186 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9187 false0, false1);
9189 else
9191 *ptrue = simplify_gen_binary (code, mode, true0, true1);
9192 *pfalse = simplify_gen_binary (code, mode, false0, false1);
9195 return cond0 ? cond0 : cond1;
9198 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9199 operands is zero when the other is nonzero, and vice-versa,
9200 and STORE_FLAG_VALUE is 1 or -1. */
9202 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9203 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9204 || code == UMAX)
9205 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9207 rtx op0 = XEXP (XEXP (x, 0), 1);
9208 rtx op1 = XEXP (XEXP (x, 1), 1);
9210 cond0 = XEXP (XEXP (x, 0), 0);
9211 cond1 = XEXP (XEXP (x, 1), 0);
9213 if (COMPARISON_P (cond0)
9214 && COMPARISON_P (cond1)
9215 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9216 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9217 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9218 || ((swap_condition (GET_CODE (cond0))
9219 == reversed_comparison_code (cond1, NULL))
9220 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9221 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9222 && ! side_effects_p (x))
9224 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9225 *pfalse = simplify_gen_binary (MULT, mode,
9226 (code == MINUS
9227 ? simplify_gen_unary (NEG, mode,
9228 op1, mode)
9229 : op1),
9230 const_true_rtx);
9231 return cond0;
9235 /* Similarly for MULT, AND and UMIN, except that for these the result
9236 is always zero. */
9237 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9238 && (code == MULT || code == AND || code == UMIN)
9239 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9241 cond0 = XEXP (XEXP (x, 0), 0);
9242 cond1 = XEXP (XEXP (x, 1), 0);
9244 if (COMPARISON_P (cond0)
9245 && COMPARISON_P (cond1)
9246 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9247 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9248 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9249 || ((swap_condition (GET_CODE (cond0))
9250 == reversed_comparison_code (cond1, NULL))
9251 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9252 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9253 && ! side_effects_p (x))
9255 *ptrue = *pfalse = const0_rtx;
9256 return cond0;
9261 else if (code == IF_THEN_ELSE)
9263 /* If we have IF_THEN_ELSE already, extract the condition and
9264 canonicalize it if it is NE or EQ. */
9265 cond0 = XEXP (x, 0);
9266 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9267 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9268 return XEXP (cond0, 0);
9269 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9271 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9272 return XEXP (cond0, 0);
9274 else
9275 return cond0;
9278 /* If X is a SUBREG, we can narrow both the true and false values
9279 if the inner expression, if there is a condition. */
9280 else if (code == SUBREG
9281 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
9282 &true0, &false0)))
9284 true0 = simplify_gen_subreg (mode, true0,
9285 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9286 false0 = simplify_gen_subreg (mode, false0,
9287 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9288 if (true0 && false0)
9290 *ptrue = true0;
9291 *pfalse = false0;
9292 return cond0;
9296 /* If X is a constant, this isn't special and will cause confusions
9297 if we treat it as such. Likewise if it is equivalent to a constant. */
9298 else if (CONSTANT_P (x)
9299 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9302 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9303 will be least confusing to the rest of the compiler. */
9304 else if (mode == BImode)
9306 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9307 return x;
9310 /* If X is known to be either 0 or -1, those are the true and
9311 false values when testing X. */
9312 else if (x == constm1_rtx || x == const0_rtx
9313 || (is_a <scalar_int_mode> (mode, &int_mode)
9314 && (num_sign_bit_copies (x, int_mode)
9315 == GET_MODE_PRECISION (int_mode))))
9317 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9318 return x;
9321 /* Likewise for 0 or a single bit. */
9322 else if (HWI_COMPUTABLE_MODE_P (mode)
9323 && pow2p_hwi (nz = nonzero_bits (x, mode)))
9325 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9326 return x;
9329 /* Otherwise fail; show no condition with true and false values the same. */
9330 *ptrue = *pfalse = x;
9331 return 0;
9334 /* Return the value of expression X given the fact that condition COND
9335 is known to be true when applied to REG as its first operand and VAL
9336 as its second. X is known to not be shared and so can be modified in
9337 place.
9339 We only handle the simplest cases, and specifically those cases that
9340 arise with IF_THEN_ELSE expressions. */
9342 static rtx
9343 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9345 enum rtx_code code = GET_CODE (x);
9346 const char *fmt;
9347 int i, j;
9349 if (side_effects_p (x))
9350 return x;
9352 /* If either operand of the condition is a floating point value,
9353 then we have to avoid collapsing an EQ comparison. */
9354 if (cond == EQ
9355 && rtx_equal_p (x, reg)
9356 && ! FLOAT_MODE_P (GET_MODE (x))
9357 && ! FLOAT_MODE_P (GET_MODE (val)))
9358 return val;
9360 if (cond == UNEQ && rtx_equal_p (x, reg))
9361 return val;
9363 /* If X is (abs REG) and we know something about REG's relationship
9364 with zero, we may be able to simplify this. */
9366 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9367 switch (cond)
9369 case GE: case GT: case EQ:
9370 return XEXP (x, 0);
9371 case LT: case LE:
9372 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9373 XEXP (x, 0),
9374 GET_MODE (XEXP (x, 0)));
9375 default:
9376 break;
9379 /* The only other cases we handle are MIN, MAX, and comparisons if the
9380 operands are the same as REG and VAL. */
9382 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9384 if (rtx_equal_p (XEXP (x, 0), val))
9386 std::swap (val, reg);
9387 cond = swap_condition (cond);
9390 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9392 if (COMPARISON_P (x))
9394 if (comparison_dominates_p (cond, code))
9395 return const_true_rtx;
9397 code = reversed_comparison_code (x, NULL);
9398 if (code != UNKNOWN
9399 && comparison_dominates_p (cond, code))
9400 return const0_rtx;
9401 else
9402 return x;
9404 else if (code == SMAX || code == SMIN
9405 || code == UMIN || code == UMAX)
9407 int unsignedp = (code == UMIN || code == UMAX);
9409 /* Do not reverse the condition when it is NE or EQ.
9410 This is because we cannot conclude anything about
9411 the value of 'SMAX (x, y)' when x is not equal to y,
9412 but we can when x equals y. */
9413 if ((code == SMAX || code == UMAX)
9414 && ! (cond == EQ || cond == NE))
9415 cond = reverse_condition (cond);
9417 switch (cond)
9419 case GE: case GT:
9420 return unsignedp ? x : XEXP (x, 1);
9421 case LE: case LT:
9422 return unsignedp ? x : XEXP (x, 0);
9423 case GEU: case GTU:
9424 return unsignedp ? XEXP (x, 1) : x;
9425 case LEU: case LTU:
9426 return unsignedp ? XEXP (x, 0) : x;
9427 default:
9428 break;
9433 else if (code == SUBREG)
9435 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9436 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9438 if (SUBREG_REG (x) != r)
9440 /* We must simplify subreg here, before we lose track of the
9441 original inner_mode. */
9442 new_rtx = simplify_subreg (GET_MODE (x), r,
9443 inner_mode, SUBREG_BYTE (x));
9444 if (new_rtx)
9445 return new_rtx;
9446 else
9447 SUBST (SUBREG_REG (x), r);
9450 return x;
9452 /* We don't have to handle SIGN_EXTEND here, because even in the
9453 case of replacing something with a modeless CONST_INT, a
9454 CONST_INT is already (supposed to be) a valid sign extension for
9455 its narrower mode, which implies it's already properly
9456 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9457 story is different. */
9458 else if (code == ZERO_EXTEND)
9460 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9461 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9463 if (XEXP (x, 0) != r)
9465 /* We must simplify the zero_extend here, before we lose
9466 track of the original inner_mode. */
9467 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9468 r, inner_mode);
9469 if (new_rtx)
9470 return new_rtx;
9471 else
9472 SUBST (XEXP (x, 0), r);
9475 return x;
9478 fmt = GET_RTX_FORMAT (code);
9479 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9481 if (fmt[i] == 'e')
9482 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9483 else if (fmt[i] == 'E')
9484 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9485 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9486 cond, reg, val));
9489 return x;
9492 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9493 assignment as a field assignment. */
9495 static int
9496 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9498 if (widen_x && GET_MODE (x) != GET_MODE (y))
9500 if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9501 return 0;
9502 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9503 return 0;
9504 /* For big endian, adjust the memory offset. */
9505 if (BYTES_BIG_ENDIAN)
9506 x = adjust_address_nv (x, GET_MODE (y),
9507 -subreg_lowpart_offset (GET_MODE (x),
9508 GET_MODE (y)));
9509 else
9510 x = adjust_address_nv (x, GET_MODE (y), 0);
9513 if (x == y || rtx_equal_p (x, y))
9514 return 1;
9516 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9517 return 0;
9519 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9520 Note that all SUBREGs of MEM are paradoxical; otherwise they
9521 would have been rewritten. */
9522 if (MEM_P (x) && GET_CODE (y) == SUBREG
9523 && MEM_P (SUBREG_REG (y))
9524 && rtx_equal_p (SUBREG_REG (y),
9525 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9526 return 1;
9528 if (MEM_P (y) && GET_CODE (x) == SUBREG
9529 && MEM_P (SUBREG_REG (x))
9530 && rtx_equal_p (SUBREG_REG (x),
9531 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9532 return 1;
9534 /* We used to see if get_last_value of X and Y were the same but that's
9535 not correct. In one direction, we'll cause the assignment to have
9536 the wrong destination and in the case, we'll import a register into this
9537 insn that might have already have been dead. So fail if none of the
9538 above cases are true. */
9539 return 0;
9542 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9543 Return that assignment if so.
9545 We only handle the most common cases. */
9547 static rtx
9548 make_field_assignment (rtx x)
9550 rtx dest = SET_DEST (x);
9551 rtx src = SET_SRC (x);
9552 rtx assign;
9553 rtx rhs, lhs;
9554 HOST_WIDE_INT c1;
9555 HOST_WIDE_INT pos;
9556 unsigned HOST_WIDE_INT len;
9557 rtx other;
9559 /* All the rules in this function are specific to scalar integers. */
9560 scalar_int_mode mode;
9561 if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9562 return x;
9564 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9565 a clear of a one-bit field. We will have changed it to
9566 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9567 for a SUBREG. */
9569 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9570 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9571 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9572 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9574 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9575 1, 1, 1, 0);
9576 if (assign != 0)
9577 return gen_rtx_SET (assign, const0_rtx);
9578 return x;
9581 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9582 && subreg_lowpart_p (XEXP (src, 0))
9583 && partial_subreg_p (XEXP (src, 0))
9584 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9585 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9586 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9587 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9589 assign = make_extraction (VOIDmode, dest, 0,
9590 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9591 1, 1, 1, 0);
9592 if (assign != 0)
9593 return gen_rtx_SET (assign, const0_rtx);
9594 return x;
9597 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9598 one-bit field. */
9599 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9600 && XEXP (XEXP (src, 0), 0) == const1_rtx
9601 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9603 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9604 1, 1, 1, 0);
9605 if (assign != 0)
9606 return gen_rtx_SET (assign, const1_rtx);
9607 return x;
9610 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9611 SRC is an AND with all bits of that field set, then we can discard
9612 the AND. */
9613 if (GET_CODE (dest) == ZERO_EXTRACT
9614 && CONST_INT_P (XEXP (dest, 1))
9615 && GET_CODE (src) == AND
9616 && CONST_INT_P (XEXP (src, 1)))
9618 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9619 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9620 unsigned HOST_WIDE_INT ze_mask;
9622 if (width >= HOST_BITS_PER_WIDE_INT)
9623 ze_mask = -1;
9624 else
9625 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9627 /* Complete overlap. We can remove the source AND. */
9628 if ((and_mask & ze_mask) == ze_mask)
9629 return gen_rtx_SET (dest, XEXP (src, 0));
9631 /* Partial overlap. We can reduce the source AND. */
9632 if ((and_mask & ze_mask) != and_mask)
9634 src = gen_rtx_AND (mode, XEXP (src, 0),
9635 gen_int_mode (and_mask & ze_mask, mode));
9636 return gen_rtx_SET (dest, src);
9640 /* The other case we handle is assignments into a constant-position
9641 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9642 a mask that has all one bits except for a group of zero bits and
9643 OTHER is known to have zeros where C1 has ones, this is such an
9644 assignment. Compute the position and length from C1. Shift OTHER
9645 to the appropriate position, force it to the required mode, and
9646 make the extraction. Check for the AND in both operands. */
9648 /* One or more SUBREGs might obscure the constant-position field
9649 assignment. The first one we are likely to encounter is an outer
9650 narrowing SUBREG, which we can just strip for the purposes of
9651 identifying the constant-field assignment. */
9652 scalar_int_mode src_mode = mode;
9653 if (GET_CODE (src) == SUBREG
9654 && subreg_lowpart_p (src)
9655 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9656 src = SUBREG_REG (src);
9658 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9659 return x;
9661 rhs = expand_compound_operation (XEXP (src, 0));
9662 lhs = expand_compound_operation (XEXP (src, 1));
9664 if (GET_CODE (rhs) == AND
9665 && CONST_INT_P (XEXP (rhs, 1))
9666 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9667 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9668 /* The second SUBREG that might get in the way is a paradoxical
9669 SUBREG around the first operand of the AND. We want to
9670 pretend the operand is as wide as the destination here. We
9671 do this by adjusting the MEM to wider mode for the sole
9672 purpose of the call to rtx_equal_for_field_assignment_p. Also
9673 note this trick only works for MEMs. */
9674 else if (GET_CODE (rhs) == AND
9675 && paradoxical_subreg_p (XEXP (rhs, 0))
9676 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9677 && CONST_INT_P (XEXP (rhs, 1))
9678 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9679 dest, true))
9680 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9681 else if (GET_CODE (lhs) == AND
9682 && CONST_INT_P (XEXP (lhs, 1))
9683 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9684 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9685 /* The second SUBREG that might get in the way is a paradoxical
9686 SUBREG around the first operand of the AND. We want to
9687 pretend the operand is as wide as the destination here. We
9688 do this by adjusting the MEM to wider mode for the sole
9689 purpose of the call to rtx_equal_for_field_assignment_p. Also
9690 note this trick only works for MEMs. */
9691 else if (GET_CODE (lhs) == AND
9692 && paradoxical_subreg_p (XEXP (lhs, 0))
9693 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9694 && CONST_INT_P (XEXP (lhs, 1))
9695 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9696 dest, true))
9697 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9698 else
9699 return x;
9701 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9702 if (pos < 0
9703 || pos + len > GET_MODE_PRECISION (mode)
9704 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9705 || (c1 & nonzero_bits (other, mode)) != 0)
9706 return x;
9708 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9709 if (assign == 0)
9710 return x;
9712 /* The mode to use for the source is the mode of the assignment, or of
9713 what is inside a possible STRICT_LOW_PART. */
9714 machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9715 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9717 /* Shift OTHER right POS places and make it the source, restricting it
9718 to the proper length and mode. */
9720 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9721 src_mode, other, pos),
9722 dest);
9723 src = force_to_mode (src, new_mode,
9724 len >= HOST_BITS_PER_WIDE_INT
9725 ? HOST_WIDE_INT_M1U
9726 : (HOST_WIDE_INT_1U << len) - 1,
9729 /* If SRC is masked by an AND that does not make a difference in
9730 the value being stored, strip it. */
9731 if (GET_CODE (assign) == ZERO_EXTRACT
9732 && CONST_INT_P (XEXP (assign, 1))
9733 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9734 && GET_CODE (src) == AND
9735 && CONST_INT_P (XEXP (src, 1))
9736 && UINTVAL (XEXP (src, 1))
9737 == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9738 src = XEXP (src, 0);
9740 return gen_rtx_SET (assign, src);
9743 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9744 if so. */
9746 static rtx
9747 apply_distributive_law (rtx x)
9749 enum rtx_code code = GET_CODE (x);
9750 enum rtx_code inner_code;
9751 rtx lhs, rhs, other;
9752 rtx tem;
9754 /* Distributivity is not true for floating point as it can change the
9755 value. So we don't do it unless -funsafe-math-optimizations. */
9756 if (FLOAT_MODE_P (GET_MODE (x))
9757 && ! flag_unsafe_math_optimizations)
9758 return x;
9760 /* The outer operation can only be one of the following: */
9761 if (code != IOR && code != AND && code != XOR
9762 && code != PLUS && code != MINUS)
9763 return x;
9765 lhs = XEXP (x, 0);
9766 rhs = XEXP (x, 1);
9768 /* If either operand is a primitive we can't do anything, so get out
9769 fast. */
9770 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9771 return x;
9773 lhs = expand_compound_operation (lhs);
9774 rhs = expand_compound_operation (rhs);
9775 inner_code = GET_CODE (lhs);
9776 if (inner_code != GET_CODE (rhs))
9777 return x;
9779 /* See if the inner and outer operations distribute. */
9780 switch (inner_code)
9782 case LSHIFTRT:
9783 case ASHIFTRT:
9784 case AND:
9785 case IOR:
9786 /* These all distribute except over PLUS. */
9787 if (code == PLUS || code == MINUS)
9788 return x;
9789 break;
9791 case MULT:
9792 if (code != PLUS && code != MINUS)
9793 return x;
9794 break;
9796 case ASHIFT:
9797 /* This is also a multiply, so it distributes over everything. */
9798 break;
9800 /* This used to handle SUBREG, but this turned out to be counter-
9801 productive, since (subreg (op ...)) usually is not handled by
9802 insn patterns, and this "optimization" therefore transformed
9803 recognizable patterns into unrecognizable ones. Therefore the
9804 SUBREG case was removed from here.
9806 It is possible that distributing SUBREG over arithmetic operations
9807 leads to an intermediate result than can then be optimized further,
9808 e.g. by moving the outer SUBREG to the other side of a SET as done
9809 in simplify_set. This seems to have been the original intent of
9810 handling SUBREGs here.
9812 However, with current GCC this does not appear to actually happen,
9813 at least on major platforms. If some case is found where removing
9814 the SUBREG case here prevents follow-on optimizations, distributing
9815 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9817 default:
9818 return x;
9821 /* Set LHS and RHS to the inner operands (A and B in the example
9822 above) and set OTHER to the common operand (C in the example).
9823 There is only one way to do this unless the inner operation is
9824 commutative. */
9825 if (COMMUTATIVE_ARITH_P (lhs)
9826 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9827 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9828 else if (COMMUTATIVE_ARITH_P (lhs)
9829 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9830 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9831 else if (COMMUTATIVE_ARITH_P (lhs)
9832 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9833 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9834 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9835 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9836 else
9837 return x;
9839 /* Form the new inner operation, seeing if it simplifies first. */
9840 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9842 /* There is one exception to the general way of distributing:
9843 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9844 if (code == XOR && inner_code == IOR)
9846 inner_code = AND;
9847 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9850 /* We may be able to continuing distributing the result, so call
9851 ourselves recursively on the inner operation before forming the
9852 outer operation, which we return. */
9853 return simplify_gen_binary (inner_code, GET_MODE (x),
9854 apply_distributive_law (tem), other);
9857 /* See if X is of the form (* (+ A B) C), and if so convert to
9858 (+ (* A C) (* B C)) and try to simplify.
9860 Most of the time, this results in no change. However, if some of
9861 the operands are the same or inverses of each other, simplifications
9862 will result.
9864 For example, (and (ior A B) (not B)) can occur as the result of
9865 expanding a bit field assignment. When we apply the distributive
9866 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9867 which then simplifies to (and (A (not B))).
9869 Note that no checks happen on the validity of applying the inverse
9870 distributive law. This is pointless since we can do it in the
9871 few places where this routine is called.
9873 N is the index of the term that is decomposed (the arithmetic operation,
9874 i.e. (+ A B) in the first example above). !N is the index of the term that
9875 is distributed, i.e. of C in the first example above. */
9876 static rtx
9877 distribute_and_simplify_rtx (rtx x, int n)
9879 machine_mode mode;
9880 enum rtx_code outer_code, inner_code;
9881 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9883 /* Distributivity is not true for floating point as it can change the
9884 value. So we don't do it unless -funsafe-math-optimizations. */
9885 if (FLOAT_MODE_P (GET_MODE (x))
9886 && ! flag_unsafe_math_optimizations)
9887 return NULL_RTX;
9889 decomposed = XEXP (x, n);
9890 if (!ARITHMETIC_P (decomposed))
9891 return NULL_RTX;
9893 mode = GET_MODE (x);
9894 outer_code = GET_CODE (x);
9895 distributed = XEXP (x, !n);
9897 inner_code = GET_CODE (decomposed);
9898 inner_op0 = XEXP (decomposed, 0);
9899 inner_op1 = XEXP (decomposed, 1);
9901 /* Special case (and (xor B C) (not A)), which is equivalent to
9902 (xor (ior A B) (ior A C)) */
9903 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9905 distributed = XEXP (distributed, 0);
9906 outer_code = IOR;
9909 if (n == 0)
9911 /* Distribute the second term. */
9912 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9913 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9915 else
9917 /* Distribute the first term. */
9918 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9919 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9922 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9923 new_op0, new_op1));
9924 if (GET_CODE (tmp) != outer_code
9925 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
9926 < set_src_cost (x, mode, optimize_this_for_speed_p)))
9927 return tmp;
9929 return NULL_RTX;
9932 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9933 in MODE. Return an equivalent form, if different from (and VAROP
9934 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9936 static rtx
9937 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
9938 unsigned HOST_WIDE_INT constop)
9940 unsigned HOST_WIDE_INT nonzero;
9941 unsigned HOST_WIDE_INT orig_constop;
9942 rtx orig_varop;
9943 int i;
9945 orig_varop = varop;
9946 orig_constop = constop;
9947 if (GET_CODE (varop) == CLOBBER)
9948 return NULL_RTX;
9950 /* Simplify VAROP knowing that we will be only looking at some of the
9951 bits in it.
9953 Note by passing in CONSTOP, we guarantee that the bits not set in
9954 CONSTOP are not significant and will never be examined. We must
9955 ensure that is the case by explicitly masking out those bits
9956 before returning. */
9957 varop = force_to_mode (varop, mode, constop, 0);
9959 /* If VAROP is a CLOBBER, we will fail so return it. */
9960 if (GET_CODE (varop) == CLOBBER)
9961 return varop;
9963 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9964 to VAROP and return the new constant. */
9965 if (CONST_INT_P (varop))
9966 return gen_int_mode (INTVAL (varop) & constop, mode);
9968 /* See what bits may be nonzero in VAROP. Unlike the general case of
9969 a call to nonzero_bits, here we don't care about bits outside
9970 MODE. */
9972 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9974 /* Turn off all bits in the constant that are known to already be zero.
9975 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9976 which is tested below. */
9978 constop &= nonzero;
9980 /* If we don't have any bits left, return zero. */
9981 if (constop == 0)
9982 return const0_rtx;
9984 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9985 a power of two, we can replace this with an ASHIFT. */
9986 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9987 && (i = exact_log2 (constop)) >= 0)
9988 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9990 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9991 or XOR, then try to apply the distributive law. This may eliminate
9992 operations if either branch can be simplified because of the AND.
9993 It may also make some cases more complex, but those cases probably
9994 won't match a pattern either with or without this. */
9996 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9998 scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
9999 return
10000 gen_lowpart
10001 (mode,
10002 apply_distributive_law
10003 (simplify_gen_binary (GET_CODE (varop), varop_mode,
10004 simplify_and_const_int (NULL_RTX, varop_mode,
10005 XEXP (varop, 0),
10006 constop),
10007 simplify_and_const_int (NULL_RTX, varop_mode,
10008 XEXP (varop, 1),
10009 constop))));
10012 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10013 the AND and see if one of the operands simplifies to zero. If so, we
10014 may eliminate it. */
10016 if (GET_CODE (varop) == PLUS
10017 && pow2p_hwi (constop + 1))
10019 rtx o0, o1;
10021 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10022 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10023 if (o0 == const0_rtx)
10024 return o1;
10025 if (o1 == const0_rtx)
10026 return o0;
10029 /* Make a SUBREG if necessary. If we can't make it, fail. */
10030 varop = gen_lowpart (mode, varop);
10031 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10032 return NULL_RTX;
10034 /* If we are only masking insignificant bits, return VAROP. */
10035 if (constop == nonzero)
10036 return varop;
10038 if (varop == orig_varop && constop == orig_constop)
10039 return NULL_RTX;
10041 /* Otherwise, return an AND. */
10042 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10046 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10047 in MODE.
10049 Return an equivalent form, if different from X. Otherwise, return X. If
10050 X is zero, we are to always construct the equivalent form. */
10052 static rtx
10053 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10054 unsigned HOST_WIDE_INT constop)
10056 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10057 if (tem)
10058 return tem;
10060 if (!x)
10061 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10062 gen_int_mode (constop, mode));
10063 if (GET_MODE (x) != mode)
10064 x = gen_lowpart (mode, x);
10065 return x;
10068 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10069 We don't care about bits outside of those defined in MODE.
10071 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10072 a shift, AND, or zero_extract, we can do better. */
10074 static rtx
10075 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10076 scalar_int_mode mode,
10077 unsigned HOST_WIDE_INT *nonzero)
10079 rtx tem;
10080 reg_stat_type *rsp;
10082 /* If X is a register whose nonzero bits value is current, use it.
10083 Otherwise, if X is a register whose value we can find, use that
10084 value. Otherwise, use the previously-computed global nonzero bits
10085 for this register. */
10087 rsp = &reg_stat[REGNO (x)];
10088 if (rsp->last_set_value != 0
10089 && (rsp->last_set_mode == mode
10090 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10091 && GET_MODE_CLASS (mode) == MODE_INT))
10092 && ((rsp->last_set_label >= label_tick_ebb_start
10093 && rsp->last_set_label < label_tick)
10094 || (rsp->last_set_label == label_tick
10095 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10096 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10097 && REGNO (x) < reg_n_sets_max
10098 && REG_N_SETS (REGNO (x)) == 1
10099 && !REGNO_REG_SET_P
10100 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10101 REGNO (x)))))
10103 /* Note that, even if the precision of last_set_mode is lower than that
10104 of mode, record_value_for_reg invoked nonzero_bits on the register
10105 with nonzero_bits_mode (because last_set_mode is necessarily integral
10106 and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10107 are all valid, hence in mode too since nonzero_bits_mode is defined
10108 to the largest HWI_COMPUTABLE_MODE_P mode. */
10109 *nonzero &= rsp->last_set_nonzero_bits;
10110 return NULL;
10113 tem = get_last_value (x);
10114 if (tem)
10116 if (SHORT_IMMEDIATES_SIGN_EXTEND)
10117 tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10119 return tem;
10122 if (nonzero_sign_valid && rsp->nonzero_bits)
10124 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10126 if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10127 /* We don't know anything about the upper bits. */
10128 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10130 *nonzero &= mask;
10133 return NULL;
10136 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10137 end of X that are known to be equal to the sign bit. X will be used
10138 in mode MODE; the returned value will always be between 1 and the
10139 number of bits in MODE. */
10141 static rtx
10142 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10143 scalar_int_mode mode,
10144 unsigned int *result)
10146 rtx tem;
10147 reg_stat_type *rsp;
10149 rsp = &reg_stat[REGNO (x)];
10150 if (rsp->last_set_value != 0
10151 && rsp->last_set_mode == mode
10152 && ((rsp->last_set_label >= label_tick_ebb_start
10153 && rsp->last_set_label < label_tick)
10154 || (rsp->last_set_label == label_tick
10155 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10156 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10157 && REGNO (x) < reg_n_sets_max
10158 && REG_N_SETS (REGNO (x)) == 1
10159 && !REGNO_REG_SET_P
10160 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10161 REGNO (x)))))
10163 *result = rsp->last_set_sign_bit_copies;
10164 return NULL;
10167 tem = get_last_value (x);
10168 if (tem != 0)
10169 return tem;
10171 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10172 && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10173 *result = rsp->sign_bit_copies;
10175 return NULL;
10178 /* Return the number of "extended" bits there are in X, when interpreted
10179 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
10180 unsigned quantities, this is the number of high-order zero bits.
10181 For signed quantities, this is the number of copies of the sign bit
10182 minus 1. In both case, this function returns the number of "spare"
10183 bits. For example, if two quantities for which this function returns
10184 at least 1 are added, the addition is known not to overflow.
10186 This function will always return 0 unless called during combine, which
10187 implies that it must be called from a define_split. */
10189 unsigned int
10190 extended_count (const_rtx x, machine_mode mode, int unsignedp)
10192 if (nonzero_sign_valid == 0)
10193 return 0;
10195 scalar_int_mode int_mode;
10196 return (unsignedp
10197 ? (is_a <scalar_int_mode> (mode, &int_mode)
10198 && HWI_COMPUTABLE_MODE_P (int_mode)
10199 ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10200 - floor_log2 (nonzero_bits (x, int_mode)))
10201 : 0)
10202 : num_sign_bit_copies (x, mode) - 1);
10205 /* This function is called from `simplify_shift_const' to merge two
10206 outer operations. Specifically, we have already found that we need
10207 to perform operation *POP0 with constant *PCONST0 at the outermost
10208 position. We would now like to also perform OP1 with constant CONST1
10209 (with *POP0 being done last).
10211 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
10212 the resulting operation. *PCOMP_P is set to 1 if we would need to
10213 complement the innermost operand, otherwise it is unchanged.
10215 MODE is the mode in which the operation will be done. No bits outside
10216 the width of this mode matter. It is assumed that the width of this mode
10217 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10219 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
10220 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
10221 result is simply *PCONST0.
10223 If the resulting operation cannot be expressed as one operation, we
10224 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
10226 static int
10227 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)
10229 enum rtx_code op0 = *pop0;
10230 HOST_WIDE_INT const0 = *pconst0;
10232 const0 &= GET_MODE_MASK (mode);
10233 const1 &= GET_MODE_MASK (mode);
10235 /* If OP0 is an AND, clear unimportant bits in CONST1. */
10236 if (op0 == AND)
10237 const1 &= const0;
10239 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
10240 if OP0 is SET. */
10242 if (op1 == UNKNOWN || op0 == SET)
10243 return 1;
10245 else if (op0 == UNKNOWN)
10246 op0 = op1, const0 = const1;
10248 else if (op0 == op1)
10250 switch (op0)
10252 case AND:
10253 const0 &= const1;
10254 break;
10255 case IOR:
10256 const0 |= const1;
10257 break;
10258 case XOR:
10259 const0 ^= const1;
10260 break;
10261 case PLUS:
10262 const0 += const1;
10263 break;
10264 case NEG:
10265 op0 = UNKNOWN;
10266 break;
10267 default:
10268 break;
10272 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
10273 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10274 return 0;
10276 /* If the two constants aren't the same, we can't do anything. The
10277 remaining six cases can all be done. */
10278 else if (const0 != const1)
10279 return 0;
10281 else
10282 switch (op0)
10284 case IOR:
10285 if (op1 == AND)
10286 /* (a & b) | b == b */
10287 op0 = SET;
10288 else /* op1 == XOR */
10289 /* (a ^ b) | b == a | b */
10291 break;
10293 case XOR:
10294 if (op1 == AND)
10295 /* (a & b) ^ b == (~a) & b */
10296 op0 = AND, *pcomp_p = 1;
10297 else /* op1 == IOR */
10298 /* (a | b) ^ b == a & ~b */
10299 op0 = AND, const0 = ~const0;
10300 break;
10302 case AND:
10303 if (op1 == IOR)
10304 /* (a | b) & b == b */
10305 op0 = SET;
10306 else /* op1 == XOR */
10307 /* (a ^ b) & b) == (~a) & b */
10308 *pcomp_p = 1;
10309 break;
10310 default:
10311 break;
10314 /* Check for NO-OP cases. */
10315 const0 &= GET_MODE_MASK (mode);
10316 if (const0 == 0
10317 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10318 op0 = UNKNOWN;
10319 else if (const0 == 0 && op0 == AND)
10320 op0 = SET;
10321 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10322 && op0 == AND)
10323 op0 = UNKNOWN;
10325 *pop0 = op0;
10327 /* ??? Slightly redundant with the above mask, but not entirely.
10328 Moving this above means we'd have to sign-extend the mode mask
10329 for the final test. */
10330 if (op0 != UNKNOWN && op0 != NEG)
10331 *pconst0 = trunc_int_for_mode (const0, mode);
10333 return 1;
10336 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10337 the shift in. The original shift operation CODE is performed on OP in
10338 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10339 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10340 result of the shift is subject to operation OUTER_CODE with operand
10341 OUTER_CONST. */
10343 static scalar_int_mode
10344 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10345 scalar_int_mode orig_mode, scalar_int_mode mode,
10346 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10348 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10350 /* In general we can't perform in wider mode for right shift and rotate. */
10351 switch (code)
10353 case ASHIFTRT:
10354 /* We can still widen if the bits brought in from the left are identical
10355 to the sign bit of ORIG_MODE. */
10356 if (num_sign_bit_copies (op, mode)
10357 > (unsigned) (GET_MODE_PRECISION (mode)
10358 - GET_MODE_PRECISION (orig_mode)))
10359 return mode;
10360 return orig_mode;
10362 case LSHIFTRT:
10363 /* Similarly here but with zero bits. */
10364 if (HWI_COMPUTABLE_MODE_P (mode)
10365 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10366 return mode;
10368 /* We can also widen if the bits brought in will be masked off. This
10369 operation is performed in ORIG_MODE. */
10370 if (outer_code == AND)
10372 int care_bits = low_bitmask_len (orig_mode, outer_const);
10374 if (care_bits >= 0
10375 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10376 return mode;
10378 /* fall through */
10380 case ROTATE:
10381 return orig_mode;
10383 case ROTATERT:
10384 gcc_unreachable ();
10386 default:
10387 return mode;
10391 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10392 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10393 if we cannot simplify it. Otherwise, return a simplified value.
10395 The shift is normally computed in the widest mode we find in VAROP, as
10396 long as it isn't a different number of words than RESULT_MODE. Exceptions
10397 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10399 static rtx
10400 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10401 rtx varop, int orig_count)
10403 enum rtx_code orig_code = code;
10404 rtx orig_varop = varop;
10405 int count;
10406 machine_mode mode = result_mode;
10407 machine_mode shift_mode;
10408 scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10409 unsigned int mode_words
10410 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10411 /* We form (outer_op (code varop count) (outer_const)). */
10412 enum rtx_code outer_op = UNKNOWN;
10413 HOST_WIDE_INT outer_const = 0;
10414 int complement_p = 0;
10415 rtx new_rtx, x;
10417 /* Make sure and truncate the "natural" shift on the way in. We don't
10418 want to do this inside the loop as it makes it more difficult to
10419 combine shifts. */
10420 if (SHIFT_COUNT_TRUNCATED)
10421 orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10423 /* If we were given an invalid count, don't do anything except exactly
10424 what was requested. */
10426 if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10427 return NULL_RTX;
10429 count = orig_count;
10431 /* Unless one of the branches of the `if' in this loop does a `continue',
10432 we will `break' the loop after the `if'. */
10434 while (count != 0)
10436 /* If we have an operand of (clobber (const_int 0)), fail. */
10437 if (GET_CODE (varop) == CLOBBER)
10438 return NULL_RTX;
10440 /* Convert ROTATERT to ROTATE. */
10441 if (code == ROTATERT)
10443 unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10444 code = ROTATE;
10445 count = bitsize - count;
10448 shift_mode = result_mode;
10449 if (shift_mode != mode)
10451 /* We only change the modes of scalar shifts. */
10452 int_mode = as_a <scalar_int_mode> (mode);
10453 int_result_mode = as_a <scalar_int_mode> (result_mode);
10454 shift_mode = try_widen_shift_mode (code, varop, count,
10455 int_result_mode, int_mode,
10456 outer_op, outer_const);
10459 scalar_int_mode shift_unit_mode
10460 = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10462 /* Handle cases where the count is greater than the size of the mode
10463 minus 1. For ASHIFT, use the size minus one as the count (this can
10464 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10465 take the count modulo the size. For other shifts, the result is
10466 zero.
10468 Since these shifts are being produced by the compiler by combining
10469 multiple operations, each of which are defined, we know what the
10470 result is supposed to be. */
10472 if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10474 if (code == ASHIFTRT)
10475 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10476 else if (code == ROTATE || code == ROTATERT)
10477 count %= GET_MODE_PRECISION (shift_unit_mode);
10478 else
10480 /* We can't simply return zero because there may be an
10481 outer op. */
10482 varop = const0_rtx;
10483 count = 0;
10484 break;
10488 /* If we discovered we had to complement VAROP, leave. Making a NOT
10489 here would cause an infinite loop. */
10490 if (complement_p)
10491 break;
10493 if (shift_mode == shift_unit_mode)
10495 /* An arithmetic right shift of a quantity known to be -1 or 0
10496 is a no-op. */
10497 if (code == ASHIFTRT
10498 && (num_sign_bit_copies (varop, shift_unit_mode)
10499 == GET_MODE_PRECISION (shift_unit_mode)))
10501 count = 0;
10502 break;
10505 /* If we are doing an arithmetic right shift and discarding all but
10506 the sign bit copies, this is equivalent to doing a shift by the
10507 bitsize minus one. Convert it into that shift because it will
10508 often allow other simplifications. */
10510 if (code == ASHIFTRT
10511 && (count + num_sign_bit_copies (varop, shift_unit_mode)
10512 >= GET_MODE_PRECISION (shift_unit_mode)))
10513 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10515 /* We simplify the tests below and elsewhere by converting
10516 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10517 `make_compound_operation' will convert it to an ASHIFTRT for
10518 those machines (such as VAX) that don't have an LSHIFTRT. */
10519 if (code == ASHIFTRT
10520 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10521 && val_signbit_known_clear_p (shift_unit_mode,
10522 nonzero_bits (varop,
10523 shift_unit_mode)))
10524 code = LSHIFTRT;
10526 if (((code == LSHIFTRT
10527 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10528 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10529 || (code == ASHIFT
10530 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10531 && !((nonzero_bits (varop, shift_unit_mode) << count)
10532 & GET_MODE_MASK (shift_unit_mode))))
10533 && !side_effects_p (varop))
10534 varop = const0_rtx;
10537 switch (GET_CODE (varop))
10539 case SIGN_EXTEND:
10540 case ZERO_EXTEND:
10541 case SIGN_EXTRACT:
10542 case ZERO_EXTRACT:
10543 new_rtx = expand_compound_operation (varop);
10544 if (new_rtx != varop)
10546 varop = new_rtx;
10547 continue;
10549 break;
10551 case MEM:
10552 /* The following rules apply only to scalars. */
10553 if (shift_mode != shift_unit_mode)
10554 break;
10555 int_mode = as_a <scalar_int_mode> (mode);
10557 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10558 minus the width of a smaller mode, we can do this with a
10559 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10560 if ((code == ASHIFTRT || code == LSHIFTRT)
10561 && ! mode_dependent_address_p (XEXP (varop, 0),
10562 MEM_ADDR_SPACE (varop))
10563 && ! MEM_VOLATILE_P (varop)
10564 && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10565 .exists (&tmode)))
10567 new_rtx = adjust_address_nv (varop, tmode,
10568 BYTES_BIG_ENDIAN ? 0
10569 : count / BITS_PER_UNIT);
10571 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10572 : ZERO_EXTEND, int_mode, new_rtx);
10573 count = 0;
10574 continue;
10576 break;
10578 case SUBREG:
10579 /* The following rules apply only to scalars. */
10580 if (shift_mode != shift_unit_mode)
10581 break;
10582 int_mode = as_a <scalar_int_mode> (mode);
10583 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10585 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10586 the same number of words as what we've seen so far. Then store
10587 the widest mode in MODE. */
10588 if (subreg_lowpart_p (varop)
10589 && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10590 && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10591 && (unsigned int) ((GET_MODE_SIZE (inner_mode)
10592 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10593 == mode_words
10594 && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10596 varop = SUBREG_REG (varop);
10597 if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10598 mode = inner_mode;
10599 continue;
10601 break;
10603 case MULT:
10604 /* Some machines use MULT instead of ASHIFT because MULT
10605 is cheaper. But it is still better on those machines to
10606 merge two shifts into one. */
10607 if (CONST_INT_P (XEXP (varop, 1))
10608 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10610 varop
10611 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10612 XEXP (varop, 0),
10613 GEN_INT (exact_log2 (
10614 UINTVAL (XEXP (varop, 1)))));
10615 continue;
10617 break;
10619 case UDIV:
10620 /* Similar, for when divides are cheaper. */
10621 if (CONST_INT_P (XEXP (varop, 1))
10622 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10624 varop
10625 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10626 XEXP (varop, 0),
10627 GEN_INT (exact_log2 (
10628 UINTVAL (XEXP (varop, 1)))));
10629 continue;
10631 break;
10633 case ASHIFTRT:
10634 /* If we are extracting just the sign bit of an arithmetic
10635 right shift, that shift is not needed. However, the sign
10636 bit of a wider mode may be different from what would be
10637 interpreted as the sign bit in a narrower mode, so, if
10638 the result is narrower, don't discard the shift. */
10639 if (code == LSHIFTRT
10640 && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10641 && (GET_MODE_UNIT_BITSIZE (result_mode)
10642 >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10644 varop = XEXP (varop, 0);
10645 continue;
10648 /* fall through */
10650 case LSHIFTRT:
10651 case ASHIFT:
10652 case ROTATE:
10653 /* The following rules apply only to scalars. */
10654 if (shift_mode != shift_unit_mode)
10655 break;
10656 int_mode = as_a <scalar_int_mode> (mode);
10657 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10658 int_result_mode = as_a <scalar_int_mode> (result_mode);
10660 /* Here we have two nested shifts. The result is usually the
10661 AND of a new shift with a mask. We compute the result below. */
10662 if (CONST_INT_P (XEXP (varop, 1))
10663 && INTVAL (XEXP (varop, 1)) >= 0
10664 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10665 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10666 && HWI_COMPUTABLE_MODE_P (int_mode))
10668 enum rtx_code first_code = GET_CODE (varop);
10669 unsigned int first_count = INTVAL (XEXP (varop, 1));
10670 unsigned HOST_WIDE_INT mask;
10671 rtx mask_rtx;
10673 /* We have one common special case. We can't do any merging if
10674 the inner code is an ASHIFTRT of a smaller mode. However, if
10675 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10676 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10677 we can convert it to
10678 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10679 This simplifies certain SIGN_EXTEND operations. */
10680 if (code == ASHIFT && first_code == ASHIFTRT
10681 && count == (GET_MODE_PRECISION (int_result_mode)
10682 - GET_MODE_PRECISION (int_varop_mode)))
10684 /* C3 has the low-order C1 bits zero. */
10686 mask = GET_MODE_MASK (int_mode)
10687 & ~((HOST_WIDE_INT_1U << first_count) - 1);
10689 varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10690 XEXP (varop, 0), mask);
10691 varop = simplify_shift_const (NULL_RTX, ASHIFT,
10692 int_result_mode, varop, count);
10693 count = first_count;
10694 code = ASHIFTRT;
10695 continue;
10698 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10699 than C1 high-order bits equal to the sign bit, we can convert
10700 this to either an ASHIFT or an ASHIFTRT depending on the
10701 two counts.
10703 We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10705 if (code == ASHIFTRT && first_code == ASHIFT
10706 && int_varop_mode == shift_unit_mode
10707 && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10708 > first_count))
10710 varop = XEXP (varop, 0);
10711 count -= first_count;
10712 if (count < 0)
10714 count = -count;
10715 code = ASHIFT;
10718 continue;
10721 /* There are some cases we can't do. If CODE is ASHIFTRT,
10722 we can only do this if FIRST_CODE is also ASHIFTRT.
10724 We can't do the case when CODE is ROTATE and FIRST_CODE is
10725 ASHIFTRT.
10727 If the mode of this shift is not the mode of the outer shift,
10728 we can't do this if either shift is a right shift or ROTATE.
10730 Finally, we can't do any of these if the mode is too wide
10731 unless the codes are the same.
10733 Handle the case where the shift codes are the same
10734 first. */
10736 if (code == first_code)
10738 if (int_varop_mode != int_result_mode
10739 && (code == ASHIFTRT || code == LSHIFTRT
10740 || code == ROTATE))
10741 break;
10743 count += first_count;
10744 varop = XEXP (varop, 0);
10745 continue;
10748 if (code == ASHIFTRT
10749 || (code == ROTATE && first_code == ASHIFTRT)
10750 || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10751 || (int_varop_mode != int_result_mode
10752 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10753 || first_code == ROTATE
10754 || code == ROTATE)))
10755 break;
10757 /* To compute the mask to apply after the shift, shift the
10758 nonzero bits of the inner shift the same way the
10759 outer shift will. */
10761 mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10762 int_result_mode);
10764 mask_rtx
10765 = simplify_const_binary_operation (code, int_result_mode,
10766 mask_rtx, GEN_INT (count));
10768 /* Give up if we can't compute an outer operation to use. */
10769 if (mask_rtx == 0
10770 || !CONST_INT_P (mask_rtx)
10771 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10772 INTVAL (mask_rtx),
10773 int_result_mode, &complement_p))
10774 break;
10776 /* If the shifts are in the same direction, we add the
10777 counts. Otherwise, we subtract them. */
10778 if ((code == ASHIFTRT || code == LSHIFTRT)
10779 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10780 count += first_count;
10781 else
10782 count -= first_count;
10784 /* If COUNT is positive, the new shift is usually CODE,
10785 except for the two exceptions below, in which case it is
10786 FIRST_CODE. If the count is negative, FIRST_CODE should
10787 always be used */
10788 if (count > 0
10789 && ((first_code == ROTATE && code == ASHIFT)
10790 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10791 code = first_code;
10792 else if (count < 0)
10793 code = first_code, count = -count;
10795 varop = XEXP (varop, 0);
10796 continue;
10799 /* If we have (A << B << C) for any shift, we can convert this to
10800 (A << C << B). This wins if A is a constant. Only try this if
10801 B is not a constant. */
10803 else if (GET_CODE (varop) == code
10804 && CONST_INT_P (XEXP (varop, 0))
10805 && !CONST_INT_P (XEXP (varop, 1)))
10807 /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10808 sure the result will be masked. See PR70222. */
10809 if (code == LSHIFTRT
10810 && int_mode != int_result_mode
10811 && !merge_outer_ops (&outer_op, &outer_const, AND,
10812 GET_MODE_MASK (int_result_mode)
10813 >> orig_count, int_result_mode,
10814 &complement_p))
10815 break;
10816 /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
10817 up outer sign extension (often left and right shift) is
10818 hardly more efficient than the original. See PR70429. */
10819 if (code == ASHIFTRT && int_mode != int_result_mode)
10820 break;
10822 rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10823 XEXP (varop, 0),
10824 GEN_INT (count));
10825 varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10826 count = 0;
10827 continue;
10829 break;
10831 case NOT:
10832 /* The following rules apply only to scalars. */
10833 if (shift_mode != shift_unit_mode)
10834 break;
10836 /* Make this fit the case below. */
10837 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10838 continue;
10840 case IOR:
10841 case AND:
10842 case XOR:
10843 /* The following rules apply only to scalars. */
10844 if (shift_mode != shift_unit_mode)
10845 break;
10846 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10847 int_result_mode = as_a <scalar_int_mode> (result_mode);
10849 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10850 with C the size of VAROP - 1 and the shift is logical if
10851 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10852 we have an (le X 0) operation. If we have an arithmetic shift
10853 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10854 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10856 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10857 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10858 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10859 && (code == LSHIFTRT || code == ASHIFTRT)
10860 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
10861 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10863 count = 0;
10864 varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
10865 const0_rtx);
10867 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10868 varop = gen_rtx_NEG (int_varop_mode, varop);
10870 continue;
10873 /* If we have (shift (logical)), move the logical to the outside
10874 to allow it to possibly combine with another logical and the
10875 shift to combine with another shift. This also canonicalizes to
10876 what a ZERO_EXTRACT looks like. Also, some machines have
10877 (and (shift)) insns. */
10879 if (CONST_INT_P (XEXP (varop, 1))
10880 /* We can't do this if we have (ashiftrt (xor)) and the
10881 constant has its sign bit set in shift_unit_mode with
10882 shift_unit_mode wider than result_mode. */
10883 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10884 && int_result_mode != shift_unit_mode
10885 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10886 shift_unit_mode))
10887 && (new_rtx = simplify_const_binary_operation
10888 (code, int_result_mode,
10889 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
10890 GEN_INT (count))) != 0
10891 && CONST_INT_P (new_rtx)
10892 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10893 INTVAL (new_rtx), int_result_mode,
10894 &complement_p))
10896 varop = XEXP (varop, 0);
10897 continue;
10900 /* If we can't do that, try to simplify the shift in each arm of the
10901 logical expression, make a new logical expression, and apply
10902 the inverse distributive law. This also can't be done for
10903 (ashiftrt (xor)) where we've widened the shift and the constant
10904 changes the sign bit. */
10905 if (CONST_INT_P (XEXP (varop, 1))
10906 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10907 && int_result_mode != shift_unit_mode
10908 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10909 shift_unit_mode)))
10911 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10912 XEXP (varop, 0), count);
10913 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10914 XEXP (varop, 1), count);
10916 varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
10917 lhs, rhs);
10918 varop = apply_distributive_law (varop);
10920 count = 0;
10921 continue;
10923 break;
10925 case EQ:
10926 /* The following rules apply only to scalars. */
10927 if (shift_mode != shift_unit_mode)
10928 break;
10929 int_result_mode = as_a <scalar_int_mode> (result_mode);
10931 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10932 says that the sign bit can be tested, FOO has mode MODE, C is
10933 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10934 that may be nonzero. */
10935 if (code == LSHIFTRT
10936 && XEXP (varop, 1) == const0_rtx
10937 && GET_MODE (XEXP (varop, 0)) == int_result_mode
10938 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10939 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10940 && STORE_FLAG_VALUE == -1
10941 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
10942 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
10943 int_result_mode, &complement_p))
10945 varop = XEXP (varop, 0);
10946 count = 0;
10947 continue;
10949 break;
10951 case NEG:
10952 /* The following rules apply only to scalars. */
10953 if (shift_mode != shift_unit_mode)
10954 break;
10955 int_result_mode = as_a <scalar_int_mode> (result_mode);
10957 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10958 than the number of bits in the mode is equivalent to A. */
10959 if (code == LSHIFTRT
10960 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10961 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
10963 varop = XEXP (varop, 0);
10964 count = 0;
10965 continue;
10968 /* NEG commutes with ASHIFT since it is multiplication. Move the
10969 NEG outside to allow shifts to combine. */
10970 if (code == ASHIFT
10971 && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
10972 int_result_mode, &complement_p))
10974 varop = XEXP (varop, 0);
10975 continue;
10977 break;
10979 case PLUS:
10980 /* The following rules apply only to scalars. */
10981 if (shift_mode != shift_unit_mode)
10982 break;
10983 int_result_mode = as_a <scalar_int_mode> (result_mode);
10985 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10986 is one less than the number of bits in the mode is
10987 equivalent to (xor A 1). */
10988 if (code == LSHIFTRT
10989 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10990 && XEXP (varop, 1) == constm1_rtx
10991 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
10992 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
10993 int_result_mode, &complement_p))
10995 count = 0;
10996 varop = XEXP (varop, 0);
10997 continue;
11000 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11001 that might be nonzero in BAR are those being shifted out and those
11002 bits are known zero in FOO, we can replace the PLUS with FOO.
11003 Similarly in the other operand order. This code occurs when
11004 we are computing the size of a variable-size array. */
11006 if ((code == ASHIFTRT || code == LSHIFTRT)
11007 && count < HOST_BITS_PER_WIDE_INT
11008 && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11009 && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11010 & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11012 varop = XEXP (varop, 0);
11013 continue;
11015 else if ((code == ASHIFTRT || code == LSHIFTRT)
11016 && count < HOST_BITS_PER_WIDE_INT
11017 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11018 && 0 == (nonzero_bits (XEXP (varop, 0), int_result_mode)
11019 >> count)
11020 && 0 == (nonzero_bits (XEXP (varop, 0), int_result_mode)
11021 & nonzero_bits (XEXP (varop, 1), int_result_mode)))
11023 varop = XEXP (varop, 1);
11024 continue;
11027 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11028 if (code == ASHIFT
11029 && CONST_INT_P (XEXP (varop, 1))
11030 && (new_rtx = simplify_const_binary_operation
11031 (ASHIFT, int_result_mode,
11032 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11033 GEN_INT (count))) != 0
11034 && CONST_INT_P (new_rtx)
11035 && merge_outer_ops (&outer_op, &outer_const, PLUS,
11036 INTVAL (new_rtx), int_result_mode,
11037 &complement_p))
11039 varop = XEXP (varop, 0);
11040 continue;
11043 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11044 signbit', and attempt to change the PLUS to an XOR and move it to
11045 the outer operation as is done above in the AND/IOR/XOR case
11046 leg for shift(logical). See details in logical handling above
11047 for reasoning in doing so. */
11048 if (code == LSHIFTRT
11049 && CONST_INT_P (XEXP (varop, 1))
11050 && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11051 && (new_rtx = simplify_const_binary_operation
11052 (code, int_result_mode,
11053 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11054 GEN_INT (count))) != 0
11055 && CONST_INT_P (new_rtx)
11056 && merge_outer_ops (&outer_op, &outer_const, XOR,
11057 INTVAL (new_rtx), int_result_mode,
11058 &complement_p))
11060 varop = XEXP (varop, 0);
11061 continue;
11064 break;
11066 case MINUS:
11067 /* The following rules apply only to scalars. */
11068 if (shift_mode != shift_unit_mode)
11069 break;
11070 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11072 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11073 with C the size of VAROP - 1 and the shift is logical if
11074 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11075 we have a (gt X 0) operation. If the shift is arithmetic with
11076 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11077 we have a (neg (gt X 0)) operation. */
11079 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11080 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11081 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11082 && (code == LSHIFTRT || code == ASHIFTRT)
11083 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11084 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11085 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11087 count = 0;
11088 varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11089 const0_rtx);
11091 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11092 varop = gen_rtx_NEG (int_varop_mode, varop);
11094 continue;
11096 break;
11098 case TRUNCATE:
11099 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11100 if the truncate does not affect the value. */
11101 if (code == LSHIFTRT
11102 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11103 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11104 && (INTVAL (XEXP (XEXP (varop, 0), 1))
11105 >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11106 - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11108 rtx varop_inner = XEXP (varop, 0);
11110 varop_inner
11111 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11112 XEXP (varop_inner, 0),
11113 GEN_INT
11114 (count + INTVAL (XEXP (varop_inner, 1))));
11115 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11116 count = 0;
11117 continue;
11119 break;
11121 default:
11122 break;
11125 break;
11128 shift_mode = result_mode;
11129 if (shift_mode != mode)
11131 /* We only change the modes of scalar shifts. */
11132 int_mode = as_a <scalar_int_mode> (mode);
11133 int_result_mode = as_a <scalar_int_mode> (result_mode);
11134 shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11135 int_mode, outer_op, outer_const);
11138 /* We have now finished analyzing the shift. The result should be
11139 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11140 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11141 to the result of the shift. OUTER_CONST is the relevant constant,
11142 but we must turn off all bits turned off in the shift. */
11144 if (outer_op == UNKNOWN
11145 && orig_code == code && orig_count == count
11146 && varop == orig_varop
11147 && shift_mode == GET_MODE (varop))
11148 return NULL_RTX;
11150 /* Make a SUBREG if necessary. If we can't make it, fail. */
11151 varop = gen_lowpart (shift_mode, varop);
11152 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11153 return NULL_RTX;
11155 /* If we have an outer operation and we just made a shift, it is
11156 possible that we could have simplified the shift were it not
11157 for the outer operation. So try to do the simplification
11158 recursively. */
11160 if (outer_op != UNKNOWN)
11161 x = simplify_shift_const_1 (code, shift_mode, varop, count);
11162 else
11163 x = NULL_RTX;
11165 if (x == NULL_RTX)
11166 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
11168 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11169 turn off all the bits that the shift would have turned off. */
11170 if (orig_code == LSHIFTRT && result_mode != shift_mode)
11171 /* We only change the modes of scalar shifts. */
11172 x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11173 x, GET_MODE_MASK (result_mode) >> orig_count);
11175 /* Do the remainder of the processing in RESULT_MODE. */
11176 x = gen_lowpart_or_truncate (result_mode, x);
11178 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11179 operation. */
11180 if (complement_p)
11181 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11183 if (outer_op != UNKNOWN)
11185 int_result_mode = as_a <scalar_int_mode> (result_mode);
11187 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11188 && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11189 outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11191 if (outer_op == AND)
11192 x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11193 else if (outer_op == SET)
11195 /* This means that we have determined that the result is
11196 equivalent to a constant. This should be rare. */
11197 if (!side_effects_p (x))
11198 x = GEN_INT (outer_const);
11200 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11201 x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11202 else
11203 x = simplify_gen_binary (outer_op, int_result_mode, x,
11204 GEN_INT (outer_const));
11207 return x;
11210 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11211 The result of the shift is RESULT_MODE. If we cannot simplify it,
11212 return X or, if it is NULL, synthesize the expression with
11213 simplify_gen_binary. Otherwise, return a simplified value.
11215 The shift is normally computed in the widest mode we find in VAROP, as
11216 long as it isn't a different number of words than RESULT_MODE. Exceptions
11217 are ASHIFTRT and ROTATE, which are always done in their original mode. */
11219 static rtx
11220 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11221 rtx varop, int count)
11223 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11224 if (tem)
11225 return tem;
11227 if (!x)
11228 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
11229 if (GET_MODE (x) != result_mode)
11230 x = gen_lowpart (result_mode, x);
11231 return x;
11235 /* A subroutine of recog_for_combine. See there for arguments and
11236 return value. */
11238 static int
11239 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11241 rtx pat = *pnewpat;
11242 rtx pat_without_clobbers;
11243 int insn_code_number;
11244 int num_clobbers_to_add = 0;
11245 int i;
11246 rtx notes = NULL_RTX;
11247 rtx old_notes, old_pat;
11248 int old_icode;
11250 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11251 we use to indicate that something didn't match. If we find such a
11252 thing, force rejection. */
11253 if (GET_CODE (pat) == PARALLEL)
11254 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11255 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11256 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11257 return -1;
11259 old_pat = PATTERN (insn);
11260 old_notes = REG_NOTES (insn);
11261 PATTERN (insn) = pat;
11262 REG_NOTES (insn) = NULL_RTX;
11264 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11265 if (dump_file && (dump_flags & TDF_DETAILS))
11267 if (insn_code_number < 0)
11268 fputs ("Failed to match this instruction:\n", dump_file);
11269 else
11270 fputs ("Successfully matched this instruction:\n", dump_file);
11271 print_rtl_single (dump_file, pat);
11274 /* If it isn't, there is the possibility that we previously had an insn
11275 that clobbered some register as a side effect, but the combined
11276 insn doesn't need to do that. So try once more without the clobbers
11277 unless this represents an ASM insn. */
11279 if (insn_code_number < 0 && ! check_asm_operands (pat)
11280 && GET_CODE (pat) == PARALLEL)
11282 int pos;
11284 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11285 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11287 if (i != pos)
11288 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11289 pos++;
11292 SUBST_INT (XVECLEN (pat, 0), pos);
11294 if (pos == 1)
11295 pat = XVECEXP (pat, 0, 0);
11297 PATTERN (insn) = pat;
11298 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11299 if (dump_file && (dump_flags & TDF_DETAILS))
11301 if (insn_code_number < 0)
11302 fputs ("Failed to match this instruction:\n", dump_file);
11303 else
11304 fputs ("Successfully matched this instruction:\n", dump_file);
11305 print_rtl_single (dump_file, pat);
11309 pat_without_clobbers = pat;
11311 PATTERN (insn) = old_pat;
11312 REG_NOTES (insn) = old_notes;
11314 /* Recognize all noop sets, these will be killed by followup pass. */
11315 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11316 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11318 /* If we had any clobbers to add, make a new pattern than contains
11319 them. Then check to make sure that all of them are dead. */
11320 if (num_clobbers_to_add)
11322 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11323 rtvec_alloc (GET_CODE (pat) == PARALLEL
11324 ? (XVECLEN (pat, 0)
11325 + num_clobbers_to_add)
11326 : num_clobbers_to_add + 1));
11328 if (GET_CODE (pat) == PARALLEL)
11329 for (i = 0; i < XVECLEN (pat, 0); i++)
11330 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11331 else
11332 XVECEXP (newpat, 0, 0) = pat;
11334 add_clobbers (newpat, insn_code_number);
11336 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11337 i < XVECLEN (newpat, 0); i++)
11339 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11340 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11341 return -1;
11342 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11344 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11345 notes = alloc_reg_note (REG_UNUSED,
11346 XEXP (XVECEXP (newpat, 0, i), 0), notes);
11349 pat = newpat;
11352 if (insn_code_number >= 0
11353 && insn_code_number != NOOP_MOVE_INSN_CODE)
11355 old_pat = PATTERN (insn);
11356 old_notes = REG_NOTES (insn);
11357 old_icode = INSN_CODE (insn);
11358 PATTERN (insn) = pat;
11359 REG_NOTES (insn) = notes;
11360 INSN_CODE (insn) = insn_code_number;
11362 /* Allow targets to reject combined insn. */
11363 if (!targetm.legitimate_combined_insn (insn))
11365 if (dump_file && (dump_flags & TDF_DETAILS))
11366 fputs ("Instruction not appropriate for target.",
11367 dump_file);
11369 /* Callers expect recog_for_combine to strip
11370 clobbers from the pattern on failure. */
11371 pat = pat_without_clobbers;
11372 notes = NULL_RTX;
11374 insn_code_number = -1;
11377 PATTERN (insn) = old_pat;
11378 REG_NOTES (insn) = old_notes;
11379 INSN_CODE (insn) = old_icode;
11382 *pnewpat = pat;
11383 *pnotes = notes;
11385 return insn_code_number;
11388 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11389 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11390 Return whether anything was so changed. */
11392 static bool
11393 change_zero_ext (rtx pat)
11395 bool changed = false;
11396 rtx *src = &SET_SRC (pat);
11398 subrtx_ptr_iterator::array_type array;
11399 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11401 rtx x = **iter;
11402 scalar_int_mode mode, inner_mode;
11403 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11404 continue;
11405 int size;
11407 if (GET_CODE (x) == ZERO_EXTRACT
11408 && CONST_INT_P (XEXP (x, 1))
11409 && CONST_INT_P (XEXP (x, 2))
11410 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11411 && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11413 size = INTVAL (XEXP (x, 1));
11415 int start = INTVAL (XEXP (x, 2));
11416 if (BITS_BIG_ENDIAN)
11417 start = GET_MODE_PRECISION (inner_mode) - size - start;
11419 if (start)
11420 x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0), GEN_INT (start));
11421 else
11422 x = XEXP (x, 0);
11423 if (mode != inner_mode)
11424 x = gen_lowpart_SUBREG (mode, x);
11426 else if (GET_CODE (x) == ZERO_EXTEND
11427 && GET_CODE (XEXP (x, 0)) == SUBREG
11428 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11429 && !paradoxical_subreg_p (XEXP (x, 0))
11430 && subreg_lowpart_p (XEXP (x, 0)))
11432 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11433 size = GET_MODE_PRECISION (inner_mode);
11434 x = SUBREG_REG (XEXP (x, 0));
11435 if (GET_MODE (x) != mode)
11436 x = gen_lowpart_SUBREG (mode, x);
11438 else if (GET_CODE (x) == ZERO_EXTEND
11439 && REG_P (XEXP (x, 0))
11440 && HARD_REGISTER_P (XEXP (x, 0))
11441 && can_change_dest_mode (XEXP (x, 0), 0, mode))
11443 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11444 size = GET_MODE_PRECISION (inner_mode);
11445 x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11447 else
11448 continue;
11450 if (!(GET_CODE (x) == LSHIFTRT
11451 && CONST_INT_P (XEXP (x, 1))
11452 && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11454 wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11455 x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11458 SUBST (**iter, x);
11459 changed = true;
11462 if (changed)
11463 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11464 maybe_swap_commutative_operands (**iter);
11466 rtx *dst = &SET_DEST (pat);
11467 scalar_int_mode mode;
11468 if (GET_CODE (*dst) == ZERO_EXTRACT
11469 && REG_P (XEXP (*dst, 0))
11470 && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11471 && CONST_INT_P (XEXP (*dst, 1))
11472 && CONST_INT_P (XEXP (*dst, 2)))
11474 rtx reg = XEXP (*dst, 0);
11475 int width = INTVAL (XEXP (*dst, 1));
11476 int offset = INTVAL (XEXP (*dst, 2));
11477 int reg_width = GET_MODE_PRECISION (mode);
11478 if (BITS_BIG_ENDIAN)
11479 offset = reg_width - width - offset;
11481 rtx x, y, z, w;
11482 wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11483 wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11484 x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11485 if (offset)
11486 y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11487 else
11488 y = SET_SRC (pat);
11489 z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11490 w = gen_rtx_IOR (mode, x, z);
11491 SUBST (SET_DEST (pat), reg);
11492 SUBST (SET_SRC (pat), w);
11494 changed = true;
11497 return changed;
11500 /* Like recog, but we receive the address of a pointer to a new pattern.
11501 We try to match the rtx that the pointer points to.
11502 If that fails, we may try to modify or replace the pattern,
11503 storing the replacement into the same pointer object.
11505 Modifications include deletion or addition of CLOBBERs. If the
11506 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11507 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11508 (and undo if that fails).
11510 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11511 the CLOBBERs are placed.
11513 The value is the final insn code from the pattern ultimately matched,
11514 or -1. */
11516 static int
11517 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11519 rtx pat = *pnewpat;
11520 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11521 if (insn_code_number >= 0 || check_asm_operands (pat))
11522 return insn_code_number;
11524 void *marker = get_undo_marker ();
11525 bool changed = false;
11527 if (GET_CODE (pat) == SET)
11528 changed = change_zero_ext (pat);
11529 else if (GET_CODE (pat) == PARALLEL)
11531 int i;
11532 for (i = 0; i < XVECLEN (pat, 0); i++)
11534 rtx set = XVECEXP (pat, 0, i);
11535 if (GET_CODE (set) == SET)
11536 changed |= change_zero_ext (set);
11540 if (changed)
11542 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11544 if (insn_code_number < 0)
11545 undo_to_marker (marker);
11548 return insn_code_number;
11551 /* Like gen_lowpart_general but for use by combine. In combine it
11552 is not possible to create any new pseudoregs. However, it is
11553 safe to create invalid memory addresses, because combine will
11554 try to recognize them and all they will do is make the combine
11555 attempt fail.
11557 If for some reason this cannot do its job, an rtx
11558 (clobber (const_int 0)) is returned.
11559 An insn containing that will not be recognized. */
11561 static rtx
11562 gen_lowpart_for_combine (machine_mode omode, rtx x)
11564 machine_mode imode = GET_MODE (x);
11565 unsigned int osize = GET_MODE_SIZE (omode);
11566 unsigned int isize = GET_MODE_SIZE (imode);
11567 rtx result;
11569 if (omode == imode)
11570 return x;
11572 /* We can only support MODE being wider than a word if X is a
11573 constant integer or has a mode the same size. */
11574 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11575 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11576 goto fail;
11578 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11579 won't know what to do. So we will strip off the SUBREG here and
11580 process normally. */
11581 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11583 x = SUBREG_REG (x);
11585 /* For use in case we fall down into the address adjustments
11586 further below, we need to adjust the known mode and size of
11587 x; imode and isize, since we just adjusted x. */
11588 imode = GET_MODE (x);
11590 if (imode == omode)
11591 return x;
11593 isize = GET_MODE_SIZE (imode);
11596 result = gen_lowpart_common (omode, x);
11598 if (result)
11599 return result;
11601 if (MEM_P (x))
11603 int offset = 0;
11605 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11606 address. */
11607 if (MEM_VOLATILE_P (x)
11608 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11609 goto fail;
11611 /* If we want to refer to something bigger than the original memref,
11612 generate a paradoxical subreg instead. That will force a reload
11613 of the original memref X. */
11614 if (paradoxical_subreg_p (omode, imode))
11615 return gen_rtx_SUBREG (omode, x, 0);
11617 if (WORDS_BIG_ENDIAN)
11618 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11620 /* Adjust the address so that the address-after-the-data is
11621 unchanged. */
11622 if (BYTES_BIG_ENDIAN)
11623 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11625 return adjust_address_nv (x, omode, offset);
11628 /* If X is a comparison operator, rewrite it in a new mode. This
11629 probably won't match, but may allow further simplifications. */
11630 else if (COMPARISON_P (x))
11631 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11633 /* If we couldn't simplify X any other way, just enclose it in a
11634 SUBREG. Normally, this SUBREG won't match, but some patterns may
11635 include an explicit SUBREG or we may simplify it further in combine. */
11636 else
11638 rtx res;
11640 if (imode == VOIDmode)
11642 imode = int_mode_for_mode (omode).require ();
11643 x = gen_lowpart_common (imode, x);
11644 if (x == NULL)
11645 goto fail;
11647 res = lowpart_subreg (omode, x, imode);
11648 if (res)
11649 return res;
11652 fail:
11653 return gen_rtx_CLOBBER (omode, const0_rtx);
11656 /* Try to simplify a comparison between OP0 and a constant OP1,
11657 where CODE is the comparison code that will be tested, into a
11658 (CODE OP0 const0_rtx) form.
11660 The result is a possibly different comparison code to use.
11661 *POP1 may be updated. */
11663 static enum rtx_code
11664 simplify_compare_const (enum rtx_code code, machine_mode mode,
11665 rtx op0, rtx *pop1)
11667 scalar_int_mode int_mode;
11668 HOST_WIDE_INT const_op = INTVAL (*pop1);
11670 /* Get the constant we are comparing against and turn off all bits
11671 not on in our mode. */
11672 if (mode != VOIDmode)
11673 const_op = trunc_int_for_mode (const_op, mode);
11675 /* If we are comparing against a constant power of two and the value
11676 being compared can only have that single bit nonzero (e.g., it was
11677 `and'ed with that bit), we can replace this with a comparison
11678 with zero. */
11679 if (const_op
11680 && (code == EQ || code == NE || code == GE || code == GEU
11681 || code == LT || code == LTU)
11682 && is_a <scalar_int_mode> (mode, &int_mode)
11683 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11684 && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11685 && (nonzero_bits (op0, int_mode)
11686 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11688 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11689 const_op = 0;
11692 /* Similarly, if we are comparing a value known to be either -1 or
11693 0 with -1, change it to the opposite comparison against zero. */
11694 if (const_op == -1
11695 && (code == EQ || code == NE || code == GT || code == LE
11696 || code == GEU || code == LTU)
11697 && is_a <scalar_int_mode> (mode, &int_mode)
11698 && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11700 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11701 const_op = 0;
11704 /* Do some canonicalizations based on the comparison code. We prefer
11705 comparisons against zero and then prefer equality comparisons.
11706 If we can reduce the size of a constant, we will do that too. */
11707 switch (code)
11709 case LT:
11710 /* < C is equivalent to <= (C - 1) */
11711 if (const_op > 0)
11713 const_op -= 1;
11714 code = LE;
11715 /* ... fall through to LE case below. */
11716 gcc_fallthrough ();
11718 else
11719 break;
11721 case LE:
11722 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11723 if (const_op < 0)
11725 const_op += 1;
11726 code = LT;
11729 /* If we are doing a <= 0 comparison on a value known to have
11730 a zero sign bit, we can replace this with == 0. */
11731 else if (const_op == 0
11732 && is_a <scalar_int_mode> (mode, &int_mode)
11733 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11734 && (nonzero_bits (op0, int_mode)
11735 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11736 == 0)
11737 code = EQ;
11738 break;
11740 case GE:
11741 /* >= C is equivalent to > (C - 1). */
11742 if (const_op > 0)
11744 const_op -= 1;
11745 code = GT;
11746 /* ... fall through to GT below. */
11747 gcc_fallthrough ();
11749 else
11750 break;
11752 case GT:
11753 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11754 if (const_op < 0)
11756 const_op += 1;
11757 code = GE;
11760 /* If we are doing a > 0 comparison on a value known to have
11761 a zero sign bit, we can replace this with != 0. */
11762 else if (const_op == 0
11763 && is_a <scalar_int_mode> (mode, &int_mode)
11764 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11765 && (nonzero_bits (op0, int_mode)
11766 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11767 == 0)
11768 code = NE;
11769 break;
11771 case LTU:
11772 /* < C is equivalent to <= (C - 1). */
11773 if (const_op > 0)
11775 const_op -= 1;
11776 code = LEU;
11777 /* ... fall through ... */
11779 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11780 else if (is_a <scalar_int_mode> (mode, &int_mode)
11781 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11782 && ((unsigned HOST_WIDE_INT) const_op
11783 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11785 const_op = 0;
11786 code = GE;
11787 break;
11789 else
11790 break;
11792 case LEU:
11793 /* unsigned <= 0 is equivalent to == 0 */
11794 if (const_op == 0)
11795 code = EQ;
11796 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11797 else if (is_a <scalar_int_mode> (mode, &int_mode)
11798 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11799 && ((unsigned HOST_WIDE_INT) const_op
11800 == ((HOST_WIDE_INT_1U
11801 << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11803 const_op = 0;
11804 code = GE;
11806 break;
11808 case GEU:
11809 /* >= C is equivalent to > (C - 1). */
11810 if (const_op > 1)
11812 const_op -= 1;
11813 code = GTU;
11814 /* ... fall through ... */
11817 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11818 else if (is_a <scalar_int_mode> (mode, &int_mode)
11819 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11820 && ((unsigned HOST_WIDE_INT) const_op
11821 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11823 const_op = 0;
11824 code = LT;
11825 break;
11827 else
11828 break;
11830 case GTU:
11831 /* unsigned > 0 is equivalent to != 0 */
11832 if (const_op == 0)
11833 code = NE;
11834 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11835 else if (is_a <scalar_int_mode> (mode, &int_mode)
11836 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11837 && ((unsigned HOST_WIDE_INT) const_op
11838 == (HOST_WIDE_INT_1U
11839 << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
11841 const_op = 0;
11842 code = LT;
11844 break;
11846 default:
11847 break;
11850 *pop1 = GEN_INT (const_op);
11851 return code;
11854 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11855 comparison code that will be tested.
11857 The result is a possibly different comparison code to use. *POP0 and
11858 *POP1 may be updated.
11860 It is possible that we might detect that a comparison is either always
11861 true or always false. However, we do not perform general constant
11862 folding in combine, so this knowledge isn't useful. Such tautologies
11863 should have been detected earlier. Hence we ignore all such cases. */
11865 static enum rtx_code
11866 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11868 rtx op0 = *pop0;
11869 rtx op1 = *pop1;
11870 rtx tem, tem1;
11871 int i;
11872 scalar_int_mode mode, inner_mode, tmode;
11873 opt_scalar_int_mode tmode_iter;
11875 /* Try a few ways of applying the same transformation to both operands. */
11876 while (1)
11878 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11879 so check specially. */
11880 if (!WORD_REGISTER_OPERATIONS
11881 && code != GTU && code != GEU && code != LTU && code != LEU
11882 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11883 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11884 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11885 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11886 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11887 && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
11888 && (is_a <scalar_int_mode>
11889 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
11890 && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
11891 && CONST_INT_P (XEXP (op0, 1))
11892 && XEXP (op0, 1) == XEXP (op1, 1)
11893 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11894 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11895 && (INTVAL (XEXP (op0, 1))
11896 == (GET_MODE_PRECISION (mode)
11897 - GET_MODE_PRECISION (inner_mode))))
11899 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11900 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11903 /* If both operands are the same constant shift, see if we can ignore the
11904 shift. We can if the shift is a rotate or if the bits shifted out of
11905 this shift are known to be zero for both inputs and if the type of
11906 comparison is compatible with the shift. */
11907 if (GET_CODE (op0) == GET_CODE (op1)
11908 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11909 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11910 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11911 && (code != GT && code != LT && code != GE && code != LE))
11912 || (GET_CODE (op0) == ASHIFTRT
11913 && (code != GTU && code != LTU
11914 && code != GEU && code != LEU)))
11915 && CONST_INT_P (XEXP (op0, 1))
11916 && INTVAL (XEXP (op0, 1)) >= 0
11917 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11918 && XEXP (op0, 1) == XEXP (op1, 1))
11920 machine_mode mode = GET_MODE (op0);
11921 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11922 int shift_count = INTVAL (XEXP (op0, 1));
11924 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11925 mask &= (mask >> shift_count) << shift_count;
11926 else if (GET_CODE (op0) == ASHIFT)
11927 mask = (mask & (mask << shift_count)) >> shift_count;
11929 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11930 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11931 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11932 else
11933 break;
11936 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11937 SUBREGs are of the same mode, and, in both cases, the AND would
11938 be redundant if the comparison was done in the narrower mode,
11939 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11940 and the operand's possibly nonzero bits are 0xffffff01; in that case
11941 if we only care about QImode, we don't need the AND). This case
11942 occurs if the output mode of an scc insn is not SImode and
11943 STORE_FLAG_VALUE == 1 (e.g., the 386).
11945 Similarly, check for a case where the AND's are ZERO_EXTEND
11946 operations from some narrower mode even though a SUBREG is not
11947 present. */
11949 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11950 && CONST_INT_P (XEXP (op0, 1))
11951 && CONST_INT_P (XEXP (op1, 1)))
11953 rtx inner_op0 = XEXP (op0, 0);
11954 rtx inner_op1 = XEXP (op1, 0);
11955 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11956 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11957 int changed = 0;
11959 if (paradoxical_subreg_p (inner_op0)
11960 && GET_CODE (inner_op1) == SUBREG
11961 && (GET_MODE (SUBREG_REG (inner_op0))
11962 == GET_MODE (SUBREG_REG (inner_op1)))
11963 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11964 <= HOST_BITS_PER_WIDE_INT)
11965 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11966 GET_MODE (SUBREG_REG (inner_op0)))))
11967 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11968 GET_MODE (SUBREG_REG (inner_op1))))))
11970 op0 = SUBREG_REG (inner_op0);
11971 op1 = SUBREG_REG (inner_op1);
11973 /* The resulting comparison is always unsigned since we masked
11974 off the original sign bit. */
11975 code = unsigned_condition (code);
11977 changed = 1;
11980 else if (c0 == c1)
11981 FOR_EACH_MODE_UNTIL (tmode,
11982 as_a <scalar_int_mode> (GET_MODE (op0)))
11983 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11985 op0 = gen_lowpart_or_truncate (tmode, inner_op0);
11986 op1 = gen_lowpart_or_truncate (tmode, inner_op1);
11987 code = unsigned_condition (code);
11988 changed = 1;
11989 break;
11992 if (! changed)
11993 break;
11996 /* If both operands are NOT, we can strip off the outer operation
11997 and adjust the comparison code for swapped operands; similarly for
11998 NEG, except that this must be an equality comparison. */
11999 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12000 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12001 && (code == EQ || code == NE)))
12002 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12004 else
12005 break;
12008 /* If the first operand is a constant, swap the operands and adjust the
12009 comparison code appropriately, but don't do this if the second operand
12010 is already a constant integer. */
12011 if (swap_commutative_operands_p (op0, op1))
12013 std::swap (op0, op1);
12014 code = swap_condition (code);
12017 /* We now enter a loop during which we will try to simplify the comparison.
12018 For the most part, we only are concerned with comparisons with zero,
12019 but some things may really be comparisons with zero but not start
12020 out looking that way. */
12022 while (CONST_INT_P (op1))
12024 machine_mode raw_mode = GET_MODE (op0);
12025 scalar_int_mode int_mode;
12026 int equality_comparison_p;
12027 int sign_bit_comparison_p;
12028 int unsigned_comparison_p;
12029 HOST_WIDE_INT const_op;
12031 /* We only want to handle integral modes. This catches VOIDmode,
12032 CCmode, and the floating-point modes. An exception is that we
12033 can handle VOIDmode if OP0 is a COMPARE or a comparison
12034 operation. */
12036 if (GET_MODE_CLASS (raw_mode) != MODE_INT
12037 && ! (raw_mode == VOIDmode
12038 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12039 break;
12041 /* Try to simplify the compare to constant, possibly changing the
12042 comparison op, and/or changing op1 to zero. */
12043 code = simplify_compare_const (code, raw_mode, op0, &op1);
12044 const_op = INTVAL (op1);
12046 /* Compute some predicates to simplify code below. */
12048 equality_comparison_p = (code == EQ || code == NE);
12049 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12050 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12051 || code == GEU);
12053 /* If this is a sign bit comparison and we can do arithmetic in
12054 MODE, say that we will only be needing the sign bit of OP0. */
12055 if (sign_bit_comparison_p
12056 && is_a <scalar_int_mode> (raw_mode, &int_mode)
12057 && HWI_COMPUTABLE_MODE_P (int_mode))
12058 op0 = force_to_mode (op0, int_mode,
12059 HOST_WIDE_INT_1U
12060 << (GET_MODE_PRECISION (int_mode) - 1),
12063 if (COMPARISON_P (op0))
12065 /* We can't do anything if OP0 is a condition code value, rather
12066 than an actual data value. */
12067 if (const_op != 0
12068 || CC0_P (XEXP (op0, 0))
12069 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12070 break;
12072 /* Get the two operands being compared. */
12073 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12074 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12075 else
12076 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12078 /* Check for the cases where we simply want the result of the
12079 earlier test or the opposite of that result. */
12080 if (code == NE || code == EQ
12081 || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12082 && (code == LT || code == GE)))
12084 enum rtx_code new_code;
12085 if (code == LT || code == NE)
12086 new_code = GET_CODE (op0);
12087 else
12088 new_code = reversed_comparison_code (op0, NULL);
12090 if (new_code != UNKNOWN)
12092 code = new_code;
12093 op0 = tem;
12094 op1 = tem1;
12095 continue;
12098 break;
12101 if (raw_mode == VOIDmode)
12102 break;
12103 scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12105 /* Now try cases based on the opcode of OP0. If none of the cases
12106 does a "continue", we exit this loop immediately after the
12107 switch. */
12109 unsigned int mode_width = GET_MODE_PRECISION (mode);
12110 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12111 switch (GET_CODE (op0))
12113 case ZERO_EXTRACT:
12114 /* If we are extracting a single bit from a variable position in
12115 a constant that has only a single bit set and are comparing it
12116 with zero, we can convert this into an equality comparison
12117 between the position and the location of the single bit. */
12118 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12119 have already reduced the shift count modulo the word size. */
12120 if (!SHIFT_COUNT_TRUNCATED
12121 && CONST_INT_P (XEXP (op0, 0))
12122 && XEXP (op0, 1) == const1_rtx
12123 && equality_comparison_p && const_op == 0
12124 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12126 if (BITS_BIG_ENDIAN)
12127 i = BITS_PER_WORD - 1 - i;
12129 op0 = XEXP (op0, 2);
12130 op1 = GEN_INT (i);
12131 const_op = i;
12133 /* Result is nonzero iff shift count is equal to I. */
12134 code = reverse_condition (code);
12135 continue;
12138 /* fall through */
12140 case SIGN_EXTRACT:
12141 tem = expand_compound_operation (op0);
12142 if (tem != op0)
12144 op0 = tem;
12145 continue;
12147 break;
12149 case NOT:
12150 /* If testing for equality, we can take the NOT of the constant. */
12151 if (equality_comparison_p
12152 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12154 op0 = XEXP (op0, 0);
12155 op1 = tem;
12156 continue;
12159 /* If just looking at the sign bit, reverse the sense of the
12160 comparison. */
12161 if (sign_bit_comparison_p)
12163 op0 = XEXP (op0, 0);
12164 code = (code == GE ? LT : GE);
12165 continue;
12167 break;
12169 case NEG:
12170 /* If testing for equality, we can take the NEG of the constant. */
12171 if (equality_comparison_p
12172 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12174 op0 = XEXP (op0, 0);
12175 op1 = tem;
12176 continue;
12179 /* The remaining cases only apply to comparisons with zero. */
12180 if (const_op != 0)
12181 break;
12183 /* When X is ABS or is known positive,
12184 (neg X) is < 0 if and only if X != 0. */
12186 if (sign_bit_comparison_p
12187 && (GET_CODE (XEXP (op0, 0)) == ABS
12188 || (mode_width <= HOST_BITS_PER_WIDE_INT
12189 && (nonzero_bits (XEXP (op0, 0), mode)
12190 & (HOST_WIDE_INT_1U << (mode_width - 1)))
12191 == 0)))
12193 op0 = XEXP (op0, 0);
12194 code = (code == LT ? NE : EQ);
12195 continue;
12198 /* If we have NEG of something whose two high-order bits are the
12199 same, we know that "(-a) < 0" is equivalent to "a > 0". */
12200 if (num_sign_bit_copies (op0, mode) >= 2)
12202 op0 = XEXP (op0, 0);
12203 code = swap_condition (code);
12204 continue;
12206 break;
12208 case ROTATE:
12209 /* If we are testing equality and our count is a constant, we
12210 can perform the inverse operation on our RHS. */
12211 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12212 && (tem = simplify_binary_operation (ROTATERT, mode,
12213 op1, XEXP (op0, 1))) != 0)
12215 op0 = XEXP (op0, 0);
12216 op1 = tem;
12217 continue;
12220 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12221 a particular bit. Convert it to an AND of a constant of that
12222 bit. This will be converted into a ZERO_EXTRACT. */
12223 if (const_op == 0 && sign_bit_comparison_p
12224 && CONST_INT_P (XEXP (op0, 1))
12225 && mode_width <= HOST_BITS_PER_WIDE_INT)
12227 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12228 (HOST_WIDE_INT_1U
12229 << (mode_width - 1
12230 - INTVAL (XEXP (op0, 1)))));
12231 code = (code == LT ? NE : EQ);
12232 continue;
12235 /* Fall through. */
12237 case ABS:
12238 /* ABS is ignorable inside an equality comparison with zero. */
12239 if (const_op == 0 && equality_comparison_p)
12241 op0 = XEXP (op0, 0);
12242 continue;
12244 break;
12246 case SIGN_EXTEND:
12247 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12248 (compare FOO CONST) if CONST fits in FOO's mode and we
12249 are either testing inequality or have an unsigned
12250 comparison with ZERO_EXTEND or a signed comparison with
12251 SIGN_EXTEND. But don't do it if we don't have a compare
12252 insn of the given mode, since we'd have to revert it
12253 later on, and then we wouldn't know whether to sign- or
12254 zero-extend. */
12255 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12256 && ! unsigned_comparison_p
12257 && HWI_COMPUTABLE_MODE_P (mode)
12258 && trunc_int_for_mode (const_op, mode) == const_op
12259 && have_insn_for (COMPARE, mode))
12261 op0 = XEXP (op0, 0);
12262 continue;
12264 break;
12266 case SUBREG:
12267 /* Check for the case where we are comparing A - C1 with C2, that is
12269 (subreg:MODE (plus (A) (-C1))) op (C2)
12271 with C1 a constant, and try to lift the SUBREG, i.e. to do the
12272 comparison in the wider mode. One of the following two conditions
12273 must be true in order for this to be valid:
12275 1. The mode extension results in the same bit pattern being added
12276 on both sides and the comparison is equality or unsigned. As
12277 C2 has been truncated to fit in MODE, the pattern can only be
12278 all 0s or all 1s.
12280 2. The mode extension results in the sign bit being copied on
12281 each side.
12283 The difficulty here is that we have predicates for A but not for
12284 (A - C1) so we need to check that C1 is within proper bounds so
12285 as to perturbate A as little as possible. */
12287 if (mode_width <= HOST_BITS_PER_WIDE_INT
12288 && subreg_lowpart_p (op0)
12289 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12290 &inner_mode)
12291 && GET_MODE_PRECISION (inner_mode) > mode_width
12292 && GET_CODE (SUBREG_REG (op0)) == PLUS
12293 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12295 rtx a = XEXP (SUBREG_REG (op0), 0);
12296 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12298 if ((c1 > 0
12299 && (unsigned HOST_WIDE_INT) c1
12300 < HOST_WIDE_INT_1U << (mode_width - 1)
12301 && (equality_comparison_p || unsigned_comparison_p)
12302 /* (A - C1) zero-extends if it is positive and sign-extends
12303 if it is negative, C2 both zero- and sign-extends. */
12304 && ((0 == (nonzero_bits (a, inner_mode)
12305 & ~GET_MODE_MASK (mode))
12306 && const_op >= 0)
12307 /* (A - C1) sign-extends if it is positive and 1-extends
12308 if it is negative, C2 both sign- and 1-extends. */
12309 || (num_sign_bit_copies (a, inner_mode)
12310 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12311 - mode_width)
12312 && const_op < 0)))
12313 || ((unsigned HOST_WIDE_INT) c1
12314 < HOST_WIDE_INT_1U << (mode_width - 2)
12315 /* (A - C1) always sign-extends, like C2. */
12316 && num_sign_bit_copies (a, inner_mode)
12317 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12318 - (mode_width - 1))))
12320 op0 = SUBREG_REG (op0);
12321 continue;
12325 /* If the inner mode is narrower and we are extracting the low part,
12326 we can treat the SUBREG as if it were a ZERO_EXTEND. */
12327 if (paradoxical_subreg_p (op0))
12329 else if (subreg_lowpart_p (op0)
12330 && GET_MODE_CLASS (mode) == MODE_INT
12331 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12332 && (code == NE || code == EQ)
12333 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12334 && !paradoxical_subreg_p (op0)
12335 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12336 & ~GET_MODE_MASK (mode)) == 0)
12338 /* Remove outer subregs that don't do anything. */
12339 tem = gen_lowpart (inner_mode, op1);
12341 if ((nonzero_bits (tem, inner_mode)
12342 & ~GET_MODE_MASK (mode)) == 0)
12344 op0 = SUBREG_REG (op0);
12345 op1 = tem;
12346 continue;
12348 break;
12350 else
12351 break;
12353 /* FALLTHROUGH */
12355 case ZERO_EXTEND:
12356 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12357 && (unsigned_comparison_p || equality_comparison_p)
12358 && HWI_COMPUTABLE_MODE_P (mode)
12359 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12360 && const_op >= 0
12361 && have_insn_for (COMPARE, mode))
12363 op0 = XEXP (op0, 0);
12364 continue;
12366 break;
12368 case PLUS:
12369 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12370 this for equality comparisons due to pathological cases involving
12371 overflows. */
12372 if (equality_comparison_p
12373 && 0 != (tem = simplify_binary_operation (MINUS, mode,
12374 op1, XEXP (op0, 1))))
12376 op0 = XEXP (op0, 0);
12377 op1 = tem;
12378 continue;
12381 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12382 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12383 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12385 op0 = XEXP (XEXP (op0, 0), 0);
12386 code = (code == LT ? EQ : NE);
12387 continue;
12389 break;
12391 case MINUS:
12392 /* We used to optimize signed comparisons against zero, but that
12393 was incorrect. Unsigned comparisons against zero (GTU, LEU)
12394 arrive here as equality comparisons, or (GEU, LTU) are
12395 optimized away. No need to special-case them. */
12397 /* (eq (minus A B) C) -> (eq A (plus B C)) or
12398 (eq B (minus A C)), whichever simplifies. We can only do
12399 this for equality comparisons due to pathological cases involving
12400 overflows. */
12401 if (equality_comparison_p
12402 && 0 != (tem = simplify_binary_operation (PLUS, mode,
12403 XEXP (op0, 1), op1)))
12405 op0 = XEXP (op0, 0);
12406 op1 = tem;
12407 continue;
12410 if (equality_comparison_p
12411 && 0 != (tem = simplify_binary_operation (MINUS, mode,
12412 XEXP (op0, 0), op1)))
12414 op0 = XEXP (op0, 1);
12415 op1 = tem;
12416 continue;
12419 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12420 of bits in X minus 1, is one iff X > 0. */
12421 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12422 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12423 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12424 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12426 op0 = XEXP (op0, 1);
12427 code = (code == GE ? LE : GT);
12428 continue;
12430 break;
12432 case XOR:
12433 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12434 if C is zero or B is a constant. */
12435 if (equality_comparison_p
12436 && 0 != (tem = simplify_binary_operation (XOR, mode,
12437 XEXP (op0, 1), op1)))
12439 op0 = XEXP (op0, 0);
12440 op1 = tem;
12441 continue;
12443 break;
12446 case IOR:
12447 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12448 iff X <= 0. */
12449 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12450 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12451 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12453 op0 = XEXP (op0, 1);
12454 code = (code == GE ? GT : LE);
12455 continue;
12457 break;
12459 case AND:
12460 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12461 will be converted to a ZERO_EXTRACT later. */
12462 if (const_op == 0 && equality_comparison_p
12463 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12464 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12466 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12467 XEXP (XEXP (op0, 0), 1));
12468 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12469 continue;
12472 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12473 zero and X is a comparison and C1 and C2 describe only bits set
12474 in STORE_FLAG_VALUE, we can compare with X. */
12475 if (const_op == 0 && equality_comparison_p
12476 && mode_width <= HOST_BITS_PER_WIDE_INT
12477 && CONST_INT_P (XEXP (op0, 1))
12478 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12479 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12480 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12481 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12483 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12484 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12485 if ((~STORE_FLAG_VALUE & mask) == 0
12486 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12487 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12488 && COMPARISON_P (tem))))
12490 op0 = XEXP (XEXP (op0, 0), 0);
12491 continue;
12495 /* If we are doing an equality comparison of an AND of a bit equal
12496 to the sign bit, replace this with a LT or GE comparison of
12497 the underlying value. */
12498 if (equality_comparison_p
12499 && const_op == 0
12500 && CONST_INT_P (XEXP (op0, 1))
12501 && mode_width <= HOST_BITS_PER_WIDE_INT
12502 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12503 == HOST_WIDE_INT_1U << (mode_width - 1)))
12505 op0 = XEXP (op0, 0);
12506 code = (code == EQ ? GE : LT);
12507 continue;
12510 /* If this AND operation is really a ZERO_EXTEND from a narrower
12511 mode, the constant fits within that mode, and this is either an
12512 equality or unsigned comparison, try to do this comparison in
12513 the narrower mode.
12515 Note that in:
12517 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12518 -> (ne:DI (reg:SI 4) (const_int 0))
12520 unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12521 known to hold a value of the required mode the
12522 transformation is invalid. */
12523 if ((equality_comparison_p || unsigned_comparison_p)
12524 && CONST_INT_P (XEXP (op0, 1))
12525 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12526 & GET_MODE_MASK (mode))
12527 + 1)) >= 0
12528 && const_op >> i == 0
12529 && int_mode_for_size (i, 1).exists (&tmode))
12531 op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12532 continue;
12535 /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12536 fits in both M1 and M2 and the SUBREG is either paradoxical
12537 or represents the low part, permute the SUBREG and the AND
12538 and try again. */
12539 if (GET_CODE (XEXP (op0, 0)) == SUBREG
12540 && CONST_INT_P (XEXP (op0, 1)))
12542 unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12543 /* Require an integral mode, to avoid creating something like
12544 (AND:SF ...). */
12545 if ((is_a <scalar_int_mode>
12546 (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12547 /* It is unsafe to commute the AND into the SUBREG if the
12548 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12549 not defined. As originally written the upper bits
12550 have a defined value due to the AND operation.
12551 However, if we commute the AND inside the SUBREG then
12552 they no longer have defined values and the meaning of
12553 the code has been changed.
12554 Also C1 should not change value in the smaller mode,
12555 see PR67028 (a positive C1 can become negative in the
12556 smaller mode, so that the AND does no longer mask the
12557 upper bits). */
12558 && ((WORD_REGISTER_OPERATIONS
12559 && mode_width > GET_MODE_PRECISION (tmode)
12560 && mode_width <= BITS_PER_WORD
12561 && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12562 || (mode_width <= GET_MODE_PRECISION (tmode)
12563 && subreg_lowpart_p (XEXP (op0, 0))))
12564 && mode_width <= HOST_BITS_PER_WIDE_INT
12565 && HWI_COMPUTABLE_MODE_P (tmode)
12566 && (c1 & ~mask) == 0
12567 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12568 && c1 != mask
12569 && c1 != GET_MODE_MASK (tmode))
12571 op0 = simplify_gen_binary (AND, tmode,
12572 SUBREG_REG (XEXP (op0, 0)),
12573 gen_int_mode (c1, tmode));
12574 op0 = gen_lowpart (mode, op0);
12575 continue;
12579 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12580 if (const_op == 0 && equality_comparison_p
12581 && XEXP (op0, 1) == const1_rtx
12582 && GET_CODE (XEXP (op0, 0)) == NOT)
12584 op0 = simplify_and_const_int (NULL_RTX, mode,
12585 XEXP (XEXP (op0, 0), 0), 1);
12586 code = (code == NE ? EQ : NE);
12587 continue;
12590 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12591 (eq (and (lshiftrt X) 1) 0).
12592 Also handle the case where (not X) is expressed using xor. */
12593 if (const_op == 0 && equality_comparison_p
12594 && XEXP (op0, 1) == const1_rtx
12595 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12597 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12598 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12600 if (GET_CODE (shift_op) == NOT
12601 || (GET_CODE (shift_op) == XOR
12602 && CONST_INT_P (XEXP (shift_op, 1))
12603 && CONST_INT_P (shift_count)
12604 && HWI_COMPUTABLE_MODE_P (mode)
12605 && (UINTVAL (XEXP (shift_op, 1))
12606 == HOST_WIDE_INT_1U
12607 << INTVAL (shift_count))))
12610 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12611 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12612 code = (code == NE ? EQ : NE);
12613 continue;
12616 break;
12618 case ASHIFT:
12619 /* If we have (compare (ashift FOO N) (const_int C)) and
12620 the high order N bits of FOO (N+1 if an inequality comparison)
12621 are known to be zero, we can do this by comparing FOO with C
12622 shifted right N bits so long as the low-order N bits of C are
12623 zero. */
12624 if (CONST_INT_P (XEXP (op0, 1))
12625 && INTVAL (XEXP (op0, 1)) >= 0
12626 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12627 < HOST_BITS_PER_WIDE_INT)
12628 && (((unsigned HOST_WIDE_INT) const_op
12629 & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12630 - 1)) == 0)
12631 && mode_width <= HOST_BITS_PER_WIDE_INT
12632 && (nonzero_bits (XEXP (op0, 0), mode)
12633 & ~(mask >> (INTVAL (XEXP (op0, 1))
12634 + ! equality_comparison_p))) == 0)
12636 /* We must perform a logical shift, not an arithmetic one,
12637 as we want the top N bits of C to be zero. */
12638 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12640 temp >>= INTVAL (XEXP (op0, 1));
12641 op1 = gen_int_mode (temp, mode);
12642 op0 = XEXP (op0, 0);
12643 continue;
12646 /* If we are doing a sign bit comparison, it means we are testing
12647 a particular bit. Convert it to the appropriate AND. */
12648 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12649 && mode_width <= HOST_BITS_PER_WIDE_INT)
12651 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12652 (HOST_WIDE_INT_1U
12653 << (mode_width - 1
12654 - INTVAL (XEXP (op0, 1)))));
12655 code = (code == LT ? NE : EQ);
12656 continue;
12659 /* If this an equality comparison with zero and we are shifting
12660 the low bit to the sign bit, we can convert this to an AND of the
12661 low-order bit. */
12662 if (const_op == 0 && equality_comparison_p
12663 && CONST_INT_P (XEXP (op0, 1))
12664 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12666 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12667 continue;
12669 break;
12671 case ASHIFTRT:
12672 /* If this is an equality comparison with zero, we can do this
12673 as a logical shift, which might be much simpler. */
12674 if (equality_comparison_p && const_op == 0
12675 && CONST_INT_P (XEXP (op0, 1)))
12677 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12678 XEXP (op0, 0),
12679 INTVAL (XEXP (op0, 1)));
12680 continue;
12683 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12684 do the comparison in a narrower mode. */
12685 if (! unsigned_comparison_p
12686 && CONST_INT_P (XEXP (op0, 1))
12687 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12688 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12689 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12690 .exists (&tmode))
12691 && (((unsigned HOST_WIDE_INT) const_op
12692 + (GET_MODE_MASK (tmode) >> 1) + 1)
12693 <= GET_MODE_MASK (tmode)))
12695 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12696 continue;
12699 /* Likewise if OP0 is a PLUS of a sign extension with a
12700 constant, which is usually represented with the PLUS
12701 between the shifts. */
12702 if (! unsigned_comparison_p
12703 && CONST_INT_P (XEXP (op0, 1))
12704 && GET_CODE (XEXP (op0, 0)) == PLUS
12705 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12706 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12707 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12708 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12709 .exists (&tmode))
12710 && (((unsigned HOST_WIDE_INT) const_op
12711 + (GET_MODE_MASK (tmode) >> 1) + 1)
12712 <= GET_MODE_MASK (tmode)))
12714 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12715 rtx add_const = XEXP (XEXP (op0, 0), 1);
12716 rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12717 add_const, XEXP (op0, 1));
12719 op0 = simplify_gen_binary (PLUS, tmode,
12720 gen_lowpart (tmode, inner),
12721 new_const);
12722 continue;
12725 /* FALLTHROUGH */
12726 case LSHIFTRT:
12727 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12728 the low order N bits of FOO are known to be zero, we can do this
12729 by comparing FOO with C shifted left N bits so long as no
12730 overflow occurs. Even if the low order N bits of FOO aren't known
12731 to be zero, if the comparison is >= or < we can use the same
12732 optimization and for > or <= by setting all the low
12733 order N bits in the comparison constant. */
12734 if (CONST_INT_P (XEXP (op0, 1))
12735 && INTVAL (XEXP (op0, 1)) > 0
12736 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12737 && mode_width <= HOST_BITS_PER_WIDE_INT
12738 && (((unsigned HOST_WIDE_INT) const_op
12739 + (GET_CODE (op0) != LSHIFTRT
12740 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12741 + 1)
12742 : 0))
12743 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12745 unsigned HOST_WIDE_INT low_bits
12746 = (nonzero_bits (XEXP (op0, 0), mode)
12747 & ((HOST_WIDE_INT_1U
12748 << INTVAL (XEXP (op0, 1))) - 1));
12749 if (low_bits == 0 || !equality_comparison_p)
12751 /* If the shift was logical, then we must make the condition
12752 unsigned. */
12753 if (GET_CODE (op0) == LSHIFTRT)
12754 code = unsigned_condition (code);
12756 const_op = (unsigned HOST_WIDE_INT) const_op
12757 << INTVAL (XEXP (op0, 1));
12758 if (low_bits != 0
12759 && (code == GT || code == GTU
12760 || code == LE || code == LEU))
12761 const_op
12762 |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12763 op1 = GEN_INT (const_op);
12764 op0 = XEXP (op0, 0);
12765 continue;
12769 /* If we are using this shift to extract just the sign bit, we
12770 can replace this with an LT or GE comparison. */
12771 if (const_op == 0
12772 && (equality_comparison_p || sign_bit_comparison_p)
12773 && CONST_INT_P (XEXP (op0, 1))
12774 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12776 op0 = XEXP (op0, 0);
12777 code = (code == NE || code == GT ? LT : GE);
12778 continue;
12780 break;
12782 default:
12783 break;
12786 break;
12789 /* Now make any compound operations involved in this comparison. Then,
12790 check for an outmost SUBREG on OP0 that is not doing anything or is
12791 paradoxical. The latter transformation must only be performed when
12792 it is known that the "extra" bits will be the same in op0 and op1 or
12793 that they don't matter. There are three cases to consider:
12795 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12796 care bits and we can assume they have any convenient value. So
12797 making the transformation is safe.
12799 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12800 In this case the upper bits of op0 are undefined. We should not make
12801 the simplification in that case as we do not know the contents of
12802 those bits.
12804 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12805 In that case we know those bits are zeros or ones. We must also be
12806 sure that they are the same as the upper bits of op1.
12808 We can never remove a SUBREG for a non-equality comparison because
12809 the sign bit is in a different place in the underlying object. */
12811 rtx_code op0_mco_code = SET;
12812 if (op1 == const0_rtx)
12813 op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12815 op0 = make_compound_operation (op0, op0_mco_code);
12816 op1 = make_compound_operation (op1, SET);
12818 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12819 && is_int_mode (GET_MODE (op0), &mode)
12820 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12821 && (code == NE || code == EQ))
12823 if (paradoxical_subreg_p (op0))
12825 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12826 implemented. */
12827 if (REG_P (SUBREG_REG (op0)))
12829 op0 = SUBREG_REG (op0);
12830 op1 = gen_lowpart (inner_mode, op1);
12833 else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12834 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12835 & ~GET_MODE_MASK (mode)) == 0)
12837 tem = gen_lowpart (inner_mode, op1);
12839 if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
12840 op0 = SUBREG_REG (op0), op1 = tem;
12844 /* We now do the opposite procedure: Some machines don't have compare
12845 insns in all modes. If OP0's mode is an integer mode smaller than a
12846 word and we can't do a compare in that mode, see if there is a larger
12847 mode for which we can do the compare. There are a number of cases in
12848 which we can use the wider mode. */
12850 if (is_int_mode (GET_MODE (op0), &mode)
12851 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12852 && ! have_insn_for (COMPARE, mode))
12853 FOR_EACH_WIDER_MODE (tmode_iter, mode)
12855 tmode = tmode_iter.require ();
12856 if (!HWI_COMPUTABLE_MODE_P (tmode))
12857 break;
12858 if (have_insn_for (COMPARE, tmode))
12860 int zero_extended;
12862 /* If this is a test for negative, we can make an explicit
12863 test of the sign bit. Test this first so we can use
12864 a paradoxical subreg to extend OP0. */
12866 if (op1 == const0_rtx && (code == LT || code == GE)
12867 && HWI_COMPUTABLE_MODE_P (mode))
12869 unsigned HOST_WIDE_INT sign
12870 = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
12871 op0 = simplify_gen_binary (AND, tmode,
12872 gen_lowpart (tmode, op0),
12873 gen_int_mode (sign, tmode));
12874 code = (code == LT) ? NE : EQ;
12875 break;
12878 /* If the only nonzero bits in OP0 and OP1 are those in the
12879 narrower mode and this is an equality or unsigned comparison,
12880 we can use the wider mode. Similarly for sign-extended
12881 values, in which case it is true for all comparisons. */
12882 zero_extended = ((code == EQ || code == NE
12883 || code == GEU || code == GTU
12884 || code == LEU || code == LTU)
12885 && (nonzero_bits (op0, tmode)
12886 & ~GET_MODE_MASK (mode)) == 0
12887 && ((CONST_INT_P (op1)
12888 || (nonzero_bits (op1, tmode)
12889 & ~GET_MODE_MASK (mode)) == 0)));
12891 if (zero_extended
12892 || ((num_sign_bit_copies (op0, tmode)
12893 > (unsigned int) (GET_MODE_PRECISION (tmode)
12894 - GET_MODE_PRECISION (mode)))
12895 && (num_sign_bit_copies (op1, tmode)
12896 > (unsigned int) (GET_MODE_PRECISION (tmode)
12897 - GET_MODE_PRECISION (mode)))))
12899 /* If OP0 is an AND and we don't have an AND in MODE either,
12900 make a new AND in the proper mode. */
12901 if (GET_CODE (op0) == AND
12902 && !have_insn_for (AND, mode))
12903 op0 = simplify_gen_binary (AND, tmode,
12904 gen_lowpart (tmode,
12905 XEXP (op0, 0)),
12906 gen_lowpart (tmode,
12907 XEXP (op0, 1)));
12908 else
12910 if (zero_extended)
12912 op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
12913 op0, mode);
12914 op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
12915 op1, mode);
12917 else
12919 op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
12920 op0, mode);
12921 op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
12922 op1, mode);
12924 break;
12930 /* We may have changed the comparison operands. Re-canonicalize. */
12931 if (swap_commutative_operands_p (op0, op1))
12933 std::swap (op0, op1);
12934 code = swap_condition (code);
12937 /* If this machine only supports a subset of valid comparisons, see if we
12938 can convert an unsupported one into a supported one. */
12939 target_canonicalize_comparison (&code, &op0, &op1, 0);
12941 *pop0 = op0;
12942 *pop1 = op1;
12944 return code;
12947 /* Utility function for record_value_for_reg. Count number of
12948 rtxs in X. */
12949 static int
12950 count_rtxs (rtx x)
12952 enum rtx_code code = GET_CODE (x);
12953 const char *fmt;
12954 int i, j, ret = 1;
12956 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12957 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12959 rtx x0 = XEXP (x, 0);
12960 rtx x1 = XEXP (x, 1);
12962 if (x0 == x1)
12963 return 1 + 2 * count_rtxs (x0);
12965 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12966 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12967 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12968 return 2 + 2 * count_rtxs (x0)
12969 + count_rtxs (x == XEXP (x1, 0)
12970 ? XEXP (x1, 1) : XEXP (x1, 0));
12972 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12973 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12974 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12975 return 2 + 2 * count_rtxs (x1)
12976 + count_rtxs (x == XEXP (x0, 0)
12977 ? XEXP (x0, 1) : XEXP (x0, 0));
12980 fmt = GET_RTX_FORMAT (code);
12981 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12982 if (fmt[i] == 'e')
12983 ret += count_rtxs (XEXP (x, i));
12984 else if (fmt[i] == 'E')
12985 for (j = 0; j < XVECLEN (x, i); j++)
12986 ret += count_rtxs (XVECEXP (x, i, j));
12988 return ret;
12991 /* Utility function for following routine. Called when X is part of a value
12992 being stored into last_set_value. Sets last_set_table_tick
12993 for each register mentioned. Similar to mention_regs in cse.c */
12995 static void
12996 update_table_tick (rtx x)
12998 enum rtx_code code = GET_CODE (x);
12999 const char *fmt = GET_RTX_FORMAT (code);
13000 int i, j;
13002 if (code == REG)
13004 unsigned int regno = REGNO (x);
13005 unsigned int endregno = END_REGNO (x);
13006 unsigned int r;
13008 for (r = regno; r < endregno; r++)
13010 reg_stat_type *rsp = &reg_stat[r];
13011 rsp->last_set_table_tick = label_tick;
13014 return;
13017 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13018 if (fmt[i] == 'e')
13020 /* Check for identical subexpressions. If x contains
13021 identical subexpression we only have to traverse one of
13022 them. */
13023 if (i == 0 && ARITHMETIC_P (x))
13025 /* Note that at this point x1 has already been
13026 processed. */
13027 rtx x0 = XEXP (x, 0);
13028 rtx x1 = XEXP (x, 1);
13030 /* If x0 and x1 are identical then there is no need to
13031 process x0. */
13032 if (x0 == x1)
13033 break;
13035 /* If x0 is identical to a subexpression of x1 then while
13036 processing x1, x0 has already been processed. Thus we
13037 are done with x. */
13038 if (ARITHMETIC_P (x1)
13039 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13040 break;
13042 /* If x1 is identical to a subexpression of x0 then we
13043 still have to process the rest of x0. */
13044 if (ARITHMETIC_P (x0)
13045 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13047 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13048 break;
13052 update_table_tick (XEXP (x, i));
13054 else if (fmt[i] == 'E')
13055 for (j = 0; j < XVECLEN (x, i); j++)
13056 update_table_tick (XVECEXP (x, i, j));
13059 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13060 are saying that the register is clobbered and we no longer know its
13061 value. If INSN is zero, don't update reg_stat[].last_set; this is
13062 only permitted with VALUE also zero and is used to invalidate the
13063 register. */
13065 static void
13066 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13068 unsigned int regno = REGNO (reg);
13069 unsigned int endregno = END_REGNO (reg);
13070 unsigned int i;
13071 reg_stat_type *rsp;
13073 /* If VALUE contains REG and we have a previous value for REG, substitute
13074 the previous value. */
13075 if (value && insn && reg_overlap_mentioned_p (reg, value))
13077 rtx tem;
13079 /* Set things up so get_last_value is allowed to see anything set up to
13080 our insn. */
13081 subst_low_luid = DF_INSN_LUID (insn);
13082 tem = get_last_value (reg);
13084 /* If TEM is simply a binary operation with two CLOBBERs as operands,
13085 it isn't going to be useful and will take a lot of time to process,
13086 so just use the CLOBBER. */
13088 if (tem)
13090 if (ARITHMETIC_P (tem)
13091 && GET_CODE (XEXP (tem, 0)) == CLOBBER
13092 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13093 tem = XEXP (tem, 0);
13094 else if (count_occurrences (value, reg, 1) >= 2)
13096 /* If there are two or more occurrences of REG in VALUE,
13097 prevent the value from growing too much. */
13098 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
13099 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13102 value = replace_rtx (copy_rtx (value), reg, tem);
13106 /* For each register modified, show we don't know its value, that
13107 we don't know about its bitwise content, that its value has been
13108 updated, and that we don't know the location of the death of the
13109 register. */
13110 for (i = regno; i < endregno; i++)
13112 rsp = &reg_stat[i];
13114 if (insn)
13115 rsp->last_set = insn;
13117 rsp->last_set_value = 0;
13118 rsp->last_set_mode = VOIDmode;
13119 rsp->last_set_nonzero_bits = 0;
13120 rsp->last_set_sign_bit_copies = 0;
13121 rsp->last_death = 0;
13122 rsp->truncated_to_mode = VOIDmode;
13125 /* Mark registers that are being referenced in this value. */
13126 if (value)
13127 update_table_tick (value);
13129 /* Now update the status of each register being set.
13130 If someone is using this register in this block, set this register
13131 to invalid since we will get confused between the two lives in this
13132 basic block. This makes using this register always invalid. In cse, we
13133 scan the table to invalidate all entries using this register, but this
13134 is too much work for us. */
13136 for (i = regno; i < endregno; i++)
13138 rsp = &reg_stat[i];
13139 rsp->last_set_label = label_tick;
13140 if (!insn
13141 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13142 rsp->last_set_invalid = 1;
13143 else
13144 rsp->last_set_invalid = 0;
13147 /* The value being assigned might refer to X (like in "x++;"). In that
13148 case, we must replace it with (clobber (const_int 0)) to prevent
13149 infinite loops. */
13150 rsp = &reg_stat[regno];
13151 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
13153 value = copy_rtx (value);
13154 if (!get_last_value_validate (&value, insn, label_tick, 1))
13155 value = 0;
13158 /* For the main register being modified, update the value, the mode, the
13159 nonzero bits, and the number of sign bit copies. */
13161 rsp->last_set_value = value;
13163 if (value)
13165 machine_mode mode = GET_MODE (reg);
13166 subst_low_luid = DF_INSN_LUID (insn);
13167 rsp->last_set_mode = mode;
13168 if (GET_MODE_CLASS (mode) == MODE_INT
13169 && HWI_COMPUTABLE_MODE_P (mode))
13170 mode = nonzero_bits_mode;
13171 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13172 rsp->last_set_sign_bit_copies
13173 = num_sign_bit_copies (value, GET_MODE (reg));
13177 /* Called via note_stores from record_dead_and_set_regs to handle one
13178 SET or CLOBBER in an insn. DATA is the instruction in which the
13179 set is occurring. */
13181 static void
13182 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13184 rtx_insn *record_dead_insn = (rtx_insn *) data;
13186 if (GET_CODE (dest) == SUBREG)
13187 dest = SUBREG_REG (dest);
13189 if (!record_dead_insn)
13191 if (REG_P (dest))
13192 record_value_for_reg (dest, NULL, NULL_RTX);
13193 return;
13196 if (REG_P (dest))
13198 /* If we are setting the whole register, we know its value. Otherwise
13199 show that we don't know the value. We can handle SUBREG in
13200 some cases. */
13201 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13202 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13203 else if (GET_CODE (setter) == SET
13204 && GET_CODE (SET_DEST (setter)) == SUBREG
13205 && SUBREG_REG (SET_DEST (setter)) == dest
13206 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
13207 && subreg_lowpart_p (SET_DEST (setter)))
13208 record_value_for_reg (dest, record_dead_insn,
13209 gen_lowpart (GET_MODE (dest),
13210 SET_SRC (setter)));
13211 else
13212 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13214 else if (MEM_P (dest)
13215 /* Ignore pushes, they clobber nothing. */
13216 && ! push_operand (dest, GET_MODE (dest)))
13217 mem_last_set = DF_INSN_LUID (record_dead_insn);
13220 /* Update the records of when each REG was most recently set or killed
13221 for the things done by INSN. This is the last thing done in processing
13222 INSN in the combiner loop.
13224 We update reg_stat[], in particular fields last_set, last_set_value,
13225 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13226 last_death, and also the similar information mem_last_set (which insn
13227 most recently modified memory) and last_call_luid (which insn was the
13228 most recent subroutine call). */
13230 static void
13231 record_dead_and_set_regs (rtx_insn *insn)
13233 rtx link;
13234 unsigned int i;
13236 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13238 if (REG_NOTE_KIND (link) == REG_DEAD
13239 && REG_P (XEXP (link, 0)))
13241 unsigned int regno = REGNO (XEXP (link, 0));
13242 unsigned int endregno = END_REGNO (XEXP (link, 0));
13244 for (i = regno; i < endregno; i++)
13246 reg_stat_type *rsp;
13248 rsp = &reg_stat[i];
13249 rsp->last_death = insn;
13252 else if (REG_NOTE_KIND (link) == REG_INC)
13253 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13256 if (CALL_P (insn))
13258 hard_reg_set_iterator hrsi;
13259 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
13261 reg_stat_type *rsp;
13263 rsp = &reg_stat[i];
13264 rsp->last_set_invalid = 1;
13265 rsp->last_set = insn;
13266 rsp->last_set_value = 0;
13267 rsp->last_set_mode = VOIDmode;
13268 rsp->last_set_nonzero_bits = 0;
13269 rsp->last_set_sign_bit_copies = 0;
13270 rsp->last_death = 0;
13271 rsp->truncated_to_mode = VOIDmode;
13274 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13276 /* We can't combine into a call pattern. Remember, though, that
13277 the return value register is set at this LUID. We could
13278 still replace a register with the return value from the
13279 wrong subroutine call! */
13280 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
13282 else
13283 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
13286 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13287 register present in the SUBREG, so for each such SUBREG go back and
13288 adjust nonzero and sign bit information of the registers that are
13289 known to have some zero/sign bits set.
13291 This is needed because when combine blows the SUBREGs away, the
13292 information on zero/sign bits is lost and further combines can be
13293 missed because of that. */
13295 static void
13296 record_promoted_value (rtx_insn *insn, rtx subreg)
13298 struct insn_link *links;
13299 rtx set;
13300 unsigned int regno = REGNO (SUBREG_REG (subreg));
13301 machine_mode mode = GET_MODE (subreg);
13303 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
13304 return;
13306 for (links = LOG_LINKS (insn); links;)
13308 reg_stat_type *rsp;
13310 insn = links->insn;
13311 set = single_set (insn);
13313 if (! set || !REG_P (SET_DEST (set))
13314 || REGNO (SET_DEST (set)) != regno
13315 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13317 links = links->next;
13318 continue;
13321 rsp = &reg_stat[regno];
13322 if (rsp->last_set == insn)
13324 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13325 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13328 if (REG_P (SET_SRC (set)))
13330 regno = REGNO (SET_SRC (set));
13331 links = LOG_LINKS (insn);
13333 else
13334 break;
13338 /* Check if X, a register, is known to contain a value already
13339 truncated to MODE. In this case we can use a subreg to refer to
13340 the truncated value even though in the generic case we would need
13341 an explicit truncation. */
13343 static bool
13344 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13346 reg_stat_type *rsp = &reg_stat[REGNO (x)];
13347 machine_mode truncated = rsp->truncated_to_mode;
13349 if (truncated == 0
13350 || rsp->truncation_label < label_tick_ebb_start)
13351 return false;
13352 if (!partial_subreg_p (mode, truncated))
13353 return true;
13354 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13355 return true;
13356 return false;
13359 /* If X is a hard reg or a subreg record the mode that the register is
13360 accessed in. For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13361 able to turn a truncate into a subreg using this information. Return true
13362 if traversing X is complete. */
13364 static bool
13365 record_truncated_value (rtx x)
13367 machine_mode truncated_mode;
13368 reg_stat_type *rsp;
13370 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13372 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13373 truncated_mode = GET_MODE (x);
13375 if (!partial_subreg_p (truncated_mode, original_mode))
13376 return true;
13378 truncated_mode = GET_MODE (x);
13379 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13380 return true;
13382 x = SUBREG_REG (x);
13384 /* ??? For hard-regs we now record everything. We might be able to
13385 optimize this using last_set_mode. */
13386 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13387 truncated_mode = GET_MODE (x);
13388 else
13389 return false;
13391 rsp = &reg_stat[REGNO (x)];
13392 if (rsp->truncated_to_mode == 0
13393 || rsp->truncation_label < label_tick_ebb_start
13394 || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13396 rsp->truncated_to_mode = truncated_mode;
13397 rsp->truncation_label = label_tick;
13400 return true;
13403 /* Callback for note_uses. Find hardregs and subregs of pseudos and
13404 the modes they are used in. This can help truning TRUNCATEs into
13405 SUBREGs. */
13407 static void
13408 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13410 subrtx_var_iterator::array_type array;
13411 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13412 if (record_truncated_value (*iter))
13413 iter.skip_subrtxes ();
13416 /* Scan X for promoted SUBREGs. For each one found,
13417 note what it implies to the registers used in it. */
13419 static void
13420 check_promoted_subreg (rtx_insn *insn, rtx x)
13422 if (GET_CODE (x) == SUBREG
13423 && SUBREG_PROMOTED_VAR_P (x)
13424 && REG_P (SUBREG_REG (x)))
13425 record_promoted_value (insn, x);
13426 else
13428 const char *format = GET_RTX_FORMAT (GET_CODE (x));
13429 int i, j;
13431 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13432 switch (format[i])
13434 case 'e':
13435 check_promoted_subreg (insn, XEXP (x, i));
13436 break;
13437 case 'V':
13438 case 'E':
13439 if (XVEC (x, i) != 0)
13440 for (j = 0; j < XVECLEN (x, i); j++)
13441 check_promoted_subreg (insn, XVECEXP (x, i, j));
13442 break;
13447 /* Verify that all the registers and memory references mentioned in *LOC are
13448 still valid. *LOC was part of a value set in INSN when label_tick was
13449 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
13450 the invalid references with (clobber (const_int 0)) and return 1. This
13451 replacement is useful because we often can get useful information about
13452 the form of a value (e.g., if it was produced by a shift that always
13453 produces -1 or 0) even though we don't know exactly what registers it
13454 was produced from. */
13456 static int
13457 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
13459 rtx x = *loc;
13460 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13461 int len = GET_RTX_LENGTH (GET_CODE (x));
13462 int i, j;
13464 if (REG_P (x))
13466 unsigned int regno = REGNO (x);
13467 unsigned int endregno = END_REGNO (x);
13468 unsigned int j;
13470 for (j = regno; j < endregno; j++)
13472 reg_stat_type *rsp = &reg_stat[j];
13473 if (rsp->last_set_invalid
13474 /* If this is a pseudo-register that was only set once and not
13475 live at the beginning of the function, it is always valid. */
13476 || (! (regno >= FIRST_PSEUDO_REGISTER
13477 && regno < reg_n_sets_max
13478 && REG_N_SETS (regno) == 1
13479 && (!REGNO_REG_SET_P
13480 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13481 regno)))
13482 && rsp->last_set_label > tick))
13484 if (replace)
13485 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13486 return replace;
13490 return 1;
13492 /* If this is a memory reference, make sure that there were no stores after
13493 it that might have clobbered the value. We don't have alias info, so we
13494 assume any store invalidates it. Moreover, we only have local UIDs, so
13495 we also assume that there were stores in the intervening basic blocks. */
13496 else if (MEM_P (x) && !MEM_READONLY_P (x)
13497 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13499 if (replace)
13500 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13501 return replace;
13504 for (i = 0; i < len; i++)
13506 if (fmt[i] == 'e')
13508 /* Check for identical subexpressions. If x contains
13509 identical subexpression we only have to traverse one of
13510 them. */
13511 if (i == 1 && ARITHMETIC_P (x))
13513 /* Note that at this point x0 has already been checked
13514 and found valid. */
13515 rtx x0 = XEXP (x, 0);
13516 rtx x1 = XEXP (x, 1);
13518 /* If x0 and x1 are identical then x is also valid. */
13519 if (x0 == x1)
13520 return 1;
13522 /* If x1 is identical to a subexpression of x0 then
13523 while checking x0, x1 has already been checked. Thus
13524 it is valid and so as x. */
13525 if (ARITHMETIC_P (x0)
13526 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13527 return 1;
13529 /* If x0 is identical to a subexpression of x1 then x is
13530 valid iff the rest of x1 is valid. */
13531 if (ARITHMETIC_P (x1)
13532 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13533 return
13534 get_last_value_validate (&XEXP (x1,
13535 x0 == XEXP (x1, 0) ? 1 : 0),
13536 insn, tick, replace);
13539 if (get_last_value_validate (&XEXP (x, i), insn, tick,
13540 replace) == 0)
13541 return 0;
13543 else if (fmt[i] == 'E')
13544 for (j = 0; j < XVECLEN (x, i); j++)
13545 if (get_last_value_validate (&XVECEXP (x, i, j),
13546 insn, tick, replace) == 0)
13547 return 0;
13550 /* If we haven't found a reason for it to be invalid, it is valid. */
13551 return 1;
13554 /* Get the last value assigned to X, if known. Some registers
13555 in the value may be replaced with (clobber (const_int 0)) if their value
13556 is known longer known reliably. */
13558 static rtx
13559 get_last_value (const_rtx x)
13561 unsigned int regno;
13562 rtx value;
13563 reg_stat_type *rsp;
13565 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13566 then convert it to the desired mode. If this is a paradoxical SUBREG,
13567 we cannot predict what values the "extra" bits might have. */
13568 if (GET_CODE (x) == SUBREG
13569 && subreg_lowpart_p (x)
13570 && !paradoxical_subreg_p (x)
13571 && (value = get_last_value (SUBREG_REG (x))) != 0)
13572 return gen_lowpart (GET_MODE (x), value);
13574 if (!REG_P (x))
13575 return 0;
13577 regno = REGNO (x);
13578 rsp = &reg_stat[regno];
13579 value = rsp->last_set_value;
13581 /* If we don't have a value, or if it isn't for this basic block and
13582 it's either a hard register, set more than once, or it's a live
13583 at the beginning of the function, return 0.
13585 Because if it's not live at the beginning of the function then the reg
13586 is always set before being used (is never used without being set).
13587 And, if it's set only once, and it's always set before use, then all
13588 uses must have the same last value, even if it's not from this basic
13589 block. */
13591 if (value == 0
13592 || (rsp->last_set_label < label_tick_ebb_start
13593 && (regno < FIRST_PSEUDO_REGISTER
13594 || regno >= reg_n_sets_max
13595 || REG_N_SETS (regno) != 1
13596 || REGNO_REG_SET_P
13597 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13598 return 0;
13600 /* If the value was set in a later insn than the ones we are processing,
13601 we can't use it even if the register was only set once. */
13602 if (rsp->last_set_label == label_tick
13603 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13604 return 0;
13606 /* If fewer bits were set than what we are asked for now, we cannot use
13607 the value. */
13608 if (GET_MODE_PRECISION (rsp->last_set_mode)
13609 < GET_MODE_PRECISION (GET_MODE (x)))
13610 return 0;
13612 /* If the value has all its registers valid, return it. */
13613 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13614 return value;
13616 /* Otherwise, make a copy and replace any invalid register with
13617 (clobber (const_int 0)). If that fails for some reason, return 0. */
13619 value = copy_rtx (value);
13620 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13621 return value;
13623 return 0;
13626 /* Return nonzero if expression X refers to a REG or to memory
13627 that is set in an instruction more recent than FROM_LUID. */
13629 static int
13630 use_crosses_set_p (const_rtx x, int from_luid)
13632 const char *fmt;
13633 int i;
13634 enum rtx_code code = GET_CODE (x);
13636 if (code == REG)
13638 unsigned int regno = REGNO (x);
13639 unsigned endreg = END_REGNO (x);
13641 #ifdef PUSH_ROUNDING
13642 /* Don't allow uses of the stack pointer to be moved,
13643 because we don't know whether the move crosses a push insn. */
13644 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13645 return 1;
13646 #endif
13647 for (; regno < endreg; regno++)
13649 reg_stat_type *rsp = &reg_stat[regno];
13650 if (rsp->last_set
13651 && rsp->last_set_label == label_tick
13652 && DF_INSN_LUID (rsp->last_set) > from_luid)
13653 return 1;
13655 return 0;
13658 if (code == MEM && mem_last_set > from_luid)
13659 return 1;
13661 fmt = GET_RTX_FORMAT (code);
13663 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13665 if (fmt[i] == 'E')
13667 int j;
13668 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13669 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13670 return 1;
13672 else if (fmt[i] == 'e'
13673 && use_crosses_set_p (XEXP (x, i), from_luid))
13674 return 1;
13676 return 0;
13679 /* Define three variables used for communication between the following
13680 routines. */
13682 static unsigned int reg_dead_regno, reg_dead_endregno;
13683 static int reg_dead_flag;
13685 /* Function called via note_stores from reg_dead_at_p.
13687 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13688 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13690 static void
13691 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13693 unsigned int regno, endregno;
13695 if (!REG_P (dest))
13696 return;
13698 regno = REGNO (dest);
13699 endregno = END_REGNO (dest);
13700 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13701 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13704 /* Return nonzero if REG is known to be dead at INSN.
13706 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13707 referencing REG, it is dead. If we hit a SET referencing REG, it is
13708 live. Otherwise, see if it is live or dead at the start of the basic
13709 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13710 must be assumed to be always live. */
13712 static int
13713 reg_dead_at_p (rtx reg, rtx_insn *insn)
13715 basic_block block;
13716 unsigned int i;
13718 /* Set variables for reg_dead_at_p_1. */
13719 reg_dead_regno = REGNO (reg);
13720 reg_dead_endregno = END_REGNO (reg);
13722 reg_dead_flag = 0;
13724 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13725 we allow the machine description to decide whether use-and-clobber
13726 patterns are OK. */
13727 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13729 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13730 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13731 return 0;
13734 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13735 beginning of basic block. */
13736 block = BLOCK_FOR_INSN (insn);
13737 for (;;)
13739 if (INSN_P (insn))
13741 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13742 return 1;
13744 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13745 if (reg_dead_flag)
13746 return reg_dead_flag == 1 ? 1 : 0;
13748 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13749 return 1;
13752 if (insn == BB_HEAD (block))
13753 break;
13755 insn = PREV_INSN (insn);
13758 /* Look at live-in sets for the basic block that we were in. */
13759 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13760 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13761 return 0;
13763 return 1;
13766 /* Note hard registers in X that are used. */
13768 static void
13769 mark_used_regs_combine (rtx x)
13771 RTX_CODE code = GET_CODE (x);
13772 unsigned int regno;
13773 int i;
13775 switch (code)
13777 case LABEL_REF:
13778 case SYMBOL_REF:
13779 case CONST:
13780 CASE_CONST_ANY:
13781 case PC:
13782 case ADDR_VEC:
13783 case ADDR_DIFF_VEC:
13784 case ASM_INPUT:
13785 /* CC0 must die in the insn after it is set, so we don't need to take
13786 special note of it here. */
13787 case CC0:
13788 return;
13790 case CLOBBER:
13791 /* If we are clobbering a MEM, mark any hard registers inside the
13792 address as used. */
13793 if (MEM_P (XEXP (x, 0)))
13794 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13795 return;
13797 case REG:
13798 regno = REGNO (x);
13799 /* A hard reg in a wide mode may really be multiple registers.
13800 If so, mark all of them just like the first. */
13801 if (regno < FIRST_PSEUDO_REGISTER)
13803 /* None of this applies to the stack, frame or arg pointers. */
13804 if (regno == STACK_POINTER_REGNUM
13805 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13806 && regno == HARD_FRAME_POINTER_REGNUM)
13807 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13808 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13809 || regno == FRAME_POINTER_REGNUM)
13810 return;
13812 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13814 return;
13816 case SET:
13818 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13819 the address. */
13820 rtx testreg = SET_DEST (x);
13822 while (GET_CODE (testreg) == SUBREG
13823 || GET_CODE (testreg) == ZERO_EXTRACT
13824 || GET_CODE (testreg) == STRICT_LOW_PART)
13825 testreg = XEXP (testreg, 0);
13827 if (MEM_P (testreg))
13828 mark_used_regs_combine (XEXP (testreg, 0));
13830 mark_used_regs_combine (SET_SRC (x));
13832 return;
13834 default:
13835 break;
13838 /* Recursively scan the operands of this expression. */
13841 const char *fmt = GET_RTX_FORMAT (code);
13843 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13845 if (fmt[i] == 'e')
13846 mark_used_regs_combine (XEXP (x, i));
13847 else if (fmt[i] == 'E')
13849 int j;
13851 for (j = 0; j < XVECLEN (x, i); j++)
13852 mark_used_regs_combine (XVECEXP (x, i, j));
13858 /* Remove register number REGNO from the dead registers list of INSN.
13860 Return the note used to record the death, if there was one. */
13863 remove_death (unsigned int regno, rtx_insn *insn)
13865 rtx note = find_regno_note (insn, REG_DEAD, regno);
13867 if (note)
13868 remove_note (insn, note);
13870 return note;
13873 /* For each register (hardware or pseudo) used within expression X, if its
13874 death is in an instruction with luid between FROM_LUID (inclusive) and
13875 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13876 list headed by PNOTES.
13878 That said, don't move registers killed by maybe_kill_insn.
13880 This is done when X is being merged by combination into TO_INSN. These
13881 notes will then be distributed as needed. */
13883 static void
13884 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13885 rtx *pnotes)
13887 const char *fmt;
13888 int len, i;
13889 enum rtx_code code = GET_CODE (x);
13891 if (code == REG)
13893 unsigned int regno = REGNO (x);
13894 rtx_insn *where_dead = reg_stat[regno].last_death;
13896 /* Don't move the register if it gets killed in between from and to. */
13897 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13898 && ! reg_referenced_p (x, maybe_kill_insn))
13899 return;
13901 if (where_dead
13902 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13903 && DF_INSN_LUID (where_dead) >= from_luid
13904 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13906 rtx note = remove_death (regno, where_dead);
13908 /* It is possible for the call above to return 0. This can occur
13909 when last_death points to I2 or I1 that we combined with.
13910 In that case make a new note.
13912 We must also check for the case where X is a hard register
13913 and NOTE is a death note for a range of hard registers
13914 including X. In that case, we must put REG_DEAD notes for
13915 the remaining registers in place of NOTE. */
13917 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13918 && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
13920 unsigned int deadregno = REGNO (XEXP (note, 0));
13921 unsigned int deadend = END_REGNO (XEXP (note, 0));
13922 unsigned int ourend = END_REGNO (x);
13923 unsigned int i;
13925 for (i = deadregno; i < deadend; i++)
13926 if (i < regno || i >= ourend)
13927 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13930 /* If we didn't find any note, or if we found a REG_DEAD note that
13931 covers only part of the given reg, and we have a multi-reg hard
13932 register, then to be safe we must check for REG_DEAD notes
13933 for each register other than the first. They could have
13934 their own REG_DEAD notes lying around. */
13935 else if ((note == 0
13936 || (note != 0
13937 && partial_subreg_p (GET_MODE (XEXP (note, 0)),
13938 GET_MODE (x))))
13939 && regno < FIRST_PSEUDO_REGISTER
13940 && REG_NREGS (x) > 1)
13942 unsigned int ourend = END_REGNO (x);
13943 unsigned int i, offset;
13944 rtx oldnotes = 0;
13946 if (note)
13947 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
13948 else
13949 offset = 1;
13951 for (i = regno + offset; i < ourend; i++)
13952 move_deaths (regno_reg_rtx[i],
13953 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13956 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13958 XEXP (note, 1) = *pnotes;
13959 *pnotes = note;
13961 else
13962 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13965 return;
13968 else if (GET_CODE (x) == SET)
13970 rtx dest = SET_DEST (x);
13972 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13974 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13975 that accesses one word of a multi-word item, some
13976 piece of everything register in the expression is used by
13977 this insn, so remove any old death. */
13978 /* ??? So why do we test for equality of the sizes? */
13980 if (GET_CODE (dest) == ZERO_EXTRACT
13981 || GET_CODE (dest) == STRICT_LOW_PART
13982 || (GET_CODE (dest) == SUBREG
13983 && (((GET_MODE_SIZE (GET_MODE (dest))
13984 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13985 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13986 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13988 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13989 return;
13992 /* If this is some other SUBREG, we know it replaces the entire
13993 value, so use that as the destination. */
13994 if (GET_CODE (dest) == SUBREG)
13995 dest = SUBREG_REG (dest);
13997 /* If this is a MEM, adjust deaths of anything used in the address.
13998 For a REG (the only other possibility), the entire value is
13999 being replaced so the old value is not used in this insn. */
14001 if (MEM_P (dest))
14002 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14003 to_insn, pnotes);
14004 return;
14007 else if (GET_CODE (x) == CLOBBER)
14008 return;
14010 len = GET_RTX_LENGTH (code);
14011 fmt = GET_RTX_FORMAT (code);
14013 for (i = 0; i < len; i++)
14015 if (fmt[i] == 'E')
14017 int j;
14018 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14019 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14020 to_insn, pnotes);
14022 else if (fmt[i] == 'e')
14023 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14027 /* Return 1 if X is the target of a bit-field assignment in BODY, the
14028 pattern of an insn. X must be a REG. */
14030 static int
14031 reg_bitfield_target_p (rtx x, rtx body)
14033 int i;
14035 if (GET_CODE (body) == SET)
14037 rtx dest = SET_DEST (body);
14038 rtx target;
14039 unsigned int regno, tregno, endregno, endtregno;
14041 if (GET_CODE (dest) == ZERO_EXTRACT)
14042 target = XEXP (dest, 0);
14043 else if (GET_CODE (dest) == STRICT_LOW_PART)
14044 target = SUBREG_REG (XEXP (dest, 0));
14045 else
14046 return 0;
14048 if (GET_CODE (target) == SUBREG)
14049 target = SUBREG_REG (target);
14051 if (!REG_P (target))
14052 return 0;
14054 tregno = REGNO (target), regno = REGNO (x);
14055 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14056 return target == x;
14058 endtregno = end_hard_regno (GET_MODE (target), tregno);
14059 endregno = end_hard_regno (GET_MODE (x), regno);
14061 return endregno > tregno && regno < endtregno;
14064 else if (GET_CODE (body) == PARALLEL)
14065 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14066 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14067 return 1;
14069 return 0;
14072 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14073 as appropriate. I3 and I2 are the insns resulting from the combination
14074 insns including FROM (I2 may be zero).
14076 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14077 not need REG_DEAD notes because they are being substituted for. This
14078 saves searching in the most common cases.
14080 Each note in the list is either ignored or placed on some insns, depending
14081 on the type of note. */
14083 static void
14084 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14085 rtx elim_i2, rtx elim_i1, rtx elim_i0)
14087 rtx note, next_note;
14088 rtx tem_note;
14089 rtx_insn *tem_insn;
14091 for (note = notes; note; note = next_note)
14093 rtx_insn *place = 0, *place2 = 0;
14095 next_note = XEXP (note, 1);
14096 switch (REG_NOTE_KIND (note))
14098 case REG_BR_PROB:
14099 case REG_BR_PRED:
14100 /* Doesn't matter much where we put this, as long as it's somewhere.
14101 It is preferable to keep these notes on branches, which is most
14102 likely to be i3. */
14103 place = i3;
14104 break;
14106 case REG_NON_LOCAL_GOTO:
14107 if (JUMP_P (i3))
14108 place = i3;
14109 else
14111 gcc_assert (i2 && JUMP_P (i2));
14112 place = i2;
14114 break;
14116 case REG_EH_REGION:
14117 /* These notes must remain with the call or trapping instruction. */
14118 if (CALL_P (i3))
14119 place = i3;
14120 else if (i2 && CALL_P (i2))
14121 place = i2;
14122 else
14124 gcc_assert (cfun->can_throw_non_call_exceptions);
14125 if (may_trap_p (i3))
14126 place = i3;
14127 else if (i2 && may_trap_p (i2))
14128 place = i2;
14129 /* ??? Otherwise assume we've combined things such that we
14130 can now prove that the instructions can't trap. Drop the
14131 note in this case. */
14133 break;
14135 case REG_ARGS_SIZE:
14136 /* ??? How to distribute between i3-i1. Assume i3 contains the
14137 entire adjustment. Assert i3 contains at least some adjust. */
14138 if (!noop_move_p (i3))
14140 int old_size, args_size = INTVAL (XEXP (note, 0));
14141 /* fixup_args_size_notes looks at REG_NORETURN note,
14142 so ensure the note is placed there first. */
14143 if (CALL_P (i3))
14145 rtx *np;
14146 for (np = &next_note; *np; np = &XEXP (*np, 1))
14147 if (REG_NOTE_KIND (*np) == REG_NORETURN)
14149 rtx n = *np;
14150 *np = XEXP (n, 1);
14151 XEXP (n, 1) = REG_NOTES (i3);
14152 REG_NOTES (i3) = n;
14153 break;
14156 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14157 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14158 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14159 gcc_assert (old_size != args_size
14160 || (CALL_P (i3)
14161 && !ACCUMULATE_OUTGOING_ARGS
14162 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14164 break;
14166 case REG_NORETURN:
14167 case REG_SETJMP:
14168 case REG_TM:
14169 case REG_CALL_DECL:
14170 /* These notes must remain with the call. It should not be
14171 possible for both I2 and I3 to be a call. */
14172 if (CALL_P (i3))
14173 place = i3;
14174 else
14176 gcc_assert (i2 && CALL_P (i2));
14177 place = i2;
14179 break;
14181 case REG_UNUSED:
14182 /* Any clobbers for i3 may still exist, and so we must process
14183 REG_UNUSED notes from that insn.
14185 Any clobbers from i2 or i1 can only exist if they were added by
14186 recog_for_combine. In that case, recog_for_combine created the
14187 necessary REG_UNUSED notes. Trying to keep any original
14188 REG_UNUSED notes from these insns can cause incorrect output
14189 if it is for the same register as the original i3 dest.
14190 In that case, we will notice that the register is set in i3,
14191 and then add a REG_UNUSED note for the destination of i3, which
14192 is wrong. However, it is possible to have REG_UNUSED notes from
14193 i2 or i1 for register which were both used and clobbered, so
14194 we keep notes from i2 or i1 if they will turn into REG_DEAD
14195 notes. */
14197 /* If this register is set or clobbered in I3, put the note there
14198 unless there is one already. */
14199 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14201 if (from_insn != i3)
14202 break;
14204 if (! (REG_P (XEXP (note, 0))
14205 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14206 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14207 place = i3;
14209 /* Otherwise, if this register is used by I3, then this register
14210 now dies here, so we must put a REG_DEAD note here unless there
14211 is one already. */
14212 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14213 && ! (REG_P (XEXP (note, 0))
14214 ? find_regno_note (i3, REG_DEAD,
14215 REGNO (XEXP (note, 0)))
14216 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14218 PUT_REG_NOTE_KIND (note, REG_DEAD);
14219 place = i3;
14221 break;
14223 case REG_EQUAL:
14224 case REG_EQUIV:
14225 case REG_NOALIAS:
14226 /* These notes say something about results of an insn. We can
14227 only support them if they used to be on I3 in which case they
14228 remain on I3. Otherwise they are ignored.
14230 If the note refers to an expression that is not a constant, we
14231 must also ignore the note since we cannot tell whether the
14232 equivalence is still true. It might be possible to do
14233 slightly better than this (we only have a problem if I2DEST
14234 or I1DEST is present in the expression), but it doesn't
14235 seem worth the trouble. */
14237 if (from_insn == i3
14238 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14239 place = i3;
14240 break;
14242 case REG_INC:
14243 /* These notes say something about how a register is used. They must
14244 be present on any use of the register in I2 or I3. */
14245 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14246 place = i3;
14248 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14250 if (place)
14251 place2 = i2;
14252 else
14253 place = i2;
14255 break;
14257 case REG_LABEL_TARGET:
14258 case REG_LABEL_OPERAND:
14259 /* This can show up in several ways -- either directly in the
14260 pattern, or hidden off in the constant pool with (or without?)
14261 a REG_EQUAL note. */
14262 /* ??? Ignore the without-reg_equal-note problem for now. */
14263 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14264 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14265 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14266 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14267 place = i3;
14269 if (i2
14270 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14271 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14272 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14273 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14275 if (place)
14276 place2 = i2;
14277 else
14278 place = i2;
14281 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14282 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14283 there. */
14284 if (place && JUMP_P (place)
14285 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14286 && (JUMP_LABEL (place) == NULL
14287 || JUMP_LABEL (place) == XEXP (note, 0)))
14289 rtx label = JUMP_LABEL (place);
14291 if (!label)
14292 JUMP_LABEL (place) = XEXP (note, 0);
14293 else if (LABEL_P (label))
14294 LABEL_NUSES (label)--;
14297 if (place2 && JUMP_P (place2)
14298 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14299 && (JUMP_LABEL (place2) == NULL
14300 || JUMP_LABEL (place2) == XEXP (note, 0)))
14302 rtx label = JUMP_LABEL (place2);
14304 if (!label)
14305 JUMP_LABEL (place2) = XEXP (note, 0);
14306 else if (LABEL_P (label))
14307 LABEL_NUSES (label)--;
14308 place2 = 0;
14310 break;
14312 case REG_NONNEG:
14313 /* This note says something about the value of a register prior
14314 to the execution of an insn. It is too much trouble to see
14315 if the note is still correct in all situations. It is better
14316 to simply delete it. */
14317 break;
14319 case REG_DEAD:
14320 /* If we replaced the right hand side of FROM_INSN with a
14321 REG_EQUAL note, the original use of the dying register
14322 will not have been combined into I3 and I2. In such cases,
14323 FROM_INSN is guaranteed to be the first of the combined
14324 instructions, so we simply need to search back before
14325 FROM_INSN for the previous use or set of this register,
14326 then alter the notes there appropriately.
14328 If the register is used as an input in I3, it dies there.
14329 Similarly for I2, if it is nonzero and adjacent to I3.
14331 If the register is not used as an input in either I3 or I2
14332 and it is not one of the registers we were supposed to eliminate,
14333 there are two possibilities. We might have a non-adjacent I2
14334 or we might have somehow eliminated an additional register
14335 from a computation. For example, we might have had A & B where
14336 we discover that B will always be zero. In this case we will
14337 eliminate the reference to A.
14339 In both cases, we must search to see if we can find a previous
14340 use of A and put the death note there. */
14342 if (from_insn
14343 && from_insn == i2mod
14344 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14345 tem_insn = from_insn;
14346 else
14348 if (from_insn
14349 && CALL_P (from_insn)
14350 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14351 place = from_insn;
14352 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14353 place = i3;
14354 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14355 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14356 place = i2;
14357 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14358 && !(i2mod
14359 && reg_overlap_mentioned_p (XEXP (note, 0),
14360 i2mod_old_rhs)))
14361 || rtx_equal_p (XEXP (note, 0), elim_i1)
14362 || rtx_equal_p (XEXP (note, 0), elim_i0))
14363 break;
14364 tem_insn = i3;
14365 /* If the new I2 sets the same register that is marked dead
14366 in the note, we do not know where to put the note.
14367 Give up. */
14368 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14369 break;
14372 if (place == 0)
14374 basic_block bb = this_basic_block;
14376 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14378 if (!NONDEBUG_INSN_P (tem_insn))
14380 if (tem_insn == BB_HEAD (bb))
14381 break;
14382 continue;
14385 /* If the register is being set at TEM_INSN, see if that is all
14386 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14387 into a REG_UNUSED note instead. Don't delete sets to
14388 global register vars. */
14389 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14390 || !global_regs[REGNO (XEXP (note, 0))])
14391 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14393 rtx set = single_set (tem_insn);
14394 rtx inner_dest = 0;
14395 rtx_insn *cc0_setter = NULL;
14397 if (set != 0)
14398 for (inner_dest = SET_DEST (set);
14399 (GET_CODE (inner_dest) == STRICT_LOW_PART
14400 || GET_CODE (inner_dest) == SUBREG
14401 || GET_CODE (inner_dest) == ZERO_EXTRACT);
14402 inner_dest = XEXP (inner_dest, 0))
14405 /* Verify that it was the set, and not a clobber that
14406 modified the register.
14408 CC0 targets must be careful to maintain setter/user
14409 pairs. If we cannot delete the setter due to side
14410 effects, mark the user with an UNUSED note instead
14411 of deleting it. */
14413 if (set != 0 && ! side_effects_p (SET_SRC (set))
14414 && rtx_equal_p (XEXP (note, 0), inner_dest)
14415 && (!HAVE_cc0
14416 || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
14417 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
14418 && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
14420 /* Move the notes and links of TEM_INSN elsewhere.
14421 This might delete other dead insns recursively.
14422 First set the pattern to something that won't use
14423 any register. */
14424 rtx old_notes = REG_NOTES (tem_insn);
14426 PATTERN (tem_insn) = pc_rtx;
14427 REG_NOTES (tem_insn) = NULL;
14429 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14430 NULL_RTX, NULL_RTX, NULL_RTX);
14431 distribute_links (LOG_LINKS (tem_insn));
14433 unsigned int regno = REGNO (XEXP (note, 0));
14434 reg_stat_type *rsp = &reg_stat[regno];
14435 if (rsp->last_set == tem_insn)
14436 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14438 SET_INSN_DELETED (tem_insn);
14439 if (tem_insn == i2)
14440 i2 = NULL;
14442 /* Delete the setter too. */
14443 if (cc0_setter)
14445 PATTERN (cc0_setter) = pc_rtx;
14446 old_notes = REG_NOTES (cc0_setter);
14447 REG_NOTES (cc0_setter) = NULL;
14449 distribute_notes (old_notes, cc0_setter,
14450 cc0_setter, NULL,
14451 NULL_RTX, NULL_RTX, NULL_RTX);
14452 distribute_links (LOG_LINKS (cc0_setter));
14454 SET_INSN_DELETED (cc0_setter);
14455 if (cc0_setter == i2)
14456 i2 = NULL;
14459 else
14461 PUT_REG_NOTE_KIND (note, REG_UNUSED);
14463 /* If there isn't already a REG_UNUSED note, put one
14464 here. Do not place a REG_DEAD note, even if
14465 the register is also used here; that would not
14466 match the algorithm used in lifetime analysis
14467 and can cause the consistency check in the
14468 scheduler to fail. */
14469 if (! find_regno_note (tem_insn, REG_UNUSED,
14470 REGNO (XEXP (note, 0))))
14471 place = tem_insn;
14472 break;
14475 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14476 || (CALL_P (tem_insn)
14477 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14479 place = tem_insn;
14481 /* If we are doing a 3->2 combination, and we have a
14482 register which formerly died in i3 and was not used
14483 by i2, which now no longer dies in i3 and is used in
14484 i2 but does not die in i2, and place is between i2
14485 and i3, then we may need to move a link from place to
14486 i2. */
14487 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14488 && from_insn
14489 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14490 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14492 struct insn_link *links = LOG_LINKS (place);
14493 LOG_LINKS (place) = NULL;
14494 distribute_links (links);
14496 break;
14499 if (tem_insn == BB_HEAD (bb))
14500 break;
14505 /* If the register is set or already dead at PLACE, we needn't do
14506 anything with this note if it is still a REG_DEAD note.
14507 We check here if it is set at all, not if is it totally replaced,
14508 which is what `dead_or_set_p' checks, so also check for it being
14509 set partially. */
14511 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14513 unsigned int regno = REGNO (XEXP (note, 0));
14514 reg_stat_type *rsp = &reg_stat[regno];
14516 if (dead_or_set_p (place, XEXP (note, 0))
14517 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14519 /* Unless the register previously died in PLACE, clear
14520 last_death. [I no longer understand why this is
14521 being done.] */
14522 if (rsp->last_death != place)
14523 rsp->last_death = 0;
14524 place = 0;
14526 else
14527 rsp->last_death = place;
14529 /* If this is a death note for a hard reg that is occupying
14530 multiple registers, ensure that we are still using all
14531 parts of the object. If we find a piece of the object
14532 that is unused, we must arrange for an appropriate REG_DEAD
14533 note to be added for it. However, we can't just emit a USE
14534 and tag the note to it, since the register might actually
14535 be dead; so we recourse, and the recursive call then finds
14536 the previous insn that used this register. */
14538 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14540 unsigned int endregno = END_REGNO (XEXP (note, 0));
14541 bool all_used = true;
14542 unsigned int i;
14544 for (i = regno; i < endregno; i++)
14545 if ((! refers_to_regno_p (i, PATTERN (place))
14546 && ! find_regno_fusage (place, USE, i))
14547 || dead_or_set_regno_p (place, i))
14549 all_used = false;
14550 break;
14553 if (! all_used)
14555 /* Put only REG_DEAD notes for pieces that are
14556 not already dead or set. */
14558 for (i = regno; i < endregno;
14559 i += hard_regno_nregs (i, reg_raw_mode[i]))
14561 rtx piece = regno_reg_rtx[i];
14562 basic_block bb = this_basic_block;
14564 if (! dead_or_set_p (place, piece)
14565 && ! reg_bitfield_target_p (piece,
14566 PATTERN (place)))
14568 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14569 NULL_RTX);
14571 distribute_notes (new_note, place, place,
14572 NULL, NULL_RTX, NULL_RTX,
14573 NULL_RTX);
14575 else if (! refers_to_regno_p (i, PATTERN (place))
14576 && ! find_regno_fusage (place, USE, i))
14577 for (tem_insn = PREV_INSN (place); ;
14578 tem_insn = PREV_INSN (tem_insn))
14580 if (!NONDEBUG_INSN_P (tem_insn))
14582 if (tem_insn == BB_HEAD (bb))
14583 break;
14584 continue;
14586 if (dead_or_set_p (tem_insn, piece)
14587 || reg_bitfield_target_p (piece,
14588 PATTERN (tem_insn)))
14590 add_reg_note (tem_insn, REG_UNUSED, piece);
14591 break;
14596 place = 0;
14600 break;
14602 default:
14603 /* Any other notes should not be present at this point in the
14604 compilation. */
14605 gcc_unreachable ();
14608 if (place)
14610 XEXP (note, 1) = REG_NOTES (place);
14611 REG_NOTES (place) = note;
14614 if (place2)
14615 add_shallow_copy_of_reg_note (place2, note);
14619 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14620 I3, I2, and I1 to new locations. This is also called to add a link
14621 pointing at I3 when I3's destination is changed. */
14623 static void
14624 distribute_links (struct insn_link *links)
14626 struct insn_link *link, *next_link;
14628 for (link = links; link; link = next_link)
14630 rtx_insn *place = 0;
14631 rtx_insn *insn;
14632 rtx set, reg;
14634 next_link = link->next;
14636 /* If the insn that this link points to is a NOTE, ignore it. */
14637 if (NOTE_P (link->insn))
14638 continue;
14640 set = 0;
14641 rtx pat = PATTERN (link->insn);
14642 if (GET_CODE (pat) == SET)
14643 set = pat;
14644 else if (GET_CODE (pat) == PARALLEL)
14646 int i;
14647 for (i = 0; i < XVECLEN (pat, 0); i++)
14649 set = XVECEXP (pat, 0, i);
14650 if (GET_CODE (set) != SET)
14651 continue;
14653 reg = SET_DEST (set);
14654 while (GET_CODE (reg) == ZERO_EXTRACT
14655 || GET_CODE (reg) == STRICT_LOW_PART
14656 || GET_CODE (reg) == SUBREG)
14657 reg = XEXP (reg, 0);
14659 if (!REG_P (reg))
14660 continue;
14662 if (REGNO (reg) == link->regno)
14663 break;
14665 if (i == XVECLEN (pat, 0))
14666 continue;
14668 else
14669 continue;
14671 reg = SET_DEST (set);
14673 while (GET_CODE (reg) == ZERO_EXTRACT
14674 || GET_CODE (reg) == STRICT_LOW_PART
14675 || GET_CODE (reg) == SUBREG)
14676 reg = XEXP (reg, 0);
14678 /* A LOG_LINK is defined as being placed on the first insn that uses
14679 a register and points to the insn that sets the register. Start
14680 searching at the next insn after the target of the link and stop
14681 when we reach a set of the register or the end of the basic block.
14683 Note that this correctly handles the link that used to point from
14684 I3 to I2. Also note that not much searching is typically done here
14685 since most links don't point very far away. */
14687 for (insn = NEXT_INSN (link->insn);
14688 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14689 || BB_HEAD (this_basic_block->next_bb) != insn));
14690 insn = NEXT_INSN (insn))
14691 if (DEBUG_INSN_P (insn))
14692 continue;
14693 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14695 if (reg_referenced_p (reg, PATTERN (insn)))
14696 place = insn;
14697 break;
14699 else if (CALL_P (insn)
14700 && find_reg_fusage (insn, USE, reg))
14702 place = insn;
14703 break;
14705 else if (INSN_P (insn) && reg_set_p (reg, insn))
14706 break;
14708 /* If we found a place to put the link, place it there unless there
14709 is already a link to the same insn as LINK at that point. */
14711 if (place)
14713 struct insn_link *link2;
14715 FOR_EACH_LOG_LINK (link2, place)
14716 if (link2->insn == link->insn && link2->regno == link->regno)
14717 break;
14719 if (link2 == NULL)
14721 link->next = LOG_LINKS (place);
14722 LOG_LINKS (place) = link;
14724 /* Set added_links_insn to the earliest insn we added a
14725 link to. */
14726 if (added_links_insn == 0
14727 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14728 added_links_insn = place;
14734 /* Check for any register or memory mentioned in EQUIV that is not
14735 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14736 of EXPR where some registers may have been replaced by constants. */
14738 static bool
14739 unmentioned_reg_p (rtx equiv, rtx expr)
14741 subrtx_iterator::array_type array;
14742 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14744 const_rtx x = *iter;
14745 if ((REG_P (x) || MEM_P (x))
14746 && !reg_mentioned_p (x, expr))
14747 return true;
14749 return false;
14752 DEBUG_FUNCTION void
14753 dump_combine_stats (FILE *file)
14755 fprintf
14756 (file,
14757 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14758 combine_attempts, combine_merges, combine_extras, combine_successes);
14761 void
14762 dump_combine_total_stats (FILE *file)
14764 fprintf
14765 (file,
14766 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14767 total_attempts, total_merges, total_extras, total_successes);
14770 /* Try combining insns through substitution. */
14771 static unsigned int
14772 rest_of_handle_combine (void)
14774 int rebuild_jump_labels_after_combine;
14776 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14777 df_note_add_problem ();
14778 df_analyze ();
14780 regstat_init_n_sets_and_refs ();
14781 reg_n_sets_max = max_reg_num ();
14783 rebuild_jump_labels_after_combine
14784 = combine_instructions (get_insns (), max_reg_num ());
14786 /* Combining insns may have turned an indirect jump into a
14787 direct jump. Rebuild the JUMP_LABEL fields of jumping
14788 instructions. */
14789 if (rebuild_jump_labels_after_combine)
14791 if (dom_info_available_p (CDI_DOMINATORS))
14792 free_dominance_info (CDI_DOMINATORS);
14793 timevar_push (TV_JUMP);
14794 rebuild_jump_labels (get_insns ());
14795 cleanup_cfg (0);
14796 timevar_pop (TV_JUMP);
14799 regstat_free_n_sets_and_refs ();
14800 return 0;
14803 namespace {
14805 const pass_data pass_data_combine =
14807 RTL_PASS, /* type */
14808 "combine", /* name */
14809 OPTGROUP_NONE, /* optinfo_flags */
14810 TV_COMBINE, /* tv_id */
14811 PROP_cfglayout, /* properties_required */
14812 0, /* properties_provided */
14813 0, /* properties_destroyed */
14814 0, /* todo_flags_start */
14815 TODO_df_finish, /* todo_flags_finish */
14818 class pass_combine : public rtl_opt_pass
14820 public:
14821 pass_combine (gcc::context *ctxt)
14822 : rtl_opt_pass (pass_data_combine, ctxt)
14825 /* opt_pass methods: */
14826 virtual bool gate (function *) { return (optimize > 0); }
14827 virtual unsigned int execute (function *)
14829 return rest_of_handle_combine ();
14832 }; // class pass_combine
14834 } // anon namespace
14836 rtl_opt_pass *
14837 make_pass_combine (gcc::context *ctxt)
14839 return new pass_combine (ctxt);