re PR libfortran/59513 (Fortran runtime error: Sequential READ or WRITE not allowed...
[official-gcc.git] / gcc / combine.c
blob71e5690459d5e61f052c02920ab7c9f2d38b2dc4
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 #ifdef HAVE_cc0
690 if (dest == cc0_rtx)
692 next = NEXT_INSN (insn);
693 if (next == 0
694 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
695 return 0;
697 result = find_single_use_1 (dest, &PATTERN (next));
698 if (result && ploc)
699 *ploc = next;
700 return result;
702 #endif
704 if (!REG_P (dest))
705 return 0;
707 bb = BLOCK_FOR_INSN (insn);
708 for (next = NEXT_INSN (insn);
709 next && BLOCK_FOR_INSN (next) == bb;
710 next = NEXT_INSN (next))
711 if (INSN_P (next) && dead_or_set_p (next, dest))
713 FOR_EACH_LOG_LINK (link, next)
714 if (link->insn == insn && link->regno == REGNO (dest))
715 break;
717 if (link)
719 result = find_single_use_1 (dest, &PATTERN (next));
720 if (ploc)
721 *ploc = next;
722 return result;
726 return 0;
729 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
730 insn. The substitution can be undone by undo_all. If INTO is already
731 set to NEWVAL, do not record this change. Because computing NEWVAL might
732 also call SUBST, we have to compute it before we put anything into
733 the undo table. */
735 static void
736 do_SUBST (rtx *into, rtx newval)
738 struct undo *buf;
739 rtx oldval = *into;
741 if (oldval == newval)
742 return;
744 /* We'd like to catch as many invalid transformations here as
745 possible. Unfortunately, there are way too many mode changes
746 that are perfectly valid, so we'd waste too much effort for
747 little gain doing the checks here. Focus on catching invalid
748 transformations involving integer constants. */
749 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
750 && CONST_INT_P (newval))
752 /* Sanity check that we're replacing oldval with a CONST_INT
753 that is a valid sign-extension for the original mode. */
754 gcc_assert (INTVAL (newval)
755 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
757 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
758 CONST_INT is not valid, because after the replacement, the
759 original mode would be gone. Unfortunately, we can't tell
760 when do_SUBST is called to replace the operand thereof, so we
761 perform this test on oldval instead, checking whether an
762 invalid replacement took place before we got here. */
763 gcc_assert (!(GET_CODE (oldval) == SUBREG
764 && CONST_INT_P (SUBREG_REG (oldval))));
765 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
766 && CONST_INT_P (XEXP (oldval, 0))));
769 if (undobuf.frees)
770 buf = undobuf.frees, undobuf.frees = buf->next;
771 else
772 buf = XNEW (struct undo);
774 buf->kind = UNDO_RTX;
775 buf->where.r = into;
776 buf->old_contents.r = oldval;
777 *into = newval;
779 buf->next = undobuf.undos, undobuf.undos = buf;
782 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
784 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
785 for the value of a HOST_WIDE_INT value (including CONST_INT) is
786 not safe. */
788 static void
789 do_SUBST_INT (int *into, int newval)
791 struct undo *buf;
792 int oldval = *into;
794 if (oldval == newval)
795 return;
797 if (undobuf.frees)
798 buf = undobuf.frees, undobuf.frees = buf->next;
799 else
800 buf = XNEW (struct undo);
802 buf->kind = UNDO_INT;
803 buf->where.i = into;
804 buf->old_contents.i = oldval;
805 *into = newval;
807 buf->next = undobuf.undos, undobuf.undos = buf;
810 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
812 /* Similar to SUBST, but just substitute the mode. This is used when
813 changing the mode of a pseudo-register, so that any other
814 references to the entry in the regno_reg_rtx array will change as
815 well. */
817 static void
818 do_SUBST_MODE (rtx *into, machine_mode newval)
820 struct undo *buf;
821 machine_mode oldval = GET_MODE (*into);
823 if (oldval == newval)
824 return;
826 if (undobuf.frees)
827 buf = undobuf.frees, undobuf.frees = buf->next;
828 else
829 buf = XNEW (struct undo);
831 buf->kind = UNDO_MODE;
832 buf->where.r = into;
833 buf->old_contents.m = oldval;
834 adjust_reg_mode (*into, newval);
836 buf->next = undobuf.undos, undobuf.undos = buf;
839 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
841 #ifndef HAVE_cc0
842 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
844 static void
845 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
847 struct undo *buf;
848 struct insn_link * oldval = *into;
850 if (oldval == newval)
851 return;
853 if (undobuf.frees)
854 buf = undobuf.frees, undobuf.frees = buf->next;
855 else
856 buf = XNEW (struct undo);
858 buf->kind = UNDO_LINKS;
859 buf->where.l = into;
860 buf->old_contents.l = oldval;
861 *into = newval;
863 buf->next = undobuf.undos, undobuf.undos = buf;
866 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
867 #endif
869 /* Subroutine of try_combine. Determine whether the replacement patterns
870 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
871 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
872 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
873 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
874 of all the instructions can be estimated and the replacements are more
875 expensive than the original sequence. */
877 static bool
878 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
879 rtx newpat, rtx newi2pat, rtx newotherpat)
881 int i0_cost, i1_cost, i2_cost, i3_cost;
882 int new_i2_cost, new_i3_cost;
883 int old_cost, new_cost;
885 /* Lookup the original insn_rtx_costs. */
886 i2_cost = INSN_COST (i2);
887 i3_cost = INSN_COST (i3);
889 if (i1)
891 i1_cost = INSN_COST (i1);
892 if (i0)
894 i0_cost = INSN_COST (i0);
895 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
896 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
898 else
900 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
901 ? i1_cost + i2_cost + i3_cost : 0);
902 i0_cost = 0;
905 else
907 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
908 i1_cost = i0_cost = 0;
911 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
912 correct that. */
913 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
914 old_cost -= i1_cost;
917 /* Calculate the replacement insn_rtx_costs. */
918 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
919 if (newi2pat)
921 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
922 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
923 ? new_i2_cost + new_i3_cost : 0;
925 else
927 new_cost = new_i3_cost;
928 new_i2_cost = 0;
931 if (undobuf.other_insn)
933 int old_other_cost, new_other_cost;
935 old_other_cost = INSN_COST (undobuf.other_insn);
936 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
937 if (old_other_cost > 0 && new_other_cost > 0)
939 old_cost += old_other_cost;
940 new_cost += new_other_cost;
942 else
943 old_cost = 0;
946 /* Disallow this combination if both new_cost and old_cost are greater than
947 zero, and new_cost is greater than old cost. */
948 int reject = old_cost > 0 && new_cost > old_cost;
950 if (dump_file)
952 fprintf (dump_file, "%s combination of insns ",
953 reject ? "rejecting" : "allowing");
954 if (i0)
955 fprintf (dump_file, "%d, ", INSN_UID (i0));
956 if (i1 && INSN_UID (i1) != INSN_UID (i2))
957 fprintf (dump_file, "%d, ", INSN_UID (i1));
958 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
960 fprintf (dump_file, "original costs ");
961 if (i0)
962 fprintf (dump_file, "%d + ", i0_cost);
963 if (i1 && INSN_UID (i1) != INSN_UID (i2))
964 fprintf (dump_file, "%d + ", i1_cost);
965 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
967 if (newi2pat)
968 fprintf (dump_file, "replacement costs %d + %d = %d\n",
969 new_i2_cost, new_i3_cost, new_cost);
970 else
971 fprintf (dump_file, "replacement cost %d\n", new_cost);
974 if (reject)
975 return false;
977 /* Update the uid_insn_cost array with the replacement costs. */
978 INSN_COST (i2) = new_i2_cost;
979 INSN_COST (i3) = new_i3_cost;
980 if (i1)
982 INSN_COST (i1) = 0;
983 if (i0)
984 INSN_COST (i0) = 0;
987 return true;
991 /* Delete any insns that copy a register to itself. */
993 static void
994 delete_noop_moves (void)
996 rtx_insn *insn, *next;
997 basic_block bb;
999 FOR_EACH_BB_FN (bb, cfun)
1001 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
1003 next = NEXT_INSN (insn);
1004 if (INSN_P (insn) && noop_move_p (insn))
1006 if (dump_file)
1007 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
1009 delete_insn_and_edges (insn);
1016 /* Return false if we do not want to (or cannot) combine DEF. */
1017 static bool
1018 can_combine_def_p (df_ref def)
1020 /* Do not consider if it is pre/post modification in MEM. */
1021 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1022 return false;
1024 unsigned int regno = DF_REF_REGNO (def);
1026 /* Do not combine frame pointer adjustments. */
1027 if ((regno == FRAME_POINTER_REGNUM
1028 && (!reload_completed || frame_pointer_needed))
1029 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1030 || (regno == HARD_FRAME_POINTER_REGNUM
1031 && (!reload_completed || frame_pointer_needed))
1032 #endif
1033 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1034 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1035 #endif
1037 return false;
1039 return true;
1042 /* Return false if we do not want to (or cannot) combine USE. */
1043 static bool
1044 can_combine_use_p (df_ref use)
1046 /* Do not consider the usage of the stack pointer by function call. */
1047 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1048 return false;
1050 return true;
1053 /* Fill in log links field for all insns. */
1055 static void
1056 create_log_links (void)
1058 basic_block bb;
1059 rtx_insn **next_use;
1060 rtx_insn *insn;
1061 df_ref def, use;
1063 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1065 /* Pass through each block from the end, recording the uses of each
1066 register and establishing log links when def is encountered.
1067 Note that we do not clear next_use array in order to save time,
1068 so we have to test whether the use is in the same basic block as def.
1070 There are a few cases below when we do not consider the definition or
1071 usage -- these are taken from original flow.c did. Don't ask me why it is
1072 done this way; I don't know and if it works, I don't want to know. */
1074 FOR_EACH_BB_FN (bb, cfun)
1076 FOR_BB_INSNS_REVERSE (bb, insn)
1078 if (!NONDEBUG_INSN_P (insn))
1079 continue;
1081 /* Log links are created only once. */
1082 gcc_assert (!LOG_LINKS (insn));
1084 FOR_EACH_INSN_DEF (def, insn)
1086 unsigned int regno = DF_REF_REGNO (def);
1087 rtx_insn *use_insn;
1089 if (!next_use[regno])
1090 continue;
1092 if (!can_combine_def_p (def))
1093 continue;
1095 use_insn = next_use[regno];
1096 next_use[regno] = NULL;
1098 if (BLOCK_FOR_INSN (use_insn) != bb)
1099 continue;
1101 /* flow.c claimed:
1103 We don't build a LOG_LINK for hard registers contained
1104 in ASM_OPERANDs. If these registers get replaced,
1105 we might wind up changing the semantics of the insn,
1106 even if reload can make what appear to be valid
1107 assignments later. */
1108 if (regno < FIRST_PSEUDO_REGISTER
1109 && asm_noperands (PATTERN (use_insn)) >= 0)
1110 continue;
1112 /* Don't add duplicate links between instructions. */
1113 struct insn_link *links;
1114 FOR_EACH_LOG_LINK (links, use_insn)
1115 if (insn == links->insn && regno == links->regno)
1116 break;
1118 if (!links)
1119 LOG_LINKS (use_insn)
1120 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1123 FOR_EACH_INSN_USE (use, insn)
1124 if (can_combine_use_p (use))
1125 next_use[DF_REF_REGNO (use)] = insn;
1129 free (next_use);
1132 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1133 true if we found a LOG_LINK that proves that A feeds B. This only works
1134 if there are no instructions between A and B which could have a link
1135 depending on A, since in that case we would not record a link for B.
1136 We also check the implicit dependency created by a cc0 setter/user
1137 pair. */
1139 static bool
1140 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1142 struct insn_link *links;
1143 FOR_EACH_LOG_LINK (links, b)
1144 if (links->insn == a)
1145 return true;
1146 #ifdef HAVE_cc0
1147 if (sets_cc0_p (a))
1148 return true;
1149 #endif
1150 return false;
1153 /* Main entry point for combiner. F is the first insn of the function.
1154 NREGS is the first unused pseudo-reg number.
1156 Return nonzero if the combiner has turned an indirect jump
1157 instruction into a direct jump. */
1158 static int
1159 combine_instructions (rtx_insn *f, unsigned int nregs)
1161 rtx_insn *insn, *next;
1162 #ifdef HAVE_cc0
1163 rtx_insn *prev;
1164 #endif
1165 struct insn_link *links, *nextlinks;
1166 rtx_insn *first;
1167 basic_block last_bb;
1169 int new_direct_jump_p = 0;
1171 for (first = f; first && !INSN_P (first); )
1172 first = NEXT_INSN (first);
1173 if (!first)
1174 return 0;
1176 combine_attempts = 0;
1177 combine_merges = 0;
1178 combine_extras = 0;
1179 combine_successes = 0;
1181 rtl_hooks = combine_rtl_hooks;
1183 reg_stat.safe_grow_cleared (nregs);
1185 init_recog_no_volatile ();
1187 /* Allocate array for insn info. */
1188 max_uid_known = get_max_uid ();
1189 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1190 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1191 gcc_obstack_init (&insn_link_obstack);
1193 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1195 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1196 problems when, for example, we have j <<= 1 in a loop. */
1198 nonzero_sign_valid = 0;
1199 label_tick = label_tick_ebb_start = 1;
1201 /* Scan all SETs and see if we can deduce anything about what
1202 bits are known to be zero for some registers and how many copies
1203 of the sign bit are known to exist for those registers.
1205 Also set any known values so that we can use it while searching
1206 for what bits are known to be set. */
1208 setup_incoming_promotions (first);
1209 /* Allow the entry block and the first block to fall into the same EBB.
1210 Conceptually the incoming promotions are assigned to the entry block. */
1211 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1213 create_log_links ();
1214 FOR_EACH_BB_FN (this_basic_block, cfun)
1216 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1217 last_call_luid = 0;
1218 mem_last_set = -1;
1220 label_tick++;
1221 if (!single_pred_p (this_basic_block)
1222 || single_pred (this_basic_block) != last_bb)
1223 label_tick_ebb_start = label_tick;
1224 last_bb = this_basic_block;
1226 FOR_BB_INSNS (this_basic_block, insn)
1227 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1229 #ifdef AUTO_INC_DEC
1230 rtx links;
1231 #endif
1233 subst_low_luid = DF_INSN_LUID (insn);
1234 subst_insn = insn;
1236 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1237 insn);
1238 record_dead_and_set_regs (insn);
1240 #ifdef AUTO_INC_DEC
1241 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1242 if (REG_NOTE_KIND (links) == REG_INC)
1243 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1244 insn);
1245 #endif
1247 /* Record the current insn_rtx_cost of this instruction. */
1248 if (NONJUMP_INSN_P (insn))
1249 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1250 optimize_this_for_speed_p);
1251 if (dump_file)
1252 fprintf (dump_file, "insn_cost %d: %d\n",
1253 INSN_UID (insn), INSN_COST (insn));
1257 nonzero_sign_valid = 1;
1259 /* Now scan all the insns in forward order. */
1260 label_tick = label_tick_ebb_start = 1;
1261 init_reg_last ();
1262 setup_incoming_promotions (first);
1263 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1264 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1266 FOR_EACH_BB_FN (this_basic_block, cfun)
1268 rtx_insn *last_combined_insn = NULL;
1269 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1270 last_call_luid = 0;
1271 mem_last_set = -1;
1273 label_tick++;
1274 if (!single_pred_p (this_basic_block)
1275 || single_pred (this_basic_block) != last_bb)
1276 label_tick_ebb_start = label_tick;
1277 last_bb = this_basic_block;
1279 rtl_profile_for_bb (this_basic_block);
1280 for (insn = BB_HEAD (this_basic_block);
1281 insn != NEXT_INSN (BB_END (this_basic_block));
1282 insn = next ? next : NEXT_INSN (insn))
1284 next = 0;
1285 if (!NONDEBUG_INSN_P (insn))
1286 continue;
1288 while (last_combined_insn
1289 && last_combined_insn->deleted ())
1290 last_combined_insn = PREV_INSN (last_combined_insn);
1291 if (last_combined_insn == NULL_RTX
1292 || BARRIER_P (last_combined_insn)
1293 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1294 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1295 last_combined_insn = insn;
1297 /* See if we know about function return values before this
1298 insn based upon SUBREG flags. */
1299 check_promoted_subreg (insn, PATTERN (insn));
1301 /* See if we can find hardregs and subreg of pseudos in
1302 narrower modes. This could help turning TRUNCATEs
1303 into SUBREGs. */
1304 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1306 /* Try this insn with each insn it links back to. */
1308 FOR_EACH_LOG_LINK (links, insn)
1309 if ((next = try_combine (insn, links->insn, NULL,
1310 NULL, &new_direct_jump_p,
1311 last_combined_insn)) != 0)
1313 statistics_counter_event (cfun, "two-insn combine", 1);
1314 goto retry;
1317 /* Try each sequence of three linked insns ending with this one. */
1319 if (max_combine >= 3)
1320 FOR_EACH_LOG_LINK (links, insn)
1322 rtx_insn *link = links->insn;
1324 /* If the linked insn has been replaced by a note, then there
1325 is no point in pursuing this chain any further. */
1326 if (NOTE_P (link))
1327 continue;
1329 FOR_EACH_LOG_LINK (nextlinks, link)
1330 if ((next = try_combine (insn, link, nextlinks->insn,
1331 NULL, &new_direct_jump_p,
1332 last_combined_insn)) != 0)
1334 statistics_counter_event (cfun, "three-insn combine", 1);
1335 goto retry;
1339 #ifdef HAVE_cc0
1340 /* Try to combine a jump insn that uses CC0
1341 with a preceding insn that sets CC0, and maybe with its
1342 logical predecessor as well.
1343 This is how we make decrement-and-branch insns.
1344 We need this special code because data flow connections
1345 via CC0 do not get entered in LOG_LINKS. */
1347 if (JUMP_P (insn)
1348 && (prev = prev_nonnote_insn (insn)) != 0
1349 && NONJUMP_INSN_P (prev)
1350 && sets_cc0_p (PATTERN (prev)))
1352 if ((next = try_combine (insn, prev, NULL, NULL,
1353 &new_direct_jump_p,
1354 last_combined_insn)) != 0)
1355 goto retry;
1357 FOR_EACH_LOG_LINK (nextlinks, prev)
1358 if ((next = try_combine (insn, prev, nextlinks->insn,
1359 NULL, &new_direct_jump_p,
1360 last_combined_insn)) != 0)
1361 goto retry;
1364 /* Do the same for an insn that explicitly references CC0. */
1365 if (NONJUMP_INSN_P (insn)
1366 && (prev = prev_nonnote_insn (insn)) != 0
1367 && NONJUMP_INSN_P (prev)
1368 && sets_cc0_p (PATTERN (prev))
1369 && GET_CODE (PATTERN (insn)) == SET
1370 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1372 if ((next = try_combine (insn, prev, NULL, NULL,
1373 &new_direct_jump_p,
1374 last_combined_insn)) != 0)
1375 goto retry;
1377 FOR_EACH_LOG_LINK (nextlinks, prev)
1378 if ((next = try_combine (insn, prev, nextlinks->insn,
1379 NULL, &new_direct_jump_p,
1380 last_combined_insn)) != 0)
1381 goto retry;
1384 /* Finally, see if any of the insns that this insn links to
1385 explicitly references CC0. If so, try this insn, that insn,
1386 and its predecessor if it sets CC0. */
1387 FOR_EACH_LOG_LINK (links, insn)
1388 if (NONJUMP_INSN_P (links->insn)
1389 && GET_CODE (PATTERN (links->insn)) == SET
1390 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1391 && (prev = prev_nonnote_insn (links->insn)) != 0
1392 && NONJUMP_INSN_P (prev)
1393 && sets_cc0_p (PATTERN (prev))
1394 && (next = try_combine (insn, links->insn,
1395 prev, NULL, &new_direct_jump_p,
1396 last_combined_insn)) != 0)
1397 goto retry;
1398 #endif
1400 /* Try combining an insn with two different insns whose results it
1401 uses. */
1402 if (max_combine >= 3)
1403 FOR_EACH_LOG_LINK (links, insn)
1404 for (nextlinks = links->next; nextlinks;
1405 nextlinks = nextlinks->next)
1406 if ((next = try_combine (insn, links->insn,
1407 nextlinks->insn, NULL,
1408 &new_direct_jump_p,
1409 last_combined_insn)) != 0)
1412 statistics_counter_event (cfun, "three-insn combine", 1);
1413 goto retry;
1416 /* Try four-instruction combinations. */
1417 if (max_combine >= 4)
1418 FOR_EACH_LOG_LINK (links, insn)
1420 struct insn_link *next1;
1421 rtx_insn *link = links->insn;
1423 /* If the linked insn has been replaced by a note, then there
1424 is no point in pursuing this chain any further. */
1425 if (NOTE_P (link))
1426 continue;
1428 FOR_EACH_LOG_LINK (next1, link)
1430 rtx_insn *link1 = next1->insn;
1431 if (NOTE_P (link1))
1432 continue;
1433 /* I0 -> I1 -> I2 -> I3. */
1434 FOR_EACH_LOG_LINK (nextlinks, link1)
1435 if ((next = try_combine (insn, link, link1,
1436 nextlinks->insn,
1437 &new_direct_jump_p,
1438 last_combined_insn)) != 0)
1440 statistics_counter_event (cfun, "four-insn combine", 1);
1441 goto retry;
1443 /* I0, I1 -> I2, I2 -> I3. */
1444 for (nextlinks = next1->next; nextlinks;
1445 nextlinks = nextlinks->next)
1446 if ((next = try_combine (insn, link, link1,
1447 nextlinks->insn,
1448 &new_direct_jump_p,
1449 last_combined_insn)) != 0)
1451 statistics_counter_event (cfun, "four-insn combine", 1);
1452 goto retry;
1456 for (next1 = links->next; next1; next1 = next1->next)
1458 rtx_insn *link1 = next1->insn;
1459 if (NOTE_P (link1))
1460 continue;
1461 /* I0 -> I2; I1, I2 -> I3. */
1462 FOR_EACH_LOG_LINK (nextlinks, link)
1463 if ((next = try_combine (insn, link, link1,
1464 nextlinks->insn,
1465 &new_direct_jump_p,
1466 last_combined_insn)) != 0)
1468 statistics_counter_event (cfun, "four-insn combine", 1);
1469 goto retry;
1471 /* I0 -> I1; I1, I2 -> I3. */
1472 FOR_EACH_LOG_LINK (nextlinks, link1)
1473 if ((next = try_combine (insn, link, link1,
1474 nextlinks->insn,
1475 &new_direct_jump_p,
1476 last_combined_insn)) != 0)
1478 statistics_counter_event (cfun, "four-insn combine", 1);
1479 goto retry;
1484 /* Try this insn with each REG_EQUAL note it links back to. */
1485 FOR_EACH_LOG_LINK (links, insn)
1487 rtx set, note;
1488 rtx_insn *temp = links->insn;
1489 if ((set = single_set (temp)) != 0
1490 && (note = find_reg_equal_equiv_note (temp)) != 0
1491 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1492 /* Avoid using a register that may already been marked
1493 dead by an earlier instruction. */
1494 && ! unmentioned_reg_p (note, SET_SRC (set))
1495 && (GET_MODE (note) == VOIDmode
1496 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1497 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1499 /* Temporarily replace the set's source with the
1500 contents of the REG_EQUAL note. The insn will
1501 be deleted or recognized by try_combine. */
1502 rtx orig = SET_SRC (set);
1503 SET_SRC (set) = note;
1504 i2mod = temp;
1505 i2mod_old_rhs = copy_rtx (orig);
1506 i2mod_new_rhs = copy_rtx (note);
1507 next = try_combine (insn, i2mod, NULL, NULL,
1508 &new_direct_jump_p,
1509 last_combined_insn);
1510 i2mod = NULL;
1511 if (next)
1513 statistics_counter_event (cfun, "insn-with-note combine", 1);
1514 goto retry;
1516 SET_SRC (set) = orig;
1520 if (!NOTE_P (insn))
1521 record_dead_and_set_regs (insn);
1523 retry:
1528 default_rtl_profile ();
1529 clear_bb_flags ();
1530 new_direct_jump_p |= purge_all_dead_edges ();
1531 delete_noop_moves ();
1533 /* Clean up. */
1534 obstack_free (&insn_link_obstack, NULL);
1535 free (uid_log_links);
1536 free (uid_insn_cost);
1537 reg_stat.release ();
1540 struct undo *undo, *next;
1541 for (undo = undobuf.frees; undo; undo = next)
1543 next = undo->next;
1544 free (undo);
1546 undobuf.frees = 0;
1549 total_attempts += combine_attempts;
1550 total_merges += combine_merges;
1551 total_extras += combine_extras;
1552 total_successes += combine_successes;
1554 nonzero_sign_valid = 0;
1555 rtl_hooks = general_rtl_hooks;
1557 /* Make recognizer allow volatile MEMs again. */
1558 init_recog ();
1560 return new_direct_jump_p;
1563 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1565 static void
1566 init_reg_last (void)
1568 unsigned int i;
1569 reg_stat_type *p;
1571 FOR_EACH_VEC_ELT (reg_stat, i, p)
1572 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1575 /* Set up any promoted values for incoming argument registers. */
1577 static void
1578 setup_incoming_promotions (rtx_insn *first)
1580 tree arg;
1581 bool strictly_local = false;
1583 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1584 arg = DECL_CHAIN (arg))
1586 rtx x, reg = DECL_INCOMING_RTL (arg);
1587 int uns1, uns3;
1588 machine_mode mode1, mode2, mode3, mode4;
1590 /* Only continue if the incoming argument is in a register. */
1591 if (!REG_P (reg))
1592 continue;
1594 /* Determine, if possible, whether all call sites of the current
1595 function lie within the current compilation unit. (This does
1596 take into account the exporting of a function via taking its
1597 address, and so forth.) */
1598 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1600 /* The mode and signedness of the argument before any promotions happen
1601 (equal to the mode of the pseudo holding it at that stage). */
1602 mode1 = TYPE_MODE (TREE_TYPE (arg));
1603 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1605 /* The mode and signedness of the argument after any source language and
1606 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1607 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1608 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1610 /* The mode and signedness of the argument as it is actually passed,
1611 see assign_parm_setup_reg in function.c. */
1612 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1613 TREE_TYPE (cfun->decl), 0);
1615 /* The mode of the register in which the argument is being passed. */
1616 mode4 = GET_MODE (reg);
1618 /* Eliminate sign extensions in the callee when:
1619 (a) A mode promotion has occurred; */
1620 if (mode1 == mode3)
1621 continue;
1622 /* (b) The mode of the register is the same as the mode of
1623 the argument as it is passed; */
1624 if (mode3 != mode4)
1625 continue;
1626 /* (c) There's no language level extension; */
1627 if (mode1 == mode2)
1629 /* (c.1) All callers are from the current compilation unit. If that's
1630 the case we don't have to rely on an ABI, we only have to know
1631 what we're generating right now, and we know that we will do the
1632 mode1 to mode2 promotion with the given sign. */
1633 else if (!strictly_local)
1634 continue;
1635 /* (c.2) The combination of the two promotions is useful. This is
1636 true when the signs match, or if the first promotion is unsigned.
1637 In the later case, (sign_extend (zero_extend x)) is the same as
1638 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1639 else if (uns1)
1640 uns3 = true;
1641 else if (uns3)
1642 continue;
1644 /* Record that the value was promoted from mode1 to mode3,
1645 so that any sign extension at the head of the current
1646 function may be eliminated. */
1647 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1648 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1649 record_value_for_reg (reg, first, x);
1653 /* Called via note_stores. If X is a pseudo that is narrower than
1654 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1656 If we are setting only a portion of X and we can't figure out what
1657 portion, assume all bits will be used since we don't know what will
1658 be happening.
1660 Similarly, set how many bits of X are known to be copies of the sign bit
1661 at all locations in the function. This is the smallest number implied
1662 by any set of X. */
1664 static void
1665 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1667 rtx_insn *insn = (rtx_insn *) data;
1668 unsigned int num;
1670 if (REG_P (x)
1671 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1672 /* If this register is undefined at the start of the file, we can't
1673 say what its contents were. */
1674 && ! REGNO_REG_SET_P
1675 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1676 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1678 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1680 if (set == 0 || GET_CODE (set) == CLOBBER)
1682 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1683 rsp->sign_bit_copies = 1;
1684 return;
1687 /* If this register is being initialized using itself, and the
1688 register is uninitialized in this basic block, and there are
1689 no LOG_LINKS which set the register, then part of the
1690 register is uninitialized. In that case we can't assume
1691 anything about the number of nonzero bits.
1693 ??? We could do better if we checked this in
1694 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1695 could avoid making assumptions about the insn which initially
1696 sets the register, while still using the information in other
1697 insns. We would have to be careful to check every insn
1698 involved in the combination. */
1700 if (insn
1701 && reg_referenced_p (x, PATTERN (insn))
1702 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1703 REGNO (x)))
1705 struct insn_link *link;
1707 FOR_EACH_LOG_LINK (link, insn)
1708 if (dead_or_set_p (link->insn, x))
1709 break;
1710 if (!link)
1712 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1713 rsp->sign_bit_copies = 1;
1714 return;
1718 /* If this is a complex assignment, see if we can convert it into a
1719 simple assignment. */
1720 set = expand_field_assignment (set);
1722 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1723 set what we know about X. */
1725 if (SET_DEST (set) == x
1726 || (paradoxical_subreg_p (SET_DEST (set))
1727 && SUBREG_REG (SET_DEST (set)) == x))
1729 rtx src = SET_SRC (set);
1731 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1732 /* If X is narrower than a word and SRC is a non-negative
1733 constant that would appear negative in the mode of X,
1734 sign-extend it for use in reg_stat[].nonzero_bits because some
1735 machines (maybe most) will actually do the sign-extension
1736 and this is the conservative approach.
1738 ??? For 2.5, try to tighten up the MD files in this regard
1739 instead of this kludge. */
1741 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1742 && CONST_INT_P (src)
1743 && INTVAL (src) > 0
1744 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1745 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1746 #endif
1748 /* Don't call nonzero_bits if it cannot change anything. */
1749 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1750 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1751 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1752 if (rsp->sign_bit_copies == 0
1753 || rsp->sign_bit_copies > num)
1754 rsp->sign_bit_copies = num;
1756 else
1758 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1759 rsp->sign_bit_copies = 1;
1764 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1765 optionally insns that were previously combined into I3 or that will be
1766 combined into the merger of INSN and I3. The order is PRED, PRED2,
1767 INSN, SUCC, SUCC2, I3.
1769 Return 0 if the combination is not allowed for any reason.
1771 If the combination is allowed, *PDEST will be set to the single
1772 destination of INSN and *PSRC to the single source, and this function
1773 will return 1. */
1775 static int
1776 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1777 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1778 rtx *pdest, rtx *psrc)
1780 int i;
1781 const_rtx set = 0;
1782 rtx src, dest;
1783 rtx_insn *p;
1784 #ifdef AUTO_INC_DEC
1785 rtx link;
1786 #endif
1787 bool all_adjacent = true;
1788 int (*is_volatile_p) (const_rtx);
1790 if (succ)
1792 if (succ2)
1794 if (next_active_insn (succ2) != i3)
1795 all_adjacent = false;
1796 if (next_active_insn (succ) != succ2)
1797 all_adjacent = false;
1799 else if (next_active_insn (succ) != i3)
1800 all_adjacent = false;
1801 if (next_active_insn (insn) != succ)
1802 all_adjacent = false;
1804 else if (next_active_insn (insn) != i3)
1805 all_adjacent = false;
1807 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1808 or a PARALLEL consisting of such a SET and CLOBBERs.
1810 If INSN has CLOBBER parallel parts, ignore them for our processing.
1811 By definition, these happen during the execution of the insn. When it
1812 is merged with another insn, all bets are off. If they are, in fact,
1813 needed and aren't also supplied in I3, they may be added by
1814 recog_for_combine. Otherwise, it won't match.
1816 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1817 note.
1819 Get the source and destination of INSN. If more than one, can't
1820 combine. */
1822 if (GET_CODE (PATTERN (insn)) == SET)
1823 set = PATTERN (insn);
1824 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1825 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1827 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1829 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1831 switch (GET_CODE (elt))
1833 /* This is important to combine floating point insns
1834 for the SH4 port. */
1835 case USE:
1836 /* Combining an isolated USE doesn't make sense.
1837 We depend here on combinable_i3pat to reject them. */
1838 /* The code below this loop only verifies that the inputs of
1839 the SET in INSN do not change. We call reg_set_between_p
1840 to verify that the REG in the USE does not change between
1841 I3 and INSN.
1842 If the USE in INSN was for a pseudo register, the matching
1843 insn pattern will likely match any register; combining this
1844 with any other USE would only be safe if we knew that the
1845 used registers have identical values, or if there was
1846 something to tell them apart, e.g. different modes. For
1847 now, we forgo such complicated tests and simply disallow
1848 combining of USES of pseudo registers with any other USE. */
1849 if (REG_P (XEXP (elt, 0))
1850 && GET_CODE (PATTERN (i3)) == PARALLEL)
1852 rtx i3pat = PATTERN (i3);
1853 int i = XVECLEN (i3pat, 0) - 1;
1854 unsigned int regno = REGNO (XEXP (elt, 0));
1858 rtx i3elt = XVECEXP (i3pat, 0, i);
1860 if (GET_CODE (i3elt) == USE
1861 && REG_P (XEXP (i3elt, 0))
1862 && (REGNO (XEXP (i3elt, 0)) == regno
1863 ? reg_set_between_p (XEXP (elt, 0),
1864 PREV_INSN (insn), i3)
1865 : regno >= FIRST_PSEUDO_REGISTER))
1866 return 0;
1868 while (--i >= 0);
1870 break;
1872 /* We can ignore CLOBBERs. */
1873 case CLOBBER:
1874 break;
1876 case SET:
1877 /* Ignore SETs whose result isn't used but not those that
1878 have side-effects. */
1879 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1880 && insn_nothrow_p (insn)
1881 && !side_effects_p (elt))
1882 break;
1884 /* If we have already found a SET, this is a second one and
1885 so we cannot combine with this insn. */
1886 if (set)
1887 return 0;
1889 set = elt;
1890 break;
1892 default:
1893 /* Anything else means we can't combine. */
1894 return 0;
1898 if (set == 0
1899 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1900 so don't do anything with it. */
1901 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1902 return 0;
1904 else
1905 return 0;
1907 if (set == 0)
1908 return 0;
1910 /* The simplification in expand_field_assignment may call back to
1911 get_last_value, so set safe guard here. */
1912 subst_low_luid = DF_INSN_LUID (insn);
1914 set = expand_field_assignment (set);
1915 src = SET_SRC (set), dest = SET_DEST (set);
1917 /* Don't eliminate a store in the stack pointer. */
1918 if (dest == stack_pointer_rtx
1919 /* Don't combine with an insn that sets a register to itself if it has
1920 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1921 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1922 /* Can't merge an ASM_OPERANDS. */
1923 || GET_CODE (src) == ASM_OPERANDS
1924 /* Can't merge a function call. */
1925 || GET_CODE (src) == CALL
1926 /* Don't eliminate a function call argument. */
1927 || (CALL_P (i3)
1928 && (find_reg_fusage (i3, USE, dest)
1929 || (REG_P (dest)
1930 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1931 && global_regs[REGNO (dest)])))
1932 /* Don't substitute into an incremented register. */
1933 || FIND_REG_INC_NOTE (i3, dest)
1934 || (succ && FIND_REG_INC_NOTE (succ, dest))
1935 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1936 /* Don't substitute into a non-local goto, this confuses CFG. */
1937 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1938 /* Make sure that DEST is not used after SUCC but before I3. */
1939 || (!all_adjacent
1940 && ((succ2
1941 && (reg_used_between_p (dest, succ2, i3)
1942 || reg_used_between_p (dest, succ, succ2)))
1943 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1944 /* Make sure that the value that is to be substituted for the register
1945 does not use any registers whose values alter in between. However,
1946 If the insns are adjacent, a use can't cross a set even though we
1947 think it might (this can happen for a sequence of insns each setting
1948 the same destination; last_set of that register might point to
1949 a NOTE). If INSN has a REG_EQUIV note, the register is always
1950 equivalent to the memory so the substitution is valid even if there
1951 are intervening stores. Also, don't move a volatile asm or
1952 UNSPEC_VOLATILE across any other insns. */
1953 || (! all_adjacent
1954 && (((!MEM_P (src)
1955 || ! find_reg_note (insn, REG_EQUIV, src))
1956 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1957 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1958 || GET_CODE (src) == UNSPEC_VOLATILE))
1959 /* Don't combine across a CALL_INSN, because that would possibly
1960 change whether the life span of some REGs crosses calls or not,
1961 and it is a pain to update that information.
1962 Exception: if source is a constant, moving it later can't hurt.
1963 Accept that as a special case. */
1964 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1965 return 0;
1967 /* DEST must either be a REG or CC0. */
1968 if (REG_P (dest))
1970 /* If register alignment is being enforced for multi-word items in all
1971 cases except for parameters, it is possible to have a register copy
1972 insn referencing a hard register that is not allowed to contain the
1973 mode being copied and which would not be valid as an operand of most
1974 insns. Eliminate this problem by not combining with such an insn.
1976 Also, on some machines we don't want to extend the life of a hard
1977 register. */
1979 if (REG_P (src)
1980 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1981 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1982 /* Don't extend the life of a hard register unless it is
1983 user variable (if we have few registers) or it can't
1984 fit into the desired register (meaning something special
1985 is going on).
1986 Also avoid substituting a return register into I3, because
1987 reload can't handle a conflict with constraints of other
1988 inputs. */
1989 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1990 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1991 return 0;
1993 else if (GET_CODE (dest) != CC0)
1994 return 0;
1997 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1998 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1999 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2001 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2003 /* If the clobber represents an earlyclobber operand, we must not
2004 substitute an expression containing the clobbered register.
2005 As we do not analyze the constraint strings here, we have to
2006 make the conservative assumption. However, if the register is
2007 a fixed hard reg, the clobber cannot represent any operand;
2008 we leave it up to the machine description to either accept or
2009 reject use-and-clobber patterns. */
2010 if (!REG_P (reg)
2011 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2012 || !fixed_regs[REGNO (reg)])
2013 if (reg_overlap_mentioned_p (reg, src))
2014 return 0;
2017 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2018 or not), reject, unless nothing volatile comes between it and I3 */
2020 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2022 /* Make sure neither succ nor succ2 contains a volatile reference. */
2023 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2024 return 0;
2025 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2026 return 0;
2027 /* We'll check insns between INSN and I3 below. */
2030 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2031 to be an explicit register variable, and was chosen for a reason. */
2033 if (GET_CODE (src) == ASM_OPERANDS
2034 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2035 return 0;
2037 /* If INSN contains volatile references (specifically volatile MEMs),
2038 we cannot combine across any other volatile references.
2039 Even if INSN doesn't contain volatile references, any intervening
2040 volatile insn might affect machine state. */
2042 is_volatile_p = volatile_refs_p (PATTERN (insn))
2043 ? volatile_refs_p
2044 : volatile_insn_p;
2046 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2047 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2048 return 0;
2050 /* If INSN contains an autoincrement or autodecrement, make sure that
2051 register is not used between there and I3, and not already used in
2052 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2053 Also insist that I3 not be a jump; if it were one
2054 and the incremented register were spilled, we would lose. */
2056 #ifdef AUTO_INC_DEC
2057 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2058 if (REG_NOTE_KIND (link) == REG_INC
2059 && (JUMP_P (i3)
2060 || reg_used_between_p (XEXP (link, 0), insn, i3)
2061 || (pred != NULL_RTX
2062 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2063 || (pred2 != NULL_RTX
2064 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2065 || (succ != NULL_RTX
2066 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2067 || (succ2 != NULL_RTX
2068 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2069 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2070 return 0;
2071 #endif
2073 #ifdef HAVE_cc0
2074 /* Don't combine an insn that follows a CC0-setting insn.
2075 An insn that uses CC0 must not be separated from the one that sets it.
2076 We do, however, allow I2 to follow a CC0-setting insn if that insn
2077 is passed as I1; in that case it will be deleted also.
2078 We also allow combining in this case if all the insns are adjacent
2079 because that would leave the two CC0 insns adjacent as well.
2080 It would be more logical to test whether CC0 occurs inside I1 or I2,
2081 but that would be much slower, and this ought to be equivalent. */
2083 p = prev_nonnote_insn (insn);
2084 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2085 && ! all_adjacent)
2086 return 0;
2087 #endif
2089 /* If we get here, we have passed all the tests and the combination is
2090 to be allowed. */
2092 *pdest = dest;
2093 *psrc = src;
2095 return 1;
2098 /* LOC is the location within I3 that contains its pattern or the component
2099 of a PARALLEL of the pattern. We validate that it is valid for combining.
2101 One problem is if I3 modifies its output, as opposed to replacing it
2102 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2103 doing so would produce an insn that is not equivalent to the original insns.
2105 Consider:
2107 (set (reg:DI 101) (reg:DI 100))
2108 (set (subreg:SI (reg:DI 101) 0) <foo>)
2110 This is NOT equivalent to:
2112 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2113 (set (reg:DI 101) (reg:DI 100))])
2115 Not only does this modify 100 (in which case it might still be valid
2116 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2118 We can also run into a problem if I2 sets a register that I1
2119 uses and I1 gets directly substituted into I3 (not via I2). In that
2120 case, we would be getting the wrong value of I2DEST into I3, so we
2121 must reject the combination. This case occurs when I2 and I1 both
2122 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2123 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2124 of a SET must prevent combination from occurring. The same situation
2125 can occur for I0, in which case I0_NOT_IN_SRC is set.
2127 Before doing the above check, we first try to expand a field assignment
2128 into a set of logical operations.
2130 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2131 we place a register that is both set and used within I3. If more than one
2132 such register is detected, we fail.
2134 Return 1 if the combination is valid, zero otherwise. */
2136 static int
2137 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2138 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2140 rtx x = *loc;
2142 if (GET_CODE (x) == SET)
2144 rtx set = x ;
2145 rtx dest = SET_DEST (set);
2146 rtx src = SET_SRC (set);
2147 rtx inner_dest = dest;
2148 rtx subdest;
2150 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2151 || GET_CODE (inner_dest) == SUBREG
2152 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2153 inner_dest = XEXP (inner_dest, 0);
2155 /* Check for the case where I3 modifies its output, as discussed
2156 above. We don't want to prevent pseudos from being combined
2157 into the address of a MEM, so only prevent the combination if
2158 i1 or i2 set the same MEM. */
2159 if ((inner_dest != dest &&
2160 (!MEM_P (inner_dest)
2161 || rtx_equal_p (i2dest, inner_dest)
2162 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2163 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2164 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2165 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2166 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2168 /* This is the same test done in can_combine_p except we can't test
2169 all_adjacent; we don't have to, since this instruction will stay
2170 in place, thus we are not considering increasing the lifetime of
2171 INNER_DEST.
2173 Also, if this insn sets a function argument, combining it with
2174 something that might need a spill could clobber a previous
2175 function argument; the all_adjacent test in can_combine_p also
2176 checks this; here, we do a more specific test for this case. */
2178 || (REG_P (inner_dest)
2179 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2180 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2181 GET_MODE (inner_dest))))
2182 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2183 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2184 return 0;
2186 /* If DEST is used in I3, it is being killed in this insn, so
2187 record that for later. We have to consider paradoxical
2188 subregs here, since they kill the whole register, but we
2189 ignore partial subregs, STRICT_LOW_PART, etc.
2190 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2191 STACK_POINTER_REGNUM, since these are always considered to be
2192 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2193 subdest = dest;
2194 if (GET_CODE (subdest) == SUBREG
2195 && (GET_MODE_SIZE (GET_MODE (subdest))
2196 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2197 subdest = SUBREG_REG (subdest);
2198 if (pi3dest_killed
2199 && REG_P (subdest)
2200 && reg_referenced_p (subdest, PATTERN (i3))
2201 && REGNO (subdest) != FRAME_POINTER_REGNUM
2202 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2203 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2204 #endif
2205 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2206 && (REGNO (subdest) != ARG_POINTER_REGNUM
2207 || ! fixed_regs [REGNO (subdest)])
2208 #endif
2209 && REGNO (subdest) != STACK_POINTER_REGNUM)
2211 if (*pi3dest_killed)
2212 return 0;
2214 *pi3dest_killed = subdest;
2218 else if (GET_CODE (x) == PARALLEL)
2220 int i;
2222 for (i = 0; i < XVECLEN (x, 0); i++)
2223 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2224 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2225 return 0;
2228 return 1;
2231 /* Return 1 if X is an arithmetic expression that contains a multiplication
2232 and division. We don't count multiplications by powers of two here. */
2234 static int
2235 contains_muldiv (rtx x)
2237 switch (GET_CODE (x))
2239 case MOD: case DIV: case UMOD: case UDIV:
2240 return 1;
2242 case MULT:
2243 return ! (CONST_INT_P (XEXP (x, 1))
2244 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2245 default:
2246 if (BINARY_P (x))
2247 return contains_muldiv (XEXP (x, 0))
2248 || contains_muldiv (XEXP (x, 1));
2250 if (UNARY_P (x))
2251 return contains_muldiv (XEXP (x, 0));
2253 return 0;
2257 /* Determine whether INSN can be used in a combination. Return nonzero if
2258 not. This is used in try_combine to detect early some cases where we
2259 can't perform combinations. */
2261 static int
2262 cant_combine_insn_p (rtx_insn *insn)
2264 rtx set;
2265 rtx src, dest;
2267 /* If this isn't really an insn, we can't do anything.
2268 This can occur when flow deletes an insn that it has merged into an
2269 auto-increment address. */
2270 if (! INSN_P (insn))
2271 return 1;
2273 /* Never combine loads and stores involving hard regs that are likely
2274 to be spilled. The register allocator can usually handle such
2275 reg-reg moves by tying. If we allow the combiner to make
2276 substitutions of likely-spilled regs, reload might die.
2277 As an exception, we allow combinations involving fixed regs; these are
2278 not available to the register allocator so there's no risk involved. */
2280 set = single_set (insn);
2281 if (! set)
2282 return 0;
2283 src = SET_SRC (set);
2284 dest = SET_DEST (set);
2285 if (GET_CODE (src) == SUBREG)
2286 src = SUBREG_REG (src);
2287 if (GET_CODE (dest) == SUBREG)
2288 dest = SUBREG_REG (dest);
2289 if (REG_P (src) && REG_P (dest)
2290 && ((HARD_REGISTER_P (src)
2291 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2292 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2293 || (HARD_REGISTER_P (dest)
2294 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2295 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2296 return 1;
2298 return 0;
2301 struct likely_spilled_retval_info
2303 unsigned regno, nregs;
2304 unsigned mask;
2307 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2308 hard registers that are known to be written to / clobbered in full. */
2309 static void
2310 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2312 struct likely_spilled_retval_info *const info =
2313 (struct likely_spilled_retval_info *) data;
2314 unsigned regno, nregs;
2315 unsigned new_mask;
2317 if (!REG_P (XEXP (set, 0)))
2318 return;
2319 regno = REGNO (x);
2320 if (regno >= info->regno + info->nregs)
2321 return;
2322 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2323 if (regno + nregs <= info->regno)
2324 return;
2325 new_mask = (2U << (nregs - 1)) - 1;
2326 if (regno < info->regno)
2327 new_mask >>= info->regno - regno;
2328 else
2329 new_mask <<= regno - info->regno;
2330 info->mask &= ~new_mask;
2333 /* Return nonzero iff part of the return value is live during INSN, and
2334 it is likely spilled. This can happen when more than one insn is needed
2335 to copy the return value, e.g. when we consider to combine into the
2336 second copy insn for a complex value. */
2338 static int
2339 likely_spilled_retval_p (rtx_insn *insn)
2341 rtx_insn *use = BB_END (this_basic_block);
2342 rtx reg;
2343 rtx_insn *p;
2344 unsigned regno, nregs;
2345 /* We assume here that no machine mode needs more than
2346 32 hard registers when the value overlaps with a register
2347 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2348 unsigned mask;
2349 struct likely_spilled_retval_info info;
2351 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2352 return 0;
2353 reg = XEXP (PATTERN (use), 0);
2354 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2355 return 0;
2356 regno = REGNO (reg);
2357 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2358 if (nregs == 1)
2359 return 0;
2360 mask = (2U << (nregs - 1)) - 1;
2362 /* Disregard parts of the return value that are set later. */
2363 info.regno = regno;
2364 info.nregs = nregs;
2365 info.mask = mask;
2366 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2367 if (INSN_P (p))
2368 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2369 mask = info.mask;
2371 /* Check if any of the (probably) live return value registers is
2372 likely spilled. */
2373 nregs --;
2376 if ((mask & 1 << nregs)
2377 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2378 return 1;
2379 } while (nregs--);
2380 return 0;
2383 /* Adjust INSN after we made a change to its destination.
2385 Changing the destination can invalidate notes that say something about
2386 the results of the insn and a LOG_LINK pointing to the insn. */
2388 static void
2389 adjust_for_new_dest (rtx_insn *insn)
2391 /* For notes, be conservative and simply remove them. */
2392 remove_reg_equal_equiv_notes (insn);
2394 /* The new insn will have a destination that was previously the destination
2395 of an insn just above it. Call distribute_links to make a LOG_LINK from
2396 the next use of that destination. */
2398 rtx set = single_set (insn);
2399 gcc_assert (set);
2401 rtx reg = SET_DEST (set);
2403 while (GET_CODE (reg) == ZERO_EXTRACT
2404 || GET_CODE (reg) == STRICT_LOW_PART
2405 || GET_CODE (reg) == SUBREG)
2406 reg = XEXP (reg, 0);
2407 gcc_assert (REG_P (reg));
2409 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2411 df_insn_rescan (insn);
2414 /* Return TRUE if combine can reuse reg X in mode MODE.
2415 ADDED_SETS is nonzero if the original set is still required. */
2416 static bool
2417 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2419 unsigned int regno;
2421 if (!REG_P (x))
2422 return false;
2424 regno = REGNO (x);
2425 /* Allow hard registers if the new mode is legal, and occupies no more
2426 registers than the old mode. */
2427 if (regno < FIRST_PSEUDO_REGISTER)
2428 return (HARD_REGNO_MODE_OK (regno, mode)
2429 && (hard_regno_nregs[regno][GET_MODE (x)]
2430 >= hard_regno_nregs[regno][mode]));
2432 /* Or a pseudo that is only used once. */
2433 return (regno < reg_n_sets_max
2434 && REG_N_SETS (regno) == 1
2435 && !added_sets
2436 && !REG_USERVAR_P (x));
2440 /* Check whether X, the destination of a set, refers to part of
2441 the register specified by REG. */
2443 static bool
2444 reg_subword_p (rtx x, rtx reg)
2446 /* Check that reg is an integer mode register. */
2447 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2448 return false;
2450 if (GET_CODE (x) == STRICT_LOW_PART
2451 || GET_CODE (x) == ZERO_EXTRACT)
2452 x = XEXP (x, 0);
2454 return GET_CODE (x) == SUBREG
2455 && SUBREG_REG (x) == reg
2456 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2459 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2460 Note that the INSN should be deleted *after* removing dead edges, so
2461 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2462 but not for a (set (pc) (label_ref FOO)). */
2464 static void
2465 update_cfg_for_uncondjump (rtx_insn *insn)
2467 basic_block bb = BLOCK_FOR_INSN (insn);
2468 gcc_assert (BB_END (bb) == insn);
2470 purge_dead_edges (bb);
2472 delete_insn (insn);
2473 if (EDGE_COUNT (bb->succs) == 1)
2475 rtx_insn *insn;
2477 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2479 /* Remove barriers from the footer if there are any. */
2480 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2481 if (BARRIER_P (insn))
2483 if (PREV_INSN (insn))
2484 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2485 else
2486 BB_FOOTER (bb) = NEXT_INSN (insn);
2487 if (NEXT_INSN (insn))
2488 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2490 else if (LABEL_P (insn))
2491 break;
2495 #ifndef HAVE_cc0
2496 /* Return whether INSN is a PARALLEL of exactly N register SETs followed
2497 by an arbitrary number of CLOBBERs. */
2498 static bool
2499 is_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2501 rtx pat = PATTERN (insn);
2503 if (GET_CODE (pat) != PARALLEL)
2504 return false;
2506 int len = XVECLEN (pat, 0);
2507 if (len < n)
2508 return false;
2510 int i;
2511 for (i = 0; i < n; i++)
2512 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2513 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2514 return false;
2515 for ( ; i < len; i++)
2516 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2517 return false;
2519 return true;
2522 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2523 CLOBBERs), can be split into individual SETs in that order, without
2524 changing semantics. */
2525 static bool
2526 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2528 if (!insn_nothrow_p (insn))
2529 return false;
2531 rtx pat = PATTERN (insn);
2533 int i, j;
2534 for (i = 0; i < n; i++)
2536 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2537 return false;
2539 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2541 for (j = i + 1; j < n; j++)
2542 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2543 return false;
2546 return true;
2548 #endif
2550 /* Try to combine the insns I0, I1 and I2 into I3.
2551 Here I0, I1 and I2 appear earlier than I3.
2552 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2555 If we are combining more than two insns and the resulting insn is not
2556 recognized, try splitting it into two insns. If that happens, I2 and I3
2557 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2558 Otherwise, I0, I1 and I2 are pseudo-deleted.
2560 Return 0 if the combination does not work. Then nothing is changed.
2561 If we did the combination, return the insn at which combine should
2562 resume scanning.
2564 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2565 new direct jump instruction.
2567 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2568 been I3 passed to an earlier try_combine within the same basic
2569 block. */
2571 static rtx_insn *
2572 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2573 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2575 /* New patterns for I3 and I2, respectively. */
2576 rtx newpat, newi2pat = 0;
2577 rtvec newpat_vec_with_clobbers = 0;
2578 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2579 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2580 dead. */
2581 int added_sets_0, added_sets_1, added_sets_2;
2582 /* Total number of SETs to put into I3. */
2583 int total_sets;
2584 /* Nonzero if I2's or I1's body now appears in I3. */
2585 int i2_is_used = 0, i1_is_used = 0;
2586 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2587 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2588 /* Contains I3 if the destination of I3 is used in its source, which means
2589 that the old life of I3 is being killed. If that usage is placed into
2590 I2 and not in I3, a REG_DEAD note must be made. */
2591 rtx i3dest_killed = 0;
2592 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2593 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2594 /* Copy of SET_SRC of I1 and I0, if needed. */
2595 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2596 /* Set if I2DEST was reused as a scratch register. */
2597 bool i2scratch = false;
2598 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2599 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2600 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2601 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2602 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2603 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2604 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2605 /* Notes that must be added to REG_NOTES in I3 and I2. */
2606 rtx new_i3_notes, new_i2_notes;
2607 /* Notes that we substituted I3 into I2 instead of the normal case. */
2608 int i3_subst_into_i2 = 0;
2609 /* Notes that I1, I2 or I3 is a MULT operation. */
2610 int have_mult = 0;
2611 int swap_i2i3 = 0;
2612 int changed_i3_dest = 0;
2614 int maxreg;
2615 rtx_insn *temp_insn;
2616 rtx temp_expr;
2617 struct insn_link *link;
2618 rtx other_pat = 0;
2619 rtx new_other_notes;
2620 int i;
2622 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2623 never be). */
2624 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2625 return 0;
2627 /* Only try four-insn combinations when there's high likelihood of
2628 success. Look for simple insns, such as loads of constants or
2629 binary operations involving a constant. */
2630 if (i0)
2632 int i;
2633 int ngood = 0;
2634 int nshift = 0;
2635 rtx set0, set3;
2637 if (!flag_expensive_optimizations)
2638 return 0;
2640 for (i = 0; i < 4; i++)
2642 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2643 rtx set = single_set (insn);
2644 rtx src;
2645 if (!set)
2646 continue;
2647 src = SET_SRC (set);
2648 if (CONSTANT_P (src))
2650 ngood += 2;
2651 break;
2653 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2654 ngood++;
2655 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2656 || GET_CODE (src) == LSHIFTRT)
2657 nshift++;
2660 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2661 are likely manipulating its value. Ideally we'll be able to combine
2662 all four insns into a bitfield insertion of some kind.
2664 Note the source in I0 might be inside a sign/zero extension and the
2665 memory modes in I0 and I3 might be different. So extract the address
2666 from the destination of I3 and search for it in the source of I0.
2668 In the event that there's a match but the source/dest do not actually
2669 refer to the same memory, the worst that happens is we try some
2670 combinations that we wouldn't have otherwise. */
2671 if ((set0 = single_set (i0))
2672 /* Ensure the source of SET0 is a MEM, possibly buried inside
2673 an extension. */
2674 && (GET_CODE (SET_SRC (set0)) == MEM
2675 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2676 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2677 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2678 && (set3 = single_set (i3))
2679 /* Ensure the destination of SET3 is a MEM. */
2680 && GET_CODE (SET_DEST (set3)) == MEM
2681 /* Would it be better to extract the base address for the MEM
2682 in SET3 and look for that? I don't have cases where it matters
2683 but I could envision such cases. */
2684 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2685 ngood += 2;
2687 if (ngood < 2 && nshift < 2)
2688 return 0;
2691 /* Exit early if one of the insns involved can't be used for
2692 combinations. */
2693 if (CALL_P (i2)
2694 || (i1 && CALL_P (i1))
2695 || (i0 && CALL_P (i0))
2696 || cant_combine_insn_p (i3)
2697 || cant_combine_insn_p (i2)
2698 || (i1 && cant_combine_insn_p (i1))
2699 || (i0 && cant_combine_insn_p (i0))
2700 || likely_spilled_retval_p (i3))
2701 return 0;
2703 combine_attempts++;
2704 undobuf.other_insn = 0;
2706 /* Reset the hard register usage information. */
2707 CLEAR_HARD_REG_SET (newpat_used_regs);
2709 if (dump_file && (dump_flags & TDF_DETAILS))
2711 if (i0)
2712 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2713 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2714 else if (i1)
2715 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2716 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2717 else
2718 fprintf (dump_file, "\nTrying %d -> %d:\n",
2719 INSN_UID (i2), INSN_UID (i3));
2722 /* If multiple insns feed into one of I2 or I3, they can be in any
2723 order. To simplify the code below, reorder them in sequence. */
2724 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2725 temp_insn = i2, i2 = i0, i0 = temp_insn;
2726 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2727 temp_insn = i1, i1 = i0, i0 = temp_insn;
2728 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2729 temp_insn = i1, i1 = i2, i2 = temp_insn;
2731 added_links_insn = 0;
2733 /* First check for one important special case that the code below will
2734 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2735 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2736 we may be able to replace that destination with the destination of I3.
2737 This occurs in the common code where we compute both a quotient and
2738 remainder into a structure, in which case we want to do the computation
2739 directly into the structure to avoid register-register copies.
2741 Note that this case handles both multiple sets in I2 and also cases
2742 where I2 has a number of CLOBBERs inside the PARALLEL.
2744 We make very conservative checks below and only try to handle the
2745 most common cases of this. For example, we only handle the case
2746 where I2 and I3 are adjacent to avoid making difficult register
2747 usage tests. */
2749 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2750 && REG_P (SET_SRC (PATTERN (i3)))
2751 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2752 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2753 && GET_CODE (PATTERN (i2)) == PARALLEL
2754 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2755 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2756 below would need to check what is inside (and reg_overlap_mentioned_p
2757 doesn't support those codes anyway). Don't allow those destinations;
2758 the resulting insn isn't likely to be recognized anyway. */
2759 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2760 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2761 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2762 SET_DEST (PATTERN (i3)))
2763 && next_active_insn (i2) == i3)
2765 rtx p2 = PATTERN (i2);
2767 /* Make sure that the destination of I3,
2768 which we are going to substitute into one output of I2,
2769 is not used within another output of I2. We must avoid making this:
2770 (parallel [(set (mem (reg 69)) ...)
2771 (set (reg 69) ...)])
2772 which is not well-defined as to order of actions.
2773 (Besides, reload can't handle output reloads for this.)
2775 The problem can also happen if the dest of I3 is a memory ref,
2776 if another dest in I2 is an indirect memory ref. */
2777 for (i = 0; i < XVECLEN (p2, 0); i++)
2778 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2779 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2780 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2781 SET_DEST (XVECEXP (p2, 0, i))))
2782 break;
2784 /* Make sure this PARALLEL is not an asm. We do not allow combining
2785 that usually (see can_combine_p), so do not here either. */
2786 for (i = 0; i < XVECLEN (p2, 0); i++)
2787 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2788 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2789 break;
2791 if (i == XVECLEN (p2, 0))
2792 for (i = 0; i < XVECLEN (p2, 0); i++)
2793 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2794 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2796 combine_merges++;
2798 subst_insn = i3;
2799 subst_low_luid = DF_INSN_LUID (i2);
2801 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2802 i2src = SET_SRC (XVECEXP (p2, 0, i));
2803 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2804 i2dest_killed = dead_or_set_p (i2, i2dest);
2806 /* Replace the dest in I2 with our dest and make the resulting
2807 insn the new pattern for I3. Then skip to where we validate
2808 the pattern. Everything was set up above. */
2809 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2810 newpat = p2;
2811 i3_subst_into_i2 = 1;
2812 goto validate_replacement;
2816 /* If I2 is setting a pseudo to a constant and I3 is setting some
2817 sub-part of it to another constant, merge them by making a new
2818 constant. */
2819 if (i1 == 0
2820 && (temp_expr = single_set (i2)) != 0
2821 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2822 && GET_CODE (PATTERN (i3)) == SET
2823 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2824 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2826 rtx dest = SET_DEST (PATTERN (i3));
2827 int offset = -1;
2828 int width = 0;
2830 if (GET_CODE (dest) == ZERO_EXTRACT)
2832 if (CONST_INT_P (XEXP (dest, 1))
2833 && CONST_INT_P (XEXP (dest, 2)))
2835 width = INTVAL (XEXP (dest, 1));
2836 offset = INTVAL (XEXP (dest, 2));
2837 dest = XEXP (dest, 0);
2838 if (BITS_BIG_ENDIAN)
2839 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2842 else
2844 if (GET_CODE (dest) == STRICT_LOW_PART)
2845 dest = XEXP (dest, 0);
2846 width = GET_MODE_PRECISION (GET_MODE (dest));
2847 offset = 0;
2850 if (offset >= 0)
2852 /* If this is the low part, we're done. */
2853 if (subreg_lowpart_p (dest))
2855 /* Handle the case where inner is twice the size of outer. */
2856 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2857 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2858 offset += GET_MODE_PRECISION (GET_MODE (dest));
2859 /* Otherwise give up for now. */
2860 else
2861 offset = -1;
2864 if (offset >= 0)
2866 rtx inner = SET_SRC (PATTERN (i3));
2867 rtx outer = SET_SRC (temp_expr);
2869 wide_int o
2870 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2871 std::make_pair (inner, GET_MODE (dest)),
2872 offset, width);
2874 combine_merges++;
2875 subst_insn = i3;
2876 subst_low_luid = DF_INSN_LUID (i2);
2877 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2878 i2dest = SET_DEST (temp_expr);
2879 i2dest_killed = dead_or_set_p (i2, i2dest);
2881 /* Replace the source in I2 with the new constant and make the
2882 resulting insn the new pattern for I3. Then skip to where we
2883 validate the pattern. Everything was set up above. */
2884 SUBST (SET_SRC (temp_expr),
2885 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2887 newpat = PATTERN (i2);
2889 /* The dest of I3 has been replaced with the dest of I2. */
2890 changed_i3_dest = 1;
2891 goto validate_replacement;
2895 #ifndef HAVE_cc0
2896 /* If we have no I1 and I2 looks like:
2897 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2898 (set Y OP)])
2899 make up a dummy I1 that is
2900 (set Y OP)
2901 and change I2 to be
2902 (set (reg:CC X) (compare:CC Y (const_int 0)))
2904 (We can ignore any trailing CLOBBERs.)
2906 This undoes a previous combination and allows us to match a branch-and-
2907 decrement insn. */
2909 if (i1 == 0
2910 && is_parallel_of_n_reg_sets (i2, 2)
2911 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2912 == MODE_CC)
2913 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2914 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2915 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2916 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2917 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2918 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2920 /* We make I1 with the same INSN_UID as I2. This gives it
2921 the same DF_INSN_LUID for value tracking. Our fake I1 will
2922 never appear in the insn stream so giving it the same INSN_UID
2923 as I2 will not cause a problem. */
2925 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2926 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2927 -1, NULL_RTX);
2928 INSN_UID (i1) = INSN_UID (i2);
2930 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2931 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2932 SET_DEST (PATTERN (i1)));
2933 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2934 SUBST_LINK (LOG_LINKS (i2),
2935 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2938 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2939 make those two SETs separate I1 and I2 insns, and make an I0 that is
2940 the original I1. */
2941 if (i0 == 0
2942 && is_parallel_of_n_reg_sets (i2, 2)
2943 && can_split_parallel_of_n_reg_sets (i2, 2)
2944 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2945 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2947 /* If there is no I1, there is no I0 either. */
2948 i0 = i1;
2950 /* We make I1 with the same INSN_UID as I2. This gives it
2951 the same DF_INSN_LUID for value tracking. Our fake I1 will
2952 never appear in the insn stream so giving it the same INSN_UID
2953 as I2 will not cause a problem. */
2955 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2956 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2957 -1, NULL_RTX);
2958 INSN_UID (i1) = INSN_UID (i2);
2960 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2962 #endif
2964 /* Verify that I2 and I1 are valid for combining. */
2965 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2966 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2967 &i1dest, &i1src))
2968 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2969 &i0dest, &i0src)))
2971 undo_all ();
2972 return 0;
2975 /* Record whether I2DEST is used in I2SRC and similarly for the other
2976 cases. Knowing this will help in register status updating below. */
2977 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2978 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2979 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2980 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2981 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2982 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2983 i2dest_killed = dead_or_set_p (i2, i2dest);
2984 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2985 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2987 /* For the earlier insns, determine which of the subsequent ones they
2988 feed. */
2989 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2990 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2991 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2992 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2993 && reg_overlap_mentioned_p (i0dest, i2src))));
2995 /* Ensure that I3's pattern can be the destination of combines. */
2996 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2997 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2998 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2999 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3000 &i3dest_killed))
3002 undo_all ();
3003 return 0;
3006 /* See if any of the insns is a MULT operation. Unless one is, we will
3007 reject a combination that is, since it must be slower. Be conservative
3008 here. */
3009 if (GET_CODE (i2src) == MULT
3010 || (i1 != 0 && GET_CODE (i1src) == MULT)
3011 || (i0 != 0 && GET_CODE (i0src) == MULT)
3012 || (GET_CODE (PATTERN (i3)) == SET
3013 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3014 have_mult = 1;
3016 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3017 We used to do this EXCEPT in one case: I3 has a post-inc in an
3018 output operand. However, that exception can give rise to insns like
3019 mov r3,(r3)+
3020 which is a famous insn on the PDP-11 where the value of r3 used as the
3021 source was model-dependent. Avoid this sort of thing. */
3023 #if 0
3024 if (!(GET_CODE (PATTERN (i3)) == SET
3025 && REG_P (SET_SRC (PATTERN (i3)))
3026 && MEM_P (SET_DEST (PATTERN (i3)))
3027 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3028 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3029 /* It's not the exception. */
3030 #endif
3031 #ifdef AUTO_INC_DEC
3033 rtx link;
3034 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3035 if (REG_NOTE_KIND (link) == REG_INC
3036 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3037 || (i1 != 0
3038 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3040 undo_all ();
3041 return 0;
3044 #endif
3046 /* See if the SETs in I1 or I2 need to be kept around in the merged
3047 instruction: whenever the value set there is still needed past I3.
3048 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3050 For the SET in I1, we have two cases: if I1 and I2 independently feed
3051 into I3, the set in I1 needs to be kept around unless I1DEST dies
3052 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3053 in I1 needs to be kept around unless I1DEST dies or is set in either
3054 I2 or I3. The same considerations apply to I0. */
3056 added_sets_2 = !dead_or_set_p (i3, i2dest);
3058 if (i1)
3059 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3060 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3061 else
3062 added_sets_1 = 0;
3064 if (i0)
3065 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3066 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3067 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3068 && dead_or_set_p (i2, i0dest)));
3069 else
3070 added_sets_0 = 0;
3072 /* We are about to copy insns for the case where they need to be kept
3073 around. Check that they can be copied in the merged instruction. */
3075 if (targetm.cannot_copy_insn_p
3076 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3077 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3078 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3080 undo_all ();
3081 return 0;
3084 /* If the set in I2 needs to be kept around, we must make a copy of
3085 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3086 PATTERN (I2), we are only substituting for the original I1DEST, not into
3087 an already-substituted copy. This also prevents making self-referential
3088 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3089 I2DEST. */
3091 if (added_sets_2)
3093 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3094 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3095 else
3096 i2pat = copy_rtx (PATTERN (i2));
3099 if (added_sets_1)
3101 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3102 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3103 else
3104 i1pat = copy_rtx (PATTERN (i1));
3107 if (added_sets_0)
3109 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3110 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3111 else
3112 i0pat = copy_rtx (PATTERN (i0));
3115 combine_merges++;
3117 /* Substitute in the latest insn for the regs set by the earlier ones. */
3119 maxreg = max_reg_num ();
3121 subst_insn = i3;
3123 #ifndef HAVE_cc0
3124 /* Many machines that don't use CC0 have insns that can both perform an
3125 arithmetic operation and set the condition code. These operations will
3126 be represented as a PARALLEL with the first element of the vector
3127 being a COMPARE of an arithmetic operation with the constant zero.
3128 The second element of the vector will set some pseudo to the result
3129 of the same arithmetic operation. If we simplify the COMPARE, we won't
3130 match such a pattern and so will generate an extra insn. Here we test
3131 for this case, where both the comparison and the operation result are
3132 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3133 I2SRC. Later we will make the PARALLEL that contains I2. */
3135 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3136 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3137 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3138 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3140 rtx newpat_dest;
3141 rtx *cc_use_loc = NULL;
3142 rtx_insn *cc_use_insn = NULL;
3143 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3144 machine_mode compare_mode, orig_compare_mode;
3145 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3147 newpat = PATTERN (i3);
3148 newpat_dest = SET_DEST (newpat);
3149 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3151 if (undobuf.other_insn == 0
3152 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3153 &cc_use_insn)))
3155 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3156 compare_code = simplify_compare_const (compare_code,
3157 GET_MODE (i2dest), op0, &op1);
3158 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3161 /* Do the rest only if op1 is const0_rtx, which may be the
3162 result of simplification. */
3163 if (op1 == const0_rtx)
3165 /* If a single use of the CC is found, prepare to modify it
3166 when SELECT_CC_MODE returns a new CC-class mode, or when
3167 the above simplify_compare_const() returned a new comparison
3168 operator. undobuf.other_insn is assigned the CC use insn
3169 when modifying it. */
3170 if (cc_use_loc)
3172 #ifdef SELECT_CC_MODE
3173 machine_mode new_mode
3174 = SELECT_CC_MODE (compare_code, op0, op1);
3175 if (new_mode != orig_compare_mode
3176 && can_change_dest_mode (SET_DEST (newpat),
3177 added_sets_2, new_mode))
3179 unsigned int regno = REGNO (newpat_dest);
3180 compare_mode = new_mode;
3181 if (regno < FIRST_PSEUDO_REGISTER)
3182 newpat_dest = gen_rtx_REG (compare_mode, regno);
3183 else
3185 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3186 newpat_dest = regno_reg_rtx[regno];
3189 #endif
3190 /* Cases for modifying the CC-using comparison. */
3191 if (compare_code != orig_compare_code
3192 /* ??? Do we need to verify the zero rtx? */
3193 && XEXP (*cc_use_loc, 1) == const0_rtx)
3195 /* Replace cc_use_loc with entire new RTX. */
3196 SUBST (*cc_use_loc,
3197 gen_rtx_fmt_ee (compare_code, compare_mode,
3198 newpat_dest, const0_rtx));
3199 undobuf.other_insn = cc_use_insn;
3201 else if (compare_mode != orig_compare_mode)
3203 /* Just replace the CC reg with a new mode. */
3204 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3205 undobuf.other_insn = cc_use_insn;
3209 /* Now we modify the current newpat:
3210 First, SET_DEST(newpat) is updated if the CC mode has been
3211 altered. For targets without SELECT_CC_MODE, this should be
3212 optimized away. */
3213 if (compare_mode != orig_compare_mode)
3214 SUBST (SET_DEST (newpat), newpat_dest);
3215 /* This is always done to propagate i2src into newpat. */
3216 SUBST (SET_SRC (newpat),
3217 gen_rtx_COMPARE (compare_mode, op0, op1));
3218 /* Create new version of i2pat if needed; the below PARALLEL
3219 creation needs this to work correctly. */
3220 if (! rtx_equal_p (i2src, op0))
3221 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3222 i2_is_used = 1;
3225 #endif
3227 if (i2_is_used == 0)
3229 /* It is possible that the source of I2 or I1 may be performing
3230 an unneeded operation, such as a ZERO_EXTEND of something
3231 that is known to have the high part zero. Handle that case
3232 by letting subst look at the inner insns.
3234 Another way to do this would be to have a function that tries
3235 to simplify a single insn instead of merging two or more
3236 insns. We don't do this because of the potential of infinite
3237 loops and because of the potential extra memory required.
3238 However, doing it the way we are is a bit of a kludge and
3239 doesn't catch all cases.
3241 But only do this if -fexpensive-optimizations since it slows
3242 things down and doesn't usually win.
3244 This is not done in the COMPARE case above because the
3245 unmodified I2PAT is used in the PARALLEL and so a pattern
3246 with a modified I2SRC would not match. */
3248 if (flag_expensive_optimizations)
3250 /* Pass pc_rtx so no substitutions are done, just
3251 simplifications. */
3252 if (i1)
3254 subst_low_luid = DF_INSN_LUID (i1);
3255 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3258 subst_low_luid = DF_INSN_LUID (i2);
3259 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3262 n_occurrences = 0; /* `subst' counts here */
3263 subst_low_luid = DF_INSN_LUID (i2);
3265 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3266 copy of I2SRC each time we substitute it, in order to avoid creating
3267 self-referential RTL when we will be substituting I1SRC for I1DEST
3268 later. Likewise if I0 feeds into I2, either directly or indirectly
3269 through I1, and I0DEST is in I0SRC. */
3270 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3271 (i1_feeds_i2_n && i1dest_in_i1src)
3272 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3273 && i0dest_in_i0src));
3274 substed_i2 = 1;
3276 /* Record whether I2's body now appears within I3's body. */
3277 i2_is_used = n_occurrences;
3280 /* If we already got a failure, don't try to do more. Otherwise, try to
3281 substitute I1 if we have it. */
3283 if (i1 && GET_CODE (newpat) != CLOBBER)
3285 /* Check that an autoincrement side-effect on I1 has not been lost.
3286 This happens if I1DEST is mentioned in I2 and dies there, and
3287 has disappeared from the new pattern. */
3288 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3289 && i1_feeds_i2_n
3290 && dead_or_set_p (i2, i1dest)
3291 && !reg_overlap_mentioned_p (i1dest, newpat))
3292 /* Before we can do this substitution, we must redo the test done
3293 above (see detailed comments there) that ensures I1DEST isn't
3294 mentioned in any SETs in NEWPAT that are field assignments. */
3295 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3296 0, 0, 0))
3298 undo_all ();
3299 return 0;
3302 n_occurrences = 0;
3303 subst_low_luid = DF_INSN_LUID (i1);
3305 /* If the following substitution will modify I1SRC, make a copy of it
3306 for the case where it is substituted for I1DEST in I2PAT later. */
3307 if (added_sets_2 && i1_feeds_i2_n)
3308 i1src_copy = copy_rtx (i1src);
3310 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3311 copy of I1SRC each time we substitute it, in order to avoid creating
3312 self-referential RTL when we will be substituting I0SRC for I0DEST
3313 later. */
3314 newpat = subst (newpat, i1dest, i1src, 0, 0,
3315 i0_feeds_i1_n && i0dest_in_i0src);
3316 substed_i1 = 1;
3318 /* Record whether I1's body now appears within I3's body. */
3319 i1_is_used = n_occurrences;
3322 /* Likewise for I0 if we have it. */
3324 if (i0 && GET_CODE (newpat) != CLOBBER)
3326 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3327 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3328 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3329 && !reg_overlap_mentioned_p (i0dest, newpat))
3330 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3331 0, 0, 0))
3333 undo_all ();
3334 return 0;
3337 /* If the following substitution will modify I0SRC, make a copy of it
3338 for the case where it is substituted for I0DEST in I1PAT later. */
3339 if (added_sets_1 && i0_feeds_i1_n)
3340 i0src_copy = copy_rtx (i0src);
3341 /* And a copy for I0DEST in I2PAT substitution. */
3342 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3343 || (i0_feeds_i2_n)))
3344 i0src_copy2 = copy_rtx (i0src);
3346 n_occurrences = 0;
3347 subst_low_luid = DF_INSN_LUID (i0);
3348 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3349 substed_i0 = 1;
3352 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3353 to count all the ways that I2SRC and I1SRC can be used. */
3354 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3355 && i2_is_used + added_sets_2 > 1)
3356 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3357 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3358 > 1))
3359 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3360 && (n_occurrences + added_sets_0
3361 + (added_sets_1 && i0_feeds_i1_n)
3362 + (added_sets_2 && i0_feeds_i2_n)
3363 > 1))
3364 /* Fail if we tried to make a new register. */
3365 || max_reg_num () != maxreg
3366 /* Fail if we couldn't do something and have a CLOBBER. */
3367 || GET_CODE (newpat) == CLOBBER
3368 /* Fail if this new pattern is a MULT and we didn't have one before
3369 at the outer level. */
3370 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3371 && ! have_mult))
3373 undo_all ();
3374 return 0;
3377 /* If the actions of the earlier insns must be kept
3378 in addition to substituting them into the latest one,
3379 we must make a new PARALLEL for the latest insn
3380 to hold additional the SETs. */
3382 if (added_sets_0 || added_sets_1 || added_sets_2)
3384 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3385 combine_extras++;
3387 if (GET_CODE (newpat) == PARALLEL)
3389 rtvec old = XVEC (newpat, 0);
3390 total_sets = XVECLEN (newpat, 0) + extra_sets;
3391 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3392 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3393 sizeof (old->elem[0]) * old->num_elem);
3395 else
3397 rtx old = newpat;
3398 total_sets = 1 + extra_sets;
3399 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3400 XVECEXP (newpat, 0, 0) = old;
3403 if (added_sets_0)
3404 XVECEXP (newpat, 0, --total_sets) = i0pat;
3406 if (added_sets_1)
3408 rtx t = i1pat;
3409 if (i0_feeds_i1_n)
3410 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3412 XVECEXP (newpat, 0, --total_sets) = t;
3414 if (added_sets_2)
3416 rtx t = i2pat;
3417 if (i1_feeds_i2_n)
3418 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3419 i0_feeds_i1_n && i0dest_in_i0src);
3420 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3421 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3423 XVECEXP (newpat, 0, --total_sets) = t;
3427 validate_replacement:
3429 /* Note which hard regs this insn has as inputs. */
3430 mark_used_regs_combine (newpat);
3432 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3433 consider splitting this pattern, we might need these clobbers. */
3434 if (i1 && GET_CODE (newpat) == PARALLEL
3435 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3437 int len = XVECLEN (newpat, 0);
3439 newpat_vec_with_clobbers = rtvec_alloc (len);
3440 for (i = 0; i < len; i++)
3441 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3444 /* We have recognized nothing yet. */
3445 insn_code_number = -1;
3447 /* See if this is a PARALLEL of two SETs where one SET's destination is
3448 a register that is unused and this isn't marked as an instruction that
3449 might trap in an EH region. In that case, we just need the other SET.
3450 We prefer this over the PARALLEL.
3452 This can occur when simplifying a divmod insn. We *must* test for this
3453 case here because the code below that splits two independent SETs doesn't
3454 handle this case correctly when it updates the register status.
3456 It's pointless doing this if we originally had two sets, one from
3457 i3, and one from i2. Combining then splitting the parallel results
3458 in the original i2 again plus an invalid insn (which we delete).
3459 The net effect is only to move instructions around, which makes
3460 debug info less accurate. */
3462 if (!(added_sets_2 && i1 == 0)
3463 && GET_CODE (newpat) == PARALLEL
3464 && XVECLEN (newpat, 0) == 2
3465 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3466 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3467 && asm_noperands (newpat) < 0)
3469 rtx set0 = XVECEXP (newpat, 0, 0);
3470 rtx set1 = XVECEXP (newpat, 0, 1);
3471 rtx oldpat = newpat;
3473 if (((REG_P (SET_DEST (set1))
3474 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3475 || (GET_CODE (SET_DEST (set1)) == SUBREG
3476 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3477 && insn_nothrow_p (i3)
3478 && !side_effects_p (SET_SRC (set1)))
3480 newpat = set0;
3481 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3484 else if (((REG_P (SET_DEST (set0))
3485 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3486 || (GET_CODE (SET_DEST (set0)) == SUBREG
3487 && find_reg_note (i3, REG_UNUSED,
3488 SUBREG_REG (SET_DEST (set0)))))
3489 && insn_nothrow_p (i3)
3490 && !side_effects_p (SET_SRC (set0)))
3492 newpat = set1;
3493 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3495 if (insn_code_number >= 0)
3496 changed_i3_dest = 1;
3499 if (insn_code_number < 0)
3500 newpat = oldpat;
3503 /* Is the result of combination a valid instruction? */
3504 if (insn_code_number < 0)
3505 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3507 /* If we were combining three insns and the result is a simple SET
3508 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3509 insns. There are two ways to do this. It can be split using a
3510 machine-specific method (like when you have an addition of a large
3511 constant) or by combine in the function find_split_point. */
3513 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3514 && asm_noperands (newpat) < 0)
3516 rtx parallel, *split;
3517 rtx_insn *m_split_insn;
3519 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3520 use I2DEST as a scratch register will help. In the latter case,
3521 convert I2DEST to the mode of the source of NEWPAT if we can. */
3523 m_split_insn = combine_split_insns (newpat, i3);
3525 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3526 inputs of NEWPAT. */
3528 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3529 possible to try that as a scratch reg. This would require adding
3530 more code to make it work though. */
3532 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3534 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3536 /* First try to split using the original register as a
3537 scratch register. */
3538 parallel = gen_rtx_PARALLEL (VOIDmode,
3539 gen_rtvec (2, newpat,
3540 gen_rtx_CLOBBER (VOIDmode,
3541 i2dest)));
3542 m_split_insn = combine_split_insns (parallel, i3);
3544 /* If that didn't work, try changing the mode of I2DEST if
3545 we can. */
3546 if (m_split_insn == 0
3547 && new_mode != GET_MODE (i2dest)
3548 && new_mode != VOIDmode
3549 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3551 machine_mode old_mode = GET_MODE (i2dest);
3552 rtx ni2dest;
3554 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3555 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3556 else
3558 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3559 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3562 parallel = (gen_rtx_PARALLEL
3563 (VOIDmode,
3564 gen_rtvec (2, newpat,
3565 gen_rtx_CLOBBER (VOIDmode,
3566 ni2dest))));
3567 m_split_insn = combine_split_insns (parallel, i3);
3569 if (m_split_insn == 0
3570 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3572 struct undo *buf;
3574 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3575 buf = undobuf.undos;
3576 undobuf.undos = buf->next;
3577 buf->next = undobuf.frees;
3578 undobuf.frees = buf;
3582 i2scratch = m_split_insn != 0;
3585 /* If recog_for_combine has discarded clobbers, try to use them
3586 again for the split. */
3587 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3589 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3590 m_split_insn = combine_split_insns (parallel, i3);
3593 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3595 rtx m_split_pat = PATTERN (m_split_insn);
3596 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3597 if (insn_code_number >= 0)
3598 newpat = m_split_pat;
3600 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3601 && (next_nonnote_nondebug_insn (i2) == i3
3602 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3604 rtx i2set, i3set;
3605 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3606 newi2pat = PATTERN (m_split_insn);
3608 i3set = single_set (NEXT_INSN (m_split_insn));
3609 i2set = single_set (m_split_insn);
3611 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3613 /* If I2 or I3 has multiple SETs, we won't know how to track
3614 register status, so don't use these insns. If I2's destination
3615 is used between I2 and I3, we also can't use these insns. */
3617 if (i2_code_number >= 0 && i2set && i3set
3618 && (next_nonnote_nondebug_insn (i2) == i3
3619 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3620 insn_code_number = recog_for_combine (&newi3pat, i3,
3621 &new_i3_notes);
3622 if (insn_code_number >= 0)
3623 newpat = newi3pat;
3625 /* It is possible that both insns now set the destination of I3.
3626 If so, we must show an extra use of it. */
3628 if (insn_code_number >= 0)
3630 rtx new_i3_dest = SET_DEST (i3set);
3631 rtx new_i2_dest = SET_DEST (i2set);
3633 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3634 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3635 || GET_CODE (new_i3_dest) == SUBREG)
3636 new_i3_dest = XEXP (new_i3_dest, 0);
3638 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3639 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3640 || GET_CODE (new_i2_dest) == SUBREG)
3641 new_i2_dest = XEXP (new_i2_dest, 0);
3643 if (REG_P (new_i3_dest)
3644 && REG_P (new_i2_dest)
3645 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3646 && REGNO (new_i2_dest) < reg_n_sets_max)
3647 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3651 /* If we can split it and use I2DEST, go ahead and see if that
3652 helps things be recognized. Verify that none of the registers
3653 are set between I2 and I3. */
3654 if (insn_code_number < 0
3655 && (split = find_split_point (&newpat, i3, false)) != 0
3656 #ifdef HAVE_cc0
3657 && REG_P (i2dest)
3658 #endif
3659 /* We need I2DEST in the proper mode. If it is a hard register
3660 or the only use of a pseudo, we can change its mode.
3661 Make sure we don't change a hard register to have a mode that
3662 isn't valid for it, or change the number of registers. */
3663 && (GET_MODE (*split) == GET_MODE (i2dest)
3664 || GET_MODE (*split) == VOIDmode
3665 || can_change_dest_mode (i2dest, added_sets_2,
3666 GET_MODE (*split)))
3667 && (next_nonnote_nondebug_insn (i2) == i3
3668 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3669 /* We can't overwrite I2DEST if its value is still used by
3670 NEWPAT. */
3671 && ! reg_referenced_p (i2dest, newpat))
3673 rtx newdest = i2dest;
3674 enum rtx_code split_code = GET_CODE (*split);
3675 machine_mode split_mode = GET_MODE (*split);
3676 bool subst_done = false;
3677 newi2pat = NULL_RTX;
3679 i2scratch = true;
3681 /* *SPLIT may be part of I2SRC, so make sure we have the
3682 original expression around for later debug processing.
3683 We should not need I2SRC any more in other cases. */
3684 if (MAY_HAVE_DEBUG_INSNS)
3685 i2src = copy_rtx (i2src);
3686 else
3687 i2src = NULL;
3689 /* Get NEWDEST as a register in the proper mode. We have already
3690 validated that we can do this. */
3691 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3693 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3694 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3695 else
3697 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3698 newdest = regno_reg_rtx[REGNO (i2dest)];
3702 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3703 an ASHIFT. This can occur if it was inside a PLUS and hence
3704 appeared to be a memory address. This is a kludge. */
3705 if (split_code == MULT
3706 && CONST_INT_P (XEXP (*split, 1))
3707 && INTVAL (XEXP (*split, 1)) > 0
3708 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3710 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3711 XEXP (*split, 0), GEN_INT (i)));
3712 /* Update split_code because we may not have a multiply
3713 anymore. */
3714 split_code = GET_CODE (*split);
3717 #ifdef INSN_SCHEDULING
3718 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3719 be written as a ZERO_EXTEND. */
3720 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3722 #ifdef LOAD_EXTEND_OP
3723 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3724 what it really is. */
3725 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3726 == SIGN_EXTEND)
3727 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3728 SUBREG_REG (*split)));
3729 else
3730 #endif
3731 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3732 SUBREG_REG (*split)));
3734 #endif
3736 /* Attempt to split binary operators using arithmetic identities. */
3737 if (BINARY_P (SET_SRC (newpat))
3738 && split_mode == GET_MODE (SET_SRC (newpat))
3739 && ! side_effects_p (SET_SRC (newpat)))
3741 rtx setsrc = SET_SRC (newpat);
3742 machine_mode mode = GET_MODE (setsrc);
3743 enum rtx_code code = GET_CODE (setsrc);
3744 rtx src_op0 = XEXP (setsrc, 0);
3745 rtx src_op1 = XEXP (setsrc, 1);
3747 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3748 if (rtx_equal_p (src_op0, src_op1))
3750 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3751 SUBST (XEXP (setsrc, 0), newdest);
3752 SUBST (XEXP (setsrc, 1), newdest);
3753 subst_done = true;
3755 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3756 else if ((code == PLUS || code == MULT)
3757 && GET_CODE (src_op0) == code
3758 && GET_CODE (XEXP (src_op0, 0)) == code
3759 && (INTEGRAL_MODE_P (mode)
3760 || (FLOAT_MODE_P (mode)
3761 && flag_unsafe_math_optimizations)))
3763 rtx p = XEXP (XEXP (src_op0, 0), 0);
3764 rtx q = XEXP (XEXP (src_op0, 0), 1);
3765 rtx r = XEXP (src_op0, 1);
3766 rtx s = src_op1;
3768 /* Split both "((X op Y) op X) op Y" and
3769 "((X op Y) op Y) op X" as "T op T" where T is
3770 "X op Y". */
3771 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3772 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3774 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3775 XEXP (src_op0, 0));
3776 SUBST (XEXP (setsrc, 0), newdest);
3777 SUBST (XEXP (setsrc, 1), newdest);
3778 subst_done = true;
3780 /* Split "((X op X) op Y) op Y)" as "T op T" where
3781 T is "X op Y". */
3782 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3784 rtx tmp = simplify_gen_binary (code, mode, p, r);
3785 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3786 SUBST (XEXP (setsrc, 0), newdest);
3787 SUBST (XEXP (setsrc, 1), newdest);
3788 subst_done = true;
3793 if (!subst_done)
3795 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3796 SUBST (*split, newdest);
3799 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3801 /* recog_for_combine might have added CLOBBERs to newi2pat.
3802 Make sure NEWPAT does not depend on the clobbered regs. */
3803 if (GET_CODE (newi2pat) == PARALLEL)
3804 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3805 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3807 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3808 if (reg_overlap_mentioned_p (reg, newpat))
3810 undo_all ();
3811 return 0;
3815 /* If the split point was a MULT and we didn't have one before,
3816 don't use one now. */
3817 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3818 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3822 /* Check for a case where we loaded from memory in a narrow mode and
3823 then sign extended it, but we need both registers. In that case,
3824 we have a PARALLEL with both loads from the same memory location.
3825 We can split this into a load from memory followed by a register-register
3826 copy. This saves at least one insn, more if register allocation can
3827 eliminate the copy.
3829 We cannot do this if the destination of the first assignment is a
3830 condition code register or cc0. We eliminate this case by making sure
3831 the SET_DEST and SET_SRC have the same mode.
3833 We cannot do this if the destination of the second assignment is
3834 a register that we have already assumed is zero-extended. Similarly
3835 for a SUBREG of such a register. */
3837 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3838 && GET_CODE (newpat) == PARALLEL
3839 && XVECLEN (newpat, 0) == 2
3840 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3841 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3842 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3843 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3844 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3845 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3846 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3847 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3848 DF_INSN_LUID (i2))
3849 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3850 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3851 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3852 (REG_P (temp_expr)
3853 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3854 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3855 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3856 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3857 != GET_MODE_MASK (word_mode))))
3858 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3859 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3860 (REG_P (temp_expr)
3861 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3862 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3863 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3864 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3865 != GET_MODE_MASK (word_mode)))))
3866 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3867 SET_SRC (XVECEXP (newpat, 0, 1)))
3868 && ! find_reg_note (i3, REG_UNUSED,
3869 SET_DEST (XVECEXP (newpat, 0, 0))))
3871 rtx ni2dest;
3873 newi2pat = XVECEXP (newpat, 0, 0);
3874 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3875 newpat = XVECEXP (newpat, 0, 1);
3876 SUBST (SET_SRC (newpat),
3877 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3878 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3880 if (i2_code_number >= 0)
3881 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3883 if (insn_code_number >= 0)
3884 swap_i2i3 = 1;
3887 /* Similarly, check for a case where we have a PARALLEL of two independent
3888 SETs but we started with three insns. In this case, we can do the sets
3889 as two separate insns. This case occurs when some SET allows two
3890 other insns to combine, but the destination of that SET is still live.
3892 Also do this if we started with two insns and (at least) one of the
3893 resulting sets is a noop; this noop will be deleted later. */
3895 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3896 && GET_CODE (newpat) == PARALLEL
3897 && XVECLEN (newpat, 0) == 2
3898 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3899 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3900 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3901 || set_noop_p (XVECEXP (newpat, 0, 1)))
3902 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3903 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3904 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3905 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3906 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3907 XVECEXP (newpat, 0, 0))
3908 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3909 XVECEXP (newpat, 0, 1))
3910 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3911 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3913 rtx set0 = XVECEXP (newpat, 0, 0);
3914 rtx set1 = XVECEXP (newpat, 0, 1);
3916 /* Normally, it doesn't matter which of the two is done first,
3917 but the one that references cc0 can't be the second, and
3918 one which uses any regs/memory set in between i2 and i3 can't
3919 be first. The PARALLEL might also have been pre-existing in i3,
3920 so we need to make sure that we won't wrongly hoist a SET to i2
3921 that would conflict with a death note present in there. */
3922 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3923 && !(REG_P (SET_DEST (set1))
3924 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3925 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3926 && find_reg_note (i2, REG_DEAD,
3927 SUBREG_REG (SET_DEST (set1))))
3928 #ifdef HAVE_cc0
3929 && !reg_referenced_p (cc0_rtx, set0)
3930 #endif
3931 /* If I3 is a jump, ensure that set0 is a jump so that
3932 we do not create invalid RTL. */
3933 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3936 newi2pat = set1;
3937 newpat = set0;
3939 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3940 && !(REG_P (SET_DEST (set0))
3941 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3942 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3943 && find_reg_note (i2, REG_DEAD,
3944 SUBREG_REG (SET_DEST (set0))))
3945 #ifdef HAVE_cc0
3946 && !reg_referenced_p (cc0_rtx, set1)
3947 #endif
3948 /* If I3 is a jump, ensure that set1 is a jump so that
3949 we do not create invalid RTL. */
3950 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3953 newi2pat = set0;
3954 newpat = set1;
3956 else
3958 undo_all ();
3959 return 0;
3962 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3964 if (i2_code_number >= 0)
3966 /* recog_for_combine might have added CLOBBERs to newi2pat.
3967 Make sure NEWPAT does not depend on the clobbered regs. */
3968 if (GET_CODE (newi2pat) == PARALLEL)
3970 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3971 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3973 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3974 if (reg_overlap_mentioned_p (reg, newpat))
3976 undo_all ();
3977 return 0;
3982 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3986 /* If it still isn't recognized, fail and change things back the way they
3987 were. */
3988 if ((insn_code_number < 0
3989 /* Is the result a reasonable ASM_OPERANDS? */
3990 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3992 undo_all ();
3993 return 0;
3996 /* If we had to change another insn, make sure it is valid also. */
3997 if (undobuf.other_insn)
3999 CLEAR_HARD_REG_SET (newpat_used_regs);
4001 other_pat = PATTERN (undobuf.other_insn);
4002 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4003 &new_other_notes);
4005 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4007 undo_all ();
4008 return 0;
4012 #ifdef HAVE_cc0
4013 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4014 they are adjacent to each other or not. */
4016 rtx_insn *p = prev_nonnote_insn (i3);
4017 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4018 && sets_cc0_p (newi2pat))
4020 undo_all ();
4021 return 0;
4024 #endif
4026 /* Only allow this combination if insn_rtx_costs reports that the
4027 replacement instructions are cheaper than the originals. */
4028 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4030 undo_all ();
4031 return 0;
4034 if (MAY_HAVE_DEBUG_INSNS)
4036 struct undo *undo;
4038 for (undo = undobuf.undos; undo; undo = undo->next)
4039 if (undo->kind == UNDO_MODE)
4041 rtx reg = *undo->where.r;
4042 machine_mode new_mode = GET_MODE (reg);
4043 machine_mode old_mode = undo->old_contents.m;
4045 /* Temporarily revert mode back. */
4046 adjust_reg_mode (reg, old_mode);
4048 if (reg == i2dest && i2scratch)
4050 /* If we used i2dest as a scratch register with a
4051 different mode, substitute it for the original
4052 i2src while its original mode is temporarily
4053 restored, and then clear i2scratch so that we don't
4054 do it again later. */
4055 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4056 this_basic_block);
4057 i2scratch = false;
4058 /* Put back the new mode. */
4059 adjust_reg_mode (reg, new_mode);
4061 else
4063 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4064 rtx_insn *first, *last;
4066 if (reg == i2dest)
4068 first = i2;
4069 last = last_combined_insn;
4071 else
4073 first = i3;
4074 last = undobuf.other_insn;
4075 gcc_assert (last);
4076 if (DF_INSN_LUID (last)
4077 < DF_INSN_LUID (last_combined_insn))
4078 last = last_combined_insn;
4081 /* We're dealing with a reg that changed mode but not
4082 meaning, so we want to turn it into a subreg for
4083 the new mode. However, because of REG sharing and
4084 because its mode had already changed, we have to do
4085 it in two steps. First, replace any debug uses of
4086 reg, with its original mode temporarily restored,
4087 with this copy we have created; then, replace the
4088 copy with the SUBREG of the original shared reg,
4089 once again changed to the new mode. */
4090 propagate_for_debug (first, last, reg, tempreg,
4091 this_basic_block);
4092 adjust_reg_mode (reg, new_mode);
4093 propagate_for_debug (first, last, tempreg,
4094 lowpart_subreg (old_mode, reg, new_mode),
4095 this_basic_block);
4100 /* If we will be able to accept this, we have made a
4101 change to the destination of I3. This requires us to
4102 do a few adjustments. */
4104 if (changed_i3_dest)
4106 PATTERN (i3) = newpat;
4107 adjust_for_new_dest (i3);
4110 /* We now know that we can do this combination. Merge the insns and
4111 update the status of registers and LOG_LINKS. */
4113 if (undobuf.other_insn)
4115 rtx note, next;
4117 PATTERN (undobuf.other_insn) = other_pat;
4119 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4120 ensure that they are still valid. Then add any non-duplicate
4121 notes added by recog_for_combine. */
4122 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4124 next = XEXP (note, 1);
4126 if ((REG_NOTE_KIND (note) == REG_DEAD
4127 && !reg_referenced_p (XEXP (note, 0),
4128 PATTERN (undobuf.other_insn)))
4129 ||(REG_NOTE_KIND (note) == REG_UNUSED
4130 && !reg_set_p (XEXP (note, 0),
4131 PATTERN (undobuf.other_insn))))
4132 remove_note (undobuf.other_insn, note);
4135 distribute_notes (new_other_notes, undobuf.other_insn,
4136 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4137 NULL_RTX);
4140 if (swap_i2i3)
4142 rtx_insn *insn;
4143 struct insn_link *link;
4144 rtx ni2dest;
4146 /* I3 now uses what used to be its destination and which is now
4147 I2's destination. This requires us to do a few adjustments. */
4148 PATTERN (i3) = newpat;
4149 adjust_for_new_dest (i3);
4151 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4152 so we still will.
4154 However, some later insn might be using I2's dest and have
4155 a LOG_LINK pointing at I3. We must remove this link.
4156 The simplest way to remove the link is to point it at I1,
4157 which we know will be a NOTE. */
4159 /* newi2pat is usually a SET here; however, recog_for_combine might
4160 have added some clobbers. */
4161 if (GET_CODE (newi2pat) == PARALLEL)
4162 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4163 else
4164 ni2dest = SET_DEST (newi2pat);
4166 for (insn = NEXT_INSN (i3);
4167 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4168 || insn != BB_HEAD (this_basic_block->next_bb));
4169 insn = NEXT_INSN (insn))
4171 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4173 FOR_EACH_LOG_LINK (link, insn)
4174 if (link->insn == i3)
4175 link->insn = i1;
4177 break;
4183 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4184 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4185 rtx midnotes = 0;
4186 int from_luid;
4187 /* Compute which registers we expect to eliminate. newi2pat may be setting
4188 either i3dest or i2dest, so we must check it. */
4189 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4190 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4191 || !i2dest_killed
4192 ? 0 : i2dest);
4193 /* For i1, we need to compute both local elimination and global
4194 elimination information with respect to newi2pat because i1dest
4195 may be the same as i3dest, in which case newi2pat may be setting
4196 i1dest. Global information is used when distributing REG_DEAD
4197 note for i2 and i3, in which case it does matter if newi2pat sets
4198 i1dest or not.
4200 Local information is used when distributing REG_DEAD note for i1,
4201 in which case it doesn't matter if newi2pat sets i1dest or not.
4202 See PR62151, if we have four insns combination:
4203 i0: r0 <- i0src
4204 i1: r1 <- i1src (using r0)
4205 REG_DEAD (r0)
4206 i2: r0 <- i2src (using r1)
4207 i3: r3 <- i3src (using r0)
4208 ix: using r0
4209 From i1's point of view, r0 is eliminated, no matter if it is set
4210 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4211 should be discarded.
4213 Note local information only affects cases in forms like "I1->I2->I3",
4214 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4215 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4216 i0dest anyway. */
4217 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4218 || !i1dest_killed
4219 ? 0 : i1dest);
4220 rtx elim_i1 = (local_elim_i1 == 0
4221 || (newi2pat && reg_set_p (i1dest, newi2pat))
4222 ? 0 : i1dest);
4223 /* Same case as i1. */
4224 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4225 ? 0 : i0dest);
4226 rtx elim_i0 = (local_elim_i0 == 0
4227 || (newi2pat && reg_set_p (i0dest, newi2pat))
4228 ? 0 : i0dest);
4230 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4231 clear them. */
4232 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4233 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4234 if (i1)
4235 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4236 if (i0)
4237 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4239 /* Ensure that we do not have something that should not be shared but
4240 occurs multiple times in the new insns. Check this by first
4241 resetting all the `used' flags and then copying anything is shared. */
4243 reset_used_flags (i3notes);
4244 reset_used_flags (i2notes);
4245 reset_used_flags (i1notes);
4246 reset_used_flags (i0notes);
4247 reset_used_flags (newpat);
4248 reset_used_flags (newi2pat);
4249 if (undobuf.other_insn)
4250 reset_used_flags (PATTERN (undobuf.other_insn));
4252 i3notes = copy_rtx_if_shared (i3notes);
4253 i2notes = copy_rtx_if_shared (i2notes);
4254 i1notes = copy_rtx_if_shared (i1notes);
4255 i0notes = copy_rtx_if_shared (i0notes);
4256 newpat = copy_rtx_if_shared (newpat);
4257 newi2pat = copy_rtx_if_shared (newi2pat);
4258 if (undobuf.other_insn)
4259 reset_used_flags (PATTERN (undobuf.other_insn));
4261 INSN_CODE (i3) = insn_code_number;
4262 PATTERN (i3) = newpat;
4264 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4266 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4268 reset_used_flags (call_usage);
4269 call_usage = copy_rtx (call_usage);
4271 if (substed_i2)
4273 /* I2SRC must still be meaningful at this point. Some splitting
4274 operations can invalidate I2SRC, but those operations do not
4275 apply to calls. */
4276 gcc_assert (i2src);
4277 replace_rtx (call_usage, i2dest, i2src);
4280 if (substed_i1)
4281 replace_rtx (call_usage, i1dest, i1src);
4282 if (substed_i0)
4283 replace_rtx (call_usage, i0dest, i0src);
4285 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4288 if (undobuf.other_insn)
4289 INSN_CODE (undobuf.other_insn) = other_code_number;
4291 /* We had one special case above where I2 had more than one set and
4292 we replaced a destination of one of those sets with the destination
4293 of I3. In that case, we have to update LOG_LINKS of insns later
4294 in this basic block. Note that this (expensive) case is rare.
4296 Also, in this case, we must pretend that all REG_NOTEs for I2
4297 actually came from I3, so that REG_UNUSED notes from I2 will be
4298 properly handled. */
4300 if (i3_subst_into_i2)
4302 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4303 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4304 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4305 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4306 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4307 && ! find_reg_note (i2, REG_UNUSED,
4308 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4309 for (temp_insn = NEXT_INSN (i2);
4310 temp_insn
4311 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4312 || BB_HEAD (this_basic_block) != temp_insn);
4313 temp_insn = NEXT_INSN (temp_insn))
4314 if (temp_insn != i3 && INSN_P (temp_insn))
4315 FOR_EACH_LOG_LINK (link, temp_insn)
4316 if (link->insn == i2)
4317 link->insn = i3;
4319 if (i3notes)
4321 rtx link = i3notes;
4322 while (XEXP (link, 1))
4323 link = XEXP (link, 1);
4324 XEXP (link, 1) = i2notes;
4326 else
4327 i3notes = i2notes;
4328 i2notes = 0;
4331 LOG_LINKS (i3) = NULL;
4332 REG_NOTES (i3) = 0;
4333 LOG_LINKS (i2) = NULL;
4334 REG_NOTES (i2) = 0;
4336 if (newi2pat)
4338 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4339 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4340 this_basic_block);
4341 INSN_CODE (i2) = i2_code_number;
4342 PATTERN (i2) = newi2pat;
4344 else
4346 if (MAY_HAVE_DEBUG_INSNS && i2src)
4347 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4348 this_basic_block);
4349 SET_INSN_DELETED (i2);
4352 if (i1)
4354 LOG_LINKS (i1) = NULL;
4355 REG_NOTES (i1) = 0;
4356 if (MAY_HAVE_DEBUG_INSNS)
4357 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4358 this_basic_block);
4359 SET_INSN_DELETED (i1);
4362 if (i0)
4364 LOG_LINKS (i0) = NULL;
4365 REG_NOTES (i0) = 0;
4366 if (MAY_HAVE_DEBUG_INSNS)
4367 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4368 this_basic_block);
4369 SET_INSN_DELETED (i0);
4372 /* Get death notes for everything that is now used in either I3 or
4373 I2 and used to die in a previous insn. If we built two new
4374 patterns, move from I1 to I2 then I2 to I3 so that we get the
4375 proper movement on registers that I2 modifies. */
4377 if (i0)
4378 from_luid = DF_INSN_LUID (i0);
4379 else if (i1)
4380 from_luid = DF_INSN_LUID (i1);
4381 else
4382 from_luid = DF_INSN_LUID (i2);
4383 if (newi2pat)
4384 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4385 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4387 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4388 if (i3notes)
4389 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4390 elim_i2, elim_i1, elim_i0);
4391 if (i2notes)
4392 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4393 elim_i2, elim_i1, elim_i0);
4394 if (i1notes)
4395 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4396 elim_i2, local_elim_i1, local_elim_i0);
4397 if (i0notes)
4398 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4399 elim_i2, elim_i1, local_elim_i0);
4400 if (midnotes)
4401 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4402 elim_i2, elim_i1, elim_i0);
4404 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4405 know these are REG_UNUSED and want them to go to the desired insn,
4406 so we always pass it as i3. */
4408 if (newi2pat && new_i2_notes)
4409 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4410 NULL_RTX);
4412 if (new_i3_notes)
4413 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4414 NULL_RTX);
4416 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4417 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4418 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4419 in that case, it might delete I2. Similarly for I2 and I1.
4420 Show an additional death due to the REG_DEAD note we make here. If
4421 we discard it in distribute_notes, we will decrement it again. */
4423 if (i3dest_killed)
4425 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4426 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4427 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4428 elim_i1, elim_i0);
4429 else
4430 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4431 elim_i2, elim_i1, elim_i0);
4434 if (i2dest_in_i2src)
4436 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4437 if (newi2pat && reg_set_p (i2dest, newi2pat))
4438 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4439 NULL_RTX, NULL_RTX);
4440 else
4441 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4442 NULL_RTX, NULL_RTX, NULL_RTX);
4445 if (i1dest_in_i1src)
4447 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4448 if (newi2pat && reg_set_p (i1dest, newi2pat))
4449 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4450 NULL_RTX, NULL_RTX);
4451 else
4452 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4453 NULL_RTX, NULL_RTX, NULL_RTX);
4456 if (i0dest_in_i0src)
4458 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4459 if (newi2pat && reg_set_p (i0dest, newi2pat))
4460 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4461 NULL_RTX, NULL_RTX);
4462 else
4463 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4464 NULL_RTX, NULL_RTX, NULL_RTX);
4467 distribute_links (i3links);
4468 distribute_links (i2links);
4469 distribute_links (i1links);
4470 distribute_links (i0links);
4472 if (REG_P (i2dest))
4474 struct insn_link *link;
4475 rtx_insn *i2_insn = 0;
4476 rtx i2_val = 0, set;
4478 /* The insn that used to set this register doesn't exist, and
4479 this life of the register may not exist either. See if one of
4480 I3's links points to an insn that sets I2DEST. If it does,
4481 that is now the last known value for I2DEST. If we don't update
4482 this and I2 set the register to a value that depended on its old
4483 contents, we will get confused. If this insn is used, thing
4484 will be set correctly in combine_instructions. */
4485 FOR_EACH_LOG_LINK (link, i3)
4486 if ((set = single_set (link->insn)) != 0
4487 && rtx_equal_p (i2dest, SET_DEST (set)))
4488 i2_insn = link->insn, i2_val = SET_SRC (set);
4490 record_value_for_reg (i2dest, i2_insn, i2_val);
4492 /* If the reg formerly set in I2 died only once and that was in I3,
4493 zero its use count so it won't make `reload' do any work. */
4494 if (! added_sets_2
4495 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4496 && ! i2dest_in_i2src
4497 && REGNO (i2dest) < reg_n_sets_max)
4498 INC_REG_N_SETS (REGNO (i2dest), -1);
4501 if (i1 && REG_P (i1dest))
4503 struct insn_link *link;
4504 rtx_insn *i1_insn = 0;
4505 rtx i1_val = 0, set;
4507 FOR_EACH_LOG_LINK (link, i3)
4508 if ((set = single_set (link->insn)) != 0
4509 && rtx_equal_p (i1dest, SET_DEST (set)))
4510 i1_insn = link->insn, i1_val = SET_SRC (set);
4512 record_value_for_reg (i1dest, i1_insn, i1_val);
4514 if (! added_sets_1
4515 && ! i1dest_in_i1src
4516 && REGNO (i1dest) < reg_n_sets_max)
4517 INC_REG_N_SETS (REGNO (i1dest), -1);
4520 if (i0 && REG_P (i0dest))
4522 struct insn_link *link;
4523 rtx_insn *i0_insn = 0;
4524 rtx i0_val = 0, set;
4526 FOR_EACH_LOG_LINK (link, i3)
4527 if ((set = single_set (link->insn)) != 0
4528 && rtx_equal_p (i0dest, SET_DEST (set)))
4529 i0_insn = link->insn, i0_val = SET_SRC (set);
4531 record_value_for_reg (i0dest, i0_insn, i0_val);
4533 if (! added_sets_0
4534 && ! i0dest_in_i0src
4535 && REGNO (i0dest) < reg_n_sets_max)
4536 INC_REG_N_SETS (REGNO (i0dest), -1);
4539 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4540 been made to this insn. The order is important, because newi2pat
4541 can affect nonzero_bits of newpat. */
4542 if (newi2pat)
4543 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4544 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4547 if (undobuf.other_insn != NULL_RTX)
4549 if (dump_file)
4551 fprintf (dump_file, "modifying other_insn ");
4552 dump_insn_slim (dump_file, undobuf.other_insn);
4554 df_insn_rescan (undobuf.other_insn);
4557 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4559 if (dump_file)
4561 fprintf (dump_file, "modifying insn i0 ");
4562 dump_insn_slim (dump_file, i0);
4564 df_insn_rescan (i0);
4567 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4569 if (dump_file)
4571 fprintf (dump_file, "modifying insn i1 ");
4572 dump_insn_slim (dump_file, i1);
4574 df_insn_rescan (i1);
4577 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4579 if (dump_file)
4581 fprintf (dump_file, "modifying insn i2 ");
4582 dump_insn_slim (dump_file, i2);
4584 df_insn_rescan (i2);
4587 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4589 if (dump_file)
4591 fprintf (dump_file, "modifying insn i3 ");
4592 dump_insn_slim (dump_file, i3);
4594 df_insn_rescan (i3);
4597 /* Set new_direct_jump_p if a new return or simple jump instruction
4598 has been created. Adjust the CFG accordingly. */
4599 if (returnjump_p (i3) || any_uncondjump_p (i3))
4601 *new_direct_jump_p = 1;
4602 mark_jump_label (PATTERN (i3), i3, 0);
4603 update_cfg_for_uncondjump (i3);
4606 if (undobuf.other_insn != NULL_RTX
4607 && (returnjump_p (undobuf.other_insn)
4608 || any_uncondjump_p (undobuf.other_insn)))
4610 *new_direct_jump_p = 1;
4611 update_cfg_for_uncondjump (undobuf.other_insn);
4614 /* A noop might also need cleaning up of CFG, if it comes from the
4615 simplification of a jump. */
4616 if (JUMP_P (i3)
4617 && GET_CODE (newpat) == SET
4618 && SET_SRC (newpat) == pc_rtx
4619 && SET_DEST (newpat) == pc_rtx)
4621 *new_direct_jump_p = 1;
4622 update_cfg_for_uncondjump (i3);
4625 if (undobuf.other_insn != NULL_RTX
4626 && JUMP_P (undobuf.other_insn)
4627 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4628 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4629 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4631 *new_direct_jump_p = 1;
4632 update_cfg_for_uncondjump (undobuf.other_insn);
4635 combine_successes++;
4636 undo_commit ();
4638 if (added_links_insn
4639 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4640 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4641 return added_links_insn;
4642 else
4643 return newi2pat ? i2 : i3;
4646 /* Undo all the modifications recorded in undobuf. */
4648 static void
4649 undo_all (void)
4651 struct undo *undo, *next;
4653 for (undo = undobuf.undos; undo; undo = next)
4655 next = undo->next;
4656 switch (undo->kind)
4658 case UNDO_RTX:
4659 *undo->where.r = undo->old_contents.r;
4660 break;
4661 case UNDO_INT:
4662 *undo->where.i = undo->old_contents.i;
4663 break;
4664 case UNDO_MODE:
4665 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4666 break;
4667 case UNDO_LINKS:
4668 *undo->where.l = undo->old_contents.l;
4669 break;
4670 default:
4671 gcc_unreachable ();
4674 undo->next = undobuf.frees;
4675 undobuf.frees = undo;
4678 undobuf.undos = 0;
4681 /* We've committed to accepting the changes we made. Move all
4682 of the undos to the free list. */
4684 static void
4685 undo_commit (void)
4687 struct undo *undo, *next;
4689 for (undo = undobuf.undos; undo; undo = next)
4691 next = undo->next;
4692 undo->next = undobuf.frees;
4693 undobuf.frees = undo;
4695 undobuf.undos = 0;
4698 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4699 where we have an arithmetic expression and return that point. LOC will
4700 be inside INSN.
4702 try_combine will call this function to see if an insn can be split into
4703 two insns. */
4705 static rtx *
4706 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4708 rtx x = *loc;
4709 enum rtx_code code = GET_CODE (x);
4710 rtx *split;
4711 unsigned HOST_WIDE_INT len = 0;
4712 HOST_WIDE_INT pos = 0;
4713 int unsignedp = 0;
4714 rtx inner = NULL_RTX;
4716 /* First special-case some codes. */
4717 switch (code)
4719 case SUBREG:
4720 #ifdef INSN_SCHEDULING
4721 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4722 point. */
4723 if (MEM_P (SUBREG_REG (x)))
4724 return loc;
4725 #endif
4726 return find_split_point (&SUBREG_REG (x), insn, false);
4728 case MEM:
4729 #ifdef HAVE_lo_sum
4730 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4731 using LO_SUM and HIGH. */
4732 if (GET_CODE (XEXP (x, 0)) == CONST
4733 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4735 machine_mode address_mode = get_address_mode (x);
4737 SUBST (XEXP (x, 0),
4738 gen_rtx_LO_SUM (address_mode,
4739 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4740 XEXP (x, 0)));
4741 return &XEXP (XEXP (x, 0), 0);
4743 #endif
4745 /* If we have a PLUS whose second operand is a constant and the
4746 address is not valid, perhaps will can split it up using
4747 the machine-specific way to split large constants. We use
4748 the first pseudo-reg (one of the virtual regs) as a placeholder;
4749 it will not remain in the result. */
4750 if (GET_CODE (XEXP (x, 0)) == PLUS
4751 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4752 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4753 MEM_ADDR_SPACE (x)))
4755 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4756 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4757 XEXP (x, 0)),
4758 subst_insn);
4760 /* This should have produced two insns, each of which sets our
4761 placeholder. If the source of the second is a valid address,
4762 we can make put both sources together and make a split point
4763 in the middle. */
4765 if (seq
4766 && NEXT_INSN (seq) != NULL_RTX
4767 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4768 && NONJUMP_INSN_P (seq)
4769 && GET_CODE (PATTERN (seq)) == SET
4770 && SET_DEST (PATTERN (seq)) == reg
4771 && ! reg_mentioned_p (reg,
4772 SET_SRC (PATTERN (seq)))
4773 && NONJUMP_INSN_P (NEXT_INSN (seq))
4774 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4775 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4776 && memory_address_addr_space_p
4777 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4778 MEM_ADDR_SPACE (x)))
4780 rtx src1 = SET_SRC (PATTERN (seq));
4781 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4783 /* Replace the placeholder in SRC2 with SRC1. If we can
4784 find where in SRC2 it was placed, that can become our
4785 split point and we can replace this address with SRC2.
4786 Just try two obvious places. */
4788 src2 = replace_rtx (src2, reg, src1);
4789 split = 0;
4790 if (XEXP (src2, 0) == src1)
4791 split = &XEXP (src2, 0);
4792 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4793 && XEXP (XEXP (src2, 0), 0) == src1)
4794 split = &XEXP (XEXP (src2, 0), 0);
4796 if (split)
4798 SUBST (XEXP (x, 0), src2);
4799 return split;
4803 /* If that didn't work, perhaps the first operand is complex and
4804 needs to be computed separately, so make a split point there.
4805 This will occur on machines that just support REG + CONST
4806 and have a constant moved through some previous computation. */
4808 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4809 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4810 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4811 return &XEXP (XEXP (x, 0), 0);
4814 /* If we have a PLUS whose first operand is complex, try computing it
4815 separately by making a split there. */
4816 if (GET_CODE (XEXP (x, 0)) == PLUS
4817 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4818 MEM_ADDR_SPACE (x))
4819 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4820 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4821 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4822 return &XEXP (XEXP (x, 0), 0);
4823 break;
4825 case SET:
4826 #ifdef HAVE_cc0
4827 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4828 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4829 we need to put the operand into a register. So split at that
4830 point. */
4832 if (SET_DEST (x) == cc0_rtx
4833 && GET_CODE (SET_SRC (x)) != COMPARE
4834 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4835 && !OBJECT_P (SET_SRC (x))
4836 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4837 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4838 return &SET_SRC (x);
4839 #endif
4841 /* See if we can split SET_SRC as it stands. */
4842 split = find_split_point (&SET_SRC (x), insn, true);
4843 if (split && split != &SET_SRC (x))
4844 return split;
4846 /* See if we can split SET_DEST as it stands. */
4847 split = find_split_point (&SET_DEST (x), insn, false);
4848 if (split && split != &SET_DEST (x))
4849 return split;
4851 /* See if this is a bitfield assignment with everything constant. If
4852 so, this is an IOR of an AND, so split it into that. */
4853 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4854 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4855 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4856 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4857 && CONST_INT_P (SET_SRC (x))
4858 && ((INTVAL (XEXP (SET_DEST (x), 1))
4859 + INTVAL (XEXP (SET_DEST (x), 2)))
4860 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4861 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4863 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4864 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4865 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4866 rtx dest = XEXP (SET_DEST (x), 0);
4867 machine_mode mode = GET_MODE (dest);
4868 unsigned HOST_WIDE_INT mask
4869 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4870 rtx or_mask;
4872 if (BITS_BIG_ENDIAN)
4873 pos = GET_MODE_PRECISION (mode) - len - pos;
4875 or_mask = gen_int_mode (src << pos, mode);
4876 if (src == mask)
4877 SUBST (SET_SRC (x),
4878 simplify_gen_binary (IOR, mode, dest, or_mask));
4879 else
4881 rtx negmask = gen_int_mode (~(mask << pos), mode);
4882 SUBST (SET_SRC (x),
4883 simplify_gen_binary (IOR, mode,
4884 simplify_gen_binary (AND, mode,
4885 dest, negmask),
4886 or_mask));
4889 SUBST (SET_DEST (x), dest);
4891 split = find_split_point (&SET_SRC (x), insn, true);
4892 if (split && split != &SET_SRC (x))
4893 return split;
4896 /* Otherwise, see if this is an operation that we can split into two.
4897 If so, try to split that. */
4898 code = GET_CODE (SET_SRC (x));
4900 switch (code)
4902 case AND:
4903 /* If we are AND'ing with a large constant that is only a single
4904 bit and the result is only being used in a context where we
4905 need to know if it is zero or nonzero, replace it with a bit
4906 extraction. This will avoid the large constant, which might
4907 have taken more than one insn to make. If the constant were
4908 not a valid argument to the AND but took only one insn to make,
4909 this is no worse, but if it took more than one insn, it will
4910 be better. */
4912 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4913 && REG_P (XEXP (SET_SRC (x), 0))
4914 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4915 && REG_P (SET_DEST (x))
4916 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4917 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4918 && XEXP (*split, 0) == SET_DEST (x)
4919 && XEXP (*split, 1) == const0_rtx)
4921 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4922 XEXP (SET_SRC (x), 0),
4923 pos, NULL_RTX, 1, 1, 0, 0);
4924 if (extraction != 0)
4926 SUBST (SET_SRC (x), extraction);
4927 return find_split_point (loc, insn, false);
4930 break;
4932 case NE:
4933 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4934 is known to be on, this can be converted into a NEG of a shift. */
4935 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4936 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4937 && 1 <= (pos = exact_log2
4938 (nonzero_bits (XEXP (SET_SRC (x), 0),
4939 GET_MODE (XEXP (SET_SRC (x), 0))))))
4941 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4943 SUBST (SET_SRC (x),
4944 gen_rtx_NEG (mode,
4945 gen_rtx_LSHIFTRT (mode,
4946 XEXP (SET_SRC (x), 0),
4947 GEN_INT (pos))));
4949 split = find_split_point (&SET_SRC (x), insn, true);
4950 if (split && split != &SET_SRC (x))
4951 return split;
4953 break;
4955 case SIGN_EXTEND:
4956 inner = XEXP (SET_SRC (x), 0);
4958 /* We can't optimize if either mode is a partial integer
4959 mode as we don't know how many bits are significant
4960 in those modes. */
4961 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4962 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4963 break;
4965 pos = 0;
4966 len = GET_MODE_PRECISION (GET_MODE (inner));
4967 unsignedp = 0;
4968 break;
4970 case SIGN_EXTRACT:
4971 case ZERO_EXTRACT:
4972 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4973 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4975 inner = XEXP (SET_SRC (x), 0);
4976 len = INTVAL (XEXP (SET_SRC (x), 1));
4977 pos = INTVAL (XEXP (SET_SRC (x), 2));
4979 if (BITS_BIG_ENDIAN)
4980 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4981 unsignedp = (code == ZERO_EXTRACT);
4983 break;
4985 default:
4986 break;
4989 if (len && pos >= 0
4990 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4992 machine_mode mode = GET_MODE (SET_SRC (x));
4994 /* For unsigned, we have a choice of a shift followed by an
4995 AND or two shifts. Use two shifts for field sizes where the
4996 constant might be too large. We assume here that we can
4997 always at least get 8-bit constants in an AND insn, which is
4998 true for every current RISC. */
5000 if (unsignedp && len <= 8)
5002 unsigned HOST_WIDE_INT mask
5003 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
5004 SUBST (SET_SRC (x),
5005 gen_rtx_AND (mode,
5006 gen_rtx_LSHIFTRT
5007 (mode, gen_lowpart (mode, inner),
5008 GEN_INT (pos)),
5009 gen_int_mode (mask, mode)));
5011 split = find_split_point (&SET_SRC (x), insn, true);
5012 if (split && split != &SET_SRC (x))
5013 return split;
5015 else
5017 SUBST (SET_SRC (x),
5018 gen_rtx_fmt_ee
5019 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5020 gen_rtx_ASHIFT (mode,
5021 gen_lowpart (mode, inner),
5022 GEN_INT (GET_MODE_PRECISION (mode)
5023 - len - pos)),
5024 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5026 split = find_split_point (&SET_SRC (x), insn, true);
5027 if (split && split != &SET_SRC (x))
5028 return split;
5032 /* See if this is a simple operation with a constant as the second
5033 operand. It might be that this constant is out of range and hence
5034 could be used as a split point. */
5035 if (BINARY_P (SET_SRC (x))
5036 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5037 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5038 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5039 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5040 return &XEXP (SET_SRC (x), 1);
5042 /* Finally, see if this is a simple operation with its first operand
5043 not in a register. The operation might require this operand in a
5044 register, so return it as a split point. We can always do this
5045 because if the first operand were another operation, we would have
5046 already found it as a split point. */
5047 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5048 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5049 return &XEXP (SET_SRC (x), 0);
5051 return 0;
5053 case AND:
5054 case IOR:
5055 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5056 it is better to write this as (not (ior A B)) so we can split it.
5057 Similarly for IOR. */
5058 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5060 SUBST (*loc,
5061 gen_rtx_NOT (GET_MODE (x),
5062 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5063 GET_MODE (x),
5064 XEXP (XEXP (x, 0), 0),
5065 XEXP (XEXP (x, 1), 0))));
5066 return find_split_point (loc, insn, set_src);
5069 /* Many RISC machines have a large set of logical insns. If the
5070 second operand is a NOT, put it first so we will try to split the
5071 other operand first. */
5072 if (GET_CODE (XEXP (x, 1)) == NOT)
5074 rtx tem = XEXP (x, 0);
5075 SUBST (XEXP (x, 0), XEXP (x, 1));
5076 SUBST (XEXP (x, 1), tem);
5078 break;
5080 case PLUS:
5081 case MINUS:
5082 /* Canonicalization can produce (minus A (mult B C)), where C is a
5083 constant. It may be better to try splitting (plus (mult B -C) A)
5084 instead if this isn't a multiply by a power of two. */
5085 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5086 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5087 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5089 machine_mode mode = GET_MODE (x);
5090 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5091 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5092 SUBST (*loc, gen_rtx_PLUS (mode,
5093 gen_rtx_MULT (mode,
5094 XEXP (XEXP (x, 1), 0),
5095 gen_int_mode (other_int,
5096 mode)),
5097 XEXP (x, 0)));
5098 return find_split_point (loc, insn, set_src);
5101 /* Split at a multiply-accumulate instruction. However if this is
5102 the SET_SRC, we likely do not have such an instruction and it's
5103 worthless to try this split. */
5104 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5105 return loc;
5107 default:
5108 break;
5111 /* Otherwise, select our actions depending on our rtx class. */
5112 switch (GET_RTX_CLASS (code))
5114 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5115 case RTX_TERNARY:
5116 split = find_split_point (&XEXP (x, 2), insn, false);
5117 if (split)
5118 return split;
5119 /* ... fall through ... */
5120 case RTX_BIN_ARITH:
5121 case RTX_COMM_ARITH:
5122 case RTX_COMPARE:
5123 case RTX_COMM_COMPARE:
5124 split = find_split_point (&XEXP (x, 1), insn, false);
5125 if (split)
5126 return split;
5127 /* ... fall through ... */
5128 case RTX_UNARY:
5129 /* Some machines have (and (shift ...) ...) insns. If X is not
5130 an AND, but XEXP (X, 0) is, use it as our split point. */
5131 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5132 return &XEXP (x, 0);
5134 split = find_split_point (&XEXP (x, 0), insn, false);
5135 if (split)
5136 return split;
5137 return loc;
5139 default:
5140 /* Otherwise, we don't have a split point. */
5141 return 0;
5145 /* Throughout X, replace FROM with TO, and return the result.
5146 The result is TO if X is FROM;
5147 otherwise the result is X, but its contents may have been modified.
5148 If they were modified, a record was made in undobuf so that
5149 undo_all will (among other things) return X to its original state.
5151 If the number of changes necessary is too much to record to undo,
5152 the excess changes are not made, so the result is invalid.
5153 The changes already made can still be undone.
5154 undobuf.num_undo is incremented for such changes, so by testing that
5155 the caller can tell whether the result is valid.
5157 `n_occurrences' is incremented each time FROM is replaced.
5159 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5161 IN_COND is nonzero if we are at the top level of a condition.
5163 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5164 by copying if `n_occurrences' is nonzero. */
5166 static rtx
5167 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5169 enum rtx_code code = GET_CODE (x);
5170 machine_mode op0_mode = VOIDmode;
5171 const char *fmt;
5172 int len, i;
5173 rtx new_rtx;
5175 /* Two expressions are equal if they are identical copies of a shared
5176 RTX or if they are both registers with the same register number
5177 and mode. */
5179 #define COMBINE_RTX_EQUAL_P(X,Y) \
5180 ((X) == (Y) \
5181 || (REG_P (X) && REG_P (Y) \
5182 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5184 /* Do not substitute into clobbers of regs -- this will never result in
5185 valid RTL. */
5186 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5187 return x;
5189 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5191 n_occurrences++;
5192 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5195 /* If X and FROM are the same register but different modes, they
5196 will not have been seen as equal above. However, the log links code
5197 will make a LOG_LINKS entry for that case. If we do nothing, we
5198 will try to rerecognize our original insn and, when it succeeds,
5199 we will delete the feeding insn, which is incorrect.
5201 So force this insn not to match in this (rare) case. */
5202 if (! in_dest && code == REG && REG_P (from)
5203 && reg_overlap_mentioned_p (x, from))
5204 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5206 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5207 of which may contain things that can be combined. */
5208 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5209 return x;
5211 /* It is possible to have a subexpression appear twice in the insn.
5212 Suppose that FROM is a register that appears within TO.
5213 Then, after that subexpression has been scanned once by `subst',
5214 the second time it is scanned, TO may be found. If we were
5215 to scan TO here, we would find FROM within it and create a
5216 self-referent rtl structure which is completely wrong. */
5217 if (COMBINE_RTX_EQUAL_P (x, to))
5218 return to;
5220 /* Parallel asm_operands need special attention because all of the
5221 inputs are shared across the arms. Furthermore, unsharing the
5222 rtl results in recognition failures. Failure to handle this case
5223 specially can result in circular rtl.
5225 Solve this by doing a normal pass across the first entry of the
5226 parallel, and only processing the SET_DESTs of the subsequent
5227 entries. Ug. */
5229 if (code == PARALLEL
5230 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5231 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5233 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5235 /* If this substitution failed, this whole thing fails. */
5236 if (GET_CODE (new_rtx) == CLOBBER
5237 && XEXP (new_rtx, 0) == const0_rtx)
5238 return new_rtx;
5240 SUBST (XVECEXP (x, 0, 0), new_rtx);
5242 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5244 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5246 if (!REG_P (dest)
5247 && GET_CODE (dest) != CC0
5248 && GET_CODE (dest) != PC)
5250 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5252 /* If this substitution failed, this whole thing fails. */
5253 if (GET_CODE (new_rtx) == CLOBBER
5254 && XEXP (new_rtx, 0) == const0_rtx)
5255 return new_rtx;
5257 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5261 else
5263 len = GET_RTX_LENGTH (code);
5264 fmt = GET_RTX_FORMAT (code);
5266 /* We don't need to process a SET_DEST that is a register, CC0,
5267 or PC, so set up to skip this common case. All other cases
5268 where we want to suppress replacing something inside a
5269 SET_SRC are handled via the IN_DEST operand. */
5270 if (code == SET
5271 && (REG_P (SET_DEST (x))
5272 || GET_CODE (SET_DEST (x)) == CC0
5273 || GET_CODE (SET_DEST (x)) == PC))
5274 fmt = "ie";
5276 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5277 constant. */
5278 if (fmt[0] == 'e')
5279 op0_mode = GET_MODE (XEXP (x, 0));
5281 for (i = 0; i < len; i++)
5283 if (fmt[i] == 'E')
5285 int j;
5286 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5288 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5290 new_rtx = (unique_copy && n_occurrences
5291 ? copy_rtx (to) : to);
5292 n_occurrences++;
5294 else
5296 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5297 unique_copy);
5299 /* If this substitution failed, this whole thing
5300 fails. */
5301 if (GET_CODE (new_rtx) == CLOBBER
5302 && XEXP (new_rtx, 0) == const0_rtx)
5303 return new_rtx;
5306 SUBST (XVECEXP (x, i, j), new_rtx);
5309 else if (fmt[i] == 'e')
5311 /* If this is a register being set, ignore it. */
5312 new_rtx = XEXP (x, i);
5313 if (in_dest
5314 && i == 0
5315 && (((code == SUBREG || code == ZERO_EXTRACT)
5316 && REG_P (new_rtx))
5317 || code == STRICT_LOW_PART))
5320 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5322 /* In general, don't install a subreg involving two
5323 modes not tieable. It can worsen register
5324 allocation, and can even make invalid reload
5325 insns, since the reg inside may need to be copied
5326 from in the outside mode, and that may be invalid
5327 if it is an fp reg copied in integer mode.
5329 We allow two exceptions to this: It is valid if
5330 it is inside another SUBREG and the mode of that
5331 SUBREG and the mode of the inside of TO is
5332 tieable and it is valid if X is a SET that copies
5333 FROM to CC0. */
5335 if (GET_CODE (to) == SUBREG
5336 && ! MODES_TIEABLE_P (GET_MODE (to),
5337 GET_MODE (SUBREG_REG (to)))
5338 && ! (code == SUBREG
5339 && MODES_TIEABLE_P (GET_MODE (x),
5340 GET_MODE (SUBREG_REG (to))))
5341 #ifdef HAVE_cc0
5342 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5343 #endif
5345 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5347 if (code == SUBREG
5348 && REG_P (to)
5349 && REGNO (to) < FIRST_PSEUDO_REGISTER
5350 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5351 SUBREG_BYTE (x),
5352 GET_MODE (x)) < 0)
5353 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5355 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5356 n_occurrences++;
5358 else
5359 /* If we are in a SET_DEST, suppress most cases unless we
5360 have gone inside a MEM, in which case we want to
5361 simplify the address. We assume here that things that
5362 are actually part of the destination have their inner
5363 parts in the first expression. This is true for SUBREG,
5364 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5365 things aside from REG and MEM that should appear in a
5366 SET_DEST. */
5367 new_rtx = subst (XEXP (x, i), from, to,
5368 (((in_dest
5369 && (code == SUBREG || code == STRICT_LOW_PART
5370 || code == ZERO_EXTRACT))
5371 || code == SET)
5372 && i == 0),
5373 code == IF_THEN_ELSE && i == 0,
5374 unique_copy);
5376 /* If we found that we will have to reject this combination,
5377 indicate that by returning the CLOBBER ourselves, rather than
5378 an expression containing it. This will speed things up as
5379 well as prevent accidents where two CLOBBERs are considered
5380 to be equal, thus producing an incorrect simplification. */
5382 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5383 return new_rtx;
5385 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5387 machine_mode mode = GET_MODE (x);
5389 x = simplify_subreg (GET_MODE (x), new_rtx,
5390 GET_MODE (SUBREG_REG (x)),
5391 SUBREG_BYTE (x));
5392 if (! x)
5393 x = gen_rtx_CLOBBER (mode, const0_rtx);
5395 else if (CONST_SCALAR_INT_P (new_rtx)
5396 && GET_CODE (x) == ZERO_EXTEND)
5398 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5399 new_rtx, GET_MODE (XEXP (x, 0)));
5400 gcc_assert (x);
5402 else
5403 SUBST (XEXP (x, i), new_rtx);
5408 /* Check if we are loading something from the constant pool via float
5409 extension; in this case we would undo compress_float_constant
5410 optimization and degenerate constant load to an immediate value. */
5411 if (GET_CODE (x) == FLOAT_EXTEND
5412 && MEM_P (XEXP (x, 0))
5413 && MEM_READONLY_P (XEXP (x, 0)))
5415 rtx tmp = avoid_constant_pool_reference (x);
5416 if (x != tmp)
5417 return x;
5420 /* Try to simplify X. If the simplification changed the code, it is likely
5421 that further simplification will help, so loop, but limit the number
5422 of repetitions that will be performed. */
5424 for (i = 0; i < 4; i++)
5426 /* If X is sufficiently simple, don't bother trying to do anything
5427 with it. */
5428 if (code != CONST_INT && code != REG && code != CLOBBER)
5429 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5431 if (GET_CODE (x) == code)
5432 break;
5434 code = GET_CODE (x);
5436 /* We no longer know the original mode of operand 0 since we
5437 have changed the form of X) */
5438 op0_mode = VOIDmode;
5441 return x;
5444 /* Simplify X, a piece of RTL. We just operate on the expression at the
5445 outer level; call `subst' to simplify recursively. Return the new
5446 expression.
5448 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5449 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5450 of a condition. */
5452 static rtx
5453 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5454 int in_cond)
5456 enum rtx_code code = GET_CODE (x);
5457 machine_mode mode = GET_MODE (x);
5458 rtx temp;
5459 int i;
5461 /* If this is a commutative operation, put a constant last and a complex
5462 expression first. We don't need to do this for comparisons here. */
5463 if (COMMUTATIVE_ARITH_P (x)
5464 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5466 temp = XEXP (x, 0);
5467 SUBST (XEXP (x, 0), XEXP (x, 1));
5468 SUBST (XEXP (x, 1), temp);
5471 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5472 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5473 things. Check for cases where both arms are testing the same
5474 condition.
5476 Don't do anything if all operands are very simple. */
5478 if ((BINARY_P (x)
5479 && ((!OBJECT_P (XEXP (x, 0))
5480 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5481 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5482 || (!OBJECT_P (XEXP (x, 1))
5483 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5484 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5485 || (UNARY_P (x)
5486 && (!OBJECT_P (XEXP (x, 0))
5487 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5488 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5490 rtx cond, true_rtx, false_rtx;
5492 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5493 if (cond != 0
5494 /* If everything is a comparison, what we have is highly unlikely
5495 to be simpler, so don't use it. */
5496 && ! (COMPARISON_P (x)
5497 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5499 rtx cop1 = const0_rtx;
5500 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5502 if (cond_code == NE && COMPARISON_P (cond))
5503 return x;
5505 /* Simplify the alternative arms; this may collapse the true and
5506 false arms to store-flag values. Be careful to use copy_rtx
5507 here since true_rtx or false_rtx might share RTL with x as a
5508 result of the if_then_else_cond call above. */
5509 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5510 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5512 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5513 is unlikely to be simpler. */
5514 if (general_operand (true_rtx, VOIDmode)
5515 && general_operand (false_rtx, VOIDmode))
5517 enum rtx_code reversed;
5519 /* Restarting if we generate a store-flag expression will cause
5520 us to loop. Just drop through in this case. */
5522 /* If the result values are STORE_FLAG_VALUE and zero, we can
5523 just make the comparison operation. */
5524 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5525 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5526 cond, cop1);
5527 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5528 && ((reversed = reversed_comparison_code_parts
5529 (cond_code, cond, cop1, NULL))
5530 != UNKNOWN))
5531 x = simplify_gen_relational (reversed, mode, VOIDmode,
5532 cond, cop1);
5534 /* Likewise, we can make the negate of a comparison operation
5535 if the result values are - STORE_FLAG_VALUE and zero. */
5536 else if (CONST_INT_P (true_rtx)
5537 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5538 && false_rtx == const0_rtx)
5539 x = simplify_gen_unary (NEG, mode,
5540 simplify_gen_relational (cond_code,
5541 mode, VOIDmode,
5542 cond, cop1),
5543 mode);
5544 else if (CONST_INT_P (false_rtx)
5545 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5546 && true_rtx == const0_rtx
5547 && ((reversed = reversed_comparison_code_parts
5548 (cond_code, cond, cop1, NULL))
5549 != UNKNOWN))
5550 x = simplify_gen_unary (NEG, mode,
5551 simplify_gen_relational (reversed,
5552 mode, VOIDmode,
5553 cond, cop1),
5554 mode);
5555 else
5556 return gen_rtx_IF_THEN_ELSE (mode,
5557 simplify_gen_relational (cond_code,
5558 mode,
5559 VOIDmode,
5560 cond,
5561 cop1),
5562 true_rtx, false_rtx);
5564 code = GET_CODE (x);
5565 op0_mode = VOIDmode;
5570 /* Try to fold this expression in case we have constants that weren't
5571 present before. */
5572 temp = 0;
5573 switch (GET_RTX_CLASS (code))
5575 case RTX_UNARY:
5576 if (op0_mode == VOIDmode)
5577 op0_mode = GET_MODE (XEXP (x, 0));
5578 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5579 break;
5580 case RTX_COMPARE:
5581 case RTX_COMM_COMPARE:
5583 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5584 if (cmp_mode == VOIDmode)
5586 cmp_mode = GET_MODE (XEXP (x, 1));
5587 if (cmp_mode == VOIDmode)
5588 cmp_mode = op0_mode;
5590 temp = simplify_relational_operation (code, mode, cmp_mode,
5591 XEXP (x, 0), XEXP (x, 1));
5593 break;
5594 case RTX_COMM_ARITH:
5595 case RTX_BIN_ARITH:
5596 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5597 break;
5598 case RTX_BITFIELD_OPS:
5599 case RTX_TERNARY:
5600 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5601 XEXP (x, 1), XEXP (x, 2));
5602 break;
5603 default:
5604 break;
5607 if (temp)
5609 x = temp;
5610 code = GET_CODE (temp);
5611 op0_mode = VOIDmode;
5612 mode = GET_MODE (temp);
5615 /* First see if we can apply the inverse distributive law. */
5616 if (code == PLUS || code == MINUS
5617 || code == AND || code == IOR || code == XOR)
5619 x = apply_distributive_law (x);
5620 code = GET_CODE (x);
5621 op0_mode = VOIDmode;
5624 /* If CODE is an associative operation not otherwise handled, see if we
5625 can associate some operands. This can win if they are constants or
5626 if they are logically related (i.e. (a & b) & a). */
5627 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5628 || code == AND || code == IOR || code == XOR
5629 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5630 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5631 || (flag_associative_math && FLOAT_MODE_P (mode))))
5633 if (GET_CODE (XEXP (x, 0)) == code)
5635 rtx other = XEXP (XEXP (x, 0), 0);
5636 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5637 rtx inner_op1 = XEXP (x, 1);
5638 rtx inner;
5640 /* Make sure we pass the constant operand if any as the second
5641 one if this is a commutative operation. */
5642 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5644 rtx tem = inner_op0;
5645 inner_op0 = inner_op1;
5646 inner_op1 = tem;
5648 inner = simplify_binary_operation (code == MINUS ? PLUS
5649 : code == DIV ? MULT
5650 : code,
5651 mode, inner_op0, inner_op1);
5653 /* For commutative operations, try the other pair if that one
5654 didn't simplify. */
5655 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5657 other = XEXP (XEXP (x, 0), 1);
5658 inner = simplify_binary_operation (code, mode,
5659 XEXP (XEXP (x, 0), 0),
5660 XEXP (x, 1));
5663 if (inner)
5664 return simplify_gen_binary (code, mode, other, inner);
5668 /* A little bit of algebraic simplification here. */
5669 switch (code)
5671 case MEM:
5672 /* Ensure that our address has any ASHIFTs converted to MULT in case
5673 address-recognizing predicates are called later. */
5674 temp = make_compound_operation (XEXP (x, 0), MEM);
5675 SUBST (XEXP (x, 0), temp);
5676 break;
5678 case SUBREG:
5679 if (op0_mode == VOIDmode)
5680 op0_mode = GET_MODE (SUBREG_REG (x));
5682 /* See if this can be moved to simplify_subreg. */
5683 if (CONSTANT_P (SUBREG_REG (x))
5684 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5685 /* Don't call gen_lowpart if the inner mode
5686 is VOIDmode and we cannot simplify it, as SUBREG without
5687 inner mode is invalid. */
5688 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5689 || gen_lowpart_common (mode, SUBREG_REG (x))))
5690 return gen_lowpart (mode, SUBREG_REG (x));
5692 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5693 break;
5695 rtx temp;
5696 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5697 SUBREG_BYTE (x));
5698 if (temp)
5699 return temp;
5701 /* If op is known to have all lower bits zero, the result is zero. */
5702 if (!in_dest
5703 && SCALAR_INT_MODE_P (mode)
5704 && SCALAR_INT_MODE_P (op0_mode)
5705 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5706 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5707 && HWI_COMPUTABLE_MODE_P (op0_mode)
5708 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5709 & GET_MODE_MASK (mode)) == 0)
5710 return CONST0_RTX (mode);
5713 /* Don't change the mode of the MEM if that would change the meaning
5714 of the address. */
5715 if (MEM_P (SUBREG_REG (x))
5716 && (MEM_VOLATILE_P (SUBREG_REG (x))
5717 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5718 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5719 return gen_rtx_CLOBBER (mode, const0_rtx);
5721 /* Note that we cannot do any narrowing for non-constants since
5722 we might have been counting on using the fact that some bits were
5723 zero. We now do this in the SET. */
5725 break;
5727 case NEG:
5728 temp = expand_compound_operation (XEXP (x, 0));
5730 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5731 replaced by (lshiftrt X C). This will convert
5732 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5734 if (GET_CODE (temp) == ASHIFTRT
5735 && CONST_INT_P (XEXP (temp, 1))
5736 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5737 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5738 INTVAL (XEXP (temp, 1)));
5740 /* If X has only a single bit that might be nonzero, say, bit I, convert
5741 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5742 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5743 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5744 or a SUBREG of one since we'd be making the expression more
5745 complex if it was just a register. */
5747 if (!REG_P (temp)
5748 && ! (GET_CODE (temp) == SUBREG
5749 && REG_P (SUBREG_REG (temp)))
5750 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5752 rtx temp1 = simplify_shift_const
5753 (NULL_RTX, ASHIFTRT, mode,
5754 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5755 GET_MODE_PRECISION (mode) - 1 - i),
5756 GET_MODE_PRECISION (mode) - 1 - i);
5758 /* If all we did was surround TEMP with the two shifts, we
5759 haven't improved anything, so don't use it. Otherwise,
5760 we are better off with TEMP1. */
5761 if (GET_CODE (temp1) != ASHIFTRT
5762 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5763 || XEXP (XEXP (temp1, 0), 0) != temp)
5764 return temp1;
5766 break;
5768 case TRUNCATE:
5769 /* We can't handle truncation to a partial integer mode here
5770 because we don't know the real bitsize of the partial
5771 integer mode. */
5772 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5773 break;
5775 if (HWI_COMPUTABLE_MODE_P (mode))
5776 SUBST (XEXP (x, 0),
5777 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5778 GET_MODE_MASK (mode), 0));
5780 /* We can truncate a constant value and return it. */
5781 if (CONST_INT_P (XEXP (x, 0)))
5782 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5784 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5785 whose value is a comparison can be replaced with a subreg if
5786 STORE_FLAG_VALUE permits. */
5787 if (HWI_COMPUTABLE_MODE_P (mode)
5788 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5789 && (temp = get_last_value (XEXP (x, 0)))
5790 && COMPARISON_P (temp))
5791 return gen_lowpart (mode, XEXP (x, 0));
5792 break;
5794 case CONST:
5795 /* (const (const X)) can become (const X). Do it this way rather than
5796 returning the inner CONST since CONST can be shared with a
5797 REG_EQUAL note. */
5798 if (GET_CODE (XEXP (x, 0)) == CONST)
5799 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5800 break;
5802 #ifdef HAVE_lo_sum
5803 case LO_SUM:
5804 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5805 can add in an offset. find_split_point will split this address up
5806 again if it doesn't match. */
5807 if (GET_CODE (XEXP (x, 0)) == HIGH
5808 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5809 return XEXP (x, 1);
5810 break;
5811 #endif
5813 case PLUS:
5814 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5815 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5816 bit-field and can be replaced by either a sign_extend or a
5817 sign_extract. The `and' may be a zero_extend and the two
5818 <c>, -<c> constants may be reversed. */
5819 if (GET_CODE (XEXP (x, 0)) == XOR
5820 && CONST_INT_P (XEXP (x, 1))
5821 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5822 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5823 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5824 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5825 && HWI_COMPUTABLE_MODE_P (mode)
5826 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5827 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5828 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5829 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5830 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5831 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5832 == (unsigned int) i + 1))))
5833 return simplify_shift_const
5834 (NULL_RTX, ASHIFTRT, mode,
5835 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5836 XEXP (XEXP (XEXP (x, 0), 0), 0),
5837 GET_MODE_PRECISION (mode) - (i + 1)),
5838 GET_MODE_PRECISION (mode) - (i + 1));
5840 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5841 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5842 the bitsize of the mode - 1. This allows simplification of
5843 "a = (b & 8) == 0;" */
5844 if (XEXP (x, 1) == constm1_rtx
5845 && !REG_P (XEXP (x, 0))
5846 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5847 && REG_P (SUBREG_REG (XEXP (x, 0))))
5848 && nonzero_bits (XEXP (x, 0), mode) == 1)
5849 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5850 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5851 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5852 GET_MODE_PRECISION (mode) - 1),
5853 GET_MODE_PRECISION (mode) - 1);
5855 /* If we are adding two things that have no bits in common, convert
5856 the addition into an IOR. This will often be further simplified,
5857 for example in cases like ((a & 1) + (a & 2)), which can
5858 become a & 3. */
5860 if (HWI_COMPUTABLE_MODE_P (mode)
5861 && (nonzero_bits (XEXP (x, 0), mode)
5862 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5864 /* Try to simplify the expression further. */
5865 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5866 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5868 /* If we could, great. If not, do not go ahead with the IOR
5869 replacement, since PLUS appears in many special purpose
5870 address arithmetic instructions. */
5871 if (GET_CODE (temp) != CLOBBER
5872 && (GET_CODE (temp) != IOR
5873 || ((XEXP (temp, 0) != XEXP (x, 0)
5874 || XEXP (temp, 1) != XEXP (x, 1))
5875 && (XEXP (temp, 0) != XEXP (x, 1)
5876 || XEXP (temp, 1) != XEXP (x, 0)))))
5877 return temp;
5879 break;
5881 case MINUS:
5882 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5883 (and <foo> (const_int pow2-1)) */
5884 if (GET_CODE (XEXP (x, 1)) == AND
5885 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5886 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5887 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5888 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5889 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5890 break;
5892 case MULT:
5893 /* If we have (mult (plus A B) C), apply the distributive law and then
5894 the inverse distributive law to see if things simplify. This
5895 occurs mostly in addresses, often when unrolling loops. */
5897 if (GET_CODE (XEXP (x, 0)) == PLUS)
5899 rtx result = distribute_and_simplify_rtx (x, 0);
5900 if (result)
5901 return result;
5904 /* Try simplify a*(b/c) as (a*b)/c. */
5905 if (FLOAT_MODE_P (mode) && flag_associative_math
5906 && GET_CODE (XEXP (x, 0)) == DIV)
5908 rtx tem = simplify_binary_operation (MULT, mode,
5909 XEXP (XEXP (x, 0), 0),
5910 XEXP (x, 1));
5911 if (tem)
5912 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5914 break;
5916 case UDIV:
5917 /* If this is a divide by a power of two, treat it as a shift if
5918 its first operand is a shift. */
5919 if (CONST_INT_P (XEXP (x, 1))
5920 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5921 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5922 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5923 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5924 || GET_CODE (XEXP (x, 0)) == ROTATE
5925 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5926 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5927 break;
5929 case EQ: case NE:
5930 case GT: case GTU: case GE: case GEU:
5931 case LT: case LTU: case LE: case LEU:
5932 case UNEQ: case LTGT:
5933 case UNGT: case UNGE:
5934 case UNLT: case UNLE:
5935 case UNORDERED: case ORDERED:
5936 /* If the first operand is a condition code, we can't do anything
5937 with it. */
5938 if (GET_CODE (XEXP (x, 0)) == COMPARE
5939 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5940 && ! CC0_P (XEXP (x, 0))))
5942 rtx op0 = XEXP (x, 0);
5943 rtx op1 = XEXP (x, 1);
5944 enum rtx_code new_code;
5946 if (GET_CODE (op0) == COMPARE)
5947 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5949 /* Simplify our comparison, if possible. */
5950 new_code = simplify_comparison (code, &op0, &op1);
5952 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5953 if only the low-order bit is possibly nonzero in X (such as when
5954 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5955 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5956 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5957 (plus X 1).
5959 Remove any ZERO_EXTRACT we made when thinking this was a
5960 comparison. It may now be simpler to use, e.g., an AND. If a
5961 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5962 the call to make_compound_operation in the SET case.
5964 Don't apply these optimizations if the caller would
5965 prefer a comparison rather than a value.
5966 E.g., for the condition in an IF_THEN_ELSE most targets need
5967 an explicit comparison. */
5969 if (in_cond)
5972 else if (STORE_FLAG_VALUE == 1
5973 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5974 && op1 == const0_rtx
5975 && mode == GET_MODE (op0)
5976 && nonzero_bits (op0, mode) == 1)
5977 return gen_lowpart (mode,
5978 expand_compound_operation (op0));
5980 else if (STORE_FLAG_VALUE == 1
5981 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5982 && op1 == const0_rtx
5983 && mode == GET_MODE (op0)
5984 && (num_sign_bit_copies (op0, mode)
5985 == GET_MODE_PRECISION (mode)))
5987 op0 = expand_compound_operation (op0);
5988 return simplify_gen_unary (NEG, mode,
5989 gen_lowpart (mode, op0),
5990 mode);
5993 else if (STORE_FLAG_VALUE == 1
5994 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5995 && op1 == const0_rtx
5996 && mode == GET_MODE (op0)
5997 && nonzero_bits (op0, mode) == 1)
5999 op0 = expand_compound_operation (op0);
6000 return simplify_gen_binary (XOR, mode,
6001 gen_lowpart (mode, op0),
6002 const1_rtx);
6005 else if (STORE_FLAG_VALUE == 1
6006 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6007 && op1 == const0_rtx
6008 && mode == GET_MODE (op0)
6009 && (num_sign_bit_copies (op0, mode)
6010 == GET_MODE_PRECISION (mode)))
6012 op0 = expand_compound_operation (op0);
6013 return plus_constant (mode, gen_lowpart (mode, op0), 1);
6016 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6017 those above. */
6018 if (in_cond)
6021 else if (STORE_FLAG_VALUE == -1
6022 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6023 && op1 == const0_rtx
6024 && mode == GET_MODE (op0)
6025 && (num_sign_bit_copies (op0, mode)
6026 == GET_MODE_PRECISION (mode)))
6027 return gen_lowpart (mode,
6028 expand_compound_operation (op0));
6030 else if (STORE_FLAG_VALUE == -1
6031 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6032 && op1 == const0_rtx
6033 && mode == GET_MODE (op0)
6034 && nonzero_bits (op0, mode) == 1)
6036 op0 = expand_compound_operation (op0);
6037 return simplify_gen_unary (NEG, mode,
6038 gen_lowpart (mode, op0),
6039 mode);
6042 else if (STORE_FLAG_VALUE == -1
6043 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6044 && op1 == const0_rtx
6045 && mode == GET_MODE (op0)
6046 && (num_sign_bit_copies (op0, mode)
6047 == GET_MODE_PRECISION (mode)))
6049 op0 = expand_compound_operation (op0);
6050 return simplify_gen_unary (NOT, mode,
6051 gen_lowpart (mode, op0),
6052 mode);
6055 /* If X is 0/1, (eq X 0) is X-1. */
6056 else if (STORE_FLAG_VALUE == -1
6057 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6058 && op1 == const0_rtx
6059 && mode == GET_MODE (op0)
6060 && nonzero_bits (op0, mode) == 1)
6062 op0 = expand_compound_operation (op0);
6063 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6066 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6067 one bit that might be nonzero, we can convert (ne x 0) to
6068 (ashift x c) where C puts the bit in the sign bit. Remove any
6069 AND with STORE_FLAG_VALUE when we are done, since we are only
6070 going to test the sign bit. */
6071 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6072 && HWI_COMPUTABLE_MODE_P (mode)
6073 && val_signbit_p (mode, STORE_FLAG_VALUE)
6074 && op1 == const0_rtx
6075 && mode == GET_MODE (op0)
6076 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6078 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6079 expand_compound_operation (op0),
6080 GET_MODE_PRECISION (mode) - 1 - i);
6081 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6082 return XEXP (x, 0);
6083 else
6084 return x;
6087 /* If the code changed, return a whole new comparison.
6088 We also need to avoid using SUBST in cases where
6089 simplify_comparison has widened a comparison with a CONST_INT,
6090 since in that case the wider CONST_INT may fail the sanity
6091 checks in do_SUBST. */
6092 if (new_code != code
6093 || (CONST_INT_P (op1)
6094 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6095 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6096 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6098 /* Otherwise, keep this operation, but maybe change its operands.
6099 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6100 SUBST (XEXP (x, 0), op0);
6101 SUBST (XEXP (x, 1), op1);
6103 break;
6105 case IF_THEN_ELSE:
6106 return simplify_if_then_else (x);
6108 case ZERO_EXTRACT:
6109 case SIGN_EXTRACT:
6110 case ZERO_EXTEND:
6111 case SIGN_EXTEND:
6112 /* If we are processing SET_DEST, we are done. */
6113 if (in_dest)
6114 return x;
6116 return expand_compound_operation (x);
6118 case SET:
6119 return simplify_set (x);
6121 case AND:
6122 case IOR:
6123 return simplify_logical (x);
6125 case ASHIFT:
6126 case LSHIFTRT:
6127 case ASHIFTRT:
6128 case ROTATE:
6129 case ROTATERT:
6130 /* If this is a shift by a constant amount, simplify it. */
6131 if (CONST_INT_P (XEXP (x, 1)))
6132 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6133 INTVAL (XEXP (x, 1)));
6135 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6136 SUBST (XEXP (x, 1),
6137 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6138 ((unsigned HOST_WIDE_INT) 1
6139 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6140 - 1,
6141 0));
6142 break;
6144 default:
6145 break;
6148 return x;
6151 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6153 static rtx
6154 simplify_if_then_else (rtx x)
6156 machine_mode mode = GET_MODE (x);
6157 rtx cond = XEXP (x, 0);
6158 rtx true_rtx = XEXP (x, 1);
6159 rtx false_rtx = XEXP (x, 2);
6160 enum rtx_code true_code = GET_CODE (cond);
6161 int comparison_p = COMPARISON_P (cond);
6162 rtx temp;
6163 int i;
6164 enum rtx_code false_code;
6165 rtx reversed;
6167 /* Simplify storing of the truth value. */
6168 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6169 return simplify_gen_relational (true_code, mode, VOIDmode,
6170 XEXP (cond, 0), XEXP (cond, 1));
6172 /* Also when the truth value has to be reversed. */
6173 if (comparison_p
6174 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6175 && (reversed = reversed_comparison (cond, mode)))
6176 return reversed;
6178 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6179 in it is being compared against certain values. Get the true and false
6180 comparisons and see if that says anything about the value of each arm. */
6182 if (comparison_p
6183 && ((false_code = reversed_comparison_code (cond, NULL))
6184 != UNKNOWN)
6185 && REG_P (XEXP (cond, 0)))
6187 HOST_WIDE_INT nzb;
6188 rtx from = XEXP (cond, 0);
6189 rtx true_val = XEXP (cond, 1);
6190 rtx false_val = true_val;
6191 int swapped = 0;
6193 /* If FALSE_CODE is EQ, swap the codes and arms. */
6195 if (false_code == EQ)
6197 swapped = 1, true_code = EQ, false_code = NE;
6198 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6201 /* If we are comparing against zero and the expression being tested has
6202 only a single bit that might be nonzero, that is its value when it is
6203 not equal to zero. Similarly if it is known to be -1 or 0. */
6205 if (true_code == EQ && true_val == const0_rtx
6206 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6208 false_code = EQ;
6209 false_val = gen_int_mode (nzb, GET_MODE (from));
6211 else if (true_code == EQ && true_val == const0_rtx
6212 && (num_sign_bit_copies (from, GET_MODE (from))
6213 == GET_MODE_PRECISION (GET_MODE (from))))
6215 false_code = EQ;
6216 false_val = constm1_rtx;
6219 /* Now simplify an arm if we know the value of the register in the
6220 branch and it is used in the arm. Be careful due to the potential
6221 of locally-shared RTL. */
6223 if (reg_mentioned_p (from, true_rtx))
6224 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6225 from, true_val),
6226 pc_rtx, pc_rtx, 0, 0, 0);
6227 if (reg_mentioned_p (from, false_rtx))
6228 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6229 from, false_val),
6230 pc_rtx, pc_rtx, 0, 0, 0);
6232 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6233 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6235 true_rtx = XEXP (x, 1);
6236 false_rtx = XEXP (x, 2);
6237 true_code = GET_CODE (cond);
6240 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6241 reversed, do so to avoid needing two sets of patterns for
6242 subtract-and-branch insns. Similarly if we have a constant in the true
6243 arm, the false arm is the same as the first operand of the comparison, or
6244 the false arm is more complicated than the true arm. */
6246 if (comparison_p
6247 && reversed_comparison_code (cond, NULL) != UNKNOWN
6248 && (true_rtx == pc_rtx
6249 || (CONSTANT_P (true_rtx)
6250 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6251 || true_rtx == const0_rtx
6252 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6253 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6254 && !OBJECT_P (false_rtx))
6255 || reg_mentioned_p (true_rtx, false_rtx)
6256 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6258 true_code = reversed_comparison_code (cond, NULL);
6259 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6260 SUBST (XEXP (x, 1), false_rtx);
6261 SUBST (XEXP (x, 2), true_rtx);
6263 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6264 cond = XEXP (x, 0);
6266 /* It is possible that the conditional has been simplified out. */
6267 true_code = GET_CODE (cond);
6268 comparison_p = COMPARISON_P (cond);
6271 /* If the two arms are identical, we don't need the comparison. */
6273 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6274 return true_rtx;
6276 /* Convert a == b ? b : a to "a". */
6277 if (true_code == EQ && ! side_effects_p (cond)
6278 && !HONOR_NANS (mode)
6279 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6280 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6281 return false_rtx;
6282 else if (true_code == NE && ! side_effects_p (cond)
6283 && !HONOR_NANS (mode)
6284 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6285 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6286 return true_rtx;
6288 /* Look for cases where we have (abs x) or (neg (abs X)). */
6290 if (GET_MODE_CLASS (mode) == MODE_INT
6291 && comparison_p
6292 && XEXP (cond, 1) == const0_rtx
6293 && GET_CODE (false_rtx) == NEG
6294 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6295 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6296 && ! side_effects_p (true_rtx))
6297 switch (true_code)
6299 case GT:
6300 case GE:
6301 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6302 case LT:
6303 case LE:
6304 return
6305 simplify_gen_unary (NEG, mode,
6306 simplify_gen_unary (ABS, mode, true_rtx, mode),
6307 mode);
6308 default:
6309 break;
6312 /* Look for MIN or MAX. */
6314 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6315 && comparison_p
6316 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6317 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6318 && ! side_effects_p (cond))
6319 switch (true_code)
6321 case GE:
6322 case GT:
6323 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6324 case LE:
6325 case LT:
6326 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6327 case GEU:
6328 case GTU:
6329 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6330 case LEU:
6331 case LTU:
6332 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6333 default:
6334 break;
6337 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6338 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6339 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6340 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6341 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6342 neither 1 or -1, but it isn't worth checking for. */
6344 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6345 && comparison_p
6346 && GET_MODE_CLASS (mode) == MODE_INT
6347 && ! side_effects_p (x))
6349 rtx t = make_compound_operation (true_rtx, SET);
6350 rtx f = make_compound_operation (false_rtx, SET);
6351 rtx cond_op0 = XEXP (cond, 0);
6352 rtx cond_op1 = XEXP (cond, 1);
6353 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6354 machine_mode m = mode;
6355 rtx z = 0, c1 = NULL_RTX;
6357 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6358 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6359 || GET_CODE (t) == ASHIFT
6360 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6361 && rtx_equal_p (XEXP (t, 0), f))
6362 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6364 /* If an identity-zero op is commutative, check whether there
6365 would be a match if we swapped the operands. */
6366 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6367 || GET_CODE (t) == XOR)
6368 && rtx_equal_p (XEXP (t, 1), f))
6369 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6370 else if (GET_CODE (t) == SIGN_EXTEND
6371 && (GET_CODE (XEXP (t, 0)) == PLUS
6372 || GET_CODE (XEXP (t, 0)) == MINUS
6373 || GET_CODE (XEXP (t, 0)) == IOR
6374 || GET_CODE (XEXP (t, 0)) == XOR
6375 || GET_CODE (XEXP (t, 0)) == ASHIFT
6376 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6377 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6378 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6379 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6380 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6381 && (num_sign_bit_copies (f, GET_MODE (f))
6382 > (unsigned int)
6383 (GET_MODE_PRECISION (mode)
6384 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6386 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6387 extend_op = SIGN_EXTEND;
6388 m = GET_MODE (XEXP (t, 0));
6390 else if (GET_CODE (t) == SIGN_EXTEND
6391 && (GET_CODE (XEXP (t, 0)) == PLUS
6392 || GET_CODE (XEXP (t, 0)) == IOR
6393 || GET_CODE (XEXP (t, 0)) == XOR)
6394 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6395 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6396 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6397 && (num_sign_bit_copies (f, GET_MODE (f))
6398 > (unsigned int)
6399 (GET_MODE_PRECISION (mode)
6400 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6402 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6403 extend_op = SIGN_EXTEND;
6404 m = GET_MODE (XEXP (t, 0));
6406 else if (GET_CODE (t) == ZERO_EXTEND
6407 && (GET_CODE (XEXP (t, 0)) == PLUS
6408 || GET_CODE (XEXP (t, 0)) == MINUS
6409 || GET_CODE (XEXP (t, 0)) == IOR
6410 || GET_CODE (XEXP (t, 0)) == XOR
6411 || GET_CODE (XEXP (t, 0)) == ASHIFT
6412 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6413 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6414 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6415 && HWI_COMPUTABLE_MODE_P (mode)
6416 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6417 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6418 && ((nonzero_bits (f, GET_MODE (f))
6419 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6420 == 0))
6422 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6423 extend_op = ZERO_EXTEND;
6424 m = GET_MODE (XEXP (t, 0));
6426 else if (GET_CODE (t) == ZERO_EXTEND
6427 && (GET_CODE (XEXP (t, 0)) == PLUS
6428 || GET_CODE (XEXP (t, 0)) == IOR
6429 || GET_CODE (XEXP (t, 0)) == XOR)
6430 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6431 && HWI_COMPUTABLE_MODE_P (mode)
6432 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6433 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6434 && ((nonzero_bits (f, GET_MODE (f))
6435 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6436 == 0))
6438 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6439 extend_op = ZERO_EXTEND;
6440 m = GET_MODE (XEXP (t, 0));
6443 if (z)
6445 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6446 cond_op0, cond_op1),
6447 pc_rtx, pc_rtx, 0, 0, 0);
6448 temp = simplify_gen_binary (MULT, m, temp,
6449 simplify_gen_binary (MULT, m, c1,
6450 const_true_rtx));
6451 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6452 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6454 if (extend_op != UNKNOWN)
6455 temp = simplify_gen_unary (extend_op, mode, temp, m);
6457 return temp;
6461 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6462 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6463 negation of a single bit, we can convert this operation to a shift. We
6464 can actually do this more generally, but it doesn't seem worth it. */
6466 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6467 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6468 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6469 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6470 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6471 == GET_MODE_PRECISION (mode))
6472 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6473 return
6474 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6475 gen_lowpart (mode, XEXP (cond, 0)), i);
6477 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6478 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6479 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6480 && GET_MODE (XEXP (cond, 0)) == mode
6481 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6482 == nonzero_bits (XEXP (cond, 0), mode)
6483 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6484 return XEXP (cond, 0);
6486 return x;
6489 /* Simplify X, a SET expression. Return the new expression. */
6491 static rtx
6492 simplify_set (rtx x)
6494 rtx src = SET_SRC (x);
6495 rtx dest = SET_DEST (x);
6496 machine_mode mode
6497 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6498 rtx_insn *other_insn;
6499 rtx *cc_use;
6501 /* (set (pc) (return)) gets written as (return). */
6502 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6503 return src;
6505 /* Now that we know for sure which bits of SRC we are using, see if we can
6506 simplify the expression for the object knowing that we only need the
6507 low-order bits. */
6509 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6511 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6512 SUBST (SET_SRC (x), src);
6515 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6516 the comparison result and try to simplify it unless we already have used
6517 undobuf.other_insn. */
6518 if ((GET_MODE_CLASS (mode) == MODE_CC
6519 || GET_CODE (src) == COMPARE
6520 || CC0_P (dest))
6521 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6522 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6523 && COMPARISON_P (*cc_use)
6524 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6526 enum rtx_code old_code = GET_CODE (*cc_use);
6527 enum rtx_code new_code;
6528 rtx op0, op1, tmp;
6529 int other_changed = 0;
6530 rtx inner_compare = NULL_RTX;
6531 machine_mode compare_mode = GET_MODE (dest);
6533 if (GET_CODE (src) == COMPARE)
6535 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6536 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6538 inner_compare = op0;
6539 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6542 else
6543 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6545 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6546 op0, op1);
6547 if (!tmp)
6548 new_code = old_code;
6549 else if (!CONSTANT_P (tmp))
6551 new_code = GET_CODE (tmp);
6552 op0 = XEXP (tmp, 0);
6553 op1 = XEXP (tmp, 1);
6555 else
6557 rtx pat = PATTERN (other_insn);
6558 undobuf.other_insn = other_insn;
6559 SUBST (*cc_use, tmp);
6561 /* Attempt to simplify CC user. */
6562 if (GET_CODE (pat) == SET)
6564 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6565 if (new_rtx != NULL_RTX)
6566 SUBST (SET_SRC (pat), new_rtx);
6569 /* Convert X into a no-op move. */
6570 SUBST (SET_DEST (x), pc_rtx);
6571 SUBST (SET_SRC (x), pc_rtx);
6572 return x;
6575 /* Simplify our comparison, if possible. */
6576 new_code = simplify_comparison (new_code, &op0, &op1);
6578 #ifdef SELECT_CC_MODE
6579 /* If this machine has CC modes other than CCmode, check to see if we
6580 need to use a different CC mode here. */
6581 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6582 compare_mode = GET_MODE (op0);
6583 else if (inner_compare
6584 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6585 && new_code == old_code
6586 && op0 == XEXP (inner_compare, 0)
6587 && op1 == XEXP (inner_compare, 1))
6588 compare_mode = GET_MODE (inner_compare);
6589 else
6590 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6592 #ifndef HAVE_cc0
6593 /* If the mode changed, we have to change SET_DEST, the mode in the
6594 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6595 a hard register, just build new versions with the proper mode. If it
6596 is a pseudo, we lose unless it is only time we set the pseudo, in
6597 which case we can safely change its mode. */
6598 if (compare_mode != GET_MODE (dest))
6600 if (can_change_dest_mode (dest, 0, compare_mode))
6602 unsigned int regno = REGNO (dest);
6603 rtx new_dest;
6605 if (regno < FIRST_PSEUDO_REGISTER)
6606 new_dest = gen_rtx_REG (compare_mode, regno);
6607 else
6609 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6610 new_dest = regno_reg_rtx[regno];
6613 SUBST (SET_DEST (x), new_dest);
6614 SUBST (XEXP (*cc_use, 0), new_dest);
6615 other_changed = 1;
6617 dest = new_dest;
6620 #endif /* cc0 */
6621 #endif /* SELECT_CC_MODE */
6623 /* If the code changed, we have to build a new comparison in
6624 undobuf.other_insn. */
6625 if (new_code != old_code)
6627 int other_changed_previously = other_changed;
6628 unsigned HOST_WIDE_INT mask;
6629 rtx old_cc_use = *cc_use;
6631 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6632 dest, const0_rtx));
6633 other_changed = 1;
6635 /* If the only change we made was to change an EQ into an NE or
6636 vice versa, OP0 has only one bit that might be nonzero, and OP1
6637 is zero, check if changing the user of the condition code will
6638 produce a valid insn. If it won't, we can keep the original code
6639 in that insn by surrounding our operation with an XOR. */
6641 if (((old_code == NE && new_code == EQ)
6642 || (old_code == EQ && new_code == NE))
6643 && ! other_changed_previously && op1 == const0_rtx
6644 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6645 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6647 rtx pat = PATTERN (other_insn), note = 0;
6649 if ((recog_for_combine (&pat, other_insn, &note) < 0
6650 && ! check_asm_operands (pat)))
6652 *cc_use = old_cc_use;
6653 other_changed = 0;
6655 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6656 gen_int_mode (mask,
6657 GET_MODE (op0)));
6662 if (other_changed)
6663 undobuf.other_insn = other_insn;
6665 /* Otherwise, if we didn't previously have a COMPARE in the
6666 correct mode, we need one. */
6667 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6669 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6670 src = SET_SRC (x);
6672 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6674 SUBST (SET_SRC (x), op0);
6675 src = SET_SRC (x);
6677 /* Otherwise, update the COMPARE if needed. */
6678 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6680 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6681 src = SET_SRC (x);
6684 else
6686 /* Get SET_SRC in a form where we have placed back any
6687 compound expressions. Then do the checks below. */
6688 src = make_compound_operation (src, SET);
6689 SUBST (SET_SRC (x), src);
6692 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6693 and X being a REG or (subreg (reg)), we may be able to convert this to
6694 (set (subreg:m2 x) (op)).
6696 We can always do this if M1 is narrower than M2 because that means that
6697 we only care about the low bits of the result.
6699 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6700 perform a narrower operation than requested since the high-order bits will
6701 be undefined. On machine where it is defined, this transformation is safe
6702 as long as M1 and M2 have the same number of words. */
6704 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6705 && !OBJECT_P (SUBREG_REG (src))
6706 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6707 / UNITS_PER_WORD)
6708 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6709 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6710 #ifndef WORD_REGISTER_OPERATIONS
6711 && (GET_MODE_SIZE (GET_MODE (src))
6712 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6713 #endif
6714 #ifdef CANNOT_CHANGE_MODE_CLASS
6715 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6716 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6717 GET_MODE (SUBREG_REG (src)),
6718 GET_MODE (src)))
6719 #endif
6720 && (REG_P (dest)
6721 || (GET_CODE (dest) == SUBREG
6722 && REG_P (SUBREG_REG (dest)))))
6724 SUBST (SET_DEST (x),
6725 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6726 dest));
6727 SUBST (SET_SRC (x), SUBREG_REG (src));
6729 src = SET_SRC (x), dest = SET_DEST (x);
6732 #ifdef HAVE_cc0
6733 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6734 in SRC. */
6735 if (dest == cc0_rtx
6736 && GET_CODE (src) == SUBREG
6737 && subreg_lowpart_p (src)
6738 && (GET_MODE_PRECISION (GET_MODE (src))
6739 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6741 rtx inner = SUBREG_REG (src);
6742 machine_mode inner_mode = GET_MODE (inner);
6744 /* Here we make sure that we don't have a sign bit on. */
6745 if (val_signbit_known_clear_p (GET_MODE (src),
6746 nonzero_bits (inner, inner_mode)))
6748 SUBST (SET_SRC (x), inner);
6749 src = SET_SRC (x);
6752 #endif
6754 #ifdef LOAD_EXTEND_OP
6755 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6756 would require a paradoxical subreg. Replace the subreg with a
6757 zero_extend to avoid the reload that would otherwise be required. */
6759 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6760 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6761 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6762 && SUBREG_BYTE (src) == 0
6763 && paradoxical_subreg_p (src)
6764 && MEM_P (SUBREG_REG (src)))
6766 SUBST (SET_SRC (x),
6767 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6768 GET_MODE (src), SUBREG_REG (src)));
6770 src = SET_SRC (x);
6772 #endif
6774 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6775 are comparing an item known to be 0 or -1 against 0, use a logical
6776 operation instead. Check for one of the arms being an IOR of the other
6777 arm with some value. We compute three terms to be IOR'ed together. In
6778 practice, at most two will be nonzero. Then we do the IOR's. */
6780 if (GET_CODE (dest) != PC
6781 && GET_CODE (src) == IF_THEN_ELSE
6782 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6783 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6784 && XEXP (XEXP (src, 0), 1) == const0_rtx
6785 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6786 #ifdef HAVE_conditional_move
6787 && ! can_conditionally_move_p (GET_MODE (src))
6788 #endif
6789 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6790 GET_MODE (XEXP (XEXP (src, 0), 0)))
6791 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6792 && ! side_effects_p (src))
6794 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6795 ? XEXP (src, 1) : XEXP (src, 2));
6796 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6797 ? XEXP (src, 2) : XEXP (src, 1));
6798 rtx term1 = const0_rtx, term2, term3;
6800 if (GET_CODE (true_rtx) == IOR
6801 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6802 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6803 else if (GET_CODE (true_rtx) == IOR
6804 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6805 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6806 else if (GET_CODE (false_rtx) == IOR
6807 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6808 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6809 else if (GET_CODE (false_rtx) == IOR
6810 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6811 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6813 term2 = simplify_gen_binary (AND, GET_MODE (src),
6814 XEXP (XEXP (src, 0), 0), true_rtx);
6815 term3 = simplify_gen_binary (AND, GET_MODE (src),
6816 simplify_gen_unary (NOT, GET_MODE (src),
6817 XEXP (XEXP (src, 0), 0),
6818 GET_MODE (src)),
6819 false_rtx);
6821 SUBST (SET_SRC (x),
6822 simplify_gen_binary (IOR, GET_MODE (src),
6823 simplify_gen_binary (IOR, GET_MODE (src),
6824 term1, term2),
6825 term3));
6827 src = SET_SRC (x);
6830 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6831 whole thing fail. */
6832 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6833 return src;
6834 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6835 return dest;
6836 else
6837 /* Convert this into a field assignment operation, if possible. */
6838 return make_field_assignment (x);
6841 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6842 result. */
6844 static rtx
6845 simplify_logical (rtx x)
6847 machine_mode mode = GET_MODE (x);
6848 rtx op0 = XEXP (x, 0);
6849 rtx op1 = XEXP (x, 1);
6851 switch (GET_CODE (x))
6853 case AND:
6854 /* We can call simplify_and_const_int only if we don't lose
6855 any (sign) bits when converting INTVAL (op1) to
6856 "unsigned HOST_WIDE_INT". */
6857 if (CONST_INT_P (op1)
6858 && (HWI_COMPUTABLE_MODE_P (mode)
6859 || INTVAL (op1) > 0))
6861 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6862 if (GET_CODE (x) != AND)
6863 return x;
6865 op0 = XEXP (x, 0);
6866 op1 = XEXP (x, 1);
6869 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6870 apply the distributive law and then the inverse distributive
6871 law to see if things simplify. */
6872 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6874 rtx result = distribute_and_simplify_rtx (x, 0);
6875 if (result)
6876 return result;
6878 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6880 rtx result = distribute_and_simplify_rtx (x, 1);
6881 if (result)
6882 return result;
6884 break;
6886 case IOR:
6887 /* If we have (ior (and A B) C), apply the distributive law and then
6888 the inverse distributive law to see if things simplify. */
6890 if (GET_CODE (op0) == AND)
6892 rtx result = distribute_and_simplify_rtx (x, 0);
6893 if (result)
6894 return result;
6897 if (GET_CODE (op1) == AND)
6899 rtx result = distribute_and_simplify_rtx (x, 1);
6900 if (result)
6901 return result;
6903 break;
6905 default:
6906 gcc_unreachable ();
6909 return x;
6912 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6913 operations" because they can be replaced with two more basic operations.
6914 ZERO_EXTEND is also considered "compound" because it can be replaced with
6915 an AND operation, which is simpler, though only one operation.
6917 The function expand_compound_operation is called with an rtx expression
6918 and will convert it to the appropriate shifts and AND operations,
6919 simplifying at each stage.
6921 The function make_compound_operation is called to convert an expression
6922 consisting of shifts and ANDs into the equivalent compound expression.
6923 It is the inverse of this function, loosely speaking. */
6925 static rtx
6926 expand_compound_operation (rtx x)
6928 unsigned HOST_WIDE_INT pos = 0, len;
6929 int unsignedp = 0;
6930 unsigned int modewidth;
6931 rtx tem;
6933 switch (GET_CODE (x))
6935 case ZERO_EXTEND:
6936 unsignedp = 1;
6937 case SIGN_EXTEND:
6938 /* We can't necessarily use a const_int for a multiword mode;
6939 it depends on implicitly extending the value.
6940 Since we don't know the right way to extend it,
6941 we can't tell whether the implicit way is right.
6943 Even for a mode that is no wider than a const_int,
6944 we can't win, because we need to sign extend one of its bits through
6945 the rest of it, and we don't know which bit. */
6946 if (CONST_INT_P (XEXP (x, 0)))
6947 return x;
6949 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6950 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6951 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6952 reloaded. If not for that, MEM's would very rarely be safe.
6954 Reject MODEs bigger than a word, because we might not be able
6955 to reference a two-register group starting with an arbitrary register
6956 (and currently gen_lowpart might crash for a SUBREG). */
6958 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6959 return x;
6961 /* Reject MODEs that aren't scalar integers because turning vector
6962 or complex modes into shifts causes problems. */
6964 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6965 return x;
6967 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6968 /* If the inner object has VOIDmode (the only way this can happen
6969 is if it is an ASM_OPERANDS), we can't do anything since we don't
6970 know how much masking to do. */
6971 if (len == 0)
6972 return x;
6974 break;
6976 case ZERO_EXTRACT:
6977 unsignedp = 1;
6979 /* ... fall through ... */
6981 case SIGN_EXTRACT:
6982 /* If the operand is a CLOBBER, just return it. */
6983 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6984 return XEXP (x, 0);
6986 if (!CONST_INT_P (XEXP (x, 1))
6987 || !CONST_INT_P (XEXP (x, 2))
6988 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6989 return x;
6991 /* Reject MODEs that aren't scalar integers because turning vector
6992 or complex modes into shifts causes problems. */
6994 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6995 return x;
6997 len = INTVAL (XEXP (x, 1));
6998 pos = INTVAL (XEXP (x, 2));
7000 /* This should stay within the object being extracted, fail otherwise. */
7001 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
7002 return x;
7004 if (BITS_BIG_ENDIAN)
7005 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
7007 break;
7009 default:
7010 return x;
7012 /* Convert sign extension to zero extension, if we know that the high
7013 bit is not set, as this is easier to optimize. It will be converted
7014 back to cheaper alternative in make_extraction. */
7015 if (GET_CODE (x) == SIGN_EXTEND
7016 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7017 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7018 & ~(((unsigned HOST_WIDE_INT)
7019 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
7020 >> 1))
7021 == 0)))
7023 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
7024 rtx temp2 = expand_compound_operation (temp);
7026 /* Make sure this is a profitable operation. */
7027 if (set_src_cost (x, optimize_this_for_speed_p)
7028 > set_src_cost (temp2, optimize_this_for_speed_p))
7029 return temp2;
7030 else if (set_src_cost (x, optimize_this_for_speed_p)
7031 > set_src_cost (temp, optimize_this_for_speed_p))
7032 return temp;
7033 else
7034 return x;
7037 /* We can optimize some special cases of ZERO_EXTEND. */
7038 if (GET_CODE (x) == ZERO_EXTEND)
7040 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7041 know that the last value didn't have any inappropriate bits
7042 set. */
7043 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7044 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7045 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7046 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
7047 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7048 return XEXP (XEXP (x, 0), 0);
7050 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7051 if (GET_CODE (XEXP (x, 0)) == SUBREG
7052 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7053 && subreg_lowpart_p (XEXP (x, 0))
7054 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7055 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
7056 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7057 return SUBREG_REG (XEXP (x, 0));
7059 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7060 is a comparison and STORE_FLAG_VALUE permits. This is like
7061 the first case, but it works even when GET_MODE (x) is larger
7062 than HOST_WIDE_INT. */
7063 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7064 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7065 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7066 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7067 <= HOST_BITS_PER_WIDE_INT)
7068 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7069 return XEXP (XEXP (x, 0), 0);
7071 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7072 if (GET_CODE (XEXP (x, 0)) == SUBREG
7073 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7074 && subreg_lowpart_p (XEXP (x, 0))
7075 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7076 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7077 <= HOST_BITS_PER_WIDE_INT)
7078 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7079 return SUBREG_REG (XEXP (x, 0));
7083 /* If we reach here, we want to return a pair of shifts. The inner
7084 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7085 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7086 logical depending on the value of UNSIGNEDP.
7088 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7089 converted into an AND of a shift.
7091 We must check for the case where the left shift would have a negative
7092 count. This can happen in a case like (x >> 31) & 255 on machines
7093 that can't shift by a constant. On those machines, we would first
7094 combine the shift with the AND to produce a variable-position
7095 extraction. Then the constant of 31 would be substituted in
7096 to produce such a position. */
7098 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7099 if (modewidth >= pos + len)
7101 machine_mode mode = GET_MODE (x);
7102 tem = gen_lowpart (mode, XEXP (x, 0));
7103 if (!tem || GET_CODE (tem) == CLOBBER)
7104 return x;
7105 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7106 tem, modewidth - pos - len);
7107 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7108 mode, tem, modewidth - len);
7110 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7111 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7112 simplify_shift_const (NULL_RTX, LSHIFTRT,
7113 GET_MODE (x),
7114 XEXP (x, 0), pos),
7115 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7116 else
7117 /* Any other cases we can't handle. */
7118 return x;
7120 /* If we couldn't do this for some reason, return the original
7121 expression. */
7122 if (GET_CODE (tem) == CLOBBER)
7123 return x;
7125 return tem;
7128 /* X is a SET which contains an assignment of one object into
7129 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7130 or certain SUBREGS). If possible, convert it into a series of
7131 logical operations.
7133 We half-heartedly support variable positions, but do not at all
7134 support variable lengths. */
7136 static const_rtx
7137 expand_field_assignment (const_rtx x)
7139 rtx inner;
7140 rtx pos; /* Always counts from low bit. */
7141 int len;
7142 rtx mask, cleared, masked;
7143 machine_mode compute_mode;
7145 /* Loop until we find something we can't simplify. */
7146 while (1)
7148 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7149 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7151 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7152 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7153 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7155 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7156 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7158 inner = XEXP (SET_DEST (x), 0);
7159 len = INTVAL (XEXP (SET_DEST (x), 1));
7160 pos = XEXP (SET_DEST (x), 2);
7162 /* A constant position should stay within the width of INNER. */
7163 if (CONST_INT_P (pos)
7164 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7165 break;
7167 if (BITS_BIG_ENDIAN)
7169 if (CONST_INT_P (pos))
7170 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7171 - INTVAL (pos));
7172 else if (GET_CODE (pos) == MINUS
7173 && CONST_INT_P (XEXP (pos, 1))
7174 && (INTVAL (XEXP (pos, 1))
7175 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7176 /* If position is ADJUST - X, new position is X. */
7177 pos = XEXP (pos, 0);
7178 else
7180 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7181 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7182 gen_int_mode (prec - len,
7183 GET_MODE (pos)),
7184 pos);
7189 /* A SUBREG between two modes that occupy the same numbers of words
7190 can be done by moving the SUBREG to the source. */
7191 else if (GET_CODE (SET_DEST (x)) == SUBREG
7192 /* We need SUBREGs to compute nonzero_bits properly. */
7193 && nonzero_sign_valid
7194 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7195 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7196 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7197 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7199 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7200 gen_lowpart
7201 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7202 SET_SRC (x)));
7203 continue;
7205 else
7206 break;
7208 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7209 inner = SUBREG_REG (inner);
7211 compute_mode = GET_MODE (inner);
7213 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7214 if (! SCALAR_INT_MODE_P (compute_mode))
7216 machine_mode imode;
7218 /* Don't do anything for vector or complex integral types. */
7219 if (! FLOAT_MODE_P (compute_mode))
7220 break;
7222 /* Try to find an integral mode to pun with. */
7223 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7224 if (imode == BLKmode)
7225 break;
7227 compute_mode = imode;
7228 inner = gen_lowpart (imode, inner);
7231 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7232 if (len >= HOST_BITS_PER_WIDE_INT)
7233 break;
7235 /* Now compute the equivalent expression. Make a copy of INNER
7236 for the SET_DEST in case it is a MEM into which we will substitute;
7237 we don't want shared RTL in that case. */
7238 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7239 compute_mode);
7240 cleared = simplify_gen_binary (AND, compute_mode,
7241 simplify_gen_unary (NOT, compute_mode,
7242 simplify_gen_binary (ASHIFT,
7243 compute_mode,
7244 mask, pos),
7245 compute_mode),
7246 inner);
7247 masked = simplify_gen_binary (ASHIFT, compute_mode,
7248 simplify_gen_binary (
7249 AND, compute_mode,
7250 gen_lowpart (compute_mode, SET_SRC (x)),
7251 mask),
7252 pos);
7254 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7255 simplify_gen_binary (IOR, compute_mode,
7256 cleared, masked));
7259 return x;
7262 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7263 it is an RTX that represents the (variable) starting position; otherwise,
7264 POS is the (constant) starting bit position. Both are counted from the LSB.
7266 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7268 IN_DEST is nonzero if this is a reference in the destination of a SET.
7269 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7270 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7271 be used.
7273 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7274 ZERO_EXTRACT should be built even for bits starting at bit 0.
7276 MODE is the desired mode of the result (if IN_DEST == 0).
7278 The result is an RTX for the extraction or NULL_RTX if the target
7279 can't handle it. */
7281 static rtx
7282 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7283 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7284 int in_dest, int in_compare)
7286 /* This mode describes the size of the storage area
7287 to fetch the overall value from. Within that, we
7288 ignore the POS lowest bits, etc. */
7289 machine_mode is_mode = GET_MODE (inner);
7290 machine_mode inner_mode;
7291 machine_mode wanted_inner_mode;
7292 machine_mode wanted_inner_reg_mode = word_mode;
7293 machine_mode pos_mode = word_mode;
7294 machine_mode extraction_mode = word_mode;
7295 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7296 rtx new_rtx = 0;
7297 rtx orig_pos_rtx = pos_rtx;
7298 HOST_WIDE_INT orig_pos;
7300 if (pos_rtx && CONST_INT_P (pos_rtx))
7301 pos = INTVAL (pos_rtx), pos_rtx = 0;
7303 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7305 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7306 consider just the QI as the memory to extract from.
7307 The subreg adds or removes high bits; its mode is
7308 irrelevant to the meaning of this extraction,
7309 since POS and LEN count from the lsb. */
7310 if (MEM_P (SUBREG_REG (inner)))
7311 is_mode = GET_MODE (SUBREG_REG (inner));
7312 inner = SUBREG_REG (inner);
7314 else if (GET_CODE (inner) == ASHIFT
7315 && CONST_INT_P (XEXP (inner, 1))
7316 && pos_rtx == 0 && pos == 0
7317 && len > UINTVAL (XEXP (inner, 1)))
7319 /* We're extracting the least significant bits of an rtx
7320 (ashift X (const_int C)), where LEN > C. Extract the
7321 least significant (LEN - C) bits of X, giving an rtx
7322 whose mode is MODE, then shift it left C times. */
7323 new_rtx = make_extraction (mode, XEXP (inner, 0),
7324 0, 0, len - INTVAL (XEXP (inner, 1)),
7325 unsignedp, in_dest, in_compare);
7326 if (new_rtx != 0)
7327 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7329 else if (GET_CODE (inner) == TRUNCATE)
7330 inner = XEXP (inner, 0);
7332 inner_mode = GET_MODE (inner);
7334 /* See if this can be done without an extraction. We never can if the
7335 width of the field is not the same as that of some integer mode. For
7336 registers, we can only avoid the extraction if the position is at the
7337 low-order bit and this is either not in the destination or we have the
7338 appropriate STRICT_LOW_PART operation available.
7340 For MEM, we can avoid an extract if the field starts on an appropriate
7341 boundary and we can change the mode of the memory reference. */
7343 if (tmode != BLKmode
7344 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7345 && !MEM_P (inner)
7346 && (inner_mode == tmode
7347 || !REG_P (inner)
7348 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7349 || reg_truncated_to_mode (tmode, inner))
7350 && (! in_dest
7351 || (REG_P (inner)
7352 && have_insn_for (STRICT_LOW_PART, tmode))))
7353 || (MEM_P (inner) && pos_rtx == 0
7354 && (pos
7355 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7356 : BITS_PER_UNIT)) == 0
7357 /* We can't do this if we are widening INNER_MODE (it
7358 may not be aligned, for one thing). */
7359 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7360 && (inner_mode == tmode
7361 || (! mode_dependent_address_p (XEXP (inner, 0),
7362 MEM_ADDR_SPACE (inner))
7363 && ! MEM_VOLATILE_P (inner))))))
7365 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7366 field. If the original and current mode are the same, we need not
7367 adjust the offset. Otherwise, we do if bytes big endian.
7369 If INNER is not a MEM, get a piece consisting of just the field
7370 of interest (in this case POS % BITS_PER_WORD must be 0). */
7372 if (MEM_P (inner))
7374 HOST_WIDE_INT offset;
7376 /* POS counts from lsb, but make OFFSET count in memory order. */
7377 if (BYTES_BIG_ENDIAN)
7378 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7379 else
7380 offset = pos / BITS_PER_UNIT;
7382 new_rtx = adjust_address_nv (inner, tmode, offset);
7384 else if (REG_P (inner))
7386 if (tmode != inner_mode)
7388 /* We can't call gen_lowpart in a DEST since we
7389 always want a SUBREG (see below) and it would sometimes
7390 return a new hard register. */
7391 if (pos || in_dest)
7393 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7395 if (WORDS_BIG_ENDIAN
7396 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7397 final_word = ((GET_MODE_SIZE (inner_mode)
7398 - GET_MODE_SIZE (tmode))
7399 / UNITS_PER_WORD) - final_word;
7401 final_word *= UNITS_PER_WORD;
7402 if (BYTES_BIG_ENDIAN &&
7403 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7404 final_word += (GET_MODE_SIZE (inner_mode)
7405 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7407 /* Avoid creating invalid subregs, for example when
7408 simplifying (x>>32)&255. */
7409 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7410 return NULL_RTX;
7412 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7414 else
7415 new_rtx = gen_lowpart (tmode, inner);
7417 else
7418 new_rtx = inner;
7420 else
7421 new_rtx = force_to_mode (inner, tmode,
7422 len >= HOST_BITS_PER_WIDE_INT
7423 ? ~(unsigned HOST_WIDE_INT) 0
7424 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7427 /* If this extraction is going into the destination of a SET,
7428 make a STRICT_LOW_PART unless we made a MEM. */
7430 if (in_dest)
7431 return (MEM_P (new_rtx) ? new_rtx
7432 : (GET_CODE (new_rtx) != SUBREG
7433 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7434 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7436 if (mode == tmode)
7437 return new_rtx;
7439 if (CONST_SCALAR_INT_P (new_rtx))
7440 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7441 mode, new_rtx, tmode);
7443 /* If we know that no extraneous bits are set, and that the high
7444 bit is not set, convert the extraction to the cheaper of
7445 sign and zero extension, that are equivalent in these cases. */
7446 if (flag_expensive_optimizations
7447 && (HWI_COMPUTABLE_MODE_P (tmode)
7448 && ((nonzero_bits (new_rtx, tmode)
7449 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7450 == 0)))
7452 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7453 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7455 /* Prefer ZERO_EXTENSION, since it gives more information to
7456 backends. */
7457 if (set_src_cost (temp, optimize_this_for_speed_p)
7458 <= set_src_cost (temp1, optimize_this_for_speed_p))
7459 return temp;
7460 return temp1;
7463 /* Otherwise, sign- or zero-extend unless we already are in the
7464 proper mode. */
7466 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7467 mode, new_rtx));
7470 /* Unless this is a COMPARE or we have a funny memory reference,
7471 don't do anything with zero-extending field extracts starting at
7472 the low-order bit since they are simple AND operations. */
7473 if (pos_rtx == 0 && pos == 0 && ! in_dest
7474 && ! in_compare && unsignedp)
7475 return 0;
7477 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7478 if the position is not a constant and the length is not 1. In all
7479 other cases, we would only be going outside our object in cases when
7480 an original shift would have been undefined. */
7481 if (MEM_P (inner)
7482 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7483 || (pos_rtx != 0 && len != 1)))
7484 return 0;
7486 enum extraction_pattern pattern = (in_dest ? EP_insv
7487 : unsignedp ? EP_extzv : EP_extv);
7489 /* If INNER is not from memory, we want it to have the mode of a register
7490 extraction pattern's structure operand, or word_mode if there is no
7491 such pattern. The same applies to extraction_mode and pos_mode
7492 and their respective operands.
7494 For memory, assume that the desired extraction_mode and pos_mode
7495 are the same as for a register operation, since at present we don't
7496 have named patterns for aligned memory structures. */
7497 struct extraction_insn insn;
7498 if (get_best_reg_extraction_insn (&insn, pattern,
7499 GET_MODE_BITSIZE (inner_mode), mode))
7501 wanted_inner_reg_mode = insn.struct_mode;
7502 pos_mode = insn.pos_mode;
7503 extraction_mode = insn.field_mode;
7506 /* Never narrow an object, since that might not be safe. */
7508 if (mode != VOIDmode
7509 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7510 extraction_mode = mode;
7512 if (!MEM_P (inner))
7513 wanted_inner_mode = wanted_inner_reg_mode;
7514 else
7516 /* Be careful not to go beyond the extracted object and maintain the
7517 natural alignment of the memory. */
7518 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7519 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7520 > GET_MODE_BITSIZE (wanted_inner_mode))
7522 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7523 gcc_assert (wanted_inner_mode != VOIDmode);
7527 orig_pos = pos;
7529 if (BITS_BIG_ENDIAN)
7531 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7532 BITS_BIG_ENDIAN style. If position is constant, compute new
7533 position. Otherwise, build subtraction.
7534 Note that POS is relative to the mode of the original argument.
7535 If it's a MEM we need to recompute POS relative to that.
7536 However, if we're extracting from (or inserting into) a register,
7537 we want to recompute POS relative to wanted_inner_mode. */
7538 int width = (MEM_P (inner)
7539 ? GET_MODE_BITSIZE (is_mode)
7540 : GET_MODE_BITSIZE (wanted_inner_mode));
7542 if (pos_rtx == 0)
7543 pos = width - len - pos;
7544 else
7545 pos_rtx
7546 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7547 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7548 pos_rtx);
7549 /* POS may be less than 0 now, but we check for that below.
7550 Note that it can only be less than 0 if !MEM_P (inner). */
7553 /* If INNER has a wider mode, and this is a constant extraction, try to
7554 make it smaller and adjust the byte to point to the byte containing
7555 the value. */
7556 if (wanted_inner_mode != VOIDmode
7557 && inner_mode != wanted_inner_mode
7558 && ! pos_rtx
7559 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7560 && MEM_P (inner)
7561 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7562 && ! MEM_VOLATILE_P (inner))
7564 int offset = 0;
7566 /* The computations below will be correct if the machine is big
7567 endian in both bits and bytes or little endian in bits and bytes.
7568 If it is mixed, we must adjust. */
7570 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7571 adjust OFFSET to compensate. */
7572 if (BYTES_BIG_ENDIAN
7573 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7574 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7576 /* We can now move to the desired byte. */
7577 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7578 * GET_MODE_SIZE (wanted_inner_mode);
7579 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7581 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7582 && is_mode != wanted_inner_mode)
7583 offset = (GET_MODE_SIZE (is_mode)
7584 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7586 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7589 /* If INNER is not memory, get it into the proper mode. If we are changing
7590 its mode, POS must be a constant and smaller than the size of the new
7591 mode. */
7592 else if (!MEM_P (inner))
7594 /* On the LHS, don't create paradoxical subregs implicitely truncating
7595 the register unless TRULY_NOOP_TRUNCATION. */
7596 if (in_dest
7597 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7598 wanted_inner_mode))
7599 return NULL_RTX;
7601 if (GET_MODE (inner) != wanted_inner_mode
7602 && (pos_rtx != 0
7603 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7604 return NULL_RTX;
7606 if (orig_pos < 0)
7607 return NULL_RTX;
7609 inner = force_to_mode (inner, wanted_inner_mode,
7610 pos_rtx
7611 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7612 ? ~(unsigned HOST_WIDE_INT) 0
7613 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7614 << orig_pos),
7618 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7619 have to zero extend. Otherwise, we can just use a SUBREG. */
7620 if (pos_rtx != 0
7621 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7623 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7624 GET_MODE (pos_rtx));
7626 /* If we know that no extraneous bits are set, and that the high
7627 bit is not set, convert extraction to cheaper one - either
7628 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7629 cases. */
7630 if (flag_expensive_optimizations
7631 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7632 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7633 & ~(((unsigned HOST_WIDE_INT)
7634 GET_MODE_MASK (GET_MODE (pos_rtx)))
7635 >> 1))
7636 == 0)))
7638 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7639 GET_MODE (pos_rtx));
7641 /* Prefer ZERO_EXTENSION, since it gives more information to
7642 backends. */
7643 if (set_src_cost (temp1, optimize_this_for_speed_p)
7644 < set_src_cost (temp, optimize_this_for_speed_p))
7645 temp = temp1;
7647 pos_rtx = temp;
7650 /* Make POS_RTX unless we already have it and it is correct. If we don't
7651 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7652 be a CONST_INT. */
7653 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7654 pos_rtx = orig_pos_rtx;
7656 else if (pos_rtx == 0)
7657 pos_rtx = GEN_INT (pos);
7659 /* Make the required operation. See if we can use existing rtx. */
7660 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7661 extraction_mode, inner, GEN_INT (len), pos_rtx);
7662 if (! in_dest)
7663 new_rtx = gen_lowpart (mode, new_rtx);
7665 return new_rtx;
7668 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7669 with any other operations in X. Return X without that shift if so. */
7671 static rtx
7672 extract_left_shift (rtx x, int count)
7674 enum rtx_code code = GET_CODE (x);
7675 machine_mode mode = GET_MODE (x);
7676 rtx tem;
7678 switch (code)
7680 case ASHIFT:
7681 /* This is the shift itself. If it is wide enough, we will return
7682 either the value being shifted if the shift count is equal to
7683 COUNT or a shift for the difference. */
7684 if (CONST_INT_P (XEXP (x, 1))
7685 && INTVAL (XEXP (x, 1)) >= count)
7686 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7687 INTVAL (XEXP (x, 1)) - count);
7688 break;
7690 case NEG: case NOT:
7691 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7692 return simplify_gen_unary (code, mode, tem, mode);
7694 break;
7696 case PLUS: case IOR: case XOR: case AND:
7697 /* If we can safely shift this constant and we find the inner shift,
7698 make a new operation. */
7699 if (CONST_INT_P (XEXP (x, 1))
7700 && (UINTVAL (XEXP (x, 1))
7701 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7702 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7704 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7705 return simplify_gen_binary (code, mode, tem,
7706 gen_int_mode (val, mode));
7708 break;
7710 default:
7711 break;
7714 return 0;
7717 /* Look at the expression rooted at X. Look for expressions
7718 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7719 Form these expressions.
7721 Return the new rtx, usually just X.
7723 Also, for machines like the VAX that don't have logical shift insns,
7724 try to convert logical to arithmetic shift operations in cases where
7725 they are equivalent. This undoes the canonicalizations to logical
7726 shifts done elsewhere.
7728 We try, as much as possible, to re-use rtl expressions to save memory.
7730 IN_CODE says what kind of expression we are processing. Normally, it is
7731 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7732 being kludges), it is MEM. When processing the arguments of a comparison
7733 or a COMPARE against zero, it is COMPARE. */
7736 make_compound_operation (rtx x, enum rtx_code in_code)
7738 enum rtx_code code = GET_CODE (x);
7739 machine_mode mode = GET_MODE (x);
7740 int mode_width = GET_MODE_PRECISION (mode);
7741 rtx rhs, lhs;
7742 enum rtx_code next_code;
7743 int i, j;
7744 rtx new_rtx = 0;
7745 rtx tem;
7746 const char *fmt;
7748 /* Select the code to be used in recursive calls. Once we are inside an
7749 address, we stay there. If we have a comparison, set to COMPARE,
7750 but once inside, go back to our default of SET. */
7752 next_code = (code == MEM ? MEM
7753 : ((code == PLUS || code == MINUS)
7754 && SCALAR_INT_MODE_P (mode)) ? MEM
7755 : ((code == COMPARE || COMPARISON_P (x))
7756 && XEXP (x, 1) == const0_rtx) ? COMPARE
7757 : in_code == COMPARE ? SET : in_code);
7759 /* Process depending on the code of this operation. If NEW is set
7760 nonzero, it will be returned. */
7762 switch (code)
7764 case ASHIFT:
7765 /* Convert shifts by constants into multiplications if inside
7766 an address. */
7767 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7768 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7769 && INTVAL (XEXP (x, 1)) >= 0
7770 && SCALAR_INT_MODE_P (mode))
7772 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7773 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7775 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7776 if (GET_CODE (new_rtx) == NEG)
7778 new_rtx = XEXP (new_rtx, 0);
7779 multval = -multval;
7781 multval = trunc_int_for_mode (multval, mode);
7782 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7784 break;
7786 case PLUS:
7787 lhs = XEXP (x, 0);
7788 rhs = XEXP (x, 1);
7789 lhs = make_compound_operation (lhs, next_code);
7790 rhs = make_compound_operation (rhs, next_code);
7791 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7792 && SCALAR_INT_MODE_P (mode))
7794 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7795 XEXP (lhs, 1));
7796 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7798 else if (GET_CODE (lhs) == MULT
7799 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7801 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7802 simplify_gen_unary (NEG, mode,
7803 XEXP (lhs, 1),
7804 mode));
7805 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7807 else
7809 SUBST (XEXP (x, 0), lhs);
7810 SUBST (XEXP (x, 1), rhs);
7811 goto maybe_swap;
7813 x = gen_lowpart (mode, new_rtx);
7814 goto maybe_swap;
7816 case MINUS:
7817 lhs = XEXP (x, 0);
7818 rhs = XEXP (x, 1);
7819 lhs = make_compound_operation (lhs, next_code);
7820 rhs = make_compound_operation (rhs, next_code);
7821 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7822 && SCALAR_INT_MODE_P (mode))
7824 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7825 XEXP (rhs, 1));
7826 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7828 else if (GET_CODE (rhs) == MULT
7829 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7831 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7832 simplify_gen_unary (NEG, mode,
7833 XEXP (rhs, 1),
7834 mode));
7835 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7837 else
7839 SUBST (XEXP (x, 0), lhs);
7840 SUBST (XEXP (x, 1), rhs);
7841 return x;
7843 return gen_lowpart (mode, new_rtx);
7845 case AND:
7846 /* If the second operand is not a constant, we can't do anything
7847 with it. */
7848 if (!CONST_INT_P (XEXP (x, 1)))
7849 break;
7851 /* If the constant is a power of two minus one and the first operand
7852 is a logical right shift, make an extraction. */
7853 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7854 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7856 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7857 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7858 0, in_code == COMPARE);
7861 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7862 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7863 && subreg_lowpart_p (XEXP (x, 0))
7864 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7865 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7867 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7868 next_code);
7869 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7870 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7871 0, in_code == COMPARE);
7873 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7874 else if ((GET_CODE (XEXP (x, 0)) == XOR
7875 || GET_CODE (XEXP (x, 0)) == IOR)
7876 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7877 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7878 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7880 /* Apply the distributive law, and then try to make extractions. */
7881 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7882 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7883 XEXP (x, 1)),
7884 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7885 XEXP (x, 1)));
7886 new_rtx = make_compound_operation (new_rtx, in_code);
7889 /* If we are have (and (rotate X C) M) and C is larger than the number
7890 of bits in M, this is an extraction. */
7892 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7893 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7894 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7895 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7897 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7898 new_rtx = make_extraction (mode, new_rtx,
7899 (GET_MODE_PRECISION (mode)
7900 - INTVAL (XEXP (XEXP (x, 0), 1))),
7901 NULL_RTX, i, 1, 0, in_code == COMPARE);
7904 /* On machines without logical shifts, if the operand of the AND is
7905 a logical shift and our mask turns off all the propagated sign
7906 bits, we can replace the logical shift with an arithmetic shift. */
7907 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7908 && !have_insn_for (LSHIFTRT, mode)
7909 && have_insn_for (ASHIFTRT, mode)
7910 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7911 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7912 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7913 && mode_width <= HOST_BITS_PER_WIDE_INT)
7915 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7917 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7918 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7919 SUBST (XEXP (x, 0),
7920 gen_rtx_ASHIFTRT (mode,
7921 make_compound_operation
7922 (XEXP (XEXP (x, 0), 0), next_code),
7923 XEXP (XEXP (x, 0), 1)));
7926 /* If the constant is one less than a power of two, this might be
7927 representable by an extraction even if no shift is present.
7928 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7929 we are in a COMPARE. */
7930 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7931 new_rtx = make_extraction (mode,
7932 make_compound_operation (XEXP (x, 0),
7933 next_code),
7934 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7936 /* If we are in a comparison and this is an AND with a power of two,
7937 convert this into the appropriate bit extract. */
7938 else if (in_code == COMPARE
7939 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7940 new_rtx = make_extraction (mode,
7941 make_compound_operation (XEXP (x, 0),
7942 next_code),
7943 i, NULL_RTX, 1, 1, 0, 1);
7945 break;
7947 case LSHIFTRT:
7948 /* If the sign bit is known to be zero, replace this with an
7949 arithmetic shift. */
7950 if (have_insn_for (ASHIFTRT, mode)
7951 && ! have_insn_for (LSHIFTRT, mode)
7952 && mode_width <= HOST_BITS_PER_WIDE_INT
7953 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7955 new_rtx = gen_rtx_ASHIFTRT (mode,
7956 make_compound_operation (XEXP (x, 0),
7957 next_code),
7958 XEXP (x, 1));
7959 break;
7962 /* ... fall through ... */
7964 case ASHIFTRT:
7965 lhs = XEXP (x, 0);
7966 rhs = XEXP (x, 1);
7968 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7969 this is a SIGN_EXTRACT. */
7970 if (CONST_INT_P (rhs)
7971 && GET_CODE (lhs) == ASHIFT
7972 && CONST_INT_P (XEXP (lhs, 1))
7973 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7974 && INTVAL (XEXP (lhs, 1)) >= 0
7975 && INTVAL (rhs) < mode_width)
7977 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7978 new_rtx = make_extraction (mode, new_rtx,
7979 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7980 NULL_RTX, mode_width - INTVAL (rhs),
7981 code == LSHIFTRT, 0, in_code == COMPARE);
7982 break;
7985 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7986 If so, try to merge the shifts into a SIGN_EXTEND. We could
7987 also do this for some cases of SIGN_EXTRACT, but it doesn't
7988 seem worth the effort; the case checked for occurs on Alpha. */
7990 if (!OBJECT_P (lhs)
7991 && ! (GET_CODE (lhs) == SUBREG
7992 && (OBJECT_P (SUBREG_REG (lhs))))
7993 && CONST_INT_P (rhs)
7994 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7995 && INTVAL (rhs) < mode_width
7996 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7997 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7998 0, NULL_RTX, mode_width - INTVAL (rhs),
7999 code == LSHIFTRT, 0, in_code == COMPARE);
8001 break;
8003 case SUBREG:
8004 /* Call ourselves recursively on the inner expression. If we are
8005 narrowing the object and it has a different RTL code from
8006 what it originally did, do this SUBREG as a force_to_mode. */
8008 rtx inner = SUBREG_REG (x), simplified;
8009 enum rtx_code subreg_code = in_code;
8011 /* If in_code is COMPARE, it isn't always safe to pass it through
8012 to the recursive make_compound_operation call. */
8013 if (subreg_code == COMPARE
8014 && (!subreg_lowpart_p (x)
8015 || GET_CODE (inner) == SUBREG
8016 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8017 is (const_int 0), rather than
8018 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
8019 || (GET_CODE (inner) == AND
8020 && CONST_INT_P (XEXP (inner, 1))
8021 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8022 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8023 >= GET_MODE_BITSIZE (mode))))
8024 subreg_code = SET;
8026 tem = make_compound_operation (inner, subreg_code);
8028 simplified
8029 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8030 if (simplified)
8031 tem = simplified;
8033 if (GET_CODE (tem) != GET_CODE (inner)
8034 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8035 && subreg_lowpart_p (x))
8037 rtx newer
8038 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
8040 /* If we have something other than a SUBREG, we might have
8041 done an expansion, so rerun ourselves. */
8042 if (GET_CODE (newer) != SUBREG)
8043 newer = make_compound_operation (newer, in_code);
8045 /* force_to_mode can expand compounds. If it just re-expanded the
8046 compound, use gen_lowpart to convert to the desired mode. */
8047 if (rtx_equal_p (newer, x)
8048 /* Likewise if it re-expanded the compound only partially.
8049 This happens for SUBREG of ZERO_EXTRACT if they extract
8050 the same number of bits. */
8051 || (GET_CODE (newer) == SUBREG
8052 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8053 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8054 && GET_CODE (inner) == AND
8055 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8056 return gen_lowpart (GET_MODE (x), tem);
8058 return newer;
8061 if (simplified)
8062 return tem;
8064 break;
8066 default:
8067 break;
8070 if (new_rtx)
8072 x = gen_lowpart (mode, new_rtx);
8073 code = GET_CODE (x);
8076 /* Now recursively process each operand of this operation. We need to
8077 handle ZERO_EXTEND specially so that we don't lose track of the
8078 inner mode. */
8079 if (GET_CODE (x) == ZERO_EXTEND)
8081 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8082 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8083 new_rtx, GET_MODE (XEXP (x, 0)));
8084 if (tem)
8085 return tem;
8086 SUBST (XEXP (x, 0), new_rtx);
8087 return x;
8090 fmt = GET_RTX_FORMAT (code);
8091 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8092 if (fmt[i] == 'e')
8094 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8095 SUBST (XEXP (x, i), new_rtx);
8097 else if (fmt[i] == 'E')
8098 for (j = 0; j < XVECLEN (x, i); j++)
8100 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8101 SUBST (XVECEXP (x, i, j), new_rtx);
8104 maybe_swap:
8105 /* If this is a commutative operation, the changes to the operands
8106 may have made it noncanonical. */
8107 if (COMMUTATIVE_ARITH_P (x)
8108 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8110 tem = XEXP (x, 0);
8111 SUBST (XEXP (x, 0), XEXP (x, 1));
8112 SUBST (XEXP (x, 1), tem);
8115 return x;
8118 /* Given M see if it is a value that would select a field of bits
8119 within an item, but not the entire word. Return -1 if not.
8120 Otherwise, return the starting position of the field, where 0 is the
8121 low-order bit.
8123 *PLEN is set to the length of the field. */
8125 static int
8126 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8128 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8129 int pos = m ? ctz_hwi (m) : -1;
8130 int len = 0;
8132 if (pos >= 0)
8133 /* Now shift off the low-order zero bits and see if we have a
8134 power of two minus 1. */
8135 len = exact_log2 ((m >> pos) + 1);
8137 if (len <= 0)
8138 pos = -1;
8140 *plen = len;
8141 return pos;
8144 /* If X refers to a register that equals REG in value, replace these
8145 references with REG. */
8146 static rtx
8147 canon_reg_for_combine (rtx x, rtx reg)
8149 rtx op0, op1, op2;
8150 const char *fmt;
8151 int i;
8152 bool copied;
8154 enum rtx_code code = GET_CODE (x);
8155 switch (GET_RTX_CLASS (code))
8157 case RTX_UNARY:
8158 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8159 if (op0 != XEXP (x, 0))
8160 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8161 GET_MODE (reg));
8162 break;
8164 case RTX_BIN_ARITH:
8165 case RTX_COMM_ARITH:
8166 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8167 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8168 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8169 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8170 break;
8172 case RTX_COMPARE:
8173 case RTX_COMM_COMPARE:
8174 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8175 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8176 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8177 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8178 GET_MODE (op0), op0, op1);
8179 break;
8181 case RTX_TERNARY:
8182 case RTX_BITFIELD_OPS:
8183 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8184 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8185 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8186 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8187 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8188 GET_MODE (op0), op0, op1, op2);
8190 case RTX_OBJ:
8191 if (REG_P (x))
8193 if (rtx_equal_p (get_last_value (reg), x)
8194 || rtx_equal_p (reg, get_last_value (x)))
8195 return reg;
8196 else
8197 break;
8200 /* fall through */
8202 default:
8203 fmt = GET_RTX_FORMAT (code);
8204 copied = false;
8205 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8206 if (fmt[i] == 'e')
8208 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8209 if (op != XEXP (x, i))
8211 if (!copied)
8213 copied = true;
8214 x = copy_rtx (x);
8216 XEXP (x, i) = op;
8219 else if (fmt[i] == 'E')
8221 int j;
8222 for (j = 0; j < XVECLEN (x, i); j++)
8224 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8225 if (op != XVECEXP (x, i, j))
8227 if (!copied)
8229 copied = true;
8230 x = copy_rtx (x);
8232 XVECEXP (x, i, j) = op;
8237 break;
8240 return x;
8243 /* Return X converted to MODE. If the value is already truncated to
8244 MODE we can just return a subreg even though in the general case we
8245 would need an explicit truncation. */
8247 static rtx
8248 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8250 if (!CONST_INT_P (x)
8251 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8252 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8253 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8255 /* Bit-cast X into an integer mode. */
8256 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8257 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8258 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8259 x, GET_MODE (x));
8262 return gen_lowpart (mode, x);
8265 /* See if X can be simplified knowing that we will only refer to it in
8266 MODE and will only refer to those bits that are nonzero in MASK.
8267 If other bits are being computed or if masking operations are done
8268 that select a superset of the bits in MASK, they can sometimes be
8269 ignored.
8271 Return a possibly simplified expression, but always convert X to
8272 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8274 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8275 are all off in X. This is used when X will be complemented, by either
8276 NOT, NEG, or XOR. */
8278 static rtx
8279 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8280 int just_select)
8282 enum rtx_code code = GET_CODE (x);
8283 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8284 machine_mode op_mode;
8285 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8286 rtx op0, op1, temp;
8288 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8289 code below will do the wrong thing since the mode of such an
8290 expression is VOIDmode.
8292 Also do nothing if X is a CLOBBER; this can happen if X was
8293 the return value from a call to gen_lowpart. */
8294 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8295 return x;
8297 /* We want to perform the operation in its present mode unless we know
8298 that the operation is valid in MODE, in which case we do the operation
8299 in MODE. */
8300 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8301 && have_insn_for (code, mode))
8302 ? mode : GET_MODE (x));
8304 /* It is not valid to do a right-shift in a narrower mode
8305 than the one it came in with. */
8306 if ((code == LSHIFTRT || code == ASHIFTRT)
8307 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8308 op_mode = GET_MODE (x);
8310 /* Truncate MASK to fit OP_MODE. */
8311 if (op_mode)
8312 mask &= GET_MODE_MASK (op_mode);
8314 /* When we have an arithmetic operation, or a shift whose count we
8315 do not know, we need to assume that all bits up to the highest-order
8316 bit in MASK will be needed. This is how we form such a mask. */
8317 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8318 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8319 else
8320 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8321 - 1);
8323 /* Determine what bits of X are guaranteed to be (non)zero. */
8324 nonzero = nonzero_bits (x, mode);
8326 /* If none of the bits in X are needed, return a zero. */
8327 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8328 x = const0_rtx;
8330 /* If X is a CONST_INT, return a new one. Do this here since the
8331 test below will fail. */
8332 if (CONST_INT_P (x))
8334 if (SCALAR_INT_MODE_P (mode))
8335 return gen_int_mode (INTVAL (x) & mask, mode);
8336 else
8338 x = GEN_INT (INTVAL (x) & mask);
8339 return gen_lowpart_common (mode, x);
8343 /* If X is narrower than MODE and we want all the bits in X's mode, just
8344 get X in the proper mode. */
8345 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8346 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8347 return gen_lowpart (mode, x);
8349 /* We can ignore the effect of a SUBREG if it narrows the mode or
8350 if the constant masks to zero all the bits the mode doesn't have. */
8351 if (GET_CODE (x) == SUBREG
8352 && subreg_lowpart_p (x)
8353 && ((GET_MODE_SIZE (GET_MODE (x))
8354 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8355 || (0 == (mask
8356 & GET_MODE_MASK (GET_MODE (x))
8357 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8358 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8360 /* The arithmetic simplifications here only work for scalar integer modes. */
8361 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8362 return gen_lowpart_or_truncate (mode, x);
8364 switch (code)
8366 case CLOBBER:
8367 /* If X is a (clobber (const_int)), return it since we know we are
8368 generating something that won't match. */
8369 return x;
8371 case SIGN_EXTEND:
8372 case ZERO_EXTEND:
8373 case ZERO_EXTRACT:
8374 case SIGN_EXTRACT:
8375 x = expand_compound_operation (x);
8376 if (GET_CODE (x) != code)
8377 return force_to_mode (x, mode, mask, next_select);
8378 break;
8380 case TRUNCATE:
8381 /* Similarly for a truncate. */
8382 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8384 case AND:
8385 /* If this is an AND with a constant, convert it into an AND
8386 whose constant is the AND of that constant with MASK. If it
8387 remains an AND of MASK, delete it since it is redundant. */
8389 if (CONST_INT_P (XEXP (x, 1)))
8391 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8392 mask & INTVAL (XEXP (x, 1)));
8394 /* If X is still an AND, see if it is an AND with a mask that
8395 is just some low-order bits. If so, and it is MASK, we don't
8396 need it. */
8398 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8399 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8400 == mask))
8401 x = XEXP (x, 0);
8403 /* If it remains an AND, try making another AND with the bits
8404 in the mode mask that aren't in MASK turned on. If the
8405 constant in the AND is wide enough, this might make a
8406 cheaper constant. */
8408 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8409 && GET_MODE_MASK (GET_MODE (x)) != mask
8410 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8412 unsigned HOST_WIDE_INT cval
8413 = UINTVAL (XEXP (x, 1))
8414 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8415 rtx y;
8417 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8418 gen_int_mode (cval, GET_MODE (x)));
8419 if (set_src_cost (y, optimize_this_for_speed_p)
8420 < set_src_cost (x, optimize_this_for_speed_p))
8421 x = y;
8424 break;
8427 goto binop;
8429 case PLUS:
8430 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8431 low-order bits (as in an alignment operation) and FOO is already
8432 aligned to that boundary, mask C1 to that boundary as well.
8433 This may eliminate that PLUS and, later, the AND. */
8436 unsigned int width = GET_MODE_PRECISION (mode);
8437 unsigned HOST_WIDE_INT smask = mask;
8439 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8440 number, sign extend it. */
8442 if (width < HOST_BITS_PER_WIDE_INT
8443 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8444 smask |= HOST_WIDE_INT_M1U << width;
8446 if (CONST_INT_P (XEXP (x, 1))
8447 && exact_log2 (- smask) >= 0
8448 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8449 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8450 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8451 (INTVAL (XEXP (x, 1)) & smask)),
8452 mode, smask, next_select);
8455 /* ... fall through ... */
8457 case MULT:
8458 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8459 most significant bit in MASK since carries from those bits will
8460 affect the bits we are interested in. */
8461 mask = fuller_mask;
8462 goto binop;
8464 case MINUS:
8465 /* If X is (minus C Y) where C's least set bit is larger than any bit
8466 in the mask, then we may replace with (neg Y). */
8467 if (CONST_INT_P (XEXP (x, 0))
8468 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8470 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8471 GET_MODE (x));
8472 return force_to_mode (x, mode, mask, next_select);
8475 /* Similarly, if C contains every bit in the fuller_mask, then we may
8476 replace with (not Y). */
8477 if (CONST_INT_P (XEXP (x, 0))
8478 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8480 x = simplify_gen_unary (NOT, GET_MODE (x),
8481 XEXP (x, 1), GET_MODE (x));
8482 return force_to_mode (x, mode, mask, next_select);
8485 mask = fuller_mask;
8486 goto binop;
8488 case IOR:
8489 case XOR:
8490 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8491 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8492 operation which may be a bitfield extraction. Ensure that the
8493 constant we form is not wider than the mode of X. */
8495 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8496 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8497 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8498 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8499 && CONST_INT_P (XEXP (x, 1))
8500 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8501 + floor_log2 (INTVAL (XEXP (x, 1))))
8502 < GET_MODE_PRECISION (GET_MODE (x)))
8503 && (UINTVAL (XEXP (x, 1))
8504 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8506 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8507 << INTVAL (XEXP (XEXP (x, 0), 1)),
8508 GET_MODE (x));
8509 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8510 XEXP (XEXP (x, 0), 0), temp);
8511 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8512 XEXP (XEXP (x, 0), 1));
8513 return force_to_mode (x, mode, mask, next_select);
8516 binop:
8517 /* For most binary operations, just propagate into the operation and
8518 change the mode if we have an operation of that mode. */
8520 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8521 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8523 /* If we ended up truncating both operands, truncate the result of the
8524 operation instead. */
8525 if (GET_CODE (op0) == TRUNCATE
8526 && GET_CODE (op1) == TRUNCATE)
8528 op0 = XEXP (op0, 0);
8529 op1 = XEXP (op1, 0);
8532 op0 = gen_lowpart_or_truncate (op_mode, op0);
8533 op1 = gen_lowpart_or_truncate (op_mode, op1);
8535 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8536 x = simplify_gen_binary (code, op_mode, op0, op1);
8537 break;
8539 case ASHIFT:
8540 /* For left shifts, do the same, but just for the first operand.
8541 However, we cannot do anything with shifts where we cannot
8542 guarantee that the counts are smaller than the size of the mode
8543 because such a count will have a different meaning in a
8544 wider mode. */
8546 if (! (CONST_INT_P (XEXP (x, 1))
8547 && INTVAL (XEXP (x, 1)) >= 0
8548 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8549 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8550 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8551 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8552 break;
8554 /* If the shift count is a constant and we can do arithmetic in
8555 the mode of the shift, refine which bits we need. Otherwise, use the
8556 conservative form of the mask. */
8557 if (CONST_INT_P (XEXP (x, 1))
8558 && INTVAL (XEXP (x, 1)) >= 0
8559 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8560 && HWI_COMPUTABLE_MODE_P (op_mode))
8561 mask >>= INTVAL (XEXP (x, 1));
8562 else
8563 mask = fuller_mask;
8565 op0 = gen_lowpart_or_truncate (op_mode,
8566 force_to_mode (XEXP (x, 0), op_mode,
8567 mask, next_select));
8569 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8570 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8571 break;
8573 case LSHIFTRT:
8574 /* Here we can only do something if the shift count is a constant,
8575 this shift constant is valid for the host, and we can do arithmetic
8576 in OP_MODE. */
8578 if (CONST_INT_P (XEXP (x, 1))
8579 && INTVAL (XEXP (x, 1)) >= 0
8580 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8581 && HWI_COMPUTABLE_MODE_P (op_mode))
8583 rtx inner = XEXP (x, 0);
8584 unsigned HOST_WIDE_INT inner_mask;
8586 /* Select the mask of the bits we need for the shift operand. */
8587 inner_mask = mask << INTVAL (XEXP (x, 1));
8589 /* We can only change the mode of the shift if we can do arithmetic
8590 in the mode of the shift and INNER_MASK is no wider than the
8591 width of X's mode. */
8592 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8593 op_mode = GET_MODE (x);
8595 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8597 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8598 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8601 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8602 shift and AND produces only copies of the sign bit (C2 is one less
8603 than a power of two), we can do this with just a shift. */
8605 if (GET_CODE (x) == LSHIFTRT
8606 && CONST_INT_P (XEXP (x, 1))
8607 /* The shift puts one of the sign bit copies in the least significant
8608 bit. */
8609 && ((INTVAL (XEXP (x, 1))
8610 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8611 >= GET_MODE_PRECISION (GET_MODE (x)))
8612 && exact_log2 (mask + 1) >= 0
8613 /* Number of bits left after the shift must be more than the mask
8614 needs. */
8615 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8616 <= GET_MODE_PRECISION (GET_MODE (x)))
8617 /* Must be more sign bit copies than the mask needs. */
8618 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8619 >= exact_log2 (mask + 1)))
8620 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8621 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8622 - exact_log2 (mask + 1)));
8624 goto shiftrt;
8626 case ASHIFTRT:
8627 /* If we are just looking for the sign bit, we don't need this shift at
8628 all, even if it has a variable count. */
8629 if (val_signbit_p (GET_MODE (x), mask))
8630 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8632 /* If this is a shift by a constant, get a mask that contains those bits
8633 that are not copies of the sign bit. We then have two cases: If
8634 MASK only includes those bits, this can be a logical shift, which may
8635 allow simplifications. If MASK is a single-bit field not within
8636 those bits, we are requesting a copy of the sign bit and hence can
8637 shift the sign bit to the appropriate location. */
8639 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8640 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8642 int i;
8644 /* If the considered data is wider than HOST_WIDE_INT, we can't
8645 represent a mask for all its bits in a single scalar.
8646 But we only care about the lower bits, so calculate these. */
8648 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8650 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8652 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8653 is the number of bits a full-width mask would have set.
8654 We need only shift if these are fewer than nonzero can
8655 hold. If not, we must keep all bits set in nonzero. */
8657 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8658 < HOST_BITS_PER_WIDE_INT)
8659 nonzero >>= INTVAL (XEXP (x, 1))
8660 + HOST_BITS_PER_WIDE_INT
8661 - GET_MODE_PRECISION (GET_MODE (x)) ;
8663 else
8665 nonzero = GET_MODE_MASK (GET_MODE (x));
8666 nonzero >>= INTVAL (XEXP (x, 1));
8669 if ((mask & ~nonzero) == 0)
8671 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8672 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8673 if (GET_CODE (x) != ASHIFTRT)
8674 return force_to_mode (x, mode, mask, next_select);
8677 else if ((i = exact_log2 (mask)) >= 0)
8679 x = simplify_shift_const
8680 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8681 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8683 if (GET_CODE (x) != ASHIFTRT)
8684 return force_to_mode (x, mode, mask, next_select);
8688 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8689 even if the shift count isn't a constant. */
8690 if (mask == 1)
8691 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8692 XEXP (x, 0), XEXP (x, 1));
8694 shiftrt:
8696 /* If this is a zero- or sign-extension operation that just affects bits
8697 we don't care about, remove it. Be sure the call above returned
8698 something that is still a shift. */
8700 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8701 && CONST_INT_P (XEXP (x, 1))
8702 && INTVAL (XEXP (x, 1)) >= 0
8703 && (INTVAL (XEXP (x, 1))
8704 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8705 && GET_CODE (XEXP (x, 0)) == ASHIFT
8706 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8707 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8708 next_select);
8710 break;
8712 case ROTATE:
8713 case ROTATERT:
8714 /* If the shift count is constant and we can do computations
8715 in the mode of X, compute where the bits we care about are.
8716 Otherwise, we can't do anything. Don't change the mode of
8717 the shift or propagate MODE into the shift, though. */
8718 if (CONST_INT_P (XEXP (x, 1))
8719 && INTVAL (XEXP (x, 1)) >= 0)
8721 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8722 GET_MODE (x),
8723 gen_int_mode (mask, GET_MODE (x)),
8724 XEXP (x, 1));
8725 if (temp && CONST_INT_P (temp))
8726 x = simplify_gen_binary (code, GET_MODE (x),
8727 force_to_mode (XEXP (x, 0), GET_MODE (x),
8728 INTVAL (temp), next_select),
8729 XEXP (x, 1));
8731 break;
8733 case NEG:
8734 /* If we just want the low-order bit, the NEG isn't needed since it
8735 won't change the low-order bit. */
8736 if (mask == 1)
8737 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8739 /* We need any bits less significant than the most significant bit in
8740 MASK since carries from those bits will affect the bits we are
8741 interested in. */
8742 mask = fuller_mask;
8743 goto unop;
8745 case NOT:
8746 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8747 same as the XOR case above. Ensure that the constant we form is not
8748 wider than the mode of X. */
8750 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8751 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8752 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8753 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8754 < GET_MODE_PRECISION (GET_MODE (x)))
8755 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8757 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8758 GET_MODE (x));
8759 temp = simplify_gen_binary (XOR, GET_MODE (x),
8760 XEXP (XEXP (x, 0), 0), temp);
8761 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8762 temp, XEXP (XEXP (x, 0), 1));
8764 return force_to_mode (x, mode, mask, next_select);
8767 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8768 use the full mask inside the NOT. */
8769 mask = fuller_mask;
8771 unop:
8772 op0 = gen_lowpart_or_truncate (op_mode,
8773 force_to_mode (XEXP (x, 0), mode, mask,
8774 next_select));
8775 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8776 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8777 break;
8779 case NE:
8780 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8781 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8782 which is equal to STORE_FLAG_VALUE. */
8783 if ((mask & ~STORE_FLAG_VALUE) == 0
8784 && XEXP (x, 1) == const0_rtx
8785 && GET_MODE (XEXP (x, 0)) == mode
8786 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8787 && (nonzero_bits (XEXP (x, 0), mode)
8788 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8789 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8791 break;
8793 case IF_THEN_ELSE:
8794 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8795 written in a narrower mode. We play it safe and do not do so. */
8797 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8798 force_to_mode (XEXP (x, 1), mode,
8799 mask, next_select));
8800 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8801 force_to_mode (XEXP (x, 2), mode,
8802 mask, next_select));
8803 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8804 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8805 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8806 op0, op1);
8807 break;
8809 default:
8810 break;
8813 /* Ensure we return a value of the proper mode. */
8814 return gen_lowpart_or_truncate (mode, x);
8817 /* Return nonzero if X is an expression that has one of two values depending on
8818 whether some other value is zero or nonzero. In that case, we return the
8819 value that is being tested, *PTRUE is set to the value if the rtx being
8820 returned has a nonzero value, and *PFALSE is set to the other alternative.
8822 If we return zero, we set *PTRUE and *PFALSE to X. */
8824 static rtx
8825 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8827 machine_mode mode = GET_MODE (x);
8828 enum rtx_code code = GET_CODE (x);
8829 rtx cond0, cond1, true0, true1, false0, false1;
8830 unsigned HOST_WIDE_INT nz;
8832 /* If we are comparing a value against zero, we are done. */
8833 if ((code == NE || code == EQ)
8834 && XEXP (x, 1) == const0_rtx)
8836 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8837 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8838 return XEXP (x, 0);
8841 /* If this is a unary operation whose operand has one of two values, apply
8842 our opcode to compute those values. */
8843 else if (UNARY_P (x)
8844 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8846 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8847 *pfalse = simplify_gen_unary (code, mode, false0,
8848 GET_MODE (XEXP (x, 0)));
8849 return cond0;
8852 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8853 make can't possibly match and would suppress other optimizations. */
8854 else if (code == COMPARE)
8857 /* If this is a binary operation, see if either side has only one of two
8858 values. If either one does or if both do and they are conditional on
8859 the same value, compute the new true and false values. */
8860 else if (BINARY_P (x))
8862 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8863 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8865 if ((cond0 != 0 || cond1 != 0)
8866 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8868 /* If if_then_else_cond returned zero, then true/false are the
8869 same rtl. We must copy one of them to prevent invalid rtl
8870 sharing. */
8871 if (cond0 == 0)
8872 true0 = copy_rtx (true0);
8873 else if (cond1 == 0)
8874 true1 = copy_rtx (true1);
8876 if (COMPARISON_P (x))
8878 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8879 true0, true1);
8880 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8881 false0, false1);
8883 else
8885 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8886 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8889 return cond0 ? cond0 : cond1;
8892 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8893 operands is zero when the other is nonzero, and vice-versa,
8894 and STORE_FLAG_VALUE is 1 or -1. */
8896 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8897 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8898 || code == UMAX)
8899 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8901 rtx op0 = XEXP (XEXP (x, 0), 1);
8902 rtx op1 = XEXP (XEXP (x, 1), 1);
8904 cond0 = XEXP (XEXP (x, 0), 0);
8905 cond1 = XEXP (XEXP (x, 1), 0);
8907 if (COMPARISON_P (cond0)
8908 && COMPARISON_P (cond1)
8909 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8910 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8911 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8912 || ((swap_condition (GET_CODE (cond0))
8913 == reversed_comparison_code (cond1, NULL))
8914 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8915 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8916 && ! side_effects_p (x))
8918 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8919 *pfalse = simplify_gen_binary (MULT, mode,
8920 (code == MINUS
8921 ? simplify_gen_unary (NEG, mode,
8922 op1, mode)
8923 : op1),
8924 const_true_rtx);
8925 return cond0;
8929 /* Similarly for MULT, AND and UMIN, except that for these the result
8930 is always zero. */
8931 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8932 && (code == MULT || code == AND || code == UMIN)
8933 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8935 cond0 = XEXP (XEXP (x, 0), 0);
8936 cond1 = XEXP (XEXP (x, 1), 0);
8938 if (COMPARISON_P (cond0)
8939 && COMPARISON_P (cond1)
8940 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8941 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8942 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8943 || ((swap_condition (GET_CODE (cond0))
8944 == reversed_comparison_code (cond1, NULL))
8945 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8946 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8947 && ! side_effects_p (x))
8949 *ptrue = *pfalse = const0_rtx;
8950 return cond0;
8955 else if (code == IF_THEN_ELSE)
8957 /* If we have IF_THEN_ELSE already, extract the condition and
8958 canonicalize it if it is NE or EQ. */
8959 cond0 = XEXP (x, 0);
8960 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8961 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8962 return XEXP (cond0, 0);
8963 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8965 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8966 return XEXP (cond0, 0);
8968 else
8969 return cond0;
8972 /* If X is a SUBREG, we can narrow both the true and false values
8973 if the inner expression, if there is a condition. */
8974 else if (code == SUBREG
8975 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8976 &true0, &false0)))
8978 true0 = simplify_gen_subreg (mode, true0,
8979 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8980 false0 = simplify_gen_subreg (mode, false0,
8981 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8982 if (true0 && false0)
8984 *ptrue = true0;
8985 *pfalse = false0;
8986 return cond0;
8990 /* If X is a constant, this isn't special and will cause confusions
8991 if we treat it as such. Likewise if it is equivalent to a constant. */
8992 else if (CONSTANT_P (x)
8993 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8996 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8997 will be least confusing to the rest of the compiler. */
8998 else if (mode == BImode)
9000 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9001 return x;
9004 /* If X is known to be either 0 or -1, those are the true and
9005 false values when testing X. */
9006 else if (x == constm1_rtx || x == const0_rtx
9007 || (mode != VOIDmode
9008 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
9010 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9011 return x;
9014 /* Likewise for 0 or a single bit. */
9015 else if (HWI_COMPUTABLE_MODE_P (mode)
9016 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
9018 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9019 return x;
9022 /* Otherwise fail; show no condition with true and false values the same. */
9023 *ptrue = *pfalse = x;
9024 return 0;
9027 /* Return the value of expression X given the fact that condition COND
9028 is known to be true when applied to REG as its first operand and VAL
9029 as its second. X is known to not be shared and so can be modified in
9030 place.
9032 We only handle the simplest cases, and specifically those cases that
9033 arise with IF_THEN_ELSE expressions. */
9035 static rtx
9036 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9038 enum rtx_code code = GET_CODE (x);
9039 rtx temp;
9040 const char *fmt;
9041 int i, j;
9043 if (side_effects_p (x))
9044 return x;
9046 /* If either operand of the condition is a floating point value,
9047 then we have to avoid collapsing an EQ comparison. */
9048 if (cond == EQ
9049 && rtx_equal_p (x, reg)
9050 && ! FLOAT_MODE_P (GET_MODE (x))
9051 && ! FLOAT_MODE_P (GET_MODE (val)))
9052 return val;
9054 if (cond == UNEQ && rtx_equal_p (x, reg))
9055 return val;
9057 /* If X is (abs REG) and we know something about REG's relationship
9058 with zero, we may be able to simplify this. */
9060 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9061 switch (cond)
9063 case GE: case GT: case EQ:
9064 return XEXP (x, 0);
9065 case LT: case LE:
9066 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9067 XEXP (x, 0),
9068 GET_MODE (XEXP (x, 0)));
9069 default:
9070 break;
9073 /* The only other cases we handle are MIN, MAX, and comparisons if the
9074 operands are the same as REG and VAL. */
9076 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9078 if (rtx_equal_p (XEXP (x, 0), val))
9079 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
9081 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9083 if (COMPARISON_P (x))
9085 if (comparison_dominates_p (cond, code))
9086 return const_true_rtx;
9088 code = reversed_comparison_code (x, NULL);
9089 if (code != UNKNOWN
9090 && comparison_dominates_p (cond, code))
9091 return const0_rtx;
9092 else
9093 return x;
9095 else if (code == SMAX || code == SMIN
9096 || code == UMIN || code == UMAX)
9098 int unsignedp = (code == UMIN || code == UMAX);
9100 /* Do not reverse the condition when it is NE or EQ.
9101 This is because we cannot conclude anything about
9102 the value of 'SMAX (x, y)' when x is not equal to y,
9103 but we can when x equals y. */
9104 if ((code == SMAX || code == UMAX)
9105 && ! (cond == EQ || cond == NE))
9106 cond = reverse_condition (cond);
9108 switch (cond)
9110 case GE: case GT:
9111 return unsignedp ? x : XEXP (x, 1);
9112 case LE: case LT:
9113 return unsignedp ? x : XEXP (x, 0);
9114 case GEU: case GTU:
9115 return unsignedp ? XEXP (x, 1) : x;
9116 case LEU: case LTU:
9117 return unsignedp ? XEXP (x, 0) : x;
9118 default:
9119 break;
9124 else if (code == SUBREG)
9126 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9127 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9129 if (SUBREG_REG (x) != r)
9131 /* We must simplify subreg here, before we lose track of the
9132 original inner_mode. */
9133 new_rtx = simplify_subreg (GET_MODE (x), r,
9134 inner_mode, SUBREG_BYTE (x));
9135 if (new_rtx)
9136 return new_rtx;
9137 else
9138 SUBST (SUBREG_REG (x), r);
9141 return x;
9143 /* We don't have to handle SIGN_EXTEND here, because even in the
9144 case of replacing something with a modeless CONST_INT, a
9145 CONST_INT is already (supposed to be) a valid sign extension for
9146 its narrower mode, which implies it's already properly
9147 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9148 story is different. */
9149 else if (code == ZERO_EXTEND)
9151 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9152 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9154 if (XEXP (x, 0) != r)
9156 /* We must simplify the zero_extend here, before we lose
9157 track of the original inner_mode. */
9158 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9159 r, inner_mode);
9160 if (new_rtx)
9161 return new_rtx;
9162 else
9163 SUBST (XEXP (x, 0), r);
9166 return x;
9169 fmt = GET_RTX_FORMAT (code);
9170 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9172 if (fmt[i] == 'e')
9173 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9174 else if (fmt[i] == 'E')
9175 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9176 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9177 cond, reg, val));
9180 return x;
9183 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9184 assignment as a field assignment. */
9186 static int
9187 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9189 if (widen_x && GET_MODE (x) != GET_MODE (y))
9191 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (y)))
9192 return 0;
9193 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9194 return 0;
9195 /* For big endian, adjust the memory offset. */
9196 if (BYTES_BIG_ENDIAN)
9197 x = adjust_address_nv (x, GET_MODE (y),
9198 -subreg_lowpart_offset (GET_MODE (x),
9199 GET_MODE (y)));
9200 else
9201 x = adjust_address_nv (x, GET_MODE (y), 0);
9204 if (x == y || rtx_equal_p (x, y))
9205 return 1;
9207 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9208 return 0;
9210 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9211 Note that all SUBREGs of MEM are paradoxical; otherwise they
9212 would have been rewritten. */
9213 if (MEM_P (x) && GET_CODE (y) == SUBREG
9214 && MEM_P (SUBREG_REG (y))
9215 && rtx_equal_p (SUBREG_REG (y),
9216 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9217 return 1;
9219 if (MEM_P (y) && GET_CODE (x) == SUBREG
9220 && MEM_P (SUBREG_REG (x))
9221 && rtx_equal_p (SUBREG_REG (x),
9222 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9223 return 1;
9225 /* We used to see if get_last_value of X and Y were the same but that's
9226 not correct. In one direction, we'll cause the assignment to have
9227 the wrong destination and in the case, we'll import a register into this
9228 insn that might have already have been dead. So fail if none of the
9229 above cases are true. */
9230 return 0;
9233 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9234 Return that assignment if so.
9236 We only handle the most common cases. */
9238 static rtx
9239 make_field_assignment (rtx x)
9241 rtx dest = SET_DEST (x);
9242 rtx src = SET_SRC (x);
9243 rtx assign;
9244 rtx rhs, lhs;
9245 HOST_WIDE_INT c1;
9246 HOST_WIDE_INT pos;
9247 unsigned HOST_WIDE_INT len;
9248 rtx other;
9249 machine_mode mode;
9251 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9252 a clear of a one-bit field. We will have changed it to
9253 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9254 for a SUBREG. */
9256 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9257 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9258 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9259 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9261 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9262 1, 1, 1, 0);
9263 if (assign != 0)
9264 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9265 return x;
9268 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9269 && subreg_lowpart_p (XEXP (src, 0))
9270 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9271 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9272 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9273 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9274 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9275 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9277 assign = make_extraction (VOIDmode, dest, 0,
9278 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9279 1, 1, 1, 0);
9280 if (assign != 0)
9281 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9282 return x;
9285 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9286 one-bit field. */
9287 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9288 && XEXP (XEXP (src, 0), 0) == const1_rtx
9289 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9291 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9292 1, 1, 1, 0);
9293 if (assign != 0)
9294 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9295 return x;
9298 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9299 SRC is an AND with all bits of that field set, then we can discard
9300 the AND. */
9301 if (GET_CODE (dest) == ZERO_EXTRACT
9302 && CONST_INT_P (XEXP (dest, 1))
9303 && GET_CODE (src) == AND
9304 && CONST_INT_P (XEXP (src, 1)))
9306 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9307 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9308 unsigned HOST_WIDE_INT ze_mask;
9310 if (width >= HOST_BITS_PER_WIDE_INT)
9311 ze_mask = -1;
9312 else
9313 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9315 /* Complete overlap. We can remove the source AND. */
9316 if ((and_mask & ze_mask) == ze_mask)
9317 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9319 /* Partial overlap. We can reduce the source AND. */
9320 if ((and_mask & ze_mask) != and_mask)
9322 mode = GET_MODE (src);
9323 src = gen_rtx_AND (mode, XEXP (src, 0),
9324 gen_int_mode (and_mask & ze_mask, mode));
9325 return gen_rtx_SET (VOIDmode, dest, src);
9329 /* The other case we handle is assignments into a constant-position
9330 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9331 a mask that has all one bits except for a group of zero bits and
9332 OTHER is known to have zeros where C1 has ones, this is such an
9333 assignment. Compute the position and length from C1. Shift OTHER
9334 to the appropriate position, force it to the required mode, and
9335 make the extraction. Check for the AND in both operands. */
9337 /* One or more SUBREGs might obscure the constant-position field
9338 assignment. The first one we are likely to encounter is an outer
9339 narrowing SUBREG, which we can just strip for the purposes of
9340 identifying the constant-field assignment. */
9341 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src))
9342 src = SUBREG_REG (src);
9344 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9345 return x;
9347 rhs = expand_compound_operation (XEXP (src, 0));
9348 lhs = expand_compound_operation (XEXP (src, 1));
9350 if (GET_CODE (rhs) == AND
9351 && CONST_INT_P (XEXP (rhs, 1))
9352 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9353 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9354 /* The second SUBREG that might get in the way is a paradoxical
9355 SUBREG around the first operand of the AND. We want to
9356 pretend the operand is as wide as the destination here. We
9357 do this by adjusting the MEM to wider mode for the sole
9358 purpose of the call to rtx_equal_for_field_assignment_p. Also
9359 note this trick only works for MEMs. */
9360 else if (GET_CODE (rhs) == AND
9361 && paradoxical_subreg_p (XEXP (rhs, 0))
9362 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9363 && CONST_INT_P (XEXP (rhs, 1))
9364 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9365 dest, true))
9366 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9367 else if (GET_CODE (lhs) == AND
9368 && CONST_INT_P (XEXP (lhs, 1))
9369 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9370 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9371 /* The second SUBREG that might get in the way is a paradoxical
9372 SUBREG around the first operand of the AND. We want to
9373 pretend the operand is as wide as the destination here. We
9374 do this by adjusting the MEM to wider mode for the sole
9375 purpose of the call to rtx_equal_for_field_assignment_p. Also
9376 note this trick only works for MEMs. */
9377 else if (GET_CODE (lhs) == AND
9378 && paradoxical_subreg_p (XEXP (lhs, 0))
9379 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9380 && CONST_INT_P (XEXP (lhs, 1))
9381 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9382 dest, true))
9383 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9384 else
9385 return x;
9387 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9388 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9389 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9390 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9391 return x;
9393 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9394 if (assign == 0)
9395 return x;
9397 /* The mode to use for the source is the mode of the assignment, or of
9398 what is inside a possible STRICT_LOW_PART. */
9399 mode = (GET_CODE (assign) == STRICT_LOW_PART
9400 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9402 /* Shift OTHER right POS places and make it the source, restricting it
9403 to the proper length and mode. */
9405 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9406 GET_MODE (src),
9407 other, pos),
9408 dest);
9409 src = force_to_mode (src, mode,
9410 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9411 ? ~(unsigned HOST_WIDE_INT) 0
9412 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9415 /* If SRC is masked by an AND that does not make a difference in
9416 the value being stored, strip it. */
9417 if (GET_CODE (assign) == ZERO_EXTRACT
9418 && CONST_INT_P (XEXP (assign, 1))
9419 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9420 && GET_CODE (src) == AND
9421 && CONST_INT_P (XEXP (src, 1))
9422 && UINTVAL (XEXP (src, 1))
9423 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9424 src = XEXP (src, 0);
9426 return gen_rtx_SET (VOIDmode, assign, src);
9429 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9430 if so. */
9432 static rtx
9433 apply_distributive_law (rtx x)
9435 enum rtx_code code = GET_CODE (x);
9436 enum rtx_code inner_code;
9437 rtx lhs, rhs, other;
9438 rtx tem;
9440 /* Distributivity is not true for floating point as it can change the
9441 value. So we don't do it unless -funsafe-math-optimizations. */
9442 if (FLOAT_MODE_P (GET_MODE (x))
9443 && ! flag_unsafe_math_optimizations)
9444 return x;
9446 /* The outer operation can only be one of the following: */
9447 if (code != IOR && code != AND && code != XOR
9448 && code != PLUS && code != MINUS)
9449 return x;
9451 lhs = XEXP (x, 0);
9452 rhs = XEXP (x, 1);
9454 /* If either operand is a primitive we can't do anything, so get out
9455 fast. */
9456 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9457 return x;
9459 lhs = expand_compound_operation (lhs);
9460 rhs = expand_compound_operation (rhs);
9461 inner_code = GET_CODE (lhs);
9462 if (inner_code != GET_CODE (rhs))
9463 return x;
9465 /* See if the inner and outer operations distribute. */
9466 switch (inner_code)
9468 case LSHIFTRT:
9469 case ASHIFTRT:
9470 case AND:
9471 case IOR:
9472 /* These all distribute except over PLUS. */
9473 if (code == PLUS || code == MINUS)
9474 return x;
9475 break;
9477 case MULT:
9478 if (code != PLUS && code != MINUS)
9479 return x;
9480 break;
9482 case ASHIFT:
9483 /* This is also a multiply, so it distributes over everything. */
9484 break;
9486 /* This used to handle SUBREG, but this turned out to be counter-
9487 productive, since (subreg (op ...)) usually is not handled by
9488 insn patterns, and this "optimization" therefore transformed
9489 recognizable patterns into unrecognizable ones. Therefore the
9490 SUBREG case was removed from here.
9492 It is possible that distributing SUBREG over arithmetic operations
9493 leads to an intermediate result than can then be optimized further,
9494 e.g. by moving the outer SUBREG to the other side of a SET as done
9495 in simplify_set. This seems to have been the original intent of
9496 handling SUBREGs here.
9498 However, with current GCC this does not appear to actually happen,
9499 at least on major platforms. If some case is found where removing
9500 the SUBREG case here prevents follow-on optimizations, distributing
9501 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9503 default:
9504 return x;
9507 /* Set LHS and RHS to the inner operands (A and B in the example
9508 above) and set OTHER to the common operand (C in the example).
9509 There is only one way to do this unless the inner operation is
9510 commutative. */
9511 if (COMMUTATIVE_ARITH_P (lhs)
9512 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9513 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9514 else if (COMMUTATIVE_ARITH_P (lhs)
9515 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9516 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9517 else if (COMMUTATIVE_ARITH_P (lhs)
9518 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9519 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9520 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9521 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9522 else
9523 return x;
9525 /* Form the new inner operation, seeing if it simplifies first. */
9526 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9528 /* There is one exception to the general way of distributing:
9529 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9530 if (code == XOR && inner_code == IOR)
9532 inner_code = AND;
9533 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9536 /* We may be able to continuing distributing the result, so call
9537 ourselves recursively on the inner operation before forming the
9538 outer operation, which we return. */
9539 return simplify_gen_binary (inner_code, GET_MODE (x),
9540 apply_distributive_law (tem), other);
9543 /* See if X is of the form (* (+ A B) C), and if so convert to
9544 (+ (* A C) (* B C)) and try to simplify.
9546 Most of the time, this results in no change. However, if some of
9547 the operands are the same or inverses of each other, simplifications
9548 will result.
9550 For example, (and (ior A B) (not B)) can occur as the result of
9551 expanding a bit field assignment. When we apply the distributive
9552 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9553 which then simplifies to (and (A (not B))).
9555 Note that no checks happen on the validity of applying the inverse
9556 distributive law. This is pointless since we can do it in the
9557 few places where this routine is called.
9559 N is the index of the term that is decomposed (the arithmetic operation,
9560 i.e. (+ A B) in the first example above). !N is the index of the term that
9561 is distributed, i.e. of C in the first example above. */
9562 static rtx
9563 distribute_and_simplify_rtx (rtx x, int n)
9565 machine_mode mode;
9566 enum rtx_code outer_code, inner_code;
9567 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9569 /* Distributivity is not true for floating point as it can change the
9570 value. So we don't do it unless -funsafe-math-optimizations. */
9571 if (FLOAT_MODE_P (GET_MODE (x))
9572 && ! flag_unsafe_math_optimizations)
9573 return NULL_RTX;
9575 decomposed = XEXP (x, n);
9576 if (!ARITHMETIC_P (decomposed))
9577 return NULL_RTX;
9579 mode = GET_MODE (x);
9580 outer_code = GET_CODE (x);
9581 distributed = XEXP (x, !n);
9583 inner_code = GET_CODE (decomposed);
9584 inner_op0 = XEXP (decomposed, 0);
9585 inner_op1 = XEXP (decomposed, 1);
9587 /* Special case (and (xor B C) (not A)), which is equivalent to
9588 (xor (ior A B) (ior A C)) */
9589 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9591 distributed = XEXP (distributed, 0);
9592 outer_code = IOR;
9595 if (n == 0)
9597 /* Distribute the second term. */
9598 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9599 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9601 else
9603 /* Distribute the first term. */
9604 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9605 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9608 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9609 new_op0, new_op1));
9610 if (GET_CODE (tmp) != outer_code
9611 && (set_src_cost (tmp, optimize_this_for_speed_p)
9612 < set_src_cost (x, optimize_this_for_speed_p)))
9613 return tmp;
9615 return NULL_RTX;
9618 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9619 in MODE. Return an equivalent form, if different from (and VAROP
9620 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9622 static rtx
9623 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9624 unsigned HOST_WIDE_INT constop)
9626 unsigned HOST_WIDE_INT nonzero;
9627 unsigned HOST_WIDE_INT orig_constop;
9628 rtx orig_varop;
9629 int i;
9631 orig_varop = varop;
9632 orig_constop = constop;
9633 if (GET_CODE (varop) == CLOBBER)
9634 return NULL_RTX;
9636 /* Simplify VAROP knowing that we will be only looking at some of the
9637 bits in it.
9639 Note by passing in CONSTOP, we guarantee that the bits not set in
9640 CONSTOP are not significant and will never be examined. We must
9641 ensure that is the case by explicitly masking out those bits
9642 before returning. */
9643 varop = force_to_mode (varop, mode, constop, 0);
9645 /* If VAROP is a CLOBBER, we will fail so return it. */
9646 if (GET_CODE (varop) == CLOBBER)
9647 return varop;
9649 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9650 to VAROP and return the new constant. */
9651 if (CONST_INT_P (varop))
9652 return gen_int_mode (INTVAL (varop) & constop, mode);
9654 /* See what bits may be nonzero in VAROP. Unlike the general case of
9655 a call to nonzero_bits, here we don't care about bits outside
9656 MODE. */
9658 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9660 /* Turn off all bits in the constant that are known to already be zero.
9661 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9662 which is tested below. */
9664 constop &= nonzero;
9666 /* If we don't have any bits left, return zero. */
9667 if (constop == 0)
9668 return const0_rtx;
9670 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9671 a power of two, we can replace this with an ASHIFT. */
9672 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9673 && (i = exact_log2 (constop)) >= 0)
9674 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9676 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9677 or XOR, then try to apply the distributive law. This may eliminate
9678 operations if either branch can be simplified because of the AND.
9679 It may also make some cases more complex, but those cases probably
9680 won't match a pattern either with or without this. */
9682 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9683 return
9684 gen_lowpart
9685 (mode,
9686 apply_distributive_law
9687 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9688 simplify_and_const_int (NULL_RTX,
9689 GET_MODE (varop),
9690 XEXP (varop, 0),
9691 constop),
9692 simplify_and_const_int (NULL_RTX,
9693 GET_MODE (varop),
9694 XEXP (varop, 1),
9695 constop))));
9697 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9698 the AND and see if one of the operands simplifies to zero. If so, we
9699 may eliminate it. */
9701 if (GET_CODE (varop) == PLUS
9702 && exact_log2 (constop + 1) >= 0)
9704 rtx o0, o1;
9706 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9707 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9708 if (o0 == const0_rtx)
9709 return o1;
9710 if (o1 == const0_rtx)
9711 return o0;
9714 /* Make a SUBREG if necessary. If we can't make it, fail. */
9715 varop = gen_lowpart (mode, varop);
9716 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9717 return NULL_RTX;
9719 /* If we are only masking insignificant bits, return VAROP. */
9720 if (constop == nonzero)
9721 return varop;
9723 if (varop == orig_varop && constop == orig_constop)
9724 return NULL_RTX;
9726 /* Otherwise, return an AND. */
9727 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9731 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9732 in MODE.
9734 Return an equivalent form, if different from X. Otherwise, return X. If
9735 X is zero, we are to always construct the equivalent form. */
9737 static rtx
9738 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9739 unsigned HOST_WIDE_INT constop)
9741 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9742 if (tem)
9743 return tem;
9745 if (!x)
9746 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9747 gen_int_mode (constop, mode));
9748 if (GET_MODE (x) != mode)
9749 x = gen_lowpart (mode, x);
9750 return x;
9753 /* Given a REG, X, compute which bits in X can be nonzero.
9754 We don't care about bits outside of those defined in MODE.
9756 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9757 a shift, AND, or zero_extract, we can do better. */
9759 static rtx
9760 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9761 const_rtx known_x ATTRIBUTE_UNUSED,
9762 machine_mode known_mode ATTRIBUTE_UNUSED,
9763 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9764 unsigned HOST_WIDE_INT *nonzero)
9766 rtx tem;
9767 reg_stat_type *rsp;
9769 /* If X is a register whose nonzero bits value is current, use it.
9770 Otherwise, if X is a register whose value we can find, use that
9771 value. Otherwise, use the previously-computed global nonzero bits
9772 for this register. */
9774 rsp = &reg_stat[REGNO (x)];
9775 if (rsp->last_set_value != 0
9776 && (rsp->last_set_mode == mode
9777 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9778 && GET_MODE_CLASS (mode) == MODE_INT))
9779 && ((rsp->last_set_label >= label_tick_ebb_start
9780 && rsp->last_set_label < label_tick)
9781 || (rsp->last_set_label == label_tick
9782 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9783 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9784 && REGNO (x) < reg_n_sets_max
9785 && REG_N_SETS (REGNO (x)) == 1
9786 && !REGNO_REG_SET_P
9787 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9788 REGNO (x)))))
9790 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9792 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9793 /* We don't know anything about the upper bits. */
9794 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9796 *nonzero &= mask;
9797 return NULL;
9800 tem = get_last_value (x);
9802 if (tem)
9804 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9805 /* If X is narrower than MODE and TEM is a non-negative
9806 constant that would appear negative in the mode of X,
9807 sign-extend it for use in reg_nonzero_bits because some
9808 machines (maybe most) will actually do the sign-extension
9809 and this is the conservative approach.
9811 ??? For 2.5, try to tighten up the MD files in this regard
9812 instead of this kludge. */
9814 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9815 && CONST_INT_P (tem)
9816 && INTVAL (tem) > 0
9817 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9818 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9819 #endif
9820 return tem;
9822 else if (nonzero_sign_valid && rsp->nonzero_bits)
9824 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9826 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9827 /* We don't know anything about the upper bits. */
9828 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9830 *nonzero &= mask;
9833 return NULL;
9836 /* Return the number of bits at the high-order end of X that are known to
9837 be equal to the sign bit. X will be used in mode MODE; if MODE is
9838 VOIDmode, X will be used in its own mode. The returned value will always
9839 be between 1 and the number of bits in MODE. */
9841 static rtx
9842 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9843 const_rtx known_x ATTRIBUTE_UNUSED,
9844 machine_mode known_mode
9845 ATTRIBUTE_UNUSED,
9846 unsigned int known_ret ATTRIBUTE_UNUSED,
9847 unsigned int *result)
9849 rtx tem;
9850 reg_stat_type *rsp;
9852 rsp = &reg_stat[REGNO (x)];
9853 if (rsp->last_set_value != 0
9854 && rsp->last_set_mode == mode
9855 && ((rsp->last_set_label >= label_tick_ebb_start
9856 && rsp->last_set_label < label_tick)
9857 || (rsp->last_set_label == label_tick
9858 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9859 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9860 && REGNO (x) < reg_n_sets_max
9861 && REG_N_SETS (REGNO (x)) == 1
9862 && !REGNO_REG_SET_P
9863 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9864 REGNO (x)))))
9866 *result = rsp->last_set_sign_bit_copies;
9867 return NULL;
9870 tem = get_last_value (x);
9871 if (tem != 0)
9872 return tem;
9874 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9875 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9876 *result = rsp->sign_bit_copies;
9878 return NULL;
9881 /* Return the number of "extended" bits there are in X, when interpreted
9882 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9883 unsigned quantities, this is the number of high-order zero bits.
9884 For signed quantities, this is the number of copies of the sign bit
9885 minus 1. In both case, this function returns the number of "spare"
9886 bits. For example, if two quantities for which this function returns
9887 at least 1 are added, the addition is known not to overflow.
9889 This function will always return 0 unless called during combine, which
9890 implies that it must be called from a define_split. */
9892 unsigned int
9893 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9895 if (nonzero_sign_valid == 0)
9896 return 0;
9898 return (unsignedp
9899 ? (HWI_COMPUTABLE_MODE_P (mode)
9900 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9901 - floor_log2 (nonzero_bits (x, mode)))
9902 : 0)
9903 : num_sign_bit_copies (x, mode) - 1);
9906 /* This function is called from `simplify_shift_const' to merge two
9907 outer operations. Specifically, we have already found that we need
9908 to perform operation *POP0 with constant *PCONST0 at the outermost
9909 position. We would now like to also perform OP1 with constant CONST1
9910 (with *POP0 being done last).
9912 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9913 the resulting operation. *PCOMP_P is set to 1 if we would need to
9914 complement the innermost operand, otherwise it is unchanged.
9916 MODE is the mode in which the operation will be done. No bits outside
9917 the width of this mode matter. It is assumed that the width of this mode
9918 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9920 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9921 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9922 result is simply *PCONST0.
9924 If the resulting operation cannot be expressed as one operation, we
9925 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9927 static int
9928 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)
9930 enum rtx_code op0 = *pop0;
9931 HOST_WIDE_INT const0 = *pconst0;
9933 const0 &= GET_MODE_MASK (mode);
9934 const1 &= GET_MODE_MASK (mode);
9936 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9937 if (op0 == AND)
9938 const1 &= const0;
9940 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9941 if OP0 is SET. */
9943 if (op1 == UNKNOWN || op0 == SET)
9944 return 1;
9946 else if (op0 == UNKNOWN)
9947 op0 = op1, const0 = const1;
9949 else if (op0 == op1)
9951 switch (op0)
9953 case AND:
9954 const0 &= const1;
9955 break;
9956 case IOR:
9957 const0 |= const1;
9958 break;
9959 case XOR:
9960 const0 ^= const1;
9961 break;
9962 case PLUS:
9963 const0 += const1;
9964 break;
9965 case NEG:
9966 op0 = UNKNOWN;
9967 break;
9968 default:
9969 break;
9973 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9974 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9975 return 0;
9977 /* If the two constants aren't the same, we can't do anything. The
9978 remaining six cases can all be done. */
9979 else if (const0 != const1)
9980 return 0;
9982 else
9983 switch (op0)
9985 case IOR:
9986 if (op1 == AND)
9987 /* (a & b) | b == b */
9988 op0 = SET;
9989 else /* op1 == XOR */
9990 /* (a ^ b) | b == a | b */
9992 break;
9994 case XOR:
9995 if (op1 == AND)
9996 /* (a & b) ^ b == (~a) & b */
9997 op0 = AND, *pcomp_p = 1;
9998 else /* op1 == IOR */
9999 /* (a | b) ^ b == a & ~b */
10000 op0 = AND, const0 = ~const0;
10001 break;
10003 case AND:
10004 if (op1 == IOR)
10005 /* (a | b) & b == b */
10006 op0 = SET;
10007 else /* op1 == XOR */
10008 /* (a ^ b) & b) == (~a) & b */
10009 *pcomp_p = 1;
10010 break;
10011 default:
10012 break;
10015 /* Check for NO-OP cases. */
10016 const0 &= GET_MODE_MASK (mode);
10017 if (const0 == 0
10018 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10019 op0 = UNKNOWN;
10020 else if (const0 == 0 && op0 == AND)
10021 op0 = SET;
10022 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10023 && op0 == AND)
10024 op0 = UNKNOWN;
10026 *pop0 = op0;
10028 /* ??? Slightly redundant with the above mask, but not entirely.
10029 Moving this above means we'd have to sign-extend the mode mask
10030 for the final test. */
10031 if (op0 != UNKNOWN && op0 != NEG)
10032 *pconst0 = trunc_int_for_mode (const0, mode);
10034 return 1;
10037 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10038 the shift in. The original shift operation CODE is performed on OP in
10039 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10040 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10041 result of the shift is subject to operation OUTER_CODE with operand
10042 OUTER_CONST. */
10044 static machine_mode
10045 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10046 machine_mode orig_mode, machine_mode mode,
10047 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10049 if (orig_mode == mode)
10050 return mode;
10051 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10053 /* In general we can't perform in wider mode for right shift and rotate. */
10054 switch (code)
10056 case ASHIFTRT:
10057 /* We can still widen if the bits brought in from the left are identical
10058 to the sign bit of ORIG_MODE. */
10059 if (num_sign_bit_copies (op, mode)
10060 > (unsigned) (GET_MODE_PRECISION (mode)
10061 - GET_MODE_PRECISION (orig_mode)))
10062 return mode;
10063 return orig_mode;
10065 case LSHIFTRT:
10066 /* Similarly here but with zero bits. */
10067 if (HWI_COMPUTABLE_MODE_P (mode)
10068 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10069 return mode;
10071 /* We can also widen if the bits brought in will be masked off. This
10072 operation is performed in ORIG_MODE. */
10073 if (outer_code == AND)
10075 int care_bits = low_bitmask_len (orig_mode, outer_const);
10077 if (care_bits >= 0
10078 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10079 return mode;
10081 /* fall through */
10083 case ROTATE:
10084 return orig_mode;
10086 case ROTATERT:
10087 gcc_unreachable ();
10089 default:
10090 return mode;
10094 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10095 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10096 if we cannot simplify it. Otherwise, return a simplified value.
10098 The shift is normally computed in the widest mode we find in VAROP, as
10099 long as it isn't a different number of words than RESULT_MODE. Exceptions
10100 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10102 static rtx
10103 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10104 rtx varop, int orig_count)
10106 enum rtx_code orig_code = code;
10107 rtx orig_varop = varop;
10108 int count;
10109 machine_mode mode = result_mode;
10110 machine_mode shift_mode, tmode;
10111 unsigned int mode_words
10112 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10113 /* We form (outer_op (code varop count) (outer_const)). */
10114 enum rtx_code outer_op = UNKNOWN;
10115 HOST_WIDE_INT outer_const = 0;
10116 int complement_p = 0;
10117 rtx new_rtx, x;
10119 /* Make sure and truncate the "natural" shift on the way in. We don't
10120 want to do this inside the loop as it makes it more difficult to
10121 combine shifts. */
10122 if (SHIFT_COUNT_TRUNCATED)
10123 orig_count &= GET_MODE_BITSIZE (mode) - 1;
10125 /* If we were given an invalid count, don't do anything except exactly
10126 what was requested. */
10128 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
10129 return NULL_RTX;
10131 count = orig_count;
10133 /* Unless one of the branches of the `if' in this loop does a `continue',
10134 we will `break' the loop after the `if'. */
10136 while (count != 0)
10138 /* If we have an operand of (clobber (const_int 0)), fail. */
10139 if (GET_CODE (varop) == CLOBBER)
10140 return NULL_RTX;
10142 /* Convert ROTATERT to ROTATE. */
10143 if (code == ROTATERT)
10145 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
10146 code = ROTATE;
10147 if (VECTOR_MODE_P (result_mode))
10148 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10149 else
10150 count = bitsize - count;
10153 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10154 mode, outer_op, outer_const);
10156 /* Handle cases where the count is greater than the size of the mode
10157 minus 1. For ASHIFT, use the size minus one as the count (this can
10158 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10159 take the count modulo the size. For other shifts, the result is
10160 zero.
10162 Since these shifts are being produced by the compiler by combining
10163 multiple operations, each of which are defined, we know what the
10164 result is supposed to be. */
10166 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10168 if (code == ASHIFTRT)
10169 count = GET_MODE_PRECISION (shift_mode) - 1;
10170 else if (code == ROTATE || code == ROTATERT)
10171 count %= GET_MODE_PRECISION (shift_mode);
10172 else
10174 /* We can't simply return zero because there may be an
10175 outer op. */
10176 varop = const0_rtx;
10177 count = 0;
10178 break;
10182 /* If we discovered we had to complement VAROP, leave. Making a NOT
10183 here would cause an infinite loop. */
10184 if (complement_p)
10185 break;
10187 /* An arithmetic right shift of a quantity known to be -1 or 0
10188 is a no-op. */
10189 if (code == ASHIFTRT
10190 && (num_sign_bit_copies (varop, shift_mode)
10191 == GET_MODE_PRECISION (shift_mode)))
10193 count = 0;
10194 break;
10197 /* If we are doing an arithmetic right shift and discarding all but
10198 the sign bit copies, this is equivalent to doing a shift by the
10199 bitsize minus one. Convert it into that shift because it will often
10200 allow other simplifications. */
10202 if (code == ASHIFTRT
10203 && (count + num_sign_bit_copies (varop, shift_mode)
10204 >= GET_MODE_PRECISION (shift_mode)))
10205 count = GET_MODE_PRECISION (shift_mode) - 1;
10207 /* We simplify the tests below and elsewhere by converting
10208 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10209 `make_compound_operation' will convert it to an ASHIFTRT for
10210 those machines (such as VAX) that don't have an LSHIFTRT. */
10211 if (code == ASHIFTRT
10212 && val_signbit_known_clear_p (shift_mode,
10213 nonzero_bits (varop, shift_mode)))
10214 code = LSHIFTRT;
10216 if (((code == LSHIFTRT
10217 && HWI_COMPUTABLE_MODE_P (shift_mode)
10218 && !(nonzero_bits (varop, shift_mode) >> count))
10219 || (code == ASHIFT
10220 && HWI_COMPUTABLE_MODE_P (shift_mode)
10221 && !((nonzero_bits (varop, shift_mode) << count)
10222 & GET_MODE_MASK (shift_mode))))
10223 && !side_effects_p (varop))
10224 varop = const0_rtx;
10226 switch (GET_CODE (varop))
10228 case SIGN_EXTEND:
10229 case ZERO_EXTEND:
10230 case SIGN_EXTRACT:
10231 case ZERO_EXTRACT:
10232 new_rtx = expand_compound_operation (varop);
10233 if (new_rtx != varop)
10235 varop = new_rtx;
10236 continue;
10238 break;
10240 case MEM:
10241 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10242 minus the width of a smaller mode, we can do this with a
10243 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10244 if ((code == ASHIFTRT || code == LSHIFTRT)
10245 && ! mode_dependent_address_p (XEXP (varop, 0),
10246 MEM_ADDR_SPACE (varop))
10247 && ! MEM_VOLATILE_P (varop)
10248 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10249 MODE_INT, 1)) != BLKmode)
10251 new_rtx = adjust_address_nv (varop, tmode,
10252 BYTES_BIG_ENDIAN ? 0
10253 : count / BITS_PER_UNIT);
10255 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10256 : ZERO_EXTEND, mode, new_rtx);
10257 count = 0;
10258 continue;
10260 break;
10262 case SUBREG:
10263 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10264 the same number of words as what we've seen so far. Then store
10265 the widest mode in MODE. */
10266 if (subreg_lowpart_p (varop)
10267 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10268 > GET_MODE_SIZE (GET_MODE (varop)))
10269 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10270 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10271 == mode_words
10272 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10273 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10275 varop = SUBREG_REG (varop);
10276 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10277 mode = GET_MODE (varop);
10278 continue;
10280 break;
10282 case MULT:
10283 /* Some machines use MULT instead of ASHIFT because MULT
10284 is cheaper. But it is still better on those machines to
10285 merge two shifts into one. */
10286 if (CONST_INT_P (XEXP (varop, 1))
10287 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10289 varop
10290 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10291 XEXP (varop, 0),
10292 GEN_INT (exact_log2 (
10293 UINTVAL (XEXP (varop, 1)))));
10294 continue;
10296 break;
10298 case UDIV:
10299 /* Similar, for when divides are cheaper. */
10300 if (CONST_INT_P (XEXP (varop, 1))
10301 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10303 varop
10304 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10305 XEXP (varop, 0),
10306 GEN_INT (exact_log2 (
10307 UINTVAL (XEXP (varop, 1)))));
10308 continue;
10310 break;
10312 case ASHIFTRT:
10313 /* If we are extracting just the sign bit of an arithmetic
10314 right shift, that shift is not needed. However, the sign
10315 bit of a wider mode may be different from what would be
10316 interpreted as the sign bit in a narrower mode, so, if
10317 the result is narrower, don't discard the shift. */
10318 if (code == LSHIFTRT
10319 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10320 && (GET_MODE_BITSIZE (result_mode)
10321 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10323 varop = XEXP (varop, 0);
10324 continue;
10327 /* ... fall through ... */
10329 case LSHIFTRT:
10330 case ASHIFT:
10331 case ROTATE:
10332 /* Here we have two nested shifts. The result is usually the
10333 AND of a new shift with a mask. We compute the result below. */
10334 if (CONST_INT_P (XEXP (varop, 1))
10335 && INTVAL (XEXP (varop, 1)) >= 0
10336 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10337 && HWI_COMPUTABLE_MODE_P (result_mode)
10338 && HWI_COMPUTABLE_MODE_P (mode)
10339 && !VECTOR_MODE_P (result_mode))
10341 enum rtx_code first_code = GET_CODE (varop);
10342 unsigned int first_count = INTVAL (XEXP (varop, 1));
10343 unsigned HOST_WIDE_INT mask;
10344 rtx mask_rtx;
10346 /* We have one common special case. We can't do any merging if
10347 the inner code is an ASHIFTRT of a smaller mode. However, if
10348 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10349 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10350 we can convert it to
10351 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10352 This simplifies certain SIGN_EXTEND operations. */
10353 if (code == ASHIFT && first_code == ASHIFTRT
10354 && count == (GET_MODE_PRECISION (result_mode)
10355 - GET_MODE_PRECISION (GET_MODE (varop))))
10357 /* C3 has the low-order C1 bits zero. */
10359 mask = GET_MODE_MASK (mode)
10360 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10362 varop = simplify_and_const_int (NULL_RTX, result_mode,
10363 XEXP (varop, 0), mask);
10364 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10365 varop, count);
10366 count = first_count;
10367 code = ASHIFTRT;
10368 continue;
10371 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10372 than C1 high-order bits equal to the sign bit, we can convert
10373 this to either an ASHIFT or an ASHIFTRT depending on the
10374 two counts.
10376 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10378 if (code == ASHIFTRT && first_code == ASHIFT
10379 && GET_MODE (varop) == shift_mode
10380 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10381 > first_count))
10383 varop = XEXP (varop, 0);
10384 count -= first_count;
10385 if (count < 0)
10387 count = -count;
10388 code = ASHIFT;
10391 continue;
10394 /* There are some cases we can't do. If CODE is ASHIFTRT,
10395 we can only do this if FIRST_CODE is also ASHIFTRT.
10397 We can't do the case when CODE is ROTATE and FIRST_CODE is
10398 ASHIFTRT.
10400 If the mode of this shift is not the mode of the outer shift,
10401 we can't do this if either shift is a right shift or ROTATE.
10403 Finally, we can't do any of these if the mode is too wide
10404 unless the codes are the same.
10406 Handle the case where the shift codes are the same
10407 first. */
10409 if (code == first_code)
10411 if (GET_MODE (varop) != result_mode
10412 && (code == ASHIFTRT || code == LSHIFTRT
10413 || code == ROTATE))
10414 break;
10416 count += first_count;
10417 varop = XEXP (varop, 0);
10418 continue;
10421 if (code == ASHIFTRT
10422 || (code == ROTATE && first_code == ASHIFTRT)
10423 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10424 || (GET_MODE (varop) != result_mode
10425 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10426 || first_code == ROTATE
10427 || code == ROTATE)))
10428 break;
10430 /* To compute the mask to apply after the shift, shift the
10431 nonzero bits of the inner shift the same way the
10432 outer shift will. */
10434 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10435 result_mode);
10437 mask_rtx
10438 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10439 GEN_INT (count));
10441 /* Give up if we can't compute an outer operation to use. */
10442 if (mask_rtx == 0
10443 || !CONST_INT_P (mask_rtx)
10444 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10445 INTVAL (mask_rtx),
10446 result_mode, &complement_p))
10447 break;
10449 /* If the shifts are in the same direction, we add the
10450 counts. Otherwise, we subtract them. */
10451 if ((code == ASHIFTRT || code == LSHIFTRT)
10452 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10453 count += first_count;
10454 else
10455 count -= first_count;
10457 /* If COUNT is positive, the new shift is usually CODE,
10458 except for the two exceptions below, in which case it is
10459 FIRST_CODE. If the count is negative, FIRST_CODE should
10460 always be used */
10461 if (count > 0
10462 && ((first_code == ROTATE && code == ASHIFT)
10463 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10464 code = first_code;
10465 else if (count < 0)
10466 code = first_code, count = -count;
10468 varop = XEXP (varop, 0);
10469 continue;
10472 /* If we have (A << B << C) for any shift, we can convert this to
10473 (A << C << B). This wins if A is a constant. Only try this if
10474 B is not a constant. */
10476 else if (GET_CODE (varop) == code
10477 && CONST_INT_P (XEXP (varop, 0))
10478 && !CONST_INT_P (XEXP (varop, 1)))
10480 rtx new_rtx = simplify_const_binary_operation (code, mode,
10481 XEXP (varop, 0),
10482 GEN_INT (count));
10483 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10484 count = 0;
10485 continue;
10487 break;
10489 case NOT:
10490 if (VECTOR_MODE_P (mode))
10491 break;
10493 /* Make this fit the case below. */
10494 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10495 continue;
10497 case IOR:
10498 case AND:
10499 case XOR:
10500 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10501 with C the size of VAROP - 1 and the shift is logical if
10502 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10503 we have an (le X 0) operation. If we have an arithmetic shift
10504 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10505 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10507 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10508 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10509 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10510 && (code == LSHIFTRT || code == ASHIFTRT)
10511 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10512 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10514 count = 0;
10515 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10516 const0_rtx);
10518 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10519 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10521 continue;
10524 /* If we have (shift (logical)), move the logical to the outside
10525 to allow it to possibly combine with another logical and the
10526 shift to combine with another shift. This also canonicalizes to
10527 what a ZERO_EXTRACT looks like. Also, some machines have
10528 (and (shift)) insns. */
10530 if (CONST_INT_P (XEXP (varop, 1))
10531 /* We can't do this if we have (ashiftrt (xor)) and the
10532 constant has its sign bit set in shift_mode with shift_mode
10533 wider than result_mode. */
10534 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10535 && result_mode != shift_mode
10536 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10537 shift_mode))
10538 && (new_rtx = simplify_const_binary_operation
10539 (code, result_mode,
10540 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10541 GEN_INT (count))) != 0
10542 && CONST_INT_P (new_rtx)
10543 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10544 INTVAL (new_rtx), result_mode, &complement_p))
10546 varop = XEXP (varop, 0);
10547 continue;
10550 /* If we can't do that, try to simplify the shift in each arm of the
10551 logical expression, make a new logical expression, and apply
10552 the inverse distributive law. This also can't be done for
10553 (ashiftrt (xor)) where we've widened the shift and the constant
10554 changes the sign bit. */
10555 if (CONST_INT_P (XEXP (varop, 1))
10556 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10557 && result_mode != shift_mode
10558 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10559 shift_mode)))
10561 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10562 XEXP (varop, 0), count);
10563 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10564 XEXP (varop, 1), count);
10566 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10567 lhs, rhs);
10568 varop = apply_distributive_law (varop);
10570 count = 0;
10571 continue;
10573 break;
10575 case EQ:
10576 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10577 says that the sign bit can be tested, FOO has mode MODE, C is
10578 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10579 that may be nonzero. */
10580 if (code == LSHIFTRT
10581 && XEXP (varop, 1) == const0_rtx
10582 && GET_MODE (XEXP (varop, 0)) == result_mode
10583 && count == (GET_MODE_PRECISION (result_mode) - 1)
10584 && HWI_COMPUTABLE_MODE_P (result_mode)
10585 && STORE_FLAG_VALUE == -1
10586 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10587 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10588 &complement_p))
10590 varop = XEXP (varop, 0);
10591 count = 0;
10592 continue;
10594 break;
10596 case NEG:
10597 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10598 than the number of bits in the mode is equivalent to A. */
10599 if (code == LSHIFTRT
10600 && count == (GET_MODE_PRECISION (result_mode) - 1)
10601 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10603 varop = XEXP (varop, 0);
10604 count = 0;
10605 continue;
10608 /* NEG commutes with ASHIFT since it is multiplication. Move the
10609 NEG outside to allow shifts to combine. */
10610 if (code == ASHIFT
10611 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10612 &complement_p))
10614 varop = XEXP (varop, 0);
10615 continue;
10617 break;
10619 case PLUS:
10620 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10621 is one less than the number of bits in the mode is
10622 equivalent to (xor A 1). */
10623 if (code == LSHIFTRT
10624 && count == (GET_MODE_PRECISION (result_mode) - 1)
10625 && XEXP (varop, 1) == constm1_rtx
10626 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10627 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10628 &complement_p))
10630 count = 0;
10631 varop = XEXP (varop, 0);
10632 continue;
10635 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10636 that might be nonzero in BAR are those being shifted out and those
10637 bits are known zero in FOO, we can replace the PLUS with FOO.
10638 Similarly in the other operand order. This code occurs when
10639 we are computing the size of a variable-size array. */
10641 if ((code == ASHIFTRT || code == LSHIFTRT)
10642 && count < HOST_BITS_PER_WIDE_INT
10643 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10644 && (nonzero_bits (XEXP (varop, 1), result_mode)
10645 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10647 varop = XEXP (varop, 0);
10648 continue;
10650 else if ((code == ASHIFTRT || code == LSHIFTRT)
10651 && count < HOST_BITS_PER_WIDE_INT
10652 && HWI_COMPUTABLE_MODE_P (result_mode)
10653 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10654 >> count)
10655 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10656 & nonzero_bits (XEXP (varop, 1),
10657 result_mode)))
10659 varop = XEXP (varop, 1);
10660 continue;
10663 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10664 if (code == ASHIFT
10665 && CONST_INT_P (XEXP (varop, 1))
10666 && (new_rtx = simplify_const_binary_operation
10667 (ASHIFT, result_mode,
10668 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10669 GEN_INT (count))) != 0
10670 && CONST_INT_P (new_rtx)
10671 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10672 INTVAL (new_rtx), result_mode, &complement_p))
10674 varop = XEXP (varop, 0);
10675 continue;
10678 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10679 signbit', and attempt to change the PLUS to an XOR and move it to
10680 the outer operation as is done above in the AND/IOR/XOR case
10681 leg for shift(logical). See details in logical handling above
10682 for reasoning in doing so. */
10683 if (code == LSHIFTRT
10684 && CONST_INT_P (XEXP (varop, 1))
10685 && mode_signbit_p (result_mode, XEXP (varop, 1))
10686 && (new_rtx = simplify_const_binary_operation
10687 (code, result_mode,
10688 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10689 GEN_INT (count))) != 0
10690 && CONST_INT_P (new_rtx)
10691 && merge_outer_ops (&outer_op, &outer_const, XOR,
10692 INTVAL (new_rtx), result_mode, &complement_p))
10694 varop = XEXP (varop, 0);
10695 continue;
10698 break;
10700 case MINUS:
10701 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10702 with C the size of VAROP - 1 and the shift is logical if
10703 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10704 we have a (gt X 0) operation. If the shift is arithmetic with
10705 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10706 we have a (neg (gt X 0)) operation. */
10708 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10709 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10710 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10711 && (code == LSHIFTRT || code == ASHIFTRT)
10712 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10713 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10714 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10716 count = 0;
10717 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10718 const0_rtx);
10720 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10721 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10723 continue;
10725 break;
10727 case TRUNCATE:
10728 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10729 if the truncate does not affect the value. */
10730 if (code == LSHIFTRT
10731 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10732 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10733 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10734 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10735 - GET_MODE_PRECISION (GET_MODE (varop)))))
10737 rtx varop_inner = XEXP (varop, 0);
10739 varop_inner
10740 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10741 XEXP (varop_inner, 0),
10742 GEN_INT
10743 (count + INTVAL (XEXP (varop_inner, 1))));
10744 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10745 count = 0;
10746 continue;
10748 break;
10750 default:
10751 break;
10754 break;
10757 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10758 outer_op, outer_const);
10760 /* We have now finished analyzing the shift. The result should be
10761 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10762 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10763 to the result of the shift. OUTER_CONST is the relevant constant,
10764 but we must turn off all bits turned off in the shift. */
10766 if (outer_op == UNKNOWN
10767 && orig_code == code && orig_count == count
10768 && varop == orig_varop
10769 && shift_mode == GET_MODE (varop))
10770 return NULL_RTX;
10772 /* Make a SUBREG if necessary. If we can't make it, fail. */
10773 varop = gen_lowpart (shift_mode, varop);
10774 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10775 return NULL_RTX;
10777 /* If we have an outer operation and we just made a shift, it is
10778 possible that we could have simplified the shift were it not
10779 for the outer operation. So try to do the simplification
10780 recursively. */
10782 if (outer_op != UNKNOWN)
10783 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10784 else
10785 x = NULL_RTX;
10787 if (x == NULL_RTX)
10788 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10790 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10791 turn off all the bits that the shift would have turned off. */
10792 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10793 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10794 GET_MODE_MASK (result_mode) >> orig_count);
10796 /* Do the remainder of the processing in RESULT_MODE. */
10797 x = gen_lowpart_or_truncate (result_mode, x);
10799 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10800 operation. */
10801 if (complement_p)
10802 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10804 if (outer_op != UNKNOWN)
10806 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10807 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10808 outer_const = trunc_int_for_mode (outer_const, result_mode);
10810 if (outer_op == AND)
10811 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10812 else if (outer_op == SET)
10814 /* This means that we have determined that the result is
10815 equivalent to a constant. This should be rare. */
10816 if (!side_effects_p (x))
10817 x = GEN_INT (outer_const);
10819 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10820 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10821 else
10822 x = simplify_gen_binary (outer_op, result_mode, x,
10823 GEN_INT (outer_const));
10826 return x;
10829 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10830 The result of the shift is RESULT_MODE. If we cannot simplify it,
10831 return X or, if it is NULL, synthesize the expression with
10832 simplify_gen_binary. Otherwise, return a simplified value.
10834 The shift is normally computed in the widest mode we find in VAROP, as
10835 long as it isn't a different number of words than RESULT_MODE. Exceptions
10836 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10838 static rtx
10839 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10840 rtx varop, int count)
10842 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10843 if (tem)
10844 return tem;
10846 if (!x)
10847 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10848 if (GET_MODE (x) != result_mode)
10849 x = gen_lowpart (result_mode, x);
10850 return x;
10854 /* Like recog, but we receive the address of a pointer to a new pattern.
10855 We try to match the rtx that the pointer points to.
10856 If that fails, we may try to modify or replace the pattern,
10857 storing the replacement into the same pointer object.
10859 Modifications include deletion or addition of CLOBBERs.
10861 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10862 the CLOBBERs are placed.
10864 The value is the final insn code from the pattern ultimately matched,
10865 or -1. */
10867 static int
10868 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10870 rtx pat = *pnewpat;
10871 rtx pat_without_clobbers;
10872 int insn_code_number;
10873 int num_clobbers_to_add = 0;
10874 int i;
10875 rtx notes = NULL_RTX;
10876 rtx old_notes, old_pat;
10877 int old_icode;
10879 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10880 we use to indicate that something didn't match. If we find such a
10881 thing, force rejection. */
10882 if (GET_CODE (pat) == PARALLEL)
10883 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10884 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10885 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10886 return -1;
10888 old_pat = PATTERN (insn);
10889 old_notes = REG_NOTES (insn);
10890 PATTERN (insn) = pat;
10891 REG_NOTES (insn) = NULL_RTX;
10893 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10894 if (dump_file && (dump_flags & TDF_DETAILS))
10896 if (insn_code_number < 0)
10897 fputs ("Failed to match this instruction:\n", dump_file);
10898 else
10899 fputs ("Successfully matched this instruction:\n", dump_file);
10900 print_rtl_single (dump_file, pat);
10903 /* If it isn't, there is the possibility that we previously had an insn
10904 that clobbered some register as a side effect, but the combined
10905 insn doesn't need to do that. So try once more without the clobbers
10906 unless this represents an ASM insn. */
10908 if (insn_code_number < 0 && ! check_asm_operands (pat)
10909 && GET_CODE (pat) == PARALLEL)
10911 int pos;
10913 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10914 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10916 if (i != pos)
10917 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10918 pos++;
10921 SUBST_INT (XVECLEN (pat, 0), pos);
10923 if (pos == 1)
10924 pat = XVECEXP (pat, 0, 0);
10926 PATTERN (insn) = pat;
10927 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10928 if (dump_file && (dump_flags & TDF_DETAILS))
10930 if (insn_code_number < 0)
10931 fputs ("Failed to match this instruction:\n", dump_file);
10932 else
10933 fputs ("Successfully matched this instruction:\n", dump_file);
10934 print_rtl_single (dump_file, pat);
10938 pat_without_clobbers = pat;
10940 PATTERN (insn) = old_pat;
10941 REG_NOTES (insn) = old_notes;
10943 /* Recognize all noop sets, these will be killed by followup pass. */
10944 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10945 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10947 /* If we had any clobbers to add, make a new pattern than contains
10948 them. Then check to make sure that all of them are dead. */
10949 if (num_clobbers_to_add)
10951 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10952 rtvec_alloc (GET_CODE (pat) == PARALLEL
10953 ? (XVECLEN (pat, 0)
10954 + num_clobbers_to_add)
10955 : num_clobbers_to_add + 1));
10957 if (GET_CODE (pat) == PARALLEL)
10958 for (i = 0; i < XVECLEN (pat, 0); i++)
10959 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10960 else
10961 XVECEXP (newpat, 0, 0) = pat;
10963 add_clobbers (newpat, insn_code_number);
10965 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10966 i < XVECLEN (newpat, 0); i++)
10968 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10969 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10970 return -1;
10971 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10973 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10974 notes = alloc_reg_note (REG_UNUSED,
10975 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10978 pat = newpat;
10981 if (insn_code_number >= 0
10982 && insn_code_number != NOOP_MOVE_INSN_CODE)
10984 old_pat = PATTERN (insn);
10985 old_notes = REG_NOTES (insn);
10986 old_icode = INSN_CODE (insn);
10987 PATTERN (insn) = pat;
10988 REG_NOTES (insn) = notes;
10990 /* Allow targets to reject combined insn. */
10991 if (!targetm.legitimate_combined_insn (insn))
10993 if (dump_file && (dump_flags & TDF_DETAILS))
10994 fputs ("Instruction not appropriate for target.",
10995 dump_file);
10997 /* Callers expect recog_for_combine to strip
10998 clobbers from the pattern on failure. */
10999 pat = pat_without_clobbers;
11000 notes = NULL_RTX;
11002 insn_code_number = -1;
11005 PATTERN (insn) = old_pat;
11006 REG_NOTES (insn) = old_notes;
11007 INSN_CODE (insn) = old_icode;
11010 *pnewpat = pat;
11011 *pnotes = notes;
11013 return insn_code_number;
11016 /* Like gen_lowpart_general but for use by combine. In combine it
11017 is not possible to create any new pseudoregs. However, it is
11018 safe to create invalid memory addresses, because combine will
11019 try to recognize them and all they will do is make the combine
11020 attempt fail.
11022 If for some reason this cannot do its job, an rtx
11023 (clobber (const_int 0)) is returned.
11024 An insn containing that will not be recognized. */
11026 static rtx
11027 gen_lowpart_for_combine (machine_mode omode, rtx x)
11029 machine_mode imode = GET_MODE (x);
11030 unsigned int osize = GET_MODE_SIZE (omode);
11031 unsigned int isize = GET_MODE_SIZE (imode);
11032 rtx result;
11034 if (omode == imode)
11035 return x;
11037 /* We can only support MODE being wider than a word if X is a
11038 constant integer or has a mode the same size. */
11039 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11040 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11041 goto fail;
11043 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11044 won't know what to do. So we will strip off the SUBREG here and
11045 process normally. */
11046 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11048 x = SUBREG_REG (x);
11050 /* For use in case we fall down into the address adjustments
11051 further below, we need to adjust the known mode and size of
11052 x; imode and isize, since we just adjusted x. */
11053 imode = GET_MODE (x);
11055 if (imode == omode)
11056 return x;
11058 isize = GET_MODE_SIZE (imode);
11061 result = gen_lowpart_common (omode, x);
11063 if (result)
11064 return result;
11066 if (MEM_P (x))
11068 int offset = 0;
11070 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11071 address. */
11072 if (MEM_VOLATILE_P (x)
11073 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11074 goto fail;
11076 /* If we want to refer to something bigger than the original memref,
11077 generate a paradoxical subreg instead. That will force a reload
11078 of the original memref X. */
11079 if (isize < osize)
11080 return gen_rtx_SUBREG (omode, x, 0);
11082 if (WORDS_BIG_ENDIAN)
11083 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11085 /* Adjust the address so that the address-after-the-data is
11086 unchanged. */
11087 if (BYTES_BIG_ENDIAN)
11088 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11090 return adjust_address_nv (x, omode, offset);
11093 /* If X is a comparison operator, rewrite it in a new mode. This
11094 probably won't match, but may allow further simplifications. */
11095 else if (COMPARISON_P (x))
11096 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11098 /* If we couldn't simplify X any other way, just enclose it in a
11099 SUBREG. Normally, this SUBREG won't match, but some patterns may
11100 include an explicit SUBREG or we may simplify it further in combine. */
11101 else
11103 int offset = 0;
11104 rtx res;
11106 offset = subreg_lowpart_offset (omode, imode);
11107 if (imode == VOIDmode)
11109 imode = int_mode_for_mode (omode);
11110 x = gen_lowpart_common (imode, x);
11111 if (x == NULL)
11112 goto fail;
11114 res = simplify_gen_subreg (omode, x, imode, offset);
11115 if (res)
11116 return res;
11119 fail:
11120 return gen_rtx_CLOBBER (omode, const0_rtx);
11123 /* Try to simplify a comparison between OP0 and a constant OP1,
11124 where CODE is the comparison code that will be tested, into a
11125 (CODE OP0 const0_rtx) form.
11127 The result is a possibly different comparison code to use.
11128 *POP1 may be updated. */
11130 static enum rtx_code
11131 simplify_compare_const (enum rtx_code code, machine_mode mode,
11132 rtx op0, rtx *pop1)
11134 unsigned int mode_width = GET_MODE_PRECISION (mode);
11135 HOST_WIDE_INT const_op = INTVAL (*pop1);
11137 /* Get the constant we are comparing against and turn off all bits
11138 not on in our mode. */
11139 if (mode != VOIDmode)
11140 const_op = trunc_int_for_mode (const_op, mode);
11142 /* If we are comparing against a constant power of two and the value
11143 being compared can only have that single bit nonzero (e.g., it was
11144 `and'ed with that bit), we can replace this with a comparison
11145 with zero. */
11146 if (const_op
11147 && (code == EQ || code == NE || code == GE || code == GEU
11148 || code == LT || code == LTU)
11149 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11150 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11151 && (nonzero_bits (op0, mode)
11152 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11154 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11155 const_op = 0;
11158 /* Similarly, if we are comparing a value known to be either -1 or
11159 0 with -1, change it to the opposite comparison against zero. */
11160 if (const_op == -1
11161 && (code == EQ || code == NE || code == GT || code == LE
11162 || code == GEU || code == LTU)
11163 && num_sign_bit_copies (op0, mode) == mode_width)
11165 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11166 const_op = 0;
11169 /* Do some canonicalizations based on the comparison code. We prefer
11170 comparisons against zero and then prefer equality comparisons.
11171 If we can reduce the size of a constant, we will do that too. */
11172 switch (code)
11174 case LT:
11175 /* < C is equivalent to <= (C - 1) */
11176 if (const_op > 0)
11178 const_op -= 1;
11179 code = LE;
11180 /* ... fall through to LE case below. */
11182 else
11183 break;
11185 case LE:
11186 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11187 if (const_op < 0)
11189 const_op += 1;
11190 code = LT;
11193 /* If we are doing a <= 0 comparison on a value known to have
11194 a zero sign bit, we can replace this with == 0. */
11195 else if (const_op == 0
11196 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11197 && (nonzero_bits (op0, mode)
11198 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11199 == 0)
11200 code = EQ;
11201 break;
11203 case GE:
11204 /* >= C is equivalent to > (C - 1). */
11205 if (const_op > 0)
11207 const_op -= 1;
11208 code = GT;
11209 /* ... fall through to GT below. */
11211 else
11212 break;
11214 case GT:
11215 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11216 if (const_op < 0)
11218 const_op += 1;
11219 code = GE;
11222 /* If we are doing a > 0 comparison on a value known to have
11223 a zero sign bit, we can replace this with != 0. */
11224 else if (const_op == 0
11225 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11226 && (nonzero_bits (op0, mode)
11227 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11228 == 0)
11229 code = NE;
11230 break;
11232 case LTU:
11233 /* < C is equivalent to <= (C - 1). */
11234 if (const_op > 0)
11236 const_op -= 1;
11237 code = LEU;
11238 /* ... fall through ... */
11240 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11241 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11242 && (unsigned HOST_WIDE_INT) const_op
11243 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11245 const_op = 0;
11246 code = GE;
11247 break;
11249 else
11250 break;
11252 case LEU:
11253 /* unsigned <= 0 is equivalent to == 0 */
11254 if (const_op == 0)
11255 code = EQ;
11256 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11257 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11258 && (unsigned HOST_WIDE_INT) const_op
11259 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11261 const_op = 0;
11262 code = GE;
11264 break;
11266 case GEU:
11267 /* >= C is equivalent to > (C - 1). */
11268 if (const_op > 1)
11270 const_op -= 1;
11271 code = GTU;
11272 /* ... fall through ... */
11275 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11276 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11277 && (unsigned HOST_WIDE_INT) const_op
11278 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11280 const_op = 0;
11281 code = LT;
11282 break;
11284 else
11285 break;
11287 case GTU:
11288 /* unsigned > 0 is equivalent to != 0 */
11289 if (const_op == 0)
11290 code = NE;
11291 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11292 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11293 && (unsigned HOST_WIDE_INT) const_op
11294 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11296 const_op = 0;
11297 code = LT;
11299 break;
11301 default:
11302 break;
11305 *pop1 = GEN_INT (const_op);
11306 return code;
11309 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11310 comparison code that will be tested.
11312 The result is a possibly different comparison code to use. *POP0 and
11313 *POP1 may be updated.
11315 It is possible that we might detect that a comparison is either always
11316 true or always false. However, we do not perform general constant
11317 folding in combine, so this knowledge isn't useful. Such tautologies
11318 should have been detected earlier. Hence we ignore all such cases. */
11320 static enum rtx_code
11321 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11323 rtx op0 = *pop0;
11324 rtx op1 = *pop1;
11325 rtx tem, tem1;
11326 int i;
11327 machine_mode mode, tmode;
11329 /* Try a few ways of applying the same transformation to both operands. */
11330 while (1)
11332 #ifndef WORD_REGISTER_OPERATIONS
11333 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11334 so check specially. */
11335 if (code != GTU && code != GEU && code != LTU && code != LEU
11336 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11337 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11338 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11339 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11340 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11341 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11342 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11343 && CONST_INT_P (XEXP (op0, 1))
11344 && XEXP (op0, 1) == XEXP (op1, 1)
11345 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11346 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11347 && (INTVAL (XEXP (op0, 1))
11348 == (GET_MODE_PRECISION (GET_MODE (op0))
11349 - (GET_MODE_PRECISION
11350 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11352 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11353 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11355 #endif
11357 /* If both operands are the same constant shift, see if we can ignore the
11358 shift. We can if the shift is a rotate or if the bits shifted out of
11359 this shift are known to be zero for both inputs and if the type of
11360 comparison is compatible with the shift. */
11361 if (GET_CODE (op0) == GET_CODE (op1)
11362 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11363 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11364 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11365 && (code != GT && code != LT && code != GE && code != LE))
11366 || (GET_CODE (op0) == ASHIFTRT
11367 && (code != GTU && code != LTU
11368 && code != GEU && code != LEU)))
11369 && CONST_INT_P (XEXP (op0, 1))
11370 && INTVAL (XEXP (op0, 1)) >= 0
11371 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11372 && XEXP (op0, 1) == XEXP (op1, 1))
11374 machine_mode mode = GET_MODE (op0);
11375 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11376 int shift_count = INTVAL (XEXP (op0, 1));
11378 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11379 mask &= (mask >> shift_count) << shift_count;
11380 else if (GET_CODE (op0) == ASHIFT)
11381 mask = (mask & (mask << shift_count)) >> shift_count;
11383 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11384 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11385 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11386 else
11387 break;
11390 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11391 SUBREGs are of the same mode, and, in both cases, the AND would
11392 be redundant if the comparison was done in the narrower mode,
11393 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11394 and the operand's possibly nonzero bits are 0xffffff01; in that case
11395 if we only care about QImode, we don't need the AND). This case
11396 occurs if the output mode of an scc insn is not SImode and
11397 STORE_FLAG_VALUE == 1 (e.g., the 386).
11399 Similarly, check for a case where the AND's are ZERO_EXTEND
11400 operations from some narrower mode even though a SUBREG is not
11401 present. */
11403 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11404 && CONST_INT_P (XEXP (op0, 1))
11405 && CONST_INT_P (XEXP (op1, 1)))
11407 rtx inner_op0 = XEXP (op0, 0);
11408 rtx inner_op1 = XEXP (op1, 0);
11409 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11410 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11411 int changed = 0;
11413 if (paradoxical_subreg_p (inner_op0)
11414 && GET_CODE (inner_op1) == SUBREG
11415 && (GET_MODE (SUBREG_REG (inner_op0))
11416 == GET_MODE (SUBREG_REG (inner_op1)))
11417 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11418 <= HOST_BITS_PER_WIDE_INT)
11419 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11420 GET_MODE (SUBREG_REG (inner_op0)))))
11421 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11422 GET_MODE (SUBREG_REG (inner_op1))))))
11424 op0 = SUBREG_REG (inner_op0);
11425 op1 = SUBREG_REG (inner_op1);
11427 /* The resulting comparison is always unsigned since we masked
11428 off the original sign bit. */
11429 code = unsigned_condition (code);
11431 changed = 1;
11434 else if (c0 == c1)
11435 for (tmode = GET_CLASS_NARROWEST_MODE
11436 (GET_MODE_CLASS (GET_MODE (op0)));
11437 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11438 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11440 op0 = gen_lowpart (tmode, inner_op0);
11441 op1 = gen_lowpart (tmode, inner_op1);
11442 code = unsigned_condition (code);
11443 changed = 1;
11444 break;
11447 if (! changed)
11448 break;
11451 /* If both operands are NOT, we can strip off the outer operation
11452 and adjust the comparison code for swapped operands; similarly for
11453 NEG, except that this must be an equality comparison. */
11454 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11455 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11456 && (code == EQ || code == NE)))
11457 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11459 else
11460 break;
11463 /* If the first operand is a constant, swap the operands and adjust the
11464 comparison code appropriately, but don't do this if the second operand
11465 is already a constant integer. */
11466 if (swap_commutative_operands_p (op0, op1))
11468 tem = op0, op0 = op1, op1 = tem;
11469 code = swap_condition (code);
11472 /* We now enter a loop during which we will try to simplify the comparison.
11473 For the most part, we only are concerned with comparisons with zero,
11474 but some things may really be comparisons with zero but not start
11475 out looking that way. */
11477 while (CONST_INT_P (op1))
11479 machine_mode mode = GET_MODE (op0);
11480 unsigned int mode_width = GET_MODE_PRECISION (mode);
11481 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11482 int equality_comparison_p;
11483 int sign_bit_comparison_p;
11484 int unsigned_comparison_p;
11485 HOST_WIDE_INT const_op;
11487 /* We only want to handle integral modes. This catches VOIDmode,
11488 CCmode, and the floating-point modes. An exception is that we
11489 can handle VOIDmode if OP0 is a COMPARE or a comparison
11490 operation. */
11492 if (GET_MODE_CLASS (mode) != MODE_INT
11493 && ! (mode == VOIDmode
11494 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11495 break;
11497 /* Try to simplify the compare to constant, possibly changing the
11498 comparison op, and/or changing op1 to zero. */
11499 code = simplify_compare_const (code, mode, op0, &op1);
11500 const_op = INTVAL (op1);
11502 /* Compute some predicates to simplify code below. */
11504 equality_comparison_p = (code == EQ || code == NE);
11505 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11506 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11507 || code == GEU);
11509 /* If this is a sign bit comparison and we can do arithmetic in
11510 MODE, say that we will only be needing the sign bit of OP0. */
11511 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11512 op0 = force_to_mode (op0, mode,
11513 (unsigned HOST_WIDE_INT) 1
11514 << (GET_MODE_PRECISION (mode) - 1),
11517 /* Now try cases based on the opcode of OP0. If none of the cases
11518 does a "continue", we exit this loop immediately after the
11519 switch. */
11521 switch (GET_CODE (op0))
11523 case ZERO_EXTRACT:
11524 /* If we are extracting a single bit from a variable position in
11525 a constant that has only a single bit set and are comparing it
11526 with zero, we can convert this into an equality comparison
11527 between the position and the location of the single bit. */
11528 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11529 have already reduced the shift count modulo the word size. */
11530 if (!SHIFT_COUNT_TRUNCATED
11531 && CONST_INT_P (XEXP (op0, 0))
11532 && XEXP (op0, 1) == const1_rtx
11533 && equality_comparison_p && const_op == 0
11534 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11536 if (BITS_BIG_ENDIAN)
11537 i = BITS_PER_WORD - 1 - i;
11539 op0 = XEXP (op0, 2);
11540 op1 = GEN_INT (i);
11541 const_op = i;
11543 /* Result is nonzero iff shift count is equal to I. */
11544 code = reverse_condition (code);
11545 continue;
11548 /* ... fall through ... */
11550 case SIGN_EXTRACT:
11551 tem = expand_compound_operation (op0);
11552 if (tem != op0)
11554 op0 = tem;
11555 continue;
11557 break;
11559 case NOT:
11560 /* If testing for equality, we can take the NOT of the constant. */
11561 if (equality_comparison_p
11562 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11564 op0 = XEXP (op0, 0);
11565 op1 = tem;
11566 continue;
11569 /* If just looking at the sign bit, reverse the sense of the
11570 comparison. */
11571 if (sign_bit_comparison_p)
11573 op0 = XEXP (op0, 0);
11574 code = (code == GE ? LT : GE);
11575 continue;
11577 break;
11579 case NEG:
11580 /* If testing for equality, we can take the NEG of the constant. */
11581 if (equality_comparison_p
11582 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11584 op0 = XEXP (op0, 0);
11585 op1 = tem;
11586 continue;
11589 /* The remaining cases only apply to comparisons with zero. */
11590 if (const_op != 0)
11591 break;
11593 /* When X is ABS or is known positive,
11594 (neg X) is < 0 if and only if X != 0. */
11596 if (sign_bit_comparison_p
11597 && (GET_CODE (XEXP (op0, 0)) == ABS
11598 || (mode_width <= HOST_BITS_PER_WIDE_INT
11599 && (nonzero_bits (XEXP (op0, 0), mode)
11600 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11601 == 0)))
11603 op0 = XEXP (op0, 0);
11604 code = (code == LT ? NE : EQ);
11605 continue;
11608 /* If we have NEG of something whose two high-order bits are the
11609 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11610 if (num_sign_bit_copies (op0, mode) >= 2)
11612 op0 = XEXP (op0, 0);
11613 code = swap_condition (code);
11614 continue;
11616 break;
11618 case ROTATE:
11619 /* If we are testing equality and our count is a constant, we
11620 can perform the inverse operation on our RHS. */
11621 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11622 && (tem = simplify_binary_operation (ROTATERT, mode,
11623 op1, XEXP (op0, 1))) != 0)
11625 op0 = XEXP (op0, 0);
11626 op1 = tem;
11627 continue;
11630 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11631 a particular bit. Convert it to an AND of a constant of that
11632 bit. This will be converted into a ZERO_EXTRACT. */
11633 if (const_op == 0 && sign_bit_comparison_p
11634 && CONST_INT_P (XEXP (op0, 1))
11635 && mode_width <= HOST_BITS_PER_WIDE_INT)
11637 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11638 ((unsigned HOST_WIDE_INT) 1
11639 << (mode_width - 1
11640 - INTVAL (XEXP (op0, 1)))));
11641 code = (code == LT ? NE : EQ);
11642 continue;
11645 /* Fall through. */
11647 case ABS:
11648 /* ABS is ignorable inside an equality comparison with zero. */
11649 if (const_op == 0 && equality_comparison_p)
11651 op0 = XEXP (op0, 0);
11652 continue;
11654 break;
11656 case SIGN_EXTEND:
11657 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11658 (compare FOO CONST) if CONST fits in FOO's mode and we
11659 are either testing inequality or have an unsigned
11660 comparison with ZERO_EXTEND or a signed comparison with
11661 SIGN_EXTEND. But don't do it if we don't have a compare
11662 insn of the given mode, since we'd have to revert it
11663 later on, and then we wouldn't know whether to sign- or
11664 zero-extend. */
11665 mode = GET_MODE (XEXP (op0, 0));
11666 if (GET_MODE_CLASS (mode) == MODE_INT
11667 && ! unsigned_comparison_p
11668 && HWI_COMPUTABLE_MODE_P (mode)
11669 && trunc_int_for_mode (const_op, mode) == const_op
11670 && have_insn_for (COMPARE, mode))
11672 op0 = XEXP (op0, 0);
11673 continue;
11675 break;
11677 case SUBREG:
11678 /* Check for the case where we are comparing A - C1 with C2, that is
11680 (subreg:MODE (plus (A) (-C1))) op (C2)
11682 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11683 comparison in the wider mode. One of the following two conditions
11684 must be true in order for this to be valid:
11686 1. The mode extension results in the same bit pattern being added
11687 on both sides and the comparison is equality or unsigned. As
11688 C2 has been truncated to fit in MODE, the pattern can only be
11689 all 0s or all 1s.
11691 2. The mode extension results in the sign bit being copied on
11692 each side.
11694 The difficulty here is that we have predicates for A but not for
11695 (A - C1) so we need to check that C1 is within proper bounds so
11696 as to perturbate A as little as possible. */
11698 if (mode_width <= HOST_BITS_PER_WIDE_INT
11699 && subreg_lowpart_p (op0)
11700 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11701 && GET_CODE (SUBREG_REG (op0)) == PLUS
11702 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11704 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11705 rtx a = XEXP (SUBREG_REG (op0), 0);
11706 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11708 if ((c1 > 0
11709 && (unsigned HOST_WIDE_INT) c1
11710 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11711 && (equality_comparison_p || unsigned_comparison_p)
11712 /* (A - C1) zero-extends if it is positive and sign-extends
11713 if it is negative, C2 both zero- and sign-extends. */
11714 && ((0 == (nonzero_bits (a, inner_mode)
11715 & ~GET_MODE_MASK (mode))
11716 && const_op >= 0)
11717 /* (A - C1) sign-extends if it is positive and 1-extends
11718 if it is negative, C2 both sign- and 1-extends. */
11719 || (num_sign_bit_copies (a, inner_mode)
11720 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11721 - mode_width)
11722 && const_op < 0)))
11723 || ((unsigned HOST_WIDE_INT) c1
11724 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11725 /* (A - C1) always sign-extends, like C2. */
11726 && num_sign_bit_copies (a, inner_mode)
11727 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11728 - (mode_width - 1))))
11730 op0 = SUBREG_REG (op0);
11731 continue;
11735 /* If the inner mode is narrower and we are extracting the low part,
11736 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11737 if (subreg_lowpart_p (op0)
11738 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11739 /* Fall through */ ;
11740 else
11741 break;
11743 /* ... fall through ... */
11745 case ZERO_EXTEND:
11746 mode = GET_MODE (XEXP (op0, 0));
11747 if (GET_MODE_CLASS (mode) == MODE_INT
11748 && (unsigned_comparison_p || equality_comparison_p)
11749 && HWI_COMPUTABLE_MODE_P (mode)
11750 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11751 && const_op >= 0
11752 && have_insn_for (COMPARE, mode))
11754 op0 = XEXP (op0, 0);
11755 continue;
11757 break;
11759 case PLUS:
11760 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11761 this for equality comparisons due to pathological cases involving
11762 overflows. */
11763 if (equality_comparison_p
11764 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11765 op1, XEXP (op0, 1))))
11767 op0 = XEXP (op0, 0);
11768 op1 = tem;
11769 continue;
11772 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11773 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11774 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11776 op0 = XEXP (XEXP (op0, 0), 0);
11777 code = (code == LT ? EQ : NE);
11778 continue;
11780 break;
11782 case MINUS:
11783 /* We used to optimize signed comparisons against zero, but that
11784 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11785 arrive here as equality comparisons, or (GEU, LTU) are
11786 optimized away. No need to special-case them. */
11788 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11789 (eq B (minus A C)), whichever simplifies. We can only do
11790 this for equality comparisons due to pathological cases involving
11791 overflows. */
11792 if (equality_comparison_p
11793 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11794 XEXP (op0, 1), op1)))
11796 op0 = XEXP (op0, 0);
11797 op1 = tem;
11798 continue;
11801 if (equality_comparison_p
11802 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11803 XEXP (op0, 0), op1)))
11805 op0 = XEXP (op0, 1);
11806 op1 = tem;
11807 continue;
11810 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11811 of bits in X minus 1, is one iff X > 0. */
11812 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11813 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11814 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11815 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11817 op0 = XEXP (op0, 1);
11818 code = (code == GE ? LE : GT);
11819 continue;
11821 break;
11823 case XOR:
11824 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11825 if C is zero or B is a constant. */
11826 if (equality_comparison_p
11827 && 0 != (tem = simplify_binary_operation (XOR, mode,
11828 XEXP (op0, 1), op1)))
11830 op0 = XEXP (op0, 0);
11831 op1 = tem;
11832 continue;
11834 break;
11836 case EQ: case NE:
11837 case UNEQ: case LTGT:
11838 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11839 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11840 case UNORDERED: case ORDERED:
11841 /* We can't do anything if OP0 is a condition code value, rather
11842 than an actual data value. */
11843 if (const_op != 0
11844 || CC0_P (XEXP (op0, 0))
11845 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11846 break;
11848 /* Get the two operands being compared. */
11849 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11850 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11851 else
11852 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11854 /* Check for the cases where we simply want the result of the
11855 earlier test or the opposite of that result. */
11856 if (code == NE || code == EQ
11857 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11858 && (code == LT || code == GE)))
11860 enum rtx_code new_code;
11861 if (code == LT || code == NE)
11862 new_code = GET_CODE (op0);
11863 else
11864 new_code = reversed_comparison_code (op0, NULL);
11866 if (new_code != UNKNOWN)
11868 code = new_code;
11869 op0 = tem;
11870 op1 = tem1;
11871 continue;
11874 break;
11876 case IOR:
11877 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11878 iff X <= 0. */
11879 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11880 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11881 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11883 op0 = XEXP (op0, 1);
11884 code = (code == GE ? GT : LE);
11885 continue;
11887 break;
11889 case AND:
11890 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11891 will be converted to a ZERO_EXTRACT later. */
11892 if (const_op == 0 && equality_comparison_p
11893 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11894 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11896 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11897 XEXP (XEXP (op0, 0), 1));
11898 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11899 continue;
11902 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11903 zero and X is a comparison and C1 and C2 describe only bits set
11904 in STORE_FLAG_VALUE, we can compare with X. */
11905 if (const_op == 0 && equality_comparison_p
11906 && mode_width <= HOST_BITS_PER_WIDE_INT
11907 && CONST_INT_P (XEXP (op0, 1))
11908 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11909 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11910 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11911 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11913 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11914 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11915 if ((~STORE_FLAG_VALUE & mask) == 0
11916 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11917 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11918 && COMPARISON_P (tem))))
11920 op0 = XEXP (XEXP (op0, 0), 0);
11921 continue;
11925 /* If we are doing an equality comparison of an AND of a bit equal
11926 to the sign bit, replace this with a LT or GE comparison of
11927 the underlying value. */
11928 if (equality_comparison_p
11929 && const_op == 0
11930 && CONST_INT_P (XEXP (op0, 1))
11931 && mode_width <= HOST_BITS_PER_WIDE_INT
11932 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11933 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11935 op0 = XEXP (op0, 0);
11936 code = (code == EQ ? GE : LT);
11937 continue;
11940 /* If this AND operation is really a ZERO_EXTEND from a narrower
11941 mode, the constant fits within that mode, and this is either an
11942 equality or unsigned comparison, try to do this comparison in
11943 the narrower mode.
11945 Note that in:
11947 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11948 -> (ne:DI (reg:SI 4) (const_int 0))
11950 unless TRULY_NOOP_TRUNCATION allows it or the register is
11951 known to hold a value of the required mode the
11952 transformation is invalid. */
11953 if ((equality_comparison_p || unsigned_comparison_p)
11954 && CONST_INT_P (XEXP (op0, 1))
11955 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11956 & GET_MODE_MASK (mode))
11957 + 1)) >= 0
11958 && const_op >> i == 0
11959 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11960 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11961 || (REG_P (XEXP (op0, 0))
11962 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11964 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11965 continue;
11968 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11969 fits in both M1 and M2 and the SUBREG is either paradoxical
11970 or represents the low part, permute the SUBREG and the AND
11971 and try again. */
11972 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11974 unsigned HOST_WIDE_INT c1;
11975 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11976 /* Require an integral mode, to avoid creating something like
11977 (AND:SF ...). */
11978 if (SCALAR_INT_MODE_P (tmode)
11979 /* It is unsafe to commute the AND into the SUBREG if the
11980 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11981 not defined. As originally written the upper bits
11982 have a defined value due to the AND operation.
11983 However, if we commute the AND inside the SUBREG then
11984 they no longer have defined values and the meaning of
11985 the code has been changed. */
11986 && (0
11987 #ifdef WORD_REGISTER_OPERATIONS
11988 || (mode_width > GET_MODE_PRECISION (tmode)
11989 && mode_width <= BITS_PER_WORD)
11990 #endif
11991 || (mode_width <= GET_MODE_PRECISION (tmode)
11992 && subreg_lowpart_p (XEXP (op0, 0))))
11993 && CONST_INT_P (XEXP (op0, 1))
11994 && mode_width <= HOST_BITS_PER_WIDE_INT
11995 && HWI_COMPUTABLE_MODE_P (tmode)
11996 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11997 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11998 && c1 != mask
11999 && c1 != GET_MODE_MASK (tmode))
12001 op0 = simplify_gen_binary (AND, tmode,
12002 SUBREG_REG (XEXP (op0, 0)),
12003 gen_int_mode (c1, tmode));
12004 op0 = gen_lowpart (mode, op0);
12005 continue;
12009 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12010 if (const_op == 0 && equality_comparison_p
12011 && XEXP (op0, 1) == const1_rtx
12012 && GET_CODE (XEXP (op0, 0)) == NOT)
12014 op0 = simplify_and_const_int (NULL_RTX, mode,
12015 XEXP (XEXP (op0, 0), 0), 1);
12016 code = (code == NE ? EQ : NE);
12017 continue;
12020 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12021 (eq (and (lshiftrt X) 1) 0).
12022 Also handle the case where (not X) is expressed using xor. */
12023 if (const_op == 0 && equality_comparison_p
12024 && XEXP (op0, 1) == const1_rtx
12025 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12027 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12028 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12030 if (GET_CODE (shift_op) == NOT
12031 || (GET_CODE (shift_op) == XOR
12032 && CONST_INT_P (XEXP (shift_op, 1))
12033 && CONST_INT_P (shift_count)
12034 && HWI_COMPUTABLE_MODE_P (mode)
12035 && (UINTVAL (XEXP (shift_op, 1))
12036 == (unsigned HOST_WIDE_INT) 1
12037 << INTVAL (shift_count))))
12040 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12041 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12042 code = (code == NE ? EQ : NE);
12043 continue;
12046 break;
12048 case ASHIFT:
12049 /* If we have (compare (ashift FOO N) (const_int C)) and
12050 the high order N bits of FOO (N+1 if an inequality comparison)
12051 are known to be zero, we can do this by comparing FOO with C
12052 shifted right N bits so long as the low-order N bits of C are
12053 zero. */
12054 if (CONST_INT_P (XEXP (op0, 1))
12055 && INTVAL (XEXP (op0, 1)) >= 0
12056 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12057 < HOST_BITS_PER_WIDE_INT)
12058 && (((unsigned HOST_WIDE_INT) const_op
12059 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
12060 - 1)) == 0)
12061 && mode_width <= HOST_BITS_PER_WIDE_INT
12062 && (nonzero_bits (XEXP (op0, 0), mode)
12063 & ~(mask >> (INTVAL (XEXP (op0, 1))
12064 + ! equality_comparison_p))) == 0)
12066 /* We must perform a logical shift, not an arithmetic one,
12067 as we want the top N bits of C to be zero. */
12068 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12070 temp >>= INTVAL (XEXP (op0, 1));
12071 op1 = gen_int_mode (temp, mode);
12072 op0 = XEXP (op0, 0);
12073 continue;
12076 /* If we are doing a sign bit comparison, it means we are testing
12077 a particular bit. Convert it to the appropriate AND. */
12078 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12079 && mode_width <= HOST_BITS_PER_WIDE_INT)
12081 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12082 ((unsigned HOST_WIDE_INT) 1
12083 << (mode_width - 1
12084 - INTVAL (XEXP (op0, 1)))));
12085 code = (code == LT ? NE : EQ);
12086 continue;
12089 /* If this an equality comparison with zero and we are shifting
12090 the low bit to the sign bit, we can convert this to an AND of the
12091 low-order bit. */
12092 if (const_op == 0 && equality_comparison_p
12093 && CONST_INT_P (XEXP (op0, 1))
12094 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12096 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12097 continue;
12099 break;
12101 case ASHIFTRT:
12102 /* If this is an equality comparison with zero, we can do this
12103 as a logical shift, which might be much simpler. */
12104 if (equality_comparison_p && const_op == 0
12105 && CONST_INT_P (XEXP (op0, 1)))
12107 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12108 XEXP (op0, 0),
12109 INTVAL (XEXP (op0, 1)));
12110 continue;
12113 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12114 do the comparison in a narrower mode. */
12115 if (! unsigned_comparison_p
12116 && CONST_INT_P (XEXP (op0, 1))
12117 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12118 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12119 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12120 MODE_INT, 1)) != BLKmode
12121 && (((unsigned HOST_WIDE_INT) const_op
12122 + (GET_MODE_MASK (tmode) >> 1) + 1)
12123 <= GET_MODE_MASK (tmode)))
12125 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12126 continue;
12129 /* Likewise if OP0 is a PLUS of a sign extension with a
12130 constant, which is usually represented with the PLUS
12131 between the shifts. */
12132 if (! unsigned_comparison_p
12133 && CONST_INT_P (XEXP (op0, 1))
12134 && GET_CODE (XEXP (op0, 0)) == PLUS
12135 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12136 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12137 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12138 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12139 MODE_INT, 1)) != BLKmode
12140 && (((unsigned HOST_WIDE_INT) const_op
12141 + (GET_MODE_MASK (tmode) >> 1) + 1)
12142 <= GET_MODE_MASK (tmode)))
12144 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12145 rtx add_const = XEXP (XEXP (op0, 0), 1);
12146 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12147 add_const, XEXP (op0, 1));
12149 op0 = simplify_gen_binary (PLUS, tmode,
12150 gen_lowpart (tmode, inner),
12151 new_const);
12152 continue;
12155 /* ... fall through ... */
12156 case LSHIFTRT:
12157 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12158 the low order N bits of FOO are known to be zero, we can do this
12159 by comparing FOO with C shifted left N bits so long as no
12160 overflow occurs. Even if the low order N bits of FOO aren't known
12161 to be zero, if the comparison is >= or < we can use the same
12162 optimization and for > or <= by setting all the low
12163 order N bits in the comparison constant. */
12164 if (CONST_INT_P (XEXP (op0, 1))
12165 && INTVAL (XEXP (op0, 1)) > 0
12166 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12167 && mode_width <= HOST_BITS_PER_WIDE_INT
12168 && (((unsigned HOST_WIDE_INT) const_op
12169 + (GET_CODE (op0) != LSHIFTRT
12170 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12171 + 1)
12172 : 0))
12173 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12175 unsigned HOST_WIDE_INT low_bits
12176 = (nonzero_bits (XEXP (op0, 0), mode)
12177 & (((unsigned HOST_WIDE_INT) 1
12178 << INTVAL (XEXP (op0, 1))) - 1));
12179 if (low_bits == 0 || !equality_comparison_p)
12181 /* If the shift was logical, then we must make the condition
12182 unsigned. */
12183 if (GET_CODE (op0) == LSHIFTRT)
12184 code = unsigned_condition (code);
12186 const_op <<= INTVAL (XEXP (op0, 1));
12187 if (low_bits != 0
12188 && (code == GT || code == GTU
12189 || code == LE || code == LEU))
12190 const_op
12191 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12192 op1 = GEN_INT (const_op);
12193 op0 = XEXP (op0, 0);
12194 continue;
12198 /* If we are using this shift to extract just the sign bit, we
12199 can replace this with an LT or GE comparison. */
12200 if (const_op == 0
12201 && (equality_comparison_p || sign_bit_comparison_p)
12202 && CONST_INT_P (XEXP (op0, 1))
12203 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12205 op0 = XEXP (op0, 0);
12206 code = (code == NE || code == GT ? LT : GE);
12207 continue;
12209 break;
12211 default:
12212 break;
12215 break;
12218 /* Now make any compound operations involved in this comparison. Then,
12219 check for an outmost SUBREG on OP0 that is not doing anything or is
12220 paradoxical. The latter transformation must only be performed when
12221 it is known that the "extra" bits will be the same in op0 and op1 or
12222 that they don't matter. There are three cases to consider:
12224 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12225 care bits and we can assume they have any convenient value. So
12226 making the transformation is safe.
12228 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12229 In this case the upper bits of op0 are undefined. We should not make
12230 the simplification in that case as we do not know the contents of
12231 those bits.
12233 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12234 UNKNOWN. In that case we know those bits are zeros or ones. We must
12235 also be sure that they are the same as the upper bits of op1.
12237 We can never remove a SUBREG for a non-equality comparison because
12238 the sign bit is in a different place in the underlying object. */
12240 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12241 op1 = make_compound_operation (op1, SET);
12243 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12244 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12245 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12246 && (code == NE || code == EQ))
12248 if (paradoxical_subreg_p (op0))
12250 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12251 implemented. */
12252 if (REG_P (SUBREG_REG (op0)))
12254 op0 = SUBREG_REG (op0);
12255 op1 = gen_lowpart (GET_MODE (op0), op1);
12258 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12259 <= HOST_BITS_PER_WIDE_INT)
12260 && (nonzero_bits (SUBREG_REG (op0),
12261 GET_MODE (SUBREG_REG (op0)))
12262 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12264 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12266 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12267 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12268 op0 = SUBREG_REG (op0), op1 = tem;
12272 /* We now do the opposite procedure: Some machines don't have compare
12273 insns in all modes. If OP0's mode is an integer mode smaller than a
12274 word and we can't do a compare in that mode, see if there is a larger
12275 mode for which we can do the compare. There are a number of cases in
12276 which we can use the wider mode. */
12278 mode = GET_MODE (op0);
12279 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12280 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12281 && ! have_insn_for (COMPARE, mode))
12282 for (tmode = GET_MODE_WIDER_MODE (mode);
12283 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12284 tmode = GET_MODE_WIDER_MODE (tmode))
12285 if (have_insn_for (COMPARE, tmode))
12287 int zero_extended;
12289 /* If this is a test for negative, we can make an explicit
12290 test of the sign bit. Test this first so we can use
12291 a paradoxical subreg to extend OP0. */
12293 if (op1 == const0_rtx && (code == LT || code == GE)
12294 && HWI_COMPUTABLE_MODE_P (mode))
12296 unsigned HOST_WIDE_INT sign
12297 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12298 op0 = simplify_gen_binary (AND, tmode,
12299 gen_lowpart (tmode, op0),
12300 gen_int_mode (sign, tmode));
12301 code = (code == LT) ? NE : EQ;
12302 break;
12305 /* If the only nonzero bits in OP0 and OP1 are those in the
12306 narrower mode and this is an equality or unsigned comparison,
12307 we can use the wider mode. Similarly for sign-extended
12308 values, in which case it is true for all comparisons. */
12309 zero_extended = ((code == EQ || code == NE
12310 || code == GEU || code == GTU
12311 || code == LEU || code == LTU)
12312 && (nonzero_bits (op0, tmode)
12313 & ~GET_MODE_MASK (mode)) == 0
12314 && ((CONST_INT_P (op1)
12315 || (nonzero_bits (op1, tmode)
12316 & ~GET_MODE_MASK (mode)) == 0)));
12318 if (zero_extended
12319 || ((num_sign_bit_copies (op0, tmode)
12320 > (unsigned int) (GET_MODE_PRECISION (tmode)
12321 - GET_MODE_PRECISION (mode)))
12322 && (num_sign_bit_copies (op1, tmode)
12323 > (unsigned int) (GET_MODE_PRECISION (tmode)
12324 - GET_MODE_PRECISION (mode)))))
12326 /* If OP0 is an AND and we don't have an AND in MODE either,
12327 make a new AND in the proper mode. */
12328 if (GET_CODE (op0) == AND
12329 && !have_insn_for (AND, mode))
12330 op0 = simplify_gen_binary (AND, tmode,
12331 gen_lowpart (tmode,
12332 XEXP (op0, 0)),
12333 gen_lowpart (tmode,
12334 XEXP (op0, 1)));
12335 else
12337 if (zero_extended)
12339 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12340 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12342 else
12344 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12345 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12347 break;
12352 /* We may have changed the comparison operands. Re-canonicalize. */
12353 if (swap_commutative_operands_p (op0, op1))
12355 tem = op0, op0 = op1, op1 = tem;
12356 code = swap_condition (code);
12359 /* If this machine only supports a subset of valid comparisons, see if we
12360 can convert an unsupported one into a supported one. */
12361 target_canonicalize_comparison (&code, &op0, &op1, 0);
12363 *pop0 = op0;
12364 *pop1 = op1;
12366 return code;
12369 /* Utility function for record_value_for_reg. Count number of
12370 rtxs in X. */
12371 static int
12372 count_rtxs (rtx x)
12374 enum rtx_code code = GET_CODE (x);
12375 const char *fmt;
12376 int i, j, ret = 1;
12378 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12379 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12381 rtx x0 = XEXP (x, 0);
12382 rtx x1 = XEXP (x, 1);
12384 if (x0 == x1)
12385 return 1 + 2 * count_rtxs (x0);
12387 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12388 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12389 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12390 return 2 + 2 * count_rtxs (x0)
12391 + count_rtxs (x == XEXP (x1, 0)
12392 ? XEXP (x1, 1) : XEXP (x1, 0));
12394 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12395 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12396 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12397 return 2 + 2 * count_rtxs (x1)
12398 + count_rtxs (x == XEXP (x0, 0)
12399 ? XEXP (x0, 1) : XEXP (x0, 0));
12402 fmt = GET_RTX_FORMAT (code);
12403 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12404 if (fmt[i] == 'e')
12405 ret += count_rtxs (XEXP (x, i));
12406 else if (fmt[i] == 'E')
12407 for (j = 0; j < XVECLEN (x, i); j++)
12408 ret += count_rtxs (XVECEXP (x, i, j));
12410 return ret;
12413 /* Utility function for following routine. Called when X is part of a value
12414 being stored into last_set_value. Sets last_set_table_tick
12415 for each register mentioned. Similar to mention_regs in cse.c */
12417 static void
12418 update_table_tick (rtx x)
12420 enum rtx_code code = GET_CODE (x);
12421 const char *fmt = GET_RTX_FORMAT (code);
12422 int i, j;
12424 if (code == REG)
12426 unsigned int regno = REGNO (x);
12427 unsigned int endregno = END_REGNO (x);
12428 unsigned int r;
12430 for (r = regno; r < endregno; r++)
12432 reg_stat_type *rsp = &reg_stat[r];
12433 rsp->last_set_table_tick = label_tick;
12436 return;
12439 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12440 if (fmt[i] == 'e')
12442 /* Check for identical subexpressions. If x contains
12443 identical subexpression we only have to traverse one of
12444 them. */
12445 if (i == 0 && ARITHMETIC_P (x))
12447 /* Note that at this point x1 has already been
12448 processed. */
12449 rtx x0 = XEXP (x, 0);
12450 rtx x1 = XEXP (x, 1);
12452 /* If x0 and x1 are identical then there is no need to
12453 process x0. */
12454 if (x0 == x1)
12455 break;
12457 /* If x0 is identical to a subexpression of x1 then while
12458 processing x1, x0 has already been processed. Thus we
12459 are done with x. */
12460 if (ARITHMETIC_P (x1)
12461 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12462 break;
12464 /* If x1 is identical to a subexpression of x0 then we
12465 still have to process the rest of x0. */
12466 if (ARITHMETIC_P (x0)
12467 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12469 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12470 break;
12474 update_table_tick (XEXP (x, i));
12476 else if (fmt[i] == 'E')
12477 for (j = 0; j < XVECLEN (x, i); j++)
12478 update_table_tick (XVECEXP (x, i, j));
12481 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12482 are saying that the register is clobbered and we no longer know its
12483 value. If INSN is zero, don't update reg_stat[].last_set; this is
12484 only permitted with VALUE also zero and is used to invalidate the
12485 register. */
12487 static void
12488 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12490 unsigned int regno = REGNO (reg);
12491 unsigned int endregno = END_REGNO (reg);
12492 unsigned int i;
12493 reg_stat_type *rsp;
12495 /* If VALUE contains REG and we have a previous value for REG, substitute
12496 the previous value. */
12497 if (value && insn && reg_overlap_mentioned_p (reg, value))
12499 rtx tem;
12501 /* Set things up so get_last_value is allowed to see anything set up to
12502 our insn. */
12503 subst_low_luid = DF_INSN_LUID (insn);
12504 tem = get_last_value (reg);
12506 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12507 it isn't going to be useful and will take a lot of time to process,
12508 so just use the CLOBBER. */
12510 if (tem)
12512 if (ARITHMETIC_P (tem)
12513 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12514 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12515 tem = XEXP (tem, 0);
12516 else if (count_occurrences (value, reg, 1) >= 2)
12518 /* If there are two or more occurrences of REG in VALUE,
12519 prevent the value from growing too much. */
12520 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12521 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12524 value = replace_rtx (copy_rtx (value), reg, tem);
12528 /* For each register modified, show we don't know its value, that
12529 we don't know about its bitwise content, that its value has been
12530 updated, and that we don't know the location of the death of the
12531 register. */
12532 for (i = regno; i < endregno; i++)
12534 rsp = &reg_stat[i];
12536 if (insn)
12537 rsp->last_set = insn;
12539 rsp->last_set_value = 0;
12540 rsp->last_set_mode = VOIDmode;
12541 rsp->last_set_nonzero_bits = 0;
12542 rsp->last_set_sign_bit_copies = 0;
12543 rsp->last_death = 0;
12544 rsp->truncated_to_mode = VOIDmode;
12547 /* Mark registers that are being referenced in this value. */
12548 if (value)
12549 update_table_tick (value);
12551 /* Now update the status of each register being set.
12552 If someone is using this register in this block, set this register
12553 to invalid since we will get confused between the two lives in this
12554 basic block. This makes using this register always invalid. In cse, we
12555 scan the table to invalidate all entries using this register, but this
12556 is too much work for us. */
12558 for (i = regno; i < endregno; i++)
12560 rsp = &reg_stat[i];
12561 rsp->last_set_label = label_tick;
12562 if (!insn
12563 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12564 rsp->last_set_invalid = 1;
12565 else
12566 rsp->last_set_invalid = 0;
12569 /* The value being assigned might refer to X (like in "x++;"). In that
12570 case, we must replace it with (clobber (const_int 0)) to prevent
12571 infinite loops. */
12572 rsp = &reg_stat[regno];
12573 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12575 value = copy_rtx (value);
12576 if (!get_last_value_validate (&value, insn, label_tick, 1))
12577 value = 0;
12580 /* For the main register being modified, update the value, the mode, the
12581 nonzero bits, and the number of sign bit copies. */
12583 rsp->last_set_value = value;
12585 if (value)
12587 machine_mode mode = GET_MODE (reg);
12588 subst_low_luid = DF_INSN_LUID (insn);
12589 rsp->last_set_mode = mode;
12590 if (GET_MODE_CLASS (mode) == MODE_INT
12591 && HWI_COMPUTABLE_MODE_P (mode))
12592 mode = nonzero_bits_mode;
12593 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12594 rsp->last_set_sign_bit_copies
12595 = num_sign_bit_copies (value, GET_MODE (reg));
12599 /* Called via note_stores from record_dead_and_set_regs to handle one
12600 SET or CLOBBER in an insn. DATA is the instruction in which the
12601 set is occurring. */
12603 static void
12604 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12606 rtx_insn *record_dead_insn = (rtx_insn *) data;
12608 if (GET_CODE (dest) == SUBREG)
12609 dest = SUBREG_REG (dest);
12611 if (!record_dead_insn)
12613 if (REG_P (dest))
12614 record_value_for_reg (dest, NULL, NULL_RTX);
12615 return;
12618 if (REG_P (dest))
12620 /* If we are setting the whole register, we know its value. Otherwise
12621 show that we don't know the value. We can handle SUBREG in
12622 some cases. */
12623 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12624 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12625 else if (GET_CODE (setter) == SET
12626 && GET_CODE (SET_DEST (setter)) == SUBREG
12627 && SUBREG_REG (SET_DEST (setter)) == dest
12628 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12629 && subreg_lowpart_p (SET_DEST (setter)))
12630 record_value_for_reg (dest, record_dead_insn,
12631 gen_lowpart (GET_MODE (dest),
12632 SET_SRC (setter)));
12633 else
12634 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12636 else if (MEM_P (dest)
12637 /* Ignore pushes, they clobber nothing. */
12638 && ! push_operand (dest, GET_MODE (dest)))
12639 mem_last_set = DF_INSN_LUID (record_dead_insn);
12642 /* Update the records of when each REG was most recently set or killed
12643 for the things done by INSN. This is the last thing done in processing
12644 INSN in the combiner loop.
12646 We update reg_stat[], in particular fields last_set, last_set_value,
12647 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12648 last_death, and also the similar information mem_last_set (which insn
12649 most recently modified memory) and last_call_luid (which insn was the
12650 most recent subroutine call). */
12652 static void
12653 record_dead_and_set_regs (rtx_insn *insn)
12655 rtx link;
12656 unsigned int i;
12658 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12660 if (REG_NOTE_KIND (link) == REG_DEAD
12661 && REG_P (XEXP (link, 0)))
12663 unsigned int regno = REGNO (XEXP (link, 0));
12664 unsigned int endregno = END_REGNO (XEXP (link, 0));
12666 for (i = regno; i < endregno; i++)
12668 reg_stat_type *rsp;
12670 rsp = &reg_stat[i];
12671 rsp->last_death = insn;
12674 else if (REG_NOTE_KIND (link) == REG_INC)
12675 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12678 if (CALL_P (insn))
12680 hard_reg_set_iterator hrsi;
12681 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12683 reg_stat_type *rsp;
12685 rsp = &reg_stat[i];
12686 rsp->last_set_invalid = 1;
12687 rsp->last_set = insn;
12688 rsp->last_set_value = 0;
12689 rsp->last_set_mode = VOIDmode;
12690 rsp->last_set_nonzero_bits = 0;
12691 rsp->last_set_sign_bit_copies = 0;
12692 rsp->last_death = 0;
12693 rsp->truncated_to_mode = VOIDmode;
12696 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12698 /* We can't combine into a call pattern. Remember, though, that
12699 the return value register is set at this LUID. We could
12700 still replace a register with the return value from the
12701 wrong subroutine call! */
12702 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12704 else
12705 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12708 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12709 register present in the SUBREG, so for each such SUBREG go back and
12710 adjust nonzero and sign bit information of the registers that are
12711 known to have some zero/sign bits set.
12713 This is needed because when combine blows the SUBREGs away, the
12714 information on zero/sign bits is lost and further combines can be
12715 missed because of that. */
12717 static void
12718 record_promoted_value (rtx_insn *insn, rtx subreg)
12720 struct insn_link *links;
12721 rtx set;
12722 unsigned int regno = REGNO (SUBREG_REG (subreg));
12723 machine_mode mode = GET_MODE (subreg);
12725 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12726 return;
12728 for (links = LOG_LINKS (insn); links;)
12730 reg_stat_type *rsp;
12732 insn = links->insn;
12733 set = single_set (insn);
12735 if (! set || !REG_P (SET_DEST (set))
12736 || REGNO (SET_DEST (set)) != regno
12737 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12739 links = links->next;
12740 continue;
12743 rsp = &reg_stat[regno];
12744 if (rsp->last_set == insn)
12746 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12747 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12750 if (REG_P (SET_SRC (set)))
12752 regno = REGNO (SET_SRC (set));
12753 links = LOG_LINKS (insn);
12755 else
12756 break;
12760 /* Check if X, a register, is known to contain a value already
12761 truncated to MODE. In this case we can use a subreg to refer to
12762 the truncated value even though in the generic case we would need
12763 an explicit truncation. */
12765 static bool
12766 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12768 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12769 machine_mode truncated = rsp->truncated_to_mode;
12771 if (truncated == 0
12772 || rsp->truncation_label < label_tick_ebb_start)
12773 return false;
12774 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12775 return true;
12776 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12777 return true;
12778 return false;
12781 /* If X is a hard reg or a subreg record the mode that the register is
12782 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12783 to turn a truncate into a subreg using this information. Return true
12784 if traversing X is complete. */
12786 static bool
12787 record_truncated_value (rtx x)
12789 machine_mode truncated_mode;
12790 reg_stat_type *rsp;
12792 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12794 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12795 truncated_mode = GET_MODE (x);
12797 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12798 return true;
12800 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12801 return true;
12803 x = SUBREG_REG (x);
12805 /* ??? For hard-regs we now record everything. We might be able to
12806 optimize this using last_set_mode. */
12807 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12808 truncated_mode = GET_MODE (x);
12809 else
12810 return false;
12812 rsp = &reg_stat[REGNO (x)];
12813 if (rsp->truncated_to_mode == 0
12814 || rsp->truncation_label < label_tick_ebb_start
12815 || (GET_MODE_SIZE (truncated_mode)
12816 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12818 rsp->truncated_to_mode = truncated_mode;
12819 rsp->truncation_label = label_tick;
12822 return true;
12825 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12826 the modes they are used in. This can help truning TRUNCATEs into
12827 SUBREGs. */
12829 static void
12830 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12832 subrtx_var_iterator::array_type array;
12833 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12834 if (record_truncated_value (*iter))
12835 iter.skip_subrtxes ();
12838 /* Scan X for promoted SUBREGs. For each one found,
12839 note what it implies to the registers used in it. */
12841 static void
12842 check_promoted_subreg (rtx_insn *insn, rtx x)
12844 if (GET_CODE (x) == SUBREG
12845 && SUBREG_PROMOTED_VAR_P (x)
12846 && REG_P (SUBREG_REG (x)))
12847 record_promoted_value (insn, x);
12848 else
12850 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12851 int i, j;
12853 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12854 switch (format[i])
12856 case 'e':
12857 check_promoted_subreg (insn, XEXP (x, i));
12858 break;
12859 case 'V':
12860 case 'E':
12861 if (XVEC (x, i) != 0)
12862 for (j = 0; j < XVECLEN (x, i); j++)
12863 check_promoted_subreg (insn, XVECEXP (x, i, j));
12864 break;
12869 /* Verify that all the registers and memory references mentioned in *LOC are
12870 still valid. *LOC was part of a value set in INSN when label_tick was
12871 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12872 the invalid references with (clobber (const_int 0)) and return 1. This
12873 replacement is useful because we often can get useful information about
12874 the form of a value (e.g., if it was produced by a shift that always
12875 produces -1 or 0) even though we don't know exactly what registers it
12876 was produced from. */
12878 static int
12879 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12881 rtx x = *loc;
12882 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12883 int len = GET_RTX_LENGTH (GET_CODE (x));
12884 int i, j;
12886 if (REG_P (x))
12888 unsigned int regno = REGNO (x);
12889 unsigned int endregno = END_REGNO (x);
12890 unsigned int j;
12892 for (j = regno; j < endregno; j++)
12894 reg_stat_type *rsp = &reg_stat[j];
12895 if (rsp->last_set_invalid
12896 /* If this is a pseudo-register that was only set once and not
12897 live at the beginning of the function, it is always valid. */
12898 || (! (regno >= FIRST_PSEUDO_REGISTER
12899 && regno < reg_n_sets_max
12900 && REG_N_SETS (regno) == 1
12901 && (!REGNO_REG_SET_P
12902 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12903 regno)))
12904 && rsp->last_set_label > tick))
12906 if (replace)
12907 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12908 return replace;
12912 return 1;
12914 /* If this is a memory reference, make sure that there were no stores after
12915 it that might have clobbered the value. We don't have alias info, so we
12916 assume any store invalidates it. Moreover, we only have local UIDs, so
12917 we also assume that there were stores in the intervening basic blocks. */
12918 else if (MEM_P (x) && !MEM_READONLY_P (x)
12919 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12921 if (replace)
12922 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12923 return replace;
12926 for (i = 0; i < len; i++)
12928 if (fmt[i] == 'e')
12930 /* Check for identical subexpressions. If x contains
12931 identical subexpression we only have to traverse one of
12932 them. */
12933 if (i == 1 && ARITHMETIC_P (x))
12935 /* Note that at this point x0 has already been checked
12936 and found valid. */
12937 rtx x0 = XEXP (x, 0);
12938 rtx x1 = XEXP (x, 1);
12940 /* If x0 and x1 are identical then x is also valid. */
12941 if (x0 == x1)
12942 return 1;
12944 /* If x1 is identical to a subexpression of x0 then
12945 while checking x0, x1 has already been checked. Thus
12946 it is valid and so as x. */
12947 if (ARITHMETIC_P (x0)
12948 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12949 return 1;
12951 /* If x0 is identical to a subexpression of x1 then x is
12952 valid iff the rest of x1 is valid. */
12953 if (ARITHMETIC_P (x1)
12954 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12955 return
12956 get_last_value_validate (&XEXP (x1,
12957 x0 == XEXP (x1, 0) ? 1 : 0),
12958 insn, tick, replace);
12961 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12962 replace) == 0)
12963 return 0;
12965 else if (fmt[i] == 'E')
12966 for (j = 0; j < XVECLEN (x, i); j++)
12967 if (get_last_value_validate (&XVECEXP (x, i, j),
12968 insn, tick, replace) == 0)
12969 return 0;
12972 /* If we haven't found a reason for it to be invalid, it is valid. */
12973 return 1;
12976 /* Get the last value assigned to X, if known. Some registers
12977 in the value may be replaced with (clobber (const_int 0)) if their value
12978 is known longer known reliably. */
12980 static rtx
12981 get_last_value (const_rtx x)
12983 unsigned int regno;
12984 rtx value;
12985 reg_stat_type *rsp;
12987 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12988 then convert it to the desired mode. If this is a paradoxical SUBREG,
12989 we cannot predict what values the "extra" bits might have. */
12990 if (GET_CODE (x) == SUBREG
12991 && subreg_lowpart_p (x)
12992 && !paradoxical_subreg_p (x)
12993 && (value = get_last_value (SUBREG_REG (x))) != 0)
12994 return gen_lowpart (GET_MODE (x), value);
12996 if (!REG_P (x))
12997 return 0;
12999 regno = REGNO (x);
13000 rsp = &reg_stat[regno];
13001 value = rsp->last_set_value;
13003 /* If we don't have a value, or if it isn't for this basic block and
13004 it's either a hard register, set more than once, or it's a live
13005 at the beginning of the function, return 0.
13007 Because if it's not live at the beginning of the function then the reg
13008 is always set before being used (is never used without being set).
13009 And, if it's set only once, and it's always set before use, then all
13010 uses must have the same last value, even if it's not from this basic
13011 block. */
13013 if (value == 0
13014 || (rsp->last_set_label < label_tick_ebb_start
13015 && (regno < FIRST_PSEUDO_REGISTER
13016 || regno >= reg_n_sets_max
13017 || REG_N_SETS (regno) != 1
13018 || REGNO_REG_SET_P
13019 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13020 return 0;
13022 /* If the value was set in a later insn than the ones we are processing,
13023 we can't use it even if the register was only set once. */
13024 if (rsp->last_set_label == label_tick
13025 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13026 return 0;
13028 /* If the value has all its registers valid, return it. */
13029 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13030 return value;
13032 /* Otherwise, make a copy and replace any invalid register with
13033 (clobber (const_int 0)). If that fails for some reason, return 0. */
13035 value = copy_rtx (value);
13036 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13037 return value;
13039 return 0;
13042 /* Return nonzero if expression X refers to a REG or to memory
13043 that is set in an instruction more recent than FROM_LUID. */
13045 static int
13046 use_crosses_set_p (const_rtx x, int from_luid)
13048 const char *fmt;
13049 int i;
13050 enum rtx_code code = GET_CODE (x);
13052 if (code == REG)
13054 unsigned int regno = REGNO (x);
13055 unsigned endreg = END_REGNO (x);
13057 #ifdef PUSH_ROUNDING
13058 /* Don't allow uses of the stack pointer to be moved,
13059 because we don't know whether the move crosses a push insn. */
13060 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13061 return 1;
13062 #endif
13063 for (; regno < endreg; regno++)
13065 reg_stat_type *rsp = &reg_stat[regno];
13066 if (rsp->last_set
13067 && rsp->last_set_label == label_tick
13068 && DF_INSN_LUID (rsp->last_set) > from_luid)
13069 return 1;
13071 return 0;
13074 if (code == MEM && mem_last_set > from_luid)
13075 return 1;
13077 fmt = GET_RTX_FORMAT (code);
13079 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13081 if (fmt[i] == 'E')
13083 int j;
13084 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13085 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13086 return 1;
13088 else if (fmt[i] == 'e'
13089 && use_crosses_set_p (XEXP (x, i), from_luid))
13090 return 1;
13092 return 0;
13095 /* Define three variables used for communication between the following
13096 routines. */
13098 static unsigned int reg_dead_regno, reg_dead_endregno;
13099 static int reg_dead_flag;
13101 /* Function called via note_stores from reg_dead_at_p.
13103 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13104 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13106 static void
13107 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13109 unsigned int regno, endregno;
13111 if (!REG_P (dest))
13112 return;
13114 regno = REGNO (dest);
13115 endregno = END_REGNO (dest);
13116 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13117 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13120 /* Return nonzero if REG is known to be dead at INSN.
13122 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13123 referencing REG, it is dead. If we hit a SET referencing REG, it is
13124 live. Otherwise, see if it is live or dead at the start of the basic
13125 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13126 must be assumed to be always live. */
13128 static int
13129 reg_dead_at_p (rtx reg, rtx_insn *insn)
13131 basic_block block;
13132 unsigned int i;
13134 /* Set variables for reg_dead_at_p_1. */
13135 reg_dead_regno = REGNO (reg);
13136 reg_dead_endregno = END_REGNO (reg);
13138 reg_dead_flag = 0;
13140 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13141 we allow the machine description to decide whether use-and-clobber
13142 patterns are OK. */
13143 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13145 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13146 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13147 return 0;
13150 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13151 beginning of basic block. */
13152 block = BLOCK_FOR_INSN (insn);
13153 for (;;)
13155 if (INSN_P (insn))
13157 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13158 return 1;
13160 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13161 if (reg_dead_flag)
13162 return reg_dead_flag == 1 ? 1 : 0;
13164 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13165 return 1;
13168 if (insn == BB_HEAD (block))
13169 break;
13171 insn = PREV_INSN (insn);
13174 /* Look at live-in sets for the basic block that we were in. */
13175 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13176 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13177 return 0;
13179 return 1;
13182 /* Note hard registers in X that are used. */
13184 static void
13185 mark_used_regs_combine (rtx x)
13187 RTX_CODE code = GET_CODE (x);
13188 unsigned int regno;
13189 int i;
13191 switch (code)
13193 case LABEL_REF:
13194 case SYMBOL_REF:
13195 case CONST:
13196 CASE_CONST_ANY:
13197 case PC:
13198 case ADDR_VEC:
13199 case ADDR_DIFF_VEC:
13200 case ASM_INPUT:
13201 #ifdef HAVE_cc0
13202 /* CC0 must die in the insn after it is set, so we don't need to take
13203 special note of it here. */
13204 case CC0:
13205 #endif
13206 return;
13208 case CLOBBER:
13209 /* If we are clobbering a MEM, mark any hard registers inside the
13210 address as used. */
13211 if (MEM_P (XEXP (x, 0)))
13212 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13213 return;
13215 case REG:
13216 regno = REGNO (x);
13217 /* A hard reg in a wide mode may really be multiple registers.
13218 If so, mark all of them just like the first. */
13219 if (regno < FIRST_PSEUDO_REGISTER)
13221 /* None of this applies to the stack, frame or arg pointers. */
13222 if (regno == STACK_POINTER_REGNUM
13223 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13224 || regno == HARD_FRAME_POINTER_REGNUM
13225 #endif
13226 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13227 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13228 #endif
13229 || regno == FRAME_POINTER_REGNUM)
13230 return;
13232 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13234 return;
13236 case SET:
13238 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13239 the address. */
13240 rtx testreg = SET_DEST (x);
13242 while (GET_CODE (testreg) == SUBREG
13243 || GET_CODE (testreg) == ZERO_EXTRACT
13244 || GET_CODE (testreg) == STRICT_LOW_PART)
13245 testreg = XEXP (testreg, 0);
13247 if (MEM_P (testreg))
13248 mark_used_regs_combine (XEXP (testreg, 0));
13250 mark_used_regs_combine (SET_SRC (x));
13252 return;
13254 default:
13255 break;
13258 /* Recursively scan the operands of this expression. */
13261 const char *fmt = GET_RTX_FORMAT (code);
13263 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13265 if (fmt[i] == 'e')
13266 mark_used_regs_combine (XEXP (x, i));
13267 else if (fmt[i] == 'E')
13269 int j;
13271 for (j = 0; j < XVECLEN (x, i); j++)
13272 mark_used_regs_combine (XVECEXP (x, i, j));
13278 /* Remove register number REGNO from the dead registers list of INSN.
13280 Return the note used to record the death, if there was one. */
13283 remove_death (unsigned int regno, rtx_insn *insn)
13285 rtx note = find_regno_note (insn, REG_DEAD, regno);
13287 if (note)
13288 remove_note (insn, note);
13290 return note;
13293 /* For each register (hardware or pseudo) used within expression X, if its
13294 death is in an instruction with luid between FROM_LUID (inclusive) and
13295 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13296 list headed by PNOTES.
13298 That said, don't move registers killed by maybe_kill_insn.
13300 This is done when X is being merged by combination into TO_INSN. These
13301 notes will then be distributed as needed. */
13303 static void
13304 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13305 rtx *pnotes)
13307 const char *fmt;
13308 int len, i;
13309 enum rtx_code code = GET_CODE (x);
13311 if (code == REG)
13313 unsigned int regno = REGNO (x);
13314 rtx_insn *where_dead = reg_stat[regno].last_death;
13316 /* Don't move the register if it gets killed in between from and to. */
13317 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13318 && ! reg_referenced_p (x, maybe_kill_insn))
13319 return;
13321 if (where_dead
13322 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13323 && DF_INSN_LUID (where_dead) >= from_luid
13324 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13326 rtx note = remove_death (regno, where_dead);
13328 /* It is possible for the call above to return 0. This can occur
13329 when last_death points to I2 or I1 that we combined with.
13330 In that case make a new note.
13332 We must also check for the case where X is a hard register
13333 and NOTE is a death note for a range of hard registers
13334 including X. In that case, we must put REG_DEAD notes for
13335 the remaining registers in place of NOTE. */
13337 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13338 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13339 > GET_MODE_SIZE (GET_MODE (x))))
13341 unsigned int deadregno = REGNO (XEXP (note, 0));
13342 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13343 unsigned int ourend = END_HARD_REGNO (x);
13344 unsigned int i;
13346 for (i = deadregno; i < deadend; i++)
13347 if (i < regno || i >= ourend)
13348 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13351 /* If we didn't find any note, or if we found a REG_DEAD note that
13352 covers only part of the given reg, and we have a multi-reg hard
13353 register, then to be safe we must check for REG_DEAD notes
13354 for each register other than the first. They could have
13355 their own REG_DEAD notes lying around. */
13356 else if ((note == 0
13357 || (note != 0
13358 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13359 < GET_MODE_SIZE (GET_MODE (x)))))
13360 && regno < FIRST_PSEUDO_REGISTER
13361 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13363 unsigned int ourend = END_HARD_REGNO (x);
13364 unsigned int i, offset;
13365 rtx oldnotes = 0;
13367 if (note)
13368 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13369 else
13370 offset = 1;
13372 for (i = regno + offset; i < ourend; i++)
13373 move_deaths (regno_reg_rtx[i],
13374 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13377 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13379 XEXP (note, 1) = *pnotes;
13380 *pnotes = note;
13382 else
13383 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13386 return;
13389 else if (GET_CODE (x) == SET)
13391 rtx dest = SET_DEST (x);
13393 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13395 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13396 that accesses one word of a multi-word item, some
13397 piece of everything register in the expression is used by
13398 this insn, so remove any old death. */
13399 /* ??? So why do we test for equality of the sizes? */
13401 if (GET_CODE (dest) == ZERO_EXTRACT
13402 || GET_CODE (dest) == STRICT_LOW_PART
13403 || (GET_CODE (dest) == SUBREG
13404 && (((GET_MODE_SIZE (GET_MODE (dest))
13405 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13406 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13407 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13409 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13410 return;
13413 /* If this is some other SUBREG, we know it replaces the entire
13414 value, so use that as the destination. */
13415 if (GET_CODE (dest) == SUBREG)
13416 dest = SUBREG_REG (dest);
13418 /* If this is a MEM, adjust deaths of anything used in the address.
13419 For a REG (the only other possibility), the entire value is
13420 being replaced so the old value is not used in this insn. */
13422 if (MEM_P (dest))
13423 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13424 to_insn, pnotes);
13425 return;
13428 else if (GET_CODE (x) == CLOBBER)
13429 return;
13431 len = GET_RTX_LENGTH (code);
13432 fmt = GET_RTX_FORMAT (code);
13434 for (i = 0; i < len; i++)
13436 if (fmt[i] == 'E')
13438 int j;
13439 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13440 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13441 to_insn, pnotes);
13443 else if (fmt[i] == 'e')
13444 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13448 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13449 pattern of an insn. X must be a REG. */
13451 static int
13452 reg_bitfield_target_p (rtx x, rtx body)
13454 int i;
13456 if (GET_CODE (body) == SET)
13458 rtx dest = SET_DEST (body);
13459 rtx target;
13460 unsigned int regno, tregno, endregno, endtregno;
13462 if (GET_CODE (dest) == ZERO_EXTRACT)
13463 target = XEXP (dest, 0);
13464 else if (GET_CODE (dest) == STRICT_LOW_PART)
13465 target = SUBREG_REG (XEXP (dest, 0));
13466 else
13467 return 0;
13469 if (GET_CODE (target) == SUBREG)
13470 target = SUBREG_REG (target);
13472 if (!REG_P (target))
13473 return 0;
13475 tregno = REGNO (target), regno = REGNO (x);
13476 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13477 return target == x;
13479 endtregno = end_hard_regno (GET_MODE (target), tregno);
13480 endregno = end_hard_regno (GET_MODE (x), regno);
13482 return endregno > tregno && regno < endtregno;
13485 else if (GET_CODE (body) == PARALLEL)
13486 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13487 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13488 return 1;
13490 return 0;
13493 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13494 as appropriate. I3 and I2 are the insns resulting from the combination
13495 insns including FROM (I2 may be zero).
13497 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13498 not need REG_DEAD notes because they are being substituted for. This
13499 saves searching in the most common cases.
13501 Each note in the list is either ignored or placed on some insns, depending
13502 on the type of note. */
13504 static void
13505 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13506 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13508 rtx note, next_note;
13509 rtx tem_note;
13510 rtx_insn *tem_insn;
13512 for (note = notes; note; note = next_note)
13514 rtx_insn *place = 0, *place2 = 0;
13516 next_note = XEXP (note, 1);
13517 switch (REG_NOTE_KIND (note))
13519 case REG_BR_PROB:
13520 case REG_BR_PRED:
13521 /* Doesn't matter much where we put this, as long as it's somewhere.
13522 It is preferable to keep these notes on branches, which is most
13523 likely to be i3. */
13524 place = i3;
13525 break;
13527 case REG_NON_LOCAL_GOTO:
13528 if (JUMP_P (i3))
13529 place = i3;
13530 else
13532 gcc_assert (i2 && JUMP_P (i2));
13533 place = i2;
13535 break;
13537 case REG_EH_REGION:
13538 /* These notes must remain with the call or trapping instruction. */
13539 if (CALL_P (i3))
13540 place = i3;
13541 else if (i2 && CALL_P (i2))
13542 place = i2;
13543 else
13545 gcc_assert (cfun->can_throw_non_call_exceptions);
13546 if (may_trap_p (i3))
13547 place = i3;
13548 else if (i2 && may_trap_p (i2))
13549 place = i2;
13550 /* ??? Otherwise assume we've combined things such that we
13551 can now prove that the instructions can't trap. Drop the
13552 note in this case. */
13554 break;
13556 case REG_ARGS_SIZE:
13557 /* ??? How to distribute between i3-i1. Assume i3 contains the
13558 entire adjustment. Assert i3 contains at least some adjust. */
13559 if (!noop_move_p (i3))
13561 int old_size, args_size = INTVAL (XEXP (note, 0));
13562 /* fixup_args_size_notes looks at REG_NORETURN note,
13563 so ensure the note is placed there first. */
13564 if (CALL_P (i3))
13566 rtx *np;
13567 for (np = &next_note; *np; np = &XEXP (*np, 1))
13568 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13570 rtx n = *np;
13571 *np = XEXP (n, 1);
13572 XEXP (n, 1) = REG_NOTES (i3);
13573 REG_NOTES (i3) = n;
13574 break;
13577 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13578 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13579 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13580 gcc_assert (old_size != args_size
13581 || (CALL_P (i3)
13582 && !ACCUMULATE_OUTGOING_ARGS
13583 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13585 break;
13587 case REG_NORETURN:
13588 case REG_SETJMP:
13589 case REG_TM:
13590 case REG_CALL_DECL:
13591 /* These notes must remain with the call. It should not be
13592 possible for both I2 and I3 to be a call. */
13593 if (CALL_P (i3))
13594 place = i3;
13595 else
13597 gcc_assert (i2 && CALL_P (i2));
13598 place = i2;
13600 break;
13602 case REG_UNUSED:
13603 /* Any clobbers for i3 may still exist, and so we must process
13604 REG_UNUSED notes from that insn.
13606 Any clobbers from i2 or i1 can only exist if they were added by
13607 recog_for_combine. In that case, recog_for_combine created the
13608 necessary REG_UNUSED notes. Trying to keep any original
13609 REG_UNUSED notes from these insns can cause incorrect output
13610 if it is for the same register as the original i3 dest.
13611 In that case, we will notice that the register is set in i3,
13612 and then add a REG_UNUSED note for the destination of i3, which
13613 is wrong. However, it is possible to have REG_UNUSED notes from
13614 i2 or i1 for register which were both used and clobbered, so
13615 we keep notes from i2 or i1 if they will turn into REG_DEAD
13616 notes. */
13618 /* If this register is set or clobbered in I3, put the note there
13619 unless there is one already. */
13620 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13622 if (from_insn != i3)
13623 break;
13625 if (! (REG_P (XEXP (note, 0))
13626 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13627 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13628 place = i3;
13630 /* Otherwise, if this register is used by I3, then this register
13631 now dies here, so we must put a REG_DEAD note here unless there
13632 is one already. */
13633 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13634 && ! (REG_P (XEXP (note, 0))
13635 ? find_regno_note (i3, REG_DEAD,
13636 REGNO (XEXP (note, 0)))
13637 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13639 PUT_REG_NOTE_KIND (note, REG_DEAD);
13640 place = i3;
13642 break;
13644 case REG_EQUAL:
13645 case REG_EQUIV:
13646 case REG_NOALIAS:
13647 /* These notes say something about results of an insn. We can
13648 only support them if they used to be on I3 in which case they
13649 remain on I3. Otherwise they are ignored.
13651 If the note refers to an expression that is not a constant, we
13652 must also ignore the note since we cannot tell whether the
13653 equivalence is still true. It might be possible to do
13654 slightly better than this (we only have a problem if I2DEST
13655 or I1DEST is present in the expression), but it doesn't
13656 seem worth the trouble. */
13658 if (from_insn == i3
13659 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13660 place = i3;
13661 break;
13663 case REG_INC:
13664 /* These notes say something about how a register is used. They must
13665 be present on any use of the register in I2 or I3. */
13666 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13667 place = i3;
13669 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13671 if (place)
13672 place2 = i2;
13673 else
13674 place = i2;
13676 break;
13678 case REG_LABEL_TARGET:
13679 case REG_LABEL_OPERAND:
13680 /* This can show up in several ways -- either directly in the
13681 pattern, or hidden off in the constant pool with (or without?)
13682 a REG_EQUAL note. */
13683 /* ??? Ignore the without-reg_equal-note problem for now. */
13684 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13685 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13686 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13687 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13688 place = i3;
13690 if (i2
13691 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13692 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13693 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13694 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13696 if (place)
13697 place2 = i2;
13698 else
13699 place = i2;
13702 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13703 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13704 there. */
13705 if (place && JUMP_P (place)
13706 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13707 && (JUMP_LABEL (place) == NULL
13708 || JUMP_LABEL (place) == XEXP (note, 0)))
13710 rtx label = JUMP_LABEL (place);
13712 if (!label)
13713 JUMP_LABEL (place) = XEXP (note, 0);
13714 else if (LABEL_P (label))
13715 LABEL_NUSES (label)--;
13718 if (place2 && JUMP_P (place2)
13719 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13720 && (JUMP_LABEL (place2) == NULL
13721 || JUMP_LABEL (place2) == XEXP (note, 0)))
13723 rtx label = JUMP_LABEL (place2);
13725 if (!label)
13726 JUMP_LABEL (place2) = XEXP (note, 0);
13727 else if (LABEL_P (label))
13728 LABEL_NUSES (label)--;
13729 place2 = 0;
13731 break;
13733 case REG_NONNEG:
13734 /* This note says something about the value of a register prior
13735 to the execution of an insn. It is too much trouble to see
13736 if the note is still correct in all situations. It is better
13737 to simply delete it. */
13738 break;
13740 case REG_DEAD:
13741 /* If we replaced the right hand side of FROM_INSN with a
13742 REG_EQUAL note, the original use of the dying register
13743 will not have been combined into I3 and I2. In such cases,
13744 FROM_INSN is guaranteed to be the first of the combined
13745 instructions, so we simply need to search back before
13746 FROM_INSN for the previous use or set of this register,
13747 then alter the notes there appropriately.
13749 If the register is used as an input in I3, it dies there.
13750 Similarly for I2, if it is nonzero and adjacent to I3.
13752 If the register is not used as an input in either I3 or I2
13753 and it is not one of the registers we were supposed to eliminate,
13754 there are two possibilities. We might have a non-adjacent I2
13755 or we might have somehow eliminated an additional register
13756 from a computation. For example, we might have had A & B where
13757 we discover that B will always be zero. In this case we will
13758 eliminate the reference to A.
13760 In both cases, we must search to see if we can find a previous
13761 use of A and put the death note there. */
13763 if (from_insn
13764 && from_insn == i2mod
13765 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13766 tem_insn = from_insn;
13767 else
13769 if (from_insn
13770 && CALL_P (from_insn)
13771 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13772 place = from_insn;
13773 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13774 place = i3;
13775 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13776 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13777 place = i2;
13778 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13779 && !(i2mod
13780 && reg_overlap_mentioned_p (XEXP (note, 0),
13781 i2mod_old_rhs)))
13782 || rtx_equal_p (XEXP (note, 0), elim_i1)
13783 || rtx_equal_p (XEXP (note, 0), elim_i0))
13784 break;
13785 tem_insn = i3;
13786 /* If the new I2 sets the same register that is marked dead
13787 in the note, the note now should not be put on I2, as the
13788 note refers to a previous incarnation of the reg. */
13789 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
13790 tem_insn = i2;
13793 if (place == 0)
13795 basic_block bb = this_basic_block;
13797 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13799 if (!NONDEBUG_INSN_P (tem_insn))
13801 if (tem_insn == BB_HEAD (bb))
13802 break;
13803 continue;
13806 /* If the register is being set at TEM_INSN, see if that is all
13807 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13808 into a REG_UNUSED note instead. Don't delete sets to
13809 global register vars. */
13810 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13811 || !global_regs[REGNO (XEXP (note, 0))])
13812 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13814 rtx set = single_set (tem_insn);
13815 rtx inner_dest = 0;
13816 #ifdef HAVE_cc0
13817 rtx_insn *cc0_setter = NULL;
13818 #endif
13820 if (set != 0)
13821 for (inner_dest = SET_DEST (set);
13822 (GET_CODE (inner_dest) == STRICT_LOW_PART
13823 || GET_CODE (inner_dest) == SUBREG
13824 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13825 inner_dest = XEXP (inner_dest, 0))
13828 /* Verify that it was the set, and not a clobber that
13829 modified the register.
13831 CC0 targets must be careful to maintain setter/user
13832 pairs. If we cannot delete the setter due to side
13833 effects, mark the user with an UNUSED note instead
13834 of deleting it. */
13836 if (set != 0 && ! side_effects_p (SET_SRC (set))
13837 && rtx_equal_p (XEXP (note, 0), inner_dest)
13838 #ifdef HAVE_cc0
13839 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13840 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13841 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13842 #endif
13845 /* Move the notes and links of TEM_INSN elsewhere.
13846 This might delete other dead insns recursively.
13847 First set the pattern to something that won't use
13848 any register. */
13849 rtx old_notes = REG_NOTES (tem_insn);
13851 PATTERN (tem_insn) = pc_rtx;
13852 REG_NOTES (tem_insn) = NULL;
13854 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13855 NULL_RTX, NULL_RTX, NULL_RTX);
13856 distribute_links (LOG_LINKS (tem_insn));
13858 SET_INSN_DELETED (tem_insn);
13859 if (tem_insn == i2)
13860 i2 = NULL;
13862 #ifdef HAVE_cc0
13863 /* Delete the setter too. */
13864 if (cc0_setter)
13866 PATTERN (cc0_setter) = pc_rtx;
13867 old_notes = REG_NOTES (cc0_setter);
13868 REG_NOTES (cc0_setter) = NULL;
13870 distribute_notes (old_notes, cc0_setter,
13871 cc0_setter, NULL,
13872 NULL_RTX, NULL_RTX, NULL_RTX);
13873 distribute_links (LOG_LINKS (cc0_setter));
13875 SET_INSN_DELETED (cc0_setter);
13876 if (cc0_setter == i2)
13877 i2 = NULL;
13879 #endif
13881 else
13883 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13885 /* If there isn't already a REG_UNUSED note, put one
13886 here. Do not place a REG_DEAD note, even if
13887 the register is also used here; that would not
13888 match the algorithm used in lifetime analysis
13889 and can cause the consistency check in the
13890 scheduler to fail. */
13891 if (! find_regno_note (tem_insn, REG_UNUSED,
13892 REGNO (XEXP (note, 0))))
13893 place = tem_insn;
13894 break;
13897 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13898 || (CALL_P (tem_insn)
13899 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13901 place = tem_insn;
13903 /* If we are doing a 3->2 combination, and we have a
13904 register which formerly died in i3 and was not used
13905 by i2, which now no longer dies in i3 and is used in
13906 i2 but does not die in i2, and place is between i2
13907 and i3, then we may need to move a link from place to
13908 i2. */
13909 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13910 && from_insn
13911 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13912 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13914 struct insn_link *links = LOG_LINKS (place);
13915 LOG_LINKS (place) = NULL;
13916 distribute_links (links);
13918 break;
13921 if (tem_insn == BB_HEAD (bb))
13922 break;
13927 /* If the register is set or already dead at PLACE, we needn't do
13928 anything with this note if it is still a REG_DEAD note.
13929 We check here if it is set at all, not if is it totally replaced,
13930 which is what `dead_or_set_p' checks, so also check for it being
13931 set partially. */
13933 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13935 unsigned int regno = REGNO (XEXP (note, 0));
13936 reg_stat_type *rsp = &reg_stat[regno];
13938 if (dead_or_set_p (place, XEXP (note, 0))
13939 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13941 /* Unless the register previously died in PLACE, clear
13942 last_death. [I no longer understand why this is
13943 being done.] */
13944 if (rsp->last_death != place)
13945 rsp->last_death = 0;
13946 place = 0;
13948 else
13949 rsp->last_death = place;
13951 /* If this is a death note for a hard reg that is occupying
13952 multiple registers, ensure that we are still using all
13953 parts of the object. If we find a piece of the object
13954 that is unused, we must arrange for an appropriate REG_DEAD
13955 note to be added for it. However, we can't just emit a USE
13956 and tag the note to it, since the register might actually
13957 be dead; so we recourse, and the recursive call then finds
13958 the previous insn that used this register. */
13960 if (place && regno < FIRST_PSEUDO_REGISTER
13961 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13963 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13964 bool all_used = true;
13965 unsigned int i;
13967 for (i = regno; i < endregno; i++)
13968 if ((! refers_to_regno_p (i, PATTERN (place))
13969 && ! find_regno_fusage (place, USE, i))
13970 || dead_or_set_regno_p (place, i))
13972 all_used = false;
13973 break;
13976 if (! all_used)
13978 /* Put only REG_DEAD notes for pieces that are
13979 not already dead or set. */
13981 for (i = regno; i < endregno;
13982 i += hard_regno_nregs[i][reg_raw_mode[i]])
13984 rtx piece = regno_reg_rtx[i];
13985 basic_block bb = this_basic_block;
13987 if (! dead_or_set_p (place, piece)
13988 && ! reg_bitfield_target_p (piece,
13989 PATTERN (place)))
13991 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13992 NULL_RTX);
13994 distribute_notes (new_note, place, place,
13995 NULL, NULL_RTX, NULL_RTX,
13996 NULL_RTX);
13998 else if (! refers_to_regno_p (i, PATTERN (place))
13999 && ! find_regno_fusage (place, USE, i))
14000 for (tem_insn = PREV_INSN (place); ;
14001 tem_insn = PREV_INSN (tem_insn))
14003 if (!NONDEBUG_INSN_P (tem_insn))
14005 if (tem_insn == BB_HEAD (bb))
14006 break;
14007 continue;
14009 if (dead_or_set_p (tem_insn, piece)
14010 || reg_bitfield_target_p (piece,
14011 PATTERN (tem_insn)))
14013 add_reg_note (tem_insn, REG_UNUSED, piece);
14014 break;
14019 place = 0;
14023 break;
14025 default:
14026 /* Any other notes should not be present at this point in the
14027 compilation. */
14028 gcc_unreachable ();
14031 if (place)
14033 XEXP (note, 1) = REG_NOTES (place);
14034 REG_NOTES (place) = note;
14037 if (place2)
14038 add_shallow_copy_of_reg_note (place2, note);
14042 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14043 I3, I2, and I1 to new locations. This is also called to add a link
14044 pointing at I3 when I3's destination is changed. */
14046 static void
14047 distribute_links (struct insn_link *links)
14049 struct insn_link *link, *next_link;
14051 for (link = links; link; link = next_link)
14053 rtx_insn *place = 0;
14054 rtx_insn *insn;
14055 rtx set, reg;
14057 next_link = link->next;
14059 /* If the insn that this link points to is a NOTE, ignore it. */
14060 if (NOTE_P (link->insn))
14061 continue;
14063 set = 0;
14064 rtx pat = PATTERN (link->insn);
14065 if (GET_CODE (pat) == SET)
14066 set = pat;
14067 else if (GET_CODE (pat) == PARALLEL)
14069 int i;
14070 for (i = 0; i < XVECLEN (pat, 0); i++)
14072 set = XVECEXP (pat, 0, i);
14073 if (GET_CODE (set) != SET)
14074 continue;
14076 reg = SET_DEST (set);
14077 while (GET_CODE (reg) == ZERO_EXTRACT
14078 || GET_CODE (reg) == STRICT_LOW_PART
14079 || GET_CODE (reg) == SUBREG)
14080 reg = XEXP (reg, 0);
14082 if (!REG_P (reg))
14083 continue;
14085 if (REGNO (reg) == link->regno)
14086 break;
14088 if (i == XVECLEN (pat, 0))
14089 continue;
14091 else
14092 continue;
14094 reg = SET_DEST (set);
14096 while (GET_CODE (reg) == ZERO_EXTRACT
14097 || GET_CODE (reg) == STRICT_LOW_PART
14098 || GET_CODE (reg) == SUBREG)
14099 reg = XEXP (reg, 0);
14101 /* A LOG_LINK is defined as being placed on the first insn that uses
14102 a register and points to the insn that sets the register. Start
14103 searching at the next insn after the target of the link and stop
14104 when we reach a set of the register or the end of the basic block.
14106 Note that this correctly handles the link that used to point from
14107 I3 to I2. Also note that not much searching is typically done here
14108 since most links don't point very far away. */
14110 for (insn = NEXT_INSN (link->insn);
14111 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14112 || BB_HEAD (this_basic_block->next_bb) != insn));
14113 insn = NEXT_INSN (insn))
14114 if (DEBUG_INSN_P (insn))
14115 continue;
14116 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14118 if (reg_referenced_p (reg, PATTERN (insn)))
14119 place = insn;
14120 break;
14122 else if (CALL_P (insn)
14123 && find_reg_fusage (insn, USE, reg))
14125 place = insn;
14126 break;
14128 else if (INSN_P (insn) && reg_set_p (reg, insn))
14129 break;
14131 /* If we found a place to put the link, place it there unless there
14132 is already a link to the same insn as LINK at that point. */
14134 if (place)
14136 struct insn_link *link2;
14138 FOR_EACH_LOG_LINK (link2, place)
14139 if (link2->insn == link->insn && link2->regno == link->regno)
14140 break;
14142 if (link2 == NULL)
14144 link->next = LOG_LINKS (place);
14145 LOG_LINKS (place) = link;
14147 /* Set added_links_insn to the earliest insn we added a
14148 link to. */
14149 if (added_links_insn == 0
14150 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14151 added_links_insn = place;
14157 /* Check for any register or memory mentioned in EQUIV that is not
14158 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14159 of EXPR where some registers may have been replaced by constants. */
14161 static bool
14162 unmentioned_reg_p (rtx equiv, rtx expr)
14164 subrtx_iterator::array_type array;
14165 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14167 const_rtx x = *iter;
14168 if ((REG_P (x) || MEM_P (x))
14169 && !reg_mentioned_p (x, expr))
14170 return true;
14172 return false;
14175 DEBUG_FUNCTION void
14176 dump_combine_stats (FILE *file)
14178 fprintf
14179 (file,
14180 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14181 combine_attempts, combine_merges, combine_extras, combine_successes);
14184 void
14185 dump_combine_total_stats (FILE *file)
14187 fprintf
14188 (file,
14189 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14190 total_attempts, total_merges, total_extras, total_successes);
14193 /* Try combining insns through substitution. */
14194 static unsigned int
14195 rest_of_handle_combine (void)
14197 int rebuild_jump_labels_after_combine;
14199 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14200 df_note_add_problem ();
14201 df_analyze ();
14203 regstat_init_n_sets_and_refs ();
14204 reg_n_sets_max = max_reg_num ();
14206 rebuild_jump_labels_after_combine
14207 = combine_instructions (get_insns (), max_reg_num ());
14209 /* Combining insns may have turned an indirect jump into a
14210 direct jump. Rebuild the JUMP_LABEL fields of jumping
14211 instructions. */
14212 if (rebuild_jump_labels_after_combine)
14214 timevar_push (TV_JUMP);
14215 rebuild_jump_labels (get_insns ());
14216 cleanup_cfg (0);
14217 timevar_pop (TV_JUMP);
14220 regstat_free_n_sets_and_refs ();
14221 return 0;
14224 namespace {
14226 const pass_data pass_data_combine =
14228 RTL_PASS, /* type */
14229 "combine", /* name */
14230 OPTGROUP_NONE, /* optinfo_flags */
14231 TV_COMBINE, /* tv_id */
14232 PROP_cfglayout, /* properties_required */
14233 0, /* properties_provided */
14234 0, /* properties_destroyed */
14235 0, /* todo_flags_start */
14236 TODO_df_finish, /* todo_flags_finish */
14239 class pass_combine : public rtl_opt_pass
14241 public:
14242 pass_combine (gcc::context *ctxt)
14243 : rtl_opt_pass (pass_data_combine, ctxt)
14246 /* opt_pass methods: */
14247 virtual bool gate (function *) { return (optimize > 0); }
14248 virtual unsigned int execute (function *)
14250 return rest_of_handle_combine ();
14253 }; // class pass_combine
14255 } // anon namespace
14257 rtl_opt_pass *
14258 make_pass_combine (gcc::context *ctxt)
14260 return new pass_combine (ctxt);