c++: only cache constexpr calls that are constant exprs
[official-gcc.git] / gcc / combine.cc
blobd9161b257e8257a483bf74ae110f19f9a8ade3ae
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2023 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 We check (with modified_between_p) to avoid combining in such a way
37 as to move a computation to a place where its value would be different.
39 Combination is done by mathematically substituting the previous
40 insn(s) values for the regs they set into the expressions in
41 the later insns that refer to these regs. If the result is a valid insn
42 for our target machine, according to the machine description,
43 we install it, delete the earlier insns, and update the data flow
44 information (LOG_LINKS and REG_NOTES) for what we did.
46 There are a few exceptions where the dataflow information isn't
47 completely updated (however this is only a local issue since it is
48 regenerated before the next pass that uses it):
50 - reg_live_length is not updated
51 - reg_n_refs is not adjusted in the rare case when a register is
52 no longer required in a computation
53 - there are extremely rare cases (see distribute_notes) when a
54 REG_DEAD note is lost
55 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
56 removed because there is no way to know which register it was
57 linking
59 To simplify substitution, we combine only when the earlier insn(s)
60 consist of only a single assignment. To simplify updating afterward,
61 we never combine when a subroutine call appears in the middle. */
63 #include "config.h"
64 #include "system.h"
65 #include "coretypes.h"
66 #include "backend.h"
67 #include "target.h"
68 #include "rtl.h"
69 #include "tree.h"
70 #include "cfghooks.h"
71 #include "predict.h"
72 #include "df.h"
73 #include "memmodel.h"
74 #include "tm_p.h"
75 #include "optabs.h"
76 #include "regs.h"
77 #include "emit-rtl.h"
78 #include "recog.h"
79 #include "cgraph.h"
80 #include "stor-layout.h"
81 #include "cfgrtl.h"
82 #include "cfgcleanup.h"
83 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
84 #include "explow.h"
85 #include "insn-attr.h"
86 #include "rtlhooks-def.h"
87 #include "expr.h"
88 #include "tree-pass.h"
89 #include "valtrack.h"
90 #include "rtl-iter.h"
91 #include "print-rtl.h"
92 #include "function-abi.h"
93 #include "rtlanal.h"
95 /* Number of attempts to combine instructions in this function. */
97 static int combine_attempts;
99 /* Number of attempts that got as far as substitution in this function. */
101 static int combine_merges;
103 /* Number of instructions combined with added SETs in this function. */
105 static int combine_extras;
107 /* Number of instructions combined in this function. */
109 static int combine_successes;
111 /* Totals over entire compilation. */
113 static int total_attempts, total_merges, total_extras, total_successes;
115 /* combine_instructions may try to replace the right hand side of the
116 second instruction with the value of an associated REG_EQUAL note
117 before throwing it at try_combine. That is problematic when there
118 is a REG_DEAD note for a register used in the old right hand side
119 and can cause distribute_notes to do wrong things. This is the
120 second instruction if it has been so modified, null otherwise. */
122 static rtx_insn *i2mod;
124 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
126 static rtx i2mod_old_rhs;
128 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
130 static rtx i2mod_new_rhs;
132 struct reg_stat_type {
133 /* Record last point of death of (hard or pseudo) register n. */
134 rtx_insn *last_death;
136 /* Record last point of modification of (hard or pseudo) register n. */
137 rtx_insn *last_set;
139 /* The next group of fields allows the recording of the last value assigned
140 to (hard or pseudo) register n. We use this information to see if an
141 operation being processed is redundant given a prior operation performed
142 on the register. For example, an `and' with a constant is redundant if
143 all the zero bits are already known to be turned off.
145 We use an approach similar to that used by cse, but change it in the
146 following ways:
148 (1) We do not want to reinitialize at each label.
149 (2) It is useful, but not critical, to know the actual value assigned
150 to a register. Often just its form is helpful.
152 Therefore, we maintain the following fields:
154 last_set_value the last value assigned
155 last_set_label records the value of label_tick when the
156 register was assigned
157 last_set_table_tick records the value of label_tick when a
158 value using the register is assigned
159 last_set_invalid set to true when it is not valid
160 to use the value of this register in some
161 register's value
163 To understand the usage of these tables, it is important to understand
164 the distinction between the value in last_set_value being valid and
165 the register being validly contained in some other expression in the
166 table.
168 (The next two parameters are out of date).
170 reg_stat[i].last_set_value is valid if it is nonzero, and either
171 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
173 Register I may validly appear in any expression returned for the value
174 of another register if reg_n_sets[i] is 1. It may also appear in the
175 value for register J if reg_stat[j].last_set_invalid is zero, or
176 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
178 If an expression is found in the table containing a register which may
179 not validly appear in an expression, the register is replaced by
180 something that won't match, (clobber (const_int 0)). */
182 /* Record last value assigned to (hard or pseudo) register n. */
184 rtx last_set_value;
186 /* Record the value of label_tick when an expression involving register n
187 is placed in last_set_value. */
189 int last_set_table_tick;
191 /* Record the value of label_tick when the value for register n is placed in
192 last_set_value. */
194 int last_set_label;
196 /* These fields are maintained in parallel with last_set_value and are
197 used to store the mode in which the register was last set, the bits
198 that were known to be zero when it was last set, and the number of
199 sign bits copies it was known to have when it was last set. */
201 unsigned HOST_WIDE_INT last_set_nonzero_bits;
202 char last_set_sign_bit_copies;
203 ENUM_BITFIELD(machine_mode) last_set_mode : MACHINE_MODE_BITSIZE;
205 /* Set to true if references to register n in expressions should not be
206 used. last_set_invalid is set nonzero when this register is being
207 assigned to and last_set_table_tick == label_tick. */
209 bool last_set_invalid;
211 /* Some registers that are set more than once and used in more than one
212 basic block are nevertheless always set in similar ways. For example,
213 a QImode register may be loaded from memory in two places on a machine
214 where byte loads zero extend.
216 We record in the following fields if a register has some leading bits
217 that are always equal to the sign bit, and what we know about the
218 nonzero bits of a register, specifically which bits are known to be
219 zero.
221 If an entry is zero, it means that we don't know anything special. */
223 unsigned char sign_bit_copies;
225 unsigned HOST_WIDE_INT nonzero_bits;
227 /* Record the value of the label_tick when the last truncation
228 happened. The field truncated_to_mode is only valid if
229 truncation_label == label_tick. */
231 int truncation_label;
233 /* Record the last truncation seen for this register. If truncation
234 is not a nop to this mode we might be able to save an explicit
235 truncation if we know that value already contains a truncated
236 value. */
238 ENUM_BITFIELD(machine_mode) truncated_to_mode : MACHINE_MODE_BITSIZE;
242 static vec<reg_stat_type> reg_stat;
244 /* One plus the highest pseudo for which we track REG_N_SETS.
245 regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
246 but during combine_split_insns new pseudos can be created. As we don't have
247 updated DF information in that case, it is hard to initialize the array
248 after growing. The combiner only cares about REG_N_SETS (regno) == 1,
249 so instead of growing the arrays, just assume all newly created pseudos
250 during combine might be set multiple times. */
252 static unsigned int reg_n_sets_max;
254 /* Record the luid of the last insn that invalidated memory
255 (anything that writes memory, and subroutine calls, but not pushes). */
257 static int mem_last_set;
259 /* Record the luid of the last CALL_INSN
260 so we can tell whether a potential combination crosses any calls. */
262 static int last_call_luid;
264 /* When `subst' is called, this is the insn that is being modified
265 (by combining in a previous insn). The PATTERN of this insn
266 is still the old pattern partially modified and it should not be
267 looked at, but this may be used to examine the successors of the insn
268 to judge whether a simplification is valid. */
270 static rtx_insn *subst_insn;
272 /* This is the lowest LUID that `subst' is currently dealing with.
273 get_last_value will not return a value if the register was set at or
274 after this LUID. If not for this mechanism, we could get confused if
275 I2 or I1 in try_combine were an insn that used the old value of a register
276 to obtain a new value. In that case, we might erroneously get the
277 new value of the register when we wanted the old one. */
279 static int subst_low_luid;
281 /* This contains any hard registers that are used in newpat; reg_dead_at_p
282 must consider all these registers to be always live. */
284 static HARD_REG_SET newpat_used_regs;
286 /* This is an insn to which a LOG_LINKS entry has been added. If this
287 insn is the earlier than I2 or I3, combine should rescan starting at
288 that location. */
290 static rtx_insn *added_links_insn;
292 /* And similarly, for notes. */
294 static rtx_insn *added_notes_insn;
296 /* Basic block in which we are performing combines. */
297 static basic_block this_basic_block;
298 static bool optimize_this_for_speed_p;
301 /* Length of the currently allocated uid_insn_cost array. */
303 static int max_uid_known;
305 /* The following array records the insn_cost for every insn
306 in the instruction stream. */
308 static int *uid_insn_cost;
310 /* The following array records the LOG_LINKS for every insn in the
311 instruction stream as struct insn_link pointers. */
313 struct insn_link {
314 rtx_insn *insn;
315 unsigned int regno;
316 struct insn_link *next;
319 static struct insn_link **uid_log_links;
321 static inline int
322 insn_uid_check (const_rtx insn)
324 int uid = INSN_UID (insn);
325 gcc_checking_assert (uid <= max_uid_known);
326 return uid;
329 #define INSN_COST(INSN) (uid_insn_cost[insn_uid_check (INSN)])
330 #define LOG_LINKS(INSN) (uid_log_links[insn_uid_check (INSN)])
332 #define FOR_EACH_LOG_LINK(L, INSN) \
333 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
335 /* Links for LOG_LINKS are allocated from this obstack. */
337 static struct obstack insn_link_obstack;
339 /* Allocate a link. */
341 static inline struct insn_link *
342 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
344 struct insn_link *l
345 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
346 sizeof (struct insn_link));
347 l->insn = insn;
348 l->regno = regno;
349 l->next = next;
350 return l;
353 /* Incremented for each basic block. */
355 static int label_tick;
357 /* Reset to label_tick for each extended basic block in scanning order. */
359 static int label_tick_ebb_start;
361 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
362 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
364 static scalar_int_mode nonzero_bits_mode;
366 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
367 be safely used. It is zero while computing them and after combine has
368 completed. This former test prevents propagating values based on
369 previously set values, which can be incorrect if a variable is modified
370 in a loop. */
372 static int nonzero_sign_valid;
375 /* Record one modification to rtl structure
376 to be undone by storing old_contents into *where. */
378 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
380 struct undo
382 struct undo *next;
383 enum undo_kind kind;
384 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
385 union { rtx *r; int *i; int regno; struct insn_link **l; } where;
388 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
389 num_undo says how many are currently recorded.
391 other_insn is nonzero if we have modified some other insn in the process
392 of working on subst_insn. It must be verified too. */
394 struct undobuf
396 struct undo *undos;
397 struct undo *frees;
398 rtx_insn *other_insn;
401 static struct undobuf undobuf;
403 /* Number of times the pseudo being substituted for
404 was found and replaced. */
406 static int n_occurrences;
408 static rtx reg_nonzero_bits_for_combine (const_rtx, scalar_int_mode,
409 scalar_int_mode,
410 unsigned HOST_WIDE_INT *);
411 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, scalar_int_mode,
412 scalar_int_mode,
413 unsigned int *);
414 static void do_SUBST (rtx *, rtx);
415 static void do_SUBST_INT (int *, int);
416 static void init_reg_last (void);
417 static void setup_incoming_promotions (rtx_insn *);
418 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
419 static bool cant_combine_insn_p (rtx_insn *);
420 static bool can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
421 rtx_insn *, rtx_insn *, rtx *, rtx *);
422 static bool combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx,
423 bool, bool, rtx *);
424 static bool contains_muldiv (rtx);
425 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
426 bool *, rtx_insn *);
427 static void undo_all (void);
428 static void undo_commit (void);
429 static rtx *find_split_point (rtx *, rtx_insn *, bool);
430 static rtx subst (rtx, rtx, rtx, bool, bool, bool);
431 static rtx combine_simplify_rtx (rtx, machine_mode, bool, bool);
432 static rtx simplify_if_then_else (rtx);
433 static rtx simplify_set (rtx);
434 static rtx simplify_logical (rtx);
435 static rtx expand_compound_operation (rtx);
436 static const_rtx expand_field_assignment (const_rtx);
437 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT, rtx,
438 unsigned HOST_WIDE_INT, bool, bool, bool);
439 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
440 unsigned HOST_WIDE_INT *);
441 static rtx canon_reg_for_combine (rtx, rtx);
442 static rtx force_int_to_mode (rtx, scalar_int_mode, scalar_int_mode,
443 scalar_int_mode, unsigned HOST_WIDE_INT, bool);
444 static rtx force_to_mode (rtx, machine_mode,
445 unsigned HOST_WIDE_INT, bool);
446 static rtx if_then_else_cond (rtx, rtx *, rtx *);
447 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
448 static bool rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
449 static rtx make_field_assignment (rtx);
450 static rtx apply_distributive_law (rtx);
451 static rtx distribute_and_simplify_rtx (rtx, int);
452 static rtx simplify_and_const_int_1 (scalar_int_mode, rtx,
453 unsigned HOST_WIDE_INT);
454 static rtx simplify_and_const_int (rtx, scalar_int_mode, rtx,
455 unsigned HOST_WIDE_INT);
456 static bool merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
457 HOST_WIDE_INT, machine_mode, bool *);
458 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
459 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
460 int);
461 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
462 static rtx gen_lowpart_for_combine (machine_mode, rtx);
463 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
464 rtx, rtx *);
465 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
466 static void update_table_tick (rtx);
467 static void record_value_for_reg (rtx, rtx_insn *, rtx);
468 static void check_promoted_subreg (rtx_insn *, rtx);
469 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
470 static void record_dead_and_set_regs (rtx_insn *);
471 static bool get_last_value_validate (rtx *, rtx_insn *, int, bool);
472 static rtx get_last_value (const_rtx);
473 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
474 static bool reg_dead_at_p (rtx, rtx_insn *);
475 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
476 static bool reg_bitfield_target_p (rtx, rtx);
477 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *,
478 rtx, rtx, rtx);
479 static void distribute_links (struct insn_link *);
480 static void mark_used_regs_combine (rtx);
481 static void record_promoted_value (rtx_insn *, rtx);
482 static bool unmentioned_reg_p (rtx, rtx);
483 static void record_truncated_values (rtx *, void *);
484 static bool reg_truncated_to_mode (machine_mode, const_rtx);
485 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
488 /* It is not safe to use ordinary gen_lowpart in combine.
489 See comments in gen_lowpart_for_combine. */
490 #undef RTL_HOOKS_GEN_LOWPART
491 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
493 /* Our implementation of gen_lowpart never emits a new pseudo. */
494 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
495 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
497 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
498 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
500 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
501 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
503 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
504 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
506 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
509 /* Convenience wrapper for the canonicalize_comparison target hook.
510 Target hooks cannot use enum rtx_code. */
511 static inline void
512 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
513 bool op0_preserve_value)
515 int code_int = (int)*code;
516 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
517 *code = (enum rtx_code)code_int;
520 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
521 PATTERN cannot be split. Otherwise, it returns an insn sequence.
522 This is a wrapper around split_insns which ensures that the
523 reg_stat vector is made larger if the splitter creates a new
524 register. */
526 static rtx_insn *
527 combine_split_insns (rtx pattern, rtx_insn *insn)
529 rtx_insn *ret;
530 unsigned int nregs;
532 ret = split_insns (pattern, insn);
533 nregs = max_reg_num ();
534 if (nregs > reg_stat.length ())
535 reg_stat.safe_grow_cleared (nregs, true);
536 return ret;
539 /* This is used by find_single_use to locate an rtx in LOC that
540 contains exactly one use of DEST, which is typically a REG.
541 It returns a pointer to the innermost rtx expression
542 containing DEST. Appearances of DEST that are being used to
543 totally replace it are not counted. */
545 static rtx *
546 find_single_use_1 (rtx dest, rtx *loc)
548 rtx x = *loc;
549 enum rtx_code code = GET_CODE (x);
550 rtx *result = NULL;
551 rtx *this_result;
552 int i;
553 const char *fmt;
555 switch (code)
557 case CONST:
558 case LABEL_REF:
559 case SYMBOL_REF:
560 CASE_CONST_ANY:
561 case CLOBBER:
562 return 0;
564 case SET:
565 /* If the destination is anything other than PC, a REG or a SUBREG
566 of a REG that occupies all of the REG, the insn uses DEST if
567 it is mentioned in the destination or the source. Otherwise, we
568 need just check the source. */
569 if (GET_CODE (SET_DEST (x)) != PC
570 && !REG_P (SET_DEST (x))
571 && ! (GET_CODE (SET_DEST (x)) == SUBREG
572 && REG_P (SUBREG_REG (SET_DEST (x)))
573 && !read_modify_subreg_p (SET_DEST (x))))
574 break;
576 return find_single_use_1 (dest, &SET_SRC (x));
578 case MEM:
579 case SUBREG:
580 return find_single_use_1 (dest, &XEXP (x, 0));
582 default:
583 break;
586 /* If it wasn't one of the common cases above, check each expression and
587 vector of this code. Look for a unique usage of DEST. */
589 fmt = GET_RTX_FORMAT (code);
590 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
592 if (fmt[i] == 'e')
594 if (dest == XEXP (x, i)
595 || (REG_P (dest) && REG_P (XEXP (x, i))
596 && REGNO (dest) == REGNO (XEXP (x, i))))
597 this_result = loc;
598 else
599 this_result = find_single_use_1 (dest, &XEXP (x, i));
601 if (result == NULL)
602 result = this_result;
603 else if (this_result)
604 /* Duplicate usage. */
605 return NULL;
607 else if (fmt[i] == 'E')
609 int j;
611 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
613 if (XVECEXP (x, i, j) == dest
614 || (REG_P (dest)
615 && REG_P (XVECEXP (x, i, j))
616 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
617 this_result = loc;
618 else
619 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
621 if (result == NULL)
622 result = this_result;
623 else if (this_result)
624 return NULL;
629 return result;
633 /* See if DEST, produced in INSN, is used only a single time in the
634 sequel. If so, return a pointer to the innermost rtx expression in which
635 it is used.
637 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
639 Otherwise, we find the single use by finding an insn that has a
640 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
641 only referenced once in that insn, we know that it must be the first
642 and last insn referencing DEST. */
644 static rtx *
645 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
647 basic_block bb;
648 rtx_insn *next;
649 rtx *result;
650 struct insn_link *link;
652 if (!REG_P (dest))
653 return 0;
655 bb = BLOCK_FOR_INSN (insn);
656 for (next = NEXT_INSN (insn);
657 next && BLOCK_FOR_INSN (next) == bb;
658 next = NEXT_INSN (next))
659 if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
661 FOR_EACH_LOG_LINK (link, next)
662 if (link->insn == insn && link->regno == REGNO (dest))
663 break;
665 if (link)
667 result = find_single_use_1 (dest, &PATTERN (next));
668 if (ploc)
669 *ploc = next;
670 return result;
674 return 0;
677 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
678 insn. The substitution can be undone by undo_all. If INTO is already
679 set to NEWVAL, do not record this change. Because computing NEWVAL might
680 also call SUBST, we have to compute it before we put anything into
681 the undo table. */
683 static void
684 do_SUBST (rtx *into, rtx newval)
686 struct undo *buf;
687 rtx oldval = *into;
689 if (oldval == newval)
690 return;
692 /* We'd like to catch as many invalid transformations here as
693 possible. Unfortunately, there are way too many mode changes
694 that are perfectly valid, so we'd waste too much effort for
695 little gain doing the checks here. Focus on catching invalid
696 transformations involving integer constants. */
697 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
698 && CONST_INT_P (newval))
700 /* Sanity check that we're replacing oldval with a CONST_INT
701 that is a valid sign-extension for the original mode. */
702 gcc_assert (INTVAL (newval)
703 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
705 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
706 CONST_INT is not valid, because after the replacement, the
707 original mode would be gone. Unfortunately, we can't tell
708 when do_SUBST is called to replace the operand thereof, so we
709 perform this test on oldval instead, checking whether an
710 invalid replacement took place before we got here. */
711 gcc_assert (!(GET_CODE (oldval) == SUBREG
712 && CONST_INT_P (SUBREG_REG (oldval))));
713 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
714 && CONST_INT_P (XEXP (oldval, 0))));
717 if (undobuf.frees)
718 buf = undobuf.frees, undobuf.frees = buf->next;
719 else
720 buf = XNEW (struct undo);
722 buf->kind = UNDO_RTX;
723 buf->where.r = into;
724 buf->old_contents.r = oldval;
725 *into = newval;
727 buf->next = undobuf.undos, undobuf.undos = buf;
730 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
732 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
733 for the value of a HOST_WIDE_INT value (including CONST_INT) is
734 not safe. */
736 static void
737 do_SUBST_INT (int *into, int newval)
739 struct undo *buf;
740 int oldval = *into;
742 if (oldval == newval)
743 return;
745 if (undobuf.frees)
746 buf = undobuf.frees, undobuf.frees = buf->next;
747 else
748 buf = XNEW (struct undo);
750 buf->kind = UNDO_INT;
751 buf->where.i = into;
752 buf->old_contents.i = oldval;
753 *into = newval;
755 buf->next = undobuf.undos, undobuf.undos = buf;
758 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
760 /* Similar to SUBST, but just substitute the mode. This is used when
761 changing the mode of a pseudo-register, so that any other
762 references to the entry in the regno_reg_rtx array will change as
763 well. */
765 static void
766 subst_mode (int regno, machine_mode newval)
768 struct undo *buf;
769 rtx reg = regno_reg_rtx[regno];
770 machine_mode oldval = GET_MODE (reg);
772 if (oldval == newval)
773 return;
775 if (undobuf.frees)
776 buf = undobuf.frees, undobuf.frees = buf->next;
777 else
778 buf = XNEW (struct undo);
780 buf->kind = UNDO_MODE;
781 buf->where.regno = regno;
782 buf->old_contents.m = oldval;
783 adjust_reg_mode (reg, newval);
785 buf->next = undobuf.undos, undobuf.undos = buf;
788 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
790 static void
791 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
793 struct undo *buf;
794 struct insn_link * oldval = *into;
796 if (oldval == newval)
797 return;
799 if (undobuf.frees)
800 buf = undobuf.frees, undobuf.frees = buf->next;
801 else
802 buf = XNEW (struct undo);
804 buf->kind = UNDO_LINKS;
805 buf->where.l = into;
806 buf->old_contents.l = oldval;
807 *into = newval;
809 buf->next = undobuf.undos, undobuf.undos = buf;
812 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
814 /* Subroutine of try_combine. Determine whether the replacement patterns
815 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_cost
816 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
817 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
818 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
819 of all the instructions can be estimated and the replacements are more
820 expensive than the original sequence. */
822 static bool
823 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
824 rtx newpat, rtx newi2pat, rtx newotherpat)
826 int i0_cost, i1_cost, i2_cost, i3_cost;
827 int new_i2_cost, new_i3_cost;
828 int old_cost, new_cost;
830 /* Lookup the original insn_costs. */
831 i2_cost = INSN_COST (i2);
832 i3_cost = INSN_COST (i3);
834 if (i1)
836 i1_cost = INSN_COST (i1);
837 if (i0)
839 i0_cost = INSN_COST (i0);
840 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
841 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
843 else
845 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
846 ? i1_cost + i2_cost + i3_cost : 0);
847 i0_cost = 0;
850 else
852 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
853 i1_cost = i0_cost = 0;
856 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
857 correct that. */
858 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
859 old_cost -= i1_cost;
862 /* Calculate the replacement insn_costs. */
863 rtx tmp = PATTERN (i3);
864 PATTERN (i3) = newpat;
865 int tmpi = INSN_CODE (i3);
866 INSN_CODE (i3) = -1;
867 new_i3_cost = insn_cost (i3, optimize_this_for_speed_p);
868 PATTERN (i3) = tmp;
869 INSN_CODE (i3) = tmpi;
870 if (newi2pat)
872 tmp = PATTERN (i2);
873 PATTERN (i2) = newi2pat;
874 tmpi = INSN_CODE (i2);
875 INSN_CODE (i2) = -1;
876 new_i2_cost = insn_cost (i2, optimize_this_for_speed_p);
877 PATTERN (i2) = tmp;
878 INSN_CODE (i2) = tmpi;
879 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
880 ? new_i2_cost + new_i3_cost : 0;
882 else
884 new_cost = new_i3_cost;
885 new_i2_cost = 0;
888 if (undobuf.other_insn)
890 int old_other_cost, new_other_cost;
892 old_other_cost = INSN_COST (undobuf.other_insn);
893 tmp = PATTERN (undobuf.other_insn);
894 PATTERN (undobuf.other_insn) = newotherpat;
895 tmpi = INSN_CODE (undobuf.other_insn);
896 INSN_CODE (undobuf.other_insn) = -1;
897 new_other_cost = insn_cost (undobuf.other_insn,
898 optimize_this_for_speed_p);
899 PATTERN (undobuf.other_insn) = tmp;
900 INSN_CODE (undobuf.other_insn) = tmpi;
901 if (old_other_cost > 0 && new_other_cost > 0)
903 old_cost += old_other_cost;
904 new_cost += new_other_cost;
906 else
907 old_cost = 0;
910 /* Disallow this combination if both new_cost and old_cost are greater than
911 zero, and new_cost is greater than old cost. */
912 bool reject = old_cost > 0 && new_cost > old_cost;
914 if (dump_file)
916 fprintf (dump_file, "%s combination of insns ",
917 reject ? "rejecting" : "allowing");
918 if (i0)
919 fprintf (dump_file, "%d, ", INSN_UID (i0));
920 if (i1 && INSN_UID (i1) != INSN_UID (i2))
921 fprintf (dump_file, "%d, ", INSN_UID (i1));
922 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
924 fprintf (dump_file, "original costs ");
925 if (i0)
926 fprintf (dump_file, "%d + ", i0_cost);
927 if (i1 && INSN_UID (i1) != INSN_UID (i2))
928 fprintf (dump_file, "%d + ", i1_cost);
929 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
931 if (newi2pat)
932 fprintf (dump_file, "replacement costs %d + %d = %d\n",
933 new_i2_cost, new_i3_cost, new_cost);
934 else
935 fprintf (dump_file, "replacement cost %d\n", new_cost);
938 if (reject)
939 return false;
941 /* Update the uid_insn_cost array with the replacement costs. */
942 INSN_COST (i2) = new_i2_cost;
943 INSN_COST (i3) = new_i3_cost;
944 if (i1)
946 INSN_COST (i1) = 0;
947 if (i0)
948 INSN_COST (i0) = 0;
951 return true;
955 /* Delete any insns that copy a register to itself.
956 Return true if the CFG was changed. */
958 static bool
959 delete_noop_moves (void)
961 rtx_insn *insn, *next;
962 basic_block bb;
964 bool edges_deleted = false;
966 FOR_EACH_BB_FN (bb, cfun)
968 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
970 next = NEXT_INSN (insn);
971 if (INSN_P (insn) && noop_move_p (insn))
973 if (dump_file)
974 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
976 edges_deleted |= delete_insn_and_edges (insn);
981 return edges_deleted;
985 /* Return false if we do not want to (or cannot) combine DEF. */
986 static bool
987 can_combine_def_p (df_ref def)
989 /* Do not consider if it is pre/post modification in MEM. */
990 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
991 return false;
993 unsigned int regno = DF_REF_REGNO (def);
995 /* Do not combine frame pointer adjustments. */
996 if ((regno == FRAME_POINTER_REGNUM
997 && (!reload_completed || frame_pointer_needed))
998 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
999 && regno == HARD_FRAME_POINTER_REGNUM
1000 && (!reload_completed || frame_pointer_needed))
1001 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1002 && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1003 return false;
1005 return true;
1008 /* Return false if we do not want to (or cannot) combine USE. */
1009 static bool
1010 can_combine_use_p (df_ref use)
1012 /* Do not consider the usage of the stack pointer by function call. */
1013 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1014 return false;
1016 return true;
1019 /* Fill in log links field for all insns. */
1021 static void
1022 create_log_links (void)
1024 basic_block bb;
1025 rtx_insn **next_use;
1026 rtx_insn *insn;
1027 df_ref def, use;
1029 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1031 /* Pass through each block from the end, recording the uses of each
1032 register and establishing log links when def is encountered.
1033 Note that we do not clear next_use array in order to save time,
1034 so we have to test whether the use is in the same basic block as def.
1036 There are a few cases below when we do not consider the definition or
1037 usage -- these are taken from original flow.c did. Don't ask me why it is
1038 done this way; I don't know and if it works, I don't want to know. */
1040 FOR_EACH_BB_FN (bb, cfun)
1042 FOR_BB_INSNS_REVERSE (bb, insn)
1044 if (!NONDEBUG_INSN_P (insn))
1045 continue;
1047 /* Log links are created only once. */
1048 gcc_assert (!LOG_LINKS (insn));
1050 FOR_EACH_INSN_DEF (def, insn)
1052 unsigned int regno = DF_REF_REGNO (def);
1053 rtx_insn *use_insn;
1055 if (!next_use[regno])
1056 continue;
1058 if (!can_combine_def_p (def))
1059 continue;
1061 use_insn = next_use[regno];
1062 next_use[regno] = NULL;
1064 if (BLOCK_FOR_INSN (use_insn) != bb)
1065 continue;
1067 /* flow.c claimed:
1069 We don't build a LOG_LINK for hard registers contained
1070 in ASM_OPERANDs. If these registers get replaced,
1071 we might wind up changing the semantics of the insn,
1072 even if reload can make what appear to be valid
1073 assignments later. */
1074 if (regno < FIRST_PSEUDO_REGISTER
1075 && asm_noperands (PATTERN (use_insn)) >= 0)
1076 continue;
1078 /* Don't add duplicate links between instructions. */
1079 struct insn_link *links;
1080 FOR_EACH_LOG_LINK (links, use_insn)
1081 if (insn == links->insn && regno == links->regno)
1082 break;
1084 if (!links)
1085 LOG_LINKS (use_insn)
1086 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1089 FOR_EACH_INSN_USE (use, insn)
1090 if (can_combine_use_p (use))
1091 next_use[DF_REF_REGNO (use)] = insn;
1095 free (next_use);
1098 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1099 true if we found a LOG_LINK that proves that A feeds B. This only works
1100 if there are no instructions between A and B which could have a link
1101 depending on A, since in that case we would not record a link for B. */
1103 static bool
1104 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1106 struct insn_link *links;
1107 FOR_EACH_LOG_LINK (links, b)
1108 if (links->insn == a)
1109 return true;
1110 return false;
1113 /* Main entry point for combiner. F is the first insn of the function.
1114 NREGS is the first unused pseudo-reg number.
1116 Return nonzero if the CFG was changed (e.g. if the combiner has
1117 turned an indirect jump instruction into a direct jump). */
1118 static bool
1119 combine_instructions (rtx_insn *f, unsigned int nregs)
1121 rtx_insn *insn, *next;
1122 struct insn_link *links, *nextlinks;
1123 rtx_insn *first;
1124 basic_block last_bb;
1126 bool new_direct_jump_p = false;
1128 for (first = f; first && !NONDEBUG_INSN_P (first); )
1129 first = NEXT_INSN (first);
1130 if (!first)
1131 return false;
1133 combine_attempts = 0;
1134 combine_merges = 0;
1135 combine_extras = 0;
1136 combine_successes = 0;
1138 rtl_hooks = combine_rtl_hooks;
1140 reg_stat.safe_grow_cleared (nregs, true);
1142 init_recog_no_volatile ();
1144 /* Allocate array for insn info. */
1145 max_uid_known = get_max_uid ();
1146 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1147 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1148 gcc_obstack_init (&insn_link_obstack);
1150 nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1152 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1153 problems when, for example, we have j <<= 1 in a loop. */
1155 nonzero_sign_valid = 0;
1156 label_tick = label_tick_ebb_start = 1;
1158 /* Scan all SETs and see if we can deduce anything about what
1159 bits are known to be zero for some registers and how many copies
1160 of the sign bit are known to exist for those registers.
1162 Also set any known values so that we can use it while searching
1163 for what bits are known to be set. */
1165 setup_incoming_promotions (first);
1166 /* Allow the entry block and the first block to fall into the same EBB.
1167 Conceptually the incoming promotions are assigned to the entry block. */
1168 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1170 create_log_links ();
1171 FOR_EACH_BB_FN (this_basic_block, cfun)
1173 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1174 last_call_luid = 0;
1175 mem_last_set = -1;
1177 label_tick++;
1178 if (!single_pred_p (this_basic_block)
1179 || single_pred (this_basic_block) != last_bb)
1180 label_tick_ebb_start = label_tick;
1181 last_bb = this_basic_block;
1183 FOR_BB_INSNS (this_basic_block, insn)
1184 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1186 rtx links;
1188 subst_low_luid = DF_INSN_LUID (insn);
1189 subst_insn = insn;
1191 note_stores (insn, set_nonzero_bits_and_sign_copies, insn);
1192 record_dead_and_set_regs (insn);
1194 if (AUTO_INC_DEC)
1195 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1196 if (REG_NOTE_KIND (links) == REG_INC)
1197 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1198 insn);
1200 /* Record the current insn_cost of this instruction. */
1201 INSN_COST (insn) = insn_cost (insn, optimize_this_for_speed_p);
1202 if (dump_file)
1204 fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1205 dump_insn_slim (dump_file, insn);
1210 nonzero_sign_valid = 1;
1212 /* Now scan all the insns in forward order. */
1213 label_tick = label_tick_ebb_start = 1;
1214 init_reg_last ();
1215 setup_incoming_promotions (first);
1216 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1217 int max_combine = param_max_combine_insns;
1219 FOR_EACH_BB_FN (this_basic_block, cfun)
1221 rtx_insn *last_combined_insn = NULL;
1223 /* Ignore instruction combination in basic blocks that are going to
1224 be removed as unreachable anyway. See PR82386. */
1225 if (EDGE_COUNT (this_basic_block->preds) == 0)
1226 continue;
1228 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1229 last_call_luid = 0;
1230 mem_last_set = -1;
1232 label_tick++;
1233 if (!single_pred_p (this_basic_block)
1234 || single_pred (this_basic_block) != last_bb)
1235 label_tick_ebb_start = label_tick;
1236 last_bb = this_basic_block;
1238 rtl_profile_for_bb (this_basic_block);
1239 for (insn = BB_HEAD (this_basic_block);
1240 insn != NEXT_INSN (BB_END (this_basic_block));
1241 insn = next ? next : NEXT_INSN (insn))
1243 next = 0;
1244 if (!NONDEBUG_INSN_P (insn))
1245 continue;
1247 while (last_combined_insn
1248 && (!NONDEBUG_INSN_P (last_combined_insn)
1249 || last_combined_insn->deleted ()))
1250 last_combined_insn = PREV_INSN (last_combined_insn);
1251 if (last_combined_insn == NULL_RTX
1252 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1253 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1254 last_combined_insn = insn;
1256 /* See if we know about function return values before this
1257 insn based upon SUBREG flags. */
1258 check_promoted_subreg (insn, PATTERN (insn));
1260 /* See if we can find hardregs and subreg of pseudos in
1261 narrower modes. This could help turning TRUNCATEs
1262 into SUBREGs. */
1263 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1265 /* Try this insn with each insn it links back to. */
1267 FOR_EACH_LOG_LINK (links, insn)
1268 if ((next = try_combine (insn, links->insn, NULL,
1269 NULL, &new_direct_jump_p,
1270 last_combined_insn)) != 0)
1272 statistics_counter_event (cfun, "two-insn combine", 1);
1273 goto retry;
1276 /* Try each sequence of three linked insns ending with this one. */
1278 if (max_combine >= 3)
1279 FOR_EACH_LOG_LINK (links, insn)
1281 rtx_insn *link = links->insn;
1283 /* If the linked insn has been replaced by a note, then there
1284 is no point in pursuing this chain any further. */
1285 if (NOTE_P (link))
1286 continue;
1288 FOR_EACH_LOG_LINK (nextlinks, link)
1289 if ((next = try_combine (insn, link, nextlinks->insn,
1290 NULL, &new_direct_jump_p,
1291 last_combined_insn)) != 0)
1293 statistics_counter_event (cfun, "three-insn combine", 1);
1294 goto retry;
1298 /* Try combining an insn with two different insns whose results it
1299 uses. */
1300 if (max_combine >= 3)
1301 FOR_EACH_LOG_LINK (links, insn)
1302 for (nextlinks = links->next; nextlinks;
1303 nextlinks = nextlinks->next)
1304 if ((next = try_combine (insn, links->insn,
1305 nextlinks->insn, NULL,
1306 &new_direct_jump_p,
1307 last_combined_insn)) != 0)
1310 statistics_counter_event (cfun, "three-insn combine", 1);
1311 goto retry;
1314 /* Try four-instruction combinations. */
1315 if (max_combine >= 4)
1316 FOR_EACH_LOG_LINK (links, insn)
1318 struct insn_link *next1;
1319 rtx_insn *link = links->insn;
1321 /* If the linked insn has been replaced by a note, then there
1322 is no point in pursuing this chain any further. */
1323 if (NOTE_P (link))
1324 continue;
1326 FOR_EACH_LOG_LINK (next1, link)
1328 rtx_insn *link1 = next1->insn;
1329 if (NOTE_P (link1))
1330 continue;
1331 /* I0 -> I1 -> I2 -> I3. */
1332 FOR_EACH_LOG_LINK (nextlinks, link1)
1333 if ((next = try_combine (insn, link, link1,
1334 nextlinks->insn,
1335 &new_direct_jump_p,
1336 last_combined_insn)) != 0)
1338 statistics_counter_event (cfun, "four-insn combine", 1);
1339 goto retry;
1341 /* I0, I1 -> I2, I2 -> I3. */
1342 for (nextlinks = next1->next; nextlinks;
1343 nextlinks = nextlinks->next)
1344 if ((next = try_combine (insn, link, link1,
1345 nextlinks->insn,
1346 &new_direct_jump_p,
1347 last_combined_insn)) != 0)
1349 statistics_counter_event (cfun, "four-insn combine", 1);
1350 goto retry;
1354 for (next1 = links->next; next1; next1 = next1->next)
1356 rtx_insn *link1 = next1->insn;
1357 if (NOTE_P (link1))
1358 continue;
1359 /* I0 -> I2; I1, I2 -> I3. */
1360 FOR_EACH_LOG_LINK (nextlinks, link)
1361 if ((next = try_combine (insn, link, link1,
1362 nextlinks->insn,
1363 &new_direct_jump_p,
1364 last_combined_insn)) != 0)
1366 statistics_counter_event (cfun, "four-insn combine", 1);
1367 goto retry;
1369 /* I0 -> I1; I1, I2 -> I3. */
1370 FOR_EACH_LOG_LINK (nextlinks, link1)
1371 if ((next = try_combine (insn, link, link1,
1372 nextlinks->insn,
1373 &new_direct_jump_p,
1374 last_combined_insn)) != 0)
1376 statistics_counter_event (cfun, "four-insn combine", 1);
1377 goto retry;
1382 /* Try this insn with each REG_EQUAL note it links back to. */
1383 FOR_EACH_LOG_LINK (links, insn)
1385 rtx set, note;
1386 rtx_insn *temp = links->insn;
1387 if ((set = single_set (temp)) != 0
1388 && (note = find_reg_equal_equiv_note (temp)) != 0
1389 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1390 && ! side_effects_p (SET_SRC (set))
1391 /* Avoid using a register that may already been marked
1392 dead by an earlier instruction. */
1393 && ! unmentioned_reg_p (note, SET_SRC (set))
1394 && (GET_MODE (note) == VOIDmode
1395 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1396 : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1397 && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1398 || (GET_MODE (XEXP (SET_DEST (set), 0))
1399 == GET_MODE (note))))))
1401 /* Temporarily replace the set's source with the
1402 contents of the REG_EQUAL note. The insn will
1403 be deleted or recognized by try_combine. */
1404 rtx orig_src = SET_SRC (set);
1405 rtx orig_dest = SET_DEST (set);
1406 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1407 SET_DEST (set) = XEXP (SET_DEST (set), 0);
1408 SET_SRC (set) = note;
1409 i2mod = temp;
1410 i2mod_old_rhs = copy_rtx (orig_src);
1411 i2mod_new_rhs = copy_rtx (note);
1412 next = try_combine (insn, i2mod, NULL, NULL,
1413 &new_direct_jump_p,
1414 last_combined_insn);
1415 i2mod = NULL;
1416 if (next)
1418 statistics_counter_event (cfun, "insn-with-note combine", 1);
1419 goto retry;
1421 INSN_CODE (temp) = -1;
1422 SET_SRC (set) = orig_src;
1423 SET_DEST (set) = orig_dest;
1427 if (!NOTE_P (insn))
1428 record_dead_and_set_regs (insn);
1430 retry:
1435 default_rtl_profile ();
1436 clear_bb_flags ();
1438 if (purge_all_dead_edges ())
1439 new_direct_jump_p = true;
1440 if (delete_noop_moves ())
1441 new_direct_jump_p = true;
1443 /* Clean up. */
1444 obstack_free (&insn_link_obstack, NULL);
1445 free (uid_log_links);
1446 free (uid_insn_cost);
1447 reg_stat.release ();
1450 struct undo *undo, *next;
1451 for (undo = undobuf.frees; undo; undo = next)
1453 next = undo->next;
1454 free (undo);
1456 undobuf.frees = 0;
1459 total_attempts += combine_attempts;
1460 total_merges += combine_merges;
1461 total_extras += combine_extras;
1462 total_successes += combine_successes;
1464 nonzero_sign_valid = 0;
1465 rtl_hooks = general_rtl_hooks;
1467 /* Make recognizer allow volatile MEMs again. */
1468 init_recog ();
1470 return new_direct_jump_p;
1473 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1475 static void
1476 init_reg_last (void)
1478 unsigned int i;
1479 reg_stat_type *p;
1481 FOR_EACH_VEC_ELT (reg_stat, i, p)
1482 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1485 /* Set up any promoted values for incoming argument registers. */
1487 static void
1488 setup_incoming_promotions (rtx_insn *first)
1490 tree arg;
1491 bool strictly_local = false;
1493 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1494 arg = DECL_CHAIN (arg))
1496 rtx x, reg = DECL_INCOMING_RTL (arg);
1497 int uns1, uns3;
1498 machine_mode mode1, mode2, mode3, mode4;
1500 /* Only continue if the incoming argument is in a register. */
1501 if (!REG_P (reg))
1502 continue;
1504 /* Determine, if possible, whether all call sites of the current
1505 function lie within the current compilation unit. (This does
1506 take into account the exporting of a function via taking its
1507 address, and so forth.) */
1508 strictly_local
1509 = cgraph_node::local_info_node (current_function_decl)->local;
1511 /* The mode and signedness of the argument before any promotions happen
1512 (equal to the mode of the pseudo holding it at that stage). */
1513 mode1 = TYPE_MODE (TREE_TYPE (arg));
1514 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1516 /* The mode and signedness of the argument after any source language and
1517 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1518 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1519 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1521 /* The mode and signedness of the argument as it is actually passed,
1522 see assign_parm_setup_reg in function.cc. */
1523 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1524 TREE_TYPE (cfun->decl), 0);
1526 /* The mode of the register in which the argument is being passed. */
1527 mode4 = GET_MODE (reg);
1529 /* Eliminate sign extensions in the callee when:
1530 (a) A mode promotion has occurred; */
1531 if (mode1 == mode3)
1532 continue;
1533 /* (b) The mode of the register is the same as the mode of
1534 the argument as it is passed; */
1535 if (mode3 != mode4)
1536 continue;
1537 /* (c) There's no language level extension; */
1538 if (mode1 == mode2)
1540 /* (c.1) All callers are from the current compilation unit. If that's
1541 the case we don't have to rely on an ABI, we only have to know
1542 what we're generating right now, and we know that we will do the
1543 mode1 to mode2 promotion with the given sign. */
1544 else if (!strictly_local)
1545 continue;
1546 /* (c.2) The combination of the two promotions is useful. This is
1547 true when the signs match, or if the first promotion is unsigned.
1548 In the later case, (sign_extend (zero_extend x)) is the same as
1549 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1550 else if (uns1)
1551 uns3 = true;
1552 else if (uns3)
1553 continue;
1555 /* Record that the value was promoted from mode1 to mode3,
1556 so that any sign extension at the head of the current
1557 function may be eliminated. */
1558 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1559 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1560 record_value_for_reg (reg, first, x);
1564 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1565 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1566 because some machines (maybe most) will actually do the sign-extension and
1567 this is the conservative approach.
1569 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1570 kludge. */
1572 static rtx
1573 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1575 scalar_int_mode int_mode;
1576 if (CONST_INT_P (src)
1577 && is_a <scalar_int_mode> (mode, &int_mode)
1578 && GET_MODE_PRECISION (int_mode) < prec
1579 && INTVAL (src) > 0
1580 && val_signbit_known_set_p (int_mode, INTVAL (src)))
1581 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1583 return src;
1586 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1587 and SET. */
1589 static void
1590 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1591 rtx x)
1593 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1594 unsigned HOST_WIDE_INT bits = 0;
1595 rtx reg_equal = NULL, src = SET_SRC (set);
1596 unsigned int num = 0;
1598 if (reg_equal_note)
1599 reg_equal = XEXP (reg_equal_note, 0);
1601 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1603 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1604 if (reg_equal)
1605 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1608 /* Don't call nonzero_bits if it cannot change anything. */
1609 if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1611 machine_mode mode = GET_MODE (x);
1612 if (GET_MODE_CLASS (mode) == MODE_INT
1613 && HWI_COMPUTABLE_MODE_P (mode))
1614 mode = nonzero_bits_mode;
1615 bits = nonzero_bits (src, mode);
1616 if (reg_equal && bits)
1617 bits &= nonzero_bits (reg_equal, mode);
1618 rsp->nonzero_bits |= bits;
1621 /* Don't call num_sign_bit_copies if it cannot change anything. */
1622 if (rsp->sign_bit_copies != 1)
1624 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1625 if (reg_equal && maybe_ne (num, GET_MODE_PRECISION (GET_MODE (x))))
1627 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1628 if (num == 0 || numeq > num)
1629 num = numeq;
1631 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1632 rsp->sign_bit_copies = num;
1636 /* Called via note_stores. If X is a pseudo that is narrower than
1637 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1639 If we are setting only a portion of X and we can't figure out what
1640 portion, assume all bits will be used since we don't know what will
1641 be happening.
1643 Similarly, set how many bits of X are known to be copies of the sign bit
1644 at all locations in the function. This is the smallest number implied
1645 by any set of X. */
1647 static void
1648 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1650 rtx_insn *insn = (rtx_insn *) data;
1651 scalar_int_mode mode;
1653 if (REG_P (x)
1654 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1655 /* If this register is undefined at the start of the file, we can't
1656 say what its contents were. */
1657 && ! REGNO_REG_SET_P
1658 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1659 && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1660 && HWI_COMPUTABLE_MODE_P (mode))
1662 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1664 if (set == 0 || GET_CODE (set) == CLOBBER)
1666 rsp->nonzero_bits = GET_MODE_MASK (mode);
1667 rsp->sign_bit_copies = 1;
1668 return;
1671 /* If this register is being initialized using itself, and the
1672 register is uninitialized in this basic block, and there are
1673 no LOG_LINKS which set the register, then part of the
1674 register is uninitialized. In that case we can't assume
1675 anything about the number of nonzero bits.
1677 ??? We could do better if we checked this in
1678 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1679 could avoid making assumptions about the insn which initially
1680 sets the register, while still using the information in other
1681 insns. We would have to be careful to check every insn
1682 involved in the combination. */
1684 if (insn
1685 && reg_referenced_p (x, PATTERN (insn))
1686 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1687 REGNO (x)))
1689 struct insn_link *link;
1691 FOR_EACH_LOG_LINK (link, insn)
1692 if (dead_or_set_p (link->insn, x))
1693 break;
1694 if (!link)
1696 rsp->nonzero_bits = GET_MODE_MASK (mode);
1697 rsp->sign_bit_copies = 1;
1698 return;
1702 /* If this is a complex assignment, see if we can convert it into a
1703 simple assignment. */
1704 set = expand_field_assignment (set);
1706 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1707 set what we know about X. */
1709 if (SET_DEST (set) == x
1710 || (paradoxical_subreg_p (SET_DEST (set))
1711 && SUBREG_REG (SET_DEST (set)) == x))
1712 update_rsp_from_reg_equal (rsp, insn, set, x);
1713 else
1715 rsp->nonzero_bits = GET_MODE_MASK (mode);
1716 rsp->sign_bit_copies = 1;
1721 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1722 optionally insns that were previously combined into I3 or that will be
1723 combined into the merger of INSN and I3. The order is PRED, PRED2,
1724 INSN, SUCC, SUCC2, I3.
1726 Return false if the combination is not allowed for any reason.
1728 If the combination is allowed, *PDEST will be set to the single
1729 destination of INSN and *PSRC to the single source, and this function
1730 will return true. */
1732 static bool
1733 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1734 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1735 rtx *pdest, rtx *psrc)
1737 int i;
1738 const_rtx set = 0;
1739 rtx src, dest;
1740 rtx_insn *p;
1741 rtx link;
1742 bool all_adjacent = true;
1743 bool (*is_volatile_p) (const_rtx);
1745 if (succ)
1747 if (succ2)
1749 if (next_active_insn (succ2) != i3)
1750 all_adjacent = false;
1751 if (next_active_insn (succ) != succ2)
1752 all_adjacent = false;
1754 else if (next_active_insn (succ) != i3)
1755 all_adjacent = false;
1756 if (next_active_insn (insn) != succ)
1757 all_adjacent = false;
1759 else if (next_active_insn (insn) != i3)
1760 all_adjacent = false;
1762 /* Can combine only if previous insn is a SET of a REG or a SUBREG,
1763 or a PARALLEL consisting of such a SET and CLOBBERs.
1765 If INSN has CLOBBER parallel parts, ignore them for our processing.
1766 By definition, these happen during the execution of the insn. When it
1767 is merged with another insn, all bets are off. If they are, in fact,
1768 needed and aren't also supplied in I3, they may be added by
1769 recog_for_combine. Otherwise, it won't match.
1771 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1772 note.
1774 Get the source and destination of INSN. If more than one, can't
1775 combine. */
1777 if (GET_CODE (PATTERN (insn)) == SET)
1778 set = PATTERN (insn);
1779 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1780 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1782 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1784 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1786 switch (GET_CODE (elt))
1788 /* This is important to combine floating point insns
1789 for the SH4 port. */
1790 case USE:
1791 /* Combining an isolated USE doesn't make sense.
1792 We depend here on combinable_i3pat to reject them. */
1793 /* The code below this loop only verifies that the inputs of
1794 the SET in INSN do not change. We call reg_set_between_p
1795 to verify that the REG in the USE does not change between
1796 I3 and INSN.
1797 If the USE in INSN was for a pseudo register, the matching
1798 insn pattern will likely match any register; combining this
1799 with any other USE would only be safe if we knew that the
1800 used registers have identical values, or if there was
1801 something to tell them apart, e.g. different modes. For
1802 now, we forgo such complicated tests and simply disallow
1803 combining of USES of pseudo registers with any other USE. */
1804 if (REG_P (XEXP (elt, 0))
1805 && GET_CODE (PATTERN (i3)) == PARALLEL)
1807 rtx i3pat = PATTERN (i3);
1808 int i = XVECLEN (i3pat, 0) - 1;
1809 unsigned int regno = REGNO (XEXP (elt, 0));
1813 rtx i3elt = XVECEXP (i3pat, 0, i);
1815 if (GET_CODE (i3elt) == USE
1816 && REG_P (XEXP (i3elt, 0))
1817 && (REGNO (XEXP (i3elt, 0)) == regno
1818 ? reg_set_between_p (XEXP (elt, 0),
1819 PREV_INSN (insn), i3)
1820 : regno >= FIRST_PSEUDO_REGISTER))
1821 return false;
1823 while (--i >= 0);
1825 break;
1827 /* We can ignore CLOBBERs. */
1828 case CLOBBER:
1829 break;
1831 case SET:
1832 /* Ignore SETs whose result isn't used but not those that
1833 have side-effects. */
1834 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1835 && insn_nothrow_p (insn)
1836 && !side_effects_p (elt))
1837 break;
1839 /* If we have already found a SET, this is a second one and
1840 so we cannot combine with this insn. */
1841 if (set)
1842 return false;
1844 set = elt;
1845 break;
1847 default:
1848 /* Anything else means we can't combine. */
1849 return false;
1853 if (set == 0
1854 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1855 so don't do anything with it. */
1856 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1857 return false;
1859 else
1860 return false;
1862 if (set == 0)
1863 return false;
1865 /* The simplification in expand_field_assignment may call back to
1866 get_last_value, so set safe guard here. */
1867 subst_low_luid = DF_INSN_LUID (insn);
1869 set = expand_field_assignment (set);
1870 src = SET_SRC (set), dest = SET_DEST (set);
1872 /* Do not eliminate user-specified register if it is in an
1873 asm input because we may break the register asm usage defined
1874 in GCC manual if allow to do so.
1875 Be aware that this may cover more cases than we expect but this
1876 should be harmless. */
1877 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1878 && extract_asm_operands (PATTERN (i3)))
1879 return false;
1881 /* Don't eliminate a store in the stack pointer. */
1882 if (dest == stack_pointer_rtx
1883 /* Don't combine with an insn that sets a register to itself if it has
1884 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1885 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1886 /* Can't merge an ASM_OPERANDS. */
1887 || GET_CODE (src) == ASM_OPERANDS
1888 /* Can't merge a function call. */
1889 || GET_CODE (src) == CALL
1890 /* Don't eliminate a function call argument. */
1891 || (CALL_P (i3)
1892 && (find_reg_fusage (i3, USE, dest)
1893 || (REG_P (dest)
1894 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1895 && global_regs[REGNO (dest)])))
1896 /* Don't substitute into an incremented register. */
1897 || FIND_REG_INC_NOTE (i3, dest)
1898 || (succ && FIND_REG_INC_NOTE (succ, dest))
1899 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1900 /* Don't substitute into a non-local goto, this confuses CFG. */
1901 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1902 /* Make sure that DEST is not used after INSN but before SUCC, or
1903 after SUCC and before SUCC2, or after SUCC2 but before I3. */
1904 || (!all_adjacent
1905 && ((succ2
1906 && (reg_used_between_p (dest, succ2, i3)
1907 || reg_used_between_p (dest, succ, succ2)))
1908 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1909 || (!succ2 && !succ && reg_used_between_p (dest, insn, i3))
1910 || (succ
1911 /* SUCC and SUCC2 can be split halves from a PARALLEL; in
1912 that case SUCC is not in the insn stream, so use SUCC2
1913 instead for this test. */
1914 && reg_used_between_p (dest, insn,
1915 succ2
1916 && INSN_UID (succ) == INSN_UID (succ2)
1917 ? succ2 : succ))))
1918 /* Make sure that the value that is to be substituted for the register
1919 does not use any registers whose values alter in between. However,
1920 If the insns are adjacent, a use can't cross a set even though we
1921 think it might (this can happen for a sequence of insns each setting
1922 the same destination; last_set of that register might point to
1923 a NOTE). If INSN has a REG_EQUIV note, the register is always
1924 equivalent to the memory so the substitution is valid even if there
1925 are intervening stores. Also, don't move a volatile asm or
1926 UNSPEC_VOLATILE across any other insns. */
1927 || (! all_adjacent
1928 && (((!MEM_P (src)
1929 || ! find_reg_note (insn, REG_EQUIV, src))
1930 && modified_between_p (src, insn, i3))
1931 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1932 || GET_CODE (src) == UNSPEC_VOLATILE))
1933 /* Don't combine across a CALL_INSN, because that would possibly
1934 change whether the life span of some REGs crosses calls or not,
1935 and it is a pain to update that information.
1936 Exception: if source is a constant, moving it later can't hurt.
1937 Accept that as a special case. */
1938 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1939 return false;
1941 /* DEST must be a REG. */
1942 if (REG_P (dest))
1944 /* If register alignment is being enforced for multi-word items in all
1945 cases except for parameters, it is possible to have a register copy
1946 insn referencing a hard register that is not allowed to contain the
1947 mode being copied and which would not be valid as an operand of most
1948 insns. Eliminate this problem by not combining with such an insn.
1950 Also, on some machines we don't want to extend the life of a hard
1951 register. */
1953 if (REG_P (src)
1954 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1955 && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
1956 /* Don't extend the life of a hard register unless it is
1957 user variable (if we have few registers) or it can't
1958 fit into the desired register (meaning something special
1959 is going on).
1960 Also avoid substituting a return register into I3, because
1961 reload can't handle a conflict with constraints of other
1962 inputs. */
1963 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1964 && !targetm.hard_regno_mode_ok (REGNO (src),
1965 GET_MODE (src)))))
1966 return false;
1968 else
1969 return false;
1972 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1973 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1974 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1976 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1978 /* If the clobber represents an earlyclobber operand, we must not
1979 substitute an expression containing the clobbered register.
1980 As we do not analyze the constraint strings here, we have to
1981 make the conservative assumption. However, if the register is
1982 a fixed hard reg, the clobber cannot represent any operand;
1983 we leave it up to the machine description to either accept or
1984 reject use-and-clobber patterns. */
1985 if (!REG_P (reg)
1986 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1987 || !fixed_regs[REGNO (reg)])
1988 if (reg_overlap_mentioned_p (reg, src))
1989 return false;
1992 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1993 or not), reject, unless nothing volatile comes between it and I3 */
1995 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1997 /* Make sure neither succ nor succ2 contains a volatile reference. */
1998 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1999 return false;
2000 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2001 return false;
2002 /* We'll check insns between INSN and I3 below. */
2005 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2006 to be an explicit register variable, and was chosen for a reason. */
2008 if (GET_CODE (src) == ASM_OPERANDS
2009 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2010 return false;
2012 /* If INSN contains volatile references (specifically volatile MEMs),
2013 we cannot combine across any other volatile references.
2014 Even if INSN doesn't contain volatile references, any intervening
2015 volatile insn might affect machine state. */
2017 is_volatile_p = volatile_refs_p (PATTERN (insn))
2018 ? volatile_refs_p
2019 : volatile_insn_p;
2021 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2022 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2023 return false;
2025 /* If INSN contains an autoincrement or autodecrement, make sure that
2026 register is not used between there and I3, and not already used in
2027 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2028 Also insist that I3 not be a jump if using LRA; if it were one
2029 and the incremented register were spilled, we would lose.
2030 Reload handles this correctly. */
2032 if (AUTO_INC_DEC)
2033 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2034 if (REG_NOTE_KIND (link) == REG_INC
2035 && ((JUMP_P (i3) && targetm.lra_p ())
2036 || reg_used_between_p (XEXP (link, 0), insn, i3)
2037 || (pred != NULL_RTX
2038 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2039 || (pred2 != NULL_RTX
2040 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2041 || (succ != NULL_RTX
2042 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2043 || (succ2 != NULL_RTX
2044 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2045 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2046 return false;
2048 /* If we get here, we have passed all the tests and the combination is
2049 to be allowed. */
2051 *pdest = dest;
2052 *psrc = src;
2054 return true;
2057 /* LOC is the location within I3 that contains its pattern or the component
2058 of a PARALLEL of the pattern. We validate that it is valid for combining.
2060 One problem is if I3 modifies its output, as opposed to replacing it
2061 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2062 doing so would produce an insn that is not equivalent to the original insns.
2064 Consider:
2066 (set (reg:DI 101) (reg:DI 100))
2067 (set (subreg:SI (reg:DI 101) 0) <foo>)
2069 This is NOT equivalent to:
2071 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2072 (set (reg:DI 101) (reg:DI 100))])
2074 Not only does this modify 100 (in which case it might still be valid
2075 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2077 We can also run into a problem if I2 sets a register that I1
2078 uses and I1 gets directly substituted into I3 (not via I2). In that
2079 case, we would be getting the wrong value of I2DEST into I3, so we
2080 must reject the combination. This case occurs when I2 and I1 both
2081 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2082 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2083 of a SET must prevent combination from occurring. The same situation
2084 can occur for I0, in which case I0_NOT_IN_SRC is set.
2086 Before doing the above check, we first try to expand a field assignment
2087 into a set of logical operations.
2089 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2090 we place a register that is both set and used within I3. If more than one
2091 such register is detected, we fail.
2093 Return true if the combination is valid, false otherwise. */
2095 static bool
2096 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2097 bool i1_not_in_src, bool i0_not_in_src, rtx *pi3dest_killed)
2099 rtx x = *loc;
2101 if (GET_CODE (x) == SET)
2103 rtx set = x ;
2104 rtx dest = SET_DEST (set);
2105 rtx src = SET_SRC (set);
2106 rtx inner_dest = dest;
2107 rtx subdest;
2109 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2110 || GET_CODE (inner_dest) == SUBREG
2111 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2112 inner_dest = XEXP (inner_dest, 0);
2114 /* Check for the case where I3 modifies its output, as discussed
2115 above. We don't want to prevent pseudos from being combined
2116 into the address of a MEM, so only prevent the combination if
2117 i1 or i2 set the same MEM. */
2118 if ((inner_dest != dest &&
2119 (!MEM_P (inner_dest)
2120 || rtx_equal_p (i2dest, inner_dest)
2121 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2122 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2123 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2124 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2125 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2127 /* This is the same test done in can_combine_p except we can't test
2128 all_adjacent; we don't have to, since this instruction will stay
2129 in place, thus we are not considering increasing the lifetime of
2130 INNER_DEST.
2132 Also, if this insn sets a function argument, combining it with
2133 something that might need a spill could clobber a previous
2134 function argument; the all_adjacent test in can_combine_p also
2135 checks this; here, we do a more specific test for this case. */
2137 || (REG_P (inner_dest)
2138 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2139 && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2140 GET_MODE (inner_dest)))
2141 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2142 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2143 return false;
2145 /* If DEST is used in I3, it is being killed in this insn, so
2146 record that for later. We have to consider paradoxical
2147 subregs here, since they kill the whole register, but we
2148 ignore partial subregs, STRICT_LOW_PART, etc.
2149 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2150 STACK_POINTER_REGNUM, since these are always considered to be
2151 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2152 subdest = dest;
2153 if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2154 subdest = SUBREG_REG (subdest);
2155 if (pi3dest_killed
2156 && REG_P (subdest)
2157 && reg_referenced_p (subdest, PATTERN (i3))
2158 && REGNO (subdest) != FRAME_POINTER_REGNUM
2159 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2160 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2161 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2162 || (REGNO (subdest) != ARG_POINTER_REGNUM
2163 || ! fixed_regs [REGNO (subdest)]))
2164 && REGNO (subdest) != STACK_POINTER_REGNUM)
2166 if (*pi3dest_killed)
2167 return false;
2169 *pi3dest_killed = subdest;
2173 else if (GET_CODE (x) == PARALLEL)
2175 int i;
2177 for (i = 0; i < XVECLEN (x, 0); i++)
2178 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2179 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2180 return false;
2183 return true;
2186 /* Return true if X is an arithmetic expression that contains a multiplication
2187 and division. We don't count multiplications by powers of two here. */
2189 static bool
2190 contains_muldiv (rtx x)
2192 switch (GET_CODE (x))
2194 case MOD: case DIV: case UMOD: case UDIV:
2195 return true;
2197 case MULT:
2198 return ! (CONST_INT_P (XEXP (x, 1))
2199 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2200 default:
2201 if (BINARY_P (x))
2202 return contains_muldiv (XEXP (x, 0))
2203 || contains_muldiv (XEXP (x, 1));
2205 if (UNARY_P (x))
2206 return contains_muldiv (XEXP (x, 0));
2208 return false;
2212 /* Determine whether INSN can be used in a combination. Return true if
2213 not. This is used in try_combine to detect early some cases where we
2214 can't perform combinations. */
2216 static bool
2217 cant_combine_insn_p (rtx_insn *insn)
2219 rtx set;
2220 rtx src, dest;
2222 /* If this isn't really an insn, we can't do anything.
2223 This can occur when flow deletes an insn that it has merged into an
2224 auto-increment address. */
2225 if (!NONDEBUG_INSN_P (insn))
2226 return true;
2228 /* Never combine loads and stores involving hard regs that are likely
2229 to be spilled. The register allocator can usually handle such
2230 reg-reg moves by tying. If we allow the combiner to make
2231 substitutions of likely-spilled regs, reload might die.
2232 As an exception, we allow combinations involving fixed regs; these are
2233 not available to the register allocator so there's no risk involved. */
2235 set = single_set (insn);
2236 if (! set)
2237 return false;
2238 src = SET_SRC (set);
2239 dest = SET_DEST (set);
2240 if (GET_CODE (src) == SUBREG)
2241 src = SUBREG_REG (src);
2242 if (GET_CODE (dest) == SUBREG)
2243 dest = SUBREG_REG (dest);
2244 if (REG_P (src) && REG_P (dest)
2245 && ((HARD_REGISTER_P (src)
2246 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2247 #ifdef LEAF_REGISTERS
2248 && ! LEAF_REGISTERS [REGNO (src)])
2249 #else
2251 #endif
2252 || (HARD_REGISTER_P (dest)
2253 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2254 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2255 return true;
2257 return false;
2260 struct likely_spilled_retval_info
2262 unsigned regno, nregs;
2263 unsigned mask;
2266 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2267 hard registers that are known to be written to / clobbered in full. */
2268 static void
2269 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2271 struct likely_spilled_retval_info *const info =
2272 (struct likely_spilled_retval_info *) data;
2273 unsigned regno, nregs;
2274 unsigned new_mask;
2276 if (!REG_P (XEXP (set, 0)))
2277 return;
2278 regno = REGNO (x);
2279 if (regno >= info->regno + info->nregs)
2280 return;
2281 nregs = REG_NREGS (x);
2282 if (regno + nregs <= info->regno)
2283 return;
2284 new_mask = (2U << (nregs - 1)) - 1;
2285 if (regno < info->regno)
2286 new_mask >>= info->regno - regno;
2287 else
2288 new_mask <<= regno - info->regno;
2289 info->mask &= ~new_mask;
2292 /* Return true iff part of the return value is live during INSN, and
2293 it is likely spilled. This can happen when more than one insn is needed
2294 to copy the return value, e.g. when we consider to combine into the
2295 second copy insn for a complex value. */
2297 static bool
2298 likely_spilled_retval_p (rtx_insn *insn)
2300 rtx_insn *use = BB_END (this_basic_block);
2301 rtx reg;
2302 rtx_insn *p;
2303 unsigned regno, nregs;
2304 /* We assume here that no machine mode needs more than
2305 32 hard registers when the value overlaps with a register
2306 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2307 unsigned mask;
2308 struct likely_spilled_retval_info info;
2310 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2311 return false;
2312 reg = XEXP (PATTERN (use), 0);
2313 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2314 return false;
2315 regno = REGNO (reg);
2316 nregs = REG_NREGS (reg);
2317 if (nregs == 1)
2318 return false;
2319 mask = (2U << (nregs - 1)) - 1;
2321 /* Disregard parts of the return value that are set later. */
2322 info.regno = regno;
2323 info.nregs = nregs;
2324 info.mask = mask;
2325 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2326 if (INSN_P (p))
2327 note_stores (p, likely_spilled_retval_1, &info);
2328 mask = info.mask;
2330 /* Check if any of the (probably) live return value registers is
2331 likely spilled. */
2332 nregs --;
2335 if ((mask & 1 << nregs)
2336 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2337 return true;
2338 } while (nregs--);
2339 return false;
2342 /* Adjust INSN after we made a change to its destination.
2344 Changing the destination can invalidate notes that say something about
2345 the results of the insn and a LOG_LINK pointing to the insn. */
2347 static void
2348 adjust_for_new_dest (rtx_insn *insn)
2350 /* For notes, be conservative and simply remove them. */
2351 remove_reg_equal_equiv_notes (insn, true);
2353 /* The new insn will have a destination that was previously the destination
2354 of an insn just above it. Call distribute_links to make a LOG_LINK from
2355 the next use of that destination. */
2357 rtx set = single_set (insn);
2358 gcc_assert (set);
2360 rtx reg = SET_DEST (set);
2362 while (GET_CODE (reg) == ZERO_EXTRACT
2363 || GET_CODE (reg) == STRICT_LOW_PART
2364 || GET_CODE (reg) == SUBREG)
2365 reg = XEXP (reg, 0);
2366 gcc_assert (REG_P (reg));
2368 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2370 df_insn_rescan (insn);
2373 /* Return TRUE if combine can reuse reg X in mode MODE.
2374 ADDED_SETS is trueif the original set is still required. */
2375 static bool
2376 can_change_dest_mode (rtx x, bool added_sets, machine_mode mode)
2378 unsigned int regno;
2380 if (!REG_P (x))
2381 return false;
2383 /* Don't change between modes with different underlying register sizes,
2384 since this could lead to invalid subregs. */
2385 if (maybe_ne (REGMODE_NATURAL_SIZE (mode),
2386 REGMODE_NATURAL_SIZE (GET_MODE (x))))
2387 return false;
2389 regno = REGNO (x);
2390 /* Allow hard registers if the new mode is legal, and occupies no more
2391 registers than the old mode. */
2392 if (regno < FIRST_PSEUDO_REGISTER)
2393 return (targetm.hard_regno_mode_ok (regno, mode)
2394 && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2396 /* Or a pseudo that is only used once. */
2397 return (regno < reg_n_sets_max
2398 && REG_N_SETS (regno) == 1
2399 && !added_sets
2400 && !REG_USERVAR_P (x));
2404 /* Check whether X, the destination of a set, refers to part of
2405 the register specified by REG. */
2407 static bool
2408 reg_subword_p (rtx x, rtx reg)
2410 /* Check that reg is an integer mode register. */
2411 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2412 return false;
2414 if (GET_CODE (x) == STRICT_LOW_PART
2415 || GET_CODE (x) == ZERO_EXTRACT)
2416 x = XEXP (x, 0);
2418 return GET_CODE (x) == SUBREG
2419 && !paradoxical_subreg_p (x)
2420 && SUBREG_REG (x) == reg
2421 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2424 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2425 by an arbitrary number of CLOBBERs. */
2426 static bool
2427 is_parallel_of_n_reg_sets (rtx pat, int n)
2429 if (GET_CODE (pat) != PARALLEL)
2430 return false;
2432 int len = XVECLEN (pat, 0);
2433 if (len < n)
2434 return false;
2436 int i;
2437 for (i = 0; i < n; i++)
2438 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2439 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2440 return false;
2441 for ( ; i < len; i++)
2442 switch (GET_CODE (XVECEXP (pat, 0, i)))
2444 case CLOBBER:
2445 if (XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2446 return false;
2447 break;
2448 default:
2449 return false;
2451 return true;
2454 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2455 CLOBBERs), can be split into individual SETs in that order, without
2456 changing semantics. */
2457 static bool
2458 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2460 if (!insn_nothrow_p (insn))
2461 return false;
2463 rtx pat = PATTERN (insn);
2465 int i, j;
2466 for (i = 0; i < n; i++)
2468 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2469 return false;
2471 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2473 for (j = i + 1; j < n; j++)
2474 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2475 return false;
2478 return true;
2481 /* Return whether X is just a single_set, with the source
2482 a general_operand. */
2483 static bool
2484 is_just_move (rtx_insn *x)
2486 rtx set = single_set (x);
2487 if (!set)
2488 return false;
2490 return general_operand (SET_SRC (set), VOIDmode);
2493 /* Callback function to count autoincs. */
2495 static int
2496 count_auto_inc (rtx, rtx, rtx, rtx, rtx, void *arg)
2498 (*((int *) arg))++;
2500 return 0;
2503 /* Try to combine the insns I0, I1 and I2 into I3.
2504 Here I0, I1 and I2 appear earlier than I3.
2505 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2508 If we are combining more than two insns and the resulting insn is not
2509 recognized, try splitting it into two insns. If that happens, I2 and I3
2510 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2511 Otherwise, I0, I1 and I2 are pseudo-deleted.
2513 Return 0 if the combination does not work. Then nothing is changed.
2514 If we did the combination, return the insn at which combine should
2515 resume scanning.
2517 Set NEW_DIRECT_JUMP_P to true if try_combine creates a
2518 new direct jump instruction.
2520 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2521 been I3 passed to an earlier try_combine within the same basic
2522 block. */
2524 static rtx_insn *
2525 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2526 bool *new_direct_jump_p, rtx_insn *last_combined_insn)
2528 /* New patterns for I3 and I2, respectively. */
2529 rtx newpat, newi2pat = 0;
2530 rtvec newpat_vec_with_clobbers = 0;
2531 bool substed_i2 = false, substed_i1 = false, substed_i0 = false;
2532 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2533 dead. */
2534 bool added_sets_0, added_sets_1, added_sets_2;
2535 /* Total number of SETs to put into I3. */
2536 int total_sets;
2537 /* Nonzero if I2's or I1's body now appears in I3. */
2538 int i2_is_used = 0, i1_is_used = 0;
2539 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2540 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2541 /* Contains I3 if the destination of I3 is used in its source, which means
2542 that the old life of I3 is being killed. If that usage is placed into
2543 I2 and not in I3, a REG_DEAD note must be made. */
2544 rtx i3dest_killed = 0;
2545 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2546 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2547 /* Copy of SET_SRC of I1 and I0, if needed. */
2548 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2549 /* Set if I2DEST was reused as a scratch register. */
2550 bool i2scratch = false;
2551 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2552 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2553 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2554 bool i2dest_in_i2src = false, i1dest_in_i1src = false;
2555 bool i2dest_in_i1src = false, i0dest_in_i0src = false;
2556 bool i1dest_in_i0src = false, i2dest_in_i0src = false;;
2557 bool i2dest_killed = false, i1dest_killed = false, i0dest_killed = false;
2558 bool i1_feeds_i2_n = false, i0_feeds_i2_n = false, i0_feeds_i1_n = false;
2559 /* Notes that must be added to REG_NOTES in I3 and I2. */
2560 rtx new_i3_notes, new_i2_notes;
2561 /* Notes that we substituted I3 into I2 instead of the normal case. */
2562 bool i3_subst_into_i2 = false;
2563 /* Notes that I1, I2 or I3 is a MULT operation. */
2564 bool have_mult = false;
2565 bool swap_i2i3 = false;
2566 bool split_i2i3 = false;
2567 bool changed_i3_dest = false;
2568 bool i2_was_move = false, i3_was_move = false;
2569 int n_auto_inc = 0;
2571 int maxreg;
2572 rtx_insn *temp_insn;
2573 rtx temp_expr;
2574 struct insn_link *link;
2575 rtx other_pat = 0;
2576 rtx new_other_notes;
2577 int i;
2578 scalar_int_mode dest_mode, temp_mode;
2579 bool has_non_call_exception = false;
2581 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2582 never be). */
2583 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2584 return 0;
2586 /* Only try four-insn combinations when there's high likelihood of
2587 success. Look for simple insns, such as loads of constants or
2588 binary operations involving a constant. */
2589 if (i0)
2591 int i;
2592 int ngood = 0;
2593 int nshift = 0;
2594 rtx set0, set3;
2596 if (!flag_expensive_optimizations)
2597 return 0;
2599 for (i = 0; i < 4; i++)
2601 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2602 rtx set = single_set (insn);
2603 rtx src;
2604 if (!set)
2605 continue;
2606 src = SET_SRC (set);
2607 if (CONSTANT_P (src))
2609 ngood += 2;
2610 break;
2612 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2613 ngood++;
2614 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2615 || GET_CODE (src) == LSHIFTRT)
2616 nshift++;
2619 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2620 are likely manipulating its value. Ideally we'll be able to combine
2621 all four insns into a bitfield insertion of some kind.
2623 Note the source in I0 might be inside a sign/zero extension and the
2624 memory modes in I0 and I3 might be different. So extract the address
2625 from the destination of I3 and search for it in the source of I0.
2627 In the event that there's a match but the source/dest do not actually
2628 refer to the same memory, the worst that happens is we try some
2629 combinations that we wouldn't have otherwise. */
2630 if ((set0 = single_set (i0))
2631 /* Ensure the source of SET0 is a MEM, possibly buried inside
2632 an extension. */
2633 && (GET_CODE (SET_SRC (set0)) == MEM
2634 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2635 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2636 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2637 && (set3 = single_set (i3))
2638 /* Ensure the destination of SET3 is a MEM. */
2639 && GET_CODE (SET_DEST (set3)) == MEM
2640 /* Would it be better to extract the base address for the MEM
2641 in SET3 and look for that? I don't have cases where it matters
2642 but I could envision such cases. */
2643 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2644 ngood += 2;
2646 if (ngood < 2 && nshift < 2)
2647 return 0;
2650 /* Exit early if one of the insns involved can't be used for
2651 combinations. */
2652 if (CALL_P (i2)
2653 || (i1 && CALL_P (i1))
2654 || (i0 && CALL_P (i0))
2655 || cant_combine_insn_p (i3)
2656 || cant_combine_insn_p (i2)
2657 || (i1 && cant_combine_insn_p (i1))
2658 || (i0 && cant_combine_insn_p (i0))
2659 || likely_spilled_retval_p (i3))
2660 return 0;
2662 combine_attempts++;
2663 undobuf.other_insn = 0;
2665 /* Reset the hard register usage information. */
2666 CLEAR_HARD_REG_SET (newpat_used_regs);
2668 if (dump_file && (dump_flags & TDF_DETAILS))
2670 if (i0)
2671 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2672 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2673 else if (i1)
2674 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2675 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2676 else
2677 fprintf (dump_file, "\nTrying %d -> %d:\n",
2678 INSN_UID (i2), INSN_UID (i3));
2680 if (i0)
2681 dump_insn_slim (dump_file, i0);
2682 if (i1)
2683 dump_insn_slim (dump_file, i1);
2684 dump_insn_slim (dump_file, i2);
2685 dump_insn_slim (dump_file, i3);
2688 /* If multiple insns feed into one of I2 or I3, they can be in any
2689 order. To simplify the code below, reorder them in sequence. */
2690 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2691 std::swap (i0, i2);
2692 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2693 std::swap (i0, i1);
2694 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2695 std::swap (i1, i2);
2697 added_links_insn = 0;
2698 added_notes_insn = 0;
2700 /* First check for one important special case that the code below will
2701 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2702 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2703 we may be able to replace that destination with the destination of I3.
2704 This occurs in the common code where we compute both a quotient and
2705 remainder into a structure, in which case we want to do the computation
2706 directly into the structure to avoid register-register copies.
2708 Note that this case handles both multiple sets in I2 and also cases
2709 where I2 has a number of CLOBBERs inside the PARALLEL.
2711 We make very conservative checks below and only try to handle the
2712 most common cases of this. For example, we only handle the case
2713 where I2 and I3 are adjacent to avoid making difficult register
2714 usage tests. */
2716 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2717 && REG_P (SET_SRC (PATTERN (i3)))
2718 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2719 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2720 && GET_CODE (PATTERN (i2)) == PARALLEL
2721 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2722 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2723 below would need to check what is inside (and reg_overlap_mentioned_p
2724 doesn't support those codes anyway). Don't allow those destinations;
2725 the resulting insn isn't likely to be recognized anyway. */
2726 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2727 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2728 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2729 SET_DEST (PATTERN (i3)))
2730 && next_active_insn (i2) == i3)
2732 rtx p2 = PATTERN (i2);
2734 /* Make sure that the destination of I3,
2735 which we are going to substitute into one output of I2,
2736 is not used within another output of I2. We must avoid making this:
2737 (parallel [(set (mem (reg 69)) ...)
2738 (set (reg 69) ...)])
2739 which is not well-defined as to order of actions.
2740 (Besides, reload can't handle output reloads for this.)
2742 The problem can also happen if the dest of I3 is a memory ref,
2743 if another dest in I2 is an indirect memory ref.
2745 Neither can this PARALLEL be an asm. We do not allow combining
2746 that usually (see can_combine_p), so do not here either. */
2747 bool ok = true;
2748 for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2750 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2751 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2752 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2753 SET_DEST (XVECEXP (p2, 0, i))))
2754 ok = false;
2755 else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2756 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2757 ok = false;
2760 if (ok)
2761 for (i = 0; i < XVECLEN (p2, 0); i++)
2762 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2763 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2765 combine_merges++;
2767 subst_insn = i3;
2768 subst_low_luid = DF_INSN_LUID (i2);
2770 added_sets_2 = added_sets_1 = added_sets_0 = false;
2771 i2src = SET_SRC (XVECEXP (p2, 0, i));
2772 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2773 i2dest_killed = dead_or_set_p (i2, i2dest);
2775 /* Replace the dest in I2 with our dest and make the resulting
2776 insn the new pattern for I3. Then skip to where we validate
2777 the pattern. Everything was set up above. */
2778 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2779 newpat = p2;
2780 i3_subst_into_i2 = true;
2781 goto validate_replacement;
2785 /* If I2 is setting a pseudo to a constant and I3 is setting some
2786 sub-part of it to another constant, merge them by making a new
2787 constant. */
2788 if (i1 == 0
2789 && (temp_expr = single_set (i2)) != 0
2790 && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2791 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2792 && GET_CODE (PATTERN (i3)) == SET
2793 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2794 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2796 rtx dest = SET_DEST (PATTERN (i3));
2797 rtx temp_dest = SET_DEST (temp_expr);
2798 int offset = -1;
2799 int width = 0;
2801 if (GET_CODE (dest) == ZERO_EXTRACT)
2803 if (CONST_INT_P (XEXP (dest, 1))
2804 && CONST_INT_P (XEXP (dest, 2))
2805 && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2806 &dest_mode))
2808 width = INTVAL (XEXP (dest, 1));
2809 offset = INTVAL (XEXP (dest, 2));
2810 dest = XEXP (dest, 0);
2811 if (BITS_BIG_ENDIAN)
2812 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2815 else
2817 if (GET_CODE (dest) == STRICT_LOW_PART)
2818 dest = XEXP (dest, 0);
2819 if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2821 width = GET_MODE_PRECISION (dest_mode);
2822 offset = 0;
2826 if (offset >= 0)
2828 /* If this is the low part, we're done. */
2829 if (subreg_lowpart_p (dest))
2831 /* Handle the case where inner is twice the size of outer. */
2832 else if (GET_MODE_PRECISION (temp_mode)
2833 == 2 * GET_MODE_PRECISION (dest_mode))
2834 offset += GET_MODE_PRECISION (dest_mode);
2835 /* Otherwise give up for now. */
2836 else
2837 offset = -1;
2840 if (offset >= 0)
2842 rtx inner = SET_SRC (PATTERN (i3));
2843 rtx outer = SET_SRC (temp_expr);
2845 wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2846 rtx_mode_t (inner, dest_mode),
2847 offset, width);
2849 combine_merges++;
2850 subst_insn = i3;
2851 subst_low_luid = DF_INSN_LUID (i2);
2852 added_sets_2 = added_sets_1 = added_sets_0 = false;
2853 i2dest = temp_dest;
2854 i2dest_killed = dead_or_set_p (i2, i2dest);
2856 /* Replace the source in I2 with the new constant and make the
2857 resulting insn the new pattern for I3. Then skip to where we
2858 validate the pattern. Everything was set up above. */
2859 SUBST (SET_SRC (temp_expr),
2860 immed_wide_int_const (o, temp_mode));
2862 newpat = PATTERN (i2);
2864 /* The dest of I3 has been replaced with the dest of I2. */
2865 changed_i3_dest = true;
2866 goto validate_replacement;
2870 /* If we have no I1 and I2 looks like:
2871 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2872 (set Y OP)])
2873 make up a dummy I1 that is
2874 (set Y OP)
2875 and change I2 to be
2876 (set (reg:CC X) (compare:CC Y (const_int 0)))
2878 (We can ignore any trailing CLOBBERs.)
2880 This undoes a previous combination and allows us to match a branch-and-
2881 decrement insn. */
2883 if (i1 == 0
2884 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2885 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2886 == MODE_CC)
2887 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2888 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2889 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2890 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2891 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2892 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2894 /* We make I1 with the same INSN_UID as I2. This gives it
2895 the same DF_INSN_LUID for value tracking. Our fake I1 will
2896 never appear in the insn stream so giving it the same INSN_UID
2897 as I2 will not cause a problem. */
2899 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2900 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2901 -1, NULL_RTX);
2902 INSN_UID (i1) = INSN_UID (i2);
2904 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2905 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2906 SET_DEST (PATTERN (i1)));
2907 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2908 SUBST_LINK (LOG_LINKS (i2),
2909 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2912 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2913 make those two SETs separate I1 and I2 insns, and make an I0 that is
2914 the original I1. */
2915 if (i0 == 0
2916 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2917 && can_split_parallel_of_n_reg_sets (i2, 2)
2918 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2919 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3)
2920 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2921 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2923 /* If there is no I1, there is no I0 either. */
2924 i0 = i1;
2926 /* We make I1 with the same INSN_UID as I2. This gives it
2927 the same DF_INSN_LUID for value tracking. Our fake I1 will
2928 never appear in the insn stream so giving it the same INSN_UID
2929 as I2 will not cause a problem. */
2931 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2932 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2933 -1, NULL_RTX);
2934 INSN_UID (i1) = INSN_UID (i2);
2936 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2939 /* Verify that I2 and maybe I1 and I0 can be combined into I3. */
2940 if (!can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src))
2942 if (dump_file && (dump_flags & TDF_DETAILS))
2943 fprintf (dump_file, "Can't combine i2 into i3\n");
2944 undo_all ();
2945 return 0;
2947 if (i1 && !can_combine_p (i1, i3, i0, NULL, i2, NULL, &i1dest, &i1src))
2949 if (dump_file && (dump_flags & TDF_DETAILS))
2950 fprintf (dump_file, "Can't combine i1 into i3\n");
2951 undo_all ();
2952 return 0;
2954 if (i0 && !can_combine_p (i0, i3, NULL, NULL, i1, i2, &i0dest, &i0src))
2956 if (dump_file && (dump_flags & TDF_DETAILS))
2957 fprintf (dump_file, "Can't combine i0 into i3\n");
2958 undo_all ();
2959 return 0;
2962 /* With non-call exceptions we can end up trying to combine multiple
2963 insns with possible EH side effects. Make sure we can combine
2964 that to a single insn which means there must be at most one insn
2965 in the combination with an EH side effect. */
2966 if (cfun->can_throw_non_call_exceptions)
2968 if (find_reg_note (i3, REG_EH_REGION, NULL_RTX)
2969 || find_reg_note (i2, REG_EH_REGION, NULL_RTX)
2970 || (i1 && find_reg_note (i1, REG_EH_REGION, NULL_RTX))
2971 || (i0 && find_reg_note (i0, REG_EH_REGION, NULL_RTX)))
2973 has_non_call_exception = true;
2974 if (insn_could_throw_p (i3)
2975 + insn_could_throw_p (i2)
2976 + (i1 ? insn_could_throw_p (i1) : 0)
2977 + (i0 ? insn_could_throw_p (i0) : 0) > 1)
2979 if (dump_file && (dump_flags & TDF_DETAILS))
2980 fprintf (dump_file, "Can't combine multiple insns with EH "
2981 "side-effects\n");
2982 undo_all ();
2983 return 0;
2988 /* Record whether i2 and i3 are trivial moves. */
2989 i2_was_move = is_just_move (i2);
2990 i3_was_move = is_just_move (i3);
2992 /* Record whether I2DEST is used in I2SRC and similarly for the other
2993 cases. Knowing this will help in register status updating below. */
2994 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2995 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2996 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2997 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2998 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2999 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3000 i2dest_killed = dead_or_set_p (i2, i2dest);
3001 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3002 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3004 /* For the earlier insns, determine which of the subsequent ones they
3005 feed. */
3006 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3007 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3008 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3009 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3010 && reg_overlap_mentioned_p (i0dest, i2src))));
3012 /* Ensure that I3's pattern can be the destination of combines. */
3013 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3014 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3015 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3016 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3017 &i3dest_killed))
3019 undo_all ();
3020 return 0;
3023 /* See if any of the insns is a MULT operation. Unless one is, we will
3024 reject a combination that is, since it must be slower. Be conservative
3025 here. */
3026 if (GET_CODE (i2src) == MULT
3027 || (i1 != 0 && GET_CODE (i1src) == MULT)
3028 || (i0 != 0 && GET_CODE (i0src) == MULT)
3029 || (GET_CODE (PATTERN (i3)) == SET
3030 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3031 have_mult = true;
3033 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3034 We used to do this EXCEPT in one case: I3 has a post-inc in an
3035 output operand. However, that exception can give rise to insns like
3036 mov r3,(r3)+
3037 which is a famous insn on the PDP-11 where the value of r3 used as the
3038 source was model-dependent. Avoid this sort of thing. */
3040 #if 0
3041 if (!(GET_CODE (PATTERN (i3)) == SET
3042 && REG_P (SET_SRC (PATTERN (i3)))
3043 && MEM_P (SET_DEST (PATTERN (i3)))
3044 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3045 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3046 /* It's not the exception. */
3047 #endif
3048 if (AUTO_INC_DEC)
3050 rtx link;
3051 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3052 if (REG_NOTE_KIND (link) == REG_INC
3053 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3054 || (i1 != 0
3055 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3057 undo_all ();
3058 return 0;
3062 /* See if the SETs in I1 or I2 need to be kept around in the merged
3063 instruction: whenever the value set there is still needed past I3.
3064 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3066 For the SET in I1, we have two cases: if I1 and I2 independently feed
3067 into I3, the set in I1 needs to be kept around unless I1DEST dies
3068 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3069 in I1 needs to be kept around unless I1DEST dies or is set in either
3070 I2 or I3. The same considerations apply to I0. */
3072 added_sets_2 = !dead_or_set_p (i3, i2dest);
3074 if (i1)
3075 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3076 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3077 else
3078 added_sets_1 = false;
3080 if (i0)
3081 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3082 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3083 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3084 && dead_or_set_p (i2, i0dest)));
3085 else
3086 added_sets_0 = false;
3088 /* We are about to copy insns for the case where they need to be kept
3089 around. Check that they can be copied in the merged instruction. */
3091 if (targetm.cannot_copy_insn_p
3092 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3093 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3094 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3096 undo_all ();
3097 return 0;
3100 /* We cannot safely duplicate volatile references in any case. */
3102 if ((added_sets_2 && volatile_refs_p (PATTERN (i2)))
3103 || (added_sets_1 && volatile_refs_p (PATTERN (i1)))
3104 || (added_sets_0 && volatile_refs_p (PATTERN (i0))))
3106 undo_all ();
3107 return 0;
3110 /* Count how many auto_inc expressions there were in the original insns;
3111 we need to have the same number in the resulting patterns. */
3113 if (i0)
3114 for_each_inc_dec (PATTERN (i0), count_auto_inc, &n_auto_inc);
3115 if (i1)
3116 for_each_inc_dec (PATTERN (i1), count_auto_inc, &n_auto_inc);
3117 for_each_inc_dec (PATTERN (i2), count_auto_inc, &n_auto_inc);
3118 for_each_inc_dec (PATTERN (i3), count_auto_inc, &n_auto_inc);
3120 /* If the set in I2 needs to be kept around, we must make a copy of
3121 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3122 PATTERN (I2), we are only substituting for the original I1DEST, not into
3123 an already-substituted copy. This also prevents making self-referential
3124 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3125 I2DEST. */
3127 if (added_sets_2)
3129 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3130 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3131 else
3132 i2pat = copy_rtx (PATTERN (i2));
3135 if (added_sets_1)
3137 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3138 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3139 else
3140 i1pat = copy_rtx (PATTERN (i1));
3143 if (added_sets_0)
3145 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3146 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3147 else
3148 i0pat = copy_rtx (PATTERN (i0));
3151 combine_merges++;
3153 /* Substitute in the latest insn for the regs set by the earlier ones. */
3155 maxreg = max_reg_num ();
3157 subst_insn = i3;
3159 /* Many machines have insns that can both perform an
3160 arithmetic operation and set the condition code. These operations will
3161 be represented as a PARALLEL with the first element of the vector
3162 being a COMPARE of an arithmetic operation with the constant zero.
3163 The second element of the vector will set some pseudo to the result
3164 of the same arithmetic operation. If we simplify the COMPARE, we won't
3165 match such a pattern and so will generate an extra insn. Here we test
3166 for this case, where both the comparison and the operation result are
3167 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3168 I2SRC. Later we will make the PARALLEL that contains I2. */
3170 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3171 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3172 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3173 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3175 rtx newpat_dest;
3176 rtx *cc_use_loc = NULL;
3177 rtx_insn *cc_use_insn = NULL;
3178 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3179 machine_mode compare_mode, orig_compare_mode;
3180 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3181 scalar_int_mode mode;
3183 newpat = PATTERN (i3);
3184 newpat_dest = SET_DEST (newpat);
3185 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3187 if (undobuf.other_insn == 0
3188 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3189 &cc_use_insn)))
3191 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3192 if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3193 compare_code = simplify_compare_const (compare_code, mode,
3194 op0, &op1);
3195 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3198 /* Do the rest only if op1 is const0_rtx, which may be the
3199 result of simplification. */
3200 if (op1 == const0_rtx)
3202 /* If a single use of the CC is found, prepare to modify it
3203 when SELECT_CC_MODE returns a new CC-class mode, or when
3204 the above simplify_compare_const() returned a new comparison
3205 operator. undobuf.other_insn is assigned the CC use insn
3206 when modifying it. */
3207 if (cc_use_loc)
3209 #ifdef SELECT_CC_MODE
3210 machine_mode new_mode
3211 = SELECT_CC_MODE (compare_code, op0, op1);
3212 if (new_mode != orig_compare_mode
3213 && can_change_dest_mode (SET_DEST (newpat),
3214 added_sets_2, new_mode))
3216 unsigned int regno = REGNO (newpat_dest);
3217 compare_mode = new_mode;
3218 if (regno < FIRST_PSEUDO_REGISTER)
3219 newpat_dest = gen_rtx_REG (compare_mode, regno);
3220 else
3222 subst_mode (regno, compare_mode);
3223 newpat_dest = regno_reg_rtx[regno];
3226 #endif
3227 /* Cases for modifying the CC-using comparison. */
3228 if (compare_code != orig_compare_code
3229 /* ??? Do we need to verify the zero rtx? */
3230 && XEXP (*cc_use_loc, 1) == const0_rtx)
3232 /* Replace cc_use_loc with entire new RTX. */
3233 SUBST (*cc_use_loc,
3234 gen_rtx_fmt_ee (compare_code, GET_MODE (*cc_use_loc),
3235 newpat_dest, const0_rtx));
3236 undobuf.other_insn = cc_use_insn;
3238 else if (compare_mode != orig_compare_mode)
3240 /* Just replace the CC reg with a new mode. */
3241 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3242 undobuf.other_insn = cc_use_insn;
3246 /* Now we modify the current newpat:
3247 First, SET_DEST(newpat) is updated if the CC mode has been
3248 altered. For targets without SELECT_CC_MODE, this should be
3249 optimized away. */
3250 if (compare_mode != orig_compare_mode)
3251 SUBST (SET_DEST (newpat), newpat_dest);
3252 /* This is always done to propagate i2src into newpat. */
3253 SUBST (SET_SRC (newpat),
3254 gen_rtx_COMPARE (compare_mode, op0, op1));
3255 /* Create new version of i2pat if needed; the below PARALLEL
3256 creation needs this to work correctly. */
3257 if (! rtx_equal_p (i2src, op0))
3258 i2pat = gen_rtx_SET (i2dest, op0);
3259 i2_is_used = 1;
3263 if (i2_is_used == 0)
3265 /* It is possible that the source of I2 or I1 may be performing
3266 an unneeded operation, such as a ZERO_EXTEND of something
3267 that is known to have the high part zero. Handle that case
3268 by letting subst look at the inner insns.
3270 Another way to do this would be to have a function that tries
3271 to simplify a single insn instead of merging two or more
3272 insns. We don't do this because of the potential of infinite
3273 loops and because of the potential extra memory required.
3274 However, doing it the way we are is a bit of a kludge and
3275 doesn't catch all cases.
3277 But only do this if -fexpensive-optimizations since it slows
3278 things down and doesn't usually win.
3280 This is not done in the COMPARE case above because the
3281 unmodified I2PAT is used in the PARALLEL and so a pattern
3282 with a modified I2SRC would not match. */
3284 if (flag_expensive_optimizations)
3286 /* Pass pc_rtx so no substitutions are done, just
3287 simplifications. */
3288 if (i1)
3290 subst_low_luid = DF_INSN_LUID (i1);
3291 i1src = subst (i1src, pc_rtx, pc_rtx, false, false, false);
3294 subst_low_luid = DF_INSN_LUID (i2);
3295 i2src = subst (i2src, pc_rtx, pc_rtx, false, false, false);
3298 n_occurrences = 0; /* `subst' counts here */
3299 subst_low_luid = DF_INSN_LUID (i2);
3301 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3302 copy of I2SRC each time we substitute it, in order to avoid creating
3303 self-referential RTL when we will be substituting I1SRC for I1DEST
3304 later. Likewise if I0 feeds into I2, either directly or indirectly
3305 through I1, and I0DEST is in I0SRC. */
3306 newpat = subst (PATTERN (i3), i2dest, i2src, false, false,
3307 (i1_feeds_i2_n && i1dest_in_i1src)
3308 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3309 && i0dest_in_i0src));
3310 substed_i2 = true;
3312 /* Record whether I2's body now appears within I3's body. */
3313 i2_is_used = n_occurrences;
3316 /* If we already got a failure, don't try to do more. Otherwise, try to
3317 substitute I1 if we have it. */
3319 if (i1 && GET_CODE (newpat) != CLOBBER)
3321 /* Before we can do this substitution, we must redo the test done
3322 above (see detailed comments there) that ensures I1DEST isn't
3323 mentioned in any SETs in NEWPAT that are field assignments. */
3324 if (!combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3325 false, false, 0))
3327 undo_all ();
3328 return 0;
3331 n_occurrences = 0;
3332 subst_low_luid = DF_INSN_LUID (i1);
3334 /* If the following substitution will modify I1SRC, make a copy of it
3335 for the case where it is substituted for I1DEST in I2PAT later. */
3336 if (added_sets_2 && i1_feeds_i2_n)
3337 i1src_copy = copy_rtx (i1src);
3339 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3340 copy of I1SRC each time we substitute it, in order to avoid creating
3341 self-referential RTL when we will be substituting I0SRC for I0DEST
3342 later. */
3343 newpat = subst (newpat, i1dest, i1src, false, false,
3344 i0_feeds_i1_n && i0dest_in_i0src);
3345 substed_i1 = true;
3347 /* Record whether I1's body now appears within I3's body. */
3348 i1_is_used = n_occurrences;
3351 /* Likewise for I0 if we have it. */
3353 if (i0 && GET_CODE (newpat) != CLOBBER)
3355 if (!combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3356 false, false, 0))
3358 undo_all ();
3359 return 0;
3362 /* If the following substitution will modify I0SRC, make a copy of it
3363 for the case where it is substituted for I0DEST in I1PAT later. */
3364 if (added_sets_1 && i0_feeds_i1_n)
3365 i0src_copy = copy_rtx (i0src);
3366 /* And a copy for I0DEST in I2PAT substitution. */
3367 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3368 || (i0_feeds_i2_n)))
3369 i0src_copy2 = copy_rtx (i0src);
3371 n_occurrences = 0;
3372 subst_low_luid = DF_INSN_LUID (i0);
3373 newpat = subst (newpat, i0dest, i0src, false, false, false);
3374 substed_i0 = true;
3377 if (n_auto_inc)
3379 int new_n_auto_inc = 0;
3380 for_each_inc_dec (newpat, count_auto_inc, &new_n_auto_inc);
3382 if (n_auto_inc != new_n_auto_inc)
3384 if (dump_file && (dump_flags & TDF_DETAILS))
3385 fprintf (dump_file, "Number of auto_inc expressions changed\n");
3386 undo_all ();
3387 return 0;
3391 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3392 to count all the ways that I2SRC and I1SRC can be used. */
3393 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3394 && i2_is_used + added_sets_2 > 1)
3395 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3396 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n) > 1))
3397 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3398 && (n_occurrences + added_sets_0
3399 + (added_sets_1 && i0_feeds_i1_n)
3400 + (added_sets_2 && i0_feeds_i2_n) > 1))
3401 /* Fail if we tried to make a new register. */
3402 || max_reg_num () != maxreg
3403 /* Fail if we couldn't do something and have a CLOBBER. */
3404 || GET_CODE (newpat) == CLOBBER
3405 /* Fail if this new pattern is a MULT and we didn't have one before
3406 at the outer level. */
3407 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3408 && ! have_mult))
3410 undo_all ();
3411 return 0;
3414 /* If the actions of the earlier insns must be kept
3415 in addition to substituting them into the latest one,
3416 we must make a new PARALLEL for the latest insn
3417 to hold additional the SETs. */
3419 if (added_sets_0 || added_sets_1 || added_sets_2)
3421 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3422 combine_extras++;
3424 if (GET_CODE (newpat) == PARALLEL)
3426 rtvec old = XVEC (newpat, 0);
3427 total_sets = XVECLEN (newpat, 0) + extra_sets;
3428 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3429 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3430 sizeof (old->elem[0]) * old->num_elem);
3432 else
3434 rtx old = newpat;
3435 total_sets = 1 + extra_sets;
3436 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3437 XVECEXP (newpat, 0, 0) = old;
3440 if (added_sets_0)
3441 XVECEXP (newpat, 0, --total_sets) = i0pat;
3443 if (added_sets_1)
3445 rtx t = i1pat;
3446 if (i0_feeds_i1_n)
3447 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src,
3448 false, false, false);
3450 XVECEXP (newpat, 0, --total_sets) = t;
3452 if (added_sets_2)
3454 rtx t = i2pat;
3455 if (i1_feeds_i2_n)
3456 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, false, false,
3457 i0_feeds_i1_n && i0dest_in_i0src);
3458 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3459 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src,
3460 false, false, false);
3462 XVECEXP (newpat, 0, --total_sets) = t;
3466 validate_replacement:
3468 /* Note which hard regs this insn has as inputs. */
3469 mark_used_regs_combine (newpat);
3471 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3472 consider splitting this pattern, we might need these clobbers. */
3473 if (i1 && GET_CODE (newpat) == PARALLEL
3474 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3476 int len = XVECLEN (newpat, 0);
3478 newpat_vec_with_clobbers = rtvec_alloc (len);
3479 for (i = 0; i < len; i++)
3480 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3483 /* We have recognized nothing yet. */
3484 insn_code_number = -1;
3486 /* See if this is a PARALLEL of two SETs where one SET's destination is
3487 a register that is unused and this isn't marked as an instruction that
3488 might trap in an EH region. In that case, we just need the other SET.
3489 We prefer this over the PARALLEL.
3491 This can occur when simplifying a divmod insn. We *must* test for this
3492 case here because the code below that splits two independent SETs doesn't
3493 handle this case correctly when it updates the register status.
3495 It's pointless doing this if we originally had two sets, one from
3496 i3, and one from i2. Combining then splitting the parallel results
3497 in the original i2 again plus an invalid insn (which we delete).
3498 The net effect is only to move instructions around, which makes
3499 debug info less accurate.
3501 If the remaining SET came from I2 its destination should not be used
3502 between I2 and I3. See PR82024. */
3504 if (!(added_sets_2 && i1 == 0)
3505 && is_parallel_of_n_reg_sets (newpat, 2)
3506 && asm_noperands (newpat) < 0)
3508 rtx set0 = XVECEXP (newpat, 0, 0);
3509 rtx set1 = XVECEXP (newpat, 0, 1);
3510 rtx oldpat = newpat;
3512 if (((REG_P (SET_DEST (set1))
3513 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3514 || (GET_CODE (SET_DEST (set1)) == SUBREG
3515 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3516 && insn_nothrow_p (i3)
3517 && !side_effects_p (SET_SRC (set1)))
3519 newpat = set0;
3520 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3523 else if (((REG_P (SET_DEST (set0))
3524 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3525 || (GET_CODE (SET_DEST (set0)) == SUBREG
3526 && find_reg_note (i3, REG_UNUSED,
3527 SUBREG_REG (SET_DEST (set0)))))
3528 && insn_nothrow_p (i3)
3529 && !side_effects_p (SET_SRC (set0)))
3531 rtx dest = SET_DEST (set1);
3532 if (GET_CODE (dest) == SUBREG)
3533 dest = SUBREG_REG (dest);
3534 if (!reg_used_between_p (dest, i2, i3))
3536 newpat = set1;
3537 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3539 if (insn_code_number >= 0)
3540 changed_i3_dest = true;
3544 if (insn_code_number < 0)
3545 newpat = oldpat;
3548 /* Is the result of combination a valid instruction? */
3549 if (insn_code_number < 0)
3550 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3552 /* If we were combining three insns and the result is a simple SET
3553 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3554 insns. There are two ways to do this. It can be split using a
3555 machine-specific method (like when you have an addition of a large
3556 constant) or by combine in the function find_split_point. */
3558 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3559 && asm_noperands (newpat) < 0)
3561 rtx parallel, *split;
3562 rtx_insn *m_split_insn;
3564 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3565 use I2DEST as a scratch register will help. In the latter case,
3566 convert I2DEST to the mode of the source of NEWPAT if we can. */
3568 m_split_insn = combine_split_insns (newpat, i3);
3570 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3571 inputs of NEWPAT. */
3573 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3574 possible to try that as a scratch reg. This would require adding
3575 more code to make it work though. */
3577 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3579 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3581 /* ??? Reusing i2dest without resetting the reg_stat entry for it
3582 (temporarily, until we are committed to this instruction
3583 combination) does not work: for example, any call to nonzero_bits
3584 on the register (from a splitter in the MD file, for example)
3585 will get the old information, which is invalid.
3587 Since nowadays we can create registers during combine just fine,
3588 we should just create a new one here, not reuse i2dest. */
3590 /* First try to split using the original register as a
3591 scratch register. */
3592 parallel = gen_rtx_PARALLEL (VOIDmode,
3593 gen_rtvec (2, newpat,
3594 gen_rtx_CLOBBER (VOIDmode,
3595 i2dest)));
3596 m_split_insn = combine_split_insns (parallel, i3);
3598 /* If that didn't work, try changing the mode of I2DEST if
3599 we can. */
3600 if (m_split_insn == 0
3601 && new_mode != GET_MODE (i2dest)
3602 && new_mode != VOIDmode
3603 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3605 machine_mode old_mode = GET_MODE (i2dest);
3606 rtx ni2dest;
3608 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3609 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3610 else
3612 subst_mode (REGNO (i2dest), new_mode);
3613 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3616 parallel = (gen_rtx_PARALLEL
3617 (VOIDmode,
3618 gen_rtvec (2, newpat,
3619 gen_rtx_CLOBBER (VOIDmode,
3620 ni2dest))));
3621 m_split_insn = combine_split_insns (parallel, i3);
3623 if (m_split_insn == 0
3624 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3626 struct undo *buf;
3628 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3629 buf = undobuf.undos;
3630 undobuf.undos = buf->next;
3631 buf->next = undobuf.frees;
3632 undobuf.frees = buf;
3636 i2scratch = m_split_insn != 0;
3639 /* If recog_for_combine has discarded clobbers, try to use them
3640 again for the split. */
3641 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3643 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3644 m_split_insn = combine_split_insns (parallel, i3);
3647 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3649 rtx m_split_pat = PATTERN (m_split_insn);
3650 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3651 if (insn_code_number >= 0)
3652 newpat = m_split_pat;
3654 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3655 && (next_nonnote_nondebug_insn (i2) == i3
3656 || !modified_between_p (PATTERN (m_split_insn), i2, i3)))
3658 rtx i2set, i3set;
3659 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3660 newi2pat = PATTERN (m_split_insn);
3662 i3set = single_set (NEXT_INSN (m_split_insn));
3663 i2set = single_set (m_split_insn);
3665 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3667 /* If I2 or I3 has multiple SETs, we won't know how to track
3668 register status, so don't use these insns. If I2's destination
3669 is used between I2 and I3, we also can't use these insns. */
3671 if (i2_code_number >= 0 && i2set && i3set
3672 && (next_nonnote_nondebug_insn (i2) == i3
3673 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3674 insn_code_number = recog_for_combine (&newi3pat, i3,
3675 &new_i3_notes);
3676 if (insn_code_number >= 0)
3677 newpat = newi3pat;
3679 /* It is possible that both insns now set the destination of I3.
3680 If so, we must show an extra use of it. */
3682 if (insn_code_number >= 0)
3684 rtx new_i3_dest = SET_DEST (i3set);
3685 rtx new_i2_dest = SET_DEST (i2set);
3687 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3688 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3689 || GET_CODE (new_i3_dest) == SUBREG)
3690 new_i3_dest = XEXP (new_i3_dest, 0);
3692 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3693 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3694 || GET_CODE (new_i2_dest) == SUBREG)
3695 new_i2_dest = XEXP (new_i2_dest, 0);
3697 if (REG_P (new_i3_dest)
3698 && REG_P (new_i2_dest)
3699 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3700 && REGNO (new_i2_dest) < reg_n_sets_max)
3701 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3705 /* If we can split it and use I2DEST, go ahead and see if that
3706 helps things be recognized. Verify that none of the registers
3707 are set between I2 and I3. */
3708 if (insn_code_number < 0
3709 && (split = find_split_point (&newpat, i3, false)) != 0
3710 /* We need I2DEST in the proper mode. If it is a hard register
3711 or the only use of a pseudo, we can change its mode.
3712 Make sure we don't change a hard register to have a mode that
3713 isn't valid for it, or change the number of registers. */
3714 && (GET_MODE (*split) == GET_MODE (i2dest)
3715 || GET_MODE (*split) == VOIDmode
3716 || can_change_dest_mode (i2dest, added_sets_2,
3717 GET_MODE (*split)))
3718 && (next_nonnote_nondebug_insn (i2) == i3
3719 || !modified_between_p (*split, i2, i3))
3720 /* We can't overwrite I2DEST if its value is still used by
3721 NEWPAT. */
3722 && ! reg_referenced_p (i2dest, newpat)
3723 /* We should not split a possibly trapping part when we
3724 care about non-call EH and have REG_EH_REGION notes
3725 to distribute. */
3726 && ! (cfun->can_throw_non_call_exceptions
3727 && has_non_call_exception
3728 && may_trap_p (*split)))
3730 rtx newdest = i2dest;
3731 enum rtx_code split_code = GET_CODE (*split);
3732 machine_mode split_mode = GET_MODE (*split);
3733 bool subst_done = false;
3734 newi2pat = NULL_RTX;
3736 i2scratch = true;
3738 /* *SPLIT may be part of I2SRC, so make sure we have the
3739 original expression around for later debug processing.
3740 We should not need I2SRC any more in other cases. */
3741 if (MAY_HAVE_DEBUG_BIND_INSNS)
3742 i2src = copy_rtx (i2src);
3743 else
3744 i2src = NULL;
3746 /* Get NEWDEST as a register in the proper mode. We have already
3747 validated that we can do this. */
3748 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3750 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3751 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3752 else
3754 subst_mode (REGNO (i2dest), split_mode);
3755 newdest = regno_reg_rtx[REGNO (i2dest)];
3759 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3760 an ASHIFT. This can occur if it was inside a PLUS and hence
3761 appeared to be a memory address. This is a kludge. */
3762 if (split_code == MULT
3763 && CONST_INT_P (XEXP (*split, 1))
3764 && INTVAL (XEXP (*split, 1)) > 0
3765 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3767 rtx i_rtx = gen_int_shift_amount (split_mode, i);
3768 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3769 XEXP (*split, 0), i_rtx));
3770 /* Update split_code because we may not have a multiply
3771 anymore. */
3772 split_code = GET_CODE (*split);
3775 /* Similarly for (plus (mult FOO (const_int pow2))). */
3776 if (split_code == PLUS
3777 && GET_CODE (XEXP (*split, 0)) == MULT
3778 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3779 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3780 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3782 rtx nsplit = XEXP (*split, 0);
3783 rtx i_rtx = gen_int_shift_amount (GET_MODE (nsplit), i);
3784 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3785 XEXP (nsplit, 0),
3786 i_rtx));
3787 /* Update split_code because we may not have a multiply
3788 anymore. */
3789 split_code = GET_CODE (*split);
3792 #ifdef INSN_SCHEDULING
3793 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3794 be written as a ZERO_EXTEND. */
3795 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3797 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3798 what it really is. */
3799 if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3800 == SIGN_EXTEND)
3801 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3802 SUBREG_REG (*split)));
3803 else
3804 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3805 SUBREG_REG (*split)));
3807 #endif
3809 /* Attempt to split binary operators using arithmetic identities. */
3810 if (BINARY_P (SET_SRC (newpat))
3811 && split_mode == GET_MODE (SET_SRC (newpat))
3812 && ! side_effects_p (SET_SRC (newpat)))
3814 rtx setsrc = SET_SRC (newpat);
3815 machine_mode mode = GET_MODE (setsrc);
3816 enum rtx_code code = GET_CODE (setsrc);
3817 rtx src_op0 = XEXP (setsrc, 0);
3818 rtx src_op1 = XEXP (setsrc, 1);
3820 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3821 if (rtx_equal_p (src_op0, src_op1))
3823 newi2pat = gen_rtx_SET (newdest, src_op0);
3824 SUBST (XEXP (setsrc, 0), newdest);
3825 SUBST (XEXP (setsrc, 1), newdest);
3826 subst_done = true;
3828 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3829 else if ((code == PLUS || code == MULT)
3830 && GET_CODE (src_op0) == code
3831 && GET_CODE (XEXP (src_op0, 0)) == code
3832 && (INTEGRAL_MODE_P (mode)
3833 || (FLOAT_MODE_P (mode)
3834 && flag_unsafe_math_optimizations)))
3836 rtx p = XEXP (XEXP (src_op0, 0), 0);
3837 rtx q = XEXP (XEXP (src_op0, 0), 1);
3838 rtx r = XEXP (src_op0, 1);
3839 rtx s = src_op1;
3841 /* Split both "((X op Y) op X) op Y" and
3842 "((X op Y) op Y) op X" as "T op T" where T is
3843 "X op Y". */
3844 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3845 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3847 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3848 SUBST (XEXP (setsrc, 0), newdest);
3849 SUBST (XEXP (setsrc, 1), newdest);
3850 subst_done = true;
3852 /* Split "((X op X) op Y) op Y)" as "T op T" where
3853 T is "X op Y". */
3854 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3856 rtx tmp = simplify_gen_binary (code, mode, p, r);
3857 newi2pat = gen_rtx_SET (newdest, tmp);
3858 SUBST (XEXP (setsrc, 0), newdest);
3859 SUBST (XEXP (setsrc, 1), newdest);
3860 subst_done = true;
3865 if (!subst_done)
3867 newi2pat = gen_rtx_SET (newdest, *split);
3868 SUBST (*split, newdest);
3871 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3873 /* recog_for_combine might have added CLOBBERs to newi2pat.
3874 Make sure NEWPAT does not depend on the clobbered regs. */
3875 if (GET_CODE (newi2pat) == PARALLEL)
3876 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3877 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3879 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3880 if (reg_overlap_mentioned_p (reg, newpat))
3882 undo_all ();
3883 return 0;
3887 /* If the split point was a MULT and we didn't have one before,
3888 don't use one now. */
3889 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3890 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3894 /* Check for a case where we loaded from memory in a narrow mode and
3895 then sign extended it, but we need both registers. In that case,
3896 we have a PARALLEL with both loads from the same memory location.
3897 We can split this into a load from memory followed by a register-register
3898 copy. This saves at least one insn, more if register allocation can
3899 eliminate the copy.
3901 We cannot do this if the destination of the first assignment is a
3902 condition code register. We eliminate this case by making sure
3903 the SET_DEST and SET_SRC have the same mode.
3905 We cannot do this if the destination of the second assignment is
3906 a register that we have already assumed is zero-extended. Similarly
3907 for a SUBREG of such a register. */
3909 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3910 && GET_CODE (newpat) == PARALLEL
3911 && XVECLEN (newpat, 0) == 2
3912 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3913 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3914 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3915 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3916 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3917 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3918 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3919 && !modified_between_p (SET_SRC (XVECEXP (newpat, 0, 1)), i2, i3)
3920 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3921 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3922 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3923 (REG_P (temp_expr)
3924 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3925 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3926 BITS_PER_WORD)
3927 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3928 HOST_BITS_PER_INT)
3929 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3930 != GET_MODE_MASK (word_mode))))
3931 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3932 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3933 (REG_P (temp_expr)
3934 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3935 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3936 BITS_PER_WORD)
3937 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3938 HOST_BITS_PER_INT)
3939 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3940 != GET_MODE_MASK (word_mode)))))
3941 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3942 SET_SRC (XVECEXP (newpat, 0, 1)))
3943 && ! find_reg_note (i3, REG_UNUSED,
3944 SET_DEST (XVECEXP (newpat, 0, 0))))
3946 rtx ni2dest;
3948 newi2pat = XVECEXP (newpat, 0, 0);
3949 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3950 newpat = XVECEXP (newpat, 0, 1);
3951 SUBST (SET_SRC (newpat),
3952 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3953 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3955 if (i2_code_number >= 0)
3956 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3958 if (insn_code_number >= 0)
3959 swap_i2i3 = 1;
3962 /* Similarly, check for a case where we have a PARALLEL of two independent
3963 SETs but we started with three insns. In this case, we can do the sets
3964 as two separate insns. This case occurs when some SET allows two
3965 other insns to combine, but the destination of that SET is still live.
3967 Also do this if we started with two insns and (at least) one of the
3968 resulting sets is a noop; this noop will be deleted later.
3970 Also do this if we started with two insns neither of which was a simple
3971 move. */
3973 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3974 && GET_CODE (newpat) == PARALLEL
3975 && XVECLEN (newpat, 0) == 2
3976 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3977 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3978 && (i1
3979 || set_noop_p (XVECEXP (newpat, 0, 0))
3980 || set_noop_p (XVECEXP (newpat, 0, 1))
3981 || (!i2_was_move && !i3_was_move))
3982 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3983 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3984 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3985 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3986 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3987 XVECEXP (newpat, 0, 0))
3988 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3989 XVECEXP (newpat, 0, 1))
3990 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3991 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3993 rtx set0 = XVECEXP (newpat, 0, 0);
3994 rtx set1 = XVECEXP (newpat, 0, 1);
3996 /* Normally, it doesn't matter which of the two is done first, but
3997 one which uses any regs/memory set in between i2 and i3 can't
3998 be first. The PARALLEL might also have been pre-existing in i3,
3999 so we need to make sure that we won't wrongly hoist a SET to i2
4000 that would conflict with a death note present in there, or would
4001 have its dest modified between i2 and i3. */
4002 if (!modified_between_p (SET_SRC (set1), i2, i3)
4003 && !(REG_P (SET_DEST (set1))
4004 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4005 && !(GET_CODE (SET_DEST (set1)) == SUBREG
4006 && find_reg_note (i2, REG_DEAD,
4007 SUBREG_REG (SET_DEST (set1))))
4008 && !modified_between_p (SET_DEST (set1), i2, i3)
4009 /* If I3 is a jump, ensure that set0 is a jump so that
4010 we do not create invalid RTL. */
4011 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
4014 newi2pat = set1;
4015 newpat = set0;
4017 else if (!modified_between_p (SET_SRC (set0), i2, i3)
4018 && !(REG_P (SET_DEST (set0))
4019 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4020 && !(GET_CODE (SET_DEST (set0)) == SUBREG
4021 && find_reg_note (i2, REG_DEAD,
4022 SUBREG_REG (SET_DEST (set0))))
4023 && !modified_between_p (SET_DEST (set0), i2, i3)
4024 /* If I3 is a jump, ensure that set1 is a jump so that
4025 we do not create invalid RTL. */
4026 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4029 newi2pat = set0;
4030 newpat = set1;
4032 else
4034 undo_all ();
4035 return 0;
4038 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4040 if (i2_code_number >= 0)
4042 /* recog_for_combine might have added CLOBBERs to newi2pat.
4043 Make sure NEWPAT does not depend on the clobbered regs. */
4044 if (GET_CODE (newi2pat) == PARALLEL)
4046 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4047 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4049 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4050 if (reg_overlap_mentioned_p (reg, newpat))
4052 undo_all ();
4053 return 0;
4058 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4060 /* Likewise, recog_for_combine might have added clobbers to NEWPAT.
4061 Checking that the SET0's SET_DEST and SET1's SET_DEST aren't
4062 mentioned/clobbered, ensures NEWI2PAT's SET_DEST is live. */
4063 if (insn_code_number >= 0 && GET_CODE (newpat) == PARALLEL)
4065 for (i = XVECLEN (newpat, 0) - 1; i >= 0; i--)
4066 if (GET_CODE (XVECEXP (newpat, 0, i)) == CLOBBER)
4068 rtx reg = XEXP (XVECEXP (newpat, 0, i), 0);
4069 if (reg_overlap_mentioned_p (reg, SET_DEST (set0))
4070 || reg_overlap_mentioned_p (reg, SET_DEST (set1)))
4072 undo_all ();
4073 return 0;
4078 if (insn_code_number >= 0)
4079 split_i2i3 = true;
4083 /* If it still isn't recognized, fail and change things back the way they
4084 were. */
4085 if ((insn_code_number < 0
4086 /* Is the result a reasonable ASM_OPERANDS? */
4087 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4089 undo_all ();
4090 return 0;
4093 /* If we had to change another insn, make sure it is valid also. */
4094 if (undobuf.other_insn)
4096 CLEAR_HARD_REG_SET (newpat_used_regs);
4098 other_pat = PATTERN (undobuf.other_insn);
4099 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4100 &new_other_notes);
4102 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4104 undo_all ();
4105 return 0;
4109 /* Only allow this combination if insn_cost reports that the
4110 replacement instructions are cheaper than the originals. */
4111 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4113 undo_all ();
4114 return 0;
4117 if (MAY_HAVE_DEBUG_BIND_INSNS)
4119 struct undo *undo;
4121 for (undo = undobuf.undos; undo; undo = undo->next)
4122 if (undo->kind == UNDO_MODE)
4124 rtx reg = regno_reg_rtx[undo->where.regno];
4125 machine_mode new_mode = GET_MODE (reg);
4126 machine_mode old_mode = undo->old_contents.m;
4128 /* Temporarily revert mode back. */
4129 adjust_reg_mode (reg, old_mode);
4131 if (reg == i2dest && i2scratch)
4133 /* If we used i2dest as a scratch register with a
4134 different mode, substitute it for the original
4135 i2src while its original mode is temporarily
4136 restored, and then clear i2scratch so that we don't
4137 do it again later. */
4138 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4139 this_basic_block);
4140 i2scratch = false;
4141 /* Put back the new mode. */
4142 adjust_reg_mode (reg, new_mode);
4144 else
4146 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4147 rtx_insn *first, *last;
4149 if (reg == i2dest)
4151 first = i2;
4152 last = last_combined_insn;
4154 else
4156 first = i3;
4157 last = undobuf.other_insn;
4158 gcc_assert (last);
4159 if (DF_INSN_LUID (last)
4160 < DF_INSN_LUID (last_combined_insn))
4161 last = last_combined_insn;
4164 /* We're dealing with a reg that changed mode but not
4165 meaning, so we want to turn it into a subreg for
4166 the new mode. However, because of REG sharing and
4167 because its mode had already changed, we have to do
4168 it in two steps. First, replace any debug uses of
4169 reg, with its original mode temporarily restored,
4170 with this copy we have created; then, replace the
4171 copy with the SUBREG of the original shared reg,
4172 once again changed to the new mode. */
4173 propagate_for_debug (first, last, reg, tempreg,
4174 this_basic_block);
4175 adjust_reg_mode (reg, new_mode);
4176 propagate_for_debug (first, last, tempreg,
4177 lowpart_subreg (old_mode, reg, new_mode),
4178 this_basic_block);
4183 /* If we will be able to accept this, we have made a
4184 change to the destination of I3. This requires us to
4185 do a few adjustments. */
4187 if (changed_i3_dest)
4189 PATTERN (i3) = newpat;
4190 adjust_for_new_dest (i3);
4193 /* We now know that we can do this combination. Merge the insns and
4194 update the status of registers and LOG_LINKS. */
4196 if (undobuf.other_insn)
4198 rtx note, next;
4200 PATTERN (undobuf.other_insn) = other_pat;
4202 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4203 ensure that they are still valid. Then add any non-duplicate
4204 notes added by recog_for_combine. */
4205 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4207 next = XEXP (note, 1);
4209 if ((REG_NOTE_KIND (note) == REG_DEAD
4210 && !reg_referenced_p (XEXP (note, 0),
4211 PATTERN (undobuf.other_insn)))
4212 ||(REG_NOTE_KIND (note) == REG_UNUSED
4213 && !reg_set_p (XEXP (note, 0),
4214 PATTERN (undobuf.other_insn)))
4215 /* Simply drop equal note since it may be no longer valid
4216 for other_insn. It may be possible to record that CC
4217 register is changed and only discard those notes, but
4218 in practice it's unnecessary complication and doesn't
4219 give any meaningful improvement.
4221 See PR78559. */
4222 || REG_NOTE_KIND (note) == REG_EQUAL
4223 || REG_NOTE_KIND (note) == REG_EQUIV)
4224 remove_note (undobuf.other_insn, note);
4227 distribute_notes (new_other_notes, undobuf.other_insn,
4228 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4229 NULL_RTX);
4232 if (swap_i2i3)
4234 /* I3 now uses what used to be its destination and which is now
4235 I2's destination. This requires us to do a few adjustments. */
4236 PATTERN (i3) = newpat;
4237 adjust_for_new_dest (i3);
4240 if (swap_i2i3 || split_i2i3)
4242 /* We might need a LOG_LINK from I3 to I2. But then we used to
4243 have one, so we still will.
4245 However, some later insn might be using I2's dest and have
4246 a LOG_LINK pointing at I3. We should change it to point at
4247 I2 instead. */
4249 /* newi2pat is usually a SET here; however, recog_for_combine might
4250 have added some clobbers. */
4251 rtx x = newi2pat;
4252 if (GET_CODE (x) == PARALLEL)
4253 x = XVECEXP (newi2pat, 0, 0);
4255 if (REG_P (SET_DEST (x))
4256 || (GET_CODE (SET_DEST (x)) == SUBREG
4257 && REG_P (SUBREG_REG (SET_DEST (x)))))
4259 unsigned int regno = reg_or_subregno (SET_DEST (x));
4261 bool done = false;
4262 for (rtx_insn *insn = NEXT_INSN (i3);
4263 !done
4264 && insn
4265 && INSN_P (insn)
4266 && BLOCK_FOR_INSN (insn) == this_basic_block;
4267 insn = NEXT_INSN (insn))
4269 if (DEBUG_INSN_P (insn))
4270 continue;
4271 struct insn_link *link;
4272 FOR_EACH_LOG_LINK (link, insn)
4273 if (link->insn == i3 && link->regno == regno)
4275 link->insn = i2;
4276 done = true;
4277 break;
4284 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4285 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4286 rtx midnotes = 0;
4287 int from_luid;
4288 /* Compute which registers we expect to eliminate. newi2pat may be setting
4289 either i3dest or i2dest, so we must check it. */
4290 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4291 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4292 || !i2dest_killed
4293 ? 0 : i2dest);
4294 /* For i1, we need to compute both local elimination and global
4295 elimination information with respect to newi2pat because i1dest
4296 may be the same as i3dest, in which case newi2pat may be setting
4297 i1dest. Global information is used when distributing REG_DEAD
4298 note for i2 and i3, in which case it does matter if newi2pat sets
4299 i1dest or not.
4301 Local information is used when distributing REG_DEAD note for i1,
4302 in which case it doesn't matter if newi2pat sets i1dest or not.
4303 See PR62151, if we have four insns combination:
4304 i0: r0 <- i0src
4305 i1: r1 <- i1src (using r0)
4306 REG_DEAD (r0)
4307 i2: r0 <- i2src (using r1)
4308 i3: r3 <- i3src (using r0)
4309 ix: using r0
4310 From i1's point of view, r0 is eliminated, no matter if it is set
4311 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4312 should be discarded.
4314 Note local information only affects cases in forms like "I1->I2->I3",
4315 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4316 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4317 i0dest anyway. */
4318 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4319 || !i1dest_killed
4320 ? 0 : i1dest);
4321 rtx elim_i1 = (local_elim_i1 == 0
4322 || (newi2pat && reg_set_p (i1dest, newi2pat))
4323 ? 0 : i1dest);
4324 /* Same case as i1. */
4325 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4326 ? 0 : i0dest);
4327 rtx elim_i0 = (local_elim_i0 == 0
4328 || (newi2pat && reg_set_p (i0dest, newi2pat))
4329 ? 0 : i0dest);
4331 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4332 clear them. */
4333 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4334 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4335 if (i1)
4336 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4337 if (i0)
4338 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4340 /* Ensure that we do not have something that should not be shared but
4341 occurs multiple times in the new insns. Check this by first
4342 resetting all the `used' flags and then copying anything is shared. */
4344 reset_used_flags (i3notes);
4345 reset_used_flags (i2notes);
4346 reset_used_flags (i1notes);
4347 reset_used_flags (i0notes);
4348 reset_used_flags (newpat);
4349 reset_used_flags (newi2pat);
4350 if (undobuf.other_insn)
4351 reset_used_flags (PATTERN (undobuf.other_insn));
4353 i3notes = copy_rtx_if_shared (i3notes);
4354 i2notes = copy_rtx_if_shared (i2notes);
4355 i1notes = copy_rtx_if_shared (i1notes);
4356 i0notes = copy_rtx_if_shared (i0notes);
4357 newpat = copy_rtx_if_shared (newpat);
4358 newi2pat = copy_rtx_if_shared (newi2pat);
4359 if (undobuf.other_insn)
4360 reset_used_flags (PATTERN (undobuf.other_insn));
4362 INSN_CODE (i3) = insn_code_number;
4363 PATTERN (i3) = newpat;
4365 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4367 for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4368 link = XEXP (link, 1))
4370 if (substed_i2)
4372 /* I2SRC must still be meaningful at this point. Some
4373 splitting operations can invalidate I2SRC, but those
4374 operations do not apply to calls. */
4375 gcc_assert (i2src);
4376 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4377 i2dest, i2src);
4379 if (substed_i1)
4380 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4381 i1dest, i1src);
4382 if (substed_i0)
4383 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4384 i0dest, i0src);
4388 if (undobuf.other_insn)
4389 INSN_CODE (undobuf.other_insn) = other_code_number;
4391 /* We had one special case above where I2 had more than one set and
4392 we replaced a destination of one of those sets with the destination
4393 of I3. In that case, we have to update LOG_LINKS of insns later
4394 in this basic block. Note that this (expensive) case is rare.
4396 Also, in this case, we must pretend that all REG_NOTEs for I2
4397 actually came from I3, so that REG_UNUSED notes from I2 will be
4398 properly handled. */
4400 if (i3_subst_into_i2)
4402 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4403 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4404 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4405 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4406 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4407 && ! find_reg_note (i2, REG_UNUSED,
4408 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4409 for (temp_insn = NEXT_INSN (i2);
4410 temp_insn
4411 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4412 || BB_HEAD (this_basic_block) != temp_insn);
4413 temp_insn = NEXT_INSN (temp_insn))
4414 if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4415 FOR_EACH_LOG_LINK (link, temp_insn)
4416 if (link->insn == i2)
4417 link->insn = i3;
4419 if (i3notes)
4421 rtx link = i3notes;
4422 while (XEXP (link, 1))
4423 link = XEXP (link, 1);
4424 XEXP (link, 1) = i2notes;
4426 else
4427 i3notes = i2notes;
4428 i2notes = 0;
4431 LOG_LINKS (i3) = NULL;
4432 REG_NOTES (i3) = 0;
4433 LOG_LINKS (i2) = NULL;
4434 REG_NOTES (i2) = 0;
4436 if (newi2pat)
4438 if (MAY_HAVE_DEBUG_BIND_INSNS && i2scratch)
4439 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4440 this_basic_block);
4441 INSN_CODE (i2) = i2_code_number;
4442 PATTERN (i2) = newi2pat;
4444 else
4446 if (MAY_HAVE_DEBUG_BIND_INSNS && i2src)
4447 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4448 this_basic_block);
4449 SET_INSN_DELETED (i2);
4452 if (i1)
4454 LOG_LINKS (i1) = NULL;
4455 REG_NOTES (i1) = 0;
4456 if (MAY_HAVE_DEBUG_BIND_INSNS)
4457 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4458 this_basic_block);
4459 SET_INSN_DELETED (i1);
4462 if (i0)
4464 LOG_LINKS (i0) = NULL;
4465 REG_NOTES (i0) = 0;
4466 if (MAY_HAVE_DEBUG_BIND_INSNS)
4467 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4468 this_basic_block);
4469 SET_INSN_DELETED (i0);
4472 /* Get death notes for everything that is now used in either I3 or
4473 I2 and used to die in a previous insn. If we built two new
4474 patterns, move from I1 to I2 then I2 to I3 so that we get the
4475 proper movement on registers that I2 modifies. */
4477 if (i0)
4478 from_luid = DF_INSN_LUID (i0);
4479 else if (i1)
4480 from_luid = DF_INSN_LUID (i1);
4481 else
4482 from_luid = DF_INSN_LUID (i2);
4483 if (newi2pat)
4484 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4485 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4487 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4488 if (i3notes)
4489 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4490 elim_i2, elim_i1, elim_i0);
4491 if (i2notes)
4492 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4493 elim_i2, elim_i1, elim_i0);
4494 if (i1notes)
4495 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4496 elim_i2, local_elim_i1, local_elim_i0);
4497 if (i0notes)
4498 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4499 elim_i2, elim_i1, local_elim_i0);
4500 if (midnotes)
4501 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4502 elim_i2, elim_i1, elim_i0);
4504 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4505 know these are REG_UNUSED and want them to go to the desired insn,
4506 so we always pass it as i3. */
4508 if (newi2pat && new_i2_notes)
4509 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4510 NULL_RTX);
4512 if (new_i3_notes)
4513 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4514 NULL_RTX);
4516 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4517 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4518 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4519 in that case, it might delete I2. Similarly for I2 and I1.
4520 Show an additional death due to the REG_DEAD note we make here. If
4521 we discard it in distribute_notes, we will decrement it again. */
4523 if (i3dest_killed)
4525 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4526 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4527 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4528 elim_i1, elim_i0);
4529 else
4530 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4531 elim_i2, elim_i1, elim_i0);
4534 if (i2dest_in_i2src)
4536 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4537 if (newi2pat && reg_set_p (i2dest, newi2pat))
4538 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4539 NULL_RTX, NULL_RTX);
4540 else
4541 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4542 NULL_RTX, NULL_RTX, NULL_RTX);
4545 if (i1dest_in_i1src)
4547 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4548 if (newi2pat && reg_set_p (i1dest, newi2pat))
4549 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4550 NULL_RTX, NULL_RTX);
4551 else
4552 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4553 NULL_RTX, NULL_RTX, NULL_RTX);
4556 if (i0dest_in_i0src)
4558 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4559 if (newi2pat && reg_set_p (i0dest, newi2pat))
4560 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4561 NULL_RTX, NULL_RTX);
4562 else
4563 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4564 NULL_RTX, NULL_RTX, NULL_RTX);
4567 distribute_links (i3links);
4568 distribute_links (i2links);
4569 distribute_links (i1links);
4570 distribute_links (i0links);
4572 if (REG_P (i2dest))
4574 struct insn_link *link;
4575 rtx_insn *i2_insn = 0;
4576 rtx i2_val = 0, set;
4578 /* The insn that used to set this register doesn't exist, and
4579 this life of the register may not exist either. See if one of
4580 I3's links points to an insn that sets I2DEST. If it does,
4581 that is now the last known value for I2DEST. If we don't update
4582 this and I2 set the register to a value that depended on its old
4583 contents, we will get confused. If this insn is used, thing
4584 will be set correctly in combine_instructions. */
4585 FOR_EACH_LOG_LINK (link, i3)
4586 if ((set = single_set (link->insn)) != 0
4587 && rtx_equal_p (i2dest, SET_DEST (set)))
4588 i2_insn = link->insn, i2_val = SET_SRC (set);
4590 record_value_for_reg (i2dest, i2_insn, i2_val);
4592 /* If the reg formerly set in I2 died only once and that was in I3,
4593 zero its use count so it won't make `reload' do any work. */
4594 if (! added_sets_2
4595 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4596 && ! i2dest_in_i2src
4597 && REGNO (i2dest) < reg_n_sets_max)
4598 INC_REG_N_SETS (REGNO (i2dest), -1);
4601 if (i1 && REG_P (i1dest))
4603 struct insn_link *link;
4604 rtx_insn *i1_insn = 0;
4605 rtx i1_val = 0, set;
4607 FOR_EACH_LOG_LINK (link, i3)
4608 if ((set = single_set (link->insn)) != 0
4609 && rtx_equal_p (i1dest, SET_DEST (set)))
4610 i1_insn = link->insn, i1_val = SET_SRC (set);
4612 record_value_for_reg (i1dest, i1_insn, i1_val);
4614 if (! added_sets_1
4615 && ! i1dest_in_i1src
4616 && REGNO (i1dest) < reg_n_sets_max)
4617 INC_REG_N_SETS (REGNO (i1dest), -1);
4620 if (i0 && REG_P (i0dest))
4622 struct insn_link *link;
4623 rtx_insn *i0_insn = 0;
4624 rtx i0_val = 0, set;
4626 FOR_EACH_LOG_LINK (link, i3)
4627 if ((set = single_set (link->insn)) != 0
4628 && rtx_equal_p (i0dest, SET_DEST (set)))
4629 i0_insn = link->insn, i0_val = SET_SRC (set);
4631 record_value_for_reg (i0dest, i0_insn, i0_val);
4633 if (! added_sets_0
4634 && ! i0dest_in_i0src
4635 && REGNO (i0dest) < reg_n_sets_max)
4636 INC_REG_N_SETS (REGNO (i0dest), -1);
4639 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4640 been made to this insn. The order is important, because newi2pat
4641 can affect nonzero_bits of newpat. */
4642 if (newi2pat)
4643 note_pattern_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4644 note_pattern_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4647 if (undobuf.other_insn != NULL_RTX)
4649 if (dump_file)
4651 fprintf (dump_file, "modifying other_insn ");
4652 dump_insn_slim (dump_file, undobuf.other_insn);
4654 df_insn_rescan (undobuf.other_insn);
4657 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4659 if (dump_file)
4661 fprintf (dump_file, "modifying insn i0 ");
4662 dump_insn_slim (dump_file, i0);
4664 df_insn_rescan (i0);
4667 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4669 if (dump_file)
4671 fprintf (dump_file, "modifying insn i1 ");
4672 dump_insn_slim (dump_file, i1);
4674 df_insn_rescan (i1);
4677 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4679 if (dump_file)
4681 fprintf (dump_file, "modifying insn i2 ");
4682 dump_insn_slim (dump_file, i2);
4684 df_insn_rescan (i2);
4687 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4689 if (dump_file)
4691 fprintf (dump_file, "modifying insn i3 ");
4692 dump_insn_slim (dump_file, i3);
4694 df_insn_rescan (i3);
4697 /* Set new_direct_jump_p if a new return or simple jump instruction
4698 has been created. Adjust the CFG accordingly. */
4699 if (returnjump_p (i3) || any_uncondjump_p (i3))
4701 *new_direct_jump_p = 1;
4702 mark_jump_label (PATTERN (i3), i3, 0);
4703 update_cfg_for_uncondjump (i3);
4706 if (undobuf.other_insn != NULL_RTX
4707 && (returnjump_p (undobuf.other_insn)
4708 || any_uncondjump_p (undobuf.other_insn)))
4710 *new_direct_jump_p = 1;
4711 update_cfg_for_uncondjump (undobuf.other_insn);
4714 if (GET_CODE (PATTERN (i3)) == TRAP_IF
4715 && XEXP (PATTERN (i3), 0) == const1_rtx)
4717 basic_block bb = BLOCK_FOR_INSN (i3);
4718 gcc_assert (bb);
4719 remove_edge (split_block (bb, i3));
4720 emit_barrier_after_bb (bb);
4721 *new_direct_jump_p = 1;
4724 if (undobuf.other_insn
4725 && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4726 && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4728 basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4729 gcc_assert (bb);
4730 remove_edge (split_block (bb, undobuf.other_insn));
4731 emit_barrier_after_bb (bb);
4732 *new_direct_jump_p = 1;
4735 /* A noop might also need cleaning up of CFG, if it comes from the
4736 simplification of a jump. */
4737 if (JUMP_P (i3)
4738 && GET_CODE (newpat) == SET
4739 && SET_SRC (newpat) == pc_rtx
4740 && SET_DEST (newpat) == pc_rtx)
4742 *new_direct_jump_p = 1;
4743 update_cfg_for_uncondjump (i3);
4746 if (undobuf.other_insn != NULL_RTX
4747 && JUMP_P (undobuf.other_insn)
4748 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4749 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4750 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4752 *new_direct_jump_p = 1;
4753 update_cfg_for_uncondjump (undobuf.other_insn);
4756 combine_successes++;
4757 undo_commit ();
4759 rtx_insn *ret = newi2pat ? i2 : i3;
4760 if (added_links_insn && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (ret))
4761 ret = added_links_insn;
4762 if (added_notes_insn && DF_INSN_LUID (added_notes_insn) < DF_INSN_LUID (ret))
4763 ret = added_notes_insn;
4765 return ret;
4768 /* Get a marker for undoing to the current state. */
4770 static void *
4771 get_undo_marker (void)
4773 return undobuf.undos;
4776 /* Undo the modifications up to the marker. */
4778 static void
4779 undo_to_marker (void *marker)
4781 struct undo *undo, *next;
4783 for (undo = undobuf.undos; undo != marker; undo = next)
4785 gcc_assert (undo);
4787 next = undo->next;
4788 switch (undo->kind)
4790 case UNDO_RTX:
4791 *undo->where.r = undo->old_contents.r;
4792 break;
4793 case UNDO_INT:
4794 *undo->where.i = undo->old_contents.i;
4795 break;
4796 case UNDO_MODE:
4797 adjust_reg_mode (regno_reg_rtx[undo->where.regno],
4798 undo->old_contents.m);
4799 break;
4800 case UNDO_LINKS:
4801 *undo->where.l = undo->old_contents.l;
4802 break;
4803 default:
4804 gcc_unreachable ();
4807 undo->next = undobuf.frees;
4808 undobuf.frees = undo;
4811 undobuf.undos = (struct undo *) marker;
4814 /* Undo all the modifications recorded in undobuf. */
4816 static void
4817 undo_all (void)
4819 undo_to_marker (0);
4822 /* We've committed to accepting the changes we made. Move all
4823 of the undos to the free list. */
4825 static void
4826 undo_commit (void)
4828 struct undo *undo, *next;
4830 for (undo = undobuf.undos; undo; undo = next)
4832 next = undo->next;
4833 undo->next = undobuf.frees;
4834 undobuf.frees = undo;
4836 undobuf.undos = 0;
4839 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4840 where we have an arithmetic expression and return that point. LOC will
4841 be inside INSN.
4843 try_combine will call this function to see if an insn can be split into
4844 two insns. */
4846 static rtx *
4847 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4849 rtx x = *loc;
4850 enum rtx_code code = GET_CODE (x);
4851 rtx *split;
4852 unsigned HOST_WIDE_INT len = 0;
4853 HOST_WIDE_INT pos = 0;
4854 bool unsignedp = false;
4855 rtx inner = NULL_RTX;
4856 scalar_int_mode mode, inner_mode;
4858 /* First special-case some codes. */
4859 switch (code)
4861 case SUBREG:
4862 #ifdef INSN_SCHEDULING
4863 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4864 point. */
4865 if (MEM_P (SUBREG_REG (x)))
4866 return loc;
4867 #endif
4868 return find_split_point (&SUBREG_REG (x), insn, false);
4870 case MEM:
4871 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4872 using LO_SUM and HIGH. */
4873 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4874 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4876 machine_mode address_mode = get_address_mode (x);
4878 SUBST (XEXP (x, 0),
4879 gen_rtx_LO_SUM (address_mode,
4880 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4881 XEXP (x, 0)));
4882 return &XEXP (XEXP (x, 0), 0);
4885 /* If we have a PLUS whose second operand is a constant and the
4886 address is not valid, perhaps we can split it up using
4887 the machine-specific way to split large constants. We use
4888 the first pseudo-reg (one of the virtual regs) as a placeholder;
4889 it will not remain in the result. */
4890 if (GET_CODE (XEXP (x, 0)) == PLUS
4891 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4892 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4893 MEM_ADDR_SPACE (x)))
4895 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4896 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4897 subst_insn);
4899 /* This should have produced two insns, each of which sets our
4900 placeholder. If the source of the second is a valid address,
4901 we can put both sources together and make a split point
4902 in the middle. */
4904 if (seq
4905 && NEXT_INSN (seq) != NULL_RTX
4906 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4907 && NONJUMP_INSN_P (seq)
4908 && GET_CODE (PATTERN (seq)) == SET
4909 && SET_DEST (PATTERN (seq)) == reg
4910 && ! reg_mentioned_p (reg,
4911 SET_SRC (PATTERN (seq)))
4912 && NONJUMP_INSN_P (NEXT_INSN (seq))
4913 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4914 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4915 && memory_address_addr_space_p
4916 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4917 MEM_ADDR_SPACE (x)))
4919 rtx src1 = SET_SRC (PATTERN (seq));
4920 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4922 /* Replace the placeholder in SRC2 with SRC1. If we can
4923 find where in SRC2 it was placed, that can become our
4924 split point and we can replace this address with SRC2.
4925 Just try two obvious places. */
4927 src2 = replace_rtx (src2, reg, src1);
4928 split = 0;
4929 if (XEXP (src2, 0) == src1)
4930 split = &XEXP (src2, 0);
4931 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4932 && XEXP (XEXP (src2, 0), 0) == src1)
4933 split = &XEXP (XEXP (src2, 0), 0);
4935 if (split)
4937 SUBST (XEXP (x, 0), src2);
4938 return split;
4942 /* If that didn't work and we have a nested plus, like:
4943 ((REG1 * CONST1) + REG2) + CONST2 and (REG1 + REG2) + CONST2
4944 is valid address, try to split (REG1 * CONST1). */
4945 if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
4946 && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
4947 && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
4948 && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SUBREG
4949 && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
4950 0), 0)))))
4952 rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 0);
4953 XEXP (XEXP (XEXP (x, 0), 0), 0) = reg;
4954 if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4955 MEM_ADDR_SPACE (x)))
4957 XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
4958 return &XEXP (XEXP (XEXP (x, 0), 0), 0);
4960 XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
4962 else if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
4963 && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
4964 && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
4965 && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SUBREG
4966 && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
4967 0), 1)))))
4969 rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 1);
4970 XEXP (XEXP (XEXP (x, 0), 0), 1) = reg;
4971 if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4972 MEM_ADDR_SPACE (x)))
4974 XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
4975 return &XEXP (XEXP (XEXP (x, 0), 0), 1);
4977 XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
4980 /* If that didn't work, perhaps the first operand is complex and
4981 needs to be computed separately, so make a split point there.
4982 This will occur on machines that just support REG + CONST
4983 and have a constant moved through some previous computation. */
4984 if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4985 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4986 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4987 return &XEXP (XEXP (x, 0), 0);
4990 /* If we have a PLUS whose first operand is complex, try computing it
4991 separately by making a split there. */
4992 if (GET_CODE (XEXP (x, 0)) == PLUS
4993 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4994 MEM_ADDR_SPACE (x))
4995 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4996 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4997 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4998 return &XEXP (XEXP (x, 0), 0);
4999 break;
5001 case SET:
5002 /* See if we can split SET_SRC as it stands. */
5003 split = find_split_point (&SET_SRC (x), insn, true);
5004 if (split && split != &SET_SRC (x))
5005 return split;
5007 /* See if we can split SET_DEST as it stands. */
5008 split = find_split_point (&SET_DEST (x), insn, false);
5009 if (split && split != &SET_DEST (x))
5010 return split;
5012 /* See if this is a bitfield assignment with everything constant. If
5013 so, this is an IOR of an AND, so split it into that. */
5014 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5015 && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
5016 &inner_mode)
5017 && HWI_COMPUTABLE_MODE_P (inner_mode)
5018 && CONST_INT_P (XEXP (SET_DEST (x), 1))
5019 && CONST_INT_P (XEXP (SET_DEST (x), 2))
5020 && CONST_INT_P (SET_SRC (x))
5021 && ((INTVAL (XEXP (SET_DEST (x), 1))
5022 + INTVAL (XEXP (SET_DEST (x), 2)))
5023 <= GET_MODE_PRECISION (inner_mode))
5024 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
5026 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
5027 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
5028 rtx dest = XEXP (SET_DEST (x), 0);
5029 unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << len) - 1;
5030 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x)) & mask;
5031 rtx or_mask;
5033 if (BITS_BIG_ENDIAN)
5034 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5036 or_mask = gen_int_mode (src << pos, inner_mode);
5037 if (src == mask)
5038 SUBST (SET_SRC (x),
5039 simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5040 else
5042 rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5043 SUBST (SET_SRC (x),
5044 simplify_gen_binary (IOR, inner_mode,
5045 simplify_gen_binary (AND, inner_mode,
5046 dest, negmask),
5047 or_mask));
5050 SUBST (SET_DEST (x), dest);
5052 split = find_split_point (&SET_SRC (x), insn, true);
5053 if (split && split != &SET_SRC (x))
5054 return split;
5057 /* Otherwise, see if this is an operation that we can split into two.
5058 If so, try to split that. */
5059 code = GET_CODE (SET_SRC (x));
5061 switch (code)
5063 case AND:
5064 /* If we are AND'ing with a large constant that is only a single
5065 bit and the result is only being used in a context where we
5066 need to know if it is zero or nonzero, replace it with a bit
5067 extraction. This will avoid the large constant, which might
5068 have taken more than one insn to make. If the constant were
5069 not a valid argument to the AND but took only one insn to make,
5070 this is no worse, but if it took more than one insn, it will
5071 be better. */
5073 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5074 && REG_P (XEXP (SET_SRC (x), 0))
5075 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5076 && REG_P (SET_DEST (x))
5077 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5078 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5079 && XEXP (*split, 0) == SET_DEST (x)
5080 && XEXP (*split, 1) == const0_rtx)
5082 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5083 XEXP (SET_SRC (x), 0),
5084 pos, NULL_RTX, 1,
5085 true, false, false);
5086 if (extraction != 0)
5088 SUBST (SET_SRC (x), extraction);
5089 return find_split_point (loc, insn, false);
5092 break;
5094 case NE:
5095 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5096 is known to be on, this can be converted into a NEG of a shift. */
5097 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5098 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5099 && ((pos = exact_log2 (nonzero_bits (XEXP (SET_SRC (x), 0),
5100 GET_MODE (XEXP (SET_SRC (x),
5101 0))))) >= 1))
5103 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5104 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5105 SUBST (SET_SRC (x),
5106 gen_rtx_NEG (mode,
5107 gen_rtx_LSHIFTRT (mode,
5108 XEXP (SET_SRC (x), 0),
5109 pos_rtx)));
5111 split = find_split_point (&SET_SRC (x), insn, true);
5112 if (split && split != &SET_SRC (x))
5113 return split;
5115 break;
5117 case SIGN_EXTEND:
5118 inner = XEXP (SET_SRC (x), 0);
5120 /* We can't optimize if either mode is a partial integer
5121 mode as we don't know how many bits are significant
5122 in those modes. */
5123 if (!is_int_mode (GET_MODE (inner), &inner_mode)
5124 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5125 break;
5127 pos = 0;
5128 len = GET_MODE_PRECISION (inner_mode);
5129 unsignedp = false;
5130 break;
5132 case SIGN_EXTRACT:
5133 case ZERO_EXTRACT:
5134 if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5135 &inner_mode)
5136 && CONST_INT_P (XEXP (SET_SRC (x), 1))
5137 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5139 inner = XEXP (SET_SRC (x), 0);
5140 len = INTVAL (XEXP (SET_SRC (x), 1));
5141 pos = INTVAL (XEXP (SET_SRC (x), 2));
5143 if (BITS_BIG_ENDIAN)
5144 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5145 unsignedp = (code == ZERO_EXTRACT);
5147 break;
5149 default:
5150 break;
5153 if (len
5154 && known_subrange_p (pos, len,
5155 0, GET_MODE_PRECISION (GET_MODE (inner)))
5156 && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5158 /* For unsigned, we have a choice of a shift followed by an
5159 AND or two shifts. Use two shifts for field sizes where the
5160 constant might be too large. We assume here that we can
5161 always at least get 8-bit constants in an AND insn, which is
5162 true for every current RISC. */
5164 if (unsignedp && len <= 8)
5166 unsigned HOST_WIDE_INT mask
5167 = (HOST_WIDE_INT_1U << len) - 1;
5168 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5169 SUBST (SET_SRC (x),
5170 gen_rtx_AND (mode,
5171 gen_rtx_LSHIFTRT
5172 (mode, gen_lowpart (mode, inner), pos_rtx),
5173 gen_int_mode (mask, mode)));
5175 split = find_split_point (&SET_SRC (x), insn, true);
5176 if (split && split != &SET_SRC (x))
5177 return split;
5179 else
5181 int left_bits = GET_MODE_PRECISION (mode) - len - pos;
5182 int right_bits = GET_MODE_PRECISION (mode) - len;
5183 SUBST (SET_SRC (x),
5184 gen_rtx_fmt_ee
5185 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5186 gen_rtx_ASHIFT (mode,
5187 gen_lowpart (mode, inner),
5188 gen_int_shift_amount (mode, left_bits)),
5189 gen_int_shift_amount (mode, right_bits)));
5191 split = find_split_point (&SET_SRC (x), insn, true);
5192 if (split && split != &SET_SRC (x))
5193 return split;
5197 /* See if this is a simple operation with a constant as the second
5198 operand. It might be that this constant is out of range and hence
5199 could be used as a split point. */
5200 if (BINARY_P (SET_SRC (x))
5201 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5202 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5203 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5204 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5205 return &XEXP (SET_SRC (x), 1);
5207 /* Finally, see if this is a simple operation with its first operand
5208 not in a register. The operation might require this operand in a
5209 register, so return it as a split point. We can always do this
5210 because if the first operand were another operation, we would have
5211 already found it as a split point. */
5212 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5213 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5214 return &XEXP (SET_SRC (x), 0);
5216 return 0;
5218 case AND:
5219 case IOR:
5220 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5221 it is better to write this as (not (ior A B)) so we can split it.
5222 Similarly for IOR. */
5223 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5225 SUBST (*loc,
5226 gen_rtx_NOT (GET_MODE (x),
5227 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5228 GET_MODE (x),
5229 XEXP (XEXP (x, 0), 0),
5230 XEXP (XEXP (x, 1), 0))));
5231 return find_split_point (loc, insn, set_src);
5234 /* Many RISC machines have a large set of logical insns. If the
5235 second operand is a NOT, put it first so we will try to split the
5236 other operand first. */
5237 if (GET_CODE (XEXP (x, 1)) == NOT)
5239 rtx tem = XEXP (x, 0);
5240 SUBST (XEXP (x, 0), XEXP (x, 1));
5241 SUBST (XEXP (x, 1), tem);
5243 break;
5245 case PLUS:
5246 case MINUS:
5247 /* Canonicalization can produce (minus A (mult B C)), where C is a
5248 constant. It may be better to try splitting (plus (mult B -C) A)
5249 instead if this isn't a multiply by a power of two. */
5250 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5251 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5252 && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5254 machine_mode mode = GET_MODE (x);
5255 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5256 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5257 SUBST (*loc, gen_rtx_PLUS (mode,
5258 gen_rtx_MULT (mode,
5259 XEXP (XEXP (x, 1), 0),
5260 gen_int_mode (other_int,
5261 mode)),
5262 XEXP (x, 0)));
5263 return find_split_point (loc, insn, set_src);
5266 /* Split at a multiply-accumulate instruction. However if this is
5267 the SET_SRC, we likely do not have such an instruction and it's
5268 worthless to try this split. */
5269 if (!set_src
5270 && (GET_CODE (XEXP (x, 0)) == MULT
5271 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5272 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5273 return loc;
5275 default:
5276 break;
5279 /* Otherwise, select our actions depending on our rtx class. */
5280 switch (GET_RTX_CLASS (code))
5282 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5283 case RTX_TERNARY:
5284 split = find_split_point (&XEXP (x, 2), insn, false);
5285 if (split)
5286 return split;
5287 /* fall through */
5288 case RTX_BIN_ARITH:
5289 case RTX_COMM_ARITH:
5290 case RTX_COMPARE:
5291 case RTX_COMM_COMPARE:
5292 split = find_split_point (&XEXP (x, 1), insn, false);
5293 if (split)
5294 return split;
5295 /* fall through */
5296 case RTX_UNARY:
5297 /* Some machines have (and (shift ...) ...) insns. If X is not
5298 an AND, but XEXP (X, 0) is, use it as our split point. */
5299 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5300 return &XEXP (x, 0);
5302 split = find_split_point (&XEXP (x, 0), insn, false);
5303 if (split)
5304 return split;
5305 return loc;
5307 default:
5308 /* Otherwise, we don't have a split point. */
5309 return 0;
5313 /* Throughout X, replace FROM with TO, and return the result.
5314 The result is TO if X is FROM;
5315 otherwise the result is X, but its contents may have been modified.
5316 If they were modified, a record was made in undobuf so that
5317 undo_all will (among other things) return X to its original state.
5319 If the number of changes necessary is too much to record to undo,
5320 the excess changes are not made, so the result is invalid.
5321 The changes already made can still be undone.
5322 undobuf.num_undo is incremented for such changes, so by testing that
5323 the caller can tell whether the result is valid.
5325 `n_occurrences' is incremented each time FROM is replaced.
5327 IN_DEST is true if we are processing the SET_DEST of a SET.
5329 IN_COND is true if we are at the top level of a condition.
5331 UNIQUE_COPY is true if each substitution must be unique. We do this
5332 by copying if `n_occurrences' is nonzero. */
5334 static rtx
5335 subst (rtx x, rtx from, rtx to, bool in_dest, bool in_cond, bool unique_copy)
5337 enum rtx_code code = GET_CODE (x);
5338 machine_mode op0_mode = VOIDmode;
5339 const char *fmt;
5340 int len, i;
5341 rtx new_rtx;
5343 /* Two expressions are equal if they are identical copies of a shared
5344 RTX or if they are both registers with the same register number
5345 and mode. */
5347 #define COMBINE_RTX_EQUAL_P(X,Y) \
5348 ((X) == (Y) \
5349 || (REG_P (X) && REG_P (Y) \
5350 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5352 /* Do not substitute into clobbers of regs -- this will never result in
5353 valid RTL. */
5354 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5355 return x;
5357 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5359 n_occurrences++;
5360 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5363 /* If X and FROM are the same register but different modes, they
5364 will not have been seen as equal above. However, the log links code
5365 will make a LOG_LINKS entry for that case. If we do nothing, we
5366 will try to rerecognize our original insn and, when it succeeds,
5367 we will delete the feeding insn, which is incorrect.
5369 So force this insn not to match in this (rare) case. */
5370 if (! in_dest && code == REG && REG_P (from)
5371 && reg_overlap_mentioned_p (x, from))
5372 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5374 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5375 of which may contain things that can be combined. */
5376 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5377 return x;
5379 /* It is possible to have a subexpression appear twice in the insn.
5380 Suppose that FROM is a register that appears within TO.
5381 Then, after that subexpression has been scanned once by `subst',
5382 the second time it is scanned, TO may be found. If we were
5383 to scan TO here, we would find FROM within it and create a
5384 self-referent rtl structure which is completely wrong. */
5385 if (COMBINE_RTX_EQUAL_P (x, to))
5386 return to;
5388 /* Parallel asm_operands need special attention because all of the
5389 inputs are shared across the arms. Furthermore, unsharing the
5390 rtl results in recognition failures. Failure to handle this case
5391 specially can result in circular rtl.
5393 Solve this by doing a normal pass across the first entry of the
5394 parallel, and only processing the SET_DESTs of the subsequent
5395 entries. Ug. */
5397 if (code == PARALLEL
5398 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5399 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5401 new_rtx = subst (XVECEXP (x, 0, 0), from, to, false, false, unique_copy);
5403 /* If this substitution failed, this whole thing fails. */
5404 if (GET_CODE (new_rtx) == CLOBBER
5405 && XEXP (new_rtx, 0) == const0_rtx)
5406 return new_rtx;
5408 SUBST (XVECEXP (x, 0, 0), new_rtx);
5410 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5412 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5414 if (!REG_P (dest) && GET_CODE (dest) != PC)
5416 new_rtx = subst (dest, from, to, false, false, unique_copy);
5418 /* If this substitution failed, this whole thing fails. */
5419 if (GET_CODE (new_rtx) == CLOBBER
5420 && XEXP (new_rtx, 0) == const0_rtx)
5421 return new_rtx;
5423 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5427 else
5429 len = GET_RTX_LENGTH (code);
5430 fmt = GET_RTX_FORMAT (code);
5432 /* We don't need to process a SET_DEST that is a register or PC, so
5433 set up to skip this common case. All other cases where we want
5434 to suppress replacing something inside a SET_SRC are handled via
5435 the IN_DEST operand. */
5436 if (code == SET
5437 && (REG_P (SET_DEST (x))
5438 || GET_CODE (SET_DEST (x)) == PC))
5439 fmt = "ie";
5441 /* Trying to simplify the operands of a widening MULT is not likely
5442 to create RTL matching a machine insn. */
5443 if (code == MULT
5444 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5445 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5446 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5447 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5448 && REG_P (XEXP (XEXP (x, 0), 0))
5449 && REG_P (XEXP (XEXP (x, 1), 0))
5450 && from == to)
5451 return x;
5454 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5455 constant. */
5456 if (fmt[0] == 'e')
5457 op0_mode = GET_MODE (XEXP (x, 0));
5459 for (i = 0; i < len; i++)
5461 if (fmt[i] == 'E')
5463 int j;
5464 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5466 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5468 new_rtx = (unique_copy && n_occurrences
5469 ? copy_rtx (to) : to);
5470 n_occurrences++;
5472 else
5474 new_rtx = subst (XVECEXP (x, i, j), from, to,
5475 false, false, unique_copy);
5477 /* If this substitution failed, this whole thing
5478 fails. */
5479 if (GET_CODE (new_rtx) == CLOBBER
5480 && XEXP (new_rtx, 0) == const0_rtx)
5481 return new_rtx;
5484 SUBST (XVECEXP (x, i, j), new_rtx);
5487 else if (fmt[i] == 'e')
5489 /* If this is a register being set, ignore it. */
5490 new_rtx = XEXP (x, i);
5491 if (in_dest
5492 && i == 0
5493 && (((code == SUBREG || code == ZERO_EXTRACT)
5494 && REG_P (new_rtx))
5495 || code == STRICT_LOW_PART))
5498 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5500 /* In general, don't install a subreg involving two
5501 modes not tieable. It can worsen register
5502 allocation, and can even make invalid reload
5503 insns, since the reg inside may need to be copied
5504 from in the outside mode, and that may be invalid
5505 if it is an fp reg copied in integer mode.
5507 We allow an exception to this: It is valid if
5508 it is inside another SUBREG and the mode of that
5509 SUBREG and the mode of the inside of TO is
5510 tieable. */
5512 if (GET_CODE (to) == SUBREG
5513 && !targetm.modes_tieable_p (GET_MODE (to),
5514 GET_MODE (SUBREG_REG (to)))
5515 && ! (code == SUBREG
5516 && (targetm.modes_tieable_p
5517 (GET_MODE (x), GET_MODE (SUBREG_REG (to))))))
5518 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5520 if (code == SUBREG
5521 && REG_P (to)
5522 && REGNO (to) < FIRST_PSEUDO_REGISTER
5523 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5524 SUBREG_BYTE (x),
5525 GET_MODE (x)) < 0)
5526 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5528 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5529 n_occurrences++;
5531 else
5532 /* If we are in a SET_DEST, suppress most cases unless we
5533 have gone inside a MEM, in which case we want to
5534 simplify the address. We assume here that things that
5535 are actually part of the destination have their inner
5536 parts in the first expression. This is true for SUBREG,
5537 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5538 things aside from REG and MEM that should appear in a
5539 SET_DEST. */
5540 new_rtx = subst (XEXP (x, i), from, to,
5541 (((in_dest
5542 && (code == SUBREG || code == STRICT_LOW_PART
5543 || code == ZERO_EXTRACT))
5544 || code == SET)
5545 && i == 0),
5546 code == IF_THEN_ELSE && i == 0,
5547 unique_copy);
5549 /* If we found that we will have to reject this combination,
5550 indicate that by returning the CLOBBER ourselves, rather than
5551 an expression containing it. This will speed things up as
5552 well as prevent accidents where two CLOBBERs are considered
5553 to be equal, thus producing an incorrect simplification. */
5555 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5556 return new_rtx;
5558 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5560 machine_mode mode = GET_MODE (x);
5562 x = simplify_subreg (GET_MODE (x), new_rtx,
5563 GET_MODE (SUBREG_REG (x)),
5564 SUBREG_BYTE (x));
5565 if (! x)
5566 x = gen_rtx_CLOBBER (mode, const0_rtx);
5568 else if (CONST_SCALAR_INT_P (new_rtx)
5569 && (GET_CODE (x) == ZERO_EXTEND
5570 || GET_CODE (x) == SIGN_EXTEND
5571 || GET_CODE (x) == FLOAT
5572 || GET_CODE (x) == UNSIGNED_FLOAT))
5574 x = simplify_unary_operation (GET_CODE (x), GET_MODE (x),
5575 new_rtx,
5576 GET_MODE (XEXP (x, 0)));
5577 if (!x)
5578 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5580 /* CONST_INTs shouldn't be substituted into PRE_DEC, PRE_MODIFY
5581 etc. arguments, otherwise we can ICE before trying to recog
5582 it. See PR104446. */
5583 else if (CONST_SCALAR_INT_P (new_rtx)
5584 && GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
5585 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5586 else
5587 SUBST (XEXP (x, i), new_rtx);
5592 /* Check if we are loading something from the constant pool via float
5593 extension; in this case we would undo compress_float_constant
5594 optimization and degenerate constant load to an immediate value. */
5595 if (GET_CODE (x) == FLOAT_EXTEND
5596 && MEM_P (XEXP (x, 0))
5597 && MEM_READONLY_P (XEXP (x, 0)))
5599 rtx tmp = avoid_constant_pool_reference (x);
5600 if (x != tmp)
5601 return x;
5604 /* Try to simplify X. If the simplification changed the code, it is likely
5605 that further simplification will help, so loop, but limit the number
5606 of repetitions that will be performed. */
5608 for (i = 0; i < 4; i++)
5610 /* If X is sufficiently simple, don't bother trying to do anything
5611 with it. */
5612 if (code != CONST_INT && code != REG && code != CLOBBER)
5613 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5615 if (GET_CODE (x) == code)
5616 break;
5618 code = GET_CODE (x);
5620 /* We no longer know the original mode of operand 0 since we
5621 have changed the form of X) */
5622 op0_mode = VOIDmode;
5625 return x;
5628 /* If X is a commutative operation whose operands are not in the canonical
5629 order, use substitutions to swap them. */
5631 static void
5632 maybe_swap_commutative_operands (rtx x)
5634 if (COMMUTATIVE_ARITH_P (x)
5635 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5637 rtx temp = XEXP (x, 0);
5638 SUBST (XEXP (x, 0), XEXP (x, 1));
5639 SUBST (XEXP (x, 1), temp);
5642 unsigned n_elts = 0;
5643 if (GET_CODE (x) == VEC_MERGE
5644 && CONST_INT_P (XEXP (x, 2))
5645 && GET_MODE_NUNITS (GET_MODE (x)).is_constant (&n_elts)
5646 && (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1))
5647 /* Two operands have same precedence, then
5648 first bit of mask select first operand. */
5649 || (!swap_commutative_operands_p (XEXP (x, 1), XEXP (x, 0))
5650 && !(UINTVAL (XEXP (x, 2)) & 1))))
5652 rtx temp = XEXP (x, 0);
5653 unsigned HOST_WIDE_INT sel = UINTVAL (XEXP (x, 2));
5654 unsigned HOST_WIDE_INT mask = HOST_WIDE_INT_1U;
5655 if (n_elts == HOST_BITS_PER_WIDE_INT)
5656 mask = -1;
5657 else
5658 mask = (HOST_WIDE_INT_1U << n_elts) - 1;
5659 SUBST (XEXP (x, 0), XEXP (x, 1));
5660 SUBST (XEXP (x, 1), temp);
5661 SUBST (XEXP (x, 2), GEN_INT (~sel & mask));
5665 /* Simplify X, a piece of RTL. We just operate on the expression at the
5666 outer level; call `subst' to simplify recursively. Return the new
5667 expression.
5669 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is true
5670 if we are inside a SET_DEST. IN_COND is true if we are at the top level
5671 of a condition. */
5673 static rtx
5674 combine_simplify_rtx (rtx x, machine_mode op0_mode, bool in_dest, bool in_cond)
5676 enum rtx_code code = GET_CODE (x);
5677 machine_mode mode = GET_MODE (x);
5678 scalar_int_mode int_mode;
5679 rtx temp;
5680 int i;
5682 /* If this is a commutative operation, put a constant last and a complex
5683 expression first. We don't need to do this for comparisons here. */
5684 maybe_swap_commutative_operands (x);
5686 /* Try to fold this expression in case we have constants that weren't
5687 present before. */
5688 temp = 0;
5689 switch (GET_RTX_CLASS (code))
5691 case RTX_UNARY:
5692 if (op0_mode == VOIDmode)
5693 op0_mode = GET_MODE (XEXP (x, 0));
5694 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5695 break;
5696 case RTX_COMPARE:
5697 case RTX_COMM_COMPARE:
5699 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5700 if (cmp_mode == VOIDmode)
5702 cmp_mode = GET_MODE (XEXP (x, 1));
5703 if (cmp_mode == VOIDmode)
5704 cmp_mode = op0_mode;
5706 temp = simplify_relational_operation (code, mode, cmp_mode,
5707 XEXP (x, 0), XEXP (x, 1));
5709 break;
5710 case RTX_COMM_ARITH:
5711 case RTX_BIN_ARITH:
5712 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5713 break;
5714 case RTX_BITFIELD_OPS:
5715 case RTX_TERNARY:
5716 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5717 XEXP (x, 1), XEXP (x, 2));
5718 break;
5719 default:
5720 break;
5723 if (temp)
5725 x = temp;
5726 code = GET_CODE (temp);
5727 op0_mode = VOIDmode;
5728 mode = GET_MODE (temp);
5731 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5732 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5733 things. Check for cases where both arms are testing the same
5734 condition.
5736 Don't do anything if all operands are very simple. */
5738 if ((BINARY_P (x)
5739 && ((!OBJECT_P (XEXP (x, 0))
5740 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5741 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5742 || (!OBJECT_P (XEXP (x, 1))
5743 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5744 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5745 || (UNARY_P (x)
5746 && (!OBJECT_P (XEXP (x, 0))
5747 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5748 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5750 rtx cond, true_rtx, false_rtx;
5752 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5753 if (cond != 0
5754 /* If everything is a comparison, what we have is highly unlikely
5755 to be simpler, so don't use it. */
5756 && ! (COMPARISON_P (x)
5757 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx)))
5758 /* Similarly, if we end up with one of the expressions the same
5759 as the original, it is certainly not simpler. */
5760 && ! rtx_equal_p (x, true_rtx)
5761 && ! rtx_equal_p (x, false_rtx))
5763 rtx cop1 = const0_rtx;
5764 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5766 if (cond_code == NE && COMPARISON_P (cond))
5767 return x;
5769 /* Simplify the alternative arms; this may collapse the true and
5770 false arms to store-flag values. Be careful to use copy_rtx
5771 here since true_rtx or false_rtx might share RTL with x as a
5772 result of the if_then_else_cond call above. */
5773 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx,
5774 false, false, false);
5775 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx,
5776 false, false, false);
5778 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5779 is unlikely to be simpler. */
5780 if (general_operand (true_rtx, VOIDmode)
5781 && general_operand (false_rtx, VOIDmode))
5783 enum rtx_code reversed;
5785 /* Restarting if we generate a store-flag expression will cause
5786 us to loop. Just drop through in this case. */
5788 /* If the result values are STORE_FLAG_VALUE and zero, we can
5789 just make the comparison operation. */
5790 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5791 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5792 cond, cop1);
5793 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5794 && ((reversed = reversed_comparison_code_parts
5795 (cond_code, cond, cop1, NULL))
5796 != UNKNOWN))
5797 x = simplify_gen_relational (reversed, mode, VOIDmode,
5798 cond, cop1);
5800 /* Likewise, we can make the negate of a comparison operation
5801 if the result values are - STORE_FLAG_VALUE and zero. */
5802 else if (CONST_INT_P (true_rtx)
5803 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5804 && false_rtx == const0_rtx)
5805 x = simplify_gen_unary (NEG, mode,
5806 simplify_gen_relational (cond_code,
5807 mode, VOIDmode,
5808 cond, cop1),
5809 mode);
5810 else if (CONST_INT_P (false_rtx)
5811 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5812 && true_rtx == const0_rtx
5813 && ((reversed = reversed_comparison_code_parts
5814 (cond_code, cond, cop1, NULL))
5815 != UNKNOWN))
5816 x = simplify_gen_unary (NEG, mode,
5817 simplify_gen_relational (reversed,
5818 mode, VOIDmode,
5819 cond, cop1),
5820 mode);
5822 code = GET_CODE (x);
5823 op0_mode = VOIDmode;
5828 /* First see if we can apply the inverse distributive law. */
5829 if (code == PLUS || code == MINUS
5830 || code == AND || code == IOR || code == XOR)
5832 x = apply_distributive_law (x);
5833 code = GET_CODE (x);
5834 op0_mode = VOIDmode;
5837 /* If CODE is an associative operation not otherwise handled, see if we
5838 can associate some operands. This can win if they are constants or
5839 if they are logically related (i.e. (a & b) & a). */
5840 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5841 || code == AND || code == IOR || code == XOR
5842 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5843 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5844 || (flag_associative_math && FLOAT_MODE_P (mode))))
5846 if (GET_CODE (XEXP (x, 0)) == code)
5848 rtx other = XEXP (XEXP (x, 0), 0);
5849 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5850 rtx inner_op1 = XEXP (x, 1);
5851 rtx inner;
5853 /* Make sure we pass the constant operand if any as the second
5854 one if this is a commutative operation. */
5855 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5856 std::swap (inner_op0, inner_op1);
5857 inner = simplify_binary_operation (code == MINUS ? PLUS
5858 : code == DIV ? MULT
5859 : code,
5860 mode, inner_op0, inner_op1);
5862 /* For commutative operations, try the other pair if that one
5863 didn't simplify. */
5864 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5866 other = XEXP (XEXP (x, 0), 1);
5867 inner = simplify_binary_operation (code, mode,
5868 XEXP (XEXP (x, 0), 0),
5869 XEXP (x, 1));
5872 if (inner)
5873 return simplify_gen_binary (code, mode, other, inner);
5877 /* A little bit of algebraic simplification here. */
5878 switch (code)
5880 case MEM:
5881 /* Ensure that our address has any ASHIFTs converted to MULT in case
5882 address-recognizing predicates are called later. */
5883 temp = make_compound_operation (XEXP (x, 0), MEM);
5884 SUBST (XEXP (x, 0), temp);
5885 break;
5887 case SUBREG:
5888 if (op0_mode == VOIDmode)
5889 op0_mode = GET_MODE (SUBREG_REG (x));
5891 /* See if this can be moved to simplify_subreg. */
5892 if (CONSTANT_P (SUBREG_REG (x))
5893 && known_eq (subreg_lowpart_offset (mode, op0_mode), SUBREG_BYTE (x))
5894 /* Don't call gen_lowpart if the inner mode
5895 is VOIDmode and we cannot simplify it, as SUBREG without
5896 inner mode is invalid. */
5897 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5898 || gen_lowpart_common (mode, SUBREG_REG (x))))
5899 return gen_lowpart (mode, SUBREG_REG (x));
5901 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5902 break;
5904 rtx temp;
5905 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5906 SUBREG_BYTE (x));
5907 if (temp)
5908 return temp;
5910 /* If op is known to have all lower bits zero, the result is zero. */
5911 scalar_int_mode int_mode, int_op0_mode;
5912 if (!in_dest
5913 && is_a <scalar_int_mode> (mode, &int_mode)
5914 && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5915 && (GET_MODE_PRECISION (int_mode)
5916 < GET_MODE_PRECISION (int_op0_mode))
5917 && known_eq (subreg_lowpart_offset (int_mode, int_op0_mode),
5918 SUBREG_BYTE (x))
5919 && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5920 && ((nonzero_bits (SUBREG_REG (x), int_op0_mode)
5921 & GET_MODE_MASK (int_mode)) == 0)
5922 && !side_effects_p (SUBREG_REG (x)))
5923 return CONST0_RTX (int_mode);
5926 /* Don't change the mode of the MEM if that would change the meaning
5927 of the address. */
5928 if (MEM_P (SUBREG_REG (x))
5929 && (MEM_VOLATILE_P (SUBREG_REG (x))
5930 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5931 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5932 return gen_rtx_CLOBBER (mode, const0_rtx);
5934 /* Note that we cannot do any narrowing for non-constants since
5935 we might have been counting on using the fact that some bits were
5936 zero. We now do this in the SET. */
5938 break;
5940 case NEG:
5941 temp = expand_compound_operation (XEXP (x, 0));
5943 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5944 replaced by (lshiftrt X C). This will convert
5945 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5947 if (GET_CODE (temp) == ASHIFTRT
5948 && CONST_INT_P (XEXP (temp, 1))
5949 && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
5950 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5951 INTVAL (XEXP (temp, 1)));
5953 /* If X has only a single bit that might be nonzero, say, bit I, convert
5954 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5955 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5956 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5957 or a SUBREG of one since we'd be making the expression more
5958 complex if it was just a register. */
5960 if (!REG_P (temp)
5961 && ! (GET_CODE (temp) == SUBREG
5962 && REG_P (SUBREG_REG (temp)))
5963 && is_a <scalar_int_mode> (mode, &int_mode)
5964 && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
5966 rtx temp1 = simplify_shift_const
5967 (NULL_RTX, ASHIFTRT, int_mode,
5968 simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
5969 GET_MODE_PRECISION (int_mode) - 1 - i),
5970 GET_MODE_PRECISION (int_mode) - 1 - i);
5972 /* If all we did was surround TEMP with the two shifts, we
5973 haven't improved anything, so don't use it. Otherwise,
5974 we are better off with TEMP1. */
5975 if (GET_CODE (temp1) != ASHIFTRT
5976 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5977 || XEXP (XEXP (temp1, 0), 0) != temp)
5978 return temp1;
5980 break;
5982 case TRUNCATE:
5983 /* We can't handle truncation to a partial integer mode here
5984 because we don't know the real bitsize of the partial
5985 integer mode. */
5986 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5987 break;
5989 if (HWI_COMPUTABLE_MODE_P (mode))
5990 SUBST (XEXP (x, 0),
5991 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5992 GET_MODE_MASK (mode), false));
5994 /* We can truncate a constant value and return it. */
5996 poly_int64 c;
5997 if (poly_int_rtx_p (XEXP (x, 0), &c))
5998 return gen_int_mode (c, mode);
6001 /* Similarly to what we do in simplify-rtx.cc, a truncate of a register
6002 whose value is a comparison can be replaced with a subreg if
6003 STORE_FLAG_VALUE permits. */
6004 if (HWI_COMPUTABLE_MODE_P (mode)
6005 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
6006 && (temp = get_last_value (XEXP (x, 0)))
6007 && COMPARISON_P (temp)
6008 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (XEXP (x, 0))))
6009 return gen_lowpart (mode, XEXP (x, 0));
6010 break;
6012 case CONST:
6013 /* (const (const X)) can become (const X). Do it this way rather than
6014 returning the inner CONST since CONST can be shared with a
6015 REG_EQUAL note. */
6016 if (GET_CODE (XEXP (x, 0)) == CONST)
6017 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
6018 break;
6020 case LO_SUM:
6021 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
6022 can add in an offset. find_split_point will split this address up
6023 again if it doesn't match. */
6024 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
6025 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
6026 return XEXP (x, 1);
6027 break;
6029 case PLUS:
6030 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
6031 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
6032 bit-field and can be replaced by either a sign_extend or a
6033 sign_extract. The `and' may be a zero_extend and the two
6034 <c>, -<c> constants may be reversed. */
6035 if (GET_CODE (XEXP (x, 0)) == XOR
6036 && is_a <scalar_int_mode> (mode, &int_mode)
6037 && CONST_INT_P (XEXP (x, 1))
6038 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
6039 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
6040 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
6041 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
6042 && HWI_COMPUTABLE_MODE_P (int_mode)
6043 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
6044 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
6045 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
6046 == (HOST_WIDE_INT_1U << (i + 1)) - 1))
6047 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
6048 && known_eq ((GET_MODE_PRECISION
6049 (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))),
6050 (unsigned int) i + 1))))
6051 return simplify_shift_const
6052 (NULL_RTX, ASHIFTRT, int_mode,
6053 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6054 XEXP (XEXP (XEXP (x, 0), 0), 0),
6055 GET_MODE_PRECISION (int_mode) - (i + 1)),
6056 GET_MODE_PRECISION (int_mode) - (i + 1));
6058 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
6059 can become (ashiftrt (ashift (xor x 1) C) C) where C is
6060 the bitsize of the mode - 1. This allows simplification of
6061 "a = (b & 8) == 0;" */
6062 if (XEXP (x, 1) == constm1_rtx
6063 && !REG_P (XEXP (x, 0))
6064 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
6065 && REG_P (SUBREG_REG (XEXP (x, 0))))
6066 && is_a <scalar_int_mode> (mode, &int_mode)
6067 && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6068 return simplify_shift_const
6069 (NULL_RTX, ASHIFTRT, int_mode,
6070 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6071 gen_rtx_XOR (int_mode, XEXP (x, 0),
6072 const1_rtx),
6073 GET_MODE_PRECISION (int_mode) - 1),
6074 GET_MODE_PRECISION (int_mode) - 1);
6076 /* If we are adding two things that have no bits in common, convert
6077 the addition into an IOR. This will often be further simplified,
6078 for example in cases like ((a & 1) + (a & 2)), which can
6079 become a & 3. */
6081 if (HWI_COMPUTABLE_MODE_P (mode)
6082 && (nonzero_bits (XEXP (x, 0), mode)
6083 & nonzero_bits (XEXP (x, 1), mode)) == 0)
6085 /* Try to simplify the expression further. */
6086 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6087 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, false);
6089 /* If we could, great. If not, do not go ahead with the IOR
6090 replacement, since PLUS appears in many special purpose
6091 address arithmetic instructions. */
6092 if (GET_CODE (temp) != CLOBBER
6093 && (GET_CODE (temp) != IOR
6094 || ((XEXP (temp, 0) != XEXP (x, 0)
6095 || XEXP (temp, 1) != XEXP (x, 1))
6096 && (XEXP (temp, 0) != XEXP (x, 1)
6097 || XEXP (temp, 1) != XEXP (x, 0)))))
6098 return temp;
6101 /* Canonicalize x + x into x << 1. */
6102 if (GET_MODE_CLASS (mode) == MODE_INT
6103 && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6104 && !side_effects_p (XEXP (x, 0)))
6105 return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6107 break;
6109 case MINUS:
6110 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6111 (and <foo> (const_int pow2-1)) */
6112 if (is_a <scalar_int_mode> (mode, &int_mode)
6113 && GET_CODE (XEXP (x, 1)) == AND
6114 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6115 && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6116 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6117 return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6118 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6119 break;
6121 case MULT:
6122 /* If we have (mult (plus A B) C), apply the distributive law and then
6123 the inverse distributive law to see if things simplify. This
6124 occurs mostly in addresses, often when unrolling loops. */
6126 if (GET_CODE (XEXP (x, 0)) == PLUS)
6128 rtx result = distribute_and_simplify_rtx (x, 0);
6129 if (result)
6130 return result;
6133 /* Try simplify a*(b/c) as (a*b)/c. */
6134 if (FLOAT_MODE_P (mode) && flag_associative_math
6135 && GET_CODE (XEXP (x, 0)) == DIV)
6137 rtx tem = simplify_binary_operation (MULT, mode,
6138 XEXP (XEXP (x, 0), 0),
6139 XEXP (x, 1));
6140 if (tem)
6141 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6143 break;
6145 case UDIV:
6146 /* If this is a divide by a power of two, treat it as a shift if
6147 its first operand is a shift. */
6148 if (is_a <scalar_int_mode> (mode, &int_mode)
6149 && CONST_INT_P (XEXP (x, 1))
6150 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6151 && (GET_CODE (XEXP (x, 0)) == ASHIFT
6152 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6153 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6154 || GET_CODE (XEXP (x, 0)) == ROTATE
6155 || GET_CODE (XEXP (x, 0)) == ROTATERT))
6156 return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6157 XEXP (x, 0), i);
6158 break;
6160 case EQ: case NE:
6161 case GT: case GTU: case GE: case GEU:
6162 case LT: case LTU: case LE: case LEU:
6163 case UNEQ: case LTGT:
6164 case UNGT: case UNGE:
6165 case UNLT: case UNLE:
6166 case UNORDERED: case ORDERED:
6167 /* If the first operand is a condition code, we can't do anything
6168 with it. */
6169 if (GET_CODE (XEXP (x, 0)) == COMPARE
6170 || GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC)
6172 rtx op0 = XEXP (x, 0);
6173 rtx op1 = XEXP (x, 1);
6174 enum rtx_code new_code;
6176 if (GET_CODE (op0) == COMPARE)
6177 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6179 /* Simplify our comparison, if possible. */
6180 new_code = simplify_comparison (code, &op0, &op1);
6182 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6183 if only the low-order bit is possibly nonzero in X (such as when
6184 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6185 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6186 known to be either 0 or -1, NE becomes a NEG and EQ becomes
6187 (plus X 1).
6189 Remove any ZERO_EXTRACT we made when thinking this was a
6190 comparison. It may now be simpler to use, e.g., an AND. If a
6191 ZERO_EXTRACT is indeed appropriate, it will be placed back by
6192 the call to make_compound_operation in the SET case.
6194 Don't apply these optimizations if the caller would
6195 prefer a comparison rather than a value.
6196 E.g., for the condition in an IF_THEN_ELSE most targets need
6197 an explicit comparison. */
6199 if (in_cond)
6202 else if (STORE_FLAG_VALUE == 1
6203 && new_code == NE
6204 && is_int_mode (mode, &int_mode)
6205 && op1 == const0_rtx
6206 && int_mode == GET_MODE (op0)
6207 && nonzero_bits (op0, int_mode) == 1)
6208 return gen_lowpart (int_mode,
6209 expand_compound_operation (op0));
6211 else if (STORE_FLAG_VALUE == 1
6212 && new_code == NE
6213 && is_int_mode (mode, &int_mode)
6214 && op1 == const0_rtx
6215 && int_mode == GET_MODE (op0)
6216 && (num_sign_bit_copies (op0, int_mode)
6217 == GET_MODE_PRECISION (int_mode)))
6219 op0 = expand_compound_operation (op0);
6220 return simplify_gen_unary (NEG, int_mode,
6221 gen_lowpart (int_mode, op0),
6222 int_mode);
6225 else if (STORE_FLAG_VALUE == 1
6226 && new_code == EQ
6227 && is_int_mode (mode, &int_mode)
6228 && op1 == const0_rtx
6229 && int_mode == GET_MODE (op0)
6230 && nonzero_bits (op0, int_mode) == 1)
6232 op0 = expand_compound_operation (op0);
6233 return simplify_gen_binary (XOR, int_mode,
6234 gen_lowpart (int_mode, op0),
6235 const1_rtx);
6238 else if (STORE_FLAG_VALUE == 1
6239 && new_code == EQ
6240 && is_int_mode (mode, &int_mode)
6241 && op1 == const0_rtx
6242 && int_mode == GET_MODE (op0)
6243 && (num_sign_bit_copies (op0, int_mode)
6244 == GET_MODE_PRECISION (int_mode)))
6246 op0 = expand_compound_operation (op0);
6247 return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6250 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6251 those above. */
6252 if (in_cond)
6255 else if (STORE_FLAG_VALUE == -1
6256 && new_code == NE
6257 && is_int_mode (mode, &int_mode)
6258 && op1 == const0_rtx
6259 && int_mode == GET_MODE (op0)
6260 && (num_sign_bit_copies (op0, int_mode)
6261 == GET_MODE_PRECISION (int_mode)))
6262 return gen_lowpart (int_mode, expand_compound_operation (op0));
6264 else if (STORE_FLAG_VALUE == -1
6265 && new_code == NE
6266 && is_int_mode (mode, &int_mode)
6267 && op1 == const0_rtx
6268 && int_mode == GET_MODE (op0)
6269 && nonzero_bits (op0, int_mode) == 1)
6271 op0 = expand_compound_operation (op0);
6272 return simplify_gen_unary (NEG, int_mode,
6273 gen_lowpart (int_mode, op0),
6274 int_mode);
6277 else if (STORE_FLAG_VALUE == -1
6278 && new_code == EQ
6279 && is_int_mode (mode, &int_mode)
6280 && op1 == const0_rtx
6281 && int_mode == GET_MODE (op0)
6282 && (num_sign_bit_copies (op0, int_mode)
6283 == GET_MODE_PRECISION (int_mode)))
6285 op0 = expand_compound_operation (op0);
6286 return simplify_gen_unary (NOT, int_mode,
6287 gen_lowpart (int_mode, op0),
6288 int_mode);
6291 /* If X is 0/1, (eq X 0) is X-1. */
6292 else if (STORE_FLAG_VALUE == -1
6293 && new_code == EQ
6294 && is_int_mode (mode, &int_mode)
6295 && op1 == const0_rtx
6296 && int_mode == GET_MODE (op0)
6297 && nonzero_bits (op0, int_mode) == 1)
6299 op0 = expand_compound_operation (op0);
6300 return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6303 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6304 one bit that might be nonzero, we can convert (ne x 0) to
6305 (ashift x c) where C puts the bit in the sign bit. Remove any
6306 AND with STORE_FLAG_VALUE when we are done, since we are only
6307 going to test the sign bit. */
6308 if (new_code == NE
6309 && is_int_mode (mode, &int_mode)
6310 && HWI_COMPUTABLE_MODE_P (int_mode)
6311 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6312 && op1 == const0_rtx
6313 && int_mode == GET_MODE (op0)
6314 && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6316 x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6317 expand_compound_operation (op0),
6318 GET_MODE_PRECISION (int_mode) - 1 - i);
6319 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6320 return XEXP (x, 0);
6321 else
6322 return x;
6325 /* If the code changed, return a whole new comparison.
6326 We also need to avoid using SUBST in cases where
6327 simplify_comparison has widened a comparison with a CONST_INT,
6328 since in that case the wider CONST_INT may fail the sanity
6329 checks in do_SUBST. */
6330 if (new_code != code
6331 || (CONST_INT_P (op1)
6332 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6333 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6334 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6336 /* Otherwise, keep this operation, but maybe change its operands.
6337 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6338 SUBST (XEXP (x, 0), op0);
6339 SUBST (XEXP (x, 1), op1);
6341 break;
6343 case IF_THEN_ELSE:
6344 return simplify_if_then_else (x);
6346 case ZERO_EXTRACT:
6347 case SIGN_EXTRACT:
6348 case ZERO_EXTEND:
6349 case SIGN_EXTEND:
6350 /* If we are processing SET_DEST, we are done. */
6351 if (in_dest)
6352 return x;
6354 return expand_compound_operation (x);
6356 case SET:
6357 return simplify_set (x);
6359 case AND:
6360 case IOR:
6361 return simplify_logical (x);
6363 case ASHIFT:
6364 case LSHIFTRT:
6365 case ASHIFTRT:
6366 case ROTATE:
6367 case ROTATERT:
6368 /* If this is a shift by a constant amount, simplify it. */
6369 if (CONST_INT_P (XEXP (x, 1)))
6370 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6371 INTVAL (XEXP (x, 1)));
6373 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6374 SUBST (XEXP (x, 1),
6375 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6376 (HOST_WIDE_INT_1U
6377 << exact_log2 (GET_MODE_UNIT_BITSIZE
6378 (GET_MODE (x)))) - 1, false));
6379 break;
6380 case VEC_SELECT:
6382 rtx trueop0 = XEXP (x, 0);
6383 mode = GET_MODE (trueop0);
6384 rtx trueop1 = XEXP (x, 1);
6385 /* If we select a low-part subreg, return that. */
6386 if (vec_series_lowpart_p (GET_MODE (x), mode, trueop1))
6388 rtx new_rtx = lowpart_subreg (GET_MODE (x), trueop0, mode);
6389 if (new_rtx != NULL_RTX)
6390 return new_rtx;
6394 default:
6395 break;
6398 return x;
6401 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6403 static rtx
6404 simplify_if_then_else (rtx x)
6406 machine_mode mode = GET_MODE (x);
6407 rtx cond = XEXP (x, 0);
6408 rtx true_rtx = XEXP (x, 1);
6409 rtx false_rtx = XEXP (x, 2);
6410 enum rtx_code true_code = GET_CODE (cond);
6411 bool comparison_p = COMPARISON_P (cond);
6412 rtx temp;
6413 int i;
6414 enum rtx_code false_code;
6415 rtx reversed;
6416 scalar_int_mode int_mode, inner_mode;
6418 /* Simplify storing of the truth value. */
6419 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6420 return simplify_gen_relational (true_code, mode, VOIDmode,
6421 XEXP (cond, 0), XEXP (cond, 1));
6423 /* Also when the truth value has to be reversed. */
6424 if (comparison_p
6425 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6426 && (reversed = reversed_comparison (cond, mode)))
6427 return reversed;
6429 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6430 in it is being compared against certain values. Get the true and false
6431 comparisons and see if that says anything about the value of each arm. */
6433 if (comparison_p
6434 && ((false_code = reversed_comparison_code (cond, NULL))
6435 != UNKNOWN)
6436 && REG_P (XEXP (cond, 0)))
6438 HOST_WIDE_INT nzb;
6439 rtx from = XEXP (cond, 0);
6440 rtx true_val = XEXP (cond, 1);
6441 rtx false_val = true_val;
6442 bool swapped = false;
6444 /* If FALSE_CODE is EQ, swap the codes and arms. */
6446 if (false_code == EQ)
6448 swapped = true, true_code = EQ, false_code = NE;
6449 std::swap (true_rtx, false_rtx);
6452 scalar_int_mode from_mode;
6453 if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6455 /* If we are comparing against zero and the expression being
6456 tested has only a single bit that might be nonzero, that is
6457 its value when it is not equal to zero. Similarly if it is
6458 known to be -1 or 0. */
6459 if (true_code == EQ
6460 && true_val == const0_rtx
6461 && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6463 false_code = EQ;
6464 false_val = gen_int_mode (nzb, from_mode);
6466 else if (true_code == EQ
6467 && true_val == const0_rtx
6468 && (num_sign_bit_copies (from, from_mode)
6469 == GET_MODE_PRECISION (from_mode)))
6471 false_code = EQ;
6472 false_val = constm1_rtx;
6476 /* Now simplify an arm if we know the value of the register in the
6477 branch and it is used in the arm. Be careful due to the potential
6478 of locally-shared RTL. */
6480 if (reg_mentioned_p (from, true_rtx))
6481 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6482 from, true_val),
6483 pc_rtx, pc_rtx, false, false, false);
6484 if (reg_mentioned_p (from, false_rtx))
6485 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6486 from, false_val),
6487 pc_rtx, pc_rtx, false, false, false);
6489 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6490 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6492 true_rtx = XEXP (x, 1);
6493 false_rtx = XEXP (x, 2);
6494 true_code = GET_CODE (cond);
6497 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6498 reversed, do so to avoid needing two sets of patterns for
6499 subtract-and-branch insns. Similarly if we have a constant in the true
6500 arm, the false arm is the same as the first operand of the comparison, or
6501 the false arm is more complicated than the true arm. */
6503 if (comparison_p
6504 && reversed_comparison_code (cond, NULL) != UNKNOWN
6505 && (true_rtx == pc_rtx
6506 || (CONSTANT_P (true_rtx)
6507 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6508 || true_rtx == const0_rtx
6509 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6510 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6511 && !OBJECT_P (false_rtx))
6512 || reg_mentioned_p (true_rtx, false_rtx)
6513 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6515 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6516 SUBST (XEXP (x, 1), false_rtx);
6517 SUBST (XEXP (x, 2), true_rtx);
6519 std::swap (true_rtx, false_rtx);
6520 cond = XEXP (x, 0);
6522 /* It is possible that the conditional has been simplified out. */
6523 true_code = GET_CODE (cond);
6524 comparison_p = COMPARISON_P (cond);
6527 /* If the two arms are identical, we don't need the comparison. */
6529 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6530 return true_rtx;
6532 /* Convert a == b ? b : a to "a". */
6533 if (true_code == EQ && ! side_effects_p (cond)
6534 && !HONOR_NANS (mode)
6535 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6536 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6537 return false_rtx;
6538 else if (true_code == NE && ! side_effects_p (cond)
6539 && !HONOR_NANS (mode)
6540 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6541 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6542 return true_rtx;
6544 /* Look for cases where we have (abs x) or (neg (abs X)). */
6546 if (GET_MODE_CLASS (mode) == MODE_INT
6547 && comparison_p
6548 && XEXP (cond, 1) == const0_rtx
6549 && GET_CODE (false_rtx) == NEG
6550 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6551 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6552 && ! side_effects_p (true_rtx))
6553 switch (true_code)
6555 case GT:
6556 case GE:
6557 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6558 case LT:
6559 case LE:
6560 return
6561 simplify_gen_unary (NEG, mode,
6562 simplify_gen_unary (ABS, mode, true_rtx, mode),
6563 mode);
6564 default:
6565 break;
6568 /* Look for MIN or MAX. */
6570 if ((! FLOAT_MODE_P (mode)
6571 || (flag_unsafe_math_optimizations
6572 && !HONOR_NANS (mode)
6573 && !HONOR_SIGNED_ZEROS (mode)))
6574 && comparison_p
6575 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6576 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6577 && ! side_effects_p (cond))
6578 switch (true_code)
6580 case GE:
6581 case GT:
6582 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6583 case LE:
6584 case LT:
6585 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6586 case GEU:
6587 case GTU:
6588 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6589 case LEU:
6590 case LTU:
6591 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6592 default:
6593 break;
6596 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6597 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6598 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6599 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6600 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6601 neither 1 or -1, but it isn't worth checking for. */
6603 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6604 && comparison_p
6605 && is_int_mode (mode, &int_mode)
6606 && ! side_effects_p (x))
6608 rtx t = make_compound_operation (true_rtx, SET);
6609 rtx f = make_compound_operation (false_rtx, SET);
6610 rtx cond_op0 = XEXP (cond, 0);
6611 rtx cond_op1 = XEXP (cond, 1);
6612 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6613 scalar_int_mode m = int_mode;
6614 rtx z = 0, c1 = NULL_RTX;
6616 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6617 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6618 || GET_CODE (t) == ASHIFT
6619 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6620 && rtx_equal_p (XEXP (t, 0), f))
6621 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6623 /* If an identity-zero op is commutative, check whether there
6624 would be a match if we swapped the operands. */
6625 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6626 || GET_CODE (t) == XOR)
6627 && rtx_equal_p (XEXP (t, 1), f))
6628 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6629 else if (GET_CODE (t) == SIGN_EXTEND
6630 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6631 && (GET_CODE (XEXP (t, 0)) == PLUS
6632 || GET_CODE (XEXP (t, 0)) == MINUS
6633 || GET_CODE (XEXP (t, 0)) == IOR
6634 || GET_CODE (XEXP (t, 0)) == XOR
6635 || GET_CODE (XEXP (t, 0)) == ASHIFT
6636 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6637 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6638 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6639 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6640 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6641 && (num_sign_bit_copies (f, GET_MODE (f))
6642 > (unsigned int)
6643 (GET_MODE_PRECISION (int_mode)
6644 - GET_MODE_PRECISION (inner_mode))))
6646 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6647 extend_op = SIGN_EXTEND;
6648 m = inner_mode;
6650 else if (GET_CODE (t) == SIGN_EXTEND
6651 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6652 && (GET_CODE (XEXP (t, 0)) == PLUS
6653 || GET_CODE (XEXP (t, 0)) == IOR
6654 || GET_CODE (XEXP (t, 0)) == XOR)
6655 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6656 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6657 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6658 && (num_sign_bit_copies (f, GET_MODE (f))
6659 > (unsigned int)
6660 (GET_MODE_PRECISION (int_mode)
6661 - GET_MODE_PRECISION (inner_mode))))
6663 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6664 extend_op = SIGN_EXTEND;
6665 m = inner_mode;
6667 else if (GET_CODE (t) == ZERO_EXTEND
6668 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6669 && (GET_CODE (XEXP (t, 0)) == PLUS
6670 || GET_CODE (XEXP (t, 0)) == MINUS
6671 || GET_CODE (XEXP (t, 0)) == IOR
6672 || GET_CODE (XEXP (t, 0)) == XOR
6673 || GET_CODE (XEXP (t, 0)) == ASHIFT
6674 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6675 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6676 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6677 && HWI_COMPUTABLE_MODE_P (int_mode)
6678 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6679 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6680 && ((nonzero_bits (f, GET_MODE (f))
6681 & ~GET_MODE_MASK (inner_mode))
6682 == 0))
6684 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6685 extend_op = ZERO_EXTEND;
6686 m = inner_mode;
6688 else if (GET_CODE (t) == ZERO_EXTEND
6689 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6690 && (GET_CODE (XEXP (t, 0)) == PLUS
6691 || GET_CODE (XEXP (t, 0)) == IOR
6692 || GET_CODE (XEXP (t, 0)) == XOR)
6693 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6694 && HWI_COMPUTABLE_MODE_P (int_mode)
6695 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6696 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6697 && ((nonzero_bits (f, GET_MODE (f))
6698 & ~GET_MODE_MASK (inner_mode))
6699 == 0))
6701 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6702 extend_op = ZERO_EXTEND;
6703 m = inner_mode;
6706 if (z)
6708 machine_mode cm = m;
6709 if ((op == ASHIFT || op == LSHIFTRT || op == ASHIFTRT)
6710 && GET_MODE (c1) != VOIDmode)
6711 cm = GET_MODE (c1);
6712 temp = subst (simplify_gen_relational (true_code, cm, VOIDmode,
6713 cond_op0, cond_op1),
6714 pc_rtx, pc_rtx, false, false, false);
6715 temp = simplify_gen_binary (MULT, cm, temp,
6716 simplify_gen_binary (MULT, cm, c1,
6717 const_true_rtx));
6718 temp = subst (temp, pc_rtx, pc_rtx, false, false, false);
6719 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6721 if (extend_op != UNKNOWN)
6722 temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6724 return temp;
6728 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6729 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6730 negation of a single bit, we can convert this operation to a shift. We
6731 can actually do this more generally, but it doesn't seem worth it. */
6733 if (true_code == NE
6734 && is_a <scalar_int_mode> (mode, &int_mode)
6735 && XEXP (cond, 1) == const0_rtx
6736 && false_rtx == const0_rtx
6737 && CONST_INT_P (true_rtx)
6738 && ((nonzero_bits (XEXP (cond, 0), int_mode) == 1
6739 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6740 || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6741 == GET_MODE_PRECISION (int_mode))
6742 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6743 return
6744 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6745 gen_lowpart (int_mode, XEXP (cond, 0)), i);
6747 /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6748 non-zero bit in A is C1. */
6749 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6750 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6751 && is_a <scalar_int_mode> (mode, &int_mode)
6752 && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6753 && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6754 == nonzero_bits (XEXP (cond, 0), inner_mode)
6755 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6757 rtx val = XEXP (cond, 0);
6758 if (inner_mode == int_mode)
6759 return val;
6760 else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6761 return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6764 return x;
6767 /* Simplify X, a SET expression. Return the new expression. */
6769 static rtx
6770 simplify_set (rtx x)
6772 rtx src = SET_SRC (x);
6773 rtx dest = SET_DEST (x);
6774 machine_mode mode
6775 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6776 rtx_insn *other_insn;
6777 rtx *cc_use;
6778 scalar_int_mode int_mode;
6780 /* (set (pc) (return)) gets written as (return). */
6781 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6782 return src;
6784 /* Now that we know for sure which bits of SRC we are using, see if we can
6785 simplify the expression for the object knowing that we only need the
6786 low-order bits. */
6788 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6790 src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, false);
6791 SUBST (SET_SRC (x), src);
6794 /* If the source is a COMPARE, look for the use of the comparison result
6795 and try to simplify it unless we already have used undobuf.other_insn. */
6796 if ((GET_MODE_CLASS (mode) == MODE_CC || GET_CODE (src) == COMPARE)
6797 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6798 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6799 && COMPARISON_P (*cc_use)
6800 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6802 enum rtx_code old_code = GET_CODE (*cc_use);
6803 enum rtx_code new_code;
6804 rtx op0, op1, tmp;
6805 bool other_changed = false;
6806 rtx inner_compare = NULL_RTX;
6807 machine_mode compare_mode = GET_MODE (dest);
6809 if (GET_CODE (src) == COMPARE)
6811 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6812 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6814 inner_compare = op0;
6815 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6818 else
6819 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6821 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6822 op0, op1);
6823 if (!tmp)
6824 new_code = old_code;
6825 else if (!CONSTANT_P (tmp))
6827 new_code = GET_CODE (tmp);
6828 op0 = XEXP (tmp, 0);
6829 op1 = XEXP (tmp, 1);
6831 else
6833 rtx pat = PATTERN (other_insn);
6834 undobuf.other_insn = other_insn;
6835 SUBST (*cc_use, tmp);
6837 /* Attempt to simplify CC user. */
6838 if (GET_CODE (pat) == SET)
6840 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6841 if (new_rtx != NULL_RTX)
6842 SUBST (SET_SRC (pat), new_rtx);
6845 /* Convert X into a no-op move. */
6846 SUBST (SET_DEST (x), pc_rtx);
6847 SUBST (SET_SRC (x), pc_rtx);
6848 return x;
6851 /* Simplify our comparison, if possible. */
6852 new_code = simplify_comparison (new_code, &op0, &op1);
6854 #ifdef SELECT_CC_MODE
6855 /* If this machine has CC modes other than CCmode, check to see if we
6856 need to use a different CC mode here. */
6857 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6858 compare_mode = GET_MODE (op0);
6859 else if (inner_compare
6860 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6861 && new_code == old_code
6862 && op0 == XEXP (inner_compare, 0)
6863 && op1 == XEXP (inner_compare, 1))
6864 compare_mode = GET_MODE (inner_compare);
6865 else
6866 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6868 /* If the mode changed, we have to change SET_DEST, the mode in the
6869 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6870 a hard register, just build new versions with the proper mode. If it
6871 is a pseudo, we lose unless it is only time we set the pseudo, in
6872 which case we can safely change its mode. */
6873 if (compare_mode != GET_MODE (dest))
6875 if (can_change_dest_mode (dest, 0, compare_mode))
6877 unsigned int regno = REGNO (dest);
6878 rtx new_dest;
6880 if (regno < FIRST_PSEUDO_REGISTER)
6881 new_dest = gen_rtx_REG (compare_mode, regno);
6882 else
6884 subst_mode (regno, compare_mode);
6885 new_dest = regno_reg_rtx[regno];
6888 SUBST (SET_DEST (x), new_dest);
6889 SUBST (XEXP (*cc_use, 0), new_dest);
6890 other_changed = true;
6892 dest = new_dest;
6895 #endif /* SELECT_CC_MODE */
6897 /* If the code changed, we have to build a new comparison in
6898 undobuf.other_insn. */
6899 if (new_code != old_code)
6901 bool other_changed_previously = other_changed;
6902 unsigned HOST_WIDE_INT mask;
6903 rtx old_cc_use = *cc_use;
6905 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6906 dest, const0_rtx));
6907 other_changed = true;
6909 /* If the only change we made was to change an EQ into an NE or
6910 vice versa, OP0 has only one bit that might be nonzero, and OP1
6911 is zero, check if changing the user of the condition code will
6912 produce a valid insn. If it won't, we can keep the original code
6913 in that insn by surrounding our operation with an XOR. */
6915 if (((old_code == NE && new_code == EQ)
6916 || (old_code == EQ && new_code == NE))
6917 && ! other_changed_previously && op1 == const0_rtx
6918 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6919 && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6921 rtx pat = PATTERN (other_insn), note = 0;
6923 if ((recog_for_combine (&pat, other_insn, &note) < 0
6924 && ! check_asm_operands (pat)))
6926 *cc_use = old_cc_use;
6927 other_changed = false;
6929 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6930 gen_int_mode (mask,
6931 GET_MODE (op0)));
6936 if (other_changed)
6937 undobuf.other_insn = other_insn;
6939 /* Don't generate a compare of a CC with 0, just use that CC. */
6940 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6942 SUBST (SET_SRC (x), op0);
6943 src = SET_SRC (x);
6945 /* Otherwise, if we didn't previously have the same COMPARE we
6946 want, create it from scratch. */
6947 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6948 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6950 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6951 src = SET_SRC (x);
6954 else
6956 /* Get SET_SRC in a form where we have placed back any
6957 compound expressions. Then do the checks below. */
6958 src = make_compound_operation (src, SET);
6959 SUBST (SET_SRC (x), src);
6962 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6963 and X being a REG or (subreg (reg)), we may be able to convert this to
6964 (set (subreg:m2 x) (op)).
6966 We can always do this if M1 is narrower than M2 because that means that
6967 we only care about the low bits of the result.
6969 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6970 perform a narrower operation than requested since the high-order bits will
6971 be undefined. On machine where it is defined, this transformation is safe
6972 as long as M1 and M2 have the same number of words. */
6974 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6975 && !OBJECT_P (SUBREG_REG (src))
6976 && (known_equal_after_align_up
6977 (GET_MODE_SIZE (GET_MODE (src)),
6978 GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))),
6979 UNITS_PER_WORD))
6980 && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
6981 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6982 && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
6983 GET_MODE (SUBREG_REG (src)),
6984 GET_MODE (src)))
6985 && (REG_P (dest)
6986 || (GET_CODE (dest) == SUBREG
6987 && REG_P (SUBREG_REG (dest)))))
6989 SUBST (SET_DEST (x),
6990 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6991 dest));
6992 SUBST (SET_SRC (x), SUBREG_REG (src));
6994 src = SET_SRC (x), dest = SET_DEST (x);
6997 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6998 would require a paradoxical subreg. Replace the subreg with a
6999 zero_extend to avoid the reload that would otherwise be required.
7000 Don't do this unless we have a scalar integer mode, otherwise the
7001 transformation is incorrect. */
7003 enum rtx_code extend_op;
7004 if (paradoxical_subreg_p (src)
7005 && MEM_P (SUBREG_REG (src))
7006 && SCALAR_INT_MODE_P (GET_MODE (src))
7007 && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
7009 SUBST (SET_SRC (x),
7010 gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
7012 src = SET_SRC (x);
7015 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
7016 are comparing an item known to be 0 or -1 against 0, use a logical
7017 operation instead. Check for one of the arms being an IOR of the other
7018 arm with some value. We compute three terms to be IOR'ed together. In
7019 practice, at most two will be nonzero. Then we do the IOR's. */
7021 if (GET_CODE (dest) != PC
7022 && GET_CODE (src) == IF_THEN_ELSE
7023 && is_int_mode (GET_MODE (src), &int_mode)
7024 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
7025 && XEXP (XEXP (src, 0), 1) == const0_rtx
7026 && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
7027 && (!HAVE_conditional_move
7028 || ! can_conditionally_move_p (int_mode))
7029 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
7030 == GET_MODE_PRECISION (int_mode))
7031 && ! side_effects_p (src))
7033 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
7034 ? XEXP (src, 1) : XEXP (src, 2));
7035 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
7036 ? XEXP (src, 2) : XEXP (src, 1));
7037 rtx term1 = const0_rtx, term2, term3;
7039 if (GET_CODE (true_rtx) == IOR
7040 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
7041 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
7042 else if (GET_CODE (true_rtx) == IOR
7043 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
7044 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
7045 else if (GET_CODE (false_rtx) == IOR
7046 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
7047 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
7048 else if (GET_CODE (false_rtx) == IOR
7049 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
7050 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
7052 term2 = simplify_gen_binary (AND, int_mode,
7053 XEXP (XEXP (src, 0), 0), true_rtx);
7054 term3 = simplify_gen_binary (AND, int_mode,
7055 simplify_gen_unary (NOT, int_mode,
7056 XEXP (XEXP (src, 0), 0),
7057 int_mode),
7058 false_rtx);
7060 SUBST (SET_SRC (x),
7061 simplify_gen_binary (IOR, int_mode,
7062 simplify_gen_binary (IOR, int_mode,
7063 term1, term2),
7064 term3));
7066 src = SET_SRC (x);
7069 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7070 whole thing fail. */
7071 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7072 return src;
7073 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7074 return dest;
7075 else
7076 /* Convert this into a field assignment operation, if possible. */
7077 return make_field_assignment (x);
7080 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7081 result. */
7083 static rtx
7084 simplify_logical (rtx x)
7086 rtx op0 = XEXP (x, 0);
7087 rtx op1 = XEXP (x, 1);
7088 scalar_int_mode mode;
7090 switch (GET_CODE (x))
7092 case AND:
7093 /* We can call simplify_and_const_int only if we don't lose
7094 any (sign) bits when converting INTVAL (op1) to
7095 "unsigned HOST_WIDE_INT". */
7096 if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7097 && CONST_INT_P (op1)
7098 && (HWI_COMPUTABLE_MODE_P (mode)
7099 || INTVAL (op1) > 0))
7101 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7102 if (GET_CODE (x) != AND)
7103 return x;
7105 op0 = XEXP (x, 0);
7106 op1 = XEXP (x, 1);
7109 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7110 apply the distributive law and then the inverse distributive
7111 law to see if things simplify. */
7112 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7114 rtx result = distribute_and_simplify_rtx (x, 0);
7115 if (result)
7116 return result;
7118 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7120 rtx result = distribute_and_simplify_rtx (x, 1);
7121 if (result)
7122 return result;
7124 break;
7126 case IOR:
7127 /* If we have (ior (and A B) C), apply the distributive law and then
7128 the inverse distributive law to see if things simplify. */
7130 if (GET_CODE (op0) == AND)
7132 rtx result = distribute_and_simplify_rtx (x, 0);
7133 if (result)
7134 return result;
7137 if (GET_CODE (op1) == AND)
7139 rtx result = distribute_and_simplify_rtx (x, 1);
7140 if (result)
7141 return result;
7143 break;
7145 default:
7146 gcc_unreachable ();
7149 return x;
7152 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7153 operations" because they can be replaced with two more basic operations.
7154 ZERO_EXTEND is also considered "compound" because it can be replaced with
7155 an AND operation, which is simpler, though only one operation.
7157 The function expand_compound_operation is called with an rtx expression
7158 and will convert it to the appropriate shifts and AND operations,
7159 simplifying at each stage.
7161 The function make_compound_operation is called to convert an expression
7162 consisting of shifts and ANDs into the equivalent compound expression.
7163 It is the inverse of this function, loosely speaking. */
7165 static rtx
7166 expand_compound_operation (rtx x)
7168 unsigned HOST_WIDE_INT pos = 0, len;
7169 bool unsignedp = false;
7170 unsigned int modewidth;
7171 rtx tem;
7172 scalar_int_mode inner_mode;
7174 switch (GET_CODE (x))
7176 case ZERO_EXTEND:
7177 unsignedp = true;
7178 /* FALLTHRU */
7179 case SIGN_EXTEND:
7180 /* We can't necessarily use a const_int for a multiword mode;
7181 it depends on implicitly extending the value.
7182 Since we don't know the right way to extend it,
7183 we can't tell whether the implicit way is right.
7185 Even for a mode that is no wider than a const_int,
7186 we can't win, because we need to sign extend one of its bits through
7187 the rest of it, and we don't know which bit. */
7188 if (CONST_INT_P (XEXP (x, 0)))
7189 return x;
7191 /* Reject modes that aren't scalar integers because turning vector
7192 or complex modes into shifts causes problems. */
7193 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7194 return x;
7196 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7197 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7198 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7199 reloaded. If not for that, MEM's would very rarely be safe.
7201 Reject modes bigger than a word, because we might not be able
7202 to reference a two-register group starting with an arbitrary register
7203 (and currently gen_lowpart might crash for a SUBREG). */
7205 if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7206 return x;
7208 len = GET_MODE_PRECISION (inner_mode);
7209 /* If the inner object has VOIDmode (the only way this can happen
7210 is if it is an ASM_OPERANDS), we can't do anything since we don't
7211 know how much masking to do. */
7212 if (len == 0)
7213 return x;
7215 break;
7217 case ZERO_EXTRACT:
7218 unsignedp = true;
7220 /* fall through */
7222 case SIGN_EXTRACT:
7223 /* If the operand is a CLOBBER, just return it. */
7224 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7225 return XEXP (x, 0);
7227 if (!CONST_INT_P (XEXP (x, 1))
7228 || !CONST_INT_P (XEXP (x, 2)))
7229 return x;
7231 /* Reject modes that aren't scalar integers because turning vector
7232 or complex modes into shifts causes problems. */
7233 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7234 return x;
7236 len = INTVAL (XEXP (x, 1));
7237 pos = INTVAL (XEXP (x, 2));
7239 /* This should stay within the object being extracted, fail otherwise. */
7240 if (len + pos > GET_MODE_PRECISION (inner_mode))
7241 return x;
7243 if (BITS_BIG_ENDIAN)
7244 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7246 break;
7248 default:
7249 return x;
7252 /* We've rejected non-scalar operations by now. */
7253 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7255 /* Convert sign extension to zero extension, if we know that the high
7256 bit is not set, as this is easier to optimize. It will be converted
7257 back to cheaper alternative in make_extraction. */
7258 if (GET_CODE (x) == SIGN_EXTEND
7259 && HWI_COMPUTABLE_MODE_P (mode)
7260 && ((nonzero_bits (XEXP (x, 0), inner_mode)
7261 & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7262 == 0))
7264 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7265 rtx temp2 = expand_compound_operation (temp);
7267 /* Make sure this is a profitable operation. */
7268 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7269 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7270 return temp2;
7271 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7272 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7273 return temp;
7274 else
7275 return x;
7278 /* We can optimize some special cases of ZERO_EXTEND. */
7279 if (GET_CODE (x) == ZERO_EXTEND)
7281 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7282 know that the last value didn't have any inappropriate bits
7283 set. */
7284 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7285 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7286 && HWI_COMPUTABLE_MODE_P (mode)
7287 && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7288 & ~GET_MODE_MASK (inner_mode)) == 0)
7289 return XEXP (XEXP (x, 0), 0);
7291 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7292 if (GET_CODE (XEXP (x, 0)) == SUBREG
7293 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7294 && subreg_lowpart_p (XEXP (x, 0))
7295 && HWI_COMPUTABLE_MODE_P (mode)
7296 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7297 & ~GET_MODE_MASK (inner_mode)) == 0)
7298 return SUBREG_REG (XEXP (x, 0));
7300 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7301 is a comparison and STORE_FLAG_VALUE permits. This is like
7302 the first case, but it works even when MODE is larger
7303 than HOST_WIDE_INT. */
7304 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7305 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7306 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7307 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7308 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7309 return XEXP (XEXP (x, 0), 0);
7311 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7312 if (GET_CODE (XEXP (x, 0)) == SUBREG
7313 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7314 && subreg_lowpart_p (XEXP (x, 0))
7315 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7316 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7317 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7318 return SUBREG_REG (XEXP (x, 0));
7322 /* If we reach here, we want to return a pair of shifts. The inner
7323 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7324 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7325 logical depending on the value of UNSIGNEDP.
7327 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7328 converted into an AND of a shift.
7330 We must check for the case where the left shift would have a negative
7331 count. This can happen in a case like (x >> 31) & 255 on machines
7332 that can't shift by a constant. On those machines, we would first
7333 combine the shift with the AND to produce a variable-position
7334 extraction. Then the constant of 31 would be substituted in
7335 to produce such a position. */
7337 modewidth = GET_MODE_PRECISION (mode);
7338 if (modewidth >= pos + len)
7340 tem = gen_lowpart (mode, XEXP (x, 0));
7341 if (!tem || GET_CODE (tem) == CLOBBER)
7342 return x;
7343 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7344 tem, modewidth - pos - len);
7345 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7346 mode, tem, modewidth - len);
7348 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7350 tem = simplify_shift_const (NULL_RTX, LSHIFTRT, inner_mode,
7351 XEXP (x, 0), pos);
7352 tem = gen_lowpart (mode, tem);
7353 if (!tem || GET_CODE (tem) == CLOBBER)
7354 return x;
7355 tem = simplify_and_const_int (NULL_RTX, mode, tem,
7356 (HOST_WIDE_INT_1U << len) - 1);
7358 else
7359 /* Any other cases we can't handle. */
7360 return x;
7362 /* If we couldn't do this for some reason, return the original
7363 expression. */
7364 if (GET_CODE (tem) == CLOBBER)
7365 return x;
7367 return tem;
7370 /* X is a SET which contains an assignment of one object into
7371 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7372 or certain SUBREGS). If possible, convert it into a series of
7373 logical operations.
7375 We half-heartedly support variable positions, but do not at all
7376 support variable lengths. */
7378 static const_rtx
7379 expand_field_assignment (const_rtx x)
7381 rtx inner;
7382 rtx pos; /* Always counts from low bit. */
7383 int len, inner_len;
7384 rtx mask, cleared, masked;
7385 scalar_int_mode compute_mode;
7387 /* Loop until we find something we can't simplify. */
7388 while (1)
7390 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7391 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7393 rtx x0 = XEXP (SET_DEST (x), 0);
7394 if (!GET_MODE_PRECISION (GET_MODE (x0)).is_constant (&len))
7395 break;
7396 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7397 pos = gen_int_mode (subreg_lsb (XEXP (SET_DEST (x), 0)),
7398 MAX_MODE_INT);
7400 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7401 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7403 inner = XEXP (SET_DEST (x), 0);
7404 if (!GET_MODE_PRECISION (GET_MODE (inner)).is_constant (&inner_len))
7405 break;
7407 len = INTVAL (XEXP (SET_DEST (x), 1));
7408 pos = XEXP (SET_DEST (x), 2);
7410 /* A constant position should stay within the width of INNER. */
7411 if (CONST_INT_P (pos) && INTVAL (pos) + len > inner_len)
7412 break;
7414 if (BITS_BIG_ENDIAN)
7416 if (CONST_INT_P (pos))
7417 pos = GEN_INT (inner_len - len - INTVAL (pos));
7418 else if (GET_CODE (pos) == MINUS
7419 && CONST_INT_P (XEXP (pos, 1))
7420 && INTVAL (XEXP (pos, 1)) == inner_len - len)
7421 /* If position is ADJUST - X, new position is X. */
7422 pos = XEXP (pos, 0);
7423 else
7424 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7425 gen_int_mode (inner_len - len,
7426 GET_MODE (pos)),
7427 pos);
7431 /* If the destination is a subreg that overwrites the whole of the inner
7432 register, we can move the subreg to the source. */
7433 else if (GET_CODE (SET_DEST (x)) == SUBREG
7434 /* We need SUBREGs to compute nonzero_bits properly. */
7435 && nonzero_sign_valid
7436 && !read_modify_subreg_p (SET_DEST (x)))
7438 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7439 gen_lowpart
7440 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7441 SET_SRC (x)));
7442 continue;
7444 else
7445 break;
7447 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7448 inner = SUBREG_REG (inner);
7450 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7451 if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7453 /* Don't do anything for vector or complex integral types. */
7454 if (! FLOAT_MODE_P (GET_MODE (inner)))
7455 break;
7457 /* Try to find an integral mode to pun with. */
7458 if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7459 .exists (&compute_mode))
7460 break;
7462 inner = gen_lowpart (compute_mode, inner);
7465 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7466 if (len >= HOST_BITS_PER_WIDE_INT)
7467 break;
7469 /* Don't try to compute in too wide unsupported modes. */
7470 if (!targetm.scalar_mode_supported_p (compute_mode))
7471 break;
7473 /* Now compute the equivalent expression. Make a copy of INNER
7474 for the SET_DEST in case it is a MEM into which we will substitute;
7475 we don't want shared RTL in that case. */
7476 mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7477 compute_mode);
7478 cleared = simplify_gen_binary (AND, compute_mode,
7479 simplify_gen_unary (NOT, compute_mode,
7480 simplify_gen_binary (ASHIFT,
7481 compute_mode,
7482 mask, pos),
7483 compute_mode),
7484 inner);
7485 masked = simplify_gen_binary (ASHIFT, compute_mode,
7486 simplify_gen_binary (
7487 AND, compute_mode,
7488 gen_lowpart (compute_mode, SET_SRC (x)),
7489 mask),
7490 pos);
7492 x = gen_rtx_SET (copy_rtx (inner),
7493 simplify_gen_binary (IOR, compute_mode,
7494 cleared, masked));
7497 return x;
7500 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7501 it is an RTX that represents the (variable) starting position; otherwise,
7502 POS is the (constant) starting bit position. Both are counted from the LSB.
7504 UNSIGNEDP is true for an unsigned reference and zero for a signed one.
7506 IN_DEST is true if this is a reference in the destination of a SET.
7507 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7508 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7509 be used.
7511 IN_COMPARE is true if we are in a COMPARE. This means that a
7512 ZERO_EXTRACT should be built even for bits starting at bit 0.
7514 MODE is the desired mode of the result (if IN_DEST == 0).
7516 The result is an RTX for the extraction or NULL_RTX if the target
7517 can't handle it. */
7519 static rtx
7520 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7521 rtx pos_rtx, unsigned HOST_WIDE_INT len, bool unsignedp,
7522 bool in_dest, bool in_compare)
7524 /* This mode describes the size of the storage area
7525 to fetch the overall value from. Within that, we
7526 ignore the POS lowest bits, etc. */
7527 machine_mode is_mode = GET_MODE (inner);
7528 machine_mode inner_mode;
7529 scalar_int_mode wanted_inner_mode;
7530 scalar_int_mode wanted_inner_reg_mode = word_mode;
7531 scalar_int_mode pos_mode = word_mode;
7532 machine_mode extraction_mode = word_mode;
7533 rtx new_rtx = 0;
7534 rtx orig_pos_rtx = pos_rtx;
7535 HOST_WIDE_INT orig_pos;
7537 if (pos_rtx && CONST_INT_P (pos_rtx))
7538 pos = INTVAL (pos_rtx), pos_rtx = 0;
7540 if (GET_CODE (inner) == SUBREG
7541 && subreg_lowpart_p (inner)
7542 && (paradoxical_subreg_p (inner)
7543 /* If trying or potentionally trying to extract
7544 bits outside of is_mode, don't look through
7545 non-paradoxical SUBREGs. See PR82192. */
7546 || (pos_rtx == NULL_RTX
7547 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))))
7549 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7550 consider just the QI as the memory to extract from.
7551 The subreg adds or removes high bits; its mode is
7552 irrelevant to the meaning of this extraction,
7553 since POS and LEN count from the lsb. */
7554 if (MEM_P (SUBREG_REG (inner)))
7555 is_mode = GET_MODE (SUBREG_REG (inner));
7556 inner = SUBREG_REG (inner);
7558 else if (GET_CODE (inner) == ASHIFT
7559 && CONST_INT_P (XEXP (inner, 1))
7560 && pos_rtx == 0 && pos == 0
7561 && len > UINTVAL (XEXP (inner, 1)))
7563 /* We're extracting the least significant bits of an rtx
7564 (ashift X (const_int C)), where LEN > C. Extract the
7565 least significant (LEN - C) bits of X, giving an rtx
7566 whose mode is MODE, then shift it left C times. */
7567 new_rtx = make_extraction (mode, XEXP (inner, 0),
7568 0, 0, len - INTVAL (XEXP (inner, 1)),
7569 unsignedp, in_dest, in_compare);
7570 if (new_rtx != 0)
7571 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7573 else if (GET_CODE (inner) == MULT
7574 && CONST_INT_P (XEXP (inner, 1))
7575 && pos_rtx == 0 && pos == 0)
7577 /* We're extracting the least significant bits of an rtx
7578 (mult X (const_int 2^C)), where LEN > C. Extract the
7579 least significant (LEN - C) bits of X, giving an rtx
7580 whose mode is MODE, then multiply it by 2^C. */
7581 const HOST_WIDE_INT shift_amt = exact_log2 (INTVAL (XEXP (inner, 1)));
7582 if (IN_RANGE (shift_amt, 1, len - 1))
7584 new_rtx = make_extraction (mode, XEXP (inner, 0),
7585 0, 0, len - shift_amt,
7586 unsignedp, in_dest, in_compare);
7587 if (new_rtx)
7588 return gen_rtx_MULT (mode, new_rtx, XEXP (inner, 1));
7591 else if (GET_CODE (inner) == TRUNCATE
7592 /* If trying or potentionally trying to extract
7593 bits outside of is_mode, don't look through
7594 TRUNCATE. See PR82192. */
7595 && pos_rtx == NULL_RTX
7596 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))
7597 inner = XEXP (inner, 0);
7599 inner_mode = GET_MODE (inner);
7601 /* See if this can be done without an extraction. We never can if the
7602 width of the field is not the same as that of some integer mode. For
7603 registers, we can only avoid the extraction if the position is at the
7604 low-order bit and this is either not in the destination or we have the
7605 appropriate STRICT_LOW_PART operation available.
7607 For MEM, we can avoid an extract if the field starts on an appropriate
7608 boundary and we can change the mode of the memory reference. */
7610 scalar_int_mode tmode;
7611 if (int_mode_for_size (len, 1).exists (&tmode)
7612 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7613 && !MEM_P (inner)
7614 && (pos == 0 || REG_P (inner))
7615 && (inner_mode == tmode
7616 || !REG_P (inner)
7617 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7618 || reg_truncated_to_mode (tmode, inner))
7619 && (! in_dest
7620 || (REG_P (inner)
7621 && have_insn_for (STRICT_LOW_PART, tmode))))
7622 || (MEM_P (inner) && pos_rtx == 0
7623 && (pos
7624 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7625 : BITS_PER_UNIT)) == 0
7626 /* We can't do this if we are widening INNER_MODE (it
7627 may not be aligned, for one thing). */
7628 && !paradoxical_subreg_p (tmode, inner_mode)
7629 && known_le (pos + len, GET_MODE_PRECISION (is_mode))
7630 && (inner_mode == tmode
7631 || (! mode_dependent_address_p (XEXP (inner, 0),
7632 MEM_ADDR_SPACE (inner))
7633 && ! MEM_VOLATILE_P (inner))))))
7635 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7636 field. If the original and current mode are the same, we need not
7637 adjust the offset. Otherwise, we do if bytes big endian.
7639 If INNER is not a MEM, get a piece consisting of just the field
7640 of interest (in this case POS % BITS_PER_WORD must be 0). */
7642 if (MEM_P (inner))
7644 poly_int64 offset;
7646 /* POS counts from lsb, but make OFFSET count in memory order. */
7647 if (BYTES_BIG_ENDIAN)
7648 offset = bits_to_bytes_round_down (GET_MODE_PRECISION (is_mode)
7649 - len - pos);
7650 else
7651 offset = pos / BITS_PER_UNIT;
7653 new_rtx = adjust_address_nv (inner, tmode, offset);
7655 else if (REG_P (inner))
7657 if (tmode != inner_mode)
7659 /* We can't call gen_lowpart in a DEST since we
7660 always want a SUBREG (see below) and it would sometimes
7661 return a new hard register. */
7662 if (pos || in_dest)
7664 poly_uint64 offset
7665 = subreg_offset_from_lsb (tmode, inner_mode, pos);
7667 /* Avoid creating invalid subregs, for example when
7668 simplifying (x>>32)&255. */
7669 if (!validate_subreg (tmode, inner_mode, inner, offset))
7670 return NULL_RTX;
7672 new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7674 else
7675 new_rtx = gen_lowpart (tmode, inner);
7677 else
7678 new_rtx = inner;
7680 else
7681 new_rtx = force_to_mode (inner, tmode,
7682 len >= HOST_BITS_PER_WIDE_INT
7683 ? HOST_WIDE_INT_M1U
7684 : (HOST_WIDE_INT_1U << len) - 1, false);
7686 /* If this extraction is going into the destination of a SET,
7687 make a STRICT_LOW_PART unless we made a MEM. */
7689 if (in_dest)
7690 return (MEM_P (new_rtx) ? new_rtx
7691 : (GET_CODE (new_rtx) != SUBREG
7692 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7693 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7695 if (mode == tmode)
7696 return new_rtx;
7698 if (CONST_SCALAR_INT_P (new_rtx))
7699 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7700 mode, new_rtx, tmode);
7702 /* If we know that no extraneous bits are set, and that the high
7703 bit is not set, convert the extraction to the cheaper of
7704 sign and zero extension, that are equivalent in these cases. */
7705 if (flag_expensive_optimizations
7706 && (HWI_COMPUTABLE_MODE_P (tmode)
7707 && ((nonzero_bits (new_rtx, tmode)
7708 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7709 == 0)))
7711 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7712 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7714 /* Prefer ZERO_EXTENSION, since it gives more information to
7715 backends. */
7716 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7717 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7718 return temp;
7719 return temp1;
7722 /* Otherwise, sign- or zero-extend unless we already are in the
7723 proper mode. */
7725 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7726 mode, new_rtx));
7729 /* Unless this is a COMPARE or we have a funny memory reference,
7730 don't do anything with zero-extending field extracts starting at
7731 the low-order bit since they are simple AND operations. */
7732 if (pos_rtx == 0 && pos == 0 && ! in_dest
7733 && ! in_compare && unsignedp)
7734 return 0;
7736 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7737 if the position is not a constant and the length is not 1. In all
7738 other cases, we would only be going outside our object in cases when
7739 an original shift would have been undefined. */
7740 if (MEM_P (inner)
7741 && ((pos_rtx == 0 && maybe_gt (pos + len, GET_MODE_PRECISION (is_mode)))
7742 || (pos_rtx != 0 && len != 1)))
7743 return 0;
7745 enum extraction_pattern pattern = (in_dest ? EP_insv
7746 : unsignedp ? EP_extzv : EP_extv);
7748 /* If INNER is not from memory, we want it to have the mode of a register
7749 extraction pattern's structure operand, or word_mode if there is no
7750 such pattern. The same applies to extraction_mode and pos_mode
7751 and their respective operands.
7753 For memory, assume that the desired extraction_mode and pos_mode
7754 are the same as for a register operation, since at present we don't
7755 have named patterns for aligned memory structures. */
7756 class extraction_insn insn;
7757 unsigned int inner_size;
7758 if (GET_MODE_BITSIZE (inner_mode).is_constant (&inner_size)
7759 && get_best_reg_extraction_insn (&insn, pattern, inner_size, mode))
7761 wanted_inner_reg_mode = insn.struct_mode.require ();
7762 pos_mode = insn.pos_mode;
7763 extraction_mode = insn.field_mode;
7766 /* Never narrow an object, since that might not be safe. */
7768 if (mode != VOIDmode
7769 && partial_subreg_p (extraction_mode, mode))
7770 extraction_mode = mode;
7772 /* Punt if len is too large for extraction_mode. */
7773 if (maybe_gt (len, GET_MODE_PRECISION (extraction_mode)))
7774 return NULL_RTX;
7776 if (!MEM_P (inner))
7777 wanted_inner_mode = wanted_inner_reg_mode;
7778 else
7780 /* Be careful not to go beyond the extracted object and maintain the
7781 natural alignment of the memory. */
7782 wanted_inner_mode = smallest_int_mode_for_size (len);
7783 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7784 > GET_MODE_BITSIZE (wanted_inner_mode))
7785 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7788 orig_pos = pos;
7790 if (BITS_BIG_ENDIAN)
7792 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7793 BITS_BIG_ENDIAN style. If position is constant, compute new
7794 position. Otherwise, build subtraction.
7795 Note that POS is relative to the mode of the original argument.
7796 If it's a MEM we need to recompute POS relative to that.
7797 However, if we're extracting from (or inserting into) a register,
7798 we want to recompute POS relative to wanted_inner_mode. */
7799 int width;
7800 if (!MEM_P (inner))
7801 width = GET_MODE_BITSIZE (wanted_inner_mode);
7802 else if (!GET_MODE_BITSIZE (is_mode).is_constant (&width))
7803 return NULL_RTX;
7805 if (pos_rtx == 0)
7806 pos = width - len - pos;
7807 else
7808 pos_rtx
7809 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7810 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7811 pos_rtx);
7812 /* POS may be less than 0 now, but we check for that below.
7813 Note that it can only be less than 0 if !MEM_P (inner). */
7816 /* If INNER has a wider mode, and this is a constant extraction, try to
7817 make it smaller and adjust the byte to point to the byte containing
7818 the value. */
7819 if (wanted_inner_mode != VOIDmode
7820 && inner_mode != wanted_inner_mode
7821 && ! pos_rtx
7822 && partial_subreg_p (wanted_inner_mode, is_mode)
7823 && MEM_P (inner)
7824 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7825 && ! MEM_VOLATILE_P (inner))
7827 poly_int64 offset = 0;
7829 /* The computations below will be correct if the machine is big
7830 endian in both bits and bytes or little endian in bits and bytes.
7831 If it is mixed, we must adjust. */
7833 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7834 adjust OFFSET to compensate. */
7835 if (BYTES_BIG_ENDIAN
7836 && paradoxical_subreg_p (is_mode, inner_mode))
7837 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7839 /* We can now move to the desired byte. */
7840 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7841 * GET_MODE_SIZE (wanted_inner_mode);
7842 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7844 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7845 && is_mode != wanted_inner_mode)
7846 offset = (GET_MODE_SIZE (is_mode)
7847 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7849 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7852 /* If INNER is not memory, get it into the proper mode. If we are changing
7853 its mode, POS must be a constant and smaller than the size of the new
7854 mode. */
7855 else if (!MEM_P (inner))
7857 /* On the LHS, don't create paradoxical subregs implicitely truncating
7858 the register unless TARGET_TRULY_NOOP_TRUNCATION. */
7859 if (in_dest
7860 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7861 wanted_inner_mode))
7862 return NULL_RTX;
7864 if (GET_MODE (inner) != wanted_inner_mode
7865 && (pos_rtx != 0
7866 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7867 return NULL_RTX;
7869 if (orig_pos < 0)
7870 return NULL_RTX;
7872 inner = force_to_mode (inner, wanted_inner_mode,
7873 pos_rtx
7874 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7875 ? HOST_WIDE_INT_M1U
7876 : (((HOST_WIDE_INT_1U << len) - 1)
7877 << orig_pos), false);
7880 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7881 have to zero extend. Otherwise, we can just use a SUBREG.
7883 We dealt with constant rtxes earlier, so pos_rtx cannot
7884 have VOIDmode at this point. */
7885 if (pos_rtx != 0
7886 && (GET_MODE_SIZE (pos_mode)
7887 > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7889 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7890 GET_MODE (pos_rtx));
7892 /* If we know that no extraneous bits are set, and that the high
7893 bit is not set, convert extraction to cheaper one - either
7894 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7895 cases. */
7896 if (flag_expensive_optimizations
7897 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7898 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7899 & ~(((unsigned HOST_WIDE_INT)
7900 GET_MODE_MASK (GET_MODE (pos_rtx)))
7901 >> 1))
7902 == 0)))
7904 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7905 GET_MODE (pos_rtx));
7907 /* Prefer ZERO_EXTENSION, since it gives more information to
7908 backends. */
7909 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7910 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7911 temp = temp1;
7913 pos_rtx = temp;
7916 /* Make POS_RTX unless we already have it and it is correct. If we don't
7917 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7918 be a CONST_INT. */
7919 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7920 pos_rtx = orig_pos_rtx;
7922 else if (pos_rtx == 0)
7923 pos_rtx = GEN_INT (pos);
7925 /* Make the required operation. See if we can use existing rtx. */
7926 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7927 extraction_mode, inner, GEN_INT (len), pos_rtx);
7928 if (! in_dest)
7929 new_rtx = gen_lowpart (mode, new_rtx);
7931 return new_rtx;
7934 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7935 can be commuted with any other operations in X. Return X without
7936 that shift if so. */
7938 static rtx
7939 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7941 enum rtx_code code = GET_CODE (x);
7942 rtx tem;
7944 switch (code)
7946 case ASHIFT:
7947 /* This is the shift itself. If it is wide enough, we will return
7948 either the value being shifted if the shift count is equal to
7949 COUNT or a shift for the difference. */
7950 if (CONST_INT_P (XEXP (x, 1))
7951 && INTVAL (XEXP (x, 1)) >= count)
7952 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7953 INTVAL (XEXP (x, 1)) - count);
7954 break;
7956 case NEG: case NOT:
7957 if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7958 return simplify_gen_unary (code, mode, tem, mode);
7960 break;
7962 case PLUS: case IOR: case XOR: case AND:
7963 /* If we can safely shift this constant and we find the inner shift,
7964 make a new operation. */
7965 if (CONST_INT_P (XEXP (x, 1))
7966 && (UINTVAL (XEXP (x, 1))
7967 & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
7968 && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7970 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7971 return simplify_gen_binary (code, mode, tem,
7972 gen_int_mode (val, mode));
7974 break;
7976 default:
7977 break;
7980 return 0;
7983 /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
7984 level of the expression and MODE is its mode. IN_CODE is as for
7985 make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
7986 that should be used when recursing on operands of *X_PTR.
7988 There are two possible actions:
7990 - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
7991 equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
7993 - Return a new rtx, which the caller returns directly. */
7995 static rtx
7996 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
7997 enum rtx_code in_code,
7998 enum rtx_code *next_code_ptr)
8000 rtx x = *x_ptr;
8001 enum rtx_code next_code = *next_code_ptr;
8002 enum rtx_code code = GET_CODE (x);
8003 int mode_width = GET_MODE_PRECISION (mode);
8004 rtx rhs, lhs;
8005 rtx new_rtx = 0;
8006 int i;
8007 rtx tem;
8008 scalar_int_mode inner_mode;
8009 bool equality_comparison = false;
8011 if (in_code == EQ)
8013 equality_comparison = true;
8014 in_code = COMPARE;
8017 /* Process depending on the code of this operation. If NEW is set
8018 nonzero, it will be returned. */
8020 switch (code)
8022 case ASHIFT:
8023 /* Convert shifts by constants into multiplications if inside
8024 an address. */
8025 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
8026 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8027 && INTVAL (XEXP (x, 1)) >= 0)
8029 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
8030 HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
8032 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8033 if (GET_CODE (new_rtx) == NEG)
8035 new_rtx = XEXP (new_rtx, 0);
8036 multval = -multval;
8038 multval = trunc_int_for_mode (multval, mode);
8039 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
8041 break;
8043 case PLUS:
8044 lhs = XEXP (x, 0);
8045 rhs = XEXP (x, 1);
8046 lhs = make_compound_operation (lhs, next_code);
8047 rhs = make_compound_operation (rhs, next_code);
8048 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
8050 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
8051 XEXP (lhs, 1));
8052 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8054 else if (GET_CODE (lhs) == MULT
8055 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
8057 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
8058 simplify_gen_unary (NEG, mode,
8059 XEXP (lhs, 1),
8060 mode));
8061 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8063 else
8065 SUBST (XEXP (x, 0), lhs);
8066 SUBST (XEXP (x, 1), rhs);
8068 maybe_swap_commutative_operands (x);
8069 return x;
8071 case MINUS:
8072 lhs = XEXP (x, 0);
8073 rhs = XEXP (x, 1);
8074 lhs = make_compound_operation (lhs, next_code);
8075 rhs = make_compound_operation (rhs, next_code);
8076 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
8078 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
8079 XEXP (rhs, 1));
8080 return simplify_gen_binary (PLUS, mode, tem, lhs);
8082 else if (GET_CODE (rhs) == MULT
8083 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
8085 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
8086 simplify_gen_unary (NEG, mode,
8087 XEXP (rhs, 1),
8088 mode));
8089 return simplify_gen_binary (PLUS, mode, tem, lhs);
8091 else
8093 SUBST (XEXP (x, 0), lhs);
8094 SUBST (XEXP (x, 1), rhs);
8095 return x;
8098 case AND:
8099 /* If the second operand is not a constant, we can't do anything
8100 with it. */
8101 if (!CONST_INT_P (XEXP (x, 1)))
8102 break;
8104 /* If the constant is a power of two minus one and the first operand
8105 is a logical right shift, make an extraction. */
8106 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8107 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8109 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8110 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8111 i, true, false, in_code == COMPARE);
8114 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
8115 else if (GET_CODE (XEXP (x, 0)) == SUBREG
8116 && subreg_lowpart_p (XEXP (x, 0))
8117 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8118 &inner_mode)
8119 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8120 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8122 rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8123 new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8124 new_rtx = make_extraction (inner_mode, new_rtx, 0,
8125 XEXP (inner_x0, 1),
8126 i, true, false, in_code == COMPARE);
8128 /* If we narrowed the mode when dropping the subreg, then we lose. */
8129 if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8130 new_rtx = NULL;
8132 /* If that didn't give anything, see if the AND simplifies on
8133 its own. */
8134 if (!new_rtx && i >= 0)
8136 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8137 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i,
8138 true, false, in_code == COMPARE);
8141 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8142 else if ((GET_CODE (XEXP (x, 0)) == XOR
8143 || GET_CODE (XEXP (x, 0)) == IOR)
8144 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8145 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8146 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8148 /* Apply the distributive law, and then try to make extractions. */
8149 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8150 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8151 XEXP (x, 1)),
8152 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8153 XEXP (x, 1)));
8154 new_rtx = make_compound_operation (new_rtx, in_code);
8157 /* If we are have (and (rotate X C) M) and C is larger than the number
8158 of bits in M, this is an extraction. */
8160 else if (GET_CODE (XEXP (x, 0)) == ROTATE
8161 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8162 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8163 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8165 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8166 new_rtx = make_extraction (mode, new_rtx,
8167 (GET_MODE_PRECISION (mode)
8168 - INTVAL (XEXP (XEXP (x, 0), 1))),
8169 NULL_RTX, i, true, false,
8170 in_code == COMPARE);
8173 /* On machines without logical shifts, if the operand of the AND is
8174 a logical shift and our mask turns off all the propagated sign
8175 bits, we can replace the logical shift with an arithmetic shift. */
8176 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8177 && !have_insn_for (LSHIFTRT, mode)
8178 && have_insn_for (ASHIFTRT, mode)
8179 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8180 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8181 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8182 && mode_width <= HOST_BITS_PER_WIDE_INT)
8184 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8186 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8187 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8188 SUBST (XEXP (x, 0),
8189 gen_rtx_ASHIFTRT (mode,
8190 make_compound_operation (XEXP (XEXP (x,
8193 next_code),
8194 XEXP (XEXP (x, 0), 1)));
8197 /* If the constant is one less than a power of two, this might be
8198 representable by an extraction even if no shift is present.
8199 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8200 we are in a COMPARE. */
8201 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8202 new_rtx = make_extraction (mode,
8203 make_compound_operation (XEXP (x, 0),
8204 next_code),
8205 0, NULL_RTX, i,
8206 true, false, in_code == COMPARE);
8208 /* If we are in a comparison and this is an AND with a power of two,
8209 convert this into the appropriate bit extract. */
8210 else if (in_code == COMPARE
8211 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8212 && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8213 new_rtx = make_extraction (mode,
8214 make_compound_operation (XEXP (x, 0),
8215 next_code),
8216 i, NULL_RTX, 1, true, false, true);
8218 /* If the one operand is a paradoxical subreg of a register or memory and
8219 the constant (limited to the smaller mode) has only zero bits where
8220 the sub expression has known zero bits, this can be expressed as
8221 a zero_extend. */
8222 else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8224 rtx sub;
8226 sub = XEXP (XEXP (x, 0), 0);
8227 machine_mode sub_mode = GET_MODE (sub);
8228 int sub_width;
8229 if ((REG_P (sub) || MEM_P (sub))
8230 && GET_MODE_PRECISION (sub_mode).is_constant (&sub_width)
8231 && sub_width < mode_width)
8233 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8234 unsigned HOST_WIDE_INT mask;
8236 /* original AND constant with all the known zero bits set */
8237 mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8238 if ((mask & mode_mask) == mode_mask)
8240 new_rtx = make_compound_operation (sub, next_code);
8241 new_rtx = make_extraction (mode, new_rtx, 0, 0, sub_width,
8242 true, false, in_code == COMPARE);
8247 break;
8249 case LSHIFTRT:
8250 /* If the sign bit is known to be zero, replace this with an
8251 arithmetic shift. */
8252 if (have_insn_for (ASHIFTRT, mode)
8253 && ! have_insn_for (LSHIFTRT, mode)
8254 && mode_width <= HOST_BITS_PER_WIDE_INT
8255 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8257 new_rtx = gen_rtx_ASHIFTRT (mode,
8258 make_compound_operation (XEXP (x, 0),
8259 next_code),
8260 XEXP (x, 1));
8261 break;
8264 /* fall through */
8266 case ASHIFTRT:
8267 lhs = XEXP (x, 0);
8268 rhs = XEXP (x, 1);
8270 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8271 this is a SIGN_EXTRACT. */
8272 if (CONST_INT_P (rhs)
8273 && GET_CODE (lhs) == ASHIFT
8274 && CONST_INT_P (XEXP (lhs, 1))
8275 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8276 && INTVAL (XEXP (lhs, 1)) >= 0
8277 && INTVAL (rhs) < mode_width)
8279 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8280 new_rtx = make_extraction (mode, new_rtx,
8281 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8282 NULL_RTX, mode_width - INTVAL (rhs),
8283 code == LSHIFTRT, false,
8284 in_code == COMPARE);
8285 break;
8288 /* See if we have operations between an ASHIFTRT and an ASHIFT.
8289 If so, try to merge the shifts into a SIGN_EXTEND. We could
8290 also do this for some cases of SIGN_EXTRACT, but it doesn't
8291 seem worth the effort; the case checked for occurs on Alpha. */
8293 if (!OBJECT_P (lhs)
8294 && ! (GET_CODE (lhs) == SUBREG
8295 && (OBJECT_P (SUBREG_REG (lhs))))
8296 && CONST_INT_P (rhs)
8297 && INTVAL (rhs) >= 0
8298 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8299 && INTVAL (rhs) < mode_width
8300 && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8301 new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8302 next_code),
8303 0, NULL_RTX, mode_width - INTVAL (rhs),
8304 code == LSHIFTRT, false, in_code == COMPARE);
8306 break;
8308 case SUBREG:
8309 /* Call ourselves recursively on the inner expression. If we are
8310 narrowing the object and it has a different RTL code from
8311 what it originally did, do this SUBREG as a force_to_mode. */
8313 rtx inner = SUBREG_REG (x), simplified;
8314 enum rtx_code subreg_code = in_code;
8316 /* If the SUBREG is masking of a logical right shift,
8317 make an extraction. */
8318 if (GET_CODE (inner) == LSHIFTRT
8319 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8320 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8321 && CONST_INT_P (XEXP (inner, 1))
8322 && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8323 && subreg_lowpart_p (x))
8325 new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8326 int width = GET_MODE_PRECISION (inner_mode)
8327 - INTVAL (XEXP (inner, 1));
8328 if (width > mode_width)
8329 width = mode_width;
8330 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8331 width, true, false, in_code == COMPARE);
8332 break;
8335 /* If in_code is COMPARE, it isn't always safe to pass it through
8336 to the recursive make_compound_operation call. */
8337 if (subreg_code == COMPARE
8338 && (!subreg_lowpart_p (x)
8339 || GET_CODE (inner) == SUBREG
8340 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8341 is (const_int 0), rather than
8342 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8343 Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8344 for non-equality comparisons against 0 is not equivalent
8345 to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8346 || (GET_CODE (inner) == AND
8347 && CONST_INT_P (XEXP (inner, 1))
8348 && partial_subreg_p (x)
8349 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8350 >= GET_MODE_BITSIZE (mode) - 1)))
8351 subreg_code = SET;
8353 tem = make_compound_operation (inner, subreg_code);
8355 simplified
8356 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8357 if (simplified)
8358 tem = simplified;
8360 if (GET_CODE (tem) != GET_CODE (inner)
8361 && partial_subreg_p (x)
8362 && subreg_lowpart_p (x))
8364 rtx newer
8365 = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, false);
8367 /* If we have something other than a SUBREG, we might have
8368 done an expansion, so rerun ourselves. */
8369 if (GET_CODE (newer) != SUBREG)
8370 newer = make_compound_operation (newer, in_code);
8372 /* force_to_mode can expand compounds. If it just re-expanded
8373 the compound, use gen_lowpart to convert to the desired
8374 mode. */
8375 if (rtx_equal_p (newer, x)
8376 /* Likewise if it re-expanded the compound only partially.
8377 This happens for SUBREG of ZERO_EXTRACT if they extract
8378 the same number of bits. */
8379 || (GET_CODE (newer) == SUBREG
8380 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8381 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8382 && GET_CODE (inner) == AND
8383 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8384 return gen_lowpart (GET_MODE (x), tem);
8386 return newer;
8389 if (simplified)
8390 return tem;
8392 break;
8394 default:
8395 break;
8398 if (new_rtx)
8399 *x_ptr = gen_lowpart (mode, new_rtx);
8400 *next_code_ptr = next_code;
8401 return NULL_RTX;
8404 /* Look at the expression rooted at X. Look for expressions
8405 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8406 Form these expressions.
8408 Return the new rtx, usually just X.
8410 Also, for machines like the VAX that don't have logical shift insns,
8411 try to convert logical to arithmetic shift operations in cases where
8412 they are equivalent. This undoes the canonicalizations to logical
8413 shifts done elsewhere.
8415 We try, as much as possible, to re-use rtl expressions to save memory.
8417 IN_CODE says what kind of expression we are processing. Normally, it is
8418 SET. In a memory address it is MEM. When processing the arguments of
8419 a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8420 precisely it is an equality comparison against zero. */
8423 make_compound_operation (rtx x, enum rtx_code in_code)
8425 enum rtx_code code = GET_CODE (x);
8426 const char *fmt;
8427 int i, j;
8428 enum rtx_code next_code;
8429 rtx new_rtx, tem;
8431 /* Select the code to be used in recursive calls. Once we are inside an
8432 address, we stay there. If we have a comparison, set to COMPARE,
8433 but once inside, go back to our default of SET. */
8435 next_code = (code == MEM ? MEM
8436 : ((code == COMPARE || COMPARISON_P (x))
8437 && XEXP (x, 1) == const0_rtx) ? COMPARE
8438 : in_code == COMPARE || in_code == EQ ? SET : in_code);
8440 scalar_int_mode mode;
8441 if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8443 rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8444 &next_code);
8445 if (new_rtx)
8446 return new_rtx;
8447 code = GET_CODE (x);
8450 /* Now recursively process each operand of this operation. We need to
8451 handle ZERO_EXTEND specially so that we don't lose track of the
8452 inner mode. */
8453 if (code == ZERO_EXTEND)
8455 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8456 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8457 new_rtx, GET_MODE (XEXP (x, 0)));
8458 if (tem)
8459 return tem;
8460 SUBST (XEXP (x, 0), new_rtx);
8461 return x;
8464 fmt = GET_RTX_FORMAT (code);
8465 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8466 if (fmt[i] == 'e')
8468 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8469 SUBST (XEXP (x, i), new_rtx);
8471 else if (fmt[i] == 'E')
8472 for (j = 0; j < XVECLEN (x, i); j++)
8474 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8475 SUBST (XVECEXP (x, i, j), new_rtx);
8478 maybe_swap_commutative_operands (x);
8479 return x;
8482 /* Given M see if it is a value that would select a field of bits
8483 within an item, but not the entire word. Return -1 if not.
8484 Otherwise, return the starting position of the field, where 0 is the
8485 low-order bit.
8487 *PLEN is set to the length of the field. */
8489 static int
8490 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8492 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8493 int pos = m ? ctz_hwi (m) : -1;
8494 int len = 0;
8496 if (pos >= 0)
8497 /* Now shift off the low-order zero bits and see if we have a
8498 power of two minus 1. */
8499 len = exact_log2 ((m >> pos) + 1);
8501 if (len <= 0)
8502 pos = -1;
8504 *plen = len;
8505 return pos;
8508 /* If X refers to a register that equals REG in value, replace these
8509 references with REG. */
8510 static rtx
8511 canon_reg_for_combine (rtx x, rtx reg)
8513 rtx op0, op1, op2;
8514 const char *fmt;
8515 int i;
8516 bool copied;
8518 enum rtx_code code = GET_CODE (x);
8519 switch (GET_RTX_CLASS (code))
8521 case RTX_UNARY:
8522 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8523 if (op0 != XEXP (x, 0))
8524 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8525 GET_MODE (reg));
8526 break;
8528 case RTX_BIN_ARITH:
8529 case RTX_COMM_ARITH:
8530 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8531 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8532 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8533 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8534 break;
8536 case RTX_COMPARE:
8537 case RTX_COMM_COMPARE:
8538 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8539 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8540 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8541 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8542 GET_MODE (op0), op0, op1);
8543 break;
8545 case RTX_TERNARY:
8546 case RTX_BITFIELD_OPS:
8547 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8548 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8549 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8550 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8551 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8552 GET_MODE (op0), op0, op1, op2);
8553 /* FALLTHRU */
8555 case RTX_OBJ:
8556 if (REG_P (x))
8558 if (rtx_equal_p (get_last_value (reg), x)
8559 || rtx_equal_p (reg, get_last_value (x)))
8560 return reg;
8561 else
8562 break;
8565 /* fall through */
8567 default:
8568 fmt = GET_RTX_FORMAT (code);
8569 copied = false;
8570 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8571 if (fmt[i] == 'e')
8573 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8574 if (op != XEXP (x, i))
8576 if (!copied)
8578 copied = true;
8579 x = copy_rtx (x);
8581 XEXP (x, i) = op;
8584 else if (fmt[i] == 'E')
8586 int j;
8587 for (j = 0; j < XVECLEN (x, i); j++)
8589 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8590 if (op != XVECEXP (x, i, j))
8592 if (!copied)
8594 copied = true;
8595 x = copy_rtx (x);
8597 XVECEXP (x, i, j) = op;
8602 break;
8605 return x;
8608 /* Return X converted to MODE. If the value is already truncated to
8609 MODE we can just return a subreg even though in the general case we
8610 would need an explicit truncation. */
8612 static rtx
8613 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8615 if (!CONST_INT_P (x)
8616 && partial_subreg_p (mode, GET_MODE (x))
8617 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8618 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8620 /* Bit-cast X into an integer mode. */
8621 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8622 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8623 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8624 x, GET_MODE (x));
8627 return gen_lowpart (mode, x);
8630 /* See if X can be simplified knowing that we will only refer to it in
8631 MODE and will only refer to those bits that are nonzero in MASK.
8632 If other bits are being computed or if masking operations are done
8633 that select a superset of the bits in MASK, they can sometimes be
8634 ignored.
8636 Return a possibly simplified expression, but always convert X to
8637 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8639 If JUST_SELECT is true, don't optimize by noticing that bits in MASK
8640 are all off in X. This is used when X will be complemented, by either
8641 NOT, NEG, or XOR. */
8643 static rtx
8644 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8645 bool just_select)
8647 enum rtx_code code = GET_CODE (x);
8648 bool next_select = just_select || code == XOR || code == NOT || code == NEG;
8649 machine_mode op_mode;
8650 unsigned HOST_WIDE_INT nonzero;
8652 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8653 code below will do the wrong thing since the mode of such an
8654 expression is VOIDmode.
8656 Also do nothing if X is a CLOBBER; this can happen if X was
8657 the return value from a call to gen_lowpart. */
8658 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8659 return x;
8661 /* We want to perform the operation in its present mode unless we know
8662 that the operation is valid in MODE, in which case we do the operation
8663 in MODE. */
8664 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8665 && have_insn_for (code, mode))
8666 ? mode : GET_MODE (x));
8668 /* It is not valid to do a right-shift in a narrower mode
8669 than the one it came in with. */
8670 if ((code == LSHIFTRT || code == ASHIFTRT)
8671 && partial_subreg_p (mode, GET_MODE (x)))
8672 op_mode = GET_MODE (x);
8674 /* Truncate MASK to fit OP_MODE. */
8675 if (op_mode)
8676 mask &= GET_MODE_MASK (op_mode);
8678 /* Determine what bits of X are guaranteed to be (non)zero. */
8679 nonzero = nonzero_bits (x, mode);
8681 /* If none of the bits in X are needed, return a zero. */
8682 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8683 x = const0_rtx;
8685 /* If X is a CONST_INT, return a new one. Do this here since the
8686 test below will fail. */
8687 if (CONST_INT_P (x))
8689 if (SCALAR_INT_MODE_P (mode))
8690 return gen_int_mode (INTVAL (x) & mask, mode);
8691 else
8693 x = GEN_INT (INTVAL (x) & mask);
8694 return gen_lowpart_common (mode, x);
8698 /* If X is narrower than MODE and we want all the bits in X's mode, just
8699 get X in the proper mode. */
8700 if (paradoxical_subreg_p (mode, GET_MODE (x))
8701 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8702 return gen_lowpart (mode, x);
8704 /* We can ignore the effect of a SUBREG if it narrows the mode or
8705 if the constant masks to zero all the bits the mode doesn't have. */
8706 if (GET_CODE (x) == SUBREG
8707 && subreg_lowpart_p (x)
8708 && (partial_subreg_p (x)
8709 || (mask
8710 & GET_MODE_MASK (GET_MODE (x))
8711 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0))
8712 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8714 scalar_int_mode int_mode, xmode;
8715 if (is_a <scalar_int_mode> (mode, &int_mode)
8716 && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8717 /* OP_MODE is either MODE or XMODE, so it must be a scalar
8718 integer too. */
8719 return force_int_to_mode (x, int_mode, xmode,
8720 as_a <scalar_int_mode> (op_mode),
8721 mask, just_select);
8723 return gen_lowpart_or_truncate (mode, x);
8726 /* Subroutine of force_to_mode that handles cases in which both X and
8727 the result are scalar integers. MODE is the mode of the result,
8728 XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8729 is preferred for simplified versions of X. The other arguments
8730 are as for force_to_mode. */
8732 static rtx
8733 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8734 scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8735 bool just_select)
8737 enum rtx_code code = GET_CODE (x);
8738 bool next_select = just_select || code == XOR || code == NOT || code == NEG;
8739 unsigned HOST_WIDE_INT fuller_mask;
8740 rtx op0, op1, temp;
8741 poly_int64 const_op0;
8743 /* When we have an arithmetic operation, or a shift whose count we
8744 do not know, we need to assume that all bits up to the highest-order
8745 bit in MASK will be needed. This is how we form such a mask. */
8746 if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8747 fuller_mask = HOST_WIDE_INT_M1U;
8748 else
8749 fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1)) - 1);
8751 switch (code)
8753 case CLOBBER:
8754 /* If X is a (clobber (const_int)), return it since we know we are
8755 generating something that won't match. */
8756 return x;
8758 case SIGN_EXTEND:
8759 case ZERO_EXTEND:
8760 case ZERO_EXTRACT:
8761 case SIGN_EXTRACT:
8762 x = expand_compound_operation (x);
8763 if (GET_CODE (x) != code)
8764 return force_to_mode (x, mode, mask, next_select);
8765 break;
8767 case TRUNCATE:
8768 /* Similarly for a truncate. */
8769 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8771 case AND:
8772 /* If this is an AND with a constant, convert it into an AND
8773 whose constant is the AND of that constant with MASK. If it
8774 remains an AND of MASK, delete it since it is redundant. */
8776 if (CONST_INT_P (XEXP (x, 1)))
8778 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8779 mask & INTVAL (XEXP (x, 1)));
8780 xmode = op_mode;
8782 /* If X is still an AND, see if it is an AND with a mask that
8783 is just some low-order bits. If so, and it is MASK, we don't
8784 need it. */
8786 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8787 && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8788 x = XEXP (x, 0);
8790 /* If it remains an AND, try making another AND with the bits
8791 in the mode mask that aren't in MASK turned on. If the
8792 constant in the AND is wide enough, this might make a
8793 cheaper constant. */
8795 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8796 && GET_MODE_MASK (xmode) != mask
8797 && HWI_COMPUTABLE_MODE_P (xmode))
8799 unsigned HOST_WIDE_INT cval
8800 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8801 rtx y;
8803 y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8804 gen_int_mode (cval, xmode));
8805 if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8806 < set_src_cost (x, xmode, optimize_this_for_speed_p))
8807 x = y;
8810 break;
8813 goto binop;
8815 case PLUS:
8816 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8817 low-order bits (as in an alignment operation) and FOO is already
8818 aligned to that boundary, mask C1 to that boundary as well.
8819 This may eliminate that PLUS and, later, the AND. */
8822 unsigned int width = GET_MODE_PRECISION (mode);
8823 unsigned HOST_WIDE_INT smask = mask;
8825 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8826 number, sign extend it. */
8828 if (width < HOST_BITS_PER_WIDE_INT
8829 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8830 smask |= HOST_WIDE_INT_M1U << width;
8832 if (CONST_INT_P (XEXP (x, 1))
8833 && pow2p_hwi (- smask)
8834 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8835 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8836 return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8837 (INTVAL (XEXP (x, 1)) & smask)),
8838 mode, smask, next_select);
8841 /* fall through */
8843 case MULT:
8844 /* Substituting into the operands of a widening MULT is not likely to
8845 create RTL matching a machine insn. */
8846 if (code == MULT
8847 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8848 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8849 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8850 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8851 && REG_P (XEXP (XEXP (x, 0), 0))
8852 && REG_P (XEXP (XEXP (x, 1), 0)))
8853 return gen_lowpart_or_truncate (mode, x);
8855 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8856 most significant bit in MASK since carries from those bits will
8857 affect the bits we are interested in. */
8858 mask = fuller_mask;
8859 goto binop;
8861 case MINUS:
8862 /* If X is (minus C Y) where C's least set bit is larger than any bit
8863 in the mask, then we may replace with (neg Y). */
8864 if (poly_int_rtx_p (XEXP (x, 0), &const_op0)
8865 && known_alignment (poly_uint64 (const_op0)) > mask)
8867 x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8868 return force_to_mode (x, mode, mask, next_select);
8871 /* Similarly, if C contains every bit in the fuller_mask, then we may
8872 replace with (not Y). */
8873 if (CONST_INT_P (XEXP (x, 0))
8874 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8876 x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8877 return force_to_mode (x, mode, mask, next_select);
8880 mask = fuller_mask;
8881 goto binop;
8883 case IOR:
8884 case XOR:
8885 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8886 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8887 operation which may be a bitfield extraction. Ensure that the
8888 constant we form is not wider than the mode of X. */
8890 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8891 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8892 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8893 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8894 && CONST_INT_P (XEXP (x, 1))
8895 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8896 + floor_log2 (INTVAL (XEXP (x, 1))))
8897 < GET_MODE_PRECISION (xmode))
8898 && (UINTVAL (XEXP (x, 1))
8899 & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8901 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8902 << INTVAL (XEXP (XEXP (x, 0), 1)),
8903 xmode);
8904 temp = simplify_gen_binary (GET_CODE (x), xmode,
8905 XEXP (XEXP (x, 0), 0), temp);
8906 x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8907 XEXP (XEXP (x, 0), 1));
8908 return force_to_mode (x, mode, mask, next_select);
8911 binop:
8912 /* For most binary operations, just propagate into the operation and
8913 change the mode if we have an operation of that mode. */
8915 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8916 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8918 /* If we ended up truncating both operands, truncate the result of the
8919 operation instead. */
8920 if (GET_CODE (op0) == TRUNCATE
8921 && GET_CODE (op1) == TRUNCATE)
8923 op0 = XEXP (op0, 0);
8924 op1 = XEXP (op1, 0);
8927 op0 = gen_lowpart_or_truncate (op_mode, op0);
8928 op1 = gen_lowpart_or_truncate (op_mode, op1);
8930 if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8932 x = simplify_gen_binary (code, op_mode, op0, op1);
8933 xmode = op_mode;
8935 break;
8937 case ASHIFT:
8938 /* For left shifts, do the same, but just for the first operand.
8939 However, we cannot do anything with shifts where we cannot
8940 guarantee that the counts are smaller than the size of the mode
8941 because such a count will have a different meaning in a
8942 wider mode. */
8944 if (! (CONST_INT_P (XEXP (x, 1))
8945 && INTVAL (XEXP (x, 1)) >= 0
8946 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8947 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8948 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8949 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8950 break;
8952 /* If the shift count is a constant and we can do arithmetic in
8953 the mode of the shift, refine which bits we need. Otherwise, use the
8954 conservative form of the mask. */
8955 if (CONST_INT_P (XEXP (x, 1))
8956 && INTVAL (XEXP (x, 1)) >= 0
8957 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8958 && HWI_COMPUTABLE_MODE_P (op_mode))
8959 mask >>= INTVAL (XEXP (x, 1));
8960 else
8961 mask = fuller_mask;
8963 op0 = gen_lowpart_or_truncate (op_mode,
8964 force_to_mode (XEXP (x, 0), mode,
8965 mask, next_select));
8967 if (op_mode != xmode || op0 != XEXP (x, 0))
8969 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8970 xmode = op_mode;
8972 break;
8974 case LSHIFTRT:
8975 /* Here we can only do something if the shift count is a constant,
8976 this shift constant is valid for the host, and we can do arithmetic
8977 in OP_MODE. */
8979 if (CONST_INT_P (XEXP (x, 1))
8980 && INTVAL (XEXP (x, 1)) >= 0
8981 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8982 && HWI_COMPUTABLE_MODE_P (op_mode))
8984 rtx inner = XEXP (x, 0);
8985 unsigned HOST_WIDE_INT inner_mask;
8987 /* Select the mask of the bits we need for the shift operand. */
8988 inner_mask = mask << INTVAL (XEXP (x, 1));
8990 /* We can only change the mode of the shift if we can do arithmetic
8991 in the mode of the shift and INNER_MASK is no wider than the
8992 width of X's mode. */
8993 if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
8994 op_mode = xmode;
8996 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8998 if (xmode != op_mode || inner != XEXP (x, 0))
9000 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
9001 xmode = op_mode;
9005 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
9006 shift and AND produces only copies of the sign bit (C2 is one less
9007 than a power of two), we can do this with just a shift. */
9009 if (GET_CODE (x) == LSHIFTRT
9010 && CONST_INT_P (XEXP (x, 1))
9011 /* The shift puts one of the sign bit copies in the least significant
9012 bit. */
9013 && ((INTVAL (XEXP (x, 1))
9014 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
9015 >= GET_MODE_PRECISION (xmode))
9016 && pow2p_hwi (mask + 1)
9017 /* Number of bits left after the shift must be more than the mask
9018 needs. */
9019 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
9020 <= GET_MODE_PRECISION (xmode))
9021 /* Must be more sign bit copies than the mask needs. */
9022 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
9023 >= exact_log2 (mask + 1)))
9025 int nbits = GET_MODE_PRECISION (xmode) - exact_log2 (mask + 1);
9026 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
9027 gen_int_shift_amount (xmode, nbits));
9029 goto shiftrt;
9031 case ASHIFTRT:
9032 /* If we are just looking for the sign bit, we don't need this shift at
9033 all, even if it has a variable count. */
9034 if (val_signbit_p (xmode, mask))
9035 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9037 /* If this is a shift by a constant, get a mask that contains those bits
9038 that are not copies of the sign bit. We then have two cases: If
9039 MASK only includes those bits, this can be a logical shift, which may
9040 allow simplifications. If MASK is a single-bit field not within
9041 those bits, we are requesting a copy of the sign bit and hence can
9042 shift the sign bit to the appropriate location. */
9044 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
9045 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
9047 unsigned HOST_WIDE_INT nonzero;
9048 int i;
9050 /* If the considered data is wider than HOST_WIDE_INT, we can't
9051 represent a mask for all its bits in a single scalar.
9052 But we only care about the lower bits, so calculate these. */
9054 if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
9056 nonzero = HOST_WIDE_INT_M1U;
9058 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
9059 is the number of bits a full-width mask would have set.
9060 We need only shift if these are fewer than nonzero can
9061 hold. If not, we must keep all bits set in nonzero. */
9063 if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
9064 < HOST_BITS_PER_WIDE_INT)
9065 nonzero >>= INTVAL (XEXP (x, 1))
9066 + HOST_BITS_PER_WIDE_INT
9067 - GET_MODE_PRECISION (xmode);
9069 else
9071 nonzero = GET_MODE_MASK (xmode);
9072 nonzero >>= INTVAL (XEXP (x, 1));
9075 if ((mask & ~nonzero) == 0)
9077 x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
9078 XEXP (x, 0), INTVAL (XEXP (x, 1)));
9079 if (GET_CODE (x) != ASHIFTRT)
9080 return force_to_mode (x, mode, mask, next_select);
9083 else if ((i = exact_log2 (mask)) >= 0)
9085 x = simplify_shift_const
9086 (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
9087 GET_MODE_PRECISION (xmode) - 1 - i);
9089 if (GET_CODE (x) != ASHIFTRT)
9090 return force_to_mode (x, mode, mask, next_select);
9094 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
9095 even if the shift count isn't a constant. */
9096 if (mask == 1)
9097 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
9099 shiftrt:
9101 /* If this is a zero- or sign-extension operation that just affects bits
9102 we don't care about, remove it. Be sure the call above returned
9103 something that is still a shift. */
9105 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9106 && CONST_INT_P (XEXP (x, 1))
9107 && INTVAL (XEXP (x, 1)) >= 0
9108 && (INTVAL (XEXP (x, 1))
9109 <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9110 && GET_CODE (XEXP (x, 0)) == ASHIFT
9111 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9112 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask, next_select);
9114 break;
9116 case ROTATE:
9117 case ROTATERT:
9118 /* If the shift count is constant and we can do computations
9119 in the mode of X, compute where the bits we care about are.
9120 Otherwise, we can't do anything. Don't change the mode of
9121 the shift or propagate MODE into the shift, though. */
9122 if (CONST_INT_P (XEXP (x, 1))
9123 && INTVAL (XEXP (x, 1)) >= 0)
9125 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9126 xmode, gen_int_mode (mask, xmode),
9127 XEXP (x, 1));
9128 if (temp && CONST_INT_P (temp))
9129 x = simplify_gen_binary (code, xmode,
9130 force_to_mode (XEXP (x, 0), xmode,
9131 INTVAL (temp), next_select),
9132 XEXP (x, 1));
9134 break;
9136 case NEG:
9137 /* If we just want the low-order bit, the NEG isn't needed since it
9138 won't change the low-order bit. */
9139 if (mask == 1)
9140 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9142 /* We need any bits less significant than the most significant bit in
9143 MASK since carries from those bits will affect the bits we are
9144 interested in. */
9145 mask = fuller_mask;
9146 goto unop;
9148 case NOT:
9149 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9150 same as the XOR case above. Ensure that the constant we form is not
9151 wider than the mode of X. */
9153 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9154 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9155 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9156 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9157 < GET_MODE_PRECISION (xmode))
9158 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9160 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9161 temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9162 x = simplify_gen_binary (LSHIFTRT, xmode,
9163 temp, XEXP (XEXP (x, 0), 1));
9165 return force_to_mode (x, mode, mask, next_select);
9168 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9169 use the full mask inside the NOT. */
9170 mask = fuller_mask;
9172 unop:
9173 op0 = gen_lowpart_or_truncate (op_mode,
9174 force_to_mode (XEXP (x, 0), mode, mask,
9175 next_select));
9176 if (op_mode != xmode || op0 != XEXP (x, 0))
9178 x = simplify_gen_unary (code, op_mode, op0, op_mode);
9179 xmode = op_mode;
9181 break;
9183 case NE:
9184 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9185 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9186 which is equal to STORE_FLAG_VALUE. */
9187 if ((mask & ~STORE_FLAG_VALUE) == 0
9188 && XEXP (x, 1) == const0_rtx
9189 && GET_MODE (XEXP (x, 0)) == mode
9190 && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9191 && (nonzero_bits (XEXP (x, 0), mode)
9192 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9193 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9195 break;
9197 case IF_THEN_ELSE:
9198 /* We have no way of knowing if the IF_THEN_ELSE can itself be
9199 written in a narrower mode. We play it safe and do not do so. */
9201 op0 = gen_lowpart_or_truncate (xmode,
9202 force_to_mode (XEXP (x, 1), mode,
9203 mask, next_select));
9204 op1 = gen_lowpart_or_truncate (xmode,
9205 force_to_mode (XEXP (x, 2), mode,
9206 mask, next_select));
9207 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9208 x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9209 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9210 op0, op1);
9211 break;
9213 default:
9214 break;
9217 /* Ensure we return a value of the proper mode. */
9218 return gen_lowpart_or_truncate (mode, x);
9221 /* Return nonzero if X is an expression that has one of two values depending on
9222 whether some other value is zero or nonzero. In that case, we return the
9223 value that is being tested, *PTRUE is set to the value if the rtx being
9224 returned has a nonzero value, and *PFALSE is set to the other alternative.
9226 If we return zero, we set *PTRUE and *PFALSE to X. */
9228 static rtx
9229 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9231 machine_mode mode = GET_MODE (x);
9232 enum rtx_code code = GET_CODE (x);
9233 rtx cond0, cond1, true0, true1, false0, false1;
9234 unsigned HOST_WIDE_INT nz;
9235 scalar_int_mode int_mode;
9237 /* If we are comparing a value against zero, we are done. */
9238 if ((code == NE || code == EQ)
9239 && XEXP (x, 1) == const0_rtx)
9241 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9242 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9243 return XEXP (x, 0);
9246 /* If this is a unary operation whose operand has one of two values, apply
9247 our opcode to compute those values. */
9248 else if (UNARY_P (x)
9249 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9251 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9252 *pfalse = simplify_gen_unary (code, mode, false0,
9253 GET_MODE (XEXP (x, 0)));
9254 return cond0;
9257 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9258 make can't possibly match and would suppress other optimizations. */
9259 else if (code == COMPARE)
9262 /* If this is a binary operation, see if either side has only one of two
9263 values. If either one does or if both do and they are conditional on
9264 the same value, compute the new true and false values. */
9265 else if (BINARY_P (x))
9267 rtx op0 = XEXP (x, 0);
9268 rtx op1 = XEXP (x, 1);
9269 cond0 = if_then_else_cond (op0, &true0, &false0);
9270 cond1 = if_then_else_cond (op1, &true1, &false1);
9272 if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9273 && (REG_P (op0) || REG_P (op1)))
9275 /* Try to enable a simplification by undoing work done by
9276 if_then_else_cond if it converted a REG into something more
9277 complex. */
9278 if (REG_P (op0))
9280 cond0 = 0;
9281 true0 = false0 = op0;
9283 else
9285 cond1 = 0;
9286 true1 = false1 = op1;
9290 if ((cond0 != 0 || cond1 != 0)
9291 && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9293 /* If if_then_else_cond returned zero, then true/false are the
9294 same rtl. We must copy one of them to prevent invalid rtl
9295 sharing. */
9296 if (cond0 == 0)
9297 true0 = copy_rtx (true0);
9298 else if (cond1 == 0)
9299 true1 = copy_rtx (true1);
9301 if (COMPARISON_P (x))
9303 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9304 true0, true1);
9305 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9306 false0, false1);
9308 else
9310 *ptrue = simplify_gen_binary (code, mode, true0, true1);
9311 *pfalse = simplify_gen_binary (code, mode, false0, false1);
9314 return cond0 ? cond0 : cond1;
9317 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9318 operands is zero when the other is nonzero, and vice-versa,
9319 and STORE_FLAG_VALUE is 1 or -1. */
9321 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9322 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9323 || code == UMAX)
9324 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9326 rtx op0 = XEXP (XEXP (x, 0), 1);
9327 rtx op1 = XEXP (XEXP (x, 1), 1);
9329 cond0 = XEXP (XEXP (x, 0), 0);
9330 cond1 = XEXP (XEXP (x, 1), 0);
9332 if (COMPARISON_P (cond0)
9333 && COMPARISON_P (cond1)
9334 && SCALAR_INT_MODE_P (mode)
9335 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9336 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9337 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9338 || ((swap_condition (GET_CODE (cond0))
9339 == reversed_comparison_code (cond1, NULL))
9340 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9341 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9342 && ! side_effects_p (x))
9344 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9345 *pfalse = simplify_gen_binary (MULT, mode,
9346 (code == MINUS
9347 ? simplify_gen_unary (NEG, mode,
9348 op1, mode)
9349 : op1),
9350 const_true_rtx);
9351 return cond0;
9355 /* Similarly for MULT, AND and UMIN, except that for these the result
9356 is always zero. */
9357 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9358 && (code == MULT || code == AND || code == UMIN)
9359 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9361 cond0 = XEXP (XEXP (x, 0), 0);
9362 cond1 = XEXP (XEXP (x, 1), 0);
9364 if (COMPARISON_P (cond0)
9365 && COMPARISON_P (cond1)
9366 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9367 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9368 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9369 || ((swap_condition (GET_CODE (cond0))
9370 == reversed_comparison_code (cond1, NULL))
9371 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9372 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9373 && ! side_effects_p (x))
9375 *ptrue = *pfalse = const0_rtx;
9376 return cond0;
9381 else if (code == IF_THEN_ELSE)
9383 /* If we have IF_THEN_ELSE already, extract the condition and
9384 canonicalize it if it is NE or EQ. */
9385 cond0 = XEXP (x, 0);
9386 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9387 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9388 return XEXP (cond0, 0);
9389 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9391 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9392 return XEXP (cond0, 0);
9394 else
9395 return cond0;
9398 /* If X is a SUBREG, we can narrow both the true and false values
9399 if the inner expression, if there is a condition. */
9400 else if (code == SUBREG
9401 && (cond0 = if_then_else_cond (SUBREG_REG (x), &true0,
9402 &false0)) != 0)
9404 true0 = simplify_gen_subreg (mode, true0,
9405 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9406 false0 = simplify_gen_subreg (mode, false0,
9407 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9408 if (true0 && false0)
9410 *ptrue = true0;
9411 *pfalse = false0;
9412 return cond0;
9416 /* If X is a constant, this isn't special and will cause confusions
9417 if we treat it as such. Likewise if it is equivalent to a constant. */
9418 else if (CONSTANT_P (x)
9419 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9422 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9423 will be least confusing to the rest of the compiler. */
9424 else if (mode == BImode)
9426 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9427 return x;
9430 /* If X is known to be either 0 or -1, those are the true and
9431 false values when testing X. */
9432 else if (x == constm1_rtx || x == const0_rtx
9433 || (is_a <scalar_int_mode> (mode, &int_mode)
9434 && (num_sign_bit_copies (x, int_mode)
9435 == GET_MODE_PRECISION (int_mode))))
9437 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9438 return x;
9441 /* Likewise for 0 or a single bit. */
9442 else if (HWI_COMPUTABLE_MODE_P (mode)
9443 && pow2p_hwi (nz = nonzero_bits (x, mode)))
9445 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9446 return x;
9449 /* Otherwise fail; show no condition with true and false values the same. */
9450 *ptrue = *pfalse = x;
9451 return 0;
9454 /* Return the value of expression X given the fact that condition COND
9455 is known to be true when applied to REG as its first operand and VAL
9456 as its second. X is known to not be shared and so can be modified in
9457 place.
9459 We only handle the simplest cases, and specifically those cases that
9460 arise with IF_THEN_ELSE expressions. */
9462 static rtx
9463 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9465 enum rtx_code code = GET_CODE (x);
9466 const char *fmt;
9467 int i, j;
9469 if (side_effects_p (x))
9470 return x;
9472 /* If either operand of the condition is a floating point value,
9473 then we have to avoid collapsing an EQ comparison. */
9474 if (cond == EQ
9475 && rtx_equal_p (x, reg)
9476 && ! FLOAT_MODE_P (GET_MODE (x))
9477 && ! FLOAT_MODE_P (GET_MODE (val)))
9478 return val;
9480 if (cond == UNEQ && rtx_equal_p (x, reg))
9481 return val;
9483 /* If X is (abs REG) and we know something about REG's relationship
9484 with zero, we may be able to simplify this. */
9486 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9487 switch (cond)
9489 case GE: case GT: case EQ:
9490 return XEXP (x, 0);
9491 case LT: case LE:
9492 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9493 XEXP (x, 0),
9494 GET_MODE (XEXP (x, 0)));
9495 default:
9496 break;
9499 /* The only other cases we handle are MIN, MAX, and comparisons if the
9500 operands are the same as REG and VAL. */
9502 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9504 if (rtx_equal_p (XEXP (x, 0), val))
9506 std::swap (val, reg);
9507 cond = swap_condition (cond);
9510 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9512 if (COMPARISON_P (x))
9514 if (comparison_dominates_p (cond, code))
9515 return VECTOR_MODE_P (GET_MODE (x)) ? x : const_true_rtx;
9517 code = reversed_comparison_code (x, NULL);
9518 if (code != UNKNOWN
9519 && comparison_dominates_p (cond, code))
9520 return CONST0_RTX (GET_MODE (x));
9521 else
9522 return x;
9524 else if (code == SMAX || code == SMIN
9525 || code == UMIN || code == UMAX)
9527 int unsignedp = (code == UMIN || code == UMAX);
9529 /* Do not reverse the condition when it is NE or EQ.
9530 This is because we cannot conclude anything about
9531 the value of 'SMAX (x, y)' when x is not equal to y,
9532 but we can when x equals y. */
9533 if ((code == SMAX || code == UMAX)
9534 && ! (cond == EQ || cond == NE))
9535 cond = reverse_condition (cond);
9537 switch (cond)
9539 case GE: case GT:
9540 return unsignedp ? x : XEXP (x, 1);
9541 case LE: case LT:
9542 return unsignedp ? x : XEXP (x, 0);
9543 case GEU: case GTU:
9544 return unsignedp ? XEXP (x, 1) : x;
9545 case LEU: case LTU:
9546 return unsignedp ? XEXP (x, 0) : x;
9547 default:
9548 break;
9553 else if (code == SUBREG)
9555 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9556 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9558 if (SUBREG_REG (x) != r)
9560 /* We must simplify subreg here, before we lose track of the
9561 original inner_mode. */
9562 new_rtx = simplify_subreg (GET_MODE (x), r,
9563 inner_mode, SUBREG_BYTE (x));
9564 if (new_rtx)
9565 return new_rtx;
9566 else
9567 SUBST (SUBREG_REG (x), r);
9570 return x;
9572 /* We don't have to handle SIGN_EXTEND here, because even in the
9573 case of replacing something with a modeless CONST_INT, a
9574 CONST_INT is already (supposed to be) a valid sign extension for
9575 its narrower mode, which implies it's already properly
9576 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9577 story is different. */
9578 else if (code == ZERO_EXTEND)
9580 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9581 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9583 if (XEXP (x, 0) != r)
9585 /* We must simplify the zero_extend here, before we lose
9586 track of the original inner_mode. */
9587 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9588 r, inner_mode);
9589 if (new_rtx)
9590 return new_rtx;
9591 else
9592 SUBST (XEXP (x, 0), r);
9595 return x;
9598 fmt = GET_RTX_FORMAT (code);
9599 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9601 if (fmt[i] == 'e')
9602 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9603 else if (fmt[i] == 'E')
9604 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9605 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9606 cond, reg, val));
9609 return x;
9612 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9613 assignment as a field assignment. */
9615 static bool
9616 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9618 if (widen_x && GET_MODE (x) != GET_MODE (y))
9620 if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9621 return false;
9622 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9623 return false;
9624 x = adjust_address_nv (x, GET_MODE (y),
9625 byte_lowpart_offset (GET_MODE (y),
9626 GET_MODE (x)));
9629 if (x == y || rtx_equal_p (x, y))
9630 return true;
9632 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9633 return false;
9635 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9636 Note that all SUBREGs of MEM are paradoxical; otherwise they
9637 would have been rewritten. */
9638 if (MEM_P (x) && GET_CODE (y) == SUBREG
9639 && MEM_P (SUBREG_REG (y))
9640 && rtx_equal_p (SUBREG_REG (y),
9641 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9642 return true;
9644 if (MEM_P (y) && GET_CODE (x) == SUBREG
9645 && MEM_P (SUBREG_REG (x))
9646 && rtx_equal_p (SUBREG_REG (x),
9647 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9648 return true;
9650 /* We used to see if get_last_value of X and Y were the same but that's
9651 not correct. In one direction, we'll cause the assignment to have
9652 the wrong destination and in the case, we'll import a register into this
9653 insn that might have already have been dead. So fail if none of the
9654 above cases are true. */
9655 return false;
9658 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9659 Return that assignment if so.
9661 We only handle the most common cases. */
9663 static rtx
9664 make_field_assignment (rtx x)
9666 rtx dest = SET_DEST (x);
9667 rtx src = SET_SRC (x);
9668 rtx assign;
9669 rtx rhs, lhs;
9670 HOST_WIDE_INT c1;
9671 HOST_WIDE_INT pos;
9672 unsigned HOST_WIDE_INT len;
9673 rtx other;
9675 /* All the rules in this function are specific to scalar integers. */
9676 scalar_int_mode mode;
9677 if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9678 return x;
9680 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9681 a clear of a one-bit field. We will have changed it to
9682 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9683 for a SUBREG. */
9685 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9686 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9687 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9688 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9690 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9691 1, true, true, false);
9692 if (assign != 0)
9693 return gen_rtx_SET (assign, const0_rtx);
9694 return x;
9697 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9698 && subreg_lowpart_p (XEXP (src, 0))
9699 && partial_subreg_p (XEXP (src, 0))
9700 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9701 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9702 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9703 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9705 assign = make_extraction (VOIDmode, dest, 0,
9706 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9707 1, true, true, false);
9708 if (assign != 0)
9709 return gen_rtx_SET (assign, const0_rtx);
9710 return x;
9713 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9714 one-bit field. */
9715 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9716 && XEXP (XEXP (src, 0), 0) == const1_rtx
9717 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9719 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9720 1, true, true, false);
9721 if (assign != 0)
9722 return gen_rtx_SET (assign, const1_rtx);
9723 return x;
9726 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9727 SRC is an AND with all bits of that field set, then we can discard
9728 the AND. */
9729 if (GET_CODE (dest) == ZERO_EXTRACT
9730 && CONST_INT_P (XEXP (dest, 1))
9731 && GET_CODE (src) == AND
9732 && CONST_INT_P (XEXP (src, 1)))
9734 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9735 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9736 unsigned HOST_WIDE_INT ze_mask;
9738 if (width >= HOST_BITS_PER_WIDE_INT)
9739 ze_mask = -1;
9740 else
9741 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9743 /* Complete overlap. We can remove the source AND. */
9744 if ((and_mask & ze_mask) == ze_mask)
9745 return gen_rtx_SET (dest, XEXP (src, 0));
9747 /* Partial overlap. We can reduce the source AND. */
9748 if ((and_mask & ze_mask) != and_mask)
9750 src = gen_rtx_AND (mode, XEXP (src, 0),
9751 gen_int_mode (and_mask & ze_mask, mode));
9752 return gen_rtx_SET (dest, src);
9756 /* The other case we handle is assignments into a constant-position
9757 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9758 a mask that has all one bits except for a group of zero bits and
9759 OTHER is known to have zeros where C1 has ones, this is such an
9760 assignment. Compute the position and length from C1. Shift OTHER
9761 to the appropriate position, force it to the required mode, and
9762 make the extraction. Check for the AND in both operands. */
9764 /* One or more SUBREGs might obscure the constant-position field
9765 assignment. The first one we are likely to encounter is an outer
9766 narrowing SUBREG, which we can just strip for the purposes of
9767 identifying the constant-field assignment. */
9768 scalar_int_mode src_mode = mode;
9769 if (GET_CODE (src) == SUBREG
9770 && subreg_lowpart_p (src)
9771 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9772 src = SUBREG_REG (src);
9774 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9775 return x;
9777 rhs = expand_compound_operation (XEXP (src, 0));
9778 lhs = expand_compound_operation (XEXP (src, 1));
9780 if (GET_CODE (rhs) == AND
9781 && CONST_INT_P (XEXP (rhs, 1))
9782 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9783 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9784 /* The second SUBREG that might get in the way is a paradoxical
9785 SUBREG around the first operand of the AND. We want to
9786 pretend the operand is as wide as the destination here. We
9787 do this by adjusting the MEM to wider mode for the sole
9788 purpose of the call to rtx_equal_for_field_assignment_p. Also
9789 note this trick only works for MEMs. */
9790 else if (GET_CODE (rhs) == AND
9791 && paradoxical_subreg_p (XEXP (rhs, 0))
9792 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9793 && CONST_INT_P (XEXP (rhs, 1))
9794 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9795 dest, true))
9796 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9797 else if (GET_CODE (lhs) == AND
9798 && CONST_INT_P (XEXP (lhs, 1))
9799 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9800 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9801 /* The second SUBREG that might get in the way is a paradoxical
9802 SUBREG around the first operand of the AND. We want to
9803 pretend the operand is as wide as the destination here. We
9804 do this by adjusting the MEM to wider mode for the sole
9805 purpose of the call to rtx_equal_for_field_assignment_p. Also
9806 note this trick only works for MEMs. */
9807 else if (GET_CODE (lhs) == AND
9808 && paradoxical_subreg_p (XEXP (lhs, 0))
9809 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9810 && CONST_INT_P (XEXP (lhs, 1))
9811 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9812 dest, true))
9813 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9814 else
9815 return x;
9817 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9818 if (pos < 0
9819 || pos + len > GET_MODE_PRECISION (mode)
9820 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9821 || (c1 & nonzero_bits (other, mode)) != 0)
9822 return x;
9824 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len,
9825 true, true, false);
9826 if (assign == 0)
9827 return x;
9829 /* The mode to use for the source is the mode of the assignment, or of
9830 what is inside a possible STRICT_LOW_PART. */
9831 machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9832 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9834 /* Shift OTHER right POS places and make it the source, restricting it
9835 to the proper length and mode. */
9837 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9838 src_mode, other, pos),
9839 dest);
9840 src = force_to_mode (src, new_mode,
9841 len >= HOST_BITS_PER_WIDE_INT
9842 ? HOST_WIDE_INT_M1U
9843 : (HOST_WIDE_INT_1U << len) - 1, false);
9845 /* If SRC is masked by an AND that does not make a difference in
9846 the value being stored, strip it. */
9847 if (GET_CODE (assign) == ZERO_EXTRACT
9848 && CONST_INT_P (XEXP (assign, 1))
9849 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9850 && GET_CODE (src) == AND
9851 && CONST_INT_P (XEXP (src, 1))
9852 && UINTVAL (XEXP (src, 1))
9853 == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9854 src = XEXP (src, 0);
9856 return gen_rtx_SET (assign, src);
9859 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9860 if so. */
9862 static rtx
9863 apply_distributive_law (rtx x)
9865 enum rtx_code code = GET_CODE (x);
9866 enum rtx_code inner_code;
9867 rtx lhs, rhs, other;
9868 rtx tem;
9870 /* Distributivity is not true for floating point as it can change the
9871 value. So we don't do it unless -funsafe-math-optimizations. */
9872 if (FLOAT_MODE_P (GET_MODE (x))
9873 && ! flag_unsafe_math_optimizations)
9874 return x;
9876 /* The outer operation can only be one of the following: */
9877 if (code != IOR && code != AND && code != XOR
9878 && code != PLUS && code != MINUS)
9879 return x;
9881 lhs = XEXP (x, 0);
9882 rhs = XEXP (x, 1);
9884 /* If either operand is a primitive we can't do anything, so get out
9885 fast. */
9886 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9887 return x;
9889 lhs = expand_compound_operation (lhs);
9890 rhs = expand_compound_operation (rhs);
9891 inner_code = GET_CODE (lhs);
9892 if (inner_code != GET_CODE (rhs))
9893 return x;
9895 /* See if the inner and outer operations distribute. */
9896 switch (inner_code)
9898 case LSHIFTRT:
9899 case ASHIFTRT:
9900 case AND:
9901 case IOR:
9902 /* These all distribute except over PLUS. */
9903 if (code == PLUS || code == MINUS)
9904 return x;
9905 break;
9907 case MULT:
9908 if (code != PLUS && code != MINUS)
9909 return x;
9910 break;
9912 case ASHIFT:
9913 /* This is also a multiply, so it distributes over everything. */
9914 break;
9916 /* This used to handle SUBREG, but this turned out to be counter-
9917 productive, since (subreg (op ...)) usually is not handled by
9918 insn patterns, and this "optimization" therefore transformed
9919 recognizable patterns into unrecognizable ones. Therefore the
9920 SUBREG case was removed from here.
9922 It is possible that distributing SUBREG over arithmetic operations
9923 leads to an intermediate result than can then be optimized further,
9924 e.g. by moving the outer SUBREG to the other side of a SET as done
9925 in simplify_set. This seems to have been the original intent of
9926 handling SUBREGs here.
9928 However, with current GCC this does not appear to actually happen,
9929 at least on major platforms. If some case is found where removing
9930 the SUBREG case here prevents follow-on optimizations, distributing
9931 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9933 default:
9934 return x;
9937 /* Set LHS and RHS to the inner operands (A and B in the example
9938 above) and set OTHER to the common operand (C in the example).
9939 There is only one way to do this unless the inner operation is
9940 commutative. */
9941 if (COMMUTATIVE_ARITH_P (lhs)
9942 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9943 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9944 else if (COMMUTATIVE_ARITH_P (lhs)
9945 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9946 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9947 else if (COMMUTATIVE_ARITH_P (lhs)
9948 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9949 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9950 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9951 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9952 else
9953 return x;
9955 /* Form the new inner operation, seeing if it simplifies first. */
9956 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9958 /* There is one exception to the general way of distributing:
9959 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9960 if (code == XOR && inner_code == IOR)
9962 inner_code = AND;
9963 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9966 /* We may be able to continuing distributing the result, so call
9967 ourselves recursively on the inner operation before forming the
9968 outer operation, which we return. */
9969 return simplify_gen_binary (inner_code, GET_MODE (x),
9970 apply_distributive_law (tem), other);
9973 /* See if X is of the form (* (+ A B) C), and if so convert to
9974 (+ (* A C) (* B C)) and try to simplify.
9976 Most of the time, this results in no change. However, if some of
9977 the operands are the same or inverses of each other, simplifications
9978 will result.
9980 For example, (and (ior A B) (not B)) can occur as the result of
9981 expanding a bit field assignment. When we apply the distributive
9982 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9983 which then simplifies to (and (A (not B))).
9985 Note that no checks happen on the validity of applying the inverse
9986 distributive law. This is pointless since we can do it in the
9987 few places where this routine is called.
9989 N is the index of the term that is decomposed (the arithmetic operation,
9990 i.e. (+ A B) in the first example above). !N is the index of the term that
9991 is distributed, i.e. of C in the first example above. */
9992 static rtx
9993 distribute_and_simplify_rtx (rtx x, int n)
9995 machine_mode mode;
9996 enum rtx_code outer_code, inner_code;
9997 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9999 /* Distributivity is not true for floating point as it can change the
10000 value. So we don't do it unless -funsafe-math-optimizations. */
10001 if (FLOAT_MODE_P (GET_MODE (x))
10002 && ! flag_unsafe_math_optimizations)
10003 return NULL_RTX;
10005 decomposed = XEXP (x, n);
10006 if (!ARITHMETIC_P (decomposed))
10007 return NULL_RTX;
10009 mode = GET_MODE (x);
10010 outer_code = GET_CODE (x);
10011 distributed = XEXP (x, !n);
10013 inner_code = GET_CODE (decomposed);
10014 inner_op0 = XEXP (decomposed, 0);
10015 inner_op1 = XEXP (decomposed, 1);
10017 /* Special case (and (xor B C) (not A)), which is equivalent to
10018 (xor (ior A B) (ior A C)) */
10019 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
10021 distributed = XEXP (distributed, 0);
10022 outer_code = IOR;
10025 if (n == 0)
10027 /* Distribute the second term. */
10028 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
10029 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
10031 else
10033 /* Distribute the first term. */
10034 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
10035 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
10038 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
10039 new_op0, new_op1));
10040 if (GET_CODE (tmp) != outer_code
10041 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
10042 < set_src_cost (x, mode, optimize_this_for_speed_p)))
10043 return tmp;
10045 return NULL_RTX;
10048 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
10049 in MODE. Return an equivalent form, if different from (and VAROP
10050 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
10052 static rtx
10053 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
10054 unsigned HOST_WIDE_INT constop)
10056 unsigned HOST_WIDE_INT nonzero;
10057 unsigned HOST_WIDE_INT orig_constop;
10058 rtx orig_varop;
10059 int i;
10061 orig_varop = varop;
10062 orig_constop = constop;
10063 if (GET_CODE (varop) == CLOBBER)
10064 return NULL_RTX;
10066 /* Simplify VAROP knowing that we will be only looking at some of the
10067 bits in it.
10069 Note by passing in CONSTOP, we guarantee that the bits not set in
10070 CONSTOP are not significant and will never be examined. We must
10071 ensure that is the case by explicitly masking out those bits
10072 before returning. */
10073 varop = force_to_mode (varop, mode, constop, false);
10075 /* If VAROP is a CLOBBER, we will fail so return it. */
10076 if (GET_CODE (varop) == CLOBBER)
10077 return varop;
10079 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
10080 to VAROP and return the new constant. */
10081 if (CONST_INT_P (varop))
10082 return gen_int_mode (INTVAL (varop) & constop, mode);
10084 /* See what bits may be nonzero in VAROP. Unlike the general case of
10085 a call to nonzero_bits, here we don't care about bits outside
10086 MODE unless WORD_REGISTER_OPERATIONS is true. */
10088 scalar_int_mode tmode = mode;
10089 if (WORD_REGISTER_OPERATIONS && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
10090 tmode = word_mode;
10091 nonzero = nonzero_bits (varop, tmode) & GET_MODE_MASK (tmode);
10093 /* Turn off all bits in the constant that are known to already be zero.
10094 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
10095 which is tested below. */
10097 constop &= nonzero;
10099 /* If we don't have any bits left, return zero. */
10100 if (constop == 0 && !side_effects_p (varop))
10101 return const0_rtx;
10103 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
10104 a power of two, we can replace this with an ASHIFT. */
10105 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), tmode) == 1
10106 && (i = exact_log2 (constop)) >= 0)
10107 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
10109 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
10110 or XOR, then try to apply the distributive law. This may eliminate
10111 operations if either branch can be simplified because of the AND.
10112 It may also make some cases more complex, but those cases probably
10113 won't match a pattern either with or without this. */
10115 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10117 scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10118 return
10119 gen_lowpart
10120 (mode,
10121 apply_distributive_law
10122 (simplify_gen_binary (GET_CODE (varop), varop_mode,
10123 simplify_and_const_int (NULL_RTX, varop_mode,
10124 XEXP (varop, 0),
10125 constop),
10126 simplify_and_const_int (NULL_RTX, varop_mode,
10127 XEXP (varop, 1),
10128 constop))));
10131 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10132 the AND and see if one of the operands simplifies to zero. If so, we
10133 may eliminate it. */
10135 if (GET_CODE (varop) == PLUS
10136 && pow2p_hwi (constop + 1))
10138 rtx o0, o1;
10140 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10141 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10142 if (o0 == const0_rtx)
10143 return o1;
10144 if (o1 == const0_rtx)
10145 return o0;
10148 /* Make a SUBREG if necessary. If we can't make it, fail. */
10149 varop = gen_lowpart (mode, varop);
10150 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10151 return NULL_RTX;
10153 /* If we are only masking insignificant bits, return VAROP. */
10154 if (constop == nonzero)
10155 return varop;
10157 if (varop == orig_varop && constop == orig_constop)
10158 return NULL_RTX;
10160 /* Otherwise, return an AND. */
10161 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10165 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10166 in MODE.
10168 Return an equivalent form, if different from X. Otherwise, return X. If
10169 X is zero, we are to always construct the equivalent form. */
10171 static rtx
10172 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10173 unsigned HOST_WIDE_INT constop)
10175 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10176 if (tem)
10177 return tem;
10179 if (!x)
10180 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10181 gen_int_mode (constop, mode));
10182 if (GET_MODE (x) != mode)
10183 x = gen_lowpart (mode, x);
10184 return x;
10187 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10188 We don't care about bits outside of those defined in MODE.
10189 We DO care about all the bits in MODE, even if XMODE is smaller than MODE.
10191 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10192 a shift, AND, or zero_extract, we can do better. */
10194 static rtx
10195 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10196 scalar_int_mode mode,
10197 unsigned HOST_WIDE_INT *nonzero)
10199 rtx tem;
10200 reg_stat_type *rsp;
10202 /* If X is a register whose nonzero bits value is current, use it.
10203 Otherwise, if X is a register whose value we can find, use that
10204 value. Otherwise, use the previously-computed global nonzero bits
10205 for this register. */
10207 rsp = &reg_stat[REGNO (x)];
10208 if (rsp->last_set_value != 0
10209 && (rsp->last_set_mode == mode
10210 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10211 && GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10212 && GET_MODE_CLASS (mode) == MODE_INT))
10213 && ((rsp->last_set_label >= label_tick_ebb_start
10214 && rsp->last_set_label < label_tick)
10215 || (rsp->last_set_label == label_tick
10216 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10217 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10218 && REGNO (x) < reg_n_sets_max
10219 && REG_N_SETS (REGNO (x)) == 1
10220 && !REGNO_REG_SET_P
10221 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10222 REGNO (x)))))
10224 /* Note that, even if the precision of last_set_mode is lower than that
10225 of mode, record_value_for_reg invoked nonzero_bits on the register
10226 with nonzero_bits_mode (because last_set_mode is necessarily integral
10227 and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10228 are all valid, hence in mode too since nonzero_bits_mode is defined
10229 to the largest HWI_COMPUTABLE_MODE_P mode. */
10230 *nonzero &= rsp->last_set_nonzero_bits;
10231 return NULL;
10234 tem = get_last_value (x);
10235 if (tem)
10237 if (SHORT_IMMEDIATES_SIGN_EXTEND)
10238 tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10240 return tem;
10243 if (nonzero_sign_valid && rsp->nonzero_bits)
10245 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10247 if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10248 /* We don't know anything about the upper bits. */
10249 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10251 *nonzero &= mask;
10254 return NULL;
10257 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10258 end of X that are known to be equal to the sign bit. X will be used
10259 in mode MODE; the returned value will always be between 1 and the
10260 number of bits in MODE. */
10262 static rtx
10263 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10264 scalar_int_mode mode,
10265 unsigned int *result)
10267 rtx tem;
10268 reg_stat_type *rsp;
10270 rsp = &reg_stat[REGNO (x)];
10271 if (rsp->last_set_value != 0
10272 && rsp->last_set_mode == mode
10273 && ((rsp->last_set_label >= label_tick_ebb_start
10274 && rsp->last_set_label < label_tick)
10275 || (rsp->last_set_label == label_tick
10276 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10277 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10278 && REGNO (x) < reg_n_sets_max
10279 && REG_N_SETS (REGNO (x)) == 1
10280 && !REGNO_REG_SET_P
10281 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10282 REGNO (x)))))
10284 *result = rsp->last_set_sign_bit_copies;
10285 return NULL;
10288 tem = get_last_value (x);
10289 if (tem != 0)
10290 return tem;
10292 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10293 && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10294 *result = rsp->sign_bit_copies;
10296 return NULL;
10299 /* Return the number of "extended" bits there are in X, when interpreted
10300 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
10301 unsigned quantities, this is the number of high-order zero bits.
10302 For signed quantities, this is the number of copies of the sign bit
10303 minus 1. In both case, this function returns the number of "spare"
10304 bits. For example, if two quantities for which this function returns
10305 at least 1 are added, the addition is known not to overflow.
10307 This function will always return 0 unless called during combine, which
10308 implies that it must be called from a define_split. */
10310 unsigned int
10311 extended_count (const_rtx x, machine_mode mode, bool unsignedp)
10313 if (nonzero_sign_valid == 0)
10314 return 0;
10316 scalar_int_mode int_mode;
10317 return (unsignedp
10318 ? (is_a <scalar_int_mode> (mode, &int_mode)
10319 && HWI_COMPUTABLE_MODE_P (int_mode)
10320 ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10321 - floor_log2 (nonzero_bits (x, int_mode)))
10322 : 0)
10323 : num_sign_bit_copies (x, mode) - 1);
10326 /* This function is called from `simplify_shift_const' to merge two
10327 outer operations. Specifically, we have already found that we need
10328 to perform operation *POP0 with constant *PCONST0 at the outermost
10329 position. We would now like to also perform OP1 with constant CONST1
10330 (with *POP0 being done last).
10332 Return true if we can do the operation and update *POP0 and *PCONST0 with
10333 the resulting operation. *PCOMP_P is set to true if we would need to
10334 complement the innermost operand, otherwise it is unchanged.
10336 MODE is the mode in which the operation will be done. No bits outside
10337 the width of this mode matter. It is assumed that the width of this mode
10338 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10340 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
10341 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
10342 result is simply *PCONST0.
10344 If the resulting operation cannot be expressed as one operation, we
10345 return false and do not change *POP0, *PCONST0, and *PCOMP_P. */
10347 static bool
10348 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0,
10349 enum rtx_code op1, HOST_WIDE_INT const1,
10350 machine_mode mode, bool *pcomp_p)
10352 enum rtx_code op0 = *pop0;
10353 HOST_WIDE_INT const0 = *pconst0;
10355 const0 &= GET_MODE_MASK (mode);
10356 const1 &= GET_MODE_MASK (mode);
10358 /* If OP0 is an AND, clear unimportant bits in CONST1. */
10359 if (op0 == AND)
10360 const1 &= const0;
10362 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
10363 if OP0 is SET. */
10365 if (op1 == UNKNOWN || op0 == SET)
10366 return true;
10368 else if (op0 == UNKNOWN)
10369 op0 = op1, const0 = const1;
10371 else if (op0 == op1)
10373 switch (op0)
10375 case AND:
10376 const0 &= const1;
10377 break;
10378 case IOR:
10379 const0 |= const1;
10380 break;
10381 case XOR:
10382 const0 ^= const1;
10383 break;
10384 case PLUS:
10385 const0 += const1;
10386 break;
10387 case NEG:
10388 op0 = UNKNOWN;
10389 break;
10390 default:
10391 break;
10395 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
10396 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10397 return false;
10399 /* If the two constants aren't the same, we can't do anything. The
10400 remaining six cases can all be done. */
10401 else if (const0 != const1)
10402 return false;
10404 else
10405 switch (op0)
10407 case IOR:
10408 if (op1 == AND)
10409 /* (a & b) | b == b */
10410 op0 = SET;
10411 else /* op1 == XOR */
10412 /* (a ^ b) | b == a | b */
10414 break;
10416 case XOR:
10417 if (op1 == AND)
10418 /* (a & b) ^ b == (~a) & b */
10419 op0 = AND, *pcomp_p = true;
10420 else /* op1 == IOR */
10421 /* (a | b) ^ b == a & ~b */
10422 op0 = AND, const0 = ~const0;
10423 break;
10425 case AND:
10426 if (op1 == IOR)
10427 /* (a | b) & b == b */
10428 op0 = SET;
10429 else /* op1 == XOR */
10430 /* (a ^ b) & b) == (~a) & b */
10431 *pcomp_p = true;
10432 break;
10433 default:
10434 break;
10437 /* Check for NO-OP cases. */
10438 const0 &= GET_MODE_MASK (mode);
10439 if (const0 == 0
10440 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10441 op0 = UNKNOWN;
10442 else if (const0 == 0 && op0 == AND)
10443 op0 = SET;
10444 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10445 && op0 == AND)
10446 op0 = UNKNOWN;
10448 *pop0 = op0;
10450 /* ??? Slightly redundant with the above mask, but not entirely.
10451 Moving this above means we'd have to sign-extend the mode mask
10452 for the final test. */
10453 if (op0 != UNKNOWN && op0 != NEG)
10454 *pconst0 = trunc_int_for_mode (const0, mode);
10456 return true;
10459 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10460 the shift in. The original shift operation CODE is performed on OP in
10461 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10462 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10463 result of the shift is subject to operation OUTER_CODE with operand
10464 OUTER_CONST. */
10466 static scalar_int_mode
10467 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10468 scalar_int_mode orig_mode, scalar_int_mode mode,
10469 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10471 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10473 /* In general we can't perform in wider mode for right shift and rotate. */
10474 switch (code)
10476 case ASHIFTRT:
10477 /* We can still widen if the bits brought in from the left are identical
10478 to the sign bit of ORIG_MODE. */
10479 if (num_sign_bit_copies (op, mode)
10480 > (unsigned) (GET_MODE_PRECISION (mode)
10481 - GET_MODE_PRECISION (orig_mode)))
10482 return mode;
10483 return orig_mode;
10485 case LSHIFTRT:
10486 /* Similarly here but with zero bits. */
10487 if (HWI_COMPUTABLE_MODE_P (mode)
10488 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10489 return mode;
10491 /* We can also widen if the bits brought in will be masked off. This
10492 operation is performed in ORIG_MODE. */
10493 if (outer_code == AND)
10495 int care_bits = low_bitmask_len (orig_mode, outer_const);
10497 if (care_bits >= 0
10498 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10499 return mode;
10501 /* fall through */
10503 case ROTATE:
10504 return orig_mode;
10506 case ROTATERT:
10507 gcc_unreachable ();
10509 default:
10510 return mode;
10514 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10515 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10516 if we cannot simplify it. Otherwise, return a simplified value.
10518 The shift is normally computed in the widest mode we find in VAROP, as
10519 long as it isn't a different number of words than RESULT_MODE. Exceptions
10520 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10522 static rtx
10523 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10524 rtx varop, int orig_count)
10526 enum rtx_code orig_code = code;
10527 rtx orig_varop = varop;
10528 int count, log2;
10529 machine_mode mode = result_mode;
10530 machine_mode shift_mode;
10531 scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10532 /* We form (outer_op (code varop count) (outer_const)). */
10533 enum rtx_code outer_op = UNKNOWN;
10534 HOST_WIDE_INT outer_const = 0;
10535 bool complement_p = false;
10536 rtx new_rtx, x;
10538 /* Make sure and truncate the "natural" shift on the way in. We don't
10539 want to do this inside the loop as it makes it more difficult to
10540 combine shifts. */
10541 if (SHIFT_COUNT_TRUNCATED)
10542 orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10544 /* If we were given an invalid count, don't do anything except exactly
10545 what was requested. */
10547 if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10548 return NULL_RTX;
10550 count = orig_count;
10552 /* Unless one of the branches of the `if' in this loop does a `continue',
10553 we will `break' the loop after the `if'. */
10555 while (count != 0)
10557 /* If we have an operand of (clobber (const_int 0)), fail. */
10558 if (GET_CODE (varop) == CLOBBER)
10559 return NULL_RTX;
10561 /* Convert ROTATERT to ROTATE. */
10562 if (code == ROTATERT)
10564 unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10565 code = ROTATE;
10566 count = bitsize - count;
10569 shift_mode = result_mode;
10570 if (shift_mode != mode)
10572 /* We only change the modes of scalar shifts. */
10573 int_mode = as_a <scalar_int_mode> (mode);
10574 int_result_mode = as_a <scalar_int_mode> (result_mode);
10575 shift_mode = try_widen_shift_mode (code, varop, count,
10576 int_result_mode, int_mode,
10577 outer_op, outer_const);
10580 scalar_int_mode shift_unit_mode
10581 = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10583 /* Handle cases where the count is greater than the size of the mode
10584 minus 1. For ASHIFT, use the size minus one as the count (this can
10585 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10586 take the count modulo the size. For other shifts, the result is
10587 zero.
10589 Since these shifts are being produced by the compiler by combining
10590 multiple operations, each of which are defined, we know what the
10591 result is supposed to be. */
10593 if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10595 if (code == ASHIFTRT)
10596 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10597 else if (code == ROTATE || code == ROTATERT)
10598 count %= GET_MODE_PRECISION (shift_unit_mode);
10599 else
10601 /* We can't simply return zero because there may be an
10602 outer op. */
10603 varop = const0_rtx;
10604 count = 0;
10605 break;
10609 /* If we discovered we had to complement VAROP, leave. Making a NOT
10610 here would cause an infinite loop. */
10611 if (complement_p)
10612 break;
10614 if (shift_mode == shift_unit_mode)
10616 /* An arithmetic right shift of a quantity known to be -1 or 0
10617 is a no-op. */
10618 if (code == ASHIFTRT
10619 && (num_sign_bit_copies (varop, shift_unit_mode)
10620 == GET_MODE_PRECISION (shift_unit_mode)))
10622 count = 0;
10623 break;
10626 /* If we are doing an arithmetic right shift and discarding all but
10627 the sign bit copies, this is equivalent to doing a shift by the
10628 bitsize minus one. Convert it into that shift because it will
10629 often allow other simplifications. */
10631 if (code == ASHIFTRT
10632 && (count + num_sign_bit_copies (varop, shift_unit_mode)
10633 >= GET_MODE_PRECISION (shift_unit_mode)))
10634 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10636 /* We simplify the tests below and elsewhere by converting
10637 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10638 `make_compound_operation' will convert it to an ASHIFTRT for
10639 those machines (such as VAX) that don't have an LSHIFTRT. */
10640 if (code == ASHIFTRT
10641 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10642 && val_signbit_known_clear_p (shift_unit_mode,
10643 nonzero_bits (varop,
10644 shift_unit_mode)))
10645 code = LSHIFTRT;
10647 if (((code == LSHIFTRT
10648 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10649 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10650 || (code == ASHIFT
10651 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10652 && !((nonzero_bits (varop, shift_unit_mode) << count)
10653 & GET_MODE_MASK (shift_unit_mode))))
10654 && !side_effects_p (varop))
10655 varop = const0_rtx;
10658 switch (GET_CODE (varop))
10660 case SIGN_EXTEND:
10661 case ZERO_EXTEND:
10662 case SIGN_EXTRACT:
10663 case ZERO_EXTRACT:
10664 new_rtx = expand_compound_operation (varop);
10665 if (new_rtx != varop)
10667 varop = new_rtx;
10668 continue;
10670 break;
10672 case MEM:
10673 /* The following rules apply only to scalars. */
10674 if (shift_mode != shift_unit_mode)
10675 break;
10676 int_mode = as_a <scalar_int_mode> (mode);
10678 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10679 minus the width of a smaller mode, we can do this with a
10680 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10681 if ((code == ASHIFTRT || code == LSHIFTRT)
10682 && ! mode_dependent_address_p (XEXP (varop, 0),
10683 MEM_ADDR_SPACE (varop))
10684 && ! MEM_VOLATILE_P (varop)
10685 && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10686 .exists (&tmode)))
10688 new_rtx = adjust_address_nv (varop, tmode,
10689 BYTES_BIG_ENDIAN ? 0
10690 : count / BITS_PER_UNIT);
10692 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10693 : ZERO_EXTEND, int_mode, new_rtx);
10694 count = 0;
10695 continue;
10697 break;
10699 case SUBREG:
10700 /* The following rules apply only to scalars. */
10701 if (shift_mode != shift_unit_mode)
10702 break;
10703 int_mode = as_a <scalar_int_mode> (mode);
10704 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10706 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10707 the same number of words as what we've seen so far. Then store
10708 the widest mode in MODE. */
10709 if (subreg_lowpart_p (varop)
10710 && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10711 && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10712 && (CEIL (GET_MODE_SIZE (inner_mode), UNITS_PER_WORD)
10713 == CEIL (GET_MODE_SIZE (int_mode), UNITS_PER_WORD))
10714 && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10716 varop = SUBREG_REG (varop);
10717 if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10718 mode = inner_mode;
10719 continue;
10721 break;
10723 case MULT:
10724 /* Some machines use MULT instead of ASHIFT because MULT
10725 is cheaper. But it is still better on those machines to
10726 merge two shifts into one. */
10727 if (CONST_INT_P (XEXP (varop, 1))
10728 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10730 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10731 varop = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10732 XEXP (varop, 0), log2_rtx);
10733 continue;
10735 break;
10737 case UDIV:
10738 /* Similar, for when divides are cheaper. */
10739 if (CONST_INT_P (XEXP (varop, 1))
10740 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10742 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10743 varop = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10744 XEXP (varop, 0), log2_rtx);
10745 continue;
10747 break;
10749 case ASHIFTRT:
10750 /* If we are extracting just the sign bit of an arithmetic
10751 right shift, that shift is not needed. However, the sign
10752 bit of a wider mode may be different from what would be
10753 interpreted as the sign bit in a narrower mode, so, if
10754 the result is narrower, don't discard the shift. */
10755 if (code == LSHIFTRT
10756 && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10757 && (GET_MODE_UNIT_BITSIZE (result_mode)
10758 >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10760 varop = XEXP (varop, 0);
10761 continue;
10764 /* fall through */
10766 case LSHIFTRT:
10767 case ASHIFT:
10768 case ROTATE:
10769 /* The following rules apply only to scalars. */
10770 if (shift_mode != shift_unit_mode)
10771 break;
10772 int_mode = as_a <scalar_int_mode> (mode);
10773 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10774 int_result_mode = as_a <scalar_int_mode> (result_mode);
10776 /* Here we have two nested shifts. The result is usually the
10777 AND of a new shift with a mask. We compute the result below. */
10778 if (CONST_INT_P (XEXP (varop, 1))
10779 && INTVAL (XEXP (varop, 1)) >= 0
10780 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10781 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10782 && HWI_COMPUTABLE_MODE_P (int_mode))
10784 enum rtx_code first_code = GET_CODE (varop);
10785 unsigned int first_count = INTVAL (XEXP (varop, 1));
10786 unsigned HOST_WIDE_INT mask;
10787 rtx mask_rtx;
10789 /* We have one common special case. We can't do any merging if
10790 the inner code is an ASHIFTRT of a smaller mode. However, if
10791 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10792 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10793 we can convert it to
10794 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10795 This simplifies certain SIGN_EXTEND operations. */
10796 if (code == ASHIFT && first_code == ASHIFTRT
10797 && count == (GET_MODE_PRECISION (int_result_mode)
10798 - GET_MODE_PRECISION (int_varop_mode)))
10800 /* C3 has the low-order C1 bits zero. */
10802 mask = GET_MODE_MASK (int_mode)
10803 & ~((HOST_WIDE_INT_1U << first_count) - 1);
10805 varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10806 XEXP (varop, 0), mask);
10807 varop = simplify_shift_const (NULL_RTX, ASHIFT,
10808 int_result_mode, varop, count);
10809 count = first_count;
10810 code = ASHIFTRT;
10811 continue;
10814 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10815 than C1 high-order bits equal to the sign bit, we can convert
10816 this to either an ASHIFT or an ASHIFTRT depending on the
10817 two counts.
10819 We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10821 if (code == ASHIFTRT && first_code == ASHIFT
10822 && int_varop_mode == shift_unit_mode
10823 && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10824 > first_count))
10826 varop = XEXP (varop, 0);
10827 count -= first_count;
10828 if (count < 0)
10830 count = -count;
10831 code = ASHIFT;
10834 continue;
10837 /* There are some cases we can't do. If CODE is ASHIFTRT,
10838 we can only do this if FIRST_CODE is also ASHIFTRT.
10840 We can't do the case when CODE is ROTATE and FIRST_CODE is
10841 ASHIFTRT.
10843 If the mode of this shift is not the mode of the outer shift,
10844 we can't do this if either shift is a right shift or ROTATE.
10846 Finally, we can't do any of these if the mode is too wide
10847 unless the codes are the same.
10849 Handle the case where the shift codes are the same
10850 first. */
10852 if (code == first_code)
10854 if (int_varop_mode != int_result_mode
10855 && (code == ASHIFTRT || code == LSHIFTRT
10856 || code == ROTATE))
10857 break;
10859 count += first_count;
10860 varop = XEXP (varop, 0);
10861 continue;
10864 if (code == ASHIFTRT
10865 || (code == ROTATE && first_code == ASHIFTRT)
10866 || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10867 || (int_varop_mode != int_result_mode
10868 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10869 || first_code == ROTATE
10870 || code == ROTATE)))
10871 break;
10873 /* To compute the mask to apply after the shift, shift the
10874 nonzero bits of the inner shift the same way the
10875 outer shift will. */
10877 mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10878 int_result_mode);
10879 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10880 mask_rtx
10881 = simplify_const_binary_operation (code, int_result_mode,
10882 mask_rtx, count_rtx);
10884 /* Give up if we can't compute an outer operation to use. */
10885 if (mask_rtx == 0
10886 || !CONST_INT_P (mask_rtx)
10887 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10888 INTVAL (mask_rtx),
10889 int_result_mode, &complement_p))
10890 break;
10892 /* If the shifts are in the same direction, we add the
10893 counts. Otherwise, we subtract them. */
10894 if ((code == ASHIFTRT || code == LSHIFTRT)
10895 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10896 count += first_count;
10897 else
10898 count -= first_count;
10900 /* If COUNT is positive, the new shift is usually CODE,
10901 except for the two exceptions below, in which case it is
10902 FIRST_CODE. If the count is negative, FIRST_CODE should
10903 always be used */
10904 if (count > 0
10905 && ((first_code == ROTATE && code == ASHIFT)
10906 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10907 code = first_code;
10908 else if (count < 0)
10909 code = first_code, count = -count;
10911 varop = XEXP (varop, 0);
10912 continue;
10915 /* If we have (A << B << C) for any shift, we can convert this to
10916 (A << C << B). This wins if A is a constant. Only try this if
10917 B is not a constant. */
10919 else if (GET_CODE (varop) == code
10920 && CONST_INT_P (XEXP (varop, 0))
10921 && !CONST_INT_P (XEXP (varop, 1)))
10923 /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10924 sure the result will be masked. See PR70222. */
10925 if (code == LSHIFTRT
10926 && int_mode != int_result_mode
10927 && !merge_outer_ops (&outer_op, &outer_const, AND,
10928 GET_MODE_MASK (int_result_mode)
10929 >> orig_count, int_result_mode,
10930 &complement_p))
10931 break;
10932 /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
10933 up outer sign extension (often left and right shift) is
10934 hardly more efficient than the original. See PR70429.
10935 Similarly punt for rotates with different modes.
10936 See PR97386. */
10937 if ((code == ASHIFTRT || code == ROTATE)
10938 && int_mode != int_result_mode)
10939 break;
10941 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10942 rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10943 XEXP (varop, 0),
10944 count_rtx);
10945 varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10946 count = 0;
10947 continue;
10949 break;
10951 case NOT:
10952 /* The following rules apply only to scalars. */
10953 if (shift_mode != shift_unit_mode)
10954 break;
10956 /* Make this fit the case below. */
10957 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10958 continue;
10960 case IOR:
10961 case AND:
10962 case XOR:
10963 /* The following rules apply only to scalars. */
10964 if (shift_mode != shift_unit_mode)
10965 break;
10966 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10967 int_result_mode = as_a <scalar_int_mode> (result_mode);
10969 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10970 with C the size of VAROP - 1 and the shift is logical if
10971 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10972 we have an (le X 0) operation. If we have an arithmetic shift
10973 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10974 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10976 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10977 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10978 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10979 && (code == LSHIFTRT || code == ASHIFTRT)
10980 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
10981 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10983 count = 0;
10984 varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
10985 const0_rtx);
10987 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10988 varop = gen_rtx_NEG (int_varop_mode, varop);
10990 continue;
10993 /* If we have (shift (logical)), move the logical to the outside
10994 to allow it to possibly combine with another logical and the
10995 shift to combine with another shift. This also canonicalizes to
10996 what a ZERO_EXTRACT looks like. Also, some machines have
10997 (and (shift)) insns. */
10999 if (CONST_INT_P (XEXP (varop, 1))
11000 /* We can't do this if we have (ashiftrt (xor)) and the
11001 constant has its sign bit set in shift_unit_mode with
11002 shift_unit_mode wider than result_mode. */
11003 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11004 && int_result_mode != shift_unit_mode
11005 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11006 shift_unit_mode) < 0)
11007 && (new_rtx = simplify_const_binary_operation
11008 (code, int_result_mode,
11009 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11010 gen_int_shift_amount (int_result_mode, count))) != 0
11011 && CONST_INT_P (new_rtx)
11012 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
11013 INTVAL (new_rtx), int_result_mode,
11014 &complement_p))
11016 varop = XEXP (varop, 0);
11017 continue;
11020 /* If we can't do that, try to simplify the shift in each arm of the
11021 logical expression, make a new logical expression, and apply
11022 the inverse distributive law. This also can't be done for
11023 (ashiftrt (xor)) where we've widened the shift and the constant
11024 changes the sign bit. */
11025 if (CONST_INT_P (XEXP (varop, 1))
11026 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11027 && int_result_mode != shift_unit_mode
11028 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11029 shift_unit_mode) < 0))
11031 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11032 XEXP (varop, 0), count);
11033 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11034 XEXP (varop, 1), count);
11036 varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
11037 lhs, rhs);
11038 varop = apply_distributive_law (varop);
11040 count = 0;
11041 continue;
11043 break;
11045 case EQ:
11046 /* The following rules apply only to scalars. */
11047 if (shift_mode != shift_unit_mode)
11048 break;
11049 int_result_mode = as_a <scalar_int_mode> (result_mode);
11051 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
11052 says that the sign bit can be tested, FOO has mode MODE, C is
11053 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
11054 that may be nonzero. */
11055 if (code == LSHIFTRT
11056 && XEXP (varop, 1) == const0_rtx
11057 && GET_MODE (XEXP (varop, 0)) == int_result_mode
11058 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11059 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11060 && STORE_FLAG_VALUE == -1
11061 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11062 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11063 int_result_mode, &complement_p))
11065 varop = XEXP (varop, 0);
11066 count = 0;
11067 continue;
11069 break;
11071 case NEG:
11072 /* The following rules apply only to scalars. */
11073 if (shift_mode != shift_unit_mode)
11074 break;
11075 int_result_mode = as_a <scalar_int_mode> (result_mode);
11077 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
11078 than the number of bits in the mode is equivalent to A. */
11079 if (code == LSHIFTRT
11080 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11081 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
11083 varop = XEXP (varop, 0);
11084 count = 0;
11085 continue;
11088 /* NEG commutes with ASHIFT since it is multiplication. Move the
11089 NEG outside to allow shifts to combine. */
11090 if (code == ASHIFT
11091 && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
11092 int_result_mode, &complement_p))
11094 varop = XEXP (varop, 0);
11095 continue;
11097 break;
11099 case PLUS:
11100 /* The following rules apply only to scalars. */
11101 if (shift_mode != shift_unit_mode)
11102 break;
11103 int_result_mode = as_a <scalar_int_mode> (result_mode);
11105 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11106 is one less than the number of bits in the mode is
11107 equivalent to (xor A 1). */
11108 if (code == LSHIFTRT
11109 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11110 && XEXP (varop, 1) == constm1_rtx
11111 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11112 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11113 int_result_mode, &complement_p))
11115 count = 0;
11116 varop = XEXP (varop, 0);
11117 continue;
11120 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11121 that might be nonzero in BAR are those being shifted out and those
11122 bits are known zero in FOO, we can replace the PLUS with FOO.
11123 Similarly in the other operand order. This code occurs when
11124 we are computing the size of a variable-size array. */
11126 if ((code == ASHIFTRT || code == LSHIFTRT)
11127 && count < HOST_BITS_PER_WIDE_INT
11128 && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11129 && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11130 & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11132 varop = XEXP (varop, 0);
11133 continue;
11135 else if ((code == ASHIFTRT || code == LSHIFTRT)
11136 && count < HOST_BITS_PER_WIDE_INT
11137 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11138 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11139 >> count) == 0
11140 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11141 & nonzero_bits (XEXP (varop, 1), int_result_mode)) == 0)
11143 varop = XEXP (varop, 1);
11144 continue;
11147 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11148 if (code == ASHIFT
11149 && CONST_INT_P (XEXP (varop, 1))
11150 && (new_rtx = simplify_const_binary_operation
11151 (ASHIFT, int_result_mode,
11152 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11153 gen_int_shift_amount (int_result_mode, count))) != 0
11154 && CONST_INT_P (new_rtx)
11155 && merge_outer_ops (&outer_op, &outer_const, PLUS,
11156 INTVAL (new_rtx), int_result_mode,
11157 &complement_p))
11159 varop = XEXP (varop, 0);
11160 continue;
11163 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11164 signbit', and attempt to change the PLUS to an XOR and move it to
11165 the outer operation as is done above in the AND/IOR/XOR case
11166 leg for shift(logical). See details in logical handling above
11167 for reasoning in doing so. */
11168 if (code == LSHIFTRT
11169 && CONST_INT_P (XEXP (varop, 1))
11170 && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11171 && (new_rtx = simplify_const_binary_operation
11172 (code, int_result_mode,
11173 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11174 gen_int_shift_amount (int_result_mode, count))) != 0
11175 && CONST_INT_P (new_rtx)
11176 && merge_outer_ops (&outer_op, &outer_const, XOR,
11177 INTVAL (new_rtx), int_result_mode,
11178 &complement_p))
11180 varop = XEXP (varop, 0);
11181 continue;
11184 break;
11186 case MINUS:
11187 /* The following rules apply only to scalars. */
11188 if (shift_mode != shift_unit_mode)
11189 break;
11190 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11192 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11193 with C the size of VAROP - 1 and the shift is logical if
11194 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11195 we have a (gt X 0) operation. If the shift is arithmetic with
11196 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11197 we have a (neg (gt X 0)) operation. */
11199 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11200 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11201 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11202 && (code == LSHIFTRT || code == ASHIFTRT)
11203 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11204 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11205 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11207 count = 0;
11208 varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11209 const0_rtx);
11211 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11212 varop = gen_rtx_NEG (int_varop_mode, varop);
11214 continue;
11216 break;
11218 case TRUNCATE:
11219 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11220 if the truncate does not affect the value. */
11221 if (code == LSHIFTRT
11222 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11223 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11224 && (INTVAL (XEXP (XEXP (varop, 0), 1))
11225 >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11226 - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11228 rtx varop_inner = XEXP (varop, 0);
11229 int new_count = count + INTVAL (XEXP (varop_inner, 1));
11230 rtx new_count_rtx = gen_int_shift_amount (GET_MODE (varop_inner),
11231 new_count);
11232 varop_inner = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11233 XEXP (varop_inner, 0),
11234 new_count_rtx);
11235 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11236 count = 0;
11237 continue;
11239 break;
11241 default:
11242 break;
11245 break;
11248 shift_mode = result_mode;
11249 if (shift_mode != mode)
11251 /* We only change the modes of scalar shifts. */
11252 int_mode = as_a <scalar_int_mode> (mode);
11253 int_result_mode = as_a <scalar_int_mode> (result_mode);
11254 shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11255 int_mode, outer_op, outer_const);
11258 /* We have now finished analyzing the shift. The result should be
11259 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11260 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11261 to the result of the shift. OUTER_CONST is the relevant constant,
11262 but we must turn off all bits turned off in the shift. */
11264 if (outer_op == UNKNOWN
11265 && orig_code == code && orig_count == count
11266 && varop == orig_varop
11267 && shift_mode == GET_MODE (varop))
11268 return NULL_RTX;
11270 /* Make a SUBREG if necessary. If we can't make it, fail. */
11271 varop = gen_lowpart (shift_mode, varop);
11272 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11273 return NULL_RTX;
11275 /* If we have an outer operation and we just made a shift, it is
11276 possible that we could have simplified the shift were it not
11277 for the outer operation. So try to do the simplification
11278 recursively. */
11280 if (outer_op != UNKNOWN)
11281 x = simplify_shift_const_1 (code, shift_mode, varop, count);
11282 else
11283 x = NULL_RTX;
11285 if (x == NULL_RTX)
11286 x = simplify_gen_binary (code, shift_mode, varop,
11287 gen_int_shift_amount (shift_mode, count));
11289 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11290 turn off all the bits that the shift would have turned off. */
11291 if (orig_code == LSHIFTRT && result_mode != shift_mode)
11292 /* We only change the modes of scalar shifts. */
11293 x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11294 x, GET_MODE_MASK (result_mode) >> orig_count);
11296 /* Do the remainder of the processing in RESULT_MODE. */
11297 x = gen_lowpart_or_truncate (result_mode, x);
11299 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11300 operation. */
11301 if (complement_p)
11302 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11304 if (outer_op != UNKNOWN)
11306 int_result_mode = as_a <scalar_int_mode> (result_mode);
11308 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11309 && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11310 outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11312 if (outer_op == AND)
11313 x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11314 else if (outer_op == SET)
11316 /* This means that we have determined that the result is
11317 equivalent to a constant. This should be rare. */
11318 if (!side_effects_p (x))
11319 x = GEN_INT (outer_const);
11321 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11322 x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11323 else
11324 x = simplify_gen_binary (outer_op, int_result_mode, x,
11325 GEN_INT (outer_const));
11328 return x;
11331 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11332 The result of the shift is RESULT_MODE. If we cannot simplify it,
11333 return X or, if it is NULL, synthesize the expression with
11334 simplify_gen_binary. Otherwise, return a simplified value.
11336 The shift is normally computed in the widest mode we find in VAROP, as
11337 long as it isn't a different number of words than RESULT_MODE. Exceptions
11338 are ASHIFTRT and ROTATE, which are always done in their original mode. */
11340 static rtx
11341 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11342 rtx varop, int count)
11344 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11345 if (tem)
11346 return tem;
11348 if (!x)
11349 x = simplify_gen_binary (code, GET_MODE (varop), varop,
11350 gen_int_shift_amount (GET_MODE (varop), count));
11351 if (GET_MODE (x) != result_mode)
11352 x = gen_lowpart (result_mode, x);
11353 return x;
11357 /* A subroutine of recog_for_combine. See there for arguments and
11358 return value. */
11360 static int
11361 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11363 rtx pat = *pnewpat;
11364 rtx pat_without_clobbers;
11365 int insn_code_number;
11366 int num_clobbers_to_add = 0;
11367 int i;
11368 rtx notes = NULL_RTX;
11369 rtx old_notes, old_pat;
11370 int old_icode;
11372 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11373 we use to indicate that something didn't match. If we find such a
11374 thing, force rejection. */
11375 if (GET_CODE (pat) == PARALLEL)
11376 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11377 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11378 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11379 return -1;
11381 old_pat = PATTERN (insn);
11382 old_notes = REG_NOTES (insn);
11383 PATTERN (insn) = pat;
11384 REG_NOTES (insn) = NULL_RTX;
11386 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11387 if (dump_file && (dump_flags & TDF_DETAILS))
11389 if (insn_code_number < 0)
11390 fputs ("Failed to match this instruction:\n", dump_file);
11391 else
11392 fputs ("Successfully matched this instruction:\n", dump_file);
11393 print_rtl_single (dump_file, pat);
11396 /* If it isn't, there is the possibility that we previously had an insn
11397 that clobbered some register as a side effect, but the combined
11398 insn doesn't need to do that. So try once more without the clobbers
11399 unless this represents an ASM insn. */
11401 if (insn_code_number < 0 && ! check_asm_operands (pat)
11402 && GET_CODE (pat) == PARALLEL)
11404 int pos;
11406 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11407 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11409 if (i != pos)
11410 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11411 pos++;
11414 SUBST_INT (XVECLEN (pat, 0), pos);
11416 if (pos == 1)
11417 pat = XVECEXP (pat, 0, 0);
11419 PATTERN (insn) = pat;
11420 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11421 if (dump_file && (dump_flags & TDF_DETAILS))
11423 if (insn_code_number < 0)
11424 fputs ("Failed to match this instruction:\n", dump_file);
11425 else
11426 fputs ("Successfully matched this instruction:\n", dump_file);
11427 print_rtl_single (dump_file, pat);
11431 pat_without_clobbers = pat;
11433 PATTERN (insn) = old_pat;
11434 REG_NOTES (insn) = old_notes;
11436 /* Recognize all noop sets, these will be killed by followup pass. */
11437 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11438 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11440 /* If we had any clobbers to add, make a new pattern than contains
11441 them. Then check to make sure that all of them are dead. */
11442 if (num_clobbers_to_add)
11444 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11445 rtvec_alloc (GET_CODE (pat) == PARALLEL
11446 ? (XVECLEN (pat, 0)
11447 + num_clobbers_to_add)
11448 : num_clobbers_to_add + 1));
11450 if (GET_CODE (pat) == PARALLEL)
11451 for (i = 0; i < XVECLEN (pat, 0); i++)
11452 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11453 else
11454 XVECEXP (newpat, 0, 0) = pat;
11456 add_clobbers (newpat, insn_code_number);
11458 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11459 i < XVECLEN (newpat, 0); i++)
11461 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11462 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11463 return -1;
11464 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11466 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11467 notes = alloc_reg_note (REG_UNUSED,
11468 XEXP (XVECEXP (newpat, 0, i), 0), notes);
11471 pat = newpat;
11474 if (insn_code_number >= 0
11475 && insn_code_number != NOOP_MOVE_INSN_CODE)
11477 old_pat = PATTERN (insn);
11478 old_notes = REG_NOTES (insn);
11479 old_icode = INSN_CODE (insn);
11480 PATTERN (insn) = pat;
11481 REG_NOTES (insn) = notes;
11482 INSN_CODE (insn) = insn_code_number;
11484 /* Allow targets to reject combined insn. */
11485 if (!targetm.legitimate_combined_insn (insn))
11487 if (dump_file && (dump_flags & TDF_DETAILS))
11488 fputs ("Instruction not appropriate for target.",
11489 dump_file);
11491 /* Callers expect recog_for_combine to strip
11492 clobbers from the pattern on failure. */
11493 pat = pat_without_clobbers;
11494 notes = NULL_RTX;
11496 insn_code_number = -1;
11499 PATTERN (insn) = old_pat;
11500 REG_NOTES (insn) = old_notes;
11501 INSN_CODE (insn) = old_icode;
11504 *pnewpat = pat;
11505 *pnotes = notes;
11507 return insn_code_number;
11510 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11511 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11512 Return whether anything was so changed. */
11514 static bool
11515 change_zero_ext (rtx pat)
11517 bool changed = false;
11518 rtx *src = &SET_SRC (pat);
11520 subrtx_ptr_iterator::array_type array;
11521 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11523 rtx x = **iter;
11524 scalar_int_mode mode, inner_mode;
11525 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11526 continue;
11527 int size;
11529 if (GET_CODE (x) == ZERO_EXTRACT
11530 && CONST_INT_P (XEXP (x, 1))
11531 && CONST_INT_P (XEXP (x, 2))
11532 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11533 && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11535 size = INTVAL (XEXP (x, 1));
11537 int start = INTVAL (XEXP (x, 2));
11538 if (BITS_BIG_ENDIAN)
11539 start = GET_MODE_PRECISION (inner_mode) - size - start;
11541 if (start != 0)
11542 x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0),
11543 gen_int_shift_amount (inner_mode, start));
11544 else
11545 x = XEXP (x, 0);
11547 if (mode != inner_mode)
11549 if (REG_P (x) && HARD_REGISTER_P (x)
11550 && !can_change_dest_mode (x, 0, mode))
11551 continue;
11553 x = gen_lowpart_SUBREG (mode, x);
11556 else if (GET_CODE (x) == ZERO_EXTEND
11557 && GET_CODE (XEXP (x, 0)) == SUBREG
11558 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11559 && !paradoxical_subreg_p (XEXP (x, 0))
11560 && subreg_lowpart_p (XEXP (x, 0)))
11562 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11563 size = GET_MODE_PRECISION (inner_mode);
11564 x = SUBREG_REG (XEXP (x, 0));
11565 if (GET_MODE (x) != mode)
11567 if (REG_P (x) && HARD_REGISTER_P (x)
11568 && !can_change_dest_mode (x, 0, mode))
11569 continue;
11571 x = gen_lowpart_SUBREG (mode, x);
11574 else if (GET_CODE (x) == ZERO_EXTEND
11575 && REG_P (XEXP (x, 0))
11576 && HARD_REGISTER_P (XEXP (x, 0))
11577 && can_change_dest_mode (XEXP (x, 0), 0, mode))
11579 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11580 size = GET_MODE_PRECISION (inner_mode);
11581 x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11583 else
11584 continue;
11586 if (!(GET_CODE (x) == LSHIFTRT
11587 && CONST_INT_P (XEXP (x, 1))
11588 && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11590 wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11591 x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11594 SUBST (**iter, x);
11595 changed = true;
11598 if (changed)
11599 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11600 maybe_swap_commutative_operands (**iter);
11602 rtx *dst = &SET_DEST (pat);
11603 scalar_int_mode mode;
11604 if (GET_CODE (*dst) == ZERO_EXTRACT
11605 && REG_P (XEXP (*dst, 0))
11606 && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11607 && CONST_INT_P (XEXP (*dst, 1))
11608 && CONST_INT_P (XEXP (*dst, 2)))
11610 rtx reg = XEXP (*dst, 0);
11611 int width = INTVAL (XEXP (*dst, 1));
11612 int offset = INTVAL (XEXP (*dst, 2));
11613 int reg_width = GET_MODE_PRECISION (mode);
11614 if (BITS_BIG_ENDIAN)
11615 offset = reg_width - width - offset;
11617 rtx x, y, z, w;
11618 wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11619 wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11620 x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11621 if (offset)
11622 y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11623 else
11624 y = SET_SRC (pat);
11625 z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11626 w = gen_rtx_IOR (mode, x, z);
11627 SUBST (SET_DEST (pat), reg);
11628 SUBST (SET_SRC (pat), w);
11630 changed = true;
11633 return changed;
11636 /* Like recog, but we receive the address of a pointer to a new pattern.
11637 We try to match the rtx that the pointer points to.
11638 If that fails, we may try to modify or replace the pattern,
11639 storing the replacement into the same pointer object.
11641 Modifications include deletion or addition of CLOBBERs. If the
11642 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11643 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11644 (and undo if that fails).
11646 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11647 the CLOBBERs are placed.
11649 The value is the final insn code from the pattern ultimately matched,
11650 or -1. */
11652 static int
11653 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11655 rtx pat = *pnewpat;
11656 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11657 if (insn_code_number >= 0 || check_asm_operands (pat))
11658 return insn_code_number;
11660 void *marker = get_undo_marker ();
11661 bool changed = false;
11663 if (GET_CODE (pat) == SET)
11665 /* For an unrecognized single set of a constant, try placing it in
11666 the constant pool, if this function already uses one. */
11667 rtx src = SET_SRC (pat);
11668 if (CONSTANT_P (src)
11669 && !CONST_INT_P (src)
11670 && crtl->uses_const_pool)
11672 machine_mode mode = GET_MODE (src);
11673 if (mode == VOIDmode)
11674 mode = GET_MODE (SET_DEST (pat));
11675 src = force_const_mem (mode, src);
11676 if (src)
11678 SUBST (SET_SRC (pat), src);
11679 changed = true;
11682 else
11683 changed = change_zero_ext (pat);
11685 else if (GET_CODE (pat) == PARALLEL)
11687 int i;
11688 for (i = 0; i < XVECLEN (pat, 0); i++)
11690 rtx set = XVECEXP (pat, 0, i);
11691 if (GET_CODE (set) == SET)
11692 changed |= change_zero_ext (set);
11696 if (changed)
11698 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11700 if (insn_code_number < 0)
11701 undo_to_marker (marker);
11704 return insn_code_number;
11707 /* Like gen_lowpart_general but for use by combine. In combine it
11708 is not possible to create any new pseudoregs. However, it is
11709 safe to create invalid memory addresses, because combine will
11710 try to recognize them and all they will do is make the combine
11711 attempt fail.
11713 If for some reason this cannot do its job, an rtx
11714 (clobber (const_int 0)) is returned.
11715 An insn containing that will not be recognized. */
11717 static rtx
11718 gen_lowpart_for_combine (machine_mode omode, rtx x)
11720 machine_mode imode = GET_MODE (x);
11721 rtx result;
11723 if (omode == imode)
11724 return x;
11726 /* We can only support MODE being wider than a word if X is a
11727 constant integer or has a mode the same size. */
11728 if (maybe_gt (GET_MODE_SIZE (omode), UNITS_PER_WORD)
11729 && ! (CONST_SCALAR_INT_P (x)
11730 || known_eq (GET_MODE_SIZE (imode), GET_MODE_SIZE (omode))))
11731 goto fail;
11733 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11734 won't know what to do. So we will strip off the SUBREG here and
11735 process normally. */
11736 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11738 x = SUBREG_REG (x);
11740 /* For use in case we fall down into the address adjustments
11741 further below, we need to adjust the known mode and size of
11742 x; imode and isize, since we just adjusted x. */
11743 imode = GET_MODE (x);
11745 if (imode == omode)
11746 return x;
11749 result = gen_lowpart_common (omode, x);
11751 if (result)
11752 return result;
11754 if (MEM_P (x))
11756 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11757 address. */
11758 if (MEM_VOLATILE_P (x)
11759 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11760 goto fail;
11762 /* If we want to refer to something bigger than the original memref,
11763 generate a paradoxical subreg instead. That will force a reload
11764 of the original memref X. */
11765 if (paradoxical_subreg_p (omode, imode))
11766 return gen_rtx_SUBREG (omode, x, 0);
11768 poly_int64 offset = byte_lowpart_offset (omode, imode);
11769 return adjust_address_nv (x, omode, offset);
11772 /* If X is a comparison operator, rewrite it in a new mode. This
11773 probably won't match, but may allow further simplifications. */
11774 else if (COMPARISON_P (x)
11775 && SCALAR_INT_MODE_P (imode)
11776 && SCALAR_INT_MODE_P (omode))
11777 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11779 /* If we couldn't simplify X any other way, just enclose it in a
11780 SUBREG. Normally, this SUBREG won't match, but some patterns may
11781 include an explicit SUBREG or we may simplify it further in combine. */
11782 else
11784 rtx res;
11786 if (imode == VOIDmode)
11788 imode = int_mode_for_mode (omode).require ();
11789 x = gen_lowpart_common (imode, x);
11790 if (x == NULL)
11791 goto fail;
11793 res = lowpart_subreg (omode, x, imode);
11794 if (res)
11795 return res;
11798 fail:
11799 return gen_rtx_CLOBBER (omode, const0_rtx);
11802 /* Try to simplify a comparison between OP0 and a constant OP1,
11803 where CODE is the comparison code that will be tested, into a
11804 (CODE OP0 const0_rtx) form.
11806 The result is a possibly different comparison code to use.
11807 *POP1 may be updated. */
11809 static enum rtx_code
11810 simplify_compare_const (enum rtx_code code, machine_mode mode,
11811 rtx op0, rtx *pop1)
11813 scalar_int_mode int_mode;
11814 HOST_WIDE_INT const_op = INTVAL (*pop1);
11816 /* Get the constant we are comparing against and turn off all bits
11817 not on in our mode. */
11818 if (mode != VOIDmode)
11819 const_op = trunc_int_for_mode (const_op, mode);
11821 /* If we are comparing against a constant power of two and the value
11822 being compared can only have that single bit nonzero (e.g., it was
11823 `and'ed with that bit), we can replace this with a comparison
11824 with zero. */
11825 if (const_op
11826 && (code == EQ || code == NE || code == GE || code == GEU
11827 || code == LT || code == LTU)
11828 && is_a <scalar_int_mode> (mode, &int_mode)
11829 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11830 && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11831 && (nonzero_bits (op0, int_mode)
11832 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11834 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11835 const_op = 0;
11838 /* Similarly, if we are comparing a value known to be either -1 or
11839 0 with -1, change it to the opposite comparison against zero. */
11840 if (const_op == -1
11841 && (code == EQ || code == NE || code == GT || code == LE
11842 || code == GEU || code == LTU)
11843 && is_a <scalar_int_mode> (mode, &int_mode)
11844 && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11846 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11847 const_op = 0;
11850 /* Do some canonicalizations based on the comparison code. We prefer
11851 comparisons against zero and then prefer equality comparisons.
11852 If we can reduce the size of a constant, we will do that too. */
11853 switch (code)
11855 case LT:
11856 /* < C is equivalent to <= (C - 1) */
11857 if (const_op > 0)
11859 const_op -= 1;
11860 code = LE;
11861 /* ... fall through to LE case below. */
11862 gcc_fallthrough ();
11864 else
11865 break;
11867 case LE:
11868 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11869 if (const_op < 0)
11871 const_op += 1;
11872 code = LT;
11875 /* If we are doing a <= 0 comparison on a value known to have
11876 a zero sign bit, we can replace this with == 0. */
11877 else if (const_op == 0
11878 && is_a <scalar_int_mode> (mode, &int_mode)
11879 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11880 && (nonzero_bits (op0, int_mode)
11881 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11882 == 0)
11883 code = EQ;
11884 break;
11886 case GE:
11887 /* >= C is equivalent to > (C - 1). */
11888 if (const_op > 0)
11890 const_op -= 1;
11891 code = GT;
11892 /* ... fall through to GT below. */
11893 gcc_fallthrough ();
11895 else
11896 break;
11898 case GT:
11899 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11900 if (const_op < 0)
11902 const_op += 1;
11903 code = GE;
11906 /* If we are doing a > 0 comparison on a value known to have
11907 a zero sign bit, we can replace this with != 0. */
11908 else if (const_op == 0
11909 && is_a <scalar_int_mode> (mode, &int_mode)
11910 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11911 && (nonzero_bits (op0, int_mode)
11912 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11913 == 0)
11914 code = NE;
11915 break;
11917 case LTU:
11918 /* < C is equivalent to <= (C - 1). */
11919 if (const_op > 0)
11921 const_op -= 1;
11922 code = LEU;
11923 /* ... fall through ... */
11924 gcc_fallthrough ();
11926 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11927 else if (is_a <scalar_int_mode> (mode, &int_mode)
11928 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11929 && ((unsigned HOST_WIDE_INT) const_op
11930 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11932 const_op = 0;
11933 code = GE;
11934 break;
11936 else
11937 break;
11939 case LEU:
11940 /* unsigned <= 0 is equivalent to == 0 */
11941 if (const_op == 0)
11942 code = EQ;
11943 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11944 else if (is_a <scalar_int_mode> (mode, &int_mode)
11945 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11946 && ((unsigned HOST_WIDE_INT) const_op
11947 == ((HOST_WIDE_INT_1U
11948 << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11950 const_op = 0;
11951 code = GE;
11953 break;
11955 case GEU:
11956 /* >= C is equivalent to > (C - 1). */
11957 if (const_op > 1)
11959 const_op -= 1;
11960 code = GTU;
11961 /* ... fall through ... */
11962 gcc_fallthrough ();
11965 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11966 else if (is_a <scalar_int_mode> (mode, &int_mode)
11967 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11968 && ((unsigned HOST_WIDE_INT) const_op
11969 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11971 const_op = 0;
11972 code = LT;
11973 break;
11975 else
11976 break;
11978 case GTU:
11979 /* unsigned > 0 is equivalent to != 0 */
11980 if (const_op == 0)
11981 code = NE;
11982 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11983 else if (is_a <scalar_int_mode> (mode, &int_mode)
11984 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11985 && ((unsigned HOST_WIDE_INT) const_op
11986 == (HOST_WIDE_INT_1U
11987 << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
11989 const_op = 0;
11990 code = LT;
11992 break;
11994 default:
11995 break;
11998 *pop1 = GEN_INT (const_op);
11999 return code;
12002 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
12003 comparison code that will be tested.
12005 The result is a possibly different comparison code to use. *POP0 and
12006 *POP1 may be updated.
12008 It is possible that we might detect that a comparison is either always
12009 true or always false. However, we do not perform general constant
12010 folding in combine, so this knowledge isn't useful. Such tautologies
12011 should have been detected earlier. Hence we ignore all such cases. */
12013 static enum rtx_code
12014 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
12016 rtx op0 = *pop0;
12017 rtx op1 = *pop1;
12018 rtx tem, tem1;
12019 int i;
12020 scalar_int_mode mode, inner_mode, tmode;
12021 opt_scalar_int_mode tmode_iter;
12023 /* Try a few ways of applying the same transformation to both operands. */
12024 while (1)
12026 /* The test below this one won't handle SIGN_EXTENDs on these machines,
12027 so check specially. */
12028 if (!WORD_REGISTER_OPERATIONS
12029 && code != GTU && code != GEU && code != LTU && code != LEU
12030 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
12031 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12032 && GET_CODE (XEXP (op1, 0)) == ASHIFT
12033 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
12034 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
12035 && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
12036 && (is_a <scalar_int_mode>
12037 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
12038 && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
12039 && CONST_INT_P (XEXP (op0, 1))
12040 && XEXP (op0, 1) == XEXP (op1, 1)
12041 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12042 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
12043 && (INTVAL (XEXP (op0, 1))
12044 == (GET_MODE_PRECISION (mode)
12045 - GET_MODE_PRECISION (inner_mode))))
12047 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
12048 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
12051 /* If both operands are the same constant shift, see if we can ignore the
12052 shift. We can if the shift is a rotate or if the bits shifted out of
12053 this shift are known to be zero for both inputs and if the type of
12054 comparison is compatible with the shift. */
12055 if (GET_CODE (op0) == GET_CODE (op1)
12056 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
12057 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
12058 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
12059 && (code != GT && code != LT && code != GE && code != LE))
12060 || (GET_CODE (op0) == ASHIFTRT
12061 && (code != GTU && code != LTU
12062 && code != GEU && code != LEU)))
12063 && CONST_INT_P (XEXP (op0, 1))
12064 && INTVAL (XEXP (op0, 1)) >= 0
12065 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12066 && XEXP (op0, 1) == XEXP (op1, 1))
12068 machine_mode mode = GET_MODE (op0);
12069 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12070 int shift_count = INTVAL (XEXP (op0, 1));
12072 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
12073 mask &= (mask >> shift_count) << shift_count;
12074 else if (GET_CODE (op0) == ASHIFT)
12075 mask = (mask & (mask << shift_count)) >> shift_count;
12077 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
12078 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
12079 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
12080 else
12081 break;
12084 /* If both operands are AND's of a paradoxical SUBREG by constant, the
12085 SUBREGs are of the same mode, and, in both cases, the AND would
12086 be redundant if the comparison was done in the narrower mode,
12087 do the comparison in the narrower mode (e.g., we are AND'ing with 1
12088 and the operand's possibly nonzero bits are 0xffffff01; in that case
12089 if we only care about QImode, we don't need the AND). This case
12090 occurs if the output mode of an scc insn is not SImode and
12091 STORE_FLAG_VALUE == 1 (e.g., the 386).
12093 Similarly, check for a case where the AND's are ZERO_EXTEND
12094 operations from some narrower mode even though a SUBREG is not
12095 present. */
12097 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
12098 && CONST_INT_P (XEXP (op0, 1))
12099 && CONST_INT_P (XEXP (op1, 1)))
12101 rtx inner_op0 = XEXP (op0, 0);
12102 rtx inner_op1 = XEXP (op1, 0);
12103 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
12104 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
12105 bool changed = false;
12107 if (paradoxical_subreg_p (inner_op0)
12108 && GET_CODE (inner_op1) == SUBREG
12109 && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
12110 && (GET_MODE (SUBREG_REG (inner_op0))
12111 == GET_MODE (SUBREG_REG (inner_op1)))
12112 && ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
12113 GET_MODE (SUBREG_REG (inner_op0)))) == 0
12114 && ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
12115 GET_MODE (SUBREG_REG (inner_op1)))) == 0)
12117 op0 = SUBREG_REG (inner_op0);
12118 op1 = SUBREG_REG (inner_op1);
12120 /* The resulting comparison is always unsigned since we masked
12121 off the original sign bit. */
12122 code = unsigned_condition (code);
12124 changed = true;
12127 else if (c0 == c1)
12128 FOR_EACH_MODE_UNTIL (tmode,
12129 as_a <scalar_int_mode> (GET_MODE (op0)))
12130 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
12132 op0 = gen_lowpart_or_truncate (tmode, inner_op0);
12133 op1 = gen_lowpart_or_truncate (tmode, inner_op1);
12134 code = unsigned_condition (code);
12135 changed = true;
12136 break;
12139 if (! changed)
12140 break;
12143 /* If both operands are NOT, we can strip off the outer operation
12144 and adjust the comparison code for swapped operands; similarly for
12145 NEG, except that this must be an equality comparison. */
12146 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12147 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12148 && (code == EQ || code == NE)))
12149 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12151 else
12152 break;
12155 /* If the first operand is a constant, swap the operands and adjust the
12156 comparison code appropriately, but don't do this if the second operand
12157 is already a constant integer. */
12158 if (swap_commutative_operands_p (op0, op1))
12160 std::swap (op0, op1);
12161 code = swap_condition (code);
12164 /* We now enter a loop during which we will try to simplify the comparison.
12165 For the most part, we only are concerned with comparisons with zero,
12166 but some things may really be comparisons with zero but not start
12167 out looking that way. */
12169 while (CONST_INT_P (op1))
12171 machine_mode raw_mode = GET_MODE (op0);
12172 scalar_int_mode int_mode;
12173 int equality_comparison_p;
12174 int sign_bit_comparison_p;
12175 int unsigned_comparison_p;
12176 HOST_WIDE_INT const_op;
12178 /* We only want to handle integral modes. This catches VOIDmode,
12179 CCmode, and the floating-point modes. An exception is that we
12180 can handle VOIDmode if OP0 is a COMPARE or a comparison
12181 operation. */
12183 if (GET_MODE_CLASS (raw_mode) != MODE_INT
12184 && ! (raw_mode == VOIDmode
12185 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12186 break;
12188 /* Try to simplify the compare to constant, possibly changing the
12189 comparison op, and/or changing op1 to zero. */
12190 code = simplify_compare_const (code, raw_mode, op0, &op1);
12191 const_op = INTVAL (op1);
12193 /* Compute some predicates to simplify code below. */
12195 equality_comparison_p = (code == EQ || code == NE);
12196 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12197 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12198 || code == GEU);
12200 /* If this is a sign bit comparison and we can do arithmetic in
12201 MODE, say that we will only be needing the sign bit of OP0. */
12202 if (sign_bit_comparison_p
12203 && is_a <scalar_int_mode> (raw_mode, &int_mode)
12204 && HWI_COMPUTABLE_MODE_P (int_mode))
12205 op0 = force_to_mode (op0, int_mode,
12206 HOST_WIDE_INT_1U
12207 << (GET_MODE_PRECISION (int_mode) - 1), false);
12209 if (COMPARISON_P (op0))
12211 /* We can't do anything if OP0 is a condition code value, rather
12212 than an actual data value. */
12213 if (const_op != 0
12214 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12215 break;
12217 /* Get the two operands being compared. */
12218 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12219 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12220 else
12221 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12223 /* Check for the cases where we simply want the result of the
12224 earlier test or the opposite of that result. */
12225 if (code == NE || code == EQ
12226 || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12227 && (code == LT || code == GE)))
12229 enum rtx_code new_code;
12230 if (code == LT || code == NE)
12231 new_code = GET_CODE (op0);
12232 else
12233 new_code = reversed_comparison_code (op0, NULL);
12235 if (new_code != UNKNOWN)
12237 code = new_code;
12238 op0 = tem;
12239 op1 = tem1;
12240 continue;
12243 break;
12246 if (raw_mode == VOIDmode)
12247 break;
12248 scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12250 /* Now try cases based on the opcode of OP0. If none of the cases
12251 does a "continue", we exit this loop immediately after the
12252 switch. */
12254 unsigned int mode_width = GET_MODE_PRECISION (mode);
12255 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12256 switch (GET_CODE (op0))
12258 case ZERO_EXTRACT:
12259 /* If we are extracting a single bit from a variable position in
12260 a constant that has only a single bit set and are comparing it
12261 with zero, we can convert this into an equality comparison
12262 between the position and the location of the single bit. */
12263 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12264 have already reduced the shift count modulo the word size. */
12265 if (!SHIFT_COUNT_TRUNCATED
12266 && CONST_INT_P (XEXP (op0, 0))
12267 && XEXP (op0, 1) == const1_rtx
12268 && equality_comparison_p && const_op == 0
12269 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12271 if (BITS_BIG_ENDIAN)
12272 i = BITS_PER_WORD - 1 - i;
12274 op0 = XEXP (op0, 2);
12275 op1 = GEN_INT (i);
12276 const_op = i;
12278 /* Result is nonzero iff shift count is equal to I. */
12279 code = reverse_condition (code);
12280 continue;
12283 /* fall through */
12285 case SIGN_EXTRACT:
12286 tem = expand_compound_operation (op0);
12287 if (tem != op0)
12289 op0 = tem;
12290 continue;
12292 break;
12294 case NOT:
12295 /* If testing for equality, we can take the NOT of the constant. */
12296 if (equality_comparison_p
12297 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12299 op0 = XEXP (op0, 0);
12300 op1 = tem;
12301 continue;
12304 /* If just looking at the sign bit, reverse the sense of the
12305 comparison. */
12306 if (sign_bit_comparison_p)
12308 op0 = XEXP (op0, 0);
12309 code = (code == GE ? LT : GE);
12310 continue;
12312 break;
12314 case NEG:
12315 /* If testing for equality, we can take the NEG of the constant. */
12316 if (equality_comparison_p
12317 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12319 op0 = XEXP (op0, 0);
12320 op1 = tem;
12321 continue;
12324 /* The remaining cases only apply to comparisons with zero. */
12325 if (const_op != 0)
12326 break;
12328 /* When X is ABS or is known positive,
12329 (neg X) is < 0 if and only if X != 0. */
12331 if (sign_bit_comparison_p
12332 && (GET_CODE (XEXP (op0, 0)) == ABS
12333 || (mode_width <= HOST_BITS_PER_WIDE_INT
12334 && (nonzero_bits (XEXP (op0, 0), mode)
12335 & (HOST_WIDE_INT_1U << (mode_width - 1)))
12336 == 0)))
12338 op0 = XEXP (op0, 0);
12339 code = (code == LT ? NE : EQ);
12340 continue;
12343 /* If we have NEG of something whose two high-order bits are the
12344 same, we know that "(-a) < 0" is equivalent to "a > 0". */
12345 if (num_sign_bit_copies (op0, mode) >= 2)
12347 op0 = XEXP (op0, 0);
12348 code = swap_condition (code);
12349 continue;
12351 break;
12353 case ROTATE:
12354 /* If we are testing equality and our count is a constant, we
12355 can perform the inverse operation on our RHS. */
12356 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12357 && (tem = simplify_binary_operation (ROTATERT, mode,
12358 op1, XEXP (op0, 1))) != 0)
12360 op0 = XEXP (op0, 0);
12361 op1 = tem;
12362 continue;
12365 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12366 a particular bit. Convert it to an AND of a constant of that
12367 bit. This will be converted into a ZERO_EXTRACT. */
12368 if (const_op == 0 && sign_bit_comparison_p
12369 && CONST_INT_P (XEXP (op0, 1))
12370 && mode_width <= HOST_BITS_PER_WIDE_INT
12371 && UINTVAL (XEXP (op0, 1)) < mode_width)
12373 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12374 (HOST_WIDE_INT_1U
12375 << (mode_width - 1
12376 - INTVAL (XEXP (op0, 1)))));
12377 code = (code == LT ? NE : EQ);
12378 continue;
12381 /* Fall through. */
12383 case ABS:
12384 /* ABS is ignorable inside an equality comparison with zero. */
12385 if (const_op == 0 && equality_comparison_p)
12387 op0 = XEXP (op0, 0);
12388 continue;
12390 break;
12392 case SIGN_EXTEND:
12393 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12394 (compare FOO CONST) if CONST fits in FOO's mode and we
12395 are either testing inequality or have an unsigned
12396 comparison with ZERO_EXTEND or a signed comparison with
12397 SIGN_EXTEND. But don't do it if we don't have a compare
12398 insn of the given mode, since we'd have to revert it
12399 later on, and then we wouldn't know whether to sign- or
12400 zero-extend. */
12401 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12402 && ! unsigned_comparison_p
12403 && HWI_COMPUTABLE_MODE_P (mode)
12404 && trunc_int_for_mode (const_op, mode) == const_op
12405 && have_insn_for (COMPARE, mode))
12407 op0 = XEXP (op0, 0);
12408 continue;
12410 break;
12412 case SUBREG:
12413 /* Check for the case where we are comparing A - C1 with C2, that is
12415 (subreg:MODE (plus (A) (-C1))) op (C2)
12417 with C1 a constant, and try to lift the SUBREG, i.e. to do the
12418 comparison in the wider mode. One of the following two conditions
12419 must be true in order for this to be valid:
12421 1. The mode extension results in the same bit pattern being added
12422 on both sides and the comparison is equality or unsigned. As
12423 C2 has been truncated to fit in MODE, the pattern can only be
12424 all 0s or all 1s.
12426 2. The mode extension results in the sign bit being copied on
12427 each side.
12429 The difficulty here is that we have predicates for A but not for
12430 (A - C1) so we need to check that C1 is within proper bounds so
12431 as to perturbate A as little as possible. */
12433 if (mode_width <= HOST_BITS_PER_WIDE_INT
12434 && subreg_lowpart_p (op0)
12435 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12436 &inner_mode)
12437 && GET_MODE_PRECISION (inner_mode) > mode_width
12438 && GET_CODE (SUBREG_REG (op0)) == PLUS
12439 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12441 rtx a = XEXP (SUBREG_REG (op0), 0);
12442 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12444 if ((c1 > 0
12445 && (unsigned HOST_WIDE_INT) c1
12446 < HOST_WIDE_INT_1U << (mode_width - 1)
12447 && (equality_comparison_p || unsigned_comparison_p)
12448 /* (A - C1) zero-extends if it is positive and sign-extends
12449 if it is negative, C2 both zero- and sign-extends. */
12450 && (((nonzero_bits (a, inner_mode)
12451 & ~GET_MODE_MASK (mode)) == 0
12452 && const_op >= 0)
12453 /* (A - C1) sign-extends if it is positive and 1-extends
12454 if it is negative, C2 both sign- and 1-extends. */
12455 || (num_sign_bit_copies (a, inner_mode)
12456 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12457 - mode_width)
12458 && const_op < 0)))
12459 || ((unsigned HOST_WIDE_INT) c1
12460 < HOST_WIDE_INT_1U << (mode_width - 2)
12461 /* (A - C1) always sign-extends, like C2. */
12462 && num_sign_bit_copies (a, inner_mode)
12463 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12464 - (mode_width - 1))))
12466 op0 = SUBREG_REG (op0);
12467 continue;
12471 /* If the inner mode is narrower and we are extracting the low part,
12472 we can treat the SUBREG as if it were a ZERO_EXTEND. */
12473 if (paradoxical_subreg_p (op0))
12475 else if (subreg_lowpart_p (op0)
12476 && GET_MODE_CLASS (mode) == MODE_INT
12477 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12478 && (code == NE || code == EQ)
12479 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12480 && !paradoxical_subreg_p (op0)
12481 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12482 & ~GET_MODE_MASK (mode)) == 0)
12484 /* Remove outer subregs that don't do anything. */
12485 tem = gen_lowpart (inner_mode, op1);
12487 if ((nonzero_bits (tem, inner_mode)
12488 & ~GET_MODE_MASK (mode)) == 0)
12490 op0 = SUBREG_REG (op0);
12491 op1 = tem;
12492 continue;
12494 break;
12496 else
12497 break;
12499 /* FALLTHROUGH */
12501 case ZERO_EXTEND:
12502 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12503 && (unsigned_comparison_p || equality_comparison_p)
12504 && HWI_COMPUTABLE_MODE_P (mode)
12505 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12506 && const_op >= 0
12507 && have_insn_for (COMPARE, mode))
12509 op0 = XEXP (op0, 0);
12510 continue;
12512 break;
12514 case PLUS:
12515 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12516 this for equality comparisons due to pathological cases involving
12517 overflows. */
12518 if (equality_comparison_p
12519 && (tem = simplify_binary_operation (MINUS, mode,
12520 op1, XEXP (op0, 1))) != 0)
12522 op0 = XEXP (op0, 0);
12523 op1 = tem;
12524 continue;
12527 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12528 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12529 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12531 op0 = XEXP (XEXP (op0, 0), 0);
12532 code = (code == LT ? EQ : NE);
12533 continue;
12535 break;
12537 case MINUS:
12538 /* We used to optimize signed comparisons against zero, but that
12539 was incorrect. Unsigned comparisons against zero (GTU, LEU)
12540 arrive here as equality comparisons, or (GEU, LTU) are
12541 optimized away. No need to special-case them. */
12543 /* (eq (minus A B) C) -> (eq A (plus B C)) or
12544 (eq B (minus A C)), whichever simplifies. We can only do
12545 this for equality comparisons due to pathological cases involving
12546 overflows. */
12547 if (equality_comparison_p
12548 && (tem = simplify_binary_operation (PLUS, mode,
12549 XEXP (op0, 1), op1)) != 0)
12551 op0 = XEXP (op0, 0);
12552 op1 = tem;
12553 continue;
12556 if (equality_comparison_p
12557 && (tem = simplify_binary_operation (MINUS, mode,
12558 XEXP (op0, 0), op1)) != 0)
12560 op0 = XEXP (op0, 1);
12561 op1 = tem;
12562 continue;
12565 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12566 of bits in X minus 1, is one iff X > 0. */
12567 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12568 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12569 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12570 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12572 op0 = XEXP (op0, 1);
12573 code = (code == GE ? LE : GT);
12574 continue;
12576 break;
12578 case XOR:
12579 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12580 if C is zero or B is a constant. */
12581 if (equality_comparison_p
12582 && (tem = simplify_binary_operation (XOR, mode,
12583 XEXP (op0, 1), op1)) != 0)
12585 op0 = XEXP (op0, 0);
12586 op1 = tem;
12587 continue;
12589 break;
12592 case IOR:
12593 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12594 iff X <= 0. */
12595 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12596 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12597 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12599 op0 = XEXP (op0, 1);
12600 code = (code == GE ? GT : LE);
12601 continue;
12603 break;
12605 case AND:
12606 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12607 will be converted to a ZERO_EXTRACT later. */
12608 if (const_op == 0 && equality_comparison_p
12609 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12610 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12612 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12613 XEXP (XEXP (op0, 0), 1));
12614 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12615 continue;
12618 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12619 zero and X is a comparison and C1 and C2 describe only bits set
12620 in STORE_FLAG_VALUE, we can compare with X. */
12621 if (const_op == 0 && equality_comparison_p
12622 && mode_width <= HOST_BITS_PER_WIDE_INT
12623 && CONST_INT_P (XEXP (op0, 1))
12624 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12625 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12626 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12627 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12629 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12630 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12631 if ((~STORE_FLAG_VALUE & mask) == 0
12632 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12633 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12634 && COMPARISON_P (tem))))
12636 op0 = XEXP (XEXP (op0, 0), 0);
12637 continue;
12641 /* If we are doing an equality comparison of an AND of a bit equal
12642 to the sign bit, replace this with a LT or GE comparison of
12643 the underlying value. */
12644 if (equality_comparison_p
12645 && const_op == 0
12646 && CONST_INT_P (XEXP (op0, 1))
12647 && mode_width <= HOST_BITS_PER_WIDE_INT
12648 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12649 == HOST_WIDE_INT_1U << (mode_width - 1)))
12651 op0 = XEXP (op0, 0);
12652 code = (code == EQ ? GE : LT);
12653 continue;
12656 /* If this AND operation is really a ZERO_EXTEND from a narrower
12657 mode, the constant fits within that mode, and this is either an
12658 equality or unsigned comparison, try to do this comparison in
12659 the narrower mode.
12661 Note that in:
12663 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12664 -> (ne:DI (reg:SI 4) (const_int 0))
12666 unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12667 known to hold a value of the required mode the
12668 transformation is invalid. */
12669 if ((equality_comparison_p || unsigned_comparison_p)
12670 && CONST_INT_P (XEXP (op0, 1))
12671 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12672 & GET_MODE_MASK (mode))
12673 + 1)) >= 0
12674 && const_op >> i == 0
12675 && int_mode_for_size (i, 1).exists (&tmode))
12677 op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12678 continue;
12681 /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12682 fits in both M1 and M2 and the SUBREG is either paradoxical
12683 or represents the low part, permute the SUBREG and the AND
12684 and try again. */
12685 if (GET_CODE (XEXP (op0, 0)) == SUBREG
12686 && CONST_INT_P (XEXP (op0, 1)))
12688 unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12689 /* Require an integral mode, to avoid creating something like
12690 (AND:SF ...). */
12691 if ((is_a <scalar_int_mode>
12692 (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12693 /* It is unsafe to commute the AND into the SUBREG if the
12694 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12695 not defined. As originally written the upper bits
12696 have a defined value due to the AND operation.
12697 However, if we commute the AND inside the SUBREG then
12698 they no longer have defined values and the meaning of
12699 the code has been changed.
12700 Also C1 should not change value in the smaller mode,
12701 see PR67028 (a positive C1 can become negative in the
12702 smaller mode, so that the AND does no longer mask the
12703 upper bits). */
12704 && ((WORD_REGISTER_OPERATIONS
12705 && mode_width > GET_MODE_PRECISION (tmode)
12706 && mode_width <= BITS_PER_WORD
12707 && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12708 || (mode_width <= GET_MODE_PRECISION (tmode)
12709 && subreg_lowpart_p (XEXP (op0, 0))))
12710 && mode_width <= HOST_BITS_PER_WIDE_INT
12711 && HWI_COMPUTABLE_MODE_P (tmode)
12712 && (c1 & ~mask) == 0
12713 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12714 && c1 != mask
12715 && c1 != GET_MODE_MASK (tmode))
12717 op0 = simplify_gen_binary (AND, tmode,
12718 SUBREG_REG (XEXP (op0, 0)),
12719 gen_int_mode (c1, tmode));
12720 op0 = gen_lowpart (mode, op0);
12721 continue;
12725 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12726 if (const_op == 0 && equality_comparison_p
12727 && XEXP (op0, 1) == const1_rtx
12728 && GET_CODE (XEXP (op0, 0)) == NOT)
12730 op0 = simplify_and_const_int (NULL_RTX, mode,
12731 XEXP (XEXP (op0, 0), 0), 1);
12732 code = (code == NE ? EQ : NE);
12733 continue;
12736 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12737 (eq (and (lshiftrt X) 1) 0).
12738 Also handle the case where (not X) is expressed using xor. */
12739 if (const_op == 0 && equality_comparison_p
12740 && XEXP (op0, 1) == const1_rtx
12741 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12743 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12744 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12746 if (GET_CODE (shift_op) == NOT
12747 || (GET_CODE (shift_op) == XOR
12748 && CONST_INT_P (XEXP (shift_op, 1))
12749 && CONST_INT_P (shift_count)
12750 && HWI_COMPUTABLE_MODE_P (mode)
12751 && (UINTVAL (XEXP (shift_op, 1))
12752 == HOST_WIDE_INT_1U
12753 << INTVAL (shift_count))))
12756 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12757 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12758 code = (code == NE ? EQ : NE);
12759 continue;
12762 break;
12764 case ASHIFT:
12765 /* If we have (compare (ashift FOO N) (const_int C)) and
12766 the high order N bits of FOO (N+1 if an inequality comparison)
12767 are known to be zero, we can do this by comparing FOO with C
12768 shifted right N bits so long as the low-order N bits of C are
12769 zero. */
12770 if (CONST_INT_P (XEXP (op0, 1))
12771 && INTVAL (XEXP (op0, 1)) >= 0
12772 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12773 < HOST_BITS_PER_WIDE_INT)
12774 && (((unsigned HOST_WIDE_INT) const_op
12775 & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12776 - 1)) == 0)
12777 && mode_width <= HOST_BITS_PER_WIDE_INT
12778 && (nonzero_bits (XEXP (op0, 0), mode)
12779 & ~(mask >> (INTVAL (XEXP (op0, 1))
12780 + ! equality_comparison_p))) == 0)
12782 /* We must perform a logical shift, not an arithmetic one,
12783 as we want the top N bits of C to be zero. */
12784 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12786 temp >>= INTVAL (XEXP (op0, 1));
12787 op1 = gen_int_mode (temp, mode);
12788 op0 = XEXP (op0, 0);
12789 continue;
12792 /* If we are doing a sign bit comparison, it means we are testing
12793 a particular bit. Convert it to the appropriate AND. */
12794 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12795 && mode_width <= HOST_BITS_PER_WIDE_INT)
12797 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12798 (HOST_WIDE_INT_1U
12799 << (mode_width - 1
12800 - INTVAL (XEXP (op0, 1)))));
12801 code = (code == LT ? NE : EQ);
12802 continue;
12805 /* If this an equality comparison with zero and we are shifting
12806 the low bit to the sign bit, we can convert this to an AND of the
12807 low-order bit. */
12808 if (const_op == 0 && equality_comparison_p
12809 && CONST_INT_P (XEXP (op0, 1))
12810 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12812 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12813 continue;
12815 break;
12817 case ASHIFTRT:
12818 /* If this is an equality comparison with zero, we can do this
12819 as a logical shift, which might be much simpler. */
12820 if (equality_comparison_p && const_op == 0
12821 && CONST_INT_P (XEXP (op0, 1)))
12823 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12824 XEXP (op0, 0),
12825 INTVAL (XEXP (op0, 1)));
12826 continue;
12829 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12830 do the comparison in a narrower mode. */
12831 if (! unsigned_comparison_p
12832 && CONST_INT_P (XEXP (op0, 1))
12833 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12834 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12835 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12836 .exists (&tmode))
12837 && (((unsigned HOST_WIDE_INT) const_op
12838 + (GET_MODE_MASK (tmode) >> 1) + 1)
12839 <= GET_MODE_MASK (tmode)))
12841 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12842 continue;
12845 /* Likewise if OP0 is a PLUS of a sign extension with a
12846 constant, which is usually represented with the PLUS
12847 between the shifts. */
12848 if (! unsigned_comparison_p
12849 && CONST_INT_P (XEXP (op0, 1))
12850 && GET_CODE (XEXP (op0, 0)) == PLUS
12851 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12852 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12853 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12854 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12855 .exists (&tmode))
12856 && (((unsigned HOST_WIDE_INT) const_op
12857 + (GET_MODE_MASK (tmode) >> 1) + 1)
12858 <= GET_MODE_MASK (tmode)))
12860 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12861 rtx add_const = XEXP (XEXP (op0, 0), 1);
12862 rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12863 add_const, XEXP (op0, 1));
12865 op0 = simplify_gen_binary (PLUS, tmode,
12866 gen_lowpart (tmode, inner),
12867 new_const);
12868 continue;
12871 /* FALLTHROUGH */
12872 case LSHIFTRT:
12873 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12874 the low order N bits of FOO are known to be zero, we can do this
12875 by comparing FOO with C shifted left N bits so long as no
12876 overflow occurs. Even if the low order N bits of FOO aren't known
12877 to be zero, if the comparison is >= or < we can use the same
12878 optimization and for > or <= by setting all the low
12879 order N bits in the comparison constant. */
12880 if (CONST_INT_P (XEXP (op0, 1))
12881 && INTVAL (XEXP (op0, 1)) > 0
12882 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12883 && mode_width <= HOST_BITS_PER_WIDE_INT
12884 && (((unsigned HOST_WIDE_INT) const_op
12885 + (GET_CODE (op0) != LSHIFTRT
12886 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12887 + 1)
12888 : 0))
12889 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12891 unsigned HOST_WIDE_INT low_bits
12892 = (nonzero_bits (XEXP (op0, 0), mode)
12893 & ((HOST_WIDE_INT_1U
12894 << INTVAL (XEXP (op0, 1))) - 1));
12895 if (low_bits == 0 || !equality_comparison_p)
12897 /* If the shift was logical, then we must make the condition
12898 unsigned. */
12899 if (GET_CODE (op0) == LSHIFTRT)
12900 code = unsigned_condition (code);
12902 const_op = (unsigned HOST_WIDE_INT) const_op
12903 << INTVAL (XEXP (op0, 1));
12904 if (low_bits != 0
12905 && (code == GT || code == GTU
12906 || code == LE || code == LEU))
12907 const_op
12908 |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12909 op1 = GEN_INT (const_op);
12910 op0 = XEXP (op0, 0);
12911 continue;
12915 /* If we are using this shift to extract just the sign bit, we
12916 can replace this with an LT or GE comparison. */
12917 if (const_op == 0
12918 && (equality_comparison_p || sign_bit_comparison_p)
12919 && CONST_INT_P (XEXP (op0, 1))
12920 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12922 op0 = XEXP (op0, 0);
12923 code = (code == NE || code == GT ? LT : GE);
12924 continue;
12926 break;
12928 default:
12929 break;
12932 break;
12935 /* Now make any compound operations involved in this comparison. Then,
12936 check for an outmost SUBREG on OP0 that is not doing anything or is
12937 paradoxical. The latter transformation must only be performed when
12938 it is known that the "extra" bits will be the same in op0 and op1 or
12939 that they don't matter. There are three cases to consider:
12941 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12942 care bits and we can assume they have any convenient value. So
12943 making the transformation is safe.
12945 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12946 In this case the upper bits of op0 are undefined. We should not make
12947 the simplification in that case as we do not know the contents of
12948 those bits.
12950 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12951 In that case we know those bits are zeros or ones. We must also be
12952 sure that they are the same as the upper bits of op1.
12954 We can never remove a SUBREG for a non-equality comparison because
12955 the sign bit is in a different place in the underlying object. */
12957 rtx_code op0_mco_code = SET;
12958 if (op1 == const0_rtx)
12959 op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12961 op0 = make_compound_operation (op0, op0_mco_code);
12962 op1 = make_compound_operation (op1, SET);
12964 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12965 && is_int_mode (GET_MODE (op0), &mode)
12966 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12967 && (code == NE || code == EQ))
12969 if (paradoxical_subreg_p (op0))
12971 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12972 implemented. */
12973 if (REG_P (SUBREG_REG (op0)))
12975 op0 = SUBREG_REG (op0);
12976 op1 = gen_lowpart (inner_mode, op1);
12979 else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12980 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12981 & ~GET_MODE_MASK (mode)) == 0)
12983 tem = gen_lowpart (inner_mode, op1);
12985 if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
12986 op0 = SUBREG_REG (op0), op1 = tem;
12990 /* We now do the opposite procedure: Some machines don't have compare
12991 insns in all modes. If OP0's mode is an integer mode smaller than a
12992 word and we can't do a compare in that mode, see if there is a larger
12993 mode for which we can do the compare. There are a number of cases in
12994 which we can use the wider mode. */
12996 if (is_int_mode (GET_MODE (op0), &mode)
12997 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12998 && ! have_insn_for (COMPARE, mode))
12999 FOR_EACH_WIDER_MODE (tmode_iter, mode)
13001 tmode = tmode_iter.require ();
13002 if (!HWI_COMPUTABLE_MODE_P (tmode))
13003 break;
13004 if (have_insn_for (COMPARE, tmode))
13006 int zero_extended;
13008 /* If this is a test for negative, we can make an explicit
13009 test of the sign bit. Test this first so we can use
13010 a paradoxical subreg to extend OP0. */
13012 if (op1 == const0_rtx && (code == LT || code == GE)
13013 && HWI_COMPUTABLE_MODE_P (mode))
13015 unsigned HOST_WIDE_INT sign
13016 = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
13017 op0 = simplify_gen_binary (AND, tmode,
13018 gen_lowpart (tmode, op0),
13019 gen_int_mode (sign, tmode));
13020 code = (code == LT) ? NE : EQ;
13021 break;
13024 /* If the only nonzero bits in OP0 and OP1 are those in the
13025 narrower mode and this is an equality or unsigned comparison,
13026 we can use the wider mode. Similarly for sign-extended
13027 values, in which case it is true for all comparisons. */
13028 zero_extended = ((code == EQ || code == NE
13029 || code == GEU || code == GTU
13030 || code == LEU || code == LTU)
13031 && (nonzero_bits (op0, tmode)
13032 & ~GET_MODE_MASK (mode)) == 0
13033 && ((CONST_INT_P (op1)
13034 || (nonzero_bits (op1, tmode)
13035 & ~GET_MODE_MASK (mode)) == 0)));
13037 if (zero_extended
13038 || ((num_sign_bit_copies (op0, tmode)
13039 > (unsigned int) (GET_MODE_PRECISION (tmode)
13040 - GET_MODE_PRECISION (mode)))
13041 && (num_sign_bit_copies (op1, tmode)
13042 > (unsigned int) (GET_MODE_PRECISION (tmode)
13043 - GET_MODE_PRECISION (mode)))))
13045 /* If OP0 is an AND and we don't have an AND in MODE either,
13046 make a new AND in the proper mode. */
13047 if (GET_CODE (op0) == AND
13048 && !have_insn_for (AND, mode))
13049 op0 = simplify_gen_binary (AND, tmode,
13050 gen_lowpart (tmode,
13051 XEXP (op0, 0)),
13052 gen_lowpart (tmode,
13053 XEXP (op0, 1)));
13054 else
13056 if (zero_extended)
13058 op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
13059 op0, mode);
13060 op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
13061 op1, mode);
13063 else
13065 op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
13066 op0, mode);
13067 op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
13068 op1, mode);
13070 break;
13076 /* We may have changed the comparison operands. Re-canonicalize. */
13077 if (swap_commutative_operands_p (op0, op1))
13079 std::swap (op0, op1);
13080 code = swap_condition (code);
13083 /* If this machine only supports a subset of valid comparisons, see if we
13084 can convert an unsupported one into a supported one. */
13085 target_canonicalize_comparison (&code, &op0, &op1, 0);
13087 *pop0 = op0;
13088 *pop1 = op1;
13090 return code;
13093 /* Utility function for record_value_for_reg. Count number of
13094 rtxs in X. */
13095 static int
13096 count_rtxs (rtx x)
13098 enum rtx_code code = GET_CODE (x);
13099 const char *fmt;
13100 int i, j, ret = 1;
13102 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
13103 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
13105 rtx x0 = XEXP (x, 0);
13106 rtx x1 = XEXP (x, 1);
13108 if (x0 == x1)
13109 return 1 + 2 * count_rtxs (x0);
13111 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
13112 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
13113 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13114 return 2 + 2 * count_rtxs (x0)
13115 + count_rtxs (x == XEXP (x1, 0)
13116 ? XEXP (x1, 1) : XEXP (x1, 0));
13118 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
13119 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
13120 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13121 return 2 + 2 * count_rtxs (x1)
13122 + count_rtxs (x == XEXP (x0, 0)
13123 ? XEXP (x0, 1) : XEXP (x0, 0));
13126 fmt = GET_RTX_FORMAT (code);
13127 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13128 if (fmt[i] == 'e')
13129 ret += count_rtxs (XEXP (x, i));
13130 else if (fmt[i] == 'E')
13131 for (j = 0; j < XVECLEN (x, i); j++)
13132 ret += count_rtxs (XVECEXP (x, i, j));
13134 return ret;
13137 /* Utility function for following routine. Called when X is part of a value
13138 being stored into last_set_value. Sets last_set_table_tick
13139 for each register mentioned. Similar to mention_regs in cse.cc */
13141 static void
13142 update_table_tick (rtx x)
13144 enum rtx_code code = GET_CODE (x);
13145 const char *fmt = GET_RTX_FORMAT (code);
13146 int i, j;
13148 if (code == REG)
13150 unsigned int regno = REGNO (x);
13151 unsigned int endregno = END_REGNO (x);
13152 unsigned int r;
13154 for (r = regno; r < endregno; r++)
13156 reg_stat_type *rsp = &reg_stat[r];
13157 rsp->last_set_table_tick = label_tick;
13160 return;
13163 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13164 if (fmt[i] == 'e')
13166 /* Check for identical subexpressions. If x contains
13167 identical subexpression we only have to traverse one of
13168 them. */
13169 if (i == 0 && ARITHMETIC_P (x))
13171 /* Note that at this point x1 has already been
13172 processed. */
13173 rtx x0 = XEXP (x, 0);
13174 rtx x1 = XEXP (x, 1);
13176 /* If x0 and x1 are identical then there is no need to
13177 process x0. */
13178 if (x0 == x1)
13179 break;
13181 /* If x0 is identical to a subexpression of x1 then while
13182 processing x1, x0 has already been processed. Thus we
13183 are done with x. */
13184 if (ARITHMETIC_P (x1)
13185 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13186 break;
13188 /* If x1 is identical to a subexpression of x0 then we
13189 still have to process the rest of x0. */
13190 if (ARITHMETIC_P (x0)
13191 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13193 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13194 break;
13198 update_table_tick (XEXP (x, i));
13200 else if (fmt[i] == 'E')
13201 for (j = 0; j < XVECLEN (x, i); j++)
13202 update_table_tick (XVECEXP (x, i, j));
13205 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13206 are saying that the register is clobbered and we no longer know its
13207 value. If INSN is zero, don't update reg_stat[].last_set; this is
13208 only permitted with VALUE also zero and is used to invalidate the
13209 register. */
13211 static void
13212 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13214 unsigned int regno = REGNO (reg);
13215 unsigned int endregno = END_REGNO (reg);
13216 unsigned int i;
13217 reg_stat_type *rsp;
13219 /* If VALUE contains REG and we have a previous value for REG, substitute
13220 the previous value. */
13221 if (value && insn && reg_overlap_mentioned_p (reg, value))
13223 rtx tem;
13225 /* Set things up so get_last_value is allowed to see anything set up to
13226 our insn. */
13227 subst_low_luid = DF_INSN_LUID (insn);
13228 tem = get_last_value (reg);
13230 /* If TEM is simply a binary operation with two CLOBBERs as operands,
13231 it isn't going to be useful and will take a lot of time to process,
13232 so just use the CLOBBER. */
13234 if (tem)
13236 if (ARITHMETIC_P (tem)
13237 && GET_CODE (XEXP (tem, 0)) == CLOBBER
13238 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13239 tem = XEXP (tem, 0);
13240 else if (count_occurrences (value, reg, 1) >= 2)
13242 /* If there are two or more occurrences of REG in VALUE,
13243 prevent the value from growing too much. */
13244 if (count_rtxs (tem) > param_max_last_value_rtl)
13245 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13248 value = replace_rtx (copy_rtx (value), reg, tem);
13252 /* For each register modified, show we don't know its value, that
13253 we don't know about its bitwise content, that its value has been
13254 updated, and that we don't know the location of the death of the
13255 register. */
13256 for (i = regno; i < endregno; i++)
13258 rsp = &reg_stat[i];
13260 if (insn)
13261 rsp->last_set = insn;
13263 rsp->last_set_value = 0;
13264 rsp->last_set_mode = VOIDmode;
13265 rsp->last_set_nonzero_bits = 0;
13266 rsp->last_set_sign_bit_copies = 0;
13267 rsp->last_death = 0;
13268 rsp->truncated_to_mode = VOIDmode;
13271 /* Mark registers that are being referenced in this value. */
13272 if (value)
13273 update_table_tick (value);
13275 /* Now update the status of each register being set.
13276 If someone is using this register in this block, set this register
13277 to invalid since we will get confused between the two lives in this
13278 basic block. This makes using this register always invalid. In cse, we
13279 scan the table to invalidate all entries using this register, but this
13280 is too much work for us. */
13282 for (i = regno; i < endregno; i++)
13284 rsp = &reg_stat[i];
13285 rsp->last_set_label = label_tick;
13286 if (!insn
13287 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13288 rsp->last_set_invalid = true;
13289 else
13290 rsp->last_set_invalid = false;
13293 /* The value being assigned might refer to X (like in "x++;"). In that
13294 case, we must replace it with (clobber (const_int 0)) to prevent
13295 infinite loops. */
13296 rsp = &reg_stat[regno];
13297 if (value && !get_last_value_validate (&value, insn, label_tick, false))
13299 value = copy_rtx (value);
13300 if (!get_last_value_validate (&value, insn, label_tick, true))
13301 value = 0;
13304 /* For the main register being modified, update the value, the mode, the
13305 nonzero bits, and the number of sign bit copies. */
13307 rsp->last_set_value = value;
13309 if (value)
13311 machine_mode mode = GET_MODE (reg);
13312 subst_low_luid = DF_INSN_LUID (insn);
13313 rsp->last_set_mode = mode;
13314 if (GET_MODE_CLASS (mode) == MODE_INT
13315 && HWI_COMPUTABLE_MODE_P (mode))
13316 mode = nonzero_bits_mode;
13317 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13318 rsp->last_set_sign_bit_copies
13319 = num_sign_bit_copies (value, GET_MODE (reg));
13323 /* Called via note_stores from record_dead_and_set_regs to handle one
13324 SET or CLOBBER in an insn. DATA is the instruction in which the
13325 set is occurring. */
13327 static void
13328 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13330 rtx_insn *record_dead_insn = (rtx_insn *) data;
13332 if (GET_CODE (dest) == SUBREG)
13333 dest = SUBREG_REG (dest);
13335 if (!record_dead_insn)
13337 if (REG_P (dest))
13338 record_value_for_reg (dest, NULL, NULL_RTX);
13339 return;
13342 if (REG_P (dest))
13344 /* If we are setting the whole register, we know its value. Otherwise
13345 show that we don't know the value. We can handle a SUBREG if it's
13346 the low part, but we must be careful with paradoxical SUBREGs on
13347 RISC architectures because we cannot strip e.g. an extension around
13348 a load and record the naked load since the RTL middle-end considers
13349 that the upper bits are defined according to LOAD_EXTEND_OP. */
13350 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13351 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13352 else if (GET_CODE (setter) == SET
13353 && GET_CODE (SET_DEST (setter)) == SUBREG
13354 && SUBREG_REG (SET_DEST (setter)) == dest
13355 && known_le (GET_MODE_PRECISION (GET_MODE (dest)),
13356 BITS_PER_WORD)
13357 && subreg_lowpart_p (SET_DEST (setter)))
13358 record_value_for_reg (dest, record_dead_insn,
13359 WORD_REGISTER_OPERATIONS
13360 && word_register_operation_p (SET_SRC (setter))
13361 && paradoxical_subreg_p (SET_DEST (setter))
13362 ? SET_SRC (setter)
13363 : gen_lowpart (GET_MODE (dest),
13364 SET_SRC (setter)));
13365 else
13366 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13368 else if (MEM_P (dest)
13369 /* Ignore pushes, they clobber nothing. */
13370 && ! push_operand (dest, GET_MODE (dest)))
13371 mem_last_set = DF_INSN_LUID (record_dead_insn);
13374 /* Update the records of when each REG was most recently set or killed
13375 for the things done by INSN. This is the last thing done in processing
13376 INSN in the combiner loop.
13378 We update reg_stat[], in particular fields last_set, last_set_value,
13379 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13380 last_death, and also the similar information mem_last_set (which insn
13381 most recently modified memory) and last_call_luid (which insn was the
13382 most recent subroutine call). */
13384 static void
13385 record_dead_and_set_regs (rtx_insn *insn)
13387 rtx link;
13388 unsigned int i;
13390 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13392 if (REG_NOTE_KIND (link) == REG_DEAD
13393 && REG_P (XEXP (link, 0)))
13395 unsigned int regno = REGNO (XEXP (link, 0));
13396 unsigned int endregno = END_REGNO (XEXP (link, 0));
13398 for (i = regno; i < endregno; i++)
13400 reg_stat_type *rsp;
13402 rsp = &reg_stat[i];
13403 rsp->last_death = insn;
13406 else if (REG_NOTE_KIND (link) == REG_INC)
13407 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13410 if (CALL_P (insn))
13412 HARD_REG_SET callee_clobbers
13413 = insn_callee_abi (insn).full_and_partial_reg_clobbers ();
13414 hard_reg_set_iterator hrsi;
13415 EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers, 0, i, hrsi)
13417 reg_stat_type *rsp;
13419 /* ??? We could try to preserve some information from the last
13420 set of register I if the call doesn't actually clobber
13421 (reg:last_set_mode I), which might be true for ABIs with
13422 partial clobbers. However, it would be difficult to
13423 update last_set_nonzero_bits and last_sign_bit_copies
13424 to account for the part of I that actually was clobbered.
13425 It wouldn't help much anyway, since we rarely see this
13426 situation before RA. */
13427 rsp = &reg_stat[i];
13428 rsp->last_set_invalid = true;
13429 rsp->last_set = insn;
13430 rsp->last_set_value = 0;
13431 rsp->last_set_mode = VOIDmode;
13432 rsp->last_set_nonzero_bits = 0;
13433 rsp->last_set_sign_bit_copies = 0;
13434 rsp->last_death = 0;
13435 rsp->truncated_to_mode = VOIDmode;
13438 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13440 /* We can't combine into a call pattern. Remember, though, that
13441 the return value register is set at this LUID. We could
13442 still replace a register with the return value from the
13443 wrong subroutine call! */
13444 note_stores (insn, record_dead_and_set_regs_1, NULL_RTX);
13446 else
13447 note_stores (insn, record_dead_and_set_regs_1, insn);
13450 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13451 register present in the SUBREG, so for each such SUBREG go back and
13452 adjust nonzero and sign bit information of the registers that are
13453 known to have some zero/sign bits set.
13455 This is needed because when combine blows the SUBREGs away, the
13456 information on zero/sign bits is lost and further combines can be
13457 missed because of that. */
13459 static void
13460 record_promoted_value (rtx_insn *insn, rtx subreg)
13462 struct insn_link *links;
13463 rtx set;
13464 unsigned int regno = REGNO (SUBREG_REG (subreg));
13465 machine_mode mode = GET_MODE (subreg);
13467 if (!HWI_COMPUTABLE_MODE_P (mode))
13468 return;
13470 for (links = LOG_LINKS (insn); links;)
13472 reg_stat_type *rsp;
13474 insn = links->insn;
13475 set = single_set (insn);
13477 if (! set || !REG_P (SET_DEST (set))
13478 || REGNO (SET_DEST (set)) != regno
13479 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13481 links = links->next;
13482 continue;
13485 rsp = &reg_stat[regno];
13486 if (rsp->last_set == insn)
13488 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13489 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13492 if (REG_P (SET_SRC (set)))
13494 regno = REGNO (SET_SRC (set));
13495 links = LOG_LINKS (insn);
13497 else
13498 break;
13502 /* Check if X, a register, is known to contain a value already
13503 truncated to MODE. In this case we can use a subreg to refer to
13504 the truncated value even though in the generic case we would need
13505 an explicit truncation. */
13507 static bool
13508 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13510 reg_stat_type *rsp = &reg_stat[REGNO (x)];
13511 machine_mode truncated = rsp->truncated_to_mode;
13513 if (truncated == 0
13514 || rsp->truncation_label < label_tick_ebb_start)
13515 return false;
13516 if (!partial_subreg_p (mode, truncated))
13517 return true;
13518 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13519 return true;
13520 return false;
13523 /* If X is a hard reg or a subreg record the mode that the register is
13524 accessed in. For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13525 able to turn a truncate into a subreg using this information. Return true
13526 if traversing X is complete. */
13528 static bool
13529 record_truncated_value (rtx x)
13531 machine_mode truncated_mode;
13532 reg_stat_type *rsp;
13534 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13536 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13537 truncated_mode = GET_MODE (x);
13539 if (!partial_subreg_p (truncated_mode, original_mode))
13540 return true;
13542 truncated_mode = GET_MODE (x);
13543 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13544 return true;
13546 x = SUBREG_REG (x);
13548 /* ??? For hard-regs we now record everything. We might be able to
13549 optimize this using last_set_mode. */
13550 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13551 truncated_mode = GET_MODE (x);
13552 else
13553 return false;
13555 rsp = &reg_stat[REGNO (x)];
13556 if (rsp->truncated_to_mode == 0
13557 || rsp->truncation_label < label_tick_ebb_start
13558 || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13560 rsp->truncated_to_mode = truncated_mode;
13561 rsp->truncation_label = label_tick;
13564 return true;
13567 /* Callback for note_uses. Find hardregs and subregs of pseudos and
13568 the modes they are used in. This can help truning TRUNCATEs into
13569 SUBREGs. */
13571 static void
13572 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13574 subrtx_var_iterator::array_type array;
13575 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13576 if (record_truncated_value (*iter))
13577 iter.skip_subrtxes ();
13580 /* Scan X for promoted SUBREGs. For each one found,
13581 note what it implies to the registers used in it. */
13583 static void
13584 check_promoted_subreg (rtx_insn *insn, rtx x)
13586 if (GET_CODE (x) == SUBREG
13587 && SUBREG_PROMOTED_VAR_P (x)
13588 && REG_P (SUBREG_REG (x)))
13589 record_promoted_value (insn, x);
13590 else
13592 const char *format = GET_RTX_FORMAT (GET_CODE (x));
13593 int i, j;
13595 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13596 switch (format[i])
13598 case 'e':
13599 check_promoted_subreg (insn, XEXP (x, i));
13600 break;
13601 case 'V':
13602 case 'E':
13603 if (XVEC (x, i) != 0)
13604 for (j = 0; j < XVECLEN (x, i); j++)
13605 check_promoted_subreg (insn, XVECEXP (x, i, j));
13606 break;
13611 /* Verify that all the registers and memory references mentioned in *LOC are
13612 still valid. *LOC was part of a value set in INSN when label_tick was
13613 equal to TICK. Return false if some are not. If REPLACE is true, replace
13614 the invalid references with (clobber (const_int 0)) and return true. This
13615 replacement is useful because we often can get useful information about
13616 the form of a value (e.g., if it was produced by a shift that always
13617 produces -1 or 0) even though we don't know exactly what registers it
13618 was produced from. */
13620 static bool
13621 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, bool replace)
13623 rtx x = *loc;
13624 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13625 int len = GET_RTX_LENGTH (GET_CODE (x));
13626 int i, j;
13628 if (REG_P (x))
13630 unsigned int regno = REGNO (x);
13631 unsigned int endregno = END_REGNO (x);
13632 unsigned int j;
13634 for (j = regno; j < endregno; j++)
13636 reg_stat_type *rsp = &reg_stat[j];
13637 if (rsp->last_set_invalid
13638 /* If this is a pseudo-register that was only set once and not
13639 live at the beginning of the function, it is always valid. */
13640 || (! (regno >= FIRST_PSEUDO_REGISTER
13641 && regno < reg_n_sets_max
13642 && REG_N_SETS (regno) == 1
13643 && (!REGNO_REG_SET_P
13644 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13645 regno)))
13646 && rsp->last_set_label > tick))
13648 if (replace)
13649 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13650 return replace;
13654 return true;
13656 /* If this is a memory reference, make sure that there were no stores after
13657 it that might have clobbered the value. We don't have alias info, so we
13658 assume any store invalidates it. Moreover, we only have local UIDs, so
13659 we also assume that there were stores in the intervening basic blocks. */
13660 else if (MEM_P (x) && !MEM_READONLY_P (x)
13661 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13663 if (replace)
13664 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13665 return replace;
13668 for (i = 0; i < len; i++)
13670 if (fmt[i] == 'e')
13672 /* Check for identical subexpressions. If x contains
13673 identical subexpression we only have to traverse one of
13674 them. */
13675 if (i == 1 && ARITHMETIC_P (x))
13677 /* Note that at this point x0 has already been checked
13678 and found valid. */
13679 rtx x0 = XEXP (x, 0);
13680 rtx x1 = XEXP (x, 1);
13682 /* If x0 and x1 are identical then x is also valid. */
13683 if (x0 == x1)
13684 return true;
13686 /* If x1 is identical to a subexpression of x0 then
13687 while checking x0, x1 has already been checked. Thus
13688 it is valid and so as x. */
13689 if (ARITHMETIC_P (x0)
13690 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13691 return true;
13693 /* If x0 is identical to a subexpression of x1 then x is
13694 valid iff the rest of x1 is valid. */
13695 if (ARITHMETIC_P (x1)
13696 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13697 return
13698 get_last_value_validate (&XEXP (x1,
13699 x0 == XEXP (x1, 0) ? 1 : 0),
13700 insn, tick, replace);
13703 if (!get_last_value_validate (&XEXP (x, i), insn, tick, replace))
13704 return false;
13706 else if (fmt[i] == 'E')
13707 for (j = 0; j < XVECLEN (x, i); j++)
13708 if (!get_last_value_validate (&XVECEXP (x, i, j),
13709 insn, tick, replace))
13710 return false;
13713 /* If we haven't found a reason for it to be invalid, it is valid. */
13714 return true;
13717 /* Get the last value assigned to X, if known. Some registers
13718 in the value may be replaced with (clobber (const_int 0)) if their value
13719 is known longer known reliably. */
13721 static rtx
13722 get_last_value (const_rtx x)
13724 unsigned int regno;
13725 rtx value;
13726 reg_stat_type *rsp;
13728 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13729 then convert it to the desired mode. If this is a paradoxical SUBREG,
13730 we cannot predict what values the "extra" bits might have. */
13731 if (GET_CODE (x) == SUBREG
13732 && subreg_lowpart_p (x)
13733 && !paradoxical_subreg_p (x)
13734 && (value = get_last_value (SUBREG_REG (x))) != 0)
13735 return gen_lowpart (GET_MODE (x), value);
13737 if (!REG_P (x))
13738 return 0;
13740 regno = REGNO (x);
13741 rsp = &reg_stat[regno];
13742 value = rsp->last_set_value;
13744 /* If we don't have a value, or if it isn't for this basic block and
13745 it's either a hard register, set more than once, or it's a live
13746 at the beginning of the function, return 0.
13748 Because if it's not live at the beginning of the function then the reg
13749 is always set before being used (is never used without being set).
13750 And, if it's set only once, and it's always set before use, then all
13751 uses must have the same last value, even if it's not from this basic
13752 block. */
13754 if (value == 0
13755 || (rsp->last_set_label < label_tick_ebb_start
13756 && (regno < FIRST_PSEUDO_REGISTER
13757 || regno >= reg_n_sets_max
13758 || REG_N_SETS (regno) != 1
13759 || REGNO_REG_SET_P
13760 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13761 return 0;
13763 /* If the value was set in a later insn than the ones we are processing,
13764 we can't use it even if the register was only set once. */
13765 if (rsp->last_set_label == label_tick
13766 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13767 return 0;
13769 /* If fewer bits were set than what we are asked for now, we cannot use
13770 the value. */
13771 if (maybe_lt (GET_MODE_PRECISION (rsp->last_set_mode),
13772 GET_MODE_PRECISION (GET_MODE (x))))
13773 return 0;
13775 /* If the value has all its registers valid, return it. */
13776 if (get_last_value_validate (&value, rsp->last_set,
13777 rsp->last_set_label, false))
13778 return value;
13780 /* Otherwise, make a copy and replace any invalid register with
13781 (clobber (const_int 0)). If that fails for some reason, return 0. */
13783 value = copy_rtx (value);
13784 if (get_last_value_validate (&value, rsp->last_set,
13785 rsp->last_set_label, true))
13786 return value;
13788 return 0;
13791 /* Define three variables used for communication between the following
13792 routines. */
13794 static unsigned int reg_dead_regno, reg_dead_endregno;
13795 static int reg_dead_flag;
13796 rtx reg_dead_reg;
13798 /* Function called via note_stores from reg_dead_at_p.
13800 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13801 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13803 static void
13804 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13806 unsigned int regno, endregno;
13808 if (!REG_P (dest))
13809 return;
13811 regno = REGNO (dest);
13812 endregno = END_REGNO (dest);
13813 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13814 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13817 /* Return true if REG is known to be dead at INSN.
13819 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13820 referencing REG, it is dead. If we hit a SET referencing REG, it is
13821 live. Otherwise, see if it is live or dead at the start of the basic
13822 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13823 must be assumed to be always live. */
13825 static bool
13826 reg_dead_at_p (rtx reg, rtx_insn *insn)
13828 basic_block block;
13829 unsigned int i;
13831 /* Set variables for reg_dead_at_p_1. */
13832 reg_dead_regno = REGNO (reg);
13833 reg_dead_endregno = END_REGNO (reg);
13834 reg_dead_reg = reg;
13836 reg_dead_flag = 0;
13838 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13839 we allow the machine description to decide whether use-and-clobber
13840 patterns are OK. */
13841 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13843 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13844 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13845 return false;
13848 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13849 beginning of basic block. */
13850 block = BLOCK_FOR_INSN (insn);
13851 for (;;)
13853 if (INSN_P (insn))
13855 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13856 return true;
13858 note_stores (insn, reg_dead_at_p_1, NULL);
13859 if (reg_dead_flag)
13860 return reg_dead_flag == 1 ? 1 : 0;
13862 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13863 return true;
13866 if (insn == BB_HEAD (block))
13867 break;
13869 insn = PREV_INSN (insn);
13872 /* Look at live-in sets for the basic block that we were in. */
13873 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13874 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13875 return false;
13877 return true;
13880 /* Note hard registers in X that are used. */
13882 static void
13883 mark_used_regs_combine (rtx x)
13885 RTX_CODE code = GET_CODE (x);
13886 unsigned int regno;
13887 int i;
13889 switch (code)
13891 case LABEL_REF:
13892 case SYMBOL_REF:
13893 case CONST:
13894 CASE_CONST_ANY:
13895 case PC:
13896 case ADDR_VEC:
13897 case ADDR_DIFF_VEC:
13898 case ASM_INPUT:
13899 return;
13901 case CLOBBER:
13902 /* If we are clobbering a MEM, mark any hard registers inside the
13903 address as used. */
13904 if (MEM_P (XEXP (x, 0)))
13905 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13906 return;
13908 case REG:
13909 regno = REGNO (x);
13910 /* A hard reg in a wide mode may really be multiple registers.
13911 If so, mark all of them just like the first. */
13912 if (regno < FIRST_PSEUDO_REGISTER)
13914 /* None of this applies to the stack, frame or arg pointers. */
13915 if (regno == STACK_POINTER_REGNUM
13916 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13917 && regno == HARD_FRAME_POINTER_REGNUM)
13918 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13919 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13920 || regno == FRAME_POINTER_REGNUM)
13921 return;
13923 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13925 return;
13927 case SET:
13929 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13930 the address. */
13931 rtx testreg = SET_DEST (x);
13933 while (GET_CODE (testreg) == SUBREG
13934 || GET_CODE (testreg) == ZERO_EXTRACT
13935 || GET_CODE (testreg) == STRICT_LOW_PART)
13936 testreg = XEXP (testreg, 0);
13938 if (MEM_P (testreg))
13939 mark_used_regs_combine (XEXP (testreg, 0));
13941 mark_used_regs_combine (SET_SRC (x));
13943 return;
13945 default:
13946 break;
13949 /* Recursively scan the operands of this expression. */
13952 const char *fmt = GET_RTX_FORMAT (code);
13954 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13956 if (fmt[i] == 'e')
13957 mark_used_regs_combine (XEXP (x, i));
13958 else if (fmt[i] == 'E')
13960 int j;
13962 for (j = 0; j < XVECLEN (x, i); j++)
13963 mark_used_regs_combine (XVECEXP (x, i, j));
13969 /* Remove register number REGNO from the dead registers list of INSN.
13971 Return the note used to record the death, if there was one. */
13974 remove_death (unsigned int regno, rtx_insn *insn)
13976 rtx note = find_regno_note (insn, REG_DEAD, regno);
13978 if (note)
13979 remove_note (insn, note);
13981 return note;
13984 /* For each register (hardware or pseudo) used within expression X, if its
13985 death is in an instruction with luid between FROM_LUID (inclusive) and
13986 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13987 list headed by PNOTES.
13989 That said, don't move registers killed by maybe_kill_insn.
13991 This is done when X is being merged by combination into TO_INSN. These
13992 notes will then be distributed as needed. */
13994 static void
13995 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13996 rtx *pnotes)
13998 const char *fmt;
13999 int len, i;
14000 enum rtx_code code = GET_CODE (x);
14002 if (code == REG)
14004 unsigned int regno = REGNO (x);
14005 rtx_insn *where_dead = reg_stat[regno].last_death;
14007 /* If we do not know where the register died, it may still die between
14008 FROM_LUID and TO_INSN. If so, find it. This is PR83304. */
14009 if (!where_dead || DF_INSN_LUID (where_dead) >= DF_INSN_LUID (to_insn))
14011 rtx_insn *insn = prev_real_nondebug_insn (to_insn);
14012 while (insn
14013 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (to_insn)
14014 && DF_INSN_LUID (insn) >= from_luid)
14016 if (dead_or_set_regno_p (insn, regno))
14018 if (find_regno_note (insn, REG_DEAD, regno))
14019 where_dead = insn;
14020 break;
14023 insn = prev_real_nondebug_insn (insn);
14027 /* Don't move the register if it gets killed in between from and to. */
14028 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
14029 && ! reg_referenced_p (x, maybe_kill_insn))
14030 return;
14032 if (where_dead
14033 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
14034 && DF_INSN_LUID (where_dead) >= from_luid
14035 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
14037 rtx note = remove_death (regno, where_dead);
14039 /* It is possible for the call above to return 0. This can occur
14040 when last_death points to I2 or I1 that we combined with.
14041 In that case make a new note.
14043 We must also check for the case where X is a hard register
14044 and NOTE is a death note for a range of hard registers
14045 including X. In that case, we must put REG_DEAD notes for
14046 the remaining registers in place of NOTE. */
14048 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
14049 && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
14051 unsigned int deadregno = REGNO (XEXP (note, 0));
14052 unsigned int deadend = END_REGNO (XEXP (note, 0));
14053 unsigned int ourend = END_REGNO (x);
14054 unsigned int i;
14056 for (i = deadregno; i < deadend; i++)
14057 if (i < regno || i >= ourend)
14058 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
14061 /* If we didn't find any note, or if we found a REG_DEAD note that
14062 covers only part of the given reg, and we have a multi-reg hard
14063 register, then to be safe we must check for REG_DEAD notes
14064 for each register other than the first. They could have
14065 their own REG_DEAD notes lying around. */
14066 else if ((note == 0
14067 || (note != 0
14068 && partial_subreg_p (GET_MODE (XEXP (note, 0)),
14069 GET_MODE (x))))
14070 && regno < FIRST_PSEUDO_REGISTER
14071 && REG_NREGS (x) > 1)
14073 unsigned int ourend = END_REGNO (x);
14074 unsigned int i, offset;
14075 rtx oldnotes = 0;
14077 if (note)
14078 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
14079 else
14080 offset = 1;
14082 for (i = regno + offset; i < ourend; i++)
14083 move_deaths (regno_reg_rtx[i],
14084 maybe_kill_insn, from_luid, to_insn, &oldnotes);
14087 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
14089 XEXP (note, 1) = *pnotes;
14090 *pnotes = note;
14092 else
14093 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
14096 return;
14099 else if (GET_CODE (x) == SET)
14101 rtx dest = SET_DEST (x);
14103 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
14105 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
14106 that accesses one word of a multi-word item, some
14107 piece of everything register in the expression is used by
14108 this insn, so remove any old death. */
14109 /* ??? So why do we test for equality of the sizes? */
14111 if (GET_CODE (dest) == ZERO_EXTRACT
14112 || GET_CODE (dest) == STRICT_LOW_PART
14113 || (GET_CODE (dest) == SUBREG
14114 && !read_modify_subreg_p (dest)))
14116 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
14117 return;
14120 /* If this is some other SUBREG, we know it replaces the entire
14121 value, so use that as the destination. */
14122 if (GET_CODE (dest) == SUBREG)
14123 dest = SUBREG_REG (dest);
14125 /* If this is a MEM, adjust deaths of anything used in the address.
14126 For a REG (the only other possibility), the entire value is
14127 being replaced so the old value is not used in this insn. */
14129 if (MEM_P (dest))
14130 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14131 to_insn, pnotes);
14132 return;
14135 else if (GET_CODE (x) == CLOBBER)
14136 return;
14138 len = GET_RTX_LENGTH (code);
14139 fmt = GET_RTX_FORMAT (code);
14141 for (i = 0; i < len; i++)
14143 if (fmt[i] == 'E')
14145 int j;
14146 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14147 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14148 to_insn, pnotes);
14150 else if (fmt[i] == 'e')
14151 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14155 /* Return true if X is the target of a bit-field assignment in BODY, the
14156 pattern of an insn. X must be a REG. */
14158 static bool
14159 reg_bitfield_target_p (rtx x, rtx body)
14161 int i;
14163 if (GET_CODE (body) == SET)
14165 rtx dest = SET_DEST (body);
14166 rtx target;
14167 unsigned int regno, tregno, endregno, endtregno;
14169 if (GET_CODE (dest) == ZERO_EXTRACT)
14170 target = XEXP (dest, 0);
14171 else if (GET_CODE (dest) == STRICT_LOW_PART)
14172 target = SUBREG_REG (XEXP (dest, 0));
14173 else
14174 return false;
14176 if (GET_CODE (target) == SUBREG)
14177 target = SUBREG_REG (target);
14179 if (!REG_P (target))
14180 return false;
14182 tregno = REGNO (target), regno = REGNO (x);
14183 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14184 return target == x;
14186 endtregno = end_hard_regno (GET_MODE (target), tregno);
14187 endregno = end_hard_regno (GET_MODE (x), regno);
14189 return endregno > tregno && regno < endtregno;
14192 else if (GET_CODE (body) == PARALLEL)
14193 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14194 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14195 return true;
14197 return false;
14200 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14201 as appropriate. I3 and I2 are the insns resulting from the combination
14202 insns including FROM (I2 may be zero).
14204 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14205 not need REG_DEAD notes because they are being substituted for. This
14206 saves searching in the most common cases.
14208 Each note in the list is either ignored or placed on some insns, depending
14209 on the type of note. */
14211 static void
14212 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14213 rtx elim_i2, rtx elim_i1, rtx elim_i0)
14215 rtx note, next_note;
14216 rtx tem_note;
14217 rtx_insn *tem_insn;
14219 for (note = notes; note; note = next_note)
14221 rtx_insn *place = 0, *place2 = 0;
14223 next_note = XEXP (note, 1);
14224 switch (REG_NOTE_KIND (note))
14226 case REG_BR_PROB:
14227 case REG_BR_PRED:
14228 /* Doesn't matter much where we put this, as long as it's somewhere.
14229 It is preferable to keep these notes on branches, which is most
14230 likely to be i3. */
14231 place = i3;
14232 break;
14234 case REG_NON_LOCAL_GOTO:
14235 if (JUMP_P (i3))
14236 place = i3;
14237 else
14239 gcc_assert (i2 && JUMP_P (i2));
14240 place = i2;
14242 break;
14244 case REG_EH_REGION:
14246 /* The landing pad handling needs to be kept in sync with the
14247 prerequisite checking in try_combine. */
14248 int lp_nr = INTVAL (XEXP (note, 0));
14249 /* A REG_EH_REGION note transfering control can only ever come
14250 from i3. */
14251 if (lp_nr > 0)
14252 gcc_assert (from_insn == i3);
14253 /* We are making sure there is a single effective REG_EH_REGION
14254 note and it's valid to put it on i3. */
14255 if (!insn_could_throw_p (from_insn)
14256 && !(lp_nr == INT_MIN && can_nonlocal_goto (from_insn)))
14257 /* Throw away stray notes on insns that can never throw or
14258 make a nonlocal goto. */
14260 else
14262 if (CALL_P (i3))
14263 place = i3;
14264 else
14266 gcc_assert (cfun->can_throw_non_call_exceptions);
14267 /* If i3 can still trap preserve the note, otherwise we've
14268 combined things such that we can now prove that the
14269 instructions can't trap. Drop the note in this case. */
14270 if (may_trap_p (i3))
14271 place = i3;
14274 break;
14277 case REG_ARGS_SIZE:
14278 /* ??? How to distribute between i3-i1. Assume i3 contains the
14279 entire adjustment. Assert i3 contains at least some adjust. */
14280 if (!noop_move_p (i3))
14282 poly_int64 old_size, args_size = get_args_size (note);
14283 /* fixup_args_size_notes looks at REG_NORETURN note,
14284 so ensure the note is placed there first. */
14285 if (CALL_P (i3))
14287 rtx *np;
14288 for (np = &next_note; *np; np = &XEXP (*np, 1))
14289 if (REG_NOTE_KIND (*np) == REG_NORETURN)
14291 rtx n = *np;
14292 *np = XEXP (n, 1);
14293 XEXP (n, 1) = REG_NOTES (i3);
14294 REG_NOTES (i3) = n;
14295 break;
14298 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14299 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14300 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14301 gcc_assert (maybe_ne (old_size, args_size)
14302 || (CALL_P (i3)
14303 && !ACCUMULATE_OUTGOING_ARGS
14304 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14306 break;
14308 case REG_NORETURN:
14309 case REG_SETJMP:
14310 case REG_TM:
14311 case REG_CALL_DECL:
14312 case REG_UNTYPED_CALL:
14313 case REG_CALL_NOCF_CHECK:
14314 /* These notes must remain with the call. It should not be
14315 possible for both I2 and I3 to be a call. */
14316 if (CALL_P (i3))
14317 place = i3;
14318 else
14320 gcc_assert (i2 && CALL_P (i2));
14321 place = i2;
14323 break;
14325 case REG_UNUSED:
14326 /* Any clobbers for i3 may still exist, and so we must process
14327 REG_UNUSED notes from that insn.
14329 Any clobbers from i2 or i1 can only exist if they were added by
14330 recog_for_combine. In that case, recog_for_combine created the
14331 necessary REG_UNUSED notes. Trying to keep any original
14332 REG_UNUSED notes from these insns can cause incorrect output
14333 if it is for the same register as the original i3 dest.
14334 In that case, we will notice that the register is set in i3,
14335 and then add a REG_UNUSED note for the destination of i3, which
14336 is wrong. However, it is possible to have REG_UNUSED notes from
14337 i2 or i1 for register which were both used and clobbered, so
14338 we keep notes from i2 or i1 if they will turn into REG_DEAD
14339 notes. */
14341 /* If this register is set or clobbered between FROM_INSN and I3,
14342 we should not create a note for it. */
14343 if (reg_set_between_p (XEXP (note, 0), from_insn, i3))
14344 break;
14346 /* If this register is set or clobbered in I3, put the note there
14347 unless there is one already. */
14348 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14350 if (from_insn != i3)
14351 break;
14353 if (! (REG_P (XEXP (note, 0))
14354 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14355 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14356 place = i3;
14358 /* Otherwise, if this register is used by I3, then this register
14359 now dies here, so we must put a REG_DEAD note here unless there
14360 is one already. */
14361 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14362 && ! (REG_P (XEXP (note, 0))
14363 ? find_regno_note (i3, REG_DEAD,
14364 REGNO (XEXP (note, 0)))
14365 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14367 PUT_REG_NOTE_KIND (note, REG_DEAD);
14368 place = i3;
14371 /* A SET or CLOBBER of the REG_UNUSED reg has been removed,
14372 but we can't tell which at this point. We must reset any
14373 expectations we had about the value that was previously
14374 stored in the reg. ??? Ideally, we'd adjust REG_N_SETS
14375 and, if appropriate, restore its previous value, but we
14376 don't have enough information for that at this point. */
14377 else
14379 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14381 /* Otherwise, if this register is now referenced in i2
14382 then the register used to be modified in one of the
14383 original insns. If it was i3 (say, in an unused
14384 parallel), it's now completely gone, so the note can
14385 be discarded. But if it was modified in i2, i1 or i0
14386 and we still reference it in i2, then we're
14387 referencing the previous value, and since the
14388 register was modified and REG_UNUSED, we know that
14389 the previous value is now dead. So, if we only
14390 reference the register in i2, we change the note to
14391 REG_DEAD, to reflect the previous value. However, if
14392 we're also setting or clobbering the register as
14393 scratch, we know (because the register was not
14394 referenced in i3) that it's unused, just as it was
14395 unused before, and we place the note in i2. */
14396 if (from_insn != i3 && i2 && INSN_P (i2)
14397 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14399 if (!reg_set_p (XEXP (note, 0), PATTERN (i2)))
14400 PUT_REG_NOTE_KIND (note, REG_DEAD);
14401 if (! (REG_P (XEXP (note, 0))
14402 ? find_regno_note (i2, REG_NOTE_KIND (note),
14403 REGNO (XEXP (note, 0)))
14404 : find_reg_note (i2, REG_NOTE_KIND (note),
14405 XEXP (note, 0))))
14406 place = i2;
14410 break;
14412 case REG_EQUAL:
14413 case REG_EQUIV:
14414 case REG_NOALIAS:
14415 /* These notes say something about results of an insn. We can
14416 only support them if they used to be on I3 in which case they
14417 remain on I3. Otherwise they are ignored.
14419 If the note refers to an expression that is not a constant, we
14420 must also ignore the note since we cannot tell whether the
14421 equivalence is still true. It might be possible to do
14422 slightly better than this (we only have a problem if I2DEST
14423 or I1DEST is present in the expression), but it doesn't
14424 seem worth the trouble. */
14426 if (from_insn == i3
14427 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14428 place = i3;
14429 break;
14431 case REG_INC:
14432 /* These notes say something about how a register is used. They must
14433 be present on any use of the register in I2 or I3. */
14434 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14435 place = i3;
14437 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14439 if (place)
14440 place2 = i2;
14441 else
14442 place = i2;
14444 break;
14446 case REG_LABEL_TARGET:
14447 case REG_LABEL_OPERAND:
14448 /* This can show up in several ways -- either directly in the
14449 pattern, or hidden off in the constant pool with (or without?)
14450 a REG_EQUAL note. */
14451 /* ??? Ignore the without-reg_equal-note problem for now. */
14452 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14453 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14454 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14455 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14456 place = i3;
14458 if (i2
14459 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14460 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14461 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14462 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14464 if (place)
14465 place2 = i2;
14466 else
14467 place = i2;
14470 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14471 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14472 there. */
14473 if (place && JUMP_P (place)
14474 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14475 && (JUMP_LABEL (place) == NULL
14476 || JUMP_LABEL (place) == XEXP (note, 0)))
14478 rtx label = JUMP_LABEL (place);
14480 if (!label)
14481 JUMP_LABEL (place) = XEXP (note, 0);
14482 else if (LABEL_P (label))
14483 LABEL_NUSES (label)--;
14486 if (place2 && JUMP_P (place2)
14487 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14488 && (JUMP_LABEL (place2) == NULL
14489 || JUMP_LABEL (place2) == XEXP (note, 0)))
14491 rtx label = JUMP_LABEL (place2);
14493 if (!label)
14494 JUMP_LABEL (place2) = XEXP (note, 0);
14495 else if (LABEL_P (label))
14496 LABEL_NUSES (label)--;
14497 place2 = 0;
14499 break;
14501 case REG_NONNEG:
14502 /* This note says something about the value of a register prior
14503 to the execution of an insn. It is too much trouble to see
14504 if the note is still correct in all situations. It is better
14505 to simply delete it. */
14506 break;
14508 case REG_DEAD:
14509 /* If we replaced the right hand side of FROM_INSN with a
14510 REG_EQUAL note, the original use of the dying register
14511 will not have been combined into I3 and I2. In such cases,
14512 FROM_INSN is guaranteed to be the first of the combined
14513 instructions, so we simply need to search back before
14514 FROM_INSN for the previous use or set of this register,
14515 then alter the notes there appropriately.
14517 If the register is used as an input in I3, it dies there.
14518 Similarly for I2, if it is nonzero and adjacent to I3.
14520 If the register is not used as an input in either I3 or I2
14521 and it is not one of the registers we were supposed to eliminate,
14522 there are two possibilities. We might have a non-adjacent I2
14523 or we might have somehow eliminated an additional register
14524 from a computation. For example, we might have had A & B where
14525 we discover that B will always be zero. In this case we will
14526 eliminate the reference to A.
14528 In both cases, we must search to see if we can find a previous
14529 use of A and put the death note there. */
14531 if (from_insn
14532 && from_insn == i2mod
14533 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14534 tem_insn = from_insn;
14535 else
14537 if (from_insn
14538 && CALL_P (from_insn)
14539 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14540 place = from_insn;
14541 else if (i2 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14543 /* If the new I2 sets the same register that is marked
14544 dead in the note, we do not in general know where to
14545 put the note. One important case we _can_ handle is
14546 when the note comes from I3. */
14547 if (from_insn == i3)
14548 place = i3;
14549 else
14550 break;
14552 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14553 place = i3;
14554 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14555 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14556 place = i2;
14557 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14558 && !(i2mod
14559 && reg_overlap_mentioned_p (XEXP (note, 0),
14560 i2mod_old_rhs)))
14561 || rtx_equal_p (XEXP (note, 0), elim_i1)
14562 || rtx_equal_p (XEXP (note, 0), elim_i0))
14563 break;
14564 tem_insn = i3;
14567 if (place == 0)
14569 basic_block bb = this_basic_block;
14571 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14573 if (!NONDEBUG_INSN_P (tem_insn))
14575 if (tem_insn == BB_HEAD (bb))
14576 break;
14577 continue;
14580 /* If the register is being set at TEM_INSN, see if that is all
14581 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14582 into a REG_UNUSED note instead. Don't delete sets to
14583 global register vars. */
14584 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14585 || !global_regs[REGNO (XEXP (note, 0))])
14586 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14588 rtx set = single_set (tem_insn);
14589 rtx inner_dest = 0;
14591 if (set != 0)
14592 for (inner_dest = SET_DEST (set);
14593 (GET_CODE (inner_dest) == STRICT_LOW_PART
14594 || GET_CODE (inner_dest) == SUBREG
14595 || GET_CODE (inner_dest) == ZERO_EXTRACT);
14596 inner_dest = XEXP (inner_dest, 0))
14599 /* Verify that it was the set, and not a clobber that
14600 modified the register.
14602 If we cannot delete the setter due to side
14603 effects, mark the user with an UNUSED note instead
14604 of deleting it. */
14606 if (set != 0 && ! side_effects_p (SET_SRC (set))
14607 && rtx_equal_p (XEXP (note, 0), inner_dest))
14609 /* Move the notes and links of TEM_INSN elsewhere.
14610 This might delete other dead insns recursively.
14611 First set the pattern to something that won't use
14612 any register. */
14613 rtx old_notes = REG_NOTES (tem_insn);
14615 PATTERN (tem_insn) = pc_rtx;
14616 REG_NOTES (tem_insn) = NULL;
14618 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14619 NULL_RTX, NULL_RTX, NULL_RTX);
14620 distribute_links (LOG_LINKS (tem_insn));
14622 unsigned int regno = REGNO (XEXP (note, 0));
14623 reg_stat_type *rsp = &reg_stat[regno];
14624 if (rsp->last_set == tem_insn)
14625 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14627 SET_INSN_DELETED (tem_insn);
14628 if (tem_insn == i2)
14629 i2 = NULL;
14631 else
14633 PUT_REG_NOTE_KIND (note, REG_UNUSED);
14635 /* If there isn't already a REG_UNUSED note, put one
14636 here. Do not place a REG_DEAD note, even if
14637 the register is also used here; that would not
14638 match the algorithm used in lifetime analysis
14639 and can cause the consistency check in the
14640 scheduler to fail. */
14641 if (! find_regno_note (tem_insn, REG_UNUSED,
14642 REGNO (XEXP (note, 0))))
14643 place = tem_insn;
14644 break;
14647 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14648 || (CALL_P (tem_insn)
14649 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14651 place = tem_insn;
14653 /* If we are doing a 3->2 combination, and we have a
14654 register which formerly died in i3 and was not used
14655 by i2, which now no longer dies in i3 and is used in
14656 i2 but does not die in i2, and place is between i2
14657 and i3, then we may need to move a link from place to
14658 i2. */
14659 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14660 && from_insn
14661 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14662 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14664 struct insn_link *links = LOG_LINKS (place);
14665 LOG_LINKS (place) = NULL;
14666 distribute_links (links);
14668 break;
14671 if (tem_insn == BB_HEAD (bb))
14672 break;
14677 /* If the register is set or already dead at PLACE, we needn't do
14678 anything with this note if it is still a REG_DEAD note.
14679 We check here if it is set at all, not if is it totally replaced,
14680 which is what `dead_or_set_p' checks, so also check for it being
14681 set partially. */
14683 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14685 unsigned int regno = REGNO (XEXP (note, 0));
14686 reg_stat_type *rsp = &reg_stat[regno];
14688 if (dead_or_set_p (place, XEXP (note, 0))
14689 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14691 /* Unless the register previously died in PLACE, clear
14692 last_death. [I no longer understand why this is
14693 being done.] */
14694 if (rsp->last_death != place)
14695 rsp->last_death = 0;
14696 place = 0;
14698 else
14699 rsp->last_death = place;
14701 /* If this is a death note for a hard reg that is occupying
14702 multiple registers, ensure that we are still using all
14703 parts of the object. If we find a piece of the object
14704 that is unused, we must arrange for an appropriate REG_DEAD
14705 note to be added for it. However, we can't just emit a USE
14706 and tag the note to it, since the register might actually
14707 be dead; so we recourse, and the recursive call then finds
14708 the previous insn that used this register. */
14710 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14712 unsigned int endregno = END_REGNO (XEXP (note, 0));
14713 bool all_used = true;
14714 unsigned int i;
14716 for (i = regno; i < endregno; i++)
14717 if ((! refers_to_regno_p (i, PATTERN (place))
14718 && ! find_regno_fusage (place, USE, i))
14719 || dead_or_set_regno_p (place, i))
14721 all_used = false;
14722 break;
14725 if (! all_used)
14727 /* Put only REG_DEAD notes for pieces that are
14728 not already dead or set. */
14730 for (i = regno; i < endregno;
14731 i += hard_regno_nregs (i, reg_raw_mode[i]))
14733 rtx piece = regno_reg_rtx[i];
14734 basic_block bb = this_basic_block;
14736 if (! dead_or_set_p (place, piece)
14737 && ! reg_bitfield_target_p (piece,
14738 PATTERN (place)))
14740 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14741 NULL_RTX);
14743 distribute_notes (new_note, place, place,
14744 NULL, NULL_RTX, NULL_RTX,
14745 NULL_RTX);
14747 else if (! refers_to_regno_p (i, PATTERN (place))
14748 && ! find_regno_fusage (place, USE, i))
14749 for (tem_insn = PREV_INSN (place); ;
14750 tem_insn = PREV_INSN (tem_insn))
14752 if (!NONDEBUG_INSN_P (tem_insn))
14754 if (tem_insn == BB_HEAD (bb))
14755 break;
14756 continue;
14758 if (dead_or_set_p (tem_insn, piece)
14759 || reg_bitfield_target_p (piece,
14760 PATTERN (tem_insn)))
14762 add_reg_note (tem_insn, REG_UNUSED, piece);
14763 break;
14768 place = 0;
14772 break;
14774 default:
14775 /* Any other notes should not be present at this point in the
14776 compilation. */
14777 gcc_unreachable ();
14780 if (place)
14782 XEXP (note, 1) = REG_NOTES (place);
14783 REG_NOTES (place) = note;
14785 /* Set added_notes_insn to the earliest insn we added a note to. */
14786 if (added_notes_insn == 0
14787 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place))
14788 added_notes_insn = place;
14791 if (place2)
14793 add_shallow_copy_of_reg_note (place2, note);
14795 /* Set added_notes_insn to the earliest insn we added a note to. */
14796 if (added_notes_insn == 0
14797 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place2))
14798 added_notes_insn = place2;
14803 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14804 I3, I2, and I1 to new locations. This is also called to add a link
14805 pointing at I3 when I3's destination is changed. */
14807 static void
14808 distribute_links (struct insn_link *links)
14810 struct insn_link *link, *next_link;
14812 for (link = links; link; link = next_link)
14814 rtx_insn *place = 0;
14815 rtx_insn *insn;
14816 rtx set, reg;
14818 next_link = link->next;
14820 /* If the insn that this link points to is a NOTE, ignore it. */
14821 if (NOTE_P (link->insn))
14822 continue;
14824 set = 0;
14825 rtx pat = PATTERN (link->insn);
14826 if (GET_CODE (pat) == SET)
14827 set = pat;
14828 else if (GET_CODE (pat) == PARALLEL)
14830 int i;
14831 for (i = 0; i < XVECLEN (pat, 0); i++)
14833 set = XVECEXP (pat, 0, i);
14834 if (GET_CODE (set) != SET)
14835 continue;
14837 reg = SET_DEST (set);
14838 while (GET_CODE (reg) == ZERO_EXTRACT
14839 || GET_CODE (reg) == STRICT_LOW_PART
14840 || GET_CODE (reg) == SUBREG)
14841 reg = XEXP (reg, 0);
14843 if (!REG_P (reg))
14844 continue;
14846 if (REGNO (reg) == link->regno)
14847 break;
14849 if (i == XVECLEN (pat, 0))
14850 continue;
14852 else
14853 continue;
14855 reg = SET_DEST (set);
14857 while (GET_CODE (reg) == ZERO_EXTRACT
14858 || GET_CODE (reg) == STRICT_LOW_PART
14859 || GET_CODE (reg) == SUBREG)
14860 reg = XEXP (reg, 0);
14862 if (reg == pc_rtx)
14863 continue;
14865 /* A LOG_LINK is defined as being placed on the first insn that uses
14866 a register and points to the insn that sets the register. Start
14867 searching at the next insn after the target of the link and stop
14868 when we reach a set of the register or the end of the basic block.
14870 Note that this correctly handles the link that used to point from
14871 I3 to I2. Also note that not much searching is typically done here
14872 since most links don't point very far away. */
14874 for (insn = NEXT_INSN (link->insn);
14875 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14876 || BB_HEAD (this_basic_block->next_bb) != insn));
14877 insn = NEXT_INSN (insn))
14878 if (DEBUG_INSN_P (insn))
14879 continue;
14880 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14882 if (reg_referenced_p (reg, PATTERN (insn)))
14883 place = insn;
14884 break;
14886 else if (CALL_P (insn)
14887 && find_reg_fusage (insn, USE, reg))
14889 place = insn;
14890 break;
14892 else if (INSN_P (insn) && reg_set_p (reg, insn))
14893 break;
14895 /* If we found a place to put the link, place it there unless there
14896 is already a link to the same insn as LINK at that point. */
14898 if (place)
14900 struct insn_link *link2;
14902 FOR_EACH_LOG_LINK (link2, place)
14903 if (link2->insn == link->insn && link2->regno == link->regno)
14904 break;
14906 if (link2 == NULL)
14908 link->next = LOG_LINKS (place);
14909 LOG_LINKS (place) = link;
14911 /* Set added_links_insn to the earliest insn we added a
14912 link to. */
14913 if (added_links_insn == 0
14914 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14915 added_links_insn = place;
14921 /* Check for any register or memory mentioned in EQUIV that is not
14922 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14923 of EXPR where some registers may have been replaced by constants. */
14925 static bool
14926 unmentioned_reg_p (rtx equiv, rtx expr)
14928 subrtx_iterator::array_type array;
14929 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14931 const_rtx x = *iter;
14932 if ((REG_P (x) || MEM_P (x))
14933 && !reg_mentioned_p (x, expr))
14934 return true;
14936 return false;
14939 DEBUG_FUNCTION void
14940 dump_combine_stats (FILE *file)
14942 fprintf
14943 (file,
14944 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14945 combine_attempts, combine_merges, combine_extras, combine_successes);
14948 void
14949 dump_combine_total_stats (FILE *file)
14951 fprintf
14952 (file,
14953 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14954 total_attempts, total_merges, total_extras, total_successes);
14957 /* Make pseudo-to-pseudo copies after every hard-reg-to-pseudo-copy, because
14958 the reg-to-reg copy can usefully combine with later instructions, but we
14959 do not want to combine the hard reg into later instructions, for that
14960 restricts register allocation. */
14961 static void
14962 make_more_copies (void)
14964 basic_block bb;
14966 FOR_EACH_BB_FN (bb, cfun)
14968 rtx_insn *insn;
14970 FOR_BB_INSNS (bb, insn)
14972 if (!NONDEBUG_INSN_P (insn))
14973 continue;
14975 rtx set = single_set (insn);
14976 if (!set)
14977 continue;
14979 rtx dest = SET_DEST (set);
14980 if (!(REG_P (dest) && !HARD_REGISTER_P (dest)))
14981 continue;
14983 rtx src = SET_SRC (set);
14984 if (!(REG_P (src) && HARD_REGISTER_P (src)))
14985 continue;
14986 if (TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src)))
14987 continue;
14989 rtx new_reg = gen_reg_rtx (GET_MODE (dest));
14990 rtx_insn *new_insn = gen_move_insn (new_reg, src);
14991 SET_SRC (set) = new_reg;
14992 emit_insn_before (new_insn, insn);
14993 df_insn_rescan (insn);
14998 /* Try combining insns through substitution. */
14999 static void
15000 rest_of_handle_combine (void)
15002 make_more_copies ();
15004 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
15005 df_note_add_problem ();
15006 df_analyze ();
15008 regstat_init_n_sets_and_refs ();
15009 reg_n_sets_max = max_reg_num ();
15011 bool rebuild_jump_labels_after_combine
15012 = combine_instructions (get_insns (), max_reg_num ());
15014 /* Combining insns may have turned an indirect jump into a
15015 direct jump. Rebuild the JUMP_LABEL fields of jumping
15016 instructions. */
15017 if (rebuild_jump_labels_after_combine)
15019 if (dom_info_available_p (CDI_DOMINATORS))
15020 free_dominance_info (CDI_DOMINATORS);
15021 timevar_push (TV_JUMP);
15022 rebuild_jump_labels (get_insns ());
15023 cleanup_cfg (0);
15024 timevar_pop (TV_JUMP);
15027 regstat_free_n_sets_and_refs ();
15030 namespace {
15032 const pass_data pass_data_combine =
15034 RTL_PASS, /* type */
15035 "combine", /* name */
15036 OPTGROUP_NONE, /* optinfo_flags */
15037 TV_COMBINE, /* tv_id */
15038 PROP_cfglayout, /* properties_required */
15039 0, /* properties_provided */
15040 0, /* properties_destroyed */
15041 0, /* todo_flags_start */
15042 TODO_df_finish, /* todo_flags_finish */
15045 class pass_combine : public rtl_opt_pass
15047 public:
15048 pass_combine (gcc::context *ctxt)
15049 : rtl_opt_pass (pass_data_combine, ctxt)
15052 /* opt_pass methods: */
15053 bool gate (function *) final override { return (optimize > 0); }
15054 unsigned int execute (function *) final override
15056 rest_of_handle_combine ();
15057 return 0;
15060 }; // class pass_combine
15062 } // anon namespace
15064 rtl_opt_pass *
15065 make_pass_combine (gcc::context *ctxt)
15067 return new pass_combine (ctxt);