Use lowpart_subreg instead of simplify_gen_subreg
[official-gcc.git] / gcc / combine.c
blob4a92f55c8a0bfb44bb735fb5e1ab73aa7d80a6fe
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 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 "backend.h"
82 #include "predict.h"
83 #include "tree.h"
84 #include "rtl.h"
85 #include "df.h"
86 #include "alias.h"
87 #include "stor-layout.h"
88 #include "tm_p.h"
89 #include "flags.h"
90 #include "regs.h"
91 #include "cfgrtl.h"
92 #include "cfgcleanup.h"
93 #include "insn-config.h"
94 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
95 #include "expmed.h"
96 #include "dojump.h"
97 #include "explow.h"
98 #include "calls.h"
99 #include "emit-rtl.h"
100 #include "varasm.h"
101 #include "stmt.h"
102 #include "expr.h"
103 #include "insn-attr.h"
104 #include "recog.h"
105 #include "diagnostic-core.h"
106 #include "target.h"
107 #include "insn-codes.h"
108 #include "optabs.h"
109 #include "rtlhooks-def.h"
110 #include "params.h"
111 #include "tree-pass.h"
112 #include "valtrack.h"
113 #include "cgraph.h"
114 #include "rtl-iter.h"
116 #ifndef LOAD_EXTEND_OP
117 #define LOAD_EXTEND_OP(M) UNKNOWN
118 #endif
120 /* Number of attempts to combine instructions in this function. */
122 static int combine_attempts;
124 /* Number of attempts that got as far as substitution in this function. */
126 static int combine_merges;
128 /* Number of instructions combined with added SETs in this function. */
130 static int combine_extras;
132 /* Number of instructions combined in this function. */
134 static int combine_successes;
136 /* Totals over entire compilation. */
138 static int total_attempts, total_merges, total_extras, total_successes;
140 /* combine_instructions may try to replace the right hand side of the
141 second instruction with the value of an associated REG_EQUAL note
142 before throwing it at try_combine. That is problematic when there
143 is a REG_DEAD note for a register used in the old right hand side
144 and can cause distribute_notes to do wrong things. This is the
145 second instruction if it has been so modified, null otherwise. */
147 static rtx_insn *i2mod;
149 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
151 static rtx i2mod_old_rhs;
153 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
155 static rtx i2mod_new_rhs;
157 typedef struct reg_stat_struct {
158 /* Record last point of death of (hard or pseudo) register n. */
159 rtx_insn *last_death;
161 /* Record last point of modification of (hard or pseudo) register n. */
162 rtx_insn *last_set;
164 /* The next group of fields allows the recording of the last value assigned
165 to (hard or pseudo) register n. We use this information to see if an
166 operation being processed is redundant given a prior operation performed
167 on the register. For example, an `and' with a constant is redundant if
168 all the zero bits are already known to be turned off.
170 We use an approach similar to that used by cse, but change it in the
171 following ways:
173 (1) We do not want to reinitialize at each label.
174 (2) It is useful, but not critical, to know the actual value assigned
175 to a register. Often just its form is helpful.
177 Therefore, we maintain the following fields:
179 last_set_value the last value assigned
180 last_set_label records the value of label_tick when the
181 register was assigned
182 last_set_table_tick records the value of label_tick when a
183 value using the register is assigned
184 last_set_invalid set to nonzero when it is not valid
185 to use the value of this register in some
186 register's value
188 To understand the usage of these tables, it is important to understand
189 the distinction between the value in last_set_value being valid and
190 the register being validly contained in some other expression in the
191 table.
193 (The next two parameters are out of date).
195 reg_stat[i].last_set_value is valid if it is nonzero, and either
196 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
198 Register I may validly appear in any expression returned for the value
199 of another register if reg_n_sets[i] is 1. It may also appear in the
200 value for register J if reg_stat[j].last_set_invalid is zero, or
201 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
203 If an expression is found in the table containing a register which may
204 not validly appear in an expression, the register is replaced by
205 something that won't match, (clobber (const_int 0)). */
207 /* Record last value assigned to (hard or pseudo) register n. */
209 rtx last_set_value;
211 /* Record the value of label_tick when an expression involving register n
212 is placed in last_set_value. */
214 int last_set_table_tick;
216 /* Record the value of label_tick when the value for register n is placed in
217 last_set_value. */
219 int last_set_label;
221 /* These fields are maintained in parallel with last_set_value and are
222 used to store the mode in which the register was last set, the bits
223 that were known to be zero when it was last set, and the number of
224 sign bits copies it was known to have when it was last set. */
226 unsigned HOST_WIDE_INT last_set_nonzero_bits;
227 char last_set_sign_bit_copies;
228 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
230 /* Set nonzero if references to register n in expressions should not be
231 used. last_set_invalid is set nonzero when this register is being
232 assigned to and last_set_table_tick == label_tick. */
234 char last_set_invalid;
236 /* Some registers that are set more than once and used in more than one
237 basic block are nevertheless always set in similar ways. For example,
238 a QImode register may be loaded from memory in two places on a machine
239 where byte loads zero extend.
241 We record in the following fields if a register has some leading bits
242 that are always equal to the sign bit, and what we know about the
243 nonzero bits of a register, specifically which bits are known to be
244 zero.
246 If an entry is zero, it means that we don't know anything special. */
248 unsigned char sign_bit_copies;
250 unsigned HOST_WIDE_INT nonzero_bits;
252 /* Record the value of the label_tick when the last truncation
253 happened. The field truncated_to_mode is only valid if
254 truncation_label == label_tick. */
256 int truncation_label;
258 /* Record the last truncation seen for this register. If truncation
259 is not a nop to this mode we might be able to save an explicit
260 truncation if we know that value already contains a truncated
261 value. */
263 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
264 } reg_stat_type;
267 static vec<reg_stat_type> reg_stat;
269 /* One plus the highest pseudo for which we track REG_N_SETS.
270 regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
271 but during combine_split_insns new pseudos can be created. As we don't have
272 updated DF information in that case, it is hard to initialize the array
273 after growing. The combiner only cares about REG_N_SETS (regno) == 1,
274 so instead of growing the arrays, just assume all newly created pseudos
275 during combine might be set multiple times. */
277 static unsigned int reg_n_sets_max;
279 /* Record the luid of the last insn that invalidated memory
280 (anything that writes memory, and subroutine calls, but not pushes). */
282 static int mem_last_set;
284 /* Record the luid of the last CALL_INSN
285 so we can tell whether a potential combination crosses any calls. */
287 static int last_call_luid;
289 /* When `subst' is called, this is the insn that is being modified
290 (by combining in a previous insn). The PATTERN of this insn
291 is still the old pattern partially modified and it should not be
292 looked at, but this may be used to examine the successors of the insn
293 to judge whether a simplification is valid. */
295 static rtx_insn *subst_insn;
297 /* This is the lowest LUID that `subst' is currently dealing with.
298 get_last_value will not return a value if the register was set at or
299 after this LUID. If not for this mechanism, we could get confused if
300 I2 or I1 in try_combine were an insn that used the old value of a register
301 to obtain a new value. In that case, we might erroneously get the
302 new value of the register when we wanted the old one. */
304 static int subst_low_luid;
306 /* This contains any hard registers that are used in newpat; reg_dead_at_p
307 must consider all these registers to be always live. */
309 static HARD_REG_SET newpat_used_regs;
311 /* This is an insn to which a LOG_LINKS entry has been added. If this
312 insn is the earlier than I2 or I3, combine should rescan starting at
313 that location. */
315 static rtx_insn *added_links_insn;
317 /* Basic block in which we are performing combines. */
318 static basic_block this_basic_block;
319 static bool optimize_this_for_speed_p;
322 /* Length of the currently allocated uid_insn_cost array. */
324 static int max_uid_known;
326 /* The following array records the insn_rtx_cost for every insn
327 in the instruction stream. */
329 static int *uid_insn_cost;
331 /* The following array records the LOG_LINKS for every insn in the
332 instruction stream as struct insn_link pointers. */
334 struct insn_link {
335 rtx_insn *insn;
336 unsigned int regno;
337 struct insn_link *next;
340 static struct insn_link **uid_log_links;
342 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
343 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
345 #define FOR_EACH_LOG_LINK(L, INSN) \
346 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
348 /* Links for LOG_LINKS are allocated from this obstack. */
350 static struct obstack insn_link_obstack;
352 /* Allocate a link. */
354 static inline struct insn_link *
355 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
357 struct insn_link *l
358 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
359 sizeof (struct insn_link));
360 l->insn = insn;
361 l->regno = regno;
362 l->next = next;
363 return l;
366 /* Incremented for each basic block. */
368 static int label_tick;
370 /* Reset to label_tick for each extended basic block in scanning order. */
372 static int label_tick_ebb_start;
374 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
375 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
377 static machine_mode nonzero_bits_mode;
379 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
380 be safely used. It is zero while computing them and after combine has
381 completed. This former test prevents propagating values based on
382 previously set values, which can be incorrect if a variable is modified
383 in a loop. */
385 static int nonzero_sign_valid;
388 /* Record one modification to rtl structure
389 to be undone by storing old_contents into *where. */
391 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
393 struct undo
395 struct undo *next;
396 enum undo_kind kind;
397 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
398 union { rtx *r; int *i; struct insn_link **l; } where;
401 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
402 num_undo says how many are currently recorded.
404 other_insn is nonzero if we have modified some other insn in the process
405 of working on subst_insn. It must be verified too. */
407 struct undobuf
409 struct undo *undos;
410 struct undo *frees;
411 rtx_insn *other_insn;
414 static struct undobuf undobuf;
416 /* Number of times the pseudo being substituted for
417 was found and replaced. */
419 static int n_occurrences;
421 static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
422 machine_mode,
423 unsigned HOST_WIDE_INT,
424 unsigned HOST_WIDE_INT *);
425 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
426 machine_mode,
427 unsigned int, unsigned int *);
428 static void do_SUBST (rtx *, rtx);
429 static void do_SUBST_INT (int *, int);
430 static void init_reg_last (void);
431 static void setup_incoming_promotions (rtx_insn *);
432 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
433 static int cant_combine_insn_p (rtx_insn *);
434 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
435 rtx_insn *, rtx_insn *, rtx *, rtx *);
436 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
437 static int contains_muldiv (rtx);
438 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
439 int *, rtx_insn *);
440 static void undo_all (void);
441 static void undo_commit (void);
442 static rtx *find_split_point (rtx *, rtx_insn *, bool);
443 static rtx subst (rtx, rtx, rtx, int, int, int);
444 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
445 static rtx simplify_if_then_else (rtx);
446 static rtx simplify_set (rtx);
447 static rtx simplify_logical (rtx);
448 static rtx expand_compound_operation (rtx);
449 static const_rtx expand_field_assignment (const_rtx);
450 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
451 rtx, unsigned HOST_WIDE_INT, int, int, int);
452 static rtx extract_left_shift (rtx, int);
453 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
454 unsigned HOST_WIDE_INT *);
455 static rtx canon_reg_for_combine (rtx, rtx);
456 static rtx force_to_mode (rtx, machine_mode,
457 unsigned HOST_WIDE_INT, int);
458 static rtx if_then_else_cond (rtx, rtx *, rtx *);
459 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
460 static int rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
461 static rtx make_field_assignment (rtx);
462 static rtx apply_distributive_law (rtx);
463 static rtx distribute_and_simplify_rtx (rtx, int);
464 static rtx simplify_and_const_int_1 (machine_mode, rtx,
465 unsigned HOST_WIDE_INT);
466 static rtx simplify_and_const_int (rtx, machine_mode, rtx,
467 unsigned HOST_WIDE_INT);
468 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
469 HOST_WIDE_INT, machine_mode, int *);
470 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
471 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
472 int);
473 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
474 static rtx gen_lowpart_for_combine (machine_mode, rtx);
475 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
476 rtx, rtx *);
477 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
478 static void update_table_tick (rtx);
479 static void record_value_for_reg (rtx, rtx_insn *, rtx);
480 static void check_promoted_subreg (rtx_insn *, rtx);
481 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
482 static void record_dead_and_set_regs (rtx_insn *);
483 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
484 static rtx get_last_value (const_rtx);
485 static int use_crosses_set_p (const_rtx, int);
486 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
487 static int reg_dead_at_p (rtx, rtx_insn *);
488 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
489 static int reg_bitfield_target_p (rtx, rtx);
490 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
491 static void distribute_links (struct insn_link *);
492 static void mark_used_regs_combine (rtx);
493 static void record_promoted_value (rtx_insn *, rtx);
494 static bool unmentioned_reg_p (rtx, rtx);
495 static void record_truncated_values (rtx *, void *);
496 static bool reg_truncated_to_mode (machine_mode, const_rtx);
497 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
500 /* It is not safe to use ordinary gen_lowpart in combine.
501 See comments in gen_lowpart_for_combine. */
502 #undef RTL_HOOKS_GEN_LOWPART
503 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
505 /* Our implementation of gen_lowpart never emits a new pseudo. */
506 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
507 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
509 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
510 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
512 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
513 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
515 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
516 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
518 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
521 /* Convenience wrapper for the canonicalize_comparison target hook.
522 Target hooks cannot use enum rtx_code. */
523 static inline void
524 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
525 bool op0_preserve_value)
527 int code_int = (int)*code;
528 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
529 *code = (enum rtx_code)code_int;
532 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
533 PATTERN can not be split. Otherwise, it returns an insn sequence.
534 This is a wrapper around split_insns which ensures that the
535 reg_stat vector is made larger if the splitter creates a new
536 register. */
538 static rtx_insn *
539 combine_split_insns (rtx pattern, rtx_insn *insn)
541 rtx_insn *ret;
542 unsigned int nregs;
544 ret = split_insns (pattern, insn);
545 nregs = max_reg_num ();
546 if (nregs > reg_stat.length ())
547 reg_stat.safe_grow_cleared (nregs);
548 return ret;
551 /* This is used by find_single_use to locate an rtx in LOC that
552 contains exactly one use of DEST, which is typically either a REG
553 or CC0. It returns a pointer to the innermost rtx expression
554 containing DEST. Appearances of DEST that are being used to
555 totally replace it are not counted. */
557 static rtx *
558 find_single_use_1 (rtx dest, rtx *loc)
560 rtx x = *loc;
561 enum rtx_code code = GET_CODE (x);
562 rtx *result = NULL;
563 rtx *this_result;
564 int i;
565 const char *fmt;
567 switch (code)
569 case CONST:
570 case LABEL_REF:
571 case SYMBOL_REF:
572 CASE_CONST_ANY:
573 case CLOBBER:
574 return 0;
576 case SET:
577 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
578 of a REG that occupies all of the REG, the insn uses DEST if
579 it is mentioned in the destination or the source. Otherwise, we
580 need just check the source. */
581 if (GET_CODE (SET_DEST (x)) != CC0
582 && GET_CODE (SET_DEST (x)) != PC
583 && !REG_P (SET_DEST (x))
584 && ! (GET_CODE (SET_DEST (x)) == SUBREG
585 && REG_P (SUBREG_REG (SET_DEST (x)))
586 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
587 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
588 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
589 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
590 break;
592 return find_single_use_1 (dest, &SET_SRC (x));
594 case MEM:
595 case SUBREG:
596 return find_single_use_1 (dest, &XEXP (x, 0));
598 default:
599 break;
602 /* If it wasn't one of the common cases above, check each expression and
603 vector of this code. Look for a unique usage of DEST. */
605 fmt = GET_RTX_FORMAT (code);
606 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
608 if (fmt[i] == 'e')
610 if (dest == XEXP (x, i)
611 || (REG_P (dest) && REG_P (XEXP (x, i))
612 && REGNO (dest) == REGNO (XEXP (x, i))))
613 this_result = loc;
614 else
615 this_result = find_single_use_1 (dest, &XEXP (x, i));
617 if (result == NULL)
618 result = this_result;
619 else if (this_result)
620 /* Duplicate usage. */
621 return NULL;
623 else if (fmt[i] == 'E')
625 int j;
627 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
629 if (XVECEXP (x, i, j) == dest
630 || (REG_P (dest)
631 && REG_P (XVECEXP (x, i, j))
632 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
633 this_result = loc;
634 else
635 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
637 if (result == NULL)
638 result = this_result;
639 else if (this_result)
640 return NULL;
645 return result;
649 /* See if DEST, produced in INSN, is used only a single time in the
650 sequel. If so, return a pointer to the innermost rtx expression in which
651 it is used.
653 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
655 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
656 care about REG_DEAD notes or LOG_LINKS.
658 Otherwise, we find the single use by finding an insn that has a
659 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
660 only referenced once in that insn, we know that it must be the first
661 and last insn referencing DEST. */
663 static rtx *
664 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
666 basic_block bb;
667 rtx_insn *next;
668 rtx *result;
669 struct insn_link *link;
671 if (dest == cc0_rtx)
673 next = NEXT_INSN (insn);
674 if (next == 0
675 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
676 return 0;
678 result = find_single_use_1 (dest, &PATTERN (next));
679 if (result && ploc)
680 *ploc = next;
681 return result;
684 if (!REG_P (dest))
685 return 0;
687 bb = BLOCK_FOR_INSN (insn);
688 for (next = NEXT_INSN (insn);
689 next && BLOCK_FOR_INSN (next) == bb;
690 next = NEXT_INSN (next))
691 if (INSN_P (next) && dead_or_set_p (next, dest))
693 FOR_EACH_LOG_LINK (link, next)
694 if (link->insn == insn && link->regno == REGNO (dest))
695 break;
697 if (link)
699 result = find_single_use_1 (dest, &PATTERN (next));
700 if (ploc)
701 *ploc = next;
702 return result;
706 return 0;
709 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
710 insn. The substitution can be undone by undo_all. If INTO is already
711 set to NEWVAL, do not record this change. Because computing NEWVAL might
712 also call SUBST, we have to compute it before we put anything into
713 the undo table. */
715 static void
716 do_SUBST (rtx *into, rtx newval)
718 struct undo *buf;
719 rtx oldval = *into;
721 if (oldval == newval)
722 return;
724 /* We'd like to catch as many invalid transformations here as
725 possible. Unfortunately, there are way too many mode changes
726 that are perfectly valid, so we'd waste too much effort for
727 little gain doing the checks here. Focus on catching invalid
728 transformations involving integer constants. */
729 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
730 && CONST_INT_P (newval))
732 /* Sanity check that we're replacing oldval with a CONST_INT
733 that is a valid sign-extension for the original mode. */
734 gcc_assert (INTVAL (newval)
735 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
737 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
738 CONST_INT is not valid, because after the replacement, the
739 original mode would be gone. Unfortunately, we can't tell
740 when do_SUBST is called to replace the operand thereof, so we
741 perform this test on oldval instead, checking whether an
742 invalid replacement took place before we got here. */
743 gcc_assert (!(GET_CODE (oldval) == SUBREG
744 && CONST_INT_P (SUBREG_REG (oldval))));
745 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
746 && CONST_INT_P (XEXP (oldval, 0))));
749 if (undobuf.frees)
750 buf = undobuf.frees, undobuf.frees = buf->next;
751 else
752 buf = XNEW (struct undo);
754 buf->kind = UNDO_RTX;
755 buf->where.r = into;
756 buf->old_contents.r = oldval;
757 *into = newval;
759 buf->next = undobuf.undos, undobuf.undos = buf;
762 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
764 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
765 for the value of a HOST_WIDE_INT value (including CONST_INT) is
766 not safe. */
768 static void
769 do_SUBST_INT (int *into, int newval)
771 struct undo *buf;
772 int oldval = *into;
774 if (oldval == newval)
775 return;
777 if (undobuf.frees)
778 buf = undobuf.frees, undobuf.frees = buf->next;
779 else
780 buf = XNEW (struct undo);
782 buf->kind = UNDO_INT;
783 buf->where.i = into;
784 buf->old_contents.i = oldval;
785 *into = newval;
787 buf->next = undobuf.undos, undobuf.undos = buf;
790 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
792 /* Similar to SUBST, but just substitute the mode. This is used when
793 changing the mode of a pseudo-register, so that any other
794 references to the entry in the regno_reg_rtx array will change as
795 well. */
797 static void
798 do_SUBST_MODE (rtx *into, machine_mode newval)
800 struct undo *buf;
801 machine_mode oldval = GET_MODE (*into);
803 if (oldval == newval)
804 return;
806 if (undobuf.frees)
807 buf = undobuf.frees, undobuf.frees = buf->next;
808 else
809 buf = XNEW (struct undo);
811 buf->kind = UNDO_MODE;
812 buf->where.r = into;
813 buf->old_contents.m = oldval;
814 adjust_reg_mode (*into, newval);
816 buf->next = undobuf.undos, undobuf.undos = buf;
819 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
821 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
823 static void
824 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
826 struct undo *buf;
827 struct insn_link * oldval = *into;
829 if (oldval == newval)
830 return;
832 if (undobuf.frees)
833 buf = undobuf.frees, undobuf.frees = buf->next;
834 else
835 buf = XNEW (struct undo);
837 buf->kind = UNDO_LINKS;
838 buf->where.l = into;
839 buf->old_contents.l = oldval;
840 *into = newval;
842 buf->next = undobuf.undos, undobuf.undos = buf;
845 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
847 /* Subroutine of try_combine. Determine whether the replacement patterns
848 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
849 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
850 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
851 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
852 of all the instructions can be estimated and the replacements are more
853 expensive than the original sequence. */
855 static bool
856 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
857 rtx newpat, rtx newi2pat, rtx newotherpat)
859 int i0_cost, i1_cost, i2_cost, i3_cost;
860 int new_i2_cost, new_i3_cost;
861 int old_cost, new_cost;
863 /* Lookup the original insn_rtx_costs. */
864 i2_cost = INSN_COST (i2);
865 i3_cost = INSN_COST (i3);
867 if (i1)
869 i1_cost = INSN_COST (i1);
870 if (i0)
872 i0_cost = INSN_COST (i0);
873 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
874 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
876 else
878 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
879 ? i1_cost + i2_cost + i3_cost : 0);
880 i0_cost = 0;
883 else
885 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
886 i1_cost = i0_cost = 0;
889 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
890 correct that. */
891 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
892 old_cost -= i1_cost;
895 /* Calculate the replacement insn_rtx_costs. */
896 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
897 if (newi2pat)
899 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
900 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
901 ? new_i2_cost + new_i3_cost : 0;
903 else
905 new_cost = new_i3_cost;
906 new_i2_cost = 0;
909 if (undobuf.other_insn)
911 int old_other_cost, new_other_cost;
913 old_other_cost = INSN_COST (undobuf.other_insn);
914 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
915 if (old_other_cost > 0 && new_other_cost > 0)
917 old_cost += old_other_cost;
918 new_cost += new_other_cost;
920 else
921 old_cost = 0;
924 /* Disallow this combination if both new_cost and old_cost are greater than
925 zero, and new_cost is greater than old cost. */
926 int reject = old_cost > 0 && new_cost > old_cost;
928 if (dump_file)
930 fprintf (dump_file, "%s combination of insns ",
931 reject ? "rejecting" : "allowing");
932 if (i0)
933 fprintf (dump_file, "%d, ", INSN_UID (i0));
934 if (i1 && INSN_UID (i1) != INSN_UID (i2))
935 fprintf (dump_file, "%d, ", INSN_UID (i1));
936 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
938 fprintf (dump_file, "original costs ");
939 if (i0)
940 fprintf (dump_file, "%d + ", i0_cost);
941 if (i1 && INSN_UID (i1) != INSN_UID (i2))
942 fprintf (dump_file, "%d + ", i1_cost);
943 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
945 if (newi2pat)
946 fprintf (dump_file, "replacement costs %d + %d = %d\n",
947 new_i2_cost, new_i3_cost, new_cost);
948 else
949 fprintf (dump_file, "replacement cost %d\n", new_cost);
952 if (reject)
953 return false;
955 /* Update the uid_insn_cost array with the replacement costs. */
956 INSN_COST (i2) = new_i2_cost;
957 INSN_COST (i3) = new_i3_cost;
958 if (i1)
960 INSN_COST (i1) = 0;
961 if (i0)
962 INSN_COST (i0) = 0;
965 return true;
969 /* Delete any insns that copy a register to itself. */
971 static void
972 delete_noop_moves (void)
974 rtx_insn *insn, *next;
975 basic_block bb;
977 FOR_EACH_BB_FN (bb, cfun)
979 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
981 next = NEXT_INSN (insn);
982 if (INSN_P (insn) && noop_move_p (insn))
984 if (dump_file)
985 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
987 delete_insn_and_edges (insn);
994 /* Return false if we do not want to (or cannot) combine DEF. */
995 static bool
996 can_combine_def_p (df_ref def)
998 /* Do not consider if it is pre/post modification in MEM. */
999 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1000 return false;
1002 unsigned int regno = DF_REF_REGNO (def);
1004 /* Do not combine frame pointer adjustments. */
1005 if ((regno == FRAME_POINTER_REGNUM
1006 && (!reload_completed || frame_pointer_needed))
1007 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1008 && regno == HARD_FRAME_POINTER_REGNUM
1009 && (!reload_completed || frame_pointer_needed))
1010 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1011 && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1012 return false;
1014 return true;
1017 /* Return false if we do not want to (or cannot) combine USE. */
1018 static bool
1019 can_combine_use_p (df_ref use)
1021 /* Do not consider the usage of the stack pointer by function call. */
1022 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1023 return false;
1025 return true;
1028 /* Fill in log links field for all insns. */
1030 static void
1031 create_log_links (void)
1033 basic_block bb;
1034 rtx_insn **next_use;
1035 rtx_insn *insn;
1036 df_ref def, use;
1038 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1040 /* Pass through each block from the end, recording the uses of each
1041 register and establishing log links when def is encountered.
1042 Note that we do not clear next_use array in order to save time,
1043 so we have to test whether the use is in the same basic block as def.
1045 There are a few cases below when we do not consider the definition or
1046 usage -- these are taken from original flow.c did. Don't ask me why it is
1047 done this way; I don't know and if it works, I don't want to know. */
1049 FOR_EACH_BB_FN (bb, cfun)
1051 FOR_BB_INSNS_REVERSE (bb, insn)
1053 if (!NONDEBUG_INSN_P (insn))
1054 continue;
1056 /* Log links are created only once. */
1057 gcc_assert (!LOG_LINKS (insn));
1059 FOR_EACH_INSN_DEF (def, insn)
1061 unsigned int regno = DF_REF_REGNO (def);
1062 rtx_insn *use_insn;
1064 if (!next_use[regno])
1065 continue;
1067 if (!can_combine_def_p (def))
1068 continue;
1070 use_insn = next_use[regno];
1071 next_use[regno] = NULL;
1073 if (BLOCK_FOR_INSN (use_insn) != bb)
1074 continue;
1076 /* flow.c claimed:
1078 We don't build a LOG_LINK for hard registers contained
1079 in ASM_OPERANDs. If these registers get replaced,
1080 we might wind up changing the semantics of the insn,
1081 even if reload can make what appear to be valid
1082 assignments later. */
1083 if (regno < FIRST_PSEUDO_REGISTER
1084 && asm_noperands (PATTERN (use_insn)) >= 0)
1085 continue;
1087 /* Don't add duplicate links between instructions. */
1088 struct insn_link *links;
1089 FOR_EACH_LOG_LINK (links, use_insn)
1090 if (insn == links->insn && regno == links->regno)
1091 break;
1093 if (!links)
1094 LOG_LINKS (use_insn)
1095 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1098 FOR_EACH_INSN_USE (use, insn)
1099 if (can_combine_use_p (use))
1100 next_use[DF_REF_REGNO (use)] = insn;
1104 free (next_use);
1107 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1108 true if we found a LOG_LINK that proves that A feeds B. This only works
1109 if there are no instructions between A and B which could have a link
1110 depending on A, since in that case we would not record a link for B.
1111 We also check the implicit dependency created by a cc0 setter/user
1112 pair. */
1114 static bool
1115 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1117 struct insn_link *links;
1118 FOR_EACH_LOG_LINK (links, b)
1119 if (links->insn == a)
1120 return true;
1121 if (HAVE_cc0 && sets_cc0_p (a))
1122 return true;
1123 return false;
1126 /* Main entry point for combiner. F is the first insn of the function.
1127 NREGS is the first unused pseudo-reg number.
1129 Return nonzero if the combiner has turned an indirect jump
1130 instruction into a direct jump. */
1131 static int
1132 combine_instructions (rtx_insn *f, unsigned int nregs)
1134 rtx_insn *insn, *next;
1135 rtx_insn *prev;
1136 struct insn_link *links, *nextlinks;
1137 rtx_insn *first;
1138 basic_block last_bb;
1140 int new_direct_jump_p = 0;
1142 for (first = f; first && !INSN_P (first); )
1143 first = NEXT_INSN (first);
1144 if (!first)
1145 return 0;
1147 combine_attempts = 0;
1148 combine_merges = 0;
1149 combine_extras = 0;
1150 combine_successes = 0;
1152 rtl_hooks = combine_rtl_hooks;
1154 reg_stat.safe_grow_cleared (nregs);
1156 init_recog_no_volatile ();
1158 /* Allocate array for insn info. */
1159 max_uid_known = get_max_uid ();
1160 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1161 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1162 gcc_obstack_init (&insn_link_obstack);
1164 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1166 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1167 problems when, for example, we have j <<= 1 in a loop. */
1169 nonzero_sign_valid = 0;
1170 label_tick = label_tick_ebb_start = 1;
1172 /* Scan all SETs and see if we can deduce anything about what
1173 bits are known to be zero for some registers and how many copies
1174 of the sign bit are known to exist for those registers.
1176 Also set any known values so that we can use it while searching
1177 for what bits are known to be set. */
1179 setup_incoming_promotions (first);
1180 /* Allow the entry block and the first block to fall into the same EBB.
1181 Conceptually the incoming promotions are assigned to the entry block. */
1182 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1184 create_log_links ();
1185 FOR_EACH_BB_FN (this_basic_block, cfun)
1187 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1188 last_call_luid = 0;
1189 mem_last_set = -1;
1191 label_tick++;
1192 if (!single_pred_p (this_basic_block)
1193 || single_pred (this_basic_block) != last_bb)
1194 label_tick_ebb_start = label_tick;
1195 last_bb = this_basic_block;
1197 FOR_BB_INSNS (this_basic_block, insn)
1198 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1200 rtx links;
1202 subst_low_luid = DF_INSN_LUID (insn);
1203 subst_insn = insn;
1205 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1206 insn);
1207 record_dead_and_set_regs (insn);
1209 if (AUTO_INC_DEC)
1210 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1211 if (REG_NOTE_KIND (links) == REG_INC)
1212 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1213 insn);
1215 /* Record the current insn_rtx_cost of this instruction. */
1216 if (NONJUMP_INSN_P (insn))
1217 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1218 optimize_this_for_speed_p);
1219 if (dump_file)
1220 fprintf (dump_file, "insn_cost %d: %d\n",
1221 INSN_UID (insn), INSN_COST (insn));
1225 nonzero_sign_valid = 1;
1227 /* Now scan all the insns in forward order. */
1228 label_tick = label_tick_ebb_start = 1;
1229 init_reg_last ();
1230 setup_incoming_promotions (first);
1231 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1232 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1234 FOR_EACH_BB_FN (this_basic_block, cfun)
1236 rtx_insn *last_combined_insn = NULL;
1237 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1238 last_call_luid = 0;
1239 mem_last_set = -1;
1241 label_tick++;
1242 if (!single_pred_p (this_basic_block)
1243 || single_pred (this_basic_block) != last_bb)
1244 label_tick_ebb_start = label_tick;
1245 last_bb = this_basic_block;
1247 rtl_profile_for_bb (this_basic_block);
1248 for (insn = BB_HEAD (this_basic_block);
1249 insn != NEXT_INSN (BB_END (this_basic_block));
1250 insn = next ? next : NEXT_INSN (insn))
1252 next = 0;
1253 if (!NONDEBUG_INSN_P (insn))
1254 continue;
1256 while (last_combined_insn
1257 && last_combined_insn->deleted ())
1258 last_combined_insn = PREV_INSN (last_combined_insn);
1259 if (last_combined_insn == NULL_RTX
1260 || BARRIER_P (last_combined_insn)
1261 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1262 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1263 last_combined_insn = insn;
1265 /* See if we know about function return values before this
1266 insn based upon SUBREG flags. */
1267 check_promoted_subreg (insn, PATTERN (insn));
1269 /* See if we can find hardregs and subreg of pseudos in
1270 narrower modes. This could help turning TRUNCATEs
1271 into SUBREGs. */
1272 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1274 /* Try this insn with each insn it links back to. */
1276 FOR_EACH_LOG_LINK (links, insn)
1277 if ((next = try_combine (insn, links->insn, NULL,
1278 NULL, &new_direct_jump_p,
1279 last_combined_insn)) != 0)
1281 statistics_counter_event (cfun, "two-insn combine", 1);
1282 goto retry;
1285 /* Try each sequence of three linked insns ending with this one. */
1287 if (max_combine >= 3)
1288 FOR_EACH_LOG_LINK (links, insn)
1290 rtx_insn *link = links->insn;
1292 /* If the linked insn has been replaced by a note, then there
1293 is no point in pursuing this chain any further. */
1294 if (NOTE_P (link))
1295 continue;
1297 FOR_EACH_LOG_LINK (nextlinks, link)
1298 if ((next = try_combine (insn, link, nextlinks->insn,
1299 NULL, &new_direct_jump_p,
1300 last_combined_insn)) != 0)
1302 statistics_counter_event (cfun, "three-insn combine", 1);
1303 goto retry;
1307 /* Try to combine a jump insn that uses CC0
1308 with a preceding insn that sets CC0, and maybe with its
1309 logical predecessor as well.
1310 This is how we make decrement-and-branch insns.
1311 We need this special code because data flow connections
1312 via CC0 do not get entered in LOG_LINKS. */
1314 if (HAVE_cc0
1315 && JUMP_P (insn)
1316 && (prev = prev_nonnote_insn (insn)) != 0
1317 && NONJUMP_INSN_P (prev)
1318 && sets_cc0_p (PATTERN (prev)))
1320 if ((next = try_combine (insn, prev, NULL, NULL,
1321 &new_direct_jump_p,
1322 last_combined_insn)) != 0)
1323 goto retry;
1325 FOR_EACH_LOG_LINK (nextlinks, prev)
1326 if ((next = try_combine (insn, prev, nextlinks->insn,
1327 NULL, &new_direct_jump_p,
1328 last_combined_insn)) != 0)
1329 goto retry;
1332 /* Do the same for an insn that explicitly references CC0. */
1333 if (HAVE_cc0 && NONJUMP_INSN_P (insn)
1334 && (prev = prev_nonnote_insn (insn)) != 0
1335 && NONJUMP_INSN_P (prev)
1336 && sets_cc0_p (PATTERN (prev))
1337 && GET_CODE (PATTERN (insn)) == SET
1338 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1340 if ((next = try_combine (insn, prev, NULL, NULL,
1341 &new_direct_jump_p,
1342 last_combined_insn)) != 0)
1343 goto retry;
1345 FOR_EACH_LOG_LINK (nextlinks, prev)
1346 if ((next = try_combine (insn, prev, nextlinks->insn,
1347 NULL, &new_direct_jump_p,
1348 last_combined_insn)) != 0)
1349 goto retry;
1352 /* Finally, see if any of the insns that this insn links to
1353 explicitly references CC0. If so, try this insn, that insn,
1354 and its predecessor if it sets CC0. */
1355 if (HAVE_cc0)
1357 FOR_EACH_LOG_LINK (links, insn)
1358 if (NONJUMP_INSN_P (links->insn)
1359 && GET_CODE (PATTERN (links->insn)) == SET
1360 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1361 && (prev = prev_nonnote_insn (links->insn)) != 0
1362 && NONJUMP_INSN_P (prev)
1363 && sets_cc0_p (PATTERN (prev))
1364 && (next = try_combine (insn, links->insn,
1365 prev, NULL, &new_direct_jump_p,
1366 last_combined_insn)) != 0)
1367 goto retry;
1370 /* Try combining an insn with two different insns whose results it
1371 uses. */
1372 if (max_combine >= 3)
1373 FOR_EACH_LOG_LINK (links, insn)
1374 for (nextlinks = links->next; nextlinks;
1375 nextlinks = nextlinks->next)
1376 if ((next = try_combine (insn, links->insn,
1377 nextlinks->insn, NULL,
1378 &new_direct_jump_p,
1379 last_combined_insn)) != 0)
1382 statistics_counter_event (cfun, "three-insn combine", 1);
1383 goto retry;
1386 /* Try four-instruction combinations. */
1387 if (max_combine >= 4)
1388 FOR_EACH_LOG_LINK (links, insn)
1390 struct insn_link *next1;
1391 rtx_insn *link = links->insn;
1393 /* If the linked insn has been replaced by a note, then there
1394 is no point in pursuing this chain any further. */
1395 if (NOTE_P (link))
1396 continue;
1398 FOR_EACH_LOG_LINK (next1, link)
1400 rtx_insn *link1 = next1->insn;
1401 if (NOTE_P (link1))
1402 continue;
1403 /* I0 -> I1 -> I2 -> I3. */
1404 FOR_EACH_LOG_LINK (nextlinks, link1)
1405 if ((next = try_combine (insn, link, link1,
1406 nextlinks->insn,
1407 &new_direct_jump_p,
1408 last_combined_insn)) != 0)
1410 statistics_counter_event (cfun, "four-insn combine", 1);
1411 goto retry;
1413 /* I0, I1 -> I2, I2 -> I3. */
1414 for (nextlinks = next1->next; nextlinks;
1415 nextlinks = nextlinks->next)
1416 if ((next = try_combine (insn, link, link1,
1417 nextlinks->insn,
1418 &new_direct_jump_p,
1419 last_combined_insn)) != 0)
1421 statistics_counter_event (cfun, "four-insn combine", 1);
1422 goto retry;
1426 for (next1 = links->next; next1; next1 = next1->next)
1428 rtx_insn *link1 = next1->insn;
1429 if (NOTE_P (link1))
1430 continue;
1431 /* I0 -> I2; I1, I2 -> I3. */
1432 FOR_EACH_LOG_LINK (nextlinks, link)
1433 if ((next = try_combine (insn, link, link1,
1434 nextlinks->insn,
1435 &new_direct_jump_p,
1436 last_combined_insn)) != 0)
1438 statistics_counter_event (cfun, "four-insn combine", 1);
1439 goto retry;
1441 /* I0 -> I1; I1, I2 -> I3. */
1442 FOR_EACH_LOG_LINK (nextlinks, link1)
1443 if ((next = try_combine (insn, link, link1,
1444 nextlinks->insn,
1445 &new_direct_jump_p,
1446 last_combined_insn)) != 0)
1448 statistics_counter_event (cfun, "four-insn combine", 1);
1449 goto retry;
1454 /* Try this insn with each REG_EQUAL note it links back to. */
1455 FOR_EACH_LOG_LINK (links, insn)
1457 rtx set, note;
1458 rtx_insn *temp = links->insn;
1459 if ((set = single_set (temp)) != 0
1460 && (note = find_reg_equal_equiv_note (temp)) != 0
1461 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1462 /* Avoid using a register that may already been marked
1463 dead by an earlier instruction. */
1464 && ! unmentioned_reg_p (note, SET_SRC (set))
1465 && (GET_MODE (note) == VOIDmode
1466 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1467 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1469 /* Temporarily replace the set's source with the
1470 contents of the REG_EQUAL note. The insn will
1471 be deleted or recognized by try_combine. */
1472 rtx orig = SET_SRC (set);
1473 SET_SRC (set) = note;
1474 i2mod = temp;
1475 i2mod_old_rhs = copy_rtx (orig);
1476 i2mod_new_rhs = copy_rtx (note);
1477 next = try_combine (insn, i2mod, NULL, NULL,
1478 &new_direct_jump_p,
1479 last_combined_insn);
1480 i2mod = NULL;
1481 if (next)
1483 statistics_counter_event (cfun, "insn-with-note combine", 1);
1484 goto retry;
1486 SET_SRC (set) = orig;
1490 if (!NOTE_P (insn))
1491 record_dead_and_set_regs (insn);
1493 retry:
1498 default_rtl_profile ();
1499 clear_bb_flags ();
1500 new_direct_jump_p |= purge_all_dead_edges ();
1501 delete_noop_moves ();
1503 /* Clean up. */
1504 obstack_free (&insn_link_obstack, NULL);
1505 free (uid_log_links);
1506 free (uid_insn_cost);
1507 reg_stat.release ();
1510 struct undo *undo, *next;
1511 for (undo = undobuf.frees; undo; undo = next)
1513 next = undo->next;
1514 free (undo);
1516 undobuf.frees = 0;
1519 total_attempts += combine_attempts;
1520 total_merges += combine_merges;
1521 total_extras += combine_extras;
1522 total_successes += combine_successes;
1524 nonzero_sign_valid = 0;
1525 rtl_hooks = general_rtl_hooks;
1527 /* Make recognizer allow volatile MEMs again. */
1528 init_recog ();
1530 return new_direct_jump_p;
1533 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1535 static void
1536 init_reg_last (void)
1538 unsigned int i;
1539 reg_stat_type *p;
1541 FOR_EACH_VEC_ELT (reg_stat, i, p)
1542 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1545 /* Set up any promoted values for incoming argument registers. */
1547 static void
1548 setup_incoming_promotions (rtx_insn *first)
1550 tree arg;
1551 bool strictly_local = false;
1553 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1554 arg = DECL_CHAIN (arg))
1556 rtx x, reg = DECL_INCOMING_RTL (arg);
1557 int uns1, uns3;
1558 machine_mode mode1, mode2, mode3, mode4;
1560 /* Only continue if the incoming argument is in a register. */
1561 if (!REG_P (reg))
1562 continue;
1564 /* Determine, if possible, whether all call sites of the current
1565 function lie within the current compilation unit. (This does
1566 take into account the exporting of a function via taking its
1567 address, and so forth.) */
1568 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1570 /* The mode and signedness of the argument before any promotions happen
1571 (equal to the mode of the pseudo holding it at that stage). */
1572 mode1 = TYPE_MODE (TREE_TYPE (arg));
1573 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1575 /* The mode and signedness of the argument after any source language and
1576 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1577 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1578 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1580 /* The mode and signedness of the argument as it is actually passed,
1581 see assign_parm_setup_reg in function.c. */
1582 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1583 TREE_TYPE (cfun->decl), 0);
1585 /* The mode of the register in which the argument is being passed. */
1586 mode4 = GET_MODE (reg);
1588 /* Eliminate sign extensions in the callee when:
1589 (a) A mode promotion has occurred; */
1590 if (mode1 == mode3)
1591 continue;
1592 /* (b) The mode of the register is the same as the mode of
1593 the argument as it is passed; */
1594 if (mode3 != mode4)
1595 continue;
1596 /* (c) There's no language level extension; */
1597 if (mode1 == mode2)
1599 /* (c.1) All callers are from the current compilation unit. If that's
1600 the case we don't have to rely on an ABI, we only have to know
1601 what we're generating right now, and we know that we will do the
1602 mode1 to mode2 promotion with the given sign. */
1603 else if (!strictly_local)
1604 continue;
1605 /* (c.2) The combination of the two promotions is useful. This is
1606 true when the signs match, or if the first promotion is unsigned.
1607 In the later case, (sign_extend (zero_extend x)) is the same as
1608 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1609 else if (uns1)
1610 uns3 = true;
1611 else if (uns3)
1612 continue;
1614 /* Record that the value was promoted from mode1 to mode3,
1615 so that any sign extension at the head of the current
1616 function may be eliminated. */
1617 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1618 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1619 record_value_for_reg (reg, first, x);
1623 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1624 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1625 because some machines (maybe most) will actually do the sign-extension and
1626 this is the conservative approach.
1628 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1629 kludge. */
1631 static rtx
1632 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1634 if (GET_MODE_PRECISION (mode) < prec
1635 && CONST_INT_P (src)
1636 && INTVAL (src) > 0
1637 && val_signbit_known_set_p (mode, INTVAL (src)))
1638 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (mode));
1640 return src;
1643 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1644 and SET. */
1646 static void
1647 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1648 rtx x)
1650 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1651 unsigned HOST_WIDE_INT bits = 0;
1652 rtx reg_equal = NULL, src = SET_SRC (set);
1653 unsigned int num = 0;
1655 if (reg_equal_note)
1656 reg_equal = XEXP (reg_equal_note, 0);
1658 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1660 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1661 if (reg_equal)
1662 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1665 /* Don't call nonzero_bits if it cannot change anything. */
1666 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1668 bits = nonzero_bits (src, nonzero_bits_mode);
1669 if (reg_equal && bits)
1670 bits &= nonzero_bits (reg_equal, nonzero_bits_mode);
1671 rsp->nonzero_bits |= bits;
1674 /* Don't call num_sign_bit_copies if it cannot change anything. */
1675 if (rsp->sign_bit_copies != 1)
1677 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1678 if (reg_equal && num != GET_MODE_PRECISION (GET_MODE (x)))
1680 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1681 if (num == 0 || numeq > num)
1682 num = numeq;
1684 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1685 rsp->sign_bit_copies = num;
1689 /* Called via note_stores. If X is a pseudo that is narrower than
1690 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1692 If we are setting only a portion of X and we can't figure out what
1693 portion, assume all bits will be used since we don't know what will
1694 be happening.
1696 Similarly, set how many bits of X are known to be copies of the sign bit
1697 at all locations in the function. This is the smallest number implied
1698 by any set of X. */
1700 static void
1701 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1703 rtx_insn *insn = (rtx_insn *) data;
1705 if (REG_P (x)
1706 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1707 /* If this register is undefined at the start of the file, we can't
1708 say what its contents were. */
1709 && ! REGNO_REG_SET_P
1710 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1711 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1713 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1715 if (set == 0 || GET_CODE (set) == CLOBBER)
1717 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1718 rsp->sign_bit_copies = 1;
1719 return;
1722 /* If this register is being initialized using itself, and the
1723 register is uninitialized in this basic block, and there are
1724 no LOG_LINKS which set the register, then part of the
1725 register is uninitialized. In that case we can't assume
1726 anything about the number of nonzero bits.
1728 ??? We could do better if we checked this in
1729 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1730 could avoid making assumptions about the insn which initially
1731 sets the register, while still using the information in other
1732 insns. We would have to be careful to check every insn
1733 involved in the combination. */
1735 if (insn
1736 && reg_referenced_p (x, PATTERN (insn))
1737 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1738 REGNO (x)))
1740 struct insn_link *link;
1742 FOR_EACH_LOG_LINK (link, insn)
1743 if (dead_or_set_p (link->insn, x))
1744 break;
1745 if (!link)
1747 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1748 rsp->sign_bit_copies = 1;
1749 return;
1753 /* If this is a complex assignment, see if we can convert it into a
1754 simple assignment. */
1755 set = expand_field_assignment (set);
1757 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1758 set what we know about X. */
1760 if (SET_DEST (set) == x
1761 || (paradoxical_subreg_p (SET_DEST (set))
1762 && SUBREG_REG (SET_DEST (set)) == x))
1763 update_rsp_from_reg_equal (rsp, insn, set, x);
1764 else
1766 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1767 rsp->sign_bit_copies = 1;
1772 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1773 optionally insns that were previously combined into I3 or that will be
1774 combined into the merger of INSN and I3. The order is PRED, PRED2,
1775 INSN, SUCC, SUCC2, I3.
1777 Return 0 if the combination is not allowed for any reason.
1779 If the combination is allowed, *PDEST will be set to the single
1780 destination of INSN and *PSRC to the single source, and this function
1781 will return 1. */
1783 static int
1784 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1785 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1786 rtx *pdest, rtx *psrc)
1788 int i;
1789 const_rtx set = 0;
1790 rtx src, dest;
1791 rtx_insn *p;
1792 rtx link;
1793 bool all_adjacent = true;
1794 int (*is_volatile_p) (const_rtx);
1796 if (succ)
1798 if (succ2)
1800 if (next_active_insn (succ2) != i3)
1801 all_adjacent = false;
1802 if (next_active_insn (succ) != succ2)
1803 all_adjacent = false;
1805 else if (next_active_insn (succ) != i3)
1806 all_adjacent = false;
1807 if (next_active_insn (insn) != succ)
1808 all_adjacent = false;
1810 else if (next_active_insn (insn) != i3)
1811 all_adjacent = false;
1813 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1814 or a PARALLEL consisting of such a SET and CLOBBERs.
1816 If INSN has CLOBBER parallel parts, ignore them for our processing.
1817 By definition, these happen during the execution of the insn. When it
1818 is merged with another insn, all bets are off. If they are, in fact,
1819 needed and aren't also supplied in I3, they may be added by
1820 recog_for_combine. Otherwise, it won't match.
1822 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1823 note.
1825 Get the source and destination of INSN. If more than one, can't
1826 combine. */
1828 if (GET_CODE (PATTERN (insn)) == SET)
1829 set = PATTERN (insn);
1830 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1831 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1833 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1835 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1837 switch (GET_CODE (elt))
1839 /* This is important to combine floating point insns
1840 for the SH4 port. */
1841 case USE:
1842 /* Combining an isolated USE doesn't make sense.
1843 We depend here on combinable_i3pat to reject them. */
1844 /* The code below this loop only verifies that the inputs of
1845 the SET in INSN do not change. We call reg_set_between_p
1846 to verify that the REG in the USE does not change between
1847 I3 and INSN.
1848 If the USE in INSN was for a pseudo register, the matching
1849 insn pattern will likely match any register; combining this
1850 with any other USE would only be safe if we knew that the
1851 used registers have identical values, or if there was
1852 something to tell them apart, e.g. different modes. For
1853 now, we forgo such complicated tests and simply disallow
1854 combining of USES of pseudo registers with any other USE. */
1855 if (REG_P (XEXP (elt, 0))
1856 && GET_CODE (PATTERN (i3)) == PARALLEL)
1858 rtx i3pat = PATTERN (i3);
1859 int i = XVECLEN (i3pat, 0) - 1;
1860 unsigned int regno = REGNO (XEXP (elt, 0));
1864 rtx i3elt = XVECEXP (i3pat, 0, i);
1866 if (GET_CODE (i3elt) == USE
1867 && REG_P (XEXP (i3elt, 0))
1868 && (REGNO (XEXP (i3elt, 0)) == regno
1869 ? reg_set_between_p (XEXP (elt, 0),
1870 PREV_INSN (insn), i3)
1871 : regno >= FIRST_PSEUDO_REGISTER))
1872 return 0;
1874 while (--i >= 0);
1876 break;
1878 /* We can ignore CLOBBERs. */
1879 case CLOBBER:
1880 break;
1882 case SET:
1883 /* Ignore SETs whose result isn't used but not those that
1884 have side-effects. */
1885 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1886 && insn_nothrow_p (insn)
1887 && !side_effects_p (elt))
1888 break;
1890 /* If we have already found a SET, this is a second one and
1891 so we cannot combine with this insn. */
1892 if (set)
1893 return 0;
1895 set = elt;
1896 break;
1898 default:
1899 /* Anything else means we can't combine. */
1900 return 0;
1904 if (set == 0
1905 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1906 so don't do anything with it. */
1907 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1908 return 0;
1910 else
1911 return 0;
1913 if (set == 0)
1914 return 0;
1916 /* The simplification in expand_field_assignment may call back to
1917 get_last_value, so set safe guard here. */
1918 subst_low_luid = DF_INSN_LUID (insn);
1920 set = expand_field_assignment (set);
1921 src = SET_SRC (set), dest = SET_DEST (set);
1923 /* Do not eliminate user-specified register if it is in an
1924 asm input because we may break the register asm usage defined
1925 in GCC manual if allow to do so.
1926 Be aware that this may cover more cases than we expect but this
1927 should be harmless. */
1928 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1929 && extract_asm_operands (PATTERN (i3)))
1930 return 0;
1932 /* Don't eliminate a store in the stack pointer. */
1933 if (dest == stack_pointer_rtx
1934 /* Don't combine with an insn that sets a register to itself if it has
1935 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1936 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1937 /* Can't merge an ASM_OPERANDS. */
1938 || GET_CODE (src) == ASM_OPERANDS
1939 /* Can't merge a function call. */
1940 || GET_CODE (src) == CALL
1941 /* Don't eliminate a function call argument. */
1942 || (CALL_P (i3)
1943 && (find_reg_fusage (i3, USE, dest)
1944 || (REG_P (dest)
1945 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1946 && global_regs[REGNO (dest)])))
1947 /* Don't substitute into an incremented register. */
1948 || FIND_REG_INC_NOTE (i3, dest)
1949 || (succ && FIND_REG_INC_NOTE (succ, dest))
1950 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1951 /* Don't substitute into a non-local goto, this confuses CFG. */
1952 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1953 /* Make sure that DEST is not used after SUCC but before I3. */
1954 || (!all_adjacent
1955 && ((succ2
1956 && (reg_used_between_p (dest, succ2, i3)
1957 || reg_used_between_p (dest, succ, succ2)))
1958 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1959 /* Make sure that the value that is to be substituted for the register
1960 does not use any registers whose values alter in between. However,
1961 If the insns are adjacent, a use can't cross a set even though we
1962 think it might (this can happen for a sequence of insns each setting
1963 the same destination; last_set of that register might point to
1964 a NOTE). If INSN has a REG_EQUIV note, the register is always
1965 equivalent to the memory so the substitution is valid even if there
1966 are intervening stores. Also, don't move a volatile asm or
1967 UNSPEC_VOLATILE across any other insns. */
1968 || (! all_adjacent
1969 && (((!MEM_P (src)
1970 || ! find_reg_note (insn, REG_EQUIV, src))
1971 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1972 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1973 || GET_CODE (src) == UNSPEC_VOLATILE))
1974 /* Don't combine across a CALL_INSN, because that would possibly
1975 change whether the life span of some REGs crosses calls or not,
1976 and it is a pain to update that information.
1977 Exception: if source is a constant, moving it later can't hurt.
1978 Accept that as a special case. */
1979 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1980 return 0;
1982 /* DEST must either be a REG or CC0. */
1983 if (REG_P (dest))
1985 /* If register alignment is being enforced for multi-word items in all
1986 cases except for parameters, it is possible to have a register copy
1987 insn referencing a hard register that is not allowed to contain the
1988 mode being copied and which would not be valid as an operand of most
1989 insns. Eliminate this problem by not combining with such an insn.
1991 Also, on some machines we don't want to extend the life of a hard
1992 register. */
1994 if (REG_P (src)
1995 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1996 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1997 /* Don't extend the life of a hard register unless it is
1998 user variable (if we have few registers) or it can't
1999 fit into the desired register (meaning something special
2000 is going on).
2001 Also avoid substituting a return register into I3, because
2002 reload can't handle a conflict with constraints of other
2003 inputs. */
2004 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2005 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
2006 return 0;
2008 else if (GET_CODE (dest) != CC0)
2009 return 0;
2012 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2013 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2014 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2016 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2018 /* If the clobber represents an earlyclobber operand, we must not
2019 substitute an expression containing the clobbered register.
2020 As we do not analyze the constraint strings here, we have to
2021 make the conservative assumption. However, if the register is
2022 a fixed hard reg, the clobber cannot represent any operand;
2023 we leave it up to the machine description to either accept or
2024 reject use-and-clobber patterns. */
2025 if (!REG_P (reg)
2026 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2027 || !fixed_regs[REGNO (reg)])
2028 if (reg_overlap_mentioned_p (reg, src))
2029 return 0;
2032 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2033 or not), reject, unless nothing volatile comes between it and I3 */
2035 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2037 /* Make sure neither succ nor succ2 contains a volatile reference. */
2038 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2039 return 0;
2040 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2041 return 0;
2042 /* We'll check insns between INSN and I3 below. */
2045 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2046 to be an explicit register variable, and was chosen for a reason. */
2048 if (GET_CODE (src) == ASM_OPERANDS
2049 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2050 return 0;
2052 /* If INSN contains volatile references (specifically volatile MEMs),
2053 we cannot combine across any other volatile references.
2054 Even if INSN doesn't contain volatile references, any intervening
2055 volatile insn might affect machine state. */
2057 is_volatile_p = volatile_refs_p (PATTERN (insn))
2058 ? volatile_refs_p
2059 : volatile_insn_p;
2061 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2062 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2063 return 0;
2065 /* If INSN contains an autoincrement or autodecrement, make sure that
2066 register is not used between there and I3, and not already used in
2067 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2068 Also insist that I3 not be a jump; if it were one
2069 and the incremented register were spilled, we would lose. */
2071 if (AUTO_INC_DEC)
2072 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2073 if (REG_NOTE_KIND (link) == REG_INC
2074 && (JUMP_P (i3)
2075 || reg_used_between_p (XEXP (link, 0), insn, i3)
2076 || (pred != NULL_RTX
2077 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2078 || (pred2 != NULL_RTX
2079 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2080 || (succ != NULL_RTX
2081 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2082 || (succ2 != NULL_RTX
2083 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2084 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2085 return 0;
2087 /* Don't combine an insn that follows a CC0-setting insn.
2088 An insn that uses CC0 must not be separated from the one that sets it.
2089 We do, however, allow I2 to follow a CC0-setting insn if that insn
2090 is passed as I1; in that case it will be deleted also.
2091 We also allow combining in this case if all the insns are adjacent
2092 because that would leave the two CC0 insns adjacent as well.
2093 It would be more logical to test whether CC0 occurs inside I1 or I2,
2094 but that would be much slower, and this ought to be equivalent. */
2096 if (HAVE_cc0)
2098 p = prev_nonnote_insn (insn);
2099 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2100 && ! all_adjacent)
2101 return 0;
2104 /* If we get here, we have passed all the tests and the combination is
2105 to be allowed. */
2107 *pdest = dest;
2108 *psrc = src;
2110 return 1;
2113 /* LOC is the location within I3 that contains its pattern or the component
2114 of a PARALLEL of the pattern. We validate that it is valid for combining.
2116 One problem is if I3 modifies its output, as opposed to replacing it
2117 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2118 doing so would produce an insn that is not equivalent to the original insns.
2120 Consider:
2122 (set (reg:DI 101) (reg:DI 100))
2123 (set (subreg:SI (reg:DI 101) 0) <foo>)
2125 This is NOT equivalent to:
2127 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2128 (set (reg:DI 101) (reg:DI 100))])
2130 Not only does this modify 100 (in which case it might still be valid
2131 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2133 We can also run into a problem if I2 sets a register that I1
2134 uses and I1 gets directly substituted into I3 (not via I2). In that
2135 case, we would be getting the wrong value of I2DEST into I3, so we
2136 must reject the combination. This case occurs when I2 and I1 both
2137 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2138 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2139 of a SET must prevent combination from occurring. The same situation
2140 can occur for I0, in which case I0_NOT_IN_SRC is set.
2142 Before doing the above check, we first try to expand a field assignment
2143 into a set of logical operations.
2145 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2146 we place a register that is both set and used within I3. If more than one
2147 such register is detected, we fail.
2149 Return 1 if the combination is valid, zero otherwise. */
2151 static int
2152 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2153 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2155 rtx x = *loc;
2157 if (GET_CODE (x) == SET)
2159 rtx set = x ;
2160 rtx dest = SET_DEST (set);
2161 rtx src = SET_SRC (set);
2162 rtx inner_dest = dest;
2163 rtx subdest;
2165 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2166 || GET_CODE (inner_dest) == SUBREG
2167 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2168 inner_dest = XEXP (inner_dest, 0);
2170 /* Check for the case where I3 modifies its output, as discussed
2171 above. We don't want to prevent pseudos from being combined
2172 into the address of a MEM, so only prevent the combination if
2173 i1 or i2 set the same MEM. */
2174 if ((inner_dest != dest &&
2175 (!MEM_P (inner_dest)
2176 || rtx_equal_p (i2dest, inner_dest)
2177 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2178 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2179 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2180 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2181 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2183 /* This is the same test done in can_combine_p except we can't test
2184 all_adjacent; we don't have to, since this instruction will stay
2185 in place, thus we are not considering increasing the lifetime of
2186 INNER_DEST.
2188 Also, if this insn sets a function argument, combining it with
2189 something that might need a spill could clobber a previous
2190 function argument; the all_adjacent test in can_combine_p also
2191 checks this; here, we do a more specific test for this case. */
2193 || (REG_P (inner_dest)
2194 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2195 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2196 GET_MODE (inner_dest))))
2197 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2198 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2199 return 0;
2201 /* If DEST is used in I3, it is being killed in this insn, so
2202 record that for later. We have to consider paradoxical
2203 subregs here, since they kill the whole register, but we
2204 ignore partial subregs, STRICT_LOW_PART, etc.
2205 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2206 STACK_POINTER_REGNUM, since these are always considered to be
2207 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2208 subdest = dest;
2209 if (GET_CODE (subdest) == SUBREG
2210 && (GET_MODE_SIZE (GET_MODE (subdest))
2211 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2212 subdest = SUBREG_REG (subdest);
2213 if (pi3dest_killed
2214 && REG_P (subdest)
2215 && reg_referenced_p (subdest, PATTERN (i3))
2216 && REGNO (subdest) != FRAME_POINTER_REGNUM
2217 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2218 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2219 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2220 || (REGNO (subdest) != ARG_POINTER_REGNUM
2221 || ! fixed_regs [REGNO (subdest)]))
2222 && REGNO (subdest) != STACK_POINTER_REGNUM)
2224 if (*pi3dest_killed)
2225 return 0;
2227 *pi3dest_killed = subdest;
2231 else if (GET_CODE (x) == PARALLEL)
2233 int i;
2235 for (i = 0; i < XVECLEN (x, 0); i++)
2236 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2237 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2238 return 0;
2241 return 1;
2244 /* Return 1 if X is an arithmetic expression that contains a multiplication
2245 and division. We don't count multiplications by powers of two here. */
2247 static int
2248 contains_muldiv (rtx x)
2250 switch (GET_CODE (x))
2252 case MOD: case DIV: case UMOD: case UDIV:
2253 return 1;
2255 case MULT:
2256 return ! (CONST_INT_P (XEXP (x, 1))
2257 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2258 default:
2259 if (BINARY_P (x))
2260 return contains_muldiv (XEXP (x, 0))
2261 || contains_muldiv (XEXP (x, 1));
2263 if (UNARY_P (x))
2264 return contains_muldiv (XEXP (x, 0));
2266 return 0;
2270 /* Determine whether INSN can be used in a combination. Return nonzero if
2271 not. This is used in try_combine to detect early some cases where we
2272 can't perform combinations. */
2274 static int
2275 cant_combine_insn_p (rtx_insn *insn)
2277 rtx set;
2278 rtx src, dest;
2280 /* If this isn't really an insn, we can't do anything.
2281 This can occur when flow deletes an insn that it has merged into an
2282 auto-increment address. */
2283 if (! INSN_P (insn))
2284 return 1;
2286 /* Never combine loads and stores involving hard regs that are likely
2287 to be spilled. The register allocator can usually handle such
2288 reg-reg moves by tying. If we allow the combiner to make
2289 substitutions of likely-spilled regs, reload might die.
2290 As an exception, we allow combinations involving fixed regs; these are
2291 not available to the register allocator so there's no risk involved. */
2293 set = single_set (insn);
2294 if (! set)
2295 return 0;
2296 src = SET_SRC (set);
2297 dest = SET_DEST (set);
2298 if (GET_CODE (src) == SUBREG)
2299 src = SUBREG_REG (src);
2300 if (GET_CODE (dest) == SUBREG)
2301 dest = SUBREG_REG (dest);
2302 if (REG_P (src) && REG_P (dest)
2303 && ((HARD_REGISTER_P (src)
2304 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2305 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2306 || (HARD_REGISTER_P (dest)
2307 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2308 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2309 return 1;
2311 return 0;
2314 struct likely_spilled_retval_info
2316 unsigned regno, nregs;
2317 unsigned mask;
2320 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2321 hard registers that are known to be written to / clobbered in full. */
2322 static void
2323 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2325 struct likely_spilled_retval_info *const info =
2326 (struct likely_spilled_retval_info *) data;
2327 unsigned regno, nregs;
2328 unsigned new_mask;
2330 if (!REG_P (XEXP (set, 0)))
2331 return;
2332 regno = REGNO (x);
2333 if (regno >= info->regno + info->nregs)
2334 return;
2335 nregs = REG_NREGS (x);
2336 if (regno + nregs <= info->regno)
2337 return;
2338 new_mask = (2U << (nregs - 1)) - 1;
2339 if (regno < info->regno)
2340 new_mask >>= info->regno - regno;
2341 else
2342 new_mask <<= regno - info->regno;
2343 info->mask &= ~new_mask;
2346 /* Return nonzero iff part of the return value is live during INSN, and
2347 it is likely spilled. This can happen when more than one insn is needed
2348 to copy the return value, e.g. when we consider to combine into the
2349 second copy insn for a complex value. */
2351 static int
2352 likely_spilled_retval_p (rtx_insn *insn)
2354 rtx_insn *use = BB_END (this_basic_block);
2355 rtx reg;
2356 rtx_insn *p;
2357 unsigned regno, nregs;
2358 /* We assume here that no machine mode needs more than
2359 32 hard registers when the value overlaps with a register
2360 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2361 unsigned mask;
2362 struct likely_spilled_retval_info info;
2364 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2365 return 0;
2366 reg = XEXP (PATTERN (use), 0);
2367 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2368 return 0;
2369 regno = REGNO (reg);
2370 nregs = REG_NREGS (reg);
2371 if (nregs == 1)
2372 return 0;
2373 mask = (2U << (nregs - 1)) - 1;
2375 /* Disregard parts of the return value that are set later. */
2376 info.regno = regno;
2377 info.nregs = nregs;
2378 info.mask = mask;
2379 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2380 if (INSN_P (p))
2381 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2382 mask = info.mask;
2384 /* Check if any of the (probably) live return value registers is
2385 likely spilled. */
2386 nregs --;
2389 if ((mask & 1 << nregs)
2390 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2391 return 1;
2392 } while (nregs--);
2393 return 0;
2396 /* Adjust INSN after we made a change to its destination.
2398 Changing the destination can invalidate notes that say something about
2399 the results of the insn and a LOG_LINK pointing to the insn. */
2401 static void
2402 adjust_for_new_dest (rtx_insn *insn)
2404 /* For notes, be conservative and simply remove them. */
2405 remove_reg_equal_equiv_notes (insn);
2407 /* The new insn will have a destination that was previously the destination
2408 of an insn just above it. Call distribute_links to make a LOG_LINK from
2409 the next use of that destination. */
2411 rtx set = single_set (insn);
2412 gcc_assert (set);
2414 rtx reg = SET_DEST (set);
2416 while (GET_CODE (reg) == ZERO_EXTRACT
2417 || GET_CODE (reg) == STRICT_LOW_PART
2418 || GET_CODE (reg) == SUBREG)
2419 reg = XEXP (reg, 0);
2420 gcc_assert (REG_P (reg));
2422 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2424 df_insn_rescan (insn);
2427 /* Return TRUE if combine can reuse reg X in mode MODE.
2428 ADDED_SETS is nonzero if the original set is still required. */
2429 static bool
2430 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2432 unsigned int regno;
2434 if (!REG_P (x))
2435 return false;
2437 regno = REGNO (x);
2438 /* Allow hard registers if the new mode is legal, and occupies no more
2439 registers than the old mode. */
2440 if (regno < FIRST_PSEUDO_REGISTER)
2441 return (HARD_REGNO_MODE_OK (regno, mode)
2442 && REG_NREGS (x) >= hard_regno_nregs[regno][mode]);
2444 /* Or a pseudo that is only used once. */
2445 return (regno < reg_n_sets_max
2446 && REG_N_SETS (regno) == 1
2447 && !added_sets
2448 && !REG_USERVAR_P (x));
2452 /* Check whether X, the destination of a set, refers to part of
2453 the register specified by REG. */
2455 static bool
2456 reg_subword_p (rtx x, rtx reg)
2458 /* Check that reg is an integer mode register. */
2459 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2460 return false;
2462 if (GET_CODE (x) == STRICT_LOW_PART
2463 || GET_CODE (x) == ZERO_EXTRACT)
2464 x = XEXP (x, 0);
2466 return GET_CODE (x) == SUBREG
2467 && SUBREG_REG (x) == reg
2468 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2471 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2472 Note that the INSN should be deleted *after* removing dead edges, so
2473 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2474 but not for a (set (pc) (label_ref FOO)). */
2476 static void
2477 update_cfg_for_uncondjump (rtx_insn *insn)
2479 basic_block bb = BLOCK_FOR_INSN (insn);
2480 gcc_assert (BB_END (bb) == insn);
2482 purge_dead_edges (bb);
2484 delete_insn (insn);
2485 if (EDGE_COUNT (bb->succs) == 1)
2487 rtx_insn *insn;
2489 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2491 /* Remove barriers from the footer if there are any. */
2492 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2493 if (BARRIER_P (insn))
2495 if (PREV_INSN (insn))
2496 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2497 else
2498 BB_FOOTER (bb) = NEXT_INSN (insn);
2499 if (NEXT_INSN (insn))
2500 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2502 else if (LABEL_P (insn))
2503 break;
2507 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2508 by an arbitrary number of CLOBBERs. */
2509 static bool
2510 is_parallel_of_n_reg_sets (rtx pat, int n)
2512 if (GET_CODE (pat) != PARALLEL)
2513 return false;
2515 int len = XVECLEN (pat, 0);
2516 if (len < n)
2517 return false;
2519 int i;
2520 for (i = 0; i < n; i++)
2521 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2522 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2523 return false;
2524 for ( ; i < len; i++)
2525 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2526 return false;
2528 return true;
2531 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2532 CLOBBERs), can be split into individual SETs in that order, without
2533 changing semantics. */
2534 static bool
2535 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2537 if (!insn_nothrow_p (insn))
2538 return false;
2540 rtx pat = PATTERN (insn);
2542 int i, j;
2543 for (i = 0; i < n; i++)
2545 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2546 return false;
2548 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2550 for (j = i + 1; j < n; j++)
2551 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2552 return false;
2555 return true;
2558 /* Try to combine the insns I0, I1 and I2 into I3.
2559 Here I0, I1 and I2 appear earlier than I3.
2560 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2563 If we are combining more than two insns and the resulting insn is not
2564 recognized, try splitting it into two insns. If that happens, I2 and I3
2565 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2566 Otherwise, I0, I1 and I2 are pseudo-deleted.
2568 Return 0 if the combination does not work. Then nothing is changed.
2569 If we did the combination, return the insn at which combine should
2570 resume scanning.
2572 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2573 new direct jump instruction.
2575 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2576 been I3 passed to an earlier try_combine within the same basic
2577 block. */
2579 static rtx_insn *
2580 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2581 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2583 /* New patterns for I3 and I2, respectively. */
2584 rtx newpat, newi2pat = 0;
2585 rtvec newpat_vec_with_clobbers = 0;
2586 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2587 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2588 dead. */
2589 int added_sets_0, added_sets_1, added_sets_2;
2590 /* Total number of SETs to put into I3. */
2591 int total_sets;
2592 /* Nonzero if I2's or I1's body now appears in I3. */
2593 int i2_is_used = 0, i1_is_used = 0;
2594 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2595 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2596 /* Contains I3 if the destination of I3 is used in its source, which means
2597 that the old life of I3 is being killed. If that usage is placed into
2598 I2 and not in I3, a REG_DEAD note must be made. */
2599 rtx i3dest_killed = 0;
2600 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2601 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2602 /* Copy of SET_SRC of I1 and I0, if needed. */
2603 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2604 /* Set if I2DEST was reused as a scratch register. */
2605 bool i2scratch = false;
2606 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2607 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2608 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2609 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2610 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2611 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2612 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2613 /* Notes that must be added to REG_NOTES in I3 and I2. */
2614 rtx new_i3_notes, new_i2_notes;
2615 /* Notes that we substituted I3 into I2 instead of the normal case. */
2616 int i3_subst_into_i2 = 0;
2617 /* Notes that I1, I2 or I3 is a MULT operation. */
2618 int have_mult = 0;
2619 int swap_i2i3 = 0;
2620 int changed_i3_dest = 0;
2622 int maxreg;
2623 rtx_insn *temp_insn;
2624 rtx temp_expr;
2625 struct insn_link *link;
2626 rtx other_pat = 0;
2627 rtx new_other_notes;
2628 int i;
2630 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2631 never be). */
2632 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2633 return 0;
2635 /* Only try four-insn combinations when there's high likelihood of
2636 success. Look for simple insns, such as loads of constants or
2637 binary operations involving a constant. */
2638 if (i0)
2640 int i;
2641 int ngood = 0;
2642 int nshift = 0;
2643 rtx set0, set3;
2645 if (!flag_expensive_optimizations)
2646 return 0;
2648 for (i = 0; i < 4; i++)
2650 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2651 rtx set = single_set (insn);
2652 rtx src;
2653 if (!set)
2654 continue;
2655 src = SET_SRC (set);
2656 if (CONSTANT_P (src))
2658 ngood += 2;
2659 break;
2661 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2662 ngood++;
2663 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2664 || GET_CODE (src) == LSHIFTRT)
2665 nshift++;
2668 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2669 are likely manipulating its value. Ideally we'll be able to combine
2670 all four insns into a bitfield insertion of some kind.
2672 Note the source in I0 might be inside a sign/zero extension and the
2673 memory modes in I0 and I3 might be different. So extract the address
2674 from the destination of I3 and search for it in the source of I0.
2676 In the event that there's a match but the source/dest do not actually
2677 refer to the same memory, the worst that happens is we try some
2678 combinations that we wouldn't have otherwise. */
2679 if ((set0 = single_set (i0))
2680 /* Ensure the source of SET0 is a MEM, possibly buried inside
2681 an extension. */
2682 && (GET_CODE (SET_SRC (set0)) == MEM
2683 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2684 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2685 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2686 && (set3 = single_set (i3))
2687 /* Ensure the destination of SET3 is a MEM. */
2688 && GET_CODE (SET_DEST (set3)) == MEM
2689 /* Would it be better to extract the base address for the MEM
2690 in SET3 and look for that? I don't have cases where it matters
2691 but I could envision such cases. */
2692 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2693 ngood += 2;
2695 if (ngood < 2 && nshift < 2)
2696 return 0;
2699 /* Exit early if one of the insns involved can't be used for
2700 combinations. */
2701 if (CALL_P (i2)
2702 || (i1 && CALL_P (i1))
2703 || (i0 && CALL_P (i0))
2704 || cant_combine_insn_p (i3)
2705 || cant_combine_insn_p (i2)
2706 || (i1 && cant_combine_insn_p (i1))
2707 || (i0 && cant_combine_insn_p (i0))
2708 || likely_spilled_retval_p (i3))
2709 return 0;
2711 combine_attempts++;
2712 undobuf.other_insn = 0;
2714 /* Reset the hard register usage information. */
2715 CLEAR_HARD_REG_SET (newpat_used_regs);
2717 if (dump_file && (dump_flags & TDF_DETAILS))
2719 if (i0)
2720 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2721 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2722 else if (i1)
2723 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2724 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2725 else
2726 fprintf (dump_file, "\nTrying %d -> %d:\n",
2727 INSN_UID (i2), INSN_UID (i3));
2730 /* If multiple insns feed into one of I2 or I3, they can be in any
2731 order. To simplify the code below, reorder them in sequence. */
2732 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2733 std::swap (i0, i2);
2734 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2735 std::swap (i0, i1);
2736 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2737 std::swap (i1, i2);
2739 added_links_insn = 0;
2741 /* First check for one important special case that the code below will
2742 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2743 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2744 we may be able to replace that destination with the destination of I3.
2745 This occurs in the common code where we compute both a quotient and
2746 remainder into a structure, in which case we want to do the computation
2747 directly into the structure to avoid register-register copies.
2749 Note that this case handles both multiple sets in I2 and also cases
2750 where I2 has a number of CLOBBERs inside the PARALLEL.
2752 We make very conservative checks below and only try to handle the
2753 most common cases of this. For example, we only handle the case
2754 where I2 and I3 are adjacent to avoid making difficult register
2755 usage tests. */
2757 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2758 && REG_P (SET_SRC (PATTERN (i3)))
2759 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2760 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2761 && GET_CODE (PATTERN (i2)) == PARALLEL
2762 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2763 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2764 below would need to check what is inside (and reg_overlap_mentioned_p
2765 doesn't support those codes anyway). Don't allow those destinations;
2766 the resulting insn isn't likely to be recognized anyway. */
2767 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2768 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2769 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2770 SET_DEST (PATTERN (i3)))
2771 && next_active_insn (i2) == i3)
2773 rtx p2 = PATTERN (i2);
2775 /* Make sure that the destination of I3,
2776 which we are going to substitute into one output of I2,
2777 is not used within another output of I2. We must avoid making this:
2778 (parallel [(set (mem (reg 69)) ...)
2779 (set (reg 69) ...)])
2780 which is not well-defined as to order of actions.
2781 (Besides, reload can't handle output reloads for this.)
2783 The problem can also happen if the dest of I3 is a memory ref,
2784 if another dest in I2 is an indirect memory ref. */
2785 for (i = 0; i < XVECLEN (p2, 0); i++)
2786 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2787 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2788 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2789 SET_DEST (XVECEXP (p2, 0, i))))
2790 break;
2792 /* Make sure this PARALLEL is not an asm. We do not allow combining
2793 that usually (see can_combine_p), so do not here either. */
2794 for (i = 0; i < XVECLEN (p2, 0); i++)
2795 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2796 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2797 break;
2799 if (i == XVECLEN (p2, 0))
2800 for (i = 0; i < XVECLEN (p2, 0); i++)
2801 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2802 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2804 combine_merges++;
2806 subst_insn = i3;
2807 subst_low_luid = DF_INSN_LUID (i2);
2809 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2810 i2src = SET_SRC (XVECEXP (p2, 0, i));
2811 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2812 i2dest_killed = dead_or_set_p (i2, i2dest);
2814 /* Replace the dest in I2 with our dest and make the resulting
2815 insn the new pattern for I3. Then skip to where we validate
2816 the pattern. Everything was set up above. */
2817 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2818 newpat = p2;
2819 i3_subst_into_i2 = 1;
2820 goto validate_replacement;
2824 /* If I2 is setting a pseudo to a constant and I3 is setting some
2825 sub-part of it to another constant, merge them by making a new
2826 constant. */
2827 if (i1 == 0
2828 && (temp_expr = single_set (i2)) != 0
2829 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2830 && GET_CODE (PATTERN (i3)) == SET
2831 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2832 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2834 rtx dest = SET_DEST (PATTERN (i3));
2835 int offset = -1;
2836 int width = 0;
2838 if (GET_CODE (dest) == ZERO_EXTRACT)
2840 if (CONST_INT_P (XEXP (dest, 1))
2841 && CONST_INT_P (XEXP (dest, 2)))
2843 width = INTVAL (XEXP (dest, 1));
2844 offset = INTVAL (XEXP (dest, 2));
2845 dest = XEXP (dest, 0);
2846 if (BITS_BIG_ENDIAN)
2847 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2850 else
2852 if (GET_CODE (dest) == STRICT_LOW_PART)
2853 dest = XEXP (dest, 0);
2854 width = GET_MODE_PRECISION (GET_MODE (dest));
2855 offset = 0;
2858 if (offset >= 0)
2860 /* If this is the low part, we're done. */
2861 if (subreg_lowpart_p (dest))
2863 /* Handle the case where inner is twice the size of outer. */
2864 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2865 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2866 offset += GET_MODE_PRECISION (GET_MODE (dest));
2867 /* Otherwise give up for now. */
2868 else
2869 offset = -1;
2872 if (offset >= 0)
2874 rtx inner = SET_SRC (PATTERN (i3));
2875 rtx outer = SET_SRC (temp_expr);
2877 wide_int o
2878 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2879 std::make_pair (inner, GET_MODE (dest)),
2880 offset, width);
2882 combine_merges++;
2883 subst_insn = i3;
2884 subst_low_luid = DF_INSN_LUID (i2);
2885 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2886 i2dest = SET_DEST (temp_expr);
2887 i2dest_killed = dead_or_set_p (i2, i2dest);
2889 /* Replace the source in I2 with the new constant and make the
2890 resulting insn the new pattern for I3. Then skip to where we
2891 validate the pattern. Everything was set up above. */
2892 SUBST (SET_SRC (temp_expr),
2893 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2895 newpat = PATTERN (i2);
2897 /* The dest of I3 has been replaced with the dest of I2. */
2898 changed_i3_dest = 1;
2899 goto validate_replacement;
2903 /* If we have no I1 and I2 looks like:
2904 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2905 (set Y OP)])
2906 make up a dummy I1 that is
2907 (set Y OP)
2908 and change I2 to be
2909 (set (reg:CC X) (compare:CC Y (const_int 0)))
2911 (We can ignore any trailing CLOBBERs.)
2913 This undoes a previous combination and allows us to match a branch-and-
2914 decrement insn. */
2916 if (!HAVE_cc0 && i1 == 0
2917 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2918 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2919 == MODE_CC)
2920 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2921 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2922 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2923 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2924 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2925 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2927 /* We make I1 with the same INSN_UID as I2. This gives it
2928 the same DF_INSN_LUID for value tracking. Our fake I1 will
2929 never appear in the insn stream so giving it the same INSN_UID
2930 as I2 will not cause a problem. */
2932 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2933 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2934 -1, NULL_RTX);
2935 INSN_UID (i1) = INSN_UID (i2);
2937 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2938 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2939 SET_DEST (PATTERN (i1)));
2940 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2941 SUBST_LINK (LOG_LINKS (i2),
2942 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2945 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2946 make those two SETs separate I1 and I2 insns, and make an I0 that is
2947 the original I1. */
2948 if (!HAVE_cc0 && i0 == 0
2949 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2950 && can_split_parallel_of_n_reg_sets (i2, 2)
2951 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2952 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2954 /* If there is no I1, there is no I0 either. */
2955 i0 = i1;
2957 /* We make I1 with the same INSN_UID as I2. This gives it
2958 the same DF_INSN_LUID for value tracking. Our fake I1 will
2959 never appear in the insn stream so giving it the same INSN_UID
2960 as I2 will not cause a problem. */
2962 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2963 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2964 -1, NULL_RTX);
2965 INSN_UID (i1) = INSN_UID (i2);
2967 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2970 /* Verify that I2 and I1 are valid for combining. */
2971 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2972 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2973 &i1dest, &i1src))
2974 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2975 &i0dest, &i0src)))
2977 undo_all ();
2978 return 0;
2981 /* Record whether I2DEST is used in I2SRC and similarly for the other
2982 cases. Knowing this will help in register status updating below. */
2983 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2984 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2985 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2986 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2987 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2988 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2989 i2dest_killed = dead_or_set_p (i2, i2dest);
2990 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2991 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2993 /* For the earlier insns, determine which of the subsequent ones they
2994 feed. */
2995 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2996 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2997 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2998 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2999 && reg_overlap_mentioned_p (i0dest, i2src))));
3001 /* Ensure that I3's pattern can be the destination of combines. */
3002 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3003 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3004 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3005 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3006 &i3dest_killed))
3008 undo_all ();
3009 return 0;
3012 /* See if any of the insns is a MULT operation. Unless one is, we will
3013 reject a combination that is, since it must be slower. Be conservative
3014 here. */
3015 if (GET_CODE (i2src) == MULT
3016 || (i1 != 0 && GET_CODE (i1src) == MULT)
3017 || (i0 != 0 && GET_CODE (i0src) == MULT)
3018 || (GET_CODE (PATTERN (i3)) == SET
3019 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3020 have_mult = 1;
3022 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3023 We used to do this EXCEPT in one case: I3 has a post-inc in an
3024 output operand. However, that exception can give rise to insns like
3025 mov r3,(r3)+
3026 which is a famous insn on the PDP-11 where the value of r3 used as the
3027 source was model-dependent. Avoid this sort of thing. */
3029 #if 0
3030 if (!(GET_CODE (PATTERN (i3)) == SET
3031 && REG_P (SET_SRC (PATTERN (i3)))
3032 && MEM_P (SET_DEST (PATTERN (i3)))
3033 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3034 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3035 /* It's not the exception. */
3036 #endif
3037 if (AUTO_INC_DEC)
3039 rtx link;
3040 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3041 if (REG_NOTE_KIND (link) == REG_INC
3042 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3043 || (i1 != 0
3044 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3046 undo_all ();
3047 return 0;
3051 /* See if the SETs in I1 or I2 need to be kept around in the merged
3052 instruction: whenever the value set there is still needed past I3.
3053 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3055 For the SET in I1, we have two cases: if I1 and I2 independently feed
3056 into I3, the set in I1 needs to be kept around unless I1DEST dies
3057 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3058 in I1 needs to be kept around unless I1DEST dies or is set in either
3059 I2 or I3. The same considerations apply to I0. */
3061 added_sets_2 = !dead_or_set_p (i3, i2dest);
3063 if (i1)
3064 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3065 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3066 else
3067 added_sets_1 = 0;
3069 if (i0)
3070 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3071 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3072 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3073 && dead_or_set_p (i2, i0dest)));
3074 else
3075 added_sets_0 = 0;
3077 /* We are about to copy insns for the case where they need to be kept
3078 around. Check that they can be copied in the merged instruction. */
3080 if (targetm.cannot_copy_insn_p
3081 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3082 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3083 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3085 undo_all ();
3086 return 0;
3089 /* If the set in I2 needs to be kept around, we must make a copy of
3090 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3091 PATTERN (I2), we are only substituting for the original I1DEST, not into
3092 an already-substituted copy. This also prevents making self-referential
3093 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3094 I2DEST. */
3096 if (added_sets_2)
3098 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3099 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3100 else
3101 i2pat = copy_rtx (PATTERN (i2));
3104 if (added_sets_1)
3106 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3107 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3108 else
3109 i1pat = copy_rtx (PATTERN (i1));
3112 if (added_sets_0)
3114 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3115 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3116 else
3117 i0pat = copy_rtx (PATTERN (i0));
3120 combine_merges++;
3122 /* Substitute in the latest insn for the regs set by the earlier ones. */
3124 maxreg = max_reg_num ();
3126 subst_insn = i3;
3128 /* Many machines that don't use CC0 have insns that can both perform an
3129 arithmetic operation and set the condition code. These operations will
3130 be represented as a PARALLEL with the first element of the vector
3131 being a COMPARE of an arithmetic operation with the constant zero.
3132 The second element of the vector will set some pseudo to the result
3133 of the same arithmetic operation. If we simplify the COMPARE, we won't
3134 match such a pattern and so will generate an extra insn. Here we test
3135 for this case, where both the comparison and the operation result are
3136 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3137 I2SRC. Later we will make the PARALLEL that contains I2. */
3139 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3140 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3141 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3142 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3144 rtx newpat_dest;
3145 rtx *cc_use_loc = NULL;
3146 rtx_insn *cc_use_insn = NULL;
3147 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3148 machine_mode compare_mode, orig_compare_mode;
3149 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3151 newpat = PATTERN (i3);
3152 newpat_dest = SET_DEST (newpat);
3153 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3155 if (undobuf.other_insn == 0
3156 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3157 &cc_use_insn)))
3159 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3160 compare_code = simplify_compare_const (compare_code,
3161 GET_MODE (i2dest), op0, &op1);
3162 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3165 /* Do the rest only if op1 is const0_rtx, which may be the
3166 result of simplification. */
3167 if (op1 == const0_rtx)
3169 /* If a single use of the CC is found, prepare to modify it
3170 when SELECT_CC_MODE returns a new CC-class mode, or when
3171 the above simplify_compare_const() returned a new comparison
3172 operator. undobuf.other_insn is assigned the CC use insn
3173 when modifying it. */
3174 if (cc_use_loc)
3176 #ifdef SELECT_CC_MODE
3177 machine_mode new_mode
3178 = SELECT_CC_MODE (compare_code, op0, op1);
3179 if (new_mode != orig_compare_mode
3180 && can_change_dest_mode (SET_DEST (newpat),
3181 added_sets_2, new_mode))
3183 unsigned int regno = REGNO (newpat_dest);
3184 compare_mode = new_mode;
3185 if (regno < FIRST_PSEUDO_REGISTER)
3186 newpat_dest = gen_rtx_REG (compare_mode, regno);
3187 else
3189 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3190 newpat_dest = regno_reg_rtx[regno];
3193 #endif
3194 /* Cases for modifying the CC-using comparison. */
3195 if (compare_code != orig_compare_code
3196 /* ??? Do we need to verify the zero rtx? */
3197 && XEXP (*cc_use_loc, 1) == const0_rtx)
3199 /* Replace cc_use_loc with entire new RTX. */
3200 SUBST (*cc_use_loc,
3201 gen_rtx_fmt_ee (compare_code, compare_mode,
3202 newpat_dest, const0_rtx));
3203 undobuf.other_insn = cc_use_insn;
3205 else if (compare_mode != orig_compare_mode)
3207 /* Just replace the CC reg with a new mode. */
3208 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3209 undobuf.other_insn = cc_use_insn;
3213 /* Now we modify the current newpat:
3214 First, SET_DEST(newpat) is updated if the CC mode has been
3215 altered. For targets without SELECT_CC_MODE, this should be
3216 optimized away. */
3217 if (compare_mode != orig_compare_mode)
3218 SUBST (SET_DEST (newpat), newpat_dest);
3219 /* This is always done to propagate i2src into newpat. */
3220 SUBST (SET_SRC (newpat),
3221 gen_rtx_COMPARE (compare_mode, op0, op1));
3222 /* Create new version of i2pat if needed; the below PARALLEL
3223 creation needs this to work correctly. */
3224 if (! rtx_equal_p (i2src, op0))
3225 i2pat = gen_rtx_SET (i2dest, op0);
3226 i2_is_used = 1;
3230 if (i2_is_used == 0)
3232 /* It is possible that the source of I2 or I1 may be performing
3233 an unneeded operation, such as a ZERO_EXTEND of something
3234 that is known to have the high part zero. Handle that case
3235 by letting subst look at the inner insns.
3237 Another way to do this would be to have a function that tries
3238 to simplify a single insn instead of merging two or more
3239 insns. We don't do this because of the potential of infinite
3240 loops and because of the potential extra memory required.
3241 However, doing it the way we are is a bit of a kludge and
3242 doesn't catch all cases.
3244 But only do this if -fexpensive-optimizations since it slows
3245 things down and doesn't usually win.
3247 This is not done in the COMPARE case above because the
3248 unmodified I2PAT is used in the PARALLEL and so a pattern
3249 with a modified I2SRC would not match. */
3251 if (flag_expensive_optimizations)
3253 /* Pass pc_rtx so no substitutions are done, just
3254 simplifications. */
3255 if (i1)
3257 subst_low_luid = DF_INSN_LUID (i1);
3258 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3261 subst_low_luid = DF_INSN_LUID (i2);
3262 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3265 n_occurrences = 0; /* `subst' counts here */
3266 subst_low_luid = DF_INSN_LUID (i2);
3268 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3269 copy of I2SRC each time we substitute it, in order to avoid creating
3270 self-referential RTL when we will be substituting I1SRC for I1DEST
3271 later. Likewise if I0 feeds into I2, either directly or indirectly
3272 through I1, and I0DEST is in I0SRC. */
3273 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3274 (i1_feeds_i2_n && i1dest_in_i1src)
3275 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3276 && i0dest_in_i0src));
3277 substed_i2 = 1;
3279 /* Record whether I2's body now appears within I3's body. */
3280 i2_is_used = n_occurrences;
3283 /* If we already got a failure, don't try to do more. Otherwise, try to
3284 substitute I1 if we have it. */
3286 if (i1 && GET_CODE (newpat) != CLOBBER)
3288 /* Check that an autoincrement side-effect on I1 has not been lost.
3289 This happens if I1DEST is mentioned in I2 and dies there, and
3290 has disappeared from the new pattern. */
3291 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3292 && i1_feeds_i2_n
3293 && dead_or_set_p (i2, i1dest)
3294 && !reg_overlap_mentioned_p (i1dest, newpat))
3295 /* Before we can do this substitution, we must redo the test done
3296 above (see detailed comments there) that ensures I1DEST isn't
3297 mentioned in any SETs in NEWPAT that are field assignments. */
3298 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3299 0, 0, 0))
3301 undo_all ();
3302 return 0;
3305 n_occurrences = 0;
3306 subst_low_luid = DF_INSN_LUID (i1);
3308 /* If the following substitution will modify I1SRC, make a copy of it
3309 for the case where it is substituted for I1DEST in I2PAT later. */
3310 if (added_sets_2 && i1_feeds_i2_n)
3311 i1src_copy = copy_rtx (i1src);
3313 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3314 copy of I1SRC each time we substitute it, in order to avoid creating
3315 self-referential RTL when we will be substituting I0SRC for I0DEST
3316 later. */
3317 newpat = subst (newpat, i1dest, i1src, 0, 0,
3318 i0_feeds_i1_n && i0dest_in_i0src);
3319 substed_i1 = 1;
3321 /* Record whether I1's body now appears within I3's body. */
3322 i1_is_used = n_occurrences;
3325 /* Likewise for I0 if we have it. */
3327 if (i0 && GET_CODE (newpat) != CLOBBER)
3329 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3330 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3331 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3332 && !reg_overlap_mentioned_p (i0dest, newpat))
3333 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3334 0, 0, 0))
3336 undo_all ();
3337 return 0;
3340 /* If the following substitution will modify I0SRC, make a copy of it
3341 for the case where it is substituted for I0DEST in I1PAT later. */
3342 if (added_sets_1 && i0_feeds_i1_n)
3343 i0src_copy = copy_rtx (i0src);
3344 /* And a copy for I0DEST in I2PAT substitution. */
3345 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3346 || (i0_feeds_i2_n)))
3347 i0src_copy2 = copy_rtx (i0src);
3349 n_occurrences = 0;
3350 subst_low_luid = DF_INSN_LUID (i0);
3351 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3352 substed_i0 = 1;
3355 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3356 to count all the ways that I2SRC and I1SRC can be used. */
3357 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3358 && i2_is_used + added_sets_2 > 1)
3359 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3360 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3361 > 1))
3362 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3363 && (n_occurrences + added_sets_0
3364 + (added_sets_1 && i0_feeds_i1_n)
3365 + (added_sets_2 && i0_feeds_i2_n)
3366 > 1))
3367 /* Fail if we tried to make a new register. */
3368 || max_reg_num () != maxreg
3369 /* Fail if we couldn't do something and have a CLOBBER. */
3370 || GET_CODE (newpat) == CLOBBER
3371 /* Fail if this new pattern is a MULT and we didn't have one before
3372 at the outer level. */
3373 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3374 && ! have_mult))
3376 undo_all ();
3377 return 0;
3380 /* If the actions of the earlier insns must be kept
3381 in addition to substituting them into the latest one,
3382 we must make a new PARALLEL for the latest insn
3383 to hold additional the SETs. */
3385 if (added_sets_0 || added_sets_1 || added_sets_2)
3387 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3388 combine_extras++;
3390 if (GET_CODE (newpat) == PARALLEL)
3392 rtvec old = XVEC (newpat, 0);
3393 total_sets = XVECLEN (newpat, 0) + extra_sets;
3394 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3395 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3396 sizeof (old->elem[0]) * old->num_elem);
3398 else
3400 rtx old = newpat;
3401 total_sets = 1 + extra_sets;
3402 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3403 XVECEXP (newpat, 0, 0) = old;
3406 if (added_sets_0)
3407 XVECEXP (newpat, 0, --total_sets) = i0pat;
3409 if (added_sets_1)
3411 rtx t = i1pat;
3412 if (i0_feeds_i1_n)
3413 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3415 XVECEXP (newpat, 0, --total_sets) = t;
3417 if (added_sets_2)
3419 rtx t = i2pat;
3420 if (i1_feeds_i2_n)
3421 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3422 i0_feeds_i1_n && i0dest_in_i0src);
3423 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3424 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3426 XVECEXP (newpat, 0, --total_sets) = t;
3430 validate_replacement:
3432 /* Note which hard regs this insn has as inputs. */
3433 mark_used_regs_combine (newpat);
3435 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3436 consider splitting this pattern, we might need these clobbers. */
3437 if (i1 && GET_CODE (newpat) == PARALLEL
3438 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3440 int len = XVECLEN (newpat, 0);
3442 newpat_vec_with_clobbers = rtvec_alloc (len);
3443 for (i = 0; i < len; i++)
3444 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3447 /* We have recognized nothing yet. */
3448 insn_code_number = -1;
3450 /* See if this is a PARALLEL of two SETs where one SET's destination is
3451 a register that is unused and this isn't marked as an instruction that
3452 might trap in an EH region. In that case, we just need the other SET.
3453 We prefer this over the PARALLEL.
3455 This can occur when simplifying a divmod insn. We *must* test for this
3456 case here because the code below that splits two independent SETs doesn't
3457 handle this case correctly when it updates the register status.
3459 It's pointless doing this if we originally had two sets, one from
3460 i3, and one from i2. Combining then splitting the parallel results
3461 in the original i2 again plus an invalid insn (which we delete).
3462 The net effect is only to move instructions around, which makes
3463 debug info less accurate. */
3465 if (!(added_sets_2 && i1 == 0)
3466 && is_parallel_of_n_reg_sets (newpat, 2)
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 && (!HAVE_cc0 || REG_P (i2dest))
3657 /* We need I2DEST in the proper mode. If it is a hard register
3658 or the only use of a pseudo, we can change its mode.
3659 Make sure we don't change a hard register to have a mode that
3660 isn't valid for it, or change the number of registers. */
3661 && (GET_MODE (*split) == GET_MODE (i2dest)
3662 || GET_MODE (*split) == VOIDmode
3663 || can_change_dest_mode (i2dest, added_sets_2,
3664 GET_MODE (*split)))
3665 && (next_nonnote_nondebug_insn (i2) == i3
3666 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3667 /* We can't overwrite I2DEST if its value is still used by
3668 NEWPAT. */
3669 && ! reg_referenced_p (i2dest, newpat))
3671 rtx newdest = i2dest;
3672 enum rtx_code split_code = GET_CODE (*split);
3673 machine_mode split_mode = GET_MODE (*split);
3674 bool subst_done = false;
3675 newi2pat = NULL_RTX;
3677 i2scratch = true;
3679 /* *SPLIT may be part of I2SRC, so make sure we have the
3680 original expression around for later debug processing.
3681 We should not need I2SRC any more in other cases. */
3682 if (MAY_HAVE_DEBUG_INSNS)
3683 i2src = copy_rtx (i2src);
3684 else
3685 i2src = NULL;
3687 /* Get NEWDEST as a register in the proper mode. We have already
3688 validated that we can do this. */
3689 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3691 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3692 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3693 else
3695 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3696 newdest = regno_reg_rtx[REGNO (i2dest)];
3700 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3701 an ASHIFT. This can occur if it was inside a PLUS and hence
3702 appeared to be a memory address. This is a kludge. */
3703 if (split_code == MULT
3704 && CONST_INT_P (XEXP (*split, 1))
3705 && INTVAL (XEXP (*split, 1)) > 0
3706 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3708 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3709 XEXP (*split, 0), GEN_INT (i)));
3710 /* Update split_code because we may not have a multiply
3711 anymore. */
3712 split_code = GET_CODE (*split);
3715 /* Similarly for (plus (mult FOO (const_int pow2))). */
3716 if (split_code == PLUS
3717 && GET_CODE (XEXP (*split, 0)) == MULT
3718 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3719 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3720 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3722 rtx nsplit = XEXP (*split, 0);
3723 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3724 XEXP (nsplit, 0), GEN_INT (i)));
3725 /* Update split_code because we may not have a multiply
3726 anymore. */
3727 split_code = GET_CODE (*split);
3730 #ifdef INSN_SCHEDULING
3731 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3732 be written as a ZERO_EXTEND. */
3733 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3735 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3736 what it really is. */
3737 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3738 == SIGN_EXTEND)
3739 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3740 SUBREG_REG (*split)));
3741 else
3742 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3743 SUBREG_REG (*split)));
3745 #endif
3747 /* Attempt to split binary operators using arithmetic identities. */
3748 if (BINARY_P (SET_SRC (newpat))
3749 && split_mode == GET_MODE (SET_SRC (newpat))
3750 && ! side_effects_p (SET_SRC (newpat)))
3752 rtx setsrc = SET_SRC (newpat);
3753 machine_mode mode = GET_MODE (setsrc);
3754 enum rtx_code code = GET_CODE (setsrc);
3755 rtx src_op0 = XEXP (setsrc, 0);
3756 rtx src_op1 = XEXP (setsrc, 1);
3758 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3759 if (rtx_equal_p (src_op0, src_op1))
3761 newi2pat = gen_rtx_SET (newdest, src_op0);
3762 SUBST (XEXP (setsrc, 0), newdest);
3763 SUBST (XEXP (setsrc, 1), newdest);
3764 subst_done = true;
3766 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3767 else if ((code == PLUS || code == MULT)
3768 && GET_CODE (src_op0) == code
3769 && GET_CODE (XEXP (src_op0, 0)) == code
3770 && (INTEGRAL_MODE_P (mode)
3771 || (FLOAT_MODE_P (mode)
3772 && flag_unsafe_math_optimizations)))
3774 rtx p = XEXP (XEXP (src_op0, 0), 0);
3775 rtx q = XEXP (XEXP (src_op0, 0), 1);
3776 rtx r = XEXP (src_op0, 1);
3777 rtx s = src_op1;
3779 /* Split both "((X op Y) op X) op Y" and
3780 "((X op Y) op Y) op X" as "T op T" where T is
3781 "X op Y". */
3782 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3783 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3785 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3786 SUBST (XEXP (setsrc, 0), newdest);
3787 SUBST (XEXP (setsrc, 1), newdest);
3788 subst_done = true;
3790 /* Split "((X op X) op Y) op Y)" as "T op T" where
3791 T is "X op Y". */
3792 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3794 rtx tmp = simplify_gen_binary (code, mode, p, r);
3795 newi2pat = gen_rtx_SET (newdest, tmp);
3796 SUBST (XEXP (setsrc, 0), newdest);
3797 SUBST (XEXP (setsrc, 1), newdest);
3798 subst_done = true;
3803 if (!subst_done)
3805 newi2pat = gen_rtx_SET (newdest, *split);
3806 SUBST (*split, newdest);
3809 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3811 /* recog_for_combine might have added CLOBBERs to newi2pat.
3812 Make sure NEWPAT does not depend on the clobbered regs. */
3813 if (GET_CODE (newi2pat) == PARALLEL)
3814 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3815 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3817 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3818 if (reg_overlap_mentioned_p (reg, newpat))
3820 undo_all ();
3821 return 0;
3825 /* If the split point was a MULT and we didn't have one before,
3826 don't use one now. */
3827 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3828 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3832 /* Check for a case where we loaded from memory in a narrow mode and
3833 then sign extended it, but we need both registers. In that case,
3834 we have a PARALLEL with both loads from the same memory location.
3835 We can split this into a load from memory followed by a register-register
3836 copy. This saves at least one insn, more if register allocation can
3837 eliminate the copy.
3839 We cannot do this if the destination of the first assignment is a
3840 condition code register or cc0. We eliminate this case by making sure
3841 the SET_DEST and SET_SRC have the same mode.
3843 We cannot do this if the destination of the second assignment is
3844 a register that we have already assumed is zero-extended. Similarly
3845 for a SUBREG of such a register. */
3847 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3848 && GET_CODE (newpat) == PARALLEL
3849 && XVECLEN (newpat, 0) == 2
3850 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3851 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3852 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3853 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3854 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3855 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3856 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3857 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3858 DF_INSN_LUID (i2))
3859 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3860 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3861 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3862 (REG_P (temp_expr)
3863 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3864 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3865 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3866 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3867 != GET_MODE_MASK (word_mode))))
3868 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3869 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3870 (REG_P (temp_expr)
3871 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3872 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3873 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3874 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3875 != GET_MODE_MASK (word_mode)))))
3876 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3877 SET_SRC (XVECEXP (newpat, 0, 1)))
3878 && ! find_reg_note (i3, REG_UNUSED,
3879 SET_DEST (XVECEXP (newpat, 0, 0))))
3881 rtx ni2dest;
3883 newi2pat = XVECEXP (newpat, 0, 0);
3884 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3885 newpat = XVECEXP (newpat, 0, 1);
3886 SUBST (SET_SRC (newpat),
3887 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3888 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3890 if (i2_code_number >= 0)
3891 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3893 if (insn_code_number >= 0)
3894 swap_i2i3 = 1;
3897 /* Similarly, check for a case where we have a PARALLEL of two independent
3898 SETs but we started with three insns. In this case, we can do the sets
3899 as two separate insns. This case occurs when some SET allows two
3900 other insns to combine, but the destination of that SET is still live.
3902 Also do this if we started with two insns and (at least) one of the
3903 resulting sets is a noop; this noop will be deleted later. */
3905 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3906 && GET_CODE (newpat) == PARALLEL
3907 && XVECLEN (newpat, 0) == 2
3908 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3909 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3910 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3911 || set_noop_p (XVECEXP (newpat, 0, 1)))
3912 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3913 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3914 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3915 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3916 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3917 XVECEXP (newpat, 0, 0))
3918 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3919 XVECEXP (newpat, 0, 1))
3920 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3921 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3923 rtx set0 = XVECEXP (newpat, 0, 0);
3924 rtx set1 = XVECEXP (newpat, 0, 1);
3926 /* Normally, it doesn't matter which of the two is done first,
3927 but the one that references cc0 can't be the second, and
3928 one which uses any regs/memory set in between i2 and i3 can't
3929 be first. The PARALLEL might also have been pre-existing in i3,
3930 so we need to make sure that we won't wrongly hoist a SET to i2
3931 that would conflict with a death note present in there. */
3932 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3933 && !(REG_P (SET_DEST (set1))
3934 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3935 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3936 && find_reg_note (i2, REG_DEAD,
3937 SUBREG_REG (SET_DEST (set1))))
3938 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
3939 /* If I3 is a jump, ensure that set0 is a jump so that
3940 we do not create invalid RTL. */
3941 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3944 newi2pat = set1;
3945 newpat = set0;
3947 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3948 && !(REG_P (SET_DEST (set0))
3949 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3950 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3951 && find_reg_note (i2, REG_DEAD,
3952 SUBREG_REG (SET_DEST (set0))))
3953 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
3954 /* If I3 is a jump, ensure that set1 is a jump so that
3955 we do not create invalid RTL. */
3956 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3959 newi2pat = set0;
3960 newpat = set1;
3962 else
3964 undo_all ();
3965 return 0;
3968 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3970 if (i2_code_number >= 0)
3972 /* recog_for_combine might have added CLOBBERs to newi2pat.
3973 Make sure NEWPAT does not depend on the clobbered regs. */
3974 if (GET_CODE (newi2pat) == PARALLEL)
3976 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3977 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3979 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3980 if (reg_overlap_mentioned_p (reg, newpat))
3982 undo_all ();
3983 return 0;
3988 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3992 /* If it still isn't recognized, fail and change things back the way they
3993 were. */
3994 if ((insn_code_number < 0
3995 /* Is the result a reasonable ASM_OPERANDS? */
3996 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3998 undo_all ();
3999 return 0;
4002 /* If we had to change another insn, make sure it is valid also. */
4003 if (undobuf.other_insn)
4005 CLEAR_HARD_REG_SET (newpat_used_regs);
4007 other_pat = PATTERN (undobuf.other_insn);
4008 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4009 &new_other_notes);
4011 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4013 undo_all ();
4014 return 0;
4018 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4019 they are adjacent to each other or not. */
4020 if (HAVE_cc0)
4022 rtx_insn *p = prev_nonnote_insn (i3);
4023 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4024 && sets_cc0_p (newi2pat))
4026 undo_all ();
4027 return 0;
4031 /* Only allow this combination if insn_rtx_costs reports that the
4032 replacement instructions are cheaper than the originals. */
4033 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4035 undo_all ();
4036 return 0;
4039 if (MAY_HAVE_DEBUG_INSNS)
4041 struct undo *undo;
4043 for (undo = undobuf.undos; undo; undo = undo->next)
4044 if (undo->kind == UNDO_MODE)
4046 rtx reg = *undo->where.r;
4047 machine_mode new_mode = GET_MODE (reg);
4048 machine_mode old_mode = undo->old_contents.m;
4050 /* Temporarily revert mode back. */
4051 adjust_reg_mode (reg, old_mode);
4053 if (reg == i2dest && i2scratch)
4055 /* If we used i2dest as a scratch register with a
4056 different mode, substitute it for the original
4057 i2src while its original mode is temporarily
4058 restored, and then clear i2scratch so that we don't
4059 do it again later. */
4060 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4061 this_basic_block);
4062 i2scratch = false;
4063 /* Put back the new mode. */
4064 adjust_reg_mode (reg, new_mode);
4066 else
4068 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4069 rtx_insn *first, *last;
4071 if (reg == i2dest)
4073 first = i2;
4074 last = last_combined_insn;
4076 else
4078 first = i3;
4079 last = undobuf.other_insn;
4080 gcc_assert (last);
4081 if (DF_INSN_LUID (last)
4082 < DF_INSN_LUID (last_combined_insn))
4083 last = last_combined_insn;
4086 /* We're dealing with a reg that changed mode but not
4087 meaning, so we want to turn it into a subreg for
4088 the new mode. However, because of REG sharing and
4089 because its mode had already changed, we have to do
4090 it in two steps. First, replace any debug uses of
4091 reg, with its original mode temporarily restored,
4092 with this copy we have created; then, replace the
4093 copy with the SUBREG of the original shared reg,
4094 once again changed to the new mode. */
4095 propagate_for_debug (first, last, reg, tempreg,
4096 this_basic_block);
4097 adjust_reg_mode (reg, new_mode);
4098 propagate_for_debug (first, last, tempreg,
4099 lowpart_subreg (old_mode, reg, new_mode),
4100 this_basic_block);
4105 /* If we will be able to accept this, we have made a
4106 change to the destination of I3. This requires us to
4107 do a few adjustments. */
4109 if (changed_i3_dest)
4111 PATTERN (i3) = newpat;
4112 adjust_for_new_dest (i3);
4115 /* We now know that we can do this combination. Merge the insns and
4116 update the status of registers and LOG_LINKS. */
4118 if (undobuf.other_insn)
4120 rtx note, next;
4122 PATTERN (undobuf.other_insn) = other_pat;
4124 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4125 ensure that they are still valid. Then add any non-duplicate
4126 notes added by recog_for_combine. */
4127 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4129 next = XEXP (note, 1);
4131 if ((REG_NOTE_KIND (note) == REG_DEAD
4132 && !reg_referenced_p (XEXP (note, 0),
4133 PATTERN (undobuf.other_insn)))
4134 ||(REG_NOTE_KIND (note) == REG_UNUSED
4135 && !reg_set_p (XEXP (note, 0),
4136 PATTERN (undobuf.other_insn))))
4137 remove_note (undobuf.other_insn, note);
4140 distribute_notes (new_other_notes, undobuf.other_insn,
4141 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4142 NULL_RTX);
4145 if (swap_i2i3)
4147 rtx_insn *insn;
4148 struct insn_link *link;
4149 rtx ni2dest;
4151 /* I3 now uses what used to be its destination and which is now
4152 I2's destination. This requires us to do a few adjustments. */
4153 PATTERN (i3) = newpat;
4154 adjust_for_new_dest (i3);
4156 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4157 so we still will.
4159 However, some later insn might be using I2's dest and have
4160 a LOG_LINK pointing at I3. We must remove this link.
4161 The simplest way to remove the link is to point it at I1,
4162 which we know will be a NOTE. */
4164 /* newi2pat is usually a SET here; however, recog_for_combine might
4165 have added some clobbers. */
4166 if (GET_CODE (newi2pat) == PARALLEL)
4167 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4168 else
4169 ni2dest = SET_DEST (newi2pat);
4171 for (insn = NEXT_INSN (i3);
4172 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4173 || insn != BB_HEAD (this_basic_block->next_bb));
4174 insn = NEXT_INSN (insn))
4176 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4178 FOR_EACH_LOG_LINK (link, insn)
4179 if (link->insn == i3)
4180 link->insn = i1;
4182 break;
4188 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4189 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4190 rtx midnotes = 0;
4191 int from_luid;
4192 /* Compute which registers we expect to eliminate. newi2pat may be setting
4193 either i3dest or i2dest, so we must check it. */
4194 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4195 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4196 || !i2dest_killed
4197 ? 0 : i2dest);
4198 /* For i1, we need to compute both local elimination and global
4199 elimination information with respect to newi2pat because i1dest
4200 may be the same as i3dest, in which case newi2pat may be setting
4201 i1dest. Global information is used when distributing REG_DEAD
4202 note for i2 and i3, in which case it does matter if newi2pat sets
4203 i1dest or not.
4205 Local information is used when distributing REG_DEAD note for i1,
4206 in which case it doesn't matter if newi2pat sets i1dest or not.
4207 See PR62151, if we have four insns combination:
4208 i0: r0 <- i0src
4209 i1: r1 <- i1src (using r0)
4210 REG_DEAD (r0)
4211 i2: r0 <- i2src (using r1)
4212 i3: r3 <- i3src (using r0)
4213 ix: using r0
4214 From i1's point of view, r0 is eliminated, no matter if it is set
4215 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4216 should be discarded.
4218 Note local information only affects cases in forms like "I1->I2->I3",
4219 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4220 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4221 i0dest anyway. */
4222 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4223 || !i1dest_killed
4224 ? 0 : i1dest);
4225 rtx elim_i1 = (local_elim_i1 == 0
4226 || (newi2pat && reg_set_p (i1dest, newi2pat))
4227 ? 0 : i1dest);
4228 /* Same case as i1. */
4229 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4230 ? 0 : i0dest);
4231 rtx elim_i0 = (local_elim_i0 == 0
4232 || (newi2pat && reg_set_p (i0dest, newi2pat))
4233 ? 0 : i0dest);
4235 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4236 clear them. */
4237 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4238 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4239 if (i1)
4240 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4241 if (i0)
4242 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4244 /* Ensure that we do not have something that should not be shared but
4245 occurs multiple times in the new insns. Check this by first
4246 resetting all the `used' flags and then copying anything is shared. */
4248 reset_used_flags (i3notes);
4249 reset_used_flags (i2notes);
4250 reset_used_flags (i1notes);
4251 reset_used_flags (i0notes);
4252 reset_used_flags (newpat);
4253 reset_used_flags (newi2pat);
4254 if (undobuf.other_insn)
4255 reset_used_flags (PATTERN (undobuf.other_insn));
4257 i3notes = copy_rtx_if_shared (i3notes);
4258 i2notes = copy_rtx_if_shared (i2notes);
4259 i1notes = copy_rtx_if_shared (i1notes);
4260 i0notes = copy_rtx_if_shared (i0notes);
4261 newpat = copy_rtx_if_shared (newpat);
4262 newi2pat = copy_rtx_if_shared (newi2pat);
4263 if (undobuf.other_insn)
4264 reset_used_flags (PATTERN (undobuf.other_insn));
4266 INSN_CODE (i3) = insn_code_number;
4267 PATTERN (i3) = newpat;
4269 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4271 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4273 reset_used_flags (call_usage);
4274 call_usage = copy_rtx (call_usage);
4276 if (substed_i2)
4278 /* I2SRC must still be meaningful at this point. Some splitting
4279 operations can invalidate I2SRC, but those operations do not
4280 apply to calls. */
4281 gcc_assert (i2src);
4282 replace_rtx (call_usage, i2dest, i2src);
4285 if (substed_i1)
4286 replace_rtx (call_usage, i1dest, i1src);
4287 if (substed_i0)
4288 replace_rtx (call_usage, i0dest, i0src);
4290 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4293 if (undobuf.other_insn)
4294 INSN_CODE (undobuf.other_insn) = other_code_number;
4296 /* We had one special case above where I2 had more than one set and
4297 we replaced a destination of one of those sets with the destination
4298 of I3. In that case, we have to update LOG_LINKS of insns later
4299 in this basic block. Note that this (expensive) case is rare.
4301 Also, in this case, we must pretend that all REG_NOTEs for I2
4302 actually came from I3, so that REG_UNUSED notes from I2 will be
4303 properly handled. */
4305 if (i3_subst_into_i2)
4307 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4308 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4309 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4310 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4311 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4312 && ! find_reg_note (i2, REG_UNUSED,
4313 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4314 for (temp_insn = NEXT_INSN (i2);
4315 temp_insn
4316 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4317 || BB_HEAD (this_basic_block) != temp_insn);
4318 temp_insn = NEXT_INSN (temp_insn))
4319 if (temp_insn != i3 && INSN_P (temp_insn))
4320 FOR_EACH_LOG_LINK (link, temp_insn)
4321 if (link->insn == i2)
4322 link->insn = i3;
4324 if (i3notes)
4326 rtx link = i3notes;
4327 while (XEXP (link, 1))
4328 link = XEXP (link, 1);
4329 XEXP (link, 1) = i2notes;
4331 else
4332 i3notes = i2notes;
4333 i2notes = 0;
4336 LOG_LINKS (i3) = NULL;
4337 REG_NOTES (i3) = 0;
4338 LOG_LINKS (i2) = NULL;
4339 REG_NOTES (i2) = 0;
4341 if (newi2pat)
4343 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4344 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4345 this_basic_block);
4346 INSN_CODE (i2) = i2_code_number;
4347 PATTERN (i2) = newi2pat;
4349 else
4351 if (MAY_HAVE_DEBUG_INSNS && i2src)
4352 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4353 this_basic_block);
4354 SET_INSN_DELETED (i2);
4357 if (i1)
4359 LOG_LINKS (i1) = NULL;
4360 REG_NOTES (i1) = 0;
4361 if (MAY_HAVE_DEBUG_INSNS)
4362 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4363 this_basic_block);
4364 SET_INSN_DELETED (i1);
4367 if (i0)
4369 LOG_LINKS (i0) = NULL;
4370 REG_NOTES (i0) = 0;
4371 if (MAY_HAVE_DEBUG_INSNS)
4372 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4373 this_basic_block);
4374 SET_INSN_DELETED (i0);
4377 /* Get death notes for everything that is now used in either I3 or
4378 I2 and used to die in a previous insn. If we built two new
4379 patterns, move from I1 to I2 then I2 to I3 so that we get the
4380 proper movement on registers that I2 modifies. */
4382 if (i0)
4383 from_luid = DF_INSN_LUID (i0);
4384 else if (i1)
4385 from_luid = DF_INSN_LUID (i1);
4386 else
4387 from_luid = DF_INSN_LUID (i2);
4388 if (newi2pat)
4389 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4390 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4392 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4393 if (i3notes)
4394 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4395 elim_i2, elim_i1, elim_i0);
4396 if (i2notes)
4397 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4398 elim_i2, elim_i1, elim_i0);
4399 if (i1notes)
4400 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4401 elim_i2, local_elim_i1, local_elim_i0);
4402 if (i0notes)
4403 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4404 elim_i2, elim_i1, local_elim_i0);
4405 if (midnotes)
4406 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4407 elim_i2, elim_i1, elim_i0);
4409 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4410 know these are REG_UNUSED and want them to go to the desired insn,
4411 so we always pass it as i3. */
4413 if (newi2pat && new_i2_notes)
4414 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4415 NULL_RTX);
4417 if (new_i3_notes)
4418 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4419 NULL_RTX);
4421 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4422 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4423 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4424 in that case, it might delete I2. Similarly for I2 and I1.
4425 Show an additional death due to the REG_DEAD note we make here. If
4426 we discard it in distribute_notes, we will decrement it again. */
4428 if (i3dest_killed)
4430 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4431 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4432 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4433 elim_i1, elim_i0);
4434 else
4435 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4436 elim_i2, elim_i1, elim_i0);
4439 if (i2dest_in_i2src)
4441 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4442 if (newi2pat && reg_set_p (i2dest, newi2pat))
4443 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4444 NULL_RTX, NULL_RTX);
4445 else
4446 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4447 NULL_RTX, NULL_RTX, NULL_RTX);
4450 if (i1dest_in_i1src)
4452 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4453 if (newi2pat && reg_set_p (i1dest, newi2pat))
4454 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4455 NULL_RTX, NULL_RTX);
4456 else
4457 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4458 NULL_RTX, NULL_RTX, NULL_RTX);
4461 if (i0dest_in_i0src)
4463 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4464 if (newi2pat && reg_set_p (i0dest, newi2pat))
4465 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4466 NULL_RTX, NULL_RTX);
4467 else
4468 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4469 NULL_RTX, NULL_RTX, NULL_RTX);
4472 distribute_links (i3links);
4473 distribute_links (i2links);
4474 distribute_links (i1links);
4475 distribute_links (i0links);
4477 if (REG_P (i2dest))
4479 struct insn_link *link;
4480 rtx_insn *i2_insn = 0;
4481 rtx i2_val = 0, set;
4483 /* The insn that used to set this register doesn't exist, and
4484 this life of the register may not exist either. See if one of
4485 I3's links points to an insn that sets I2DEST. If it does,
4486 that is now the last known value for I2DEST. If we don't update
4487 this and I2 set the register to a value that depended on its old
4488 contents, we will get confused. If this insn is used, thing
4489 will be set correctly in combine_instructions. */
4490 FOR_EACH_LOG_LINK (link, i3)
4491 if ((set = single_set (link->insn)) != 0
4492 && rtx_equal_p (i2dest, SET_DEST (set)))
4493 i2_insn = link->insn, i2_val = SET_SRC (set);
4495 record_value_for_reg (i2dest, i2_insn, i2_val);
4497 /* If the reg formerly set in I2 died only once and that was in I3,
4498 zero its use count so it won't make `reload' do any work. */
4499 if (! added_sets_2
4500 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4501 && ! i2dest_in_i2src
4502 && REGNO (i2dest) < reg_n_sets_max)
4503 INC_REG_N_SETS (REGNO (i2dest), -1);
4506 if (i1 && REG_P (i1dest))
4508 struct insn_link *link;
4509 rtx_insn *i1_insn = 0;
4510 rtx i1_val = 0, set;
4512 FOR_EACH_LOG_LINK (link, i3)
4513 if ((set = single_set (link->insn)) != 0
4514 && rtx_equal_p (i1dest, SET_DEST (set)))
4515 i1_insn = link->insn, i1_val = SET_SRC (set);
4517 record_value_for_reg (i1dest, i1_insn, i1_val);
4519 if (! added_sets_1
4520 && ! i1dest_in_i1src
4521 && REGNO (i1dest) < reg_n_sets_max)
4522 INC_REG_N_SETS (REGNO (i1dest), -1);
4525 if (i0 && REG_P (i0dest))
4527 struct insn_link *link;
4528 rtx_insn *i0_insn = 0;
4529 rtx i0_val = 0, set;
4531 FOR_EACH_LOG_LINK (link, i3)
4532 if ((set = single_set (link->insn)) != 0
4533 && rtx_equal_p (i0dest, SET_DEST (set)))
4534 i0_insn = link->insn, i0_val = SET_SRC (set);
4536 record_value_for_reg (i0dest, i0_insn, i0_val);
4538 if (! added_sets_0
4539 && ! i0dest_in_i0src
4540 && REGNO (i0dest) < reg_n_sets_max)
4541 INC_REG_N_SETS (REGNO (i0dest), -1);
4544 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4545 been made to this insn. The order is important, because newi2pat
4546 can affect nonzero_bits of newpat. */
4547 if (newi2pat)
4548 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4549 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4552 if (undobuf.other_insn != NULL_RTX)
4554 if (dump_file)
4556 fprintf (dump_file, "modifying other_insn ");
4557 dump_insn_slim (dump_file, undobuf.other_insn);
4559 df_insn_rescan (undobuf.other_insn);
4562 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4564 if (dump_file)
4566 fprintf (dump_file, "modifying insn i0 ");
4567 dump_insn_slim (dump_file, i0);
4569 df_insn_rescan (i0);
4572 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4574 if (dump_file)
4576 fprintf (dump_file, "modifying insn i1 ");
4577 dump_insn_slim (dump_file, i1);
4579 df_insn_rescan (i1);
4582 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4584 if (dump_file)
4586 fprintf (dump_file, "modifying insn i2 ");
4587 dump_insn_slim (dump_file, i2);
4589 df_insn_rescan (i2);
4592 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4594 if (dump_file)
4596 fprintf (dump_file, "modifying insn i3 ");
4597 dump_insn_slim (dump_file, i3);
4599 df_insn_rescan (i3);
4602 /* Set new_direct_jump_p if a new return or simple jump instruction
4603 has been created. Adjust the CFG accordingly. */
4604 if (returnjump_p (i3) || any_uncondjump_p (i3))
4606 *new_direct_jump_p = 1;
4607 mark_jump_label (PATTERN (i3), i3, 0);
4608 update_cfg_for_uncondjump (i3);
4611 if (undobuf.other_insn != NULL_RTX
4612 && (returnjump_p (undobuf.other_insn)
4613 || any_uncondjump_p (undobuf.other_insn)))
4615 *new_direct_jump_p = 1;
4616 update_cfg_for_uncondjump (undobuf.other_insn);
4619 /* A noop might also need cleaning up of CFG, if it comes from the
4620 simplification of a jump. */
4621 if (JUMP_P (i3)
4622 && GET_CODE (newpat) == SET
4623 && SET_SRC (newpat) == pc_rtx
4624 && SET_DEST (newpat) == pc_rtx)
4626 *new_direct_jump_p = 1;
4627 update_cfg_for_uncondjump (i3);
4630 if (undobuf.other_insn != NULL_RTX
4631 && JUMP_P (undobuf.other_insn)
4632 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4633 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4634 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4636 *new_direct_jump_p = 1;
4637 update_cfg_for_uncondjump (undobuf.other_insn);
4640 combine_successes++;
4641 undo_commit ();
4643 if (added_links_insn
4644 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4645 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4646 return added_links_insn;
4647 else
4648 return newi2pat ? i2 : i3;
4651 /* Get a marker for undoing to the current state. */
4653 static void *
4654 get_undo_marker (void)
4656 return undobuf.undos;
4659 /* Undo the modifications up to the marker. */
4661 static void
4662 undo_to_marker (void *marker)
4664 struct undo *undo, *next;
4666 for (undo = undobuf.undos; undo != marker; undo = next)
4668 gcc_assert (undo);
4670 next = undo->next;
4671 switch (undo->kind)
4673 case UNDO_RTX:
4674 *undo->where.r = undo->old_contents.r;
4675 break;
4676 case UNDO_INT:
4677 *undo->where.i = undo->old_contents.i;
4678 break;
4679 case UNDO_MODE:
4680 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4681 break;
4682 case UNDO_LINKS:
4683 *undo->where.l = undo->old_contents.l;
4684 break;
4685 default:
4686 gcc_unreachable ();
4689 undo->next = undobuf.frees;
4690 undobuf.frees = undo;
4693 undobuf.undos = (struct undo *) marker;
4696 /* Undo all the modifications recorded in undobuf. */
4698 static void
4699 undo_all (void)
4701 undo_to_marker (0);
4704 /* We've committed to accepting the changes we made. Move all
4705 of the undos to the free list. */
4707 static void
4708 undo_commit (void)
4710 struct undo *undo, *next;
4712 for (undo = undobuf.undos; undo; undo = next)
4714 next = undo->next;
4715 undo->next = undobuf.frees;
4716 undobuf.frees = undo;
4718 undobuf.undos = 0;
4721 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4722 where we have an arithmetic expression and return that point. LOC will
4723 be inside INSN.
4725 try_combine will call this function to see if an insn can be split into
4726 two insns. */
4728 static rtx *
4729 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4731 rtx x = *loc;
4732 enum rtx_code code = GET_CODE (x);
4733 rtx *split;
4734 unsigned HOST_WIDE_INT len = 0;
4735 HOST_WIDE_INT pos = 0;
4736 int unsignedp = 0;
4737 rtx inner = NULL_RTX;
4739 /* First special-case some codes. */
4740 switch (code)
4742 case SUBREG:
4743 #ifdef INSN_SCHEDULING
4744 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4745 point. */
4746 if (MEM_P (SUBREG_REG (x)))
4747 return loc;
4748 #endif
4749 return find_split_point (&SUBREG_REG (x), insn, false);
4751 case MEM:
4752 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4753 using LO_SUM and HIGH. */
4754 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4755 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4757 machine_mode address_mode = get_address_mode (x);
4759 SUBST (XEXP (x, 0),
4760 gen_rtx_LO_SUM (address_mode,
4761 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4762 XEXP (x, 0)));
4763 return &XEXP (XEXP (x, 0), 0);
4766 /* If we have a PLUS whose second operand is a constant and the
4767 address is not valid, perhaps will can split it up using
4768 the machine-specific way to split large constants. We use
4769 the first pseudo-reg (one of the virtual regs) as a placeholder;
4770 it will not remain in the result. */
4771 if (GET_CODE (XEXP (x, 0)) == PLUS
4772 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4773 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4774 MEM_ADDR_SPACE (x)))
4776 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4777 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4778 subst_insn);
4780 /* This should have produced two insns, each of which sets our
4781 placeholder. If the source of the second is a valid address,
4782 we can make put both sources together and make a split point
4783 in the middle. */
4785 if (seq
4786 && NEXT_INSN (seq) != NULL_RTX
4787 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4788 && NONJUMP_INSN_P (seq)
4789 && GET_CODE (PATTERN (seq)) == SET
4790 && SET_DEST (PATTERN (seq)) == reg
4791 && ! reg_mentioned_p (reg,
4792 SET_SRC (PATTERN (seq)))
4793 && NONJUMP_INSN_P (NEXT_INSN (seq))
4794 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4795 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4796 && memory_address_addr_space_p
4797 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4798 MEM_ADDR_SPACE (x)))
4800 rtx src1 = SET_SRC (PATTERN (seq));
4801 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4803 /* Replace the placeholder in SRC2 with SRC1. If we can
4804 find where in SRC2 it was placed, that can become our
4805 split point and we can replace this address with SRC2.
4806 Just try two obvious places. */
4808 src2 = replace_rtx (src2, reg, src1);
4809 split = 0;
4810 if (XEXP (src2, 0) == src1)
4811 split = &XEXP (src2, 0);
4812 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4813 && XEXP (XEXP (src2, 0), 0) == src1)
4814 split = &XEXP (XEXP (src2, 0), 0);
4816 if (split)
4818 SUBST (XEXP (x, 0), src2);
4819 return split;
4823 /* If that didn't work, perhaps the first operand is complex and
4824 needs to be computed separately, so make a split point there.
4825 This will occur on machines that just support REG + CONST
4826 and have a constant moved through some previous computation. */
4828 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4829 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4830 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4831 return &XEXP (XEXP (x, 0), 0);
4834 /* If we have a PLUS whose first operand is complex, try computing it
4835 separately by making a split there. */
4836 if (GET_CODE (XEXP (x, 0)) == PLUS
4837 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4838 MEM_ADDR_SPACE (x))
4839 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4840 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4841 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4842 return &XEXP (XEXP (x, 0), 0);
4843 break;
4845 case SET:
4846 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4847 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4848 we need to put the operand into a register. So split at that
4849 point. */
4851 if (SET_DEST (x) == cc0_rtx
4852 && GET_CODE (SET_SRC (x)) != COMPARE
4853 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4854 && !OBJECT_P (SET_SRC (x))
4855 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4856 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4857 return &SET_SRC (x);
4859 /* See if we can split SET_SRC as it stands. */
4860 split = find_split_point (&SET_SRC (x), insn, true);
4861 if (split && split != &SET_SRC (x))
4862 return split;
4864 /* See if we can split SET_DEST as it stands. */
4865 split = find_split_point (&SET_DEST (x), insn, false);
4866 if (split && split != &SET_DEST (x))
4867 return split;
4869 /* See if this is a bitfield assignment with everything constant. If
4870 so, this is an IOR of an AND, so split it into that. */
4871 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4872 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4873 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4874 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4875 && CONST_INT_P (SET_SRC (x))
4876 && ((INTVAL (XEXP (SET_DEST (x), 1))
4877 + INTVAL (XEXP (SET_DEST (x), 2)))
4878 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4879 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4881 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4882 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4883 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4884 rtx dest = XEXP (SET_DEST (x), 0);
4885 machine_mode mode = GET_MODE (dest);
4886 unsigned HOST_WIDE_INT mask
4887 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4888 rtx or_mask;
4890 if (BITS_BIG_ENDIAN)
4891 pos = GET_MODE_PRECISION (mode) - len - pos;
4893 or_mask = gen_int_mode (src << pos, mode);
4894 if (src == mask)
4895 SUBST (SET_SRC (x),
4896 simplify_gen_binary (IOR, mode, dest, or_mask));
4897 else
4899 rtx negmask = gen_int_mode (~(mask << pos), mode);
4900 SUBST (SET_SRC (x),
4901 simplify_gen_binary (IOR, mode,
4902 simplify_gen_binary (AND, mode,
4903 dest, negmask),
4904 or_mask));
4907 SUBST (SET_DEST (x), dest);
4909 split = find_split_point (&SET_SRC (x), insn, true);
4910 if (split && split != &SET_SRC (x))
4911 return split;
4914 /* Otherwise, see if this is an operation that we can split into two.
4915 If so, try to split that. */
4916 code = GET_CODE (SET_SRC (x));
4918 switch (code)
4920 case AND:
4921 /* If we are AND'ing with a large constant that is only a single
4922 bit and the result is only being used in a context where we
4923 need to know if it is zero or nonzero, replace it with a bit
4924 extraction. This will avoid the large constant, which might
4925 have taken more than one insn to make. If the constant were
4926 not a valid argument to the AND but took only one insn to make,
4927 this is no worse, but if it took more than one insn, it will
4928 be better. */
4930 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4931 && REG_P (XEXP (SET_SRC (x), 0))
4932 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4933 && REG_P (SET_DEST (x))
4934 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4935 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4936 && XEXP (*split, 0) == SET_DEST (x)
4937 && XEXP (*split, 1) == const0_rtx)
4939 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4940 XEXP (SET_SRC (x), 0),
4941 pos, NULL_RTX, 1, 1, 0, 0);
4942 if (extraction != 0)
4944 SUBST (SET_SRC (x), extraction);
4945 return find_split_point (loc, insn, false);
4948 break;
4950 case NE:
4951 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4952 is known to be on, this can be converted into a NEG of a shift. */
4953 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4954 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4955 && 1 <= (pos = exact_log2
4956 (nonzero_bits (XEXP (SET_SRC (x), 0),
4957 GET_MODE (XEXP (SET_SRC (x), 0))))))
4959 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4961 SUBST (SET_SRC (x),
4962 gen_rtx_NEG (mode,
4963 gen_rtx_LSHIFTRT (mode,
4964 XEXP (SET_SRC (x), 0),
4965 GEN_INT (pos))));
4967 split = find_split_point (&SET_SRC (x), insn, true);
4968 if (split && split != &SET_SRC (x))
4969 return split;
4971 break;
4973 case SIGN_EXTEND:
4974 inner = XEXP (SET_SRC (x), 0);
4976 /* We can't optimize if either mode is a partial integer
4977 mode as we don't know how many bits are significant
4978 in those modes. */
4979 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4980 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4981 break;
4983 pos = 0;
4984 len = GET_MODE_PRECISION (GET_MODE (inner));
4985 unsignedp = 0;
4986 break;
4988 case SIGN_EXTRACT:
4989 case ZERO_EXTRACT:
4990 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4991 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4993 inner = XEXP (SET_SRC (x), 0);
4994 len = INTVAL (XEXP (SET_SRC (x), 1));
4995 pos = INTVAL (XEXP (SET_SRC (x), 2));
4997 if (BITS_BIG_ENDIAN)
4998 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4999 unsignedp = (code == ZERO_EXTRACT);
5001 break;
5003 default:
5004 break;
5007 if (len && pos >= 0
5008 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
5010 machine_mode mode = GET_MODE (SET_SRC (x));
5012 /* For unsigned, we have a choice of a shift followed by an
5013 AND or two shifts. Use two shifts for field sizes where the
5014 constant might be too large. We assume here that we can
5015 always at least get 8-bit constants in an AND insn, which is
5016 true for every current RISC. */
5018 if (unsignedp && len <= 8)
5020 unsigned HOST_WIDE_INT mask
5021 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
5022 SUBST (SET_SRC (x),
5023 gen_rtx_AND (mode,
5024 gen_rtx_LSHIFTRT
5025 (mode, gen_lowpart (mode, inner),
5026 GEN_INT (pos)),
5027 gen_int_mode (mask, mode)));
5029 split = find_split_point (&SET_SRC (x), insn, true);
5030 if (split && split != &SET_SRC (x))
5031 return split;
5033 else
5035 SUBST (SET_SRC (x),
5036 gen_rtx_fmt_ee
5037 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5038 gen_rtx_ASHIFT (mode,
5039 gen_lowpart (mode, inner),
5040 GEN_INT (GET_MODE_PRECISION (mode)
5041 - len - pos)),
5042 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5044 split = find_split_point (&SET_SRC (x), insn, true);
5045 if (split && split != &SET_SRC (x))
5046 return split;
5050 /* See if this is a simple operation with a constant as the second
5051 operand. It might be that this constant is out of range and hence
5052 could be used as a split point. */
5053 if (BINARY_P (SET_SRC (x))
5054 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5055 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5056 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5057 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5058 return &XEXP (SET_SRC (x), 1);
5060 /* Finally, see if this is a simple operation with its first operand
5061 not in a register. The operation might require this operand in a
5062 register, so return it as a split point. We can always do this
5063 because if the first operand were another operation, we would have
5064 already found it as a split point. */
5065 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5066 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5067 return &XEXP (SET_SRC (x), 0);
5069 return 0;
5071 case AND:
5072 case IOR:
5073 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5074 it is better to write this as (not (ior A B)) so we can split it.
5075 Similarly for IOR. */
5076 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5078 SUBST (*loc,
5079 gen_rtx_NOT (GET_MODE (x),
5080 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5081 GET_MODE (x),
5082 XEXP (XEXP (x, 0), 0),
5083 XEXP (XEXP (x, 1), 0))));
5084 return find_split_point (loc, insn, set_src);
5087 /* Many RISC machines have a large set of logical insns. If the
5088 second operand is a NOT, put it first so we will try to split the
5089 other operand first. */
5090 if (GET_CODE (XEXP (x, 1)) == NOT)
5092 rtx tem = XEXP (x, 0);
5093 SUBST (XEXP (x, 0), XEXP (x, 1));
5094 SUBST (XEXP (x, 1), tem);
5096 break;
5098 case PLUS:
5099 case MINUS:
5100 /* Canonicalization can produce (minus A (mult B C)), where C is a
5101 constant. It may be better to try splitting (plus (mult B -C) A)
5102 instead if this isn't a multiply by a power of two. */
5103 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5104 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5105 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5107 machine_mode mode = GET_MODE (x);
5108 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5109 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5110 SUBST (*loc, gen_rtx_PLUS (mode,
5111 gen_rtx_MULT (mode,
5112 XEXP (XEXP (x, 1), 0),
5113 gen_int_mode (other_int,
5114 mode)),
5115 XEXP (x, 0)));
5116 return find_split_point (loc, insn, set_src);
5119 /* Split at a multiply-accumulate instruction. However if this is
5120 the SET_SRC, we likely do not have such an instruction and it's
5121 worthless to try this split. */
5122 if (!set_src
5123 && (GET_CODE (XEXP (x, 0)) == MULT
5124 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5125 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5126 return loc;
5128 default:
5129 break;
5132 /* Otherwise, select our actions depending on our rtx class. */
5133 switch (GET_RTX_CLASS (code))
5135 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5136 case RTX_TERNARY:
5137 split = find_split_point (&XEXP (x, 2), insn, false);
5138 if (split)
5139 return split;
5140 /* ... fall through ... */
5141 case RTX_BIN_ARITH:
5142 case RTX_COMM_ARITH:
5143 case RTX_COMPARE:
5144 case RTX_COMM_COMPARE:
5145 split = find_split_point (&XEXP (x, 1), insn, false);
5146 if (split)
5147 return split;
5148 /* ... fall through ... */
5149 case RTX_UNARY:
5150 /* Some machines have (and (shift ...) ...) insns. If X is not
5151 an AND, but XEXP (X, 0) is, use it as our split point. */
5152 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5153 return &XEXP (x, 0);
5155 split = find_split_point (&XEXP (x, 0), insn, false);
5156 if (split)
5157 return split;
5158 return loc;
5160 default:
5161 /* Otherwise, we don't have a split point. */
5162 return 0;
5166 /* Throughout X, replace FROM with TO, and return the result.
5167 The result is TO if X is FROM;
5168 otherwise the result is X, but its contents may have been modified.
5169 If they were modified, a record was made in undobuf so that
5170 undo_all will (among other things) return X to its original state.
5172 If the number of changes necessary is too much to record to undo,
5173 the excess changes are not made, so the result is invalid.
5174 The changes already made can still be undone.
5175 undobuf.num_undo is incremented for such changes, so by testing that
5176 the caller can tell whether the result is valid.
5178 `n_occurrences' is incremented each time FROM is replaced.
5180 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5182 IN_COND is nonzero if we are at the top level of a condition.
5184 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5185 by copying if `n_occurrences' is nonzero. */
5187 static rtx
5188 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5190 enum rtx_code code = GET_CODE (x);
5191 machine_mode op0_mode = VOIDmode;
5192 const char *fmt;
5193 int len, i;
5194 rtx new_rtx;
5196 /* Two expressions are equal if they are identical copies of a shared
5197 RTX or if they are both registers with the same register number
5198 and mode. */
5200 #define COMBINE_RTX_EQUAL_P(X,Y) \
5201 ((X) == (Y) \
5202 || (REG_P (X) && REG_P (Y) \
5203 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5205 /* Do not substitute into clobbers of regs -- this will never result in
5206 valid RTL. */
5207 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5208 return x;
5210 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5212 n_occurrences++;
5213 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5216 /* If X and FROM are the same register but different modes, they
5217 will not have been seen as equal above. However, the log links code
5218 will make a LOG_LINKS entry for that case. If we do nothing, we
5219 will try to rerecognize our original insn and, when it succeeds,
5220 we will delete the feeding insn, which is incorrect.
5222 So force this insn not to match in this (rare) case. */
5223 if (! in_dest && code == REG && REG_P (from)
5224 && reg_overlap_mentioned_p (x, from))
5225 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5227 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5228 of which may contain things that can be combined. */
5229 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5230 return x;
5232 /* It is possible to have a subexpression appear twice in the insn.
5233 Suppose that FROM is a register that appears within TO.
5234 Then, after that subexpression has been scanned once by `subst',
5235 the second time it is scanned, TO may be found. If we were
5236 to scan TO here, we would find FROM within it and create a
5237 self-referent rtl structure which is completely wrong. */
5238 if (COMBINE_RTX_EQUAL_P (x, to))
5239 return to;
5241 /* Parallel asm_operands need special attention because all of the
5242 inputs are shared across the arms. Furthermore, unsharing the
5243 rtl results in recognition failures. Failure to handle this case
5244 specially can result in circular rtl.
5246 Solve this by doing a normal pass across the first entry of the
5247 parallel, and only processing the SET_DESTs of the subsequent
5248 entries. Ug. */
5250 if (code == PARALLEL
5251 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5252 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5254 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5256 /* If this substitution failed, this whole thing fails. */
5257 if (GET_CODE (new_rtx) == CLOBBER
5258 && XEXP (new_rtx, 0) == const0_rtx)
5259 return new_rtx;
5261 SUBST (XVECEXP (x, 0, 0), new_rtx);
5263 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5265 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5267 if (!REG_P (dest)
5268 && GET_CODE (dest) != CC0
5269 && GET_CODE (dest) != PC)
5271 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5273 /* If this substitution failed, this whole thing fails. */
5274 if (GET_CODE (new_rtx) == CLOBBER
5275 && XEXP (new_rtx, 0) == const0_rtx)
5276 return new_rtx;
5278 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5282 else
5284 len = GET_RTX_LENGTH (code);
5285 fmt = GET_RTX_FORMAT (code);
5287 /* We don't need to process a SET_DEST that is a register, CC0,
5288 or PC, so set up to skip this common case. All other cases
5289 where we want to suppress replacing something inside a
5290 SET_SRC are handled via the IN_DEST operand. */
5291 if (code == SET
5292 && (REG_P (SET_DEST (x))
5293 || GET_CODE (SET_DEST (x)) == CC0
5294 || GET_CODE (SET_DEST (x)) == PC))
5295 fmt = "ie";
5297 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5298 constant. */
5299 if (fmt[0] == 'e')
5300 op0_mode = GET_MODE (XEXP (x, 0));
5302 for (i = 0; i < len; i++)
5304 if (fmt[i] == 'E')
5306 int j;
5307 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5309 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5311 new_rtx = (unique_copy && n_occurrences
5312 ? copy_rtx (to) : to);
5313 n_occurrences++;
5315 else
5317 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5318 unique_copy);
5320 /* If this substitution failed, this whole thing
5321 fails. */
5322 if (GET_CODE (new_rtx) == CLOBBER
5323 && XEXP (new_rtx, 0) == const0_rtx)
5324 return new_rtx;
5327 SUBST (XVECEXP (x, i, j), new_rtx);
5330 else if (fmt[i] == 'e')
5332 /* If this is a register being set, ignore it. */
5333 new_rtx = XEXP (x, i);
5334 if (in_dest
5335 && i == 0
5336 && (((code == SUBREG || code == ZERO_EXTRACT)
5337 && REG_P (new_rtx))
5338 || code == STRICT_LOW_PART))
5341 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5343 /* In general, don't install a subreg involving two
5344 modes not tieable. It can worsen register
5345 allocation, and can even make invalid reload
5346 insns, since the reg inside may need to be copied
5347 from in the outside mode, and that may be invalid
5348 if it is an fp reg copied in integer mode.
5350 We allow two exceptions to this: It is valid if
5351 it is inside another SUBREG and the mode of that
5352 SUBREG and the mode of the inside of TO is
5353 tieable and it is valid if X is a SET that copies
5354 FROM to CC0. */
5356 if (GET_CODE (to) == SUBREG
5357 && ! MODES_TIEABLE_P (GET_MODE (to),
5358 GET_MODE (SUBREG_REG (to)))
5359 && ! (code == SUBREG
5360 && MODES_TIEABLE_P (GET_MODE (x),
5361 GET_MODE (SUBREG_REG (to))))
5362 && (!HAVE_cc0
5363 || (! (code == SET
5364 && i == 1
5365 && XEXP (x, 0) == cc0_rtx))))
5366 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5368 if (code == SUBREG
5369 && REG_P (to)
5370 && REGNO (to) < FIRST_PSEUDO_REGISTER
5371 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5372 SUBREG_BYTE (x),
5373 GET_MODE (x)) < 0)
5374 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5376 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5377 n_occurrences++;
5379 else
5380 /* If we are in a SET_DEST, suppress most cases unless we
5381 have gone inside a MEM, in which case we want to
5382 simplify the address. We assume here that things that
5383 are actually part of the destination have their inner
5384 parts in the first expression. This is true for SUBREG,
5385 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5386 things aside from REG and MEM that should appear in a
5387 SET_DEST. */
5388 new_rtx = subst (XEXP (x, i), from, to,
5389 (((in_dest
5390 && (code == SUBREG || code == STRICT_LOW_PART
5391 || code == ZERO_EXTRACT))
5392 || code == SET)
5393 && i == 0),
5394 code == IF_THEN_ELSE && i == 0,
5395 unique_copy);
5397 /* If we found that we will have to reject this combination,
5398 indicate that by returning the CLOBBER ourselves, rather than
5399 an expression containing it. This will speed things up as
5400 well as prevent accidents where two CLOBBERs are considered
5401 to be equal, thus producing an incorrect simplification. */
5403 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5404 return new_rtx;
5406 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5408 machine_mode mode = GET_MODE (x);
5410 x = simplify_subreg (GET_MODE (x), new_rtx,
5411 GET_MODE (SUBREG_REG (x)),
5412 SUBREG_BYTE (x));
5413 if (! x)
5414 x = gen_rtx_CLOBBER (mode, const0_rtx);
5416 else if (CONST_SCALAR_INT_P (new_rtx)
5417 && GET_CODE (x) == ZERO_EXTEND)
5419 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5420 new_rtx, GET_MODE (XEXP (x, 0)));
5421 gcc_assert (x);
5423 else
5424 SUBST (XEXP (x, i), new_rtx);
5429 /* Check if we are loading something from the constant pool via float
5430 extension; in this case we would undo compress_float_constant
5431 optimization and degenerate constant load to an immediate value. */
5432 if (GET_CODE (x) == FLOAT_EXTEND
5433 && MEM_P (XEXP (x, 0))
5434 && MEM_READONLY_P (XEXP (x, 0)))
5436 rtx tmp = avoid_constant_pool_reference (x);
5437 if (x != tmp)
5438 return x;
5441 /* Try to simplify X. If the simplification changed the code, it is likely
5442 that further simplification will help, so loop, but limit the number
5443 of repetitions that will be performed. */
5445 for (i = 0; i < 4; i++)
5447 /* If X is sufficiently simple, don't bother trying to do anything
5448 with it. */
5449 if (code != CONST_INT && code != REG && code != CLOBBER)
5450 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5452 if (GET_CODE (x) == code)
5453 break;
5455 code = GET_CODE (x);
5457 /* We no longer know the original mode of operand 0 since we
5458 have changed the form of X) */
5459 op0_mode = VOIDmode;
5462 return x;
5465 /* Simplify X, a piece of RTL. We just operate on the expression at the
5466 outer level; call `subst' to simplify recursively. Return the new
5467 expression.
5469 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5470 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5471 of a condition. */
5473 static rtx
5474 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5475 int in_cond)
5477 enum rtx_code code = GET_CODE (x);
5478 machine_mode mode = GET_MODE (x);
5479 rtx temp;
5480 int i;
5482 /* If this is a commutative operation, put a constant last and a complex
5483 expression first. We don't need to do this for comparisons here. */
5484 if (COMMUTATIVE_ARITH_P (x)
5485 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5487 temp = XEXP (x, 0);
5488 SUBST (XEXP (x, 0), XEXP (x, 1));
5489 SUBST (XEXP (x, 1), temp);
5492 /* Try to fold this expression in case we have constants that weren't
5493 present before. */
5494 temp = 0;
5495 switch (GET_RTX_CLASS (code))
5497 case RTX_UNARY:
5498 if (op0_mode == VOIDmode)
5499 op0_mode = GET_MODE (XEXP (x, 0));
5500 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5501 break;
5502 case RTX_COMPARE:
5503 case RTX_COMM_COMPARE:
5505 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5506 if (cmp_mode == VOIDmode)
5508 cmp_mode = GET_MODE (XEXP (x, 1));
5509 if (cmp_mode == VOIDmode)
5510 cmp_mode = op0_mode;
5512 temp = simplify_relational_operation (code, mode, cmp_mode,
5513 XEXP (x, 0), XEXP (x, 1));
5515 break;
5516 case RTX_COMM_ARITH:
5517 case RTX_BIN_ARITH:
5518 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5519 break;
5520 case RTX_BITFIELD_OPS:
5521 case RTX_TERNARY:
5522 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5523 XEXP (x, 1), XEXP (x, 2));
5524 break;
5525 default:
5526 break;
5529 if (temp)
5531 x = temp;
5532 code = GET_CODE (temp);
5533 op0_mode = VOIDmode;
5534 mode = GET_MODE (temp);
5537 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5538 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5539 things. Check for cases where both arms are testing the same
5540 condition.
5542 Don't do anything if all operands are very simple. */
5544 if ((BINARY_P (x)
5545 && ((!OBJECT_P (XEXP (x, 0))
5546 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5547 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5548 || (!OBJECT_P (XEXP (x, 1))
5549 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5550 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5551 || (UNARY_P (x)
5552 && (!OBJECT_P (XEXP (x, 0))
5553 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5554 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5556 rtx cond, true_rtx, false_rtx;
5558 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5559 if (cond != 0
5560 /* If everything is a comparison, what we have is highly unlikely
5561 to be simpler, so don't use it. */
5562 && ! (COMPARISON_P (x)
5563 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5565 rtx cop1 = const0_rtx;
5566 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5568 if (cond_code == NE && COMPARISON_P (cond))
5569 return x;
5571 /* Simplify the alternative arms; this may collapse the true and
5572 false arms to store-flag values. Be careful to use copy_rtx
5573 here since true_rtx or false_rtx might share RTL with x as a
5574 result of the if_then_else_cond call above. */
5575 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5576 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5578 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5579 is unlikely to be simpler. */
5580 if (general_operand (true_rtx, VOIDmode)
5581 && general_operand (false_rtx, VOIDmode))
5583 enum rtx_code reversed;
5585 /* Restarting if we generate a store-flag expression will cause
5586 us to loop. Just drop through in this case. */
5588 /* If the result values are STORE_FLAG_VALUE and zero, we can
5589 just make the comparison operation. */
5590 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5591 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5592 cond, cop1);
5593 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5594 && ((reversed = reversed_comparison_code_parts
5595 (cond_code, cond, cop1, NULL))
5596 != UNKNOWN))
5597 x = simplify_gen_relational (reversed, mode, VOIDmode,
5598 cond, cop1);
5600 /* Likewise, we can make the negate of a comparison operation
5601 if the result values are - STORE_FLAG_VALUE and zero. */
5602 else if (CONST_INT_P (true_rtx)
5603 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5604 && false_rtx == const0_rtx)
5605 x = simplify_gen_unary (NEG, mode,
5606 simplify_gen_relational (cond_code,
5607 mode, VOIDmode,
5608 cond, cop1),
5609 mode);
5610 else if (CONST_INT_P (false_rtx)
5611 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5612 && true_rtx == const0_rtx
5613 && ((reversed = reversed_comparison_code_parts
5614 (cond_code, cond, cop1, NULL))
5615 != UNKNOWN))
5616 x = simplify_gen_unary (NEG, mode,
5617 simplify_gen_relational (reversed,
5618 mode, VOIDmode,
5619 cond, cop1),
5620 mode);
5621 else
5622 return gen_rtx_IF_THEN_ELSE (mode,
5623 simplify_gen_relational (cond_code,
5624 mode,
5625 VOIDmode,
5626 cond,
5627 cop1),
5628 true_rtx, false_rtx);
5630 code = GET_CODE (x);
5631 op0_mode = VOIDmode;
5636 /* First see if we can apply the inverse distributive law. */
5637 if (code == PLUS || code == MINUS
5638 || code == AND || code == IOR || code == XOR)
5640 x = apply_distributive_law (x);
5641 code = GET_CODE (x);
5642 op0_mode = VOIDmode;
5645 /* If CODE is an associative operation not otherwise handled, see if we
5646 can associate some operands. This can win if they are constants or
5647 if they are logically related (i.e. (a & b) & a). */
5648 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5649 || code == AND || code == IOR || code == XOR
5650 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5651 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5652 || (flag_associative_math && FLOAT_MODE_P (mode))))
5654 if (GET_CODE (XEXP (x, 0)) == code)
5656 rtx other = XEXP (XEXP (x, 0), 0);
5657 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5658 rtx inner_op1 = XEXP (x, 1);
5659 rtx inner;
5661 /* Make sure we pass the constant operand if any as the second
5662 one if this is a commutative operation. */
5663 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5664 std::swap (inner_op0, inner_op1);
5665 inner = simplify_binary_operation (code == MINUS ? PLUS
5666 : code == DIV ? MULT
5667 : code,
5668 mode, inner_op0, inner_op1);
5670 /* For commutative operations, try the other pair if that one
5671 didn't simplify. */
5672 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5674 other = XEXP (XEXP (x, 0), 1);
5675 inner = simplify_binary_operation (code, mode,
5676 XEXP (XEXP (x, 0), 0),
5677 XEXP (x, 1));
5680 if (inner)
5681 return simplify_gen_binary (code, mode, other, inner);
5685 /* A little bit of algebraic simplification here. */
5686 switch (code)
5688 case MEM:
5689 /* Ensure that our address has any ASHIFTs converted to MULT in case
5690 address-recognizing predicates are called later. */
5691 temp = make_compound_operation (XEXP (x, 0), MEM);
5692 SUBST (XEXP (x, 0), temp);
5693 break;
5695 case SUBREG:
5696 if (op0_mode == VOIDmode)
5697 op0_mode = GET_MODE (SUBREG_REG (x));
5699 /* See if this can be moved to simplify_subreg. */
5700 if (CONSTANT_P (SUBREG_REG (x))
5701 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5702 /* Don't call gen_lowpart if the inner mode
5703 is VOIDmode and we cannot simplify it, as SUBREG without
5704 inner mode is invalid. */
5705 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5706 || gen_lowpart_common (mode, SUBREG_REG (x))))
5707 return gen_lowpart (mode, SUBREG_REG (x));
5709 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5710 break;
5712 rtx temp;
5713 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5714 SUBREG_BYTE (x));
5715 if (temp)
5716 return temp;
5718 /* If op is known to have all lower bits zero, the result is zero. */
5719 if (!in_dest
5720 && SCALAR_INT_MODE_P (mode)
5721 && SCALAR_INT_MODE_P (op0_mode)
5722 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5723 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5724 && HWI_COMPUTABLE_MODE_P (op0_mode)
5725 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5726 & GET_MODE_MASK (mode)) == 0)
5727 return CONST0_RTX (mode);
5730 /* Don't change the mode of the MEM if that would change the meaning
5731 of the address. */
5732 if (MEM_P (SUBREG_REG (x))
5733 && (MEM_VOLATILE_P (SUBREG_REG (x))
5734 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5735 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5736 return gen_rtx_CLOBBER (mode, const0_rtx);
5738 /* Note that we cannot do any narrowing for non-constants since
5739 we might have been counting on using the fact that some bits were
5740 zero. We now do this in the SET. */
5742 break;
5744 case NEG:
5745 temp = expand_compound_operation (XEXP (x, 0));
5747 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5748 replaced by (lshiftrt X C). This will convert
5749 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5751 if (GET_CODE (temp) == ASHIFTRT
5752 && CONST_INT_P (XEXP (temp, 1))
5753 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5754 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5755 INTVAL (XEXP (temp, 1)));
5757 /* If X has only a single bit that might be nonzero, say, bit I, convert
5758 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5759 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5760 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5761 or a SUBREG of one since we'd be making the expression more
5762 complex if it was just a register. */
5764 if (!REG_P (temp)
5765 && ! (GET_CODE (temp) == SUBREG
5766 && REG_P (SUBREG_REG (temp)))
5767 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5769 rtx temp1 = simplify_shift_const
5770 (NULL_RTX, ASHIFTRT, mode,
5771 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5772 GET_MODE_PRECISION (mode) - 1 - i),
5773 GET_MODE_PRECISION (mode) - 1 - i);
5775 /* If all we did was surround TEMP with the two shifts, we
5776 haven't improved anything, so don't use it. Otherwise,
5777 we are better off with TEMP1. */
5778 if (GET_CODE (temp1) != ASHIFTRT
5779 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5780 || XEXP (XEXP (temp1, 0), 0) != temp)
5781 return temp1;
5783 break;
5785 case TRUNCATE:
5786 /* We can't handle truncation to a partial integer mode here
5787 because we don't know the real bitsize of the partial
5788 integer mode. */
5789 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5790 break;
5792 if (HWI_COMPUTABLE_MODE_P (mode))
5793 SUBST (XEXP (x, 0),
5794 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5795 GET_MODE_MASK (mode), 0));
5797 /* We can truncate a constant value and return it. */
5798 if (CONST_INT_P (XEXP (x, 0)))
5799 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5801 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5802 whose value is a comparison can be replaced with a subreg if
5803 STORE_FLAG_VALUE permits. */
5804 if (HWI_COMPUTABLE_MODE_P (mode)
5805 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5806 && (temp = get_last_value (XEXP (x, 0)))
5807 && COMPARISON_P (temp))
5808 return gen_lowpart (mode, XEXP (x, 0));
5809 break;
5811 case CONST:
5812 /* (const (const X)) can become (const X). Do it this way rather than
5813 returning the inner CONST since CONST can be shared with a
5814 REG_EQUAL note. */
5815 if (GET_CODE (XEXP (x, 0)) == CONST)
5816 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5817 break;
5819 case LO_SUM:
5820 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5821 can add in an offset. find_split_point will split this address up
5822 again if it doesn't match. */
5823 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
5824 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5825 return XEXP (x, 1);
5826 break;
5828 case PLUS:
5829 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5830 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5831 bit-field and can be replaced by either a sign_extend or a
5832 sign_extract. The `and' may be a zero_extend and the two
5833 <c>, -<c> constants may be reversed. */
5834 if (GET_CODE (XEXP (x, 0)) == XOR
5835 && CONST_INT_P (XEXP (x, 1))
5836 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5837 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5838 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5839 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5840 && HWI_COMPUTABLE_MODE_P (mode)
5841 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5842 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5843 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5844 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5845 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5846 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5847 == (unsigned int) i + 1))))
5848 return simplify_shift_const
5849 (NULL_RTX, ASHIFTRT, mode,
5850 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5851 XEXP (XEXP (XEXP (x, 0), 0), 0),
5852 GET_MODE_PRECISION (mode) - (i + 1)),
5853 GET_MODE_PRECISION (mode) - (i + 1));
5855 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5856 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5857 the bitsize of the mode - 1. This allows simplification of
5858 "a = (b & 8) == 0;" */
5859 if (XEXP (x, 1) == constm1_rtx
5860 && !REG_P (XEXP (x, 0))
5861 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5862 && REG_P (SUBREG_REG (XEXP (x, 0))))
5863 && nonzero_bits (XEXP (x, 0), mode) == 1)
5864 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5865 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5866 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5867 GET_MODE_PRECISION (mode) - 1),
5868 GET_MODE_PRECISION (mode) - 1);
5870 /* If we are adding two things that have no bits in common, convert
5871 the addition into an IOR. This will often be further simplified,
5872 for example in cases like ((a & 1) + (a & 2)), which can
5873 become a & 3. */
5875 if (HWI_COMPUTABLE_MODE_P (mode)
5876 && (nonzero_bits (XEXP (x, 0), mode)
5877 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5879 /* Try to simplify the expression further. */
5880 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5881 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5883 /* If we could, great. If not, do not go ahead with the IOR
5884 replacement, since PLUS appears in many special purpose
5885 address arithmetic instructions. */
5886 if (GET_CODE (temp) != CLOBBER
5887 && (GET_CODE (temp) != IOR
5888 || ((XEXP (temp, 0) != XEXP (x, 0)
5889 || XEXP (temp, 1) != XEXP (x, 1))
5890 && (XEXP (temp, 0) != XEXP (x, 1)
5891 || XEXP (temp, 1) != XEXP (x, 0)))))
5892 return temp;
5894 break;
5896 case MINUS:
5897 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5898 (and <foo> (const_int pow2-1)) */
5899 if (GET_CODE (XEXP (x, 1)) == AND
5900 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5901 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5902 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5903 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5904 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5905 break;
5907 case MULT:
5908 /* If we have (mult (plus A B) C), apply the distributive law and then
5909 the inverse distributive law to see if things simplify. This
5910 occurs mostly in addresses, often when unrolling loops. */
5912 if (GET_CODE (XEXP (x, 0)) == PLUS)
5914 rtx result = distribute_and_simplify_rtx (x, 0);
5915 if (result)
5916 return result;
5919 /* Try simplify a*(b/c) as (a*b)/c. */
5920 if (FLOAT_MODE_P (mode) && flag_associative_math
5921 && GET_CODE (XEXP (x, 0)) == DIV)
5923 rtx tem = simplify_binary_operation (MULT, mode,
5924 XEXP (XEXP (x, 0), 0),
5925 XEXP (x, 1));
5926 if (tem)
5927 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5929 break;
5931 case UDIV:
5932 /* If this is a divide by a power of two, treat it as a shift if
5933 its first operand is a shift. */
5934 if (CONST_INT_P (XEXP (x, 1))
5935 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5936 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5937 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5938 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5939 || GET_CODE (XEXP (x, 0)) == ROTATE
5940 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5941 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5942 break;
5944 case EQ: case NE:
5945 case GT: case GTU: case GE: case GEU:
5946 case LT: case LTU: case LE: case LEU:
5947 case UNEQ: case LTGT:
5948 case UNGT: case UNGE:
5949 case UNLT: case UNLE:
5950 case UNORDERED: case ORDERED:
5951 /* If the first operand is a condition code, we can't do anything
5952 with it. */
5953 if (GET_CODE (XEXP (x, 0)) == COMPARE
5954 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5955 && ! CC0_P (XEXP (x, 0))))
5957 rtx op0 = XEXP (x, 0);
5958 rtx op1 = XEXP (x, 1);
5959 enum rtx_code new_code;
5961 if (GET_CODE (op0) == COMPARE)
5962 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5964 /* Simplify our comparison, if possible. */
5965 new_code = simplify_comparison (code, &op0, &op1);
5967 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5968 if only the low-order bit is possibly nonzero in X (such as when
5969 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5970 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5971 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5972 (plus X 1).
5974 Remove any ZERO_EXTRACT we made when thinking this was a
5975 comparison. It may now be simpler to use, e.g., an AND. If a
5976 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5977 the call to make_compound_operation in the SET case.
5979 Don't apply these optimizations if the caller would
5980 prefer a comparison rather than a value.
5981 E.g., for the condition in an IF_THEN_ELSE most targets need
5982 an explicit comparison. */
5984 if (in_cond)
5987 else if (STORE_FLAG_VALUE == 1
5988 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5989 && op1 == const0_rtx
5990 && mode == GET_MODE (op0)
5991 && nonzero_bits (op0, mode) == 1)
5992 return gen_lowpart (mode,
5993 expand_compound_operation (op0));
5995 else if (STORE_FLAG_VALUE == 1
5996 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5997 && op1 == const0_rtx
5998 && mode == GET_MODE (op0)
5999 && (num_sign_bit_copies (op0, mode)
6000 == GET_MODE_PRECISION (mode)))
6002 op0 = expand_compound_operation (op0);
6003 return simplify_gen_unary (NEG, mode,
6004 gen_lowpart (mode, op0),
6005 mode);
6008 else if (STORE_FLAG_VALUE == 1
6009 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6010 && op1 == const0_rtx
6011 && mode == GET_MODE (op0)
6012 && nonzero_bits (op0, mode) == 1)
6014 op0 = expand_compound_operation (op0);
6015 return simplify_gen_binary (XOR, mode,
6016 gen_lowpart (mode, op0),
6017 const1_rtx);
6020 else if (STORE_FLAG_VALUE == 1
6021 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6022 && op1 == const0_rtx
6023 && mode == GET_MODE (op0)
6024 && (num_sign_bit_copies (op0, mode)
6025 == GET_MODE_PRECISION (mode)))
6027 op0 = expand_compound_operation (op0);
6028 return plus_constant (mode, gen_lowpart (mode, op0), 1);
6031 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6032 those above. */
6033 if (in_cond)
6036 else if (STORE_FLAG_VALUE == -1
6037 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6038 && op1 == const0_rtx
6039 && mode == GET_MODE (op0)
6040 && (num_sign_bit_copies (op0, mode)
6041 == GET_MODE_PRECISION (mode)))
6042 return gen_lowpart (mode,
6043 expand_compound_operation (op0));
6045 else if (STORE_FLAG_VALUE == -1
6046 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6047 && op1 == const0_rtx
6048 && mode == GET_MODE (op0)
6049 && nonzero_bits (op0, mode) == 1)
6051 op0 = expand_compound_operation (op0);
6052 return simplify_gen_unary (NEG, mode,
6053 gen_lowpart (mode, op0),
6054 mode);
6057 else if (STORE_FLAG_VALUE == -1
6058 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6059 && op1 == const0_rtx
6060 && mode == GET_MODE (op0)
6061 && (num_sign_bit_copies (op0, mode)
6062 == GET_MODE_PRECISION (mode)))
6064 op0 = expand_compound_operation (op0);
6065 return simplify_gen_unary (NOT, mode,
6066 gen_lowpart (mode, op0),
6067 mode);
6070 /* If X is 0/1, (eq X 0) is X-1. */
6071 else if (STORE_FLAG_VALUE == -1
6072 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6073 && op1 == const0_rtx
6074 && mode == GET_MODE (op0)
6075 && nonzero_bits (op0, mode) == 1)
6077 op0 = expand_compound_operation (op0);
6078 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6081 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6082 one bit that might be nonzero, we can convert (ne x 0) to
6083 (ashift x c) where C puts the bit in the sign bit. Remove any
6084 AND with STORE_FLAG_VALUE when we are done, since we are only
6085 going to test the sign bit. */
6086 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6087 && HWI_COMPUTABLE_MODE_P (mode)
6088 && val_signbit_p (mode, STORE_FLAG_VALUE)
6089 && op1 == const0_rtx
6090 && mode == GET_MODE (op0)
6091 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6093 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6094 expand_compound_operation (op0),
6095 GET_MODE_PRECISION (mode) - 1 - i);
6096 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6097 return XEXP (x, 0);
6098 else
6099 return x;
6102 /* If the code changed, return a whole new comparison.
6103 We also need to avoid using SUBST in cases where
6104 simplify_comparison has widened a comparison with a CONST_INT,
6105 since in that case the wider CONST_INT may fail the sanity
6106 checks in do_SUBST. */
6107 if (new_code != code
6108 || (CONST_INT_P (op1)
6109 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6110 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6111 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6113 /* Otherwise, keep this operation, but maybe change its operands.
6114 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6115 SUBST (XEXP (x, 0), op0);
6116 SUBST (XEXP (x, 1), op1);
6118 break;
6120 case IF_THEN_ELSE:
6121 return simplify_if_then_else (x);
6123 case ZERO_EXTRACT:
6124 case SIGN_EXTRACT:
6125 case ZERO_EXTEND:
6126 case SIGN_EXTEND:
6127 /* If we are processing SET_DEST, we are done. */
6128 if (in_dest)
6129 return x;
6131 return expand_compound_operation (x);
6133 case SET:
6134 return simplify_set (x);
6136 case AND:
6137 case IOR:
6138 return simplify_logical (x);
6140 case ASHIFT:
6141 case LSHIFTRT:
6142 case ASHIFTRT:
6143 case ROTATE:
6144 case ROTATERT:
6145 /* If this is a shift by a constant amount, simplify it. */
6146 if (CONST_INT_P (XEXP (x, 1)))
6147 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6148 INTVAL (XEXP (x, 1)));
6150 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6151 SUBST (XEXP (x, 1),
6152 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6153 ((unsigned HOST_WIDE_INT) 1
6154 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6155 - 1,
6156 0));
6157 break;
6159 default:
6160 break;
6163 return x;
6166 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6168 static rtx
6169 simplify_if_then_else (rtx x)
6171 machine_mode mode = GET_MODE (x);
6172 rtx cond = XEXP (x, 0);
6173 rtx true_rtx = XEXP (x, 1);
6174 rtx false_rtx = XEXP (x, 2);
6175 enum rtx_code true_code = GET_CODE (cond);
6176 int comparison_p = COMPARISON_P (cond);
6177 rtx temp;
6178 int i;
6179 enum rtx_code false_code;
6180 rtx reversed;
6182 /* Simplify storing of the truth value. */
6183 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6184 return simplify_gen_relational (true_code, mode, VOIDmode,
6185 XEXP (cond, 0), XEXP (cond, 1));
6187 /* Also when the truth value has to be reversed. */
6188 if (comparison_p
6189 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6190 && (reversed = reversed_comparison (cond, mode)))
6191 return reversed;
6193 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6194 in it is being compared against certain values. Get the true and false
6195 comparisons and see if that says anything about the value of each arm. */
6197 if (comparison_p
6198 && ((false_code = reversed_comparison_code (cond, NULL))
6199 != UNKNOWN)
6200 && REG_P (XEXP (cond, 0)))
6202 HOST_WIDE_INT nzb;
6203 rtx from = XEXP (cond, 0);
6204 rtx true_val = XEXP (cond, 1);
6205 rtx false_val = true_val;
6206 int swapped = 0;
6208 /* If FALSE_CODE is EQ, swap the codes and arms. */
6210 if (false_code == EQ)
6212 swapped = 1, true_code = EQ, false_code = NE;
6213 std::swap (true_rtx, false_rtx);
6216 /* If we are comparing against zero and the expression being tested has
6217 only a single bit that might be nonzero, that is its value when it is
6218 not equal to zero. Similarly if it is known to be -1 or 0. */
6220 if (true_code == EQ && true_val == const0_rtx
6221 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6223 false_code = EQ;
6224 false_val = gen_int_mode (nzb, GET_MODE (from));
6226 else if (true_code == EQ && true_val == const0_rtx
6227 && (num_sign_bit_copies (from, GET_MODE (from))
6228 == GET_MODE_PRECISION (GET_MODE (from))))
6230 false_code = EQ;
6231 false_val = constm1_rtx;
6234 /* Now simplify an arm if we know the value of the register in the
6235 branch and it is used in the arm. Be careful due to the potential
6236 of locally-shared RTL. */
6238 if (reg_mentioned_p (from, true_rtx))
6239 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6240 from, true_val),
6241 pc_rtx, pc_rtx, 0, 0, 0);
6242 if (reg_mentioned_p (from, false_rtx))
6243 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6244 from, false_val),
6245 pc_rtx, pc_rtx, 0, 0, 0);
6247 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6248 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6250 true_rtx = XEXP (x, 1);
6251 false_rtx = XEXP (x, 2);
6252 true_code = GET_CODE (cond);
6255 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6256 reversed, do so to avoid needing two sets of patterns for
6257 subtract-and-branch insns. Similarly if we have a constant in the true
6258 arm, the false arm is the same as the first operand of the comparison, or
6259 the false arm is more complicated than the true arm. */
6261 if (comparison_p
6262 && reversed_comparison_code (cond, NULL) != UNKNOWN
6263 && (true_rtx == pc_rtx
6264 || (CONSTANT_P (true_rtx)
6265 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6266 || true_rtx == const0_rtx
6267 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6268 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6269 && !OBJECT_P (false_rtx))
6270 || reg_mentioned_p (true_rtx, false_rtx)
6271 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6273 true_code = reversed_comparison_code (cond, NULL);
6274 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6275 SUBST (XEXP (x, 1), false_rtx);
6276 SUBST (XEXP (x, 2), true_rtx);
6278 std::swap (true_rtx, false_rtx);
6279 cond = XEXP (x, 0);
6281 /* It is possible that the conditional has been simplified out. */
6282 true_code = GET_CODE (cond);
6283 comparison_p = COMPARISON_P (cond);
6286 /* If the two arms are identical, we don't need the comparison. */
6288 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6289 return true_rtx;
6291 /* Convert a == b ? b : a to "a". */
6292 if (true_code == EQ && ! side_effects_p (cond)
6293 && !HONOR_NANS (mode)
6294 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6295 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6296 return false_rtx;
6297 else if (true_code == NE && ! side_effects_p (cond)
6298 && !HONOR_NANS (mode)
6299 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6300 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6301 return true_rtx;
6303 /* Look for cases where we have (abs x) or (neg (abs X)). */
6305 if (GET_MODE_CLASS (mode) == MODE_INT
6306 && comparison_p
6307 && XEXP (cond, 1) == const0_rtx
6308 && GET_CODE (false_rtx) == NEG
6309 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6310 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6311 && ! side_effects_p (true_rtx))
6312 switch (true_code)
6314 case GT:
6315 case GE:
6316 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6317 case LT:
6318 case LE:
6319 return
6320 simplify_gen_unary (NEG, mode,
6321 simplify_gen_unary (ABS, mode, true_rtx, mode),
6322 mode);
6323 default:
6324 break;
6327 /* Look for MIN or MAX. */
6329 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6330 && comparison_p
6331 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6332 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6333 && ! side_effects_p (cond))
6334 switch (true_code)
6336 case GE:
6337 case GT:
6338 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6339 case LE:
6340 case LT:
6341 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6342 case GEU:
6343 case GTU:
6344 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6345 case LEU:
6346 case LTU:
6347 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6348 default:
6349 break;
6352 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6353 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6354 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6355 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6356 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6357 neither 1 or -1, but it isn't worth checking for. */
6359 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6360 && comparison_p
6361 && GET_MODE_CLASS (mode) == MODE_INT
6362 && ! side_effects_p (x))
6364 rtx t = make_compound_operation (true_rtx, SET);
6365 rtx f = make_compound_operation (false_rtx, SET);
6366 rtx cond_op0 = XEXP (cond, 0);
6367 rtx cond_op1 = XEXP (cond, 1);
6368 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6369 machine_mode m = mode;
6370 rtx z = 0, c1 = NULL_RTX;
6372 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6373 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6374 || GET_CODE (t) == ASHIFT
6375 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6376 && rtx_equal_p (XEXP (t, 0), f))
6377 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6379 /* If an identity-zero op is commutative, check whether there
6380 would be a match if we swapped the operands. */
6381 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6382 || GET_CODE (t) == XOR)
6383 && rtx_equal_p (XEXP (t, 1), f))
6384 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6385 else if (GET_CODE (t) == SIGN_EXTEND
6386 && (GET_CODE (XEXP (t, 0)) == PLUS
6387 || GET_CODE (XEXP (t, 0)) == MINUS
6388 || GET_CODE (XEXP (t, 0)) == IOR
6389 || GET_CODE (XEXP (t, 0)) == XOR
6390 || GET_CODE (XEXP (t, 0)) == ASHIFT
6391 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6392 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6393 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6394 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6395 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6396 && (num_sign_bit_copies (f, GET_MODE (f))
6397 > (unsigned int)
6398 (GET_MODE_PRECISION (mode)
6399 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6401 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6402 extend_op = SIGN_EXTEND;
6403 m = GET_MODE (XEXP (t, 0));
6405 else if (GET_CODE (t) == SIGN_EXTEND
6406 && (GET_CODE (XEXP (t, 0)) == PLUS
6407 || GET_CODE (XEXP (t, 0)) == IOR
6408 || GET_CODE (XEXP (t, 0)) == XOR)
6409 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6410 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6411 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6412 && (num_sign_bit_copies (f, GET_MODE (f))
6413 > (unsigned int)
6414 (GET_MODE_PRECISION (mode)
6415 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6417 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6418 extend_op = SIGN_EXTEND;
6419 m = GET_MODE (XEXP (t, 0));
6421 else if (GET_CODE (t) == ZERO_EXTEND
6422 && (GET_CODE (XEXP (t, 0)) == PLUS
6423 || GET_CODE (XEXP (t, 0)) == MINUS
6424 || GET_CODE (XEXP (t, 0)) == IOR
6425 || GET_CODE (XEXP (t, 0)) == XOR
6426 || GET_CODE (XEXP (t, 0)) == ASHIFT
6427 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6428 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6429 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6430 && HWI_COMPUTABLE_MODE_P (mode)
6431 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6432 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6433 && ((nonzero_bits (f, GET_MODE (f))
6434 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6435 == 0))
6437 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6438 extend_op = ZERO_EXTEND;
6439 m = GET_MODE (XEXP (t, 0));
6441 else if (GET_CODE (t) == ZERO_EXTEND
6442 && (GET_CODE (XEXP (t, 0)) == PLUS
6443 || GET_CODE (XEXP (t, 0)) == IOR
6444 || GET_CODE (XEXP (t, 0)) == XOR)
6445 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6446 && HWI_COMPUTABLE_MODE_P (mode)
6447 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6448 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6449 && ((nonzero_bits (f, GET_MODE (f))
6450 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6451 == 0))
6453 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6454 extend_op = ZERO_EXTEND;
6455 m = GET_MODE (XEXP (t, 0));
6458 if (z)
6460 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6461 cond_op0, cond_op1),
6462 pc_rtx, pc_rtx, 0, 0, 0);
6463 temp = simplify_gen_binary (MULT, m, temp,
6464 simplify_gen_binary (MULT, m, c1,
6465 const_true_rtx));
6466 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6467 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6469 if (extend_op != UNKNOWN)
6470 temp = simplify_gen_unary (extend_op, mode, temp, m);
6472 return temp;
6476 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6477 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6478 negation of a single bit, we can convert this operation to a shift. We
6479 can actually do this more generally, but it doesn't seem worth it. */
6481 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6482 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6483 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6484 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6485 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6486 == GET_MODE_PRECISION (mode))
6487 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6488 return
6489 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6490 gen_lowpart (mode, XEXP (cond, 0)), i);
6492 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6493 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6494 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6495 && GET_MODE (XEXP (cond, 0)) == mode
6496 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6497 == nonzero_bits (XEXP (cond, 0), mode)
6498 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6499 return XEXP (cond, 0);
6501 return x;
6504 /* Simplify X, a SET expression. Return the new expression. */
6506 static rtx
6507 simplify_set (rtx x)
6509 rtx src = SET_SRC (x);
6510 rtx dest = SET_DEST (x);
6511 machine_mode mode
6512 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6513 rtx_insn *other_insn;
6514 rtx *cc_use;
6516 /* (set (pc) (return)) gets written as (return). */
6517 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6518 return src;
6520 /* Now that we know for sure which bits of SRC we are using, see if we can
6521 simplify the expression for the object knowing that we only need the
6522 low-order bits. */
6524 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6526 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6527 SUBST (SET_SRC (x), src);
6530 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6531 the comparison result and try to simplify it unless we already have used
6532 undobuf.other_insn. */
6533 if ((GET_MODE_CLASS (mode) == MODE_CC
6534 || GET_CODE (src) == COMPARE
6535 || CC0_P (dest))
6536 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6537 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6538 && COMPARISON_P (*cc_use)
6539 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6541 enum rtx_code old_code = GET_CODE (*cc_use);
6542 enum rtx_code new_code;
6543 rtx op0, op1, tmp;
6544 int other_changed = 0;
6545 rtx inner_compare = NULL_RTX;
6546 machine_mode compare_mode = GET_MODE (dest);
6548 if (GET_CODE (src) == COMPARE)
6550 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6551 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6553 inner_compare = op0;
6554 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6557 else
6558 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6560 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6561 op0, op1);
6562 if (!tmp)
6563 new_code = old_code;
6564 else if (!CONSTANT_P (tmp))
6566 new_code = GET_CODE (tmp);
6567 op0 = XEXP (tmp, 0);
6568 op1 = XEXP (tmp, 1);
6570 else
6572 rtx pat = PATTERN (other_insn);
6573 undobuf.other_insn = other_insn;
6574 SUBST (*cc_use, tmp);
6576 /* Attempt to simplify CC user. */
6577 if (GET_CODE (pat) == SET)
6579 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6580 if (new_rtx != NULL_RTX)
6581 SUBST (SET_SRC (pat), new_rtx);
6584 /* Convert X into a no-op move. */
6585 SUBST (SET_DEST (x), pc_rtx);
6586 SUBST (SET_SRC (x), pc_rtx);
6587 return x;
6590 /* Simplify our comparison, if possible. */
6591 new_code = simplify_comparison (new_code, &op0, &op1);
6593 #ifdef SELECT_CC_MODE
6594 /* If this machine has CC modes other than CCmode, check to see if we
6595 need to use a different CC mode here. */
6596 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6597 compare_mode = GET_MODE (op0);
6598 else if (inner_compare
6599 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6600 && new_code == old_code
6601 && op0 == XEXP (inner_compare, 0)
6602 && op1 == XEXP (inner_compare, 1))
6603 compare_mode = GET_MODE (inner_compare);
6604 else
6605 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6607 /* If the mode changed, we have to change SET_DEST, the mode in the
6608 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6609 a hard register, just build new versions with the proper mode. If it
6610 is a pseudo, we lose unless it is only time we set the pseudo, in
6611 which case we can safely change its mode. */
6612 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6614 if (can_change_dest_mode (dest, 0, compare_mode))
6616 unsigned int regno = REGNO (dest);
6617 rtx new_dest;
6619 if (regno < FIRST_PSEUDO_REGISTER)
6620 new_dest = gen_rtx_REG (compare_mode, regno);
6621 else
6623 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6624 new_dest = regno_reg_rtx[regno];
6627 SUBST (SET_DEST (x), new_dest);
6628 SUBST (XEXP (*cc_use, 0), new_dest);
6629 other_changed = 1;
6631 dest = new_dest;
6634 #endif /* SELECT_CC_MODE */
6636 /* If the code changed, we have to build a new comparison in
6637 undobuf.other_insn. */
6638 if (new_code != old_code)
6640 int other_changed_previously = other_changed;
6641 unsigned HOST_WIDE_INT mask;
6642 rtx old_cc_use = *cc_use;
6644 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6645 dest, const0_rtx));
6646 other_changed = 1;
6648 /* If the only change we made was to change an EQ into an NE or
6649 vice versa, OP0 has only one bit that might be nonzero, and OP1
6650 is zero, check if changing the user of the condition code will
6651 produce a valid insn. If it won't, we can keep the original code
6652 in that insn by surrounding our operation with an XOR. */
6654 if (((old_code == NE && new_code == EQ)
6655 || (old_code == EQ && new_code == NE))
6656 && ! other_changed_previously && op1 == const0_rtx
6657 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6658 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6660 rtx pat = PATTERN (other_insn), note = 0;
6662 if ((recog_for_combine (&pat, other_insn, &note) < 0
6663 && ! check_asm_operands (pat)))
6665 *cc_use = old_cc_use;
6666 other_changed = 0;
6668 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6669 gen_int_mode (mask,
6670 GET_MODE (op0)));
6675 if (other_changed)
6676 undobuf.other_insn = other_insn;
6678 /* Don't generate a compare of a CC with 0, just use that CC. */
6679 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6681 SUBST (SET_SRC (x), op0);
6682 src = SET_SRC (x);
6684 /* Otherwise, if we didn't previously have the same COMPARE we
6685 want, create it from scratch. */
6686 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6687 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6689 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6690 src = SET_SRC (x);
6693 else
6695 /* Get SET_SRC in a form where we have placed back any
6696 compound expressions. Then do the checks below. */
6697 src = make_compound_operation (src, SET);
6698 SUBST (SET_SRC (x), src);
6701 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6702 and X being a REG or (subreg (reg)), we may be able to convert this to
6703 (set (subreg:m2 x) (op)).
6705 We can always do this if M1 is narrower than M2 because that means that
6706 we only care about the low bits of the result.
6708 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6709 perform a narrower operation than requested since the high-order bits will
6710 be undefined. On machine where it is defined, this transformation is safe
6711 as long as M1 and M2 have the same number of words. */
6713 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6714 && !OBJECT_P (SUBREG_REG (src))
6715 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6716 / UNITS_PER_WORD)
6717 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6718 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6719 && (WORD_REGISTER_OPERATIONS
6720 || (GET_MODE_SIZE (GET_MODE (src))
6721 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
6722 #ifdef CANNOT_CHANGE_MODE_CLASS
6723 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6724 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6725 GET_MODE (SUBREG_REG (src)),
6726 GET_MODE (src)))
6727 #endif
6728 && (REG_P (dest)
6729 || (GET_CODE (dest) == SUBREG
6730 && REG_P (SUBREG_REG (dest)))))
6732 SUBST (SET_DEST (x),
6733 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6734 dest));
6735 SUBST (SET_SRC (x), SUBREG_REG (src));
6737 src = SET_SRC (x), dest = SET_DEST (x);
6740 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6741 in SRC. */
6742 if (dest == cc0_rtx
6743 && GET_CODE (src) == SUBREG
6744 && subreg_lowpart_p (src)
6745 && (GET_MODE_PRECISION (GET_MODE (src))
6746 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6748 rtx inner = SUBREG_REG (src);
6749 machine_mode inner_mode = GET_MODE (inner);
6751 /* Here we make sure that we don't have a sign bit on. */
6752 if (val_signbit_known_clear_p (GET_MODE (src),
6753 nonzero_bits (inner, inner_mode)))
6755 SUBST (SET_SRC (x), inner);
6756 src = SET_SRC (x);
6760 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6761 would require a paradoxical subreg. Replace the subreg with a
6762 zero_extend to avoid the reload that would otherwise be required. */
6764 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6765 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6766 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6767 && SUBREG_BYTE (src) == 0
6768 && paradoxical_subreg_p (src)
6769 && MEM_P (SUBREG_REG (src)))
6771 SUBST (SET_SRC (x),
6772 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6773 GET_MODE (src), SUBREG_REG (src)));
6775 src = SET_SRC (x);
6778 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6779 are comparing an item known to be 0 or -1 against 0, use a logical
6780 operation instead. Check for one of the arms being an IOR of the other
6781 arm with some value. We compute three terms to be IOR'ed together. In
6782 practice, at most two will be nonzero. Then we do the IOR's. */
6784 if (GET_CODE (dest) != PC
6785 && GET_CODE (src) == IF_THEN_ELSE
6786 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6787 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6788 && XEXP (XEXP (src, 0), 1) == const0_rtx
6789 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6790 && (!HAVE_conditional_move
6791 || ! can_conditionally_move_p (GET_MODE (src)))
6792 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6793 GET_MODE (XEXP (XEXP (src, 0), 0)))
6794 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6795 && ! side_effects_p (src))
6797 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6798 ? XEXP (src, 1) : XEXP (src, 2));
6799 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6800 ? XEXP (src, 2) : XEXP (src, 1));
6801 rtx term1 = const0_rtx, term2, term3;
6803 if (GET_CODE (true_rtx) == IOR
6804 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6805 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6806 else if (GET_CODE (true_rtx) == IOR
6807 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6808 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6809 else if (GET_CODE (false_rtx) == IOR
6810 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6811 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6812 else if (GET_CODE (false_rtx) == IOR
6813 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6814 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6816 term2 = simplify_gen_binary (AND, GET_MODE (src),
6817 XEXP (XEXP (src, 0), 0), true_rtx);
6818 term3 = simplify_gen_binary (AND, GET_MODE (src),
6819 simplify_gen_unary (NOT, GET_MODE (src),
6820 XEXP (XEXP (src, 0), 0),
6821 GET_MODE (src)),
6822 false_rtx);
6824 SUBST (SET_SRC (x),
6825 simplify_gen_binary (IOR, GET_MODE (src),
6826 simplify_gen_binary (IOR, GET_MODE (src),
6827 term1, term2),
6828 term3));
6830 src = SET_SRC (x);
6833 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6834 whole thing fail. */
6835 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6836 return src;
6837 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6838 return dest;
6839 else
6840 /* Convert this into a field assignment operation, if possible. */
6841 return make_field_assignment (x);
6844 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6845 result. */
6847 static rtx
6848 simplify_logical (rtx x)
6850 machine_mode mode = GET_MODE (x);
6851 rtx op0 = XEXP (x, 0);
6852 rtx op1 = XEXP (x, 1);
6854 switch (GET_CODE (x))
6856 case AND:
6857 /* We can call simplify_and_const_int only if we don't lose
6858 any (sign) bits when converting INTVAL (op1) to
6859 "unsigned HOST_WIDE_INT". */
6860 if (CONST_INT_P (op1)
6861 && (HWI_COMPUTABLE_MODE_P (mode)
6862 || INTVAL (op1) > 0))
6864 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6865 if (GET_CODE (x) != AND)
6866 return x;
6868 op0 = XEXP (x, 0);
6869 op1 = XEXP (x, 1);
6872 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6873 apply the distributive law and then the inverse distributive
6874 law to see if things simplify. */
6875 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6877 rtx result = distribute_and_simplify_rtx (x, 0);
6878 if (result)
6879 return result;
6881 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6883 rtx result = distribute_and_simplify_rtx (x, 1);
6884 if (result)
6885 return result;
6887 break;
6889 case IOR:
6890 /* If we have (ior (and A B) C), apply the distributive law and then
6891 the inverse distributive law to see if things simplify. */
6893 if (GET_CODE (op0) == AND)
6895 rtx result = distribute_and_simplify_rtx (x, 0);
6896 if (result)
6897 return result;
6900 if (GET_CODE (op1) == AND)
6902 rtx result = distribute_and_simplify_rtx (x, 1);
6903 if (result)
6904 return result;
6906 break;
6908 default:
6909 gcc_unreachable ();
6912 return x;
6915 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6916 operations" because they can be replaced with two more basic operations.
6917 ZERO_EXTEND is also considered "compound" because it can be replaced with
6918 an AND operation, which is simpler, though only one operation.
6920 The function expand_compound_operation is called with an rtx expression
6921 and will convert it to the appropriate shifts and AND operations,
6922 simplifying at each stage.
6924 The function make_compound_operation is called to convert an expression
6925 consisting of shifts and ANDs into the equivalent compound expression.
6926 It is the inverse of this function, loosely speaking. */
6928 static rtx
6929 expand_compound_operation (rtx x)
6931 unsigned HOST_WIDE_INT pos = 0, len;
6932 int unsignedp = 0;
6933 unsigned int modewidth;
6934 rtx tem;
6936 switch (GET_CODE (x))
6938 case ZERO_EXTEND:
6939 unsignedp = 1;
6940 case SIGN_EXTEND:
6941 /* We can't necessarily use a const_int for a multiword mode;
6942 it depends on implicitly extending the value.
6943 Since we don't know the right way to extend it,
6944 we can't tell whether the implicit way is right.
6946 Even for a mode that is no wider than a const_int,
6947 we can't win, because we need to sign extend one of its bits through
6948 the rest of it, and we don't know which bit. */
6949 if (CONST_INT_P (XEXP (x, 0)))
6950 return x;
6952 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6953 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6954 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6955 reloaded. If not for that, MEM's would very rarely be safe.
6957 Reject MODEs bigger than a word, because we might not be able
6958 to reference a two-register group starting with an arbitrary register
6959 (and currently gen_lowpart might crash for a SUBREG). */
6961 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6962 return x;
6964 /* Reject MODEs that aren't scalar integers because turning vector
6965 or complex modes into shifts causes problems. */
6967 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6968 return x;
6970 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6971 /* If the inner object has VOIDmode (the only way this can happen
6972 is if it is an ASM_OPERANDS), we can't do anything since we don't
6973 know how much masking to do. */
6974 if (len == 0)
6975 return x;
6977 break;
6979 case ZERO_EXTRACT:
6980 unsignedp = 1;
6982 /* ... fall through ... */
6984 case SIGN_EXTRACT:
6985 /* If the operand is a CLOBBER, just return it. */
6986 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6987 return XEXP (x, 0);
6989 if (!CONST_INT_P (XEXP (x, 1))
6990 || !CONST_INT_P (XEXP (x, 2))
6991 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6992 return x;
6994 /* Reject MODEs that aren't scalar integers because turning vector
6995 or complex modes into shifts causes problems. */
6997 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6998 return x;
7000 len = INTVAL (XEXP (x, 1));
7001 pos = INTVAL (XEXP (x, 2));
7003 /* This should stay within the object being extracted, fail otherwise. */
7004 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
7005 return x;
7007 if (BITS_BIG_ENDIAN)
7008 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
7010 break;
7012 default:
7013 return x;
7015 /* Convert sign extension to zero extension, if we know that the high
7016 bit is not set, as this is easier to optimize. It will be converted
7017 back to cheaper alternative in make_extraction. */
7018 if (GET_CODE (x) == SIGN_EXTEND
7019 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7020 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7021 & ~(((unsigned HOST_WIDE_INT)
7022 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
7023 >> 1))
7024 == 0)))
7026 machine_mode mode = GET_MODE (x);
7027 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7028 rtx temp2 = expand_compound_operation (temp);
7030 /* Make sure this is a profitable operation. */
7031 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7032 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7033 return temp2;
7034 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7035 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7036 return temp;
7037 else
7038 return x;
7041 /* We can optimize some special cases of ZERO_EXTEND. */
7042 if (GET_CODE (x) == ZERO_EXTEND)
7044 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7045 know that the last value didn't have any inappropriate bits
7046 set. */
7047 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7048 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7049 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7050 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
7051 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7052 return XEXP (XEXP (x, 0), 0);
7054 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7055 if (GET_CODE (XEXP (x, 0)) == SUBREG
7056 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7057 && subreg_lowpart_p (XEXP (x, 0))
7058 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7059 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
7060 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7061 return SUBREG_REG (XEXP (x, 0));
7063 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7064 is a comparison and STORE_FLAG_VALUE permits. This is like
7065 the first case, but it works even when GET_MODE (x) is larger
7066 than HOST_WIDE_INT. */
7067 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7068 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7069 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7070 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7071 <= HOST_BITS_PER_WIDE_INT)
7072 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7073 return XEXP (XEXP (x, 0), 0);
7075 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7076 if (GET_CODE (XEXP (x, 0)) == SUBREG
7077 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7078 && subreg_lowpart_p (XEXP (x, 0))
7079 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7080 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7081 <= HOST_BITS_PER_WIDE_INT)
7082 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7083 return SUBREG_REG (XEXP (x, 0));
7087 /* If we reach here, we want to return a pair of shifts. The inner
7088 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7089 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7090 logical depending on the value of UNSIGNEDP.
7092 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7093 converted into an AND of a shift.
7095 We must check for the case where the left shift would have a negative
7096 count. This can happen in a case like (x >> 31) & 255 on machines
7097 that can't shift by a constant. On those machines, we would first
7098 combine the shift with the AND to produce a variable-position
7099 extraction. Then the constant of 31 would be substituted in
7100 to produce such a position. */
7102 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7103 if (modewidth >= pos + len)
7105 machine_mode mode = GET_MODE (x);
7106 tem = gen_lowpart (mode, XEXP (x, 0));
7107 if (!tem || GET_CODE (tem) == CLOBBER)
7108 return x;
7109 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7110 tem, modewidth - pos - len);
7111 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7112 mode, tem, modewidth - len);
7114 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7115 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7116 simplify_shift_const (NULL_RTX, LSHIFTRT,
7117 GET_MODE (x),
7118 XEXP (x, 0), pos),
7119 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7120 else
7121 /* Any other cases we can't handle. */
7122 return x;
7124 /* If we couldn't do this for some reason, return the original
7125 expression. */
7126 if (GET_CODE (tem) == CLOBBER)
7127 return x;
7129 return tem;
7132 /* X is a SET which contains an assignment of one object into
7133 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7134 or certain SUBREGS). If possible, convert it into a series of
7135 logical operations.
7137 We half-heartedly support variable positions, but do not at all
7138 support variable lengths. */
7140 static const_rtx
7141 expand_field_assignment (const_rtx x)
7143 rtx inner;
7144 rtx pos; /* Always counts from low bit. */
7145 int len;
7146 rtx mask, cleared, masked;
7147 machine_mode compute_mode;
7149 /* Loop until we find something we can't simplify. */
7150 while (1)
7152 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7153 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7155 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7156 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7157 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7159 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7160 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7162 inner = XEXP (SET_DEST (x), 0);
7163 len = INTVAL (XEXP (SET_DEST (x), 1));
7164 pos = XEXP (SET_DEST (x), 2);
7166 /* A constant position should stay within the width of INNER. */
7167 if (CONST_INT_P (pos)
7168 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7169 break;
7171 if (BITS_BIG_ENDIAN)
7173 if (CONST_INT_P (pos))
7174 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7175 - INTVAL (pos));
7176 else if (GET_CODE (pos) == MINUS
7177 && CONST_INT_P (XEXP (pos, 1))
7178 && (INTVAL (XEXP (pos, 1))
7179 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7180 /* If position is ADJUST - X, new position is X. */
7181 pos = XEXP (pos, 0);
7182 else
7184 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7185 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7186 gen_int_mode (prec - len,
7187 GET_MODE (pos)),
7188 pos);
7193 /* A SUBREG between two modes that occupy the same numbers of words
7194 can be done by moving the SUBREG to the source. */
7195 else if (GET_CODE (SET_DEST (x)) == SUBREG
7196 /* We need SUBREGs to compute nonzero_bits properly. */
7197 && nonzero_sign_valid
7198 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7199 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7200 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7201 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7203 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7204 gen_lowpart
7205 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7206 SET_SRC (x)));
7207 continue;
7209 else
7210 break;
7212 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7213 inner = SUBREG_REG (inner);
7215 compute_mode = GET_MODE (inner);
7217 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7218 if (! SCALAR_INT_MODE_P (compute_mode))
7220 machine_mode imode;
7222 /* Don't do anything for vector or complex integral types. */
7223 if (! FLOAT_MODE_P (compute_mode))
7224 break;
7226 /* Try to find an integral mode to pun with. */
7227 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7228 if (imode == BLKmode)
7229 break;
7231 compute_mode = imode;
7232 inner = gen_lowpart (imode, inner);
7235 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7236 if (len >= HOST_BITS_PER_WIDE_INT)
7237 break;
7239 /* Now compute the equivalent expression. Make a copy of INNER
7240 for the SET_DEST in case it is a MEM into which we will substitute;
7241 we don't want shared RTL in that case. */
7242 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7243 compute_mode);
7244 cleared = simplify_gen_binary (AND, compute_mode,
7245 simplify_gen_unary (NOT, compute_mode,
7246 simplify_gen_binary (ASHIFT,
7247 compute_mode,
7248 mask, pos),
7249 compute_mode),
7250 inner);
7251 masked = simplify_gen_binary (ASHIFT, compute_mode,
7252 simplify_gen_binary (
7253 AND, compute_mode,
7254 gen_lowpart (compute_mode, SET_SRC (x)),
7255 mask),
7256 pos);
7258 x = gen_rtx_SET (copy_rtx (inner),
7259 simplify_gen_binary (IOR, compute_mode,
7260 cleared, masked));
7263 return x;
7266 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7267 it is an RTX that represents the (variable) starting position; otherwise,
7268 POS is the (constant) starting bit position. Both are counted from the LSB.
7270 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7272 IN_DEST is nonzero if this is a reference in the destination of a SET.
7273 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7274 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7275 be used.
7277 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7278 ZERO_EXTRACT should be built even for bits starting at bit 0.
7280 MODE is the desired mode of the result (if IN_DEST == 0).
7282 The result is an RTX for the extraction or NULL_RTX if the target
7283 can't handle it. */
7285 static rtx
7286 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7287 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7288 int in_dest, int in_compare)
7290 /* This mode describes the size of the storage area
7291 to fetch the overall value from. Within that, we
7292 ignore the POS lowest bits, etc. */
7293 machine_mode is_mode = GET_MODE (inner);
7294 machine_mode inner_mode;
7295 machine_mode wanted_inner_mode;
7296 machine_mode wanted_inner_reg_mode = word_mode;
7297 machine_mode pos_mode = word_mode;
7298 machine_mode extraction_mode = word_mode;
7299 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7300 rtx new_rtx = 0;
7301 rtx orig_pos_rtx = pos_rtx;
7302 HOST_WIDE_INT orig_pos;
7304 if (pos_rtx && CONST_INT_P (pos_rtx))
7305 pos = INTVAL (pos_rtx), pos_rtx = 0;
7307 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7309 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7310 consider just the QI as the memory to extract from.
7311 The subreg adds or removes high bits; its mode is
7312 irrelevant to the meaning of this extraction,
7313 since POS and LEN count from the lsb. */
7314 if (MEM_P (SUBREG_REG (inner)))
7315 is_mode = GET_MODE (SUBREG_REG (inner));
7316 inner = SUBREG_REG (inner);
7318 else if (GET_CODE (inner) == ASHIFT
7319 && CONST_INT_P (XEXP (inner, 1))
7320 && pos_rtx == 0 && pos == 0
7321 && len > UINTVAL (XEXP (inner, 1)))
7323 /* We're extracting the least significant bits of an rtx
7324 (ashift X (const_int C)), where LEN > C. Extract the
7325 least significant (LEN - C) bits of X, giving an rtx
7326 whose mode is MODE, then shift it left C times. */
7327 new_rtx = make_extraction (mode, XEXP (inner, 0),
7328 0, 0, len - INTVAL (XEXP (inner, 1)),
7329 unsignedp, in_dest, in_compare);
7330 if (new_rtx != 0)
7331 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7333 else if (GET_CODE (inner) == TRUNCATE)
7334 inner = XEXP (inner, 0);
7336 inner_mode = GET_MODE (inner);
7338 /* See if this can be done without an extraction. We never can if the
7339 width of the field is not the same as that of some integer mode. For
7340 registers, we can only avoid the extraction if the position is at the
7341 low-order bit and this is either not in the destination or we have the
7342 appropriate STRICT_LOW_PART operation available.
7344 For MEM, we can avoid an extract if the field starts on an appropriate
7345 boundary and we can change the mode of the memory reference. */
7347 if (tmode != BLKmode
7348 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7349 && !MEM_P (inner)
7350 && (inner_mode == tmode
7351 || !REG_P (inner)
7352 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7353 || reg_truncated_to_mode (tmode, inner))
7354 && (! in_dest
7355 || (REG_P (inner)
7356 && have_insn_for (STRICT_LOW_PART, tmode))))
7357 || (MEM_P (inner) && pos_rtx == 0
7358 && (pos
7359 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7360 : BITS_PER_UNIT)) == 0
7361 /* We can't do this if we are widening INNER_MODE (it
7362 may not be aligned, for one thing). */
7363 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7364 && (inner_mode == tmode
7365 || (! mode_dependent_address_p (XEXP (inner, 0),
7366 MEM_ADDR_SPACE (inner))
7367 && ! MEM_VOLATILE_P (inner))))))
7369 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7370 field. If the original and current mode are the same, we need not
7371 adjust the offset. Otherwise, we do if bytes big endian.
7373 If INNER is not a MEM, get a piece consisting of just the field
7374 of interest (in this case POS % BITS_PER_WORD must be 0). */
7376 if (MEM_P (inner))
7378 HOST_WIDE_INT offset;
7380 /* POS counts from lsb, but make OFFSET count in memory order. */
7381 if (BYTES_BIG_ENDIAN)
7382 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7383 else
7384 offset = pos / BITS_PER_UNIT;
7386 new_rtx = adjust_address_nv (inner, tmode, offset);
7388 else if (REG_P (inner))
7390 if (tmode != inner_mode)
7392 /* We can't call gen_lowpart in a DEST since we
7393 always want a SUBREG (see below) and it would sometimes
7394 return a new hard register. */
7395 if (pos || in_dest)
7397 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7399 if (WORDS_BIG_ENDIAN
7400 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7401 final_word = ((GET_MODE_SIZE (inner_mode)
7402 - GET_MODE_SIZE (tmode))
7403 / UNITS_PER_WORD) - final_word;
7405 final_word *= UNITS_PER_WORD;
7406 if (BYTES_BIG_ENDIAN &&
7407 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7408 final_word += (GET_MODE_SIZE (inner_mode)
7409 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7411 /* Avoid creating invalid subregs, for example when
7412 simplifying (x>>32)&255. */
7413 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7414 return NULL_RTX;
7416 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7418 else
7419 new_rtx = gen_lowpart (tmode, inner);
7421 else
7422 new_rtx = inner;
7424 else
7425 new_rtx = force_to_mode (inner, tmode,
7426 len >= HOST_BITS_PER_WIDE_INT
7427 ? ~(unsigned HOST_WIDE_INT) 0
7428 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7431 /* If this extraction is going into the destination of a SET,
7432 make a STRICT_LOW_PART unless we made a MEM. */
7434 if (in_dest)
7435 return (MEM_P (new_rtx) ? new_rtx
7436 : (GET_CODE (new_rtx) != SUBREG
7437 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7438 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7440 if (mode == tmode)
7441 return new_rtx;
7443 if (CONST_SCALAR_INT_P (new_rtx))
7444 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7445 mode, new_rtx, tmode);
7447 /* If we know that no extraneous bits are set, and that the high
7448 bit is not set, convert the extraction to the cheaper of
7449 sign and zero extension, that are equivalent in these cases. */
7450 if (flag_expensive_optimizations
7451 && (HWI_COMPUTABLE_MODE_P (tmode)
7452 && ((nonzero_bits (new_rtx, tmode)
7453 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7454 == 0)))
7456 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7457 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7459 /* Prefer ZERO_EXTENSION, since it gives more information to
7460 backends. */
7461 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7462 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7463 return temp;
7464 return temp1;
7467 /* Otherwise, sign- or zero-extend unless we already are in the
7468 proper mode. */
7470 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7471 mode, new_rtx));
7474 /* Unless this is a COMPARE or we have a funny memory reference,
7475 don't do anything with zero-extending field extracts starting at
7476 the low-order bit since they are simple AND operations. */
7477 if (pos_rtx == 0 && pos == 0 && ! in_dest
7478 && ! in_compare && unsignedp)
7479 return 0;
7481 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7482 if the position is not a constant and the length is not 1. In all
7483 other cases, we would only be going outside our object in cases when
7484 an original shift would have been undefined. */
7485 if (MEM_P (inner)
7486 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7487 || (pos_rtx != 0 && len != 1)))
7488 return 0;
7490 enum extraction_pattern pattern = (in_dest ? EP_insv
7491 : unsignedp ? EP_extzv : EP_extv);
7493 /* If INNER is not from memory, we want it to have the mode of a register
7494 extraction pattern's structure operand, or word_mode if there is no
7495 such pattern. The same applies to extraction_mode and pos_mode
7496 and their respective operands.
7498 For memory, assume that the desired extraction_mode and pos_mode
7499 are the same as for a register operation, since at present we don't
7500 have named patterns for aligned memory structures. */
7501 struct extraction_insn insn;
7502 if (get_best_reg_extraction_insn (&insn, pattern,
7503 GET_MODE_BITSIZE (inner_mode), mode))
7505 wanted_inner_reg_mode = insn.struct_mode;
7506 pos_mode = insn.pos_mode;
7507 extraction_mode = insn.field_mode;
7510 /* Never narrow an object, since that might not be safe. */
7512 if (mode != VOIDmode
7513 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7514 extraction_mode = mode;
7516 if (!MEM_P (inner))
7517 wanted_inner_mode = wanted_inner_reg_mode;
7518 else
7520 /* Be careful not to go beyond the extracted object and maintain the
7521 natural alignment of the memory. */
7522 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7523 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7524 > GET_MODE_BITSIZE (wanted_inner_mode))
7526 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7527 gcc_assert (wanted_inner_mode != VOIDmode);
7531 orig_pos = pos;
7533 if (BITS_BIG_ENDIAN)
7535 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7536 BITS_BIG_ENDIAN style. If position is constant, compute new
7537 position. Otherwise, build subtraction.
7538 Note that POS is relative to the mode of the original argument.
7539 If it's a MEM we need to recompute POS relative to that.
7540 However, if we're extracting from (or inserting into) a register,
7541 we want to recompute POS relative to wanted_inner_mode. */
7542 int width = (MEM_P (inner)
7543 ? GET_MODE_BITSIZE (is_mode)
7544 : GET_MODE_BITSIZE (wanted_inner_mode));
7546 if (pos_rtx == 0)
7547 pos = width - len - pos;
7548 else
7549 pos_rtx
7550 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7551 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7552 pos_rtx);
7553 /* POS may be less than 0 now, but we check for that below.
7554 Note that it can only be less than 0 if !MEM_P (inner). */
7557 /* If INNER has a wider mode, and this is a constant extraction, try to
7558 make it smaller and adjust the byte to point to the byte containing
7559 the value. */
7560 if (wanted_inner_mode != VOIDmode
7561 && inner_mode != wanted_inner_mode
7562 && ! pos_rtx
7563 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7564 && MEM_P (inner)
7565 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7566 && ! MEM_VOLATILE_P (inner))
7568 int offset = 0;
7570 /* The computations below will be correct if the machine is big
7571 endian in both bits and bytes or little endian in bits and bytes.
7572 If it is mixed, we must adjust. */
7574 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7575 adjust OFFSET to compensate. */
7576 if (BYTES_BIG_ENDIAN
7577 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7578 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7580 /* We can now move to the desired byte. */
7581 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7582 * GET_MODE_SIZE (wanted_inner_mode);
7583 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7585 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7586 && is_mode != wanted_inner_mode)
7587 offset = (GET_MODE_SIZE (is_mode)
7588 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7590 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7593 /* If INNER is not memory, get it into the proper mode. If we are changing
7594 its mode, POS must be a constant and smaller than the size of the new
7595 mode. */
7596 else if (!MEM_P (inner))
7598 /* On the LHS, don't create paradoxical subregs implicitely truncating
7599 the register unless TRULY_NOOP_TRUNCATION. */
7600 if (in_dest
7601 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7602 wanted_inner_mode))
7603 return NULL_RTX;
7605 if (GET_MODE (inner) != wanted_inner_mode
7606 && (pos_rtx != 0
7607 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7608 return NULL_RTX;
7610 if (orig_pos < 0)
7611 return NULL_RTX;
7613 inner = force_to_mode (inner, wanted_inner_mode,
7614 pos_rtx
7615 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7616 ? ~(unsigned HOST_WIDE_INT) 0
7617 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7618 << orig_pos),
7622 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7623 have to zero extend. Otherwise, we can just use a SUBREG. */
7624 if (pos_rtx != 0
7625 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7627 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7628 GET_MODE (pos_rtx));
7630 /* If we know that no extraneous bits are set, and that the high
7631 bit is not set, convert extraction to cheaper one - either
7632 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7633 cases. */
7634 if (flag_expensive_optimizations
7635 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7636 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7637 & ~(((unsigned HOST_WIDE_INT)
7638 GET_MODE_MASK (GET_MODE (pos_rtx)))
7639 >> 1))
7640 == 0)))
7642 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7643 GET_MODE (pos_rtx));
7645 /* Prefer ZERO_EXTENSION, since it gives more information to
7646 backends. */
7647 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7648 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7649 temp = temp1;
7651 pos_rtx = temp;
7654 /* Make POS_RTX unless we already have it and it is correct. If we don't
7655 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7656 be a CONST_INT. */
7657 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7658 pos_rtx = orig_pos_rtx;
7660 else if (pos_rtx == 0)
7661 pos_rtx = GEN_INT (pos);
7663 /* Make the required operation. See if we can use existing rtx. */
7664 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7665 extraction_mode, inner, GEN_INT (len), pos_rtx);
7666 if (! in_dest)
7667 new_rtx = gen_lowpart (mode, new_rtx);
7669 return new_rtx;
7672 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7673 with any other operations in X. Return X without that shift if so. */
7675 static rtx
7676 extract_left_shift (rtx x, int count)
7678 enum rtx_code code = GET_CODE (x);
7679 machine_mode mode = GET_MODE (x);
7680 rtx tem;
7682 switch (code)
7684 case ASHIFT:
7685 /* This is the shift itself. If it is wide enough, we will return
7686 either the value being shifted if the shift count is equal to
7687 COUNT or a shift for the difference. */
7688 if (CONST_INT_P (XEXP (x, 1))
7689 && INTVAL (XEXP (x, 1)) >= count)
7690 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7691 INTVAL (XEXP (x, 1)) - count);
7692 break;
7694 case NEG: case NOT:
7695 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7696 return simplify_gen_unary (code, mode, tem, mode);
7698 break;
7700 case PLUS: case IOR: case XOR: case AND:
7701 /* If we can safely shift this constant and we find the inner shift,
7702 make a new operation. */
7703 if (CONST_INT_P (XEXP (x, 1))
7704 && (UINTVAL (XEXP (x, 1))
7705 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7706 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7708 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7709 return simplify_gen_binary (code, mode, tem,
7710 gen_int_mode (val, mode));
7712 break;
7714 default:
7715 break;
7718 return 0;
7721 /* Look at the expression rooted at X. Look for expressions
7722 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7723 Form these expressions.
7725 Return the new rtx, usually just X.
7727 Also, for machines like the VAX that don't have logical shift insns,
7728 try to convert logical to arithmetic shift operations in cases where
7729 they are equivalent. This undoes the canonicalizations to logical
7730 shifts done elsewhere.
7732 We try, as much as possible, to re-use rtl expressions to save memory.
7734 IN_CODE says what kind of expression we are processing. Normally, it is
7735 SET. In a memory address it is MEM. When processing the arguments of
7736 a comparison or a COMPARE against zero, it is COMPARE. */
7739 make_compound_operation (rtx x, enum rtx_code in_code)
7741 enum rtx_code code = GET_CODE (x);
7742 machine_mode mode = GET_MODE (x);
7743 int mode_width = GET_MODE_PRECISION (mode);
7744 rtx rhs, lhs;
7745 enum rtx_code next_code;
7746 int i, j;
7747 rtx new_rtx = 0;
7748 rtx tem;
7749 const char *fmt;
7751 /* Select the code to be used in recursive calls. Once we are inside an
7752 address, we stay there. If we have a comparison, set to COMPARE,
7753 but once inside, go back to our default of SET. */
7755 next_code = (code == MEM ? MEM
7756 : ((code == COMPARE || COMPARISON_P (x))
7757 && XEXP (x, 1) == const0_rtx) ? COMPARE
7758 : in_code == COMPARE ? SET : in_code);
7760 /* Process depending on the code of this operation. If NEW is set
7761 nonzero, it will be returned. */
7763 switch (code)
7765 case ASHIFT:
7766 /* Convert shifts by constants into multiplications if inside
7767 an address. */
7768 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7769 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7770 && INTVAL (XEXP (x, 1)) >= 0
7771 && SCALAR_INT_MODE_P (mode))
7773 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7774 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7776 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7777 if (GET_CODE (new_rtx) == NEG)
7779 new_rtx = XEXP (new_rtx, 0);
7780 multval = -multval;
7782 multval = trunc_int_for_mode (multval, mode);
7783 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7785 break;
7787 case PLUS:
7788 lhs = XEXP (x, 0);
7789 rhs = XEXP (x, 1);
7790 lhs = make_compound_operation (lhs, next_code);
7791 rhs = make_compound_operation (rhs, next_code);
7792 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7793 && SCALAR_INT_MODE_P (mode))
7795 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7796 XEXP (lhs, 1));
7797 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7799 else if (GET_CODE (lhs) == MULT
7800 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7802 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7803 simplify_gen_unary (NEG, mode,
7804 XEXP (lhs, 1),
7805 mode));
7806 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7808 else
7810 SUBST (XEXP (x, 0), lhs);
7811 SUBST (XEXP (x, 1), rhs);
7812 goto maybe_swap;
7814 x = gen_lowpart (mode, new_rtx);
7815 goto maybe_swap;
7817 case MINUS:
7818 lhs = XEXP (x, 0);
7819 rhs = XEXP (x, 1);
7820 lhs = make_compound_operation (lhs, next_code);
7821 rhs = make_compound_operation (rhs, next_code);
7822 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7823 && SCALAR_INT_MODE_P (mode))
7825 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7826 XEXP (rhs, 1));
7827 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7829 else if (GET_CODE (rhs) == MULT
7830 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7832 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7833 simplify_gen_unary (NEG, mode,
7834 XEXP (rhs, 1),
7835 mode));
7836 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7838 else
7840 SUBST (XEXP (x, 0), lhs);
7841 SUBST (XEXP (x, 1), rhs);
7842 return x;
7844 return gen_lowpart (mode, new_rtx);
7846 case AND:
7847 /* If the second operand is not a constant, we can't do anything
7848 with it. */
7849 if (!CONST_INT_P (XEXP (x, 1)))
7850 break;
7852 /* If the constant is a power of two minus one and the first operand
7853 is a logical right shift, make an extraction. */
7854 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7855 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7857 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7858 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7859 0, in_code == COMPARE);
7862 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7863 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7864 && subreg_lowpart_p (XEXP (x, 0))
7865 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7866 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7868 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7869 next_code);
7870 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7871 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7872 0, in_code == COMPARE);
7874 /* If that didn't give anything, see if the AND simplifies on
7875 its own. */
7876 if (!new_rtx && i >= 0)
7878 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7879 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
7880 0, in_code == COMPARE);
7883 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7884 else if ((GET_CODE (XEXP (x, 0)) == XOR
7885 || GET_CODE (XEXP (x, 0)) == IOR)
7886 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7887 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7888 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7890 /* Apply the distributive law, and then try to make extractions. */
7891 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7892 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7893 XEXP (x, 1)),
7894 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7895 XEXP (x, 1)));
7896 new_rtx = make_compound_operation (new_rtx, in_code);
7899 /* If we are have (and (rotate X C) M) and C is larger than the number
7900 of bits in M, this is an extraction. */
7902 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7903 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7904 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7905 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7907 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7908 new_rtx = make_extraction (mode, new_rtx,
7909 (GET_MODE_PRECISION (mode)
7910 - INTVAL (XEXP (XEXP (x, 0), 1))),
7911 NULL_RTX, i, 1, 0, in_code == COMPARE);
7914 /* On machines without logical shifts, if the operand of the AND is
7915 a logical shift and our mask turns off all the propagated sign
7916 bits, we can replace the logical shift with an arithmetic shift. */
7917 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7918 && !have_insn_for (LSHIFTRT, mode)
7919 && have_insn_for (ASHIFTRT, mode)
7920 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7921 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7922 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7923 && mode_width <= HOST_BITS_PER_WIDE_INT)
7925 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7927 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7928 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7929 SUBST (XEXP (x, 0),
7930 gen_rtx_ASHIFTRT (mode,
7931 make_compound_operation
7932 (XEXP (XEXP (x, 0), 0), next_code),
7933 XEXP (XEXP (x, 0), 1)));
7936 /* If the constant is one less than a power of two, this might be
7937 representable by an extraction even if no shift is present.
7938 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7939 we are in a COMPARE. */
7940 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7941 new_rtx = make_extraction (mode,
7942 make_compound_operation (XEXP (x, 0),
7943 next_code),
7944 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7946 /* If we are in a comparison and this is an AND with a power of two,
7947 convert this into the appropriate bit extract. */
7948 else if (in_code == COMPARE
7949 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7950 new_rtx = make_extraction (mode,
7951 make_compound_operation (XEXP (x, 0),
7952 next_code),
7953 i, NULL_RTX, 1, 1, 0, 1);
7955 break;
7957 case LSHIFTRT:
7958 /* If the sign bit is known to be zero, replace this with an
7959 arithmetic shift. */
7960 if (have_insn_for (ASHIFTRT, mode)
7961 && ! have_insn_for (LSHIFTRT, mode)
7962 && mode_width <= HOST_BITS_PER_WIDE_INT
7963 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7965 new_rtx = gen_rtx_ASHIFTRT (mode,
7966 make_compound_operation (XEXP (x, 0),
7967 next_code),
7968 XEXP (x, 1));
7969 break;
7972 /* ... fall through ... */
7974 case ASHIFTRT:
7975 lhs = XEXP (x, 0);
7976 rhs = XEXP (x, 1);
7978 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7979 this is a SIGN_EXTRACT. */
7980 if (CONST_INT_P (rhs)
7981 && GET_CODE (lhs) == ASHIFT
7982 && CONST_INT_P (XEXP (lhs, 1))
7983 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7984 && INTVAL (XEXP (lhs, 1)) >= 0
7985 && INTVAL (rhs) < mode_width)
7987 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7988 new_rtx = make_extraction (mode, new_rtx,
7989 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7990 NULL_RTX, mode_width - INTVAL (rhs),
7991 code == LSHIFTRT, 0, in_code == COMPARE);
7992 break;
7995 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7996 If so, try to merge the shifts into a SIGN_EXTEND. We could
7997 also do this for some cases of SIGN_EXTRACT, but it doesn't
7998 seem worth the effort; the case checked for occurs on Alpha. */
8000 if (!OBJECT_P (lhs)
8001 && ! (GET_CODE (lhs) == SUBREG
8002 && (OBJECT_P (SUBREG_REG (lhs))))
8003 && CONST_INT_P (rhs)
8004 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8005 && INTVAL (rhs) < mode_width
8006 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
8007 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
8008 0, NULL_RTX, mode_width - INTVAL (rhs),
8009 code == LSHIFTRT, 0, in_code == COMPARE);
8011 break;
8013 case SUBREG:
8014 /* Call ourselves recursively on the inner expression. If we are
8015 narrowing the object and it has a different RTL code from
8016 what it originally did, do this SUBREG as a force_to_mode. */
8018 rtx inner = SUBREG_REG (x), simplified;
8019 enum rtx_code subreg_code = in_code;
8021 /* If in_code is COMPARE, it isn't always safe to pass it through
8022 to the recursive make_compound_operation call. */
8023 if (subreg_code == COMPARE
8024 && (!subreg_lowpart_p (x)
8025 || GET_CODE (inner) == SUBREG
8026 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8027 is (const_int 0), rather than
8028 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
8029 || (GET_CODE (inner) == AND
8030 && CONST_INT_P (XEXP (inner, 1))
8031 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8032 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8033 >= GET_MODE_BITSIZE (mode))))
8034 subreg_code = SET;
8036 tem = make_compound_operation (inner, subreg_code);
8038 simplified
8039 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8040 if (simplified)
8041 tem = simplified;
8043 if (GET_CODE (tem) != GET_CODE (inner)
8044 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8045 && subreg_lowpart_p (x))
8047 rtx newer
8048 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
8050 /* If we have something other than a SUBREG, we might have
8051 done an expansion, so rerun ourselves. */
8052 if (GET_CODE (newer) != SUBREG)
8053 newer = make_compound_operation (newer, in_code);
8055 /* force_to_mode can expand compounds. If it just re-expanded the
8056 compound, use gen_lowpart to convert to the desired mode. */
8057 if (rtx_equal_p (newer, x)
8058 /* Likewise if it re-expanded the compound only partially.
8059 This happens for SUBREG of ZERO_EXTRACT if they extract
8060 the same number of bits. */
8061 || (GET_CODE (newer) == SUBREG
8062 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8063 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8064 && GET_CODE (inner) == AND
8065 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8066 return gen_lowpart (GET_MODE (x), tem);
8068 return newer;
8071 if (simplified)
8072 return tem;
8074 break;
8076 default:
8077 break;
8080 if (new_rtx)
8082 x = gen_lowpart (mode, new_rtx);
8083 code = GET_CODE (x);
8086 /* Now recursively process each operand of this operation. We need to
8087 handle ZERO_EXTEND specially so that we don't lose track of the
8088 inner mode. */
8089 if (GET_CODE (x) == ZERO_EXTEND)
8091 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8092 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8093 new_rtx, GET_MODE (XEXP (x, 0)));
8094 if (tem)
8095 return tem;
8096 SUBST (XEXP (x, 0), new_rtx);
8097 return x;
8100 fmt = GET_RTX_FORMAT (code);
8101 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8102 if (fmt[i] == 'e')
8104 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8105 SUBST (XEXP (x, i), new_rtx);
8107 else if (fmt[i] == 'E')
8108 for (j = 0; j < XVECLEN (x, i); j++)
8110 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8111 SUBST (XVECEXP (x, i, j), new_rtx);
8114 maybe_swap:
8115 /* If this is a commutative operation, the changes to the operands
8116 may have made it noncanonical. */
8117 if (COMMUTATIVE_ARITH_P (x)
8118 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8120 tem = XEXP (x, 0);
8121 SUBST (XEXP (x, 0), XEXP (x, 1));
8122 SUBST (XEXP (x, 1), tem);
8125 return x;
8128 /* Given M see if it is a value that would select a field of bits
8129 within an item, but not the entire word. Return -1 if not.
8130 Otherwise, return the starting position of the field, where 0 is the
8131 low-order bit.
8133 *PLEN is set to the length of the field. */
8135 static int
8136 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8138 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8139 int pos = m ? ctz_hwi (m) : -1;
8140 int len = 0;
8142 if (pos >= 0)
8143 /* Now shift off the low-order zero bits and see if we have a
8144 power of two minus 1. */
8145 len = exact_log2 ((m >> pos) + 1);
8147 if (len <= 0)
8148 pos = -1;
8150 *plen = len;
8151 return pos;
8154 /* If X refers to a register that equals REG in value, replace these
8155 references with REG. */
8156 static rtx
8157 canon_reg_for_combine (rtx x, rtx reg)
8159 rtx op0, op1, op2;
8160 const char *fmt;
8161 int i;
8162 bool copied;
8164 enum rtx_code code = GET_CODE (x);
8165 switch (GET_RTX_CLASS (code))
8167 case RTX_UNARY:
8168 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8169 if (op0 != XEXP (x, 0))
8170 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8171 GET_MODE (reg));
8172 break;
8174 case RTX_BIN_ARITH:
8175 case RTX_COMM_ARITH:
8176 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8177 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8178 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8179 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8180 break;
8182 case RTX_COMPARE:
8183 case RTX_COMM_COMPARE:
8184 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8185 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8186 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8187 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8188 GET_MODE (op0), op0, op1);
8189 break;
8191 case RTX_TERNARY:
8192 case RTX_BITFIELD_OPS:
8193 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8194 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8195 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8196 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8197 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8198 GET_MODE (op0), op0, op1, op2);
8200 case RTX_OBJ:
8201 if (REG_P (x))
8203 if (rtx_equal_p (get_last_value (reg), x)
8204 || rtx_equal_p (reg, get_last_value (x)))
8205 return reg;
8206 else
8207 break;
8210 /* fall through */
8212 default:
8213 fmt = GET_RTX_FORMAT (code);
8214 copied = false;
8215 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8216 if (fmt[i] == 'e')
8218 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8219 if (op != XEXP (x, i))
8221 if (!copied)
8223 copied = true;
8224 x = copy_rtx (x);
8226 XEXP (x, i) = op;
8229 else if (fmt[i] == 'E')
8231 int j;
8232 for (j = 0; j < XVECLEN (x, i); j++)
8234 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8235 if (op != XVECEXP (x, i, j))
8237 if (!copied)
8239 copied = true;
8240 x = copy_rtx (x);
8242 XVECEXP (x, i, j) = op;
8247 break;
8250 return x;
8253 /* Return X converted to MODE. If the value is already truncated to
8254 MODE we can just return a subreg even though in the general case we
8255 would need an explicit truncation. */
8257 static rtx
8258 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8260 if (!CONST_INT_P (x)
8261 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8262 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8263 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8265 /* Bit-cast X into an integer mode. */
8266 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8267 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8268 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8269 x, GET_MODE (x));
8272 return gen_lowpart (mode, x);
8275 /* See if X can be simplified knowing that we will only refer to it in
8276 MODE and will only refer to those bits that are nonzero in MASK.
8277 If other bits are being computed or if masking operations are done
8278 that select a superset of the bits in MASK, they can sometimes be
8279 ignored.
8281 Return a possibly simplified expression, but always convert X to
8282 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8284 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8285 are all off in X. This is used when X will be complemented, by either
8286 NOT, NEG, or XOR. */
8288 static rtx
8289 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8290 int just_select)
8292 enum rtx_code code = GET_CODE (x);
8293 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8294 machine_mode op_mode;
8295 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8296 rtx op0, op1, temp;
8298 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8299 code below will do the wrong thing since the mode of such an
8300 expression is VOIDmode.
8302 Also do nothing if X is a CLOBBER; this can happen if X was
8303 the return value from a call to gen_lowpart. */
8304 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8305 return x;
8307 /* We want to perform the operation in its present mode unless we know
8308 that the operation is valid in MODE, in which case we do the operation
8309 in MODE. */
8310 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8311 && have_insn_for (code, mode))
8312 ? mode : GET_MODE (x));
8314 /* It is not valid to do a right-shift in a narrower mode
8315 than the one it came in with. */
8316 if ((code == LSHIFTRT || code == ASHIFTRT)
8317 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8318 op_mode = GET_MODE (x);
8320 /* Truncate MASK to fit OP_MODE. */
8321 if (op_mode)
8322 mask &= GET_MODE_MASK (op_mode);
8324 /* When we have an arithmetic operation, or a shift whose count we
8325 do not know, we need to assume that all bits up to the highest-order
8326 bit in MASK will be needed. This is how we form such a mask. */
8327 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8328 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8329 else
8330 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8331 - 1);
8333 /* Determine what bits of X are guaranteed to be (non)zero. */
8334 nonzero = nonzero_bits (x, mode);
8336 /* If none of the bits in X are needed, return a zero. */
8337 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8338 x = const0_rtx;
8340 /* If X is a CONST_INT, return a new one. Do this here since the
8341 test below will fail. */
8342 if (CONST_INT_P (x))
8344 if (SCALAR_INT_MODE_P (mode))
8345 return gen_int_mode (INTVAL (x) & mask, mode);
8346 else
8348 x = GEN_INT (INTVAL (x) & mask);
8349 return gen_lowpart_common (mode, x);
8353 /* If X is narrower than MODE and we want all the bits in X's mode, just
8354 get X in the proper mode. */
8355 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8356 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8357 return gen_lowpart (mode, x);
8359 /* We can ignore the effect of a SUBREG if it narrows the mode or
8360 if the constant masks to zero all the bits the mode doesn't have. */
8361 if (GET_CODE (x) == SUBREG
8362 && subreg_lowpart_p (x)
8363 && ((GET_MODE_SIZE (GET_MODE (x))
8364 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8365 || (0 == (mask
8366 & GET_MODE_MASK (GET_MODE (x))
8367 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8368 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8370 /* The arithmetic simplifications here only work for scalar integer modes. */
8371 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8372 return gen_lowpart_or_truncate (mode, x);
8374 switch (code)
8376 case CLOBBER:
8377 /* If X is a (clobber (const_int)), return it since we know we are
8378 generating something that won't match. */
8379 return x;
8381 case SIGN_EXTEND:
8382 case ZERO_EXTEND:
8383 case ZERO_EXTRACT:
8384 case SIGN_EXTRACT:
8385 x = expand_compound_operation (x);
8386 if (GET_CODE (x) != code)
8387 return force_to_mode (x, mode, mask, next_select);
8388 break;
8390 case TRUNCATE:
8391 /* Similarly for a truncate. */
8392 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8394 case AND:
8395 /* If this is an AND with a constant, convert it into an AND
8396 whose constant is the AND of that constant with MASK. If it
8397 remains an AND of MASK, delete it since it is redundant. */
8399 if (CONST_INT_P (XEXP (x, 1)))
8401 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8402 mask & INTVAL (XEXP (x, 1)));
8404 /* If X is still an AND, see if it is an AND with a mask that
8405 is just some low-order bits. If so, and it is MASK, we don't
8406 need it. */
8408 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8409 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8410 == mask))
8411 x = XEXP (x, 0);
8413 /* If it remains an AND, try making another AND with the bits
8414 in the mode mask that aren't in MASK turned on. If the
8415 constant in the AND is wide enough, this might make a
8416 cheaper constant. */
8418 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8419 && GET_MODE_MASK (GET_MODE (x)) != mask
8420 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8422 unsigned HOST_WIDE_INT cval
8423 = UINTVAL (XEXP (x, 1))
8424 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8425 rtx y;
8427 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8428 gen_int_mode (cval, GET_MODE (x)));
8429 if (set_src_cost (y, GET_MODE (x), optimize_this_for_speed_p)
8430 < set_src_cost (x, GET_MODE (x), optimize_this_for_speed_p))
8431 x = y;
8434 break;
8437 goto binop;
8439 case PLUS:
8440 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8441 low-order bits (as in an alignment operation) and FOO is already
8442 aligned to that boundary, mask C1 to that boundary as well.
8443 This may eliminate that PLUS and, later, the AND. */
8446 unsigned int width = GET_MODE_PRECISION (mode);
8447 unsigned HOST_WIDE_INT smask = mask;
8449 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8450 number, sign extend it. */
8452 if (width < HOST_BITS_PER_WIDE_INT
8453 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8454 smask |= HOST_WIDE_INT_M1U << width;
8456 if (CONST_INT_P (XEXP (x, 1))
8457 && exact_log2 (- smask) >= 0
8458 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8459 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8460 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8461 (INTVAL (XEXP (x, 1)) & smask)),
8462 mode, smask, next_select);
8465 /* ... fall through ... */
8467 case MULT:
8468 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8469 most significant bit in MASK since carries from those bits will
8470 affect the bits we are interested in. */
8471 mask = fuller_mask;
8472 goto binop;
8474 case MINUS:
8475 /* If X is (minus C Y) where C's least set bit is larger than any bit
8476 in the mask, then we may replace with (neg Y). */
8477 if (CONST_INT_P (XEXP (x, 0))
8478 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8480 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8481 GET_MODE (x));
8482 return force_to_mode (x, mode, mask, next_select);
8485 /* Similarly, if C contains every bit in the fuller_mask, then we may
8486 replace with (not Y). */
8487 if (CONST_INT_P (XEXP (x, 0))
8488 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8490 x = simplify_gen_unary (NOT, GET_MODE (x),
8491 XEXP (x, 1), GET_MODE (x));
8492 return force_to_mode (x, mode, mask, next_select);
8495 mask = fuller_mask;
8496 goto binop;
8498 case IOR:
8499 case XOR:
8500 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8501 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8502 operation which may be a bitfield extraction. Ensure that the
8503 constant we form is not wider than the mode of X. */
8505 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8506 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8507 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8508 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8509 && CONST_INT_P (XEXP (x, 1))
8510 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8511 + floor_log2 (INTVAL (XEXP (x, 1))))
8512 < GET_MODE_PRECISION (GET_MODE (x)))
8513 && (UINTVAL (XEXP (x, 1))
8514 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8516 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8517 << INTVAL (XEXP (XEXP (x, 0), 1)),
8518 GET_MODE (x));
8519 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8520 XEXP (XEXP (x, 0), 0), temp);
8521 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8522 XEXP (XEXP (x, 0), 1));
8523 return force_to_mode (x, mode, mask, next_select);
8526 binop:
8527 /* For most binary operations, just propagate into the operation and
8528 change the mode if we have an operation of that mode. */
8530 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8531 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8533 /* If we ended up truncating both operands, truncate the result of the
8534 operation instead. */
8535 if (GET_CODE (op0) == TRUNCATE
8536 && GET_CODE (op1) == TRUNCATE)
8538 op0 = XEXP (op0, 0);
8539 op1 = XEXP (op1, 0);
8542 op0 = gen_lowpart_or_truncate (op_mode, op0);
8543 op1 = gen_lowpart_or_truncate (op_mode, op1);
8545 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8546 x = simplify_gen_binary (code, op_mode, op0, op1);
8547 break;
8549 case ASHIFT:
8550 /* For left shifts, do the same, but just for the first operand.
8551 However, we cannot do anything with shifts where we cannot
8552 guarantee that the counts are smaller than the size of the mode
8553 because such a count will have a different meaning in a
8554 wider mode. */
8556 if (! (CONST_INT_P (XEXP (x, 1))
8557 && INTVAL (XEXP (x, 1)) >= 0
8558 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8559 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8560 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8561 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8562 break;
8564 /* If the shift count is a constant and we can do arithmetic in
8565 the mode of the shift, refine which bits we need. Otherwise, use the
8566 conservative form of the mask. */
8567 if (CONST_INT_P (XEXP (x, 1))
8568 && INTVAL (XEXP (x, 1)) >= 0
8569 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8570 && HWI_COMPUTABLE_MODE_P (op_mode))
8571 mask >>= INTVAL (XEXP (x, 1));
8572 else
8573 mask = fuller_mask;
8575 op0 = gen_lowpart_or_truncate (op_mode,
8576 force_to_mode (XEXP (x, 0), op_mode,
8577 mask, next_select));
8579 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8580 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8581 break;
8583 case LSHIFTRT:
8584 /* Here we can only do something if the shift count is a constant,
8585 this shift constant is valid for the host, and we can do arithmetic
8586 in OP_MODE. */
8588 if (CONST_INT_P (XEXP (x, 1))
8589 && INTVAL (XEXP (x, 1)) >= 0
8590 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8591 && HWI_COMPUTABLE_MODE_P (op_mode))
8593 rtx inner = XEXP (x, 0);
8594 unsigned HOST_WIDE_INT inner_mask;
8596 /* Select the mask of the bits we need for the shift operand. */
8597 inner_mask = mask << INTVAL (XEXP (x, 1));
8599 /* We can only change the mode of the shift if we can do arithmetic
8600 in the mode of the shift and INNER_MASK is no wider than the
8601 width of X's mode. */
8602 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8603 op_mode = GET_MODE (x);
8605 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8607 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8608 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8611 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8612 shift and AND produces only copies of the sign bit (C2 is one less
8613 than a power of two), we can do this with just a shift. */
8615 if (GET_CODE (x) == LSHIFTRT
8616 && CONST_INT_P (XEXP (x, 1))
8617 /* The shift puts one of the sign bit copies in the least significant
8618 bit. */
8619 && ((INTVAL (XEXP (x, 1))
8620 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8621 >= GET_MODE_PRECISION (GET_MODE (x)))
8622 && exact_log2 (mask + 1) >= 0
8623 /* Number of bits left after the shift must be more than the mask
8624 needs. */
8625 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8626 <= GET_MODE_PRECISION (GET_MODE (x)))
8627 /* Must be more sign bit copies than the mask needs. */
8628 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8629 >= exact_log2 (mask + 1)))
8630 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8631 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8632 - exact_log2 (mask + 1)));
8634 goto shiftrt;
8636 case ASHIFTRT:
8637 /* If we are just looking for the sign bit, we don't need this shift at
8638 all, even if it has a variable count. */
8639 if (val_signbit_p (GET_MODE (x), mask))
8640 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8642 /* If this is a shift by a constant, get a mask that contains those bits
8643 that are not copies of the sign bit. We then have two cases: If
8644 MASK only includes those bits, this can be a logical shift, which may
8645 allow simplifications. If MASK is a single-bit field not within
8646 those bits, we are requesting a copy of the sign bit and hence can
8647 shift the sign bit to the appropriate location. */
8649 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8650 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8652 int i;
8654 /* If the considered data is wider than HOST_WIDE_INT, we can't
8655 represent a mask for all its bits in a single scalar.
8656 But we only care about the lower bits, so calculate these. */
8658 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8660 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8662 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8663 is the number of bits a full-width mask would have set.
8664 We need only shift if these are fewer than nonzero can
8665 hold. If not, we must keep all bits set in nonzero. */
8667 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8668 < HOST_BITS_PER_WIDE_INT)
8669 nonzero >>= INTVAL (XEXP (x, 1))
8670 + HOST_BITS_PER_WIDE_INT
8671 - GET_MODE_PRECISION (GET_MODE (x)) ;
8673 else
8675 nonzero = GET_MODE_MASK (GET_MODE (x));
8676 nonzero >>= INTVAL (XEXP (x, 1));
8679 if ((mask & ~nonzero) == 0)
8681 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8682 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8683 if (GET_CODE (x) != ASHIFTRT)
8684 return force_to_mode (x, mode, mask, next_select);
8687 else if ((i = exact_log2 (mask)) >= 0)
8689 x = simplify_shift_const
8690 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8691 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8693 if (GET_CODE (x) != ASHIFTRT)
8694 return force_to_mode (x, mode, mask, next_select);
8698 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8699 even if the shift count isn't a constant. */
8700 if (mask == 1)
8701 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8702 XEXP (x, 0), XEXP (x, 1));
8704 shiftrt:
8706 /* If this is a zero- or sign-extension operation that just affects bits
8707 we don't care about, remove it. Be sure the call above returned
8708 something that is still a shift. */
8710 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8711 && CONST_INT_P (XEXP (x, 1))
8712 && INTVAL (XEXP (x, 1)) >= 0
8713 && (INTVAL (XEXP (x, 1))
8714 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8715 && GET_CODE (XEXP (x, 0)) == ASHIFT
8716 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8717 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8718 next_select);
8720 break;
8722 case ROTATE:
8723 case ROTATERT:
8724 /* If the shift count is constant and we can do computations
8725 in the mode of X, compute where the bits we care about are.
8726 Otherwise, we can't do anything. Don't change the mode of
8727 the shift or propagate MODE into the shift, though. */
8728 if (CONST_INT_P (XEXP (x, 1))
8729 && INTVAL (XEXP (x, 1)) >= 0)
8731 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8732 GET_MODE (x),
8733 gen_int_mode (mask, GET_MODE (x)),
8734 XEXP (x, 1));
8735 if (temp && CONST_INT_P (temp))
8736 x = simplify_gen_binary (code, GET_MODE (x),
8737 force_to_mode (XEXP (x, 0), GET_MODE (x),
8738 INTVAL (temp), next_select),
8739 XEXP (x, 1));
8741 break;
8743 case NEG:
8744 /* If we just want the low-order bit, the NEG isn't needed since it
8745 won't change the low-order bit. */
8746 if (mask == 1)
8747 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8749 /* We need any bits less significant than the most significant bit in
8750 MASK since carries from those bits will affect the bits we are
8751 interested in. */
8752 mask = fuller_mask;
8753 goto unop;
8755 case NOT:
8756 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8757 same as the XOR case above. Ensure that the constant we form is not
8758 wider than the mode of X. */
8760 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8761 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8762 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8763 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8764 < GET_MODE_PRECISION (GET_MODE (x)))
8765 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8767 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8768 GET_MODE (x));
8769 temp = simplify_gen_binary (XOR, GET_MODE (x),
8770 XEXP (XEXP (x, 0), 0), temp);
8771 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8772 temp, XEXP (XEXP (x, 0), 1));
8774 return force_to_mode (x, mode, mask, next_select);
8777 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8778 use the full mask inside the NOT. */
8779 mask = fuller_mask;
8781 unop:
8782 op0 = gen_lowpart_or_truncate (op_mode,
8783 force_to_mode (XEXP (x, 0), mode, mask,
8784 next_select));
8785 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8786 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8787 break;
8789 case NE:
8790 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8791 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8792 which is equal to STORE_FLAG_VALUE. */
8793 if ((mask & ~STORE_FLAG_VALUE) == 0
8794 && XEXP (x, 1) == const0_rtx
8795 && GET_MODE (XEXP (x, 0)) == mode
8796 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8797 && (nonzero_bits (XEXP (x, 0), mode)
8798 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8799 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8801 break;
8803 case IF_THEN_ELSE:
8804 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8805 written in a narrower mode. We play it safe and do not do so. */
8807 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8808 force_to_mode (XEXP (x, 1), mode,
8809 mask, next_select));
8810 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8811 force_to_mode (XEXP (x, 2), mode,
8812 mask, next_select));
8813 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8814 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8815 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8816 op0, op1);
8817 break;
8819 default:
8820 break;
8823 /* Ensure we return a value of the proper mode. */
8824 return gen_lowpart_or_truncate (mode, x);
8827 /* Return nonzero if X is an expression that has one of two values depending on
8828 whether some other value is zero or nonzero. In that case, we return the
8829 value that is being tested, *PTRUE is set to the value if the rtx being
8830 returned has a nonzero value, and *PFALSE is set to the other alternative.
8832 If we return zero, we set *PTRUE and *PFALSE to X. */
8834 static rtx
8835 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8837 machine_mode mode = GET_MODE (x);
8838 enum rtx_code code = GET_CODE (x);
8839 rtx cond0, cond1, true0, true1, false0, false1;
8840 unsigned HOST_WIDE_INT nz;
8842 /* If we are comparing a value against zero, we are done. */
8843 if ((code == NE || code == EQ)
8844 && XEXP (x, 1) == const0_rtx)
8846 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8847 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8848 return XEXP (x, 0);
8851 /* If this is a unary operation whose operand has one of two values, apply
8852 our opcode to compute those values. */
8853 else if (UNARY_P (x)
8854 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8856 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8857 *pfalse = simplify_gen_unary (code, mode, false0,
8858 GET_MODE (XEXP (x, 0)));
8859 return cond0;
8862 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8863 make can't possibly match and would suppress other optimizations. */
8864 else if (code == COMPARE)
8867 /* If this is a binary operation, see if either side has only one of two
8868 values. If either one does or if both do and they are conditional on
8869 the same value, compute the new true and false values. */
8870 else if (BINARY_P (x))
8872 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8873 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8875 if ((cond0 != 0 || cond1 != 0)
8876 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8878 /* If if_then_else_cond returned zero, then true/false are the
8879 same rtl. We must copy one of them to prevent invalid rtl
8880 sharing. */
8881 if (cond0 == 0)
8882 true0 = copy_rtx (true0);
8883 else if (cond1 == 0)
8884 true1 = copy_rtx (true1);
8886 if (COMPARISON_P (x))
8888 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8889 true0, true1);
8890 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8891 false0, false1);
8893 else
8895 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8896 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8899 return cond0 ? cond0 : cond1;
8902 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8903 operands is zero when the other is nonzero, and vice-versa,
8904 and STORE_FLAG_VALUE is 1 or -1. */
8906 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8907 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8908 || code == UMAX)
8909 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8911 rtx op0 = XEXP (XEXP (x, 0), 1);
8912 rtx op1 = XEXP (XEXP (x, 1), 1);
8914 cond0 = XEXP (XEXP (x, 0), 0);
8915 cond1 = XEXP (XEXP (x, 1), 0);
8917 if (COMPARISON_P (cond0)
8918 && COMPARISON_P (cond1)
8919 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8920 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8921 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8922 || ((swap_condition (GET_CODE (cond0))
8923 == reversed_comparison_code (cond1, NULL))
8924 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8925 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8926 && ! side_effects_p (x))
8928 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8929 *pfalse = simplify_gen_binary (MULT, mode,
8930 (code == MINUS
8931 ? simplify_gen_unary (NEG, mode,
8932 op1, mode)
8933 : op1),
8934 const_true_rtx);
8935 return cond0;
8939 /* Similarly for MULT, AND and UMIN, except that for these the result
8940 is always zero. */
8941 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8942 && (code == MULT || code == AND || code == UMIN)
8943 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8945 cond0 = XEXP (XEXP (x, 0), 0);
8946 cond1 = XEXP (XEXP (x, 1), 0);
8948 if (COMPARISON_P (cond0)
8949 && COMPARISON_P (cond1)
8950 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8951 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8952 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8953 || ((swap_condition (GET_CODE (cond0))
8954 == reversed_comparison_code (cond1, NULL))
8955 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8956 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8957 && ! side_effects_p (x))
8959 *ptrue = *pfalse = const0_rtx;
8960 return cond0;
8965 else if (code == IF_THEN_ELSE)
8967 /* If we have IF_THEN_ELSE already, extract the condition and
8968 canonicalize it if it is NE or EQ. */
8969 cond0 = XEXP (x, 0);
8970 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8971 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8972 return XEXP (cond0, 0);
8973 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8975 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8976 return XEXP (cond0, 0);
8978 else
8979 return cond0;
8982 /* If X is a SUBREG, we can narrow both the true and false values
8983 if the inner expression, if there is a condition. */
8984 else if (code == SUBREG
8985 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8986 &true0, &false0)))
8988 true0 = simplify_gen_subreg (mode, true0,
8989 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8990 false0 = simplify_gen_subreg (mode, false0,
8991 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8992 if (true0 && false0)
8994 *ptrue = true0;
8995 *pfalse = false0;
8996 return cond0;
9000 /* If X is a constant, this isn't special and will cause confusions
9001 if we treat it as such. Likewise if it is equivalent to a constant. */
9002 else if (CONSTANT_P (x)
9003 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9006 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9007 will be least confusing to the rest of the compiler. */
9008 else if (mode == BImode)
9010 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9011 return x;
9014 /* If X is known to be either 0 or -1, those are the true and
9015 false values when testing X. */
9016 else if (x == constm1_rtx || x == const0_rtx
9017 || (mode != VOIDmode
9018 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
9020 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9021 return x;
9024 /* Likewise for 0 or a single bit. */
9025 else if (HWI_COMPUTABLE_MODE_P (mode)
9026 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
9028 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9029 return x;
9032 /* Otherwise fail; show no condition with true and false values the same. */
9033 *ptrue = *pfalse = x;
9034 return 0;
9037 /* Return the value of expression X given the fact that condition COND
9038 is known to be true when applied to REG as its first operand and VAL
9039 as its second. X is known to not be shared and so can be modified in
9040 place.
9042 We only handle the simplest cases, and specifically those cases that
9043 arise with IF_THEN_ELSE expressions. */
9045 static rtx
9046 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9048 enum rtx_code code = GET_CODE (x);
9049 const char *fmt;
9050 int i, j;
9052 if (side_effects_p (x))
9053 return x;
9055 /* If either operand of the condition is a floating point value,
9056 then we have to avoid collapsing an EQ comparison. */
9057 if (cond == EQ
9058 && rtx_equal_p (x, reg)
9059 && ! FLOAT_MODE_P (GET_MODE (x))
9060 && ! FLOAT_MODE_P (GET_MODE (val)))
9061 return val;
9063 if (cond == UNEQ && rtx_equal_p (x, reg))
9064 return val;
9066 /* If X is (abs REG) and we know something about REG's relationship
9067 with zero, we may be able to simplify this. */
9069 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9070 switch (cond)
9072 case GE: case GT: case EQ:
9073 return XEXP (x, 0);
9074 case LT: case LE:
9075 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9076 XEXP (x, 0),
9077 GET_MODE (XEXP (x, 0)));
9078 default:
9079 break;
9082 /* The only other cases we handle are MIN, MAX, and comparisons if the
9083 operands are the same as REG and VAL. */
9085 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9087 if (rtx_equal_p (XEXP (x, 0), val))
9089 std::swap (val, reg);
9090 cond = swap_condition (cond);
9093 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9095 if (COMPARISON_P (x))
9097 if (comparison_dominates_p (cond, code))
9098 return const_true_rtx;
9100 code = reversed_comparison_code (x, NULL);
9101 if (code != UNKNOWN
9102 && comparison_dominates_p (cond, code))
9103 return const0_rtx;
9104 else
9105 return x;
9107 else if (code == SMAX || code == SMIN
9108 || code == UMIN || code == UMAX)
9110 int unsignedp = (code == UMIN || code == UMAX);
9112 /* Do not reverse the condition when it is NE or EQ.
9113 This is because we cannot conclude anything about
9114 the value of 'SMAX (x, y)' when x is not equal to y,
9115 but we can when x equals y. */
9116 if ((code == SMAX || code == UMAX)
9117 && ! (cond == EQ || cond == NE))
9118 cond = reverse_condition (cond);
9120 switch (cond)
9122 case GE: case GT:
9123 return unsignedp ? x : XEXP (x, 1);
9124 case LE: case LT:
9125 return unsignedp ? x : XEXP (x, 0);
9126 case GEU: case GTU:
9127 return unsignedp ? XEXP (x, 1) : x;
9128 case LEU: case LTU:
9129 return unsignedp ? XEXP (x, 0) : x;
9130 default:
9131 break;
9136 else if (code == SUBREG)
9138 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9139 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9141 if (SUBREG_REG (x) != r)
9143 /* We must simplify subreg here, before we lose track of the
9144 original inner_mode. */
9145 new_rtx = simplify_subreg (GET_MODE (x), r,
9146 inner_mode, SUBREG_BYTE (x));
9147 if (new_rtx)
9148 return new_rtx;
9149 else
9150 SUBST (SUBREG_REG (x), r);
9153 return x;
9155 /* We don't have to handle SIGN_EXTEND here, because even in the
9156 case of replacing something with a modeless CONST_INT, a
9157 CONST_INT is already (supposed to be) a valid sign extension for
9158 its narrower mode, which implies it's already properly
9159 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9160 story is different. */
9161 else if (code == ZERO_EXTEND)
9163 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9164 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9166 if (XEXP (x, 0) != r)
9168 /* We must simplify the zero_extend here, before we lose
9169 track of the original inner_mode. */
9170 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9171 r, inner_mode);
9172 if (new_rtx)
9173 return new_rtx;
9174 else
9175 SUBST (XEXP (x, 0), r);
9178 return x;
9181 fmt = GET_RTX_FORMAT (code);
9182 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9184 if (fmt[i] == 'e')
9185 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9186 else if (fmt[i] == 'E')
9187 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9188 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9189 cond, reg, val));
9192 return x;
9195 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9196 assignment as a field assignment. */
9198 static int
9199 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9201 if (widen_x && GET_MODE (x) != GET_MODE (y))
9203 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (y)))
9204 return 0;
9205 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9206 return 0;
9207 /* For big endian, adjust the memory offset. */
9208 if (BYTES_BIG_ENDIAN)
9209 x = adjust_address_nv (x, GET_MODE (y),
9210 -subreg_lowpart_offset (GET_MODE (x),
9211 GET_MODE (y)));
9212 else
9213 x = adjust_address_nv (x, GET_MODE (y), 0);
9216 if (x == y || rtx_equal_p (x, y))
9217 return 1;
9219 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9220 return 0;
9222 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9223 Note that all SUBREGs of MEM are paradoxical; otherwise they
9224 would have been rewritten. */
9225 if (MEM_P (x) && GET_CODE (y) == SUBREG
9226 && MEM_P (SUBREG_REG (y))
9227 && rtx_equal_p (SUBREG_REG (y),
9228 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9229 return 1;
9231 if (MEM_P (y) && GET_CODE (x) == SUBREG
9232 && MEM_P (SUBREG_REG (x))
9233 && rtx_equal_p (SUBREG_REG (x),
9234 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9235 return 1;
9237 /* We used to see if get_last_value of X and Y were the same but that's
9238 not correct. In one direction, we'll cause the assignment to have
9239 the wrong destination and in the case, we'll import a register into this
9240 insn that might have already have been dead. So fail if none of the
9241 above cases are true. */
9242 return 0;
9245 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9246 Return that assignment if so.
9248 We only handle the most common cases. */
9250 static rtx
9251 make_field_assignment (rtx x)
9253 rtx dest = SET_DEST (x);
9254 rtx src = SET_SRC (x);
9255 rtx assign;
9256 rtx rhs, lhs;
9257 HOST_WIDE_INT c1;
9258 HOST_WIDE_INT pos;
9259 unsigned HOST_WIDE_INT len;
9260 rtx other;
9261 machine_mode mode;
9263 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9264 a clear of a one-bit field. We will have changed it to
9265 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9266 for a SUBREG. */
9268 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9269 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9270 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9271 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9273 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9274 1, 1, 1, 0);
9275 if (assign != 0)
9276 return gen_rtx_SET (assign, const0_rtx);
9277 return x;
9280 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9281 && subreg_lowpart_p (XEXP (src, 0))
9282 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9283 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9284 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9285 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9286 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9287 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9289 assign = make_extraction (VOIDmode, dest, 0,
9290 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9291 1, 1, 1, 0);
9292 if (assign != 0)
9293 return gen_rtx_SET (assign, const0_rtx);
9294 return x;
9297 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9298 one-bit field. */
9299 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9300 && XEXP (XEXP (src, 0), 0) == const1_rtx
9301 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9303 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9304 1, 1, 1, 0);
9305 if (assign != 0)
9306 return gen_rtx_SET (assign, const1_rtx);
9307 return x;
9310 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9311 SRC is an AND with all bits of that field set, then we can discard
9312 the AND. */
9313 if (GET_CODE (dest) == ZERO_EXTRACT
9314 && CONST_INT_P (XEXP (dest, 1))
9315 && GET_CODE (src) == AND
9316 && CONST_INT_P (XEXP (src, 1)))
9318 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9319 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9320 unsigned HOST_WIDE_INT ze_mask;
9322 if (width >= HOST_BITS_PER_WIDE_INT)
9323 ze_mask = -1;
9324 else
9325 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9327 /* Complete overlap. We can remove the source AND. */
9328 if ((and_mask & ze_mask) == ze_mask)
9329 return gen_rtx_SET (dest, XEXP (src, 0));
9331 /* Partial overlap. We can reduce the source AND. */
9332 if ((and_mask & ze_mask) != and_mask)
9334 mode = GET_MODE (src);
9335 src = gen_rtx_AND (mode, XEXP (src, 0),
9336 gen_int_mode (and_mask & ze_mask, mode));
9337 return gen_rtx_SET (dest, src);
9341 /* The other case we handle is assignments into a constant-position
9342 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9343 a mask that has all one bits except for a group of zero bits and
9344 OTHER is known to have zeros where C1 has ones, this is such an
9345 assignment. Compute the position and length from C1. Shift OTHER
9346 to the appropriate position, force it to the required mode, and
9347 make the extraction. Check for the AND in both operands. */
9349 /* One or more SUBREGs might obscure the constant-position field
9350 assignment. The first one we are likely to encounter is an outer
9351 narrowing SUBREG, which we can just strip for the purposes of
9352 identifying the constant-field assignment. */
9353 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src))
9354 src = SUBREG_REG (src);
9356 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9357 return x;
9359 rhs = expand_compound_operation (XEXP (src, 0));
9360 lhs = expand_compound_operation (XEXP (src, 1));
9362 if (GET_CODE (rhs) == AND
9363 && CONST_INT_P (XEXP (rhs, 1))
9364 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9365 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9366 /* The second SUBREG that might get in the way is a paradoxical
9367 SUBREG around the first operand of the AND. We want to
9368 pretend the operand is as wide as the destination here. We
9369 do this by adjusting the MEM to wider mode for the sole
9370 purpose of the call to rtx_equal_for_field_assignment_p. Also
9371 note this trick only works for MEMs. */
9372 else if (GET_CODE (rhs) == AND
9373 && paradoxical_subreg_p (XEXP (rhs, 0))
9374 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9375 && CONST_INT_P (XEXP (rhs, 1))
9376 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9377 dest, true))
9378 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9379 else if (GET_CODE (lhs) == AND
9380 && CONST_INT_P (XEXP (lhs, 1))
9381 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9382 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9383 /* The second SUBREG that might get in the way is a paradoxical
9384 SUBREG around the first operand of the AND. We want to
9385 pretend the operand is as wide as the destination here. We
9386 do this by adjusting the MEM to wider mode for the sole
9387 purpose of the call to rtx_equal_for_field_assignment_p. Also
9388 note this trick only works for MEMs. */
9389 else if (GET_CODE (lhs) == AND
9390 && paradoxical_subreg_p (XEXP (lhs, 0))
9391 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9392 && CONST_INT_P (XEXP (lhs, 1))
9393 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9394 dest, true))
9395 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9396 else
9397 return x;
9399 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9400 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9401 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9402 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9403 return x;
9405 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9406 if (assign == 0)
9407 return x;
9409 /* The mode to use for the source is the mode of the assignment, or of
9410 what is inside a possible STRICT_LOW_PART. */
9411 mode = (GET_CODE (assign) == STRICT_LOW_PART
9412 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9414 /* Shift OTHER right POS places and make it the source, restricting it
9415 to the proper length and mode. */
9417 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9418 GET_MODE (src),
9419 other, pos),
9420 dest);
9421 src = force_to_mode (src, mode,
9422 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9423 ? ~(unsigned HOST_WIDE_INT) 0
9424 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9427 /* If SRC is masked by an AND that does not make a difference in
9428 the value being stored, strip it. */
9429 if (GET_CODE (assign) == ZERO_EXTRACT
9430 && CONST_INT_P (XEXP (assign, 1))
9431 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9432 && GET_CODE (src) == AND
9433 && CONST_INT_P (XEXP (src, 1))
9434 && UINTVAL (XEXP (src, 1))
9435 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9436 src = XEXP (src, 0);
9438 return gen_rtx_SET (assign, src);
9441 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9442 if so. */
9444 static rtx
9445 apply_distributive_law (rtx x)
9447 enum rtx_code code = GET_CODE (x);
9448 enum rtx_code inner_code;
9449 rtx lhs, rhs, other;
9450 rtx tem;
9452 /* Distributivity is not true for floating point as it can change the
9453 value. So we don't do it unless -funsafe-math-optimizations. */
9454 if (FLOAT_MODE_P (GET_MODE (x))
9455 && ! flag_unsafe_math_optimizations)
9456 return x;
9458 /* The outer operation can only be one of the following: */
9459 if (code != IOR && code != AND && code != XOR
9460 && code != PLUS && code != MINUS)
9461 return x;
9463 lhs = XEXP (x, 0);
9464 rhs = XEXP (x, 1);
9466 /* If either operand is a primitive we can't do anything, so get out
9467 fast. */
9468 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9469 return x;
9471 lhs = expand_compound_operation (lhs);
9472 rhs = expand_compound_operation (rhs);
9473 inner_code = GET_CODE (lhs);
9474 if (inner_code != GET_CODE (rhs))
9475 return x;
9477 /* See if the inner and outer operations distribute. */
9478 switch (inner_code)
9480 case LSHIFTRT:
9481 case ASHIFTRT:
9482 case AND:
9483 case IOR:
9484 /* These all distribute except over PLUS. */
9485 if (code == PLUS || code == MINUS)
9486 return x;
9487 break;
9489 case MULT:
9490 if (code != PLUS && code != MINUS)
9491 return x;
9492 break;
9494 case ASHIFT:
9495 /* This is also a multiply, so it distributes over everything. */
9496 break;
9498 /* This used to handle SUBREG, but this turned out to be counter-
9499 productive, since (subreg (op ...)) usually is not handled by
9500 insn patterns, and this "optimization" therefore transformed
9501 recognizable patterns into unrecognizable ones. Therefore the
9502 SUBREG case was removed from here.
9504 It is possible that distributing SUBREG over arithmetic operations
9505 leads to an intermediate result than can then be optimized further,
9506 e.g. by moving the outer SUBREG to the other side of a SET as done
9507 in simplify_set. This seems to have been the original intent of
9508 handling SUBREGs here.
9510 However, with current GCC this does not appear to actually happen,
9511 at least on major platforms. If some case is found where removing
9512 the SUBREG case here prevents follow-on optimizations, distributing
9513 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9515 default:
9516 return x;
9519 /* Set LHS and RHS to the inner operands (A and B in the example
9520 above) and set OTHER to the common operand (C in the example).
9521 There is only one way to do this unless the inner operation is
9522 commutative. */
9523 if (COMMUTATIVE_ARITH_P (lhs)
9524 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9525 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9526 else if (COMMUTATIVE_ARITH_P (lhs)
9527 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9528 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9529 else if (COMMUTATIVE_ARITH_P (lhs)
9530 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9531 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9532 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9533 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9534 else
9535 return x;
9537 /* Form the new inner operation, seeing if it simplifies first. */
9538 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9540 /* There is one exception to the general way of distributing:
9541 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9542 if (code == XOR && inner_code == IOR)
9544 inner_code = AND;
9545 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9548 /* We may be able to continuing distributing the result, so call
9549 ourselves recursively on the inner operation before forming the
9550 outer operation, which we return. */
9551 return simplify_gen_binary (inner_code, GET_MODE (x),
9552 apply_distributive_law (tem), other);
9555 /* See if X is of the form (* (+ A B) C), and if so convert to
9556 (+ (* A C) (* B C)) and try to simplify.
9558 Most of the time, this results in no change. However, if some of
9559 the operands are the same or inverses of each other, simplifications
9560 will result.
9562 For example, (and (ior A B) (not B)) can occur as the result of
9563 expanding a bit field assignment. When we apply the distributive
9564 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9565 which then simplifies to (and (A (not B))).
9567 Note that no checks happen on the validity of applying the inverse
9568 distributive law. This is pointless since we can do it in the
9569 few places where this routine is called.
9571 N is the index of the term that is decomposed (the arithmetic operation,
9572 i.e. (+ A B) in the first example above). !N is the index of the term that
9573 is distributed, i.e. of C in the first example above. */
9574 static rtx
9575 distribute_and_simplify_rtx (rtx x, int n)
9577 machine_mode mode;
9578 enum rtx_code outer_code, inner_code;
9579 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9581 /* Distributivity is not true for floating point as it can change the
9582 value. So we don't do it unless -funsafe-math-optimizations. */
9583 if (FLOAT_MODE_P (GET_MODE (x))
9584 && ! flag_unsafe_math_optimizations)
9585 return NULL_RTX;
9587 decomposed = XEXP (x, n);
9588 if (!ARITHMETIC_P (decomposed))
9589 return NULL_RTX;
9591 mode = GET_MODE (x);
9592 outer_code = GET_CODE (x);
9593 distributed = XEXP (x, !n);
9595 inner_code = GET_CODE (decomposed);
9596 inner_op0 = XEXP (decomposed, 0);
9597 inner_op1 = XEXP (decomposed, 1);
9599 /* Special case (and (xor B C) (not A)), which is equivalent to
9600 (xor (ior A B) (ior A C)) */
9601 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9603 distributed = XEXP (distributed, 0);
9604 outer_code = IOR;
9607 if (n == 0)
9609 /* Distribute the second term. */
9610 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9611 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9613 else
9615 /* Distribute the first term. */
9616 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9617 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9620 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9621 new_op0, new_op1));
9622 if (GET_CODE (tmp) != outer_code
9623 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
9624 < set_src_cost (x, mode, optimize_this_for_speed_p)))
9625 return tmp;
9627 return NULL_RTX;
9630 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9631 in MODE. Return an equivalent form, if different from (and VAROP
9632 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9634 static rtx
9635 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9636 unsigned HOST_WIDE_INT constop)
9638 unsigned HOST_WIDE_INT nonzero;
9639 unsigned HOST_WIDE_INT orig_constop;
9640 rtx orig_varop;
9641 int i;
9643 orig_varop = varop;
9644 orig_constop = constop;
9645 if (GET_CODE (varop) == CLOBBER)
9646 return NULL_RTX;
9648 /* Simplify VAROP knowing that we will be only looking at some of the
9649 bits in it.
9651 Note by passing in CONSTOP, we guarantee that the bits not set in
9652 CONSTOP are not significant and will never be examined. We must
9653 ensure that is the case by explicitly masking out those bits
9654 before returning. */
9655 varop = force_to_mode (varop, mode, constop, 0);
9657 /* If VAROP is a CLOBBER, we will fail so return it. */
9658 if (GET_CODE (varop) == CLOBBER)
9659 return varop;
9661 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9662 to VAROP and return the new constant. */
9663 if (CONST_INT_P (varop))
9664 return gen_int_mode (INTVAL (varop) & constop, mode);
9666 /* See what bits may be nonzero in VAROP. Unlike the general case of
9667 a call to nonzero_bits, here we don't care about bits outside
9668 MODE. */
9670 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9672 /* Turn off all bits in the constant that are known to already be zero.
9673 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9674 which is tested below. */
9676 constop &= nonzero;
9678 /* If we don't have any bits left, return zero. */
9679 if (constop == 0)
9680 return const0_rtx;
9682 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9683 a power of two, we can replace this with an ASHIFT. */
9684 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9685 && (i = exact_log2 (constop)) >= 0)
9686 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9688 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9689 or XOR, then try to apply the distributive law. This may eliminate
9690 operations if either branch can be simplified because of the AND.
9691 It may also make some cases more complex, but those cases probably
9692 won't match a pattern either with or without this. */
9694 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9695 return
9696 gen_lowpart
9697 (mode,
9698 apply_distributive_law
9699 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9700 simplify_and_const_int (NULL_RTX,
9701 GET_MODE (varop),
9702 XEXP (varop, 0),
9703 constop),
9704 simplify_and_const_int (NULL_RTX,
9705 GET_MODE (varop),
9706 XEXP (varop, 1),
9707 constop))));
9709 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9710 the AND and see if one of the operands simplifies to zero. If so, we
9711 may eliminate it. */
9713 if (GET_CODE (varop) == PLUS
9714 && exact_log2 (constop + 1) >= 0)
9716 rtx o0, o1;
9718 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9719 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9720 if (o0 == const0_rtx)
9721 return o1;
9722 if (o1 == const0_rtx)
9723 return o0;
9726 /* Make a SUBREG if necessary. If we can't make it, fail. */
9727 varop = gen_lowpart (mode, varop);
9728 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9729 return NULL_RTX;
9731 /* If we are only masking insignificant bits, return VAROP. */
9732 if (constop == nonzero)
9733 return varop;
9735 if (varop == orig_varop && constop == orig_constop)
9736 return NULL_RTX;
9738 /* Otherwise, return an AND. */
9739 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9743 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9744 in MODE.
9746 Return an equivalent form, if different from X. Otherwise, return X. If
9747 X is zero, we are to always construct the equivalent form. */
9749 static rtx
9750 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9751 unsigned HOST_WIDE_INT constop)
9753 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9754 if (tem)
9755 return tem;
9757 if (!x)
9758 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9759 gen_int_mode (constop, mode));
9760 if (GET_MODE (x) != mode)
9761 x = gen_lowpart (mode, x);
9762 return x;
9765 /* Given a REG, X, compute which bits in X can be nonzero.
9766 We don't care about bits outside of those defined in MODE.
9768 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9769 a shift, AND, or zero_extract, we can do better. */
9771 static rtx
9772 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9773 const_rtx known_x ATTRIBUTE_UNUSED,
9774 machine_mode known_mode ATTRIBUTE_UNUSED,
9775 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9776 unsigned HOST_WIDE_INT *nonzero)
9778 rtx tem;
9779 reg_stat_type *rsp;
9781 /* If X is a register whose nonzero bits value is current, use it.
9782 Otherwise, if X is a register whose value we can find, use that
9783 value. Otherwise, use the previously-computed global nonzero bits
9784 for this register. */
9786 rsp = &reg_stat[REGNO (x)];
9787 if (rsp->last_set_value != 0
9788 && (rsp->last_set_mode == mode
9789 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9790 && GET_MODE_CLASS (mode) == MODE_INT))
9791 && ((rsp->last_set_label >= label_tick_ebb_start
9792 && rsp->last_set_label < label_tick)
9793 || (rsp->last_set_label == label_tick
9794 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9795 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9796 && REGNO (x) < reg_n_sets_max
9797 && REG_N_SETS (REGNO (x)) == 1
9798 && !REGNO_REG_SET_P
9799 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9800 REGNO (x)))))
9802 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9804 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9805 /* We don't know anything about the upper bits. */
9806 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9808 *nonzero &= mask;
9809 return NULL;
9812 tem = get_last_value (x);
9814 if (tem)
9816 if (SHORT_IMMEDIATES_SIGN_EXTEND)
9817 tem = sign_extend_short_imm (tem, GET_MODE (x),
9818 GET_MODE_PRECISION (mode));
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 /* A subroutine of recog_for_combine. See there for arguments and
10855 return value. */
10857 static int
10858 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10860 rtx pat = *pnewpat;
10861 rtx pat_without_clobbers;
10862 int insn_code_number;
10863 int num_clobbers_to_add = 0;
10864 int i;
10865 rtx notes = NULL_RTX;
10866 rtx old_notes, old_pat;
10867 int old_icode;
10869 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10870 we use to indicate that something didn't match. If we find such a
10871 thing, force rejection. */
10872 if (GET_CODE (pat) == PARALLEL)
10873 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10874 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10875 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10876 return -1;
10878 old_pat = PATTERN (insn);
10879 old_notes = REG_NOTES (insn);
10880 PATTERN (insn) = pat;
10881 REG_NOTES (insn) = NULL_RTX;
10883 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10884 if (dump_file && (dump_flags & TDF_DETAILS))
10886 if (insn_code_number < 0)
10887 fputs ("Failed to match this instruction:\n", dump_file);
10888 else
10889 fputs ("Successfully matched this instruction:\n", dump_file);
10890 print_rtl_single (dump_file, pat);
10893 /* If it isn't, there is the possibility that we previously had an insn
10894 that clobbered some register as a side effect, but the combined
10895 insn doesn't need to do that. So try once more without the clobbers
10896 unless this represents an ASM insn. */
10898 if (insn_code_number < 0 && ! check_asm_operands (pat)
10899 && GET_CODE (pat) == PARALLEL)
10901 int pos;
10903 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10904 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10906 if (i != pos)
10907 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10908 pos++;
10911 SUBST_INT (XVECLEN (pat, 0), pos);
10913 if (pos == 1)
10914 pat = XVECEXP (pat, 0, 0);
10916 PATTERN (insn) = pat;
10917 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10918 if (dump_file && (dump_flags & TDF_DETAILS))
10920 if (insn_code_number < 0)
10921 fputs ("Failed to match this instruction:\n", dump_file);
10922 else
10923 fputs ("Successfully matched this instruction:\n", dump_file);
10924 print_rtl_single (dump_file, pat);
10928 pat_without_clobbers = pat;
10930 PATTERN (insn) = old_pat;
10931 REG_NOTES (insn) = old_notes;
10933 /* Recognize all noop sets, these will be killed by followup pass. */
10934 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10935 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10937 /* If we had any clobbers to add, make a new pattern than contains
10938 them. Then check to make sure that all of them are dead. */
10939 if (num_clobbers_to_add)
10941 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10942 rtvec_alloc (GET_CODE (pat) == PARALLEL
10943 ? (XVECLEN (pat, 0)
10944 + num_clobbers_to_add)
10945 : num_clobbers_to_add + 1));
10947 if (GET_CODE (pat) == PARALLEL)
10948 for (i = 0; i < XVECLEN (pat, 0); i++)
10949 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10950 else
10951 XVECEXP (newpat, 0, 0) = pat;
10953 add_clobbers (newpat, insn_code_number);
10955 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10956 i < XVECLEN (newpat, 0); i++)
10958 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10959 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10960 return -1;
10961 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10963 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10964 notes = alloc_reg_note (REG_UNUSED,
10965 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10968 pat = newpat;
10971 if (insn_code_number >= 0
10972 && insn_code_number != NOOP_MOVE_INSN_CODE)
10974 old_pat = PATTERN (insn);
10975 old_notes = REG_NOTES (insn);
10976 old_icode = INSN_CODE (insn);
10977 PATTERN (insn) = pat;
10978 REG_NOTES (insn) = notes;
10980 /* Allow targets to reject combined insn. */
10981 if (!targetm.legitimate_combined_insn (insn))
10983 if (dump_file && (dump_flags & TDF_DETAILS))
10984 fputs ("Instruction not appropriate for target.",
10985 dump_file);
10987 /* Callers expect recog_for_combine to strip
10988 clobbers from the pattern on failure. */
10989 pat = pat_without_clobbers;
10990 notes = NULL_RTX;
10992 insn_code_number = -1;
10995 PATTERN (insn) = old_pat;
10996 REG_NOTES (insn) = old_notes;
10997 INSN_CODE (insn) = old_icode;
11000 *pnewpat = pat;
11001 *pnotes = notes;
11003 return insn_code_number;
11006 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11007 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11008 Return whether anything was so changed. */
11010 static bool
11011 change_zero_ext (rtx *src)
11013 bool changed = false;
11015 subrtx_ptr_iterator::array_type array;
11016 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11018 rtx x = **iter;
11019 machine_mode mode = GET_MODE (x);
11020 int size;
11022 if (GET_CODE (x) == ZERO_EXTRACT
11023 && CONST_INT_P (XEXP (x, 1))
11024 && CONST_INT_P (XEXP (x, 2))
11025 && GET_MODE (XEXP (x, 0)) == mode)
11027 size = INTVAL (XEXP (x, 1));
11029 int start = INTVAL (XEXP (x, 2));
11030 if (BITS_BIG_ENDIAN)
11031 start = GET_MODE_PRECISION (mode) - size - start;
11033 x = gen_rtx_LSHIFTRT (mode, XEXP (x, 0), GEN_INT (start));
11035 else if (GET_CODE (x) == ZERO_EXTEND
11036 && GET_CODE (XEXP (x, 0)) == SUBREG
11037 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
11038 && subreg_lowpart_p (XEXP (x, 0)))
11040 size = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
11041 x = SUBREG_REG (XEXP (x, 0));
11043 else
11044 continue;
11046 unsigned HOST_WIDE_INT mask = 1;
11047 mask <<= size;
11048 mask--;
11050 x = gen_rtx_AND (mode, x, GEN_INT (mask));
11052 SUBST (**iter, x);
11053 changed = true;
11056 return changed;
11059 /* Like recog, but we receive the address of a pointer to a new pattern.
11060 We try to match the rtx that the pointer points to.
11061 If that fails, we may try to modify or replace the pattern,
11062 storing the replacement into the same pointer object.
11064 Modifications include deletion or addition of CLOBBERs. If the
11065 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11066 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11067 (and undo if that fails).
11069 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11070 the CLOBBERs are placed.
11072 The value is the final insn code from the pattern ultimately matched,
11073 or -1. */
11075 static int
11076 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11078 rtx pat = PATTERN (insn);
11079 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11080 if (insn_code_number >= 0 || check_asm_operands (pat))
11081 return insn_code_number;
11083 void *marker = get_undo_marker ();
11084 bool changed = false;
11086 if (GET_CODE (pat) == SET)
11087 changed = change_zero_ext (&SET_SRC (pat));
11088 else if (GET_CODE (pat) == PARALLEL)
11090 int i;
11091 for (i = 0; i < XVECLEN (pat, 0); i++)
11093 rtx set = XVECEXP (pat, 0, i);
11094 if (GET_CODE (set) == SET)
11095 changed |= change_zero_ext (&SET_SRC (set));
11099 if (changed)
11101 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11103 if (insn_code_number < 0)
11104 undo_to_marker (marker);
11107 return insn_code_number;
11110 /* Like gen_lowpart_general but for use by combine. In combine it
11111 is not possible to create any new pseudoregs. However, it is
11112 safe to create invalid memory addresses, because combine will
11113 try to recognize them and all they will do is make the combine
11114 attempt fail.
11116 If for some reason this cannot do its job, an rtx
11117 (clobber (const_int 0)) is returned.
11118 An insn containing that will not be recognized. */
11120 static rtx
11121 gen_lowpart_for_combine (machine_mode omode, rtx x)
11123 machine_mode imode = GET_MODE (x);
11124 unsigned int osize = GET_MODE_SIZE (omode);
11125 unsigned int isize = GET_MODE_SIZE (imode);
11126 rtx result;
11128 if (omode == imode)
11129 return x;
11131 /* We can only support MODE being wider than a word if X is a
11132 constant integer or has a mode the same size. */
11133 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11134 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11135 goto fail;
11137 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11138 won't know what to do. So we will strip off the SUBREG here and
11139 process normally. */
11140 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11142 x = SUBREG_REG (x);
11144 /* For use in case we fall down into the address adjustments
11145 further below, we need to adjust the known mode and size of
11146 x; imode and isize, since we just adjusted x. */
11147 imode = GET_MODE (x);
11149 if (imode == omode)
11150 return x;
11152 isize = GET_MODE_SIZE (imode);
11155 result = gen_lowpart_common (omode, x);
11157 if (result)
11158 return result;
11160 if (MEM_P (x))
11162 int offset = 0;
11164 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11165 address. */
11166 if (MEM_VOLATILE_P (x)
11167 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11168 goto fail;
11170 /* If we want to refer to something bigger than the original memref,
11171 generate a paradoxical subreg instead. That will force a reload
11172 of the original memref X. */
11173 if (isize < osize)
11174 return gen_rtx_SUBREG (omode, x, 0);
11176 if (WORDS_BIG_ENDIAN)
11177 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11179 /* Adjust the address so that the address-after-the-data is
11180 unchanged. */
11181 if (BYTES_BIG_ENDIAN)
11182 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11184 return adjust_address_nv (x, omode, offset);
11187 /* If X is a comparison operator, rewrite it in a new mode. This
11188 probably won't match, but may allow further simplifications. */
11189 else if (COMPARISON_P (x))
11190 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11192 /* If we couldn't simplify X any other way, just enclose it in a
11193 SUBREG. Normally, this SUBREG won't match, but some patterns may
11194 include an explicit SUBREG or we may simplify it further in combine. */
11195 else
11197 rtx res;
11199 if (imode == VOIDmode)
11201 imode = int_mode_for_mode (omode);
11202 x = gen_lowpart_common (imode, x);
11203 if (x == NULL)
11204 goto fail;
11206 res = lowpart_subreg (omode, x, imode);
11207 if (res)
11208 return res;
11211 fail:
11212 return gen_rtx_CLOBBER (omode, const0_rtx);
11215 /* Try to simplify a comparison between OP0 and a constant OP1,
11216 where CODE is the comparison code that will be tested, into a
11217 (CODE OP0 const0_rtx) form.
11219 The result is a possibly different comparison code to use.
11220 *POP1 may be updated. */
11222 static enum rtx_code
11223 simplify_compare_const (enum rtx_code code, machine_mode mode,
11224 rtx op0, rtx *pop1)
11226 unsigned int mode_width = GET_MODE_PRECISION (mode);
11227 HOST_WIDE_INT const_op = INTVAL (*pop1);
11229 /* Get the constant we are comparing against and turn off all bits
11230 not on in our mode. */
11231 if (mode != VOIDmode)
11232 const_op = trunc_int_for_mode (const_op, mode);
11234 /* If we are comparing against a constant power of two and the value
11235 being compared can only have that single bit nonzero (e.g., it was
11236 `and'ed with that bit), we can replace this with a comparison
11237 with zero. */
11238 if (const_op
11239 && (code == EQ || code == NE || code == GE || code == GEU
11240 || code == LT || code == LTU)
11241 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11242 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11243 && (nonzero_bits (op0, mode)
11244 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11246 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11247 const_op = 0;
11250 /* Similarly, if we are comparing a value known to be either -1 or
11251 0 with -1, change it to the opposite comparison against zero. */
11252 if (const_op == -1
11253 && (code == EQ || code == NE || code == GT || code == LE
11254 || code == GEU || code == LTU)
11255 && num_sign_bit_copies (op0, mode) == mode_width)
11257 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11258 const_op = 0;
11261 /* Do some canonicalizations based on the comparison code. We prefer
11262 comparisons against zero and then prefer equality comparisons.
11263 If we can reduce the size of a constant, we will do that too. */
11264 switch (code)
11266 case LT:
11267 /* < C is equivalent to <= (C - 1) */
11268 if (const_op > 0)
11270 const_op -= 1;
11271 code = LE;
11272 /* ... fall through to LE case below. */
11274 else
11275 break;
11277 case LE:
11278 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11279 if (const_op < 0)
11281 const_op += 1;
11282 code = LT;
11285 /* If we are doing a <= 0 comparison on a value known to have
11286 a zero sign bit, we can replace this with == 0. */
11287 else if (const_op == 0
11288 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11289 && (nonzero_bits (op0, mode)
11290 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11291 == 0)
11292 code = EQ;
11293 break;
11295 case GE:
11296 /* >= C is equivalent to > (C - 1). */
11297 if (const_op > 0)
11299 const_op -= 1;
11300 code = GT;
11301 /* ... fall through to GT below. */
11303 else
11304 break;
11306 case GT:
11307 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11308 if (const_op < 0)
11310 const_op += 1;
11311 code = GE;
11314 /* If we are doing a > 0 comparison on a value known to have
11315 a zero sign bit, we can replace this with != 0. */
11316 else if (const_op == 0
11317 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11318 && (nonzero_bits (op0, mode)
11319 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11320 == 0)
11321 code = NE;
11322 break;
11324 case LTU:
11325 /* < C is equivalent to <= (C - 1). */
11326 if (const_op > 0)
11328 const_op -= 1;
11329 code = LEU;
11330 /* ... fall through ... */
11332 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11333 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11334 && (unsigned HOST_WIDE_INT) const_op
11335 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11337 const_op = 0;
11338 code = GE;
11339 break;
11341 else
11342 break;
11344 case LEU:
11345 /* unsigned <= 0 is equivalent to == 0 */
11346 if (const_op == 0)
11347 code = EQ;
11348 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11349 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11350 && (unsigned HOST_WIDE_INT) const_op
11351 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11353 const_op = 0;
11354 code = GE;
11356 break;
11358 case GEU:
11359 /* >= C is equivalent to > (C - 1). */
11360 if (const_op > 1)
11362 const_op -= 1;
11363 code = GTU;
11364 /* ... fall through ... */
11367 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11368 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11369 && (unsigned HOST_WIDE_INT) const_op
11370 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11372 const_op = 0;
11373 code = LT;
11374 break;
11376 else
11377 break;
11379 case GTU:
11380 /* unsigned > 0 is equivalent to != 0 */
11381 if (const_op == 0)
11382 code = NE;
11383 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11384 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11385 && (unsigned HOST_WIDE_INT) const_op
11386 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11388 const_op = 0;
11389 code = LT;
11391 break;
11393 default:
11394 break;
11397 *pop1 = GEN_INT (const_op);
11398 return code;
11401 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11402 comparison code that will be tested.
11404 The result is a possibly different comparison code to use. *POP0 and
11405 *POP1 may be updated.
11407 It is possible that we might detect that a comparison is either always
11408 true or always false. However, we do not perform general constant
11409 folding in combine, so this knowledge isn't useful. Such tautologies
11410 should have been detected earlier. Hence we ignore all such cases. */
11412 static enum rtx_code
11413 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11415 rtx op0 = *pop0;
11416 rtx op1 = *pop1;
11417 rtx tem, tem1;
11418 int i;
11419 machine_mode mode, tmode;
11421 /* Try a few ways of applying the same transformation to both operands. */
11422 while (1)
11424 #if !WORD_REGISTER_OPERATIONS
11425 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11426 so check specially. */
11427 if (code != GTU && code != GEU && code != LTU && code != LEU
11428 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11429 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11430 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11431 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11432 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11433 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11434 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11435 && CONST_INT_P (XEXP (op0, 1))
11436 && XEXP (op0, 1) == XEXP (op1, 1)
11437 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11438 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11439 && (INTVAL (XEXP (op0, 1))
11440 == (GET_MODE_PRECISION (GET_MODE (op0))
11441 - (GET_MODE_PRECISION
11442 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11444 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11445 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11447 #endif
11449 /* If both operands are the same constant shift, see if we can ignore the
11450 shift. We can if the shift is a rotate or if the bits shifted out of
11451 this shift are known to be zero for both inputs and if the type of
11452 comparison is compatible with the shift. */
11453 if (GET_CODE (op0) == GET_CODE (op1)
11454 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11455 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11456 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11457 && (code != GT && code != LT && code != GE && code != LE))
11458 || (GET_CODE (op0) == ASHIFTRT
11459 && (code != GTU && code != LTU
11460 && code != GEU && code != LEU)))
11461 && CONST_INT_P (XEXP (op0, 1))
11462 && INTVAL (XEXP (op0, 1)) >= 0
11463 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11464 && XEXP (op0, 1) == XEXP (op1, 1))
11466 machine_mode mode = GET_MODE (op0);
11467 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11468 int shift_count = INTVAL (XEXP (op0, 1));
11470 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11471 mask &= (mask >> shift_count) << shift_count;
11472 else if (GET_CODE (op0) == ASHIFT)
11473 mask = (mask & (mask << shift_count)) >> shift_count;
11475 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11476 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11477 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11478 else
11479 break;
11482 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11483 SUBREGs are of the same mode, and, in both cases, the AND would
11484 be redundant if the comparison was done in the narrower mode,
11485 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11486 and the operand's possibly nonzero bits are 0xffffff01; in that case
11487 if we only care about QImode, we don't need the AND). This case
11488 occurs if the output mode of an scc insn is not SImode and
11489 STORE_FLAG_VALUE == 1 (e.g., the 386).
11491 Similarly, check for a case where the AND's are ZERO_EXTEND
11492 operations from some narrower mode even though a SUBREG is not
11493 present. */
11495 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11496 && CONST_INT_P (XEXP (op0, 1))
11497 && CONST_INT_P (XEXP (op1, 1)))
11499 rtx inner_op0 = XEXP (op0, 0);
11500 rtx inner_op1 = XEXP (op1, 0);
11501 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11502 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11503 int changed = 0;
11505 if (paradoxical_subreg_p (inner_op0)
11506 && GET_CODE (inner_op1) == SUBREG
11507 && (GET_MODE (SUBREG_REG (inner_op0))
11508 == GET_MODE (SUBREG_REG (inner_op1)))
11509 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11510 <= HOST_BITS_PER_WIDE_INT)
11511 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11512 GET_MODE (SUBREG_REG (inner_op0)))))
11513 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11514 GET_MODE (SUBREG_REG (inner_op1))))))
11516 op0 = SUBREG_REG (inner_op0);
11517 op1 = SUBREG_REG (inner_op1);
11519 /* The resulting comparison is always unsigned since we masked
11520 off the original sign bit. */
11521 code = unsigned_condition (code);
11523 changed = 1;
11526 else if (c0 == c1)
11527 for (tmode = GET_CLASS_NARROWEST_MODE
11528 (GET_MODE_CLASS (GET_MODE (op0)));
11529 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11530 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11532 op0 = gen_lowpart (tmode, inner_op0);
11533 op1 = gen_lowpart (tmode, inner_op1);
11534 code = unsigned_condition (code);
11535 changed = 1;
11536 break;
11539 if (! changed)
11540 break;
11543 /* If both operands are NOT, we can strip off the outer operation
11544 and adjust the comparison code for swapped operands; similarly for
11545 NEG, except that this must be an equality comparison. */
11546 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11547 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11548 && (code == EQ || code == NE)))
11549 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11551 else
11552 break;
11555 /* If the first operand is a constant, swap the operands and adjust the
11556 comparison code appropriately, but don't do this if the second operand
11557 is already a constant integer. */
11558 if (swap_commutative_operands_p (op0, op1))
11560 std::swap (op0, op1);
11561 code = swap_condition (code);
11564 /* We now enter a loop during which we will try to simplify the comparison.
11565 For the most part, we only are concerned with comparisons with zero,
11566 but some things may really be comparisons with zero but not start
11567 out looking that way. */
11569 while (CONST_INT_P (op1))
11571 machine_mode mode = GET_MODE (op0);
11572 unsigned int mode_width = GET_MODE_PRECISION (mode);
11573 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11574 int equality_comparison_p;
11575 int sign_bit_comparison_p;
11576 int unsigned_comparison_p;
11577 HOST_WIDE_INT const_op;
11579 /* We only want to handle integral modes. This catches VOIDmode,
11580 CCmode, and the floating-point modes. An exception is that we
11581 can handle VOIDmode if OP0 is a COMPARE or a comparison
11582 operation. */
11584 if (GET_MODE_CLASS (mode) != MODE_INT
11585 && ! (mode == VOIDmode
11586 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11587 break;
11589 /* Try to simplify the compare to constant, possibly changing the
11590 comparison op, and/or changing op1 to zero. */
11591 code = simplify_compare_const (code, mode, op0, &op1);
11592 const_op = INTVAL (op1);
11594 /* Compute some predicates to simplify code below. */
11596 equality_comparison_p = (code == EQ || code == NE);
11597 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11598 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11599 || code == GEU);
11601 /* If this is a sign bit comparison and we can do arithmetic in
11602 MODE, say that we will only be needing the sign bit of OP0. */
11603 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11604 op0 = force_to_mode (op0, mode,
11605 (unsigned HOST_WIDE_INT) 1
11606 << (GET_MODE_PRECISION (mode) - 1),
11609 /* Now try cases based on the opcode of OP0. If none of the cases
11610 does a "continue", we exit this loop immediately after the
11611 switch. */
11613 switch (GET_CODE (op0))
11615 case ZERO_EXTRACT:
11616 /* If we are extracting a single bit from a variable position in
11617 a constant that has only a single bit set and are comparing it
11618 with zero, we can convert this into an equality comparison
11619 between the position and the location of the single bit. */
11620 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11621 have already reduced the shift count modulo the word size. */
11622 if (!SHIFT_COUNT_TRUNCATED
11623 && CONST_INT_P (XEXP (op0, 0))
11624 && XEXP (op0, 1) == const1_rtx
11625 && equality_comparison_p && const_op == 0
11626 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11628 if (BITS_BIG_ENDIAN)
11629 i = BITS_PER_WORD - 1 - i;
11631 op0 = XEXP (op0, 2);
11632 op1 = GEN_INT (i);
11633 const_op = i;
11635 /* Result is nonzero iff shift count is equal to I. */
11636 code = reverse_condition (code);
11637 continue;
11640 /* ... fall through ... */
11642 case SIGN_EXTRACT:
11643 tem = expand_compound_operation (op0);
11644 if (tem != op0)
11646 op0 = tem;
11647 continue;
11649 break;
11651 case NOT:
11652 /* If testing for equality, we can take the NOT of the constant. */
11653 if (equality_comparison_p
11654 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11656 op0 = XEXP (op0, 0);
11657 op1 = tem;
11658 continue;
11661 /* If just looking at the sign bit, reverse the sense of the
11662 comparison. */
11663 if (sign_bit_comparison_p)
11665 op0 = XEXP (op0, 0);
11666 code = (code == GE ? LT : GE);
11667 continue;
11669 break;
11671 case NEG:
11672 /* If testing for equality, we can take the NEG of the constant. */
11673 if (equality_comparison_p
11674 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11676 op0 = XEXP (op0, 0);
11677 op1 = tem;
11678 continue;
11681 /* The remaining cases only apply to comparisons with zero. */
11682 if (const_op != 0)
11683 break;
11685 /* When X is ABS or is known positive,
11686 (neg X) is < 0 if and only if X != 0. */
11688 if (sign_bit_comparison_p
11689 && (GET_CODE (XEXP (op0, 0)) == ABS
11690 || (mode_width <= HOST_BITS_PER_WIDE_INT
11691 && (nonzero_bits (XEXP (op0, 0), mode)
11692 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11693 == 0)))
11695 op0 = XEXP (op0, 0);
11696 code = (code == LT ? NE : EQ);
11697 continue;
11700 /* If we have NEG of something whose two high-order bits are the
11701 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11702 if (num_sign_bit_copies (op0, mode) >= 2)
11704 op0 = XEXP (op0, 0);
11705 code = swap_condition (code);
11706 continue;
11708 break;
11710 case ROTATE:
11711 /* If we are testing equality and our count is a constant, we
11712 can perform the inverse operation on our RHS. */
11713 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11714 && (tem = simplify_binary_operation (ROTATERT, mode,
11715 op1, XEXP (op0, 1))) != 0)
11717 op0 = XEXP (op0, 0);
11718 op1 = tem;
11719 continue;
11722 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11723 a particular bit. Convert it to an AND of a constant of that
11724 bit. This will be converted into a ZERO_EXTRACT. */
11725 if (const_op == 0 && sign_bit_comparison_p
11726 && CONST_INT_P (XEXP (op0, 1))
11727 && mode_width <= HOST_BITS_PER_WIDE_INT)
11729 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11730 ((unsigned HOST_WIDE_INT) 1
11731 << (mode_width - 1
11732 - INTVAL (XEXP (op0, 1)))));
11733 code = (code == LT ? NE : EQ);
11734 continue;
11737 /* Fall through. */
11739 case ABS:
11740 /* ABS is ignorable inside an equality comparison with zero. */
11741 if (const_op == 0 && equality_comparison_p)
11743 op0 = XEXP (op0, 0);
11744 continue;
11746 break;
11748 case SIGN_EXTEND:
11749 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11750 (compare FOO CONST) if CONST fits in FOO's mode and we
11751 are either testing inequality or have an unsigned
11752 comparison with ZERO_EXTEND or a signed comparison with
11753 SIGN_EXTEND. But don't do it if we don't have a compare
11754 insn of the given mode, since we'd have to revert it
11755 later on, and then we wouldn't know whether to sign- or
11756 zero-extend. */
11757 mode = GET_MODE (XEXP (op0, 0));
11758 if (GET_MODE_CLASS (mode) == MODE_INT
11759 && ! unsigned_comparison_p
11760 && HWI_COMPUTABLE_MODE_P (mode)
11761 && trunc_int_for_mode (const_op, mode) == const_op
11762 && have_insn_for (COMPARE, mode))
11764 op0 = XEXP (op0, 0);
11765 continue;
11767 break;
11769 case SUBREG:
11770 /* Check for the case where we are comparing A - C1 with C2, that is
11772 (subreg:MODE (plus (A) (-C1))) op (C2)
11774 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11775 comparison in the wider mode. One of the following two conditions
11776 must be true in order for this to be valid:
11778 1. The mode extension results in the same bit pattern being added
11779 on both sides and the comparison is equality or unsigned. As
11780 C2 has been truncated to fit in MODE, the pattern can only be
11781 all 0s or all 1s.
11783 2. The mode extension results in the sign bit being copied on
11784 each side.
11786 The difficulty here is that we have predicates for A but not for
11787 (A - C1) so we need to check that C1 is within proper bounds so
11788 as to perturbate A as little as possible. */
11790 if (mode_width <= HOST_BITS_PER_WIDE_INT
11791 && subreg_lowpart_p (op0)
11792 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11793 && GET_CODE (SUBREG_REG (op0)) == PLUS
11794 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11796 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11797 rtx a = XEXP (SUBREG_REG (op0), 0);
11798 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11800 if ((c1 > 0
11801 && (unsigned HOST_WIDE_INT) c1
11802 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11803 && (equality_comparison_p || unsigned_comparison_p)
11804 /* (A - C1) zero-extends if it is positive and sign-extends
11805 if it is negative, C2 both zero- and sign-extends. */
11806 && ((0 == (nonzero_bits (a, inner_mode)
11807 & ~GET_MODE_MASK (mode))
11808 && const_op >= 0)
11809 /* (A - C1) sign-extends if it is positive and 1-extends
11810 if it is negative, C2 both sign- and 1-extends. */
11811 || (num_sign_bit_copies (a, inner_mode)
11812 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11813 - mode_width)
11814 && const_op < 0)))
11815 || ((unsigned HOST_WIDE_INT) c1
11816 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11817 /* (A - C1) always sign-extends, like C2. */
11818 && num_sign_bit_copies (a, inner_mode)
11819 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11820 - (mode_width - 1))))
11822 op0 = SUBREG_REG (op0);
11823 continue;
11827 /* If the inner mode is narrower and we are extracting the low part,
11828 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11829 if (subreg_lowpart_p (op0)
11830 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11831 /* Fall through */ ;
11832 else
11833 break;
11835 /* ... fall through ... */
11837 case ZERO_EXTEND:
11838 mode = GET_MODE (XEXP (op0, 0));
11839 if (GET_MODE_CLASS (mode) == MODE_INT
11840 && (unsigned_comparison_p || equality_comparison_p)
11841 && HWI_COMPUTABLE_MODE_P (mode)
11842 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11843 && const_op >= 0
11844 && have_insn_for (COMPARE, mode))
11846 op0 = XEXP (op0, 0);
11847 continue;
11849 break;
11851 case PLUS:
11852 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11853 this for equality comparisons due to pathological cases involving
11854 overflows. */
11855 if (equality_comparison_p
11856 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11857 op1, XEXP (op0, 1))))
11859 op0 = XEXP (op0, 0);
11860 op1 = tem;
11861 continue;
11864 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11865 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11866 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11868 op0 = XEXP (XEXP (op0, 0), 0);
11869 code = (code == LT ? EQ : NE);
11870 continue;
11872 break;
11874 case MINUS:
11875 /* We used to optimize signed comparisons against zero, but that
11876 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11877 arrive here as equality comparisons, or (GEU, LTU) are
11878 optimized away. No need to special-case them. */
11880 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11881 (eq B (minus A C)), whichever simplifies. We can only do
11882 this for equality comparisons due to pathological cases involving
11883 overflows. */
11884 if (equality_comparison_p
11885 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11886 XEXP (op0, 1), op1)))
11888 op0 = XEXP (op0, 0);
11889 op1 = tem;
11890 continue;
11893 if (equality_comparison_p
11894 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11895 XEXP (op0, 0), op1)))
11897 op0 = XEXP (op0, 1);
11898 op1 = tem;
11899 continue;
11902 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11903 of bits in X minus 1, is one iff X > 0. */
11904 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11905 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11906 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11907 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11909 op0 = XEXP (op0, 1);
11910 code = (code == GE ? LE : GT);
11911 continue;
11913 break;
11915 case XOR:
11916 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11917 if C is zero or B is a constant. */
11918 if (equality_comparison_p
11919 && 0 != (tem = simplify_binary_operation (XOR, mode,
11920 XEXP (op0, 1), op1)))
11922 op0 = XEXP (op0, 0);
11923 op1 = tem;
11924 continue;
11926 break;
11928 case EQ: case NE:
11929 case UNEQ: case LTGT:
11930 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11931 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11932 case UNORDERED: case ORDERED:
11933 /* We can't do anything if OP0 is a condition code value, rather
11934 than an actual data value. */
11935 if (const_op != 0
11936 || CC0_P (XEXP (op0, 0))
11937 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11938 break;
11940 /* Get the two operands being compared. */
11941 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11942 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11943 else
11944 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11946 /* Check for the cases where we simply want the result of the
11947 earlier test or the opposite of that result. */
11948 if (code == NE || code == EQ
11949 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11950 && (code == LT || code == GE)))
11952 enum rtx_code new_code;
11953 if (code == LT || code == NE)
11954 new_code = GET_CODE (op0);
11955 else
11956 new_code = reversed_comparison_code (op0, NULL);
11958 if (new_code != UNKNOWN)
11960 code = new_code;
11961 op0 = tem;
11962 op1 = tem1;
11963 continue;
11966 break;
11968 case IOR:
11969 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11970 iff X <= 0. */
11971 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11972 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11973 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11975 op0 = XEXP (op0, 1);
11976 code = (code == GE ? GT : LE);
11977 continue;
11979 break;
11981 case AND:
11982 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11983 will be converted to a ZERO_EXTRACT later. */
11984 if (const_op == 0 && equality_comparison_p
11985 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11986 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11988 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11989 XEXP (XEXP (op0, 0), 1));
11990 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11991 continue;
11994 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11995 zero and X is a comparison and C1 and C2 describe only bits set
11996 in STORE_FLAG_VALUE, we can compare with X. */
11997 if (const_op == 0 && equality_comparison_p
11998 && mode_width <= HOST_BITS_PER_WIDE_INT
11999 && CONST_INT_P (XEXP (op0, 1))
12000 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12001 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12002 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12003 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12005 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12006 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12007 if ((~STORE_FLAG_VALUE & mask) == 0
12008 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12009 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12010 && COMPARISON_P (tem))))
12012 op0 = XEXP (XEXP (op0, 0), 0);
12013 continue;
12017 /* If we are doing an equality comparison of an AND of a bit equal
12018 to the sign bit, replace this with a LT or GE comparison of
12019 the underlying value. */
12020 if (equality_comparison_p
12021 && const_op == 0
12022 && CONST_INT_P (XEXP (op0, 1))
12023 && mode_width <= HOST_BITS_PER_WIDE_INT
12024 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12025 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
12027 op0 = XEXP (op0, 0);
12028 code = (code == EQ ? GE : LT);
12029 continue;
12032 /* If this AND operation is really a ZERO_EXTEND from a narrower
12033 mode, the constant fits within that mode, and this is either an
12034 equality or unsigned comparison, try to do this comparison in
12035 the narrower mode.
12037 Note that in:
12039 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12040 -> (ne:DI (reg:SI 4) (const_int 0))
12042 unless TRULY_NOOP_TRUNCATION allows it or the register is
12043 known to hold a value of the required mode the
12044 transformation is invalid. */
12045 if ((equality_comparison_p || unsigned_comparison_p)
12046 && CONST_INT_P (XEXP (op0, 1))
12047 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12048 & GET_MODE_MASK (mode))
12049 + 1)) >= 0
12050 && const_op >> i == 0
12051 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
12052 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
12053 || (REG_P (XEXP (op0, 0))
12054 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
12056 op0 = gen_lowpart (tmode, XEXP (op0, 0));
12057 continue;
12060 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
12061 fits in both M1 and M2 and the SUBREG is either paradoxical
12062 or represents the low part, permute the SUBREG and the AND
12063 and try again. */
12064 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
12066 unsigned HOST_WIDE_INT c1;
12067 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
12068 /* Require an integral mode, to avoid creating something like
12069 (AND:SF ...). */
12070 if (SCALAR_INT_MODE_P (tmode)
12071 /* It is unsafe to commute the AND into the SUBREG if the
12072 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12073 not defined. As originally written the upper bits
12074 have a defined value due to the AND operation.
12075 However, if we commute the AND inside the SUBREG then
12076 they no longer have defined values and the meaning of
12077 the code has been changed. */
12078 && ((WORD_REGISTER_OPERATIONS
12079 && mode_width > GET_MODE_PRECISION (tmode)
12080 && mode_width <= BITS_PER_WORD)
12081 || (mode_width <= GET_MODE_PRECISION (tmode)
12082 && subreg_lowpart_p (XEXP (op0, 0))))
12083 && CONST_INT_P (XEXP (op0, 1))
12084 && mode_width <= HOST_BITS_PER_WIDE_INT
12085 && HWI_COMPUTABLE_MODE_P (tmode)
12086 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
12087 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12088 && c1 != mask
12089 && c1 != GET_MODE_MASK (tmode))
12091 op0 = simplify_gen_binary (AND, tmode,
12092 SUBREG_REG (XEXP (op0, 0)),
12093 gen_int_mode (c1, tmode));
12094 op0 = gen_lowpart (mode, op0);
12095 continue;
12099 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12100 if (const_op == 0 && equality_comparison_p
12101 && XEXP (op0, 1) == const1_rtx
12102 && GET_CODE (XEXP (op0, 0)) == NOT)
12104 op0 = simplify_and_const_int (NULL_RTX, mode,
12105 XEXP (XEXP (op0, 0), 0), 1);
12106 code = (code == NE ? EQ : NE);
12107 continue;
12110 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12111 (eq (and (lshiftrt X) 1) 0).
12112 Also handle the case where (not X) is expressed using xor. */
12113 if (const_op == 0 && equality_comparison_p
12114 && XEXP (op0, 1) == const1_rtx
12115 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12117 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12118 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12120 if (GET_CODE (shift_op) == NOT
12121 || (GET_CODE (shift_op) == XOR
12122 && CONST_INT_P (XEXP (shift_op, 1))
12123 && CONST_INT_P (shift_count)
12124 && HWI_COMPUTABLE_MODE_P (mode)
12125 && (UINTVAL (XEXP (shift_op, 1))
12126 == (unsigned HOST_WIDE_INT) 1
12127 << INTVAL (shift_count))))
12130 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12131 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12132 code = (code == NE ? EQ : NE);
12133 continue;
12136 break;
12138 case ASHIFT:
12139 /* If we have (compare (ashift FOO N) (const_int C)) and
12140 the high order N bits of FOO (N+1 if an inequality comparison)
12141 are known to be zero, we can do this by comparing FOO with C
12142 shifted right N bits so long as the low-order N bits of C are
12143 zero. */
12144 if (CONST_INT_P (XEXP (op0, 1))
12145 && INTVAL (XEXP (op0, 1)) >= 0
12146 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12147 < HOST_BITS_PER_WIDE_INT)
12148 && (((unsigned HOST_WIDE_INT) const_op
12149 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
12150 - 1)) == 0)
12151 && mode_width <= HOST_BITS_PER_WIDE_INT
12152 && (nonzero_bits (XEXP (op0, 0), mode)
12153 & ~(mask >> (INTVAL (XEXP (op0, 1))
12154 + ! equality_comparison_p))) == 0)
12156 /* We must perform a logical shift, not an arithmetic one,
12157 as we want the top N bits of C to be zero. */
12158 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12160 temp >>= INTVAL (XEXP (op0, 1));
12161 op1 = gen_int_mode (temp, mode);
12162 op0 = XEXP (op0, 0);
12163 continue;
12166 /* If we are doing a sign bit comparison, it means we are testing
12167 a particular bit. Convert it to the appropriate AND. */
12168 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12169 && mode_width <= HOST_BITS_PER_WIDE_INT)
12171 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12172 ((unsigned HOST_WIDE_INT) 1
12173 << (mode_width - 1
12174 - INTVAL (XEXP (op0, 1)))));
12175 code = (code == LT ? NE : EQ);
12176 continue;
12179 /* If this an equality comparison with zero and we are shifting
12180 the low bit to the sign bit, we can convert this to an AND of the
12181 low-order bit. */
12182 if (const_op == 0 && equality_comparison_p
12183 && CONST_INT_P (XEXP (op0, 1))
12184 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12186 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12187 continue;
12189 break;
12191 case ASHIFTRT:
12192 /* If this is an equality comparison with zero, we can do this
12193 as a logical shift, which might be much simpler. */
12194 if (equality_comparison_p && const_op == 0
12195 && CONST_INT_P (XEXP (op0, 1)))
12197 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12198 XEXP (op0, 0),
12199 INTVAL (XEXP (op0, 1)));
12200 continue;
12203 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12204 do the comparison in a narrower mode. */
12205 if (! unsigned_comparison_p
12206 && CONST_INT_P (XEXP (op0, 1))
12207 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12208 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12209 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12210 MODE_INT, 1)) != BLKmode
12211 && (((unsigned HOST_WIDE_INT) const_op
12212 + (GET_MODE_MASK (tmode) >> 1) + 1)
12213 <= GET_MODE_MASK (tmode)))
12215 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12216 continue;
12219 /* Likewise if OP0 is a PLUS of a sign extension with a
12220 constant, which is usually represented with the PLUS
12221 between the shifts. */
12222 if (! unsigned_comparison_p
12223 && CONST_INT_P (XEXP (op0, 1))
12224 && GET_CODE (XEXP (op0, 0)) == PLUS
12225 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12226 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12227 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12228 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12229 MODE_INT, 1)) != BLKmode
12230 && (((unsigned HOST_WIDE_INT) const_op
12231 + (GET_MODE_MASK (tmode) >> 1) + 1)
12232 <= GET_MODE_MASK (tmode)))
12234 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12235 rtx add_const = XEXP (XEXP (op0, 0), 1);
12236 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12237 add_const, XEXP (op0, 1));
12239 op0 = simplify_gen_binary (PLUS, tmode,
12240 gen_lowpart (tmode, inner),
12241 new_const);
12242 continue;
12245 /* ... fall through ... */
12246 case LSHIFTRT:
12247 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12248 the low order N bits of FOO are known to be zero, we can do this
12249 by comparing FOO with C shifted left N bits so long as no
12250 overflow occurs. Even if the low order N bits of FOO aren't known
12251 to be zero, if the comparison is >= or < we can use the same
12252 optimization and for > or <= by setting all the low
12253 order N bits in the comparison constant. */
12254 if (CONST_INT_P (XEXP (op0, 1))
12255 && INTVAL (XEXP (op0, 1)) > 0
12256 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12257 && mode_width <= HOST_BITS_PER_WIDE_INT
12258 && (((unsigned HOST_WIDE_INT) const_op
12259 + (GET_CODE (op0) != LSHIFTRT
12260 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12261 + 1)
12262 : 0))
12263 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12265 unsigned HOST_WIDE_INT low_bits
12266 = (nonzero_bits (XEXP (op0, 0), mode)
12267 & (((unsigned HOST_WIDE_INT) 1
12268 << INTVAL (XEXP (op0, 1))) - 1));
12269 if (low_bits == 0 || !equality_comparison_p)
12271 /* If the shift was logical, then we must make the condition
12272 unsigned. */
12273 if (GET_CODE (op0) == LSHIFTRT)
12274 code = unsigned_condition (code);
12276 const_op <<= INTVAL (XEXP (op0, 1));
12277 if (low_bits != 0
12278 && (code == GT || code == GTU
12279 || code == LE || code == LEU))
12280 const_op
12281 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12282 op1 = GEN_INT (const_op);
12283 op0 = XEXP (op0, 0);
12284 continue;
12288 /* If we are using this shift to extract just the sign bit, we
12289 can replace this with an LT or GE comparison. */
12290 if (const_op == 0
12291 && (equality_comparison_p || sign_bit_comparison_p)
12292 && CONST_INT_P (XEXP (op0, 1))
12293 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12295 op0 = XEXP (op0, 0);
12296 code = (code == NE || code == GT ? LT : GE);
12297 continue;
12299 break;
12301 default:
12302 break;
12305 break;
12308 /* Now make any compound operations involved in this comparison. Then,
12309 check for an outmost SUBREG on OP0 that is not doing anything or is
12310 paradoxical. The latter transformation must only be performed when
12311 it is known that the "extra" bits will be the same in op0 and op1 or
12312 that they don't matter. There are three cases to consider:
12314 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12315 care bits and we can assume they have any convenient value. So
12316 making the transformation is safe.
12318 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12319 In this case the upper bits of op0 are undefined. We should not make
12320 the simplification in that case as we do not know the contents of
12321 those bits.
12323 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12324 UNKNOWN. In that case we know those bits are zeros or ones. We must
12325 also be sure that they are the same as the upper bits of op1.
12327 We can never remove a SUBREG for a non-equality comparison because
12328 the sign bit is in a different place in the underlying object. */
12330 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12331 op1 = make_compound_operation (op1, SET);
12333 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12334 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12335 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12336 && (code == NE || code == EQ))
12338 if (paradoxical_subreg_p (op0))
12340 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12341 implemented. */
12342 if (REG_P (SUBREG_REG (op0)))
12344 op0 = SUBREG_REG (op0);
12345 op1 = gen_lowpart (GET_MODE (op0), op1);
12348 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12349 <= HOST_BITS_PER_WIDE_INT)
12350 && (nonzero_bits (SUBREG_REG (op0),
12351 GET_MODE (SUBREG_REG (op0)))
12352 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12354 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12356 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12357 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12358 op0 = SUBREG_REG (op0), op1 = tem;
12362 /* We now do the opposite procedure: Some machines don't have compare
12363 insns in all modes. If OP0's mode is an integer mode smaller than a
12364 word and we can't do a compare in that mode, see if there is a larger
12365 mode for which we can do the compare. There are a number of cases in
12366 which we can use the wider mode. */
12368 mode = GET_MODE (op0);
12369 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12370 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12371 && ! have_insn_for (COMPARE, mode))
12372 for (tmode = GET_MODE_WIDER_MODE (mode);
12373 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12374 tmode = GET_MODE_WIDER_MODE (tmode))
12375 if (have_insn_for (COMPARE, tmode))
12377 int zero_extended;
12379 /* If this is a test for negative, we can make an explicit
12380 test of the sign bit. Test this first so we can use
12381 a paradoxical subreg to extend OP0. */
12383 if (op1 == const0_rtx && (code == LT || code == GE)
12384 && HWI_COMPUTABLE_MODE_P (mode))
12386 unsigned HOST_WIDE_INT sign
12387 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12388 op0 = simplify_gen_binary (AND, tmode,
12389 gen_lowpart (tmode, op0),
12390 gen_int_mode (sign, tmode));
12391 code = (code == LT) ? NE : EQ;
12392 break;
12395 /* If the only nonzero bits in OP0 and OP1 are those in the
12396 narrower mode and this is an equality or unsigned comparison,
12397 we can use the wider mode. Similarly for sign-extended
12398 values, in which case it is true for all comparisons. */
12399 zero_extended = ((code == EQ || code == NE
12400 || code == GEU || code == GTU
12401 || code == LEU || code == LTU)
12402 && (nonzero_bits (op0, tmode)
12403 & ~GET_MODE_MASK (mode)) == 0
12404 && ((CONST_INT_P (op1)
12405 || (nonzero_bits (op1, tmode)
12406 & ~GET_MODE_MASK (mode)) == 0)));
12408 if (zero_extended
12409 || ((num_sign_bit_copies (op0, tmode)
12410 > (unsigned int) (GET_MODE_PRECISION (tmode)
12411 - GET_MODE_PRECISION (mode)))
12412 && (num_sign_bit_copies (op1, tmode)
12413 > (unsigned int) (GET_MODE_PRECISION (tmode)
12414 - GET_MODE_PRECISION (mode)))))
12416 /* If OP0 is an AND and we don't have an AND in MODE either,
12417 make a new AND in the proper mode. */
12418 if (GET_CODE (op0) == AND
12419 && !have_insn_for (AND, mode))
12420 op0 = simplify_gen_binary (AND, tmode,
12421 gen_lowpart (tmode,
12422 XEXP (op0, 0)),
12423 gen_lowpart (tmode,
12424 XEXP (op0, 1)));
12425 else
12427 if (zero_extended)
12429 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12430 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12432 else
12434 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12435 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12437 break;
12442 /* We may have changed the comparison operands. Re-canonicalize. */
12443 if (swap_commutative_operands_p (op0, op1))
12445 std::swap (op0, op1);
12446 code = swap_condition (code);
12449 /* If this machine only supports a subset of valid comparisons, see if we
12450 can convert an unsupported one into a supported one. */
12451 target_canonicalize_comparison (&code, &op0, &op1, 0);
12453 *pop0 = op0;
12454 *pop1 = op1;
12456 return code;
12459 /* Utility function for record_value_for_reg. Count number of
12460 rtxs in X. */
12461 static int
12462 count_rtxs (rtx x)
12464 enum rtx_code code = GET_CODE (x);
12465 const char *fmt;
12466 int i, j, ret = 1;
12468 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12469 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12471 rtx x0 = XEXP (x, 0);
12472 rtx x1 = XEXP (x, 1);
12474 if (x0 == x1)
12475 return 1 + 2 * count_rtxs (x0);
12477 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12478 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12479 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12480 return 2 + 2 * count_rtxs (x0)
12481 + count_rtxs (x == XEXP (x1, 0)
12482 ? XEXP (x1, 1) : XEXP (x1, 0));
12484 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12485 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12486 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12487 return 2 + 2 * count_rtxs (x1)
12488 + count_rtxs (x == XEXP (x0, 0)
12489 ? XEXP (x0, 1) : XEXP (x0, 0));
12492 fmt = GET_RTX_FORMAT (code);
12493 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12494 if (fmt[i] == 'e')
12495 ret += count_rtxs (XEXP (x, i));
12496 else if (fmt[i] == 'E')
12497 for (j = 0; j < XVECLEN (x, i); j++)
12498 ret += count_rtxs (XVECEXP (x, i, j));
12500 return ret;
12503 /* Utility function for following routine. Called when X is part of a value
12504 being stored into last_set_value. Sets last_set_table_tick
12505 for each register mentioned. Similar to mention_regs in cse.c */
12507 static void
12508 update_table_tick (rtx x)
12510 enum rtx_code code = GET_CODE (x);
12511 const char *fmt = GET_RTX_FORMAT (code);
12512 int i, j;
12514 if (code == REG)
12516 unsigned int regno = REGNO (x);
12517 unsigned int endregno = END_REGNO (x);
12518 unsigned int r;
12520 for (r = regno; r < endregno; r++)
12522 reg_stat_type *rsp = &reg_stat[r];
12523 rsp->last_set_table_tick = label_tick;
12526 return;
12529 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12530 if (fmt[i] == 'e')
12532 /* Check for identical subexpressions. If x contains
12533 identical subexpression we only have to traverse one of
12534 them. */
12535 if (i == 0 && ARITHMETIC_P (x))
12537 /* Note that at this point x1 has already been
12538 processed. */
12539 rtx x0 = XEXP (x, 0);
12540 rtx x1 = XEXP (x, 1);
12542 /* If x0 and x1 are identical then there is no need to
12543 process x0. */
12544 if (x0 == x1)
12545 break;
12547 /* If x0 is identical to a subexpression of x1 then while
12548 processing x1, x0 has already been processed. Thus we
12549 are done with x. */
12550 if (ARITHMETIC_P (x1)
12551 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12552 break;
12554 /* If x1 is identical to a subexpression of x0 then we
12555 still have to process the rest of x0. */
12556 if (ARITHMETIC_P (x0)
12557 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12559 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12560 break;
12564 update_table_tick (XEXP (x, i));
12566 else if (fmt[i] == 'E')
12567 for (j = 0; j < XVECLEN (x, i); j++)
12568 update_table_tick (XVECEXP (x, i, j));
12571 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12572 are saying that the register is clobbered and we no longer know its
12573 value. If INSN is zero, don't update reg_stat[].last_set; this is
12574 only permitted with VALUE also zero and is used to invalidate the
12575 register. */
12577 static void
12578 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12580 unsigned int regno = REGNO (reg);
12581 unsigned int endregno = END_REGNO (reg);
12582 unsigned int i;
12583 reg_stat_type *rsp;
12585 /* If VALUE contains REG and we have a previous value for REG, substitute
12586 the previous value. */
12587 if (value && insn && reg_overlap_mentioned_p (reg, value))
12589 rtx tem;
12591 /* Set things up so get_last_value is allowed to see anything set up to
12592 our insn. */
12593 subst_low_luid = DF_INSN_LUID (insn);
12594 tem = get_last_value (reg);
12596 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12597 it isn't going to be useful and will take a lot of time to process,
12598 so just use the CLOBBER. */
12600 if (tem)
12602 if (ARITHMETIC_P (tem)
12603 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12604 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12605 tem = XEXP (tem, 0);
12606 else if (count_occurrences (value, reg, 1) >= 2)
12608 /* If there are two or more occurrences of REG in VALUE,
12609 prevent the value from growing too much. */
12610 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12611 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12614 value = replace_rtx (copy_rtx (value), reg, tem);
12618 /* For each register modified, show we don't know its value, that
12619 we don't know about its bitwise content, that its value has been
12620 updated, and that we don't know the location of the death of the
12621 register. */
12622 for (i = regno; i < endregno; i++)
12624 rsp = &reg_stat[i];
12626 if (insn)
12627 rsp->last_set = insn;
12629 rsp->last_set_value = 0;
12630 rsp->last_set_mode = VOIDmode;
12631 rsp->last_set_nonzero_bits = 0;
12632 rsp->last_set_sign_bit_copies = 0;
12633 rsp->last_death = 0;
12634 rsp->truncated_to_mode = VOIDmode;
12637 /* Mark registers that are being referenced in this value. */
12638 if (value)
12639 update_table_tick (value);
12641 /* Now update the status of each register being set.
12642 If someone is using this register in this block, set this register
12643 to invalid since we will get confused between the two lives in this
12644 basic block. This makes using this register always invalid. In cse, we
12645 scan the table to invalidate all entries using this register, but this
12646 is too much work for us. */
12648 for (i = regno; i < endregno; i++)
12650 rsp = &reg_stat[i];
12651 rsp->last_set_label = label_tick;
12652 if (!insn
12653 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12654 rsp->last_set_invalid = 1;
12655 else
12656 rsp->last_set_invalid = 0;
12659 /* The value being assigned might refer to X (like in "x++;"). In that
12660 case, we must replace it with (clobber (const_int 0)) to prevent
12661 infinite loops. */
12662 rsp = &reg_stat[regno];
12663 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12665 value = copy_rtx (value);
12666 if (!get_last_value_validate (&value, insn, label_tick, 1))
12667 value = 0;
12670 /* For the main register being modified, update the value, the mode, the
12671 nonzero bits, and the number of sign bit copies. */
12673 rsp->last_set_value = value;
12675 if (value)
12677 machine_mode mode = GET_MODE (reg);
12678 subst_low_luid = DF_INSN_LUID (insn);
12679 rsp->last_set_mode = mode;
12680 if (GET_MODE_CLASS (mode) == MODE_INT
12681 && HWI_COMPUTABLE_MODE_P (mode))
12682 mode = nonzero_bits_mode;
12683 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12684 rsp->last_set_sign_bit_copies
12685 = num_sign_bit_copies (value, GET_MODE (reg));
12689 /* Called via note_stores from record_dead_and_set_regs to handle one
12690 SET or CLOBBER in an insn. DATA is the instruction in which the
12691 set is occurring. */
12693 static void
12694 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12696 rtx_insn *record_dead_insn = (rtx_insn *) data;
12698 if (GET_CODE (dest) == SUBREG)
12699 dest = SUBREG_REG (dest);
12701 if (!record_dead_insn)
12703 if (REG_P (dest))
12704 record_value_for_reg (dest, NULL, NULL_RTX);
12705 return;
12708 if (REG_P (dest))
12710 /* If we are setting the whole register, we know its value. Otherwise
12711 show that we don't know the value. We can handle SUBREG in
12712 some cases. */
12713 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12714 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12715 else if (GET_CODE (setter) == SET
12716 && GET_CODE (SET_DEST (setter)) == SUBREG
12717 && SUBREG_REG (SET_DEST (setter)) == dest
12718 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12719 && subreg_lowpart_p (SET_DEST (setter)))
12720 record_value_for_reg (dest, record_dead_insn,
12721 gen_lowpart (GET_MODE (dest),
12722 SET_SRC (setter)));
12723 else
12724 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12726 else if (MEM_P (dest)
12727 /* Ignore pushes, they clobber nothing. */
12728 && ! push_operand (dest, GET_MODE (dest)))
12729 mem_last_set = DF_INSN_LUID (record_dead_insn);
12732 /* Update the records of when each REG was most recently set or killed
12733 for the things done by INSN. This is the last thing done in processing
12734 INSN in the combiner loop.
12736 We update reg_stat[], in particular fields last_set, last_set_value,
12737 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12738 last_death, and also the similar information mem_last_set (which insn
12739 most recently modified memory) and last_call_luid (which insn was the
12740 most recent subroutine call). */
12742 static void
12743 record_dead_and_set_regs (rtx_insn *insn)
12745 rtx link;
12746 unsigned int i;
12748 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12750 if (REG_NOTE_KIND (link) == REG_DEAD
12751 && REG_P (XEXP (link, 0)))
12753 unsigned int regno = REGNO (XEXP (link, 0));
12754 unsigned int endregno = END_REGNO (XEXP (link, 0));
12756 for (i = regno; i < endregno; i++)
12758 reg_stat_type *rsp;
12760 rsp = &reg_stat[i];
12761 rsp->last_death = insn;
12764 else if (REG_NOTE_KIND (link) == REG_INC)
12765 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12768 if (CALL_P (insn))
12770 hard_reg_set_iterator hrsi;
12771 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12773 reg_stat_type *rsp;
12775 rsp = &reg_stat[i];
12776 rsp->last_set_invalid = 1;
12777 rsp->last_set = insn;
12778 rsp->last_set_value = 0;
12779 rsp->last_set_mode = VOIDmode;
12780 rsp->last_set_nonzero_bits = 0;
12781 rsp->last_set_sign_bit_copies = 0;
12782 rsp->last_death = 0;
12783 rsp->truncated_to_mode = VOIDmode;
12786 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12788 /* We can't combine into a call pattern. Remember, though, that
12789 the return value register is set at this LUID. We could
12790 still replace a register with the return value from the
12791 wrong subroutine call! */
12792 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12794 else
12795 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12798 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12799 register present in the SUBREG, so for each such SUBREG go back and
12800 adjust nonzero and sign bit information of the registers that are
12801 known to have some zero/sign bits set.
12803 This is needed because when combine blows the SUBREGs away, the
12804 information on zero/sign bits is lost and further combines can be
12805 missed because of that. */
12807 static void
12808 record_promoted_value (rtx_insn *insn, rtx subreg)
12810 struct insn_link *links;
12811 rtx set;
12812 unsigned int regno = REGNO (SUBREG_REG (subreg));
12813 machine_mode mode = GET_MODE (subreg);
12815 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12816 return;
12818 for (links = LOG_LINKS (insn); links;)
12820 reg_stat_type *rsp;
12822 insn = links->insn;
12823 set = single_set (insn);
12825 if (! set || !REG_P (SET_DEST (set))
12826 || REGNO (SET_DEST (set)) != regno
12827 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12829 links = links->next;
12830 continue;
12833 rsp = &reg_stat[regno];
12834 if (rsp->last_set == insn)
12836 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12837 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12840 if (REG_P (SET_SRC (set)))
12842 regno = REGNO (SET_SRC (set));
12843 links = LOG_LINKS (insn);
12845 else
12846 break;
12850 /* Check if X, a register, is known to contain a value already
12851 truncated to MODE. In this case we can use a subreg to refer to
12852 the truncated value even though in the generic case we would need
12853 an explicit truncation. */
12855 static bool
12856 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12858 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12859 machine_mode truncated = rsp->truncated_to_mode;
12861 if (truncated == 0
12862 || rsp->truncation_label < label_tick_ebb_start)
12863 return false;
12864 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12865 return true;
12866 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12867 return true;
12868 return false;
12871 /* If X is a hard reg or a subreg record the mode that the register is
12872 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12873 to turn a truncate into a subreg using this information. Return true
12874 if traversing X is complete. */
12876 static bool
12877 record_truncated_value (rtx x)
12879 machine_mode truncated_mode;
12880 reg_stat_type *rsp;
12882 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12884 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12885 truncated_mode = GET_MODE (x);
12887 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12888 return true;
12890 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12891 return true;
12893 x = SUBREG_REG (x);
12895 /* ??? For hard-regs we now record everything. We might be able to
12896 optimize this using last_set_mode. */
12897 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12898 truncated_mode = GET_MODE (x);
12899 else
12900 return false;
12902 rsp = &reg_stat[REGNO (x)];
12903 if (rsp->truncated_to_mode == 0
12904 || rsp->truncation_label < label_tick_ebb_start
12905 || (GET_MODE_SIZE (truncated_mode)
12906 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12908 rsp->truncated_to_mode = truncated_mode;
12909 rsp->truncation_label = label_tick;
12912 return true;
12915 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12916 the modes they are used in. This can help truning TRUNCATEs into
12917 SUBREGs. */
12919 static void
12920 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12922 subrtx_var_iterator::array_type array;
12923 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12924 if (record_truncated_value (*iter))
12925 iter.skip_subrtxes ();
12928 /* Scan X for promoted SUBREGs. For each one found,
12929 note what it implies to the registers used in it. */
12931 static void
12932 check_promoted_subreg (rtx_insn *insn, rtx x)
12934 if (GET_CODE (x) == SUBREG
12935 && SUBREG_PROMOTED_VAR_P (x)
12936 && REG_P (SUBREG_REG (x)))
12937 record_promoted_value (insn, x);
12938 else
12940 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12941 int i, j;
12943 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12944 switch (format[i])
12946 case 'e':
12947 check_promoted_subreg (insn, XEXP (x, i));
12948 break;
12949 case 'V':
12950 case 'E':
12951 if (XVEC (x, i) != 0)
12952 for (j = 0; j < XVECLEN (x, i); j++)
12953 check_promoted_subreg (insn, XVECEXP (x, i, j));
12954 break;
12959 /* Verify that all the registers and memory references mentioned in *LOC are
12960 still valid. *LOC was part of a value set in INSN when label_tick was
12961 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12962 the invalid references with (clobber (const_int 0)) and return 1. This
12963 replacement is useful because we often can get useful information about
12964 the form of a value (e.g., if it was produced by a shift that always
12965 produces -1 or 0) even though we don't know exactly what registers it
12966 was produced from. */
12968 static int
12969 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12971 rtx x = *loc;
12972 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12973 int len = GET_RTX_LENGTH (GET_CODE (x));
12974 int i, j;
12976 if (REG_P (x))
12978 unsigned int regno = REGNO (x);
12979 unsigned int endregno = END_REGNO (x);
12980 unsigned int j;
12982 for (j = regno; j < endregno; j++)
12984 reg_stat_type *rsp = &reg_stat[j];
12985 if (rsp->last_set_invalid
12986 /* If this is a pseudo-register that was only set once and not
12987 live at the beginning of the function, it is always valid. */
12988 || (! (regno >= FIRST_PSEUDO_REGISTER
12989 && regno < reg_n_sets_max
12990 && REG_N_SETS (regno) == 1
12991 && (!REGNO_REG_SET_P
12992 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12993 regno)))
12994 && rsp->last_set_label > tick))
12996 if (replace)
12997 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12998 return replace;
13002 return 1;
13004 /* If this is a memory reference, make sure that there were no stores after
13005 it that might have clobbered the value. We don't have alias info, so we
13006 assume any store invalidates it. Moreover, we only have local UIDs, so
13007 we also assume that there were stores in the intervening basic blocks. */
13008 else if (MEM_P (x) && !MEM_READONLY_P (x)
13009 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13011 if (replace)
13012 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13013 return replace;
13016 for (i = 0; i < len; i++)
13018 if (fmt[i] == 'e')
13020 /* Check for identical subexpressions. If x contains
13021 identical subexpression we only have to traverse one of
13022 them. */
13023 if (i == 1 && ARITHMETIC_P (x))
13025 /* Note that at this point x0 has already been checked
13026 and found valid. */
13027 rtx x0 = XEXP (x, 0);
13028 rtx x1 = XEXP (x, 1);
13030 /* If x0 and x1 are identical then x is also valid. */
13031 if (x0 == x1)
13032 return 1;
13034 /* If x1 is identical to a subexpression of x0 then
13035 while checking x0, x1 has already been checked. Thus
13036 it is valid and so as x. */
13037 if (ARITHMETIC_P (x0)
13038 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13039 return 1;
13041 /* If x0 is identical to a subexpression of x1 then x is
13042 valid iff the rest of x1 is valid. */
13043 if (ARITHMETIC_P (x1)
13044 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13045 return
13046 get_last_value_validate (&XEXP (x1,
13047 x0 == XEXP (x1, 0) ? 1 : 0),
13048 insn, tick, replace);
13051 if (get_last_value_validate (&XEXP (x, i), insn, tick,
13052 replace) == 0)
13053 return 0;
13055 else if (fmt[i] == 'E')
13056 for (j = 0; j < XVECLEN (x, i); j++)
13057 if (get_last_value_validate (&XVECEXP (x, i, j),
13058 insn, tick, replace) == 0)
13059 return 0;
13062 /* If we haven't found a reason for it to be invalid, it is valid. */
13063 return 1;
13066 /* Get the last value assigned to X, if known. Some registers
13067 in the value may be replaced with (clobber (const_int 0)) if their value
13068 is known longer known reliably. */
13070 static rtx
13071 get_last_value (const_rtx x)
13073 unsigned int regno;
13074 rtx value;
13075 reg_stat_type *rsp;
13077 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13078 then convert it to the desired mode. If this is a paradoxical SUBREG,
13079 we cannot predict what values the "extra" bits might have. */
13080 if (GET_CODE (x) == SUBREG
13081 && subreg_lowpart_p (x)
13082 && !paradoxical_subreg_p (x)
13083 && (value = get_last_value (SUBREG_REG (x))) != 0)
13084 return gen_lowpart (GET_MODE (x), value);
13086 if (!REG_P (x))
13087 return 0;
13089 regno = REGNO (x);
13090 rsp = &reg_stat[regno];
13091 value = rsp->last_set_value;
13093 /* If we don't have a value, or if it isn't for this basic block and
13094 it's either a hard register, set more than once, or it's a live
13095 at the beginning of the function, return 0.
13097 Because if it's not live at the beginning of the function then the reg
13098 is always set before being used (is never used without being set).
13099 And, if it's set only once, and it's always set before use, then all
13100 uses must have the same last value, even if it's not from this basic
13101 block. */
13103 if (value == 0
13104 || (rsp->last_set_label < label_tick_ebb_start
13105 && (regno < FIRST_PSEUDO_REGISTER
13106 || regno >= reg_n_sets_max
13107 || REG_N_SETS (regno) != 1
13108 || REGNO_REG_SET_P
13109 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13110 return 0;
13112 /* If the value was set in a later insn than the ones we are processing,
13113 we can't use it even if the register was only set once. */
13114 if (rsp->last_set_label == label_tick
13115 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13116 return 0;
13118 /* If the value has all its registers valid, return it. */
13119 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13120 return value;
13122 /* Otherwise, make a copy and replace any invalid register with
13123 (clobber (const_int 0)). If that fails for some reason, return 0. */
13125 value = copy_rtx (value);
13126 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13127 return value;
13129 return 0;
13132 /* Return nonzero if expression X refers to a REG or to memory
13133 that is set in an instruction more recent than FROM_LUID. */
13135 static int
13136 use_crosses_set_p (const_rtx x, int from_luid)
13138 const char *fmt;
13139 int i;
13140 enum rtx_code code = GET_CODE (x);
13142 if (code == REG)
13144 unsigned int regno = REGNO (x);
13145 unsigned endreg = END_REGNO (x);
13147 #ifdef PUSH_ROUNDING
13148 /* Don't allow uses of the stack pointer to be moved,
13149 because we don't know whether the move crosses a push insn. */
13150 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13151 return 1;
13152 #endif
13153 for (; regno < endreg; regno++)
13155 reg_stat_type *rsp = &reg_stat[regno];
13156 if (rsp->last_set
13157 && rsp->last_set_label == label_tick
13158 && DF_INSN_LUID (rsp->last_set) > from_luid)
13159 return 1;
13161 return 0;
13164 if (code == MEM && mem_last_set > from_luid)
13165 return 1;
13167 fmt = GET_RTX_FORMAT (code);
13169 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13171 if (fmt[i] == 'E')
13173 int j;
13174 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13175 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13176 return 1;
13178 else if (fmt[i] == 'e'
13179 && use_crosses_set_p (XEXP (x, i), from_luid))
13180 return 1;
13182 return 0;
13185 /* Define three variables used for communication between the following
13186 routines. */
13188 static unsigned int reg_dead_regno, reg_dead_endregno;
13189 static int reg_dead_flag;
13191 /* Function called via note_stores from reg_dead_at_p.
13193 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13194 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13196 static void
13197 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13199 unsigned int regno, endregno;
13201 if (!REG_P (dest))
13202 return;
13204 regno = REGNO (dest);
13205 endregno = END_REGNO (dest);
13206 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13207 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13210 /* Return nonzero if REG is known to be dead at INSN.
13212 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13213 referencing REG, it is dead. If we hit a SET referencing REG, it is
13214 live. Otherwise, see if it is live or dead at the start of the basic
13215 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13216 must be assumed to be always live. */
13218 static int
13219 reg_dead_at_p (rtx reg, rtx_insn *insn)
13221 basic_block block;
13222 unsigned int i;
13224 /* Set variables for reg_dead_at_p_1. */
13225 reg_dead_regno = REGNO (reg);
13226 reg_dead_endregno = END_REGNO (reg);
13228 reg_dead_flag = 0;
13230 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13231 we allow the machine description to decide whether use-and-clobber
13232 patterns are OK. */
13233 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13235 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13236 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13237 return 0;
13240 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13241 beginning of basic block. */
13242 block = BLOCK_FOR_INSN (insn);
13243 for (;;)
13245 if (INSN_P (insn))
13247 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13248 return 1;
13250 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13251 if (reg_dead_flag)
13252 return reg_dead_flag == 1 ? 1 : 0;
13254 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13255 return 1;
13258 if (insn == BB_HEAD (block))
13259 break;
13261 insn = PREV_INSN (insn);
13264 /* Look at live-in sets for the basic block that we were in. */
13265 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13266 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13267 return 0;
13269 return 1;
13272 /* Note hard registers in X that are used. */
13274 static void
13275 mark_used_regs_combine (rtx x)
13277 RTX_CODE code = GET_CODE (x);
13278 unsigned int regno;
13279 int i;
13281 switch (code)
13283 case LABEL_REF:
13284 case SYMBOL_REF:
13285 case CONST:
13286 CASE_CONST_ANY:
13287 case PC:
13288 case ADDR_VEC:
13289 case ADDR_DIFF_VEC:
13290 case ASM_INPUT:
13291 /* CC0 must die in the insn after it is set, so we don't need to take
13292 special note of it here. */
13293 case CC0:
13294 return;
13296 case CLOBBER:
13297 /* If we are clobbering a MEM, mark any hard registers inside the
13298 address as used. */
13299 if (MEM_P (XEXP (x, 0)))
13300 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13301 return;
13303 case REG:
13304 regno = REGNO (x);
13305 /* A hard reg in a wide mode may really be multiple registers.
13306 If so, mark all of them just like the first. */
13307 if (regno < FIRST_PSEUDO_REGISTER)
13309 /* None of this applies to the stack, frame or arg pointers. */
13310 if (regno == STACK_POINTER_REGNUM
13311 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13312 && regno == HARD_FRAME_POINTER_REGNUM)
13313 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13314 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13315 || regno == FRAME_POINTER_REGNUM)
13316 return;
13318 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13320 return;
13322 case SET:
13324 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13325 the address. */
13326 rtx testreg = SET_DEST (x);
13328 while (GET_CODE (testreg) == SUBREG
13329 || GET_CODE (testreg) == ZERO_EXTRACT
13330 || GET_CODE (testreg) == STRICT_LOW_PART)
13331 testreg = XEXP (testreg, 0);
13333 if (MEM_P (testreg))
13334 mark_used_regs_combine (XEXP (testreg, 0));
13336 mark_used_regs_combine (SET_SRC (x));
13338 return;
13340 default:
13341 break;
13344 /* Recursively scan the operands of this expression. */
13347 const char *fmt = GET_RTX_FORMAT (code);
13349 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13351 if (fmt[i] == 'e')
13352 mark_used_regs_combine (XEXP (x, i));
13353 else if (fmt[i] == 'E')
13355 int j;
13357 for (j = 0; j < XVECLEN (x, i); j++)
13358 mark_used_regs_combine (XVECEXP (x, i, j));
13364 /* Remove register number REGNO from the dead registers list of INSN.
13366 Return the note used to record the death, if there was one. */
13369 remove_death (unsigned int regno, rtx_insn *insn)
13371 rtx note = find_regno_note (insn, REG_DEAD, regno);
13373 if (note)
13374 remove_note (insn, note);
13376 return note;
13379 /* For each register (hardware or pseudo) used within expression X, if its
13380 death is in an instruction with luid between FROM_LUID (inclusive) and
13381 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13382 list headed by PNOTES.
13384 That said, don't move registers killed by maybe_kill_insn.
13386 This is done when X is being merged by combination into TO_INSN. These
13387 notes will then be distributed as needed. */
13389 static void
13390 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13391 rtx *pnotes)
13393 const char *fmt;
13394 int len, i;
13395 enum rtx_code code = GET_CODE (x);
13397 if (code == REG)
13399 unsigned int regno = REGNO (x);
13400 rtx_insn *where_dead = reg_stat[regno].last_death;
13402 /* Don't move the register if it gets killed in between from and to. */
13403 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13404 && ! reg_referenced_p (x, maybe_kill_insn))
13405 return;
13407 if (where_dead
13408 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13409 && DF_INSN_LUID (where_dead) >= from_luid
13410 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13412 rtx note = remove_death (regno, where_dead);
13414 /* It is possible for the call above to return 0. This can occur
13415 when last_death points to I2 or I1 that we combined with.
13416 In that case make a new note.
13418 We must also check for the case where X is a hard register
13419 and NOTE is a death note for a range of hard registers
13420 including X. In that case, we must put REG_DEAD notes for
13421 the remaining registers in place of NOTE. */
13423 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13424 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13425 > GET_MODE_SIZE (GET_MODE (x))))
13427 unsigned int deadregno = REGNO (XEXP (note, 0));
13428 unsigned int deadend = END_REGNO (XEXP (note, 0));
13429 unsigned int ourend = END_REGNO (x);
13430 unsigned int i;
13432 for (i = deadregno; i < deadend; i++)
13433 if (i < regno || i >= ourend)
13434 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13437 /* If we didn't find any note, or if we found a REG_DEAD note that
13438 covers only part of the given reg, and we have a multi-reg hard
13439 register, then to be safe we must check for REG_DEAD notes
13440 for each register other than the first. They could have
13441 their own REG_DEAD notes lying around. */
13442 else if ((note == 0
13443 || (note != 0
13444 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13445 < GET_MODE_SIZE (GET_MODE (x)))))
13446 && regno < FIRST_PSEUDO_REGISTER
13447 && REG_NREGS (x) > 1)
13449 unsigned int ourend = END_REGNO (x);
13450 unsigned int i, offset;
13451 rtx oldnotes = 0;
13453 if (note)
13454 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13455 else
13456 offset = 1;
13458 for (i = regno + offset; i < ourend; i++)
13459 move_deaths (regno_reg_rtx[i],
13460 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13463 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13465 XEXP (note, 1) = *pnotes;
13466 *pnotes = note;
13468 else
13469 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13472 return;
13475 else if (GET_CODE (x) == SET)
13477 rtx dest = SET_DEST (x);
13479 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13481 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13482 that accesses one word of a multi-word item, some
13483 piece of everything register in the expression is used by
13484 this insn, so remove any old death. */
13485 /* ??? So why do we test for equality of the sizes? */
13487 if (GET_CODE (dest) == ZERO_EXTRACT
13488 || GET_CODE (dest) == STRICT_LOW_PART
13489 || (GET_CODE (dest) == SUBREG
13490 && (((GET_MODE_SIZE (GET_MODE (dest))
13491 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13492 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13493 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13495 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13496 return;
13499 /* If this is some other SUBREG, we know it replaces the entire
13500 value, so use that as the destination. */
13501 if (GET_CODE (dest) == SUBREG)
13502 dest = SUBREG_REG (dest);
13504 /* If this is a MEM, adjust deaths of anything used in the address.
13505 For a REG (the only other possibility), the entire value is
13506 being replaced so the old value is not used in this insn. */
13508 if (MEM_P (dest))
13509 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13510 to_insn, pnotes);
13511 return;
13514 else if (GET_CODE (x) == CLOBBER)
13515 return;
13517 len = GET_RTX_LENGTH (code);
13518 fmt = GET_RTX_FORMAT (code);
13520 for (i = 0; i < len; i++)
13522 if (fmt[i] == 'E')
13524 int j;
13525 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13526 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13527 to_insn, pnotes);
13529 else if (fmt[i] == 'e')
13530 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13534 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13535 pattern of an insn. X must be a REG. */
13537 static int
13538 reg_bitfield_target_p (rtx x, rtx body)
13540 int i;
13542 if (GET_CODE (body) == SET)
13544 rtx dest = SET_DEST (body);
13545 rtx target;
13546 unsigned int regno, tregno, endregno, endtregno;
13548 if (GET_CODE (dest) == ZERO_EXTRACT)
13549 target = XEXP (dest, 0);
13550 else if (GET_CODE (dest) == STRICT_LOW_PART)
13551 target = SUBREG_REG (XEXP (dest, 0));
13552 else
13553 return 0;
13555 if (GET_CODE (target) == SUBREG)
13556 target = SUBREG_REG (target);
13558 if (!REG_P (target))
13559 return 0;
13561 tregno = REGNO (target), regno = REGNO (x);
13562 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13563 return target == x;
13565 endtregno = end_hard_regno (GET_MODE (target), tregno);
13566 endregno = end_hard_regno (GET_MODE (x), regno);
13568 return endregno > tregno && regno < endtregno;
13571 else if (GET_CODE (body) == PARALLEL)
13572 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13573 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13574 return 1;
13576 return 0;
13579 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13580 as appropriate. I3 and I2 are the insns resulting from the combination
13581 insns including FROM (I2 may be zero).
13583 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13584 not need REG_DEAD notes because they are being substituted for. This
13585 saves searching in the most common cases.
13587 Each note in the list is either ignored or placed on some insns, depending
13588 on the type of note. */
13590 static void
13591 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13592 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13594 rtx note, next_note;
13595 rtx tem_note;
13596 rtx_insn *tem_insn;
13598 for (note = notes; note; note = next_note)
13600 rtx_insn *place = 0, *place2 = 0;
13602 next_note = XEXP (note, 1);
13603 switch (REG_NOTE_KIND (note))
13605 case REG_BR_PROB:
13606 case REG_BR_PRED:
13607 /* Doesn't matter much where we put this, as long as it's somewhere.
13608 It is preferable to keep these notes on branches, which is most
13609 likely to be i3. */
13610 place = i3;
13611 break;
13613 case REG_NON_LOCAL_GOTO:
13614 if (JUMP_P (i3))
13615 place = i3;
13616 else
13618 gcc_assert (i2 && JUMP_P (i2));
13619 place = i2;
13621 break;
13623 case REG_EH_REGION:
13624 /* These notes must remain with the call or trapping instruction. */
13625 if (CALL_P (i3))
13626 place = i3;
13627 else if (i2 && CALL_P (i2))
13628 place = i2;
13629 else
13631 gcc_assert (cfun->can_throw_non_call_exceptions);
13632 if (may_trap_p (i3))
13633 place = i3;
13634 else if (i2 && may_trap_p (i2))
13635 place = i2;
13636 /* ??? Otherwise assume we've combined things such that we
13637 can now prove that the instructions can't trap. Drop the
13638 note in this case. */
13640 break;
13642 case REG_ARGS_SIZE:
13643 /* ??? How to distribute between i3-i1. Assume i3 contains the
13644 entire adjustment. Assert i3 contains at least some adjust. */
13645 if (!noop_move_p (i3))
13647 int old_size, args_size = INTVAL (XEXP (note, 0));
13648 /* fixup_args_size_notes looks at REG_NORETURN note,
13649 so ensure the note is placed there first. */
13650 if (CALL_P (i3))
13652 rtx *np;
13653 for (np = &next_note; *np; np = &XEXP (*np, 1))
13654 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13656 rtx n = *np;
13657 *np = XEXP (n, 1);
13658 XEXP (n, 1) = REG_NOTES (i3);
13659 REG_NOTES (i3) = n;
13660 break;
13663 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13664 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13665 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13666 gcc_assert (old_size != args_size
13667 || (CALL_P (i3)
13668 && !ACCUMULATE_OUTGOING_ARGS
13669 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13671 break;
13673 case REG_NORETURN:
13674 case REG_SETJMP:
13675 case REG_TM:
13676 case REG_CALL_DECL:
13677 /* These notes must remain with the call. It should not be
13678 possible for both I2 and I3 to be a call. */
13679 if (CALL_P (i3))
13680 place = i3;
13681 else
13683 gcc_assert (i2 && CALL_P (i2));
13684 place = i2;
13686 break;
13688 case REG_UNUSED:
13689 /* Any clobbers for i3 may still exist, and so we must process
13690 REG_UNUSED notes from that insn.
13692 Any clobbers from i2 or i1 can only exist if they were added by
13693 recog_for_combine. In that case, recog_for_combine created the
13694 necessary REG_UNUSED notes. Trying to keep any original
13695 REG_UNUSED notes from these insns can cause incorrect output
13696 if it is for the same register as the original i3 dest.
13697 In that case, we will notice that the register is set in i3,
13698 and then add a REG_UNUSED note for the destination of i3, which
13699 is wrong. However, it is possible to have REG_UNUSED notes from
13700 i2 or i1 for register which were both used and clobbered, so
13701 we keep notes from i2 or i1 if they will turn into REG_DEAD
13702 notes. */
13704 /* If this register is set or clobbered in I3, put the note there
13705 unless there is one already. */
13706 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13708 if (from_insn != i3)
13709 break;
13711 if (! (REG_P (XEXP (note, 0))
13712 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13713 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13714 place = i3;
13716 /* Otherwise, if this register is used by I3, then this register
13717 now dies here, so we must put a REG_DEAD note here unless there
13718 is one already. */
13719 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13720 && ! (REG_P (XEXP (note, 0))
13721 ? find_regno_note (i3, REG_DEAD,
13722 REGNO (XEXP (note, 0)))
13723 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13725 PUT_REG_NOTE_KIND (note, REG_DEAD);
13726 place = i3;
13728 break;
13730 case REG_EQUAL:
13731 case REG_EQUIV:
13732 case REG_NOALIAS:
13733 /* These notes say something about results of an insn. We can
13734 only support them if they used to be on I3 in which case they
13735 remain on I3. Otherwise they are ignored.
13737 If the note refers to an expression that is not a constant, we
13738 must also ignore the note since we cannot tell whether the
13739 equivalence is still true. It might be possible to do
13740 slightly better than this (we only have a problem if I2DEST
13741 or I1DEST is present in the expression), but it doesn't
13742 seem worth the trouble. */
13744 if (from_insn == i3
13745 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13746 place = i3;
13747 break;
13749 case REG_INC:
13750 /* These notes say something about how a register is used. They must
13751 be present on any use of the register in I2 or I3. */
13752 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13753 place = i3;
13755 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13757 if (place)
13758 place2 = i2;
13759 else
13760 place = i2;
13762 break;
13764 case REG_LABEL_TARGET:
13765 case REG_LABEL_OPERAND:
13766 /* This can show up in several ways -- either directly in the
13767 pattern, or hidden off in the constant pool with (or without?)
13768 a REG_EQUAL note. */
13769 /* ??? Ignore the without-reg_equal-note problem for now. */
13770 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13771 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13772 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13773 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13774 place = i3;
13776 if (i2
13777 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13778 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13779 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13780 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13782 if (place)
13783 place2 = i2;
13784 else
13785 place = i2;
13788 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13789 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13790 there. */
13791 if (place && JUMP_P (place)
13792 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13793 && (JUMP_LABEL (place) == NULL
13794 || JUMP_LABEL (place) == XEXP (note, 0)))
13796 rtx label = JUMP_LABEL (place);
13798 if (!label)
13799 JUMP_LABEL (place) = XEXP (note, 0);
13800 else if (LABEL_P (label))
13801 LABEL_NUSES (label)--;
13804 if (place2 && JUMP_P (place2)
13805 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13806 && (JUMP_LABEL (place2) == NULL
13807 || JUMP_LABEL (place2) == XEXP (note, 0)))
13809 rtx label = JUMP_LABEL (place2);
13811 if (!label)
13812 JUMP_LABEL (place2) = XEXP (note, 0);
13813 else if (LABEL_P (label))
13814 LABEL_NUSES (label)--;
13815 place2 = 0;
13817 break;
13819 case REG_NONNEG:
13820 /* This note says something about the value of a register prior
13821 to the execution of an insn. It is too much trouble to see
13822 if the note is still correct in all situations. It is better
13823 to simply delete it. */
13824 break;
13826 case REG_DEAD:
13827 /* If we replaced the right hand side of FROM_INSN with a
13828 REG_EQUAL note, the original use of the dying register
13829 will not have been combined into I3 and I2. In such cases,
13830 FROM_INSN is guaranteed to be the first of the combined
13831 instructions, so we simply need to search back before
13832 FROM_INSN for the previous use or set of this register,
13833 then alter the notes there appropriately.
13835 If the register is used as an input in I3, it dies there.
13836 Similarly for I2, if it is nonzero and adjacent to I3.
13838 If the register is not used as an input in either I3 or I2
13839 and it is not one of the registers we were supposed to eliminate,
13840 there are two possibilities. We might have a non-adjacent I2
13841 or we might have somehow eliminated an additional register
13842 from a computation. For example, we might have had A & B where
13843 we discover that B will always be zero. In this case we will
13844 eliminate the reference to A.
13846 In both cases, we must search to see if we can find a previous
13847 use of A and put the death note there. */
13849 if (from_insn
13850 && from_insn == i2mod
13851 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13852 tem_insn = from_insn;
13853 else
13855 if (from_insn
13856 && CALL_P (from_insn)
13857 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13858 place = from_insn;
13859 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13860 place = i3;
13861 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13862 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13863 place = i2;
13864 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13865 && !(i2mod
13866 && reg_overlap_mentioned_p (XEXP (note, 0),
13867 i2mod_old_rhs)))
13868 || rtx_equal_p (XEXP (note, 0), elim_i1)
13869 || rtx_equal_p (XEXP (note, 0), elim_i0))
13870 break;
13871 tem_insn = i3;
13872 /* If the new I2 sets the same register that is marked dead
13873 in the note, the note now should not be put on I2, as the
13874 note refers to a previous incarnation of the reg. */
13875 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
13876 tem_insn = i2;
13879 if (place == 0)
13881 basic_block bb = this_basic_block;
13883 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13885 if (!NONDEBUG_INSN_P (tem_insn))
13887 if (tem_insn == BB_HEAD (bb))
13888 break;
13889 continue;
13892 /* If the register is being set at TEM_INSN, see if that is all
13893 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13894 into a REG_UNUSED note instead. Don't delete sets to
13895 global register vars. */
13896 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13897 || !global_regs[REGNO (XEXP (note, 0))])
13898 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13900 rtx set = single_set (tem_insn);
13901 rtx inner_dest = 0;
13902 rtx_insn *cc0_setter = NULL;
13904 if (set != 0)
13905 for (inner_dest = SET_DEST (set);
13906 (GET_CODE (inner_dest) == STRICT_LOW_PART
13907 || GET_CODE (inner_dest) == SUBREG
13908 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13909 inner_dest = XEXP (inner_dest, 0))
13912 /* Verify that it was the set, and not a clobber that
13913 modified the register.
13915 CC0 targets must be careful to maintain setter/user
13916 pairs. If we cannot delete the setter due to side
13917 effects, mark the user with an UNUSED note instead
13918 of deleting it. */
13920 if (set != 0 && ! side_effects_p (SET_SRC (set))
13921 && rtx_equal_p (XEXP (note, 0), inner_dest)
13922 && (!HAVE_cc0
13923 || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13924 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13925 && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
13927 /* Move the notes and links of TEM_INSN elsewhere.
13928 This might delete other dead insns recursively.
13929 First set the pattern to something that won't use
13930 any register. */
13931 rtx old_notes = REG_NOTES (tem_insn);
13933 PATTERN (tem_insn) = pc_rtx;
13934 REG_NOTES (tem_insn) = NULL;
13936 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13937 NULL_RTX, NULL_RTX, NULL_RTX);
13938 distribute_links (LOG_LINKS (tem_insn));
13940 SET_INSN_DELETED (tem_insn);
13941 if (tem_insn == i2)
13942 i2 = NULL;
13944 /* Delete the setter too. */
13945 if (cc0_setter)
13947 PATTERN (cc0_setter) = pc_rtx;
13948 old_notes = REG_NOTES (cc0_setter);
13949 REG_NOTES (cc0_setter) = NULL;
13951 distribute_notes (old_notes, cc0_setter,
13952 cc0_setter, NULL,
13953 NULL_RTX, NULL_RTX, NULL_RTX);
13954 distribute_links (LOG_LINKS (cc0_setter));
13956 SET_INSN_DELETED (cc0_setter);
13957 if (cc0_setter == i2)
13958 i2 = NULL;
13961 else
13963 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13965 /* If there isn't already a REG_UNUSED note, put one
13966 here. Do not place a REG_DEAD note, even if
13967 the register is also used here; that would not
13968 match the algorithm used in lifetime analysis
13969 and can cause the consistency check in the
13970 scheduler to fail. */
13971 if (! find_regno_note (tem_insn, REG_UNUSED,
13972 REGNO (XEXP (note, 0))))
13973 place = tem_insn;
13974 break;
13977 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13978 || (CALL_P (tem_insn)
13979 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13981 place = tem_insn;
13983 /* If we are doing a 3->2 combination, and we have a
13984 register which formerly died in i3 and was not used
13985 by i2, which now no longer dies in i3 and is used in
13986 i2 but does not die in i2, and place is between i2
13987 and i3, then we may need to move a link from place to
13988 i2. */
13989 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13990 && from_insn
13991 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13992 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13994 struct insn_link *links = LOG_LINKS (place);
13995 LOG_LINKS (place) = NULL;
13996 distribute_links (links);
13998 break;
14001 if (tem_insn == BB_HEAD (bb))
14002 break;
14007 /* If the register is set or already dead at PLACE, we needn't do
14008 anything with this note if it is still a REG_DEAD note.
14009 We check here if it is set at all, not if is it totally replaced,
14010 which is what `dead_or_set_p' checks, so also check for it being
14011 set partially. */
14013 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14015 unsigned int regno = REGNO (XEXP (note, 0));
14016 reg_stat_type *rsp = &reg_stat[regno];
14018 if (dead_or_set_p (place, XEXP (note, 0))
14019 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14021 /* Unless the register previously died in PLACE, clear
14022 last_death. [I no longer understand why this is
14023 being done.] */
14024 if (rsp->last_death != place)
14025 rsp->last_death = 0;
14026 place = 0;
14028 else
14029 rsp->last_death = place;
14031 /* If this is a death note for a hard reg that is occupying
14032 multiple registers, ensure that we are still using all
14033 parts of the object. If we find a piece of the object
14034 that is unused, we must arrange for an appropriate REG_DEAD
14035 note to be added for it. However, we can't just emit a USE
14036 and tag the note to it, since the register might actually
14037 be dead; so we recourse, and the recursive call then finds
14038 the previous insn that used this register. */
14040 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14042 unsigned int endregno = END_REGNO (XEXP (note, 0));
14043 bool all_used = true;
14044 unsigned int i;
14046 for (i = regno; i < endregno; i++)
14047 if ((! refers_to_regno_p (i, PATTERN (place))
14048 && ! find_regno_fusage (place, USE, i))
14049 || dead_or_set_regno_p (place, i))
14051 all_used = false;
14052 break;
14055 if (! all_used)
14057 /* Put only REG_DEAD notes for pieces that are
14058 not already dead or set. */
14060 for (i = regno; i < endregno;
14061 i += hard_regno_nregs[i][reg_raw_mode[i]])
14063 rtx piece = regno_reg_rtx[i];
14064 basic_block bb = this_basic_block;
14066 if (! dead_or_set_p (place, piece)
14067 && ! reg_bitfield_target_p (piece,
14068 PATTERN (place)))
14070 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14071 NULL_RTX);
14073 distribute_notes (new_note, place, place,
14074 NULL, NULL_RTX, NULL_RTX,
14075 NULL_RTX);
14077 else if (! refers_to_regno_p (i, PATTERN (place))
14078 && ! find_regno_fusage (place, USE, i))
14079 for (tem_insn = PREV_INSN (place); ;
14080 tem_insn = PREV_INSN (tem_insn))
14082 if (!NONDEBUG_INSN_P (tem_insn))
14084 if (tem_insn == BB_HEAD (bb))
14085 break;
14086 continue;
14088 if (dead_or_set_p (tem_insn, piece)
14089 || reg_bitfield_target_p (piece,
14090 PATTERN (tem_insn)))
14092 add_reg_note (tem_insn, REG_UNUSED, piece);
14093 break;
14098 place = 0;
14102 break;
14104 default:
14105 /* Any other notes should not be present at this point in the
14106 compilation. */
14107 gcc_unreachable ();
14110 if (place)
14112 XEXP (note, 1) = REG_NOTES (place);
14113 REG_NOTES (place) = note;
14116 if (place2)
14117 add_shallow_copy_of_reg_note (place2, note);
14121 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14122 I3, I2, and I1 to new locations. This is also called to add a link
14123 pointing at I3 when I3's destination is changed. */
14125 static void
14126 distribute_links (struct insn_link *links)
14128 struct insn_link *link, *next_link;
14130 for (link = links; link; link = next_link)
14132 rtx_insn *place = 0;
14133 rtx_insn *insn;
14134 rtx set, reg;
14136 next_link = link->next;
14138 /* If the insn that this link points to is a NOTE, ignore it. */
14139 if (NOTE_P (link->insn))
14140 continue;
14142 set = 0;
14143 rtx pat = PATTERN (link->insn);
14144 if (GET_CODE (pat) == SET)
14145 set = pat;
14146 else if (GET_CODE (pat) == PARALLEL)
14148 int i;
14149 for (i = 0; i < XVECLEN (pat, 0); i++)
14151 set = XVECEXP (pat, 0, i);
14152 if (GET_CODE (set) != SET)
14153 continue;
14155 reg = SET_DEST (set);
14156 while (GET_CODE (reg) == ZERO_EXTRACT
14157 || GET_CODE (reg) == STRICT_LOW_PART
14158 || GET_CODE (reg) == SUBREG)
14159 reg = XEXP (reg, 0);
14161 if (!REG_P (reg))
14162 continue;
14164 if (REGNO (reg) == link->regno)
14165 break;
14167 if (i == XVECLEN (pat, 0))
14168 continue;
14170 else
14171 continue;
14173 reg = SET_DEST (set);
14175 while (GET_CODE (reg) == ZERO_EXTRACT
14176 || GET_CODE (reg) == STRICT_LOW_PART
14177 || GET_CODE (reg) == SUBREG)
14178 reg = XEXP (reg, 0);
14180 /* A LOG_LINK is defined as being placed on the first insn that uses
14181 a register and points to the insn that sets the register. Start
14182 searching at the next insn after the target of the link and stop
14183 when we reach a set of the register or the end of the basic block.
14185 Note that this correctly handles the link that used to point from
14186 I3 to I2. Also note that not much searching is typically done here
14187 since most links don't point very far away. */
14189 for (insn = NEXT_INSN (link->insn);
14190 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14191 || BB_HEAD (this_basic_block->next_bb) != insn));
14192 insn = NEXT_INSN (insn))
14193 if (DEBUG_INSN_P (insn))
14194 continue;
14195 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14197 if (reg_referenced_p (reg, PATTERN (insn)))
14198 place = insn;
14199 break;
14201 else if (CALL_P (insn)
14202 && find_reg_fusage (insn, USE, reg))
14204 place = insn;
14205 break;
14207 else if (INSN_P (insn) && reg_set_p (reg, insn))
14208 break;
14210 /* If we found a place to put the link, place it there unless there
14211 is already a link to the same insn as LINK at that point. */
14213 if (place)
14215 struct insn_link *link2;
14217 FOR_EACH_LOG_LINK (link2, place)
14218 if (link2->insn == link->insn && link2->regno == link->regno)
14219 break;
14221 if (link2 == NULL)
14223 link->next = LOG_LINKS (place);
14224 LOG_LINKS (place) = link;
14226 /* Set added_links_insn to the earliest insn we added a
14227 link to. */
14228 if (added_links_insn == 0
14229 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14230 added_links_insn = place;
14236 /* Check for any register or memory mentioned in EQUIV that is not
14237 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14238 of EXPR where some registers may have been replaced by constants. */
14240 static bool
14241 unmentioned_reg_p (rtx equiv, rtx expr)
14243 subrtx_iterator::array_type array;
14244 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14246 const_rtx x = *iter;
14247 if ((REG_P (x) || MEM_P (x))
14248 && !reg_mentioned_p (x, expr))
14249 return true;
14251 return false;
14254 DEBUG_FUNCTION void
14255 dump_combine_stats (FILE *file)
14257 fprintf
14258 (file,
14259 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14260 combine_attempts, combine_merges, combine_extras, combine_successes);
14263 void
14264 dump_combine_total_stats (FILE *file)
14266 fprintf
14267 (file,
14268 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14269 total_attempts, total_merges, total_extras, total_successes);
14272 /* Try combining insns through substitution. */
14273 static unsigned int
14274 rest_of_handle_combine (void)
14276 int rebuild_jump_labels_after_combine;
14278 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14279 df_note_add_problem ();
14280 df_analyze ();
14282 regstat_init_n_sets_and_refs ();
14283 reg_n_sets_max = max_reg_num ();
14285 rebuild_jump_labels_after_combine
14286 = combine_instructions (get_insns (), max_reg_num ());
14288 /* Combining insns may have turned an indirect jump into a
14289 direct jump. Rebuild the JUMP_LABEL fields of jumping
14290 instructions. */
14291 if (rebuild_jump_labels_after_combine)
14293 timevar_push (TV_JUMP);
14294 rebuild_jump_labels (get_insns ());
14295 cleanup_cfg (0);
14296 timevar_pop (TV_JUMP);
14299 regstat_free_n_sets_and_refs ();
14300 return 0;
14303 namespace {
14305 const pass_data pass_data_combine =
14307 RTL_PASS, /* type */
14308 "combine", /* name */
14309 OPTGROUP_NONE, /* optinfo_flags */
14310 TV_COMBINE, /* tv_id */
14311 PROP_cfglayout, /* properties_required */
14312 0, /* properties_provided */
14313 0, /* properties_destroyed */
14314 0, /* todo_flags_start */
14315 TODO_df_finish, /* todo_flags_finish */
14318 class pass_combine : public rtl_opt_pass
14320 public:
14321 pass_combine (gcc::context *ctxt)
14322 : rtl_opt_pass (pass_data_combine, ctxt)
14325 /* opt_pass methods: */
14326 virtual bool gate (function *) { return (optimize > 0); }
14327 virtual unsigned int execute (function *)
14329 return rest_of_handle_combine ();
14332 }; // class pass_combine
14334 } // anon namespace
14336 rtl_opt_pass *
14337 make_pass_combine (gcc::context *ctxt)
14339 return new pass_combine (ctxt);