PR sanitizer/65081
[official-gcc.git] / gcc / combine.c
blobf779117cd7174f463c4eccd88fdb0a9f14a1f311
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);
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 #ifdef HAVE_cc0
690 if (dest == cc0_rtx)
692 next = NEXT_INSN (insn);
693 if (next == 0
694 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
695 return 0;
697 result = find_single_use_1 (dest, &PATTERN (next));
698 if (result && ploc)
699 *ploc = next;
700 return result;
702 #endif
704 if (!REG_P (dest))
705 return 0;
707 bb = BLOCK_FOR_INSN (insn);
708 for (next = NEXT_INSN (insn);
709 next && BLOCK_FOR_INSN (next) == bb;
710 next = NEXT_INSN (next))
711 if (INSN_P (next) && dead_or_set_p (next, dest))
713 FOR_EACH_LOG_LINK (link, next)
714 if (link->insn == insn && link->regno == REGNO (dest))
715 break;
717 if (link)
719 result = find_single_use_1 (dest, &PATTERN (next));
720 if (ploc)
721 *ploc = next;
722 return result;
726 return 0;
729 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
730 insn. The substitution can be undone by undo_all. If INTO is already
731 set to NEWVAL, do not record this change. Because computing NEWVAL might
732 also call SUBST, we have to compute it before we put anything into
733 the undo table. */
735 static void
736 do_SUBST (rtx *into, rtx newval)
738 struct undo *buf;
739 rtx oldval = *into;
741 if (oldval == newval)
742 return;
744 /* We'd like to catch as many invalid transformations here as
745 possible. Unfortunately, there are way too many mode changes
746 that are perfectly valid, so we'd waste too much effort for
747 little gain doing the checks here. Focus on catching invalid
748 transformations involving integer constants. */
749 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
750 && CONST_INT_P (newval))
752 /* Sanity check that we're replacing oldval with a CONST_INT
753 that is a valid sign-extension for the original mode. */
754 gcc_assert (INTVAL (newval)
755 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
757 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
758 CONST_INT is not valid, because after the replacement, the
759 original mode would be gone. Unfortunately, we can't tell
760 when do_SUBST is called to replace the operand thereof, so we
761 perform this test on oldval instead, checking whether an
762 invalid replacement took place before we got here. */
763 gcc_assert (!(GET_CODE (oldval) == SUBREG
764 && CONST_INT_P (SUBREG_REG (oldval))));
765 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
766 && CONST_INT_P (XEXP (oldval, 0))));
769 if (undobuf.frees)
770 buf = undobuf.frees, undobuf.frees = buf->next;
771 else
772 buf = XNEW (struct undo);
774 buf->kind = UNDO_RTX;
775 buf->where.r = into;
776 buf->old_contents.r = oldval;
777 *into = newval;
779 buf->next = undobuf.undos, undobuf.undos = buf;
782 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
784 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
785 for the value of a HOST_WIDE_INT value (including CONST_INT) is
786 not safe. */
788 static void
789 do_SUBST_INT (int *into, int newval)
791 struct undo *buf;
792 int oldval = *into;
794 if (oldval == newval)
795 return;
797 if (undobuf.frees)
798 buf = undobuf.frees, undobuf.frees = buf->next;
799 else
800 buf = XNEW (struct undo);
802 buf->kind = UNDO_INT;
803 buf->where.i = into;
804 buf->old_contents.i = oldval;
805 *into = newval;
807 buf->next = undobuf.undos, undobuf.undos = buf;
810 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
812 /* Similar to SUBST, but just substitute the mode. This is used when
813 changing the mode of a pseudo-register, so that any other
814 references to the entry in the regno_reg_rtx array will change as
815 well. */
817 static void
818 do_SUBST_MODE (rtx *into, machine_mode newval)
820 struct undo *buf;
821 machine_mode oldval = GET_MODE (*into);
823 if (oldval == newval)
824 return;
826 if (undobuf.frees)
827 buf = undobuf.frees, undobuf.frees = buf->next;
828 else
829 buf = XNEW (struct undo);
831 buf->kind = UNDO_MODE;
832 buf->where.r = into;
833 buf->old_contents.m = oldval;
834 adjust_reg_mode (*into, newval);
836 buf->next = undobuf.undos, undobuf.undos = buf;
839 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
841 #ifndef HAVE_cc0
842 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
844 static void
845 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
847 struct undo *buf;
848 struct insn_link * oldval = *into;
850 if (oldval == newval)
851 return;
853 if (undobuf.frees)
854 buf = undobuf.frees, undobuf.frees = buf->next;
855 else
856 buf = XNEW (struct undo);
858 buf->kind = UNDO_LINKS;
859 buf->where.l = into;
860 buf->old_contents.l = oldval;
861 *into = newval;
863 buf->next = undobuf.undos, undobuf.undos = buf;
866 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
867 #endif
869 /* Subroutine of try_combine. Determine whether the replacement patterns
870 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
871 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
872 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
873 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
874 of all the instructions can be estimated and the replacements are more
875 expensive than the original sequence. */
877 static bool
878 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
879 rtx newpat, rtx newi2pat, rtx newotherpat)
881 int i0_cost, i1_cost, i2_cost, i3_cost;
882 int new_i2_cost, new_i3_cost;
883 int old_cost, new_cost;
885 /* Lookup the original insn_rtx_costs. */
886 i2_cost = INSN_COST (i2);
887 i3_cost = INSN_COST (i3);
889 if (i1)
891 i1_cost = INSN_COST (i1);
892 if (i0)
894 i0_cost = INSN_COST (i0);
895 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
896 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
898 else
900 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
901 ? i1_cost + i2_cost + i3_cost : 0);
902 i0_cost = 0;
905 else
907 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
908 i1_cost = i0_cost = 0;
911 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
912 correct that. */
913 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
914 old_cost -= i1_cost;
917 /* Calculate the replacement insn_rtx_costs. */
918 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
919 if (newi2pat)
921 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
922 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
923 ? new_i2_cost + new_i3_cost : 0;
925 else
927 new_cost = new_i3_cost;
928 new_i2_cost = 0;
931 if (undobuf.other_insn)
933 int old_other_cost, new_other_cost;
935 old_other_cost = INSN_COST (undobuf.other_insn);
936 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
937 if (old_other_cost > 0 && new_other_cost > 0)
939 old_cost += old_other_cost;
940 new_cost += new_other_cost;
942 else
943 old_cost = 0;
946 /* Disallow this combination if both new_cost and old_cost are greater than
947 zero, and new_cost is greater than old cost. */
948 int reject = old_cost > 0 && new_cost > old_cost;
950 if (dump_file)
952 fprintf (dump_file, "%s combination of insns ",
953 reject ? "rejecting" : "allowing");
954 if (i0)
955 fprintf (dump_file, "%d, ", INSN_UID (i0));
956 if (i1 && INSN_UID (i1) != INSN_UID (i2))
957 fprintf (dump_file, "%d, ", INSN_UID (i1));
958 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
960 fprintf (dump_file, "original costs ");
961 if (i0)
962 fprintf (dump_file, "%d + ", i0_cost);
963 if (i1 && INSN_UID (i1) != INSN_UID (i2))
964 fprintf (dump_file, "%d + ", i1_cost);
965 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
967 if (newi2pat)
968 fprintf (dump_file, "replacement costs %d + %d = %d\n",
969 new_i2_cost, new_i3_cost, new_cost);
970 else
971 fprintf (dump_file, "replacement cost %d\n", new_cost);
974 if (reject)
975 return false;
977 /* Update the uid_insn_cost array with the replacement costs. */
978 INSN_COST (i2) = new_i2_cost;
979 INSN_COST (i3) = new_i3_cost;
980 if (i1)
982 INSN_COST (i1) = 0;
983 if (i0)
984 INSN_COST (i0) = 0;
987 return true;
991 /* Delete any insns that copy a register to itself. */
993 static void
994 delete_noop_moves (void)
996 rtx_insn *insn, *next;
997 basic_block bb;
999 FOR_EACH_BB_FN (bb, cfun)
1001 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
1003 next = NEXT_INSN (insn);
1004 if (INSN_P (insn) && noop_move_p (insn))
1006 if (dump_file)
1007 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
1009 delete_insn_and_edges (insn);
1016 /* Return false if we do not want to (or cannot) combine DEF. */
1017 static bool
1018 can_combine_def_p (df_ref def)
1020 /* Do not consider if it is pre/post modification in MEM. */
1021 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1022 return false;
1024 unsigned int regno = DF_REF_REGNO (def);
1026 /* Do not combine frame pointer adjustments. */
1027 if ((regno == FRAME_POINTER_REGNUM
1028 && (!reload_completed || frame_pointer_needed))
1029 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1030 || (regno == HARD_FRAME_POINTER_REGNUM
1031 && (!reload_completed || frame_pointer_needed))
1032 #endif
1033 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1034 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1035 #endif
1037 return false;
1039 return true;
1042 /* Return false if we do not want to (or cannot) combine USE. */
1043 static bool
1044 can_combine_use_p (df_ref use)
1046 /* Do not consider the usage of the stack pointer by function call. */
1047 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1048 return false;
1050 return true;
1053 /* Fill in log links field for all insns. */
1055 static void
1056 create_log_links (void)
1058 basic_block bb;
1059 rtx_insn **next_use;
1060 rtx_insn *insn;
1061 df_ref def, use;
1063 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1065 /* Pass through each block from the end, recording the uses of each
1066 register and establishing log links when def is encountered.
1067 Note that we do not clear next_use array in order to save time,
1068 so we have to test whether the use is in the same basic block as def.
1070 There are a few cases below when we do not consider the definition or
1071 usage -- these are taken from original flow.c did. Don't ask me why it is
1072 done this way; I don't know and if it works, I don't want to know. */
1074 FOR_EACH_BB_FN (bb, cfun)
1076 FOR_BB_INSNS_REVERSE (bb, insn)
1078 if (!NONDEBUG_INSN_P (insn))
1079 continue;
1081 /* Log links are created only once. */
1082 gcc_assert (!LOG_LINKS (insn));
1084 FOR_EACH_INSN_DEF (def, insn)
1086 unsigned int regno = DF_REF_REGNO (def);
1087 rtx_insn *use_insn;
1089 if (!next_use[regno])
1090 continue;
1092 if (!can_combine_def_p (def))
1093 continue;
1095 use_insn = next_use[regno];
1096 next_use[regno] = NULL;
1098 if (BLOCK_FOR_INSN (use_insn) != bb)
1099 continue;
1101 /* flow.c claimed:
1103 We don't build a LOG_LINK for hard registers contained
1104 in ASM_OPERANDs. If these registers get replaced,
1105 we might wind up changing the semantics of the insn,
1106 even if reload can make what appear to be valid
1107 assignments later. */
1108 if (regno < FIRST_PSEUDO_REGISTER
1109 && asm_noperands (PATTERN (use_insn)) >= 0)
1110 continue;
1112 /* Don't add duplicate links between instructions. */
1113 struct insn_link *links;
1114 FOR_EACH_LOG_LINK (links, use_insn)
1115 if (insn == links->insn && regno == links->regno)
1116 break;
1118 if (!links)
1119 LOG_LINKS (use_insn)
1120 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1123 FOR_EACH_INSN_USE (use, insn)
1124 if (can_combine_use_p (use))
1125 next_use[DF_REF_REGNO (use)] = insn;
1129 free (next_use);
1132 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1133 true if we found a LOG_LINK that proves that A feeds B. This only works
1134 if there are no instructions between A and B which could have a link
1135 depending on A, since in that case we would not record a link for B.
1136 We also check the implicit dependency created by a cc0 setter/user
1137 pair. */
1139 static bool
1140 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1142 struct insn_link *links;
1143 FOR_EACH_LOG_LINK (links, b)
1144 if (links->insn == a)
1145 return true;
1146 #ifdef HAVE_cc0
1147 if (sets_cc0_p (a))
1148 return true;
1149 #endif
1150 return false;
1153 /* Main entry point for combiner. F is the first insn of the function.
1154 NREGS is the first unused pseudo-reg number.
1156 Return nonzero if the combiner has turned an indirect jump
1157 instruction into a direct jump. */
1158 static int
1159 combine_instructions (rtx_insn *f, unsigned int nregs)
1161 rtx_insn *insn, *next;
1162 #ifdef HAVE_cc0
1163 rtx_insn *prev;
1164 #endif
1165 struct insn_link *links, *nextlinks;
1166 rtx_insn *first;
1167 basic_block last_bb;
1169 int new_direct_jump_p = 0;
1171 for (first = f; first && !INSN_P (first); )
1172 first = NEXT_INSN (first);
1173 if (!first)
1174 return 0;
1176 combine_attempts = 0;
1177 combine_merges = 0;
1178 combine_extras = 0;
1179 combine_successes = 0;
1181 rtl_hooks = combine_rtl_hooks;
1183 reg_stat.safe_grow_cleared (nregs);
1185 init_recog_no_volatile ();
1187 /* Allocate array for insn info. */
1188 max_uid_known = get_max_uid ();
1189 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1190 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1191 gcc_obstack_init (&insn_link_obstack);
1193 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1195 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1196 problems when, for example, we have j <<= 1 in a loop. */
1198 nonzero_sign_valid = 0;
1199 label_tick = label_tick_ebb_start = 1;
1201 /* Scan all SETs and see if we can deduce anything about what
1202 bits are known to be zero for some registers and how many copies
1203 of the sign bit are known to exist for those registers.
1205 Also set any known values so that we can use it while searching
1206 for what bits are known to be set. */
1208 setup_incoming_promotions (first);
1209 /* Allow the entry block and the first block to fall into the same EBB.
1210 Conceptually the incoming promotions are assigned to the entry block. */
1211 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1213 create_log_links ();
1214 FOR_EACH_BB_FN (this_basic_block, cfun)
1216 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1217 last_call_luid = 0;
1218 mem_last_set = -1;
1220 label_tick++;
1221 if (!single_pred_p (this_basic_block)
1222 || single_pred (this_basic_block) != last_bb)
1223 label_tick_ebb_start = label_tick;
1224 last_bb = this_basic_block;
1226 FOR_BB_INSNS (this_basic_block, insn)
1227 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1229 #ifdef AUTO_INC_DEC
1230 rtx links;
1231 #endif
1233 subst_low_luid = DF_INSN_LUID (insn);
1234 subst_insn = insn;
1236 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1237 insn);
1238 record_dead_and_set_regs (insn);
1240 #ifdef AUTO_INC_DEC
1241 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1242 if (REG_NOTE_KIND (links) == REG_INC)
1243 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1244 insn);
1245 #endif
1247 /* Record the current insn_rtx_cost of this instruction. */
1248 if (NONJUMP_INSN_P (insn))
1249 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1250 optimize_this_for_speed_p);
1251 if (dump_file)
1252 fprintf (dump_file, "insn_cost %d: %d\n",
1253 INSN_UID (insn), INSN_COST (insn));
1257 nonzero_sign_valid = 1;
1259 /* Now scan all the insns in forward order. */
1260 label_tick = label_tick_ebb_start = 1;
1261 init_reg_last ();
1262 setup_incoming_promotions (first);
1263 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1264 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1266 FOR_EACH_BB_FN (this_basic_block, cfun)
1268 rtx_insn *last_combined_insn = NULL;
1269 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1270 last_call_luid = 0;
1271 mem_last_set = -1;
1273 label_tick++;
1274 if (!single_pred_p (this_basic_block)
1275 || single_pred (this_basic_block) != last_bb)
1276 label_tick_ebb_start = label_tick;
1277 last_bb = this_basic_block;
1279 rtl_profile_for_bb (this_basic_block);
1280 for (insn = BB_HEAD (this_basic_block);
1281 insn != NEXT_INSN (BB_END (this_basic_block));
1282 insn = next ? next : NEXT_INSN (insn))
1284 next = 0;
1285 if (!NONDEBUG_INSN_P (insn))
1286 continue;
1288 while (last_combined_insn
1289 && last_combined_insn->deleted ())
1290 last_combined_insn = PREV_INSN (last_combined_insn);
1291 if (last_combined_insn == NULL_RTX
1292 || BARRIER_P (last_combined_insn)
1293 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1294 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1295 last_combined_insn = insn;
1297 /* See if we know about function return values before this
1298 insn based upon SUBREG flags. */
1299 check_promoted_subreg (insn, PATTERN (insn));
1301 /* See if we can find hardregs and subreg of pseudos in
1302 narrower modes. This could help turning TRUNCATEs
1303 into SUBREGs. */
1304 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1306 /* Try this insn with each insn it links back to. */
1308 FOR_EACH_LOG_LINK (links, insn)
1309 if ((next = try_combine (insn, links->insn, NULL,
1310 NULL, &new_direct_jump_p,
1311 last_combined_insn)) != 0)
1313 statistics_counter_event (cfun, "two-insn combine", 1);
1314 goto retry;
1317 /* Try each sequence of three linked insns ending with this one. */
1319 if (max_combine >= 3)
1320 FOR_EACH_LOG_LINK (links, insn)
1322 rtx_insn *link = links->insn;
1324 /* If the linked insn has been replaced by a note, then there
1325 is no point in pursuing this chain any further. */
1326 if (NOTE_P (link))
1327 continue;
1329 FOR_EACH_LOG_LINK (nextlinks, link)
1330 if ((next = try_combine (insn, link, nextlinks->insn,
1331 NULL, &new_direct_jump_p,
1332 last_combined_insn)) != 0)
1334 statistics_counter_event (cfun, "three-insn combine", 1);
1335 goto retry;
1339 #ifdef HAVE_cc0
1340 /* Try to combine a jump insn that uses CC0
1341 with a preceding insn that sets CC0, and maybe with its
1342 logical predecessor as well.
1343 This is how we make decrement-and-branch insns.
1344 We need this special code because data flow connections
1345 via CC0 do not get entered in LOG_LINKS. */
1347 if (JUMP_P (insn)
1348 && (prev = prev_nonnote_insn (insn)) != 0
1349 && NONJUMP_INSN_P (prev)
1350 && sets_cc0_p (PATTERN (prev)))
1352 if ((next = try_combine (insn, prev, NULL, NULL,
1353 &new_direct_jump_p,
1354 last_combined_insn)) != 0)
1355 goto retry;
1357 FOR_EACH_LOG_LINK (nextlinks, prev)
1358 if ((next = try_combine (insn, prev, nextlinks->insn,
1359 NULL, &new_direct_jump_p,
1360 last_combined_insn)) != 0)
1361 goto retry;
1364 /* Do the same for an insn that explicitly references CC0. */
1365 if (NONJUMP_INSN_P (insn)
1366 && (prev = prev_nonnote_insn (insn)) != 0
1367 && NONJUMP_INSN_P (prev)
1368 && sets_cc0_p (PATTERN (prev))
1369 && GET_CODE (PATTERN (insn)) == SET
1370 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1372 if ((next = try_combine (insn, prev, NULL, NULL,
1373 &new_direct_jump_p,
1374 last_combined_insn)) != 0)
1375 goto retry;
1377 FOR_EACH_LOG_LINK (nextlinks, prev)
1378 if ((next = try_combine (insn, prev, nextlinks->insn,
1379 NULL, &new_direct_jump_p,
1380 last_combined_insn)) != 0)
1381 goto retry;
1384 /* Finally, see if any of the insns that this insn links to
1385 explicitly references CC0. If so, try this insn, that insn,
1386 and its predecessor if it sets CC0. */
1387 FOR_EACH_LOG_LINK (links, insn)
1388 if (NONJUMP_INSN_P (links->insn)
1389 && GET_CODE (PATTERN (links->insn)) == SET
1390 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1391 && (prev = prev_nonnote_insn (links->insn)) != 0
1392 && NONJUMP_INSN_P (prev)
1393 && sets_cc0_p (PATTERN (prev))
1394 && (next = try_combine (insn, links->insn,
1395 prev, NULL, &new_direct_jump_p,
1396 last_combined_insn)) != 0)
1397 goto retry;
1398 #endif
1400 /* Try combining an insn with two different insns whose results it
1401 uses. */
1402 if (max_combine >= 3)
1403 FOR_EACH_LOG_LINK (links, insn)
1404 for (nextlinks = links->next; nextlinks;
1405 nextlinks = nextlinks->next)
1406 if ((next = try_combine (insn, links->insn,
1407 nextlinks->insn, NULL,
1408 &new_direct_jump_p,
1409 last_combined_insn)) != 0)
1412 statistics_counter_event (cfun, "three-insn combine", 1);
1413 goto retry;
1416 /* Try four-instruction combinations. */
1417 if (max_combine >= 4)
1418 FOR_EACH_LOG_LINK (links, insn)
1420 struct insn_link *next1;
1421 rtx_insn *link = links->insn;
1423 /* If the linked insn has been replaced by a note, then there
1424 is no point in pursuing this chain any further. */
1425 if (NOTE_P (link))
1426 continue;
1428 FOR_EACH_LOG_LINK (next1, link)
1430 rtx_insn *link1 = next1->insn;
1431 if (NOTE_P (link1))
1432 continue;
1433 /* I0 -> I1 -> I2 -> I3. */
1434 FOR_EACH_LOG_LINK (nextlinks, link1)
1435 if ((next = try_combine (insn, link, link1,
1436 nextlinks->insn,
1437 &new_direct_jump_p,
1438 last_combined_insn)) != 0)
1440 statistics_counter_event (cfun, "four-insn combine", 1);
1441 goto retry;
1443 /* I0, I1 -> I2, I2 -> I3. */
1444 for (nextlinks = next1->next; nextlinks;
1445 nextlinks = nextlinks->next)
1446 if ((next = try_combine (insn, link, link1,
1447 nextlinks->insn,
1448 &new_direct_jump_p,
1449 last_combined_insn)) != 0)
1451 statistics_counter_event (cfun, "four-insn combine", 1);
1452 goto retry;
1456 for (next1 = links->next; next1; next1 = next1->next)
1458 rtx_insn *link1 = next1->insn;
1459 if (NOTE_P (link1))
1460 continue;
1461 /* I0 -> I2; I1, I2 -> I3. */
1462 FOR_EACH_LOG_LINK (nextlinks, link)
1463 if ((next = try_combine (insn, link, link1,
1464 nextlinks->insn,
1465 &new_direct_jump_p,
1466 last_combined_insn)) != 0)
1468 statistics_counter_event (cfun, "four-insn combine", 1);
1469 goto retry;
1471 /* I0 -> I1; I1, I2 -> I3. */
1472 FOR_EACH_LOG_LINK (nextlinks, link1)
1473 if ((next = try_combine (insn, link, link1,
1474 nextlinks->insn,
1475 &new_direct_jump_p,
1476 last_combined_insn)) != 0)
1478 statistics_counter_event (cfun, "four-insn combine", 1);
1479 goto retry;
1484 /* Try this insn with each REG_EQUAL note it links back to. */
1485 FOR_EACH_LOG_LINK (links, insn)
1487 rtx set, note;
1488 rtx_insn *temp = links->insn;
1489 if ((set = single_set (temp)) != 0
1490 && (note = find_reg_equal_equiv_note (temp)) != 0
1491 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1492 /* Avoid using a register that may already been marked
1493 dead by an earlier instruction. */
1494 && ! unmentioned_reg_p (note, SET_SRC (set))
1495 && (GET_MODE (note) == VOIDmode
1496 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1497 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1499 /* Temporarily replace the set's source with the
1500 contents of the REG_EQUAL note. The insn will
1501 be deleted or recognized by try_combine. */
1502 rtx orig = SET_SRC (set);
1503 SET_SRC (set) = note;
1504 i2mod = temp;
1505 i2mod_old_rhs = copy_rtx (orig);
1506 i2mod_new_rhs = copy_rtx (note);
1507 next = try_combine (insn, i2mod, NULL, NULL,
1508 &new_direct_jump_p,
1509 last_combined_insn);
1510 i2mod = NULL;
1511 if (next)
1513 statistics_counter_event (cfun, "insn-with-note combine", 1);
1514 goto retry;
1516 SET_SRC (set) = orig;
1520 if (!NOTE_P (insn))
1521 record_dead_and_set_regs (insn);
1523 retry:
1528 default_rtl_profile ();
1529 clear_bb_flags ();
1530 new_direct_jump_p |= purge_all_dead_edges ();
1531 delete_noop_moves ();
1533 /* Clean up. */
1534 obstack_free (&insn_link_obstack, NULL);
1535 free (uid_log_links);
1536 free (uid_insn_cost);
1537 reg_stat.release ();
1540 struct undo *undo, *next;
1541 for (undo = undobuf.frees; undo; undo = next)
1543 next = undo->next;
1544 free (undo);
1546 undobuf.frees = 0;
1549 total_attempts += combine_attempts;
1550 total_merges += combine_merges;
1551 total_extras += combine_extras;
1552 total_successes += combine_successes;
1554 nonzero_sign_valid = 0;
1555 rtl_hooks = general_rtl_hooks;
1557 /* Make recognizer allow volatile MEMs again. */
1558 init_recog ();
1560 return new_direct_jump_p;
1563 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1565 static void
1566 init_reg_last (void)
1568 unsigned int i;
1569 reg_stat_type *p;
1571 FOR_EACH_VEC_ELT (reg_stat, i, p)
1572 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1575 /* Set up any promoted values for incoming argument registers. */
1577 static void
1578 setup_incoming_promotions (rtx_insn *first)
1580 tree arg;
1581 bool strictly_local = false;
1583 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1584 arg = DECL_CHAIN (arg))
1586 rtx x, reg = DECL_INCOMING_RTL (arg);
1587 int uns1, uns3;
1588 machine_mode mode1, mode2, mode3, mode4;
1590 /* Only continue if the incoming argument is in a register. */
1591 if (!REG_P (reg))
1592 continue;
1594 /* Determine, if possible, whether all call sites of the current
1595 function lie within the current compilation unit. (This does
1596 take into account the exporting of a function via taking its
1597 address, and so forth.) */
1598 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1600 /* The mode and signedness of the argument before any promotions happen
1601 (equal to the mode of the pseudo holding it at that stage). */
1602 mode1 = TYPE_MODE (TREE_TYPE (arg));
1603 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1605 /* The mode and signedness of the argument after any source language and
1606 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1607 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1608 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1610 /* The mode and signedness of the argument as it is actually passed,
1611 see assign_parm_setup_reg in function.c. */
1612 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1613 TREE_TYPE (cfun->decl), 0);
1615 /* The mode of the register in which the argument is being passed. */
1616 mode4 = GET_MODE (reg);
1618 /* Eliminate sign extensions in the callee when:
1619 (a) A mode promotion has occurred; */
1620 if (mode1 == mode3)
1621 continue;
1622 /* (b) The mode of the register is the same as the mode of
1623 the argument as it is passed; */
1624 if (mode3 != mode4)
1625 continue;
1626 /* (c) There's no language level extension; */
1627 if (mode1 == mode2)
1629 /* (c.1) All callers are from the current compilation unit. If that's
1630 the case we don't have to rely on an ABI, we only have to know
1631 what we're generating right now, and we know that we will do the
1632 mode1 to mode2 promotion with the given sign. */
1633 else if (!strictly_local)
1634 continue;
1635 /* (c.2) The combination of the two promotions is useful. This is
1636 true when the signs match, or if the first promotion is unsigned.
1637 In the later case, (sign_extend (zero_extend x)) is the same as
1638 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1639 else if (uns1)
1640 uns3 = true;
1641 else if (uns3)
1642 continue;
1644 /* Record that the value was promoted from mode1 to mode3,
1645 so that any sign extension at the head of the current
1646 function may be eliminated. */
1647 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1648 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1649 record_value_for_reg (reg, first, x);
1653 /* Called via note_stores. If X is a pseudo that is narrower than
1654 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1656 If we are setting only a portion of X and we can't figure out what
1657 portion, assume all bits will be used since we don't know what will
1658 be happening.
1660 Similarly, set how many bits of X are known to be copies of the sign bit
1661 at all locations in the function. This is the smallest number implied
1662 by any set of X. */
1664 static void
1665 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1667 rtx_insn *insn = (rtx_insn *) data;
1668 unsigned int num;
1670 if (REG_P (x)
1671 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1672 /* If this register is undefined at the start of the file, we can't
1673 say what its contents were. */
1674 && ! REGNO_REG_SET_P
1675 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1676 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1678 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1680 if (set == 0 || GET_CODE (set) == CLOBBER)
1682 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1683 rsp->sign_bit_copies = 1;
1684 return;
1687 /* If this register is being initialized using itself, and the
1688 register is uninitialized in this basic block, and there are
1689 no LOG_LINKS which set the register, then part of the
1690 register is uninitialized. In that case we can't assume
1691 anything about the number of nonzero bits.
1693 ??? We could do better if we checked this in
1694 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1695 could avoid making assumptions about the insn which initially
1696 sets the register, while still using the information in other
1697 insns. We would have to be careful to check every insn
1698 involved in the combination. */
1700 if (insn
1701 && reg_referenced_p (x, PATTERN (insn))
1702 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1703 REGNO (x)))
1705 struct insn_link *link;
1707 FOR_EACH_LOG_LINK (link, insn)
1708 if (dead_or_set_p (link->insn, x))
1709 break;
1710 if (!link)
1712 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1713 rsp->sign_bit_copies = 1;
1714 return;
1718 /* If this is a complex assignment, see if we can convert it into a
1719 simple assignment. */
1720 set = expand_field_assignment (set);
1722 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1723 set what we know about X. */
1725 if (SET_DEST (set) == x
1726 || (paradoxical_subreg_p (SET_DEST (set))
1727 && SUBREG_REG (SET_DEST (set)) == x))
1729 rtx src = SET_SRC (set);
1731 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1732 /* If X is narrower than a word and SRC is a non-negative
1733 constant that would appear negative in the mode of X,
1734 sign-extend it for use in reg_stat[].nonzero_bits because some
1735 machines (maybe most) will actually do the sign-extension
1736 and this is the conservative approach.
1738 ??? For 2.5, try to tighten up the MD files in this regard
1739 instead of this kludge. */
1741 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1742 && CONST_INT_P (src)
1743 && INTVAL (src) > 0
1744 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1745 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1746 #endif
1748 /* Don't call nonzero_bits if it cannot change anything. */
1749 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1750 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1751 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1752 if (rsp->sign_bit_copies == 0
1753 || rsp->sign_bit_copies > num)
1754 rsp->sign_bit_copies = num;
1756 else
1758 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1759 rsp->sign_bit_copies = 1;
1764 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1765 optionally insns that were previously combined into I3 or that will be
1766 combined into the merger of INSN and I3. The order is PRED, PRED2,
1767 INSN, SUCC, SUCC2, I3.
1769 Return 0 if the combination is not allowed for any reason.
1771 If the combination is allowed, *PDEST will be set to the single
1772 destination of INSN and *PSRC to the single source, and this function
1773 will return 1. */
1775 static int
1776 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1777 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1778 rtx *pdest, rtx *psrc)
1780 int i;
1781 const_rtx set = 0;
1782 rtx src, dest;
1783 rtx_insn *p;
1784 #ifdef AUTO_INC_DEC
1785 rtx link;
1786 #endif
1787 bool all_adjacent = true;
1788 int (*is_volatile_p) (const_rtx);
1790 if (succ)
1792 if (succ2)
1794 if (next_active_insn (succ2) != i3)
1795 all_adjacent = false;
1796 if (next_active_insn (succ) != succ2)
1797 all_adjacent = false;
1799 else if (next_active_insn (succ) != i3)
1800 all_adjacent = false;
1801 if (next_active_insn (insn) != succ)
1802 all_adjacent = false;
1804 else if (next_active_insn (insn) != i3)
1805 all_adjacent = false;
1807 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1808 or a PARALLEL consisting of such a SET and CLOBBERs.
1810 If INSN has CLOBBER parallel parts, ignore them for our processing.
1811 By definition, these happen during the execution of the insn. When it
1812 is merged with another insn, all bets are off. If they are, in fact,
1813 needed and aren't also supplied in I3, they may be added by
1814 recog_for_combine. Otherwise, it won't match.
1816 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1817 note.
1819 Get the source and destination of INSN. If more than one, can't
1820 combine. */
1822 if (GET_CODE (PATTERN (insn)) == SET)
1823 set = PATTERN (insn);
1824 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1825 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1827 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1829 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1831 switch (GET_CODE (elt))
1833 /* This is important to combine floating point insns
1834 for the SH4 port. */
1835 case USE:
1836 /* Combining an isolated USE doesn't make sense.
1837 We depend here on combinable_i3pat to reject them. */
1838 /* The code below this loop only verifies that the inputs of
1839 the SET in INSN do not change. We call reg_set_between_p
1840 to verify that the REG in the USE does not change between
1841 I3 and INSN.
1842 If the USE in INSN was for a pseudo register, the matching
1843 insn pattern will likely match any register; combining this
1844 with any other USE would only be safe if we knew that the
1845 used registers have identical values, or if there was
1846 something to tell them apart, e.g. different modes. For
1847 now, we forgo such complicated tests and simply disallow
1848 combining of USES of pseudo registers with any other USE. */
1849 if (REG_P (XEXP (elt, 0))
1850 && GET_CODE (PATTERN (i3)) == PARALLEL)
1852 rtx i3pat = PATTERN (i3);
1853 int i = XVECLEN (i3pat, 0) - 1;
1854 unsigned int regno = REGNO (XEXP (elt, 0));
1858 rtx i3elt = XVECEXP (i3pat, 0, i);
1860 if (GET_CODE (i3elt) == USE
1861 && REG_P (XEXP (i3elt, 0))
1862 && (REGNO (XEXP (i3elt, 0)) == regno
1863 ? reg_set_between_p (XEXP (elt, 0),
1864 PREV_INSN (insn), i3)
1865 : regno >= FIRST_PSEUDO_REGISTER))
1866 return 0;
1868 while (--i >= 0);
1870 break;
1872 /* We can ignore CLOBBERs. */
1873 case CLOBBER:
1874 break;
1876 case SET:
1877 /* Ignore SETs whose result isn't used but not those that
1878 have side-effects. */
1879 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1880 && insn_nothrow_p (insn)
1881 && !side_effects_p (elt))
1882 break;
1884 /* If we have already found a SET, this is a second one and
1885 so we cannot combine with this insn. */
1886 if (set)
1887 return 0;
1889 set = elt;
1890 break;
1892 default:
1893 /* Anything else means we can't combine. */
1894 return 0;
1898 if (set == 0
1899 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1900 so don't do anything with it. */
1901 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1902 return 0;
1904 else
1905 return 0;
1907 if (set == 0)
1908 return 0;
1910 /* The simplification in expand_field_assignment may call back to
1911 get_last_value, so set safe guard here. */
1912 subst_low_luid = DF_INSN_LUID (insn);
1914 set = expand_field_assignment (set);
1915 src = SET_SRC (set), dest = SET_DEST (set);
1917 /* Don't eliminate a store in the stack pointer. */
1918 if (dest == stack_pointer_rtx
1919 /* Don't combine with an insn that sets a register to itself if it has
1920 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1921 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1922 /* Can't merge an ASM_OPERANDS. */
1923 || GET_CODE (src) == ASM_OPERANDS
1924 /* Can't merge a function call. */
1925 || GET_CODE (src) == CALL
1926 /* Don't eliminate a function call argument. */
1927 || (CALL_P (i3)
1928 && (find_reg_fusage (i3, USE, dest)
1929 || (REG_P (dest)
1930 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1931 && global_regs[REGNO (dest)])))
1932 /* Don't substitute into an incremented register. */
1933 || FIND_REG_INC_NOTE (i3, dest)
1934 || (succ && FIND_REG_INC_NOTE (succ, dest))
1935 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1936 /* Don't substitute into a non-local goto, this confuses CFG. */
1937 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1938 /* Make sure that DEST is not used after SUCC but before I3. */
1939 || (!all_adjacent
1940 && ((succ2
1941 && (reg_used_between_p (dest, succ2, i3)
1942 || reg_used_between_p (dest, succ, succ2)))
1943 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1944 /* Make sure that the value that is to be substituted for the register
1945 does not use any registers whose values alter in between. However,
1946 If the insns are adjacent, a use can't cross a set even though we
1947 think it might (this can happen for a sequence of insns each setting
1948 the same destination; last_set of that register might point to
1949 a NOTE). If INSN has a REG_EQUIV note, the register is always
1950 equivalent to the memory so the substitution is valid even if there
1951 are intervening stores. Also, don't move a volatile asm or
1952 UNSPEC_VOLATILE across any other insns. */
1953 || (! all_adjacent
1954 && (((!MEM_P (src)
1955 || ! find_reg_note (insn, REG_EQUIV, src))
1956 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1957 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1958 || GET_CODE (src) == UNSPEC_VOLATILE))
1959 /* Don't combine across a CALL_INSN, because that would possibly
1960 change whether the life span of some REGs crosses calls or not,
1961 and it is a pain to update that information.
1962 Exception: if source is a constant, moving it later can't hurt.
1963 Accept that as a special case. */
1964 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1965 return 0;
1967 /* DEST must either be a REG or CC0. */
1968 if (REG_P (dest))
1970 /* If register alignment is being enforced for multi-word items in all
1971 cases except for parameters, it is possible to have a register copy
1972 insn referencing a hard register that is not allowed to contain the
1973 mode being copied and which would not be valid as an operand of most
1974 insns. Eliminate this problem by not combining with such an insn.
1976 Also, on some machines we don't want to extend the life of a hard
1977 register. */
1979 if (REG_P (src)
1980 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1981 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1982 /* Don't extend the life of a hard register unless it is
1983 user variable (if we have few registers) or it can't
1984 fit into the desired register (meaning something special
1985 is going on).
1986 Also avoid substituting a return register into I3, because
1987 reload can't handle a conflict with constraints of other
1988 inputs. */
1989 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1990 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1991 return 0;
1993 else if (GET_CODE (dest) != CC0)
1994 return 0;
1997 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1998 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1999 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2001 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2003 /* If the clobber represents an earlyclobber operand, we must not
2004 substitute an expression containing the clobbered register.
2005 As we do not analyze the constraint strings here, we have to
2006 make the conservative assumption. However, if the register is
2007 a fixed hard reg, the clobber cannot represent any operand;
2008 we leave it up to the machine description to either accept or
2009 reject use-and-clobber patterns. */
2010 if (!REG_P (reg)
2011 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2012 || !fixed_regs[REGNO (reg)])
2013 if (reg_overlap_mentioned_p (reg, src))
2014 return 0;
2017 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2018 or not), reject, unless nothing volatile comes between it and I3 */
2020 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2022 /* Make sure neither succ nor succ2 contains a volatile reference. */
2023 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2024 return 0;
2025 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2026 return 0;
2027 /* We'll check insns between INSN and I3 below. */
2030 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2031 to be an explicit register variable, and was chosen for a reason. */
2033 if (GET_CODE (src) == ASM_OPERANDS
2034 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2035 return 0;
2037 /* If INSN contains volatile references (specifically volatile MEMs),
2038 we cannot combine across any other volatile references.
2039 Even if INSN doesn't contain volatile references, any intervening
2040 volatile insn might affect machine state. */
2042 is_volatile_p = volatile_refs_p (PATTERN (insn))
2043 ? volatile_refs_p
2044 : volatile_insn_p;
2046 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2047 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2048 return 0;
2050 /* If INSN contains an autoincrement or autodecrement, make sure that
2051 register is not used between there and I3, and not already used in
2052 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2053 Also insist that I3 not be a jump; if it were one
2054 and the incremented register were spilled, we would lose. */
2056 #ifdef AUTO_INC_DEC
2057 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2058 if (REG_NOTE_KIND (link) == REG_INC
2059 && (JUMP_P (i3)
2060 || reg_used_between_p (XEXP (link, 0), insn, i3)
2061 || (pred != NULL_RTX
2062 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2063 || (pred2 != NULL_RTX
2064 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2065 || (succ != NULL_RTX
2066 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2067 || (succ2 != NULL_RTX
2068 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2069 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2070 return 0;
2071 #endif
2073 #ifdef HAVE_cc0
2074 /* Don't combine an insn that follows a CC0-setting insn.
2075 An insn that uses CC0 must not be separated from the one that sets it.
2076 We do, however, allow I2 to follow a CC0-setting insn if that insn
2077 is passed as I1; in that case it will be deleted also.
2078 We also allow combining in this case if all the insns are adjacent
2079 because that would leave the two CC0 insns adjacent as well.
2080 It would be more logical to test whether CC0 occurs inside I1 or I2,
2081 but that would be much slower, and this ought to be equivalent. */
2083 p = prev_nonnote_insn (insn);
2084 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2085 && ! all_adjacent)
2086 return 0;
2087 #endif
2089 /* If we get here, we have passed all the tests and the combination is
2090 to be allowed. */
2092 *pdest = dest;
2093 *psrc = src;
2095 return 1;
2098 /* LOC is the location within I3 that contains its pattern or the component
2099 of a PARALLEL of the pattern. We validate that it is valid for combining.
2101 One problem is if I3 modifies its output, as opposed to replacing it
2102 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2103 doing so would produce an insn that is not equivalent to the original insns.
2105 Consider:
2107 (set (reg:DI 101) (reg:DI 100))
2108 (set (subreg:SI (reg:DI 101) 0) <foo>)
2110 This is NOT equivalent to:
2112 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2113 (set (reg:DI 101) (reg:DI 100))])
2115 Not only does this modify 100 (in which case it might still be valid
2116 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2118 We can also run into a problem if I2 sets a register that I1
2119 uses and I1 gets directly substituted into I3 (not via I2). In that
2120 case, we would be getting the wrong value of I2DEST into I3, so we
2121 must reject the combination. This case occurs when I2 and I1 both
2122 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2123 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2124 of a SET must prevent combination from occurring. The same situation
2125 can occur for I0, in which case I0_NOT_IN_SRC is set.
2127 Before doing the above check, we first try to expand a field assignment
2128 into a set of logical operations.
2130 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2131 we place a register that is both set and used within I3. If more than one
2132 such register is detected, we fail.
2134 Return 1 if the combination is valid, zero otherwise. */
2136 static int
2137 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2138 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2140 rtx x = *loc;
2142 if (GET_CODE (x) == SET)
2144 rtx set = x ;
2145 rtx dest = SET_DEST (set);
2146 rtx src = SET_SRC (set);
2147 rtx inner_dest = dest;
2148 rtx subdest;
2150 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2151 || GET_CODE (inner_dest) == SUBREG
2152 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2153 inner_dest = XEXP (inner_dest, 0);
2155 /* Check for the case where I3 modifies its output, as discussed
2156 above. We don't want to prevent pseudos from being combined
2157 into the address of a MEM, so only prevent the combination if
2158 i1 or i2 set the same MEM. */
2159 if ((inner_dest != dest &&
2160 (!MEM_P (inner_dest)
2161 || rtx_equal_p (i2dest, inner_dest)
2162 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2163 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2164 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2165 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2166 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2168 /* This is the same test done in can_combine_p except we can't test
2169 all_adjacent; we don't have to, since this instruction will stay
2170 in place, thus we are not considering increasing the lifetime of
2171 INNER_DEST.
2173 Also, if this insn sets a function argument, combining it with
2174 something that might need a spill could clobber a previous
2175 function argument; the all_adjacent test in can_combine_p also
2176 checks this; here, we do a more specific test for this case. */
2178 || (REG_P (inner_dest)
2179 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2180 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2181 GET_MODE (inner_dest))))
2182 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2183 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2184 return 0;
2186 /* If DEST is used in I3, it is being killed in this insn, so
2187 record that for later. We have to consider paradoxical
2188 subregs here, since they kill the whole register, but we
2189 ignore partial subregs, STRICT_LOW_PART, etc.
2190 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2191 STACK_POINTER_REGNUM, since these are always considered to be
2192 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2193 subdest = dest;
2194 if (GET_CODE (subdest) == SUBREG
2195 && (GET_MODE_SIZE (GET_MODE (subdest))
2196 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2197 subdest = SUBREG_REG (subdest);
2198 if (pi3dest_killed
2199 && REG_P (subdest)
2200 && reg_referenced_p (subdest, PATTERN (i3))
2201 && REGNO (subdest) != FRAME_POINTER_REGNUM
2202 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2203 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2204 #endif
2205 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2206 && (REGNO (subdest) != ARG_POINTER_REGNUM
2207 || ! fixed_regs [REGNO (subdest)])
2208 #endif
2209 && REGNO (subdest) != STACK_POINTER_REGNUM)
2211 if (*pi3dest_killed)
2212 return 0;
2214 *pi3dest_killed = subdest;
2218 else if (GET_CODE (x) == PARALLEL)
2220 int i;
2222 for (i = 0; i < XVECLEN (x, 0); i++)
2223 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2224 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2225 return 0;
2228 return 1;
2231 /* Return 1 if X is an arithmetic expression that contains a multiplication
2232 and division. We don't count multiplications by powers of two here. */
2234 static int
2235 contains_muldiv (rtx x)
2237 switch (GET_CODE (x))
2239 case MOD: case DIV: case UMOD: case UDIV:
2240 return 1;
2242 case MULT:
2243 return ! (CONST_INT_P (XEXP (x, 1))
2244 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2245 default:
2246 if (BINARY_P (x))
2247 return contains_muldiv (XEXP (x, 0))
2248 || contains_muldiv (XEXP (x, 1));
2250 if (UNARY_P (x))
2251 return contains_muldiv (XEXP (x, 0));
2253 return 0;
2257 /* Determine whether INSN can be used in a combination. Return nonzero if
2258 not. This is used in try_combine to detect early some cases where we
2259 can't perform combinations. */
2261 static int
2262 cant_combine_insn_p (rtx_insn *insn)
2264 rtx set;
2265 rtx src, dest;
2267 /* If this isn't really an insn, we can't do anything.
2268 This can occur when flow deletes an insn that it has merged into an
2269 auto-increment address. */
2270 if (! INSN_P (insn))
2271 return 1;
2273 /* Never combine loads and stores involving hard regs that are likely
2274 to be spilled. The register allocator can usually handle such
2275 reg-reg moves by tying. If we allow the combiner to make
2276 substitutions of likely-spilled regs, reload might die.
2277 As an exception, we allow combinations involving fixed regs; these are
2278 not available to the register allocator so there's no risk involved. */
2280 set = single_set (insn);
2281 if (! set)
2282 return 0;
2283 src = SET_SRC (set);
2284 dest = SET_DEST (set);
2285 if (GET_CODE (src) == SUBREG)
2286 src = SUBREG_REG (src);
2287 if (GET_CODE (dest) == SUBREG)
2288 dest = SUBREG_REG (dest);
2289 if (REG_P (src) && REG_P (dest)
2290 && ((HARD_REGISTER_P (src)
2291 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2292 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2293 || (HARD_REGISTER_P (dest)
2294 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2295 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2296 return 1;
2298 return 0;
2301 struct likely_spilled_retval_info
2303 unsigned regno, nregs;
2304 unsigned mask;
2307 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2308 hard registers that are known to be written to / clobbered in full. */
2309 static void
2310 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2312 struct likely_spilled_retval_info *const info =
2313 (struct likely_spilled_retval_info *) data;
2314 unsigned regno, nregs;
2315 unsigned new_mask;
2317 if (!REG_P (XEXP (set, 0)))
2318 return;
2319 regno = REGNO (x);
2320 if (regno >= info->regno + info->nregs)
2321 return;
2322 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2323 if (regno + nregs <= info->regno)
2324 return;
2325 new_mask = (2U << (nregs - 1)) - 1;
2326 if (regno < info->regno)
2327 new_mask >>= info->regno - regno;
2328 else
2329 new_mask <<= regno - info->regno;
2330 info->mask &= ~new_mask;
2333 /* Return nonzero iff part of the return value is live during INSN, and
2334 it is likely spilled. This can happen when more than one insn is needed
2335 to copy the return value, e.g. when we consider to combine into the
2336 second copy insn for a complex value. */
2338 static int
2339 likely_spilled_retval_p (rtx_insn *insn)
2341 rtx_insn *use = BB_END (this_basic_block);
2342 rtx reg;
2343 rtx_insn *p;
2344 unsigned regno, nregs;
2345 /* We assume here that no machine mode needs more than
2346 32 hard registers when the value overlaps with a register
2347 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2348 unsigned mask;
2349 struct likely_spilled_retval_info info;
2351 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2352 return 0;
2353 reg = XEXP (PATTERN (use), 0);
2354 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2355 return 0;
2356 regno = REGNO (reg);
2357 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2358 if (nregs == 1)
2359 return 0;
2360 mask = (2U << (nregs - 1)) - 1;
2362 /* Disregard parts of the return value that are set later. */
2363 info.regno = regno;
2364 info.nregs = nregs;
2365 info.mask = mask;
2366 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2367 if (INSN_P (p))
2368 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2369 mask = info.mask;
2371 /* Check if any of the (probably) live return value registers is
2372 likely spilled. */
2373 nregs --;
2376 if ((mask & 1 << nregs)
2377 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2378 return 1;
2379 } while (nregs--);
2380 return 0;
2383 /* Adjust INSN after we made a change to its destination.
2385 Changing the destination can invalidate notes that say something about
2386 the results of the insn and a LOG_LINK pointing to the insn. */
2388 static void
2389 adjust_for_new_dest (rtx_insn *insn)
2391 /* For notes, be conservative and simply remove them. */
2392 remove_reg_equal_equiv_notes (insn);
2394 /* The new insn will have a destination that was previously the destination
2395 of an insn just above it. Call distribute_links to make a LOG_LINK from
2396 the next use of that destination. */
2398 rtx set = single_set (insn);
2399 gcc_assert (set);
2401 rtx reg = SET_DEST (set);
2403 while (GET_CODE (reg) == ZERO_EXTRACT
2404 || GET_CODE (reg) == STRICT_LOW_PART
2405 || GET_CODE (reg) == SUBREG)
2406 reg = XEXP (reg, 0);
2407 gcc_assert (REG_P (reg));
2409 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2411 df_insn_rescan (insn);
2414 /* Return TRUE if combine can reuse reg X in mode MODE.
2415 ADDED_SETS is nonzero if the original set is still required. */
2416 static bool
2417 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2419 unsigned int regno;
2421 if (!REG_P (x))
2422 return false;
2424 regno = REGNO (x);
2425 /* Allow hard registers if the new mode is legal, and occupies no more
2426 registers than the old mode. */
2427 if (regno < FIRST_PSEUDO_REGISTER)
2428 return (HARD_REGNO_MODE_OK (regno, mode)
2429 && (hard_regno_nregs[regno][GET_MODE (x)]
2430 >= hard_regno_nregs[regno][mode]));
2432 /* Or a pseudo that is only used once. */
2433 return (regno < reg_n_sets_max
2434 && REG_N_SETS (regno) == 1
2435 && !added_sets
2436 && !REG_USERVAR_P (x));
2440 /* Check whether X, the destination of a set, refers to part of
2441 the register specified by REG. */
2443 static bool
2444 reg_subword_p (rtx x, rtx reg)
2446 /* Check that reg is an integer mode register. */
2447 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2448 return false;
2450 if (GET_CODE (x) == STRICT_LOW_PART
2451 || GET_CODE (x) == ZERO_EXTRACT)
2452 x = XEXP (x, 0);
2454 return GET_CODE (x) == SUBREG
2455 && SUBREG_REG (x) == reg
2456 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2459 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2460 Note that the INSN should be deleted *after* removing dead edges, so
2461 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2462 but not for a (set (pc) (label_ref FOO)). */
2464 static void
2465 update_cfg_for_uncondjump (rtx_insn *insn)
2467 basic_block bb = BLOCK_FOR_INSN (insn);
2468 gcc_assert (BB_END (bb) == insn);
2470 purge_dead_edges (bb);
2472 delete_insn (insn);
2473 if (EDGE_COUNT (bb->succs) == 1)
2475 rtx_insn *insn;
2477 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2479 /* Remove barriers from the footer if there are any. */
2480 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2481 if (BARRIER_P (insn))
2483 if (PREV_INSN (insn))
2484 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2485 else
2486 BB_FOOTER (bb) = NEXT_INSN (insn);
2487 if (NEXT_INSN (insn))
2488 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2490 else if (LABEL_P (insn))
2491 break;
2495 #ifndef HAVE_cc0
2496 /* Return whether INSN is a PARALLEL of exactly N register SETs followed
2497 by an arbitrary number of CLOBBERs. */
2498 static bool
2499 is_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2501 rtx pat = PATTERN (insn);
2503 if (GET_CODE (pat) != PARALLEL)
2504 return false;
2506 int len = XVECLEN (pat, 0);
2507 if (len < n)
2508 return false;
2510 int i;
2511 for (i = 0; i < n; i++)
2512 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2513 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2514 return false;
2515 for ( ; i < len; i++)
2516 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2517 return false;
2519 return true;
2522 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2523 CLOBBERs), can be split into individual SETs in that order, without
2524 changing semantics. */
2525 static bool
2526 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2528 if (!insn_nothrow_p (insn))
2529 return false;
2531 rtx pat = PATTERN (insn);
2533 int i, j;
2534 for (i = 0; i < n; i++)
2536 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2537 return false;
2539 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2541 for (j = i + 1; j < n; j++)
2542 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2543 return false;
2546 return true;
2548 #endif
2550 /* Try to combine the insns I0, I1 and I2 into I3.
2551 Here I0, I1 and I2 appear earlier than I3.
2552 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2555 If we are combining more than two insns and the resulting insn is not
2556 recognized, try splitting it into two insns. If that happens, I2 and I3
2557 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2558 Otherwise, I0, I1 and I2 are pseudo-deleted.
2560 Return 0 if the combination does not work. Then nothing is changed.
2561 If we did the combination, return the insn at which combine should
2562 resume scanning.
2564 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2565 new direct jump instruction.
2567 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2568 been I3 passed to an earlier try_combine within the same basic
2569 block. */
2571 static rtx_insn *
2572 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2573 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2575 /* New patterns for I3 and I2, respectively. */
2576 rtx newpat, newi2pat = 0;
2577 rtvec newpat_vec_with_clobbers = 0;
2578 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2579 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2580 dead. */
2581 int added_sets_0, added_sets_1, added_sets_2;
2582 /* Total number of SETs to put into I3. */
2583 int total_sets;
2584 /* Nonzero if I2's or I1's body now appears in I3. */
2585 int i2_is_used = 0, i1_is_used = 0;
2586 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2587 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2588 /* Contains I3 if the destination of I3 is used in its source, which means
2589 that the old life of I3 is being killed. If that usage is placed into
2590 I2 and not in I3, a REG_DEAD note must be made. */
2591 rtx i3dest_killed = 0;
2592 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2593 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2594 /* Copy of SET_SRC of I1 and I0, if needed. */
2595 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2596 /* Set if I2DEST was reused as a scratch register. */
2597 bool i2scratch = false;
2598 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2599 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2600 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2601 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2602 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2603 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2604 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2605 /* Notes that must be added to REG_NOTES in I3 and I2. */
2606 rtx new_i3_notes, new_i2_notes;
2607 /* Notes that we substituted I3 into I2 instead of the normal case. */
2608 int i3_subst_into_i2 = 0;
2609 /* Notes that I1, I2 or I3 is a MULT operation. */
2610 int have_mult = 0;
2611 int swap_i2i3 = 0;
2612 int changed_i3_dest = 0;
2614 int maxreg;
2615 rtx_insn *temp_insn;
2616 rtx temp_expr;
2617 struct insn_link *link;
2618 rtx other_pat = 0;
2619 rtx new_other_notes;
2620 int i;
2622 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2623 never be). */
2624 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2625 return 0;
2627 /* Only try four-insn combinations when there's high likelihood of
2628 success. Look for simple insns, such as loads of constants or
2629 binary operations involving a constant. */
2630 if (i0)
2632 int i;
2633 int ngood = 0;
2634 int nshift = 0;
2635 rtx set0, set3;
2637 if (!flag_expensive_optimizations)
2638 return 0;
2640 for (i = 0; i < 4; i++)
2642 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2643 rtx set = single_set (insn);
2644 rtx src;
2645 if (!set)
2646 continue;
2647 src = SET_SRC (set);
2648 if (CONSTANT_P (src))
2650 ngood += 2;
2651 break;
2653 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2654 ngood++;
2655 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2656 || GET_CODE (src) == LSHIFTRT)
2657 nshift++;
2660 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2661 are likely manipulating its value. Ideally we'll be able to combine
2662 all four insns into a bitfield insertion of some kind.
2664 Note the source in I0 might be inside a sign/zero extension and the
2665 memory modes in I0 and I3 might be different. So extract the address
2666 from the destination of I3 and search for it in the source of I0.
2668 In the event that there's a match but the source/dest do not actually
2669 refer to the same memory, the worst that happens is we try some
2670 combinations that we wouldn't have otherwise. */
2671 if ((set0 = single_set (i0))
2672 /* Ensure the source of SET0 is a MEM, possibly buried inside
2673 an extension. */
2674 && (GET_CODE (SET_SRC (set0)) == MEM
2675 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2676 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2677 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2678 && (set3 = single_set (i3))
2679 /* Ensure the destination of SET3 is a MEM. */
2680 && GET_CODE (SET_DEST (set3)) == MEM
2681 /* Would it be better to extract the base address for the MEM
2682 in SET3 and look for that? I don't have cases where it matters
2683 but I could envision such cases. */
2684 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2685 ngood += 2;
2687 if (ngood < 2 && nshift < 2)
2688 return 0;
2691 /* Exit early if one of the insns involved can't be used for
2692 combinations. */
2693 if (CALL_P (i2)
2694 || (i1 && CALL_P (i1))
2695 || (i0 && CALL_P (i0))
2696 || cant_combine_insn_p (i3)
2697 || cant_combine_insn_p (i2)
2698 || (i1 && cant_combine_insn_p (i1))
2699 || (i0 && cant_combine_insn_p (i0))
2700 || likely_spilled_retval_p (i3))
2701 return 0;
2703 combine_attempts++;
2704 undobuf.other_insn = 0;
2706 /* Reset the hard register usage information. */
2707 CLEAR_HARD_REG_SET (newpat_used_regs);
2709 if (dump_file && (dump_flags & TDF_DETAILS))
2711 if (i0)
2712 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2713 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2714 else if (i1)
2715 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2716 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2717 else
2718 fprintf (dump_file, "\nTrying %d -> %d:\n",
2719 INSN_UID (i2), INSN_UID (i3));
2722 /* If multiple insns feed into one of I2 or I3, they can be in any
2723 order. To simplify the code below, reorder them in sequence. */
2724 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2725 temp_insn = i2, i2 = i0, i0 = temp_insn;
2726 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2727 temp_insn = i1, i1 = i0, i0 = temp_insn;
2728 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2729 temp_insn = i1, i1 = i2, i2 = temp_insn;
2731 added_links_insn = 0;
2733 /* First check for one important special case that the code below will
2734 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2735 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2736 we may be able to replace that destination with the destination of I3.
2737 This occurs in the common code where we compute both a quotient and
2738 remainder into a structure, in which case we want to do the computation
2739 directly into the structure to avoid register-register copies.
2741 Note that this case handles both multiple sets in I2 and also cases
2742 where I2 has a number of CLOBBERs inside the PARALLEL.
2744 We make very conservative checks below and only try to handle the
2745 most common cases of this. For example, we only handle the case
2746 where I2 and I3 are adjacent to avoid making difficult register
2747 usage tests. */
2749 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2750 && REG_P (SET_SRC (PATTERN (i3)))
2751 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2752 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2753 && GET_CODE (PATTERN (i2)) == PARALLEL
2754 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2755 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2756 below would need to check what is inside (and reg_overlap_mentioned_p
2757 doesn't support those codes anyway). Don't allow those destinations;
2758 the resulting insn isn't likely to be recognized anyway. */
2759 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2760 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2761 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2762 SET_DEST (PATTERN (i3)))
2763 && next_active_insn (i2) == i3)
2765 rtx p2 = PATTERN (i2);
2767 /* Make sure that the destination of I3,
2768 which we are going to substitute into one output of I2,
2769 is not used within another output of I2. We must avoid making this:
2770 (parallel [(set (mem (reg 69)) ...)
2771 (set (reg 69) ...)])
2772 which is not well-defined as to order of actions.
2773 (Besides, reload can't handle output reloads for this.)
2775 The problem can also happen if the dest of I3 is a memory ref,
2776 if another dest in I2 is an indirect memory ref. */
2777 for (i = 0; i < XVECLEN (p2, 0); i++)
2778 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2779 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2780 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2781 SET_DEST (XVECEXP (p2, 0, i))))
2782 break;
2784 /* Make sure this PARALLEL is not an asm. We do not allow combining
2785 that usually (see can_combine_p), so do not here either. */
2786 for (i = 0; i < XVECLEN (p2, 0); i++)
2787 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2788 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2789 break;
2791 if (i == XVECLEN (p2, 0))
2792 for (i = 0; i < XVECLEN (p2, 0); i++)
2793 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2794 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2796 combine_merges++;
2798 subst_insn = i3;
2799 subst_low_luid = DF_INSN_LUID (i2);
2801 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2802 i2src = SET_SRC (XVECEXP (p2, 0, i));
2803 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2804 i2dest_killed = dead_or_set_p (i2, i2dest);
2806 /* Replace the dest in I2 with our dest and make the resulting
2807 insn the new pattern for I3. Then skip to where we validate
2808 the pattern. Everything was set up above. */
2809 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2810 newpat = p2;
2811 i3_subst_into_i2 = 1;
2812 goto validate_replacement;
2816 /* If I2 is setting a pseudo to a constant and I3 is setting some
2817 sub-part of it to another constant, merge them by making a new
2818 constant. */
2819 if (i1 == 0
2820 && (temp_expr = single_set (i2)) != 0
2821 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2822 && GET_CODE (PATTERN (i3)) == SET
2823 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2824 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2826 rtx dest = SET_DEST (PATTERN (i3));
2827 int offset = -1;
2828 int width = 0;
2830 if (GET_CODE (dest) == ZERO_EXTRACT)
2832 if (CONST_INT_P (XEXP (dest, 1))
2833 && CONST_INT_P (XEXP (dest, 2)))
2835 width = INTVAL (XEXP (dest, 1));
2836 offset = INTVAL (XEXP (dest, 2));
2837 dest = XEXP (dest, 0);
2838 if (BITS_BIG_ENDIAN)
2839 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2842 else
2844 if (GET_CODE (dest) == STRICT_LOW_PART)
2845 dest = XEXP (dest, 0);
2846 width = GET_MODE_PRECISION (GET_MODE (dest));
2847 offset = 0;
2850 if (offset >= 0)
2852 /* If this is the low part, we're done. */
2853 if (subreg_lowpart_p (dest))
2855 /* Handle the case where inner is twice the size of outer. */
2856 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2857 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2858 offset += GET_MODE_PRECISION (GET_MODE (dest));
2859 /* Otherwise give up for now. */
2860 else
2861 offset = -1;
2864 if (offset >= 0)
2866 rtx inner = SET_SRC (PATTERN (i3));
2867 rtx outer = SET_SRC (temp_expr);
2869 wide_int o
2870 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2871 std::make_pair (inner, GET_MODE (dest)),
2872 offset, width);
2874 combine_merges++;
2875 subst_insn = i3;
2876 subst_low_luid = DF_INSN_LUID (i2);
2877 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2878 i2dest = SET_DEST (temp_expr);
2879 i2dest_killed = dead_or_set_p (i2, i2dest);
2881 /* Replace the source in I2 with the new constant and make the
2882 resulting insn the new pattern for I3. Then skip to where we
2883 validate the pattern. Everything was set up above. */
2884 SUBST (SET_SRC (temp_expr),
2885 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2887 newpat = PATTERN (i2);
2889 /* The dest of I3 has been replaced with the dest of I2. */
2890 changed_i3_dest = 1;
2891 goto validate_replacement;
2895 #ifndef HAVE_cc0
2896 /* If we have no I1 and I2 looks like:
2897 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2898 (set Y OP)])
2899 make up a dummy I1 that is
2900 (set Y OP)
2901 and change I2 to be
2902 (set (reg:CC X) (compare:CC Y (const_int 0)))
2904 (We can ignore any trailing CLOBBERs.)
2906 This undoes a previous combination and allows us to match a branch-and-
2907 decrement insn. */
2909 if (i1 == 0
2910 && is_parallel_of_n_reg_sets (i2, 2)
2911 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2912 == MODE_CC)
2913 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2914 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2915 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2916 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2917 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2918 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2920 /* We make I1 with the same INSN_UID as I2. This gives it
2921 the same DF_INSN_LUID for value tracking. Our fake I1 will
2922 never appear in the insn stream so giving it the same INSN_UID
2923 as I2 will not cause a problem. */
2925 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2926 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2927 -1, NULL_RTX);
2928 INSN_UID (i1) = INSN_UID (i2);
2930 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2931 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2932 SET_DEST (PATTERN (i1)));
2933 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2934 SUBST_LINK (LOG_LINKS (i2),
2935 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2938 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2939 make those two SETs separate I1 and I2 insns, and make an I0 that is
2940 the original I1. */
2941 if (i0 == 0
2942 && is_parallel_of_n_reg_sets (i2, 2)
2943 && can_split_parallel_of_n_reg_sets (i2, 2)
2944 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2945 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2947 /* If there is no I1, there is no I0 either. */
2948 i0 = i1;
2950 /* We make I1 with the same INSN_UID as I2. This gives it
2951 the same DF_INSN_LUID for value tracking. Our fake I1 will
2952 never appear in the insn stream so giving it the same INSN_UID
2953 as I2 will not cause a problem. */
2955 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2956 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2957 -1, NULL_RTX);
2958 INSN_UID (i1) = INSN_UID (i2);
2960 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2962 #endif
2964 /* Verify that I2 and I1 are valid for combining. */
2965 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2966 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2967 &i1dest, &i1src))
2968 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2969 &i0dest, &i0src)))
2971 undo_all ();
2972 return 0;
2975 /* Record whether I2DEST is used in I2SRC and similarly for the other
2976 cases. Knowing this will help in register status updating below. */
2977 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2978 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2979 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2980 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2981 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2982 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2983 i2dest_killed = dead_or_set_p (i2, i2dest);
2984 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2985 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2987 /* For the earlier insns, determine which of the subsequent ones they
2988 feed. */
2989 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2990 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2991 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2992 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2993 && reg_overlap_mentioned_p (i0dest, i2src))));
2995 /* Ensure that I3's pattern can be the destination of combines. */
2996 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2997 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2998 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2999 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3000 &i3dest_killed))
3002 undo_all ();
3003 return 0;
3006 /* See if any of the insns is a MULT operation. Unless one is, we will
3007 reject a combination that is, since it must be slower. Be conservative
3008 here. */
3009 if (GET_CODE (i2src) == MULT
3010 || (i1 != 0 && GET_CODE (i1src) == MULT)
3011 || (i0 != 0 && GET_CODE (i0src) == MULT)
3012 || (GET_CODE (PATTERN (i3)) == SET
3013 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3014 have_mult = 1;
3016 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3017 We used to do this EXCEPT in one case: I3 has a post-inc in an
3018 output operand. However, that exception can give rise to insns like
3019 mov r3,(r3)+
3020 which is a famous insn on the PDP-11 where the value of r3 used as the
3021 source was model-dependent. Avoid this sort of thing. */
3023 #if 0
3024 if (!(GET_CODE (PATTERN (i3)) == SET
3025 && REG_P (SET_SRC (PATTERN (i3)))
3026 && MEM_P (SET_DEST (PATTERN (i3)))
3027 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3028 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3029 /* It's not the exception. */
3030 #endif
3031 #ifdef AUTO_INC_DEC
3033 rtx link;
3034 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3035 if (REG_NOTE_KIND (link) == REG_INC
3036 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3037 || (i1 != 0
3038 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3040 undo_all ();
3041 return 0;
3044 #endif
3046 /* See if the SETs in I1 or I2 need to be kept around in the merged
3047 instruction: whenever the value set there is still needed past I3.
3048 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3050 For the SET in I1, we have two cases: if I1 and I2 independently feed
3051 into I3, the set in I1 needs to be kept around unless I1DEST dies
3052 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3053 in I1 needs to be kept around unless I1DEST dies or is set in either
3054 I2 or I3. The same considerations apply to I0. */
3056 added_sets_2 = !dead_or_set_p (i3, i2dest);
3058 if (i1)
3059 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3060 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3061 else
3062 added_sets_1 = 0;
3064 if (i0)
3065 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3066 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3067 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3068 && dead_or_set_p (i2, i0dest)));
3069 else
3070 added_sets_0 = 0;
3072 /* We are about to copy insns for the case where they need to be kept
3073 around. Check that they can be copied in the merged instruction. */
3075 if (targetm.cannot_copy_insn_p
3076 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3077 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3078 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3080 undo_all ();
3081 return 0;
3084 /* If the set in I2 needs to be kept around, we must make a copy of
3085 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3086 PATTERN (I2), we are only substituting for the original I1DEST, not into
3087 an already-substituted copy. This also prevents making self-referential
3088 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3089 I2DEST. */
3091 if (added_sets_2)
3093 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3094 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3095 else
3096 i2pat = copy_rtx (PATTERN (i2));
3099 if (added_sets_1)
3101 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3102 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3103 else
3104 i1pat = copy_rtx (PATTERN (i1));
3107 if (added_sets_0)
3109 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3110 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3111 else
3112 i0pat = copy_rtx (PATTERN (i0));
3115 combine_merges++;
3117 /* Substitute in the latest insn for the regs set by the earlier ones. */
3119 maxreg = max_reg_num ();
3121 subst_insn = i3;
3123 #ifndef HAVE_cc0
3124 /* Many machines that don't use CC0 have insns that can both perform an
3125 arithmetic operation and set the condition code. These operations will
3126 be represented as a PARALLEL with the first element of the vector
3127 being a COMPARE of an arithmetic operation with the constant zero.
3128 The second element of the vector will set some pseudo to the result
3129 of the same arithmetic operation. If we simplify the COMPARE, we won't
3130 match such a pattern and so will generate an extra insn. Here we test
3131 for this case, where both the comparison and the operation result are
3132 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3133 I2SRC. Later we will make the PARALLEL that contains I2. */
3135 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3136 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3137 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3138 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3140 rtx newpat_dest;
3141 rtx *cc_use_loc = NULL;
3142 rtx_insn *cc_use_insn = NULL;
3143 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3144 machine_mode compare_mode, orig_compare_mode;
3145 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3147 newpat = PATTERN (i3);
3148 newpat_dest = SET_DEST (newpat);
3149 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3151 if (undobuf.other_insn == 0
3152 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3153 &cc_use_insn)))
3155 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3156 compare_code = simplify_compare_const (compare_code,
3157 GET_MODE (i2dest), op0, &op1);
3158 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3161 /* Do the rest only if op1 is const0_rtx, which may be the
3162 result of simplification. */
3163 if (op1 == const0_rtx)
3165 /* If a single use of the CC is found, prepare to modify it
3166 when SELECT_CC_MODE returns a new CC-class mode, or when
3167 the above simplify_compare_const() returned a new comparison
3168 operator. undobuf.other_insn is assigned the CC use insn
3169 when modifying it. */
3170 if (cc_use_loc)
3172 #ifdef SELECT_CC_MODE
3173 machine_mode new_mode
3174 = SELECT_CC_MODE (compare_code, op0, op1);
3175 if (new_mode != orig_compare_mode
3176 && can_change_dest_mode (SET_DEST (newpat),
3177 added_sets_2, new_mode))
3179 unsigned int regno = REGNO (newpat_dest);
3180 compare_mode = new_mode;
3181 if (regno < FIRST_PSEUDO_REGISTER)
3182 newpat_dest = gen_rtx_REG (compare_mode, regno);
3183 else
3185 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3186 newpat_dest = regno_reg_rtx[regno];
3189 #endif
3190 /* Cases for modifying the CC-using comparison. */
3191 if (compare_code != orig_compare_code
3192 /* ??? Do we need to verify the zero rtx? */
3193 && XEXP (*cc_use_loc, 1) == const0_rtx)
3195 /* Replace cc_use_loc with entire new RTX. */
3196 SUBST (*cc_use_loc,
3197 gen_rtx_fmt_ee (compare_code, compare_mode,
3198 newpat_dest, const0_rtx));
3199 undobuf.other_insn = cc_use_insn;
3201 else if (compare_mode != orig_compare_mode)
3203 /* Just replace the CC reg with a new mode. */
3204 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3205 undobuf.other_insn = cc_use_insn;
3209 /* Now we modify the current newpat:
3210 First, SET_DEST(newpat) is updated if the CC mode has been
3211 altered. For targets without SELECT_CC_MODE, this should be
3212 optimized away. */
3213 if (compare_mode != orig_compare_mode)
3214 SUBST (SET_DEST (newpat), newpat_dest);
3215 /* This is always done to propagate i2src into newpat. */
3216 SUBST (SET_SRC (newpat),
3217 gen_rtx_COMPARE (compare_mode, op0, op1));
3218 /* Create new version of i2pat if needed; the below PARALLEL
3219 creation needs this to work correctly. */
3220 if (! rtx_equal_p (i2src, op0))
3221 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3222 i2_is_used = 1;
3225 #endif
3227 if (i2_is_used == 0)
3229 /* It is possible that the source of I2 or I1 may be performing
3230 an unneeded operation, such as a ZERO_EXTEND of something
3231 that is known to have the high part zero. Handle that case
3232 by letting subst look at the inner insns.
3234 Another way to do this would be to have a function that tries
3235 to simplify a single insn instead of merging two or more
3236 insns. We don't do this because of the potential of infinite
3237 loops and because of the potential extra memory required.
3238 However, doing it the way we are is a bit of a kludge and
3239 doesn't catch all cases.
3241 But only do this if -fexpensive-optimizations since it slows
3242 things down and doesn't usually win.
3244 This is not done in the COMPARE case above because the
3245 unmodified I2PAT is used in the PARALLEL and so a pattern
3246 with a modified I2SRC would not match. */
3248 if (flag_expensive_optimizations)
3250 /* Pass pc_rtx so no substitutions are done, just
3251 simplifications. */
3252 if (i1)
3254 subst_low_luid = DF_INSN_LUID (i1);
3255 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3258 subst_low_luid = DF_INSN_LUID (i2);
3259 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3262 n_occurrences = 0; /* `subst' counts here */
3263 subst_low_luid = DF_INSN_LUID (i2);
3265 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3266 copy of I2SRC each time we substitute it, in order to avoid creating
3267 self-referential RTL when we will be substituting I1SRC for I1DEST
3268 later. Likewise if I0 feeds into I2, either directly or indirectly
3269 through I1, and I0DEST is in I0SRC. */
3270 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3271 (i1_feeds_i2_n && i1dest_in_i1src)
3272 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3273 && i0dest_in_i0src));
3274 substed_i2 = 1;
3276 /* Record whether I2's body now appears within I3's body. */
3277 i2_is_used = n_occurrences;
3280 /* If we already got a failure, don't try to do more. Otherwise, try to
3281 substitute I1 if we have it. */
3283 if (i1 && GET_CODE (newpat) != CLOBBER)
3285 /* Check that an autoincrement side-effect on I1 has not been lost.
3286 This happens if I1DEST is mentioned in I2 and dies there, and
3287 has disappeared from the new pattern. */
3288 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3289 && i1_feeds_i2_n
3290 && dead_or_set_p (i2, i1dest)
3291 && !reg_overlap_mentioned_p (i1dest, newpat))
3292 /* Before we can do this substitution, we must redo the test done
3293 above (see detailed comments there) that ensures I1DEST isn't
3294 mentioned in any SETs in NEWPAT that are field assignments. */
3295 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3296 0, 0, 0))
3298 undo_all ();
3299 return 0;
3302 n_occurrences = 0;
3303 subst_low_luid = DF_INSN_LUID (i1);
3305 /* If the following substitution will modify I1SRC, make a copy of it
3306 for the case where it is substituted for I1DEST in I2PAT later. */
3307 if (added_sets_2 && i1_feeds_i2_n)
3308 i1src_copy = copy_rtx (i1src);
3310 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3311 copy of I1SRC each time we substitute it, in order to avoid creating
3312 self-referential RTL when we will be substituting I0SRC for I0DEST
3313 later. */
3314 newpat = subst (newpat, i1dest, i1src, 0, 0,
3315 i0_feeds_i1_n && i0dest_in_i0src);
3316 substed_i1 = 1;
3318 /* Record whether I1's body now appears within I3's body. */
3319 i1_is_used = n_occurrences;
3322 /* Likewise for I0 if we have it. */
3324 if (i0 && GET_CODE (newpat) != CLOBBER)
3326 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3327 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3328 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3329 && !reg_overlap_mentioned_p (i0dest, newpat))
3330 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3331 0, 0, 0))
3333 undo_all ();
3334 return 0;
3337 /* If the following substitution will modify I0SRC, make a copy of it
3338 for the case where it is substituted for I0DEST in I1PAT later. */
3339 if (added_sets_1 && i0_feeds_i1_n)
3340 i0src_copy = copy_rtx (i0src);
3341 /* And a copy for I0DEST in I2PAT substitution. */
3342 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3343 || (i0_feeds_i2_n)))
3344 i0src_copy2 = copy_rtx (i0src);
3346 n_occurrences = 0;
3347 subst_low_luid = DF_INSN_LUID (i0);
3348 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3349 substed_i0 = 1;
3352 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3353 to count all the ways that I2SRC and I1SRC can be used. */
3354 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3355 && i2_is_used + added_sets_2 > 1)
3356 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3357 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3358 > 1))
3359 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3360 && (n_occurrences + added_sets_0
3361 + (added_sets_1 && i0_feeds_i1_n)
3362 + (added_sets_2 && i0_feeds_i2_n)
3363 > 1))
3364 /* Fail if we tried to make a new register. */
3365 || max_reg_num () != maxreg
3366 /* Fail if we couldn't do something and have a CLOBBER. */
3367 || GET_CODE (newpat) == CLOBBER
3368 /* Fail if this new pattern is a MULT and we didn't have one before
3369 at the outer level. */
3370 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3371 && ! have_mult))
3373 undo_all ();
3374 return 0;
3377 /* If the actions of the earlier insns must be kept
3378 in addition to substituting them into the latest one,
3379 we must make a new PARALLEL for the latest insn
3380 to hold additional the SETs. */
3382 if (added_sets_0 || added_sets_1 || added_sets_2)
3384 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3385 combine_extras++;
3387 if (GET_CODE (newpat) == PARALLEL)
3389 rtvec old = XVEC (newpat, 0);
3390 total_sets = XVECLEN (newpat, 0) + extra_sets;
3391 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3392 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3393 sizeof (old->elem[0]) * old->num_elem);
3395 else
3397 rtx old = newpat;
3398 total_sets = 1 + extra_sets;
3399 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3400 XVECEXP (newpat, 0, 0) = old;
3403 if (added_sets_0)
3404 XVECEXP (newpat, 0, --total_sets) = i0pat;
3406 if (added_sets_1)
3408 rtx t = i1pat;
3409 if (i0_feeds_i1_n)
3410 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3412 XVECEXP (newpat, 0, --total_sets) = t;
3414 if (added_sets_2)
3416 rtx t = i2pat;
3417 if (i1_feeds_i2_n)
3418 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3419 i0_feeds_i1_n && i0dest_in_i0src);
3420 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3421 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3423 XVECEXP (newpat, 0, --total_sets) = t;
3427 validate_replacement:
3429 /* Note which hard regs this insn has as inputs. */
3430 mark_used_regs_combine (newpat);
3432 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3433 consider splitting this pattern, we might need these clobbers. */
3434 if (i1 && GET_CODE (newpat) == PARALLEL
3435 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3437 int len = XVECLEN (newpat, 0);
3439 newpat_vec_with_clobbers = rtvec_alloc (len);
3440 for (i = 0; i < len; i++)
3441 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3444 /* We have recognized nothing yet. */
3445 insn_code_number = -1;
3447 /* See if this is a PARALLEL of two SETs where one SET's destination is
3448 a register that is unused and this isn't marked as an instruction that
3449 might trap in an EH region. In that case, we just need the other SET.
3450 We prefer this over the PARALLEL.
3452 This can occur when simplifying a divmod insn. We *must* test for this
3453 case here because the code below that splits two independent SETs doesn't
3454 handle this case correctly when it updates the register status.
3456 It's pointless doing this if we originally had two sets, one from
3457 i3, and one from i2. Combining then splitting the parallel results
3458 in the original i2 again plus an invalid insn (which we delete).
3459 The net effect is only to move instructions around, which makes
3460 debug info less accurate. */
3462 if (!(added_sets_2 && i1 == 0)
3463 && GET_CODE (newpat) == PARALLEL
3464 && XVECLEN (newpat, 0) == 2
3465 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3466 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3467 && asm_noperands (newpat) < 0)
3469 rtx set0 = XVECEXP (newpat, 0, 0);
3470 rtx set1 = XVECEXP (newpat, 0, 1);
3471 rtx oldpat = newpat;
3473 if (((REG_P (SET_DEST (set1))
3474 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3475 || (GET_CODE (SET_DEST (set1)) == SUBREG
3476 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3477 && insn_nothrow_p (i3)
3478 && !side_effects_p (SET_SRC (set1)))
3480 newpat = set0;
3481 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3484 else if (((REG_P (SET_DEST (set0))
3485 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3486 || (GET_CODE (SET_DEST (set0)) == SUBREG
3487 && find_reg_note (i3, REG_UNUSED,
3488 SUBREG_REG (SET_DEST (set0)))))
3489 && insn_nothrow_p (i3)
3490 && !side_effects_p (SET_SRC (set0)))
3492 newpat = set1;
3493 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3495 if (insn_code_number >= 0)
3496 changed_i3_dest = 1;
3499 if (insn_code_number < 0)
3500 newpat = oldpat;
3503 /* Is the result of combination a valid instruction? */
3504 if (insn_code_number < 0)
3505 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3507 /* If we were combining three insns and the result is a simple SET
3508 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3509 insns. There are two ways to do this. It can be split using a
3510 machine-specific method (like when you have an addition of a large
3511 constant) or by combine in the function find_split_point. */
3513 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3514 && asm_noperands (newpat) < 0)
3516 rtx parallel, *split;
3517 rtx_insn *m_split_insn;
3519 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3520 use I2DEST as a scratch register will help. In the latter case,
3521 convert I2DEST to the mode of the source of NEWPAT if we can. */
3523 m_split_insn = combine_split_insns (newpat, i3);
3525 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3526 inputs of NEWPAT. */
3528 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3529 possible to try that as a scratch reg. This would require adding
3530 more code to make it work though. */
3532 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3534 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3536 /* First try to split using the original register as a
3537 scratch register. */
3538 parallel = gen_rtx_PARALLEL (VOIDmode,
3539 gen_rtvec (2, newpat,
3540 gen_rtx_CLOBBER (VOIDmode,
3541 i2dest)));
3542 m_split_insn = combine_split_insns (parallel, i3);
3544 /* If that didn't work, try changing the mode of I2DEST if
3545 we can. */
3546 if (m_split_insn == 0
3547 && new_mode != GET_MODE (i2dest)
3548 && new_mode != VOIDmode
3549 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3551 machine_mode old_mode = GET_MODE (i2dest);
3552 rtx ni2dest;
3554 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3555 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3556 else
3558 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3559 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3562 parallel = (gen_rtx_PARALLEL
3563 (VOIDmode,
3564 gen_rtvec (2, newpat,
3565 gen_rtx_CLOBBER (VOIDmode,
3566 ni2dest))));
3567 m_split_insn = combine_split_insns (parallel, i3);
3569 if (m_split_insn == 0
3570 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3572 struct undo *buf;
3574 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3575 buf = undobuf.undos;
3576 undobuf.undos = buf->next;
3577 buf->next = undobuf.frees;
3578 undobuf.frees = buf;
3582 i2scratch = m_split_insn != 0;
3585 /* If recog_for_combine has discarded clobbers, try to use them
3586 again for the split. */
3587 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3589 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3590 m_split_insn = combine_split_insns (parallel, i3);
3593 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3595 rtx m_split_pat = PATTERN (m_split_insn);
3596 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3597 if (insn_code_number >= 0)
3598 newpat = m_split_pat;
3600 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3601 && (next_nonnote_nondebug_insn (i2) == i3
3602 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3604 rtx i2set, i3set;
3605 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3606 newi2pat = PATTERN (m_split_insn);
3608 i3set = single_set (NEXT_INSN (m_split_insn));
3609 i2set = single_set (m_split_insn);
3611 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3613 /* If I2 or I3 has multiple SETs, we won't know how to track
3614 register status, so don't use these insns. If I2's destination
3615 is used between I2 and I3, we also can't use these insns. */
3617 if (i2_code_number >= 0 && i2set && i3set
3618 && (next_nonnote_nondebug_insn (i2) == i3
3619 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3620 insn_code_number = recog_for_combine (&newi3pat, i3,
3621 &new_i3_notes);
3622 if (insn_code_number >= 0)
3623 newpat = newi3pat;
3625 /* It is possible that both insns now set the destination of I3.
3626 If so, we must show an extra use of it. */
3628 if (insn_code_number >= 0)
3630 rtx new_i3_dest = SET_DEST (i3set);
3631 rtx new_i2_dest = SET_DEST (i2set);
3633 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3634 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3635 || GET_CODE (new_i3_dest) == SUBREG)
3636 new_i3_dest = XEXP (new_i3_dest, 0);
3638 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3639 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3640 || GET_CODE (new_i2_dest) == SUBREG)
3641 new_i2_dest = XEXP (new_i2_dest, 0);
3643 if (REG_P (new_i3_dest)
3644 && REG_P (new_i2_dest)
3645 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3646 && REGNO (new_i2_dest) < reg_n_sets_max)
3647 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3651 /* If we can split it and use I2DEST, go ahead and see if that
3652 helps things be recognized. Verify that none of the registers
3653 are set between I2 and I3. */
3654 if (insn_code_number < 0
3655 && (split = find_split_point (&newpat, i3, false)) != 0
3656 #ifdef HAVE_cc0
3657 && REG_P (i2dest)
3658 #endif
3659 /* We need I2DEST in the proper mode. If it is a hard register
3660 or the only use of a pseudo, we can change its mode.
3661 Make sure we don't change a hard register to have a mode that
3662 isn't valid for it, or change the number of registers. */
3663 && (GET_MODE (*split) == GET_MODE (i2dest)
3664 || GET_MODE (*split) == VOIDmode
3665 || can_change_dest_mode (i2dest, added_sets_2,
3666 GET_MODE (*split)))
3667 && (next_nonnote_nondebug_insn (i2) == i3
3668 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3669 /* We can't overwrite I2DEST if its value is still used by
3670 NEWPAT. */
3671 && ! reg_referenced_p (i2dest, newpat))
3673 rtx newdest = i2dest;
3674 enum rtx_code split_code = GET_CODE (*split);
3675 machine_mode split_mode = GET_MODE (*split);
3676 bool subst_done = false;
3677 newi2pat = NULL_RTX;
3679 i2scratch = true;
3681 /* *SPLIT may be part of I2SRC, so make sure we have the
3682 original expression around for later debug processing.
3683 We should not need I2SRC any more in other cases. */
3684 if (MAY_HAVE_DEBUG_INSNS)
3685 i2src = copy_rtx (i2src);
3686 else
3687 i2src = NULL;
3689 /* Get NEWDEST as a register in the proper mode. We have already
3690 validated that we can do this. */
3691 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3693 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3694 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3695 else
3697 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3698 newdest = regno_reg_rtx[REGNO (i2dest)];
3702 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3703 an ASHIFT. This can occur if it was inside a PLUS and hence
3704 appeared to be a memory address. This is a kludge. */
3705 if (split_code == MULT
3706 && CONST_INT_P (XEXP (*split, 1))
3707 && INTVAL (XEXP (*split, 1)) > 0
3708 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3710 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3711 XEXP (*split, 0), GEN_INT (i)));
3712 /* Update split_code because we may not have a multiply
3713 anymore. */
3714 split_code = GET_CODE (*split);
3717 #ifdef INSN_SCHEDULING
3718 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3719 be written as a ZERO_EXTEND. */
3720 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3722 #ifdef LOAD_EXTEND_OP
3723 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3724 what it really is. */
3725 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3726 == SIGN_EXTEND)
3727 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3728 SUBREG_REG (*split)));
3729 else
3730 #endif
3731 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3732 SUBREG_REG (*split)));
3734 #endif
3736 /* Attempt to split binary operators using arithmetic identities. */
3737 if (BINARY_P (SET_SRC (newpat))
3738 && split_mode == GET_MODE (SET_SRC (newpat))
3739 && ! side_effects_p (SET_SRC (newpat)))
3741 rtx setsrc = SET_SRC (newpat);
3742 machine_mode mode = GET_MODE (setsrc);
3743 enum rtx_code code = GET_CODE (setsrc);
3744 rtx src_op0 = XEXP (setsrc, 0);
3745 rtx src_op1 = XEXP (setsrc, 1);
3747 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3748 if (rtx_equal_p (src_op0, src_op1))
3750 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3751 SUBST (XEXP (setsrc, 0), newdest);
3752 SUBST (XEXP (setsrc, 1), newdest);
3753 subst_done = true;
3755 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3756 else if ((code == PLUS || code == MULT)
3757 && GET_CODE (src_op0) == code
3758 && GET_CODE (XEXP (src_op0, 0)) == code
3759 && (INTEGRAL_MODE_P (mode)
3760 || (FLOAT_MODE_P (mode)
3761 && flag_unsafe_math_optimizations)))
3763 rtx p = XEXP (XEXP (src_op0, 0), 0);
3764 rtx q = XEXP (XEXP (src_op0, 0), 1);
3765 rtx r = XEXP (src_op0, 1);
3766 rtx s = src_op1;
3768 /* Split both "((X op Y) op X) op Y" and
3769 "((X op Y) op Y) op X" as "T op T" where T is
3770 "X op Y". */
3771 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3772 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3774 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3775 XEXP (src_op0, 0));
3776 SUBST (XEXP (setsrc, 0), newdest);
3777 SUBST (XEXP (setsrc, 1), newdest);
3778 subst_done = true;
3780 /* Split "((X op X) op Y) op Y)" as "T op T" where
3781 T is "X op Y". */
3782 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3784 rtx tmp = simplify_gen_binary (code, mode, p, r);
3785 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3786 SUBST (XEXP (setsrc, 0), newdest);
3787 SUBST (XEXP (setsrc, 1), newdest);
3788 subst_done = true;
3793 if (!subst_done)
3795 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3796 SUBST (*split, newdest);
3799 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3801 /* recog_for_combine might have added CLOBBERs to newi2pat.
3802 Make sure NEWPAT does not depend on the clobbered regs. */
3803 if (GET_CODE (newi2pat) == PARALLEL)
3804 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3805 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3807 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3808 if (reg_overlap_mentioned_p (reg, newpat))
3810 undo_all ();
3811 return 0;
3815 /* If the split point was a MULT and we didn't have one before,
3816 don't use one now. */
3817 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3818 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3822 /* Check for a case where we loaded from memory in a narrow mode and
3823 then sign extended it, but we need both registers. In that case,
3824 we have a PARALLEL with both loads from the same memory location.
3825 We can split this into a load from memory followed by a register-register
3826 copy. This saves at least one insn, more if register allocation can
3827 eliminate the copy.
3829 We cannot do this if the destination of the first assignment is a
3830 condition code register or cc0. We eliminate this case by making sure
3831 the SET_DEST and SET_SRC have the same mode.
3833 We cannot do this if the destination of the second assignment is
3834 a register that we have already assumed is zero-extended. Similarly
3835 for a SUBREG of such a register. */
3837 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3838 && GET_CODE (newpat) == PARALLEL
3839 && XVECLEN (newpat, 0) == 2
3840 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3841 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3842 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3843 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3844 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3845 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3846 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3847 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3848 DF_INSN_LUID (i2))
3849 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3850 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3851 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3852 (REG_P (temp_expr)
3853 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3854 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3855 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3856 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3857 != GET_MODE_MASK (word_mode))))
3858 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3859 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3860 (REG_P (temp_expr)
3861 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3862 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3863 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3864 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3865 != GET_MODE_MASK (word_mode)))))
3866 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3867 SET_SRC (XVECEXP (newpat, 0, 1)))
3868 && ! find_reg_note (i3, REG_UNUSED,
3869 SET_DEST (XVECEXP (newpat, 0, 0))))
3871 rtx ni2dest;
3873 newi2pat = XVECEXP (newpat, 0, 0);
3874 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3875 newpat = XVECEXP (newpat, 0, 1);
3876 SUBST (SET_SRC (newpat),
3877 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3878 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3880 if (i2_code_number >= 0)
3881 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3883 if (insn_code_number >= 0)
3884 swap_i2i3 = 1;
3887 /* Similarly, check for a case where we have a PARALLEL of two independent
3888 SETs but we started with three insns. In this case, we can do the sets
3889 as two separate insns. This case occurs when some SET allows two
3890 other insns to combine, but the destination of that SET is still live.
3892 Also do this if we started with two insns and (at least) one of the
3893 resulting sets is a noop; this noop will be deleted later. */
3895 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3896 && GET_CODE (newpat) == PARALLEL
3897 && XVECLEN (newpat, 0) == 2
3898 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3899 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3900 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3901 || set_noop_p (XVECEXP (newpat, 0, 1)))
3902 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3903 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3904 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3905 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3906 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3907 XVECEXP (newpat, 0, 0))
3908 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3909 XVECEXP (newpat, 0, 1))
3910 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3911 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3913 rtx set0 = XVECEXP (newpat, 0, 0);
3914 rtx set1 = XVECEXP (newpat, 0, 1);
3916 /* Normally, it doesn't matter which of the two is done first,
3917 but the one that references cc0 can't be the second, and
3918 one which uses any regs/memory set in between i2 and i3 can't
3919 be first. The PARALLEL might also have been pre-existing in i3,
3920 so we need to make sure that we won't wrongly hoist a SET to i2
3921 that would conflict with a death note present in there. */
3922 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3923 && !(REG_P (SET_DEST (set1))
3924 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3925 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3926 && find_reg_note (i2, REG_DEAD,
3927 SUBREG_REG (SET_DEST (set1))))
3928 #ifdef HAVE_cc0
3929 && !reg_referenced_p (cc0_rtx, set0)
3930 #endif
3931 /* If I3 is a jump, ensure that set0 is a jump so that
3932 we do not create invalid RTL. */
3933 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3936 newi2pat = set1;
3937 newpat = set0;
3939 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3940 && !(REG_P (SET_DEST (set0))
3941 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3942 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3943 && find_reg_note (i2, REG_DEAD,
3944 SUBREG_REG (SET_DEST (set0))))
3945 #ifdef HAVE_cc0
3946 && !reg_referenced_p (cc0_rtx, set1)
3947 #endif
3948 /* If I3 is a jump, ensure that set1 is a jump so that
3949 we do not create invalid RTL. */
3950 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3953 newi2pat = set0;
3954 newpat = set1;
3956 else
3958 undo_all ();
3959 return 0;
3962 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3964 if (i2_code_number >= 0)
3966 /* recog_for_combine might have added CLOBBERs to newi2pat.
3967 Make sure NEWPAT does not depend on the clobbered regs. */
3968 if (GET_CODE (newi2pat) == PARALLEL)
3970 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3971 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3973 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3974 if (reg_overlap_mentioned_p (reg, newpat))
3976 undo_all ();
3977 return 0;
3982 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3986 /* If it still isn't recognized, fail and change things back the way they
3987 were. */
3988 if ((insn_code_number < 0
3989 /* Is the result a reasonable ASM_OPERANDS? */
3990 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3992 undo_all ();
3993 return 0;
3996 /* If we had to change another insn, make sure it is valid also. */
3997 if (undobuf.other_insn)
3999 CLEAR_HARD_REG_SET (newpat_used_regs);
4001 other_pat = PATTERN (undobuf.other_insn);
4002 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4003 &new_other_notes);
4005 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4007 undo_all ();
4008 return 0;
4012 #ifdef HAVE_cc0
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. */
4016 rtx_insn *p = prev_nonnote_insn (i3);
4017 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4018 && sets_cc0_p (newi2pat))
4020 undo_all ();
4021 return 0;
4024 #endif
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 (VOIDmode, reg,
4757 XEXP (x, 0)),
4758 subst_insn);
4760 /* This should have produced two insns, each of which sets our
4761 placeholder. If the source of the second is a valid address,
4762 we can make put both sources together and make a split point
4763 in the middle. */
4765 if (seq
4766 && NEXT_INSN (seq) != NULL_RTX
4767 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4768 && NONJUMP_INSN_P (seq)
4769 && GET_CODE (PATTERN (seq)) == SET
4770 && SET_DEST (PATTERN (seq)) == reg
4771 && ! reg_mentioned_p (reg,
4772 SET_SRC (PATTERN (seq)))
4773 && NONJUMP_INSN_P (NEXT_INSN (seq))
4774 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4775 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4776 && memory_address_addr_space_p
4777 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4778 MEM_ADDR_SPACE (x)))
4780 rtx src1 = SET_SRC (PATTERN (seq));
4781 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4783 /* Replace the placeholder in SRC2 with SRC1. If we can
4784 find where in SRC2 it was placed, that can become our
4785 split point and we can replace this address with SRC2.
4786 Just try two obvious places. */
4788 src2 = replace_rtx (src2, reg, src1);
4789 split = 0;
4790 if (XEXP (src2, 0) == src1)
4791 split = &XEXP (src2, 0);
4792 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4793 && XEXP (XEXP (src2, 0), 0) == src1)
4794 split = &XEXP (XEXP (src2, 0), 0);
4796 if (split)
4798 SUBST (XEXP (x, 0), src2);
4799 return split;
4803 /* If that didn't work, perhaps the first operand is complex and
4804 needs to be computed separately, so make a split point there.
4805 This will occur on machines that just support REG + CONST
4806 and have a constant moved through some previous computation. */
4808 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4809 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4810 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4811 return &XEXP (XEXP (x, 0), 0);
4814 /* If we have a PLUS whose first operand is complex, try computing it
4815 separately by making a split there. */
4816 if (GET_CODE (XEXP (x, 0)) == PLUS
4817 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4818 MEM_ADDR_SPACE (x))
4819 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4820 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4821 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4822 return &XEXP (XEXP (x, 0), 0);
4823 break;
4825 case SET:
4826 #ifdef HAVE_cc0
4827 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4828 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4829 we need to put the operand into a register. So split at that
4830 point. */
4832 if (SET_DEST (x) == cc0_rtx
4833 && GET_CODE (SET_SRC (x)) != COMPARE
4834 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4835 && !OBJECT_P (SET_SRC (x))
4836 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4837 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4838 return &SET_SRC (x);
4839 #endif
4841 /* See if we can split SET_SRC as it stands. */
4842 split = find_split_point (&SET_SRC (x), insn, true);
4843 if (split && split != &SET_SRC (x))
4844 return split;
4846 /* See if we can split SET_DEST as it stands. */
4847 split = find_split_point (&SET_DEST (x), insn, false);
4848 if (split && split != &SET_DEST (x))
4849 return split;
4851 /* See if this is a bitfield assignment with everything constant. If
4852 so, this is an IOR of an AND, so split it into that. */
4853 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4854 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4855 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4856 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4857 && CONST_INT_P (SET_SRC (x))
4858 && ((INTVAL (XEXP (SET_DEST (x), 1))
4859 + INTVAL (XEXP (SET_DEST (x), 2)))
4860 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4861 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4863 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4864 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4865 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4866 rtx dest = XEXP (SET_DEST (x), 0);
4867 machine_mode mode = GET_MODE (dest);
4868 unsigned HOST_WIDE_INT mask
4869 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4870 rtx or_mask;
4872 if (BITS_BIG_ENDIAN)
4873 pos = GET_MODE_PRECISION (mode) - len - pos;
4875 or_mask = gen_int_mode (src << pos, mode);
4876 if (src == mask)
4877 SUBST (SET_SRC (x),
4878 simplify_gen_binary (IOR, mode, dest, or_mask));
4879 else
4881 rtx negmask = gen_int_mode (~(mask << pos), mode);
4882 SUBST (SET_SRC (x),
4883 simplify_gen_binary (IOR, mode,
4884 simplify_gen_binary (AND, mode,
4885 dest, negmask),
4886 or_mask));
4889 SUBST (SET_DEST (x), dest);
4891 split = find_split_point (&SET_SRC (x), insn, true);
4892 if (split && split != &SET_SRC (x))
4893 return split;
4896 /* Otherwise, see if this is an operation that we can split into two.
4897 If so, try to split that. */
4898 code = GET_CODE (SET_SRC (x));
4900 switch (code)
4902 case AND:
4903 /* If we are AND'ing with a large constant that is only a single
4904 bit and the result is only being used in a context where we
4905 need to know if it is zero or nonzero, replace it with a bit
4906 extraction. This will avoid the large constant, which might
4907 have taken more than one insn to make. If the constant were
4908 not a valid argument to the AND but took only one insn to make,
4909 this is no worse, but if it took more than one insn, it will
4910 be better. */
4912 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4913 && REG_P (XEXP (SET_SRC (x), 0))
4914 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4915 && REG_P (SET_DEST (x))
4916 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4917 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4918 && XEXP (*split, 0) == SET_DEST (x)
4919 && XEXP (*split, 1) == const0_rtx)
4921 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4922 XEXP (SET_SRC (x), 0),
4923 pos, NULL_RTX, 1, 1, 0, 0);
4924 if (extraction != 0)
4926 SUBST (SET_SRC (x), extraction);
4927 return find_split_point (loc, insn, false);
4930 break;
4932 case NE:
4933 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4934 is known to be on, this can be converted into a NEG of a shift. */
4935 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4936 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4937 && 1 <= (pos = exact_log2
4938 (nonzero_bits (XEXP (SET_SRC (x), 0),
4939 GET_MODE (XEXP (SET_SRC (x), 0))))))
4941 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4943 SUBST (SET_SRC (x),
4944 gen_rtx_NEG (mode,
4945 gen_rtx_LSHIFTRT (mode,
4946 XEXP (SET_SRC (x), 0),
4947 GEN_INT (pos))));
4949 split = find_split_point (&SET_SRC (x), insn, true);
4950 if (split && split != &SET_SRC (x))
4951 return split;
4953 break;
4955 case SIGN_EXTEND:
4956 inner = XEXP (SET_SRC (x), 0);
4958 /* We can't optimize if either mode is a partial integer
4959 mode as we don't know how many bits are significant
4960 in those modes. */
4961 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4962 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4963 break;
4965 pos = 0;
4966 len = GET_MODE_PRECISION (GET_MODE (inner));
4967 unsignedp = 0;
4968 break;
4970 case SIGN_EXTRACT:
4971 case ZERO_EXTRACT:
4972 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4973 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4975 inner = XEXP (SET_SRC (x), 0);
4976 len = INTVAL (XEXP (SET_SRC (x), 1));
4977 pos = INTVAL (XEXP (SET_SRC (x), 2));
4979 if (BITS_BIG_ENDIAN)
4980 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4981 unsignedp = (code == ZERO_EXTRACT);
4983 break;
4985 default:
4986 break;
4989 if (len && pos >= 0
4990 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4992 machine_mode mode = GET_MODE (SET_SRC (x));
4994 /* For unsigned, we have a choice of a shift followed by an
4995 AND or two shifts. Use two shifts for field sizes where the
4996 constant might be too large. We assume here that we can
4997 always at least get 8-bit constants in an AND insn, which is
4998 true for every current RISC. */
5000 if (unsignedp && len <= 8)
5002 unsigned HOST_WIDE_INT mask
5003 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
5004 SUBST (SET_SRC (x),
5005 gen_rtx_AND (mode,
5006 gen_rtx_LSHIFTRT
5007 (mode, gen_lowpart (mode, inner),
5008 GEN_INT (pos)),
5009 gen_int_mode (mask, mode)));
5011 split = find_split_point (&SET_SRC (x), insn, true);
5012 if (split && split != &SET_SRC (x))
5013 return split;
5015 else
5017 SUBST (SET_SRC (x),
5018 gen_rtx_fmt_ee
5019 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5020 gen_rtx_ASHIFT (mode,
5021 gen_lowpart (mode, inner),
5022 GEN_INT (GET_MODE_PRECISION (mode)
5023 - len - pos)),
5024 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5026 split = find_split_point (&SET_SRC (x), insn, true);
5027 if (split && split != &SET_SRC (x))
5028 return split;
5032 /* See if this is a simple operation with a constant as the second
5033 operand. It might be that this constant is out of range and hence
5034 could be used as a split point. */
5035 if (BINARY_P (SET_SRC (x))
5036 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5037 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5038 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5039 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5040 return &XEXP (SET_SRC (x), 1);
5042 /* Finally, see if this is a simple operation with its first operand
5043 not in a register. The operation might require this operand in a
5044 register, so return it as a split point. We can always do this
5045 because if the first operand were another operation, we would have
5046 already found it as a split point. */
5047 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5048 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5049 return &XEXP (SET_SRC (x), 0);
5051 return 0;
5053 case AND:
5054 case IOR:
5055 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5056 it is better to write this as (not (ior A B)) so we can split it.
5057 Similarly for IOR. */
5058 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5060 SUBST (*loc,
5061 gen_rtx_NOT (GET_MODE (x),
5062 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5063 GET_MODE (x),
5064 XEXP (XEXP (x, 0), 0),
5065 XEXP (XEXP (x, 1), 0))));
5066 return find_split_point (loc, insn, set_src);
5069 /* Many RISC machines have a large set of logical insns. If the
5070 second operand is a NOT, put it first so we will try to split the
5071 other operand first. */
5072 if (GET_CODE (XEXP (x, 1)) == NOT)
5074 rtx tem = XEXP (x, 0);
5075 SUBST (XEXP (x, 0), XEXP (x, 1));
5076 SUBST (XEXP (x, 1), tem);
5078 break;
5080 case PLUS:
5081 case MINUS:
5082 /* Canonicalization can produce (minus A (mult B C)), where C is a
5083 constant. It may be better to try splitting (plus (mult B -C) A)
5084 instead if this isn't a multiply by a power of two. */
5085 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5086 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5087 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5089 machine_mode mode = GET_MODE (x);
5090 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5091 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5092 SUBST (*loc, gen_rtx_PLUS (mode,
5093 gen_rtx_MULT (mode,
5094 XEXP (XEXP (x, 1), 0),
5095 gen_int_mode (other_int,
5096 mode)),
5097 XEXP (x, 0)));
5098 return find_split_point (loc, insn, set_src);
5101 /* Split at a multiply-accumulate instruction. However if this is
5102 the SET_SRC, we likely do not have such an instruction and it's
5103 worthless to try this split. */
5104 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5105 return loc;
5107 default:
5108 break;
5111 /* Otherwise, select our actions depending on our rtx class. */
5112 switch (GET_RTX_CLASS (code))
5114 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5115 case RTX_TERNARY:
5116 split = find_split_point (&XEXP (x, 2), insn, false);
5117 if (split)
5118 return split;
5119 /* ... fall through ... */
5120 case RTX_BIN_ARITH:
5121 case RTX_COMM_ARITH:
5122 case RTX_COMPARE:
5123 case RTX_COMM_COMPARE:
5124 split = find_split_point (&XEXP (x, 1), insn, false);
5125 if (split)
5126 return split;
5127 /* ... fall through ... */
5128 case RTX_UNARY:
5129 /* Some machines have (and (shift ...) ...) insns. If X is not
5130 an AND, but XEXP (X, 0) is, use it as our split point. */
5131 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5132 return &XEXP (x, 0);
5134 split = find_split_point (&XEXP (x, 0), insn, false);
5135 if (split)
5136 return split;
5137 return loc;
5139 default:
5140 /* Otherwise, we don't have a split point. */
5141 return 0;
5145 /* Throughout X, replace FROM with TO, and return the result.
5146 The result is TO if X is FROM;
5147 otherwise the result is X, but its contents may have been modified.
5148 If they were modified, a record was made in undobuf so that
5149 undo_all will (among other things) return X to its original state.
5151 If the number of changes necessary is too much to record to undo,
5152 the excess changes are not made, so the result is invalid.
5153 The changes already made can still be undone.
5154 undobuf.num_undo is incremented for such changes, so by testing that
5155 the caller can tell whether the result is valid.
5157 `n_occurrences' is incremented each time FROM is replaced.
5159 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5161 IN_COND is nonzero if we are at the top level of a condition.
5163 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5164 by copying if `n_occurrences' is nonzero. */
5166 static rtx
5167 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5169 enum rtx_code code = GET_CODE (x);
5170 machine_mode op0_mode = VOIDmode;
5171 const char *fmt;
5172 int len, i;
5173 rtx new_rtx;
5175 /* Two expressions are equal if they are identical copies of a shared
5176 RTX or if they are both registers with the same register number
5177 and mode. */
5179 #define COMBINE_RTX_EQUAL_P(X,Y) \
5180 ((X) == (Y) \
5181 || (REG_P (X) && REG_P (Y) \
5182 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5184 /* Do not substitute into clobbers of regs -- this will never result in
5185 valid RTL. */
5186 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5187 return x;
5189 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5191 n_occurrences++;
5192 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5195 /* If X and FROM are the same register but different modes, they
5196 will not have been seen as equal above. However, the log links code
5197 will make a LOG_LINKS entry for that case. If we do nothing, we
5198 will try to rerecognize our original insn and, when it succeeds,
5199 we will delete the feeding insn, which is incorrect.
5201 So force this insn not to match in this (rare) case. */
5202 if (! in_dest && code == REG && REG_P (from)
5203 && reg_overlap_mentioned_p (x, from))
5204 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5206 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5207 of which may contain things that can be combined. */
5208 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5209 return x;
5211 /* It is possible to have a subexpression appear twice in the insn.
5212 Suppose that FROM is a register that appears within TO.
5213 Then, after that subexpression has been scanned once by `subst',
5214 the second time it is scanned, TO may be found. If we were
5215 to scan TO here, we would find FROM within it and create a
5216 self-referent rtl structure which is completely wrong. */
5217 if (COMBINE_RTX_EQUAL_P (x, to))
5218 return to;
5220 /* Parallel asm_operands need special attention because all of the
5221 inputs are shared across the arms. Furthermore, unsharing the
5222 rtl results in recognition failures. Failure to handle this case
5223 specially can result in circular rtl.
5225 Solve this by doing a normal pass across the first entry of the
5226 parallel, and only processing the SET_DESTs of the subsequent
5227 entries. Ug. */
5229 if (code == PARALLEL
5230 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5231 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5233 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5235 /* If this substitution failed, this whole thing fails. */
5236 if (GET_CODE (new_rtx) == CLOBBER
5237 && XEXP (new_rtx, 0) == const0_rtx)
5238 return new_rtx;
5240 SUBST (XVECEXP (x, 0, 0), new_rtx);
5242 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5244 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5246 if (!REG_P (dest)
5247 && GET_CODE (dest) != CC0
5248 && GET_CODE (dest) != PC)
5250 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5252 /* If this substitution failed, this whole thing fails. */
5253 if (GET_CODE (new_rtx) == CLOBBER
5254 && XEXP (new_rtx, 0) == const0_rtx)
5255 return new_rtx;
5257 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5261 else
5263 len = GET_RTX_LENGTH (code);
5264 fmt = GET_RTX_FORMAT (code);
5266 /* We don't need to process a SET_DEST that is a register, CC0,
5267 or PC, so set up to skip this common case. All other cases
5268 where we want to suppress replacing something inside a
5269 SET_SRC are handled via the IN_DEST operand. */
5270 if (code == SET
5271 && (REG_P (SET_DEST (x))
5272 || GET_CODE (SET_DEST (x)) == CC0
5273 || GET_CODE (SET_DEST (x)) == PC))
5274 fmt = "ie";
5276 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5277 constant. */
5278 if (fmt[0] == 'e')
5279 op0_mode = GET_MODE (XEXP (x, 0));
5281 for (i = 0; i < len; i++)
5283 if (fmt[i] == 'E')
5285 int j;
5286 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5288 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5290 new_rtx = (unique_copy && n_occurrences
5291 ? copy_rtx (to) : to);
5292 n_occurrences++;
5294 else
5296 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5297 unique_copy);
5299 /* If this substitution failed, this whole thing
5300 fails. */
5301 if (GET_CODE (new_rtx) == CLOBBER
5302 && XEXP (new_rtx, 0) == const0_rtx)
5303 return new_rtx;
5306 SUBST (XVECEXP (x, i, j), new_rtx);
5309 else if (fmt[i] == 'e')
5311 /* If this is a register being set, ignore it. */
5312 new_rtx = XEXP (x, i);
5313 if (in_dest
5314 && i == 0
5315 && (((code == SUBREG || code == ZERO_EXTRACT)
5316 && REG_P (new_rtx))
5317 || code == STRICT_LOW_PART))
5320 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5322 /* In general, don't install a subreg involving two
5323 modes not tieable. It can worsen register
5324 allocation, and can even make invalid reload
5325 insns, since the reg inside may need to be copied
5326 from in the outside mode, and that may be invalid
5327 if it is an fp reg copied in integer mode.
5329 We allow two exceptions to this: It is valid if
5330 it is inside another SUBREG and the mode of that
5331 SUBREG and the mode of the inside of TO is
5332 tieable and it is valid if X is a SET that copies
5333 FROM to CC0. */
5335 if (GET_CODE (to) == SUBREG
5336 && ! MODES_TIEABLE_P (GET_MODE (to),
5337 GET_MODE (SUBREG_REG (to)))
5338 && ! (code == SUBREG
5339 && MODES_TIEABLE_P (GET_MODE (x),
5340 GET_MODE (SUBREG_REG (to))))
5341 #ifdef HAVE_cc0
5342 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5343 #endif
5345 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5347 if (code == SUBREG
5348 && REG_P (to)
5349 && REGNO (to) < FIRST_PSEUDO_REGISTER
5350 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5351 SUBREG_BYTE (x),
5352 GET_MODE (x)) < 0)
5353 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5355 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5356 n_occurrences++;
5358 else
5359 /* If we are in a SET_DEST, suppress most cases unless we
5360 have gone inside a MEM, in which case we want to
5361 simplify the address. We assume here that things that
5362 are actually part of the destination have their inner
5363 parts in the first expression. This is true for SUBREG,
5364 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5365 things aside from REG and MEM that should appear in a
5366 SET_DEST. */
5367 new_rtx = subst (XEXP (x, i), from, to,
5368 (((in_dest
5369 && (code == SUBREG || code == STRICT_LOW_PART
5370 || code == ZERO_EXTRACT))
5371 || code == SET)
5372 && i == 0),
5373 code == IF_THEN_ELSE && i == 0,
5374 unique_copy);
5376 /* If we found that we will have to reject this combination,
5377 indicate that by returning the CLOBBER ourselves, rather than
5378 an expression containing it. This will speed things up as
5379 well as prevent accidents where two CLOBBERs are considered
5380 to be equal, thus producing an incorrect simplification. */
5382 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5383 return new_rtx;
5385 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5387 machine_mode mode = GET_MODE (x);
5389 x = simplify_subreg (GET_MODE (x), new_rtx,
5390 GET_MODE (SUBREG_REG (x)),
5391 SUBREG_BYTE (x));
5392 if (! x)
5393 x = gen_rtx_CLOBBER (mode, const0_rtx);
5395 else if (CONST_SCALAR_INT_P (new_rtx)
5396 && GET_CODE (x) == ZERO_EXTEND)
5398 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5399 new_rtx, GET_MODE (XEXP (x, 0)));
5400 gcc_assert (x);
5402 else
5403 SUBST (XEXP (x, i), new_rtx);
5408 /* Check if we are loading something from the constant pool via float
5409 extension; in this case we would undo compress_float_constant
5410 optimization and degenerate constant load to an immediate value. */
5411 if (GET_CODE (x) == FLOAT_EXTEND
5412 && MEM_P (XEXP (x, 0))
5413 && MEM_READONLY_P (XEXP (x, 0)))
5415 rtx tmp = avoid_constant_pool_reference (x);
5416 if (x != tmp)
5417 return x;
5420 /* Try to simplify X. If the simplification changed the code, it is likely
5421 that further simplification will help, so loop, but limit the number
5422 of repetitions that will be performed. */
5424 for (i = 0; i < 4; i++)
5426 /* If X is sufficiently simple, don't bother trying to do anything
5427 with it. */
5428 if (code != CONST_INT && code != REG && code != CLOBBER)
5429 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5431 if (GET_CODE (x) == code)
5432 break;
5434 code = GET_CODE (x);
5436 /* We no longer know the original mode of operand 0 since we
5437 have changed the form of X) */
5438 op0_mode = VOIDmode;
5441 return x;
5444 /* Simplify X, a piece of RTL. We just operate on the expression at the
5445 outer level; call `subst' to simplify recursively. Return the new
5446 expression.
5448 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5449 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5450 of a condition. */
5452 static rtx
5453 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5454 int in_cond)
5456 enum rtx_code code = GET_CODE (x);
5457 machine_mode mode = GET_MODE (x);
5458 rtx temp;
5459 int i;
5461 /* If this is a commutative operation, put a constant last and a complex
5462 expression first. We don't need to do this for comparisons here. */
5463 if (COMMUTATIVE_ARITH_P (x)
5464 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5466 temp = XEXP (x, 0);
5467 SUBST (XEXP (x, 0), XEXP (x, 1));
5468 SUBST (XEXP (x, 1), temp);
5471 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5472 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5473 things. Check for cases where both arms are testing the same
5474 condition.
5476 Don't do anything if all operands are very simple. */
5478 if ((BINARY_P (x)
5479 && ((!OBJECT_P (XEXP (x, 0))
5480 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5481 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5482 || (!OBJECT_P (XEXP (x, 1))
5483 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5484 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5485 || (UNARY_P (x)
5486 && (!OBJECT_P (XEXP (x, 0))
5487 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5488 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5490 rtx cond, true_rtx, false_rtx;
5492 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5493 if (cond != 0
5494 /* If everything is a comparison, what we have is highly unlikely
5495 to be simpler, so don't use it. */
5496 && ! (COMPARISON_P (x)
5497 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5499 rtx cop1 = const0_rtx;
5500 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5502 if (cond_code == NE && COMPARISON_P (cond))
5503 return x;
5505 /* Simplify the alternative arms; this may collapse the true and
5506 false arms to store-flag values. Be careful to use copy_rtx
5507 here since true_rtx or false_rtx might share RTL with x as a
5508 result of the if_then_else_cond call above. */
5509 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5510 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5512 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5513 is unlikely to be simpler. */
5514 if (general_operand (true_rtx, VOIDmode)
5515 && general_operand (false_rtx, VOIDmode))
5517 enum rtx_code reversed;
5519 /* Restarting if we generate a store-flag expression will cause
5520 us to loop. Just drop through in this case. */
5522 /* If the result values are STORE_FLAG_VALUE and zero, we can
5523 just make the comparison operation. */
5524 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5525 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5526 cond, cop1);
5527 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5528 && ((reversed = reversed_comparison_code_parts
5529 (cond_code, cond, cop1, NULL))
5530 != UNKNOWN))
5531 x = simplify_gen_relational (reversed, mode, VOIDmode,
5532 cond, cop1);
5534 /* Likewise, we can make the negate of a comparison operation
5535 if the result values are - STORE_FLAG_VALUE and zero. */
5536 else if (CONST_INT_P (true_rtx)
5537 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5538 && false_rtx == const0_rtx)
5539 x = simplify_gen_unary (NEG, mode,
5540 simplify_gen_relational (cond_code,
5541 mode, VOIDmode,
5542 cond, cop1),
5543 mode);
5544 else if (CONST_INT_P (false_rtx)
5545 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5546 && true_rtx == const0_rtx
5547 && ((reversed = reversed_comparison_code_parts
5548 (cond_code, cond, cop1, NULL))
5549 != UNKNOWN))
5550 x = simplify_gen_unary (NEG, mode,
5551 simplify_gen_relational (reversed,
5552 mode, VOIDmode,
5553 cond, cop1),
5554 mode);
5555 else
5556 return gen_rtx_IF_THEN_ELSE (mode,
5557 simplify_gen_relational (cond_code,
5558 mode,
5559 VOIDmode,
5560 cond,
5561 cop1),
5562 true_rtx, false_rtx);
5564 code = GET_CODE (x);
5565 op0_mode = VOIDmode;
5570 /* Try to fold this expression in case we have constants that weren't
5571 present before. */
5572 temp = 0;
5573 switch (GET_RTX_CLASS (code))
5575 case RTX_UNARY:
5576 if (op0_mode == VOIDmode)
5577 op0_mode = GET_MODE (XEXP (x, 0));
5578 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5579 break;
5580 case RTX_COMPARE:
5581 case RTX_COMM_COMPARE:
5583 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5584 if (cmp_mode == VOIDmode)
5586 cmp_mode = GET_MODE (XEXP (x, 1));
5587 if (cmp_mode == VOIDmode)
5588 cmp_mode = op0_mode;
5590 temp = simplify_relational_operation (code, mode, cmp_mode,
5591 XEXP (x, 0), XEXP (x, 1));
5593 break;
5594 case RTX_COMM_ARITH:
5595 case RTX_BIN_ARITH:
5596 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5597 break;
5598 case RTX_BITFIELD_OPS:
5599 case RTX_TERNARY:
5600 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5601 XEXP (x, 1), XEXP (x, 2));
5602 break;
5603 default:
5604 break;
5607 if (temp)
5609 x = temp;
5610 code = GET_CODE (temp);
5611 op0_mode = VOIDmode;
5612 mode = GET_MODE (temp);
5615 /* First see if we can apply the inverse distributive law. */
5616 if (code == PLUS || code == MINUS
5617 || code == AND || code == IOR || code == XOR)
5619 x = apply_distributive_law (x);
5620 code = GET_CODE (x);
5621 op0_mode = VOIDmode;
5624 /* If CODE is an associative operation not otherwise handled, see if we
5625 can associate some operands. This can win if they are constants or
5626 if they are logically related (i.e. (a & b) & a). */
5627 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5628 || code == AND || code == IOR || code == XOR
5629 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5630 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5631 || (flag_associative_math && FLOAT_MODE_P (mode))))
5633 if (GET_CODE (XEXP (x, 0)) == code)
5635 rtx other = XEXP (XEXP (x, 0), 0);
5636 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5637 rtx inner_op1 = XEXP (x, 1);
5638 rtx inner;
5640 /* Make sure we pass the constant operand if any as the second
5641 one if this is a commutative operation. */
5642 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5644 rtx tem = inner_op0;
5645 inner_op0 = inner_op1;
5646 inner_op1 = tem;
5648 inner = simplify_binary_operation (code == MINUS ? PLUS
5649 : code == DIV ? MULT
5650 : code,
5651 mode, inner_op0, inner_op1);
5653 /* For commutative operations, try the other pair if that one
5654 didn't simplify. */
5655 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5657 other = XEXP (XEXP (x, 0), 1);
5658 inner = simplify_binary_operation (code, mode,
5659 XEXP (XEXP (x, 0), 0),
5660 XEXP (x, 1));
5663 if (inner)
5664 return simplify_gen_binary (code, mode, other, inner);
5668 /* A little bit of algebraic simplification here. */
5669 switch (code)
5671 case MEM:
5672 /* Ensure that our address has any ASHIFTs converted to MULT in case
5673 address-recognizing predicates are called later. */
5674 temp = make_compound_operation (XEXP (x, 0), MEM);
5675 SUBST (XEXP (x, 0), temp);
5676 break;
5678 case SUBREG:
5679 if (op0_mode == VOIDmode)
5680 op0_mode = GET_MODE (SUBREG_REG (x));
5682 /* See if this can be moved to simplify_subreg. */
5683 if (CONSTANT_P (SUBREG_REG (x))
5684 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5685 /* Don't call gen_lowpart if the inner mode
5686 is VOIDmode and we cannot simplify it, as SUBREG without
5687 inner mode is invalid. */
5688 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5689 || gen_lowpart_common (mode, SUBREG_REG (x))))
5690 return gen_lowpart (mode, SUBREG_REG (x));
5692 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5693 break;
5695 rtx temp;
5696 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5697 SUBREG_BYTE (x));
5698 if (temp)
5699 return temp;
5701 /* If op is known to have all lower bits zero, the result is zero. */
5702 if (!in_dest
5703 && SCALAR_INT_MODE_P (mode)
5704 && SCALAR_INT_MODE_P (op0_mode)
5705 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5706 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5707 && HWI_COMPUTABLE_MODE_P (op0_mode)
5708 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5709 & GET_MODE_MASK (mode)) == 0)
5710 return CONST0_RTX (mode);
5713 /* Don't change the mode of the MEM if that would change the meaning
5714 of the address. */
5715 if (MEM_P (SUBREG_REG (x))
5716 && (MEM_VOLATILE_P (SUBREG_REG (x))
5717 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5718 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5719 return gen_rtx_CLOBBER (mode, const0_rtx);
5721 /* Note that we cannot do any narrowing for non-constants since
5722 we might have been counting on using the fact that some bits were
5723 zero. We now do this in the SET. */
5725 break;
5727 case NEG:
5728 temp = expand_compound_operation (XEXP (x, 0));
5730 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5731 replaced by (lshiftrt X C). This will convert
5732 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5734 if (GET_CODE (temp) == ASHIFTRT
5735 && CONST_INT_P (XEXP (temp, 1))
5736 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5737 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5738 INTVAL (XEXP (temp, 1)));
5740 /* If X has only a single bit that might be nonzero, say, bit I, convert
5741 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5742 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5743 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5744 or a SUBREG of one since we'd be making the expression more
5745 complex if it was just a register. */
5747 if (!REG_P (temp)
5748 && ! (GET_CODE (temp) == SUBREG
5749 && REG_P (SUBREG_REG (temp)))
5750 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5752 rtx temp1 = simplify_shift_const
5753 (NULL_RTX, ASHIFTRT, mode,
5754 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5755 GET_MODE_PRECISION (mode) - 1 - i),
5756 GET_MODE_PRECISION (mode) - 1 - i);
5758 /* If all we did was surround TEMP with the two shifts, we
5759 haven't improved anything, so don't use it. Otherwise,
5760 we are better off with TEMP1. */
5761 if (GET_CODE (temp1) != ASHIFTRT
5762 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5763 || XEXP (XEXP (temp1, 0), 0) != temp)
5764 return temp1;
5766 break;
5768 case TRUNCATE:
5769 /* We can't handle truncation to a partial integer mode here
5770 because we don't know the real bitsize of the partial
5771 integer mode. */
5772 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5773 break;
5775 if (HWI_COMPUTABLE_MODE_P (mode))
5776 SUBST (XEXP (x, 0),
5777 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5778 GET_MODE_MASK (mode), 0));
5780 /* We can truncate a constant value and return it. */
5781 if (CONST_INT_P (XEXP (x, 0)))
5782 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5784 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5785 whose value is a comparison can be replaced with a subreg if
5786 STORE_FLAG_VALUE permits. */
5787 if (HWI_COMPUTABLE_MODE_P (mode)
5788 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5789 && (temp = get_last_value (XEXP (x, 0)))
5790 && COMPARISON_P (temp))
5791 return gen_lowpart (mode, XEXP (x, 0));
5792 break;
5794 case CONST:
5795 /* (const (const X)) can become (const X). Do it this way rather than
5796 returning the inner CONST since CONST can be shared with a
5797 REG_EQUAL note. */
5798 if (GET_CODE (XEXP (x, 0)) == CONST)
5799 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5800 break;
5802 #ifdef HAVE_lo_sum
5803 case LO_SUM:
5804 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5805 can add in an offset. find_split_point will split this address up
5806 again if it doesn't match. */
5807 if (GET_CODE (XEXP (x, 0)) == HIGH
5808 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5809 return XEXP (x, 1);
5810 break;
5811 #endif
5813 case PLUS:
5814 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5815 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5816 bit-field and can be replaced by either a sign_extend or a
5817 sign_extract. The `and' may be a zero_extend and the two
5818 <c>, -<c> constants may be reversed. */
5819 if (GET_CODE (XEXP (x, 0)) == XOR
5820 && CONST_INT_P (XEXP (x, 1))
5821 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5822 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5823 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5824 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5825 && HWI_COMPUTABLE_MODE_P (mode)
5826 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5827 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5828 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5829 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5830 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5831 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5832 == (unsigned int) i + 1))))
5833 return simplify_shift_const
5834 (NULL_RTX, ASHIFTRT, mode,
5835 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5836 XEXP (XEXP (XEXP (x, 0), 0), 0),
5837 GET_MODE_PRECISION (mode) - (i + 1)),
5838 GET_MODE_PRECISION (mode) - (i + 1));
5840 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5841 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5842 the bitsize of the mode - 1. This allows simplification of
5843 "a = (b & 8) == 0;" */
5844 if (XEXP (x, 1) == constm1_rtx
5845 && !REG_P (XEXP (x, 0))
5846 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5847 && REG_P (SUBREG_REG (XEXP (x, 0))))
5848 && nonzero_bits (XEXP (x, 0), mode) == 1)
5849 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5850 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5851 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5852 GET_MODE_PRECISION (mode) - 1),
5853 GET_MODE_PRECISION (mode) - 1);
5855 /* If we are adding two things that have no bits in common, convert
5856 the addition into an IOR. This will often be further simplified,
5857 for example in cases like ((a & 1) + (a & 2)), which can
5858 become a & 3. */
5860 if (HWI_COMPUTABLE_MODE_P (mode)
5861 && (nonzero_bits (XEXP (x, 0), mode)
5862 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5864 /* Try to simplify the expression further. */
5865 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5866 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5868 /* If we could, great. If not, do not go ahead with the IOR
5869 replacement, since PLUS appears in many special purpose
5870 address arithmetic instructions. */
5871 if (GET_CODE (temp) != CLOBBER
5872 && (GET_CODE (temp) != IOR
5873 || ((XEXP (temp, 0) != XEXP (x, 0)
5874 || XEXP (temp, 1) != XEXP (x, 1))
5875 && (XEXP (temp, 0) != XEXP (x, 1)
5876 || XEXP (temp, 1) != XEXP (x, 0)))))
5877 return temp;
5879 break;
5881 case MINUS:
5882 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5883 (and <foo> (const_int pow2-1)) */
5884 if (GET_CODE (XEXP (x, 1)) == AND
5885 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5886 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5887 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5888 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5889 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5890 break;
5892 case MULT:
5893 /* If we have (mult (plus A B) C), apply the distributive law and then
5894 the inverse distributive law to see if things simplify. This
5895 occurs mostly in addresses, often when unrolling loops. */
5897 if (GET_CODE (XEXP (x, 0)) == PLUS)
5899 rtx result = distribute_and_simplify_rtx (x, 0);
5900 if (result)
5901 return result;
5904 /* Try simplify a*(b/c) as (a*b)/c. */
5905 if (FLOAT_MODE_P (mode) && flag_associative_math
5906 && GET_CODE (XEXP (x, 0)) == DIV)
5908 rtx tem = simplify_binary_operation (MULT, mode,
5909 XEXP (XEXP (x, 0), 0),
5910 XEXP (x, 1));
5911 if (tem)
5912 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5914 break;
5916 case UDIV:
5917 /* If this is a divide by a power of two, treat it as a shift if
5918 its first operand is a shift. */
5919 if (CONST_INT_P (XEXP (x, 1))
5920 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5921 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5922 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5923 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5924 || GET_CODE (XEXP (x, 0)) == ROTATE
5925 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5926 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5927 break;
5929 case EQ: case NE:
5930 case GT: case GTU: case GE: case GEU:
5931 case LT: case LTU: case LE: case LEU:
5932 case UNEQ: case LTGT:
5933 case UNGT: case UNGE:
5934 case UNLT: case UNLE:
5935 case UNORDERED: case ORDERED:
5936 /* If the first operand is a condition code, we can't do anything
5937 with it. */
5938 if (GET_CODE (XEXP (x, 0)) == COMPARE
5939 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5940 && ! CC0_P (XEXP (x, 0))))
5942 rtx op0 = XEXP (x, 0);
5943 rtx op1 = XEXP (x, 1);
5944 enum rtx_code new_code;
5946 if (GET_CODE (op0) == COMPARE)
5947 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5949 /* Simplify our comparison, if possible. */
5950 new_code = simplify_comparison (code, &op0, &op1);
5952 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5953 if only the low-order bit is possibly nonzero in X (such as when
5954 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5955 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5956 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5957 (plus X 1).
5959 Remove any ZERO_EXTRACT we made when thinking this was a
5960 comparison. It may now be simpler to use, e.g., an AND. If a
5961 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5962 the call to make_compound_operation in the SET case.
5964 Don't apply these optimizations if the caller would
5965 prefer a comparison rather than a value.
5966 E.g., for the condition in an IF_THEN_ELSE most targets need
5967 an explicit comparison. */
5969 if (in_cond)
5972 else if (STORE_FLAG_VALUE == 1
5973 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5974 && op1 == const0_rtx
5975 && mode == GET_MODE (op0)
5976 && nonzero_bits (op0, mode) == 1)
5977 return gen_lowpart (mode,
5978 expand_compound_operation (op0));
5980 else if (STORE_FLAG_VALUE == 1
5981 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5982 && op1 == const0_rtx
5983 && mode == GET_MODE (op0)
5984 && (num_sign_bit_copies (op0, mode)
5985 == GET_MODE_PRECISION (mode)))
5987 op0 = expand_compound_operation (op0);
5988 return simplify_gen_unary (NEG, mode,
5989 gen_lowpart (mode, op0),
5990 mode);
5993 else if (STORE_FLAG_VALUE == 1
5994 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5995 && op1 == const0_rtx
5996 && mode == GET_MODE (op0)
5997 && nonzero_bits (op0, mode) == 1)
5999 op0 = expand_compound_operation (op0);
6000 return simplify_gen_binary (XOR, mode,
6001 gen_lowpart (mode, op0),
6002 const1_rtx);
6005 else if (STORE_FLAG_VALUE == 1
6006 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6007 && op1 == const0_rtx
6008 && mode == GET_MODE (op0)
6009 && (num_sign_bit_copies (op0, mode)
6010 == GET_MODE_PRECISION (mode)))
6012 op0 = expand_compound_operation (op0);
6013 return plus_constant (mode, gen_lowpart (mode, op0), 1);
6016 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6017 those above. */
6018 if (in_cond)
6021 else if (STORE_FLAG_VALUE == -1
6022 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6023 && op1 == const0_rtx
6024 && mode == GET_MODE (op0)
6025 && (num_sign_bit_copies (op0, mode)
6026 == GET_MODE_PRECISION (mode)))
6027 return gen_lowpart (mode,
6028 expand_compound_operation (op0));
6030 else if (STORE_FLAG_VALUE == -1
6031 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6032 && op1 == const0_rtx
6033 && mode == GET_MODE (op0)
6034 && nonzero_bits (op0, mode) == 1)
6036 op0 = expand_compound_operation (op0);
6037 return simplify_gen_unary (NEG, mode,
6038 gen_lowpart (mode, op0),
6039 mode);
6042 else if (STORE_FLAG_VALUE == -1
6043 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6044 && op1 == const0_rtx
6045 && mode == GET_MODE (op0)
6046 && (num_sign_bit_copies (op0, mode)
6047 == GET_MODE_PRECISION (mode)))
6049 op0 = expand_compound_operation (op0);
6050 return simplify_gen_unary (NOT, mode,
6051 gen_lowpart (mode, op0),
6052 mode);
6055 /* If X is 0/1, (eq X 0) is X-1. */
6056 else if (STORE_FLAG_VALUE == -1
6057 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6058 && op1 == const0_rtx
6059 && mode == GET_MODE (op0)
6060 && nonzero_bits (op0, mode) == 1)
6062 op0 = expand_compound_operation (op0);
6063 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6066 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6067 one bit that might be nonzero, we can convert (ne x 0) to
6068 (ashift x c) where C puts the bit in the sign bit. Remove any
6069 AND with STORE_FLAG_VALUE when we are done, since we are only
6070 going to test the sign bit. */
6071 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6072 && HWI_COMPUTABLE_MODE_P (mode)
6073 && val_signbit_p (mode, STORE_FLAG_VALUE)
6074 && op1 == const0_rtx
6075 && mode == GET_MODE (op0)
6076 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6078 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6079 expand_compound_operation (op0),
6080 GET_MODE_PRECISION (mode) - 1 - i);
6081 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6082 return XEXP (x, 0);
6083 else
6084 return x;
6087 /* If the code changed, return a whole new comparison.
6088 We also need to avoid using SUBST in cases where
6089 simplify_comparison has widened a comparison with a CONST_INT,
6090 since in that case the wider CONST_INT may fail the sanity
6091 checks in do_SUBST. */
6092 if (new_code != code
6093 || (CONST_INT_P (op1)
6094 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6095 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6096 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6098 /* Otherwise, keep this operation, but maybe change its operands.
6099 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6100 SUBST (XEXP (x, 0), op0);
6101 SUBST (XEXP (x, 1), op1);
6103 break;
6105 case IF_THEN_ELSE:
6106 return simplify_if_then_else (x);
6108 case ZERO_EXTRACT:
6109 case SIGN_EXTRACT:
6110 case ZERO_EXTEND:
6111 case SIGN_EXTEND:
6112 /* If we are processing SET_DEST, we are done. */
6113 if (in_dest)
6114 return x;
6116 return expand_compound_operation (x);
6118 case SET:
6119 return simplify_set (x);
6121 case AND:
6122 case IOR:
6123 return simplify_logical (x);
6125 case ASHIFT:
6126 case LSHIFTRT:
6127 case ASHIFTRT:
6128 case ROTATE:
6129 case ROTATERT:
6130 /* If this is a shift by a constant amount, simplify it. */
6131 if (CONST_INT_P (XEXP (x, 1)))
6132 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6133 INTVAL (XEXP (x, 1)));
6135 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6136 SUBST (XEXP (x, 1),
6137 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6138 ((unsigned HOST_WIDE_INT) 1
6139 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6140 - 1,
6141 0));
6142 break;
6144 default:
6145 break;
6148 return x;
6151 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6153 static rtx
6154 simplify_if_then_else (rtx x)
6156 machine_mode mode = GET_MODE (x);
6157 rtx cond = XEXP (x, 0);
6158 rtx true_rtx = XEXP (x, 1);
6159 rtx false_rtx = XEXP (x, 2);
6160 enum rtx_code true_code = GET_CODE (cond);
6161 int comparison_p = COMPARISON_P (cond);
6162 rtx temp;
6163 int i;
6164 enum rtx_code false_code;
6165 rtx reversed;
6167 /* Simplify storing of the truth value. */
6168 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6169 return simplify_gen_relational (true_code, mode, VOIDmode,
6170 XEXP (cond, 0), XEXP (cond, 1));
6172 /* Also when the truth value has to be reversed. */
6173 if (comparison_p
6174 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6175 && (reversed = reversed_comparison (cond, mode)))
6176 return reversed;
6178 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6179 in it is being compared against certain values. Get the true and false
6180 comparisons and see if that says anything about the value of each arm. */
6182 if (comparison_p
6183 && ((false_code = reversed_comparison_code (cond, NULL))
6184 != UNKNOWN)
6185 && REG_P (XEXP (cond, 0)))
6187 HOST_WIDE_INT nzb;
6188 rtx from = XEXP (cond, 0);
6189 rtx true_val = XEXP (cond, 1);
6190 rtx false_val = true_val;
6191 int swapped = 0;
6193 /* If FALSE_CODE is EQ, swap the codes and arms. */
6195 if (false_code == EQ)
6197 swapped = 1, true_code = EQ, false_code = NE;
6198 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6201 /* If we are comparing against zero and the expression being tested has
6202 only a single bit that might be nonzero, that is its value when it is
6203 not equal to zero. Similarly if it is known to be -1 or 0. */
6205 if (true_code == EQ && true_val == const0_rtx
6206 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6208 false_code = EQ;
6209 false_val = gen_int_mode (nzb, GET_MODE (from));
6211 else if (true_code == EQ && true_val == const0_rtx
6212 && (num_sign_bit_copies (from, GET_MODE (from))
6213 == GET_MODE_PRECISION (GET_MODE (from))))
6215 false_code = EQ;
6216 false_val = constm1_rtx;
6219 /* Now simplify an arm if we know the value of the register in the
6220 branch and it is used in the arm. Be careful due to the potential
6221 of locally-shared RTL. */
6223 if (reg_mentioned_p (from, true_rtx))
6224 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6225 from, true_val),
6226 pc_rtx, pc_rtx, 0, 0, 0);
6227 if (reg_mentioned_p (from, false_rtx))
6228 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6229 from, false_val),
6230 pc_rtx, pc_rtx, 0, 0, 0);
6232 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6233 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6235 true_rtx = XEXP (x, 1);
6236 false_rtx = XEXP (x, 2);
6237 true_code = GET_CODE (cond);
6240 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6241 reversed, do so to avoid needing two sets of patterns for
6242 subtract-and-branch insns. Similarly if we have a constant in the true
6243 arm, the false arm is the same as the first operand of the comparison, or
6244 the false arm is more complicated than the true arm. */
6246 if (comparison_p
6247 && reversed_comparison_code (cond, NULL) != UNKNOWN
6248 && (true_rtx == pc_rtx
6249 || (CONSTANT_P (true_rtx)
6250 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6251 || true_rtx == const0_rtx
6252 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6253 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6254 && !OBJECT_P (false_rtx))
6255 || reg_mentioned_p (true_rtx, false_rtx)
6256 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6258 true_code = reversed_comparison_code (cond, NULL);
6259 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6260 SUBST (XEXP (x, 1), false_rtx);
6261 SUBST (XEXP (x, 2), true_rtx);
6263 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6264 cond = XEXP (x, 0);
6266 /* It is possible that the conditional has been simplified out. */
6267 true_code = GET_CODE (cond);
6268 comparison_p = COMPARISON_P (cond);
6271 /* If the two arms are identical, we don't need the comparison. */
6273 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6274 return true_rtx;
6276 /* Convert a == b ? b : a to "a". */
6277 if (true_code == EQ && ! side_effects_p (cond)
6278 && !HONOR_NANS (mode)
6279 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6280 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6281 return false_rtx;
6282 else if (true_code == NE && ! side_effects_p (cond)
6283 && !HONOR_NANS (mode)
6284 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6285 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6286 return true_rtx;
6288 /* Look for cases where we have (abs x) or (neg (abs X)). */
6290 if (GET_MODE_CLASS (mode) == MODE_INT
6291 && comparison_p
6292 && XEXP (cond, 1) == const0_rtx
6293 && GET_CODE (false_rtx) == NEG
6294 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6295 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6296 && ! side_effects_p (true_rtx))
6297 switch (true_code)
6299 case GT:
6300 case GE:
6301 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6302 case LT:
6303 case LE:
6304 return
6305 simplify_gen_unary (NEG, mode,
6306 simplify_gen_unary (ABS, mode, true_rtx, mode),
6307 mode);
6308 default:
6309 break;
6312 /* Look for MIN or MAX. */
6314 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6315 && comparison_p
6316 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6317 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6318 && ! side_effects_p (cond))
6319 switch (true_code)
6321 case GE:
6322 case GT:
6323 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6324 case LE:
6325 case LT:
6326 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6327 case GEU:
6328 case GTU:
6329 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6330 case LEU:
6331 case LTU:
6332 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6333 default:
6334 break;
6337 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6338 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6339 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6340 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6341 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6342 neither 1 or -1, but it isn't worth checking for. */
6344 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6345 && comparison_p
6346 && GET_MODE_CLASS (mode) == MODE_INT
6347 && ! side_effects_p (x))
6349 rtx t = make_compound_operation (true_rtx, SET);
6350 rtx f = make_compound_operation (false_rtx, SET);
6351 rtx cond_op0 = XEXP (cond, 0);
6352 rtx cond_op1 = XEXP (cond, 1);
6353 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6354 machine_mode m = mode;
6355 rtx z = 0, c1 = NULL_RTX;
6357 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6358 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6359 || GET_CODE (t) == ASHIFT
6360 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6361 && rtx_equal_p (XEXP (t, 0), f))
6362 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6364 /* If an identity-zero op is commutative, check whether there
6365 would be a match if we swapped the operands. */
6366 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6367 || GET_CODE (t) == XOR)
6368 && rtx_equal_p (XEXP (t, 1), f))
6369 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6370 else if (GET_CODE (t) == SIGN_EXTEND
6371 && (GET_CODE (XEXP (t, 0)) == PLUS
6372 || GET_CODE (XEXP (t, 0)) == MINUS
6373 || GET_CODE (XEXP (t, 0)) == IOR
6374 || GET_CODE (XEXP (t, 0)) == XOR
6375 || GET_CODE (XEXP (t, 0)) == ASHIFT
6376 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6377 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6378 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6379 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6380 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6381 && (num_sign_bit_copies (f, GET_MODE (f))
6382 > (unsigned int)
6383 (GET_MODE_PRECISION (mode)
6384 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6386 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6387 extend_op = SIGN_EXTEND;
6388 m = GET_MODE (XEXP (t, 0));
6390 else if (GET_CODE (t) == SIGN_EXTEND
6391 && (GET_CODE (XEXP (t, 0)) == PLUS
6392 || GET_CODE (XEXP (t, 0)) == IOR
6393 || GET_CODE (XEXP (t, 0)) == XOR)
6394 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6395 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6396 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6397 && (num_sign_bit_copies (f, GET_MODE (f))
6398 > (unsigned int)
6399 (GET_MODE_PRECISION (mode)
6400 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6402 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6403 extend_op = SIGN_EXTEND;
6404 m = GET_MODE (XEXP (t, 0));
6406 else if (GET_CODE (t) == ZERO_EXTEND
6407 && (GET_CODE (XEXP (t, 0)) == PLUS
6408 || GET_CODE (XEXP (t, 0)) == MINUS
6409 || GET_CODE (XEXP (t, 0)) == IOR
6410 || GET_CODE (XEXP (t, 0)) == XOR
6411 || GET_CODE (XEXP (t, 0)) == ASHIFT
6412 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6413 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6414 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6415 && HWI_COMPUTABLE_MODE_P (mode)
6416 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6417 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6418 && ((nonzero_bits (f, GET_MODE (f))
6419 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6420 == 0))
6422 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6423 extend_op = ZERO_EXTEND;
6424 m = GET_MODE (XEXP (t, 0));
6426 else if (GET_CODE (t) == ZERO_EXTEND
6427 && (GET_CODE (XEXP (t, 0)) == PLUS
6428 || GET_CODE (XEXP (t, 0)) == IOR
6429 || GET_CODE (XEXP (t, 0)) == XOR)
6430 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6431 && HWI_COMPUTABLE_MODE_P (mode)
6432 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6433 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6434 && ((nonzero_bits (f, GET_MODE (f))
6435 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6436 == 0))
6438 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6439 extend_op = ZERO_EXTEND;
6440 m = GET_MODE (XEXP (t, 0));
6443 if (z)
6445 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6446 cond_op0, cond_op1),
6447 pc_rtx, pc_rtx, 0, 0, 0);
6448 temp = simplify_gen_binary (MULT, m, temp,
6449 simplify_gen_binary (MULT, m, c1,
6450 const_true_rtx));
6451 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6452 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6454 if (extend_op != UNKNOWN)
6455 temp = simplify_gen_unary (extend_op, mode, temp, m);
6457 return temp;
6461 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6462 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6463 negation of a single bit, we can convert this operation to a shift. We
6464 can actually do this more generally, but it doesn't seem worth it. */
6466 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6467 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6468 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6469 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6470 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6471 == GET_MODE_PRECISION (mode))
6472 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6473 return
6474 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6475 gen_lowpart (mode, XEXP (cond, 0)), i);
6477 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6478 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6479 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6480 && GET_MODE (XEXP (cond, 0)) == mode
6481 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6482 == nonzero_bits (XEXP (cond, 0), mode)
6483 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6484 return XEXP (cond, 0);
6486 return x;
6489 /* Simplify X, a SET expression. Return the new expression. */
6491 static rtx
6492 simplify_set (rtx x)
6494 rtx src = SET_SRC (x);
6495 rtx dest = SET_DEST (x);
6496 machine_mode mode
6497 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6498 rtx_insn *other_insn;
6499 rtx *cc_use;
6501 /* (set (pc) (return)) gets written as (return). */
6502 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6503 return src;
6505 /* Now that we know for sure which bits of SRC we are using, see if we can
6506 simplify the expression for the object knowing that we only need the
6507 low-order bits. */
6509 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6511 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6512 SUBST (SET_SRC (x), src);
6515 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6516 the comparison result and try to simplify it unless we already have used
6517 undobuf.other_insn. */
6518 if ((GET_MODE_CLASS (mode) == MODE_CC
6519 || GET_CODE (src) == COMPARE
6520 || CC0_P (dest))
6521 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6522 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6523 && COMPARISON_P (*cc_use)
6524 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6526 enum rtx_code old_code = GET_CODE (*cc_use);
6527 enum rtx_code new_code;
6528 rtx op0, op1, tmp;
6529 int other_changed = 0;
6530 rtx inner_compare = NULL_RTX;
6531 machine_mode compare_mode = GET_MODE (dest);
6533 if (GET_CODE (src) == COMPARE)
6535 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6536 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6538 inner_compare = op0;
6539 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6542 else
6543 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6545 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6546 op0, op1);
6547 if (!tmp)
6548 new_code = old_code;
6549 else if (!CONSTANT_P (tmp))
6551 new_code = GET_CODE (tmp);
6552 op0 = XEXP (tmp, 0);
6553 op1 = XEXP (tmp, 1);
6555 else
6557 rtx pat = PATTERN (other_insn);
6558 undobuf.other_insn = other_insn;
6559 SUBST (*cc_use, tmp);
6561 /* Attempt to simplify CC user. */
6562 if (GET_CODE (pat) == SET)
6564 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6565 if (new_rtx != NULL_RTX)
6566 SUBST (SET_SRC (pat), new_rtx);
6569 /* Convert X into a no-op move. */
6570 SUBST (SET_DEST (x), pc_rtx);
6571 SUBST (SET_SRC (x), pc_rtx);
6572 return x;
6575 /* Simplify our comparison, if possible. */
6576 new_code = simplify_comparison (new_code, &op0, &op1);
6578 #ifdef SELECT_CC_MODE
6579 /* If this machine has CC modes other than CCmode, check to see if we
6580 need to use a different CC mode here. */
6581 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6582 compare_mode = GET_MODE (op0);
6583 else if (inner_compare
6584 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6585 && new_code == old_code
6586 && op0 == XEXP (inner_compare, 0)
6587 && op1 == XEXP (inner_compare, 1))
6588 compare_mode = GET_MODE (inner_compare);
6589 else
6590 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6592 #ifndef HAVE_cc0
6593 /* If the mode changed, we have to change SET_DEST, the mode in the
6594 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6595 a hard register, just build new versions with the proper mode. If it
6596 is a pseudo, we lose unless it is only time we set the pseudo, in
6597 which case we can safely change its mode. */
6598 if (compare_mode != GET_MODE (dest))
6600 if (can_change_dest_mode (dest, 0, compare_mode))
6602 unsigned int regno = REGNO (dest);
6603 rtx new_dest;
6605 if (regno < FIRST_PSEUDO_REGISTER)
6606 new_dest = gen_rtx_REG (compare_mode, regno);
6607 else
6609 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6610 new_dest = regno_reg_rtx[regno];
6613 SUBST (SET_DEST (x), new_dest);
6614 SUBST (XEXP (*cc_use, 0), new_dest);
6615 other_changed = 1;
6617 dest = new_dest;
6620 #endif /* cc0 */
6621 #endif /* SELECT_CC_MODE */
6623 /* If the code changed, we have to build a new comparison in
6624 undobuf.other_insn. */
6625 if (new_code != old_code)
6627 int other_changed_previously = other_changed;
6628 unsigned HOST_WIDE_INT mask;
6629 rtx old_cc_use = *cc_use;
6631 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6632 dest, const0_rtx));
6633 other_changed = 1;
6635 /* If the only change we made was to change an EQ into an NE or
6636 vice versa, OP0 has only one bit that might be nonzero, and OP1
6637 is zero, check if changing the user of the condition code will
6638 produce a valid insn. If it won't, we can keep the original code
6639 in that insn by surrounding our operation with an XOR. */
6641 if (((old_code == NE && new_code == EQ)
6642 || (old_code == EQ && new_code == NE))
6643 && ! other_changed_previously && op1 == const0_rtx
6644 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6645 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6647 rtx pat = PATTERN (other_insn), note = 0;
6649 if ((recog_for_combine (&pat, other_insn, &note) < 0
6650 && ! check_asm_operands (pat)))
6652 *cc_use = old_cc_use;
6653 other_changed = 0;
6655 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6656 gen_int_mode (mask,
6657 GET_MODE (op0)));
6662 if (other_changed)
6663 undobuf.other_insn = other_insn;
6665 /* Otherwise, if we didn't previously have a COMPARE in the
6666 correct mode, we need one. */
6667 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6669 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6670 src = SET_SRC (x);
6672 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6674 SUBST (SET_SRC (x), op0);
6675 src = SET_SRC (x);
6677 /* Otherwise, update the COMPARE if needed. */
6678 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6680 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6681 src = SET_SRC (x);
6684 else
6686 /* Get SET_SRC in a form where we have placed back any
6687 compound expressions. Then do the checks below. */
6688 src = make_compound_operation (src, SET);
6689 SUBST (SET_SRC (x), src);
6692 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6693 and X being a REG or (subreg (reg)), we may be able to convert this to
6694 (set (subreg:m2 x) (op)).
6696 We can always do this if M1 is narrower than M2 because that means that
6697 we only care about the low bits of the result.
6699 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6700 perform a narrower operation than requested since the high-order bits will
6701 be undefined. On machine where it is defined, this transformation is safe
6702 as long as M1 and M2 have the same number of words. */
6704 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6705 && !OBJECT_P (SUBREG_REG (src))
6706 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6707 / UNITS_PER_WORD)
6708 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6709 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6710 #ifndef WORD_REGISTER_OPERATIONS
6711 && (GET_MODE_SIZE (GET_MODE (src))
6712 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6713 #endif
6714 #ifdef CANNOT_CHANGE_MODE_CLASS
6715 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6716 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6717 GET_MODE (SUBREG_REG (src)),
6718 GET_MODE (src)))
6719 #endif
6720 && (REG_P (dest)
6721 || (GET_CODE (dest) == SUBREG
6722 && REG_P (SUBREG_REG (dest)))))
6724 SUBST (SET_DEST (x),
6725 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6726 dest));
6727 SUBST (SET_SRC (x), SUBREG_REG (src));
6729 src = SET_SRC (x), dest = SET_DEST (x);
6732 #ifdef HAVE_cc0
6733 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6734 in SRC. */
6735 if (dest == cc0_rtx
6736 && GET_CODE (src) == SUBREG
6737 && subreg_lowpart_p (src)
6738 && (GET_MODE_PRECISION (GET_MODE (src))
6739 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6741 rtx inner = SUBREG_REG (src);
6742 machine_mode inner_mode = GET_MODE (inner);
6744 /* Here we make sure that we don't have a sign bit on. */
6745 if (val_signbit_known_clear_p (GET_MODE (src),
6746 nonzero_bits (inner, inner_mode)))
6748 SUBST (SET_SRC (x), inner);
6749 src = SET_SRC (x);
6752 #endif
6754 #ifdef LOAD_EXTEND_OP
6755 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6756 would require a paradoxical subreg. Replace the subreg with a
6757 zero_extend to avoid the reload that would otherwise be required. */
6759 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6760 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6761 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6762 && SUBREG_BYTE (src) == 0
6763 && paradoxical_subreg_p (src)
6764 && MEM_P (SUBREG_REG (src)))
6766 SUBST (SET_SRC (x),
6767 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6768 GET_MODE (src), SUBREG_REG (src)));
6770 src = SET_SRC (x);
6772 #endif
6774 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6775 are comparing an item known to be 0 or -1 against 0, use a logical
6776 operation instead. Check for one of the arms being an IOR of the other
6777 arm with some value. We compute three terms to be IOR'ed together. In
6778 practice, at most two will be nonzero. Then we do the IOR's. */
6780 if (GET_CODE (dest) != PC
6781 && GET_CODE (src) == IF_THEN_ELSE
6782 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6783 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6784 && XEXP (XEXP (src, 0), 1) == const0_rtx
6785 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6786 #ifdef HAVE_conditional_move
6787 && ! can_conditionally_move_p (GET_MODE (src))
6788 #endif
6789 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6790 GET_MODE (XEXP (XEXP (src, 0), 0)))
6791 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6792 && ! side_effects_p (src))
6794 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6795 ? XEXP (src, 1) : XEXP (src, 2));
6796 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6797 ? XEXP (src, 2) : XEXP (src, 1));
6798 rtx term1 = const0_rtx, term2, term3;
6800 if (GET_CODE (true_rtx) == IOR
6801 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6802 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6803 else if (GET_CODE (true_rtx) == IOR
6804 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6805 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6806 else if (GET_CODE (false_rtx) == IOR
6807 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6808 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6809 else if (GET_CODE (false_rtx) == IOR
6810 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6811 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6813 term2 = simplify_gen_binary (AND, GET_MODE (src),
6814 XEXP (XEXP (src, 0), 0), true_rtx);
6815 term3 = simplify_gen_binary (AND, GET_MODE (src),
6816 simplify_gen_unary (NOT, GET_MODE (src),
6817 XEXP (XEXP (src, 0), 0),
6818 GET_MODE (src)),
6819 false_rtx);
6821 SUBST (SET_SRC (x),
6822 simplify_gen_binary (IOR, GET_MODE (src),
6823 simplify_gen_binary (IOR, GET_MODE (src),
6824 term1, term2),
6825 term3));
6827 src = SET_SRC (x);
6830 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6831 whole thing fail. */
6832 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6833 return src;
6834 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6835 return dest;
6836 else
6837 /* Convert this into a field assignment operation, if possible. */
6838 return make_field_assignment (x);
6841 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6842 result. */
6844 static rtx
6845 simplify_logical (rtx x)
6847 machine_mode mode = GET_MODE (x);
6848 rtx op0 = XEXP (x, 0);
6849 rtx op1 = XEXP (x, 1);
6851 switch (GET_CODE (x))
6853 case AND:
6854 /* We can call simplify_and_const_int only if we don't lose
6855 any (sign) bits when converting INTVAL (op1) to
6856 "unsigned HOST_WIDE_INT". */
6857 if (CONST_INT_P (op1)
6858 && (HWI_COMPUTABLE_MODE_P (mode)
6859 || INTVAL (op1) > 0))
6861 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6862 if (GET_CODE (x) != AND)
6863 return x;
6865 op0 = XEXP (x, 0);
6866 op1 = XEXP (x, 1);
6869 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6870 apply the distributive law and then the inverse distributive
6871 law to see if things simplify. */
6872 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6874 rtx result = distribute_and_simplify_rtx (x, 0);
6875 if (result)
6876 return result;
6878 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6880 rtx result = distribute_and_simplify_rtx (x, 1);
6881 if (result)
6882 return result;
6884 break;
6886 case IOR:
6887 /* If we have (ior (and A B) C), apply the distributive law and then
6888 the inverse distributive law to see if things simplify. */
6890 if (GET_CODE (op0) == AND)
6892 rtx result = distribute_and_simplify_rtx (x, 0);
6893 if (result)
6894 return result;
6897 if (GET_CODE (op1) == AND)
6899 rtx result = distribute_and_simplify_rtx (x, 1);
6900 if (result)
6901 return result;
6903 break;
6905 default:
6906 gcc_unreachable ();
6909 return x;
6912 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6913 operations" because they can be replaced with two more basic operations.
6914 ZERO_EXTEND is also considered "compound" because it can be replaced with
6915 an AND operation, which is simpler, though only one operation.
6917 The function expand_compound_operation is called with an rtx expression
6918 and will convert it to the appropriate shifts and AND operations,
6919 simplifying at each stage.
6921 The function make_compound_operation is called to convert an expression
6922 consisting of shifts and ANDs into the equivalent compound expression.
6923 It is the inverse of this function, loosely speaking. */
6925 static rtx
6926 expand_compound_operation (rtx x)
6928 unsigned HOST_WIDE_INT pos = 0, len;
6929 int unsignedp = 0;
6930 unsigned int modewidth;
6931 rtx tem;
6933 switch (GET_CODE (x))
6935 case ZERO_EXTEND:
6936 unsignedp = 1;
6937 case SIGN_EXTEND:
6938 /* We can't necessarily use a const_int for a multiword mode;
6939 it depends on implicitly extending the value.
6940 Since we don't know the right way to extend it,
6941 we can't tell whether the implicit way is right.
6943 Even for a mode that is no wider than a const_int,
6944 we can't win, because we need to sign extend one of its bits through
6945 the rest of it, and we don't know which bit. */
6946 if (CONST_INT_P (XEXP (x, 0)))
6947 return x;
6949 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6950 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6951 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6952 reloaded. If not for that, MEM's would very rarely be safe.
6954 Reject MODEs bigger than a word, because we might not be able
6955 to reference a two-register group starting with an arbitrary register
6956 (and currently gen_lowpart might crash for a SUBREG). */
6958 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6959 return x;
6961 /* Reject MODEs that aren't scalar integers because turning vector
6962 or complex modes into shifts causes problems. */
6964 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6965 return x;
6967 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6968 /* If the inner object has VOIDmode (the only way this can happen
6969 is if it is an ASM_OPERANDS), we can't do anything since we don't
6970 know how much masking to do. */
6971 if (len == 0)
6972 return x;
6974 break;
6976 case ZERO_EXTRACT:
6977 unsignedp = 1;
6979 /* ... fall through ... */
6981 case SIGN_EXTRACT:
6982 /* If the operand is a CLOBBER, just return it. */
6983 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6984 return XEXP (x, 0);
6986 if (!CONST_INT_P (XEXP (x, 1))
6987 || !CONST_INT_P (XEXP (x, 2))
6988 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6989 return x;
6991 /* Reject MODEs that aren't scalar integers because turning vector
6992 or complex modes into shifts causes problems. */
6994 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6995 return x;
6997 len = INTVAL (XEXP (x, 1));
6998 pos = INTVAL (XEXP (x, 2));
7000 /* This should stay within the object being extracted, fail otherwise. */
7001 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
7002 return x;
7004 if (BITS_BIG_ENDIAN)
7005 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
7007 break;
7009 default:
7010 return x;
7012 /* Convert sign extension to zero extension, if we know that the high
7013 bit is not set, as this is easier to optimize. It will be converted
7014 back to cheaper alternative in make_extraction. */
7015 if (GET_CODE (x) == SIGN_EXTEND
7016 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7017 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7018 & ~(((unsigned HOST_WIDE_INT)
7019 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
7020 >> 1))
7021 == 0)))
7023 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
7024 rtx temp2 = expand_compound_operation (temp);
7026 /* Make sure this is a profitable operation. */
7027 if (set_src_cost (x, optimize_this_for_speed_p)
7028 > set_src_cost (temp2, optimize_this_for_speed_p))
7029 return temp2;
7030 else if (set_src_cost (x, optimize_this_for_speed_p)
7031 > set_src_cost (temp, optimize_this_for_speed_p))
7032 return temp;
7033 else
7034 return x;
7037 /* We can optimize some special cases of ZERO_EXTEND. */
7038 if (GET_CODE (x) == ZERO_EXTEND)
7040 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7041 know that the last value didn't have any inappropriate bits
7042 set. */
7043 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7044 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7045 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7046 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
7047 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7048 return XEXP (XEXP (x, 0), 0);
7050 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7051 if (GET_CODE (XEXP (x, 0)) == SUBREG
7052 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7053 && subreg_lowpart_p (XEXP (x, 0))
7054 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7055 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
7056 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7057 return SUBREG_REG (XEXP (x, 0));
7059 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7060 is a comparison and STORE_FLAG_VALUE permits. This is like
7061 the first case, but it works even when GET_MODE (x) is larger
7062 than HOST_WIDE_INT. */
7063 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7064 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7065 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7066 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7067 <= HOST_BITS_PER_WIDE_INT)
7068 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7069 return XEXP (XEXP (x, 0), 0);
7071 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7072 if (GET_CODE (XEXP (x, 0)) == SUBREG
7073 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7074 && subreg_lowpart_p (XEXP (x, 0))
7075 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7076 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7077 <= HOST_BITS_PER_WIDE_INT)
7078 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7079 return SUBREG_REG (XEXP (x, 0));
7083 /* If we reach here, we want to return a pair of shifts. The inner
7084 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7085 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7086 logical depending on the value of UNSIGNEDP.
7088 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7089 converted into an AND of a shift.
7091 We must check for the case where the left shift would have a negative
7092 count. This can happen in a case like (x >> 31) & 255 on machines
7093 that can't shift by a constant. On those machines, we would first
7094 combine the shift with the AND to produce a variable-position
7095 extraction. Then the constant of 31 would be substituted in
7096 to produce such a position. */
7098 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7099 if (modewidth >= pos + len)
7101 machine_mode mode = GET_MODE (x);
7102 tem = gen_lowpart (mode, XEXP (x, 0));
7103 if (!tem || GET_CODE (tem) == CLOBBER)
7104 return x;
7105 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7106 tem, modewidth - pos - len);
7107 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7108 mode, tem, modewidth - len);
7110 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7111 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7112 simplify_shift_const (NULL_RTX, LSHIFTRT,
7113 GET_MODE (x),
7114 XEXP (x, 0), pos),
7115 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7116 else
7117 /* Any other cases we can't handle. */
7118 return x;
7120 /* If we couldn't do this for some reason, return the original
7121 expression. */
7122 if (GET_CODE (tem) == CLOBBER)
7123 return x;
7125 return tem;
7128 /* X is a SET which contains an assignment of one object into
7129 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7130 or certain SUBREGS). If possible, convert it into a series of
7131 logical operations.
7133 We half-heartedly support variable positions, but do not at all
7134 support variable lengths. */
7136 static const_rtx
7137 expand_field_assignment (const_rtx x)
7139 rtx inner;
7140 rtx pos; /* Always counts from low bit. */
7141 int len;
7142 rtx mask, cleared, masked;
7143 machine_mode compute_mode;
7145 /* Loop until we find something we can't simplify. */
7146 while (1)
7148 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7149 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7151 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7152 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7153 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7155 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7156 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7158 inner = XEXP (SET_DEST (x), 0);
7159 len = INTVAL (XEXP (SET_DEST (x), 1));
7160 pos = XEXP (SET_DEST (x), 2);
7162 /* A constant position should stay within the width of INNER. */
7163 if (CONST_INT_P (pos)
7164 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7165 break;
7167 if (BITS_BIG_ENDIAN)
7169 if (CONST_INT_P (pos))
7170 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7171 - INTVAL (pos));
7172 else if (GET_CODE (pos) == MINUS
7173 && CONST_INT_P (XEXP (pos, 1))
7174 && (INTVAL (XEXP (pos, 1))
7175 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7176 /* If position is ADJUST - X, new position is X. */
7177 pos = XEXP (pos, 0);
7178 else
7180 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7181 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7182 gen_int_mode (prec - len,
7183 GET_MODE (pos)),
7184 pos);
7189 /* A SUBREG between two modes that occupy the same numbers of words
7190 can be done by moving the SUBREG to the source. */
7191 else if (GET_CODE (SET_DEST (x)) == SUBREG
7192 /* We need SUBREGs to compute nonzero_bits properly. */
7193 && nonzero_sign_valid
7194 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7195 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7196 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7197 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7199 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7200 gen_lowpart
7201 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7202 SET_SRC (x)));
7203 continue;
7205 else
7206 break;
7208 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7209 inner = SUBREG_REG (inner);
7211 compute_mode = GET_MODE (inner);
7213 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7214 if (! SCALAR_INT_MODE_P (compute_mode))
7216 machine_mode imode;
7218 /* Don't do anything for vector or complex integral types. */
7219 if (! FLOAT_MODE_P (compute_mode))
7220 break;
7222 /* Try to find an integral mode to pun with. */
7223 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7224 if (imode == BLKmode)
7225 break;
7227 compute_mode = imode;
7228 inner = gen_lowpart (imode, inner);
7231 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7232 if (len >= HOST_BITS_PER_WIDE_INT)
7233 break;
7235 /* Now compute the equivalent expression. Make a copy of INNER
7236 for the SET_DEST in case it is a MEM into which we will substitute;
7237 we don't want shared RTL in that case. */
7238 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7239 compute_mode);
7240 cleared = simplify_gen_binary (AND, compute_mode,
7241 simplify_gen_unary (NOT, compute_mode,
7242 simplify_gen_binary (ASHIFT,
7243 compute_mode,
7244 mask, pos),
7245 compute_mode),
7246 inner);
7247 masked = simplify_gen_binary (ASHIFT, compute_mode,
7248 simplify_gen_binary (
7249 AND, compute_mode,
7250 gen_lowpart (compute_mode, SET_SRC (x)),
7251 mask),
7252 pos);
7254 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7255 simplify_gen_binary (IOR, compute_mode,
7256 cleared, masked));
7259 return x;
7262 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7263 it is an RTX that represents the (variable) starting position; otherwise,
7264 POS is the (constant) starting bit position. Both are counted from the LSB.
7266 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7268 IN_DEST is nonzero if this is a reference in the destination of a SET.
7269 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7270 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7271 be used.
7273 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7274 ZERO_EXTRACT should be built even for bits starting at bit 0.
7276 MODE is the desired mode of the result (if IN_DEST == 0).
7278 The result is an RTX for the extraction or NULL_RTX if the target
7279 can't handle it. */
7281 static rtx
7282 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7283 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7284 int in_dest, int in_compare)
7286 /* This mode describes the size of the storage area
7287 to fetch the overall value from. Within that, we
7288 ignore the POS lowest bits, etc. */
7289 machine_mode is_mode = GET_MODE (inner);
7290 machine_mode inner_mode;
7291 machine_mode wanted_inner_mode;
7292 machine_mode wanted_inner_reg_mode = word_mode;
7293 machine_mode pos_mode = word_mode;
7294 machine_mode extraction_mode = word_mode;
7295 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7296 rtx new_rtx = 0;
7297 rtx orig_pos_rtx = pos_rtx;
7298 HOST_WIDE_INT orig_pos;
7300 if (pos_rtx && CONST_INT_P (pos_rtx))
7301 pos = INTVAL (pos_rtx), pos_rtx = 0;
7303 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7305 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7306 consider just the QI as the memory to extract from.
7307 The subreg adds or removes high bits; its mode is
7308 irrelevant to the meaning of this extraction,
7309 since POS and LEN count from the lsb. */
7310 if (MEM_P (SUBREG_REG (inner)))
7311 is_mode = GET_MODE (SUBREG_REG (inner));
7312 inner = SUBREG_REG (inner);
7314 else if (GET_CODE (inner) == ASHIFT
7315 && CONST_INT_P (XEXP (inner, 1))
7316 && pos_rtx == 0 && pos == 0
7317 && len > UINTVAL (XEXP (inner, 1)))
7319 /* We're extracting the least significant bits of an rtx
7320 (ashift X (const_int C)), where LEN > C. Extract the
7321 least significant (LEN - C) bits of X, giving an rtx
7322 whose mode is MODE, then shift it left C times. */
7323 new_rtx = make_extraction (mode, XEXP (inner, 0),
7324 0, 0, len - INTVAL (XEXP (inner, 1)),
7325 unsignedp, in_dest, in_compare);
7326 if (new_rtx != 0)
7327 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7329 else if (GET_CODE (inner) == TRUNCATE)
7330 inner = XEXP (inner, 0);
7332 inner_mode = GET_MODE (inner);
7334 /* See if this can be done without an extraction. We never can if the
7335 width of the field is not the same as that of some integer mode. For
7336 registers, we can only avoid the extraction if the position is at the
7337 low-order bit and this is either not in the destination or we have the
7338 appropriate STRICT_LOW_PART operation available.
7340 For MEM, we can avoid an extract if the field starts on an appropriate
7341 boundary and we can change the mode of the memory reference. */
7343 if (tmode != BLKmode
7344 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7345 && !MEM_P (inner)
7346 && (inner_mode == tmode
7347 || !REG_P (inner)
7348 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7349 || reg_truncated_to_mode (tmode, inner))
7350 && (! in_dest
7351 || (REG_P (inner)
7352 && have_insn_for (STRICT_LOW_PART, tmode))))
7353 || (MEM_P (inner) && pos_rtx == 0
7354 && (pos
7355 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7356 : BITS_PER_UNIT)) == 0
7357 /* We can't do this if we are widening INNER_MODE (it
7358 may not be aligned, for one thing). */
7359 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7360 && (inner_mode == tmode
7361 || (! mode_dependent_address_p (XEXP (inner, 0),
7362 MEM_ADDR_SPACE (inner))
7363 && ! MEM_VOLATILE_P (inner))))))
7365 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7366 field. If the original and current mode are the same, we need not
7367 adjust the offset. Otherwise, we do if bytes big endian.
7369 If INNER is not a MEM, get a piece consisting of just the field
7370 of interest (in this case POS % BITS_PER_WORD must be 0). */
7372 if (MEM_P (inner))
7374 HOST_WIDE_INT offset;
7376 /* POS counts from lsb, but make OFFSET count in memory order. */
7377 if (BYTES_BIG_ENDIAN)
7378 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7379 else
7380 offset = pos / BITS_PER_UNIT;
7382 new_rtx = adjust_address_nv (inner, tmode, offset);
7384 else if (REG_P (inner))
7386 if (tmode != inner_mode)
7388 /* We can't call gen_lowpart in a DEST since we
7389 always want a SUBREG (see below) and it would sometimes
7390 return a new hard register. */
7391 if (pos || in_dest)
7393 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7395 if (WORDS_BIG_ENDIAN
7396 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7397 final_word = ((GET_MODE_SIZE (inner_mode)
7398 - GET_MODE_SIZE (tmode))
7399 / UNITS_PER_WORD) - final_word;
7401 final_word *= UNITS_PER_WORD;
7402 if (BYTES_BIG_ENDIAN &&
7403 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7404 final_word += (GET_MODE_SIZE (inner_mode)
7405 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7407 /* Avoid creating invalid subregs, for example when
7408 simplifying (x>>32)&255. */
7409 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7410 return NULL_RTX;
7412 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7414 else
7415 new_rtx = gen_lowpart (tmode, inner);
7417 else
7418 new_rtx = inner;
7420 else
7421 new_rtx = force_to_mode (inner, tmode,
7422 len >= HOST_BITS_PER_WIDE_INT
7423 ? ~(unsigned HOST_WIDE_INT) 0
7424 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7427 /* If this extraction is going into the destination of a SET,
7428 make a STRICT_LOW_PART unless we made a MEM. */
7430 if (in_dest)
7431 return (MEM_P (new_rtx) ? new_rtx
7432 : (GET_CODE (new_rtx) != SUBREG
7433 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7434 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7436 if (mode == tmode)
7437 return new_rtx;
7439 if (CONST_SCALAR_INT_P (new_rtx))
7440 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7441 mode, new_rtx, tmode);
7443 /* If we know that no extraneous bits are set, and that the high
7444 bit is not set, convert the extraction to the cheaper of
7445 sign and zero extension, that are equivalent in these cases. */
7446 if (flag_expensive_optimizations
7447 && (HWI_COMPUTABLE_MODE_P (tmode)
7448 && ((nonzero_bits (new_rtx, tmode)
7449 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7450 == 0)))
7452 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7453 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7455 /* Prefer ZERO_EXTENSION, since it gives more information to
7456 backends. */
7457 if (set_src_cost (temp, optimize_this_for_speed_p)
7458 <= set_src_cost (temp1, optimize_this_for_speed_p))
7459 return temp;
7460 return temp1;
7463 /* Otherwise, sign- or zero-extend unless we already are in the
7464 proper mode. */
7466 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7467 mode, new_rtx));
7470 /* Unless this is a COMPARE or we have a funny memory reference,
7471 don't do anything with zero-extending field extracts starting at
7472 the low-order bit since they are simple AND operations. */
7473 if (pos_rtx == 0 && pos == 0 && ! in_dest
7474 && ! in_compare && unsignedp)
7475 return 0;
7477 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7478 if the position is not a constant and the length is not 1. In all
7479 other cases, we would only be going outside our object in cases when
7480 an original shift would have been undefined. */
7481 if (MEM_P (inner)
7482 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7483 || (pos_rtx != 0 && len != 1)))
7484 return 0;
7486 enum extraction_pattern pattern = (in_dest ? EP_insv
7487 : unsignedp ? EP_extzv : EP_extv);
7489 /* If INNER is not from memory, we want it to have the mode of a register
7490 extraction pattern's structure operand, or word_mode if there is no
7491 such pattern. The same applies to extraction_mode and pos_mode
7492 and their respective operands.
7494 For memory, assume that the desired extraction_mode and pos_mode
7495 are the same as for a register operation, since at present we don't
7496 have named patterns for aligned memory structures. */
7497 struct extraction_insn insn;
7498 if (get_best_reg_extraction_insn (&insn, pattern,
7499 GET_MODE_BITSIZE (inner_mode), mode))
7501 wanted_inner_reg_mode = insn.struct_mode;
7502 pos_mode = insn.pos_mode;
7503 extraction_mode = insn.field_mode;
7506 /* Never narrow an object, since that might not be safe. */
7508 if (mode != VOIDmode
7509 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7510 extraction_mode = mode;
7512 if (!MEM_P (inner))
7513 wanted_inner_mode = wanted_inner_reg_mode;
7514 else
7516 /* Be careful not to go beyond the extracted object and maintain the
7517 natural alignment of the memory. */
7518 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7519 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7520 > GET_MODE_BITSIZE (wanted_inner_mode))
7522 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7523 gcc_assert (wanted_inner_mode != VOIDmode);
7527 orig_pos = pos;
7529 if (BITS_BIG_ENDIAN)
7531 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7532 BITS_BIG_ENDIAN style. If position is constant, compute new
7533 position. Otherwise, build subtraction.
7534 Note that POS is relative to the mode of the original argument.
7535 If it's a MEM we need to recompute POS relative to that.
7536 However, if we're extracting from (or inserting into) a register,
7537 we want to recompute POS relative to wanted_inner_mode. */
7538 int width = (MEM_P (inner)
7539 ? GET_MODE_BITSIZE (is_mode)
7540 : GET_MODE_BITSIZE (wanted_inner_mode));
7542 if (pos_rtx == 0)
7543 pos = width - len - pos;
7544 else
7545 pos_rtx
7546 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7547 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7548 pos_rtx);
7549 /* POS may be less than 0 now, but we check for that below.
7550 Note that it can only be less than 0 if !MEM_P (inner). */
7553 /* If INNER has a wider mode, and this is a constant extraction, try to
7554 make it smaller and adjust the byte to point to the byte containing
7555 the value. */
7556 if (wanted_inner_mode != VOIDmode
7557 && inner_mode != wanted_inner_mode
7558 && ! pos_rtx
7559 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7560 && MEM_P (inner)
7561 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7562 && ! MEM_VOLATILE_P (inner))
7564 int offset = 0;
7566 /* The computations below will be correct if the machine is big
7567 endian in both bits and bytes or little endian in bits and bytes.
7568 If it is mixed, we must adjust. */
7570 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7571 adjust OFFSET to compensate. */
7572 if (BYTES_BIG_ENDIAN
7573 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7574 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7576 /* We can now move to the desired byte. */
7577 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7578 * GET_MODE_SIZE (wanted_inner_mode);
7579 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7581 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7582 && is_mode != wanted_inner_mode)
7583 offset = (GET_MODE_SIZE (is_mode)
7584 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7586 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7589 /* If INNER is not memory, get it into the proper mode. If we are changing
7590 its mode, POS must be a constant and smaller than the size of the new
7591 mode. */
7592 else if (!MEM_P (inner))
7594 /* On the LHS, don't create paradoxical subregs implicitely truncating
7595 the register unless TRULY_NOOP_TRUNCATION. */
7596 if (in_dest
7597 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7598 wanted_inner_mode))
7599 return NULL_RTX;
7601 if (GET_MODE (inner) != wanted_inner_mode
7602 && (pos_rtx != 0
7603 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7604 return NULL_RTX;
7606 if (orig_pos < 0)
7607 return NULL_RTX;
7609 inner = force_to_mode (inner, wanted_inner_mode,
7610 pos_rtx
7611 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7612 ? ~(unsigned HOST_WIDE_INT) 0
7613 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7614 << orig_pos),
7618 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7619 have to zero extend. Otherwise, we can just use a SUBREG. */
7620 if (pos_rtx != 0
7621 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7623 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7624 GET_MODE (pos_rtx));
7626 /* If we know that no extraneous bits are set, and that the high
7627 bit is not set, convert extraction to cheaper one - either
7628 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7629 cases. */
7630 if (flag_expensive_optimizations
7631 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7632 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7633 & ~(((unsigned HOST_WIDE_INT)
7634 GET_MODE_MASK (GET_MODE (pos_rtx)))
7635 >> 1))
7636 == 0)))
7638 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7639 GET_MODE (pos_rtx));
7641 /* Prefer ZERO_EXTENSION, since it gives more information to
7642 backends. */
7643 if (set_src_cost (temp1, optimize_this_for_speed_p)
7644 < set_src_cost (temp, optimize_this_for_speed_p))
7645 temp = temp1;
7647 pos_rtx = temp;
7650 /* Make POS_RTX unless we already have it and it is correct. If we don't
7651 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7652 be a CONST_INT. */
7653 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7654 pos_rtx = orig_pos_rtx;
7656 else if (pos_rtx == 0)
7657 pos_rtx = GEN_INT (pos);
7659 /* Make the required operation. See if we can use existing rtx. */
7660 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7661 extraction_mode, inner, GEN_INT (len), pos_rtx);
7662 if (! in_dest)
7663 new_rtx = gen_lowpart (mode, new_rtx);
7665 return new_rtx;
7668 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7669 with any other operations in X. Return X without that shift if so. */
7671 static rtx
7672 extract_left_shift (rtx x, int count)
7674 enum rtx_code code = GET_CODE (x);
7675 machine_mode mode = GET_MODE (x);
7676 rtx tem;
7678 switch (code)
7680 case ASHIFT:
7681 /* This is the shift itself. If it is wide enough, we will return
7682 either the value being shifted if the shift count is equal to
7683 COUNT or a shift for the difference. */
7684 if (CONST_INT_P (XEXP (x, 1))
7685 && INTVAL (XEXP (x, 1)) >= count)
7686 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7687 INTVAL (XEXP (x, 1)) - count);
7688 break;
7690 case NEG: case NOT:
7691 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7692 return simplify_gen_unary (code, mode, tem, mode);
7694 break;
7696 case PLUS: case IOR: case XOR: case AND:
7697 /* If we can safely shift this constant and we find the inner shift,
7698 make a new operation. */
7699 if (CONST_INT_P (XEXP (x, 1))
7700 && (UINTVAL (XEXP (x, 1))
7701 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7702 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7704 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7705 return simplify_gen_binary (code, mode, tem,
7706 gen_int_mode (val, mode));
7708 break;
7710 default:
7711 break;
7714 return 0;
7717 /* Look at the expression rooted at X. Look for expressions
7718 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7719 Form these expressions.
7721 Return the new rtx, usually just X.
7723 Also, for machines like the VAX that don't have logical shift insns,
7724 try to convert logical to arithmetic shift operations in cases where
7725 they are equivalent. This undoes the canonicalizations to logical
7726 shifts done elsewhere.
7728 We try, as much as possible, to re-use rtl expressions to save memory.
7730 IN_CODE says what kind of expression we are processing. Normally, it is
7731 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7732 being kludges), it is MEM. When processing the arguments of a comparison
7733 or a COMPARE against zero, it is COMPARE. */
7736 make_compound_operation (rtx x, enum rtx_code in_code)
7738 enum rtx_code code = GET_CODE (x);
7739 machine_mode mode = GET_MODE (x);
7740 int mode_width = GET_MODE_PRECISION (mode);
7741 rtx rhs, lhs;
7742 enum rtx_code next_code;
7743 int i, j;
7744 rtx new_rtx = 0;
7745 rtx tem;
7746 const char *fmt;
7748 /* Select the code to be used in recursive calls. Once we are inside an
7749 address, we stay there. If we have a comparison, set to COMPARE,
7750 but once inside, go back to our default of SET. */
7752 next_code = (code == MEM ? MEM
7753 : ((code == PLUS || code == MINUS)
7754 && SCALAR_INT_MODE_P (mode)) ? MEM
7755 : ((code == COMPARE || COMPARISON_P (x))
7756 && XEXP (x, 1) == const0_rtx) ? COMPARE
7757 : in_code == COMPARE ? SET : in_code);
7759 /* Process depending on the code of this operation. If NEW is set
7760 nonzero, it will be returned. */
7762 switch (code)
7764 case ASHIFT:
7765 /* Convert shifts by constants into multiplications if inside
7766 an address. */
7767 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7768 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7769 && INTVAL (XEXP (x, 1)) >= 0
7770 && SCALAR_INT_MODE_P (mode))
7772 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7773 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7775 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7776 if (GET_CODE (new_rtx) == NEG)
7778 new_rtx = XEXP (new_rtx, 0);
7779 multval = -multval;
7781 multval = trunc_int_for_mode (multval, mode);
7782 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7784 break;
7786 case PLUS:
7787 lhs = XEXP (x, 0);
7788 rhs = XEXP (x, 1);
7789 lhs = make_compound_operation (lhs, next_code);
7790 rhs = make_compound_operation (rhs, next_code);
7791 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7792 && SCALAR_INT_MODE_P (mode))
7794 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7795 XEXP (lhs, 1));
7796 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7798 else if (GET_CODE (lhs) == MULT
7799 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7801 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7802 simplify_gen_unary (NEG, mode,
7803 XEXP (lhs, 1),
7804 mode));
7805 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7807 else
7809 SUBST (XEXP (x, 0), lhs);
7810 SUBST (XEXP (x, 1), rhs);
7811 goto maybe_swap;
7813 x = gen_lowpart (mode, new_rtx);
7814 goto maybe_swap;
7816 case MINUS:
7817 lhs = XEXP (x, 0);
7818 rhs = XEXP (x, 1);
7819 lhs = make_compound_operation (lhs, next_code);
7820 rhs = make_compound_operation (rhs, next_code);
7821 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7822 && SCALAR_INT_MODE_P (mode))
7824 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7825 XEXP (rhs, 1));
7826 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7828 else if (GET_CODE (rhs) == MULT
7829 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7831 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7832 simplify_gen_unary (NEG, mode,
7833 XEXP (rhs, 1),
7834 mode));
7835 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7837 else
7839 SUBST (XEXP (x, 0), lhs);
7840 SUBST (XEXP (x, 1), rhs);
7841 return x;
7843 return gen_lowpart (mode, new_rtx);
7845 case AND:
7846 /* If the second operand is not a constant, we can't do anything
7847 with it. */
7848 if (!CONST_INT_P (XEXP (x, 1)))
7849 break;
7851 /* If the constant is a power of two minus one and the first operand
7852 is a logical right shift, make an extraction. */
7853 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7854 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7856 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7857 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7858 0, in_code == COMPARE);
7861 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7862 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7863 && subreg_lowpart_p (XEXP (x, 0))
7864 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7865 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7867 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7868 next_code);
7869 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7870 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7871 0, in_code == COMPARE);
7873 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7874 else if ((GET_CODE (XEXP (x, 0)) == XOR
7875 || GET_CODE (XEXP (x, 0)) == IOR)
7876 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7877 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7878 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7880 /* Apply the distributive law, and then try to make extractions. */
7881 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7882 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7883 XEXP (x, 1)),
7884 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7885 XEXP (x, 1)));
7886 new_rtx = make_compound_operation (new_rtx, in_code);
7889 /* If we are have (and (rotate X C) M) and C is larger than the number
7890 of bits in M, this is an extraction. */
7892 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7893 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7894 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7895 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7897 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7898 new_rtx = make_extraction (mode, new_rtx,
7899 (GET_MODE_PRECISION (mode)
7900 - INTVAL (XEXP (XEXP (x, 0), 1))),
7901 NULL_RTX, i, 1, 0, in_code == COMPARE);
7904 /* On machines without logical shifts, if the operand of the AND is
7905 a logical shift and our mask turns off all the propagated sign
7906 bits, we can replace the logical shift with an arithmetic shift. */
7907 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7908 && !have_insn_for (LSHIFTRT, mode)
7909 && have_insn_for (ASHIFTRT, mode)
7910 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7911 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7912 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7913 && mode_width <= HOST_BITS_PER_WIDE_INT)
7915 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7917 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7918 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7919 SUBST (XEXP (x, 0),
7920 gen_rtx_ASHIFTRT (mode,
7921 make_compound_operation
7922 (XEXP (XEXP (x, 0), 0), next_code),
7923 XEXP (XEXP (x, 0), 1)));
7926 /* If the constant is one less than a power of two, this might be
7927 representable by an extraction even if no shift is present.
7928 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7929 we are in a COMPARE. */
7930 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7931 new_rtx = make_extraction (mode,
7932 make_compound_operation (XEXP (x, 0),
7933 next_code),
7934 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7936 /* If we are in a comparison and this is an AND with a power of two,
7937 convert this into the appropriate bit extract. */
7938 else if (in_code == COMPARE
7939 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7940 new_rtx = make_extraction (mode,
7941 make_compound_operation (XEXP (x, 0),
7942 next_code),
7943 i, NULL_RTX, 1, 1, 0, 1);
7945 break;
7947 case LSHIFTRT:
7948 /* If the sign bit is known to be zero, replace this with an
7949 arithmetic shift. */
7950 if (have_insn_for (ASHIFTRT, mode)
7951 && ! have_insn_for (LSHIFTRT, mode)
7952 && mode_width <= HOST_BITS_PER_WIDE_INT
7953 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7955 new_rtx = gen_rtx_ASHIFTRT (mode,
7956 make_compound_operation (XEXP (x, 0),
7957 next_code),
7958 XEXP (x, 1));
7959 break;
7962 /* ... fall through ... */
7964 case ASHIFTRT:
7965 lhs = XEXP (x, 0);
7966 rhs = XEXP (x, 1);
7968 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7969 this is a SIGN_EXTRACT. */
7970 if (CONST_INT_P (rhs)
7971 && GET_CODE (lhs) == ASHIFT
7972 && CONST_INT_P (XEXP (lhs, 1))
7973 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7974 && INTVAL (XEXP (lhs, 1)) >= 0
7975 && INTVAL (rhs) < mode_width)
7977 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7978 new_rtx = make_extraction (mode, new_rtx,
7979 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7980 NULL_RTX, mode_width - INTVAL (rhs),
7981 code == LSHIFTRT, 0, in_code == COMPARE);
7982 break;
7985 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7986 If so, try to merge the shifts into a SIGN_EXTEND. We could
7987 also do this for some cases of SIGN_EXTRACT, but it doesn't
7988 seem worth the effort; the case checked for occurs on Alpha. */
7990 if (!OBJECT_P (lhs)
7991 && ! (GET_CODE (lhs) == SUBREG
7992 && (OBJECT_P (SUBREG_REG (lhs))))
7993 && CONST_INT_P (rhs)
7994 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7995 && INTVAL (rhs) < mode_width
7996 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7997 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7998 0, NULL_RTX, mode_width - INTVAL (rhs),
7999 code == LSHIFTRT, 0, in_code == COMPARE);
8001 break;
8003 case SUBREG:
8004 /* Call ourselves recursively on the inner expression. If we are
8005 narrowing the object and it has a different RTL code from
8006 what it originally did, do this SUBREG as a force_to_mode. */
8008 rtx inner = SUBREG_REG (x), simplified;
8009 enum rtx_code subreg_code = in_code;
8011 /* If in_code is COMPARE, it isn't always safe to pass it through
8012 to the recursive make_compound_operation call. */
8013 if (subreg_code == COMPARE
8014 && (!subreg_lowpart_p (x)
8015 || GET_CODE (inner) == SUBREG
8016 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8017 is (const_int 0), rather than
8018 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
8019 || (GET_CODE (inner) == AND
8020 && CONST_INT_P (XEXP (inner, 1))
8021 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8022 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8023 >= GET_MODE_BITSIZE (mode))))
8024 subreg_code = SET;
8026 tem = make_compound_operation (inner, subreg_code);
8028 simplified
8029 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8030 if (simplified)
8031 tem = simplified;
8033 if (GET_CODE (tem) != GET_CODE (inner)
8034 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8035 && subreg_lowpart_p (x))
8037 rtx newer
8038 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
8040 /* If we have something other than a SUBREG, we might have
8041 done an expansion, so rerun ourselves. */
8042 if (GET_CODE (newer) != SUBREG)
8043 newer = make_compound_operation (newer, in_code);
8045 /* force_to_mode can expand compounds. If it just re-expanded the
8046 compound, use gen_lowpart to convert to the desired mode. */
8047 if (rtx_equal_p (newer, x)
8048 /* Likewise if it re-expanded the compound only partially.
8049 This happens for SUBREG of ZERO_EXTRACT if they extract
8050 the same number of bits. */
8051 || (GET_CODE (newer) == SUBREG
8052 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8053 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8054 && GET_CODE (inner) == AND
8055 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8056 return gen_lowpart (GET_MODE (x), tem);
8058 return newer;
8061 if (simplified)
8062 return tem;
8064 break;
8066 default:
8067 break;
8070 if (new_rtx)
8072 x = gen_lowpart (mode, new_rtx);
8073 code = GET_CODE (x);
8076 /* Now recursively process each operand of this operation. We need to
8077 handle ZERO_EXTEND specially so that we don't lose track of the
8078 inner mode. */
8079 if (GET_CODE (x) == ZERO_EXTEND)
8081 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8082 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8083 new_rtx, GET_MODE (XEXP (x, 0)));
8084 if (tem)
8085 return tem;
8086 SUBST (XEXP (x, 0), new_rtx);
8087 return x;
8090 fmt = GET_RTX_FORMAT (code);
8091 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8092 if (fmt[i] == 'e')
8094 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8095 SUBST (XEXP (x, i), new_rtx);
8097 else if (fmt[i] == 'E')
8098 for (j = 0; j < XVECLEN (x, i); j++)
8100 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8101 SUBST (XVECEXP (x, i, j), new_rtx);
8104 maybe_swap:
8105 /* If this is a commutative operation, the changes to the operands
8106 may have made it noncanonical. */
8107 if (COMMUTATIVE_ARITH_P (x)
8108 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8110 tem = XEXP (x, 0);
8111 SUBST (XEXP (x, 0), XEXP (x, 1));
8112 SUBST (XEXP (x, 1), tem);
8115 return x;
8118 /* Given M see if it is a value that would select a field of bits
8119 within an item, but not the entire word. Return -1 if not.
8120 Otherwise, return the starting position of the field, where 0 is the
8121 low-order bit.
8123 *PLEN is set to the length of the field. */
8125 static int
8126 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8128 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8129 int pos = m ? ctz_hwi (m) : -1;
8130 int len = 0;
8132 if (pos >= 0)
8133 /* Now shift off the low-order zero bits and see if we have a
8134 power of two minus 1. */
8135 len = exact_log2 ((m >> pos) + 1);
8137 if (len <= 0)
8138 pos = -1;
8140 *plen = len;
8141 return pos;
8144 /* If X refers to a register that equals REG in value, replace these
8145 references with REG. */
8146 static rtx
8147 canon_reg_for_combine (rtx x, rtx reg)
8149 rtx op0, op1, op2;
8150 const char *fmt;
8151 int i;
8152 bool copied;
8154 enum rtx_code code = GET_CODE (x);
8155 switch (GET_RTX_CLASS (code))
8157 case RTX_UNARY:
8158 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8159 if (op0 != XEXP (x, 0))
8160 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8161 GET_MODE (reg));
8162 break;
8164 case RTX_BIN_ARITH:
8165 case RTX_COMM_ARITH:
8166 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8167 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8168 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8169 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8170 break;
8172 case RTX_COMPARE:
8173 case RTX_COMM_COMPARE:
8174 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8175 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8176 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8177 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8178 GET_MODE (op0), op0, op1);
8179 break;
8181 case RTX_TERNARY:
8182 case RTX_BITFIELD_OPS:
8183 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8184 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8185 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8186 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8187 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8188 GET_MODE (op0), op0, op1, op2);
8190 case RTX_OBJ:
8191 if (REG_P (x))
8193 if (rtx_equal_p (get_last_value (reg), x)
8194 || rtx_equal_p (reg, get_last_value (x)))
8195 return reg;
8196 else
8197 break;
8200 /* fall through */
8202 default:
8203 fmt = GET_RTX_FORMAT (code);
8204 copied = false;
8205 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8206 if (fmt[i] == 'e')
8208 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8209 if (op != XEXP (x, i))
8211 if (!copied)
8213 copied = true;
8214 x = copy_rtx (x);
8216 XEXP (x, i) = op;
8219 else if (fmt[i] == 'E')
8221 int j;
8222 for (j = 0; j < XVECLEN (x, i); j++)
8224 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8225 if (op != XVECEXP (x, i, j))
8227 if (!copied)
8229 copied = true;
8230 x = copy_rtx (x);
8232 XVECEXP (x, i, j) = op;
8237 break;
8240 return x;
8243 /* Return X converted to MODE. If the value is already truncated to
8244 MODE we can just return a subreg even though in the general case we
8245 would need an explicit truncation. */
8247 static rtx
8248 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8250 if (!CONST_INT_P (x)
8251 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8252 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8253 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8255 /* Bit-cast X into an integer mode. */
8256 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8257 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8258 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8259 x, GET_MODE (x));
8262 return gen_lowpart (mode, x);
8265 /* See if X can be simplified knowing that we will only refer to it in
8266 MODE and will only refer to those bits that are nonzero in MASK.
8267 If other bits are being computed or if masking operations are done
8268 that select a superset of the bits in MASK, they can sometimes be
8269 ignored.
8271 Return a possibly simplified expression, but always convert X to
8272 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8274 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8275 are all off in X. This is used when X will be complemented, by either
8276 NOT, NEG, or XOR. */
8278 static rtx
8279 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8280 int just_select)
8282 enum rtx_code code = GET_CODE (x);
8283 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8284 machine_mode op_mode;
8285 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8286 rtx op0, op1, temp;
8288 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8289 code below will do the wrong thing since the mode of such an
8290 expression is VOIDmode.
8292 Also do nothing if X is a CLOBBER; this can happen if X was
8293 the return value from a call to gen_lowpart. */
8294 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8295 return x;
8297 /* We want to perform the operation in its present mode unless we know
8298 that the operation is valid in MODE, in which case we do the operation
8299 in MODE. */
8300 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8301 && have_insn_for (code, mode))
8302 ? mode : GET_MODE (x));
8304 /* It is not valid to do a right-shift in a narrower mode
8305 than the one it came in with. */
8306 if ((code == LSHIFTRT || code == ASHIFTRT)
8307 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8308 op_mode = GET_MODE (x);
8310 /* Truncate MASK to fit OP_MODE. */
8311 if (op_mode)
8312 mask &= GET_MODE_MASK (op_mode);
8314 /* When we have an arithmetic operation, or a shift whose count we
8315 do not know, we need to assume that all bits up to the highest-order
8316 bit in MASK will be needed. This is how we form such a mask. */
8317 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8318 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8319 else
8320 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8321 - 1);
8323 /* Determine what bits of X are guaranteed to be (non)zero. */
8324 nonzero = nonzero_bits (x, mode);
8326 /* If none of the bits in X are needed, return a zero. */
8327 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8328 x = const0_rtx;
8330 /* If X is a CONST_INT, return a new one. Do this here since the
8331 test below will fail. */
8332 if (CONST_INT_P (x))
8334 if (SCALAR_INT_MODE_P (mode))
8335 return gen_int_mode (INTVAL (x) & mask, mode);
8336 else
8338 x = GEN_INT (INTVAL (x) & mask);
8339 return gen_lowpart_common (mode, x);
8343 /* If X is narrower than MODE and we want all the bits in X's mode, just
8344 get X in the proper mode. */
8345 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8346 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8347 return gen_lowpart (mode, x);
8349 /* We can ignore the effect of a SUBREG if it narrows the mode or
8350 if the constant masks to zero all the bits the mode doesn't have. */
8351 if (GET_CODE (x) == SUBREG
8352 && subreg_lowpart_p (x)
8353 && ((GET_MODE_SIZE (GET_MODE (x))
8354 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8355 || (0 == (mask
8356 & GET_MODE_MASK (GET_MODE (x))
8357 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8358 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8360 /* The arithmetic simplifications here only work for scalar integer modes. */
8361 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8362 return gen_lowpart_or_truncate (mode, x);
8364 switch (code)
8366 case CLOBBER:
8367 /* If X is a (clobber (const_int)), return it since we know we are
8368 generating something that won't match. */
8369 return x;
8371 case SIGN_EXTEND:
8372 case ZERO_EXTEND:
8373 case ZERO_EXTRACT:
8374 case SIGN_EXTRACT:
8375 x = expand_compound_operation (x);
8376 if (GET_CODE (x) != code)
8377 return force_to_mode (x, mode, mask, next_select);
8378 break;
8380 case TRUNCATE:
8381 /* Similarly for a truncate. */
8382 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8384 case AND:
8385 /* If this is an AND with a constant, convert it into an AND
8386 whose constant is the AND of that constant with MASK. If it
8387 remains an AND of MASK, delete it since it is redundant. */
8389 if (CONST_INT_P (XEXP (x, 1)))
8391 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8392 mask & INTVAL (XEXP (x, 1)));
8394 /* If X is still an AND, see if it is an AND with a mask that
8395 is just some low-order bits. If so, and it is MASK, we don't
8396 need it. */
8398 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8399 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8400 == mask))
8401 x = XEXP (x, 0);
8403 /* If it remains an AND, try making another AND with the bits
8404 in the mode mask that aren't in MASK turned on. If the
8405 constant in the AND is wide enough, this might make a
8406 cheaper constant. */
8408 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8409 && GET_MODE_MASK (GET_MODE (x)) != mask
8410 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8412 unsigned HOST_WIDE_INT cval
8413 = UINTVAL (XEXP (x, 1))
8414 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8415 rtx y;
8417 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8418 gen_int_mode (cval, GET_MODE (x)));
8419 if (set_src_cost (y, optimize_this_for_speed_p)
8420 < set_src_cost (x, optimize_this_for_speed_p))
8421 x = y;
8424 break;
8427 goto binop;
8429 case PLUS:
8430 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8431 low-order bits (as in an alignment operation) and FOO is already
8432 aligned to that boundary, mask C1 to that boundary as well.
8433 This may eliminate that PLUS and, later, the AND. */
8436 unsigned int width = GET_MODE_PRECISION (mode);
8437 unsigned HOST_WIDE_INT smask = mask;
8439 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8440 number, sign extend it. */
8442 if (width < HOST_BITS_PER_WIDE_INT
8443 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8444 smask |= HOST_WIDE_INT_M1U << width;
8446 if (CONST_INT_P (XEXP (x, 1))
8447 && exact_log2 (- smask) >= 0
8448 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8449 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8450 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8451 (INTVAL (XEXP (x, 1)) & smask)),
8452 mode, smask, next_select);
8455 /* ... fall through ... */
8457 case MULT:
8458 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8459 most significant bit in MASK since carries from those bits will
8460 affect the bits we are interested in. */
8461 mask = fuller_mask;
8462 goto binop;
8464 case MINUS:
8465 /* If X is (minus C Y) where C's least set bit is larger than any bit
8466 in the mask, then we may replace with (neg Y). */
8467 if (CONST_INT_P (XEXP (x, 0))
8468 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8470 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8471 GET_MODE (x));
8472 return force_to_mode (x, mode, mask, next_select);
8475 /* Similarly, if C contains every bit in the fuller_mask, then we may
8476 replace with (not Y). */
8477 if (CONST_INT_P (XEXP (x, 0))
8478 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8480 x = simplify_gen_unary (NOT, GET_MODE (x),
8481 XEXP (x, 1), GET_MODE (x));
8482 return force_to_mode (x, mode, mask, next_select);
8485 mask = fuller_mask;
8486 goto binop;
8488 case IOR:
8489 case XOR:
8490 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8491 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8492 operation which may be a bitfield extraction. Ensure that the
8493 constant we form is not wider than the mode of X. */
8495 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8496 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8497 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8498 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8499 && CONST_INT_P (XEXP (x, 1))
8500 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8501 + floor_log2 (INTVAL (XEXP (x, 1))))
8502 < GET_MODE_PRECISION (GET_MODE (x)))
8503 && (UINTVAL (XEXP (x, 1))
8504 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8506 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8507 << INTVAL (XEXP (XEXP (x, 0), 1)),
8508 GET_MODE (x));
8509 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8510 XEXP (XEXP (x, 0), 0), temp);
8511 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8512 XEXP (XEXP (x, 0), 1));
8513 return force_to_mode (x, mode, mask, next_select);
8516 binop:
8517 /* For most binary operations, just propagate into the operation and
8518 change the mode if we have an operation of that mode. */
8520 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8521 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8523 /* If we ended up truncating both operands, truncate the result of the
8524 operation instead. */
8525 if (GET_CODE (op0) == TRUNCATE
8526 && GET_CODE (op1) == TRUNCATE)
8528 op0 = XEXP (op0, 0);
8529 op1 = XEXP (op1, 0);
8532 op0 = gen_lowpart_or_truncate (op_mode, op0);
8533 op1 = gen_lowpart_or_truncate (op_mode, op1);
8535 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8536 x = simplify_gen_binary (code, op_mode, op0, op1);
8537 break;
8539 case ASHIFT:
8540 /* For left shifts, do the same, but just for the first operand.
8541 However, we cannot do anything with shifts where we cannot
8542 guarantee that the counts are smaller than the size of the mode
8543 because such a count will have a different meaning in a
8544 wider mode. */
8546 if (! (CONST_INT_P (XEXP (x, 1))
8547 && INTVAL (XEXP (x, 1)) >= 0
8548 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8549 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8550 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8551 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8552 break;
8554 /* If the shift count is a constant and we can do arithmetic in
8555 the mode of the shift, refine which bits we need. Otherwise, use the
8556 conservative form of the mask. */
8557 if (CONST_INT_P (XEXP (x, 1))
8558 && INTVAL (XEXP (x, 1)) >= 0
8559 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8560 && HWI_COMPUTABLE_MODE_P (op_mode))
8561 mask >>= INTVAL (XEXP (x, 1));
8562 else
8563 mask = fuller_mask;
8565 op0 = gen_lowpart_or_truncate (op_mode,
8566 force_to_mode (XEXP (x, 0), op_mode,
8567 mask, next_select));
8569 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8570 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8571 break;
8573 case LSHIFTRT:
8574 /* Here we can only do something if the shift count is a constant,
8575 this shift constant is valid for the host, and we can do arithmetic
8576 in OP_MODE. */
8578 if (CONST_INT_P (XEXP (x, 1))
8579 && INTVAL (XEXP (x, 1)) >= 0
8580 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8581 && HWI_COMPUTABLE_MODE_P (op_mode))
8583 rtx inner = XEXP (x, 0);
8584 unsigned HOST_WIDE_INT inner_mask;
8586 /* Select the mask of the bits we need for the shift operand. */
8587 inner_mask = mask << INTVAL (XEXP (x, 1));
8589 /* We can only change the mode of the shift if we can do arithmetic
8590 in the mode of the shift and INNER_MASK is no wider than the
8591 width of X's mode. */
8592 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8593 op_mode = GET_MODE (x);
8595 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8597 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8598 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8601 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8602 shift and AND produces only copies of the sign bit (C2 is one less
8603 than a power of two), we can do this with just a shift. */
8605 if (GET_CODE (x) == LSHIFTRT
8606 && CONST_INT_P (XEXP (x, 1))
8607 /* The shift puts one of the sign bit copies in the least significant
8608 bit. */
8609 && ((INTVAL (XEXP (x, 1))
8610 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8611 >= GET_MODE_PRECISION (GET_MODE (x)))
8612 && exact_log2 (mask + 1) >= 0
8613 /* Number of bits left after the shift must be more than the mask
8614 needs. */
8615 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8616 <= GET_MODE_PRECISION (GET_MODE (x)))
8617 /* Must be more sign bit copies than the mask needs. */
8618 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8619 >= exact_log2 (mask + 1)))
8620 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8621 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8622 - exact_log2 (mask + 1)));
8624 goto shiftrt;
8626 case ASHIFTRT:
8627 /* If we are just looking for the sign bit, we don't need this shift at
8628 all, even if it has a variable count. */
8629 if (val_signbit_p (GET_MODE (x), mask))
8630 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8632 /* If this is a shift by a constant, get a mask that contains those bits
8633 that are not copies of the sign bit. We then have two cases: If
8634 MASK only includes those bits, this can be a logical shift, which may
8635 allow simplifications. If MASK is a single-bit field not within
8636 those bits, we are requesting a copy of the sign bit and hence can
8637 shift the sign bit to the appropriate location. */
8639 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8640 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8642 int i;
8644 /* If the considered data is wider than HOST_WIDE_INT, we can't
8645 represent a mask for all its bits in a single scalar.
8646 But we only care about the lower bits, so calculate these. */
8648 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8650 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8652 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8653 is the number of bits a full-width mask would have set.
8654 We need only shift if these are fewer than nonzero can
8655 hold. If not, we must keep all bits set in nonzero. */
8657 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8658 < HOST_BITS_PER_WIDE_INT)
8659 nonzero >>= INTVAL (XEXP (x, 1))
8660 + HOST_BITS_PER_WIDE_INT
8661 - GET_MODE_PRECISION (GET_MODE (x)) ;
8663 else
8665 nonzero = GET_MODE_MASK (GET_MODE (x));
8666 nonzero >>= INTVAL (XEXP (x, 1));
8669 if ((mask & ~nonzero) == 0)
8671 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8672 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8673 if (GET_CODE (x) != ASHIFTRT)
8674 return force_to_mode (x, mode, mask, next_select);
8677 else if ((i = exact_log2 (mask)) >= 0)
8679 x = simplify_shift_const
8680 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8681 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8683 if (GET_CODE (x) != ASHIFTRT)
8684 return force_to_mode (x, mode, mask, next_select);
8688 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8689 even if the shift count isn't a constant. */
8690 if (mask == 1)
8691 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8692 XEXP (x, 0), XEXP (x, 1));
8694 shiftrt:
8696 /* If this is a zero- or sign-extension operation that just affects bits
8697 we don't care about, remove it. Be sure the call above returned
8698 something that is still a shift. */
8700 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8701 && CONST_INT_P (XEXP (x, 1))
8702 && INTVAL (XEXP (x, 1)) >= 0
8703 && (INTVAL (XEXP (x, 1))
8704 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8705 && GET_CODE (XEXP (x, 0)) == ASHIFT
8706 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8707 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8708 next_select);
8710 break;
8712 case ROTATE:
8713 case ROTATERT:
8714 /* If the shift count is constant and we can do computations
8715 in the mode of X, compute where the bits we care about are.
8716 Otherwise, we can't do anything. Don't change the mode of
8717 the shift or propagate MODE into the shift, though. */
8718 if (CONST_INT_P (XEXP (x, 1))
8719 && INTVAL (XEXP (x, 1)) >= 0)
8721 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8722 GET_MODE (x),
8723 gen_int_mode (mask, GET_MODE (x)),
8724 XEXP (x, 1));
8725 if (temp && CONST_INT_P (temp))
8726 x = simplify_gen_binary (code, GET_MODE (x),
8727 force_to_mode (XEXP (x, 0), GET_MODE (x),
8728 INTVAL (temp), next_select),
8729 XEXP (x, 1));
8731 break;
8733 case NEG:
8734 /* If we just want the low-order bit, the NEG isn't needed since it
8735 won't change the low-order bit. */
8736 if (mask == 1)
8737 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8739 /* We need any bits less significant than the most significant bit in
8740 MASK since carries from those bits will affect the bits we are
8741 interested in. */
8742 mask = fuller_mask;
8743 goto unop;
8745 case NOT:
8746 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8747 same as the XOR case above. Ensure that the constant we form is not
8748 wider than the mode of X. */
8750 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8751 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8752 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8753 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8754 < GET_MODE_PRECISION (GET_MODE (x)))
8755 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8757 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8758 GET_MODE (x));
8759 temp = simplify_gen_binary (XOR, GET_MODE (x),
8760 XEXP (XEXP (x, 0), 0), temp);
8761 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8762 temp, XEXP (XEXP (x, 0), 1));
8764 return force_to_mode (x, mode, mask, next_select);
8767 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8768 use the full mask inside the NOT. */
8769 mask = fuller_mask;
8771 unop:
8772 op0 = gen_lowpart_or_truncate (op_mode,
8773 force_to_mode (XEXP (x, 0), mode, mask,
8774 next_select));
8775 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8776 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8777 break;
8779 case NE:
8780 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8781 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8782 which is equal to STORE_FLAG_VALUE. */
8783 if ((mask & ~STORE_FLAG_VALUE) == 0
8784 && XEXP (x, 1) == const0_rtx
8785 && GET_MODE (XEXP (x, 0)) == mode
8786 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8787 && (nonzero_bits (XEXP (x, 0), mode)
8788 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8789 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8791 break;
8793 case IF_THEN_ELSE:
8794 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8795 written in a narrower mode. We play it safe and do not do so. */
8797 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8798 force_to_mode (XEXP (x, 1), mode,
8799 mask, next_select));
8800 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8801 force_to_mode (XEXP (x, 2), mode,
8802 mask, next_select));
8803 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8804 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8805 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8806 op0, op1);
8807 break;
8809 default:
8810 break;
8813 /* Ensure we return a value of the proper mode. */
8814 return gen_lowpart_or_truncate (mode, x);
8817 /* Return nonzero if X is an expression that has one of two values depending on
8818 whether some other value is zero or nonzero. In that case, we return the
8819 value that is being tested, *PTRUE is set to the value if the rtx being
8820 returned has a nonzero value, and *PFALSE is set to the other alternative.
8822 If we return zero, we set *PTRUE and *PFALSE to X. */
8824 static rtx
8825 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8827 machine_mode mode = GET_MODE (x);
8828 enum rtx_code code = GET_CODE (x);
8829 rtx cond0, cond1, true0, true1, false0, false1;
8830 unsigned HOST_WIDE_INT nz;
8832 /* If we are comparing a value against zero, we are done. */
8833 if ((code == NE || code == EQ)
8834 && XEXP (x, 1) == const0_rtx)
8836 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8837 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8838 return XEXP (x, 0);
8841 /* If this is a unary operation whose operand has one of two values, apply
8842 our opcode to compute those values. */
8843 else if (UNARY_P (x)
8844 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8846 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8847 *pfalse = simplify_gen_unary (code, mode, false0,
8848 GET_MODE (XEXP (x, 0)));
8849 return cond0;
8852 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8853 make can't possibly match and would suppress other optimizations. */
8854 else if (code == COMPARE)
8857 /* If this is a binary operation, see if either side has only one of two
8858 values. If either one does or if both do and they are conditional on
8859 the same value, compute the new true and false values. */
8860 else if (BINARY_P (x))
8862 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8863 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8865 if ((cond0 != 0 || cond1 != 0)
8866 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8868 /* If if_then_else_cond returned zero, then true/false are the
8869 same rtl. We must copy one of them to prevent invalid rtl
8870 sharing. */
8871 if (cond0 == 0)
8872 true0 = copy_rtx (true0);
8873 else if (cond1 == 0)
8874 true1 = copy_rtx (true1);
8876 if (COMPARISON_P (x))
8878 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8879 true0, true1);
8880 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8881 false0, false1);
8883 else
8885 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8886 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8889 return cond0 ? cond0 : cond1;
8892 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8893 operands is zero when the other is nonzero, and vice-versa,
8894 and STORE_FLAG_VALUE is 1 or -1. */
8896 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8897 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8898 || code == UMAX)
8899 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8901 rtx op0 = XEXP (XEXP (x, 0), 1);
8902 rtx op1 = XEXP (XEXP (x, 1), 1);
8904 cond0 = XEXP (XEXP (x, 0), 0);
8905 cond1 = XEXP (XEXP (x, 1), 0);
8907 if (COMPARISON_P (cond0)
8908 && COMPARISON_P (cond1)
8909 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8910 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8911 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8912 || ((swap_condition (GET_CODE (cond0))
8913 == reversed_comparison_code (cond1, NULL))
8914 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8915 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8916 && ! side_effects_p (x))
8918 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8919 *pfalse = simplify_gen_binary (MULT, mode,
8920 (code == MINUS
8921 ? simplify_gen_unary (NEG, mode,
8922 op1, mode)
8923 : op1),
8924 const_true_rtx);
8925 return cond0;
8929 /* Similarly for MULT, AND and UMIN, except that for these the result
8930 is always zero. */
8931 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8932 && (code == MULT || code == AND || code == UMIN)
8933 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8935 cond0 = XEXP (XEXP (x, 0), 0);
8936 cond1 = XEXP (XEXP (x, 1), 0);
8938 if (COMPARISON_P (cond0)
8939 && COMPARISON_P (cond1)
8940 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8941 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8942 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8943 || ((swap_condition (GET_CODE (cond0))
8944 == reversed_comparison_code (cond1, NULL))
8945 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8946 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8947 && ! side_effects_p (x))
8949 *ptrue = *pfalse = const0_rtx;
8950 return cond0;
8955 else if (code == IF_THEN_ELSE)
8957 /* If we have IF_THEN_ELSE already, extract the condition and
8958 canonicalize it if it is NE or EQ. */
8959 cond0 = XEXP (x, 0);
8960 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8961 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8962 return XEXP (cond0, 0);
8963 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8965 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8966 return XEXP (cond0, 0);
8968 else
8969 return cond0;
8972 /* If X is a SUBREG, we can narrow both the true and false values
8973 if the inner expression, if there is a condition. */
8974 else if (code == SUBREG
8975 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8976 &true0, &false0)))
8978 true0 = simplify_gen_subreg (mode, true0,
8979 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8980 false0 = simplify_gen_subreg (mode, false0,
8981 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8982 if (true0 && false0)
8984 *ptrue = true0;
8985 *pfalse = false0;
8986 return cond0;
8990 /* If X is a constant, this isn't special and will cause confusions
8991 if we treat it as such. Likewise if it is equivalent to a constant. */
8992 else if (CONSTANT_P (x)
8993 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8996 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8997 will be least confusing to the rest of the compiler. */
8998 else if (mode == BImode)
9000 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9001 return x;
9004 /* If X is known to be either 0 or -1, those are the true and
9005 false values when testing X. */
9006 else if (x == constm1_rtx || x == const0_rtx
9007 || (mode != VOIDmode
9008 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
9010 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9011 return x;
9014 /* Likewise for 0 or a single bit. */
9015 else if (HWI_COMPUTABLE_MODE_P (mode)
9016 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
9018 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9019 return x;
9022 /* Otherwise fail; show no condition with true and false values the same. */
9023 *ptrue = *pfalse = x;
9024 return 0;
9027 /* Return the value of expression X given the fact that condition COND
9028 is known to be true when applied to REG as its first operand and VAL
9029 as its second. X is known to not be shared and so can be modified in
9030 place.
9032 We only handle the simplest cases, and specifically those cases that
9033 arise with IF_THEN_ELSE expressions. */
9035 static rtx
9036 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9038 enum rtx_code code = GET_CODE (x);
9039 rtx temp;
9040 const char *fmt;
9041 int i, j;
9043 if (side_effects_p (x))
9044 return x;
9046 /* If either operand of the condition is a floating point value,
9047 then we have to avoid collapsing an EQ comparison. */
9048 if (cond == EQ
9049 && rtx_equal_p (x, reg)
9050 && ! FLOAT_MODE_P (GET_MODE (x))
9051 && ! FLOAT_MODE_P (GET_MODE (val)))
9052 return val;
9054 if (cond == UNEQ && rtx_equal_p (x, reg))
9055 return val;
9057 /* If X is (abs REG) and we know something about REG's relationship
9058 with zero, we may be able to simplify this. */
9060 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9061 switch (cond)
9063 case GE: case GT: case EQ:
9064 return XEXP (x, 0);
9065 case LT: case LE:
9066 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9067 XEXP (x, 0),
9068 GET_MODE (XEXP (x, 0)));
9069 default:
9070 break;
9073 /* The only other cases we handle are MIN, MAX, and comparisons if the
9074 operands are the same as REG and VAL. */
9076 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9078 if (rtx_equal_p (XEXP (x, 0), val))
9079 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
9081 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9083 if (COMPARISON_P (x))
9085 if (comparison_dominates_p (cond, code))
9086 return const_true_rtx;
9088 code = reversed_comparison_code (x, NULL);
9089 if (code != UNKNOWN
9090 && comparison_dominates_p (cond, code))
9091 return const0_rtx;
9092 else
9093 return x;
9095 else if (code == SMAX || code == SMIN
9096 || code == UMIN || code == UMAX)
9098 int unsignedp = (code == UMIN || code == UMAX);
9100 /* Do not reverse the condition when it is NE or EQ.
9101 This is because we cannot conclude anything about
9102 the value of 'SMAX (x, y)' when x is not equal to y,
9103 but we can when x equals y. */
9104 if ((code == SMAX || code == UMAX)
9105 && ! (cond == EQ || cond == NE))
9106 cond = reverse_condition (cond);
9108 switch (cond)
9110 case GE: case GT:
9111 return unsignedp ? x : XEXP (x, 1);
9112 case LE: case LT:
9113 return unsignedp ? x : XEXP (x, 0);
9114 case GEU: case GTU:
9115 return unsignedp ? XEXP (x, 1) : x;
9116 case LEU: case LTU:
9117 return unsignedp ? XEXP (x, 0) : x;
9118 default:
9119 break;
9124 else if (code == SUBREG)
9126 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9127 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9129 if (SUBREG_REG (x) != r)
9131 /* We must simplify subreg here, before we lose track of the
9132 original inner_mode. */
9133 new_rtx = simplify_subreg (GET_MODE (x), r,
9134 inner_mode, SUBREG_BYTE (x));
9135 if (new_rtx)
9136 return new_rtx;
9137 else
9138 SUBST (SUBREG_REG (x), r);
9141 return x;
9143 /* We don't have to handle SIGN_EXTEND here, because even in the
9144 case of replacing something with a modeless CONST_INT, a
9145 CONST_INT is already (supposed to be) a valid sign extension for
9146 its narrower mode, which implies it's already properly
9147 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9148 story is different. */
9149 else if (code == ZERO_EXTEND)
9151 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9152 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9154 if (XEXP (x, 0) != r)
9156 /* We must simplify the zero_extend here, before we lose
9157 track of the original inner_mode. */
9158 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9159 r, inner_mode);
9160 if (new_rtx)
9161 return new_rtx;
9162 else
9163 SUBST (XEXP (x, 0), r);
9166 return x;
9169 fmt = GET_RTX_FORMAT (code);
9170 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9172 if (fmt[i] == 'e')
9173 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9174 else if (fmt[i] == 'E')
9175 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9176 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9177 cond, reg, val));
9180 return x;
9183 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9184 assignment as a field assignment. */
9186 static int
9187 rtx_equal_for_field_assignment_p (rtx x, rtx y)
9189 if (x == y || rtx_equal_p (x, y))
9190 return 1;
9192 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9193 return 0;
9195 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9196 Note that all SUBREGs of MEM are paradoxical; otherwise they
9197 would have been rewritten. */
9198 if (MEM_P (x) && GET_CODE (y) == SUBREG
9199 && MEM_P (SUBREG_REG (y))
9200 && rtx_equal_p (SUBREG_REG (y),
9201 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9202 return 1;
9204 if (MEM_P (y) && GET_CODE (x) == SUBREG
9205 && MEM_P (SUBREG_REG (x))
9206 && rtx_equal_p (SUBREG_REG (x),
9207 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9208 return 1;
9210 /* We used to see if get_last_value of X and Y were the same but that's
9211 not correct. In one direction, we'll cause the assignment to have
9212 the wrong destination and in the case, we'll import a register into this
9213 insn that might have already have been dead. So fail if none of the
9214 above cases are true. */
9215 return 0;
9218 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9219 Return that assignment if so.
9221 We only handle the most common cases. */
9223 static rtx
9224 make_field_assignment (rtx x)
9226 rtx dest = SET_DEST (x);
9227 rtx src = SET_SRC (x);
9228 rtx assign;
9229 rtx rhs, lhs;
9230 HOST_WIDE_INT c1;
9231 HOST_WIDE_INT pos;
9232 unsigned HOST_WIDE_INT len;
9233 rtx other;
9234 machine_mode mode;
9236 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9237 a clear of a one-bit field. We will have changed it to
9238 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9239 for a SUBREG. */
9241 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9242 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9243 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9244 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9246 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9247 1, 1, 1, 0);
9248 if (assign != 0)
9249 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9250 return x;
9253 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9254 && subreg_lowpart_p (XEXP (src, 0))
9255 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9256 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9257 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9258 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9259 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9260 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9262 assign = make_extraction (VOIDmode, dest, 0,
9263 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9264 1, 1, 1, 0);
9265 if (assign != 0)
9266 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9267 return x;
9270 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9271 one-bit field. */
9272 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9273 && XEXP (XEXP (src, 0), 0) == const1_rtx
9274 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9276 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9277 1, 1, 1, 0);
9278 if (assign != 0)
9279 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9280 return x;
9283 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9284 SRC is an AND with all bits of that field set, then we can discard
9285 the AND. */
9286 if (GET_CODE (dest) == ZERO_EXTRACT
9287 && CONST_INT_P (XEXP (dest, 1))
9288 && GET_CODE (src) == AND
9289 && CONST_INT_P (XEXP (src, 1)))
9291 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9292 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9293 unsigned HOST_WIDE_INT ze_mask;
9295 if (width >= HOST_BITS_PER_WIDE_INT)
9296 ze_mask = -1;
9297 else
9298 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9300 /* Complete overlap. We can remove the source AND. */
9301 if ((and_mask & ze_mask) == ze_mask)
9302 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9304 /* Partial overlap. We can reduce the source AND. */
9305 if ((and_mask & ze_mask) != and_mask)
9307 mode = GET_MODE (src);
9308 src = gen_rtx_AND (mode, XEXP (src, 0),
9309 gen_int_mode (and_mask & ze_mask, mode));
9310 return gen_rtx_SET (VOIDmode, dest, src);
9314 /* The other case we handle is assignments into a constant-position
9315 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9316 a mask that has all one bits except for a group of zero bits and
9317 OTHER is known to have zeros where C1 has ones, this is such an
9318 assignment. Compute the position and length from C1. Shift OTHER
9319 to the appropriate position, force it to the required mode, and
9320 make the extraction. Check for the AND in both operands. */
9322 /* One or more SUBREGs might obscure the constant-position field
9323 assignment. The first one we are likely to encounter is an outer
9324 narrowing SUBREG, which we can just strip for the purposes of
9325 identifying the constant-field assignment. */
9326 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src))
9327 src = SUBREG_REG (src);
9329 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9330 return x;
9332 rhs = expand_compound_operation (XEXP (src, 0));
9333 lhs = expand_compound_operation (XEXP (src, 1));
9335 if (GET_CODE (rhs) == AND
9336 && CONST_INT_P (XEXP (rhs, 1))
9337 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9338 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9339 /* The second SUBREG that might get in the way is a paradoxical
9340 SUBREG around the first operand of the AND. We want to
9341 pretend the operand is as wide as the destination here. We
9342 do this by creating a new MEM in the wider mode for the sole
9343 purpose of the call to rtx_equal_for_field_assignment_p. Also
9344 note this trick only works for MEMs. */
9345 else if (GET_CODE (rhs) == AND
9346 && paradoxical_subreg_p (XEXP (rhs, 0))
9347 && GET_CODE (SUBREG_REG (XEXP (rhs, 0))) == MEM
9348 && CONST_INT_P (XEXP (rhs, 1))
9349 && rtx_equal_for_field_assignment_p (gen_rtx_MEM (GET_MODE (dest),
9350 XEXP (SUBREG_REG (XEXP (rhs, 0)), 0)),
9351 dest))
9352 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9353 else if (GET_CODE (lhs) == AND
9354 && CONST_INT_P (XEXP (lhs, 1))
9355 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9356 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9357 /* The second SUBREG that might get in the way is a paradoxical
9358 SUBREG around the first operand of the AND. We want to
9359 pretend the operand is as wide as the destination here. We
9360 do this by creating a new MEM in the wider mode for the sole
9361 purpose of the call to rtx_equal_for_field_assignment_p. Also
9362 note this trick only works for MEMs. */
9363 else if (GET_CODE (lhs) == AND
9364 && paradoxical_subreg_p (XEXP (lhs, 0))
9365 && GET_CODE (SUBREG_REG (XEXP (lhs, 0))) == MEM
9366 && CONST_INT_P (XEXP (lhs, 1))
9367 && rtx_equal_for_field_assignment_p (gen_rtx_MEM (GET_MODE (dest),
9368 XEXP (SUBREG_REG (XEXP (lhs, 0)), 0)),
9369 dest))
9370 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9371 else
9372 return x;
9374 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9375 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9376 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9377 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9378 return x;
9380 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9381 if (assign == 0)
9382 return x;
9384 /* The mode to use for the source is the mode of the assignment, or of
9385 what is inside a possible STRICT_LOW_PART. */
9386 mode = (GET_CODE (assign) == STRICT_LOW_PART
9387 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9389 /* Shift OTHER right POS places and make it the source, restricting it
9390 to the proper length and mode. */
9392 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9393 GET_MODE (src),
9394 other, pos),
9395 dest);
9396 src = force_to_mode (src, mode,
9397 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9398 ? ~(unsigned HOST_WIDE_INT) 0
9399 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9402 /* If SRC is masked by an AND that does not make a difference in
9403 the value being stored, strip it. */
9404 if (GET_CODE (assign) == ZERO_EXTRACT
9405 && CONST_INT_P (XEXP (assign, 1))
9406 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9407 && GET_CODE (src) == AND
9408 && CONST_INT_P (XEXP (src, 1))
9409 && UINTVAL (XEXP (src, 1))
9410 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9411 src = XEXP (src, 0);
9413 return gen_rtx_SET (VOIDmode, assign, src);
9416 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9417 if so. */
9419 static rtx
9420 apply_distributive_law (rtx x)
9422 enum rtx_code code = GET_CODE (x);
9423 enum rtx_code inner_code;
9424 rtx lhs, rhs, other;
9425 rtx tem;
9427 /* Distributivity is not true for floating point as it can change the
9428 value. So we don't do it unless -funsafe-math-optimizations. */
9429 if (FLOAT_MODE_P (GET_MODE (x))
9430 && ! flag_unsafe_math_optimizations)
9431 return x;
9433 /* The outer operation can only be one of the following: */
9434 if (code != IOR && code != AND && code != XOR
9435 && code != PLUS && code != MINUS)
9436 return x;
9438 lhs = XEXP (x, 0);
9439 rhs = XEXP (x, 1);
9441 /* If either operand is a primitive we can't do anything, so get out
9442 fast. */
9443 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9444 return x;
9446 lhs = expand_compound_operation (lhs);
9447 rhs = expand_compound_operation (rhs);
9448 inner_code = GET_CODE (lhs);
9449 if (inner_code != GET_CODE (rhs))
9450 return x;
9452 /* See if the inner and outer operations distribute. */
9453 switch (inner_code)
9455 case LSHIFTRT:
9456 case ASHIFTRT:
9457 case AND:
9458 case IOR:
9459 /* These all distribute except over PLUS. */
9460 if (code == PLUS || code == MINUS)
9461 return x;
9462 break;
9464 case MULT:
9465 if (code != PLUS && code != MINUS)
9466 return x;
9467 break;
9469 case ASHIFT:
9470 /* This is also a multiply, so it distributes over everything. */
9471 break;
9473 /* This used to handle SUBREG, but this turned out to be counter-
9474 productive, since (subreg (op ...)) usually is not handled by
9475 insn patterns, and this "optimization" therefore transformed
9476 recognizable patterns into unrecognizable ones. Therefore the
9477 SUBREG case was removed from here.
9479 It is possible that distributing SUBREG over arithmetic operations
9480 leads to an intermediate result than can then be optimized further,
9481 e.g. by moving the outer SUBREG to the other side of a SET as done
9482 in simplify_set. This seems to have been the original intent of
9483 handling SUBREGs here.
9485 However, with current GCC this does not appear to actually happen,
9486 at least on major platforms. If some case is found where removing
9487 the SUBREG case here prevents follow-on optimizations, distributing
9488 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9490 default:
9491 return x;
9494 /* Set LHS and RHS to the inner operands (A and B in the example
9495 above) and set OTHER to the common operand (C in the example).
9496 There is only one way to do this unless the inner operation is
9497 commutative. */
9498 if (COMMUTATIVE_ARITH_P (lhs)
9499 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9500 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9501 else if (COMMUTATIVE_ARITH_P (lhs)
9502 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9503 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9504 else if (COMMUTATIVE_ARITH_P (lhs)
9505 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9506 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9507 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9508 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9509 else
9510 return x;
9512 /* Form the new inner operation, seeing if it simplifies first. */
9513 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9515 /* There is one exception to the general way of distributing:
9516 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9517 if (code == XOR && inner_code == IOR)
9519 inner_code = AND;
9520 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9523 /* We may be able to continuing distributing the result, so call
9524 ourselves recursively on the inner operation before forming the
9525 outer operation, which we return. */
9526 return simplify_gen_binary (inner_code, GET_MODE (x),
9527 apply_distributive_law (tem), other);
9530 /* See if X is of the form (* (+ A B) C), and if so convert to
9531 (+ (* A C) (* B C)) and try to simplify.
9533 Most of the time, this results in no change. However, if some of
9534 the operands are the same or inverses of each other, simplifications
9535 will result.
9537 For example, (and (ior A B) (not B)) can occur as the result of
9538 expanding a bit field assignment. When we apply the distributive
9539 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9540 which then simplifies to (and (A (not B))).
9542 Note that no checks happen on the validity of applying the inverse
9543 distributive law. This is pointless since we can do it in the
9544 few places where this routine is called.
9546 N is the index of the term that is decomposed (the arithmetic operation,
9547 i.e. (+ A B) in the first example above). !N is the index of the term that
9548 is distributed, i.e. of C in the first example above. */
9549 static rtx
9550 distribute_and_simplify_rtx (rtx x, int n)
9552 machine_mode mode;
9553 enum rtx_code outer_code, inner_code;
9554 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9556 /* Distributivity is not true for floating point as it can change the
9557 value. So we don't do it unless -funsafe-math-optimizations. */
9558 if (FLOAT_MODE_P (GET_MODE (x))
9559 && ! flag_unsafe_math_optimizations)
9560 return NULL_RTX;
9562 decomposed = XEXP (x, n);
9563 if (!ARITHMETIC_P (decomposed))
9564 return NULL_RTX;
9566 mode = GET_MODE (x);
9567 outer_code = GET_CODE (x);
9568 distributed = XEXP (x, !n);
9570 inner_code = GET_CODE (decomposed);
9571 inner_op0 = XEXP (decomposed, 0);
9572 inner_op1 = XEXP (decomposed, 1);
9574 /* Special case (and (xor B C) (not A)), which is equivalent to
9575 (xor (ior A B) (ior A C)) */
9576 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9578 distributed = XEXP (distributed, 0);
9579 outer_code = IOR;
9582 if (n == 0)
9584 /* Distribute the second term. */
9585 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9586 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9588 else
9590 /* Distribute the first term. */
9591 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9592 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9595 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9596 new_op0, new_op1));
9597 if (GET_CODE (tmp) != outer_code
9598 && (set_src_cost (tmp, optimize_this_for_speed_p)
9599 < set_src_cost (x, optimize_this_for_speed_p)))
9600 return tmp;
9602 return NULL_RTX;
9605 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9606 in MODE. Return an equivalent form, if different from (and VAROP
9607 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9609 static rtx
9610 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9611 unsigned HOST_WIDE_INT constop)
9613 unsigned HOST_WIDE_INT nonzero;
9614 unsigned HOST_WIDE_INT orig_constop;
9615 rtx orig_varop;
9616 int i;
9618 orig_varop = varop;
9619 orig_constop = constop;
9620 if (GET_CODE (varop) == CLOBBER)
9621 return NULL_RTX;
9623 /* Simplify VAROP knowing that we will be only looking at some of the
9624 bits in it.
9626 Note by passing in CONSTOP, we guarantee that the bits not set in
9627 CONSTOP are not significant and will never be examined. We must
9628 ensure that is the case by explicitly masking out those bits
9629 before returning. */
9630 varop = force_to_mode (varop, mode, constop, 0);
9632 /* If VAROP is a CLOBBER, we will fail so return it. */
9633 if (GET_CODE (varop) == CLOBBER)
9634 return varop;
9636 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9637 to VAROP and return the new constant. */
9638 if (CONST_INT_P (varop))
9639 return gen_int_mode (INTVAL (varop) & constop, mode);
9641 /* See what bits may be nonzero in VAROP. Unlike the general case of
9642 a call to nonzero_bits, here we don't care about bits outside
9643 MODE. */
9645 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9647 /* Turn off all bits in the constant that are known to already be zero.
9648 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9649 which is tested below. */
9651 constop &= nonzero;
9653 /* If we don't have any bits left, return zero. */
9654 if (constop == 0)
9655 return const0_rtx;
9657 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9658 a power of two, we can replace this with an ASHIFT. */
9659 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9660 && (i = exact_log2 (constop)) >= 0)
9661 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9663 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9664 or XOR, then try to apply the distributive law. This may eliminate
9665 operations if either branch can be simplified because of the AND.
9666 It may also make some cases more complex, but those cases probably
9667 won't match a pattern either with or without this. */
9669 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9670 return
9671 gen_lowpart
9672 (mode,
9673 apply_distributive_law
9674 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9675 simplify_and_const_int (NULL_RTX,
9676 GET_MODE (varop),
9677 XEXP (varop, 0),
9678 constop),
9679 simplify_and_const_int (NULL_RTX,
9680 GET_MODE (varop),
9681 XEXP (varop, 1),
9682 constop))));
9684 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9685 the AND and see if one of the operands simplifies to zero. If so, we
9686 may eliminate it. */
9688 if (GET_CODE (varop) == PLUS
9689 && exact_log2 (constop + 1) >= 0)
9691 rtx o0, o1;
9693 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9694 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9695 if (o0 == const0_rtx)
9696 return o1;
9697 if (o1 == const0_rtx)
9698 return o0;
9701 /* Make a SUBREG if necessary. If we can't make it, fail. */
9702 varop = gen_lowpart (mode, varop);
9703 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9704 return NULL_RTX;
9706 /* If we are only masking insignificant bits, return VAROP. */
9707 if (constop == nonzero)
9708 return varop;
9710 if (varop == orig_varop && constop == orig_constop)
9711 return NULL_RTX;
9713 /* Otherwise, return an AND. */
9714 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9718 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9719 in MODE.
9721 Return an equivalent form, if different from X. Otherwise, return X. If
9722 X is zero, we are to always construct the equivalent form. */
9724 static rtx
9725 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9726 unsigned HOST_WIDE_INT constop)
9728 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9729 if (tem)
9730 return tem;
9732 if (!x)
9733 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9734 gen_int_mode (constop, mode));
9735 if (GET_MODE (x) != mode)
9736 x = gen_lowpart (mode, x);
9737 return x;
9740 /* Given a REG, X, compute which bits in X can be nonzero.
9741 We don't care about bits outside of those defined in MODE.
9743 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9744 a shift, AND, or zero_extract, we can do better. */
9746 static rtx
9747 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9748 const_rtx known_x ATTRIBUTE_UNUSED,
9749 machine_mode known_mode ATTRIBUTE_UNUSED,
9750 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9751 unsigned HOST_WIDE_INT *nonzero)
9753 rtx tem;
9754 reg_stat_type *rsp;
9756 /* If X is a register whose nonzero bits value is current, use it.
9757 Otherwise, if X is a register whose value we can find, use that
9758 value. Otherwise, use the previously-computed global nonzero bits
9759 for this register. */
9761 rsp = &reg_stat[REGNO (x)];
9762 if (rsp->last_set_value != 0
9763 && (rsp->last_set_mode == mode
9764 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9765 && GET_MODE_CLASS (mode) == MODE_INT))
9766 && ((rsp->last_set_label >= label_tick_ebb_start
9767 && rsp->last_set_label < label_tick)
9768 || (rsp->last_set_label == label_tick
9769 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9770 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9771 && REGNO (x) < reg_n_sets_max
9772 && REG_N_SETS (REGNO (x)) == 1
9773 && !REGNO_REG_SET_P
9774 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9775 REGNO (x)))))
9777 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9779 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9780 /* We don't know anything about the upper bits. */
9781 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9783 *nonzero &= mask;
9784 return NULL;
9787 tem = get_last_value (x);
9789 if (tem)
9791 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9792 /* If X is narrower than MODE and TEM is a non-negative
9793 constant that would appear negative in the mode of X,
9794 sign-extend it for use in reg_nonzero_bits because some
9795 machines (maybe most) will actually do the sign-extension
9796 and this is the conservative approach.
9798 ??? For 2.5, try to tighten up the MD files in this regard
9799 instead of this kludge. */
9801 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9802 && CONST_INT_P (tem)
9803 && INTVAL (tem) > 0
9804 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9805 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9806 #endif
9807 return tem;
9809 else if (nonzero_sign_valid && rsp->nonzero_bits)
9811 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9813 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9814 /* We don't know anything about the upper bits. */
9815 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9817 *nonzero &= mask;
9820 return NULL;
9823 /* Return the number of bits at the high-order end of X that are known to
9824 be equal to the sign bit. X will be used in mode MODE; if MODE is
9825 VOIDmode, X will be used in its own mode. The returned value will always
9826 be between 1 and the number of bits in MODE. */
9828 static rtx
9829 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9830 const_rtx known_x ATTRIBUTE_UNUSED,
9831 machine_mode known_mode
9832 ATTRIBUTE_UNUSED,
9833 unsigned int known_ret ATTRIBUTE_UNUSED,
9834 unsigned int *result)
9836 rtx tem;
9837 reg_stat_type *rsp;
9839 rsp = &reg_stat[REGNO (x)];
9840 if (rsp->last_set_value != 0
9841 && rsp->last_set_mode == mode
9842 && ((rsp->last_set_label >= label_tick_ebb_start
9843 && rsp->last_set_label < label_tick)
9844 || (rsp->last_set_label == label_tick
9845 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9846 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9847 && REGNO (x) < reg_n_sets_max
9848 && REG_N_SETS (REGNO (x)) == 1
9849 && !REGNO_REG_SET_P
9850 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9851 REGNO (x)))))
9853 *result = rsp->last_set_sign_bit_copies;
9854 return NULL;
9857 tem = get_last_value (x);
9858 if (tem != 0)
9859 return tem;
9861 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9862 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9863 *result = rsp->sign_bit_copies;
9865 return NULL;
9868 /* Return the number of "extended" bits there are in X, when interpreted
9869 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9870 unsigned quantities, this is the number of high-order zero bits.
9871 For signed quantities, this is the number of copies of the sign bit
9872 minus 1. In both case, this function returns the number of "spare"
9873 bits. For example, if two quantities for which this function returns
9874 at least 1 are added, the addition is known not to overflow.
9876 This function will always return 0 unless called during combine, which
9877 implies that it must be called from a define_split. */
9879 unsigned int
9880 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9882 if (nonzero_sign_valid == 0)
9883 return 0;
9885 return (unsignedp
9886 ? (HWI_COMPUTABLE_MODE_P (mode)
9887 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9888 - floor_log2 (nonzero_bits (x, mode)))
9889 : 0)
9890 : num_sign_bit_copies (x, mode) - 1);
9893 /* This function is called from `simplify_shift_const' to merge two
9894 outer operations. Specifically, we have already found that we need
9895 to perform operation *POP0 with constant *PCONST0 at the outermost
9896 position. We would now like to also perform OP1 with constant CONST1
9897 (with *POP0 being done last).
9899 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9900 the resulting operation. *PCOMP_P is set to 1 if we would need to
9901 complement the innermost operand, otherwise it is unchanged.
9903 MODE is the mode in which the operation will be done. No bits outside
9904 the width of this mode matter. It is assumed that the width of this mode
9905 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9907 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9908 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9909 result is simply *PCONST0.
9911 If the resulting operation cannot be expressed as one operation, we
9912 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9914 static int
9915 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)
9917 enum rtx_code op0 = *pop0;
9918 HOST_WIDE_INT const0 = *pconst0;
9920 const0 &= GET_MODE_MASK (mode);
9921 const1 &= GET_MODE_MASK (mode);
9923 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9924 if (op0 == AND)
9925 const1 &= const0;
9927 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9928 if OP0 is SET. */
9930 if (op1 == UNKNOWN || op0 == SET)
9931 return 1;
9933 else if (op0 == UNKNOWN)
9934 op0 = op1, const0 = const1;
9936 else if (op0 == op1)
9938 switch (op0)
9940 case AND:
9941 const0 &= const1;
9942 break;
9943 case IOR:
9944 const0 |= const1;
9945 break;
9946 case XOR:
9947 const0 ^= const1;
9948 break;
9949 case PLUS:
9950 const0 += const1;
9951 break;
9952 case NEG:
9953 op0 = UNKNOWN;
9954 break;
9955 default:
9956 break;
9960 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9961 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9962 return 0;
9964 /* If the two constants aren't the same, we can't do anything. The
9965 remaining six cases can all be done. */
9966 else if (const0 != const1)
9967 return 0;
9969 else
9970 switch (op0)
9972 case IOR:
9973 if (op1 == AND)
9974 /* (a & b) | b == b */
9975 op0 = SET;
9976 else /* op1 == XOR */
9977 /* (a ^ b) | b == a | b */
9979 break;
9981 case XOR:
9982 if (op1 == AND)
9983 /* (a & b) ^ b == (~a) & b */
9984 op0 = AND, *pcomp_p = 1;
9985 else /* op1 == IOR */
9986 /* (a | b) ^ b == a & ~b */
9987 op0 = AND, const0 = ~const0;
9988 break;
9990 case AND:
9991 if (op1 == IOR)
9992 /* (a | b) & b == b */
9993 op0 = SET;
9994 else /* op1 == XOR */
9995 /* (a ^ b) & b) == (~a) & b */
9996 *pcomp_p = 1;
9997 break;
9998 default:
9999 break;
10002 /* Check for NO-OP cases. */
10003 const0 &= GET_MODE_MASK (mode);
10004 if (const0 == 0
10005 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10006 op0 = UNKNOWN;
10007 else if (const0 == 0 && op0 == AND)
10008 op0 = SET;
10009 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10010 && op0 == AND)
10011 op0 = UNKNOWN;
10013 *pop0 = op0;
10015 /* ??? Slightly redundant with the above mask, but not entirely.
10016 Moving this above means we'd have to sign-extend the mode mask
10017 for the final test. */
10018 if (op0 != UNKNOWN && op0 != NEG)
10019 *pconst0 = trunc_int_for_mode (const0, mode);
10021 return 1;
10024 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10025 the shift in. The original shift operation CODE is performed on OP in
10026 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10027 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10028 result of the shift is subject to operation OUTER_CODE with operand
10029 OUTER_CONST. */
10031 static machine_mode
10032 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10033 machine_mode orig_mode, machine_mode mode,
10034 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10036 if (orig_mode == mode)
10037 return mode;
10038 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10040 /* In general we can't perform in wider mode for right shift and rotate. */
10041 switch (code)
10043 case ASHIFTRT:
10044 /* We can still widen if the bits brought in from the left are identical
10045 to the sign bit of ORIG_MODE. */
10046 if (num_sign_bit_copies (op, mode)
10047 > (unsigned) (GET_MODE_PRECISION (mode)
10048 - GET_MODE_PRECISION (orig_mode)))
10049 return mode;
10050 return orig_mode;
10052 case LSHIFTRT:
10053 /* Similarly here but with zero bits. */
10054 if (HWI_COMPUTABLE_MODE_P (mode)
10055 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10056 return mode;
10058 /* We can also widen if the bits brought in will be masked off. This
10059 operation is performed in ORIG_MODE. */
10060 if (outer_code == AND)
10062 int care_bits = low_bitmask_len (orig_mode, outer_const);
10064 if (care_bits >= 0
10065 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10066 return mode;
10068 /* fall through */
10070 case ROTATE:
10071 return orig_mode;
10073 case ROTATERT:
10074 gcc_unreachable ();
10076 default:
10077 return mode;
10081 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10082 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10083 if we cannot simplify it. Otherwise, return a simplified value.
10085 The shift is normally computed in the widest mode we find in VAROP, as
10086 long as it isn't a different number of words than RESULT_MODE. Exceptions
10087 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10089 static rtx
10090 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10091 rtx varop, int orig_count)
10093 enum rtx_code orig_code = code;
10094 rtx orig_varop = varop;
10095 int count;
10096 machine_mode mode = result_mode;
10097 machine_mode shift_mode, tmode;
10098 unsigned int mode_words
10099 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10100 /* We form (outer_op (code varop count) (outer_const)). */
10101 enum rtx_code outer_op = UNKNOWN;
10102 HOST_WIDE_INT outer_const = 0;
10103 int complement_p = 0;
10104 rtx new_rtx, x;
10106 /* Make sure and truncate the "natural" shift on the way in. We don't
10107 want to do this inside the loop as it makes it more difficult to
10108 combine shifts. */
10109 if (SHIFT_COUNT_TRUNCATED)
10110 orig_count &= GET_MODE_BITSIZE (mode) - 1;
10112 /* If we were given an invalid count, don't do anything except exactly
10113 what was requested. */
10115 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
10116 return NULL_RTX;
10118 count = orig_count;
10120 /* Unless one of the branches of the `if' in this loop does a `continue',
10121 we will `break' the loop after the `if'. */
10123 while (count != 0)
10125 /* If we have an operand of (clobber (const_int 0)), fail. */
10126 if (GET_CODE (varop) == CLOBBER)
10127 return NULL_RTX;
10129 /* Convert ROTATERT to ROTATE. */
10130 if (code == ROTATERT)
10132 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
10133 code = ROTATE;
10134 if (VECTOR_MODE_P (result_mode))
10135 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10136 else
10137 count = bitsize - count;
10140 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10141 mode, outer_op, outer_const);
10143 /* Handle cases where the count is greater than the size of the mode
10144 minus 1. For ASHIFT, use the size minus one as the count (this can
10145 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10146 take the count modulo the size. For other shifts, the result is
10147 zero.
10149 Since these shifts are being produced by the compiler by combining
10150 multiple operations, each of which are defined, we know what the
10151 result is supposed to be. */
10153 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10155 if (code == ASHIFTRT)
10156 count = GET_MODE_PRECISION (shift_mode) - 1;
10157 else if (code == ROTATE || code == ROTATERT)
10158 count %= GET_MODE_PRECISION (shift_mode);
10159 else
10161 /* We can't simply return zero because there may be an
10162 outer op. */
10163 varop = const0_rtx;
10164 count = 0;
10165 break;
10169 /* If we discovered we had to complement VAROP, leave. Making a NOT
10170 here would cause an infinite loop. */
10171 if (complement_p)
10172 break;
10174 /* An arithmetic right shift of a quantity known to be -1 or 0
10175 is a no-op. */
10176 if (code == ASHIFTRT
10177 && (num_sign_bit_copies (varop, shift_mode)
10178 == GET_MODE_PRECISION (shift_mode)))
10180 count = 0;
10181 break;
10184 /* If we are doing an arithmetic right shift and discarding all but
10185 the sign bit copies, this is equivalent to doing a shift by the
10186 bitsize minus one. Convert it into that shift because it will often
10187 allow other simplifications. */
10189 if (code == ASHIFTRT
10190 && (count + num_sign_bit_copies (varop, shift_mode)
10191 >= GET_MODE_PRECISION (shift_mode)))
10192 count = GET_MODE_PRECISION (shift_mode) - 1;
10194 /* We simplify the tests below and elsewhere by converting
10195 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10196 `make_compound_operation' will convert it to an ASHIFTRT for
10197 those machines (such as VAX) that don't have an LSHIFTRT. */
10198 if (code == ASHIFTRT
10199 && val_signbit_known_clear_p (shift_mode,
10200 nonzero_bits (varop, shift_mode)))
10201 code = LSHIFTRT;
10203 if (((code == LSHIFTRT
10204 && HWI_COMPUTABLE_MODE_P (shift_mode)
10205 && !(nonzero_bits (varop, shift_mode) >> count))
10206 || (code == ASHIFT
10207 && HWI_COMPUTABLE_MODE_P (shift_mode)
10208 && !((nonzero_bits (varop, shift_mode) << count)
10209 & GET_MODE_MASK (shift_mode))))
10210 && !side_effects_p (varop))
10211 varop = const0_rtx;
10213 switch (GET_CODE (varop))
10215 case SIGN_EXTEND:
10216 case ZERO_EXTEND:
10217 case SIGN_EXTRACT:
10218 case ZERO_EXTRACT:
10219 new_rtx = expand_compound_operation (varop);
10220 if (new_rtx != varop)
10222 varop = new_rtx;
10223 continue;
10225 break;
10227 case MEM:
10228 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10229 minus the width of a smaller mode, we can do this with a
10230 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10231 if ((code == ASHIFTRT || code == LSHIFTRT)
10232 && ! mode_dependent_address_p (XEXP (varop, 0),
10233 MEM_ADDR_SPACE (varop))
10234 && ! MEM_VOLATILE_P (varop)
10235 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10236 MODE_INT, 1)) != BLKmode)
10238 new_rtx = adjust_address_nv (varop, tmode,
10239 BYTES_BIG_ENDIAN ? 0
10240 : count / BITS_PER_UNIT);
10242 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10243 : ZERO_EXTEND, mode, new_rtx);
10244 count = 0;
10245 continue;
10247 break;
10249 case SUBREG:
10250 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10251 the same number of words as what we've seen so far. Then store
10252 the widest mode in MODE. */
10253 if (subreg_lowpart_p (varop)
10254 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10255 > GET_MODE_SIZE (GET_MODE (varop)))
10256 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10257 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10258 == mode_words
10259 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10260 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10262 varop = SUBREG_REG (varop);
10263 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10264 mode = GET_MODE (varop);
10265 continue;
10267 break;
10269 case MULT:
10270 /* Some machines use MULT instead of ASHIFT because MULT
10271 is cheaper. But it is still better on those machines to
10272 merge two shifts into one. */
10273 if (CONST_INT_P (XEXP (varop, 1))
10274 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10276 varop
10277 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10278 XEXP (varop, 0),
10279 GEN_INT (exact_log2 (
10280 UINTVAL (XEXP (varop, 1)))));
10281 continue;
10283 break;
10285 case UDIV:
10286 /* Similar, for when divides are cheaper. */
10287 if (CONST_INT_P (XEXP (varop, 1))
10288 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10290 varop
10291 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10292 XEXP (varop, 0),
10293 GEN_INT (exact_log2 (
10294 UINTVAL (XEXP (varop, 1)))));
10295 continue;
10297 break;
10299 case ASHIFTRT:
10300 /* If we are extracting just the sign bit of an arithmetic
10301 right shift, that shift is not needed. However, the sign
10302 bit of a wider mode may be different from what would be
10303 interpreted as the sign bit in a narrower mode, so, if
10304 the result is narrower, don't discard the shift. */
10305 if (code == LSHIFTRT
10306 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10307 && (GET_MODE_BITSIZE (result_mode)
10308 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10310 varop = XEXP (varop, 0);
10311 continue;
10314 /* ... fall through ... */
10316 case LSHIFTRT:
10317 case ASHIFT:
10318 case ROTATE:
10319 /* Here we have two nested shifts. The result is usually the
10320 AND of a new shift with a mask. We compute the result below. */
10321 if (CONST_INT_P (XEXP (varop, 1))
10322 && INTVAL (XEXP (varop, 1)) >= 0
10323 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10324 && HWI_COMPUTABLE_MODE_P (result_mode)
10325 && HWI_COMPUTABLE_MODE_P (mode)
10326 && !VECTOR_MODE_P (result_mode))
10328 enum rtx_code first_code = GET_CODE (varop);
10329 unsigned int first_count = INTVAL (XEXP (varop, 1));
10330 unsigned HOST_WIDE_INT mask;
10331 rtx mask_rtx;
10333 /* We have one common special case. We can't do any merging if
10334 the inner code is an ASHIFTRT of a smaller mode. However, if
10335 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10336 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10337 we can convert it to
10338 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10339 This simplifies certain SIGN_EXTEND operations. */
10340 if (code == ASHIFT && first_code == ASHIFTRT
10341 && count == (GET_MODE_PRECISION (result_mode)
10342 - GET_MODE_PRECISION (GET_MODE (varop))))
10344 /* C3 has the low-order C1 bits zero. */
10346 mask = GET_MODE_MASK (mode)
10347 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10349 varop = simplify_and_const_int (NULL_RTX, result_mode,
10350 XEXP (varop, 0), mask);
10351 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10352 varop, count);
10353 count = first_count;
10354 code = ASHIFTRT;
10355 continue;
10358 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10359 than C1 high-order bits equal to the sign bit, we can convert
10360 this to either an ASHIFT or an ASHIFTRT depending on the
10361 two counts.
10363 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10365 if (code == ASHIFTRT && first_code == ASHIFT
10366 && GET_MODE (varop) == shift_mode
10367 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10368 > first_count))
10370 varop = XEXP (varop, 0);
10371 count -= first_count;
10372 if (count < 0)
10374 count = -count;
10375 code = ASHIFT;
10378 continue;
10381 /* There are some cases we can't do. If CODE is ASHIFTRT,
10382 we can only do this if FIRST_CODE is also ASHIFTRT.
10384 We can't do the case when CODE is ROTATE and FIRST_CODE is
10385 ASHIFTRT.
10387 If the mode of this shift is not the mode of the outer shift,
10388 we can't do this if either shift is a right shift or ROTATE.
10390 Finally, we can't do any of these if the mode is too wide
10391 unless the codes are the same.
10393 Handle the case where the shift codes are the same
10394 first. */
10396 if (code == first_code)
10398 if (GET_MODE (varop) != result_mode
10399 && (code == ASHIFTRT || code == LSHIFTRT
10400 || code == ROTATE))
10401 break;
10403 count += first_count;
10404 varop = XEXP (varop, 0);
10405 continue;
10408 if (code == ASHIFTRT
10409 || (code == ROTATE && first_code == ASHIFTRT)
10410 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10411 || (GET_MODE (varop) != result_mode
10412 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10413 || first_code == ROTATE
10414 || code == ROTATE)))
10415 break;
10417 /* To compute the mask to apply after the shift, shift the
10418 nonzero bits of the inner shift the same way the
10419 outer shift will. */
10421 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10422 result_mode);
10424 mask_rtx
10425 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10426 GEN_INT (count));
10428 /* Give up if we can't compute an outer operation to use. */
10429 if (mask_rtx == 0
10430 || !CONST_INT_P (mask_rtx)
10431 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10432 INTVAL (mask_rtx),
10433 result_mode, &complement_p))
10434 break;
10436 /* If the shifts are in the same direction, we add the
10437 counts. Otherwise, we subtract them. */
10438 if ((code == ASHIFTRT || code == LSHIFTRT)
10439 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10440 count += first_count;
10441 else
10442 count -= first_count;
10444 /* If COUNT is positive, the new shift is usually CODE,
10445 except for the two exceptions below, in which case it is
10446 FIRST_CODE. If the count is negative, FIRST_CODE should
10447 always be used */
10448 if (count > 0
10449 && ((first_code == ROTATE && code == ASHIFT)
10450 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10451 code = first_code;
10452 else if (count < 0)
10453 code = first_code, count = -count;
10455 varop = XEXP (varop, 0);
10456 continue;
10459 /* If we have (A << B << C) for any shift, we can convert this to
10460 (A << C << B). This wins if A is a constant. Only try this if
10461 B is not a constant. */
10463 else if (GET_CODE (varop) == code
10464 && CONST_INT_P (XEXP (varop, 0))
10465 && !CONST_INT_P (XEXP (varop, 1)))
10467 rtx new_rtx = simplify_const_binary_operation (code, mode,
10468 XEXP (varop, 0),
10469 GEN_INT (count));
10470 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10471 count = 0;
10472 continue;
10474 break;
10476 case NOT:
10477 if (VECTOR_MODE_P (mode))
10478 break;
10480 /* Make this fit the case below. */
10481 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10482 continue;
10484 case IOR:
10485 case AND:
10486 case XOR:
10487 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10488 with C the size of VAROP - 1 and the shift is logical if
10489 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10490 we have an (le X 0) operation. If we have an arithmetic shift
10491 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10492 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10494 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10495 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10496 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10497 && (code == LSHIFTRT || code == ASHIFTRT)
10498 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10499 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10501 count = 0;
10502 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10503 const0_rtx);
10505 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10506 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10508 continue;
10511 /* If we have (shift (logical)), move the logical to the outside
10512 to allow it to possibly combine with another logical and the
10513 shift to combine with another shift. This also canonicalizes to
10514 what a ZERO_EXTRACT looks like. Also, some machines have
10515 (and (shift)) insns. */
10517 if (CONST_INT_P (XEXP (varop, 1))
10518 /* We can't do this if we have (ashiftrt (xor)) and the
10519 constant has its sign bit set in shift_mode with shift_mode
10520 wider than result_mode. */
10521 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10522 && result_mode != shift_mode
10523 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10524 shift_mode))
10525 && (new_rtx = simplify_const_binary_operation
10526 (code, result_mode,
10527 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10528 GEN_INT (count))) != 0
10529 && CONST_INT_P (new_rtx)
10530 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10531 INTVAL (new_rtx), result_mode, &complement_p))
10533 varop = XEXP (varop, 0);
10534 continue;
10537 /* If we can't do that, try to simplify the shift in each arm of the
10538 logical expression, make a new logical expression, and apply
10539 the inverse distributive law. This also can't be done for
10540 (ashiftrt (xor)) where we've widened the shift and the constant
10541 changes the sign bit. */
10542 if (CONST_INT_P (XEXP (varop, 1))
10543 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10544 && result_mode != shift_mode
10545 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10546 shift_mode)))
10548 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10549 XEXP (varop, 0), count);
10550 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10551 XEXP (varop, 1), count);
10553 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10554 lhs, rhs);
10555 varop = apply_distributive_law (varop);
10557 count = 0;
10558 continue;
10560 break;
10562 case EQ:
10563 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10564 says that the sign bit can be tested, FOO has mode MODE, C is
10565 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10566 that may be nonzero. */
10567 if (code == LSHIFTRT
10568 && XEXP (varop, 1) == const0_rtx
10569 && GET_MODE (XEXP (varop, 0)) == result_mode
10570 && count == (GET_MODE_PRECISION (result_mode) - 1)
10571 && HWI_COMPUTABLE_MODE_P (result_mode)
10572 && STORE_FLAG_VALUE == -1
10573 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10574 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10575 &complement_p))
10577 varop = XEXP (varop, 0);
10578 count = 0;
10579 continue;
10581 break;
10583 case NEG:
10584 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10585 than the number of bits in the mode is equivalent to A. */
10586 if (code == LSHIFTRT
10587 && count == (GET_MODE_PRECISION (result_mode) - 1)
10588 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10590 varop = XEXP (varop, 0);
10591 count = 0;
10592 continue;
10595 /* NEG commutes with ASHIFT since it is multiplication. Move the
10596 NEG outside to allow shifts to combine. */
10597 if (code == ASHIFT
10598 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10599 &complement_p))
10601 varop = XEXP (varop, 0);
10602 continue;
10604 break;
10606 case PLUS:
10607 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10608 is one less than the number of bits in the mode is
10609 equivalent to (xor A 1). */
10610 if (code == LSHIFTRT
10611 && count == (GET_MODE_PRECISION (result_mode) - 1)
10612 && XEXP (varop, 1) == constm1_rtx
10613 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10614 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10615 &complement_p))
10617 count = 0;
10618 varop = XEXP (varop, 0);
10619 continue;
10622 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10623 that might be nonzero in BAR are those being shifted out and those
10624 bits are known zero in FOO, we can replace the PLUS with FOO.
10625 Similarly in the other operand order. This code occurs when
10626 we are computing the size of a variable-size array. */
10628 if ((code == ASHIFTRT || code == LSHIFTRT)
10629 && count < HOST_BITS_PER_WIDE_INT
10630 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10631 && (nonzero_bits (XEXP (varop, 1), result_mode)
10632 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10634 varop = XEXP (varop, 0);
10635 continue;
10637 else if ((code == ASHIFTRT || code == LSHIFTRT)
10638 && count < HOST_BITS_PER_WIDE_INT
10639 && HWI_COMPUTABLE_MODE_P (result_mode)
10640 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10641 >> count)
10642 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10643 & nonzero_bits (XEXP (varop, 1),
10644 result_mode)))
10646 varop = XEXP (varop, 1);
10647 continue;
10650 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10651 if (code == ASHIFT
10652 && CONST_INT_P (XEXP (varop, 1))
10653 && (new_rtx = simplify_const_binary_operation
10654 (ASHIFT, result_mode,
10655 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10656 GEN_INT (count))) != 0
10657 && CONST_INT_P (new_rtx)
10658 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10659 INTVAL (new_rtx), result_mode, &complement_p))
10661 varop = XEXP (varop, 0);
10662 continue;
10665 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10666 signbit', and attempt to change the PLUS to an XOR and move it to
10667 the outer operation as is done above in the AND/IOR/XOR case
10668 leg for shift(logical). See details in logical handling above
10669 for reasoning in doing so. */
10670 if (code == LSHIFTRT
10671 && CONST_INT_P (XEXP (varop, 1))
10672 && mode_signbit_p (result_mode, XEXP (varop, 1))
10673 && (new_rtx = simplify_const_binary_operation
10674 (code, result_mode,
10675 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10676 GEN_INT (count))) != 0
10677 && CONST_INT_P (new_rtx)
10678 && merge_outer_ops (&outer_op, &outer_const, XOR,
10679 INTVAL (new_rtx), result_mode, &complement_p))
10681 varop = XEXP (varop, 0);
10682 continue;
10685 break;
10687 case MINUS:
10688 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10689 with C the size of VAROP - 1 and the shift is logical if
10690 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10691 we have a (gt X 0) operation. If the shift is arithmetic with
10692 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10693 we have a (neg (gt X 0)) operation. */
10695 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10696 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10697 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10698 && (code == LSHIFTRT || code == ASHIFTRT)
10699 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10700 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10701 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10703 count = 0;
10704 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10705 const0_rtx);
10707 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10708 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10710 continue;
10712 break;
10714 case TRUNCATE:
10715 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10716 if the truncate does not affect the value. */
10717 if (code == LSHIFTRT
10718 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10719 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10720 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10721 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10722 - GET_MODE_PRECISION (GET_MODE (varop)))))
10724 rtx varop_inner = XEXP (varop, 0);
10726 varop_inner
10727 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10728 XEXP (varop_inner, 0),
10729 GEN_INT
10730 (count + INTVAL (XEXP (varop_inner, 1))));
10731 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10732 count = 0;
10733 continue;
10735 break;
10737 default:
10738 break;
10741 break;
10744 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10745 outer_op, outer_const);
10747 /* We have now finished analyzing the shift. The result should be
10748 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10749 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10750 to the result of the shift. OUTER_CONST is the relevant constant,
10751 but we must turn off all bits turned off in the shift. */
10753 if (outer_op == UNKNOWN
10754 && orig_code == code && orig_count == count
10755 && varop == orig_varop
10756 && shift_mode == GET_MODE (varop))
10757 return NULL_RTX;
10759 /* Make a SUBREG if necessary. If we can't make it, fail. */
10760 varop = gen_lowpart (shift_mode, varop);
10761 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10762 return NULL_RTX;
10764 /* If we have an outer operation and we just made a shift, it is
10765 possible that we could have simplified the shift were it not
10766 for the outer operation. So try to do the simplification
10767 recursively. */
10769 if (outer_op != UNKNOWN)
10770 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10771 else
10772 x = NULL_RTX;
10774 if (x == NULL_RTX)
10775 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10777 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10778 turn off all the bits that the shift would have turned off. */
10779 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10780 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10781 GET_MODE_MASK (result_mode) >> orig_count);
10783 /* Do the remainder of the processing in RESULT_MODE. */
10784 x = gen_lowpart_or_truncate (result_mode, x);
10786 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10787 operation. */
10788 if (complement_p)
10789 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10791 if (outer_op != UNKNOWN)
10793 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10794 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10795 outer_const = trunc_int_for_mode (outer_const, result_mode);
10797 if (outer_op == AND)
10798 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10799 else if (outer_op == SET)
10801 /* This means that we have determined that the result is
10802 equivalent to a constant. This should be rare. */
10803 if (!side_effects_p (x))
10804 x = GEN_INT (outer_const);
10806 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10807 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10808 else
10809 x = simplify_gen_binary (outer_op, result_mode, x,
10810 GEN_INT (outer_const));
10813 return x;
10816 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10817 The result of the shift is RESULT_MODE. If we cannot simplify it,
10818 return X or, if it is NULL, synthesize the expression with
10819 simplify_gen_binary. Otherwise, return a simplified value.
10821 The shift is normally computed in the widest mode we find in VAROP, as
10822 long as it isn't a different number of words than RESULT_MODE. Exceptions
10823 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10825 static rtx
10826 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10827 rtx varop, int count)
10829 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10830 if (tem)
10831 return tem;
10833 if (!x)
10834 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10835 if (GET_MODE (x) != result_mode)
10836 x = gen_lowpart (result_mode, x);
10837 return x;
10841 /* Like recog, but we receive the address of a pointer to a new pattern.
10842 We try to match the rtx that the pointer points to.
10843 If that fails, we may try to modify or replace the pattern,
10844 storing the replacement into the same pointer object.
10846 Modifications include deletion or addition of CLOBBERs.
10848 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10849 the CLOBBERs are placed.
10851 The value is the final insn code from the pattern ultimately matched,
10852 or -1. */
10854 static int
10855 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10857 rtx pat = *pnewpat;
10858 rtx pat_without_clobbers;
10859 int insn_code_number;
10860 int num_clobbers_to_add = 0;
10861 int i;
10862 rtx notes = NULL_RTX;
10863 rtx old_notes, old_pat;
10864 int old_icode;
10866 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10867 we use to indicate that something didn't match. If we find such a
10868 thing, force rejection. */
10869 if (GET_CODE (pat) == PARALLEL)
10870 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10871 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10872 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10873 return -1;
10875 old_pat = PATTERN (insn);
10876 old_notes = REG_NOTES (insn);
10877 PATTERN (insn) = pat;
10878 REG_NOTES (insn) = NULL_RTX;
10880 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10881 if (dump_file && (dump_flags & TDF_DETAILS))
10883 if (insn_code_number < 0)
10884 fputs ("Failed to match this instruction:\n", dump_file);
10885 else
10886 fputs ("Successfully matched this instruction:\n", dump_file);
10887 print_rtl_single (dump_file, pat);
10890 /* If it isn't, there is the possibility that we previously had an insn
10891 that clobbered some register as a side effect, but the combined
10892 insn doesn't need to do that. So try once more without the clobbers
10893 unless this represents an ASM insn. */
10895 if (insn_code_number < 0 && ! check_asm_operands (pat)
10896 && GET_CODE (pat) == PARALLEL)
10898 int pos;
10900 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10901 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10903 if (i != pos)
10904 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10905 pos++;
10908 SUBST_INT (XVECLEN (pat, 0), pos);
10910 if (pos == 1)
10911 pat = XVECEXP (pat, 0, 0);
10913 PATTERN (insn) = pat;
10914 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10915 if (dump_file && (dump_flags & TDF_DETAILS))
10917 if (insn_code_number < 0)
10918 fputs ("Failed to match this instruction:\n", dump_file);
10919 else
10920 fputs ("Successfully matched this instruction:\n", dump_file);
10921 print_rtl_single (dump_file, pat);
10925 pat_without_clobbers = pat;
10927 PATTERN (insn) = old_pat;
10928 REG_NOTES (insn) = old_notes;
10930 /* Recognize all noop sets, these will be killed by followup pass. */
10931 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10932 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10934 /* If we had any clobbers to add, make a new pattern than contains
10935 them. Then check to make sure that all of them are dead. */
10936 if (num_clobbers_to_add)
10938 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10939 rtvec_alloc (GET_CODE (pat) == PARALLEL
10940 ? (XVECLEN (pat, 0)
10941 + num_clobbers_to_add)
10942 : num_clobbers_to_add + 1));
10944 if (GET_CODE (pat) == PARALLEL)
10945 for (i = 0; i < XVECLEN (pat, 0); i++)
10946 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10947 else
10948 XVECEXP (newpat, 0, 0) = pat;
10950 add_clobbers (newpat, insn_code_number);
10952 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10953 i < XVECLEN (newpat, 0); i++)
10955 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10956 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10957 return -1;
10958 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10960 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10961 notes = alloc_reg_note (REG_UNUSED,
10962 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10965 pat = newpat;
10968 if (insn_code_number >= 0
10969 && insn_code_number != NOOP_MOVE_INSN_CODE)
10971 old_pat = PATTERN (insn);
10972 old_notes = REG_NOTES (insn);
10973 old_icode = INSN_CODE (insn);
10974 PATTERN (insn) = pat;
10975 REG_NOTES (insn) = notes;
10977 /* Allow targets to reject combined insn. */
10978 if (!targetm.legitimate_combined_insn (insn))
10980 if (dump_file && (dump_flags & TDF_DETAILS))
10981 fputs ("Instruction not appropriate for target.",
10982 dump_file);
10984 /* Callers expect recog_for_combine to strip
10985 clobbers from the pattern on failure. */
10986 pat = pat_without_clobbers;
10987 notes = NULL_RTX;
10989 insn_code_number = -1;
10992 PATTERN (insn) = old_pat;
10993 REG_NOTES (insn) = old_notes;
10994 INSN_CODE (insn) = old_icode;
10997 *pnewpat = pat;
10998 *pnotes = notes;
11000 return insn_code_number;
11003 /* Like gen_lowpart_general but for use by combine. In combine it
11004 is not possible to create any new pseudoregs. However, it is
11005 safe to create invalid memory addresses, because combine will
11006 try to recognize them and all they will do is make the combine
11007 attempt fail.
11009 If for some reason this cannot do its job, an rtx
11010 (clobber (const_int 0)) is returned.
11011 An insn containing that will not be recognized. */
11013 static rtx
11014 gen_lowpart_for_combine (machine_mode omode, rtx x)
11016 machine_mode imode = GET_MODE (x);
11017 unsigned int osize = GET_MODE_SIZE (omode);
11018 unsigned int isize = GET_MODE_SIZE (imode);
11019 rtx result;
11021 if (omode == imode)
11022 return x;
11024 /* We can only support MODE being wider than a word if X is a
11025 constant integer or has a mode the same size. */
11026 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11027 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11028 goto fail;
11030 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11031 won't know what to do. So we will strip off the SUBREG here and
11032 process normally. */
11033 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11035 x = SUBREG_REG (x);
11037 /* For use in case we fall down into the address adjustments
11038 further below, we need to adjust the known mode and size of
11039 x; imode and isize, since we just adjusted x. */
11040 imode = GET_MODE (x);
11042 if (imode == omode)
11043 return x;
11045 isize = GET_MODE_SIZE (imode);
11048 result = gen_lowpart_common (omode, x);
11050 if (result)
11051 return result;
11053 if (MEM_P (x))
11055 int offset = 0;
11057 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11058 address. */
11059 if (MEM_VOLATILE_P (x)
11060 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11061 goto fail;
11063 /* If we want to refer to something bigger than the original memref,
11064 generate a paradoxical subreg instead. That will force a reload
11065 of the original memref X. */
11066 if (isize < osize)
11067 return gen_rtx_SUBREG (omode, x, 0);
11069 if (WORDS_BIG_ENDIAN)
11070 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11072 /* Adjust the address so that the address-after-the-data is
11073 unchanged. */
11074 if (BYTES_BIG_ENDIAN)
11075 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11077 return adjust_address_nv (x, omode, offset);
11080 /* If X is a comparison operator, rewrite it in a new mode. This
11081 probably won't match, but may allow further simplifications. */
11082 else if (COMPARISON_P (x))
11083 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11085 /* If we couldn't simplify X any other way, just enclose it in a
11086 SUBREG. Normally, this SUBREG won't match, but some patterns may
11087 include an explicit SUBREG or we may simplify it further in combine. */
11088 else
11090 int offset = 0;
11091 rtx res;
11093 offset = subreg_lowpart_offset (omode, imode);
11094 if (imode == VOIDmode)
11096 imode = int_mode_for_mode (omode);
11097 x = gen_lowpart_common (imode, x);
11098 if (x == NULL)
11099 goto fail;
11101 res = simplify_gen_subreg (omode, x, imode, offset);
11102 if (res)
11103 return res;
11106 fail:
11107 return gen_rtx_CLOBBER (omode, const0_rtx);
11110 /* Try to simplify a comparison between OP0 and a constant OP1,
11111 where CODE is the comparison code that will be tested, into a
11112 (CODE OP0 const0_rtx) form.
11114 The result is a possibly different comparison code to use.
11115 *POP1 may be updated. */
11117 static enum rtx_code
11118 simplify_compare_const (enum rtx_code code, machine_mode mode,
11119 rtx op0, rtx *pop1)
11121 unsigned int mode_width = GET_MODE_PRECISION (mode);
11122 HOST_WIDE_INT const_op = INTVAL (*pop1);
11124 /* Get the constant we are comparing against and turn off all bits
11125 not on in our mode. */
11126 if (mode != VOIDmode)
11127 const_op = trunc_int_for_mode (const_op, mode);
11129 /* If we are comparing against a constant power of two and the value
11130 being compared can only have that single bit nonzero (e.g., it was
11131 `and'ed with that bit), we can replace this with a comparison
11132 with zero. */
11133 if (const_op
11134 && (code == EQ || code == NE || code == GE || code == GEU
11135 || code == LT || code == LTU)
11136 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11137 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11138 && (nonzero_bits (op0, mode)
11139 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11141 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11142 const_op = 0;
11145 /* Similarly, if we are comparing a value known to be either -1 or
11146 0 with -1, change it to the opposite comparison against zero. */
11147 if (const_op == -1
11148 && (code == EQ || code == NE || code == GT || code == LE
11149 || code == GEU || code == LTU)
11150 && num_sign_bit_copies (op0, mode) == mode_width)
11152 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11153 const_op = 0;
11156 /* Do some canonicalizations based on the comparison code. We prefer
11157 comparisons against zero and then prefer equality comparisons.
11158 If we can reduce the size of a constant, we will do that too. */
11159 switch (code)
11161 case LT:
11162 /* < C is equivalent to <= (C - 1) */
11163 if (const_op > 0)
11165 const_op -= 1;
11166 code = LE;
11167 /* ... fall through to LE case below. */
11169 else
11170 break;
11172 case LE:
11173 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11174 if (const_op < 0)
11176 const_op += 1;
11177 code = LT;
11180 /* If we are doing a <= 0 comparison on a value known to have
11181 a zero sign bit, we can replace this with == 0. */
11182 else if (const_op == 0
11183 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11184 && (nonzero_bits (op0, mode)
11185 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11186 == 0)
11187 code = EQ;
11188 break;
11190 case GE:
11191 /* >= C is equivalent to > (C - 1). */
11192 if (const_op > 0)
11194 const_op -= 1;
11195 code = GT;
11196 /* ... fall through to GT below. */
11198 else
11199 break;
11201 case GT:
11202 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11203 if (const_op < 0)
11205 const_op += 1;
11206 code = GE;
11209 /* If we are doing a > 0 comparison on a value known to have
11210 a zero sign bit, we can replace this with != 0. */
11211 else if (const_op == 0
11212 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11213 && (nonzero_bits (op0, mode)
11214 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11215 == 0)
11216 code = NE;
11217 break;
11219 case LTU:
11220 /* < C is equivalent to <= (C - 1). */
11221 if (const_op > 0)
11223 const_op -= 1;
11224 code = LEU;
11225 /* ... fall through ... */
11227 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11228 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11229 && (unsigned HOST_WIDE_INT) const_op
11230 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11232 const_op = 0;
11233 code = GE;
11234 break;
11236 else
11237 break;
11239 case LEU:
11240 /* unsigned <= 0 is equivalent to == 0 */
11241 if (const_op == 0)
11242 code = EQ;
11243 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11244 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11245 && (unsigned HOST_WIDE_INT) const_op
11246 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11248 const_op = 0;
11249 code = GE;
11251 break;
11253 case GEU:
11254 /* >= C is equivalent to > (C - 1). */
11255 if (const_op > 1)
11257 const_op -= 1;
11258 code = GTU;
11259 /* ... fall through ... */
11262 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11263 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11264 && (unsigned HOST_WIDE_INT) const_op
11265 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11267 const_op = 0;
11268 code = LT;
11269 break;
11271 else
11272 break;
11274 case GTU:
11275 /* unsigned > 0 is equivalent to != 0 */
11276 if (const_op == 0)
11277 code = NE;
11278 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11279 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11280 && (unsigned HOST_WIDE_INT) const_op
11281 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11283 const_op = 0;
11284 code = LT;
11286 break;
11288 default:
11289 break;
11292 *pop1 = GEN_INT (const_op);
11293 return code;
11296 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11297 comparison code that will be tested.
11299 The result is a possibly different comparison code to use. *POP0 and
11300 *POP1 may be updated.
11302 It is possible that we might detect that a comparison is either always
11303 true or always false. However, we do not perform general constant
11304 folding in combine, so this knowledge isn't useful. Such tautologies
11305 should have been detected earlier. Hence we ignore all such cases. */
11307 static enum rtx_code
11308 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11310 rtx op0 = *pop0;
11311 rtx op1 = *pop1;
11312 rtx tem, tem1;
11313 int i;
11314 machine_mode mode, tmode;
11316 /* Try a few ways of applying the same transformation to both operands. */
11317 while (1)
11319 #ifndef WORD_REGISTER_OPERATIONS
11320 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11321 so check specially. */
11322 if (code != GTU && code != GEU && code != LTU && code != LEU
11323 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11324 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11325 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11326 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11327 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11328 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11329 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11330 && CONST_INT_P (XEXP (op0, 1))
11331 && XEXP (op0, 1) == XEXP (op1, 1)
11332 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11333 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11334 && (INTVAL (XEXP (op0, 1))
11335 == (GET_MODE_PRECISION (GET_MODE (op0))
11336 - (GET_MODE_PRECISION
11337 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11339 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11340 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11342 #endif
11344 /* If both operands are the same constant shift, see if we can ignore the
11345 shift. We can if the shift is a rotate or if the bits shifted out of
11346 this shift are known to be zero for both inputs and if the type of
11347 comparison is compatible with the shift. */
11348 if (GET_CODE (op0) == GET_CODE (op1)
11349 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11350 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11351 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11352 && (code != GT && code != LT && code != GE && code != LE))
11353 || (GET_CODE (op0) == ASHIFTRT
11354 && (code != GTU && code != LTU
11355 && code != GEU && code != LEU)))
11356 && CONST_INT_P (XEXP (op0, 1))
11357 && INTVAL (XEXP (op0, 1)) >= 0
11358 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11359 && XEXP (op0, 1) == XEXP (op1, 1))
11361 machine_mode mode = GET_MODE (op0);
11362 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11363 int shift_count = INTVAL (XEXP (op0, 1));
11365 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11366 mask &= (mask >> shift_count) << shift_count;
11367 else if (GET_CODE (op0) == ASHIFT)
11368 mask = (mask & (mask << shift_count)) >> shift_count;
11370 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11371 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11372 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11373 else
11374 break;
11377 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11378 SUBREGs are of the same mode, and, in both cases, the AND would
11379 be redundant if the comparison was done in the narrower mode,
11380 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11381 and the operand's possibly nonzero bits are 0xffffff01; in that case
11382 if we only care about QImode, we don't need the AND). This case
11383 occurs if the output mode of an scc insn is not SImode and
11384 STORE_FLAG_VALUE == 1 (e.g., the 386).
11386 Similarly, check for a case where the AND's are ZERO_EXTEND
11387 operations from some narrower mode even though a SUBREG is not
11388 present. */
11390 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11391 && CONST_INT_P (XEXP (op0, 1))
11392 && CONST_INT_P (XEXP (op1, 1)))
11394 rtx inner_op0 = XEXP (op0, 0);
11395 rtx inner_op1 = XEXP (op1, 0);
11396 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11397 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11398 int changed = 0;
11400 if (paradoxical_subreg_p (inner_op0)
11401 && GET_CODE (inner_op1) == SUBREG
11402 && (GET_MODE (SUBREG_REG (inner_op0))
11403 == GET_MODE (SUBREG_REG (inner_op1)))
11404 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11405 <= HOST_BITS_PER_WIDE_INT)
11406 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11407 GET_MODE (SUBREG_REG (inner_op0)))))
11408 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11409 GET_MODE (SUBREG_REG (inner_op1))))))
11411 op0 = SUBREG_REG (inner_op0);
11412 op1 = SUBREG_REG (inner_op1);
11414 /* The resulting comparison is always unsigned since we masked
11415 off the original sign bit. */
11416 code = unsigned_condition (code);
11418 changed = 1;
11421 else if (c0 == c1)
11422 for (tmode = GET_CLASS_NARROWEST_MODE
11423 (GET_MODE_CLASS (GET_MODE (op0)));
11424 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11425 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11427 op0 = gen_lowpart (tmode, inner_op0);
11428 op1 = gen_lowpart (tmode, inner_op1);
11429 code = unsigned_condition (code);
11430 changed = 1;
11431 break;
11434 if (! changed)
11435 break;
11438 /* If both operands are NOT, we can strip off the outer operation
11439 and adjust the comparison code for swapped operands; similarly for
11440 NEG, except that this must be an equality comparison. */
11441 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11442 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11443 && (code == EQ || code == NE)))
11444 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11446 else
11447 break;
11450 /* If the first operand is a constant, swap the operands and adjust the
11451 comparison code appropriately, but don't do this if the second operand
11452 is already a constant integer. */
11453 if (swap_commutative_operands_p (op0, op1))
11455 tem = op0, op0 = op1, op1 = tem;
11456 code = swap_condition (code);
11459 /* We now enter a loop during which we will try to simplify the comparison.
11460 For the most part, we only are concerned with comparisons with zero,
11461 but some things may really be comparisons with zero but not start
11462 out looking that way. */
11464 while (CONST_INT_P (op1))
11466 machine_mode mode = GET_MODE (op0);
11467 unsigned int mode_width = GET_MODE_PRECISION (mode);
11468 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11469 int equality_comparison_p;
11470 int sign_bit_comparison_p;
11471 int unsigned_comparison_p;
11472 HOST_WIDE_INT const_op;
11474 /* We only want to handle integral modes. This catches VOIDmode,
11475 CCmode, and the floating-point modes. An exception is that we
11476 can handle VOIDmode if OP0 is a COMPARE or a comparison
11477 operation. */
11479 if (GET_MODE_CLASS (mode) != MODE_INT
11480 && ! (mode == VOIDmode
11481 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11482 break;
11484 /* Try to simplify the compare to constant, possibly changing the
11485 comparison op, and/or changing op1 to zero. */
11486 code = simplify_compare_const (code, mode, op0, &op1);
11487 const_op = INTVAL (op1);
11489 /* Compute some predicates to simplify code below. */
11491 equality_comparison_p = (code == EQ || code == NE);
11492 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11493 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11494 || code == GEU);
11496 /* If this is a sign bit comparison and we can do arithmetic in
11497 MODE, say that we will only be needing the sign bit of OP0. */
11498 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11499 op0 = force_to_mode (op0, mode,
11500 (unsigned HOST_WIDE_INT) 1
11501 << (GET_MODE_PRECISION (mode) - 1),
11504 /* Now try cases based on the opcode of OP0. If none of the cases
11505 does a "continue", we exit this loop immediately after the
11506 switch. */
11508 switch (GET_CODE (op0))
11510 case ZERO_EXTRACT:
11511 /* If we are extracting a single bit from a variable position in
11512 a constant that has only a single bit set and are comparing it
11513 with zero, we can convert this into an equality comparison
11514 between the position and the location of the single bit. */
11515 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11516 have already reduced the shift count modulo the word size. */
11517 if (!SHIFT_COUNT_TRUNCATED
11518 && CONST_INT_P (XEXP (op0, 0))
11519 && XEXP (op0, 1) == const1_rtx
11520 && equality_comparison_p && const_op == 0
11521 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11523 if (BITS_BIG_ENDIAN)
11524 i = BITS_PER_WORD - 1 - i;
11526 op0 = XEXP (op0, 2);
11527 op1 = GEN_INT (i);
11528 const_op = i;
11530 /* Result is nonzero iff shift count is equal to I. */
11531 code = reverse_condition (code);
11532 continue;
11535 /* ... fall through ... */
11537 case SIGN_EXTRACT:
11538 tem = expand_compound_operation (op0);
11539 if (tem != op0)
11541 op0 = tem;
11542 continue;
11544 break;
11546 case NOT:
11547 /* If testing for equality, we can take the NOT of the constant. */
11548 if (equality_comparison_p
11549 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11551 op0 = XEXP (op0, 0);
11552 op1 = tem;
11553 continue;
11556 /* If just looking at the sign bit, reverse the sense of the
11557 comparison. */
11558 if (sign_bit_comparison_p)
11560 op0 = XEXP (op0, 0);
11561 code = (code == GE ? LT : GE);
11562 continue;
11564 break;
11566 case NEG:
11567 /* If testing for equality, we can take the NEG of the constant. */
11568 if (equality_comparison_p
11569 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11571 op0 = XEXP (op0, 0);
11572 op1 = tem;
11573 continue;
11576 /* The remaining cases only apply to comparisons with zero. */
11577 if (const_op != 0)
11578 break;
11580 /* When X is ABS or is known positive,
11581 (neg X) is < 0 if and only if X != 0. */
11583 if (sign_bit_comparison_p
11584 && (GET_CODE (XEXP (op0, 0)) == ABS
11585 || (mode_width <= HOST_BITS_PER_WIDE_INT
11586 && (nonzero_bits (XEXP (op0, 0), mode)
11587 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11588 == 0)))
11590 op0 = XEXP (op0, 0);
11591 code = (code == LT ? NE : EQ);
11592 continue;
11595 /* If we have NEG of something whose two high-order bits are the
11596 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11597 if (num_sign_bit_copies (op0, mode) >= 2)
11599 op0 = XEXP (op0, 0);
11600 code = swap_condition (code);
11601 continue;
11603 break;
11605 case ROTATE:
11606 /* If we are testing equality and our count is a constant, we
11607 can perform the inverse operation on our RHS. */
11608 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11609 && (tem = simplify_binary_operation (ROTATERT, mode,
11610 op1, XEXP (op0, 1))) != 0)
11612 op0 = XEXP (op0, 0);
11613 op1 = tem;
11614 continue;
11617 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11618 a particular bit. Convert it to an AND of a constant of that
11619 bit. This will be converted into a ZERO_EXTRACT. */
11620 if (const_op == 0 && sign_bit_comparison_p
11621 && CONST_INT_P (XEXP (op0, 1))
11622 && mode_width <= HOST_BITS_PER_WIDE_INT)
11624 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11625 ((unsigned HOST_WIDE_INT) 1
11626 << (mode_width - 1
11627 - INTVAL (XEXP (op0, 1)))));
11628 code = (code == LT ? NE : EQ);
11629 continue;
11632 /* Fall through. */
11634 case ABS:
11635 /* ABS is ignorable inside an equality comparison with zero. */
11636 if (const_op == 0 && equality_comparison_p)
11638 op0 = XEXP (op0, 0);
11639 continue;
11641 break;
11643 case SIGN_EXTEND:
11644 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11645 (compare FOO CONST) if CONST fits in FOO's mode and we
11646 are either testing inequality or have an unsigned
11647 comparison with ZERO_EXTEND or a signed comparison with
11648 SIGN_EXTEND. But don't do it if we don't have a compare
11649 insn of the given mode, since we'd have to revert it
11650 later on, and then we wouldn't know whether to sign- or
11651 zero-extend. */
11652 mode = GET_MODE (XEXP (op0, 0));
11653 if (GET_MODE_CLASS (mode) == MODE_INT
11654 && ! unsigned_comparison_p
11655 && HWI_COMPUTABLE_MODE_P (mode)
11656 && trunc_int_for_mode (const_op, mode) == const_op
11657 && have_insn_for (COMPARE, mode))
11659 op0 = XEXP (op0, 0);
11660 continue;
11662 break;
11664 case SUBREG:
11665 /* Check for the case where we are comparing A - C1 with C2, that is
11667 (subreg:MODE (plus (A) (-C1))) op (C2)
11669 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11670 comparison in the wider mode. One of the following two conditions
11671 must be true in order for this to be valid:
11673 1. The mode extension results in the same bit pattern being added
11674 on both sides and the comparison is equality or unsigned. As
11675 C2 has been truncated to fit in MODE, the pattern can only be
11676 all 0s or all 1s.
11678 2. The mode extension results in the sign bit being copied on
11679 each side.
11681 The difficulty here is that we have predicates for A but not for
11682 (A - C1) so we need to check that C1 is within proper bounds so
11683 as to perturbate A as little as possible. */
11685 if (mode_width <= HOST_BITS_PER_WIDE_INT
11686 && subreg_lowpart_p (op0)
11687 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11688 && GET_CODE (SUBREG_REG (op0)) == PLUS
11689 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11691 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11692 rtx a = XEXP (SUBREG_REG (op0), 0);
11693 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11695 if ((c1 > 0
11696 && (unsigned HOST_WIDE_INT) c1
11697 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11698 && (equality_comparison_p || unsigned_comparison_p)
11699 /* (A - C1) zero-extends if it is positive and sign-extends
11700 if it is negative, C2 both zero- and sign-extends. */
11701 && ((0 == (nonzero_bits (a, inner_mode)
11702 & ~GET_MODE_MASK (mode))
11703 && const_op >= 0)
11704 /* (A - C1) sign-extends if it is positive and 1-extends
11705 if it is negative, C2 both sign- and 1-extends. */
11706 || (num_sign_bit_copies (a, inner_mode)
11707 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11708 - mode_width)
11709 && const_op < 0)))
11710 || ((unsigned HOST_WIDE_INT) c1
11711 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11712 /* (A - C1) always sign-extends, like C2. */
11713 && num_sign_bit_copies (a, inner_mode)
11714 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11715 - (mode_width - 1))))
11717 op0 = SUBREG_REG (op0);
11718 continue;
11722 /* If the inner mode is narrower and we are extracting the low part,
11723 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11724 if (subreg_lowpart_p (op0)
11725 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11726 /* Fall through */ ;
11727 else
11728 break;
11730 /* ... fall through ... */
11732 case ZERO_EXTEND:
11733 mode = GET_MODE (XEXP (op0, 0));
11734 if (GET_MODE_CLASS (mode) == MODE_INT
11735 && (unsigned_comparison_p || equality_comparison_p)
11736 && HWI_COMPUTABLE_MODE_P (mode)
11737 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11738 && const_op >= 0
11739 && have_insn_for (COMPARE, mode))
11741 op0 = XEXP (op0, 0);
11742 continue;
11744 break;
11746 case PLUS:
11747 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11748 this for equality comparisons due to pathological cases involving
11749 overflows. */
11750 if (equality_comparison_p
11751 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11752 op1, XEXP (op0, 1))))
11754 op0 = XEXP (op0, 0);
11755 op1 = tem;
11756 continue;
11759 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11760 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11761 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11763 op0 = XEXP (XEXP (op0, 0), 0);
11764 code = (code == LT ? EQ : NE);
11765 continue;
11767 break;
11769 case MINUS:
11770 /* We used to optimize signed comparisons against zero, but that
11771 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11772 arrive here as equality comparisons, or (GEU, LTU) are
11773 optimized away. No need to special-case them. */
11775 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11776 (eq B (minus A C)), whichever simplifies. We can only do
11777 this for equality comparisons due to pathological cases involving
11778 overflows. */
11779 if (equality_comparison_p
11780 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11781 XEXP (op0, 1), op1)))
11783 op0 = XEXP (op0, 0);
11784 op1 = tem;
11785 continue;
11788 if (equality_comparison_p
11789 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11790 XEXP (op0, 0), op1)))
11792 op0 = XEXP (op0, 1);
11793 op1 = tem;
11794 continue;
11797 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11798 of bits in X minus 1, is one iff X > 0. */
11799 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11800 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11801 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11802 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11804 op0 = XEXP (op0, 1);
11805 code = (code == GE ? LE : GT);
11806 continue;
11808 break;
11810 case XOR:
11811 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11812 if C is zero or B is a constant. */
11813 if (equality_comparison_p
11814 && 0 != (tem = simplify_binary_operation (XOR, mode,
11815 XEXP (op0, 1), op1)))
11817 op0 = XEXP (op0, 0);
11818 op1 = tem;
11819 continue;
11821 break;
11823 case EQ: case NE:
11824 case UNEQ: case LTGT:
11825 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11826 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11827 case UNORDERED: case ORDERED:
11828 /* We can't do anything if OP0 is a condition code value, rather
11829 than an actual data value. */
11830 if (const_op != 0
11831 || CC0_P (XEXP (op0, 0))
11832 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11833 break;
11835 /* Get the two operands being compared. */
11836 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11837 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11838 else
11839 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11841 /* Check for the cases where we simply want the result of the
11842 earlier test or the opposite of that result. */
11843 if (code == NE || code == EQ
11844 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11845 && (code == LT || code == GE)))
11847 enum rtx_code new_code;
11848 if (code == LT || code == NE)
11849 new_code = GET_CODE (op0);
11850 else
11851 new_code = reversed_comparison_code (op0, NULL);
11853 if (new_code != UNKNOWN)
11855 code = new_code;
11856 op0 = tem;
11857 op1 = tem1;
11858 continue;
11861 break;
11863 case IOR:
11864 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11865 iff X <= 0. */
11866 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11867 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11868 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11870 op0 = XEXP (op0, 1);
11871 code = (code == GE ? GT : LE);
11872 continue;
11874 break;
11876 case AND:
11877 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11878 will be converted to a ZERO_EXTRACT later. */
11879 if (const_op == 0 && equality_comparison_p
11880 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11881 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11883 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11884 XEXP (XEXP (op0, 0), 1));
11885 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11886 continue;
11889 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11890 zero and X is a comparison and C1 and C2 describe only bits set
11891 in STORE_FLAG_VALUE, we can compare with X. */
11892 if (const_op == 0 && equality_comparison_p
11893 && mode_width <= HOST_BITS_PER_WIDE_INT
11894 && CONST_INT_P (XEXP (op0, 1))
11895 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11896 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11897 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11898 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11900 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11901 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11902 if ((~STORE_FLAG_VALUE & mask) == 0
11903 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11904 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11905 && COMPARISON_P (tem))))
11907 op0 = XEXP (XEXP (op0, 0), 0);
11908 continue;
11912 /* If we are doing an equality comparison of an AND of a bit equal
11913 to the sign bit, replace this with a LT or GE comparison of
11914 the underlying value. */
11915 if (equality_comparison_p
11916 && const_op == 0
11917 && CONST_INT_P (XEXP (op0, 1))
11918 && mode_width <= HOST_BITS_PER_WIDE_INT
11919 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11920 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11922 op0 = XEXP (op0, 0);
11923 code = (code == EQ ? GE : LT);
11924 continue;
11927 /* If this AND operation is really a ZERO_EXTEND from a narrower
11928 mode, the constant fits within that mode, and this is either an
11929 equality or unsigned comparison, try to do this comparison in
11930 the narrower mode.
11932 Note that in:
11934 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11935 -> (ne:DI (reg:SI 4) (const_int 0))
11937 unless TRULY_NOOP_TRUNCATION allows it or the register is
11938 known to hold a value of the required mode the
11939 transformation is invalid. */
11940 if ((equality_comparison_p || unsigned_comparison_p)
11941 && CONST_INT_P (XEXP (op0, 1))
11942 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11943 & GET_MODE_MASK (mode))
11944 + 1)) >= 0
11945 && const_op >> i == 0
11946 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11947 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11948 || (REG_P (XEXP (op0, 0))
11949 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11951 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11952 continue;
11955 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11956 fits in both M1 and M2 and the SUBREG is either paradoxical
11957 or represents the low part, permute the SUBREG and the AND
11958 and try again. */
11959 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11961 unsigned HOST_WIDE_INT c1;
11962 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11963 /* Require an integral mode, to avoid creating something like
11964 (AND:SF ...). */
11965 if (SCALAR_INT_MODE_P (tmode)
11966 /* It is unsafe to commute the AND into the SUBREG if the
11967 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11968 not defined. As originally written the upper bits
11969 have a defined value due to the AND operation.
11970 However, if we commute the AND inside the SUBREG then
11971 they no longer have defined values and the meaning of
11972 the code has been changed. */
11973 && (0
11974 #ifdef WORD_REGISTER_OPERATIONS
11975 || (mode_width > GET_MODE_PRECISION (tmode)
11976 && mode_width <= BITS_PER_WORD)
11977 #endif
11978 || (mode_width <= GET_MODE_PRECISION (tmode)
11979 && subreg_lowpart_p (XEXP (op0, 0))))
11980 && CONST_INT_P (XEXP (op0, 1))
11981 && mode_width <= HOST_BITS_PER_WIDE_INT
11982 && HWI_COMPUTABLE_MODE_P (tmode)
11983 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11984 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11985 && c1 != mask
11986 && c1 != GET_MODE_MASK (tmode))
11988 op0 = simplify_gen_binary (AND, tmode,
11989 SUBREG_REG (XEXP (op0, 0)),
11990 gen_int_mode (c1, tmode));
11991 op0 = gen_lowpart (mode, op0);
11992 continue;
11996 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11997 if (const_op == 0 && equality_comparison_p
11998 && XEXP (op0, 1) == const1_rtx
11999 && GET_CODE (XEXP (op0, 0)) == NOT)
12001 op0 = simplify_and_const_int (NULL_RTX, mode,
12002 XEXP (XEXP (op0, 0), 0), 1);
12003 code = (code == NE ? EQ : NE);
12004 continue;
12007 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12008 (eq (and (lshiftrt X) 1) 0).
12009 Also handle the case where (not X) is expressed using xor. */
12010 if (const_op == 0 && equality_comparison_p
12011 && XEXP (op0, 1) == const1_rtx
12012 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12014 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12015 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12017 if (GET_CODE (shift_op) == NOT
12018 || (GET_CODE (shift_op) == XOR
12019 && CONST_INT_P (XEXP (shift_op, 1))
12020 && CONST_INT_P (shift_count)
12021 && HWI_COMPUTABLE_MODE_P (mode)
12022 && (UINTVAL (XEXP (shift_op, 1))
12023 == (unsigned HOST_WIDE_INT) 1
12024 << INTVAL (shift_count))))
12027 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12028 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12029 code = (code == NE ? EQ : NE);
12030 continue;
12033 break;
12035 case ASHIFT:
12036 /* If we have (compare (ashift FOO N) (const_int C)) and
12037 the high order N bits of FOO (N+1 if an inequality comparison)
12038 are known to be zero, we can do this by comparing FOO with C
12039 shifted right N bits so long as the low-order N bits of C are
12040 zero. */
12041 if (CONST_INT_P (XEXP (op0, 1))
12042 && INTVAL (XEXP (op0, 1)) >= 0
12043 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12044 < HOST_BITS_PER_WIDE_INT)
12045 && (((unsigned HOST_WIDE_INT) const_op
12046 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
12047 - 1)) == 0)
12048 && mode_width <= HOST_BITS_PER_WIDE_INT
12049 && (nonzero_bits (XEXP (op0, 0), mode)
12050 & ~(mask >> (INTVAL (XEXP (op0, 1))
12051 + ! equality_comparison_p))) == 0)
12053 /* We must perform a logical shift, not an arithmetic one,
12054 as we want the top N bits of C to be zero. */
12055 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12057 temp >>= INTVAL (XEXP (op0, 1));
12058 op1 = gen_int_mode (temp, mode);
12059 op0 = XEXP (op0, 0);
12060 continue;
12063 /* If we are doing a sign bit comparison, it means we are testing
12064 a particular bit. Convert it to the appropriate AND. */
12065 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12066 && mode_width <= HOST_BITS_PER_WIDE_INT)
12068 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12069 ((unsigned HOST_WIDE_INT) 1
12070 << (mode_width - 1
12071 - INTVAL (XEXP (op0, 1)))));
12072 code = (code == LT ? NE : EQ);
12073 continue;
12076 /* If this an equality comparison with zero and we are shifting
12077 the low bit to the sign bit, we can convert this to an AND of the
12078 low-order bit. */
12079 if (const_op == 0 && equality_comparison_p
12080 && CONST_INT_P (XEXP (op0, 1))
12081 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12083 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12084 continue;
12086 break;
12088 case ASHIFTRT:
12089 /* If this is an equality comparison with zero, we can do this
12090 as a logical shift, which might be much simpler. */
12091 if (equality_comparison_p && const_op == 0
12092 && CONST_INT_P (XEXP (op0, 1)))
12094 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12095 XEXP (op0, 0),
12096 INTVAL (XEXP (op0, 1)));
12097 continue;
12100 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12101 do the comparison in a narrower mode. */
12102 if (! unsigned_comparison_p
12103 && CONST_INT_P (XEXP (op0, 1))
12104 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12105 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12106 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12107 MODE_INT, 1)) != BLKmode
12108 && (((unsigned HOST_WIDE_INT) const_op
12109 + (GET_MODE_MASK (tmode) >> 1) + 1)
12110 <= GET_MODE_MASK (tmode)))
12112 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12113 continue;
12116 /* Likewise if OP0 is a PLUS of a sign extension with a
12117 constant, which is usually represented with the PLUS
12118 between the shifts. */
12119 if (! unsigned_comparison_p
12120 && CONST_INT_P (XEXP (op0, 1))
12121 && GET_CODE (XEXP (op0, 0)) == PLUS
12122 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12123 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12124 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12125 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12126 MODE_INT, 1)) != BLKmode
12127 && (((unsigned HOST_WIDE_INT) const_op
12128 + (GET_MODE_MASK (tmode) >> 1) + 1)
12129 <= GET_MODE_MASK (tmode)))
12131 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12132 rtx add_const = XEXP (XEXP (op0, 0), 1);
12133 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12134 add_const, XEXP (op0, 1));
12136 op0 = simplify_gen_binary (PLUS, tmode,
12137 gen_lowpart (tmode, inner),
12138 new_const);
12139 continue;
12142 /* ... fall through ... */
12143 case LSHIFTRT:
12144 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12145 the low order N bits of FOO are known to be zero, we can do this
12146 by comparing FOO with C shifted left N bits so long as no
12147 overflow occurs. Even if the low order N bits of FOO aren't known
12148 to be zero, if the comparison is >= or < we can use the same
12149 optimization and for > or <= by setting all the low
12150 order N bits in the comparison constant. */
12151 if (CONST_INT_P (XEXP (op0, 1))
12152 && INTVAL (XEXP (op0, 1)) > 0
12153 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12154 && mode_width <= HOST_BITS_PER_WIDE_INT
12155 && (((unsigned HOST_WIDE_INT) const_op
12156 + (GET_CODE (op0) != LSHIFTRT
12157 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12158 + 1)
12159 : 0))
12160 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12162 unsigned HOST_WIDE_INT low_bits
12163 = (nonzero_bits (XEXP (op0, 0), mode)
12164 & (((unsigned HOST_WIDE_INT) 1
12165 << INTVAL (XEXP (op0, 1))) - 1));
12166 if (low_bits == 0 || !equality_comparison_p)
12168 /* If the shift was logical, then we must make the condition
12169 unsigned. */
12170 if (GET_CODE (op0) == LSHIFTRT)
12171 code = unsigned_condition (code);
12173 const_op <<= INTVAL (XEXP (op0, 1));
12174 if (low_bits != 0
12175 && (code == GT || code == GTU
12176 || code == LE || code == LEU))
12177 const_op
12178 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12179 op1 = GEN_INT (const_op);
12180 op0 = XEXP (op0, 0);
12181 continue;
12185 /* If we are using this shift to extract just the sign bit, we
12186 can replace this with an LT or GE comparison. */
12187 if (const_op == 0
12188 && (equality_comparison_p || sign_bit_comparison_p)
12189 && CONST_INT_P (XEXP (op0, 1))
12190 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12192 op0 = XEXP (op0, 0);
12193 code = (code == NE || code == GT ? LT : GE);
12194 continue;
12196 break;
12198 default:
12199 break;
12202 break;
12205 /* Now make any compound operations involved in this comparison. Then,
12206 check for an outmost SUBREG on OP0 that is not doing anything or is
12207 paradoxical. The latter transformation must only be performed when
12208 it is known that the "extra" bits will be the same in op0 and op1 or
12209 that they don't matter. There are three cases to consider:
12211 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12212 care bits and we can assume they have any convenient value. So
12213 making the transformation is safe.
12215 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12216 In this case the upper bits of op0 are undefined. We should not make
12217 the simplification in that case as we do not know the contents of
12218 those bits.
12220 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12221 UNKNOWN. In that case we know those bits are zeros or ones. We must
12222 also be sure that they are the same as the upper bits of op1.
12224 We can never remove a SUBREG for a non-equality comparison because
12225 the sign bit is in a different place in the underlying object. */
12227 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12228 op1 = make_compound_operation (op1, SET);
12230 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12231 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12232 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12233 && (code == NE || code == EQ))
12235 if (paradoxical_subreg_p (op0))
12237 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12238 implemented. */
12239 if (REG_P (SUBREG_REG (op0)))
12241 op0 = SUBREG_REG (op0);
12242 op1 = gen_lowpart (GET_MODE (op0), op1);
12245 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12246 <= HOST_BITS_PER_WIDE_INT)
12247 && (nonzero_bits (SUBREG_REG (op0),
12248 GET_MODE (SUBREG_REG (op0)))
12249 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12251 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12253 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12254 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12255 op0 = SUBREG_REG (op0), op1 = tem;
12259 /* We now do the opposite procedure: Some machines don't have compare
12260 insns in all modes. If OP0's mode is an integer mode smaller than a
12261 word and we can't do a compare in that mode, see if there is a larger
12262 mode for which we can do the compare. There are a number of cases in
12263 which we can use the wider mode. */
12265 mode = GET_MODE (op0);
12266 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12267 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12268 && ! have_insn_for (COMPARE, mode))
12269 for (tmode = GET_MODE_WIDER_MODE (mode);
12270 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12271 tmode = GET_MODE_WIDER_MODE (tmode))
12272 if (have_insn_for (COMPARE, tmode))
12274 int zero_extended;
12276 /* If this is a test for negative, we can make an explicit
12277 test of the sign bit. Test this first so we can use
12278 a paradoxical subreg to extend OP0. */
12280 if (op1 == const0_rtx && (code == LT || code == GE)
12281 && HWI_COMPUTABLE_MODE_P (mode))
12283 unsigned HOST_WIDE_INT sign
12284 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12285 op0 = simplify_gen_binary (AND, tmode,
12286 gen_lowpart (tmode, op0),
12287 gen_int_mode (sign, tmode));
12288 code = (code == LT) ? NE : EQ;
12289 break;
12292 /* If the only nonzero bits in OP0 and OP1 are those in the
12293 narrower mode and this is an equality or unsigned comparison,
12294 we can use the wider mode. Similarly for sign-extended
12295 values, in which case it is true for all comparisons. */
12296 zero_extended = ((code == EQ || code == NE
12297 || code == GEU || code == GTU
12298 || code == LEU || code == LTU)
12299 && (nonzero_bits (op0, tmode)
12300 & ~GET_MODE_MASK (mode)) == 0
12301 && ((CONST_INT_P (op1)
12302 || (nonzero_bits (op1, tmode)
12303 & ~GET_MODE_MASK (mode)) == 0)));
12305 if (zero_extended
12306 || ((num_sign_bit_copies (op0, tmode)
12307 > (unsigned int) (GET_MODE_PRECISION (tmode)
12308 - GET_MODE_PRECISION (mode)))
12309 && (num_sign_bit_copies (op1, tmode)
12310 > (unsigned int) (GET_MODE_PRECISION (tmode)
12311 - GET_MODE_PRECISION (mode)))))
12313 /* If OP0 is an AND and we don't have an AND in MODE either,
12314 make a new AND in the proper mode. */
12315 if (GET_CODE (op0) == AND
12316 && !have_insn_for (AND, mode))
12317 op0 = simplify_gen_binary (AND, tmode,
12318 gen_lowpart (tmode,
12319 XEXP (op0, 0)),
12320 gen_lowpart (tmode,
12321 XEXP (op0, 1)));
12322 else
12324 if (zero_extended)
12326 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12327 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12329 else
12331 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12332 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12334 break;
12339 /* We may have changed the comparison operands. Re-canonicalize. */
12340 if (swap_commutative_operands_p (op0, op1))
12342 tem = op0, op0 = op1, op1 = tem;
12343 code = swap_condition (code);
12346 /* If this machine only supports a subset of valid comparisons, see if we
12347 can convert an unsupported one into a supported one. */
12348 target_canonicalize_comparison (&code, &op0, &op1, 0);
12350 *pop0 = op0;
12351 *pop1 = op1;
12353 return code;
12356 /* Utility function for record_value_for_reg. Count number of
12357 rtxs in X. */
12358 static int
12359 count_rtxs (rtx x)
12361 enum rtx_code code = GET_CODE (x);
12362 const char *fmt;
12363 int i, j, ret = 1;
12365 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12366 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12368 rtx x0 = XEXP (x, 0);
12369 rtx x1 = XEXP (x, 1);
12371 if (x0 == x1)
12372 return 1 + 2 * count_rtxs (x0);
12374 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12375 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12376 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12377 return 2 + 2 * count_rtxs (x0)
12378 + count_rtxs (x == XEXP (x1, 0)
12379 ? XEXP (x1, 1) : XEXP (x1, 0));
12381 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12382 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12383 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12384 return 2 + 2 * count_rtxs (x1)
12385 + count_rtxs (x == XEXP (x0, 0)
12386 ? XEXP (x0, 1) : XEXP (x0, 0));
12389 fmt = GET_RTX_FORMAT (code);
12390 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12391 if (fmt[i] == 'e')
12392 ret += count_rtxs (XEXP (x, i));
12393 else if (fmt[i] == 'E')
12394 for (j = 0; j < XVECLEN (x, i); j++)
12395 ret += count_rtxs (XVECEXP (x, i, j));
12397 return ret;
12400 /* Utility function for following routine. Called when X is part of a value
12401 being stored into last_set_value. Sets last_set_table_tick
12402 for each register mentioned. Similar to mention_regs in cse.c */
12404 static void
12405 update_table_tick (rtx x)
12407 enum rtx_code code = GET_CODE (x);
12408 const char *fmt = GET_RTX_FORMAT (code);
12409 int i, j;
12411 if (code == REG)
12413 unsigned int regno = REGNO (x);
12414 unsigned int endregno = END_REGNO (x);
12415 unsigned int r;
12417 for (r = regno; r < endregno; r++)
12419 reg_stat_type *rsp = &reg_stat[r];
12420 rsp->last_set_table_tick = label_tick;
12423 return;
12426 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12427 if (fmt[i] == 'e')
12429 /* Check for identical subexpressions. If x contains
12430 identical subexpression we only have to traverse one of
12431 them. */
12432 if (i == 0 && ARITHMETIC_P (x))
12434 /* Note that at this point x1 has already been
12435 processed. */
12436 rtx x0 = XEXP (x, 0);
12437 rtx x1 = XEXP (x, 1);
12439 /* If x0 and x1 are identical then there is no need to
12440 process x0. */
12441 if (x0 == x1)
12442 break;
12444 /* If x0 is identical to a subexpression of x1 then while
12445 processing x1, x0 has already been processed. Thus we
12446 are done with x. */
12447 if (ARITHMETIC_P (x1)
12448 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12449 break;
12451 /* If x1 is identical to a subexpression of x0 then we
12452 still have to process the rest of x0. */
12453 if (ARITHMETIC_P (x0)
12454 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12456 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12457 break;
12461 update_table_tick (XEXP (x, i));
12463 else if (fmt[i] == 'E')
12464 for (j = 0; j < XVECLEN (x, i); j++)
12465 update_table_tick (XVECEXP (x, i, j));
12468 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12469 are saying that the register is clobbered and we no longer know its
12470 value. If INSN is zero, don't update reg_stat[].last_set; this is
12471 only permitted with VALUE also zero and is used to invalidate the
12472 register. */
12474 static void
12475 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12477 unsigned int regno = REGNO (reg);
12478 unsigned int endregno = END_REGNO (reg);
12479 unsigned int i;
12480 reg_stat_type *rsp;
12482 /* If VALUE contains REG and we have a previous value for REG, substitute
12483 the previous value. */
12484 if (value && insn && reg_overlap_mentioned_p (reg, value))
12486 rtx tem;
12488 /* Set things up so get_last_value is allowed to see anything set up to
12489 our insn. */
12490 subst_low_luid = DF_INSN_LUID (insn);
12491 tem = get_last_value (reg);
12493 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12494 it isn't going to be useful and will take a lot of time to process,
12495 so just use the CLOBBER. */
12497 if (tem)
12499 if (ARITHMETIC_P (tem)
12500 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12501 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12502 tem = XEXP (tem, 0);
12503 else if (count_occurrences (value, reg, 1) >= 2)
12505 /* If there are two or more occurrences of REG in VALUE,
12506 prevent the value from growing too much. */
12507 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12508 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12511 value = replace_rtx (copy_rtx (value), reg, tem);
12515 /* For each register modified, show we don't know its value, that
12516 we don't know about its bitwise content, that its value has been
12517 updated, and that we don't know the location of the death of the
12518 register. */
12519 for (i = regno; i < endregno; i++)
12521 rsp = &reg_stat[i];
12523 if (insn)
12524 rsp->last_set = insn;
12526 rsp->last_set_value = 0;
12527 rsp->last_set_mode = VOIDmode;
12528 rsp->last_set_nonzero_bits = 0;
12529 rsp->last_set_sign_bit_copies = 0;
12530 rsp->last_death = 0;
12531 rsp->truncated_to_mode = VOIDmode;
12534 /* Mark registers that are being referenced in this value. */
12535 if (value)
12536 update_table_tick (value);
12538 /* Now update the status of each register being set.
12539 If someone is using this register in this block, set this register
12540 to invalid since we will get confused between the two lives in this
12541 basic block. This makes using this register always invalid. In cse, we
12542 scan the table to invalidate all entries using this register, but this
12543 is too much work for us. */
12545 for (i = regno; i < endregno; i++)
12547 rsp = &reg_stat[i];
12548 rsp->last_set_label = label_tick;
12549 if (!insn
12550 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12551 rsp->last_set_invalid = 1;
12552 else
12553 rsp->last_set_invalid = 0;
12556 /* The value being assigned might refer to X (like in "x++;"). In that
12557 case, we must replace it with (clobber (const_int 0)) to prevent
12558 infinite loops. */
12559 rsp = &reg_stat[regno];
12560 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12562 value = copy_rtx (value);
12563 if (!get_last_value_validate (&value, insn, label_tick, 1))
12564 value = 0;
12567 /* For the main register being modified, update the value, the mode, the
12568 nonzero bits, and the number of sign bit copies. */
12570 rsp->last_set_value = value;
12572 if (value)
12574 machine_mode mode = GET_MODE (reg);
12575 subst_low_luid = DF_INSN_LUID (insn);
12576 rsp->last_set_mode = mode;
12577 if (GET_MODE_CLASS (mode) == MODE_INT
12578 && HWI_COMPUTABLE_MODE_P (mode))
12579 mode = nonzero_bits_mode;
12580 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12581 rsp->last_set_sign_bit_copies
12582 = num_sign_bit_copies (value, GET_MODE (reg));
12586 /* Called via note_stores from record_dead_and_set_regs to handle one
12587 SET or CLOBBER in an insn. DATA is the instruction in which the
12588 set is occurring. */
12590 static void
12591 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12593 rtx_insn *record_dead_insn = (rtx_insn *) data;
12595 if (GET_CODE (dest) == SUBREG)
12596 dest = SUBREG_REG (dest);
12598 if (!record_dead_insn)
12600 if (REG_P (dest))
12601 record_value_for_reg (dest, NULL, NULL_RTX);
12602 return;
12605 if (REG_P (dest))
12607 /* If we are setting the whole register, we know its value. Otherwise
12608 show that we don't know the value. We can handle SUBREG in
12609 some cases. */
12610 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12611 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12612 else if (GET_CODE (setter) == SET
12613 && GET_CODE (SET_DEST (setter)) == SUBREG
12614 && SUBREG_REG (SET_DEST (setter)) == dest
12615 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12616 && subreg_lowpart_p (SET_DEST (setter)))
12617 record_value_for_reg (dest, record_dead_insn,
12618 gen_lowpart (GET_MODE (dest),
12619 SET_SRC (setter)));
12620 else
12621 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12623 else if (MEM_P (dest)
12624 /* Ignore pushes, they clobber nothing. */
12625 && ! push_operand (dest, GET_MODE (dest)))
12626 mem_last_set = DF_INSN_LUID (record_dead_insn);
12629 /* Update the records of when each REG was most recently set or killed
12630 for the things done by INSN. This is the last thing done in processing
12631 INSN in the combiner loop.
12633 We update reg_stat[], in particular fields last_set, last_set_value,
12634 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12635 last_death, and also the similar information mem_last_set (which insn
12636 most recently modified memory) and last_call_luid (which insn was the
12637 most recent subroutine call). */
12639 static void
12640 record_dead_and_set_regs (rtx_insn *insn)
12642 rtx link;
12643 unsigned int i;
12645 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12647 if (REG_NOTE_KIND (link) == REG_DEAD
12648 && REG_P (XEXP (link, 0)))
12650 unsigned int regno = REGNO (XEXP (link, 0));
12651 unsigned int endregno = END_REGNO (XEXP (link, 0));
12653 for (i = regno; i < endregno; i++)
12655 reg_stat_type *rsp;
12657 rsp = &reg_stat[i];
12658 rsp->last_death = insn;
12661 else if (REG_NOTE_KIND (link) == REG_INC)
12662 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12665 if (CALL_P (insn))
12667 hard_reg_set_iterator hrsi;
12668 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12670 reg_stat_type *rsp;
12672 rsp = &reg_stat[i];
12673 rsp->last_set_invalid = 1;
12674 rsp->last_set = insn;
12675 rsp->last_set_value = 0;
12676 rsp->last_set_mode = VOIDmode;
12677 rsp->last_set_nonzero_bits = 0;
12678 rsp->last_set_sign_bit_copies = 0;
12679 rsp->last_death = 0;
12680 rsp->truncated_to_mode = VOIDmode;
12683 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12685 /* We can't combine into a call pattern. Remember, though, that
12686 the return value register is set at this LUID. We could
12687 still replace a register with the return value from the
12688 wrong subroutine call! */
12689 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12691 else
12692 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12695 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12696 register present in the SUBREG, so for each such SUBREG go back and
12697 adjust nonzero and sign bit information of the registers that are
12698 known to have some zero/sign bits set.
12700 This is needed because when combine blows the SUBREGs away, the
12701 information on zero/sign bits is lost and further combines can be
12702 missed because of that. */
12704 static void
12705 record_promoted_value (rtx_insn *insn, rtx subreg)
12707 struct insn_link *links;
12708 rtx set;
12709 unsigned int regno = REGNO (SUBREG_REG (subreg));
12710 machine_mode mode = GET_MODE (subreg);
12712 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12713 return;
12715 for (links = LOG_LINKS (insn); links;)
12717 reg_stat_type *rsp;
12719 insn = links->insn;
12720 set = single_set (insn);
12722 if (! set || !REG_P (SET_DEST (set))
12723 || REGNO (SET_DEST (set)) != regno
12724 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12726 links = links->next;
12727 continue;
12730 rsp = &reg_stat[regno];
12731 if (rsp->last_set == insn)
12733 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12734 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12737 if (REG_P (SET_SRC (set)))
12739 regno = REGNO (SET_SRC (set));
12740 links = LOG_LINKS (insn);
12742 else
12743 break;
12747 /* Check if X, a register, is known to contain a value already
12748 truncated to MODE. In this case we can use a subreg to refer to
12749 the truncated value even though in the generic case we would need
12750 an explicit truncation. */
12752 static bool
12753 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12755 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12756 machine_mode truncated = rsp->truncated_to_mode;
12758 if (truncated == 0
12759 || rsp->truncation_label < label_tick_ebb_start)
12760 return false;
12761 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12762 return true;
12763 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12764 return true;
12765 return false;
12768 /* If X is a hard reg or a subreg record the mode that the register is
12769 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12770 to turn a truncate into a subreg using this information. Return true
12771 if traversing X is complete. */
12773 static bool
12774 record_truncated_value (rtx x)
12776 machine_mode truncated_mode;
12777 reg_stat_type *rsp;
12779 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12781 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12782 truncated_mode = GET_MODE (x);
12784 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12785 return true;
12787 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12788 return true;
12790 x = SUBREG_REG (x);
12792 /* ??? For hard-regs we now record everything. We might be able to
12793 optimize this using last_set_mode. */
12794 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12795 truncated_mode = GET_MODE (x);
12796 else
12797 return false;
12799 rsp = &reg_stat[REGNO (x)];
12800 if (rsp->truncated_to_mode == 0
12801 || rsp->truncation_label < label_tick_ebb_start
12802 || (GET_MODE_SIZE (truncated_mode)
12803 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12805 rsp->truncated_to_mode = truncated_mode;
12806 rsp->truncation_label = label_tick;
12809 return true;
12812 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12813 the modes they are used in. This can help truning TRUNCATEs into
12814 SUBREGs. */
12816 static void
12817 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12819 subrtx_var_iterator::array_type array;
12820 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12821 if (record_truncated_value (*iter))
12822 iter.skip_subrtxes ();
12825 /* Scan X for promoted SUBREGs. For each one found,
12826 note what it implies to the registers used in it. */
12828 static void
12829 check_promoted_subreg (rtx_insn *insn, rtx x)
12831 if (GET_CODE (x) == SUBREG
12832 && SUBREG_PROMOTED_VAR_P (x)
12833 && REG_P (SUBREG_REG (x)))
12834 record_promoted_value (insn, x);
12835 else
12837 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12838 int i, j;
12840 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12841 switch (format[i])
12843 case 'e':
12844 check_promoted_subreg (insn, XEXP (x, i));
12845 break;
12846 case 'V':
12847 case 'E':
12848 if (XVEC (x, i) != 0)
12849 for (j = 0; j < XVECLEN (x, i); j++)
12850 check_promoted_subreg (insn, XVECEXP (x, i, j));
12851 break;
12856 /* Verify that all the registers and memory references mentioned in *LOC are
12857 still valid. *LOC was part of a value set in INSN when label_tick was
12858 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12859 the invalid references with (clobber (const_int 0)) and return 1. This
12860 replacement is useful because we often can get useful information about
12861 the form of a value (e.g., if it was produced by a shift that always
12862 produces -1 or 0) even though we don't know exactly what registers it
12863 was produced from. */
12865 static int
12866 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12868 rtx x = *loc;
12869 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12870 int len = GET_RTX_LENGTH (GET_CODE (x));
12871 int i, j;
12873 if (REG_P (x))
12875 unsigned int regno = REGNO (x);
12876 unsigned int endregno = END_REGNO (x);
12877 unsigned int j;
12879 for (j = regno; j < endregno; j++)
12881 reg_stat_type *rsp = &reg_stat[j];
12882 if (rsp->last_set_invalid
12883 /* If this is a pseudo-register that was only set once and not
12884 live at the beginning of the function, it is always valid. */
12885 || (! (regno >= FIRST_PSEUDO_REGISTER
12886 && regno < reg_n_sets_max
12887 && REG_N_SETS (regno) == 1
12888 && (!REGNO_REG_SET_P
12889 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12890 regno)))
12891 && rsp->last_set_label > tick))
12893 if (replace)
12894 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12895 return replace;
12899 return 1;
12901 /* If this is a memory reference, make sure that there were no stores after
12902 it that might have clobbered the value. We don't have alias info, so we
12903 assume any store invalidates it. Moreover, we only have local UIDs, so
12904 we also assume that there were stores in the intervening basic blocks. */
12905 else if (MEM_P (x) && !MEM_READONLY_P (x)
12906 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12908 if (replace)
12909 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12910 return replace;
12913 for (i = 0; i < len; i++)
12915 if (fmt[i] == 'e')
12917 /* Check for identical subexpressions. If x contains
12918 identical subexpression we only have to traverse one of
12919 them. */
12920 if (i == 1 && ARITHMETIC_P (x))
12922 /* Note that at this point x0 has already been checked
12923 and found valid. */
12924 rtx x0 = XEXP (x, 0);
12925 rtx x1 = XEXP (x, 1);
12927 /* If x0 and x1 are identical then x is also valid. */
12928 if (x0 == x1)
12929 return 1;
12931 /* If x1 is identical to a subexpression of x0 then
12932 while checking x0, x1 has already been checked. Thus
12933 it is valid and so as x. */
12934 if (ARITHMETIC_P (x0)
12935 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12936 return 1;
12938 /* If x0 is identical to a subexpression of x1 then x is
12939 valid iff the rest of x1 is valid. */
12940 if (ARITHMETIC_P (x1)
12941 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12942 return
12943 get_last_value_validate (&XEXP (x1,
12944 x0 == XEXP (x1, 0) ? 1 : 0),
12945 insn, tick, replace);
12948 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12949 replace) == 0)
12950 return 0;
12952 else if (fmt[i] == 'E')
12953 for (j = 0; j < XVECLEN (x, i); j++)
12954 if (get_last_value_validate (&XVECEXP (x, i, j),
12955 insn, tick, replace) == 0)
12956 return 0;
12959 /* If we haven't found a reason for it to be invalid, it is valid. */
12960 return 1;
12963 /* Get the last value assigned to X, if known. Some registers
12964 in the value may be replaced with (clobber (const_int 0)) if their value
12965 is known longer known reliably. */
12967 static rtx
12968 get_last_value (const_rtx x)
12970 unsigned int regno;
12971 rtx value;
12972 reg_stat_type *rsp;
12974 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12975 then convert it to the desired mode. If this is a paradoxical SUBREG,
12976 we cannot predict what values the "extra" bits might have. */
12977 if (GET_CODE (x) == SUBREG
12978 && subreg_lowpart_p (x)
12979 && !paradoxical_subreg_p (x)
12980 && (value = get_last_value (SUBREG_REG (x))) != 0)
12981 return gen_lowpart (GET_MODE (x), value);
12983 if (!REG_P (x))
12984 return 0;
12986 regno = REGNO (x);
12987 rsp = &reg_stat[regno];
12988 value = rsp->last_set_value;
12990 /* If we don't have a value, or if it isn't for this basic block and
12991 it's either a hard register, set more than once, or it's a live
12992 at the beginning of the function, return 0.
12994 Because if it's not live at the beginning of the function then the reg
12995 is always set before being used (is never used without being set).
12996 And, if it's set only once, and it's always set before use, then all
12997 uses must have the same last value, even if it's not from this basic
12998 block. */
13000 if (value == 0
13001 || (rsp->last_set_label < label_tick_ebb_start
13002 && (regno < FIRST_PSEUDO_REGISTER
13003 || regno >= reg_n_sets_max
13004 || REG_N_SETS (regno) != 1
13005 || REGNO_REG_SET_P
13006 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13007 return 0;
13009 /* If the value was set in a later insn than the ones we are processing,
13010 we can't use it even if the register was only set once. */
13011 if (rsp->last_set_label == label_tick
13012 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13013 return 0;
13015 /* If the value has all its registers valid, return it. */
13016 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13017 return value;
13019 /* Otherwise, make a copy and replace any invalid register with
13020 (clobber (const_int 0)). If that fails for some reason, return 0. */
13022 value = copy_rtx (value);
13023 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13024 return value;
13026 return 0;
13029 /* Return nonzero if expression X refers to a REG or to memory
13030 that is set in an instruction more recent than FROM_LUID. */
13032 static int
13033 use_crosses_set_p (const_rtx x, int from_luid)
13035 const char *fmt;
13036 int i;
13037 enum rtx_code code = GET_CODE (x);
13039 if (code == REG)
13041 unsigned int regno = REGNO (x);
13042 unsigned endreg = END_REGNO (x);
13044 #ifdef PUSH_ROUNDING
13045 /* Don't allow uses of the stack pointer to be moved,
13046 because we don't know whether the move crosses a push insn. */
13047 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13048 return 1;
13049 #endif
13050 for (; regno < endreg; regno++)
13052 reg_stat_type *rsp = &reg_stat[regno];
13053 if (rsp->last_set
13054 && rsp->last_set_label == label_tick
13055 && DF_INSN_LUID (rsp->last_set) > from_luid)
13056 return 1;
13058 return 0;
13061 if (code == MEM && mem_last_set > from_luid)
13062 return 1;
13064 fmt = GET_RTX_FORMAT (code);
13066 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13068 if (fmt[i] == 'E')
13070 int j;
13071 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13072 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13073 return 1;
13075 else if (fmt[i] == 'e'
13076 && use_crosses_set_p (XEXP (x, i), from_luid))
13077 return 1;
13079 return 0;
13082 /* Define three variables used for communication between the following
13083 routines. */
13085 static unsigned int reg_dead_regno, reg_dead_endregno;
13086 static int reg_dead_flag;
13088 /* Function called via note_stores from reg_dead_at_p.
13090 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13091 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13093 static void
13094 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13096 unsigned int regno, endregno;
13098 if (!REG_P (dest))
13099 return;
13101 regno = REGNO (dest);
13102 endregno = END_REGNO (dest);
13103 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13104 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13107 /* Return nonzero if REG is known to be dead at INSN.
13109 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13110 referencing REG, it is dead. If we hit a SET referencing REG, it is
13111 live. Otherwise, see if it is live or dead at the start of the basic
13112 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13113 must be assumed to be always live. */
13115 static int
13116 reg_dead_at_p (rtx reg, rtx_insn *insn)
13118 basic_block block;
13119 unsigned int i;
13121 /* Set variables for reg_dead_at_p_1. */
13122 reg_dead_regno = REGNO (reg);
13123 reg_dead_endregno = END_REGNO (reg);
13125 reg_dead_flag = 0;
13127 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13128 we allow the machine description to decide whether use-and-clobber
13129 patterns are OK. */
13130 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13132 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13133 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13134 return 0;
13137 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13138 beginning of basic block. */
13139 block = BLOCK_FOR_INSN (insn);
13140 for (;;)
13142 if (INSN_P (insn))
13144 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13145 return 1;
13147 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13148 if (reg_dead_flag)
13149 return reg_dead_flag == 1 ? 1 : 0;
13151 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13152 return 1;
13155 if (insn == BB_HEAD (block))
13156 break;
13158 insn = PREV_INSN (insn);
13161 /* Look at live-in sets for the basic block that we were in. */
13162 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13163 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13164 return 0;
13166 return 1;
13169 /* Note hard registers in X that are used. */
13171 static void
13172 mark_used_regs_combine (rtx x)
13174 RTX_CODE code = GET_CODE (x);
13175 unsigned int regno;
13176 int i;
13178 switch (code)
13180 case LABEL_REF:
13181 case SYMBOL_REF:
13182 case CONST:
13183 CASE_CONST_ANY:
13184 case PC:
13185 case ADDR_VEC:
13186 case ADDR_DIFF_VEC:
13187 case ASM_INPUT:
13188 #ifdef HAVE_cc0
13189 /* CC0 must die in the insn after it is set, so we don't need to take
13190 special note of it here. */
13191 case CC0:
13192 #endif
13193 return;
13195 case CLOBBER:
13196 /* If we are clobbering a MEM, mark any hard registers inside the
13197 address as used. */
13198 if (MEM_P (XEXP (x, 0)))
13199 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13200 return;
13202 case REG:
13203 regno = REGNO (x);
13204 /* A hard reg in a wide mode may really be multiple registers.
13205 If so, mark all of them just like the first. */
13206 if (regno < FIRST_PSEUDO_REGISTER)
13208 /* None of this applies to the stack, frame or arg pointers. */
13209 if (regno == STACK_POINTER_REGNUM
13210 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13211 || regno == HARD_FRAME_POINTER_REGNUM
13212 #endif
13213 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13214 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13215 #endif
13216 || regno == FRAME_POINTER_REGNUM)
13217 return;
13219 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13221 return;
13223 case SET:
13225 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13226 the address. */
13227 rtx testreg = SET_DEST (x);
13229 while (GET_CODE (testreg) == SUBREG
13230 || GET_CODE (testreg) == ZERO_EXTRACT
13231 || GET_CODE (testreg) == STRICT_LOW_PART)
13232 testreg = XEXP (testreg, 0);
13234 if (MEM_P (testreg))
13235 mark_used_regs_combine (XEXP (testreg, 0));
13237 mark_used_regs_combine (SET_SRC (x));
13239 return;
13241 default:
13242 break;
13245 /* Recursively scan the operands of this expression. */
13248 const char *fmt = GET_RTX_FORMAT (code);
13250 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13252 if (fmt[i] == 'e')
13253 mark_used_regs_combine (XEXP (x, i));
13254 else if (fmt[i] == 'E')
13256 int j;
13258 for (j = 0; j < XVECLEN (x, i); j++)
13259 mark_used_regs_combine (XVECEXP (x, i, j));
13265 /* Remove register number REGNO from the dead registers list of INSN.
13267 Return the note used to record the death, if there was one. */
13270 remove_death (unsigned int regno, rtx_insn *insn)
13272 rtx note = find_regno_note (insn, REG_DEAD, regno);
13274 if (note)
13275 remove_note (insn, note);
13277 return note;
13280 /* For each register (hardware or pseudo) used within expression X, if its
13281 death is in an instruction with luid between FROM_LUID (inclusive) and
13282 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13283 list headed by PNOTES.
13285 That said, don't move registers killed by maybe_kill_insn.
13287 This is done when X is being merged by combination into TO_INSN. These
13288 notes will then be distributed as needed. */
13290 static void
13291 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13292 rtx *pnotes)
13294 const char *fmt;
13295 int len, i;
13296 enum rtx_code code = GET_CODE (x);
13298 if (code == REG)
13300 unsigned int regno = REGNO (x);
13301 rtx_insn *where_dead = reg_stat[regno].last_death;
13303 /* Don't move the register if it gets killed in between from and to. */
13304 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13305 && ! reg_referenced_p (x, maybe_kill_insn))
13306 return;
13308 if (where_dead
13309 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13310 && DF_INSN_LUID (where_dead) >= from_luid
13311 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13313 rtx note = remove_death (regno, where_dead);
13315 /* It is possible for the call above to return 0. This can occur
13316 when last_death points to I2 or I1 that we combined with.
13317 In that case make a new note.
13319 We must also check for the case where X is a hard register
13320 and NOTE is a death note for a range of hard registers
13321 including X. In that case, we must put REG_DEAD notes for
13322 the remaining registers in place of NOTE. */
13324 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13325 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13326 > GET_MODE_SIZE (GET_MODE (x))))
13328 unsigned int deadregno = REGNO (XEXP (note, 0));
13329 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13330 unsigned int ourend = END_HARD_REGNO (x);
13331 unsigned int i;
13333 for (i = deadregno; i < deadend; i++)
13334 if (i < regno || i >= ourend)
13335 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13338 /* If we didn't find any note, or if we found a REG_DEAD note that
13339 covers only part of the given reg, and we have a multi-reg hard
13340 register, then to be safe we must check for REG_DEAD notes
13341 for each register other than the first. They could have
13342 their own REG_DEAD notes lying around. */
13343 else if ((note == 0
13344 || (note != 0
13345 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13346 < GET_MODE_SIZE (GET_MODE (x)))))
13347 && regno < FIRST_PSEUDO_REGISTER
13348 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13350 unsigned int ourend = END_HARD_REGNO (x);
13351 unsigned int i, offset;
13352 rtx oldnotes = 0;
13354 if (note)
13355 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13356 else
13357 offset = 1;
13359 for (i = regno + offset; i < ourend; i++)
13360 move_deaths (regno_reg_rtx[i],
13361 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13364 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13366 XEXP (note, 1) = *pnotes;
13367 *pnotes = note;
13369 else
13370 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13373 return;
13376 else if (GET_CODE (x) == SET)
13378 rtx dest = SET_DEST (x);
13380 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13382 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13383 that accesses one word of a multi-word item, some
13384 piece of everything register in the expression is used by
13385 this insn, so remove any old death. */
13386 /* ??? So why do we test for equality of the sizes? */
13388 if (GET_CODE (dest) == ZERO_EXTRACT
13389 || GET_CODE (dest) == STRICT_LOW_PART
13390 || (GET_CODE (dest) == SUBREG
13391 && (((GET_MODE_SIZE (GET_MODE (dest))
13392 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13393 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13394 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13396 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13397 return;
13400 /* If this is some other SUBREG, we know it replaces the entire
13401 value, so use that as the destination. */
13402 if (GET_CODE (dest) == SUBREG)
13403 dest = SUBREG_REG (dest);
13405 /* If this is a MEM, adjust deaths of anything used in the address.
13406 For a REG (the only other possibility), the entire value is
13407 being replaced so the old value is not used in this insn. */
13409 if (MEM_P (dest))
13410 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13411 to_insn, pnotes);
13412 return;
13415 else if (GET_CODE (x) == CLOBBER)
13416 return;
13418 len = GET_RTX_LENGTH (code);
13419 fmt = GET_RTX_FORMAT (code);
13421 for (i = 0; i < len; i++)
13423 if (fmt[i] == 'E')
13425 int j;
13426 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13427 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13428 to_insn, pnotes);
13430 else if (fmt[i] == 'e')
13431 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13435 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13436 pattern of an insn. X must be a REG. */
13438 static int
13439 reg_bitfield_target_p (rtx x, rtx body)
13441 int i;
13443 if (GET_CODE (body) == SET)
13445 rtx dest = SET_DEST (body);
13446 rtx target;
13447 unsigned int regno, tregno, endregno, endtregno;
13449 if (GET_CODE (dest) == ZERO_EXTRACT)
13450 target = XEXP (dest, 0);
13451 else if (GET_CODE (dest) == STRICT_LOW_PART)
13452 target = SUBREG_REG (XEXP (dest, 0));
13453 else
13454 return 0;
13456 if (GET_CODE (target) == SUBREG)
13457 target = SUBREG_REG (target);
13459 if (!REG_P (target))
13460 return 0;
13462 tregno = REGNO (target), regno = REGNO (x);
13463 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13464 return target == x;
13466 endtregno = end_hard_regno (GET_MODE (target), tregno);
13467 endregno = end_hard_regno (GET_MODE (x), regno);
13469 return endregno > tregno && regno < endtregno;
13472 else if (GET_CODE (body) == PARALLEL)
13473 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13474 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13475 return 1;
13477 return 0;
13480 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13481 as appropriate. I3 and I2 are the insns resulting from the combination
13482 insns including FROM (I2 may be zero).
13484 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13485 not need REG_DEAD notes because they are being substituted for. This
13486 saves searching in the most common cases.
13488 Each note in the list is either ignored or placed on some insns, depending
13489 on the type of note. */
13491 static void
13492 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13493 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13495 rtx note, next_note;
13496 rtx tem_note;
13497 rtx_insn *tem_insn;
13499 for (note = notes; note; note = next_note)
13501 rtx_insn *place = 0, *place2 = 0;
13503 next_note = XEXP (note, 1);
13504 switch (REG_NOTE_KIND (note))
13506 case REG_BR_PROB:
13507 case REG_BR_PRED:
13508 /* Doesn't matter much where we put this, as long as it's somewhere.
13509 It is preferable to keep these notes on branches, which is most
13510 likely to be i3. */
13511 place = i3;
13512 break;
13514 case REG_NON_LOCAL_GOTO:
13515 if (JUMP_P (i3))
13516 place = i3;
13517 else
13519 gcc_assert (i2 && JUMP_P (i2));
13520 place = i2;
13522 break;
13524 case REG_EH_REGION:
13525 /* These notes must remain with the call or trapping instruction. */
13526 if (CALL_P (i3))
13527 place = i3;
13528 else if (i2 && CALL_P (i2))
13529 place = i2;
13530 else
13532 gcc_assert (cfun->can_throw_non_call_exceptions);
13533 if (may_trap_p (i3))
13534 place = i3;
13535 else if (i2 && may_trap_p (i2))
13536 place = i2;
13537 /* ??? Otherwise assume we've combined things such that we
13538 can now prove that the instructions can't trap. Drop the
13539 note in this case. */
13541 break;
13543 case REG_ARGS_SIZE:
13544 /* ??? How to distribute between i3-i1. Assume i3 contains the
13545 entire adjustment. Assert i3 contains at least some adjust. */
13546 if (!noop_move_p (i3))
13548 int old_size, args_size = INTVAL (XEXP (note, 0));
13549 /* fixup_args_size_notes looks at REG_NORETURN note,
13550 so ensure the note is placed there first. */
13551 if (CALL_P (i3))
13553 rtx *np;
13554 for (np = &next_note; *np; np = &XEXP (*np, 1))
13555 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13557 rtx n = *np;
13558 *np = XEXP (n, 1);
13559 XEXP (n, 1) = REG_NOTES (i3);
13560 REG_NOTES (i3) = n;
13561 break;
13564 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13565 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13566 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13567 gcc_assert (old_size != args_size
13568 || (CALL_P (i3)
13569 && !ACCUMULATE_OUTGOING_ARGS
13570 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13572 break;
13574 case REG_NORETURN:
13575 case REG_SETJMP:
13576 case REG_TM:
13577 case REG_CALL_DECL:
13578 /* These notes must remain with the call. It should not be
13579 possible for both I2 and I3 to be a call. */
13580 if (CALL_P (i3))
13581 place = i3;
13582 else
13584 gcc_assert (i2 && CALL_P (i2));
13585 place = i2;
13587 break;
13589 case REG_UNUSED:
13590 /* Any clobbers for i3 may still exist, and so we must process
13591 REG_UNUSED notes from that insn.
13593 Any clobbers from i2 or i1 can only exist if they were added by
13594 recog_for_combine. In that case, recog_for_combine created the
13595 necessary REG_UNUSED notes. Trying to keep any original
13596 REG_UNUSED notes from these insns can cause incorrect output
13597 if it is for the same register as the original i3 dest.
13598 In that case, we will notice that the register is set in i3,
13599 and then add a REG_UNUSED note for the destination of i3, which
13600 is wrong. However, it is possible to have REG_UNUSED notes from
13601 i2 or i1 for register which were both used and clobbered, so
13602 we keep notes from i2 or i1 if they will turn into REG_DEAD
13603 notes. */
13605 /* If this register is set or clobbered in I3, put the note there
13606 unless there is one already. */
13607 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13609 if (from_insn != i3)
13610 break;
13612 if (! (REG_P (XEXP (note, 0))
13613 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13614 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13615 place = i3;
13617 /* Otherwise, if this register is used by I3, then this register
13618 now dies here, so we must put a REG_DEAD note here unless there
13619 is one already. */
13620 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13621 && ! (REG_P (XEXP (note, 0))
13622 ? find_regno_note (i3, REG_DEAD,
13623 REGNO (XEXP (note, 0)))
13624 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13626 PUT_REG_NOTE_KIND (note, REG_DEAD);
13627 place = i3;
13629 break;
13631 case REG_EQUAL:
13632 case REG_EQUIV:
13633 case REG_NOALIAS:
13634 /* These notes say something about results of an insn. We can
13635 only support them if they used to be on I3 in which case they
13636 remain on I3. Otherwise they are ignored.
13638 If the note refers to an expression that is not a constant, we
13639 must also ignore the note since we cannot tell whether the
13640 equivalence is still true. It might be possible to do
13641 slightly better than this (we only have a problem if I2DEST
13642 or I1DEST is present in the expression), but it doesn't
13643 seem worth the trouble. */
13645 if (from_insn == i3
13646 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13647 place = i3;
13648 break;
13650 case REG_INC:
13651 /* These notes say something about how a register is used. They must
13652 be present on any use of the register in I2 or I3. */
13653 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13654 place = i3;
13656 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13658 if (place)
13659 place2 = i2;
13660 else
13661 place = i2;
13663 break;
13665 case REG_LABEL_TARGET:
13666 case REG_LABEL_OPERAND:
13667 /* This can show up in several ways -- either directly in the
13668 pattern, or hidden off in the constant pool with (or without?)
13669 a REG_EQUAL note. */
13670 /* ??? Ignore the without-reg_equal-note problem for now. */
13671 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13672 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13673 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13674 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13675 place = i3;
13677 if (i2
13678 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13679 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13680 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13681 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13683 if (place)
13684 place2 = i2;
13685 else
13686 place = i2;
13689 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13690 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13691 there. */
13692 if (place && JUMP_P (place)
13693 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13694 && (JUMP_LABEL (place) == NULL
13695 || JUMP_LABEL (place) == XEXP (note, 0)))
13697 rtx label = JUMP_LABEL (place);
13699 if (!label)
13700 JUMP_LABEL (place) = XEXP (note, 0);
13701 else if (LABEL_P (label))
13702 LABEL_NUSES (label)--;
13705 if (place2 && JUMP_P (place2)
13706 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13707 && (JUMP_LABEL (place2) == NULL
13708 || JUMP_LABEL (place2) == XEXP (note, 0)))
13710 rtx label = JUMP_LABEL (place2);
13712 if (!label)
13713 JUMP_LABEL (place2) = XEXP (note, 0);
13714 else if (LABEL_P (label))
13715 LABEL_NUSES (label)--;
13716 place2 = 0;
13718 break;
13720 case REG_NONNEG:
13721 /* This note says something about the value of a register prior
13722 to the execution of an insn. It is too much trouble to see
13723 if the note is still correct in all situations. It is better
13724 to simply delete it. */
13725 break;
13727 case REG_DEAD:
13728 /* If we replaced the right hand side of FROM_INSN with a
13729 REG_EQUAL note, the original use of the dying register
13730 will not have been combined into I3 and I2. In such cases,
13731 FROM_INSN is guaranteed to be the first of the combined
13732 instructions, so we simply need to search back before
13733 FROM_INSN for the previous use or set of this register,
13734 then alter the notes there appropriately.
13736 If the register is used as an input in I3, it dies there.
13737 Similarly for I2, if it is nonzero and adjacent to I3.
13739 If the register is not used as an input in either I3 or I2
13740 and it is not one of the registers we were supposed to eliminate,
13741 there are two possibilities. We might have a non-adjacent I2
13742 or we might have somehow eliminated an additional register
13743 from a computation. For example, we might have had A & B where
13744 we discover that B will always be zero. In this case we will
13745 eliminate the reference to A.
13747 In both cases, we must search to see if we can find a previous
13748 use of A and put the death note there. */
13750 if (from_insn
13751 && from_insn == i2mod
13752 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13753 tem_insn = from_insn;
13754 else
13756 if (from_insn
13757 && CALL_P (from_insn)
13758 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13759 place = from_insn;
13760 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13761 place = i3;
13762 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13763 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13764 place = i2;
13765 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13766 && !(i2mod
13767 && reg_overlap_mentioned_p (XEXP (note, 0),
13768 i2mod_old_rhs)))
13769 || rtx_equal_p (XEXP (note, 0), elim_i1)
13770 || rtx_equal_p (XEXP (note, 0), elim_i0))
13771 break;
13772 tem_insn = i3;
13773 /* If the new I2 sets the same register that is marked dead
13774 in the note, the note now should not be put on I2, as the
13775 note refers to a previous incarnation of the reg. */
13776 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
13777 tem_insn = i2;
13780 if (place == 0)
13782 basic_block bb = this_basic_block;
13784 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13786 if (!NONDEBUG_INSN_P (tem_insn))
13788 if (tem_insn == BB_HEAD (bb))
13789 break;
13790 continue;
13793 /* If the register is being set at TEM_INSN, see if that is all
13794 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13795 into a REG_UNUSED note instead. Don't delete sets to
13796 global register vars. */
13797 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13798 || !global_regs[REGNO (XEXP (note, 0))])
13799 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13801 rtx set = single_set (tem_insn);
13802 rtx inner_dest = 0;
13803 #ifdef HAVE_cc0
13804 rtx_insn *cc0_setter = NULL;
13805 #endif
13807 if (set != 0)
13808 for (inner_dest = SET_DEST (set);
13809 (GET_CODE (inner_dest) == STRICT_LOW_PART
13810 || GET_CODE (inner_dest) == SUBREG
13811 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13812 inner_dest = XEXP (inner_dest, 0))
13815 /* Verify that it was the set, and not a clobber that
13816 modified the register.
13818 CC0 targets must be careful to maintain setter/user
13819 pairs. If we cannot delete the setter due to side
13820 effects, mark the user with an UNUSED note instead
13821 of deleting it. */
13823 if (set != 0 && ! side_effects_p (SET_SRC (set))
13824 && rtx_equal_p (XEXP (note, 0), inner_dest)
13825 #ifdef HAVE_cc0
13826 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13827 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13828 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13829 #endif
13832 /* Move the notes and links of TEM_INSN elsewhere.
13833 This might delete other dead insns recursively.
13834 First set the pattern to something that won't use
13835 any register. */
13836 rtx old_notes = REG_NOTES (tem_insn);
13838 PATTERN (tem_insn) = pc_rtx;
13839 REG_NOTES (tem_insn) = NULL;
13841 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13842 NULL_RTX, NULL_RTX, NULL_RTX);
13843 distribute_links (LOG_LINKS (tem_insn));
13845 SET_INSN_DELETED (tem_insn);
13846 if (tem_insn == i2)
13847 i2 = NULL;
13849 #ifdef HAVE_cc0
13850 /* Delete the setter too. */
13851 if (cc0_setter)
13853 PATTERN (cc0_setter) = pc_rtx;
13854 old_notes = REG_NOTES (cc0_setter);
13855 REG_NOTES (cc0_setter) = NULL;
13857 distribute_notes (old_notes, cc0_setter,
13858 cc0_setter, NULL,
13859 NULL_RTX, NULL_RTX, NULL_RTX);
13860 distribute_links (LOG_LINKS (cc0_setter));
13862 SET_INSN_DELETED (cc0_setter);
13863 if (cc0_setter == i2)
13864 i2 = NULL;
13866 #endif
13868 else
13870 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13872 /* If there isn't already a REG_UNUSED note, put one
13873 here. Do not place a REG_DEAD note, even if
13874 the register is also used here; that would not
13875 match the algorithm used in lifetime analysis
13876 and can cause the consistency check in the
13877 scheduler to fail. */
13878 if (! find_regno_note (tem_insn, REG_UNUSED,
13879 REGNO (XEXP (note, 0))))
13880 place = tem_insn;
13881 break;
13884 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13885 || (CALL_P (tem_insn)
13886 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13888 place = tem_insn;
13890 /* If we are doing a 3->2 combination, and we have a
13891 register which formerly died in i3 and was not used
13892 by i2, which now no longer dies in i3 and is used in
13893 i2 but does not die in i2, and place is between i2
13894 and i3, then we may need to move a link from place to
13895 i2. */
13896 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13897 && from_insn
13898 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13899 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13901 struct insn_link *links = LOG_LINKS (place);
13902 LOG_LINKS (place) = NULL;
13903 distribute_links (links);
13905 break;
13908 if (tem_insn == BB_HEAD (bb))
13909 break;
13914 /* If the register is set or already dead at PLACE, we needn't do
13915 anything with this note if it is still a REG_DEAD note.
13916 We check here if it is set at all, not if is it totally replaced,
13917 which is what `dead_or_set_p' checks, so also check for it being
13918 set partially. */
13920 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13922 unsigned int regno = REGNO (XEXP (note, 0));
13923 reg_stat_type *rsp = &reg_stat[regno];
13925 if (dead_or_set_p (place, XEXP (note, 0))
13926 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13928 /* Unless the register previously died in PLACE, clear
13929 last_death. [I no longer understand why this is
13930 being done.] */
13931 if (rsp->last_death != place)
13932 rsp->last_death = 0;
13933 place = 0;
13935 else
13936 rsp->last_death = place;
13938 /* If this is a death note for a hard reg that is occupying
13939 multiple registers, ensure that we are still using all
13940 parts of the object. If we find a piece of the object
13941 that is unused, we must arrange for an appropriate REG_DEAD
13942 note to be added for it. However, we can't just emit a USE
13943 and tag the note to it, since the register might actually
13944 be dead; so we recourse, and the recursive call then finds
13945 the previous insn that used this register. */
13947 if (place && regno < FIRST_PSEUDO_REGISTER
13948 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13950 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13951 bool all_used = true;
13952 unsigned int i;
13954 for (i = regno; i < endregno; i++)
13955 if ((! refers_to_regno_p (i, PATTERN (place))
13956 && ! find_regno_fusage (place, USE, i))
13957 || dead_or_set_regno_p (place, i))
13959 all_used = false;
13960 break;
13963 if (! all_used)
13965 /* Put only REG_DEAD notes for pieces that are
13966 not already dead or set. */
13968 for (i = regno; i < endregno;
13969 i += hard_regno_nregs[i][reg_raw_mode[i]])
13971 rtx piece = regno_reg_rtx[i];
13972 basic_block bb = this_basic_block;
13974 if (! dead_or_set_p (place, piece)
13975 && ! reg_bitfield_target_p (piece,
13976 PATTERN (place)))
13978 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13979 NULL_RTX);
13981 distribute_notes (new_note, place, place,
13982 NULL, NULL_RTX, NULL_RTX,
13983 NULL_RTX);
13985 else if (! refers_to_regno_p (i, PATTERN (place))
13986 && ! find_regno_fusage (place, USE, i))
13987 for (tem_insn = PREV_INSN (place); ;
13988 tem_insn = PREV_INSN (tem_insn))
13990 if (!NONDEBUG_INSN_P (tem_insn))
13992 if (tem_insn == BB_HEAD (bb))
13993 break;
13994 continue;
13996 if (dead_or_set_p (tem_insn, piece)
13997 || reg_bitfield_target_p (piece,
13998 PATTERN (tem_insn)))
14000 add_reg_note (tem_insn, REG_UNUSED, piece);
14001 break;
14006 place = 0;
14010 break;
14012 default:
14013 /* Any other notes should not be present at this point in the
14014 compilation. */
14015 gcc_unreachable ();
14018 if (place)
14020 XEXP (note, 1) = REG_NOTES (place);
14021 REG_NOTES (place) = note;
14024 if (place2)
14025 add_shallow_copy_of_reg_note (place2, note);
14029 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14030 I3, I2, and I1 to new locations. This is also called to add a link
14031 pointing at I3 when I3's destination is changed. */
14033 static void
14034 distribute_links (struct insn_link *links)
14036 struct insn_link *link, *next_link;
14038 for (link = links; link; link = next_link)
14040 rtx_insn *place = 0;
14041 rtx_insn *insn;
14042 rtx set, reg;
14044 next_link = link->next;
14046 /* If the insn that this link points to is a NOTE, ignore it. */
14047 if (NOTE_P (link->insn))
14048 continue;
14050 set = 0;
14051 rtx pat = PATTERN (link->insn);
14052 if (GET_CODE (pat) == SET)
14053 set = pat;
14054 else if (GET_CODE (pat) == PARALLEL)
14056 int i;
14057 for (i = 0; i < XVECLEN (pat, 0); i++)
14059 set = XVECEXP (pat, 0, i);
14060 if (GET_CODE (set) != SET)
14061 continue;
14063 reg = SET_DEST (set);
14064 while (GET_CODE (reg) == ZERO_EXTRACT
14065 || GET_CODE (reg) == STRICT_LOW_PART
14066 || GET_CODE (reg) == SUBREG)
14067 reg = XEXP (reg, 0);
14069 if (!REG_P (reg))
14070 continue;
14072 if (REGNO (reg) == link->regno)
14073 break;
14075 if (i == XVECLEN (pat, 0))
14076 continue;
14078 else
14079 continue;
14081 reg = SET_DEST (set);
14083 while (GET_CODE (reg) == ZERO_EXTRACT
14084 || GET_CODE (reg) == STRICT_LOW_PART
14085 || GET_CODE (reg) == SUBREG)
14086 reg = XEXP (reg, 0);
14088 /* A LOG_LINK is defined as being placed on the first insn that uses
14089 a register and points to the insn that sets the register. Start
14090 searching at the next insn after the target of the link and stop
14091 when we reach a set of the register or the end of the basic block.
14093 Note that this correctly handles the link that used to point from
14094 I3 to I2. Also note that not much searching is typically done here
14095 since most links don't point very far away. */
14097 for (insn = NEXT_INSN (link->insn);
14098 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14099 || BB_HEAD (this_basic_block->next_bb) != insn));
14100 insn = NEXT_INSN (insn))
14101 if (DEBUG_INSN_P (insn))
14102 continue;
14103 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14105 if (reg_referenced_p (reg, PATTERN (insn)))
14106 place = insn;
14107 break;
14109 else if (CALL_P (insn)
14110 && find_reg_fusage (insn, USE, reg))
14112 place = insn;
14113 break;
14115 else if (INSN_P (insn) && reg_set_p (reg, insn))
14116 break;
14118 /* If we found a place to put the link, place it there unless there
14119 is already a link to the same insn as LINK at that point. */
14121 if (place)
14123 struct insn_link *link2;
14125 FOR_EACH_LOG_LINK (link2, place)
14126 if (link2->insn == link->insn && link2->regno == link->regno)
14127 break;
14129 if (link2 == NULL)
14131 link->next = LOG_LINKS (place);
14132 LOG_LINKS (place) = link;
14134 /* Set added_links_insn to the earliest insn we added a
14135 link to. */
14136 if (added_links_insn == 0
14137 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14138 added_links_insn = place;
14144 /* Check for any register or memory mentioned in EQUIV that is not
14145 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14146 of EXPR where some registers may have been replaced by constants. */
14148 static bool
14149 unmentioned_reg_p (rtx equiv, rtx expr)
14151 subrtx_iterator::array_type array;
14152 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14154 const_rtx x = *iter;
14155 if ((REG_P (x) || MEM_P (x))
14156 && !reg_mentioned_p (x, expr))
14157 return true;
14159 return false;
14162 DEBUG_FUNCTION void
14163 dump_combine_stats (FILE *file)
14165 fprintf
14166 (file,
14167 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14168 combine_attempts, combine_merges, combine_extras, combine_successes);
14171 void
14172 dump_combine_total_stats (FILE *file)
14174 fprintf
14175 (file,
14176 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14177 total_attempts, total_merges, total_extras, total_successes);
14180 /* Try combining insns through substitution. */
14181 static unsigned int
14182 rest_of_handle_combine (void)
14184 int rebuild_jump_labels_after_combine;
14186 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14187 df_note_add_problem ();
14188 df_analyze ();
14190 regstat_init_n_sets_and_refs ();
14191 reg_n_sets_max = max_reg_num ();
14193 rebuild_jump_labels_after_combine
14194 = combine_instructions (get_insns (), max_reg_num ());
14196 /* Combining insns may have turned an indirect jump into a
14197 direct jump. Rebuild the JUMP_LABEL fields of jumping
14198 instructions. */
14199 if (rebuild_jump_labels_after_combine)
14201 timevar_push (TV_JUMP);
14202 rebuild_jump_labels (get_insns ());
14203 cleanup_cfg (0);
14204 timevar_pop (TV_JUMP);
14207 regstat_free_n_sets_and_refs ();
14208 return 0;
14211 namespace {
14213 const pass_data pass_data_combine =
14215 RTL_PASS, /* type */
14216 "combine", /* name */
14217 OPTGROUP_NONE, /* optinfo_flags */
14218 TV_COMBINE, /* tv_id */
14219 PROP_cfglayout, /* properties_required */
14220 0, /* properties_provided */
14221 0, /* properties_destroyed */
14222 0, /* todo_flags_start */
14223 TODO_df_finish, /* todo_flags_finish */
14226 class pass_combine : public rtl_opt_pass
14228 public:
14229 pass_combine (gcc::context *ctxt)
14230 : rtl_opt_pass (pass_data_combine, ctxt)
14233 /* opt_pass methods: */
14234 virtual bool gate (function *) { return (optimize > 0); }
14235 virtual unsigned int execute (function *)
14237 return rest_of_handle_combine ();
14240 }; // class pass_combine
14242 } // anon namespace
14244 rtl_opt_pass *
14245 make_pass_combine (gcc::context *ctxt)
14247 return new pass_combine (ctxt);