* gcov-tool.c (do_merge): Refactore to remove int ret.
[official-gcc.git] / gcc / combine.c
blobb806959265874ed85539d3117760a0e15bfb4270
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2015 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 of success.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "hash-set.h"
84 #include "machmode.h"
85 #include "vec.h"
86 #include "double-int.h"
87 #include "input.h"
88 #include "alias.h"
89 #include "symtab.h"
90 #include "wide-int.h"
91 #include "inchash.h"
92 #include "tree.h"
93 #include "stor-layout.h"
94 #include "tm_p.h"
95 #include "flags.h"
96 #include "regs.h"
97 #include "hard-reg-set.h"
98 #include "predict.h"
99 #include "function.h"
100 #include "dominance.h"
101 #include "cfg.h"
102 #include "cfgrtl.h"
103 #include "cfgcleanup.h"
104 #include "basic-block.h"
105 #include "insn-config.h"
106 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
107 #include "hashtab.h"
108 #include "statistics.h"
109 #include "real.h"
110 #include "fixed-value.h"
111 #include "expmed.h"
112 #include "dojump.h"
113 #include "explow.h"
114 #include "calls.h"
115 #include "emit-rtl.h"
116 #include "varasm.h"
117 #include "stmt.h"
118 #include "expr.h"
119 #include "insn-attr.h"
120 #include "recog.h"
121 #include "diagnostic-core.h"
122 #include "target.h"
123 #include "insn-codes.h"
124 #include "optabs.h"
125 #include "rtlhooks-def.h"
126 #include "params.h"
127 #include "tree-pass.h"
128 #include "df.h"
129 #include "valtrack.h"
130 #include "hash-map.h"
131 #include "is-a.h"
132 #include "plugin-api.h"
133 #include "ipa-ref.h"
134 #include "cgraph.h"
135 #include "obstack.h"
136 #include "rtl-iter.h"
138 /* Number of attempts to combine instructions in this function. */
140 static int combine_attempts;
142 /* Number of attempts that got as far as substitution in this function. */
144 static int combine_merges;
146 /* Number of instructions combined with added SETs in this function. */
148 static int combine_extras;
150 /* Number of instructions combined in this function. */
152 static int combine_successes;
154 /* Totals over entire compilation. */
156 static int total_attempts, total_merges, total_extras, total_successes;
158 /* combine_instructions may try to replace the right hand side of the
159 second instruction with the value of an associated REG_EQUAL note
160 before throwing it at try_combine. That is problematic when there
161 is a REG_DEAD note for a register used in the old right hand side
162 and can cause distribute_notes to do wrong things. This is the
163 second instruction if it has been so modified, null otherwise. */
165 static rtx_insn *i2mod;
167 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
169 static rtx i2mod_old_rhs;
171 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
173 static rtx i2mod_new_rhs;
175 typedef struct reg_stat_struct {
176 /* Record last point of death of (hard or pseudo) register n. */
177 rtx_insn *last_death;
179 /* Record last point of modification of (hard or pseudo) register n. */
180 rtx_insn *last_set;
182 /* The next group of fields allows the recording of the last value assigned
183 to (hard or pseudo) register n. We use this information to see if an
184 operation being processed is redundant given a prior operation performed
185 on the register. For example, an `and' with a constant is redundant if
186 all the zero bits are already known to be turned off.
188 We use an approach similar to that used by cse, but change it in the
189 following ways:
191 (1) We do not want to reinitialize at each label.
192 (2) It is useful, but not critical, to know the actual value assigned
193 to a register. Often just its form is helpful.
195 Therefore, we maintain the following fields:
197 last_set_value the last value assigned
198 last_set_label records the value of label_tick when the
199 register was assigned
200 last_set_table_tick records the value of label_tick when a
201 value using the register is assigned
202 last_set_invalid set to nonzero when it is not valid
203 to use the value of this register in some
204 register's value
206 To understand the usage of these tables, it is important to understand
207 the distinction between the value in last_set_value being valid and
208 the register being validly contained in some other expression in the
209 table.
211 (The next two parameters are out of date).
213 reg_stat[i].last_set_value is valid if it is nonzero, and either
214 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
216 Register I may validly appear in any expression returned for the value
217 of another register if reg_n_sets[i] is 1. It may also appear in the
218 value for register J if reg_stat[j].last_set_invalid is zero, or
219 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
221 If an expression is found in the table containing a register which may
222 not validly appear in an expression, the register is replaced by
223 something that won't match, (clobber (const_int 0)). */
225 /* Record last value assigned to (hard or pseudo) register n. */
227 rtx last_set_value;
229 /* Record the value of label_tick when an expression involving register n
230 is placed in last_set_value. */
232 int last_set_table_tick;
234 /* Record the value of label_tick when the value for register n is placed in
235 last_set_value. */
237 int last_set_label;
239 /* These fields are maintained in parallel with last_set_value and are
240 used to store the mode in which the register was last set, the bits
241 that were known to be zero when it was last set, and the number of
242 sign bits copies it was known to have when it was last set. */
244 unsigned HOST_WIDE_INT last_set_nonzero_bits;
245 char last_set_sign_bit_copies;
246 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
248 /* Set nonzero if references to register n in expressions should not be
249 used. last_set_invalid is set nonzero when this register is being
250 assigned to and last_set_table_tick == label_tick. */
252 char last_set_invalid;
254 /* Some registers that are set more than once and used in more than one
255 basic block are nevertheless always set in similar ways. For example,
256 a QImode register may be loaded from memory in two places on a machine
257 where byte loads zero extend.
259 We record in the following fields if a register has some leading bits
260 that are always equal to the sign bit, and what we know about the
261 nonzero bits of a register, specifically which bits are known to be
262 zero.
264 If an entry is zero, it means that we don't know anything special. */
266 unsigned char sign_bit_copies;
268 unsigned HOST_WIDE_INT nonzero_bits;
270 /* Record the value of the label_tick when the last truncation
271 happened. The field truncated_to_mode is only valid if
272 truncation_label == label_tick. */
274 int truncation_label;
276 /* Record the last truncation seen for this register. If truncation
277 is not a nop to this mode we might be able to save an explicit
278 truncation if we know that value already contains a truncated
279 value. */
281 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
282 } reg_stat_type;
285 static vec<reg_stat_type> reg_stat;
287 /* One plus the highest pseudo for which we track REG_N_SETS.
288 regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
289 but during combine_split_insns new pseudos can be created. As we don't have
290 updated DF information in that case, it is hard to initialize the array
291 after growing. The combiner only cares about REG_N_SETS (regno) == 1,
292 so instead of growing the arrays, just assume all newly created pseudos
293 during combine might be set multiple times. */
295 static unsigned int reg_n_sets_max;
297 /* Record the luid of the last insn that invalidated memory
298 (anything that writes memory, and subroutine calls, but not pushes). */
300 static int mem_last_set;
302 /* Record the luid of the last CALL_INSN
303 so we can tell whether a potential combination crosses any calls. */
305 static int last_call_luid;
307 /* When `subst' is called, this is the insn that is being modified
308 (by combining in a previous insn). The PATTERN of this insn
309 is still the old pattern partially modified and it should not be
310 looked at, but this may be used to examine the successors of the insn
311 to judge whether a simplification is valid. */
313 static rtx_insn *subst_insn;
315 /* This is the lowest LUID that `subst' is currently dealing with.
316 get_last_value will not return a value if the register was set at or
317 after this LUID. If not for this mechanism, we could get confused if
318 I2 or I1 in try_combine were an insn that used the old value of a register
319 to obtain a new value. In that case, we might erroneously get the
320 new value of the register when we wanted the old one. */
322 static int subst_low_luid;
324 /* This contains any hard registers that are used in newpat; reg_dead_at_p
325 must consider all these registers to be always live. */
327 static HARD_REG_SET newpat_used_regs;
329 /* This is an insn to which a LOG_LINKS entry has been added. If this
330 insn is the earlier than I2 or I3, combine should rescan starting at
331 that location. */
333 static rtx_insn *added_links_insn;
335 /* Basic block in which we are performing combines. */
336 static basic_block this_basic_block;
337 static bool optimize_this_for_speed_p;
340 /* Length of the currently allocated uid_insn_cost array. */
342 static int max_uid_known;
344 /* The following array records the insn_rtx_cost for every insn
345 in the instruction stream. */
347 static int *uid_insn_cost;
349 /* The following array records the LOG_LINKS for every insn in the
350 instruction stream as struct insn_link pointers. */
352 struct insn_link {
353 rtx_insn *insn;
354 unsigned int regno;
355 struct insn_link *next;
358 static struct insn_link **uid_log_links;
360 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
361 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
363 #define FOR_EACH_LOG_LINK(L, INSN) \
364 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
366 /* Links for LOG_LINKS are allocated from this obstack. */
368 static struct obstack insn_link_obstack;
370 /* Allocate a link. */
372 static inline struct insn_link *
373 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
375 struct insn_link *l
376 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
377 sizeof (struct insn_link));
378 l->insn = insn;
379 l->regno = regno;
380 l->next = next;
381 return l;
384 /* Incremented for each basic block. */
386 static int label_tick;
388 /* Reset to label_tick for each extended basic block in scanning order. */
390 static int label_tick_ebb_start;
392 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
393 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
395 static machine_mode nonzero_bits_mode;
397 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
398 be safely used. It is zero while computing them and after combine has
399 completed. This former test prevents propagating values based on
400 previously set values, which can be incorrect if a variable is modified
401 in a loop. */
403 static int nonzero_sign_valid;
406 /* Record one modification to rtl structure
407 to be undone by storing old_contents into *where. */
409 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
411 struct undo
413 struct undo *next;
414 enum undo_kind kind;
415 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
416 union { rtx *r; int *i; struct insn_link **l; } where;
419 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
420 num_undo says how many are currently recorded.
422 other_insn is nonzero if we have modified some other insn in the process
423 of working on subst_insn. It must be verified too. */
425 struct undobuf
427 struct undo *undos;
428 struct undo *frees;
429 rtx_insn *other_insn;
432 static struct undobuf undobuf;
434 /* Number of times the pseudo being substituted for
435 was found and replaced. */
437 static int n_occurrences;
439 static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
440 machine_mode,
441 unsigned HOST_WIDE_INT,
442 unsigned HOST_WIDE_INT *);
443 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
444 machine_mode,
445 unsigned int, unsigned int *);
446 static void do_SUBST (rtx *, rtx);
447 static void do_SUBST_INT (int *, int);
448 static void init_reg_last (void);
449 static void setup_incoming_promotions (rtx_insn *);
450 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
451 static int cant_combine_insn_p (rtx_insn *);
452 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
453 rtx_insn *, rtx_insn *, rtx *, rtx *);
454 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
455 static int contains_muldiv (rtx);
456 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
457 int *, rtx_insn *);
458 static void undo_all (void);
459 static void undo_commit (void);
460 static rtx *find_split_point (rtx *, rtx_insn *, bool);
461 static rtx subst (rtx, rtx, rtx, int, int, int);
462 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
463 static rtx simplify_if_then_else (rtx);
464 static rtx simplify_set (rtx);
465 static rtx simplify_logical (rtx);
466 static rtx expand_compound_operation (rtx);
467 static const_rtx expand_field_assignment (const_rtx);
468 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
469 rtx, unsigned HOST_WIDE_INT, int, int, int);
470 static rtx extract_left_shift (rtx, int);
471 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
472 unsigned HOST_WIDE_INT *);
473 static rtx canon_reg_for_combine (rtx, rtx);
474 static rtx force_to_mode (rtx, machine_mode,
475 unsigned HOST_WIDE_INT, int);
476 static rtx if_then_else_cond (rtx, rtx *, rtx *);
477 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
478 static int rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
479 static rtx make_field_assignment (rtx);
480 static rtx apply_distributive_law (rtx);
481 static rtx distribute_and_simplify_rtx (rtx, int);
482 static rtx simplify_and_const_int_1 (machine_mode, rtx,
483 unsigned HOST_WIDE_INT);
484 static rtx simplify_and_const_int (rtx, machine_mode, rtx,
485 unsigned HOST_WIDE_INT);
486 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
487 HOST_WIDE_INT, machine_mode, int *);
488 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
489 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
490 int);
491 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
492 static rtx gen_lowpart_for_combine (machine_mode, rtx);
493 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
494 rtx, rtx *);
495 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
496 static void update_table_tick (rtx);
497 static void record_value_for_reg (rtx, rtx_insn *, rtx);
498 static void check_promoted_subreg (rtx_insn *, rtx);
499 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
500 static void record_dead_and_set_regs (rtx_insn *);
501 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
502 static rtx get_last_value (const_rtx);
503 static int use_crosses_set_p (const_rtx, int);
504 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
505 static int reg_dead_at_p (rtx, rtx_insn *);
506 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
507 static int reg_bitfield_target_p (rtx, rtx);
508 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
509 static void distribute_links (struct insn_link *);
510 static void mark_used_regs_combine (rtx);
511 static void record_promoted_value (rtx_insn *, rtx);
512 static bool unmentioned_reg_p (rtx, rtx);
513 static void record_truncated_values (rtx *, void *);
514 static bool reg_truncated_to_mode (machine_mode, const_rtx);
515 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
518 /* It is not safe to use ordinary gen_lowpart in combine.
519 See comments in gen_lowpart_for_combine. */
520 #undef RTL_HOOKS_GEN_LOWPART
521 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
523 /* Our implementation of gen_lowpart never emits a new pseudo. */
524 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
525 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
527 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
528 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
530 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
531 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
533 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
534 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
536 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
539 /* Convenience wrapper for the canonicalize_comparison target hook.
540 Target hooks cannot use enum rtx_code. */
541 static inline void
542 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
543 bool op0_preserve_value)
545 int code_int = (int)*code;
546 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
547 *code = (enum rtx_code)code_int;
550 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
551 PATTERN can not be split. Otherwise, it returns an insn sequence.
552 This is a wrapper around split_insns which ensures that the
553 reg_stat vector is made larger if the splitter creates a new
554 register. */
556 static rtx_insn *
557 combine_split_insns (rtx pattern, rtx insn)
559 rtx_insn *ret;
560 unsigned int nregs;
562 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
563 nregs = max_reg_num ();
564 if (nregs > reg_stat.length ())
565 reg_stat.safe_grow_cleared (nregs);
566 return ret;
569 /* This is used by find_single_use to locate an rtx in LOC that
570 contains exactly one use of DEST, which is typically either a REG
571 or CC0. It returns a pointer to the innermost rtx expression
572 containing DEST. Appearances of DEST that are being used to
573 totally replace it are not counted. */
575 static rtx *
576 find_single_use_1 (rtx dest, rtx *loc)
578 rtx x = *loc;
579 enum rtx_code code = GET_CODE (x);
580 rtx *result = NULL;
581 rtx *this_result;
582 int i;
583 const char *fmt;
585 switch (code)
587 case CONST:
588 case LABEL_REF:
589 case SYMBOL_REF:
590 CASE_CONST_ANY:
591 case CLOBBER:
592 return 0;
594 case SET:
595 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
596 of a REG that occupies all of the REG, the insn uses DEST if
597 it is mentioned in the destination or the source. Otherwise, we
598 need just check the source. */
599 if (GET_CODE (SET_DEST (x)) != CC0
600 && GET_CODE (SET_DEST (x)) != PC
601 && !REG_P (SET_DEST (x))
602 && ! (GET_CODE (SET_DEST (x)) == SUBREG
603 && REG_P (SUBREG_REG (SET_DEST (x)))
604 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
605 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
606 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
607 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
608 break;
610 return find_single_use_1 (dest, &SET_SRC (x));
612 case MEM:
613 case SUBREG:
614 return find_single_use_1 (dest, &XEXP (x, 0));
616 default:
617 break;
620 /* If it wasn't one of the common cases above, check each expression and
621 vector of this code. Look for a unique usage of DEST. */
623 fmt = GET_RTX_FORMAT (code);
624 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
626 if (fmt[i] == 'e')
628 if (dest == XEXP (x, i)
629 || (REG_P (dest) && REG_P (XEXP (x, i))
630 && REGNO (dest) == REGNO (XEXP (x, i))))
631 this_result = loc;
632 else
633 this_result = find_single_use_1 (dest, &XEXP (x, i));
635 if (result == NULL)
636 result = this_result;
637 else if (this_result)
638 /* Duplicate usage. */
639 return NULL;
641 else if (fmt[i] == 'E')
643 int j;
645 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
647 if (XVECEXP (x, i, j) == dest
648 || (REG_P (dest)
649 && REG_P (XVECEXP (x, i, j))
650 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
651 this_result = loc;
652 else
653 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
655 if (result == NULL)
656 result = this_result;
657 else if (this_result)
658 return NULL;
663 return result;
667 /* See if DEST, produced in INSN, is used only a single time in the
668 sequel. If so, return a pointer to the innermost rtx expression in which
669 it is used.
671 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
673 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
674 care about REG_DEAD notes or LOG_LINKS.
676 Otherwise, we find the single use by finding an insn that has a
677 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
678 only referenced once in that insn, we know that it must be the first
679 and last insn referencing DEST. */
681 static rtx *
682 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
684 basic_block bb;
685 rtx_insn *next;
686 rtx *result;
687 struct insn_link *link;
689 if (dest == cc0_rtx)
691 next = NEXT_INSN (insn);
692 if (next == 0
693 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
694 return 0;
696 result = find_single_use_1 (dest, &PATTERN (next));
697 if (result && ploc)
698 *ploc = next;
699 return result;
702 if (!REG_P (dest))
703 return 0;
705 bb = BLOCK_FOR_INSN (insn);
706 for (next = NEXT_INSN (insn);
707 next && BLOCK_FOR_INSN (next) == bb;
708 next = NEXT_INSN (next))
709 if (INSN_P (next) && dead_or_set_p (next, dest))
711 FOR_EACH_LOG_LINK (link, next)
712 if (link->insn == insn && link->regno == REGNO (dest))
713 break;
715 if (link)
717 result = find_single_use_1 (dest, &PATTERN (next));
718 if (ploc)
719 *ploc = next;
720 return result;
724 return 0;
727 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
728 insn. The substitution can be undone by undo_all. If INTO is already
729 set to NEWVAL, do not record this change. Because computing NEWVAL might
730 also call SUBST, we have to compute it before we put anything into
731 the undo table. */
733 static void
734 do_SUBST (rtx *into, rtx newval)
736 struct undo *buf;
737 rtx oldval = *into;
739 if (oldval == newval)
740 return;
742 /* We'd like to catch as many invalid transformations here as
743 possible. Unfortunately, there are way too many mode changes
744 that are perfectly valid, so we'd waste too much effort for
745 little gain doing the checks here. Focus on catching invalid
746 transformations involving integer constants. */
747 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
748 && CONST_INT_P (newval))
750 /* Sanity check that we're replacing oldval with a CONST_INT
751 that is a valid sign-extension for the original mode. */
752 gcc_assert (INTVAL (newval)
753 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
755 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
756 CONST_INT is not valid, because after the replacement, the
757 original mode would be gone. Unfortunately, we can't tell
758 when do_SUBST is called to replace the operand thereof, so we
759 perform this test on oldval instead, checking whether an
760 invalid replacement took place before we got here. */
761 gcc_assert (!(GET_CODE (oldval) == SUBREG
762 && CONST_INT_P (SUBREG_REG (oldval))));
763 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
764 && CONST_INT_P (XEXP (oldval, 0))));
767 if (undobuf.frees)
768 buf = undobuf.frees, undobuf.frees = buf->next;
769 else
770 buf = XNEW (struct undo);
772 buf->kind = UNDO_RTX;
773 buf->where.r = into;
774 buf->old_contents.r = oldval;
775 *into = newval;
777 buf->next = undobuf.undos, undobuf.undos = buf;
780 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
782 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
783 for the value of a HOST_WIDE_INT value (including CONST_INT) is
784 not safe. */
786 static void
787 do_SUBST_INT (int *into, int newval)
789 struct undo *buf;
790 int oldval = *into;
792 if (oldval == newval)
793 return;
795 if (undobuf.frees)
796 buf = undobuf.frees, undobuf.frees = buf->next;
797 else
798 buf = XNEW (struct undo);
800 buf->kind = UNDO_INT;
801 buf->where.i = into;
802 buf->old_contents.i = oldval;
803 *into = newval;
805 buf->next = undobuf.undos, undobuf.undos = buf;
808 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
810 /* Similar to SUBST, but just substitute the mode. This is used when
811 changing the mode of a pseudo-register, so that any other
812 references to the entry in the regno_reg_rtx array will change as
813 well. */
815 static void
816 do_SUBST_MODE (rtx *into, machine_mode newval)
818 struct undo *buf;
819 machine_mode oldval = GET_MODE (*into);
821 if (oldval == newval)
822 return;
824 if (undobuf.frees)
825 buf = undobuf.frees, undobuf.frees = buf->next;
826 else
827 buf = XNEW (struct undo);
829 buf->kind = UNDO_MODE;
830 buf->where.r = into;
831 buf->old_contents.m = oldval;
832 adjust_reg_mode (*into, newval);
834 buf->next = undobuf.undos, undobuf.undos = buf;
837 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
839 #if !HAVE_cc0
840 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
842 static void
843 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
845 struct undo *buf;
846 struct insn_link * oldval = *into;
848 if (oldval == newval)
849 return;
851 if (undobuf.frees)
852 buf = undobuf.frees, undobuf.frees = buf->next;
853 else
854 buf = XNEW (struct undo);
856 buf->kind = UNDO_LINKS;
857 buf->where.l = into;
858 buf->old_contents.l = oldval;
859 *into = newval;
861 buf->next = undobuf.undos, undobuf.undos = buf;
864 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
865 #endif
867 /* Subroutine of try_combine. Determine whether the replacement patterns
868 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
869 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
870 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
871 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
872 of all the instructions can be estimated and the replacements are more
873 expensive than the original sequence. */
875 static bool
876 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
877 rtx newpat, rtx newi2pat, rtx newotherpat)
879 int i0_cost, i1_cost, i2_cost, i3_cost;
880 int new_i2_cost, new_i3_cost;
881 int old_cost, new_cost;
883 /* Lookup the original insn_rtx_costs. */
884 i2_cost = INSN_COST (i2);
885 i3_cost = INSN_COST (i3);
887 if (i1)
889 i1_cost = INSN_COST (i1);
890 if (i0)
892 i0_cost = INSN_COST (i0);
893 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
894 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
896 else
898 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
899 ? i1_cost + i2_cost + i3_cost : 0);
900 i0_cost = 0;
903 else
905 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
906 i1_cost = i0_cost = 0;
909 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
910 correct that. */
911 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
912 old_cost -= i1_cost;
915 /* Calculate the replacement insn_rtx_costs. */
916 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
917 if (newi2pat)
919 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
920 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
921 ? new_i2_cost + new_i3_cost : 0;
923 else
925 new_cost = new_i3_cost;
926 new_i2_cost = 0;
929 if (undobuf.other_insn)
931 int old_other_cost, new_other_cost;
933 old_other_cost = INSN_COST (undobuf.other_insn);
934 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
935 if (old_other_cost > 0 && new_other_cost > 0)
937 old_cost += old_other_cost;
938 new_cost += new_other_cost;
940 else
941 old_cost = 0;
944 /* Disallow this combination if both new_cost and old_cost are greater than
945 zero, and new_cost is greater than old cost. */
946 int reject = old_cost > 0 && new_cost > old_cost;
948 if (dump_file)
950 fprintf (dump_file, "%s combination of insns ",
951 reject ? "rejecting" : "allowing");
952 if (i0)
953 fprintf (dump_file, "%d, ", INSN_UID (i0));
954 if (i1 && INSN_UID (i1) != INSN_UID (i2))
955 fprintf (dump_file, "%d, ", INSN_UID (i1));
956 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
958 fprintf (dump_file, "original costs ");
959 if (i0)
960 fprintf (dump_file, "%d + ", i0_cost);
961 if (i1 && INSN_UID (i1) != INSN_UID (i2))
962 fprintf (dump_file, "%d + ", i1_cost);
963 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
965 if (newi2pat)
966 fprintf (dump_file, "replacement costs %d + %d = %d\n",
967 new_i2_cost, new_i3_cost, new_cost);
968 else
969 fprintf (dump_file, "replacement cost %d\n", new_cost);
972 if (reject)
973 return false;
975 /* Update the uid_insn_cost array with the replacement costs. */
976 INSN_COST (i2) = new_i2_cost;
977 INSN_COST (i3) = new_i3_cost;
978 if (i1)
980 INSN_COST (i1) = 0;
981 if (i0)
982 INSN_COST (i0) = 0;
985 return true;
989 /* Delete any insns that copy a register to itself. */
991 static void
992 delete_noop_moves (void)
994 rtx_insn *insn, *next;
995 basic_block bb;
997 FOR_EACH_BB_FN (bb, cfun)
999 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
1001 next = NEXT_INSN (insn);
1002 if (INSN_P (insn) && noop_move_p (insn))
1004 if (dump_file)
1005 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
1007 delete_insn_and_edges (insn);
1014 /* Return false if we do not want to (or cannot) combine DEF. */
1015 static bool
1016 can_combine_def_p (df_ref def)
1018 /* Do not consider if it is pre/post modification in MEM. */
1019 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1020 return false;
1022 unsigned int regno = DF_REF_REGNO (def);
1024 /* Do not combine frame pointer adjustments. */
1025 if ((regno == FRAME_POINTER_REGNUM
1026 && (!reload_completed || frame_pointer_needed))
1027 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1028 || (regno == HARD_FRAME_POINTER_REGNUM
1029 && (!reload_completed || frame_pointer_needed))
1030 #endif
1031 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1032 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1033 #endif
1035 return false;
1037 return true;
1040 /* Return false if we do not want to (or cannot) combine USE. */
1041 static bool
1042 can_combine_use_p (df_ref use)
1044 /* Do not consider the usage of the stack pointer by function call. */
1045 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1046 return false;
1048 return true;
1051 /* Fill in log links field for all insns. */
1053 static void
1054 create_log_links (void)
1056 basic_block bb;
1057 rtx_insn **next_use;
1058 rtx_insn *insn;
1059 df_ref def, use;
1061 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1063 /* Pass through each block from the end, recording the uses of each
1064 register and establishing log links when def is encountered.
1065 Note that we do not clear next_use array in order to save time,
1066 so we have to test whether the use is in the same basic block as def.
1068 There are a few cases below when we do not consider the definition or
1069 usage -- these are taken from original flow.c did. Don't ask me why it is
1070 done this way; I don't know and if it works, I don't want to know. */
1072 FOR_EACH_BB_FN (bb, cfun)
1074 FOR_BB_INSNS_REVERSE (bb, insn)
1076 if (!NONDEBUG_INSN_P (insn))
1077 continue;
1079 /* Log links are created only once. */
1080 gcc_assert (!LOG_LINKS (insn));
1082 FOR_EACH_INSN_DEF (def, insn)
1084 unsigned int regno = DF_REF_REGNO (def);
1085 rtx_insn *use_insn;
1087 if (!next_use[regno])
1088 continue;
1090 if (!can_combine_def_p (def))
1091 continue;
1093 use_insn = next_use[regno];
1094 next_use[regno] = NULL;
1096 if (BLOCK_FOR_INSN (use_insn) != bb)
1097 continue;
1099 /* flow.c claimed:
1101 We don't build a LOG_LINK for hard registers contained
1102 in ASM_OPERANDs. If these registers get replaced,
1103 we might wind up changing the semantics of the insn,
1104 even if reload can make what appear to be valid
1105 assignments later. */
1106 if (regno < FIRST_PSEUDO_REGISTER
1107 && asm_noperands (PATTERN (use_insn)) >= 0)
1108 continue;
1110 /* Don't add duplicate links between instructions. */
1111 struct insn_link *links;
1112 FOR_EACH_LOG_LINK (links, use_insn)
1113 if (insn == links->insn && regno == links->regno)
1114 break;
1116 if (!links)
1117 LOG_LINKS (use_insn)
1118 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1121 FOR_EACH_INSN_USE (use, insn)
1122 if (can_combine_use_p (use))
1123 next_use[DF_REF_REGNO (use)] = insn;
1127 free (next_use);
1130 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1131 true if we found a LOG_LINK that proves that A feeds B. This only works
1132 if there are no instructions between A and B which could have a link
1133 depending on A, since in that case we would not record a link for B.
1134 We also check the implicit dependency created by a cc0 setter/user
1135 pair. */
1137 static bool
1138 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1140 struct insn_link *links;
1141 FOR_EACH_LOG_LINK (links, b)
1142 if (links->insn == a)
1143 return true;
1144 if (HAVE_cc0 && sets_cc0_p (a))
1145 return true;
1146 return false;
1149 /* Main entry point for combiner. F is the first insn of the function.
1150 NREGS is the first unused pseudo-reg number.
1152 Return nonzero if the combiner has turned an indirect jump
1153 instruction into a direct jump. */
1154 static int
1155 combine_instructions (rtx_insn *f, unsigned int nregs)
1157 rtx_insn *insn, *next;
1158 #if HAVE_cc0
1159 rtx_insn *prev;
1160 #endif
1161 struct insn_link *links, *nextlinks;
1162 rtx_insn *first;
1163 basic_block last_bb;
1165 int new_direct_jump_p = 0;
1167 for (first = f; first && !INSN_P (first); )
1168 first = NEXT_INSN (first);
1169 if (!first)
1170 return 0;
1172 combine_attempts = 0;
1173 combine_merges = 0;
1174 combine_extras = 0;
1175 combine_successes = 0;
1177 rtl_hooks = combine_rtl_hooks;
1179 reg_stat.safe_grow_cleared (nregs);
1181 init_recog_no_volatile ();
1183 /* Allocate array for insn info. */
1184 max_uid_known = get_max_uid ();
1185 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1186 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1187 gcc_obstack_init (&insn_link_obstack);
1189 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1191 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1192 problems when, for example, we have j <<= 1 in a loop. */
1194 nonzero_sign_valid = 0;
1195 label_tick = label_tick_ebb_start = 1;
1197 /* Scan all SETs and see if we can deduce anything about what
1198 bits are known to be zero for some registers and how many copies
1199 of the sign bit are known to exist for those registers.
1201 Also set any known values so that we can use it while searching
1202 for what bits are known to be set. */
1204 setup_incoming_promotions (first);
1205 /* Allow the entry block and the first block to fall into the same EBB.
1206 Conceptually the incoming promotions are assigned to the entry block. */
1207 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1209 create_log_links ();
1210 FOR_EACH_BB_FN (this_basic_block, cfun)
1212 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1213 last_call_luid = 0;
1214 mem_last_set = -1;
1216 label_tick++;
1217 if (!single_pred_p (this_basic_block)
1218 || single_pred (this_basic_block) != last_bb)
1219 label_tick_ebb_start = label_tick;
1220 last_bb = this_basic_block;
1222 FOR_BB_INSNS (this_basic_block, insn)
1223 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1225 #ifdef AUTO_INC_DEC
1226 rtx links;
1227 #endif
1229 subst_low_luid = DF_INSN_LUID (insn);
1230 subst_insn = insn;
1232 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1233 insn);
1234 record_dead_and_set_regs (insn);
1236 #ifdef AUTO_INC_DEC
1237 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1238 if (REG_NOTE_KIND (links) == REG_INC)
1239 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1240 insn);
1241 #endif
1243 /* Record the current insn_rtx_cost of this instruction. */
1244 if (NONJUMP_INSN_P (insn))
1245 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1246 optimize_this_for_speed_p);
1247 if (dump_file)
1248 fprintf (dump_file, "insn_cost %d: %d\n",
1249 INSN_UID (insn), INSN_COST (insn));
1253 nonzero_sign_valid = 1;
1255 /* Now scan all the insns in forward order. */
1256 label_tick = label_tick_ebb_start = 1;
1257 init_reg_last ();
1258 setup_incoming_promotions (first);
1259 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1260 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1262 FOR_EACH_BB_FN (this_basic_block, cfun)
1264 rtx_insn *last_combined_insn = NULL;
1265 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1266 last_call_luid = 0;
1267 mem_last_set = -1;
1269 label_tick++;
1270 if (!single_pred_p (this_basic_block)
1271 || single_pred (this_basic_block) != last_bb)
1272 label_tick_ebb_start = label_tick;
1273 last_bb = this_basic_block;
1275 rtl_profile_for_bb (this_basic_block);
1276 for (insn = BB_HEAD (this_basic_block);
1277 insn != NEXT_INSN (BB_END (this_basic_block));
1278 insn = next ? next : NEXT_INSN (insn))
1280 next = 0;
1281 if (!NONDEBUG_INSN_P (insn))
1282 continue;
1284 while (last_combined_insn
1285 && last_combined_insn->deleted ())
1286 last_combined_insn = PREV_INSN (last_combined_insn);
1287 if (last_combined_insn == NULL_RTX
1288 || BARRIER_P (last_combined_insn)
1289 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1290 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1291 last_combined_insn = insn;
1293 /* See if we know about function return values before this
1294 insn based upon SUBREG flags. */
1295 check_promoted_subreg (insn, PATTERN (insn));
1297 /* See if we can find hardregs and subreg of pseudos in
1298 narrower modes. This could help turning TRUNCATEs
1299 into SUBREGs. */
1300 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1302 /* Try this insn with each insn it links back to. */
1304 FOR_EACH_LOG_LINK (links, insn)
1305 if ((next = try_combine (insn, links->insn, NULL,
1306 NULL, &new_direct_jump_p,
1307 last_combined_insn)) != 0)
1309 statistics_counter_event (cfun, "two-insn combine", 1);
1310 goto retry;
1313 /* Try each sequence of three linked insns ending with this one. */
1315 if (max_combine >= 3)
1316 FOR_EACH_LOG_LINK (links, insn)
1318 rtx_insn *link = links->insn;
1320 /* If the linked insn has been replaced by a note, then there
1321 is no point in pursuing this chain any further. */
1322 if (NOTE_P (link))
1323 continue;
1325 FOR_EACH_LOG_LINK (nextlinks, link)
1326 if ((next = try_combine (insn, link, nextlinks->insn,
1327 NULL, &new_direct_jump_p,
1328 last_combined_insn)) != 0)
1330 statistics_counter_event (cfun, "three-insn combine", 1);
1331 goto retry;
1335 #if HAVE_cc0
1336 /* Try to combine a jump insn that uses CC0
1337 with a preceding insn that sets CC0, and maybe with its
1338 logical predecessor as well.
1339 This is how we make decrement-and-branch insns.
1340 We need this special code because data flow connections
1341 via CC0 do not get entered in LOG_LINKS. */
1343 if (JUMP_P (insn)
1344 && (prev = prev_nonnote_insn (insn)) != 0
1345 && NONJUMP_INSN_P (prev)
1346 && sets_cc0_p (PATTERN (prev)))
1348 if ((next = try_combine (insn, prev, NULL, NULL,
1349 &new_direct_jump_p,
1350 last_combined_insn)) != 0)
1351 goto retry;
1353 FOR_EACH_LOG_LINK (nextlinks, prev)
1354 if ((next = try_combine (insn, prev, nextlinks->insn,
1355 NULL, &new_direct_jump_p,
1356 last_combined_insn)) != 0)
1357 goto retry;
1360 /* Do the same for an insn that explicitly references CC0. */
1361 if (NONJUMP_INSN_P (insn)
1362 && (prev = prev_nonnote_insn (insn)) != 0
1363 && NONJUMP_INSN_P (prev)
1364 && sets_cc0_p (PATTERN (prev))
1365 && GET_CODE (PATTERN (insn)) == SET
1366 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1368 if ((next = try_combine (insn, prev, NULL, NULL,
1369 &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1373 FOR_EACH_LOG_LINK (nextlinks, prev)
1374 if ((next = try_combine (insn, prev, nextlinks->insn,
1375 NULL, &new_direct_jump_p,
1376 last_combined_insn)) != 0)
1377 goto retry;
1380 /* Finally, see if any of the insns that this insn links to
1381 explicitly references CC0. If so, try this insn, that insn,
1382 and its predecessor if it sets CC0. */
1383 FOR_EACH_LOG_LINK (links, insn)
1384 if (NONJUMP_INSN_P (links->insn)
1385 && GET_CODE (PATTERN (links->insn)) == SET
1386 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1387 && (prev = prev_nonnote_insn (links->insn)) != 0
1388 && NONJUMP_INSN_P (prev)
1389 && sets_cc0_p (PATTERN (prev))
1390 && (next = try_combine (insn, links->insn,
1391 prev, NULL, &new_direct_jump_p,
1392 last_combined_insn)) != 0)
1393 goto retry;
1394 #endif
1396 /* Try combining an insn with two different insns whose results it
1397 uses. */
1398 if (max_combine >= 3)
1399 FOR_EACH_LOG_LINK (links, insn)
1400 for (nextlinks = links->next; nextlinks;
1401 nextlinks = nextlinks->next)
1402 if ((next = try_combine (insn, links->insn,
1403 nextlinks->insn, NULL,
1404 &new_direct_jump_p,
1405 last_combined_insn)) != 0)
1408 statistics_counter_event (cfun, "three-insn combine", 1);
1409 goto retry;
1412 /* Try four-instruction combinations. */
1413 if (max_combine >= 4)
1414 FOR_EACH_LOG_LINK (links, insn)
1416 struct insn_link *next1;
1417 rtx_insn *link = links->insn;
1419 /* If the linked insn has been replaced by a note, then there
1420 is no point in pursuing this chain any further. */
1421 if (NOTE_P (link))
1422 continue;
1424 FOR_EACH_LOG_LINK (next1, link)
1426 rtx_insn *link1 = next1->insn;
1427 if (NOTE_P (link1))
1428 continue;
1429 /* I0 -> I1 -> I2 -> I3. */
1430 FOR_EACH_LOG_LINK (nextlinks, link1)
1431 if ((next = try_combine (insn, link, link1,
1432 nextlinks->insn,
1433 &new_direct_jump_p,
1434 last_combined_insn)) != 0)
1436 statistics_counter_event (cfun, "four-insn combine", 1);
1437 goto retry;
1439 /* I0, I1 -> I2, I2 -> I3. */
1440 for (nextlinks = next1->next; nextlinks;
1441 nextlinks = nextlinks->next)
1442 if ((next = try_combine (insn, link, link1,
1443 nextlinks->insn,
1444 &new_direct_jump_p,
1445 last_combined_insn)) != 0)
1447 statistics_counter_event (cfun, "four-insn combine", 1);
1448 goto retry;
1452 for (next1 = links->next; next1; next1 = next1->next)
1454 rtx_insn *link1 = next1->insn;
1455 if (NOTE_P (link1))
1456 continue;
1457 /* I0 -> I2; I1, I2 -> I3. */
1458 FOR_EACH_LOG_LINK (nextlinks, link)
1459 if ((next = try_combine (insn, link, link1,
1460 nextlinks->insn,
1461 &new_direct_jump_p,
1462 last_combined_insn)) != 0)
1464 statistics_counter_event (cfun, "four-insn combine", 1);
1465 goto retry;
1467 /* I0 -> I1; I1, I2 -> I3. */
1468 FOR_EACH_LOG_LINK (nextlinks, link1)
1469 if ((next = try_combine (insn, link, link1,
1470 nextlinks->insn,
1471 &new_direct_jump_p,
1472 last_combined_insn)) != 0)
1474 statistics_counter_event (cfun, "four-insn combine", 1);
1475 goto retry;
1480 /* Try this insn with each REG_EQUAL note it links back to. */
1481 FOR_EACH_LOG_LINK (links, insn)
1483 rtx set, note;
1484 rtx_insn *temp = links->insn;
1485 if ((set = single_set (temp)) != 0
1486 && (note = find_reg_equal_equiv_note (temp)) != 0
1487 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1488 /* Avoid using a register that may already been marked
1489 dead by an earlier instruction. */
1490 && ! unmentioned_reg_p (note, SET_SRC (set))
1491 && (GET_MODE (note) == VOIDmode
1492 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1493 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1495 /* Temporarily replace the set's source with the
1496 contents of the REG_EQUAL note. The insn will
1497 be deleted or recognized by try_combine. */
1498 rtx orig = SET_SRC (set);
1499 SET_SRC (set) = note;
1500 i2mod = temp;
1501 i2mod_old_rhs = copy_rtx (orig);
1502 i2mod_new_rhs = copy_rtx (note);
1503 next = try_combine (insn, i2mod, NULL, NULL,
1504 &new_direct_jump_p,
1505 last_combined_insn);
1506 i2mod = NULL;
1507 if (next)
1509 statistics_counter_event (cfun, "insn-with-note combine", 1);
1510 goto retry;
1512 SET_SRC (set) = orig;
1516 if (!NOTE_P (insn))
1517 record_dead_and_set_regs (insn);
1519 retry:
1524 default_rtl_profile ();
1525 clear_bb_flags ();
1526 new_direct_jump_p |= purge_all_dead_edges ();
1527 delete_noop_moves ();
1529 /* Clean up. */
1530 obstack_free (&insn_link_obstack, NULL);
1531 free (uid_log_links);
1532 free (uid_insn_cost);
1533 reg_stat.release ();
1536 struct undo *undo, *next;
1537 for (undo = undobuf.frees; undo; undo = next)
1539 next = undo->next;
1540 free (undo);
1542 undobuf.frees = 0;
1545 total_attempts += combine_attempts;
1546 total_merges += combine_merges;
1547 total_extras += combine_extras;
1548 total_successes += combine_successes;
1550 nonzero_sign_valid = 0;
1551 rtl_hooks = general_rtl_hooks;
1553 /* Make recognizer allow volatile MEMs again. */
1554 init_recog ();
1556 return new_direct_jump_p;
1559 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1561 static void
1562 init_reg_last (void)
1564 unsigned int i;
1565 reg_stat_type *p;
1567 FOR_EACH_VEC_ELT (reg_stat, i, p)
1568 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1571 /* Set up any promoted values for incoming argument registers. */
1573 static void
1574 setup_incoming_promotions (rtx_insn *first)
1576 tree arg;
1577 bool strictly_local = false;
1579 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1580 arg = DECL_CHAIN (arg))
1582 rtx x, reg = DECL_INCOMING_RTL (arg);
1583 int uns1, uns3;
1584 machine_mode mode1, mode2, mode3, mode4;
1586 /* Only continue if the incoming argument is in a register. */
1587 if (!REG_P (reg))
1588 continue;
1590 /* Determine, if possible, whether all call sites of the current
1591 function lie within the current compilation unit. (This does
1592 take into account the exporting of a function via taking its
1593 address, and so forth.) */
1594 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1596 /* The mode and signedness of the argument before any promotions happen
1597 (equal to the mode of the pseudo holding it at that stage). */
1598 mode1 = TYPE_MODE (TREE_TYPE (arg));
1599 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1601 /* The mode and signedness of the argument after any source language and
1602 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1603 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1604 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1606 /* The mode and signedness of the argument as it is actually passed,
1607 see assign_parm_setup_reg in function.c. */
1608 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1609 TREE_TYPE (cfun->decl), 0);
1611 /* The mode of the register in which the argument is being passed. */
1612 mode4 = GET_MODE (reg);
1614 /* Eliminate sign extensions in the callee when:
1615 (a) A mode promotion has occurred; */
1616 if (mode1 == mode3)
1617 continue;
1618 /* (b) The mode of the register is the same as the mode of
1619 the argument as it is passed; */
1620 if (mode3 != mode4)
1621 continue;
1622 /* (c) There's no language level extension; */
1623 if (mode1 == mode2)
1625 /* (c.1) All callers are from the current compilation unit. If that's
1626 the case we don't have to rely on an ABI, we only have to know
1627 what we're generating right now, and we know that we will do the
1628 mode1 to mode2 promotion with the given sign. */
1629 else if (!strictly_local)
1630 continue;
1631 /* (c.2) The combination of the two promotions is useful. This is
1632 true when the signs match, or if the first promotion is unsigned.
1633 In the later case, (sign_extend (zero_extend x)) is the same as
1634 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1635 else if (uns1)
1636 uns3 = true;
1637 else if (uns3)
1638 continue;
1640 /* Record that the value was promoted from mode1 to mode3,
1641 so that any sign extension at the head of the current
1642 function may be eliminated. */
1643 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1644 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1645 record_value_for_reg (reg, first, x);
1649 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1650 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1651 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1652 because some machines (maybe most) will actually do the sign-extension and
1653 this is the conservative approach.
1655 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1656 kludge. */
1658 static rtx
1659 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1661 if (GET_MODE_PRECISION (mode) < prec
1662 && CONST_INT_P (src)
1663 && INTVAL (src) > 0
1664 && val_signbit_known_set_p (mode, INTVAL (src)))
1665 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (mode));
1667 return src;
1669 #endif
1671 /* Called via note_stores. If X is a pseudo that is narrower than
1672 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1674 If we are setting only a portion of X and we can't figure out what
1675 portion, assume all bits will be used since we don't know what will
1676 be happening.
1678 Similarly, set how many bits of X are known to be copies of the sign bit
1679 at all locations in the function. This is the smallest number implied
1680 by any set of X. */
1682 static void
1683 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1685 rtx_insn *insn = (rtx_insn *) data;
1686 unsigned int num;
1688 if (REG_P (x)
1689 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1690 /* If this register is undefined at the start of the file, we can't
1691 say what its contents were. */
1692 && ! REGNO_REG_SET_P
1693 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1694 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1696 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1698 if (set == 0 || GET_CODE (set) == CLOBBER)
1700 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1701 rsp->sign_bit_copies = 1;
1702 return;
1705 /* If this register is being initialized using itself, and the
1706 register is uninitialized in this basic block, and there are
1707 no LOG_LINKS which set the register, then part of the
1708 register is uninitialized. In that case we can't assume
1709 anything about the number of nonzero bits.
1711 ??? We could do better if we checked this in
1712 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1713 could avoid making assumptions about the insn which initially
1714 sets the register, while still using the information in other
1715 insns. We would have to be careful to check every insn
1716 involved in the combination. */
1718 if (insn
1719 && reg_referenced_p (x, PATTERN (insn))
1720 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1721 REGNO (x)))
1723 struct insn_link *link;
1725 FOR_EACH_LOG_LINK (link, insn)
1726 if (dead_or_set_p (link->insn, x))
1727 break;
1728 if (!link)
1730 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1731 rsp->sign_bit_copies = 1;
1732 return;
1736 /* If this is a complex assignment, see if we can convert it into a
1737 simple assignment. */
1738 set = expand_field_assignment (set);
1740 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1741 set what we know about X. */
1743 if (SET_DEST (set) == x
1744 || (paradoxical_subreg_p (SET_DEST (set))
1745 && SUBREG_REG (SET_DEST (set)) == x))
1747 rtx src = SET_SRC (set);
1749 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1750 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1751 #endif
1753 /* Don't call nonzero_bits if it cannot change anything. */
1754 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1755 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1756 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1757 if (rsp->sign_bit_copies == 0
1758 || rsp->sign_bit_copies > num)
1759 rsp->sign_bit_copies = num;
1761 else
1763 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1764 rsp->sign_bit_copies = 1;
1769 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1770 optionally insns that were previously combined into I3 or that will be
1771 combined into the merger of INSN and I3. The order is PRED, PRED2,
1772 INSN, SUCC, SUCC2, I3.
1774 Return 0 if the combination is not allowed for any reason.
1776 If the combination is allowed, *PDEST will be set to the single
1777 destination of INSN and *PSRC to the single source, and this function
1778 will return 1. */
1780 static int
1781 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1782 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1783 rtx *pdest, rtx *psrc)
1785 int i;
1786 const_rtx set = 0;
1787 rtx src, dest;
1788 rtx_insn *p;
1789 #ifdef AUTO_INC_DEC
1790 rtx link;
1791 #endif
1792 bool all_adjacent = true;
1793 int (*is_volatile_p) (const_rtx);
1795 if (succ)
1797 if (succ2)
1799 if (next_active_insn (succ2) != i3)
1800 all_adjacent = false;
1801 if (next_active_insn (succ) != succ2)
1802 all_adjacent = false;
1804 else if (next_active_insn (succ) != i3)
1805 all_adjacent = false;
1806 if (next_active_insn (insn) != succ)
1807 all_adjacent = false;
1809 else if (next_active_insn (insn) != i3)
1810 all_adjacent = false;
1812 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1813 or a PARALLEL consisting of such a SET and CLOBBERs.
1815 If INSN has CLOBBER parallel parts, ignore them for our processing.
1816 By definition, these happen during the execution of the insn. When it
1817 is merged with another insn, all bets are off. If they are, in fact,
1818 needed and aren't also supplied in I3, they may be added by
1819 recog_for_combine. Otherwise, it won't match.
1821 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1822 note.
1824 Get the source and destination of INSN. If more than one, can't
1825 combine. */
1827 if (GET_CODE (PATTERN (insn)) == SET)
1828 set = PATTERN (insn);
1829 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1830 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1832 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1834 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1836 switch (GET_CODE (elt))
1838 /* This is important to combine floating point insns
1839 for the SH4 port. */
1840 case USE:
1841 /* Combining an isolated USE doesn't make sense.
1842 We depend here on combinable_i3pat to reject them. */
1843 /* The code below this loop only verifies that the inputs of
1844 the SET in INSN do not change. We call reg_set_between_p
1845 to verify that the REG in the USE does not change between
1846 I3 and INSN.
1847 If the USE in INSN was for a pseudo register, the matching
1848 insn pattern will likely match any register; combining this
1849 with any other USE would only be safe if we knew that the
1850 used registers have identical values, or if there was
1851 something to tell them apart, e.g. different modes. For
1852 now, we forgo such complicated tests and simply disallow
1853 combining of USES of pseudo registers with any other USE. */
1854 if (REG_P (XEXP (elt, 0))
1855 && GET_CODE (PATTERN (i3)) == PARALLEL)
1857 rtx i3pat = PATTERN (i3);
1858 int i = XVECLEN (i3pat, 0) - 1;
1859 unsigned int regno = REGNO (XEXP (elt, 0));
1863 rtx i3elt = XVECEXP (i3pat, 0, i);
1865 if (GET_CODE (i3elt) == USE
1866 && REG_P (XEXP (i3elt, 0))
1867 && (REGNO (XEXP (i3elt, 0)) == regno
1868 ? reg_set_between_p (XEXP (elt, 0),
1869 PREV_INSN (insn), i3)
1870 : regno >= FIRST_PSEUDO_REGISTER))
1871 return 0;
1873 while (--i >= 0);
1875 break;
1877 /* We can ignore CLOBBERs. */
1878 case CLOBBER:
1879 break;
1881 case SET:
1882 /* Ignore SETs whose result isn't used but not those that
1883 have side-effects. */
1884 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1885 && insn_nothrow_p (insn)
1886 && !side_effects_p (elt))
1887 break;
1889 /* If we have already found a SET, this is a second one and
1890 so we cannot combine with this insn. */
1891 if (set)
1892 return 0;
1894 set = elt;
1895 break;
1897 default:
1898 /* Anything else means we can't combine. */
1899 return 0;
1903 if (set == 0
1904 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1905 so don't do anything with it. */
1906 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1907 return 0;
1909 else
1910 return 0;
1912 if (set == 0)
1913 return 0;
1915 /* The simplification in expand_field_assignment may call back to
1916 get_last_value, so set safe guard here. */
1917 subst_low_luid = DF_INSN_LUID (insn);
1919 set = expand_field_assignment (set);
1920 src = SET_SRC (set), dest = SET_DEST (set);
1922 /* Do not eliminate user-specified register if it is in an
1923 asm input because we may break the register asm usage defined
1924 in GCC manual if allow to do so.
1925 Be aware that this may cover more cases than we expect but this
1926 should be harmless. */
1927 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1928 && extract_asm_operands (PATTERN (i3)))
1929 return 0;
1931 /* Don't eliminate a store in the stack pointer. */
1932 if (dest == stack_pointer_rtx
1933 /* Don't combine with an insn that sets a register to itself if it has
1934 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1935 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1936 /* Can't merge an ASM_OPERANDS. */
1937 || GET_CODE (src) == ASM_OPERANDS
1938 /* Can't merge a function call. */
1939 || GET_CODE (src) == CALL
1940 /* Don't eliminate a function call argument. */
1941 || (CALL_P (i3)
1942 && (find_reg_fusage (i3, USE, dest)
1943 || (REG_P (dest)
1944 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1945 && global_regs[REGNO (dest)])))
1946 /* Don't substitute into an incremented register. */
1947 || FIND_REG_INC_NOTE (i3, dest)
1948 || (succ && FIND_REG_INC_NOTE (succ, dest))
1949 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1950 /* Don't substitute into a non-local goto, this confuses CFG. */
1951 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1952 /* Make sure that DEST is not used after SUCC but before I3. */
1953 || (!all_adjacent
1954 && ((succ2
1955 && (reg_used_between_p (dest, succ2, i3)
1956 || reg_used_between_p (dest, succ, succ2)))
1957 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1958 /* Make sure that the value that is to be substituted for the register
1959 does not use any registers whose values alter in between. However,
1960 If the insns are adjacent, a use can't cross a set even though we
1961 think it might (this can happen for a sequence of insns each setting
1962 the same destination; last_set of that register might point to
1963 a NOTE). If INSN has a REG_EQUIV note, the register is always
1964 equivalent to the memory so the substitution is valid even if there
1965 are intervening stores. Also, don't move a volatile asm or
1966 UNSPEC_VOLATILE across any other insns. */
1967 || (! all_adjacent
1968 && (((!MEM_P (src)
1969 || ! find_reg_note (insn, REG_EQUIV, src))
1970 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1971 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1972 || GET_CODE (src) == UNSPEC_VOLATILE))
1973 /* Don't combine across a CALL_INSN, because that would possibly
1974 change whether the life span of some REGs crosses calls or not,
1975 and it is a pain to update that information.
1976 Exception: if source is a constant, moving it later can't hurt.
1977 Accept that as a special case. */
1978 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1979 return 0;
1981 /* DEST must either be a REG or CC0. */
1982 if (REG_P (dest))
1984 /* If register alignment is being enforced for multi-word items in all
1985 cases except for parameters, it is possible to have a register copy
1986 insn referencing a hard register that is not allowed to contain the
1987 mode being copied and which would not be valid as an operand of most
1988 insns. Eliminate this problem by not combining with such an insn.
1990 Also, on some machines we don't want to extend the life of a hard
1991 register. */
1993 if (REG_P (src)
1994 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1995 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1996 /* Don't extend the life of a hard register unless it is
1997 user variable (if we have few registers) or it can't
1998 fit into the desired register (meaning something special
1999 is going on).
2000 Also avoid substituting a return register into I3, because
2001 reload can't handle a conflict with constraints of other
2002 inputs. */
2003 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2004 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
2005 return 0;
2007 else if (GET_CODE (dest) != CC0)
2008 return 0;
2011 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2012 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2013 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2015 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2017 /* If the clobber represents an earlyclobber operand, we must not
2018 substitute an expression containing the clobbered register.
2019 As we do not analyze the constraint strings here, we have to
2020 make the conservative assumption. However, if the register is
2021 a fixed hard reg, the clobber cannot represent any operand;
2022 we leave it up to the machine description to either accept or
2023 reject use-and-clobber patterns. */
2024 if (!REG_P (reg)
2025 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2026 || !fixed_regs[REGNO (reg)])
2027 if (reg_overlap_mentioned_p (reg, src))
2028 return 0;
2031 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2032 or not), reject, unless nothing volatile comes between it and I3 */
2034 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2036 /* Make sure neither succ nor succ2 contains a volatile reference. */
2037 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2038 return 0;
2039 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2040 return 0;
2041 /* We'll check insns between INSN and I3 below. */
2044 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2045 to be an explicit register variable, and was chosen for a reason. */
2047 if (GET_CODE (src) == ASM_OPERANDS
2048 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2049 return 0;
2051 /* If INSN contains volatile references (specifically volatile MEMs),
2052 we cannot combine across any other volatile references.
2053 Even if INSN doesn't contain volatile references, any intervening
2054 volatile insn might affect machine state. */
2056 is_volatile_p = volatile_refs_p (PATTERN (insn))
2057 ? volatile_refs_p
2058 : volatile_insn_p;
2060 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2061 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2062 return 0;
2064 /* If INSN contains an autoincrement or autodecrement, make sure that
2065 register is not used between there and I3, and not already used in
2066 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2067 Also insist that I3 not be a jump; if it were one
2068 and the incremented register were spilled, we would lose. */
2070 #ifdef AUTO_INC_DEC
2071 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2072 if (REG_NOTE_KIND (link) == REG_INC
2073 && (JUMP_P (i3)
2074 || reg_used_between_p (XEXP (link, 0), insn, i3)
2075 || (pred != NULL_RTX
2076 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2077 || (pred2 != NULL_RTX
2078 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2079 || (succ != NULL_RTX
2080 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2081 || (succ2 != NULL_RTX
2082 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2083 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2084 return 0;
2085 #endif
2087 /* Don't combine an insn that follows a CC0-setting insn.
2088 An insn that uses CC0 must not be separated from the one that sets it.
2089 We do, however, allow I2 to follow a CC0-setting insn if that insn
2090 is passed as I1; in that case it will be deleted also.
2091 We also allow combining in this case if all the insns are adjacent
2092 because that would leave the two CC0 insns adjacent as well.
2093 It would be more logical to test whether CC0 occurs inside I1 or I2,
2094 but that would be much slower, and this ought to be equivalent. */
2096 if (HAVE_cc0)
2098 p = prev_nonnote_insn (insn);
2099 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2100 && ! all_adjacent)
2101 return 0;
2104 /* If we get here, we have passed all the tests and the combination is
2105 to be allowed. */
2107 *pdest = dest;
2108 *psrc = src;
2110 return 1;
2113 /* LOC is the location within I3 that contains its pattern or the component
2114 of a PARALLEL of the pattern. We validate that it is valid for combining.
2116 One problem is if I3 modifies its output, as opposed to replacing it
2117 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2118 doing so would produce an insn that is not equivalent to the original insns.
2120 Consider:
2122 (set (reg:DI 101) (reg:DI 100))
2123 (set (subreg:SI (reg:DI 101) 0) <foo>)
2125 This is NOT equivalent to:
2127 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2128 (set (reg:DI 101) (reg:DI 100))])
2130 Not only does this modify 100 (in which case it might still be valid
2131 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2133 We can also run into a problem if I2 sets a register that I1
2134 uses and I1 gets directly substituted into I3 (not via I2). In that
2135 case, we would be getting the wrong value of I2DEST into I3, so we
2136 must reject the combination. This case occurs when I2 and I1 both
2137 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2138 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2139 of a SET must prevent combination from occurring. The same situation
2140 can occur for I0, in which case I0_NOT_IN_SRC is set.
2142 Before doing the above check, we first try to expand a field assignment
2143 into a set of logical operations.
2145 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2146 we place a register that is both set and used within I3. If more than one
2147 such register is detected, we fail.
2149 Return 1 if the combination is valid, zero otherwise. */
2151 static int
2152 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2153 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2155 rtx x = *loc;
2157 if (GET_CODE (x) == SET)
2159 rtx set = x ;
2160 rtx dest = SET_DEST (set);
2161 rtx src = SET_SRC (set);
2162 rtx inner_dest = dest;
2163 rtx subdest;
2165 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2166 || GET_CODE (inner_dest) == SUBREG
2167 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2168 inner_dest = XEXP (inner_dest, 0);
2170 /* Check for the case where I3 modifies its output, as discussed
2171 above. We don't want to prevent pseudos from being combined
2172 into the address of a MEM, so only prevent the combination if
2173 i1 or i2 set the same MEM. */
2174 if ((inner_dest != dest &&
2175 (!MEM_P (inner_dest)
2176 || rtx_equal_p (i2dest, inner_dest)
2177 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2178 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2179 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2180 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2181 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2183 /* This is the same test done in can_combine_p except we can't test
2184 all_adjacent; we don't have to, since this instruction will stay
2185 in place, thus we are not considering increasing the lifetime of
2186 INNER_DEST.
2188 Also, if this insn sets a function argument, combining it with
2189 something that might need a spill could clobber a previous
2190 function argument; the all_adjacent test in can_combine_p also
2191 checks this; here, we do a more specific test for this case. */
2193 || (REG_P (inner_dest)
2194 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2195 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2196 GET_MODE (inner_dest))))
2197 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2198 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2199 return 0;
2201 /* If DEST is used in I3, it is being killed in this insn, so
2202 record that for later. We have to consider paradoxical
2203 subregs here, since they kill the whole register, but we
2204 ignore partial subregs, STRICT_LOW_PART, etc.
2205 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2206 STACK_POINTER_REGNUM, since these are always considered to be
2207 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2208 subdest = dest;
2209 if (GET_CODE (subdest) == SUBREG
2210 && (GET_MODE_SIZE (GET_MODE (subdest))
2211 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2212 subdest = SUBREG_REG (subdest);
2213 if (pi3dest_killed
2214 && REG_P (subdest)
2215 && reg_referenced_p (subdest, PATTERN (i3))
2216 && REGNO (subdest) != FRAME_POINTER_REGNUM
2217 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2218 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2219 #endif
2220 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2221 && (REGNO (subdest) != ARG_POINTER_REGNUM
2222 || ! fixed_regs [REGNO (subdest)])
2223 #endif
2224 && REGNO (subdest) != STACK_POINTER_REGNUM)
2226 if (*pi3dest_killed)
2227 return 0;
2229 *pi3dest_killed = subdest;
2233 else if (GET_CODE (x) == PARALLEL)
2235 int i;
2237 for (i = 0; i < XVECLEN (x, 0); i++)
2238 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2239 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2240 return 0;
2243 return 1;
2246 /* Return 1 if X is an arithmetic expression that contains a multiplication
2247 and division. We don't count multiplications by powers of two here. */
2249 static int
2250 contains_muldiv (rtx x)
2252 switch (GET_CODE (x))
2254 case MOD: case DIV: case UMOD: case UDIV:
2255 return 1;
2257 case MULT:
2258 return ! (CONST_INT_P (XEXP (x, 1))
2259 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2260 default:
2261 if (BINARY_P (x))
2262 return contains_muldiv (XEXP (x, 0))
2263 || contains_muldiv (XEXP (x, 1));
2265 if (UNARY_P (x))
2266 return contains_muldiv (XEXP (x, 0));
2268 return 0;
2272 /* Determine whether INSN can be used in a combination. Return nonzero if
2273 not. This is used in try_combine to detect early some cases where we
2274 can't perform combinations. */
2276 static int
2277 cant_combine_insn_p (rtx_insn *insn)
2279 rtx set;
2280 rtx src, dest;
2282 /* If this isn't really an insn, we can't do anything.
2283 This can occur when flow deletes an insn that it has merged into an
2284 auto-increment address. */
2285 if (! INSN_P (insn))
2286 return 1;
2288 /* Never combine loads and stores involving hard regs that are likely
2289 to be spilled. The register allocator can usually handle such
2290 reg-reg moves by tying. If we allow the combiner to make
2291 substitutions of likely-spilled regs, reload might die.
2292 As an exception, we allow combinations involving fixed regs; these are
2293 not available to the register allocator so there's no risk involved. */
2295 set = single_set (insn);
2296 if (! set)
2297 return 0;
2298 src = SET_SRC (set);
2299 dest = SET_DEST (set);
2300 if (GET_CODE (src) == SUBREG)
2301 src = SUBREG_REG (src);
2302 if (GET_CODE (dest) == SUBREG)
2303 dest = SUBREG_REG (dest);
2304 if (REG_P (src) && REG_P (dest)
2305 && ((HARD_REGISTER_P (src)
2306 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2307 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2308 || (HARD_REGISTER_P (dest)
2309 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2310 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2311 return 1;
2313 return 0;
2316 struct likely_spilled_retval_info
2318 unsigned regno, nregs;
2319 unsigned mask;
2322 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2323 hard registers that are known to be written to / clobbered in full. */
2324 static void
2325 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2327 struct likely_spilled_retval_info *const info =
2328 (struct likely_spilled_retval_info *) data;
2329 unsigned regno, nregs;
2330 unsigned new_mask;
2332 if (!REG_P (XEXP (set, 0)))
2333 return;
2334 regno = REGNO (x);
2335 if (regno >= info->regno + info->nregs)
2336 return;
2337 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2338 if (regno + nregs <= info->regno)
2339 return;
2340 new_mask = (2U << (nregs - 1)) - 1;
2341 if (regno < info->regno)
2342 new_mask >>= info->regno - regno;
2343 else
2344 new_mask <<= regno - info->regno;
2345 info->mask &= ~new_mask;
2348 /* Return nonzero iff part of the return value is live during INSN, and
2349 it is likely spilled. This can happen when more than one insn is needed
2350 to copy the return value, e.g. when we consider to combine into the
2351 second copy insn for a complex value. */
2353 static int
2354 likely_spilled_retval_p (rtx_insn *insn)
2356 rtx_insn *use = BB_END (this_basic_block);
2357 rtx reg;
2358 rtx_insn *p;
2359 unsigned regno, nregs;
2360 /* We assume here that no machine mode needs more than
2361 32 hard registers when the value overlaps with a register
2362 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2363 unsigned mask;
2364 struct likely_spilled_retval_info info;
2366 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2367 return 0;
2368 reg = XEXP (PATTERN (use), 0);
2369 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2370 return 0;
2371 regno = REGNO (reg);
2372 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2373 if (nregs == 1)
2374 return 0;
2375 mask = (2U << (nregs - 1)) - 1;
2377 /* Disregard parts of the return value that are set later. */
2378 info.regno = regno;
2379 info.nregs = nregs;
2380 info.mask = mask;
2381 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2382 if (INSN_P (p))
2383 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2384 mask = info.mask;
2386 /* Check if any of the (probably) live return value registers is
2387 likely spilled. */
2388 nregs --;
2391 if ((mask & 1 << nregs)
2392 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2393 return 1;
2394 } while (nregs--);
2395 return 0;
2398 /* Adjust INSN after we made a change to its destination.
2400 Changing the destination can invalidate notes that say something about
2401 the results of the insn and a LOG_LINK pointing to the insn. */
2403 static void
2404 adjust_for_new_dest (rtx_insn *insn)
2406 /* For notes, be conservative and simply remove them. */
2407 remove_reg_equal_equiv_notes (insn);
2409 /* The new insn will have a destination that was previously the destination
2410 of an insn just above it. Call distribute_links to make a LOG_LINK from
2411 the next use of that destination. */
2413 rtx set = single_set (insn);
2414 gcc_assert (set);
2416 rtx reg = SET_DEST (set);
2418 while (GET_CODE (reg) == ZERO_EXTRACT
2419 || GET_CODE (reg) == STRICT_LOW_PART
2420 || GET_CODE (reg) == SUBREG)
2421 reg = XEXP (reg, 0);
2422 gcc_assert (REG_P (reg));
2424 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2426 df_insn_rescan (insn);
2429 /* Return TRUE if combine can reuse reg X in mode MODE.
2430 ADDED_SETS is nonzero if the original set is still required. */
2431 static bool
2432 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2434 unsigned int regno;
2436 if (!REG_P (x))
2437 return false;
2439 regno = REGNO (x);
2440 /* Allow hard registers if the new mode is legal, and occupies no more
2441 registers than the old mode. */
2442 if (regno < FIRST_PSEUDO_REGISTER)
2443 return (HARD_REGNO_MODE_OK (regno, mode)
2444 && (hard_regno_nregs[regno][GET_MODE (x)]
2445 >= hard_regno_nregs[regno][mode]));
2447 /* Or a pseudo that is only used once. */
2448 return (regno < reg_n_sets_max
2449 && REG_N_SETS (regno) == 1
2450 && !added_sets
2451 && !REG_USERVAR_P (x));
2455 /* Check whether X, the destination of a set, refers to part of
2456 the register specified by REG. */
2458 static bool
2459 reg_subword_p (rtx x, rtx reg)
2461 /* Check that reg is an integer mode register. */
2462 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2463 return false;
2465 if (GET_CODE (x) == STRICT_LOW_PART
2466 || GET_CODE (x) == ZERO_EXTRACT)
2467 x = XEXP (x, 0);
2469 return GET_CODE (x) == SUBREG
2470 && SUBREG_REG (x) == reg
2471 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2474 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2475 Note that the INSN should be deleted *after* removing dead edges, so
2476 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2477 but not for a (set (pc) (label_ref FOO)). */
2479 static void
2480 update_cfg_for_uncondjump (rtx_insn *insn)
2482 basic_block bb = BLOCK_FOR_INSN (insn);
2483 gcc_assert (BB_END (bb) == insn);
2485 purge_dead_edges (bb);
2487 delete_insn (insn);
2488 if (EDGE_COUNT (bb->succs) == 1)
2490 rtx_insn *insn;
2492 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2494 /* Remove barriers from the footer if there are any. */
2495 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2496 if (BARRIER_P (insn))
2498 if (PREV_INSN (insn))
2499 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2500 else
2501 BB_FOOTER (bb) = NEXT_INSN (insn);
2502 if (NEXT_INSN (insn))
2503 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2505 else if (LABEL_P (insn))
2506 break;
2510 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2511 by an arbitrary number of CLOBBERs. */
2512 static bool
2513 is_parallel_of_n_reg_sets (rtx pat, int n)
2515 if (GET_CODE (pat) != PARALLEL)
2516 return false;
2518 int len = XVECLEN (pat, 0);
2519 if (len < n)
2520 return false;
2522 int i;
2523 for (i = 0; i < n; i++)
2524 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2525 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2526 return false;
2527 for ( ; i < len; i++)
2528 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2529 return false;
2531 return true;
2534 #if !HAVE_cc0
2535 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2536 CLOBBERs), can be split into individual SETs in that order, without
2537 changing semantics. */
2538 static bool
2539 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2541 if (!insn_nothrow_p (insn))
2542 return false;
2544 rtx pat = PATTERN (insn);
2546 int i, j;
2547 for (i = 0; i < n; i++)
2549 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2550 return false;
2552 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2554 for (j = i + 1; j < n; j++)
2555 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2556 return false;
2559 return true;
2561 #endif
2563 /* Try to combine the insns I0, I1 and I2 into I3.
2564 Here I0, I1 and I2 appear earlier than I3.
2565 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2568 If we are combining more than two insns and the resulting insn is not
2569 recognized, try splitting it into two insns. If that happens, I2 and I3
2570 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2571 Otherwise, I0, I1 and I2 are pseudo-deleted.
2573 Return 0 if the combination does not work. Then nothing is changed.
2574 If we did the combination, return the insn at which combine should
2575 resume scanning.
2577 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2578 new direct jump instruction.
2580 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2581 been I3 passed to an earlier try_combine within the same basic
2582 block. */
2584 static rtx_insn *
2585 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2586 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2588 /* New patterns for I3 and I2, respectively. */
2589 rtx newpat, newi2pat = 0;
2590 rtvec newpat_vec_with_clobbers = 0;
2591 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2592 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2593 dead. */
2594 int added_sets_0, added_sets_1, added_sets_2;
2595 /* Total number of SETs to put into I3. */
2596 int total_sets;
2597 /* Nonzero if I2's or I1's body now appears in I3. */
2598 int i2_is_used = 0, i1_is_used = 0;
2599 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2600 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2601 /* Contains I3 if the destination of I3 is used in its source, which means
2602 that the old life of I3 is being killed. If that usage is placed into
2603 I2 and not in I3, a REG_DEAD note must be made. */
2604 rtx i3dest_killed = 0;
2605 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2606 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2607 /* Copy of SET_SRC of I1 and I0, if needed. */
2608 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2609 /* Set if I2DEST was reused as a scratch register. */
2610 bool i2scratch = false;
2611 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2612 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2613 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2614 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2615 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2616 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2617 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2618 /* Notes that must be added to REG_NOTES in I3 and I2. */
2619 rtx new_i3_notes, new_i2_notes;
2620 /* Notes that we substituted I3 into I2 instead of the normal case. */
2621 int i3_subst_into_i2 = 0;
2622 /* Notes that I1, I2 or I3 is a MULT operation. */
2623 int have_mult = 0;
2624 int swap_i2i3 = 0;
2625 int changed_i3_dest = 0;
2627 int maxreg;
2628 rtx_insn *temp_insn;
2629 rtx temp_expr;
2630 struct insn_link *link;
2631 rtx other_pat = 0;
2632 rtx new_other_notes;
2633 int i;
2635 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2636 never be). */
2637 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2638 return 0;
2640 /* Only try four-insn combinations when there's high likelihood of
2641 success. Look for simple insns, such as loads of constants or
2642 binary operations involving a constant. */
2643 if (i0)
2645 int i;
2646 int ngood = 0;
2647 int nshift = 0;
2648 rtx set0, set3;
2650 if (!flag_expensive_optimizations)
2651 return 0;
2653 for (i = 0; i < 4; i++)
2655 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2656 rtx set = single_set (insn);
2657 rtx src;
2658 if (!set)
2659 continue;
2660 src = SET_SRC (set);
2661 if (CONSTANT_P (src))
2663 ngood += 2;
2664 break;
2666 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2667 ngood++;
2668 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2669 || GET_CODE (src) == LSHIFTRT)
2670 nshift++;
2673 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2674 are likely manipulating its value. Ideally we'll be able to combine
2675 all four insns into a bitfield insertion of some kind.
2677 Note the source in I0 might be inside a sign/zero extension and the
2678 memory modes in I0 and I3 might be different. So extract the address
2679 from the destination of I3 and search for it in the source of I0.
2681 In the event that there's a match but the source/dest do not actually
2682 refer to the same memory, the worst that happens is we try some
2683 combinations that we wouldn't have otherwise. */
2684 if ((set0 = single_set (i0))
2685 /* Ensure the source of SET0 is a MEM, possibly buried inside
2686 an extension. */
2687 && (GET_CODE (SET_SRC (set0)) == MEM
2688 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2689 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2690 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2691 && (set3 = single_set (i3))
2692 /* Ensure the destination of SET3 is a MEM. */
2693 && GET_CODE (SET_DEST (set3)) == MEM
2694 /* Would it be better to extract the base address for the MEM
2695 in SET3 and look for that? I don't have cases where it matters
2696 but I could envision such cases. */
2697 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2698 ngood += 2;
2700 if (ngood < 2 && nshift < 2)
2701 return 0;
2704 /* Exit early if one of the insns involved can't be used for
2705 combinations. */
2706 if (CALL_P (i2)
2707 || (i1 && CALL_P (i1))
2708 || (i0 && CALL_P (i0))
2709 || cant_combine_insn_p (i3)
2710 || cant_combine_insn_p (i2)
2711 || (i1 && cant_combine_insn_p (i1))
2712 || (i0 && cant_combine_insn_p (i0))
2713 || likely_spilled_retval_p (i3))
2714 return 0;
2716 combine_attempts++;
2717 undobuf.other_insn = 0;
2719 /* Reset the hard register usage information. */
2720 CLEAR_HARD_REG_SET (newpat_used_regs);
2722 if (dump_file && (dump_flags & TDF_DETAILS))
2724 if (i0)
2725 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2726 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2727 else if (i1)
2728 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2729 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2730 else
2731 fprintf (dump_file, "\nTrying %d -> %d:\n",
2732 INSN_UID (i2), INSN_UID (i3));
2735 /* If multiple insns feed into one of I2 or I3, they can be in any
2736 order. To simplify the code below, reorder them in sequence. */
2737 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2738 temp_insn = i2, i2 = i0, i0 = temp_insn;
2739 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2740 temp_insn = i1, i1 = i0, i0 = temp_insn;
2741 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2742 temp_insn = i1, i1 = i2, i2 = temp_insn;
2744 added_links_insn = 0;
2746 /* First check for one important special case that the code below will
2747 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2748 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2749 we may be able to replace that destination with the destination of I3.
2750 This occurs in the common code where we compute both a quotient and
2751 remainder into a structure, in which case we want to do the computation
2752 directly into the structure to avoid register-register copies.
2754 Note that this case handles both multiple sets in I2 and also cases
2755 where I2 has a number of CLOBBERs inside the PARALLEL.
2757 We make very conservative checks below and only try to handle the
2758 most common cases of this. For example, we only handle the case
2759 where I2 and I3 are adjacent to avoid making difficult register
2760 usage tests. */
2762 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2763 && REG_P (SET_SRC (PATTERN (i3)))
2764 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2765 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2766 && GET_CODE (PATTERN (i2)) == PARALLEL
2767 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2768 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2769 below would need to check what is inside (and reg_overlap_mentioned_p
2770 doesn't support those codes anyway). Don't allow those destinations;
2771 the resulting insn isn't likely to be recognized anyway. */
2772 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2773 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2774 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2775 SET_DEST (PATTERN (i3)))
2776 && next_active_insn (i2) == i3)
2778 rtx p2 = PATTERN (i2);
2780 /* Make sure that the destination of I3,
2781 which we are going to substitute into one output of I2,
2782 is not used within another output of I2. We must avoid making this:
2783 (parallel [(set (mem (reg 69)) ...)
2784 (set (reg 69) ...)])
2785 which is not well-defined as to order of actions.
2786 (Besides, reload can't handle output reloads for this.)
2788 The problem can also happen if the dest of I3 is a memory ref,
2789 if another dest in I2 is an indirect memory ref. */
2790 for (i = 0; i < XVECLEN (p2, 0); i++)
2791 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2792 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2793 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2794 SET_DEST (XVECEXP (p2, 0, i))))
2795 break;
2797 /* Make sure this PARALLEL is not an asm. We do not allow combining
2798 that usually (see can_combine_p), so do not here either. */
2799 for (i = 0; i < XVECLEN (p2, 0); i++)
2800 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2801 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2802 break;
2804 if (i == XVECLEN (p2, 0))
2805 for (i = 0; i < XVECLEN (p2, 0); i++)
2806 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2807 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2809 combine_merges++;
2811 subst_insn = i3;
2812 subst_low_luid = DF_INSN_LUID (i2);
2814 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2815 i2src = SET_SRC (XVECEXP (p2, 0, i));
2816 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2817 i2dest_killed = dead_or_set_p (i2, i2dest);
2819 /* Replace the dest in I2 with our dest and make the resulting
2820 insn the new pattern for I3. Then skip to where we validate
2821 the pattern. Everything was set up above. */
2822 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2823 newpat = p2;
2824 i3_subst_into_i2 = 1;
2825 goto validate_replacement;
2829 /* If I2 is setting a pseudo to a constant and I3 is setting some
2830 sub-part of it to another constant, merge them by making a new
2831 constant. */
2832 if (i1 == 0
2833 && (temp_expr = single_set (i2)) != 0
2834 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2835 && GET_CODE (PATTERN (i3)) == SET
2836 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2837 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2839 rtx dest = SET_DEST (PATTERN (i3));
2840 int offset = -1;
2841 int width = 0;
2843 if (GET_CODE (dest) == ZERO_EXTRACT)
2845 if (CONST_INT_P (XEXP (dest, 1))
2846 && CONST_INT_P (XEXP (dest, 2)))
2848 width = INTVAL (XEXP (dest, 1));
2849 offset = INTVAL (XEXP (dest, 2));
2850 dest = XEXP (dest, 0);
2851 if (BITS_BIG_ENDIAN)
2852 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2855 else
2857 if (GET_CODE (dest) == STRICT_LOW_PART)
2858 dest = XEXP (dest, 0);
2859 width = GET_MODE_PRECISION (GET_MODE (dest));
2860 offset = 0;
2863 if (offset >= 0)
2865 /* If this is the low part, we're done. */
2866 if (subreg_lowpart_p (dest))
2868 /* Handle the case where inner is twice the size of outer. */
2869 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2870 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2871 offset += GET_MODE_PRECISION (GET_MODE (dest));
2872 /* Otherwise give up for now. */
2873 else
2874 offset = -1;
2877 if (offset >= 0)
2879 rtx inner = SET_SRC (PATTERN (i3));
2880 rtx outer = SET_SRC (temp_expr);
2882 wide_int o
2883 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2884 std::make_pair (inner, GET_MODE (dest)),
2885 offset, width);
2887 combine_merges++;
2888 subst_insn = i3;
2889 subst_low_luid = DF_INSN_LUID (i2);
2890 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2891 i2dest = SET_DEST (temp_expr);
2892 i2dest_killed = dead_or_set_p (i2, i2dest);
2894 /* Replace the source in I2 with the new constant and make the
2895 resulting insn the new pattern for I3. Then skip to where we
2896 validate the pattern. Everything was set up above. */
2897 SUBST (SET_SRC (temp_expr),
2898 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2900 newpat = PATTERN (i2);
2902 /* The dest of I3 has been replaced with the dest of I2. */
2903 changed_i3_dest = 1;
2904 goto validate_replacement;
2908 #if !HAVE_cc0
2909 /* If we have no I1 and I2 looks like:
2910 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2911 (set Y OP)])
2912 make up a dummy I1 that is
2913 (set Y OP)
2914 and change I2 to be
2915 (set (reg:CC X) (compare:CC Y (const_int 0)))
2917 (We can ignore any trailing CLOBBERs.)
2919 This undoes a previous combination and allows us to match a branch-and-
2920 decrement insn. */
2922 if (i1 == 0
2923 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2924 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2925 == MODE_CC)
2926 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2927 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2928 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2929 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2930 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2931 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2933 /* We make I1 with the same INSN_UID as I2. This gives it
2934 the same DF_INSN_LUID for value tracking. Our fake I1 will
2935 never appear in the insn stream so giving it the same INSN_UID
2936 as I2 will not cause a problem. */
2938 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2939 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2940 -1, NULL_RTX);
2941 INSN_UID (i1) = INSN_UID (i2);
2943 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2944 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2945 SET_DEST (PATTERN (i1)));
2946 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2947 SUBST_LINK (LOG_LINKS (i2),
2948 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2951 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2952 make those two SETs separate I1 and I2 insns, and make an I0 that is
2953 the original I1. */
2954 if (i0 == 0
2955 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2956 && can_split_parallel_of_n_reg_sets (i2, 2)
2957 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2958 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2960 /* If there is no I1, there is no I0 either. */
2961 i0 = i1;
2963 /* We make I1 with the same INSN_UID as I2. This gives it
2964 the same DF_INSN_LUID for value tracking. Our fake I1 will
2965 never appear in the insn stream so giving it the same INSN_UID
2966 as I2 will not cause a problem. */
2968 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2969 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2970 -1, NULL_RTX);
2971 INSN_UID (i1) = INSN_UID (i2);
2973 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2975 #endif
2977 /* Verify that I2 and I1 are valid for combining. */
2978 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2979 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2980 &i1dest, &i1src))
2981 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2982 &i0dest, &i0src)))
2984 undo_all ();
2985 return 0;
2988 /* Record whether I2DEST is used in I2SRC and similarly for the other
2989 cases. Knowing this will help in register status updating below. */
2990 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2991 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2992 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2993 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2994 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2995 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2996 i2dest_killed = dead_or_set_p (i2, i2dest);
2997 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2998 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3000 /* For the earlier insns, determine which of the subsequent ones they
3001 feed. */
3002 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3003 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3004 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3005 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3006 && reg_overlap_mentioned_p (i0dest, i2src))));
3008 /* Ensure that I3's pattern can be the destination of combines. */
3009 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3010 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3011 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3012 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3013 &i3dest_killed))
3015 undo_all ();
3016 return 0;
3019 /* See if any of the insns is a MULT operation. Unless one is, we will
3020 reject a combination that is, since it must be slower. Be conservative
3021 here. */
3022 if (GET_CODE (i2src) == MULT
3023 || (i1 != 0 && GET_CODE (i1src) == MULT)
3024 || (i0 != 0 && GET_CODE (i0src) == MULT)
3025 || (GET_CODE (PATTERN (i3)) == SET
3026 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3027 have_mult = 1;
3029 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3030 We used to do this EXCEPT in one case: I3 has a post-inc in an
3031 output operand. However, that exception can give rise to insns like
3032 mov r3,(r3)+
3033 which is a famous insn on the PDP-11 where the value of r3 used as the
3034 source was model-dependent. Avoid this sort of thing. */
3036 #if 0
3037 if (!(GET_CODE (PATTERN (i3)) == SET
3038 && REG_P (SET_SRC (PATTERN (i3)))
3039 && MEM_P (SET_DEST (PATTERN (i3)))
3040 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3041 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3042 /* It's not the exception. */
3043 #endif
3044 #ifdef AUTO_INC_DEC
3046 rtx link;
3047 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3048 if (REG_NOTE_KIND (link) == REG_INC
3049 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3050 || (i1 != 0
3051 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3053 undo_all ();
3054 return 0;
3057 #endif
3059 /* See if the SETs in I1 or I2 need to be kept around in the merged
3060 instruction: whenever the value set there is still needed past I3.
3061 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3063 For the SET in I1, we have two cases: if I1 and I2 independently feed
3064 into I3, the set in I1 needs to be kept around unless I1DEST dies
3065 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3066 in I1 needs to be kept around unless I1DEST dies or is set in either
3067 I2 or I3. The same considerations apply to I0. */
3069 added_sets_2 = !dead_or_set_p (i3, i2dest);
3071 if (i1)
3072 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3073 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3074 else
3075 added_sets_1 = 0;
3077 if (i0)
3078 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3079 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3080 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3081 && dead_or_set_p (i2, i0dest)));
3082 else
3083 added_sets_0 = 0;
3085 /* We are about to copy insns for the case where they need to be kept
3086 around. Check that they can be copied in the merged instruction. */
3088 if (targetm.cannot_copy_insn_p
3089 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3090 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3091 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3093 undo_all ();
3094 return 0;
3097 /* If the set in I2 needs to be kept around, we must make a copy of
3098 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3099 PATTERN (I2), we are only substituting for the original I1DEST, not into
3100 an already-substituted copy. This also prevents making self-referential
3101 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3102 I2DEST. */
3104 if (added_sets_2)
3106 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3107 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3108 else
3109 i2pat = copy_rtx (PATTERN (i2));
3112 if (added_sets_1)
3114 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3115 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3116 else
3117 i1pat = copy_rtx (PATTERN (i1));
3120 if (added_sets_0)
3122 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3123 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3124 else
3125 i0pat = copy_rtx (PATTERN (i0));
3128 combine_merges++;
3130 /* Substitute in the latest insn for the regs set by the earlier ones. */
3132 maxreg = max_reg_num ();
3134 subst_insn = i3;
3136 /* Many machines that don't use CC0 have insns that can both perform an
3137 arithmetic operation and set the condition code. These operations will
3138 be represented as a PARALLEL with the first element of the vector
3139 being a COMPARE of an arithmetic operation with the constant zero.
3140 The second element of the vector will set some pseudo to the result
3141 of the same arithmetic operation. If we simplify the COMPARE, we won't
3142 match such a pattern and so will generate an extra insn. Here we test
3143 for this case, where both the comparison and the operation result are
3144 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3145 I2SRC. Later we will make the PARALLEL that contains I2. */
3147 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3148 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3149 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3150 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3152 rtx newpat_dest;
3153 rtx *cc_use_loc = NULL;
3154 rtx_insn *cc_use_insn = NULL;
3155 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3156 machine_mode compare_mode, orig_compare_mode;
3157 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3159 newpat = PATTERN (i3);
3160 newpat_dest = SET_DEST (newpat);
3161 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3163 if (undobuf.other_insn == 0
3164 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3165 &cc_use_insn)))
3167 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3168 compare_code = simplify_compare_const (compare_code,
3169 GET_MODE (i2dest), op0, &op1);
3170 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3173 /* Do the rest only if op1 is const0_rtx, which may be the
3174 result of simplification. */
3175 if (op1 == const0_rtx)
3177 /* If a single use of the CC is found, prepare to modify it
3178 when SELECT_CC_MODE returns a new CC-class mode, or when
3179 the above simplify_compare_const() returned a new comparison
3180 operator. undobuf.other_insn is assigned the CC use insn
3181 when modifying it. */
3182 if (cc_use_loc)
3184 #ifdef SELECT_CC_MODE
3185 machine_mode new_mode
3186 = SELECT_CC_MODE (compare_code, op0, op1);
3187 if (new_mode != orig_compare_mode
3188 && can_change_dest_mode (SET_DEST (newpat),
3189 added_sets_2, new_mode))
3191 unsigned int regno = REGNO (newpat_dest);
3192 compare_mode = new_mode;
3193 if (regno < FIRST_PSEUDO_REGISTER)
3194 newpat_dest = gen_rtx_REG (compare_mode, regno);
3195 else
3197 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3198 newpat_dest = regno_reg_rtx[regno];
3201 #endif
3202 /* Cases for modifying the CC-using comparison. */
3203 if (compare_code != orig_compare_code
3204 /* ??? Do we need to verify the zero rtx? */
3205 && XEXP (*cc_use_loc, 1) == const0_rtx)
3207 /* Replace cc_use_loc with entire new RTX. */
3208 SUBST (*cc_use_loc,
3209 gen_rtx_fmt_ee (compare_code, compare_mode,
3210 newpat_dest, const0_rtx));
3211 undobuf.other_insn = cc_use_insn;
3213 else if (compare_mode != orig_compare_mode)
3215 /* Just replace the CC reg with a new mode. */
3216 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3217 undobuf.other_insn = cc_use_insn;
3221 /* Now we modify the current newpat:
3222 First, SET_DEST(newpat) is updated if the CC mode has been
3223 altered. For targets without SELECT_CC_MODE, this should be
3224 optimized away. */
3225 if (compare_mode != orig_compare_mode)
3226 SUBST (SET_DEST (newpat), newpat_dest);
3227 /* This is always done to propagate i2src into newpat. */
3228 SUBST (SET_SRC (newpat),
3229 gen_rtx_COMPARE (compare_mode, op0, op1));
3230 /* Create new version of i2pat if needed; the below PARALLEL
3231 creation needs this to work correctly. */
3232 if (! rtx_equal_p (i2src, op0))
3233 i2pat = gen_rtx_SET (i2dest, op0);
3234 i2_is_used = 1;
3238 if (i2_is_used == 0)
3240 /* It is possible that the source of I2 or I1 may be performing
3241 an unneeded operation, such as a ZERO_EXTEND of something
3242 that is known to have the high part zero. Handle that case
3243 by letting subst look at the inner insns.
3245 Another way to do this would be to have a function that tries
3246 to simplify a single insn instead of merging two or more
3247 insns. We don't do this because of the potential of infinite
3248 loops and because of the potential extra memory required.
3249 However, doing it the way we are is a bit of a kludge and
3250 doesn't catch all cases.
3252 But only do this if -fexpensive-optimizations since it slows
3253 things down and doesn't usually win.
3255 This is not done in the COMPARE case above because the
3256 unmodified I2PAT is used in the PARALLEL and so a pattern
3257 with a modified I2SRC would not match. */
3259 if (flag_expensive_optimizations)
3261 /* Pass pc_rtx so no substitutions are done, just
3262 simplifications. */
3263 if (i1)
3265 subst_low_luid = DF_INSN_LUID (i1);
3266 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3269 subst_low_luid = DF_INSN_LUID (i2);
3270 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3273 n_occurrences = 0; /* `subst' counts here */
3274 subst_low_luid = DF_INSN_LUID (i2);
3276 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3277 copy of I2SRC each time we substitute it, in order to avoid creating
3278 self-referential RTL when we will be substituting I1SRC for I1DEST
3279 later. Likewise if I0 feeds into I2, either directly or indirectly
3280 through I1, and I0DEST is in I0SRC. */
3281 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3282 (i1_feeds_i2_n && i1dest_in_i1src)
3283 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3284 && i0dest_in_i0src));
3285 substed_i2 = 1;
3287 /* Record whether I2's body now appears within I3's body. */
3288 i2_is_used = n_occurrences;
3291 /* If we already got a failure, don't try to do more. Otherwise, try to
3292 substitute I1 if we have it. */
3294 if (i1 && GET_CODE (newpat) != CLOBBER)
3296 /* Check that an autoincrement side-effect on I1 has not been lost.
3297 This happens if I1DEST is mentioned in I2 and dies there, and
3298 has disappeared from the new pattern. */
3299 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3300 && i1_feeds_i2_n
3301 && dead_or_set_p (i2, i1dest)
3302 && !reg_overlap_mentioned_p (i1dest, newpat))
3303 /* Before we can do this substitution, we must redo the test done
3304 above (see detailed comments there) that ensures I1DEST isn't
3305 mentioned in any SETs in NEWPAT that are field assignments. */
3306 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3307 0, 0, 0))
3309 undo_all ();
3310 return 0;
3313 n_occurrences = 0;
3314 subst_low_luid = DF_INSN_LUID (i1);
3316 /* If the following substitution will modify I1SRC, make a copy of it
3317 for the case where it is substituted for I1DEST in I2PAT later. */
3318 if (added_sets_2 && i1_feeds_i2_n)
3319 i1src_copy = copy_rtx (i1src);
3321 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3322 copy of I1SRC each time we substitute it, in order to avoid creating
3323 self-referential RTL when we will be substituting I0SRC for I0DEST
3324 later. */
3325 newpat = subst (newpat, i1dest, i1src, 0, 0,
3326 i0_feeds_i1_n && i0dest_in_i0src);
3327 substed_i1 = 1;
3329 /* Record whether I1's body now appears within I3's body. */
3330 i1_is_used = n_occurrences;
3333 /* Likewise for I0 if we have it. */
3335 if (i0 && GET_CODE (newpat) != CLOBBER)
3337 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3338 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3339 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3340 && !reg_overlap_mentioned_p (i0dest, newpat))
3341 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3342 0, 0, 0))
3344 undo_all ();
3345 return 0;
3348 /* If the following substitution will modify I0SRC, make a copy of it
3349 for the case where it is substituted for I0DEST in I1PAT later. */
3350 if (added_sets_1 && i0_feeds_i1_n)
3351 i0src_copy = copy_rtx (i0src);
3352 /* And a copy for I0DEST in I2PAT substitution. */
3353 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3354 || (i0_feeds_i2_n)))
3355 i0src_copy2 = copy_rtx (i0src);
3357 n_occurrences = 0;
3358 subst_low_luid = DF_INSN_LUID (i0);
3359 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3360 substed_i0 = 1;
3363 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3364 to count all the ways that I2SRC and I1SRC can be used. */
3365 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3366 && i2_is_used + added_sets_2 > 1)
3367 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3368 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3369 > 1))
3370 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3371 && (n_occurrences + added_sets_0
3372 + (added_sets_1 && i0_feeds_i1_n)
3373 + (added_sets_2 && i0_feeds_i2_n)
3374 > 1))
3375 /* Fail if we tried to make a new register. */
3376 || max_reg_num () != maxreg
3377 /* Fail if we couldn't do something and have a CLOBBER. */
3378 || GET_CODE (newpat) == CLOBBER
3379 /* Fail if this new pattern is a MULT and we didn't have one before
3380 at the outer level. */
3381 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3382 && ! have_mult))
3384 undo_all ();
3385 return 0;
3388 /* If the actions of the earlier insns must be kept
3389 in addition to substituting them into the latest one,
3390 we must make a new PARALLEL for the latest insn
3391 to hold additional the SETs. */
3393 if (added_sets_0 || added_sets_1 || added_sets_2)
3395 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3396 combine_extras++;
3398 if (GET_CODE (newpat) == PARALLEL)
3400 rtvec old = XVEC (newpat, 0);
3401 total_sets = XVECLEN (newpat, 0) + extra_sets;
3402 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3403 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3404 sizeof (old->elem[0]) * old->num_elem);
3406 else
3408 rtx old = newpat;
3409 total_sets = 1 + extra_sets;
3410 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3411 XVECEXP (newpat, 0, 0) = old;
3414 if (added_sets_0)
3415 XVECEXP (newpat, 0, --total_sets) = i0pat;
3417 if (added_sets_1)
3419 rtx t = i1pat;
3420 if (i0_feeds_i1_n)
3421 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3423 XVECEXP (newpat, 0, --total_sets) = t;
3425 if (added_sets_2)
3427 rtx t = i2pat;
3428 if (i1_feeds_i2_n)
3429 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3430 i0_feeds_i1_n && i0dest_in_i0src);
3431 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3432 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3434 XVECEXP (newpat, 0, --total_sets) = t;
3438 validate_replacement:
3440 /* Note which hard regs this insn has as inputs. */
3441 mark_used_regs_combine (newpat);
3443 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3444 consider splitting this pattern, we might need these clobbers. */
3445 if (i1 && GET_CODE (newpat) == PARALLEL
3446 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3448 int len = XVECLEN (newpat, 0);
3450 newpat_vec_with_clobbers = rtvec_alloc (len);
3451 for (i = 0; i < len; i++)
3452 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3455 /* We have recognized nothing yet. */
3456 insn_code_number = -1;
3458 /* See if this is a PARALLEL of two SETs where one SET's destination is
3459 a register that is unused and this isn't marked as an instruction that
3460 might trap in an EH region. In that case, we just need the other SET.
3461 We prefer this over the PARALLEL.
3463 This can occur when simplifying a divmod insn. We *must* test for this
3464 case here because the code below that splits two independent SETs doesn't
3465 handle this case correctly when it updates the register status.
3467 It's pointless doing this if we originally had two sets, one from
3468 i3, and one from i2. Combining then splitting the parallel results
3469 in the original i2 again plus an invalid insn (which we delete).
3470 The net effect is only to move instructions around, which makes
3471 debug info less accurate. */
3473 if (!(added_sets_2 && i1 == 0)
3474 && is_parallel_of_n_reg_sets (newpat, 2)
3475 && asm_noperands (newpat) < 0)
3477 rtx set0 = XVECEXP (newpat, 0, 0);
3478 rtx set1 = XVECEXP (newpat, 0, 1);
3479 rtx oldpat = newpat;
3481 if (((REG_P (SET_DEST (set1))
3482 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3483 || (GET_CODE (SET_DEST (set1)) == SUBREG
3484 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3485 && insn_nothrow_p (i3)
3486 && !side_effects_p (SET_SRC (set1)))
3488 newpat = set0;
3489 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3492 else if (((REG_P (SET_DEST (set0))
3493 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3494 || (GET_CODE (SET_DEST (set0)) == SUBREG
3495 && find_reg_note (i3, REG_UNUSED,
3496 SUBREG_REG (SET_DEST (set0)))))
3497 && insn_nothrow_p (i3)
3498 && !side_effects_p (SET_SRC (set0)))
3500 newpat = set1;
3501 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3503 if (insn_code_number >= 0)
3504 changed_i3_dest = 1;
3507 if (insn_code_number < 0)
3508 newpat = oldpat;
3511 /* Is the result of combination a valid instruction? */
3512 if (insn_code_number < 0)
3513 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3515 /* If we were combining three insns and the result is a simple SET
3516 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3517 insns. There are two ways to do this. It can be split using a
3518 machine-specific method (like when you have an addition of a large
3519 constant) or by combine in the function find_split_point. */
3521 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3522 && asm_noperands (newpat) < 0)
3524 rtx parallel, *split;
3525 rtx_insn *m_split_insn;
3527 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3528 use I2DEST as a scratch register will help. In the latter case,
3529 convert I2DEST to the mode of the source of NEWPAT if we can. */
3531 m_split_insn = combine_split_insns (newpat, i3);
3533 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3534 inputs of NEWPAT. */
3536 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3537 possible to try that as a scratch reg. This would require adding
3538 more code to make it work though. */
3540 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3542 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3544 /* First try to split using the original register as a
3545 scratch register. */
3546 parallel = gen_rtx_PARALLEL (VOIDmode,
3547 gen_rtvec (2, newpat,
3548 gen_rtx_CLOBBER (VOIDmode,
3549 i2dest)));
3550 m_split_insn = combine_split_insns (parallel, i3);
3552 /* If that didn't work, try changing the mode of I2DEST if
3553 we can. */
3554 if (m_split_insn == 0
3555 && new_mode != GET_MODE (i2dest)
3556 && new_mode != VOIDmode
3557 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3559 machine_mode old_mode = GET_MODE (i2dest);
3560 rtx ni2dest;
3562 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3563 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3564 else
3566 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3567 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3570 parallel = (gen_rtx_PARALLEL
3571 (VOIDmode,
3572 gen_rtvec (2, newpat,
3573 gen_rtx_CLOBBER (VOIDmode,
3574 ni2dest))));
3575 m_split_insn = combine_split_insns (parallel, i3);
3577 if (m_split_insn == 0
3578 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3580 struct undo *buf;
3582 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3583 buf = undobuf.undos;
3584 undobuf.undos = buf->next;
3585 buf->next = undobuf.frees;
3586 undobuf.frees = buf;
3590 i2scratch = m_split_insn != 0;
3593 /* If recog_for_combine has discarded clobbers, try to use them
3594 again for the split. */
3595 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3597 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3598 m_split_insn = combine_split_insns (parallel, i3);
3601 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3603 rtx m_split_pat = PATTERN (m_split_insn);
3604 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3605 if (insn_code_number >= 0)
3606 newpat = m_split_pat;
3608 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3609 && (next_nonnote_nondebug_insn (i2) == i3
3610 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3612 rtx i2set, i3set;
3613 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3614 newi2pat = PATTERN (m_split_insn);
3616 i3set = single_set (NEXT_INSN (m_split_insn));
3617 i2set = single_set (m_split_insn);
3619 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3621 /* If I2 or I3 has multiple SETs, we won't know how to track
3622 register status, so don't use these insns. If I2's destination
3623 is used between I2 and I3, we also can't use these insns. */
3625 if (i2_code_number >= 0 && i2set && i3set
3626 && (next_nonnote_nondebug_insn (i2) == i3
3627 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3628 insn_code_number = recog_for_combine (&newi3pat, i3,
3629 &new_i3_notes);
3630 if (insn_code_number >= 0)
3631 newpat = newi3pat;
3633 /* It is possible that both insns now set the destination of I3.
3634 If so, we must show an extra use of it. */
3636 if (insn_code_number >= 0)
3638 rtx new_i3_dest = SET_DEST (i3set);
3639 rtx new_i2_dest = SET_DEST (i2set);
3641 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3642 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3643 || GET_CODE (new_i3_dest) == SUBREG)
3644 new_i3_dest = XEXP (new_i3_dest, 0);
3646 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3647 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3648 || GET_CODE (new_i2_dest) == SUBREG)
3649 new_i2_dest = XEXP (new_i2_dest, 0);
3651 if (REG_P (new_i3_dest)
3652 && REG_P (new_i2_dest)
3653 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3654 && REGNO (new_i2_dest) < reg_n_sets_max)
3655 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3659 /* If we can split it and use I2DEST, go ahead and see if that
3660 helps things be recognized. Verify that none of the registers
3661 are set between I2 and I3. */
3662 if (insn_code_number < 0
3663 && (split = find_split_point (&newpat, i3, false)) != 0
3664 && (!HAVE_cc0 || REG_P (i2dest))
3665 /* We need I2DEST in the proper mode. If it is a hard register
3666 or the only use of a pseudo, we can change its mode.
3667 Make sure we don't change a hard register to have a mode that
3668 isn't valid for it, or change the number of registers. */
3669 && (GET_MODE (*split) == GET_MODE (i2dest)
3670 || GET_MODE (*split) == VOIDmode
3671 || can_change_dest_mode (i2dest, added_sets_2,
3672 GET_MODE (*split)))
3673 && (next_nonnote_nondebug_insn (i2) == i3
3674 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3675 /* We can't overwrite I2DEST if its value is still used by
3676 NEWPAT. */
3677 && ! reg_referenced_p (i2dest, newpat))
3679 rtx newdest = i2dest;
3680 enum rtx_code split_code = GET_CODE (*split);
3681 machine_mode split_mode = GET_MODE (*split);
3682 bool subst_done = false;
3683 newi2pat = NULL_RTX;
3685 i2scratch = true;
3687 /* *SPLIT may be part of I2SRC, so make sure we have the
3688 original expression around for later debug processing.
3689 We should not need I2SRC any more in other cases. */
3690 if (MAY_HAVE_DEBUG_INSNS)
3691 i2src = copy_rtx (i2src);
3692 else
3693 i2src = NULL;
3695 /* Get NEWDEST as a register in the proper mode. We have already
3696 validated that we can do this. */
3697 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3699 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3700 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3701 else
3703 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3704 newdest = regno_reg_rtx[REGNO (i2dest)];
3708 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3709 an ASHIFT. This can occur if it was inside a PLUS and hence
3710 appeared to be a memory address. This is a kludge. */
3711 if (split_code == MULT
3712 && CONST_INT_P (XEXP (*split, 1))
3713 && INTVAL (XEXP (*split, 1)) > 0
3714 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3716 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3717 XEXP (*split, 0), GEN_INT (i)));
3718 /* Update split_code because we may not have a multiply
3719 anymore. */
3720 split_code = GET_CODE (*split);
3723 #ifdef INSN_SCHEDULING
3724 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3725 be written as a ZERO_EXTEND. */
3726 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3728 #ifdef LOAD_EXTEND_OP
3729 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3730 what it really is. */
3731 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3732 == SIGN_EXTEND)
3733 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3734 SUBREG_REG (*split)));
3735 else
3736 #endif
3737 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3738 SUBREG_REG (*split)));
3740 #endif
3742 /* Attempt to split binary operators using arithmetic identities. */
3743 if (BINARY_P (SET_SRC (newpat))
3744 && split_mode == GET_MODE (SET_SRC (newpat))
3745 && ! side_effects_p (SET_SRC (newpat)))
3747 rtx setsrc = SET_SRC (newpat);
3748 machine_mode mode = GET_MODE (setsrc);
3749 enum rtx_code code = GET_CODE (setsrc);
3750 rtx src_op0 = XEXP (setsrc, 0);
3751 rtx src_op1 = XEXP (setsrc, 1);
3753 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3754 if (rtx_equal_p (src_op0, src_op1))
3756 newi2pat = gen_rtx_SET (newdest, src_op0);
3757 SUBST (XEXP (setsrc, 0), newdest);
3758 SUBST (XEXP (setsrc, 1), newdest);
3759 subst_done = true;
3761 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3762 else if ((code == PLUS || code == MULT)
3763 && GET_CODE (src_op0) == code
3764 && GET_CODE (XEXP (src_op0, 0)) == code
3765 && (INTEGRAL_MODE_P (mode)
3766 || (FLOAT_MODE_P (mode)
3767 && flag_unsafe_math_optimizations)))
3769 rtx p = XEXP (XEXP (src_op0, 0), 0);
3770 rtx q = XEXP (XEXP (src_op0, 0), 1);
3771 rtx r = XEXP (src_op0, 1);
3772 rtx s = src_op1;
3774 /* Split both "((X op Y) op X) op Y" and
3775 "((X op Y) op Y) op X" as "T op T" where T is
3776 "X op Y". */
3777 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3778 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3780 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3781 SUBST (XEXP (setsrc, 0), newdest);
3782 SUBST (XEXP (setsrc, 1), newdest);
3783 subst_done = true;
3785 /* Split "((X op X) op Y) op Y)" as "T op T" where
3786 T is "X op Y". */
3787 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3789 rtx tmp = simplify_gen_binary (code, mode, p, r);
3790 newi2pat = gen_rtx_SET (newdest, tmp);
3791 SUBST (XEXP (setsrc, 0), newdest);
3792 SUBST (XEXP (setsrc, 1), newdest);
3793 subst_done = true;
3798 if (!subst_done)
3800 newi2pat = gen_rtx_SET (newdest, *split);
3801 SUBST (*split, newdest);
3804 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3806 /* recog_for_combine might have added CLOBBERs to newi2pat.
3807 Make sure NEWPAT does not depend on the clobbered regs. */
3808 if (GET_CODE (newi2pat) == PARALLEL)
3809 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3810 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3812 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3813 if (reg_overlap_mentioned_p (reg, newpat))
3815 undo_all ();
3816 return 0;
3820 /* If the split point was a MULT and we didn't have one before,
3821 don't use one now. */
3822 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3823 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3827 /* Check for a case where we loaded from memory in a narrow mode and
3828 then sign extended it, but we need both registers. In that case,
3829 we have a PARALLEL with both loads from the same memory location.
3830 We can split this into a load from memory followed by a register-register
3831 copy. This saves at least one insn, more if register allocation can
3832 eliminate the copy.
3834 We cannot do this if the destination of the first assignment is a
3835 condition code register or cc0. We eliminate this case by making sure
3836 the SET_DEST and SET_SRC have the same mode.
3838 We cannot do this if the destination of the second assignment is
3839 a register that we have already assumed is zero-extended. Similarly
3840 for a SUBREG of such a register. */
3842 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3843 && GET_CODE (newpat) == PARALLEL
3844 && XVECLEN (newpat, 0) == 2
3845 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3846 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3847 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3848 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3849 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3850 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3851 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3852 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3853 DF_INSN_LUID (i2))
3854 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3855 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3856 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3857 (REG_P (temp_expr)
3858 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3859 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3860 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3861 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3862 != GET_MODE_MASK (word_mode))))
3863 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3864 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3865 (REG_P (temp_expr)
3866 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3867 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3868 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3869 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3870 != GET_MODE_MASK (word_mode)))))
3871 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3872 SET_SRC (XVECEXP (newpat, 0, 1)))
3873 && ! find_reg_note (i3, REG_UNUSED,
3874 SET_DEST (XVECEXP (newpat, 0, 0))))
3876 rtx ni2dest;
3878 newi2pat = XVECEXP (newpat, 0, 0);
3879 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3880 newpat = XVECEXP (newpat, 0, 1);
3881 SUBST (SET_SRC (newpat),
3882 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3883 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3885 if (i2_code_number >= 0)
3886 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3888 if (insn_code_number >= 0)
3889 swap_i2i3 = 1;
3892 /* Similarly, check for a case where we have a PARALLEL of two independent
3893 SETs but we started with three insns. In this case, we can do the sets
3894 as two separate insns. This case occurs when some SET allows two
3895 other insns to combine, but the destination of that SET is still live.
3897 Also do this if we started with two insns and (at least) one of the
3898 resulting sets is a noop; this noop will be deleted later. */
3900 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3901 && GET_CODE (newpat) == PARALLEL
3902 && XVECLEN (newpat, 0) == 2
3903 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3904 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3905 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3906 || set_noop_p (XVECEXP (newpat, 0, 1)))
3907 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3908 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3909 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3910 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3911 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3912 XVECEXP (newpat, 0, 0))
3913 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3914 XVECEXP (newpat, 0, 1))
3915 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3916 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3918 rtx set0 = XVECEXP (newpat, 0, 0);
3919 rtx set1 = XVECEXP (newpat, 0, 1);
3921 /* Normally, it doesn't matter which of the two is done first,
3922 but the one that references cc0 can't be the second, and
3923 one which uses any regs/memory set in between i2 and i3 can't
3924 be first. The PARALLEL might also have been pre-existing in i3,
3925 so we need to make sure that we won't wrongly hoist a SET to i2
3926 that would conflict with a death note present in there. */
3927 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3928 && !(REG_P (SET_DEST (set1))
3929 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3930 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3931 && find_reg_note (i2, REG_DEAD,
3932 SUBREG_REG (SET_DEST (set1))))
3933 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
3934 /* If I3 is a jump, ensure that set0 is a jump so that
3935 we do not create invalid RTL. */
3936 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3939 newi2pat = set1;
3940 newpat = set0;
3942 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3943 && !(REG_P (SET_DEST (set0))
3944 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3945 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3946 && find_reg_note (i2, REG_DEAD,
3947 SUBREG_REG (SET_DEST (set0))))
3948 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
3949 /* If I3 is a jump, ensure that set1 is a jump so that
3950 we do not create invalid RTL. */
3951 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3954 newi2pat = set0;
3955 newpat = set1;
3957 else
3959 undo_all ();
3960 return 0;
3963 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3965 if (i2_code_number >= 0)
3967 /* recog_for_combine might have added CLOBBERs to newi2pat.
3968 Make sure NEWPAT does not depend on the clobbered regs. */
3969 if (GET_CODE (newi2pat) == PARALLEL)
3971 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3972 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3974 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3975 if (reg_overlap_mentioned_p (reg, newpat))
3977 undo_all ();
3978 return 0;
3983 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3987 /* If it still isn't recognized, fail and change things back the way they
3988 were. */
3989 if ((insn_code_number < 0
3990 /* Is the result a reasonable ASM_OPERANDS? */
3991 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3993 undo_all ();
3994 return 0;
3997 /* If we had to change another insn, make sure it is valid also. */
3998 if (undobuf.other_insn)
4000 CLEAR_HARD_REG_SET (newpat_used_regs);
4002 other_pat = PATTERN (undobuf.other_insn);
4003 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4004 &new_other_notes);
4006 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4008 undo_all ();
4009 return 0;
4013 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4014 they are adjacent to each other or not. */
4015 if (HAVE_cc0)
4017 rtx_insn *p = prev_nonnote_insn (i3);
4018 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4019 && sets_cc0_p (newi2pat))
4021 undo_all ();
4022 return 0;
4026 /* Only allow this combination if insn_rtx_costs reports that the
4027 replacement instructions are cheaper than the originals. */
4028 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4030 undo_all ();
4031 return 0;
4034 if (MAY_HAVE_DEBUG_INSNS)
4036 struct undo *undo;
4038 for (undo = undobuf.undos; undo; undo = undo->next)
4039 if (undo->kind == UNDO_MODE)
4041 rtx reg = *undo->where.r;
4042 machine_mode new_mode = GET_MODE (reg);
4043 machine_mode old_mode = undo->old_contents.m;
4045 /* Temporarily revert mode back. */
4046 adjust_reg_mode (reg, old_mode);
4048 if (reg == i2dest && i2scratch)
4050 /* If we used i2dest as a scratch register with a
4051 different mode, substitute it for the original
4052 i2src while its original mode is temporarily
4053 restored, and then clear i2scratch so that we don't
4054 do it again later. */
4055 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4056 this_basic_block);
4057 i2scratch = false;
4058 /* Put back the new mode. */
4059 adjust_reg_mode (reg, new_mode);
4061 else
4063 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4064 rtx_insn *first, *last;
4066 if (reg == i2dest)
4068 first = i2;
4069 last = last_combined_insn;
4071 else
4073 first = i3;
4074 last = undobuf.other_insn;
4075 gcc_assert (last);
4076 if (DF_INSN_LUID (last)
4077 < DF_INSN_LUID (last_combined_insn))
4078 last = last_combined_insn;
4081 /* We're dealing with a reg that changed mode but not
4082 meaning, so we want to turn it into a subreg for
4083 the new mode. However, because of REG sharing and
4084 because its mode had already changed, we have to do
4085 it in two steps. First, replace any debug uses of
4086 reg, with its original mode temporarily restored,
4087 with this copy we have created; then, replace the
4088 copy with the SUBREG of the original shared reg,
4089 once again changed to the new mode. */
4090 propagate_for_debug (first, last, reg, tempreg,
4091 this_basic_block);
4092 adjust_reg_mode (reg, new_mode);
4093 propagate_for_debug (first, last, tempreg,
4094 lowpart_subreg (old_mode, reg, new_mode),
4095 this_basic_block);
4100 /* If we will be able to accept this, we have made a
4101 change to the destination of I3. This requires us to
4102 do a few adjustments. */
4104 if (changed_i3_dest)
4106 PATTERN (i3) = newpat;
4107 adjust_for_new_dest (i3);
4110 /* We now know that we can do this combination. Merge the insns and
4111 update the status of registers and LOG_LINKS. */
4113 if (undobuf.other_insn)
4115 rtx note, next;
4117 PATTERN (undobuf.other_insn) = other_pat;
4119 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4120 ensure that they are still valid. Then add any non-duplicate
4121 notes added by recog_for_combine. */
4122 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4124 next = XEXP (note, 1);
4126 if ((REG_NOTE_KIND (note) == REG_DEAD
4127 && !reg_referenced_p (XEXP (note, 0),
4128 PATTERN (undobuf.other_insn)))
4129 ||(REG_NOTE_KIND (note) == REG_UNUSED
4130 && !reg_set_p (XEXP (note, 0),
4131 PATTERN (undobuf.other_insn))))
4132 remove_note (undobuf.other_insn, note);
4135 distribute_notes (new_other_notes, undobuf.other_insn,
4136 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4137 NULL_RTX);
4140 if (swap_i2i3)
4142 rtx_insn *insn;
4143 struct insn_link *link;
4144 rtx ni2dest;
4146 /* I3 now uses what used to be its destination and which is now
4147 I2's destination. This requires us to do a few adjustments. */
4148 PATTERN (i3) = newpat;
4149 adjust_for_new_dest (i3);
4151 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4152 so we still will.
4154 However, some later insn might be using I2's dest and have
4155 a LOG_LINK pointing at I3. We must remove this link.
4156 The simplest way to remove the link is to point it at I1,
4157 which we know will be a NOTE. */
4159 /* newi2pat is usually a SET here; however, recog_for_combine might
4160 have added some clobbers. */
4161 if (GET_CODE (newi2pat) == PARALLEL)
4162 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4163 else
4164 ni2dest = SET_DEST (newi2pat);
4166 for (insn = NEXT_INSN (i3);
4167 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4168 || insn != BB_HEAD (this_basic_block->next_bb));
4169 insn = NEXT_INSN (insn))
4171 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4173 FOR_EACH_LOG_LINK (link, insn)
4174 if (link->insn == i3)
4175 link->insn = i1;
4177 break;
4183 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4184 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4185 rtx midnotes = 0;
4186 int from_luid;
4187 /* Compute which registers we expect to eliminate. newi2pat may be setting
4188 either i3dest or i2dest, so we must check it. */
4189 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4190 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4191 || !i2dest_killed
4192 ? 0 : i2dest);
4193 /* For i1, we need to compute both local elimination and global
4194 elimination information with respect to newi2pat because i1dest
4195 may be the same as i3dest, in which case newi2pat may be setting
4196 i1dest. Global information is used when distributing REG_DEAD
4197 note for i2 and i3, in which case it does matter if newi2pat sets
4198 i1dest or not.
4200 Local information is used when distributing REG_DEAD note for i1,
4201 in which case it doesn't matter if newi2pat sets i1dest or not.
4202 See PR62151, if we have four insns combination:
4203 i0: r0 <- i0src
4204 i1: r1 <- i1src (using r0)
4205 REG_DEAD (r0)
4206 i2: r0 <- i2src (using r1)
4207 i3: r3 <- i3src (using r0)
4208 ix: using r0
4209 From i1's point of view, r0 is eliminated, no matter if it is set
4210 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4211 should be discarded.
4213 Note local information only affects cases in forms like "I1->I2->I3",
4214 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4215 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4216 i0dest anyway. */
4217 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4218 || !i1dest_killed
4219 ? 0 : i1dest);
4220 rtx elim_i1 = (local_elim_i1 == 0
4221 || (newi2pat && reg_set_p (i1dest, newi2pat))
4222 ? 0 : i1dest);
4223 /* Same case as i1. */
4224 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4225 ? 0 : i0dest);
4226 rtx elim_i0 = (local_elim_i0 == 0
4227 || (newi2pat && reg_set_p (i0dest, newi2pat))
4228 ? 0 : i0dest);
4230 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4231 clear them. */
4232 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4233 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4234 if (i1)
4235 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4236 if (i0)
4237 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4239 /* Ensure that we do not have something that should not be shared but
4240 occurs multiple times in the new insns. Check this by first
4241 resetting all the `used' flags and then copying anything is shared. */
4243 reset_used_flags (i3notes);
4244 reset_used_flags (i2notes);
4245 reset_used_flags (i1notes);
4246 reset_used_flags (i0notes);
4247 reset_used_flags (newpat);
4248 reset_used_flags (newi2pat);
4249 if (undobuf.other_insn)
4250 reset_used_flags (PATTERN (undobuf.other_insn));
4252 i3notes = copy_rtx_if_shared (i3notes);
4253 i2notes = copy_rtx_if_shared (i2notes);
4254 i1notes = copy_rtx_if_shared (i1notes);
4255 i0notes = copy_rtx_if_shared (i0notes);
4256 newpat = copy_rtx_if_shared (newpat);
4257 newi2pat = copy_rtx_if_shared (newi2pat);
4258 if (undobuf.other_insn)
4259 reset_used_flags (PATTERN (undobuf.other_insn));
4261 INSN_CODE (i3) = insn_code_number;
4262 PATTERN (i3) = newpat;
4264 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4266 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4268 reset_used_flags (call_usage);
4269 call_usage = copy_rtx (call_usage);
4271 if (substed_i2)
4273 /* I2SRC must still be meaningful at this point. Some splitting
4274 operations can invalidate I2SRC, but those operations do not
4275 apply to calls. */
4276 gcc_assert (i2src);
4277 replace_rtx (call_usage, i2dest, i2src);
4280 if (substed_i1)
4281 replace_rtx (call_usage, i1dest, i1src);
4282 if (substed_i0)
4283 replace_rtx (call_usage, i0dest, i0src);
4285 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4288 if (undobuf.other_insn)
4289 INSN_CODE (undobuf.other_insn) = other_code_number;
4291 /* We had one special case above where I2 had more than one set and
4292 we replaced a destination of one of those sets with the destination
4293 of I3. In that case, we have to update LOG_LINKS of insns later
4294 in this basic block. Note that this (expensive) case is rare.
4296 Also, in this case, we must pretend that all REG_NOTEs for I2
4297 actually came from I3, so that REG_UNUSED notes from I2 will be
4298 properly handled. */
4300 if (i3_subst_into_i2)
4302 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4303 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4304 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4305 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4306 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4307 && ! find_reg_note (i2, REG_UNUSED,
4308 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4309 for (temp_insn = NEXT_INSN (i2);
4310 temp_insn
4311 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4312 || BB_HEAD (this_basic_block) != temp_insn);
4313 temp_insn = NEXT_INSN (temp_insn))
4314 if (temp_insn != i3 && INSN_P (temp_insn))
4315 FOR_EACH_LOG_LINK (link, temp_insn)
4316 if (link->insn == i2)
4317 link->insn = i3;
4319 if (i3notes)
4321 rtx link = i3notes;
4322 while (XEXP (link, 1))
4323 link = XEXP (link, 1);
4324 XEXP (link, 1) = i2notes;
4326 else
4327 i3notes = i2notes;
4328 i2notes = 0;
4331 LOG_LINKS (i3) = NULL;
4332 REG_NOTES (i3) = 0;
4333 LOG_LINKS (i2) = NULL;
4334 REG_NOTES (i2) = 0;
4336 if (newi2pat)
4338 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4339 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4340 this_basic_block);
4341 INSN_CODE (i2) = i2_code_number;
4342 PATTERN (i2) = newi2pat;
4344 else
4346 if (MAY_HAVE_DEBUG_INSNS && i2src)
4347 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4348 this_basic_block);
4349 SET_INSN_DELETED (i2);
4352 if (i1)
4354 LOG_LINKS (i1) = NULL;
4355 REG_NOTES (i1) = 0;
4356 if (MAY_HAVE_DEBUG_INSNS)
4357 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4358 this_basic_block);
4359 SET_INSN_DELETED (i1);
4362 if (i0)
4364 LOG_LINKS (i0) = NULL;
4365 REG_NOTES (i0) = 0;
4366 if (MAY_HAVE_DEBUG_INSNS)
4367 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4368 this_basic_block);
4369 SET_INSN_DELETED (i0);
4372 /* Get death notes for everything that is now used in either I3 or
4373 I2 and used to die in a previous insn. If we built two new
4374 patterns, move from I1 to I2 then I2 to I3 so that we get the
4375 proper movement on registers that I2 modifies. */
4377 if (i0)
4378 from_luid = DF_INSN_LUID (i0);
4379 else if (i1)
4380 from_luid = DF_INSN_LUID (i1);
4381 else
4382 from_luid = DF_INSN_LUID (i2);
4383 if (newi2pat)
4384 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4385 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4387 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4388 if (i3notes)
4389 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4390 elim_i2, elim_i1, elim_i0);
4391 if (i2notes)
4392 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4393 elim_i2, elim_i1, elim_i0);
4394 if (i1notes)
4395 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4396 elim_i2, local_elim_i1, local_elim_i0);
4397 if (i0notes)
4398 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4399 elim_i2, elim_i1, local_elim_i0);
4400 if (midnotes)
4401 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4402 elim_i2, elim_i1, elim_i0);
4404 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4405 know these are REG_UNUSED and want them to go to the desired insn,
4406 so we always pass it as i3. */
4408 if (newi2pat && new_i2_notes)
4409 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4410 NULL_RTX);
4412 if (new_i3_notes)
4413 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4414 NULL_RTX);
4416 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4417 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4418 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4419 in that case, it might delete I2. Similarly for I2 and I1.
4420 Show an additional death due to the REG_DEAD note we make here. If
4421 we discard it in distribute_notes, we will decrement it again. */
4423 if (i3dest_killed)
4425 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4426 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4427 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4428 elim_i1, elim_i0);
4429 else
4430 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4431 elim_i2, elim_i1, elim_i0);
4434 if (i2dest_in_i2src)
4436 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4437 if (newi2pat && reg_set_p (i2dest, newi2pat))
4438 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4439 NULL_RTX, NULL_RTX);
4440 else
4441 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4442 NULL_RTX, NULL_RTX, NULL_RTX);
4445 if (i1dest_in_i1src)
4447 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4448 if (newi2pat && reg_set_p (i1dest, newi2pat))
4449 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4450 NULL_RTX, NULL_RTX);
4451 else
4452 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4453 NULL_RTX, NULL_RTX, NULL_RTX);
4456 if (i0dest_in_i0src)
4458 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4459 if (newi2pat && reg_set_p (i0dest, newi2pat))
4460 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4461 NULL_RTX, NULL_RTX);
4462 else
4463 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4464 NULL_RTX, NULL_RTX, NULL_RTX);
4467 distribute_links (i3links);
4468 distribute_links (i2links);
4469 distribute_links (i1links);
4470 distribute_links (i0links);
4472 if (REG_P (i2dest))
4474 struct insn_link *link;
4475 rtx_insn *i2_insn = 0;
4476 rtx i2_val = 0, set;
4478 /* The insn that used to set this register doesn't exist, and
4479 this life of the register may not exist either. See if one of
4480 I3's links points to an insn that sets I2DEST. If it does,
4481 that is now the last known value for I2DEST. If we don't update
4482 this and I2 set the register to a value that depended on its old
4483 contents, we will get confused. If this insn is used, thing
4484 will be set correctly in combine_instructions. */
4485 FOR_EACH_LOG_LINK (link, i3)
4486 if ((set = single_set (link->insn)) != 0
4487 && rtx_equal_p (i2dest, SET_DEST (set)))
4488 i2_insn = link->insn, i2_val = SET_SRC (set);
4490 record_value_for_reg (i2dest, i2_insn, i2_val);
4492 /* If the reg formerly set in I2 died only once and that was in I3,
4493 zero its use count so it won't make `reload' do any work. */
4494 if (! added_sets_2
4495 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4496 && ! i2dest_in_i2src
4497 && REGNO (i2dest) < reg_n_sets_max)
4498 INC_REG_N_SETS (REGNO (i2dest), -1);
4501 if (i1 && REG_P (i1dest))
4503 struct insn_link *link;
4504 rtx_insn *i1_insn = 0;
4505 rtx i1_val = 0, set;
4507 FOR_EACH_LOG_LINK (link, i3)
4508 if ((set = single_set (link->insn)) != 0
4509 && rtx_equal_p (i1dest, SET_DEST (set)))
4510 i1_insn = link->insn, i1_val = SET_SRC (set);
4512 record_value_for_reg (i1dest, i1_insn, i1_val);
4514 if (! added_sets_1
4515 && ! i1dest_in_i1src
4516 && REGNO (i1dest) < reg_n_sets_max)
4517 INC_REG_N_SETS (REGNO (i1dest), -1);
4520 if (i0 && REG_P (i0dest))
4522 struct insn_link *link;
4523 rtx_insn *i0_insn = 0;
4524 rtx i0_val = 0, set;
4526 FOR_EACH_LOG_LINK (link, i3)
4527 if ((set = single_set (link->insn)) != 0
4528 && rtx_equal_p (i0dest, SET_DEST (set)))
4529 i0_insn = link->insn, i0_val = SET_SRC (set);
4531 record_value_for_reg (i0dest, i0_insn, i0_val);
4533 if (! added_sets_0
4534 && ! i0dest_in_i0src
4535 && REGNO (i0dest) < reg_n_sets_max)
4536 INC_REG_N_SETS (REGNO (i0dest), -1);
4539 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4540 been made to this insn. The order is important, because newi2pat
4541 can affect nonzero_bits of newpat. */
4542 if (newi2pat)
4543 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4544 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4547 if (undobuf.other_insn != NULL_RTX)
4549 if (dump_file)
4551 fprintf (dump_file, "modifying other_insn ");
4552 dump_insn_slim (dump_file, undobuf.other_insn);
4554 df_insn_rescan (undobuf.other_insn);
4557 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4559 if (dump_file)
4561 fprintf (dump_file, "modifying insn i0 ");
4562 dump_insn_slim (dump_file, i0);
4564 df_insn_rescan (i0);
4567 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4569 if (dump_file)
4571 fprintf (dump_file, "modifying insn i1 ");
4572 dump_insn_slim (dump_file, i1);
4574 df_insn_rescan (i1);
4577 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4579 if (dump_file)
4581 fprintf (dump_file, "modifying insn i2 ");
4582 dump_insn_slim (dump_file, i2);
4584 df_insn_rescan (i2);
4587 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4589 if (dump_file)
4591 fprintf (dump_file, "modifying insn i3 ");
4592 dump_insn_slim (dump_file, i3);
4594 df_insn_rescan (i3);
4597 /* Set new_direct_jump_p if a new return or simple jump instruction
4598 has been created. Adjust the CFG accordingly. */
4599 if (returnjump_p (i3) || any_uncondjump_p (i3))
4601 *new_direct_jump_p = 1;
4602 mark_jump_label (PATTERN (i3), i3, 0);
4603 update_cfg_for_uncondjump (i3);
4606 if (undobuf.other_insn != NULL_RTX
4607 && (returnjump_p (undobuf.other_insn)
4608 || any_uncondjump_p (undobuf.other_insn)))
4610 *new_direct_jump_p = 1;
4611 update_cfg_for_uncondjump (undobuf.other_insn);
4614 /* A noop might also need cleaning up of CFG, if it comes from the
4615 simplification of a jump. */
4616 if (JUMP_P (i3)
4617 && GET_CODE (newpat) == SET
4618 && SET_SRC (newpat) == pc_rtx
4619 && SET_DEST (newpat) == pc_rtx)
4621 *new_direct_jump_p = 1;
4622 update_cfg_for_uncondjump (i3);
4625 if (undobuf.other_insn != NULL_RTX
4626 && JUMP_P (undobuf.other_insn)
4627 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4628 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4629 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4631 *new_direct_jump_p = 1;
4632 update_cfg_for_uncondjump (undobuf.other_insn);
4635 combine_successes++;
4636 undo_commit ();
4638 if (added_links_insn
4639 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4640 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4641 return added_links_insn;
4642 else
4643 return newi2pat ? i2 : i3;
4646 /* Undo all the modifications recorded in undobuf. */
4648 static void
4649 undo_all (void)
4651 struct undo *undo, *next;
4653 for (undo = undobuf.undos; undo; undo = next)
4655 next = undo->next;
4656 switch (undo->kind)
4658 case UNDO_RTX:
4659 *undo->where.r = undo->old_contents.r;
4660 break;
4661 case UNDO_INT:
4662 *undo->where.i = undo->old_contents.i;
4663 break;
4664 case UNDO_MODE:
4665 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4666 break;
4667 case UNDO_LINKS:
4668 *undo->where.l = undo->old_contents.l;
4669 break;
4670 default:
4671 gcc_unreachable ();
4674 undo->next = undobuf.frees;
4675 undobuf.frees = undo;
4678 undobuf.undos = 0;
4681 /* We've committed to accepting the changes we made. Move all
4682 of the undos to the free list. */
4684 static void
4685 undo_commit (void)
4687 struct undo *undo, *next;
4689 for (undo = undobuf.undos; undo; undo = next)
4691 next = undo->next;
4692 undo->next = undobuf.frees;
4693 undobuf.frees = undo;
4695 undobuf.undos = 0;
4698 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4699 where we have an arithmetic expression and return that point. LOC will
4700 be inside INSN.
4702 try_combine will call this function to see if an insn can be split into
4703 two insns. */
4705 static rtx *
4706 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4708 rtx x = *loc;
4709 enum rtx_code code = GET_CODE (x);
4710 rtx *split;
4711 unsigned HOST_WIDE_INT len = 0;
4712 HOST_WIDE_INT pos = 0;
4713 int unsignedp = 0;
4714 rtx inner = NULL_RTX;
4716 /* First special-case some codes. */
4717 switch (code)
4719 case SUBREG:
4720 #ifdef INSN_SCHEDULING
4721 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4722 point. */
4723 if (MEM_P (SUBREG_REG (x)))
4724 return loc;
4725 #endif
4726 return find_split_point (&SUBREG_REG (x), insn, false);
4728 case MEM:
4729 #ifdef HAVE_lo_sum
4730 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4731 using LO_SUM and HIGH. */
4732 if (GET_CODE (XEXP (x, 0)) == CONST
4733 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4735 machine_mode address_mode = get_address_mode (x);
4737 SUBST (XEXP (x, 0),
4738 gen_rtx_LO_SUM (address_mode,
4739 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4740 XEXP (x, 0)));
4741 return &XEXP (XEXP (x, 0), 0);
4743 #endif
4745 /* If we have a PLUS whose second operand is a constant and the
4746 address is not valid, perhaps will can split it up using
4747 the machine-specific way to split large constants. We use
4748 the first pseudo-reg (one of the virtual regs) as a placeholder;
4749 it will not remain in the result. */
4750 if (GET_CODE (XEXP (x, 0)) == PLUS
4751 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4752 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4753 MEM_ADDR_SPACE (x)))
4755 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4756 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4757 subst_insn);
4759 /* This should have produced two insns, each of which sets our
4760 placeholder. If the source of the second is a valid address,
4761 we can make put both sources together and make a split point
4762 in the middle. */
4764 if (seq
4765 && NEXT_INSN (seq) != NULL_RTX
4766 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4767 && NONJUMP_INSN_P (seq)
4768 && GET_CODE (PATTERN (seq)) == SET
4769 && SET_DEST (PATTERN (seq)) == reg
4770 && ! reg_mentioned_p (reg,
4771 SET_SRC (PATTERN (seq)))
4772 && NONJUMP_INSN_P (NEXT_INSN (seq))
4773 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4774 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4775 && memory_address_addr_space_p
4776 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4777 MEM_ADDR_SPACE (x)))
4779 rtx src1 = SET_SRC (PATTERN (seq));
4780 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4782 /* Replace the placeholder in SRC2 with SRC1. If we can
4783 find where in SRC2 it was placed, that can become our
4784 split point and we can replace this address with SRC2.
4785 Just try two obvious places. */
4787 src2 = replace_rtx (src2, reg, src1);
4788 split = 0;
4789 if (XEXP (src2, 0) == src1)
4790 split = &XEXP (src2, 0);
4791 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4792 && XEXP (XEXP (src2, 0), 0) == src1)
4793 split = &XEXP (XEXP (src2, 0), 0);
4795 if (split)
4797 SUBST (XEXP (x, 0), src2);
4798 return split;
4802 /* If that didn't work, perhaps the first operand is complex and
4803 needs to be computed separately, so make a split point there.
4804 This will occur on machines that just support REG + CONST
4805 and have a constant moved through some previous computation. */
4807 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4808 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4809 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4810 return &XEXP (XEXP (x, 0), 0);
4813 /* If we have a PLUS whose first operand is complex, try computing it
4814 separately by making a split there. */
4815 if (GET_CODE (XEXP (x, 0)) == PLUS
4816 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4817 MEM_ADDR_SPACE (x))
4818 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4819 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4820 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4821 return &XEXP (XEXP (x, 0), 0);
4822 break;
4824 case SET:
4825 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4826 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4827 we need to put the operand into a register. So split at that
4828 point. */
4830 if (SET_DEST (x) == cc0_rtx
4831 && GET_CODE (SET_SRC (x)) != COMPARE
4832 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4833 && !OBJECT_P (SET_SRC (x))
4834 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4835 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4836 return &SET_SRC (x);
4838 /* See if we can split SET_SRC as it stands. */
4839 split = find_split_point (&SET_SRC (x), insn, true);
4840 if (split && split != &SET_SRC (x))
4841 return split;
4843 /* See if we can split SET_DEST as it stands. */
4844 split = find_split_point (&SET_DEST (x), insn, false);
4845 if (split && split != &SET_DEST (x))
4846 return split;
4848 /* See if this is a bitfield assignment with everything constant. If
4849 so, this is an IOR of an AND, so split it into that. */
4850 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4851 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4852 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4853 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4854 && CONST_INT_P (SET_SRC (x))
4855 && ((INTVAL (XEXP (SET_DEST (x), 1))
4856 + INTVAL (XEXP (SET_DEST (x), 2)))
4857 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4858 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4860 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4861 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4862 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4863 rtx dest = XEXP (SET_DEST (x), 0);
4864 machine_mode mode = GET_MODE (dest);
4865 unsigned HOST_WIDE_INT mask
4866 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4867 rtx or_mask;
4869 if (BITS_BIG_ENDIAN)
4870 pos = GET_MODE_PRECISION (mode) - len - pos;
4872 or_mask = gen_int_mode (src << pos, mode);
4873 if (src == mask)
4874 SUBST (SET_SRC (x),
4875 simplify_gen_binary (IOR, mode, dest, or_mask));
4876 else
4878 rtx negmask = gen_int_mode (~(mask << pos), mode);
4879 SUBST (SET_SRC (x),
4880 simplify_gen_binary (IOR, mode,
4881 simplify_gen_binary (AND, mode,
4882 dest, negmask),
4883 or_mask));
4886 SUBST (SET_DEST (x), dest);
4888 split = find_split_point (&SET_SRC (x), insn, true);
4889 if (split && split != &SET_SRC (x))
4890 return split;
4893 /* Otherwise, see if this is an operation that we can split into two.
4894 If so, try to split that. */
4895 code = GET_CODE (SET_SRC (x));
4897 switch (code)
4899 case AND:
4900 /* If we are AND'ing with a large constant that is only a single
4901 bit and the result is only being used in a context where we
4902 need to know if it is zero or nonzero, replace it with a bit
4903 extraction. This will avoid the large constant, which might
4904 have taken more than one insn to make. If the constant were
4905 not a valid argument to the AND but took only one insn to make,
4906 this is no worse, but if it took more than one insn, it will
4907 be better. */
4909 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4910 && REG_P (XEXP (SET_SRC (x), 0))
4911 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4912 && REG_P (SET_DEST (x))
4913 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4914 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4915 && XEXP (*split, 0) == SET_DEST (x)
4916 && XEXP (*split, 1) == const0_rtx)
4918 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4919 XEXP (SET_SRC (x), 0),
4920 pos, NULL_RTX, 1, 1, 0, 0);
4921 if (extraction != 0)
4923 SUBST (SET_SRC (x), extraction);
4924 return find_split_point (loc, insn, false);
4927 break;
4929 case NE:
4930 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4931 is known to be on, this can be converted into a NEG of a shift. */
4932 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4933 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4934 && 1 <= (pos = exact_log2
4935 (nonzero_bits (XEXP (SET_SRC (x), 0),
4936 GET_MODE (XEXP (SET_SRC (x), 0))))))
4938 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4940 SUBST (SET_SRC (x),
4941 gen_rtx_NEG (mode,
4942 gen_rtx_LSHIFTRT (mode,
4943 XEXP (SET_SRC (x), 0),
4944 GEN_INT (pos))));
4946 split = find_split_point (&SET_SRC (x), insn, true);
4947 if (split && split != &SET_SRC (x))
4948 return split;
4950 break;
4952 case SIGN_EXTEND:
4953 inner = XEXP (SET_SRC (x), 0);
4955 /* We can't optimize if either mode is a partial integer
4956 mode as we don't know how many bits are significant
4957 in those modes. */
4958 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4959 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4960 break;
4962 pos = 0;
4963 len = GET_MODE_PRECISION (GET_MODE (inner));
4964 unsignedp = 0;
4965 break;
4967 case SIGN_EXTRACT:
4968 case ZERO_EXTRACT:
4969 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4970 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4972 inner = XEXP (SET_SRC (x), 0);
4973 len = INTVAL (XEXP (SET_SRC (x), 1));
4974 pos = INTVAL (XEXP (SET_SRC (x), 2));
4976 if (BITS_BIG_ENDIAN)
4977 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4978 unsignedp = (code == ZERO_EXTRACT);
4980 break;
4982 default:
4983 break;
4986 if (len && pos >= 0
4987 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4989 machine_mode mode = GET_MODE (SET_SRC (x));
4991 /* For unsigned, we have a choice of a shift followed by an
4992 AND or two shifts. Use two shifts for field sizes where the
4993 constant might be too large. We assume here that we can
4994 always at least get 8-bit constants in an AND insn, which is
4995 true for every current RISC. */
4997 if (unsignedp && len <= 8)
4999 unsigned HOST_WIDE_INT mask
5000 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
5001 SUBST (SET_SRC (x),
5002 gen_rtx_AND (mode,
5003 gen_rtx_LSHIFTRT
5004 (mode, gen_lowpart (mode, inner),
5005 GEN_INT (pos)),
5006 gen_int_mode (mask, mode)));
5008 split = find_split_point (&SET_SRC (x), insn, true);
5009 if (split && split != &SET_SRC (x))
5010 return split;
5012 else
5014 SUBST (SET_SRC (x),
5015 gen_rtx_fmt_ee
5016 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5017 gen_rtx_ASHIFT (mode,
5018 gen_lowpart (mode, inner),
5019 GEN_INT (GET_MODE_PRECISION (mode)
5020 - len - pos)),
5021 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5023 split = find_split_point (&SET_SRC (x), insn, true);
5024 if (split && split != &SET_SRC (x))
5025 return split;
5029 /* See if this is a simple operation with a constant as the second
5030 operand. It might be that this constant is out of range and hence
5031 could be used as a split point. */
5032 if (BINARY_P (SET_SRC (x))
5033 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5034 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5035 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5036 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5037 return &XEXP (SET_SRC (x), 1);
5039 /* Finally, see if this is a simple operation with its first operand
5040 not in a register. The operation might require this operand in a
5041 register, so return it as a split point. We can always do this
5042 because if the first operand were another operation, we would have
5043 already found it as a split point. */
5044 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5045 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5046 return &XEXP (SET_SRC (x), 0);
5048 return 0;
5050 case AND:
5051 case IOR:
5052 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5053 it is better to write this as (not (ior A B)) so we can split it.
5054 Similarly for IOR. */
5055 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5057 SUBST (*loc,
5058 gen_rtx_NOT (GET_MODE (x),
5059 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5060 GET_MODE (x),
5061 XEXP (XEXP (x, 0), 0),
5062 XEXP (XEXP (x, 1), 0))));
5063 return find_split_point (loc, insn, set_src);
5066 /* Many RISC machines have a large set of logical insns. If the
5067 second operand is a NOT, put it first so we will try to split the
5068 other operand first. */
5069 if (GET_CODE (XEXP (x, 1)) == NOT)
5071 rtx tem = XEXP (x, 0);
5072 SUBST (XEXP (x, 0), XEXP (x, 1));
5073 SUBST (XEXP (x, 1), tem);
5075 break;
5077 case PLUS:
5078 case MINUS:
5079 /* Canonicalization can produce (minus A (mult B C)), where C is a
5080 constant. It may be better to try splitting (plus (mult B -C) A)
5081 instead if this isn't a multiply by a power of two. */
5082 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5083 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5084 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5086 machine_mode mode = GET_MODE (x);
5087 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5088 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5089 SUBST (*loc, gen_rtx_PLUS (mode,
5090 gen_rtx_MULT (mode,
5091 XEXP (XEXP (x, 1), 0),
5092 gen_int_mode (other_int,
5093 mode)),
5094 XEXP (x, 0)));
5095 return find_split_point (loc, insn, set_src);
5098 /* Split at a multiply-accumulate instruction. However if this is
5099 the SET_SRC, we likely do not have such an instruction and it's
5100 worthless to try this split. */
5101 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5102 return loc;
5104 default:
5105 break;
5108 /* Otherwise, select our actions depending on our rtx class. */
5109 switch (GET_RTX_CLASS (code))
5111 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5112 case RTX_TERNARY:
5113 split = find_split_point (&XEXP (x, 2), insn, false);
5114 if (split)
5115 return split;
5116 /* ... fall through ... */
5117 case RTX_BIN_ARITH:
5118 case RTX_COMM_ARITH:
5119 case RTX_COMPARE:
5120 case RTX_COMM_COMPARE:
5121 split = find_split_point (&XEXP (x, 1), insn, false);
5122 if (split)
5123 return split;
5124 /* ... fall through ... */
5125 case RTX_UNARY:
5126 /* Some machines have (and (shift ...) ...) insns. If X is not
5127 an AND, but XEXP (X, 0) is, use it as our split point. */
5128 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5129 return &XEXP (x, 0);
5131 split = find_split_point (&XEXP (x, 0), insn, false);
5132 if (split)
5133 return split;
5134 return loc;
5136 default:
5137 /* Otherwise, we don't have a split point. */
5138 return 0;
5142 /* Throughout X, replace FROM with TO, and return the result.
5143 The result is TO if X is FROM;
5144 otherwise the result is X, but its contents may have been modified.
5145 If they were modified, a record was made in undobuf so that
5146 undo_all will (among other things) return X to its original state.
5148 If the number of changes necessary is too much to record to undo,
5149 the excess changes are not made, so the result is invalid.
5150 The changes already made can still be undone.
5151 undobuf.num_undo is incremented for such changes, so by testing that
5152 the caller can tell whether the result is valid.
5154 `n_occurrences' is incremented each time FROM is replaced.
5156 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5158 IN_COND is nonzero if we are at the top level of a condition.
5160 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5161 by copying if `n_occurrences' is nonzero. */
5163 static rtx
5164 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5166 enum rtx_code code = GET_CODE (x);
5167 machine_mode op0_mode = VOIDmode;
5168 const char *fmt;
5169 int len, i;
5170 rtx new_rtx;
5172 /* Two expressions are equal if they are identical copies of a shared
5173 RTX or if they are both registers with the same register number
5174 and mode. */
5176 #define COMBINE_RTX_EQUAL_P(X,Y) \
5177 ((X) == (Y) \
5178 || (REG_P (X) && REG_P (Y) \
5179 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5181 /* Do not substitute into clobbers of regs -- this will never result in
5182 valid RTL. */
5183 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5184 return x;
5186 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5188 n_occurrences++;
5189 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5192 /* If X and FROM are the same register but different modes, they
5193 will not have been seen as equal above. However, the log links code
5194 will make a LOG_LINKS entry for that case. If we do nothing, we
5195 will try to rerecognize our original insn and, when it succeeds,
5196 we will delete the feeding insn, which is incorrect.
5198 So force this insn not to match in this (rare) case. */
5199 if (! in_dest && code == REG && REG_P (from)
5200 && reg_overlap_mentioned_p (x, from))
5201 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5203 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5204 of which may contain things that can be combined. */
5205 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5206 return x;
5208 /* It is possible to have a subexpression appear twice in the insn.
5209 Suppose that FROM is a register that appears within TO.
5210 Then, after that subexpression has been scanned once by `subst',
5211 the second time it is scanned, TO may be found. If we were
5212 to scan TO here, we would find FROM within it and create a
5213 self-referent rtl structure which is completely wrong. */
5214 if (COMBINE_RTX_EQUAL_P (x, to))
5215 return to;
5217 /* Parallel asm_operands need special attention because all of the
5218 inputs are shared across the arms. Furthermore, unsharing the
5219 rtl results in recognition failures. Failure to handle this case
5220 specially can result in circular rtl.
5222 Solve this by doing a normal pass across the first entry of the
5223 parallel, and only processing the SET_DESTs of the subsequent
5224 entries. Ug. */
5226 if (code == PARALLEL
5227 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5228 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5230 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5232 /* If this substitution failed, this whole thing fails. */
5233 if (GET_CODE (new_rtx) == CLOBBER
5234 && XEXP (new_rtx, 0) == const0_rtx)
5235 return new_rtx;
5237 SUBST (XVECEXP (x, 0, 0), new_rtx);
5239 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5241 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5243 if (!REG_P (dest)
5244 && GET_CODE (dest) != CC0
5245 && GET_CODE (dest) != PC)
5247 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5249 /* If this substitution failed, this whole thing fails. */
5250 if (GET_CODE (new_rtx) == CLOBBER
5251 && XEXP (new_rtx, 0) == const0_rtx)
5252 return new_rtx;
5254 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5258 else
5260 len = GET_RTX_LENGTH (code);
5261 fmt = GET_RTX_FORMAT (code);
5263 /* We don't need to process a SET_DEST that is a register, CC0,
5264 or PC, so set up to skip this common case. All other cases
5265 where we want to suppress replacing something inside a
5266 SET_SRC are handled via the IN_DEST operand. */
5267 if (code == SET
5268 && (REG_P (SET_DEST (x))
5269 || GET_CODE (SET_DEST (x)) == CC0
5270 || GET_CODE (SET_DEST (x)) == PC))
5271 fmt = "ie";
5273 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5274 constant. */
5275 if (fmt[0] == 'e')
5276 op0_mode = GET_MODE (XEXP (x, 0));
5278 for (i = 0; i < len; i++)
5280 if (fmt[i] == 'E')
5282 int j;
5283 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5285 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5287 new_rtx = (unique_copy && n_occurrences
5288 ? copy_rtx (to) : to);
5289 n_occurrences++;
5291 else
5293 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5294 unique_copy);
5296 /* If this substitution failed, this whole thing
5297 fails. */
5298 if (GET_CODE (new_rtx) == CLOBBER
5299 && XEXP (new_rtx, 0) == const0_rtx)
5300 return new_rtx;
5303 SUBST (XVECEXP (x, i, j), new_rtx);
5306 else if (fmt[i] == 'e')
5308 /* If this is a register being set, ignore it. */
5309 new_rtx = XEXP (x, i);
5310 if (in_dest
5311 && i == 0
5312 && (((code == SUBREG || code == ZERO_EXTRACT)
5313 && REG_P (new_rtx))
5314 || code == STRICT_LOW_PART))
5317 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5319 /* In general, don't install a subreg involving two
5320 modes not tieable. It can worsen register
5321 allocation, and can even make invalid reload
5322 insns, since the reg inside may need to be copied
5323 from in the outside mode, and that may be invalid
5324 if it is an fp reg copied in integer mode.
5326 We allow two exceptions to this: It is valid if
5327 it is inside another SUBREG and the mode of that
5328 SUBREG and the mode of the inside of TO is
5329 tieable and it is valid if X is a SET that copies
5330 FROM to CC0. */
5332 if (GET_CODE (to) == SUBREG
5333 && ! MODES_TIEABLE_P (GET_MODE (to),
5334 GET_MODE (SUBREG_REG (to)))
5335 && ! (code == SUBREG
5336 && MODES_TIEABLE_P (GET_MODE (x),
5337 GET_MODE (SUBREG_REG (to))))
5338 #if HAVE_cc0
5339 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5340 #endif
5342 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5344 if (code == SUBREG
5345 && REG_P (to)
5346 && REGNO (to) < FIRST_PSEUDO_REGISTER
5347 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5348 SUBREG_BYTE (x),
5349 GET_MODE (x)) < 0)
5350 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5352 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5353 n_occurrences++;
5355 else
5356 /* If we are in a SET_DEST, suppress most cases unless we
5357 have gone inside a MEM, in which case we want to
5358 simplify the address. We assume here that things that
5359 are actually part of the destination have their inner
5360 parts in the first expression. This is true for SUBREG,
5361 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5362 things aside from REG and MEM that should appear in a
5363 SET_DEST. */
5364 new_rtx = subst (XEXP (x, i), from, to,
5365 (((in_dest
5366 && (code == SUBREG || code == STRICT_LOW_PART
5367 || code == ZERO_EXTRACT))
5368 || code == SET)
5369 && i == 0),
5370 code == IF_THEN_ELSE && i == 0,
5371 unique_copy);
5373 /* If we found that we will have to reject this combination,
5374 indicate that by returning the CLOBBER ourselves, rather than
5375 an expression containing it. This will speed things up as
5376 well as prevent accidents where two CLOBBERs are considered
5377 to be equal, thus producing an incorrect simplification. */
5379 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5380 return new_rtx;
5382 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5384 machine_mode mode = GET_MODE (x);
5386 x = simplify_subreg (GET_MODE (x), new_rtx,
5387 GET_MODE (SUBREG_REG (x)),
5388 SUBREG_BYTE (x));
5389 if (! x)
5390 x = gen_rtx_CLOBBER (mode, const0_rtx);
5392 else if (CONST_SCALAR_INT_P (new_rtx)
5393 && GET_CODE (x) == ZERO_EXTEND)
5395 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5396 new_rtx, GET_MODE (XEXP (x, 0)));
5397 gcc_assert (x);
5399 else
5400 SUBST (XEXP (x, i), new_rtx);
5405 /* Check if we are loading something from the constant pool via float
5406 extension; in this case we would undo compress_float_constant
5407 optimization and degenerate constant load to an immediate value. */
5408 if (GET_CODE (x) == FLOAT_EXTEND
5409 && MEM_P (XEXP (x, 0))
5410 && MEM_READONLY_P (XEXP (x, 0)))
5412 rtx tmp = avoid_constant_pool_reference (x);
5413 if (x != tmp)
5414 return x;
5417 /* Try to simplify X. If the simplification changed the code, it is likely
5418 that further simplification will help, so loop, but limit the number
5419 of repetitions that will be performed. */
5421 for (i = 0; i < 4; i++)
5423 /* If X is sufficiently simple, don't bother trying to do anything
5424 with it. */
5425 if (code != CONST_INT && code != REG && code != CLOBBER)
5426 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5428 if (GET_CODE (x) == code)
5429 break;
5431 code = GET_CODE (x);
5433 /* We no longer know the original mode of operand 0 since we
5434 have changed the form of X) */
5435 op0_mode = VOIDmode;
5438 return x;
5441 /* Simplify X, a piece of RTL. We just operate on the expression at the
5442 outer level; call `subst' to simplify recursively. Return the new
5443 expression.
5445 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5446 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5447 of a condition. */
5449 static rtx
5450 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5451 int in_cond)
5453 enum rtx_code code = GET_CODE (x);
5454 machine_mode mode = GET_MODE (x);
5455 rtx temp;
5456 int i;
5458 /* If this is a commutative operation, put a constant last and a complex
5459 expression first. We don't need to do this for comparisons here. */
5460 if (COMMUTATIVE_ARITH_P (x)
5461 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5463 temp = XEXP (x, 0);
5464 SUBST (XEXP (x, 0), XEXP (x, 1));
5465 SUBST (XEXP (x, 1), temp);
5468 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5469 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5470 things. Check for cases where both arms are testing the same
5471 condition.
5473 Don't do anything if all operands are very simple. */
5475 if ((BINARY_P (x)
5476 && ((!OBJECT_P (XEXP (x, 0))
5477 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5478 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5479 || (!OBJECT_P (XEXP (x, 1))
5480 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5481 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5482 || (UNARY_P (x)
5483 && (!OBJECT_P (XEXP (x, 0))
5484 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5485 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5487 rtx cond, true_rtx, false_rtx;
5489 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5490 if (cond != 0
5491 /* If everything is a comparison, what we have is highly unlikely
5492 to be simpler, so don't use it. */
5493 && ! (COMPARISON_P (x)
5494 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5496 rtx cop1 = const0_rtx;
5497 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5499 if (cond_code == NE && COMPARISON_P (cond))
5500 return x;
5502 /* Simplify the alternative arms; this may collapse the true and
5503 false arms to store-flag values. Be careful to use copy_rtx
5504 here since true_rtx or false_rtx might share RTL with x as a
5505 result of the if_then_else_cond call above. */
5506 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5507 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5509 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5510 is unlikely to be simpler. */
5511 if (general_operand (true_rtx, VOIDmode)
5512 && general_operand (false_rtx, VOIDmode))
5514 enum rtx_code reversed;
5516 /* Restarting if we generate a store-flag expression will cause
5517 us to loop. Just drop through in this case. */
5519 /* If the result values are STORE_FLAG_VALUE and zero, we can
5520 just make the comparison operation. */
5521 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5522 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5523 cond, cop1);
5524 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5525 && ((reversed = reversed_comparison_code_parts
5526 (cond_code, cond, cop1, NULL))
5527 != UNKNOWN))
5528 x = simplify_gen_relational (reversed, mode, VOIDmode,
5529 cond, cop1);
5531 /* Likewise, we can make the negate of a comparison operation
5532 if the result values are - STORE_FLAG_VALUE and zero. */
5533 else if (CONST_INT_P (true_rtx)
5534 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5535 && false_rtx == const0_rtx)
5536 x = simplify_gen_unary (NEG, mode,
5537 simplify_gen_relational (cond_code,
5538 mode, VOIDmode,
5539 cond, cop1),
5540 mode);
5541 else if (CONST_INT_P (false_rtx)
5542 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5543 && true_rtx == const0_rtx
5544 && ((reversed = reversed_comparison_code_parts
5545 (cond_code, cond, cop1, NULL))
5546 != UNKNOWN))
5547 x = simplify_gen_unary (NEG, mode,
5548 simplify_gen_relational (reversed,
5549 mode, VOIDmode,
5550 cond, cop1),
5551 mode);
5552 else
5553 return gen_rtx_IF_THEN_ELSE (mode,
5554 simplify_gen_relational (cond_code,
5555 mode,
5556 VOIDmode,
5557 cond,
5558 cop1),
5559 true_rtx, false_rtx);
5561 code = GET_CODE (x);
5562 op0_mode = VOIDmode;
5567 /* Try to fold this expression in case we have constants that weren't
5568 present before. */
5569 temp = 0;
5570 switch (GET_RTX_CLASS (code))
5572 case RTX_UNARY:
5573 if (op0_mode == VOIDmode)
5574 op0_mode = GET_MODE (XEXP (x, 0));
5575 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5576 break;
5577 case RTX_COMPARE:
5578 case RTX_COMM_COMPARE:
5580 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5581 if (cmp_mode == VOIDmode)
5583 cmp_mode = GET_MODE (XEXP (x, 1));
5584 if (cmp_mode == VOIDmode)
5585 cmp_mode = op0_mode;
5587 temp = simplify_relational_operation (code, mode, cmp_mode,
5588 XEXP (x, 0), XEXP (x, 1));
5590 break;
5591 case RTX_COMM_ARITH:
5592 case RTX_BIN_ARITH:
5593 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5594 break;
5595 case RTX_BITFIELD_OPS:
5596 case RTX_TERNARY:
5597 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5598 XEXP (x, 1), XEXP (x, 2));
5599 break;
5600 default:
5601 break;
5604 if (temp)
5606 x = temp;
5607 code = GET_CODE (temp);
5608 op0_mode = VOIDmode;
5609 mode = GET_MODE (temp);
5612 /* First see if we can apply the inverse distributive law. */
5613 if (code == PLUS || code == MINUS
5614 || code == AND || code == IOR || code == XOR)
5616 x = apply_distributive_law (x);
5617 code = GET_CODE (x);
5618 op0_mode = VOIDmode;
5621 /* If CODE is an associative operation not otherwise handled, see if we
5622 can associate some operands. This can win if they are constants or
5623 if they are logically related (i.e. (a & b) & a). */
5624 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5625 || code == AND || code == IOR || code == XOR
5626 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5627 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5628 || (flag_associative_math && FLOAT_MODE_P (mode))))
5630 if (GET_CODE (XEXP (x, 0)) == code)
5632 rtx other = XEXP (XEXP (x, 0), 0);
5633 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5634 rtx inner_op1 = XEXP (x, 1);
5635 rtx inner;
5637 /* Make sure we pass the constant operand if any as the second
5638 one if this is a commutative operation. */
5639 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5641 rtx tem = inner_op0;
5642 inner_op0 = inner_op1;
5643 inner_op1 = tem;
5645 inner = simplify_binary_operation (code == MINUS ? PLUS
5646 : code == DIV ? MULT
5647 : code,
5648 mode, inner_op0, inner_op1);
5650 /* For commutative operations, try the other pair if that one
5651 didn't simplify. */
5652 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5654 other = XEXP (XEXP (x, 0), 1);
5655 inner = simplify_binary_operation (code, mode,
5656 XEXP (XEXP (x, 0), 0),
5657 XEXP (x, 1));
5660 if (inner)
5661 return simplify_gen_binary (code, mode, other, inner);
5665 /* A little bit of algebraic simplification here. */
5666 switch (code)
5668 case MEM:
5669 /* Ensure that our address has any ASHIFTs converted to MULT in case
5670 address-recognizing predicates are called later. */
5671 temp = make_compound_operation (XEXP (x, 0), MEM);
5672 SUBST (XEXP (x, 0), temp);
5673 break;
5675 case SUBREG:
5676 if (op0_mode == VOIDmode)
5677 op0_mode = GET_MODE (SUBREG_REG (x));
5679 /* See if this can be moved to simplify_subreg. */
5680 if (CONSTANT_P (SUBREG_REG (x))
5681 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5682 /* Don't call gen_lowpart if the inner mode
5683 is VOIDmode and we cannot simplify it, as SUBREG without
5684 inner mode is invalid. */
5685 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5686 || gen_lowpart_common (mode, SUBREG_REG (x))))
5687 return gen_lowpart (mode, SUBREG_REG (x));
5689 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5690 break;
5692 rtx temp;
5693 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5694 SUBREG_BYTE (x));
5695 if (temp)
5696 return temp;
5698 /* If op is known to have all lower bits zero, the result is zero. */
5699 if (!in_dest
5700 && SCALAR_INT_MODE_P (mode)
5701 && SCALAR_INT_MODE_P (op0_mode)
5702 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5703 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5704 && HWI_COMPUTABLE_MODE_P (op0_mode)
5705 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5706 & GET_MODE_MASK (mode)) == 0)
5707 return CONST0_RTX (mode);
5710 /* Don't change the mode of the MEM if that would change the meaning
5711 of the address. */
5712 if (MEM_P (SUBREG_REG (x))
5713 && (MEM_VOLATILE_P (SUBREG_REG (x))
5714 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5715 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5716 return gen_rtx_CLOBBER (mode, const0_rtx);
5718 /* Note that we cannot do any narrowing for non-constants since
5719 we might have been counting on using the fact that some bits were
5720 zero. We now do this in the SET. */
5722 break;
5724 case NEG:
5725 temp = expand_compound_operation (XEXP (x, 0));
5727 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5728 replaced by (lshiftrt X C). This will convert
5729 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5731 if (GET_CODE (temp) == ASHIFTRT
5732 && CONST_INT_P (XEXP (temp, 1))
5733 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5734 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5735 INTVAL (XEXP (temp, 1)));
5737 /* If X has only a single bit that might be nonzero, say, bit I, convert
5738 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5739 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5740 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5741 or a SUBREG of one since we'd be making the expression more
5742 complex if it was just a register. */
5744 if (!REG_P (temp)
5745 && ! (GET_CODE (temp) == SUBREG
5746 && REG_P (SUBREG_REG (temp)))
5747 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5749 rtx temp1 = simplify_shift_const
5750 (NULL_RTX, ASHIFTRT, mode,
5751 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5752 GET_MODE_PRECISION (mode) - 1 - i),
5753 GET_MODE_PRECISION (mode) - 1 - i);
5755 /* If all we did was surround TEMP with the two shifts, we
5756 haven't improved anything, so don't use it. Otherwise,
5757 we are better off with TEMP1. */
5758 if (GET_CODE (temp1) != ASHIFTRT
5759 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5760 || XEXP (XEXP (temp1, 0), 0) != temp)
5761 return temp1;
5763 break;
5765 case TRUNCATE:
5766 /* We can't handle truncation to a partial integer mode here
5767 because we don't know the real bitsize of the partial
5768 integer mode. */
5769 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5770 break;
5772 if (HWI_COMPUTABLE_MODE_P (mode))
5773 SUBST (XEXP (x, 0),
5774 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5775 GET_MODE_MASK (mode), 0));
5777 /* We can truncate a constant value and return it. */
5778 if (CONST_INT_P (XEXP (x, 0)))
5779 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5781 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5782 whose value is a comparison can be replaced with a subreg if
5783 STORE_FLAG_VALUE permits. */
5784 if (HWI_COMPUTABLE_MODE_P (mode)
5785 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5786 && (temp = get_last_value (XEXP (x, 0)))
5787 && COMPARISON_P (temp))
5788 return gen_lowpart (mode, XEXP (x, 0));
5789 break;
5791 case CONST:
5792 /* (const (const X)) can become (const X). Do it this way rather than
5793 returning the inner CONST since CONST can be shared with a
5794 REG_EQUAL note. */
5795 if (GET_CODE (XEXP (x, 0)) == CONST)
5796 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5797 break;
5799 #ifdef HAVE_lo_sum
5800 case LO_SUM:
5801 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5802 can add in an offset. find_split_point will split this address up
5803 again if it doesn't match. */
5804 if (GET_CODE (XEXP (x, 0)) == HIGH
5805 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5806 return XEXP (x, 1);
5807 break;
5808 #endif
5810 case PLUS:
5811 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5812 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5813 bit-field and can be replaced by either a sign_extend or a
5814 sign_extract. The `and' may be a zero_extend and the two
5815 <c>, -<c> constants may be reversed. */
5816 if (GET_CODE (XEXP (x, 0)) == XOR
5817 && CONST_INT_P (XEXP (x, 1))
5818 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5819 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5820 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5821 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5822 && HWI_COMPUTABLE_MODE_P (mode)
5823 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5824 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5825 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5826 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5827 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5828 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5829 == (unsigned int) i + 1))))
5830 return simplify_shift_const
5831 (NULL_RTX, ASHIFTRT, mode,
5832 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5833 XEXP (XEXP (XEXP (x, 0), 0), 0),
5834 GET_MODE_PRECISION (mode) - (i + 1)),
5835 GET_MODE_PRECISION (mode) - (i + 1));
5837 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5838 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5839 the bitsize of the mode - 1. This allows simplification of
5840 "a = (b & 8) == 0;" */
5841 if (XEXP (x, 1) == constm1_rtx
5842 && !REG_P (XEXP (x, 0))
5843 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5844 && REG_P (SUBREG_REG (XEXP (x, 0))))
5845 && nonzero_bits (XEXP (x, 0), mode) == 1)
5846 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5847 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5848 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5849 GET_MODE_PRECISION (mode) - 1),
5850 GET_MODE_PRECISION (mode) - 1);
5852 /* If we are adding two things that have no bits in common, convert
5853 the addition into an IOR. This will often be further simplified,
5854 for example in cases like ((a & 1) + (a & 2)), which can
5855 become a & 3. */
5857 if (HWI_COMPUTABLE_MODE_P (mode)
5858 && (nonzero_bits (XEXP (x, 0), mode)
5859 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5861 /* Try to simplify the expression further. */
5862 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5863 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5865 /* If we could, great. If not, do not go ahead with the IOR
5866 replacement, since PLUS appears in many special purpose
5867 address arithmetic instructions. */
5868 if (GET_CODE (temp) != CLOBBER
5869 && (GET_CODE (temp) != IOR
5870 || ((XEXP (temp, 0) != XEXP (x, 0)
5871 || XEXP (temp, 1) != XEXP (x, 1))
5872 && (XEXP (temp, 0) != XEXP (x, 1)
5873 || XEXP (temp, 1) != XEXP (x, 0)))))
5874 return temp;
5876 break;
5878 case MINUS:
5879 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5880 (and <foo> (const_int pow2-1)) */
5881 if (GET_CODE (XEXP (x, 1)) == AND
5882 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5883 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5884 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5885 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5886 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5887 break;
5889 case MULT:
5890 /* If we have (mult (plus A B) C), apply the distributive law and then
5891 the inverse distributive law to see if things simplify. This
5892 occurs mostly in addresses, often when unrolling loops. */
5894 if (GET_CODE (XEXP (x, 0)) == PLUS)
5896 rtx result = distribute_and_simplify_rtx (x, 0);
5897 if (result)
5898 return result;
5901 /* Try simplify a*(b/c) as (a*b)/c. */
5902 if (FLOAT_MODE_P (mode) && flag_associative_math
5903 && GET_CODE (XEXP (x, 0)) == DIV)
5905 rtx tem = simplify_binary_operation (MULT, mode,
5906 XEXP (XEXP (x, 0), 0),
5907 XEXP (x, 1));
5908 if (tem)
5909 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5911 break;
5913 case UDIV:
5914 /* If this is a divide by a power of two, treat it as a shift if
5915 its first operand is a shift. */
5916 if (CONST_INT_P (XEXP (x, 1))
5917 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5918 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5919 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5920 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5921 || GET_CODE (XEXP (x, 0)) == ROTATE
5922 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5923 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5924 break;
5926 case EQ: case NE:
5927 case GT: case GTU: case GE: case GEU:
5928 case LT: case LTU: case LE: case LEU:
5929 case UNEQ: case LTGT:
5930 case UNGT: case UNGE:
5931 case UNLT: case UNLE:
5932 case UNORDERED: case ORDERED:
5933 /* If the first operand is a condition code, we can't do anything
5934 with it. */
5935 if (GET_CODE (XEXP (x, 0)) == COMPARE
5936 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5937 && ! CC0_P (XEXP (x, 0))))
5939 rtx op0 = XEXP (x, 0);
5940 rtx op1 = XEXP (x, 1);
5941 enum rtx_code new_code;
5943 if (GET_CODE (op0) == COMPARE)
5944 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5946 /* Simplify our comparison, if possible. */
5947 new_code = simplify_comparison (code, &op0, &op1);
5949 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5950 if only the low-order bit is possibly nonzero in X (such as when
5951 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5952 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5953 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5954 (plus X 1).
5956 Remove any ZERO_EXTRACT we made when thinking this was a
5957 comparison. It may now be simpler to use, e.g., an AND. If a
5958 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5959 the call to make_compound_operation in the SET case.
5961 Don't apply these optimizations if the caller would
5962 prefer a comparison rather than a value.
5963 E.g., for the condition in an IF_THEN_ELSE most targets need
5964 an explicit comparison. */
5966 if (in_cond)
5969 else if (STORE_FLAG_VALUE == 1
5970 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5971 && op1 == const0_rtx
5972 && mode == GET_MODE (op0)
5973 && nonzero_bits (op0, mode) == 1)
5974 return gen_lowpart (mode,
5975 expand_compound_operation (op0));
5977 else if (STORE_FLAG_VALUE == 1
5978 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5979 && op1 == const0_rtx
5980 && mode == GET_MODE (op0)
5981 && (num_sign_bit_copies (op0, mode)
5982 == GET_MODE_PRECISION (mode)))
5984 op0 = expand_compound_operation (op0);
5985 return simplify_gen_unary (NEG, mode,
5986 gen_lowpart (mode, op0),
5987 mode);
5990 else if (STORE_FLAG_VALUE == 1
5991 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5992 && op1 == const0_rtx
5993 && mode == GET_MODE (op0)
5994 && nonzero_bits (op0, mode) == 1)
5996 op0 = expand_compound_operation (op0);
5997 return simplify_gen_binary (XOR, mode,
5998 gen_lowpart (mode, op0),
5999 const1_rtx);
6002 else if (STORE_FLAG_VALUE == 1
6003 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6004 && op1 == const0_rtx
6005 && mode == GET_MODE (op0)
6006 && (num_sign_bit_copies (op0, mode)
6007 == GET_MODE_PRECISION (mode)))
6009 op0 = expand_compound_operation (op0);
6010 return plus_constant (mode, gen_lowpart (mode, op0), 1);
6013 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6014 those above. */
6015 if (in_cond)
6018 else if (STORE_FLAG_VALUE == -1
6019 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6020 && op1 == const0_rtx
6021 && mode == GET_MODE (op0)
6022 && (num_sign_bit_copies (op0, mode)
6023 == GET_MODE_PRECISION (mode)))
6024 return gen_lowpart (mode,
6025 expand_compound_operation (op0));
6027 else if (STORE_FLAG_VALUE == -1
6028 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6029 && op1 == const0_rtx
6030 && mode == GET_MODE (op0)
6031 && nonzero_bits (op0, mode) == 1)
6033 op0 = expand_compound_operation (op0);
6034 return simplify_gen_unary (NEG, mode,
6035 gen_lowpart (mode, op0),
6036 mode);
6039 else if (STORE_FLAG_VALUE == -1
6040 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6041 && op1 == const0_rtx
6042 && mode == GET_MODE (op0)
6043 && (num_sign_bit_copies (op0, mode)
6044 == GET_MODE_PRECISION (mode)))
6046 op0 = expand_compound_operation (op0);
6047 return simplify_gen_unary (NOT, mode,
6048 gen_lowpart (mode, op0),
6049 mode);
6052 /* If X is 0/1, (eq X 0) is X-1. */
6053 else if (STORE_FLAG_VALUE == -1
6054 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6055 && op1 == const0_rtx
6056 && mode == GET_MODE (op0)
6057 && nonzero_bits (op0, mode) == 1)
6059 op0 = expand_compound_operation (op0);
6060 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6063 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6064 one bit that might be nonzero, we can convert (ne x 0) to
6065 (ashift x c) where C puts the bit in the sign bit. Remove any
6066 AND with STORE_FLAG_VALUE when we are done, since we are only
6067 going to test the sign bit. */
6068 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6069 && HWI_COMPUTABLE_MODE_P (mode)
6070 && val_signbit_p (mode, STORE_FLAG_VALUE)
6071 && op1 == const0_rtx
6072 && mode == GET_MODE (op0)
6073 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6075 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6076 expand_compound_operation (op0),
6077 GET_MODE_PRECISION (mode) - 1 - i);
6078 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6079 return XEXP (x, 0);
6080 else
6081 return x;
6084 /* If the code changed, return a whole new comparison.
6085 We also need to avoid using SUBST in cases where
6086 simplify_comparison has widened a comparison with a CONST_INT,
6087 since in that case the wider CONST_INT may fail the sanity
6088 checks in do_SUBST. */
6089 if (new_code != code
6090 || (CONST_INT_P (op1)
6091 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6092 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6093 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6095 /* Otherwise, keep this operation, but maybe change its operands.
6096 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6097 SUBST (XEXP (x, 0), op0);
6098 SUBST (XEXP (x, 1), op1);
6100 break;
6102 case IF_THEN_ELSE:
6103 return simplify_if_then_else (x);
6105 case ZERO_EXTRACT:
6106 case SIGN_EXTRACT:
6107 case ZERO_EXTEND:
6108 case SIGN_EXTEND:
6109 /* If we are processing SET_DEST, we are done. */
6110 if (in_dest)
6111 return x;
6113 return expand_compound_operation (x);
6115 case SET:
6116 return simplify_set (x);
6118 case AND:
6119 case IOR:
6120 return simplify_logical (x);
6122 case ASHIFT:
6123 case LSHIFTRT:
6124 case ASHIFTRT:
6125 case ROTATE:
6126 case ROTATERT:
6127 /* If this is a shift by a constant amount, simplify it. */
6128 if (CONST_INT_P (XEXP (x, 1)))
6129 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6130 INTVAL (XEXP (x, 1)));
6132 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6133 SUBST (XEXP (x, 1),
6134 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6135 ((unsigned HOST_WIDE_INT) 1
6136 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6137 - 1,
6138 0));
6139 break;
6141 default:
6142 break;
6145 return x;
6148 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6150 static rtx
6151 simplify_if_then_else (rtx x)
6153 machine_mode mode = GET_MODE (x);
6154 rtx cond = XEXP (x, 0);
6155 rtx true_rtx = XEXP (x, 1);
6156 rtx false_rtx = XEXP (x, 2);
6157 enum rtx_code true_code = GET_CODE (cond);
6158 int comparison_p = COMPARISON_P (cond);
6159 rtx temp;
6160 int i;
6161 enum rtx_code false_code;
6162 rtx reversed;
6164 /* Simplify storing of the truth value. */
6165 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6166 return simplify_gen_relational (true_code, mode, VOIDmode,
6167 XEXP (cond, 0), XEXP (cond, 1));
6169 /* Also when the truth value has to be reversed. */
6170 if (comparison_p
6171 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6172 && (reversed = reversed_comparison (cond, mode)))
6173 return reversed;
6175 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6176 in it is being compared against certain values. Get the true and false
6177 comparisons and see if that says anything about the value of each arm. */
6179 if (comparison_p
6180 && ((false_code = reversed_comparison_code (cond, NULL))
6181 != UNKNOWN)
6182 && REG_P (XEXP (cond, 0)))
6184 HOST_WIDE_INT nzb;
6185 rtx from = XEXP (cond, 0);
6186 rtx true_val = XEXP (cond, 1);
6187 rtx false_val = true_val;
6188 int swapped = 0;
6190 /* If FALSE_CODE is EQ, swap the codes and arms. */
6192 if (false_code == EQ)
6194 swapped = 1, true_code = EQ, false_code = NE;
6195 std::swap (true_rtx, false_rtx);
6198 /* If we are comparing against zero and the expression being tested has
6199 only a single bit that might be nonzero, that is its value when it is
6200 not equal to zero. Similarly if it is known to be -1 or 0. */
6202 if (true_code == EQ && true_val == const0_rtx
6203 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6205 false_code = EQ;
6206 false_val = gen_int_mode (nzb, GET_MODE (from));
6208 else if (true_code == EQ && true_val == const0_rtx
6209 && (num_sign_bit_copies (from, GET_MODE (from))
6210 == GET_MODE_PRECISION (GET_MODE (from))))
6212 false_code = EQ;
6213 false_val = constm1_rtx;
6216 /* Now simplify an arm if we know the value of the register in the
6217 branch and it is used in the arm. Be careful due to the potential
6218 of locally-shared RTL. */
6220 if (reg_mentioned_p (from, true_rtx))
6221 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6222 from, true_val),
6223 pc_rtx, pc_rtx, 0, 0, 0);
6224 if (reg_mentioned_p (from, false_rtx))
6225 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6226 from, false_val),
6227 pc_rtx, pc_rtx, 0, 0, 0);
6229 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6230 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6232 true_rtx = XEXP (x, 1);
6233 false_rtx = XEXP (x, 2);
6234 true_code = GET_CODE (cond);
6237 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6238 reversed, do so to avoid needing two sets of patterns for
6239 subtract-and-branch insns. Similarly if we have a constant in the true
6240 arm, the false arm is the same as the first operand of the comparison, or
6241 the false arm is more complicated than the true arm. */
6243 if (comparison_p
6244 && reversed_comparison_code (cond, NULL) != UNKNOWN
6245 && (true_rtx == pc_rtx
6246 || (CONSTANT_P (true_rtx)
6247 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6248 || true_rtx == const0_rtx
6249 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6250 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6251 && !OBJECT_P (false_rtx))
6252 || reg_mentioned_p (true_rtx, false_rtx)
6253 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6255 true_code = reversed_comparison_code (cond, NULL);
6256 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6257 SUBST (XEXP (x, 1), false_rtx);
6258 SUBST (XEXP (x, 2), true_rtx);
6260 std::swap (true_rtx, false_rtx);
6261 cond = XEXP (x, 0);
6263 /* It is possible that the conditional has been simplified out. */
6264 true_code = GET_CODE (cond);
6265 comparison_p = COMPARISON_P (cond);
6268 /* If the two arms are identical, we don't need the comparison. */
6270 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6271 return true_rtx;
6273 /* Convert a == b ? b : a to "a". */
6274 if (true_code == EQ && ! side_effects_p (cond)
6275 && !HONOR_NANS (mode)
6276 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6277 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6278 return false_rtx;
6279 else if (true_code == NE && ! side_effects_p (cond)
6280 && !HONOR_NANS (mode)
6281 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6282 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6283 return true_rtx;
6285 /* Look for cases where we have (abs x) or (neg (abs X)). */
6287 if (GET_MODE_CLASS (mode) == MODE_INT
6288 && comparison_p
6289 && XEXP (cond, 1) == const0_rtx
6290 && GET_CODE (false_rtx) == NEG
6291 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6292 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6293 && ! side_effects_p (true_rtx))
6294 switch (true_code)
6296 case GT:
6297 case GE:
6298 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6299 case LT:
6300 case LE:
6301 return
6302 simplify_gen_unary (NEG, mode,
6303 simplify_gen_unary (ABS, mode, true_rtx, mode),
6304 mode);
6305 default:
6306 break;
6309 /* Look for MIN or MAX. */
6311 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6312 && comparison_p
6313 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6314 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6315 && ! side_effects_p (cond))
6316 switch (true_code)
6318 case GE:
6319 case GT:
6320 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6321 case LE:
6322 case LT:
6323 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6324 case GEU:
6325 case GTU:
6326 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6327 case LEU:
6328 case LTU:
6329 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6330 default:
6331 break;
6334 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6335 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6336 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6337 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6338 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6339 neither 1 or -1, but it isn't worth checking for. */
6341 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6342 && comparison_p
6343 && GET_MODE_CLASS (mode) == MODE_INT
6344 && ! side_effects_p (x))
6346 rtx t = make_compound_operation (true_rtx, SET);
6347 rtx f = make_compound_operation (false_rtx, SET);
6348 rtx cond_op0 = XEXP (cond, 0);
6349 rtx cond_op1 = XEXP (cond, 1);
6350 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6351 machine_mode m = mode;
6352 rtx z = 0, c1 = NULL_RTX;
6354 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6355 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6356 || GET_CODE (t) == ASHIFT
6357 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6358 && rtx_equal_p (XEXP (t, 0), f))
6359 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6361 /* If an identity-zero op is commutative, check whether there
6362 would be a match if we swapped the operands. */
6363 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6364 || GET_CODE (t) == XOR)
6365 && rtx_equal_p (XEXP (t, 1), f))
6366 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6367 else if (GET_CODE (t) == SIGN_EXTEND
6368 && (GET_CODE (XEXP (t, 0)) == PLUS
6369 || GET_CODE (XEXP (t, 0)) == MINUS
6370 || GET_CODE (XEXP (t, 0)) == IOR
6371 || GET_CODE (XEXP (t, 0)) == XOR
6372 || GET_CODE (XEXP (t, 0)) == ASHIFT
6373 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6374 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6375 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6376 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6377 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6378 && (num_sign_bit_copies (f, GET_MODE (f))
6379 > (unsigned int)
6380 (GET_MODE_PRECISION (mode)
6381 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6383 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6384 extend_op = SIGN_EXTEND;
6385 m = GET_MODE (XEXP (t, 0));
6387 else if (GET_CODE (t) == SIGN_EXTEND
6388 && (GET_CODE (XEXP (t, 0)) == PLUS
6389 || GET_CODE (XEXP (t, 0)) == IOR
6390 || GET_CODE (XEXP (t, 0)) == XOR)
6391 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6392 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6393 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6394 && (num_sign_bit_copies (f, GET_MODE (f))
6395 > (unsigned int)
6396 (GET_MODE_PRECISION (mode)
6397 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6399 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6400 extend_op = SIGN_EXTEND;
6401 m = GET_MODE (XEXP (t, 0));
6403 else if (GET_CODE (t) == ZERO_EXTEND
6404 && (GET_CODE (XEXP (t, 0)) == PLUS
6405 || GET_CODE (XEXP (t, 0)) == MINUS
6406 || GET_CODE (XEXP (t, 0)) == IOR
6407 || GET_CODE (XEXP (t, 0)) == XOR
6408 || GET_CODE (XEXP (t, 0)) == ASHIFT
6409 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6410 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6411 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6412 && HWI_COMPUTABLE_MODE_P (mode)
6413 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6414 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6415 && ((nonzero_bits (f, GET_MODE (f))
6416 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6417 == 0))
6419 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6420 extend_op = ZERO_EXTEND;
6421 m = GET_MODE (XEXP (t, 0));
6423 else if (GET_CODE (t) == ZERO_EXTEND
6424 && (GET_CODE (XEXP (t, 0)) == PLUS
6425 || GET_CODE (XEXP (t, 0)) == IOR
6426 || GET_CODE (XEXP (t, 0)) == XOR)
6427 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6428 && HWI_COMPUTABLE_MODE_P (mode)
6429 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6430 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6431 && ((nonzero_bits (f, GET_MODE (f))
6432 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6433 == 0))
6435 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6436 extend_op = ZERO_EXTEND;
6437 m = GET_MODE (XEXP (t, 0));
6440 if (z)
6442 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6443 cond_op0, cond_op1),
6444 pc_rtx, pc_rtx, 0, 0, 0);
6445 temp = simplify_gen_binary (MULT, m, temp,
6446 simplify_gen_binary (MULT, m, c1,
6447 const_true_rtx));
6448 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6449 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6451 if (extend_op != UNKNOWN)
6452 temp = simplify_gen_unary (extend_op, mode, temp, m);
6454 return temp;
6458 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6459 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6460 negation of a single bit, we can convert this operation to a shift. We
6461 can actually do this more generally, but it doesn't seem worth it. */
6463 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6464 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6465 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6466 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6467 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6468 == GET_MODE_PRECISION (mode))
6469 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6470 return
6471 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6472 gen_lowpart (mode, XEXP (cond, 0)), i);
6474 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6475 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6476 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6477 && GET_MODE (XEXP (cond, 0)) == mode
6478 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6479 == nonzero_bits (XEXP (cond, 0), mode)
6480 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6481 return XEXP (cond, 0);
6483 return x;
6486 /* Simplify X, a SET expression. Return the new expression. */
6488 static rtx
6489 simplify_set (rtx x)
6491 rtx src = SET_SRC (x);
6492 rtx dest = SET_DEST (x);
6493 machine_mode mode
6494 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6495 rtx_insn *other_insn;
6496 rtx *cc_use;
6498 /* (set (pc) (return)) gets written as (return). */
6499 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6500 return src;
6502 /* Now that we know for sure which bits of SRC we are using, see if we can
6503 simplify the expression for the object knowing that we only need the
6504 low-order bits. */
6506 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6508 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6509 SUBST (SET_SRC (x), src);
6512 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6513 the comparison result and try to simplify it unless we already have used
6514 undobuf.other_insn. */
6515 if ((GET_MODE_CLASS (mode) == MODE_CC
6516 || GET_CODE (src) == COMPARE
6517 || CC0_P (dest))
6518 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6519 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6520 && COMPARISON_P (*cc_use)
6521 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6523 enum rtx_code old_code = GET_CODE (*cc_use);
6524 enum rtx_code new_code;
6525 rtx op0, op1, tmp;
6526 int other_changed = 0;
6527 rtx inner_compare = NULL_RTX;
6528 machine_mode compare_mode = GET_MODE (dest);
6530 if (GET_CODE (src) == COMPARE)
6532 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6533 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6535 inner_compare = op0;
6536 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6539 else
6540 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6542 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6543 op0, op1);
6544 if (!tmp)
6545 new_code = old_code;
6546 else if (!CONSTANT_P (tmp))
6548 new_code = GET_CODE (tmp);
6549 op0 = XEXP (tmp, 0);
6550 op1 = XEXP (tmp, 1);
6552 else
6554 rtx pat = PATTERN (other_insn);
6555 undobuf.other_insn = other_insn;
6556 SUBST (*cc_use, tmp);
6558 /* Attempt to simplify CC user. */
6559 if (GET_CODE (pat) == SET)
6561 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6562 if (new_rtx != NULL_RTX)
6563 SUBST (SET_SRC (pat), new_rtx);
6566 /* Convert X into a no-op move. */
6567 SUBST (SET_DEST (x), pc_rtx);
6568 SUBST (SET_SRC (x), pc_rtx);
6569 return x;
6572 /* Simplify our comparison, if possible. */
6573 new_code = simplify_comparison (new_code, &op0, &op1);
6575 #ifdef SELECT_CC_MODE
6576 /* If this machine has CC modes other than CCmode, check to see if we
6577 need to use a different CC mode here. */
6578 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6579 compare_mode = GET_MODE (op0);
6580 else if (inner_compare
6581 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6582 && new_code == old_code
6583 && op0 == XEXP (inner_compare, 0)
6584 && op1 == XEXP (inner_compare, 1))
6585 compare_mode = GET_MODE (inner_compare);
6586 else
6587 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6589 /* If the mode changed, we have to change SET_DEST, the mode in the
6590 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6591 a hard register, just build new versions with the proper mode. If it
6592 is a pseudo, we lose unless it is only time we set the pseudo, in
6593 which case we can safely change its mode. */
6594 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6596 if (can_change_dest_mode (dest, 0, compare_mode))
6598 unsigned int regno = REGNO (dest);
6599 rtx new_dest;
6601 if (regno < FIRST_PSEUDO_REGISTER)
6602 new_dest = gen_rtx_REG (compare_mode, regno);
6603 else
6605 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6606 new_dest = regno_reg_rtx[regno];
6609 SUBST (SET_DEST (x), new_dest);
6610 SUBST (XEXP (*cc_use, 0), new_dest);
6611 other_changed = 1;
6613 dest = new_dest;
6616 #endif /* SELECT_CC_MODE */
6618 /* If the code changed, we have to build a new comparison in
6619 undobuf.other_insn. */
6620 if (new_code != old_code)
6622 int other_changed_previously = other_changed;
6623 unsigned HOST_WIDE_INT mask;
6624 rtx old_cc_use = *cc_use;
6626 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6627 dest, const0_rtx));
6628 other_changed = 1;
6630 /* If the only change we made was to change an EQ into an NE or
6631 vice versa, OP0 has only one bit that might be nonzero, and OP1
6632 is zero, check if changing the user of the condition code will
6633 produce a valid insn. If it won't, we can keep the original code
6634 in that insn by surrounding our operation with an XOR. */
6636 if (((old_code == NE && new_code == EQ)
6637 || (old_code == EQ && new_code == NE))
6638 && ! other_changed_previously && op1 == const0_rtx
6639 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6640 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6642 rtx pat = PATTERN (other_insn), note = 0;
6644 if ((recog_for_combine (&pat, other_insn, &note) < 0
6645 && ! check_asm_operands (pat)))
6647 *cc_use = old_cc_use;
6648 other_changed = 0;
6650 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6651 gen_int_mode (mask,
6652 GET_MODE (op0)));
6657 if (other_changed)
6658 undobuf.other_insn = other_insn;
6660 /* Otherwise, if we didn't previously have a COMPARE in the
6661 correct mode, we need one. */
6662 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6664 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6665 src = SET_SRC (x);
6667 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6669 SUBST (SET_SRC (x), op0);
6670 src = SET_SRC (x);
6672 /* Otherwise, update the COMPARE if needed. */
6673 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6675 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6676 src = SET_SRC (x);
6679 else
6681 /* Get SET_SRC in a form where we have placed back any
6682 compound expressions. Then do the checks below. */
6683 src = make_compound_operation (src, SET);
6684 SUBST (SET_SRC (x), src);
6687 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6688 and X being a REG or (subreg (reg)), we may be able to convert this to
6689 (set (subreg:m2 x) (op)).
6691 We can always do this if M1 is narrower than M2 because that means that
6692 we only care about the low bits of the result.
6694 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6695 perform a narrower operation than requested since the high-order bits will
6696 be undefined. On machine where it is defined, this transformation is safe
6697 as long as M1 and M2 have the same number of words. */
6699 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6700 && !OBJECT_P (SUBREG_REG (src))
6701 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6702 / UNITS_PER_WORD)
6703 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6704 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6705 #ifndef WORD_REGISTER_OPERATIONS
6706 && (GET_MODE_SIZE (GET_MODE (src))
6707 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6708 #endif
6709 #ifdef CANNOT_CHANGE_MODE_CLASS
6710 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6711 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6712 GET_MODE (SUBREG_REG (src)),
6713 GET_MODE (src)))
6714 #endif
6715 && (REG_P (dest)
6716 || (GET_CODE (dest) == SUBREG
6717 && REG_P (SUBREG_REG (dest)))))
6719 SUBST (SET_DEST (x),
6720 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6721 dest));
6722 SUBST (SET_SRC (x), SUBREG_REG (src));
6724 src = SET_SRC (x), dest = SET_DEST (x);
6727 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6728 in SRC. */
6729 if (dest == cc0_rtx
6730 && GET_CODE (src) == SUBREG
6731 && subreg_lowpart_p (src)
6732 && (GET_MODE_PRECISION (GET_MODE (src))
6733 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6735 rtx inner = SUBREG_REG (src);
6736 machine_mode inner_mode = GET_MODE (inner);
6738 /* Here we make sure that we don't have a sign bit on. */
6739 if (val_signbit_known_clear_p (GET_MODE (src),
6740 nonzero_bits (inner, inner_mode)))
6742 SUBST (SET_SRC (x), inner);
6743 src = SET_SRC (x);
6747 #ifdef LOAD_EXTEND_OP
6748 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6749 would require a paradoxical subreg. Replace the subreg with a
6750 zero_extend to avoid the reload that would otherwise be required. */
6752 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6753 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6754 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6755 && SUBREG_BYTE (src) == 0
6756 && paradoxical_subreg_p (src)
6757 && MEM_P (SUBREG_REG (src)))
6759 SUBST (SET_SRC (x),
6760 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6761 GET_MODE (src), SUBREG_REG (src)));
6763 src = SET_SRC (x);
6765 #endif
6767 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6768 are comparing an item known to be 0 or -1 against 0, use a logical
6769 operation instead. Check for one of the arms being an IOR of the other
6770 arm with some value. We compute three terms to be IOR'ed together. In
6771 practice, at most two will be nonzero. Then we do the IOR's. */
6773 if (GET_CODE (dest) != PC
6774 && GET_CODE (src) == IF_THEN_ELSE
6775 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6776 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6777 && XEXP (XEXP (src, 0), 1) == const0_rtx
6778 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6779 #ifdef HAVE_conditional_move
6780 && ! can_conditionally_move_p (GET_MODE (src))
6781 #endif
6782 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6783 GET_MODE (XEXP (XEXP (src, 0), 0)))
6784 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6785 && ! side_effects_p (src))
6787 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6788 ? XEXP (src, 1) : XEXP (src, 2));
6789 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6790 ? XEXP (src, 2) : XEXP (src, 1));
6791 rtx term1 = const0_rtx, term2, term3;
6793 if (GET_CODE (true_rtx) == IOR
6794 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6795 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6796 else if (GET_CODE (true_rtx) == IOR
6797 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6798 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6799 else if (GET_CODE (false_rtx) == IOR
6800 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6801 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6802 else if (GET_CODE (false_rtx) == IOR
6803 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6804 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6806 term2 = simplify_gen_binary (AND, GET_MODE (src),
6807 XEXP (XEXP (src, 0), 0), true_rtx);
6808 term3 = simplify_gen_binary (AND, GET_MODE (src),
6809 simplify_gen_unary (NOT, GET_MODE (src),
6810 XEXP (XEXP (src, 0), 0),
6811 GET_MODE (src)),
6812 false_rtx);
6814 SUBST (SET_SRC (x),
6815 simplify_gen_binary (IOR, GET_MODE (src),
6816 simplify_gen_binary (IOR, GET_MODE (src),
6817 term1, term2),
6818 term3));
6820 src = SET_SRC (x);
6823 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6824 whole thing fail. */
6825 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6826 return src;
6827 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6828 return dest;
6829 else
6830 /* Convert this into a field assignment operation, if possible. */
6831 return make_field_assignment (x);
6834 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6835 result. */
6837 static rtx
6838 simplify_logical (rtx x)
6840 machine_mode mode = GET_MODE (x);
6841 rtx op0 = XEXP (x, 0);
6842 rtx op1 = XEXP (x, 1);
6844 switch (GET_CODE (x))
6846 case AND:
6847 /* We can call simplify_and_const_int only if we don't lose
6848 any (sign) bits when converting INTVAL (op1) to
6849 "unsigned HOST_WIDE_INT". */
6850 if (CONST_INT_P (op1)
6851 && (HWI_COMPUTABLE_MODE_P (mode)
6852 || INTVAL (op1) > 0))
6854 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6855 if (GET_CODE (x) != AND)
6856 return x;
6858 op0 = XEXP (x, 0);
6859 op1 = XEXP (x, 1);
6862 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6863 apply the distributive law and then the inverse distributive
6864 law to see if things simplify. */
6865 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6867 rtx result = distribute_and_simplify_rtx (x, 0);
6868 if (result)
6869 return result;
6871 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6873 rtx result = distribute_and_simplify_rtx (x, 1);
6874 if (result)
6875 return result;
6877 break;
6879 case IOR:
6880 /* If we have (ior (and A B) C), apply the distributive law and then
6881 the inverse distributive law to see if things simplify. */
6883 if (GET_CODE (op0) == AND)
6885 rtx result = distribute_and_simplify_rtx (x, 0);
6886 if (result)
6887 return result;
6890 if (GET_CODE (op1) == AND)
6892 rtx result = distribute_and_simplify_rtx (x, 1);
6893 if (result)
6894 return result;
6896 break;
6898 default:
6899 gcc_unreachable ();
6902 return x;
6905 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6906 operations" because they can be replaced with two more basic operations.
6907 ZERO_EXTEND is also considered "compound" because it can be replaced with
6908 an AND operation, which is simpler, though only one operation.
6910 The function expand_compound_operation is called with an rtx expression
6911 and will convert it to the appropriate shifts and AND operations,
6912 simplifying at each stage.
6914 The function make_compound_operation is called to convert an expression
6915 consisting of shifts and ANDs into the equivalent compound expression.
6916 It is the inverse of this function, loosely speaking. */
6918 static rtx
6919 expand_compound_operation (rtx x)
6921 unsigned HOST_WIDE_INT pos = 0, len;
6922 int unsignedp = 0;
6923 unsigned int modewidth;
6924 rtx tem;
6926 switch (GET_CODE (x))
6928 case ZERO_EXTEND:
6929 unsignedp = 1;
6930 case SIGN_EXTEND:
6931 /* We can't necessarily use a const_int for a multiword mode;
6932 it depends on implicitly extending the value.
6933 Since we don't know the right way to extend it,
6934 we can't tell whether the implicit way is right.
6936 Even for a mode that is no wider than a const_int,
6937 we can't win, because we need to sign extend one of its bits through
6938 the rest of it, and we don't know which bit. */
6939 if (CONST_INT_P (XEXP (x, 0)))
6940 return x;
6942 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6943 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6944 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6945 reloaded. If not for that, MEM's would very rarely be safe.
6947 Reject MODEs bigger than a word, because we might not be able
6948 to reference a two-register group starting with an arbitrary register
6949 (and currently gen_lowpart might crash for a SUBREG). */
6951 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6952 return x;
6954 /* Reject MODEs that aren't scalar integers because turning vector
6955 or complex modes into shifts causes problems. */
6957 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6958 return x;
6960 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6961 /* If the inner object has VOIDmode (the only way this can happen
6962 is if it is an ASM_OPERANDS), we can't do anything since we don't
6963 know how much masking to do. */
6964 if (len == 0)
6965 return x;
6967 break;
6969 case ZERO_EXTRACT:
6970 unsignedp = 1;
6972 /* ... fall through ... */
6974 case SIGN_EXTRACT:
6975 /* If the operand is a CLOBBER, just return it. */
6976 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6977 return XEXP (x, 0);
6979 if (!CONST_INT_P (XEXP (x, 1))
6980 || !CONST_INT_P (XEXP (x, 2))
6981 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6982 return x;
6984 /* Reject MODEs that aren't scalar integers because turning vector
6985 or complex modes into shifts causes problems. */
6987 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6988 return x;
6990 len = INTVAL (XEXP (x, 1));
6991 pos = INTVAL (XEXP (x, 2));
6993 /* This should stay within the object being extracted, fail otherwise. */
6994 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6995 return x;
6997 if (BITS_BIG_ENDIAN)
6998 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
7000 break;
7002 default:
7003 return x;
7005 /* Convert sign extension to zero extension, if we know that the high
7006 bit is not set, as this is easier to optimize. It will be converted
7007 back to cheaper alternative in make_extraction. */
7008 if (GET_CODE (x) == SIGN_EXTEND
7009 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7010 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7011 & ~(((unsigned HOST_WIDE_INT)
7012 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
7013 >> 1))
7014 == 0)))
7016 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
7017 rtx temp2 = expand_compound_operation (temp);
7019 /* Make sure this is a profitable operation. */
7020 if (set_src_cost (x, optimize_this_for_speed_p)
7021 > set_src_cost (temp2, optimize_this_for_speed_p))
7022 return temp2;
7023 else if (set_src_cost (x, optimize_this_for_speed_p)
7024 > set_src_cost (temp, optimize_this_for_speed_p))
7025 return temp;
7026 else
7027 return x;
7030 /* We can optimize some special cases of ZERO_EXTEND. */
7031 if (GET_CODE (x) == ZERO_EXTEND)
7033 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7034 know that the last value didn't have any inappropriate bits
7035 set. */
7036 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7037 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7038 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7039 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
7040 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7041 return XEXP (XEXP (x, 0), 0);
7043 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7044 if (GET_CODE (XEXP (x, 0)) == SUBREG
7045 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7046 && subreg_lowpart_p (XEXP (x, 0))
7047 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7048 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
7049 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7050 return SUBREG_REG (XEXP (x, 0));
7052 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7053 is a comparison and STORE_FLAG_VALUE permits. This is like
7054 the first case, but it works even when GET_MODE (x) is larger
7055 than HOST_WIDE_INT. */
7056 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7057 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7058 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7059 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7060 <= HOST_BITS_PER_WIDE_INT)
7061 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7062 return XEXP (XEXP (x, 0), 0);
7064 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7065 if (GET_CODE (XEXP (x, 0)) == SUBREG
7066 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7067 && subreg_lowpart_p (XEXP (x, 0))
7068 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7069 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7070 <= HOST_BITS_PER_WIDE_INT)
7071 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7072 return SUBREG_REG (XEXP (x, 0));
7076 /* If we reach here, we want to return a pair of shifts. The inner
7077 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7078 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7079 logical depending on the value of UNSIGNEDP.
7081 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7082 converted into an AND of a shift.
7084 We must check for the case where the left shift would have a negative
7085 count. This can happen in a case like (x >> 31) & 255 on machines
7086 that can't shift by a constant. On those machines, we would first
7087 combine the shift with the AND to produce a variable-position
7088 extraction. Then the constant of 31 would be substituted in
7089 to produce such a position. */
7091 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7092 if (modewidth >= pos + len)
7094 machine_mode mode = GET_MODE (x);
7095 tem = gen_lowpart (mode, XEXP (x, 0));
7096 if (!tem || GET_CODE (tem) == CLOBBER)
7097 return x;
7098 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7099 tem, modewidth - pos - len);
7100 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7101 mode, tem, modewidth - len);
7103 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7104 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7105 simplify_shift_const (NULL_RTX, LSHIFTRT,
7106 GET_MODE (x),
7107 XEXP (x, 0), pos),
7108 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7109 else
7110 /* Any other cases we can't handle. */
7111 return x;
7113 /* If we couldn't do this for some reason, return the original
7114 expression. */
7115 if (GET_CODE (tem) == CLOBBER)
7116 return x;
7118 return tem;
7121 /* X is a SET which contains an assignment of one object into
7122 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7123 or certain SUBREGS). If possible, convert it into a series of
7124 logical operations.
7126 We half-heartedly support variable positions, but do not at all
7127 support variable lengths. */
7129 static const_rtx
7130 expand_field_assignment (const_rtx x)
7132 rtx inner;
7133 rtx pos; /* Always counts from low bit. */
7134 int len;
7135 rtx mask, cleared, masked;
7136 machine_mode compute_mode;
7138 /* Loop until we find something we can't simplify. */
7139 while (1)
7141 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7142 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7144 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7145 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7146 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7148 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7149 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7151 inner = XEXP (SET_DEST (x), 0);
7152 len = INTVAL (XEXP (SET_DEST (x), 1));
7153 pos = XEXP (SET_DEST (x), 2);
7155 /* A constant position should stay within the width of INNER. */
7156 if (CONST_INT_P (pos)
7157 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7158 break;
7160 if (BITS_BIG_ENDIAN)
7162 if (CONST_INT_P (pos))
7163 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7164 - INTVAL (pos));
7165 else if (GET_CODE (pos) == MINUS
7166 && CONST_INT_P (XEXP (pos, 1))
7167 && (INTVAL (XEXP (pos, 1))
7168 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7169 /* If position is ADJUST - X, new position is X. */
7170 pos = XEXP (pos, 0);
7171 else
7173 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7174 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7175 gen_int_mode (prec - len,
7176 GET_MODE (pos)),
7177 pos);
7182 /* A SUBREG between two modes that occupy the same numbers of words
7183 can be done by moving the SUBREG to the source. */
7184 else if (GET_CODE (SET_DEST (x)) == SUBREG
7185 /* We need SUBREGs to compute nonzero_bits properly. */
7186 && nonzero_sign_valid
7187 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7188 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7189 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7190 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7192 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7193 gen_lowpart
7194 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7195 SET_SRC (x)));
7196 continue;
7198 else
7199 break;
7201 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7202 inner = SUBREG_REG (inner);
7204 compute_mode = GET_MODE (inner);
7206 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7207 if (! SCALAR_INT_MODE_P (compute_mode))
7209 machine_mode imode;
7211 /* Don't do anything for vector or complex integral types. */
7212 if (! FLOAT_MODE_P (compute_mode))
7213 break;
7215 /* Try to find an integral mode to pun with. */
7216 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7217 if (imode == BLKmode)
7218 break;
7220 compute_mode = imode;
7221 inner = gen_lowpart (imode, inner);
7224 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7225 if (len >= HOST_BITS_PER_WIDE_INT)
7226 break;
7228 /* Now compute the equivalent expression. Make a copy of INNER
7229 for the SET_DEST in case it is a MEM into which we will substitute;
7230 we don't want shared RTL in that case. */
7231 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7232 compute_mode);
7233 cleared = simplify_gen_binary (AND, compute_mode,
7234 simplify_gen_unary (NOT, compute_mode,
7235 simplify_gen_binary (ASHIFT,
7236 compute_mode,
7237 mask, pos),
7238 compute_mode),
7239 inner);
7240 masked = simplify_gen_binary (ASHIFT, compute_mode,
7241 simplify_gen_binary (
7242 AND, compute_mode,
7243 gen_lowpart (compute_mode, SET_SRC (x)),
7244 mask),
7245 pos);
7247 x = gen_rtx_SET (copy_rtx (inner),
7248 simplify_gen_binary (IOR, compute_mode,
7249 cleared, masked));
7252 return x;
7255 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7256 it is an RTX that represents the (variable) starting position; otherwise,
7257 POS is the (constant) starting bit position. Both are counted from the LSB.
7259 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7261 IN_DEST is nonzero if this is a reference in the destination of a SET.
7262 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7263 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7264 be used.
7266 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7267 ZERO_EXTRACT should be built even for bits starting at bit 0.
7269 MODE is the desired mode of the result (if IN_DEST == 0).
7271 The result is an RTX for the extraction or NULL_RTX if the target
7272 can't handle it. */
7274 static rtx
7275 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7276 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7277 int in_dest, int in_compare)
7279 /* This mode describes the size of the storage area
7280 to fetch the overall value from. Within that, we
7281 ignore the POS lowest bits, etc. */
7282 machine_mode is_mode = GET_MODE (inner);
7283 machine_mode inner_mode;
7284 machine_mode wanted_inner_mode;
7285 machine_mode wanted_inner_reg_mode = word_mode;
7286 machine_mode pos_mode = word_mode;
7287 machine_mode extraction_mode = word_mode;
7288 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7289 rtx new_rtx = 0;
7290 rtx orig_pos_rtx = pos_rtx;
7291 HOST_WIDE_INT orig_pos;
7293 if (pos_rtx && CONST_INT_P (pos_rtx))
7294 pos = INTVAL (pos_rtx), pos_rtx = 0;
7296 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7298 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7299 consider just the QI as the memory to extract from.
7300 The subreg adds or removes high bits; its mode is
7301 irrelevant to the meaning of this extraction,
7302 since POS and LEN count from the lsb. */
7303 if (MEM_P (SUBREG_REG (inner)))
7304 is_mode = GET_MODE (SUBREG_REG (inner));
7305 inner = SUBREG_REG (inner);
7307 else if (GET_CODE (inner) == ASHIFT
7308 && CONST_INT_P (XEXP (inner, 1))
7309 && pos_rtx == 0 && pos == 0
7310 && len > UINTVAL (XEXP (inner, 1)))
7312 /* We're extracting the least significant bits of an rtx
7313 (ashift X (const_int C)), where LEN > C. Extract the
7314 least significant (LEN - C) bits of X, giving an rtx
7315 whose mode is MODE, then shift it left C times. */
7316 new_rtx = make_extraction (mode, XEXP (inner, 0),
7317 0, 0, len - INTVAL (XEXP (inner, 1)),
7318 unsignedp, in_dest, in_compare);
7319 if (new_rtx != 0)
7320 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7322 else if (GET_CODE (inner) == TRUNCATE)
7323 inner = XEXP (inner, 0);
7325 inner_mode = GET_MODE (inner);
7327 /* See if this can be done without an extraction. We never can if the
7328 width of the field is not the same as that of some integer mode. For
7329 registers, we can only avoid the extraction if the position is at the
7330 low-order bit and this is either not in the destination or we have the
7331 appropriate STRICT_LOW_PART operation available.
7333 For MEM, we can avoid an extract if the field starts on an appropriate
7334 boundary and we can change the mode of the memory reference. */
7336 if (tmode != BLKmode
7337 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7338 && !MEM_P (inner)
7339 && (inner_mode == tmode
7340 || !REG_P (inner)
7341 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7342 || reg_truncated_to_mode (tmode, inner))
7343 && (! in_dest
7344 || (REG_P (inner)
7345 && have_insn_for (STRICT_LOW_PART, tmode))))
7346 || (MEM_P (inner) && pos_rtx == 0
7347 && (pos
7348 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7349 : BITS_PER_UNIT)) == 0
7350 /* We can't do this if we are widening INNER_MODE (it
7351 may not be aligned, for one thing). */
7352 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7353 && (inner_mode == tmode
7354 || (! mode_dependent_address_p (XEXP (inner, 0),
7355 MEM_ADDR_SPACE (inner))
7356 && ! MEM_VOLATILE_P (inner))))))
7358 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7359 field. If the original and current mode are the same, we need not
7360 adjust the offset. Otherwise, we do if bytes big endian.
7362 If INNER is not a MEM, get a piece consisting of just the field
7363 of interest (in this case POS % BITS_PER_WORD must be 0). */
7365 if (MEM_P (inner))
7367 HOST_WIDE_INT offset;
7369 /* POS counts from lsb, but make OFFSET count in memory order. */
7370 if (BYTES_BIG_ENDIAN)
7371 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7372 else
7373 offset = pos / BITS_PER_UNIT;
7375 new_rtx = adjust_address_nv (inner, tmode, offset);
7377 else if (REG_P (inner))
7379 if (tmode != inner_mode)
7381 /* We can't call gen_lowpart in a DEST since we
7382 always want a SUBREG (see below) and it would sometimes
7383 return a new hard register. */
7384 if (pos || in_dest)
7386 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7388 if (WORDS_BIG_ENDIAN
7389 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7390 final_word = ((GET_MODE_SIZE (inner_mode)
7391 - GET_MODE_SIZE (tmode))
7392 / UNITS_PER_WORD) - final_word;
7394 final_word *= UNITS_PER_WORD;
7395 if (BYTES_BIG_ENDIAN &&
7396 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7397 final_word += (GET_MODE_SIZE (inner_mode)
7398 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7400 /* Avoid creating invalid subregs, for example when
7401 simplifying (x>>32)&255. */
7402 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7403 return NULL_RTX;
7405 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7407 else
7408 new_rtx = gen_lowpart (tmode, inner);
7410 else
7411 new_rtx = inner;
7413 else
7414 new_rtx = force_to_mode (inner, tmode,
7415 len >= HOST_BITS_PER_WIDE_INT
7416 ? ~(unsigned HOST_WIDE_INT) 0
7417 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7420 /* If this extraction is going into the destination of a SET,
7421 make a STRICT_LOW_PART unless we made a MEM. */
7423 if (in_dest)
7424 return (MEM_P (new_rtx) ? new_rtx
7425 : (GET_CODE (new_rtx) != SUBREG
7426 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7427 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7429 if (mode == tmode)
7430 return new_rtx;
7432 if (CONST_SCALAR_INT_P (new_rtx))
7433 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7434 mode, new_rtx, tmode);
7436 /* If we know that no extraneous bits are set, and that the high
7437 bit is not set, convert the extraction to the cheaper of
7438 sign and zero extension, that are equivalent in these cases. */
7439 if (flag_expensive_optimizations
7440 && (HWI_COMPUTABLE_MODE_P (tmode)
7441 && ((nonzero_bits (new_rtx, tmode)
7442 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7443 == 0)))
7445 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7446 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7448 /* Prefer ZERO_EXTENSION, since it gives more information to
7449 backends. */
7450 if (set_src_cost (temp, optimize_this_for_speed_p)
7451 <= set_src_cost (temp1, optimize_this_for_speed_p))
7452 return temp;
7453 return temp1;
7456 /* Otherwise, sign- or zero-extend unless we already are in the
7457 proper mode. */
7459 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7460 mode, new_rtx));
7463 /* Unless this is a COMPARE or we have a funny memory reference,
7464 don't do anything with zero-extending field extracts starting at
7465 the low-order bit since they are simple AND operations. */
7466 if (pos_rtx == 0 && pos == 0 && ! in_dest
7467 && ! in_compare && unsignedp)
7468 return 0;
7470 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7471 if the position is not a constant and the length is not 1. In all
7472 other cases, we would only be going outside our object in cases when
7473 an original shift would have been undefined. */
7474 if (MEM_P (inner)
7475 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7476 || (pos_rtx != 0 && len != 1)))
7477 return 0;
7479 enum extraction_pattern pattern = (in_dest ? EP_insv
7480 : unsignedp ? EP_extzv : EP_extv);
7482 /* If INNER is not from memory, we want it to have the mode of a register
7483 extraction pattern's structure operand, or word_mode if there is no
7484 such pattern. The same applies to extraction_mode and pos_mode
7485 and their respective operands.
7487 For memory, assume that the desired extraction_mode and pos_mode
7488 are the same as for a register operation, since at present we don't
7489 have named patterns for aligned memory structures. */
7490 struct extraction_insn insn;
7491 if (get_best_reg_extraction_insn (&insn, pattern,
7492 GET_MODE_BITSIZE (inner_mode), mode))
7494 wanted_inner_reg_mode = insn.struct_mode;
7495 pos_mode = insn.pos_mode;
7496 extraction_mode = insn.field_mode;
7499 /* Never narrow an object, since that might not be safe. */
7501 if (mode != VOIDmode
7502 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7503 extraction_mode = mode;
7505 if (!MEM_P (inner))
7506 wanted_inner_mode = wanted_inner_reg_mode;
7507 else
7509 /* Be careful not to go beyond the extracted object and maintain the
7510 natural alignment of the memory. */
7511 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7512 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7513 > GET_MODE_BITSIZE (wanted_inner_mode))
7515 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7516 gcc_assert (wanted_inner_mode != VOIDmode);
7520 orig_pos = pos;
7522 if (BITS_BIG_ENDIAN)
7524 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7525 BITS_BIG_ENDIAN style. If position is constant, compute new
7526 position. Otherwise, build subtraction.
7527 Note that POS is relative to the mode of the original argument.
7528 If it's a MEM we need to recompute POS relative to that.
7529 However, if we're extracting from (or inserting into) a register,
7530 we want to recompute POS relative to wanted_inner_mode. */
7531 int width = (MEM_P (inner)
7532 ? GET_MODE_BITSIZE (is_mode)
7533 : GET_MODE_BITSIZE (wanted_inner_mode));
7535 if (pos_rtx == 0)
7536 pos = width - len - pos;
7537 else
7538 pos_rtx
7539 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7540 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7541 pos_rtx);
7542 /* POS may be less than 0 now, but we check for that below.
7543 Note that it can only be less than 0 if !MEM_P (inner). */
7546 /* If INNER has a wider mode, and this is a constant extraction, try to
7547 make it smaller and adjust the byte to point to the byte containing
7548 the value. */
7549 if (wanted_inner_mode != VOIDmode
7550 && inner_mode != wanted_inner_mode
7551 && ! pos_rtx
7552 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7553 && MEM_P (inner)
7554 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7555 && ! MEM_VOLATILE_P (inner))
7557 int offset = 0;
7559 /* The computations below will be correct if the machine is big
7560 endian in both bits and bytes or little endian in bits and bytes.
7561 If it is mixed, we must adjust. */
7563 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7564 adjust OFFSET to compensate. */
7565 if (BYTES_BIG_ENDIAN
7566 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7567 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7569 /* We can now move to the desired byte. */
7570 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7571 * GET_MODE_SIZE (wanted_inner_mode);
7572 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7574 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7575 && is_mode != wanted_inner_mode)
7576 offset = (GET_MODE_SIZE (is_mode)
7577 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7579 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7582 /* If INNER is not memory, get it into the proper mode. If we are changing
7583 its mode, POS must be a constant and smaller than the size of the new
7584 mode. */
7585 else if (!MEM_P (inner))
7587 /* On the LHS, don't create paradoxical subregs implicitely truncating
7588 the register unless TRULY_NOOP_TRUNCATION. */
7589 if (in_dest
7590 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7591 wanted_inner_mode))
7592 return NULL_RTX;
7594 if (GET_MODE (inner) != wanted_inner_mode
7595 && (pos_rtx != 0
7596 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7597 return NULL_RTX;
7599 if (orig_pos < 0)
7600 return NULL_RTX;
7602 inner = force_to_mode (inner, wanted_inner_mode,
7603 pos_rtx
7604 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7605 ? ~(unsigned HOST_WIDE_INT) 0
7606 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7607 << orig_pos),
7611 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7612 have to zero extend. Otherwise, we can just use a SUBREG. */
7613 if (pos_rtx != 0
7614 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7616 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7617 GET_MODE (pos_rtx));
7619 /* If we know that no extraneous bits are set, and that the high
7620 bit is not set, convert extraction to cheaper one - either
7621 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7622 cases. */
7623 if (flag_expensive_optimizations
7624 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7625 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7626 & ~(((unsigned HOST_WIDE_INT)
7627 GET_MODE_MASK (GET_MODE (pos_rtx)))
7628 >> 1))
7629 == 0)))
7631 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7632 GET_MODE (pos_rtx));
7634 /* Prefer ZERO_EXTENSION, since it gives more information to
7635 backends. */
7636 if (set_src_cost (temp1, optimize_this_for_speed_p)
7637 < set_src_cost (temp, optimize_this_for_speed_p))
7638 temp = temp1;
7640 pos_rtx = temp;
7643 /* Make POS_RTX unless we already have it and it is correct. If we don't
7644 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7645 be a CONST_INT. */
7646 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7647 pos_rtx = orig_pos_rtx;
7649 else if (pos_rtx == 0)
7650 pos_rtx = GEN_INT (pos);
7652 /* Make the required operation. See if we can use existing rtx. */
7653 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7654 extraction_mode, inner, GEN_INT (len), pos_rtx);
7655 if (! in_dest)
7656 new_rtx = gen_lowpart (mode, new_rtx);
7658 return new_rtx;
7661 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7662 with any other operations in X. Return X without that shift if so. */
7664 static rtx
7665 extract_left_shift (rtx x, int count)
7667 enum rtx_code code = GET_CODE (x);
7668 machine_mode mode = GET_MODE (x);
7669 rtx tem;
7671 switch (code)
7673 case ASHIFT:
7674 /* This is the shift itself. If it is wide enough, we will return
7675 either the value being shifted if the shift count is equal to
7676 COUNT or a shift for the difference. */
7677 if (CONST_INT_P (XEXP (x, 1))
7678 && INTVAL (XEXP (x, 1)) >= count)
7679 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7680 INTVAL (XEXP (x, 1)) - count);
7681 break;
7683 case NEG: case NOT:
7684 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7685 return simplify_gen_unary (code, mode, tem, mode);
7687 break;
7689 case PLUS: case IOR: case XOR: case AND:
7690 /* If we can safely shift this constant and we find the inner shift,
7691 make a new operation. */
7692 if (CONST_INT_P (XEXP (x, 1))
7693 && (UINTVAL (XEXP (x, 1))
7694 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7695 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7697 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7698 return simplify_gen_binary (code, mode, tem,
7699 gen_int_mode (val, mode));
7701 break;
7703 default:
7704 break;
7707 return 0;
7710 /* Look at the expression rooted at X. Look for expressions
7711 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7712 Form these expressions.
7714 Return the new rtx, usually just X.
7716 Also, for machines like the VAX that don't have logical shift insns,
7717 try to convert logical to arithmetic shift operations in cases where
7718 they are equivalent. This undoes the canonicalizations to logical
7719 shifts done elsewhere.
7721 We try, as much as possible, to re-use rtl expressions to save memory.
7723 IN_CODE says what kind of expression we are processing. Normally, it is
7724 SET. In a memory address it is MEM. When processing the arguments of
7725 a comparison or a COMPARE against zero, it is COMPARE. */
7728 make_compound_operation (rtx x, enum rtx_code in_code)
7730 enum rtx_code code = GET_CODE (x);
7731 machine_mode mode = GET_MODE (x);
7732 int mode_width = GET_MODE_PRECISION (mode);
7733 rtx rhs, lhs;
7734 enum rtx_code next_code;
7735 int i, j;
7736 rtx new_rtx = 0;
7737 rtx tem;
7738 const char *fmt;
7740 /* Select the code to be used in recursive calls. Once we are inside an
7741 address, we stay there. If we have a comparison, set to COMPARE,
7742 but once inside, go back to our default of SET. */
7744 next_code = (code == MEM ? MEM
7745 : ((code == COMPARE || COMPARISON_P (x))
7746 && XEXP (x, 1) == const0_rtx) ? COMPARE
7747 : in_code == COMPARE ? SET : in_code);
7749 /* Process depending on the code of this operation. If NEW is set
7750 nonzero, it will be returned. */
7752 switch (code)
7754 case ASHIFT:
7755 /* Convert shifts by constants into multiplications if inside
7756 an address. */
7757 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7758 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7759 && INTVAL (XEXP (x, 1)) >= 0
7760 && SCALAR_INT_MODE_P (mode))
7762 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7763 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7765 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7766 if (GET_CODE (new_rtx) == NEG)
7768 new_rtx = XEXP (new_rtx, 0);
7769 multval = -multval;
7771 multval = trunc_int_for_mode (multval, mode);
7772 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7774 break;
7776 case PLUS:
7777 lhs = XEXP (x, 0);
7778 rhs = XEXP (x, 1);
7779 lhs = make_compound_operation (lhs, next_code);
7780 rhs = make_compound_operation (rhs, next_code);
7781 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7782 && SCALAR_INT_MODE_P (mode))
7784 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7785 XEXP (lhs, 1));
7786 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7788 else if (GET_CODE (lhs) == MULT
7789 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7791 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7792 simplify_gen_unary (NEG, mode,
7793 XEXP (lhs, 1),
7794 mode));
7795 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7797 else
7799 SUBST (XEXP (x, 0), lhs);
7800 SUBST (XEXP (x, 1), rhs);
7801 goto maybe_swap;
7803 x = gen_lowpart (mode, new_rtx);
7804 goto maybe_swap;
7806 case MINUS:
7807 lhs = XEXP (x, 0);
7808 rhs = XEXP (x, 1);
7809 lhs = make_compound_operation (lhs, next_code);
7810 rhs = make_compound_operation (rhs, next_code);
7811 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7812 && SCALAR_INT_MODE_P (mode))
7814 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7815 XEXP (rhs, 1));
7816 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7818 else if (GET_CODE (rhs) == MULT
7819 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7821 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7822 simplify_gen_unary (NEG, mode,
7823 XEXP (rhs, 1),
7824 mode));
7825 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7827 else
7829 SUBST (XEXP (x, 0), lhs);
7830 SUBST (XEXP (x, 1), rhs);
7831 return x;
7833 return gen_lowpart (mode, new_rtx);
7835 case AND:
7836 /* If the second operand is not a constant, we can't do anything
7837 with it. */
7838 if (!CONST_INT_P (XEXP (x, 1)))
7839 break;
7841 /* If the constant is a power of two minus one and the first operand
7842 is a logical right shift, make an extraction. */
7843 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7844 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7846 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7847 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7848 0, in_code == COMPARE);
7851 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7852 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7853 && subreg_lowpart_p (XEXP (x, 0))
7854 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7855 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7857 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7858 next_code);
7859 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7860 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7861 0, in_code == COMPARE);
7863 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7864 else if ((GET_CODE (XEXP (x, 0)) == XOR
7865 || GET_CODE (XEXP (x, 0)) == IOR)
7866 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7867 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7868 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7870 /* Apply the distributive law, and then try to make extractions. */
7871 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7872 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7873 XEXP (x, 1)),
7874 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7875 XEXP (x, 1)));
7876 new_rtx = make_compound_operation (new_rtx, in_code);
7879 /* If we are have (and (rotate X C) M) and C is larger than the number
7880 of bits in M, this is an extraction. */
7882 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7883 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7884 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7885 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7887 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7888 new_rtx = make_extraction (mode, new_rtx,
7889 (GET_MODE_PRECISION (mode)
7890 - INTVAL (XEXP (XEXP (x, 0), 1))),
7891 NULL_RTX, i, 1, 0, in_code == COMPARE);
7894 /* On machines without logical shifts, if the operand of the AND is
7895 a logical shift and our mask turns off all the propagated sign
7896 bits, we can replace the logical shift with an arithmetic shift. */
7897 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7898 && !have_insn_for (LSHIFTRT, mode)
7899 && have_insn_for (ASHIFTRT, mode)
7900 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7901 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7902 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7903 && mode_width <= HOST_BITS_PER_WIDE_INT)
7905 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7907 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7908 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7909 SUBST (XEXP (x, 0),
7910 gen_rtx_ASHIFTRT (mode,
7911 make_compound_operation
7912 (XEXP (XEXP (x, 0), 0), next_code),
7913 XEXP (XEXP (x, 0), 1)));
7916 /* If the constant is one less than a power of two, this might be
7917 representable by an extraction even if no shift is present.
7918 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7919 we are in a COMPARE. */
7920 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7921 new_rtx = make_extraction (mode,
7922 make_compound_operation (XEXP (x, 0),
7923 next_code),
7924 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7926 /* If we are in a comparison and this is an AND with a power of two,
7927 convert this into the appropriate bit extract. */
7928 else if (in_code == COMPARE
7929 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7930 new_rtx = make_extraction (mode,
7931 make_compound_operation (XEXP (x, 0),
7932 next_code),
7933 i, NULL_RTX, 1, 1, 0, 1);
7935 break;
7937 case LSHIFTRT:
7938 /* If the sign bit is known to be zero, replace this with an
7939 arithmetic shift. */
7940 if (have_insn_for (ASHIFTRT, mode)
7941 && ! have_insn_for (LSHIFTRT, mode)
7942 && mode_width <= HOST_BITS_PER_WIDE_INT
7943 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7945 new_rtx = gen_rtx_ASHIFTRT (mode,
7946 make_compound_operation (XEXP (x, 0),
7947 next_code),
7948 XEXP (x, 1));
7949 break;
7952 /* ... fall through ... */
7954 case ASHIFTRT:
7955 lhs = XEXP (x, 0);
7956 rhs = XEXP (x, 1);
7958 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7959 this is a SIGN_EXTRACT. */
7960 if (CONST_INT_P (rhs)
7961 && GET_CODE (lhs) == ASHIFT
7962 && CONST_INT_P (XEXP (lhs, 1))
7963 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7964 && INTVAL (XEXP (lhs, 1)) >= 0
7965 && INTVAL (rhs) < mode_width)
7967 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7968 new_rtx = make_extraction (mode, new_rtx,
7969 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7970 NULL_RTX, mode_width - INTVAL (rhs),
7971 code == LSHIFTRT, 0, in_code == COMPARE);
7972 break;
7975 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7976 If so, try to merge the shifts into a SIGN_EXTEND. We could
7977 also do this for some cases of SIGN_EXTRACT, but it doesn't
7978 seem worth the effort; the case checked for occurs on Alpha. */
7980 if (!OBJECT_P (lhs)
7981 && ! (GET_CODE (lhs) == SUBREG
7982 && (OBJECT_P (SUBREG_REG (lhs))))
7983 && CONST_INT_P (rhs)
7984 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7985 && INTVAL (rhs) < mode_width
7986 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7987 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7988 0, NULL_RTX, mode_width - INTVAL (rhs),
7989 code == LSHIFTRT, 0, in_code == COMPARE);
7991 break;
7993 case SUBREG:
7994 /* Call ourselves recursively on the inner expression. If we are
7995 narrowing the object and it has a different RTL code from
7996 what it originally did, do this SUBREG as a force_to_mode. */
7998 rtx inner = SUBREG_REG (x), simplified;
7999 enum rtx_code subreg_code = in_code;
8001 /* If in_code is COMPARE, it isn't always safe to pass it through
8002 to the recursive make_compound_operation call. */
8003 if (subreg_code == COMPARE
8004 && (!subreg_lowpart_p (x)
8005 || GET_CODE (inner) == SUBREG
8006 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8007 is (const_int 0), rather than
8008 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
8009 || (GET_CODE (inner) == AND
8010 && CONST_INT_P (XEXP (inner, 1))
8011 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8012 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8013 >= GET_MODE_BITSIZE (mode))))
8014 subreg_code = SET;
8016 tem = make_compound_operation (inner, subreg_code);
8018 simplified
8019 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8020 if (simplified)
8021 tem = simplified;
8023 if (GET_CODE (tem) != GET_CODE (inner)
8024 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8025 && subreg_lowpart_p (x))
8027 rtx newer
8028 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
8030 /* If we have something other than a SUBREG, we might have
8031 done an expansion, so rerun ourselves. */
8032 if (GET_CODE (newer) != SUBREG)
8033 newer = make_compound_operation (newer, in_code);
8035 /* force_to_mode can expand compounds. If it just re-expanded the
8036 compound, use gen_lowpart to convert to the desired mode. */
8037 if (rtx_equal_p (newer, x)
8038 /* Likewise if it re-expanded the compound only partially.
8039 This happens for SUBREG of ZERO_EXTRACT if they extract
8040 the same number of bits. */
8041 || (GET_CODE (newer) == SUBREG
8042 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8043 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8044 && GET_CODE (inner) == AND
8045 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8046 return gen_lowpart (GET_MODE (x), tem);
8048 return newer;
8051 if (simplified)
8052 return tem;
8054 break;
8056 default:
8057 break;
8060 if (new_rtx)
8062 x = gen_lowpart (mode, new_rtx);
8063 code = GET_CODE (x);
8066 /* Now recursively process each operand of this operation. We need to
8067 handle ZERO_EXTEND specially so that we don't lose track of the
8068 inner mode. */
8069 if (GET_CODE (x) == ZERO_EXTEND)
8071 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8072 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8073 new_rtx, GET_MODE (XEXP (x, 0)));
8074 if (tem)
8075 return tem;
8076 SUBST (XEXP (x, 0), new_rtx);
8077 return x;
8080 fmt = GET_RTX_FORMAT (code);
8081 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8082 if (fmt[i] == 'e')
8084 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8085 SUBST (XEXP (x, i), new_rtx);
8087 else if (fmt[i] == 'E')
8088 for (j = 0; j < XVECLEN (x, i); j++)
8090 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8091 SUBST (XVECEXP (x, i, j), new_rtx);
8094 maybe_swap:
8095 /* If this is a commutative operation, the changes to the operands
8096 may have made it noncanonical. */
8097 if (COMMUTATIVE_ARITH_P (x)
8098 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8100 tem = XEXP (x, 0);
8101 SUBST (XEXP (x, 0), XEXP (x, 1));
8102 SUBST (XEXP (x, 1), tem);
8105 return x;
8108 /* Given M see if it is a value that would select a field of bits
8109 within an item, but not the entire word. Return -1 if not.
8110 Otherwise, return the starting position of the field, where 0 is the
8111 low-order bit.
8113 *PLEN is set to the length of the field. */
8115 static int
8116 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8118 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8119 int pos = m ? ctz_hwi (m) : -1;
8120 int len = 0;
8122 if (pos >= 0)
8123 /* Now shift off the low-order zero bits and see if we have a
8124 power of two minus 1. */
8125 len = exact_log2 ((m >> pos) + 1);
8127 if (len <= 0)
8128 pos = -1;
8130 *plen = len;
8131 return pos;
8134 /* If X refers to a register that equals REG in value, replace these
8135 references with REG. */
8136 static rtx
8137 canon_reg_for_combine (rtx x, rtx reg)
8139 rtx op0, op1, op2;
8140 const char *fmt;
8141 int i;
8142 bool copied;
8144 enum rtx_code code = GET_CODE (x);
8145 switch (GET_RTX_CLASS (code))
8147 case RTX_UNARY:
8148 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8149 if (op0 != XEXP (x, 0))
8150 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8151 GET_MODE (reg));
8152 break;
8154 case RTX_BIN_ARITH:
8155 case RTX_COMM_ARITH:
8156 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8157 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8158 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8159 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8160 break;
8162 case RTX_COMPARE:
8163 case RTX_COMM_COMPARE:
8164 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8165 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8166 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8167 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8168 GET_MODE (op0), op0, op1);
8169 break;
8171 case RTX_TERNARY:
8172 case RTX_BITFIELD_OPS:
8173 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8174 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8175 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8176 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8177 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8178 GET_MODE (op0), op0, op1, op2);
8180 case RTX_OBJ:
8181 if (REG_P (x))
8183 if (rtx_equal_p (get_last_value (reg), x)
8184 || rtx_equal_p (reg, get_last_value (x)))
8185 return reg;
8186 else
8187 break;
8190 /* fall through */
8192 default:
8193 fmt = GET_RTX_FORMAT (code);
8194 copied = false;
8195 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8196 if (fmt[i] == 'e')
8198 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8199 if (op != XEXP (x, i))
8201 if (!copied)
8203 copied = true;
8204 x = copy_rtx (x);
8206 XEXP (x, i) = op;
8209 else if (fmt[i] == 'E')
8211 int j;
8212 for (j = 0; j < XVECLEN (x, i); j++)
8214 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8215 if (op != XVECEXP (x, i, j))
8217 if (!copied)
8219 copied = true;
8220 x = copy_rtx (x);
8222 XVECEXP (x, i, j) = op;
8227 break;
8230 return x;
8233 /* Return X converted to MODE. If the value is already truncated to
8234 MODE we can just return a subreg even though in the general case we
8235 would need an explicit truncation. */
8237 static rtx
8238 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8240 if (!CONST_INT_P (x)
8241 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8242 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8243 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8245 /* Bit-cast X into an integer mode. */
8246 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8247 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8248 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8249 x, GET_MODE (x));
8252 return gen_lowpart (mode, x);
8255 /* See if X can be simplified knowing that we will only refer to it in
8256 MODE and will only refer to those bits that are nonzero in MASK.
8257 If other bits are being computed or if masking operations are done
8258 that select a superset of the bits in MASK, they can sometimes be
8259 ignored.
8261 Return a possibly simplified expression, but always convert X to
8262 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8264 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8265 are all off in X. This is used when X will be complemented, by either
8266 NOT, NEG, or XOR. */
8268 static rtx
8269 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8270 int just_select)
8272 enum rtx_code code = GET_CODE (x);
8273 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8274 machine_mode op_mode;
8275 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8276 rtx op0, op1, temp;
8278 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8279 code below will do the wrong thing since the mode of such an
8280 expression is VOIDmode.
8282 Also do nothing if X is a CLOBBER; this can happen if X was
8283 the return value from a call to gen_lowpart. */
8284 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8285 return x;
8287 /* We want to perform the operation in its present mode unless we know
8288 that the operation is valid in MODE, in which case we do the operation
8289 in MODE. */
8290 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8291 && have_insn_for (code, mode))
8292 ? mode : GET_MODE (x));
8294 /* It is not valid to do a right-shift in a narrower mode
8295 than the one it came in with. */
8296 if ((code == LSHIFTRT || code == ASHIFTRT)
8297 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8298 op_mode = GET_MODE (x);
8300 /* Truncate MASK to fit OP_MODE. */
8301 if (op_mode)
8302 mask &= GET_MODE_MASK (op_mode);
8304 /* When we have an arithmetic operation, or a shift whose count we
8305 do not know, we need to assume that all bits up to the highest-order
8306 bit in MASK will be needed. This is how we form such a mask. */
8307 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8308 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8309 else
8310 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8311 - 1);
8313 /* Determine what bits of X are guaranteed to be (non)zero. */
8314 nonzero = nonzero_bits (x, mode);
8316 /* If none of the bits in X are needed, return a zero. */
8317 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8318 x = const0_rtx;
8320 /* If X is a CONST_INT, return a new one. Do this here since the
8321 test below will fail. */
8322 if (CONST_INT_P (x))
8324 if (SCALAR_INT_MODE_P (mode))
8325 return gen_int_mode (INTVAL (x) & mask, mode);
8326 else
8328 x = GEN_INT (INTVAL (x) & mask);
8329 return gen_lowpart_common (mode, x);
8333 /* If X is narrower than MODE and we want all the bits in X's mode, just
8334 get X in the proper mode. */
8335 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8336 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8337 return gen_lowpart (mode, x);
8339 /* We can ignore the effect of a SUBREG if it narrows the mode or
8340 if the constant masks to zero all the bits the mode doesn't have. */
8341 if (GET_CODE (x) == SUBREG
8342 && subreg_lowpart_p (x)
8343 && ((GET_MODE_SIZE (GET_MODE (x))
8344 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8345 || (0 == (mask
8346 & GET_MODE_MASK (GET_MODE (x))
8347 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8348 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8350 /* The arithmetic simplifications here only work for scalar integer modes. */
8351 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8352 return gen_lowpart_or_truncate (mode, x);
8354 switch (code)
8356 case CLOBBER:
8357 /* If X is a (clobber (const_int)), return it since we know we are
8358 generating something that won't match. */
8359 return x;
8361 case SIGN_EXTEND:
8362 case ZERO_EXTEND:
8363 case ZERO_EXTRACT:
8364 case SIGN_EXTRACT:
8365 x = expand_compound_operation (x);
8366 if (GET_CODE (x) != code)
8367 return force_to_mode (x, mode, mask, next_select);
8368 break;
8370 case TRUNCATE:
8371 /* Similarly for a truncate. */
8372 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8374 case AND:
8375 /* If this is an AND with a constant, convert it into an AND
8376 whose constant is the AND of that constant with MASK. If it
8377 remains an AND of MASK, delete it since it is redundant. */
8379 if (CONST_INT_P (XEXP (x, 1)))
8381 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8382 mask & INTVAL (XEXP (x, 1)));
8384 /* If X is still an AND, see if it is an AND with a mask that
8385 is just some low-order bits. If so, and it is MASK, we don't
8386 need it. */
8388 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8389 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8390 == mask))
8391 x = XEXP (x, 0);
8393 /* If it remains an AND, try making another AND with the bits
8394 in the mode mask that aren't in MASK turned on. If the
8395 constant in the AND is wide enough, this might make a
8396 cheaper constant. */
8398 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8399 && GET_MODE_MASK (GET_MODE (x)) != mask
8400 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8402 unsigned HOST_WIDE_INT cval
8403 = UINTVAL (XEXP (x, 1))
8404 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8405 rtx y;
8407 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8408 gen_int_mode (cval, GET_MODE (x)));
8409 if (set_src_cost (y, optimize_this_for_speed_p)
8410 < set_src_cost (x, optimize_this_for_speed_p))
8411 x = y;
8414 break;
8417 goto binop;
8419 case PLUS:
8420 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8421 low-order bits (as in an alignment operation) and FOO is already
8422 aligned to that boundary, mask C1 to that boundary as well.
8423 This may eliminate that PLUS and, later, the AND. */
8426 unsigned int width = GET_MODE_PRECISION (mode);
8427 unsigned HOST_WIDE_INT smask = mask;
8429 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8430 number, sign extend it. */
8432 if (width < HOST_BITS_PER_WIDE_INT
8433 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8434 smask |= HOST_WIDE_INT_M1U << width;
8436 if (CONST_INT_P (XEXP (x, 1))
8437 && exact_log2 (- smask) >= 0
8438 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8439 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8440 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8441 (INTVAL (XEXP (x, 1)) & smask)),
8442 mode, smask, next_select);
8445 /* ... fall through ... */
8447 case MULT:
8448 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8449 most significant bit in MASK since carries from those bits will
8450 affect the bits we are interested in. */
8451 mask = fuller_mask;
8452 goto binop;
8454 case MINUS:
8455 /* If X is (minus C Y) where C's least set bit is larger than any bit
8456 in the mask, then we may replace with (neg Y). */
8457 if (CONST_INT_P (XEXP (x, 0))
8458 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8460 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8461 GET_MODE (x));
8462 return force_to_mode (x, mode, mask, next_select);
8465 /* Similarly, if C contains every bit in the fuller_mask, then we may
8466 replace with (not Y). */
8467 if (CONST_INT_P (XEXP (x, 0))
8468 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8470 x = simplify_gen_unary (NOT, GET_MODE (x),
8471 XEXP (x, 1), GET_MODE (x));
8472 return force_to_mode (x, mode, mask, next_select);
8475 mask = fuller_mask;
8476 goto binop;
8478 case IOR:
8479 case XOR:
8480 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8481 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8482 operation which may be a bitfield extraction. Ensure that the
8483 constant we form is not wider than the mode of X. */
8485 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8486 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8487 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8488 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8489 && CONST_INT_P (XEXP (x, 1))
8490 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8491 + floor_log2 (INTVAL (XEXP (x, 1))))
8492 < GET_MODE_PRECISION (GET_MODE (x)))
8493 && (UINTVAL (XEXP (x, 1))
8494 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8496 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8497 << INTVAL (XEXP (XEXP (x, 0), 1)),
8498 GET_MODE (x));
8499 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8500 XEXP (XEXP (x, 0), 0), temp);
8501 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8502 XEXP (XEXP (x, 0), 1));
8503 return force_to_mode (x, mode, mask, next_select);
8506 binop:
8507 /* For most binary operations, just propagate into the operation and
8508 change the mode if we have an operation of that mode. */
8510 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8511 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8513 /* If we ended up truncating both operands, truncate the result of the
8514 operation instead. */
8515 if (GET_CODE (op0) == TRUNCATE
8516 && GET_CODE (op1) == TRUNCATE)
8518 op0 = XEXP (op0, 0);
8519 op1 = XEXP (op1, 0);
8522 op0 = gen_lowpart_or_truncate (op_mode, op0);
8523 op1 = gen_lowpart_or_truncate (op_mode, op1);
8525 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8526 x = simplify_gen_binary (code, op_mode, op0, op1);
8527 break;
8529 case ASHIFT:
8530 /* For left shifts, do the same, but just for the first operand.
8531 However, we cannot do anything with shifts where we cannot
8532 guarantee that the counts are smaller than the size of the mode
8533 because such a count will have a different meaning in a
8534 wider mode. */
8536 if (! (CONST_INT_P (XEXP (x, 1))
8537 && INTVAL (XEXP (x, 1)) >= 0
8538 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8539 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8540 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8541 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8542 break;
8544 /* If the shift count is a constant and we can do arithmetic in
8545 the mode of the shift, refine which bits we need. Otherwise, use the
8546 conservative form of the mask. */
8547 if (CONST_INT_P (XEXP (x, 1))
8548 && INTVAL (XEXP (x, 1)) >= 0
8549 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8550 && HWI_COMPUTABLE_MODE_P (op_mode))
8551 mask >>= INTVAL (XEXP (x, 1));
8552 else
8553 mask = fuller_mask;
8555 op0 = gen_lowpart_or_truncate (op_mode,
8556 force_to_mode (XEXP (x, 0), op_mode,
8557 mask, next_select));
8559 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8560 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8561 break;
8563 case LSHIFTRT:
8564 /* Here we can only do something if the shift count is a constant,
8565 this shift constant is valid for the host, and we can do arithmetic
8566 in OP_MODE. */
8568 if (CONST_INT_P (XEXP (x, 1))
8569 && INTVAL (XEXP (x, 1)) >= 0
8570 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8571 && HWI_COMPUTABLE_MODE_P (op_mode))
8573 rtx inner = XEXP (x, 0);
8574 unsigned HOST_WIDE_INT inner_mask;
8576 /* Select the mask of the bits we need for the shift operand. */
8577 inner_mask = mask << INTVAL (XEXP (x, 1));
8579 /* We can only change the mode of the shift if we can do arithmetic
8580 in the mode of the shift and INNER_MASK is no wider than the
8581 width of X's mode. */
8582 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8583 op_mode = GET_MODE (x);
8585 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8587 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8588 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8591 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8592 shift and AND produces only copies of the sign bit (C2 is one less
8593 than a power of two), we can do this with just a shift. */
8595 if (GET_CODE (x) == LSHIFTRT
8596 && CONST_INT_P (XEXP (x, 1))
8597 /* The shift puts one of the sign bit copies in the least significant
8598 bit. */
8599 && ((INTVAL (XEXP (x, 1))
8600 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8601 >= GET_MODE_PRECISION (GET_MODE (x)))
8602 && exact_log2 (mask + 1) >= 0
8603 /* Number of bits left after the shift must be more than the mask
8604 needs. */
8605 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8606 <= GET_MODE_PRECISION (GET_MODE (x)))
8607 /* Must be more sign bit copies than the mask needs. */
8608 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8609 >= exact_log2 (mask + 1)))
8610 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8611 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8612 - exact_log2 (mask + 1)));
8614 goto shiftrt;
8616 case ASHIFTRT:
8617 /* If we are just looking for the sign bit, we don't need this shift at
8618 all, even if it has a variable count. */
8619 if (val_signbit_p (GET_MODE (x), mask))
8620 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8622 /* If this is a shift by a constant, get a mask that contains those bits
8623 that are not copies of the sign bit. We then have two cases: If
8624 MASK only includes those bits, this can be a logical shift, which may
8625 allow simplifications. If MASK is a single-bit field not within
8626 those bits, we are requesting a copy of the sign bit and hence can
8627 shift the sign bit to the appropriate location. */
8629 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8630 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8632 int i;
8634 /* If the considered data is wider than HOST_WIDE_INT, we can't
8635 represent a mask for all its bits in a single scalar.
8636 But we only care about the lower bits, so calculate these. */
8638 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8640 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8642 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8643 is the number of bits a full-width mask would have set.
8644 We need only shift if these are fewer than nonzero can
8645 hold. If not, we must keep all bits set in nonzero. */
8647 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8648 < HOST_BITS_PER_WIDE_INT)
8649 nonzero >>= INTVAL (XEXP (x, 1))
8650 + HOST_BITS_PER_WIDE_INT
8651 - GET_MODE_PRECISION (GET_MODE (x)) ;
8653 else
8655 nonzero = GET_MODE_MASK (GET_MODE (x));
8656 nonzero >>= INTVAL (XEXP (x, 1));
8659 if ((mask & ~nonzero) == 0)
8661 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8662 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8663 if (GET_CODE (x) != ASHIFTRT)
8664 return force_to_mode (x, mode, mask, next_select);
8667 else if ((i = exact_log2 (mask)) >= 0)
8669 x = simplify_shift_const
8670 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8671 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8673 if (GET_CODE (x) != ASHIFTRT)
8674 return force_to_mode (x, mode, mask, next_select);
8678 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8679 even if the shift count isn't a constant. */
8680 if (mask == 1)
8681 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8682 XEXP (x, 0), XEXP (x, 1));
8684 shiftrt:
8686 /* If this is a zero- or sign-extension operation that just affects bits
8687 we don't care about, remove it. Be sure the call above returned
8688 something that is still a shift. */
8690 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8691 && CONST_INT_P (XEXP (x, 1))
8692 && INTVAL (XEXP (x, 1)) >= 0
8693 && (INTVAL (XEXP (x, 1))
8694 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8695 && GET_CODE (XEXP (x, 0)) == ASHIFT
8696 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8697 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8698 next_select);
8700 break;
8702 case ROTATE:
8703 case ROTATERT:
8704 /* If the shift count is constant and we can do computations
8705 in the mode of X, compute where the bits we care about are.
8706 Otherwise, we can't do anything. Don't change the mode of
8707 the shift or propagate MODE into the shift, though. */
8708 if (CONST_INT_P (XEXP (x, 1))
8709 && INTVAL (XEXP (x, 1)) >= 0)
8711 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8712 GET_MODE (x),
8713 gen_int_mode (mask, GET_MODE (x)),
8714 XEXP (x, 1));
8715 if (temp && CONST_INT_P (temp))
8716 x = simplify_gen_binary (code, GET_MODE (x),
8717 force_to_mode (XEXP (x, 0), GET_MODE (x),
8718 INTVAL (temp), next_select),
8719 XEXP (x, 1));
8721 break;
8723 case NEG:
8724 /* If we just want the low-order bit, the NEG isn't needed since it
8725 won't change the low-order bit. */
8726 if (mask == 1)
8727 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8729 /* We need any bits less significant than the most significant bit in
8730 MASK since carries from those bits will affect the bits we are
8731 interested in. */
8732 mask = fuller_mask;
8733 goto unop;
8735 case NOT:
8736 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8737 same as the XOR case above. Ensure that the constant we form is not
8738 wider than the mode of X. */
8740 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8741 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8742 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8743 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8744 < GET_MODE_PRECISION (GET_MODE (x)))
8745 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8747 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8748 GET_MODE (x));
8749 temp = simplify_gen_binary (XOR, GET_MODE (x),
8750 XEXP (XEXP (x, 0), 0), temp);
8751 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8752 temp, XEXP (XEXP (x, 0), 1));
8754 return force_to_mode (x, mode, mask, next_select);
8757 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8758 use the full mask inside the NOT. */
8759 mask = fuller_mask;
8761 unop:
8762 op0 = gen_lowpart_or_truncate (op_mode,
8763 force_to_mode (XEXP (x, 0), mode, mask,
8764 next_select));
8765 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8766 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8767 break;
8769 case NE:
8770 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8771 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8772 which is equal to STORE_FLAG_VALUE. */
8773 if ((mask & ~STORE_FLAG_VALUE) == 0
8774 && XEXP (x, 1) == const0_rtx
8775 && GET_MODE (XEXP (x, 0)) == mode
8776 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8777 && (nonzero_bits (XEXP (x, 0), mode)
8778 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8779 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8781 break;
8783 case IF_THEN_ELSE:
8784 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8785 written in a narrower mode. We play it safe and do not do so. */
8787 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8788 force_to_mode (XEXP (x, 1), mode,
8789 mask, next_select));
8790 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8791 force_to_mode (XEXP (x, 2), mode,
8792 mask, next_select));
8793 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8794 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8795 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8796 op0, op1);
8797 break;
8799 default:
8800 break;
8803 /* Ensure we return a value of the proper mode. */
8804 return gen_lowpart_or_truncate (mode, x);
8807 /* Return nonzero if X is an expression that has one of two values depending on
8808 whether some other value is zero or nonzero. In that case, we return the
8809 value that is being tested, *PTRUE is set to the value if the rtx being
8810 returned has a nonzero value, and *PFALSE is set to the other alternative.
8812 If we return zero, we set *PTRUE and *PFALSE to X. */
8814 static rtx
8815 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8817 machine_mode mode = GET_MODE (x);
8818 enum rtx_code code = GET_CODE (x);
8819 rtx cond0, cond1, true0, true1, false0, false1;
8820 unsigned HOST_WIDE_INT nz;
8822 /* If we are comparing a value against zero, we are done. */
8823 if ((code == NE || code == EQ)
8824 && XEXP (x, 1) == const0_rtx)
8826 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8827 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8828 return XEXP (x, 0);
8831 /* If this is a unary operation whose operand has one of two values, apply
8832 our opcode to compute those values. */
8833 else if (UNARY_P (x)
8834 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8836 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8837 *pfalse = simplify_gen_unary (code, mode, false0,
8838 GET_MODE (XEXP (x, 0)));
8839 return cond0;
8842 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8843 make can't possibly match and would suppress other optimizations. */
8844 else if (code == COMPARE)
8847 /* If this is a binary operation, see if either side has only one of two
8848 values. If either one does or if both do and they are conditional on
8849 the same value, compute the new true and false values. */
8850 else if (BINARY_P (x))
8852 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8853 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8855 if ((cond0 != 0 || cond1 != 0)
8856 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8858 /* If if_then_else_cond returned zero, then true/false are the
8859 same rtl. We must copy one of them to prevent invalid rtl
8860 sharing. */
8861 if (cond0 == 0)
8862 true0 = copy_rtx (true0);
8863 else if (cond1 == 0)
8864 true1 = copy_rtx (true1);
8866 if (COMPARISON_P (x))
8868 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8869 true0, true1);
8870 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8871 false0, false1);
8873 else
8875 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8876 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8879 return cond0 ? cond0 : cond1;
8882 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8883 operands is zero when the other is nonzero, and vice-versa,
8884 and STORE_FLAG_VALUE is 1 or -1. */
8886 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8887 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8888 || code == UMAX)
8889 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8891 rtx op0 = XEXP (XEXP (x, 0), 1);
8892 rtx op1 = XEXP (XEXP (x, 1), 1);
8894 cond0 = XEXP (XEXP (x, 0), 0);
8895 cond1 = XEXP (XEXP (x, 1), 0);
8897 if (COMPARISON_P (cond0)
8898 && COMPARISON_P (cond1)
8899 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8900 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8901 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8902 || ((swap_condition (GET_CODE (cond0))
8903 == reversed_comparison_code (cond1, NULL))
8904 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8905 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8906 && ! side_effects_p (x))
8908 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8909 *pfalse = simplify_gen_binary (MULT, mode,
8910 (code == MINUS
8911 ? simplify_gen_unary (NEG, mode,
8912 op1, mode)
8913 : op1),
8914 const_true_rtx);
8915 return cond0;
8919 /* Similarly for MULT, AND and UMIN, except that for these the result
8920 is always zero. */
8921 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8922 && (code == MULT || code == AND || code == UMIN)
8923 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8925 cond0 = XEXP (XEXP (x, 0), 0);
8926 cond1 = XEXP (XEXP (x, 1), 0);
8928 if (COMPARISON_P (cond0)
8929 && COMPARISON_P (cond1)
8930 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8931 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8932 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8933 || ((swap_condition (GET_CODE (cond0))
8934 == reversed_comparison_code (cond1, NULL))
8935 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8936 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8937 && ! side_effects_p (x))
8939 *ptrue = *pfalse = const0_rtx;
8940 return cond0;
8945 else if (code == IF_THEN_ELSE)
8947 /* If we have IF_THEN_ELSE already, extract the condition and
8948 canonicalize it if it is NE or EQ. */
8949 cond0 = XEXP (x, 0);
8950 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8951 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8952 return XEXP (cond0, 0);
8953 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8955 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8956 return XEXP (cond0, 0);
8958 else
8959 return cond0;
8962 /* If X is a SUBREG, we can narrow both the true and false values
8963 if the inner expression, if there is a condition. */
8964 else if (code == SUBREG
8965 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8966 &true0, &false0)))
8968 true0 = simplify_gen_subreg (mode, true0,
8969 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8970 false0 = simplify_gen_subreg (mode, false0,
8971 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8972 if (true0 && false0)
8974 *ptrue = true0;
8975 *pfalse = false0;
8976 return cond0;
8980 /* If X is a constant, this isn't special and will cause confusions
8981 if we treat it as such. Likewise if it is equivalent to a constant. */
8982 else if (CONSTANT_P (x)
8983 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8986 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8987 will be least confusing to the rest of the compiler. */
8988 else if (mode == BImode)
8990 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8991 return x;
8994 /* If X is known to be either 0 or -1, those are the true and
8995 false values when testing X. */
8996 else if (x == constm1_rtx || x == const0_rtx
8997 || (mode != VOIDmode
8998 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
9000 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9001 return x;
9004 /* Likewise for 0 or a single bit. */
9005 else if (HWI_COMPUTABLE_MODE_P (mode)
9006 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
9008 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9009 return x;
9012 /* Otherwise fail; show no condition with true and false values the same. */
9013 *ptrue = *pfalse = x;
9014 return 0;
9017 /* Return the value of expression X given the fact that condition COND
9018 is known to be true when applied to REG as its first operand and VAL
9019 as its second. X is known to not be shared and so can be modified in
9020 place.
9022 We only handle the simplest cases, and specifically those cases that
9023 arise with IF_THEN_ELSE expressions. */
9025 static rtx
9026 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9028 enum rtx_code code = GET_CODE (x);
9029 const char *fmt;
9030 int i, j;
9032 if (side_effects_p (x))
9033 return x;
9035 /* If either operand of the condition is a floating point value,
9036 then we have to avoid collapsing an EQ comparison. */
9037 if (cond == EQ
9038 && rtx_equal_p (x, reg)
9039 && ! FLOAT_MODE_P (GET_MODE (x))
9040 && ! FLOAT_MODE_P (GET_MODE (val)))
9041 return val;
9043 if (cond == UNEQ && rtx_equal_p (x, reg))
9044 return val;
9046 /* If X is (abs REG) and we know something about REG's relationship
9047 with zero, we may be able to simplify this. */
9049 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9050 switch (cond)
9052 case GE: case GT: case EQ:
9053 return XEXP (x, 0);
9054 case LT: case LE:
9055 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9056 XEXP (x, 0),
9057 GET_MODE (XEXP (x, 0)));
9058 default:
9059 break;
9062 /* The only other cases we handle are MIN, MAX, and comparisons if the
9063 operands are the same as REG and VAL. */
9065 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9067 if (rtx_equal_p (XEXP (x, 0), val))
9069 std::swap (val, reg);
9070 cond = swap_condition (cond);
9073 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9075 if (COMPARISON_P (x))
9077 if (comparison_dominates_p (cond, code))
9078 return const_true_rtx;
9080 code = reversed_comparison_code (x, NULL);
9081 if (code != UNKNOWN
9082 && comparison_dominates_p (cond, code))
9083 return const0_rtx;
9084 else
9085 return x;
9087 else if (code == SMAX || code == SMIN
9088 || code == UMIN || code == UMAX)
9090 int unsignedp = (code == UMIN || code == UMAX);
9092 /* Do not reverse the condition when it is NE or EQ.
9093 This is because we cannot conclude anything about
9094 the value of 'SMAX (x, y)' when x is not equal to y,
9095 but we can when x equals y. */
9096 if ((code == SMAX || code == UMAX)
9097 && ! (cond == EQ || cond == NE))
9098 cond = reverse_condition (cond);
9100 switch (cond)
9102 case GE: case GT:
9103 return unsignedp ? x : XEXP (x, 1);
9104 case LE: case LT:
9105 return unsignedp ? x : XEXP (x, 0);
9106 case GEU: case GTU:
9107 return unsignedp ? XEXP (x, 1) : x;
9108 case LEU: case LTU:
9109 return unsignedp ? XEXP (x, 0) : x;
9110 default:
9111 break;
9116 else if (code == SUBREG)
9118 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9119 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9121 if (SUBREG_REG (x) != r)
9123 /* We must simplify subreg here, before we lose track of the
9124 original inner_mode. */
9125 new_rtx = simplify_subreg (GET_MODE (x), r,
9126 inner_mode, SUBREG_BYTE (x));
9127 if (new_rtx)
9128 return new_rtx;
9129 else
9130 SUBST (SUBREG_REG (x), r);
9133 return x;
9135 /* We don't have to handle SIGN_EXTEND here, because even in the
9136 case of replacing something with a modeless CONST_INT, a
9137 CONST_INT is already (supposed to be) a valid sign extension for
9138 its narrower mode, which implies it's already properly
9139 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9140 story is different. */
9141 else if (code == ZERO_EXTEND)
9143 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9144 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9146 if (XEXP (x, 0) != r)
9148 /* We must simplify the zero_extend here, before we lose
9149 track of the original inner_mode. */
9150 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9151 r, inner_mode);
9152 if (new_rtx)
9153 return new_rtx;
9154 else
9155 SUBST (XEXP (x, 0), r);
9158 return x;
9161 fmt = GET_RTX_FORMAT (code);
9162 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9164 if (fmt[i] == 'e')
9165 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9166 else if (fmt[i] == 'E')
9167 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9168 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9169 cond, reg, val));
9172 return x;
9175 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9176 assignment as a field assignment. */
9178 static int
9179 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9181 if (widen_x && GET_MODE (x) != GET_MODE (y))
9183 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (y)))
9184 return 0;
9185 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9186 return 0;
9187 /* For big endian, adjust the memory offset. */
9188 if (BYTES_BIG_ENDIAN)
9189 x = adjust_address_nv (x, GET_MODE (y),
9190 -subreg_lowpart_offset (GET_MODE (x),
9191 GET_MODE (y)));
9192 else
9193 x = adjust_address_nv (x, GET_MODE (y), 0);
9196 if (x == y || rtx_equal_p (x, y))
9197 return 1;
9199 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9200 return 0;
9202 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9203 Note that all SUBREGs of MEM are paradoxical; otherwise they
9204 would have been rewritten. */
9205 if (MEM_P (x) && GET_CODE (y) == SUBREG
9206 && MEM_P (SUBREG_REG (y))
9207 && rtx_equal_p (SUBREG_REG (y),
9208 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9209 return 1;
9211 if (MEM_P (y) && GET_CODE (x) == SUBREG
9212 && MEM_P (SUBREG_REG (x))
9213 && rtx_equal_p (SUBREG_REG (x),
9214 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9215 return 1;
9217 /* We used to see if get_last_value of X and Y were the same but that's
9218 not correct. In one direction, we'll cause the assignment to have
9219 the wrong destination and in the case, we'll import a register into this
9220 insn that might have already have been dead. So fail if none of the
9221 above cases are true. */
9222 return 0;
9225 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9226 Return that assignment if so.
9228 We only handle the most common cases. */
9230 static rtx
9231 make_field_assignment (rtx x)
9233 rtx dest = SET_DEST (x);
9234 rtx src = SET_SRC (x);
9235 rtx assign;
9236 rtx rhs, lhs;
9237 HOST_WIDE_INT c1;
9238 HOST_WIDE_INT pos;
9239 unsigned HOST_WIDE_INT len;
9240 rtx other;
9241 machine_mode mode;
9243 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9244 a clear of a one-bit field. We will have changed it to
9245 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9246 for a SUBREG. */
9248 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9249 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9250 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9251 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9253 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9254 1, 1, 1, 0);
9255 if (assign != 0)
9256 return gen_rtx_SET (assign, const0_rtx);
9257 return x;
9260 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9261 && subreg_lowpart_p (XEXP (src, 0))
9262 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9263 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9264 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9265 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9266 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9267 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9269 assign = make_extraction (VOIDmode, dest, 0,
9270 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9271 1, 1, 1, 0);
9272 if (assign != 0)
9273 return gen_rtx_SET (assign, const0_rtx);
9274 return x;
9277 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9278 one-bit field. */
9279 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9280 && XEXP (XEXP (src, 0), 0) == const1_rtx
9281 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9283 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9284 1, 1, 1, 0);
9285 if (assign != 0)
9286 return gen_rtx_SET (assign, const1_rtx);
9287 return x;
9290 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9291 SRC is an AND with all bits of that field set, then we can discard
9292 the AND. */
9293 if (GET_CODE (dest) == ZERO_EXTRACT
9294 && CONST_INT_P (XEXP (dest, 1))
9295 && GET_CODE (src) == AND
9296 && CONST_INT_P (XEXP (src, 1)))
9298 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9299 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9300 unsigned HOST_WIDE_INT ze_mask;
9302 if (width >= HOST_BITS_PER_WIDE_INT)
9303 ze_mask = -1;
9304 else
9305 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9307 /* Complete overlap. We can remove the source AND. */
9308 if ((and_mask & ze_mask) == ze_mask)
9309 return gen_rtx_SET (dest, XEXP (src, 0));
9311 /* Partial overlap. We can reduce the source AND. */
9312 if ((and_mask & ze_mask) != and_mask)
9314 mode = GET_MODE (src);
9315 src = gen_rtx_AND (mode, XEXP (src, 0),
9316 gen_int_mode (and_mask & ze_mask, mode));
9317 return gen_rtx_SET (dest, src);
9321 /* The other case we handle is assignments into a constant-position
9322 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9323 a mask that has all one bits except for a group of zero bits and
9324 OTHER is known to have zeros where C1 has ones, this is such an
9325 assignment. Compute the position and length from C1. Shift OTHER
9326 to the appropriate position, force it to the required mode, and
9327 make the extraction. Check for the AND in both operands. */
9329 /* One or more SUBREGs might obscure the constant-position field
9330 assignment. The first one we are likely to encounter is an outer
9331 narrowing SUBREG, which we can just strip for the purposes of
9332 identifying the constant-field assignment. */
9333 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src))
9334 src = SUBREG_REG (src);
9336 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9337 return x;
9339 rhs = expand_compound_operation (XEXP (src, 0));
9340 lhs = expand_compound_operation (XEXP (src, 1));
9342 if (GET_CODE (rhs) == AND
9343 && CONST_INT_P (XEXP (rhs, 1))
9344 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9345 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9346 /* The second SUBREG that might get in the way is a paradoxical
9347 SUBREG around the first operand of the AND. We want to
9348 pretend the operand is as wide as the destination here. We
9349 do this by adjusting the MEM to wider mode for the sole
9350 purpose of the call to rtx_equal_for_field_assignment_p. Also
9351 note this trick only works for MEMs. */
9352 else if (GET_CODE (rhs) == AND
9353 && paradoxical_subreg_p (XEXP (rhs, 0))
9354 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9355 && CONST_INT_P (XEXP (rhs, 1))
9356 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9357 dest, true))
9358 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9359 else if (GET_CODE (lhs) == AND
9360 && CONST_INT_P (XEXP (lhs, 1))
9361 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9362 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9363 /* The second SUBREG that might get in the way is a paradoxical
9364 SUBREG around the first operand of the AND. We want to
9365 pretend the operand is as wide as the destination here. We
9366 do this by adjusting the MEM to wider mode for the sole
9367 purpose of the call to rtx_equal_for_field_assignment_p. Also
9368 note this trick only works for MEMs. */
9369 else if (GET_CODE (lhs) == AND
9370 && paradoxical_subreg_p (XEXP (lhs, 0))
9371 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9372 && CONST_INT_P (XEXP (lhs, 1))
9373 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9374 dest, true))
9375 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9376 else
9377 return x;
9379 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9380 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9381 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9382 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9383 return x;
9385 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9386 if (assign == 0)
9387 return x;
9389 /* The mode to use for the source is the mode of the assignment, or of
9390 what is inside a possible STRICT_LOW_PART. */
9391 mode = (GET_CODE (assign) == STRICT_LOW_PART
9392 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9394 /* Shift OTHER right POS places and make it the source, restricting it
9395 to the proper length and mode. */
9397 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9398 GET_MODE (src),
9399 other, pos),
9400 dest);
9401 src = force_to_mode (src, mode,
9402 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9403 ? ~(unsigned HOST_WIDE_INT) 0
9404 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9407 /* If SRC is masked by an AND that does not make a difference in
9408 the value being stored, strip it. */
9409 if (GET_CODE (assign) == ZERO_EXTRACT
9410 && CONST_INT_P (XEXP (assign, 1))
9411 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9412 && GET_CODE (src) == AND
9413 && CONST_INT_P (XEXP (src, 1))
9414 && UINTVAL (XEXP (src, 1))
9415 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9416 src = XEXP (src, 0);
9418 return gen_rtx_SET (assign, src);
9421 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9422 if so. */
9424 static rtx
9425 apply_distributive_law (rtx x)
9427 enum rtx_code code = GET_CODE (x);
9428 enum rtx_code inner_code;
9429 rtx lhs, rhs, other;
9430 rtx tem;
9432 /* Distributivity is not true for floating point as it can change the
9433 value. So we don't do it unless -funsafe-math-optimizations. */
9434 if (FLOAT_MODE_P (GET_MODE (x))
9435 && ! flag_unsafe_math_optimizations)
9436 return x;
9438 /* The outer operation can only be one of the following: */
9439 if (code != IOR && code != AND && code != XOR
9440 && code != PLUS && code != MINUS)
9441 return x;
9443 lhs = XEXP (x, 0);
9444 rhs = XEXP (x, 1);
9446 /* If either operand is a primitive we can't do anything, so get out
9447 fast. */
9448 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9449 return x;
9451 lhs = expand_compound_operation (lhs);
9452 rhs = expand_compound_operation (rhs);
9453 inner_code = GET_CODE (lhs);
9454 if (inner_code != GET_CODE (rhs))
9455 return x;
9457 /* See if the inner and outer operations distribute. */
9458 switch (inner_code)
9460 case LSHIFTRT:
9461 case ASHIFTRT:
9462 case AND:
9463 case IOR:
9464 /* These all distribute except over PLUS. */
9465 if (code == PLUS || code == MINUS)
9466 return x;
9467 break;
9469 case MULT:
9470 if (code != PLUS && code != MINUS)
9471 return x;
9472 break;
9474 case ASHIFT:
9475 /* This is also a multiply, so it distributes over everything. */
9476 break;
9478 /* This used to handle SUBREG, but this turned out to be counter-
9479 productive, since (subreg (op ...)) usually is not handled by
9480 insn patterns, and this "optimization" therefore transformed
9481 recognizable patterns into unrecognizable ones. Therefore the
9482 SUBREG case was removed from here.
9484 It is possible that distributing SUBREG over arithmetic operations
9485 leads to an intermediate result than can then be optimized further,
9486 e.g. by moving the outer SUBREG to the other side of a SET as done
9487 in simplify_set. This seems to have been the original intent of
9488 handling SUBREGs here.
9490 However, with current GCC this does not appear to actually happen,
9491 at least on major platforms. If some case is found where removing
9492 the SUBREG case here prevents follow-on optimizations, distributing
9493 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9495 default:
9496 return x;
9499 /* Set LHS and RHS to the inner operands (A and B in the example
9500 above) and set OTHER to the common operand (C in the example).
9501 There is only one way to do this unless the inner operation is
9502 commutative. */
9503 if (COMMUTATIVE_ARITH_P (lhs)
9504 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9505 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9506 else if (COMMUTATIVE_ARITH_P (lhs)
9507 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9508 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9509 else if (COMMUTATIVE_ARITH_P (lhs)
9510 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9511 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9512 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9513 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9514 else
9515 return x;
9517 /* Form the new inner operation, seeing if it simplifies first. */
9518 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9520 /* There is one exception to the general way of distributing:
9521 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9522 if (code == XOR && inner_code == IOR)
9524 inner_code = AND;
9525 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9528 /* We may be able to continuing distributing the result, so call
9529 ourselves recursively on the inner operation before forming the
9530 outer operation, which we return. */
9531 return simplify_gen_binary (inner_code, GET_MODE (x),
9532 apply_distributive_law (tem), other);
9535 /* See if X is of the form (* (+ A B) C), and if so convert to
9536 (+ (* A C) (* B C)) and try to simplify.
9538 Most of the time, this results in no change. However, if some of
9539 the operands are the same or inverses of each other, simplifications
9540 will result.
9542 For example, (and (ior A B) (not B)) can occur as the result of
9543 expanding a bit field assignment. When we apply the distributive
9544 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9545 which then simplifies to (and (A (not B))).
9547 Note that no checks happen on the validity of applying the inverse
9548 distributive law. This is pointless since we can do it in the
9549 few places where this routine is called.
9551 N is the index of the term that is decomposed (the arithmetic operation,
9552 i.e. (+ A B) in the first example above). !N is the index of the term that
9553 is distributed, i.e. of C in the first example above. */
9554 static rtx
9555 distribute_and_simplify_rtx (rtx x, int n)
9557 machine_mode mode;
9558 enum rtx_code outer_code, inner_code;
9559 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9561 /* Distributivity is not true for floating point as it can change the
9562 value. So we don't do it unless -funsafe-math-optimizations. */
9563 if (FLOAT_MODE_P (GET_MODE (x))
9564 && ! flag_unsafe_math_optimizations)
9565 return NULL_RTX;
9567 decomposed = XEXP (x, n);
9568 if (!ARITHMETIC_P (decomposed))
9569 return NULL_RTX;
9571 mode = GET_MODE (x);
9572 outer_code = GET_CODE (x);
9573 distributed = XEXP (x, !n);
9575 inner_code = GET_CODE (decomposed);
9576 inner_op0 = XEXP (decomposed, 0);
9577 inner_op1 = XEXP (decomposed, 1);
9579 /* Special case (and (xor B C) (not A)), which is equivalent to
9580 (xor (ior A B) (ior A C)) */
9581 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9583 distributed = XEXP (distributed, 0);
9584 outer_code = IOR;
9587 if (n == 0)
9589 /* Distribute the second term. */
9590 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9591 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9593 else
9595 /* Distribute the first term. */
9596 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9597 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9600 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9601 new_op0, new_op1));
9602 if (GET_CODE (tmp) != outer_code
9603 && (set_src_cost (tmp, optimize_this_for_speed_p)
9604 < set_src_cost (x, optimize_this_for_speed_p)))
9605 return tmp;
9607 return NULL_RTX;
9610 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9611 in MODE. Return an equivalent form, if different from (and VAROP
9612 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9614 static rtx
9615 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9616 unsigned HOST_WIDE_INT constop)
9618 unsigned HOST_WIDE_INT nonzero;
9619 unsigned HOST_WIDE_INT orig_constop;
9620 rtx orig_varop;
9621 int i;
9623 orig_varop = varop;
9624 orig_constop = constop;
9625 if (GET_CODE (varop) == CLOBBER)
9626 return NULL_RTX;
9628 /* Simplify VAROP knowing that we will be only looking at some of the
9629 bits in it.
9631 Note by passing in CONSTOP, we guarantee that the bits not set in
9632 CONSTOP are not significant and will never be examined. We must
9633 ensure that is the case by explicitly masking out those bits
9634 before returning. */
9635 varop = force_to_mode (varop, mode, constop, 0);
9637 /* If VAROP is a CLOBBER, we will fail so return it. */
9638 if (GET_CODE (varop) == CLOBBER)
9639 return varop;
9641 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9642 to VAROP and return the new constant. */
9643 if (CONST_INT_P (varop))
9644 return gen_int_mode (INTVAL (varop) & constop, mode);
9646 /* See what bits may be nonzero in VAROP. Unlike the general case of
9647 a call to nonzero_bits, here we don't care about bits outside
9648 MODE. */
9650 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9652 /* Turn off all bits in the constant that are known to already be zero.
9653 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9654 which is tested below. */
9656 constop &= nonzero;
9658 /* If we don't have any bits left, return zero. */
9659 if (constop == 0)
9660 return const0_rtx;
9662 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9663 a power of two, we can replace this with an ASHIFT. */
9664 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9665 && (i = exact_log2 (constop)) >= 0)
9666 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9668 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9669 or XOR, then try to apply the distributive law. This may eliminate
9670 operations if either branch can be simplified because of the AND.
9671 It may also make some cases more complex, but those cases probably
9672 won't match a pattern either with or without this. */
9674 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9675 return
9676 gen_lowpart
9677 (mode,
9678 apply_distributive_law
9679 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9680 simplify_and_const_int (NULL_RTX,
9681 GET_MODE (varop),
9682 XEXP (varop, 0),
9683 constop),
9684 simplify_and_const_int (NULL_RTX,
9685 GET_MODE (varop),
9686 XEXP (varop, 1),
9687 constop))));
9689 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9690 the AND and see if one of the operands simplifies to zero. If so, we
9691 may eliminate it. */
9693 if (GET_CODE (varop) == PLUS
9694 && exact_log2 (constop + 1) >= 0)
9696 rtx o0, o1;
9698 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9699 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9700 if (o0 == const0_rtx)
9701 return o1;
9702 if (o1 == const0_rtx)
9703 return o0;
9706 /* Make a SUBREG if necessary. If we can't make it, fail. */
9707 varop = gen_lowpart (mode, varop);
9708 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9709 return NULL_RTX;
9711 /* If we are only masking insignificant bits, return VAROP. */
9712 if (constop == nonzero)
9713 return varop;
9715 if (varop == orig_varop && constop == orig_constop)
9716 return NULL_RTX;
9718 /* Otherwise, return an AND. */
9719 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9723 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9724 in MODE.
9726 Return an equivalent form, if different from X. Otherwise, return X. If
9727 X is zero, we are to always construct the equivalent form. */
9729 static rtx
9730 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9731 unsigned HOST_WIDE_INT constop)
9733 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9734 if (tem)
9735 return tem;
9737 if (!x)
9738 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9739 gen_int_mode (constop, mode));
9740 if (GET_MODE (x) != mode)
9741 x = gen_lowpart (mode, x);
9742 return x;
9745 /* Given a REG, X, compute which bits in X can be nonzero.
9746 We don't care about bits outside of those defined in MODE.
9748 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9749 a shift, AND, or zero_extract, we can do better. */
9751 static rtx
9752 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9753 const_rtx known_x ATTRIBUTE_UNUSED,
9754 machine_mode known_mode ATTRIBUTE_UNUSED,
9755 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9756 unsigned HOST_WIDE_INT *nonzero)
9758 rtx tem;
9759 reg_stat_type *rsp;
9761 /* If X is a register whose nonzero bits value is current, use it.
9762 Otherwise, if X is a register whose value we can find, use that
9763 value. Otherwise, use the previously-computed global nonzero bits
9764 for this register. */
9766 rsp = &reg_stat[REGNO (x)];
9767 if (rsp->last_set_value != 0
9768 && (rsp->last_set_mode == mode
9769 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9770 && GET_MODE_CLASS (mode) == MODE_INT))
9771 && ((rsp->last_set_label >= label_tick_ebb_start
9772 && rsp->last_set_label < label_tick)
9773 || (rsp->last_set_label == label_tick
9774 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9775 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9776 && REGNO (x) < reg_n_sets_max
9777 && REG_N_SETS (REGNO (x)) == 1
9778 && !REGNO_REG_SET_P
9779 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9780 REGNO (x)))))
9782 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9784 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9785 /* We don't know anything about the upper bits. */
9786 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9788 *nonzero &= mask;
9789 return NULL;
9792 tem = get_last_value (x);
9794 if (tem)
9796 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9797 tem = sign_extend_short_imm (tem, GET_MODE (x),
9798 GET_MODE_PRECISION (mode));
9799 #endif
9800 return tem;
9802 else if (nonzero_sign_valid && rsp->nonzero_bits)
9804 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9806 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9807 /* We don't know anything about the upper bits. */
9808 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9810 *nonzero &= mask;
9813 return NULL;
9816 /* Return the number of bits at the high-order end of X that are known to
9817 be equal to the sign bit. X will be used in mode MODE; if MODE is
9818 VOIDmode, X will be used in its own mode. The returned value will always
9819 be between 1 and the number of bits in MODE. */
9821 static rtx
9822 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9823 const_rtx known_x ATTRIBUTE_UNUSED,
9824 machine_mode known_mode
9825 ATTRIBUTE_UNUSED,
9826 unsigned int known_ret ATTRIBUTE_UNUSED,
9827 unsigned int *result)
9829 rtx tem;
9830 reg_stat_type *rsp;
9832 rsp = &reg_stat[REGNO (x)];
9833 if (rsp->last_set_value != 0
9834 && rsp->last_set_mode == mode
9835 && ((rsp->last_set_label >= label_tick_ebb_start
9836 && rsp->last_set_label < label_tick)
9837 || (rsp->last_set_label == label_tick
9838 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9839 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9840 && REGNO (x) < reg_n_sets_max
9841 && REG_N_SETS (REGNO (x)) == 1
9842 && !REGNO_REG_SET_P
9843 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9844 REGNO (x)))))
9846 *result = rsp->last_set_sign_bit_copies;
9847 return NULL;
9850 tem = get_last_value (x);
9851 if (tem != 0)
9852 return tem;
9854 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9855 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9856 *result = rsp->sign_bit_copies;
9858 return NULL;
9861 /* Return the number of "extended" bits there are in X, when interpreted
9862 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9863 unsigned quantities, this is the number of high-order zero bits.
9864 For signed quantities, this is the number of copies of the sign bit
9865 minus 1. In both case, this function returns the number of "spare"
9866 bits. For example, if two quantities for which this function returns
9867 at least 1 are added, the addition is known not to overflow.
9869 This function will always return 0 unless called during combine, which
9870 implies that it must be called from a define_split. */
9872 unsigned int
9873 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9875 if (nonzero_sign_valid == 0)
9876 return 0;
9878 return (unsignedp
9879 ? (HWI_COMPUTABLE_MODE_P (mode)
9880 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9881 - floor_log2 (nonzero_bits (x, mode)))
9882 : 0)
9883 : num_sign_bit_copies (x, mode) - 1);
9886 /* This function is called from `simplify_shift_const' to merge two
9887 outer operations. Specifically, we have already found that we need
9888 to perform operation *POP0 with constant *PCONST0 at the outermost
9889 position. We would now like to also perform OP1 with constant CONST1
9890 (with *POP0 being done last).
9892 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9893 the resulting operation. *PCOMP_P is set to 1 if we would need to
9894 complement the innermost operand, otherwise it is unchanged.
9896 MODE is the mode in which the operation will be done. No bits outside
9897 the width of this mode matter. It is assumed that the width of this mode
9898 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9900 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9901 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9902 result is simply *PCONST0.
9904 If the resulting operation cannot be expressed as one operation, we
9905 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9907 static int
9908 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, machine_mode mode, int *pcomp_p)
9910 enum rtx_code op0 = *pop0;
9911 HOST_WIDE_INT const0 = *pconst0;
9913 const0 &= GET_MODE_MASK (mode);
9914 const1 &= GET_MODE_MASK (mode);
9916 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9917 if (op0 == AND)
9918 const1 &= const0;
9920 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9921 if OP0 is SET. */
9923 if (op1 == UNKNOWN || op0 == SET)
9924 return 1;
9926 else if (op0 == UNKNOWN)
9927 op0 = op1, const0 = const1;
9929 else if (op0 == op1)
9931 switch (op0)
9933 case AND:
9934 const0 &= const1;
9935 break;
9936 case IOR:
9937 const0 |= const1;
9938 break;
9939 case XOR:
9940 const0 ^= const1;
9941 break;
9942 case PLUS:
9943 const0 += const1;
9944 break;
9945 case NEG:
9946 op0 = UNKNOWN;
9947 break;
9948 default:
9949 break;
9953 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9954 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9955 return 0;
9957 /* If the two constants aren't the same, we can't do anything. The
9958 remaining six cases can all be done. */
9959 else if (const0 != const1)
9960 return 0;
9962 else
9963 switch (op0)
9965 case IOR:
9966 if (op1 == AND)
9967 /* (a & b) | b == b */
9968 op0 = SET;
9969 else /* op1 == XOR */
9970 /* (a ^ b) | b == a | b */
9972 break;
9974 case XOR:
9975 if (op1 == AND)
9976 /* (a & b) ^ b == (~a) & b */
9977 op0 = AND, *pcomp_p = 1;
9978 else /* op1 == IOR */
9979 /* (a | b) ^ b == a & ~b */
9980 op0 = AND, const0 = ~const0;
9981 break;
9983 case AND:
9984 if (op1 == IOR)
9985 /* (a | b) & b == b */
9986 op0 = SET;
9987 else /* op1 == XOR */
9988 /* (a ^ b) & b) == (~a) & b */
9989 *pcomp_p = 1;
9990 break;
9991 default:
9992 break;
9995 /* Check for NO-OP cases. */
9996 const0 &= GET_MODE_MASK (mode);
9997 if (const0 == 0
9998 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9999 op0 = UNKNOWN;
10000 else if (const0 == 0 && op0 == AND)
10001 op0 = SET;
10002 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10003 && op0 == AND)
10004 op0 = UNKNOWN;
10006 *pop0 = op0;
10008 /* ??? Slightly redundant with the above mask, but not entirely.
10009 Moving this above means we'd have to sign-extend the mode mask
10010 for the final test. */
10011 if (op0 != UNKNOWN && op0 != NEG)
10012 *pconst0 = trunc_int_for_mode (const0, mode);
10014 return 1;
10017 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10018 the shift in. The original shift operation CODE is performed on OP in
10019 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10020 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10021 result of the shift is subject to operation OUTER_CODE with operand
10022 OUTER_CONST. */
10024 static machine_mode
10025 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10026 machine_mode orig_mode, machine_mode mode,
10027 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10029 if (orig_mode == mode)
10030 return mode;
10031 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10033 /* In general we can't perform in wider mode for right shift and rotate. */
10034 switch (code)
10036 case ASHIFTRT:
10037 /* We can still widen if the bits brought in from the left are identical
10038 to the sign bit of ORIG_MODE. */
10039 if (num_sign_bit_copies (op, mode)
10040 > (unsigned) (GET_MODE_PRECISION (mode)
10041 - GET_MODE_PRECISION (orig_mode)))
10042 return mode;
10043 return orig_mode;
10045 case LSHIFTRT:
10046 /* Similarly here but with zero bits. */
10047 if (HWI_COMPUTABLE_MODE_P (mode)
10048 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10049 return mode;
10051 /* We can also widen if the bits brought in will be masked off. This
10052 operation is performed in ORIG_MODE. */
10053 if (outer_code == AND)
10055 int care_bits = low_bitmask_len (orig_mode, outer_const);
10057 if (care_bits >= 0
10058 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10059 return mode;
10061 /* fall through */
10063 case ROTATE:
10064 return orig_mode;
10066 case ROTATERT:
10067 gcc_unreachable ();
10069 default:
10070 return mode;
10074 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10075 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10076 if we cannot simplify it. Otherwise, return a simplified value.
10078 The shift is normally computed in the widest mode we find in VAROP, as
10079 long as it isn't a different number of words than RESULT_MODE. Exceptions
10080 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10082 static rtx
10083 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10084 rtx varop, int orig_count)
10086 enum rtx_code orig_code = code;
10087 rtx orig_varop = varop;
10088 int count;
10089 machine_mode mode = result_mode;
10090 machine_mode shift_mode, tmode;
10091 unsigned int mode_words
10092 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10093 /* We form (outer_op (code varop count) (outer_const)). */
10094 enum rtx_code outer_op = UNKNOWN;
10095 HOST_WIDE_INT outer_const = 0;
10096 int complement_p = 0;
10097 rtx new_rtx, x;
10099 /* Make sure and truncate the "natural" shift on the way in. We don't
10100 want to do this inside the loop as it makes it more difficult to
10101 combine shifts. */
10102 if (SHIFT_COUNT_TRUNCATED)
10103 orig_count &= GET_MODE_BITSIZE (mode) - 1;
10105 /* If we were given an invalid count, don't do anything except exactly
10106 what was requested. */
10108 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
10109 return NULL_RTX;
10111 count = orig_count;
10113 /* Unless one of the branches of the `if' in this loop does a `continue',
10114 we will `break' the loop after the `if'. */
10116 while (count != 0)
10118 /* If we have an operand of (clobber (const_int 0)), fail. */
10119 if (GET_CODE (varop) == CLOBBER)
10120 return NULL_RTX;
10122 /* Convert ROTATERT to ROTATE. */
10123 if (code == ROTATERT)
10125 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
10126 code = ROTATE;
10127 if (VECTOR_MODE_P (result_mode))
10128 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10129 else
10130 count = bitsize - count;
10133 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10134 mode, outer_op, outer_const);
10136 /* Handle cases where the count is greater than the size of the mode
10137 minus 1. For ASHIFT, use the size minus one as the count (this can
10138 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10139 take the count modulo the size. For other shifts, the result is
10140 zero.
10142 Since these shifts are being produced by the compiler by combining
10143 multiple operations, each of which are defined, we know what the
10144 result is supposed to be. */
10146 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10148 if (code == ASHIFTRT)
10149 count = GET_MODE_PRECISION (shift_mode) - 1;
10150 else if (code == ROTATE || code == ROTATERT)
10151 count %= GET_MODE_PRECISION (shift_mode);
10152 else
10154 /* We can't simply return zero because there may be an
10155 outer op. */
10156 varop = const0_rtx;
10157 count = 0;
10158 break;
10162 /* If we discovered we had to complement VAROP, leave. Making a NOT
10163 here would cause an infinite loop. */
10164 if (complement_p)
10165 break;
10167 /* An arithmetic right shift of a quantity known to be -1 or 0
10168 is a no-op. */
10169 if (code == ASHIFTRT
10170 && (num_sign_bit_copies (varop, shift_mode)
10171 == GET_MODE_PRECISION (shift_mode)))
10173 count = 0;
10174 break;
10177 /* If we are doing an arithmetic right shift and discarding all but
10178 the sign bit copies, this is equivalent to doing a shift by the
10179 bitsize minus one. Convert it into that shift because it will often
10180 allow other simplifications. */
10182 if (code == ASHIFTRT
10183 && (count + num_sign_bit_copies (varop, shift_mode)
10184 >= GET_MODE_PRECISION (shift_mode)))
10185 count = GET_MODE_PRECISION (shift_mode) - 1;
10187 /* We simplify the tests below and elsewhere by converting
10188 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10189 `make_compound_operation' will convert it to an ASHIFTRT for
10190 those machines (such as VAX) that don't have an LSHIFTRT. */
10191 if (code == ASHIFTRT
10192 && val_signbit_known_clear_p (shift_mode,
10193 nonzero_bits (varop, shift_mode)))
10194 code = LSHIFTRT;
10196 if (((code == LSHIFTRT
10197 && HWI_COMPUTABLE_MODE_P (shift_mode)
10198 && !(nonzero_bits (varop, shift_mode) >> count))
10199 || (code == ASHIFT
10200 && HWI_COMPUTABLE_MODE_P (shift_mode)
10201 && !((nonzero_bits (varop, shift_mode) << count)
10202 & GET_MODE_MASK (shift_mode))))
10203 && !side_effects_p (varop))
10204 varop = const0_rtx;
10206 switch (GET_CODE (varop))
10208 case SIGN_EXTEND:
10209 case ZERO_EXTEND:
10210 case SIGN_EXTRACT:
10211 case ZERO_EXTRACT:
10212 new_rtx = expand_compound_operation (varop);
10213 if (new_rtx != varop)
10215 varop = new_rtx;
10216 continue;
10218 break;
10220 case MEM:
10221 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10222 minus the width of a smaller mode, we can do this with a
10223 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10224 if ((code == ASHIFTRT || code == LSHIFTRT)
10225 && ! mode_dependent_address_p (XEXP (varop, 0),
10226 MEM_ADDR_SPACE (varop))
10227 && ! MEM_VOLATILE_P (varop)
10228 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10229 MODE_INT, 1)) != BLKmode)
10231 new_rtx = adjust_address_nv (varop, tmode,
10232 BYTES_BIG_ENDIAN ? 0
10233 : count / BITS_PER_UNIT);
10235 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10236 : ZERO_EXTEND, mode, new_rtx);
10237 count = 0;
10238 continue;
10240 break;
10242 case SUBREG:
10243 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10244 the same number of words as what we've seen so far. Then store
10245 the widest mode in MODE. */
10246 if (subreg_lowpart_p (varop)
10247 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10248 > GET_MODE_SIZE (GET_MODE (varop)))
10249 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10250 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10251 == mode_words
10252 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10253 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10255 varop = SUBREG_REG (varop);
10256 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10257 mode = GET_MODE (varop);
10258 continue;
10260 break;
10262 case MULT:
10263 /* Some machines use MULT instead of ASHIFT because MULT
10264 is cheaper. But it is still better on those machines to
10265 merge two shifts into one. */
10266 if (CONST_INT_P (XEXP (varop, 1))
10267 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10269 varop
10270 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10271 XEXP (varop, 0),
10272 GEN_INT (exact_log2 (
10273 UINTVAL (XEXP (varop, 1)))));
10274 continue;
10276 break;
10278 case UDIV:
10279 /* Similar, for when divides are cheaper. */
10280 if (CONST_INT_P (XEXP (varop, 1))
10281 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10283 varop
10284 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10285 XEXP (varop, 0),
10286 GEN_INT (exact_log2 (
10287 UINTVAL (XEXP (varop, 1)))));
10288 continue;
10290 break;
10292 case ASHIFTRT:
10293 /* If we are extracting just the sign bit of an arithmetic
10294 right shift, that shift is not needed. However, the sign
10295 bit of a wider mode may be different from what would be
10296 interpreted as the sign bit in a narrower mode, so, if
10297 the result is narrower, don't discard the shift. */
10298 if (code == LSHIFTRT
10299 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10300 && (GET_MODE_BITSIZE (result_mode)
10301 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10303 varop = XEXP (varop, 0);
10304 continue;
10307 /* ... fall through ... */
10309 case LSHIFTRT:
10310 case ASHIFT:
10311 case ROTATE:
10312 /* Here we have two nested shifts. The result is usually the
10313 AND of a new shift with a mask. We compute the result below. */
10314 if (CONST_INT_P (XEXP (varop, 1))
10315 && INTVAL (XEXP (varop, 1)) >= 0
10316 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10317 && HWI_COMPUTABLE_MODE_P (result_mode)
10318 && HWI_COMPUTABLE_MODE_P (mode)
10319 && !VECTOR_MODE_P (result_mode))
10321 enum rtx_code first_code = GET_CODE (varop);
10322 unsigned int first_count = INTVAL (XEXP (varop, 1));
10323 unsigned HOST_WIDE_INT mask;
10324 rtx mask_rtx;
10326 /* We have one common special case. We can't do any merging if
10327 the inner code is an ASHIFTRT of a smaller mode. However, if
10328 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10329 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10330 we can convert it to
10331 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10332 This simplifies certain SIGN_EXTEND operations. */
10333 if (code == ASHIFT && first_code == ASHIFTRT
10334 && count == (GET_MODE_PRECISION (result_mode)
10335 - GET_MODE_PRECISION (GET_MODE (varop))))
10337 /* C3 has the low-order C1 bits zero. */
10339 mask = GET_MODE_MASK (mode)
10340 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10342 varop = simplify_and_const_int (NULL_RTX, result_mode,
10343 XEXP (varop, 0), mask);
10344 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10345 varop, count);
10346 count = first_count;
10347 code = ASHIFTRT;
10348 continue;
10351 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10352 than C1 high-order bits equal to the sign bit, we can convert
10353 this to either an ASHIFT or an ASHIFTRT depending on the
10354 two counts.
10356 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10358 if (code == ASHIFTRT && first_code == ASHIFT
10359 && GET_MODE (varop) == shift_mode
10360 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10361 > first_count))
10363 varop = XEXP (varop, 0);
10364 count -= first_count;
10365 if (count < 0)
10367 count = -count;
10368 code = ASHIFT;
10371 continue;
10374 /* There are some cases we can't do. If CODE is ASHIFTRT,
10375 we can only do this if FIRST_CODE is also ASHIFTRT.
10377 We can't do the case when CODE is ROTATE and FIRST_CODE is
10378 ASHIFTRT.
10380 If the mode of this shift is not the mode of the outer shift,
10381 we can't do this if either shift is a right shift or ROTATE.
10383 Finally, we can't do any of these if the mode is too wide
10384 unless the codes are the same.
10386 Handle the case where the shift codes are the same
10387 first. */
10389 if (code == first_code)
10391 if (GET_MODE (varop) != result_mode
10392 && (code == ASHIFTRT || code == LSHIFTRT
10393 || code == ROTATE))
10394 break;
10396 count += first_count;
10397 varop = XEXP (varop, 0);
10398 continue;
10401 if (code == ASHIFTRT
10402 || (code == ROTATE && first_code == ASHIFTRT)
10403 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10404 || (GET_MODE (varop) != result_mode
10405 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10406 || first_code == ROTATE
10407 || code == ROTATE)))
10408 break;
10410 /* To compute the mask to apply after the shift, shift the
10411 nonzero bits of the inner shift the same way the
10412 outer shift will. */
10414 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10415 result_mode);
10417 mask_rtx
10418 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10419 GEN_INT (count));
10421 /* Give up if we can't compute an outer operation to use. */
10422 if (mask_rtx == 0
10423 || !CONST_INT_P (mask_rtx)
10424 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10425 INTVAL (mask_rtx),
10426 result_mode, &complement_p))
10427 break;
10429 /* If the shifts are in the same direction, we add the
10430 counts. Otherwise, we subtract them. */
10431 if ((code == ASHIFTRT || code == LSHIFTRT)
10432 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10433 count += first_count;
10434 else
10435 count -= first_count;
10437 /* If COUNT is positive, the new shift is usually CODE,
10438 except for the two exceptions below, in which case it is
10439 FIRST_CODE. If the count is negative, FIRST_CODE should
10440 always be used */
10441 if (count > 0
10442 && ((first_code == ROTATE && code == ASHIFT)
10443 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10444 code = first_code;
10445 else if (count < 0)
10446 code = first_code, count = -count;
10448 varop = XEXP (varop, 0);
10449 continue;
10452 /* If we have (A << B << C) for any shift, we can convert this to
10453 (A << C << B). This wins if A is a constant. Only try this if
10454 B is not a constant. */
10456 else if (GET_CODE (varop) == code
10457 && CONST_INT_P (XEXP (varop, 0))
10458 && !CONST_INT_P (XEXP (varop, 1)))
10460 rtx new_rtx = simplify_const_binary_operation (code, mode,
10461 XEXP (varop, 0),
10462 GEN_INT (count));
10463 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10464 count = 0;
10465 continue;
10467 break;
10469 case NOT:
10470 if (VECTOR_MODE_P (mode))
10471 break;
10473 /* Make this fit the case below. */
10474 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10475 continue;
10477 case IOR:
10478 case AND:
10479 case XOR:
10480 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10481 with C the size of VAROP - 1 and the shift is logical if
10482 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10483 we have an (le X 0) operation. If we have an arithmetic shift
10484 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10485 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10487 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10488 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10489 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10490 && (code == LSHIFTRT || code == ASHIFTRT)
10491 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10492 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10494 count = 0;
10495 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10496 const0_rtx);
10498 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10499 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10501 continue;
10504 /* If we have (shift (logical)), move the logical to the outside
10505 to allow it to possibly combine with another logical and the
10506 shift to combine with another shift. This also canonicalizes to
10507 what a ZERO_EXTRACT looks like. Also, some machines have
10508 (and (shift)) insns. */
10510 if (CONST_INT_P (XEXP (varop, 1))
10511 /* We can't do this if we have (ashiftrt (xor)) and the
10512 constant has its sign bit set in shift_mode with shift_mode
10513 wider than result_mode. */
10514 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10515 && result_mode != shift_mode
10516 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10517 shift_mode))
10518 && (new_rtx = simplify_const_binary_operation
10519 (code, result_mode,
10520 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10521 GEN_INT (count))) != 0
10522 && CONST_INT_P (new_rtx)
10523 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10524 INTVAL (new_rtx), result_mode, &complement_p))
10526 varop = XEXP (varop, 0);
10527 continue;
10530 /* If we can't do that, try to simplify the shift in each arm of the
10531 logical expression, make a new logical expression, and apply
10532 the inverse distributive law. This also can't be done for
10533 (ashiftrt (xor)) where we've widened the shift and the constant
10534 changes the sign bit. */
10535 if (CONST_INT_P (XEXP (varop, 1))
10536 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10537 && result_mode != shift_mode
10538 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10539 shift_mode)))
10541 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10542 XEXP (varop, 0), count);
10543 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10544 XEXP (varop, 1), count);
10546 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10547 lhs, rhs);
10548 varop = apply_distributive_law (varop);
10550 count = 0;
10551 continue;
10553 break;
10555 case EQ:
10556 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10557 says that the sign bit can be tested, FOO has mode MODE, C is
10558 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10559 that may be nonzero. */
10560 if (code == LSHIFTRT
10561 && XEXP (varop, 1) == const0_rtx
10562 && GET_MODE (XEXP (varop, 0)) == result_mode
10563 && count == (GET_MODE_PRECISION (result_mode) - 1)
10564 && HWI_COMPUTABLE_MODE_P (result_mode)
10565 && STORE_FLAG_VALUE == -1
10566 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10567 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10568 &complement_p))
10570 varop = XEXP (varop, 0);
10571 count = 0;
10572 continue;
10574 break;
10576 case NEG:
10577 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10578 than the number of bits in the mode is equivalent to A. */
10579 if (code == LSHIFTRT
10580 && count == (GET_MODE_PRECISION (result_mode) - 1)
10581 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10583 varop = XEXP (varop, 0);
10584 count = 0;
10585 continue;
10588 /* NEG commutes with ASHIFT since it is multiplication. Move the
10589 NEG outside to allow shifts to combine. */
10590 if (code == ASHIFT
10591 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10592 &complement_p))
10594 varop = XEXP (varop, 0);
10595 continue;
10597 break;
10599 case PLUS:
10600 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10601 is one less than the number of bits in the mode is
10602 equivalent to (xor A 1). */
10603 if (code == LSHIFTRT
10604 && count == (GET_MODE_PRECISION (result_mode) - 1)
10605 && XEXP (varop, 1) == constm1_rtx
10606 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10607 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10608 &complement_p))
10610 count = 0;
10611 varop = XEXP (varop, 0);
10612 continue;
10615 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10616 that might be nonzero in BAR are those being shifted out and those
10617 bits are known zero in FOO, we can replace the PLUS with FOO.
10618 Similarly in the other operand order. This code occurs when
10619 we are computing the size of a variable-size array. */
10621 if ((code == ASHIFTRT || code == LSHIFTRT)
10622 && count < HOST_BITS_PER_WIDE_INT
10623 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10624 && (nonzero_bits (XEXP (varop, 1), result_mode)
10625 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10627 varop = XEXP (varop, 0);
10628 continue;
10630 else if ((code == ASHIFTRT || code == LSHIFTRT)
10631 && count < HOST_BITS_PER_WIDE_INT
10632 && HWI_COMPUTABLE_MODE_P (result_mode)
10633 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10634 >> count)
10635 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10636 & nonzero_bits (XEXP (varop, 1),
10637 result_mode)))
10639 varop = XEXP (varop, 1);
10640 continue;
10643 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10644 if (code == ASHIFT
10645 && CONST_INT_P (XEXP (varop, 1))
10646 && (new_rtx = simplify_const_binary_operation
10647 (ASHIFT, result_mode,
10648 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10649 GEN_INT (count))) != 0
10650 && CONST_INT_P (new_rtx)
10651 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10652 INTVAL (new_rtx), result_mode, &complement_p))
10654 varop = XEXP (varop, 0);
10655 continue;
10658 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10659 signbit', and attempt to change the PLUS to an XOR and move it to
10660 the outer operation as is done above in the AND/IOR/XOR case
10661 leg for shift(logical). See details in logical handling above
10662 for reasoning in doing so. */
10663 if (code == LSHIFTRT
10664 && CONST_INT_P (XEXP (varop, 1))
10665 && mode_signbit_p (result_mode, XEXP (varop, 1))
10666 && (new_rtx = simplify_const_binary_operation
10667 (code, result_mode,
10668 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10669 GEN_INT (count))) != 0
10670 && CONST_INT_P (new_rtx)
10671 && merge_outer_ops (&outer_op, &outer_const, XOR,
10672 INTVAL (new_rtx), result_mode, &complement_p))
10674 varop = XEXP (varop, 0);
10675 continue;
10678 break;
10680 case MINUS:
10681 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10682 with C the size of VAROP - 1 and the shift is logical if
10683 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10684 we have a (gt X 0) operation. If the shift is arithmetic with
10685 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10686 we have a (neg (gt X 0)) operation. */
10688 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10689 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10690 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10691 && (code == LSHIFTRT || code == ASHIFTRT)
10692 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10693 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10694 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10696 count = 0;
10697 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10698 const0_rtx);
10700 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10701 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10703 continue;
10705 break;
10707 case TRUNCATE:
10708 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10709 if the truncate does not affect the value. */
10710 if (code == LSHIFTRT
10711 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10712 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10713 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10714 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10715 - GET_MODE_PRECISION (GET_MODE (varop)))))
10717 rtx varop_inner = XEXP (varop, 0);
10719 varop_inner
10720 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10721 XEXP (varop_inner, 0),
10722 GEN_INT
10723 (count + INTVAL (XEXP (varop_inner, 1))));
10724 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10725 count = 0;
10726 continue;
10728 break;
10730 default:
10731 break;
10734 break;
10737 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10738 outer_op, outer_const);
10740 /* We have now finished analyzing the shift. The result should be
10741 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10742 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10743 to the result of the shift. OUTER_CONST is the relevant constant,
10744 but we must turn off all bits turned off in the shift. */
10746 if (outer_op == UNKNOWN
10747 && orig_code == code && orig_count == count
10748 && varop == orig_varop
10749 && shift_mode == GET_MODE (varop))
10750 return NULL_RTX;
10752 /* Make a SUBREG if necessary. If we can't make it, fail. */
10753 varop = gen_lowpart (shift_mode, varop);
10754 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10755 return NULL_RTX;
10757 /* If we have an outer operation and we just made a shift, it is
10758 possible that we could have simplified the shift were it not
10759 for the outer operation. So try to do the simplification
10760 recursively. */
10762 if (outer_op != UNKNOWN)
10763 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10764 else
10765 x = NULL_RTX;
10767 if (x == NULL_RTX)
10768 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10770 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10771 turn off all the bits that the shift would have turned off. */
10772 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10773 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10774 GET_MODE_MASK (result_mode) >> orig_count);
10776 /* Do the remainder of the processing in RESULT_MODE. */
10777 x = gen_lowpart_or_truncate (result_mode, x);
10779 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10780 operation. */
10781 if (complement_p)
10782 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10784 if (outer_op != UNKNOWN)
10786 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10787 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10788 outer_const = trunc_int_for_mode (outer_const, result_mode);
10790 if (outer_op == AND)
10791 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10792 else if (outer_op == SET)
10794 /* This means that we have determined that the result is
10795 equivalent to a constant. This should be rare. */
10796 if (!side_effects_p (x))
10797 x = GEN_INT (outer_const);
10799 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10800 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10801 else
10802 x = simplify_gen_binary (outer_op, result_mode, x,
10803 GEN_INT (outer_const));
10806 return x;
10809 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10810 The result of the shift is RESULT_MODE. If we cannot simplify it,
10811 return X or, if it is NULL, synthesize the expression with
10812 simplify_gen_binary. Otherwise, return a simplified value.
10814 The shift is normally computed in the widest mode we find in VAROP, as
10815 long as it isn't a different number of words than RESULT_MODE. Exceptions
10816 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10818 static rtx
10819 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10820 rtx varop, int count)
10822 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10823 if (tem)
10824 return tem;
10826 if (!x)
10827 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10828 if (GET_MODE (x) != result_mode)
10829 x = gen_lowpart (result_mode, x);
10830 return x;
10834 /* Like recog, but we receive the address of a pointer to a new pattern.
10835 We try to match the rtx that the pointer points to.
10836 If that fails, we may try to modify or replace the pattern,
10837 storing the replacement into the same pointer object.
10839 Modifications include deletion or addition of CLOBBERs.
10841 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10842 the CLOBBERs are placed.
10844 The value is the final insn code from the pattern ultimately matched,
10845 or -1. */
10847 static int
10848 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10850 rtx pat = *pnewpat;
10851 rtx pat_without_clobbers;
10852 int insn_code_number;
10853 int num_clobbers_to_add = 0;
10854 int i;
10855 rtx notes = NULL_RTX;
10856 rtx old_notes, old_pat;
10857 int old_icode;
10859 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10860 we use to indicate that something didn't match. If we find such a
10861 thing, force rejection. */
10862 if (GET_CODE (pat) == PARALLEL)
10863 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10864 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10865 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10866 return -1;
10868 old_pat = PATTERN (insn);
10869 old_notes = REG_NOTES (insn);
10870 PATTERN (insn) = pat;
10871 REG_NOTES (insn) = NULL_RTX;
10873 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10874 if (dump_file && (dump_flags & TDF_DETAILS))
10876 if (insn_code_number < 0)
10877 fputs ("Failed to match this instruction:\n", dump_file);
10878 else
10879 fputs ("Successfully matched this instruction:\n", dump_file);
10880 print_rtl_single (dump_file, pat);
10883 /* If it isn't, there is the possibility that we previously had an insn
10884 that clobbered some register as a side effect, but the combined
10885 insn doesn't need to do that. So try once more without the clobbers
10886 unless this represents an ASM insn. */
10888 if (insn_code_number < 0 && ! check_asm_operands (pat)
10889 && GET_CODE (pat) == PARALLEL)
10891 int pos;
10893 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10894 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10896 if (i != pos)
10897 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10898 pos++;
10901 SUBST_INT (XVECLEN (pat, 0), pos);
10903 if (pos == 1)
10904 pat = XVECEXP (pat, 0, 0);
10906 PATTERN (insn) = pat;
10907 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10908 if (dump_file && (dump_flags & TDF_DETAILS))
10910 if (insn_code_number < 0)
10911 fputs ("Failed to match this instruction:\n", dump_file);
10912 else
10913 fputs ("Successfully matched this instruction:\n", dump_file);
10914 print_rtl_single (dump_file, pat);
10918 pat_without_clobbers = pat;
10920 PATTERN (insn) = old_pat;
10921 REG_NOTES (insn) = old_notes;
10923 /* Recognize all noop sets, these will be killed by followup pass. */
10924 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10925 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10927 /* If we had any clobbers to add, make a new pattern than contains
10928 them. Then check to make sure that all of them are dead. */
10929 if (num_clobbers_to_add)
10931 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10932 rtvec_alloc (GET_CODE (pat) == PARALLEL
10933 ? (XVECLEN (pat, 0)
10934 + num_clobbers_to_add)
10935 : num_clobbers_to_add + 1));
10937 if (GET_CODE (pat) == PARALLEL)
10938 for (i = 0; i < XVECLEN (pat, 0); i++)
10939 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10940 else
10941 XVECEXP (newpat, 0, 0) = pat;
10943 add_clobbers (newpat, insn_code_number);
10945 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10946 i < XVECLEN (newpat, 0); i++)
10948 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10949 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10950 return -1;
10951 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10953 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10954 notes = alloc_reg_note (REG_UNUSED,
10955 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10958 pat = newpat;
10961 if (insn_code_number >= 0
10962 && insn_code_number != NOOP_MOVE_INSN_CODE)
10964 old_pat = PATTERN (insn);
10965 old_notes = REG_NOTES (insn);
10966 old_icode = INSN_CODE (insn);
10967 PATTERN (insn) = pat;
10968 REG_NOTES (insn) = notes;
10970 /* Allow targets to reject combined insn. */
10971 if (!targetm.legitimate_combined_insn (insn))
10973 if (dump_file && (dump_flags & TDF_DETAILS))
10974 fputs ("Instruction not appropriate for target.",
10975 dump_file);
10977 /* Callers expect recog_for_combine to strip
10978 clobbers from the pattern on failure. */
10979 pat = pat_without_clobbers;
10980 notes = NULL_RTX;
10982 insn_code_number = -1;
10985 PATTERN (insn) = old_pat;
10986 REG_NOTES (insn) = old_notes;
10987 INSN_CODE (insn) = old_icode;
10990 *pnewpat = pat;
10991 *pnotes = notes;
10993 return insn_code_number;
10996 /* Like gen_lowpart_general but for use by combine. In combine it
10997 is not possible to create any new pseudoregs. However, it is
10998 safe to create invalid memory addresses, because combine will
10999 try to recognize them and all they will do is make the combine
11000 attempt fail.
11002 If for some reason this cannot do its job, an rtx
11003 (clobber (const_int 0)) is returned.
11004 An insn containing that will not be recognized. */
11006 static rtx
11007 gen_lowpart_for_combine (machine_mode omode, rtx x)
11009 machine_mode imode = GET_MODE (x);
11010 unsigned int osize = GET_MODE_SIZE (omode);
11011 unsigned int isize = GET_MODE_SIZE (imode);
11012 rtx result;
11014 if (omode == imode)
11015 return x;
11017 /* We can only support MODE being wider than a word if X is a
11018 constant integer or has a mode the same size. */
11019 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11020 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11021 goto fail;
11023 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11024 won't know what to do. So we will strip off the SUBREG here and
11025 process normally. */
11026 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11028 x = SUBREG_REG (x);
11030 /* For use in case we fall down into the address adjustments
11031 further below, we need to adjust the known mode and size of
11032 x; imode and isize, since we just adjusted x. */
11033 imode = GET_MODE (x);
11035 if (imode == omode)
11036 return x;
11038 isize = GET_MODE_SIZE (imode);
11041 result = gen_lowpart_common (omode, x);
11043 if (result)
11044 return result;
11046 if (MEM_P (x))
11048 int offset = 0;
11050 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11051 address. */
11052 if (MEM_VOLATILE_P (x)
11053 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11054 goto fail;
11056 /* If we want to refer to something bigger than the original memref,
11057 generate a paradoxical subreg instead. That will force a reload
11058 of the original memref X. */
11059 if (isize < osize)
11060 return gen_rtx_SUBREG (omode, x, 0);
11062 if (WORDS_BIG_ENDIAN)
11063 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11065 /* Adjust the address so that the address-after-the-data is
11066 unchanged. */
11067 if (BYTES_BIG_ENDIAN)
11068 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11070 return adjust_address_nv (x, omode, offset);
11073 /* If X is a comparison operator, rewrite it in a new mode. This
11074 probably won't match, but may allow further simplifications. */
11075 else if (COMPARISON_P (x))
11076 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11078 /* If we couldn't simplify X any other way, just enclose it in a
11079 SUBREG. Normally, this SUBREG won't match, but some patterns may
11080 include an explicit SUBREG or we may simplify it further in combine. */
11081 else
11083 int offset = 0;
11084 rtx res;
11086 offset = subreg_lowpart_offset (omode, imode);
11087 if (imode == VOIDmode)
11089 imode = int_mode_for_mode (omode);
11090 x = gen_lowpart_common (imode, x);
11091 if (x == NULL)
11092 goto fail;
11094 res = simplify_gen_subreg (omode, x, imode, offset);
11095 if (res)
11096 return res;
11099 fail:
11100 return gen_rtx_CLOBBER (omode, const0_rtx);
11103 /* Try to simplify a comparison between OP0 and a constant OP1,
11104 where CODE is the comparison code that will be tested, into a
11105 (CODE OP0 const0_rtx) form.
11107 The result is a possibly different comparison code to use.
11108 *POP1 may be updated. */
11110 static enum rtx_code
11111 simplify_compare_const (enum rtx_code code, machine_mode mode,
11112 rtx op0, rtx *pop1)
11114 unsigned int mode_width = GET_MODE_PRECISION (mode);
11115 HOST_WIDE_INT const_op = INTVAL (*pop1);
11117 /* Get the constant we are comparing against and turn off all bits
11118 not on in our mode. */
11119 if (mode != VOIDmode)
11120 const_op = trunc_int_for_mode (const_op, mode);
11122 /* If we are comparing against a constant power of two and the value
11123 being compared can only have that single bit nonzero (e.g., it was
11124 `and'ed with that bit), we can replace this with a comparison
11125 with zero. */
11126 if (const_op
11127 && (code == EQ || code == NE || code == GE || code == GEU
11128 || code == LT || code == LTU)
11129 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11130 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11131 && (nonzero_bits (op0, mode)
11132 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11134 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11135 const_op = 0;
11138 /* Similarly, if we are comparing a value known to be either -1 or
11139 0 with -1, change it to the opposite comparison against zero. */
11140 if (const_op == -1
11141 && (code == EQ || code == NE || code == GT || code == LE
11142 || code == GEU || code == LTU)
11143 && num_sign_bit_copies (op0, mode) == mode_width)
11145 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11146 const_op = 0;
11149 /* Do some canonicalizations based on the comparison code. We prefer
11150 comparisons against zero and then prefer equality comparisons.
11151 If we can reduce the size of a constant, we will do that too. */
11152 switch (code)
11154 case LT:
11155 /* < C is equivalent to <= (C - 1) */
11156 if (const_op > 0)
11158 const_op -= 1;
11159 code = LE;
11160 /* ... fall through to LE case below. */
11162 else
11163 break;
11165 case LE:
11166 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11167 if (const_op < 0)
11169 const_op += 1;
11170 code = LT;
11173 /* If we are doing a <= 0 comparison on a value known to have
11174 a zero sign bit, we can replace this with == 0. */
11175 else if (const_op == 0
11176 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11177 && (nonzero_bits (op0, mode)
11178 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11179 == 0)
11180 code = EQ;
11181 break;
11183 case GE:
11184 /* >= C is equivalent to > (C - 1). */
11185 if (const_op > 0)
11187 const_op -= 1;
11188 code = GT;
11189 /* ... fall through to GT below. */
11191 else
11192 break;
11194 case GT:
11195 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11196 if (const_op < 0)
11198 const_op += 1;
11199 code = GE;
11202 /* If we are doing a > 0 comparison on a value known to have
11203 a zero sign bit, we can replace this with != 0. */
11204 else if (const_op == 0
11205 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11206 && (nonzero_bits (op0, mode)
11207 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11208 == 0)
11209 code = NE;
11210 break;
11212 case LTU:
11213 /* < C is equivalent to <= (C - 1). */
11214 if (const_op > 0)
11216 const_op -= 1;
11217 code = LEU;
11218 /* ... fall through ... */
11220 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11221 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11222 && (unsigned HOST_WIDE_INT) const_op
11223 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11225 const_op = 0;
11226 code = GE;
11227 break;
11229 else
11230 break;
11232 case LEU:
11233 /* unsigned <= 0 is equivalent to == 0 */
11234 if (const_op == 0)
11235 code = EQ;
11236 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11237 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11238 && (unsigned HOST_WIDE_INT) const_op
11239 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11241 const_op = 0;
11242 code = GE;
11244 break;
11246 case GEU:
11247 /* >= C is equivalent to > (C - 1). */
11248 if (const_op > 1)
11250 const_op -= 1;
11251 code = GTU;
11252 /* ... fall through ... */
11255 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11256 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11257 && (unsigned HOST_WIDE_INT) const_op
11258 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11260 const_op = 0;
11261 code = LT;
11262 break;
11264 else
11265 break;
11267 case GTU:
11268 /* unsigned > 0 is equivalent to != 0 */
11269 if (const_op == 0)
11270 code = NE;
11271 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11272 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11273 && (unsigned HOST_WIDE_INT) const_op
11274 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11276 const_op = 0;
11277 code = LT;
11279 break;
11281 default:
11282 break;
11285 *pop1 = GEN_INT (const_op);
11286 return code;
11289 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11290 comparison code that will be tested.
11292 The result is a possibly different comparison code to use. *POP0 and
11293 *POP1 may be updated.
11295 It is possible that we might detect that a comparison is either always
11296 true or always false. However, we do not perform general constant
11297 folding in combine, so this knowledge isn't useful. Such tautologies
11298 should have been detected earlier. Hence we ignore all such cases. */
11300 static enum rtx_code
11301 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11303 rtx op0 = *pop0;
11304 rtx op1 = *pop1;
11305 rtx tem, tem1;
11306 int i;
11307 machine_mode mode, tmode;
11309 /* Try a few ways of applying the same transformation to both operands. */
11310 while (1)
11312 #ifndef WORD_REGISTER_OPERATIONS
11313 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11314 so check specially. */
11315 if (code != GTU && code != GEU && code != LTU && code != LEU
11316 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11317 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11318 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11319 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11320 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11321 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11322 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11323 && CONST_INT_P (XEXP (op0, 1))
11324 && XEXP (op0, 1) == XEXP (op1, 1)
11325 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11326 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11327 && (INTVAL (XEXP (op0, 1))
11328 == (GET_MODE_PRECISION (GET_MODE (op0))
11329 - (GET_MODE_PRECISION
11330 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11332 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11333 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11335 #endif
11337 /* If both operands are the same constant shift, see if we can ignore the
11338 shift. We can if the shift is a rotate or if the bits shifted out of
11339 this shift are known to be zero for both inputs and if the type of
11340 comparison is compatible with the shift. */
11341 if (GET_CODE (op0) == GET_CODE (op1)
11342 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11343 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11344 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11345 && (code != GT && code != LT && code != GE && code != LE))
11346 || (GET_CODE (op0) == ASHIFTRT
11347 && (code != GTU && code != LTU
11348 && code != GEU && code != LEU)))
11349 && CONST_INT_P (XEXP (op0, 1))
11350 && INTVAL (XEXP (op0, 1)) >= 0
11351 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11352 && XEXP (op0, 1) == XEXP (op1, 1))
11354 machine_mode mode = GET_MODE (op0);
11355 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11356 int shift_count = INTVAL (XEXP (op0, 1));
11358 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11359 mask &= (mask >> shift_count) << shift_count;
11360 else if (GET_CODE (op0) == ASHIFT)
11361 mask = (mask & (mask << shift_count)) >> shift_count;
11363 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11364 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11365 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11366 else
11367 break;
11370 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11371 SUBREGs are of the same mode, and, in both cases, the AND would
11372 be redundant if the comparison was done in the narrower mode,
11373 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11374 and the operand's possibly nonzero bits are 0xffffff01; in that case
11375 if we only care about QImode, we don't need the AND). This case
11376 occurs if the output mode of an scc insn is not SImode and
11377 STORE_FLAG_VALUE == 1 (e.g., the 386).
11379 Similarly, check for a case where the AND's are ZERO_EXTEND
11380 operations from some narrower mode even though a SUBREG is not
11381 present. */
11383 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11384 && CONST_INT_P (XEXP (op0, 1))
11385 && CONST_INT_P (XEXP (op1, 1)))
11387 rtx inner_op0 = XEXP (op0, 0);
11388 rtx inner_op1 = XEXP (op1, 0);
11389 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11390 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11391 int changed = 0;
11393 if (paradoxical_subreg_p (inner_op0)
11394 && GET_CODE (inner_op1) == SUBREG
11395 && (GET_MODE (SUBREG_REG (inner_op0))
11396 == GET_MODE (SUBREG_REG (inner_op1)))
11397 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11398 <= HOST_BITS_PER_WIDE_INT)
11399 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11400 GET_MODE (SUBREG_REG (inner_op0)))))
11401 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11402 GET_MODE (SUBREG_REG (inner_op1))))))
11404 op0 = SUBREG_REG (inner_op0);
11405 op1 = SUBREG_REG (inner_op1);
11407 /* The resulting comparison is always unsigned since we masked
11408 off the original sign bit. */
11409 code = unsigned_condition (code);
11411 changed = 1;
11414 else if (c0 == c1)
11415 for (tmode = GET_CLASS_NARROWEST_MODE
11416 (GET_MODE_CLASS (GET_MODE (op0)));
11417 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11418 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11420 op0 = gen_lowpart (tmode, inner_op0);
11421 op1 = gen_lowpart (tmode, inner_op1);
11422 code = unsigned_condition (code);
11423 changed = 1;
11424 break;
11427 if (! changed)
11428 break;
11431 /* If both operands are NOT, we can strip off the outer operation
11432 and adjust the comparison code for swapped operands; similarly for
11433 NEG, except that this must be an equality comparison. */
11434 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11435 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11436 && (code == EQ || code == NE)))
11437 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11439 else
11440 break;
11443 /* If the first operand is a constant, swap the operands and adjust the
11444 comparison code appropriately, but don't do this if the second operand
11445 is already a constant integer. */
11446 if (swap_commutative_operands_p (op0, op1))
11448 std::swap (op0, op1);
11449 code = swap_condition (code);
11452 /* We now enter a loop during which we will try to simplify the comparison.
11453 For the most part, we only are concerned with comparisons with zero,
11454 but some things may really be comparisons with zero but not start
11455 out looking that way. */
11457 while (CONST_INT_P (op1))
11459 machine_mode mode = GET_MODE (op0);
11460 unsigned int mode_width = GET_MODE_PRECISION (mode);
11461 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11462 int equality_comparison_p;
11463 int sign_bit_comparison_p;
11464 int unsigned_comparison_p;
11465 HOST_WIDE_INT const_op;
11467 /* We only want to handle integral modes. This catches VOIDmode,
11468 CCmode, and the floating-point modes. An exception is that we
11469 can handle VOIDmode if OP0 is a COMPARE or a comparison
11470 operation. */
11472 if (GET_MODE_CLASS (mode) != MODE_INT
11473 && ! (mode == VOIDmode
11474 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11475 break;
11477 /* Try to simplify the compare to constant, possibly changing the
11478 comparison op, and/or changing op1 to zero. */
11479 code = simplify_compare_const (code, mode, op0, &op1);
11480 const_op = INTVAL (op1);
11482 /* Compute some predicates to simplify code below. */
11484 equality_comparison_p = (code == EQ || code == NE);
11485 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11486 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11487 || code == GEU);
11489 /* If this is a sign bit comparison and we can do arithmetic in
11490 MODE, say that we will only be needing the sign bit of OP0. */
11491 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11492 op0 = force_to_mode (op0, mode,
11493 (unsigned HOST_WIDE_INT) 1
11494 << (GET_MODE_PRECISION (mode) - 1),
11497 /* Now try cases based on the opcode of OP0. If none of the cases
11498 does a "continue", we exit this loop immediately after the
11499 switch. */
11501 switch (GET_CODE (op0))
11503 case ZERO_EXTRACT:
11504 /* If we are extracting a single bit from a variable position in
11505 a constant that has only a single bit set and are comparing it
11506 with zero, we can convert this into an equality comparison
11507 between the position and the location of the single bit. */
11508 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11509 have already reduced the shift count modulo the word size. */
11510 if (!SHIFT_COUNT_TRUNCATED
11511 && CONST_INT_P (XEXP (op0, 0))
11512 && XEXP (op0, 1) == const1_rtx
11513 && equality_comparison_p && const_op == 0
11514 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11516 if (BITS_BIG_ENDIAN)
11517 i = BITS_PER_WORD - 1 - i;
11519 op0 = XEXP (op0, 2);
11520 op1 = GEN_INT (i);
11521 const_op = i;
11523 /* Result is nonzero iff shift count is equal to I. */
11524 code = reverse_condition (code);
11525 continue;
11528 /* ... fall through ... */
11530 case SIGN_EXTRACT:
11531 tem = expand_compound_operation (op0);
11532 if (tem != op0)
11534 op0 = tem;
11535 continue;
11537 break;
11539 case NOT:
11540 /* If testing for equality, we can take the NOT of the constant. */
11541 if (equality_comparison_p
11542 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11544 op0 = XEXP (op0, 0);
11545 op1 = tem;
11546 continue;
11549 /* If just looking at the sign bit, reverse the sense of the
11550 comparison. */
11551 if (sign_bit_comparison_p)
11553 op0 = XEXP (op0, 0);
11554 code = (code == GE ? LT : GE);
11555 continue;
11557 break;
11559 case NEG:
11560 /* If testing for equality, we can take the NEG of the constant. */
11561 if (equality_comparison_p
11562 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11564 op0 = XEXP (op0, 0);
11565 op1 = tem;
11566 continue;
11569 /* The remaining cases only apply to comparisons with zero. */
11570 if (const_op != 0)
11571 break;
11573 /* When X is ABS or is known positive,
11574 (neg X) is < 0 if and only if X != 0. */
11576 if (sign_bit_comparison_p
11577 && (GET_CODE (XEXP (op0, 0)) == ABS
11578 || (mode_width <= HOST_BITS_PER_WIDE_INT
11579 && (nonzero_bits (XEXP (op0, 0), mode)
11580 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11581 == 0)))
11583 op0 = XEXP (op0, 0);
11584 code = (code == LT ? NE : EQ);
11585 continue;
11588 /* If we have NEG of something whose two high-order bits are the
11589 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11590 if (num_sign_bit_copies (op0, mode) >= 2)
11592 op0 = XEXP (op0, 0);
11593 code = swap_condition (code);
11594 continue;
11596 break;
11598 case ROTATE:
11599 /* If we are testing equality and our count is a constant, we
11600 can perform the inverse operation on our RHS. */
11601 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11602 && (tem = simplify_binary_operation (ROTATERT, mode,
11603 op1, XEXP (op0, 1))) != 0)
11605 op0 = XEXP (op0, 0);
11606 op1 = tem;
11607 continue;
11610 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11611 a particular bit. Convert it to an AND of a constant of that
11612 bit. This will be converted into a ZERO_EXTRACT. */
11613 if (const_op == 0 && sign_bit_comparison_p
11614 && CONST_INT_P (XEXP (op0, 1))
11615 && mode_width <= HOST_BITS_PER_WIDE_INT)
11617 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11618 ((unsigned HOST_WIDE_INT) 1
11619 << (mode_width - 1
11620 - INTVAL (XEXP (op0, 1)))));
11621 code = (code == LT ? NE : EQ);
11622 continue;
11625 /* Fall through. */
11627 case ABS:
11628 /* ABS is ignorable inside an equality comparison with zero. */
11629 if (const_op == 0 && equality_comparison_p)
11631 op0 = XEXP (op0, 0);
11632 continue;
11634 break;
11636 case SIGN_EXTEND:
11637 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11638 (compare FOO CONST) if CONST fits in FOO's mode and we
11639 are either testing inequality or have an unsigned
11640 comparison with ZERO_EXTEND or a signed comparison with
11641 SIGN_EXTEND. But don't do it if we don't have a compare
11642 insn of the given mode, since we'd have to revert it
11643 later on, and then we wouldn't know whether to sign- or
11644 zero-extend. */
11645 mode = GET_MODE (XEXP (op0, 0));
11646 if (GET_MODE_CLASS (mode) == MODE_INT
11647 && ! unsigned_comparison_p
11648 && HWI_COMPUTABLE_MODE_P (mode)
11649 && trunc_int_for_mode (const_op, mode) == const_op
11650 && have_insn_for (COMPARE, mode))
11652 op0 = XEXP (op0, 0);
11653 continue;
11655 break;
11657 case SUBREG:
11658 /* Check for the case where we are comparing A - C1 with C2, that is
11660 (subreg:MODE (plus (A) (-C1))) op (C2)
11662 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11663 comparison in the wider mode. One of the following two conditions
11664 must be true in order for this to be valid:
11666 1. The mode extension results in the same bit pattern being added
11667 on both sides and the comparison is equality or unsigned. As
11668 C2 has been truncated to fit in MODE, the pattern can only be
11669 all 0s or all 1s.
11671 2. The mode extension results in the sign bit being copied on
11672 each side.
11674 The difficulty here is that we have predicates for A but not for
11675 (A - C1) so we need to check that C1 is within proper bounds so
11676 as to perturbate A as little as possible. */
11678 if (mode_width <= HOST_BITS_PER_WIDE_INT
11679 && subreg_lowpart_p (op0)
11680 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11681 && GET_CODE (SUBREG_REG (op0)) == PLUS
11682 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11684 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11685 rtx a = XEXP (SUBREG_REG (op0), 0);
11686 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11688 if ((c1 > 0
11689 && (unsigned HOST_WIDE_INT) c1
11690 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11691 && (equality_comparison_p || unsigned_comparison_p)
11692 /* (A - C1) zero-extends if it is positive and sign-extends
11693 if it is negative, C2 both zero- and sign-extends. */
11694 && ((0 == (nonzero_bits (a, inner_mode)
11695 & ~GET_MODE_MASK (mode))
11696 && const_op >= 0)
11697 /* (A - C1) sign-extends if it is positive and 1-extends
11698 if it is negative, C2 both sign- and 1-extends. */
11699 || (num_sign_bit_copies (a, inner_mode)
11700 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11701 - mode_width)
11702 && const_op < 0)))
11703 || ((unsigned HOST_WIDE_INT) c1
11704 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11705 /* (A - C1) always sign-extends, like C2. */
11706 && num_sign_bit_copies (a, inner_mode)
11707 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11708 - (mode_width - 1))))
11710 op0 = SUBREG_REG (op0);
11711 continue;
11715 /* If the inner mode is narrower and we are extracting the low part,
11716 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11717 if (subreg_lowpart_p (op0)
11718 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11719 /* Fall through */ ;
11720 else
11721 break;
11723 /* ... fall through ... */
11725 case ZERO_EXTEND:
11726 mode = GET_MODE (XEXP (op0, 0));
11727 if (GET_MODE_CLASS (mode) == MODE_INT
11728 && (unsigned_comparison_p || equality_comparison_p)
11729 && HWI_COMPUTABLE_MODE_P (mode)
11730 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11731 && const_op >= 0
11732 && have_insn_for (COMPARE, mode))
11734 op0 = XEXP (op0, 0);
11735 continue;
11737 break;
11739 case PLUS:
11740 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11741 this for equality comparisons due to pathological cases involving
11742 overflows. */
11743 if (equality_comparison_p
11744 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11745 op1, XEXP (op0, 1))))
11747 op0 = XEXP (op0, 0);
11748 op1 = tem;
11749 continue;
11752 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11753 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11754 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11756 op0 = XEXP (XEXP (op0, 0), 0);
11757 code = (code == LT ? EQ : NE);
11758 continue;
11760 break;
11762 case MINUS:
11763 /* We used to optimize signed comparisons against zero, but that
11764 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11765 arrive here as equality comparisons, or (GEU, LTU) are
11766 optimized away. No need to special-case them. */
11768 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11769 (eq B (minus A C)), whichever simplifies. We can only do
11770 this for equality comparisons due to pathological cases involving
11771 overflows. */
11772 if (equality_comparison_p
11773 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11774 XEXP (op0, 1), op1)))
11776 op0 = XEXP (op0, 0);
11777 op1 = tem;
11778 continue;
11781 if (equality_comparison_p
11782 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11783 XEXP (op0, 0), op1)))
11785 op0 = XEXP (op0, 1);
11786 op1 = tem;
11787 continue;
11790 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11791 of bits in X minus 1, is one iff X > 0. */
11792 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11793 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11794 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11795 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11797 op0 = XEXP (op0, 1);
11798 code = (code == GE ? LE : GT);
11799 continue;
11801 break;
11803 case XOR:
11804 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11805 if C is zero or B is a constant. */
11806 if (equality_comparison_p
11807 && 0 != (tem = simplify_binary_operation (XOR, mode,
11808 XEXP (op0, 1), op1)))
11810 op0 = XEXP (op0, 0);
11811 op1 = tem;
11812 continue;
11814 break;
11816 case EQ: case NE:
11817 case UNEQ: case LTGT:
11818 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11819 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11820 case UNORDERED: case ORDERED:
11821 /* We can't do anything if OP0 is a condition code value, rather
11822 than an actual data value. */
11823 if (const_op != 0
11824 || CC0_P (XEXP (op0, 0))
11825 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11826 break;
11828 /* Get the two operands being compared. */
11829 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11830 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11831 else
11832 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11834 /* Check for the cases where we simply want the result of the
11835 earlier test or the opposite of that result. */
11836 if (code == NE || code == EQ
11837 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11838 && (code == LT || code == GE)))
11840 enum rtx_code new_code;
11841 if (code == LT || code == NE)
11842 new_code = GET_CODE (op0);
11843 else
11844 new_code = reversed_comparison_code (op0, NULL);
11846 if (new_code != UNKNOWN)
11848 code = new_code;
11849 op0 = tem;
11850 op1 = tem1;
11851 continue;
11854 break;
11856 case IOR:
11857 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11858 iff X <= 0. */
11859 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11860 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11861 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11863 op0 = XEXP (op0, 1);
11864 code = (code == GE ? GT : LE);
11865 continue;
11867 break;
11869 case AND:
11870 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11871 will be converted to a ZERO_EXTRACT later. */
11872 if (const_op == 0 && equality_comparison_p
11873 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11874 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11876 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11877 XEXP (XEXP (op0, 0), 1));
11878 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11879 continue;
11882 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11883 zero and X is a comparison and C1 and C2 describe only bits set
11884 in STORE_FLAG_VALUE, we can compare with X. */
11885 if (const_op == 0 && equality_comparison_p
11886 && mode_width <= HOST_BITS_PER_WIDE_INT
11887 && CONST_INT_P (XEXP (op0, 1))
11888 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11889 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11890 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11891 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11893 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11894 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11895 if ((~STORE_FLAG_VALUE & mask) == 0
11896 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11897 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11898 && COMPARISON_P (tem))))
11900 op0 = XEXP (XEXP (op0, 0), 0);
11901 continue;
11905 /* If we are doing an equality comparison of an AND of a bit equal
11906 to the sign bit, replace this with a LT or GE comparison of
11907 the underlying value. */
11908 if (equality_comparison_p
11909 && const_op == 0
11910 && CONST_INT_P (XEXP (op0, 1))
11911 && mode_width <= HOST_BITS_PER_WIDE_INT
11912 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11913 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11915 op0 = XEXP (op0, 0);
11916 code = (code == EQ ? GE : LT);
11917 continue;
11920 /* If this AND operation is really a ZERO_EXTEND from a narrower
11921 mode, the constant fits within that mode, and this is either an
11922 equality or unsigned comparison, try to do this comparison in
11923 the narrower mode.
11925 Note that in:
11927 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11928 -> (ne:DI (reg:SI 4) (const_int 0))
11930 unless TRULY_NOOP_TRUNCATION allows it or the register is
11931 known to hold a value of the required mode the
11932 transformation is invalid. */
11933 if ((equality_comparison_p || unsigned_comparison_p)
11934 && CONST_INT_P (XEXP (op0, 1))
11935 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11936 & GET_MODE_MASK (mode))
11937 + 1)) >= 0
11938 && const_op >> i == 0
11939 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11940 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11941 || (REG_P (XEXP (op0, 0))
11942 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11944 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11945 continue;
11948 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11949 fits in both M1 and M2 and the SUBREG is either paradoxical
11950 or represents the low part, permute the SUBREG and the AND
11951 and try again. */
11952 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11954 unsigned HOST_WIDE_INT c1;
11955 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11956 /* Require an integral mode, to avoid creating something like
11957 (AND:SF ...). */
11958 if (SCALAR_INT_MODE_P (tmode)
11959 /* It is unsafe to commute the AND into the SUBREG if the
11960 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11961 not defined. As originally written the upper bits
11962 have a defined value due to the AND operation.
11963 However, if we commute the AND inside the SUBREG then
11964 they no longer have defined values and the meaning of
11965 the code has been changed. */
11966 && (0
11967 #ifdef WORD_REGISTER_OPERATIONS
11968 || (mode_width > GET_MODE_PRECISION (tmode)
11969 && mode_width <= BITS_PER_WORD)
11970 #endif
11971 || (mode_width <= GET_MODE_PRECISION (tmode)
11972 && subreg_lowpart_p (XEXP (op0, 0))))
11973 && CONST_INT_P (XEXP (op0, 1))
11974 && mode_width <= HOST_BITS_PER_WIDE_INT
11975 && HWI_COMPUTABLE_MODE_P (tmode)
11976 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11977 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11978 && c1 != mask
11979 && c1 != GET_MODE_MASK (tmode))
11981 op0 = simplify_gen_binary (AND, tmode,
11982 SUBREG_REG (XEXP (op0, 0)),
11983 gen_int_mode (c1, tmode));
11984 op0 = gen_lowpart (mode, op0);
11985 continue;
11989 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11990 if (const_op == 0 && equality_comparison_p
11991 && XEXP (op0, 1) == const1_rtx
11992 && GET_CODE (XEXP (op0, 0)) == NOT)
11994 op0 = simplify_and_const_int (NULL_RTX, mode,
11995 XEXP (XEXP (op0, 0), 0), 1);
11996 code = (code == NE ? EQ : NE);
11997 continue;
12000 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12001 (eq (and (lshiftrt X) 1) 0).
12002 Also handle the case where (not X) is expressed using xor. */
12003 if (const_op == 0 && equality_comparison_p
12004 && XEXP (op0, 1) == const1_rtx
12005 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12007 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12008 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12010 if (GET_CODE (shift_op) == NOT
12011 || (GET_CODE (shift_op) == XOR
12012 && CONST_INT_P (XEXP (shift_op, 1))
12013 && CONST_INT_P (shift_count)
12014 && HWI_COMPUTABLE_MODE_P (mode)
12015 && (UINTVAL (XEXP (shift_op, 1))
12016 == (unsigned HOST_WIDE_INT) 1
12017 << INTVAL (shift_count))))
12020 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12021 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12022 code = (code == NE ? EQ : NE);
12023 continue;
12026 break;
12028 case ASHIFT:
12029 /* If we have (compare (ashift FOO N) (const_int C)) and
12030 the high order N bits of FOO (N+1 if an inequality comparison)
12031 are known to be zero, we can do this by comparing FOO with C
12032 shifted right N bits so long as the low-order N bits of C are
12033 zero. */
12034 if (CONST_INT_P (XEXP (op0, 1))
12035 && INTVAL (XEXP (op0, 1)) >= 0
12036 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12037 < HOST_BITS_PER_WIDE_INT)
12038 && (((unsigned HOST_WIDE_INT) const_op
12039 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
12040 - 1)) == 0)
12041 && mode_width <= HOST_BITS_PER_WIDE_INT
12042 && (nonzero_bits (XEXP (op0, 0), mode)
12043 & ~(mask >> (INTVAL (XEXP (op0, 1))
12044 + ! equality_comparison_p))) == 0)
12046 /* We must perform a logical shift, not an arithmetic one,
12047 as we want the top N bits of C to be zero. */
12048 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12050 temp >>= INTVAL (XEXP (op0, 1));
12051 op1 = gen_int_mode (temp, mode);
12052 op0 = XEXP (op0, 0);
12053 continue;
12056 /* If we are doing a sign bit comparison, it means we are testing
12057 a particular bit. Convert it to the appropriate AND. */
12058 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12059 && mode_width <= HOST_BITS_PER_WIDE_INT)
12061 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12062 ((unsigned HOST_WIDE_INT) 1
12063 << (mode_width - 1
12064 - INTVAL (XEXP (op0, 1)))));
12065 code = (code == LT ? NE : EQ);
12066 continue;
12069 /* If this an equality comparison with zero and we are shifting
12070 the low bit to the sign bit, we can convert this to an AND of the
12071 low-order bit. */
12072 if (const_op == 0 && equality_comparison_p
12073 && CONST_INT_P (XEXP (op0, 1))
12074 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12076 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12077 continue;
12079 break;
12081 case ASHIFTRT:
12082 /* If this is an equality comparison with zero, we can do this
12083 as a logical shift, which might be much simpler. */
12084 if (equality_comparison_p && const_op == 0
12085 && CONST_INT_P (XEXP (op0, 1)))
12087 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12088 XEXP (op0, 0),
12089 INTVAL (XEXP (op0, 1)));
12090 continue;
12093 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12094 do the comparison in a narrower mode. */
12095 if (! unsigned_comparison_p
12096 && CONST_INT_P (XEXP (op0, 1))
12097 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12098 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12099 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12100 MODE_INT, 1)) != BLKmode
12101 && (((unsigned HOST_WIDE_INT) const_op
12102 + (GET_MODE_MASK (tmode) >> 1) + 1)
12103 <= GET_MODE_MASK (tmode)))
12105 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12106 continue;
12109 /* Likewise if OP0 is a PLUS of a sign extension with a
12110 constant, which is usually represented with the PLUS
12111 between the shifts. */
12112 if (! unsigned_comparison_p
12113 && CONST_INT_P (XEXP (op0, 1))
12114 && GET_CODE (XEXP (op0, 0)) == PLUS
12115 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12116 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12117 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12118 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12119 MODE_INT, 1)) != BLKmode
12120 && (((unsigned HOST_WIDE_INT) const_op
12121 + (GET_MODE_MASK (tmode) >> 1) + 1)
12122 <= GET_MODE_MASK (tmode)))
12124 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12125 rtx add_const = XEXP (XEXP (op0, 0), 1);
12126 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12127 add_const, XEXP (op0, 1));
12129 op0 = simplify_gen_binary (PLUS, tmode,
12130 gen_lowpart (tmode, inner),
12131 new_const);
12132 continue;
12135 /* ... fall through ... */
12136 case LSHIFTRT:
12137 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12138 the low order N bits of FOO are known to be zero, we can do this
12139 by comparing FOO with C shifted left N bits so long as no
12140 overflow occurs. Even if the low order N bits of FOO aren't known
12141 to be zero, if the comparison is >= or < we can use the same
12142 optimization and for > or <= by setting all the low
12143 order N bits in the comparison constant. */
12144 if (CONST_INT_P (XEXP (op0, 1))
12145 && INTVAL (XEXP (op0, 1)) > 0
12146 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12147 && mode_width <= HOST_BITS_PER_WIDE_INT
12148 && (((unsigned HOST_WIDE_INT) const_op
12149 + (GET_CODE (op0) != LSHIFTRT
12150 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12151 + 1)
12152 : 0))
12153 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12155 unsigned HOST_WIDE_INT low_bits
12156 = (nonzero_bits (XEXP (op0, 0), mode)
12157 & (((unsigned HOST_WIDE_INT) 1
12158 << INTVAL (XEXP (op0, 1))) - 1));
12159 if (low_bits == 0 || !equality_comparison_p)
12161 /* If the shift was logical, then we must make the condition
12162 unsigned. */
12163 if (GET_CODE (op0) == LSHIFTRT)
12164 code = unsigned_condition (code);
12166 const_op <<= INTVAL (XEXP (op0, 1));
12167 if (low_bits != 0
12168 && (code == GT || code == GTU
12169 || code == LE || code == LEU))
12170 const_op
12171 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12172 op1 = GEN_INT (const_op);
12173 op0 = XEXP (op0, 0);
12174 continue;
12178 /* If we are using this shift to extract just the sign bit, we
12179 can replace this with an LT or GE comparison. */
12180 if (const_op == 0
12181 && (equality_comparison_p || sign_bit_comparison_p)
12182 && CONST_INT_P (XEXP (op0, 1))
12183 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12185 op0 = XEXP (op0, 0);
12186 code = (code == NE || code == GT ? LT : GE);
12187 continue;
12189 break;
12191 default:
12192 break;
12195 break;
12198 /* Now make any compound operations involved in this comparison. Then,
12199 check for an outmost SUBREG on OP0 that is not doing anything or is
12200 paradoxical. The latter transformation must only be performed when
12201 it is known that the "extra" bits will be the same in op0 and op1 or
12202 that they don't matter. There are three cases to consider:
12204 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12205 care bits and we can assume they have any convenient value. So
12206 making the transformation is safe.
12208 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12209 In this case the upper bits of op0 are undefined. We should not make
12210 the simplification in that case as we do not know the contents of
12211 those bits.
12213 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12214 UNKNOWN. In that case we know those bits are zeros or ones. We must
12215 also be sure that they are the same as the upper bits of op1.
12217 We can never remove a SUBREG for a non-equality comparison because
12218 the sign bit is in a different place in the underlying object. */
12220 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12221 op1 = make_compound_operation (op1, SET);
12223 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12224 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12225 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12226 && (code == NE || code == EQ))
12228 if (paradoxical_subreg_p (op0))
12230 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12231 implemented. */
12232 if (REG_P (SUBREG_REG (op0)))
12234 op0 = SUBREG_REG (op0);
12235 op1 = gen_lowpart (GET_MODE (op0), op1);
12238 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12239 <= HOST_BITS_PER_WIDE_INT)
12240 && (nonzero_bits (SUBREG_REG (op0),
12241 GET_MODE (SUBREG_REG (op0)))
12242 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12244 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12246 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12247 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12248 op0 = SUBREG_REG (op0), op1 = tem;
12252 /* We now do the opposite procedure: Some machines don't have compare
12253 insns in all modes. If OP0's mode is an integer mode smaller than a
12254 word and we can't do a compare in that mode, see if there is a larger
12255 mode for which we can do the compare. There are a number of cases in
12256 which we can use the wider mode. */
12258 mode = GET_MODE (op0);
12259 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12260 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12261 && ! have_insn_for (COMPARE, mode))
12262 for (tmode = GET_MODE_WIDER_MODE (mode);
12263 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12264 tmode = GET_MODE_WIDER_MODE (tmode))
12265 if (have_insn_for (COMPARE, tmode))
12267 int zero_extended;
12269 /* If this is a test for negative, we can make an explicit
12270 test of the sign bit. Test this first so we can use
12271 a paradoxical subreg to extend OP0. */
12273 if (op1 == const0_rtx && (code == LT || code == GE)
12274 && HWI_COMPUTABLE_MODE_P (mode))
12276 unsigned HOST_WIDE_INT sign
12277 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12278 op0 = simplify_gen_binary (AND, tmode,
12279 gen_lowpart (tmode, op0),
12280 gen_int_mode (sign, tmode));
12281 code = (code == LT) ? NE : EQ;
12282 break;
12285 /* If the only nonzero bits in OP0 and OP1 are those in the
12286 narrower mode and this is an equality or unsigned comparison,
12287 we can use the wider mode. Similarly for sign-extended
12288 values, in which case it is true for all comparisons. */
12289 zero_extended = ((code == EQ || code == NE
12290 || code == GEU || code == GTU
12291 || code == LEU || code == LTU)
12292 && (nonzero_bits (op0, tmode)
12293 & ~GET_MODE_MASK (mode)) == 0
12294 && ((CONST_INT_P (op1)
12295 || (nonzero_bits (op1, tmode)
12296 & ~GET_MODE_MASK (mode)) == 0)));
12298 if (zero_extended
12299 || ((num_sign_bit_copies (op0, tmode)
12300 > (unsigned int) (GET_MODE_PRECISION (tmode)
12301 - GET_MODE_PRECISION (mode)))
12302 && (num_sign_bit_copies (op1, tmode)
12303 > (unsigned int) (GET_MODE_PRECISION (tmode)
12304 - GET_MODE_PRECISION (mode)))))
12306 /* If OP0 is an AND and we don't have an AND in MODE either,
12307 make a new AND in the proper mode. */
12308 if (GET_CODE (op0) == AND
12309 && !have_insn_for (AND, mode))
12310 op0 = simplify_gen_binary (AND, tmode,
12311 gen_lowpart (tmode,
12312 XEXP (op0, 0)),
12313 gen_lowpart (tmode,
12314 XEXP (op0, 1)));
12315 else
12317 if (zero_extended)
12319 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12320 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12322 else
12324 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12325 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12327 break;
12332 /* We may have changed the comparison operands. Re-canonicalize. */
12333 if (swap_commutative_operands_p (op0, op1))
12335 std::swap (op0, op1);
12336 code = swap_condition (code);
12339 /* If this machine only supports a subset of valid comparisons, see if we
12340 can convert an unsupported one into a supported one. */
12341 target_canonicalize_comparison (&code, &op0, &op1, 0);
12343 *pop0 = op0;
12344 *pop1 = op1;
12346 return code;
12349 /* Utility function for record_value_for_reg. Count number of
12350 rtxs in X. */
12351 static int
12352 count_rtxs (rtx x)
12354 enum rtx_code code = GET_CODE (x);
12355 const char *fmt;
12356 int i, j, ret = 1;
12358 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12359 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12361 rtx x0 = XEXP (x, 0);
12362 rtx x1 = XEXP (x, 1);
12364 if (x0 == x1)
12365 return 1 + 2 * count_rtxs (x0);
12367 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12368 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12369 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12370 return 2 + 2 * count_rtxs (x0)
12371 + count_rtxs (x == XEXP (x1, 0)
12372 ? XEXP (x1, 1) : XEXP (x1, 0));
12374 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12375 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12376 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12377 return 2 + 2 * count_rtxs (x1)
12378 + count_rtxs (x == XEXP (x0, 0)
12379 ? XEXP (x0, 1) : XEXP (x0, 0));
12382 fmt = GET_RTX_FORMAT (code);
12383 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12384 if (fmt[i] == 'e')
12385 ret += count_rtxs (XEXP (x, i));
12386 else if (fmt[i] == 'E')
12387 for (j = 0; j < XVECLEN (x, i); j++)
12388 ret += count_rtxs (XVECEXP (x, i, j));
12390 return ret;
12393 /* Utility function for following routine. Called when X is part of a value
12394 being stored into last_set_value. Sets last_set_table_tick
12395 for each register mentioned. Similar to mention_regs in cse.c */
12397 static void
12398 update_table_tick (rtx x)
12400 enum rtx_code code = GET_CODE (x);
12401 const char *fmt = GET_RTX_FORMAT (code);
12402 int i, j;
12404 if (code == REG)
12406 unsigned int regno = REGNO (x);
12407 unsigned int endregno = END_REGNO (x);
12408 unsigned int r;
12410 for (r = regno; r < endregno; r++)
12412 reg_stat_type *rsp = &reg_stat[r];
12413 rsp->last_set_table_tick = label_tick;
12416 return;
12419 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12420 if (fmt[i] == 'e')
12422 /* Check for identical subexpressions. If x contains
12423 identical subexpression we only have to traverse one of
12424 them. */
12425 if (i == 0 && ARITHMETIC_P (x))
12427 /* Note that at this point x1 has already been
12428 processed. */
12429 rtx x0 = XEXP (x, 0);
12430 rtx x1 = XEXP (x, 1);
12432 /* If x0 and x1 are identical then there is no need to
12433 process x0. */
12434 if (x0 == x1)
12435 break;
12437 /* If x0 is identical to a subexpression of x1 then while
12438 processing x1, x0 has already been processed. Thus we
12439 are done with x. */
12440 if (ARITHMETIC_P (x1)
12441 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12442 break;
12444 /* If x1 is identical to a subexpression of x0 then we
12445 still have to process the rest of x0. */
12446 if (ARITHMETIC_P (x0)
12447 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12449 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12450 break;
12454 update_table_tick (XEXP (x, i));
12456 else if (fmt[i] == 'E')
12457 for (j = 0; j < XVECLEN (x, i); j++)
12458 update_table_tick (XVECEXP (x, i, j));
12461 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12462 are saying that the register is clobbered and we no longer know its
12463 value. If INSN is zero, don't update reg_stat[].last_set; this is
12464 only permitted with VALUE also zero and is used to invalidate the
12465 register. */
12467 static void
12468 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12470 unsigned int regno = REGNO (reg);
12471 unsigned int endregno = END_REGNO (reg);
12472 unsigned int i;
12473 reg_stat_type *rsp;
12475 /* If VALUE contains REG and we have a previous value for REG, substitute
12476 the previous value. */
12477 if (value && insn && reg_overlap_mentioned_p (reg, value))
12479 rtx tem;
12481 /* Set things up so get_last_value is allowed to see anything set up to
12482 our insn. */
12483 subst_low_luid = DF_INSN_LUID (insn);
12484 tem = get_last_value (reg);
12486 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12487 it isn't going to be useful and will take a lot of time to process,
12488 so just use the CLOBBER. */
12490 if (tem)
12492 if (ARITHMETIC_P (tem)
12493 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12494 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12495 tem = XEXP (tem, 0);
12496 else if (count_occurrences (value, reg, 1) >= 2)
12498 /* If there are two or more occurrences of REG in VALUE,
12499 prevent the value from growing too much. */
12500 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12501 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12504 value = replace_rtx (copy_rtx (value), reg, tem);
12508 /* For each register modified, show we don't know its value, that
12509 we don't know about its bitwise content, that its value has been
12510 updated, and that we don't know the location of the death of the
12511 register. */
12512 for (i = regno; i < endregno; i++)
12514 rsp = &reg_stat[i];
12516 if (insn)
12517 rsp->last_set = insn;
12519 rsp->last_set_value = 0;
12520 rsp->last_set_mode = VOIDmode;
12521 rsp->last_set_nonzero_bits = 0;
12522 rsp->last_set_sign_bit_copies = 0;
12523 rsp->last_death = 0;
12524 rsp->truncated_to_mode = VOIDmode;
12527 /* Mark registers that are being referenced in this value. */
12528 if (value)
12529 update_table_tick (value);
12531 /* Now update the status of each register being set.
12532 If someone is using this register in this block, set this register
12533 to invalid since we will get confused between the two lives in this
12534 basic block. This makes using this register always invalid. In cse, we
12535 scan the table to invalidate all entries using this register, but this
12536 is too much work for us. */
12538 for (i = regno; i < endregno; i++)
12540 rsp = &reg_stat[i];
12541 rsp->last_set_label = label_tick;
12542 if (!insn
12543 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12544 rsp->last_set_invalid = 1;
12545 else
12546 rsp->last_set_invalid = 0;
12549 /* The value being assigned might refer to X (like in "x++;"). In that
12550 case, we must replace it with (clobber (const_int 0)) to prevent
12551 infinite loops. */
12552 rsp = &reg_stat[regno];
12553 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12555 value = copy_rtx (value);
12556 if (!get_last_value_validate (&value, insn, label_tick, 1))
12557 value = 0;
12560 /* For the main register being modified, update the value, the mode, the
12561 nonzero bits, and the number of sign bit copies. */
12563 rsp->last_set_value = value;
12565 if (value)
12567 machine_mode mode = GET_MODE (reg);
12568 subst_low_luid = DF_INSN_LUID (insn);
12569 rsp->last_set_mode = mode;
12570 if (GET_MODE_CLASS (mode) == MODE_INT
12571 && HWI_COMPUTABLE_MODE_P (mode))
12572 mode = nonzero_bits_mode;
12573 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12574 rsp->last_set_sign_bit_copies
12575 = num_sign_bit_copies (value, GET_MODE (reg));
12579 /* Called via note_stores from record_dead_and_set_regs to handle one
12580 SET or CLOBBER in an insn. DATA is the instruction in which the
12581 set is occurring. */
12583 static void
12584 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12586 rtx_insn *record_dead_insn = (rtx_insn *) data;
12588 if (GET_CODE (dest) == SUBREG)
12589 dest = SUBREG_REG (dest);
12591 if (!record_dead_insn)
12593 if (REG_P (dest))
12594 record_value_for_reg (dest, NULL, NULL_RTX);
12595 return;
12598 if (REG_P (dest))
12600 /* If we are setting the whole register, we know its value. Otherwise
12601 show that we don't know the value. We can handle SUBREG in
12602 some cases. */
12603 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12604 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12605 else if (GET_CODE (setter) == SET
12606 && GET_CODE (SET_DEST (setter)) == SUBREG
12607 && SUBREG_REG (SET_DEST (setter)) == dest
12608 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12609 && subreg_lowpart_p (SET_DEST (setter)))
12610 record_value_for_reg (dest, record_dead_insn,
12611 gen_lowpart (GET_MODE (dest),
12612 SET_SRC (setter)));
12613 else
12614 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12616 else if (MEM_P (dest)
12617 /* Ignore pushes, they clobber nothing. */
12618 && ! push_operand (dest, GET_MODE (dest)))
12619 mem_last_set = DF_INSN_LUID (record_dead_insn);
12622 /* Update the records of when each REG was most recently set or killed
12623 for the things done by INSN. This is the last thing done in processing
12624 INSN in the combiner loop.
12626 We update reg_stat[], in particular fields last_set, last_set_value,
12627 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12628 last_death, and also the similar information mem_last_set (which insn
12629 most recently modified memory) and last_call_luid (which insn was the
12630 most recent subroutine call). */
12632 static void
12633 record_dead_and_set_regs (rtx_insn *insn)
12635 rtx link;
12636 unsigned int i;
12638 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12640 if (REG_NOTE_KIND (link) == REG_DEAD
12641 && REG_P (XEXP (link, 0)))
12643 unsigned int regno = REGNO (XEXP (link, 0));
12644 unsigned int endregno = END_REGNO (XEXP (link, 0));
12646 for (i = regno; i < endregno; i++)
12648 reg_stat_type *rsp;
12650 rsp = &reg_stat[i];
12651 rsp->last_death = insn;
12654 else if (REG_NOTE_KIND (link) == REG_INC)
12655 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12658 if (CALL_P (insn))
12660 hard_reg_set_iterator hrsi;
12661 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12663 reg_stat_type *rsp;
12665 rsp = &reg_stat[i];
12666 rsp->last_set_invalid = 1;
12667 rsp->last_set = insn;
12668 rsp->last_set_value = 0;
12669 rsp->last_set_mode = VOIDmode;
12670 rsp->last_set_nonzero_bits = 0;
12671 rsp->last_set_sign_bit_copies = 0;
12672 rsp->last_death = 0;
12673 rsp->truncated_to_mode = VOIDmode;
12676 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12678 /* We can't combine into a call pattern. Remember, though, that
12679 the return value register is set at this LUID. We could
12680 still replace a register with the return value from the
12681 wrong subroutine call! */
12682 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12684 else
12685 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12688 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12689 register present in the SUBREG, so for each such SUBREG go back and
12690 adjust nonzero and sign bit information of the registers that are
12691 known to have some zero/sign bits set.
12693 This is needed because when combine blows the SUBREGs away, the
12694 information on zero/sign bits is lost and further combines can be
12695 missed because of that. */
12697 static void
12698 record_promoted_value (rtx_insn *insn, rtx subreg)
12700 struct insn_link *links;
12701 rtx set;
12702 unsigned int regno = REGNO (SUBREG_REG (subreg));
12703 machine_mode mode = GET_MODE (subreg);
12705 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12706 return;
12708 for (links = LOG_LINKS (insn); links;)
12710 reg_stat_type *rsp;
12712 insn = links->insn;
12713 set = single_set (insn);
12715 if (! set || !REG_P (SET_DEST (set))
12716 || REGNO (SET_DEST (set)) != regno
12717 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12719 links = links->next;
12720 continue;
12723 rsp = &reg_stat[regno];
12724 if (rsp->last_set == insn)
12726 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12727 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12730 if (REG_P (SET_SRC (set)))
12732 regno = REGNO (SET_SRC (set));
12733 links = LOG_LINKS (insn);
12735 else
12736 break;
12740 /* Check if X, a register, is known to contain a value already
12741 truncated to MODE. In this case we can use a subreg to refer to
12742 the truncated value even though in the generic case we would need
12743 an explicit truncation. */
12745 static bool
12746 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12748 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12749 machine_mode truncated = rsp->truncated_to_mode;
12751 if (truncated == 0
12752 || rsp->truncation_label < label_tick_ebb_start)
12753 return false;
12754 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12755 return true;
12756 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12757 return true;
12758 return false;
12761 /* If X is a hard reg or a subreg record the mode that the register is
12762 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12763 to turn a truncate into a subreg using this information. Return true
12764 if traversing X is complete. */
12766 static bool
12767 record_truncated_value (rtx x)
12769 machine_mode truncated_mode;
12770 reg_stat_type *rsp;
12772 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12774 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12775 truncated_mode = GET_MODE (x);
12777 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12778 return true;
12780 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12781 return true;
12783 x = SUBREG_REG (x);
12785 /* ??? For hard-regs we now record everything. We might be able to
12786 optimize this using last_set_mode. */
12787 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12788 truncated_mode = GET_MODE (x);
12789 else
12790 return false;
12792 rsp = &reg_stat[REGNO (x)];
12793 if (rsp->truncated_to_mode == 0
12794 || rsp->truncation_label < label_tick_ebb_start
12795 || (GET_MODE_SIZE (truncated_mode)
12796 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12798 rsp->truncated_to_mode = truncated_mode;
12799 rsp->truncation_label = label_tick;
12802 return true;
12805 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12806 the modes they are used in. This can help truning TRUNCATEs into
12807 SUBREGs. */
12809 static void
12810 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12812 subrtx_var_iterator::array_type array;
12813 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12814 if (record_truncated_value (*iter))
12815 iter.skip_subrtxes ();
12818 /* Scan X for promoted SUBREGs. For each one found,
12819 note what it implies to the registers used in it. */
12821 static void
12822 check_promoted_subreg (rtx_insn *insn, rtx x)
12824 if (GET_CODE (x) == SUBREG
12825 && SUBREG_PROMOTED_VAR_P (x)
12826 && REG_P (SUBREG_REG (x)))
12827 record_promoted_value (insn, x);
12828 else
12830 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12831 int i, j;
12833 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12834 switch (format[i])
12836 case 'e':
12837 check_promoted_subreg (insn, XEXP (x, i));
12838 break;
12839 case 'V':
12840 case 'E':
12841 if (XVEC (x, i) != 0)
12842 for (j = 0; j < XVECLEN (x, i); j++)
12843 check_promoted_subreg (insn, XVECEXP (x, i, j));
12844 break;
12849 /* Verify that all the registers and memory references mentioned in *LOC are
12850 still valid. *LOC was part of a value set in INSN when label_tick was
12851 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12852 the invalid references with (clobber (const_int 0)) and return 1. This
12853 replacement is useful because we often can get useful information about
12854 the form of a value (e.g., if it was produced by a shift that always
12855 produces -1 or 0) even though we don't know exactly what registers it
12856 was produced from. */
12858 static int
12859 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12861 rtx x = *loc;
12862 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12863 int len = GET_RTX_LENGTH (GET_CODE (x));
12864 int i, j;
12866 if (REG_P (x))
12868 unsigned int regno = REGNO (x);
12869 unsigned int endregno = END_REGNO (x);
12870 unsigned int j;
12872 for (j = regno; j < endregno; j++)
12874 reg_stat_type *rsp = &reg_stat[j];
12875 if (rsp->last_set_invalid
12876 /* If this is a pseudo-register that was only set once and not
12877 live at the beginning of the function, it is always valid. */
12878 || (! (regno >= FIRST_PSEUDO_REGISTER
12879 && regno < reg_n_sets_max
12880 && REG_N_SETS (regno) == 1
12881 && (!REGNO_REG_SET_P
12882 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12883 regno)))
12884 && rsp->last_set_label > tick))
12886 if (replace)
12887 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12888 return replace;
12892 return 1;
12894 /* If this is a memory reference, make sure that there were no stores after
12895 it that might have clobbered the value. We don't have alias info, so we
12896 assume any store invalidates it. Moreover, we only have local UIDs, so
12897 we also assume that there were stores in the intervening basic blocks. */
12898 else if (MEM_P (x) && !MEM_READONLY_P (x)
12899 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12901 if (replace)
12902 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12903 return replace;
12906 for (i = 0; i < len; i++)
12908 if (fmt[i] == 'e')
12910 /* Check for identical subexpressions. If x contains
12911 identical subexpression we only have to traverse one of
12912 them. */
12913 if (i == 1 && ARITHMETIC_P (x))
12915 /* Note that at this point x0 has already been checked
12916 and found valid. */
12917 rtx x0 = XEXP (x, 0);
12918 rtx x1 = XEXP (x, 1);
12920 /* If x0 and x1 are identical then x is also valid. */
12921 if (x0 == x1)
12922 return 1;
12924 /* If x1 is identical to a subexpression of x0 then
12925 while checking x0, x1 has already been checked. Thus
12926 it is valid and so as x. */
12927 if (ARITHMETIC_P (x0)
12928 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12929 return 1;
12931 /* If x0 is identical to a subexpression of x1 then x is
12932 valid iff the rest of x1 is valid. */
12933 if (ARITHMETIC_P (x1)
12934 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12935 return
12936 get_last_value_validate (&XEXP (x1,
12937 x0 == XEXP (x1, 0) ? 1 : 0),
12938 insn, tick, replace);
12941 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12942 replace) == 0)
12943 return 0;
12945 else if (fmt[i] == 'E')
12946 for (j = 0; j < XVECLEN (x, i); j++)
12947 if (get_last_value_validate (&XVECEXP (x, i, j),
12948 insn, tick, replace) == 0)
12949 return 0;
12952 /* If we haven't found a reason for it to be invalid, it is valid. */
12953 return 1;
12956 /* Get the last value assigned to X, if known. Some registers
12957 in the value may be replaced with (clobber (const_int 0)) if their value
12958 is known longer known reliably. */
12960 static rtx
12961 get_last_value (const_rtx x)
12963 unsigned int regno;
12964 rtx value;
12965 reg_stat_type *rsp;
12967 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12968 then convert it to the desired mode. If this is a paradoxical SUBREG,
12969 we cannot predict what values the "extra" bits might have. */
12970 if (GET_CODE (x) == SUBREG
12971 && subreg_lowpart_p (x)
12972 && !paradoxical_subreg_p (x)
12973 && (value = get_last_value (SUBREG_REG (x))) != 0)
12974 return gen_lowpart (GET_MODE (x), value);
12976 if (!REG_P (x))
12977 return 0;
12979 regno = REGNO (x);
12980 rsp = &reg_stat[regno];
12981 value = rsp->last_set_value;
12983 /* If we don't have a value, or if it isn't for this basic block and
12984 it's either a hard register, set more than once, or it's a live
12985 at the beginning of the function, return 0.
12987 Because if it's not live at the beginning of the function then the reg
12988 is always set before being used (is never used without being set).
12989 And, if it's set only once, and it's always set before use, then all
12990 uses must have the same last value, even if it's not from this basic
12991 block. */
12993 if (value == 0
12994 || (rsp->last_set_label < label_tick_ebb_start
12995 && (regno < FIRST_PSEUDO_REGISTER
12996 || regno >= reg_n_sets_max
12997 || REG_N_SETS (regno) != 1
12998 || REGNO_REG_SET_P
12999 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13000 return 0;
13002 /* If the value was set in a later insn than the ones we are processing,
13003 we can't use it even if the register was only set once. */
13004 if (rsp->last_set_label == label_tick
13005 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13006 return 0;
13008 /* If the value has all its registers valid, return it. */
13009 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13010 return value;
13012 /* Otherwise, make a copy and replace any invalid register with
13013 (clobber (const_int 0)). If that fails for some reason, return 0. */
13015 value = copy_rtx (value);
13016 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13017 return value;
13019 return 0;
13022 /* Return nonzero if expression X refers to a REG or to memory
13023 that is set in an instruction more recent than FROM_LUID. */
13025 static int
13026 use_crosses_set_p (const_rtx x, int from_luid)
13028 const char *fmt;
13029 int i;
13030 enum rtx_code code = GET_CODE (x);
13032 if (code == REG)
13034 unsigned int regno = REGNO (x);
13035 unsigned endreg = END_REGNO (x);
13037 #ifdef PUSH_ROUNDING
13038 /* Don't allow uses of the stack pointer to be moved,
13039 because we don't know whether the move crosses a push insn. */
13040 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13041 return 1;
13042 #endif
13043 for (; regno < endreg; regno++)
13045 reg_stat_type *rsp = &reg_stat[regno];
13046 if (rsp->last_set
13047 && rsp->last_set_label == label_tick
13048 && DF_INSN_LUID (rsp->last_set) > from_luid)
13049 return 1;
13051 return 0;
13054 if (code == MEM && mem_last_set > from_luid)
13055 return 1;
13057 fmt = GET_RTX_FORMAT (code);
13059 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13061 if (fmt[i] == 'E')
13063 int j;
13064 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13065 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13066 return 1;
13068 else if (fmt[i] == 'e'
13069 && use_crosses_set_p (XEXP (x, i), from_luid))
13070 return 1;
13072 return 0;
13075 /* Define three variables used for communication between the following
13076 routines. */
13078 static unsigned int reg_dead_regno, reg_dead_endregno;
13079 static int reg_dead_flag;
13081 /* Function called via note_stores from reg_dead_at_p.
13083 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13084 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13086 static void
13087 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13089 unsigned int regno, endregno;
13091 if (!REG_P (dest))
13092 return;
13094 regno = REGNO (dest);
13095 endregno = END_REGNO (dest);
13096 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13097 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13100 /* Return nonzero if REG is known to be dead at INSN.
13102 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13103 referencing REG, it is dead. If we hit a SET referencing REG, it is
13104 live. Otherwise, see if it is live or dead at the start of the basic
13105 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13106 must be assumed to be always live. */
13108 static int
13109 reg_dead_at_p (rtx reg, rtx_insn *insn)
13111 basic_block block;
13112 unsigned int i;
13114 /* Set variables for reg_dead_at_p_1. */
13115 reg_dead_regno = REGNO (reg);
13116 reg_dead_endregno = END_REGNO (reg);
13118 reg_dead_flag = 0;
13120 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13121 we allow the machine description to decide whether use-and-clobber
13122 patterns are OK. */
13123 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13125 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13126 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13127 return 0;
13130 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13131 beginning of basic block. */
13132 block = BLOCK_FOR_INSN (insn);
13133 for (;;)
13135 if (INSN_P (insn))
13137 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13138 return 1;
13140 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13141 if (reg_dead_flag)
13142 return reg_dead_flag == 1 ? 1 : 0;
13144 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13145 return 1;
13148 if (insn == BB_HEAD (block))
13149 break;
13151 insn = PREV_INSN (insn);
13154 /* Look at live-in sets for the basic block that we were in. */
13155 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13156 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13157 return 0;
13159 return 1;
13162 /* Note hard registers in X that are used. */
13164 static void
13165 mark_used_regs_combine (rtx x)
13167 RTX_CODE code = GET_CODE (x);
13168 unsigned int regno;
13169 int i;
13171 switch (code)
13173 case LABEL_REF:
13174 case SYMBOL_REF:
13175 case CONST:
13176 CASE_CONST_ANY:
13177 case PC:
13178 case ADDR_VEC:
13179 case ADDR_DIFF_VEC:
13180 case ASM_INPUT:
13181 /* CC0 must die in the insn after it is set, so we don't need to take
13182 special note of it here. */
13183 case CC0:
13184 return;
13186 case CLOBBER:
13187 /* If we are clobbering a MEM, mark any hard registers inside the
13188 address as used. */
13189 if (MEM_P (XEXP (x, 0)))
13190 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13191 return;
13193 case REG:
13194 regno = REGNO (x);
13195 /* A hard reg in a wide mode may really be multiple registers.
13196 If so, mark all of them just like the first. */
13197 if (regno < FIRST_PSEUDO_REGISTER)
13199 /* None of this applies to the stack, frame or arg pointers. */
13200 if (regno == STACK_POINTER_REGNUM
13201 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13202 || regno == HARD_FRAME_POINTER_REGNUM
13203 #endif
13204 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13205 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13206 #endif
13207 || regno == FRAME_POINTER_REGNUM)
13208 return;
13210 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13212 return;
13214 case SET:
13216 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13217 the address. */
13218 rtx testreg = SET_DEST (x);
13220 while (GET_CODE (testreg) == SUBREG
13221 || GET_CODE (testreg) == ZERO_EXTRACT
13222 || GET_CODE (testreg) == STRICT_LOW_PART)
13223 testreg = XEXP (testreg, 0);
13225 if (MEM_P (testreg))
13226 mark_used_regs_combine (XEXP (testreg, 0));
13228 mark_used_regs_combine (SET_SRC (x));
13230 return;
13232 default:
13233 break;
13236 /* Recursively scan the operands of this expression. */
13239 const char *fmt = GET_RTX_FORMAT (code);
13241 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13243 if (fmt[i] == 'e')
13244 mark_used_regs_combine (XEXP (x, i));
13245 else if (fmt[i] == 'E')
13247 int j;
13249 for (j = 0; j < XVECLEN (x, i); j++)
13250 mark_used_regs_combine (XVECEXP (x, i, j));
13256 /* Remove register number REGNO from the dead registers list of INSN.
13258 Return the note used to record the death, if there was one. */
13261 remove_death (unsigned int regno, rtx_insn *insn)
13263 rtx note = find_regno_note (insn, REG_DEAD, regno);
13265 if (note)
13266 remove_note (insn, note);
13268 return note;
13271 /* For each register (hardware or pseudo) used within expression X, if its
13272 death is in an instruction with luid between FROM_LUID (inclusive) and
13273 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13274 list headed by PNOTES.
13276 That said, don't move registers killed by maybe_kill_insn.
13278 This is done when X is being merged by combination into TO_INSN. These
13279 notes will then be distributed as needed. */
13281 static void
13282 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13283 rtx *pnotes)
13285 const char *fmt;
13286 int len, i;
13287 enum rtx_code code = GET_CODE (x);
13289 if (code == REG)
13291 unsigned int regno = REGNO (x);
13292 rtx_insn *where_dead = reg_stat[regno].last_death;
13294 /* Don't move the register if it gets killed in between from and to. */
13295 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13296 && ! reg_referenced_p (x, maybe_kill_insn))
13297 return;
13299 if (where_dead
13300 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13301 && DF_INSN_LUID (where_dead) >= from_luid
13302 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13304 rtx note = remove_death (regno, where_dead);
13306 /* It is possible for the call above to return 0. This can occur
13307 when last_death points to I2 or I1 that we combined with.
13308 In that case make a new note.
13310 We must also check for the case where X is a hard register
13311 and NOTE is a death note for a range of hard registers
13312 including X. In that case, we must put REG_DEAD notes for
13313 the remaining registers in place of NOTE. */
13315 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13316 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13317 > GET_MODE_SIZE (GET_MODE (x))))
13319 unsigned int deadregno = REGNO (XEXP (note, 0));
13320 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13321 unsigned int ourend = END_HARD_REGNO (x);
13322 unsigned int i;
13324 for (i = deadregno; i < deadend; i++)
13325 if (i < regno || i >= ourend)
13326 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13329 /* If we didn't find any note, or if we found a REG_DEAD note that
13330 covers only part of the given reg, and we have a multi-reg hard
13331 register, then to be safe we must check for REG_DEAD notes
13332 for each register other than the first. They could have
13333 their own REG_DEAD notes lying around. */
13334 else if ((note == 0
13335 || (note != 0
13336 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13337 < GET_MODE_SIZE (GET_MODE (x)))))
13338 && regno < FIRST_PSEUDO_REGISTER
13339 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13341 unsigned int ourend = END_HARD_REGNO (x);
13342 unsigned int i, offset;
13343 rtx oldnotes = 0;
13345 if (note)
13346 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13347 else
13348 offset = 1;
13350 for (i = regno + offset; i < ourend; i++)
13351 move_deaths (regno_reg_rtx[i],
13352 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13355 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13357 XEXP (note, 1) = *pnotes;
13358 *pnotes = note;
13360 else
13361 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13364 return;
13367 else if (GET_CODE (x) == SET)
13369 rtx dest = SET_DEST (x);
13371 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13373 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13374 that accesses one word of a multi-word item, some
13375 piece of everything register in the expression is used by
13376 this insn, so remove any old death. */
13377 /* ??? So why do we test for equality of the sizes? */
13379 if (GET_CODE (dest) == ZERO_EXTRACT
13380 || GET_CODE (dest) == STRICT_LOW_PART
13381 || (GET_CODE (dest) == SUBREG
13382 && (((GET_MODE_SIZE (GET_MODE (dest))
13383 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13384 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13385 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13387 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13388 return;
13391 /* If this is some other SUBREG, we know it replaces the entire
13392 value, so use that as the destination. */
13393 if (GET_CODE (dest) == SUBREG)
13394 dest = SUBREG_REG (dest);
13396 /* If this is a MEM, adjust deaths of anything used in the address.
13397 For a REG (the only other possibility), the entire value is
13398 being replaced so the old value is not used in this insn. */
13400 if (MEM_P (dest))
13401 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13402 to_insn, pnotes);
13403 return;
13406 else if (GET_CODE (x) == CLOBBER)
13407 return;
13409 len = GET_RTX_LENGTH (code);
13410 fmt = GET_RTX_FORMAT (code);
13412 for (i = 0; i < len; i++)
13414 if (fmt[i] == 'E')
13416 int j;
13417 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13418 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13419 to_insn, pnotes);
13421 else if (fmt[i] == 'e')
13422 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13426 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13427 pattern of an insn. X must be a REG. */
13429 static int
13430 reg_bitfield_target_p (rtx x, rtx body)
13432 int i;
13434 if (GET_CODE (body) == SET)
13436 rtx dest = SET_DEST (body);
13437 rtx target;
13438 unsigned int regno, tregno, endregno, endtregno;
13440 if (GET_CODE (dest) == ZERO_EXTRACT)
13441 target = XEXP (dest, 0);
13442 else if (GET_CODE (dest) == STRICT_LOW_PART)
13443 target = SUBREG_REG (XEXP (dest, 0));
13444 else
13445 return 0;
13447 if (GET_CODE (target) == SUBREG)
13448 target = SUBREG_REG (target);
13450 if (!REG_P (target))
13451 return 0;
13453 tregno = REGNO (target), regno = REGNO (x);
13454 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13455 return target == x;
13457 endtregno = end_hard_regno (GET_MODE (target), tregno);
13458 endregno = end_hard_regno (GET_MODE (x), regno);
13460 return endregno > tregno && regno < endtregno;
13463 else if (GET_CODE (body) == PARALLEL)
13464 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13465 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13466 return 1;
13468 return 0;
13471 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13472 as appropriate. I3 and I2 are the insns resulting from the combination
13473 insns including FROM (I2 may be zero).
13475 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13476 not need REG_DEAD notes because they are being substituted for. This
13477 saves searching in the most common cases.
13479 Each note in the list is either ignored or placed on some insns, depending
13480 on the type of note. */
13482 static void
13483 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13484 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13486 rtx note, next_note;
13487 rtx tem_note;
13488 rtx_insn *tem_insn;
13490 for (note = notes; note; note = next_note)
13492 rtx_insn *place = 0, *place2 = 0;
13494 next_note = XEXP (note, 1);
13495 switch (REG_NOTE_KIND (note))
13497 case REG_BR_PROB:
13498 case REG_BR_PRED:
13499 /* Doesn't matter much where we put this, as long as it's somewhere.
13500 It is preferable to keep these notes on branches, which is most
13501 likely to be i3. */
13502 place = i3;
13503 break;
13505 case REG_NON_LOCAL_GOTO:
13506 if (JUMP_P (i3))
13507 place = i3;
13508 else
13510 gcc_assert (i2 && JUMP_P (i2));
13511 place = i2;
13513 break;
13515 case REG_EH_REGION:
13516 /* These notes must remain with the call or trapping instruction. */
13517 if (CALL_P (i3))
13518 place = i3;
13519 else if (i2 && CALL_P (i2))
13520 place = i2;
13521 else
13523 gcc_assert (cfun->can_throw_non_call_exceptions);
13524 if (may_trap_p (i3))
13525 place = i3;
13526 else if (i2 && may_trap_p (i2))
13527 place = i2;
13528 /* ??? Otherwise assume we've combined things such that we
13529 can now prove that the instructions can't trap. Drop the
13530 note in this case. */
13532 break;
13534 case REG_ARGS_SIZE:
13535 /* ??? How to distribute between i3-i1. Assume i3 contains the
13536 entire adjustment. Assert i3 contains at least some adjust. */
13537 if (!noop_move_p (i3))
13539 int old_size, args_size = INTVAL (XEXP (note, 0));
13540 /* fixup_args_size_notes looks at REG_NORETURN note,
13541 so ensure the note is placed there first. */
13542 if (CALL_P (i3))
13544 rtx *np;
13545 for (np = &next_note; *np; np = &XEXP (*np, 1))
13546 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13548 rtx n = *np;
13549 *np = XEXP (n, 1);
13550 XEXP (n, 1) = REG_NOTES (i3);
13551 REG_NOTES (i3) = n;
13552 break;
13555 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13556 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13557 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13558 gcc_assert (old_size != args_size
13559 || (CALL_P (i3)
13560 && !ACCUMULATE_OUTGOING_ARGS
13561 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13563 break;
13565 case REG_NORETURN:
13566 case REG_SETJMP:
13567 case REG_TM:
13568 case REG_CALL_DECL:
13569 /* These notes must remain with the call. It should not be
13570 possible for both I2 and I3 to be a call. */
13571 if (CALL_P (i3))
13572 place = i3;
13573 else
13575 gcc_assert (i2 && CALL_P (i2));
13576 place = i2;
13578 break;
13580 case REG_UNUSED:
13581 /* Any clobbers for i3 may still exist, and so we must process
13582 REG_UNUSED notes from that insn.
13584 Any clobbers from i2 or i1 can only exist if they were added by
13585 recog_for_combine. In that case, recog_for_combine created the
13586 necessary REG_UNUSED notes. Trying to keep any original
13587 REG_UNUSED notes from these insns can cause incorrect output
13588 if it is for the same register as the original i3 dest.
13589 In that case, we will notice that the register is set in i3,
13590 and then add a REG_UNUSED note for the destination of i3, which
13591 is wrong. However, it is possible to have REG_UNUSED notes from
13592 i2 or i1 for register which were both used and clobbered, so
13593 we keep notes from i2 or i1 if they will turn into REG_DEAD
13594 notes. */
13596 /* If this register is set or clobbered in I3, put the note there
13597 unless there is one already. */
13598 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13600 if (from_insn != i3)
13601 break;
13603 if (! (REG_P (XEXP (note, 0))
13604 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13605 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13606 place = i3;
13608 /* Otherwise, if this register is used by I3, then this register
13609 now dies here, so we must put a REG_DEAD note here unless there
13610 is one already. */
13611 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13612 && ! (REG_P (XEXP (note, 0))
13613 ? find_regno_note (i3, REG_DEAD,
13614 REGNO (XEXP (note, 0)))
13615 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13617 PUT_REG_NOTE_KIND (note, REG_DEAD);
13618 place = i3;
13620 break;
13622 case REG_EQUAL:
13623 case REG_EQUIV:
13624 case REG_NOALIAS:
13625 /* These notes say something about results of an insn. We can
13626 only support them if they used to be on I3 in which case they
13627 remain on I3. Otherwise they are ignored.
13629 If the note refers to an expression that is not a constant, we
13630 must also ignore the note since we cannot tell whether the
13631 equivalence is still true. It might be possible to do
13632 slightly better than this (we only have a problem if I2DEST
13633 or I1DEST is present in the expression), but it doesn't
13634 seem worth the trouble. */
13636 if (from_insn == i3
13637 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13638 place = i3;
13639 break;
13641 case REG_INC:
13642 /* These notes say something about how a register is used. They must
13643 be present on any use of the register in I2 or I3. */
13644 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13645 place = i3;
13647 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13649 if (place)
13650 place2 = i2;
13651 else
13652 place = i2;
13654 break;
13656 case REG_LABEL_TARGET:
13657 case REG_LABEL_OPERAND:
13658 /* This can show up in several ways -- either directly in the
13659 pattern, or hidden off in the constant pool with (or without?)
13660 a REG_EQUAL note. */
13661 /* ??? Ignore the without-reg_equal-note problem for now. */
13662 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13663 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13664 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13665 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13666 place = i3;
13668 if (i2
13669 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13670 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13671 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13672 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13674 if (place)
13675 place2 = i2;
13676 else
13677 place = i2;
13680 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13681 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13682 there. */
13683 if (place && JUMP_P (place)
13684 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13685 && (JUMP_LABEL (place) == NULL
13686 || JUMP_LABEL (place) == XEXP (note, 0)))
13688 rtx label = JUMP_LABEL (place);
13690 if (!label)
13691 JUMP_LABEL (place) = XEXP (note, 0);
13692 else if (LABEL_P (label))
13693 LABEL_NUSES (label)--;
13696 if (place2 && JUMP_P (place2)
13697 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13698 && (JUMP_LABEL (place2) == NULL
13699 || JUMP_LABEL (place2) == XEXP (note, 0)))
13701 rtx label = JUMP_LABEL (place2);
13703 if (!label)
13704 JUMP_LABEL (place2) = XEXP (note, 0);
13705 else if (LABEL_P (label))
13706 LABEL_NUSES (label)--;
13707 place2 = 0;
13709 break;
13711 case REG_NONNEG:
13712 /* This note says something about the value of a register prior
13713 to the execution of an insn. It is too much trouble to see
13714 if the note is still correct in all situations. It is better
13715 to simply delete it. */
13716 break;
13718 case REG_DEAD:
13719 /* If we replaced the right hand side of FROM_INSN with a
13720 REG_EQUAL note, the original use of the dying register
13721 will not have been combined into I3 and I2. In such cases,
13722 FROM_INSN is guaranteed to be the first of the combined
13723 instructions, so we simply need to search back before
13724 FROM_INSN for the previous use or set of this register,
13725 then alter the notes there appropriately.
13727 If the register is used as an input in I3, it dies there.
13728 Similarly for I2, if it is nonzero and adjacent to I3.
13730 If the register is not used as an input in either I3 or I2
13731 and it is not one of the registers we were supposed to eliminate,
13732 there are two possibilities. We might have a non-adjacent I2
13733 or we might have somehow eliminated an additional register
13734 from a computation. For example, we might have had A & B where
13735 we discover that B will always be zero. In this case we will
13736 eliminate the reference to A.
13738 In both cases, we must search to see if we can find a previous
13739 use of A and put the death note there. */
13741 if (from_insn
13742 && from_insn == i2mod
13743 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13744 tem_insn = from_insn;
13745 else
13747 if (from_insn
13748 && CALL_P (from_insn)
13749 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13750 place = from_insn;
13751 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13752 place = i3;
13753 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13754 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13755 place = i2;
13756 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13757 && !(i2mod
13758 && reg_overlap_mentioned_p (XEXP (note, 0),
13759 i2mod_old_rhs)))
13760 || rtx_equal_p (XEXP (note, 0), elim_i1)
13761 || rtx_equal_p (XEXP (note, 0), elim_i0))
13762 break;
13763 tem_insn = i3;
13764 /* If the new I2 sets the same register that is marked dead
13765 in the note, the note now should not be put on I2, as the
13766 note refers to a previous incarnation of the reg. */
13767 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
13768 tem_insn = i2;
13771 if (place == 0)
13773 basic_block bb = this_basic_block;
13775 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13777 if (!NONDEBUG_INSN_P (tem_insn))
13779 if (tem_insn == BB_HEAD (bb))
13780 break;
13781 continue;
13784 /* If the register is being set at TEM_INSN, see if that is all
13785 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13786 into a REG_UNUSED note instead. Don't delete sets to
13787 global register vars. */
13788 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13789 || !global_regs[REGNO (XEXP (note, 0))])
13790 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13792 rtx set = single_set (tem_insn);
13793 rtx inner_dest = 0;
13794 rtx_insn *cc0_setter = NULL;
13796 if (set != 0)
13797 for (inner_dest = SET_DEST (set);
13798 (GET_CODE (inner_dest) == STRICT_LOW_PART
13799 || GET_CODE (inner_dest) == SUBREG
13800 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13801 inner_dest = XEXP (inner_dest, 0))
13804 /* Verify that it was the set, and not a clobber that
13805 modified the register.
13807 CC0 targets must be careful to maintain setter/user
13808 pairs. If we cannot delete the setter due to side
13809 effects, mark the user with an UNUSED note instead
13810 of deleting it. */
13812 if (set != 0 && ! side_effects_p (SET_SRC (set))
13813 && rtx_equal_p (XEXP (note, 0), inner_dest)
13814 #if HAVE_cc0
13815 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13816 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13817 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13818 #endif
13821 /* Move the notes and links of TEM_INSN elsewhere.
13822 This might delete other dead insns recursively.
13823 First set the pattern to something that won't use
13824 any register. */
13825 rtx old_notes = REG_NOTES (tem_insn);
13827 PATTERN (tem_insn) = pc_rtx;
13828 REG_NOTES (tem_insn) = NULL;
13830 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13831 NULL_RTX, NULL_RTX, NULL_RTX);
13832 distribute_links (LOG_LINKS (tem_insn));
13834 SET_INSN_DELETED (tem_insn);
13835 if (tem_insn == i2)
13836 i2 = NULL;
13838 /* Delete the setter too. */
13839 if (cc0_setter)
13841 PATTERN (cc0_setter) = pc_rtx;
13842 old_notes = REG_NOTES (cc0_setter);
13843 REG_NOTES (cc0_setter) = NULL;
13845 distribute_notes (old_notes, cc0_setter,
13846 cc0_setter, NULL,
13847 NULL_RTX, NULL_RTX, NULL_RTX);
13848 distribute_links (LOG_LINKS (cc0_setter));
13850 SET_INSN_DELETED (cc0_setter);
13851 if (cc0_setter == i2)
13852 i2 = NULL;
13855 else
13857 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13859 /* If there isn't already a REG_UNUSED note, put one
13860 here. Do not place a REG_DEAD note, even if
13861 the register is also used here; that would not
13862 match the algorithm used in lifetime analysis
13863 and can cause the consistency check in the
13864 scheduler to fail. */
13865 if (! find_regno_note (tem_insn, REG_UNUSED,
13866 REGNO (XEXP (note, 0))))
13867 place = tem_insn;
13868 break;
13871 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13872 || (CALL_P (tem_insn)
13873 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13875 place = tem_insn;
13877 /* If we are doing a 3->2 combination, and we have a
13878 register which formerly died in i3 and was not used
13879 by i2, which now no longer dies in i3 and is used in
13880 i2 but does not die in i2, and place is between i2
13881 and i3, then we may need to move a link from place to
13882 i2. */
13883 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13884 && from_insn
13885 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13886 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13888 struct insn_link *links = LOG_LINKS (place);
13889 LOG_LINKS (place) = NULL;
13890 distribute_links (links);
13892 break;
13895 if (tem_insn == BB_HEAD (bb))
13896 break;
13901 /* If the register is set or already dead at PLACE, we needn't do
13902 anything with this note if it is still a REG_DEAD note.
13903 We check here if it is set at all, not if is it totally replaced,
13904 which is what `dead_or_set_p' checks, so also check for it being
13905 set partially. */
13907 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13909 unsigned int regno = REGNO (XEXP (note, 0));
13910 reg_stat_type *rsp = &reg_stat[regno];
13912 if (dead_or_set_p (place, XEXP (note, 0))
13913 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13915 /* Unless the register previously died in PLACE, clear
13916 last_death. [I no longer understand why this is
13917 being done.] */
13918 if (rsp->last_death != place)
13919 rsp->last_death = 0;
13920 place = 0;
13922 else
13923 rsp->last_death = place;
13925 /* If this is a death note for a hard reg that is occupying
13926 multiple registers, ensure that we are still using all
13927 parts of the object. If we find a piece of the object
13928 that is unused, we must arrange for an appropriate REG_DEAD
13929 note to be added for it. However, we can't just emit a USE
13930 and tag the note to it, since the register might actually
13931 be dead; so we recourse, and the recursive call then finds
13932 the previous insn that used this register. */
13934 if (place && regno < FIRST_PSEUDO_REGISTER
13935 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13937 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13938 bool all_used = true;
13939 unsigned int i;
13941 for (i = regno; i < endregno; i++)
13942 if ((! refers_to_regno_p (i, PATTERN (place))
13943 && ! find_regno_fusage (place, USE, i))
13944 || dead_or_set_regno_p (place, i))
13946 all_used = false;
13947 break;
13950 if (! all_used)
13952 /* Put only REG_DEAD notes for pieces that are
13953 not already dead or set. */
13955 for (i = regno; i < endregno;
13956 i += hard_regno_nregs[i][reg_raw_mode[i]])
13958 rtx piece = regno_reg_rtx[i];
13959 basic_block bb = this_basic_block;
13961 if (! dead_or_set_p (place, piece)
13962 && ! reg_bitfield_target_p (piece,
13963 PATTERN (place)))
13965 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13966 NULL_RTX);
13968 distribute_notes (new_note, place, place,
13969 NULL, NULL_RTX, NULL_RTX,
13970 NULL_RTX);
13972 else if (! refers_to_regno_p (i, PATTERN (place))
13973 && ! find_regno_fusage (place, USE, i))
13974 for (tem_insn = PREV_INSN (place); ;
13975 tem_insn = PREV_INSN (tem_insn))
13977 if (!NONDEBUG_INSN_P (tem_insn))
13979 if (tem_insn == BB_HEAD (bb))
13980 break;
13981 continue;
13983 if (dead_or_set_p (tem_insn, piece)
13984 || reg_bitfield_target_p (piece,
13985 PATTERN (tem_insn)))
13987 add_reg_note (tem_insn, REG_UNUSED, piece);
13988 break;
13993 place = 0;
13997 break;
13999 default:
14000 /* Any other notes should not be present at this point in the
14001 compilation. */
14002 gcc_unreachable ();
14005 if (place)
14007 XEXP (note, 1) = REG_NOTES (place);
14008 REG_NOTES (place) = note;
14011 if (place2)
14012 add_shallow_copy_of_reg_note (place2, note);
14016 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14017 I3, I2, and I1 to new locations. This is also called to add a link
14018 pointing at I3 when I3's destination is changed. */
14020 static void
14021 distribute_links (struct insn_link *links)
14023 struct insn_link *link, *next_link;
14025 for (link = links; link; link = next_link)
14027 rtx_insn *place = 0;
14028 rtx_insn *insn;
14029 rtx set, reg;
14031 next_link = link->next;
14033 /* If the insn that this link points to is a NOTE, ignore it. */
14034 if (NOTE_P (link->insn))
14035 continue;
14037 set = 0;
14038 rtx pat = PATTERN (link->insn);
14039 if (GET_CODE (pat) == SET)
14040 set = pat;
14041 else if (GET_CODE (pat) == PARALLEL)
14043 int i;
14044 for (i = 0; i < XVECLEN (pat, 0); i++)
14046 set = XVECEXP (pat, 0, i);
14047 if (GET_CODE (set) != SET)
14048 continue;
14050 reg = SET_DEST (set);
14051 while (GET_CODE (reg) == ZERO_EXTRACT
14052 || GET_CODE (reg) == STRICT_LOW_PART
14053 || GET_CODE (reg) == SUBREG)
14054 reg = XEXP (reg, 0);
14056 if (!REG_P (reg))
14057 continue;
14059 if (REGNO (reg) == link->regno)
14060 break;
14062 if (i == XVECLEN (pat, 0))
14063 continue;
14065 else
14066 continue;
14068 reg = SET_DEST (set);
14070 while (GET_CODE (reg) == ZERO_EXTRACT
14071 || GET_CODE (reg) == STRICT_LOW_PART
14072 || GET_CODE (reg) == SUBREG)
14073 reg = XEXP (reg, 0);
14075 /* A LOG_LINK is defined as being placed on the first insn that uses
14076 a register and points to the insn that sets the register. Start
14077 searching at the next insn after the target of the link and stop
14078 when we reach a set of the register or the end of the basic block.
14080 Note that this correctly handles the link that used to point from
14081 I3 to I2. Also note that not much searching is typically done here
14082 since most links don't point very far away. */
14084 for (insn = NEXT_INSN (link->insn);
14085 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14086 || BB_HEAD (this_basic_block->next_bb) != insn));
14087 insn = NEXT_INSN (insn))
14088 if (DEBUG_INSN_P (insn))
14089 continue;
14090 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14092 if (reg_referenced_p (reg, PATTERN (insn)))
14093 place = insn;
14094 break;
14096 else if (CALL_P (insn)
14097 && find_reg_fusage (insn, USE, reg))
14099 place = insn;
14100 break;
14102 else if (INSN_P (insn) && reg_set_p (reg, insn))
14103 break;
14105 /* If we found a place to put the link, place it there unless there
14106 is already a link to the same insn as LINK at that point. */
14108 if (place)
14110 struct insn_link *link2;
14112 FOR_EACH_LOG_LINK (link2, place)
14113 if (link2->insn == link->insn && link2->regno == link->regno)
14114 break;
14116 if (link2 == NULL)
14118 link->next = LOG_LINKS (place);
14119 LOG_LINKS (place) = link;
14121 /* Set added_links_insn to the earliest insn we added a
14122 link to. */
14123 if (added_links_insn == 0
14124 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14125 added_links_insn = place;
14131 /* Check for any register or memory mentioned in EQUIV that is not
14132 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14133 of EXPR where some registers may have been replaced by constants. */
14135 static bool
14136 unmentioned_reg_p (rtx equiv, rtx expr)
14138 subrtx_iterator::array_type array;
14139 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14141 const_rtx x = *iter;
14142 if ((REG_P (x) || MEM_P (x))
14143 && !reg_mentioned_p (x, expr))
14144 return true;
14146 return false;
14149 DEBUG_FUNCTION void
14150 dump_combine_stats (FILE *file)
14152 fprintf
14153 (file,
14154 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14155 combine_attempts, combine_merges, combine_extras, combine_successes);
14158 void
14159 dump_combine_total_stats (FILE *file)
14161 fprintf
14162 (file,
14163 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14164 total_attempts, total_merges, total_extras, total_successes);
14167 /* Try combining insns through substitution. */
14168 static unsigned int
14169 rest_of_handle_combine (void)
14171 int rebuild_jump_labels_after_combine;
14173 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14174 df_note_add_problem ();
14175 df_analyze ();
14177 regstat_init_n_sets_and_refs ();
14178 reg_n_sets_max = max_reg_num ();
14180 rebuild_jump_labels_after_combine
14181 = combine_instructions (get_insns (), max_reg_num ());
14183 /* Combining insns may have turned an indirect jump into a
14184 direct jump. Rebuild the JUMP_LABEL fields of jumping
14185 instructions. */
14186 if (rebuild_jump_labels_after_combine)
14188 timevar_push (TV_JUMP);
14189 rebuild_jump_labels (get_insns ());
14190 cleanup_cfg (0);
14191 timevar_pop (TV_JUMP);
14194 regstat_free_n_sets_and_refs ();
14195 return 0;
14198 namespace {
14200 const pass_data pass_data_combine =
14202 RTL_PASS, /* type */
14203 "combine", /* name */
14204 OPTGROUP_NONE, /* optinfo_flags */
14205 TV_COMBINE, /* tv_id */
14206 PROP_cfglayout, /* properties_required */
14207 0, /* properties_provided */
14208 0, /* properties_destroyed */
14209 0, /* todo_flags_start */
14210 TODO_df_finish, /* todo_flags_finish */
14213 class pass_combine : public rtl_opt_pass
14215 public:
14216 pass_combine (gcc::context *ctxt)
14217 : rtl_opt_pass (pass_data_combine, ctxt)
14220 /* opt_pass methods: */
14221 virtual bool gate (function *) { return (optimize > 0); }
14222 virtual unsigned int execute (function *)
14224 return rest_of_handle_combine ();
14227 }; // class pass_combine
14229 } // anon namespace
14231 rtl_opt_pass *
14232 make_pass_combine (gcc::context *ctxt)
14234 return new pass_combine (ctxt);