Fix g++.dg/torture/Wsizeof-pointer-memaccess2.C with -std=c++11
[official-gcc.git] / gcc / combine.c
blobc04146ae645a766a923d335c7c2d246cea9ee0fc
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This module is essentially the "combiner" phase of the U. of Arizona
21 Portable Optimizer, but redone to work on our list-structured
22 representation for RTL instead of their string representation.
24 The LOG_LINKS of each insn identify the most recent assignment
25 to each REG used in the insn. It is a list of previous insns,
26 each of which contains a SET for a REG that is used in this insn
27 and not used or set in between. LOG_LINKs never cross basic blocks.
28 They were set up by the preceding pass (lifetime analysis).
30 We try to combine each pair of insns joined by a logical link.
31 We also try to combine triplets of insns A, B and C when C has
32 a link back to B and B has a link back to A. Likewise for a
33 small number of quadruplets of insns A, B, C and D for which
34 there's high likelihood of of success.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "hash-set.h"
84 #include "machmode.h"
85 #include "vec.h"
86 #include "double-int.h"
87 #include "input.h"
88 #include "alias.h"
89 #include "symtab.h"
90 #include "wide-int.h"
91 #include "inchash.h"
92 #include "tree.h"
93 #include "stor-layout.h"
94 #include "tm_p.h"
95 #include "flags.h"
96 #include "regs.h"
97 #include "hard-reg-set.h"
98 #include "predict.h"
99 #include "function.h"
100 #include "dominance.h"
101 #include "cfg.h"
102 #include "cfgrtl.h"
103 #include "cfgcleanup.h"
104 #include "basic-block.h"
105 #include "insn-config.h"
106 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
107 #include "hashtab.h"
108 #include "statistics.h"
109 #include "real.h"
110 #include "fixed-value.h"
111 #include "expmed.h"
112 #include "dojump.h"
113 #include "explow.h"
114 #include "calls.h"
115 #include "emit-rtl.h"
116 #include "varasm.h"
117 #include "stmt.h"
118 #include "expr.h"
119 #include "insn-attr.h"
120 #include "recog.h"
121 #include "diagnostic-core.h"
122 #include "target.h"
123 #include "insn-codes.h"
124 #include "optabs.h"
125 #include "rtlhooks-def.h"
126 #include "params.h"
127 #include "tree-pass.h"
128 #include "df.h"
129 #include "valtrack.h"
130 #include "hash-map.h"
131 #include "is-a.h"
132 #include "plugin-api.h"
133 #include "ipa-ref.h"
134 #include "cgraph.h"
135 #include "obstack.h"
136 #include "rtl-iter.h"
138 /* Number of attempts to combine instructions in this function. */
140 static int combine_attempts;
142 /* Number of attempts that got as far as substitution in this function. */
144 static int combine_merges;
146 /* Number of instructions combined with added SETs in this function. */
148 static int combine_extras;
150 /* Number of instructions combined in this function. */
152 static int combine_successes;
154 /* Totals over entire compilation. */
156 static int total_attempts, total_merges, total_extras, total_successes;
158 /* combine_instructions may try to replace the right hand side of the
159 second instruction with the value of an associated REG_EQUAL note
160 before throwing it at try_combine. That is problematic when there
161 is a REG_DEAD note for a register used in the old right hand side
162 and can cause distribute_notes to do wrong things. This is the
163 second instruction if it has been so modified, null otherwise. */
165 static rtx_insn *i2mod;
167 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
169 static rtx i2mod_old_rhs;
171 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
173 static rtx i2mod_new_rhs;
175 typedef struct reg_stat_struct {
176 /* Record last point of death of (hard or pseudo) register n. */
177 rtx_insn *last_death;
179 /* Record last point of modification of (hard or pseudo) register n. */
180 rtx_insn *last_set;
182 /* The next group of fields allows the recording of the last value assigned
183 to (hard or pseudo) register n. We use this information to see if an
184 operation being processed is redundant given a prior operation performed
185 on the register. For example, an `and' with a constant is redundant if
186 all the zero bits are already known to be turned off.
188 We use an approach similar to that used by cse, but change it in the
189 following ways:
191 (1) We do not want to reinitialize at each label.
192 (2) It is useful, but not critical, to know the actual value assigned
193 to a register. Often just its form is helpful.
195 Therefore, we maintain the following fields:
197 last_set_value the last value assigned
198 last_set_label records the value of label_tick when the
199 register was assigned
200 last_set_table_tick records the value of label_tick when a
201 value using the register is assigned
202 last_set_invalid set to nonzero when it is not valid
203 to use the value of this register in some
204 register's value
206 To understand the usage of these tables, it is important to understand
207 the distinction between the value in last_set_value being valid and
208 the register being validly contained in some other expression in the
209 table.
211 (The next two parameters are out of date).
213 reg_stat[i].last_set_value is valid if it is nonzero, and either
214 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
216 Register I may validly appear in any expression returned for the value
217 of another register if reg_n_sets[i] is 1. It may also appear in the
218 value for register J if reg_stat[j].last_set_invalid is zero, or
219 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
221 If an expression is found in the table containing a register which may
222 not validly appear in an expression, the register is replaced by
223 something that won't match, (clobber (const_int 0)). */
225 /* Record last value assigned to (hard or pseudo) register n. */
227 rtx last_set_value;
229 /* Record the value of label_tick when an expression involving register n
230 is placed in last_set_value. */
232 int last_set_table_tick;
234 /* Record the value of label_tick when the value for register n is placed in
235 last_set_value. */
237 int last_set_label;
239 /* These fields are maintained in parallel with last_set_value and are
240 used to store the mode in which the register was last set, the bits
241 that were known to be zero when it was last set, and the number of
242 sign bits copies it was known to have when it was last set. */
244 unsigned HOST_WIDE_INT last_set_nonzero_bits;
245 char last_set_sign_bit_copies;
246 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
248 /* Set nonzero if references to register n in expressions should not be
249 used. last_set_invalid is set nonzero when this register is being
250 assigned to and last_set_table_tick == label_tick. */
252 char last_set_invalid;
254 /* Some registers that are set more than once and used in more than one
255 basic block are nevertheless always set in similar ways. For example,
256 a QImode register may be loaded from memory in two places on a machine
257 where byte loads zero extend.
259 We record in the following fields if a register has some leading bits
260 that are always equal to the sign bit, and what we know about the
261 nonzero bits of a register, specifically which bits are known to be
262 zero.
264 If an entry is zero, it means that we don't know anything special. */
266 unsigned char sign_bit_copies;
268 unsigned HOST_WIDE_INT nonzero_bits;
270 /* Record the value of the label_tick when the last truncation
271 happened. The field truncated_to_mode is only valid if
272 truncation_label == label_tick. */
274 int truncation_label;
276 /* Record the last truncation seen for this register. If truncation
277 is not a nop to this mode we might be able to save an explicit
278 truncation if we know that value already contains a truncated
279 value. */
281 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
282 } reg_stat_type;
285 static vec<reg_stat_type> reg_stat;
287 /* One plus the highest pseudo for which we track REG_N_SETS.
288 regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
289 but during combine_split_insns new pseudos can be created. As we don't have
290 updated DF information in that case, it is hard to initialize the array
291 after growing. The combiner only cares about REG_N_SETS (regno) == 1,
292 so instead of growing the arrays, just assume all newly created pseudos
293 during combine might be set multiple times. */
295 static unsigned int reg_n_sets_max;
297 /* Record the luid of the last insn that invalidated memory
298 (anything that writes memory, and subroutine calls, but not pushes). */
300 static int mem_last_set;
302 /* Record the luid of the last CALL_INSN
303 so we can tell whether a potential combination crosses any calls. */
305 static int last_call_luid;
307 /* When `subst' is called, this is the insn that is being modified
308 (by combining in a previous insn). The PATTERN of this insn
309 is still the old pattern partially modified and it should not be
310 looked at, but this may be used to examine the successors of the insn
311 to judge whether a simplification is valid. */
313 static rtx_insn *subst_insn;
315 /* This is the lowest LUID that `subst' is currently dealing with.
316 get_last_value will not return a value if the register was set at or
317 after this LUID. If not for this mechanism, we could get confused if
318 I2 or I1 in try_combine were an insn that used the old value of a register
319 to obtain a new value. In that case, we might erroneously get the
320 new value of the register when we wanted the old one. */
322 static int subst_low_luid;
324 /* This contains any hard registers that are used in newpat; reg_dead_at_p
325 must consider all these registers to be always live. */
327 static HARD_REG_SET newpat_used_regs;
329 /* This is an insn to which a LOG_LINKS entry has been added. If this
330 insn is the earlier than I2 or I3, combine should rescan starting at
331 that location. */
333 static rtx_insn *added_links_insn;
335 /* Basic block in which we are performing combines. */
336 static basic_block this_basic_block;
337 static bool optimize_this_for_speed_p;
340 /* Length of the currently allocated uid_insn_cost array. */
342 static int max_uid_known;
344 /* The following array records the insn_rtx_cost for every insn
345 in the instruction stream. */
347 static int *uid_insn_cost;
349 /* The following array records the LOG_LINKS for every insn in the
350 instruction stream as struct insn_link pointers. */
352 struct insn_link {
353 rtx_insn *insn;
354 unsigned int regno;
355 struct insn_link *next;
358 static struct insn_link **uid_log_links;
360 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
361 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
363 #define FOR_EACH_LOG_LINK(L, INSN) \
364 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
366 /* Links for LOG_LINKS are allocated from this obstack. */
368 static struct obstack insn_link_obstack;
370 /* Allocate a link. */
372 static inline struct insn_link *
373 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
375 struct insn_link *l
376 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
377 sizeof (struct insn_link));
378 l->insn = insn;
379 l->regno = regno;
380 l->next = next;
381 return l;
384 /* Incremented for each basic block. */
386 static int label_tick;
388 /* Reset to label_tick for each extended basic block in scanning order. */
390 static int label_tick_ebb_start;
392 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
393 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
395 static machine_mode nonzero_bits_mode;
397 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
398 be safely used. It is zero while computing them and after combine has
399 completed. This former test prevents propagating values based on
400 previously set values, which can be incorrect if a variable is modified
401 in a loop. */
403 static int nonzero_sign_valid;
406 /* Record one modification to rtl structure
407 to be undone by storing old_contents into *where. */
409 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
411 struct undo
413 struct undo *next;
414 enum undo_kind kind;
415 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
416 union { rtx *r; int *i; struct insn_link **l; } where;
419 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
420 num_undo says how many are currently recorded.
422 other_insn is nonzero if we have modified some other insn in the process
423 of working on subst_insn. It must be verified too. */
425 struct undobuf
427 struct undo *undos;
428 struct undo *frees;
429 rtx_insn *other_insn;
432 static struct undobuf undobuf;
434 /* Number of times the pseudo being substituted for
435 was found and replaced. */
437 static int n_occurrences;
439 static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
440 machine_mode,
441 unsigned HOST_WIDE_INT,
442 unsigned HOST_WIDE_INT *);
443 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
444 machine_mode,
445 unsigned int, unsigned int *);
446 static void do_SUBST (rtx *, rtx);
447 static void do_SUBST_INT (int *, int);
448 static void init_reg_last (void);
449 static void setup_incoming_promotions (rtx_insn *);
450 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
451 static int cant_combine_insn_p (rtx_insn *);
452 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
453 rtx_insn *, rtx_insn *, rtx *, rtx *);
454 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
455 static int contains_muldiv (rtx);
456 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
457 int *, rtx_insn *);
458 static void undo_all (void);
459 static void undo_commit (void);
460 static rtx *find_split_point (rtx *, rtx_insn *, bool);
461 static rtx subst (rtx, rtx, rtx, int, int, int);
462 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
463 static rtx simplify_if_then_else (rtx);
464 static rtx simplify_set (rtx);
465 static rtx simplify_logical (rtx);
466 static rtx expand_compound_operation (rtx);
467 static const_rtx expand_field_assignment (const_rtx);
468 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
469 rtx, unsigned HOST_WIDE_INT, int, int, int);
470 static rtx extract_left_shift (rtx, int);
471 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
472 unsigned HOST_WIDE_INT *);
473 static rtx canon_reg_for_combine (rtx, rtx);
474 static rtx force_to_mode (rtx, machine_mode,
475 unsigned HOST_WIDE_INT, int);
476 static rtx if_then_else_cond (rtx, rtx *, rtx *);
477 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
478 static int rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
479 static rtx make_field_assignment (rtx);
480 static rtx apply_distributive_law (rtx);
481 static rtx distribute_and_simplify_rtx (rtx, int);
482 static rtx simplify_and_const_int_1 (machine_mode, rtx,
483 unsigned HOST_WIDE_INT);
484 static rtx simplify_and_const_int (rtx, machine_mode, rtx,
485 unsigned HOST_WIDE_INT);
486 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
487 HOST_WIDE_INT, machine_mode, int *);
488 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
489 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
490 int);
491 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
492 static rtx gen_lowpart_for_combine (machine_mode, rtx);
493 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
494 rtx, rtx *);
495 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
496 static void update_table_tick (rtx);
497 static void record_value_for_reg (rtx, rtx_insn *, rtx);
498 static void check_promoted_subreg (rtx_insn *, rtx);
499 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
500 static void record_dead_and_set_regs (rtx_insn *);
501 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
502 static rtx get_last_value (const_rtx);
503 static int use_crosses_set_p (const_rtx, int);
504 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
505 static int reg_dead_at_p (rtx, rtx_insn *);
506 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
507 static int reg_bitfield_target_p (rtx, rtx);
508 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
509 static void distribute_links (struct insn_link *);
510 static void mark_used_regs_combine (rtx);
511 static void record_promoted_value (rtx_insn *, rtx);
512 static bool unmentioned_reg_p (rtx, rtx);
513 static void record_truncated_values (rtx *, void *);
514 static bool reg_truncated_to_mode (machine_mode, const_rtx);
515 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
518 /* It is not safe to use ordinary gen_lowpart in combine.
519 See comments in gen_lowpart_for_combine. */
520 #undef RTL_HOOKS_GEN_LOWPART
521 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
523 /* Our implementation of gen_lowpart never emits a new pseudo. */
524 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
525 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
527 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
528 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
530 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
531 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
533 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
534 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
536 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
539 /* Convenience wrapper for the canonicalize_comparison target hook.
540 Target hooks cannot use enum rtx_code. */
541 static inline void
542 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
543 bool op0_preserve_value)
545 int code_int = (int)*code;
546 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
547 *code = (enum rtx_code)code_int;
550 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
551 PATTERN can not be split. Otherwise, it returns an insn sequence.
552 This is a wrapper around split_insns which ensures that the
553 reg_stat vector is made larger if the splitter creates a new
554 register. */
556 static rtx_insn *
557 combine_split_insns (rtx pattern, rtx insn)
559 rtx_insn *ret;
560 unsigned int nregs;
562 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
563 nregs = max_reg_num ();
564 if (nregs > reg_stat.length ())
565 reg_stat.safe_grow_cleared (nregs);
566 return ret;
569 /* This is used by find_single_use to locate an rtx in LOC that
570 contains exactly one use of DEST, which is typically either a REG
571 or CC0. It returns a pointer to the innermost rtx expression
572 containing DEST. Appearances of DEST that are being used to
573 totally replace it are not counted. */
575 static rtx *
576 find_single_use_1 (rtx dest, rtx *loc)
578 rtx x = *loc;
579 enum rtx_code code = GET_CODE (x);
580 rtx *result = NULL;
581 rtx *this_result;
582 int i;
583 const char *fmt;
585 switch (code)
587 case CONST:
588 case LABEL_REF:
589 case SYMBOL_REF:
590 CASE_CONST_ANY:
591 case CLOBBER:
592 return 0;
594 case SET:
595 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
596 of a REG that occupies all of the REG, the insn uses DEST if
597 it is mentioned in the destination or the source. Otherwise, we
598 need just check the source. */
599 if (GET_CODE (SET_DEST (x)) != CC0
600 && GET_CODE (SET_DEST (x)) != PC
601 && !REG_P (SET_DEST (x))
602 && ! (GET_CODE (SET_DEST (x)) == SUBREG
603 && REG_P (SUBREG_REG (SET_DEST (x)))
604 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
605 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
606 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
607 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
608 break;
610 return find_single_use_1 (dest, &SET_SRC (x));
612 case MEM:
613 case SUBREG:
614 return find_single_use_1 (dest, &XEXP (x, 0));
616 default:
617 break;
620 /* If it wasn't one of the common cases above, check each expression and
621 vector of this code. Look for a unique usage of DEST. */
623 fmt = GET_RTX_FORMAT (code);
624 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
626 if (fmt[i] == 'e')
628 if (dest == XEXP (x, i)
629 || (REG_P (dest) && REG_P (XEXP (x, i))
630 && REGNO (dest) == REGNO (XEXP (x, i))))
631 this_result = loc;
632 else
633 this_result = find_single_use_1 (dest, &XEXP (x, i));
635 if (result == NULL)
636 result = this_result;
637 else if (this_result)
638 /* Duplicate usage. */
639 return NULL;
641 else if (fmt[i] == 'E')
643 int j;
645 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
647 if (XVECEXP (x, i, j) == dest
648 || (REG_P (dest)
649 && REG_P (XVECEXP (x, i, j))
650 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
651 this_result = loc;
652 else
653 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
655 if (result == NULL)
656 result = this_result;
657 else if (this_result)
658 return NULL;
663 return result;
667 /* See if DEST, produced in INSN, is used only a single time in the
668 sequel. If so, return a pointer to the innermost rtx expression in which
669 it is used.
671 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
673 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
674 care about REG_DEAD notes or LOG_LINKS.
676 Otherwise, we find the single use by finding an insn that has a
677 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
678 only referenced once in that insn, we know that it must be the first
679 and last insn referencing DEST. */
681 static rtx *
682 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
684 basic_block bb;
685 rtx_insn *next;
686 rtx *result;
687 struct insn_link *link;
689 if (dest == cc0_rtx)
691 next = NEXT_INSN (insn);
692 if (next == 0
693 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
694 return 0;
696 result = find_single_use_1 (dest, &PATTERN (next));
697 if (result && ploc)
698 *ploc = next;
699 return result;
702 if (!REG_P (dest))
703 return 0;
705 bb = BLOCK_FOR_INSN (insn);
706 for (next = NEXT_INSN (insn);
707 next && BLOCK_FOR_INSN (next) == bb;
708 next = NEXT_INSN (next))
709 if (INSN_P (next) && dead_or_set_p (next, dest))
711 FOR_EACH_LOG_LINK (link, next)
712 if (link->insn == insn && link->regno == REGNO (dest))
713 break;
715 if (link)
717 result = find_single_use_1 (dest, &PATTERN (next));
718 if (ploc)
719 *ploc = next;
720 return result;
724 return 0;
727 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
728 insn. The substitution can be undone by undo_all. If INTO is already
729 set to NEWVAL, do not record this change. Because computing NEWVAL might
730 also call SUBST, we have to compute it before we put anything into
731 the undo table. */
733 static void
734 do_SUBST (rtx *into, rtx newval)
736 struct undo *buf;
737 rtx oldval = *into;
739 if (oldval == newval)
740 return;
742 /* We'd like to catch as many invalid transformations here as
743 possible. Unfortunately, there are way too many mode changes
744 that are perfectly valid, so we'd waste too much effort for
745 little gain doing the checks here. Focus on catching invalid
746 transformations involving integer constants. */
747 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
748 && CONST_INT_P (newval))
750 /* Sanity check that we're replacing oldval with a CONST_INT
751 that is a valid sign-extension for the original mode. */
752 gcc_assert (INTVAL (newval)
753 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
755 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
756 CONST_INT is not valid, because after the replacement, the
757 original mode would be gone. Unfortunately, we can't tell
758 when do_SUBST is called to replace the operand thereof, so we
759 perform this test on oldval instead, checking whether an
760 invalid replacement took place before we got here. */
761 gcc_assert (!(GET_CODE (oldval) == SUBREG
762 && CONST_INT_P (SUBREG_REG (oldval))));
763 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
764 && CONST_INT_P (XEXP (oldval, 0))));
767 if (undobuf.frees)
768 buf = undobuf.frees, undobuf.frees = buf->next;
769 else
770 buf = XNEW (struct undo);
772 buf->kind = UNDO_RTX;
773 buf->where.r = into;
774 buf->old_contents.r = oldval;
775 *into = newval;
777 buf->next = undobuf.undos, undobuf.undos = buf;
780 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
782 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
783 for the value of a HOST_WIDE_INT value (including CONST_INT) is
784 not safe. */
786 static void
787 do_SUBST_INT (int *into, int newval)
789 struct undo *buf;
790 int oldval = *into;
792 if (oldval == newval)
793 return;
795 if (undobuf.frees)
796 buf = undobuf.frees, undobuf.frees = buf->next;
797 else
798 buf = XNEW (struct undo);
800 buf->kind = UNDO_INT;
801 buf->where.i = into;
802 buf->old_contents.i = oldval;
803 *into = newval;
805 buf->next = undobuf.undos, undobuf.undos = buf;
808 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
810 /* Similar to SUBST, but just substitute the mode. This is used when
811 changing the mode of a pseudo-register, so that any other
812 references to the entry in the regno_reg_rtx array will change as
813 well. */
815 static void
816 do_SUBST_MODE (rtx *into, machine_mode newval)
818 struct undo *buf;
819 machine_mode oldval = GET_MODE (*into);
821 if (oldval == newval)
822 return;
824 if (undobuf.frees)
825 buf = undobuf.frees, undobuf.frees = buf->next;
826 else
827 buf = XNEW (struct undo);
829 buf->kind = UNDO_MODE;
830 buf->where.r = into;
831 buf->old_contents.m = oldval;
832 adjust_reg_mode (*into, newval);
834 buf->next = undobuf.undos, undobuf.undos = buf;
837 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
839 #if !HAVE_cc0
840 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
842 static void
843 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
845 struct undo *buf;
846 struct insn_link * oldval = *into;
848 if (oldval == newval)
849 return;
851 if (undobuf.frees)
852 buf = undobuf.frees, undobuf.frees = buf->next;
853 else
854 buf = XNEW (struct undo);
856 buf->kind = UNDO_LINKS;
857 buf->where.l = into;
858 buf->old_contents.l = oldval;
859 *into = newval;
861 buf->next = undobuf.undos, undobuf.undos = buf;
864 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
865 #endif
867 /* Subroutine of try_combine. Determine whether the replacement patterns
868 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
869 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
870 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
871 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
872 of all the instructions can be estimated and the replacements are more
873 expensive than the original sequence. */
875 static bool
876 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
877 rtx newpat, rtx newi2pat, rtx newotherpat)
879 int i0_cost, i1_cost, i2_cost, i3_cost;
880 int new_i2_cost, new_i3_cost;
881 int old_cost, new_cost;
883 /* Lookup the original insn_rtx_costs. */
884 i2_cost = INSN_COST (i2);
885 i3_cost = INSN_COST (i3);
887 if (i1)
889 i1_cost = INSN_COST (i1);
890 if (i0)
892 i0_cost = INSN_COST (i0);
893 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
894 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
896 else
898 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
899 ? i1_cost + i2_cost + i3_cost : 0);
900 i0_cost = 0;
903 else
905 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
906 i1_cost = i0_cost = 0;
909 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
910 correct that. */
911 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
912 old_cost -= i1_cost;
915 /* Calculate the replacement insn_rtx_costs. */
916 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
917 if (newi2pat)
919 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
920 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
921 ? new_i2_cost + new_i3_cost : 0;
923 else
925 new_cost = new_i3_cost;
926 new_i2_cost = 0;
929 if (undobuf.other_insn)
931 int old_other_cost, new_other_cost;
933 old_other_cost = INSN_COST (undobuf.other_insn);
934 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
935 if (old_other_cost > 0 && new_other_cost > 0)
937 old_cost += old_other_cost;
938 new_cost += new_other_cost;
940 else
941 old_cost = 0;
944 /* Disallow this combination if both new_cost and old_cost are greater than
945 zero, and new_cost is greater than old cost. */
946 int reject = old_cost > 0 && new_cost > old_cost;
948 if (dump_file)
950 fprintf (dump_file, "%s combination of insns ",
951 reject ? "rejecting" : "allowing");
952 if (i0)
953 fprintf (dump_file, "%d, ", INSN_UID (i0));
954 if (i1 && INSN_UID (i1) != INSN_UID (i2))
955 fprintf (dump_file, "%d, ", INSN_UID (i1));
956 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
958 fprintf (dump_file, "original costs ");
959 if (i0)
960 fprintf (dump_file, "%d + ", i0_cost);
961 if (i1 && INSN_UID (i1) != INSN_UID (i2))
962 fprintf (dump_file, "%d + ", i1_cost);
963 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
965 if (newi2pat)
966 fprintf (dump_file, "replacement costs %d + %d = %d\n",
967 new_i2_cost, new_i3_cost, new_cost);
968 else
969 fprintf (dump_file, "replacement cost %d\n", new_cost);
972 if (reject)
973 return false;
975 /* Update the uid_insn_cost array with the replacement costs. */
976 INSN_COST (i2) = new_i2_cost;
977 INSN_COST (i3) = new_i3_cost;
978 if (i1)
980 INSN_COST (i1) = 0;
981 if (i0)
982 INSN_COST (i0) = 0;
985 return true;
989 /* Delete any insns that copy a register to itself. */
991 static void
992 delete_noop_moves (void)
994 rtx_insn *insn, *next;
995 basic_block bb;
997 FOR_EACH_BB_FN (bb, cfun)
999 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
1001 next = NEXT_INSN (insn);
1002 if (INSN_P (insn) && noop_move_p (insn))
1004 if (dump_file)
1005 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
1007 delete_insn_and_edges (insn);
1014 /* Return false if we do not want to (or cannot) combine DEF. */
1015 static bool
1016 can_combine_def_p (df_ref def)
1018 /* Do not consider if it is pre/post modification in MEM. */
1019 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1020 return false;
1022 unsigned int regno = DF_REF_REGNO (def);
1024 /* Do not combine frame pointer adjustments. */
1025 if ((regno == FRAME_POINTER_REGNUM
1026 && (!reload_completed || frame_pointer_needed))
1027 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1028 || (regno == HARD_FRAME_POINTER_REGNUM
1029 && (!reload_completed || frame_pointer_needed))
1030 #endif
1031 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1032 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1033 #endif
1035 return false;
1037 return true;
1040 /* Return false if we do not want to (or cannot) combine USE. */
1041 static bool
1042 can_combine_use_p (df_ref use)
1044 /* Do not consider the usage of the stack pointer by function call. */
1045 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1046 return false;
1048 return true;
1051 /* Fill in log links field for all insns. */
1053 static void
1054 create_log_links (void)
1056 basic_block bb;
1057 rtx_insn **next_use;
1058 rtx_insn *insn;
1059 df_ref def, use;
1061 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1063 /* Pass through each block from the end, recording the uses of each
1064 register and establishing log links when def is encountered.
1065 Note that we do not clear next_use array in order to save time,
1066 so we have to test whether the use is in the same basic block as def.
1068 There are a few cases below when we do not consider the definition or
1069 usage -- these are taken from original flow.c did. Don't ask me why it is
1070 done this way; I don't know and if it works, I don't want to know. */
1072 FOR_EACH_BB_FN (bb, cfun)
1074 FOR_BB_INSNS_REVERSE (bb, insn)
1076 if (!NONDEBUG_INSN_P (insn))
1077 continue;
1079 /* Log links are created only once. */
1080 gcc_assert (!LOG_LINKS (insn));
1082 FOR_EACH_INSN_DEF (def, insn)
1084 unsigned int regno = DF_REF_REGNO (def);
1085 rtx_insn *use_insn;
1087 if (!next_use[regno])
1088 continue;
1090 if (!can_combine_def_p (def))
1091 continue;
1093 use_insn = next_use[regno];
1094 next_use[regno] = NULL;
1096 if (BLOCK_FOR_INSN (use_insn) != bb)
1097 continue;
1099 /* flow.c claimed:
1101 We don't build a LOG_LINK for hard registers contained
1102 in ASM_OPERANDs. If these registers get replaced,
1103 we might wind up changing the semantics of the insn,
1104 even if reload can make what appear to be valid
1105 assignments later. */
1106 if (regno < FIRST_PSEUDO_REGISTER
1107 && asm_noperands (PATTERN (use_insn)) >= 0)
1108 continue;
1110 /* Don't add duplicate links between instructions. */
1111 struct insn_link *links;
1112 FOR_EACH_LOG_LINK (links, use_insn)
1113 if (insn == links->insn && regno == links->regno)
1114 break;
1116 if (!links)
1117 LOG_LINKS (use_insn)
1118 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1121 FOR_EACH_INSN_USE (use, insn)
1122 if (can_combine_use_p (use))
1123 next_use[DF_REF_REGNO (use)] = insn;
1127 free (next_use);
1130 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1131 true if we found a LOG_LINK that proves that A feeds B. This only works
1132 if there are no instructions between A and B which could have a link
1133 depending on A, since in that case we would not record a link for B.
1134 We also check the implicit dependency created by a cc0 setter/user
1135 pair. */
1137 static bool
1138 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1140 struct insn_link *links;
1141 FOR_EACH_LOG_LINK (links, b)
1142 if (links->insn == a)
1143 return true;
1144 if (HAVE_cc0 && sets_cc0_p (a))
1145 return true;
1146 return false;
1149 /* Main entry point for combiner. F is the first insn of the function.
1150 NREGS is the first unused pseudo-reg number.
1152 Return nonzero if the combiner has turned an indirect jump
1153 instruction into a direct jump. */
1154 static int
1155 combine_instructions (rtx_insn *f, unsigned int nregs)
1157 rtx_insn *insn, *next;
1158 #if HAVE_cc0
1159 rtx_insn *prev;
1160 #endif
1161 struct insn_link *links, *nextlinks;
1162 rtx_insn *first;
1163 basic_block last_bb;
1165 int new_direct_jump_p = 0;
1167 for (first = f; first && !INSN_P (first); )
1168 first = NEXT_INSN (first);
1169 if (!first)
1170 return 0;
1172 combine_attempts = 0;
1173 combine_merges = 0;
1174 combine_extras = 0;
1175 combine_successes = 0;
1177 rtl_hooks = combine_rtl_hooks;
1179 reg_stat.safe_grow_cleared (nregs);
1181 init_recog_no_volatile ();
1183 /* Allocate array for insn info. */
1184 max_uid_known = get_max_uid ();
1185 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1186 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1187 gcc_obstack_init (&insn_link_obstack);
1189 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1191 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1192 problems when, for example, we have j <<= 1 in a loop. */
1194 nonzero_sign_valid = 0;
1195 label_tick = label_tick_ebb_start = 1;
1197 /* Scan all SETs and see if we can deduce anything about what
1198 bits are known to be zero for some registers and how many copies
1199 of the sign bit are known to exist for those registers.
1201 Also set any known values so that we can use it while searching
1202 for what bits are known to be set. */
1204 setup_incoming_promotions (first);
1205 /* Allow the entry block and the first block to fall into the same EBB.
1206 Conceptually the incoming promotions are assigned to the entry block. */
1207 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1209 create_log_links ();
1210 FOR_EACH_BB_FN (this_basic_block, cfun)
1212 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1213 last_call_luid = 0;
1214 mem_last_set = -1;
1216 label_tick++;
1217 if (!single_pred_p (this_basic_block)
1218 || single_pred (this_basic_block) != last_bb)
1219 label_tick_ebb_start = label_tick;
1220 last_bb = this_basic_block;
1222 FOR_BB_INSNS (this_basic_block, insn)
1223 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1225 #ifdef AUTO_INC_DEC
1226 rtx links;
1227 #endif
1229 subst_low_luid = DF_INSN_LUID (insn);
1230 subst_insn = insn;
1232 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1233 insn);
1234 record_dead_and_set_regs (insn);
1236 #ifdef AUTO_INC_DEC
1237 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1238 if (REG_NOTE_KIND (links) == REG_INC)
1239 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1240 insn);
1241 #endif
1243 /* Record the current insn_rtx_cost of this instruction. */
1244 if (NONJUMP_INSN_P (insn))
1245 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1246 optimize_this_for_speed_p);
1247 if (dump_file)
1248 fprintf (dump_file, "insn_cost %d: %d\n",
1249 INSN_UID (insn), INSN_COST (insn));
1253 nonzero_sign_valid = 1;
1255 /* Now scan all the insns in forward order. */
1256 label_tick = label_tick_ebb_start = 1;
1257 init_reg_last ();
1258 setup_incoming_promotions (first);
1259 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1260 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1262 FOR_EACH_BB_FN (this_basic_block, cfun)
1264 rtx_insn *last_combined_insn = NULL;
1265 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1266 last_call_luid = 0;
1267 mem_last_set = -1;
1269 label_tick++;
1270 if (!single_pred_p (this_basic_block)
1271 || single_pred (this_basic_block) != last_bb)
1272 label_tick_ebb_start = label_tick;
1273 last_bb = this_basic_block;
1275 rtl_profile_for_bb (this_basic_block);
1276 for (insn = BB_HEAD (this_basic_block);
1277 insn != NEXT_INSN (BB_END (this_basic_block));
1278 insn = next ? next : NEXT_INSN (insn))
1280 next = 0;
1281 if (!NONDEBUG_INSN_P (insn))
1282 continue;
1284 while (last_combined_insn
1285 && last_combined_insn->deleted ())
1286 last_combined_insn = PREV_INSN (last_combined_insn);
1287 if (last_combined_insn == NULL_RTX
1288 || BARRIER_P (last_combined_insn)
1289 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1290 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1291 last_combined_insn = insn;
1293 /* See if we know about function return values before this
1294 insn based upon SUBREG flags. */
1295 check_promoted_subreg (insn, PATTERN (insn));
1297 /* See if we can find hardregs and subreg of pseudos in
1298 narrower modes. This could help turning TRUNCATEs
1299 into SUBREGs. */
1300 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1302 /* Try this insn with each insn it links back to. */
1304 FOR_EACH_LOG_LINK (links, insn)
1305 if ((next = try_combine (insn, links->insn, NULL,
1306 NULL, &new_direct_jump_p,
1307 last_combined_insn)) != 0)
1309 statistics_counter_event (cfun, "two-insn combine", 1);
1310 goto retry;
1313 /* Try each sequence of three linked insns ending with this one. */
1315 if (max_combine >= 3)
1316 FOR_EACH_LOG_LINK (links, insn)
1318 rtx_insn *link = links->insn;
1320 /* If the linked insn has been replaced by a note, then there
1321 is no point in pursuing this chain any further. */
1322 if (NOTE_P (link))
1323 continue;
1325 FOR_EACH_LOG_LINK (nextlinks, link)
1326 if ((next = try_combine (insn, link, nextlinks->insn,
1327 NULL, &new_direct_jump_p,
1328 last_combined_insn)) != 0)
1330 statistics_counter_event (cfun, "three-insn combine", 1);
1331 goto retry;
1335 #if HAVE_cc0
1336 /* Try to combine a jump insn that uses CC0
1337 with a preceding insn that sets CC0, and maybe with its
1338 logical predecessor as well.
1339 This is how we make decrement-and-branch insns.
1340 We need this special code because data flow connections
1341 via CC0 do not get entered in LOG_LINKS. */
1343 if (JUMP_P (insn)
1344 && (prev = prev_nonnote_insn (insn)) != 0
1345 && NONJUMP_INSN_P (prev)
1346 && sets_cc0_p (PATTERN (prev)))
1348 if ((next = try_combine (insn, prev, NULL, NULL,
1349 &new_direct_jump_p,
1350 last_combined_insn)) != 0)
1351 goto retry;
1353 FOR_EACH_LOG_LINK (nextlinks, prev)
1354 if ((next = try_combine (insn, prev, nextlinks->insn,
1355 NULL, &new_direct_jump_p,
1356 last_combined_insn)) != 0)
1357 goto retry;
1360 /* Do the same for an insn that explicitly references CC0. */
1361 if (NONJUMP_INSN_P (insn)
1362 && (prev = prev_nonnote_insn (insn)) != 0
1363 && NONJUMP_INSN_P (prev)
1364 && sets_cc0_p (PATTERN (prev))
1365 && GET_CODE (PATTERN (insn)) == SET
1366 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1368 if ((next = try_combine (insn, prev, NULL, NULL,
1369 &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1373 FOR_EACH_LOG_LINK (nextlinks, prev)
1374 if ((next = try_combine (insn, prev, nextlinks->insn,
1375 NULL, &new_direct_jump_p,
1376 last_combined_insn)) != 0)
1377 goto retry;
1380 /* Finally, see if any of the insns that this insn links to
1381 explicitly references CC0. If so, try this insn, that insn,
1382 and its predecessor if it sets CC0. */
1383 FOR_EACH_LOG_LINK (links, insn)
1384 if (NONJUMP_INSN_P (links->insn)
1385 && GET_CODE (PATTERN (links->insn)) == SET
1386 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1387 && (prev = prev_nonnote_insn (links->insn)) != 0
1388 && NONJUMP_INSN_P (prev)
1389 && sets_cc0_p (PATTERN (prev))
1390 && (next = try_combine (insn, links->insn,
1391 prev, NULL, &new_direct_jump_p,
1392 last_combined_insn)) != 0)
1393 goto retry;
1394 #endif
1396 /* Try combining an insn with two different insns whose results it
1397 uses. */
1398 if (max_combine >= 3)
1399 FOR_EACH_LOG_LINK (links, insn)
1400 for (nextlinks = links->next; nextlinks;
1401 nextlinks = nextlinks->next)
1402 if ((next = try_combine (insn, links->insn,
1403 nextlinks->insn, NULL,
1404 &new_direct_jump_p,
1405 last_combined_insn)) != 0)
1408 statistics_counter_event (cfun, "three-insn combine", 1);
1409 goto retry;
1412 /* Try four-instruction combinations. */
1413 if (max_combine >= 4)
1414 FOR_EACH_LOG_LINK (links, insn)
1416 struct insn_link *next1;
1417 rtx_insn *link = links->insn;
1419 /* If the linked insn has been replaced by a note, then there
1420 is no point in pursuing this chain any further. */
1421 if (NOTE_P (link))
1422 continue;
1424 FOR_EACH_LOG_LINK (next1, link)
1426 rtx_insn *link1 = next1->insn;
1427 if (NOTE_P (link1))
1428 continue;
1429 /* I0 -> I1 -> I2 -> I3. */
1430 FOR_EACH_LOG_LINK (nextlinks, link1)
1431 if ((next = try_combine (insn, link, link1,
1432 nextlinks->insn,
1433 &new_direct_jump_p,
1434 last_combined_insn)) != 0)
1436 statistics_counter_event (cfun, "four-insn combine", 1);
1437 goto retry;
1439 /* I0, I1 -> I2, I2 -> I3. */
1440 for (nextlinks = next1->next; nextlinks;
1441 nextlinks = nextlinks->next)
1442 if ((next = try_combine (insn, link, link1,
1443 nextlinks->insn,
1444 &new_direct_jump_p,
1445 last_combined_insn)) != 0)
1447 statistics_counter_event (cfun, "four-insn combine", 1);
1448 goto retry;
1452 for (next1 = links->next; next1; next1 = next1->next)
1454 rtx_insn *link1 = next1->insn;
1455 if (NOTE_P (link1))
1456 continue;
1457 /* I0 -> I2; I1, I2 -> I3. */
1458 FOR_EACH_LOG_LINK (nextlinks, link)
1459 if ((next = try_combine (insn, link, link1,
1460 nextlinks->insn,
1461 &new_direct_jump_p,
1462 last_combined_insn)) != 0)
1464 statistics_counter_event (cfun, "four-insn combine", 1);
1465 goto retry;
1467 /* I0 -> I1; I1, I2 -> I3. */
1468 FOR_EACH_LOG_LINK (nextlinks, link1)
1469 if ((next = try_combine (insn, link, link1,
1470 nextlinks->insn,
1471 &new_direct_jump_p,
1472 last_combined_insn)) != 0)
1474 statistics_counter_event (cfun, "four-insn combine", 1);
1475 goto retry;
1480 /* Try this insn with each REG_EQUAL note it links back to. */
1481 FOR_EACH_LOG_LINK (links, insn)
1483 rtx set, note;
1484 rtx_insn *temp = links->insn;
1485 if ((set = single_set (temp)) != 0
1486 && (note = find_reg_equal_equiv_note (temp)) != 0
1487 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1488 /* Avoid using a register that may already been marked
1489 dead by an earlier instruction. */
1490 && ! unmentioned_reg_p (note, SET_SRC (set))
1491 && (GET_MODE (note) == VOIDmode
1492 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1493 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1495 /* Temporarily replace the set's source with the
1496 contents of the REG_EQUAL note. The insn will
1497 be deleted or recognized by try_combine. */
1498 rtx orig = SET_SRC (set);
1499 SET_SRC (set) = note;
1500 i2mod = temp;
1501 i2mod_old_rhs = copy_rtx (orig);
1502 i2mod_new_rhs = copy_rtx (note);
1503 next = try_combine (insn, i2mod, NULL, NULL,
1504 &new_direct_jump_p,
1505 last_combined_insn);
1506 i2mod = NULL;
1507 if (next)
1509 statistics_counter_event (cfun, "insn-with-note combine", 1);
1510 goto retry;
1512 SET_SRC (set) = orig;
1516 if (!NOTE_P (insn))
1517 record_dead_and_set_regs (insn);
1519 retry:
1524 default_rtl_profile ();
1525 clear_bb_flags ();
1526 new_direct_jump_p |= purge_all_dead_edges ();
1527 delete_noop_moves ();
1529 /* Clean up. */
1530 obstack_free (&insn_link_obstack, NULL);
1531 free (uid_log_links);
1532 free (uid_insn_cost);
1533 reg_stat.release ();
1536 struct undo *undo, *next;
1537 for (undo = undobuf.frees; undo; undo = next)
1539 next = undo->next;
1540 free (undo);
1542 undobuf.frees = 0;
1545 total_attempts += combine_attempts;
1546 total_merges += combine_merges;
1547 total_extras += combine_extras;
1548 total_successes += combine_successes;
1550 nonzero_sign_valid = 0;
1551 rtl_hooks = general_rtl_hooks;
1553 /* Make recognizer allow volatile MEMs again. */
1554 init_recog ();
1556 return new_direct_jump_p;
1559 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1561 static void
1562 init_reg_last (void)
1564 unsigned int i;
1565 reg_stat_type *p;
1567 FOR_EACH_VEC_ELT (reg_stat, i, p)
1568 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1571 /* Set up any promoted values for incoming argument registers. */
1573 static void
1574 setup_incoming_promotions (rtx_insn *first)
1576 tree arg;
1577 bool strictly_local = false;
1579 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1580 arg = DECL_CHAIN (arg))
1582 rtx x, reg = DECL_INCOMING_RTL (arg);
1583 int uns1, uns3;
1584 machine_mode mode1, mode2, mode3, mode4;
1586 /* Only continue if the incoming argument is in a register. */
1587 if (!REG_P (reg))
1588 continue;
1590 /* Determine, if possible, whether all call sites of the current
1591 function lie within the current compilation unit. (This does
1592 take into account the exporting of a function via taking its
1593 address, and so forth.) */
1594 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1596 /* The mode and signedness of the argument before any promotions happen
1597 (equal to the mode of the pseudo holding it at that stage). */
1598 mode1 = TYPE_MODE (TREE_TYPE (arg));
1599 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1601 /* The mode and signedness of the argument after any source language and
1602 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1603 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1604 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1606 /* The mode and signedness of the argument as it is actually passed,
1607 see assign_parm_setup_reg in function.c. */
1608 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1609 TREE_TYPE (cfun->decl), 0);
1611 /* The mode of the register in which the argument is being passed. */
1612 mode4 = GET_MODE (reg);
1614 /* Eliminate sign extensions in the callee when:
1615 (a) A mode promotion has occurred; */
1616 if (mode1 == mode3)
1617 continue;
1618 /* (b) The mode of the register is the same as the mode of
1619 the argument as it is passed; */
1620 if (mode3 != mode4)
1621 continue;
1622 /* (c) There's no language level extension; */
1623 if (mode1 == mode2)
1625 /* (c.1) All callers are from the current compilation unit. If that's
1626 the case we don't have to rely on an ABI, we only have to know
1627 what we're generating right now, and we know that we will do the
1628 mode1 to mode2 promotion with the given sign. */
1629 else if (!strictly_local)
1630 continue;
1631 /* (c.2) The combination of the two promotions is useful. This is
1632 true when the signs match, or if the first promotion is unsigned.
1633 In the later case, (sign_extend (zero_extend x)) is the same as
1634 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1635 else if (uns1)
1636 uns3 = true;
1637 else if (uns3)
1638 continue;
1640 /* Record that the value was promoted from mode1 to mode3,
1641 so that any sign extension at the head of the current
1642 function may be eliminated. */
1643 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1644 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1645 record_value_for_reg (reg, first, x);
1649 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1650 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1651 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1652 because some machines (maybe most) will actually do the sign-extension and
1653 this is the conservative approach.
1655 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1656 kludge. */
1658 static rtx
1659 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1661 if (GET_MODE_PRECISION (mode) < prec
1662 && CONST_INT_P (src)
1663 && INTVAL (src) > 0
1664 && val_signbit_known_set_p (mode, INTVAL (src)))
1665 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (mode));
1667 return src;
1669 #endif
1671 /* Called via note_stores. If X is a pseudo that is narrower than
1672 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1674 If we are setting only a portion of X and we can't figure out what
1675 portion, assume all bits will be used since we don't know what will
1676 be happening.
1678 Similarly, set how many bits of X are known to be copies of the sign bit
1679 at all locations in the function. This is the smallest number implied
1680 by any set of X. */
1682 static void
1683 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1685 rtx_insn *insn = (rtx_insn *) data;
1686 unsigned int num;
1688 if (REG_P (x)
1689 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1690 /* If this register is undefined at the start of the file, we can't
1691 say what its contents were. */
1692 && ! REGNO_REG_SET_P
1693 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1694 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1696 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1698 if (set == 0 || GET_CODE (set) == CLOBBER)
1700 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1701 rsp->sign_bit_copies = 1;
1702 return;
1705 /* If this register is being initialized using itself, and the
1706 register is uninitialized in this basic block, and there are
1707 no LOG_LINKS which set the register, then part of the
1708 register is uninitialized. In that case we can't assume
1709 anything about the number of nonzero bits.
1711 ??? We could do better if we checked this in
1712 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1713 could avoid making assumptions about the insn which initially
1714 sets the register, while still using the information in other
1715 insns. We would have to be careful to check every insn
1716 involved in the combination. */
1718 if (insn
1719 && reg_referenced_p (x, PATTERN (insn))
1720 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1721 REGNO (x)))
1723 struct insn_link *link;
1725 FOR_EACH_LOG_LINK (link, insn)
1726 if (dead_or_set_p (link->insn, x))
1727 break;
1728 if (!link)
1730 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1731 rsp->sign_bit_copies = 1;
1732 return;
1736 /* If this is a complex assignment, see if we can convert it into a
1737 simple assignment. */
1738 set = expand_field_assignment (set);
1740 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1741 set what we know about X. */
1743 if (SET_DEST (set) == x
1744 || (paradoxical_subreg_p (SET_DEST (set))
1745 && SUBREG_REG (SET_DEST (set)) == x))
1747 rtx src = SET_SRC (set);
1749 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1750 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1751 #endif
1753 /* Don't call nonzero_bits if it cannot change anything. */
1754 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1755 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1756 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1757 if (rsp->sign_bit_copies == 0
1758 || rsp->sign_bit_copies > num)
1759 rsp->sign_bit_copies = num;
1761 else
1763 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1764 rsp->sign_bit_copies = 1;
1769 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1770 optionally insns that were previously combined into I3 or that will be
1771 combined into the merger of INSN and I3. The order is PRED, PRED2,
1772 INSN, SUCC, SUCC2, I3.
1774 Return 0 if the combination is not allowed for any reason.
1776 If the combination is allowed, *PDEST will be set to the single
1777 destination of INSN and *PSRC to the single source, and this function
1778 will return 1. */
1780 static int
1781 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1782 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1783 rtx *pdest, rtx *psrc)
1785 int i;
1786 const_rtx set = 0;
1787 rtx src, dest;
1788 rtx_insn *p;
1789 #ifdef AUTO_INC_DEC
1790 rtx link;
1791 #endif
1792 bool all_adjacent = true;
1793 int (*is_volatile_p) (const_rtx);
1795 if (succ)
1797 if (succ2)
1799 if (next_active_insn (succ2) != i3)
1800 all_adjacent = false;
1801 if (next_active_insn (succ) != succ2)
1802 all_adjacent = false;
1804 else if (next_active_insn (succ) != i3)
1805 all_adjacent = false;
1806 if (next_active_insn (insn) != succ)
1807 all_adjacent = false;
1809 else if (next_active_insn (insn) != i3)
1810 all_adjacent = false;
1812 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1813 or a PARALLEL consisting of such a SET and CLOBBERs.
1815 If INSN has CLOBBER parallel parts, ignore them for our processing.
1816 By definition, these happen during the execution of the insn. When it
1817 is merged with another insn, all bets are off. If they are, in fact,
1818 needed and aren't also supplied in I3, they may be added by
1819 recog_for_combine. Otherwise, it won't match.
1821 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1822 note.
1824 Get the source and destination of INSN. If more than one, can't
1825 combine. */
1827 if (GET_CODE (PATTERN (insn)) == SET)
1828 set = PATTERN (insn);
1829 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1830 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1832 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1834 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1836 switch (GET_CODE (elt))
1838 /* This is important to combine floating point insns
1839 for the SH4 port. */
1840 case USE:
1841 /* Combining an isolated USE doesn't make sense.
1842 We depend here on combinable_i3pat to reject them. */
1843 /* The code below this loop only verifies that the inputs of
1844 the SET in INSN do not change. We call reg_set_between_p
1845 to verify that the REG in the USE does not change between
1846 I3 and INSN.
1847 If the USE in INSN was for a pseudo register, the matching
1848 insn pattern will likely match any register; combining this
1849 with any other USE would only be safe if we knew that the
1850 used registers have identical values, or if there was
1851 something to tell them apart, e.g. different modes. For
1852 now, we forgo such complicated tests and simply disallow
1853 combining of USES of pseudo registers with any other USE. */
1854 if (REG_P (XEXP (elt, 0))
1855 && GET_CODE (PATTERN (i3)) == PARALLEL)
1857 rtx i3pat = PATTERN (i3);
1858 int i = XVECLEN (i3pat, 0) - 1;
1859 unsigned int regno = REGNO (XEXP (elt, 0));
1863 rtx i3elt = XVECEXP (i3pat, 0, i);
1865 if (GET_CODE (i3elt) == USE
1866 && REG_P (XEXP (i3elt, 0))
1867 && (REGNO (XEXP (i3elt, 0)) == regno
1868 ? reg_set_between_p (XEXP (elt, 0),
1869 PREV_INSN (insn), i3)
1870 : regno >= FIRST_PSEUDO_REGISTER))
1871 return 0;
1873 while (--i >= 0);
1875 break;
1877 /* We can ignore CLOBBERs. */
1878 case CLOBBER:
1879 break;
1881 case SET:
1882 /* Ignore SETs whose result isn't used but not those that
1883 have side-effects. */
1884 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1885 && insn_nothrow_p (insn)
1886 && !side_effects_p (elt))
1887 break;
1889 /* If we have already found a SET, this is a second one and
1890 so we cannot combine with this insn. */
1891 if (set)
1892 return 0;
1894 set = elt;
1895 break;
1897 default:
1898 /* Anything else means we can't combine. */
1899 return 0;
1903 if (set == 0
1904 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1905 so don't do anything with it. */
1906 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1907 return 0;
1909 else
1910 return 0;
1912 if (set == 0)
1913 return 0;
1915 /* The simplification in expand_field_assignment may call back to
1916 get_last_value, so set safe guard here. */
1917 subst_low_luid = DF_INSN_LUID (insn);
1919 set = expand_field_assignment (set);
1920 src = SET_SRC (set), dest = SET_DEST (set);
1922 /* Do not eliminate user-specified register if it is in an
1923 asm input because we may break the register asm usage defined
1924 in GCC manual if allow to do so.
1925 Be aware that this may cover more cases than we expect but this
1926 should be harmless. */
1927 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1928 && extract_asm_operands (PATTERN (i3)))
1929 return 0;
1931 /* Don't eliminate a store in the stack pointer. */
1932 if (dest == stack_pointer_rtx
1933 /* Don't combine with an insn that sets a register to itself if it has
1934 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1935 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1936 /* Can't merge an ASM_OPERANDS. */
1937 || GET_CODE (src) == ASM_OPERANDS
1938 /* Can't merge a function call. */
1939 || GET_CODE (src) == CALL
1940 /* Don't eliminate a function call argument. */
1941 || (CALL_P (i3)
1942 && (find_reg_fusage (i3, USE, dest)
1943 || (REG_P (dest)
1944 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1945 && global_regs[REGNO (dest)])))
1946 /* Don't substitute into an incremented register. */
1947 || FIND_REG_INC_NOTE (i3, dest)
1948 || (succ && FIND_REG_INC_NOTE (succ, dest))
1949 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1950 /* Don't substitute into a non-local goto, this confuses CFG. */
1951 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1952 /* Make sure that DEST is not used after SUCC but before I3. */
1953 || (!all_adjacent
1954 && ((succ2
1955 && (reg_used_between_p (dest, succ2, i3)
1956 || reg_used_between_p (dest, succ, succ2)))
1957 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1958 /* Make sure that the value that is to be substituted for the register
1959 does not use any registers whose values alter in between. However,
1960 If the insns are adjacent, a use can't cross a set even though we
1961 think it might (this can happen for a sequence of insns each setting
1962 the same destination; last_set of that register might point to
1963 a NOTE). If INSN has a REG_EQUIV note, the register is always
1964 equivalent to the memory so the substitution is valid even if there
1965 are intervening stores. Also, don't move a volatile asm or
1966 UNSPEC_VOLATILE across any other insns. */
1967 || (! all_adjacent
1968 && (((!MEM_P (src)
1969 || ! find_reg_note (insn, REG_EQUIV, src))
1970 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1971 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1972 || GET_CODE (src) == UNSPEC_VOLATILE))
1973 /* Don't combine across a CALL_INSN, because that would possibly
1974 change whether the life span of some REGs crosses calls or not,
1975 and it is a pain to update that information.
1976 Exception: if source is a constant, moving it later can't hurt.
1977 Accept that as a special case. */
1978 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1979 return 0;
1981 /* DEST must either be a REG or CC0. */
1982 if (REG_P (dest))
1984 /* If register alignment is being enforced for multi-word items in all
1985 cases except for parameters, it is possible to have a register copy
1986 insn referencing a hard register that is not allowed to contain the
1987 mode being copied and which would not be valid as an operand of most
1988 insns. Eliminate this problem by not combining with such an insn.
1990 Also, on some machines we don't want to extend the life of a hard
1991 register. */
1993 if (REG_P (src)
1994 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1995 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1996 /* Don't extend the life of a hard register unless it is
1997 user variable (if we have few registers) or it can't
1998 fit into the desired register (meaning something special
1999 is going on).
2000 Also avoid substituting a return register into I3, because
2001 reload can't handle a conflict with constraints of other
2002 inputs. */
2003 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2004 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
2005 return 0;
2007 else if (GET_CODE (dest) != CC0)
2008 return 0;
2011 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2012 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2013 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2015 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2017 /* If the clobber represents an earlyclobber operand, we must not
2018 substitute an expression containing the clobbered register.
2019 As we do not analyze the constraint strings here, we have to
2020 make the conservative assumption. However, if the register is
2021 a fixed hard reg, the clobber cannot represent any operand;
2022 we leave it up to the machine description to either accept or
2023 reject use-and-clobber patterns. */
2024 if (!REG_P (reg)
2025 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2026 || !fixed_regs[REGNO (reg)])
2027 if (reg_overlap_mentioned_p (reg, src))
2028 return 0;
2031 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2032 or not), reject, unless nothing volatile comes between it and I3 */
2034 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2036 /* Make sure neither succ nor succ2 contains a volatile reference. */
2037 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2038 return 0;
2039 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2040 return 0;
2041 /* We'll check insns between INSN and I3 below. */
2044 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2045 to be an explicit register variable, and was chosen for a reason. */
2047 if (GET_CODE (src) == ASM_OPERANDS
2048 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2049 return 0;
2051 /* If INSN contains volatile references (specifically volatile MEMs),
2052 we cannot combine across any other volatile references.
2053 Even if INSN doesn't contain volatile references, any intervening
2054 volatile insn might affect machine state. */
2056 is_volatile_p = volatile_refs_p (PATTERN (insn))
2057 ? volatile_refs_p
2058 : volatile_insn_p;
2060 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2061 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2062 return 0;
2064 /* If INSN contains an autoincrement or autodecrement, make sure that
2065 register is not used between there and I3, and not already used in
2066 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2067 Also insist that I3 not be a jump; if it were one
2068 and the incremented register were spilled, we would lose. */
2070 #ifdef AUTO_INC_DEC
2071 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2072 if (REG_NOTE_KIND (link) == REG_INC
2073 && (JUMP_P (i3)
2074 || reg_used_between_p (XEXP (link, 0), insn, i3)
2075 || (pred != NULL_RTX
2076 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2077 || (pred2 != NULL_RTX
2078 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2079 || (succ != NULL_RTX
2080 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2081 || (succ2 != NULL_RTX
2082 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2083 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2084 return 0;
2085 #endif
2087 /* Don't combine an insn that follows a CC0-setting insn.
2088 An insn that uses CC0 must not be separated from the one that sets it.
2089 We do, however, allow I2 to follow a CC0-setting insn if that insn
2090 is passed as I1; in that case it will be deleted also.
2091 We also allow combining in this case if all the insns are adjacent
2092 because that would leave the two CC0 insns adjacent as well.
2093 It would be more logical to test whether CC0 occurs inside I1 or I2,
2094 but that would be much slower, and this ought to be equivalent. */
2096 if (HAVE_cc0)
2098 p = prev_nonnote_insn (insn);
2099 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2100 && ! all_adjacent)
2101 return 0;
2104 /* If we get here, we have passed all the tests and the combination is
2105 to be allowed. */
2107 *pdest = dest;
2108 *psrc = src;
2110 return 1;
2113 /* LOC is the location within I3 that contains its pattern or the component
2114 of a PARALLEL of the pattern. We validate that it is valid for combining.
2116 One problem is if I3 modifies its output, as opposed to replacing it
2117 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2118 doing so would produce an insn that is not equivalent to the original insns.
2120 Consider:
2122 (set (reg:DI 101) (reg:DI 100))
2123 (set (subreg:SI (reg:DI 101) 0) <foo>)
2125 This is NOT equivalent to:
2127 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2128 (set (reg:DI 101) (reg:DI 100))])
2130 Not only does this modify 100 (in which case it might still be valid
2131 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2133 We can also run into a problem if I2 sets a register that I1
2134 uses and I1 gets directly substituted into I3 (not via I2). In that
2135 case, we would be getting the wrong value of I2DEST into I3, so we
2136 must reject the combination. This case occurs when I2 and I1 both
2137 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2138 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2139 of a SET must prevent combination from occurring. The same situation
2140 can occur for I0, in which case I0_NOT_IN_SRC is set.
2142 Before doing the above check, we first try to expand a field assignment
2143 into a set of logical operations.
2145 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2146 we place a register that is both set and used within I3. If more than one
2147 such register is detected, we fail.
2149 Return 1 if the combination is valid, zero otherwise. */
2151 static int
2152 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2153 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2155 rtx x = *loc;
2157 if (GET_CODE (x) == SET)
2159 rtx set = x ;
2160 rtx dest = SET_DEST (set);
2161 rtx src = SET_SRC (set);
2162 rtx inner_dest = dest;
2163 rtx subdest;
2165 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2166 || GET_CODE (inner_dest) == SUBREG
2167 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2168 inner_dest = XEXP (inner_dest, 0);
2170 /* Check for the case where I3 modifies its output, as discussed
2171 above. We don't want to prevent pseudos from being combined
2172 into the address of a MEM, so only prevent the combination if
2173 i1 or i2 set the same MEM. */
2174 if ((inner_dest != dest &&
2175 (!MEM_P (inner_dest)
2176 || rtx_equal_p (i2dest, inner_dest)
2177 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2178 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2179 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2180 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2181 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2183 /* This is the same test done in can_combine_p except we can't test
2184 all_adjacent; we don't have to, since this instruction will stay
2185 in place, thus we are not considering increasing the lifetime of
2186 INNER_DEST.
2188 Also, if this insn sets a function argument, combining it with
2189 something that might need a spill could clobber a previous
2190 function argument; the all_adjacent test in can_combine_p also
2191 checks this; here, we do a more specific test for this case. */
2193 || (REG_P (inner_dest)
2194 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2195 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2196 GET_MODE (inner_dest))))
2197 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2198 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2199 return 0;
2201 /* If DEST is used in I3, it is being killed in this insn, so
2202 record that for later. We have to consider paradoxical
2203 subregs here, since they kill the whole register, but we
2204 ignore partial subregs, STRICT_LOW_PART, etc.
2205 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2206 STACK_POINTER_REGNUM, since these are always considered to be
2207 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2208 subdest = dest;
2209 if (GET_CODE (subdest) == SUBREG
2210 && (GET_MODE_SIZE (GET_MODE (subdest))
2211 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2212 subdest = SUBREG_REG (subdest);
2213 if (pi3dest_killed
2214 && REG_P (subdest)
2215 && reg_referenced_p (subdest, PATTERN (i3))
2216 && REGNO (subdest) != FRAME_POINTER_REGNUM
2217 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2218 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2219 #endif
2220 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2221 && (REGNO (subdest) != ARG_POINTER_REGNUM
2222 || ! fixed_regs [REGNO (subdest)])
2223 #endif
2224 && REGNO (subdest) != STACK_POINTER_REGNUM)
2226 if (*pi3dest_killed)
2227 return 0;
2229 *pi3dest_killed = subdest;
2233 else if (GET_CODE (x) == PARALLEL)
2235 int i;
2237 for (i = 0; i < XVECLEN (x, 0); i++)
2238 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2239 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2240 return 0;
2243 return 1;
2246 /* Return 1 if X is an arithmetic expression that contains a multiplication
2247 and division. We don't count multiplications by powers of two here. */
2249 static int
2250 contains_muldiv (rtx x)
2252 switch (GET_CODE (x))
2254 case MOD: case DIV: case UMOD: case UDIV:
2255 return 1;
2257 case MULT:
2258 return ! (CONST_INT_P (XEXP (x, 1))
2259 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2260 default:
2261 if (BINARY_P (x))
2262 return contains_muldiv (XEXP (x, 0))
2263 || contains_muldiv (XEXP (x, 1));
2265 if (UNARY_P (x))
2266 return contains_muldiv (XEXP (x, 0));
2268 return 0;
2272 /* Determine whether INSN can be used in a combination. Return nonzero if
2273 not. This is used in try_combine to detect early some cases where we
2274 can't perform combinations. */
2276 static int
2277 cant_combine_insn_p (rtx_insn *insn)
2279 rtx set;
2280 rtx src, dest;
2282 /* If this isn't really an insn, we can't do anything.
2283 This can occur when flow deletes an insn that it has merged into an
2284 auto-increment address. */
2285 if (! INSN_P (insn))
2286 return 1;
2288 /* Never combine loads and stores involving hard regs that are likely
2289 to be spilled. The register allocator can usually handle such
2290 reg-reg moves by tying. If we allow the combiner to make
2291 substitutions of likely-spilled regs, reload might die.
2292 As an exception, we allow combinations involving fixed regs; these are
2293 not available to the register allocator so there's no risk involved. */
2295 set = single_set (insn);
2296 if (! set)
2297 return 0;
2298 src = SET_SRC (set);
2299 dest = SET_DEST (set);
2300 if (GET_CODE (src) == SUBREG)
2301 src = SUBREG_REG (src);
2302 if (GET_CODE (dest) == SUBREG)
2303 dest = SUBREG_REG (dest);
2304 if (REG_P (src) && REG_P (dest)
2305 && ((HARD_REGISTER_P (src)
2306 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2307 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2308 || (HARD_REGISTER_P (dest)
2309 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2310 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2311 return 1;
2313 return 0;
2316 struct likely_spilled_retval_info
2318 unsigned regno, nregs;
2319 unsigned mask;
2322 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2323 hard registers that are known to be written to / clobbered in full. */
2324 static void
2325 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2327 struct likely_spilled_retval_info *const info =
2328 (struct likely_spilled_retval_info *) data;
2329 unsigned regno, nregs;
2330 unsigned new_mask;
2332 if (!REG_P (XEXP (set, 0)))
2333 return;
2334 regno = REGNO (x);
2335 if (regno >= info->regno + info->nregs)
2336 return;
2337 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2338 if (regno + nregs <= info->regno)
2339 return;
2340 new_mask = (2U << (nregs - 1)) - 1;
2341 if (regno < info->regno)
2342 new_mask >>= info->regno - regno;
2343 else
2344 new_mask <<= regno - info->regno;
2345 info->mask &= ~new_mask;
2348 /* Return nonzero iff part of the return value is live during INSN, and
2349 it is likely spilled. This can happen when more than one insn is needed
2350 to copy the return value, e.g. when we consider to combine into the
2351 second copy insn for a complex value. */
2353 static int
2354 likely_spilled_retval_p (rtx_insn *insn)
2356 rtx_insn *use = BB_END (this_basic_block);
2357 rtx reg;
2358 rtx_insn *p;
2359 unsigned regno, nregs;
2360 /* We assume here that no machine mode needs more than
2361 32 hard registers when the value overlaps with a register
2362 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2363 unsigned mask;
2364 struct likely_spilled_retval_info info;
2366 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2367 return 0;
2368 reg = XEXP (PATTERN (use), 0);
2369 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2370 return 0;
2371 regno = REGNO (reg);
2372 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2373 if (nregs == 1)
2374 return 0;
2375 mask = (2U << (nregs - 1)) - 1;
2377 /* Disregard parts of the return value that are set later. */
2378 info.regno = regno;
2379 info.nregs = nregs;
2380 info.mask = mask;
2381 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2382 if (INSN_P (p))
2383 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2384 mask = info.mask;
2386 /* Check if any of the (probably) live return value registers is
2387 likely spilled. */
2388 nregs --;
2391 if ((mask & 1 << nregs)
2392 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2393 return 1;
2394 } while (nregs--);
2395 return 0;
2398 /* Adjust INSN after we made a change to its destination.
2400 Changing the destination can invalidate notes that say something about
2401 the results of the insn and a LOG_LINK pointing to the insn. */
2403 static void
2404 adjust_for_new_dest (rtx_insn *insn)
2406 /* For notes, be conservative and simply remove them. */
2407 remove_reg_equal_equiv_notes (insn);
2409 /* The new insn will have a destination that was previously the destination
2410 of an insn just above it. Call distribute_links to make a LOG_LINK from
2411 the next use of that destination. */
2413 rtx set = single_set (insn);
2414 gcc_assert (set);
2416 rtx reg = SET_DEST (set);
2418 while (GET_CODE (reg) == ZERO_EXTRACT
2419 || GET_CODE (reg) == STRICT_LOW_PART
2420 || GET_CODE (reg) == SUBREG)
2421 reg = XEXP (reg, 0);
2422 gcc_assert (REG_P (reg));
2424 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2426 df_insn_rescan (insn);
2429 /* Return TRUE if combine can reuse reg X in mode MODE.
2430 ADDED_SETS is nonzero if the original set is still required. */
2431 static bool
2432 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2434 unsigned int regno;
2436 if (!REG_P (x))
2437 return false;
2439 regno = REGNO (x);
2440 /* Allow hard registers if the new mode is legal, and occupies no more
2441 registers than the old mode. */
2442 if (regno < FIRST_PSEUDO_REGISTER)
2443 return (HARD_REGNO_MODE_OK (regno, mode)
2444 && (hard_regno_nregs[regno][GET_MODE (x)]
2445 >= hard_regno_nregs[regno][mode]));
2447 /* Or a pseudo that is only used once. */
2448 return (regno < reg_n_sets_max
2449 && REG_N_SETS (regno) == 1
2450 && !added_sets
2451 && !REG_USERVAR_P (x));
2455 /* Check whether X, the destination of a set, refers to part of
2456 the register specified by REG. */
2458 static bool
2459 reg_subword_p (rtx x, rtx reg)
2461 /* Check that reg is an integer mode register. */
2462 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2463 return false;
2465 if (GET_CODE (x) == STRICT_LOW_PART
2466 || GET_CODE (x) == ZERO_EXTRACT)
2467 x = XEXP (x, 0);
2469 return GET_CODE (x) == SUBREG
2470 && SUBREG_REG (x) == reg
2471 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2474 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2475 Note that the INSN should be deleted *after* removing dead edges, so
2476 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2477 but not for a (set (pc) (label_ref FOO)). */
2479 static void
2480 update_cfg_for_uncondjump (rtx_insn *insn)
2482 basic_block bb = BLOCK_FOR_INSN (insn);
2483 gcc_assert (BB_END (bb) == insn);
2485 purge_dead_edges (bb);
2487 delete_insn (insn);
2488 if (EDGE_COUNT (bb->succs) == 1)
2490 rtx_insn *insn;
2492 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2494 /* Remove barriers from the footer if there are any. */
2495 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2496 if (BARRIER_P (insn))
2498 if (PREV_INSN (insn))
2499 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2500 else
2501 BB_FOOTER (bb) = NEXT_INSN (insn);
2502 if (NEXT_INSN (insn))
2503 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2505 else if (LABEL_P (insn))
2506 break;
2510 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2511 by an arbitrary number of CLOBBERs. */
2512 static bool
2513 is_parallel_of_n_reg_sets (rtx pat, int n)
2515 if (GET_CODE (pat) != PARALLEL)
2516 return false;
2518 int len = XVECLEN (pat, 0);
2519 if (len < n)
2520 return false;
2522 int i;
2523 for (i = 0; i < n; i++)
2524 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2525 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2526 return false;
2527 for ( ; i < len; i++)
2528 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2529 return false;
2531 return true;
2534 #if !HAVE_cc0
2535 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2536 CLOBBERs), can be split into individual SETs in that order, without
2537 changing semantics. */
2538 static bool
2539 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2541 if (!insn_nothrow_p (insn))
2542 return false;
2544 rtx pat = PATTERN (insn);
2546 int i, j;
2547 for (i = 0; i < n; i++)
2549 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2550 return false;
2552 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2554 for (j = i + 1; j < n; j++)
2555 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2556 return false;
2559 return true;
2561 #endif
2563 /* Try to combine the insns I0, I1 and I2 into I3.
2564 Here I0, I1 and I2 appear earlier than I3.
2565 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2568 If we are combining more than two insns and the resulting insn is not
2569 recognized, try splitting it into two insns. If that happens, I2 and I3
2570 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2571 Otherwise, I0, I1 and I2 are pseudo-deleted.
2573 Return 0 if the combination does not work. Then nothing is changed.
2574 If we did the combination, return the insn at which combine should
2575 resume scanning.
2577 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2578 new direct jump instruction.
2580 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2581 been I3 passed to an earlier try_combine within the same basic
2582 block. */
2584 static rtx_insn *
2585 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2586 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2588 /* New patterns for I3 and I2, respectively. */
2589 rtx newpat, newi2pat = 0;
2590 rtvec newpat_vec_with_clobbers = 0;
2591 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2592 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2593 dead. */
2594 int added_sets_0, added_sets_1, added_sets_2;
2595 /* Total number of SETs to put into I3. */
2596 int total_sets;
2597 /* Nonzero if I2's or I1's body now appears in I3. */
2598 int i2_is_used = 0, i1_is_used = 0;
2599 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2600 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2601 /* Contains I3 if the destination of I3 is used in its source, which means
2602 that the old life of I3 is being killed. If that usage is placed into
2603 I2 and not in I3, a REG_DEAD note must be made. */
2604 rtx i3dest_killed = 0;
2605 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2606 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2607 /* Copy of SET_SRC of I1 and I0, if needed. */
2608 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2609 /* Set if I2DEST was reused as a scratch register. */
2610 bool i2scratch = false;
2611 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2612 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2613 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2614 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2615 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2616 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2617 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2618 /* Notes that must be added to REG_NOTES in I3 and I2. */
2619 rtx new_i3_notes, new_i2_notes;
2620 /* Notes that we substituted I3 into I2 instead of the normal case. */
2621 int i3_subst_into_i2 = 0;
2622 /* Notes that I1, I2 or I3 is a MULT operation. */
2623 int have_mult = 0;
2624 int swap_i2i3 = 0;
2625 int changed_i3_dest = 0;
2627 int maxreg;
2628 rtx_insn *temp_insn;
2629 rtx temp_expr;
2630 struct insn_link *link;
2631 rtx other_pat = 0;
2632 rtx new_other_notes;
2633 int i;
2635 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2636 never be). */
2637 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2638 return 0;
2640 /* Only try four-insn combinations when there's high likelihood of
2641 success. Look for simple insns, such as loads of constants or
2642 binary operations involving a constant. */
2643 if (i0)
2645 int i;
2646 int ngood = 0;
2647 int nshift = 0;
2648 rtx set0, set3;
2650 if (!flag_expensive_optimizations)
2651 return 0;
2653 for (i = 0; i < 4; i++)
2655 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2656 rtx set = single_set (insn);
2657 rtx src;
2658 if (!set)
2659 continue;
2660 src = SET_SRC (set);
2661 if (CONSTANT_P (src))
2663 ngood += 2;
2664 break;
2666 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2667 ngood++;
2668 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2669 || GET_CODE (src) == LSHIFTRT)
2670 nshift++;
2673 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2674 are likely manipulating its value. Ideally we'll be able to combine
2675 all four insns into a bitfield insertion of some kind.
2677 Note the source in I0 might be inside a sign/zero extension and the
2678 memory modes in I0 and I3 might be different. So extract the address
2679 from the destination of I3 and search for it in the source of I0.
2681 In the event that there's a match but the source/dest do not actually
2682 refer to the same memory, the worst that happens is we try some
2683 combinations that we wouldn't have otherwise. */
2684 if ((set0 = single_set (i0))
2685 /* Ensure the source of SET0 is a MEM, possibly buried inside
2686 an extension. */
2687 && (GET_CODE (SET_SRC (set0)) == MEM
2688 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2689 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2690 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2691 && (set3 = single_set (i3))
2692 /* Ensure the destination of SET3 is a MEM. */
2693 && GET_CODE (SET_DEST (set3)) == MEM
2694 /* Would it be better to extract the base address for the MEM
2695 in SET3 and look for that? I don't have cases where it matters
2696 but I could envision such cases. */
2697 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2698 ngood += 2;
2700 if (ngood < 2 && nshift < 2)
2701 return 0;
2704 /* Exit early if one of the insns involved can't be used for
2705 combinations. */
2706 if (CALL_P (i2)
2707 || (i1 && CALL_P (i1))
2708 || (i0 && CALL_P (i0))
2709 || cant_combine_insn_p (i3)
2710 || cant_combine_insn_p (i2)
2711 || (i1 && cant_combine_insn_p (i1))
2712 || (i0 && cant_combine_insn_p (i0))
2713 || likely_spilled_retval_p (i3))
2714 return 0;
2716 combine_attempts++;
2717 undobuf.other_insn = 0;
2719 /* Reset the hard register usage information. */
2720 CLEAR_HARD_REG_SET (newpat_used_regs);
2722 if (dump_file && (dump_flags & TDF_DETAILS))
2724 if (i0)
2725 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2726 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2727 else if (i1)
2728 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2729 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2730 else
2731 fprintf (dump_file, "\nTrying %d -> %d:\n",
2732 INSN_UID (i2), INSN_UID (i3));
2735 /* If multiple insns feed into one of I2 or I3, they can be in any
2736 order. To simplify the code below, reorder them in sequence. */
2737 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2738 temp_insn = i2, i2 = i0, i0 = temp_insn;
2739 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2740 temp_insn = i1, i1 = i0, i0 = temp_insn;
2741 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2742 temp_insn = i1, i1 = i2, i2 = temp_insn;
2744 added_links_insn = 0;
2746 /* First check for one important special case that the code below will
2747 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2748 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2749 we may be able to replace that destination with the destination of I3.
2750 This occurs in the common code where we compute both a quotient and
2751 remainder into a structure, in which case we want to do the computation
2752 directly into the structure to avoid register-register copies.
2754 Note that this case handles both multiple sets in I2 and also cases
2755 where I2 has a number of CLOBBERs inside the PARALLEL.
2757 We make very conservative checks below and only try to handle the
2758 most common cases of this. For example, we only handle the case
2759 where I2 and I3 are adjacent to avoid making difficult register
2760 usage tests. */
2762 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2763 && REG_P (SET_SRC (PATTERN (i3)))
2764 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2765 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2766 && GET_CODE (PATTERN (i2)) == PARALLEL
2767 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2768 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2769 below would need to check what is inside (and reg_overlap_mentioned_p
2770 doesn't support those codes anyway). Don't allow those destinations;
2771 the resulting insn isn't likely to be recognized anyway. */
2772 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2773 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2774 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2775 SET_DEST (PATTERN (i3)))
2776 && next_active_insn (i2) == i3)
2778 rtx p2 = PATTERN (i2);
2780 /* Make sure that the destination of I3,
2781 which we are going to substitute into one output of I2,
2782 is not used within another output of I2. We must avoid making this:
2783 (parallel [(set (mem (reg 69)) ...)
2784 (set (reg 69) ...)])
2785 which is not well-defined as to order of actions.
2786 (Besides, reload can't handle output reloads for this.)
2788 The problem can also happen if the dest of I3 is a memory ref,
2789 if another dest in I2 is an indirect memory ref. */
2790 for (i = 0; i < XVECLEN (p2, 0); i++)
2791 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2792 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2793 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2794 SET_DEST (XVECEXP (p2, 0, i))))
2795 break;
2797 /* Make sure this PARALLEL is not an asm. We do not allow combining
2798 that usually (see can_combine_p), so do not here either. */
2799 for (i = 0; i < XVECLEN (p2, 0); i++)
2800 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2801 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2802 break;
2804 if (i == XVECLEN (p2, 0))
2805 for (i = 0; i < XVECLEN (p2, 0); i++)
2806 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2807 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2809 combine_merges++;
2811 subst_insn = i3;
2812 subst_low_luid = DF_INSN_LUID (i2);
2814 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2815 i2src = SET_SRC (XVECEXP (p2, 0, i));
2816 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2817 i2dest_killed = dead_or_set_p (i2, i2dest);
2819 /* Replace the dest in I2 with our dest and make the resulting
2820 insn the new pattern for I3. Then skip to where we validate
2821 the pattern. Everything was set up above. */
2822 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2823 newpat = p2;
2824 i3_subst_into_i2 = 1;
2825 goto validate_replacement;
2829 /* If I2 is setting a pseudo to a constant and I3 is setting some
2830 sub-part of it to another constant, merge them by making a new
2831 constant. */
2832 if (i1 == 0
2833 && (temp_expr = single_set (i2)) != 0
2834 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2835 && GET_CODE (PATTERN (i3)) == SET
2836 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2837 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2839 rtx dest = SET_DEST (PATTERN (i3));
2840 int offset = -1;
2841 int width = 0;
2843 if (GET_CODE (dest) == ZERO_EXTRACT)
2845 if (CONST_INT_P (XEXP (dest, 1))
2846 && CONST_INT_P (XEXP (dest, 2)))
2848 width = INTVAL (XEXP (dest, 1));
2849 offset = INTVAL (XEXP (dest, 2));
2850 dest = XEXP (dest, 0);
2851 if (BITS_BIG_ENDIAN)
2852 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2855 else
2857 if (GET_CODE (dest) == STRICT_LOW_PART)
2858 dest = XEXP (dest, 0);
2859 width = GET_MODE_PRECISION (GET_MODE (dest));
2860 offset = 0;
2863 if (offset >= 0)
2865 /* If this is the low part, we're done. */
2866 if (subreg_lowpart_p (dest))
2868 /* Handle the case where inner is twice the size of outer. */
2869 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2870 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2871 offset += GET_MODE_PRECISION (GET_MODE (dest));
2872 /* Otherwise give up for now. */
2873 else
2874 offset = -1;
2877 if (offset >= 0)
2879 rtx inner = SET_SRC (PATTERN (i3));
2880 rtx outer = SET_SRC (temp_expr);
2882 wide_int o
2883 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2884 std::make_pair (inner, GET_MODE (dest)),
2885 offset, width);
2887 combine_merges++;
2888 subst_insn = i3;
2889 subst_low_luid = DF_INSN_LUID (i2);
2890 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2891 i2dest = SET_DEST (temp_expr);
2892 i2dest_killed = dead_or_set_p (i2, i2dest);
2894 /* Replace the source in I2 with the new constant and make the
2895 resulting insn the new pattern for I3. Then skip to where we
2896 validate the pattern. Everything was set up above. */
2897 SUBST (SET_SRC (temp_expr),
2898 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2900 newpat = PATTERN (i2);
2902 /* The dest of I3 has been replaced with the dest of I2. */
2903 changed_i3_dest = 1;
2904 goto validate_replacement;
2908 #if !HAVE_cc0
2909 /* If we have no I1 and I2 looks like:
2910 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2911 (set Y OP)])
2912 make up a dummy I1 that is
2913 (set Y OP)
2914 and change I2 to be
2915 (set (reg:CC X) (compare:CC Y (const_int 0)))
2917 (We can ignore any trailing CLOBBERs.)
2919 This undoes a previous combination and allows us to match a branch-and-
2920 decrement insn. */
2922 if (i1 == 0
2923 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2924 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2925 == MODE_CC)
2926 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2927 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2928 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2929 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2930 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2931 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2933 /* We make I1 with the same INSN_UID as I2. This gives it
2934 the same DF_INSN_LUID for value tracking. Our fake I1 will
2935 never appear in the insn stream so giving it the same INSN_UID
2936 as I2 will not cause a problem. */
2938 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2939 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2940 -1, NULL_RTX);
2941 INSN_UID (i1) = INSN_UID (i2);
2943 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2944 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2945 SET_DEST (PATTERN (i1)));
2946 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2947 SUBST_LINK (LOG_LINKS (i2),
2948 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2951 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2952 make those two SETs separate I1 and I2 insns, and make an I0 that is
2953 the original I1. */
2954 if (i0 == 0
2955 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2956 && can_split_parallel_of_n_reg_sets (i2, 2)
2957 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2958 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2960 /* If there is no I1, there is no I0 either. */
2961 i0 = i1;
2963 /* We make I1 with the same INSN_UID as I2. This gives it
2964 the same DF_INSN_LUID for value tracking. Our fake I1 will
2965 never appear in the insn stream so giving it the same INSN_UID
2966 as I2 will not cause a problem. */
2968 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2969 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2970 -1, NULL_RTX);
2971 INSN_UID (i1) = INSN_UID (i2);
2973 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2975 #endif
2977 /* Verify that I2 and I1 are valid for combining. */
2978 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2979 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2980 &i1dest, &i1src))
2981 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2982 &i0dest, &i0src)))
2984 undo_all ();
2985 return 0;
2988 /* Record whether I2DEST is used in I2SRC and similarly for the other
2989 cases. Knowing this will help in register status updating below. */
2990 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2991 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2992 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2993 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2994 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2995 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2996 i2dest_killed = dead_or_set_p (i2, i2dest);
2997 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2998 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3000 /* For the earlier insns, determine which of the subsequent ones they
3001 feed. */
3002 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3003 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3004 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3005 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3006 && reg_overlap_mentioned_p (i0dest, i2src))));
3008 /* Ensure that I3's pattern can be the destination of combines. */
3009 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3010 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3011 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3012 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3013 &i3dest_killed))
3015 undo_all ();
3016 return 0;
3019 /* See if any of the insns is a MULT operation. Unless one is, we will
3020 reject a combination that is, since it must be slower. Be conservative
3021 here. */
3022 if (GET_CODE (i2src) == MULT
3023 || (i1 != 0 && GET_CODE (i1src) == MULT)
3024 || (i0 != 0 && GET_CODE (i0src) == MULT)
3025 || (GET_CODE (PATTERN (i3)) == SET
3026 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3027 have_mult = 1;
3029 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3030 We used to do this EXCEPT in one case: I3 has a post-inc in an
3031 output operand. However, that exception can give rise to insns like
3032 mov r3,(r3)+
3033 which is a famous insn on the PDP-11 where the value of r3 used as the
3034 source was model-dependent. Avoid this sort of thing. */
3036 #if 0
3037 if (!(GET_CODE (PATTERN (i3)) == SET
3038 && REG_P (SET_SRC (PATTERN (i3)))
3039 && MEM_P (SET_DEST (PATTERN (i3)))
3040 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3041 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3042 /* It's not the exception. */
3043 #endif
3044 #ifdef AUTO_INC_DEC
3046 rtx link;
3047 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3048 if (REG_NOTE_KIND (link) == REG_INC
3049 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3050 || (i1 != 0
3051 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3053 undo_all ();
3054 return 0;
3057 #endif
3059 /* See if the SETs in I1 or I2 need to be kept around in the merged
3060 instruction: whenever the value set there is still needed past I3.
3061 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3063 For the SET in I1, we have two cases: if I1 and I2 independently feed
3064 into I3, the set in I1 needs to be kept around unless I1DEST dies
3065 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3066 in I1 needs to be kept around unless I1DEST dies or is set in either
3067 I2 or I3. The same considerations apply to I0. */
3069 added_sets_2 = !dead_or_set_p (i3, i2dest);
3071 if (i1)
3072 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3073 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3074 else
3075 added_sets_1 = 0;
3077 if (i0)
3078 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3079 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3080 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3081 && dead_or_set_p (i2, i0dest)));
3082 else
3083 added_sets_0 = 0;
3085 /* We are about to copy insns for the case where they need to be kept
3086 around. Check that they can be copied in the merged instruction. */
3088 if (targetm.cannot_copy_insn_p
3089 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3090 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3091 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3093 undo_all ();
3094 return 0;
3097 /* If the set in I2 needs to be kept around, we must make a copy of
3098 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3099 PATTERN (I2), we are only substituting for the original I1DEST, not into
3100 an already-substituted copy. This also prevents making self-referential
3101 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3102 I2DEST. */
3104 if (added_sets_2)
3106 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3107 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3108 else
3109 i2pat = copy_rtx (PATTERN (i2));
3112 if (added_sets_1)
3114 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3115 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3116 else
3117 i1pat = copy_rtx (PATTERN (i1));
3120 if (added_sets_0)
3122 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3123 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3124 else
3125 i0pat = copy_rtx (PATTERN (i0));
3128 combine_merges++;
3130 /* Substitute in the latest insn for the regs set by the earlier ones. */
3132 maxreg = max_reg_num ();
3134 subst_insn = i3;
3136 /* Many machines that don't use CC0 have insns that can both perform an
3137 arithmetic operation and set the condition code. These operations will
3138 be represented as a PARALLEL with the first element of the vector
3139 being a COMPARE of an arithmetic operation with the constant zero.
3140 The second element of the vector will set some pseudo to the result
3141 of the same arithmetic operation. If we simplify the COMPARE, we won't
3142 match such a pattern and so will generate an extra insn. Here we test
3143 for this case, where both the comparison and the operation result are
3144 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3145 I2SRC. Later we will make the PARALLEL that contains I2. */
3147 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3148 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3149 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3150 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3152 rtx newpat_dest;
3153 rtx *cc_use_loc = NULL;
3154 rtx_insn *cc_use_insn = NULL;
3155 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3156 machine_mode compare_mode, orig_compare_mode;
3157 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3159 newpat = PATTERN (i3);
3160 newpat_dest = SET_DEST (newpat);
3161 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3163 if (undobuf.other_insn == 0
3164 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3165 &cc_use_insn)))
3167 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3168 compare_code = simplify_compare_const (compare_code,
3169 GET_MODE (i2dest), op0, &op1);
3170 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3173 /* Do the rest only if op1 is const0_rtx, which may be the
3174 result of simplification. */
3175 if (op1 == const0_rtx)
3177 /* If a single use of the CC is found, prepare to modify it
3178 when SELECT_CC_MODE returns a new CC-class mode, or when
3179 the above simplify_compare_const() returned a new comparison
3180 operator. undobuf.other_insn is assigned the CC use insn
3181 when modifying it. */
3182 if (cc_use_loc)
3184 #ifdef SELECT_CC_MODE
3185 machine_mode new_mode
3186 = SELECT_CC_MODE (compare_code, op0, op1);
3187 if (new_mode != orig_compare_mode
3188 && can_change_dest_mode (SET_DEST (newpat),
3189 added_sets_2, new_mode))
3191 unsigned int regno = REGNO (newpat_dest);
3192 compare_mode = new_mode;
3193 if (regno < FIRST_PSEUDO_REGISTER)
3194 newpat_dest = gen_rtx_REG (compare_mode, regno);
3195 else
3197 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3198 newpat_dest = regno_reg_rtx[regno];
3201 #endif
3202 /* Cases for modifying the CC-using comparison. */
3203 if (compare_code != orig_compare_code
3204 /* ??? Do we need to verify the zero rtx? */
3205 && XEXP (*cc_use_loc, 1) == const0_rtx)
3207 /* Replace cc_use_loc with entire new RTX. */
3208 SUBST (*cc_use_loc,
3209 gen_rtx_fmt_ee (compare_code, compare_mode,
3210 newpat_dest, const0_rtx));
3211 undobuf.other_insn = cc_use_insn;
3213 else if (compare_mode != orig_compare_mode)
3215 /* Just replace the CC reg with a new mode. */
3216 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3217 undobuf.other_insn = cc_use_insn;
3221 /* Now we modify the current newpat:
3222 First, SET_DEST(newpat) is updated if the CC mode has been
3223 altered. For targets without SELECT_CC_MODE, this should be
3224 optimized away. */
3225 if (compare_mode != orig_compare_mode)
3226 SUBST (SET_DEST (newpat), newpat_dest);
3227 /* This is always done to propagate i2src into newpat. */
3228 SUBST (SET_SRC (newpat),
3229 gen_rtx_COMPARE (compare_mode, op0, op1));
3230 /* Create new version of i2pat if needed; the below PARALLEL
3231 creation needs this to work correctly. */
3232 if (! rtx_equal_p (i2src, op0))
3233 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3234 i2_is_used = 1;
3238 if (i2_is_used == 0)
3240 /* It is possible that the source of I2 or I1 may be performing
3241 an unneeded operation, such as a ZERO_EXTEND of something
3242 that is known to have the high part zero. Handle that case
3243 by letting subst look at the inner insns.
3245 Another way to do this would be to have a function that tries
3246 to simplify a single insn instead of merging two or more
3247 insns. We don't do this because of the potential of infinite
3248 loops and because of the potential extra memory required.
3249 However, doing it the way we are is a bit of a kludge and
3250 doesn't catch all cases.
3252 But only do this if -fexpensive-optimizations since it slows
3253 things down and doesn't usually win.
3255 This is not done in the COMPARE case above because the
3256 unmodified I2PAT is used in the PARALLEL and so a pattern
3257 with a modified I2SRC would not match. */
3259 if (flag_expensive_optimizations)
3261 /* Pass pc_rtx so no substitutions are done, just
3262 simplifications. */
3263 if (i1)
3265 subst_low_luid = DF_INSN_LUID (i1);
3266 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3269 subst_low_luid = DF_INSN_LUID (i2);
3270 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3273 n_occurrences = 0; /* `subst' counts here */
3274 subst_low_luid = DF_INSN_LUID (i2);
3276 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3277 copy of I2SRC each time we substitute it, in order to avoid creating
3278 self-referential RTL when we will be substituting I1SRC for I1DEST
3279 later. Likewise if I0 feeds into I2, either directly or indirectly
3280 through I1, and I0DEST is in I0SRC. */
3281 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3282 (i1_feeds_i2_n && i1dest_in_i1src)
3283 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3284 && i0dest_in_i0src));
3285 substed_i2 = 1;
3287 /* Record whether I2's body now appears within I3's body. */
3288 i2_is_used = n_occurrences;
3291 /* If we already got a failure, don't try to do more. Otherwise, try to
3292 substitute I1 if we have it. */
3294 if (i1 && GET_CODE (newpat) != CLOBBER)
3296 /* Check that an autoincrement side-effect on I1 has not been lost.
3297 This happens if I1DEST is mentioned in I2 and dies there, and
3298 has disappeared from the new pattern. */
3299 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3300 && i1_feeds_i2_n
3301 && dead_or_set_p (i2, i1dest)
3302 && !reg_overlap_mentioned_p (i1dest, newpat))
3303 /* Before we can do this substitution, we must redo the test done
3304 above (see detailed comments there) that ensures I1DEST isn't
3305 mentioned in any SETs in NEWPAT that are field assignments. */
3306 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3307 0, 0, 0))
3309 undo_all ();
3310 return 0;
3313 n_occurrences = 0;
3314 subst_low_luid = DF_INSN_LUID (i1);
3316 /* If the following substitution will modify I1SRC, make a copy of it
3317 for the case where it is substituted for I1DEST in I2PAT later. */
3318 if (added_sets_2 && i1_feeds_i2_n)
3319 i1src_copy = copy_rtx (i1src);
3321 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3322 copy of I1SRC each time we substitute it, in order to avoid creating
3323 self-referential RTL when we will be substituting I0SRC for I0DEST
3324 later. */
3325 newpat = subst (newpat, i1dest, i1src, 0, 0,
3326 i0_feeds_i1_n && i0dest_in_i0src);
3327 substed_i1 = 1;
3329 /* Record whether I1's body now appears within I3's body. */
3330 i1_is_used = n_occurrences;
3333 /* Likewise for I0 if we have it. */
3335 if (i0 && GET_CODE (newpat) != CLOBBER)
3337 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3338 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3339 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3340 && !reg_overlap_mentioned_p (i0dest, newpat))
3341 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3342 0, 0, 0))
3344 undo_all ();
3345 return 0;
3348 /* If the following substitution will modify I0SRC, make a copy of it
3349 for the case where it is substituted for I0DEST in I1PAT later. */
3350 if (added_sets_1 && i0_feeds_i1_n)
3351 i0src_copy = copy_rtx (i0src);
3352 /* And a copy for I0DEST in I2PAT substitution. */
3353 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3354 || (i0_feeds_i2_n)))
3355 i0src_copy2 = copy_rtx (i0src);
3357 n_occurrences = 0;
3358 subst_low_luid = DF_INSN_LUID (i0);
3359 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3360 substed_i0 = 1;
3363 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3364 to count all the ways that I2SRC and I1SRC can be used. */
3365 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3366 && i2_is_used + added_sets_2 > 1)
3367 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3368 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3369 > 1))
3370 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3371 && (n_occurrences + added_sets_0
3372 + (added_sets_1 && i0_feeds_i1_n)
3373 + (added_sets_2 && i0_feeds_i2_n)
3374 > 1))
3375 /* Fail if we tried to make a new register. */
3376 || max_reg_num () != maxreg
3377 /* Fail if we couldn't do something and have a CLOBBER. */
3378 || GET_CODE (newpat) == CLOBBER
3379 /* Fail if this new pattern is a MULT and we didn't have one before
3380 at the outer level. */
3381 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3382 && ! have_mult))
3384 undo_all ();
3385 return 0;
3388 /* If the actions of the earlier insns must be kept
3389 in addition to substituting them into the latest one,
3390 we must make a new PARALLEL for the latest insn
3391 to hold additional the SETs. */
3393 if (added_sets_0 || added_sets_1 || added_sets_2)
3395 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3396 combine_extras++;
3398 if (GET_CODE (newpat) == PARALLEL)
3400 rtvec old = XVEC (newpat, 0);
3401 total_sets = XVECLEN (newpat, 0) + extra_sets;
3402 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3403 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3404 sizeof (old->elem[0]) * old->num_elem);
3406 else
3408 rtx old = newpat;
3409 total_sets = 1 + extra_sets;
3410 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3411 XVECEXP (newpat, 0, 0) = old;
3414 if (added_sets_0)
3415 XVECEXP (newpat, 0, --total_sets) = i0pat;
3417 if (added_sets_1)
3419 rtx t = i1pat;
3420 if (i0_feeds_i1_n)
3421 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3423 XVECEXP (newpat, 0, --total_sets) = t;
3425 if (added_sets_2)
3427 rtx t = i2pat;
3428 if (i1_feeds_i2_n)
3429 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3430 i0_feeds_i1_n && i0dest_in_i0src);
3431 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3432 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3434 XVECEXP (newpat, 0, --total_sets) = t;
3438 validate_replacement:
3440 /* Note which hard regs this insn has as inputs. */
3441 mark_used_regs_combine (newpat);
3443 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3444 consider splitting this pattern, we might need these clobbers. */
3445 if (i1 && GET_CODE (newpat) == PARALLEL
3446 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3448 int len = XVECLEN (newpat, 0);
3450 newpat_vec_with_clobbers = rtvec_alloc (len);
3451 for (i = 0; i < len; i++)
3452 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3455 /* We have recognized nothing yet. */
3456 insn_code_number = -1;
3458 /* See if this is a PARALLEL of two SETs where one SET's destination is
3459 a register that is unused and this isn't marked as an instruction that
3460 might trap in an EH region. In that case, we just need the other SET.
3461 We prefer this over the PARALLEL.
3463 This can occur when simplifying a divmod insn. We *must* test for this
3464 case here because the code below that splits two independent SETs doesn't
3465 handle this case correctly when it updates the register status.
3467 It's pointless doing this if we originally had two sets, one from
3468 i3, and one from i2. Combining then splitting the parallel results
3469 in the original i2 again plus an invalid insn (which we delete).
3470 The net effect is only to move instructions around, which makes
3471 debug info less accurate. */
3473 if (!(added_sets_2 && i1 == 0)
3474 && is_parallel_of_n_reg_sets (newpat, 2)
3475 && asm_noperands (newpat) < 0)
3477 rtx set0 = XVECEXP (newpat, 0, 0);
3478 rtx set1 = XVECEXP (newpat, 0, 1);
3479 rtx oldpat = newpat;
3481 if (((REG_P (SET_DEST (set1))
3482 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3483 || (GET_CODE (SET_DEST (set1)) == SUBREG
3484 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3485 && insn_nothrow_p (i3)
3486 && !side_effects_p (SET_SRC (set1)))
3488 newpat = set0;
3489 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3492 else if (((REG_P (SET_DEST (set0))
3493 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3494 || (GET_CODE (SET_DEST (set0)) == SUBREG
3495 && find_reg_note (i3, REG_UNUSED,
3496 SUBREG_REG (SET_DEST (set0)))))
3497 && insn_nothrow_p (i3)
3498 && !side_effects_p (SET_SRC (set0)))
3500 newpat = set1;
3501 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3503 if (insn_code_number >= 0)
3504 changed_i3_dest = 1;
3507 if (insn_code_number < 0)
3508 newpat = oldpat;
3511 /* Is the result of combination a valid instruction? */
3512 if (insn_code_number < 0)
3513 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3515 /* If we were combining three insns and the result is a simple SET
3516 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3517 insns. There are two ways to do this. It can be split using a
3518 machine-specific method (like when you have an addition of a large
3519 constant) or by combine in the function find_split_point. */
3521 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3522 && asm_noperands (newpat) < 0)
3524 rtx parallel, *split;
3525 rtx_insn *m_split_insn;
3527 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3528 use I2DEST as a scratch register will help. In the latter case,
3529 convert I2DEST to the mode of the source of NEWPAT if we can. */
3531 m_split_insn = combine_split_insns (newpat, i3);
3533 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3534 inputs of NEWPAT. */
3536 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3537 possible to try that as a scratch reg. This would require adding
3538 more code to make it work though. */
3540 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3542 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3544 /* First try to split using the original register as a
3545 scratch register. */
3546 parallel = gen_rtx_PARALLEL (VOIDmode,
3547 gen_rtvec (2, newpat,
3548 gen_rtx_CLOBBER (VOIDmode,
3549 i2dest)));
3550 m_split_insn = combine_split_insns (parallel, i3);
3552 /* If that didn't work, try changing the mode of I2DEST if
3553 we can. */
3554 if (m_split_insn == 0
3555 && new_mode != GET_MODE (i2dest)
3556 && new_mode != VOIDmode
3557 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3559 machine_mode old_mode = GET_MODE (i2dest);
3560 rtx ni2dest;
3562 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3563 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3564 else
3566 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3567 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3570 parallel = (gen_rtx_PARALLEL
3571 (VOIDmode,
3572 gen_rtvec (2, newpat,
3573 gen_rtx_CLOBBER (VOIDmode,
3574 ni2dest))));
3575 m_split_insn = combine_split_insns (parallel, i3);
3577 if (m_split_insn == 0
3578 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3580 struct undo *buf;
3582 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3583 buf = undobuf.undos;
3584 undobuf.undos = buf->next;
3585 buf->next = undobuf.frees;
3586 undobuf.frees = buf;
3590 i2scratch = m_split_insn != 0;
3593 /* If recog_for_combine has discarded clobbers, try to use them
3594 again for the split. */
3595 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3597 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3598 m_split_insn = combine_split_insns (parallel, i3);
3601 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3603 rtx m_split_pat = PATTERN (m_split_insn);
3604 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3605 if (insn_code_number >= 0)
3606 newpat = m_split_pat;
3608 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3609 && (next_nonnote_nondebug_insn (i2) == i3
3610 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3612 rtx i2set, i3set;
3613 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3614 newi2pat = PATTERN (m_split_insn);
3616 i3set = single_set (NEXT_INSN (m_split_insn));
3617 i2set = single_set (m_split_insn);
3619 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3621 /* If I2 or I3 has multiple SETs, we won't know how to track
3622 register status, so don't use these insns. If I2's destination
3623 is used between I2 and I3, we also can't use these insns. */
3625 if (i2_code_number >= 0 && i2set && i3set
3626 && (next_nonnote_nondebug_insn (i2) == i3
3627 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3628 insn_code_number = recog_for_combine (&newi3pat, i3,
3629 &new_i3_notes);
3630 if (insn_code_number >= 0)
3631 newpat = newi3pat;
3633 /* It is possible that both insns now set the destination of I3.
3634 If so, we must show an extra use of it. */
3636 if (insn_code_number >= 0)
3638 rtx new_i3_dest = SET_DEST (i3set);
3639 rtx new_i2_dest = SET_DEST (i2set);
3641 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3642 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3643 || GET_CODE (new_i3_dest) == SUBREG)
3644 new_i3_dest = XEXP (new_i3_dest, 0);
3646 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3647 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3648 || GET_CODE (new_i2_dest) == SUBREG)
3649 new_i2_dest = XEXP (new_i2_dest, 0);
3651 if (REG_P (new_i3_dest)
3652 && REG_P (new_i2_dest)
3653 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3654 && REGNO (new_i2_dest) < reg_n_sets_max)
3655 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3659 /* If we can split it and use I2DEST, go ahead and see if that
3660 helps things be recognized. Verify that none of the registers
3661 are set between I2 and I3. */
3662 if (insn_code_number < 0
3663 && (split = find_split_point (&newpat, i3, false)) != 0
3664 && (!HAVE_cc0 || REG_P (i2dest))
3665 /* We need I2DEST in the proper mode. If it is a hard register
3666 or the only use of a pseudo, we can change its mode.
3667 Make sure we don't change a hard register to have a mode that
3668 isn't valid for it, or change the number of registers. */
3669 && (GET_MODE (*split) == GET_MODE (i2dest)
3670 || GET_MODE (*split) == VOIDmode
3671 || can_change_dest_mode (i2dest, added_sets_2,
3672 GET_MODE (*split)))
3673 && (next_nonnote_nondebug_insn (i2) == i3
3674 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3675 /* We can't overwrite I2DEST if its value is still used by
3676 NEWPAT. */
3677 && ! reg_referenced_p (i2dest, newpat))
3679 rtx newdest = i2dest;
3680 enum rtx_code split_code = GET_CODE (*split);
3681 machine_mode split_mode = GET_MODE (*split);
3682 bool subst_done = false;
3683 newi2pat = NULL_RTX;
3685 i2scratch = true;
3687 /* *SPLIT may be part of I2SRC, so make sure we have the
3688 original expression around for later debug processing.
3689 We should not need I2SRC any more in other cases. */
3690 if (MAY_HAVE_DEBUG_INSNS)
3691 i2src = copy_rtx (i2src);
3692 else
3693 i2src = NULL;
3695 /* Get NEWDEST as a register in the proper mode. We have already
3696 validated that we can do this. */
3697 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3699 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3700 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3701 else
3703 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3704 newdest = regno_reg_rtx[REGNO (i2dest)];
3708 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3709 an ASHIFT. This can occur if it was inside a PLUS and hence
3710 appeared to be a memory address. This is a kludge. */
3711 if (split_code == MULT
3712 && CONST_INT_P (XEXP (*split, 1))
3713 && INTVAL (XEXP (*split, 1)) > 0
3714 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3716 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3717 XEXP (*split, 0), GEN_INT (i)));
3718 /* Update split_code because we may not have a multiply
3719 anymore. */
3720 split_code = GET_CODE (*split);
3723 #ifdef INSN_SCHEDULING
3724 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3725 be written as a ZERO_EXTEND. */
3726 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3728 #ifdef LOAD_EXTEND_OP
3729 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3730 what it really is. */
3731 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3732 == SIGN_EXTEND)
3733 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3734 SUBREG_REG (*split)));
3735 else
3736 #endif
3737 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3738 SUBREG_REG (*split)));
3740 #endif
3742 /* Attempt to split binary operators using arithmetic identities. */
3743 if (BINARY_P (SET_SRC (newpat))
3744 && split_mode == GET_MODE (SET_SRC (newpat))
3745 && ! side_effects_p (SET_SRC (newpat)))
3747 rtx setsrc = SET_SRC (newpat);
3748 machine_mode mode = GET_MODE (setsrc);
3749 enum rtx_code code = GET_CODE (setsrc);
3750 rtx src_op0 = XEXP (setsrc, 0);
3751 rtx src_op1 = XEXP (setsrc, 1);
3753 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3754 if (rtx_equal_p (src_op0, src_op1))
3756 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3757 SUBST (XEXP (setsrc, 0), newdest);
3758 SUBST (XEXP (setsrc, 1), newdest);
3759 subst_done = true;
3761 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3762 else if ((code == PLUS || code == MULT)
3763 && GET_CODE (src_op0) == code
3764 && GET_CODE (XEXP (src_op0, 0)) == code
3765 && (INTEGRAL_MODE_P (mode)
3766 || (FLOAT_MODE_P (mode)
3767 && flag_unsafe_math_optimizations)))
3769 rtx p = XEXP (XEXP (src_op0, 0), 0);
3770 rtx q = XEXP (XEXP (src_op0, 0), 1);
3771 rtx r = XEXP (src_op0, 1);
3772 rtx s = src_op1;
3774 /* Split both "((X op Y) op X) op Y" and
3775 "((X op Y) op Y) op X" as "T op T" where T is
3776 "X op Y". */
3777 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3778 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3780 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3781 XEXP (src_op0, 0));
3782 SUBST (XEXP (setsrc, 0), newdest);
3783 SUBST (XEXP (setsrc, 1), newdest);
3784 subst_done = true;
3786 /* Split "((X op X) op Y) op Y)" as "T op T" where
3787 T is "X op Y". */
3788 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3790 rtx tmp = simplify_gen_binary (code, mode, p, r);
3791 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3792 SUBST (XEXP (setsrc, 0), newdest);
3793 SUBST (XEXP (setsrc, 1), newdest);
3794 subst_done = true;
3799 if (!subst_done)
3801 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3802 SUBST (*split, newdest);
3805 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3807 /* recog_for_combine might have added CLOBBERs to newi2pat.
3808 Make sure NEWPAT does not depend on the clobbered regs. */
3809 if (GET_CODE (newi2pat) == PARALLEL)
3810 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3811 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3813 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3814 if (reg_overlap_mentioned_p (reg, newpat))
3816 undo_all ();
3817 return 0;
3821 /* If the split point was a MULT and we didn't have one before,
3822 don't use one now. */
3823 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3824 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3828 /* Check for a case where we loaded from memory in a narrow mode and
3829 then sign extended it, but we need both registers. In that case,
3830 we have a PARALLEL with both loads from the same memory location.
3831 We can split this into a load from memory followed by a register-register
3832 copy. This saves at least one insn, more if register allocation can
3833 eliminate the copy.
3835 We cannot do this if the destination of the first assignment is a
3836 condition code register or cc0. We eliminate this case by making sure
3837 the SET_DEST and SET_SRC have the same mode.
3839 We cannot do this if the destination of the second assignment is
3840 a register that we have already assumed is zero-extended. Similarly
3841 for a SUBREG of such a register. */
3843 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3844 && GET_CODE (newpat) == PARALLEL
3845 && XVECLEN (newpat, 0) == 2
3846 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3847 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3848 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3849 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3850 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3851 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3852 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3853 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3854 DF_INSN_LUID (i2))
3855 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3856 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3857 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3858 (REG_P (temp_expr)
3859 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3860 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3861 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3862 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3863 != GET_MODE_MASK (word_mode))))
3864 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3865 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3866 (REG_P (temp_expr)
3867 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3868 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3869 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3870 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3871 != GET_MODE_MASK (word_mode)))))
3872 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3873 SET_SRC (XVECEXP (newpat, 0, 1)))
3874 && ! find_reg_note (i3, REG_UNUSED,
3875 SET_DEST (XVECEXP (newpat, 0, 0))))
3877 rtx ni2dest;
3879 newi2pat = XVECEXP (newpat, 0, 0);
3880 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3881 newpat = XVECEXP (newpat, 0, 1);
3882 SUBST (SET_SRC (newpat),
3883 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3884 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3886 if (i2_code_number >= 0)
3887 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3889 if (insn_code_number >= 0)
3890 swap_i2i3 = 1;
3893 /* Similarly, check for a case where we have a PARALLEL of two independent
3894 SETs but we started with three insns. In this case, we can do the sets
3895 as two separate insns. This case occurs when some SET allows two
3896 other insns to combine, but the destination of that SET is still live.
3898 Also do this if we started with two insns and (at least) one of the
3899 resulting sets is a noop; this noop will be deleted later. */
3901 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3902 && GET_CODE (newpat) == PARALLEL
3903 && XVECLEN (newpat, 0) == 2
3904 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3905 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3906 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3907 || set_noop_p (XVECEXP (newpat, 0, 1)))
3908 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3909 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3910 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3911 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3912 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3913 XVECEXP (newpat, 0, 0))
3914 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3915 XVECEXP (newpat, 0, 1))
3916 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3917 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3919 rtx set0 = XVECEXP (newpat, 0, 0);
3920 rtx set1 = XVECEXP (newpat, 0, 1);
3922 /* Normally, it doesn't matter which of the two is done first,
3923 but the one that references cc0 can't be the second, and
3924 one which uses any regs/memory set in between i2 and i3 can't
3925 be first. The PARALLEL might also have been pre-existing in i3,
3926 so we need to make sure that we won't wrongly hoist a SET to i2
3927 that would conflict with a death note present in there. */
3928 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3929 && !(REG_P (SET_DEST (set1))
3930 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3931 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3932 && find_reg_note (i2, REG_DEAD,
3933 SUBREG_REG (SET_DEST (set1))))
3934 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
3935 /* If I3 is a jump, ensure that set0 is a jump so that
3936 we do not create invalid RTL. */
3937 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3940 newi2pat = set1;
3941 newpat = set0;
3943 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3944 && !(REG_P (SET_DEST (set0))
3945 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3946 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3947 && find_reg_note (i2, REG_DEAD,
3948 SUBREG_REG (SET_DEST (set0))))
3949 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
3950 /* If I3 is a jump, ensure that set1 is a jump so that
3951 we do not create invalid RTL. */
3952 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3955 newi2pat = set0;
3956 newpat = set1;
3958 else
3960 undo_all ();
3961 return 0;
3964 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3966 if (i2_code_number >= 0)
3968 /* recog_for_combine might have added CLOBBERs to newi2pat.
3969 Make sure NEWPAT does not depend on the clobbered regs. */
3970 if (GET_CODE (newi2pat) == PARALLEL)
3972 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3973 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3975 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3976 if (reg_overlap_mentioned_p (reg, newpat))
3978 undo_all ();
3979 return 0;
3984 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3988 /* If it still isn't recognized, fail and change things back the way they
3989 were. */
3990 if ((insn_code_number < 0
3991 /* Is the result a reasonable ASM_OPERANDS? */
3992 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3994 undo_all ();
3995 return 0;
3998 /* If we had to change another insn, make sure it is valid also. */
3999 if (undobuf.other_insn)
4001 CLEAR_HARD_REG_SET (newpat_used_regs);
4003 other_pat = PATTERN (undobuf.other_insn);
4004 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4005 &new_other_notes);
4007 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4009 undo_all ();
4010 return 0;
4014 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4015 they are adjacent to each other or not. */
4016 if (HAVE_cc0)
4018 rtx_insn *p = prev_nonnote_insn (i3);
4019 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4020 && sets_cc0_p (newi2pat))
4022 undo_all ();
4023 return 0;
4027 /* Only allow this combination if insn_rtx_costs reports that the
4028 replacement instructions are cheaper than the originals. */
4029 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4031 undo_all ();
4032 return 0;
4035 if (MAY_HAVE_DEBUG_INSNS)
4037 struct undo *undo;
4039 for (undo = undobuf.undos; undo; undo = undo->next)
4040 if (undo->kind == UNDO_MODE)
4042 rtx reg = *undo->where.r;
4043 machine_mode new_mode = GET_MODE (reg);
4044 machine_mode old_mode = undo->old_contents.m;
4046 /* Temporarily revert mode back. */
4047 adjust_reg_mode (reg, old_mode);
4049 if (reg == i2dest && i2scratch)
4051 /* If we used i2dest as a scratch register with a
4052 different mode, substitute it for the original
4053 i2src while its original mode is temporarily
4054 restored, and then clear i2scratch so that we don't
4055 do it again later. */
4056 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4057 this_basic_block);
4058 i2scratch = false;
4059 /* Put back the new mode. */
4060 adjust_reg_mode (reg, new_mode);
4062 else
4064 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4065 rtx_insn *first, *last;
4067 if (reg == i2dest)
4069 first = i2;
4070 last = last_combined_insn;
4072 else
4074 first = i3;
4075 last = undobuf.other_insn;
4076 gcc_assert (last);
4077 if (DF_INSN_LUID (last)
4078 < DF_INSN_LUID (last_combined_insn))
4079 last = last_combined_insn;
4082 /* We're dealing with a reg that changed mode but not
4083 meaning, so we want to turn it into a subreg for
4084 the new mode. However, because of REG sharing and
4085 because its mode had already changed, we have to do
4086 it in two steps. First, replace any debug uses of
4087 reg, with its original mode temporarily restored,
4088 with this copy we have created; then, replace the
4089 copy with the SUBREG of the original shared reg,
4090 once again changed to the new mode. */
4091 propagate_for_debug (first, last, reg, tempreg,
4092 this_basic_block);
4093 adjust_reg_mode (reg, new_mode);
4094 propagate_for_debug (first, last, tempreg,
4095 lowpart_subreg (old_mode, reg, new_mode),
4096 this_basic_block);
4101 /* If we will be able to accept this, we have made a
4102 change to the destination of I3. This requires us to
4103 do a few adjustments. */
4105 if (changed_i3_dest)
4107 PATTERN (i3) = newpat;
4108 adjust_for_new_dest (i3);
4111 /* We now know that we can do this combination. Merge the insns and
4112 update the status of registers and LOG_LINKS. */
4114 if (undobuf.other_insn)
4116 rtx note, next;
4118 PATTERN (undobuf.other_insn) = other_pat;
4120 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4121 ensure that they are still valid. Then add any non-duplicate
4122 notes added by recog_for_combine. */
4123 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4125 next = XEXP (note, 1);
4127 if ((REG_NOTE_KIND (note) == REG_DEAD
4128 && !reg_referenced_p (XEXP (note, 0),
4129 PATTERN (undobuf.other_insn)))
4130 ||(REG_NOTE_KIND (note) == REG_UNUSED
4131 && !reg_set_p (XEXP (note, 0),
4132 PATTERN (undobuf.other_insn))))
4133 remove_note (undobuf.other_insn, note);
4136 distribute_notes (new_other_notes, undobuf.other_insn,
4137 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4138 NULL_RTX);
4141 if (swap_i2i3)
4143 rtx_insn *insn;
4144 struct insn_link *link;
4145 rtx ni2dest;
4147 /* I3 now uses what used to be its destination and which is now
4148 I2's destination. This requires us to do a few adjustments. */
4149 PATTERN (i3) = newpat;
4150 adjust_for_new_dest (i3);
4152 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4153 so we still will.
4155 However, some later insn might be using I2's dest and have
4156 a LOG_LINK pointing at I3. We must remove this link.
4157 The simplest way to remove the link is to point it at I1,
4158 which we know will be a NOTE. */
4160 /* newi2pat is usually a SET here; however, recog_for_combine might
4161 have added some clobbers. */
4162 if (GET_CODE (newi2pat) == PARALLEL)
4163 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4164 else
4165 ni2dest = SET_DEST (newi2pat);
4167 for (insn = NEXT_INSN (i3);
4168 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4169 || insn != BB_HEAD (this_basic_block->next_bb));
4170 insn = NEXT_INSN (insn))
4172 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4174 FOR_EACH_LOG_LINK (link, insn)
4175 if (link->insn == i3)
4176 link->insn = i1;
4178 break;
4184 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4185 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4186 rtx midnotes = 0;
4187 int from_luid;
4188 /* Compute which registers we expect to eliminate. newi2pat may be setting
4189 either i3dest or i2dest, so we must check it. */
4190 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4191 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4192 || !i2dest_killed
4193 ? 0 : i2dest);
4194 /* For i1, we need to compute both local elimination and global
4195 elimination information with respect to newi2pat because i1dest
4196 may be the same as i3dest, in which case newi2pat may be setting
4197 i1dest. Global information is used when distributing REG_DEAD
4198 note for i2 and i3, in which case it does matter if newi2pat sets
4199 i1dest or not.
4201 Local information is used when distributing REG_DEAD note for i1,
4202 in which case it doesn't matter if newi2pat sets i1dest or not.
4203 See PR62151, if we have four insns combination:
4204 i0: r0 <- i0src
4205 i1: r1 <- i1src (using r0)
4206 REG_DEAD (r0)
4207 i2: r0 <- i2src (using r1)
4208 i3: r3 <- i3src (using r0)
4209 ix: using r0
4210 From i1's point of view, r0 is eliminated, no matter if it is set
4211 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4212 should be discarded.
4214 Note local information only affects cases in forms like "I1->I2->I3",
4215 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4216 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4217 i0dest anyway. */
4218 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4219 || !i1dest_killed
4220 ? 0 : i1dest);
4221 rtx elim_i1 = (local_elim_i1 == 0
4222 || (newi2pat && reg_set_p (i1dest, newi2pat))
4223 ? 0 : i1dest);
4224 /* Same case as i1. */
4225 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4226 ? 0 : i0dest);
4227 rtx elim_i0 = (local_elim_i0 == 0
4228 || (newi2pat && reg_set_p (i0dest, newi2pat))
4229 ? 0 : i0dest);
4231 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4232 clear them. */
4233 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4234 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4235 if (i1)
4236 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4237 if (i0)
4238 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4240 /* Ensure that we do not have something that should not be shared but
4241 occurs multiple times in the new insns. Check this by first
4242 resetting all the `used' flags and then copying anything is shared. */
4244 reset_used_flags (i3notes);
4245 reset_used_flags (i2notes);
4246 reset_used_flags (i1notes);
4247 reset_used_flags (i0notes);
4248 reset_used_flags (newpat);
4249 reset_used_flags (newi2pat);
4250 if (undobuf.other_insn)
4251 reset_used_flags (PATTERN (undobuf.other_insn));
4253 i3notes = copy_rtx_if_shared (i3notes);
4254 i2notes = copy_rtx_if_shared (i2notes);
4255 i1notes = copy_rtx_if_shared (i1notes);
4256 i0notes = copy_rtx_if_shared (i0notes);
4257 newpat = copy_rtx_if_shared (newpat);
4258 newi2pat = copy_rtx_if_shared (newi2pat);
4259 if (undobuf.other_insn)
4260 reset_used_flags (PATTERN (undobuf.other_insn));
4262 INSN_CODE (i3) = insn_code_number;
4263 PATTERN (i3) = newpat;
4265 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4267 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4269 reset_used_flags (call_usage);
4270 call_usage = copy_rtx (call_usage);
4272 if (substed_i2)
4274 /* I2SRC must still be meaningful at this point. Some splitting
4275 operations can invalidate I2SRC, but those operations do not
4276 apply to calls. */
4277 gcc_assert (i2src);
4278 replace_rtx (call_usage, i2dest, i2src);
4281 if (substed_i1)
4282 replace_rtx (call_usage, i1dest, i1src);
4283 if (substed_i0)
4284 replace_rtx (call_usage, i0dest, i0src);
4286 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4289 if (undobuf.other_insn)
4290 INSN_CODE (undobuf.other_insn) = other_code_number;
4292 /* We had one special case above where I2 had more than one set and
4293 we replaced a destination of one of those sets with the destination
4294 of I3. In that case, we have to update LOG_LINKS of insns later
4295 in this basic block. Note that this (expensive) case is rare.
4297 Also, in this case, we must pretend that all REG_NOTEs for I2
4298 actually came from I3, so that REG_UNUSED notes from I2 will be
4299 properly handled. */
4301 if (i3_subst_into_i2)
4303 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4304 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4305 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4306 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4307 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4308 && ! find_reg_note (i2, REG_UNUSED,
4309 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4310 for (temp_insn = NEXT_INSN (i2);
4311 temp_insn
4312 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4313 || BB_HEAD (this_basic_block) != temp_insn);
4314 temp_insn = NEXT_INSN (temp_insn))
4315 if (temp_insn != i3 && INSN_P (temp_insn))
4316 FOR_EACH_LOG_LINK (link, temp_insn)
4317 if (link->insn == i2)
4318 link->insn = i3;
4320 if (i3notes)
4322 rtx link = i3notes;
4323 while (XEXP (link, 1))
4324 link = XEXP (link, 1);
4325 XEXP (link, 1) = i2notes;
4327 else
4328 i3notes = i2notes;
4329 i2notes = 0;
4332 LOG_LINKS (i3) = NULL;
4333 REG_NOTES (i3) = 0;
4334 LOG_LINKS (i2) = NULL;
4335 REG_NOTES (i2) = 0;
4337 if (newi2pat)
4339 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4340 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4341 this_basic_block);
4342 INSN_CODE (i2) = i2_code_number;
4343 PATTERN (i2) = newi2pat;
4345 else
4347 if (MAY_HAVE_DEBUG_INSNS && i2src)
4348 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4349 this_basic_block);
4350 SET_INSN_DELETED (i2);
4353 if (i1)
4355 LOG_LINKS (i1) = NULL;
4356 REG_NOTES (i1) = 0;
4357 if (MAY_HAVE_DEBUG_INSNS)
4358 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4359 this_basic_block);
4360 SET_INSN_DELETED (i1);
4363 if (i0)
4365 LOG_LINKS (i0) = NULL;
4366 REG_NOTES (i0) = 0;
4367 if (MAY_HAVE_DEBUG_INSNS)
4368 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4369 this_basic_block);
4370 SET_INSN_DELETED (i0);
4373 /* Get death notes for everything that is now used in either I3 or
4374 I2 and used to die in a previous insn. If we built two new
4375 patterns, move from I1 to I2 then I2 to I3 so that we get the
4376 proper movement on registers that I2 modifies. */
4378 if (i0)
4379 from_luid = DF_INSN_LUID (i0);
4380 else if (i1)
4381 from_luid = DF_INSN_LUID (i1);
4382 else
4383 from_luid = DF_INSN_LUID (i2);
4384 if (newi2pat)
4385 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4386 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4388 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4389 if (i3notes)
4390 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4391 elim_i2, elim_i1, elim_i0);
4392 if (i2notes)
4393 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4394 elim_i2, elim_i1, elim_i0);
4395 if (i1notes)
4396 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4397 elim_i2, local_elim_i1, local_elim_i0);
4398 if (i0notes)
4399 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4400 elim_i2, elim_i1, local_elim_i0);
4401 if (midnotes)
4402 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4403 elim_i2, elim_i1, elim_i0);
4405 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4406 know these are REG_UNUSED and want them to go to the desired insn,
4407 so we always pass it as i3. */
4409 if (newi2pat && new_i2_notes)
4410 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4411 NULL_RTX);
4413 if (new_i3_notes)
4414 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4415 NULL_RTX);
4417 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4418 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4419 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4420 in that case, it might delete I2. Similarly for I2 and I1.
4421 Show an additional death due to the REG_DEAD note we make here. If
4422 we discard it in distribute_notes, we will decrement it again. */
4424 if (i3dest_killed)
4426 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4427 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4428 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4429 elim_i1, elim_i0);
4430 else
4431 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4432 elim_i2, elim_i1, elim_i0);
4435 if (i2dest_in_i2src)
4437 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4438 if (newi2pat && reg_set_p (i2dest, newi2pat))
4439 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4440 NULL_RTX, NULL_RTX);
4441 else
4442 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4443 NULL_RTX, NULL_RTX, NULL_RTX);
4446 if (i1dest_in_i1src)
4448 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4449 if (newi2pat && reg_set_p (i1dest, newi2pat))
4450 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4451 NULL_RTX, NULL_RTX);
4452 else
4453 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4454 NULL_RTX, NULL_RTX, NULL_RTX);
4457 if (i0dest_in_i0src)
4459 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4460 if (newi2pat && reg_set_p (i0dest, newi2pat))
4461 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4462 NULL_RTX, NULL_RTX);
4463 else
4464 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4465 NULL_RTX, NULL_RTX, NULL_RTX);
4468 distribute_links (i3links);
4469 distribute_links (i2links);
4470 distribute_links (i1links);
4471 distribute_links (i0links);
4473 if (REG_P (i2dest))
4475 struct insn_link *link;
4476 rtx_insn *i2_insn = 0;
4477 rtx i2_val = 0, set;
4479 /* The insn that used to set this register doesn't exist, and
4480 this life of the register may not exist either. See if one of
4481 I3's links points to an insn that sets I2DEST. If it does,
4482 that is now the last known value for I2DEST. If we don't update
4483 this and I2 set the register to a value that depended on its old
4484 contents, we will get confused. If this insn is used, thing
4485 will be set correctly in combine_instructions. */
4486 FOR_EACH_LOG_LINK (link, i3)
4487 if ((set = single_set (link->insn)) != 0
4488 && rtx_equal_p (i2dest, SET_DEST (set)))
4489 i2_insn = link->insn, i2_val = SET_SRC (set);
4491 record_value_for_reg (i2dest, i2_insn, i2_val);
4493 /* If the reg formerly set in I2 died only once and that was in I3,
4494 zero its use count so it won't make `reload' do any work. */
4495 if (! added_sets_2
4496 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4497 && ! i2dest_in_i2src
4498 && REGNO (i2dest) < reg_n_sets_max)
4499 INC_REG_N_SETS (REGNO (i2dest), -1);
4502 if (i1 && REG_P (i1dest))
4504 struct insn_link *link;
4505 rtx_insn *i1_insn = 0;
4506 rtx i1_val = 0, set;
4508 FOR_EACH_LOG_LINK (link, i3)
4509 if ((set = single_set (link->insn)) != 0
4510 && rtx_equal_p (i1dest, SET_DEST (set)))
4511 i1_insn = link->insn, i1_val = SET_SRC (set);
4513 record_value_for_reg (i1dest, i1_insn, i1_val);
4515 if (! added_sets_1
4516 && ! i1dest_in_i1src
4517 && REGNO (i1dest) < reg_n_sets_max)
4518 INC_REG_N_SETS (REGNO (i1dest), -1);
4521 if (i0 && REG_P (i0dest))
4523 struct insn_link *link;
4524 rtx_insn *i0_insn = 0;
4525 rtx i0_val = 0, set;
4527 FOR_EACH_LOG_LINK (link, i3)
4528 if ((set = single_set (link->insn)) != 0
4529 && rtx_equal_p (i0dest, SET_DEST (set)))
4530 i0_insn = link->insn, i0_val = SET_SRC (set);
4532 record_value_for_reg (i0dest, i0_insn, i0_val);
4534 if (! added_sets_0
4535 && ! i0dest_in_i0src
4536 && REGNO (i0dest) < reg_n_sets_max)
4537 INC_REG_N_SETS (REGNO (i0dest), -1);
4540 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4541 been made to this insn. The order is important, because newi2pat
4542 can affect nonzero_bits of newpat. */
4543 if (newi2pat)
4544 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4545 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4548 if (undobuf.other_insn != NULL_RTX)
4550 if (dump_file)
4552 fprintf (dump_file, "modifying other_insn ");
4553 dump_insn_slim (dump_file, undobuf.other_insn);
4555 df_insn_rescan (undobuf.other_insn);
4558 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4560 if (dump_file)
4562 fprintf (dump_file, "modifying insn i0 ");
4563 dump_insn_slim (dump_file, i0);
4565 df_insn_rescan (i0);
4568 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4570 if (dump_file)
4572 fprintf (dump_file, "modifying insn i1 ");
4573 dump_insn_slim (dump_file, i1);
4575 df_insn_rescan (i1);
4578 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4580 if (dump_file)
4582 fprintf (dump_file, "modifying insn i2 ");
4583 dump_insn_slim (dump_file, i2);
4585 df_insn_rescan (i2);
4588 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4590 if (dump_file)
4592 fprintf (dump_file, "modifying insn i3 ");
4593 dump_insn_slim (dump_file, i3);
4595 df_insn_rescan (i3);
4598 /* Set new_direct_jump_p if a new return or simple jump instruction
4599 has been created. Adjust the CFG accordingly. */
4600 if (returnjump_p (i3) || any_uncondjump_p (i3))
4602 *new_direct_jump_p = 1;
4603 mark_jump_label (PATTERN (i3), i3, 0);
4604 update_cfg_for_uncondjump (i3);
4607 if (undobuf.other_insn != NULL_RTX
4608 && (returnjump_p (undobuf.other_insn)
4609 || any_uncondjump_p (undobuf.other_insn)))
4611 *new_direct_jump_p = 1;
4612 update_cfg_for_uncondjump (undobuf.other_insn);
4615 /* A noop might also need cleaning up of CFG, if it comes from the
4616 simplification of a jump. */
4617 if (JUMP_P (i3)
4618 && GET_CODE (newpat) == SET
4619 && SET_SRC (newpat) == pc_rtx
4620 && SET_DEST (newpat) == pc_rtx)
4622 *new_direct_jump_p = 1;
4623 update_cfg_for_uncondjump (i3);
4626 if (undobuf.other_insn != NULL_RTX
4627 && JUMP_P (undobuf.other_insn)
4628 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4629 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4630 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4632 *new_direct_jump_p = 1;
4633 update_cfg_for_uncondjump (undobuf.other_insn);
4636 combine_successes++;
4637 undo_commit ();
4639 if (added_links_insn
4640 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4641 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4642 return added_links_insn;
4643 else
4644 return newi2pat ? i2 : i3;
4647 /* Undo all the modifications recorded in undobuf. */
4649 static void
4650 undo_all (void)
4652 struct undo *undo, *next;
4654 for (undo = undobuf.undos; undo; undo = next)
4656 next = undo->next;
4657 switch (undo->kind)
4659 case UNDO_RTX:
4660 *undo->where.r = undo->old_contents.r;
4661 break;
4662 case UNDO_INT:
4663 *undo->where.i = undo->old_contents.i;
4664 break;
4665 case UNDO_MODE:
4666 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4667 break;
4668 case UNDO_LINKS:
4669 *undo->where.l = undo->old_contents.l;
4670 break;
4671 default:
4672 gcc_unreachable ();
4675 undo->next = undobuf.frees;
4676 undobuf.frees = undo;
4679 undobuf.undos = 0;
4682 /* We've committed to accepting the changes we made. Move all
4683 of the undos to the free list. */
4685 static void
4686 undo_commit (void)
4688 struct undo *undo, *next;
4690 for (undo = undobuf.undos; undo; undo = next)
4692 next = undo->next;
4693 undo->next = undobuf.frees;
4694 undobuf.frees = undo;
4696 undobuf.undos = 0;
4699 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4700 where we have an arithmetic expression and return that point. LOC will
4701 be inside INSN.
4703 try_combine will call this function to see if an insn can be split into
4704 two insns. */
4706 static rtx *
4707 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4709 rtx x = *loc;
4710 enum rtx_code code = GET_CODE (x);
4711 rtx *split;
4712 unsigned HOST_WIDE_INT len = 0;
4713 HOST_WIDE_INT pos = 0;
4714 int unsignedp = 0;
4715 rtx inner = NULL_RTX;
4717 /* First special-case some codes. */
4718 switch (code)
4720 case SUBREG:
4721 #ifdef INSN_SCHEDULING
4722 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4723 point. */
4724 if (MEM_P (SUBREG_REG (x)))
4725 return loc;
4726 #endif
4727 return find_split_point (&SUBREG_REG (x), insn, false);
4729 case MEM:
4730 #ifdef HAVE_lo_sum
4731 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4732 using LO_SUM and HIGH. */
4733 if (GET_CODE (XEXP (x, 0)) == CONST
4734 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4736 machine_mode address_mode = get_address_mode (x);
4738 SUBST (XEXP (x, 0),
4739 gen_rtx_LO_SUM (address_mode,
4740 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4741 XEXP (x, 0)));
4742 return &XEXP (XEXP (x, 0), 0);
4744 #endif
4746 /* If we have a PLUS whose second operand is a constant and the
4747 address is not valid, perhaps will can split it up using
4748 the machine-specific way to split large constants. We use
4749 the first pseudo-reg (one of the virtual regs) as a placeholder;
4750 it will not remain in the result. */
4751 if (GET_CODE (XEXP (x, 0)) == PLUS
4752 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4753 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4754 MEM_ADDR_SPACE (x)))
4756 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4757 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4758 XEXP (x, 0)),
4759 subst_insn);
4761 /* This should have produced two insns, each of which sets our
4762 placeholder. If the source of the second is a valid address,
4763 we can make put both sources together and make a split point
4764 in the middle. */
4766 if (seq
4767 && NEXT_INSN (seq) != NULL_RTX
4768 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4769 && NONJUMP_INSN_P (seq)
4770 && GET_CODE (PATTERN (seq)) == SET
4771 && SET_DEST (PATTERN (seq)) == reg
4772 && ! reg_mentioned_p (reg,
4773 SET_SRC (PATTERN (seq)))
4774 && NONJUMP_INSN_P (NEXT_INSN (seq))
4775 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4776 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4777 && memory_address_addr_space_p
4778 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4779 MEM_ADDR_SPACE (x)))
4781 rtx src1 = SET_SRC (PATTERN (seq));
4782 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4784 /* Replace the placeholder in SRC2 with SRC1. If we can
4785 find where in SRC2 it was placed, that can become our
4786 split point and we can replace this address with SRC2.
4787 Just try two obvious places. */
4789 src2 = replace_rtx (src2, reg, src1);
4790 split = 0;
4791 if (XEXP (src2, 0) == src1)
4792 split = &XEXP (src2, 0);
4793 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4794 && XEXP (XEXP (src2, 0), 0) == src1)
4795 split = &XEXP (XEXP (src2, 0), 0);
4797 if (split)
4799 SUBST (XEXP (x, 0), src2);
4800 return split;
4804 /* If that didn't work, perhaps the first operand is complex and
4805 needs to be computed separately, so make a split point there.
4806 This will occur on machines that just support REG + CONST
4807 and have a constant moved through some previous computation. */
4809 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4810 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4811 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4812 return &XEXP (XEXP (x, 0), 0);
4815 /* If we have a PLUS whose first operand is complex, try computing it
4816 separately by making a split there. */
4817 if (GET_CODE (XEXP (x, 0)) == PLUS
4818 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4819 MEM_ADDR_SPACE (x))
4820 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4821 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4822 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4823 return &XEXP (XEXP (x, 0), 0);
4824 break;
4826 case SET:
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);
4840 /* See if we can split SET_SRC as it stands. */
4841 split = find_split_point (&SET_SRC (x), insn, true);
4842 if (split && split != &SET_SRC (x))
4843 return split;
4845 /* See if we can split SET_DEST as it stands. */
4846 split = find_split_point (&SET_DEST (x), insn, false);
4847 if (split && split != &SET_DEST (x))
4848 return split;
4850 /* See if this is a bitfield assignment with everything constant. If
4851 so, this is an IOR of an AND, so split it into that. */
4852 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4853 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4854 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4855 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4856 && CONST_INT_P (SET_SRC (x))
4857 && ((INTVAL (XEXP (SET_DEST (x), 1))
4858 + INTVAL (XEXP (SET_DEST (x), 2)))
4859 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4860 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4862 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4863 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4864 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4865 rtx dest = XEXP (SET_DEST (x), 0);
4866 machine_mode mode = GET_MODE (dest);
4867 unsigned HOST_WIDE_INT mask
4868 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4869 rtx or_mask;
4871 if (BITS_BIG_ENDIAN)
4872 pos = GET_MODE_PRECISION (mode) - len - pos;
4874 or_mask = gen_int_mode (src << pos, mode);
4875 if (src == mask)
4876 SUBST (SET_SRC (x),
4877 simplify_gen_binary (IOR, mode, dest, or_mask));
4878 else
4880 rtx negmask = gen_int_mode (~(mask << pos), mode);
4881 SUBST (SET_SRC (x),
4882 simplify_gen_binary (IOR, mode,
4883 simplify_gen_binary (AND, mode,
4884 dest, negmask),
4885 or_mask));
4888 SUBST (SET_DEST (x), dest);
4890 split = find_split_point (&SET_SRC (x), insn, true);
4891 if (split && split != &SET_SRC (x))
4892 return split;
4895 /* Otherwise, see if this is an operation that we can split into two.
4896 If so, try to split that. */
4897 code = GET_CODE (SET_SRC (x));
4899 switch (code)
4901 case AND:
4902 /* If we are AND'ing with a large constant that is only a single
4903 bit and the result is only being used in a context where we
4904 need to know if it is zero or nonzero, replace it with a bit
4905 extraction. This will avoid the large constant, which might
4906 have taken more than one insn to make. If the constant were
4907 not a valid argument to the AND but took only one insn to make,
4908 this is no worse, but if it took more than one insn, it will
4909 be better. */
4911 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4912 && REG_P (XEXP (SET_SRC (x), 0))
4913 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4914 && REG_P (SET_DEST (x))
4915 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4916 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4917 && XEXP (*split, 0) == SET_DEST (x)
4918 && XEXP (*split, 1) == const0_rtx)
4920 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4921 XEXP (SET_SRC (x), 0),
4922 pos, NULL_RTX, 1, 1, 0, 0);
4923 if (extraction != 0)
4925 SUBST (SET_SRC (x), extraction);
4926 return find_split_point (loc, insn, false);
4929 break;
4931 case NE:
4932 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4933 is known to be on, this can be converted into a NEG of a shift. */
4934 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4935 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4936 && 1 <= (pos = exact_log2
4937 (nonzero_bits (XEXP (SET_SRC (x), 0),
4938 GET_MODE (XEXP (SET_SRC (x), 0))))))
4940 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4942 SUBST (SET_SRC (x),
4943 gen_rtx_NEG (mode,
4944 gen_rtx_LSHIFTRT (mode,
4945 XEXP (SET_SRC (x), 0),
4946 GEN_INT (pos))));
4948 split = find_split_point (&SET_SRC (x), insn, true);
4949 if (split && split != &SET_SRC (x))
4950 return split;
4952 break;
4954 case SIGN_EXTEND:
4955 inner = XEXP (SET_SRC (x), 0);
4957 /* We can't optimize if either mode is a partial integer
4958 mode as we don't know how many bits are significant
4959 in those modes. */
4960 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4961 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4962 break;
4964 pos = 0;
4965 len = GET_MODE_PRECISION (GET_MODE (inner));
4966 unsignedp = 0;
4967 break;
4969 case SIGN_EXTRACT:
4970 case ZERO_EXTRACT:
4971 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4972 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4974 inner = XEXP (SET_SRC (x), 0);
4975 len = INTVAL (XEXP (SET_SRC (x), 1));
4976 pos = INTVAL (XEXP (SET_SRC (x), 2));
4978 if (BITS_BIG_ENDIAN)
4979 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4980 unsignedp = (code == ZERO_EXTRACT);
4982 break;
4984 default:
4985 break;
4988 if (len && pos >= 0
4989 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4991 machine_mode mode = GET_MODE (SET_SRC (x));
4993 /* For unsigned, we have a choice of a shift followed by an
4994 AND or two shifts. Use two shifts for field sizes where the
4995 constant might be too large. We assume here that we can
4996 always at least get 8-bit constants in an AND insn, which is
4997 true for every current RISC. */
4999 if (unsignedp && len <= 8)
5001 unsigned HOST_WIDE_INT mask
5002 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
5003 SUBST (SET_SRC (x),
5004 gen_rtx_AND (mode,
5005 gen_rtx_LSHIFTRT
5006 (mode, gen_lowpart (mode, inner),
5007 GEN_INT (pos)),
5008 gen_int_mode (mask, mode)));
5010 split = find_split_point (&SET_SRC (x), insn, true);
5011 if (split && split != &SET_SRC (x))
5012 return split;
5014 else
5016 SUBST (SET_SRC (x),
5017 gen_rtx_fmt_ee
5018 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5019 gen_rtx_ASHIFT (mode,
5020 gen_lowpart (mode, inner),
5021 GEN_INT (GET_MODE_PRECISION (mode)
5022 - len - pos)),
5023 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5025 split = find_split_point (&SET_SRC (x), insn, true);
5026 if (split && split != &SET_SRC (x))
5027 return split;
5031 /* See if this is a simple operation with a constant as the second
5032 operand. It might be that this constant is out of range and hence
5033 could be used as a split point. */
5034 if (BINARY_P (SET_SRC (x))
5035 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5036 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5037 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5038 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5039 return &XEXP (SET_SRC (x), 1);
5041 /* Finally, see if this is a simple operation with its first operand
5042 not in a register. The operation might require this operand in a
5043 register, so return it as a split point. We can always do this
5044 because if the first operand were another operation, we would have
5045 already found it as a split point. */
5046 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5047 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5048 return &XEXP (SET_SRC (x), 0);
5050 return 0;
5052 case AND:
5053 case IOR:
5054 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5055 it is better to write this as (not (ior A B)) so we can split it.
5056 Similarly for IOR. */
5057 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5059 SUBST (*loc,
5060 gen_rtx_NOT (GET_MODE (x),
5061 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5062 GET_MODE (x),
5063 XEXP (XEXP (x, 0), 0),
5064 XEXP (XEXP (x, 1), 0))));
5065 return find_split_point (loc, insn, set_src);
5068 /* Many RISC machines have a large set of logical insns. If the
5069 second operand is a NOT, put it first so we will try to split the
5070 other operand first. */
5071 if (GET_CODE (XEXP (x, 1)) == NOT)
5073 rtx tem = XEXP (x, 0);
5074 SUBST (XEXP (x, 0), XEXP (x, 1));
5075 SUBST (XEXP (x, 1), tem);
5077 break;
5079 case PLUS:
5080 case MINUS:
5081 /* Canonicalization can produce (minus A (mult B C)), where C is a
5082 constant. It may be better to try splitting (plus (mult B -C) A)
5083 instead if this isn't a multiply by a power of two. */
5084 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5085 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5086 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5088 machine_mode mode = GET_MODE (x);
5089 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5090 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5091 SUBST (*loc, gen_rtx_PLUS (mode,
5092 gen_rtx_MULT (mode,
5093 XEXP (XEXP (x, 1), 0),
5094 gen_int_mode (other_int,
5095 mode)),
5096 XEXP (x, 0)));
5097 return find_split_point (loc, insn, set_src);
5100 /* Split at a multiply-accumulate instruction. However if this is
5101 the SET_SRC, we likely do not have such an instruction and it's
5102 worthless to try this split. */
5103 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5104 return loc;
5106 default:
5107 break;
5110 /* Otherwise, select our actions depending on our rtx class. */
5111 switch (GET_RTX_CLASS (code))
5113 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5114 case RTX_TERNARY:
5115 split = find_split_point (&XEXP (x, 2), insn, false);
5116 if (split)
5117 return split;
5118 /* ... fall through ... */
5119 case RTX_BIN_ARITH:
5120 case RTX_COMM_ARITH:
5121 case RTX_COMPARE:
5122 case RTX_COMM_COMPARE:
5123 split = find_split_point (&XEXP (x, 1), insn, false);
5124 if (split)
5125 return split;
5126 /* ... fall through ... */
5127 case RTX_UNARY:
5128 /* Some machines have (and (shift ...) ...) insns. If X is not
5129 an AND, but XEXP (X, 0) is, use it as our split point. */
5130 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5131 return &XEXP (x, 0);
5133 split = find_split_point (&XEXP (x, 0), insn, false);
5134 if (split)
5135 return split;
5136 return loc;
5138 default:
5139 /* Otherwise, we don't have a split point. */
5140 return 0;
5144 /* Throughout X, replace FROM with TO, and return the result.
5145 The result is TO if X is FROM;
5146 otherwise the result is X, but its contents may have been modified.
5147 If they were modified, a record was made in undobuf so that
5148 undo_all will (among other things) return X to its original state.
5150 If the number of changes necessary is too much to record to undo,
5151 the excess changes are not made, so the result is invalid.
5152 The changes already made can still be undone.
5153 undobuf.num_undo is incremented for such changes, so by testing that
5154 the caller can tell whether the result is valid.
5156 `n_occurrences' is incremented each time FROM is replaced.
5158 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5160 IN_COND is nonzero if we are at the top level of a condition.
5162 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5163 by copying if `n_occurrences' is nonzero. */
5165 static rtx
5166 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5168 enum rtx_code code = GET_CODE (x);
5169 machine_mode op0_mode = VOIDmode;
5170 const char *fmt;
5171 int len, i;
5172 rtx new_rtx;
5174 /* Two expressions are equal if they are identical copies of a shared
5175 RTX or if they are both registers with the same register number
5176 and mode. */
5178 #define COMBINE_RTX_EQUAL_P(X,Y) \
5179 ((X) == (Y) \
5180 || (REG_P (X) && REG_P (Y) \
5181 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5183 /* Do not substitute into clobbers of regs -- this will never result in
5184 valid RTL. */
5185 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5186 return x;
5188 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5190 n_occurrences++;
5191 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5194 /* If X and FROM are the same register but different modes, they
5195 will not have been seen as equal above. However, the log links code
5196 will make a LOG_LINKS entry for that case. If we do nothing, we
5197 will try to rerecognize our original insn and, when it succeeds,
5198 we will delete the feeding insn, which is incorrect.
5200 So force this insn not to match in this (rare) case. */
5201 if (! in_dest && code == REG && REG_P (from)
5202 && reg_overlap_mentioned_p (x, from))
5203 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5205 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5206 of which may contain things that can be combined. */
5207 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5208 return x;
5210 /* It is possible to have a subexpression appear twice in the insn.
5211 Suppose that FROM is a register that appears within TO.
5212 Then, after that subexpression has been scanned once by `subst',
5213 the second time it is scanned, TO may be found. If we were
5214 to scan TO here, we would find FROM within it and create a
5215 self-referent rtl structure which is completely wrong. */
5216 if (COMBINE_RTX_EQUAL_P (x, to))
5217 return to;
5219 /* Parallel asm_operands need special attention because all of the
5220 inputs are shared across the arms. Furthermore, unsharing the
5221 rtl results in recognition failures. Failure to handle this case
5222 specially can result in circular rtl.
5224 Solve this by doing a normal pass across the first entry of the
5225 parallel, and only processing the SET_DESTs of the subsequent
5226 entries. Ug. */
5228 if (code == PARALLEL
5229 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5230 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5232 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5234 /* If this substitution failed, this whole thing fails. */
5235 if (GET_CODE (new_rtx) == CLOBBER
5236 && XEXP (new_rtx, 0) == const0_rtx)
5237 return new_rtx;
5239 SUBST (XVECEXP (x, 0, 0), new_rtx);
5241 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5243 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5245 if (!REG_P (dest)
5246 && GET_CODE (dest) != CC0
5247 && GET_CODE (dest) != PC)
5249 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5251 /* If this substitution failed, this whole thing fails. */
5252 if (GET_CODE (new_rtx) == CLOBBER
5253 && XEXP (new_rtx, 0) == const0_rtx)
5254 return new_rtx;
5256 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5260 else
5262 len = GET_RTX_LENGTH (code);
5263 fmt = GET_RTX_FORMAT (code);
5265 /* We don't need to process a SET_DEST that is a register, CC0,
5266 or PC, so set up to skip this common case. All other cases
5267 where we want to suppress replacing something inside a
5268 SET_SRC are handled via the IN_DEST operand. */
5269 if (code == SET
5270 && (REG_P (SET_DEST (x))
5271 || GET_CODE (SET_DEST (x)) == CC0
5272 || GET_CODE (SET_DEST (x)) == PC))
5273 fmt = "ie";
5275 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5276 constant. */
5277 if (fmt[0] == 'e')
5278 op0_mode = GET_MODE (XEXP (x, 0));
5280 for (i = 0; i < len; i++)
5282 if (fmt[i] == 'E')
5284 int j;
5285 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5287 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5289 new_rtx = (unique_copy && n_occurrences
5290 ? copy_rtx (to) : to);
5291 n_occurrences++;
5293 else
5295 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5296 unique_copy);
5298 /* If this substitution failed, this whole thing
5299 fails. */
5300 if (GET_CODE (new_rtx) == CLOBBER
5301 && XEXP (new_rtx, 0) == const0_rtx)
5302 return new_rtx;
5305 SUBST (XVECEXP (x, i, j), new_rtx);
5308 else if (fmt[i] == 'e')
5310 /* If this is a register being set, ignore it. */
5311 new_rtx = XEXP (x, i);
5312 if (in_dest
5313 && i == 0
5314 && (((code == SUBREG || code == ZERO_EXTRACT)
5315 && REG_P (new_rtx))
5316 || code == STRICT_LOW_PART))
5319 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5321 /* In general, don't install a subreg involving two
5322 modes not tieable. It can worsen register
5323 allocation, and can even make invalid reload
5324 insns, since the reg inside may need to be copied
5325 from in the outside mode, and that may be invalid
5326 if it is an fp reg copied in integer mode.
5328 We allow two exceptions to this: It is valid if
5329 it is inside another SUBREG and the mode of that
5330 SUBREG and the mode of the inside of TO is
5331 tieable and it is valid if X is a SET that copies
5332 FROM to CC0. */
5334 if (GET_CODE (to) == SUBREG
5335 && ! MODES_TIEABLE_P (GET_MODE (to),
5336 GET_MODE (SUBREG_REG (to)))
5337 && ! (code == SUBREG
5338 && MODES_TIEABLE_P (GET_MODE (x),
5339 GET_MODE (SUBREG_REG (to))))
5340 #if HAVE_cc0
5341 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5342 #endif
5344 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5346 if (code == SUBREG
5347 && REG_P (to)
5348 && REGNO (to) < FIRST_PSEUDO_REGISTER
5349 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5350 SUBREG_BYTE (x),
5351 GET_MODE (x)) < 0)
5352 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5354 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5355 n_occurrences++;
5357 else
5358 /* If we are in a SET_DEST, suppress most cases unless we
5359 have gone inside a MEM, in which case we want to
5360 simplify the address. We assume here that things that
5361 are actually part of the destination have their inner
5362 parts in the first expression. This is true for SUBREG,
5363 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5364 things aside from REG and MEM that should appear in a
5365 SET_DEST. */
5366 new_rtx = subst (XEXP (x, i), from, to,
5367 (((in_dest
5368 && (code == SUBREG || code == STRICT_LOW_PART
5369 || code == ZERO_EXTRACT))
5370 || code == SET)
5371 && i == 0),
5372 code == IF_THEN_ELSE && i == 0,
5373 unique_copy);
5375 /* If we found that we will have to reject this combination,
5376 indicate that by returning the CLOBBER ourselves, rather than
5377 an expression containing it. This will speed things up as
5378 well as prevent accidents where two CLOBBERs are considered
5379 to be equal, thus producing an incorrect simplification. */
5381 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5382 return new_rtx;
5384 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5386 machine_mode mode = GET_MODE (x);
5388 x = simplify_subreg (GET_MODE (x), new_rtx,
5389 GET_MODE (SUBREG_REG (x)),
5390 SUBREG_BYTE (x));
5391 if (! x)
5392 x = gen_rtx_CLOBBER (mode, const0_rtx);
5394 else if (CONST_SCALAR_INT_P (new_rtx)
5395 && GET_CODE (x) == ZERO_EXTEND)
5397 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5398 new_rtx, GET_MODE (XEXP (x, 0)));
5399 gcc_assert (x);
5401 else
5402 SUBST (XEXP (x, i), new_rtx);
5407 /* Check if we are loading something from the constant pool via float
5408 extension; in this case we would undo compress_float_constant
5409 optimization and degenerate constant load to an immediate value. */
5410 if (GET_CODE (x) == FLOAT_EXTEND
5411 && MEM_P (XEXP (x, 0))
5412 && MEM_READONLY_P (XEXP (x, 0)))
5414 rtx tmp = avoid_constant_pool_reference (x);
5415 if (x != tmp)
5416 return x;
5419 /* Try to simplify X. If the simplification changed the code, it is likely
5420 that further simplification will help, so loop, but limit the number
5421 of repetitions that will be performed. */
5423 for (i = 0; i < 4; i++)
5425 /* If X is sufficiently simple, don't bother trying to do anything
5426 with it. */
5427 if (code != CONST_INT && code != REG && code != CLOBBER)
5428 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5430 if (GET_CODE (x) == code)
5431 break;
5433 code = GET_CODE (x);
5435 /* We no longer know the original mode of operand 0 since we
5436 have changed the form of X) */
5437 op0_mode = VOIDmode;
5440 return x;
5443 /* Simplify X, a piece of RTL. We just operate on the expression at the
5444 outer level; call `subst' to simplify recursively. Return the new
5445 expression.
5447 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5448 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5449 of a condition. */
5451 static rtx
5452 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5453 int in_cond)
5455 enum rtx_code code = GET_CODE (x);
5456 machine_mode mode = GET_MODE (x);
5457 rtx temp;
5458 int i;
5460 /* If this is a commutative operation, put a constant last and a complex
5461 expression first. We don't need to do this for comparisons here. */
5462 if (COMMUTATIVE_ARITH_P (x)
5463 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5465 temp = XEXP (x, 0);
5466 SUBST (XEXP (x, 0), XEXP (x, 1));
5467 SUBST (XEXP (x, 1), temp);
5470 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5471 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5472 things. Check for cases where both arms are testing the same
5473 condition.
5475 Don't do anything if all operands are very simple. */
5477 if ((BINARY_P (x)
5478 && ((!OBJECT_P (XEXP (x, 0))
5479 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5480 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5481 || (!OBJECT_P (XEXP (x, 1))
5482 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5483 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5484 || (UNARY_P (x)
5485 && (!OBJECT_P (XEXP (x, 0))
5486 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5487 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5489 rtx cond, true_rtx, false_rtx;
5491 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5492 if (cond != 0
5493 /* If everything is a comparison, what we have is highly unlikely
5494 to be simpler, so don't use it. */
5495 && ! (COMPARISON_P (x)
5496 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5498 rtx cop1 = const0_rtx;
5499 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5501 if (cond_code == NE && COMPARISON_P (cond))
5502 return x;
5504 /* Simplify the alternative arms; this may collapse the true and
5505 false arms to store-flag values. Be careful to use copy_rtx
5506 here since true_rtx or false_rtx might share RTL with x as a
5507 result of the if_then_else_cond call above. */
5508 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5509 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5511 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5512 is unlikely to be simpler. */
5513 if (general_operand (true_rtx, VOIDmode)
5514 && general_operand (false_rtx, VOIDmode))
5516 enum rtx_code reversed;
5518 /* Restarting if we generate a store-flag expression will cause
5519 us to loop. Just drop through in this case. */
5521 /* If the result values are STORE_FLAG_VALUE and zero, we can
5522 just make the comparison operation. */
5523 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5524 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5525 cond, cop1);
5526 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5527 && ((reversed = reversed_comparison_code_parts
5528 (cond_code, cond, cop1, NULL))
5529 != UNKNOWN))
5530 x = simplify_gen_relational (reversed, mode, VOIDmode,
5531 cond, cop1);
5533 /* Likewise, we can make the negate of a comparison operation
5534 if the result values are - STORE_FLAG_VALUE and zero. */
5535 else if (CONST_INT_P (true_rtx)
5536 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5537 && false_rtx == const0_rtx)
5538 x = simplify_gen_unary (NEG, mode,
5539 simplify_gen_relational (cond_code,
5540 mode, VOIDmode,
5541 cond, cop1),
5542 mode);
5543 else if (CONST_INT_P (false_rtx)
5544 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5545 && true_rtx == const0_rtx
5546 && ((reversed = reversed_comparison_code_parts
5547 (cond_code, cond, cop1, NULL))
5548 != UNKNOWN))
5549 x = simplify_gen_unary (NEG, mode,
5550 simplify_gen_relational (reversed,
5551 mode, VOIDmode,
5552 cond, cop1),
5553 mode);
5554 else
5555 return gen_rtx_IF_THEN_ELSE (mode,
5556 simplify_gen_relational (cond_code,
5557 mode,
5558 VOIDmode,
5559 cond,
5560 cop1),
5561 true_rtx, false_rtx);
5563 code = GET_CODE (x);
5564 op0_mode = VOIDmode;
5569 /* Try to fold this expression in case we have constants that weren't
5570 present before. */
5571 temp = 0;
5572 switch (GET_RTX_CLASS (code))
5574 case RTX_UNARY:
5575 if (op0_mode == VOIDmode)
5576 op0_mode = GET_MODE (XEXP (x, 0));
5577 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5578 break;
5579 case RTX_COMPARE:
5580 case RTX_COMM_COMPARE:
5582 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5583 if (cmp_mode == VOIDmode)
5585 cmp_mode = GET_MODE (XEXP (x, 1));
5586 if (cmp_mode == VOIDmode)
5587 cmp_mode = op0_mode;
5589 temp = simplify_relational_operation (code, mode, cmp_mode,
5590 XEXP (x, 0), XEXP (x, 1));
5592 break;
5593 case RTX_COMM_ARITH:
5594 case RTX_BIN_ARITH:
5595 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5596 break;
5597 case RTX_BITFIELD_OPS:
5598 case RTX_TERNARY:
5599 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5600 XEXP (x, 1), XEXP (x, 2));
5601 break;
5602 default:
5603 break;
5606 if (temp)
5608 x = temp;
5609 code = GET_CODE (temp);
5610 op0_mode = VOIDmode;
5611 mode = GET_MODE (temp);
5614 /* First see if we can apply the inverse distributive law. */
5615 if (code == PLUS || code == MINUS
5616 || code == AND || code == IOR || code == XOR)
5618 x = apply_distributive_law (x);
5619 code = GET_CODE (x);
5620 op0_mode = VOIDmode;
5623 /* If CODE is an associative operation not otherwise handled, see if we
5624 can associate some operands. This can win if they are constants or
5625 if they are logically related (i.e. (a & b) & a). */
5626 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5627 || code == AND || code == IOR || code == XOR
5628 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5629 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5630 || (flag_associative_math && FLOAT_MODE_P (mode))))
5632 if (GET_CODE (XEXP (x, 0)) == code)
5634 rtx other = XEXP (XEXP (x, 0), 0);
5635 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5636 rtx inner_op1 = XEXP (x, 1);
5637 rtx inner;
5639 /* Make sure we pass the constant operand if any as the second
5640 one if this is a commutative operation. */
5641 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5643 rtx tem = inner_op0;
5644 inner_op0 = inner_op1;
5645 inner_op1 = tem;
5647 inner = simplify_binary_operation (code == MINUS ? PLUS
5648 : code == DIV ? MULT
5649 : code,
5650 mode, inner_op0, inner_op1);
5652 /* For commutative operations, try the other pair if that one
5653 didn't simplify. */
5654 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5656 other = XEXP (XEXP (x, 0), 1);
5657 inner = simplify_binary_operation (code, mode,
5658 XEXP (XEXP (x, 0), 0),
5659 XEXP (x, 1));
5662 if (inner)
5663 return simplify_gen_binary (code, mode, other, inner);
5667 /* A little bit of algebraic simplification here. */
5668 switch (code)
5670 case MEM:
5671 /* Ensure that our address has any ASHIFTs converted to MULT in case
5672 address-recognizing predicates are called later. */
5673 temp = make_compound_operation (XEXP (x, 0), MEM);
5674 SUBST (XEXP (x, 0), temp);
5675 break;
5677 case SUBREG:
5678 if (op0_mode == VOIDmode)
5679 op0_mode = GET_MODE (SUBREG_REG (x));
5681 /* See if this can be moved to simplify_subreg. */
5682 if (CONSTANT_P (SUBREG_REG (x))
5683 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5684 /* Don't call gen_lowpart if the inner mode
5685 is VOIDmode and we cannot simplify it, as SUBREG without
5686 inner mode is invalid. */
5687 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5688 || gen_lowpart_common (mode, SUBREG_REG (x))))
5689 return gen_lowpart (mode, SUBREG_REG (x));
5691 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5692 break;
5694 rtx temp;
5695 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5696 SUBREG_BYTE (x));
5697 if (temp)
5698 return temp;
5700 /* If op is known to have all lower bits zero, the result is zero. */
5701 if (!in_dest
5702 && SCALAR_INT_MODE_P (mode)
5703 && SCALAR_INT_MODE_P (op0_mode)
5704 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5705 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5706 && HWI_COMPUTABLE_MODE_P (op0_mode)
5707 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5708 & GET_MODE_MASK (mode)) == 0)
5709 return CONST0_RTX (mode);
5712 /* Don't change the mode of the MEM if that would change the meaning
5713 of the address. */
5714 if (MEM_P (SUBREG_REG (x))
5715 && (MEM_VOLATILE_P (SUBREG_REG (x))
5716 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5717 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5718 return gen_rtx_CLOBBER (mode, const0_rtx);
5720 /* Note that we cannot do any narrowing for non-constants since
5721 we might have been counting on using the fact that some bits were
5722 zero. We now do this in the SET. */
5724 break;
5726 case NEG:
5727 temp = expand_compound_operation (XEXP (x, 0));
5729 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5730 replaced by (lshiftrt X C). This will convert
5731 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5733 if (GET_CODE (temp) == ASHIFTRT
5734 && CONST_INT_P (XEXP (temp, 1))
5735 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5736 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5737 INTVAL (XEXP (temp, 1)));
5739 /* If X has only a single bit that might be nonzero, say, bit I, convert
5740 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5741 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5742 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5743 or a SUBREG of one since we'd be making the expression more
5744 complex if it was just a register. */
5746 if (!REG_P (temp)
5747 && ! (GET_CODE (temp) == SUBREG
5748 && REG_P (SUBREG_REG (temp)))
5749 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5751 rtx temp1 = simplify_shift_const
5752 (NULL_RTX, ASHIFTRT, mode,
5753 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5754 GET_MODE_PRECISION (mode) - 1 - i),
5755 GET_MODE_PRECISION (mode) - 1 - i);
5757 /* If all we did was surround TEMP with the two shifts, we
5758 haven't improved anything, so don't use it. Otherwise,
5759 we are better off with TEMP1. */
5760 if (GET_CODE (temp1) != ASHIFTRT
5761 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5762 || XEXP (XEXP (temp1, 0), 0) != temp)
5763 return temp1;
5765 break;
5767 case TRUNCATE:
5768 /* We can't handle truncation to a partial integer mode here
5769 because we don't know the real bitsize of the partial
5770 integer mode. */
5771 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5772 break;
5774 if (HWI_COMPUTABLE_MODE_P (mode))
5775 SUBST (XEXP (x, 0),
5776 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5777 GET_MODE_MASK (mode), 0));
5779 /* We can truncate a constant value and return it. */
5780 if (CONST_INT_P (XEXP (x, 0)))
5781 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5783 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5784 whose value is a comparison can be replaced with a subreg if
5785 STORE_FLAG_VALUE permits. */
5786 if (HWI_COMPUTABLE_MODE_P (mode)
5787 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5788 && (temp = get_last_value (XEXP (x, 0)))
5789 && COMPARISON_P (temp))
5790 return gen_lowpart (mode, XEXP (x, 0));
5791 break;
5793 case CONST:
5794 /* (const (const X)) can become (const X). Do it this way rather than
5795 returning the inner CONST since CONST can be shared with a
5796 REG_EQUAL note. */
5797 if (GET_CODE (XEXP (x, 0)) == CONST)
5798 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5799 break;
5801 #ifdef HAVE_lo_sum
5802 case LO_SUM:
5803 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5804 can add in an offset. find_split_point will split this address up
5805 again if it doesn't match. */
5806 if (GET_CODE (XEXP (x, 0)) == HIGH
5807 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5808 return XEXP (x, 1);
5809 break;
5810 #endif
5812 case PLUS:
5813 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5814 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5815 bit-field and can be replaced by either a sign_extend or a
5816 sign_extract. The `and' may be a zero_extend and the two
5817 <c>, -<c> constants may be reversed. */
5818 if (GET_CODE (XEXP (x, 0)) == XOR
5819 && CONST_INT_P (XEXP (x, 1))
5820 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5821 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5822 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5823 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5824 && HWI_COMPUTABLE_MODE_P (mode)
5825 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5826 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5827 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5828 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5829 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5830 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5831 == (unsigned int) i + 1))))
5832 return simplify_shift_const
5833 (NULL_RTX, ASHIFTRT, mode,
5834 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5835 XEXP (XEXP (XEXP (x, 0), 0), 0),
5836 GET_MODE_PRECISION (mode) - (i + 1)),
5837 GET_MODE_PRECISION (mode) - (i + 1));
5839 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5840 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5841 the bitsize of the mode - 1. This allows simplification of
5842 "a = (b & 8) == 0;" */
5843 if (XEXP (x, 1) == constm1_rtx
5844 && !REG_P (XEXP (x, 0))
5845 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5846 && REG_P (SUBREG_REG (XEXP (x, 0))))
5847 && nonzero_bits (XEXP (x, 0), mode) == 1)
5848 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5849 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5850 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5851 GET_MODE_PRECISION (mode) - 1),
5852 GET_MODE_PRECISION (mode) - 1);
5854 /* If we are adding two things that have no bits in common, convert
5855 the addition into an IOR. This will often be further simplified,
5856 for example in cases like ((a & 1) + (a & 2)), which can
5857 become a & 3. */
5859 if (HWI_COMPUTABLE_MODE_P (mode)
5860 && (nonzero_bits (XEXP (x, 0), mode)
5861 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5863 /* Try to simplify the expression further. */
5864 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5865 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5867 /* If we could, great. If not, do not go ahead with the IOR
5868 replacement, since PLUS appears in many special purpose
5869 address arithmetic instructions. */
5870 if (GET_CODE (temp) != CLOBBER
5871 && (GET_CODE (temp) != IOR
5872 || ((XEXP (temp, 0) != XEXP (x, 0)
5873 || XEXP (temp, 1) != XEXP (x, 1))
5874 && (XEXP (temp, 0) != XEXP (x, 1)
5875 || XEXP (temp, 1) != XEXP (x, 0)))))
5876 return temp;
5878 break;
5880 case MINUS:
5881 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5882 (and <foo> (const_int pow2-1)) */
5883 if (GET_CODE (XEXP (x, 1)) == AND
5884 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5885 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5886 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5887 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5888 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5889 break;
5891 case MULT:
5892 /* If we have (mult (plus A B) C), apply the distributive law and then
5893 the inverse distributive law to see if things simplify. This
5894 occurs mostly in addresses, often when unrolling loops. */
5896 if (GET_CODE (XEXP (x, 0)) == PLUS)
5898 rtx result = distribute_and_simplify_rtx (x, 0);
5899 if (result)
5900 return result;
5903 /* Try simplify a*(b/c) as (a*b)/c. */
5904 if (FLOAT_MODE_P (mode) && flag_associative_math
5905 && GET_CODE (XEXP (x, 0)) == DIV)
5907 rtx tem = simplify_binary_operation (MULT, mode,
5908 XEXP (XEXP (x, 0), 0),
5909 XEXP (x, 1));
5910 if (tem)
5911 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5913 break;
5915 case UDIV:
5916 /* If this is a divide by a power of two, treat it as a shift if
5917 its first operand is a shift. */
5918 if (CONST_INT_P (XEXP (x, 1))
5919 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5920 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5921 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5922 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5923 || GET_CODE (XEXP (x, 0)) == ROTATE
5924 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5925 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5926 break;
5928 case EQ: case NE:
5929 case GT: case GTU: case GE: case GEU:
5930 case LT: case LTU: case LE: case LEU:
5931 case UNEQ: case LTGT:
5932 case UNGT: case UNGE:
5933 case UNLT: case UNLE:
5934 case UNORDERED: case ORDERED:
5935 /* If the first operand is a condition code, we can't do anything
5936 with it. */
5937 if (GET_CODE (XEXP (x, 0)) == COMPARE
5938 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5939 && ! CC0_P (XEXP (x, 0))))
5941 rtx op0 = XEXP (x, 0);
5942 rtx op1 = XEXP (x, 1);
5943 enum rtx_code new_code;
5945 if (GET_CODE (op0) == COMPARE)
5946 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5948 /* Simplify our comparison, if possible. */
5949 new_code = simplify_comparison (code, &op0, &op1);
5951 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5952 if only the low-order bit is possibly nonzero in X (such as when
5953 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5954 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5955 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5956 (plus X 1).
5958 Remove any ZERO_EXTRACT we made when thinking this was a
5959 comparison. It may now be simpler to use, e.g., an AND. If a
5960 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5961 the call to make_compound_operation in the SET case.
5963 Don't apply these optimizations if the caller would
5964 prefer a comparison rather than a value.
5965 E.g., for the condition in an IF_THEN_ELSE most targets need
5966 an explicit comparison. */
5968 if (in_cond)
5971 else if (STORE_FLAG_VALUE == 1
5972 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5973 && op1 == const0_rtx
5974 && mode == GET_MODE (op0)
5975 && nonzero_bits (op0, mode) == 1)
5976 return gen_lowpart (mode,
5977 expand_compound_operation (op0));
5979 else if (STORE_FLAG_VALUE == 1
5980 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5981 && op1 == const0_rtx
5982 && mode == GET_MODE (op0)
5983 && (num_sign_bit_copies (op0, mode)
5984 == GET_MODE_PRECISION (mode)))
5986 op0 = expand_compound_operation (op0);
5987 return simplify_gen_unary (NEG, mode,
5988 gen_lowpart (mode, op0),
5989 mode);
5992 else if (STORE_FLAG_VALUE == 1
5993 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5994 && op1 == const0_rtx
5995 && mode == GET_MODE (op0)
5996 && nonzero_bits (op0, mode) == 1)
5998 op0 = expand_compound_operation (op0);
5999 return simplify_gen_binary (XOR, mode,
6000 gen_lowpart (mode, op0),
6001 const1_rtx);
6004 else if (STORE_FLAG_VALUE == 1
6005 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6006 && op1 == const0_rtx
6007 && mode == GET_MODE (op0)
6008 && (num_sign_bit_copies (op0, mode)
6009 == GET_MODE_PRECISION (mode)))
6011 op0 = expand_compound_operation (op0);
6012 return plus_constant (mode, gen_lowpart (mode, op0), 1);
6015 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6016 those above. */
6017 if (in_cond)
6020 else if (STORE_FLAG_VALUE == -1
6021 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6022 && op1 == const0_rtx
6023 && mode == GET_MODE (op0)
6024 && (num_sign_bit_copies (op0, mode)
6025 == GET_MODE_PRECISION (mode)))
6026 return gen_lowpart (mode,
6027 expand_compound_operation (op0));
6029 else if (STORE_FLAG_VALUE == -1
6030 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6031 && op1 == const0_rtx
6032 && mode == GET_MODE (op0)
6033 && nonzero_bits (op0, mode) == 1)
6035 op0 = expand_compound_operation (op0);
6036 return simplify_gen_unary (NEG, mode,
6037 gen_lowpart (mode, op0),
6038 mode);
6041 else if (STORE_FLAG_VALUE == -1
6042 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6043 && op1 == const0_rtx
6044 && mode == GET_MODE (op0)
6045 && (num_sign_bit_copies (op0, mode)
6046 == GET_MODE_PRECISION (mode)))
6048 op0 = expand_compound_operation (op0);
6049 return simplify_gen_unary (NOT, mode,
6050 gen_lowpart (mode, op0),
6051 mode);
6054 /* If X is 0/1, (eq X 0) is X-1. */
6055 else if (STORE_FLAG_VALUE == -1
6056 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6057 && op1 == const0_rtx
6058 && mode == GET_MODE (op0)
6059 && nonzero_bits (op0, mode) == 1)
6061 op0 = expand_compound_operation (op0);
6062 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6065 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6066 one bit that might be nonzero, we can convert (ne x 0) to
6067 (ashift x c) where C puts the bit in the sign bit. Remove any
6068 AND with STORE_FLAG_VALUE when we are done, since we are only
6069 going to test the sign bit. */
6070 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6071 && HWI_COMPUTABLE_MODE_P (mode)
6072 && val_signbit_p (mode, STORE_FLAG_VALUE)
6073 && op1 == const0_rtx
6074 && mode == GET_MODE (op0)
6075 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6077 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6078 expand_compound_operation (op0),
6079 GET_MODE_PRECISION (mode) - 1 - i);
6080 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6081 return XEXP (x, 0);
6082 else
6083 return x;
6086 /* If the code changed, return a whole new comparison.
6087 We also need to avoid using SUBST in cases where
6088 simplify_comparison has widened a comparison with a CONST_INT,
6089 since in that case the wider CONST_INT may fail the sanity
6090 checks in do_SUBST. */
6091 if (new_code != code
6092 || (CONST_INT_P (op1)
6093 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6094 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6095 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6097 /* Otherwise, keep this operation, but maybe change its operands.
6098 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6099 SUBST (XEXP (x, 0), op0);
6100 SUBST (XEXP (x, 1), op1);
6102 break;
6104 case IF_THEN_ELSE:
6105 return simplify_if_then_else (x);
6107 case ZERO_EXTRACT:
6108 case SIGN_EXTRACT:
6109 case ZERO_EXTEND:
6110 case SIGN_EXTEND:
6111 /* If we are processing SET_DEST, we are done. */
6112 if (in_dest)
6113 return x;
6115 return expand_compound_operation (x);
6117 case SET:
6118 return simplify_set (x);
6120 case AND:
6121 case IOR:
6122 return simplify_logical (x);
6124 case ASHIFT:
6125 case LSHIFTRT:
6126 case ASHIFTRT:
6127 case ROTATE:
6128 case ROTATERT:
6129 /* If this is a shift by a constant amount, simplify it. */
6130 if (CONST_INT_P (XEXP (x, 1)))
6131 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6132 INTVAL (XEXP (x, 1)));
6134 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6135 SUBST (XEXP (x, 1),
6136 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6137 ((unsigned HOST_WIDE_INT) 1
6138 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6139 - 1,
6140 0));
6141 break;
6143 default:
6144 break;
6147 return x;
6150 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6152 static rtx
6153 simplify_if_then_else (rtx x)
6155 machine_mode mode = GET_MODE (x);
6156 rtx cond = XEXP (x, 0);
6157 rtx true_rtx = XEXP (x, 1);
6158 rtx false_rtx = XEXP (x, 2);
6159 enum rtx_code true_code = GET_CODE (cond);
6160 int comparison_p = COMPARISON_P (cond);
6161 rtx temp;
6162 int i;
6163 enum rtx_code false_code;
6164 rtx reversed;
6166 /* Simplify storing of the truth value. */
6167 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6168 return simplify_gen_relational (true_code, mode, VOIDmode,
6169 XEXP (cond, 0), XEXP (cond, 1));
6171 /* Also when the truth value has to be reversed. */
6172 if (comparison_p
6173 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6174 && (reversed = reversed_comparison (cond, mode)))
6175 return reversed;
6177 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6178 in it is being compared against certain values. Get the true and false
6179 comparisons and see if that says anything about the value of each arm. */
6181 if (comparison_p
6182 && ((false_code = reversed_comparison_code (cond, NULL))
6183 != UNKNOWN)
6184 && REG_P (XEXP (cond, 0)))
6186 HOST_WIDE_INT nzb;
6187 rtx from = XEXP (cond, 0);
6188 rtx true_val = XEXP (cond, 1);
6189 rtx false_val = true_val;
6190 int swapped = 0;
6192 /* If FALSE_CODE is EQ, swap the codes and arms. */
6194 if (false_code == EQ)
6196 swapped = 1, true_code = EQ, false_code = NE;
6197 std::swap (true_rtx, false_rtx);
6200 /* If we are comparing against zero and the expression being tested has
6201 only a single bit that might be nonzero, that is its value when it is
6202 not equal to zero. Similarly if it is known to be -1 or 0. */
6204 if (true_code == EQ && true_val == const0_rtx
6205 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6207 false_code = EQ;
6208 false_val = gen_int_mode (nzb, GET_MODE (from));
6210 else if (true_code == EQ && true_val == const0_rtx
6211 && (num_sign_bit_copies (from, GET_MODE (from))
6212 == GET_MODE_PRECISION (GET_MODE (from))))
6214 false_code = EQ;
6215 false_val = constm1_rtx;
6218 /* Now simplify an arm if we know the value of the register in the
6219 branch and it is used in the arm. Be careful due to the potential
6220 of locally-shared RTL. */
6222 if (reg_mentioned_p (from, true_rtx))
6223 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6224 from, true_val),
6225 pc_rtx, pc_rtx, 0, 0, 0);
6226 if (reg_mentioned_p (from, false_rtx))
6227 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6228 from, false_val),
6229 pc_rtx, pc_rtx, 0, 0, 0);
6231 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6232 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6234 true_rtx = XEXP (x, 1);
6235 false_rtx = XEXP (x, 2);
6236 true_code = GET_CODE (cond);
6239 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6240 reversed, do so to avoid needing two sets of patterns for
6241 subtract-and-branch insns. Similarly if we have a constant in the true
6242 arm, the false arm is the same as the first operand of the comparison, or
6243 the false arm is more complicated than the true arm. */
6245 if (comparison_p
6246 && reversed_comparison_code (cond, NULL) != UNKNOWN
6247 && (true_rtx == pc_rtx
6248 || (CONSTANT_P (true_rtx)
6249 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6250 || true_rtx == const0_rtx
6251 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6252 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6253 && !OBJECT_P (false_rtx))
6254 || reg_mentioned_p (true_rtx, false_rtx)
6255 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6257 true_code = reversed_comparison_code (cond, NULL);
6258 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6259 SUBST (XEXP (x, 1), false_rtx);
6260 SUBST (XEXP (x, 2), true_rtx);
6262 std::swap (true_rtx, false_rtx);
6263 cond = XEXP (x, 0);
6265 /* It is possible that the conditional has been simplified out. */
6266 true_code = GET_CODE (cond);
6267 comparison_p = COMPARISON_P (cond);
6270 /* If the two arms are identical, we don't need the comparison. */
6272 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6273 return true_rtx;
6275 /* Convert a == b ? b : a to "a". */
6276 if (true_code == EQ && ! side_effects_p (cond)
6277 && !HONOR_NANS (mode)
6278 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6279 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6280 return false_rtx;
6281 else if (true_code == NE && ! side_effects_p (cond)
6282 && !HONOR_NANS (mode)
6283 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6284 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6285 return true_rtx;
6287 /* Look for cases where we have (abs x) or (neg (abs X)). */
6289 if (GET_MODE_CLASS (mode) == MODE_INT
6290 && comparison_p
6291 && XEXP (cond, 1) == const0_rtx
6292 && GET_CODE (false_rtx) == NEG
6293 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6294 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6295 && ! side_effects_p (true_rtx))
6296 switch (true_code)
6298 case GT:
6299 case GE:
6300 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6301 case LT:
6302 case LE:
6303 return
6304 simplify_gen_unary (NEG, mode,
6305 simplify_gen_unary (ABS, mode, true_rtx, mode),
6306 mode);
6307 default:
6308 break;
6311 /* Look for MIN or MAX. */
6313 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6314 && comparison_p
6315 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6316 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6317 && ! side_effects_p (cond))
6318 switch (true_code)
6320 case GE:
6321 case GT:
6322 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6323 case LE:
6324 case LT:
6325 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6326 case GEU:
6327 case GTU:
6328 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6329 case LEU:
6330 case LTU:
6331 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6332 default:
6333 break;
6336 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6337 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6338 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6339 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6340 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6341 neither 1 or -1, but it isn't worth checking for. */
6343 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6344 && comparison_p
6345 && GET_MODE_CLASS (mode) == MODE_INT
6346 && ! side_effects_p (x))
6348 rtx t = make_compound_operation (true_rtx, SET);
6349 rtx f = make_compound_operation (false_rtx, SET);
6350 rtx cond_op0 = XEXP (cond, 0);
6351 rtx cond_op1 = XEXP (cond, 1);
6352 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6353 machine_mode m = mode;
6354 rtx z = 0, c1 = NULL_RTX;
6356 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6357 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6358 || GET_CODE (t) == ASHIFT
6359 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6360 && rtx_equal_p (XEXP (t, 0), f))
6361 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6363 /* If an identity-zero op is commutative, check whether there
6364 would be a match if we swapped the operands. */
6365 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6366 || GET_CODE (t) == XOR)
6367 && rtx_equal_p (XEXP (t, 1), f))
6368 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6369 else if (GET_CODE (t) == SIGN_EXTEND
6370 && (GET_CODE (XEXP (t, 0)) == PLUS
6371 || GET_CODE (XEXP (t, 0)) == MINUS
6372 || GET_CODE (XEXP (t, 0)) == IOR
6373 || GET_CODE (XEXP (t, 0)) == XOR
6374 || GET_CODE (XEXP (t, 0)) == ASHIFT
6375 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6376 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6377 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6378 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6379 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6380 && (num_sign_bit_copies (f, GET_MODE (f))
6381 > (unsigned int)
6382 (GET_MODE_PRECISION (mode)
6383 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6385 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6386 extend_op = SIGN_EXTEND;
6387 m = GET_MODE (XEXP (t, 0));
6389 else if (GET_CODE (t) == SIGN_EXTEND
6390 && (GET_CODE (XEXP (t, 0)) == PLUS
6391 || GET_CODE (XEXP (t, 0)) == IOR
6392 || GET_CODE (XEXP (t, 0)) == XOR)
6393 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6394 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6395 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6396 && (num_sign_bit_copies (f, GET_MODE (f))
6397 > (unsigned int)
6398 (GET_MODE_PRECISION (mode)
6399 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6401 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6402 extend_op = SIGN_EXTEND;
6403 m = GET_MODE (XEXP (t, 0));
6405 else if (GET_CODE (t) == ZERO_EXTEND
6406 && (GET_CODE (XEXP (t, 0)) == PLUS
6407 || GET_CODE (XEXP (t, 0)) == MINUS
6408 || GET_CODE (XEXP (t, 0)) == IOR
6409 || GET_CODE (XEXP (t, 0)) == XOR
6410 || GET_CODE (XEXP (t, 0)) == ASHIFT
6411 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6412 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6413 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6414 && HWI_COMPUTABLE_MODE_P (mode)
6415 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6416 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6417 && ((nonzero_bits (f, GET_MODE (f))
6418 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6419 == 0))
6421 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6422 extend_op = ZERO_EXTEND;
6423 m = GET_MODE (XEXP (t, 0));
6425 else if (GET_CODE (t) == ZERO_EXTEND
6426 && (GET_CODE (XEXP (t, 0)) == PLUS
6427 || GET_CODE (XEXP (t, 0)) == IOR
6428 || GET_CODE (XEXP (t, 0)) == XOR)
6429 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6430 && HWI_COMPUTABLE_MODE_P (mode)
6431 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6432 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6433 && ((nonzero_bits (f, GET_MODE (f))
6434 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6435 == 0))
6437 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6438 extend_op = ZERO_EXTEND;
6439 m = GET_MODE (XEXP (t, 0));
6442 if (z)
6444 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6445 cond_op0, cond_op1),
6446 pc_rtx, pc_rtx, 0, 0, 0);
6447 temp = simplify_gen_binary (MULT, m, temp,
6448 simplify_gen_binary (MULT, m, c1,
6449 const_true_rtx));
6450 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6451 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6453 if (extend_op != UNKNOWN)
6454 temp = simplify_gen_unary (extend_op, mode, temp, m);
6456 return temp;
6460 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6461 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6462 negation of a single bit, we can convert this operation to a shift. We
6463 can actually do this more generally, but it doesn't seem worth it. */
6465 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6466 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6467 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6468 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6469 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6470 == GET_MODE_PRECISION (mode))
6471 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6472 return
6473 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6474 gen_lowpart (mode, XEXP (cond, 0)), i);
6476 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6477 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6478 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6479 && GET_MODE (XEXP (cond, 0)) == mode
6480 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6481 == nonzero_bits (XEXP (cond, 0), mode)
6482 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6483 return XEXP (cond, 0);
6485 return x;
6488 /* Simplify X, a SET expression. Return the new expression. */
6490 static rtx
6491 simplify_set (rtx x)
6493 rtx src = SET_SRC (x);
6494 rtx dest = SET_DEST (x);
6495 machine_mode mode
6496 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6497 rtx_insn *other_insn;
6498 rtx *cc_use;
6500 /* (set (pc) (return)) gets written as (return). */
6501 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6502 return src;
6504 /* Now that we know for sure which bits of SRC we are using, see if we can
6505 simplify the expression for the object knowing that we only need the
6506 low-order bits. */
6508 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6510 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6511 SUBST (SET_SRC (x), src);
6514 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6515 the comparison result and try to simplify it unless we already have used
6516 undobuf.other_insn. */
6517 if ((GET_MODE_CLASS (mode) == MODE_CC
6518 || GET_CODE (src) == COMPARE
6519 || CC0_P (dest))
6520 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6521 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6522 && COMPARISON_P (*cc_use)
6523 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6525 enum rtx_code old_code = GET_CODE (*cc_use);
6526 enum rtx_code new_code;
6527 rtx op0, op1, tmp;
6528 int other_changed = 0;
6529 rtx inner_compare = NULL_RTX;
6530 machine_mode compare_mode = GET_MODE (dest);
6532 if (GET_CODE (src) == COMPARE)
6534 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6535 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6537 inner_compare = op0;
6538 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6541 else
6542 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6544 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6545 op0, op1);
6546 if (!tmp)
6547 new_code = old_code;
6548 else if (!CONSTANT_P (tmp))
6550 new_code = GET_CODE (tmp);
6551 op0 = XEXP (tmp, 0);
6552 op1 = XEXP (tmp, 1);
6554 else
6556 rtx pat = PATTERN (other_insn);
6557 undobuf.other_insn = other_insn;
6558 SUBST (*cc_use, tmp);
6560 /* Attempt to simplify CC user. */
6561 if (GET_CODE (pat) == SET)
6563 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6564 if (new_rtx != NULL_RTX)
6565 SUBST (SET_SRC (pat), new_rtx);
6568 /* Convert X into a no-op move. */
6569 SUBST (SET_DEST (x), pc_rtx);
6570 SUBST (SET_SRC (x), pc_rtx);
6571 return x;
6574 /* Simplify our comparison, if possible. */
6575 new_code = simplify_comparison (new_code, &op0, &op1);
6577 #ifdef SELECT_CC_MODE
6578 /* If this machine has CC modes other than CCmode, check to see if we
6579 need to use a different CC mode here. */
6580 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6581 compare_mode = GET_MODE (op0);
6582 else if (inner_compare
6583 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6584 && new_code == old_code
6585 && op0 == XEXP (inner_compare, 0)
6586 && op1 == XEXP (inner_compare, 1))
6587 compare_mode = GET_MODE (inner_compare);
6588 else
6589 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6591 /* If the mode changed, we have to change SET_DEST, the mode in the
6592 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6593 a hard register, just build new versions with the proper mode. If it
6594 is a pseudo, we lose unless it is only time we set the pseudo, in
6595 which case we can safely change its mode. */
6596 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6598 if (can_change_dest_mode (dest, 0, compare_mode))
6600 unsigned int regno = REGNO (dest);
6601 rtx new_dest;
6603 if (regno < FIRST_PSEUDO_REGISTER)
6604 new_dest = gen_rtx_REG (compare_mode, regno);
6605 else
6607 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6608 new_dest = regno_reg_rtx[regno];
6611 SUBST (SET_DEST (x), new_dest);
6612 SUBST (XEXP (*cc_use, 0), new_dest);
6613 other_changed = 1;
6615 dest = new_dest;
6618 #endif /* SELECT_CC_MODE */
6620 /* If the code changed, we have to build a new comparison in
6621 undobuf.other_insn. */
6622 if (new_code != old_code)
6624 int other_changed_previously = other_changed;
6625 unsigned HOST_WIDE_INT mask;
6626 rtx old_cc_use = *cc_use;
6628 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6629 dest, const0_rtx));
6630 other_changed = 1;
6632 /* If the only change we made was to change an EQ into an NE or
6633 vice versa, OP0 has only one bit that might be nonzero, and OP1
6634 is zero, check if changing the user of the condition code will
6635 produce a valid insn. If it won't, we can keep the original code
6636 in that insn by surrounding our operation with an XOR. */
6638 if (((old_code == NE && new_code == EQ)
6639 || (old_code == EQ && new_code == NE))
6640 && ! other_changed_previously && op1 == const0_rtx
6641 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6642 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6644 rtx pat = PATTERN (other_insn), note = 0;
6646 if ((recog_for_combine (&pat, other_insn, &note) < 0
6647 && ! check_asm_operands (pat)))
6649 *cc_use = old_cc_use;
6650 other_changed = 0;
6652 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6653 gen_int_mode (mask,
6654 GET_MODE (op0)));
6659 if (other_changed)
6660 undobuf.other_insn = other_insn;
6662 /* Otherwise, if we didn't previously have a COMPARE in the
6663 correct mode, we need one. */
6664 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6666 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6667 src = SET_SRC (x);
6669 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6671 SUBST (SET_SRC (x), op0);
6672 src = SET_SRC (x);
6674 /* Otherwise, update the COMPARE if needed. */
6675 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6677 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6678 src = SET_SRC (x);
6681 else
6683 /* Get SET_SRC in a form where we have placed back any
6684 compound expressions. Then do the checks below. */
6685 src = make_compound_operation (src, SET);
6686 SUBST (SET_SRC (x), src);
6689 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6690 and X being a REG or (subreg (reg)), we may be able to convert this to
6691 (set (subreg:m2 x) (op)).
6693 We can always do this if M1 is narrower than M2 because that means that
6694 we only care about the low bits of the result.
6696 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6697 perform a narrower operation than requested since the high-order bits will
6698 be undefined. On machine where it is defined, this transformation is safe
6699 as long as M1 and M2 have the same number of words. */
6701 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6702 && !OBJECT_P (SUBREG_REG (src))
6703 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6704 / UNITS_PER_WORD)
6705 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6706 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6707 #ifndef WORD_REGISTER_OPERATIONS
6708 && (GET_MODE_SIZE (GET_MODE (src))
6709 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6710 #endif
6711 #ifdef CANNOT_CHANGE_MODE_CLASS
6712 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6713 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6714 GET_MODE (SUBREG_REG (src)),
6715 GET_MODE (src)))
6716 #endif
6717 && (REG_P (dest)
6718 || (GET_CODE (dest) == SUBREG
6719 && REG_P (SUBREG_REG (dest)))))
6721 SUBST (SET_DEST (x),
6722 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6723 dest));
6724 SUBST (SET_SRC (x), SUBREG_REG (src));
6726 src = SET_SRC (x), dest = SET_DEST (x);
6729 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6730 in SRC. */
6731 if (dest == cc0_rtx
6732 && GET_CODE (src) == SUBREG
6733 && subreg_lowpart_p (src)
6734 && (GET_MODE_PRECISION (GET_MODE (src))
6735 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6737 rtx inner = SUBREG_REG (src);
6738 machine_mode inner_mode = GET_MODE (inner);
6740 /* Here we make sure that we don't have a sign bit on. */
6741 if (val_signbit_known_clear_p (GET_MODE (src),
6742 nonzero_bits (inner, inner_mode)))
6744 SUBST (SET_SRC (x), inner);
6745 src = SET_SRC (x);
6749 #ifdef LOAD_EXTEND_OP
6750 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6751 would require a paradoxical subreg. Replace the subreg with a
6752 zero_extend to avoid the reload that would otherwise be required. */
6754 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6755 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6756 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6757 && SUBREG_BYTE (src) == 0
6758 && paradoxical_subreg_p (src)
6759 && MEM_P (SUBREG_REG (src)))
6761 SUBST (SET_SRC (x),
6762 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6763 GET_MODE (src), SUBREG_REG (src)));
6765 src = SET_SRC (x);
6767 #endif
6769 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6770 are comparing an item known to be 0 or -1 against 0, use a logical
6771 operation instead. Check for one of the arms being an IOR of the other
6772 arm with some value. We compute three terms to be IOR'ed together. In
6773 practice, at most two will be nonzero. Then we do the IOR's. */
6775 if (GET_CODE (dest) != PC
6776 && GET_CODE (src) == IF_THEN_ELSE
6777 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6778 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6779 && XEXP (XEXP (src, 0), 1) == const0_rtx
6780 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6781 #ifdef HAVE_conditional_move
6782 && ! can_conditionally_move_p (GET_MODE (src))
6783 #endif
6784 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6785 GET_MODE (XEXP (XEXP (src, 0), 0)))
6786 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6787 && ! side_effects_p (src))
6789 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6790 ? XEXP (src, 1) : XEXP (src, 2));
6791 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6792 ? XEXP (src, 2) : XEXP (src, 1));
6793 rtx term1 = const0_rtx, term2, term3;
6795 if (GET_CODE (true_rtx) == IOR
6796 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6797 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6798 else if (GET_CODE (true_rtx) == IOR
6799 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6800 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6801 else if (GET_CODE (false_rtx) == IOR
6802 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6803 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6804 else if (GET_CODE (false_rtx) == IOR
6805 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6806 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6808 term2 = simplify_gen_binary (AND, GET_MODE (src),
6809 XEXP (XEXP (src, 0), 0), true_rtx);
6810 term3 = simplify_gen_binary (AND, GET_MODE (src),
6811 simplify_gen_unary (NOT, GET_MODE (src),
6812 XEXP (XEXP (src, 0), 0),
6813 GET_MODE (src)),
6814 false_rtx);
6816 SUBST (SET_SRC (x),
6817 simplify_gen_binary (IOR, GET_MODE (src),
6818 simplify_gen_binary (IOR, GET_MODE (src),
6819 term1, term2),
6820 term3));
6822 src = SET_SRC (x);
6825 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6826 whole thing fail. */
6827 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6828 return src;
6829 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6830 return dest;
6831 else
6832 /* Convert this into a field assignment operation, if possible. */
6833 return make_field_assignment (x);
6836 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6837 result. */
6839 static rtx
6840 simplify_logical (rtx x)
6842 machine_mode mode = GET_MODE (x);
6843 rtx op0 = XEXP (x, 0);
6844 rtx op1 = XEXP (x, 1);
6846 switch (GET_CODE (x))
6848 case AND:
6849 /* We can call simplify_and_const_int only if we don't lose
6850 any (sign) bits when converting INTVAL (op1) to
6851 "unsigned HOST_WIDE_INT". */
6852 if (CONST_INT_P (op1)
6853 && (HWI_COMPUTABLE_MODE_P (mode)
6854 || INTVAL (op1) > 0))
6856 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6857 if (GET_CODE (x) != AND)
6858 return x;
6860 op0 = XEXP (x, 0);
6861 op1 = XEXP (x, 1);
6864 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6865 apply the distributive law and then the inverse distributive
6866 law to see if things simplify. */
6867 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6869 rtx result = distribute_and_simplify_rtx (x, 0);
6870 if (result)
6871 return result;
6873 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6875 rtx result = distribute_and_simplify_rtx (x, 1);
6876 if (result)
6877 return result;
6879 break;
6881 case IOR:
6882 /* If we have (ior (and A B) C), apply the distributive law and then
6883 the inverse distributive law to see if things simplify. */
6885 if (GET_CODE (op0) == AND)
6887 rtx result = distribute_and_simplify_rtx (x, 0);
6888 if (result)
6889 return result;
6892 if (GET_CODE (op1) == AND)
6894 rtx result = distribute_and_simplify_rtx (x, 1);
6895 if (result)
6896 return result;
6898 break;
6900 default:
6901 gcc_unreachable ();
6904 return x;
6907 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6908 operations" because they can be replaced with two more basic operations.
6909 ZERO_EXTEND is also considered "compound" because it can be replaced with
6910 an AND operation, which is simpler, though only one operation.
6912 The function expand_compound_operation is called with an rtx expression
6913 and will convert it to the appropriate shifts and AND operations,
6914 simplifying at each stage.
6916 The function make_compound_operation is called to convert an expression
6917 consisting of shifts and ANDs into the equivalent compound expression.
6918 It is the inverse of this function, loosely speaking. */
6920 static rtx
6921 expand_compound_operation (rtx x)
6923 unsigned HOST_WIDE_INT pos = 0, len;
6924 int unsignedp = 0;
6925 unsigned int modewidth;
6926 rtx tem;
6928 switch (GET_CODE (x))
6930 case ZERO_EXTEND:
6931 unsignedp = 1;
6932 case SIGN_EXTEND:
6933 /* We can't necessarily use a const_int for a multiword mode;
6934 it depends on implicitly extending the value.
6935 Since we don't know the right way to extend it,
6936 we can't tell whether the implicit way is right.
6938 Even for a mode that is no wider than a const_int,
6939 we can't win, because we need to sign extend one of its bits through
6940 the rest of it, and we don't know which bit. */
6941 if (CONST_INT_P (XEXP (x, 0)))
6942 return x;
6944 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6945 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6946 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6947 reloaded. If not for that, MEM's would very rarely be safe.
6949 Reject MODEs bigger than a word, because we might not be able
6950 to reference a two-register group starting with an arbitrary register
6951 (and currently gen_lowpart might crash for a SUBREG). */
6953 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6954 return x;
6956 /* Reject MODEs that aren't scalar integers because turning vector
6957 or complex modes into shifts causes problems. */
6959 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6960 return x;
6962 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6963 /* If the inner object has VOIDmode (the only way this can happen
6964 is if it is an ASM_OPERANDS), we can't do anything since we don't
6965 know how much masking to do. */
6966 if (len == 0)
6967 return x;
6969 break;
6971 case ZERO_EXTRACT:
6972 unsignedp = 1;
6974 /* ... fall through ... */
6976 case SIGN_EXTRACT:
6977 /* If the operand is a CLOBBER, just return it. */
6978 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6979 return XEXP (x, 0);
6981 if (!CONST_INT_P (XEXP (x, 1))
6982 || !CONST_INT_P (XEXP (x, 2))
6983 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6984 return x;
6986 /* Reject MODEs that aren't scalar integers because turning vector
6987 or complex modes into shifts causes problems. */
6989 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6990 return x;
6992 len = INTVAL (XEXP (x, 1));
6993 pos = INTVAL (XEXP (x, 2));
6995 /* This should stay within the object being extracted, fail otherwise. */
6996 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6997 return x;
6999 if (BITS_BIG_ENDIAN)
7000 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
7002 break;
7004 default:
7005 return x;
7007 /* Convert sign extension to zero extension, if we know that the high
7008 bit is not set, as this is easier to optimize. It will be converted
7009 back to cheaper alternative in make_extraction. */
7010 if (GET_CODE (x) == SIGN_EXTEND
7011 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7012 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7013 & ~(((unsigned HOST_WIDE_INT)
7014 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
7015 >> 1))
7016 == 0)))
7018 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
7019 rtx temp2 = expand_compound_operation (temp);
7021 /* Make sure this is a profitable operation. */
7022 if (set_src_cost (x, optimize_this_for_speed_p)
7023 > set_src_cost (temp2, optimize_this_for_speed_p))
7024 return temp2;
7025 else if (set_src_cost (x, optimize_this_for_speed_p)
7026 > set_src_cost (temp, optimize_this_for_speed_p))
7027 return temp;
7028 else
7029 return x;
7032 /* We can optimize some special cases of ZERO_EXTEND. */
7033 if (GET_CODE (x) == ZERO_EXTEND)
7035 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7036 know that the last value didn't have any inappropriate bits
7037 set. */
7038 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7039 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7040 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7041 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
7042 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7043 return XEXP (XEXP (x, 0), 0);
7045 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7046 if (GET_CODE (XEXP (x, 0)) == SUBREG
7047 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7048 && subreg_lowpart_p (XEXP (x, 0))
7049 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7050 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
7051 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7052 return SUBREG_REG (XEXP (x, 0));
7054 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7055 is a comparison and STORE_FLAG_VALUE permits. This is like
7056 the first case, but it works even when GET_MODE (x) is larger
7057 than HOST_WIDE_INT. */
7058 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7059 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7060 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7061 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7062 <= HOST_BITS_PER_WIDE_INT)
7063 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7064 return XEXP (XEXP (x, 0), 0);
7066 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7067 if (GET_CODE (XEXP (x, 0)) == SUBREG
7068 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7069 && subreg_lowpart_p (XEXP (x, 0))
7070 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7071 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7072 <= HOST_BITS_PER_WIDE_INT)
7073 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7074 return SUBREG_REG (XEXP (x, 0));
7078 /* If we reach here, we want to return a pair of shifts. The inner
7079 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7080 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7081 logical depending on the value of UNSIGNEDP.
7083 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7084 converted into an AND of a shift.
7086 We must check for the case where the left shift would have a negative
7087 count. This can happen in a case like (x >> 31) & 255 on machines
7088 that can't shift by a constant. On those machines, we would first
7089 combine the shift with the AND to produce a variable-position
7090 extraction. Then the constant of 31 would be substituted in
7091 to produce such a position. */
7093 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7094 if (modewidth >= pos + len)
7096 machine_mode mode = GET_MODE (x);
7097 tem = gen_lowpart (mode, XEXP (x, 0));
7098 if (!tem || GET_CODE (tem) == CLOBBER)
7099 return x;
7100 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7101 tem, modewidth - pos - len);
7102 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7103 mode, tem, modewidth - len);
7105 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7106 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7107 simplify_shift_const (NULL_RTX, LSHIFTRT,
7108 GET_MODE (x),
7109 XEXP (x, 0), pos),
7110 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7111 else
7112 /* Any other cases we can't handle. */
7113 return x;
7115 /* If we couldn't do this for some reason, return the original
7116 expression. */
7117 if (GET_CODE (tem) == CLOBBER)
7118 return x;
7120 return tem;
7123 /* X is a SET which contains an assignment of one object into
7124 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7125 or certain SUBREGS). If possible, convert it into a series of
7126 logical operations.
7128 We half-heartedly support variable positions, but do not at all
7129 support variable lengths. */
7131 static const_rtx
7132 expand_field_assignment (const_rtx x)
7134 rtx inner;
7135 rtx pos; /* Always counts from low bit. */
7136 int len;
7137 rtx mask, cleared, masked;
7138 machine_mode compute_mode;
7140 /* Loop until we find something we can't simplify. */
7141 while (1)
7143 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7144 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7146 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7147 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7148 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7150 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7151 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7153 inner = XEXP (SET_DEST (x), 0);
7154 len = INTVAL (XEXP (SET_DEST (x), 1));
7155 pos = XEXP (SET_DEST (x), 2);
7157 /* A constant position should stay within the width of INNER. */
7158 if (CONST_INT_P (pos)
7159 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7160 break;
7162 if (BITS_BIG_ENDIAN)
7164 if (CONST_INT_P (pos))
7165 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7166 - INTVAL (pos));
7167 else if (GET_CODE (pos) == MINUS
7168 && CONST_INT_P (XEXP (pos, 1))
7169 && (INTVAL (XEXP (pos, 1))
7170 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7171 /* If position is ADJUST - X, new position is X. */
7172 pos = XEXP (pos, 0);
7173 else
7175 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7176 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7177 gen_int_mode (prec - len,
7178 GET_MODE (pos)),
7179 pos);
7184 /* A SUBREG between two modes that occupy the same numbers of words
7185 can be done by moving the SUBREG to the source. */
7186 else if (GET_CODE (SET_DEST (x)) == SUBREG
7187 /* We need SUBREGs to compute nonzero_bits properly. */
7188 && nonzero_sign_valid
7189 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7190 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7191 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7192 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7194 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7195 gen_lowpart
7196 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7197 SET_SRC (x)));
7198 continue;
7200 else
7201 break;
7203 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7204 inner = SUBREG_REG (inner);
7206 compute_mode = GET_MODE (inner);
7208 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7209 if (! SCALAR_INT_MODE_P (compute_mode))
7211 machine_mode imode;
7213 /* Don't do anything for vector or complex integral types. */
7214 if (! FLOAT_MODE_P (compute_mode))
7215 break;
7217 /* Try to find an integral mode to pun with. */
7218 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7219 if (imode == BLKmode)
7220 break;
7222 compute_mode = imode;
7223 inner = gen_lowpart (imode, inner);
7226 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7227 if (len >= HOST_BITS_PER_WIDE_INT)
7228 break;
7230 /* Now compute the equivalent expression. Make a copy of INNER
7231 for the SET_DEST in case it is a MEM into which we will substitute;
7232 we don't want shared RTL in that case. */
7233 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7234 compute_mode);
7235 cleared = simplify_gen_binary (AND, compute_mode,
7236 simplify_gen_unary (NOT, compute_mode,
7237 simplify_gen_binary (ASHIFT,
7238 compute_mode,
7239 mask, pos),
7240 compute_mode),
7241 inner);
7242 masked = simplify_gen_binary (ASHIFT, compute_mode,
7243 simplify_gen_binary (
7244 AND, compute_mode,
7245 gen_lowpart (compute_mode, SET_SRC (x)),
7246 mask),
7247 pos);
7249 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7250 simplify_gen_binary (IOR, compute_mode,
7251 cleared, masked));
7254 return x;
7257 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7258 it is an RTX that represents the (variable) starting position; otherwise,
7259 POS is the (constant) starting bit position. Both are counted from the LSB.
7261 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7263 IN_DEST is nonzero if this is a reference in the destination of a SET.
7264 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7265 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7266 be used.
7268 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7269 ZERO_EXTRACT should be built even for bits starting at bit 0.
7271 MODE is the desired mode of the result (if IN_DEST == 0).
7273 The result is an RTX for the extraction or NULL_RTX if the target
7274 can't handle it. */
7276 static rtx
7277 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7278 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7279 int in_dest, int in_compare)
7281 /* This mode describes the size of the storage area
7282 to fetch the overall value from. Within that, we
7283 ignore the POS lowest bits, etc. */
7284 machine_mode is_mode = GET_MODE (inner);
7285 machine_mode inner_mode;
7286 machine_mode wanted_inner_mode;
7287 machine_mode wanted_inner_reg_mode = word_mode;
7288 machine_mode pos_mode = word_mode;
7289 machine_mode extraction_mode = word_mode;
7290 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7291 rtx new_rtx = 0;
7292 rtx orig_pos_rtx = pos_rtx;
7293 HOST_WIDE_INT orig_pos;
7295 if (pos_rtx && CONST_INT_P (pos_rtx))
7296 pos = INTVAL (pos_rtx), pos_rtx = 0;
7298 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7300 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7301 consider just the QI as the memory to extract from.
7302 The subreg adds or removes high bits; its mode is
7303 irrelevant to the meaning of this extraction,
7304 since POS and LEN count from the lsb. */
7305 if (MEM_P (SUBREG_REG (inner)))
7306 is_mode = GET_MODE (SUBREG_REG (inner));
7307 inner = SUBREG_REG (inner);
7309 else if (GET_CODE (inner) == ASHIFT
7310 && CONST_INT_P (XEXP (inner, 1))
7311 && pos_rtx == 0 && pos == 0
7312 && len > UINTVAL (XEXP (inner, 1)))
7314 /* We're extracting the least significant bits of an rtx
7315 (ashift X (const_int C)), where LEN > C. Extract the
7316 least significant (LEN - C) bits of X, giving an rtx
7317 whose mode is MODE, then shift it left C times. */
7318 new_rtx = make_extraction (mode, XEXP (inner, 0),
7319 0, 0, len - INTVAL (XEXP (inner, 1)),
7320 unsignedp, in_dest, in_compare);
7321 if (new_rtx != 0)
7322 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7324 else if (GET_CODE (inner) == TRUNCATE)
7325 inner = XEXP (inner, 0);
7327 inner_mode = GET_MODE (inner);
7329 /* See if this can be done without an extraction. We never can if the
7330 width of the field is not the same as that of some integer mode. For
7331 registers, we can only avoid the extraction if the position is at the
7332 low-order bit and this is either not in the destination or we have the
7333 appropriate STRICT_LOW_PART operation available.
7335 For MEM, we can avoid an extract if the field starts on an appropriate
7336 boundary and we can change the mode of the memory reference. */
7338 if (tmode != BLKmode
7339 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7340 && !MEM_P (inner)
7341 && (inner_mode == tmode
7342 || !REG_P (inner)
7343 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7344 || reg_truncated_to_mode (tmode, inner))
7345 && (! in_dest
7346 || (REG_P (inner)
7347 && have_insn_for (STRICT_LOW_PART, tmode))))
7348 || (MEM_P (inner) && pos_rtx == 0
7349 && (pos
7350 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7351 : BITS_PER_UNIT)) == 0
7352 /* We can't do this if we are widening INNER_MODE (it
7353 may not be aligned, for one thing). */
7354 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7355 && (inner_mode == tmode
7356 || (! mode_dependent_address_p (XEXP (inner, 0),
7357 MEM_ADDR_SPACE (inner))
7358 && ! MEM_VOLATILE_P (inner))))))
7360 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7361 field. If the original and current mode are the same, we need not
7362 adjust the offset. Otherwise, we do if bytes big endian.
7364 If INNER is not a MEM, get a piece consisting of just the field
7365 of interest (in this case POS % BITS_PER_WORD must be 0). */
7367 if (MEM_P (inner))
7369 HOST_WIDE_INT offset;
7371 /* POS counts from lsb, but make OFFSET count in memory order. */
7372 if (BYTES_BIG_ENDIAN)
7373 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7374 else
7375 offset = pos / BITS_PER_UNIT;
7377 new_rtx = adjust_address_nv (inner, tmode, offset);
7379 else if (REG_P (inner))
7381 if (tmode != inner_mode)
7383 /* We can't call gen_lowpart in a DEST since we
7384 always want a SUBREG (see below) and it would sometimes
7385 return a new hard register. */
7386 if (pos || in_dest)
7388 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7390 if (WORDS_BIG_ENDIAN
7391 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7392 final_word = ((GET_MODE_SIZE (inner_mode)
7393 - GET_MODE_SIZE (tmode))
7394 / UNITS_PER_WORD) - final_word;
7396 final_word *= UNITS_PER_WORD;
7397 if (BYTES_BIG_ENDIAN &&
7398 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7399 final_word += (GET_MODE_SIZE (inner_mode)
7400 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7402 /* Avoid creating invalid subregs, for example when
7403 simplifying (x>>32)&255. */
7404 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7405 return NULL_RTX;
7407 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7409 else
7410 new_rtx = gen_lowpart (tmode, inner);
7412 else
7413 new_rtx = inner;
7415 else
7416 new_rtx = force_to_mode (inner, tmode,
7417 len >= HOST_BITS_PER_WIDE_INT
7418 ? ~(unsigned HOST_WIDE_INT) 0
7419 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7422 /* If this extraction is going into the destination of a SET,
7423 make a STRICT_LOW_PART unless we made a MEM. */
7425 if (in_dest)
7426 return (MEM_P (new_rtx) ? new_rtx
7427 : (GET_CODE (new_rtx) != SUBREG
7428 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7429 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7431 if (mode == tmode)
7432 return new_rtx;
7434 if (CONST_SCALAR_INT_P (new_rtx))
7435 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7436 mode, new_rtx, tmode);
7438 /* If we know that no extraneous bits are set, and that the high
7439 bit is not set, convert the extraction to the cheaper of
7440 sign and zero extension, that are equivalent in these cases. */
7441 if (flag_expensive_optimizations
7442 && (HWI_COMPUTABLE_MODE_P (tmode)
7443 && ((nonzero_bits (new_rtx, tmode)
7444 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7445 == 0)))
7447 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7448 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7450 /* Prefer ZERO_EXTENSION, since it gives more information to
7451 backends. */
7452 if (set_src_cost (temp, optimize_this_for_speed_p)
7453 <= set_src_cost (temp1, optimize_this_for_speed_p))
7454 return temp;
7455 return temp1;
7458 /* Otherwise, sign- or zero-extend unless we already are in the
7459 proper mode. */
7461 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7462 mode, new_rtx));
7465 /* Unless this is a COMPARE or we have a funny memory reference,
7466 don't do anything with zero-extending field extracts starting at
7467 the low-order bit since they are simple AND operations. */
7468 if (pos_rtx == 0 && pos == 0 && ! in_dest
7469 && ! in_compare && unsignedp)
7470 return 0;
7472 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7473 if the position is not a constant and the length is not 1. In all
7474 other cases, we would only be going outside our object in cases when
7475 an original shift would have been undefined. */
7476 if (MEM_P (inner)
7477 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7478 || (pos_rtx != 0 && len != 1)))
7479 return 0;
7481 enum extraction_pattern pattern = (in_dest ? EP_insv
7482 : unsignedp ? EP_extzv : EP_extv);
7484 /* If INNER is not from memory, we want it to have the mode of a register
7485 extraction pattern's structure operand, or word_mode if there is no
7486 such pattern. The same applies to extraction_mode and pos_mode
7487 and their respective operands.
7489 For memory, assume that the desired extraction_mode and pos_mode
7490 are the same as for a register operation, since at present we don't
7491 have named patterns for aligned memory structures. */
7492 struct extraction_insn insn;
7493 if (get_best_reg_extraction_insn (&insn, pattern,
7494 GET_MODE_BITSIZE (inner_mode), mode))
7496 wanted_inner_reg_mode = insn.struct_mode;
7497 pos_mode = insn.pos_mode;
7498 extraction_mode = insn.field_mode;
7501 /* Never narrow an object, since that might not be safe. */
7503 if (mode != VOIDmode
7504 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7505 extraction_mode = mode;
7507 if (!MEM_P (inner))
7508 wanted_inner_mode = wanted_inner_reg_mode;
7509 else
7511 /* Be careful not to go beyond the extracted object and maintain the
7512 natural alignment of the memory. */
7513 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7514 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7515 > GET_MODE_BITSIZE (wanted_inner_mode))
7517 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7518 gcc_assert (wanted_inner_mode != VOIDmode);
7522 orig_pos = pos;
7524 if (BITS_BIG_ENDIAN)
7526 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7527 BITS_BIG_ENDIAN style. If position is constant, compute new
7528 position. Otherwise, build subtraction.
7529 Note that POS is relative to the mode of the original argument.
7530 If it's a MEM we need to recompute POS relative to that.
7531 However, if we're extracting from (or inserting into) a register,
7532 we want to recompute POS relative to wanted_inner_mode. */
7533 int width = (MEM_P (inner)
7534 ? GET_MODE_BITSIZE (is_mode)
7535 : GET_MODE_BITSIZE (wanted_inner_mode));
7537 if (pos_rtx == 0)
7538 pos = width - len - pos;
7539 else
7540 pos_rtx
7541 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7542 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7543 pos_rtx);
7544 /* POS may be less than 0 now, but we check for that below.
7545 Note that it can only be less than 0 if !MEM_P (inner). */
7548 /* If INNER has a wider mode, and this is a constant extraction, try to
7549 make it smaller and adjust the byte to point to the byte containing
7550 the value. */
7551 if (wanted_inner_mode != VOIDmode
7552 && inner_mode != wanted_inner_mode
7553 && ! pos_rtx
7554 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7555 && MEM_P (inner)
7556 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7557 && ! MEM_VOLATILE_P (inner))
7559 int offset = 0;
7561 /* The computations below will be correct if the machine is big
7562 endian in both bits and bytes or little endian in bits and bytes.
7563 If it is mixed, we must adjust. */
7565 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7566 adjust OFFSET to compensate. */
7567 if (BYTES_BIG_ENDIAN
7568 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7569 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7571 /* We can now move to the desired byte. */
7572 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7573 * GET_MODE_SIZE (wanted_inner_mode);
7574 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7576 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7577 && is_mode != wanted_inner_mode)
7578 offset = (GET_MODE_SIZE (is_mode)
7579 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7581 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7584 /* If INNER is not memory, get it into the proper mode. If we are changing
7585 its mode, POS must be a constant and smaller than the size of the new
7586 mode. */
7587 else if (!MEM_P (inner))
7589 /* On the LHS, don't create paradoxical subregs implicitely truncating
7590 the register unless TRULY_NOOP_TRUNCATION. */
7591 if (in_dest
7592 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7593 wanted_inner_mode))
7594 return NULL_RTX;
7596 if (GET_MODE (inner) != wanted_inner_mode
7597 && (pos_rtx != 0
7598 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7599 return NULL_RTX;
7601 if (orig_pos < 0)
7602 return NULL_RTX;
7604 inner = force_to_mode (inner, wanted_inner_mode,
7605 pos_rtx
7606 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7607 ? ~(unsigned HOST_WIDE_INT) 0
7608 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7609 << orig_pos),
7613 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7614 have to zero extend. Otherwise, we can just use a SUBREG. */
7615 if (pos_rtx != 0
7616 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7618 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7619 GET_MODE (pos_rtx));
7621 /* If we know that no extraneous bits are set, and that the high
7622 bit is not set, convert extraction to cheaper one - either
7623 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7624 cases. */
7625 if (flag_expensive_optimizations
7626 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7627 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7628 & ~(((unsigned HOST_WIDE_INT)
7629 GET_MODE_MASK (GET_MODE (pos_rtx)))
7630 >> 1))
7631 == 0)))
7633 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7634 GET_MODE (pos_rtx));
7636 /* Prefer ZERO_EXTENSION, since it gives more information to
7637 backends. */
7638 if (set_src_cost (temp1, optimize_this_for_speed_p)
7639 < set_src_cost (temp, optimize_this_for_speed_p))
7640 temp = temp1;
7642 pos_rtx = temp;
7645 /* Make POS_RTX unless we already have it and it is correct. If we don't
7646 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7647 be a CONST_INT. */
7648 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7649 pos_rtx = orig_pos_rtx;
7651 else if (pos_rtx == 0)
7652 pos_rtx = GEN_INT (pos);
7654 /* Make the required operation. See if we can use existing rtx. */
7655 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7656 extraction_mode, inner, GEN_INT (len), pos_rtx);
7657 if (! in_dest)
7658 new_rtx = gen_lowpart (mode, new_rtx);
7660 return new_rtx;
7663 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7664 with any other operations in X. Return X without that shift if so. */
7666 static rtx
7667 extract_left_shift (rtx x, int count)
7669 enum rtx_code code = GET_CODE (x);
7670 machine_mode mode = GET_MODE (x);
7671 rtx tem;
7673 switch (code)
7675 case ASHIFT:
7676 /* This is the shift itself. If it is wide enough, we will return
7677 either the value being shifted if the shift count is equal to
7678 COUNT or a shift for the difference. */
7679 if (CONST_INT_P (XEXP (x, 1))
7680 && INTVAL (XEXP (x, 1)) >= count)
7681 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7682 INTVAL (XEXP (x, 1)) - count);
7683 break;
7685 case NEG: case NOT:
7686 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7687 return simplify_gen_unary (code, mode, tem, mode);
7689 break;
7691 case PLUS: case IOR: case XOR: case AND:
7692 /* If we can safely shift this constant and we find the inner shift,
7693 make a new operation. */
7694 if (CONST_INT_P (XEXP (x, 1))
7695 && (UINTVAL (XEXP (x, 1))
7696 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7697 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7699 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7700 return simplify_gen_binary (code, mode, tem,
7701 gen_int_mode (val, mode));
7703 break;
7705 default:
7706 break;
7709 return 0;
7712 /* Look at the expression rooted at X. Look for expressions
7713 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7714 Form these expressions.
7716 Return the new rtx, usually just X.
7718 Also, for machines like the VAX that don't have logical shift insns,
7719 try to convert logical to arithmetic shift operations in cases where
7720 they are equivalent. This undoes the canonicalizations to logical
7721 shifts done elsewhere.
7723 We try, as much as possible, to re-use rtl expressions to save memory.
7725 IN_CODE says what kind of expression we are processing. Normally, it is
7726 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7727 being kludges), it is MEM. When processing the arguments of a comparison
7728 or a COMPARE against zero, it is COMPARE. */
7731 make_compound_operation (rtx x, enum rtx_code in_code)
7733 enum rtx_code code = GET_CODE (x);
7734 machine_mode mode = GET_MODE (x);
7735 int mode_width = GET_MODE_PRECISION (mode);
7736 rtx rhs, lhs;
7737 enum rtx_code next_code;
7738 int i, j;
7739 rtx new_rtx = 0;
7740 rtx tem;
7741 const char *fmt;
7743 /* Select the code to be used in recursive calls. Once we are inside an
7744 address, we stay there. If we have a comparison, set to COMPARE,
7745 but once inside, go back to our default of SET. */
7747 next_code = (code == MEM ? MEM
7748 : ((code == PLUS || code == MINUS)
7749 && SCALAR_INT_MODE_P (mode)) ? MEM
7750 : ((code == COMPARE || COMPARISON_P (x))
7751 && XEXP (x, 1) == const0_rtx) ? COMPARE
7752 : in_code == COMPARE ? SET : in_code);
7754 /* Process depending on the code of this operation. If NEW is set
7755 nonzero, it will be returned. */
7757 switch (code)
7759 case ASHIFT:
7760 /* Convert shifts by constants into multiplications if inside
7761 an address. */
7762 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7763 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7764 && INTVAL (XEXP (x, 1)) >= 0
7765 && SCALAR_INT_MODE_P (mode))
7767 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7768 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7770 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7771 if (GET_CODE (new_rtx) == NEG)
7773 new_rtx = XEXP (new_rtx, 0);
7774 multval = -multval;
7776 multval = trunc_int_for_mode (multval, mode);
7777 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7779 break;
7781 case PLUS:
7782 lhs = XEXP (x, 0);
7783 rhs = XEXP (x, 1);
7784 lhs = make_compound_operation (lhs, next_code);
7785 rhs = make_compound_operation (rhs, next_code);
7786 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7787 && SCALAR_INT_MODE_P (mode))
7789 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7790 XEXP (lhs, 1));
7791 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7793 else if (GET_CODE (lhs) == MULT
7794 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7796 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7797 simplify_gen_unary (NEG, mode,
7798 XEXP (lhs, 1),
7799 mode));
7800 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7802 else
7804 SUBST (XEXP (x, 0), lhs);
7805 SUBST (XEXP (x, 1), rhs);
7806 goto maybe_swap;
7808 x = gen_lowpart (mode, new_rtx);
7809 goto maybe_swap;
7811 case MINUS:
7812 lhs = XEXP (x, 0);
7813 rhs = XEXP (x, 1);
7814 lhs = make_compound_operation (lhs, next_code);
7815 rhs = make_compound_operation (rhs, next_code);
7816 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7817 && SCALAR_INT_MODE_P (mode))
7819 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7820 XEXP (rhs, 1));
7821 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7823 else if (GET_CODE (rhs) == MULT
7824 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7826 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7827 simplify_gen_unary (NEG, mode,
7828 XEXP (rhs, 1),
7829 mode));
7830 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7832 else
7834 SUBST (XEXP (x, 0), lhs);
7835 SUBST (XEXP (x, 1), rhs);
7836 return x;
7838 return gen_lowpart (mode, new_rtx);
7840 case AND:
7841 /* If the second operand is not a constant, we can't do anything
7842 with it. */
7843 if (!CONST_INT_P (XEXP (x, 1)))
7844 break;
7846 /* If the constant is a power of two minus one and the first operand
7847 is a logical right shift, make an extraction. */
7848 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7849 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7851 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7852 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7853 0, in_code == COMPARE);
7856 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7857 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7858 && subreg_lowpart_p (XEXP (x, 0))
7859 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7860 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7862 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7863 next_code);
7864 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7865 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7866 0, in_code == COMPARE);
7868 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7869 else if ((GET_CODE (XEXP (x, 0)) == XOR
7870 || GET_CODE (XEXP (x, 0)) == IOR)
7871 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7872 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7873 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7875 /* Apply the distributive law, and then try to make extractions. */
7876 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7877 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7878 XEXP (x, 1)),
7879 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7880 XEXP (x, 1)));
7881 new_rtx = make_compound_operation (new_rtx, in_code);
7884 /* If we are have (and (rotate X C) M) and C is larger than the number
7885 of bits in M, this is an extraction. */
7887 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7888 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7889 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7890 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7892 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7893 new_rtx = make_extraction (mode, new_rtx,
7894 (GET_MODE_PRECISION (mode)
7895 - INTVAL (XEXP (XEXP (x, 0), 1))),
7896 NULL_RTX, i, 1, 0, in_code == COMPARE);
7899 /* On machines without logical shifts, if the operand of the AND is
7900 a logical shift and our mask turns off all the propagated sign
7901 bits, we can replace the logical shift with an arithmetic shift. */
7902 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7903 && !have_insn_for (LSHIFTRT, mode)
7904 && have_insn_for (ASHIFTRT, mode)
7905 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7906 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7907 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7908 && mode_width <= HOST_BITS_PER_WIDE_INT)
7910 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7912 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7913 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7914 SUBST (XEXP (x, 0),
7915 gen_rtx_ASHIFTRT (mode,
7916 make_compound_operation
7917 (XEXP (XEXP (x, 0), 0), next_code),
7918 XEXP (XEXP (x, 0), 1)));
7921 /* If the constant is one less than a power of two, this might be
7922 representable by an extraction even if no shift is present.
7923 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7924 we are in a COMPARE. */
7925 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7926 new_rtx = make_extraction (mode,
7927 make_compound_operation (XEXP (x, 0),
7928 next_code),
7929 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7931 /* If we are in a comparison and this is an AND with a power of two,
7932 convert this into the appropriate bit extract. */
7933 else if (in_code == COMPARE
7934 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7935 new_rtx = make_extraction (mode,
7936 make_compound_operation (XEXP (x, 0),
7937 next_code),
7938 i, NULL_RTX, 1, 1, 0, 1);
7940 break;
7942 case LSHIFTRT:
7943 /* If the sign bit is known to be zero, replace this with an
7944 arithmetic shift. */
7945 if (have_insn_for (ASHIFTRT, mode)
7946 && ! have_insn_for (LSHIFTRT, mode)
7947 && mode_width <= HOST_BITS_PER_WIDE_INT
7948 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7950 new_rtx = gen_rtx_ASHIFTRT (mode,
7951 make_compound_operation (XEXP (x, 0),
7952 next_code),
7953 XEXP (x, 1));
7954 break;
7957 /* ... fall through ... */
7959 case ASHIFTRT:
7960 lhs = XEXP (x, 0);
7961 rhs = XEXP (x, 1);
7963 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7964 this is a SIGN_EXTRACT. */
7965 if (CONST_INT_P (rhs)
7966 && GET_CODE (lhs) == ASHIFT
7967 && CONST_INT_P (XEXP (lhs, 1))
7968 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7969 && INTVAL (XEXP (lhs, 1)) >= 0
7970 && INTVAL (rhs) < mode_width)
7972 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7973 new_rtx = make_extraction (mode, new_rtx,
7974 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7975 NULL_RTX, mode_width - INTVAL (rhs),
7976 code == LSHIFTRT, 0, in_code == COMPARE);
7977 break;
7980 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7981 If so, try to merge the shifts into a SIGN_EXTEND. We could
7982 also do this for some cases of SIGN_EXTRACT, but it doesn't
7983 seem worth the effort; the case checked for occurs on Alpha. */
7985 if (!OBJECT_P (lhs)
7986 && ! (GET_CODE (lhs) == SUBREG
7987 && (OBJECT_P (SUBREG_REG (lhs))))
7988 && CONST_INT_P (rhs)
7989 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7990 && INTVAL (rhs) < mode_width
7991 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7992 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7993 0, NULL_RTX, mode_width - INTVAL (rhs),
7994 code == LSHIFTRT, 0, in_code == COMPARE);
7996 break;
7998 case SUBREG:
7999 /* Call ourselves recursively on the inner expression. If we are
8000 narrowing the object and it has a different RTL code from
8001 what it originally did, do this SUBREG as a force_to_mode. */
8003 rtx inner = SUBREG_REG (x), simplified;
8004 enum rtx_code subreg_code = in_code;
8006 /* If in_code is COMPARE, it isn't always safe to pass it through
8007 to the recursive make_compound_operation call. */
8008 if (subreg_code == COMPARE
8009 && (!subreg_lowpart_p (x)
8010 || GET_CODE (inner) == SUBREG
8011 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8012 is (const_int 0), rather than
8013 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
8014 || (GET_CODE (inner) == AND
8015 && CONST_INT_P (XEXP (inner, 1))
8016 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8017 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8018 >= GET_MODE_BITSIZE (mode))))
8019 subreg_code = SET;
8021 tem = make_compound_operation (inner, subreg_code);
8023 simplified
8024 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8025 if (simplified)
8026 tem = simplified;
8028 if (GET_CODE (tem) != GET_CODE (inner)
8029 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8030 && subreg_lowpart_p (x))
8032 rtx newer
8033 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
8035 /* If we have something other than a SUBREG, we might have
8036 done an expansion, so rerun ourselves. */
8037 if (GET_CODE (newer) != SUBREG)
8038 newer = make_compound_operation (newer, in_code);
8040 /* force_to_mode can expand compounds. If it just re-expanded the
8041 compound, use gen_lowpart to convert to the desired mode. */
8042 if (rtx_equal_p (newer, x)
8043 /* Likewise if it re-expanded the compound only partially.
8044 This happens for SUBREG of ZERO_EXTRACT if they extract
8045 the same number of bits. */
8046 || (GET_CODE (newer) == SUBREG
8047 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8048 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8049 && GET_CODE (inner) == AND
8050 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8051 return gen_lowpart (GET_MODE (x), tem);
8053 return newer;
8056 if (simplified)
8057 return tem;
8059 break;
8061 default:
8062 break;
8065 if (new_rtx)
8067 x = gen_lowpart (mode, new_rtx);
8068 code = GET_CODE (x);
8071 /* Now recursively process each operand of this operation. We need to
8072 handle ZERO_EXTEND specially so that we don't lose track of the
8073 inner mode. */
8074 if (GET_CODE (x) == ZERO_EXTEND)
8076 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8077 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8078 new_rtx, GET_MODE (XEXP (x, 0)));
8079 if (tem)
8080 return tem;
8081 SUBST (XEXP (x, 0), new_rtx);
8082 return x;
8085 fmt = GET_RTX_FORMAT (code);
8086 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8087 if (fmt[i] == 'e')
8089 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8090 SUBST (XEXP (x, i), new_rtx);
8092 else if (fmt[i] == 'E')
8093 for (j = 0; j < XVECLEN (x, i); j++)
8095 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8096 SUBST (XVECEXP (x, i, j), new_rtx);
8099 maybe_swap:
8100 /* If this is a commutative operation, the changes to the operands
8101 may have made it noncanonical. */
8102 if (COMMUTATIVE_ARITH_P (x)
8103 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8105 tem = XEXP (x, 0);
8106 SUBST (XEXP (x, 0), XEXP (x, 1));
8107 SUBST (XEXP (x, 1), tem);
8110 return x;
8113 /* Given M see if it is a value that would select a field of bits
8114 within an item, but not the entire word. Return -1 if not.
8115 Otherwise, return the starting position of the field, where 0 is the
8116 low-order bit.
8118 *PLEN is set to the length of the field. */
8120 static int
8121 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8123 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8124 int pos = m ? ctz_hwi (m) : -1;
8125 int len = 0;
8127 if (pos >= 0)
8128 /* Now shift off the low-order zero bits and see if we have a
8129 power of two minus 1. */
8130 len = exact_log2 ((m >> pos) + 1);
8132 if (len <= 0)
8133 pos = -1;
8135 *plen = len;
8136 return pos;
8139 /* If X refers to a register that equals REG in value, replace these
8140 references with REG. */
8141 static rtx
8142 canon_reg_for_combine (rtx x, rtx reg)
8144 rtx op0, op1, op2;
8145 const char *fmt;
8146 int i;
8147 bool copied;
8149 enum rtx_code code = GET_CODE (x);
8150 switch (GET_RTX_CLASS (code))
8152 case RTX_UNARY:
8153 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8154 if (op0 != XEXP (x, 0))
8155 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8156 GET_MODE (reg));
8157 break;
8159 case RTX_BIN_ARITH:
8160 case RTX_COMM_ARITH:
8161 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8162 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8163 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8164 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8165 break;
8167 case RTX_COMPARE:
8168 case RTX_COMM_COMPARE:
8169 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8170 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8171 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8172 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8173 GET_MODE (op0), op0, op1);
8174 break;
8176 case RTX_TERNARY:
8177 case RTX_BITFIELD_OPS:
8178 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8179 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8180 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8181 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8182 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8183 GET_MODE (op0), op0, op1, op2);
8185 case RTX_OBJ:
8186 if (REG_P (x))
8188 if (rtx_equal_p (get_last_value (reg), x)
8189 || rtx_equal_p (reg, get_last_value (x)))
8190 return reg;
8191 else
8192 break;
8195 /* fall through */
8197 default:
8198 fmt = GET_RTX_FORMAT (code);
8199 copied = false;
8200 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8201 if (fmt[i] == 'e')
8203 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8204 if (op != XEXP (x, i))
8206 if (!copied)
8208 copied = true;
8209 x = copy_rtx (x);
8211 XEXP (x, i) = op;
8214 else if (fmt[i] == 'E')
8216 int j;
8217 for (j = 0; j < XVECLEN (x, i); j++)
8219 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8220 if (op != XVECEXP (x, i, j))
8222 if (!copied)
8224 copied = true;
8225 x = copy_rtx (x);
8227 XVECEXP (x, i, j) = op;
8232 break;
8235 return x;
8238 /* Return X converted to MODE. If the value is already truncated to
8239 MODE we can just return a subreg even though in the general case we
8240 would need an explicit truncation. */
8242 static rtx
8243 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8245 if (!CONST_INT_P (x)
8246 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8247 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8248 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8250 /* Bit-cast X into an integer mode. */
8251 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8252 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8253 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8254 x, GET_MODE (x));
8257 return gen_lowpart (mode, x);
8260 /* See if X can be simplified knowing that we will only refer to it in
8261 MODE and will only refer to those bits that are nonzero in MASK.
8262 If other bits are being computed or if masking operations are done
8263 that select a superset of the bits in MASK, they can sometimes be
8264 ignored.
8266 Return a possibly simplified expression, but always convert X to
8267 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8269 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8270 are all off in X. This is used when X will be complemented, by either
8271 NOT, NEG, or XOR. */
8273 static rtx
8274 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8275 int just_select)
8277 enum rtx_code code = GET_CODE (x);
8278 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8279 machine_mode op_mode;
8280 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8281 rtx op0, op1, temp;
8283 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8284 code below will do the wrong thing since the mode of such an
8285 expression is VOIDmode.
8287 Also do nothing if X is a CLOBBER; this can happen if X was
8288 the return value from a call to gen_lowpart. */
8289 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8290 return x;
8292 /* We want to perform the operation in its present mode unless we know
8293 that the operation is valid in MODE, in which case we do the operation
8294 in MODE. */
8295 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8296 && have_insn_for (code, mode))
8297 ? mode : GET_MODE (x));
8299 /* It is not valid to do a right-shift in a narrower mode
8300 than the one it came in with. */
8301 if ((code == LSHIFTRT || code == ASHIFTRT)
8302 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8303 op_mode = GET_MODE (x);
8305 /* Truncate MASK to fit OP_MODE. */
8306 if (op_mode)
8307 mask &= GET_MODE_MASK (op_mode);
8309 /* When we have an arithmetic operation, or a shift whose count we
8310 do not know, we need to assume that all bits up to the highest-order
8311 bit in MASK will be needed. This is how we form such a mask. */
8312 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8313 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8314 else
8315 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8316 - 1);
8318 /* Determine what bits of X are guaranteed to be (non)zero. */
8319 nonzero = nonzero_bits (x, mode);
8321 /* If none of the bits in X are needed, return a zero. */
8322 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8323 x = const0_rtx;
8325 /* If X is a CONST_INT, return a new one. Do this here since the
8326 test below will fail. */
8327 if (CONST_INT_P (x))
8329 if (SCALAR_INT_MODE_P (mode))
8330 return gen_int_mode (INTVAL (x) & mask, mode);
8331 else
8333 x = GEN_INT (INTVAL (x) & mask);
8334 return gen_lowpart_common (mode, x);
8338 /* If X is narrower than MODE and we want all the bits in X's mode, just
8339 get X in the proper mode. */
8340 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8341 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8342 return gen_lowpart (mode, x);
8344 /* We can ignore the effect of a SUBREG if it narrows the mode or
8345 if the constant masks to zero all the bits the mode doesn't have. */
8346 if (GET_CODE (x) == SUBREG
8347 && subreg_lowpart_p (x)
8348 && ((GET_MODE_SIZE (GET_MODE (x))
8349 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8350 || (0 == (mask
8351 & GET_MODE_MASK (GET_MODE (x))
8352 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8353 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8355 /* The arithmetic simplifications here only work for scalar integer modes. */
8356 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8357 return gen_lowpart_or_truncate (mode, x);
8359 switch (code)
8361 case CLOBBER:
8362 /* If X is a (clobber (const_int)), return it since we know we are
8363 generating something that won't match. */
8364 return x;
8366 case SIGN_EXTEND:
8367 case ZERO_EXTEND:
8368 case ZERO_EXTRACT:
8369 case SIGN_EXTRACT:
8370 x = expand_compound_operation (x);
8371 if (GET_CODE (x) != code)
8372 return force_to_mode (x, mode, mask, next_select);
8373 break;
8375 case TRUNCATE:
8376 /* Similarly for a truncate. */
8377 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8379 case AND:
8380 /* If this is an AND with a constant, convert it into an AND
8381 whose constant is the AND of that constant with MASK. If it
8382 remains an AND of MASK, delete it since it is redundant. */
8384 if (CONST_INT_P (XEXP (x, 1)))
8386 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8387 mask & INTVAL (XEXP (x, 1)));
8389 /* If X is still an AND, see if it is an AND with a mask that
8390 is just some low-order bits. If so, and it is MASK, we don't
8391 need it. */
8393 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8394 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8395 == mask))
8396 x = XEXP (x, 0);
8398 /* If it remains an AND, try making another AND with the bits
8399 in the mode mask that aren't in MASK turned on. If the
8400 constant in the AND is wide enough, this might make a
8401 cheaper constant. */
8403 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8404 && GET_MODE_MASK (GET_MODE (x)) != mask
8405 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8407 unsigned HOST_WIDE_INT cval
8408 = UINTVAL (XEXP (x, 1))
8409 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8410 rtx y;
8412 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8413 gen_int_mode (cval, GET_MODE (x)));
8414 if (set_src_cost (y, optimize_this_for_speed_p)
8415 < set_src_cost (x, optimize_this_for_speed_p))
8416 x = y;
8419 break;
8422 goto binop;
8424 case PLUS:
8425 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8426 low-order bits (as in an alignment operation) and FOO is already
8427 aligned to that boundary, mask C1 to that boundary as well.
8428 This may eliminate that PLUS and, later, the AND. */
8431 unsigned int width = GET_MODE_PRECISION (mode);
8432 unsigned HOST_WIDE_INT smask = mask;
8434 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8435 number, sign extend it. */
8437 if (width < HOST_BITS_PER_WIDE_INT
8438 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8439 smask |= HOST_WIDE_INT_M1U << width;
8441 if (CONST_INT_P (XEXP (x, 1))
8442 && exact_log2 (- smask) >= 0
8443 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8444 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8445 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8446 (INTVAL (XEXP (x, 1)) & smask)),
8447 mode, smask, next_select);
8450 /* ... fall through ... */
8452 case MULT:
8453 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8454 most significant bit in MASK since carries from those bits will
8455 affect the bits we are interested in. */
8456 mask = fuller_mask;
8457 goto binop;
8459 case MINUS:
8460 /* If X is (minus C Y) where C's least set bit is larger than any bit
8461 in the mask, then we may replace with (neg Y). */
8462 if (CONST_INT_P (XEXP (x, 0))
8463 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8465 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8466 GET_MODE (x));
8467 return force_to_mode (x, mode, mask, next_select);
8470 /* Similarly, if C contains every bit in the fuller_mask, then we may
8471 replace with (not Y). */
8472 if (CONST_INT_P (XEXP (x, 0))
8473 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8475 x = simplify_gen_unary (NOT, GET_MODE (x),
8476 XEXP (x, 1), GET_MODE (x));
8477 return force_to_mode (x, mode, mask, next_select);
8480 mask = fuller_mask;
8481 goto binop;
8483 case IOR:
8484 case XOR:
8485 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8486 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8487 operation which may be a bitfield extraction. Ensure that the
8488 constant we form is not wider than the mode of X. */
8490 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8491 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8492 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8493 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8494 && CONST_INT_P (XEXP (x, 1))
8495 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8496 + floor_log2 (INTVAL (XEXP (x, 1))))
8497 < GET_MODE_PRECISION (GET_MODE (x)))
8498 && (UINTVAL (XEXP (x, 1))
8499 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8501 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8502 << INTVAL (XEXP (XEXP (x, 0), 1)),
8503 GET_MODE (x));
8504 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8505 XEXP (XEXP (x, 0), 0), temp);
8506 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8507 XEXP (XEXP (x, 0), 1));
8508 return force_to_mode (x, mode, mask, next_select);
8511 binop:
8512 /* For most binary operations, just propagate into the operation and
8513 change the mode if we have an operation of that mode. */
8515 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8516 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8518 /* If we ended up truncating both operands, truncate the result of the
8519 operation instead. */
8520 if (GET_CODE (op0) == TRUNCATE
8521 && GET_CODE (op1) == TRUNCATE)
8523 op0 = XEXP (op0, 0);
8524 op1 = XEXP (op1, 0);
8527 op0 = gen_lowpart_or_truncate (op_mode, op0);
8528 op1 = gen_lowpart_or_truncate (op_mode, op1);
8530 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8531 x = simplify_gen_binary (code, op_mode, op0, op1);
8532 break;
8534 case ASHIFT:
8535 /* For left shifts, do the same, but just for the first operand.
8536 However, we cannot do anything with shifts where we cannot
8537 guarantee that the counts are smaller than the size of the mode
8538 because such a count will have a different meaning in a
8539 wider mode. */
8541 if (! (CONST_INT_P (XEXP (x, 1))
8542 && INTVAL (XEXP (x, 1)) >= 0
8543 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8544 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8545 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8546 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8547 break;
8549 /* If the shift count is a constant and we can do arithmetic in
8550 the mode of the shift, refine which bits we need. Otherwise, use the
8551 conservative form of the mask. */
8552 if (CONST_INT_P (XEXP (x, 1))
8553 && INTVAL (XEXP (x, 1)) >= 0
8554 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8555 && HWI_COMPUTABLE_MODE_P (op_mode))
8556 mask >>= INTVAL (XEXP (x, 1));
8557 else
8558 mask = fuller_mask;
8560 op0 = gen_lowpart_or_truncate (op_mode,
8561 force_to_mode (XEXP (x, 0), op_mode,
8562 mask, next_select));
8564 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8565 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8566 break;
8568 case LSHIFTRT:
8569 /* Here we can only do something if the shift count is a constant,
8570 this shift constant is valid for the host, and we can do arithmetic
8571 in OP_MODE. */
8573 if (CONST_INT_P (XEXP (x, 1))
8574 && INTVAL (XEXP (x, 1)) >= 0
8575 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8576 && HWI_COMPUTABLE_MODE_P (op_mode))
8578 rtx inner = XEXP (x, 0);
8579 unsigned HOST_WIDE_INT inner_mask;
8581 /* Select the mask of the bits we need for the shift operand. */
8582 inner_mask = mask << INTVAL (XEXP (x, 1));
8584 /* We can only change the mode of the shift if we can do arithmetic
8585 in the mode of the shift and INNER_MASK is no wider than the
8586 width of X's mode. */
8587 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8588 op_mode = GET_MODE (x);
8590 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8592 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8593 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8596 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8597 shift and AND produces only copies of the sign bit (C2 is one less
8598 than a power of two), we can do this with just a shift. */
8600 if (GET_CODE (x) == LSHIFTRT
8601 && CONST_INT_P (XEXP (x, 1))
8602 /* The shift puts one of the sign bit copies in the least significant
8603 bit. */
8604 && ((INTVAL (XEXP (x, 1))
8605 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8606 >= GET_MODE_PRECISION (GET_MODE (x)))
8607 && exact_log2 (mask + 1) >= 0
8608 /* Number of bits left after the shift must be more than the mask
8609 needs. */
8610 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8611 <= GET_MODE_PRECISION (GET_MODE (x)))
8612 /* Must be more sign bit copies than the mask needs. */
8613 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8614 >= exact_log2 (mask + 1)))
8615 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8616 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8617 - exact_log2 (mask + 1)));
8619 goto shiftrt;
8621 case ASHIFTRT:
8622 /* If we are just looking for the sign bit, we don't need this shift at
8623 all, even if it has a variable count. */
8624 if (val_signbit_p (GET_MODE (x), mask))
8625 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8627 /* If this is a shift by a constant, get a mask that contains those bits
8628 that are not copies of the sign bit. We then have two cases: If
8629 MASK only includes those bits, this can be a logical shift, which may
8630 allow simplifications. If MASK is a single-bit field not within
8631 those bits, we are requesting a copy of the sign bit and hence can
8632 shift the sign bit to the appropriate location. */
8634 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8635 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8637 int i;
8639 /* If the considered data is wider than HOST_WIDE_INT, we can't
8640 represent a mask for all its bits in a single scalar.
8641 But we only care about the lower bits, so calculate these. */
8643 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8645 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8647 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8648 is the number of bits a full-width mask would have set.
8649 We need only shift if these are fewer than nonzero can
8650 hold. If not, we must keep all bits set in nonzero. */
8652 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8653 < HOST_BITS_PER_WIDE_INT)
8654 nonzero >>= INTVAL (XEXP (x, 1))
8655 + HOST_BITS_PER_WIDE_INT
8656 - GET_MODE_PRECISION (GET_MODE (x)) ;
8658 else
8660 nonzero = GET_MODE_MASK (GET_MODE (x));
8661 nonzero >>= INTVAL (XEXP (x, 1));
8664 if ((mask & ~nonzero) == 0)
8666 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8667 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8668 if (GET_CODE (x) != ASHIFTRT)
8669 return force_to_mode (x, mode, mask, next_select);
8672 else if ((i = exact_log2 (mask)) >= 0)
8674 x = simplify_shift_const
8675 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8676 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8678 if (GET_CODE (x) != ASHIFTRT)
8679 return force_to_mode (x, mode, mask, next_select);
8683 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8684 even if the shift count isn't a constant. */
8685 if (mask == 1)
8686 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8687 XEXP (x, 0), XEXP (x, 1));
8689 shiftrt:
8691 /* If this is a zero- or sign-extension operation that just affects bits
8692 we don't care about, remove it. Be sure the call above returned
8693 something that is still a shift. */
8695 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8696 && CONST_INT_P (XEXP (x, 1))
8697 && INTVAL (XEXP (x, 1)) >= 0
8698 && (INTVAL (XEXP (x, 1))
8699 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8700 && GET_CODE (XEXP (x, 0)) == ASHIFT
8701 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8702 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8703 next_select);
8705 break;
8707 case ROTATE:
8708 case ROTATERT:
8709 /* If the shift count is constant and we can do computations
8710 in the mode of X, compute where the bits we care about are.
8711 Otherwise, we can't do anything. Don't change the mode of
8712 the shift or propagate MODE into the shift, though. */
8713 if (CONST_INT_P (XEXP (x, 1))
8714 && INTVAL (XEXP (x, 1)) >= 0)
8716 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8717 GET_MODE (x),
8718 gen_int_mode (mask, GET_MODE (x)),
8719 XEXP (x, 1));
8720 if (temp && CONST_INT_P (temp))
8721 x = simplify_gen_binary (code, GET_MODE (x),
8722 force_to_mode (XEXP (x, 0), GET_MODE (x),
8723 INTVAL (temp), next_select),
8724 XEXP (x, 1));
8726 break;
8728 case NEG:
8729 /* If we just want the low-order bit, the NEG isn't needed since it
8730 won't change the low-order bit. */
8731 if (mask == 1)
8732 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8734 /* We need any bits less significant than the most significant bit in
8735 MASK since carries from those bits will affect the bits we are
8736 interested in. */
8737 mask = fuller_mask;
8738 goto unop;
8740 case NOT:
8741 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8742 same as the XOR case above. Ensure that the constant we form is not
8743 wider than the mode of X. */
8745 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8746 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8747 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8748 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8749 < GET_MODE_PRECISION (GET_MODE (x)))
8750 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8752 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8753 GET_MODE (x));
8754 temp = simplify_gen_binary (XOR, GET_MODE (x),
8755 XEXP (XEXP (x, 0), 0), temp);
8756 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8757 temp, XEXP (XEXP (x, 0), 1));
8759 return force_to_mode (x, mode, mask, next_select);
8762 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8763 use the full mask inside the NOT. */
8764 mask = fuller_mask;
8766 unop:
8767 op0 = gen_lowpart_or_truncate (op_mode,
8768 force_to_mode (XEXP (x, 0), mode, mask,
8769 next_select));
8770 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8771 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8772 break;
8774 case NE:
8775 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8776 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8777 which is equal to STORE_FLAG_VALUE. */
8778 if ((mask & ~STORE_FLAG_VALUE) == 0
8779 && XEXP (x, 1) == const0_rtx
8780 && GET_MODE (XEXP (x, 0)) == mode
8781 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8782 && (nonzero_bits (XEXP (x, 0), mode)
8783 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8784 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8786 break;
8788 case IF_THEN_ELSE:
8789 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8790 written in a narrower mode. We play it safe and do not do so. */
8792 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8793 force_to_mode (XEXP (x, 1), mode,
8794 mask, next_select));
8795 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8796 force_to_mode (XEXP (x, 2), mode,
8797 mask, next_select));
8798 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8799 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8800 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8801 op0, op1);
8802 break;
8804 default:
8805 break;
8808 /* Ensure we return a value of the proper mode. */
8809 return gen_lowpart_or_truncate (mode, x);
8812 /* Return nonzero if X is an expression that has one of two values depending on
8813 whether some other value is zero or nonzero. In that case, we return the
8814 value that is being tested, *PTRUE is set to the value if the rtx being
8815 returned has a nonzero value, and *PFALSE is set to the other alternative.
8817 If we return zero, we set *PTRUE and *PFALSE to X. */
8819 static rtx
8820 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8822 machine_mode mode = GET_MODE (x);
8823 enum rtx_code code = GET_CODE (x);
8824 rtx cond0, cond1, true0, true1, false0, false1;
8825 unsigned HOST_WIDE_INT nz;
8827 /* If we are comparing a value against zero, we are done. */
8828 if ((code == NE || code == EQ)
8829 && XEXP (x, 1) == const0_rtx)
8831 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8832 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8833 return XEXP (x, 0);
8836 /* If this is a unary operation whose operand has one of two values, apply
8837 our opcode to compute those values. */
8838 else if (UNARY_P (x)
8839 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8841 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8842 *pfalse = simplify_gen_unary (code, mode, false0,
8843 GET_MODE (XEXP (x, 0)));
8844 return cond0;
8847 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8848 make can't possibly match and would suppress other optimizations. */
8849 else if (code == COMPARE)
8852 /* If this is a binary operation, see if either side has only one of two
8853 values. If either one does or if both do and they are conditional on
8854 the same value, compute the new true and false values. */
8855 else if (BINARY_P (x))
8857 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8858 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8860 if ((cond0 != 0 || cond1 != 0)
8861 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8863 /* If if_then_else_cond returned zero, then true/false are the
8864 same rtl. We must copy one of them to prevent invalid rtl
8865 sharing. */
8866 if (cond0 == 0)
8867 true0 = copy_rtx (true0);
8868 else if (cond1 == 0)
8869 true1 = copy_rtx (true1);
8871 if (COMPARISON_P (x))
8873 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8874 true0, true1);
8875 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8876 false0, false1);
8878 else
8880 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8881 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8884 return cond0 ? cond0 : cond1;
8887 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8888 operands is zero when the other is nonzero, and vice-versa,
8889 and STORE_FLAG_VALUE is 1 or -1. */
8891 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8892 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8893 || code == UMAX)
8894 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8896 rtx op0 = XEXP (XEXP (x, 0), 1);
8897 rtx op1 = XEXP (XEXP (x, 1), 1);
8899 cond0 = XEXP (XEXP (x, 0), 0);
8900 cond1 = XEXP (XEXP (x, 1), 0);
8902 if (COMPARISON_P (cond0)
8903 && COMPARISON_P (cond1)
8904 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8905 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8906 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8907 || ((swap_condition (GET_CODE (cond0))
8908 == reversed_comparison_code (cond1, NULL))
8909 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8910 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8911 && ! side_effects_p (x))
8913 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8914 *pfalse = simplify_gen_binary (MULT, mode,
8915 (code == MINUS
8916 ? simplify_gen_unary (NEG, mode,
8917 op1, mode)
8918 : op1),
8919 const_true_rtx);
8920 return cond0;
8924 /* Similarly for MULT, AND and UMIN, except that for these the result
8925 is always zero. */
8926 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8927 && (code == MULT || code == AND || code == UMIN)
8928 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8930 cond0 = XEXP (XEXP (x, 0), 0);
8931 cond1 = XEXP (XEXP (x, 1), 0);
8933 if (COMPARISON_P (cond0)
8934 && COMPARISON_P (cond1)
8935 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8936 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8937 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8938 || ((swap_condition (GET_CODE (cond0))
8939 == reversed_comparison_code (cond1, NULL))
8940 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8941 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8942 && ! side_effects_p (x))
8944 *ptrue = *pfalse = const0_rtx;
8945 return cond0;
8950 else if (code == IF_THEN_ELSE)
8952 /* If we have IF_THEN_ELSE already, extract the condition and
8953 canonicalize it if it is NE or EQ. */
8954 cond0 = XEXP (x, 0);
8955 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8956 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8957 return XEXP (cond0, 0);
8958 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8960 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8961 return XEXP (cond0, 0);
8963 else
8964 return cond0;
8967 /* If X is a SUBREG, we can narrow both the true and false values
8968 if the inner expression, if there is a condition. */
8969 else if (code == SUBREG
8970 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8971 &true0, &false0)))
8973 true0 = simplify_gen_subreg (mode, true0,
8974 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8975 false0 = simplify_gen_subreg (mode, false0,
8976 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8977 if (true0 && false0)
8979 *ptrue = true0;
8980 *pfalse = false0;
8981 return cond0;
8985 /* If X is a constant, this isn't special and will cause confusions
8986 if we treat it as such. Likewise if it is equivalent to a constant. */
8987 else if (CONSTANT_P (x)
8988 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8991 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8992 will be least confusing to the rest of the compiler. */
8993 else if (mode == BImode)
8995 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8996 return x;
8999 /* If X is known to be either 0 or -1, those are the true and
9000 false values when testing X. */
9001 else if (x == constm1_rtx || x == const0_rtx
9002 || (mode != VOIDmode
9003 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
9005 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9006 return x;
9009 /* Likewise for 0 or a single bit. */
9010 else if (HWI_COMPUTABLE_MODE_P (mode)
9011 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
9013 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9014 return x;
9017 /* Otherwise fail; show no condition with true and false values the same. */
9018 *ptrue = *pfalse = x;
9019 return 0;
9022 /* Return the value of expression X given the fact that condition COND
9023 is known to be true when applied to REG as its first operand and VAL
9024 as its second. X is known to not be shared and so can be modified in
9025 place.
9027 We only handle the simplest cases, and specifically those cases that
9028 arise with IF_THEN_ELSE expressions. */
9030 static rtx
9031 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9033 enum rtx_code code = GET_CODE (x);
9034 const char *fmt;
9035 int i, j;
9037 if (side_effects_p (x))
9038 return x;
9040 /* If either operand of the condition is a floating point value,
9041 then we have to avoid collapsing an EQ comparison. */
9042 if (cond == EQ
9043 && rtx_equal_p (x, reg)
9044 && ! FLOAT_MODE_P (GET_MODE (x))
9045 && ! FLOAT_MODE_P (GET_MODE (val)))
9046 return val;
9048 if (cond == UNEQ && rtx_equal_p (x, reg))
9049 return val;
9051 /* If X is (abs REG) and we know something about REG's relationship
9052 with zero, we may be able to simplify this. */
9054 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9055 switch (cond)
9057 case GE: case GT: case EQ:
9058 return XEXP (x, 0);
9059 case LT: case LE:
9060 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9061 XEXP (x, 0),
9062 GET_MODE (XEXP (x, 0)));
9063 default:
9064 break;
9067 /* The only other cases we handle are MIN, MAX, and comparisons if the
9068 operands are the same as REG and VAL. */
9070 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9072 if (rtx_equal_p (XEXP (x, 0), val))
9074 std::swap (val, reg);
9075 cond = swap_condition (cond);
9078 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9080 if (COMPARISON_P (x))
9082 if (comparison_dominates_p (cond, code))
9083 return const_true_rtx;
9085 code = reversed_comparison_code (x, NULL);
9086 if (code != UNKNOWN
9087 && comparison_dominates_p (cond, code))
9088 return const0_rtx;
9089 else
9090 return x;
9092 else if (code == SMAX || code == SMIN
9093 || code == UMIN || code == UMAX)
9095 int unsignedp = (code == UMIN || code == UMAX);
9097 /* Do not reverse the condition when it is NE or EQ.
9098 This is because we cannot conclude anything about
9099 the value of 'SMAX (x, y)' when x is not equal to y,
9100 but we can when x equals y. */
9101 if ((code == SMAX || code == UMAX)
9102 && ! (cond == EQ || cond == NE))
9103 cond = reverse_condition (cond);
9105 switch (cond)
9107 case GE: case GT:
9108 return unsignedp ? x : XEXP (x, 1);
9109 case LE: case LT:
9110 return unsignedp ? x : XEXP (x, 0);
9111 case GEU: case GTU:
9112 return unsignedp ? XEXP (x, 1) : x;
9113 case LEU: case LTU:
9114 return unsignedp ? XEXP (x, 0) : x;
9115 default:
9116 break;
9121 else if (code == SUBREG)
9123 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9124 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9126 if (SUBREG_REG (x) != r)
9128 /* We must simplify subreg here, before we lose track of the
9129 original inner_mode. */
9130 new_rtx = simplify_subreg (GET_MODE (x), r,
9131 inner_mode, SUBREG_BYTE (x));
9132 if (new_rtx)
9133 return new_rtx;
9134 else
9135 SUBST (SUBREG_REG (x), r);
9138 return x;
9140 /* We don't have to handle SIGN_EXTEND here, because even in the
9141 case of replacing something with a modeless CONST_INT, a
9142 CONST_INT is already (supposed to be) a valid sign extension for
9143 its narrower mode, which implies it's already properly
9144 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9145 story is different. */
9146 else if (code == ZERO_EXTEND)
9148 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9149 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9151 if (XEXP (x, 0) != r)
9153 /* We must simplify the zero_extend here, before we lose
9154 track of the original inner_mode. */
9155 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9156 r, inner_mode);
9157 if (new_rtx)
9158 return new_rtx;
9159 else
9160 SUBST (XEXP (x, 0), r);
9163 return x;
9166 fmt = GET_RTX_FORMAT (code);
9167 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9169 if (fmt[i] == 'e')
9170 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9171 else if (fmt[i] == 'E')
9172 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9173 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9174 cond, reg, val));
9177 return x;
9180 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9181 assignment as a field assignment. */
9183 static int
9184 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9186 if (widen_x && GET_MODE (x) != GET_MODE (y))
9188 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (y)))
9189 return 0;
9190 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9191 return 0;
9192 /* For big endian, adjust the memory offset. */
9193 if (BYTES_BIG_ENDIAN)
9194 x = adjust_address_nv (x, GET_MODE (y),
9195 -subreg_lowpart_offset (GET_MODE (x),
9196 GET_MODE (y)));
9197 else
9198 x = adjust_address_nv (x, GET_MODE (y), 0);
9201 if (x == y || rtx_equal_p (x, y))
9202 return 1;
9204 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9205 return 0;
9207 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9208 Note that all SUBREGs of MEM are paradoxical; otherwise they
9209 would have been rewritten. */
9210 if (MEM_P (x) && GET_CODE (y) == SUBREG
9211 && MEM_P (SUBREG_REG (y))
9212 && rtx_equal_p (SUBREG_REG (y),
9213 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9214 return 1;
9216 if (MEM_P (y) && GET_CODE (x) == SUBREG
9217 && MEM_P (SUBREG_REG (x))
9218 && rtx_equal_p (SUBREG_REG (x),
9219 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9220 return 1;
9222 /* We used to see if get_last_value of X and Y were the same but that's
9223 not correct. In one direction, we'll cause the assignment to have
9224 the wrong destination and in the case, we'll import a register into this
9225 insn that might have already have been dead. So fail if none of the
9226 above cases are true. */
9227 return 0;
9230 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9231 Return that assignment if so.
9233 We only handle the most common cases. */
9235 static rtx
9236 make_field_assignment (rtx x)
9238 rtx dest = SET_DEST (x);
9239 rtx src = SET_SRC (x);
9240 rtx assign;
9241 rtx rhs, lhs;
9242 HOST_WIDE_INT c1;
9243 HOST_WIDE_INT pos;
9244 unsigned HOST_WIDE_INT len;
9245 rtx other;
9246 machine_mode mode;
9248 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9249 a clear of a one-bit field. We will have changed it to
9250 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9251 for a SUBREG. */
9253 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9254 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9255 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9256 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9258 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9259 1, 1, 1, 0);
9260 if (assign != 0)
9261 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9262 return x;
9265 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9266 && subreg_lowpart_p (XEXP (src, 0))
9267 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9268 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9269 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9270 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9271 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9272 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9274 assign = make_extraction (VOIDmode, dest, 0,
9275 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9276 1, 1, 1, 0);
9277 if (assign != 0)
9278 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9279 return x;
9282 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9283 one-bit field. */
9284 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9285 && XEXP (XEXP (src, 0), 0) == const1_rtx
9286 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9288 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9289 1, 1, 1, 0);
9290 if (assign != 0)
9291 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9292 return x;
9295 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9296 SRC is an AND with all bits of that field set, then we can discard
9297 the AND. */
9298 if (GET_CODE (dest) == ZERO_EXTRACT
9299 && CONST_INT_P (XEXP (dest, 1))
9300 && GET_CODE (src) == AND
9301 && CONST_INT_P (XEXP (src, 1)))
9303 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9304 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9305 unsigned HOST_WIDE_INT ze_mask;
9307 if (width >= HOST_BITS_PER_WIDE_INT)
9308 ze_mask = -1;
9309 else
9310 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9312 /* Complete overlap. We can remove the source AND. */
9313 if ((and_mask & ze_mask) == ze_mask)
9314 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9316 /* Partial overlap. We can reduce the source AND. */
9317 if ((and_mask & ze_mask) != and_mask)
9319 mode = GET_MODE (src);
9320 src = gen_rtx_AND (mode, XEXP (src, 0),
9321 gen_int_mode (and_mask & ze_mask, mode));
9322 return gen_rtx_SET (VOIDmode, dest, src);
9326 /* The other case we handle is assignments into a constant-position
9327 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9328 a mask that has all one bits except for a group of zero bits and
9329 OTHER is known to have zeros where C1 has ones, this is such an
9330 assignment. Compute the position and length from C1. Shift OTHER
9331 to the appropriate position, force it to the required mode, and
9332 make the extraction. Check for the AND in both operands. */
9334 /* One or more SUBREGs might obscure the constant-position field
9335 assignment. The first one we are likely to encounter is an outer
9336 narrowing SUBREG, which we can just strip for the purposes of
9337 identifying the constant-field assignment. */
9338 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src))
9339 src = SUBREG_REG (src);
9341 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9342 return x;
9344 rhs = expand_compound_operation (XEXP (src, 0));
9345 lhs = expand_compound_operation (XEXP (src, 1));
9347 if (GET_CODE (rhs) == AND
9348 && CONST_INT_P (XEXP (rhs, 1))
9349 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9350 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9351 /* The second SUBREG that might get in the way is a paradoxical
9352 SUBREG around the first operand of the AND. We want to
9353 pretend the operand is as wide as the destination here. We
9354 do this by adjusting the MEM to wider mode for the sole
9355 purpose of the call to rtx_equal_for_field_assignment_p. Also
9356 note this trick only works for MEMs. */
9357 else if (GET_CODE (rhs) == AND
9358 && paradoxical_subreg_p (XEXP (rhs, 0))
9359 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9360 && CONST_INT_P (XEXP (rhs, 1))
9361 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9362 dest, true))
9363 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9364 else if (GET_CODE (lhs) == AND
9365 && CONST_INT_P (XEXP (lhs, 1))
9366 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9367 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9368 /* The second SUBREG that might get in the way is a paradoxical
9369 SUBREG around the first operand of the AND. We want to
9370 pretend the operand is as wide as the destination here. We
9371 do this by adjusting the MEM to wider mode for the sole
9372 purpose of the call to rtx_equal_for_field_assignment_p. Also
9373 note this trick only works for MEMs. */
9374 else if (GET_CODE (lhs) == AND
9375 && paradoxical_subreg_p (XEXP (lhs, 0))
9376 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9377 && CONST_INT_P (XEXP (lhs, 1))
9378 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9379 dest, true))
9380 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9381 else
9382 return x;
9384 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9385 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9386 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9387 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9388 return x;
9390 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9391 if (assign == 0)
9392 return x;
9394 /* The mode to use for the source is the mode of the assignment, or of
9395 what is inside a possible STRICT_LOW_PART. */
9396 mode = (GET_CODE (assign) == STRICT_LOW_PART
9397 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9399 /* Shift OTHER right POS places and make it the source, restricting it
9400 to the proper length and mode. */
9402 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9403 GET_MODE (src),
9404 other, pos),
9405 dest);
9406 src = force_to_mode (src, mode,
9407 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9408 ? ~(unsigned HOST_WIDE_INT) 0
9409 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9412 /* If SRC is masked by an AND that does not make a difference in
9413 the value being stored, strip it. */
9414 if (GET_CODE (assign) == ZERO_EXTRACT
9415 && CONST_INT_P (XEXP (assign, 1))
9416 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9417 && GET_CODE (src) == AND
9418 && CONST_INT_P (XEXP (src, 1))
9419 && UINTVAL (XEXP (src, 1))
9420 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9421 src = XEXP (src, 0);
9423 return gen_rtx_SET (VOIDmode, assign, src);
9426 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9427 if so. */
9429 static rtx
9430 apply_distributive_law (rtx x)
9432 enum rtx_code code = GET_CODE (x);
9433 enum rtx_code inner_code;
9434 rtx lhs, rhs, other;
9435 rtx tem;
9437 /* Distributivity is not true for floating point as it can change the
9438 value. So we don't do it unless -funsafe-math-optimizations. */
9439 if (FLOAT_MODE_P (GET_MODE (x))
9440 && ! flag_unsafe_math_optimizations)
9441 return x;
9443 /* The outer operation can only be one of the following: */
9444 if (code != IOR && code != AND && code != XOR
9445 && code != PLUS && code != MINUS)
9446 return x;
9448 lhs = XEXP (x, 0);
9449 rhs = XEXP (x, 1);
9451 /* If either operand is a primitive we can't do anything, so get out
9452 fast. */
9453 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9454 return x;
9456 lhs = expand_compound_operation (lhs);
9457 rhs = expand_compound_operation (rhs);
9458 inner_code = GET_CODE (lhs);
9459 if (inner_code != GET_CODE (rhs))
9460 return x;
9462 /* See if the inner and outer operations distribute. */
9463 switch (inner_code)
9465 case LSHIFTRT:
9466 case ASHIFTRT:
9467 case AND:
9468 case IOR:
9469 /* These all distribute except over PLUS. */
9470 if (code == PLUS || code == MINUS)
9471 return x;
9472 break;
9474 case MULT:
9475 if (code != PLUS && code != MINUS)
9476 return x;
9477 break;
9479 case ASHIFT:
9480 /* This is also a multiply, so it distributes over everything. */
9481 break;
9483 /* This used to handle SUBREG, but this turned out to be counter-
9484 productive, since (subreg (op ...)) usually is not handled by
9485 insn patterns, and this "optimization" therefore transformed
9486 recognizable patterns into unrecognizable ones. Therefore the
9487 SUBREG case was removed from here.
9489 It is possible that distributing SUBREG over arithmetic operations
9490 leads to an intermediate result than can then be optimized further,
9491 e.g. by moving the outer SUBREG to the other side of a SET as done
9492 in simplify_set. This seems to have been the original intent of
9493 handling SUBREGs here.
9495 However, with current GCC this does not appear to actually happen,
9496 at least on major platforms. If some case is found where removing
9497 the SUBREG case here prevents follow-on optimizations, distributing
9498 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9500 default:
9501 return x;
9504 /* Set LHS and RHS to the inner operands (A and B in the example
9505 above) and set OTHER to the common operand (C in the example).
9506 There is only one way to do this unless the inner operation is
9507 commutative. */
9508 if (COMMUTATIVE_ARITH_P (lhs)
9509 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9510 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9511 else if (COMMUTATIVE_ARITH_P (lhs)
9512 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9513 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9514 else if (COMMUTATIVE_ARITH_P (lhs)
9515 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9516 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9517 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9518 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9519 else
9520 return x;
9522 /* Form the new inner operation, seeing if it simplifies first. */
9523 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9525 /* There is one exception to the general way of distributing:
9526 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9527 if (code == XOR && inner_code == IOR)
9529 inner_code = AND;
9530 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9533 /* We may be able to continuing distributing the result, so call
9534 ourselves recursively on the inner operation before forming the
9535 outer operation, which we return. */
9536 return simplify_gen_binary (inner_code, GET_MODE (x),
9537 apply_distributive_law (tem), other);
9540 /* See if X is of the form (* (+ A B) C), and if so convert to
9541 (+ (* A C) (* B C)) and try to simplify.
9543 Most of the time, this results in no change. However, if some of
9544 the operands are the same or inverses of each other, simplifications
9545 will result.
9547 For example, (and (ior A B) (not B)) can occur as the result of
9548 expanding a bit field assignment. When we apply the distributive
9549 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9550 which then simplifies to (and (A (not B))).
9552 Note that no checks happen on the validity of applying the inverse
9553 distributive law. This is pointless since we can do it in the
9554 few places where this routine is called.
9556 N is the index of the term that is decomposed (the arithmetic operation,
9557 i.e. (+ A B) in the first example above). !N is the index of the term that
9558 is distributed, i.e. of C in the first example above. */
9559 static rtx
9560 distribute_and_simplify_rtx (rtx x, int n)
9562 machine_mode mode;
9563 enum rtx_code outer_code, inner_code;
9564 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9566 /* Distributivity is not true for floating point as it can change the
9567 value. So we don't do it unless -funsafe-math-optimizations. */
9568 if (FLOAT_MODE_P (GET_MODE (x))
9569 && ! flag_unsafe_math_optimizations)
9570 return NULL_RTX;
9572 decomposed = XEXP (x, n);
9573 if (!ARITHMETIC_P (decomposed))
9574 return NULL_RTX;
9576 mode = GET_MODE (x);
9577 outer_code = GET_CODE (x);
9578 distributed = XEXP (x, !n);
9580 inner_code = GET_CODE (decomposed);
9581 inner_op0 = XEXP (decomposed, 0);
9582 inner_op1 = XEXP (decomposed, 1);
9584 /* Special case (and (xor B C) (not A)), which is equivalent to
9585 (xor (ior A B) (ior A C)) */
9586 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9588 distributed = XEXP (distributed, 0);
9589 outer_code = IOR;
9592 if (n == 0)
9594 /* Distribute the second term. */
9595 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9596 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9598 else
9600 /* Distribute the first term. */
9601 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9602 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9605 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9606 new_op0, new_op1));
9607 if (GET_CODE (tmp) != outer_code
9608 && (set_src_cost (tmp, optimize_this_for_speed_p)
9609 < set_src_cost (x, optimize_this_for_speed_p)))
9610 return tmp;
9612 return NULL_RTX;
9615 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9616 in MODE. Return an equivalent form, if different from (and VAROP
9617 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9619 static rtx
9620 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9621 unsigned HOST_WIDE_INT constop)
9623 unsigned HOST_WIDE_INT nonzero;
9624 unsigned HOST_WIDE_INT orig_constop;
9625 rtx orig_varop;
9626 int i;
9628 orig_varop = varop;
9629 orig_constop = constop;
9630 if (GET_CODE (varop) == CLOBBER)
9631 return NULL_RTX;
9633 /* Simplify VAROP knowing that we will be only looking at some of the
9634 bits in it.
9636 Note by passing in CONSTOP, we guarantee that the bits not set in
9637 CONSTOP are not significant and will never be examined. We must
9638 ensure that is the case by explicitly masking out those bits
9639 before returning. */
9640 varop = force_to_mode (varop, mode, constop, 0);
9642 /* If VAROP is a CLOBBER, we will fail so return it. */
9643 if (GET_CODE (varop) == CLOBBER)
9644 return varop;
9646 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9647 to VAROP and return the new constant. */
9648 if (CONST_INT_P (varop))
9649 return gen_int_mode (INTVAL (varop) & constop, mode);
9651 /* See what bits may be nonzero in VAROP. Unlike the general case of
9652 a call to nonzero_bits, here we don't care about bits outside
9653 MODE. */
9655 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9657 /* Turn off all bits in the constant that are known to already be zero.
9658 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9659 which is tested below. */
9661 constop &= nonzero;
9663 /* If we don't have any bits left, return zero. */
9664 if (constop == 0)
9665 return const0_rtx;
9667 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9668 a power of two, we can replace this with an ASHIFT. */
9669 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9670 && (i = exact_log2 (constop)) >= 0)
9671 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9673 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9674 or XOR, then try to apply the distributive law. This may eliminate
9675 operations if either branch can be simplified because of the AND.
9676 It may also make some cases more complex, but those cases probably
9677 won't match a pattern either with or without this. */
9679 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9680 return
9681 gen_lowpart
9682 (mode,
9683 apply_distributive_law
9684 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9685 simplify_and_const_int (NULL_RTX,
9686 GET_MODE (varop),
9687 XEXP (varop, 0),
9688 constop),
9689 simplify_and_const_int (NULL_RTX,
9690 GET_MODE (varop),
9691 XEXP (varop, 1),
9692 constop))));
9694 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9695 the AND and see if one of the operands simplifies to zero. If so, we
9696 may eliminate it. */
9698 if (GET_CODE (varop) == PLUS
9699 && exact_log2 (constop + 1) >= 0)
9701 rtx o0, o1;
9703 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9704 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9705 if (o0 == const0_rtx)
9706 return o1;
9707 if (o1 == const0_rtx)
9708 return o0;
9711 /* Make a SUBREG if necessary. If we can't make it, fail. */
9712 varop = gen_lowpart (mode, varop);
9713 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9714 return NULL_RTX;
9716 /* If we are only masking insignificant bits, return VAROP. */
9717 if (constop == nonzero)
9718 return varop;
9720 if (varop == orig_varop && constop == orig_constop)
9721 return NULL_RTX;
9723 /* Otherwise, return an AND. */
9724 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9728 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9729 in MODE.
9731 Return an equivalent form, if different from X. Otherwise, return X. If
9732 X is zero, we are to always construct the equivalent form. */
9734 static rtx
9735 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9736 unsigned HOST_WIDE_INT constop)
9738 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9739 if (tem)
9740 return tem;
9742 if (!x)
9743 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9744 gen_int_mode (constop, mode));
9745 if (GET_MODE (x) != mode)
9746 x = gen_lowpart (mode, x);
9747 return x;
9750 /* Given a REG, X, compute which bits in X can be nonzero.
9751 We don't care about bits outside of those defined in MODE.
9753 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9754 a shift, AND, or zero_extract, we can do better. */
9756 static rtx
9757 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9758 const_rtx known_x ATTRIBUTE_UNUSED,
9759 machine_mode known_mode ATTRIBUTE_UNUSED,
9760 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9761 unsigned HOST_WIDE_INT *nonzero)
9763 rtx tem;
9764 reg_stat_type *rsp;
9766 /* If X is a register whose nonzero bits value is current, use it.
9767 Otherwise, if X is a register whose value we can find, use that
9768 value. Otherwise, use the previously-computed global nonzero bits
9769 for this register. */
9771 rsp = &reg_stat[REGNO (x)];
9772 if (rsp->last_set_value != 0
9773 && (rsp->last_set_mode == mode
9774 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9775 && GET_MODE_CLASS (mode) == MODE_INT))
9776 && ((rsp->last_set_label >= label_tick_ebb_start
9777 && rsp->last_set_label < label_tick)
9778 || (rsp->last_set_label == label_tick
9779 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9780 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9781 && REGNO (x) < reg_n_sets_max
9782 && REG_N_SETS (REGNO (x)) == 1
9783 && !REGNO_REG_SET_P
9784 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9785 REGNO (x)))))
9787 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9789 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9790 /* We don't know anything about the upper bits. */
9791 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9793 *nonzero &= mask;
9794 return NULL;
9797 tem = get_last_value (x);
9799 if (tem)
9801 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9802 tem = sign_extend_short_imm (tem, GET_MODE (x),
9803 GET_MODE_PRECISION (mode));
9804 #endif
9805 return tem;
9807 else if (nonzero_sign_valid && rsp->nonzero_bits)
9809 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9811 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9812 /* We don't know anything about the upper bits. */
9813 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9815 *nonzero &= mask;
9818 return NULL;
9821 /* Return the number of bits at the high-order end of X that are known to
9822 be equal to the sign bit. X will be used in mode MODE; if MODE is
9823 VOIDmode, X will be used in its own mode. The returned value will always
9824 be between 1 and the number of bits in MODE. */
9826 static rtx
9827 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9828 const_rtx known_x ATTRIBUTE_UNUSED,
9829 machine_mode known_mode
9830 ATTRIBUTE_UNUSED,
9831 unsigned int known_ret ATTRIBUTE_UNUSED,
9832 unsigned int *result)
9834 rtx tem;
9835 reg_stat_type *rsp;
9837 rsp = &reg_stat[REGNO (x)];
9838 if (rsp->last_set_value != 0
9839 && rsp->last_set_mode == mode
9840 && ((rsp->last_set_label >= label_tick_ebb_start
9841 && rsp->last_set_label < label_tick)
9842 || (rsp->last_set_label == label_tick
9843 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9844 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9845 && REGNO (x) < reg_n_sets_max
9846 && REG_N_SETS (REGNO (x)) == 1
9847 && !REGNO_REG_SET_P
9848 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9849 REGNO (x)))))
9851 *result = rsp->last_set_sign_bit_copies;
9852 return NULL;
9855 tem = get_last_value (x);
9856 if (tem != 0)
9857 return tem;
9859 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9860 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9861 *result = rsp->sign_bit_copies;
9863 return NULL;
9866 /* Return the number of "extended" bits there are in X, when interpreted
9867 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9868 unsigned quantities, this is the number of high-order zero bits.
9869 For signed quantities, this is the number of copies of the sign bit
9870 minus 1. In both case, this function returns the number of "spare"
9871 bits. For example, if two quantities for which this function returns
9872 at least 1 are added, the addition is known not to overflow.
9874 This function will always return 0 unless called during combine, which
9875 implies that it must be called from a define_split. */
9877 unsigned int
9878 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9880 if (nonzero_sign_valid == 0)
9881 return 0;
9883 return (unsignedp
9884 ? (HWI_COMPUTABLE_MODE_P (mode)
9885 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9886 - floor_log2 (nonzero_bits (x, mode)))
9887 : 0)
9888 : num_sign_bit_copies (x, mode) - 1);
9891 /* This function is called from `simplify_shift_const' to merge two
9892 outer operations. Specifically, we have already found that we need
9893 to perform operation *POP0 with constant *PCONST0 at the outermost
9894 position. We would now like to also perform OP1 with constant CONST1
9895 (with *POP0 being done last).
9897 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9898 the resulting operation. *PCOMP_P is set to 1 if we would need to
9899 complement the innermost operand, otherwise it is unchanged.
9901 MODE is the mode in which the operation will be done. No bits outside
9902 the width of this mode matter. It is assumed that the width of this mode
9903 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9905 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9906 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9907 result is simply *PCONST0.
9909 If the resulting operation cannot be expressed as one operation, we
9910 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9912 static int
9913 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)
9915 enum rtx_code op0 = *pop0;
9916 HOST_WIDE_INT const0 = *pconst0;
9918 const0 &= GET_MODE_MASK (mode);
9919 const1 &= GET_MODE_MASK (mode);
9921 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9922 if (op0 == AND)
9923 const1 &= const0;
9925 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9926 if OP0 is SET. */
9928 if (op1 == UNKNOWN || op0 == SET)
9929 return 1;
9931 else if (op0 == UNKNOWN)
9932 op0 = op1, const0 = const1;
9934 else if (op0 == op1)
9936 switch (op0)
9938 case AND:
9939 const0 &= const1;
9940 break;
9941 case IOR:
9942 const0 |= const1;
9943 break;
9944 case XOR:
9945 const0 ^= const1;
9946 break;
9947 case PLUS:
9948 const0 += const1;
9949 break;
9950 case NEG:
9951 op0 = UNKNOWN;
9952 break;
9953 default:
9954 break;
9958 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9959 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9960 return 0;
9962 /* If the two constants aren't the same, we can't do anything. The
9963 remaining six cases can all be done. */
9964 else if (const0 != const1)
9965 return 0;
9967 else
9968 switch (op0)
9970 case IOR:
9971 if (op1 == AND)
9972 /* (a & b) | b == b */
9973 op0 = SET;
9974 else /* op1 == XOR */
9975 /* (a ^ b) | b == a | b */
9977 break;
9979 case XOR:
9980 if (op1 == AND)
9981 /* (a & b) ^ b == (~a) & b */
9982 op0 = AND, *pcomp_p = 1;
9983 else /* op1 == IOR */
9984 /* (a | b) ^ b == a & ~b */
9985 op0 = AND, const0 = ~const0;
9986 break;
9988 case AND:
9989 if (op1 == IOR)
9990 /* (a | b) & b == b */
9991 op0 = SET;
9992 else /* op1 == XOR */
9993 /* (a ^ b) & b) == (~a) & b */
9994 *pcomp_p = 1;
9995 break;
9996 default:
9997 break;
10000 /* Check for NO-OP cases. */
10001 const0 &= GET_MODE_MASK (mode);
10002 if (const0 == 0
10003 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10004 op0 = UNKNOWN;
10005 else if (const0 == 0 && op0 == AND)
10006 op0 = SET;
10007 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10008 && op0 == AND)
10009 op0 = UNKNOWN;
10011 *pop0 = op0;
10013 /* ??? Slightly redundant with the above mask, but not entirely.
10014 Moving this above means we'd have to sign-extend the mode mask
10015 for the final test. */
10016 if (op0 != UNKNOWN && op0 != NEG)
10017 *pconst0 = trunc_int_for_mode (const0, mode);
10019 return 1;
10022 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10023 the shift in. The original shift operation CODE is performed on OP in
10024 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10025 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10026 result of the shift is subject to operation OUTER_CODE with operand
10027 OUTER_CONST. */
10029 static machine_mode
10030 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10031 machine_mode orig_mode, machine_mode mode,
10032 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10034 if (orig_mode == mode)
10035 return mode;
10036 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10038 /* In general we can't perform in wider mode for right shift and rotate. */
10039 switch (code)
10041 case ASHIFTRT:
10042 /* We can still widen if the bits brought in from the left are identical
10043 to the sign bit of ORIG_MODE. */
10044 if (num_sign_bit_copies (op, mode)
10045 > (unsigned) (GET_MODE_PRECISION (mode)
10046 - GET_MODE_PRECISION (orig_mode)))
10047 return mode;
10048 return orig_mode;
10050 case LSHIFTRT:
10051 /* Similarly here but with zero bits. */
10052 if (HWI_COMPUTABLE_MODE_P (mode)
10053 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10054 return mode;
10056 /* We can also widen if the bits brought in will be masked off. This
10057 operation is performed in ORIG_MODE. */
10058 if (outer_code == AND)
10060 int care_bits = low_bitmask_len (orig_mode, outer_const);
10062 if (care_bits >= 0
10063 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10064 return mode;
10066 /* fall through */
10068 case ROTATE:
10069 return orig_mode;
10071 case ROTATERT:
10072 gcc_unreachable ();
10074 default:
10075 return mode;
10079 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10080 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10081 if we cannot simplify it. Otherwise, return a simplified value.
10083 The shift is normally computed in the widest mode we find in VAROP, as
10084 long as it isn't a different number of words than RESULT_MODE. Exceptions
10085 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10087 static rtx
10088 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10089 rtx varop, int orig_count)
10091 enum rtx_code orig_code = code;
10092 rtx orig_varop = varop;
10093 int count;
10094 machine_mode mode = result_mode;
10095 machine_mode shift_mode, tmode;
10096 unsigned int mode_words
10097 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10098 /* We form (outer_op (code varop count) (outer_const)). */
10099 enum rtx_code outer_op = UNKNOWN;
10100 HOST_WIDE_INT outer_const = 0;
10101 int complement_p = 0;
10102 rtx new_rtx, x;
10104 /* Make sure and truncate the "natural" shift on the way in. We don't
10105 want to do this inside the loop as it makes it more difficult to
10106 combine shifts. */
10107 if (SHIFT_COUNT_TRUNCATED)
10108 orig_count &= GET_MODE_BITSIZE (mode) - 1;
10110 /* If we were given an invalid count, don't do anything except exactly
10111 what was requested. */
10113 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
10114 return NULL_RTX;
10116 count = orig_count;
10118 /* Unless one of the branches of the `if' in this loop does a `continue',
10119 we will `break' the loop after the `if'. */
10121 while (count != 0)
10123 /* If we have an operand of (clobber (const_int 0)), fail. */
10124 if (GET_CODE (varop) == CLOBBER)
10125 return NULL_RTX;
10127 /* Convert ROTATERT to ROTATE. */
10128 if (code == ROTATERT)
10130 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
10131 code = ROTATE;
10132 if (VECTOR_MODE_P (result_mode))
10133 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10134 else
10135 count = bitsize - count;
10138 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10139 mode, outer_op, outer_const);
10141 /* Handle cases where the count is greater than the size of the mode
10142 minus 1. For ASHIFT, use the size minus one as the count (this can
10143 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10144 take the count modulo the size. For other shifts, the result is
10145 zero.
10147 Since these shifts are being produced by the compiler by combining
10148 multiple operations, each of which are defined, we know what the
10149 result is supposed to be. */
10151 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10153 if (code == ASHIFTRT)
10154 count = GET_MODE_PRECISION (shift_mode) - 1;
10155 else if (code == ROTATE || code == ROTATERT)
10156 count %= GET_MODE_PRECISION (shift_mode);
10157 else
10159 /* We can't simply return zero because there may be an
10160 outer op. */
10161 varop = const0_rtx;
10162 count = 0;
10163 break;
10167 /* If we discovered we had to complement VAROP, leave. Making a NOT
10168 here would cause an infinite loop. */
10169 if (complement_p)
10170 break;
10172 /* An arithmetic right shift of a quantity known to be -1 or 0
10173 is a no-op. */
10174 if (code == ASHIFTRT
10175 && (num_sign_bit_copies (varop, shift_mode)
10176 == GET_MODE_PRECISION (shift_mode)))
10178 count = 0;
10179 break;
10182 /* If we are doing an arithmetic right shift and discarding all but
10183 the sign bit copies, this is equivalent to doing a shift by the
10184 bitsize minus one. Convert it into that shift because it will often
10185 allow other simplifications. */
10187 if (code == ASHIFTRT
10188 && (count + num_sign_bit_copies (varop, shift_mode)
10189 >= GET_MODE_PRECISION (shift_mode)))
10190 count = GET_MODE_PRECISION (shift_mode) - 1;
10192 /* We simplify the tests below and elsewhere by converting
10193 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10194 `make_compound_operation' will convert it to an ASHIFTRT for
10195 those machines (such as VAX) that don't have an LSHIFTRT. */
10196 if (code == ASHIFTRT
10197 && val_signbit_known_clear_p (shift_mode,
10198 nonzero_bits (varop, shift_mode)))
10199 code = LSHIFTRT;
10201 if (((code == LSHIFTRT
10202 && HWI_COMPUTABLE_MODE_P (shift_mode)
10203 && !(nonzero_bits (varop, shift_mode) >> count))
10204 || (code == ASHIFT
10205 && HWI_COMPUTABLE_MODE_P (shift_mode)
10206 && !((nonzero_bits (varop, shift_mode) << count)
10207 & GET_MODE_MASK (shift_mode))))
10208 && !side_effects_p (varop))
10209 varop = const0_rtx;
10211 switch (GET_CODE (varop))
10213 case SIGN_EXTEND:
10214 case ZERO_EXTEND:
10215 case SIGN_EXTRACT:
10216 case ZERO_EXTRACT:
10217 new_rtx = expand_compound_operation (varop);
10218 if (new_rtx != varop)
10220 varop = new_rtx;
10221 continue;
10223 break;
10225 case MEM:
10226 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10227 minus the width of a smaller mode, we can do this with a
10228 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10229 if ((code == ASHIFTRT || code == LSHIFTRT)
10230 && ! mode_dependent_address_p (XEXP (varop, 0),
10231 MEM_ADDR_SPACE (varop))
10232 && ! MEM_VOLATILE_P (varop)
10233 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10234 MODE_INT, 1)) != BLKmode)
10236 new_rtx = adjust_address_nv (varop, tmode,
10237 BYTES_BIG_ENDIAN ? 0
10238 : count / BITS_PER_UNIT);
10240 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10241 : ZERO_EXTEND, mode, new_rtx);
10242 count = 0;
10243 continue;
10245 break;
10247 case SUBREG:
10248 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10249 the same number of words as what we've seen so far. Then store
10250 the widest mode in MODE. */
10251 if (subreg_lowpart_p (varop)
10252 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10253 > GET_MODE_SIZE (GET_MODE (varop)))
10254 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10255 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10256 == mode_words
10257 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10258 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10260 varop = SUBREG_REG (varop);
10261 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10262 mode = GET_MODE (varop);
10263 continue;
10265 break;
10267 case MULT:
10268 /* Some machines use MULT instead of ASHIFT because MULT
10269 is cheaper. But it is still better on those machines to
10270 merge two shifts into one. */
10271 if (CONST_INT_P (XEXP (varop, 1))
10272 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10274 varop
10275 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10276 XEXP (varop, 0),
10277 GEN_INT (exact_log2 (
10278 UINTVAL (XEXP (varop, 1)))));
10279 continue;
10281 break;
10283 case UDIV:
10284 /* Similar, for when divides are cheaper. */
10285 if (CONST_INT_P (XEXP (varop, 1))
10286 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10288 varop
10289 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10290 XEXP (varop, 0),
10291 GEN_INT (exact_log2 (
10292 UINTVAL (XEXP (varop, 1)))));
10293 continue;
10295 break;
10297 case ASHIFTRT:
10298 /* If we are extracting just the sign bit of an arithmetic
10299 right shift, that shift is not needed. However, the sign
10300 bit of a wider mode may be different from what would be
10301 interpreted as the sign bit in a narrower mode, so, if
10302 the result is narrower, don't discard the shift. */
10303 if (code == LSHIFTRT
10304 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10305 && (GET_MODE_BITSIZE (result_mode)
10306 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10308 varop = XEXP (varop, 0);
10309 continue;
10312 /* ... fall through ... */
10314 case LSHIFTRT:
10315 case ASHIFT:
10316 case ROTATE:
10317 /* Here we have two nested shifts. The result is usually the
10318 AND of a new shift with a mask. We compute the result below. */
10319 if (CONST_INT_P (XEXP (varop, 1))
10320 && INTVAL (XEXP (varop, 1)) >= 0
10321 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10322 && HWI_COMPUTABLE_MODE_P (result_mode)
10323 && HWI_COMPUTABLE_MODE_P (mode)
10324 && !VECTOR_MODE_P (result_mode))
10326 enum rtx_code first_code = GET_CODE (varop);
10327 unsigned int first_count = INTVAL (XEXP (varop, 1));
10328 unsigned HOST_WIDE_INT mask;
10329 rtx mask_rtx;
10331 /* We have one common special case. We can't do any merging if
10332 the inner code is an ASHIFTRT of a smaller mode. However, if
10333 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10334 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10335 we can convert it to
10336 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10337 This simplifies certain SIGN_EXTEND operations. */
10338 if (code == ASHIFT && first_code == ASHIFTRT
10339 && count == (GET_MODE_PRECISION (result_mode)
10340 - GET_MODE_PRECISION (GET_MODE (varop))))
10342 /* C3 has the low-order C1 bits zero. */
10344 mask = GET_MODE_MASK (mode)
10345 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10347 varop = simplify_and_const_int (NULL_RTX, result_mode,
10348 XEXP (varop, 0), mask);
10349 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10350 varop, count);
10351 count = first_count;
10352 code = ASHIFTRT;
10353 continue;
10356 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10357 than C1 high-order bits equal to the sign bit, we can convert
10358 this to either an ASHIFT or an ASHIFTRT depending on the
10359 two counts.
10361 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10363 if (code == ASHIFTRT && first_code == ASHIFT
10364 && GET_MODE (varop) == shift_mode
10365 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10366 > first_count))
10368 varop = XEXP (varop, 0);
10369 count -= first_count;
10370 if (count < 0)
10372 count = -count;
10373 code = ASHIFT;
10376 continue;
10379 /* There are some cases we can't do. If CODE is ASHIFTRT,
10380 we can only do this if FIRST_CODE is also ASHIFTRT.
10382 We can't do the case when CODE is ROTATE and FIRST_CODE is
10383 ASHIFTRT.
10385 If the mode of this shift is not the mode of the outer shift,
10386 we can't do this if either shift is a right shift or ROTATE.
10388 Finally, we can't do any of these if the mode is too wide
10389 unless the codes are the same.
10391 Handle the case where the shift codes are the same
10392 first. */
10394 if (code == first_code)
10396 if (GET_MODE (varop) != result_mode
10397 && (code == ASHIFTRT || code == LSHIFTRT
10398 || code == ROTATE))
10399 break;
10401 count += first_count;
10402 varop = XEXP (varop, 0);
10403 continue;
10406 if (code == ASHIFTRT
10407 || (code == ROTATE && first_code == ASHIFTRT)
10408 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10409 || (GET_MODE (varop) != result_mode
10410 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10411 || first_code == ROTATE
10412 || code == ROTATE)))
10413 break;
10415 /* To compute the mask to apply after the shift, shift the
10416 nonzero bits of the inner shift the same way the
10417 outer shift will. */
10419 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10420 result_mode);
10422 mask_rtx
10423 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10424 GEN_INT (count));
10426 /* Give up if we can't compute an outer operation to use. */
10427 if (mask_rtx == 0
10428 || !CONST_INT_P (mask_rtx)
10429 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10430 INTVAL (mask_rtx),
10431 result_mode, &complement_p))
10432 break;
10434 /* If the shifts are in the same direction, we add the
10435 counts. Otherwise, we subtract them. */
10436 if ((code == ASHIFTRT || code == LSHIFTRT)
10437 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10438 count += first_count;
10439 else
10440 count -= first_count;
10442 /* If COUNT is positive, the new shift is usually CODE,
10443 except for the two exceptions below, in which case it is
10444 FIRST_CODE. If the count is negative, FIRST_CODE should
10445 always be used */
10446 if (count > 0
10447 && ((first_code == ROTATE && code == ASHIFT)
10448 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10449 code = first_code;
10450 else if (count < 0)
10451 code = first_code, count = -count;
10453 varop = XEXP (varop, 0);
10454 continue;
10457 /* If we have (A << B << C) for any shift, we can convert this to
10458 (A << C << B). This wins if A is a constant. Only try this if
10459 B is not a constant. */
10461 else if (GET_CODE (varop) == code
10462 && CONST_INT_P (XEXP (varop, 0))
10463 && !CONST_INT_P (XEXP (varop, 1)))
10465 rtx new_rtx = simplify_const_binary_operation (code, mode,
10466 XEXP (varop, 0),
10467 GEN_INT (count));
10468 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10469 count = 0;
10470 continue;
10472 break;
10474 case NOT:
10475 if (VECTOR_MODE_P (mode))
10476 break;
10478 /* Make this fit the case below. */
10479 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10480 continue;
10482 case IOR:
10483 case AND:
10484 case XOR:
10485 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10486 with C the size of VAROP - 1 and the shift is logical if
10487 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10488 we have an (le X 0) operation. If we have an arithmetic shift
10489 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10490 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10492 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10493 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10494 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10495 && (code == LSHIFTRT || code == ASHIFTRT)
10496 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10497 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10499 count = 0;
10500 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10501 const0_rtx);
10503 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10504 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10506 continue;
10509 /* If we have (shift (logical)), move the logical to the outside
10510 to allow it to possibly combine with another logical and the
10511 shift to combine with another shift. This also canonicalizes to
10512 what a ZERO_EXTRACT looks like. Also, some machines have
10513 (and (shift)) insns. */
10515 if (CONST_INT_P (XEXP (varop, 1))
10516 /* We can't do this if we have (ashiftrt (xor)) and the
10517 constant has its sign bit set in shift_mode with shift_mode
10518 wider than result_mode. */
10519 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10520 && result_mode != shift_mode
10521 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10522 shift_mode))
10523 && (new_rtx = simplify_const_binary_operation
10524 (code, result_mode,
10525 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10526 GEN_INT (count))) != 0
10527 && CONST_INT_P (new_rtx)
10528 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10529 INTVAL (new_rtx), result_mode, &complement_p))
10531 varop = XEXP (varop, 0);
10532 continue;
10535 /* If we can't do that, try to simplify the shift in each arm of the
10536 logical expression, make a new logical expression, and apply
10537 the inverse distributive law. This also can't be done for
10538 (ashiftrt (xor)) where we've widened the shift and the constant
10539 changes the sign bit. */
10540 if (CONST_INT_P (XEXP (varop, 1))
10541 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10542 && result_mode != shift_mode
10543 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10544 shift_mode)))
10546 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10547 XEXP (varop, 0), count);
10548 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10549 XEXP (varop, 1), count);
10551 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10552 lhs, rhs);
10553 varop = apply_distributive_law (varop);
10555 count = 0;
10556 continue;
10558 break;
10560 case EQ:
10561 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10562 says that the sign bit can be tested, FOO has mode MODE, C is
10563 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10564 that may be nonzero. */
10565 if (code == LSHIFTRT
10566 && XEXP (varop, 1) == const0_rtx
10567 && GET_MODE (XEXP (varop, 0)) == result_mode
10568 && count == (GET_MODE_PRECISION (result_mode) - 1)
10569 && HWI_COMPUTABLE_MODE_P (result_mode)
10570 && STORE_FLAG_VALUE == -1
10571 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10572 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10573 &complement_p))
10575 varop = XEXP (varop, 0);
10576 count = 0;
10577 continue;
10579 break;
10581 case NEG:
10582 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10583 than the number of bits in the mode is equivalent to A. */
10584 if (code == LSHIFTRT
10585 && count == (GET_MODE_PRECISION (result_mode) - 1)
10586 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10588 varop = XEXP (varop, 0);
10589 count = 0;
10590 continue;
10593 /* NEG commutes with ASHIFT since it is multiplication. Move the
10594 NEG outside to allow shifts to combine. */
10595 if (code == ASHIFT
10596 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10597 &complement_p))
10599 varop = XEXP (varop, 0);
10600 continue;
10602 break;
10604 case PLUS:
10605 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10606 is one less than the number of bits in the mode is
10607 equivalent to (xor A 1). */
10608 if (code == LSHIFTRT
10609 && count == (GET_MODE_PRECISION (result_mode) - 1)
10610 && XEXP (varop, 1) == constm1_rtx
10611 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10612 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10613 &complement_p))
10615 count = 0;
10616 varop = XEXP (varop, 0);
10617 continue;
10620 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10621 that might be nonzero in BAR are those being shifted out and those
10622 bits are known zero in FOO, we can replace the PLUS with FOO.
10623 Similarly in the other operand order. This code occurs when
10624 we are computing the size of a variable-size array. */
10626 if ((code == ASHIFTRT || code == LSHIFTRT)
10627 && count < HOST_BITS_PER_WIDE_INT
10628 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10629 && (nonzero_bits (XEXP (varop, 1), result_mode)
10630 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10632 varop = XEXP (varop, 0);
10633 continue;
10635 else if ((code == ASHIFTRT || code == LSHIFTRT)
10636 && count < HOST_BITS_PER_WIDE_INT
10637 && HWI_COMPUTABLE_MODE_P (result_mode)
10638 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10639 >> count)
10640 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10641 & nonzero_bits (XEXP (varop, 1),
10642 result_mode)))
10644 varop = XEXP (varop, 1);
10645 continue;
10648 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10649 if (code == ASHIFT
10650 && CONST_INT_P (XEXP (varop, 1))
10651 && (new_rtx = simplify_const_binary_operation
10652 (ASHIFT, result_mode,
10653 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10654 GEN_INT (count))) != 0
10655 && CONST_INT_P (new_rtx)
10656 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10657 INTVAL (new_rtx), result_mode, &complement_p))
10659 varop = XEXP (varop, 0);
10660 continue;
10663 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10664 signbit', and attempt to change the PLUS to an XOR and move it to
10665 the outer operation as is done above in the AND/IOR/XOR case
10666 leg for shift(logical). See details in logical handling above
10667 for reasoning in doing so. */
10668 if (code == LSHIFTRT
10669 && CONST_INT_P (XEXP (varop, 1))
10670 && mode_signbit_p (result_mode, XEXP (varop, 1))
10671 && (new_rtx = simplify_const_binary_operation
10672 (code, result_mode,
10673 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10674 GEN_INT (count))) != 0
10675 && CONST_INT_P (new_rtx)
10676 && merge_outer_ops (&outer_op, &outer_const, XOR,
10677 INTVAL (new_rtx), result_mode, &complement_p))
10679 varop = XEXP (varop, 0);
10680 continue;
10683 break;
10685 case MINUS:
10686 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10687 with C the size of VAROP - 1 and the shift is logical if
10688 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10689 we have a (gt X 0) operation. If the shift is arithmetic with
10690 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10691 we have a (neg (gt X 0)) operation. */
10693 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10694 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10695 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10696 && (code == LSHIFTRT || code == ASHIFTRT)
10697 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10698 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10699 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10701 count = 0;
10702 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10703 const0_rtx);
10705 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10706 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10708 continue;
10710 break;
10712 case TRUNCATE:
10713 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10714 if the truncate does not affect the value. */
10715 if (code == LSHIFTRT
10716 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10717 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10718 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10719 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10720 - GET_MODE_PRECISION (GET_MODE (varop)))))
10722 rtx varop_inner = XEXP (varop, 0);
10724 varop_inner
10725 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10726 XEXP (varop_inner, 0),
10727 GEN_INT
10728 (count + INTVAL (XEXP (varop_inner, 1))));
10729 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10730 count = 0;
10731 continue;
10733 break;
10735 default:
10736 break;
10739 break;
10742 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10743 outer_op, outer_const);
10745 /* We have now finished analyzing the shift. The result should be
10746 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10747 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10748 to the result of the shift. OUTER_CONST is the relevant constant,
10749 but we must turn off all bits turned off in the shift. */
10751 if (outer_op == UNKNOWN
10752 && orig_code == code && orig_count == count
10753 && varop == orig_varop
10754 && shift_mode == GET_MODE (varop))
10755 return NULL_RTX;
10757 /* Make a SUBREG if necessary. If we can't make it, fail. */
10758 varop = gen_lowpart (shift_mode, varop);
10759 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10760 return NULL_RTX;
10762 /* If we have an outer operation and we just made a shift, it is
10763 possible that we could have simplified the shift were it not
10764 for the outer operation. So try to do the simplification
10765 recursively. */
10767 if (outer_op != UNKNOWN)
10768 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10769 else
10770 x = NULL_RTX;
10772 if (x == NULL_RTX)
10773 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10775 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10776 turn off all the bits that the shift would have turned off. */
10777 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10778 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10779 GET_MODE_MASK (result_mode) >> orig_count);
10781 /* Do the remainder of the processing in RESULT_MODE. */
10782 x = gen_lowpart_or_truncate (result_mode, x);
10784 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10785 operation. */
10786 if (complement_p)
10787 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10789 if (outer_op != UNKNOWN)
10791 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10792 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10793 outer_const = trunc_int_for_mode (outer_const, result_mode);
10795 if (outer_op == AND)
10796 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10797 else if (outer_op == SET)
10799 /* This means that we have determined that the result is
10800 equivalent to a constant. This should be rare. */
10801 if (!side_effects_p (x))
10802 x = GEN_INT (outer_const);
10804 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10805 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10806 else
10807 x = simplify_gen_binary (outer_op, result_mode, x,
10808 GEN_INT (outer_const));
10811 return x;
10814 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10815 The result of the shift is RESULT_MODE. If we cannot simplify it,
10816 return X or, if it is NULL, synthesize the expression with
10817 simplify_gen_binary. Otherwise, return a simplified value.
10819 The shift is normally computed in the widest mode we find in VAROP, as
10820 long as it isn't a different number of words than RESULT_MODE. Exceptions
10821 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10823 static rtx
10824 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10825 rtx varop, int count)
10827 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10828 if (tem)
10829 return tem;
10831 if (!x)
10832 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10833 if (GET_MODE (x) != result_mode)
10834 x = gen_lowpart (result_mode, x);
10835 return x;
10839 /* Like recog, but we receive the address of a pointer to a new pattern.
10840 We try to match the rtx that the pointer points to.
10841 If that fails, we may try to modify or replace the pattern,
10842 storing the replacement into the same pointer object.
10844 Modifications include deletion or addition of CLOBBERs.
10846 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10847 the CLOBBERs are placed.
10849 The value is the final insn code from the pattern ultimately matched,
10850 or -1. */
10852 static int
10853 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10855 rtx pat = *pnewpat;
10856 rtx pat_without_clobbers;
10857 int insn_code_number;
10858 int num_clobbers_to_add = 0;
10859 int i;
10860 rtx notes = NULL_RTX;
10861 rtx old_notes, old_pat;
10862 int old_icode;
10864 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10865 we use to indicate that something didn't match. If we find such a
10866 thing, force rejection. */
10867 if (GET_CODE (pat) == PARALLEL)
10868 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10869 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10870 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10871 return -1;
10873 old_pat = PATTERN (insn);
10874 old_notes = REG_NOTES (insn);
10875 PATTERN (insn) = pat;
10876 REG_NOTES (insn) = NULL_RTX;
10878 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10879 if (dump_file && (dump_flags & TDF_DETAILS))
10881 if (insn_code_number < 0)
10882 fputs ("Failed to match this instruction:\n", dump_file);
10883 else
10884 fputs ("Successfully matched this instruction:\n", dump_file);
10885 print_rtl_single (dump_file, pat);
10888 /* If it isn't, there is the possibility that we previously had an insn
10889 that clobbered some register as a side effect, but the combined
10890 insn doesn't need to do that. So try once more without the clobbers
10891 unless this represents an ASM insn. */
10893 if (insn_code_number < 0 && ! check_asm_operands (pat)
10894 && GET_CODE (pat) == PARALLEL)
10896 int pos;
10898 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10899 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10901 if (i != pos)
10902 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10903 pos++;
10906 SUBST_INT (XVECLEN (pat, 0), pos);
10908 if (pos == 1)
10909 pat = XVECEXP (pat, 0, 0);
10911 PATTERN (insn) = pat;
10912 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10913 if (dump_file && (dump_flags & TDF_DETAILS))
10915 if (insn_code_number < 0)
10916 fputs ("Failed to match this instruction:\n", dump_file);
10917 else
10918 fputs ("Successfully matched this instruction:\n", dump_file);
10919 print_rtl_single (dump_file, pat);
10923 pat_without_clobbers = pat;
10925 PATTERN (insn) = old_pat;
10926 REG_NOTES (insn) = old_notes;
10928 /* Recognize all noop sets, these will be killed by followup pass. */
10929 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10930 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10932 /* If we had any clobbers to add, make a new pattern than contains
10933 them. Then check to make sure that all of them are dead. */
10934 if (num_clobbers_to_add)
10936 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10937 rtvec_alloc (GET_CODE (pat) == PARALLEL
10938 ? (XVECLEN (pat, 0)
10939 + num_clobbers_to_add)
10940 : num_clobbers_to_add + 1));
10942 if (GET_CODE (pat) == PARALLEL)
10943 for (i = 0; i < XVECLEN (pat, 0); i++)
10944 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10945 else
10946 XVECEXP (newpat, 0, 0) = pat;
10948 add_clobbers (newpat, insn_code_number);
10950 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10951 i < XVECLEN (newpat, 0); i++)
10953 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10954 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10955 return -1;
10956 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10958 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10959 notes = alloc_reg_note (REG_UNUSED,
10960 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10963 pat = newpat;
10966 if (insn_code_number >= 0
10967 && insn_code_number != NOOP_MOVE_INSN_CODE)
10969 old_pat = PATTERN (insn);
10970 old_notes = REG_NOTES (insn);
10971 old_icode = INSN_CODE (insn);
10972 PATTERN (insn) = pat;
10973 REG_NOTES (insn) = notes;
10975 /* Allow targets to reject combined insn. */
10976 if (!targetm.legitimate_combined_insn (insn))
10978 if (dump_file && (dump_flags & TDF_DETAILS))
10979 fputs ("Instruction not appropriate for target.",
10980 dump_file);
10982 /* Callers expect recog_for_combine to strip
10983 clobbers from the pattern on failure. */
10984 pat = pat_without_clobbers;
10985 notes = NULL_RTX;
10987 insn_code_number = -1;
10990 PATTERN (insn) = old_pat;
10991 REG_NOTES (insn) = old_notes;
10992 INSN_CODE (insn) = old_icode;
10995 *pnewpat = pat;
10996 *pnotes = notes;
10998 return insn_code_number;
11001 /* Like gen_lowpart_general but for use by combine. In combine it
11002 is not possible to create any new pseudoregs. However, it is
11003 safe to create invalid memory addresses, because combine will
11004 try to recognize them and all they will do is make the combine
11005 attempt fail.
11007 If for some reason this cannot do its job, an rtx
11008 (clobber (const_int 0)) is returned.
11009 An insn containing that will not be recognized. */
11011 static rtx
11012 gen_lowpart_for_combine (machine_mode omode, rtx x)
11014 machine_mode imode = GET_MODE (x);
11015 unsigned int osize = GET_MODE_SIZE (omode);
11016 unsigned int isize = GET_MODE_SIZE (imode);
11017 rtx result;
11019 if (omode == imode)
11020 return x;
11022 /* We can only support MODE being wider than a word if X is a
11023 constant integer or has a mode the same size. */
11024 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11025 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11026 goto fail;
11028 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11029 won't know what to do. So we will strip off the SUBREG here and
11030 process normally. */
11031 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11033 x = SUBREG_REG (x);
11035 /* For use in case we fall down into the address adjustments
11036 further below, we need to adjust the known mode and size of
11037 x; imode and isize, since we just adjusted x. */
11038 imode = GET_MODE (x);
11040 if (imode == omode)
11041 return x;
11043 isize = GET_MODE_SIZE (imode);
11046 result = gen_lowpart_common (omode, x);
11048 if (result)
11049 return result;
11051 if (MEM_P (x))
11053 int offset = 0;
11055 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11056 address. */
11057 if (MEM_VOLATILE_P (x)
11058 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11059 goto fail;
11061 /* If we want to refer to something bigger than the original memref,
11062 generate a paradoxical subreg instead. That will force a reload
11063 of the original memref X. */
11064 if (isize < osize)
11065 return gen_rtx_SUBREG (omode, x, 0);
11067 if (WORDS_BIG_ENDIAN)
11068 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11070 /* Adjust the address so that the address-after-the-data is
11071 unchanged. */
11072 if (BYTES_BIG_ENDIAN)
11073 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11075 return adjust_address_nv (x, omode, offset);
11078 /* If X is a comparison operator, rewrite it in a new mode. This
11079 probably won't match, but may allow further simplifications. */
11080 else if (COMPARISON_P (x))
11081 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11083 /* If we couldn't simplify X any other way, just enclose it in a
11084 SUBREG. Normally, this SUBREG won't match, but some patterns may
11085 include an explicit SUBREG or we may simplify it further in combine. */
11086 else
11088 int offset = 0;
11089 rtx res;
11091 offset = subreg_lowpart_offset (omode, imode);
11092 if (imode == VOIDmode)
11094 imode = int_mode_for_mode (omode);
11095 x = gen_lowpart_common (imode, x);
11096 if (x == NULL)
11097 goto fail;
11099 res = simplify_gen_subreg (omode, x, imode, offset);
11100 if (res)
11101 return res;
11104 fail:
11105 return gen_rtx_CLOBBER (omode, const0_rtx);
11108 /* Try to simplify a comparison between OP0 and a constant OP1,
11109 where CODE is the comparison code that will be tested, into a
11110 (CODE OP0 const0_rtx) form.
11112 The result is a possibly different comparison code to use.
11113 *POP1 may be updated. */
11115 static enum rtx_code
11116 simplify_compare_const (enum rtx_code code, machine_mode mode,
11117 rtx op0, rtx *pop1)
11119 unsigned int mode_width = GET_MODE_PRECISION (mode);
11120 HOST_WIDE_INT const_op = INTVAL (*pop1);
11122 /* Get the constant we are comparing against and turn off all bits
11123 not on in our mode. */
11124 if (mode != VOIDmode)
11125 const_op = trunc_int_for_mode (const_op, mode);
11127 /* If we are comparing against a constant power of two and the value
11128 being compared can only have that single bit nonzero (e.g., it was
11129 `and'ed with that bit), we can replace this with a comparison
11130 with zero. */
11131 if (const_op
11132 && (code == EQ || code == NE || code == GE || code == GEU
11133 || code == LT || code == LTU)
11134 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11135 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11136 && (nonzero_bits (op0, mode)
11137 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11139 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11140 const_op = 0;
11143 /* Similarly, if we are comparing a value known to be either -1 or
11144 0 with -1, change it to the opposite comparison against zero. */
11145 if (const_op == -1
11146 && (code == EQ || code == NE || code == GT || code == LE
11147 || code == GEU || code == LTU)
11148 && num_sign_bit_copies (op0, mode) == mode_width)
11150 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11151 const_op = 0;
11154 /* Do some canonicalizations based on the comparison code. We prefer
11155 comparisons against zero and then prefer equality comparisons.
11156 If we can reduce the size of a constant, we will do that too. */
11157 switch (code)
11159 case LT:
11160 /* < C is equivalent to <= (C - 1) */
11161 if (const_op > 0)
11163 const_op -= 1;
11164 code = LE;
11165 /* ... fall through to LE case below. */
11167 else
11168 break;
11170 case LE:
11171 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11172 if (const_op < 0)
11174 const_op += 1;
11175 code = LT;
11178 /* If we are doing a <= 0 comparison on a value known to have
11179 a zero sign bit, we can replace this with == 0. */
11180 else if (const_op == 0
11181 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11182 && (nonzero_bits (op0, mode)
11183 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11184 == 0)
11185 code = EQ;
11186 break;
11188 case GE:
11189 /* >= C is equivalent to > (C - 1). */
11190 if (const_op > 0)
11192 const_op -= 1;
11193 code = GT;
11194 /* ... fall through to GT below. */
11196 else
11197 break;
11199 case GT:
11200 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11201 if (const_op < 0)
11203 const_op += 1;
11204 code = GE;
11207 /* If we are doing a > 0 comparison on a value known to have
11208 a zero sign bit, we can replace this with != 0. */
11209 else if (const_op == 0
11210 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11211 && (nonzero_bits (op0, mode)
11212 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11213 == 0)
11214 code = NE;
11215 break;
11217 case LTU:
11218 /* < C is equivalent to <= (C - 1). */
11219 if (const_op > 0)
11221 const_op -= 1;
11222 code = LEU;
11223 /* ... fall through ... */
11225 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11226 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11227 && (unsigned HOST_WIDE_INT) const_op
11228 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11230 const_op = 0;
11231 code = GE;
11232 break;
11234 else
11235 break;
11237 case LEU:
11238 /* unsigned <= 0 is equivalent to == 0 */
11239 if (const_op == 0)
11240 code = EQ;
11241 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11242 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11243 && (unsigned HOST_WIDE_INT) const_op
11244 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11246 const_op = 0;
11247 code = GE;
11249 break;
11251 case GEU:
11252 /* >= C is equivalent to > (C - 1). */
11253 if (const_op > 1)
11255 const_op -= 1;
11256 code = GTU;
11257 /* ... fall through ... */
11260 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11261 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11262 && (unsigned HOST_WIDE_INT) const_op
11263 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11265 const_op = 0;
11266 code = LT;
11267 break;
11269 else
11270 break;
11272 case GTU:
11273 /* unsigned > 0 is equivalent to != 0 */
11274 if (const_op == 0)
11275 code = NE;
11276 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11277 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11278 && (unsigned HOST_WIDE_INT) const_op
11279 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11281 const_op = 0;
11282 code = LT;
11284 break;
11286 default:
11287 break;
11290 *pop1 = GEN_INT (const_op);
11291 return code;
11294 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11295 comparison code that will be tested.
11297 The result is a possibly different comparison code to use. *POP0 and
11298 *POP1 may be updated.
11300 It is possible that we might detect that a comparison is either always
11301 true or always false. However, we do not perform general constant
11302 folding in combine, so this knowledge isn't useful. Such tautologies
11303 should have been detected earlier. Hence we ignore all such cases. */
11305 static enum rtx_code
11306 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11308 rtx op0 = *pop0;
11309 rtx op1 = *pop1;
11310 rtx tem, tem1;
11311 int i;
11312 machine_mode mode, tmode;
11314 /* Try a few ways of applying the same transformation to both operands. */
11315 while (1)
11317 #ifndef WORD_REGISTER_OPERATIONS
11318 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11319 so check specially. */
11320 if (code != GTU && code != GEU && code != LTU && code != LEU
11321 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11322 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11323 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11324 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11325 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11326 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11327 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11328 && CONST_INT_P (XEXP (op0, 1))
11329 && XEXP (op0, 1) == XEXP (op1, 1)
11330 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11331 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11332 && (INTVAL (XEXP (op0, 1))
11333 == (GET_MODE_PRECISION (GET_MODE (op0))
11334 - (GET_MODE_PRECISION
11335 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11337 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11338 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11340 #endif
11342 /* If both operands are the same constant shift, see if we can ignore the
11343 shift. We can if the shift is a rotate or if the bits shifted out of
11344 this shift are known to be zero for both inputs and if the type of
11345 comparison is compatible with the shift. */
11346 if (GET_CODE (op0) == GET_CODE (op1)
11347 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11348 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11349 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11350 && (code != GT && code != LT && code != GE && code != LE))
11351 || (GET_CODE (op0) == ASHIFTRT
11352 && (code != GTU && code != LTU
11353 && code != GEU && code != LEU)))
11354 && CONST_INT_P (XEXP (op0, 1))
11355 && INTVAL (XEXP (op0, 1)) >= 0
11356 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11357 && XEXP (op0, 1) == XEXP (op1, 1))
11359 machine_mode mode = GET_MODE (op0);
11360 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11361 int shift_count = INTVAL (XEXP (op0, 1));
11363 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11364 mask &= (mask >> shift_count) << shift_count;
11365 else if (GET_CODE (op0) == ASHIFT)
11366 mask = (mask & (mask << shift_count)) >> shift_count;
11368 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11369 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11370 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11371 else
11372 break;
11375 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11376 SUBREGs are of the same mode, and, in both cases, the AND would
11377 be redundant if the comparison was done in the narrower mode,
11378 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11379 and the operand's possibly nonzero bits are 0xffffff01; in that case
11380 if we only care about QImode, we don't need the AND). This case
11381 occurs if the output mode of an scc insn is not SImode and
11382 STORE_FLAG_VALUE == 1 (e.g., the 386).
11384 Similarly, check for a case where the AND's are ZERO_EXTEND
11385 operations from some narrower mode even though a SUBREG is not
11386 present. */
11388 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11389 && CONST_INT_P (XEXP (op0, 1))
11390 && CONST_INT_P (XEXP (op1, 1)))
11392 rtx inner_op0 = XEXP (op0, 0);
11393 rtx inner_op1 = XEXP (op1, 0);
11394 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11395 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11396 int changed = 0;
11398 if (paradoxical_subreg_p (inner_op0)
11399 && GET_CODE (inner_op1) == SUBREG
11400 && (GET_MODE (SUBREG_REG (inner_op0))
11401 == GET_MODE (SUBREG_REG (inner_op1)))
11402 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11403 <= HOST_BITS_PER_WIDE_INT)
11404 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11405 GET_MODE (SUBREG_REG (inner_op0)))))
11406 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11407 GET_MODE (SUBREG_REG (inner_op1))))))
11409 op0 = SUBREG_REG (inner_op0);
11410 op1 = SUBREG_REG (inner_op1);
11412 /* The resulting comparison is always unsigned since we masked
11413 off the original sign bit. */
11414 code = unsigned_condition (code);
11416 changed = 1;
11419 else if (c0 == c1)
11420 for (tmode = GET_CLASS_NARROWEST_MODE
11421 (GET_MODE_CLASS (GET_MODE (op0)));
11422 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11423 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11425 op0 = gen_lowpart (tmode, inner_op0);
11426 op1 = gen_lowpart (tmode, inner_op1);
11427 code = unsigned_condition (code);
11428 changed = 1;
11429 break;
11432 if (! changed)
11433 break;
11436 /* If both operands are NOT, we can strip off the outer operation
11437 and adjust the comparison code for swapped operands; similarly for
11438 NEG, except that this must be an equality comparison. */
11439 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11440 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11441 && (code == EQ || code == NE)))
11442 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11444 else
11445 break;
11448 /* If the first operand is a constant, swap the operands and adjust the
11449 comparison code appropriately, but don't do this if the second operand
11450 is already a constant integer. */
11451 if (swap_commutative_operands_p (op0, op1))
11453 std::swap (op0, op1);
11454 code = swap_condition (code);
11457 /* We now enter a loop during which we will try to simplify the comparison.
11458 For the most part, we only are concerned with comparisons with zero,
11459 but some things may really be comparisons with zero but not start
11460 out looking that way. */
11462 while (CONST_INT_P (op1))
11464 machine_mode mode = GET_MODE (op0);
11465 unsigned int mode_width = GET_MODE_PRECISION (mode);
11466 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11467 int equality_comparison_p;
11468 int sign_bit_comparison_p;
11469 int unsigned_comparison_p;
11470 HOST_WIDE_INT const_op;
11472 /* We only want to handle integral modes. This catches VOIDmode,
11473 CCmode, and the floating-point modes. An exception is that we
11474 can handle VOIDmode if OP0 is a COMPARE or a comparison
11475 operation. */
11477 if (GET_MODE_CLASS (mode) != MODE_INT
11478 && ! (mode == VOIDmode
11479 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11480 break;
11482 /* Try to simplify the compare to constant, possibly changing the
11483 comparison op, and/or changing op1 to zero. */
11484 code = simplify_compare_const (code, mode, op0, &op1);
11485 const_op = INTVAL (op1);
11487 /* Compute some predicates to simplify code below. */
11489 equality_comparison_p = (code == EQ || code == NE);
11490 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11491 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11492 || code == GEU);
11494 /* If this is a sign bit comparison and we can do arithmetic in
11495 MODE, say that we will only be needing the sign bit of OP0. */
11496 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11497 op0 = force_to_mode (op0, mode,
11498 (unsigned HOST_WIDE_INT) 1
11499 << (GET_MODE_PRECISION (mode) - 1),
11502 /* Now try cases based on the opcode of OP0. If none of the cases
11503 does a "continue", we exit this loop immediately after the
11504 switch. */
11506 switch (GET_CODE (op0))
11508 case ZERO_EXTRACT:
11509 /* If we are extracting a single bit from a variable position in
11510 a constant that has only a single bit set and are comparing it
11511 with zero, we can convert this into an equality comparison
11512 between the position and the location of the single bit. */
11513 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11514 have already reduced the shift count modulo the word size. */
11515 if (!SHIFT_COUNT_TRUNCATED
11516 && CONST_INT_P (XEXP (op0, 0))
11517 && XEXP (op0, 1) == const1_rtx
11518 && equality_comparison_p && const_op == 0
11519 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11521 if (BITS_BIG_ENDIAN)
11522 i = BITS_PER_WORD - 1 - i;
11524 op0 = XEXP (op0, 2);
11525 op1 = GEN_INT (i);
11526 const_op = i;
11528 /* Result is nonzero iff shift count is equal to I. */
11529 code = reverse_condition (code);
11530 continue;
11533 /* ... fall through ... */
11535 case SIGN_EXTRACT:
11536 tem = expand_compound_operation (op0);
11537 if (tem != op0)
11539 op0 = tem;
11540 continue;
11542 break;
11544 case NOT:
11545 /* If testing for equality, we can take the NOT of the constant. */
11546 if (equality_comparison_p
11547 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11549 op0 = XEXP (op0, 0);
11550 op1 = tem;
11551 continue;
11554 /* If just looking at the sign bit, reverse the sense of the
11555 comparison. */
11556 if (sign_bit_comparison_p)
11558 op0 = XEXP (op0, 0);
11559 code = (code == GE ? LT : GE);
11560 continue;
11562 break;
11564 case NEG:
11565 /* If testing for equality, we can take the NEG of the constant. */
11566 if (equality_comparison_p
11567 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11569 op0 = XEXP (op0, 0);
11570 op1 = tem;
11571 continue;
11574 /* The remaining cases only apply to comparisons with zero. */
11575 if (const_op != 0)
11576 break;
11578 /* When X is ABS or is known positive,
11579 (neg X) is < 0 if and only if X != 0. */
11581 if (sign_bit_comparison_p
11582 && (GET_CODE (XEXP (op0, 0)) == ABS
11583 || (mode_width <= HOST_BITS_PER_WIDE_INT
11584 && (nonzero_bits (XEXP (op0, 0), mode)
11585 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11586 == 0)))
11588 op0 = XEXP (op0, 0);
11589 code = (code == LT ? NE : EQ);
11590 continue;
11593 /* If we have NEG of something whose two high-order bits are the
11594 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11595 if (num_sign_bit_copies (op0, mode) >= 2)
11597 op0 = XEXP (op0, 0);
11598 code = swap_condition (code);
11599 continue;
11601 break;
11603 case ROTATE:
11604 /* If we are testing equality and our count is a constant, we
11605 can perform the inverse operation on our RHS. */
11606 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11607 && (tem = simplify_binary_operation (ROTATERT, mode,
11608 op1, XEXP (op0, 1))) != 0)
11610 op0 = XEXP (op0, 0);
11611 op1 = tem;
11612 continue;
11615 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11616 a particular bit. Convert it to an AND of a constant of that
11617 bit. This will be converted into a ZERO_EXTRACT. */
11618 if (const_op == 0 && sign_bit_comparison_p
11619 && CONST_INT_P (XEXP (op0, 1))
11620 && mode_width <= HOST_BITS_PER_WIDE_INT)
11622 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11623 ((unsigned HOST_WIDE_INT) 1
11624 << (mode_width - 1
11625 - INTVAL (XEXP (op0, 1)))));
11626 code = (code == LT ? NE : EQ);
11627 continue;
11630 /* Fall through. */
11632 case ABS:
11633 /* ABS is ignorable inside an equality comparison with zero. */
11634 if (const_op == 0 && equality_comparison_p)
11636 op0 = XEXP (op0, 0);
11637 continue;
11639 break;
11641 case SIGN_EXTEND:
11642 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11643 (compare FOO CONST) if CONST fits in FOO's mode and we
11644 are either testing inequality or have an unsigned
11645 comparison with ZERO_EXTEND or a signed comparison with
11646 SIGN_EXTEND. But don't do it if we don't have a compare
11647 insn of the given mode, since we'd have to revert it
11648 later on, and then we wouldn't know whether to sign- or
11649 zero-extend. */
11650 mode = GET_MODE (XEXP (op0, 0));
11651 if (GET_MODE_CLASS (mode) == MODE_INT
11652 && ! unsigned_comparison_p
11653 && HWI_COMPUTABLE_MODE_P (mode)
11654 && trunc_int_for_mode (const_op, mode) == const_op
11655 && have_insn_for (COMPARE, mode))
11657 op0 = XEXP (op0, 0);
11658 continue;
11660 break;
11662 case SUBREG:
11663 /* Check for the case where we are comparing A - C1 with C2, that is
11665 (subreg:MODE (plus (A) (-C1))) op (C2)
11667 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11668 comparison in the wider mode. One of the following two conditions
11669 must be true in order for this to be valid:
11671 1. The mode extension results in the same bit pattern being added
11672 on both sides and the comparison is equality or unsigned. As
11673 C2 has been truncated to fit in MODE, the pattern can only be
11674 all 0s or all 1s.
11676 2. The mode extension results in the sign bit being copied on
11677 each side.
11679 The difficulty here is that we have predicates for A but not for
11680 (A - C1) so we need to check that C1 is within proper bounds so
11681 as to perturbate A as little as possible. */
11683 if (mode_width <= HOST_BITS_PER_WIDE_INT
11684 && subreg_lowpart_p (op0)
11685 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11686 && GET_CODE (SUBREG_REG (op0)) == PLUS
11687 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11689 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11690 rtx a = XEXP (SUBREG_REG (op0), 0);
11691 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11693 if ((c1 > 0
11694 && (unsigned HOST_WIDE_INT) c1
11695 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11696 && (equality_comparison_p || unsigned_comparison_p)
11697 /* (A - C1) zero-extends if it is positive and sign-extends
11698 if it is negative, C2 both zero- and sign-extends. */
11699 && ((0 == (nonzero_bits (a, inner_mode)
11700 & ~GET_MODE_MASK (mode))
11701 && const_op >= 0)
11702 /* (A - C1) sign-extends if it is positive and 1-extends
11703 if it is negative, C2 both sign- and 1-extends. */
11704 || (num_sign_bit_copies (a, inner_mode)
11705 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11706 - mode_width)
11707 && const_op < 0)))
11708 || ((unsigned HOST_WIDE_INT) c1
11709 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11710 /* (A - C1) always sign-extends, like C2. */
11711 && num_sign_bit_copies (a, inner_mode)
11712 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11713 - (mode_width - 1))))
11715 op0 = SUBREG_REG (op0);
11716 continue;
11720 /* If the inner mode is narrower and we are extracting the low part,
11721 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11722 if (subreg_lowpart_p (op0)
11723 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11724 /* Fall through */ ;
11725 else
11726 break;
11728 /* ... fall through ... */
11730 case ZERO_EXTEND:
11731 mode = GET_MODE (XEXP (op0, 0));
11732 if (GET_MODE_CLASS (mode) == MODE_INT
11733 && (unsigned_comparison_p || equality_comparison_p)
11734 && HWI_COMPUTABLE_MODE_P (mode)
11735 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11736 && const_op >= 0
11737 && have_insn_for (COMPARE, mode))
11739 op0 = XEXP (op0, 0);
11740 continue;
11742 break;
11744 case PLUS:
11745 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11746 this for equality comparisons due to pathological cases involving
11747 overflows. */
11748 if (equality_comparison_p
11749 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11750 op1, XEXP (op0, 1))))
11752 op0 = XEXP (op0, 0);
11753 op1 = tem;
11754 continue;
11757 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11758 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11759 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11761 op0 = XEXP (XEXP (op0, 0), 0);
11762 code = (code == LT ? EQ : NE);
11763 continue;
11765 break;
11767 case MINUS:
11768 /* We used to optimize signed comparisons against zero, but that
11769 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11770 arrive here as equality comparisons, or (GEU, LTU) are
11771 optimized away. No need to special-case them. */
11773 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11774 (eq B (minus A C)), whichever simplifies. We can only do
11775 this for equality comparisons due to pathological cases involving
11776 overflows. */
11777 if (equality_comparison_p
11778 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11779 XEXP (op0, 1), op1)))
11781 op0 = XEXP (op0, 0);
11782 op1 = tem;
11783 continue;
11786 if (equality_comparison_p
11787 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11788 XEXP (op0, 0), op1)))
11790 op0 = XEXP (op0, 1);
11791 op1 = tem;
11792 continue;
11795 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11796 of bits in X minus 1, is one iff X > 0. */
11797 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11798 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11799 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11800 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11802 op0 = XEXP (op0, 1);
11803 code = (code == GE ? LE : GT);
11804 continue;
11806 break;
11808 case XOR:
11809 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11810 if C is zero or B is a constant. */
11811 if (equality_comparison_p
11812 && 0 != (tem = simplify_binary_operation (XOR, mode,
11813 XEXP (op0, 1), op1)))
11815 op0 = XEXP (op0, 0);
11816 op1 = tem;
11817 continue;
11819 break;
11821 case EQ: case NE:
11822 case UNEQ: case LTGT:
11823 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11824 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11825 case UNORDERED: case ORDERED:
11826 /* We can't do anything if OP0 is a condition code value, rather
11827 than an actual data value. */
11828 if (const_op != 0
11829 || CC0_P (XEXP (op0, 0))
11830 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11831 break;
11833 /* Get the two operands being compared. */
11834 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11835 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11836 else
11837 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11839 /* Check for the cases where we simply want the result of the
11840 earlier test or the opposite of that result. */
11841 if (code == NE || code == EQ
11842 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11843 && (code == LT || code == GE)))
11845 enum rtx_code new_code;
11846 if (code == LT || code == NE)
11847 new_code = GET_CODE (op0);
11848 else
11849 new_code = reversed_comparison_code (op0, NULL);
11851 if (new_code != UNKNOWN)
11853 code = new_code;
11854 op0 = tem;
11855 op1 = tem1;
11856 continue;
11859 break;
11861 case IOR:
11862 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11863 iff X <= 0. */
11864 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11865 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11866 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11868 op0 = XEXP (op0, 1);
11869 code = (code == GE ? GT : LE);
11870 continue;
11872 break;
11874 case AND:
11875 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11876 will be converted to a ZERO_EXTRACT later. */
11877 if (const_op == 0 && equality_comparison_p
11878 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11879 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11881 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11882 XEXP (XEXP (op0, 0), 1));
11883 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11884 continue;
11887 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11888 zero and X is a comparison and C1 and C2 describe only bits set
11889 in STORE_FLAG_VALUE, we can compare with X. */
11890 if (const_op == 0 && equality_comparison_p
11891 && mode_width <= HOST_BITS_PER_WIDE_INT
11892 && CONST_INT_P (XEXP (op0, 1))
11893 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11894 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11895 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11896 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11898 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11899 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11900 if ((~STORE_FLAG_VALUE & mask) == 0
11901 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11902 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11903 && COMPARISON_P (tem))))
11905 op0 = XEXP (XEXP (op0, 0), 0);
11906 continue;
11910 /* If we are doing an equality comparison of an AND of a bit equal
11911 to the sign bit, replace this with a LT or GE comparison of
11912 the underlying value. */
11913 if (equality_comparison_p
11914 && const_op == 0
11915 && CONST_INT_P (XEXP (op0, 1))
11916 && mode_width <= HOST_BITS_PER_WIDE_INT
11917 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11918 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11920 op0 = XEXP (op0, 0);
11921 code = (code == EQ ? GE : LT);
11922 continue;
11925 /* If this AND operation is really a ZERO_EXTEND from a narrower
11926 mode, the constant fits within that mode, and this is either an
11927 equality or unsigned comparison, try to do this comparison in
11928 the narrower mode.
11930 Note that in:
11932 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11933 -> (ne:DI (reg:SI 4) (const_int 0))
11935 unless TRULY_NOOP_TRUNCATION allows it or the register is
11936 known to hold a value of the required mode the
11937 transformation is invalid. */
11938 if ((equality_comparison_p || unsigned_comparison_p)
11939 && CONST_INT_P (XEXP (op0, 1))
11940 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11941 & GET_MODE_MASK (mode))
11942 + 1)) >= 0
11943 && const_op >> i == 0
11944 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11945 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11946 || (REG_P (XEXP (op0, 0))
11947 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11949 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11950 continue;
11953 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11954 fits in both M1 and M2 and the SUBREG is either paradoxical
11955 or represents the low part, permute the SUBREG and the AND
11956 and try again. */
11957 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11959 unsigned HOST_WIDE_INT c1;
11960 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11961 /* Require an integral mode, to avoid creating something like
11962 (AND:SF ...). */
11963 if (SCALAR_INT_MODE_P (tmode)
11964 /* It is unsafe to commute the AND into the SUBREG if the
11965 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11966 not defined. As originally written the upper bits
11967 have a defined value due to the AND operation.
11968 However, if we commute the AND inside the SUBREG then
11969 they no longer have defined values and the meaning of
11970 the code has been changed. */
11971 && (0
11972 #ifdef WORD_REGISTER_OPERATIONS
11973 || (mode_width > GET_MODE_PRECISION (tmode)
11974 && mode_width <= BITS_PER_WORD)
11975 #endif
11976 || (mode_width <= GET_MODE_PRECISION (tmode)
11977 && subreg_lowpart_p (XEXP (op0, 0))))
11978 && CONST_INT_P (XEXP (op0, 1))
11979 && mode_width <= HOST_BITS_PER_WIDE_INT
11980 && HWI_COMPUTABLE_MODE_P (tmode)
11981 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11982 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11983 && c1 != mask
11984 && c1 != GET_MODE_MASK (tmode))
11986 op0 = simplify_gen_binary (AND, tmode,
11987 SUBREG_REG (XEXP (op0, 0)),
11988 gen_int_mode (c1, tmode));
11989 op0 = gen_lowpart (mode, op0);
11990 continue;
11994 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11995 if (const_op == 0 && equality_comparison_p
11996 && XEXP (op0, 1) == const1_rtx
11997 && GET_CODE (XEXP (op0, 0)) == NOT)
11999 op0 = simplify_and_const_int (NULL_RTX, mode,
12000 XEXP (XEXP (op0, 0), 0), 1);
12001 code = (code == NE ? EQ : NE);
12002 continue;
12005 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12006 (eq (and (lshiftrt X) 1) 0).
12007 Also handle the case where (not X) is expressed using xor. */
12008 if (const_op == 0 && equality_comparison_p
12009 && XEXP (op0, 1) == const1_rtx
12010 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12012 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12013 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12015 if (GET_CODE (shift_op) == NOT
12016 || (GET_CODE (shift_op) == XOR
12017 && CONST_INT_P (XEXP (shift_op, 1))
12018 && CONST_INT_P (shift_count)
12019 && HWI_COMPUTABLE_MODE_P (mode)
12020 && (UINTVAL (XEXP (shift_op, 1))
12021 == (unsigned HOST_WIDE_INT) 1
12022 << INTVAL (shift_count))))
12025 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12026 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12027 code = (code == NE ? EQ : NE);
12028 continue;
12031 break;
12033 case ASHIFT:
12034 /* If we have (compare (ashift FOO N) (const_int C)) and
12035 the high order N bits of FOO (N+1 if an inequality comparison)
12036 are known to be zero, we can do this by comparing FOO with C
12037 shifted right N bits so long as the low-order N bits of C are
12038 zero. */
12039 if (CONST_INT_P (XEXP (op0, 1))
12040 && INTVAL (XEXP (op0, 1)) >= 0
12041 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12042 < HOST_BITS_PER_WIDE_INT)
12043 && (((unsigned HOST_WIDE_INT) const_op
12044 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
12045 - 1)) == 0)
12046 && mode_width <= HOST_BITS_PER_WIDE_INT
12047 && (nonzero_bits (XEXP (op0, 0), mode)
12048 & ~(mask >> (INTVAL (XEXP (op0, 1))
12049 + ! equality_comparison_p))) == 0)
12051 /* We must perform a logical shift, not an arithmetic one,
12052 as we want the top N bits of C to be zero. */
12053 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12055 temp >>= INTVAL (XEXP (op0, 1));
12056 op1 = gen_int_mode (temp, mode);
12057 op0 = XEXP (op0, 0);
12058 continue;
12061 /* If we are doing a sign bit comparison, it means we are testing
12062 a particular bit. Convert it to the appropriate AND. */
12063 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12064 && mode_width <= HOST_BITS_PER_WIDE_INT)
12066 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12067 ((unsigned HOST_WIDE_INT) 1
12068 << (mode_width - 1
12069 - INTVAL (XEXP (op0, 1)))));
12070 code = (code == LT ? NE : EQ);
12071 continue;
12074 /* If this an equality comparison with zero and we are shifting
12075 the low bit to the sign bit, we can convert this to an AND of the
12076 low-order bit. */
12077 if (const_op == 0 && equality_comparison_p
12078 && CONST_INT_P (XEXP (op0, 1))
12079 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12081 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12082 continue;
12084 break;
12086 case ASHIFTRT:
12087 /* If this is an equality comparison with zero, we can do this
12088 as a logical shift, which might be much simpler. */
12089 if (equality_comparison_p && const_op == 0
12090 && CONST_INT_P (XEXP (op0, 1)))
12092 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12093 XEXP (op0, 0),
12094 INTVAL (XEXP (op0, 1)));
12095 continue;
12098 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12099 do the comparison in a narrower mode. */
12100 if (! unsigned_comparison_p
12101 && CONST_INT_P (XEXP (op0, 1))
12102 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12103 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12104 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12105 MODE_INT, 1)) != BLKmode
12106 && (((unsigned HOST_WIDE_INT) const_op
12107 + (GET_MODE_MASK (tmode) >> 1) + 1)
12108 <= GET_MODE_MASK (tmode)))
12110 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12111 continue;
12114 /* Likewise if OP0 is a PLUS of a sign extension with a
12115 constant, which is usually represented with the PLUS
12116 between the shifts. */
12117 if (! unsigned_comparison_p
12118 && CONST_INT_P (XEXP (op0, 1))
12119 && GET_CODE (XEXP (op0, 0)) == PLUS
12120 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12121 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12122 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12123 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12124 MODE_INT, 1)) != BLKmode
12125 && (((unsigned HOST_WIDE_INT) const_op
12126 + (GET_MODE_MASK (tmode) >> 1) + 1)
12127 <= GET_MODE_MASK (tmode)))
12129 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12130 rtx add_const = XEXP (XEXP (op0, 0), 1);
12131 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12132 add_const, XEXP (op0, 1));
12134 op0 = simplify_gen_binary (PLUS, tmode,
12135 gen_lowpart (tmode, inner),
12136 new_const);
12137 continue;
12140 /* ... fall through ... */
12141 case LSHIFTRT:
12142 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12143 the low order N bits of FOO are known to be zero, we can do this
12144 by comparing FOO with C shifted left N bits so long as no
12145 overflow occurs. Even if the low order N bits of FOO aren't known
12146 to be zero, if the comparison is >= or < we can use the same
12147 optimization and for > or <= by setting all the low
12148 order N bits in the comparison constant. */
12149 if (CONST_INT_P (XEXP (op0, 1))
12150 && INTVAL (XEXP (op0, 1)) > 0
12151 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12152 && mode_width <= HOST_BITS_PER_WIDE_INT
12153 && (((unsigned HOST_WIDE_INT) const_op
12154 + (GET_CODE (op0) != LSHIFTRT
12155 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12156 + 1)
12157 : 0))
12158 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12160 unsigned HOST_WIDE_INT low_bits
12161 = (nonzero_bits (XEXP (op0, 0), mode)
12162 & (((unsigned HOST_WIDE_INT) 1
12163 << INTVAL (XEXP (op0, 1))) - 1));
12164 if (low_bits == 0 || !equality_comparison_p)
12166 /* If the shift was logical, then we must make the condition
12167 unsigned. */
12168 if (GET_CODE (op0) == LSHIFTRT)
12169 code = unsigned_condition (code);
12171 const_op <<= INTVAL (XEXP (op0, 1));
12172 if (low_bits != 0
12173 && (code == GT || code == GTU
12174 || code == LE || code == LEU))
12175 const_op
12176 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12177 op1 = GEN_INT (const_op);
12178 op0 = XEXP (op0, 0);
12179 continue;
12183 /* If we are using this shift to extract just the sign bit, we
12184 can replace this with an LT or GE comparison. */
12185 if (const_op == 0
12186 && (equality_comparison_p || sign_bit_comparison_p)
12187 && CONST_INT_P (XEXP (op0, 1))
12188 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12190 op0 = XEXP (op0, 0);
12191 code = (code == NE || code == GT ? LT : GE);
12192 continue;
12194 break;
12196 default:
12197 break;
12200 break;
12203 /* Now make any compound operations involved in this comparison. Then,
12204 check for an outmost SUBREG on OP0 that is not doing anything or is
12205 paradoxical. The latter transformation must only be performed when
12206 it is known that the "extra" bits will be the same in op0 and op1 or
12207 that they don't matter. There are three cases to consider:
12209 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12210 care bits and we can assume they have any convenient value. So
12211 making the transformation is safe.
12213 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12214 In this case the upper bits of op0 are undefined. We should not make
12215 the simplification in that case as we do not know the contents of
12216 those bits.
12218 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12219 UNKNOWN. In that case we know those bits are zeros or ones. We must
12220 also be sure that they are the same as the upper bits of op1.
12222 We can never remove a SUBREG for a non-equality comparison because
12223 the sign bit is in a different place in the underlying object. */
12225 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12226 op1 = make_compound_operation (op1, SET);
12228 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12229 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12230 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12231 && (code == NE || code == EQ))
12233 if (paradoxical_subreg_p (op0))
12235 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12236 implemented. */
12237 if (REG_P (SUBREG_REG (op0)))
12239 op0 = SUBREG_REG (op0);
12240 op1 = gen_lowpart (GET_MODE (op0), op1);
12243 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12244 <= HOST_BITS_PER_WIDE_INT)
12245 && (nonzero_bits (SUBREG_REG (op0),
12246 GET_MODE (SUBREG_REG (op0)))
12247 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12249 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12251 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12252 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12253 op0 = SUBREG_REG (op0), op1 = tem;
12257 /* We now do the opposite procedure: Some machines don't have compare
12258 insns in all modes. If OP0's mode is an integer mode smaller than a
12259 word and we can't do a compare in that mode, see if there is a larger
12260 mode for which we can do the compare. There are a number of cases in
12261 which we can use the wider mode. */
12263 mode = GET_MODE (op0);
12264 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12265 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12266 && ! have_insn_for (COMPARE, mode))
12267 for (tmode = GET_MODE_WIDER_MODE (mode);
12268 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12269 tmode = GET_MODE_WIDER_MODE (tmode))
12270 if (have_insn_for (COMPARE, tmode))
12272 int zero_extended;
12274 /* If this is a test for negative, we can make an explicit
12275 test of the sign bit. Test this first so we can use
12276 a paradoxical subreg to extend OP0. */
12278 if (op1 == const0_rtx && (code == LT || code == GE)
12279 && HWI_COMPUTABLE_MODE_P (mode))
12281 unsigned HOST_WIDE_INT sign
12282 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12283 op0 = simplify_gen_binary (AND, tmode,
12284 gen_lowpart (tmode, op0),
12285 gen_int_mode (sign, tmode));
12286 code = (code == LT) ? NE : EQ;
12287 break;
12290 /* If the only nonzero bits in OP0 and OP1 are those in the
12291 narrower mode and this is an equality or unsigned comparison,
12292 we can use the wider mode. Similarly for sign-extended
12293 values, in which case it is true for all comparisons. */
12294 zero_extended = ((code == EQ || code == NE
12295 || code == GEU || code == GTU
12296 || code == LEU || code == LTU)
12297 && (nonzero_bits (op0, tmode)
12298 & ~GET_MODE_MASK (mode)) == 0
12299 && ((CONST_INT_P (op1)
12300 || (nonzero_bits (op1, tmode)
12301 & ~GET_MODE_MASK (mode)) == 0)));
12303 if (zero_extended
12304 || ((num_sign_bit_copies (op0, tmode)
12305 > (unsigned int) (GET_MODE_PRECISION (tmode)
12306 - GET_MODE_PRECISION (mode)))
12307 && (num_sign_bit_copies (op1, tmode)
12308 > (unsigned int) (GET_MODE_PRECISION (tmode)
12309 - GET_MODE_PRECISION (mode)))))
12311 /* If OP0 is an AND and we don't have an AND in MODE either,
12312 make a new AND in the proper mode. */
12313 if (GET_CODE (op0) == AND
12314 && !have_insn_for (AND, mode))
12315 op0 = simplify_gen_binary (AND, tmode,
12316 gen_lowpart (tmode,
12317 XEXP (op0, 0)),
12318 gen_lowpart (tmode,
12319 XEXP (op0, 1)));
12320 else
12322 if (zero_extended)
12324 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12325 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12327 else
12329 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12330 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12332 break;
12337 /* We may have changed the comparison operands. Re-canonicalize. */
12338 if (swap_commutative_operands_p (op0, op1))
12340 std::swap (op0, op1);
12341 code = swap_condition (code);
12344 /* If this machine only supports a subset of valid comparisons, see if we
12345 can convert an unsupported one into a supported one. */
12346 target_canonicalize_comparison (&code, &op0, &op1, 0);
12348 *pop0 = op0;
12349 *pop1 = op1;
12351 return code;
12354 /* Utility function for record_value_for_reg. Count number of
12355 rtxs in X. */
12356 static int
12357 count_rtxs (rtx x)
12359 enum rtx_code code = GET_CODE (x);
12360 const char *fmt;
12361 int i, j, ret = 1;
12363 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12364 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12366 rtx x0 = XEXP (x, 0);
12367 rtx x1 = XEXP (x, 1);
12369 if (x0 == x1)
12370 return 1 + 2 * count_rtxs (x0);
12372 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12373 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12374 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12375 return 2 + 2 * count_rtxs (x0)
12376 + count_rtxs (x == XEXP (x1, 0)
12377 ? XEXP (x1, 1) : XEXP (x1, 0));
12379 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12380 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12381 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12382 return 2 + 2 * count_rtxs (x1)
12383 + count_rtxs (x == XEXP (x0, 0)
12384 ? XEXP (x0, 1) : XEXP (x0, 0));
12387 fmt = GET_RTX_FORMAT (code);
12388 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12389 if (fmt[i] == 'e')
12390 ret += count_rtxs (XEXP (x, i));
12391 else if (fmt[i] == 'E')
12392 for (j = 0; j < XVECLEN (x, i); j++)
12393 ret += count_rtxs (XVECEXP (x, i, j));
12395 return ret;
12398 /* Utility function for following routine. Called when X is part of a value
12399 being stored into last_set_value. Sets last_set_table_tick
12400 for each register mentioned. Similar to mention_regs in cse.c */
12402 static void
12403 update_table_tick (rtx x)
12405 enum rtx_code code = GET_CODE (x);
12406 const char *fmt = GET_RTX_FORMAT (code);
12407 int i, j;
12409 if (code == REG)
12411 unsigned int regno = REGNO (x);
12412 unsigned int endregno = END_REGNO (x);
12413 unsigned int r;
12415 for (r = regno; r < endregno; r++)
12417 reg_stat_type *rsp = &reg_stat[r];
12418 rsp->last_set_table_tick = label_tick;
12421 return;
12424 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12425 if (fmt[i] == 'e')
12427 /* Check for identical subexpressions. If x contains
12428 identical subexpression we only have to traverse one of
12429 them. */
12430 if (i == 0 && ARITHMETIC_P (x))
12432 /* Note that at this point x1 has already been
12433 processed. */
12434 rtx x0 = XEXP (x, 0);
12435 rtx x1 = XEXP (x, 1);
12437 /* If x0 and x1 are identical then there is no need to
12438 process x0. */
12439 if (x0 == x1)
12440 break;
12442 /* If x0 is identical to a subexpression of x1 then while
12443 processing x1, x0 has already been processed. Thus we
12444 are done with x. */
12445 if (ARITHMETIC_P (x1)
12446 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12447 break;
12449 /* If x1 is identical to a subexpression of x0 then we
12450 still have to process the rest of x0. */
12451 if (ARITHMETIC_P (x0)
12452 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12454 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12455 break;
12459 update_table_tick (XEXP (x, i));
12461 else if (fmt[i] == 'E')
12462 for (j = 0; j < XVECLEN (x, i); j++)
12463 update_table_tick (XVECEXP (x, i, j));
12466 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12467 are saying that the register is clobbered and we no longer know its
12468 value. If INSN is zero, don't update reg_stat[].last_set; this is
12469 only permitted with VALUE also zero and is used to invalidate the
12470 register. */
12472 static void
12473 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12475 unsigned int regno = REGNO (reg);
12476 unsigned int endregno = END_REGNO (reg);
12477 unsigned int i;
12478 reg_stat_type *rsp;
12480 /* If VALUE contains REG and we have a previous value for REG, substitute
12481 the previous value. */
12482 if (value && insn && reg_overlap_mentioned_p (reg, value))
12484 rtx tem;
12486 /* Set things up so get_last_value is allowed to see anything set up to
12487 our insn. */
12488 subst_low_luid = DF_INSN_LUID (insn);
12489 tem = get_last_value (reg);
12491 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12492 it isn't going to be useful and will take a lot of time to process,
12493 so just use the CLOBBER. */
12495 if (tem)
12497 if (ARITHMETIC_P (tem)
12498 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12499 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12500 tem = XEXP (tem, 0);
12501 else if (count_occurrences (value, reg, 1) >= 2)
12503 /* If there are two or more occurrences of REG in VALUE,
12504 prevent the value from growing too much. */
12505 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12506 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12509 value = replace_rtx (copy_rtx (value), reg, tem);
12513 /* For each register modified, show we don't know its value, that
12514 we don't know about its bitwise content, that its value has been
12515 updated, and that we don't know the location of the death of the
12516 register. */
12517 for (i = regno; i < endregno; i++)
12519 rsp = &reg_stat[i];
12521 if (insn)
12522 rsp->last_set = insn;
12524 rsp->last_set_value = 0;
12525 rsp->last_set_mode = VOIDmode;
12526 rsp->last_set_nonzero_bits = 0;
12527 rsp->last_set_sign_bit_copies = 0;
12528 rsp->last_death = 0;
12529 rsp->truncated_to_mode = VOIDmode;
12532 /* Mark registers that are being referenced in this value. */
12533 if (value)
12534 update_table_tick (value);
12536 /* Now update the status of each register being set.
12537 If someone is using this register in this block, set this register
12538 to invalid since we will get confused between the two lives in this
12539 basic block. This makes using this register always invalid. In cse, we
12540 scan the table to invalidate all entries using this register, but this
12541 is too much work for us. */
12543 for (i = regno; i < endregno; i++)
12545 rsp = &reg_stat[i];
12546 rsp->last_set_label = label_tick;
12547 if (!insn
12548 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12549 rsp->last_set_invalid = 1;
12550 else
12551 rsp->last_set_invalid = 0;
12554 /* The value being assigned might refer to X (like in "x++;"). In that
12555 case, we must replace it with (clobber (const_int 0)) to prevent
12556 infinite loops. */
12557 rsp = &reg_stat[regno];
12558 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12560 value = copy_rtx (value);
12561 if (!get_last_value_validate (&value, insn, label_tick, 1))
12562 value = 0;
12565 /* For the main register being modified, update the value, the mode, the
12566 nonzero bits, and the number of sign bit copies. */
12568 rsp->last_set_value = value;
12570 if (value)
12572 machine_mode mode = GET_MODE (reg);
12573 subst_low_luid = DF_INSN_LUID (insn);
12574 rsp->last_set_mode = mode;
12575 if (GET_MODE_CLASS (mode) == MODE_INT
12576 && HWI_COMPUTABLE_MODE_P (mode))
12577 mode = nonzero_bits_mode;
12578 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12579 rsp->last_set_sign_bit_copies
12580 = num_sign_bit_copies (value, GET_MODE (reg));
12584 /* Called via note_stores from record_dead_and_set_regs to handle one
12585 SET or CLOBBER in an insn. DATA is the instruction in which the
12586 set is occurring. */
12588 static void
12589 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12591 rtx_insn *record_dead_insn = (rtx_insn *) data;
12593 if (GET_CODE (dest) == SUBREG)
12594 dest = SUBREG_REG (dest);
12596 if (!record_dead_insn)
12598 if (REG_P (dest))
12599 record_value_for_reg (dest, NULL, NULL_RTX);
12600 return;
12603 if (REG_P (dest))
12605 /* If we are setting the whole register, we know its value. Otherwise
12606 show that we don't know the value. We can handle SUBREG in
12607 some cases. */
12608 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12609 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12610 else if (GET_CODE (setter) == SET
12611 && GET_CODE (SET_DEST (setter)) == SUBREG
12612 && SUBREG_REG (SET_DEST (setter)) == dest
12613 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12614 && subreg_lowpart_p (SET_DEST (setter)))
12615 record_value_for_reg (dest, record_dead_insn,
12616 gen_lowpart (GET_MODE (dest),
12617 SET_SRC (setter)));
12618 else
12619 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12621 else if (MEM_P (dest)
12622 /* Ignore pushes, they clobber nothing. */
12623 && ! push_operand (dest, GET_MODE (dest)))
12624 mem_last_set = DF_INSN_LUID (record_dead_insn);
12627 /* Update the records of when each REG was most recently set or killed
12628 for the things done by INSN. This is the last thing done in processing
12629 INSN in the combiner loop.
12631 We update reg_stat[], in particular fields last_set, last_set_value,
12632 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12633 last_death, and also the similar information mem_last_set (which insn
12634 most recently modified memory) and last_call_luid (which insn was the
12635 most recent subroutine call). */
12637 static void
12638 record_dead_and_set_regs (rtx_insn *insn)
12640 rtx link;
12641 unsigned int i;
12643 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12645 if (REG_NOTE_KIND (link) == REG_DEAD
12646 && REG_P (XEXP (link, 0)))
12648 unsigned int regno = REGNO (XEXP (link, 0));
12649 unsigned int endregno = END_REGNO (XEXP (link, 0));
12651 for (i = regno; i < endregno; i++)
12653 reg_stat_type *rsp;
12655 rsp = &reg_stat[i];
12656 rsp->last_death = insn;
12659 else if (REG_NOTE_KIND (link) == REG_INC)
12660 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12663 if (CALL_P (insn))
12665 hard_reg_set_iterator hrsi;
12666 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12668 reg_stat_type *rsp;
12670 rsp = &reg_stat[i];
12671 rsp->last_set_invalid = 1;
12672 rsp->last_set = insn;
12673 rsp->last_set_value = 0;
12674 rsp->last_set_mode = VOIDmode;
12675 rsp->last_set_nonzero_bits = 0;
12676 rsp->last_set_sign_bit_copies = 0;
12677 rsp->last_death = 0;
12678 rsp->truncated_to_mode = VOIDmode;
12681 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12683 /* We can't combine into a call pattern. Remember, though, that
12684 the return value register is set at this LUID. We could
12685 still replace a register with the return value from the
12686 wrong subroutine call! */
12687 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12689 else
12690 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12693 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12694 register present in the SUBREG, so for each such SUBREG go back and
12695 adjust nonzero and sign bit information of the registers that are
12696 known to have some zero/sign bits set.
12698 This is needed because when combine blows the SUBREGs away, the
12699 information on zero/sign bits is lost and further combines can be
12700 missed because of that. */
12702 static void
12703 record_promoted_value (rtx_insn *insn, rtx subreg)
12705 struct insn_link *links;
12706 rtx set;
12707 unsigned int regno = REGNO (SUBREG_REG (subreg));
12708 machine_mode mode = GET_MODE (subreg);
12710 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12711 return;
12713 for (links = LOG_LINKS (insn); links;)
12715 reg_stat_type *rsp;
12717 insn = links->insn;
12718 set = single_set (insn);
12720 if (! set || !REG_P (SET_DEST (set))
12721 || REGNO (SET_DEST (set)) != regno
12722 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12724 links = links->next;
12725 continue;
12728 rsp = &reg_stat[regno];
12729 if (rsp->last_set == insn)
12731 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12732 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12735 if (REG_P (SET_SRC (set)))
12737 regno = REGNO (SET_SRC (set));
12738 links = LOG_LINKS (insn);
12740 else
12741 break;
12745 /* Check if X, a register, is known to contain a value already
12746 truncated to MODE. In this case we can use a subreg to refer to
12747 the truncated value even though in the generic case we would need
12748 an explicit truncation. */
12750 static bool
12751 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12753 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12754 machine_mode truncated = rsp->truncated_to_mode;
12756 if (truncated == 0
12757 || rsp->truncation_label < label_tick_ebb_start)
12758 return false;
12759 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12760 return true;
12761 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12762 return true;
12763 return false;
12766 /* If X is a hard reg or a subreg record the mode that the register is
12767 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12768 to turn a truncate into a subreg using this information. Return true
12769 if traversing X is complete. */
12771 static bool
12772 record_truncated_value (rtx x)
12774 machine_mode truncated_mode;
12775 reg_stat_type *rsp;
12777 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12779 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12780 truncated_mode = GET_MODE (x);
12782 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12783 return true;
12785 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12786 return true;
12788 x = SUBREG_REG (x);
12790 /* ??? For hard-regs we now record everything. We might be able to
12791 optimize this using last_set_mode. */
12792 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12793 truncated_mode = GET_MODE (x);
12794 else
12795 return false;
12797 rsp = &reg_stat[REGNO (x)];
12798 if (rsp->truncated_to_mode == 0
12799 || rsp->truncation_label < label_tick_ebb_start
12800 || (GET_MODE_SIZE (truncated_mode)
12801 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12803 rsp->truncated_to_mode = truncated_mode;
12804 rsp->truncation_label = label_tick;
12807 return true;
12810 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12811 the modes they are used in. This can help truning TRUNCATEs into
12812 SUBREGs. */
12814 static void
12815 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12817 subrtx_var_iterator::array_type array;
12818 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12819 if (record_truncated_value (*iter))
12820 iter.skip_subrtxes ();
12823 /* Scan X for promoted SUBREGs. For each one found,
12824 note what it implies to the registers used in it. */
12826 static void
12827 check_promoted_subreg (rtx_insn *insn, rtx x)
12829 if (GET_CODE (x) == SUBREG
12830 && SUBREG_PROMOTED_VAR_P (x)
12831 && REG_P (SUBREG_REG (x)))
12832 record_promoted_value (insn, x);
12833 else
12835 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12836 int i, j;
12838 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12839 switch (format[i])
12841 case 'e':
12842 check_promoted_subreg (insn, XEXP (x, i));
12843 break;
12844 case 'V':
12845 case 'E':
12846 if (XVEC (x, i) != 0)
12847 for (j = 0; j < XVECLEN (x, i); j++)
12848 check_promoted_subreg (insn, XVECEXP (x, i, j));
12849 break;
12854 /* Verify that all the registers and memory references mentioned in *LOC are
12855 still valid. *LOC was part of a value set in INSN when label_tick was
12856 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12857 the invalid references with (clobber (const_int 0)) and return 1. This
12858 replacement is useful because we often can get useful information about
12859 the form of a value (e.g., if it was produced by a shift that always
12860 produces -1 or 0) even though we don't know exactly what registers it
12861 was produced from. */
12863 static int
12864 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12866 rtx x = *loc;
12867 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12868 int len = GET_RTX_LENGTH (GET_CODE (x));
12869 int i, j;
12871 if (REG_P (x))
12873 unsigned int regno = REGNO (x);
12874 unsigned int endregno = END_REGNO (x);
12875 unsigned int j;
12877 for (j = regno; j < endregno; j++)
12879 reg_stat_type *rsp = &reg_stat[j];
12880 if (rsp->last_set_invalid
12881 /* If this is a pseudo-register that was only set once and not
12882 live at the beginning of the function, it is always valid. */
12883 || (! (regno >= FIRST_PSEUDO_REGISTER
12884 && regno < reg_n_sets_max
12885 && REG_N_SETS (regno) == 1
12886 && (!REGNO_REG_SET_P
12887 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12888 regno)))
12889 && rsp->last_set_label > tick))
12891 if (replace)
12892 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12893 return replace;
12897 return 1;
12899 /* If this is a memory reference, make sure that there were no stores after
12900 it that might have clobbered the value. We don't have alias info, so we
12901 assume any store invalidates it. Moreover, we only have local UIDs, so
12902 we also assume that there were stores in the intervening basic blocks. */
12903 else if (MEM_P (x) && !MEM_READONLY_P (x)
12904 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12906 if (replace)
12907 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12908 return replace;
12911 for (i = 0; i < len; i++)
12913 if (fmt[i] == 'e')
12915 /* Check for identical subexpressions. If x contains
12916 identical subexpression we only have to traverse one of
12917 them. */
12918 if (i == 1 && ARITHMETIC_P (x))
12920 /* Note that at this point x0 has already been checked
12921 and found valid. */
12922 rtx x0 = XEXP (x, 0);
12923 rtx x1 = XEXP (x, 1);
12925 /* If x0 and x1 are identical then x is also valid. */
12926 if (x0 == x1)
12927 return 1;
12929 /* If x1 is identical to a subexpression of x0 then
12930 while checking x0, x1 has already been checked. Thus
12931 it is valid and so as x. */
12932 if (ARITHMETIC_P (x0)
12933 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12934 return 1;
12936 /* If x0 is identical to a subexpression of x1 then x is
12937 valid iff the rest of x1 is valid. */
12938 if (ARITHMETIC_P (x1)
12939 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12940 return
12941 get_last_value_validate (&XEXP (x1,
12942 x0 == XEXP (x1, 0) ? 1 : 0),
12943 insn, tick, replace);
12946 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12947 replace) == 0)
12948 return 0;
12950 else if (fmt[i] == 'E')
12951 for (j = 0; j < XVECLEN (x, i); j++)
12952 if (get_last_value_validate (&XVECEXP (x, i, j),
12953 insn, tick, replace) == 0)
12954 return 0;
12957 /* If we haven't found a reason for it to be invalid, it is valid. */
12958 return 1;
12961 /* Get the last value assigned to X, if known. Some registers
12962 in the value may be replaced with (clobber (const_int 0)) if their value
12963 is known longer known reliably. */
12965 static rtx
12966 get_last_value (const_rtx x)
12968 unsigned int regno;
12969 rtx value;
12970 reg_stat_type *rsp;
12972 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12973 then convert it to the desired mode. If this is a paradoxical SUBREG,
12974 we cannot predict what values the "extra" bits might have. */
12975 if (GET_CODE (x) == SUBREG
12976 && subreg_lowpart_p (x)
12977 && !paradoxical_subreg_p (x)
12978 && (value = get_last_value (SUBREG_REG (x))) != 0)
12979 return gen_lowpart (GET_MODE (x), value);
12981 if (!REG_P (x))
12982 return 0;
12984 regno = REGNO (x);
12985 rsp = &reg_stat[regno];
12986 value = rsp->last_set_value;
12988 /* If we don't have a value, or if it isn't for this basic block and
12989 it's either a hard register, set more than once, or it's a live
12990 at the beginning of the function, return 0.
12992 Because if it's not live at the beginning of the function then the reg
12993 is always set before being used (is never used without being set).
12994 And, if it's set only once, and it's always set before use, then all
12995 uses must have the same last value, even if it's not from this basic
12996 block. */
12998 if (value == 0
12999 || (rsp->last_set_label < label_tick_ebb_start
13000 && (regno < FIRST_PSEUDO_REGISTER
13001 || regno >= reg_n_sets_max
13002 || REG_N_SETS (regno) != 1
13003 || REGNO_REG_SET_P
13004 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13005 return 0;
13007 /* If the value was set in a later insn than the ones we are processing,
13008 we can't use it even if the register was only set once. */
13009 if (rsp->last_set_label == label_tick
13010 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13011 return 0;
13013 /* If the value has all its registers valid, return it. */
13014 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13015 return value;
13017 /* Otherwise, make a copy and replace any invalid register with
13018 (clobber (const_int 0)). If that fails for some reason, return 0. */
13020 value = copy_rtx (value);
13021 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13022 return value;
13024 return 0;
13027 /* Return nonzero if expression X refers to a REG or to memory
13028 that is set in an instruction more recent than FROM_LUID. */
13030 static int
13031 use_crosses_set_p (const_rtx x, int from_luid)
13033 const char *fmt;
13034 int i;
13035 enum rtx_code code = GET_CODE (x);
13037 if (code == REG)
13039 unsigned int regno = REGNO (x);
13040 unsigned endreg = END_REGNO (x);
13042 #ifdef PUSH_ROUNDING
13043 /* Don't allow uses of the stack pointer to be moved,
13044 because we don't know whether the move crosses a push insn. */
13045 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13046 return 1;
13047 #endif
13048 for (; regno < endreg; regno++)
13050 reg_stat_type *rsp = &reg_stat[regno];
13051 if (rsp->last_set
13052 && rsp->last_set_label == label_tick
13053 && DF_INSN_LUID (rsp->last_set) > from_luid)
13054 return 1;
13056 return 0;
13059 if (code == MEM && mem_last_set > from_luid)
13060 return 1;
13062 fmt = GET_RTX_FORMAT (code);
13064 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13066 if (fmt[i] == 'E')
13068 int j;
13069 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13070 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13071 return 1;
13073 else if (fmt[i] == 'e'
13074 && use_crosses_set_p (XEXP (x, i), from_luid))
13075 return 1;
13077 return 0;
13080 /* Define three variables used for communication between the following
13081 routines. */
13083 static unsigned int reg_dead_regno, reg_dead_endregno;
13084 static int reg_dead_flag;
13086 /* Function called via note_stores from reg_dead_at_p.
13088 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13089 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13091 static void
13092 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13094 unsigned int regno, endregno;
13096 if (!REG_P (dest))
13097 return;
13099 regno = REGNO (dest);
13100 endregno = END_REGNO (dest);
13101 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13102 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13105 /* Return nonzero if REG is known to be dead at INSN.
13107 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13108 referencing REG, it is dead. If we hit a SET referencing REG, it is
13109 live. Otherwise, see if it is live or dead at the start of the basic
13110 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13111 must be assumed to be always live. */
13113 static int
13114 reg_dead_at_p (rtx reg, rtx_insn *insn)
13116 basic_block block;
13117 unsigned int i;
13119 /* Set variables for reg_dead_at_p_1. */
13120 reg_dead_regno = REGNO (reg);
13121 reg_dead_endregno = END_REGNO (reg);
13123 reg_dead_flag = 0;
13125 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13126 we allow the machine description to decide whether use-and-clobber
13127 patterns are OK. */
13128 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13130 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13131 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13132 return 0;
13135 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13136 beginning of basic block. */
13137 block = BLOCK_FOR_INSN (insn);
13138 for (;;)
13140 if (INSN_P (insn))
13142 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13143 return 1;
13145 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13146 if (reg_dead_flag)
13147 return reg_dead_flag == 1 ? 1 : 0;
13149 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13150 return 1;
13153 if (insn == BB_HEAD (block))
13154 break;
13156 insn = PREV_INSN (insn);
13159 /* Look at live-in sets for the basic block that we were in. */
13160 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13161 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13162 return 0;
13164 return 1;
13167 /* Note hard registers in X that are used. */
13169 static void
13170 mark_used_regs_combine (rtx x)
13172 RTX_CODE code = GET_CODE (x);
13173 unsigned int regno;
13174 int i;
13176 switch (code)
13178 case LABEL_REF:
13179 case SYMBOL_REF:
13180 case CONST:
13181 CASE_CONST_ANY:
13182 case PC:
13183 case ADDR_VEC:
13184 case ADDR_DIFF_VEC:
13185 case ASM_INPUT:
13186 /* CC0 must die in the insn after it is set, so we don't need to take
13187 special note of it here. */
13188 case CC0:
13189 return;
13191 case CLOBBER:
13192 /* If we are clobbering a MEM, mark any hard registers inside the
13193 address as used. */
13194 if (MEM_P (XEXP (x, 0)))
13195 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13196 return;
13198 case REG:
13199 regno = REGNO (x);
13200 /* A hard reg in a wide mode may really be multiple registers.
13201 If so, mark all of them just like the first. */
13202 if (regno < FIRST_PSEUDO_REGISTER)
13204 /* None of this applies to the stack, frame or arg pointers. */
13205 if (regno == STACK_POINTER_REGNUM
13206 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13207 || regno == HARD_FRAME_POINTER_REGNUM
13208 #endif
13209 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13210 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13211 #endif
13212 || regno == FRAME_POINTER_REGNUM)
13213 return;
13215 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13217 return;
13219 case SET:
13221 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13222 the address. */
13223 rtx testreg = SET_DEST (x);
13225 while (GET_CODE (testreg) == SUBREG
13226 || GET_CODE (testreg) == ZERO_EXTRACT
13227 || GET_CODE (testreg) == STRICT_LOW_PART)
13228 testreg = XEXP (testreg, 0);
13230 if (MEM_P (testreg))
13231 mark_used_regs_combine (XEXP (testreg, 0));
13233 mark_used_regs_combine (SET_SRC (x));
13235 return;
13237 default:
13238 break;
13241 /* Recursively scan the operands of this expression. */
13244 const char *fmt = GET_RTX_FORMAT (code);
13246 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13248 if (fmt[i] == 'e')
13249 mark_used_regs_combine (XEXP (x, i));
13250 else if (fmt[i] == 'E')
13252 int j;
13254 for (j = 0; j < XVECLEN (x, i); j++)
13255 mark_used_regs_combine (XVECEXP (x, i, j));
13261 /* Remove register number REGNO from the dead registers list of INSN.
13263 Return the note used to record the death, if there was one. */
13266 remove_death (unsigned int regno, rtx_insn *insn)
13268 rtx note = find_regno_note (insn, REG_DEAD, regno);
13270 if (note)
13271 remove_note (insn, note);
13273 return note;
13276 /* For each register (hardware or pseudo) used within expression X, if its
13277 death is in an instruction with luid between FROM_LUID (inclusive) and
13278 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13279 list headed by PNOTES.
13281 That said, don't move registers killed by maybe_kill_insn.
13283 This is done when X is being merged by combination into TO_INSN. These
13284 notes will then be distributed as needed. */
13286 static void
13287 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13288 rtx *pnotes)
13290 const char *fmt;
13291 int len, i;
13292 enum rtx_code code = GET_CODE (x);
13294 if (code == REG)
13296 unsigned int regno = REGNO (x);
13297 rtx_insn *where_dead = reg_stat[regno].last_death;
13299 /* Don't move the register if it gets killed in between from and to. */
13300 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13301 && ! reg_referenced_p (x, maybe_kill_insn))
13302 return;
13304 if (where_dead
13305 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13306 && DF_INSN_LUID (where_dead) >= from_luid
13307 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13309 rtx note = remove_death (regno, where_dead);
13311 /* It is possible for the call above to return 0. This can occur
13312 when last_death points to I2 or I1 that we combined with.
13313 In that case make a new note.
13315 We must also check for the case where X is a hard register
13316 and NOTE is a death note for a range of hard registers
13317 including X. In that case, we must put REG_DEAD notes for
13318 the remaining registers in place of NOTE. */
13320 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13321 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13322 > GET_MODE_SIZE (GET_MODE (x))))
13324 unsigned int deadregno = REGNO (XEXP (note, 0));
13325 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13326 unsigned int ourend = END_HARD_REGNO (x);
13327 unsigned int i;
13329 for (i = deadregno; i < deadend; i++)
13330 if (i < regno || i >= ourend)
13331 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13334 /* If we didn't find any note, or if we found a REG_DEAD note that
13335 covers only part of the given reg, and we have a multi-reg hard
13336 register, then to be safe we must check for REG_DEAD notes
13337 for each register other than the first. They could have
13338 their own REG_DEAD notes lying around. */
13339 else if ((note == 0
13340 || (note != 0
13341 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13342 < GET_MODE_SIZE (GET_MODE (x)))))
13343 && regno < FIRST_PSEUDO_REGISTER
13344 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13346 unsigned int ourend = END_HARD_REGNO (x);
13347 unsigned int i, offset;
13348 rtx oldnotes = 0;
13350 if (note)
13351 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13352 else
13353 offset = 1;
13355 for (i = regno + offset; i < ourend; i++)
13356 move_deaths (regno_reg_rtx[i],
13357 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13360 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13362 XEXP (note, 1) = *pnotes;
13363 *pnotes = note;
13365 else
13366 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13369 return;
13372 else if (GET_CODE (x) == SET)
13374 rtx dest = SET_DEST (x);
13376 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13378 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13379 that accesses one word of a multi-word item, some
13380 piece of everything register in the expression is used by
13381 this insn, so remove any old death. */
13382 /* ??? So why do we test for equality of the sizes? */
13384 if (GET_CODE (dest) == ZERO_EXTRACT
13385 || GET_CODE (dest) == STRICT_LOW_PART
13386 || (GET_CODE (dest) == SUBREG
13387 && (((GET_MODE_SIZE (GET_MODE (dest))
13388 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13389 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13390 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13392 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13393 return;
13396 /* If this is some other SUBREG, we know it replaces the entire
13397 value, so use that as the destination. */
13398 if (GET_CODE (dest) == SUBREG)
13399 dest = SUBREG_REG (dest);
13401 /* If this is a MEM, adjust deaths of anything used in the address.
13402 For a REG (the only other possibility), the entire value is
13403 being replaced so the old value is not used in this insn. */
13405 if (MEM_P (dest))
13406 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13407 to_insn, pnotes);
13408 return;
13411 else if (GET_CODE (x) == CLOBBER)
13412 return;
13414 len = GET_RTX_LENGTH (code);
13415 fmt = GET_RTX_FORMAT (code);
13417 for (i = 0; i < len; i++)
13419 if (fmt[i] == 'E')
13421 int j;
13422 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13423 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13424 to_insn, pnotes);
13426 else if (fmt[i] == 'e')
13427 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13431 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13432 pattern of an insn. X must be a REG. */
13434 static int
13435 reg_bitfield_target_p (rtx x, rtx body)
13437 int i;
13439 if (GET_CODE (body) == SET)
13441 rtx dest = SET_DEST (body);
13442 rtx target;
13443 unsigned int regno, tregno, endregno, endtregno;
13445 if (GET_CODE (dest) == ZERO_EXTRACT)
13446 target = XEXP (dest, 0);
13447 else if (GET_CODE (dest) == STRICT_LOW_PART)
13448 target = SUBREG_REG (XEXP (dest, 0));
13449 else
13450 return 0;
13452 if (GET_CODE (target) == SUBREG)
13453 target = SUBREG_REG (target);
13455 if (!REG_P (target))
13456 return 0;
13458 tregno = REGNO (target), regno = REGNO (x);
13459 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13460 return target == x;
13462 endtregno = end_hard_regno (GET_MODE (target), tregno);
13463 endregno = end_hard_regno (GET_MODE (x), regno);
13465 return endregno > tregno && regno < endtregno;
13468 else if (GET_CODE (body) == PARALLEL)
13469 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13470 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13471 return 1;
13473 return 0;
13476 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13477 as appropriate. I3 and I2 are the insns resulting from the combination
13478 insns including FROM (I2 may be zero).
13480 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13481 not need REG_DEAD notes because they are being substituted for. This
13482 saves searching in the most common cases.
13484 Each note in the list is either ignored or placed on some insns, depending
13485 on the type of note. */
13487 static void
13488 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13489 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13491 rtx note, next_note;
13492 rtx tem_note;
13493 rtx_insn *tem_insn;
13495 for (note = notes; note; note = next_note)
13497 rtx_insn *place = 0, *place2 = 0;
13499 next_note = XEXP (note, 1);
13500 switch (REG_NOTE_KIND (note))
13502 case REG_BR_PROB:
13503 case REG_BR_PRED:
13504 /* Doesn't matter much where we put this, as long as it's somewhere.
13505 It is preferable to keep these notes on branches, which is most
13506 likely to be i3. */
13507 place = i3;
13508 break;
13510 case REG_NON_LOCAL_GOTO:
13511 if (JUMP_P (i3))
13512 place = i3;
13513 else
13515 gcc_assert (i2 && JUMP_P (i2));
13516 place = i2;
13518 break;
13520 case REG_EH_REGION:
13521 /* These notes must remain with the call or trapping instruction. */
13522 if (CALL_P (i3))
13523 place = i3;
13524 else if (i2 && CALL_P (i2))
13525 place = i2;
13526 else
13528 gcc_assert (cfun->can_throw_non_call_exceptions);
13529 if (may_trap_p (i3))
13530 place = i3;
13531 else if (i2 && may_trap_p (i2))
13532 place = i2;
13533 /* ??? Otherwise assume we've combined things such that we
13534 can now prove that the instructions can't trap. Drop the
13535 note in this case. */
13537 break;
13539 case REG_ARGS_SIZE:
13540 /* ??? How to distribute between i3-i1. Assume i3 contains the
13541 entire adjustment. Assert i3 contains at least some adjust. */
13542 if (!noop_move_p (i3))
13544 int old_size, args_size = INTVAL (XEXP (note, 0));
13545 /* fixup_args_size_notes looks at REG_NORETURN note,
13546 so ensure the note is placed there first. */
13547 if (CALL_P (i3))
13549 rtx *np;
13550 for (np = &next_note; *np; np = &XEXP (*np, 1))
13551 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13553 rtx n = *np;
13554 *np = XEXP (n, 1);
13555 XEXP (n, 1) = REG_NOTES (i3);
13556 REG_NOTES (i3) = n;
13557 break;
13560 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13561 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13562 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13563 gcc_assert (old_size != args_size
13564 || (CALL_P (i3)
13565 && !ACCUMULATE_OUTGOING_ARGS
13566 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13568 break;
13570 case REG_NORETURN:
13571 case REG_SETJMP:
13572 case REG_TM:
13573 case REG_CALL_DECL:
13574 /* These notes must remain with the call. It should not be
13575 possible for both I2 and I3 to be a call. */
13576 if (CALL_P (i3))
13577 place = i3;
13578 else
13580 gcc_assert (i2 && CALL_P (i2));
13581 place = i2;
13583 break;
13585 case REG_UNUSED:
13586 /* Any clobbers for i3 may still exist, and so we must process
13587 REG_UNUSED notes from that insn.
13589 Any clobbers from i2 or i1 can only exist if they were added by
13590 recog_for_combine. In that case, recog_for_combine created the
13591 necessary REG_UNUSED notes. Trying to keep any original
13592 REG_UNUSED notes from these insns can cause incorrect output
13593 if it is for the same register as the original i3 dest.
13594 In that case, we will notice that the register is set in i3,
13595 and then add a REG_UNUSED note for the destination of i3, which
13596 is wrong. However, it is possible to have REG_UNUSED notes from
13597 i2 or i1 for register which were both used and clobbered, so
13598 we keep notes from i2 or i1 if they will turn into REG_DEAD
13599 notes. */
13601 /* If this register is set or clobbered in I3, put the note there
13602 unless there is one already. */
13603 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13605 if (from_insn != i3)
13606 break;
13608 if (! (REG_P (XEXP (note, 0))
13609 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13610 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13611 place = i3;
13613 /* Otherwise, if this register is used by I3, then this register
13614 now dies here, so we must put a REG_DEAD note here unless there
13615 is one already. */
13616 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13617 && ! (REG_P (XEXP (note, 0))
13618 ? find_regno_note (i3, REG_DEAD,
13619 REGNO (XEXP (note, 0)))
13620 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13622 PUT_REG_NOTE_KIND (note, REG_DEAD);
13623 place = i3;
13625 break;
13627 case REG_EQUAL:
13628 case REG_EQUIV:
13629 case REG_NOALIAS:
13630 /* These notes say something about results of an insn. We can
13631 only support them if they used to be on I3 in which case they
13632 remain on I3. Otherwise they are ignored.
13634 If the note refers to an expression that is not a constant, we
13635 must also ignore the note since we cannot tell whether the
13636 equivalence is still true. It might be possible to do
13637 slightly better than this (we only have a problem if I2DEST
13638 or I1DEST is present in the expression), but it doesn't
13639 seem worth the trouble. */
13641 if (from_insn == i3
13642 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13643 place = i3;
13644 break;
13646 case REG_INC:
13647 /* These notes say something about how a register is used. They must
13648 be present on any use of the register in I2 or I3. */
13649 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13650 place = i3;
13652 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13654 if (place)
13655 place2 = i2;
13656 else
13657 place = i2;
13659 break;
13661 case REG_LABEL_TARGET:
13662 case REG_LABEL_OPERAND:
13663 /* This can show up in several ways -- either directly in the
13664 pattern, or hidden off in the constant pool with (or without?)
13665 a REG_EQUAL note. */
13666 /* ??? Ignore the without-reg_equal-note problem for now. */
13667 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13668 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13669 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13670 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13671 place = i3;
13673 if (i2
13674 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13675 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13676 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13677 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13679 if (place)
13680 place2 = i2;
13681 else
13682 place = i2;
13685 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13686 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13687 there. */
13688 if (place && JUMP_P (place)
13689 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13690 && (JUMP_LABEL (place) == NULL
13691 || JUMP_LABEL (place) == XEXP (note, 0)))
13693 rtx label = JUMP_LABEL (place);
13695 if (!label)
13696 JUMP_LABEL (place) = XEXP (note, 0);
13697 else if (LABEL_P (label))
13698 LABEL_NUSES (label)--;
13701 if (place2 && JUMP_P (place2)
13702 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13703 && (JUMP_LABEL (place2) == NULL
13704 || JUMP_LABEL (place2) == XEXP (note, 0)))
13706 rtx label = JUMP_LABEL (place2);
13708 if (!label)
13709 JUMP_LABEL (place2) = XEXP (note, 0);
13710 else if (LABEL_P (label))
13711 LABEL_NUSES (label)--;
13712 place2 = 0;
13714 break;
13716 case REG_NONNEG:
13717 /* This note says something about the value of a register prior
13718 to the execution of an insn. It is too much trouble to see
13719 if the note is still correct in all situations. It is better
13720 to simply delete it. */
13721 break;
13723 case REG_DEAD:
13724 /* If we replaced the right hand side of FROM_INSN with a
13725 REG_EQUAL note, the original use of the dying register
13726 will not have been combined into I3 and I2. In such cases,
13727 FROM_INSN is guaranteed to be the first of the combined
13728 instructions, so we simply need to search back before
13729 FROM_INSN for the previous use or set of this register,
13730 then alter the notes there appropriately.
13732 If the register is used as an input in I3, it dies there.
13733 Similarly for I2, if it is nonzero and adjacent to I3.
13735 If the register is not used as an input in either I3 or I2
13736 and it is not one of the registers we were supposed to eliminate,
13737 there are two possibilities. We might have a non-adjacent I2
13738 or we might have somehow eliminated an additional register
13739 from a computation. For example, we might have had A & B where
13740 we discover that B will always be zero. In this case we will
13741 eliminate the reference to A.
13743 In both cases, we must search to see if we can find a previous
13744 use of A and put the death note there. */
13746 if (from_insn
13747 && from_insn == i2mod
13748 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13749 tem_insn = from_insn;
13750 else
13752 if (from_insn
13753 && CALL_P (from_insn)
13754 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13755 place = from_insn;
13756 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13757 place = i3;
13758 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13759 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13760 place = i2;
13761 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13762 && !(i2mod
13763 && reg_overlap_mentioned_p (XEXP (note, 0),
13764 i2mod_old_rhs)))
13765 || rtx_equal_p (XEXP (note, 0), elim_i1)
13766 || rtx_equal_p (XEXP (note, 0), elim_i0))
13767 break;
13768 tem_insn = i3;
13769 /* If the new I2 sets the same register that is marked dead
13770 in the note, the note now should not be put on I2, as the
13771 note refers to a previous incarnation of the reg. */
13772 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
13773 tem_insn = i2;
13776 if (place == 0)
13778 basic_block bb = this_basic_block;
13780 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13782 if (!NONDEBUG_INSN_P (tem_insn))
13784 if (tem_insn == BB_HEAD (bb))
13785 break;
13786 continue;
13789 /* If the register is being set at TEM_INSN, see if that is all
13790 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13791 into a REG_UNUSED note instead. Don't delete sets to
13792 global register vars. */
13793 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13794 || !global_regs[REGNO (XEXP (note, 0))])
13795 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13797 rtx set = single_set (tem_insn);
13798 rtx inner_dest = 0;
13799 rtx_insn *cc0_setter = NULL;
13801 if (set != 0)
13802 for (inner_dest = SET_DEST (set);
13803 (GET_CODE (inner_dest) == STRICT_LOW_PART
13804 || GET_CODE (inner_dest) == SUBREG
13805 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13806 inner_dest = XEXP (inner_dest, 0))
13809 /* Verify that it was the set, and not a clobber that
13810 modified the register.
13812 CC0 targets must be careful to maintain setter/user
13813 pairs. If we cannot delete the setter due to side
13814 effects, mark the user with an UNUSED note instead
13815 of deleting it. */
13817 if (set != 0 && ! side_effects_p (SET_SRC (set))
13818 && rtx_equal_p (XEXP (note, 0), inner_dest)
13819 #if HAVE_cc0
13820 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13821 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13822 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13823 #endif
13826 /* Move the notes and links of TEM_INSN elsewhere.
13827 This might delete other dead insns recursively.
13828 First set the pattern to something that won't use
13829 any register. */
13830 rtx old_notes = REG_NOTES (tem_insn);
13832 PATTERN (tem_insn) = pc_rtx;
13833 REG_NOTES (tem_insn) = NULL;
13835 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13836 NULL_RTX, NULL_RTX, NULL_RTX);
13837 distribute_links (LOG_LINKS (tem_insn));
13839 SET_INSN_DELETED (tem_insn);
13840 if (tem_insn == i2)
13841 i2 = NULL;
13843 /* Delete the setter too. */
13844 if (cc0_setter)
13846 PATTERN (cc0_setter) = pc_rtx;
13847 old_notes = REG_NOTES (cc0_setter);
13848 REG_NOTES (cc0_setter) = NULL;
13850 distribute_notes (old_notes, cc0_setter,
13851 cc0_setter, NULL,
13852 NULL_RTX, NULL_RTX, NULL_RTX);
13853 distribute_links (LOG_LINKS (cc0_setter));
13855 SET_INSN_DELETED (cc0_setter);
13856 if (cc0_setter == i2)
13857 i2 = NULL;
13860 else
13862 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13864 /* If there isn't already a REG_UNUSED note, put one
13865 here. Do not place a REG_DEAD note, even if
13866 the register is also used here; that would not
13867 match the algorithm used in lifetime analysis
13868 and can cause the consistency check in the
13869 scheduler to fail. */
13870 if (! find_regno_note (tem_insn, REG_UNUSED,
13871 REGNO (XEXP (note, 0))))
13872 place = tem_insn;
13873 break;
13876 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13877 || (CALL_P (tem_insn)
13878 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13880 place = tem_insn;
13882 /* If we are doing a 3->2 combination, and we have a
13883 register which formerly died in i3 and was not used
13884 by i2, which now no longer dies in i3 and is used in
13885 i2 but does not die in i2, and place is between i2
13886 and i3, then we may need to move a link from place to
13887 i2. */
13888 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13889 && from_insn
13890 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13891 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13893 struct insn_link *links = LOG_LINKS (place);
13894 LOG_LINKS (place) = NULL;
13895 distribute_links (links);
13897 break;
13900 if (tem_insn == BB_HEAD (bb))
13901 break;
13906 /* If the register is set or already dead at PLACE, we needn't do
13907 anything with this note if it is still a REG_DEAD note.
13908 We check here if it is set at all, not if is it totally replaced,
13909 which is what `dead_or_set_p' checks, so also check for it being
13910 set partially. */
13912 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13914 unsigned int regno = REGNO (XEXP (note, 0));
13915 reg_stat_type *rsp = &reg_stat[regno];
13917 if (dead_or_set_p (place, XEXP (note, 0))
13918 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13920 /* Unless the register previously died in PLACE, clear
13921 last_death. [I no longer understand why this is
13922 being done.] */
13923 if (rsp->last_death != place)
13924 rsp->last_death = 0;
13925 place = 0;
13927 else
13928 rsp->last_death = place;
13930 /* If this is a death note for a hard reg that is occupying
13931 multiple registers, ensure that we are still using all
13932 parts of the object. If we find a piece of the object
13933 that is unused, we must arrange for an appropriate REG_DEAD
13934 note to be added for it. However, we can't just emit a USE
13935 and tag the note to it, since the register might actually
13936 be dead; so we recourse, and the recursive call then finds
13937 the previous insn that used this register. */
13939 if (place && regno < FIRST_PSEUDO_REGISTER
13940 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13942 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13943 bool all_used = true;
13944 unsigned int i;
13946 for (i = regno; i < endregno; i++)
13947 if ((! refers_to_regno_p (i, PATTERN (place))
13948 && ! find_regno_fusage (place, USE, i))
13949 || dead_or_set_regno_p (place, i))
13951 all_used = false;
13952 break;
13955 if (! all_used)
13957 /* Put only REG_DEAD notes for pieces that are
13958 not already dead or set. */
13960 for (i = regno; i < endregno;
13961 i += hard_regno_nregs[i][reg_raw_mode[i]])
13963 rtx piece = regno_reg_rtx[i];
13964 basic_block bb = this_basic_block;
13966 if (! dead_or_set_p (place, piece)
13967 && ! reg_bitfield_target_p (piece,
13968 PATTERN (place)))
13970 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13971 NULL_RTX);
13973 distribute_notes (new_note, place, place,
13974 NULL, NULL_RTX, NULL_RTX,
13975 NULL_RTX);
13977 else if (! refers_to_regno_p (i, PATTERN (place))
13978 && ! find_regno_fusage (place, USE, i))
13979 for (tem_insn = PREV_INSN (place); ;
13980 tem_insn = PREV_INSN (tem_insn))
13982 if (!NONDEBUG_INSN_P (tem_insn))
13984 if (tem_insn == BB_HEAD (bb))
13985 break;
13986 continue;
13988 if (dead_or_set_p (tem_insn, piece)
13989 || reg_bitfield_target_p (piece,
13990 PATTERN (tem_insn)))
13992 add_reg_note (tem_insn, REG_UNUSED, piece);
13993 break;
13998 place = 0;
14002 break;
14004 default:
14005 /* Any other notes should not be present at this point in the
14006 compilation. */
14007 gcc_unreachable ();
14010 if (place)
14012 XEXP (note, 1) = REG_NOTES (place);
14013 REG_NOTES (place) = note;
14016 if (place2)
14017 add_shallow_copy_of_reg_note (place2, note);
14021 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14022 I3, I2, and I1 to new locations. This is also called to add a link
14023 pointing at I3 when I3's destination is changed. */
14025 static void
14026 distribute_links (struct insn_link *links)
14028 struct insn_link *link, *next_link;
14030 for (link = links; link; link = next_link)
14032 rtx_insn *place = 0;
14033 rtx_insn *insn;
14034 rtx set, reg;
14036 next_link = link->next;
14038 /* If the insn that this link points to is a NOTE, ignore it. */
14039 if (NOTE_P (link->insn))
14040 continue;
14042 set = 0;
14043 rtx pat = PATTERN (link->insn);
14044 if (GET_CODE (pat) == SET)
14045 set = pat;
14046 else if (GET_CODE (pat) == PARALLEL)
14048 int i;
14049 for (i = 0; i < XVECLEN (pat, 0); i++)
14051 set = XVECEXP (pat, 0, i);
14052 if (GET_CODE (set) != SET)
14053 continue;
14055 reg = SET_DEST (set);
14056 while (GET_CODE (reg) == ZERO_EXTRACT
14057 || GET_CODE (reg) == STRICT_LOW_PART
14058 || GET_CODE (reg) == SUBREG)
14059 reg = XEXP (reg, 0);
14061 if (!REG_P (reg))
14062 continue;
14064 if (REGNO (reg) == link->regno)
14065 break;
14067 if (i == XVECLEN (pat, 0))
14068 continue;
14070 else
14071 continue;
14073 reg = SET_DEST (set);
14075 while (GET_CODE (reg) == ZERO_EXTRACT
14076 || GET_CODE (reg) == STRICT_LOW_PART
14077 || GET_CODE (reg) == SUBREG)
14078 reg = XEXP (reg, 0);
14080 /* A LOG_LINK is defined as being placed on the first insn that uses
14081 a register and points to the insn that sets the register. Start
14082 searching at the next insn after the target of the link and stop
14083 when we reach a set of the register or the end of the basic block.
14085 Note that this correctly handles the link that used to point from
14086 I3 to I2. Also note that not much searching is typically done here
14087 since most links don't point very far away. */
14089 for (insn = NEXT_INSN (link->insn);
14090 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14091 || BB_HEAD (this_basic_block->next_bb) != insn));
14092 insn = NEXT_INSN (insn))
14093 if (DEBUG_INSN_P (insn))
14094 continue;
14095 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14097 if (reg_referenced_p (reg, PATTERN (insn)))
14098 place = insn;
14099 break;
14101 else if (CALL_P (insn)
14102 && find_reg_fusage (insn, USE, reg))
14104 place = insn;
14105 break;
14107 else if (INSN_P (insn) && reg_set_p (reg, insn))
14108 break;
14110 /* If we found a place to put the link, place it there unless there
14111 is already a link to the same insn as LINK at that point. */
14113 if (place)
14115 struct insn_link *link2;
14117 FOR_EACH_LOG_LINK (link2, place)
14118 if (link2->insn == link->insn && link2->regno == link->regno)
14119 break;
14121 if (link2 == NULL)
14123 link->next = LOG_LINKS (place);
14124 LOG_LINKS (place) = link;
14126 /* Set added_links_insn to the earliest insn we added a
14127 link to. */
14128 if (added_links_insn == 0
14129 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14130 added_links_insn = place;
14136 /* Check for any register or memory mentioned in EQUIV that is not
14137 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14138 of EXPR where some registers may have been replaced by constants. */
14140 static bool
14141 unmentioned_reg_p (rtx equiv, rtx expr)
14143 subrtx_iterator::array_type array;
14144 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14146 const_rtx x = *iter;
14147 if ((REG_P (x) || MEM_P (x))
14148 && !reg_mentioned_p (x, expr))
14149 return true;
14151 return false;
14154 DEBUG_FUNCTION void
14155 dump_combine_stats (FILE *file)
14157 fprintf
14158 (file,
14159 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14160 combine_attempts, combine_merges, combine_extras, combine_successes);
14163 void
14164 dump_combine_total_stats (FILE *file)
14166 fprintf
14167 (file,
14168 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14169 total_attempts, total_merges, total_extras, total_successes);
14172 /* Try combining insns through substitution. */
14173 static unsigned int
14174 rest_of_handle_combine (void)
14176 int rebuild_jump_labels_after_combine;
14178 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14179 df_note_add_problem ();
14180 df_analyze ();
14182 regstat_init_n_sets_and_refs ();
14183 reg_n_sets_max = max_reg_num ();
14185 rebuild_jump_labels_after_combine
14186 = combine_instructions (get_insns (), max_reg_num ());
14188 /* Combining insns may have turned an indirect jump into a
14189 direct jump. Rebuild the JUMP_LABEL fields of jumping
14190 instructions. */
14191 if (rebuild_jump_labels_after_combine)
14193 timevar_push (TV_JUMP);
14194 rebuild_jump_labels (get_insns ());
14195 cleanup_cfg (0);
14196 timevar_pop (TV_JUMP);
14199 regstat_free_n_sets_and_refs ();
14200 return 0;
14203 namespace {
14205 const pass_data pass_data_combine =
14207 RTL_PASS, /* type */
14208 "combine", /* name */
14209 OPTGROUP_NONE, /* optinfo_flags */
14210 TV_COMBINE, /* tv_id */
14211 PROP_cfglayout, /* properties_required */
14212 0, /* properties_provided */
14213 0, /* properties_destroyed */
14214 0, /* todo_flags_start */
14215 TODO_df_finish, /* todo_flags_finish */
14218 class pass_combine : public rtl_opt_pass
14220 public:
14221 pass_combine (gcc::context *ctxt)
14222 : rtl_opt_pass (pass_data_combine, ctxt)
14225 /* opt_pass methods: */
14226 virtual bool gate (function *) { return (optimize > 0); }
14227 virtual unsigned int execute (function *)
14229 return rest_of_handle_combine ();
14232 }; // class pass_combine
14234 } // anon namespace
14236 rtl_opt_pass *
14237 make_pass_combine (gcc::context *ctxt)
14239 return new pass_combine (ctxt);