* gcc.dg/20020919-1.c: Use _ARCH_PPC64 to test for -mpowerpc64.
[official-gcc.git] / gcc / combine.c
blob597aa8035ff3ac3199c65ed4fa2758230806b51f
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This module is essentially the "combiner" phase of the U. of Arizona
21 Portable Optimizer, but redone to work on our list-structured
22 representation for RTL instead of their string representation.
24 The LOG_LINKS of each insn identify the most recent assignment
25 to each REG used in the insn. It is a list of previous insns,
26 each of which contains a SET for a REG that is used in this insn
27 and not used or set in between. LOG_LINKs never cross basic blocks.
28 They were set up by the preceding pass (lifetime analysis).
30 We try to combine each pair of insns joined by a logical link.
31 We also try to combine triplets of insns A, B and C when C has
32 a link back to B and B has a link back to A. Likewise for a
33 small number of quadruplets of insns A, B, C and D for which
34 there's high likelihood of of success.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "hash-set.h"
84 #include "machmode.h"
85 #include "vec.h"
86 #include "double-int.h"
87 #include "input.h"
88 #include "alias.h"
89 #include "symtab.h"
90 #include "wide-int.h"
91 #include "inchash.h"
92 #include "tree.h"
93 #include "stor-layout.h"
94 #include "tm_p.h"
95 #include "flags.h"
96 #include "regs.h"
97 #include "hard-reg-set.h"
98 #include "predict.h"
99 #include "function.h"
100 #include "dominance.h"
101 #include "cfg.h"
102 #include "cfgrtl.h"
103 #include "cfgcleanup.h"
104 #include "basic-block.h"
105 #include "insn-config.h"
106 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
107 #include "hashtab.h"
108 #include "statistics.h"
109 #include "real.h"
110 #include "fixed-value.h"
111 #include "expmed.h"
112 #include "dojump.h"
113 #include "explow.h"
114 #include "calls.h"
115 #include "emit-rtl.h"
116 #include "varasm.h"
117 #include "stmt.h"
118 #include "expr.h"
119 #include "insn-attr.h"
120 #include "recog.h"
121 #include "diagnostic-core.h"
122 #include "target.h"
123 #include "insn-codes.h"
124 #include "optabs.h"
125 #include "rtlhooks-def.h"
126 #include "params.h"
127 #include "tree-pass.h"
128 #include "df.h"
129 #include "valtrack.h"
130 #include "hash-map.h"
131 #include "is-a.h"
132 #include "plugin-api.h"
133 #include "ipa-ref.h"
134 #include "cgraph.h"
135 #include "obstack.h"
136 #include "rtl-iter.h"
138 /* Number of attempts to combine instructions in this function. */
140 static int combine_attempts;
142 /* Number of attempts that got as far as substitution in this function. */
144 static int combine_merges;
146 /* Number of instructions combined with added SETs in this function. */
148 static int combine_extras;
150 /* Number of instructions combined in this function. */
152 static int combine_successes;
154 /* Totals over entire compilation. */
156 static int total_attempts, total_merges, total_extras, total_successes;
158 /* combine_instructions may try to replace the right hand side of the
159 second instruction with the value of an associated REG_EQUAL note
160 before throwing it at try_combine. That is problematic when there
161 is a REG_DEAD note for a register used in the old right hand side
162 and can cause distribute_notes to do wrong things. This is the
163 second instruction if it has been so modified, null otherwise. */
165 static rtx_insn *i2mod;
167 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
169 static rtx i2mod_old_rhs;
171 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
173 static rtx i2mod_new_rhs;
175 typedef struct reg_stat_struct {
176 /* Record last point of death of (hard or pseudo) register n. */
177 rtx_insn *last_death;
179 /* Record last point of modification of (hard or pseudo) register n. */
180 rtx_insn *last_set;
182 /* The next group of fields allows the recording of the last value assigned
183 to (hard or pseudo) register n. We use this information to see if an
184 operation being processed is redundant given a prior operation performed
185 on the register. For example, an `and' with a constant is redundant if
186 all the zero bits are already known to be turned off.
188 We use an approach similar to that used by cse, but change it in the
189 following ways:
191 (1) We do not want to reinitialize at each label.
192 (2) It is useful, but not critical, to know the actual value assigned
193 to a register. Often just its form is helpful.
195 Therefore, we maintain the following fields:
197 last_set_value the last value assigned
198 last_set_label records the value of label_tick when the
199 register was assigned
200 last_set_table_tick records the value of label_tick when a
201 value using the register is assigned
202 last_set_invalid set to nonzero when it is not valid
203 to use the value of this register in some
204 register's value
206 To understand the usage of these tables, it is important to understand
207 the distinction between the value in last_set_value being valid and
208 the register being validly contained in some other expression in the
209 table.
211 (The next two parameters are out of date).
213 reg_stat[i].last_set_value is valid if it is nonzero, and either
214 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
216 Register I may validly appear in any expression returned for the value
217 of another register if reg_n_sets[i] is 1. It may also appear in the
218 value for register J if reg_stat[j].last_set_invalid is zero, or
219 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
221 If an expression is found in the table containing a register which may
222 not validly appear in an expression, the register is replaced by
223 something that won't match, (clobber (const_int 0)). */
225 /* Record last value assigned to (hard or pseudo) register n. */
227 rtx last_set_value;
229 /* Record the value of label_tick when an expression involving register n
230 is placed in last_set_value. */
232 int last_set_table_tick;
234 /* Record the value of label_tick when the value for register n is placed in
235 last_set_value. */
237 int last_set_label;
239 /* These fields are maintained in parallel with last_set_value and are
240 used to store the mode in which the register was last set, the bits
241 that were known to be zero when it was last set, and the number of
242 sign bits copies it was known to have when it was last set. */
244 unsigned HOST_WIDE_INT last_set_nonzero_bits;
245 char last_set_sign_bit_copies;
246 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
248 /* Set nonzero if references to register n in expressions should not be
249 used. last_set_invalid is set nonzero when this register is being
250 assigned to and last_set_table_tick == label_tick. */
252 char last_set_invalid;
254 /* Some registers that are set more than once and used in more than one
255 basic block are nevertheless always set in similar ways. For example,
256 a QImode register may be loaded from memory in two places on a machine
257 where byte loads zero extend.
259 We record in the following fields if a register has some leading bits
260 that are always equal to the sign bit, and what we know about the
261 nonzero bits of a register, specifically which bits are known to be
262 zero.
264 If an entry is zero, it means that we don't know anything special. */
266 unsigned char sign_bit_copies;
268 unsigned HOST_WIDE_INT nonzero_bits;
270 /* Record the value of the label_tick when the last truncation
271 happened. The field truncated_to_mode is only valid if
272 truncation_label == label_tick. */
274 int truncation_label;
276 /* Record the last truncation seen for this register. If truncation
277 is not a nop to this mode we might be able to save an explicit
278 truncation if we know that value already contains a truncated
279 value. */
281 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
282 } reg_stat_type;
285 static vec<reg_stat_type> reg_stat;
287 /* Record the luid of the last insn that invalidated memory
288 (anything that writes memory, and subroutine calls, but not pushes). */
290 static int mem_last_set;
292 /* Record the luid of the last CALL_INSN
293 so we can tell whether a potential combination crosses any calls. */
295 static int last_call_luid;
297 /* When `subst' is called, this is the insn that is being modified
298 (by combining in a previous insn). The PATTERN of this insn
299 is still the old pattern partially modified and it should not be
300 looked at, but this may be used to examine the successors of the insn
301 to judge whether a simplification is valid. */
303 static rtx_insn *subst_insn;
305 /* This is the lowest LUID that `subst' is currently dealing with.
306 get_last_value will not return a value if the register was set at or
307 after this LUID. If not for this mechanism, we could get confused if
308 I2 or I1 in try_combine were an insn that used the old value of a register
309 to obtain a new value. In that case, we might erroneously get the
310 new value of the register when we wanted the old one. */
312 static int subst_low_luid;
314 /* This contains any hard registers that are used in newpat; reg_dead_at_p
315 must consider all these registers to be always live. */
317 static HARD_REG_SET newpat_used_regs;
319 /* This is an insn to which a LOG_LINKS entry has been added. If this
320 insn is the earlier than I2 or I3, combine should rescan starting at
321 that location. */
323 static rtx_insn *added_links_insn;
325 /* Basic block in which we are performing combines. */
326 static basic_block this_basic_block;
327 static bool optimize_this_for_speed_p;
330 /* Length of the currently allocated uid_insn_cost array. */
332 static int max_uid_known;
334 /* The following array records the insn_rtx_cost for every insn
335 in the instruction stream. */
337 static int *uid_insn_cost;
339 /* The following array records the LOG_LINKS for every insn in the
340 instruction stream as struct insn_link pointers. */
342 struct insn_link {
343 rtx_insn *insn;
344 unsigned int regno;
345 struct insn_link *next;
348 static struct insn_link **uid_log_links;
350 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
351 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
353 #define FOR_EACH_LOG_LINK(L, INSN) \
354 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
356 /* Links for LOG_LINKS are allocated from this obstack. */
358 static struct obstack insn_link_obstack;
360 /* Allocate a link. */
362 static inline struct insn_link *
363 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
365 struct insn_link *l
366 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
367 sizeof (struct insn_link));
368 l->insn = insn;
369 l->regno = regno;
370 l->next = next;
371 return l;
374 /* Incremented for each basic block. */
376 static int label_tick;
378 /* Reset to label_tick for each extended basic block in scanning order. */
380 static int label_tick_ebb_start;
382 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
383 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
385 static machine_mode nonzero_bits_mode;
387 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
388 be safely used. It is zero while computing them and after combine has
389 completed. This former test prevents propagating values based on
390 previously set values, which can be incorrect if a variable is modified
391 in a loop. */
393 static int nonzero_sign_valid;
396 /* Record one modification to rtl structure
397 to be undone by storing old_contents into *where. */
399 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
401 struct undo
403 struct undo *next;
404 enum undo_kind kind;
405 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
406 union { rtx *r; int *i; struct insn_link **l; } where;
409 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
410 num_undo says how many are currently recorded.
412 other_insn is nonzero if we have modified some other insn in the process
413 of working on subst_insn. It must be verified too. */
415 struct undobuf
417 struct undo *undos;
418 struct undo *frees;
419 rtx_insn *other_insn;
422 static struct undobuf undobuf;
424 /* Number of times the pseudo being substituted for
425 was found and replaced. */
427 static int n_occurrences;
429 static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
430 machine_mode,
431 unsigned HOST_WIDE_INT,
432 unsigned HOST_WIDE_INT *);
433 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
434 machine_mode,
435 unsigned int, unsigned int *);
436 static void do_SUBST (rtx *, rtx);
437 static void do_SUBST_INT (int *, int);
438 static void init_reg_last (void);
439 static void setup_incoming_promotions (rtx_insn *);
440 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
441 static int cant_combine_insn_p (rtx_insn *);
442 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
443 rtx_insn *, rtx_insn *, rtx *, rtx *);
444 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
445 static int contains_muldiv (rtx);
446 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
447 int *, rtx_insn *);
448 static void undo_all (void);
449 static void undo_commit (void);
450 static rtx *find_split_point (rtx *, rtx_insn *, bool);
451 static rtx subst (rtx, rtx, rtx, int, int, int);
452 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
453 static rtx simplify_if_then_else (rtx);
454 static rtx simplify_set (rtx);
455 static rtx simplify_logical (rtx);
456 static rtx expand_compound_operation (rtx);
457 static const_rtx expand_field_assignment (const_rtx);
458 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
459 rtx, unsigned HOST_WIDE_INT, int, int, int);
460 static rtx extract_left_shift (rtx, int);
461 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
462 unsigned HOST_WIDE_INT *);
463 static rtx canon_reg_for_combine (rtx, rtx);
464 static rtx force_to_mode (rtx, machine_mode,
465 unsigned HOST_WIDE_INT, int);
466 static rtx if_then_else_cond (rtx, rtx *, rtx *);
467 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
468 static int rtx_equal_for_field_assignment_p (rtx, rtx);
469 static rtx make_field_assignment (rtx);
470 static rtx apply_distributive_law (rtx);
471 static rtx distribute_and_simplify_rtx (rtx, int);
472 static rtx simplify_and_const_int_1 (machine_mode, rtx,
473 unsigned HOST_WIDE_INT);
474 static rtx simplify_and_const_int (rtx, machine_mode, rtx,
475 unsigned HOST_WIDE_INT);
476 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
477 HOST_WIDE_INT, machine_mode, int *);
478 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
479 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
480 int);
481 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
482 static rtx gen_lowpart_for_combine (machine_mode, rtx);
483 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
484 rtx, rtx *);
485 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
486 static void update_table_tick (rtx);
487 static void record_value_for_reg (rtx, rtx_insn *, rtx);
488 static void check_promoted_subreg (rtx_insn *, rtx);
489 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
490 static void record_dead_and_set_regs (rtx_insn *);
491 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
492 static rtx get_last_value (const_rtx);
493 static int use_crosses_set_p (const_rtx, int);
494 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
495 static int reg_dead_at_p (rtx, rtx_insn *);
496 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
497 static int reg_bitfield_target_p (rtx, rtx);
498 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
499 static void distribute_links (struct insn_link *);
500 static void mark_used_regs_combine (rtx);
501 static void record_promoted_value (rtx_insn *, rtx);
502 static bool unmentioned_reg_p (rtx, rtx);
503 static void record_truncated_values (rtx *, void *);
504 static bool reg_truncated_to_mode (machine_mode, const_rtx);
505 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
508 /* It is not safe to use ordinary gen_lowpart in combine.
509 See comments in gen_lowpart_for_combine. */
510 #undef RTL_HOOKS_GEN_LOWPART
511 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
513 /* Our implementation of gen_lowpart never emits a new pseudo. */
514 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
515 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
517 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
518 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
520 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
521 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
523 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
524 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
526 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
529 /* Convenience wrapper for the canonicalize_comparison target hook.
530 Target hooks cannot use enum rtx_code. */
531 static inline void
532 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
533 bool op0_preserve_value)
535 int code_int = (int)*code;
536 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
537 *code = (enum rtx_code)code_int;
540 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
541 PATTERN can not be split. Otherwise, it returns an insn sequence.
542 This is a wrapper around split_insns which ensures that the
543 reg_stat vector is made larger if the splitter creates a new
544 register. */
546 static rtx_insn *
547 combine_split_insns (rtx pattern, rtx insn)
549 rtx_insn *ret;
550 unsigned int nregs;
552 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
553 nregs = max_reg_num ();
554 if (nregs > reg_stat.length ())
555 reg_stat.safe_grow_cleared (nregs);
556 return ret;
559 /* This is used by find_single_use to locate an rtx in LOC that
560 contains exactly one use of DEST, which is typically either a REG
561 or CC0. It returns a pointer to the innermost rtx expression
562 containing DEST. Appearances of DEST that are being used to
563 totally replace it are not counted. */
565 static rtx *
566 find_single_use_1 (rtx dest, rtx *loc)
568 rtx x = *loc;
569 enum rtx_code code = GET_CODE (x);
570 rtx *result = NULL;
571 rtx *this_result;
572 int i;
573 const char *fmt;
575 switch (code)
577 case CONST:
578 case LABEL_REF:
579 case SYMBOL_REF:
580 CASE_CONST_ANY:
581 case CLOBBER:
582 return 0;
584 case SET:
585 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
586 of a REG that occupies all of the REG, the insn uses DEST if
587 it is mentioned in the destination or the source. Otherwise, we
588 need just check the source. */
589 if (GET_CODE (SET_DEST (x)) != CC0
590 && GET_CODE (SET_DEST (x)) != PC
591 && !REG_P (SET_DEST (x))
592 && ! (GET_CODE (SET_DEST (x)) == SUBREG
593 && REG_P (SUBREG_REG (SET_DEST (x)))
594 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
595 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
596 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
597 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
598 break;
600 return find_single_use_1 (dest, &SET_SRC (x));
602 case MEM:
603 case SUBREG:
604 return find_single_use_1 (dest, &XEXP (x, 0));
606 default:
607 break;
610 /* If it wasn't one of the common cases above, check each expression and
611 vector of this code. Look for a unique usage of DEST. */
613 fmt = GET_RTX_FORMAT (code);
614 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
616 if (fmt[i] == 'e')
618 if (dest == XEXP (x, i)
619 || (REG_P (dest) && REG_P (XEXP (x, i))
620 && REGNO (dest) == REGNO (XEXP (x, i))))
621 this_result = loc;
622 else
623 this_result = find_single_use_1 (dest, &XEXP (x, i));
625 if (result == NULL)
626 result = this_result;
627 else if (this_result)
628 /* Duplicate usage. */
629 return NULL;
631 else if (fmt[i] == 'E')
633 int j;
635 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
637 if (XVECEXP (x, i, j) == dest
638 || (REG_P (dest)
639 && REG_P (XVECEXP (x, i, j))
640 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
641 this_result = loc;
642 else
643 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
645 if (result == NULL)
646 result = this_result;
647 else if (this_result)
648 return NULL;
653 return result;
657 /* See if DEST, produced in INSN, is used only a single time in the
658 sequel. If so, return a pointer to the innermost rtx expression in which
659 it is used.
661 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
663 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
664 care about REG_DEAD notes or LOG_LINKS.
666 Otherwise, we find the single use by finding an insn that has a
667 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
668 only referenced once in that insn, we know that it must be the first
669 and last insn referencing DEST. */
671 static rtx *
672 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
674 basic_block bb;
675 rtx_insn *next;
676 rtx *result;
677 struct insn_link *link;
679 #ifdef HAVE_cc0
680 if (dest == cc0_rtx)
682 next = NEXT_INSN (insn);
683 if (next == 0
684 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
685 return 0;
687 result = find_single_use_1 (dest, &PATTERN (next));
688 if (result && ploc)
689 *ploc = next;
690 return result;
692 #endif
694 if (!REG_P (dest))
695 return 0;
697 bb = BLOCK_FOR_INSN (insn);
698 for (next = NEXT_INSN (insn);
699 next && BLOCK_FOR_INSN (next) == bb;
700 next = NEXT_INSN (next))
701 if (INSN_P (next) && dead_or_set_p (next, dest))
703 FOR_EACH_LOG_LINK (link, next)
704 if (link->insn == insn && link->regno == REGNO (dest))
705 break;
707 if (link)
709 result = find_single_use_1 (dest, &PATTERN (next));
710 if (ploc)
711 *ploc = next;
712 return result;
716 return 0;
719 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
720 insn. The substitution can be undone by undo_all. If INTO is already
721 set to NEWVAL, do not record this change. Because computing NEWVAL might
722 also call SUBST, we have to compute it before we put anything into
723 the undo table. */
725 static void
726 do_SUBST (rtx *into, rtx newval)
728 struct undo *buf;
729 rtx oldval = *into;
731 if (oldval == newval)
732 return;
734 /* We'd like to catch as many invalid transformations here as
735 possible. Unfortunately, there are way too many mode changes
736 that are perfectly valid, so we'd waste too much effort for
737 little gain doing the checks here. Focus on catching invalid
738 transformations involving integer constants. */
739 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
740 && CONST_INT_P (newval))
742 /* Sanity check that we're replacing oldval with a CONST_INT
743 that is a valid sign-extension for the original mode. */
744 gcc_assert (INTVAL (newval)
745 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
747 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
748 CONST_INT is not valid, because after the replacement, the
749 original mode would be gone. Unfortunately, we can't tell
750 when do_SUBST is called to replace the operand thereof, so we
751 perform this test on oldval instead, checking whether an
752 invalid replacement took place before we got here. */
753 gcc_assert (!(GET_CODE (oldval) == SUBREG
754 && CONST_INT_P (SUBREG_REG (oldval))));
755 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
756 && CONST_INT_P (XEXP (oldval, 0))));
759 if (undobuf.frees)
760 buf = undobuf.frees, undobuf.frees = buf->next;
761 else
762 buf = XNEW (struct undo);
764 buf->kind = UNDO_RTX;
765 buf->where.r = into;
766 buf->old_contents.r = oldval;
767 *into = newval;
769 buf->next = undobuf.undos, undobuf.undos = buf;
772 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
774 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
775 for the value of a HOST_WIDE_INT value (including CONST_INT) is
776 not safe. */
778 static void
779 do_SUBST_INT (int *into, int newval)
781 struct undo *buf;
782 int oldval = *into;
784 if (oldval == newval)
785 return;
787 if (undobuf.frees)
788 buf = undobuf.frees, undobuf.frees = buf->next;
789 else
790 buf = XNEW (struct undo);
792 buf->kind = UNDO_INT;
793 buf->where.i = into;
794 buf->old_contents.i = oldval;
795 *into = newval;
797 buf->next = undobuf.undos, undobuf.undos = buf;
800 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
802 /* Similar to SUBST, but just substitute the mode. This is used when
803 changing the mode of a pseudo-register, so that any other
804 references to the entry in the regno_reg_rtx array will change as
805 well. */
807 static void
808 do_SUBST_MODE (rtx *into, machine_mode newval)
810 struct undo *buf;
811 machine_mode oldval = GET_MODE (*into);
813 if (oldval == newval)
814 return;
816 if (undobuf.frees)
817 buf = undobuf.frees, undobuf.frees = buf->next;
818 else
819 buf = XNEW (struct undo);
821 buf->kind = UNDO_MODE;
822 buf->where.r = into;
823 buf->old_contents.m = oldval;
824 adjust_reg_mode (*into, newval);
826 buf->next = undobuf.undos, undobuf.undos = buf;
829 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
831 #ifndef HAVE_cc0
832 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
834 static void
835 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
837 struct undo *buf;
838 struct insn_link * oldval = *into;
840 if (oldval == newval)
841 return;
843 if (undobuf.frees)
844 buf = undobuf.frees, undobuf.frees = buf->next;
845 else
846 buf = XNEW (struct undo);
848 buf->kind = UNDO_LINKS;
849 buf->where.l = into;
850 buf->old_contents.l = oldval;
851 *into = newval;
853 buf->next = undobuf.undos, undobuf.undos = buf;
856 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
857 #endif
859 /* Subroutine of try_combine. Determine whether the replacement patterns
860 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
861 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
862 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
863 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
864 of all the instructions can be estimated and the replacements are more
865 expensive than the original sequence. */
867 static bool
868 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
869 rtx newpat, rtx newi2pat, rtx newotherpat)
871 int i0_cost, i1_cost, i2_cost, i3_cost;
872 int new_i2_cost, new_i3_cost;
873 int old_cost, new_cost;
875 /* Lookup the original insn_rtx_costs. */
876 i2_cost = INSN_COST (i2);
877 i3_cost = INSN_COST (i3);
879 if (i1)
881 i1_cost = INSN_COST (i1);
882 if (i0)
884 i0_cost = INSN_COST (i0);
885 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
886 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
888 else
890 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
891 ? i1_cost + i2_cost + i3_cost : 0);
892 i0_cost = 0;
895 else
897 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
898 i1_cost = i0_cost = 0;
901 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
902 correct that. */
903 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
904 old_cost -= i1_cost;
907 /* Calculate the replacement insn_rtx_costs. */
908 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
909 if (newi2pat)
911 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
912 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
913 ? new_i2_cost + new_i3_cost : 0;
915 else
917 new_cost = new_i3_cost;
918 new_i2_cost = 0;
921 if (undobuf.other_insn)
923 int old_other_cost, new_other_cost;
925 old_other_cost = INSN_COST (undobuf.other_insn);
926 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
927 if (old_other_cost > 0 && new_other_cost > 0)
929 old_cost += old_other_cost;
930 new_cost += new_other_cost;
932 else
933 old_cost = 0;
936 /* Disallow this combination if both new_cost and old_cost are greater than
937 zero, and new_cost is greater than old cost. */
938 int reject = old_cost > 0 && new_cost > old_cost;
940 if (dump_file)
942 fprintf (dump_file, "%s combination of insns ",
943 reject ? "rejecting" : "allowing");
944 if (i0)
945 fprintf (dump_file, "%d, ", INSN_UID (i0));
946 if (i1 && INSN_UID (i1) != INSN_UID (i2))
947 fprintf (dump_file, "%d, ", INSN_UID (i1));
948 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
950 fprintf (dump_file, "original costs ");
951 if (i0)
952 fprintf (dump_file, "%d + ", i0_cost);
953 if (i1 && INSN_UID (i1) != INSN_UID (i2))
954 fprintf (dump_file, "%d + ", i1_cost);
955 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
957 if (newi2pat)
958 fprintf (dump_file, "replacement costs %d + %d = %d\n",
959 new_i2_cost, new_i3_cost, new_cost);
960 else
961 fprintf (dump_file, "replacement cost %d\n", new_cost);
964 if (reject)
965 return false;
967 /* Update the uid_insn_cost array with the replacement costs. */
968 INSN_COST (i2) = new_i2_cost;
969 INSN_COST (i3) = new_i3_cost;
970 if (i1)
972 INSN_COST (i1) = 0;
973 if (i0)
974 INSN_COST (i0) = 0;
977 return true;
981 /* Delete any insns that copy a register to itself. */
983 static void
984 delete_noop_moves (void)
986 rtx_insn *insn, *next;
987 basic_block bb;
989 FOR_EACH_BB_FN (bb, cfun)
991 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
993 next = NEXT_INSN (insn);
994 if (INSN_P (insn) && noop_move_p (insn))
996 if (dump_file)
997 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
999 delete_insn_and_edges (insn);
1006 /* Return false if we do not want to (or cannot) combine DEF. */
1007 static bool
1008 can_combine_def_p (df_ref def)
1010 /* Do not consider if it is pre/post modification in MEM. */
1011 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1012 return false;
1014 unsigned int regno = DF_REF_REGNO (def);
1016 /* Do not combine frame pointer adjustments. */
1017 if ((regno == FRAME_POINTER_REGNUM
1018 && (!reload_completed || frame_pointer_needed))
1019 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1020 || (regno == HARD_FRAME_POINTER_REGNUM
1021 && (!reload_completed || frame_pointer_needed))
1022 #endif
1023 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1024 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1025 #endif
1027 return false;
1029 return true;
1032 /* Return false if we do not want to (or cannot) combine USE. */
1033 static bool
1034 can_combine_use_p (df_ref use)
1036 /* Do not consider the usage of the stack pointer by function call. */
1037 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1038 return false;
1040 return true;
1043 /* Fill in log links field for all insns. */
1045 static void
1046 create_log_links (void)
1048 basic_block bb;
1049 rtx_insn **next_use;
1050 rtx_insn *insn;
1051 df_ref def, use;
1053 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1055 /* Pass through each block from the end, recording the uses of each
1056 register and establishing log links when def is encountered.
1057 Note that we do not clear next_use array in order to save time,
1058 so we have to test whether the use is in the same basic block as def.
1060 There are a few cases below when we do not consider the definition or
1061 usage -- these are taken from original flow.c did. Don't ask me why it is
1062 done this way; I don't know and if it works, I don't want to know. */
1064 FOR_EACH_BB_FN (bb, cfun)
1066 FOR_BB_INSNS_REVERSE (bb, insn)
1068 if (!NONDEBUG_INSN_P (insn))
1069 continue;
1071 /* Log links are created only once. */
1072 gcc_assert (!LOG_LINKS (insn));
1074 FOR_EACH_INSN_DEF (def, insn)
1076 unsigned int regno = DF_REF_REGNO (def);
1077 rtx_insn *use_insn;
1079 if (!next_use[regno])
1080 continue;
1082 if (!can_combine_def_p (def))
1083 continue;
1085 use_insn = next_use[regno];
1086 next_use[regno] = NULL;
1088 if (BLOCK_FOR_INSN (use_insn) != bb)
1089 continue;
1091 /* flow.c claimed:
1093 We don't build a LOG_LINK for hard registers contained
1094 in ASM_OPERANDs. If these registers get replaced,
1095 we might wind up changing the semantics of the insn,
1096 even if reload can make what appear to be valid
1097 assignments later. */
1098 if (regno < FIRST_PSEUDO_REGISTER
1099 && asm_noperands (PATTERN (use_insn)) >= 0)
1100 continue;
1102 /* Don't add duplicate links between instructions. */
1103 struct insn_link *links;
1104 FOR_EACH_LOG_LINK (links, use_insn)
1105 if (insn == links->insn && regno == links->regno)
1106 break;
1108 if (!links)
1109 LOG_LINKS (use_insn)
1110 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1113 FOR_EACH_INSN_USE (use, insn)
1114 if (can_combine_use_p (use))
1115 next_use[DF_REF_REGNO (use)] = insn;
1119 free (next_use);
1122 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1123 true if we found a LOG_LINK that proves that A feeds B. This only works
1124 if there are no instructions between A and B which could have a link
1125 depending on A, since in that case we would not record a link for B.
1126 We also check the implicit dependency created by a cc0 setter/user
1127 pair. */
1129 static bool
1130 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1132 struct insn_link *links;
1133 FOR_EACH_LOG_LINK (links, b)
1134 if (links->insn == a)
1135 return true;
1136 #ifdef HAVE_cc0
1137 if (sets_cc0_p (a))
1138 return true;
1139 #endif
1140 return false;
1143 /* Main entry point for combiner. F is the first insn of the function.
1144 NREGS is the first unused pseudo-reg number.
1146 Return nonzero if the combiner has turned an indirect jump
1147 instruction into a direct jump. */
1148 static int
1149 combine_instructions (rtx_insn *f, unsigned int nregs)
1151 rtx_insn *insn, *next;
1152 #ifdef HAVE_cc0
1153 rtx_insn *prev;
1154 #endif
1155 struct insn_link *links, *nextlinks;
1156 rtx_insn *first;
1157 basic_block last_bb;
1159 int new_direct_jump_p = 0;
1161 for (first = f; first && !INSN_P (first); )
1162 first = NEXT_INSN (first);
1163 if (!first)
1164 return 0;
1166 combine_attempts = 0;
1167 combine_merges = 0;
1168 combine_extras = 0;
1169 combine_successes = 0;
1171 rtl_hooks = combine_rtl_hooks;
1173 reg_stat.safe_grow_cleared (nregs);
1175 init_recog_no_volatile ();
1177 /* Allocate array for insn info. */
1178 max_uid_known = get_max_uid ();
1179 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1180 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1181 gcc_obstack_init (&insn_link_obstack);
1183 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1185 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1186 problems when, for example, we have j <<= 1 in a loop. */
1188 nonzero_sign_valid = 0;
1189 label_tick = label_tick_ebb_start = 1;
1191 /* Scan all SETs and see if we can deduce anything about what
1192 bits are known to be zero for some registers and how many copies
1193 of the sign bit are known to exist for those registers.
1195 Also set any known values so that we can use it while searching
1196 for what bits are known to be set. */
1198 setup_incoming_promotions (first);
1199 /* Allow the entry block and the first block to fall into the same EBB.
1200 Conceptually the incoming promotions are assigned to the entry block. */
1201 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1203 create_log_links ();
1204 FOR_EACH_BB_FN (this_basic_block, cfun)
1206 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1207 last_call_luid = 0;
1208 mem_last_set = -1;
1210 label_tick++;
1211 if (!single_pred_p (this_basic_block)
1212 || single_pred (this_basic_block) != last_bb)
1213 label_tick_ebb_start = label_tick;
1214 last_bb = this_basic_block;
1216 FOR_BB_INSNS (this_basic_block, insn)
1217 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1219 #ifdef AUTO_INC_DEC
1220 rtx links;
1221 #endif
1223 subst_low_luid = DF_INSN_LUID (insn);
1224 subst_insn = insn;
1226 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1227 insn);
1228 record_dead_and_set_regs (insn);
1230 #ifdef AUTO_INC_DEC
1231 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1232 if (REG_NOTE_KIND (links) == REG_INC)
1233 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1234 insn);
1235 #endif
1237 /* Record the current insn_rtx_cost of this instruction. */
1238 if (NONJUMP_INSN_P (insn))
1239 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1240 optimize_this_for_speed_p);
1241 if (dump_file)
1242 fprintf (dump_file, "insn_cost %d: %d\n",
1243 INSN_UID (insn), INSN_COST (insn));
1247 nonzero_sign_valid = 1;
1249 /* Now scan all the insns in forward order. */
1250 label_tick = label_tick_ebb_start = 1;
1251 init_reg_last ();
1252 setup_incoming_promotions (first);
1253 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1254 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1256 FOR_EACH_BB_FN (this_basic_block, cfun)
1258 rtx_insn *last_combined_insn = NULL;
1259 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1260 last_call_luid = 0;
1261 mem_last_set = -1;
1263 label_tick++;
1264 if (!single_pred_p (this_basic_block)
1265 || single_pred (this_basic_block) != last_bb)
1266 label_tick_ebb_start = label_tick;
1267 last_bb = this_basic_block;
1269 rtl_profile_for_bb (this_basic_block);
1270 for (insn = BB_HEAD (this_basic_block);
1271 insn != NEXT_INSN (BB_END (this_basic_block));
1272 insn = next ? next : NEXT_INSN (insn))
1274 next = 0;
1275 if (!NONDEBUG_INSN_P (insn))
1276 continue;
1278 while (last_combined_insn
1279 && last_combined_insn->deleted ())
1280 last_combined_insn = PREV_INSN (last_combined_insn);
1281 if (last_combined_insn == NULL_RTX
1282 || BARRIER_P (last_combined_insn)
1283 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1284 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1285 last_combined_insn = insn;
1287 /* See if we know about function return values before this
1288 insn based upon SUBREG flags. */
1289 check_promoted_subreg (insn, PATTERN (insn));
1291 /* See if we can find hardregs and subreg of pseudos in
1292 narrower modes. This could help turning TRUNCATEs
1293 into SUBREGs. */
1294 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1296 /* Try this insn with each insn it links back to. */
1298 FOR_EACH_LOG_LINK (links, insn)
1299 if ((next = try_combine (insn, links->insn, NULL,
1300 NULL, &new_direct_jump_p,
1301 last_combined_insn)) != 0)
1303 statistics_counter_event (cfun, "two-insn combine", 1);
1304 goto retry;
1307 /* Try each sequence of three linked insns ending with this one. */
1309 if (max_combine >= 3)
1310 FOR_EACH_LOG_LINK (links, insn)
1312 rtx_insn *link = links->insn;
1314 /* If the linked insn has been replaced by a note, then there
1315 is no point in pursuing this chain any further. */
1316 if (NOTE_P (link))
1317 continue;
1319 FOR_EACH_LOG_LINK (nextlinks, link)
1320 if ((next = try_combine (insn, link, nextlinks->insn,
1321 NULL, &new_direct_jump_p,
1322 last_combined_insn)) != 0)
1324 statistics_counter_event (cfun, "three-insn combine", 1);
1325 goto retry;
1329 #ifdef HAVE_cc0
1330 /* Try to combine a jump insn that uses CC0
1331 with a preceding insn that sets CC0, and maybe with its
1332 logical predecessor as well.
1333 This is how we make decrement-and-branch insns.
1334 We need this special code because data flow connections
1335 via CC0 do not get entered in LOG_LINKS. */
1337 if (JUMP_P (insn)
1338 && (prev = prev_nonnote_insn (insn)) != 0
1339 && NONJUMP_INSN_P (prev)
1340 && sets_cc0_p (PATTERN (prev)))
1342 if ((next = try_combine (insn, prev, NULL, NULL,
1343 &new_direct_jump_p,
1344 last_combined_insn)) != 0)
1345 goto retry;
1347 FOR_EACH_LOG_LINK (nextlinks, prev)
1348 if ((next = try_combine (insn, prev, nextlinks->insn,
1349 NULL, &new_direct_jump_p,
1350 last_combined_insn)) != 0)
1351 goto retry;
1354 /* Do the same for an insn that explicitly references CC0. */
1355 if (NONJUMP_INSN_P (insn)
1356 && (prev = prev_nonnote_insn (insn)) != 0
1357 && NONJUMP_INSN_P (prev)
1358 && sets_cc0_p (PATTERN (prev))
1359 && GET_CODE (PATTERN (insn)) == SET
1360 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1362 if ((next = try_combine (insn, prev, NULL, NULL,
1363 &new_direct_jump_p,
1364 last_combined_insn)) != 0)
1365 goto retry;
1367 FOR_EACH_LOG_LINK (nextlinks, prev)
1368 if ((next = try_combine (insn, prev, nextlinks->insn,
1369 NULL, &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1374 /* Finally, see if any of the insns that this insn links to
1375 explicitly references CC0. If so, try this insn, that insn,
1376 and its predecessor if it sets CC0. */
1377 FOR_EACH_LOG_LINK (links, insn)
1378 if (NONJUMP_INSN_P (links->insn)
1379 && GET_CODE (PATTERN (links->insn)) == SET
1380 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1381 && (prev = prev_nonnote_insn (links->insn)) != 0
1382 && NONJUMP_INSN_P (prev)
1383 && sets_cc0_p (PATTERN (prev))
1384 && (next = try_combine (insn, links->insn,
1385 prev, NULL, &new_direct_jump_p,
1386 last_combined_insn)) != 0)
1387 goto retry;
1388 #endif
1390 /* Try combining an insn with two different insns whose results it
1391 uses. */
1392 if (max_combine >= 3)
1393 FOR_EACH_LOG_LINK (links, insn)
1394 for (nextlinks = links->next; nextlinks;
1395 nextlinks = nextlinks->next)
1396 if ((next = try_combine (insn, links->insn,
1397 nextlinks->insn, NULL,
1398 &new_direct_jump_p,
1399 last_combined_insn)) != 0)
1402 statistics_counter_event (cfun, "three-insn combine", 1);
1403 goto retry;
1406 /* Try four-instruction combinations. */
1407 if (max_combine >= 4)
1408 FOR_EACH_LOG_LINK (links, insn)
1410 struct insn_link *next1;
1411 rtx_insn *link = links->insn;
1413 /* If the linked insn has been replaced by a note, then there
1414 is no point in pursuing this chain any further. */
1415 if (NOTE_P (link))
1416 continue;
1418 FOR_EACH_LOG_LINK (next1, link)
1420 rtx_insn *link1 = next1->insn;
1421 if (NOTE_P (link1))
1422 continue;
1423 /* I0 -> I1 -> I2 -> I3. */
1424 FOR_EACH_LOG_LINK (nextlinks, link1)
1425 if ((next = try_combine (insn, link, link1,
1426 nextlinks->insn,
1427 &new_direct_jump_p,
1428 last_combined_insn)) != 0)
1430 statistics_counter_event (cfun, "four-insn combine", 1);
1431 goto retry;
1433 /* I0, I1 -> I2, I2 -> I3. */
1434 for (nextlinks = next1->next; nextlinks;
1435 nextlinks = nextlinks->next)
1436 if ((next = try_combine (insn, link, link1,
1437 nextlinks->insn,
1438 &new_direct_jump_p,
1439 last_combined_insn)) != 0)
1441 statistics_counter_event (cfun, "four-insn combine", 1);
1442 goto retry;
1446 for (next1 = links->next; next1; next1 = next1->next)
1448 rtx_insn *link1 = next1->insn;
1449 if (NOTE_P (link1))
1450 continue;
1451 /* I0 -> I2; I1, I2 -> I3. */
1452 FOR_EACH_LOG_LINK (nextlinks, link)
1453 if ((next = try_combine (insn, link, link1,
1454 nextlinks->insn,
1455 &new_direct_jump_p,
1456 last_combined_insn)) != 0)
1458 statistics_counter_event (cfun, "four-insn combine", 1);
1459 goto retry;
1461 /* I0 -> I1; I1, I2 -> I3. */
1462 FOR_EACH_LOG_LINK (nextlinks, link1)
1463 if ((next = try_combine (insn, link, link1,
1464 nextlinks->insn,
1465 &new_direct_jump_p,
1466 last_combined_insn)) != 0)
1468 statistics_counter_event (cfun, "four-insn combine", 1);
1469 goto retry;
1474 /* Try this insn with each REG_EQUAL note it links back to. */
1475 FOR_EACH_LOG_LINK (links, insn)
1477 rtx set, note;
1478 rtx_insn *temp = links->insn;
1479 if ((set = single_set (temp)) != 0
1480 && (note = find_reg_equal_equiv_note (temp)) != 0
1481 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1482 /* Avoid using a register that may already been marked
1483 dead by an earlier instruction. */
1484 && ! unmentioned_reg_p (note, SET_SRC (set))
1485 && (GET_MODE (note) == VOIDmode
1486 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1487 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1489 /* Temporarily replace the set's source with the
1490 contents of the REG_EQUAL note. The insn will
1491 be deleted or recognized by try_combine. */
1492 rtx orig = SET_SRC (set);
1493 SET_SRC (set) = note;
1494 i2mod = temp;
1495 i2mod_old_rhs = copy_rtx (orig);
1496 i2mod_new_rhs = copy_rtx (note);
1497 next = try_combine (insn, i2mod, NULL, NULL,
1498 &new_direct_jump_p,
1499 last_combined_insn);
1500 i2mod = NULL;
1501 if (next)
1503 statistics_counter_event (cfun, "insn-with-note combine", 1);
1504 goto retry;
1506 SET_SRC (set) = orig;
1510 if (!NOTE_P (insn))
1511 record_dead_and_set_regs (insn);
1513 retry:
1518 default_rtl_profile ();
1519 clear_bb_flags ();
1520 new_direct_jump_p |= purge_all_dead_edges ();
1521 delete_noop_moves ();
1523 /* Clean up. */
1524 obstack_free (&insn_link_obstack, NULL);
1525 free (uid_log_links);
1526 free (uid_insn_cost);
1527 reg_stat.release ();
1530 struct undo *undo, *next;
1531 for (undo = undobuf.frees; undo; undo = next)
1533 next = undo->next;
1534 free (undo);
1536 undobuf.frees = 0;
1539 total_attempts += combine_attempts;
1540 total_merges += combine_merges;
1541 total_extras += combine_extras;
1542 total_successes += combine_successes;
1544 nonzero_sign_valid = 0;
1545 rtl_hooks = general_rtl_hooks;
1547 /* Make recognizer allow volatile MEMs again. */
1548 init_recog ();
1550 return new_direct_jump_p;
1553 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1555 static void
1556 init_reg_last (void)
1558 unsigned int i;
1559 reg_stat_type *p;
1561 FOR_EACH_VEC_ELT (reg_stat, i, p)
1562 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1565 /* Set up any promoted values for incoming argument registers. */
1567 static void
1568 setup_incoming_promotions (rtx_insn *first)
1570 tree arg;
1571 bool strictly_local = false;
1573 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1574 arg = DECL_CHAIN (arg))
1576 rtx x, reg = DECL_INCOMING_RTL (arg);
1577 int uns1, uns3;
1578 machine_mode mode1, mode2, mode3, mode4;
1580 /* Only continue if the incoming argument is in a register. */
1581 if (!REG_P (reg))
1582 continue;
1584 /* Determine, if possible, whether all call sites of the current
1585 function lie within the current compilation unit. (This does
1586 take into account the exporting of a function via taking its
1587 address, and so forth.) */
1588 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1590 /* The mode and signedness of the argument before any promotions happen
1591 (equal to the mode of the pseudo holding it at that stage). */
1592 mode1 = TYPE_MODE (TREE_TYPE (arg));
1593 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1595 /* The mode and signedness of the argument after any source language and
1596 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1597 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1598 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1600 /* The mode and signedness of the argument as it is actually passed,
1601 see assign_parm_setup_reg in function.c. */
1602 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1603 TREE_TYPE (cfun->decl), 0);
1605 /* The mode of the register in which the argument is being passed. */
1606 mode4 = GET_MODE (reg);
1608 /* Eliminate sign extensions in the callee when:
1609 (a) A mode promotion has occurred; */
1610 if (mode1 == mode3)
1611 continue;
1612 /* (b) The mode of the register is the same as the mode of
1613 the argument as it is passed; */
1614 if (mode3 != mode4)
1615 continue;
1616 /* (c) There's no language level extension; */
1617 if (mode1 == mode2)
1619 /* (c.1) All callers are from the current compilation unit. If that's
1620 the case we don't have to rely on an ABI, we only have to know
1621 what we're generating right now, and we know that we will do the
1622 mode1 to mode2 promotion with the given sign. */
1623 else if (!strictly_local)
1624 continue;
1625 /* (c.2) The combination of the two promotions is useful. This is
1626 true when the signs match, or if the first promotion is unsigned.
1627 In the later case, (sign_extend (zero_extend x)) is the same as
1628 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1629 else if (uns1)
1630 uns3 = true;
1631 else if (uns3)
1632 continue;
1634 /* Record that the value was promoted from mode1 to mode3,
1635 so that any sign extension at the head of the current
1636 function may be eliminated. */
1637 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1638 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1639 record_value_for_reg (reg, first, x);
1643 /* Called via note_stores. If X is a pseudo that is narrower than
1644 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1646 If we are setting only a portion of X and we can't figure out what
1647 portion, assume all bits will be used since we don't know what will
1648 be happening.
1650 Similarly, set how many bits of X are known to be copies of the sign bit
1651 at all locations in the function. This is the smallest number implied
1652 by any set of X. */
1654 static void
1655 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1657 rtx_insn *insn = (rtx_insn *) data;
1658 unsigned int num;
1660 if (REG_P (x)
1661 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1662 /* If this register is undefined at the start of the file, we can't
1663 say what its contents were. */
1664 && ! REGNO_REG_SET_P
1665 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1666 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1668 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1670 if (set == 0 || GET_CODE (set) == CLOBBER)
1672 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1673 rsp->sign_bit_copies = 1;
1674 return;
1677 /* If this register is being initialized using itself, and the
1678 register is uninitialized in this basic block, and there are
1679 no LOG_LINKS which set the register, then part of the
1680 register is uninitialized. In that case we can't assume
1681 anything about the number of nonzero bits.
1683 ??? We could do better if we checked this in
1684 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1685 could avoid making assumptions about the insn which initially
1686 sets the register, while still using the information in other
1687 insns. We would have to be careful to check every insn
1688 involved in the combination. */
1690 if (insn
1691 && reg_referenced_p (x, PATTERN (insn))
1692 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1693 REGNO (x)))
1695 struct insn_link *link;
1697 FOR_EACH_LOG_LINK (link, insn)
1698 if (dead_or_set_p (link->insn, x))
1699 break;
1700 if (!link)
1702 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1703 rsp->sign_bit_copies = 1;
1704 return;
1708 /* If this is a complex assignment, see if we can convert it into a
1709 simple assignment. */
1710 set = expand_field_assignment (set);
1712 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1713 set what we know about X. */
1715 if (SET_DEST (set) == x
1716 || (paradoxical_subreg_p (SET_DEST (set))
1717 && SUBREG_REG (SET_DEST (set)) == x))
1719 rtx src = SET_SRC (set);
1721 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1722 /* If X is narrower than a word and SRC is a non-negative
1723 constant that would appear negative in the mode of X,
1724 sign-extend it for use in reg_stat[].nonzero_bits because some
1725 machines (maybe most) will actually do the sign-extension
1726 and this is the conservative approach.
1728 ??? For 2.5, try to tighten up the MD files in this regard
1729 instead of this kludge. */
1731 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1732 && CONST_INT_P (src)
1733 && INTVAL (src) > 0
1734 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1735 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1736 #endif
1738 /* Don't call nonzero_bits if it cannot change anything. */
1739 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1740 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1741 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1742 if (rsp->sign_bit_copies == 0
1743 || rsp->sign_bit_copies > num)
1744 rsp->sign_bit_copies = num;
1746 else
1748 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1749 rsp->sign_bit_copies = 1;
1754 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1755 optionally insns that were previously combined into I3 or that will be
1756 combined into the merger of INSN and I3. The order is PRED, PRED2,
1757 INSN, SUCC, SUCC2, I3.
1759 Return 0 if the combination is not allowed for any reason.
1761 If the combination is allowed, *PDEST will be set to the single
1762 destination of INSN and *PSRC to the single source, and this function
1763 will return 1. */
1765 static int
1766 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1767 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1768 rtx *pdest, rtx *psrc)
1770 int i;
1771 const_rtx set = 0;
1772 rtx src, dest;
1773 rtx_insn *p;
1774 #ifdef AUTO_INC_DEC
1775 rtx link;
1776 #endif
1777 bool all_adjacent = true;
1778 int (*is_volatile_p) (const_rtx);
1780 if (succ)
1782 if (succ2)
1784 if (next_active_insn (succ2) != i3)
1785 all_adjacent = false;
1786 if (next_active_insn (succ) != succ2)
1787 all_adjacent = false;
1789 else if (next_active_insn (succ) != i3)
1790 all_adjacent = false;
1791 if (next_active_insn (insn) != succ)
1792 all_adjacent = false;
1794 else if (next_active_insn (insn) != i3)
1795 all_adjacent = false;
1797 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1798 or a PARALLEL consisting of such a SET and CLOBBERs.
1800 If INSN has CLOBBER parallel parts, ignore them for our processing.
1801 By definition, these happen during the execution of the insn. When it
1802 is merged with another insn, all bets are off. If they are, in fact,
1803 needed and aren't also supplied in I3, they may be added by
1804 recog_for_combine. Otherwise, it won't match.
1806 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1807 note.
1809 Get the source and destination of INSN. If more than one, can't
1810 combine. */
1812 if (GET_CODE (PATTERN (insn)) == SET)
1813 set = PATTERN (insn);
1814 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1815 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1817 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1819 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1821 switch (GET_CODE (elt))
1823 /* This is important to combine floating point insns
1824 for the SH4 port. */
1825 case USE:
1826 /* Combining an isolated USE doesn't make sense.
1827 We depend here on combinable_i3pat to reject them. */
1828 /* The code below this loop only verifies that the inputs of
1829 the SET in INSN do not change. We call reg_set_between_p
1830 to verify that the REG in the USE does not change between
1831 I3 and INSN.
1832 If the USE in INSN was for a pseudo register, the matching
1833 insn pattern will likely match any register; combining this
1834 with any other USE would only be safe if we knew that the
1835 used registers have identical values, or if there was
1836 something to tell them apart, e.g. different modes. For
1837 now, we forgo such complicated tests and simply disallow
1838 combining of USES of pseudo registers with any other USE. */
1839 if (REG_P (XEXP (elt, 0))
1840 && GET_CODE (PATTERN (i3)) == PARALLEL)
1842 rtx i3pat = PATTERN (i3);
1843 int i = XVECLEN (i3pat, 0) - 1;
1844 unsigned int regno = REGNO (XEXP (elt, 0));
1848 rtx i3elt = XVECEXP (i3pat, 0, i);
1850 if (GET_CODE (i3elt) == USE
1851 && REG_P (XEXP (i3elt, 0))
1852 && (REGNO (XEXP (i3elt, 0)) == regno
1853 ? reg_set_between_p (XEXP (elt, 0),
1854 PREV_INSN (insn), i3)
1855 : regno >= FIRST_PSEUDO_REGISTER))
1856 return 0;
1858 while (--i >= 0);
1860 break;
1862 /* We can ignore CLOBBERs. */
1863 case CLOBBER:
1864 break;
1866 case SET:
1867 /* Ignore SETs whose result isn't used but not those that
1868 have side-effects. */
1869 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1870 && insn_nothrow_p (insn)
1871 && !side_effects_p (elt))
1872 break;
1874 /* If we have already found a SET, this is a second one and
1875 so we cannot combine with this insn. */
1876 if (set)
1877 return 0;
1879 set = elt;
1880 break;
1882 default:
1883 /* Anything else means we can't combine. */
1884 return 0;
1888 if (set == 0
1889 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1890 so don't do anything with it. */
1891 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1892 return 0;
1894 else
1895 return 0;
1897 if (set == 0)
1898 return 0;
1900 /* The simplification in expand_field_assignment may call back to
1901 get_last_value, so set safe guard here. */
1902 subst_low_luid = DF_INSN_LUID (insn);
1904 set = expand_field_assignment (set);
1905 src = SET_SRC (set), dest = SET_DEST (set);
1907 /* Don't eliminate a store in the stack pointer. */
1908 if (dest == stack_pointer_rtx
1909 /* Don't combine with an insn that sets a register to itself if it has
1910 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1911 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1912 /* Can't merge an ASM_OPERANDS. */
1913 || GET_CODE (src) == ASM_OPERANDS
1914 /* Can't merge a function call. */
1915 || GET_CODE (src) == CALL
1916 /* Don't eliminate a function call argument. */
1917 || (CALL_P (i3)
1918 && (find_reg_fusage (i3, USE, dest)
1919 || (REG_P (dest)
1920 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1921 && global_regs[REGNO (dest)])))
1922 /* Don't substitute into an incremented register. */
1923 || FIND_REG_INC_NOTE (i3, dest)
1924 || (succ && FIND_REG_INC_NOTE (succ, dest))
1925 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1926 /* Don't substitute into a non-local goto, this confuses CFG. */
1927 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1928 /* Make sure that DEST is not used after SUCC but before I3. */
1929 || (!all_adjacent
1930 && ((succ2
1931 && (reg_used_between_p (dest, succ2, i3)
1932 || reg_used_between_p (dest, succ, succ2)))
1933 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1934 /* Make sure that the value that is to be substituted for the register
1935 does not use any registers whose values alter in between. However,
1936 If the insns are adjacent, a use can't cross a set even though we
1937 think it might (this can happen for a sequence of insns each setting
1938 the same destination; last_set of that register might point to
1939 a NOTE). If INSN has a REG_EQUIV note, the register is always
1940 equivalent to the memory so the substitution is valid even if there
1941 are intervening stores. Also, don't move a volatile asm or
1942 UNSPEC_VOLATILE across any other insns. */
1943 || (! all_adjacent
1944 && (((!MEM_P (src)
1945 || ! find_reg_note (insn, REG_EQUIV, src))
1946 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1947 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1948 || GET_CODE (src) == UNSPEC_VOLATILE))
1949 /* Don't combine across a CALL_INSN, because that would possibly
1950 change whether the life span of some REGs crosses calls or not,
1951 and it is a pain to update that information.
1952 Exception: if source is a constant, moving it later can't hurt.
1953 Accept that as a special case. */
1954 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1955 return 0;
1957 /* DEST must either be a REG or CC0. */
1958 if (REG_P (dest))
1960 /* If register alignment is being enforced for multi-word items in all
1961 cases except for parameters, it is possible to have a register copy
1962 insn referencing a hard register that is not allowed to contain the
1963 mode being copied and which would not be valid as an operand of most
1964 insns. Eliminate this problem by not combining with such an insn.
1966 Also, on some machines we don't want to extend the life of a hard
1967 register. */
1969 if (REG_P (src)
1970 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1971 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1972 /* Don't extend the life of a hard register unless it is
1973 user variable (if we have few registers) or it can't
1974 fit into the desired register (meaning something special
1975 is going on).
1976 Also avoid substituting a return register into I3, because
1977 reload can't handle a conflict with constraints of other
1978 inputs. */
1979 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1980 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1981 return 0;
1983 else if (GET_CODE (dest) != CC0)
1984 return 0;
1987 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1988 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1989 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1991 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1993 /* If the clobber represents an earlyclobber operand, we must not
1994 substitute an expression containing the clobbered register.
1995 As we do not analyze the constraint strings here, we have to
1996 make the conservative assumption. However, if the register is
1997 a fixed hard reg, the clobber cannot represent any operand;
1998 we leave it up to the machine description to either accept or
1999 reject use-and-clobber patterns. */
2000 if (!REG_P (reg)
2001 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2002 || !fixed_regs[REGNO (reg)])
2003 if (reg_overlap_mentioned_p (reg, src))
2004 return 0;
2007 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2008 or not), reject, unless nothing volatile comes between it and I3 */
2010 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2012 /* Make sure neither succ nor succ2 contains a volatile reference. */
2013 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2014 return 0;
2015 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2016 return 0;
2017 /* We'll check insns between INSN and I3 below. */
2020 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2021 to be an explicit register variable, and was chosen for a reason. */
2023 if (GET_CODE (src) == ASM_OPERANDS
2024 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2025 return 0;
2027 /* If INSN contains volatile references (specifically volatile MEMs),
2028 we cannot combine across any other volatile references.
2029 Even if INSN doesn't contain volatile references, any intervening
2030 volatile insn might affect machine state. */
2032 is_volatile_p = volatile_refs_p (PATTERN (insn))
2033 ? volatile_refs_p
2034 : volatile_insn_p;
2036 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2037 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2038 return 0;
2040 /* If INSN contains an autoincrement or autodecrement, make sure that
2041 register is not used between there and I3, and not already used in
2042 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2043 Also insist that I3 not be a jump; if it were one
2044 and the incremented register were spilled, we would lose. */
2046 #ifdef AUTO_INC_DEC
2047 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2048 if (REG_NOTE_KIND (link) == REG_INC
2049 && (JUMP_P (i3)
2050 || reg_used_between_p (XEXP (link, 0), insn, i3)
2051 || (pred != NULL_RTX
2052 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2053 || (pred2 != NULL_RTX
2054 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2055 || (succ != NULL_RTX
2056 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2057 || (succ2 != NULL_RTX
2058 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2059 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2060 return 0;
2061 #endif
2063 #ifdef HAVE_cc0
2064 /* Don't combine an insn that follows a CC0-setting insn.
2065 An insn that uses CC0 must not be separated from the one that sets it.
2066 We do, however, allow I2 to follow a CC0-setting insn if that insn
2067 is passed as I1; in that case it will be deleted also.
2068 We also allow combining in this case if all the insns are adjacent
2069 because that would leave the two CC0 insns adjacent as well.
2070 It would be more logical to test whether CC0 occurs inside I1 or I2,
2071 but that would be much slower, and this ought to be equivalent. */
2073 p = prev_nonnote_insn (insn);
2074 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2075 && ! all_adjacent)
2076 return 0;
2077 #endif
2079 /* If we get here, we have passed all the tests and the combination is
2080 to be allowed. */
2082 *pdest = dest;
2083 *psrc = src;
2085 return 1;
2088 /* LOC is the location within I3 that contains its pattern or the component
2089 of a PARALLEL of the pattern. We validate that it is valid for combining.
2091 One problem is if I3 modifies its output, as opposed to replacing it
2092 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2093 doing so would produce an insn that is not equivalent to the original insns.
2095 Consider:
2097 (set (reg:DI 101) (reg:DI 100))
2098 (set (subreg:SI (reg:DI 101) 0) <foo>)
2100 This is NOT equivalent to:
2102 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2103 (set (reg:DI 101) (reg:DI 100))])
2105 Not only does this modify 100 (in which case it might still be valid
2106 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2108 We can also run into a problem if I2 sets a register that I1
2109 uses and I1 gets directly substituted into I3 (not via I2). In that
2110 case, we would be getting the wrong value of I2DEST into I3, so we
2111 must reject the combination. This case occurs when I2 and I1 both
2112 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2113 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2114 of a SET must prevent combination from occurring. The same situation
2115 can occur for I0, in which case I0_NOT_IN_SRC is set.
2117 Before doing the above check, we first try to expand a field assignment
2118 into a set of logical operations.
2120 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2121 we place a register that is both set and used within I3. If more than one
2122 such register is detected, we fail.
2124 Return 1 if the combination is valid, zero otherwise. */
2126 static int
2127 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2128 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2130 rtx x = *loc;
2132 if (GET_CODE (x) == SET)
2134 rtx set = x ;
2135 rtx dest = SET_DEST (set);
2136 rtx src = SET_SRC (set);
2137 rtx inner_dest = dest;
2138 rtx subdest;
2140 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2141 || GET_CODE (inner_dest) == SUBREG
2142 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2143 inner_dest = XEXP (inner_dest, 0);
2145 /* Check for the case where I3 modifies its output, as discussed
2146 above. We don't want to prevent pseudos from being combined
2147 into the address of a MEM, so only prevent the combination if
2148 i1 or i2 set the same MEM. */
2149 if ((inner_dest != dest &&
2150 (!MEM_P (inner_dest)
2151 || rtx_equal_p (i2dest, inner_dest)
2152 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2153 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2154 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2155 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2156 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2158 /* This is the same test done in can_combine_p except we can't test
2159 all_adjacent; we don't have to, since this instruction will stay
2160 in place, thus we are not considering increasing the lifetime of
2161 INNER_DEST.
2163 Also, if this insn sets a function argument, combining it with
2164 something that might need a spill could clobber a previous
2165 function argument; the all_adjacent test in can_combine_p also
2166 checks this; here, we do a more specific test for this case. */
2168 || (REG_P (inner_dest)
2169 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2170 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2171 GET_MODE (inner_dest))))
2172 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2173 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2174 return 0;
2176 /* If DEST is used in I3, it is being killed in this insn, so
2177 record that for later. We have to consider paradoxical
2178 subregs here, since they kill the whole register, but we
2179 ignore partial subregs, STRICT_LOW_PART, etc.
2180 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2181 STACK_POINTER_REGNUM, since these are always considered to be
2182 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2183 subdest = dest;
2184 if (GET_CODE (subdest) == SUBREG
2185 && (GET_MODE_SIZE (GET_MODE (subdest))
2186 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2187 subdest = SUBREG_REG (subdest);
2188 if (pi3dest_killed
2189 && REG_P (subdest)
2190 && reg_referenced_p (subdest, PATTERN (i3))
2191 && REGNO (subdest) != FRAME_POINTER_REGNUM
2192 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2193 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2194 #endif
2195 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2196 && (REGNO (subdest) != ARG_POINTER_REGNUM
2197 || ! fixed_regs [REGNO (subdest)])
2198 #endif
2199 && REGNO (subdest) != STACK_POINTER_REGNUM)
2201 if (*pi3dest_killed)
2202 return 0;
2204 *pi3dest_killed = subdest;
2208 else if (GET_CODE (x) == PARALLEL)
2210 int i;
2212 for (i = 0; i < XVECLEN (x, 0); i++)
2213 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2214 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2215 return 0;
2218 return 1;
2221 /* Return 1 if X is an arithmetic expression that contains a multiplication
2222 and division. We don't count multiplications by powers of two here. */
2224 static int
2225 contains_muldiv (rtx x)
2227 switch (GET_CODE (x))
2229 case MOD: case DIV: case UMOD: case UDIV:
2230 return 1;
2232 case MULT:
2233 return ! (CONST_INT_P (XEXP (x, 1))
2234 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2235 default:
2236 if (BINARY_P (x))
2237 return contains_muldiv (XEXP (x, 0))
2238 || contains_muldiv (XEXP (x, 1));
2240 if (UNARY_P (x))
2241 return contains_muldiv (XEXP (x, 0));
2243 return 0;
2247 /* Determine whether INSN can be used in a combination. Return nonzero if
2248 not. This is used in try_combine to detect early some cases where we
2249 can't perform combinations. */
2251 static int
2252 cant_combine_insn_p (rtx_insn *insn)
2254 rtx set;
2255 rtx src, dest;
2257 /* If this isn't really an insn, we can't do anything.
2258 This can occur when flow deletes an insn that it has merged into an
2259 auto-increment address. */
2260 if (! INSN_P (insn))
2261 return 1;
2263 /* Never combine loads and stores involving hard regs that are likely
2264 to be spilled. The register allocator can usually handle such
2265 reg-reg moves by tying. If we allow the combiner to make
2266 substitutions of likely-spilled regs, reload might die.
2267 As an exception, we allow combinations involving fixed regs; these are
2268 not available to the register allocator so there's no risk involved. */
2270 set = single_set (insn);
2271 if (! set)
2272 return 0;
2273 src = SET_SRC (set);
2274 dest = SET_DEST (set);
2275 if (GET_CODE (src) == SUBREG)
2276 src = SUBREG_REG (src);
2277 if (GET_CODE (dest) == SUBREG)
2278 dest = SUBREG_REG (dest);
2279 if (REG_P (src) && REG_P (dest)
2280 && ((HARD_REGISTER_P (src)
2281 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2282 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2283 || (HARD_REGISTER_P (dest)
2284 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2285 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2286 return 1;
2288 return 0;
2291 struct likely_spilled_retval_info
2293 unsigned regno, nregs;
2294 unsigned mask;
2297 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2298 hard registers that are known to be written to / clobbered in full. */
2299 static void
2300 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2302 struct likely_spilled_retval_info *const info =
2303 (struct likely_spilled_retval_info *) data;
2304 unsigned regno, nregs;
2305 unsigned new_mask;
2307 if (!REG_P (XEXP (set, 0)))
2308 return;
2309 regno = REGNO (x);
2310 if (regno >= info->regno + info->nregs)
2311 return;
2312 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2313 if (regno + nregs <= info->regno)
2314 return;
2315 new_mask = (2U << (nregs - 1)) - 1;
2316 if (regno < info->regno)
2317 new_mask >>= info->regno - regno;
2318 else
2319 new_mask <<= regno - info->regno;
2320 info->mask &= ~new_mask;
2323 /* Return nonzero iff part of the return value is live during INSN, and
2324 it is likely spilled. This can happen when more than one insn is needed
2325 to copy the return value, e.g. when we consider to combine into the
2326 second copy insn for a complex value. */
2328 static int
2329 likely_spilled_retval_p (rtx_insn *insn)
2331 rtx_insn *use = BB_END (this_basic_block);
2332 rtx reg;
2333 rtx_insn *p;
2334 unsigned regno, nregs;
2335 /* We assume here that no machine mode needs more than
2336 32 hard registers when the value overlaps with a register
2337 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2338 unsigned mask;
2339 struct likely_spilled_retval_info info;
2341 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2342 return 0;
2343 reg = XEXP (PATTERN (use), 0);
2344 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2345 return 0;
2346 regno = REGNO (reg);
2347 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2348 if (nregs == 1)
2349 return 0;
2350 mask = (2U << (nregs - 1)) - 1;
2352 /* Disregard parts of the return value that are set later. */
2353 info.regno = regno;
2354 info.nregs = nregs;
2355 info.mask = mask;
2356 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2357 if (INSN_P (p))
2358 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2359 mask = info.mask;
2361 /* Check if any of the (probably) live return value registers is
2362 likely spilled. */
2363 nregs --;
2366 if ((mask & 1 << nregs)
2367 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2368 return 1;
2369 } while (nregs--);
2370 return 0;
2373 /* Adjust INSN after we made a change to its destination.
2375 Changing the destination can invalidate notes that say something about
2376 the results of the insn and a LOG_LINK pointing to the insn. */
2378 static void
2379 adjust_for_new_dest (rtx_insn *insn)
2381 /* For notes, be conservative and simply remove them. */
2382 remove_reg_equal_equiv_notes (insn);
2384 /* The new insn will have a destination that was previously the destination
2385 of an insn just above it. Call distribute_links to make a LOG_LINK from
2386 the next use of that destination. */
2388 rtx set = single_set (insn);
2389 gcc_assert (set);
2391 rtx reg = SET_DEST (set);
2393 while (GET_CODE (reg) == ZERO_EXTRACT
2394 || GET_CODE (reg) == STRICT_LOW_PART
2395 || GET_CODE (reg) == SUBREG)
2396 reg = XEXP (reg, 0);
2397 gcc_assert (REG_P (reg));
2399 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2401 df_insn_rescan (insn);
2404 /* Return TRUE if combine can reuse reg X in mode MODE.
2405 ADDED_SETS is nonzero if the original set is still required. */
2406 static bool
2407 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2409 unsigned int regno;
2411 if (!REG_P (x))
2412 return false;
2414 regno = REGNO (x);
2415 /* Allow hard registers if the new mode is legal, and occupies no more
2416 registers than the old mode. */
2417 if (regno < FIRST_PSEUDO_REGISTER)
2418 return (HARD_REGNO_MODE_OK (regno, mode)
2419 && (hard_regno_nregs[regno][GET_MODE (x)]
2420 >= hard_regno_nregs[regno][mode]));
2422 /* Or a pseudo that is only used once. */
2423 return (REG_N_SETS (regno) == 1 && !added_sets
2424 && !REG_USERVAR_P (x));
2428 /* Check whether X, the destination of a set, refers to part of
2429 the register specified by REG. */
2431 static bool
2432 reg_subword_p (rtx x, rtx reg)
2434 /* Check that reg is an integer mode register. */
2435 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2436 return false;
2438 if (GET_CODE (x) == STRICT_LOW_PART
2439 || GET_CODE (x) == ZERO_EXTRACT)
2440 x = XEXP (x, 0);
2442 return GET_CODE (x) == SUBREG
2443 && SUBREG_REG (x) == reg
2444 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2447 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2448 Note that the INSN should be deleted *after* removing dead edges, so
2449 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2450 but not for a (set (pc) (label_ref FOO)). */
2452 static void
2453 update_cfg_for_uncondjump (rtx_insn *insn)
2455 basic_block bb = BLOCK_FOR_INSN (insn);
2456 gcc_assert (BB_END (bb) == insn);
2458 purge_dead_edges (bb);
2460 delete_insn (insn);
2461 if (EDGE_COUNT (bb->succs) == 1)
2463 rtx_insn *insn;
2465 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2467 /* Remove barriers from the footer if there are any. */
2468 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2469 if (BARRIER_P (insn))
2471 if (PREV_INSN (insn))
2472 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2473 else
2474 BB_FOOTER (bb) = NEXT_INSN (insn);
2475 if (NEXT_INSN (insn))
2476 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2478 else if (LABEL_P (insn))
2479 break;
2483 #ifndef HAVE_cc0
2484 /* Return whether INSN is a PARALLEL of exactly N register SETs followed
2485 by an arbitrary number of CLOBBERs. */
2486 static bool
2487 is_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2489 rtx pat = PATTERN (insn);
2491 if (GET_CODE (pat) != PARALLEL)
2492 return false;
2494 int len = XVECLEN (pat, 0);
2495 if (len < n)
2496 return false;
2498 int i;
2499 for (i = 0; i < n; i++)
2500 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2501 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2502 return false;
2503 for ( ; i < len; i++)
2504 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2505 return false;
2507 return true;
2510 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2511 CLOBBERs), can be split into individual SETs in that order, without
2512 changing semantics. */
2513 static bool
2514 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2516 if (!insn_nothrow_p (insn))
2517 return false;
2519 rtx pat = PATTERN (insn);
2521 int i, j;
2522 for (i = 0; i < n; i++)
2524 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2525 return false;
2527 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2529 for (j = i + 1; j < n; j++)
2530 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2531 return false;
2534 return true;
2536 #endif
2538 /* Try to combine the insns I0, I1 and I2 into I3.
2539 Here I0, I1 and I2 appear earlier than I3.
2540 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2543 If we are combining more than two insns and the resulting insn is not
2544 recognized, try splitting it into two insns. If that happens, I2 and I3
2545 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2546 Otherwise, I0, I1 and I2 are pseudo-deleted.
2548 Return 0 if the combination does not work. Then nothing is changed.
2549 If we did the combination, return the insn at which combine should
2550 resume scanning.
2552 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2553 new direct jump instruction.
2555 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2556 been I3 passed to an earlier try_combine within the same basic
2557 block. */
2559 static rtx_insn *
2560 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2561 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2563 /* New patterns for I3 and I2, respectively. */
2564 rtx newpat, newi2pat = 0;
2565 rtvec newpat_vec_with_clobbers = 0;
2566 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2567 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2568 dead. */
2569 int added_sets_0, added_sets_1, added_sets_2;
2570 /* Total number of SETs to put into I3. */
2571 int total_sets;
2572 /* Nonzero if I2's or I1's body now appears in I3. */
2573 int i2_is_used = 0, i1_is_used = 0;
2574 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2575 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2576 /* Contains I3 if the destination of I3 is used in its source, which means
2577 that the old life of I3 is being killed. If that usage is placed into
2578 I2 and not in I3, a REG_DEAD note must be made. */
2579 rtx i3dest_killed = 0;
2580 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2581 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2582 /* Copy of SET_SRC of I1 and I0, if needed. */
2583 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2584 /* Set if I2DEST was reused as a scratch register. */
2585 bool i2scratch = false;
2586 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2587 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2588 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2589 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2590 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2591 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2592 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2593 /* Notes that must be added to REG_NOTES in I3 and I2. */
2594 rtx new_i3_notes, new_i2_notes;
2595 /* Notes that we substituted I3 into I2 instead of the normal case. */
2596 int i3_subst_into_i2 = 0;
2597 /* Notes that I1, I2 or I3 is a MULT operation. */
2598 int have_mult = 0;
2599 int swap_i2i3 = 0;
2600 int changed_i3_dest = 0;
2602 int maxreg;
2603 rtx_insn *temp_insn;
2604 rtx temp_expr;
2605 struct insn_link *link;
2606 rtx other_pat = 0;
2607 rtx new_other_notes;
2608 int i;
2610 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2611 never be). */
2612 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2613 return 0;
2615 /* Only try four-insn combinations when there's high likelihood of
2616 success. Look for simple insns, such as loads of constants or
2617 binary operations involving a constant. */
2618 if (i0)
2620 int i;
2621 int ngood = 0;
2622 int nshift = 0;
2624 if (!flag_expensive_optimizations)
2625 return 0;
2627 for (i = 0; i < 4; i++)
2629 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2630 rtx set = single_set (insn);
2631 rtx src;
2632 if (!set)
2633 continue;
2634 src = SET_SRC (set);
2635 if (CONSTANT_P (src))
2637 ngood += 2;
2638 break;
2640 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2641 ngood++;
2642 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2643 || GET_CODE (src) == LSHIFTRT)
2644 nshift++;
2646 if (ngood < 2 && nshift < 2)
2647 return 0;
2650 /* Exit early if one of the insns involved can't be used for
2651 combinations. */
2652 if (CALL_P (i2)
2653 || (i1 && CALL_P (i1))
2654 || (i0 && CALL_P (i0))
2655 || cant_combine_insn_p (i3)
2656 || cant_combine_insn_p (i2)
2657 || (i1 && cant_combine_insn_p (i1))
2658 || (i0 && cant_combine_insn_p (i0))
2659 || likely_spilled_retval_p (i3))
2660 return 0;
2662 combine_attempts++;
2663 undobuf.other_insn = 0;
2665 /* Reset the hard register usage information. */
2666 CLEAR_HARD_REG_SET (newpat_used_regs);
2668 if (dump_file && (dump_flags & TDF_DETAILS))
2670 if (i0)
2671 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2672 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2673 else if (i1)
2674 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2675 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2676 else
2677 fprintf (dump_file, "\nTrying %d -> %d:\n",
2678 INSN_UID (i2), INSN_UID (i3));
2681 /* If multiple insns feed into one of I2 or I3, they can be in any
2682 order. To simplify the code below, reorder them in sequence. */
2683 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2684 temp_insn = i2, i2 = i0, i0 = temp_insn;
2685 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2686 temp_insn = i1, i1 = i0, i0 = temp_insn;
2687 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2688 temp_insn = i1, i1 = i2, i2 = temp_insn;
2690 added_links_insn = 0;
2692 /* First check for one important special case that the code below will
2693 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2694 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2695 we may be able to replace that destination with the destination of I3.
2696 This occurs in the common code where we compute both a quotient and
2697 remainder into a structure, in which case we want to do the computation
2698 directly into the structure to avoid register-register copies.
2700 Note that this case handles both multiple sets in I2 and also cases
2701 where I2 has a number of CLOBBERs inside the PARALLEL.
2703 We make very conservative checks below and only try to handle the
2704 most common cases of this. For example, we only handle the case
2705 where I2 and I3 are adjacent to avoid making difficult register
2706 usage tests. */
2708 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2709 && REG_P (SET_SRC (PATTERN (i3)))
2710 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2711 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2712 && GET_CODE (PATTERN (i2)) == PARALLEL
2713 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2714 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2715 below would need to check what is inside (and reg_overlap_mentioned_p
2716 doesn't support those codes anyway). Don't allow those destinations;
2717 the resulting insn isn't likely to be recognized anyway. */
2718 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2719 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2720 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2721 SET_DEST (PATTERN (i3)))
2722 && next_active_insn (i2) == i3)
2724 rtx p2 = PATTERN (i2);
2726 /* Make sure that the destination of I3,
2727 which we are going to substitute into one output of I2,
2728 is not used within another output of I2. We must avoid making this:
2729 (parallel [(set (mem (reg 69)) ...)
2730 (set (reg 69) ...)])
2731 which is not well-defined as to order of actions.
2732 (Besides, reload can't handle output reloads for this.)
2734 The problem can also happen if the dest of I3 is a memory ref,
2735 if another dest in I2 is an indirect memory ref. */
2736 for (i = 0; i < XVECLEN (p2, 0); i++)
2737 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2738 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2739 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2740 SET_DEST (XVECEXP (p2, 0, i))))
2741 break;
2743 /* Make sure this PARALLEL is not an asm. We do not allow combining
2744 that usually (see can_combine_p), so do not here either. */
2745 for (i = 0; i < XVECLEN (p2, 0); i++)
2746 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2747 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2748 break;
2750 if (i == XVECLEN (p2, 0))
2751 for (i = 0; i < XVECLEN (p2, 0); i++)
2752 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2753 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2755 combine_merges++;
2757 subst_insn = i3;
2758 subst_low_luid = DF_INSN_LUID (i2);
2760 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2761 i2src = SET_SRC (XVECEXP (p2, 0, i));
2762 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2763 i2dest_killed = dead_or_set_p (i2, i2dest);
2765 /* Replace the dest in I2 with our dest and make the resulting
2766 insn the new pattern for I3. Then skip to where we validate
2767 the pattern. Everything was set up above. */
2768 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2769 newpat = p2;
2770 i3_subst_into_i2 = 1;
2771 goto validate_replacement;
2775 /* If I2 is setting a pseudo to a constant and I3 is setting some
2776 sub-part of it to another constant, merge them by making a new
2777 constant. */
2778 if (i1 == 0
2779 && (temp_expr = single_set (i2)) != 0
2780 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2781 && GET_CODE (PATTERN (i3)) == SET
2782 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2783 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2785 rtx dest = SET_DEST (PATTERN (i3));
2786 int offset = -1;
2787 int width = 0;
2789 if (GET_CODE (dest) == ZERO_EXTRACT)
2791 if (CONST_INT_P (XEXP (dest, 1))
2792 && CONST_INT_P (XEXP (dest, 2)))
2794 width = INTVAL (XEXP (dest, 1));
2795 offset = INTVAL (XEXP (dest, 2));
2796 dest = XEXP (dest, 0);
2797 if (BITS_BIG_ENDIAN)
2798 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2801 else
2803 if (GET_CODE (dest) == STRICT_LOW_PART)
2804 dest = XEXP (dest, 0);
2805 width = GET_MODE_PRECISION (GET_MODE (dest));
2806 offset = 0;
2809 if (offset >= 0)
2811 /* If this is the low part, we're done. */
2812 if (subreg_lowpart_p (dest))
2814 /* Handle the case where inner is twice the size of outer. */
2815 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2816 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2817 offset += GET_MODE_PRECISION (GET_MODE (dest));
2818 /* Otherwise give up for now. */
2819 else
2820 offset = -1;
2823 if (offset >= 0)
2825 rtx inner = SET_SRC (PATTERN (i3));
2826 rtx outer = SET_SRC (temp_expr);
2828 wide_int o
2829 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2830 std::make_pair (inner, GET_MODE (dest)),
2831 offset, width);
2833 combine_merges++;
2834 subst_insn = i3;
2835 subst_low_luid = DF_INSN_LUID (i2);
2836 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2837 i2dest = SET_DEST (temp_expr);
2838 i2dest_killed = dead_or_set_p (i2, i2dest);
2840 /* Replace the source in I2 with the new constant and make the
2841 resulting insn the new pattern for I3. Then skip to where we
2842 validate the pattern. Everything was set up above. */
2843 SUBST (SET_SRC (temp_expr),
2844 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2846 newpat = PATTERN (i2);
2848 /* The dest of I3 has been replaced with the dest of I2. */
2849 changed_i3_dest = 1;
2850 goto validate_replacement;
2854 #ifndef HAVE_cc0
2855 /* If we have no I1 and I2 looks like:
2856 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2857 (set Y OP)])
2858 make up a dummy I1 that is
2859 (set Y OP)
2860 and change I2 to be
2861 (set (reg:CC X) (compare:CC Y (const_int 0)))
2863 (We can ignore any trailing CLOBBERs.)
2865 This undoes a previous combination and allows us to match a branch-and-
2866 decrement insn. */
2868 if (i1 == 0
2869 && is_parallel_of_n_reg_sets (i2, 2)
2870 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2871 == MODE_CC)
2872 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2873 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2874 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2875 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2876 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2877 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2879 /* We make I1 with the same INSN_UID as I2. This gives it
2880 the same DF_INSN_LUID for value tracking. Our fake I1 will
2881 never appear in the insn stream so giving it the same INSN_UID
2882 as I2 will not cause a problem. */
2884 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2885 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2886 -1, NULL_RTX);
2887 INSN_UID (i1) = INSN_UID (i2);
2889 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2890 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2891 SET_DEST (PATTERN (i1)));
2892 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2893 SUBST_LINK (LOG_LINKS (i2),
2894 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2897 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2898 make those two SETs separate I1 and I2 insns, and make an I0 that is
2899 the original I1. */
2900 if (i0 == 0
2901 && is_parallel_of_n_reg_sets (i2, 2)
2902 && can_split_parallel_of_n_reg_sets (i2, 2)
2903 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2904 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2906 /* If there is no I1, there is no I0 either. */
2907 i0 = i1;
2909 /* We make I1 with the same INSN_UID as I2. This gives it
2910 the same DF_INSN_LUID for value tracking. Our fake I1 will
2911 never appear in the insn stream so giving it the same INSN_UID
2912 as I2 will not cause a problem. */
2914 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2915 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2916 -1, NULL_RTX);
2917 INSN_UID (i1) = INSN_UID (i2);
2919 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2921 #endif
2923 /* Verify that I2 and I1 are valid for combining. */
2924 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2925 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2926 &i1dest, &i1src))
2927 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2928 &i0dest, &i0src)))
2930 undo_all ();
2931 return 0;
2934 /* Record whether I2DEST is used in I2SRC and similarly for the other
2935 cases. Knowing this will help in register status updating below. */
2936 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2937 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2938 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2939 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2940 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2941 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2942 i2dest_killed = dead_or_set_p (i2, i2dest);
2943 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2944 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2946 /* For the earlier insns, determine which of the subsequent ones they
2947 feed. */
2948 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2949 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2950 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2951 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2952 && reg_overlap_mentioned_p (i0dest, i2src))));
2954 /* Ensure that I3's pattern can be the destination of combines. */
2955 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2956 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2957 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2958 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2959 &i3dest_killed))
2961 undo_all ();
2962 return 0;
2965 /* See if any of the insns is a MULT operation. Unless one is, we will
2966 reject a combination that is, since it must be slower. Be conservative
2967 here. */
2968 if (GET_CODE (i2src) == MULT
2969 || (i1 != 0 && GET_CODE (i1src) == MULT)
2970 || (i0 != 0 && GET_CODE (i0src) == MULT)
2971 || (GET_CODE (PATTERN (i3)) == SET
2972 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2973 have_mult = 1;
2975 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2976 We used to do this EXCEPT in one case: I3 has a post-inc in an
2977 output operand. However, that exception can give rise to insns like
2978 mov r3,(r3)+
2979 which is a famous insn on the PDP-11 where the value of r3 used as the
2980 source was model-dependent. Avoid this sort of thing. */
2982 #if 0
2983 if (!(GET_CODE (PATTERN (i3)) == SET
2984 && REG_P (SET_SRC (PATTERN (i3)))
2985 && MEM_P (SET_DEST (PATTERN (i3)))
2986 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2987 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2988 /* It's not the exception. */
2989 #endif
2990 #ifdef AUTO_INC_DEC
2992 rtx link;
2993 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2994 if (REG_NOTE_KIND (link) == REG_INC
2995 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2996 || (i1 != 0
2997 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2999 undo_all ();
3000 return 0;
3003 #endif
3005 /* See if the SETs in I1 or I2 need to be kept around in the merged
3006 instruction: whenever the value set there is still needed past I3.
3007 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3009 For the SET in I1, we have two cases: if I1 and I2 independently feed
3010 into I3, the set in I1 needs to be kept around unless I1DEST dies
3011 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3012 in I1 needs to be kept around unless I1DEST dies or is set in either
3013 I2 or I3. The same considerations apply to I0. */
3015 added_sets_2 = !dead_or_set_p (i3, i2dest);
3017 if (i1)
3018 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3019 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3020 else
3021 added_sets_1 = 0;
3023 if (i0)
3024 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3025 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3026 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3027 && dead_or_set_p (i2, i0dest)));
3028 else
3029 added_sets_0 = 0;
3031 /* We are about to copy insns for the case where they need to be kept
3032 around. Check that they can be copied in the merged instruction. */
3034 if (targetm.cannot_copy_insn_p
3035 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3036 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3037 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3039 undo_all ();
3040 return 0;
3043 /* If the set in I2 needs to be kept around, we must make a copy of
3044 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3045 PATTERN (I2), we are only substituting for the original I1DEST, not into
3046 an already-substituted copy. This also prevents making self-referential
3047 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3048 I2DEST. */
3050 if (added_sets_2)
3052 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3053 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3054 else
3055 i2pat = copy_rtx (PATTERN (i2));
3058 if (added_sets_1)
3060 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3061 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3062 else
3063 i1pat = copy_rtx (PATTERN (i1));
3066 if (added_sets_0)
3068 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3069 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3070 else
3071 i0pat = copy_rtx (PATTERN (i0));
3074 combine_merges++;
3076 /* Substitute in the latest insn for the regs set by the earlier ones. */
3078 maxreg = max_reg_num ();
3080 subst_insn = i3;
3082 #ifndef HAVE_cc0
3083 /* Many machines that don't use CC0 have insns that can both perform an
3084 arithmetic operation and set the condition code. These operations will
3085 be represented as a PARALLEL with the first element of the vector
3086 being a COMPARE of an arithmetic operation with the constant zero.
3087 The second element of the vector will set some pseudo to the result
3088 of the same arithmetic operation. If we simplify the COMPARE, we won't
3089 match such a pattern and so will generate an extra insn. Here we test
3090 for this case, where both the comparison and the operation result are
3091 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3092 I2SRC. Later we will make the PARALLEL that contains I2. */
3094 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3095 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3096 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3097 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3099 rtx newpat_dest;
3100 rtx *cc_use_loc = NULL;
3101 rtx_insn *cc_use_insn = NULL;
3102 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3103 machine_mode compare_mode, orig_compare_mode;
3104 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3106 newpat = PATTERN (i3);
3107 newpat_dest = SET_DEST (newpat);
3108 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3110 if (undobuf.other_insn == 0
3111 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3112 &cc_use_insn)))
3114 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3115 compare_code = simplify_compare_const (compare_code,
3116 GET_MODE (i2dest), op0, &op1);
3117 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3120 /* Do the rest only if op1 is const0_rtx, which may be the
3121 result of simplification. */
3122 if (op1 == const0_rtx)
3124 /* If a single use of the CC is found, prepare to modify it
3125 when SELECT_CC_MODE returns a new CC-class mode, or when
3126 the above simplify_compare_const() returned a new comparison
3127 operator. undobuf.other_insn is assigned the CC use insn
3128 when modifying it. */
3129 if (cc_use_loc)
3131 #ifdef SELECT_CC_MODE
3132 machine_mode new_mode
3133 = SELECT_CC_MODE (compare_code, op0, op1);
3134 if (new_mode != orig_compare_mode
3135 && can_change_dest_mode (SET_DEST (newpat),
3136 added_sets_2, new_mode))
3138 unsigned int regno = REGNO (newpat_dest);
3139 compare_mode = new_mode;
3140 if (regno < FIRST_PSEUDO_REGISTER)
3141 newpat_dest = gen_rtx_REG (compare_mode, regno);
3142 else
3144 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3145 newpat_dest = regno_reg_rtx[regno];
3148 #endif
3149 /* Cases for modifying the CC-using comparison. */
3150 if (compare_code != orig_compare_code
3151 /* ??? Do we need to verify the zero rtx? */
3152 && XEXP (*cc_use_loc, 1) == const0_rtx)
3154 /* Replace cc_use_loc with entire new RTX. */
3155 SUBST (*cc_use_loc,
3156 gen_rtx_fmt_ee (compare_code, compare_mode,
3157 newpat_dest, const0_rtx));
3158 undobuf.other_insn = cc_use_insn;
3160 else if (compare_mode != orig_compare_mode)
3162 /* Just replace the CC reg with a new mode. */
3163 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3164 undobuf.other_insn = cc_use_insn;
3168 /* Now we modify the current newpat:
3169 First, SET_DEST(newpat) is updated if the CC mode has been
3170 altered. For targets without SELECT_CC_MODE, this should be
3171 optimized away. */
3172 if (compare_mode != orig_compare_mode)
3173 SUBST (SET_DEST (newpat), newpat_dest);
3174 /* This is always done to propagate i2src into newpat. */
3175 SUBST (SET_SRC (newpat),
3176 gen_rtx_COMPARE (compare_mode, op0, op1));
3177 /* Create new version of i2pat if needed; the below PARALLEL
3178 creation needs this to work correctly. */
3179 if (! rtx_equal_p (i2src, op0))
3180 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3181 i2_is_used = 1;
3184 #endif
3186 if (i2_is_used == 0)
3188 /* It is possible that the source of I2 or I1 may be performing
3189 an unneeded operation, such as a ZERO_EXTEND of something
3190 that is known to have the high part zero. Handle that case
3191 by letting subst look at the inner insns.
3193 Another way to do this would be to have a function that tries
3194 to simplify a single insn instead of merging two or more
3195 insns. We don't do this because of the potential of infinite
3196 loops and because of the potential extra memory required.
3197 However, doing it the way we are is a bit of a kludge and
3198 doesn't catch all cases.
3200 But only do this if -fexpensive-optimizations since it slows
3201 things down and doesn't usually win.
3203 This is not done in the COMPARE case above because the
3204 unmodified I2PAT is used in the PARALLEL and so a pattern
3205 with a modified I2SRC would not match. */
3207 if (flag_expensive_optimizations)
3209 /* Pass pc_rtx so no substitutions are done, just
3210 simplifications. */
3211 if (i1)
3213 subst_low_luid = DF_INSN_LUID (i1);
3214 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3217 subst_low_luid = DF_INSN_LUID (i2);
3218 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3221 n_occurrences = 0; /* `subst' counts here */
3222 subst_low_luid = DF_INSN_LUID (i2);
3224 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3225 copy of I2SRC each time we substitute it, in order to avoid creating
3226 self-referential RTL when we will be substituting I1SRC for I1DEST
3227 later. Likewise if I0 feeds into I2, either directly or indirectly
3228 through I1, and I0DEST is in I0SRC. */
3229 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3230 (i1_feeds_i2_n && i1dest_in_i1src)
3231 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3232 && i0dest_in_i0src));
3233 substed_i2 = 1;
3235 /* Record whether I2's body now appears within I3's body. */
3236 i2_is_used = n_occurrences;
3239 /* If we already got a failure, don't try to do more. Otherwise, try to
3240 substitute I1 if we have it. */
3242 if (i1 && GET_CODE (newpat) != CLOBBER)
3244 /* Check that an autoincrement side-effect on I1 has not been lost.
3245 This happens if I1DEST is mentioned in I2 and dies there, and
3246 has disappeared from the new pattern. */
3247 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3248 && i1_feeds_i2_n
3249 && dead_or_set_p (i2, i1dest)
3250 && !reg_overlap_mentioned_p (i1dest, newpat))
3251 /* Before we can do this substitution, we must redo the test done
3252 above (see detailed comments there) that ensures I1DEST isn't
3253 mentioned in any SETs in NEWPAT that are field assignments. */
3254 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3255 0, 0, 0))
3257 undo_all ();
3258 return 0;
3261 n_occurrences = 0;
3262 subst_low_luid = DF_INSN_LUID (i1);
3264 /* If the following substitution will modify I1SRC, make a copy of it
3265 for the case where it is substituted for I1DEST in I2PAT later. */
3266 if (added_sets_2 && i1_feeds_i2_n)
3267 i1src_copy = copy_rtx (i1src);
3269 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3270 copy of I1SRC each time we substitute it, in order to avoid creating
3271 self-referential RTL when we will be substituting I0SRC for I0DEST
3272 later. */
3273 newpat = subst (newpat, i1dest, i1src, 0, 0,
3274 i0_feeds_i1_n && i0dest_in_i0src);
3275 substed_i1 = 1;
3277 /* Record whether I1's body now appears within I3's body. */
3278 i1_is_used = n_occurrences;
3281 /* Likewise for I0 if we have it. */
3283 if (i0 && GET_CODE (newpat) != CLOBBER)
3285 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3286 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3287 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3288 && !reg_overlap_mentioned_p (i0dest, newpat))
3289 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3290 0, 0, 0))
3292 undo_all ();
3293 return 0;
3296 /* If the following substitution will modify I0SRC, make a copy of it
3297 for the case where it is substituted for I0DEST in I1PAT later. */
3298 if (added_sets_1 && i0_feeds_i1_n)
3299 i0src_copy = copy_rtx (i0src);
3300 /* And a copy for I0DEST in I2PAT substitution. */
3301 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3302 || (i0_feeds_i2_n)))
3303 i0src_copy2 = copy_rtx (i0src);
3305 n_occurrences = 0;
3306 subst_low_luid = DF_INSN_LUID (i0);
3307 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3308 substed_i0 = 1;
3311 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3312 to count all the ways that I2SRC and I1SRC can be used. */
3313 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3314 && i2_is_used + added_sets_2 > 1)
3315 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3316 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3317 > 1))
3318 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3319 && (n_occurrences + added_sets_0
3320 + (added_sets_1 && i0_feeds_i1_n)
3321 + (added_sets_2 && i0_feeds_i2_n)
3322 > 1))
3323 /* Fail if we tried to make a new register. */
3324 || max_reg_num () != maxreg
3325 /* Fail if we couldn't do something and have a CLOBBER. */
3326 || GET_CODE (newpat) == CLOBBER
3327 /* Fail if this new pattern is a MULT and we didn't have one before
3328 at the outer level. */
3329 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3330 && ! have_mult))
3332 undo_all ();
3333 return 0;
3336 /* If the actions of the earlier insns must be kept
3337 in addition to substituting them into the latest one,
3338 we must make a new PARALLEL for the latest insn
3339 to hold additional the SETs. */
3341 if (added_sets_0 || added_sets_1 || added_sets_2)
3343 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3344 combine_extras++;
3346 if (GET_CODE (newpat) == PARALLEL)
3348 rtvec old = XVEC (newpat, 0);
3349 total_sets = XVECLEN (newpat, 0) + extra_sets;
3350 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3351 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3352 sizeof (old->elem[0]) * old->num_elem);
3354 else
3356 rtx old = newpat;
3357 total_sets = 1 + extra_sets;
3358 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3359 XVECEXP (newpat, 0, 0) = old;
3362 if (added_sets_0)
3363 XVECEXP (newpat, 0, --total_sets) = i0pat;
3365 if (added_sets_1)
3367 rtx t = i1pat;
3368 if (i0_feeds_i1_n)
3369 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3371 XVECEXP (newpat, 0, --total_sets) = t;
3373 if (added_sets_2)
3375 rtx t = i2pat;
3376 if (i1_feeds_i2_n)
3377 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3378 i0_feeds_i1_n && i0dest_in_i0src);
3379 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3380 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3382 XVECEXP (newpat, 0, --total_sets) = t;
3386 validate_replacement:
3388 /* Note which hard regs this insn has as inputs. */
3389 mark_used_regs_combine (newpat);
3391 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3392 consider splitting this pattern, we might need these clobbers. */
3393 if (i1 && GET_CODE (newpat) == PARALLEL
3394 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3396 int len = XVECLEN (newpat, 0);
3398 newpat_vec_with_clobbers = rtvec_alloc (len);
3399 for (i = 0; i < len; i++)
3400 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3403 /* We have recognized nothing yet. */
3404 insn_code_number = -1;
3406 /* See if this is a PARALLEL of two SETs where one SET's destination is
3407 a register that is unused and this isn't marked as an instruction that
3408 might trap in an EH region. In that case, we just need the other SET.
3409 We prefer this over the PARALLEL.
3411 This can occur when simplifying a divmod insn. We *must* test for this
3412 case here because the code below that splits two independent SETs doesn't
3413 handle this case correctly when it updates the register status.
3415 It's pointless doing this if we originally had two sets, one from
3416 i3, and one from i2. Combining then splitting the parallel results
3417 in the original i2 again plus an invalid insn (which we delete).
3418 The net effect is only to move instructions around, which makes
3419 debug info less accurate. */
3421 if (!(added_sets_2 && i1 == 0)
3422 && GET_CODE (newpat) == PARALLEL
3423 && XVECLEN (newpat, 0) == 2
3424 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3425 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3426 && asm_noperands (newpat) < 0)
3428 rtx set0 = XVECEXP (newpat, 0, 0);
3429 rtx set1 = XVECEXP (newpat, 0, 1);
3430 rtx oldpat = newpat;
3432 if (((REG_P (SET_DEST (set1))
3433 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3434 || (GET_CODE (SET_DEST (set1)) == SUBREG
3435 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3436 && insn_nothrow_p (i3)
3437 && !side_effects_p (SET_SRC (set1)))
3439 newpat = set0;
3440 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3443 else if (((REG_P (SET_DEST (set0))
3444 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3445 || (GET_CODE (SET_DEST (set0)) == SUBREG
3446 && find_reg_note (i3, REG_UNUSED,
3447 SUBREG_REG (SET_DEST (set0)))))
3448 && insn_nothrow_p (i3)
3449 && !side_effects_p (SET_SRC (set0)))
3451 newpat = set1;
3452 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3454 if (insn_code_number >= 0)
3455 changed_i3_dest = 1;
3458 if (insn_code_number < 0)
3459 newpat = oldpat;
3462 /* Is the result of combination a valid instruction? */
3463 if (insn_code_number < 0)
3464 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3466 /* If we were combining three insns and the result is a simple SET
3467 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3468 insns. There are two ways to do this. It can be split using a
3469 machine-specific method (like when you have an addition of a large
3470 constant) or by combine in the function find_split_point. */
3472 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3473 && asm_noperands (newpat) < 0)
3475 rtx parallel, *split;
3476 rtx_insn *m_split_insn;
3478 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3479 use I2DEST as a scratch register will help. In the latter case,
3480 convert I2DEST to the mode of the source of NEWPAT if we can. */
3482 m_split_insn = combine_split_insns (newpat, i3);
3484 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3485 inputs of NEWPAT. */
3487 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3488 possible to try that as a scratch reg. This would require adding
3489 more code to make it work though. */
3491 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3493 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3495 /* First try to split using the original register as a
3496 scratch register. */
3497 parallel = gen_rtx_PARALLEL (VOIDmode,
3498 gen_rtvec (2, newpat,
3499 gen_rtx_CLOBBER (VOIDmode,
3500 i2dest)));
3501 m_split_insn = combine_split_insns (parallel, i3);
3503 /* If that didn't work, try changing the mode of I2DEST if
3504 we can. */
3505 if (m_split_insn == 0
3506 && new_mode != GET_MODE (i2dest)
3507 && new_mode != VOIDmode
3508 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3510 machine_mode old_mode = GET_MODE (i2dest);
3511 rtx ni2dest;
3513 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3514 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3515 else
3517 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3518 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3521 parallel = (gen_rtx_PARALLEL
3522 (VOIDmode,
3523 gen_rtvec (2, newpat,
3524 gen_rtx_CLOBBER (VOIDmode,
3525 ni2dest))));
3526 m_split_insn = combine_split_insns (parallel, i3);
3528 if (m_split_insn == 0
3529 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3531 struct undo *buf;
3533 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3534 buf = undobuf.undos;
3535 undobuf.undos = buf->next;
3536 buf->next = undobuf.frees;
3537 undobuf.frees = buf;
3541 i2scratch = m_split_insn != 0;
3544 /* If recog_for_combine has discarded clobbers, try to use them
3545 again for the split. */
3546 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3548 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3549 m_split_insn = combine_split_insns (parallel, i3);
3552 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3554 rtx m_split_pat = PATTERN (m_split_insn);
3555 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3556 if (insn_code_number >= 0)
3557 newpat = m_split_pat;
3559 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3560 && (next_nonnote_nondebug_insn (i2) == i3
3561 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3563 rtx i2set, i3set;
3564 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3565 newi2pat = PATTERN (m_split_insn);
3567 i3set = single_set (NEXT_INSN (m_split_insn));
3568 i2set = single_set (m_split_insn);
3570 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3572 /* If I2 or I3 has multiple SETs, we won't know how to track
3573 register status, so don't use these insns. If I2's destination
3574 is used between I2 and I3, we also can't use these insns. */
3576 if (i2_code_number >= 0 && i2set && i3set
3577 && (next_nonnote_nondebug_insn (i2) == i3
3578 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3579 insn_code_number = recog_for_combine (&newi3pat, i3,
3580 &new_i3_notes);
3581 if (insn_code_number >= 0)
3582 newpat = newi3pat;
3584 /* It is possible that both insns now set the destination of I3.
3585 If so, we must show an extra use of it. */
3587 if (insn_code_number >= 0)
3589 rtx new_i3_dest = SET_DEST (i3set);
3590 rtx new_i2_dest = SET_DEST (i2set);
3592 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3593 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3594 || GET_CODE (new_i3_dest) == SUBREG)
3595 new_i3_dest = XEXP (new_i3_dest, 0);
3597 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3598 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3599 || GET_CODE (new_i2_dest) == SUBREG)
3600 new_i2_dest = XEXP (new_i2_dest, 0);
3602 if (REG_P (new_i3_dest)
3603 && REG_P (new_i2_dest)
3604 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3605 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3609 /* If we can split it and use I2DEST, go ahead and see if that
3610 helps things be recognized. Verify that none of the registers
3611 are set between I2 and I3. */
3612 if (insn_code_number < 0
3613 && (split = find_split_point (&newpat, i3, false)) != 0
3614 #ifdef HAVE_cc0
3615 && REG_P (i2dest)
3616 #endif
3617 /* We need I2DEST in the proper mode. If it is a hard register
3618 or the only use of a pseudo, we can change its mode.
3619 Make sure we don't change a hard register to have a mode that
3620 isn't valid for it, or change the number of registers. */
3621 && (GET_MODE (*split) == GET_MODE (i2dest)
3622 || GET_MODE (*split) == VOIDmode
3623 || can_change_dest_mode (i2dest, added_sets_2,
3624 GET_MODE (*split)))
3625 && (next_nonnote_nondebug_insn (i2) == i3
3626 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3627 /* We can't overwrite I2DEST if its value is still used by
3628 NEWPAT. */
3629 && ! reg_referenced_p (i2dest, newpat))
3631 rtx newdest = i2dest;
3632 enum rtx_code split_code = GET_CODE (*split);
3633 machine_mode split_mode = GET_MODE (*split);
3634 bool subst_done = false;
3635 newi2pat = NULL_RTX;
3637 i2scratch = true;
3639 /* *SPLIT may be part of I2SRC, so make sure we have the
3640 original expression around for later debug processing.
3641 We should not need I2SRC any more in other cases. */
3642 if (MAY_HAVE_DEBUG_INSNS)
3643 i2src = copy_rtx (i2src);
3644 else
3645 i2src = NULL;
3647 /* Get NEWDEST as a register in the proper mode. We have already
3648 validated that we can do this. */
3649 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3651 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3652 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3653 else
3655 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3656 newdest = regno_reg_rtx[REGNO (i2dest)];
3660 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3661 an ASHIFT. This can occur if it was inside a PLUS and hence
3662 appeared to be a memory address. This is a kludge. */
3663 if (split_code == MULT
3664 && CONST_INT_P (XEXP (*split, 1))
3665 && INTVAL (XEXP (*split, 1)) > 0
3666 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3668 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3669 XEXP (*split, 0), GEN_INT (i)));
3670 /* Update split_code because we may not have a multiply
3671 anymore. */
3672 split_code = GET_CODE (*split);
3675 #ifdef INSN_SCHEDULING
3676 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3677 be written as a ZERO_EXTEND. */
3678 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3680 #ifdef LOAD_EXTEND_OP
3681 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3682 what it really is. */
3683 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3684 == SIGN_EXTEND)
3685 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3686 SUBREG_REG (*split)));
3687 else
3688 #endif
3689 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3690 SUBREG_REG (*split)));
3692 #endif
3694 /* Attempt to split binary operators using arithmetic identities. */
3695 if (BINARY_P (SET_SRC (newpat))
3696 && split_mode == GET_MODE (SET_SRC (newpat))
3697 && ! side_effects_p (SET_SRC (newpat)))
3699 rtx setsrc = SET_SRC (newpat);
3700 machine_mode mode = GET_MODE (setsrc);
3701 enum rtx_code code = GET_CODE (setsrc);
3702 rtx src_op0 = XEXP (setsrc, 0);
3703 rtx src_op1 = XEXP (setsrc, 1);
3705 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3706 if (rtx_equal_p (src_op0, src_op1))
3708 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3709 SUBST (XEXP (setsrc, 0), newdest);
3710 SUBST (XEXP (setsrc, 1), newdest);
3711 subst_done = true;
3713 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3714 else if ((code == PLUS || code == MULT)
3715 && GET_CODE (src_op0) == code
3716 && GET_CODE (XEXP (src_op0, 0)) == code
3717 && (INTEGRAL_MODE_P (mode)
3718 || (FLOAT_MODE_P (mode)
3719 && flag_unsafe_math_optimizations)))
3721 rtx p = XEXP (XEXP (src_op0, 0), 0);
3722 rtx q = XEXP (XEXP (src_op0, 0), 1);
3723 rtx r = XEXP (src_op0, 1);
3724 rtx s = src_op1;
3726 /* Split both "((X op Y) op X) op Y" and
3727 "((X op Y) op Y) op X" as "T op T" where T is
3728 "X op Y". */
3729 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3730 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3732 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3733 XEXP (src_op0, 0));
3734 SUBST (XEXP (setsrc, 0), newdest);
3735 SUBST (XEXP (setsrc, 1), newdest);
3736 subst_done = true;
3738 /* Split "((X op X) op Y) op Y)" as "T op T" where
3739 T is "X op Y". */
3740 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3742 rtx tmp = simplify_gen_binary (code, mode, p, r);
3743 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3744 SUBST (XEXP (setsrc, 0), newdest);
3745 SUBST (XEXP (setsrc, 1), newdest);
3746 subst_done = true;
3751 if (!subst_done)
3753 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3754 SUBST (*split, newdest);
3757 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3759 /* recog_for_combine might have added CLOBBERs to newi2pat.
3760 Make sure NEWPAT does not depend on the clobbered regs. */
3761 if (GET_CODE (newi2pat) == PARALLEL)
3762 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3763 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3765 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3766 if (reg_overlap_mentioned_p (reg, newpat))
3768 undo_all ();
3769 return 0;
3773 /* If the split point was a MULT and we didn't have one before,
3774 don't use one now. */
3775 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3776 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3780 /* Check for a case where we loaded from memory in a narrow mode and
3781 then sign extended it, but we need both registers. In that case,
3782 we have a PARALLEL with both loads from the same memory location.
3783 We can split this into a load from memory followed by a register-register
3784 copy. This saves at least one insn, more if register allocation can
3785 eliminate the copy.
3787 We cannot do this if the destination of the first assignment is a
3788 condition code register or cc0. We eliminate this case by making sure
3789 the SET_DEST and SET_SRC have the same mode.
3791 We cannot do this if the destination of the second assignment is
3792 a register that we have already assumed is zero-extended. Similarly
3793 for a SUBREG of such a register. */
3795 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3796 && GET_CODE (newpat) == PARALLEL
3797 && XVECLEN (newpat, 0) == 2
3798 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3799 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3800 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3801 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3802 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3803 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3804 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3805 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3806 DF_INSN_LUID (i2))
3807 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3808 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3809 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3810 (REG_P (temp_expr)
3811 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3812 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3813 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3814 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3815 != GET_MODE_MASK (word_mode))))
3816 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3817 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3818 (REG_P (temp_expr)
3819 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3820 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3821 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3822 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3823 != GET_MODE_MASK (word_mode)))))
3824 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3825 SET_SRC (XVECEXP (newpat, 0, 1)))
3826 && ! find_reg_note (i3, REG_UNUSED,
3827 SET_DEST (XVECEXP (newpat, 0, 0))))
3829 rtx ni2dest;
3831 newi2pat = XVECEXP (newpat, 0, 0);
3832 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3833 newpat = XVECEXP (newpat, 0, 1);
3834 SUBST (SET_SRC (newpat),
3835 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3836 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3838 if (i2_code_number >= 0)
3839 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3841 if (insn_code_number >= 0)
3842 swap_i2i3 = 1;
3845 /* Similarly, check for a case where we have a PARALLEL of two independent
3846 SETs but we started with three insns. In this case, we can do the sets
3847 as two separate insns. This case occurs when some SET allows two
3848 other insns to combine, but the destination of that SET is still live.
3850 Also do this if we started with two insns and (at least) one of the
3851 resulting sets is a noop; this noop will be deleted later. */
3853 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3854 && GET_CODE (newpat) == PARALLEL
3855 && XVECLEN (newpat, 0) == 2
3856 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3857 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3858 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3859 || set_noop_p (XVECEXP (newpat, 0, 1)))
3860 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3861 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3862 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3863 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3864 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3865 XVECEXP (newpat, 0, 0))
3866 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3867 XVECEXP (newpat, 0, 1))
3868 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3869 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3871 rtx set0 = XVECEXP (newpat, 0, 0);
3872 rtx set1 = XVECEXP (newpat, 0, 1);
3874 /* Normally, it doesn't matter which of the two is done first,
3875 but the one that references cc0 can't be the second, and
3876 one which uses any regs/memory set in between i2 and i3 can't
3877 be first. The PARALLEL might also have been pre-existing in i3,
3878 so we need to make sure that we won't wrongly hoist a SET to i2
3879 that would conflict with a death note present in there. */
3880 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3881 && !(REG_P (SET_DEST (set1))
3882 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3883 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3884 && find_reg_note (i2, REG_DEAD,
3885 SUBREG_REG (SET_DEST (set1))))
3886 #ifdef HAVE_cc0
3887 && !reg_referenced_p (cc0_rtx, set0)
3888 #endif
3889 /* If I3 is a jump, ensure that set0 is a jump so that
3890 we do not create invalid RTL. */
3891 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3894 newi2pat = set1;
3895 newpat = set0;
3897 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3898 && !(REG_P (SET_DEST (set0))
3899 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3900 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3901 && find_reg_note (i2, REG_DEAD,
3902 SUBREG_REG (SET_DEST (set0))))
3903 #ifdef HAVE_cc0
3904 && !reg_referenced_p (cc0_rtx, set1)
3905 #endif
3906 /* If I3 is a jump, ensure that set1 is a jump so that
3907 we do not create invalid RTL. */
3908 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3911 newi2pat = set0;
3912 newpat = set1;
3914 else
3916 undo_all ();
3917 return 0;
3920 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3922 if (i2_code_number >= 0)
3924 /* recog_for_combine might have added CLOBBERs to newi2pat.
3925 Make sure NEWPAT does not depend on the clobbered regs. */
3926 if (GET_CODE (newi2pat) == PARALLEL)
3928 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3929 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3931 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3932 if (reg_overlap_mentioned_p (reg, newpat))
3934 undo_all ();
3935 return 0;
3940 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3944 /* If it still isn't recognized, fail and change things back the way they
3945 were. */
3946 if ((insn_code_number < 0
3947 /* Is the result a reasonable ASM_OPERANDS? */
3948 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3950 undo_all ();
3951 return 0;
3954 /* If we had to change another insn, make sure it is valid also. */
3955 if (undobuf.other_insn)
3957 CLEAR_HARD_REG_SET (newpat_used_regs);
3959 other_pat = PATTERN (undobuf.other_insn);
3960 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3961 &new_other_notes);
3963 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3965 undo_all ();
3966 return 0;
3970 #ifdef HAVE_cc0
3971 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3972 they are adjacent to each other or not. */
3974 rtx_insn *p = prev_nonnote_insn (i3);
3975 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3976 && sets_cc0_p (newi2pat))
3978 undo_all ();
3979 return 0;
3982 #endif
3984 /* Only allow this combination if insn_rtx_costs reports that the
3985 replacement instructions are cheaper than the originals. */
3986 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3988 undo_all ();
3989 return 0;
3992 if (MAY_HAVE_DEBUG_INSNS)
3994 struct undo *undo;
3996 for (undo = undobuf.undos; undo; undo = undo->next)
3997 if (undo->kind == UNDO_MODE)
3999 rtx reg = *undo->where.r;
4000 machine_mode new_mode = GET_MODE (reg);
4001 machine_mode old_mode = undo->old_contents.m;
4003 /* Temporarily revert mode back. */
4004 adjust_reg_mode (reg, old_mode);
4006 if (reg == i2dest && i2scratch)
4008 /* If we used i2dest as a scratch register with a
4009 different mode, substitute it for the original
4010 i2src while its original mode is temporarily
4011 restored, and then clear i2scratch so that we don't
4012 do it again later. */
4013 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4014 this_basic_block);
4015 i2scratch = false;
4016 /* Put back the new mode. */
4017 adjust_reg_mode (reg, new_mode);
4019 else
4021 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4022 rtx_insn *first, *last;
4024 if (reg == i2dest)
4026 first = i2;
4027 last = last_combined_insn;
4029 else
4031 first = i3;
4032 last = undobuf.other_insn;
4033 gcc_assert (last);
4034 if (DF_INSN_LUID (last)
4035 < DF_INSN_LUID (last_combined_insn))
4036 last = last_combined_insn;
4039 /* We're dealing with a reg that changed mode but not
4040 meaning, so we want to turn it into a subreg for
4041 the new mode. However, because of REG sharing and
4042 because its mode had already changed, we have to do
4043 it in two steps. First, replace any debug uses of
4044 reg, with its original mode temporarily restored,
4045 with this copy we have created; then, replace the
4046 copy with the SUBREG of the original shared reg,
4047 once again changed to the new mode. */
4048 propagate_for_debug (first, last, reg, tempreg,
4049 this_basic_block);
4050 adjust_reg_mode (reg, new_mode);
4051 propagate_for_debug (first, last, tempreg,
4052 lowpart_subreg (old_mode, reg, new_mode),
4053 this_basic_block);
4058 /* If we will be able to accept this, we have made a
4059 change to the destination of I3. This requires us to
4060 do a few adjustments. */
4062 if (changed_i3_dest)
4064 PATTERN (i3) = newpat;
4065 adjust_for_new_dest (i3);
4068 /* We now know that we can do this combination. Merge the insns and
4069 update the status of registers and LOG_LINKS. */
4071 if (undobuf.other_insn)
4073 rtx note, next;
4075 PATTERN (undobuf.other_insn) = other_pat;
4077 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4078 ensure that they are still valid. Then add any non-duplicate
4079 notes added by recog_for_combine. */
4080 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4082 next = XEXP (note, 1);
4084 if ((REG_NOTE_KIND (note) == REG_DEAD
4085 && !reg_referenced_p (XEXP (note, 0),
4086 PATTERN (undobuf.other_insn)))
4087 ||(REG_NOTE_KIND (note) == REG_UNUSED
4088 && !reg_set_p (XEXP (note, 0),
4089 PATTERN (undobuf.other_insn))))
4090 remove_note (undobuf.other_insn, note);
4093 distribute_notes (new_other_notes, undobuf.other_insn,
4094 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4095 NULL_RTX);
4098 if (swap_i2i3)
4100 rtx_insn *insn;
4101 struct insn_link *link;
4102 rtx ni2dest;
4104 /* I3 now uses what used to be its destination and which is now
4105 I2's destination. This requires us to do a few adjustments. */
4106 PATTERN (i3) = newpat;
4107 adjust_for_new_dest (i3);
4109 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4110 so we still will.
4112 However, some later insn might be using I2's dest and have
4113 a LOG_LINK pointing at I3. We must remove this link.
4114 The simplest way to remove the link is to point it at I1,
4115 which we know will be a NOTE. */
4117 /* newi2pat is usually a SET here; however, recog_for_combine might
4118 have added some clobbers. */
4119 if (GET_CODE (newi2pat) == PARALLEL)
4120 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4121 else
4122 ni2dest = SET_DEST (newi2pat);
4124 for (insn = NEXT_INSN (i3);
4125 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4126 || insn != BB_HEAD (this_basic_block->next_bb));
4127 insn = NEXT_INSN (insn))
4129 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4131 FOR_EACH_LOG_LINK (link, insn)
4132 if (link->insn == i3)
4133 link->insn = i1;
4135 break;
4141 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4142 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4143 rtx midnotes = 0;
4144 int from_luid;
4145 /* Compute which registers we expect to eliminate. newi2pat may be setting
4146 either i3dest or i2dest, so we must check it. */
4147 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4148 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4149 || !i2dest_killed
4150 ? 0 : i2dest);
4151 /* For i1, we need to compute both local elimination and global
4152 elimination information with respect to newi2pat because i1dest
4153 may be the same as i3dest, in which case newi2pat may be setting
4154 i1dest. Global information is used when distributing REG_DEAD
4155 note for i2 and i3, in which case it does matter if newi2pat sets
4156 i1dest or not.
4158 Local information is used when distributing REG_DEAD note for i1,
4159 in which case it doesn't matter if newi2pat sets i1dest or not.
4160 See PR62151, if we have four insns combination:
4161 i0: r0 <- i0src
4162 i1: r1 <- i1src (using r0)
4163 REG_DEAD (r0)
4164 i2: r0 <- i2src (using r1)
4165 i3: r3 <- i3src (using r0)
4166 ix: using r0
4167 From i1's point of view, r0 is eliminated, no matter if it is set
4168 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4169 should be discarded.
4171 Note local information only affects cases in forms like "I1->I2->I3",
4172 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4173 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4174 i0dest anyway. */
4175 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4176 || !i1dest_killed
4177 ? 0 : i1dest);
4178 rtx elim_i1 = (local_elim_i1 == 0
4179 || (newi2pat && reg_set_p (i1dest, newi2pat))
4180 ? 0 : i1dest);
4181 /* Same case as i1. */
4182 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4183 ? 0 : i0dest);
4184 rtx elim_i0 = (local_elim_i0 == 0
4185 || (newi2pat && reg_set_p (i0dest, newi2pat))
4186 ? 0 : i0dest);
4188 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4189 clear them. */
4190 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4191 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4192 if (i1)
4193 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4194 if (i0)
4195 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4197 /* Ensure that we do not have something that should not be shared but
4198 occurs multiple times in the new insns. Check this by first
4199 resetting all the `used' flags and then copying anything is shared. */
4201 reset_used_flags (i3notes);
4202 reset_used_flags (i2notes);
4203 reset_used_flags (i1notes);
4204 reset_used_flags (i0notes);
4205 reset_used_flags (newpat);
4206 reset_used_flags (newi2pat);
4207 if (undobuf.other_insn)
4208 reset_used_flags (PATTERN (undobuf.other_insn));
4210 i3notes = copy_rtx_if_shared (i3notes);
4211 i2notes = copy_rtx_if_shared (i2notes);
4212 i1notes = copy_rtx_if_shared (i1notes);
4213 i0notes = copy_rtx_if_shared (i0notes);
4214 newpat = copy_rtx_if_shared (newpat);
4215 newi2pat = copy_rtx_if_shared (newi2pat);
4216 if (undobuf.other_insn)
4217 reset_used_flags (PATTERN (undobuf.other_insn));
4219 INSN_CODE (i3) = insn_code_number;
4220 PATTERN (i3) = newpat;
4222 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4224 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4226 reset_used_flags (call_usage);
4227 call_usage = copy_rtx (call_usage);
4229 if (substed_i2)
4231 /* I2SRC must still be meaningful at this point. Some splitting
4232 operations can invalidate I2SRC, but those operations do not
4233 apply to calls. */
4234 gcc_assert (i2src);
4235 replace_rtx (call_usage, i2dest, i2src);
4238 if (substed_i1)
4239 replace_rtx (call_usage, i1dest, i1src);
4240 if (substed_i0)
4241 replace_rtx (call_usage, i0dest, i0src);
4243 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4246 if (undobuf.other_insn)
4247 INSN_CODE (undobuf.other_insn) = other_code_number;
4249 /* We had one special case above where I2 had more than one set and
4250 we replaced a destination of one of those sets with the destination
4251 of I3. In that case, we have to update LOG_LINKS of insns later
4252 in this basic block. Note that this (expensive) case is rare.
4254 Also, in this case, we must pretend that all REG_NOTEs for I2
4255 actually came from I3, so that REG_UNUSED notes from I2 will be
4256 properly handled. */
4258 if (i3_subst_into_i2)
4260 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4261 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4262 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4263 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4264 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4265 && ! find_reg_note (i2, REG_UNUSED,
4266 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4267 for (temp_insn = NEXT_INSN (i2);
4268 temp_insn
4269 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4270 || BB_HEAD (this_basic_block) != temp_insn);
4271 temp_insn = NEXT_INSN (temp_insn))
4272 if (temp_insn != i3 && INSN_P (temp_insn))
4273 FOR_EACH_LOG_LINK (link, temp_insn)
4274 if (link->insn == i2)
4275 link->insn = i3;
4277 if (i3notes)
4279 rtx link = i3notes;
4280 while (XEXP (link, 1))
4281 link = XEXP (link, 1);
4282 XEXP (link, 1) = i2notes;
4284 else
4285 i3notes = i2notes;
4286 i2notes = 0;
4289 LOG_LINKS (i3) = NULL;
4290 REG_NOTES (i3) = 0;
4291 LOG_LINKS (i2) = NULL;
4292 REG_NOTES (i2) = 0;
4294 if (newi2pat)
4296 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4297 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4298 this_basic_block);
4299 INSN_CODE (i2) = i2_code_number;
4300 PATTERN (i2) = newi2pat;
4302 else
4304 if (MAY_HAVE_DEBUG_INSNS && i2src)
4305 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4306 this_basic_block);
4307 SET_INSN_DELETED (i2);
4310 if (i1)
4312 LOG_LINKS (i1) = NULL;
4313 REG_NOTES (i1) = 0;
4314 if (MAY_HAVE_DEBUG_INSNS)
4315 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4316 this_basic_block);
4317 SET_INSN_DELETED (i1);
4320 if (i0)
4322 LOG_LINKS (i0) = NULL;
4323 REG_NOTES (i0) = 0;
4324 if (MAY_HAVE_DEBUG_INSNS)
4325 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4326 this_basic_block);
4327 SET_INSN_DELETED (i0);
4330 /* Get death notes for everything that is now used in either I3 or
4331 I2 and used to die in a previous insn. If we built two new
4332 patterns, move from I1 to I2 then I2 to I3 so that we get the
4333 proper movement on registers that I2 modifies. */
4335 if (i0)
4336 from_luid = DF_INSN_LUID (i0);
4337 else if (i1)
4338 from_luid = DF_INSN_LUID (i1);
4339 else
4340 from_luid = DF_INSN_LUID (i2);
4341 if (newi2pat)
4342 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4343 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4345 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4346 if (i3notes)
4347 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4348 elim_i2, elim_i1, elim_i0);
4349 if (i2notes)
4350 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4351 elim_i2, elim_i1, elim_i0);
4352 if (i1notes)
4353 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4354 elim_i2, local_elim_i1, local_elim_i0);
4355 if (i0notes)
4356 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4357 elim_i2, elim_i1, local_elim_i0);
4358 if (midnotes)
4359 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4360 elim_i2, elim_i1, elim_i0);
4362 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4363 know these are REG_UNUSED and want them to go to the desired insn,
4364 so we always pass it as i3. */
4366 if (newi2pat && new_i2_notes)
4367 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4368 NULL_RTX);
4370 if (new_i3_notes)
4371 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4372 NULL_RTX);
4374 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4375 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4376 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4377 in that case, it might delete I2. Similarly for I2 and I1.
4378 Show an additional death due to the REG_DEAD note we make here. If
4379 we discard it in distribute_notes, we will decrement it again. */
4381 if (i3dest_killed)
4383 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4384 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4385 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4386 elim_i1, elim_i0);
4387 else
4388 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4389 elim_i2, elim_i1, elim_i0);
4392 if (i2dest_in_i2src)
4394 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4395 if (newi2pat && reg_set_p (i2dest, newi2pat))
4396 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4397 NULL_RTX, NULL_RTX);
4398 else
4399 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4400 NULL_RTX, NULL_RTX, NULL_RTX);
4403 if (i1dest_in_i1src)
4405 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4406 if (newi2pat && reg_set_p (i1dest, newi2pat))
4407 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4408 NULL_RTX, NULL_RTX);
4409 else
4410 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4411 NULL_RTX, NULL_RTX, NULL_RTX);
4414 if (i0dest_in_i0src)
4416 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4417 if (newi2pat && reg_set_p (i0dest, newi2pat))
4418 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4419 NULL_RTX, NULL_RTX);
4420 else
4421 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4422 NULL_RTX, NULL_RTX, NULL_RTX);
4425 distribute_links (i3links);
4426 distribute_links (i2links);
4427 distribute_links (i1links);
4428 distribute_links (i0links);
4430 if (REG_P (i2dest))
4432 struct insn_link *link;
4433 rtx_insn *i2_insn = 0;
4434 rtx i2_val = 0, set;
4436 /* The insn that used to set this register doesn't exist, and
4437 this life of the register may not exist either. See if one of
4438 I3's links points to an insn that sets I2DEST. If it does,
4439 that is now the last known value for I2DEST. If we don't update
4440 this and I2 set the register to a value that depended on its old
4441 contents, we will get confused. If this insn is used, thing
4442 will be set correctly in combine_instructions. */
4443 FOR_EACH_LOG_LINK (link, i3)
4444 if ((set = single_set (link->insn)) != 0
4445 && rtx_equal_p (i2dest, SET_DEST (set)))
4446 i2_insn = link->insn, i2_val = SET_SRC (set);
4448 record_value_for_reg (i2dest, i2_insn, i2_val);
4450 /* If the reg formerly set in I2 died only once and that was in I3,
4451 zero its use count so it won't make `reload' do any work. */
4452 if (! added_sets_2
4453 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4454 && ! i2dest_in_i2src)
4455 INC_REG_N_SETS (REGNO (i2dest), -1);
4458 if (i1 && REG_P (i1dest))
4460 struct insn_link *link;
4461 rtx_insn *i1_insn = 0;
4462 rtx i1_val = 0, set;
4464 FOR_EACH_LOG_LINK (link, i3)
4465 if ((set = single_set (link->insn)) != 0
4466 && rtx_equal_p (i1dest, SET_DEST (set)))
4467 i1_insn = link->insn, i1_val = SET_SRC (set);
4469 record_value_for_reg (i1dest, i1_insn, i1_val);
4471 if (! added_sets_1 && ! i1dest_in_i1src)
4472 INC_REG_N_SETS (REGNO (i1dest), -1);
4475 if (i0 && REG_P (i0dest))
4477 struct insn_link *link;
4478 rtx_insn *i0_insn = 0;
4479 rtx i0_val = 0, set;
4481 FOR_EACH_LOG_LINK (link, i3)
4482 if ((set = single_set (link->insn)) != 0
4483 && rtx_equal_p (i0dest, SET_DEST (set)))
4484 i0_insn = link->insn, i0_val = SET_SRC (set);
4486 record_value_for_reg (i0dest, i0_insn, i0_val);
4488 if (! added_sets_0 && ! i0dest_in_i0src)
4489 INC_REG_N_SETS (REGNO (i0dest), -1);
4492 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4493 been made to this insn. The order is important, because newi2pat
4494 can affect nonzero_bits of newpat. */
4495 if (newi2pat)
4496 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4497 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4500 if (undobuf.other_insn != NULL_RTX)
4502 if (dump_file)
4504 fprintf (dump_file, "modifying other_insn ");
4505 dump_insn_slim (dump_file, undobuf.other_insn);
4507 df_insn_rescan (undobuf.other_insn);
4510 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4512 if (dump_file)
4514 fprintf (dump_file, "modifying insn i0 ");
4515 dump_insn_slim (dump_file, i0);
4517 df_insn_rescan (i0);
4520 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4522 if (dump_file)
4524 fprintf (dump_file, "modifying insn i1 ");
4525 dump_insn_slim (dump_file, i1);
4527 df_insn_rescan (i1);
4530 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4532 if (dump_file)
4534 fprintf (dump_file, "modifying insn i2 ");
4535 dump_insn_slim (dump_file, i2);
4537 df_insn_rescan (i2);
4540 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4542 if (dump_file)
4544 fprintf (dump_file, "modifying insn i3 ");
4545 dump_insn_slim (dump_file, i3);
4547 df_insn_rescan (i3);
4550 /* Set new_direct_jump_p if a new return or simple jump instruction
4551 has been created. Adjust the CFG accordingly. */
4552 if (returnjump_p (i3) || any_uncondjump_p (i3))
4554 *new_direct_jump_p = 1;
4555 mark_jump_label (PATTERN (i3), i3, 0);
4556 update_cfg_for_uncondjump (i3);
4559 if (undobuf.other_insn != NULL_RTX
4560 && (returnjump_p (undobuf.other_insn)
4561 || any_uncondjump_p (undobuf.other_insn)))
4563 *new_direct_jump_p = 1;
4564 update_cfg_for_uncondjump (undobuf.other_insn);
4567 /* A noop might also need cleaning up of CFG, if it comes from the
4568 simplification of a jump. */
4569 if (JUMP_P (i3)
4570 && GET_CODE (newpat) == SET
4571 && SET_SRC (newpat) == pc_rtx
4572 && SET_DEST (newpat) == pc_rtx)
4574 *new_direct_jump_p = 1;
4575 update_cfg_for_uncondjump (i3);
4578 if (undobuf.other_insn != NULL_RTX
4579 && JUMP_P (undobuf.other_insn)
4580 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4581 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4582 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4584 *new_direct_jump_p = 1;
4585 update_cfg_for_uncondjump (undobuf.other_insn);
4588 combine_successes++;
4589 undo_commit ();
4591 if (added_links_insn
4592 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4593 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4594 return added_links_insn;
4595 else
4596 return newi2pat ? i2 : i3;
4599 /* Undo all the modifications recorded in undobuf. */
4601 static void
4602 undo_all (void)
4604 struct undo *undo, *next;
4606 for (undo = undobuf.undos; undo; undo = next)
4608 next = undo->next;
4609 switch (undo->kind)
4611 case UNDO_RTX:
4612 *undo->where.r = undo->old_contents.r;
4613 break;
4614 case UNDO_INT:
4615 *undo->where.i = undo->old_contents.i;
4616 break;
4617 case UNDO_MODE:
4618 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4619 break;
4620 case UNDO_LINKS:
4621 *undo->where.l = undo->old_contents.l;
4622 break;
4623 default:
4624 gcc_unreachable ();
4627 undo->next = undobuf.frees;
4628 undobuf.frees = undo;
4631 undobuf.undos = 0;
4634 /* We've committed to accepting the changes we made. Move all
4635 of the undos to the free list. */
4637 static void
4638 undo_commit (void)
4640 struct undo *undo, *next;
4642 for (undo = undobuf.undos; undo; undo = next)
4644 next = undo->next;
4645 undo->next = undobuf.frees;
4646 undobuf.frees = undo;
4648 undobuf.undos = 0;
4651 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4652 where we have an arithmetic expression and return that point. LOC will
4653 be inside INSN.
4655 try_combine will call this function to see if an insn can be split into
4656 two insns. */
4658 static rtx *
4659 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4661 rtx x = *loc;
4662 enum rtx_code code = GET_CODE (x);
4663 rtx *split;
4664 unsigned HOST_WIDE_INT len = 0;
4665 HOST_WIDE_INT pos = 0;
4666 int unsignedp = 0;
4667 rtx inner = NULL_RTX;
4669 /* First special-case some codes. */
4670 switch (code)
4672 case SUBREG:
4673 #ifdef INSN_SCHEDULING
4674 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4675 point. */
4676 if (MEM_P (SUBREG_REG (x)))
4677 return loc;
4678 #endif
4679 return find_split_point (&SUBREG_REG (x), insn, false);
4681 case MEM:
4682 #ifdef HAVE_lo_sum
4683 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4684 using LO_SUM and HIGH. */
4685 if (GET_CODE (XEXP (x, 0)) == CONST
4686 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4688 machine_mode address_mode = get_address_mode (x);
4690 SUBST (XEXP (x, 0),
4691 gen_rtx_LO_SUM (address_mode,
4692 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4693 XEXP (x, 0)));
4694 return &XEXP (XEXP (x, 0), 0);
4696 #endif
4698 /* If we have a PLUS whose second operand is a constant and the
4699 address is not valid, perhaps will can split it up using
4700 the machine-specific way to split large constants. We use
4701 the first pseudo-reg (one of the virtual regs) as a placeholder;
4702 it will not remain in the result. */
4703 if (GET_CODE (XEXP (x, 0)) == PLUS
4704 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4705 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4706 MEM_ADDR_SPACE (x)))
4708 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4709 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4710 XEXP (x, 0)),
4711 subst_insn);
4713 /* This should have produced two insns, each of which sets our
4714 placeholder. If the source of the second is a valid address,
4715 we can make put both sources together and make a split point
4716 in the middle. */
4718 if (seq
4719 && NEXT_INSN (seq) != NULL_RTX
4720 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4721 && NONJUMP_INSN_P (seq)
4722 && GET_CODE (PATTERN (seq)) == SET
4723 && SET_DEST (PATTERN (seq)) == reg
4724 && ! reg_mentioned_p (reg,
4725 SET_SRC (PATTERN (seq)))
4726 && NONJUMP_INSN_P (NEXT_INSN (seq))
4727 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4728 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4729 && memory_address_addr_space_p
4730 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4731 MEM_ADDR_SPACE (x)))
4733 rtx src1 = SET_SRC (PATTERN (seq));
4734 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4736 /* Replace the placeholder in SRC2 with SRC1. If we can
4737 find where in SRC2 it was placed, that can become our
4738 split point and we can replace this address with SRC2.
4739 Just try two obvious places. */
4741 src2 = replace_rtx (src2, reg, src1);
4742 split = 0;
4743 if (XEXP (src2, 0) == src1)
4744 split = &XEXP (src2, 0);
4745 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4746 && XEXP (XEXP (src2, 0), 0) == src1)
4747 split = &XEXP (XEXP (src2, 0), 0);
4749 if (split)
4751 SUBST (XEXP (x, 0), src2);
4752 return split;
4756 /* If that didn't work, perhaps the first operand is complex and
4757 needs to be computed separately, so make a split point there.
4758 This will occur on machines that just support REG + CONST
4759 and have a constant moved through some previous computation. */
4761 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4762 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4763 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4764 return &XEXP (XEXP (x, 0), 0);
4767 /* If we have a PLUS whose first operand is complex, try computing it
4768 separately by making a split there. */
4769 if (GET_CODE (XEXP (x, 0)) == PLUS
4770 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4771 MEM_ADDR_SPACE (x))
4772 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4773 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4774 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4775 return &XEXP (XEXP (x, 0), 0);
4776 break;
4778 case SET:
4779 #ifdef HAVE_cc0
4780 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4781 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4782 we need to put the operand into a register. So split at that
4783 point. */
4785 if (SET_DEST (x) == cc0_rtx
4786 && GET_CODE (SET_SRC (x)) != COMPARE
4787 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4788 && !OBJECT_P (SET_SRC (x))
4789 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4790 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4791 return &SET_SRC (x);
4792 #endif
4794 /* See if we can split SET_SRC as it stands. */
4795 split = find_split_point (&SET_SRC (x), insn, true);
4796 if (split && split != &SET_SRC (x))
4797 return split;
4799 /* See if we can split SET_DEST as it stands. */
4800 split = find_split_point (&SET_DEST (x), insn, false);
4801 if (split && split != &SET_DEST (x))
4802 return split;
4804 /* See if this is a bitfield assignment with everything constant. If
4805 so, this is an IOR of an AND, so split it into that. */
4806 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4807 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4808 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4809 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4810 && CONST_INT_P (SET_SRC (x))
4811 && ((INTVAL (XEXP (SET_DEST (x), 1))
4812 + INTVAL (XEXP (SET_DEST (x), 2)))
4813 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4814 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4816 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4817 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4818 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4819 rtx dest = XEXP (SET_DEST (x), 0);
4820 machine_mode mode = GET_MODE (dest);
4821 unsigned HOST_WIDE_INT mask
4822 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4823 rtx or_mask;
4825 if (BITS_BIG_ENDIAN)
4826 pos = GET_MODE_PRECISION (mode) - len - pos;
4828 or_mask = gen_int_mode (src << pos, mode);
4829 if (src == mask)
4830 SUBST (SET_SRC (x),
4831 simplify_gen_binary (IOR, mode, dest, or_mask));
4832 else
4834 rtx negmask = gen_int_mode (~(mask << pos), mode);
4835 SUBST (SET_SRC (x),
4836 simplify_gen_binary (IOR, mode,
4837 simplify_gen_binary (AND, mode,
4838 dest, negmask),
4839 or_mask));
4842 SUBST (SET_DEST (x), dest);
4844 split = find_split_point (&SET_SRC (x), insn, true);
4845 if (split && split != &SET_SRC (x))
4846 return split;
4849 /* Otherwise, see if this is an operation that we can split into two.
4850 If so, try to split that. */
4851 code = GET_CODE (SET_SRC (x));
4853 switch (code)
4855 case AND:
4856 /* If we are AND'ing with a large constant that is only a single
4857 bit and the result is only being used in a context where we
4858 need to know if it is zero or nonzero, replace it with a bit
4859 extraction. This will avoid the large constant, which might
4860 have taken more than one insn to make. If the constant were
4861 not a valid argument to the AND but took only one insn to make,
4862 this is no worse, but if it took more than one insn, it will
4863 be better. */
4865 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4866 && REG_P (XEXP (SET_SRC (x), 0))
4867 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4868 && REG_P (SET_DEST (x))
4869 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4870 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4871 && XEXP (*split, 0) == SET_DEST (x)
4872 && XEXP (*split, 1) == const0_rtx)
4874 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4875 XEXP (SET_SRC (x), 0),
4876 pos, NULL_RTX, 1, 1, 0, 0);
4877 if (extraction != 0)
4879 SUBST (SET_SRC (x), extraction);
4880 return find_split_point (loc, insn, false);
4883 break;
4885 case NE:
4886 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4887 is known to be on, this can be converted into a NEG of a shift. */
4888 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4889 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4890 && 1 <= (pos = exact_log2
4891 (nonzero_bits (XEXP (SET_SRC (x), 0),
4892 GET_MODE (XEXP (SET_SRC (x), 0))))))
4894 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4896 SUBST (SET_SRC (x),
4897 gen_rtx_NEG (mode,
4898 gen_rtx_LSHIFTRT (mode,
4899 XEXP (SET_SRC (x), 0),
4900 GEN_INT (pos))));
4902 split = find_split_point (&SET_SRC (x), insn, true);
4903 if (split && split != &SET_SRC (x))
4904 return split;
4906 break;
4908 case SIGN_EXTEND:
4909 inner = XEXP (SET_SRC (x), 0);
4911 /* We can't optimize if either mode is a partial integer
4912 mode as we don't know how many bits are significant
4913 in those modes. */
4914 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4915 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4916 break;
4918 pos = 0;
4919 len = GET_MODE_PRECISION (GET_MODE (inner));
4920 unsignedp = 0;
4921 break;
4923 case SIGN_EXTRACT:
4924 case ZERO_EXTRACT:
4925 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4926 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4928 inner = XEXP (SET_SRC (x), 0);
4929 len = INTVAL (XEXP (SET_SRC (x), 1));
4930 pos = INTVAL (XEXP (SET_SRC (x), 2));
4932 if (BITS_BIG_ENDIAN)
4933 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4934 unsignedp = (code == ZERO_EXTRACT);
4936 break;
4938 default:
4939 break;
4942 if (len && pos >= 0
4943 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4945 machine_mode mode = GET_MODE (SET_SRC (x));
4947 /* For unsigned, we have a choice of a shift followed by an
4948 AND or two shifts. Use two shifts for field sizes where the
4949 constant might be too large. We assume here that we can
4950 always at least get 8-bit constants in an AND insn, which is
4951 true for every current RISC. */
4953 if (unsignedp && len <= 8)
4955 unsigned HOST_WIDE_INT mask
4956 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4957 SUBST (SET_SRC (x),
4958 gen_rtx_AND (mode,
4959 gen_rtx_LSHIFTRT
4960 (mode, gen_lowpart (mode, inner),
4961 GEN_INT (pos)),
4962 gen_int_mode (mask, mode)));
4964 split = find_split_point (&SET_SRC (x), insn, true);
4965 if (split && split != &SET_SRC (x))
4966 return split;
4968 else
4970 SUBST (SET_SRC (x),
4971 gen_rtx_fmt_ee
4972 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4973 gen_rtx_ASHIFT (mode,
4974 gen_lowpart (mode, inner),
4975 GEN_INT (GET_MODE_PRECISION (mode)
4976 - len - pos)),
4977 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4979 split = find_split_point (&SET_SRC (x), insn, true);
4980 if (split && split != &SET_SRC (x))
4981 return split;
4985 /* See if this is a simple operation with a constant as the second
4986 operand. It might be that this constant is out of range and hence
4987 could be used as a split point. */
4988 if (BINARY_P (SET_SRC (x))
4989 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4990 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4991 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4992 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4993 return &XEXP (SET_SRC (x), 1);
4995 /* Finally, see if this is a simple operation with its first operand
4996 not in a register. The operation might require this operand in a
4997 register, so return it as a split point. We can always do this
4998 because if the first operand were another operation, we would have
4999 already found it as a split point. */
5000 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5001 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5002 return &XEXP (SET_SRC (x), 0);
5004 return 0;
5006 case AND:
5007 case IOR:
5008 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5009 it is better to write this as (not (ior A B)) so we can split it.
5010 Similarly for IOR. */
5011 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5013 SUBST (*loc,
5014 gen_rtx_NOT (GET_MODE (x),
5015 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5016 GET_MODE (x),
5017 XEXP (XEXP (x, 0), 0),
5018 XEXP (XEXP (x, 1), 0))));
5019 return find_split_point (loc, insn, set_src);
5022 /* Many RISC machines have a large set of logical insns. If the
5023 second operand is a NOT, put it first so we will try to split the
5024 other operand first. */
5025 if (GET_CODE (XEXP (x, 1)) == NOT)
5027 rtx tem = XEXP (x, 0);
5028 SUBST (XEXP (x, 0), XEXP (x, 1));
5029 SUBST (XEXP (x, 1), tem);
5031 break;
5033 case PLUS:
5034 case MINUS:
5035 /* Canonicalization can produce (minus A (mult B C)), where C is a
5036 constant. It may be better to try splitting (plus (mult B -C) A)
5037 instead if this isn't a multiply by a power of two. */
5038 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5039 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5040 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5042 machine_mode mode = GET_MODE (x);
5043 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5044 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5045 SUBST (*loc, gen_rtx_PLUS (mode,
5046 gen_rtx_MULT (mode,
5047 XEXP (XEXP (x, 1), 0),
5048 gen_int_mode (other_int,
5049 mode)),
5050 XEXP (x, 0)));
5051 return find_split_point (loc, insn, set_src);
5054 /* Split at a multiply-accumulate instruction. However if this is
5055 the SET_SRC, we likely do not have such an instruction and it's
5056 worthless to try this split. */
5057 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5058 return loc;
5060 default:
5061 break;
5064 /* Otherwise, select our actions depending on our rtx class. */
5065 switch (GET_RTX_CLASS (code))
5067 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5068 case RTX_TERNARY:
5069 split = find_split_point (&XEXP (x, 2), insn, false);
5070 if (split)
5071 return split;
5072 /* ... fall through ... */
5073 case RTX_BIN_ARITH:
5074 case RTX_COMM_ARITH:
5075 case RTX_COMPARE:
5076 case RTX_COMM_COMPARE:
5077 split = find_split_point (&XEXP (x, 1), insn, false);
5078 if (split)
5079 return split;
5080 /* ... fall through ... */
5081 case RTX_UNARY:
5082 /* Some machines have (and (shift ...) ...) insns. If X is not
5083 an AND, but XEXP (X, 0) is, use it as our split point. */
5084 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5085 return &XEXP (x, 0);
5087 split = find_split_point (&XEXP (x, 0), insn, false);
5088 if (split)
5089 return split;
5090 return loc;
5092 default:
5093 /* Otherwise, we don't have a split point. */
5094 return 0;
5098 /* Throughout X, replace FROM with TO, and return the result.
5099 The result is TO if X is FROM;
5100 otherwise the result is X, but its contents may have been modified.
5101 If they were modified, a record was made in undobuf so that
5102 undo_all will (among other things) return X to its original state.
5104 If the number of changes necessary is too much to record to undo,
5105 the excess changes are not made, so the result is invalid.
5106 The changes already made can still be undone.
5107 undobuf.num_undo is incremented for such changes, so by testing that
5108 the caller can tell whether the result is valid.
5110 `n_occurrences' is incremented each time FROM is replaced.
5112 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5114 IN_COND is nonzero if we are at the top level of a condition.
5116 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5117 by copying if `n_occurrences' is nonzero. */
5119 static rtx
5120 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5122 enum rtx_code code = GET_CODE (x);
5123 machine_mode op0_mode = VOIDmode;
5124 const char *fmt;
5125 int len, i;
5126 rtx new_rtx;
5128 /* Two expressions are equal if they are identical copies of a shared
5129 RTX or if they are both registers with the same register number
5130 and mode. */
5132 #define COMBINE_RTX_EQUAL_P(X,Y) \
5133 ((X) == (Y) \
5134 || (REG_P (X) && REG_P (Y) \
5135 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5137 /* Do not substitute into clobbers of regs -- this will never result in
5138 valid RTL. */
5139 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5140 return x;
5142 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5144 n_occurrences++;
5145 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5148 /* If X and FROM are the same register but different modes, they
5149 will not have been seen as equal above. However, the log links code
5150 will make a LOG_LINKS entry for that case. If we do nothing, we
5151 will try to rerecognize our original insn and, when it succeeds,
5152 we will delete the feeding insn, which is incorrect.
5154 So force this insn not to match in this (rare) case. */
5155 if (! in_dest && code == REG && REG_P (from)
5156 && reg_overlap_mentioned_p (x, from))
5157 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5159 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5160 of which may contain things that can be combined. */
5161 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5162 return x;
5164 /* It is possible to have a subexpression appear twice in the insn.
5165 Suppose that FROM is a register that appears within TO.
5166 Then, after that subexpression has been scanned once by `subst',
5167 the second time it is scanned, TO may be found. If we were
5168 to scan TO here, we would find FROM within it and create a
5169 self-referent rtl structure which is completely wrong. */
5170 if (COMBINE_RTX_EQUAL_P (x, to))
5171 return to;
5173 /* Parallel asm_operands need special attention because all of the
5174 inputs are shared across the arms. Furthermore, unsharing the
5175 rtl results in recognition failures. Failure to handle this case
5176 specially can result in circular rtl.
5178 Solve this by doing a normal pass across the first entry of the
5179 parallel, and only processing the SET_DESTs of the subsequent
5180 entries. Ug. */
5182 if (code == PARALLEL
5183 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5184 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5186 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5188 /* If this substitution failed, this whole thing fails. */
5189 if (GET_CODE (new_rtx) == CLOBBER
5190 && XEXP (new_rtx, 0) == const0_rtx)
5191 return new_rtx;
5193 SUBST (XVECEXP (x, 0, 0), new_rtx);
5195 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5197 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5199 if (!REG_P (dest)
5200 && GET_CODE (dest) != CC0
5201 && GET_CODE (dest) != PC)
5203 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5205 /* If this substitution failed, this whole thing fails. */
5206 if (GET_CODE (new_rtx) == CLOBBER
5207 && XEXP (new_rtx, 0) == const0_rtx)
5208 return new_rtx;
5210 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5214 else
5216 len = GET_RTX_LENGTH (code);
5217 fmt = GET_RTX_FORMAT (code);
5219 /* We don't need to process a SET_DEST that is a register, CC0,
5220 or PC, so set up to skip this common case. All other cases
5221 where we want to suppress replacing something inside a
5222 SET_SRC are handled via the IN_DEST operand. */
5223 if (code == SET
5224 && (REG_P (SET_DEST (x))
5225 || GET_CODE (SET_DEST (x)) == CC0
5226 || GET_CODE (SET_DEST (x)) == PC))
5227 fmt = "ie";
5229 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5230 constant. */
5231 if (fmt[0] == 'e')
5232 op0_mode = GET_MODE (XEXP (x, 0));
5234 for (i = 0; i < len; i++)
5236 if (fmt[i] == 'E')
5238 int j;
5239 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5241 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5243 new_rtx = (unique_copy && n_occurrences
5244 ? copy_rtx (to) : to);
5245 n_occurrences++;
5247 else
5249 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5250 unique_copy);
5252 /* If this substitution failed, this whole thing
5253 fails. */
5254 if (GET_CODE (new_rtx) == CLOBBER
5255 && XEXP (new_rtx, 0) == const0_rtx)
5256 return new_rtx;
5259 SUBST (XVECEXP (x, i, j), new_rtx);
5262 else if (fmt[i] == 'e')
5264 /* If this is a register being set, ignore it. */
5265 new_rtx = XEXP (x, i);
5266 if (in_dest
5267 && i == 0
5268 && (((code == SUBREG || code == ZERO_EXTRACT)
5269 && REG_P (new_rtx))
5270 || code == STRICT_LOW_PART))
5273 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5275 /* In general, don't install a subreg involving two
5276 modes not tieable. It can worsen register
5277 allocation, and can even make invalid reload
5278 insns, since the reg inside may need to be copied
5279 from in the outside mode, and that may be invalid
5280 if it is an fp reg copied in integer mode.
5282 We allow two exceptions to this: It is valid if
5283 it is inside another SUBREG and the mode of that
5284 SUBREG and the mode of the inside of TO is
5285 tieable and it is valid if X is a SET that copies
5286 FROM to CC0. */
5288 if (GET_CODE (to) == SUBREG
5289 && ! MODES_TIEABLE_P (GET_MODE (to),
5290 GET_MODE (SUBREG_REG (to)))
5291 && ! (code == SUBREG
5292 && MODES_TIEABLE_P (GET_MODE (x),
5293 GET_MODE (SUBREG_REG (to))))
5294 #ifdef HAVE_cc0
5295 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5296 #endif
5298 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5300 if (code == SUBREG
5301 && REG_P (to)
5302 && REGNO (to) < FIRST_PSEUDO_REGISTER
5303 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5304 SUBREG_BYTE (x),
5305 GET_MODE (x)) < 0)
5306 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5308 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5309 n_occurrences++;
5311 else
5312 /* If we are in a SET_DEST, suppress most cases unless we
5313 have gone inside a MEM, in which case we want to
5314 simplify the address. We assume here that things that
5315 are actually part of the destination have their inner
5316 parts in the first expression. This is true for SUBREG,
5317 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5318 things aside from REG and MEM that should appear in a
5319 SET_DEST. */
5320 new_rtx = subst (XEXP (x, i), from, to,
5321 (((in_dest
5322 && (code == SUBREG || code == STRICT_LOW_PART
5323 || code == ZERO_EXTRACT))
5324 || code == SET)
5325 && i == 0),
5326 code == IF_THEN_ELSE && i == 0,
5327 unique_copy);
5329 /* If we found that we will have to reject this combination,
5330 indicate that by returning the CLOBBER ourselves, rather than
5331 an expression containing it. This will speed things up as
5332 well as prevent accidents where two CLOBBERs are considered
5333 to be equal, thus producing an incorrect simplification. */
5335 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5336 return new_rtx;
5338 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5340 machine_mode mode = GET_MODE (x);
5342 x = simplify_subreg (GET_MODE (x), new_rtx,
5343 GET_MODE (SUBREG_REG (x)),
5344 SUBREG_BYTE (x));
5345 if (! x)
5346 x = gen_rtx_CLOBBER (mode, const0_rtx);
5348 else if (CONST_SCALAR_INT_P (new_rtx)
5349 && GET_CODE (x) == ZERO_EXTEND)
5351 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5352 new_rtx, GET_MODE (XEXP (x, 0)));
5353 gcc_assert (x);
5355 else
5356 SUBST (XEXP (x, i), new_rtx);
5361 /* Check if we are loading something from the constant pool via float
5362 extension; in this case we would undo compress_float_constant
5363 optimization and degenerate constant load to an immediate value. */
5364 if (GET_CODE (x) == FLOAT_EXTEND
5365 && MEM_P (XEXP (x, 0))
5366 && MEM_READONLY_P (XEXP (x, 0)))
5368 rtx tmp = avoid_constant_pool_reference (x);
5369 if (x != tmp)
5370 return x;
5373 /* Try to simplify X. If the simplification changed the code, it is likely
5374 that further simplification will help, so loop, but limit the number
5375 of repetitions that will be performed. */
5377 for (i = 0; i < 4; i++)
5379 /* If X is sufficiently simple, don't bother trying to do anything
5380 with it. */
5381 if (code != CONST_INT && code != REG && code != CLOBBER)
5382 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5384 if (GET_CODE (x) == code)
5385 break;
5387 code = GET_CODE (x);
5389 /* We no longer know the original mode of operand 0 since we
5390 have changed the form of X) */
5391 op0_mode = VOIDmode;
5394 return x;
5397 /* Simplify X, a piece of RTL. We just operate on the expression at the
5398 outer level; call `subst' to simplify recursively. Return the new
5399 expression.
5401 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5402 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5403 of a condition. */
5405 static rtx
5406 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5407 int in_cond)
5409 enum rtx_code code = GET_CODE (x);
5410 machine_mode mode = GET_MODE (x);
5411 rtx temp;
5412 int i;
5414 /* If this is a commutative operation, put a constant last and a complex
5415 expression first. We don't need to do this for comparisons here. */
5416 if (COMMUTATIVE_ARITH_P (x)
5417 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5419 temp = XEXP (x, 0);
5420 SUBST (XEXP (x, 0), XEXP (x, 1));
5421 SUBST (XEXP (x, 1), temp);
5424 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5425 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5426 things. Check for cases where both arms are testing the same
5427 condition.
5429 Don't do anything if all operands are very simple. */
5431 if ((BINARY_P (x)
5432 && ((!OBJECT_P (XEXP (x, 0))
5433 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5434 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5435 || (!OBJECT_P (XEXP (x, 1))
5436 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5437 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5438 || (UNARY_P (x)
5439 && (!OBJECT_P (XEXP (x, 0))
5440 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5441 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5443 rtx cond, true_rtx, false_rtx;
5445 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5446 if (cond != 0
5447 /* If everything is a comparison, what we have is highly unlikely
5448 to be simpler, so don't use it. */
5449 && ! (COMPARISON_P (x)
5450 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5452 rtx cop1 = const0_rtx;
5453 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5455 if (cond_code == NE && COMPARISON_P (cond))
5456 return x;
5458 /* Simplify the alternative arms; this may collapse the true and
5459 false arms to store-flag values. Be careful to use copy_rtx
5460 here since true_rtx or false_rtx might share RTL with x as a
5461 result of the if_then_else_cond call above. */
5462 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5463 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5465 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5466 is unlikely to be simpler. */
5467 if (general_operand (true_rtx, VOIDmode)
5468 && general_operand (false_rtx, VOIDmode))
5470 enum rtx_code reversed;
5472 /* Restarting if we generate a store-flag expression will cause
5473 us to loop. Just drop through in this case. */
5475 /* If the result values are STORE_FLAG_VALUE and zero, we can
5476 just make the comparison operation. */
5477 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5478 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5479 cond, cop1);
5480 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5481 && ((reversed = reversed_comparison_code_parts
5482 (cond_code, cond, cop1, NULL))
5483 != UNKNOWN))
5484 x = simplify_gen_relational (reversed, mode, VOIDmode,
5485 cond, cop1);
5487 /* Likewise, we can make the negate of a comparison operation
5488 if the result values are - STORE_FLAG_VALUE and zero. */
5489 else if (CONST_INT_P (true_rtx)
5490 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5491 && false_rtx == const0_rtx)
5492 x = simplify_gen_unary (NEG, mode,
5493 simplify_gen_relational (cond_code,
5494 mode, VOIDmode,
5495 cond, cop1),
5496 mode);
5497 else if (CONST_INT_P (false_rtx)
5498 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5499 && true_rtx == const0_rtx
5500 && ((reversed = reversed_comparison_code_parts
5501 (cond_code, cond, cop1, NULL))
5502 != UNKNOWN))
5503 x = simplify_gen_unary (NEG, mode,
5504 simplify_gen_relational (reversed,
5505 mode, VOIDmode,
5506 cond, cop1),
5507 mode);
5508 else
5509 return gen_rtx_IF_THEN_ELSE (mode,
5510 simplify_gen_relational (cond_code,
5511 mode,
5512 VOIDmode,
5513 cond,
5514 cop1),
5515 true_rtx, false_rtx);
5517 code = GET_CODE (x);
5518 op0_mode = VOIDmode;
5523 /* Try to fold this expression in case we have constants that weren't
5524 present before. */
5525 temp = 0;
5526 switch (GET_RTX_CLASS (code))
5528 case RTX_UNARY:
5529 if (op0_mode == VOIDmode)
5530 op0_mode = GET_MODE (XEXP (x, 0));
5531 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5532 break;
5533 case RTX_COMPARE:
5534 case RTX_COMM_COMPARE:
5536 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5537 if (cmp_mode == VOIDmode)
5539 cmp_mode = GET_MODE (XEXP (x, 1));
5540 if (cmp_mode == VOIDmode)
5541 cmp_mode = op0_mode;
5543 temp = simplify_relational_operation (code, mode, cmp_mode,
5544 XEXP (x, 0), XEXP (x, 1));
5546 break;
5547 case RTX_COMM_ARITH:
5548 case RTX_BIN_ARITH:
5549 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5550 break;
5551 case RTX_BITFIELD_OPS:
5552 case RTX_TERNARY:
5553 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5554 XEXP (x, 1), XEXP (x, 2));
5555 break;
5556 default:
5557 break;
5560 if (temp)
5562 x = temp;
5563 code = GET_CODE (temp);
5564 op0_mode = VOIDmode;
5565 mode = GET_MODE (temp);
5568 /* First see if we can apply the inverse distributive law. */
5569 if (code == PLUS || code == MINUS
5570 || code == AND || code == IOR || code == XOR)
5572 x = apply_distributive_law (x);
5573 code = GET_CODE (x);
5574 op0_mode = VOIDmode;
5577 /* If CODE is an associative operation not otherwise handled, see if we
5578 can associate some operands. This can win if they are constants or
5579 if they are logically related (i.e. (a & b) & a). */
5580 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5581 || code == AND || code == IOR || code == XOR
5582 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5583 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5584 || (flag_associative_math && FLOAT_MODE_P (mode))))
5586 if (GET_CODE (XEXP (x, 0)) == code)
5588 rtx other = XEXP (XEXP (x, 0), 0);
5589 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5590 rtx inner_op1 = XEXP (x, 1);
5591 rtx inner;
5593 /* Make sure we pass the constant operand if any as the second
5594 one if this is a commutative operation. */
5595 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5597 rtx tem = inner_op0;
5598 inner_op0 = inner_op1;
5599 inner_op1 = tem;
5601 inner = simplify_binary_operation (code == MINUS ? PLUS
5602 : code == DIV ? MULT
5603 : code,
5604 mode, inner_op0, inner_op1);
5606 /* For commutative operations, try the other pair if that one
5607 didn't simplify. */
5608 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5610 other = XEXP (XEXP (x, 0), 1);
5611 inner = simplify_binary_operation (code, mode,
5612 XEXP (XEXP (x, 0), 0),
5613 XEXP (x, 1));
5616 if (inner)
5617 return simplify_gen_binary (code, mode, other, inner);
5621 /* A little bit of algebraic simplification here. */
5622 switch (code)
5624 case MEM:
5625 /* Ensure that our address has any ASHIFTs converted to MULT in case
5626 address-recognizing predicates are called later. */
5627 temp = make_compound_operation (XEXP (x, 0), MEM);
5628 SUBST (XEXP (x, 0), temp);
5629 break;
5631 case SUBREG:
5632 if (op0_mode == VOIDmode)
5633 op0_mode = GET_MODE (SUBREG_REG (x));
5635 /* See if this can be moved to simplify_subreg. */
5636 if (CONSTANT_P (SUBREG_REG (x))
5637 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5638 /* Don't call gen_lowpart if the inner mode
5639 is VOIDmode and we cannot simplify it, as SUBREG without
5640 inner mode is invalid. */
5641 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5642 || gen_lowpart_common (mode, SUBREG_REG (x))))
5643 return gen_lowpart (mode, SUBREG_REG (x));
5645 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5646 break;
5648 rtx temp;
5649 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5650 SUBREG_BYTE (x));
5651 if (temp)
5652 return temp;
5654 /* If op is known to have all lower bits zero, the result is zero. */
5655 if (!in_dest
5656 && SCALAR_INT_MODE_P (mode)
5657 && SCALAR_INT_MODE_P (op0_mode)
5658 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5659 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5660 && HWI_COMPUTABLE_MODE_P (op0_mode)
5661 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5662 & GET_MODE_MASK (mode)) == 0)
5663 return CONST0_RTX (mode);
5666 /* Don't change the mode of the MEM if that would change the meaning
5667 of the address. */
5668 if (MEM_P (SUBREG_REG (x))
5669 && (MEM_VOLATILE_P (SUBREG_REG (x))
5670 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5671 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5672 return gen_rtx_CLOBBER (mode, const0_rtx);
5674 /* Note that we cannot do any narrowing for non-constants since
5675 we might have been counting on using the fact that some bits were
5676 zero. We now do this in the SET. */
5678 break;
5680 case NEG:
5681 temp = expand_compound_operation (XEXP (x, 0));
5683 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5684 replaced by (lshiftrt X C). This will convert
5685 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5687 if (GET_CODE (temp) == ASHIFTRT
5688 && CONST_INT_P (XEXP (temp, 1))
5689 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5690 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5691 INTVAL (XEXP (temp, 1)));
5693 /* If X has only a single bit that might be nonzero, say, bit I, convert
5694 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5695 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5696 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5697 or a SUBREG of one since we'd be making the expression more
5698 complex if it was just a register. */
5700 if (!REG_P (temp)
5701 && ! (GET_CODE (temp) == SUBREG
5702 && REG_P (SUBREG_REG (temp)))
5703 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5705 rtx temp1 = simplify_shift_const
5706 (NULL_RTX, ASHIFTRT, mode,
5707 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5708 GET_MODE_PRECISION (mode) - 1 - i),
5709 GET_MODE_PRECISION (mode) - 1 - i);
5711 /* If all we did was surround TEMP with the two shifts, we
5712 haven't improved anything, so don't use it. Otherwise,
5713 we are better off with TEMP1. */
5714 if (GET_CODE (temp1) != ASHIFTRT
5715 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5716 || XEXP (XEXP (temp1, 0), 0) != temp)
5717 return temp1;
5719 break;
5721 case TRUNCATE:
5722 /* We can't handle truncation to a partial integer mode here
5723 because we don't know the real bitsize of the partial
5724 integer mode. */
5725 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5726 break;
5728 if (HWI_COMPUTABLE_MODE_P (mode))
5729 SUBST (XEXP (x, 0),
5730 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5731 GET_MODE_MASK (mode), 0));
5733 /* We can truncate a constant value and return it. */
5734 if (CONST_INT_P (XEXP (x, 0)))
5735 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5737 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5738 whose value is a comparison can be replaced with a subreg if
5739 STORE_FLAG_VALUE permits. */
5740 if (HWI_COMPUTABLE_MODE_P (mode)
5741 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5742 && (temp = get_last_value (XEXP (x, 0)))
5743 && COMPARISON_P (temp))
5744 return gen_lowpart (mode, XEXP (x, 0));
5745 break;
5747 case CONST:
5748 /* (const (const X)) can become (const X). Do it this way rather than
5749 returning the inner CONST since CONST can be shared with a
5750 REG_EQUAL note. */
5751 if (GET_CODE (XEXP (x, 0)) == CONST)
5752 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5753 break;
5755 #ifdef HAVE_lo_sum
5756 case LO_SUM:
5757 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5758 can add in an offset. find_split_point will split this address up
5759 again if it doesn't match. */
5760 if (GET_CODE (XEXP (x, 0)) == HIGH
5761 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5762 return XEXP (x, 1);
5763 break;
5764 #endif
5766 case PLUS:
5767 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5768 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5769 bit-field and can be replaced by either a sign_extend or a
5770 sign_extract. The `and' may be a zero_extend and the two
5771 <c>, -<c> constants may be reversed. */
5772 if (GET_CODE (XEXP (x, 0)) == XOR
5773 && CONST_INT_P (XEXP (x, 1))
5774 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5775 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5776 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5777 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5778 && HWI_COMPUTABLE_MODE_P (mode)
5779 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5780 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5781 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5782 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5783 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5784 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5785 == (unsigned int) i + 1))))
5786 return simplify_shift_const
5787 (NULL_RTX, ASHIFTRT, mode,
5788 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5789 XEXP (XEXP (XEXP (x, 0), 0), 0),
5790 GET_MODE_PRECISION (mode) - (i + 1)),
5791 GET_MODE_PRECISION (mode) - (i + 1));
5793 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5794 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5795 the bitsize of the mode - 1. This allows simplification of
5796 "a = (b & 8) == 0;" */
5797 if (XEXP (x, 1) == constm1_rtx
5798 && !REG_P (XEXP (x, 0))
5799 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5800 && REG_P (SUBREG_REG (XEXP (x, 0))))
5801 && nonzero_bits (XEXP (x, 0), mode) == 1)
5802 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5803 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5804 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5805 GET_MODE_PRECISION (mode) - 1),
5806 GET_MODE_PRECISION (mode) - 1);
5808 /* If we are adding two things that have no bits in common, convert
5809 the addition into an IOR. This will often be further simplified,
5810 for example in cases like ((a & 1) + (a & 2)), which can
5811 become a & 3. */
5813 if (HWI_COMPUTABLE_MODE_P (mode)
5814 && (nonzero_bits (XEXP (x, 0), mode)
5815 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5817 /* Try to simplify the expression further. */
5818 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5819 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5821 /* If we could, great. If not, do not go ahead with the IOR
5822 replacement, since PLUS appears in many special purpose
5823 address arithmetic instructions. */
5824 if (GET_CODE (temp) != CLOBBER
5825 && (GET_CODE (temp) != IOR
5826 || ((XEXP (temp, 0) != XEXP (x, 0)
5827 || XEXP (temp, 1) != XEXP (x, 1))
5828 && (XEXP (temp, 0) != XEXP (x, 1)
5829 || XEXP (temp, 1) != XEXP (x, 0)))))
5830 return temp;
5832 break;
5834 case MINUS:
5835 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5836 (and <foo> (const_int pow2-1)) */
5837 if (GET_CODE (XEXP (x, 1)) == AND
5838 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5839 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5840 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5841 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5842 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5843 break;
5845 case MULT:
5846 /* If we have (mult (plus A B) C), apply the distributive law and then
5847 the inverse distributive law to see if things simplify. This
5848 occurs mostly in addresses, often when unrolling loops. */
5850 if (GET_CODE (XEXP (x, 0)) == PLUS)
5852 rtx result = distribute_and_simplify_rtx (x, 0);
5853 if (result)
5854 return result;
5857 /* Try simplify a*(b/c) as (a*b)/c. */
5858 if (FLOAT_MODE_P (mode) && flag_associative_math
5859 && GET_CODE (XEXP (x, 0)) == DIV)
5861 rtx tem = simplify_binary_operation (MULT, mode,
5862 XEXP (XEXP (x, 0), 0),
5863 XEXP (x, 1));
5864 if (tem)
5865 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5867 break;
5869 case UDIV:
5870 /* If this is a divide by a power of two, treat it as a shift if
5871 its first operand is a shift. */
5872 if (CONST_INT_P (XEXP (x, 1))
5873 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5874 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5875 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5876 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5877 || GET_CODE (XEXP (x, 0)) == ROTATE
5878 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5879 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5880 break;
5882 case EQ: case NE:
5883 case GT: case GTU: case GE: case GEU:
5884 case LT: case LTU: case LE: case LEU:
5885 case UNEQ: case LTGT:
5886 case UNGT: case UNGE:
5887 case UNLT: case UNLE:
5888 case UNORDERED: case ORDERED:
5889 /* If the first operand is a condition code, we can't do anything
5890 with it. */
5891 if (GET_CODE (XEXP (x, 0)) == COMPARE
5892 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5893 && ! CC0_P (XEXP (x, 0))))
5895 rtx op0 = XEXP (x, 0);
5896 rtx op1 = XEXP (x, 1);
5897 enum rtx_code new_code;
5899 if (GET_CODE (op0) == COMPARE)
5900 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5902 /* Simplify our comparison, if possible. */
5903 new_code = simplify_comparison (code, &op0, &op1);
5905 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5906 if only the low-order bit is possibly nonzero in X (such as when
5907 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5908 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5909 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5910 (plus X 1).
5912 Remove any ZERO_EXTRACT we made when thinking this was a
5913 comparison. It may now be simpler to use, e.g., an AND. If a
5914 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5915 the call to make_compound_operation in the SET case.
5917 Don't apply these optimizations if the caller would
5918 prefer a comparison rather than a value.
5919 E.g., for the condition in an IF_THEN_ELSE most targets need
5920 an explicit comparison. */
5922 if (in_cond)
5925 else if (STORE_FLAG_VALUE == 1
5926 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5927 && op1 == const0_rtx
5928 && mode == GET_MODE (op0)
5929 && nonzero_bits (op0, mode) == 1)
5930 return gen_lowpart (mode,
5931 expand_compound_operation (op0));
5933 else if (STORE_FLAG_VALUE == 1
5934 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5935 && op1 == const0_rtx
5936 && mode == GET_MODE (op0)
5937 && (num_sign_bit_copies (op0, mode)
5938 == GET_MODE_PRECISION (mode)))
5940 op0 = expand_compound_operation (op0);
5941 return simplify_gen_unary (NEG, mode,
5942 gen_lowpart (mode, op0),
5943 mode);
5946 else if (STORE_FLAG_VALUE == 1
5947 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5948 && op1 == const0_rtx
5949 && mode == GET_MODE (op0)
5950 && nonzero_bits (op0, mode) == 1)
5952 op0 = expand_compound_operation (op0);
5953 return simplify_gen_binary (XOR, mode,
5954 gen_lowpart (mode, op0),
5955 const1_rtx);
5958 else if (STORE_FLAG_VALUE == 1
5959 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5960 && op1 == const0_rtx
5961 && mode == GET_MODE (op0)
5962 && (num_sign_bit_copies (op0, mode)
5963 == GET_MODE_PRECISION (mode)))
5965 op0 = expand_compound_operation (op0);
5966 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5969 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5970 those above. */
5971 if (in_cond)
5974 else if (STORE_FLAG_VALUE == -1
5975 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5976 && op1 == const0_rtx
5977 && mode == GET_MODE (op0)
5978 && (num_sign_bit_copies (op0, mode)
5979 == GET_MODE_PRECISION (mode)))
5980 return gen_lowpart (mode,
5981 expand_compound_operation (op0));
5983 else if (STORE_FLAG_VALUE == -1
5984 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5985 && op1 == const0_rtx
5986 && mode == GET_MODE (op0)
5987 && nonzero_bits (op0, mode) == 1)
5989 op0 = expand_compound_operation (op0);
5990 return simplify_gen_unary (NEG, mode,
5991 gen_lowpart (mode, op0),
5992 mode);
5995 else if (STORE_FLAG_VALUE == -1
5996 && new_code == EQ && 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 (NOT, mode,
6004 gen_lowpart (mode, op0),
6005 mode);
6008 /* If X is 0/1, (eq X 0) is X-1. */
6009 else if (STORE_FLAG_VALUE == -1
6010 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6011 && op1 == const0_rtx
6012 && mode == GET_MODE (op0)
6013 && nonzero_bits (op0, mode) == 1)
6015 op0 = expand_compound_operation (op0);
6016 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6019 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6020 one bit that might be nonzero, we can convert (ne x 0) to
6021 (ashift x c) where C puts the bit in the sign bit. Remove any
6022 AND with STORE_FLAG_VALUE when we are done, since we are only
6023 going to test the sign bit. */
6024 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6025 && HWI_COMPUTABLE_MODE_P (mode)
6026 && val_signbit_p (mode, STORE_FLAG_VALUE)
6027 && op1 == const0_rtx
6028 && mode == GET_MODE (op0)
6029 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6031 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6032 expand_compound_operation (op0),
6033 GET_MODE_PRECISION (mode) - 1 - i);
6034 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6035 return XEXP (x, 0);
6036 else
6037 return x;
6040 /* If the code changed, return a whole new comparison.
6041 We also need to avoid using SUBST in cases where
6042 simplify_comparison has widened a comparison with a CONST_INT,
6043 since in that case the wider CONST_INT may fail the sanity
6044 checks in do_SUBST. */
6045 if (new_code != code
6046 || (CONST_INT_P (op1)
6047 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6048 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6049 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6051 /* Otherwise, keep this operation, but maybe change its operands.
6052 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6053 SUBST (XEXP (x, 0), op0);
6054 SUBST (XEXP (x, 1), op1);
6056 break;
6058 case IF_THEN_ELSE:
6059 return simplify_if_then_else (x);
6061 case ZERO_EXTRACT:
6062 case SIGN_EXTRACT:
6063 case ZERO_EXTEND:
6064 case SIGN_EXTEND:
6065 /* If we are processing SET_DEST, we are done. */
6066 if (in_dest)
6067 return x;
6069 return expand_compound_operation (x);
6071 case SET:
6072 return simplify_set (x);
6074 case AND:
6075 case IOR:
6076 return simplify_logical (x);
6078 case ASHIFT:
6079 case LSHIFTRT:
6080 case ASHIFTRT:
6081 case ROTATE:
6082 case ROTATERT:
6083 /* If this is a shift by a constant amount, simplify it. */
6084 if (CONST_INT_P (XEXP (x, 1)))
6085 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6086 INTVAL (XEXP (x, 1)));
6088 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6089 SUBST (XEXP (x, 1),
6090 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6091 ((unsigned HOST_WIDE_INT) 1
6092 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6093 - 1,
6094 0));
6095 break;
6097 default:
6098 break;
6101 return x;
6104 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6106 static rtx
6107 simplify_if_then_else (rtx x)
6109 machine_mode mode = GET_MODE (x);
6110 rtx cond = XEXP (x, 0);
6111 rtx true_rtx = XEXP (x, 1);
6112 rtx false_rtx = XEXP (x, 2);
6113 enum rtx_code true_code = GET_CODE (cond);
6114 int comparison_p = COMPARISON_P (cond);
6115 rtx temp;
6116 int i;
6117 enum rtx_code false_code;
6118 rtx reversed;
6120 /* Simplify storing of the truth value. */
6121 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6122 return simplify_gen_relational (true_code, mode, VOIDmode,
6123 XEXP (cond, 0), XEXP (cond, 1));
6125 /* Also when the truth value has to be reversed. */
6126 if (comparison_p
6127 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6128 && (reversed = reversed_comparison (cond, mode)))
6129 return reversed;
6131 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6132 in it is being compared against certain values. Get the true and false
6133 comparisons and see if that says anything about the value of each arm. */
6135 if (comparison_p
6136 && ((false_code = reversed_comparison_code (cond, NULL))
6137 != UNKNOWN)
6138 && REG_P (XEXP (cond, 0)))
6140 HOST_WIDE_INT nzb;
6141 rtx from = XEXP (cond, 0);
6142 rtx true_val = XEXP (cond, 1);
6143 rtx false_val = true_val;
6144 int swapped = 0;
6146 /* If FALSE_CODE is EQ, swap the codes and arms. */
6148 if (false_code == EQ)
6150 swapped = 1, true_code = EQ, false_code = NE;
6151 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6154 /* If we are comparing against zero and the expression being tested has
6155 only a single bit that might be nonzero, that is its value when it is
6156 not equal to zero. Similarly if it is known to be -1 or 0. */
6158 if (true_code == EQ && true_val == const0_rtx
6159 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6161 false_code = EQ;
6162 false_val = gen_int_mode (nzb, GET_MODE (from));
6164 else if (true_code == EQ && true_val == const0_rtx
6165 && (num_sign_bit_copies (from, GET_MODE (from))
6166 == GET_MODE_PRECISION (GET_MODE (from))))
6168 false_code = EQ;
6169 false_val = constm1_rtx;
6172 /* Now simplify an arm if we know the value of the register in the
6173 branch and it is used in the arm. Be careful due to the potential
6174 of locally-shared RTL. */
6176 if (reg_mentioned_p (from, true_rtx))
6177 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6178 from, true_val),
6179 pc_rtx, pc_rtx, 0, 0, 0);
6180 if (reg_mentioned_p (from, false_rtx))
6181 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6182 from, false_val),
6183 pc_rtx, pc_rtx, 0, 0, 0);
6185 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6186 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6188 true_rtx = XEXP (x, 1);
6189 false_rtx = XEXP (x, 2);
6190 true_code = GET_CODE (cond);
6193 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6194 reversed, do so to avoid needing two sets of patterns for
6195 subtract-and-branch insns. Similarly if we have a constant in the true
6196 arm, the false arm is the same as the first operand of the comparison, or
6197 the false arm is more complicated than the true arm. */
6199 if (comparison_p
6200 && reversed_comparison_code (cond, NULL) != UNKNOWN
6201 && (true_rtx == pc_rtx
6202 || (CONSTANT_P (true_rtx)
6203 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6204 || true_rtx == const0_rtx
6205 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6206 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6207 && !OBJECT_P (false_rtx))
6208 || reg_mentioned_p (true_rtx, false_rtx)
6209 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6211 true_code = reversed_comparison_code (cond, NULL);
6212 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6213 SUBST (XEXP (x, 1), false_rtx);
6214 SUBST (XEXP (x, 2), true_rtx);
6216 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6217 cond = XEXP (x, 0);
6219 /* It is possible that the conditional has been simplified out. */
6220 true_code = GET_CODE (cond);
6221 comparison_p = COMPARISON_P (cond);
6224 /* If the two arms are identical, we don't need the comparison. */
6226 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6227 return true_rtx;
6229 /* Convert a == b ? b : a to "a". */
6230 if (true_code == EQ && ! side_effects_p (cond)
6231 && !HONOR_NANS (mode)
6232 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6233 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6234 return false_rtx;
6235 else if (true_code == NE && ! side_effects_p (cond)
6236 && !HONOR_NANS (mode)
6237 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6238 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6239 return true_rtx;
6241 /* Look for cases where we have (abs x) or (neg (abs X)). */
6243 if (GET_MODE_CLASS (mode) == MODE_INT
6244 && comparison_p
6245 && XEXP (cond, 1) == const0_rtx
6246 && GET_CODE (false_rtx) == NEG
6247 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6248 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6249 && ! side_effects_p (true_rtx))
6250 switch (true_code)
6252 case GT:
6253 case GE:
6254 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6255 case LT:
6256 case LE:
6257 return
6258 simplify_gen_unary (NEG, mode,
6259 simplify_gen_unary (ABS, mode, true_rtx, mode),
6260 mode);
6261 default:
6262 break;
6265 /* Look for MIN or MAX. */
6267 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6268 && comparison_p
6269 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6270 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6271 && ! side_effects_p (cond))
6272 switch (true_code)
6274 case GE:
6275 case GT:
6276 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6277 case LE:
6278 case LT:
6279 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6280 case GEU:
6281 case GTU:
6282 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6283 case LEU:
6284 case LTU:
6285 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6286 default:
6287 break;
6290 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6291 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6292 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6293 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6294 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6295 neither 1 or -1, but it isn't worth checking for. */
6297 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6298 && comparison_p
6299 && GET_MODE_CLASS (mode) == MODE_INT
6300 && ! side_effects_p (x))
6302 rtx t = make_compound_operation (true_rtx, SET);
6303 rtx f = make_compound_operation (false_rtx, SET);
6304 rtx cond_op0 = XEXP (cond, 0);
6305 rtx cond_op1 = XEXP (cond, 1);
6306 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6307 machine_mode m = mode;
6308 rtx z = 0, c1 = NULL_RTX;
6310 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6311 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6312 || GET_CODE (t) == ASHIFT
6313 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6314 && rtx_equal_p (XEXP (t, 0), f))
6315 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6317 /* If an identity-zero op is commutative, check whether there
6318 would be a match if we swapped the operands. */
6319 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6320 || GET_CODE (t) == XOR)
6321 && rtx_equal_p (XEXP (t, 1), f))
6322 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6323 else if (GET_CODE (t) == SIGN_EXTEND
6324 && (GET_CODE (XEXP (t, 0)) == PLUS
6325 || GET_CODE (XEXP (t, 0)) == MINUS
6326 || GET_CODE (XEXP (t, 0)) == IOR
6327 || GET_CODE (XEXP (t, 0)) == XOR
6328 || GET_CODE (XEXP (t, 0)) == ASHIFT
6329 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6330 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6331 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6332 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6333 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6334 && (num_sign_bit_copies (f, GET_MODE (f))
6335 > (unsigned int)
6336 (GET_MODE_PRECISION (mode)
6337 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6339 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6340 extend_op = SIGN_EXTEND;
6341 m = GET_MODE (XEXP (t, 0));
6343 else if (GET_CODE (t) == SIGN_EXTEND
6344 && (GET_CODE (XEXP (t, 0)) == PLUS
6345 || GET_CODE (XEXP (t, 0)) == IOR
6346 || GET_CODE (XEXP (t, 0)) == XOR)
6347 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6348 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6349 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6350 && (num_sign_bit_copies (f, GET_MODE (f))
6351 > (unsigned int)
6352 (GET_MODE_PRECISION (mode)
6353 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6355 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6356 extend_op = SIGN_EXTEND;
6357 m = GET_MODE (XEXP (t, 0));
6359 else if (GET_CODE (t) == ZERO_EXTEND
6360 && (GET_CODE (XEXP (t, 0)) == PLUS
6361 || GET_CODE (XEXP (t, 0)) == MINUS
6362 || GET_CODE (XEXP (t, 0)) == IOR
6363 || GET_CODE (XEXP (t, 0)) == XOR
6364 || GET_CODE (XEXP (t, 0)) == ASHIFT
6365 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6366 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6367 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6368 && HWI_COMPUTABLE_MODE_P (mode)
6369 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6370 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6371 && ((nonzero_bits (f, GET_MODE (f))
6372 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6373 == 0))
6375 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6376 extend_op = ZERO_EXTEND;
6377 m = GET_MODE (XEXP (t, 0));
6379 else if (GET_CODE (t) == ZERO_EXTEND
6380 && (GET_CODE (XEXP (t, 0)) == PLUS
6381 || GET_CODE (XEXP (t, 0)) == IOR
6382 || GET_CODE (XEXP (t, 0)) == XOR)
6383 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6384 && HWI_COMPUTABLE_MODE_P (mode)
6385 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6386 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6387 && ((nonzero_bits (f, GET_MODE (f))
6388 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6389 == 0))
6391 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6392 extend_op = ZERO_EXTEND;
6393 m = GET_MODE (XEXP (t, 0));
6396 if (z)
6398 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6399 cond_op0, cond_op1),
6400 pc_rtx, pc_rtx, 0, 0, 0);
6401 temp = simplify_gen_binary (MULT, m, temp,
6402 simplify_gen_binary (MULT, m, c1,
6403 const_true_rtx));
6404 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6405 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6407 if (extend_op != UNKNOWN)
6408 temp = simplify_gen_unary (extend_op, mode, temp, m);
6410 return temp;
6414 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6415 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6416 negation of a single bit, we can convert this operation to a shift. We
6417 can actually do this more generally, but it doesn't seem worth it. */
6419 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6420 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6421 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6422 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6423 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6424 == GET_MODE_PRECISION (mode))
6425 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6426 return
6427 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6428 gen_lowpart (mode, XEXP (cond, 0)), i);
6430 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6431 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6432 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6433 && GET_MODE (XEXP (cond, 0)) == mode
6434 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6435 == nonzero_bits (XEXP (cond, 0), mode)
6436 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6437 return XEXP (cond, 0);
6439 return x;
6442 /* Simplify X, a SET expression. Return the new expression. */
6444 static rtx
6445 simplify_set (rtx x)
6447 rtx src = SET_SRC (x);
6448 rtx dest = SET_DEST (x);
6449 machine_mode mode
6450 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6451 rtx_insn *other_insn;
6452 rtx *cc_use;
6454 /* (set (pc) (return)) gets written as (return). */
6455 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6456 return src;
6458 /* Now that we know for sure which bits of SRC we are using, see if we can
6459 simplify the expression for the object knowing that we only need the
6460 low-order bits. */
6462 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6464 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6465 SUBST (SET_SRC (x), src);
6468 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6469 the comparison result and try to simplify it unless we already have used
6470 undobuf.other_insn. */
6471 if ((GET_MODE_CLASS (mode) == MODE_CC
6472 || GET_CODE (src) == COMPARE
6473 || CC0_P (dest))
6474 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6475 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6476 && COMPARISON_P (*cc_use)
6477 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6479 enum rtx_code old_code = GET_CODE (*cc_use);
6480 enum rtx_code new_code;
6481 rtx op0, op1, tmp;
6482 int other_changed = 0;
6483 rtx inner_compare = NULL_RTX;
6484 machine_mode compare_mode = GET_MODE (dest);
6486 if (GET_CODE (src) == COMPARE)
6488 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6489 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6491 inner_compare = op0;
6492 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6495 else
6496 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6498 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6499 op0, op1);
6500 if (!tmp)
6501 new_code = old_code;
6502 else if (!CONSTANT_P (tmp))
6504 new_code = GET_CODE (tmp);
6505 op0 = XEXP (tmp, 0);
6506 op1 = XEXP (tmp, 1);
6508 else
6510 rtx pat = PATTERN (other_insn);
6511 undobuf.other_insn = other_insn;
6512 SUBST (*cc_use, tmp);
6514 /* Attempt to simplify CC user. */
6515 if (GET_CODE (pat) == SET)
6517 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6518 if (new_rtx != NULL_RTX)
6519 SUBST (SET_SRC (pat), new_rtx);
6522 /* Convert X into a no-op move. */
6523 SUBST (SET_DEST (x), pc_rtx);
6524 SUBST (SET_SRC (x), pc_rtx);
6525 return x;
6528 /* Simplify our comparison, if possible. */
6529 new_code = simplify_comparison (new_code, &op0, &op1);
6531 #ifdef SELECT_CC_MODE
6532 /* If this machine has CC modes other than CCmode, check to see if we
6533 need to use a different CC mode here. */
6534 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6535 compare_mode = GET_MODE (op0);
6536 else if (inner_compare
6537 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6538 && new_code == old_code
6539 && op0 == XEXP (inner_compare, 0)
6540 && op1 == XEXP (inner_compare, 1))
6541 compare_mode = GET_MODE (inner_compare);
6542 else
6543 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6545 #ifndef HAVE_cc0
6546 /* If the mode changed, we have to change SET_DEST, the mode in the
6547 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6548 a hard register, just build new versions with the proper mode. If it
6549 is a pseudo, we lose unless it is only time we set the pseudo, in
6550 which case we can safely change its mode. */
6551 if (compare_mode != GET_MODE (dest))
6553 if (can_change_dest_mode (dest, 0, compare_mode))
6555 unsigned int regno = REGNO (dest);
6556 rtx new_dest;
6558 if (regno < FIRST_PSEUDO_REGISTER)
6559 new_dest = gen_rtx_REG (compare_mode, regno);
6560 else
6562 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6563 new_dest = regno_reg_rtx[regno];
6566 SUBST (SET_DEST (x), new_dest);
6567 SUBST (XEXP (*cc_use, 0), new_dest);
6568 other_changed = 1;
6570 dest = new_dest;
6573 #endif /* cc0 */
6574 #endif /* SELECT_CC_MODE */
6576 /* If the code changed, we have to build a new comparison in
6577 undobuf.other_insn. */
6578 if (new_code != old_code)
6580 int other_changed_previously = other_changed;
6581 unsigned HOST_WIDE_INT mask;
6582 rtx old_cc_use = *cc_use;
6584 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6585 dest, const0_rtx));
6586 other_changed = 1;
6588 /* If the only change we made was to change an EQ into an NE or
6589 vice versa, OP0 has only one bit that might be nonzero, and OP1
6590 is zero, check if changing the user of the condition code will
6591 produce a valid insn. If it won't, we can keep the original code
6592 in that insn by surrounding our operation with an XOR. */
6594 if (((old_code == NE && new_code == EQ)
6595 || (old_code == EQ && new_code == NE))
6596 && ! other_changed_previously && op1 == const0_rtx
6597 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6598 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6600 rtx pat = PATTERN (other_insn), note = 0;
6602 if ((recog_for_combine (&pat, other_insn, &note) < 0
6603 && ! check_asm_operands (pat)))
6605 *cc_use = old_cc_use;
6606 other_changed = 0;
6608 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6609 gen_int_mode (mask,
6610 GET_MODE (op0)));
6615 if (other_changed)
6616 undobuf.other_insn = other_insn;
6618 /* Otherwise, if we didn't previously have a COMPARE in the
6619 correct mode, we need one. */
6620 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6622 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6623 src = SET_SRC (x);
6625 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6627 SUBST (SET_SRC (x), op0);
6628 src = SET_SRC (x);
6630 /* Otherwise, update the COMPARE if needed. */
6631 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6633 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6634 src = SET_SRC (x);
6637 else
6639 /* Get SET_SRC in a form where we have placed back any
6640 compound expressions. Then do the checks below. */
6641 src = make_compound_operation (src, SET);
6642 SUBST (SET_SRC (x), src);
6645 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6646 and X being a REG or (subreg (reg)), we may be able to convert this to
6647 (set (subreg:m2 x) (op)).
6649 We can always do this if M1 is narrower than M2 because that means that
6650 we only care about the low bits of the result.
6652 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6653 perform a narrower operation than requested since the high-order bits will
6654 be undefined. On machine where it is defined, this transformation is safe
6655 as long as M1 and M2 have the same number of words. */
6657 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6658 && !OBJECT_P (SUBREG_REG (src))
6659 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6660 / UNITS_PER_WORD)
6661 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6662 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6663 #ifndef WORD_REGISTER_OPERATIONS
6664 && (GET_MODE_SIZE (GET_MODE (src))
6665 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6666 #endif
6667 #ifdef CANNOT_CHANGE_MODE_CLASS
6668 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6669 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6670 GET_MODE (SUBREG_REG (src)),
6671 GET_MODE (src)))
6672 #endif
6673 && (REG_P (dest)
6674 || (GET_CODE (dest) == SUBREG
6675 && REG_P (SUBREG_REG (dest)))))
6677 SUBST (SET_DEST (x),
6678 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6679 dest));
6680 SUBST (SET_SRC (x), SUBREG_REG (src));
6682 src = SET_SRC (x), dest = SET_DEST (x);
6685 #ifdef HAVE_cc0
6686 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6687 in SRC. */
6688 if (dest == cc0_rtx
6689 && GET_CODE (src) == SUBREG
6690 && subreg_lowpart_p (src)
6691 && (GET_MODE_PRECISION (GET_MODE (src))
6692 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6694 rtx inner = SUBREG_REG (src);
6695 machine_mode inner_mode = GET_MODE (inner);
6697 /* Here we make sure that we don't have a sign bit on. */
6698 if (val_signbit_known_clear_p (GET_MODE (src),
6699 nonzero_bits (inner, inner_mode)))
6701 SUBST (SET_SRC (x), inner);
6702 src = SET_SRC (x);
6705 #endif
6707 #ifdef LOAD_EXTEND_OP
6708 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6709 would require a paradoxical subreg. Replace the subreg with a
6710 zero_extend to avoid the reload that would otherwise be required. */
6712 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6713 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6714 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6715 && SUBREG_BYTE (src) == 0
6716 && paradoxical_subreg_p (src)
6717 && MEM_P (SUBREG_REG (src)))
6719 SUBST (SET_SRC (x),
6720 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6721 GET_MODE (src), SUBREG_REG (src)));
6723 src = SET_SRC (x);
6725 #endif
6727 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6728 are comparing an item known to be 0 or -1 against 0, use a logical
6729 operation instead. Check for one of the arms being an IOR of the other
6730 arm with some value. We compute three terms to be IOR'ed together. In
6731 practice, at most two will be nonzero. Then we do the IOR's. */
6733 if (GET_CODE (dest) != PC
6734 && GET_CODE (src) == IF_THEN_ELSE
6735 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6736 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6737 && XEXP (XEXP (src, 0), 1) == const0_rtx
6738 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6739 #ifdef HAVE_conditional_move
6740 && ! can_conditionally_move_p (GET_MODE (src))
6741 #endif
6742 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6743 GET_MODE (XEXP (XEXP (src, 0), 0)))
6744 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6745 && ! side_effects_p (src))
6747 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6748 ? XEXP (src, 1) : XEXP (src, 2));
6749 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6750 ? XEXP (src, 2) : XEXP (src, 1));
6751 rtx term1 = const0_rtx, term2, term3;
6753 if (GET_CODE (true_rtx) == IOR
6754 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6755 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6756 else if (GET_CODE (true_rtx) == IOR
6757 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6758 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6759 else if (GET_CODE (false_rtx) == IOR
6760 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6761 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6762 else if (GET_CODE (false_rtx) == IOR
6763 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6764 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6766 term2 = simplify_gen_binary (AND, GET_MODE (src),
6767 XEXP (XEXP (src, 0), 0), true_rtx);
6768 term3 = simplify_gen_binary (AND, GET_MODE (src),
6769 simplify_gen_unary (NOT, GET_MODE (src),
6770 XEXP (XEXP (src, 0), 0),
6771 GET_MODE (src)),
6772 false_rtx);
6774 SUBST (SET_SRC (x),
6775 simplify_gen_binary (IOR, GET_MODE (src),
6776 simplify_gen_binary (IOR, GET_MODE (src),
6777 term1, term2),
6778 term3));
6780 src = SET_SRC (x);
6783 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6784 whole thing fail. */
6785 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6786 return src;
6787 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6788 return dest;
6789 else
6790 /* Convert this into a field assignment operation, if possible. */
6791 return make_field_assignment (x);
6794 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6795 result. */
6797 static rtx
6798 simplify_logical (rtx x)
6800 machine_mode mode = GET_MODE (x);
6801 rtx op0 = XEXP (x, 0);
6802 rtx op1 = XEXP (x, 1);
6804 switch (GET_CODE (x))
6806 case AND:
6807 /* We can call simplify_and_const_int only if we don't lose
6808 any (sign) bits when converting INTVAL (op1) to
6809 "unsigned HOST_WIDE_INT". */
6810 if (CONST_INT_P (op1)
6811 && (HWI_COMPUTABLE_MODE_P (mode)
6812 || INTVAL (op1) > 0))
6814 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6815 if (GET_CODE (x) != AND)
6816 return x;
6818 op0 = XEXP (x, 0);
6819 op1 = XEXP (x, 1);
6822 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6823 apply the distributive law and then the inverse distributive
6824 law to see if things simplify. */
6825 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6827 rtx result = distribute_and_simplify_rtx (x, 0);
6828 if (result)
6829 return result;
6831 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6833 rtx result = distribute_and_simplify_rtx (x, 1);
6834 if (result)
6835 return result;
6837 break;
6839 case IOR:
6840 /* If we have (ior (and A B) C), apply the distributive law and then
6841 the inverse distributive law to see if things simplify. */
6843 if (GET_CODE (op0) == AND)
6845 rtx result = distribute_and_simplify_rtx (x, 0);
6846 if (result)
6847 return result;
6850 if (GET_CODE (op1) == AND)
6852 rtx result = distribute_and_simplify_rtx (x, 1);
6853 if (result)
6854 return result;
6856 break;
6858 default:
6859 gcc_unreachable ();
6862 return x;
6865 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6866 operations" because they can be replaced with two more basic operations.
6867 ZERO_EXTEND is also considered "compound" because it can be replaced with
6868 an AND operation, which is simpler, though only one operation.
6870 The function expand_compound_operation is called with an rtx expression
6871 and will convert it to the appropriate shifts and AND operations,
6872 simplifying at each stage.
6874 The function make_compound_operation is called to convert an expression
6875 consisting of shifts and ANDs into the equivalent compound expression.
6876 It is the inverse of this function, loosely speaking. */
6878 static rtx
6879 expand_compound_operation (rtx x)
6881 unsigned HOST_WIDE_INT pos = 0, len;
6882 int unsignedp = 0;
6883 unsigned int modewidth;
6884 rtx tem;
6886 switch (GET_CODE (x))
6888 case ZERO_EXTEND:
6889 unsignedp = 1;
6890 case SIGN_EXTEND:
6891 /* We can't necessarily use a const_int for a multiword mode;
6892 it depends on implicitly extending the value.
6893 Since we don't know the right way to extend it,
6894 we can't tell whether the implicit way is right.
6896 Even for a mode that is no wider than a const_int,
6897 we can't win, because we need to sign extend one of its bits through
6898 the rest of it, and we don't know which bit. */
6899 if (CONST_INT_P (XEXP (x, 0)))
6900 return x;
6902 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6903 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6904 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6905 reloaded. If not for that, MEM's would very rarely be safe.
6907 Reject MODEs bigger than a word, because we might not be able
6908 to reference a two-register group starting with an arbitrary register
6909 (and currently gen_lowpart might crash for a SUBREG). */
6911 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6912 return x;
6914 /* Reject MODEs that aren't scalar integers because turning vector
6915 or complex modes into shifts causes problems. */
6917 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6918 return x;
6920 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6921 /* If the inner object has VOIDmode (the only way this can happen
6922 is if it is an ASM_OPERANDS), we can't do anything since we don't
6923 know how much masking to do. */
6924 if (len == 0)
6925 return x;
6927 break;
6929 case ZERO_EXTRACT:
6930 unsignedp = 1;
6932 /* ... fall through ... */
6934 case SIGN_EXTRACT:
6935 /* If the operand is a CLOBBER, just return it. */
6936 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6937 return XEXP (x, 0);
6939 if (!CONST_INT_P (XEXP (x, 1))
6940 || !CONST_INT_P (XEXP (x, 2))
6941 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6942 return x;
6944 /* Reject MODEs that aren't scalar integers because turning vector
6945 or complex modes into shifts causes problems. */
6947 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6948 return x;
6950 len = INTVAL (XEXP (x, 1));
6951 pos = INTVAL (XEXP (x, 2));
6953 /* This should stay within the object being extracted, fail otherwise. */
6954 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6955 return x;
6957 if (BITS_BIG_ENDIAN)
6958 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6960 break;
6962 default:
6963 return x;
6965 /* Convert sign extension to zero extension, if we know that the high
6966 bit is not set, as this is easier to optimize. It will be converted
6967 back to cheaper alternative in make_extraction. */
6968 if (GET_CODE (x) == SIGN_EXTEND
6969 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6970 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6971 & ~(((unsigned HOST_WIDE_INT)
6972 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6973 >> 1))
6974 == 0)))
6976 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6977 rtx temp2 = expand_compound_operation (temp);
6979 /* Make sure this is a profitable operation. */
6980 if (set_src_cost (x, optimize_this_for_speed_p)
6981 > set_src_cost (temp2, optimize_this_for_speed_p))
6982 return temp2;
6983 else if (set_src_cost (x, optimize_this_for_speed_p)
6984 > set_src_cost (temp, optimize_this_for_speed_p))
6985 return temp;
6986 else
6987 return x;
6990 /* We can optimize some special cases of ZERO_EXTEND. */
6991 if (GET_CODE (x) == ZERO_EXTEND)
6993 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6994 know that the last value didn't have any inappropriate bits
6995 set. */
6996 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6997 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6998 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6999 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
7000 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7001 return XEXP (XEXP (x, 0), 0);
7003 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7004 if (GET_CODE (XEXP (x, 0)) == SUBREG
7005 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7006 && subreg_lowpart_p (XEXP (x, 0))
7007 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7008 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
7009 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7010 return SUBREG_REG (XEXP (x, 0));
7012 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7013 is a comparison and STORE_FLAG_VALUE permits. This is like
7014 the first case, but it works even when GET_MODE (x) is larger
7015 than HOST_WIDE_INT. */
7016 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7017 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7018 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7019 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7020 <= HOST_BITS_PER_WIDE_INT)
7021 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7022 return XEXP (XEXP (x, 0), 0);
7024 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7025 if (GET_CODE (XEXP (x, 0)) == SUBREG
7026 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7027 && subreg_lowpart_p (XEXP (x, 0))
7028 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7029 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7030 <= HOST_BITS_PER_WIDE_INT)
7031 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7032 return SUBREG_REG (XEXP (x, 0));
7036 /* If we reach here, we want to return a pair of shifts. The inner
7037 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7038 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7039 logical depending on the value of UNSIGNEDP.
7041 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7042 converted into an AND of a shift.
7044 We must check for the case where the left shift would have a negative
7045 count. This can happen in a case like (x >> 31) & 255 on machines
7046 that can't shift by a constant. On those machines, we would first
7047 combine the shift with the AND to produce a variable-position
7048 extraction. Then the constant of 31 would be substituted in
7049 to produce such a position. */
7051 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7052 if (modewidth >= pos + len)
7054 machine_mode mode = GET_MODE (x);
7055 tem = gen_lowpart (mode, XEXP (x, 0));
7056 if (!tem || GET_CODE (tem) == CLOBBER)
7057 return x;
7058 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7059 tem, modewidth - pos - len);
7060 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7061 mode, tem, modewidth - len);
7063 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7064 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7065 simplify_shift_const (NULL_RTX, LSHIFTRT,
7066 GET_MODE (x),
7067 XEXP (x, 0), pos),
7068 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7069 else
7070 /* Any other cases we can't handle. */
7071 return x;
7073 /* If we couldn't do this for some reason, return the original
7074 expression. */
7075 if (GET_CODE (tem) == CLOBBER)
7076 return x;
7078 return tem;
7081 /* X is a SET which contains an assignment of one object into
7082 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7083 or certain SUBREGS). If possible, convert it into a series of
7084 logical operations.
7086 We half-heartedly support variable positions, but do not at all
7087 support variable lengths. */
7089 static const_rtx
7090 expand_field_assignment (const_rtx x)
7092 rtx inner;
7093 rtx pos; /* Always counts from low bit. */
7094 int len;
7095 rtx mask, cleared, masked;
7096 machine_mode compute_mode;
7098 /* Loop until we find something we can't simplify. */
7099 while (1)
7101 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7102 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7104 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7105 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7106 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7108 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7109 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7111 inner = XEXP (SET_DEST (x), 0);
7112 len = INTVAL (XEXP (SET_DEST (x), 1));
7113 pos = XEXP (SET_DEST (x), 2);
7115 /* A constant position should stay within the width of INNER. */
7116 if (CONST_INT_P (pos)
7117 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7118 break;
7120 if (BITS_BIG_ENDIAN)
7122 if (CONST_INT_P (pos))
7123 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7124 - INTVAL (pos));
7125 else if (GET_CODE (pos) == MINUS
7126 && CONST_INT_P (XEXP (pos, 1))
7127 && (INTVAL (XEXP (pos, 1))
7128 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7129 /* If position is ADJUST - X, new position is X. */
7130 pos = XEXP (pos, 0);
7131 else
7133 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7134 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7135 gen_int_mode (prec - len,
7136 GET_MODE (pos)),
7137 pos);
7142 /* A SUBREG between two modes that occupy the same numbers of words
7143 can be done by moving the SUBREG to the source. */
7144 else if (GET_CODE (SET_DEST (x)) == SUBREG
7145 /* We need SUBREGs to compute nonzero_bits properly. */
7146 && nonzero_sign_valid
7147 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7148 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7149 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7150 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7152 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7153 gen_lowpart
7154 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7155 SET_SRC (x)));
7156 continue;
7158 else
7159 break;
7161 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7162 inner = SUBREG_REG (inner);
7164 compute_mode = GET_MODE (inner);
7166 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7167 if (! SCALAR_INT_MODE_P (compute_mode))
7169 machine_mode imode;
7171 /* Don't do anything for vector or complex integral types. */
7172 if (! FLOAT_MODE_P (compute_mode))
7173 break;
7175 /* Try to find an integral mode to pun with. */
7176 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7177 if (imode == BLKmode)
7178 break;
7180 compute_mode = imode;
7181 inner = gen_lowpart (imode, inner);
7184 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7185 if (len >= HOST_BITS_PER_WIDE_INT)
7186 break;
7188 /* Now compute the equivalent expression. Make a copy of INNER
7189 for the SET_DEST in case it is a MEM into which we will substitute;
7190 we don't want shared RTL in that case. */
7191 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7192 compute_mode);
7193 cleared = simplify_gen_binary (AND, compute_mode,
7194 simplify_gen_unary (NOT, compute_mode,
7195 simplify_gen_binary (ASHIFT,
7196 compute_mode,
7197 mask, pos),
7198 compute_mode),
7199 inner);
7200 masked = simplify_gen_binary (ASHIFT, compute_mode,
7201 simplify_gen_binary (
7202 AND, compute_mode,
7203 gen_lowpart (compute_mode, SET_SRC (x)),
7204 mask),
7205 pos);
7207 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7208 simplify_gen_binary (IOR, compute_mode,
7209 cleared, masked));
7212 return x;
7215 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7216 it is an RTX that represents the (variable) starting position; otherwise,
7217 POS is the (constant) starting bit position. Both are counted from the LSB.
7219 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7221 IN_DEST is nonzero if this is a reference in the destination of a SET.
7222 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7223 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7224 be used.
7226 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7227 ZERO_EXTRACT should be built even for bits starting at bit 0.
7229 MODE is the desired mode of the result (if IN_DEST == 0).
7231 The result is an RTX for the extraction or NULL_RTX if the target
7232 can't handle it. */
7234 static rtx
7235 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7236 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7237 int in_dest, int in_compare)
7239 /* This mode describes the size of the storage area
7240 to fetch the overall value from. Within that, we
7241 ignore the POS lowest bits, etc. */
7242 machine_mode is_mode = GET_MODE (inner);
7243 machine_mode inner_mode;
7244 machine_mode wanted_inner_mode;
7245 machine_mode wanted_inner_reg_mode = word_mode;
7246 machine_mode pos_mode = word_mode;
7247 machine_mode extraction_mode = word_mode;
7248 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7249 rtx new_rtx = 0;
7250 rtx orig_pos_rtx = pos_rtx;
7251 HOST_WIDE_INT orig_pos;
7253 if (pos_rtx && CONST_INT_P (pos_rtx))
7254 pos = INTVAL (pos_rtx), pos_rtx = 0;
7256 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7258 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7259 consider just the QI as the memory to extract from.
7260 The subreg adds or removes high bits; its mode is
7261 irrelevant to the meaning of this extraction,
7262 since POS and LEN count from the lsb. */
7263 if (MEM_P (SUBREG_REG (inner)))
7264 is_mode = GET_MODE (SUBREG_REG (inner));
7265 inner = SUBREG_REG (inner);
7267 else if (GET_CODE (inner) == ASHIFT
7268 && CONST_INT_P (XEXP (inner, 1))
7269 && pos_rtx == 0 && pos == 0
7270 && len > UINTVAL (XEXP (inner, 1)))
7272 /* We're extracting the least significant bits of an rtx
7273 (ashift X (const_int C)), where LEN > C. Extract the
7274 least significant (LEN - C) bits of X, giving an rtx
7275 whose mode is MODE, then shift it left C times. */
7276 new_rtx = make_extraction (mode, XEXP (inner, 0),
7277 0, 0, len - INTVAL (XEXP (inner, 1)),
7278 unsignedp, in_dest, in_compare);
7279 if (new_rtx != 0)
7280 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7282 else if (GET_CODE (inner) == TRUNCATE)
7283 inner = XEXP (inner, 0);
7285 inner_mode = GET_MODE (inner);
7287 /* See if this can be done without an extraction. We never can if the
7288 width of the field is not the same as that of some integer mode. For
7289 registers, we can only avoid the extraction if the position is at the
7290 low-order bit and this is either not in the destination or we have the
7291 appropriate STRICT_LOW_PART operation available.
7293 For MEM, we can avoid an extract if the field starts on an appropriate
7294 boundary and we can change the mode of the memory reference. */
7296 if (tmode != BLKmode
7297 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7298 && !MEM_P (inner)
7299 && (inner_mode == tmode
7300 || !REG_P (inner)
7301 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7302 || reg_truncated_to_mode (tmode, inner))
7303 && (! in_dest
7304 || (REG_P (inner)
7305 && have_insn_for (STRICT_LOW_PART, tmode))))
7306 || (MEM_P (inner) && pos_rtx == 0
7307 && (pos
7308 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7309 : BITS_PER_UNIT)) == 0
7310 /* We can't do this if we are widening INNER_MODE (it
7311 may not be aligned, for one thing). */
7312 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7313 && (inner_mode == tmode
7314 || (! mode_dependent_address_p (XEXP (inner, 0),
7315 MEM_ADDR_SPACE (inner))
7316 && ! MEM_VOLATILE_P (inner))))))
7318 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7319 field. If the original and current mode are the same, we need not
7320 adjust the offset. Otherwise, we do if bytes big endian.
7322 If INNER is not a MEM, get a piece consisting of just the field
7323 of interest (in this case POS % BITS_PER_WORD must be 0). */
7325 if (MEM_P (inner))
7327 HOST_WIDE_INT offset;
7329 /* POS counts from lsb, but make OFFSET count in memory order. */
7330 if (BYTES_BIG_ENDIAN)
7331 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7332 else
7333 offset = pos / BITS_PER_UNIT;
7335 new_rtx = adjust_address_nv (inner, tmode, offset);
7337 else if (REG_P (inner))
7339 if (tmode != inner_mode)
7341 /* We can't call gen_lowpart in a DEST since we
7342 always want a SUBREG (see below) and it would sometimes
7343 return a new hard register. */
7344 if (pos || in_dest)
7346 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7348 if (WORDS_BIG_ENDIAN
7349 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7350 final_word = ((GET_MODE_SIZE (inner_mode)
7351 - GET_MODE_SIZE (tmode))
7352 / UNITS_PER_WORD) - final_word;
7354 final_word *= UNITS_PER_WORD;
7355 if (BYTES_BIG_ENDIAN &&
7356 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7357 final_word += (GET_MODE_SIZE (inner_mode)
7358 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7360 /* Avoid creating invalid subregs, for example when
7361 simplifying (x>>32)&255. */
7362 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7363 return NULL_RTX;
7365 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7367 else
7368 new_rtx = gen_lowpart (tmode, inner);
7370 else
7371 new_rtx = inner;
7373 else
7374 new_rtx = force_to_mode (inner, tmode,
7375 len >= HOST_BITS_PER_WIDE_INT
7376 ? ~(unsigned HOST_WIDE_INT) 0
7377 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7380 /* If this extraction is going into the destination of a SET,
7381 make a STRICT_LOW_PART unless we made a MEM. */
7383 if (in_dest)
7384 return (MEM_P (new_rtx) ? new_rtx
7385 : (GET_CODE (new_rtx) != SUBREG
7386 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7387 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7389 if (mode == tmode)
7390 return new_rtx;
7392 if (CONST_SCALAR_INT_P (new_rtx))
7393 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7394 mode, new_rtx, tmode);
7396 /* If we know that no extraneous bits are set, and that the high
7397 bit is not set, convert the extraction to the cheaper of
7398 sign and zero extension, that are equivalent in these cases. */
7399 if (flag_expensive_optimizations
7400 && (HWI_COMPUTABLE_MODE_P (tmode)
7401 && ((nonzero_bits (new_rtx, tmode)
7402 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7403 == 0)))
7405 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7406 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7408 /* Prefer ZERO_EXTENSION, since it gives more information to
7409 backends. */
7410 if (set_src_cost (temp, optimize_this_for_speed_p)
7411 <= set_src_cost (temp1, optimize_this_for_speed_p))
7412 return temp;
7413 return temp1;
7416 /* Otherwise, sign- or zero-extend unless we already are in the
7417 proper mode. */
7419 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7420 mode, new_rtx));
7423 /* Unless this is a COMPARE or we have a funny memory reference,
7424 don't do anything with zero-extending field extracts starting at
7425 the low-order bit since they are simple AND operations. */
7426 if (pos_rtx == 0 && pos == 0 && ! in_dest
7427 && ! in_compare && unsignedp)
7428 return 0;
7430 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7431 if the position is not a constant and the length is not 1. In all
7432 other cases, we would only be going outside our object in cases when
7433 an original shift would have been undefined. */
7434 if (MEM_P (inner)
7435 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7436 || (pos_rtx != 0 && len != 1)))
7437 return 0;
7439 enum extraction_pattern pattern = (in_dest ? EP_insv
7440 : unsignedp ? EP_extzv : EP_extv);
7442 /* If INNER is not from memory, we want it to have the mode of a register
7443 extraction pattern's structure operand, or word_mode if there is no
7444 such pattern. The same applies to extraction_mode and pos_mode
7445 and their respective operands.
7447 For memory, assume that the desired extraction_mode and pos_mode
7448 are the same as for a register operation, since at present we don't
7449 have named patterns for aligned memory structures. */
7450 struct extraction_insn insn;
7451 if (get_best_reg_extraction_insn (&insn, pattern,
7452 GET_MODE_BITSIZE (inner_mode), mode))
7454 wanted_inner_reg_mode = insn.struct_mode;
7455 pos_mode = insn.pos_mode;
7456 extraction_mode = insn.field_mode;
7459 /* Never narrow an object, since that might not be safe. */
7461 if (mode != VOIDmode
7462 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7463 extraction_mode = mode;
7465 if (!MEM_P (inner))
7466 wanted_inner_mode = wanted_inner_reg_mode;
7467 else
7469 /* Be careful not to go beyond the extracted object and maintain the
7470 natural alignment of the memory. */
7471 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7472 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7473 > GET_MODE_BITSIZE (wanted_inner_mode))
7475 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7476 gcc_assert (wanted_inner_mode != VOIDmode);
7480 orig_pos = pos;
7482 if (BITS_BIG_ENDIAN)
7484 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7485 BITS_BIG_ENDIAN style. If position is constant, compute new
7486 position. Otherwise, build subtraction.
7487 Note that POS is relative to the mode of the original argument.
7488 If it's a MEM we need to recompute POS relative to that.
7489 However, if we're extracting from (or inserting into) a register,
7490 we want to recompute POS relative to wanted_inner_mode. */
7491 int width = (MEM_P (inner)
7492 ? GET_MODE_BITSIZE (is_mode)
7493 : GET_MODE_BITSIZE (wanted_inner_mode));
7495 if (pos_rtx == 0)
7496 pos = width - len - pos;
7497 else
7498 pos_rtx
7499 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7500 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7501 pos_rtx);
7502 /* POS may be less than 0 now, but we check for that below.
7503 Note that it can only be less than 0 if !MEM_P (inner). */
7506 /* If INNER has a wider mode, and this is a constant extraction, try to
7507 make it smaller and adjust the byte to point to the byte containing
7508 the value. */
7509 if (wanted_inner_mode != VOIDmode
7510 && inner_mode != wanted_inner_mode
7511 && ! pos_rtx
7512 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7513 && MEM_P (inner)
7514 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7515 && ! MEM_VOLATILE_P (inner))
7517 int offset = 0;
7519 /* The computations below will be correct if the machine is big
7520 endian in both bits and bytes or little endian in bits and bytes.
7521 If it is mixed, we must adjust. */
7523 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7524 adjust OFFSET to compensate. */
7525 if (BYTES_BIG_ENDIAN
7526 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7527 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7529 /* We can now move to the desired byte. */
7530 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7531 * GET_MODE_SIZE (wanted_inner_mode);
7532 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7534 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7535 && is_mode != wanted_inner_mode)
7536 offset = (GET_MODE_SIZE (is_mode)
7537 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7539 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7542 /* If INNER is not memory, get it into the proper mode. If we are changing
7543 its mode, POS must be a constant and smaller than the size of the new
7544 mode. */
7545 else if (!MEM_P (inner))
7547 /* On the LHS, don't create paradoxical subregs implicitely truncating
7548 the register unless TRULY_NOOP_TRUNCATION. */
7549 if (in_dest
7550 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7551 wanted_inner_mode))
7552 return NULL_RTX;
7554 if (GET_MODE (inner) != wanted_inner_mode
7555 && (pos_rtx != 0
7556 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7557 return NULL_RTX;
7559 if (orig_pos < 0)
7560 return NULL_RTX;
7562 inner = force_to_mode (inner, wanted_inner_mode,
7563 pos_rtx
7564 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7565 ? ~(unsigned HOST_WIDE_INT) 0
7566 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7567 << orig_pos),
7571 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7572 have to zero extend. Otherwise, we can just use a SUBREG. */
7573 if (pos_rtx != 0
7574 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7576 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7577 GET_MODE (pos_rtx));
7579 /* If we know that no extraneous bits are set, and that the high
7580 bit is not set, convert extraction to cheaper one - either
7581 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7582 cases. */
7583 if (flag_expensive_optimizations
7584 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7585 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7586 & ~(((unsigned HOST_WIDE_INT)
7587 GET_MODE_MASK (GET_MODE (pos_rtx)))
7588 >> 1))
7589 == 0)))
7591 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7592 GET_MODE (pos_rtx));
7594 /* Prefer ZERO_EXTENSION, since it gives more information to
7595 backends. */
7596 if (set_src_cost (temp1, optimize_this_for_speed_p)
7597 < set_src_cost (temp, optimize_this_for_speed_p))
7598 temp = temp1;
7600 pos_rtx = temp;
7603 /* Make POS_RTX unless we already have it and it is correct. If we don't
7604 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7605 be a CONST_INT. */
7606 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7607 pos_rtx = orig_pos_rtx;
7609 else if (pos_rtx == 0)
7610 pos_rtx = GEN_INT (pos);
7612 /* Make the required operation. See if we can use existing rtx. */
7613 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7614 extraction_mode, inner, GEN_INT (len), pos_rtx);
7615 if (! in_dest)
7616 new_rtx = gen_lowpart (mode, new_rtx);
7618 return new_rtx;
7621 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7622 with any other operations in X. Return X without that shift if so. */
7624 static rtx
7625 extract_left_shift (rtx x, int count)
7627 enum rtx_code code = GET_CODE (x);
7628 machine_mode mode = GET_MODE (x);
7629 rtx tem;
7631 switch (code)
7633 case ASHIFT:
7634 /* This is the shift itself. If it is wide enough, we will return
7635 either the value being shifted if the shift count is equal to
7636 COUNT or a shift for the difference. */
7637 if (CONST_INT_P (XEXP (x, 1))
7638 && INTVAL (XEXP (x, 1)) >= count)
7639 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7640 INTVAL (XEXP (x, 1)) - count);
7641 break;
7643 case NEG: case NOT:
7644 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7645 return simplify_gen_unary (code, mode, tem, mode);
7647 break;
7649 case PLUS: case IOR: case XOR: case AND:
7650 /* If we can safely shift this constant and we find the inner shift,
7651 make a new operation. */
7652 if (CONST_INT_P (XEXP (x, 1))
7653 && (UINTVAL (XEXP (x, 1))
7654 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7655 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7657 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7658 return simplify_gen_binary (code, mode, tem,
7659 gen_int_mode (val, mode));
7661 break;
7663 default:
7664 break;
7667 return 0;
7670 /* Look at the expression rooted at X. Look for expressions
7671 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7672 Form these expressions.
7674 Return the new rtx, usually just X.
7676 Also, for machines like the VAX that don't have logical shift insns,
7677 try to convert logical to arithmetic shift operations in cases where
7678 they are equivalent. This undoes the canonicalizations to logical
7679 shifts done elsewhere.
7681 We try, as much as possible, to re-use rtl expressions to save memory.
7683 IN_CODE says what kind of expression we are processing. Normally, it is
7684 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7685 being kludges), it is MEM. When processing the arguments of a comparison
7686 or a COMPARE against zero, it is COMPARE. */
7689 make_compound_operation (rtx x, enum rtx_code in_code)
7691 enum rtx_code code = GET_CODE (x);
7692 machine_mode mode = GET_MODE (x);
7693 int mode_width = GET_MODE_PRECISION (mode);
7694 rtx rhs, lhs;
7695 enum rtx_code next_code;
7696 int i, j;
7697 rtx new_rtx = 0;
7698 rtx tem;
7699 const char *fmt;
7701 /* Select the code to be used in recursive calls. Once we are inside an
7702 address, we stay there. If we have a comparison, set to COMPARE,
7703 but once inside, go back to our default of SET. */
7705 next_code = (code == MEM ? MEM
7706 : ((code == PLUS || code == MINUS)
7707 && SCALAR_INT_MODE_P (mode)) ? MEM
7708 : ((code == COMPARE || COMPARISON_P (x))
7709 && XEXP (x, 1) == const0_rtx) ? COMPARE
7710 : in_code == COMPARE ? SET : in_code);
7712 /* Process depending on the code of this operation. If NEW is set
7713 nonzero, it will be returned. */
7715 switch (code)
7717 case ASHIFT:
7718 /* Convert shifts by constants into multiplications if inside
7719 an address. */
7720 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7721 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7722 && INTVAL (XEXP (x, 1)) >= 0
7723 && SCALAR_INT_MODE_P (mode))
7725 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7726 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7728 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7729 if (GET_CODE (new_rtx) == NEG)
7731 new_rtx = XEXP (new_rtx, 0);
7732 multval = -multval;
7734 multval = trunc_int_for_mode (multval, mode);
7735 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7737 break;
7739 case PLUS:
7740 lhs = XEXP (x, 0);
7741 rhs = XEXP (x, 1);
7742 lhs = make_compound_operation (lhs, next_code);
7743 rhs = make_compound_operation (rhs, next_code);
7744 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7745 && SCALAR_INT_MODE_P (mode))
7747 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7748 XEXP (lhs, 1));
7749 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7751 else if (GET_CODE (lhs) == MULT
7752 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7754 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7755 simplify_gen_unary (NEG, mode,
7756 XEXP (lhs, 1),
7757 mode));
7758 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7760 else
7762 SUBST (XEXP (x, 0), lhs);
7763 SUBST (XEXP (x, 1), rhs);
7764 goto maybe_swap;
7766 x = gen_lowpart (mode, new_rtx);
7767 goto maybe_swap;
7769 case MINUS:
7770 lhs = XEXP (x, 0);
7771 rhs = XEXP (x, 1);
7772 lhs = make_compound_operation (lhs, next_code);
7773 rhs = make_compound_operation (rhs, next_code);
7774 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7775 && SCALAR_INT_MODE_P (mode))
7777 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7778 XEXP (rhs, 1));
7779 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7781 else if (GET_CODE (rhs) == MULT
7782 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7784 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7785 simplify_gen_unary (NEG, mode,
7786 XEXP (rhs, 1),
7787 mode));
7788 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7790 else
7792 SUBST (XEXP (x, 0), lhs);
7793 SUBST (XEXP (x, 1), rhs);
7794 return x;
7796 return gen_lowpart (mode, new_rtx);
7798 case AND:
7799 /* If the second operand is not a constant, we can't do anything
7800 with it. */
7801 if (!CONST_INT_P (XEXP (x, 1)))
7802 break;
7804 /* If the constant is a power of two minus one and the first operand
7805 is a logical right shift, make an extraction. */
7806 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7807 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7809 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7810 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7811 0, in_code == COMPARE);
7814 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7815 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7816 && subreg_lowpart_p (XEXP (x, 0))
7817 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7818 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7820 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7821 next_code);
7822 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7823 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7824 0, in_code == COMPARE);
7826 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7827 else if ((GET_CODE (XEXP (x, 0)) == XOR
7828 || GET_CODE (XEXP (x, 0)) == IOR)
7829 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7830 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7831 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7833 /* Apply the distributive law, and then try to make extractions. */
7834 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7835 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7836 XEXP (x, 1)),
7837 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7838 XEXP (x, 1)));
7839 new_rtx = make_compound_operation (new_rtx, in_code);
7842 /* If we are have (and (rotate X C) M) and C is larger than the number
7843 of bits in M, this is an extraction. */
7845 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7846 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7847 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7848 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7850 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7851 new_rtx = make_extraction (mode, new_rtx,
7852 (GET_MODE_PRECISION (mode)
7853 - INTVAL (XEXP (XEXP (x, 0), 1))),
7854 NULL_RTX, i, 1, 0, in_code == COMPARE);
7857 /* On machines without logical shifts, if the operand of the AND is
7858 a logical shift and our mask turns off all the propagated sign
7859 bits, we can replace the logical shift with an arithmetic shift. */
7860 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7861 && !have_insn_for (LSHIFTRT, mode)
7862 && have_insn_for (ASHIFTRT, mode)
7863 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7864 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7865 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7866 && mode_width <= HOST_BITS_PER_WIDE_INT)
7868 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7870 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7871 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7872 SUBST (XEXP (x, 0),
7873 gen_rtx_ASHIFTRT (mode,
7874 make_compound_operation
7875 (XEXP (XEXP (x, 0), 0), next_code),
7876 XEXP (XEXP (x, 0), 1)));
7879 /* If the constant is one less than a power of two, this might be
7880 representable by an extraction even if no shift is present.
7881 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7882 we are in a COMPARE. */
7883 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7884 new_rtx = make_extraction (mode,
7885 make_compound_operation (XEXP (x, 0),
7886 next_code),
7887 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7889 /* If we are in a comparison and this is an AND with a power of two,
7890 convert this into the appropriate bit extract. */
7891 else if (in_code == COMPARE
7892 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7893 new_rtx = make_extraction (mode,
7894 make_compound_operation (XEXP (x, 0),
7895 next_code),
7896 i, NULL_RTX, 1, 1, 0, 1);
7898 break;
7900 case LSHIFTRT:
7901 /* If the sign bit is known to be zero, replace this with an
7902 arithmetic shift. */
7903 if (have_insn_for (ASHIFTRT, mode)
7904 && ! have_insn_for (LSHIFTRT, mode)
7905 && mode_width <= HOST_BITS_PER_WIDE_INT
7906 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7908 new_rtx = gen_rtx_ASHIFTRT (mode,
7909 make_compound_operation (XEXP (x, 0),
7910 next_code),
7911 XEXP (x, 1));
7912 break;
7915 /* ... fall through ... */
7917 case ASHIFTRT:
7918 lhs = XEXP (x, 0);
7919 rhs = XEXP (x, 1);
7921 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7922 this is a SIGN_EXTRACT. */
7923 if (CONST_INT_P (rhs)
7924 && GET_CODE (lhs) == ASHIFT
7925 && CONST_INT_P (XEXP (lhs, 1))
7926 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7927 && INTVAL (XEXP (lhs, 1)) >= 0
7928 && INTVAL (rhs) < mode_width)
7930 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7931 new_rtx = make_extraction (mode, new_rtx,
7932 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7933 NULL_RTX, mode_width - INTVAL (rhs),
7934 code == LSHIFTRT, 0, in_code == COMPARE);
7935 break;
7938 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7939 If so, try to merge the shifts into a SIGN_EXTEND. We could
7940 also do this for some cases of SIGN_EXTRACT, but it doesn't
7941 seem worth the effort; the case checked for occurs on Alpha. */
7943 if (!OBJECT_P (lhs)
7944 && ! (GET_CODE (lhs) == SUBREG
7945 && (OBJECT_P (SUBREG_REG (lhs))))
7946 && CONST_INT_P (rhs)
7947 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7948 && INTVAL (rhs) < mode_width
7949 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7950 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7951 0, NULL_RTX, mode_width - INTVAL (rhs),
7952 code == LSHIFTRT, 0, in_code == COMPARE);
7954 break;
7956 case SUBREG:
7957 /* Call ourselves recursively on the inner expression. If we are
7958 narrowing the object and it has a different RTL code from
7959 what it originally did, do this SUBREG as a force_to_mode. */
7961 rtx inner = SUBREG_REG (x), simplified;
7962 enum rtx_code subreg_code = in_code;
7964 /* If in_code is COMPARE, it isn't always safe to pass it through
7965 to the recursive make_compound_operation call. */
7966 if (subreg_code == COMPARE
7967 && (!subreg_lowpart_p (x)
7968 || GET_CODE (inner) == SUBREG
7969 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7970 is (const_int 0), rather than
7971 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7972 || (GET_CODE (inner) == AND
7973 && CONST_INT_P (XEXP (inner, 1))
7974 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7975 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7976 >= GET_MODE_BITSIZE (mode))))
7977 subreg_code = SET;
7979 tem = make_compound_operation (inner, subreg_code);
7981 simplified
7982 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7983 if (simplified)
7984 tem = simplified;
7986 if (GET_CODE (tem) != GET_CODE (inner)
7987 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7988 && subreg_lowpart_p (x))
7990 rtx newer
7991 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7993 /* If we have something other than a SUBREG, we might have
7994 done an expansion, so rerun ourselves. */
7995 if (GET_CODE (newer) != SUBREG)
7996 newer = make_compound_operation (newer, in_code);
7998 /* force_to_mode can expand compounds. If it just re-expanded the
7999 compound, use gen_lowpart to convert to the desired mode. */
8000 if (rtx_equal_p (newer, x)
8001 /* Likewise if it re-expanded the compound only partially.
8002 This happens for SUBREG of ZERO_EXTRACT if they extract
8003 the same number of bits. */
8004 || (GET_CODE (newer) == SUBREG
8005 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8006 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8007 && GET_CODE (inner) == AND
8008 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8009 return gen_lowpart (GET_MODE (x), tem);
8011 return newer;
8014 if (simplified)
8015 return tem;
8017 break;
8019 default:
8020 break;
8023 if (new_rtx)
8025 x = gen_lowpart (mode, new_rtx);
8026 code = GET_CODE (x);
8029 /* Now recursively process each operand of this operation. We need to
8030 handle ZERO_EXTEND specially so that we don't lose track of the
8031 inner mode. */
8032 if (GET_CODE (x) == ZERO_EXTEND)
8034 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8035 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8036 new_rtx, GET_MODE (XEXP (x, 0)));
8037 if (tem)
8038 return tem;
8039 SUBST (XEXP (x, 0), new_rtx);
8040 return x;
8043 fmt = GET_RTX_FORMAT (code);
8044 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8045 if (fmt[i] == 'e')
8047 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8048 SUBST (XEXP (x, i), new_rtx);
8050 else if (fmt[i] == 'E')
8051 for (j = 0; j < XVECLEN (x, i); j++)
8053 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8054 SUBST (XVECEXP (x, i, j), new_rtx);
8057 maybe_swap:
8058 /* If this is a commutative operation, the changes to the operands
8059 may have made it noncanonical. */
8060 if (COMMUTATIVE_ARITH_P (x)
8061 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8063 tem = XEXP (x, 0);
8064 SUBST (XEXP (x, 0), XEXP (x, 1));
8065 SUBST (XEXP (x, 1), tem);
8068 return x;
8071 /* Given M see if it is a value that would select a field of bits
8072 within an item, but not the entire word. Return -1 if not.
8073 Otherwise, return the starting position of the field, where 0 is the
8074 low-order bit.
8076 *PLEN is set to the length of the field. */
8078 static int
8079 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8081 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8082 int pos = m ? ctz_hwi (m) : -1;
8083 int len = 0;
8085 if (pos >= 0)
8086 /* Now shift off the low-order zero bits and see if we have a
8087 power of two minus 1. */
8088 len = exact_log2 ((m >> pos) + 1);
8090 if (len <= 0)
8091 pos = -1;
8093 *plen = len;
8094 return pos;
8097 /* If X refers to a register that equals REG in value, replace these
8098 references with REG. */
8099 static rtx
8100 canon_reg_for_combine (rtx x, rtx reg)
8102 rtx op0, op1, op2;
8103 const char *fmt;
8104 int i;
8105 bool copied;
8107 enum rtx_code code = GET_CODE (x);
8108 switch (GET_RTX_CLASS (code))
8110 case RTX_UNARY:
8111 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8112 if (op0 != XEXP (x, 0))
8113 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8114 GET_MODE (reg));
8115 break;
8117 case RTX_BIN_ARITH:
8118 case RTX_COMM_ARITH:
8119 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8120 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8121 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8122 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8123 break;
8125 case RTX_COMPARE:
8126 case RTX_COMM_COMPARE:
8127 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8128 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8129 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8130 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8131 GET_MODE (op0), op0, op1);
8132 break;
8134 case RTX_TERNARY:
8135 case RTX_BITFIELD_OPS:
8136 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8137 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8138 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8139 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8140 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8141 GET_MODE (op0), op0, op1, op2);
8143 case RTX_OBJ:
8144 if (REG_P (x))
8146 if (rtx_equal_p (get_last_value (reg), x)
8147 || rtx_equal_p (reg, get_last_value (x)))
8148 return reg;
8149 else
8150 break;
8153 /* fall through */
8155 default:
8156 fmt = GET_RTX_FORMAT (code);
8157 copied = false;
8158 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8159 if (fmt[i] == 'e')
8161 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8162 if (op != XEXP (x, i))
8164 if (!copied)
8166 copied = true;
8167 x = copy_rtx (x);
8169 XEXP (x, i) = op;
8172 else if (fmt[i] == 'E')
8174 int j;
8175 for (j = 0; j < XVECLEN (x, i); j++)
8177 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8178 if (op != XVECEXP (x, i, j))
8180 if (!copied)
8182 copied = true;
8183 x = copy_rtx (x);
8185 XVECEXP (x, i, j) = op;
8190 break;
8193 return x;
8196 /* Return X converted to MODE. If the value is already truncated to
8197 MODE we can just return a subreg even though in the general case we
8198 would need an explicit truncation. */
8200 static rtx
8201 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8203 if (!CONST_INT_P (x)
8204 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8205 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8206 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8208 /* Bit-cast X into an integer mode. */
8209 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8210 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8211 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8212 x, GET_MODE (x));
8215 return gen_lowpart (mode, x);
8218 /* See if X can be simplified knowing that we will only refer to it in
8219 MODE and will only refer to those bits that are nonzero in MASK.
8220 If other bits are being computed or if masking operations are done
8221 that select a superset of the bits in MASK, they can sometimes be
8222 ignored.
8224 Return a possibly simplified expression, but always convert X to
8225 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8227 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8228 are all off in X. This is used when X will be complemented, by either
8229 NOT, NEG, or XOR. */
8231 static rtx
8232 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8233 int just_select)
8235 enum rtx_code code = GET_CODE (x);
8236 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8237 machine_mode op_mode;
8238 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8239 rtx op0, op1, temp;
8241 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8242 code below will do the wrong thing since the mode of such an
8243 expression is VOIDmode.
8245 Also do nothing if X is a CLOBBER; this can happen if X was
8246 the return value from a call to gen_lowpart. */
8247 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8248 return x;
8250 /* We want to perform the operation in its present mode unless we know
8251 that the operation is valid in MODE, in which case we do the operation
8252 in MODE. */
8253 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8254 && have_insn_for (code, mode))
8255 ? mode : GET_MODE (x));
8257 /* It is not valid to do a right-shift in a narrower mode
8258 than the one it came in with. */
8259 if ((code == LSHIFTRT || code == ASHIFTRT)
8260 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8261 op_mode = GET_MODE (x);
8263 /* Truncate MASK to fit OP_MODE. */
8264 if (op_mode)
8265 mask &= GET_MODE_MASK (op_mode);
8267 /* When we have an arithmetic operation, or a shift whose count we
8268 do not know, we need to assume that all bits up to the highest-order
8269 bit in MASK will be needed. This is how we form such a mask. */
8270 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8271 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8272 else
8273 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8274 - 1);
8276 /* Determine what bits of X are guaranteed to be (non)zero. */
8277 nonzero = nonzero_bits (x, mode);
8279 /* If none of the bits in X are needed, return a zero. */
8280 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8281 x = const0_rtx;
8283 /* If X is a CONST_INT, return a new one. Do this here since the
8284 test below will fail. */
8285 if (CONST_INT_P (x))
8287 if (SCALAR_INT_MODE_P (mode))
8288 return gen_int_mode (INTVAL (x) & mask, mode);
8289 else
8291 x = GEN_INT (INTVAL (x) & mask);
8292 return gen_lowpart_common (mode, x);
8296 /* If X is narrower than MODE and we want all the bits in X's mode, just
8297 get X in the proper mode. */
8298 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8299 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8300 return gen_lowpart (mode, x);
8302 /* We can ignore the effect of a SUBREG if it narrows the mode or
8303 if the constant masks to zero all the bits the mode doesn't have. */
8304 if (GET_CODE (x) == SUBREG
8305 && subreg_lowpart_p (x)
8306 && ((GET_MODE_SIZE (GET_MODE (x))
8307 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8308 || (0 == (mask
8309 & GET_MODE_MASK (GET_MODE (x))
8310 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8311 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8313 /* The arithmetic simplifications here only work for scalar integer modes. */
8314 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8315 return gen_lowpart_or_truncate (mode, x);
8317 switch (code)
8319 case CLOBBER:
8320 /* If X is a (clobber (const_int)), return it since we know we are
8321 generating something that won't match. */
8322 return x;
8324 case SIGN_EXTEND:
8325 case ZERO_EXTEND:
8326 case ZERO_EXTRACT:
8327 case SIGN_EXTRACT:
8328 x = expand_compound_operation (x);
8329 if (GET_CODE (x) != code)
8330 return force_to_mode (x, mode, mask, next_select);
8331 break;
8333 case TRUNCATE:
8334 /* Similarly for a truncate. */
8335 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8337 case AND:
8338 /* If this is an AND with a constant, convert it into an AND
8339 whose constant is the AND of that constant with MASK. If it
8340 remains an AND of MASK, delete it since it is redundant. */
8342 if (CONST_INT_P (XEXP (x, 1)))
8344 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8345 mask & INTVAL (XEXP (x, 1)));
8347 /* If X is still an AND, see if it is an AND with a mask that
8348 is just some low-order bits. If so, and it is MASK, we don't
8349 need it. */
8351 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8352 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8353 == mask))
8354 x = XEXP (x, 0);
8356 /* If it remains an AND, try making another AND with the bits
8357 in the mode mask that aren't in MASK turned on. If the
8358 constant in the AND is wide enough, this might make a
8359 cheaper constant. */
8361 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8362 && GET_MODE_MASK (GET_MODE (x)) != mask
8363 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8365 unsigned HOST_WIDE_INT cval
8366 = UINTVAL (XEXP (x, 1))
8367 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8368 rtx y;
8370 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8371 gen_int_mode (cval, GET_MODE (x)));
8372 if (set_src_cost (y, optimize_this_for_speed_p)
8373 < set_src_cost (x, optimize_this_for_speed_p))
8374 x = y;
8377 break;
8380 goto binop;
8382 case PLUS:
8383 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8384 low-order bits (as in an alignment operation) and FOO is already
8385 aligned to that boundary, mask C1 to that boundary as well.
8386 This may eliminate that PLUS and, later, the AND. */
8389 unsigned int width = GET_MODE_PRECISION (mode);
8390 unsigned HOST_WIDE_INT smask = mask;
8392 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8393 number, sign extend it. */
8395 if (width < HOST_BITS_PER_WIDE_INT
8396 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8397 smask |= HOST_WIDE_INT_M1U << width;
8399 if (CONST_INT_P (XEXP (x, 1))
8400 && exact_log2 (- smask) >= 0
8401 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8402 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8403 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8404 (INTVAL (XEXP (x, 1)) & smask)),
8405 mode, smask, next_select);
8408 /* ... fall through ... */
8410 case MULT:
8411 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8412 most significant bit in MASK since carries from those bits will
8413 affect the bits we are interested in. */
8414 mask = fuller_mask;
8415 goto binop;
8417 case MINUS:
8418 /* If X is (minus C Y) where C's least set bit is larger than any bit
8419 in the mask, then we may replace with (neg Y). */
8420 if (CONST_INT_P (XEXP (x, 0))
8421 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8423 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8424 GET_MODE (x));
8425 return force_to_mode (x, mode, mask, next_select);
8428 /* Similarly, if C contains every bit in the fuller_mask, then we may
8429 replace with (not Y). */
8430 if (CONST_INT_P (XEXP (x, 0))
8431 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8433 x = simplify_gen_unary (NOT, GET_MODE (x),
8434 XEXP (x, 1), GET_MODE (x));
8435 return force_to_mode (x, mode, mask, next_select);
8438 mask = fuller_mask;
8439 goto binop;
8441 case IOR:
8442 case XOR:
8443 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8444 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8445 operation which may be a bitfield extraction. Ensure that the
8446 constant we form is not wider than the mode of X. */
8448 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8449 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8450 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8451 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8452 && CONST_INT_P (XEXP (x, 1))
8453 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8454 + floor_log2 (INTVAL (XEXP (x, 1))))
8455 < GET_MODE_PRECISION (GET_MODE (x)))
8456 && (UINTVAL (XEXP (x, 1))
8457 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8459 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8460 << INTVAL (XEXP (XEXP (x, 0), 1)),
8461 GET_MODE (x));
8462 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8463 XEXP (XEXP (x, 0), 0), temp);
8464 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8465 XEXP (XEXP (x, 0), 1));
8466 return force_to_mode (x, mode, mask, next_select);
8469 binop:
8470 /* For most binary operations, just propagate into the operation and
8471 change the mode if we have an operation of that mode. */
8473 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8474 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8476 /* If we ended up truncating both operands, truncate the result of the
8477 operation instead. */
8478 if (GET_CODE (op0) == TRUNCATE
8479 && GET_CODE (op1) == TRUNCATE)
8481 op0 = XEXP (op0, 0);
8482 op1 = XEXP (op1, 0);
8485 op0 = gen_lowpart_or_truncate (op_mode, op0);
8486 op1 = gen_lowpart_or_truncate (op_mode, op1);
8488 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8489 x = simplify_gen_binary (code, op_mode, op0, op1);
8490 break;
8492 case ASHIFT:
8493 /* For left shifts, do the same, but just for the first operand.
8494 However, we cannot do anything with shifts where we cannot
8495 guarantee that the counts are smaller than the size of the mode
8496 because such a count will have a different meaning in a
8497 wider mode. */
8499 if (! (CONST_INT_P (XEXP (x, 1))
8500 && INTVAL (XEXP (x, 1)) >= 0
8501 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8502 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8503 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8504 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8505 break;
8507 /* If the shift count is a constant and we can do arithmetic in
8508 the mode of the shift, refine which bits we need. Otherwise, use the
8509 conservative form of the mask. */
8510 if (CONST_INT_P (XEXP (x, 1))
8511 && INTVAL (XEXP (x, 1)) >= 0
8512 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8513 && HWI_COMPUTABLE_MODE_P (op_mode))
8514 mask >>= INTVAL (XEXP (x, 1));
8515 else
8516 mask = fuller_mask;
8518 op0 = gen_lowpart_or_truncate (op_mode,
8519 force_to_mode (XEXP (x, 0), op_mode,
8520 mask, next_select));
8522 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8523 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8524 break;
8526 case LSHIFTRT:
8527 /* Here we can only do something if the shift count is a constant,
8528 this shift constant is valid for the host, and we can do arithmetic
8529 in OP_MODE. */
8531 if (CONST_INT_P (XEXP (x, 1))
8532 && INTVAL (XEXP (x, 1)) >= 0
8533 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8534 && HWI_COMPUTABLE_MODE_P (op_mode))
8536 rtx inner = XEXP (x, 0);
8537 unsigned HOST_WIDE_INT inner_mask;
8539 /* Select the mask of the bits we need for the shift operand. */
8540 inner_mask = mask << INTVAL (XEXP (x, 1));
8542 /* We can only change the mode of the shift if we can do arithmetic
8543 in the mode of the shift and INNER_MASK is no wider than the
8544 width of X's mode. */
8545 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8546 op_mode = GET_MODE (x);
8548 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8550 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8551 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8554 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8555 shift and AND produces only copies of the sign bit (C2 is one less
8556 than a power of two), we can do this with just a shift. */
8558 if (GET_CODE (x) == LSHIFTRT
8559 && CONST_INT_P (XEXP (x, 1))
8560 /* The shift puts one of the sign bit copies in the least significant
8561 bit. */
8562 && ((INTVAL (XEXP (x, 1))
8563 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8564 >= GET_MODE_PRECISION (GET_MODE (x)))
8565 && exact_log2 (mask + 1) >= 0
8566 /* Number of bits left after the shift must be more than the mask
8567 needs. */
8568 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8569 <= GET_MODE_PRECISION (GET_MODE (x)))
8570 /* Must be more sign bit copies than the mask needs. */
8571 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8572 >= exact_log2 (mask + 1)))
8573 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8574 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8575 - exact_log2 (mask + 1)));
8577 goto shiftrt;
8579 case ASHIFTRT:
8580 /* If we are just looking for the sign bit, we don't need this shift at
8581 all, even if it has a variable count. */
8582 if (val_signbit_p (GET_MODE (x), mask))
8583 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8585 /* If this is a shift by a constant, get a mask that contains those bits
8586 that are not copies of the sign bit. We then have two cases: If
8587 MASK only includes those bits, this can be a logical shift, which may
8588 allow simplifications. If MASK is a single-bit field not within
8589 those bits, we are requesting a copy of the sign bit and hence can
8590 shift the sign bit to the appropriate location. */
8592 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8593 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8595 int i;
8597 /* If the considered data is wider than HOST_WIDE_INT, we can't
8598 represent a mask for all its bits in a single scalar.
8599 But we only care about the lower bits, so calculate these. */
8601 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8603 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8605 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8606 is the number of bits a full-width mask would have set.
8607 We need only shift if these are fewer than nonzero can
8608 hold. If not, we must keep all bits set in nonzero. */
8610 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8611 < HOST_BITS_PER_WIDE_INT)
8612 nonzero >>= INTVAL (XEXP (x, 1))
8613 + HOST_BITS_PER_WIDE_INT
8614 - GET_MODE_PRECISION (GET_MODE (x)) ;
8616 else
8618 nonzero = GET_MODE_MASK (GET_MODE (x));
8619 nonzero >>= INTVAL (XEXP (x, 1));
8622 if ((mask & ~nonzero) == 0)
8624 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8625 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8626 if (GET_CODE (x) != ASHIFTRT)
8627 return force_to_mode (x, mode, mask, next_select);
8630 else if ((i = exact_log2 (mask)) >= 0)
8632 x = simplify_shift_const
8633 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8634 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8636 if (GET_CODE (x) != ASHIFTRT)
8637 return force_to_mode (x, mode, mask, next_select);
8641 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8642 even if the shift count isn't a constant. */
8643 if (mask == 1)
8644 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8645 XEXP (x, 0), XEXP (x, 1));
8647 shiftrt:
8649 /* If this is a zero- or sign-extension operation that just affects bits
8650 we don't care about, remove it. Be sure the call above returned
8651 something that is still a shift. */
8653 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8654 && CONST_INT_P (XEXP (x, 1))
8655 && INTVAL (XEXP (x, 1)) >= 0
8656 && (INTVAL (XEXP (x, 1))
8657 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8658 && GET_CODE (XEXP (x, 0)) == ASHIFT
8659 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8660 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8661 next_select);
8663 break;
8665 case ROTATE:
8666 case ROTATERT:
8667 /* If the shift count is constant and we can do computations
8668 in the mode of X, compute where the bits we care about are.
8669 Otherwise, we can't do anything. Don't change the mode of
8670 the shift or propagate MODE into the shift, though. */
8671 if (CONST_INT_P (XEXP (x, 1))
8672 && INTVAL (XEXP (x, 1)) >= 0)
8674 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8675 GET_MODE (x),
8676 gen_int_mode (mask, GET_MODE (x)),
8677 XEXP (x, 1));
8678 if (temp && CONST_INT_P (temp))
8679 x = simplify_gen_binary (code, GET_MODE (x),
8680 force_to_mode (XEXP (x, 0), GET_MODE (x),
8681 INTVAL (temp), next_select),
8682 XEXP (x, 1));
8684 break;
8686 case NEG:
8687 /* If we just want the low-order bit, the NEG isn't needed since it
8688 won't change the low-order bit. */
8689 if (mask == 1)
8690 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8692 /* We need any bits less significant than the most significant bit in
8693 MASK since carries from those bits will affect the bits we are
8694 interested in. */
8695 mask = fuller_mask;
8696 goto unop;
8698 case NOT:
8699 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8700 same as the XOR case above. Ensure that the constant we form is not
8701 wider than the mode of X. */
8703 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8704 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8705 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8706 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8707 < GET_MODE_PRECISION (GET_MODE (x)))
8708 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8710 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8711 GET_MODE (x));
8712 temp = simplify_gen_binary (XOR, GET_MODE (x),
8713 XEXP (XEXP (x, 0), 0), temp);
8714 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8715 temp, XEXP (XEXP (x, 0), 1));
8717 return force_to_mode (x, mode, mask, next_select);
8720 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8721 use the full mask inside the NOT. */
8722 mask = fuller_mask;
8724 unop:
8725 op0 = gen_lowpart_or_truncate (op_mode,
8726 force_to_mode (XEXP (x, 0), mode, mask,
8727 next_select));
8728 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8729 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8730 break;
8732 case NE:
8733 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8734 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8735 which is equal to STORE_FLAG_VALUE. */
8736 if ((mask & ~STORE_FLAG_VALUE) == 0
8737 && XEXP (x, 1) == const0_rtx
8738 && GET_MODE (XEXP (x, 0)) == mode
8739 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8740 && (nonzero_bits (XEXP (x, 0), mode)
8741 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8742 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8744 break;
8746 case IF_THEN_ELSE:
8747 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8748 written in a narrower mode. We play it safe and do not do so. */
8750 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8751 force_to_mode (XEXP (x, 1), mode,
8752 mask, next_select));
8753 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8754 force_to_mode (XEXP (x, 2), mode,
8755 mask, next_select));
8756 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8757 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8758 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8759 op0, op1);
8760 break;
8762 default:
8763 break;
8766 /* Ensure we return a value of the proper mode. */
8767 return gen_lowpart_or_truncate (mode, x);
8770 /* Return nonzero if X is an expression that has one of two values depending on
8771 whether some other value is zero or nonzero. In that case, we return the
8772 value that is being tested, *PTRUE is set to the value if the rtx being
8773 returned has a nonzero value, and *PFALSE is set to the other alternative.
8775 If we return zero, we set *PTRUE and *PFALSE to X. */
8777 static rtx
8778 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8780 machine_mode mode = GET_MODE (x);
8781 enum rtx_code code = GET_CODE (x);
8782 rtx cond0, cond1, true0, true1, false0, false1;
8783 unsigned HOST_WIDE_INT nz;
8785 /* If we are comparing a value against zero, we are done. */
8786 if ((code == NE || code == EQ)
8787 && XEXP (x, 1) == const0_rtx)
8789 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8790 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8791 return XEXP (x, 0);
8794 /* If this is a unary operation whose operand has one of two values, apply
8795 our opcode to compute those values. */
8796 else if (UNARY_P (x)
8797 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8799 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8800 *pfalse = simplify_gen_unary (code, mode, false0,
8801 GET_MODE (XEXP (x, 0)));
8802 return cond0;
8805 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8806 make can't possibly match and would suppress other optimizations. */
8807 else if (code == COMPARE)
8810 /* If this is a binary operation, see if either side has only one of two
8811 values. If either one does or if both do and they are conditional on
8812 the same value, compute the new true and false values. */
8813 else if (BINARY_P (x))
8815 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8816 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8818 if ((cond0 != 0 || cond1 != 0)
8819 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8821 /* If if_then_else_cond returned zero, then true/false are the
8822 same rtl. We must copy one of them to prevent invalid rtl
8823 sharing. */
8824 if (cond0 == 0)
8825 true0 = copy_rtx (true0);
8826 else if (cond1 == 0)
8827 true1 = copy_rtx (true1);
8829 if (COMPARISON_P (x))
8831 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8832 true0, true1);
8833 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8834 false0, false1);
8836 else
8838 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8839 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8842 return cond0 ? cond0 : cond1;
8845 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8846 operands is zero when the other is nonzero, and vice-versa,
8847 and STORE_FLAG_VALUE is 1 or -1. */
8849 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8850 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8851 || code == UMAX)
8852 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8854 rtx op0 = XEXP (XEXP (x, 0), 1);
8855 rtx op1 = XEXP (XEXP (x, 1), 1);
8857 cond0 = XEXP (XEXP (x, 0), 0);
8858 cond1 = XEXP (XEXP (x, 1), 0);
8860 if (COMPARISON_P (cond0)
8861 && COMPARISON_P (cond1)
8862 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8863 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8864 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8865 || ((swap_condition (GET_CODE (cond0))
8866 == reversed_comparison_code (cond1, NULL))
8867 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8868 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8869 && ! side_effects_p (x))
8871 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8872 *pfalse = simplify_gen_binary (MULT, mode,
8873 (code == MINUS
8874 ? simplify_gen_unary (NEG, mode,
8875 op1, mode)
8876 : op1),
8877 const_true_rtx);
8878 return cond0;
8882 /* Similarly for MULT, AND and UMIN, except that for these the result
8883 is always zero. */
8884 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8885 && (code == MULT || code == AND || code == UMIN)
8886 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8888 cond0 = XEXP (XEXP (x, 0), 0);
8889 cond1 = XEXP (XEXP (x, 1), 0);
8891 if (COMPARISON_P (cond0)
8892 && COMPARISON_P (cond1)
8893 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8894 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8895 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8896 || ((swap_condition (GET_CODE (cond0))
8897 == reversed_comparison_code (cond1, NULL))
8898 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8899 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8900 && ! side_effects_p (x))
8902 *ptrue = *pfalse = const0_rtx;
8903 return cond0;
8908 else if (code == IF_THEN_ELSE)
8910 /* If we have IF_THEN_ELSE already, extract the condition and
8911 canonicalize it if it is NE or EQ. */
8912 cond0 = XEXP (x, 0);
8913 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8914 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8915 return XEXP (cond0, 0);
8916 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8918 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8919 return XEXP (cond0, 0);
8921 else
8922 return cond0;
8925 /* If X is a SUBREG, we can narrow both the true and false values
8926 if the inner expression, if there is a condition. */
8927 else if (code == SUBREG
8928 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8929 &true0, &false0)))
8931 true0 = simplify_gen_subreg (mode, true0,
8932 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8933 false0 = simplify_gen_subreg (mode, false0,
8934 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8935 if (true0 && false0)
8937 *ptrue = true0;
8938 *pfalse = false0;
8939 return cond0;
8943 /* If X is a constant, this isn't special and will cause confusions
8944 if we treat it as such. Likewise if it is equivalent to a constant. */
8945 else if (CONSTANT_P (x)
8946 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8949 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8950 will be least confusing to the rest of the compiler. */
8951 else if (mode == BImode)
8953 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8954 return x;
8957 /* If X is known to be either 0 or -1, those are the true and
8958 false values when testing X. */
8959 else if (x == constm1_rtx || x == const0_rtx
8960 || (mode != VOIDmode
8961 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8963 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8964 return x;
8967 /* Likewise for 0 or a single bit. */
8968 else if (HWI_COMPUTABLE_MODE_P (mode)
8969 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8971 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8972 return x;
8975 /* Otherwise fail; show no condition with true and false values the same. */
8976 *ptrue = *pfalse = x;
8977 return 0;
8980 /* Return the value of expression X given the fact that condition COND
8981 is known to be true when applied to REG as its first operand and VAL
8982 as its second. X is known to not be shared and so can be modified in
8983 place.
8985 We only handle the simplest cases, and specifically those cases that
8986 arise with IF_THEN_ELSE expressions. */
8988 static rtx
8989 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8991 enum rtx_code code = GET_CODE (x);
8992 rtx temp;
8993 const char *fmt;
8994 int i, j;
8996 if (side_effects_p (x))
8997 return x;
8999 /* If either operand of the condition is a floating point value,
9000 then we have to avoid collapsing an EQ comparison. */
9001 if (cond == EQ
9002 && rtx_equal_p (x, reg)
9003 && ! FLOAT_MODE_P (GET_MODE (x))
9004 && ! FLOAT_MODE_P (GET_MODE (val)))
9005 return val;
9007 if (cond == UNEQ && rtx_equal_p (x, reg))
9008 return val;
9010 /* If X is (abs REG) and we know something about REG's relationship
9011 with zero, we may be able to simplify this. */
9013 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9014 switch (cond)
9016 case GE: case GT: case EQ:
9017 return XEXP (x, 0);
9018 case LT: case LE:
9019 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9020 XEXP (x, 0),
9021 GET_MODE (XEXP (x, 0)));
9022 default:
9023 break;
9026 /* The only other cases we handle are MIN, MAX, and comparisons if the
9027 operands are the same as REG and VAL. */
9029 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9031 if (rtx_equal_p (XEXP (x, 0), val))
9032 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
9034 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9036 if (COMPARISON_P (x))
9038 if (comparison_dominates_p (cond, code))
9039 return const_true_rtx;
9041 code = reversed_comparison_code (x, NULL);
9042 if (code != UNKNOWN
9043 && comparison_dominates_p (cond, code))
9044 return const0_rtx;
9045 else
9046 return x;
9048 else if (code == SMAX || code == SMIN
9049 || code == UMIN || code == UMAX)
9051 int unsignedp = (code == UMIN || code == UMAX);
9053 /* Do not reverse the condition when it is NE or EQ.
9054 This is because we cannot conclude anything about
9055 the value of 'SMAX (x, y)' when x is not equal to y,
9056 but we can when x equals y. */
9057 if ((code == SMAX || code == UMAX)
9058 && ! (cond == EQ || cond == NE))
9059 cond = reverse_condition (cond);
9061 switch (cond)
9063 case GE: case GT:
9064 return unsignedp ? x : XEXP (x, 1);
9065 case LE: case LT:
9066 return unsignedp ? x : XEXP (x, 0);
9067 case GEU: case GTU:
9068 return unsignedp ? XEXP (x, 1) : x;
9069 case LEU: case LTU:
9070 return unsignedp ? XEXP (x, 0) : x;
9071 default:
9072 break;
9077 else if (code == SUBREG)
9079 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9080 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9082 if (SUBREG_REG (x) != r)
9084 /* We must simplify subreg here, before we lose track of the
9085 original inner_mode. */
9086 new_rtx = simplify_subreg (GET_MODE (x), r,
9087 inner_mode, SUBREG_BYTE (x));
9088 if (new_rtx)
9089 return new_rtx;
9090 else
9091 SUBST (SUBREG_REG (x), r);
9094 return x;
9096 /* We don't have to handle SIGN_EXTEND here, because even in the
9097 case of replacing something with a modeless CONST_INT, a
9098 CONST_INT is already (supposed to be) a valid sign extension for
9099 its narrower mode, which implies it's already properly
9100 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9101 story is different. */
9102 else if (code == ZERO_EXTEND)
9104 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9105 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9107 if (XEXP (x, 0) != r)
9109 /* We must simplify the zero_extend here, before we lose
9110 track of the original inner_mode. */
9111 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9112 r, inner_mode);
9113 if (new_rtx)
9114 return new_rtx;
9115 else
9116 SUBST (XEXP (x, 0), r);
9119 return x;
9122 fmt = GET_RTX_FORMAT (code);
9123 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9125 if (fmt[i] == 'e')
9126 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9127 else if (fmt[i] == 'E')
9128 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9129 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9130 cond, reg, val));
9133 return x;
9136 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9137 assignment as a field assignment. */
9139 static int
9140 rtx_equal_for_field_assignment_p (rtx x, rtx y)
9142 if (x == y || rtx_equal_p (x, y))
9143 return 1;
9145 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9146 return 0;
9148 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9149 Note that all SUBREGs of MEM are paradoxical; otherwise they
9150 would have been rewritten. */
9151 if (MEM_P (x) && GET_CODE (y) == SUBREG
9152 && MEM_P (SUBREG_REG (y))
9153 && rtx_equal_p (SUBREG_REG (y),
9154 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9155 return 1;
9157 if (MEM_P (y) && GET_CODE (x) == SUBREG
9158 && MEM_P (SUBREG_REG (x))
9159 && rtx_equal_p (SUBREG_REG (x),
9160 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9161 return 1;
9163 /* We used to see if get_last_value of X and Y were the same but that's
9164 not correct. In one direction, we'll cause the assignment to have
9165 the wrong destination and in the case, we'll import a register into this
9166 insn that might have already have been dead. So fail if none of the
9167 above cases are true. */
9168 return 0;
9171 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9172 Return that assignment if so.
9174 We only handle the most common cases. */
9176 static rtx
9177 make_field_assignment (rtx x)
9179 rtx dest = SET_DEST (x);
9180 rtx src = SET_SRC (x);
9181 rtx assign;
9182 rtx rhs, lhs;
9183 HOST_WIDE_INT c1;
9184 HOST_WIDE_INT pos;
9185 unsigned HOST_WIDE_INT len;
9186 rtx other;
9187 machine_mode mode;
9189 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9190 a clear of a one-bit field. We will have changed it to
9191 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9192 for a SUBREG. */
9194 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9195 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9196 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9197 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9199 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9200 1, 1, 1, 0);
9201 if (assign != 0)
9202 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9203 return x;
9206 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9207 && subreg_lowpart_p (XEXP (src, 0))
9208 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9209 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9210 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9211 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9212 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9213 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9215 assign = make_extraction (VOIDmode, dest, 0,
9216 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9217 1, 1, 1, 0);
9218 if (assign != 0)
9219 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9220 return x;
9223 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9224 one-bit field. */
9225 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9226 && XEXP (XEXP (src, 0), 0) == const1_rtx
9227 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9229 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9230 1, 1, 1, 0);
9231 if (assign != 0)
9232 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9233 return x;
9236 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9237 SRC is an AND with all bits of that field set, then we can discard
9238 the AND. */
9239 if (GET_CODE (dest) == ZERO_EXTRACT
9240 && CONST_INT_P (XEXP (dest, 1))
9241 && GET_CODE (src) == AND
9242 && CONST_INT_P (XEXP (src, 1)))
9244 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9245 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9246 unsigned HOST_WIDE_INT ze_mask;
9248 if (width >= HOST_BITS_PER_WIDE_INT)
9249 ze_mask = -1;
9250 else
9251 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9253 /* Complete overlap. We can remove the source AND. */
9254 if ((and_mask & ze_mask) == ze_mask)
9255 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9257 /* Partial overlap. We can reduce the source AND. */
9258 if ((and_mask & ze_mask) != and_mask)
9260 mode = GET_MODE (src);
9261 src = gen_rtx_AND (mode, XEXP (src, 0),
9262 gen_int_mode (and_mask & ze_mask, mode));
9263 return gen_rtx_SET (VOIDmode, dest, src);
9267 /* The other case we handle is assignments into a constant-position
9268 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9269 a mask that has all one bits except for a group of zero bits and
9270 OTHER is known to have zeros where C1 has ones, this is such an
9271 assignment. Compute the position and length from C1. Shift OTHER
9272 to the appropriate position, force it to the required mode, and
9273 make the extraction. Check for the AND in both operands. */
9275 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9276 return x;
9278 rhs = expand_compound_operation (XEXP (src, 0));
9279 lhs = expand_compound_operation (XEXP (src, 1));
9281 if (GET_CODE (rhs) == AND
9282 && CONST_INT_P (XEXP (rhs, 1))
9283 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9284 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9285 else if (GET_CODE (lhs) == AND
9286 && CONST_INT_P (XEXP (lhs, 1))
9287 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9288 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9289 else
9290 return x;
9292 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9293 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9294 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9295 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9296 return x;
9298 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9299 if (assign == 0)
9300 return x;
9302 /* The mode to use for the source is the mode of the assignment, or of
9303 what is inside a possible STRICT_LOW_PART. */
9304 mode = (GET_CODE (assign) == STRICT_LOW_PART
9305 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9307 /* Shift OTHER right POS places and make it the source, restricting it
9308 to the proper length and mode. */
9310 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9311 GET_MODE (src),
9312 other, pos),
9313 dest);
9314 src = force_to_mode (src, mode,
9315 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9316 ? ~(unsigned HOST_WIDE_INT) 0
9317 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9320 /* If SRC is masked by an AND that does not make a difference in
9321 the value being stored, strip it. */
9322 if (GET_CODE (assign) == ZERO_EXTRACT
9323 && CONST_INT_P (XEXP (assign, 1))
9324 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9325 && GET_CODE (src) == AND
9326 && CONST_INT_P (XEXP (src, 1))
9327 && UINTVAL (XEXP (src, 1))
9328 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9329 src = XEXP (src, 0);
9331 return gen_rtx_SET (VOIDmode, assign, src);
9334 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9335 if so. */
9337 static rtx
9338 apply_distributive_law (rtx x)
9340 enum rtx_code code = GET_CODE (x);
9341 enum rtx_code inner_code;
9342 rtx lhs, rhs, other;
9343 rtx tem;
9345 /* Distributivity is not true for floating point as it can change the
9346 value. So we don't do it unless -funsafe-math-optimizations. */
9347 if (FLOAT_MODE_P (GET_MODE (x))
9348 && ! flag_unsafe_math_optimizations)
9349 return x;
9351 /* The outer operation can only be one of the following: */
9352 if (code != IOR && code != AND && code != XOR
9353 && code != PLUS && code != MINUS)
9354 return x;
9356 lhs = XEXP (x, 0);
9357 rhs = XEXP (x, 1);
9359 /* If either operand is a primitive we can't do anything, so get out
9360 fast. */
9361 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9362 return x;
9364 lhs = expand_compound_operation (lhs);
9365 rhs = expand_compound_operation (rhs);
9366 inner_code = GET_CODE (lhs);
9367 if (inner_code != GET_CODE (rhs))
9368 return x;
9370 /* See if the inner and outer operations distribute. */
9371 switch (inner_code)
9373 case LSHIFTRT:
9374 case ASHIFTRT:
9375 case AND:
9376 case IOR:
9377 /* These all distribute except over PLUS. */
9378 if (code == PLUS || code == MINUS)
9379 return x;
9380 break;
9382 case MULT:
9383 if (code != PLUS && code != MINUS)
9384 return x;
9385 break;
9387 case ASHIFT:
9388 /* This is also a multiply, so it distributes over everything. */
9389 break;
9391 /* This used to handle SUBREG, but this turned out to be counter-
9392 productive, since (subreg (op ...)) usually is not handled by
9393 insn patterns, and this "optimization" therefore transformed
9394 recognizable patterns into unrecognizable ones. Therefore the
9395 SUBREG case was removed from here.
9397 It is possible that distributing SUBREG over arithmetic operations
9398 leads to an intermediate result than can then be optimized further,
9399 e.g. by moving the outer SUBREG to the other side of a SET as done
9400 in simplify_set. This seems to have been the original intent of
9401 handling SUBREGs here.
9403 However, with current GCC this does not appear to actually happen,
9404 at least on major platforms. If some case is found where removing
9405 the SUBREG case here prevents follow-on optimizations, distributing
9406 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9408 default:
9409 return x;
9412 /* Set LHS and RHS to the inner operands (A and B in the example
9413 above) and set OTHER to the common operand (C in the example).
9414 There is only one way to do this unless the inner operation is
9415 commutative. */
9416 if (COMMUTATIVE_ARITH_P (lhs)
9417 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9418 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9419 else if (COMMUTATIVE_ARITH_P (lhs)
9420 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9421 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9422 else if (COMMUTATIVE_ARITH_P (lhs)
9423 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9424 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9425 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9426 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9427 else
9428 return x;
9430 /* Form the new inner operation, seeing if it simplifies first. */
9431 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9433 /* There is one exception to the general way of distributing:
9434 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9435 if (code == XOR && inner_code == IOR)
9437 inner_code = AND;
9438 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9441 /* We may be able to continuing distributing the result, so call
9442 ourselves recursively on the inner operation before forming the
9443 outer operation, which we return. */
9444 return simplify_gen_binary (inner_code, GET_MODE (x),
9445 apply_distributive_law (tem), other);
9448 /* See if X is of the form (* (+ A B) C), and if so convert to
9449 (+ (* A C) (* B C)) and try to simplify.
9451 Most of the time, this results in no change. However, if some of
9452 the operands are the same or inverses of each other, simplifications
9453 will result.
9455 For example, (and (ior A B) (not B)) can occur as the result of
9456 expanding a bit field assignment. When we apply the distributive
9457 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9458 which then simplifies to (and (A (not B))).
9460 Note that no checks happen on the validity of applying the inverse
9461 distributive law. This is pointless since we can do it in the
9462 few places where this routine is called.
9464 N is the index of the term that is decomposed (the arithmetic operation,
9465 i.e. (+ A B) in the first example above). !N is the index of the term that
9466 is distributed, i.e. of C in the first example above. */
9467 static rtx
9468 distribute_and_simplify_rtx (rtx x, int n)
9470 machine_mode mode;
9471 enum rtx_code outer_code, inner_code;
9472 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9474 /* Distributivity is not true for floating point as it can change the
9475 value. So we don't do it unless -funsafe-math-optimizations. */
9476 if (FLOAT_MODE_P (GET_MODE (x))
9477 && ! flag_unsafe_math_optimizations)
9478 return NULL_RTX;
9480 decomposed = XEXP (x, n);
9481 if (!ARITHMETIC_P (decomposed))
9482 return NULL_RTX;
9484 mode = GET_MODE (x);
9485 outer_code = GET_CODE (x);
9486 distributed = XEXP (x, !n);
9488 inner_code = GET_CODE (decomposed);
9489 inner_op0 = XEXP (decomposed, 0);
9490 inner_op1 = XEXP (decomposed, 1);
9492 /* Special case (and (xor B C) (not A)), which is equivalent to
9493 (xor (ior A B) (ior A C)) */
9494 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9496 distributed = XEXP (distributed, 0);
9497 outer_code = IOR;
9500 if (n == 0)
9502 /* Distribute the second term. */
9503 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9504 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9506 else
9508 /* Distribute the first term. */
9509 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9510 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9513 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9514 new_op0, new_op1));
9515 if (GET_CODE (tmp) != outer_code
9516 && (set_src_cost (tmp, optimize_this_for_speed_p)
9517 < set_src_cost (x, optimize_this_for_speed_p)))
9518 return tmp;
9520 return NULL_RTX;
9523 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9524 in MODE. Return an equivalent form, if different from (and VAROP
9525 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9527 static rtx
9528 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9529 unsigned HOST_WIDE_INT constop)
9531 unsigned HOST_WIDE_INT nonzero;
9532 unsigned HOST_WIDE_INT orig_constop;
9533 rtx orig_varop;
9534 int i;
9536 orig_varop = varop;
9537 orig_constop = constop;
9538 if (GET_CODE (varop) == CLOBBER)
9539 return NULL_RTX;
9541 /* Simplify VAROP knowing that we will be only looking at some of the
9542 bits in it.
9544 Note by passing in CONSTOP, we guarantee that the bits not set in
9545 CONSTOP are not significant and will never be examined. We must
9546 ensure that is the case by explicitly masking out those bits
9547 before returning. */
9548 varop = force_to_mode (varop, mode, constop, 0);
9550 /* If VAROP is a CLOBBER, we will fail so return it. */
9551 if (GET_CODE (varop) == CLOBBER)
9552 return varop;
9554 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9555 to VAROP and return the new constant. */
9556 if (CONST_INT_P (varop))
9557 return gen_int_mode (INTVAL (varop) & constop, mode);
9559 /* See what bits may be nonzero in VAROP. Unlike the general case of
9560 a call to nonzero_bits, here we don't care about bits outside
9561 MODE. */
9563 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9565 /* Turn off all bits in the constant that are known to already be zero.
9566 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9567 which is tested below. */
9569 constop &= nonzero;
9571 /* If we don't have any bits left, return zero. */
9572 if (constop == 0)
9573 return const0_rtx;
9575 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9576 a power of two, we can replace this with an ASHIFT. */
9577 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9578 && (i = exact_log2 (constop)) >= 0)
9579 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9581 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9582 or XOR, then try to apply the distributive law. This may eliminate
9583 operations if either branch can be simplified because of the AND.
9584 It may also make some cases more complex, but those cases probably
9585 won't match a pattern either with or without this. */
9587 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9588 return
9589 gen_lowpart
9590 (mode,
9591 apply_distributive_law
9592 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9593 simplify_and_const_int (NULL_RTX,
9594 GET_MODE (varop),
9595 XEXP (varop, 0),
9596 constop),
9597 simplify_and_const_int (NULL_RTX,
9598 GET_MODE (varop),
9599 XEXP (varop, 1),
9600 constop))));
9602 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9603 the AND and see if one of the operands simplifies to zero. If so, we
9604 may eliminate it. */
9606 if (GET_CODE (varop) == PLUS
9607 && exact_log2 (constop + 1) >= 0)
9609 rtx o0, o1;
9611 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9612 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9613 if (o0 == const0_rtx)
9614 return o1;
9615 if (o1 == const0_rtx)
9616 return o0;
9619 /* Make a SUBREG if necessary. If we can't make it, fail. */
9620 varop = gen_lowpart (mode, varop);
9621 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9622 return NULL_RTX;
9624 /* If we are only masking insignificant bits, return VAROP. */
9625 if (constop == nonzero)
9626 return varop;
9628 if (varop == orig_varop && constop == orig_constop)
9629 return NULL_RTX;
9631 /* Otherwise, return an AND. */
9632 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9636 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9637 in MODE.
9639 Return an equivalent form, if different from X. Otherwise, return X. If
9640 X is zero, we are to always construct the equivalent form. */
9642 static rtx
9643 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9644 unsigned HOST_WIDE_INT constop)
9646 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9647 if (tem)
9648 return tem;
9650 if (!x)
9651 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9652 gen_int_mode (constop, mode));
9653 if (GET_MODE (x) != mode)
9654 x = gen_lowpart (mode, x);
9655 return x;
9658 /* Given a REG, X, compute which bits in X can be nonzero.
9659 We don't care about bits outside of those defined in MODE.
9661 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9662 a shift, AND, or zero_extract, we can do better. */
9664 static rtx
9665 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9666 const_rtx known_x ATTRIBUTE_UNUSED,
9667 machine_mode known_mode ATTRIBUTE_UNUSED,
9668 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9669 unsigned HOST_WIDE_INT *nonzero)
9671 rtx tem;
9672 reg_stat_type *rsp;
9674 /* If X is a register whose nonzero bits value is current, use it.
9675 Otherwise, if X is a register whose value we can find, use that
9676 value. Otherwise, use the previously-computed global nonzero bits
9677 for this register. */
9679 rsp = &reg_stat[REGNO (x)];
9680 if (rsp->last_set_value != 0
9681 && (rsp->last_set_mode == mode
9682 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9683 && GET_MODE_CLASS (mode) == MODE_INT))
9684 && ((rsp->last_set_label >= label_tick_ebb_start
9685 && rsp->last_set_label < label_tick)
9686 || (rsp->last_set_label == label_tick
9687 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9688 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9689 && REG_N_SETS (REGNO (x)) == 1
9690 && !REGNO_REG_SET_P
9691 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9692 REGNO (x)))))
9694 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9696 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9697 /* We don't know anything about the upper bits. */
9698 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9700 *nonzero &= mask;
9701 return NULL;
9704 tem = get_last_value (x);
9706 if (tem)
9708 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9709 /* If X is narrower than MODE and TEM is a non-negative
9710 constant that would appear negative in the mode of X,
9711 sign-extend it for use in reg_nonzero_bits because some
9712 machines (maybe most) will actually do the sign-extension
9713 and this is the conservative approach.
9715 ??? For 2.5, try to tighten up the MD files in this regard
9716 instead of this kludge. */
9718 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9719 && CONST_INT_P (tem)
9720 && INTVAL (tem) > 0
9721 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9722 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9723 #endif
9724 return tem;
9726 else if (nonzero_sign_valid && rsp->nonzero_bits)
9728 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9730 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9731 /* We don't know anything about the upper bits. */
9732 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9734 *nonzero &= mask;
9737 return NULL;
9740 /* Return the number of bits at the high-order end of X that are known to
9741 be equal to the sign bit. X will be used in mode MODE; if MODE is
9742 VOIDmode, X will be used in its own mode. The returned value will always
9743 be between 1 and the number of bits in MODE. */
9745 static rtx
9746 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9747 const_rtx known_x ATTRIBUTE_UNUSED,
9748 machine_mode known_mode
9749 ATTRIBUTE_UNUSED,
9750 unsigned int known_ret ATTRIBUTE_UNUSED,
9751 unsigned int *result)
9753 rtx tem;
9754 reg_stat_type *rsp;
9756 rsp = &reg_stat[REGNO (x)];
9757 if (rsp->last_set_value != 0
9758 && rsp->last_set_mode == mode
9759 && ((rsp->last_set_label >= label_tick_ebb_start
9760 && rsp->last_set_label < label_tick)
9761 || (rsp->last_set_label == label_tick
9762 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9763 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9764 && REG_N_SETS (REGNO (x)) == 1
9765 && !REGNO_REG_SET_P
9766 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9767 REGNO (x)))))
9769 *result = rsp->last_set_sign_bit_copies;
9770 return NULL;
9773 tem = get_last_value (x);
9774 if (tem != 0)
9775 return tem;
9777 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9778 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9779 *result = rsp->sign_bit_copies;
9781 return NULL;
9784 /* Return the number of "extended" bits there are in X, when interpreted
9785 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9786 unsigned quantities, this is the number of high-order zero bits.
9787 For signed quantities, this is the number of copies of the sign bit
9788 minus 1. In both case, this function returns the number of "spare"
9789 bits. For example, if two quantities for which this function returns
9790 at least 1 are added, the addition is known not to overflow.
9792 This function will always return 0 unless called during combine, which
9793 implies that it must be called from a define_split. */
9795 unsigned int
9796 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9798 if (nonzero_sign_valid == 0)
9799 return 0;
9801 return (unsignedp
9802 ? (HWI_COMPUTABLE_MODE_P (mode)
9803 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9804 - floor_log2 (nonzero_bits (x, mode)))
9805 : 0)
9806 : num_sign_bit_copies (x, mode) - 1);
9809 /* This function is called from `simplify_shift_const' to merge two
9810 outer operations. Specifically, we have already found that we need
9811 to perform operation *POP0 with constant *PCONST0 at the outermost
9812 position. We would now like to also perform OP1 with constant CONST1
9813 (with *POP0 being done last).
9815 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9816 the resulting operation. *PCOMP_P is set to 1 if we would need to
9817 complement the innermost operand, otherwise it is unchanged.
9819 MODE is the mode in which the operation will be done. No bits outside
9820 the width of this mode matter. It is assumed that the width of this mode
9821 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9823 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9824 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9825 result is simply *PCONST0.
9827 If the resulting operation cannot be expressed as one operation, we
9828 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9830 static int
9831 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)
9833 enum rtx_code op0 = *pop0;
9834 HOST_WIDE_INT const0 = *pconst0;
9836 const0 &= GET_MODE_MASK (mode);
9837 const1 &= GET_MODE_MASK (mode);
9839 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9840 if (op0 == AND)
9841 const1 &= const0;
9843 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9844 if OP0 is SET. */
9846 if (op1 == UNKNOWN || op0 == SET)
9847 return 1;
9849 else if (op0 == UNKNOWN)
9850 op0 = op1, const0 = const1;
9852 else if (op0 == op1)
9854 switch (op0)
9856 case AND:
9857 const0 &= const1;
9858 break;
9859 case IOR:
9860 const0 |= const1;
9861 break;
9862 case XOR:
9863 const0 ^= const1;
9864 break;
9865 case PLUS:
9866 const0 += const1;
9867 break;
9868 case NEG:
9869 op0 = UNKNOWN;
9870 break;
9871 default:
9872 break;
9876 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9877 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9878 return 0;
9880 /* If the two constants aren't the same, we can't do anything. The
9881 remaining six cases can all be done. */
9882 else if (const0 != const1)
9883 return 0;
9885 else
9886 switch (op0)
9888 case IOR:
9889 if (op1 == AND)
9890 /* (a & b) | b == b */
9891 op0 = SET;
9892 else /* op1 == XOR */
9893 /* (a ^ b) | b == a | b */
9895 break;
9897 case XOR:
9898 if (op1 == AND)
9899 /* (a & b) ^ b == (~a) & b */
9900 op0 = AND, *pcomp_p = 1;
9901 else /* op1 == IOR */
9902 /* (a | b) ^ b == a & ~b */
9903 op0 = AND, const0 = ~const0;
9904 break;
9906 case AND:
9907 if (op1 == IOR)
9908 /* (a | b) & b == b */
9909 op0 = SET;
9910 else /* op1 == XOR */
9911 /* (a ^ b) & b) == (~a) & b */
9912 *pcomp_p = 1;
9913 break;
9914 default:
9915 break;
9918 /* Check for NO-OP cases. */
9919 const0 &= GET_MODE_MASK (mode);
9920 if (const0 == 0
9921 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9922 op0 = UNKNOWN;
9923 else if (const0 == 0 && op0 == AND)
9924 op0 = SET;
9925 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9926 && op0 == AND)
9927 op0 = UNKNOWN;
9929 *pop0 = op0;
9931 /* ??? Slightly redundant with the above mask, but not entirely.
9932 Moving this above means we'd have to sign-extend the mode mask
9933 for the final test. */
9934 if (op0 != UNKNOWN && op0 != NEG)
9935 *pconst0 = trunc_int_for_mode (const0, mode);
9937 return 1;
9940 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9941 the shift in. The original shift operation CODE is performed on OP in
9942 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9943 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9944 result of the shift is subject to operation OUTER_CODE with operand
9945 OUTER_CONST. */
9947 static machine_mode
9948 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9949 machine_mode orig_mode, machine_mode mode,
9950 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9952 if (orig_mode == mode)
9953 return mode;
9954 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9956 /* In general we can't perform in wider mode for right shift and rotate. */
9957 switch (code)
9959 case ASHIFTRT:
9960 /* We can still widen if the bits brought in from the left are identical
9961 to the sign bit of ORIG_MODE. */
9962 if (num_sign_bit_copies (op, mode)
9963 > (unsigned) (GET_MODE_PRECISION (mode)
9964 - GET_MODE_PRECISION (orig_mode)))
9965 return mode;
9966 return orig_mode;
9968 case LSHIFTRT:
9969 /* Similarly here but with zero bits. */
9970 if (HWI_COMPUTABLE_MODE_P (mode)
9971 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9972 return mode;
9974 /* We can also widen if the bits brought in will be masked off. This
9975 operation is performed in ORIG_MODE. */
9976 if (outer_code == AND)
9978 int care_bits = low_bitmask_len (orig_mode, outer_const);
9980 if (care_bits >= 0
9981 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9982 return mode;
9984 /* fall through */
9986 case ROTATE:
9987 return orig_mode;
9989 case ROTATERT:
9990 gcc_unreachable ();
9992 default:
9993 return mode;
9997 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9998 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9999 if we cannot simplify it. Otherwise, return a simplified value.
10001 The shift is normally computed in the widest mode we find in VAROP, as
10002 long as it isn't a different number of words than RESULT_MODE. Exceptions
10003 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10005 static rtx
10006 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10007 rtx varop, int orig_count)
10009 enum rtx_code orig_code = code;
10010 rtx orig_varop = varop;
10011 int count;
10012 machine_mode mode = result_mode;
10013 machine_mode shift_mode, tmode;
10014 unsigned int mode_words
10015 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10016 /* We form (outer_op (code varop count) (outer_const)). */
10017 enum rtx_code outer_op = UNKNOWN;
10018 HOST_WIDE_INT outer_const = 0;
10019 int complement_p = 0;
10020 rtx new_rtx, x;
10022 /* Make sure and truncate the "natural" shift on the way in. We don't
10023 want to do this inside the loop as it makes it more difficult to
10024 combine shifts. */
10025 if (SHIFT_COUNT_TRUNCATED)
10026 orig_count &= GET_MODE_BITSIZE (mode) - 1;
10028 /* If we were given an invalid count, don't do anything except exactly
10029 what was requested. */
10031 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
10032 return NULL_RTX;
10034 count = orig_count;
10036 /* Unless one of the branches of the `if' in this loop does a `continue',
10037 we will `break' the loop after the `if'. */
10039 while (count != 0)
10041 /* If we have an operand of (clobber (const_int 0)), fail. */
10042 if (GET_CODE (varop) == CLOBBER)
10043 return NULL_RTX;
10045 /* Convert ROTATERT to ROTATE. */
10046 if (code == ROTATERT)
10048 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
10049 code = ROTATE;
10050 if (VECTOR_MODE_P (result_mode))
10051 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10052 else
10053 count = bitsize - count;
10056 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10057 mode, outer_op, outer_const);
10059 /* Handle cases where the count is greater than the size of the mode
10060 minus 1. For ASHIFT, use the size minus one as the count (this can
10061 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10062 take the count modulo the size. For other shifts, the result is
10063 zero.
10065 Since these shifts are being produced by the compiler by combining
10066 multiple operations, each of which are defined, we know what the
10067 result is supposed to be. */
10069 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10071 if (code == ASHIFTRT)
10072 count = GET_MODE_PRECISION (shift_mode) - 1;
10073 else if (code == ROTATE || code == ROTATERT)
10074 count %= GET_MODE_PRECISION (shift_mode);
10075 else
10077 /* We can't simply return zero because there may be an
10078 outer op. */
10079 varop = const0_rtx;
10080 count = 0;
10081 break;
10085 /* If we discovered we had to complement VAROP, leave. Making a NOT
10086 here would cause an infinite loop. */
10087 if (complement_p)
10088 break;
10090 /* An arithmetic right shift of a quantity known to be -1 or 0
10091 is a no-op. */
10092 if (code == ASHIFTRT
10093 && (num_sign_bit_copies (varop, shift_mode)
10094 == GET_MODE_PRECISION (shift_mode)))
10096 count = 0;
10097 break;
10100 /* If we are doing an arithmetic right shift and discarding all but
10101 the sign bit copies, this is equivalent to doing a shift by the
10102 bitsize minus one. Convert it into that shift because it will often
10103 allow other simplifications. */
10105 if (code == ASHIFTRT
10106 && (count + num_sign_bit_copies (varop, shift_mode)
10107 >= GET_MODE_PRECISION (shift_mode)))
10108 count = GET_MODE_PRECISION (shift_mode) - 1;
10110 /* We simplify the tests below and elsewhere by converting
10111 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10112 `make_compound_operation' will convert it to an ASHIFTRT for
10113 those machines (such as VAX) that don't have an LSHIFTRT. */
10114 if (code == ASHIFTRT
10115 && val_signbit_known_clear_p (shift_mode,
10116 nonzero_bits (varop, shift_mode)))
10117 code = LSHIFTRT;
10119 if (((code == LSHIFTRT
10120 && HWI_COMPUTABLE_MODE_P (shift_mode)
10121 && !(nonzero_bits (varop, shift_mode) >> count))
10122 || (code == ASHIFT
10123 && HWI_COMPUTABLE_MODE_P (shift_mode)
10124 && !((nonzero_bits (varop, shift_mode) << count)
10125 & GET_MODE_MASK (shift_mode))))
10126 && !side_effects_p (varop))
10127 varop = const0_rtx;
10129 switch (GET_CODE (varop))
10131 case SIGN_EXTEND:
10132 case ZERO_EXTEND:
10133 case SIGN_EXTRACT:
10134 case ZERO_EXTRACT:
10135 new_rtx = expand_compound_operation (varop);
10136 if (new_rtx != varop)
10138 varop = new_rtx;
10139 continue;
10141 break;
10143 case MEM:
10144 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10145 minus the width of a smaller mode, we can do this with a
10146 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10147 if ((code == ASHIFTRT || code == LSHIFTRT)
10148 && ! mode_dependent_address_p (XEXP (varop, 0),
10149 MEM_ADDR_SPACE (varop))
10150 && ! MEM_VOLATILE_P (varop)
10151 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10152 MODE_INT, 1)) != BLKmode)
10154 new_rtx = adjust_address_nv (varop, tmode,
10155 BYTES_BIG_ENDIAN ? 0
10156 : count / BITS_PER_UNIT);
10158 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10159 : ZERO_EXTEND, mode, new_rtx);
10160 count = 0;
10161 continue;
10163 break;
10165 case SUBREG:
10166 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10167 the same number of words as what we've seen so far. Then store
10168 the widest mode in MODE. */
10169 if (subreg_lowpart_p (varop)
10170 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10171 > GET_MODE_SIZE (GET_MODE (varop)))
10172 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10173 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10174 == mode_words
10175 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10176 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10178 varop = SUBREG_REG (varop);
10179 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10180 mode = GET_MODE (varop);
10181 continue;
10183 break;
10185 case MULT:
10186 /* Some machines use MULT instead of ASHIFT because MULT
10187 is cheaper. But it is still better on those machines to
10188 merge two shifts into one. */
10189 if (CONST_INT_P (XEXP (varop, 1))
10190 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10192 varop
10193 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10194 XEXP (varop, 0),
10195 GEN_INT (exact_log2 (
10196 UINTVAL (XEXP (varop, 1)))));
10197 continue;
10199 break;
10201 case UDIV:
10202 /* Similar, for when divides are cheaper. */
10203 if (CONST_INT_P (XEXP (varop, 1))
10204 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10206 varop
10207 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10208 XEXP (varop, 0),
10209 GEN_INT (exact_log2 (
10210 UINTVAL (XEXP (varop, 1)))));
10211 continue;
10213 break;
10215 case ASHIFTRT:
10216 /* If we are extracting just the sign bit of an arithmetic
10217 right shift, that shift is not needed. However, the sign
10218 bit of a wider mode may be different from what would be
10219 interpreted as the sign bit in a narrower mode, so, if
10220 the result is narrower, don't discard the shift. */
10221 if (code == LSHIFTRT
10222 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10223 && (GET_MODE_BITSIZE (result_mode)
10224 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10226 varop = XEXP (varop, 0);
10227 continue;
10230 /* ... fall through ... */
10232 case LSHIFTRT:
10233 case ASHIFT:
10234 case ROTATE:
10235 /* Here we have two nested shifts. The result is usually the
10236 AND of a new shift with a mask. We compute the result below. */
10237 if (CONST_INT_P (XEXP (varop, 1))
10238 && INTVAL (XEXP (varop, 1)) >= 0
10239 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10240 && HWI_COMPUTABLE_MODE_P (result_mode)
10241 && HWI_COMPUTABLE_MODE_P (mode)
10242 && !VECTOR_MODE_P (result_mode))
10244 enum rtx_code first_code = GET_CODE (varop);
10245 unsigned int first_count = INTVAL (XEXP (varop, 1));
10246 unsigned HOST_WIDE_INT mask;
10247 rtx mask_rtx;
10249 /* We have one common special case. We can't do any merging if
10250 the inner code is an ASHIFTRT of a smaller mode. However, if
10251 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10252 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10253 we can convert it to
10254 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10255 This simplifies certain SIGN_EXTEND operations. */
10256 if (code == ASHIFT && first_code == ASHIFTRT
10257 && count == (GET_MODE_PRECISION (result_mode)
10258 - GET_MODE_PRECISION (GET_MODE (varop))))
10260 /* C3 has the low-order C1 bits zero. */
10262 mask = GET_MODE_MASK (mode)
10263 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10265 varop = simplify_and_const_int (NULL_RTX, result_mode,
10266 XEXP (varop, 0), mask);
10267 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10268 varop, count);
10269 count = first_count;
10270 code = ASHIFTRT;
10271 continue;
10274 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10275 than C1 high-order bits equal to the sign bit, we can convert
10276 this to either an ASHIFT or an ASHIFTRT depending on the
10277 two counts.
10279 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10281 if (code == ASHIFTRT && first_code == ASHIFT
10282 && GET_MODE (varop) == shift_mode
10283 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10284 > first_count))
10286 varop = XEXP (varop, 0);
10287 count -= first_count;
10288 if (count < 0)
10290 count = -count;
10291 code = ASHIFT;
10294 continue;
10297 /* There are some cases we can't do. If CODE is ASHIFTRT,
10298 we can only do this if FIRST_CODE is also ASHIFTRT.
10300 We can't do the case when CODE is ROTATE and FIRST_CODE is
10301 ASHIFTRT.
10303 If the mode of this shift is not the mode of the outer shift,
10304 we can't do this if either shift is a right shift or ROTATE.
10306 Finally, we can't do any of these if the mode is too wide
10307 unless the codes are the same.
10309 Handle the case where the shift codes are the same
10310 first. */
10312 if (code == first_code)
10314 if (GET_MODE (varop) != result_mode
10315 && (code == ASHIFTRT || code == LSHIFTRT
10316 || code == ROTATE))
10317 break;
10319 count += first_count;
10320 varop = XEXP (varop, 0);
10321 continue;
10324 if (code == ASHIFTRT
10325 || (code == ROTATE && first_code == ASHIFTRT)
10326 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10327 || (GET_MODE (varop) != result_mode
10328 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10329 || first_code == ROTATE
10330 || code == ROTATE)))
10331 break;
10333 /* To compute the mask to apply after the shift, shift the
10334 nonzero bits of the inner shift the same way the
10335 outer shift will. */
10337 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10338 result_mode);
10340 mask_rtx
10341 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10342 GEN_INT (count));
10344 /* Give up if we can't compute an outer operation to use. */
10345 if (mask_rtx == 0
10346 || !CONST_INT_P (mask_rtx)
10347 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10348 INTVAL (mask_rtx),
10349 result_mode, &complement_p))
10350 break;
10352 /* If the shifts are in the same direction, we add the
10353 counts. Otherwise, we subtract them. */
10354 if ((code == ASHIFTRT || code == LSHIFTRT)
10355 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10356 count += first_count;
10357 else
10358 count -= first_count;
10360 /* If COUNT is positive, the new shift is usually CODE,
10361 except for the two exceptions below, in which case it is
10362 FIRST_CODE. If the count is negative, FIRST_CODE should
10363 always be used */
10364 if (count > 0
10365 && ((first_code == ROTATE && code == ASHIFT)
10366 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10367 code = first_code;
10368 else if (count < 0)
10369 code = first_code, count = -count;
10371 varop = XEXP (varop, 0);
10372 continue;
10375 /* If we have (A << B << C) for any shift, we can convert this to
10376 (A << C << B). This wins if A is a constant. Only try this if
10377 B is not a constant. */
10379 else if (GET_CODE (varop) == code
10380 && CONST_INT_P (XEXP (varop, 0))
10381 && !CONST_INT_P (XEXP (varop, 1)))
10383 rtx new_rtx = simplify_const_binary_operation (code, mode,
10384 XEXP (varop, 0),
10385 GEN_INT (count));
10386 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10387 count = 0;
10388 continue;
10390 break;
10392 case NOT:
10393 if (VECTOR_MODE_P (mode))
10394 break;
10396 /* Make this fit the case below. */
10397 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10398 continue;
10400 case IOR:
10401 case AND:
10402 case XOR:
10403 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10404 with C the size of VAROP - 1 and the shift is logical if
10405 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10406 we have an (le X 0) operation. If we have an arithmetic shift
10407 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10408 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10410 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10411 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10412 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10413 && (code == LSHIFTRT || code == ASHIFTRT)
10414 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10415 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10417 count = 0;
10418 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10419 const0_rtx);
10421 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10422 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10424 continue;
10427 /* If we have (shift (logical)), move the logical to the outside
10428 to allow it to possibly combine with another logical and the
10429 shift to combine with another shift. This also canonicalizes to
10430 what a ZERO_EXTRACT looks like. Also, some machines have
10431 (and (shift)) insns. */
10433 if (CONST_INT_P (XEXP (varop, 1))
10434 /* We can't do this if we have (ashiftrt (xor)) and the
10435 constant has its sign bit set in shift_mode with shift_mode
10436 wider than result_mode. */
10437 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10438 && result_mode != shift_mode
10439 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10440 shift_mode))
10441 && (new_rtx = simplify_const_binary_operation
10442 (code, result_mode,
10443 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10444 GEN_INT (count))) != 0
10445 && CONST_INT_P (new_rtx)
10446 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10447 INTVAL (new_rtx), result_mode, &complement_p))
10449 varop = XEXP (varop, 0);
10450 continue;
10453 /* If we can't do that, try to simplify the shift in each arm of the
10454 logical expression, make a new logical expression, and apply
10455 the inverse distributive law. This also can't be done for
10456 (ashiftrt (xor)) where we've widened the shift and the constant
10457 changes the sign bit. */
10458 if (CONST_INT_P (XEXP (varop, 1))
10459 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10460 && result_mode != shift_mode
10461 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10462 shift_mode)))
10464 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10465 XEXP (varop, 0), count);
10466 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10467 XEXP (varop, 1), count);
10469 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10470 lhs, rhs);
10471 varop = apply_distributive_law (varop);
10473 count = 0;
10474 continue;
10476 break;
10478 case EQ:
10479 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10480 says that the sign bit can be tested, FOO has mode MODE, C is
10481 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10482 that may be nonzero. */
10483 if (code == LSHIFTRT
10484 && XEXP (varop, 1) == const0_rtx
10485 && GET_MODE (XEXP (varop, 0)) == result_mode
10486 && count == (GET_MODE_PRECISION (result_mode) - 1)
10487 && HWI_COMPUTABLE_MODE_P (result_mode)
10488 && STORE_FLAG_VALUE == -1
10489 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10490 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10491 &complement_p))
10493 varop = XEXP (varop, 0);
10494 count = 0;
10495 continue;
10497 break;
10499 case NEG:
10500 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10501 than the number of bits in the mode is equivalent to A. */
10502 if (code == LSHIFTRT
10503 && count == (GET_MODE_PRECISION (result_mode) - 1)
10504 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10506 varop = XEXP (varop, 0);
10507 count = 0;
10508 continue;
10511 /* NEG commutes with ASHIFT since it is multiplication. Move the
10512 NEG outside to allow shifts to combine. */
10513 if (code == ASHIFT
10514 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10515 &complement_p))
10517 varop = XEXP (varop, 0);
10518 continue;
10520 break;
10522 case PLUS:
10523 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10524 is one less than the number of bits in the mode is
10525 equivalent to (xor A 1). */
10526 if (code == LSHIFTRT
10527 && count == (GET_MODE_PRECISION (result_mode) - 1)
10528 && XEXP (varop, 1) == constm1_rtx
10529 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10530 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10531 &complement_p))
10533 count = 0;
10534 varop = XEXP (varop, 0);
10535 continue;
10538 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10539 that might be nonzero in BAR are those being shifted out and those
10540 bits are known zero in FOO, we can replace the PLUS with FOO.
10541 Similarly in the other operand order. This code occurs when
10542 we are computing the size of a variable-size array. */
10544 if ((code == ASHIFTRT || code == LSHIFTRT)
10545 && count < HOST_BITS_PER_WIDE_INT
10546 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10547 && (nonzero_bits (XEXP (varop, 1), result_mode)
10548 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10550 varop = XEXP (varop, 0);
10551 continue;
10553 else if ((code == ASHIFTRT || code == LSHIFTRT)
10554 && count < HOST_BITS_PER_WIDE_INT
10555 && HWI_COMPUTABLE_MODE_P (result_mode)
10556 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10557 >> count)
10558 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10559 & nonzero_bits (XEXP (varop, 1),
10560 result_mode)))
10562 varop = XEXP (varop, 1);
10563 continue;
10566 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10567 if (code == ASHIFT
10568 && CONST_INT_P (XEXP (varop, 1))
10569 && (new_rtx = simplify_const_binary_operation
10570 (ASHIFT, result_mode,
10571 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10572 GEN_INT (count))) != 0
10573 && CONST_INT_P (new_rtx)
10574 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10575 INTVAL (new_rtx), result_mode, &complement_p))
10577 varop = XEXP (varop, 0);
10578 continue;
10581 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10582 signbit', and attempt to change the PLUS to an XOR and move it to
10583 the outer operation as is done above in the AND/IOR/XOR case
10584 leg for shift(logical). See details in logical handling above
10585 for reasoning in doing so. */
10586 if (code == LSHIFTRT
10587 && CONST_INT_P (XEXP (varop, 1))
10588 && mode_signbit_p (result_mode, XEXP (varop, 1))
10589 && (new_rtx = simplify_const_binary_operation
10590 (code, result_mode,
10591 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10592 GEN_INT (count))) != 0
10593 && CONST_INT_P (new_rtx)
10594 && merge_outer_ops (&outer_op, &outer_const, XOR,
10595 INTVAL (new_rtx), result_mode, &complement_p))
10597 varop = XEXP (varop, 0);
10598 continue;
10601 break;
10603 case MINUS:
10604 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10605 with C the size of VAROP - 1 and the shift is logical if
10606 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10607 we have a (gt X 0) operation. If the shift is arithmetic with
10608 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10609 we have a (neg (gt X 0)) operation. */
10611 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10612 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10613 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10614 && (code == LSHIFTRT || code == ASHIFTRT)
10615 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10616 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10617 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10619 count = 0;
10620 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10621 const0_rtx);
10623 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10624 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10626 continue;
10628 break;
10630 case TRUNCATE:
10631 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10632 if the truncate does not affect the value. */
10633 if (code == LSHIFTRT
10634 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10635 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10636 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10637 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10638 - GET_MODE_PRECISION (GET_MODE (varop)))))
10640 rtx varop_inner = XEXP (varop, 0);
10642 varop_inner
10643 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10644 XEXP (varop_inner, 0),
10645 GEN_INT
10646 (count + INTVAL (XEXP (varop_inner, 1))));
10647 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10648 count = 0;
10649 continue;
10651 break;
10653 default:
10654 break;
10657 break;
10660 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10661 outer_op, outer_const);
10663 /* We have now finished analyzing the shift. The result should be
10664 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10665 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10666 to the result of the shift. OUTER_CONST is the relevant constant,
10667 but we must turn off all bits turned off in the shift. */
10669 if (outer_op == UNKNOWN
10670 && orig_code == code && orig_count == count
10671 && varop == orig_varop
10672 && shift_mode == GET_MODE (varop))
10673 return NULL_RTX;
10675 /* Make a SUBREG if necessary. If we can't make it, fail. */
10676 varop = gen_lowpart (shift_mode, varop);
10677 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10678 return NULL_RTX;
10680 /* If we have an outer operation and we just made a shift, it is
10681 possible that we could have simplified the shift were it not
10682 for the outer operation. So try to do the simplification
10683 recursively. */
10685 if (outer_op != UNKNOWN)
10686 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10687 else
10688 x = NULL_RTX;
10690 if (x == NULL_RTX)
10691 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10693 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10694 turn off all the bits that the shift would have turned off. */
10695 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10696 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10697 GET_MODE_MASK (result_mode) >> orig_count);
10699 /* Do the remainder of the processing in RESULT_MODE. */
10700 x = gen_lowpart_or_truncate (result_mode, x);
10702 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10703 operation. */
10704 if (complement_p)
10705 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10707 if (outer_op != UNKNOWN)
10709 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10710 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10711 outer_const = trunc_int_for_mode (outer_const, result_mode);
10713 if (outer_op == AND)
10714 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10715 else if (outer_op == SET)
10717 /* This means that we have determined that the result is
10718 equivalent to a constant. This should be rare. */
10719 if (!side_effects_p (x))
10720 x = GEN_INT (outer_const);
10722 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10723 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10724 else
10725 x = simplify_gen_binary (outer_op, result_mode, x,
10726 GEN_INT (outer_const));
10729 return x;
10732 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10733 The result of the shift is RESULT_MODE. If we cannot simplify it,
10734 return X or, if it is NULL, synthesize the expression with
10735 simplify_gen_binary. Otherwise, return a simplified value.
10737 The shift is normally computed in the widest mode we find in VAROP, as
10738 long as it isn't a different number of words than RESULT_MODE. Exceptions
10739 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10741 static rtx
10742 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10743 rtx varop, int count)
10745 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10746 if (tem)
10747 return tem;
10749 if (!x)
10750 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10751 if (GET_MODE (x) != result_mode)
10752 x = gen_lowpart (result_mode, x);
10753 return x;
10757 /* Like recog, but we receive the address of a pointer to a new pattern.
10758 We try to match the rtx that the pointer points to.
10759 If that fails, we may try to modify or replace the pattern,
10760 storing the replacement into the same pointer object.
10762 Modifications include deletion or addition of CLOBBERs.
10764 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10765 the CLOBBERs are placed.
10767 The value is the final insn code from the pattern ultimately matched,
10768 or -1. */
10770 static int
10771 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10773 rtx pat = *pnewpat;
10774 rtx pat_without_clobbers;
10775 int insn_code_number;
10776 int num_clobbers_to_add = 0;
10777 int i;
10778 rtx notes = NULL_RTX;
10779 rtx old_notes, old_pat;
10780 int old_icode;
10782 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10783 we use to indicate that something didn't match. If we find such a
10784 thing, force rejection. */
10785 if (GET_CODE (pat) == PARALLEL)
10786 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10787 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10788 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10789 return -1;
10791 old_pat = PATTERN (insn);
10792 old_notes = REG_NOTES (insn);
10793 PATTERN (insn) = pat;
10794 REG_NOTES (insn) = NULL_RTX;
10796 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10797 if (dump_file && (dump_flags & TDF_DETAILS))
10799 if (insn_code_number < 0)
10800 fputs ("Failed to match this instruction:\n", dump_file);
10801 else
10802 fputs ("Successfully matched this instruction:\n", dump_file);
10803 print_rtl_single (dump_file, pat);
10806 /* If it isn't, there is the possibility that we previously had an insn
10807 that clobbered some register as a side effect, but the combined
10808 insn doesn't need to do that. So try once more without the clobbers
10809 unless this represents an ASM insn. */
10811 if (insn_code_number < 0 && ! check_asm_operands (pat)
10812 && GET_CODE (pat) == PARALLEL)
10814 int pos;
10816 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10817 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10819 if (i != pos)
10820 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10821 pos++;
10824 SUBST_INT (XVECLEN (pat, 0), pos);
10826 if (pos == 1)
10827 pat = XVECEXP (pat, 0, 0);
10829 PATTERN (insn) = pat;
10830 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10831 if (dump_file && (dump_flags & TDF_DETAILS))
10833 if (insn_code_number < 0)
10834 fputs ("Failed to match this instruction:\n", dump_file);
10835 else
10836 fputs ("Successfully matched this instruction:\n", dump_file);
10837 print_rtl_single (dump_file, pat);
10841 pat_without_clobbers = pat;
10843 PATTERN (insn) = old_pat;
10844 REG_NOTES (insn) = old_notes;
10846 /* Recognize all noop sets, these will be killed by followup pass. */
10847 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10848 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10850 /* If we had any clobbers to add, make a new pattern than contains
10851 them. Then check to make sure that all of them are dead. */
10852 if (num_clobbers_to_add)
10854 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10855 rtvec_alloc (GET_CODE (pat) == PARALLEL
10856 ? (XVECLEN (pat, 0)
10857 + num_clobbers_to_add)
10858 : num_clobbers_to_add + 1));
10860 if (GET_CODE (pat) == PARALLEL)
10861 for (i = 0; i < XVECLEN (pat, 0); i++)
10862 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10863 else
10864 XVECEXP (newpat, 0, 0) = pat;
10866 add_clobbers (newpat, insn_code_number);
10868 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10869 i < XVECLEN (newpat, 0); i++)
10871 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10872 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10873 return -1;
10874 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10876 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10877 notes = alloc_reg_note (REG_UNUSED,
10878 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10881 pat = newpat;
10884 if (insn_code_number >= 0
10885 && insn_code_number != NOOP_MOVE_INSN_CODE)
10887 old_pat = PATTERN (insn);
10888 old_notes = REG_NOTES (insn);
10889 old_icode = INSN_CODE (insn);
10890 PATTERN (insn) = pat;
10891 REG_NOTES (insn) = notes;
10893 /* Allow targets to reject combined insn. */
10894 if (!targetm.legitimate_combined_insn (insn))
10896 if (dump_file && (dump_flags & TDF_DETAILS))
10897 fputs ("Instruction not appropriate for target.",
10898 dump_file);
10900 /* Callers expect recog_for_combine to strip
10901 clobbers from the pattern on failure. */
10902 pat = pat_without_clobbers;
10903 notes = NULL_RTX;
10905 insn_code_number = -1;
10908 PATTERN (insn) = old_pat;
10909 REG_NOTES (insn) = old_notes;
10910 INSN_CODE (insn) = old_icode;
10913 *pnewpat = pat;
10914 *pnotes = notes;
10916 return insn_code_number;
10919 /* Like gen_lowpart_general but for use by combine. In combine it
10920 is not possible to create any new pseudoregs. However, it is
10921 safe to create invalid memory addresses, because combine will
10922 try to recognize them and all they will do is make the combine
10923 attempt fail.
10925 If for some reason this cannot do its job, an rtx
10926 (clobber (const_int 0)) is returned.
10927 An insn containing that will not be recognized. */
10929 static rtx
10930 gen_lowpart_for_combine (machine_mode omode, rtx x)
10932 machine_mode imode = GET_MODE (x);
10933 unsigned int osize = GET_MODE_SIZE (omode);
10934 unsigned int isize = GET_MODE_SIZE (imode);
10935 rtx result;
10937 if (omode == imode)
10938 return x;
10940 /* We can only support MODE being wider than a word if X is a
10941 constant integer or has a mode the same size. */
10942 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10943 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10944 goto fail;
10946 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10947 won't know what to do. So we will strip off the SUBREG here and
10948 process normally. */
10949 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10951 x = SUBREG_REG (x);
10953 /* For use in case we fall down into the address adjustments
10954 further below, we need to adjust the known mode and size of
10955 x; imode and isize, since we just adjusted x. */
10956 imode = GET_MODE (x);
10958 if (imode == omode)
10959 return x;
10961 isize = GET_MODE_SIZE (imode);
10964 result = gen_lowpart_common (omode, x);
10966 if (result)
10967 return result;
10969 if (MEM_P (x))
10971 int offset = 0;
10973 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10974 address. */
10975 if (MEM_VOLATILE_P (x)
10976 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10977 goto fail;
10979 /* If we want to refer to something bigger than the original memref,
10980 generate a paradoxical subreg instead. That will force a reload
10981 of the original memref X. */
10982 if (isize < osize)
10983 return gen_rtx_SUBREG (omode, x, 0);
10985 if (WORDS_BIG_ENDIAN)
10986 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10988 /* Adjust the address so that the address-after-the-data is
10989 unchanged. */
10990 if (BYTES_BIG_ENDIAN)
10991 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10993 return adjust_address_nv (x, omode, offset);
10996 /* If X is a comparison operator, rewrite it in a new mode. This
10997 probably won't match, but may allow further simplifications. */
10998 else if (COMPARISON_P (x))
10999 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11001 /* If we couldn't simplify X any other way, just enclose it in a
11002 SUBREG. Normally, this SUBREG won't match, but some patterns may
11003 include an explicit SUBREG or we may simplify it further in combine. */
11004 else
11006 int offset = 0;
11007 rtx res;
11009 offset = subreg_lowpart_offset (omode, imode);
11010 if (imode == VOIDmode)
11012 imode = int_mode_for_mode (omode);
11013 x = gen_lowpart_common (imode, x);
11014 if (x == NULL)
11015 goto fail;
11017 res = simplify_gen_subreg (omode, x, imode, offset);
11018 if (res)
11019 return res;
11022 fail:
11023 return gen_rtx_CLOBBER (omode, const0_rtx);
11026 /* Try to simplify a comparison between OP0 and a constant OP1,
11027 where CODE is the comparison code that will be tested, into a
11028 (CODE OP0 const0_rtx) form.
11030 The result is a possibly different comparison code to use.
11031 *POP1 may be updated. */
11033 static enum rtx_code
11034 simplify_compare_const (enum rtx_code code, machine_mode mode,
11035 rtx op0, rtx *pop1)
11037 unsigned int mode_width = GET_MODE_PRECISION (mode);
11038 HOST_WIDE_INT const_op = INTVAL (*pop1);
11040 /* Get the constant we are comparing against and turn off all bits
11041 not on in our mode. */
11042 if (mode != VOIDmode)
11043 const_op = trunc_int_for_mode (const_op, mode);
11045 /* If we are comparing against a constant power of two and the value
11046 being compared can only have that single bit nonzero (e.g., it was
11047 `and'ed with that bit), we can replace this with a comparison
11048 with zero. */
11049 if (const_op
11050 && (code == EQ || code == NE || code == GE || code == GEU
11051 || code == LT || code == LTU)
11052 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11053 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11054 && (nonzero_bits (op0, mode)
11055 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11057 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11058 const_op = 0;
11061 /* Similarly, if we are comparing a value known to be either -1 or
11062 0 with -1, change it to the opposite comparison against zero. */
11063 if (const_op == -1
11064 && (code == EQ || code == NE || code == GT || code == LE
11065 || code == GEU || code == LTU)
11066 && num_sign_bit_copies (op0, mode) == mode_width)
11068 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11069 const_op = 0;
11072 /* Do some canonicalizations based on the comparison code. We prefer
11073 comparisons against zero and then prefer equality comparisons.
11074 If we can reduce the size of a constant, we will do that too. */
11075 switch (code)
11077 case LT:
11078 /* < C is equivalent to <= (C - 1) */
11079 if (const_op > 0)
11081 const_op -= 1;
11082 code = LE;
11083 /* ... fall through to LE case below. */
11085 else
11086 break;
11088 case LE:
11089 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11090 if (const_op < 0)
11092 const_op += 1;
11093 code = LT;
11096 /* If we are doing a <= 0 comparison on a value known to have
11097 a zero sign bit, we can replace this with == 0. */
11098 else if (const_op == 0
11099 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11100 && (nonzero_bits (op0, mode)
11101 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11102 == 0)
11103 code = EQ;
11104 break;
11106 case GE:
11107 /* >= C is equivalent to > (C - 1). */
11108 if (const_op > 0)
11110 const_op -= 1;
11111 code = GT;
11112 /* ... fall through to GT below. */
11114 else
11115 break;
11117 case GT:
11118 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11119 if (const_op < 0)
11121 const_op += 1;
11122 code = GE;
11125 /* If we are doing a > 0 comparison on a value known to have
11126 a zero sign bit, we can replace this with != 0. */
11127 else if (const_op == 0
11128 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11129 && (nonzero_bits (op0, mode)
11130 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11131 == 0)
11132 code = NE;
11133 break;
11135 case LTU:
11136 /* < C is equivalent to <= (C - 1). */
11137 if (const_op > 0)
11139 const_op -= 1;
11140 code = LEU;
11141 /* ... fall through ... */
11143 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11144 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11145 && (unsigned HOST_WIDE_INT) const_op
11146 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11148 const_op = 0;
11149 code = GE;
11150 break;
11152 else
11153 break;
11155 case LEU:
11156 /* unsigned <= 0 is equivalent to == 0 */
11157 if (const_op == 0)
11158 code = EQ;
11159 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11160 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11161 && (unsigned HOST_WIDE_INT) const_op
11162 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11164 const_op = 0;
11165 code = GE;
11167 break;
11169 case GEU:
11170 /* >= C is equivalent to > (C - 1). */
11171 if (const_op > 1)
11173 const_op -= 1;
11174 code = GTU;
11175 /* ... fall through ... */
11178 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11179 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11180 && (unsigned HOST_WIDE_INT) const_op
11181 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11183 const_op = 0;
11184 code = LT;
11185 break;
11187 else
11188 break;
11190 case GTU:
11191 /* unsigned > 0 is equivalent to != 0 */
11192 if (const_op == 0)
11193 code = NE;
11194 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11195 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11196 && (unsigned HOST_WIDE_INT) const_op
11197 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11199 const_op = 0;
11200 code = LT;
11202 break;
11204 default:
11205 break;
11208 *pop1 = GEN_INT (const_op);
11209 return code;
11212 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11213 comparison code that will be tested.
11215 The result is a possibly different comparison code to use. *POP0 and
11216 *POP1 may be updated.
11218 It is possible that we might detect that a comparison is either always
11219 true or always false. However, we do not perform general constant
11220 folding in combine, so this knowledge isn't useful. Such tautologies
11221 should have been detected earlier. Hence we ignore all such cases. */
11223 static enum rtx_code
11224 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11226 rtx op0 = *pop0;
11227 rtx op1 = *pop1;
11228 rtx tem, tem1;
11229 int i;
11230 machine_mode mode, tmode;
11232 /* Try a few ways of applying the same transformation to both operands. */
11233 while (1)
11235 #ifndef WORD_REGISTER_OPERATIONS
11236 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11237 so check specially. */
11238 if (code != GTU && code != GEU && code != LTU && code != LEU
11239 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11240 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11241 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11242 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11243 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11244 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11245 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11246 && CONST_INT_P (XEXP (op0, 1))
11247 && XEXP (op0, 1) == XEXP (op1, 1)
11248 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11249 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11250 && (INTVAL (XEXP (op0, 1))
11251 == (GET_MODE_PRECISION (GET_MODE (op0))
11252 - (GET_MODE_PRECISION
11253 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11255 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11256 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11258 #endif
11260 /* If both operands are the same constant shift, see if we can ignore the
11261 shift. We can if the shift is a rotate or if the bits shifted out of
11262 this shift are known to be zero for both inputs and if the type of
11263 comparison is compatible with the shift. */
11264 if (GET_CODE (op0) == GET_CODE (op1)
11265 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11266 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11267 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11268 && (code != GT && code != LT && code != GE && code != LE))
11269 || (GET_CODE (op0) == ASHIFTRT
11270 && (code != GTU && code != LTU
11271 && code != GEU && code != LEU)))
11272 && CONST_INT_P (XEXP (op0, 1))
11273 && INTVAL (XEXP (op0, 1)) >= 0
11274 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11275 && XEXP (op0, 1) == XEXP (op1, 1))
11277 machine_mode mode = GET_MODE (op0);
11278 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11279 int shift_count = INTVAL (XEXP (op0, 1));
11281 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11282 mask &= (mask >> shift_count) << shift_count;
11283 else if (GET_CODE (op0) == ASHIFT)
11284 mask = (mask & (mask << shift_count)) >> shift_count;
11286 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11287 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11288 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11289 else
11290 break;
11293 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11294 SUBREGs are of the same mode, and, in both cases, the AND would
11295 be redundant if the comparison was done in the narrower mode,
11296 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11297 and the operand's possibly nonzero bits are 0xffffff01; in that case
11298 if we only care about QImode, we don't need the AND). This case
11299 occurs if the output mode of an scc insn is not SImode and
11300 STORE_FLAG_VALUE == 1 (e.g., the 386).
11302 Similarly, check for a case where the AND's are ZERO_EXTEND
11303 operations from some narrower mode even though a SUBREG is not
11304 present. */
11306 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11307 && CONST_INT_P (XEXP (op0, 1))
11308 && CONST_INT_P (XEXP (op1, 1)))
11310 rtx inner_op0 = XEXP (op0, 0);
11311 rtx inner_op1 = XEXP (op1, 0);
11312 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11313 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11314 int changed = 0;
11316 if (paradoxical_subreg_p (inner_op0)
11317 && GET_CODE (inner_op1) == SUBREG
11318 && (GET_MODE (SUBREG_REG (inner_op0))
11319 == GET_MODE (SUBREG_REG (inner_op1)))
11320 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11321 <= HOST_BITS_PER_WIDE_INT)
11322 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11323 GET_MODE (SUBREG_REG (inner_op0)))))
11324 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11325 GET_MODE (SUBREG_REG (inner_op1))))))
11327 op0 = SUBREG_REG (inner_op0);
11328 op1 = SUBREG_REG (inner_op1);
11330 /* The resulting comparison is always unsigned since we masked
11331 off the original sign bit. */
11332 code = unsigned_condition (code);
11334 changed = 1;
11337 else if (c0 == c1)
11338 for (tmode = GET_CLASS_NARROWEST_MODE
11339 (GET_MODE_CLASS (GET_MODE (op0)));
11340 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11341 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11343 op0 = gen_lowpart (tmode, inner_op0);
11344 op1 = gen_lowpart (tmode, inner_op1);
11345 code = unsigned_condition (code);
11346 changed = 1;
11347 break;
11350 if (! changed)
11351 break;
11354 /* If both operands are NOT, we can strip off the outer operation
11355 and adjust the comparison code for swapped operands; similarly for
11356 NEG, except that this must be an equality comparison. */
11357 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11358 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11359 && (code == EQ || code == NE)))
11360 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11362 else
11363 break;
11366 /* If the first operand is a constant, swap the operands and adjust the
11367 comparison code appropriately, but don't do this if the second operand
11368 is already a constant integer. */
11369 if (swap_commutative_operands_p (op0, op1))
11371 tem = op0, op0 = op1, op1 = tem;
11372 code = swap_condition (code);
11375 /* We now enter a loop during which we will try to simplify the comparison.
11376 For the most part, we only are concerned with comparisons with zero,
11377 but some things may really be comparisons with zero but not start
11378 out looking that way. */
11380 while (CONST_INT_P (op1))
11382 machine_mode mode = GET_MODE (op0);
11383 unsigned int mode_width = GET_MODE_PRECISION (mode);
11384 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11385 int equality_comparison_p;
11386 int sign_bit_comparison_p;
11387 int unsigned_comparison_p;
11388 HOST_WIDE_INT const_op;
11390 /* We only want to handle integral modes. This catches VOIDmode,
11391 CCmode, and the floating-point modes. An exception is that we
11392 can handle VOIDmode if OP0 is a COMPARE or a comparison
11393 operation. */
11395 if (GET_MODE_CLASS (mode) != MODE_INT
11396 && ! (mode == VOIDmode
11397 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11398 break;
11400 /* Try to simplify the compare to constant, possibly changing the
11401 comparison op, and/or changing op1 to zero. */
11402 code = simplify_compare_const (code, mode, op0, &op1);
11403 const_op = INTVAL (op1);
11405 /* Compute some predicates to simplify code below. */
11407 equality_comparison_p = (code == EQ || code == NE);
11408 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11409 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11410 || code == GEU);
11412 /* If this is a sign bit comparison and we can do arithmetic in
11413 MODE, say that we will only be needing the sign bit of OP0. */
11414 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11415 op0 = force_to_mode (op0, mode,
11416 (unsigned HOST_WIDE_INT) 1
11417 << (GET_MODE_PRECISION (mode) - 1),
11420 /* Now try cases based on the opcode of OP0. If none of the cases
11421 does a "continue", we exit this loop immediately after the
11422 switch. */
11424 switch (GET_CODE (op0))
11426 case ZERO_EXTRACT:
11427 /* If we are extracting a single bit from a variable position in
11428 a constant that has only a single bit set and are comparing it
11429 with zero, we can convert this into an equality comparison
11430 between the position and the location of the single bit. */
11431 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11432 have already reduced the shift count modulo the word size. */
11433 if (!SHIFT_COUNT_TRUNCATED
11434 && CONST_INT_P (XEXP (op0, 0))
11435 && XEXP (op0, 1) == const1_rtx
11436 && equality_comparison_p && const_op == 0
11437 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11439 if (BITS_BIG_ENDIAN)
11440 i = BITS_PER_WORD - 1 - i;
11442 op0 = XEXP (op0, 2);
11443 op1 = GEN_INT (i);
11444 const_op = i;
11446 /* Result is nonzero iff shift count is equal to I. */
11447 code = reverse_condition (code);
11448 continue;
11451 /* ... fall through ... */
11453 case SIGN_EXTRACT:
11454 tem = expand_compound_operation (op0);
11455 if (tem != op0)
11457 op0 = tem;
11458 continue;
11460 break;
11462 case NOT:
11463 /* If testing for equality, we can take the NOT of the constant. */
11464 if (equality_comparison_p
11465 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11467 op0 = XEXP (op0, 0);
11468 op1 = tem;
11469 continue;
11472 /* If just looking at the sign bit, reverse the sense of the
11473 comparison. */
11474 if (sign_bit_comparison_p)
11476 op0 = XEXP (op0, 0);
11477 code = (code == GE ? LT : GE);
11478 continue;
11480 break;
11482 case NEG:
11483 /* If testing for equality, we can take the NEG of the constant. */
11484 if (equality_comparison_p
11485 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11487 op0 = XEXP (op0, 0);
11488 op1 = tem;
11489 continue;
11492 /* The remaining cases only apply to comparisons with zero. */
11493 if (const_op != 0)
11494 break;
11496 /* When X is ABS or is known positive,
11497 (neg X) is < 0 if and only if X != 0. */
11499 if (sign_bit_comparison_p
11500 && (GET_CODE (XEXP (op0, 0)) == ABS
11501 || (mode_width <= HOST_BITS_PER_WIDE_INT
11502 && (nonzero_bits (XEXP (op0, 0), mode)
11503 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11504 == 0)))
11506 op0 = XEXP (op0, 0);
11507 code = (code == LT ? NE : EQ);
11508 continue;
11511 /* If we have NEG of something whose two high-order bits are the
11512 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11513 if (num_sign_bit_copies (op0, mode) >= 2)
11515 op0 = XEXP (op0, 0);
11516 code = swap_condition (code);
11517 continue;
11519 break;
11521 case ROTATE:
11522 /* If we are testing equality and our count is a constant, we
11523 can perform the inverse operation on our RHS. */
11524 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11525 && (tem = simplify_binary_operation (ROTATERT, mode,
11526 op1, XEXP (op0, 1))) != 0)
11528 op0 = XEXP (op0, 0);
11529 op1 = tem;
11530 continue;
11533 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11534 a particular bit. Convert it to an AND of a constant of that
11535 bit. This will be converted into a ZERO_EXTRACT. */
11536 if (const_op == 0 && sign_bit_comparison_p
11537 && CONST_INT_P (XEXP (op0, 1))
11538 && mode_width <= HOST_BITS_PER_WIDE_INT)
11540 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11541 ((unsigned HOST_WIDE_INT) 1
11542 << (mode_width - 1
11543 - INTVAL (XEXP (op0, 1)))));
11544 code = (code == LT ? NE : EQ);
11545 continue;
11548 /* Fall through. */
11550 case ABS:
11551 /* ABS is ignorable inside an equality comparison with zero. */
11552 if (const_op == 0 && equality_comparison_p)
11554 op0 = XEXP (op0, 0);
11555 continue;
11557 break;
11559 case SIGN_EXTEND:
11560 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11561 (compare FOO CONST) if CONST fits in FOO's mode and we
11562 are either testing inequality or have an unsigned
11563 comparison with ZERO_EXTEND or a signed comparison with
11564 SIGN_EXTEND. But don't do it if we don't have a compare
11565 insn of the given mode, since we'd have to revert it
11566 later on, and then we wouldn't know whether to sign- or
11567 zero-extend. */
11568 mode = GET_MODE (XEXP (op0, 0));
11569 if (GET_MODE_CLASS (mode) == MODE_INT
11570 && ! unsigned_comparison_p
11571 && HWI_COMPUTABLE_MODE_P (mode)
11572 && trunc_int_for_mode (const_op, mode) == const_op
11573 && have_insn_for (COMPARE, mode))
11575 op0 = XEXP (op0, 0);
11576 continue;
11578 break;
11580 case SUBREG:
11581 /* Check for the case where we are comparing A - C1 with C2, that is
11583 (subreg:MODE (plus (A) (-C1))) op (C2)
11585 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11586 comparison in the wider mode. One of the following two conditions
11587 must be true in order for this to be valid:
11589 1. The mode extension results in the same bit pattern being added
11590 on both sides and the comparison is equality or unsigned. As
11591 C2 has been truncated to fit in MODE, the pattern can only be
11592 all 0s or all 1s.
11594 2. The mode extension results in the sign bit being copied on
11595 each side.
11597 The difficulty here is that we have predicates for A but not for
11598 (A - C1) so we need to check that C1 is within proper bounds so
11599 as to perturbate A as little as possible. */
11601 if (mode_width <= HOST_BITS_PER_WIDE_INT
11602 && subreg_lowpart_p (op0)
11603 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11604 && GET_CODE (SUBREG_REG (op0)) == PLUS
11605 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11607 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11608 rtx a = XEXP (SUBREG_REG (op0), 0);
11609 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11611 if ((c1 > 0
11612 && (unsigned HOST_WIDE_INT) c1
11613 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11614 && (equality_comparison_p || unsigned_comparison_p)
11615 /* (A - C1) zero-extends if it is positive and sign-extends
11616 if it is negative, C2 both zero- and sign-extends. */
11617 && ((0 == (nonzero_bits (a, inner_mode)
11618 & ~GET_MODE_MASK (mode))
11619 && const_op >= 0)
11620 /* (A - C1) sign-extends if it is positive and 1-extends
11621 if it is negative, C2 both sign- and 1-extends. */
11622 || (num_sign_bit_copies (a, inner_mode)
11623 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11624 - mode_width)
11625 && const_op < 0)))
11626 || ((unsigned HOST_WIDE_INT) c1
11627 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11628 /* (A - C1) always sign-extends, like C2. */
11629 && num_sign_bit_copies (a, inner_mode)
11630 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11631 - (mode_width - 1))))
11633 op0 = SUBREG_REG (op0);
11634 continue;
11638 /* If the inner mode is narrower and we are extracting the low part,
11639 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11640 if (subreg_lowpart_p (op0)
11641 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11642 /* Fall through */ ;
11643 else
11644 break;
11646 /* ... fall through ... */
11648 case ZERO_EXTEND:
11649 mode = GET_MODE (XEXP (op0, 0));
11650 if (GET_MODE_CLASS (mode) == MODE_INT
11651 && (unsigned_comparison_p || equality_comparison_p)
11652 && HWI_COMPUTABLE_MODE_P (mode)
11653 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11654 && const_op >= 0
11655 && have_insn_for (COMPARE, mode))
11657 op0 = XEXP (op0, 0);
11658 continue;
11660 break;
11662 case PLUS:
11663 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11664 this for equality comparisons due to pathological cases involving
11665 overflows. */
11666 if (equality_comparison_p
11667 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11668 op1, XEXP (op0, 1))))
11670 op0 = XEXP (op0, 0);
11671 op1 = tem;
11672 continue;
11675 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11676 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11677 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11679 op0 = XEXP (XEXP (op0, 0), 0);
11680 code = (code == LT ? EQ : NE);
11681 continue;
11683 break;
11685 case MINUS:
11686 /* We used to optimize signed comparisons against zero, but that
11687 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11688 arrive here as equality comparisons, or (GEU, LTU) are
11689 optimized away. No need to special-case them. */
11691 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11692 (eq B (minus A C)), whichever simplifies. We can only do
11693 this for equality comparisons due to pathological cases involving
11694 overflows. */
11695 if (equality_comparison_p
11696 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11697 XEXP (op0, 1), op1)))
11699 op0 = XEXP (op0, 0);
11700 op1 = tem;
11701 continue;
11704 if (equality_comparison_p
11705 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11706 XEXP (op0, 0), op1)))
11708 op0 = XEXP (op0, 1);
11709 op1 = tem;
11710 continue;
11713 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11714 of bits in X minus 1, is one iff X > 0. */
11715 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11716 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11717 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11718 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11720 op0 = XEXP (op0, 1);
11721 code = (code == GE ? LE : GT);
11722 continue;
11724 break;
11726 case XOR:
11727 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11728 if C is zero or B is a constant. */
11729 if (equality_comparison_p
11730 && 0 != (tem = simplify_binary_operation (XOR, mode,
11731 XEXP (op0, 1), op1)))
11733 op0 = XEXP (op0, 0);
11734 op1 = tem;
11735 continue;
11737 break;
11739 case EQ: case NE:
11740 case UNEQ: case LTGT:
11741 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11742 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11743 case UNORDERED: case ORDERED:
11744 /* We can't do anything if OP0 is a condition code value, rather
11745 than an actual data value. */
11746 if (const_op != 0
11747 || CC0_P (XEXP (op0, 0))
11748 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11749 break;
11751 /* Get the two operands being compared. */
11752 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11753 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11754 else
11755 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11757 /* Check for the cases where we simply want the result of the
11758 earlier test or the opposite of that result. */
11759 if (code == NE || code == EQ
11760 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11761 && (code == LT || code == GE)))
11763 enum rtx_code new_code;
11764 if (code == LT || code == NE)
11765 new_code = GET_CODE (op0);
11766 else
11767 new_code = reversed_comparison_code (op0, NULL);
11769 if (new_code != UNKNOWN)
11771 code = new_code;
11772 op0 = tem;
11773 op1 = tem1;
11774 continue;
11777 break;
11779 case IOR:
11780 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11781 iff X <= 0. */
11782 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11783 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11784 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11786 op0 = XEXP (op0, 1);
11787 code = (code == GE ? GT : LE);
11788 continue;
11790 break;
11792 case AND:
11793 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11794 will be converted to a ZERO_EXTRACT later. */
11795 if (const_op == 0 && equality_comparison_p
11796 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11797 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11799 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11800 XEXP (XEXP (op0, 0), 1));
11801 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11802 continue;
11805 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11806 zero and X is a comparison and C1 and C2 describe only bits set
11807 in STORE_FLAG_VALUE, we can compare with X. */
11808 if (const_op == 0 && equality_comparison_p
11809 && mode_width <= HOST_BITS_PER_WIDE_INT
11810 && CONST_INT_P (XEXP (op0, 1))
11811 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11812 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11813 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11814 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11816 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11817 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11818 if ((~STORE_FLAG_VALUE & mask) == 0
11819 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11820 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11821 && COMPARISON_P (tem))))
11823 op0 = XEXP (XEXP (op0, 0), 0);
11824 continue;
11828 /* If we are doing an equality comparison of an AND of a bit equal
11829 to the sign bit, replace this with a LT or GE comparison of
11830 the underlying value. */
11831 if (equality_comparison_p
11832 && const_op == 0
11833 && CONST_INT_P (XEXP (op0, 1))
11834 && mode_width <= HOST_BITS_PER_WIDE_INT
11835 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11836 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11838 op0 = XEXP (op0, 0);
11839 code = (code == EQ ? GE : LT);
11840 continue;
11843 /* If this AND operation is really a ZERO_EXTEND from a narrower
11844 mode, the constant fits within that mode, and this is either an
11845 equality or unsigned comparison, try to do this comparison in
11846 the narrower mode.
11848 Note that in:
11850 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11851 -> (ne:DI (reg:SI 4) (const_int 0))
11853 unless TRULY_NOOP_TRUNCATION allows it or the register is
11854 known to hold a value of the required mode the
11855 transformation is invalid. */
11856 if ((equality_comparison_p || unsigned_comparison_p)
11857 && CONST_INT_P (XEXP (op0, 1))
11858 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11859 & GET_MODE_MASK (mode))
11860 + 1)) >= 0
11861 && const_op >> i == 0
11862 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11863 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11864 || (REG_P (XEXP (op0, 0))
11865 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11867 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11868 continue;
11871 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11872 fits in both M1 and M2 and the SUBREG is either paradoxical
11873 or represents the low part, permute the SUBREG and the AND
11874 and try again. */
11875 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11877 unsigned HOST_WIDE_INT c1;
11878 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11879 /* Require an integral mode, to avoid creating something like
11880 (AND:SF ...). */
11881 if (SCALAR_INT_MODE_P (tmode)
11882 /* It is unsafe to commute the AND into the SUBREG if the
11883 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11884 not defined. As originally written the upper bits
11885 have a defined value due to the AND operation.
11886 However, if we commute the AND inside the SUBREG then
11887 they no longer have defined values and the meaning of
11888 the code has been changed. */
11889 && (0
11890 #ifdef WORD_REGISTER_OPERATIONS
11891 || (mode_width > GET_MODE_PRECISION (tmode)
11892 && mode_width <= BITS_PER_WORD)
11893 #endif
11894 || (mode_width <= GET_MODE_PRECISION (tmode)
11895 && subreg_lowpart_p (XEXP (op0, 0))))
11896 && CONST_INT_P (XEXP (op0, 1))
11897 && mode_width <= HOST_BITS_PER_WIDE_INT
11898 && HWI_COMPUTABLE_MODE_P (tmode)
11899 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11900 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11901 && c1 != mask
11902 && c1 != GET_MODE_MASK (tmode))
11904 op0 = simplify_gen_binary (AND, tmode,
11905 SUBREG_REG (XEXP (op0, 0)),
11906 gen_int_mode (c1, tmode));
11907 op0 = gen_lowpart (mode, op0);
11908 continue;
11912 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11913 if (const_op == 0 && equality_comparison_p
11914 && XEXP (op0, 1) == const1_rtx
11915 && GET_CODE (XEXP (op0, 0)) == NOT)
11917 op0 = simplify_and_const_int (NULL_RTX, mode,
11918 XEXP (XEXP (op0, 0), 0), 1);
11919 code = (code == NE ? EQ : NE);
11920 continue;
11923 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11924 (eq (and (lshiftrt X) 1) 0).
11925 Also handle the case where (not X) is expressed using xor. */
11926 if (const_op == 0 && equality_comparison_p
11927 && XEXP (op0, 1) == const1_rtx
11928 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11930 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11931 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11933 if (GET_CODE (shift_op) == NOT
11934 || (GET_CODE (shift_op) == XOR
11935 && CONST_INT_P (XEXP (shift_op, 1))
11936 && CONST_INT_P (shift_count)
11937 && HWI_COMPUTABLE_MODE_P (mode)
11938 && (UINTVAL (XEXP (shift_op, 1))
11939 == (unsigned HOST_WIDE_INT) 1
11940 << INTVAL (shift_count))))
11943 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11944 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11945 code = (code == NE ? EQ : NE);
11946 continue;
11949 break;
11951 case ASHIFT:
11952 /* If we have (compare (ashift FOO N) (const_int C)) and
11953 the high order N bits of FOO (N+1 if an inequality comparison)
11954 are known to be zero, we can do this by comparing FOO with C
11955 shifted right N bits so long as the low-order N bits of C are
11956 zero. */
11957 if (CONST_INT_P (XEXP (op0, 1))
11958 && INTVAL (XEXP (op0, 1)) >= 0
11959 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11960 < HOST_BITS_PER_WIDE_INT)
11961 && (((unsigned HOST_WIDE_INT) const_op
11962 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11963 - 1)) == 0)
11964 && mode_width <= HOST_BITS_PER_WIDE_INT
11965 && (nonzero_bits (XEXP (op0, 0), mode)
11966 & ~(mask >> (INTVAL (XEXP (op0, 1))
11967 + ! equality_comparison_p))) == 0)
11969 /* We must perform a logical shift, not an arithmetic one,
11970 as we want the top N bits of C to be zero. */
11971 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11973 temp >>= INTVAL (XEXP (op0, 1));
11974 op1 = gen_int_mode (temp, mode);
11975 op0 = XEXP (op0, 0);
11976 continue;
11979 /* If we are doing a sign bit comparison, it means we are testing
11980 a particular bit. Convert it to the appropriate AND. */
11981 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11982 && mode_width <= HOST_BITS_PER_WIDE_INT)
11984 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11985 ((unsigned HOST_WIDE_INT) 1
11986 << (mode_width - 1
11987 - INTVAL (XEXP (op0, 1)))));
11988 code = (code == LT ? NE : EQ);
11989 continue;
11992 /* If this an equality comparison with zero and we are shifting
11993 the low bit to the sign bit, we can convert this to an AND of the
11994 low-order bit. */
11995 if (const_op == 0 && equality_comparison_p
11996 && CONST_INT_P (XEXP (op0, 1))
11997 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11999 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12000 continue;
12002 break;
12004 case ASHIFTRT:
12005 /* If this is an equality comparison with zero, we can do this
12006 as a logical shift, which might be much simpler. */
12007 if (equality_comparison_p && const_op == 0
12008 && CONST_INT_P (XEXP (op0, 1)))
12010 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12011 XEXP (op0, 0),
12012 INTVAL (XEXP (op0, 1)));
12013 continue;
12016 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12017 do the comparison in a narrower mode. */
12018 if (! unsigned_comparison_p
12019 && CONST_INT_P (XEXP (op0, 1))
12020 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12021 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12022 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12023 MODE_INT, 1)) != BLKmode
12024 && (((unsigned HOST_WIDE_INT) const_op
12025 + (GET_MODE_MASK (tmode) >> 1) + 1)
12026 <= GET_MODE_MASK (tmode)))
12028 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12029 continue;
12032 /* Likewise if OP0 is a PLUS of a sign extension with a
12033 constant, which is usually represented with the PLUS
12034 between the shifts. */
12035 if (! unsigned_comparison_p
12036 && CONST_INT_P (XEXP (op0, 1))
12037 && GET_CODE (XEXP (op0, 0)) == PLUS
12038 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12039 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12040 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12041 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12042 MODE_INT, 1)) != BLKmode
12043 && (((unsigned HOST_WIDE_INT) const_op
12044 + (GET_MODE_MASK (tmode) >> 1) + 1)
12045 <= GET_MODE_MASK (tmode)))
12047 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12048 rtx add_const = XEXP (XEXP (op0, 0), 1);
12049 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12050 add_const, XEXP (op0, 1));
12052 op0 = simplify_gen_binary (PLUS, tmode,
12053 gen_lowpart (tmode, inner),
12054 new_const);
12055 continue;
12058 /* ... fall through ... */
12059 case LSHIFTRT:
12060 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12061 the low order N bits of FOO are known to be zero, we can do this
12062 by comparing FOO with C shifted left N bits so long as no
12063 overflow occurs. Even if the low order N bits of FOO aren't known
12064 to be zero, if the comparison is >= or < we can use the same
12065 optimization and for > or <= by setting all the low
12066 order N bits in the comparison constant. */
12067 if (CONST_INT_P (XEXP (op0, 1))
12068 && INTVAL (XEXP (op0, 1)) > 0
12069 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12070 && mode_width <= HOST_BITS_PER_WIDE_INT
12071 && (((unsigned HOST_WIDE_INT) const_op
12072 + (GET_CODE (op0) != LSHIFTRT
12073 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12074 + 1)
12075 : 0))
12076 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12078 unsigned HOST_WIDE_INT low_bits
12079 = (nonzero_bits (XEXP (op0, 0), mode)
12080 & (((unsigned HOST_WIDE_INT) 1
12081 << INTVAL (XEXP (op0, 1))) - 1));
12082 if (low_bits == 0 || !equality_comparison_p)
12084 /* If the shift was logical, then we must make the condition
12085 unsigned. */
12086 if (GET_CODE (op0) == LSHIFTRT)
12087 code = unsigned_condition (code);
12089 const_op <<= INTVAL (XEXP (op0, 1));
12090 if (low_bits != 0
12091 && (code == GT || code == GTU
12092 || code == LE || code == LEU))
12093 const_op
12094 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12095 op1 = GEN_INT (const_op);
12096 op0 = XEXP (op0, 0);
12097 continue;
12101 /* If we are using this shift to extract just the sign bit, we
12102 can replace this with an LT or GE comparison. */
12103 if (const_op == 0
12104 && (equality_comparison_p || sign_bit_comparison_p)
12105 && CONST_INT_P (XEXP (op0, 1))
12106 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12108 op0 = XEXP (op0, 0);
12109 code = (code == NE || code == GT ? LT : GE);
12110 continue;
12112 break;
12114 default:
12115 break;
12118 break;
12121 /* Now make any compound operations involved in this comparison. Then,
12122 check for an outmost SUBREG on OP0 that is not doing anything or is
12123 paradoxical. The latter transformation must only be performed when
12124 it is known that the "extra" bits will be the same in op0 and op1 or
12125 that they don't matter. There are three cases to consider:
12127 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12128 care bits and we can assume they have any convenient value. So
12129 making the transformation is safe.
12131 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12132 In this case the upper bits of op0 are undefined. We should not make
12133 the simplification in that case as we do not know the contents of
12134 those bits.
12136 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12137 UNKNOWN. In that case we know those bits are zeros or ones. We must
12138 also be sure that they are the same as the upper bits of op1.
12140 We can never remove a SUBREG for a non-equality comparison because
12141 the sign bit is in a different place in the underlying object. */
12143 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12144 op1 = make_compound_operation (op1, SET);
12146 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12147 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12148 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12149 && (code == NE || code == EQ))
12151 if (paradoxical_subreg_p (op0))
12153 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12154 implemented. */
12155 if (REG_P (SUBREG_REG (op0)))
12157 op0 = SUBREG_REG (op0);
12158 op1 = gen_lowpart (GET_MODE (op0), op1);
12161 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12162 <= HOST_BITS_PER_WIDE_INT)
12163 && (nonzero_bits (SUBREG_REG (op0),
12164 GET_MODE (SUBREG_REG (op0)))
12165 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12167 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12169 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12170 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12171 op0 = SUBREG_REG (op0), op1 = tem;
12175 /* We now do the opposite procedure: Some machines don't have compare
12176 insns in all modes. If OP0's mode is an integer mode smaller than a
12177 word and we can't do a compare in that mode, see if there is a larger
12178 mode for which we can do the compare. There are a number of cases in
12179 which we can use the wider mode. */
12181 mode = GET_MODE (op0);
12182 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12183 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12184 && ! have_insn_for (COMPARE, mode))
12185 for (tmode = GET_MODE_WIDER_MODE (mode);
12186 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12187 tmode = GET_MODE_WIDER_MODE (tmode))
12188 if (have_insn_for (COMPARE, tmode))
12190 int zero_extended;
12192 /* If this is a test for negative, we can make an explicit
12193 test of the sign bit. Test this first so we can use
12194 a paradoxical subreg to extend OP0. */
12196 if (op1 == const0_rtx && (code == LT || code == GE)
12197 && HWI_COMPUTABLE_MODE_P (mode))
12199 unsigned HOST_WIDE_INT sign
12200 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12201 op0 = simplify_gen_binary (AND, tmode,
12202 gen_lowpart (tmode, op0),
12203 gen_int_mode (sign, tmode));
12204 code = (code == LT) ? NE : EQ;
12205 break;
12208 /* If the only nonzero bits in OP0 and OP1 are those in the
12209 narrower mode and this is an equality or unsigned comparison,
12210 we can use the wider mode. Similarly for sign-extended
12211 values, in which case it is true for all comparisons. */
12212 zero_extended = ((code == EQ || code == NE
12213 || code == GEU || code == GTU
12214 || code == LEU || code == LTU)
12215 && (nonzero_bits (op0, tmode)
12216 & ~GET_MODE_MASK (mode)) == 0
12217 && ((CONST_INT_P (op1)
12218 || (nonzero_bits (op1, tmode)
12219 & ~GET_MODE_MASK (mode)) == 0)));
12221 if (zero_extended
12222 || ((num_sign_bit_copies (op0, tmode)
12223 > (unsigned int) (GET_MODE_PRECISION (tmode)
12224 - GET_MODE_PRECISION (mode)))
12225 && (num_sign_bit_copies (op1, tmode)
12226 > (unsigned int) (GET_MODE_PRECISION (tmode)
12227 - GET_MODE_PRECISION (mode)))))
12229 /* If OP0 is an AND and we don't have an AND in MODE either,
12230 make a new AND in the proper mode. */
12231 if (GET_CODE (op0) == AND
12232 && !have_insn_for (AND, mode))
12233 op0 = simplify_gen_binary (AND, tmode,
12234 gen_lowpart (tmode,
12235 XEXP (op0, 0)),
12236 gen_lowpart (tmode,
12237 XEXP (op0, 1)));
12238 else
12240 if (zero_extended)
12242 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12243 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12245 else
12247 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12248 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12250 break;
12255 /* We may have changed the comparison operands. Re-canonicalize. */
12256 if (swap_commutative_operands_p (op0, op1))
12258 tem = op0, op0 = op1, op1 = tem;
12259 code = swap_condition (code);
12262 /* If this machine only supports a subset of valid comparisons, see if we
12263 can convert an unsupported one into a supported one. */
12264 target_canonicalize_comparison (&code, &op0, &op1, 0);
12266 *pop0 = op0;
12267 *pop1 = op1;
12269 return code;
12272 /* Utility function for record_value_for_reg. Count number of
12273 rtxs in X. */
12274 static int
12275 count_rtxs (rtx x)
12277 enum rtx_code code = GET_CODE (x);
12278 const char *fmt;
12279 int i, j, ret = 1;
12281 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12282 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12284 rtx x0 = XEXP (x, 0);
12285 rtx x1 = XEXP (x, 1);
12287 if (x0 == x1)
12288 return 1 + 2 * count_rtxs (x0);
12290 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12291 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12292 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12293 return 2 + 2 * count_rtxs (x0)
12294 + count_rtxs (x == XEXP (x1, 0)
12295 ? XEXP (x1, 1) : XEXP (x1, 0));
12297 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12298 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12299 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12300 return 2 + 2 * count_rtxs (x1)
12301 + count_rtxs (x == XEXP (x0, 0)
12302 ? XEXP (x0, 1) : XEXP (x0, 0));
12305 fmt = GET_RTX_FORMAT (code);
12306 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12307 if (fmt[i] == 'e')
12308 ret += count_rtxs (XEXP (x, i));
12309 else if (fmt[i] == 'E')
12310 for (j = 0; j < XVECLEN (x, i); j++)
12311 ret += count_rtxs (XVECEXP (x, i, j));
12313 return ret;
12316 /* Utility function for following routine. Called when X is part of a value
12317 being stored into last_set_value. Sets last_set_table_tick
12318 for each register mentioned. Similar to mention_regs in cse.c */
12320 static void
12321 update_table_tick (rtx x)
12323 enum rtx_code code = GET_CODE (x);
12324 const char *fmt = GET_RTX_FORMAT (code);
12325 int i, j;
12327 if (code == REG)
12329 unsigned int regno = REGNO (x);
12330 unsigned int endregno = END_REGNO (x);
12331 unsigned int r;
12333 for (r = regno; r < endregno; r++)
12335 reg_stat_type *rsp = &reg_stat[r];
12336 rsp->last_set_table_tick = label_tick;
12339 return;
12342 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12343 if (fmt[i] == 'e')
12345 /* Check for identical subexpressions. If x contains
12346 identical subexpression we only have to traverse one of
12347 them. */
12348 if (i == 0 && ARITHMETIC_P (x))
12350 /* Note that at this point x1 has already been
12351 processed. */
12352 rtx x0 = XEXP (x, 0);
12353 rtx x1 = XEXP (x, 1);
12355 /* If x0 and x1 are identical then there is no need to
12356 process x0. */
12357 if (x0 == x1)
12358 break;
12360 /* If x0 is identical to a subexpression of x1 then while
12361 processing x1, x0 has already been processed. Thus we
12362 are done with x. */
12363 if (ARITHMETIC_P (x1)
12364 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12365 break;
12367 /* If x1 is identical to a subexpression of x0 then we
12368 still have to process the rest of x0. */
12369 if (ARITHMETIC_P (x0)
12370 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12372 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12373 break;
12377 update_table_tick (XEXP (x, i));
12379 else if (fmt[i] == 'E')
12380 for (j = 0; j < XVECLEN (x, i); j++)
12381 update_table_tick (XVECEXP (x, i, j));
12384 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12385 are saying that the register is clobbered and we no longer know its
12386 value. If INSN is zero, don't update reg_stat[].last_set; this is
12387 only permitted with VALUE also zero and is used to invalidate the
12388 register. */
12390 static void
12391 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12393 unsigned int regno = REGNO (reg);
12394 unsigned int endregno = END_REGNO (reg);
12395 unsigned int i;
12396 reg_stat_type *rsp;
12398 /* If VALUE contains REG and we have a previous value for REG, substitute
12399 the previous value. */
12400 if (value && insn && reg_overlap_mentioned_p (reg, value))
12402 rtx tem;
12404 /* Set things up so get_last_value is allowed to see anything set up to
12405 our insn. */
12406 subst_low_luid = DF_INSN_LUID (insn);
12407 tem = get_last_value (reg);
12409 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12410 it isn't going to be useful and will take a lot of time to process,
12411 so just use the CLOBBER. */
12413 if (tem)
12415 if (ARITHMETIC_P (tem)
12416 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12417 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12418 tem = XEXP (tem, 0);
12419 else if (count_occurrences (value, reg, 1) >= 2)
12421 /* If there are two or more occurrences of REG in VALUE,
12422 prevent the value from growing too much. */
12423 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12424 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12427 value = replace_rtx (copy_rtx (value), reg, tem);
12431 /* For each register modified, show we don't know its value, that
12432 we don't know about its bitwise content, that its value has been
12433 updated, and that we don't know the location of the death of the
12434 register. */
12435 for (i = regno; i < endregno; i++)
12437 rsp = &reg_stat[i];
12439 if (insn)
12440 rsp->last_set = insn;
12442 rsp->last_set_value = 0;
12443 rsp->last_set_mode = VOIDmode;
12444 rsp->last_set_nonzero_bits = 0;
12445 rsp->last_set_sign_bit_copies = 0;
12446 rsp->last_death = 0;
12447 rsp->truncated_to_mode = VOIDmode;
12450 /* Mark registers that are being referenced in this value. */
12451 if (value)
12452 update_table_tick (value);
12454 /* Now update the status of each register being set.
12455 If someone is using this register in this block, set this register
12456 to invalid since we will get confused between the two lives in this
12457 basic block. This makes using this register always invalid. In cse, we
12458 scan the table to invalidate all entries using this register, but this
12459 is too much work for us. */
12461 for (i = regno; i < endregno; i++)
12463 rsp = &reg_stat[i];
12464 rsp->last_set_label = label_tick;
12465 if (!insn
12466 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12467 rsp->last_set_invalid = 1;
12468 else
12469 rsp->last_set_invalid = 0;
12472 /* The value being assigned might refer to X (like in "x++;"). In that
12473 case, we must replace it with (clobber (const_int 0)) to prevent
12474 infinite loops. */
12475 rsp = &reg_stat[regno];
12476 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12478 value = copy_rtx (value);
12479 if (!get_last_value_validate (&value, insn, label_tick, 1))
12480 value = 0;
12483 /* For the main register being modified, update the value, the mode, the
12484 nonzero bits, and the number of sign bit copies. */
12486 rsp->last_set_value = value;
12488 if (value)
12490 machine_mode mode = GET_MODE (reg);
12491 subst_low_luid = DF_INSN_LUID (insn);
12492 rsp->last_set_mode = mode;
12493 if (GET_MODE_CLASS (mode) == MODE_INT
12494 && HWI_COMPUTABLE_MODE_P (mode))
12495 mode = nonzero_bits_mode;
12496 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12497 rsp->last_set_sign_bit_copies
12498 = num_sign_bit_copies (value, GET_MODE (reg));
12502 /* Called via note_stores from record_dead_and_set_regs to handle one
12503 SET or CLOBBER in an insn. DATA is the instruction in which the
12504 set is occurring. */
12506 static void
12507 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12509 rtx_insn *record_dead_insn = (rtx_insn *) data;
12511 if (GET_CODE (dest) == SUBREG)
12512 dest = SUBREG_REG (dest);
12514 if (!record_dead_insn)
12516 if (REG_P (dest))
12517 record_value_for_reg (dest, NULL, NULL_RTX);
12518 return;
12521 if (REG_P (dest))
12523 /* If we are setting the whole register, we know its value. Otherwise
12524 show that we don't know the value. We can handle SUBREG in
12525 some cases. */
12526 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12527 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12528 else if (GET_CODE (setter) == SET
12529 && GET_CODE (SET_DEST (setter)) == SUBREG
12530 && SUBREG_REG (SET_DEST (setter)) == dest
12531 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12532 && subreg_lowpart_p (SET_DEST (setter)))
12533 record_value_for_reg (dest, record_dead_insn,
12534 gen_lowpart (GET_MODE (dest),
12535 SET_SRC (setter)));
12536 else
12537 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12539 else if (MEM_P (dest)
12540 /* Ignore pushes, they clobber nothing. */
12541 && ! push_operand (dest, GET_MODE (dest)))
12542 mem_last_set = DF_INSN_LUID (record_dead_insn);
12545 /* Update the records of when each REG was most recently set or killed
12546 for the things done by INSN. This is the last thing done in processing
12547 INSN in the combiner loop.
12549 We update reg_stat[], in particular fields last_set, last_set_value,
12550 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12551 last_death, and also the similar information mem_last_set (which insn
12552 most recently modified memory) and last_call_luid (which insn was the
12553 most recent subroutine call). */
12555 static void
12556 record_dead_and_set_regs (rtx_insn *insn)
12558 rtx link;
12559 unsigned int i;
12561 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12563 if (REG_NOTE_KIND (link) == REG_DEAD
12564 && REG_P (XEXP (link, 0)))
12566 unsigned int regno = REGNO (XEXP (link, 0));
12567 unsigned int endregno = END_REGNO (XEXP (link, 0));
12569 for (i = regno; i < endregno; i++)
12571 reg_stat_type *rsp;
12573 rsp = &reg_stat[i];
12574 rsp->last_death = insn;
12577 else if (REG_NOTE_KIND (link) == REG_INC)
12578 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12581 if (CALL_P (insn))
12583 hard_reg_set_iterator hrsi;
12584 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12586 reg_stat_type *rsp;
12588 rsp = &reg_stat[i];
12589 rsp->last_set_invalid = 1;
12590 rsp->last_set = insn;
12591 rsp->last_set_value = 0;
12592 rsp->last_set_mode = VOIDmode;
12593 rsp->last_set_nonzero_bits = 0;
12594 rsp->last_set_sign_bit_copies = 0;
12595 rsp->last_death = 0;
12596 rsp->truncated_to_mode = VOIDmode;
12599 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12601 /* We can't combine into a call pattern. Remember, though, that
12602 the return value register is set at this LUID. We could
12603 still replace a register with the return value from the
12604 wrong subroutine call! */
12605 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12607 else
12608 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12611 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12612 register present in the SUBREG, so for each such SUBREG go back and
12613 adjust nonzero and sign bit information of the registers that are
12614 known to have some zero/sign bits set.
12616 This is needed because when combine blows the SUBREGs away, the
12617 information on zero/sign bits is lost and further combines can be
12618 missed because of that. */
12620 static void
12621 record_promoted_value (rtx_insn *insn, rtx subreg)
12623 struct insn_link *links;
12624 rtx set;
12625 unsigned int regno = REGNO (SUBREG_REG (subreg));
12626 machine_mode mode = GET_MODE (subreg);
12628 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12629 return;
12631 for (links = LOG_LINKS (insn); links;)
12633 reg_stat_type *rsp;
12635 insn = links->insn;
12636 set = single_set (insn);
12638 if (! set || !REG_P (SET_DEST (set))
12639 || REGNO (SET_DEST (set)) != regno
12640 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12642 links = links->next;
12643 continue;
12646 rsp = &reg_stat[regno];
12647 if (rsp->last_set == insn)
12649 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12650 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12653 if (REG_P (SET_SRC (set)))
12655 regno = REGNO (SET_SRC (set));
12656 links = LOG_LINKS (insn);
12658 else
12659 break;
12663 /* Check if X, a register, is known to contain a value already
12664 truncated to MODE. In this case we can use a subreg to refer to
12665 the truncated value even though in the generic case we would need
12666 an explicit truncation. */
12668 static bool
12669 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12671 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12672 machine_mode truncated = rsp->truncated_to_mode;
12674 if (truncated == 0
12675 || rsp->truncation_label < label_tick_ebb_start)
12676 return false;
12677 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12678 return true;
12679 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12680 return true;
12681 return false;
12684 /* If X is a hard reg or a subreg record the mode that the register is
12685 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12686 to turn a truncate into a subreg using this information. Return true
12687 if traversing X is complete. */
12689 static bool
12690 record_truncated_value (rtx x)
12692 machine_mode truncated_mode;
12693 reg_stat_type *rsp;
12695 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12697 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12698 truncated_mode = GET_MODE (x);
12700 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12701 return true;
12703 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12704 return true;
12706 x = SUBREG_REG (x);
12708 /* ??? For hard-regs we now record everything. We might be able to
12709 optimize this using last_set_mode. */
12710 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12711 truncated_mode = GET_MODE (x);
12712 else
12713 return false;
12715 rsp = &reg_stat[REGNO (x)];
12716 if (rsp->truncated_to_mode == 0
12717 || rsp->truncation_label < label_tick_ebb_start
12718 || (GET_MODE_SIZE (truncated_mode)
12719 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12721 rsp->truncated_to_mode = truncated_mode;
12722 rsp->truncation_label = label_tick;
12725 return true;
12728 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12729 the modes they are used in. This can help truning TRUNCATEs into
12730 SUBREGs. */
12732 static void
12733 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12735 subrtx_var_iterator::array_type array;
12736 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12737 if (record_truncated_value (*iter))
12738 iter.skip_subrtxes ();
12741 /* Scan X for promoted SUBREGs. For each one found,
12742 note what it implies to the registers used in it. */
12744 static void
12745 check_promoted_subreg (rtx_insn *insn, rtx x)
12747 if (GET_CODE (x) == SUBREG
12748 && SUBREG_PROMOTED_VAR_P (x)
12749 && REG_P (SUBREG_REG (x)))
12750 record_promoted_value (insn, x);
12751 else
12753 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12754 int i, j;
12756 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12757 switch (format[i])
12759 case 'e':
12760 check_promoted_subreg (insn, XEXP (x, i));
12761 break;
12762 case 'V':
12763 case 'E':
12764 if (XVEC (x, i) != 0)
12765 for (j = 0; j < XVECLEN (x, i); j++)
12766 check_promoted_subreg (insn, XVECEXP (x, i, j));
12767 break;
12772 /* Verify that all the registers and memory references mentioned in *LOC are
12773 still valid. *LOC was part of a value set in INSN when label_tick was
12774 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12775 the invalid references with (clobber (const_int 0)) and return 1. This
12776 replacement is useful because we often can get useful information about
12777 the form of a value (e.g., if it was produced by a shift that always
12778 produces -1 or 0) even though we don't know exactly what registers it
12779 was produced from. */
12781 static int
12782 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12784 rtx x = *loc;
12785 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12786 int len = GET_RTX_LENGTH (GET_CODE (x));
12787 int i, j;
12789 if (REG_P (x))
12791 unsigned int regno = REGNO (x);
12792 unsigned int endregno = END_REGNO (x);
12793 unsigned int j;
12795 for (j = regno; j < endregno; j++)
12797 reg_stat_type *rsp = &reg_stat[j];
12798 if (rsp->last_set_invalid
12799 /* If this is a pseudo-register that was only set once and not
12800 live at the beginning of the function, it is always valid. */
12801 || (! (regno >= FIRST_PSEUDO_REGISTER
12802 && REG_N_SETS (regno) == 1
12803 && (!REGNO_REG_SET_P
12804 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12805 regno)))
12806 && rsp->last_set_label > tick))
12808 if (replace)
12809 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12810 return replace;
12814 return 1;
12816 /* If this is a memory reference, make sure that there were no stores after
12817 it that might have clobbered the value. We don't have alias info, so we
12818 assume any store invalidates it. Moreover, we only have local UIDs, so
12819 we also assume that there were stores in the intervening basic blocks. */
12820 else if (MEM_P (x) && !MEM_READONLY_P (x)
12821 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12823 if (replace)
12824 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12825 return replace;
12828 for (i = 0; i < len; i++)
12830 if (fmt[i] == 'e')
12832 /* Check for identical subexpressions. If x contains
12833 identical subexpression we only have to traverse one of
12834 them. */
12835 if (i == 1 && ARITHMETIC_P (x))
12837 /* Note that at this point x0 has already been checked
12838 and found valid. */
12839 rtx x0 = XEXP (x, 0);
12840 rtx x1 = XEXP (x, 1);
12842 /* If x0 and x1 are identical then x is also valid. */
12843 if (x0 == x1)
12844 return 1;
12846 /* If x1 is identical to a subexpression of x0 then
12847 while checking x0, x1 has already been checked. Thus
12848 it is valid and so as x. */
12849 if (ARITHMETIC_P (x0)
12850 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12851 return 1;
12853 /* If x0 is identical to a subexpression of x1 then x is
12854 valid iff the rest of x1 is valid. */
12855 if (ARITHMETIC_P (x1)
12856 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12857 return
12858 get_last_value_validate (&XEXP (x1,
12859 x0 == XEXP (x1, 0) ? 1 : 0),
12860 insn, tick, replace);
12863 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12864 replace) == 0)
12865 return 0;
12867 else if (fmt[i] == 'E')
12868 for (j = 0; j < XVECLEN (x, i); j++)
12869 if (get_last_value_validate (&XVECEXP (x, i, j),
12870 insn, tick, replace) == 0)
12871 return 0;
12874 /* If we haven't found a reason for it to be invalid, it is valid. */
12875 return 1;
12878 /* Get the last value assigned to X, if known. Some registers
12879 in the value may be replaced with (clobber (const_int 0)) if their value
12880 is known longer known reliably. */
12882 static rtx
12883 get_last_value (const_rtx x)
12885 unsigned int regno;
12886 rtx value;
12887 reg_stat_type *rsp;
12889 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12890 then convert it to the desired mode. If this is a paradoxical SUBREG,
12891 we cannot predict what values the "extra" bits might have. */
12892 if (GET_CODE (x) == SUBREG
12893 && subreg_lowpart_p (x)
12894 && !paradoxical_subreg_p (x)
12895 && (value = get_last_value (SUBREG_REG (x))) != 0)
12896 return gen_lowpart (GET_MODE (x), value);
12898 if (!REG_P (x))
12899 return 0;
12901 regno = REGNO (x);
12902 rsp = &reg_stat[regno];
12903 value = rsp->last_set_value;
12905 /* If we don't have a value, or if it isn't for this basic block and
12906 it's either a hard register, set more than once, or it's a live
12907 at the beginning of the function, return 0.
12909 Because if it's not live at the beginning of the function then the reg
12910 is always set before being used (is never used without being set).
12911 And, if it's set only once, and it's always set before use, then all
12912 uses must have the same last value, even if it's not from this basic
12913 block. */
12915 if (value == 0
12916 || (rsp->last_set_label < label_tick_ebb_start
12917 && (regno < FIRST_PSEUDO_REGISTER
12918 || REG_N_SETS (regno) != 1
12919 || REGNO_REG_SET_P
12920 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12921 return 0;
12923 /* If the value was set in a later insn than the ones we are processing,
12924 we can't use it even if the register was only set once. */
12925 if (rsp->last_set_label == label_tick
12926 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12927 return 0;
12929 /* If the value has all its registers valid, return it. */
12930 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12931 return value;
12933 /* Otherwise, make a copy and replace any invalid register with
12934 (clobber (const_int 0)). If that fails for some reason, return 0. */
12936 value = copy_rtx (value);
12937 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12938 return value;
12940 return 0;
12943 /* Return nonzero if expression X refers to a REG or to memory
12944 that is set in an instruction more recent than FROM_LUID. */
12946 static int
12947 use_crosses_set_p (const_rtx x, int from_luid)
12949 const char *fmt;
12950 int i;
12951 enum rtx_code code = GET_CODE (x);
12953 if (code == REG)
12955 unsigned int regno = REGNO (x);
12956 unsigned endreg = END_REGNO (x);
12958 #ifdef PUSH_ROUNDING
12959 /* Don't allow uses of the stack pointer to be moved,
12960 because we don't know whether the move crosses a push insn. */
12961 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12962 return 1;
12963 #endif
12964 for (; regno < endreg; regno++)
12966 reg_stat_type *rsp = &reg_stat[regno];
12967 if (rsp->last_set
12968 && rsp->last_set_label == label_tick
12969 && DF_INSN_LUID (rsp->last_set) > from_luid)
12970 return 1;
12972 return 0;
12975 if (code == MEM && mem_last_set > from_luid)
12976 return 1;
12978 fmt = GET_RTX_FORMAT (code);
12980 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12982 if (fmt[i] == 'E')
12984 int j;
12985 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12986 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12987 return 1;
12989 else if (fmt[i] == 'e'
12990 && use_crosses_set_p (XEXP (x, i), from_luid))
12991 return 1;
12993 return 0;
12996 /* Define three variables used for communication between the following
12997 routines. */
12999 static unsigned int reg_dead_regno, reg_dead_endregno;
13000 static int reg_dead_flag;
13002 /* Function called via note_stores from reg_dead_at_p.
13004 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13005 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13007 static void
13008 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13010 unsigned int regno, endregno;
13012 if (!REG_P (dest))
13013 return;
13015 regno = REGNO (dest);
13016 endregno = END_REGNO (dest);
13017 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13018 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13021 /* Return nonzero if REG is known to be dead at INSN.
13023 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13024 referencing REG, it is dead. If we hit a SET referencing REG, it is
13025 live. Otherwise, see if it is live or dead at the start of the basic
13026 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13027 must be assumed to be always live. */
13029 static int
13030 reg_dead_at_p (rtx reg, rtx_insn *insn)
13032 basic_block block;
13033 unsigned int i;
13035 /* Set variables for reg_dead_at_p_1. */
13036 reg_dead_regno = REGNO (reg);
13037 reg_dead_endregno = END_REGNO (reg);
13039 reg_dead_flag = 0;
13041 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13042 we allow the machine description to decide whether use-and-clobber
13043 patterns are OK. */
13044 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13046 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13047 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13048 return 0;
13051 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13052 beginning of basic block. */
13053 block = BLOCK_FOR_INSN (insn);
13054 for (;;)
13056 if (INSN_P (insn))
13058 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13059 return 1;
13061 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13062 if (reg_dead_flag)
13063 return reg_dead_flag == 1 ? 1 : 0;
13065 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13066 return 1;
13069 if (insn == BB_HEAD (block))
13070 break;
13072 insn = PREV_INSN (insn);
13075 /* Look at live-in sets for the basic block that we were in. */
13076 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13077 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13078 return 0;
13080 return 1;
13083 /* Note hard registers in X that are used. */
13085 static void
13086 mark_used_regs_combine (rtx x)
13088 RTX_CODE code = GET_CODE (x);
13089 unsigned int regno;
13090 int i;
13092 switch (code)
13094 case LABEL_REF:
13095 case SYMBOL_REF:
13096 case CONST:
13097 CASE_CONST_ANY:
13098 case PC:
13099 case ADDR_VEC:
13100 case ADDR_DIFF_VEC:
13101 case ASM_INPUT:
13102 #ifdef HAVE_cc0
13103 /* CC0 must die in the insn after it is set, so we don't need to take
13104 special note of it here. */
13105 case CC0:
13106 #endif
13107 return;
13109 case CLOBBER:
13110 /* If we are clobbering a MEM, mark any hard registers inside the
13111 address as used. */
13112 if (MEM_P (XEXP (x, 0)))
13113 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13114 return;
13116 case REG:
13117 regno = REGNO (x);
13118 /* A hard reg in a wide mode may really be multiple registers.
13119 If so, mark all of them just like the first. */
13120 if (regno < FIRST_PSEUDO_REGISTER)
13122 /* None of this applies to the stack, frame or arg pointers. */
13123 if (regno == STACK_POINTER_REGNUM
13124 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13125 || regno == HARD_FRAME_POINTER_REGNUM
13126 #endif
13127 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13128 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13129 #endif
13130 || regno == FRAME_POINTER_REGNUM)
13131 return;
13133 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13135 return;
13137 case SET:
13139 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13140 the address. */
13141 rtx testreg = SET_DEST (x);
13143 while (GET_CODE (testreg) == SUBREG
13144 || GET_CODE (testreg) == ZERO_EXTRACT
13145 || GET_CODE (testreg) == STRICT_LOW_PART)
13146 testreg = XEXP (testreg, 0);
13148 if (MEM_P (testreg))
13149 mark_used_regs_combine (XEXP (testreg, 0));
13151 mark_used_regs_combine (SET_SRC (x));
13153 return;
13155 default:
13156 break;
13159 /* Recursively scan the operands of this expression. */
13162 const char *fmt = GET_RTX_FORMAT (code);
13164 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13166 if (fmt[i] == 'e')
13167 mark_used_regs_combine (XEXP (x, i));
13168 else if (fmt[i] == 'E')
13170 int j;
13172 for (j = 0; j < XVECLEN (x, i); j++)
13173 mark_used_regs_combine (XVECEXP (x, i, j));
13179 /* Remove register number REGNO from the dead registers list of INSN.
13181 Return the note used to record the death, if there was one. */
13184 remove_death (unsigned int regno, rtx_insn *insn)
13186 rtx note = find_regno_note (insn, REG_DEAD, regno);
13188 if (note)
13189 remove_note (insn, note);
13191 return note;
13194 /* For each register (hardware or pseudo) used within expression X, if its
13195 death is in an instruction with luid between FROM_LUID (inclusive) and
13196 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13197 list headed by PNOTES.
13199 That said, don't move registers killed by maybe_kill_insn.
13201 This is done when X is being merged by combination into TO_INSN. These
13202 notes will then be distributed as needed. */
13204 static void
13205 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13206 rtx *pnotes)
13208 const char *fmt;
13209 int len, i;
13210 enum rtx_code code = GET_CODE (x);
13212 if (code == REG)
13214 unsigned int regno = REGNO (x);
13215 rtx_insn *where_dead = reg_stat[regno].last_death;
13217 /* Don't move the register if it gets killed in between from and to. */
13218 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13219 && ! reg_referenced_p (x, maybe_kill_insn))
13220 return;
13222 if (where_dead
13223 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13224 && DF_INSN_LUID (where_dead) >= from_luid
13225 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13227 rtx note = remove_death (regno, where_dead);
13229 /* It is possible for the call above to return 0. This can occur
13230 when last_death points to I2 or I1 that we combined with.
13231 In that case make a new note.
13233 We must also check for the case where X is a hard register
13234 and NOTE is a death note for a range of hard registers
13235 including X. In that case, we must put REG_DEAD notes for
13236 the remaining registers in place of NOTE. */
13238 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13239 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13240 > GET_MODE_SIZE (GET_MODE (x))))
13242 unsigned int deadregno = REGNO (XEXP (note, 0));
13243 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13244 unsigned int ourend = END_HARD_REGNO (x);
13245 unsigned int i;
13247 for (i = deadregno; i < deadend; i++)
13248 if (i < regno || i >= ourend)
13249 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13252 /* If we didn't find any note, or if we found a REG_DEAD note that
13253 covers only part of the given reg, and we have a multi-reg hard
13254 register, then to be safe we must check for REG_DEAD notes
13255 for each register other than the first. They could have
13256 their own REG_DEAD notes lying around. */
13257 else if ((note == 0
13258 || (note != 0
13259 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13260 < GET_MODE_SIZE (GET_MODE (x)))))
13261 && regno < FIRST_PSEUDO_REGISTER
13262 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13264 unsigned int ourend = END_HARD_REGNO (x);
13265 unsigned int i, offset;
13266 rtx oldnotes = 0;
13268 if (note)
13269 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13270 else
13271 offset = 1;
13273 for (i = regno + offset; i < ourend; i++)
13274 move_deaths (regno_reg_rtx[i],
13275 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13278 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13280 XEXP (note, 1) = *pnotes;
13281 *pnotes = note;
13283 else
13284 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13287 return;
13290 else if (GET_CODE (x) == SET)
13292 rtx dest = SET_DEST (x);
13294 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13296 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13297 that accesses one word of a multi-word item, some
13298 piece of everything register in the expression is used by
13299 this insn, so remove any old death. */
13300 /* ??? So why do we test for equality of the sizes? */
13302 if (GET_CODE (dest) == ZERO_EXTRACT
13303 || GET_CODE (dest) == STRICT_LOW_PART
13304 || (GET_CODE (dest) == SUBREG
13305 && (((GET_MODE_SIZE (GET_MODE (dest))
13306 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13307 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13308 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13310 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13311 return;
13314 /* If this is some other SUBREG, we know it replaces the entire
13315 value, so use that as the destination. */
13316 if (GET_CODE (dest) == SUBREG)
13317 dest = SUBREG_REG (dest);
13319 /* If this is a MEM, adjust deaths of anything used in the address.
13320 For a REG (the only other possibility), the entire value is
13321 being replaced so the old value is not used in this insn. */
13323 if (MEM_P (dest))
13324 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13325 to_insn, pnotes);
13326 return;
13329 else if (GET_CODE (x) == CLOBBER)
13330 return;
13332 len = GET_RTX_LENGTH (code);
13333 fmt = GET_RTX_FORMAT (code);
13335 for (i = 0; i < len; i++)
13337 if (fmt[i] == 'E')
13339 int j;
13340 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13341 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13342 to_insn, pnotes);
13344 else if (fmt[i] == 'e')
13345 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13349 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13350 pattern of an insn. X must be a REG. */
13352 static int
13353 reg_bitfield_target_p (rtx x, rtx body)
13355 int i;
13357 if (GET_CODE (body) == SET)
13359 rtx dest = SET_DEST (body);
13360 rtx target;
13361 unsigned int regno, tregno, endregno, endtregno;
13363 if (GET_CODE (dest) == ZERO_EXTRACT)
13364 target = XEXP (dest, 0);
13365 else if (GET_CODE (dest) == STRICT_LOW_PART)
13366 target = SUBREG_REG (XEXP (dest, 0));
13367 else
13368 return 0;
13370 if (GET_CODE (target) == SUBREG)
13371 target = SUBREG_REG (target);
13373 if (!REG_P (target))
13374 return 0;
13376 tregno = REGNO (target), regno = REGNO (x);
13377 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13378 return target == x;
13380 endtregno = end_hard_regno (GET_MODE (target), tregno);
13381 endregno = end_hard_regno (GET_MODE (x), regno);
13383 return endregno > tregno && regno < endtregno;
13386 else if (GET_CODE (body) == PARALLEL)
13387 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13388 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13389 return 1;
13391 return 0;
13394 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13395 as appropriate. I3 and I2 are the insns resulting from the combination
13396 insns including FROM (I2 may be zero).
13398 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13399 not need REG_DEAD notes because they are being substituted for. This
13400 saves searching in the most common cases.
13402 Each note in the list is either ignored or placed on some insns, depending
13403 on the type of note. */
13405 static void
13406 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13407 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13409 rtx note, next_note;
13410 rtx tem_note;
13411 rtx_insn *tem_insn;
13413 for (note = notes; note; note = next_note)
13415 rtx_insn *place = 0, *place2 = 0;
13417 next_note = XEXP (note, 1);
13418 switch (REG_NOTE_KIND (note))
13420 case REG_BR_PROB:
13421 case REG_BR_PRED:
13422 /* Doesn't matter much where we put this, as long as it's somewhere.
13423 It is preferable to keep these notes on branches, which is most
13424 likely to be i3. */
13425 place = i3;
13426 break;
13428 case REG_NON_LOCAL_GOTO:
13429 if (JUMP_P (i3))
13430 place = i3;
13431 else
13433 gcc_assert (i2 && JUMP_P (i2));
13434 place = i2;
13436 break;
13438 case REG_EH_REGION:
13439 /* These notes must remain with the call or trapping instruction. */
13440 if (CALL_P (i3))
13441 place = i3;
13442 else if (i2 && CALL_P (i2))
13443 place = i2;
13444 else
13446 gcc_assert (cfun->can_throw_non_call_exceptions);
13447 if (may_trap_p (i3))
13448 place = i3;
13449 else if (i2 && may_trap_p (i2))
13450 place = i2;
13451 /* ??? Otherwise assume we've combined things such that we
13452 can now prove that the instructions can't trap. Drop the
13453 note in this case. */
13455 break;
13457 case REG_ARGS_SIZE:
13458 /* ??? How to distribute between i3-i1. Assume i3 contains the
13459 entire adjustment. Assert i3 contains at least some adjust. */
13460 if (!noop_move_p (i3))
13462 int old_size, args_size = INTVAL (XEXP (note, 0));
13463 /* fixup_args_size_notes looks at REG_NORETURN note,
13464 so ensure the note is placed there first. */
13465 if (CALL_P (i3))
13467 rtx *np;
13468 for (np = &next_note; *np; np = &XEXP (*np, 1))
13469 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13471 rtx n = *np;
13472 *np = XEXP (n, 1);
13473 XEXP (n, 1) = REG_NOTES (i3);
13474 REG_NOTES (i3) = n;
13475 break;
13478 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13479 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13480 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13481 gcc_assert (old_size != args_size
13482 || (CALL_P (i3)
13483 && !ACCUMULATE_OUTGOING_ARGS
13484 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13486 break;
13488 case REG_NORETURN:
13489 case REG_SETJMP:
13490 case REG_TM:
13491 case REG_CALL_DECL:
13492 /* These notes must remain with the call. It should not be
13493 possible for both I2 and I3 to be a call. */
13494 if (CALL_P (i3))
13495 place = i3;
13496 else
13498 gcc_assert (i2 && CALL_P (i2));
13499 place = i2;
13501 break;
13503 case REG_UNUSED:
13504 /* Any clobbers for i3 may still exist, and so we must process
13505 REG_UNUSED notes from that insn.
13507 Any clobbers from i2 or i1 can only exist if they were added by
13508 recog_for_combine. In that case, recog_for_combine created the
13509 necessary REG_UNUSED notes. Trying to keep any original
13510 REG_UNUSED notes from these insns can cause incorrect output
13511 if it is for the same register as the original i3 dest.
13512 In that case, we will notice that the register is set in i3,
13513 and then add a REG_UNUSED note for the destination of i3, which
13514 is wrong. However, it is possible to have REG_UNUSED notes from
13515 i2 or i1 for register which were both used and clobbered, so
13516 we keep notes from i2 or i1 if they will turn into REG_DEAD
13517 notes. */
13519 /* If this register is set or clobbered in I3, put the note there
13520 unless there is one already. */
13521 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13523 if (from_insn != i3)
13524 break;
13526 if (! (REG_P (XEXP (note, 0))
13527 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13528 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13529 place = i3;
13531 /* Otherwise, if this register is used by I3, then this register
13532 now dies here, so we must put a REG_DEAD note here unless there
13533 is one already. */
13534 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13535 && ! (REG_P (XEXP (note, 0))
13536 ? find_regno_note (i3, REG_DEAD,
13537 REGNO (XEXP (note, 0)))
13538 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13540 PUT_REG_NOTE_KIND (note, REG_DEAD);
13541 place = i3;
13543 break;
13545 case REG_EQUAL:
13546 case REG_EQUIV:
13547 case REG_NOALIAS:
13548 /* These notes say something about results of an insn. We can
13549 only support them if they used to be on I3 in which case they
13550 remain on I3. Otherwise they are ignored.
13552 If the note refers to an expression that is not a constant, we
13553 must also ignore the note since we cannot tell whether the
13554 equivalence is still true. It might be possible to do
13555 slightly better than this (we only have a problem if I2DEST
13556 or I1DEST is present in the expression), but it doesn't
13557 seem worth the trouble. */
13559 if (from_insn == i3
13560 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13561 place = i3;
13562 break;
13564 case REG_INC:
13565 /* These notes say something about how a register is used. They must
13566 be present on any use of the register in I2 or I3. */
13567 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13568 place = i3;
13570 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13572 if (place)
13573 place2 = i2;
13574 else
13575 place = i2;
13577 break;
13579 case REG_LABEL_TARGET:
13580 case REG_LABEL_OPERAND:
13581 /* This can show up in several ways -- either directly in the
13582 pattern, or hidden off in the constant pool with (or without?)
13583 a REG_EQUAL note. */
13584 /* ??? Ignore the without-reg_equal-note problem for now. */
13585 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13586 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13587 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13588 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13589 place = i3;
13591 if (i2
13592 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13593 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13594 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13595 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13597 if (place)
13598 place2 = i2;
13599 else
13600 place = i2;
13603 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13604 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13605 there. */
13606 if (place && JUMP_P (place)
13607 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13608 && (JUMP_LABEL (place) == NULL
13609 || JUMP_LABEL (place) == XEXP (note, 0)))
13611 rtx label = JUMP_LABEL (place);
13613 if (!label)
13614 JUMP_LABEL (place) = XEXP (note, 0);
13615 else if (LABEL_P (label))
13616 LABEL_NUSES (label)--;
13619 if (place2 && JUMP_P (place2)
13620 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13621 && (JUMP_LABEL (place2) == NULL
13622 || JUMP_LABEL (place2) == XEXP (note, 0)))
13624 rtx label = JUMP_LABEL (place2);
13626 if (!label)
13627 JUMP_LABEL (place2) = XEXP (note, 0);
13628 else if (LABEL_P (label))
13629 LABEL_NUSES (label)--;
13630 place2 = 0;
13632 break;
13634 case REG_NONNEG:
13635 /* This note says something about the value of a register prior
13636 to the execution of an insn. It is too much trouble to see
13637 if the note is still correct in all situations. It is better
13638 to simply delete it. */
13639 break;
13641 case REG_DEAD:
13642 /* If we replaced the right hand side of FROM_INSN with a
13643 REG_EQUAL note, the original use of the dying register
13644 will not have been combined into I3 and I2. In such cases,
13645 FROM_INSN is guaranteed to be the first of the combined
13646 instructions, so we simply need to search back before
13647 FROM_INSN for the previous use or set of this register,
13648 then alter the notes there appropriately.
13650 If the register is used as an input in I3, it dies there.
13651 Similarly for I2, if it is nonzero and adjacent to I3.
13653 If the register is not used as an input in either I3 or I2
13654 and it is not one of the registers we were supposed to eliminate,
13655 there are two possibilities. We might have a non-adjacent I2
13656 or we might have somehow eliminated an additional register
13657 from a computation. For example, we might have had A & B where
13658 we discover that B will always be zero. In this case we will
13659 eliminate the reference to A.
13661 In both cases, we must search to see if we can find a previous
13662 use of A and put the death note there. */
13664 if (from_insn
13665 && from_insn == i2mod
13666 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13667 tem_insn = from_insn;
13668 else
13670 if (from_insn
13671 && CALL_P (from_insn)
13672 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13673 place = from_insn;
13674 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13675 place = i3;
13676 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13677 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13678 place = i2;
13679 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13680 && !(i2mod
13681 && reg_overlap_mentioned_p (XEXP (note, 0),
13682 i2mod_old_rhs)))
13683 || rtx_equal_p (XEXP (note, 0), elim_i1)
13684 || rtx_equal_p (XEXP (note, 0), elim_i0))
13685 break;
13686 tem_insn = i3;
13689 if (place == 0)
13691 basic_block bb = this_basic_block;
13693 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13695 if (!NONDEBUG_INSN_P (tem_insn))
13697 if (tem_insn == BB_HEAD (bb))
13698 break;
13699 continue;
13702 /* If the register is being set at TEM_INSN, see if that is all
13703 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13704 into a REG_UNUSED note instead. Don't delete sets to
13705 global register vars. */
13706 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13707 || !global_regs[REGNO (XEXP (note, 0))])
13708 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13710 rtx set = single_set (tem_insn);
13711 rtx inner_dest = 0;
13712 #ifdef HAVE_cc0
13713 rtx_insn *cc0_setter = NULL;
13714 #endif
13716 if (set != 0)
13717 for (inner_dest = SET_DEST (set);
13718 (GET_CODE (inner_dest) == STRICT_LOW_PART
13719 || GET_CODE (inner_dest) == SUBREG
13720 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13721 inner_dest = XEXP (inner_dest, 0))
13724 /* Verify that it was the set, and not a clobber that
13725 modified the register.
13727 CC0 targets must be careful to maintain setter/user
13728 pairs. If we cannot delete the setter due to side
13729 effects, mark the user with an UNUSED note instead
13730 of deleting it. */
13732 if (set != 0 && ! side_effects_p (SET_SRC (set))
13733 && rtx_equal_p (XEXP (note, 0), inner_dest)
13734 #ifdef HAVE_cc0
13735 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13736 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13737 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13738 #endif
13741 /* Move the notes and links of TEM_INSN elsewhere.
13742 This might delete other dead insns recursively.
13743 First set the pattern to something that won't use
13744 any register. */
13745 rtx old_notes = REG_NOTES (tem_insn);
13747 PATTERN (tem_insn) = pc_rtx;
13748 REG_NOTES (tem_insn) = NULL;
13750 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13751 NULL_RTX, NULL_RTX, NULL_RTX);
13752 distribute_links (LOG_LINKS (tem_insn));
13754 SET_INSN_DELETED (tem_insn);
13755 if (tem_insn == i2)
13756 i2 = NULL;
13758 #ifdef HAVE_cc0
13759 /* Delete the setter too. */
13760 if (cc0_setter)
13762 PATTERN (cc0_setter) = pc_rtx;
13763 old_notes = REG_NOTES (cc0_setter);
13764 REG_NOTES (cc0_setter) = NULL;
13766 distribute_notes (old_notes, cc0_setter,
13767 cc0_setter, NULL,
13768 NULL_RTX, NULL_RTX, NULL_RTX);
13769 distribute_links (LOG_LINKS (cc0_setter));
13771 SET_INSN_DELETED (cc0_setter);
13772 if (cc0_setter == i2)
13773 i2 = NULL;
13775 #endif
13777 else
13779 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13781 /* If there isn't already a REG_UNUSED note, put one
13782 here. Do not place a REG_DEAD note, even if
13783 the register is also used here; that would not
13784 match the algorithm used in lifetime analysis
13785 and can cause the consistency check in the
13786 scheduler to fail. */
13787 if (! find_regno_note (tem_insn, REG_UNUSED,
13788 REGNO (XEXP (note, 0))))
13789 place = tem_insn;
13790 break;
13793 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13794 || (CALL_P (tem_insn)
13795 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13797 place = tem_insn;
13799 /* If we are doing a 3->2 combination, and we have a
13800 register which formerly died in i3 and was not used
13801 by i2, which now no longer dies in i3 and is used in
13802 i2 but does not die in i2, and place is between i2
13803 and i3, then we may need to move a link from place to
13804 i2. */
13805 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13806 && from_insn
13807 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13808 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13810 struct insn_link *links = LOG_LINKS (place);
13811 LOG_LINKS (place) = NULL;
13812 distribute_links (links);
13814 break;
13817 if (tem_insn == BB_HEAD (bb))
13818 break;
13823 /* If the register is set or already dead at PLACE, we needn't do
13824 anything with this note if it is still a REG_DEAD note.
13825 We check here if it is set at all, not if is it totally replaced,
13826 which is what `dead_or_set_p' checks, so also check for it being
13827 set partially. */
13829 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13831 unsigned int regno = REGNO (XEXP (note, 0));
13832 reg_stat_type *rsp = &reg_stat[regno];
13834 if (dead_or_set_p (place, XEXP (note, 0))
13835 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13837 /* Unless the register previously died in PLACE, clear
13838 last_death. [I no longer understand why this is
13839 being done.] */
13840 if (rsp->last_death != place)
13841 rsp->last_death = 0;
13842 place = 0;
13844 else
13845 rsp->last_death = place;
13847 /* If this is a death note for a hard reg that is occupying
13848 multiple registers, ensure that we are still using all
13849 parts of the object. If we find a piece of the object
13850 that is unused, we must arrange for an appropriate REG_DEAD
13851 note to be added for it. However, we can't just emit a USE
13852 and tag the note to it, since the register might actually
13853 be dead; so we recourse, and the recursive call then finds
13854 the previous insn that used this register. */
13856 if (place && regno < FIRST_PSEUDO_REGISTER
13857 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13859 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13860 bool all_used = true;
13861 unsigned int i;
13863 for (i = regno; i < endregno; i++)
13864 if ((! refers_to_regno_p (i, PATTERN (place))
13865 && ! find_regno_fusage (place, USE, i))
13866 || dead_or_set_regno_p (place, i))
13868 all_used = false;
13869 break;
13872 if (! all_used)
13874 /* Put only REG_DEAD notes for pieces that are
13875 not already dead or set. */
13877 for (i = regno; i < endregno;
13878 i += hard_regno_nregs[i][reg_raw_mode[i]])
13880 rtx piece = regno_reg_rtx[i];
13881 basic_block bb = this_basic_block;
13883 if (! dead_or_set_p (place, piece)
13884 && ! reg_bitfield_target_p (piece,
13885 PATTERN (place)))
13887 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13888 NULL_RTX);
13890 distribute_notes (new_note, place, place,
13891 NULL, NULL_RTX, NULL_RTX,
13892 NULL_RTX);
13894 else if (! refers_to_regno_p (i, PATTERN (place))
13895 && ! find_regno_fusage (place, USE, i))
13896 for (tem_insn = PREV_INSN (place); ;
13897 tem_insn = PREV_INSN (tem_insn))
13899 if (!NONDEBUG_INSN_P (tem_insn))
13901 if (tem_insn == BB_HEAD (bb))
13902 break;
13903 continue;
13905 if (dead_or_set_p (tem_insn, piece)
13906 || reg_bitfield_target_p (piece,
13907 PATTERN (tem_insn)))
13909 add_reg_note (tem_insn, REG_UNUSED, piece);
13910 break;
13915 place = 0;
13919 break;
13921 default:
13922 /* Any other notes should not be present at this point in the
13923 compilation. */
13924 gcc_unreachable ();
13927 if (place)
13929 XEXP (note, 1) = REG_NOTES (place);
13930 REG_NOTES (place) = note;
13933 if (place2)
13934 add_shallow_copy_of_reg_note (place2, note);
13938 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13939 I3, I2, and I1 to new locations. This is also called to add a link
13940 pointing at I3 when I3's destination is changed. */
13942 static void
13943 distribute_links (struct insn_link *links)
13945 struct insn_link *link, *next_link;
13947 for (link = links; link; link = next_link)
13949 rtx_insn *place = 0;
13950 rtx_insn *insn;
13951 rtx set, reg;
13953 next_link = link->next;
13955 /* If the insn that this link points to is a NOTE, ignore it. */
13956 if (NOTE_P (link->insn))
13957 continue;
13959 set = 0;
13960 rtx pat = PATTERN (link->insn);
13961 if (GET_CODE (pat) == SET)
13962 set = pat;
13963 else if (GET_CODE (pat) == PARALLEL)
13965 int i;
13966 for (i = 0; i < XVECLEN (pat, 0); i++)
13968 set = XVECEXP (pat, 0, i);
13969 if (GET_CODE (set) != SET)
13970 continue;
13972 reg = SET_DEST (set);
13973 while (GET_CODE (reg) == ZERO_EXTRACT
13974 || GET_CODE (reg) == STRICT_LOW_PART
13975 || GET_CODE (reg) == SUBREG)
13976 reg = XEXP (reg, 0);
13978 if (!REG_P (reg))
13979 continue;
13981 if (REGNO (reg) == link->regno)
13982 break;
13984 if (i == XVECLEN (pat, 0))
13985 continue;
13987 else
13988 continue;
13990 reg = SET_DEST (set);
13992 while (GET_CODE (reg) == ZERO_EXTRACT
13993 || GET_CODE (reg) == STRICT_LOW_PART
13994 || GET_CODE (reg) == SUBREG)
13995 reg = XEXP (reg, 0);
13997 /* A LOG_LINK is defined as being placed on the first insn that uses
13998 a register and points to the insn that sets the register. Start
13999 searching at the next insn after the target of the link and stop
14000 when we reach a set of the register or the end of the basic block.
14002 Note that this correctly handles the link that used to point from
14003 I3 to I2. Also note that not much searching is typically done here
14004 since most links don't point very far away. */
14006 for (insn = NEXT_INSN (link->insn);
14007 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14008 || BB_HEAD (this_basic_block->next_bb) != insn));
14009 insn = NEXT_INSN (insn))
14010 if (DEBUG_INSN_P (insn))
14011 continue;
14012 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14014 if (reg_referenced_p (reg, PATTERN (insn)))
14015 place = insn;
14016 break;
14018 else if (CALL_P (insn)
14019 && find_reg_fusage (insn, USE, reg))
14021 place = insn;
14022 break;
14024 else if (INSN_P (insn) && reg_set_p (reg, insn))
14025 break;
14027 /* If we found a place to put the link, place it there unless there
14028 is already a link to the same insn as LINK at that point. */
14030 if (place)
14032 struct insn_link *link2;
14034 FOR_EACH_LOG_LINK (link2, place)
14035 if (link2->insn == link->insn && link2->regno == link->regno)
14036 break;
14038 if (link2 == NULL)
14040 link->next = LOG_LINKS (place);
14041 LOG_LINKS (place) = link;
14043 /* Set added_links_insn to the earliest insn we added a
14044 link to. */
14045 if (added_links_insn == 0
14046 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14047 added_links_insn = place;
14053 /* Check for any register or memory mentioned in EQUIV that is not
14054 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14055 of EXPR where some registers may have been replaced by constants. */
14057 static bool
14058 unmentioned_reg_p (rtx equiv, rtx expr)
14060 subrtx_iterator::array_type array;
14061 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14063 const_rtx x = *iter;
14064 if ((REG_P (x) || MEM_P (x))
14065 && !reg_mentioned_p (x, expr))
14066 return true;
14068 return false;
14071 DEBUG_FUNCTION void
14072 dump_combine_stats (FILE *file)
14074 fprintf
14075 (file,
14076 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14077 combine_attempts, combine_merges, combine_extras, combine_successes);
14080 void
14081 dump_combine_total_stats (FILE *file)
14083 fprintf
14084 (file,
14085 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14086 total_attempts, total_merges, total_extras, total_successes);
14089 /* Try combining insns through substitution. */
14090 static unsigned int
14091 rest_of_handle_combine (void)
14093 int rebuild_jump_labels_after_combine;
14095 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14096 df_note_add_problem ();
14097 df_analyze ();
14099 regstat_init_n_sets_and_refs ();
14101 rebuild_jump_labels_after_combine
14102 = combine_instructions (get_insns (), max_reg_num ());
14104 /* Combining insns may have turned an indirect jump into a
14105 direct jump. Rebuild the JUMP_LABEL fields of jumping
14106 instructions. */
14107 if (rebuild_jump_labels_after_combine)
14109 timevar_push (TV_JUMP);
14110 rebuild_jump_labels (get_insns ());
14111 cleanup_cfg (0);
14112 timevar_pop (TV_JUMP);
14115 regstat_free_n_sets_and_refs ();
14116 return 0;
14119 namespace {
14121 const pass_data pass_data_combine =
14123 RTL_PASS, /* type */
14124 "combine", /* name */
14125 OPTGROUP_NONE, /* optinfo_flags */
14126 TV_COMBINE, /* tv_id */
14127 PROP_cfglayout, /* properties_required */
14128 0, /* properties_provided */
14129 0, /* properties_destroyed */
14130 0, /* todo_flags_start */
14131 TODO_df_finish, /* todo_flags_finish */
14134 class pass_combine : public rtl_opt_pass
14136 public:
14137 pass_combine (gcc::context *ctxt)
14138 : rtl_opt_pass (pass_data_combine, ctxt)
14141 /* opt_pass methods: */
14142 virtual bool gate (function *) { return (optimize > 0); }
14143 virtual unsigned int execute (function *)
14145 return rest_of_handle_combine ();
14148 }; // class pass_combine
14150 } // anon namespace
14152 rtl_opt_pass *
14153 make_pass_combine (gcc::context *ctxt)
14155 return new pass_combine (ctxt);