PR82045: Avoid passing machine modes through "..."
[official-gcc.git] / gcc / combine.c
blobc748c92d2eda7f8a35de905a6d02fec43fe41986
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 machine_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 = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
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;
1235 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1236 last_call_luid = 0;
1237 mem_last_set = -1;
1239 label_tick++;
1240 if (!single_pred_p (this_basic_block)
1241 || single_pred (this_basic_block) != last_bb)
1242 label_tick_ebb_start = label_tick;
1243 last_bb = this_basic_block;
1245 rtl_profile_for_bb (this_basic_block);
1246 for (insn = BB_HEAD (this_basic_block);
1247 insn != NEXT_INSN (BB_END (this_basic_block));
1248 insn = next ? next : NEXT_INSN (insn))
1250 next = 0;
1251 if (!NONDEBUG_INSN_P (insn))
1252 continue;
1254 while (last_combined_insn
1255 && (!NONDEBUG_INSN_P (last_combined_insn)
1256 || last_combined_insn->deleted ()))
1257 last_combined_insn = PREV_INSN (last_combined_insn);
1258 if (last_combined_insn == NULL_RTX
1259 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1260 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1261 last_combined_insn = insn;
1263 /* See if we know about function return values before this
1264 insn based upon SUBREG flags. */
1265 check_promoted_subreg (insn, PATTERN (insn));
1267 /* See if we can find hardregs and subreg of pseudos in
1268 narrower modes. This could help turning TRUNCATEs
1269 into SUBREGs. */
1270 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1272 /* Try this insn with each insn it links back to. */
1274 FOR_EACH_LOG_LINK (links, insn)
1275 if ((next = try_combine (insn, links->insn, NULL,
1276 NULL, &new_direct_jump_p,
1277 last_combined_insn)) != 0)
1279 statistics_counter_event (cfun, "two-insn combine", 1);
1280 goto retry;
1283 /* Try each sequence of three linked insns ending with this one. */
1285 if (max_combine >= 3)
1286 FOR_EACH_LOG_LINK (links, insn)
1288 rtx_insn *link = links->insn;
1290 /* If the linked insn has been replaced by a note, then there
1291 is no point in pursuing this chain any further. */
1292 if (NOTE_P (link))
1293 continue;
1295 FOR_EACH_LOG_LINK (nextlinks, link)
1296 if ((next = try_combine (insn, link, nextlinks->insn,
1297 NULL, &new_direct_jump_p,
1298 last_combined_insn)) != 0)
1300 statistics_counter_event (cfun, "three-insn combine", 1);
1301 goto retry;
1305 /* Try to combine a jump insn that uses CC0
1306 with a preceding insn that sets CC0, and maybe with its
1307 logical predecessor as well.
1308 This is how we make decrement-and-branch insns.
1309 We need this special code because data flow connections
1310 via CC0 do not get entered in LOG_LINKS. */
1312 if (HAVE_cc0
1313 && JUMP_P (insn)
1314 && (prev = prev_nonnote_insn (insn)) != 0
1315 && NONJUMP_INSN_P (prev)
1316 && sets_cc0_p (PATTERN (prev)))
1318 if ((next = try_combine (insn, prev, NULL, NULL,
1319 &new_direct_jump_p,
1320 last_combined_insn)) != 0)
1321 goto retry;
1323 FOR_EACH_LOG_LINK (nextlinks, prev)
1324 if ((next = try_combine (insn, prev, nextlinks->insn,
1325 NULL, &new_direct_jump_p,
1326 last_combined_insn)) != 0)
1327 goto retry;
1330 /* Do the same for an insn that explicitly references CC0. */
1331 if (HAVE_cc0 && NONJUMP_INSN_P (insn)
1332 && (prev = prev_nonnote_insn (insn)) != 0
1333 && NONJUMP_INSN_P (prev)
1334 && sets_cc0_p (PATTERN (prev))
1335 && GET_CODE (PATTERN (insn)) == SET
1336 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1338 if ((next = try_combine (insn, prev, NULL, NULL,
1339 &new_direct_jump_p,
1340 last_combined_insn)) != 0)
1341 goto retry;
1343 FOR_EACH_LOG_LINK (nextlinks, prev)
1344 if ((next = try_combine (insn, prev, nextlinks->insn,
1345 NULL, &new_direct_jump_p,
1346 last_combined_insn)) != 0)
1347 goto retry;
1350 /* Finally, see if any of the insns that this insn links to
1351 explicitly references CC0. If so, try this insn, that insn,
1352 and its predecessor if it sets CC0. */
1353 if (HAVE_cc0)
1355 FOR_EACH_LOG_LINK (links, insn)
1356 if (NONJUMP_INSN_P (links->insn)
1357 && GET_CODE (PATTERN (links->insn)) == SET
1358 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1359 && (prev = prev_nonnote_insn (links->insn)) != 0
1360 && NONJUMP_INSN_P (prev)
1361 && sets_cc0_p (PATTERN (prev))
1362 && (next = try_combine (insn, links->insn,
1363 prev, NULL, &new_direct_jump_p,
1364 last_combined_insn)) != 0)
1365 goto retry;
1368 /* Try combining an insn with two different insns whose results it
1369 uses. */
1370 if (max_combine >= 3)
1371 FOR_EACH_LOG_LINK (links, insn)
1372 for (nextlinks = links->next; nextlinks;
1373 nextlinks = nextlinks->next)
1374 if ((next = try_combine (insn, links->insn,
1375 nextlinks->insn, NULL,
1376 &new_direct_jump_p,
1377 last_combined_insn)) != 0)
1380 statistics_counter_event (cfun, "three-insn combine", 1);
1381 goto retry;
1384 /* Try four-instruction combinations. */
1385 if (max_combine >= 4)
1386 FOR_EACH_LOG_LINK (links, insn)
1388 struct insn_link *next1;
1389 rtx_insn *link = links->insn;
1391 /* If the linked insn has been replaced by a note, then there
1392 is no point in pursuing this chain any further. */
1393 if (NOTE_P (link))
1394 continue;
1396 FOR_EACH_LOG_LINK (next1, link)
1398 rtx_insn *link1 = next1->insn;
1399 if (NOTE_P (link1))
1400 continue;
1401 /* I0 -> I1 -> I2 -> I3. */
1402 FOR_EACH_LOG_LINK (nextlinks, link1)
1403 if ((next = try_combine (insn, link, link1,
1404 nextlinks->insn,
1405 &new_direct_jump_p,
1406 last_combined_insn)) != 0)
1408 statistics_counter_event (cfun, "four-insn combine", 1);
1409 goto retry;
1411 /* I0, I1 -> I2, I2 -> I3. */
1412 for (nextlinks = next1->next; nextlinks;
1413 nextlinks = nextlinks->next)
1414 if ((next = try_combine (insn, link, link1,
1415 nextlinks->insn,
1416 &new_direct_jump_p,
1417 last_combined_insn)) != 0)
1419 statistics_counter_event (cfun, "four-insn combine", 1);
1420 goto retry;
1424 for (next1 = links->next; next1; next1 = next1->next)
1426 rtx_insn *link1 = next1->insn;
1427 if (NOTE_P (link1))
1428 continue;
1429 /* I0 -> I2; I1, I2 -> I3. */
1430 FOR_EACH_LOG_LINK (nextlinks, link)
1431 if ((next = try_combine (insn, link, link1,
1432 nextlinks->insn,
1433 &new_direct_jump_p,
1434 last_combined_insn)) != 0)
1436 statistics_counter_event (cfun, "four-insn combine", 1);
1437 goto retry;
1439 /* I0 -> I1; I1, I2 -> I3. */
1440 FOR_EACH_LOG_LINK (nextlinks, link1)
1441 if ((next = try_combine (insn, link, link1,
1442 nextlinks->insn,
1443 &new_direct_jump_p,
1444 last_combined_insn)) != 0)
1446 statistics_counter_event (cfun, "four-insn combine", 1);
1447 goto retry;
1452 /* Try this insn with each REG_EQUAL note it links back to. */
1453 FOR_EACH_LOG_LINK (links, insn)
1455 rtx set, note;
1456 rtx_insn *temp = links->insn;
1457 if ((set = single_set (temp)) != 0
1458 && (note = find_reg_equal_equiv_note (temp)) != 0
1459 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1460 /* Avoid using a register that may already been marked
1461 dead by an earlier instruction. */
1462 && ! unmentioned_reg_p (note, SET_SRC (set))
1463 && (GET_MODE (note) == VOIDmode
1464 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1465 : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1466 && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1467 || (GET_MODE (XEXP (SET_DEST (set), 0))
1468 == GET_MODE (note))))))
1470 /* Temporarily replace the set's source with the
1471 contents of the REG_EQUAL note. The insn will
1472 be deleted or recognized by try_combine. */
1473 rtx orig_src = SET_SRC (set);
1474 rtx orig_dest = SET_DEST (set);
1475 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1476 SET_DEST (set) = XEXP (SET_DEST (set), 0);
1477 SET_SRC (set) = note;
1478 i2mod = temp;
1479 i2mod_old_rhs = copy_rtx (orig_src);
1480 i2mod_new_rhs = copy_rtx (note);
1481 next = try_combine (insn, i2mod, NULL, NULL,
1482 &new_direct_jump_p,
1483 last_combined_insn);
1484 i2mod = NULL;
1485 if (next)
1487 statistics_counter_event (cfun, "insn-with-note combine", 1);
1488 goto retry;
1490 SET_SRC (set) = orig_src;
1491 SET_DEST (set) = orig_dest;
1495 if (!NOTE_P (insn))
1496 record_dead_and_set_regs (insn);
1498 retry:
1503 default_rtl_profile ();
1504 clear_bb_flags ();
1505 new_direct_jump_p |= purge_all_dead_edges ();
1506 delete_noop_moves ();
1508 /* Clean up. */
1509 obstack_free (&insn_link_obstack, NULL);
1510 free (uid_log_links);
1511 free (uid_insn_cost);
1512 reg_stat.release ();
1515 struct undo *undo, *next;
1516 for (undo = undobuf.frees; undo; undo = next)
1518 next = undo->next;
1519 free (undo);
1521 undobuf.frees = 0;
1524 total_attempts += combine_attempts;
1525 total_merges += combine_merges;
1526 total_extras += combine_extras;
1527 total_successes += combine_successes;
1529 nonzero_sign_valid = 0;
1530 rtl_hooks = general_rtl_hooks;
1532 /* Make recognizer allow volatile MEMs again. */
1533 init_recog ();
1535 return new_direct_jump_p;
1538 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1540 static void
1541 init_reg_last (void)
1543 unsigned int i;
1544 reg_stat_type *p;
1546 FOR_EACH_VEC_ELT (reg_stat, i, p)
1547 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1550 /* Set up any promoted values for incoming argument registers. */
1552 static void
1553 setup_incoming_promotions (rtx_insn *first)
1555 tree arg;
1556 bool strictly_local = false;
1558 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1559 arg = DECL_CHAIN (arg))
1561 rtx x, reg = DECL_INCOMING_RTL (arg);
1562 int uns1, uns3;
1563 machine_mode mode1, mode2, mode3, mode4;
1565 /* Only continue if the incoming argument is in a register. */
1566 if (!REG_P (reg))
1567 continue;
1569 /* Determine, if possible, whether all call sites of the current
1570 function lie within the current compilation unit. (This does
1571 take into account the exporting of a function via taking its
1572 address, and so forth.) */
1573 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1575 /* The mode and signedness of the argument before any promotions happen
1576 (equal to the mode of the pseudo holding it at that stage). */
1577 mode1 = TYPE_MODE (TREE_TYPE (arg));
1578 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1580 /* The mode and signedness of the argument after any source language and
1581 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1582 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1583 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1585 /* The mode and signedness of the argument as it is actually passed,
1586 see assign_parm_setup_reg in function.c. */
1587 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1588 TREE_TYPE (cfun->decl), 0);
1590 /* The mode of the register in which the argument is being passed. */
1591 mode4 = GET_MODE (reg);
1593 /* Eliminate sign extensions in the callee when:
1594 (a) A mode promotion has occurred; */
1595 if (mode1 == mode3)
1596 continue;
1597 /* (b) The mode of the register is the same as the mode of
1598 the argument as it is passed; */
1599 if (mode3 != mode4)
1600 continue;
1601 /* (c) There's no language level extension; */
1602 if (mode1 == mode2)
1604 /* (c.1) All callers are from the current compilation unit. If that's
1605 the case we don't have to rely on an ABI, we only have to know
1606 what we're generating right now, and we know that we will do the
1607 mode1 to mode2 promotion with the given sign. */
1608 else if (!strictly_local)
1609 continue;
1610 /* (c.2) The combination of the two promotions is useful. This is
1611 true when the signs match, or if the first promotion is unsigned.
1612 In the later case, (sign_extend (zero_extend x)) is the same as
1613 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1614 else if (uns1)
1615 uns3 = true;
1616 else if (uns3)
1617 continue;
1619 /* Record that the value was promoted from mode1 to mode3,
1620 so that any sign extension at the head of the current
1621 function may be eliminated. */
1622 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1623 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1624 record_value_for_reg (reg, first, x);
1628 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1629 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1630 because some machines (maybe most) will actually do the sign-extension and
1631 this is the conservative approach.
1633 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1634 kludge. */
1636 static rtx
1637 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1639 scalar_int_mode int_mode;
1640 if (CONST_INT_P (src)
1641 && is_a <scalar_int_mode> (mode, &int_mode)
1642 && GET_MODE_PRECISION (int_mode) < prec
1643 && INTVAL (src) > 0
1644 && val_signbit_known_set_p (int_mode, INTVAL (src)))
1645 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1647 return src;
1650 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1651 and SET. */
1653 static void
1654 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1655 rtx x)
1657 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1658 unsigned HOST_WIDE_INT bits = 0;
1659 rtx reg_equal = NULL, src = SET_SRC (set);
1660 unsigned int num = 0;
1662 if (reg_equal_note)
1663 reg_equal = XEXP (reg_equal_note, 0);
1665 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1667 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1668 if (reg_equal)
1669 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1672 /* Don't call nonzero_bits if it cannot change anything. */
1673 if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1675 bits = nonzero_bits (src, nonzero_bits_mode);
1676 if (reg_equal && bits)
1677 bits &= nonzero_bits (reg_equal, nonzero_bits_mode);
1678 rsp->nonzero_bits |= bits;
1681 /* Don't call num_sign_bit_copies if it cannot change anything. */
1682 if (rsp->sign_bit_copies != 1)
1684 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1685 if (reg_equal && num != GET_MODE_PRECISION (GET_MODE (x)))
1687 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1688 if (num == 0 || numeq > num)
1689 num = numeq;
1691 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1692 rsp->sign_bit_copies = num;
1696 /* Called via note_stores. If X is a pseudo that is narrower than
1697 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1699 If we are setting only a portion of X and we can't figure out what
1700 portion, assume all bits will be used since we don't know what will
1701 be happening.
1703 Similarly, set how many bits of X are known to be copies of the sign bit
1704 at all locations in the function. This is the smallest number implied
1705 by any set of X. */
1707 static void
1708 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1710 rtx_insn *insn = (rtx_insn *) data;
1711 scalar_int_mode mode;
1713 if (REG_P (x)
1714 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1715 /* If this register is undefined at the start of the file, we can't
1716 say what its contents were. */
1717 && ! REGNO_REG_SET_P
1718 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1719 && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1720 && HWI_COMPUTABLE_MODE_P (mode))
1722 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1724 if (set == 0 || GET_CODE (set) == CLOBBER)
1726 rsp->nonzero_bits = GET_MODE_MASK (mode);
1727 rsp->sign_bit_copies = 1;
1728 return;
1731 /* If this register is being initialized using itself, and the
1732 register is uninitialized in this basic block, and there are
1733 no LOG_LINKS which set the register, then part of the
1734 register is uninitialized. In that case we can't assume
1735 anything about the number of nonzero bits.
1737 ??? We could do better if we checked this in
1738 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1739 could avoid making assumptions about the insn which initially
1740 sets the register, while still using the information in other
1741 insns. We would have to be careful to check every insn
1742 involved in the combination. */
1744 if (insn
1745 && reg_referenced_p (x, PATTERN (insn))
1746 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1747 REGNO (x)))
1749 struct insn_link *link;
1751 FOR_EACH_LOG_LINK (link, insn)
1752 if (dead_or_set_p (link->insn, x))
1753 break;
1754 if (!link)
1756 rsp->nonzero_bits = GET_MODE_MASK (mode);
1757 rsp->sign_bit_copies = 1;
1758 return;
1762 /* If this is a complex assignment, see if we can convert it into a
1763 simple assignment. */
1764 set = expand_field_assignment (set);
1766 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1767 set what we know about X. */
1769 if (SET_DEST (set) == x
1770 || (paradoxical_subreg_p (SET_DEST (set))
1771 && SUBREG_REG (SET_DEST (set)) == x))
1772 update_rsp_from_reg_equal (rsp, insn, set, x);
1773 else
1775 rsp->nonzero_bits = GET_MODE_MASK (mode);
1776 rsp->sign_bit_copies = 1;
1781 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1782 optionally insns that were previously combined into I3 or that will be
1783 combined into the merger of INSN and I3. The order is PRED, PRED2,
1784 INSN, SUCC, SUCC2, I3.
1786 Return 0 if the combination is not allowed for any reason.
1788 If the combination is allowed, *PDEST will be set to the single
1789 destination of INSN and *PSRC to the single source, and this function
1790 will return 1. */
1792 static int
1793 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1794 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1795 rtx *pdest, rtx *psrc)
1797 int i;
1798 const_rtx set = 0;
1799 rtx src, dest;
1800 rtx_insn *p;
1801 rtx link;
1802 bool all_adjacent = true;
1803 int (*is_volatile_p) (const_rtx);
1805 if (succ)
1807 if (succ2)
1809 if (next_active_insn (succ2) != i3)
1810 all_adjacent = false;
1811 if (next_active_insn (succ) != succ2)
1812 all_adjacent = false;
1814 else if (next_active_insn (succ) != i3)
1815 all_adjacent = false;
1816 if (next_active_insn (insn) != succ)
1817 all_adjacent = false;
1819 else if (next_active_insn (insn) != i3)
1820 all_adjacent = false;
1822 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1823 or a PARALLEL consisting of such a SET and CLOBBERs.
1825 If INSN has CLOBBER parallel parts, ignore them for our processing.
1826 By definition, these happen during the execution of the insn. When it
1827 is merged with another insn, all bets are off. If they are, in fact,
1828 needed and aren't also supplied in I3, they may be added by
1829 recog_for_combine. Otherwise, it won't match.
1831 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1832 note.
1834 Get the source and destination of INSN. If more than one, can't
1835 combine. */
1837 if (GET_CODE (PATTERN (insn)) == SET)
1838 set = PATTERN (insn);
1839 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1840 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1842 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1844 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1846 switch (GET_CODE (elt))
1848 /* This is important to combine floating point insns
1849 for the SH4 port. */
1850 case USE:
1851 /* Combining an isolated USE doesn't make sense.
1852 We depend here on combinable_i3pat to reject them. */
1853 /* The code below this loop only verifies that the inputs of
1854 the SET in INSN do not change. We call reg_set_between_p
1855 to verify that the REG in the USE does not change between
1856 I3 and INSN.
1857 If the USE in INSN was for a pseudo register, the matching
1858 insn pattern will likely match any register; combining this
1859 with any other USE would only be safe if we knew that the
1860 used registers have identical values, or if there was
1861 something to tell them apart, e.g. different modes. For
1862 now, we forgo such complicated tests and simply disallow
1863 combining of USES of pseudo registers with any other USE. */
1864 if (REG_P (XEXP (elt, 0))
1865 && GET_CODE (PATTERN (i3)) == PARALLEL)
1867 rtx i3pat = PATTERN (i3);
1868 int i = XVECLEN (i3pat, 0) - 1;
1869 unsigned int regno = REGNO (XEXP (elt, 0));
1873 rtx i3elt = XVECEXP (i3pat, 0, i);
1875 if (GET_CODE (i3elt) == USE
1876 && REG_P (XEXP (i3elt, 0))
1877 && (REGNO (XEXP (i3elt, 0)) == regno
1878 ? reg_set_between_p (XEXP (elt, 0),
1879 PREV_INSN (insn), i3)
1880 : regno >= FIRST_PSEUDO_REGISTER))
1881 return 0;
1883 while (--i >= 0);
1885 break;
1887 /* We can ignore CLOBBERs. */
1888 case CLOBBER:
1889 break;
1891 case SET:
1892 /* Ignore SETs whose result isn't used but not those that
1893 have side-effects. */
1894 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1895 && insn_nothrow_p (insn)
1896 && !side_effects_p (elt))
1897 break;
1899 /* If we have already found a SET, this is a second one and
1900 so we cannot combine with this insn. */
1901 if (set)
1902 return 0;
1904 set = elt;
1905 break;
1907 default:
1908 /* Anything else means we can't combine. */
1909 return 0;
1913 if (set == 0
1914 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1915 so don't do anything with it. */
1916 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1917 return 0;
1919 else
1920 return 0;
1922 if (set == 0)
1923 return 0;
1925 /* The simplification in expand_field_assignment may call back to
1926 get_last_value, so set safe guard here. */
1927 subst_low_luid = DF_INSN_LUID (insn);
1929 set = expand_field_assignment (set);
1930 src = SET_SRC (set), dest = SET_DEST (set);
1932 /* Do not eliminate user-specified register if it is in an
1933 asm input because we may break the register asm usage defined
1934 in GCC manual if allow to do so.
1935 Be aware that this may cover more cases than we expect but this
1936 should be harmless. */
1937 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1938 && extract_asm_operands (PATTERN (i3)))
1939 return 0;
1941 /* Don't eliminate a store in the stack pointer. */
1942 if (dest == stack_pointer_rtx
1943 /* Don't combine with an insn that sets a register to itself if it has
1944 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1945 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1946 /* Can't merge an ASM_OPERANDS. */
1947 || GET_CODE (src) == ASM_OPERANDS
1948 /* Can't merge a function call. */
1949 || GET_CODE (src) == CALL
1950 /* Don't eliminate a function call argument. */
1951 || (CALL_P (i3)
1952 && (find_reg_fusage (i3, USE, dest)
1953 || (REG_P (dest)
1954 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1955 && global_regs[REGNO (dest)])))
1956 /* Don't substitute into an incremented register. */
1957 || FIND_REG_INC_NOTE (i3, dest)
1958 || (succ && FIND_REG_INC_NOTE (succ, dest))
1959 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1960 /* Don't substitute into a non-local goto, this confuses CFG. */
1961 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1962 /* Make sure that DEST is not used after INSN but before SUCC, or
1963 after SUCC and before SUCC2, or after SUCC2 but before I3. */
1964 || (!all_adjacent
1965 && ((succ2
1966 && (reg_used_between_p (dest, succ2, i3)
1967 || reg_used_between_p (dest, succ, succ2)))
1968 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1969 || (succ
1970 /* SUCC and SUCC2 can be split halves from a PARALLEL; in
1971 that case SUCC is not in the insn stream, so use SUCC2
1972 instead for this test. */
1973 && reg_used_between_p (dest, insn,
1974 succ2
1975 && INSN_UID (succ) == INSN_UID (succ2)
1976 ? succ2 : succ))))
1977 /* Make sure that the value that is to be substituted for the register
1978 does not use any registers whose values alter in between. However,
1979 If the insns are adjacent, a use can't cross a set even though we
1980 think it might (this can happen for a sequence of insns each setting
1981 the same destination; last_set of that register might point to
1982 a NOTE). If INSN has a REG_EQUIV note, the register is always
1983 equivalent to the memory so the substitution is valid even if there
1984 are intervening stores. Also, don't move a volatile asm or
1985 UNSPEC_VOLATILE across any other insns. */
1986 || (! all_adjacent
1987 && (((!MEM_P (src)
1988 || ! find_reg_note (insn, REG_EQUIV, src))
1989 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1990 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1991 || GET_CODE (src) == UNSPEC_VOLATILE))
1992 /* Don't combine across a CALL_INSN, because that would possibly
1993 change whether the life span of some REGs crosses calls or not,
1994 and it is a pain to update that information.
1995 Exception: if source is a constant, moving it later can't hurt.
1996 Accept that as a special case. */
1997 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1998 return 0;
2000 /* DEST must either be a REG or CC0. */
2001 if (REG_P (dest))
2003 /* If register alignment is being enforced for multi-word items in all
2004 cases except for parameters, it is possible to have a register copy
2005 insn referencing a hard register that is not allowed to contain the
2006 mode being copied and which would not be valid as an operand of most
2007 insns. Eliminate this problem by not combining with such an insn.
2009 Also, on some machines we don't want to extend the life of a hard
2010 register. */
2012 if (REG_P (src)
2013 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
2014 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
2015 /* Don't extend the life of a hard register unless it is
2016 user variable (if we have few registers) or it can't
2017 fit into the desired register (meaning something special
2018 is going on).
2019 Also avoid substituting a return register into I3, because
2020 reload can't handle a conflict with constraints of other
2021 inputs. */
2022 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2023 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
2024 return 0;
2026 else if (GET_CODE (dest) != CC0)
2027 return 0;
2030 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2031 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2032 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2034 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2036 /* If the clobber represents an earlyclobber operand, we must not
2037 substitute an expression containing the clobbered register.
2038 As we do not analyze the constraint strings here, we have to
2039 make the conservative assumption. However, if the register is
2040 a fixed hard reg, the clobber cannot represent any operand;
2041 we leave it up to the machine description to either accept or
2042 reject use-and-clobber patterns. */
2043 if (!REG_P (reg)
2044 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2045 || !fixed_regs[REGNO (reg)])
2046 if (reg_overlap_mentioned_p (reg, src))
2047 return 0;
2050 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2051 or not), reject, unless nothing volatile comes between it and I3 */
2053 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2055 /* Make sure neither succ nor succ2 contains a volatile reference. */
2056 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2057 return 0;
2058 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2059 return 0;
2060 /* We'll check insns between INSN and I3 below. */
2063 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2064 to be an explicit register variable, and was chosen for a reason. */
2066 if (GET_CODE (src) == ASM_OPERANDS
2067 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2068 return 0;
2070 /* If INSN contains volatile references (specifically volatile MEMs),
2071 we cannot combine across any other volatile references.
2072 Even if INSN doesn't contain volatile references, any intervening
2073 volatile insn might affect machine state. */
2075 is_volatile_p = volatile_refs_p (PATTERN (insn))
2076 ? volatile_refs_p
2077 : volatile_insn_p;
2079 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2080 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2081 return 0;
2083 /* If INSN contains an autoincrement or autodecrement, make sure that
2084 register is not used between there and I3, and not already used in
2085 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2086 Also insist that I3 not be a jump; if it were one
2087 and the incremented register were spilled, we would lose. */
2089 if (AUTO_INC_DEC)
2090 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2091 if (REG_NOTE_KIND (link) == REG_INC
2092 && (JUMP_P (i3)
2093 || reg_used_between_p (XEXP (link, 0), insn, i3)
2094 || (pred != NULL_RTX
2095 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2096 || (pred2 != NULL_RTX
2097 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2098 || (succ != NULL_RTX
2099 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2100 || (succ2 != NULL_RTX
2101 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2102 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2103 return 0;
2105 /* Don't combine an insn that follows a CC0-setting insn.
2106 An insn that uses CC0 must not be separated from the one that sets it.
2107 We do, however, allow I2 to follow a CC0-setting insn if that insn
2108 is passed as I1; in that case it will be deleted also.
2109 We also allow combining in this case if all the insns are adjacent
2110 because that would leave the two CC0 insns adjacent as well.
2111 It would be more logical to test whether CC0 occurs inside I1 or I2,
2112 but that would be much slower, and this ought to be equivalent. */
2114 if (HAVE_cc0)
2116 p = prev_nonnote_insn (insn);
2117 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2118 && ! all_adjacent)
2119 return 0;
2122 /* If we get here, we have passed all the tests and the combination is
2123 to be allowed. */
2125 *pdest = dest;
2126 *psrc = src;
2128 return 1;
2131 /* LOC is the location within I3 that contains its pattern or the component
2132 of a PARALLEL of the pattern. We validate that it is valid for combining.
2134 One problem is if I3 modifies its output, as opposed to replacing it
2135 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2136 doing so would produce an insn that is not equivalent to the original insns.
2138 Consider:
2140 (set (reg:DI 101) (reg:DI 100))
2141 (set (subreg:SI (reg:DI 101) 0) <foo>)
2143 This is NOT equivalent to:
2145 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2146 (set (reg:DI 101) (reg:DI 100))])
2148 Not only does this modify 100 (in which case it might still be valid
2149 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2151 We can also run into a problem if I2 sets a register that I1
2152 uses and I1 gets directly substituted into I3 (not via I2). In that
2153 case, we would be getting the wrong value of I2DEST into I3, so we
2154 must reject the combination. This case occurs when I2 and I1 both
2155 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2156 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2157 of a SET must prevent combination from occurring. The same situation
2158 can occur for I0, in which case I0_NOT_IN_SRC is set.
2160 Before doing the above check, we first try to expand a field assignment
2161 into a set of logical operations.
2163 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2164 we place a register that is both set and used within I3. If more than one
2165 such register is detected, we fail.
2167 Return 1 if the combination is valid, zero otherwise. */
2169 static int
2170 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2171 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2173 rtx x = *loc;
2175 if (GET_CODE (x) == SET)
2177 rtx set = x ;
2178 rtx dest = SET_DEST (set);
2179 rtx src = SET_SRC (set);
2180 rtx inner_dest = dest;
2181 rtx subdest;
2183 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2184 || GET_CODE (inner_dest) == SUBREG
2185 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2186 inner_dest = XEXP (inner_dest, 0);
2188 /* Check for the case where I3 modifies its output, as discussed
2189 above. We don't want to prevent pseudos from being combined
2190 into the address of a MEM, so only prevent the combination if
2191 i1 or i2 set the same MEM. */
2192 if ((inner_dest != dest &&
2193 (!MEM_P (inner_dest)
2194 || rtx_equal_p (i2dest, inner_dest)
2195 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2196 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2197 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2198 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2199 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2201 /* This is the same test done in can_combine_p except we can't test
2202 all_adjacent; we don't have to, since this instruction will stay
2203 in place, thus we are not considering increasing the lifetime of
2204 INNER_DEST.
2206 Also, if this insn sets a function argument, combining it with
2207 something that might need a spill could clobber a previous
2208 function argument; the all_adjacent test in can_combine_p also
2209 checks this; here, we do a more specific test for this case. */
2211 || (REG_P (inner_dest)
2212 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2213 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2214 GET_MODE (inner_dest))))
2215 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2216 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2217 return 0;
2219 /* If DEST is used in I3, it is being killed in this insn, so
2220 record that for later. We have to consider paradoxical
2221 subregs here, since they kill the whole register, but we
2222 ignore partial subregs, STRICT_LOW_PART, etc.
2223 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2224 STACK_POINTER_REGNUM, since these are always considered to be
2225 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2226 subdest = dest;
2227 if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2228 subdest = SUBREG_REG (subdest);
2229 if (pi3dest_killed
2230 && REG_P (subdest)
2231 && reg_referenced_p (subdest, PATTERN (i3))
2232 && REGNO (subdest) != FRAME_POINTER_REGNUM
2233 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2234 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2235 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2236 || (REGNO (subdest) != ARG_POINTER_REGNUM
2237 || ! fixed_regs [REGNO (subdest)]))
2238 && REGNO (subdest) != STACK_POINTER_REGNUM)
2240 if (*pi3dest_killed)
2241 return 0;
2243 *pi3dest_killed = subdest;
2247 else if (GET_CODE (x) == PARALLEL)
2249 int i;
2251 for (i = 0; i < XVECLEN (x, 0); i++)
2252 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2253 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2254 return 0;
2257 return 1;
2260 /* Return 1 if X is an arithmetic expression that contains a multiplication
2261 and division. We don't count multiplications by powers of two here. */
2263 static int
2264 contains_muldiv (rtx x)
2266 switch (GET_CODE (x))
2268 case MOD: case DIV: case UMOD: case UDIV:
2269 return 1;
2271 case MULT:
2272 return ! (CONST_INT_P (XEXP (x, 1))
2273 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2274 default:
2275 if (BINARY_P (x))
2276 return contains_muldiv (XEXP (x, 0))
2277 || contains_muldiv (XEXP (x, 1));
2279 if (UNARY_P (x))
2280 return contains_muldiv (XEXP (x, 0));
2282 return 0;
2286 /* Determine whether INSN can be used in a combination. Return nonzero if
2287 not. This is used in try_combine to detect early some cases where we
2288 can't perform combinations. */
2290 static int
2291 cant_combine_insn_p (rtx_insn *insn)
2293 rtx set;
2294 rtx src, dest;
2296 /* If this isn't really an insn, we can't do anything.
2297 This can occur when flow deletes an insn that it has merged into an
2298 auto-increment address. */
2299 if (!NONDEBUG_INSN_P (insn))
2300 return 1;
2302 /* Never combine loads and stores involving hard regs that are likely
2303 to be spilled. The register allocator can usually handle such
2304 reg-reg moves by tying. If we allow the combiner to make
2305 substitutions of likely-spilled regs, reload might die.
2306 As an exception, we allow combinations involving fixed regs; these are
2307 not available to the register allocator so there's no risk involved. */
2309 set = single_set (insn);
2310 if (! set)
2311 return 0;
2312 src = SET_SRC (set);
2313 dest = SET_DEST (set);
2314 if (GET_CODE (src) == SUBREG)
2315 src = SUBREG_REG (src);
2316 if (GET_CODE (dest) == SUBREG)
2317 dest = SUBREG_REG (dest);
2318 if (REG_P (src) && REG_P (dest)
2319 && ((HARD_REGISTER_P (src)
2320 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2321 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2322 || (HARD_REGISTER_P (dest)
2323 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2324 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2325 return 1;
2327 return 0;
2330 struct likely_spilled_retval_info
2332 unsigned regno, nregs;
2333 unsigned mask;
2336 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2337 hard registers that are known to be written to / clobbered in full. */
2338 static void
2339 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2341 struct likely_spilled_retval_info *const info =
2342 (struct likely_spilled_retval_info *) data;
2343 unsigned regno, nregs;
2344 unsigned new_mask;
2346 if (!REG_P (XEXP (set, 0)))
2347 return;
2348 regno = REGNO (x);
2349 if (regno >= info->regno + info->nregs)
2350 return;
2351 nregs = REG_NREGS (x);
2352 if (regno + nregs <= info->regno)
2353 return;
2354 new_mask = (2U << (nregs - 1)) - 1;
2355 if (regno < info->regno)
2356 new_mask >>= info->regno - regno;
2357 else
2358 new_mask <<= regno - info->regno;
2359 info->mask &= ~new_mask;
2362 /* Return nonzero iff part of the return value is live during INSN, and
2363 it is likely spilled. This can happen when more than one insn is needed
2364 to copy the return value, e.g. when we consider to combine into the
2365 second copy insn for a complex value. */
2367 static int
2368 likely_spilled_retval_p (rtx_insn *insn)
2370 rtx_insn *use = BB_END (this_basic_block);
2371 rtx reg;
2372 rtx_insn *p;
2373 unsigned regno, nregs;
2374 /* We assume here that no machine mode needs more than
2375 32 hard registers when the value overlaps with a register
2376 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2377 unsigned mask;
2378 struct likely_spilled_retval_info info;
2380 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2381 return 0;
2382 reg = XEXP (PATTERN (use), 0);
2383 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2384 return 0;
2385 regno = REGNO (reg);
2386 nregs = REG_NREGS (reg);
2387 if (nregs == 1)
2388 return 0;
2389 mask = (2U << (nregs - 1)) - 1;
2391 /* Disregard parts of the return value that are set later. */
2392 info.regno = regno;
2393 info.nregs = nregs;
2394 info.mask = mask;
2395 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2396 if (INSN_P (p))
2397 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2398 mask = info.mask;
2400 /* Check if any of the (probably) live return value registers is
2401 likely spilled. */
2402 nregs --;
2405 if ((mask & 1 << nregs)
2406 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2407 return 1;
2408 } while (nregs--);
2409 return 0;
2412 /* Adjust INSN after we made a change to its destination.
2414 Changing the destination can invalidate notes that say something about
2415 the results of the insn and a LOG_LINK pointing to the insn. */
2417 static void
2418 adjust_for_new_dest (rtx_insn *insn)
2420 /* For notes, be conservative and simply remove them. */
2421 remove_reg_equal_equiv_notes (insn);
2423 /* The new insn will have a destination that was previously the destination
2424 of an insn just above it. Call distribute_links to make a LOG_LINK from
2425 the next use of that destination. */
2427 rtx set = single_set (insn);
2428 gcc_assert (set);
2430 rtx reg = SET_DEST (set);
2432 while (GET_CODE (reg) == ZERO_EXTRACT
2433 || GET_CODE (reg) == STRICT_LOW_PART
2434 || GET_CODE (reg) == SUBREG)
2435 reg = XEXP (reg, 0);
2436 gcc_assert (REG_P (reg));
2438 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2440 df_insn_rescan (insn);
2443 /* Return TRUE if combine can reuse reg X in mode MODE.
2444 ADDED_SETS is nonzero if the original set is still required. */
2445 static bool
2446 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2448 unsigned int regno;
2450 if (!REG_P (x))
2451 return false;
2453 regno = REGNO (x);
2454 /* Allow hard registers if the new mode is legal, and occupies no more
2455 registers than the old mode. */
2456 if (regno < FIRST_PSEUDO_REGISTER)
2457 return (HARD_REGNO_MODE_OK (regno, mode)
2458 && REG_NREGS (x) >= hard_regno_nregs[regno][mode]);
2460 /* Or a pseudo that is only used once. */
2461 return (regno < reg_n_sets_max
2462 && REG_N_SETS (regno) == 1
2463 && !added_sets
2464 && !REG_USERVAR_P (x));
2468 /* Check whether X, the destination of a set, refers to part of
2469 the register specified by REG. */
2471 static bool
2472 reg_subword_p (rtx x, rtx reg)
2474 /* Check that reg is an integer mode register. */
2475 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2476 return false;
2478 if (GET_CODE (x) == STRICT_LOW_PART
2479 || GET_CODE (x) == ZERO_EXTRACT)
2480 x = XEXP (x, 0);
2482 return GET_CODE (x) == SUBREG
2483 && SUBREG_REG (x) == reg
2484 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2487 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2488 Note that the INSN should be deleted *after* removing dead edges, so
2489 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2490 but not for a (set (pc) (label_ref FOO)). */
2492 static void
2493 update_cfg_for_uncondjump (rtx_insn *insn)
2495 basic_block bb = BLOCK_FOR_INSN (insn);
2496 gcc_assert (BB_END (bb) == insn);
2498 purge_dead_edges (bb);
2500 delete_insn (insn);
2501 if (EDGE_COUNT (bb->succs) == 1)
2503 rtx_insn *insn;
2505 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2507 /* Remove barriers from the footer if there are any. */
2508 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2509 if (BARRIER_P (insn))
2511 if (PREV_INSN (insn))
2512 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2513 else
2514 BB_FOOTER (bb) = NEXT_INSN (insn);
2515 if (NEXT_INSN (insn))
2516 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2518 else if (LABEL_P (insn))
2519 break;
2523 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2524 by an arbitrary number of CLOBBERs. */
2525 static bool
2526 is_parallel_of_n_reg_sets (rtx pat, int n)
2528 if (GET_CODE (pat) != PARALLEL)
2529 return false;
2531 int len = XVECLEN (pat, 0);
2532 if (len < n)
2533 return false;
2535 int i;
2536 for (i = 0; i < n; i++)
2537 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2538 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2539 return false;
2540 for ( ; i < len; i++)
2541 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER
2542 || XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2543 return false;
2545 return true;
2548 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2549 CLOBBERs), can be split into individual SETs in that order, without
2550 changing semantics. */
2551 static bool
2552 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2554 if (!insn_nothrow_p (insn))
2555 return false;
2557 rtx pat = PATTERN (insn);
2559 int i, j;
2560 for (i = 0; i < n; i++)
2562 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2563 return false;
2565 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2567 for (j = i + 1; j < n; j++)
2568 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2569 return false;
2572 return true;
2575 /* Try to combine the insns I0, I1 and I2 into I3.
2576 Here I0, I1 and I2 appear earlier than I3.
2577 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2580 If we are combining more than two insns and the resulting insn is not
2581 recognized, try splitting it into two insns. If that happens, I2 and I3
2582 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2583 Otherwise, I0, I1 and I2 are pseudo-deleted.
2585 Return 0 if the combination does not work. Then nothing is changed.
2586 If we did the combination, return the insn at which combine should
2587 resume scanning.
2589 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2590 new direct jump instruction.
2592 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2593 been I3 passed to an earlier try_combine within the same basic
2594 block. */
2596 static rtx_insn *
2597 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2598 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2600 /* New patterns for I3 and I2, respectively. */
2601 rtx newpat, newi2pat = 0;
2602 rtvec newpat_vec_with_clobbers = 0;
2603 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2604 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2605 dead. */
2606 int added_sets_0, added_sets_1, added_sets_2;
2607 /* Total number of SETs to put into I3. */
2608 int total_sets;
2609 /* Nonzero if I2's or I1's body now appears in I3. */
2610 int i2_is_used = 0, i1_is_used = 0;
2611 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2612 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2613 /* Contains I3 if the destination of I3 is used in its source, which means
2614 that the old life of I3 is being killed. If that usage is placed into
2615 I2 and not in I3, a REG_DEAD note must be made. */
2616 rtx i3dest_killed = 0;
2617 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2618 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2619 /* Copy of SET_SRC of I1 and I0, if needed. */
2620 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2621 /* Set if I2DEST was reused as a scratch register. */
2622 bool i2scratch = false;
2623 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2624 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2625 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2626 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2627 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2628 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2629 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2630 /* Notes that must be added to REG_NOTES in I3 and I2. */
2631 rtx new_i3_notes, new_i2_notes;
2632 /* Notes that we substituted I3 into I2 instead of the normal case. */
2633 int i3_subst_into_i2 = 0;
2634 /* Notes that I1, I2 or I3 is a MULT operation. */
2635 int have_mult = 0;
2636 int swap_i2i3 = 0;
2637 int changed_i3_dest = 0;
2639 int maxreg;
2640 rtx_insn *temp_insn;
2641 rtx temp_expr;
2642 struct insn_link *link;
2643 rtx other_pat = 0;
2644 rtx new_other_notes;
2645 int i;
2646 scalar_int_mode dest_mode, temp_mode;
2648 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2649 never be). */
2650 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2651 return 0;
2653 /* Only try four-insn combinations when there's high likelihood of
2654 success. Look for simple insns, such as loads of constants or
2655 binary operations involving a constant. */
2656 if (i0)
2658 int i;
2659 int ngood = 0;
2660 int nshift = 0;
2661 rtx set0, set3;
2663 if (!flag_expensive_optimizations)
2664 return 0;
2666 for (i = 0; i < 4; i++)
2668 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2669 rtx set = single_set (insn);
2670 rtx src;
2671 if (!set)
2672 continue;
2673 src = SET_SRC (set);
2674 if (CONSTANT_P (src))
2676 ngood += 2;
2677 break;
2679 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2680 ngood++;
2681 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2682 || GET_CODE (src) == LSHIFTRT)
2683 nshift++;
2686 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2687 are likely manipulating its value. Ideally we'll be able to combine
2688 all four insns into a bitfield insertion of some kind.
2690 Note the source in I0 might be inside a sign/zero extension and the
2691 memory modes in I0 and I3 might be different. So extract the address
2692 from the destination of I3 and search for it in the source of I0.
2694 In the event that there's a match but the source/dest do not actually
2695 refer to the same memory, the worst that happens is we try some
2696 combinations that we wouldn't have otherwise. */
2697 if ((set0 = single_set (i0))
2698 /* Ensure the source of SET0 is a MEM, possibly buried inside
2699 an extension. */
2700 && (GET_CODE (SET_SRC (set0)) == MEM
2701 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2702 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2703 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2704 && (set3 = single_set (i3))
2705 /* Ensure the destination of SET3 is a MEM. */
2706 && GET_CODE (SET_DEST (set3)) == MEM
2707 /* Would it be better to extract the base address for the MEM
2708 in SET3 and look for that? I don't have cases where it matters
2709 but I could envision such cases. */
2710 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2711 ngood += 2;
2713 if (ngood < 2 && nshift < 2)
2714 return 0;
2717 /* Exit early if one of the insns involved can't be used for
2718 combinations. */
2719 if (CALL_P (i2)
2720 || (i1 && CALL_P (i1))
2721 || (i0 && CALL_P (i0))
2722 || cant_combine_insn_p (i3)
2723 || cant_combine_insn_p (i2)
2724 || (i1 && cant_combine_insn_p (i1))
2725 || (i0 && cant_combine_insn_p (i0))
2726 || likely_spilled_retval_p (i3))
2727 return 0;
2729 combine_attempts++;
2730 undobuf.other_insn = 0;
2732 /* Reset the hard register usage information. */
2733 CLEAR_HARD_REG_SET (newpat_used_regs);
2735 if (dump_file && (dump_flags & TDF_DETAILS))
2737 if (i0)
2738 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2739 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2740 else if (i1)
2741 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2742 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2743 else
2744 fprintf (dump_file, "\nTrying %d -> %d:\n",
2745 INSN_UID (i2), INSN_UID (i3));
2748 /* If multiple insns feed into one of I2 or I3, they can be in any
2749 order. To simplify the code below, reorder them in sequence. */
2750 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2751 std::swap (i0, i2);
2752 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2753 std::swap (i0, i1);
2754 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2755 std::swap (i1, i2);
2757 added_links_insn = 0;
2759 /* First check for one important special case that the code below will
2760 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2761 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2762 we may be able to replace that destination with the destination of I3.
2763 This occurs in the common code where we compute both a quotient and
2764 remainder into a structure, in which case we want to do the computation
2765 directly into the structure to avoid register-register copies.
2767 Note that this case handles both multiple sets in I2 and also cases
2768 where I2 has a number of CLOBBERs inside the PARALLEL.
2770 We make very conservative checks below and only try to handle the
2771 most common cases of this. For example, we only handle the case
2772 where I2 and I3 are adjacent to avoid making difficult register
2773 usage tests. */
2775 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2776 && REG_P (SET_SRC (PATTERN (i3)))
2777 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2778 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2779 && GET_CODE (PATTERN (i2)) == PARALLEL
2780 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2781 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2782 below would need to check what is inside (and reg_overlap_mentioned_p
2783 doesn't support those codes anyway). Don't allow those destinations;
2784 the resulting insn isn't likely to be recognized anyway. */
2785 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2786 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2787 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2788 SET_DEST (PATTERN (i3)))
2789 && next_active_insn (i2) == i3)
2791 rtx p2 = PATTERN (i2);
2793 /* Make sure that the destination of I3,
2794 which we are going to substitute into one output of I2,
2795 is not used within another output of I2. We must avoid making this:
2796 (parallel [(set (mem (reg 69)) ...)
2797 (set (reg 69) ...)])
2798 which is not well-defined as to order of actions.
2799 (Besides, reload can't handle output reloads for this.)
2801 The problem can also happen if the dest of I3 is a memory ref,
2802 if another dest in I2 is an indirect memory ref.
2804 Neither can this PARALLEL be an asm. We do not allow combining
2805 that usually (see can_combine_p), so do not here either. */
2806 bool ok = true;
2807 for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2809 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2810 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2811 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2812 SET_DEST (XVECEXP (p2, 0, i))))
2813 ok = false;
2814 else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2815 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2816 ok = false;
2819 if (ok)
2820 for (i = 0; i < XVECLEN (p2, 0); i++)
2821 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2822 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2824 combine_merges++;
2826 subst_insn = i3;
2827 subst_low_luid = DF_INSN_LUID (i2);
2829 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2830 i2src = SET_SRC (XVECEXP (p2, 0, i));
2831 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2832 i2dest_killed = dead_or_set_p (i2, i2dest);
2834 /* Replace the dest in I2 with our dest and make the resulting
2835 insn the new pattern for I3. Then skip to where we validate
2836 the pattern. Everything was set up above. */
2837 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2838 newpat = p2;
2839 i3_subst_into_i2 = 1;
2840 goto validate_replacement;
2844 /* If I2 is setting a pseudo to a constant and I3 is setting some
2845 sub-part of it to another constant, merge them by making a new
2846 constant. */
2847 if (i1 == 0
2848 && (temp_expr = single_set (i2)) != 0
2849 && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2850 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2851 && GET_CODE (PATTERN (i3)) == SET
2852 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2853 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2855 rtx dest = SET_DEST (PATTERN (i3));
2856 rtx temp_dest = SET_DEST (temp_expr);
2857 int offset = -1;
2858 int width = 0;
2860 if (GET_CODE (dest) == ZERO_EXTRACT)
2862 if (CONST_INT_P (XEXP (dest, 1))
2863 && CONST_INT_P (XEXP (dest, 2))
2864 && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2865 &dest_mode))
2867 width = INTVAL (XEXP (dest, 1));
2868 offset = INTVAL (XEXP (dest, 2));
2869 dest = XEXP (dest, 0);
2870 if (BITS_BIG_ENDIAN)
2871 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2874 else
2876 if (GET_CODE (dest) == STRICT_LOW_PART)
2877 dest = XEXP (dest, 0);
2878 if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2880 width = GET_MODE_PRECISION (dest_mode);
2881 offset = 0;
2885 if (offset >= 0)
2887 /* If this is the low part, we're done. */
2888 if (subreg_lowpart_p (dest))
2890 /* Handle the case where inner is twice the size of outer. */
2891 else if (GET_MODE_PRECISION (temp_mode)
2892 == 2 * GET_MODE_PRECISION (dest_mode))
2893 offset += GET_MODE_PRECISION (dest_mode);
2894 /* Otherwise give up for now. */
2895 else
2896 offset = -1;
2899 if (offset >= 0)
2901 rtx inner = SET_SRC (PATTERN (i3));
2902 rtx outer = SET_SRC (temp_expr);
2904 wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2905 rtx_mode_t (inner, dest_mode),
2906 offset, width);
2908 combine_merges++;
2909 subst_insn = i3;
2910 subst_low_luid = DF_INSN_LUID (i2);
2911 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2912 i2dest = temp_dest;
2913 i2dest_killed = dead_or_set_p (i2, i2dest);
2915 /* Replace the source in I2 with the new constant and make the
2916 resulting insn the new pattern for I3. Then skip to where we
2917 validate the pattern. Everything was set up above. */
2918 SUBST (SET_SRC (temp_expr),
2919 immed_wide_int_const (o, temp_mode));
2921 newpat = PATTERN (i2);
2923 /* The dest of I3 has been replaced with the dest of I2. */
2924 changed_i3_dest = 1;
2925 goto validate_replacement;
2929 /* If we have no I1 and I2 looks like:
2930 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2931 (set Y OP)])
2932 make up a dummy I1 that is
2933 (set Y OP)
2934 and change I2 to be
2935 (set (reg:CC X) (compare:CC Y (const_int 0)))
2937 (We can ignore any trailing CLOBBERs.)
2939 This undoes a previous combination and allows us to match a branch-and-
2940 decrement insn. */
2942 if (!HAVE_cc0 && i1 == 0
2943 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2944 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2945 == MODE_CC)
2946 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2947 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2948 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2949 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2950 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2951 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2953 /* We make I1 with the same INSN_UID as I2. This gives it
2954 the same DF_INSN_LUID for value tracking. Our fake I1 will
2955 never appear in the insn stream so giving it the same INSN_UID
2956 as I2 will not cause a problem. */
2958 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2959 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2960 -1, NULL_RTX);
2961 INSN_UID (i1) = INSN_UID (i2);
2963 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2964 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2965 SET_DEST (PATTERN (i1)));
2966 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2967 SUBST_LINK (LOG_LINKS (i2),
2968 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2971 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2972 make those two SETs separate I1 and I2 insns, and make an I0 that is
2973 the original I1. */
2974 if (!HAVE_cc0 && i0 == 0
2975 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2976 && can_split_parallel_of_n_reg_sets (i2, 2)
2977 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2978 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2980 /* If there is no I1, there is no I0 either. */
2981 i0 = i1;
2983 /* We make I1 with the same INSN_UID as I2. This gives it
2984 the same DF_INSN_LUID for value tracking. Our fake I1 will
2985 never appear in the insn stream so giving it the same INSN_UID
2986 as I2 will not cause a problem. */
2988 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2989 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2990 -1, NULL_RTX);
2991 INSN_UID (i1) = INSN_UID (i2);
2993 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2996 /* Verify that I2 and I1 are valid for combining. */
2997 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2998 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2999 &i1dest, &i1src))
3000 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
3001 &i0dest, &i0src)))
3003 undo_all ();
3004 return 0;
3007 /* Record whether I2DEST is used in I2SRC and similarly for the other
3008 cases. Knowing this will help in register status updating below. */
3009 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3010 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3011 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3012 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3013 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3014 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3015 i2dest_killed = dead_or_set_p (i2, i2dest);
3016 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3017 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3019 /* For the earlier insns, determine which of the subsequent ones they
3020 feed. */
3021 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3022 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3023 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3024 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3025 && reg_overlap_mentioned_p (i0dest, i2src))));
3027 /* Ensure that I3's pattern can be the destination of combines. */
3028 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3029 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3030 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3031 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3032 &i3dest_killed))
3034 undo_all ();
3035 return 0;
3038 /* See if any of the insns is a MULT operation. Unless one is, we will
3039 reject a combination that is, since it must be slower. Be conservative
3040 here. */
3041 if (GET_CODE (i2src) == MULT
3042 || (i1 != 0 && GET_CODE (i1src) == MULT)
3043 || (i0 != 0 && GET_CODE (i0src) == MULT)
3044 || (GET_CODE (PATTERN (i3)) == SET
3045 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3046 have_mult = 1;
3048 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3049 We used to do this EXCEPT in one case: I3 has a post-inc in an
3050 output operand. However, that exception can give rise to insns like
3051 mov r3,(r3)+
3052 which is a famous insn on the PDP-11 where the value of r3 used as the
3053 source was model-dependent. Avoid this sort of thing. */
3055 #if 0
3056 if (!(GET_CODE (PATTERN (i3)) == SET
3057 && REG_P (SET_SRC (PATTERN (i3)))
3058 && MEM_P (SET_DEST (PATTERN (i3)))
3059 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3060 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3061 /* It's not the exception. */
3062 #endif
3063 if (AUTO_INC_DEC)
3065 rtx link;
3066 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3067 if (REG_NOTE_KIND (link) == REG_INC
3068 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3069 || (i1 != 0
3070 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3072 undo_all ();
3073 return 0;
3077 /* See if the SETs in I1 or I2 need to be kept around in the merged
3078 instruction: whenever the value set there is still needed past I3.
3079 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3081 For the SET in I1, we have two cases: if I1 and I2 independently feed
3082 into I3, the set in I1 needs to be kept around unless I1DEST dies
3083 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3084 in I1 needs to be kept around unless I1DEST dies or is set in either
3085 I2 or I3. The same considerations apply to I0. */
3087 added_sets_2 = !dead_or_set_p (i3, i2dest);
3089 if (i1)
3090 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3091 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3092 else
3093 added_sets_1 = 0;
3095 if (i0)
3096 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3097 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3098 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3099 && dead_or_set_p (i2, i0dest)));
3100 else
3101 added_sets_0 = 0;
3103 /* We are about to copy insns for the case where they need to be kept
3104 around. Check that they can be copied in the merged instruction. */
3106 if (targetm.cannot_copy_insn_p
3107 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3108 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3109 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3111 undo_all ();
3112 return 0;
3115 /* If the set in I2 needs to be kept around, we must make a copy of
3116 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3117 PATTERN (I2), we are only substituting for the original I1DEST, not into
3118 an already-substituted copy. This also prevents making self-referential
3119 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3120 I2DEST. */
3122 if (added_sets_2)
3124 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3125 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3126 else
3127 i2pat = copy_rtx (PATTERN (i2));
3130 if (added_sets_1)
3132 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3133 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3134 else
3135 i1pat = copy_rtx (PATTERN (i1));
3138 if (added_sets_0)
3140 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3141 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3142 else
3143 i0pat = copy_rtx (PATTERN (i0));
3146 combine_merges++;
3148 /* Substitute in the latest insn for the regs set by the earlier ones. */
3150 maxreg = max_reg_num ();
3152 subst_insn = i3;
3154 /* Many machines that don't use CC0 have insns that can both perform an
3155 arithmetic operation and set the condition code. These operations will
3156 be represented as a PARALLEL with the first element of the vector
3157 being a COMPARE of an arithmetic operation with the constant zero.
3158 The second element of the vector will set some pseudo to the result
3159 of the same arithmetic operation. If we simplify the COMPARE, we won't
3160 match such a pattern and so will generate an extra insn. Here we test
3161 for this case, where both the comparison and the operation result are
3162 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3163 I2SRC. Later we will make the PARALLEL that contains I2. */
3165 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3166 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3167 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3168 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3170 rtx newpat_dest;
3171 rtx *cc_use_loc = NULL;
3172 rtx_insn *cc_use_insn = NULL;
3173 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3174 machine_mode compare_mode, orig_compare_mode;
3175 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3176 scalar_int_mode mode;
3178 newpat = PATTERN (i3);
3179 newpat_dest = SET_DEST (newpat);
3180 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3182 if (undobuf.other_insn == 0
3183 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3184 &cc_use_insn)))
3186 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3187 if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3188 compare_code = simplify_compare_const (compare_code, mode,
3189 op0, &op1);
3190 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3193 /* Do the rest only if op1 is const0_rtx, which may be the
3194 result of simplification. */
3195 if (op1 == const0_rtx)
3197 /* If a single use of the CC is found, prepare to modify it
3198 when SELECT_CC_MODE returns a new CC-class mode, or when
3199 the above simplify_compare_const() returned a new comparison
3200 operator. undobuf.other_insn is assigned the CC use insn
3201 when modifying it. */
3202 if (cc_use_loc)
3204 #ifdef SELECT_CC_MODE
3205 machine_mode new_mode
3206 = SELECT_CC_MODE (compare_code, op0, op1);
3207 if (new_mode != orig_compare_mode
3208 && can_change_dest_mode (SET_DEST (newpat),
3209 added_sets_2, new_mode))
3211 unsigned int regno = REGNO (newpat_dest);
3212 compare_mode = new_mode;
3213 if (regno < FIRST_PSEUDO_REGISTER)
3214 newpat_dest = gen_rtx_REG (compare_mode, regno);
3215 else
3217 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3218 newpat_dest = regno_reg_rtx[regno];
3221 #endif
3222 /* Cases for modifying the CC-using comparison. */
3223 if (compare_code != orig_compare_code
3224 /* ??? Do we need to verify the zero rtx? */
3225 && XEXP (*cc_use_loc, 1) == const0_rtx)
3227 /* Replace cc_use_loc with entire new RTX. */
3228 SUBST (*cc_use_loc,
3229 gen_rtx_fmt_ee (compare_code, compare_mode,
3230 newpat_dest, const0_rtx));
3231 undobuf.other_insn = cc_use_insn;
3233 else if (compare_mode != orig_compare_mode)
3235 /* Just replace the CC reg with a new mode. */
3236 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3237 undobuf.other_insn = cc_use_insn;
3241 /* Now we modify the current newpat:
3242 First, SET_DEST(newpat) is updated if the CC mode has been
3243 altered. For targets without SELECT_CC_MODE, this should be
3244 optimized away. */
3245 if (compare_mode != orig_compare_mode)
3246 SUBST (SET_DEST (newpat), newpat_dest);
3247 /* This is always done to propagate i2src into newpat. */
3248 SUBST (SET_SRC (newpat),
3249 gen_rtx_COMPARE (compare_mode, op0, op1));
3250 /* Create new version of i2pat if needed; the below PARALLEL
3251 creation needs this to work correctly. */
3252 if (! rtx_equal_p (i2src, op0))
3253 i2pat = gen_rtx_SET (i2dest, op0);
3254 i2_is_used = 1;
3258 if (i2_is_used == 0)
3260 /* It is possible that the source of I2 or I1 may be performing
3261 an unneeded operation, such as a ZERO_EXTEND of something
3262 that is known to have the high part zero. Handle that case
3263 by letting subst look at the inner insns.
3265 Another way to do this would be to have a function that tries
3266 to simplify a single insn instead of merging two or more
3267 insns. We don't do this because of the potential of infinite
3268 loops and because of the potential extra memory required.
3269 However, doing it the way we are is a bit of a kludge and
3270 doesn't catch all cases.
3272 But only do this if -fexpensive-optimizations since it slows
3273 things down and doesn't usually win.
3275 This is not done in the COMPARE case above because the
3276 unmodified I2PAT is used in the PARALLEL and so a pattern
3277 with a modified I2SRC would not match. */
3279 if (flag_expensive_optimizations)
3281 /* Pass pc_rtx so no substitutions are done, just
3282 simplifications. */
3283 if (i1)
3285 subst_low_luid = DF_INSN_LUID (i1);
3286 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3289 subst_low_luid = DF_INSN_LUID (i2);
3290 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3293 n_occurrences = 0; /* `subst' counts here */
3294 subst_low_luid = DF_INSN_LUID (i2);
3296 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3297 copy of I2SRC each time we substitute it, in order to avoid creating
3298 self-referential RTL when we will be substituting I1SRC for I1DEST
3299 later. Likewise if I0 feeds into I2, either directly or indirectly
3300 through I1, and I0DEST is in I0SRC. */
3301 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3302 (i1_feeds_i2_n && i1dest_in_i1src)
3303 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3304 && i0dest_in_i0src));
3305 substed_i2 = 1;
3307 /* Record whether I2's body now appears within I3's body. */
3308 i2_is_used = n_occurrences;
3311 /* If we already got a failure, don't try to do more. Otherwise, try to
3312 substitute I1 if we have it. */
3314 if (i1 && GET_CODE (newpat) != CLOBBER)
3316 /* Check that an autoincrement side-effect on I1 has not been lost.
3317 This happens if I1DEST is mentioned in I2 and dies there, and
3318 has disappeared from the new pattern. */
3319 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3320 && i1_feeds_i2_n
3321 && dead_or_set_p (i2, i1dest)
3322 && !reg_overlap_mentioned_p (i1dest, newpat))
3323 /* Before we can do this substitution, we must redo the test done
3324 above (see detailed comments there) that ensures I1DEST isn't
3325 mentioned in any SETs in NEWPAT that are field assignments. */
3326 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3327 0, 0, 0))
3329 undo_all ();
3330 return 0;
3333 n_occurrences = 0;
3334 subst_low_luid = DF_INSN_LUID (i1);
3336 /* If the following substitution will modify I1SRC, make a copy of it
3337 for the case where it is substituted for I1DEST in I2PAT later. */
3338 if (added_sets_2 && i1_feeds_i2_n)
3339 i1src_copy = copy_rtx (i1src);
3341 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3342 copy of I1SRC each time we substitute it, in order to avoid creating
3343 self-referential RTL when we will be substituting I0SRC for I0DEST
3344 later. */
3345 newpat = subst (newpat, i1dest, i1src, 0, 0,
3346 i0_feeds_i1_n && i0dest_in_i0src);
3347 substed_i1 = 1;
3349 /* Record whether I1's body now appears within I3's body. */
3350 i1_is_used = n_occurrences;
3353 /* Likewise for I0 if we have it. */
3355 if (i0 && GET_CODE (newpat) != CLOBBER)
3357 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3358 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3359 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3360 && !reg_overlap_mentioned_p (i0dest, newpat))
3361 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3362 0, 0, 0))
3364 undo_all ();
3365 return 0;
3368 /* If the following substitution will modify I0SRC, make a copy of it
3369 for the case where it is substituted for I0DEST in I1PAT later. */
3370 if (added_sets_1 && i0_feeds_i1_n)
3371 i0src_copy = copy_rtx (i0src);
3372 /* And a copy for I0DEST in I2PAT substitution. */
3373 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3374 || (i0_feeds_i2_n)))
3375 i0src_copy2 = copy_rtx (i0src);
3377 n_occurrences = 0;
3378 subst_low_luid = DF_INSN_LUID (i0);
3379 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3380 substed_i0 = 1;
3383 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3384 to count all the ways that I2SRC and I1SRC can be used. */
3385 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3386 && i2_is_used + added_sets_2 > 1)
3387 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3388 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3389 > 1))
3390 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3391 && (n_occurrences + added_sets_0
3392 + (added_sets_1 && i0_feeds_i1_n)
3393 + (added_sets_2 && i0_feeds_i2_n)
3394 > 1))
3395 /* Fail if we tried to make a new register. */
3396 || max_reg_num () != maxreg
3397 /* Fail if we couldn't do something and have a CLOBBER. */
3398 || GET_CODE (newpat) == CLOBBER
3399 /* Fail if this new pattern is a MULT and we didn't have one before
3400 at the outer level. */
3401 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3402 && ! have_mult))
3404 undo_all ();
3405 return 0;
3408 /* If the actions of the earlier insns must be kept
3409 in addition to substituting them into the latest one,
3410 we must make a new PARALLEL for the latest insn
3411 to hold additional the SETs. */
3413 if (added_sets_0 || added_sets_1 || added_sets_2)
3415 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3416 combine_extras++;
3418 if (GET_CODE (newpat) == PARALLEL)
3420 rtvec old = XVEC (newpat, 0);
3421 total_sets = XVECLEN (newpat, 0) + extra_sets;
3422 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3423 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3424 sizeof (old->elem[0]) * old->num_elem);
3426 else
3428 rtx old = newpat;
3429 total_sets = 1 + extra_sets;
3430 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3431 XVECEXP (newpat, 0, 0) = old;
3434 if (added_sets_0)
3435 XVECEXP (newpat, 0, --total_sets) = i0pat;
3437 if (added_sets_1)
3439 rtx t = i1pat;
3440 if (i0_feeds_i1_n)
3441 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3443 XVECEXP (newpat, 0, --total_sets) = t;
3445 if (added_sets_2)
3447 rtx t = i2pat;
3448 if (i1_feeds_i2_n)
3449 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3450 i0_feeds_i1_n && i0dest_in_i0src);
3451 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3452 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3454 XVECEXP (newpat, 0, --total_sets) = t;
3458 validate_replacement:
3460 /* Note which hard regs this insn has as inputs. */
3461 mark_used_regs_combine (newpat);
3463 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3464 consider splitting this pattern, we might need these clobbers. */
3465 if (i1 && GET_CODE (newpat) == PARALLEL
3466 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3468 int len = XVECLEN (newpat, 0);
3470 newpat_vec_with_clobbers = rtvec_alloc (len);
3471 for (i = 0; i < len; i++)
3472 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3475 /* We have recognized nothing yet. */
3476 insn_code_number = -1;
3478 /* See if this is a PARALLEL of two SETs where one SET's destination is
3479 a register that is unused and this isn't marked as an instruction that
3480 might trap in an EH region. In that case, we just need the other SET.
3481 We prefer this over the PARALLEL.
3483 This can occur when simplifying a divmod insn. We *must* test for this
3484 case here because the code below that splits two independent SETs doesn't
3485 handle this case correctly when it updates the register status.
3487 It's pointless doing this if we originally had two sets, one from
3488 i3, and one from i2. Combining then splitting the parallel results
3489 in the original i2 again plus an invalid insn (which we delete).
3490 The net effect is only to move instructions around, which makes
3491 debug info less accurate.
3493 If the remaining SET came from I2 its destination should not be used
3494 between I2 and I3. See PR82024. */
3496 if (!(added_sets_2 && i1 == 0)
3497 && is_parallel_of_n_reg_sets (newpat, 2)
3498 && asm_noperands (newpat) < 0)
3500 rtx set0 = XVECEXP (newpat, 0, 0);
3501 rtx set1 = XVECEXP (newpat, 0, 1);
3502 rtx oldpat = newpat;
3504 if (((REG_P (SET_DEST (set1))
3505 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3506 || (GET_CODE (SET_DEST (set1)) == SUBREG
3507 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3508 && insn_nothrow_p (i3)
3509 && !side_effects_p (SET_SRC (set1)))
3511 newpat = set0;
3512 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3515 else if (((REG_P (SET_DEST (set0))
3516 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3517 || (GET_CODE (SET_DEST (set0)) == SUBREG
3518 && find_reg_note (i3, REG_UNUSED,
3519 SUBREG_REG (SET_DEST (set0)))))
3520 && insn_nothrow_p (i3)
3521 && !side_effects_p (SET_SRC (set0)))
3523 rtx dest = SET_DEST (set1);
3524 if (GET_CODE (dest) == SUBREG)
3525 dest = SUBREG_REG (dest);
3526 if (!reg_used_between_p (dest, i2, i3))
3528 newpat = set1;
3529 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3531 if (insn_code_number >= 0)
3532 changed_i3_dest = 1;
3536 if (insn_code_number < 0)
3537 newpat = oldpat;
3540 /* Is the result of combination a valid instruction? */
3541 if (insn_code_number < 0)
3542 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3544 /* If we were combining three insns and the result is a simple SET
3545 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3546 insns. There are two ways to do this. It can be split using a
3547 machine-specific method (like when you have an addition of a large
3548 constant) or by combine in the function find_split_point. */
3550 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3551 && asm_noperands (newpat) < 0)
3553 rtx parallel, *split;
3554 rtx_insn *m_split_insn;
3556 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3557 use I2DEST as a scratch register will help. In the latter case,
3558 convert I2DEST to the mode of the source of NEWPAT if we can. */
3560 m_split_insn = combine_split_insns (newpat, i3);
3562 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3563 inputs of NEWPAT. */
3565 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3566 possible to try that as a scratch reg. This would require adding
3567 more code to make it work though. */
3569 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3571 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3573 /* ??? Reusing i2dest without resetting the reg_stat entry for it
3574 (temporarily, until we are committed to this instruction
3575 combination) does not work: for example, any call to nonzero_bits
3576 on the register (from a splitter in the MD file, for example)
3577 will get the old information, which is invalid.
3579 Since nowadays we can create registers during combine just fine,
3580 we should just create a new one here, not reuse i2dest. */
3582 /* First try to split using the original register as a
3583 scratch register. */
3584 parallel = gen_rtx_PARALLEL (VOIDmode,
3585 gen_rtvec (2, newpat,
3586 gen_rtx_CLOBBER (VOIDmode,
3587 i2dest)));
3588 m_split_insn = combine_split_insns (parallel, i3);
3590 /* If that didn't work, try changing the mode of I2DEST if
3591 we can. */
3592 if (m_split_insn == 0
3593 && new_mode != GET_MODE (i2dest)
3594 && new_mode != VOIDmode
3595 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3597 machine_mode old_mode = GET_MODE (i2dest);
3598 rtx ni2dest;
3600 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3601 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3602 else
3604 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3605 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3608 parallel = (gen_rtx_PARALLEL
3609 (VOIDmode,
3610 gen_rtvec (2, newpat,
3611 gen_rtx_CLOBBER (VOIDmode,
3612 ni2dest))));
3613 m_split_insn = combine_split_insns (parallel, i3);
3615 if (m_split_insn == 0
3616 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3618 struct undo *buf;
3620 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3621 buf = undobuf.undos;
3622 undobuf.undos = buf->next;
3623 buf->next = undobuf.frees;
3624 undobuf.frees = buf;
3628 i2scratch = m_split_insn != 0;
3631 /* If recog_for_combine has discarded clobbers, try to use them
3632 again for the split. */
3633 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3635 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3636 m_split_insn = combine_split_insns (parallel, i3);
3639 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3641 rtx m_split_pat = PATTERN (m_split_insn);
3642 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3643 if (insn_code_number >= 0)
3644 newpat = m_split_pat;
3646 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3647 && (next_nonnote_nondebug_insn (i2) == i3
3648 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3650 rtx i2set, i3set;
3651 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3652 newi2pat = PATTERN (m_split_insn);
3654 i3set = single_set (NEXT_INSN (m_split_insn));
3655 i2set = single_set (m_split_insn);
3657 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3659 /* If I2 or I3 has multiple SETs, we won't know how to track
3660 register status, so don't use these insns. If I2's destination
3661 is used between I2 and I3, we also can't use these insns. */
3663 if (i2_code_number >= 0 && i2set && i3set
3664 && (next_nonnote_nondebug_insn (i2) == i3
3665 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3666 insn_code_number = recog_for_combine (&newi3pat, i3,
3667 &new_i3_notes);
3668 if (insn_code_number >= 0)
3669 newpat = newi3pat;
3671 /* It is possible that both insns now set the destination of I3.
3672 If so, we must show an extra use of it. */
3674 if (insn_code_number >= 0)
3676 rtx new_i3_dest = SET_DEST (i3set);
3677 rtx new_i2_dest = SET_DEST (i2set);
3679 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3680 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3681 || GET_CODE (new_i3_dest) == SUBREG)
3682 new_i3_dest = XEXP (new_i3_dest, 0);
3684 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3685 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3686 || GET_CODE (new_i2_dest) == SUBREG)
3687 new_i2_dest = XEXP (new_i2_dest, 0);
3689 if (REG_P (new_i3_dest)
3690 && REG_P (new_i2_dest)
3691 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3692 && REGNO (new_i2_dest) < reg_n_sets_max)
3693 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3697 /* If we can split it and use I2DEST, go ahead and see if that
3698 helps things be recognized. Verify that none of the registers
3699 are set between I2 and I3. */
3700 if (insn_code_number < 0
3701 && (split = find_split_point (&newpat, i3, false)) != 0
3702 && (!HAVE_cc0 || REG_P (i2dest))
3703 /* We need I2DEST in the proper mode. If it is a hard register
3704 or the only use of a pseudo, we can change its mode.
3705 Make sure we don't change a hard register to have a mode that
3706 isn't valid for it, or change the number of registers. */
3707 && (GET_MODE (*split) == GET_MODE (i2dest)
3708 || GET_MODE (*split) == VOIDmode
3709 || can_change_dest_mode (i2dest, added_sets_2,
3710 GET_MODE (*split)))
3711 && (next_nonnote_nondebug_insn (i2) == i3
3712 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3713 /* We can't overwrite I2DEST if its value is still used by
3714 NEWPAT. */
3715 && ! reg_referenced_p (i2dest, newpat))
3717 rtx newdest = i2dest;
3718 enum rtx_code split_code = GET_CODE (*split);
3719 machine_mode split_mode = GET_MODE (*split);
3720 bool subst_done = false;
3721 newi2pat = NULL_RTX;
3723 i2scratch = true;
3725 /* *SPLIT may be part of I2SRC, so make sure we have the
3726 original expression around for later debug processing.
3727 We should not need I2SRC any more in other cases. */
3728 if (MAY_HAVE_DEBUG_INSNS)
3729 i2src = copy_rtx (i2src);
3730 else
3731 i2src = NULL;
3733 /* Get NEWDEST as a register in the proper mode. We have already
3734 validated that we can do this. */
3735 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3737 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3738 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3739 else
3741 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3742 newdest = regno_reg_rtx[REGNO (i2dest)];
3746 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3747 an ASHIFT. This can occur if it was inside a PLUS and hence
3748 appeared to be a memory address. This is a kludge. */
3749 if (split_code == MULT
3750 && CONST_INT_P (XEXP (*split, 1))
3751 && INTVAL (XEXP (*split, 1)) > 0
3752 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3754 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3755 XEXP (*split, 0), GEN_INT (i)));
3756 /* Update split_code because we may not have a multiply
3757 anymore. */
3758 split_code = GET_CODE (*split);
3761 /* Similarly for (plus (mult FOO (const_int pow2))). */
3762 if (split_code == PLUS
3763 && GET_CODE (XEXP (*split, 0)) == MULT
3764 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3765 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3766 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3768 rtx nsplit = XEXP (*split, 0);
3769 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3770 XEXP (nsplit, 0), GEN_INT (i)));
3771 /* Update split_code because we may not have a multiply
3772 anymore. */
3773 split_code = GET_CODE (*split);
3776 #ifdef INSN_SCHEDULING
3777 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3778 be written as a ZERO_EXTEND. */
3779 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3781 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3782 what it really is. */
3783 if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3784 == SIGN_EXTEND)
3785 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3786 SUBREG_REG (*split)));
3787 else
3788 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3789 SUBREG_REG (*split)));
3791 #endif
3793 /* Attempt to split binary operators using arithmetic identities. */
3794 if (BINARY_P (SET_SRC (newpat))
3795 && split_mode == GET_MODE (SET_SRC (newpat))
3796 && ! side_effects_p (SET_SRC (newpat)))
3798 rtx setsrc = SET_SRC (newpat);
3799 machine_mode mode = GET_MODE (setsrc);
3800 enum rtx_code code = GET_CODE (setsrc);
3801 rtx src_op0 = XEXP (setsrc, 0);
3802 rtx src_op1 = XEXP (setsrc, 1);
3804 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3805 if (rtx_equal_p (src_op0, src_op1))
3807 newi2pat = gen_rtx_SET (newdest, src_op0);
3808 SUBST (XEXP (setsrc, 0), newdest);
3809 SUBST (XEXP (setsrc, 1), newdest);
3810 subst_done = true;
3812 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3813 else if ((code == PLUS || code == MULT)
3814 && GET_CODE (src_op0) == code
3815 && GET_CODE (XEXP (src_op0, 0)) == code
3816 && (INTEGRAL_MODE_P (mode)
3817 || (FLOAT_MODE_P (mode)
3818 && flag_unsafe_math_optimizations)))
3820 rtx p = XEXP (XEXP (src_op0, 0), 0);
3821 rtx q = XEXP (XEXP (src_op0, 0), 1);
3822 rtx r = XEXP (src_op0, 1);
3823 rtx s = src_op1;
3825 /* Split both "((X op Y) op X) op Y" and
3826 "((X op Y) op Y) op X" as "T op T" where T is
3827 "X op Y". */
3828 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3829 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3831 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3832 SUBST (XEXP (setsrc, 0), newdest);
3833 SUBST (XEXP (setsrc, 1), newdest);
3834 subst_done = true;
3836 /* Split "((X op X) op Y) op Y)" as "T op T" where
3837 T is "X op Y". */
3838 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3840 rtx tmp = simplify_gen_binary (code, mode, p, r);
3841 newi2pat = gen_rtx_SET (newdest, tmp);
3842 SUBST (XEXP (setsrc, 0), newdest);
3843 SUBST (XEXP (setsrc, 1), newdest);
3844 subst_done = true;
3849 if (!subst_done)
3851 newi2pat = gen_rtx_SET (newdest, *split);
3852 SUBST (*split, newdest);
3855 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3857 /* recog_for_combine might have added CLOBBERs to newi2pat.
3858 Make sure NEWPAT does not depend on the clobbered regs. */
3859 if (GET_CODE (newi2pat) == PARALLEL)
3860 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3861 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3863 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3864 if (reg_overlap_mentioned_p (reg, newpat))
3866 undo_all ();
3867 return 0;
3871 /* If the split point was a MULT and we didn't have one before,
3872 don't use one now. */
3873 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3874 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3878 /* Check for a case where we loaded from memory in a narrow mode and
3879 then sign extended it, but we need both registers. In that case,
3880 we have a PARALLEL with both loads from the same memory location.
3881 We can split this into a load from memory followed by a register-register
3882 copy. This saves at least one insn, more if register allocation can
3883 eliminate the copy.
3885 We cannot do this if the destination of the first assignment is a
3886 condition code register or cc0. We eliminate this case by making sure
3887 the SET_DEST and SET_SRC have the same mode.
3889 We cannot do this if the destination of the second assignment is
3890 a register that we have already assumed is zero-extended. Similarly
3891 for a SUBREG of such a register. */
3893 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3894 && GET_CODE (newpat) == PARALLEL
3895 && XVECLEN (newpat, 0) == 2
3896 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3897 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3898 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3899 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3900 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3901 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3902 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3903 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3904 DF_INSN_LUID (i2))
3905 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3906 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3907 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3908 (REG_P (temp_expr)
3909 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3910 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3911 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3912 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3913 != GET_MODE_MASK (word_mode))))
3914 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3915 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3916 (REG_P (temp_expr)
3917 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3918 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3919 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3920 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3921 != GET_MODE_MASK (word_mode)))))
3922 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3923 SET_SRC (XVECEXP (newpat, 0, 1)))
3924 && ! find_reg_note (i3, REG_UNUSED,
3925 SET_DEST (XVECEXP (newpat, 0, 0))))
3927 rtx ni2dest;
3929 newi2pat = XVECEXP (newpat, 0, 0);
3930 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3931 newpat = XVECEXP (newpat, 0, 1);
3932 SUBST (SET_SRC (newpat),
3933 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3934 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3936 if (i2_code_number >= 0)
3937 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3939 if (insn_code_number >= 0)
3940 swap_i2i3 = 1;
3943 /* Similarly, check for a case where we have a PARALLEL of two independent
3944 SETs but we started with three insns. In this case, we can do the sets
3945 as two separate insns. This case occurs when some SET allows two
3946 other insns to combine, but the destination of that SET is still live.
3948 Also do this if we started with two insns and (at least) one of the
3949 resulting sets is a noop; this noop will be deleted later. */
3951 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3952 && GET_CODE (newpat) == PARALLEL
3953 && XVECLEN (newpat, 0) == 2
3954 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3955 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3956 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3957 || set_noop_p (XVECEXP (newpat, 0, 1)))
3958 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3959 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3960 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3961 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3962 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3963 XVECEXP (newpat, 0, 0))
3964 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3965 XVECEXP (newpat, 0, 1))
3966 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3967 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3969 rtx set0 = XVECEXP (newpat, 0, 0);
3970 rtx set1 = XVECEXP (newpat, 0, 1);
3972 /* Normally, it doesn't matter which of the two is done first,
3973 but the one that references cc0 can't be the second, and
3974 one which uses any regs/memory set in between i2 and i3 can't
3975 be first. The PARALLEL might also have been pre-existing in i3,
3976 so we need to make sure that we won't wrongly hoist a SET to i2
3977 that would conflict with a death note present in there. */
3978 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3979 && !(REG_P (SET_DEST (set1))
3980 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3981 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3982 && find_reg_note (i2, REG_DEAD,
3983 SUBREG_REG (SET_DEST (set1))))
3984 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
3985 /* If I3 is a jump, ensure that set0 is a jump so that
3986 we do not create invalid RTL. */
3987 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3990 newi2pat = set1;
3991 newpat = set0;
3993 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3994 && !(REG_P (SET_DEST (set0))
3995 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3996 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3997 && find_reg_note (i2, REG_DEAD,
3998 SUBREG_REG (SET_DEST (set0))))
3999 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
4000 /* If I3 is a jump, ensure that set1 is a jump so that
4001 we do not create invalid RTL. */
4002 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4005 newi2pat = set0;
4006 newpat = set1;
4008 else
4010 undo_all ();
4011 return 0;
4014 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4016 if (i2_code_number >= 0)
4018 /* recog_for_combine might have added CLOBBERs to newi2pat.
4019 Make sure NEWPAT does not depend on the clobbered regs. */
4020 if (GET_CODE (newi2pat) == PARALLEL)
4022 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4023 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4025 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4026 if (reg_overlap_mentioned_p (reg, newpat))
4028 undo_all ();
4029 return 0;
4034 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4038 /* If it still isn't recognized, fail and change things back the way they
4039 were. */
4040 if ((insn_code_number < 0
4041 /* Is the result a reasonable ASM_OPERANDS? */
4042 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4044 undo_all ();
4045 return 0;
4048 /* If we had to change another insn, make sure it is valid also. */
4049 if (undobuf.other_insn)
4051 CLEAR_HARD_REG_SET (newpat_used_regs);
4053 other_pat = PATTERN (undobuf.other_insn);
4054 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4055 &new_other_notes);
4057 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4059 undo_all ();
4060 return 0;
4064 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4065 they are adjacent to each other or not. */
4066 if (HAVE_cc0)
4068 rtx_insn *p = prev_nonnote_insn (i3);
4069 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4070 && sets_cc0_p (newi2pat))
4072 undo_all ();
4073 return 0;
4077 /* Only allow this combination if insn_rtx_costs reports that the
4078 replacement instructions are cheaper than the originals. */
4079 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4081 undo_all ();
4082 return 0;
4085 if (MAY_HAVE_DEBUG_INSNS)
4087 struct undo *undo;
4089 for (undo = undobuf.undos; undo; undo = undo->next)
4090 if (undo->kind == UNDO_MODE)
4092 rtx reg = *undo->where.r;
4093 machine_mode new_mode = GET_MODE (reg);
4094 machine_mode old_mode = undo->old_contents.m;
4096 /* Temporarily revert mode back. */
4097 adjust_reg_mode (reg, old_mode);
4099 if (reg == i2dest && i2scratch)
4101 /* If we used i2dest as a scratch register with a
4102 different mode, substitute it for the original
4103 i2src while its original mode is temporarily
4104 restored, and then clear i2scratch so that we don't
4105 do it again later. */
4106 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4107 this_basic_block);
4108 i2scratch = false;
4109 /* Put back the new mode. */
4110 adjust_reg_mode (reg, new_mode);
4112 else
4114 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4115 rtx_insn *first, *last;
4117 if (reg == i2dest)
4119 first = i2;
4120 last = last_combined_insn;
4122 else
4124 first = i3;
4125 last = undobuf.other_insn;
4126 gcc_assert (last);
4127 if (DF_INSN_LUID (last)
4128 < DF_INSN_LUID (last_combined_insn))
4129 last = last_combined_insn;
4132 /* We're dealing with a reg that changed mode but not
4133 meaning, so we want to turn it into a subreg for
4134 the new mode. However, because of REG sharing and
4135 because its mode had already changed, we have to do
4136 it in two steps. First, replace any debug uses of
4137 reg, with its original mode temporarily restored,
4138 with this copy we have created; then, replace the
4139 copy with the SUBREG of the original shared reg,
4140 once again changed to the new mode. */
4141 propagate_for_debug (first, last, reg, tempreg,
4142 this_basic_block);
4143 adjust_reg_mode (reg, new_mode);
4144 propagate_for_debug (first, last, tempreg,
4145 lowpart_subreg (old_mode, reg, new_mode),
4146 this_basic_block);
4151 /* If we will be able to accept this, we have made a
4152 change to the destination of I3. This requires us to
4153 do a few adjustments. */
4155 if (changed_i3_dest)
4157 PATTERN (i3) = newpat;
4158 adjust_for_new_dest (i3);
4161 /* We now know that we can do this combination. Merge the insns and
4162 update the status of registers and LOG_LINKS. */
4164 if (undobuf.other_insn)
4166 rtx note, next;
4168 PATTERN (undobuf.other_insn) = other_pat;
4170 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4171 ensure that they are still valid. Then add any non-duplicate
4172 notes added by recog_for_combine. */
4173 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4175 next = XEXP (note, 1);
4177 if ((REG_NOTE_KIND (note) == REG_DEAD
4178 && !reg_referenced_p (XEXP (note, 0),
4179 PATTERN (undobuf.other_insn)))
4180 ||(REG_NOTE_KIND (note) == REG_UNUSED
4181 && !reg_set_p (XEXP (note, 0),
4182 PATTERN (undobuf.other_insn)))
4183 /* Simply drop equal note since it may be no longer valid
4184 for other_insn. It may be possible to record that CC
4185 register is changed and only discard those notes, but
4186 in practice it's unnecessary complication and doesn't
4187 give any meaningful improvement.
4189 See PR78559. */
4190 || REG_NOTE_KIND (note) == REG_EQUAL
4191 || REG_NOTE_KIND (note) == REG_EQUIV)
4192 remove_note (undobuf.other_insn, note);
4195 distribute_notes (new_other_notes, undobuf.other_insn,
4196 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4197 NULL_RTX);
4200 if (swap_i2i3)
4202 rtx_insn *insn;
4203 struct insn_link *link;
4204 rtx ni2dest;
4206 /* I3 now uses what used to be its destination and which is now
4207 I2's destination. This requires us to do a few adjustments. */
4208 PATTERN (i3) = newpat;
4209 adjust_for_new_dest (i3);
4211 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4212 so we still will.
4214 However, some later insn might be using I2's dest and have
4215 a LOG_LINK pointing at I3. We must remove this link.
4216 The simplest way to remove the link is to point it at I1,
4217 which we know will be a NOTE. */
4219 /* newi2pat is usually a SET here; however, recog_for_combine might
4220 have added some clobbers. */
4221 if (GET_CODE (newi2pat) == PARALLEL)
4222 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4223 else
4224 ni2dest = SET_DEST (newi2pat);
4226 for (insn = NEXT_INSN (i3);
4227 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4228 || insn != BB_HEAD (this_basic_block->next_bb));
4229 insn = NEXT_INSN (insn))
4231 if (NONDEBUG_INSN_P (insn)
4232 && reg_referenced_p (ni2dest, PATTERN (insn)))
4234 FOR_EACH_LOG_LINK (link, insn)
4235 if (link->insn == i3)
4236 link->insn = i1;
4238 break;
4244 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4245 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4246 rtx midnotes = 0;
4247 int from_luid;
4248 /* Compute which registers we expect to eliminate. newi2pat may be setting
4249 either i3dest or i2dest, so we must check it. */
4250 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4251 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4252 || !i2dest_killed
4253 ? 0 : i2dest);
4254 /* For i1, we need to compute both local elimination and global
4255 elimination information with respect to newi2pat because i1dest
4256 may be the same as i3dest, in which case newi2pat may be setting
4257 i1dest. Global information is used when distributing REG_DEAD
4258 note for i2 and i3, in which case it does matter if newi2pat sets
4259 i1dest or not.
4261 Local information is used when distributing REG_DEAD note for i1,
4262 in which case it doesn't matter if newi2pat sets i1dest or not.
4263 See PR62151, if we have four insns combination:
4264 i0: r0 <- i0src
4265 i1: r1 <- i1src (using r0)
4266 REG_DEAD (r0)
4267 i2: r0 <- i2src (using r1)
4268 i3: r3 <- i3src (using r0)
4269 ix: using r0
4270 From i1's point of view, r0 is eliminated, no matter if it is set
4271 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4272 should be discarded.
4274 Note local information only affects cases in forms like "I1->I2->I3",
4275 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4276 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4277 i0dest anyway. */
4278 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4279 || !i1dest_killed
4280 ? 0 : i1dest);
4281 rtx elim_i1 = (local_elim_i1 == 0
4282 || (newi2pat && reg_set_p (i1dest, newi2pat))
4283 ? 0 : i1dest);
4284 /* Same case as i1. */
4285 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4286 ? 0 : i0dest);
4287 rtx elim_i0 = (local_elim_i0 == 0
4288 || (newi2pat && reg_set_p (i0dest, newi2pat))
4289 ? 0 : i0dest);
4291 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4292 clear them. */
4293 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4294 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4295 if (i1)
4296 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4297 if (i0)
4298 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4300 /* Ensure that we do not have something that should not be shared but
4301 occurs multiple times in the new insns. Check this by first
4302 resetting all the `used' flags and then copying anything is shared. */
4304 reset_used_flags (i3notes);
4305 reset_used_flags (i2notes);
4306 reset_used_flags (i1notes);
4307 reset_used_flags (i0notes);
4308 reset_used_flags (newpat);
4309 reset_used_flags (newi2pat);
4310 if (undobuf.other_insn)
4311 reset_used_flags (PATTERN (undobuf.other_insn));
4313 i3notes = copy_rtx_if_shared (i3notes);
4314 i2notes = copy_rtx_if_shared (i2notes);
4315 i1notes = copy_rtx_if_shared (i1notes);
4316 i0notes = copy_rtx_if_shared (i0notes);
4317 newpat = copy_rtx_if_shared (newpat);
4318 newi2pat = copy_rtx_if_shared (newi2pat);
4319 if (undobuf.other_insn)
4320 reset_used_flags (PATTERN (undobuf.other_insn));
4322 INSN_CODE (i3) = insn_code_number;
4323 PATTERN (i3) = newpat;
4325 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4327 for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4328 link = XEXP (link, 1))
4330 if (substed_i2)
4332 /* I2SRC must still be meaningful at this point. Some
4333 splitting operations can invalidate I2SRC, but those
4334 operations do not apply to calls. */
4335 gcc_assert (i2src);
4336 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4337 i2dest, i2src);
4339 if (substed_i1)
4340 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4341 i1dest, i1src);
4342 if (substed_i0)
4343 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4344 i0dest, i0src);
4348 if (undobuf.other_insn)
4349 INSN_CODE (undobuf.other_insn) = other_code_number;
4351 /* We had one special case above where I2 had more than one set and
4352 we replaced a destination of one of those sets with the destination
4353 of I3. In that case, we have to update LOG_LINKS of insns later
4354 in this basic block. Note that this (expensive) case is rare.
4356 Also, in this case, we must pretend that all REG_NOTEs for I2
4357 actually came from I3, so that REG_UNUSED notes from I2 will be
4358 properly handled. */
4360 if (i3_subst_into_i2)
4362 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4363 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4364 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4365 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4366 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4367 && ! find_reg_note (i2, REG_UNUSED,
4368 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4369 for (temp_insn = NEXT_INSN (i2);
4370 temp_insn
4371 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4372 || BB_HEAD (this_basic_block) != temp_insn);
4373 temp_insn = NEXT_INSN (temp_insn))
4374 if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4375 FOR_EACH_LOG_LINK (link, temp_insn)
4376 if (link->insn == i2)
4377 link->insn = i3;
4379 if (i3notes)
4381 rtx link = i3notes;
4382 while (XEXP (link, 1))
4383 link = XEXP (link, 1);
4384 XEXP (link, 1) = i2notes;
4386 else
4387 i3notes = i2notes;
4388 i2notes = 0;
4391 LOG_LINKS (i3) = NULL;
4392 REG_NOTES (i3) = 0;
4393 LOG_LINKS (i2) = NULL;
4394 REG_NOTES (i2) = 0;
4396 if (newi2pat)
4398 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4399 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4400 this_basic_block);
4401 INSN_CODE (i2) = i2_code_number;
4402 PATTERN (i2) = newi2pat;
4404 else
4406 if (MAY_HAVE_DEBUG_INSNS && i2src)
4407 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4408 this_basic_block);
4409 SET_INSN_DELETED (i2);
4412 if (i1)
4414 LOG_LINKS (i1) = NULL;
4415 REG_NOTES (i1) = 0;
4416 if (MAY_HAVE_DEBUG_INSNS)
4417 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4418 this_basic_block);
4419 SET_INSN_DELETED (i1);
4422 if (i0)
4424 LOG_LINKS (i0) = NULL;
4425 REG_NOTES (i0) = 0;
4426 if (MAY_HAVE_DEBUG_INSNS)
4427 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4428 this_basic_block);
4429 SET_INSN_DELETED (i0);
4432 /* Get death notes for everything that is now used in either I3 or
4433 I2 and used to die in a previous insn. If we built two new
4434 patterns, move from I1 to I2 then I2 to I3 so that we get the
4435 proper movement on registers that I2 modifies. */
4437 if (i0)
4438 from_luid = DF_INSN_LUID (i0);
4439 else if (i1)
4440 from_luid = DF_INSN_LUID (i1);
4441 else
4442 from_luid = DF_INSN_LUID (i2);
4443 if (newi2pat)
4444 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4445 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4447 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4448 if (i3notes)
4449 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4450 elim_i2, elim_i1, elim_i0);
4451 if (i2notes)
4452 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4453 elim_i2, elim_i1, elim_i0);
4454 if (i1notes)
4455 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4456 elim_i2, local_elim_i1, local_elim_i0);
4457 if (i0notes)
4458 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4459 elim_i2, elim_i1, local_elim_i0);
4460 if (midnotes)
4461 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4462 elim_i2, elim_i1, elim_i0);
4464 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4465 know these are REG_UNUSED and want them to go to the desired insn,
4466 so we always pass it as i3. */
4468 if (newi2pat && new_i2_notes)
4469 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4470 NULL_RTX);
4472 if (new_i3_notes)
4473 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4474 NULL_RTX);
4476 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4477 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4478 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4479 in that case, it might delete I2. Similarly for I2 and I1.
4480 Show an additional death due to the REG_DEAD note we make here. If
4481 we discard it in distribute_notes, we will decrement it again. */
4483 if (i3dest_killed)
4485 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4486 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4487 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4488 elim_i1, elim_i0);
4489 else
4490 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4491 elim_i2, elim_i1, elim_i0);
4494 if (i2dest_in_i2src)
4496 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4497 if (newi2pat && reg_set_p (i2dest, newi2pat))
4498 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4499 NULL_RTX, NULL_RTX);
4500 else
4501 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4502 NULL_RTX, NULL_RTX, NULL_RTX);
4505 if (i1dest_in_i1src)
4507 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4508 if (newi2pat && reg_set_p (i1dest, newi2pat))
4509 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4510 NULL_RTX, NULL_RTX);
4511 else
4512 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4513 NULL_RTX, NULL_RTX, NULL_RTX);
4516 if (i0dest_in_i0src)
4518 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4519 if (newi2pat && reg_set_p (i0dest, newi2pat))
4520 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4521 NULL_RTX, NULL_RTX);
4522 else
4523 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4524 NULL_RTX, NULL_RTX, NULL_RTX);
4527 distribute_links (i3links);
4528 distribute_links (i2links);
4529 distribute_links (i1links);
4530 distribute_links (i0links);
4532 if (REG_P (i2dest))
4534 struct insn_link *link;
4535 rtx_insn *i2_insn = 0;
4536 rtx i2_val = 0, set;
4538 /* The insn that used to set this register doesn't exist, and
4539 this life of the register may not exist either. See if one of
4540 I3's links points to an insn that sets I2DEST. If it does,
4541 that is now the last known value for I2DEST. If we don't update
4542 this and I2 set the register to a value that depended on its old
4543 contents, we will get confused. If this insn is used, thing
4544 will be set correctly in combine_instructions. */
4545 FOR_EACH_LOG_LINK (link, i3)
4546 if ((set = single_set (link->insn)) != 0
4547 && rtx_equal_p (i2dest, SET_DEST (set)))
4548 i2_insn = link->insn, i2_val = SET_SRC (set);
4550 record_value_for_reg (i2dest, i2_insn, i2_val);
4552 /* If the reg formerly set in I2 died only once and that was in I3,
4553 zero its use count so it won't make `reload' do any work. */
4554 if (! added_sets_2
4555 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4556 && ! i2dest_in_i2src
4557 && REGNO (i2dest) < reg_n_sets_max)
4558 INC_REG_N_SETS (REGNO (i2dest), -1);
4561 if (i1 && REG_P (i1dest))
4563 struct insn_link *link;
4564 rtx_insn *i1_insn = 0;
4565 rtx i1_val = 0, set;
4567 FOR_EACH_LOG_LINK (link, i3)
4568 if ((set = single_set (link->insn)) != 0
4569 && rtx_equal_p (i1dest, SET_DEST (set)))
4570 i1_insn = link->insn, i1_val = SET_SRC (set);
4572 record_value_for_reg (i1dest, i1_insn, i1_val);
4574 if (! added_sets_1
4575 && ! i1dest_in_i1src
4576 && REGNO (i1dest) < reg_n_sets_max)
4577 INC_REG_N_SETS (REGNO (i1dest), -1);
4580 if (i0 && REG_P (i0dest))
4582 struct insn_link *link;
4583 rtx_insn *i0_insn = 0;
4584 rtx i0_val = 0, set;
4586 FOR_EACH_LOG_LINK (link, i3)
4587 if ((set = single_set (link->insn)) != 0
4588 && rtx_equal_p (i0dest, SET_DEST (set)))
4589 i0_insn = link->insn, i0_val = SET_SRC (set);
4591 record_value_for_reg (i0dest, i0_insn, i0_val);
4593 if (! added_sets_0
4594 && ! i0dest_in_i0src
4595 && REGNO (i0dest) < reg_n_sets_max)
4596 INC_REG_N_SETS (REGNO (i0dest), -1);
4599 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4600 been made to this insn. The order is important, because newi2pat
4601 can affect nonzero_bits of newpat. */
4602 if (newi2pat)
4603 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4604 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4607 if (undobuf.other_insn != NULL_RTX)
4609 if (dump_file)
4611 fprintf (dump_file, "modifying other_insn ");
4612 dump_insn_slim (dump_file, undobuf.other_insn);
4614 df_insn_rescan (undobuf.other_insn);
4617 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4619 if (dump_file)
4621 fprintf (dump_file, "modifying insn i0 ");
4622 dump_insn_slim (dump_file, i0);
4624 df_insn_rescan (i0);
4627 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4629 if (dump_file)
4631 fprintf (dump_file, "modifying insn i1 ");
4632 dump_insn_slim (dump_file, i1);
4634 df_insn_rescan (i1);
4637 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4639 if (dump_file)
4641 fprintf (dump_file, "modifying insn i2 ");
4642 dump_insn_slim (dump_file, i2);
4644 df_insn_rescan (i2);
4647 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4649 if (dump_file)
4651 fprintf (dump_file, "modifying insn i3 ");
4652 dump_insn_slim (dump_file, i3);
4654 df_insn_rescan (i3);
4657 /* Set new_direct_jump_p if a new return or simple jump instruction
4658 has been created. Adjust the CFG accordingly. */
4659 if (returnjump_p (i3) || any_uncondjump_p (i3))
4661 *new_direct_jump_p = 1;
4662 mark_jump_label (PATTERN (i3), i3, 0);
4663 update_cfg_for_uncondjump (i3);
4666 if (undobuf.other_insn != NULL_RTX
4667 && (returnjump_p (undobuf.other_insn)
4668 || any_uncondjump_p (undobuf.other_insn)))
4670 *new_direct_jump_p = 1;
4671 update_cfg_for_uncondjump (undobuf.other_insn);
4674 if (GET_CODE (PATTERN (i3)) == TRAP_IF
4675 && XEXP (PATTERN (i3), 0) == const1_rtx)
4677 basic_block bb = BLOCK_FOR_INSN (i3);
4678 gcc_assert (bb);
4679 remove_edge (split_block (bb, i3));
4680 emit_barrier_after_bb (bb);
4681 *new_direct_jump_p = 1;
4684 if (undobuf.other_insn
4685 && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4686 && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4688 basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4689 gcc_assert (bb);
4690 remove_edge (split_block (bb, undobuf.other_insn));
4691 emit_barrier_after_bb (bb);
4692 *new_direct_jump_p = 1;
4695 /* A noop might also need cleaning up of CFG, if it comes from the
4696 simplification of a jump. */
4697 if (JUMP_P (i3)
4698 && GET_CODE (newpat) == SET
4699 && SET_SRC (newpat) == pc_rtx
4700 && SET_DEST (newpat) == pc_rtx)
4702 *new_direct_jump_p = 1;
4703 update_cfg_for_uncondjump (i3);
4706 if (undobuf.other_insn != NULL_RTX
4707 && JUMP_P (undobuf.other_insn)
4708 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4709 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4710 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4712 *new_direct_jump_p = 1;
4713 update_cfg_for_uncondjump (undobuf.other_insn);
4716 combine_successes++;
4717 undo_commit ();
4719 if (added_links_insn
4720 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4721 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4722 return added_links_insn;
4723 else
4724 return newi2pat ? i2 : i3;
4727 /* Get a marker for undoing to the current state. */
4729 static void *
4730 get_undo_marker (void)
4732 return undobuf.undos;
4735 /* Undo the modifications up to the marker. */
4737 static void
4738 undo_to_marker (void *marker)
4740 struct undo *undo, *next;
4742 for (undo = undobuf.undos; undo != marker; undo = next)
4744 gcc_assert (undo);
4746 next = undo->next;
4747 switch (undo->kind)
4749 case UNDO_RTX:
4750 *undo->where.r = undo->old_contents.r;
4751 break;
4752 case UNDO_INT:
4753 *undo->where.i = undo->old_contents.i;
4754 break;
4755 case UNDO_MODE:
4756 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4757 break;
4758 case UNDO_LINKS:
4759 *undo->where.l = undo->old_contents.l;
4760 break;
4761 default:
4762 gcc_unreachable ();
4765 undo->next = undobuf.frees;
4766 undobuf.frees = undo;
4769 undobuf.undos = (struct undo *) marker;
4772 /* Undo all the modifications recorded in undobuf. */
4774 static void
4775 undo_all (void)
4777 undo_to_marker (0);
4780 /* We've committed to accepting the changes we made. Move all
4781 of the undos to the free list. */
4783 static void
4784 undo_commit (void)
4786 struct undo *undo, *next;
4788 for (undo = undobuf.undos; undo; undo = next)
4790 next = undo->next;
4791 undo->next = undobuf.frees;
4792 undobuf.frees = undo;
4794 undobuf.undos = 0;
4797 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4798 where we have an arithmetic expression and return that point. LOC will
4799 be inside INSN.
4801 try_combine will call this function to see if an insn can be split into
4802 two insns. */
4804 static rtx *
4805 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4807 rtx x = *loc;
4808 enum rtx_code code = GET_CODE (x);
4809 rtx *split;
4810 unsigned HOST_WIDE_INT len = 0;
4811 HOST_WIDE_INT pos = 0;
4812 int unsignedp = 0;
4813 rtx inner = NULL_RTX;
4814 scalar_int_mode mode, inner_mode;
4816 /* First special-case some codes. */
4817 switch (code)
4819 case SUBREG:
4820 #ifdef INSN_SCHEDULING
4821 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4822 point. */
4823 if (MEM_P (SUBREG_REG (x)))
4824 return loc;
4825 #endif
4826 return find_split_point (&SUBREG_REG (x), insn, false);
4828 case MEM:
4829 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4830 using LO_SUM and HIGH. */
4831 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4832 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4834 machine_mode address_mode = get_address_mode (x);
4836 SUBST (XEXP (x, 0),
4837 gen_rtx_LO_SUM (address_mode,
4838 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4839 XEXP (x, 0)));
4840 return &XEXP (XEXP (x, 0), 0);
4843 /* If we have a PLUS whose second operand is a constant and the
4844 address is not valid, perhaps will can split it up using
4845 the machine-specific way to split large constants. We use
4846 the first pseudo-reg (one of the virtual regs) as a placeholder;
4847 it will not remain in the result. */
4848 if (GET_CODE (XEXP (x, 0)) == PLUS
4849 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4850 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4851 MEM_ADDR_SPACE (x)))
4853 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4854 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4855 subst_insn);
4857 /* This should have produced two insns, each of which sets our
4858 placeholder. If the source of the second is a valid address,
4859 we can make put both sources together and make a split point
4860 in the middle. */
4862 if (seq
4863 && NEXT_INSN (seq) != NULL_RTX
4864 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4865 && NONJUMP_INSN_P (seq)
4866 && GET_CODE (PATTERN (seq)) == SET
4867 && SET_DEST (PATTERN (seq)) == reg
4868 && ! reg_mentioned_p (reg,
4869 SET_SRC (PATTERN (seq)))
4870 && NONJUMP_INSN_P (NEXT_INSN (seq))
4871 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4872 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4873 && memory_address_addr_space_p
4874 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4875 MEM_ADDR_SPACE (x)))
4877 rtx src1 = SET_SRC (PATTERN (seq));
4878 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4880 /* Replace the placeholder in SRC2 with SRC1. If we can
4881 find where in SRC2 it was placed, that can become our
4882 split point and we can replace this address with SRC2.
4883 Just try two obvious places. */
4885 src2 = replace_rtx (src2, reg, src1);
4886 split = 0;
4887 if (XEXP (src2, 0) == src1)
4888 split = &XEXP (src2, 0);
4889 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4890 && XEXP (XEXP (src2, 0), 0) == src1)
4891 split = &XEXP (XEXP (src2, 0), 0);
4893 if (split)
4895 SUBST (XEXP (x, 0), src2);
4896 return split;
4900 /* If that didn't work, perhaps the first operand is complex and
4901 needs to be computed separately, so make a split point there.
4902 This will occur on machines that just support REG + CONST
4903 and have a constant moved through some previous computation. */
4905 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4906 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4907 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4908 return &XEXP (XEXP (x, 0), 0);
4911 /* If we have a PLUS whose first operand is complex, try computing it
4912 separately by making a split there. */
4913 if (GET_CODE (XEXP (x, 0)) == PLUS
4914 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4915 MEM_ADDR_SPACE (x))
4916 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4917 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4918 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4919 return &XEXP (XEXP (x, 0), 0);
4920 break;
4922 case SET:
4923 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4924 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4925 we need to put the operand into a register. So split at that
4926 point. */
4928 if (SET_DEST (x) == cc0_rtx
4929 && GET_CODE (SET_SRC (x)) != COMPARE
4930 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4931 && !OBJECT_P (SET_SRC (x))
4932 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4933 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4934 return &SET_SRC (x);
4936 /* See if we can split SET_SRC as it stands. */
4937 split = find_split_point (&SET_SRC (x), insn, true);
4938 if (split && split != &SET_SRC (x))
4939 return split;
4941 /* See if we can split SET_DEST as it stands. */
4942 split = find_split_point (&SET_DEST (x), insn, false);
4943 if (split && split != &SET_DEST (x))
4944 return split;
4946 /* See if this is a bitfield assignment with everything constant. If
4947 so, this is an IOR of an AND, so split it into that. */
4948 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4949 && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
4950 &inner_mode)
4951 && HWI_COMPUTABLE_MODE_P (inner_mode)
4952 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4953 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4954 && CONST_INT_P (SET_SRC (x))
4955 && ((INTVAL (XEXP (SET_DEST (x), 1))
4956 + INTVAL (XEXP (SET_DEST (x), 2)))
4957 <= GET_MODE_PRECISION (inner_mode))
4958 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4960 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4961 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4962 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4963 rtx dest = XEXP (SET_DEST (x), 0);
4964 unsigned HOST_WIDE_INT mask
4965 = (HOST_WIDE_INT_1U << len) - 1;
4966 rtx or_mask;
4968 if (BITS_BIG_ENDIAN)
4969 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
4971 or_mask = gen_int_mode (src << pos, inner_mode);
4972 if (src == mask)
4973 SUBST (SET_SRC (x),
4974 simplify_gen_binary (IOR, inner_mode, dest, or_mask));
4975 else
4977 rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
4978 SUBST (SET_SRC (x),
4979 simplify_gen_binary (IOR, inner_mode,
4980 simplify_gen_binary (AND, inner_mode,
4981 dest, negmask),
4982 or_mask));
4985 SUBST (SET_DEST (x), dest);
4987 split = find_split_point (&SET_SRC (x), insn, true);
4988 if (split && split != &SET_SRC (x))
4989 return split;
4992 /* Otherwise, see if this is an operation that we can split into two.
4993 If so, try to split that. */
4994 code = GET_CODE (SET_SRC (x));
4996 switch (code)
4998 case AND:
4999 /* If we are AND'ing with a large constant that is only a single
5000 bit and the result is only being used in a context where we
5001 need to know if it is zero or nonzero, replace it with a bit
5002 extraction. This will avoid the large constant, which might
5003 have taken more than one insn to make. If the constant were
5004 not a valid argument to the AND but took only one insn to make,
5005 this is no worse, but if it took more than one insn, it will
5006 be better. */
5008 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5009 && REG_P (XEXP (SET_SRC (x), 0))
5010 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5011 && REG_P (SET_DEST (x))
5012 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5013 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5014 && XEXP (*split, 0) == SET_DEST (x)
5015 && XEXP (*split, 1) == const0_rtx)
5017 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5018 XEXP (SET_SRC (x), 0),
5019 pos, NULL_RTX, 1, 1, 0, 0);
5020 if (extraction != 0)
5022 SUBST (SET_SRC (x), extraction);
5023 return find_split_point (loc, insn, false);
5026 break;
5028 case NE:
5029 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5030 is known to be on, this can be converted into a NEG of a shift. */
5031 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5032 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5033 && 1 <= (pos = exact_log2
5034 (nonzero_bits (XEXP (SET_SRC (x), 0),
5035 GET_MODE (XEXP (SET_SRC (x), 0))))))
5037 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5039 SUBST (SET_SRC (x),
5040 gen_rtx_NEG (mode,
5041 gen_rtx_LSHIFTRT (mode,
5042 XEXP (SET_SRC (x), 0),
5043 GEN_INT (pos))));
5045 split = find_split_point (&SET_SRC (x), insn, true);
5046 if (split && split != &SET_SRC (x))
5047 return split;
5049 break;
5051 case SIGN_EXTEND:
5052 inner = XEXP (SET_SRC (x), 0);
5054 /* We can't optimize if either mode is a partial integer
5055 mode as we don't know how many bits are significant
5056 in those modes. */
5057 if (!is_int_mode (GET_MODE (inner), &inner_mode)
5058 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5059 break;
5061 pos = 0;
5062 len = GET_MODE_PRECISION (inner_mode);
5063 unsignedp = 0;
5064 break;
5066 case SIGN_EXTRACT:
5067 case ZERO_EXTRACT:
5068 if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5069 &inner_mode)
5070 && CONST_INT_P (XEXP (SET_SRC (x), 1))
5071 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5073 inner = XEXP (SET_SRC (x), 0);
5074 len = INTVAL (XEXP (SET_SRC (x), 1));
5075 pos = INTVAL (XEXP (SET_SRC (x), 2));
5077 if (BITS_BIG_ENDIAN)
5078 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5079 unsignedp = (code == ZERO_EXTRACT);
5081 break;
5083 default:
5084 break;
5087 if (len && pos >= 0
5088 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner))
5089 && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5091 /* For unsigned, we have a choice of a shift followed by an
5092 AND or two shifts. Use two shifts for field sizes where the
5093 constant might be too large. We assume here that we can
5094 always at least get 8-bit constants in an AND insn, which is
5095 true for every current RISC. */
5097 if (unsignedp && len <= 8)
5099 unsigned HOST_WIDE_INT mask
5100 = (HOST_WIDE_INT_1U << len) - 1;
5101 SUBST (SET_SRC (x),
5102 gen_rtx_AND (mode,
5103 gen_rtx_LSHIFTRT
5104 (mode, gen_lowpart (mode, inner),
5105 GEN_INT (pos)),
5106 gen_int_mode (mask, mode)));
5108 split = find_split_point (&SET_SRC (x), insn, true);
5109 if (split && split != &SET_SRC (x))
5110 return split;
5112 else
5114 SUBST (SET_SRC (x),
5115 gen_rtx_fmt_ee
5116 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5117 gen_rtx_ASHIFT (mode,
5118 gen_lowpart (mode, inner),
5119 GEN_INT (GET_MODE_PRECISION (mode)
5120 - len - pos)),
5121 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5123 split = find_split_point (&SET_SRC (x), insn, true);
5124 if (split && split != &SET_SRC (x))
5125 return split;
5129 /* See if this is a simple operation with a constant as the second
5130 operand. It might be that this constant is out of range and hence
5131 could be used as a split point. */
5132 if (BINARY_P (SET_SRC (x))
5133 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5134 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5135 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5136 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5137 return &XEXP (SET_SRC (x), 1);
5139 /* Finally, see if this is a simple operation with its first operand
5140 not in a register. The operation might require this operand in a
5141 register, so return it as a split point. We can always do this
5142 because if the first operand were another operation, we would have
5143 already found it as a split point. */
5144 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5145 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5146 return &XEXP (SET_SRC (x), 0);
5148 return 0;
5150 case AND:
5151 case IOR:
5152 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5153 it is better to write this as (not (ior A B)) so we can split it.
5154 Similarly for IOR. */
5155 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5157 SUBST (*loc,
5158 gen_rtx_NOT (GET_MODE (x),
5159 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5160 GET_MODE (x),
5161 XEXP (XEXP (x, 0), 0),
5162 XEXP (XEXP (x, 1), 0))));
5163 return find_split_point (loc, insn, set_src);
5166 /* Many RISC machines have a large set of logical insns. If the
5167 second operand is a NOT, put it first so we will try to split the
5168 other operand first. */
5169 if (GET_CODE (XEXP (x, 1)) == NOT)
5171 rtx tem = XEXP (x, 0);
5172 SUBST (XEXP (x, 0), XEXP (x, 1));
5173 SUBST (XEXP (x, 1), tem);
5175 break;
5177 case PLUS:
5178 case MINUS:
5179 /* Canonicalization can produce (minus A (mult B C)), where C is a
5180 constant. It may be better to try splitting (plus (mult B -C) A)
5181 instead if this isn't a multiply by a power of two. */
5182 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5183 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5184 && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5186 machine_mode mode = GET_MODE (x);
5187 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5188 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5189 SUBST (*loc, gen_rtx_PLUS (mode,
5190 gen_rtx_MULT (mode,
5191 XEXP (XEXP (x, 1), 0),
5192 gen_int_mode (other_int,
5193 mode)),
5194 XEXP (x, 0)));
5195 return find_split_point (loc, insn, set_src);
5198 /* Split at a multiply-accumulate instruction. However if this is
5199 the SET_SRC, we likely do not have such an instruction and it's
5200 worthless to try this split. */
5201 if (!set_src
5202 && (GET_CODE (XEXP (x, 0)) == MULT
5203 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5204 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5205 return loc;
5207 default:
5208 break;
5211 /* Otherwise, select our actions depending on our rtx class. */
5212 switch (GET_RTX_CLASS (code))
5214 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5215 case RTX_TERNARY:
5216 split = find_split_point (&XEXP (x, 2), insn, false);
5217 if (split)
5218 return split;
5219 /* fall through */
5220 case RTX_BIN_ARITH:
5221 case RTX_COMM_ARITH:
5222 case RTX_COMPARE:
5223 case RTX_COMM_COMPARE:
5224 split = find_split_point (&XEXP (x, 1), insn, false);
5225 if (split)
5226 return split;
5227 /* fall through */
5228 case RTX_UNARY:
5229 /* Some machines have (and (shift ...) ...) insns. If X is not
5230 an AND, but XEXP (X, 0) is, use it as our split point. */
5231 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5232 return &XEXP (x, 0);
5234 split = find_split_point (&XEXP (x, 0), insn, false);
5235 if (split)
5236 return split;
5237 return loc;
5239 default:
5240 /* Otherwise, we don't have a split point. */
5241 return 0;
5245 /* Throughout X, replace FROM with TO, and return the result.
5246 The result is TO if X is FROM;
5247 otherwise the result is X, but its contents may have been modified.
5248 If they were modified, a record was made in undobuf so that
5249 undo_all will (among other things) return X to its original state.
5251 If the number of changes necessary is too much to record to undo,
5252 the excess changes are not made, so the result is invalid.
5253 The changes already made can still be undone.
5254 undobuf.num_undo is incremented for such changes, so by testing that
5255 the caller can tell whether the result is valid.
5257 `n_occurrences' is incremented each time FROM is replaced.
5259 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5261 IN_COND is nonzero if we are at the top level of a condition.
5263 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5264 by copying if `n_occurrences' is nonzero. */
5266 static rtx
5267 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5269 enum rtx_code code = GET_CODE (x);
5270 machine_mode op0_mode = VOIDmode;
5271 const char *fmt;
5272 int len, i;
5273 rtx new_rtx;
5275 /* Two expressions are equal if they are identical copies of a shared
5276 RTX or if they are both registers with the same register number
5277 and mode. */
5279 #define COMBINE_RTX_EQUAL_P(X,Y) \
5280 ((X) == (Y) \
5281 || (REG_P (X) && REG_P (Y) \
5282 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5284 /* Do not substitute into clobbers of regs -- this will never result in
5285 valid RTL. */
5286 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5287 return x;
5289 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5291 n_occurrences++;
5292 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5295 /* If X and FROM are the same register but different modes, they
5296 will not have been seen as equal above. However, the log links code
5297 will make a LOG_LINKS entry for that case. If we do nothing, we
5298 will try to rerecognize our original insn and, when it succeeds,
5299 we will delete the feeding insn, which is incorrect.
5301 So force this insn not to match in this (rare) case. */
5302 if (! in_dest && code == REG && REG_P (from)
5303 && reg_overlap_mentioned_p (x, from))
5304 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5306 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5307 of which may contain things that can be combined. */
5308 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5309 return x;
5311 /* It is possible to have a subexpression appear twice in the insn.
5312 Suppose that FROM is a register that appears within TO.
5313 Then, after that subexpression has been scanned once by `subst',
5314 the second time it is scanned, TO may be found. If we were
5315 to scan TO here, we would find FROM within it and create a
5316 self-referent rtl structure which is completely wrong. */
5317 if (COMBINE_RTX_EQUAL_P (x, to))
5318 return to;
5320 /* Parallel asm_operands need special attention because all of the
5321 inputs are shared across the arms. Furthermore, unsharing the
5322 rtl results in recognition failures. Failure to handle this case
5323 specially can result in circular rtl.
5325 Solve this by doing a normal pass across the first entry of the
5326 parallel, and only processing the SET_DESTs of the subsequent
5327 entries. Ug. */
5329 if (code == PARALLEL
5330 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5331 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5333 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5335 /* If this substitution failed, this whole thing fails. */
5336 if (GET_CODE (new_rtx) == CLOBBER
5337 && XEXP (new_rtx, 0) == const0_rtx)
5338 return new_rtx;
5340 SUBST (XVECEXP (x, 0, 0), new_rtx);
5342 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5344 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5346 if (!REG_P (dest)
5347 && GET_CODE (dest) != CC0
5348 && GET_CODE (dest) != PC)
5350 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5352 /* If this substitution failed, this whole thing fails. */
5353 if (GET_CODE (new_rtx) == CLOBBER
5354 && XEXP (new_rtx, 0) == const0_rtx)
5355 return new_rtx;
5357 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5361 else
5363 len = GET_RTX_LENGTH (code);
5364 fmt = GET_RTX_FORMAT (code);
5366 /* We don't need to process a SET_DEST that is a register, CC0,
5367 or PC, so set up to skip this common case. All other cases
5368 where we want to suppress replacing something inside a
5369 SET_SRC are handled via the IN_DEST operand. */
5370 if (code == SET
5371 && (REG_P (SET_DEST (x))
5372 || GET_CODE (SET_DEST (x)) == CC0
5373 || GET_CODE (SET_DEST (x)) == PC))
5374 fmt = "ie";
5376 /* Trying to simplify the operands of a widening MULT is not likely
5377 to create RTL matching a machine insn. */
5378 if (code == MULT
5379 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5380 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5381 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5382 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5383 && REG_P (XEXP (XEXP (x, 0), 0))
5384 && REG_P (XEXP (XEXP (x, 1), 0))
5385 && from == to)
5386 return x;
5389 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5390 constant. */
5391 if (fmt[0] == 'e')
5392 op0_mode = GET_MODE (XEXP (x, 0));
5394 for (i = 0; i < len; i++)
5396 if (fmt[i] == 'E')
5398 int j;
5399 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5401 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5403 new_rtx = (unique_copy && n_occurrences
5404 ? copy_rtx (to) : to);
5405 n_occurrences++;
5407 else
5409 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5410 unique_copy);
5412 /* If this substitution failed, this whole thing
5413 fails. */
5414 if (GET_CODE (new_rtx) == CLOBBER
5415 && XEXP (new_rtx, 0) == const0_rtx)
5416 return new_rtx;
5419 SUBST (XVECEXP (x, i, j), new_rtx);
5422 else if (fmt[i] == 'e')
5424 /* If this is a register being set, ignore it. */
5425 new_rtx = XEXP (x, i);
5426 if (in_dest
5427 && i == 0
5428 && (((code == SUBREG || code == ZERO_EXTRACT)
5429 && REG_P (new_rtx))
5430 || code == STRICT_LOW_PART))
5433 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5435 /* In general, don't install a subreg involving two
5436 modes not tieable. It can worsen register
5437 allocation, and can even make invalid reload
5438 insns, since the reg inside may need to be copied
5439 from in the outside mode, and that may be invalid
5440 if it is an fp reg copied in integer mode.
5442 We allow two exceptions to this: It is valid if
5443 it is inside another SUBREG and the mode of that
5444 SUBREG and the mode of the inside of TO is
5445 tieable and it is valid if X is a SET that copies
5446 FROM to CC0. */
5448 if (GET_CODE (to) == SUBREG
5449 && ! MODES_TIEABLE_P (GET_MODE (to),
5450 GET_MODE (SUBREG_REG (to)))
5451 && ! (code == SUBREG
5452 && MODES_TIEABLE_P (GET_MODE (x),
5453 GET_MODE (SUBREG_REG (to))))
5454 && (!HAVE_cc0
5455 || (! (code == SET
5456 && i == 1
5457 && XEXP (x, 0) == cc0_rtx))))
5458 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5460 if (code == SUBREG
5461 && REG_P (to)
5462 && REGNO (to) < FIRST_PSEUDO_REGISTER
5463 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5464 SUBREG_BYTE (x),
5465 GET_MODE (x)) < 0)
5466 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5468 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5469 n_occurrences++;
5471 else
5472 /* If we are in a SET_DEST, suppress most cases unless we
5473 have gone inside a MEM, in which case we want to
5474 simplify the address. We assume here that things that
5475 are actually part of the destination have their inner
5476 parts in the first expression. This is true for SUBREG,
5477 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5478 things aside from REG and MEM that should appear in a
5479 SET_DEST. */
5480 new_rtx = subst (XEXP (x, i), from, to,
5481 (((in_dest
5482 && (code == SUBREG || code == STRICT_LOW_PART
5483 || code == ZERO_EXTRACT))
5484 || code == SET)
5485 && i == 0),
5486 code == IF_THEN_ELSE && i == 0,
5487 unique_copy);
5489 /* If we found that we will have to reject this combination,
5490 indicate that by returning the CLOBBER ourselves, rather than
5491 an expression containing it. This will speed things up as
5492 well as prevent accidents where two CLOBBERs are considered
5493 to be equal, thus producing an incorrect simplification. */
5495 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5496 return new_rtx;
5498 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5500 machine_mode mode = GET_MODE (x);
5502 x = simplify_subreg (GET_MODE (x), new_rtx,
5503 GET_MODE (SUBREG_REG (x)),
5504 SUBREG_BYTE (x));
5505 if (! x)
5506 x = gen_rtx_CLOBBER (mode, const0_rtx);
5508 else if (CONST_SCALAR_INT_P (new_rtx)
5509 && GET_CODE (x) == ZERO_EXTEND)
5511 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5512 new_rtx, GET_MODE (XEXP (x, 0)));
5513 gcc_assert (x);
5515 else
5516 SUBST (XEXP (x, i), new_rtx);
5521 /* Check if we are loading something from the constant pool via float
5522 extension; in this case we would undo compress_float_constant
5523 optimization and degenerate constant load to an immediate value. */
5524 if (GET_CODE (x) == FLOAT_EXTEND
5525 && MEM_P (XEXP (x, 0))
5526 && MEM_READONLY_P (XEXP (x, 0)))
5528 rtx tmp = avoid_constant_pool_reference (x);
5529 if (x != tmp)
5530 return x;
5533 /* Try to simplify X. If the simplification changed the code, it is likely
5534 that further simplification will help, so loop, but limit the number
5535 of repetitions that will be performed. */
5537 for (i = 0; i < 4; i++)
5539 /* If X is sufficiently simple, don't bother trying to do anything
5540 with it. */
5541 if (code != CONST_INT && code != REG && code != CLOBBER)
5542 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5544 if (GET_CODE (x) == code)
5545 break;
5547 code = GET_CODE (x);
5549 /* We no longer know the original mode of operand 0 since we
5550 have changed the form of X) */
5551 op0_mode = VOIDmode;
5554 return x;
5557 /* If X is a commutative operation whose operands are not in the canonical
5558 order, use substitutions to swap them. */
5560 static void
5561 maybe_swap_commutative_operands (rtx x)
5563 if (COMMUTATIVE_ARITH_P (x)
5564 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5566 rtx temp = XEXP (x, 0);
5567 SUBST (XEXP (x, 0), XEXP (x, 1));
5568 SUBST (XEXP (x, 1), temp);
5572 /* Simplify X, a piece of RTL. We just operate on the expression at the
5573 outer level; call `subst' to simplify recursively. Return the new
5574 expression.
5576 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5577 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5578 of a condition. */
5580 static rtx
5581 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5582 int in_cond)
5584 enum rtx_code code = GET_CODE (x);
5585 machine_mode mode = GET_MODE (x);
5586 scalar_int_mode int_mode;
5587 rtx temp;
5588 int i;
5590 /* If this is a commutative operation, put a constant last and a complex
5591 expression first. We don't need to do this for comparisons here. */
5592 maybe_swap_commutative_operands (x);
5594 /* Try to fold this expression in case we have constants that weren't
5595 present before. */
5596 temp = 0;
5597 switch (GET_RTX_CLASS (code))
5599 case RTX_UNARY:
5600 if (op0_mode == VOIDmode)
5601 op0_mode = GET_MODE (XEXP (x, 0));
5602 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5603 break;
5604 case RTX_COMPARE:
5605 case RTX_COMM_COMPARE:
5607 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5608 if (cmp_mode == VOIDmode)
5610 cmp_mode = GET_MODE (XEXP (x, 1));
5611 if (cmp_mode == VOIDmode)
5612 cmp_mode = op0_mode;
5614 temp = simplify_relational_operation (code, mode, cmp_mode,
5615 XEXP (x, 0), XEXP (x, 1));
5617 break;
5618 case RTX_COMM_ARITH:
5619 case RTX_BIN_ARITH:
5620 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5621 break;
5622 case RTX_BITFIELD_OPS:
5623 case RTX_TERNARY:
5624 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5625 XEXP (x, 1), XEXP (x, 2));
5626 break;
5627 default:
5628 break;
5631 if (temp)
5633 x = temp;
5634 code = GET_CODE (temp);
5635 op0_mode = VOIDmode;
5636 mode = GET_MODE (temp);
5639 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5640 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5641 things. Check for cases where both arms are testing the same
5642 condition.
5644 Don't do anything if all operands are very simple. */
5646 if ((BINARY_P (x)
5647 && ((!OBJECT_P (XEXP (x, 0))
5648 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5649 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5650 || (!OBJECT_P (XEXP (x, 1))
5651 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5652 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5653 || (UNARY_P (x)
5654 && (!OBJECT_P (XEXP (x, 0))
5655 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5656 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5658 rtx cond, true_rtx, false_rtx;
5660 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5661 if (cond != 0
5662 /* If everything is a comparison, what we have is highly unlikely
5663 to be simpler, so don't use it. */
5664 && ! (COMPARISON_P (x)
5665 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5667 rtx cop1 = const0_rtx;
5668 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5670 if (cond_code == NE && COMPARISON_P (cond))
5671 return x;
5673 /* Simplify the alternative arms; this may collapse the true and
5674 false arms to store-flag values. Be careful to use copy_rtx
5675 here since true_rtx or false_rtx might share RTL with x as a
5676 result of the if_then_else_cond call above. */
5677 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5678 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5680 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5681 is unlikely to be simpler. */
5682 if (general_operand (true_rtx, VOIDmode)
5683 && general_operand (false_rtx, VOIDmode))
5685 enum rtx_code reversed;
5687 /* Restarting if we generate a store-flag expression will cause
5688 us to loop. Just drop through in this case. */
5690 /* If the result values are STORE_FLAG_VALUE and zero, we can
5691 just make the comparison operation. */
5692 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5693 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5694 cond, cop1);
5695 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5696 && ((reversed = reversed_comparison_code_parts
5697 (cond_code, cond, cop1, NULL))
5698 != UNKNOWN))
5699 x = simplify_gen_relational (reversed, mode, VOIDmode,
5700 cond, cop1);
5702 /* Likewise, we can make the negate of a comparison operation
5703 if the result values are - STORE_FLAG_VALUE and zero. */
5704 else if (CONST_INT_P (true_rtx)
5705 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5706 && false_rtx == const0_rtx)
5707 x = simplify_gen_unary (NEG, mode,
5708 simplify_gen_relational (cond_code,
5709 mode, VOIDmode,
5710 cond, cop1),
5711 mode);
5712 else if (CONST_INT_P (false_rtx)
5713 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5714 && true_rtx == const0_rtx
5715 && ((reversed = reversed_comparison_code_parts
5716 (cond_code, cond, cop1, NULL))
5717 != UNKNOWN))
5718 x = simplify_gen_unary (NEG, mode,
5719 simplify_gen_relational (reversed,
5720 mode, VOIDmode,
5721 cond, cop1),
5722 mode);
5723 else
5724 return gen_rtx_IF_THEN_ELSE (mode,
5725 simplify_gen_relational (cond_code,
5726 mode,
5727 VOIDmode,
5728 cond,
5729 cop1),
5730 true_rtx, false_rtx);
5732 code = GET_CODE (x);
5733 op0_mode = VOIDmode;
5738 /* First see if we can apply the inverse distributive law. */
5739 if (code == PLUS || code == MINUS
5740 || code == AND || code == IOR || code == XOR)
5742 x = apply_distributive_law (x);
5743 code = GET_CODE (x);
5744 op0_mode = VOIDmode;
5747 /* If CODE is an associative operation not otherwise handled, see if we
5748 can associate some operands. This can win if they are constants or
5749 if they are logically related (i.e. (a & b) & a). */
5750 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5751 || code == AND || code == IOR || code == XOR
5752 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5753 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5754 || (flag_associative_math && FLOAT_MODE_P (mode))))
5756 if (GET_CODE (XEXP (x, 0)) == code)
5758 rtx other = XEXP (XEXP (x, 0), 0);
5759 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5760 rtx inner_op1 = XEXP (x, 1);
5761 rtx inner;
5763 /* Make sure we pass the constant operand if any as the second
5764 one if this is a commutative operation. */
5765 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5766 std::swap (inner_op0, inner_op1);
5767 inner = simplify_binary_operation (code == MINUS ? PLUS
5768 : code == DIV ? MULT
5769 : code,
5770 mode, inner_op0, inner_op1);
5772 /* For commutative operations, try the other pair if that one
5773 didn't simplify. */
5774 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5776 other = XEXP (XEXP (x, 0), 1);
5777 inner = simplify_binary_operation (code, mode,
5778 XEXP (XEXP (x, 0), 0),
5779 XEXP (x, 1));
5782 if (inner)
5783 return simplify_gen_binary (code, mode, other, inner);
5787 /* A little bit of algebraic simplification here. */
5788 switch (code)
5790 case MEM:
5791 /* Ensure that our address has any ASHIFTs converted to MULT in case
5792 address-recognizing predicates are called later. */
5793 temp = make_compound_operation (XEXP (x, 0), MEM);
5794 SUBST (XEXP (x, 0), temp);
5795 break;
5797 case SUBREG:
5798 if (op0_mode == VOIDmode)
5799 op0_mode = GET_MODE (SUBREG_REG (x));
5801 /* See if this can be moved to simplify_subreg. */
5802 if (CONSTANT_P (SUBREG_REG (x))
5803 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5804 /* Don't call gen_lowpart if the inner mode
5805 is VOIDmode and we cannot simplify it, as SUBREG without
5806 inner mode is invalid. */
5807 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5808 || gen_lowpart_common (mode, SUBREG_REG (x))))
5809 return gen_lowpart (mode, SUBREG_REG (x));
5811 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5812 break;
5814 rtx temp;
5815 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5816 SUBREG_BYTE (x));
5817 if (temp)
5818 return temp;
5820 /* If op is known to have all lower bits zero, the result is zero. */
5821 scalar_int_mode int_mode, int_op0_mode;
5822 if (!in_dest
5823 && is_a <scalar_int_mode> (mode, &int_mode)
5824 && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5825 && (GET_MODE_PRECISION (int_mode)
5826 < GET_MODE_PRECISION (int_op0_mode))
5827 && (subreg_lowpart_offset (int_mode, int_op0_mode)
5828 == SUBREG_BYTE (x))
5829 && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5830 && (nonzero_bits (SUBREG_REG (x), int_op0_mode)
5831 & GET_MODE_MASK (int_mode)) == 0)
5832 return CONST0_RTX (int_mode);
5835 /* Don't change the mode of the MEM if that would change the meaning
5836 of the address. */
5837 if (MEM_P (SUBREG_REG (x))
5838 && (MEM_VOLATILE_P (SUBREG_REG (x))
5839 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5840 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5841 return gen_rtx_CLOBBER (mode, const0_rtx);
5843 /* Note that we cannot do any narrowing for non-constants since
5844 we might have been counting on using the fact that some bits were
5845 zero. We now do this in the SET. */
5847 break;
5849 case NEG:
5850 temp = expand_compound_operation (XEXP (x, 0));
5852 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5853 replaced by (lshiftrt X C). This will convert
5854 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5856 if (GET_CODE (temp) == ASHIFTRT
5857 && CONST_INT_P (XEXP (temp, 1))
5858 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5859 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5860 INTVAL (XEXP (temp, 1)));
5862 /* If X has only a single bit that might be nonzero, say, bit I, convert
5863 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5864 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5865 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5866 or a SUBREG of one since we'd be making the expression more
5867 complex if it was just a register. */
5869 if (!REG_P (temp)
5870 && ! (GET_CODE (temp) == SUBREG
5871 && REG_P (SUBREG_REG (temp)))
5872 && is_a <scalar_int_mode> (mode, &int_mode)
5873 && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
5875 rtx temp1 = simplify_shift_const
5876 (NULL_RTX, ASHIFTRT, int_mode,
5877 simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
5878 GET_MODE_PRECISION (int_mode) - 1 - i),
5879 GET_MODE_PRECISION (int_mode) - 1 - i);
5881 /* If all we did was surround TEMP with the two shifts, we
5882 haven't improved anything, so don't use it. Otherwise,
5883 we are better off with TEMP1. */
5884 if (GET_CODE (temp1) != ASHIFTRT
5885 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5886 || XEXP (XEXP (temp1, 0), 0) != temp)
5887 return temp1;
5889 break;
5891 case TRUNCATE:
5892 /* We can't handle truncation to a partial integer mode here
5893 because we don't know the real bitsize of the partial
5894 integer mode. */
5895 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5896 break;
5898 if (HWI_COMPUTABLE_MODE_P (mode))
5899 SUBST (XEXP (x, 0),
5900 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5901 GET_MODE_MASK (mode), 0));
5903 /* We can truncate a constant value and return it. */
5904 if (CONST_INT_P (XEXP (x, 0)))
5905 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5907 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5908 whose value is a comparison can be replaced with a subreg if
5909 STORE_FLAG_VALUE permits. */
5910 if (HWI_COMPUTABLE_MODE_P (mode)
5911 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5912 && (temp = get_last_value (XEXP (x, 0)))
5913 && COMPARISON_P (temp))
5914 return gen_lowpart (mode, XEXP (x, 0));
5915 break;
5917 case CONST:
5918 /* (const (const X)) can become (const X). Do it this way rather than
5919 returning the inner CONST since CONST can be shared with a
5920 REG_EQUAL note. */
5921 if (GET_CODE (XEXP (x, 0)) == CONST)
5922 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5923 break;
5925 case LO_SUM:
5926 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5927 can add in an offset. find_split_point will split this address up
5928 again if it doesn't match. */
5929 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
5930 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5931 return XEXP (x, 1);
5932 break;
5934 case PLUS:
5935 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5936 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5937 bit-field and can be replaced by either a sign_extend or a
5938 sign_extract. The `and' may be a zero_extend and the two
5939 <c>, -<c> constants may be reversed. */
5940 if (GET_CODE (XEXP (x, 0)) == XOR
5941 && is_a <scalar_int_mode> (mode, &int_mode)
5942 && CONST_INT_P (XEXP (x, 1))
5943 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5944 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5945 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5946 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5947 && HWI_COMPUTABLE_MODE_P (int_mode)
5948 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5949 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5950 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5951 == (HOST_WIDE_INT_1U << (i + 1)) - 1))
5952 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5953 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5954 == (unsigned int) i + 1))))
5955 return simplify_shift_const
5956 (NULL_RTX, ASHIFTRT, int_mode,
5957 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
5958 XEXP (XEXP (XEXP (x, 0), 0), 0),
5959 GET_MODE_PRECISION (int_mode) - (i + 1)),
5960 GET_MODE_PRECISION (int_mode) - (i + 1));
5962 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5963 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5964 the bitsize of the mode - 1. This allows simplification of
5965 "a = (b & 8) == 0;" */
5966 if (XEXP (x, 1) == constm1_rtx
5967 && !REG_P (XEXP (x, 0))
5968 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5969 && REG_P (SUBREG_REG (XEXP (x, 0))))
5970 && is_a <scalar_int_mode> (mode, &int_mode)
5971 && nonzero_bits (XEXP (x, 0), int_mode) == 1)
5972 return simplify_shift_const
5973 (NULL_RTX, ASHIFTRT, int_mode,
5974 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
5975 gen_rtx_XOR (int_mode, XEXP (x, 0),
5976 const1_rtx),
5977 GET_MODE_PRECISION (int_mode) - 1),
5978 GET_MODE_PRECISION (int_mode) - 1);
5980 /* If we are adding two things that have no bits in common, convert
5981 the addition into an IOR. This will often be further simplified,
5982 for example in cases like ((a & 1) + (a & 2)), which can
5983 become a & 3. */
5985 if (HWI_COMPUTABLE_MODE_P (mode)
5986 && (nonzero_bits (XEXP (x, 0), mode)
5987 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5989 /* Try to simplify the expression further. */
5990 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5991 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5993 /* If we could, great. If not, do not go ahead with the IOR
5994 replacement, since PLUS appears in many special purpose
5995 address arithmetic instructions. */
5996 if (GET_CODE (temp) != CLOBBER
5997 && (GET_CODE (temp) != IOR
5998 || ((XEXP (temp, 0) != XEXP (x, 0)
5999 || XEXP (temp, 1) != XEXP (x, 1))
6000 && (XEXP (temp, 0) != XEXP (x, 1)
6001 || XEXP (temp, 1) != XEXP (x, 0)))))
6002 return temp;
6005 /* Canonicalize x + x into x << 1. */
6006 if (GET_MODE_CLASS (mode) == MODE_INT
6007 && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6008 && !side_effects_p (XEXP (x, 0)))
6009 return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6011 break;
6013 case MINUS:
6014 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6015 (and <foo> (const_int pow2-1)) */
6016 if (is_a <scalar_int_mode> (mode, &int_mode)
6017 && GET_CODE (XEXP (x, 1)) == AND
6018 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6019 && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6020 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6021 return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6022 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6023 break;
6025 case MULT:
6026 /* If we have (mult (plus A B) C), apply the distributive law and then
6027 the inverse distributive law to see if things simplify. This
6028 occurs mostly in addresses, often when unrolling loops. */
6030 if (GET_CODE (XEXP (x, 0)) == PLUS)
6032 rtx result = distribute_and_simplify_rtx (x, 0);
6033 if (result)
6034 return result;
6037 /* Try simplify a*(b/c) as (a*b)/c. */
6038 if (FLOAT_MODE_P (mode) && flag_associative_math
6039 && GET_CODE (XEXP (x, 0)) == DIV)
6041 rtx tem = simplify_binary_operation (MULT, mode,
6042 XEXP (XEXP (x, 0), 0),
6043 XEXP (x, 1));
6044 if (tem)
6045 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6047 break;
6049 case UDIV:
6050 /* If this is a divide by a power of two, treat it as a shift if
6051 its first operand is a shift. */
6052 if (is_a <scalar_int_mode> (mode, &int_mode)
6053 && CONST_INT_P (XEXP (x, 1))
6054 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6055 && (GET_CODE (XEXP (x, 0)) == ASHIFT
6056 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6057 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6058 || GET_CODE (XEXP (x, 0)) == ROTATE
6059 || GET_CODE (XEXP (x, 0)) == ROTATERT))
6060 return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6061 XEXP (x, 0), i);
6062 break;
6064 case EQ: case NE:
6065 case GT: case GTU: case GE: case GEU:
6066 case LT: case LTU: case LE: case LEU:
6067 case UNEQ: case LTGT:
6068 case UNGT: case UNGE:
6069 case UNLT: case UNLE:
6070 case UNORDERED: case ORDERED:
6071 /* If the first operand is a condition code, we can't do anything
6072 with it. */
6073 if (GET_CODE (XEXP (x, 0)) == COMPARE
6074 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
6075 && ! CC0_P (XEXP (x, 0))))
6077 rtx op0 = XEXP (x, 0);
6078 rtx op1 = XEXP (x, 1);
6079 enum rtx_code new_code;
6081 if (GET_CODE (op0) == COMPARE)
6082 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6084 /* Simplify our comparison, if possible. */
6085 new_code = simplify_comparison (code, &op0, &op1);
6087 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6088 if only the low-order bit is possibly nonzero in X (such as when
6089 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6090 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6091 known to be either 0 or -1, NE becomes a NEG and EQ becomes
6092 (plus X 1).
6094 Remove any ZERO_EXTRACT we made when thinking this was a
6095 comparison. It may now be simpler to use, e.g., an AND. If a
6096 ZERO_EXTRACT is indeed appropriate, it will be placed back by
6097 the call to make_compound_operation in the SET case.
6099 Don't apply these optimizations if the caller would
6100 prefer a comparison rather than a value.
6101 E.g., for the condition in an IF_THEN_ELSE most targets need
6102 an explicit comparison. */
6104 if (in_cond)
6107 else if (STORE_FLAG_VALUE == 1
6108 && new_code == NE
6109 && is_int_mode (mode, &int_mode)
6110 && op1 == const0_rtx
6111 && int_mode == GET_MODE (op0)
6112 && nonzero_bits (op0, int_mode) == 1)
6113 return gen_lowpart (int_mode,
6114 expand_compound_operation (op0));
6116 else if (STORE_FLAG_VALUE == 1
6117 && new_code == NE
6118 && is_int_mode (mode, &int_mode)
6119 && op1 == const0_rtx
6120 && int_mode == GET_MODE (op0)
6121 && (num_sign_bit_copies (op0, int_mode)
6122 == GET_MODE_PRECISION (int_mode)))
6124 op0 = expand_compound_operation (op0);
6125 return simplify_gen_unary (NEG, int_mode,
6126 gen_lowpart (int_mode, op0),
6127 int_mode);
6130 else if (STORE_FLAG_VALUE == 1
6131 && new_code == EQ
6132 && is_int_mode (mode, &int_mode)
6133 && op1 == const0_rtx
6134 && int_mode == GET_MODE (op0)
6135 && nonzero_bits (op0, int_mode) == 1)
6137 op0 = expand_compound_operation (op0);
6138 return simplify_gen_binary (XOR, int_mode,
6139 gen_lowpart (int_mode, op0),
6140 const1_rtx);
6143 else if (STORE_FLAG_VALUE == 1
6144 && new_code == EQ
6145 && is_int_mode (mode, &int_mode)
6146 && op1 == const0_rtx
6147 && int_mode == GET_MODE (op0)
6148 && (num_sign_bit_copies (op0, int_mode)
6149 == GET_MODE_PRECISION (int_mode)))
6151 op0 = expand_compound_operation (op0);
6152 return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6155 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6156 those above. */
6157 if (in_cond)
6160 else if (STORE_FLAG_VALUE == -1
6161 && new_code == NE
6162 && is_int_mode (mode, &int_mode)
6163 && op1 == const0_rtx
6164 && int_mode == GET_MODE (op0)
6165 && (num_sign_bit_copies (op0, int_mode)
6166 == GET_MODE_PRECISION (int_mode)))
6167 return gen_lowpart (int_mode, expand_compound_operation (op0));
6169 else if (STORE_FLAG_VALUE == -1
6170 && new_code == NE
6171 && is_int_mode (mode, &int_mode)
6172 && op1 == const0_rtx
6173 && int_mode == GET_MODE (op0)
6174 && nonzero_bits (op0, int_mode) == 1)
6176 op0 = expand_compound_operation (op0);
6177 return simplify_gen_unary (NEG, int_mode,
6178 gen_lowpart (int_mode, op0),
6179 int_mode);
6182 else if (STORE_FLAG_VALUE == -1
6183 && new_code == EQ
6184 && is_int_mode (mode, &int_mode)
6185 && op1 == const0_rtx
6186 && int_mode == GET_MODE (op0)
6187 && (num_sign_bit_copies (op0, int_mode)
6188 == GET_MODE_PRECISION (int_mode)))
6190 op0 = expand_compound_operation (op0);
6191 return simplify_gen_unary (NOT, int_mode,
6192 gen_lowpart (int_mode, op0),
6193 int_mode);
6196 /* If X is 0/1, (eq X 0) is X-1. */
6197 else if (STORE_FLAG_VALUE == -1
6198 && new_code == EQ
6199 && is_int_mode (mode, &int_mode)
6200 && op1 == const0_rtx
6201 && int_mode == GET_MODE (op0)
6202 && nonzero_bits (op0, int_mode) == 1)
6204 op0 = expand_compound_operation (op0);
6205 return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6208 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6209 one bit that might be nonzero, we can convert (ne x 0) to
6210 (ashift x c) where C puts the bit in the sign bit. Remove any
6211 AND with STORE_FLAG_VALUE when we are done, since we are only
6212 going to test the sign bit. */
6213 if (new_code == NE
6214 && is_int_mode (mode, &int_mode)
6215 && HWI_COMPUTABLE_MODE_P (int_mode)
6216 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6217 && op1 == const0_rtx
6218 && int_mode == GET_MODE (op0)
6219 && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6221 x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6222 expand_compound_operation (op0),
6223 GET_MODE_PRECISION (int_mode) - 1 - i);
6224 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6225 return XEXP (x, 0);
6226 else
6227 return x;
6230 /* If the code changed, return a whole new comparison.
6231 We also need to avoid using SUBST in cases where
6232 simplify_comparison has widened a comparison with a CONST_INT,
6233 since in that case the wider CONST_INT may fail the sanity
6234 checks in do_SUBST. */
6235 if (new_code != code
6236 || (CONST_INT_P (op1)
6237 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6238 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6239 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6241 /* Otherwise, keep this operation, but maybe change its operands.
6242 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6243 SUBST (XEXP (x, 0), op0);
6244 SUBST (XEXP (x, 1), op1);
6246 break;
6248 case IF_THEN_ELSE:
6249 return simplify_if_then_else (x);
6251 case ZERO_EXTRACT:
6252 case SIGN_EXTRACT:
6253 case ZERO_EXTEND:
6254 case SIGN_EXTEND:
6255 /* If we are processing SET_DEST, we are done. */
6256 if (in_dest)
6257 return x;
6259 return expand_compound_operation (x);
6261 case SET:
6262 return simplify_set (x);
6264 case AND:
6265 case IOR:
6266 return simplify_logical (x);
6268 case ASHIFT:
6269 case LSHIFTRT:
6270 case ASHIFTRT:
6271 case ROTATE:
6272 case ROTATERT:
6273 /* If this is a shift by a constant amount, simplify it. */
6274 if (CONST_INT_P (XEXP (x, 1)))
6275 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6276 INTVAL (XEXP (x, 1)));
6278 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6279 SUBST (XEXP (x, 1),
6280 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6281 (HOST_WIDE_INT_1U
6282 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6283 - 1,
6284 0));
6285 break;
6287 default:
6288 break;
6291 return x;
6294 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6296 static rtx
6297 simplify_if_then_else (rtx x)
6299 machine_mode mode = GET_MODE (x);
6300 rtx cond = XEXP (x, 0);
6301 rtx true_rtx = XEXP (x, 1);
6302 rtx false_rtx = XEXP (x, 2);
6303 enum rtx_code true_code = GET_CODE (cond);
6304 int comparison_p = COMPARISON_P (cond);
6305 rtx temp;
6306 int i;
6307 enum rtx_code false_code;
6308 rtx reversed;
6309 scalar_int_mode int_mode, inner_mode;
6311 /* Simplify storing of the truth value. */
6312 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6313 return simplify_gen_relational (true_code, mode, VOIDmode,
6314 XEXP (cond, 0), XEXP (cond, 1));
6316 /* Also when the truth value has to be reversed. */
6317 if (comparison_p
6318 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6319 && (reversed = reversed_comparison (cond, mode)))
6320 return reversed;
6322 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6323 in it is being compared against certain values. Get the true and false
6324 comparisons and see if that says anything about the value of each arm. */
6326 if (comparison_p
6327 && ((false_code = reversed_comparison_code (cond, NULL))
6328 != UNKNOWN)
6329 && REG_P (XEXP (cond, 0)))
6331 HOST_WIDE_INT nzb;
6332 rtx from = XEXP (cond, 0);
6333 rtx true_val = XEXP (cond, 1);
6334 rtx false_val = true_val;
6335 int swapped = 0;
6337 /* If FALSE_CODE is EQ, swap the codes and arms. */
6339 if (false_code == EQ)
6341 swapped = 1, true_code = EQ, false_code = NE;
6342 std::swap (true_rtx, false_rtx);
6345 scalar_int_mode from_mode;
6346 if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6348 /* If we are comparing against zero and the expression being
6349 tested has only a single bit that might be nonzero, that is
6350 its value when it is not equal to zero. Similarly if it is
6351 known to be -1 or 0. */
6352 if (true_code == EQ
6353 && true_val == const0_rtx
6354 && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6356 false_code = EQ;
6357 false_val = gen_int_mode (nzb, from_mode);
6359 else if (true_code == EQ
6360 && true_val == const0_rtx
6361 && (num_sign_bit_copies (from, from_mode)
6362 == GET_MODE_PRECISION (from_mode)))
6364 false_code = EQ;
6365 false_val = constm1_rtx;
6369 /* Now simplify an arm if we know the value of the register in the
6370 branch and it is used in the arm. Be careful due to the potential
6371 of locally-shared RTL. */
6373 if (reg_mentioned_p (from, true_rtx))
6374 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6375 from, true_val),
6376 pc_rtx, pc_rtx, 0, 0, 0);
6377 if (reg_mentioned_p (from, false_rtx))
6378 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6379 from, false_val),
6380 pc_rtx, pc_rtx, 0, 0, 0);
6382 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6383 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6385 true_rtx = XEXP (x, 1);
6386 false_rtx = XEXP (x, 2);
6387 true_code = GET_CODE (cond);
6390 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6391 reversed, do so to avoid needing two sets of patterns for
6392 subtract-and-branch insns. Similarly if we have a constant in the true
6393 arm, the false arm is the same as the first operand of the comparison, or
6394 the false arm is more complicated than the true arm. */
6396 if (comparison_p
6397 && reversed_comparison_code (cond, NULL) != UNKNOWN
6398 && (true_rtx == pc_rtx
6399 || (CONSTANT_P (true_rtx)
6400 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6401 || true_rtx == const0_rtx
6402 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6403 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6404 && !OBJECT_P (false_rtx))
6405 || reg_mentioned_p (true_rtx, false_rtx)
6406 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6408 true_code = reversed_comparison_code (cond, NULL);
6409 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6410 SUBST (XEXP (x, 1), false_rtx);
6411 SUBST (XEXP (x, 2), true_rtx);
6413 std::swap (true_rtx, false_rtx);
6414 cond = XEXP (x, 0);
6416 /* It is possible that the conditional has been simplified out. */
6417 true_code = GET_CODE (cond);
6418 comparison_p = COMPARISON_P (cond);
6421 /* If the two arms are identical, we don't need the comparison. */
6423 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6424 return true_rtx;
6426 /* Convert a == b ? b : a to "a". */
6427 if (true_code == EQ && ! side_effects_p (cond)
6428 && !HONOR_NANS (mode)
6429 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6430 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6431 return false_rtx;
6432 else if (true_code == NE && ! side_effects_p (cond)
6433 && !HONOR_NANS (mode)
6434 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6435 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6436 return true_rtx;
6438 /* Look for cases where we have (abs x) or (neg (abs X)). */
6440 if (GET_MODE_CLASS (mode) == MODE_INT
6441 && comparison_p
6442 && XEXP (cond, 1) == const0_rtx
6443 && GET_CODE (false_rtx) == NEG
6444 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6445 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6446 && ! side_effects_p (true_rtx))
6447 switch (true_code)
6449 case GT:
6450 case GE:
6451 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6452 case LT:
6453 case LE:
6454 return
6455 simplify_gen_unary (NEG, mode,
6456 simplify_gen_unary (ABS, mode, true_rtx, mode),
6457 mode);
6458 default:
6459 break;
6462 /* Look for MIN or MAX. */
6464 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6465 && comparison_p
6466 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6467 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6468 && ! side_effects_p (cond))
6469 switch (true_code)
6471 case GE:
6472 case GT:
6473 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6474 case LE:
6475 case LT:
6476 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6477 case GEU:
6478 case GTU:
6479 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6480 case LEU:
6481 case LTU:
6482 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6483 default:
6484 break;
6487 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6488 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6489 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6490 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6491 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6492 neither 1 or -1, but it isn't worth checking for. */
6494 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6495 && comparison_p
6496 && is_int_mode (mode, &int_mode)
6497 && ! side_effects_p (x))
6499 rtx t = make_compound_operation (true_rtx, SET);
6500 rtx f = make_compound_operation (false_rtx, SET);
6501 rtx cond_op0 = XEXP (cond, 0);
6502 rtx cond_op1 = XEXP (cond, 1);
6503 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6504 scalar_int_mode m = int_mode;
6505 rtx z = 0, c1 = NULL_RTX;
6507 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6508 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6509 || GET_CODE (t) == ASHIFT
6510 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6511 && rtx_equal_p (XEXP (t, 0), f))
6512 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6514 /* If an identity-zero op is commutative, check whether there
6515 would be a match if we swapped the operands. */
6516 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6517 || GET_CODE (t) == XOR)
6518 && rtx_equal_p (XEXP (t, 1), f))
6519 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6520 else if (GET_CODE (t) == SIGN_EXTEND
6521 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6522 && (GET_CODE (XEXP (t, 0)) == PLUS
6523 || GET_CODE (XEXP (t, 0)) == MINUS
6524 || GET_CODE (XEXP (t, 0)) == IOR
6525 || GET_CODE (XEXP (t, 0)) == XOR
6526 || GET_CODE (XEXP (t, 0)) == ASHIFT
6527 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6528 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6529 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6530 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6531 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6532 && (num_sign_bit_copies (f, GET_MODE (f))
6533 > (unsigned int)
6534 (GET_MODE_PRECISION (int_mode)
6535 - GET_MODE_PRECISION (inner_mode))))
6537 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6538 extend_op = SIGN_EXTEND;
6539 m = inner_mode;
6541 else if (GET_CODE (t) == SIGN_EXTEND
6542 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6543 && (GET_CODE (XEXP (t, 0)) == PLUS
6544 || GET_CODE (XEXP (t, 0)) == IOR
6545 || GET_CODE (XEXP (t, 0)) == XOR)
6546 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6547 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6548 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6549 && (num_sign_bit_copies (f, GET_MODE (f))
6550 > (unsigned int)
6551 (GET_MODE_PRECISION (int_mode)
6552 - GET_MODE_PRECISION (inner_mode))))
6554 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6555 extend_op = SIGN_EXTEND;
6556 m = inner_mode;
6558 else if (GET_CODE (t) == ZERO_EXTEND
6559 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6560 && (GET_CODE (XEXP (t, 0)) == PLUS
6561 || GET_CODE (XEXP (t, 0)) == MINUS
6562 || GET_CODE (XEXP (t, 0)) == IOR
6563 || GET_CODE (XEXP (t, 0)) == XOR
6564 || GET_CODE (XEXP (t, 0)) == ASHIFT
6565 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6566 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6567 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6568 && HWI_COMPUTABLE_MODE_P (int_mode)
6569 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6570 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6571 && ((nonzero_bits (f, GET_MODE (f))
6572 & ~GET_MODE_MASK (inner_mode))
6573 == 0))
6575 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6576 extend_op = ZERO_EXTEND;
6577 m = inner_mode;
6579 else if (GET_CODE (t) == ZERO_EXTEND
6580 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6581 && (GET_CODE (XEXP (t, 0)) == PLUS
6582 || GET_CODE (XEXP (t, 0)) == IOR
6583 || GET_CODE (XEXP (t, 0)) == XOR)
6584 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6585 && HWI_COMPUTABLE_MODE_P (int_mode)
6586 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6587 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6588 && ((nonzero_bits (f, GET_MODE (f))
6589 & ~GET_MODE_MASK (inner_mode))
6590 == 0))
6592 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6593 extend_op = ZERO_EXTEND;
6594 m = inner_mode;
6597 if (z)
6599 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6600 cond_op0, cond_op1),
6601 pc_rtx, pc_rtx, 0, 0, 0);
6602 temp = simplify_gen_binary (MULT, m, temp,
6603 simplify_gen_binary (MULT, m, c1,
6604 const_true_rtx));
6605 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6606 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6608 if (extend_op != UNKNOWN)
6609 temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6611 return temp;
6615 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6616 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6617 negation of a single bit, we can convert this operation to a shift. We
6618 can actually do this more generally, but it doesn't seem worth it. */
6620 if (true_code == NE
6621 && is_a <scalar_int_mode> (mode, &int_mode)
6622 && XEXP (cond, 1) == const0_rtx
6623 && false_rtx == const0_rtx
6624 && CONST_INT_P (true_rtx)
6625 && ((1 == nonzero_bits (XEXP (cond, 0), int_mode)
6626 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6627 || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6628 == GET_MODE_PRECISION (int_mode))
6629 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6630 return
6631 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6632 gen_lowpart (int_mode, XEXP (cond, 0)), i);
6634 /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6635 non-zero bit in A is C1. */
6636 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6637 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6638 && is_a <scalar_int_mode> (mode, &int_mode)
6639 && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6640 && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6641 == nonzero_bits (XEXP (cond, 0), inner_mode)
6642 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6644 rtx val = XEXP (cond, 0);
6645 if (inner_mode == int_mode)
6646 return val;
6647 else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6648 return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6651 return x;
6654 /* Simplify X, a SET expression. Return the new expression. */
6656 static rtx
6657 simplify_set (rtx x)
6659 rtx src = SET_SRC (x);
6660 rtx dest = SET_DEST (x);
6661 machine_mode mode
6662 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6663 rtx_insn *other_insn;
6664 rtx *cc_use;
6665 scalar_int_mode int_mode;
6667 /* (set (pc) (return)) gets written as (return). */
6668 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6669 return src;
6671 /* Now that we know for sure which bits of SRC we are using, see if we can
6672 simplify the expression for the object knowing that we only need the
6673 low-order bits. */
6675 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6677 src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, 0);
6678 SUBST (SET_SRC (x), src);
6681 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6682 the comparison result and try to simplify it unless we already have used
6683 undobuf.other_insn. */
6684 if ((GET_MODE_CLASS (mode) == MODE_CC
6685 || GET_CODE (src) == COMPARE
6686 || CC0_P (dest))
6687 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6688 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6689 && COMPARISON_P (*cc_use)
6690 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6692 enum rtx_code old_code = GET_CODE (*cc_use);
6693 enum rtx_code new_code;
6694 rtx op0, op1, tmp;
6695 int other_changed = 0;
6696 rtx inner_compare = NULL_RTX;
6697 machine_mode compare_mode = GET_MODE (dest);
6699 if (GET_CODE (src) == COMPARE)
6701 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6702 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6704 inner_compare = op0;
6705 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6708 else
6709 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6711 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6712 op0, op1);
6713 if (!tmp)
6714 new_code = old_code;
6715 else if (!CONSTANT_P (tmp))
6717 new_code = GET_CODE (tmp);
6718 op0 = XEXP (tmp, 0);
6719 op1 = XEXP (tmp, 1);
6721 else
6723 rtx pat = PATTERN (other_insn);
6724 undobuf.other_insn = other_insn;
6725 SUBST (*cc_use, tmp);
6727 /* Attempt to simplify CC user. */
6728 if (GET_CODE (pat) == SET)
6730 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6731 if (new_rtx != NULL_RTX)
6732 SUBST (SET_SRC (pat), new_rtx);
6735 /* Convert X into a no-op move. */
6736 SUBST (SET_DEST (x), pc_rtx);
6737 SUBST (SET_SRC (x), pc_rtx);
6738 return x;
6741 /* Simplify our comparison, if possible. */
6742 new_code = simplify_comparison (new_code, &op0, &op1);
6744 #ifdef SELECT_CC_MODE
6745 /* If this machine has CC modes other than CCmode, check to see if we
6746 need to use a different CC mode here. */
6747 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6748 compare_mode = GET_MODE (op0);
6749 else if (inner_compare
6750 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6751 && new_code == old_code
6752 && op0 == XEXP (inner_compare, 0)
6753 && op1 == XEXP (inner_compare, 1))
6754 compare_mode = GET_MODE (inner_compare);
6755 else
6756 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6758 /* If the mode changed, we have to change SET_DEST, the mode in the
6759 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6760 a hard register, just build new versions with the proper mode. If it
6761 is a pseudo, we lose unless it is only time we set the pseudo, in
6762 which case we can safely change its mode. */
6763 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6765 if (can_change_dest_mode (dest, 0, compare_mode))
6767 unsigned int regno = REGNO (dest);
6768 rtx new_dest;
6770 if (regno < FIRST_PSEUDO_REGISTER)
6771 new_dest = gen_rtx_REG (compare_mode, regno);
6772 else
6774 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6775 new_dest = regno_reg_rtx[regno];
6778 SUBST (SET_DEST (x), new_dest);
6779 SUBST (XEXP (*cc_use, 0), new_dest);
6780 other_changed = 1;
6782 dest = new_dest;
6785 #endif /* SELECT_CC_MODE */
6787 /* If the code changed, we have to build a new comparison in
6788 undobuf.other_insn. */
6789 if (new_code != old_code)
6791 int other_changed_previously = other_changed;
6792 unsigned HOST_WIDE_INT mask;
6793 rtx old_cc_use = *cc_use;
6795 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6796 dest, const0_rtx));
6797 other_changed = 1;
6799 /* If the only change we made was to change an EQ into an NE or
6800 vice versa, OP0 has only one bit that might be nonzero, and OP1
6801 is zero, check if changing the user of the condition code will
6802 produce a valid insn. If it won't, we can keep the original code
6803 in that insn by surrounding our operation with an XOR. */
6805 if (((old_code == NE && new_code == EQ)
6806 || (old_code == EQ && new_code == NE))
6807 && ! other_changed_previously && op1 == const0_rtx
6808 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6809 && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6811 rtx pat = PATTERN (other_insn), note = 0;
6813 if ((recog_for_combine (&pat, other_insn, &note) < 0
6814 && ! check_asm_operands (pat)))
6816 *cc_use = old_cc_use;
6817 other_changed = 0;
6819 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6820 gen_int_mode (mask,
6821 GET_MODE (op0)));
6826 if (other_changed)
6827 undobuf.other_insn = other_insn;
6829 /* Don't generate a compare of a CC with 0, just use that CC. */
6830 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6832 SUBST (SET_SRC (x), op0);
6833 src = SET_SRC (x);
6835 /* Otherwise, if we didn't previously have the same COMPARE we
6836 want, create it from scratch. */
6837 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6838 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6840 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6841 src = SET_SRC (x);
6844 else
6846 /* Get SET_SRC in a form where we have placed back any
6847 compound expressions. Then do the checks below. */
6848 src = make_compound_operation (src, SET);
6849 SUBST (SET_SRC (x), src);
6852 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6853 and X being a REG or (subreg (reg)), we may be able to convert this to
6854 (set (subreg:m2 x) (op)).
6856 We can always do this if M1 is narrower than M2 because that means that
6857 we only care about the low bits of the result.
6859 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6860 perform a narrower operation than requested since the high-order bits will
6861 be undefined. On machine where it is defined, this transformation is safe
6862 as long as M1 and M2 have the same number of words. */
6864 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6865 && !OBJECT_P (SUBREG_REG (src))
6866 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6867 / UNITS_PER_WORD)
6868 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6869 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6870 && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
6871 #ifdef CANNOT_CHANGE_MODE_CLASS
6872 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6873 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6874 GET_MODE (SUBREG_REG (src)),
6875 GET_MODE (src)))
6876 #endif
6877 && (REG_P (dest)
6878 || (GET_CODE (dest) == SUBREG
6879 && REG_P (SUBREG_REG (dest)))))
6881 SUBST (SET_DEST (x),
6882 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6883 dest));
6884 SUBST (SET_SRC (x), SUBREG_REG (src));
6886 src = SET_SRC (x), dest = SET_DEST (x);
6889 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6890 in SRC. */
6891 if (dest == cc0_rtx
6892 && partial_subreg_p (src)
6893 && subreg_lowpart_p (src))
6895 rtx inner = SUBREG_REG (src);
6896 machine_mode inner_mode = GET_MODE (inner);
6898 /* Here we make sure that we don't have a sign bit on. */
6899 if (val_signbit_known_clear_p (GET_MODE (src),
6900 nonzero_bits (inner, inner_mode)))
6902 SUBST (SET_SRC (x), inner);
6903 src = SET_SRC (x);
6907 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6908 would require a paradoxical subreg. Replace the subreg with a
6909 zero_extend to avoid the reload that would otherwise be required. */
6911 enum rtx_code extend_op;
6912 if (paradoxical_subreg_p (src)
6913 && MEM_P (SUBREG_REG (src))
6914 && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
6916 SUBST (SET_SRC (x),
6917 gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
6919 src = SET_SRC (x);
6922 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6923 are comparing an item known to be 0 or -1 against 0, use a logical
6924 operation instead. Check for one of the arms being an IOR of the other
6925 arm with some value. We compute three terms to be IOR'ed together. In
6926 practice, at most two will be nonzero. Then we do the IOR's. */
6928 if (GET_CODE (dest) != PC
6929 && GET_CODE (src) == IF_THEN_ELSE
6930 && is_int_mode (GET_MODE (src), &int_mode)
6931 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6932 && XEXP (XEXP (src, 0), 1) == const0_rtx
6933 && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
6934 && (!HAVE_conditional_move
6935 || ! can_conditionally_move_p (int_mode))
6936 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
6937 == GET_MODE_PRECISION (int_mode))
6938 && ! side_effects_p (src))
6940 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6941 ? XEXP (src, 1) : XEXP (src, 2));
6942 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6943 ? XEXP (src, 2) : XEXP (src, 1));
6944 rtx term1 = const0_rtx, term2, term3;
6946 if (GET_CODE (true_rtx) == IOR
6947 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6948 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6949 else if (GET_CODE (true_rtx) == IOR
6950 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6951 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6952 else if (GET_CODE (false_rtx) == IOR
6953 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6954 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6955 else if (GET_CODE (false_rtx) == IOR
6956 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6957 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6959 term2 = simplify_gen_binary (AND, int_mode,
6960 XEXP (XEXP (src, 0), 0), true_rtx);
6961 term3 = simplify_gen_binary (AND, int_mode,
6962 simplify_gen_unary (NOT, int_mode,
6963 XEXP (XEXP (src, 0), 0),
6964 int_mode),
6965 false_rtx);
6967 SUBST (SET_SRC (x),
6968 simplify_gen_binary (IOR, int_mode,
6969 simplify_gen_binary (IOR, int_mode,
6970 term1, term2),
6971 term3));
6973 src = SET_SRC (x);
6976 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6977 whole thing fail. */
6978 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6979 return src;
6980 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6981 return dest;
6982 else
6983 /* Convert this into a field assignment operation, if possible. */
6984 return make_field_assignment (x);
6987 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6988 result. */
6990 static rtx
6991 simplify_logical (rtx x)
6993 rtx op0 = XEXP (x, 0);
6994 rtx op1 = XEXP (x, 1);
6995 scalar_int_mode mode;
6997 switch (GET_CODE (x))
6999 case AND:
7000 /* We can call simplify_and_const_int only if we don't lose
7001 any (sign) bits when converting INTVAL (op1) to
7002 "unsigned HOST_WIDE_INT". */
7003 if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7004 && CONST_INT_P (op1)
7005 && (HWI_COMPUTABLE_MODE_P (mode)
7006 || INTVAL (op1) > 0))
7008 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7009 if (GET_CODE (x) != AND)
7010 return x;
7012 op0 = XEXP (x, 0);
7013 op1 = XEXP (x, 1);
7016 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7017 apply the distributive law and then the inverse distributive
7018 law to see if things simplify. */
7019 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7021 rtx result = distribute_and_simplify_rtx (x, 0);
7022 if (result)
7023 return result;
7025 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7027 rtx result = distribute_and_simplify_rtx (x, 1);
7028 if (result)
7029 return result;
7031 break;
7033 case IOR:
7034 /* If we have (ior (and A B) C), apply the distributive law and then
7035 the inverse distributive law to see if things simplify. */
7037 if (GET_CODE (op0) == AND)
7039 rtx result = distribute_and_simplify_rtx (x, 0);
7040 if (result)
7041 return result;
7044 if (GET_CODE (op1) == AND)
7046 rtx result = distribute_and_simplify_rtx (x, 1);
7047 if (result)
7048 return result;
7050 break;
7052 default:
7053 gcc_unreachable ();
7056 return x;
7059 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7060 operations" because they can be replaced with two more basic operations.
7061 ZERO_EXTEND is also considered "compound" because it can be replaced with
7062 an AND operation, which is simpler, though only one operation.
7064 The function expand_compound_operation is called with an rtx expression
7065 and will convert it to the appropriate shifts and AND operations,
7066 simplifying at each stage.
7068 The function make_compound_operation is called to convert an expression
7069 consisting of shifts and ANDs into the equivalent compound expression.
7070 It is the inverse of this function, loosely speaking. */
7072 static rtx
7073 expand_compound_operation (rtx x)
7075 unsigned HOST_WIDE_INT pos = 0, len;
7076 int unsignedp = 0;
7077 unsigned int modewidth;
7078 rtx tem;
7079 scalar_int_mode inner_mode;
7081 switch (GET_CODE (x))
7083 case ZERO_EXTEND:
7084 unsignedp = 1;
7085 /* FALLTHRU */
7086 case SIGN_EXTEND:
7087 /* We can't necessarily use a const_int for a multiword mode;
7088 it depends on implicitly extending the value.
7089 Since we don't know the right way to extend it,
7090 we can't tell whether the implicit way is right.
7092 Even for a mode that is no wider than a const_int,
7093 we can't win, because we need to sign extend one of its bits through
7094 the rest of it, and we don't know which bit. */
7095 if (CONST_INT_P (XEXP (x, 0)))
7096 return x;
7098 /* Reject modes that aren't scalar integers because turning vector
7099 or complex modes into shifts causes problems. */
7100 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7101 return x;
7103 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7104 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7105 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7106 reloaded. If not for that, MEM's would very rarely be safe.
7108 Reject modes bigger than a word, because we might not be able
7109 to reference a two-register group starting with an arbitrary register
7110 (and currently gen_lowpart might crash for a SUBREG). */
7112 if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7113 return x;
7115 len = GET_MODE_PRECISION (inner_mode);
7116 /* If the inner object has VOIDmode (the only way this can happen
7117 is if it is an ASM_OPERANDS), we can't do anything since we don't
7118 know how much masking to do. */
7119 if (len == 0)
7120 return x;
7122 break;
7124 case ZERO_EXTRACT:
7125 unsignedp = 1;
7127 /* fall through */
7129 case SIGN_EXTRACT:
7130 /* If the operand is a CLOBBER, just return it. */
7131 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7132 return XEXP (x, 0);
7134 if (!CONST_INT_P (XEXP (x, 1))
7135 || !CONST_INT_P (XEXP (x, 2)))
7136 return x;
7138 /* Reject modes that aren't scalar integers because turning vector
7139 or complex modes into shifts causes problems. */
7140 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7141 return x;
7143 len = INTVAL (XEXP (x, 1));
7144 pos = INTVAL (XEXP (x, 2));
7146 /* This should stay within the object being extracted, fail otherwise. */
7147 if (len + pos > GET_MODE_PRECISION (inner_mode))
7148 return x;
7150 if (BITS_BIG_ENDIAN)
7151 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7153 break;
7155 default:
7156 return x;
7159 /* We've rejected non-scalar operations by now. */
7160 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7162 /* Convert sign extension to zero extension, if we know that the high
7163 bit is not set, as this is easier to optimize. It will be converted
7164 back to cheaper alternative in make_extraction. */
7165 if (GET_CODE (x) == SIGN_EXTEND
7166 && HWI_COMPUTABLE_MODE_P (mode)
7167 && ((nonzero_bits (XEXP (x, 0), inner_mode)
7168 & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7169 == 0))
7171 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7172 rtx temp2 = expand_compound_operation (temp);
7174 /* Make sure this is a profitable operation. */
7175 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7176 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7177 return temp2;
7178 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7179 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7180 return temp;
7181 else
7182 return x;
7185 /* We can optimize some special cases of ZERO_EXTEND. */
7186 if (GET_CODE (x) == ZERO_EXTEND)
7188 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7189 know that the last value didn't have any inappropriate bits
7190 set. */
7191 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7192 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7193 && HWI_COMPUTABLE_MODE_P (mode)
7194 && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7195 & ~GET_MODE_MASK (inner_mode)) == 0)
7196 return XEXP (XEXP (x, 0), 0);
7198 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7199 if (GET_CODE (XEXP (x, 0)) == SUBREG
7200 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7201 && subreg_lowpart_p (XEXP (x, 0))
7202 && HWI_COMPUTABLE_MODE_P (mode)
7203 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7204 & ~GET_MODE_MASK (inner_mode)) == 0)
7205 return SUBREG_REG (XEXP (x, 0));
7207 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7208 is a comparison and STORE_FLAG_VALUE permits. This is like
7209 the first case, but it works even when MODE is larger
7210 than HOST_WIDE_INT. */
7211 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7212 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7213 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7214 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7215 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7216 return XEXP (XEXP (x, 0), 0);
7218 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7219 if (GET_CODE (XEXP (x, 0)) == SUBREG
7220 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7221 && subreg_lowpart_p (XEXP (x, 0))
7222 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7223 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7224 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7225 return SUBREG_REG (XEXP (x, 0));
7229 /* If we reach here, we want to return a pair of shifts. The inner
7230 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7231 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7232 logical depending on the value of UNSIGNEDP.
7234 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7235 converted into an AND of a shift.
7237 We must check for the case where the left shift would have a negative
7238 count. This can happen in a case like (x >> 31) & 255 on machines
7239 that can't shift by a constant. On those machines, we would first
7240 combine the shift with the AND to produce a variable-position
7241 extraction. Then the constant of 31 would be substituted in
7242 to produce such a position. */
7244 modewidth = GET_MODE_PRECISION (mode);
7245 if (modewidth >= pos + len)
7247 tem = gen_lowpart (mode, XEXP (x, 0));
7248 if (!tem || GET_CODE (tem) == CLOBBER)
7249 return x;
7250 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7251 tem, modewidth - pos - len);
7252 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7253 mode, tem, modewidth - len);
7255 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7256 tem = simplify_and_const_int (NULL_RTX, mode,
7257 simplify_shift_const (NULL_RTX, LSHIFTRT,
7258 mode, XEXP (x, 0),
7259 pos),
7260 (HOST_WIDE_INT_1U << len) - 1);
7261 else
7262 /* Any other cases we can't handle. */
7263 return x;
7265 /* If we couldn't do this for some reason, return the original
7266 expression. */
7267 if (GET_CODE (tem) == CLOBBER)
7268 return x;
7270 return tem;
7273 /* X is a SET which contains an assignment of one object into
7274 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7275 or certain SUBREGS). If possible, convert it into a series of
7276 logical operations.
7278 We half-heartedly support variable positions, but do not at all
7279 support variable lengths. */
7281 static const_rtx
7282 expand_field_assignment (const_rtx x)
7284 rtx inner;
7285 rtx pos; /* Always counts from low bit. */
7286 int len;
7287 rtx mask, cleared, masked;
7288 scalar_int_mode compute_mode;
7290 /* Loop until we find something we can't simplify. */
7291 while (1)
7293 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7294 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7296 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7297 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7298 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7300 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7301 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7303 inner = XEXP (SET_DEST (x), 0);
7304 len = INTVAL (XEXP (SET_DEST (x), 1));
7305 pos = XEXP (SET_DEST (x), 2);
7307 /* A constant position should stay within the width of INNER. */
7308 if (CONST_INT_P (pos)
7309 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7310 break;
7312 if (BITS_BIG_ENDIAN)
7314 if (CONST_INT_P (pos))
7315 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7316 - INTVAL (pos));
7317 else if (GET_CODE (pos) == MINUS
7318 && CONST_INT_P (XEXP (pos, 1))
7319 && (INTVAL (XEXP (pos, 1))
7320 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7321 /* If position is ADJUST - X, new position is X. */
7322 pos = XEXP (pos, 0);
7323 else
7325 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7326 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7327 gen_int_mode (prec - len,
7328 GET_MODE (pos)),
7329 pos);
7334 /* A SUBREG between two modes that occupy the same numbers of words
7335 can be done by moving the SUBREG to the source. */
7336 else if (GET_CODE (SET_DEST (x)) == SUBREG
7337 /* We need SUBREGs to compute nonzero_bits properly. */
7338 && nonzero_sign_valid
7339 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7340 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7341 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7342 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7344 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7345 gen_lowpart
7346 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7347 SET_SRC (x)));
7348 continue;
7350 else
7351 break;
7353 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7354 inner = SUBREG_REG (inner);
7356 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7357 if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7359 /* Don't do anything for vector or complex integral types. */
7360 if (! FLOAT_MODE_P (GET_MODE (inner)))
7361 break;
7363 /* Try to find an integral mode to pun with. */
7364 if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7365 .exists (&compute_mode))
7366 break;
7368 inner = gen_lowpart (compute_mode, inner);
7371 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7372 if (len >= HOST_BITS_PER_WIDE_INT)
7373 break;
7375 /* Don't try to compute in too wide unsupported modes. */
7376 if (!targetm.scalar_mode_supported_p (compute_mode))
7377 break;
7379 /* Now compute the equivalent expression. Make a copy of INNER
7380 for the SET_DEST in case it is a MEM into which we will substitute;
7381 we don't want shared RTL in that case. */
7382 mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7383 compute_mode);
7384 cleared = simplify_gen_binary (AND, compute_mode,
7385 simplify_gen_unary (NOT, compute_mode,
7386 simplify_gen_binary (ASHIFT,
7387 compute_mode,
7388 mask, pos),
7389 compute_mode),
7390 inner);
7391 masked = simplify_gen_binary (ASHIFT, compute_mode,
7392 simplify_gen_binary (
7393 AND, compute_mode,
7394 gen_lowpart (compute_mode, SET_SRC (x)),
7395 mask),
7396 pos);
7398 x = gen_rtx_SET (copy_rtx (inner),
7399 simplify_gen_binary (IOR, compute_mode,
7400 cleared, masked));
7403 return x;
7406 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7407 it is an RTX that represents the (variable) starting position; otherwise,
7408 POS is the (constant) starting bit position. Both are counted from the LSB.
7410 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7412 IN_DEST is nonzero if this is a reference in the destination of a SET.
7413 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7414 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7415 be used.
7417 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7418 ZERO_EXTRACT should be built even for bits starting at bit 0.
7420 MODE is the desired mode of the result (if IN_DEST == 0).
7422 The result is an RTX for the extraction or NULL_RTX if the target
7423 can't handle it. */
7425 static rtx
7426 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7427 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7428 int in_dest, int in_compare)
7430 /* This mode describes the size of the storage area
7431 to fetch the overall value from. Within that, we
7432 ignore the POS lowest bits, etc. */
7433 machine_mode is_mode = GET_MODE (inner);
7434 machine_mode inner_mode;
7435 scalar_int_mode wanted_inner_mode;
7436 scalar_int_mode wanted_inner_reg_mode = word_mode;
7437 scalar_int_mode pos_mode = word_mode;
7438 machine_mode extraction_mode = word_mode;
7439 rtx new_rtx = 0;
7440 rtx orig_pos_rtx = pos_rtx;
7441 HOST_WIDE_INT orig_pos;
7443 if (pos_rtx && CONST_INT_P (pos_rtx))
7444 pos = INTVAL (pos_rtx), pos_rtx = 0;
7446 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7448 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7449 consider just the QI as the memory to extract from.
7450 The subreg adds or removes high bits; its mode is
7451 irrelevant to the meaning of this extraction,
7452 since POS and LEN count from the lsb. */
7453 if (MEM_P (SUBREG_REG (inner)))
7454 is_mode = GET_MODE (SUBREG_REG (inner));
7455 inner = SUBREG_REG (inner);
7457 else if (GET_CODE (inner) == ASHIFT
7458 && CONST_INT_P (XEXP (inner, 1))
7459 && pos_rtx == 0 && pos == 0
7460 && len > UINTVAL (XEXP (inner, 1)))
7462 /* We're extracting the least significant bits of an rtx
7463 (ashift X (const_int C)), where LEN > C. Extract the
7464 least significant (LEN - C) bits of X, giving an rtx
7465 whose mode is MODE, then shift it left C times. */
7466 new_rtx = make_extraction (mode, XEXP (inner, 0),
7467 0, 0, len - INTVAL (XEXP (inner, 1)),
7468 unsignedp, in_dest, in_compare);
7469 if (new_rtx != 0)
7470 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7472 else if (GET_CODE (inner) == TRUNCATE)
7473 inner = XEXP (inner, 0);
7475 inner_mode = GET_MODE (inner);
7477 /* See if this can be done without an extraction. We never can if the
7478 width of the field is not the same as that of some integer mode. For
7479 registers, we can only avoid the extraction if the position is at the
7480 low-order bit and this is either not in the destination or we have the
7481 appropriate STRICT_LOW_PART operation available.
7483 For MEM, we can avoid an extract if the field starts on an appropriate
7484 boundary and we can change the mode of the memory reference. */
7486 scalar_int_mode tmode;
7487 if (int_mode_for_size (len, 1).exists (&tmode)
7488 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7489 && !MEM_P (inner)
7490 && (pos == 0 || REG_P (inner))
7491 && (inner_mode == tmode
7492 || !REG_P (inner)
7493 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7494 || reg_truncated_to_mode (tmode, inner))
7495 && (! in_dest
7496 || (REG_P (inner)
7497 && have_insn_for (STRICT_LOW_PART, tmode))))
7498 || (MEM_P (inner) && pos_rtx == 0
7499 && (pos
7500 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7501 : BITS_PER_UNIT)) == 0
7502 /* We can't do this if we are widening INNER_MODE (it
7503 may not be aligned, for one thing). */
7504 && !paradoxical_subreg_p (tmode, inner_mode)
7505 && (inner_mode == tmode
7506 || (! mode_dependent_address_p (XEXP (inner, 0),
7507 MEM_ADDR_SPACE (inner))
7508 && ! MEM_VOLATILE_P (inner))))))
7510 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7511 field. If the original and current mode are the same, we need not
7512 adjust the offset. Otherwise, we do if bytes big endian.
7514 If INNER is not a MEM, get a piece consisting of just the field
7515 of interest (in this case POS % BITS_PER_WORD must be 0). */
7517 if (MEM_P (inner))
7519 HOST_WIDE_INT offset;
7521 /* POS counts from lsb, but make OFFSET count in memory order. */
7522 if (BYTES_BIG_ENDIAN)
7523 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7524 else
7525 offset = pos / BITS_PER_UNIT;
7527 new_rtx = adjust_address_nv (inner, tmode, offset);
7529 else if (REG_P (inner))
7531 if (tmode != inner_mode)
7533 /* We can't call gen_lowpart in a DEST since we
7534 always want a SUBREG (see below) and it would sometimes
7535 return a new hard register. */
7536 if (pos || in_dest)
7538 unsigned int offset
7539 = subreg_offset_from_lsb (tmode, inner_mode, pos);
7541 /* Avoid creating invalid subregs, for example when
7542 simplifying (x>>32)&255. */
7543 if (!validate_subreg (tmode, inner_mode, inner, offset))
7544 return NULL_RTX;
7546 new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7548 else
7549 new_rtx = gen_lowpart (tmode, inner);
7551 else
7552 new_rtx = inner;
7554 else
7555 new_rtx = force_to_mode (inner, tmode,
7556 len >= HOST_BITS_PER_WIDE_INT
7557 ? HOST_WIDE_INT_M1U
7558 : (HOST_WIDE_INT_1U << len) - 1, 0);
7560 /* If this extraction is going into the destination of a SET,
7561 make a STRICT_LOW_PART unless we made a MEM. */
7563 if (in_dest)
7564 return (MEM_P (new_rtx) ? new_rtx
7565 : (GET_CODE (new_rtx) != SUBREG
7566 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7567 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7569 if (mode == tmode)
7570 return new_rtx;
7572 if (CONST_SCALAR_INT_P (new_rtx))
7573 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7574 mode, new_rtx, tmode);
7576 /* If we know that no extraneous bits are set, and that the high
7577 bit is not set, convert the extraction to the cheaper of
7578 sign and zero extension, that are equivalent in these cases. */
7579 if (flag_expensive_optimizations
7580 && (HWI_COMPUTABLE_MODE_P (tmode)
7581 && ((nonzero_bits (new_rtx, tmode)
7582 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7583 == 0)))
7585 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7586 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7588 /* Prefer ZERO_EXTENSION, since it gives more information to
7589 backends. */
7590 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7591 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7592 return temp;
7593 return temp1;
7596 /* Otherwise, sign- or zero-extend unless we already are in the
7597 proper mode. */
7599 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7600 mode, new_rtx));
7603 /* Unless this is a COMPARE or we have a funny memory reference,
7604 don't do anything with zero-extending field extracts starting at
7605 the low-order bit since they are simple AND operations. */
7606 if (pos_rtx == 0 && pos == 0 && ! in_dest
7607 && ! in_compare && unsignedp)
7608 return 0;
7610 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7611 if the position is not a constant and the length is not 1. In all
7612 other cases, we would only be going outside our object in cases when
7613 an original shift would have been undefined. */
7614 if (MEM_P (inner)
7615 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7616 || (pos_rtx != 0 && len != 1)))
7617 return 0;
7619 enum extraction_pattern pattern = (in_dest ? EP_insv
7620 : unsignedp ? EP_extzv : EP_extv);
7622 /* If INNER is not from memory, we want it to have the mode of a register
7623 extraction pattern's structure operand, or word_mode if there is no
7624 such pattern. The same applies to extraction_mode and pos_mode
7625 and their respective operands.
7627 For memory, assume that the desired extraction_mode and pos_mode
7628 are the same as for a register operation, since at present we don't
7629 have named patterns for aligned memory structures. */
7630 struct extraction_insn insn;
7631 if (get_best_reg_extraction_insn (&insn, pattern,
7632 GET_MODE_BITSIZE (inner_mode), mode))
7634 wanted_inner_reg_mode = insn.struct_mode.require ();
7635 pos_mode = insn.pos_mode;
7636 extraction_mode = insn.field_mode;
7639 /* Never narrow an object, since that might not be safe. */
7641 if (mode != VOIDmode
7642 && partial_subreg_p (extraction_mode, mode))
7643 extraction_mode = mode;
7645 if (!MEM_P (inner))
7646 wanted_inner_mode = wanted_inner_reg_mode;
7647 else
7649 /* Be careful not to go beyond the extracted object and maintain the
7650 natural alignment of the memory. */
7651 wanted_inner_mode = smallest_int_mode_for_size (len);
7652 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7653 > GET_MODE_BITSIZE (wanted_inner_mode))
7654 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7657 orig_pos = pos;
7659 if (BITS_BIG_ENDIAN)
7661 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7662 BITS_BIG_ENDIAN style. If position is constant, compute new
7663 position. Otherwise, build subtraction.
7664 Note that POS is relative to the mode of the original argument.
7665 If it's a MEM we need to recompute POS relative to that.
7666 However, if we're extracting from (or inserting into) a register,
7667 we want to recompute POS relative to wanted_inner_mode. */
7668 int width = (MEM_P (inner)
7669 ? GET_MODE_BITSIZE (is_mode)
7670 : GET_MODE_BITSIZE (wanted_inner_mode));
7672 if (pos_rtx == 0)
7673 pos = width - len - pos;
7674 else
7675 pos_rtx
7676 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7677 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7678 pos_rtx);
7679 /* POS may be less than 0 now, but we check for that below.
7680 Note that it can only be less than 0 if !MEM_P (inner). */
7683 /* If INNER has a wider mode, and this is a constant extraction, try to
7684 make it smaller and adjust the byte to point to the byte containing
7685 the value. */
7686 if (wanted_inner_mode != VOIDmode
7687 && inner_mode != wanted_inner_mode
7688 && ! pos_rtx
7689 && partial_subreg_p (wanted_inner_mode, is_mode)
7690 && MEM_P (inner)
7691 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7692 && ! MEM_VOLATILE_P (inner))
7694 int offset = 0;
7696 /* The computations below will be correct if the machine is big
7697 endian in both bits and bytes or little endian in bits and bytes.
7698 If it is mixed, we must adjust. */
7700 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7701 adjust OFFSET to compensate. */
7702 if (BYTES_BIG_ENDIAN
7703 && paradoxical_subreg_p (is_mode, inner_mode))
7704 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7706 /* We can now move to the desired byte. */
7707 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7708 * GET_MODE_SIZE (wanted_inner_mode);
7709 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7711 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7712 && is_mode != wanted_inner_mode)
7713 offset = (GET_MODE_SIZE (is_mode)
7714 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7716 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7719 /* If INNER is not memory, get it into the proper mode. If we are changing
7720 its mode, POS must be a constant and smaller than the size of the new
7721 mode. */
7722 else if (!MEM_P (inner))
7724 /* On the LHS, don't create paradoxical subregs implicitely truncating
7725 the register unless TRULY_NOOP_TRUNCATION. */
7726 if (in_dest
7727 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7728 wanted_inner_mode))
7729 return NULL_RTX;
7731 if (GET_MODE (inner) != wanted_inner_mode
7732 && (pos_rtx != 0
7733 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7734 return NULL_RTX;
7736 if (orig_pos < 0)
7737 return NULL_RTX;
7739 inner = force_to_mode (inner, wanted_inner_mode,
7740 pos_rtx
7741 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7742 ? HOST_WIDE_INT_M1U
7743 : (((HOST_WIDE_INT_1U << len) - 1)
7744 << orig_pos),
7748 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7749 have to zero extend. Otherwise, we can just use a SUBREG.
7751 We dealt with constant rtxes earlier, so pos_rtx cannot
7752 have VOIDmode at this point. */
7753 if (pos_rtx != 0
7754 && (GET_MODE_SIZE (pos_mode)
7755 > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7757 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7758 GET_MODE (pos_rtx));
7760 /* If we know that no extraneous bits are set, and that the high
7761 bit is not set, convert extraction to cheaper one - either
7762 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7763 cases. */
7764 if (flag_expensive_optimizations
7765 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7766 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7767 & ~(((unsigned HOST_WIDE_INT)
7768 GET_MODE_MASK (GET_MODE (pos_rtx)))
7769 >> 1))
7770 == 0)))
7772 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7773 GET_MODE (pos_rtx));
7775 /* Prefer ZERO_EXTENSION, since it gives more information to
7776 backends. */
7777 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7778 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7779 temp = temp1;
7781 pos_rtx = temp;
7784 /* Make POS_RTX unless we already have it and it is correct. If we don't
7785 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7786 be a CONST_INT. */
7787 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7788 pos_rtx = orig_pos_rtx;
7790 else if (pos_rtx == 0)
7791 pos_rtx = GEN_INT (pos);
7793 /* Make the required operation. See if we can use existing rtx. */
7794 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7795 extraction_mode, inner, GEN_INT (len), pos_rtx);
7796 if (! in_dest)
7797 new_rtx = gen_lowpart (mode, new_rtx);
7799 return new_rtx;
7802 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7803 can be commuted with any other operations in X. Return X without
7804 that shift if so. */
7806 static rtx
7807 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7809 enum rtx_code code = GET_CODE (x);
7810 rtx tem;
7812 switch (code)
7814 case ASHIFT:
7815 /* This is the shift itself. If it is wide enough, we will return
7816 either the value being shifted if the shift count is equal to
7817 COUNT or a shift for the difference. */
7818 if (CONST_INT_P (XEXP (x, 1))
7819 && INTVAL (XEXP (x, 1)) >= count)
7820 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7821 INTVAL (XEXP (x, 1)) - count);
7822 break;
7824 case NEG: case NOT:
7825 if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7826 return simplify_gen_unary (code, mode, tem, mode);
7828 break;
7830 case PLUS: case IOR: case XOR: case AND:
7831 /* If we can safely shift this constant and we find the inner shift,
7832 make a new operation. */
7833 if (CONST_INT_P (XEXP (x, 1))
7834 && (UINTVAL (XEXP (x, 1))
7835 & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
7836 && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7838 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7839 return simplify_gen_binary (code, mode, tem,
7840 gen_int_mode (val, mode));
7842 break;
7844 default:
7845 break;
7848 return 0;
7851 /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
7852 level of the expression and MODE is its mode. IN_CODE is as for
7853 make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
7854 that should be used when recursing on operands of *X_PTR.
7856 There are two possible actions:
7858 - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
7859 equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
7861 - Return a new rtx, which the caller returns directly. */
7863 static rtx
7864 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
7865 enum rtx_code in_code,
7866 enum rtx_code *next_code_ptr)
7868 rtx x = *x_ptr;
7869 enum rtx_code next_code = *next_code_ptr;
7870 enum rtx_code code = GET_CODE (x);
7871 int mode_width = GET_MODE_PRECISION (mode);
7872 rtx rhs, lhs;
7873 rtx new_rtx = 0;
7874 int i;
7875 rtx tem;
7876 scalar_int_mode inner_mode;
7877 bool equality_comparison = false;
7879 if (in_code == EQ)
7881 equality_comparison = true;
7882 in_code = COMPARE;
7885 /* Process depending on the code of this operation. If NEW is set
7886 nonzero, it will be returned. */
7888 switch (code)
7890 case ASHIFT:
7891 /* Convert shifts by constants into multiplications if inside
7892 an address. */
7893 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7894 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7895 && INTVAL (XEXP (x, 1)) >= 0)
7897 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7898 HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
7900 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7901 if (GET_CODE (new_rtx) == NEG)
7903 new_rtx = XEXP (new_rtx, 0);
7904 multval = -multval;
7906 multval = trunc_int_for_mode (multval, mode);
7907 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7909 break;
7911 case PLUS:
7912 lhs = XEXP (x, 0);
7913 rhs = XEXP (x, 1);
7914 lhs = make_compound_operation (lhs, next_code);
7915 rhs = make_compound_operation (rhs, next_code);
7916 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
7918 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7919 XEXP (lhs, 1));
7920 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7922 else if (GET_CODE (lhs) == MULT
7923 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7925 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7926 simplify_gen_unary (NEG, mode,
7927 XEXP (lhs, 1),
7928 mode));
7929 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7931 else
7933 SUBST (XEXP (x, 0), lhs);
7934 SUBST (XEXP (x, 1), rhs);
7936 maybe_swap_commutative_operands (x);
7937 return x;
7939 case MINUS:
7940 lhs = XEXP (x, 0);
7941 rhs = XEXP (x, 1);
7942 lhs = make_compound_operation (lhs, next_code);
7943 rhs = make_compound_operation (rhs, next_code);
7944 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
7946 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7947 XEXP (rhs, 1));
7948 return simplify_gen_binary (PLUS, mode, tem, lhs);
7950 else if (GET_CODE (rhs) == MULT
7951 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7953 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7954 simplify_gen_unary (NEG, mode,
7955 XEXP (rhs, 1),
7956 mode));
7957 return simplify_gen_binary (PLUS, mode, tem, lhs);
7959 else
7961 SUBST (XEXP (x, 0), lhs);
7962 SUBST (XEXP (x, 1), rhs);
7963 return x;
7966 case AND:
7967 /* If the second operand is not a constant, we can't do anything
7968 with it. */
7969 if (!CONST_INT_P (XEXP (x, 1)))
7970 break;
7972 /* If the constant is a power of two minus one and the first operand
7973 is a logical right shift, make an extraction. */
7974 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7975 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7977 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7978 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7979 0, in_code == COMPARE);
7982 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7983 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7984 && subreg_lowpart_p (XEXP (x, 0))
7985 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
7986 &inner_mode)
7987 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7988 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7990 rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
7991 new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
7992 new_rtx = make_extraction (inner_mode, new_rtx, 0,
7993 XEXP (inner_x0, 1),
7994 i, 1, 0, in_code == COMPARE);
7996 /* If we narrowed the mode when dropping the subreg, then we lose. */
7997 if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
7998 new_rtx = NULL;
8000 /* If that didn't give anything, see if the AND simplifies on
8001 its own. */
8002 if (!new_rtx && i >= 0)
8004 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8005 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
8006 0, in_code == COMPARE);
8009 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8010 else if ((GET_CODE (XEXP (x, 0)) == XOR
8011 || GET_CODE (XEXP (x, 0)) == IOR)
8012 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8013 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8014 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8016 /* Apply the distributive law, and then try to make extractions. */
8017 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8018 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8019 XEXP (x, 1)),
8020 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8021 XEXP (x, 1)));
8022 new_rtx = make_compound_operation (new_rtx, in_code);
8025 /* If we are have (and (rotate X C) M) and C is larger than the number
8026 of bits in M, this is an extraction. */
8028 else if (GET_CODE (XEXP (x, 0)) == ROTATE
8029 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8030 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8031 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8033 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8034 new_rtx = make_extraction (mode, new_rtx,
8035 (GET_MODE_PRECISION (mode)
8036 - INTVAL (XEXP (XEXP (x, 0), 1))),
8037 NULL_RTX, i, 1, 0, in_code == COMPARE);
8040 /* On machines without logical shifts, if the operand of the AND is
8041 a logical shift and our mask turns off all the propagated sign
8042 bits, we can replace the logical shift with an arithmetic shift. */
8043 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8044 && !have_insn_for (LSHIFTRT, mode)
8045 && have_insn_for (ASHIFTRT, mode)
8046 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8047 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8048 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8049 && mode_width <= HOST_BITS_PER_WIDE_INT)
8051 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8053 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8054 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8055 SUBST (XEXP (x, 0),
8056 gen_rtx_ASHIFTRT (mode,
8057 make_compound_operation
8058 (XEXP (XEXP (x, 0), 0), next_code),
8059 XEXP (XEXP (x, 0), 1)));
8062 /* If the constant is one less than a power of two, this might be
8063 representable by an extraction even if no shift is present.
8064 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8065 we are in a COMPARE. */
8066 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8067 new_rtx = make_extraction (mode,
8068 make_compound_operation (XEXP (x, 0),
8069 next_code),
8070 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
8072 /* If we are in a comparison and this is an AND with a power of two,
8073 convert this into the appropriate bit extract. */
8074 else if (in_code == COMPARE
8075 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8076 && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8077 new_rtx = make_extraction (mode,
8078 make_compound_operation (XEXP (x, 0),
8079 next_code),
8080 i, NULL_RTX, 1, 1, 0, 1);
8082 /* If the one operand is a paradoxical subreg of a register or memory and
8083 the constant (limited to the smaller mode) has only zero bits where
8084 the sub expression has known zero bits, this can be expressed as
8085 a zero_extend. */
8086 else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8088 rtx sub;
8090 sub = XEXP (XEXP (x, 0), 0);
8091 machine_mode sub_mode = GET_MODE (sub);
8092 if ((REG_P (sub) || MEM_P (sub))
8093 && GET_MODE_PRECISION (sub_mode) < mode_width)
8095 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8096 unsigned HOST_WIDE_INT mask;
8098 /* original AND constant with all the known zero bits set */
8099 mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8100 if ((mask & mode_mask) == mode_mask)
8102 new_rtx = make_compound_operation (sub, next_code);
8103 new_rtx = make_extraction (mode, new_rtx, 0, 0,
8104 GET_MODE_PRECISION (sub_mode),
8105 1, 0, in_code == COMPARE);
8110 break;
8112 case LSHIFTRT:
8113 /* If the sign bit is known to be zero, replace this with an
8114 arithmetic shift. */
8115 if (have_insn_for (ASHIFTRT, mode)
8116 && ! have_insn_for (LSHIFTRT, mode)
8117 && mode_width <= HOST_BITS_PER_WIDE_INT
8118 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8120 new_rtx = gen_rtx_ASHIFTRT (mode,
8121 make_compound_operation (XEXP (x, 0),
8122 next_code),
8123 XEXP (x, 1));
8124 break;
8127 /* fall through */
8129 case ASHIFTRT:
8130 lhs = XEXP (x, 0);
8131 rhs = XEXP (x, 1);
8133 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8134 this is a SIGN_EXTRACT. */
8135 if (CONST_INT_P (rhs)
8136 && GET_CODE (lhs) == ASHIFT
8137 && CONST_INT_P (XEXP (lhs, 1))
8138 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8139 && INTVAL (XEXP (lhs, 1)) >= 0
8140 && INTVAL (rhs) < mode_width)
8142 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8143 new_rtx = make_extraction (mode, new_rtx,
8144 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8145 NULL_RTX, mode_width - INTVAL (rhs),
8146 code == LSHIFTRT, 0, in_code == COMPARE);
8147 break;
8150 /* See if we have operations between an ASHIFTRT and an ASHIFT.
8151 If so, try to merge the shifts into a SIGN_EXTEND. We could
8152 also do this for some cases of SIGN_EXTRACT, but it doesn't
8153 seem worth the effort; the case checked for occurs on Alpha. */
8155 if (!OBJECT_P (lhs)
8156 && ! (GET_CODE (lhs) == SUBREG
8157 && (OBJECT_P (SUBREG_REG (lhs))))
8158 && CONST_INT_P (rhs)
8159 && INTVAL (rhs) >= 0
8160 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8161 && INTVAL (rhs) < mode_width
8162 && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8163 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
8164 0, NULL_RTX, mode_width - INTVAL (rhs),
8165 code == LSHIFTRT, 0, in_code == COMPARE);
8167 break;
8169 case SUBREG:
8170 /* Call ourselves recursively on the inner expression. If we are
8171 narrowing the object and it has a different RTL code from
8172 what it originally did, do this SUBREG as a force_to_mode. */
8174 rtx inner = SUBREG_REG (x), simplified;
8175 enum rtx_code subreg_code = in_code;
8177 /* If the SUBREG is masking of a logical right shift,
8178 make an extraction. */
8179 if (GET_CODE (inner) == LSHIFTRT
8180 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8181 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8182 && CONST_INT_P (XEXP (inner, 1))
8183 && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8184 && subreg_lowpart_p (x))
8186 new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8187 int width = GET_MODE_PRECISION (inner_mode)
8188 - INTVAL (XEXP (inner, 1));
8189 if (width > mode_width)
8190 width = mode_width;
8191 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8192 width, 1, 0, in_code == COMPARE);
8193 break;
8196 /* If in_code is COMPARE, it isn't always safe to pass it through
8197 to the recursive make_compound_operation call. */
8198 if (subreg_code == COMPARE
8199 && (!subreg_lowpart_p (x)
8200 || GET_CODE (inner) == SUBREG
8201 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8202 is (const_int 0), rather than
8203 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8204 Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8205 for non-equality comparisons against 0 is not equivalent
8206 to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8207 || (GET_CODE (inner) == AND
8208 && CONST_INT_P (XEXP (inner, 1))
8209 && partial_subreg_p (x)
8210 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8211 >= GET_MODE_BITSIZE (mode) - 1)))
8212 subreg_code = SET;
8214 tem = make_compound_operation (inner, subreg_code);
8216 simplified
8217 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8218 if (simplified)
8219 tem = simplified;
8221 if (GET_CODE (tem) != GET_CODE (inner)
8222 && partial_subreg_p (x)
8223 && subreg_lowpart_p (x))
8225 rtx newer
8226 = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, 0);
8228 /* If we have something other than a SUBREG, we might have
8229 done an expansion, so rerun ourselves. */
8230 if (GET_CODE (newer) != SUBREG)
8231 newer = make_compound_operation (newer, in_code);
8233 /* force_to_mode can expand compounds. If it just re-expanded
8234 the compound, use gen_lowpart to convert to the desired
8235 mode. */
8236 if (rtx_equal_p (newer, x)
8237 /* Likewise if it re-expanded the compound only partially.
8238 This happens for SUBREG of ZERO_EXTRACT if they extract
8239 the same number of bits. */
8240 || (GET_CODE (newer) == SUBREG
8241 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8242 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8243 && GET_CODE (inner) == AND
8244 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8245 return gen_lowpart (GET_MODE (x), tem);
8247 return newer;
8250 if (simplified)
8251 return tem;
8253 break;
8255 default:
8256 break;
8259 if (new_rtx)
8260 *x_ptr = gen_lowpart (mode, new_rtx);
8261 *next_code_ptr = next_code;
8262 return NULL_RTX;
8265 /* Look at the expression rooted at X. Look for expressions
8266 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8267 Form these expressions.
8269 Return the new rtx, usually just X.
8271 Also, for machines like the VAX that don't have logical shift insns,
8272 try to convert logical to arithmetic shift operations in cases where
8273 they are equivalent. This undoes the canonicalizations to logical
8274 shifts done elsewhere.
8276 We try, as much as possible, to re-use rtl expressions to save memory.
8278 IN_CODE says what kind of expression we are processing. Normally, it is
8279 SET. In a memory address it is MEM. When processing the arguments of
8280 a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8281 precisely it is an equality comparison against zero. */
8284 make_compound_operation (rtx x, enum rtx_code in_code)
8286 enum rtx_code code = GET_CODE (x);
8287 const char *fmt;
8288 int i, j;
8289 enum rtx_code next_code;
8290 rtx new_rtx, tem;
8292 /* Select the code to be used in recursive calls. Once we are inside an
8293 address, we stay there. If we have a comparison, set to COMPARE,
8294 but once inside, go back to our default of SET. */
8296 next_code = (code == MEM ? MEM
8297 : ((code == COMPARE || COMPARISON_P (x))
8298 && XEXP (x, 1) == const0_rtx) ? COMPARE
8299 : in_code == COMPARE || in_code == EQ ? SET : in_code);
8301 scalar_int_mode mode;
8302 if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8304 rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8305 &next_code);
8306 if (new_rtx)
8307 return new_rtx;
8308 code = GET_CODE (x);
8311 /* Now recursively process each operand of this operation. We need to
8312 handle ZERO_EXTEND specially so that we don't lose track of the
8313 inner mode. */
8314 if (code == ZERO_EXTEND)
8316 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8317 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8318 new_rtx, GET_MODE (XEXP (x, 0)));
8319 if (tem)
8320 return tem;
8321 SUBST (XEXP (x, 0), new_rtx);
8322 return x;
8325 fmt = GET_RTX_FORMAT (code);
8326 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8327 if (fmt[i] == 'e')
8329 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8330 SUBST (XEXP (x, i), new_rtx);
8332 else if (fmt[i] == 'E')
8333 for (j = 0; j < XVECLEN (x, i); j++)
8335 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8336 SUBST (XVECEXP (x, i, j), new_rtx);
8339 maybe_swap_commutative_operands (x);
8340 return x;
8343 /* Given M see if it is a value that would select a field of bits
8344 within an item, but not the entire word. Return -1 if not.
8345 Otherwise, return the starting position of the field, where 0 is the
8346 low-order bit.
8348 *PLEN is set to the length of the field. */
8350 static int
8351 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8353 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8354 int pos = m ? ctz_hwi (m) : -1;
8355 int len = 0;
8357 if (pos >= 0)
8358 /* Now shift off the low-order zero bits and see if we have a
8359 power of two minus 1. */
8360 len = exact_log2 ((m >> pos) + 1);
8362 if (len <= 0)
8363 pos = -1;
8365 *plen = len;
8366 return pos;
8369 /* If X refers to a register that equals REG in value, replace these
8370 references with REG. */
8371 static rtx
8372 canon_reg_for_combine (rtx x, rtx reg)
8374 rtx op0, op1, op2;
8375 const char *fmt;
8376 int i;
8377 bool copied;
8379 enum rtx_code code = GET_CODE (x);
8380 switch (GET_RTX_CLASS (code))
8382 case RTX_UNARY:
8383 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8384 if (op0 != XEXP (x, 0))
8385 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8386 GET_MODE (reg));
8387 break;
8389 case RTX_BIN_ARITH:
8390 case RTX_COMM_ARITH:
8391 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8392 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8393 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8394 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8395 break;
8397 case RTX_COMPARE:
8398 case RTX_COMM_COMPARE:
8399 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8400 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8401 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8402 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8403 GET_MODE (op0), op0, op1);
8404 break;
8406 case RTX_TERNARY:
8407 case RTX_BITFIELD_OPS:
8408 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8409 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8410 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8411 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8412 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8413 GET_MODE (op0), op0, op1, op2);
8414 /* FALLTHRU */
8416 case RTX_OBJ:
8417 if (REG_P (x))
8419 if (rtx_equal_p (get_last_value (reg), x)
8420 || rtx_equal_p (reg, get_last_value (x)))
8421 return reg;
8422 else
8423 break;
8426 /* fall through */
8428 default:
8429 fmt = GET_RTX_FORMAT (code);
8430 copied = false;
8431 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8432 if (fmt[i] == 'e')
8434 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8435 if (op != XEXP (x, i))
8437 if (!copied)
8439 copied = true;
8440 x = copy_rtx (x);
8442 XEXP (x, i) = op;
8445 else if (fmt[i] == 'E')
8447 int j;
8448 for (j = 0; j < XVECLEN (x, i); j++)
8450 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8451 if (op != XVECEXP (x, i, j))
8453 if (!copied)
8455 copied = true;
8456 x = copy_rtx (x);
8458 XVECEXP (x, i, j) = op;
8463 break;
8466 return x;
8469 /* Return X converted to MODE. If the value is already truncated to
8470 MODE we can just return a subreg even though in the general case we
8471 would need an explicit truncation. */
8473 static rtx
8474 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8476 if (!CONST_INT_P (x)
8477 && partial_subreg_p (mode, GET_MODE (x))
8478 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8479 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8481 /* Bit-cast X into an integer mode. */
8482 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8483 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8484 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8485 x, GET_MODE (x));
8488 return gen_lowpart (mode, x);
8491 /* See if X can be simplified knowing that we will only refer to it in
8492 MODE and will only refer to those bits that are nonzero in MASK.
8493 If other bits are being computed or if masking operations are done
8494 that select a superset of the bits in MASK, they can sometimes be
8495 ignored.
8497 Return a possibly simplified expression, but always convert X to
8498 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8500 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8501 are all off in X. This is used when X will be complemented, by either
8502 NOT, NEG, or XOR. */
8504 static rtx
8505 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8506 int just_select)
8508 enum rtx_code code = GET_CODE (x);
8509 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8510 machine_mode op_mode;
8511 unsigned HOST_WIDE_INT nonzero;
8513 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8514 code below will do the wrong thing since the mode of such an
8515 expression is VOIDmode.
8517 Also do nothing if X is a CLOBBER; this can happen if X was
8518 the return value from a call to gen_lowpart. */
8519 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8520 return x;
8522 /* We want to perform the operation in its present mode unless we know
8523 that the operation is valid in MODE, in which case we do the operation
8524 in MODE. */
8525 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8526 && have_insn_for (code, mode))
8527 ? mode : GET_MODE (x));
8529 /* It is not valid to do a right-shift in a narrower mode
8530 than the one it came in with. */
8531 if ((code == LSHIFTRT || code == ASHIFTRT)
8532 && partial_subreg_p (mode, GET_MODE (x)))
8533 op_mode = GET_MODE (x);
8535 /* Truncate MASK to fit OP_MODE. */
8536 if (op_mode)
8537 mask &= GET_MODE_MASK (op_mode);
8539 /* Determine what bits of X are guaranteed to be (non)zero. */
8540 nonzero = nonzero_bits (x, mode);
8542 /* If none of the bits in X are needed, return a zero. */
8543 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8544 x = const0_rtx;
8546 /* If X is a CONST_INT, return a new one. Do this here since the
8547 test below will fail. */
8548 if (CONST_INT_P (x))
8550 if (SCALAR_INT_MODE_P (mode))
8551 return gen_int_mode (INTVAL (x) & mask, mode);
8552 else
8554 x = GEN_INT (INTVAL (x) & mask);
8555 return gen_lowpart_common (mode, x);
8559 /* If X is narrower than MODE and we want all the bits in X's mode, just
8560 get X in the proper mode. */
8561 if (paradoxical_subreg_p (mode, GET_MODE (x))
8562 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8563 return gen_lowpart (mode, x);
8565 /* We can ignore the effect of a SUBREG if it narrows the mode or
8566 if the constant masks to zero all the bits the mode doesn't have. */
8567 if (GET_CODE (x) == SUBREG
8568 && subreg_lowpart_p (x)
8569 && (partial_subreg_p (x)
8570 || (0 == (mask
8571 & GET_MODE_MASK (GET_MODE (x))
8572 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8573 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8575 scalar_int_mode int_mode, xmode;
8576 if (is_a <scalar_int_mode> (mode, &int_mode)
8577 && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8578 /* OP_MODE is either MODE or XMODE, so it must be a scalar
8579 integer too. */
8580 return force_int_to_mode (x, int_mode, xmode,
8581 as_a <scalar_int_mode> (op_mode),
8582 mask, just_select);
8584 return gen_lowpart_or_truncate (mode, x);
8587 /* Subroutine of force_to_mode that handles cases in which both X and
8588 the result are scalar integers. MODE is the mode of the result,
8589 XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8590 is preferred for simplified versions of X. The other arguments
8591 are as for force_to_mode. */
8593 static rtx
8594 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8595 scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8596 int just_select)
8598 enum rtx_code code = GET_CODE (x);
8599 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8600 unsigned HOST_WIDE_INT fuller_mask;
8601 rtx op0, op1, temp;
8603 /* When we have an arithmetic operation, or a shift whose count we
8604 do not know, we need to assume that all bits up to the highest-order
8605 bit in MASK will be needed. This is how we form such a mask. */
8606 if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8607 fuller_mask = HOST_WIDE_INT_M1U;
8608 else
8609 fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1))
8610 - 1);
8612 switch (code)
8614 case CLOBBER:
8615 /* If X is a (clobber (const_int)), return it since we know we are
8616 generating something that won't match. */
8617 return x;
8619 case SIGN_EXTEND:
8620 case ZERO_EXTEND:
8621 case ZERO_EXTRACT:
8622 case SIGN_EXTRACT:
8623 x = expand_compound_operation (x);
8624 if (GET_CODE (x) != code)
8625 return force_to_mode (x, mode, mask, next_select);
8626 break;
8628 case TRUNCATE:
8629 /* Similarly for a truncate. */
8630 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8632 case AND:
8633 /* If this is an AND with a constant, convert it into an AND
8634 whose constant is the AND of that constant with MASK. If it
8635 remains an AND of MASK, delete it since it is redundant. */
8637 if (CONST_INT_P (XEXP (x, 1)))
8639 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8640 mask & INTVAL (XEXP (x, 1)));
8641 xmode = op_mode;
8643 /* If X is still an AND, see if it is an AND with a mask that
8644 is just some low-order bits. If so, and it is MASK, we don't
8645 need it. */
8647 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8648 && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8649 x = XEXP (x, 0);
8651 /* If it remains an AND, try making another AND with the bits
8652 in the mode mask that aren't in MASK turned on. If the
8653 constant in the AND is wide enough, this might make a
8654 cheaper constant. */
8656 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8657 && GET_MODE_MASK (xmode) != mask
8658 && HWI_COMPUTABLE_MODE_P (xmode))
8660 unsigned HOST_WIDE_INT cval
8661 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8662 rtx y;
8664 y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8665 gen_int_mode (cval, xmode));
8666 if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8667 < set_src_cost (x, xmode, optimize_this_for_speed_p))
8668 x = y;
8671 break;
8674 goto binop;
8676 case PLUS:
8677 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8678 low-order bits (as in an alignment operation) and FOO is already
8679 aligned to that boundary, mask C1 to that boundary as well.
8680 This may eliminate that PLUS and, later, the AND. */
8683 unsigned int width = GET_MODE_PRECISION (mode);
8684 unsigned HOST_WIDE_INT smask = mask;
8686 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8687 number, sign extend it. */
8689 if (width < HOST_BITS_PER_WIDE_INT
8690 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8691 smask |= HOST_WIDE_INT_M1U << width;
8693 if (CONST_INT_P (XEXP (x, 1))
8694 && pow2p_hwi (- smask)
8695 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8696 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8697 return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8698 (INTVAL (XEXP (x, 1)) & smask)),
8699 mode, smask, next_select);
8702 /* fall through */
8704 case MULT:
8705 /* Substituting into the operands of a widening MULT is not likely to
8706 create RTL matching a machine insn. */
8707 if (code == MULT
8708 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8709 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8710 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8711 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8712 && REG_P (XEXP (XEXP (x, 0), 0))
8713 && REG_P (XEXP (XEXP (x, 1), 0)))
8714 return gen_lowpart_or_truncate (mode, x);
8716 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8717 most significant bit in MASK since carries from those bits will
8718 affect the bits we are interested in. */
8719 mask = fuller_mask;
8720 goto binop;
8722 case MINUS:
8723 /* If X is (minus C Y) where C's least set bit is larger than any bit
8724 in the mask, then we may replace with (neg Y). */
8725 if (CONST_INT_P (XEXP (x, 0))
8726 && least_bit_hwi (UINTVAL (XEXP (x, 0))) > mask)
8728 x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8729 return force_to_mode (x, mode, mask, next_select);
8732 /* Similarly, if C contains every bit in the fuller_mask, then we may
8733 replace with (not Y). */
8734 if (CONST_INT_P (XEXP (x, 0))
8735 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8737 x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8738 return force_to_mode (x, mode, mask, next_select);
8741 mask = fuller_mask;
8742 goto binop;
8744 case IOR:
8745 case XOR:
8746 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8747 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8748 operation which may be a bitfield extraction. Ensure that the
8749 constant we form is not wider than the mode of X. */
8751 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8752 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8753 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8754 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8755 && CONST_INT_P (XEXP (x, 1))
8756 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8757 + floor_log2 (INTVAL (XEXP (x, 1))))
8758 < GET_MODE_PRECISION (xmode))
8759 && (UINTVAL (XEXP (x, 1))
8760 & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8762 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8763 << INTVAL (XEXP (XEXP (x, 0), 1)),
8764 xmode);
8765 temp = simplify_gen_binary (GET_CODE (x), xmode,
8766 XEXP (XEXP (x, 0), 0), temp);
8767 x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8768 XEXP (XEXP (x, 0), 1));
8769 return force_to_mode (x, mode, mask, next_select);
8772 binop:
8773 /* For most binary operations, just propagate into the operation and
8774 change the mode if we have an operation of that mode. */
8776 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8777 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8779 /* If we ended up truncating both operands, truncate the result of the
8780 operation instead. */
8781 if (GET_CODE (op0) == TRUNCATE
8782 && GET_CODE (op1) == TRUNCATE)
8784 op0 = XEXP (op0, 0);
8785 op1 = XEXP (op1, 0);
8788 op0 = gen_lowpart_or_truncate (op_mode, op0);
8789 op1 = gen_lowpart_or_truncate (op_mode, op1);
8791 if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8793 x = simplify_gen_binary (code, op_mode, op0, op1);
8794 xmode = op_mode;
8796 break;
8798 case ASHIFT:
8799 /* For left shifts, do the same, but just for the first operand.
8800 However, we cannot do anything with shifts where we cannot
8801 guarantee that the counts are smaller than the size of the mode
8802 because such a count will have a different meaning in a
8803 wider mode. */
8805 if (! (CONST_INT_P (XEXP (x, 1))
8806 && INTVAL (XEXP (x, 1)) >= 0
8807 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8808 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8809 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8810 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8811 break;
8813 /* If the shift count is a constant and we can do arithmetic in
8814 the mode of the shift, refine which bits we need. Otherwise, use the
8815 conservative form of the mask. */
8816 if (CONST_INT_P (XEXP (x, 1))
8817 && INTVAL (XEXP (x, 1)) >= 0
8818 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8819 && HWI_COMPUTABLE_MODE_P (op_mode))
8820 mask >>= INTVAL (XEXP (x, 1));
8821 else
8822 mask = fuller_mask;
8824 op0 = gen_lowpart_or_truncate (op_mode,
8825 force_to_mode (XEXP (x, 0), op_mode,
8826 mask, next_select));
8828 if (op_mode != xmode || op0 != XEXP (x, 0))
8830 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8831 xmode = op_mode;
8833 break;
8835 case LSHIFTRT:
8836 /* Here we can only do something if the shift count is a constant,
8837 this shift constant is valid for the host, and we can do arithmetic
8838 in OP_MODE. */
8840 if (CONST_INT_P (XEXP (x, 1))
8841 && INTVAL (XEXP (x, 1)) >= 0
8842 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8843 && HWI_COMPUTABLE_MODE_P (op_mode))
8845 rtx inner = XEXP (x, 0);
8846 unsigned HOST_WIDE_INT inner_mask;
8848 /* Select the mask of the bits we need for the shift operand. */
8849 inner_mask = mask << INTVAL (XEXP (x, 1));
8851 /* We can only change the mode of the shift if we can do arithmetic
8852 in the mode of the shift and INNER_MASK is no wider than the
8853 width of X's mode. */
8854 if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
8855 op_mode = xmode;
8857 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8859 if (xmode != op_mode || inner != XEXP (x, 0))
8861 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8862 xmode = op_mode;
8866 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8867 shift and AND produces only copies of the sign bit (C2 is one less
8868 than a power of two), we can do this with just a shift. */
8870 if (GET_CODE (x) == LSHIFTRT
8871 && CONST_INT_P (XEXP (x, 1))
8872 /* The shift puts one of the sign bit copies in the least significant
8873 bit. */
8874 && ((INTVAL (XEXP (x, 1))
8875 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8876 >= GET_MODE_PRECISION (xmode))
8877 && pow2p_hwi (mask + 1)
8878 /* Number of bits left after the shift must be more than the mask
8879 needs. */
8880 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8881 <= GET_MODE_PRECISION (xmode))
8882 /* Must be more sign bit copies than the mask needs. */
8883 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8884 >= exact_log2 (mask + 1)))
8885 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
8886 GEN_INT (GET_MODE_PRECISION (xmode)
8887 - exact_log2 (mask + 1)));
8889 goto shiftrt;
8891 case ASHIFTRT:
8892 /* If we are just looking for the sign bit, we don't need this shift at
8893 all, even if it has a variable count. */
8894 if (val_signbit_p (xmode, mask))
8895 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8897 /* If this is a shift by a constant, get a mask that contains those bits
8898 that are not copies of the sign bit. We then have two cases: If
8899 MASK only includes those bits, this can be a logical shift, which may
8900 allow simplifications. If MASK is a single-bit field not within
8901 those bits, we are requesting a copy of the sign bit and hence can
8902 shift the sign bit to the appropriate location. */
8904 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8905 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8907 unsigned HOST_WIDE_INT nonzero;
8908 int i;
8910 /* If the considered data is wider than HOST_WIDE_INT, we can't
8911 represent a mask for all its bits in a single scalar.
8912 But we only care about the lower bits, so calculate these. */
8914 if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
8916 nonzero = HOST_WIDE_INT_M1U;
8918 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8919 is the number of bits a full-width mask would have set.
8920 We need only shift if these are fewer than nonzero can
8921 hold. If not, we must keep all bits set in nonzero. */
8923 if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
8924 < HOST_BITS_PER_WIDE_INT)
8925 nonzero >>= INTVAL (XEXP (x, 1))
8926 + HOST_BITS_PER_WIDE_INT
8927 - GET_MODE_PRECISION (xmode);
8929 else
8931 nonzero = GET_MODE_MASK (xmode);
8932 nonzero >>= INTVAL (XEXP (x, 1));
8935 if ((mask & ~nonzero) == 0)
8937 x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
8938 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8939 if (GET_CODE (x) != ASHIFTRT)
8940 return force_to_mode (x, mode, mask, next_select);
8943 else if ((i = exact_log2 (mask)) >= 0)
8945 x = simplify_shift_const
8946 (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
8947 GET_MODE_PRECISION (xmode) - 1 - i);
8949 if (GET_CODE (x) != ASHIFTRT)
8950 return force_to_mode (x, mode, mask, next_select);
8954 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8955 even if the shift count isn't a constant. */
8956 if (mask == 1)
8957 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
8959 shiftrt:
8961 /* If this is a zero- or sign-extension operation that just affects bits
8962 we don't care about, remove it. Be sure the call above returned
8963 something that is still a shift. */
8965 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8966 && CONST_INT_P (XEXP (x, 1))
8967 && INTVAL (XEXP (x, 1)) >= 0
8968 && (INTVAL (XEXP (x, 1))
8969 <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
8970 && GET_CODE (XEXP (x, 0)) == ASHIFT
8971 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8972 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8973 next_select);
8975 break;
8977 case ROTATE:
8978 case ROTATERT:
8979 /* If the shift count is constant and we can do computations
8980 in the mode of X, compute where the bits we care about are.
8981 Otherwise, we can't do anything. Don't change the mode of
8982 the shift or propagate MODE into the shift, though. */
8983 if (CONST_INT_P (XEXP (x, 1))
8984 && INTVAL (XEXP (x, 1)) >= 0)
8986 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8987 xmode, gen_int_mode (mask, xmode),
8988 XEXP (x, 1));
8989 if (temp && CONST_INT_P (temp))
8990 x = simplify_gen_binary (code, xmode,
8991 force_to_mode (XEXP (x, 0), xmode,
8992 INTVAL (temp), next_select),
8993 XEXP (x, 1));
8995 break;
8997 case NEG:
8998 /* If we just want the low-order bit, the NEG isn't needed since it
8999 won't change the low-order bit. */
9000 if (mask == 1)
9001 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9003 /* We need any bits less significant than the most significant bit in
9004 MASK since carries from those bits will affect the bits we are
9005 interested in. */
9006 mask = fuller_mask;
9007 goto unop;
9009 case NOT:
9010 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9011 same as the XOR case above. Ensure that the constant we form is not
9012 wider than the mode of X. */
9014 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9015 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9016 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9017 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9018 < GET_MODE_PRECISION (xmode))
9019 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9021 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9022 temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9023 x = simplify_gen_binary (LSHIFTRT, xmode,
9024 temp, XEXP (XEXP (x, 0), 1));
9026 return force_to_mode (x, mode, mask, next_select);
9029 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9030 use the full mask inside the NOT. */
9031 mask = fuller_mask;
9033 unop:
9034 op0 = gen_lowpart_or_truncate (op_mode,
9035 force_to_mode (XEXP (x, 0), mode, mask,
9036 next_select));
9037 if (op_mode != xmode || op0 != XEXP (x, 0))
9039 x = simplify_gen_unary (code, op_mode, op0, op_mode);
9040 xmode = op_mode;
9042 break;
9044 case NE:
9045 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9046 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9047 which is equal to STORE_FLAG_VALUE. */
9048 if ((mask & ~STORE_FLAG_VALUE) == 0
9049 && XEXP (x, 1) == const0_rtx
9050 && GET_MODE (XEXP (x, 0)) == mode
9051 && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9052 && (nonzero_bits (XEXP (x, 0), mode)
9053 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9054 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9056 break;
9058 case IF_THEN_ELSE:
9059 /* We have no way of knowing if the IF_THEN_ELSE can itself be
9060 written in a narrower mode. We play it safe and do not do so. */
9062 op0 = gen_lowpart_or_truncate (xmode,
9063 force_to_mode (XEXP (x, 1), mode,
9064 mask, next_select));
9065 op1 = gen_lowpart_or_truncate (xmode,
9066 force_to_mode (XEXP (x, 2), mode,
9067 mask, next_select));
9068 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9069 x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9070 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9071 op0, op1);
9072 break;
9074 default:
9075 break;
9078 /* Ensure we return a value of the proper mode. */
9079 return gen_lowpart_or_truncate (mode, x);
9082 /* Return nonzero if X is an expression that has one of two values depending on
9083 whether some other value is zero or nonzero. In that case, we return the
9084 value that is being tested, *PTRUE is set to the value if the rtx being
9085 returned has a nonzero value, and *PFALSE is set to the other alternative.
9087 If we return zero, we set *PTRUE and *PFALSE to X. */
9089 static rtx
9090 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9092 machine_mode mode = GET_MODE (x);
9093 enum rtx_code code = GET_CODE (x);
9094 rtx cond0, cond1, true0, true1, false0, false1;
9095 unsigned HOST_WIDE_INT nz;
9096 scalar_int_mode int_mode;
9098 /* If we are comparing a value against zero, we are done. */
9099 if ((code == NE || code == EQ)
9100 && XEXP (x, 1) == const0_rtx)
9102 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9103 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9104 return XEXP (x, 0);
9107 /* If this is a unary operation whose operand has one of two values, apply
9108 our opcode to compute those values. */
9109 else if (UNARY_P (x)
9110 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9112 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9113 *pfalse = simplify_gen_unary (code, mode, false0,
9114 GET_MODE (XEXP (x, 0)));
9115 return cond0;
9118 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9119 make can't possibly match and would suppress other optimizations. */
9120 else if (code == COMPARE)
9123 /* If this is a binary operation, see if either side has only one of two
9124 values. If either one does or if both do and they are conditional on
9125 the same value, compute the new true and false values. */
9126 else if (BINARY_P (x))
9128 rtx op0 = XEXP (x, 0);
9129 rtx op1 = XEXP (x, 1);
9130 cond0 = if_then_else_cond (op0, &true0, &false0);
9131 cond1 = if_then_else_cond (op1, &true1, &false1);
9133 if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9134 && (REG_P (op0) || REG_P (op1)))
9136 /* Try to enable a simplification by undoing work done by
9137 if_then_else_cond if it converted a REG into something more
9138 complex. */
9139 if (REG_P (op0))
9141 cond0 = 0;
9142 true0 = false0 = op0;
9144 else
9146 cond1 = 0;
9147 true1 = false1 = op1;
9151 if ((cond0 != 0 || cond1 != 0)
9152 && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9154 /* If if_then_else_cond returned zero, then true/false are the
9155 same rtl. We must copy one of them to prevent invalid rtl
9156 sharing. */
9157 if (cond0 == 0)
9158 true0 = copy_rtx (true0);
9159 else if (cond1 == 0)
9160 true1 = copy_rtx (true1);
9162 if (COMPARISON_P (x))
9164 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9165 true0, true1);
9166 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9167 false0, false1);
9169 else
9171 *ptrue = simplify_gen_binary (code, mode, true0, true1);
9172 *pfalse = simplify_gen_binary (code, mode, false0, false1);
9175 return cond0 ? cond0 : cond1;
9178 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9179 operands is zero when the other is nonzero, and vice-versa,
9180 and STORE_FLAG_VALUE is 1 or -1. */
9182 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9183 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9184 || code == UMAX)
9185 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9187 rtx op0 = XEXP (XEXP (x, 0), 1);
9188 rtx op1 = XEXP (XEXP (x, 1), 1);
9190 cond0 = XEXP (XEXP (x, 0), 0);
9191 cond1 = XEXP (XEXP (x, 1), 0);
9193 if (COMPARISON_P (cond0)
9194 && COMPARISON_P (cond1)
9195 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9196 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9197 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9198 || ((swap_condition (GET_CODE (cond0))
9199 == reversed_comparison_code (cond1, NULL))
9200 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9201 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9202 && ! side_effects_p (x))
9204 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9205 *pfalse = simplify_gen_binary (MULT, mode,
9206 (code == MINUS
9207 ? simplify_gen_unary (NEG, mode,
9208 op1, mode)
9209 : op1),
9210 const_true_rtx);
9211 return cond0;
9215 /* Similarly for MULT, AND and UMIN, except that for these the result
9216 is always zero. */
9217 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9218 && (code == MULT || code == AND || code == UMIN)
9219 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9221 cond0 = XEXP (XEXP (x, 0), 0);
9222 cond1 = XEXP (XEXP (x, 1), 0);
9224 if (COMPARISON_P (cond0)
9225 && COMPARISON_P (cond1)
9226 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9227 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9228 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9229 || ((swap_condition (GET_CODE (cond0))
9230 == reversed_comparison_code (cond1, NULL))
9231 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9232 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9233 && ! side_effects_p (x))
9235 *ptrue = *pfalse = const0_rtx;
9236 return cond0;
9241 else if (code == IF_THEN_ELSE)
9243 /* If we have IF_THEN_ELSE already, extract the condition and
9244 canonicalize it if it is NE or EQ. */
9245 cond0 = XEXP (x, 0);
9246 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9247 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9248 return XEXP (cond0, 0);
9249 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9251 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9252 return XEXP (cond0, 0);
9254 else
9255 return cond0;
9258 /* If X is a SUBREG, we can narrow both the true and false values
9259 if the inner expression, if there is a condition. */
9260 else if (code == SUBREG
9261 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
9262 &true0, &false0)))
9264 true0 = simplify_gen_subreg (mode, true0,
9265 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9266 false0 = simplify_gen_subreg (mode, false0,
9267 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9268 if (true0 && false0)
9270 *ptrue = true0;
9271 *pfalse = false0;
9272 return cond0;
9276 /* If X is a constant, this isn't special and will cause confusions
9277 if we treat it as such. Likewise if it is equivalent to a constant. */
9278 else if (CONSTANT_P (x)
9279 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9282 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9283 will be least confusing to the rest of the compiler. */
9284 else if (mode == BImode)
9286 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9287 return x;
9290 /* If X is known to be either 0 or -1, those are the true and
9291 false values when testing X. */
9292 else if (x == constm1_rtx || x == const0_rtx
9293 || (is_a <scalar_int_mode> (mode, &int_mode)
9294 && (num_sign_bit_copies (x, int_mode)
9295 == GET_MODE_PRECISION (int_mode))))
9297 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9298 return x;
9301 /* Likewise for 0 or a single bit. */
9302 else if (HWI_COMPUTABLE_MODE_P (mode)
9303 && pow2p_hwi (nz = nonzero_bits (x, mode)))
9305 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9306 return x;
9309 /* Otherwise fail; show no condition with true and false values the same. */
9310 *ptrue = *pfalse = x;
9311 return 0;
9314 /* Return the value of expression X given the fact that condition COND
9315 is known to be true when applied to REG as its first operand and VAL
9316 as its second. X is known to not be shared and so can be modified in
9317 place.
9319 We only handle the simplest cases, and specifically those cases that
9320 arise with IF_THEN_ELSE expressions. */
9322 static rtx
9323 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9325 enum rtx_code code = GET_CODE (x);
9326 const char *fmt;
9327 int i, j;
9329 if (side_effects_p (x))
9330 return x;
9332 /* If either operand of the condition is a floating point value,
9333 then we have to avoid collapsing an EQ comparison. */
9334 if (cond == EQ
9335 && rtx_equal_p (x, reg)
9336 && ! FLOAT_MODE_P (GET_MODE (x))
9337 && ! FLOAT_MODE_P (GET_MODE (val)))
9338 return val;
9340 if (cond == UNEQ && rtx_equal_p (x, reg))
9341 return val;
9343 /* If X is (abs REG) and we know something about REG's relationship
9344 with zero, we may be able to simplify this. */
9346 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9347 switch (cond)
9349 case GE: case GT: case EQ:
9350 return XEXP (x, 0);
9351 case LT: case LE:
9352 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9353 XEXP (x, 0),
9354 GET_MODE (XEXP (x, 0)));
9355 default:
9356 break;
9359 /* The only other cases we handle are MIN, MAX, and comparisons if the
9360 operands are the same as REG and VAL. */
9362 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9364 if (rtx_equal_p (XEXP (x, 0), val))
9366 std::swap (val, reg);
9367 cond = swap_condition (cond);
9370 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9372 if (COMPARISON_P (x))
9374 if (comparison_dominates_p (cond, code))
9375 return const_true_rtx;
9377 code = reversed_comparison_code (x, NULL);
9378 if (code != UNKNOWN
9379 && comparison_dominates_p (cond, code))
9380 return const0_rtx;
9381 else
9382 return x;
9384 else if (code == SMAX || code == SMIN
9385 || code == UMIN || code == UMAX)
9387 int unsignedp = (code == UMIN || code == UMAX);
9389 /* Do not reverse the condition when it is NE or EQ.
9390 This is because we cannot conclude anything about
9391 the value of 'SMAX (x, y)' when x is not equal to y,
9392 but we can when x equals y. */
9393 if ((code == SMAX || code == UMAX)
9394 && ! (cond == EQ || cond == NE))
9395 cond = reverse_condition (cond);
9397 switch (cond)
9399 case GE: case GT:
9400 return unsignedp ? x : XEXP (x, 1);
9401 case LE: case LT:
9402 return unsignedp ? x : XEXP (x, 0);
9403 case GEU: case GTU:
9404 return unsignedp ? XEXP (x, 1) : x;
9405 case LEU: case LTU:
9406 return unsignedp ? XEXP (x, 0) : x;
9407 default:
9408 break;
9413 else if (code == SUBREG)
9415 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9416 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9418 if (SUBREG_REG (x) != r)
9420 /* We must simplify subreg here, before we lose track of the
9421 original inner_mode. */
9422 new_rtx = simplify_subreg (GET_MODE (x), r,
9423 inner_mode, SUBREG_BYTE (x));
9424 if (new_rtx)
9425 return new_rtx;
9426 else
9427 SUBST (SUBREG_REG (x), r);
9430 return x;
9432 /* We don't have to handle SIGN_EXTEND here, because even in the
9433 case of replacing something with a modeless CONST_INT, a
9434 CONST_INT is already (supposed to be) a valid sign extension for
9435 its narrower mode, which implies it's already properly
9436 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9437 story is different. */
9438 else if (code == ZERO_EXTEND)
9440 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9441 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9443 if (XEXP (x, 0) != r)
9445 /* We must simplify the zero_extend here, before we lose
9446 track of the original inner_mode. */
9447 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9448 r, inner_mode);
9449 if (new_rtx)
9450 return new_rtx;
9451 else
9452 SUBST (XEXP (x, 0), r);
9455 return x;
9458 fmt = GET_RTX_FORMAT (code);
9459 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9461 if (fmt[i] == 'e')
9462 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9463 else if (fmt[i] == 'E')
9464 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9465 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9466 cond, reg, val));
9469 return x;
9472 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9473 assignment as a field assignment. */
9475 static int
9476 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9478 if (widen_x && GET_MODE (x) != GET_MODE (y))
9480 if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9481 return 0;
9482 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9483 return 0;
9484 /* For big endian, adjust the memory offset. */
9485 if (BYTES_BIG_ENDIAN)
9486 x = adjust_address_nv (x, GET_MODE (y),
9487 -subreg_lowpart_offset (GET_MODE (x),
9488 GET_MODE (y)));
9489 else
9490 x = adjust_address_nv (x, GET_MODE (y), 0);
9493 if (x == y || rtx_equal_p (x, y))
9494 return 1;
9496 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9497 return 0;
9499 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9500 Note that all SUBREGs of MEM are paradoxical; otherwise they
9501 would have been rewritten. */
9502 if (MEM_P (x) && GET_CODE (y) == SUBREG
9503 && MEM_P (SUBREG_REG (y))
9504 && rtx_equal_p (SUBREG_REG (y),
9505 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9506 return 1;
9508 if (MEM_P (y) && GET_CODE (x) == SUBREG
9509 && MEM_P (SUBREG_REG (x))
9510 && rtx_equal_p (SUBREG_REG (x),
9511 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9512 return 1;
9514 /* We used to see if get_last_value of X and Y were the same but that's
9515 not correct. In one direction, we'll cause the assignment to have
9516 the wrong destination and in the case, we'll import a register into this
9517 insn that might have already have been dead. So fail if none of the
9518 above cases are true. */
9519 return 0;
9522 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9523 Return that assignment if so.
9525 We only handle the most common cases. */
9527 static rtx
9528 make_field_assignment (rtx x)
9530 rtx dest = SET_DEST (x);
9531 rtx src = SET_SRC (x);
9532 rtx assign;
9533 rtx rhs, lhs;
9534 HOST_WIDE_INT c1;
9535 HOST_WIDE_INT pos;
9536 unsigned HOST_WIDE_INT len;
9537 rtx other;
9539 /* All the rules in this function are specific to scalar integers. */
9540 scalar_int_mode mode;
9541 if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9542 return x;
9544 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9545 a clear of a one-bit field. We will have changed it to
9546 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9547 for a SUBREG. */
9549 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9550 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9551 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9552 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9554 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9555 1, 1, 1, 0);
9556 if (assign != 0)
9557 return gen_rtx_SET (assign, const0_rtx);
9558 return x;
9561 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9562 && subreg_lowpart_p (XEXP (src, 0))
9563 && partial_subreg_p (XEXP (src, 0))
9564 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9565 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9566 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9567 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9569 assign = make_extraction (VOIDmode, dest, 0,
9570 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9571 1, 1, 1, 0);
9572 if (assign != 0)
9573 return gen_rtx_SET (assign, const0_rtx);
9574 return x;
9577 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9578 one-bit field. */
9579 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9580 && XEXP (XEXP (src, 0), 0) == const1_rtx
9581 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9583 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9584 1, 1, 1, 0);
9585 if (assign != 0)
9586 return gen_rtx_SET (assign, const1_rtx);
9587 return x;
9590 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9591 SRC is an AND with all bits of that field set, then we can discard
9592 the AND. */
9593 if (GET_CODE (dest) == ZERO_EXTRACT
9594 && CONST_INT_P (XEXP (dest, 1))
9595 && GET_CODE (src) == AND
9596 && CONST_INT_P (XEXP (src, 1)))
9598 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9599 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9600 unsigned HOST_WIDE_INT ze_mask;
9602 if (width >= HOST_BITS_PER_WIDE_INT)
9603 ze_mask = -1;
9604 else
9605 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9607 /* Complete overlap. We can remove the source AND. */
9608 if ((and_mask & ze_mask) == ze_mask)
9609 return gen_rtx_SET (dest, XEXP (src, 0));
9611 /* Partial overlap. We can reduce the source AND. */
9612 if ((and_mask & ze_mask) != and_mask)
9614 src = gen_rtx_AND (mode, XEXP (src, 0),
9615 gen_int_mode (and_mask & ze_mask, mode));
9616 return gen_rtx_SET (dest, src);
9620 /* The other case we handle is assignments into a constant-position
9621 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9622 a mask that has all one bits except for a group of zero bits and
9623 OTHER is known to have zeros where C1 has ones, this is such an
9624 assignment. Compute the position and length from C1. Shift OTHER
9625 to the appropriate position, force it to the required mode, and
9626 make the extraction. Check for the AND in both operands. */
9628 /* One or more SUBREGs might obscure the constant-position field
9629 assignment. The first one we are likely to encounter is an outer
9630 narrowing SUBREG, which we can just strip for the purposes of
9631 identifying the constant-field assignment. */
9632 scalar_int_mode src_mode = mode;
9633 if (GET_CODE (src) == SUBREG
9634 && subreg_lowpart_p (src)
9635 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9636 src = SUBREG_REG (src);
9638 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9639 return x;
9641 rhs = expand_compound_operation (XEXP (src, 0));
9642 lhs = expand_compound_operation (XEXP (src, 1));
9644 if (GET_CODE (rhs) == AND
9645 && CONST_INT_P (XEXP (rhs, 1))
9646 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9647 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9648 /* The second SUBREG that might get in the way is a paradoxical
9649 SUBREG around the first operand of the AND. We want to
9650 pretend the operand is as wide as the destination here. We
9651 do this by adjusting the MEM to wider mode for the sole
9652 purpose of the call to rtx_equal_for_field_assignment_p. Also
9653 note this trick only works for MEMs. */
9654 else if (GET_CODE (rhs) == AND
9655 && paradoxical_subreg_p (XEXP (rhs, 0))
9656 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9657 && CONST_INT_P (XEXP (rhs, 1))
9658 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9659 dest, true))
9660 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9661 else if (GET_CODE (lhs) == AND
9662 && CONST_INT_P (XEXP (lhs, 1))
9663 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9664 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9665 /* The second SUBREG that might get in the way is a paradoxical
9666 SUBREG around the first operand of the AND. We want to
9667 pretend the operand is as wide as the destination here. We
9668 do this by adjusting the MEM to wider mode for the sole
9669 purpose of the call to rtx_equal_for_field_assignment_p. Also
9670 note this trick only works for MEMs. */
9671 else if (GET_CODE (lhs) == AND
9672 && paradoxical_subreg_p (XEXP (lhs, 0))
9673 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9674 && CONST_INT_P (XEXP (lhs, 1))
9675 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9676 dest, true))
9677 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9678 else
9679 return x;
9681 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9682 if (pos < 0
9683 || pos + len > GET_MODE_PRECISION (mode)
9684 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9685 || (c1 & nonzero_bits (other, mode)) != 0)
9686 return x;
9688 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9689 if (assign == 0)
9690 return x;
9692 /* The mode to use for the source is the mode of the assignment, or of
9693 what is inside a possible STRICT_LOW_PART. */
9694 machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9695 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9697 /* Shift OTHER right POS places and make it the source, restricting it
9698 to the proper length and mode. */
9700 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9701 src_mode, other, pos),
9702 dest);
9703 src = force_to_mode (src, new_mode,
9704 len >= HOST_BITS_PER_WIDE_INT
9705 ? HOST_WIDE_INT_M1U
9706 : (HOST_WIDE_INT_1U << len) - 1,
9709 /* If SRC is masked by an AND that does not make a difference in
9710 the value being stored, strip it. */
9711 if (GET_CODE (assign) == ZERO_EXTRACT
9712 && CONST_INT_P (XEXP (assign, 1))
9713 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9714 && GET_CODE (src) == AND
9715 && CONST_INT_P (XEXP (src, 1))
9716 && UINTVAL (XEXP (src, 1))
9717 == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9718 src = XEXP (src, 0);
9720 return gen_rtx_SET (assign, src);
9723 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9724 if so. */
9726 static rtx
9727 apply_distributive_law (rtx x)
9729 enum rtx_code code = GET_CODE (x);
9730 enum rtx_code inner_code;
9731 rtx lhs, rhs, other;
9732 rtx tem;
9734 /* Distributivity is not true for floating point as it can change the
9735 value. So we don't do it unless -funsafe-math-optimizations. */
9736 if (FLOAT_MODE_P (GET_MODE (x))
9737 && ! flag_unsafe_math_optimizations)
9738 return x;
9740 /* The outer operation can only be one of the following: */
9741 if (code != IOR && code != AND && code != XOR
9742 && code != PLUS && code != MINUS)
9743 return x;
9745 lhs = XEXP (x, 0);
9746 rhs = XEXP (x, 1);
9748 /* If either operand is a primitive we can't do anything, so get out
9749 fast. */
9750 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9751 return x;
9753 lhs = expand_compound_operation (lhs);
9754 rhs = expand_compound_operation (rhs);
9755 inner_code = GET_CODE (lhs);
9756 if (inner_code != GET_CODE (rhs))
9757 return x;
9759 /* See if the inner and outer operations distribute. */
9760 switch (inner_code)
9762 case LSHIFTRT:
9763 case ASHIFTRT:
9764 case AND:
9765 case IOR:
9766 /* These all distribute except over PLUS. */
9767 if (code == PLUS || code == MINUS)
9768 return x;
9769 break;
9771 case MULT:
9772 if (code != PLUS && code != MINUS)
9773 return x;
9774 break;
9776 case ASHIFT:
9777 /* This is also a multiply, so it distributes over everything. */
9778 break;
9780 /* This used to handle SUBREG, but this turned out to be counter-
9781 productive, since (subreg (op ...)) usually is not handled by
9782 insn patterns, and this "optimization" therefore transformed
9783 recognizable patterns into unrecognizable ones. Therefore the
9784 SUBREG case was removed from here.
9786 It is possible that distributing SUBREG over arithmetic operations
9787 leads to an intermediate result than can then be optimized further,
9788 e.g. by moving the outer SUBREG to the other side of a SET as done
9789 in simplify_set. This seems to have been the original intent of
9790 handling SUBREGs here.
9792 However, with current GCC this does not appear to actually happen,
9793 at least on major platforms. If some case is found where removing
9794 the SUBREG case here prevents follow-on optimizations, distributing
9795 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9797 default:
9798 return x;
9801 /* Set LHS and RHS to the inner operands (A and B in the example
9802 above) and set OTHER to the common operand (C in the example).
9803 There is only one way to do this unless the inner operation is
9804 commutative. */
9805 if (COMMUTATIVE_ARITH_P (lhs)
9806 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9807 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9808 else if (COMMUTATIVE_ARITH_P (lhs)
9809 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9810 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9811 else if (COMMUTATIVE_ARITH_P (lhs)
9812 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9813 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9814 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9815 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9816 else
9817 return x;
9819 /* Form the new inner operation, seeing if it simplifies first. */
9820 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9822 /* There is one exception to the general way of distributing:
9823 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9824 if (code == XOR && inner_code == IOR)
9826 inner_code = AND;
9827 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9830 /* We may be able to continuing distributing the result, so call
9831 ourselves recursively on the inner operation before forming the
9832 outer operation, which we return. */
9833 return simplify_gen_binary (inner_code, GET_MODE (x),
9834 apply_distributive_law (tem), other);
9837 /* See if X is of the form (* (+ A B) C), and if so convert to
9838 (+ (* A C) (* B C)) and try to simplify.
9840 Most of the time, this results in no change. However, if some of
9841 the operands are the same or inverses of each other, simplifications
9842 will result.
9844 For example, (and (ior A B) (not B)) can occur as the result of
9845 expanding a bit field assignment. When we apply the distributive
9846 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9847 which then simplifies to (and (A (not B))).
9849 Note that no checks happen on the validity of applying the inverse
9850 distributive law. This is pointless since we can do it in the
9851 few places where this routine is called.
9853 N is the index of the term that is decomposed (the arithmetic operation,
9854 i.e. (+ A B) in the first example above). !N is the index of the term that
9855 is distributed, i.e. of C in the first example above. */
9856 static rtx
9857 distribute_and_simplify_rtx (rtx x, int n)
9859 machine_mode mode;
9860 enum rtx_code outer_code, inner_code;
9861 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9863 /* Distributivity is not true for floating point as it can change the
9864 value. So we don't do it unless -funsafe-math-optimizations. */
9865 if (FLOAT_MODE_P (GET_MODE (x))
9866 && ! flag_unsafe_math_optimizations)
9867 return NULL_RTX;
9869 decomposed = XEXP (x, n);
9870 if (!ARITHMETIC_P (decomposed))
9871 return NULL_RTX;
9873 mode = GET_MODE (x);
9874 outer_code = GET_CODE (x);
9875 distributed = XEXP (x, !n);
9877 inner_code = GET_CODE (decomposed);
9878 inner_op0 = XEXP (decomposed, 0);
9879 inner_op1 = XEXP (decomposed, 1);
9881 /* Special case (and (xor B C) (not A)), which is equivalent to
9882 (xor (ior A B) (ior A C)) */
9883 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9885 distributed = XEXP (distributed, 0);
9886 outer_code = IOR;
9889 if (n == 0)
9891 /* Distribute the second term. */
9892 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9893 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9895 else
9897 /* Distribute the first term. */
9898 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9899 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9902 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9903 new_op0, new_op1));
9904 if (GET_CODE (tmp) != outer_code
9905 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
9906 < set_src_cost (x, mode, optimize_this_for_speed_p)))
9907 return tmp;
9909 return NULL_RTX;
9912 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9913 in MODE. Return an equivalent form, if different from (and VAROP
9914 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9916 static rtx
9917 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
9918 unsigned HOST_WIDE_INT constop)
9920 unsigned HOST_WIDE_INT nonzero;
9921 unsigned HOST_WIDE_INT orig_constop;
9922 rtx orig_varop;
9923 int i;
9925 orig_varop = varop;
9926 orig_constop = constop;
9927 if (GET_CODE (varop) == CLOBBER)
9928 return NULL_RTX;
9930 /* Simplify VAROP knowing that we will be only looking at some of the
9931 bits in it.
9933 Note by passing in CONSTOP, we guarantee that the bits not set in
9934 CONSTOP are not significant and will never be examined. We must
9935 ensure that is the case by explicitly masking out those bits
9936 before returning. */
9937 varop = force_to_mode (varop, mode, constop, 0);
9939 /* If VAROP is a CLOBBER, we will fail so return it. */
9940 if (GET_CODE (varop) == CLOBBER)
9941 return varop;
9943 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9944 to VAROP and return the new constant. */
9945 if (CONST_INT_P (varop))
9946 return gen_int_mode (INTVAL (varop) & constop, mode);
9948 /* See what bits may be nonzero in VAROP. Unlike the general case of
9949 a call to nonzero_bits, here we don't care about bits outside
9950 MODE. */
9952 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9954 /* Turn off all bits in the constant that are known to already be zero.
9955 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9956 which is tested below. */
9958 constop &= nonzero;
9960 /* If we don't have any bits left, return zero. */
9961 if (constop == 0)
9962 return const0_rtx;
9964 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9965 a power of two, we can replace this with an ASHIFT. */
9966 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9967 && (i = exact_log2 (constop)) >= 0)
9968 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9970 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9971 or XOR, then try to apply the distributive law. This may eliminate
9972 operations if either branch can be simplified because of the AND.
9973 It may also make some cases more complex, but those cases probably
9974 won't match a pattern either with or without this. */
9976 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9978 scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
9979 return
9980 gen_lowpart
9981 (mode,
9982 apply_distributive_law
9983 (simplify_gen_binary (GET_CODE (varop), varop_mode,
9984 simplify_and_const_int (NULL_RTX, varop_mode,
9985 XEXP (varop, 0),
9986 constop),
9987 simplify_and_const_int (NULL_RTX, varop_mode,
9988 XEXP (varop, 1),
9989 constop))));
9992 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9993 the AND and see if one of the operands simplifies to zero. If so, we
9994 may eliminate it. */
9996 if (GET_CODE (varop) == PLUS
9997 && pow2p_hwi (constop + 1))
9999 rtx o0, o1;
10001 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10002 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10003 if (o0 == const0_rtx)
10004 return o1;
10005 if (o1 == const0_rtx)
10006 return o0;
10009 /* Make a SUBREG if necessary. If we can't make it, fail. */
10010 varop = gen_lowpart (mode, varop);
10011 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10012 return NULL_RTX;
10014 /* If we are only masking insignificant bits, return VAROP. */
10015 if (constop == nonzero)
10016 return varop;
10018 if (varop == orig_varop && constop == orig_constop)
10019 return NULL_RTX;
10021 /* Otherwise, return an AND. */
10022 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10026 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10027 in MODE.
10029 Return an equivalent form, if different from X. Otherwise, return X. If
10030 X is zero, we are to always construct the equivalent form. */
10032 static rtx
10033 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10034 unsigned HOST_WIDE_INT constop)
10036 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10037 if (tem)
10038 return tem;
10040 if (!x)
10041 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10042 gen_int_mode (constop, mode));
10043 if (GET_MODE (x) != mode)
10044 x = gen_lowpart (mode, x);
10045 return x;
10048 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10049 We don't care about bits outside of those defined in MODE.
10051 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10052 a shift, AND, or zero_extract, we can do better. */
10054 static rtx
10055 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10056 scalar_int_mode mode,
10057 unsigned HOST_WIDE_INT *nonzero)
10059 rtx tem;
10060 reg_stat_type *rsp;
10062 /* If X is a register whose nonzero bits value is current, use it.
10063 Otherwise, if X is a register whose value we can find, use that
10064 value. Otherwise, use the previously-computed global nonzero bits
10065 for this register. */
10067 rsp = &reg_stat[REGNO (x)];
10068 if (rsp->last_set_value != 0
10069 && (rsp->last_set_mode == mode
10070 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10071 && GET_MODE_CLASS (mode) == MODE_INT))
10072 && ((rsp->last_set_label >= label_tick_ebb_start
10073 && rsp->last_set_label < label_tick)
10074 || (rsp->last_set_label == label_tick
10075 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10076 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10077 && REGNO (x) < reg_n_sets_max
10078 && REG_N_SETS (REGNO (x)) == 1
10079 && !REGNO_REG_SET_P
10080 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10081 REGNO (x)))))
10083 /* Note that, even if the precision of last_set_mode is lower than that
10084 of mode, record_value_for_reg invoked nonzero_bits on the register
10085 with nonzero_bits_mode (because last_set_mode is necessarily integral
10086 and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10087 are all valid, hence in mode too since nonzero_bits_mode is defined
10088 to the largest HWI_COMPUTABLE_MODE_P mode. */
10089 *nonzero &= rsp->last_set_nonzero_bits;
10090 return NULL;
10093 tem = get_last_value (x);
10094 if (tem)
10096 if (SHORT_IMMEDIATES_SIGN_EXTEND)
10097 tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10099 return tem;
10102 if (nonzero_sign_valid && rsp->nonzero_bits)
10104 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10106 if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10107 /* We don't know anything about the upper bits. */
10108 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10110 *nonzero &= mask;
10113 return NULL;
10116 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10117 end of X that are known to be equal to the sign bit. X will be used
10118 in mode MODE; the returned value will always be between 1 and the
10119 number of bits in MODE. */
10121 static rtx
10122 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10123 scalar_int_mode mode,
10124 unsigned int *result)
10126 rtx tem;
10127 reg_stat_type *rsp;
10129 rsp = &reg_stat[REGNO (x)];
10130 if (rsp->last_set_value != 0
10131 && rsp->last_set_mode == mode
10132 && ((rsp->last_set_label >= label_tick_ebb_start
10133 && rsp->last_set_label < label_tick)
10134 || (rsp->last_set_label == label_tick
10135 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10136 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10137 && REGNO (x) < reg_n_sets_max
10138 && REG_N_SETS (REGNO (x)) == 1
10139 && !REGNO_REG_SET_P
10140 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10141 REGNO (x)))))
10143 *result = rsp->last_set_sign_bit_copies;
10144 return NULL;
10147 tem = get_last_value (x);
10148 if (tem != 0)
10149 return tem;
10151 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10152 && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10153 *result = rsp->sign_bit_copies;
10155 return NULL;
10158 /* Return the number of "extended" bits there are in X, when interpreted
10159 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
10160 unsigned quantities, this is the number of high-order zero bits.
10161 For signed quantities, this is the number of copies of the sign bit
10162 minus 1. In both case, this function returns the number of "spare"
10163 bits. For example, if two quantities for which this function returns
10164 at least 1 are added, the addition is known not to overflow.
10166 This function will always return 0 unless called during combine, which
10167 implies that it must be called from a define_split. */
10169 unsigned int
10170 extended_count (const_rtx x, machine_mode mode, int unsignedp)
10172 if (nonzero_sign_valid == 0)
10173 return 0;
10175 scalar_int_mode int_mode;
10176 return (unsignedp
10177 ? (is_a <scalar_int_mode> (mode, &int_mode)
10178 && HWI_COMPUTABLE_MODE_P (int_mode)
10179 ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10180 - floor_log2 (nonzero_bits (x, int_mode)))
10181 : 0)
10182 : num_sign_bit_copies (x, mode) - 1);
10185 /* This function is called from `simplify_shift_const' to merge two
10186 outer operations. Specifically, we have already found that we need
10187 to perform operation *POP0 with constant *PCONST0 at the outermost
10188 position. We would now like to also perform OP1 with constant CONST1
10189 (with *POP0 being done last).
10191 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
10192 the resulting operation. *PCOMP_P is set to 1 if we would need to
10193 complement the innermost operand, otherwise it is unchanged.
10195 MODE is the mode in which the operation will be done. No bits outside
10196 the width of this mode matter. It is assumed that the width of this mode
10197 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10199 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
10200 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
10201 result is simply *PCONST0.
10203 If the resulting operation cannot be expressed as one operation, we
10204 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
10206 static int
10207 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)
10209 enum rtx_code op0 = *pop0;
10210 HOST_WIDE_INT const0 = *pconst0;
10212 const0 &= GET_MODE_MASK (mode);
10213 const1 &= GET_MODE_MASK (mode);
10215 /* If OP0 is an AND, clear unimportant bits in CONST1. */
10216 if (op0 == AND)
10217 const1 &= const0;
10219 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
10220 if OP0 is SET. */
10222 if (op1 == UNKNOWN || op0 == SET)
10223 return 1;
10225 else if (op0 == UNKNOWN)
10226 op0 = op1, const0 = const1;
10228 else if (op0 == op1)
10230 switch (op0)
10232 case AND:
10233 const0 &= const1;
10234 break;
10235 case IOR:
10236 const0 |= const1;
10237 break;
10238 case XOR:
10239 const0 ^= const1;
10240 break;
10241 case PLUS:
10242 const0 += const1;
10243 break;
10244 case NEG:
10245 op0 = UNKNOWN;
10246 break;
10247 default:
10248 break;
10252 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
10253 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10254 return 0;
10256 /* If the two constants aren't the same, we can't do anything. The
10257 remaining six cases can all be done. */
10258 else if (const0 != const1)
10259 return 0;
10261 else
10262 switch (op0)
10264 case IOR:
10265 if (op1 == AND)
10266 /* (a & b) | b == b */
10267 op0 = SET;
10268 else /* op1 == XOR */
10269 /* (a ^ b) | b == a | b */
10271 break;
10273 case XOR:
10274 if (op1 == AND)
10275 /* (a & b) ^ b == (~a) & b */
10276 op0 = AND, *pcomp_p = 1;
10277 else /* op1 == IOR */
10278 /* (a | b) ^ b == a & ~b */
10279 op0 = AND, const0 = ~const0;
10280 break;
10282 case AND:
10283 if (op1 == IOR)
10284 /* (a | b) & b == b */
10285 op0 = SET;
10286 else /* op1 == XOR */
10287 /* (a ^ b) & b) == (~a) & b */
10288 *pcomp_p = 1;
10289 break;
10290 default:
10291 break;
10294 /* Check for NO-OP cases. */
10295 const0 &= GET_MODE_MASK (mode);
10296 if (const0 == 0
10297 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10298 op0 = UNKNOWN;
10299 else if (const0 == 0 && op0 == AND)
10300 op0 = SET;
10301 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10302 && op0 == AND)
10303 op0 = UNKNOWN;
10305 *pop0 = op0;
10307 /* ??? Slightly redundant with the above mask, but not entirely.
10308 Moving this above means we'd have to sign-extend the mode mask
10309 for the final test. */
10310 if (op0 != UNKNOWN && op0 != NEG)
10311 *pconst0 = trunc_int_for_mode (const0, mode);
10313 return 1;
10316 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10317 the shift in. The original shift operation CODE is performed on OP in
10318 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10319 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10320 result of the shift is subject to operation OUTER_CODE with operand
10321 OUTER_CONST. */
10323 static scalar_int_mode
10324 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10325 scalar_int_mode orig_mode, scalar_int_mode mode,
10326 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10328 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10330 /* In general we can't perform in wider mode for right shift and rotate. */
10331 switch (code)
10333 case ASHIFTRT:
10334 /* We can still widen if the bits brought in from the left are identical
10335 to the sign bit of ORIG_MODE. */
10336 if (num_sign_bit_copies (op, mode)
10337 > (unsigned) (GET_MODE_PRECISION (mode)
10338 - GET_MODE_PRECISION (orig_mode)))
10339 return mode;
10340 return orig_mode;
10342 case LSHIFTRT:
10343 /* Similarly here but with zero bits. */
10344 if (HWI_COMPUTABLE_MODE_P (mode)
10345 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10346 return mode;
10348 /* We can also widen if the bits brought in will be masked off. This
10349 operation is performed in ORIG_MODE. */
10350 if (outer_code == AND)
10352 int care_bits = low_bitmask_len (orig_mode, outer_const);
10354 if (care_bits >= 0
10355 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10356 return mode;
10358 /* fall through */
10360 case ROTATE:
10361 return orig_mode;
10363 case ROTATERT:
10364 gcc_unreachable ();
10366 default:
10367 return mode;
10371 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10372 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10373 if we cannot simplify it. Otherwise, return a simplified value.
10375 The shift is normally computed in the widest mode we find in VAROP, as
10376 long as it isn't a different number of words than RESULT_MODE. Exceptions
10377 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10379 static rtx
10380 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10381 rtx varop, int orig_count)
10383 enum rtx_code orig_code = code;
10384 rtx orig_varop = varop;
10385 int count;
10386 machine_mode mode = result_mode;
10387 machine_mode shift_mode;
10388 scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10389 unsigned int mode_words
10390 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10391 /* We form (outer_op (code varop count) (outer_const)). */
10392 enum rtx_code outer_op = UNKNOWN;
10393 HOST_WIDE_INT outer_const = 0;
10394 int complement_p = 0;
10395 rtx new_rtx, x;
10397 /* Make sure and truncate the "natural" shift on the way in. We don't
10398 want to do this inside the loop as it makes it more difficult to
10399 combine shifts. */
10400 if (SHIFT_COUNT_TRUNCATED)
10401 orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10403 /* If we were given an invalid count, don't do anything except exactly
10404 what was requested. */
10406 if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10407 return NULL_RTX;
10409 count = orig_count;
10411 /* Unless one of the branches of the `if' in this loop does a `continue',
10412 we will `break' the loop after the `if'. */
10414 while (count != 0)
10416 /* If we have an operand of (clobber (const_int 0)), fail. */
10417 if (GET_CODE (varop) == CLOBBER)
10418 return NULL_RTX;
10420 /* Convert ROTATERT to ROTATE. */
10421 if (code == ROTATERT)
10423 unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10424 code = ROTATE;
10425 count = bitsize - count;
10428 shift_mode = result_mode;
10429 if (shift_mode != mode)
10431 /* We only change the modes of scalar shifts. */
10432 int_mode = as_a <scalar_int_mode> (mode);
10433 int_result_mode = as_a <scalar_int_mode> (result_mode);
10434 shift_mode = try_widen_shift_mode (code, varop, count,
10435 int_result_mode, int_mode,
10436 outer_op, outer_const);
10439 scalar_int_mode shift_unit_mode
10440 = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10442 /* Handle cases where the count is greater than the size of the mode
10443 minus 1. For ASHIFT, use the size minus one as the count (this can
10444 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10445 take the count modulo the size. For other shifts, the result is
10446 zero.
10448 Since these shifts are being produced by the compiler by combining
10449 multiple operations, each of which are defined, we know what the
10450 result is supposed to be. */
10452 if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10454 if (code == ASHIFTRT)
10455 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10456 else if (code == ROTATE || code == ROTATERT)
10457 count %= GET_MODE_PRECISION (shift_unit_mode);
10458 else
10460 /* We can't simply return zero because there may be an
10461 outer op. */
10462 varop = const0_rtx;
10463 count = 0;
10464 break;
10468 /* If we discovered we had to complement VAROP, leave. Making a NOT
10469 here would cause an infinite loop. */
10470 if (complement_p)
10471 break;
10473 if (shift_mode == shift_unit_mode)
10475 /* An arithmetic right shift of a quantity known to be -1 or 0
10476 is a no-op. */
10477 if (code == ASHIFTRT
10478 && (num_sign_bit_copies (varop, shift_unit_mode)
10479 == GET_MODE_PRECISION (shift_unit_mode)))
10481 count = 0;
10482 break;
10485 /* If we are doing an arithmetic right shift and discarding all but
10486 the sign bit copies, this is equivalent to doing a shift by the
10487 bitsize minus one. Convert it into that shift because it will
10488 often allow other simplifications. */
10490 if (code == ASHIFTRT
10491 && (count + num_sign_bit_copies (varop, shift_unit_mode)
10492 >= GET_MODE_PRECISION (shift_unit_mode)))
10493 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10495 /* We simplify the tests below and elsewhere by converting
10496 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10497 `make_compound_operation' will convert it to an ASHIFTRT for
10498 those machines (such as VAX) that don't have an LSHIFTRT. */
10499 if (code == ASHIFTRT
10500 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10501 && val_signbit_known_clear_p (shift_unit_mode,
10502 nonzero_bits (varop,
10503 shift_unit_mode)))
10504 code = LSHIFTRT;
10506 if (((code == LSHIFTRT
10507 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10508 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10509 || (code == ASHIFT
10510 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10511 && !((nonzero_bits (varop, shift_unit_mode) << count)
10512 & GET_MODE_MASK (shift_unit_mode))))
10513 && !side_effects_p (varop))
10514 varop = const0_rtx;
10517 switch (GET_CODE (varop))
10519 case SIGN_EXTEND:
10520 case ZERO_EXTEND:
10521 case SIGN_EXTRACT:
10522 case ZERO_EXTRACT:
10523 new_rtx = expand_compound_operation (varop);
10524 if (new_rtx != varop)
10526 varop = new_rtx;
10527 continue;
10529 break;
10531 case MEM:
10532 /* The following rules apply only to scalars. */
10533 if (shift_mode != shift_unit_mode)
10534 break;
10535 int_mode = as_a <scalar_int_mode> (mode);
10537 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10538 minus the width of a smaller mode, we can do this with a
10539 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10540 if ((code == ASHIFTRT || code == LSHIFTRT)
10541 && ! mode_dependent_address_p (XEXP (varop, 0),
10542 MEM_ADDR_SPACE (varop))
10543 && ! MEM_VOLATILE_P (varop)
10544 && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10545 .exists (&tmode)))
10547 new_rtx = adjust_address_nv (varop, tmode,
10548 BYTES_BIG_ENDIAN ? 0
10549 : count / BITS_PER_UNIT);
10551 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10552 : ZERO_EXTEND, int_mode, new_rtx);
10553 count = 0;
10554 continue;
10556 break;
10558 case SUBREG:
10559 /* The following rules apply only to scalars. */
10560 if (shift_mode != shift_unit_mode)
10561 break;
10562 int_mode = as_a <scalar_int_mode> (mode);
10563 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10565 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10566 the same number of words as what we've seen so far. Then store
10567 the widest mode in MODE. */
10568 if (subreg_lowpart_p (varop)
10569 && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10570 && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10571 && (unsigned int) ((GET_MODE_SIZE (inner_mode)
10572 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10573 == mode_words
10574 && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10576 varop = SUBREG_REG (varop);
10577 if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10578 mode = inner_mode;
10579 continue;
10581 break;
10583 case MULT:
10584 /* Some machines use MULT instead of ASHIFT because MULT
10585 is cheaper. But it is still better on those machines to
10586 merge two shifts into one. */
10587 if (CONST_INT_P (XEXP (varop, 1))
10588 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10590 varop
10591 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10592 XEXP (varop, 0),
10593 GEN_INT (exact_log2 (
10594 UINTVAL (XEXP (varop, 1)))));
10595 continue;
10597 break;
10599 case UDIV:
10600 /* Similar, for when divides are cheaper. */
10601 if (CONST_INT_P (XEXP (varop, 1))
10602 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10604 varop
10605 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10606 XEXP (varop, 0),
10607 GEN_INT (exact_log2 (
10608 UINTVAL (XEXP (varop, 1)))));
10609 continue;
10611 break;
10613 case ASHIFTRT:
10614 /* If we are extracting just the sign bit of an arithmetic
10615 right shift, that shift is not needed. However, the sign
10616 bit of a wider mode may be different from what would be
10617 interpreted as the sign bit in a narrower mode, so, if
10618 the result is narrower, don't discard the shift. */
10619 if (code == LSHIFTRT
10620 && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10621 && (GET_MODE_UNIT_BITSIZE (result_mode)
10622 >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10624 varop = XEXP (varop, 0);
10625 continue;
10628 /* fall through */
10630 case LSHIFTRT:
10631 case ASHIFT:
10632 case ROTATE:
10633 /* The following rules apply only to scalars. */
10634 if (shift_mode != shift_unit_mode)
10635 break;
10636 int_mode = as_a <scalar_int_mode> (mode);
10637 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10638 int_result_mode = as_a <scalar_int_mode> (result_mode);
10640 /* Here we have two nested shifts. The result is usually the
10641 AND of a new shift with a mask. We compute the result below. */
10642 if (CONST_INT_P (XEXP (varop, 1))
10643 && INTVAL (XEXP (varop, 1)) >= 0
10644 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10645 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10646 && HWI_COMPUTABLE_MODE_P (int_mode))
10648 enum rtx_code first_code = GET_CODE (varop);
10649 unsigned int first_count = INTVAL (XEXP (varop, 1));
10650 unsigned HOST_WIDE_INT mask;
10651 rtx mask_rtx;
10653 /* We have one common special case. We can't do any merging if
10654 the inner code is an ASHIFTRT of a smaller mode. However, if
10655 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10656 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10657 we can convert it to
10658 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10659 This simplifies certain SIGN_EXTEND operations. */
10660 if (code == ASHIFT && first_code == ASHIFTRT
10661 && count == (GET_MODE_PRECISION (int_result_mode)
10662 - GET_MODE_PRECISION (int_varop_mode)))
10664 /* C3 has the low-order C1 bits zero. */
10666 mask = GET_MODE_MASK (int_mode)
10667 & ~((HOST_WIDE_INT_1U << first_count) - 1);
10669 varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10670 XEXP (varop, 0), mask);
10671 varop = simplify_shift_const (NULL_RTX, ASHIFT,
10672 int_result_mode, varop, count);
10673 count = first_count;
10674 code = ASHIFTRT;
10675 continue;
10678 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10679 than C1 high-order bits equal to the sign bit, we can convert
10680 this to either an ASHIFT or an ASHIFTRT depending on the
10681 two counts.
10683 We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10685 if (code == ASHIFTRT && first_code == ASHIFT
10686 && int_varop_mode == shift_unit_mode
10687 && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10688 > first_count))
10690 varop = XEXP (varop, 0);
10691 count -= first_count;
10692 if (count < 0)
10694 count = -count;
10695 code = ASHIFT;
10698 continue;
10701 /* There are some cases we can't do. If CODE is ASHIFTRT,
10702 we can only do this if FIRST_CODE is also ASHIFTRT.
10704 We can't do the case when CODE is ROTATE and FIRST_CODE is
10705 ASHIFTRT.
10707 If the mode of this shift is not the mode of the outer shift,
10708 we can't do this if either shift is a right shift or ROTATE.
10710 Finally, we can't do any of these if the mode is too wide
10711 unless the codes are the same.
10713 Handle the case where the shift codes are the same
10714 first. */
10716 if (code == first_code)
10718 if (int_varop_mode != int_result_mode
10719 && (code == ASHIFTRT || code == LSHIFTRT
10720 || code == ROTATE))
10721 break;
10723 count += first_count;
10724 varop = XEXP (varop, 0);
10725 continue;
10728 if (code == ASHIFTRT
10729 || (code == ROTATE && first_code == ASHIFTRT)
10730 || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10731 || (int_varop_mode != int_result_mode
10732 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10733 || first_code == ROTATE
10734 || code == ROTATE)))
10735 break;
10737 /* To compute the mask to apply after the shift, shift the
10738 nonzero bits of the inner shift the same way the
10739 outer shift will. */
10741 mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10742 int_result_mode);
10744 mask_rtx
10745 = simplify_const_binary_operation (code, int_result_mode,
10746 mask_rtx, GEN_INT (count));
10748 /* Give up if we can't compute an outer operation to use. */
10749 if (mask_rtx == 0
10750 || !CONST_INT_P (mask_rtx)
10751 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10752 INTVAL (mask_rtx),
10753 int_result_mode, &complement_p))
10754 break;
10756 /* If the shifts are in the same direction, we add the
10757 counts. Otherwise, we subtract them. */
10758 if ((code == ASHIFTRT || code == LSHIFTRT)
10759 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10760 count += first_count;
10761 else
10762 count -= first_count;
10764 /* If COUNT is positive, the new shift is usually CODE,
10765 except for the two exceptions below, in which case it is
10766 FIRST_CODE. If the count is negative, FIRST_CODE should
10767 always be used */
10768 if (count > 0
10769 && ((first_code == ROTATE && code == ASHIFT)
10770 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10771 code = first_code;
10772 else if (count < 0)
10773 code = first_code, count = -count;
10775 varop = XEXP (varop, 0);
10776 continue;
10779 /* If we have (A << B << C) for any shift, we can convert this to
10780 (A << C << B). This wins if A is a constant. Only try this if
10781 B is not a constant. */
10783 else if (GET_CODE (varop) == code
10784 && CONST_INT_P (XEXP (varop, 0))
10785 && !CONST_INT_P (XEXP (varop, 1)))
10787 /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10788 sure the result will be masked. See PR70222. */
10789 if (code == LSHIFTRT
10790 && int_mode != int_result_mode
10791 && !merge_outer_ops (&outer_op, &outer_const, AND,
10792 GET_MODE_MASK (int_result_mode)
10793 >> orig_count, int_result_mode,
10794 &complement_p))
10795 break;
10796 /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
10797 up outer sign extension (often left and right shift) is
10798 hardly more efficient than the original. See PR70429. */
10799 if (code == ASHIFTRT && int_mode != int_result_mode)
10800 break;
10802 rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10803 XEXP (varop, 0),
10804 GEN_INT (count));
10805 varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10806 count = 0;
10807 continue;
10809 break;
10811 case NOT:
10812 /* The following rules apply only to scalars. */
10813 if (shift_mode != shift_unit_mode)
10814 break;
10816 /* Make this fit the case below. */
10817 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10818 continue;
10820 case IOR:
10821 case AND:
10822 case XOR:
10823 /* The following rules apply only to scalars. */
10824 if (shift_mode != shift_unit_mode)
10825 break;
10826 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10827 int_result_mode = as_a <scalar_int_mode> (result_mode);
10829 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10830 with C the size of VAROP - 1 and the shift is logical if
10831 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10832 we have an (le X 0) operation. If we have an arithmetic shift
10833 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10834 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10836 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10837 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10838 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10839 && (code == LSHIFTRT || code == ASHIFTRT)
10840 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
10841 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10843 count = 0;
10844 varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
10845 const0_rtx);
10847 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10848 varop = gen_rtx_NEG (int_varop_mode, varop);
10850 continue;
10853 /* If we have (shift (logical)), move the logical to the outside
10854 to allow it to possibly combine with another logical and the
10855 shift to combine with another shift. This also canonicalizes to
10856 what a ZERO_EXTRACT looks like. Also, some machines have
10857 (and (shift)) insns. */
10859 if (CONST_INT_P (XEXP (varop, 1))
10860 /* We can't do this if we have (ashiftrt (xor)) and the
10861 constant has its sign bit set in shift_unit_mode with
10862 shift_unit_mode wider than result_mode. */
10863 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10864 && int_result_mode != shift_unit_mode
10865 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10866 shift_unit_mode))
10867 && (new_rtx = simplify_const_binary_operation
10868 (code, int_result_mode,
10869 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
10870 GEN_INT (count))) != 0
10871 && CONST_INT_P (new_rtx)
10872 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10873 INTVAL (new_rtx), int_result_mode,
10874 &complement_p))
10876 varop = XEXP (varop, 0);
10877 continue;
10880 /* If we can't do that, try to simplify the shift in each arm of the
10881 logical expression, make a new logical expression, and apply
10882 the inverse distributive law. This also can't be done for
10883 (ashiftrt (xor)) where we've widened the shift and the constant
10884 changes the sign bit. */
10885 if (CONST_INT_P (XEXP (varop, 1))
10886 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10887 && int_result_mode != shift_unit_mode
10888 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10889 shift_unit_mode)))
10891 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10892 XEXP (varop, 0), count);
10893 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10894 XEXP (varop, 1), count);
10896 varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
10897 lhs, rhs);
10898 varop = apply_distributive_law (varop);
10900 count = 0;
10901 continue;
10903 break;
10905 case EQ:
10906 /* The following rules apply only to scalars. */
10907 if (shift_mode != shift_unit_mode)
10908 break;
10909 int_result_mode = as_a <scalar_int_mode> (result_mode);
10911 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10912 says that the sign bit can be tested, FOO has mode MODE, C is
10913 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10914 that may be nonzero. */
10915 if (code == LSHIFTRT
10916 && XEXP (varop, 1) == const0_rtx
10917 && GET_MODE (XEXP (varop, 0)) == int_result_mode
10918 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10919 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10920 && STORE_FLAG_VALUE == -1
10921 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
10922 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
10923 int_result_mode, &complement_p))
10925 varop = XEXP (varop, 0);
10926 count = 0;
10927 continue;
10929 break;
10931 case NEG:
10932 /* The following rules apply only to scalars. */
10933 if (shift_mode != shift_unit_mode)
10934 break;
10935 int_result_mode = as_a <scalar_int_mode> (result_mode);
10937 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10938 than the number of bits in the mode is equivalent to A. */
10939 if (code == LSHIFTRT
10940 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10941 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
10943 varop = XEXP (varop, 0);
10944 count = 0;
10945 continue;
10948 /* NEG commutes with ASHIFT since it is multiplication. Move the
10949 NEG outside to allow shifts to combine. */
10950 if (code == ASHIFT
10951 && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
10952 int_result_mode, &complement_p))
10954 varop = XEXP (varop, 0);
10955 continue;
10957 break;
10959 case PLUS:
10960 /* The following rules apply only to scalars. */
10961 if (shift_mode != shift_unit_mode)
10962 break;
10963 int_result_mode = as_a <scalar_int_mode> (result_mode);
10965 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10966 is one less than the number of bits in the mode is
10967 equivalent to (xor A 1). */
10968 if (code == LSHIFTRT
10969 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
10970 && XEXP (varop, 1) == constm1_rtx
10971 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
10972 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
10973 int_result_mode, &complement_p))
10975 count = 0;
10976 varop = XEXP (varop, 0);
10977 continue;
10980 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10981 that might be nonzero in BAR are those being shifted out and those
10982 bits are known zero in FOO, we can replace the PLUS with FOO.
10983 Similarly in the other operand order. This code occurs when
10984 we are computing the size of a variable-size array. */
10986 if ((code == ASHIFTRT || code == LSHIFTRT)
10987 && count < HOST_BITS_PER_WIDE_INT
10988 && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
10989 && (nonzero_bits (XEXP (varop, 1), int_result_mode)
10990 & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
10992 varop = XEXP (varop, 0);
10993 continue;
10995 else if ((code == ASHIFTRT || code == LSHIFTRT)
10996 && count < HOST_BITS_PER_WIDE_INT
10997 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10998 && 0 == (nonzero_bits (XEXP (varop, 0), int_result_mode)
10999 >> count)
11000 && 0 == (nonzero_bits (XEXP (varop, 0), int_result_mode)
11001 & nonzero_bits (XEXP (varop, 1), int_result_mode)))
11003 varop = XEXP (varop, 1);
11004 continue;
11007 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11008 if (code == ASHIFT
11009 && CONST_INT_P (XEXP (varop, 1))
11010 && (new_rtx = simplify_const_binary_operation
11011 (ASHIFT, int_result_mode,
11012 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11013 GEN_INT (count))) != 0
11014 && CONST_INT_P (new_rtx)
11015 && merge_outer_ops (&outer_op, &outer_const, PLUS,
11016 INTVAL (new_rtx), int_result_mode,
11017 &complement_p))
11019 varop = XEXP (varop, 0);
11020 continue;
11023 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11024 signbit', and attempt to change the PLUS to an XOR and move it to
11025 the outer operation as is done above in the AND/IOR/XOR case
11026 leg for shift(logical). See details in logical handling above
11027 for reasoning in doing so. */
11028 if (code == LSHIFTRT
11029 && CONST_INT_P (XEXP (varop, 1))
11030 && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11031 && (new_rtx = simplify_const_binary_operation
11032 (code, int_result_mode,
11033 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11034 GEN_INT (count))) != 0
11035 && CONST_INT_P (new_rtx)
11036 && merge_outer_ops (&outer_op, &outer_const, XOR,
11037 INTVAL (new_rtx), int_result_mode,
11038 &complement_p))
11040 varop = XEXP (varop, 0);
11041 continue;
11044 break;
11046 case MINUS:
11047 /* The following rules apply only to scalars. */
11048 if (shift_mode != shift_unit_mode)
11049 break;
11050 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11052 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11053 with C the size of VAROP - 1 and the shift is logical if
11054 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11055 we have a (gt X 0) operation. If the shift is arithmetic with
11056 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11057 we have a (neg (gt X 0)) operation. */
11059 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11060 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11061 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11062 && (code == LSHIFTRT || code == ASHIFTRT)
11063 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11064 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11065 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11067 count = 0;
11068 varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11069 const0_rtx);
11071 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11072 varop = gen_rtx_NEG (int_varop_mode, varop);
11074 continue;
11076 break;
11078 case TRUNCATE:
11079 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11080 if the truncate does not affect the value. */
11081 if (code == LSHIFTRT
11082 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11083 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11084 && (INTVAL (XEXP (XEXP (varop, 0), 1))
11085 >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11086 - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11088 rtx varop_inner = XEXP (varop, 0);
11090 varop_inner
11091 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11092 XEXP (varop_inner, 0),
11093 GEN_INT
11094 (count + INTVAL (XEXP (varop_inner, 1))));
11095 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11096 count = 0;
11097 continue;
11099 break;
11101 default:
11102 break;
11105 break;
11108 shift_mode = result_mode;
11109 if (shift_mode != mode)
11111 /* We only change the modes of scalar shifts. */
11112 int_mode = as_a <scalar_int_mode> (mode);
11113 int_result_mode = as_a <scalar_int_mode> (result_mode);
11114 shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11115 int_mode, outer_op, outer_const);
11118 /* We have now finished analyzing the shift. The result should be
11119 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11120 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11121 to the result of the shift. OUTER_CONST is the relevant constant,
11122 but we must turn off all bits turned off in the shift. */
11124 if (outer_op == UNKNOWN
11125 && orig_code == code && orig_count == count
11126 && varop == orig_varop
11127 && shift_mode == GET_MODE (varop))
11128 return NULL_RTX;
11130 /* Make a SUBREG if necessary. If we can't make it, fail. */
11131 varop = gen_lowpart (shift_mode, varop);
11132 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11133 return NULL_RTX;
11135 /* If we have an outer operation and we just made a shift, it is
11136 possible that we could have simplified the shift were it not
11137 for the outer operation. So try to do the simplification
11138 recursively. */
11140 if (outer_op != UNKNOWN)
11141 x = simplify_shift_const_1 (code, shift_mode, varop, count);
11142 else
11143 x = NULL_RTX;
11145 if (x == NULL_RTX)
11146 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
11148 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11149 turn off all the bits that the shift would have turned off. */
11150 if (orig_code == LSHIFTRT && result_mode != shift_mode)
11151 /* We only change the modes of scalar shifts. */
11152 x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11153 x, GET_MODE_MASK (result_mode) >> orig_count);
11155 /* Do the remainder of the processing in RESULT_MODE. */
11156 x = gen_lowpart_or_truncate (result_mode, x);
11158 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11159 operation. */
11160 if (complement_p)
11161 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11163 if (outer_op != UNKNOWN)
11165 int_result_mode = as_a <scalar_int_mode> (result_mode);
11167 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11168 && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11169 outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11171 if (outer_op == AND)
11172 x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11173 else if (outer_op == SET)
11175 /* This means that we have determined that the result is
11176 equivalent to a constant. This should be rare. */
11177 if (!side_effects_p (x))
11178 x = GEN_INT (outer_const);
11180 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11181 x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11182 else
11183 x = simplify_gen_binary (outer_op, int_result_mode, x,
11184 GEN_INT (outer_const));
11187 return x;
11190 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11191 The result of the shift is RESULT_MODE. If we cannot simplify it,
11192 return X or, if it is NULL, synthesize the expression with
11193 simplify_gen_binary. Otherwise, return a simplified value.
11195 The shift is normally computed in the widest mode we find in VAROP, as
11196 long as it isn't a different number of words than RESULT_MODE. Exceptions
11197 are ASHIFTRT and ROTATE, which are always done in their original mode. */
11199 static rtx
11200 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11201 rtx varop, int count)
11203 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11204 if (tem)
11205 return tem;
11207 if (!x)
11208 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
11209 if (GET_MODE (x) != result_mode)
11210 x = gen_lowpart (result_mode, x);
11211 return x;
11215 /* A subroutine of recog_for_combine. See there for arguments and
11216 return value. */
11218 static int
11219 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11221 rtx pat = *pnewpat;
11222 rtx pat_without_clobbers;
11223 int insn_code_number;
11224 int num_clobbers_to_add = 0;
11225 int i;
11226 rtx notes = NULL_RTX;
11227 rtx old_notes, old_pat;
11228 int old_icode;
11230 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11231 we use to indicate that something didn't match. If we find such a
11232 thing, force rejection. */
11233 if (GET_CODE (pat) == PARALLEL)
11234 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11235 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11236 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11237 return -1;
11239 old_pat = PATTERN (insn);
11240 old_notes = REG_NOTES (insn);
11241 PATTERN (insn) = pat;
11242 REG_NOTES (insn) = NULL_RTX;
11244 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11245 if (dump_file && (dump_flags & TDF_DETAILS))
11247 if (insn_code_number < 0)
11248 fputs ("Failed to match this instruction:\n", dump_file);
11249 else
11250 fputs ("Successfully matched this instruction:\n", dump_file);
11251 print_rtl_single (dump_file, pat);
11254 /* If it isn't, there is the possibility that we previously had an insn
11255 that clobbered some register as a side effect, but the combined
11256 insn doesn't need to do that. So try once more without the clobbers
11257 unless this represents an ASM insn. */
11259 if (insn_code_number < 0 && ! check_asm_operands (pat)
11260 && GET_CODE (pat) == PARALLEL)
11262 int pos;
11264 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11265 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11267 if (i != pos)
11268 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11269 pos++;
11272 SUBST_INT (XVECLEN (pat, 0), pos);
11274 if (pos == 1)
11275 pat = XVECEXP (pat, 0, 0);
11277 PATTERN (insn) = pat;
11278 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11279 if (dump_file && (dump_flags & TDF_DETAILS))
11281 if (insn_code_number < 0)
11282 fputs ("Failed to match this instruction:\n", dump_file);
11283 else
11284 fputs ("Successfully matched this instruction:\n", dump_file);
11285 print_rtl_single (dump_file, pat);
11289 pat_without_clobbers = pat;
11291 PATTERN (insn) = old_pat;
11292 REG_NOTES (insn) = old_notes;
11294 /* Recognize all noop sets, these will be killed by followup pass. */
11295 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11296 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11298 /* If we had any clobbers to add, make a new pattern than contains
11299 them. Then check to make sure that all of them are dead. */
11300 if (num_clobbers_to_add)
11302 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11303 rtvec_alloc (GET_CODE (pat) == PARALLEL
11304 ? (XVECLEN (pat, 0)
11305 + num_clobbers_to_add)
11306 : num_clobbers_to_add + 1));
11308 if (GET_CODE (pat) == PARALLEL)
11309 for (i = 0; i < XVECLEN (pat, 0); i++)
11310 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11311 else
11312 XVECEXP (newpat, 0, 0) = pat;
11314 add_clobbers (newpat, insn_code_number);
11316 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11317 i < XVECLEN (newpat, 0); i++)
11319 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11320 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11321 return -1;
11322 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11324 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11325 notes = alloc_reg_note (REG_UNUSED,
11326 XEXP (XVECEXP (newpat, 0, i), 0), notes);
11329 pat = newpat;
11332 if (insn_code_number >= 0
11333 && insn_code_number != NOOP_MOVE_INSN_CODE)
11335 old_pat = PATTERN (insn);
11336 old_notes = REG_NOTES (insn);
11337 old_icode = INSN_CODE (insn);
11338 PATTERN (insn) = pat;
11339 REG_NOTES (insn) = notes;
11340 INSN_CODE (insn) = insn_code_number;
11342 /* Allow targets to reject combined insn. */
11343 if (!targetm.legitimate_combined_insn (insn))
11345 if (dump_file && (dump_flags & TDF_DETAILS))
11346 fputs ("Instruction not appropriate for target.",
11347 dump_file);
11349 /* Callers expect recog_for_combine to strip
11350 clobbers from the pattern on failure. */
11351 pat = pat_without_clobbers;
11352 notes = NULL_RTX;
11354 insn_code_number = -1;
11357 PATTERN (insn) = old_pat;
11358 REG_NOTES (insn) = old_notes;
11359 INSN_CODE (insn) = old_icode;
11362 *pnewpat = pat;
11363 *pnotes = notes;
11365 return insn_code_number;
11368 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11369 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11370 Return whether anything was so changed. */
11372 static bool
11373 change_zero_ext (rtx pat)
11375 bool changed = false;
11376 rtx *src = &SET_SRC (pat);
11378 subrtx_ptr_iterator::array_type array;
11379 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11381 rtx x = **iter;
11382 scalar_int_mode mode, inner_mode;
11383 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11384 continue;
11385 int size;
11387 if (GET_CODE (x) == ZERO_EXTRACT
11388 && CONST_INT_P (XEXP (x, 1))
11389 && CONST_INT_P (XEXP (x, 2))
11390 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11391 && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11393 size = INTVAL (XEXP (x, 1));
11395 int start = INTVAL (XEXP (x, 2));
11396 if (BITS_BIG_ENDIAN)
11397 start = GET_MODE_PRECISION (inner_mode) - size - start;
11399 if (start)
11400 x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0), GEN_INT (start));
11401 else
11402 x = XEXP (x, 0);
11403 if (mode != inner_mode)
11404 x = gen_lowpart_SUBREG (mode, x);
11406 else if (GET_CODE (x) == ZERO_EXTEND
11407 && GET_CODE (XEXP (x, 0)) == SUBREG
11408 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11409 && !paradoxical_subreg_p (XEXP (x, 0))
11410 && subreg_lowpart_p (XEXP (x, 0)))
11412 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11413 size = GET_MODE_PRECISION (inner_mode);
11414 x = SUBREG_REG (XEXP (x, 0));
11415 if (GET_MODE (x) != mode)
11416 x = gen_lowpart_SUBREG (mode, x);
11418 else if (GET_CODE (x) == ZERO_EXTEND
11419 && REG_P (XEXP (x, 0))
11420 && HARD_REGISTER_P (XEXP (x, 0))
11421 && can_change_dest_mode (XEXP (x, 0), 0, mode))
11423 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11424 size = GET_MODE_PRECISION (inner_mode);
11425 x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11427 else
11428 continue;
11430 if (!(GET_CODE (x) == LSHIFTRT
11431 && CONST_INT_P (XEXP (x, 1))
11432 && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11434 wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11435 x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11438 SUBST (**iter, x);
11439 changed = true;
11442 if (changed)
11443 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11444 maybe_swap_commutative_operands (**iter);
11446 rtx *dst = &SET_DEST (pat);
11447 scalar_int_mode mode;
11448 if (GET_CODE (*dst) == ZERO_EXTRACT
11449 && REG_P (XEXP (*dst, 0))
11450 && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11451 && CONST_INT_P (XEXP (*dst, 1))
11452 && CONST_INT_P (XEXP (*dst, 2)))
11454 rtx reg = XEXP (*dst, 0);
11455 int width = INTVAL (XEXP (*dst, 1));
11456 int offset = INTVAL (XEXP (*dst, 2));
11457 int reg_width = GET_MODE_PRECISION (mode);
11458 if (BITS_BIG_ENDIAN)
11459 offset = reg_width - width - offset;
11461 rtx x, y, z, w;
11462 wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11463 wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11464 x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11465 if (offset)
11466 y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11467 else
11468 y = SET_SRC (pat);
11469 z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11470 w = gen_rtx_IOR (mode, x, z);
11471 SUBST (SET_DEST (pat), reg);
11472 SUBST (SET_SRC (pat), w);
11474 changed = true;
11477 return changed;
11480 /* Like recog, but we receive the address of a pointer to a new pattern.
11481 We try to match the rtx that the pointer points to.
11482 If that fails, we may try to modify or replace the pattern,
11483 storing the replacement into the same pointer object.
11485 Modifications include deletion or addition of CLOBBERs. If the
11486 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11487 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11488 (and undo if that fails).
11490 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11491 the CLOBBERs are placed.
11493 The value is the final insn code from the pattern ultimately matched,
11494 or -1. */
11496 static int
11497 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11499 rtx pat = *pnewpat;
11500 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11501 if (insn_code_number >= 0 || check_asm_operands (pat))
11502 return insn_code_number;
11504 void *marker = get_undo_marker ();
11505 bool changed = false;
11507 if (GET_CODE (pat) == SET)
11508 changed = change_zero_ext (pat);
11509 else if (GET_CODE (pat) == PARALLEL)
11511 int i;
11512 for (i = 0; i < XVECLEN (pat, 0); i++)
11514 rtx set = XVECEXP (pat, 0, i);
11515 if (GET_CODE (set) == SET)
11516 changed |= change_zero_ext (set);
11520 if (changed)
11522 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11524 if (insn_code_number < 0)
11525 undo_to_marker (marker);
11528 return insn_code_number;
11531 /* Like gen_lowpart_general but for use by combine. In combine it
11532 is not possible to create any new pseudoregs. However, it is
11533 safe to create invalid memory addresses, because combine will
11534 try to recognize them and all they will do is make the combine
11535 attempt fail.
11537 If for some reason this cannot do its job, an rtx
11538 (clobber (const_int 0)) is returned.
11539 An insn containing that will not be recognized. */
11541 static rtx
11542 gen_lowpart_for_combine (machine_mode omode, rtx x)
11544 machine_mode imode = GET_MODE (x);
11545 unsigned int osize = GET_MODE_SIZE (omode);
11546 unsigned int isize = GET_MODE_SIZE (imode);
11547 rtx result;
11549 if (omode == imode)
11550 return x;
11552 /* We can only support MODE being wider than a word if X is a
11553 constant integer or has a mode the same size. */
11554 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11555 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11556 goto fail;
11558 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11559 won't know what to do. So we will strip off the SUBREG here and
11560 process normally. */
11561 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11563 x = SUBREG_REG (x);
11565 /* For use in case we fall down into the address adjustments
11566 further below, we need to adjust the known mode and size of
11567 x; imode and isize, since we just adjusted x. */
11568 imode = GET_MODE (x);
11570 if (imode == omode)
11571 return x;
11573 isize = GET_MODE_SIZE (imode);
11576 result = gen_lowpart_common (omode, x);
11578 if (result)
11579 return result;
11581 if (MEM_P (x))
11583 int offset = 0;
11585 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11586 address. */
11587 if (MEM_VOLATILE_P (x)
11588 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11589 goto fail;
11591 /* If we want to refer to something bigger than the original memref,
11592 generate a paradoxical subreg instead. That will force a reload
11593 of the original memref X. */
11594 if (paradoxical_subreg_p (omode, imode))
11595 return gen_rtx_SUBREG (omode, x, 0);
11597 if (WORDS_BIG_ENDIAN)
11598 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11600 /* Adjust the address so that the address-after-the-data is
11601 unchanged. */
11602 if (BYTES_BIG_ENDIAN)
11603 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11605 return adjust_address_nv (x, omode, offset);
11608 /* If X is a comparison operator, rewrite it in a new mode. This
11609 probably won't match, but may allow further simplifications. */
11610 else if (COMPARISON_P (x))
11611 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11613 /* If we couldn't simplify X any other way, just enclose it in a
11614 SUBREG. Normally, this SUBREG won't match, but some patterns may
11615 include an explicit SUBREG or we may simplify it further in combine. */
11616 else
11618 rtx res;
11620 if (imode == VOIDmode)
11622 imode = int_mode_for_mode (omode).require ();
11623 x = gen_lowpart_common (imode, x);
11624 if (x == NULL)
11625 goto fail;
11627 res = lowpart_subreg (omode, x, imode);
11628 if (res)
11629 return res;
11632 fail:
11633 return gen_rtx_CLOBBER (omode, const0_rtx);
11636 /* Try to simplify a comparison between OP0 and a constant OP1,
11637 where CODE is the comparison code that will be tested, into a
11638 (CODE OP0 const0_rtx) form.
11640 The result is a possibly different comparison code to use.
11641 *POP1 may be updated. */
11643 static enum rtx_code
11644 simplify_compare_const (enum rtx_code code, machine_mode mode,
11645 rtx op0, rtx *pop1)
11647 scalar_int_mode int_mode;
11648 HOST_WIDE_INT const_op = INTVAL (*pop1);
11650 /* Get the constant we are comparing against and turn off all bits
11651 not on in our mode. */
11652 if (mode != VOIDmode)
11653 const_op = trunc_int_for_mode (const_op, mode);
11655 /* If we are comparing against a constant power of two and the value
11656 being compared can only have that single bit nonzero (e.g., it was
11657 `and'ed with that bit), we can replace this with a comparison
11658 with zero. */
11659 if (const_op
11660 && (code == EQ || code == NE || code == GE || code == GEU
11661 || code == LT || code == LTU)
11662 && is_a <scalar_int_mode> (mode, &int_mode)
11663 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11664 && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11665 && (nonzero_bits (op0, int_mode)
11666 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11668 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11669 const_op = 0;
11672 /* Similarly, if we are comparing a value known to be either -1 or
11673 0 with -1, change it to the opposite comparison against zero. */
11674 if (const_op == -1
11675 && (code == EQ || code == NE || code == GT || code == LE
11676 || code == GEU || code == LTU)
11677 && is_a <scalar_int_mode> (mode, &int_mode)
11678 && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11680 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11681 const_op = 0;
11684 /* Do some canonicalizations based on the comparison code. We prefer
11685 comparisons against zero and then prefer equality comparisons.
11686 If we can reduce the size of a constant, we will do that too. */
11687 switch (code)
11689 case LT:
11690 /* < C is equivalent to <= (C - 1) */
11691 if (const_op > 0)
11693 const_op -= 1;
11694 code = LE;
11695 /* ... fall through to LE case below. */
11696 gcc_fallthrough ();
11698 else
11699 break;
11701 case LE:
11702 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11703 if (const_op < 0)
11705 const_op += 1;
11706 code = LT;
11709 /* If we are doing a <= 0 comparison on a value known to have
11710 a zero sign bit, we can replace this with == 0. */
11711 else if (const_op == 0
11712 && is_a <scalar_int_mode> (mode, &int_mode)
11713 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11714 && (nonzero_bits (op0, int_mode)
11715 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11716 == 0)
11717 code = EQ;
11718 break;
11720 case GE:
11721 /* >= C is equivalent to > (C - 1). */
11722 if (const_op > 0)
11724 const_op -= 1;
11725 code = GT;
11726 /* ... fall through to GT below. */
11727 gcc_fallthrough ();
11729 else
11730 break;
11732 case GT:
11733 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11734 if (const_op < 0)
11736 const_op += 1;
11737 code = GE;
11740 /* If we are doing a > 0 comparison on a value known to have
11741 a zero sign bit, we can replace this with != 0. */
11742 else if (const_op == 0
11743 && is_a <scalar_int_mode> (mode, &int_mode)
11744 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11745 && (nonzero_bits (op0, int_mode)
11746 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11747 == 0)
11748 code = NE;
11749 break;
11751 case LTU:
11752 /* < C is equivalent to <= (C - 1). */
11753 if (const_op > 0)
11755 const_op -= 1;
11756 code = LEU;
11757 /* ... fall through ... */
11759 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11760 else if (is_a <scalar_int_mode> (mode, &int_mode)
11761 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11762 && ((unsigned HOST_WIDE_INT) const_op
11763 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11765 const_op = 0;
11766 code = GE;
11767 break;
11769 else
11770 break;
11772 case LEU:
11773 /* unsigned <= 0 is equivalent to == 0 */
11774 if (const_op == 0)
11775 code = EQ;
11776 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11777 else if (is_a <scalar_int_mode> (mode, &int_mode)
11778 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11779 && ((unsigned HOST_WIDE_INT) const_op
11780 == ((HOST_WIDE_INT_1U
11781 << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11783 const_op = 0;
11784 code = GE;
11786 break;
11788 case GEU:
11789 /* >= C is equivalent to > (C - 1). */
11790 if (const_op > 1)
11792 const_op -= 1;
11793 code = GTU;
11794 /* ... fall through ... */
11797 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11798 else if (is_a <scalar_int_mode> (mode, &int_mode)
11799 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11800 && ((unsigned HOST_WIDE_INT) const_op
11801 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11803 const_op = 0;
11804 code = LT;
11805 break;
11807 else
11808 break;
11810 case GTU:
11811 /* unsigned > 0 is equivalent to != 0 */
11812 if (const_op == 0)
11813 code = NE;
11814 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11815 else if (is_a <scalar_int_mode> (mode, &int_mode)
11816 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11817 && ((unsigned HOST_WIDE_INT) const_op
11818 == (HOST_WIDE_INT_1U
11819 << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
11821 const_op = 0;
11822 code = LT;
11824 break;
11826 default:
11827 break;
11830 *pop1 = GEN_INT (const_op);
11831 return code;
11834 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11835 comparison code that will be tested.
11837 The result is a possibly different comparison code to use. *POP0 and
11838 *POP1 may be updated.
11840 It is possible that we might detect that a comparison is either always
11841 true or always false. However, we do not perform general constant
11842 folding in combine, so this knowledge isn't useful. Such tautologies
11843 should have been detected earlier. Hence we ignore all such cases. */
11845 static enum rtx_code
11846 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11848 rtx op0 = *pop0;
11849 rtx op1 = *pop1;
11850 rtx tem, tem1;
11851 int i;
11852 scalar_int_mode mode, inner_mode, tmode;
11853 opt_scalar_int_mode tmode_iter;
11855 /* Try a few ways of applying the same transformation to both operands. */
11856 while (1)
11858 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11859 so check specially. */
11860 if (!WORD_REGISTER_OPERATIONS
11861 && code != GTU && code != GEU && code != LTU && code != LEU
11862 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11863 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11864 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11865 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11866 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11867 && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
11868 && (is_a <scalar_int_mode>
11869 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
11870 && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
11871 && CONST_INT_P (XEXP (op0, 1))
11872 && XEXP (op0, 1) == XEXP (op1, 1)
11873 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11874 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11875 && (INTVAL (XEXP (op0, 1))
11876 == (GET_MODE_PRECISION (mode)
11877 - GET_MODE_PRECISION (inner_mode))))
11879 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11880 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11883 /* If both operands are the same constant shift, see if we can ignore the
11884 shift. We can if the shift is a rotate or if the bits shifted out of
11885 this shift are known to be zero for both inputs and if the type of
11886 comparison is compatible with the shift. */
11887 if (GET_CODE (op0) == GET_CODE (op1)
11888 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11889 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11890 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11891 && (code != GT && code != LT && code != GE && code != LE))
11892 || (GET_CODE (op0) == ASHIFTRT
11893 && (code != GTU && code != LTU
11894 && code != GEU && code != LEU)))
11895 && CONST_INT_P (XEXP (op0, 1))
11896 && INTVAL (XEXP (op0, 1)) >= 0
11897 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11898 && XEXP (op0, 1) == XEXP (op1, 1))
11900 machine_mode mode = GET_MODE (op0);
11901 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11902 int shift_count = INTVAL (XEXP (op0, 1));
11904 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11905 mask &= (mask >> shift_count) << shift_count;
11906 else if (GET_CODE (op0) == ASHIFT)
11907 mask = (mask & (mask << shift_count)) >> shift_count;
11909 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11910 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11911 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11912 else
11913 break;
11916 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11917 SUBREGs are of the same mode, and, in both cases, the AND would
11918 be redundant if the comparison was done in the narrower mode,
11919 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11920 and the operand's possibly nonzero bits are 0xffffff01; in that case
11921 if we only care about QImode, we don't need the AND). This case
11922 occurs if the output mode of an scc insn is not SImode and
11923 STORE_FLAG_VALUE == 1 (e.g., the 386).
11925 Similarly, check for a case where the AND's are ZERO_EXTEND
11926 operations from some narrower mode even though a SUBREG is not
11927 present. */
11929 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11930 && CONST_INT_P (XEXP (op0, 1))
11931 && CONST_INT_P (XEXP (op1, 1)))
11933 rtx inner_op0 = XEXP (op0, 0);
11934 rtx inner_op1 = XEXP (op1, 0);
11935 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11936 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11937 int changed = 0;
11939 if (paradoxical_subreg_p (inner_op0)
11940 && GET_CODE (inner_op1) == SUBREG
11941 && (GET_MODE (SUBREG_REG (inner_op0))
11942 == GET_MODE (SUBREG_REG (inner_op1)))
11943 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11944 <= HOST_BITS_PER_WIDE_INT)
11945 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11946 GET_MODE (SUBREG_REG (inner_op0)))))
11947 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11948 GET_MODE (SUBREG_REG (inner_op1))))))
11950 op0 = SUBREG_REG (inner_op0);
11951 op1 = SUBREG_REG (inner_op1);
11953 /* The resulting comparison is always unsigned since we masked
11954 off the original sign bit. */
11955 code = unsigned_condition (code);
11957 changed = 1;
11960 else if (c0 == c1)
11961 FOR_EACH_MODE_UNTIL (tmode,
11962 as_a <scalar_int_mode> (GET_MODE (op0)))
11963 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11965 op0 = gen_lowpart_or_truncate (tmode, inner_op0);
11966 op1 = gen_lowpart_or_truncate (tmode, inner_op1);
11967 code = unsigned_condition (code);
11968 changed = 1;
11969 break;
11972 if (! changed)
11973 break;
11976 /* If both operands are NOT, we can strip off the outer operation
11977 and adjust the comparison code for swapped operands; similarly for
11978 NEG, except that this must be an equality comparison. */
11979 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11980 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11981 && (code == EQ || code == NE)))
11982 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11984 else
11985 break;
11988 /* If the first operand is a constant, swap the operands and adjust the
11989 comparison code appropriately, but don't do this if the second operand
11990 is already a constant integer. */
11991 if (swap_commutative_operands_p (op0, op1))
11993 std::swap (op0, op1);
11994 code = swap_condition (code);
11997 /* We now enter a loop during which we will try to simplify the comparison.
11998 For the most part, we only are concerned with comparisons with zero,
11999 but some things may really be comparisons with zero but not start
12000 out looking that way. */
12002 while (CONST_INT_P (op1))
12004 machine_mode raw_mode = GET_MODE (op0);
12005 scalar_int_mode int_mode;
12006 int equality_comparison_p;
12007 int sign_bit_comparison_p;
12008 int unsigned_comparison_p;
12009 HOST_WIDE_INT const_op;
12011 /* We only want to handle integral modes. This catches VOIDmode,
12012 CCmode, and the floating-point modes. An exception is that we
12013 can handle VOIDmode if OP0 is a COMPARE or a comparison
12014 operation. */
12016 if (GET_MODE_CLASS (raw_mode) != MODE_INT
12017 && ! (raw_mode == VOIDmode
12018 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12019 break;
12021 /* Try to simplify the compare to constant, possibly changing the
12022 comparison op, and/or changing op1 to zero. */
12023 code = simplify_compare_const (code, raw_mode, op0, &op1);
12024 const_op = INTVAL (op1);
12026 /* Compute some predicates to simplify code below. */
12028 equality_comparison_p = (code == EQ || code == NE);
12029 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12030 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12031 || code == GEU);
12033 /* If this is a sign bit comparison and we can do arithmetic in
12034 MODE, say that we will only be needing the sign bit of OP0. */
12035 if (sign_bit_comparison_p
12036 && is_a <scalar_int_mode> (raw_mode, &int_mode)
12037 && HWI_COMPUTABLE_MODE_P (int_mode))
12038 op0 = force_to_mode (op0, int_mode,
12039 HOST_WIDE_INT_1U
12040 << (GET_MODE_PRECISION (int_mode) - 1),
12043 if (COMPARISON_P (op0))
12045 /* We can't do anything if OP0 is a condition code value, rather
12046 than an actual data value. */
12047 if (const_op != 0
12048 || CC0_P (XEXP (op0, 0))
12049 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12050 break;
12052 /* Get the two operands being compared. */
12053 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12054 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12055 else
12056 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12058 /* Check for the cases where we simply want the result of the
12059 earlier test or the opposite of that result. */
12060 if (code == NE || code == EQ
12061 || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12062 && (code == LT || code == GE)))
12064 enum rtx_code new_code;
12065 if (code == LT || code == NE)
12066 new_code = GET_CODE (op0);
12067 else
12068 new_code = reversed_comparison_code (op0, NULL);
12070 if (new_code != UNKNOWN)
12072 code = new_code;
12073 op0 = tem;
12074 op1 = tem1;
12075 continue;
12078 break;
12081 if (raw_mode == VOIDmode)
12082 break;
12083 scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12085 /* Now try cases based on the opcode of OP0. If none of the cases
12086 does a "continue", we exit this loop immediately after the
12087 switch. */
12089 unsigned int mode_width = GET_MODE_PRECISION (mode);
12090 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12091 switch (GET_CODE (op0))
12093 case ZERO_EXTRACT:
12094 /* If we are extracting a single bit from a variable position in
12095 a constant that has only a single bit set and are comparing it
12096 with zero, we can convert this into an equality comparison
12097 between the position and the location of the single bit. */
12098 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12099 have already reduced the shift count modulo the word size. */
12100 if (!SHIFT_COUNT_TRUNCATED
12101 && CONST_INT_P (XEXP (op0, 0))
12102 && XEXP (op0, 1) == const1_rtx
12103 && equality_comparison_p && const_op == 0
12104 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12106 if (BITS_BIG_ENDIAN)
12107 i = BITS_PER_WORD - 1 - i;
12109 op0 = XEXP (op0, 2);
12110 op1 = GEN_INT (i);
12111 const_op = i;
12113 /* Result is nonzero iff shift count is equal to I. */
12114 code = reverse_condition (code);
12115 continue;
12118 /* fall through */
12120 case SIGN_EXTRACT:
12121 tem = expand_compound_operation (op0);
12122 if (tem != op0)
12124 op0 = tem;
12125 continue;
12127 break;
12129 case NOT:
12130 /* If testing for equality, we can take the NOT of the constant. */
12131 if (equality_comparison_p
12132 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12134 op0 = XEXP (op0, 0);
12135 op1 = tem;
12136 continue;
12139 /* If just looking at the sign bit, reverse the sense of the
12140 comparison. */
12141 if (sign_bit_comparison_p)
12143 op0 = XEXP (op0, 0);
12144 code = (code == GE ? LT : GE);
12145 continue;
12147 break;
12149 case NEG:
12150 /* If testing for equality, we can take the NEG of the constant. */
12151 if (equality_comparison_p
12152 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12154 op0 = XEXP (op0, 0);
12155 op1 = tem;
12156 continue;
12159 /* The remaining cases only apply to comparisons with zero. */
12160 if (const_op != 0)
12161 break;
12163 /* When X is ABS or is known positive,
12164 (neg X) is < 0 if and only if X != 0. */
12166 if (sign_bit_comparison_p
12167 && (GET_CODE (XEXP (op0, 0)) == ABS
12168 || (mode_width <= HOST_BITS_PER_WIDE_INT
12169 && (nonzero_bits (XEXP (op0, 0), mode)
12170 & (HOST_WIDE_INT_1U << (mode_width - 1)))
12171 == 0)))
12173 op0 = XEXP (op0, 0);
12174 code = (code == LT ? NE : EQ);
12175 continue;
12178 /* If we have NEG of something whose two high-order bits are the
12179 same, we know that "(-a) < 0" is equivalent to "a > 0". */
12180 if (num_sign_bit_copies (op0, mode) >= 2)
12182 op0 = XEXP (op0, 0);
12183 code = swap_condition (code);
12184 continue;
12186 break;
12188 case ROTATE:
12189 /* If we are testing equality and our count is a constant, we
12190 can perform the inverse operation on our RHS. */
12191 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12192 && (tem = simplify_binary_operation (ROTATERT, mode,
12193 op1, XEXP (op0, 1))) != 0)
12195 op0 = XEXP (op0, 0);
12196 op1 = tem;
12197 continue;
12200 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12201 a particular bit. Convert it to an AND of a constant of that
12202 bit. This will be converted into a ZERO_EXTRACT. */
12203 if (const_op == 0 && sign_bit_comparison_p
12204 && CONST_INT_P (XEXP (op0, 1))
12205 && mode_width <= HOST_BITS_PER_WIDE_INT)
12207 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12208 (HOST_WIDE_INT_1U
12209 << (mode_width - 1
12210 - INTVAL (XEXP (op0, 1)))));
12211 code = (code == LT ? NE : EQ);
12212 continue;
12215 /* Fall through. */
12217 case ABS:
12218 /* ABS is ignorable inside an equality comparison with zero. */
12219 if (const_op == 0 && equality_comparison_p)
12221 op0 = XEXP (op0, 0);
12222 continue;
12224 break;
12226 case SIGN_EXTEND:
12227 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12228 (compare FOO CONST) if CONST fits in FOO's mode and we
12229 are either testing inequality or have an unsigned
12230 comparison with ZERO_EXTEND or a signed comparison with
12231 SIGN_EXTEND. But don't do it if we don't have a compare
12232 insn of the given mode, since we'd have to revert it
12233 later on, and then we wouldn't know whether to sign- or
12234 zero-extend. */
12235 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12236 && ! unsigned_comparison_p
12237 && HWI_COMPUTABLE_MODE_P (mode)
12238 && trunc_int_for_mode (const_op, mode) == const_op
12239 && have_insn_for (COMPARE, mode))
12241 op0 = XEXP (op0, 0);
12242 continue;
12244 break;
12246 case SUBREG:
12247 /* Check for the case where we are comparing A - C1 with C2, that is
12249 (subreg:MODE (plus (A) (-C1))) op (C2)
12251 with C1 a constant, and try to lift the SUBREG, i.e. to do the
12252 comparison in the wider mode. One of the following two conditions
12253 must be true in order for this to be valid:
12255 1. The mode extension results in the same bit pattern being added
12256 on both sides and the comparison is equality or unsigned. As
12257 C2 has been truncated to fit in MODE, the pattern can only be
12258 all 0s or all 1s.
12260 2. The mode extension results in the sign bit being copied on
12261 each side.
12263 The difficulty here is that we have predicates for A but not for
12264 (A - C1) so we need to check that C1 is within proper bounds so
12265 as to perturbate A as little as possible. */
12267 if (mode_width <= HOST_BITS_PER_WIDE_INT
12268 && subreg_lowpart_p (op0)
12269 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12270 &inner_mode)
12271 && GET_MODE_PRECISION (inner_mode) > mode_width
12272 && GET_CODE (SUBREG_REG (op0)) == PLUS
12273 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12275 rtx a = XEXP (SUBREG_REG (op0), 0);
12276 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12278 if ((c1 > 0
12279 && (unsigned HOST_WIDE_INT) c1
12280 < HOST_WIDE_INT_1U << (mode_width - 1)
12281 && (equality_comparison_p || unsigned_comparison_p)
12282 /* (A - C1) zero-extends if it is positive and sign-extends
12283 if it is negative, C2 both zero- and sign-extends. */
12284 && ((0 == (nonzero_bits (a, inner_mode)
12285 & ~GET_MODE_MASK (mode))
12286 && const_op >= 0)
12287 /* (A - C1) sign-extends if it is positive and 1-extends
12288 if it is negative, C2 both sign- and 1-extends. */
12289 || (num_sign_bit_copies (a, inner_mode)
12290 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12291 - mode_width)
12292 && const_op < 0)))
12293 || ((unsigned HOST_WIDE_INT) c1
12294 < HOST_WIDE_INT_1U << (mode_width - 2)
12295 /* (A - C1) always sign-extends, like C2. */
12296 && num_sign_bit_copies (a, inner_mode)
12297 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12298 - (mode_width - 1))))
12300 op0 = SUBREG_REG (op0);
12301 continue;
12305 /* If the inner mode is narrower and we are extracting the low part,
12306 we can treat the SUBREG as if it were a ZERO_EXTEND. */
12307 if (paradoxical_subreg_p (op0))
12309 else if (subreg_lowpart_p (op0)
12310 && GET_MODE_CLASS (mode) == MODE_INT
12311 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12312 && (code == NE || code == EQ)
12313 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12314 && !paradoxical_subreg_p (op0)
12315 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12316 & ~GET_MODE_MASK (mode)) == 0)
12318 /* Remove outer subregs that don't do anything. */
12319 tem = gen_lowpart (inner_mode, op1);
12321 if ((nonzero_bits (tem, inner_mode)
12322 & ~GET_MODE_MASK (mode)) == 0)
12324 op0 = SUBREG_REG (op0);
12325 op1 = tem;
12326 continue;
12328 break;
12330 else
12331 break;
12333 /* FALLTHROUGH */
12335 case ZERO_EXTEND:
12336 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12337 && (unsigned_comparison_p || equality_comparison_p)
12338 && HWI_COMPUTABLE_MODE_P (mode)
12339 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12340 && const_op >= 0
12341 && have_insn_for (COMPARE, mode))
12343 op0 = XEXP (op0, 0);
12344 continue;
12346 break;
12348 case PLUS:
12349 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12350 this for equality comparisons due to pathological cases involving
12351 overflows. */
12352 if (equality_comparison_p
12353 && 0 != (tem = simplify_binary_operation (MINUS, mode,
12354 op1, XEXP (op0, 1))))
12356 op0 = XEXP (op0, 0);
12357 op1 = tem;
12358 continue;
12361 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12362 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12363 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12365 op0 = XEXP (XEXP (op0, 0), 0);
12366 code = (code == LT ? EQ : NE);
12367 continue;
12369 break;
12371 case MINUS:
12372 /* We used to optimize signed comparisons against zero, but that
12373 was incorrect. Unsigned comparisons against zero (GTU, LEU)
12374 arrive here as equality comparisons, or (GEU, LTU) are
12375 optimized away. No need to special-case them. */
12377 /* (eq (minus A B) C) -> (eq A (plus B C)) or
12378 (eq B (minus A C)), whichever simplifies. We can only do
12379 this for equality comparisons due to pathological cases involving
12380 overflows. */
12381 if (equality_comparison_p
12382 && 0 != (tem = simplify_binary_operation (PLUS, mode,
12383 XEXP (op0, 1), op1)))
12385 op0 = XEXP (op0, 0);
12386 op1 = tem;
12387 continue;
12390 if (equality_comparison_p
12391 && 0 != (tem = simplify_binary_operation (MINUS, mode,
12392 XEXP (op0, 0), op1)))
12394 op0 = XEXP (op0, 1);
12395 op1 = tem;
12396 continue;
12399 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12400 of bits in X minus 1, is one iff X > 0. */
12401 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12402 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12403 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12404 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12406 op0 = XEXP (op0, 1);
12407 code = (code == GE ? LE : GT);
12408 continue;
12410 break;
12412 case XOR:
12413 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12414 if C is zero or B is a constant. */
12415 if (equality_comparison_p
12416 && 0 != (tem = simplify_binary_operation (XOR, mode,
12417 XEXP (op0, 1), op1)))
12419 op0 = XEXP (op0, 0);
12420 op1 = tem;
12421 continue;
12423 break;
12426 case IOR:
12427 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12428 iff X <= 0. */
12429 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12430 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12431 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12433 op0 = XEXP (op0, 1);
12434 code = (code == GE ? GT : LE);
12435 continue;
12437 break;
12439 case AND:
12440 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12441 will be converted to a ZERO_EXTRACT later. */
12442 if (const_op == 0 && equality_comparison_p
12443 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12444 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12446 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12447 XEXP (XEXP (op0, 0), 1));
12448 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12449 continue;
12452 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12453 zero and X is a comparison and C1 and C2 describe only bits set
12454 in STORE_FLAG_VALUE, we can compare with X. */
12455 if (const_op == 0 && equality_comparison_p
12456 && mode_width <= HOST_BITS_PER_WIDE_INT
12457 && CONST_INT_P (XEXP (op0, 1))
12458 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12459 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12460 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12461 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12463 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12464 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12465 if ((~STORE_FLAG_VALUE & mask) == 0
12466 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12467 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12468 && COMPARISON_P (tem))))
12470 op0 = XEXP (XEXP (op0, 0), 0);
12471 continue;
12475 /* If we are doing an equality comparison of an AND of a bit equal
12476 to the sign bit, replace this with a LT or GE comparison of
12477 the underlying value. */
12478 if (equality_comparison_p
12479 && const_op == 0
12480 && CONST_INT_P (XEXP (op0, 1))
12481 && mode_width <= HOST_BITS_PER_WIDE_INT
12482 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12483 == HOST_WIDE_INT_1U << (mode_width - 1)))
12485 op0 = XEXP (op0, 0);
12486 code = (code == EQ ? GE : LT);
12487 continue;
12490 /* If this AND operation is really a ZERO_EXTEND from a narrower
12491 mode, the constant fits within that mode, and this is either an
12492 equality or unsigned comparison, try to do this comparison in
12493 the narrower mode.
12495 Note that in:
12497 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12498 -> (ne:DI (reg:SI 4) (const_int 0))
12500 unless TRULY_NOOP_TRUNCATION allows it or the register is
12501 known to hold a value of the required mode the
12502 transformation is invalid. */
12503 if ((equality_comparison_p || unsigned_comparison_p)
12504 && CONST_INT_P (XEXP (op0, 1))
12505 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12506 & GET_MODE_MASK (mode))
12507 + 1)) >= 0
12508 && const_op >> i == 0
12509 && int_mode_for_size (i, 1).exists (&tmode))
12511 op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12512 continue;
12515 /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12516 fits in both M1 and M2 and the SUBREG is either paradoxical
12517 or represents the low part, permute the SUBREG and the AND
12518 and try again. */
12519 if (GET_CODE (XEXP (op0, 0)) == SUBREG
12520 && CONST_INT_P (XEXP (op0, 1)))
12522 unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12523 /* Require an integral mode, to avoid creating something like
12524 (AND:SF ...). */
12525 if ((is_a <scalar_int_mode>
12526 (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12527 /* It is unsafe to commute the AND into the SUBREG if the
12528 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12529 not defined. As originally written the upper bits
12530 have a defined value due to the AND operation.
12531 However, if we commute the AND inside the SUBREG then
12532 they no longer have defined values and the meaning of
12533 the code has been changed.
12534 Also C1 should not change value in the smaller mode,
12535 see PR67028 (a positive C1 can become negative in the
12536 smaller mode, so that the AND does no longer mask the
12537 upper bits). */
12538 && ((WORD_REGISTER_OPERATIONS
12539 && mode_width > GET_MODE_PRECISION (tmode)
12540 && mode_width <= BITS_PER_WORD
12541 && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12542 || (mode_width <= GET_MODE_PRECISION (tmode)
12543 && subreg_lowpart_p (XEXP (op0, 0))))
12544 && mode_width <= HOST_BITS_PER_WIDE_INT
12545 && HWI_COMPUTABLE_MODE_P (tmode)
12546 && (c1 & ~mask) == 0
12547 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12548 && c1 != mask
12549 && c1 != GET_MODE_MASK (tmode))
12551 op0 = simplify_gen_binary (AND, tmode,
12552 SUBREG_REG (XEXP (op0, 0)),
12553 gen_int_mode (c1, tmode));
12554 op0 = gen_lowpart (mode, op0);
12555 continue;
12559 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12560 if (const_op == 0 && equality_comparison_p
12561 && XEXP (op0, 1) == const1_rtx
12562 && GET_CODE (XEXP (op0, 0)) == NOT)
12564 op0 = simplify_and_const_int (NULL_RTX, mode,
12565 XEXP (XEXP (op0, 0), 0), 1);
12566 code = (code == NE ? EQ : NE);
12567 continue;
12570 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12571 (eq (and (lshiftrt X) 1) 0).
12572 Also handle the case where (not X) is expressed using xor. */
12573 if (const_op == 0 && equality_comparison_p
12574 && XEXP (op0, 1) == const1_rtx
12575 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12577 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12578 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12580 if (GET_CODE (shift_op) == NOT
12581 || (GET_CODE (shift_op) == XOR
12582 && CONST_INT_P (XEXP (shift_op, 1))
12583 && CONST_INT_P (shift_count)
12584 && HWI_COMPUTABLE_MODE_P (mode)
12585 && (UINTVAL (XEXP (shift_op, 1))
12586 == HOST_WIDE_INT_1U
12587 << INTVAL (shift_count))))
12590 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12591 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12592 code = (code == NE ? EQ : NE);
12593 continue;
12596 break;
12598 case ASHIFT:
12599 /* If we have (compare (ashift FOO N) (const_int C)) and
12600 the high order N bits of FOO (N+1 if an inequality comparison)
12601 are known to be zero, we can do this by comparing FOO with C
12602 shifted right N bits so long as the low-order N bits of C are
12603 zero. */
12604 if (CONST_INT_P (XEXP (op0, 1))
12605 && INTVAL (XEXP (op0, 1)) >= 0
12606 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12607 < HOST_BITS_PER_WIDE_INT)
12608 && (((unsigned HOST_WIDE_INT) const_op
12609 & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12610 - 1)) == 0)
12611 && mode_width <= HOST_BITS_PER_WIDE_INT
12612 && (nonzero_bits (XEXP (op0, 0), mode)
12613 & ~(mask >> (INTVAL (XEXP (op0, 1))
12614 + ! equality_comparison_p))) == 0)
12616 /* We must perform a logical shift, not an arithmetic one,
12617 as we want the top N bits of C to be zero. */
12618 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12620 temp >>= INTVAL (XEXP (op0, 1));
12621 op1 = gen_int_mode (temp, mode);
12622 op0 = XEXP (op0, 0);
12623 continue;
12626 /* If we are doing a sign bit comparison, it means we are testing
12627 a particular bit. Convert it to the appropriate AND. */
12628 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12629 && mode_width <= HOST_BITS_PER_WIDE_INT)
12631 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12632 (HOST_WIDE_INT_1U
12633 << (mode_width - 1
12634 - INTVAL (XEXP (op0, 1)))));
12635 code = (code == LT ? NE : EQ);
12636 continue;
12639 /* If this an equality comparison with zero and we are shifting
12640 the low bit to the sign bit, we can convert this to an AND of the
12641 low-order bit. */
12642 if (const_op == 0 && equality_comparison_p
12643 && CONST_INT_P (XEXP (op0, 1))
12644 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12646 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12647 continue;
12649 break;
12651 case ASHIFTRT:
12652 /* If this is an equality comparison with zero, we can do this
12653 as a logical shift, which might be much simpler. */
12654 if (equality_comparison_p && const_op == 0
12655 && CONST_INT_P (XEXP (op0, 1)))
12657 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12658 XEXP (op0, 0),
12659 INTVAL (XEXP (op0, 1)));
12660 continue;
12663 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12664 do the comparison in a narrower mode. */
12665 if (! unsigned_comparison_p
12666 && CONST_INT_P (XEXP (op0, 1))
12667 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12668 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12669 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12670 .exists (&tmode))
12671 && (((unsigned HOST_WIDE_INT) const_op
12672 + (GET_MODE_MASK (tmode) >> 1) + 1)
12673 <= GET_MODE_MASK (tmode)))
12675 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12676 continue;
12679 /* Likewise if OP0 is a PLUS of a sign extension with a
12680 constant, which is usually represented with the PLUS
12681 between the shifts. */
12682 if (! unsigned_comparison_p
12683 && CONST_INT_P (XEXP (op0, 1))
12684 && GET_CODE (XEXP (op0, 0)) == PLUS
12685 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12686 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12687 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12688 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12689 .exists (&tmode))
12690 && (((unsigned HOST_WIDE_INT) const_op
12691 + (GET_MODE_MASK (tmode) >> 1) + 1)
12692 <= GET_MODE_MASK (tmode)))
12694 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12695 rtx add_const = XEXP (XEXP (op0, 0), 1);
12696 rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12697 add_const, XEXP (op0, 1));
12699 op0 = simplify_gen_binary (PLUS, tmode,
12700 gen_lowpart (tmode, inner),
12701 new_const);
12702 continue;
12705 /* FALLTHROUGH */
12706 case LSHIFTRT:
12707 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12708 the low order N bits of FOO are known to be zero, we can do this
12709 by comparing FOO with C shifted left N bits so long as no
12710 overflow occurs. Even if the low order N bits of FOO aren't known
12711 to be zero, if the comparison is >= or < we can use the same
12712 optimization and for > or <= by setting all the low
12713 order N bits in the comparison constant. */
12714 if (CONST_INT_P (XEXP (op0, 1))
12715 && INTVAL (XEXP (op0, 1)) > 0
12716 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12717 && mode_width <= HOST_BITS_PER_WIDE_INT
12718 && (((unsigned HOST_WIDE_INT) const_op
12719 + (GET_CODE (op0) != LSHIFTRT
12720 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12721 + 1)
12722 : 0))
12723 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12725 unsigned HOST_WIDE_INT low_bits
12726 = (nonzero_bits (XEXP (op0, 0), mode)
12727 & ((HOST_WIDE_INT_1U
12728 << INTVAL (XEXP (op0, 1))) - 1));
12729 if (low_bits == 0 || !equality_comparison_p)
12731 /* If the shift was logical, then we must make the condition
12732 unsigned. */
12733 if (GET_CODE (op0) == LSHIFTRT)
12734 code = unsigned_condition (code);
12736 const_op = (unsigned HOST_WIDE_INT) const_op
12737 << INTVAL (XEXP (op0, 1));
12738 if (low_bits != 0
12739 && (code == GT || code == GTU
12740 || code == LE || code == LEU))
12741 const_op
12742 |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12743 op1 = GEN_INT (const_op);
12744 op0 = XEXP (op0, 0);
12745 continue;
12749 /* If we are using this shift to extract just the sign bit, we
12750 can replace this with an LT or GE comparison. */
12751 if (const_op == 0
12752 && (equality_comparison_p || sign_bit_comparison_p)
12753 && CONST_INT_P (XEXP (op0, 1))
12754 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12756 op0 = XEXP (op0, 0);
12757 code = (code == NE || code == GT ? LT : GE);
12758 continue;
12760 break;
12762 default:
12763 break;
12766 break;
12769 /* Now make any compound operations involved in this comparison. Then,
12770 check for an outmost SUBREG on OP0 that is not doing anything or is
12771 paradoxical. The latter transformation must only be performed when
12772 it is known that the "extra" bits will be the same in op0 and op1 or
12773 that they don't matter. There are three cases to consider:
12775 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12776 care bits and we can assume they have any convenient value. So
12777 making the transformation is safe.
12779 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12780 In this case the upper bits of op0 are undefined. We should not make
12781 the simplification in that case as we do not know the contents of
12782 those bits.
12784 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12785 In that case we know those bits are zeros or ones. We must also be
12786 sure that they are the same as the upper bits of op1.
12788 We can never remove a SUBREG for a non-equality comparison because
12789 the sign bit is in a different place in the underlying object. */
12791 rtx_code op0_mco_code = SET;
12792 if (op1 == const0_rtx)
12793 op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12795 op0 = make_compound_operation (op0, op0_mco_code);
12796 op1 = make_compound_operation (op1, SET);
12798 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12799 && is_int_mode (GET_MODE (op0), &mode)
12800 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12801 && (code == NE || code == EQ))
12803 if (paradoxical_subreg_p (op0))
12805 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12806 implemented. */
12807 if (REG_P (SUBREG_REG (op0)))
12809 op0 = SUBREG_REG (op0);
12810 op1 = gen_lowpart (inner_mode, op1);
12813 else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12814 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12815 & ~GET_MODE_MASK (mode)) == 0)
12817 tem = gen_lowpart (inner_mode, op1);
12819 if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
12820 op0 = SUBREG_REG (op0), op1 = tem;
12824 /* We now do the opposite procedure: Some machines don't have compare
12825 insns in all modes. If OP0's mode is an integer mode smaller than a
12826 word and we can't do a compare in that mode, see if there is a larger
12827 mode for which we can do the compare. There are a number of cases in
12828 which we can use the wider mode. */
12830 if (is_int_mode (GET_MODE (op0), &mode)
12831 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12832 && ! have_insn_for (COMPARE, mode))
12833 FOR_EACH_WIDER_MODE (tmode_iter, mode)
12835 tmode = tmode_iter.require ();
12836 if (!HWI_COMPUTABLE_MODE_P (tmode))
12837 break;
12838 if (have_insn_for (COMPARE, tmode))
12840 int zero_extended;
12842 /* If this is a test for negative, we can make an explicit
12843 test of the sign bit. Test this first so we can use
12844 a paradoxical subreg to extend OP0. */
12846 if (op1 == const0_rtx && (code == LT || code == GE)
12847 && HWI_COMPUTABLE_MODE_P (mode))
12849 unsigned HOST_WIDE_INT sign
12850 = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
12851 op0 = simplify_gen_binary (AND, tmode,
12852 gen_lowpart (tmode, op0),
12853 gen_int_mode (sign, tmode));
12854 code = (code == LT) ? NE : EQ;
12855 break;
12858 /* If the only nonzero bits in OP0 and OP1 are those in the
12859 narrower mode and this is an equality or unsigned comparison,
12860 we can use the wider mode. Similarly for sign-extended
12861 values, in which case it is true for all comparisons. */
12862 zero_extended = ((code == EQ || code == NE
12863 || code == GEU || code == GTU
12864 || code == LEU || code == LTU)
12865 && (nonzero_bits (op0, tmode)
12866 & ~GET_MODE_MASK (mode)) == 0
12867 && ((CONST_INT_P (op1)
12868 || (nonzero_bits (op1, tmode)
12869 & ~GET_MODE_MASK (mode)) == 0)));
12871 if (zero_extended
12872 || ((num_sign_bit_copies (op0, tmode)
12873 > (unsigned int) (GET_MODE_PRECISION (tmode)
12874 - GET_MODE_PRECISION (mode)))
12875 && (num_sign_bit_copies (op1, tmode)
12876 > (unsigned int) (GET_MODE_PRECISION (tmode)
12877 - GET_MODE_PRECISION (mode)))))
12879 /* If OP0 is an AND and we don't have an AND in MODE either,
12880 make a new AND in the proper mode. */
12881 if (GET_CODE (op0) == AND
12882 && !have_insn_for (AND, mode))
12883 op0 = simplify_gen_binary (AND, tmode,
12884 gen_lowpart (tmode,
12885 XEXP (op0, 0)),
12886 gen_lowpart (tmode,
12887 XEXP (op0, 1)));
12888 else
12890 if (zero_extended)
12892 op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
12893 op0, mode);
12894 op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
12895 op1, mode);
12897 else
12899 op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
12900 op0, mode);
12901 op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
12902 op1, mode);
12904 break;
12910 /* We may have changed the comparison operands. Re-canonicalize. */
12911 if (swap_commutative_operands_p (op0, op1))
12913 std::swap (op0, op1);
12914 code = swap_condition (code);
12917 /* If this machine only supports a subset of valid comparisons, see if we
12918 can convert an unsupported one into a supported one. */
12919 target_canonicalize_comparison (&code, &op0, &op1, 0);
12921 *pop0 = op0;
12922 *pop1 = op1;
12924 return code;
12927 /* Utility function for record_value_for_reg. Count number of
12928 rtxs in X. */
12929 static int
12930 count_rtxs (rtx x)
12932 enum rtx_code code = GET_CODE (x);
12933 const char *fmt;
12934 int i, j, ret = 1;
12936 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12937 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12939 rtx x0 = XEXP (x, 0);
12940 rtx x1 = XEXP (x, 1);
12942 if (x0 == x1)
12943 return 1 + 2 * count_rtxs (x0);
12945 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12946 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12947 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12948 return 2 + 2 * count_rtxs (x0)
12949 + count_rtxs (x == XEXP (x1, 0)
12950 ? XEXP (x1, 1) : XEXP (x1, 0));
12952 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12953 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12954 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12955 return 2 + 2 * count_rtxs (x1)
12956 + count_rtxs (x == XEXP (x0, 0)
12957 ? XEXP (x0, 1) : XEXP (x0, 0));
12960 fmt = GET_RTX_FORMAT (code);
12961 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12962 if (fmt[i] == 'e')
12963 ret += count_rtxs (XEXP (x, i));
12964 else if (fmt[i] == 'E')
12965 for (j = 0; j < XVECLEN (x, i); j++)
12966 ret += count_rtxs (XVECEXP (x, i, j));
12968 return ret;
12971 /* Utility function for following routine. Called when X is part of a value
12972 being stored into last_set_value. Sets last_set_table_tick
12973 for each register mentioned. Similar to mention_regs in cse.c */
12975 static void
12976 update_table_tick (rtx x)
12978 enum rtx_code code = GET_CODE (x);
12979 const char *fmt = GET_RTX_FORMAT (code);
12980 int i, j;
12982 if (code == REG)
12984 unsigned int regno = REGNO (x);
12985 unsigned int endregno = END_REGNO (x);
12986 unsigned int r;
12988 for (r = regno; r < endregno; r++)
12990 reg_stat_type *rsp = &reg_stat[r];
12991 rsp->last_set_table_tick = label_tick;
12994 return;
12997 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12998 if (fmt[i] == 'e')
13000 /* Check for identical subexpressions. If x contains
13001 identical subexpression we only have to traverse one of
13002 them. */
13003 if (i == 0 && ARITHMETIC_P (x))
13005 /* Note that at this point x1 has already been
13006 processed. */
13007 rtx x0 = XEXP (x, 0);
13008 rtx x1 = XEXP (x, 1);
13010 /* If x0 and x1 are identical then there is no need to
13011 process x0. */
13012 if (x0 == x1)
13013 break;
13015 /* If x0 is identical to a subexpression of x1 then while
13016 processing x1, x0 has already been processed. Thus we
13017 are done with x. */
13018 if (ARITHMETIC_P (x1)
13019 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13020 break;
13022 /* If x1 is identical to a subexpression of x0 then we
13023 still have to process the rest of x0. */
13024 if (ARITHMETIC_P (x0)
13025 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13027 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13028 break;
13032 update_table_tick (XEXP (x, i));
13034 else if (fmt[i] == 'E')
13035 for (j = 0; j < XVECLEN (x, i); j++)
13036 update_table_tick (XVECEXP (x, i, j));
13039 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13040 are saying that the register is clobbered and we no longer know its
13041 value. If INSN is zero, don't update reg_stat[].last_set; this is
13042 only permitted with VALUE also zero and is used to invalidate the
13043 register. */
13045 static void
13046 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13048 unsigned int regno = REGNO (reg);
13049 unsigned int endregno = END_REGNO (reg);
13050 unsigned int i;
13051 reg_stat_type *rsp;
13053 /* If VALUE contains REG and we have a previous value for REG, substitute
13054 the previous value. */
13055 if (value && insn && reg_overlap_mentioned_p (reg, value))
13057 rtx tem;
13059 /* Set things up so get_last_value is allowed to see anything set up to
13060 our insn. */
13061 subst_low_luid = DF_INSN_LUID (insn);
13062 tem = get_last_value (reg);
13064 /* If TEM is simply a binary operation with two CLOBBERs as operands,
13065 it isn't going to be useful and will take a lot of time to process,
13066 so just use the CLOBBER. */
13068 if (tem)
13070 if (ARITHMETIC_P (tem)
13071 && GET_CODE (XEXP (tem, 0)) == CLOBBER
13072 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13073 tem = XEXP (tem, 0);
13074 else if (count_occurrences (value, reg, 1) >= 2)
13076 /* If there are two or more occurrences of REG in VALUE,
13077 prevent the value from growing too much. */
13078 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
13079 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13082 value = replace_rtx (copy_rtx (value), reg, tem);
13086 /* For each register modified, show we don't know its value, that
13087 we don't know about its bitwise content, that its value has been
13088 updated, and that we don't know the location of the death of the
13089 register. */
13090 for (i = regno; i < endregno; i++)
13092 rsp = &reg_stat[i];
13094 if (insn)
13095 rsp->last_set = insn;
13097 rsp->last_set_value = 0;
13098 rsp->last_set_mode = VOIDmode;
13099 rsp->last_set_nonzero_bits = 0;
13100 rsp->last_set_sign_bit_copies = 0;
13101 rsp->last_death = 0;
13102 rsp->truncated_to_mode = VOIDmode;
13105 /* Mark registers that are being referenced in this value. */
13106 if (value)
13107 update_table_tick (value);
13109 /* Now update the status of each register being set.
13110 If someone is using this register in this block, set this register
13111 to invalid since we will get confused between the two lives in this
13112 basic block. This makes using this register always invalid. In cse, we
13113 scan the table to invalidate all entries using this register, but this
13114 is too much work for us. */
13116 for (i = regno; i < endregno; i++)
13118 rsp = &reg_stat[i];
13119 rsp->last_set_label = label_tick;
13120 if (!insn
13121 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13122 rsp->last_set_invalid = 1;
13123 else
13124 rsp->last_set_invalid = 0;
13127 /* The value being assigned might refer to X (like in "x++;"). In that
13128 case, we must replace it with (clobber (const_int 0)) to prevent
13129 infinite loops. */
13130 rsp = &reg_stat[regno];
13131 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
13133 value = copy_rtx (value);
13134 if (!get_last_value_validate (&value, insn, label_tick, 1))
13135 value = 0;
13138 /* For the main register being modified, update the value, the mode, the
13139 nonzero bits, and the number of sign bit copies. */
13141 rsp->last_set_value = value;
13143 if (value)
13145 machine_mode mode = GET_MODE (reg);
13146 subst_low_luid = DF_INSN_LUID (insn);
13147 rsp->last_set_mode = mode;
13148 if (GET_MODE_CLASS (mode) == MODE_INT
13149 && HWI_COMPUTABLE_MODE_P (mode))
13150 mode = nonzero_bits_mode;
13151 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13152 rsp->last_set_sign_bit_copies
13153 = num_sign_bit_copies (value, GET_MODE (reg));
13157 /* Called via note_stores from record_dead_and_set_regs to handle one
13158 SET or CLOBBER in an insn. DATA is the instruction in which the
13159 set is occurring. */
13161 static void
13162 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13164 rtx_insn *record_dead_insn = (rtx_insn *) data;
13166 if (GET_CODE (dest) == SUBREG)
13167 dest = SUBREG_REG (dest);
13169 if (!record_dead_insn)
13171 if (REG_P (dest))
13172 record_value_for_reg (dest, NULL, NULL_RTX);
13173 return;
13176 if (REG_P (dest))
13178 /* If we are setting the whole register, we know its value. Otherwise
13179 show that we don't know the value. We can handle SUBREG in
13180 some cases. */
13181 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13182 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13183 else if (GET_CODE (setter) == SET
13184 && GET_CODE (SET_DEST (setter)) == SUBREG
13185 && SUBREG_REG (SET_DEST (setter)) == dest
13186 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
13187 && subreg_lowpart_p (SET_DEST (setter)))
13188 record_value_for_reg (dest, record_dead_insn,
13189 gen_lowpart (GET_MODE (dest),
13190 SET_SRC (setter)));
13191 else
13192 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13194 else if (MEM_P (dest)
13195 /* Ignore pushes, they clobber nothing. */
13196 && ! push_operand (dest, GET_MODE (dest)))
13197 mem_last_set = DF_INSN_LUID (record_dead_insn);
13200 /* Update the records of when each REG was most recently set or killed
13201 for the things done by INSN. This is the last thing done in processing
13202 INSN in the combiner loop.
13204 We update reg_stat[], in particular fields last_set, last_set_value,
13205 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13206 last_death, and also the similar information mem_last_set (which insn
13207 most recently modified memory) and last_call_luid (which insn was the
13208 most recent subroutine call). */
13210 static void
13211 record_dead_and_set_regs (rtx_insn *insn)
13213 rtx link;
13214 unsigned int i;
13216 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13218 if (REG_NOTE_KIND (link) == REG_DEAD
13219 && REG_P (XEXP (link, 0)))
13221 unsigned int regno = REGNO (XEXP (link, 0));
13222 unsigned int endregno = END_REGNO (XEXP (link, 0));
13224 for (i = regno; i < endregno; i++)
13226 reg_stat_type *rsp;
13228 rsp = &reg_stat[i];
13229 rsp->last_death = insn;
13232 else if (REG_NOTE_KIND (link) == REG_INC)
13233 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13236 if (CALL_P (insn))
13238 hard_reg_set_iterator hrsi;
13239 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
13241 reg_stat_type *rsp;
13243 rsp = &reg_stat[i];
13244 rsp->last_set_invalid = 1;
13245 rsp->last_set = insn;
13246 rsp->last_set_value = 0;
13247 rsp->last_set_mode = VOIDmode;
13248 rsp->last_set_nonzero_bits = 0;
13249 rsp->last_set_sign_bit_copies = 0;
13250 rsp->last_death = 0;
13251 rsp->truncated_to_mode = VOIDmode;
13254 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13256 /* We can't combine into a call pattern. Remember, though, that
13257 the return value register is set at this LUID. We could
13258 still replace a register with the return value from the
13259 wrong subroutine call! */
13260 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
13262 else
13263 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
13266 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13267 register present in the SUBREG, so for each such SUBREG go back and
13268 adjust nonzero and sign bit information of the registers that are
13269 known to have some zero/sign bits set.
13271 This is needed because when combine blows the SUBREGs away, the
13272 information on zero/sign bits is lost and further combines can be
13273 missed because of that. */
13275 static void
13276 record_promoted_value (rtx_insn *insn, rtx subreg)
13278 struct insn_link *links;
13279 rtx set;
13280 unsigned int regno = REGNO (SUBREG_REG (subreg));
13281 machine_mode mode = GET_MODE (subreg);
13283 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
13284 return;
13286 for (links = LOG_LINKS (insn); links;)
13288 reg_stat_type *rsp;
13290 insn = links->insn;
13291 set = single_set (insn);
13293 if (! set || !REG_P (SET_DEST (set))
13294 || REGNO (SET_DEST (set)) != regno
13295 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13297 links = links->next;
13298 continue;
13301 rsp = &reg_stat[regno];
13302 if (rsp->last_set == insn)
13304 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13305 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13308 if (REG_P (SET_SRC (set)))
13310 regno = REGNO (SET_SRC (set));
13311 links = LOG_LINKS (insn);
13313 else
13314 break;
13318 /* Check if X, a register, is known to contain a value already
13319 truncated to MODE. In this case we can use a subreg to refer to
13320 the truncated value even though in the generic case we would need
13321 an explicit truncation. */
13323 static bool
13324 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13326 reg_stat_type *rsp = &reg_stat[REGNO (x)];
13327 machine_mode truncated = rsp->truncated_to_mode;
13329 if (truncated == 0
13330 || rsp->truncation_label < label_tick_ebb_start)
13331 return false;
13332 if (!partial_subreg_p (mode, truncated))
13333 return true;
13334 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13335 return true;
13336 return false;
13339 /* If X is a hard reg or a subreg record the mode that the register is
13340 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
13341 to turn a truncate into a subreg using this information. Return true
13342 if traversing X is complete. */
13344 static bool
13345 record_truncated_value (rtx x)
13347 machine_mode truncated_mode;
13348 reg_stat_type *rsp;
13350 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13352 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13353 truncated_mode = GET_MODE (x);
13355 if (!partial_subreg_p (truncated_mode, original_mode))
13356 return true;
13358 truncated_mode = GET_MODE (x);
13359 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13360 return true;
13362 x = SUBREG_REG (x);
13364 /* ??? For hard-regs we now record everything. We might be able to
13365 optimize this using last_set_mode. */
13366 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13367 truncated_mode = GET_MODE (x);
13368 else
13369 return false;
13371 rsp = &reg_stat[REGNO (x)];
13372 if (rsp->truncated_to_mode == 0
13373 || rsp->truncation_label < label_tick_ebb_start
13374 || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13376 rsp->truncated_to_mode = truncated_mode;
13377 rsp->truncation_label = label_tick;
13380 return true;
13383 /* Callback for note_uses. Find hardregs and subregs of pseudos and
13384 the modes they are used in. This can help truning TRUNCATEs into
13385 SUBREGs. */
13387 static void
13388 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13390 subrtx_var_iterator::array_type array;
13391 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13392 if (record_truncated_value (*iter))
13393 iter.skip_subrtxes ();
13396 /* Scan X for promoted SUBREGs. For each one found,
13397 note what it implies to the registers used in it. */
13399 static void
13400 check_promoted_subreg (rtx_insn *insn, rtx x)
13402 if (GET_CODE (x) == SUBREG
13403 && SUBREG_PROMOTED_VAR_P (x)
13404 && REG_P (SUBREG_REG (x)))
13405 record_promoted_value (insn, x);
13406 else
13408 const char *format = GET_RTX_FORMAT (GET_CODE (x));
13409 int i, j;
13411 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13412 switch (format[i])
13414 case 'e':
13415 check_promoted_subreg (insn, XEXP (x, i));
13416 break;
13417 case 'V':
13418 case 'E':
13419 if (XVEC (x, i) != 0)
13420 for (j = 0; j < XVECLEN (x, i); j++)
13421 check_promoted_subreg (insn, XVECEXP (x, i, j));
13422 break;
13427 /* Verify that all the registers and memory references mentioned in *LOC are
13428 still valid. *LOC was part of a value set in INSN when label_tick was
13429 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
13430 the invalid references with (clobber (const_int 0)) and return 1. This
13431 replacement is useful because we often can get useful information about
13432 the form of a value (e.g., if it was produced by a shift that always
13433 produces -1 or 0) even though we don't know exactly what registers it
13434 was produced from. */
13436 static int
13437 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
13439 rtx x = *loc;
13440 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13441 int len = GET_RTX_LENGTH (GET_CODE (x));
13442 int i, j;
13444 if (REG_P (x))
13446 unsigned int regno = REGNO (x);
13447 unsigned int endregno = END_REGNO (x);
13448 unsigned int j;
13450 for (j = regno; j < endregno; j++)
13452 reg_stat_type *rsp = &reg_stat[j];
13453 if (rsp->last_set_invalid
13454 /* If this is a pseudo-register that was only set once and not
13455 live at the beginning of the function, it is always valid. */
13456 || (! (regno >= FIRST_PSEUDO_REGISTER
13457 && regno < reg_n_sets_max
13458 && REG_N_SETS (regno) == 1
13459 && (!REGNO_REG_SET_P
13460 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13461 regno)))
13462 && rsp->last_set_label > tick))
13464 if (replace)
13465 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13466 return replace;
13470 return 1;
13472 /* If this is a memory reference, make sure that there were no stores after
13473 it that might have clobbered the value. We don't have alias info, so we
13474 assume any store invalidates it. Moreover, we only have local UIDs, so
13475 we also assume that there were stores in the intervening basic blocks. */
13476 else if (MEM_P (x) && !MEM_READONLY_P (x)
13477 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13479 if (replace)
13480 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13481 return replace;
13484 for (i = 0; i < len; i++)
13486 if (fmt[i] == 'e')
13488 /* Check for identical subexpressions. If x contains
13489 identical subexpression we only have to traverse one of
13490 them. */
13491 if (i == 1 && ARITHMETIC_P (x))
13493 /* Note that at this point x0 has already been checked
13494 and found valid. */
13495 rtx x0 = XEXP (x, 0);
13496 rtx x1 = XEXP (x, 1);
13498 /* If x0 and x1 are identical then x is also valid. */
13499 if (x0 == x1)
13500 return 1;
13502 /* If x1 is identical to a subexpression of x0 then
13503 while checking x0, x1 has already been checked. Thus
13504 it is valid and so as x. */
13505 if (ARITHMETIC_P (x0)
13506 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13507 return 1;
13509 /* If x0 is identical to a subexpression of x1 then x is
13510 valid iff the rest of x1 is valid. */
13511 if (ARITHMETIC_P (x1)
13512 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13513 return
13514 get_last_value_validate (&XEXP (x1,
13515 x0 == XEXP (x1, 0) ? 1 : 0),
13516 insn, tick, replace);
13519 if (get_last_value_validate (&XEXP (x, i), insn, tick,
13520 replace) == 0)
13521 return 0;
13523 else if (fmt[i] == 'E')
13524 for (j = 0; j < XVECLEN (x, i); j++)
13525 if (get_last_value_validate (&XVECEXP (x, i, j),
13526 insn, tick, replace) == 0)
13527 return 0;
13530 /* If we haven't found a reason for it to be invalid, it is valid. */
13531 return 1;
13534 /* Get the last value assigned to X, if known. Some registers
13535 in the value may be replaced with (clobber (const_int 0)) if their value
13536 is known longer known reliably. */
13538 static rtx
13539 get_last_value (const_rtx x)
13541 unsigned int regno;
13542 rtx value;
13543 reg_stat_type *rsp;
13545 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13546 then convert it to the desired mode. If this is a paradoxical SUBREG,
13547 we cannot predict what values the "extra" bits might have. */
13548 if (GET_CODE (x) == SUBREG
13549 && subreg_lowpart_p (x)
13550 && !paradoxical_subreg_p (x)
13551 && (value = get_last_value (SUBREG_REG (x))) != 0)
13552 return gen_lowpart (GET_MODE (x), value);
13554 if (!REG_P (x))
13555 return 0;
13557 regno = REGNO (x);
13558 rsp = &reg_stat[regno];
13559 value = rsp->last_set_value;
13561 /* If we don't have a value, or if it isn't for this basic block and
13562 it's either a hard register, set more than once, or it's a live
13563 at the beginning of the function, return 0.
13565 Because if it's not live at the beginning of the function then the reg
13566 is always set before being used (is never used without being set).
13567 And, if it's set only once, and it's always set before use, then all
13568 uses must have the same last value, even if it's not from this basic
13569 block. */
13571 if (value == 0
13572 || (rsp->last_set_label < label_tick_ebb_start
13573 && (regno < FIRST_PSEUDO_REGISTER
13574 || regno >= reg_n_sets_max
13575 || REG_N_SETS (regno) != 1
13576 || REGNO_REG_SET_P
13577 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13578 return 0;
13580 /* If the value was set in a later insn than the ones we are processing,
13581 we can't use it even if the register was only set once. */
13582 if (rsp->last_set_label == label_tick
13583 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13584 return 0;
13586 /* If fewer bits were set than what we are asked for now, we cannot use
13587 the value. */
13588 if (GET_MODE_PRECISION (rsp->last_set_mode)
13589 < GET_MODE_PRECISION (GET_MODE (x)))
13590 return 0;
13592 /* If the value has all its registers valid, return it. */
13593 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13594 return value;
13596 /* Otherwise, make a copy and replace any invalid register with
13597 (clobber (const_int 0)). If that fails for some reason, return 0. */
13599 value = copy_rtx (value);
13600 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13601 return value;
13603 return 0;
13606 /* Return nonzero if expression X refers to a REG or to memory
13607 that is set in an instruction more recent than FROM_LUID. */
13609 static int
13610 use_crosses_set_p (const_rtx x, int from_luid)
13612 const char *fmt;
13613 int i;
13614 enum rtx_code code = GET_CODE (x);
13616 if (code == REG)
13618 unsigned int regno = REGNO (x);
13619 unsigned endreg = END_REGNO (x);
13621 #ifdef PUSH_ROUNDING
13622 /* Don't allow uses of the stack pointer to be moved,
13623 because we don't know whether the move crosses a push insn. */
13624 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13625 return 1;
13626 #endif
13627 for (; regno < endreg; regno++)
13629 reg_stat_type *rsp = &reg_stat[regno];
13630 if (rsp->last_set
13631 && rsp->last_set_label == label_tick
13632 && DF_INSN_LUID (rsp->last_set) > from_luid)
13633 return 1;
13635 return 0;
13638 if (code == MEM && mem_last_set > from_luid)
13639 return 1;
13641 fmt = GET_RTX_FORMAT (code);
13643 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13645 if (fmt[i] == 'E')
13647 int j;
13648 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13649 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13650 return 1;
13652 else if (fmt[i] == 'e'
13653 && use_crosses_set_p (XEXP (x, i), from_luid))
13654 return 1;
13656 return 0;
13659 /* Define three variables used for communication between the following
13660 routines. */
13662 static unsigned int reg_dead_regno, reg_dead_endregno;
13663 static int reg_dead_flag;
13665 /* Function called via note_stores from reg_dead_at_p.
13667 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13668 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13670 static void
13671 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13673 unsigned int regno, endregno;
13675 if (!REG_P (dest))
13676 return;
13678 regno = REGNO (dest);
13679 endregno = END_REGNO (dest);
13680 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13681 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13684 /* Return nonzero if REG is known to be dead at INSN.
13686 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13687 referencing REG, it is dead. If we hit a SET referencing REG, it is
13688 live. Otherwise, see if it is live or dead at the start of the basic
13689 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13690 must be assumed to be always live. */
13692 static int
13693 reg_dead_at_p (rtx reg, rtx_insn *insn)
13695 basic_block block;
13696 unsigned int i;
13698 /* Set variables for reg_dead_at_p_1. */
13699 reg_dead_regno = REGNO (reg);
13700 reg_dead_endregno = END_REGNO (reg);
13702 reg_dead_flag = 0;
13704 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13705 we allow the machine description to decide whether use-and-clobber
13706 patterns are OK. */
13707 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13709 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13710 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13711 return 0;
13714 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13715 beginning of basic block. */
13716 block = BLOCK_FOR_INSN (insn);
13717 for (;;)
13719 if (INSN_P (insn))
13721 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13722 return 1;
13724 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13725 if (reg_dead_flag)
13726 return reg_dead_flag == 1 ? 1 : 0;
13728 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13729 return 1;
13732 if (insn == BB_HEAD (block))
13733 break;
13735 insn = PREV_INSN (insn);
13738 /* Look at live-in sets for the basic block that we were in. */
13739 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13740 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13741 return 0;
13743 return 1;
13746 /* Note hard registers in X that are used. */
13748 static void
13749 mark_used_regs_combine (rtx x)
13751 RTX_CODE code = GET_CODE (x);
13752 unsigned int regno;
13753 int i;
13755 switch (code)
13757 case LABEL_REF:
13758 case SYMBOL_REF:
13759 case CONST:
13760 CASE_CONST_ANY:
13761 case PC:
13762 case ADDR_VEC:
13763 case ADDR_DIFF_VEC:
13764 case ASM_INPUT:
13765 /* CC0 must die in the insn after it is set, so we don't need to take
13766 special note of it here. */
13767 case CC0:
13768 return;
13770 case CLOBBER:
13771 /* If we are clobbering a MEM, mark any hard registers inside the
13772 address as used. */
13773 if (MEM_P (XEXP (x, 0)))
13774 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13775 return;
13777 case REG:
13778 regno = REGNO (x);
13779 /* A hard reg in a wide mode may really be multiple registers.
13780 If so, mark all of them just like the first. */
13781 if (regno < FIRST_PSEUDO_REGISTER)
13783 /* None of this applies to the stack, frame or arg pointers. */
13784 if (regno == STACK_POINTER_REGNUM
13785 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13786 && regno == HARD_FRAME_POINTER_REGNUM)
13787 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13788 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13789 || regno == FRAME_POINTER_REGNUM)
13790 return;
13792 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13794 return;
13796 case SET:
13798 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13799 the address. */
13800 rtx testreg = SET_DEST (x);
13802 while (GET_CODE (testreg) == SUBREG
13803 || GET_CODE (testreg) == ZERO_EXTRACT
13804 || GET_CODE (testreg) == STRICT_LOW_PART)
13805 testreg = XEXP (testreg, 0);
13807 if (MEM_P (testreg))
13808 mark_used_regs_combine (XEXP (testreg, 0));
13810 mark_used_regs_combine (SET_SRC (x));
13812 return;
13814 default:
13815 break;
13818 /* Recursively scan the operands of this expression. */
13821 const char *fmt = GET_RTX_FORMAT (code);
13823 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13825 if (fmt[i] == 'e')
13826 mark_used_regs_combine (XEXP (x, i));
13827 else if (fmt[i] == 'E')
13829 int j;
13831 for (j = 0; j < XVECLEN (x, i); j++)
13832 mark_used_regs_combine (XVECEXP (x, i, j));
13838 /* Remove register number REGNO from the dead registers list of INSN.
13840 Return the note used to record the death, if there was one. */
13843 remove_death (unsigned int regno, rtx_insn *insn)
13845 rtx note = find_regno_note (insn, REG_DEAD, regno);
13847 if (note)
13848 remove_note (insn, note);
13850 return note;
13853 /* For each register (hardware or pseudo) used within expression X, if its
13854 death is in an instruction with luid between FROM_LUID (inclusive) and
13855 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13856 list headed by PNOTES.
13858 That said, don't move registers killed by maybe_kill_insn.
13860 This is done when X is being merged by combination into TO_INSN. These
13861 notes will then be distributed as needed. */
13863 static void
13864 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13865 rtx *pnotes)
13867 const char *fmt;
13868 int len, i;
13869 enum rtx_code code = GET_CODE (x);
13871 if (code == REG)
13873 unsigned int regno = REGNO (x);
13874 rtx_insn *where_dead = reg_stat[regno].last_death;
13876 /* Don't move the register if it gets killed in between from and to. */
13877 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13878 && ! reg_referenced_p (x, maybe_kill_insn))
13879 return;
13881 if (where_dead
13882 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13883 && DF_INSN_LUID (where_dead) >= from_luid
13884 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13886 rtx note = remove_death (regno, where_dead);
13888 /* It is possible for the call above to return 0. This can occur
13889 when last_death points to I2 or I1 that we combined with.
13890 In that case make a new note.
13892 We must also check for the case where X is a hard register
13893 and NOTE is a death note for a range of hard registers
13894 including X. In that case, we must put REG_DEAD notes for
13895 the remaining registers in place of NOTE. */
13897 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13898 && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
13900 unsigned int deadregno = REGNO (XEXP (note, 0));
13901 unsigned int deadend = END_REGNO (XEXP (note, 0));
13902 unsigned int ourend = END_REGNO (x);
13903 unsigned int i;
13905 for (i = deadregno; i < deadend; i++)
13906 if (i < regno || i >= ourend)
13907 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13910 /* If we didn't find any note, or if we found a REG_DEAD note that
13911 covers only part of the given reg, and we have a multi-reg hard
13912 register, then to be safe we must check for REG_DEAD notes
13913 for each register other than the first. They could have
13914 their own REG_DEAD notes lying around. */
13915 else if ((note == 0
13916 || (note != 0
13917 && partial_subreg_p (GET_MODE (XEXP (note, 0)),
13918 GET_MODE (x))))
13919 && regno < FIRST_PSEUDO_REGISTER
13920 && REG_NREGS (x) > 1)
13922 unsigned int ourend = END_REGNO (x);
13923 unsigned int i, offset;
13924 rtx oldnotes = 0;
13926 if (note)
13927 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13928 else
13929 offset = 1;
13931 for (i = regno + offset; i < ourend; i++)
13932 move_deaths (regno_reg_rtx[i],
13933 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13936 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13938 XEXP (note, 1) = *pnotes;
13939 *pnotes = note;
13941 else
13942 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13945 return;
13948 else if (GET_CODE (x) == SET)
13950 rtx dest = SET_DEST (x);
13952 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13954 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13955 that accesses one word of a multi-word item, some
13956 piece of everything register in the expression is used by
13957 this insn, so remove any old death. */
13958 /* ??? So why do we test for equality of the sizes? */
13960 if (GET_CODE (dest) == ZERO_EXTRACT
13961 || GET_CODE (dest) == STRICT_LOW_PART
13962 || (GET_CODE (dest) == SUBREG
13963 && (((GET_MODE_SIZE (GET_MODE (dest))
13964 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13965 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13966 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13968 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13969 return;
13972 /* If this is some other SUBREG, we know it replaces the entire
13973 value, so use that as the destination. */
13974 if (GET_CODE (dest) == SUBREG)
13975 dest = SUBREG_REG (dest);
13977 /* If this is a MEM, adjust deaths of anything used in the address.
13978 For a REG (the only other possibility), the entire value is
13979 being replaced so the old value is not used in this insn. */
13981 if (MEM_P (dest))
13982 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13983 to_insn, pnotes);
13984 return;
13987 else if (GET_CODE (x) == CLOBBER)
13988 return;
13990 len = GET_RTX_LENGTH (code);
13991 fmt = GET_RTX_FORMAT (code);
13993 for (i = 0; i < len; i++)
13995 if (fmt[i] == 'E')
13997 int j;
13998 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13999 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14000 to_insn, pnotes);
14002 else if (fmt[i] == 'e')
14003 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14007 /* Return 1 if X is the target of a bit-field assignment in BODY, the
14008 pattern of an insn. X must be a REG. */
14010 static int
14011 reg_bitfield_target_p (rtx x, rtx body)
14013 int i;
14015 if (GET_CODE (body) == SET)
14017 rtx dest = SET_DEST (body);
14018 rtx target;
14019 unsigned int regno, tregno, endregno, endtregno;
14021 if (GET_CODE (dest) == ZERO_EXTRACT)
14022 target = XEXP (dest, 0);
14023 else if (GET_CODE (dest) == STRICT_LOW_PART)
14024 target = SUBREG_REG (XEXP (dest, 0));
14025 else
14026 return 0;
14028 if (GET_CODE (target) == SUBREG)
14029 target = SUBREG_REG (target);
14031 if (!REG_P (target))
14032 return 0;
14034 tregno = REGNO (target), regno = REGNO (x);
14035 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14036 return target == x;
14038 endtregno = end_hard_regno (GET_MODE (target), tregno);
14039 endregno = end_hard_regno (GET_MODE (x), regno);
14041 return endregno > tregno && regno < endtregno;
14044 else if (GET_CODE (body) == PARALLEL)
14045 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14046 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14047 return 1;
14049 return 0;
14052 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14053 as appropriate. I3 and I2 are the insns resulting from the combination
14054 insns including FROM (I2 may be zero).
14056 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14057 not need REG_DEAD notes because they are being substituted for. This
14058 saves searching in the most common cases.
14060 Each note in the list is either ignored or placed on some insns, depending
14061 on the type of note. */
14063 static void
14064 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14065 rtx elim_i2, rtx elim_i1, rtx elim_i0)
14067 rtx note, next_note;
14068 rtx tem_note;
14069 rtx_insn *tem_insn;
14071 for (note = notes; note; note = next_note)
14073 rtx_insn *place = 0, *place2 = 0;
14075 next_note = XEXP (note, 1);
14076 switch (REG_NOTE_KIND (note))
14078 case REG_BR_PROB:
14079 case REG_BR_PRED:
14080 /* Doesn't matter much where we put this, as long as it's somewhere.
14081 It is preferable to keep these notes on branches, which is most
14082 likely to be i3. */
14083 place = i3;
14084 break;
14086 case REG_NON_LOCAL_GOTO:
14087 if (JUMP_P (i3))
14088 place = i3;
14089 else
14091 gcc_assert (i2 && JUMP_P (i2));
14092 place = i2;
14094 break;
14096 case REG_EH_REGION:
14097 /* These notes must remain with the call or trapping instruction. */
14098 if (CALL_P (i3))
14099 place = i3;
14100 else if (i2 && CALL_P (i2))
14101 place = i2;
14102 else
14104 gcc_assert (cfun->can_throw_non_call_exceptions);
14105 if (may_trap_p (i3))
14106 place = i3;
14107 else if (i2 && may_trap_p (i2))
14108 place = i2;
14109 /* ??? Otherwise assume we've combined things such that we
14110 can now prove that the instructions can't trap. Drop the
14111 note in this case. */
14113 break;
14115 case REG_ARGS_SIZE:
14116 /* ??? How to distribute between i3-i1. Assume i3 contains the
14117 entire adjustment. Assert i3 contains at least some adjust. */
14118 if (!noop_move_p (i3))
14120 int old_size, args_size = INTVAL (XEXP (note, 0));
14121 /* fixup_args_size_notes looks at REG_NORETURN note,
14122 so ensure the note is placed there first. */
14123 if (CALL_P (i3))
14125 rtx *np;
14126 for (np = &next_note; *np; np = &XEXP (*np, 1))
14127 if (REG_NOTE_KIND (*np) == REG_NORETURN)
14129 rtx n = *np;
14130 *np = XEXP (n, 1);
14131 XEXP (n, 1) = REG_NOTES (i3);
14132 REG_NOTES (i3) = n;
14133 break;
14136 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14137 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14138 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14139 gcc_assert (old_size != args_size
14140 || (CALL_P (i3)
14141 && !ACCUMULATE_OUTGOING_ARGS
14142 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14144 break;
14146 case REG_NORETURN:
14147 case REG_SETJMP:
14148 case REG_TM:
14149 case REG_CALL_DECL:
14150 /* These notes must remain with the call. It should not be
14151 possible for both I2 and I3 to be a call. */
14152 if (CALL_P (i3))
14153 place = i3;
14154 else
14156 gcc_assert (i2 && CALL_P (i2));
14157 place = i2;
14159 break;
14161 case REG_UNUSED:
14162 /* Any clobbers for i3 may still exist, and so we must process
14163 REG_UNUSED notes from that insn.
14165 Any clobbers from i2 or i1 can only exist if they were added by
14166 recog_for_combine. In that case, recog_for_combine created the
14167 necessary REG_UNUSED notes. Trying to keep any original
14168 REG_UNUSED notes from these insns can cause incorrect output
14169 if it is for the same register as the original i3 dest.
14170 In that case, we will notice that the register is set in i3,
14171 and then add a REG_UNUSED note for the destination of i3, which
14172 is wrong. However, it is possible to have REG_UNUSED notes from
14173 i2 or i1 for register which were both used and clobbered, so
14174 we keep notes from i2 or i1 if they will turn into REG_DEAD
14175 notes. */
14177 /* If this register is set or clobbered in I3, put the note there
14178 unless there is one already. */
14179 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14181 if (from_insn != i3)
14182 break;
14184 if (! (REG_P (XEXP (note, 0))
14185 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14186 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14187 place = i3;
14189 /* Otherwise, if this register is used by I3, then this register
14190 now dies here, so we must put a REG_DEAD note here unless there
14191 is one already. */
14192 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14193 && ! (REG_P (XEXP (note, 0))
14194 ? find_regno_note (i3, REG_DEAD,
14195 REGNO (XEXP (note, 0)))
14196 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14198 PUT_REG_NOTE_KIND (note, REG_DEAD);
14199 place = i3;
14201 break;
14203 case REG_EQUAL:
14204 case REG_EQUIV:
14205 case REG_NOALIAS:
14206 /* These notes say something about results of an insn. We can
14207 only support them if they used to be on I3 in which case they
14208 remain on I3. Otherwise they are ignored.
14210 If the note refers to an expression that is not a constant, we
14211 must also ignore the note since we cannot tell whether the
14212 equivalence is still true. It might be possible to do
14213 slightly better than this (we only have a problem if I2DEST
14214 or I1DEST is present in the expression), but it doesn't
14215 seem worth the trouble. */
14217 if (from_insn == i3
14218 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14219 place = i3;
14220 break;
14222 case REG_INC:
14223 /* These notes say something about how a register is used. They must
14224 be present on any use of the register in I2 or I3. */
14225 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14226 place = i3;
14228 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14230 if (place)
14231 place2 = i2;
14232 else
14233 place = i2;
14235 break;
14237 case REG_LABEL_TARGET:
14238 case REG_LABEL_OPERAND:
14239 /* This can show up in several ways -- either directly in the
14240 pattern, or hidden off in the constant pool with (or without?)
14241 a REG_EQUAL note. */
14242 /* ??? Ignore the without-reg_equal-note problem for now. */
14243 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14244 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14245 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14246 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14247 place = i3;
14249 if (i2
14250 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14251 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14252 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14253 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14255 if (place)
14256 place2 = i2;
14257 else
14258 place = i2;
14261 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14262 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14263 there. */
14264 if (place && JUMP_P (place)
14265 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14266 && (JUMP_LABEL (place) == NULL
14267 || JUMP_LABEL (place) == XEXP (note, 0)))
14269 rtx label = JUMP_LABEL (place);
14271 if (!label)
14272 JUMP_LABEL (place) = XEXP (note, 0);
14273 else if (LABEL_P (label))
14274 LABEL_NUSES (label)--;
14277 if (place2 && JUMP_P (place2)
14278 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14279 && (JUMP_LABEL (place2) == NULL
14280 || JUMP_LABEL (place2) == XEXP (note, 0)))
14282 rtx label = JUMP_LABEL (place2);
14284 if (!label)
14285 JUMP_LABEL (place2) = XEXP (note, 0);
14286 else if (LABEL_P (label))
14287 LABEL_NUSES (label)--;
14288 place2 = 0;
14290 break;
14292 case REG_NONNEG:
14293 /* This note says something about the value of a register prior
14294 to the execution of an insn. It is too much trouble to see
14295 if the note is still correct in all situations. It is better
14296 to simply delete it. */
14297 break;
14299 case REG_DEAD:
14300 /* If we replaced the right hand side of FROM_INSN with a
14301 REG_EQUAL note, the original use of the dying register
14302 will not have been combined into I3 and I2. In such cases,
14303 FROM_INSN is guaranteed to be the first of the combined
14304 instructions, so we simply need to search back before
14305 FROM_INSN for the previous use or set of this register,
14306 then alter the notes there appropriately.
14308 If the register is used as an input in I3, it dies there.
14309 Similarly for I2, if it is nonzero and adjacent to I3.
14311 If the register is not used as an input in either I3 or I2
14312 and it is not one of the registers we were supposed to eliminate,
14313 there are two possibilities. We might have a non-adjacent I2
14314 or we might have somehow eliminated an additional register
14315 from a computation. For example, we might have had A & B where
14316 we discover that B will always be zero. In this case we will
14317 eliminate the reference to A.
14319 In both cases, we must search to see if we can find a previous
14320 use of A and put the death note there. */
14322 if (from_insn
14323 && from_insn == i2mod
14324 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14325 tem_insn = from_insn;
14326 else
14328 if (from_insn
14329 && CALL_P (from_insn)
14330 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14331 place = from_insn;
14332 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14333 place = i3;
14334 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14335 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14336 place = i2;
14337 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14338 && !(i2mod
14339 && reg_overlap_mentioned_p (XEXP (note, 0),
14340 i2mod_old_rhs)))
14341 || rtx_equal_p (XEXP (note, 0), elim_i1)
14342 || rtx_equal_p (XEXP (note, 0), elim_i0))
14343 break;
14344 tem_insn = i3;
14345 /* If the new I2 sets the same register that is marked dead
14346 in the note, we do not know where to put the note.
14347 Give up. */
14348 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14349 break;
14352 if (place == 0)
14354 basic_block bb = this_basic_block;
14356 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14358 if (!NONDEBUG_INSN_P (tem_insn))
14360 if (tem_insn == BB_HEAD (bb))
14361 break;
14362 continue;
14365 /* If the register is being set at TEM_INSN, see if that is all
14366 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14367 into a REG_UNUSED note instead. Don't delete sets to
14368 global register vars. */
14369 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14370 || !global_regs[REGNO (XEXP (note, 0))])
14371 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14373 rtx set = single_set (tem_insn);
14374 rtx inner_dest = 0;
14375 rtx_insn *cc0_setter = NULL;
14377 if (set != 0)
14378 for (inner_dest = SET_DEST (set);
14379 (GET_CODE (inner_dest) == STRICT_LOW_PART
14380 || GET_CODE (inner_dest) == SUBREG
14381 || GET_CODE (inner_dest) == ZERO_EXTRACT);
14382 inner_dest = XEXP (inner_dest, 0))
14385 /* Verify that it was the set, and not a clobber that
14386 modified the register.
14388 CC0 targets must be careful to maintain setter/user
14389 pairs. If we cannot delete the setter due to side
14390 effects, mark the user with an UNUSED note instead
14391 of deleting it. */
14393 if (set != 0 && ! side_effects_p (SET_SRC (set))
14394 && rtx_equal_p (XEXP (note, 0), inner_dest)
14395 && (!HAVE_cc0
14396 || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
14397 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
14398 && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
14400 /* Move the notes and links of TEM_INSN elsewhere.
14401 This might delete other dead insns recursively.
14402 First set the pattern to something that won't use
14403 any register. */
14404 rtx old_notes = REG_NOTES (tem_insn);
14406 PATTERN (tem_insn) = pc_rtx;
14407 REG_NOTES (tem_insn) = NULL;
14409 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14410 NULL_RTX, NULL_RTX, NULL_RTX);
14411 distribute_links (LOG_LINKS (tem_insn));
14413 unsigned int regno = REGNO (XEXP (note, 0));
14414 reg_stat_type *rsp = &reg_stat[regno];
14415 if (rsp->last_set == tem_insn)
14416 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14418 SET_INSN_DELETED (tem_insn);
14419 if (tem_insn == i2)
14420 i2 = NULL;
14422 /* Delete the setter too. */
14423 if (cc0_setter)
14425 PATTERN (cc0_setter) = pc_rtx;
14426 old_notes = REG_NOTES (cc0_setter);
14427 REG_NOTES (cc0_setter) = NULL;
14429 distribute_notes (old_notes, cc0_setter,
14430 cc0_setter, NULL,
14431 NULL_RTX, NULL_RTX, NULL_RTX);
14432 distribute_links (LOG_LINKS (cc0_setter));
14434 SET_INSN_DELETED (cc0_setter);
14435 if (cc0_setter == i2)
14436 i2 = NULL;
14439 else
14441 PUT_REG_NOTE_KIND (note, REG_UNUSED);
14443 /* If there isn't already a REG_UNUSED note, put one
14444 here. Do not place a REG_DEAD note, even if
14445 the register is also used here; that would not
14446 match the algorithm used in lifetime analysis
14447 and can cause the consistency check in the
14448 scheduler to fail. */
14449 if (! find_regno_note (tem_insn, REG_UNUSED,
14450 REGNO (XEXP (note, 0))))
14451 place = tem_insn;
14452 break;
14455 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14456 || (CALL_P (tem_insn)
14457 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14459 place = tem_insn;
14461 /* If we are doing a 3->2 combination, and we have a
14462 register which formerly died in i3 and was not used
14463 by i2, which now no longer dies in i3 and is used in
14464 i2 but does not die in i2, and place is between i2
14465 and i3, then we may need to move a link from place to
14466 i2. */
14467 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14468 && from_insn
14469 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14470 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14472 struct insn_link *links = LOG_LINKS (place);
14473 LOG_LINKS (place) = NULL;
14474 distribute_links (links);
14476 break;
14479 if (tem_insn == BB_HEAD (bb))
14480 break;
14485 /* If the register is set or already dead at PLACE, we needn't do
14486 anything with this note if it is still a REG_DEAD note.
14487 We check here if it is set at all, not if is it totally replaced,
14488 which is what `dead_or_set_p' checks, so also check for it being
14489 set partially. */
14491 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14493 unsigned int regno = REGNO (XEXP (note, 0));
14494 reg_stat_type *rsp = &reg_stat[regno];
14496 if (dead_or_set_p (place, XEXP (note, 0))
14497 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14499 /* Unless the register previously died in PLACE, clear
14500 last_death. [I no longer understand why this is
14501 being done.] */
14502 if (rsp->last_death != place)
14503 rsp->last_death = 0;
14504 place = 0;
14506 else
14507 rsp->last_death = place;
14509 /* If this is a death note for a hard reg that is occupying
14510 multiple registers, ensure that we are still using all
14511 parts of the object. If we find a piece of the object
14512 that is unused, we must arrange for an appropriate REG_DEAD
14513 note to be added for it. However, we can't just emit a USE
14514 and tag the note to it, since the register might actually
14515 be dead; so we recourse, and the recursive call then finds
14516 the previous insn that used this register. */
14518 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14520 unsigned int endregno = END_REGNO (XEXP (note, 0));
14521 bool all_used = true;
14522 unsigned int i;
14524 for (i = regno; i < endregno; i++)
14525 if ((! refers_to_regno_p (i, PATTERN (place))
14526 && ! find_regno_fusage (place, USE, i))
14527 || dead_or_set_regno_p (place, i))
14529 all_used = false;
14530 break;
14533 if (! all_used)
14535 /* Put only REG_DEAD notes for pieces that are
14536 not already dead or set. */
14538 for (i = regno; i < endregno;
14539 i += hard_regno_nregs[i][reg_raw_mode[i]])
14541 rtx piece = regno_reg_rtx[i];
14542 basic_block bb = this_basic_block;
14544 if (! dead_or_set_p (place, piece)
14545 && ! reg_bitfield_target_p (piece,
14546 PATTERN (place)))
14548 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14549 NULL_RTX);
14551 distribute_notes (new_note, place, place,
14552 NULL, NULL_RTX, NULL_RTX,
14553 NULL_RTX);
14555 else if (! refers_to_regno_p (i, PATTERN (place))
14556 && ! find_regno_fusage (place, USE, i))
14557 for (tem_insn = PREV_INSN (place); ;
14558 tem_insn = PREV_INSN (tem_insn))
14560 if (!NONDEBUG_INSN_P (tem_insn))
14562 if (tem_insn == BB_HEAD (bb))
14563 break;
14564 continue;
14566 if (dead_or_set_p (tem_insn, piece)
14567 || reg_bitfield_target_p (piece,
14568 PATTERN (tem_insn)))
14570 add_reg_note (tem_insn, REG_UNUSED, piece);
14571 break;
14576 place = 0;
14580 break;
14582 default:
14583 /* Any other notes should not be present at this point in the
14584 compilation. */
14585 gcc_unreachable ();
14588 if (place)
14590 XEXP (note, 1) = REG_NOTES (place);
14591 REG_NOTES (place) = note;
14594 if (place2)
14595 add_shallow_copy_of_reg_note (place2, note);
14599 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14600 I3, I2, and I1 to new locations. This is also called to add a link
14601 pointing at I3 when I3's destination is changed. */
14603 static void
14604 distribute_links (struct insn_link *links)
14606 struct insn_link *link, *next_link;
14608 for (link = links; link; link = next_link)
14610 rtx_insn *place = 0;
14611 rtx_insn *insn;
14612 rtx set, reg;
14614 next_link = link->next;
14616 /* If the insn that this link points to is a NOTE, ignore it. */
14617 if (NOTE_P (link->insn))
14618 continue;
14620 set = 0;
14621 rtx pat = PATTERN (link->insn);
14622 if (GET_CODE (pat) == SET)
14623 set = pat;
14624 else if (GET_CODE (pat) == PARALLEL)
14626 int i;
14627 for (i = 0; i < XVECLEN (pat, 0); i++)
14629 set = XVECEXP (pat, 0, i);
14630 if (GET_CODE (set) != SET)
14631 continue;
14633 reg = SET_DEST (set);
14634 while (GET_CODE (reg) == ZERO_EXTRACT
14635 || GET_CODE (reg) == STRICT_LOW_PART
14636 || GET_CODE (reg) == SUBREG)
14637 reg = XEXP (reg, 0);
14639 if (!REG_P (reg))
14640 continue;
14642 if (REGNO (reg) == link->regno)
14643 break;
14645 if (i == XVECLEN (pat, 0))
14646 continue;
14648 else
14649 continue;
14651 reg = SET_DEST (set);
14653 while (GET_CODE (reg) == ZERO_EXTRACT
14654 || GET_CODE (reg) == STRICT_LOW_PART
14655 || GET_CODE (reg) == SUBREG)
14656 reg = XEXP (reg, 0);
14658 /* A LOG_LINK is defined as being placed on the first insn that uses
14659 a register and points to the insn that sets the register. Start
14660 searching at the next insn after the target of the link and stop
14661 when we reach a set of the register or the end of the basic block.
14663 Note that this correctly handles the link that used to point from
14664 I3 to I2. Also note that not much searching is typically done here
14665 since most links don't point very far away. */
14667 for (insn = NEXT_INSN (link->insn);
14668 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14669 || BB_HEAD (this_basic_block->next_bb) != insn));
14670 insn = NEXT_INSN (insn))
14671 if (DEBUG_INSN_P (insn))
14672 continue;
14673 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14675 if (reg_referenced_p (reg, PATTERN (insn)))
14676 place = insn;
14677 break;
14679 else if (CALL_P (insn)
14680 && find_reg_fusage (insn, USE, reg))
14682 place = insn;
14683 break;
14685 else if (INSN_P (insn) && reg_set_p (reg, insn))
14686 break;
14688 /* If we found a place to put the link, place it there unless there
14689 is already a link to the same insn as LINK at that point. */
14691 if (place)
14693 struct insn_link *link2;
14695 FOR_EACH_LOG_LINK (link2, place)
14696 if (link2->insn == link->insn && link2->regno == link->regno)
14697 break;
14699 if (link2 == NULL)
14701 link->next = LOG_LINKS (place);
14702 LOG_LINKS (place) = link;
14704 /* Set added_links_insn to the earliest insn we added a
14705 link to. */
14706 if (added_links_insn == 0
14707 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14708 added_links_insn = place;
14714 /* Check for any register or memory mentioned in EQUIV that is not
14715 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14716 of EXPR where some registers may have been replaced by constants. */
14718 static bool
14719 unmentioned_reg_p (rtx equiv, rtx expr)
14721 subrtx_iterator::array_type array;
14722 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14724 const_rtx x = *iter;
14725 if ((REG_P (x) || MEM_P (x))
14726 && !reg_mentioned_p (x, expr))
14727 return true;
14729 return false;
14732 DEBUG_FUNCTION void
14733 dump_combine_stats (FILE *file)
14735 fprintf
14736 (file,
14737 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14738 combine_attempts, combine_merges, combine_extras, combine_successes);
14741 void
14742 dump_combine_total_stats (FILE *file)
14744 fprintf
14745 (file,
14746 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14747 total_attempts, total_merges, total_extras, total_successes);
14750 /* Try combining insns through substitution. */
14751 static unsigned int
14752 rest_of_handle_combine (void)
14754 int rebuild_jump_labels_after_combine;
14756 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14757 df_note_add_problem ();
14758 df_analyze ();
14760 regstat_init_n_sets_and_refs ();
14761 reg_n_sets_max = max_reg_num ();
14763 rebuild_jump_labels_after_combine
14764 = combine_instructions (get_insns (), max_reg_num ());
14766 /* Combining insns may have turned an indirect jump into a
14767 direct jump. Rebuild the JUMP_LABEL fields of jumping
14768 instructions. */
14769 if (rebuild_jump_labels_after_combine)
14771 if (dom_info_available_p (CDI_DOMINATORS))
14772 free_dominance_info (CDI_DOMINATORS);
14773 timevar_push (TV_JUMP);
14774 rebuild_jump_labels (get_insns ());
14775 cleanup_cfg (0);
14776 timevar_pop (TV_JUMP);
14779 regstat_free_n_sets_and_refs ();
14780 return 0;
14783 namespace {
14785 const pass_data pass_data_combine =
14787 RTL_PASS, /* type */
14788 "combine", /* name */
14789 OPTGROUP_NONE, /* optinfo_flags */
14790 TV_COMBINE, /* tv_id */
14791 PROP_cfglayout, /* properties_required */
14792 0, /* properties_provided */
14793 0, /* properties_destroyed */
14794 0, /* todo_flags_start */
14795 TODO_df_finish, /* todo_flags_finish */
14798 class pass_combine : public rtl_opt_pass
14800 public:
14801 pass_combine (gcc::context *ctxt)
14802 : rtl_opt_pass (pass_data_combine, ctxt)
14805 /* opt_pass methods: */
14806 virtual bool gate (function *) { return (optimize > 0); }
14807 virtual unsigned int execute (function *)
14809 return rest_of_handle_combine ();
14812 }; // class pass_combine
14814 } // anon namespace
14816 rtl_opt_pass *
14817 make_pass_combine (gcc::context *ctxt)
14819 return new pass_combine (ctxt);