PR rtl-optimization/57003
[official-gcc.git] / gcc / combine.c
blobff5f0dbd509c917fca50af74f7f049a0ad242f3a
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2014 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 "tree.h"
84 #include "stor-layout.h"
85 #include "tm_p.h"
86 #include "flags.h"
87 #include "regs.h"
88 #include "hard-reg-set.h"
89 #include "basic-block.h"
90 #include "insn-config.h"
91 #include "function.h"
92 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
93 #include "expr.h"
94 #include "insn-attr.h"
95 #include "recog.h"
96 #include "diagnostic-core.h"
97 #include "target.h"
98 #include "optabs.h"
99 #include "insn-codes.h"
100 #include "rtlhooks-def.h"
101 #include "params.h"
102 #include "tree-pass.h"
103 #include "df.h"
104 #include "valtrack.h"
105 #include "cgraph.h"
106 #include "obstack.h"
107 #include "statistics.h"
108 #include "params.h"
109 #include "rtl-iter.h"
111 /* Number of attempts to combine instructions in this function. */
113 static int combine_attempts;
115 /* Number of attempts that got as far as substitution in this function. */
117 static int combine_merges;
119 /* Number of instructions combined with added SETs in this function. */
121 static int combine_extras;
123 /* Number of instructions combined in this function. */
125 static int combine_successes;
127 /* Totals over entire compilation. */
129 static int total_attempts, total_merges, total_extras, total_successes;
131 /* combine_instructions may try to replace the right hand side of the
132 second instruction with the value of an associated REG_EQUAL note
133 before throwing it at try_combine. That is problematic when there
134 is a REG_DEAD note for a register used in the old right hand side
135 and can cause distribute_notes to do wrong things. This is the
136 second instruction if it has been so modified, null otherwise. */
138 static rtx_insn *i2mod;
140 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
142 static rtx i2mod_old_rhs;
144 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
146 static rtx i2mod_new_rhs;
148 typedef struct reg_stat_struct {
149 /* Record last point of death of (hard or pseudo) register n. */
150 rtx_insn *last_death;
152 /* Record last point of modification of (hard or pseudo) register n. */
153 rtx_insn *last_set;
155 /* The next group of fields allows the recording of the last value assigned
156 to (hard or pseudo) register n. We use this information to see if an
157 operation being processed is redundant given a prior operation performed
158 on the register. For example, an `and' with a constant is redundant if
159 all the zero bits are already known to be turned off.
161 We use an approach similar to that used by cse, but change it in the
162 following ways:
164 (1) We do not want to reinitialize at each label.
165 (2) It is useful, but not critical, to know the actual value assigned
166 to a register. Often just its form is helpful.
168 Therefore, we maintain the following fields:
170 last_set_value the last value assigned
171 last_set_label records the value of label_tick when the
172 register was assigned
173 last_set_table_tick records the value of label_tick when a
174 value using the register is assigned
175 last_set_invalid set to nonzero when it is not valid
176 to use the value of this register in some
177 register's value
179 To understand the usage of these tables, it is important to understand
180 the distinction between the value in last_set_value being valid and
181 the register being validly contained in some other expression in the
182 table.
184 (The next two parameters are out of date).
186 reg_stat[i].last_set_value is valid if it is nonzero, and either
187 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
189 Register I may validly appear in any expression returned for the value
190 of another register if reg_n_sets[i] is 1. It may also appear in the
191 value for register J if reg_stat[j].last_set_invalid is zero, or
192 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
194 If an expression is found in the table containing a register which may
195 not validly appear in an expression, the register is replaced by
196 something that won't match, (clobber (const_int 0)). */
198 /* Record last value assigned to (hard or pseudo) register n. */
200 rtx last_set_value;
202 /* Record the value of label_tick when an expression involving register n
203 is placed in last_set_value. */
205 int last_set_table_tick;
207 /* Record the value of label_tick when the value for register n is placed in
208 last_set_value. */
210 int last_set_label;
212 /* These fields are maintained in parallel with last_set_value and are
213 used to store the mode in which the register was last set, the bits
214 that were known to be zero when it was last set, and the number of
215 sign bits copies it was known to have when it was last set. */
217 unsigned HOST_WIDE_INT last_set_nonzero_bits;
218 char last_set_sign_bit_copies;
219 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
221 /* Set nonzero if references to register n in expressions should not be
222 used. last_set_invalid is set nonzero when this register is being
223 assigned to and last_set_table_tick == label_tick. */
225 char last_set_invalid;
227 /* Some registers that are set more than once and used in more than one
228 basic block are nevertheless always set in similar ways. For example,
229 a QImode register may be loaded from memory in two places on a machine
230 where byte loads zero extend.
232 We record in the following fields if a register has some leading bits
233 that are always equal to the sign bit, and what we know about the
234 nonzero bits of a register, specifically which bits are known to be
235 zero.
237 If an entry is zero, it means that we don't know anything special. */
239 unsigned char sign_bit_copies;
241 unsigned HOST_WIDE_INT nonzero_bits;
243 /* Record the value of the label_tick when the last truncation
244 happened. The field truncated_to_mode is only valid if
245 truncation_label == label_tick. */
247 int truncation_label;
249 /* Record the last truncation seen for this register. If truncation
250 is not a nop to this mode we might be able to save an explicit
251 truncation if we know that value already contains a truncated
252 value. */
254 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
255 } reg_stat_type;
258 static vec<reg_stat_type> reg_stat;
260 /* Record the luid of the last insn that invalidated memory
261 (anything that writes memory, and subroutine calls, but not pushes). */
263 static int mem_last_set;
265 /* Record the luid of the last CALL_INSN
266 so we can tell whether a potential combination crosses any calls. */
268 static int last_call_luid;
270 /* When `subst' is called, this is the insn that is being modified
271 (by combining in a previous insn). The PATTERN of this insn
272 is still the old pattern partially modified and it should not be
273 looked at, but this may be used to examine the successors of the insn
274 to judge whether a simplification is valid. */
276 static rtx_insn *subst_insn;
278 /* This is the lowest LUID that `subst' is currently dealing with.
279 get_last_value will not return a value if the register was set at or
280 after this LUID. If not for this mechanism, we could get confused if
281 I2 or I1 in try_combine were an insn that used the old value of a register
282 to obtain a new value. In that case, we might erroneously get the
283 new value of the register when we wanted the old one. */
285 static int subst_low_luid;
287 /* This contains any hard registers that are used in newpat; reg_dead_at_p
288 must consider all these registers to be always live. */
290 static HARD_REG_SET newpat_used_regs;
292 /* This is an insn to which a LOG_LINKS entry has been added. If this
293 insn is the earlier than I2 or I3, combine should rescan starting at
294 that location. */
296 static rtx_insn *added_links_insn;
298 /* Basic block in which we are performing combines. */
299 static basic_block this_basic_block;
300 static bool optimize_this_for_speed_p;
303 /* Length of the currently allocated uid_insn_cost array. */
305 static int max_uid_known;
307 /* The following array records the insn_rtx_cost for every insn
308 in the instruction stream. */
310 static int *uid_insn_cost;
312 /* The following array records the LOG_LINKS for every insn in the
313 instruction stream as struct insn_link pointers. */
315 struct insn_link {
316 rtx_insn *insn;
317 struct insn_link *next;
320 static struct insn_link **uid_log_links;
322 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
323 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
325 #define FOR_EACH_LOG_LINK(L, INSN) \
326 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
328 /* Links for LOG_LINKS are allocated from this obstack. */
330 static struct obstack insn_link_obstack;
332 /* Allocate a link. */
334 static inline struct insn_link *
335 alloc_insn_link (rtx_insn *insn, struct insn_link *next)
337 struct insn_link *l
338 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
339 sizeof (struct insn_link));
340 l->insn = insn;
341 l->next = next;
342 return l;
345 /* Incremented for each basic block. */
347 static int label_tick;
349 /* Reset to label_tick for each extended basic block in scanning order. */
351 static int label_tick_ebb_start;
353 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
354 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
356 static enum machine_mode nonzero_bits_mode;
358 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
359 be safely used. It is zero while computing them and after combine has
360 completed. This former test prevents propagating values based on
361 previously set values, which can be incorrect if a variable is modified
362 in a loop. */
364 static int nonzero_sign_valid;
367 /* Record one modification to rtl structure
368 to be undone by storing old_contents into *where. */
370 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
372 struct undo
374 struct undo *next;
375 enum undo_kind kind;
376 union { rtx r; int i; enum machine_mode m; struct insn_link *l; } old_contents;
377 union { rtx *r; int *i; struct insn_link **l; } where;
380 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
381 num_undo says how many are currently recorded.
383 other_insn is nonzero if we have modified some other insn in the process
384 of working on subst_insn. It must be verified too. */
386 struct undobuf
388 struct undo *undos;
389 struct undo *frees;
390 rtx_insn *other_insn;
393 static struct undobuf undobuf;
395 /* Number of times the pseudo being substituted for
396 was found and replaced. */
398 static int n_occurrences;
400 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
401 enum machine_mode,
402 unsigned HOST_WIDE_INT,
403 unsigned HOST_WIDE_INT *);
404 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
405 enum machine_mode,
406 unsigned int, unsigned int *);
407 static void do_SUBST (rtx *, rtx);
408 static void do_SUBST_INT (int *, int);
409 static void init_reg_last (void);
410 static void setup_incoming_promotions (rtx_insn *);
411 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
412 static int cant_combine_insn_p (rtx_insn *);
413 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
414 rtx_insn *, rtx_insn *, rtx *, rtx *);
415 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
416 static int contains_muldiv (rtx);
417 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
418 int *, rtx_insn *);
419 static void undo_all (void);
420 static void undo_commit (void);
421 static rtx *find_split_point (rtx *, rtx_insn *, bool);
422 static rtx subst (rtx, rtx, rtx, int, int, int);
423 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
424 static rtx simplify_if_then_else (rtx);
425 static rtx simplify_set (rtx);
426 static rtx simplify_logical (rtx);
427 static rtx expand_compound_operation (rtx);
428 static const_rtx expand_field_assignment (const_rtx);
429 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
430 rtx, unsigned HOST_WIDE_INT, int, int, int);
431 static rtx extract_left_shift (rtx, int);
432 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
433 unsigned HOST_WIDE_INT *);
434 static rtx canon_reg_for_combine (rtx, rtx);
435 static rtx force_to_mode (rtx, enum machine_mode,
436 unsigned HOST_WIDE_INT, int);
437 static rtx if_then_else_cond (rtx, rtx *, rtx *);
438 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
439 static int rtx_equal_for_field_assignment_p (rtx, rtx);
440 static rtx make_field_assignment (rtx);
441 static rtx apply_distributive_law (rtx);
442 static rtx distribute_and_simplify_rtx (rtx, int);
443 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
444 unsigned HOST_WIDE_INT);
445 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
446 unsigned HOST_WIDE_INT);
447 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
448 HOST_WIDE_INT, enum machine_mode, int *);
449 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
450 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
451 int);
452 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
453 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
454 static enum rtx_code simplify_compare_const (enum rtx_code, enum machine_mode,
455 rtx, rtx *);
456 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
457 static void update_table_tick (rtx);
458 static void record_value_for_reg (rtx, rtx_insn *, rtx);
459 static void check_promoted_subreg (rtx_insn *, rtx);
460 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
461 static void record_dead_and_set_regs (rtx_insn *);
462 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
463 static rtx get_last_value (const_rtx);
464 static int use_crosses_set_p (const_rtx, int);
465 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
466 static int reg_dead_at_p (rtx, rtx_insn *);
467 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
468 static int reg_bitfield_target_p (rtx, rtx);
469 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
470 static void distribute_links (struct insn_link *);
471 static void mark_used_regs_combine (rtx);
472 static void record_promoted_value (rtx_insn *, rtx);
473 static bool unmentioned_reg_p (rtx, rtx);
474 static void record_truncated_values (rtx *, void *);
475 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
476 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
479 /* It is not safe to use ordinary gen_lowpart in combine.
480 See comments in gen_lowpart_for_combine. */
481 #undef RTL_HOOKS_GEN_LOWPART
482 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
484 /* Our implementation of gen_lowpart never emits a new pseudo. */
485 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
486 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
488 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
489 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
491 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
492 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
494 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
495 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
497 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
500 /* Convenience wrapper for the canonicalize_comparison target hook.
501 Target hooks cannot use enum rtx_code. */
502 static inline void
503 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
504 bool op0_preserve_value)
506 int code_int = (int)*code;
507 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
508 *code = (enum rtx_code)code_int;
511 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
512 PATTERN can not be split. Otherwise, it returns an insn sequence.
513 This is a wrapper around split_insns which ensures that the
514 reg_stat vector is made larger if the splitter creates a new
515 register. */
517 static rtx_insn *
518 combine_split_insns (rtx pattern, rtx insn)
520 rtx_insn *ret;
521 unsigned int nregs;
523 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
524 nregs = max_reg_num ();
525 if (nregs > reg_stat.length ())
526 reg_stat.safe_grow_cleared (nregs);
527 return ret;
530 /* This is used by find_single_use to locate an rtx in LOC that
531 contains exactly one use of DEST, which is typically either a REG
532 or CC0. It returns a pointer to the innermost rtx expression
533 containing DEST. Appearances of DEST that are being used to
534 totally replace it are not counted. */
536 static rtx *
537 find_single_use_1 (rtx dest, rtx *loc)
539 rtx x = *loc;
540 enum rtx_code code = GET_CODE (x);
541 rtx *result = NULL;
542 rtx *this_result;
543 int i;
544 const char *fmt;
546 switch (code)
548 case CONST:
549 case LABEL_REF:
550 case SYMBOL_REF:
551 CASE_CONST_ANY:
552 case CLOBBER:
553 return 0;
555 case SET:
556 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
557 of a REG that occupies all of the REG, the insn uses DEST if
558 it is mentioned in the destination or the source. Otherwise, we
559 need just check the source. */
560 if (GET_CODE (SET_DEST (x)) != CC0
561 && GET_CODE (SET_DEST (x)) != PC
562 && !REG_P (SET_DEST (x))
563 && ! (GET_CODE (SET_DEST (x)) == SUBREG
564 && REG_P (SUBREG_REG (SET_DEST (x)))
565 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
566 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
567 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
568 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
569 break;
571 return find_single_use_1 (dest, &SET_SRC (x));
573 case MEM:
574 case SUBREG:
575 return find_single_use_1 (dest, &XEXP (x, 0));
577 default:
578 break;
581 /* If it wasn't one of the common cases above, check each expression and
582 vector of this code. Look for a unique usage of DEST. */
584 fmt = GET_RTX_FORMAT (code);
585 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
587 if (fmt[i] == 'e')
589 if (dest == XEXP (x, i)
590 || (REG_P (dest) && REG_P (XEXP (x, i))
591 && REGNO (dest) == REGNO (XEXP (x, i))))
592 this_result = loc;
593 else
594 this_result = find_single_use_1 (dest, &XEXP (x, i));
596 if (result == NULL)
597 result = this_result;
598 else if (this_result)
599 /* Duplicate usage. */
600 return NULL;
602 else if (fmt[i] == 'E')
604 int j;
606 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
608 if (XVECEXP (x, i, j) == dest
609 || (REG_P (dest)
610 && REG_P (XVECEXP (x, i, j))
611 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
612 this_result = loc;
613 else
614 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
616 if (result == NULL)
617 result = this_result;
618 else if (this_result)
619 return NULL;
624 return result;
628 /* See if DEST, produced in INSN, is used only a single time in the
629 sequel. If so, return a pointer to the innermost rtx expression in which
630 it is used.
632 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
634 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
635 care about REG_DEAD notes or LOG_LINKS.
637 Otherwise, we find the single use by finding an insn that has a
638 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
639 only referenced once in that insn, we know that it must be the first
640 and last insn referencing DEST. */
642 static rtx *
643 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
645 basic_block bb;
646 rtx_insn *next;
647 rtx *result;
648 struct insn_link *link;
650 #ifdef HAVE_cc0
651 if (dest == cc0_rtx)
653 next = NEXT_INSN (insn);
654 if (next == 0
655 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
656 return 0;
658 result = find_single_use_1 (dest, &PATTERN (next));
659 if (result && ploc)
660 *ploc = next;
661 return result;
663 #endif
665 if (!REG_P (dest))
666 return 0;
668 bb = BLOCK_FOR_INSN (insn);
669 for (next = NEXT_INSN (insn);
670 next && BLOCK_FOR_INSN (next) == bb;
671 next = NEXT_INSN (next))
672 if (INSN_P (next) && dead_or_set_p (next, dest))
674 FOR_EACH_LOG_LINK (link, next)
675 if (link->insn == insn)
676 break;
678 if (link)
680 result = find_single_use_1 (dest, &PATTERN (next));
681 if (ploc)
682 *ploc = next;
683 return result;
687 return 0;
690 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
691 insn. The substitution can be undone by undo_all. If INTO is already
692 set to NEWVAL, do not record this change. Because computing NEWVAL might
693 also call SUBST, we have to compute it before we put anything into
694 the undo table. */
696 static void
697 do_SUBST (rtx *into, rtx newval)
699 struct undo *buf;
700 rtx oldval = *into;
702 if (oldval == newval)
703 return;
705 /* We'd like to catch as many invalid transformations here as
706 possible. Unfortunately, there are way too many mode changes
707 that are perfectly valid, so we'd waste too much effort for
708 little gain doing the checks here. Focus on catching invalid
709 transformations involving integer constants. */
710 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
711 && CONST_INT_P (newval))
713 /* Sanity check that we're replacing oldval with a CONST_INT
714 that is a valid sign-extension for the original mode. */
715 gcc_assert (INTVAL (newval)
716 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
718 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
719 CONST_INT is not valid, because after the replacement, the
720 original mode would be gone. Unfortunately, we can't tell
721 when do_SUBST is called to replace the operand thereof, so we
722 perform this test on oldval instead, checking whether an
723 invalid replacement took place before we got here. */
724 gcc_assert (!(GET_CODE (oldval) == SUBREG
725 && CONST_INT_P (SUBREG_REG (oldval))));
726 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
727 && CONST_INT_P (XEXP (oldval, 0))));
730 if (undobuf.frees)
731 buf = undobuf.frees, undobuf.frees = buf->next;
732 else
733 buf = XNEW (struct undo);
735 buf->kind = UNDO_RTX;
736 buf->where.r = into;
737 buf->old_contents.r = oldval;
738 *into = newval;
740 buf->next = undobuf.undos, undobuf.undos = buf;
743 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
745 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
746 for the value of a HOST_WIDE_INT value (including CONST_INT) is
747 not safe. */
749 static void
750 do_SUBST_INT (int *into, int newval)
752 struct undo *buf;
753 int oldval = *into;
755 if (oldval == newval)
756 return;
758 if (undobuf.frees)
759 buf = undobuf.frees, undobuf.frees = buf->next;
760 else
761 buf = XNEW (struct undo);
763 buf->kind = UNDO_INT;
764 buf->where.i = into;
765 buf->old_contents.i = oldval;
766 *into = newval;
768 buf->next = undobuf.undos, undobuf.undos = buf;
771 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
773 /* Similar to SUBST, but just substitute the mode. This is used when
774 changing the mode of a pseudo-register, so that any other
775 references to the entry in the regno_reg_rtx array will change as
776 well. */
778 static void
779 do_SUBST_MODE (rtx *into, enum machine_mode newval)
781 struct undo *buf;
782 enum machine_mode oldval = GET_MODE (*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_MODE;
793 buf->where.r = into;
794 buf->old_contents.m = oldval;
795 adjust_reg_mode (*into, newval);
797 buf->next = undobuf.undos, undobuf.undos = buf;
800 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
802 #ifndef HAVE_cc0
803 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
805 static void
806 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
808 struct undo *buf;
809 struct insn_link * oldval = *into;
811 if (oldval == newval)
812 return;
814 if (undobuf.frees)
815 buf = undobuf.frees, undobuf.frees = buf->next;
816 else
817 buf = XNEW (struct undo);
819 buf->kind = UNDO_LINKS;
820 buf->where.l = into;
821 buf->old_contents.l = oldval;
822 *into = newval;
824 buf->next = undobuf.undos, undobuf.undos = buf;
827 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
828 #endif
830 /* Subroutine of try_combine. Determine whether the replacement patterns
831 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
832 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
833 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
834 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
835 of all the instructions can be estimated and the replacements are more
836 expensive than the original sequence. */
838 static bool
839 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
840 rtx newpat, rtx newi2pat, rtx newotherpat)
842 int i0_cost, i1_cost, i2_cost, i3_cost;
843 int new_i2_cost, new_i3_cost;
844 int old_cost, new_cost;
846 /* Lookup the original insn_rtx_costs. */
847 i2_cost = INSN_COST (i2);
848 i3_cost = INSN_COST (i3);
850 if (i1)
852 i1_cost = INSN_COST (i1);
853 if (i0)
855 i0_cost = INSN_COST (i0);
856 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
857 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
859 else
861 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
862 ? i1_cost + i2_cost + i3_cost : 0);
863 i0_cost = 0;
866 else
868 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
869 i1_cost = i0_cost = 0;
872 /* Calculate the replacement insn_rtx_costs. */
873 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
874 if (newi2pat)
876 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
877 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
878 ? new_i2_cost + new_i3_cost : 0;
880 else
882 new_cost = new_i3_cost;
883 new_i2_cost = 0;
886 if (undobuf.other_insn)
888 int old_other_cost, new_other_cost;
890 old_other_cost = INSN_COST (undobuf.other_insn);
891 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
892 if (old_other_cost > 0 && new_other_cost > 0)
894 old_cost += old_other_cost;
895 new_cost += new_other_cost;
897 else
898 old_cost = 0;
901 /* Disallow this combination if both new_cost and old_cost are greater than
902 zero, and new_cost is greater than old cost. */
903 if (old_cost > 0 && new_cost > old_cost)
905 if (dump_file)
907 if (i0)
909 fprintf (dump_file,
910 "rejecting combination of insns %d, %d, %d and %d\n",
911 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
912 INSN_UID (i3));
913 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
914 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
916 else if (i1)
918 fprintf (dump_file,
919 "rejecting combination of insns %d, %d and %d\n",
920 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
921 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
922 i1_cost, i2_cost, i3_cost, old_cost);
924 else
926 fprintf (dump_file,
927 "rejecting combination of insns %d and %d\n",
928 INSN_UID (i2), INSN_UID (i3));
929 fprintf (dump_file, "original costs %d + %d = %d\n",
930 i2_cost, i3_cost, old_cost);
933 if (newi2pat)
935 fprintf (dump_file, "replacement costs %d + %d = %d\n",
936 new_i2_cost, new_i3_cost, new_cost);
938 else
939 fprintf (dump_file, "replacement cost %d\n", new_cost);
942 return false;
945 /* Update the uid_insn_cost array with the replacement costs. */
946 INSN_COST (i2) = new_i2_cost;
947 INSN_COST (i3) = new_i3_cost;
948 if (i1)
950 INSN_COST (i1) = 0;
951 if (i0)
952 INSN_COST (i0) = 0;
955 return true;
959 /* Delete any insns that copy a register to itself. */
961 static void
962 delete_noop_moves (void)
964 rtx_insn *insn, *next;
965 basic_block bb;
967 FOR_EACH_BB_FN (bb, cfun)
969 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
971 next = NEXT_INSN (insn);
972 if (INSN_P (insn) && noop_move_p (insn))
974 if (dump_file)
975 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
977 delete_insn_and_edges (insn);
984 /* Fill in log links field for all insns. */
986 static void
987 create_log_links (void)
989 basic_block bb;
990 rtx_insn **next_use;
991 rtx_insn *insn;
992 df_ref def, use;
994 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
996 /* Pass through each block from the end, recording the uses of each
997 register and establishing log links when def is encountered.
998 Note that we do not clear next_use array in order to save time,
999 so we have to test whether the use is in the same basic block as def.
1001 There are a few cases below when we do not consider the definition or
1002 usage -- these are taken from original flow.c did. Don't ask me why it is
1003 done this way; I don't know and if it works, I don't want to know. */
1005 FOR_EACH_BB_FN (bb, cfun)
1007 FOR_BB_INSNS_REVERSE (bb, insn)
1009 if (!NONDEBUG_INSN_P (insn))
1010 continue;
1012 /* Log links are created only once. */
1013 gcc_assert (!LOG_LINKS (insn));
1015 FOR_EACH_INSN_DEF (def, insn)
1017 int regno = DF_REF_REGNO (def);
1018 rtx_insn *use_insn;
1020 if (!next_use[regno])
1021 continue;
1023 /* Do not consider if it is pre/post modification in MEM. */
1024 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1025 continue;
1027 /* Do not make the log link for frame pointer. */
1028 if ((regno == FRAME_POINTER_REGNUM
1029 && (! reload_completed || frame_pointer_needed))
1030 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1031 || (regno == HARD_FRAME_POINTER_REGNUM
1032 && (! reload_completed || frame_pointer_needed))
1033 #endif
1034 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1035 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1036 #endif
1038 continue;
1040 use_insn = next_use[regno];
1041 if (BLOCK_FOR_INSN (use_insn) == bb)
1043 /* flow.c claimed:
1045 We don't build a LOG_LINK for hard registers contained
1046 in ASM_OPERANDs. If these registers get replaced,
1047 we might wind up changing the semantics of the insn,
1048 even if reload can make what appear to be valid
1049 assignments later. */
1050 if (regno >= FIRST_PSEUDO_REGISTER
1051 || asm_noperands (PATTERN (use_insn)) < 0)
1053 /* Don't add duplicate links between instructions. */
1054 struct insn_link *links;
1055 FOR_EACH_LOG_LINK (links, use_insn)
1056 if (insn == links->insn)
1057 break;
1059 if (!links)
1060 LOG_LINKS (use_insn)
1061 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1064 next_use[regno] = NULL;
1067 FOR_EACH_INSN_USE (use, insn)
1069 int regno = DF_REF_REGNO (use);
1071 /* Do not consider the usage of the stack pointer
1072 by function call. */
1073 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1074 continue;
1076 next_use[regno] = insn;
1081 free (next_use);
1084 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1085 true if we found a LOG_LINK that proves that A feeds B. This only works
1086 if there are no instructions between A and B which could have a link
1087 depending on A, since in that case we would not record a link for B.
1088 We also check the implicit dependency created by a cc0 setter/user
1089 pair. */
1091 static bool
1092 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1094 struct insn_link *links;
1095 FOR_EACH_LOG_LINK (links, b)
1096 if (links->insn == a)
1097 return true;
1098 #ifdef HAVE_cc0
1099 if (sets_cc0_p (a))
1100 return true;
1101 #endif
1102 return false;
1105 /* Main entry point for combiner. F is the first insn of the function.
1106 NREGS is the first unused pseudo-reg number.
1108 Return nonzero if the combiner has turned an indirect jump
1109 instruction into a direct jump. */
1110 static int
1111 combine_instructions (rtx_insn *f, unsigned int nregs)
1113 rtx_insn *insn, *next;
1114 #ifdef HAVE_cc0
1115 rtx_insn *prev;
1116 #endif
1117 struct insn_link *links, *nextlinks;
1118 rtx_insn *first;
1119 basic_block last_bb;
1121 int new_direct_jump_p = 0;
1123 for (first = f; first && !INSN_P (first); )
1124 first = NEXT_INSN (first);
1125 if (!first)
1126 return 0;
1128 combine_attempts = 0;
1129 combine_merges = 0;
1130 combine_extras = 0;
1131 combine_successes = 0;
1133 rtl_hooks = combine_rtl_hooks;
1135 reg_stat.safe_grow_cleared (nregs);
1137 init_recog_no_volatile ();
1139 /* Allocate array for insn info. */
1140 max_uid_known = get_max_uid ();
1141 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1142 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1143 gcc_obstack_init (&insn_link_obstack);
1145 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1147 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1148 problems when, for example, we have j <<= 1 in a loop. */
1150 nonzero_sign_valid = 0;
1151 label_tick = label_tick_ebb_start = 1;
1153 /* Scan all SETs and see if we can deduce anything about what
1154 bits are known to be zero for some registers and how many copies
1155 of the sign bit are known to exist for those registers.
1157 Also set any known values so that we can use it while searching
1158 for what bits are known to be set. */
1160 setup_incoming_promotions (first);
1161 /* Allow the entry block and the first block to fall into the same EBB.
1162 Conceptually the incoming promotions are assigned to the entry block. */
1163 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1165 create_log_links ();
1166 FOR_EACH_BB_FN (this_basic_block, cfun)
1168 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1169 last_call_luid = 0;
1170 mem_last_set = -1;
1172 label_tick++;
1173 if (!single_pred_p (this_basic_block)
1174 || single_pred (this_basic_block) != last_bb)
1175 label_tick_ebb_start = label_tick;
1176 last_bb = this_basic_block;
1178 FOR_BB_INSNS (this_basic_block, insn)
1179 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1181 #ifdef AUTO_INC_DEC
1182 rtx links;
1183 #endif
1185 subst_low_luid = DF_INSN_LUID (insn);
1186 subst_insn = insn;
1188 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1189 insn);
1190 record_dead_and_set_regs (insn);
1192 #ifdef AUTO_INC_DEC
1193 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1194 if (REG_NOTE_KIND (links) == REG_INC)
1195 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1196 insn);
1197 #endif
1199 /* Record the current insn_rtx_cost of this instruction. */
1200 if (NONJUMP_INSN_P (insn))
1201 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1202 optimize_this_for_speed_p);
1203 if (dump_file)
1204 fprintf (dump_file, "insn_cost %d: %d\n",
1205 INSN_UID (insn), INSN_COST (insn));
1209 nonzero_sign_valid = 1;
1211 /* Now scan all the insns in forward order. */
1212 label_tick = label_tick_ebb_start = 1;
1213 init_reg_last ();
1214 setup_incoming_promotions (first);
1215 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1216 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1218 FOR_EACH_BB_FN (this_basic_block, cfun)
1220 rtx_insn *last_combined_insn = NULL;
1221 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1222 last_call_luid = 0;
1223 mem_last_set = -1;
1225 label_tick++;
1226 if (!single_pred_p (this_basic_block)
1227 || single_pred (this_basic_block) != last_bb)
1228 label_tick_ebb_start = label_tick;
1229 last_bb = this_basic_block;
1231 rtl_profile_for_bb (this_basic_block);
1232 for (insn = BB_HEAD (this_basic_block);
1233 insn != NEXT_INSN (BB_END (this_basic_block));
1234 insn = next ? next : NEXT_INSN (insn))
1236 next = 0;
1237 if (!NONDEBUG_INSN_P (insn))
1238 continue;
1240 while (last_combined_insn
1241 && last_combined_insn->deleted ())
1242 last_combined_insn = PREV_INSN (last_combined_insn);
1243 if (last_combined_insn == NULL_RTX
1244 || BARRIER_P (last_combined_insn)
1245 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1246 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1247 last_combined_insn = insn;
1249 /* See if we know about function return values before this
1250 insn based upon SUBREG flags. */
1251 check_promoted_subreg (insn, PATTERN (insn));
1253 /* See if we can find hardregs and subreg of pseudos in
1254 narrower modes. This could help turning TRUNCATEs
1255 into SUBREGs. */
1256 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1258 /* Try this insn with each insn it links back to. */
1260 FOR_EACH_LOG_LINK (links, insn)
1261 if ((next = try_combine (insn, links->insn, NULL,
1262 NULL, &new_direct_jump_p,
1263 last_combined_insn)) != 0)
1265 statistics_counter_event (cfun, "two-insn combine", 1);
1266 goto retry;
1269 /* Try each sequence of three linked insns ending with this one. */
1271 if (max_combine >= 3)
1272 FOR_EACH_LOG_LINK (links, insn)
1274 rtx_insn *link = links->insn;
1276 /* If the linked insn has been replaced by a note, then there
1277 is no point in pursuing this chain any further. */
1278 if (NOTE_P (link))
1279 continue;
1281 FOR_EACH_LOG_LINK (nextlinks, link)
1282 if ((next = try_combine (insn, link, nextlinks->insn,
1283 NULL, &new_direct_jump_p,
1284 last_combined_insn)) != 0)
1286 statistics_counter_event (cfun, "three-insn combine", 1);
1287 goto retry;
1291 #ifdef HAVE_cc0
1292 /* Try to combine a jump insn that uses CC0
1293 with a preceding insn that sets CC0, and maybe with its
1294 logical predecessor as well.
1295 This is how we make decrement-and-branch insns.
1296 We need this special code because data flow connections
1297 via CC0 do not get entered in LOG_LINKS. */
1299 if (JUMP_P (insn)
1300 && (prev = prev_nonnote_insn (insn)) != 0
1301 && NONJUMP_INSN_P (prev)
1302 && sets_cc0_p (PATTERN (prev)))
1304 if ((next = try_combine (insn, prev, NULL, NULL,
1305 &new_direct_jump_p,
1306 last_combined_insn)) != 0)
1307 goto retry;
1309 FOR_EACH_LOG_LINK (nextlinks, prev)
1310 if ((next = try_combine (insn, prev, nextlinks->insn,
1311 NULL, &new_direct_jump_p,
1312 last_combined_insn)) != 0)
1313 goto retry;
1316 /* Do the same for an insn that explicitly references CC0. */
1317 if (NONJUMP_INSN_P (insn)
1318 && (prev = prev_nonnote_insn (insn)) != 0
1319 && NONJUMP_INSN_P (prev)
1320 && sets_cc0_p (PATTERN (prev))
1321 && GET_CODE (PATTERN (insn)) == SET
1322 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1324 if ((next = try_combine (insn, prev, NULL, NULL,
1325 &new_direct_jump_p,
1326 last_combined_insn)) != 0)
1327 goto retry;
1329 FOR_EACH_LOG_LINK (nextlinks, prev)
1330 if ((next = try_combine (insn, prev, nextlinks->insn,
1331 NULL, &new_direct_jump_p,
1332 last_combined_insn)) != 0)
1333 goto retry;
1336 /* Finally, see if any of the insns that this insn links to
1337 explicitly references CC0. If so, try this insn, that insn,
1338 and its predecessor if it sets CC0. */
1339 FOR_EACH_LOG_LINK (links, insn)
1340 if (NONJUMP_INSN_P (links->insn)
1341 && GET_CODE (PATTERN (links->insn)) == SET
1342 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1343 && (prev = prev_nonnote_insn (links->insn)) != 0
1344 && NONJUMP_INSN_P (prev)
1345 && sets_cc0_p (PATTERN (prev))
1346 && (next = try_combine (insn, links->insn,
1347 prev, NULL, &new_direct_jump_p,
1348 last_combined_insn)) != 0)
1349 goto retry;
1350 #endif
1352 /* Try combining an insn with two different insns whose results it
1353 uses. */
1354 if (max_combine >= 3)
1355 FOR_EACH_LOG_LINK (links, insn)
1356 for (nextlinks = links->next; nextlinks;
1357 nextlinks = nextlinks->next)
1358 if ((next = try_combine (insn, links->insn,
1359 nextlinks->insn, NULL,
1360 &new_direct_jump_p,
1361 last_combined_insn)) != 0)
1364 statistics_counter_event (cfun, "three-insn combine", 1);
1365 goto retry;
1368 /* Try four-instruction combinations. */
1369 if (max_combine >= 4)
1370 FOR_EACH_LOG_LINK (links, insn)
1372 struct insn_link *next1;
1373 rtx_insn *link = links->insn;
1375 /* If the linked insn has been replaced by a note, then there
1376 is no point in pursuing this chain any further. */
1377 if (NOTE_P (link))
1378 continue;
1380 FOR_EACH_LOG_LINK (next1, link)
1382 rtx_insn *link1 = next1->insn;
1383 if (NOTE_P (link1))
1384 continue;
1385 /* I0 -> I1 -> I2 -> I3. */
1386 FOR_EACH_LOG_LINK (nextlinks, link1)
1387 if ((next = try_combine (insn, link, link1,
1388 nextlinks->insn,
1389 &new_direct_jump_p,
1390 last_combined_insn)) != 0)
1392 statistics_counter_event (cfun, "four-insn combine", 1);
1393 goto retry;
1395 /* I0, I1 -> I2, I2 -> I3. */
1396 for (nextlinks = next1->next; nextlinks;
1397 nextlinks = nextlinks->next)
1398 if ((next = try_combine (insn, link, link1,
1399 nextlinks->insn,
1400 &new_direct_jump_p,
1401 last_combined_insn)) != 0)
1403 statistics_counter_event (cfun, "four-insn combine", 1);
1404 goto retry;
1408 for (next1 = links->next; next1; next1 = next1->next)
1410 rtx_insn *link1 = next1->insn;
1411 if (NOTE_P (link1))
1412 continue;
1413 /* I0 -> I2; I1, I2 -> I3. */
1414 FOR_EACH_LOG_LINK (nextlinks, link)
1415 if ((next = try_combine (insn, link, link1,
1416 nextlinks->insn,
1417 &new_direct_jump_p,
1418 last_combined_insn)) != 0)
1420 statistics_counter_event (cfun, "four-insn combine", 1);
1421 goto retry;
1423 /* I0 -> I1; 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;
1436 /* Try this insn with each REG_EQUAL note it links back to. */
1437 FOR_EACH_LOG_LINK (links, insn)
1439 rtx set, note;
1440 rtx_insn *temp = links->insn;
1441 if ((set = single_set (temp)) != 0
1442 && (note = find_reg_equal_equiv_note (temp)) != 0
1443 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1444 /* Avoid using a register that may already been marked
1445 dead by an earlier instruction. */
1446 && ! unmentioned_reg_p (note, SET_SRC (set))
1447 && (GET_MODE (note) == VOIDmode
1448 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1449 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1451 /* Temporarily replace the set's source with the
1452 contents of the REG_EQUAL note. The insn will
1453 be deleted or recognized by try_combine. */
1454 rtx orig = SET_SRC (set);
1455 SET_SRC (set) = note;
1456 i2mod = temp;
1457 i2mod_old_rhs = copy_rtx (orig);
1458 i2mod_new_rhs = copy_rtx (note);
1459 next = try_combine (insn, i2mod, NULL, NULL,
1460 &new_direct_jump_p,
1461 last_combined_insn);
1462 i2mod = NULL;
1463 if (next)
1465 statistics_counter_event (cfun, "insn-with-note combine", 1);
1466 goto retry;
1468 SET_SRC (set) = orig;
1472 if (!NOTE_P (insn))
1473 record_dead_and_set_regs (insn);
1475 retry:
1480 default_rtl_profile ();
1481 clear_bb_flags ();
1482 new_direct_jump_p |= purge_all_dead_edges ();
1483 delete_noop_moves ();
1485 /* Clean up. */
1486 obstack_free (&insn_link_obstack, NULL);
1487 free (uid_log_links);
1488 free (uid_insn_cost);
1489 reg_stat.release ();
1492 struct undo *undo, *next;
1493 for (undo = undobuf.frees; undo; undo = next)
1495 next = undo->next;
1496 free (undo);
1498 undobuf.frees = 0;
1501 total_attempts += combine_attempts;
1502 total_merges += combine_merges;
1503 total_extras += combine_extras;
1504 total_successes += combine_successes;
1506 nonzero_sign_valid = 0;
1507 rtl_hooks = general_rtl_hooks;
1509 /* Make recognizer allow volatile MEMs again. */
1510 init_recog ();
1512 return new_direct_jump_p;
1515 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1517 static void
1518 init_reg_last (void)
1520 unsigned int i;
1521 reg_stat_type *p;
1523 FOR_EACH_VEC_ELT (reg_stat, i, p)
1524 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1527 /* Set up any promoted values for incoming argument registers. */
1529 static void
1530 setup_incoming_promotions (rtx_insn *first)
1532 tree arg;
1533 bool strictly_local = false;
1535 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1536 arg = DECL_CHAIN (arg))
1538 rtx x, reg = DECL_INCOMING_RTL (arg);
1539 int uns1, uns3;
1540 enum machine_mode mode1, mode2, mode3, mode4;
1542 /* Only continue if the incoming argument is in a register. */
1543 if (!REG_P (reg))
1544 continue;
1546 /* Determine, if possible, whether all call sites of the current
1547 function lie within the current compilation unit. (This does
1548 take into account the exporting of a function via taking its
1549 address, and so forth.) */
1550 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1552 /* The mode and signedness of the argument before any promotions happen
1553 (equal to the mode of the pseudo holding it at that stage). */
1554 mode1 = TYPE_MODE (TREE_TYPE (arg));
1555 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1557 /* The mode and signedness of the argument after any source language and
1558 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1559 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1560 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1562 /* The mode and signedness of the argument as it is actually passed,
1563 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1564 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1565 TREE_TYPE (cfun->decl), 0);
1567 /* The mode of the register in which the argument is being passed. */
1568 mode4 = GET_MODE (reg);
1570 /* Eliminate sign extensions in the callee when:
1571 (a) A mode promotion has occurred; */
1572 if (mode1 == mode3)
1573 continue;
1574 /* (b) The mode of the register is the same as the mode of
1575 the argument as it is passed; */
1576 if (mode3 != mode4)
1577 continue;
1578 /* (c) There's no language level extension; */
1579 if (mode1 == mode2)
1581 /* (c.1) All callers are from the current compilation unit. If that's
1582 the case we don't have to rely on an ABI, we only have to know
1583 what we're generating right now, and we know that we will do the
1584 mode1 to mode2 promotion with the given sign. */
1585 else if (!strictly_local)
1586 continue;
1587 /* (c.2) The combination of the two promotions is useful. This is
1588 true when the signs match, or if the first promotion is unsigned.
1589 In the later case, (sign_extend (zero_extend x)) is the same as
1590 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1591 else if (uns1)
1592 uns3 = true;
1593 else if (uns3)
1594 continue;
1596 /* Record that the value was promoted from mode1 to mode3,
1597 so that any sign extension at the head of the current
1598 function may be eliminated. */
1599 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1600 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1601 record_value_for_reg (reg, first, x);
1605 /* Called via note_stores. If X is a pseudo that is narrower than
1606 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1608 If we are setting only a portion of X and we can't figure out what
1609 portion, assume all bits will be used since we don't know what will
1610 be happening.
1612 Similarly, set how many bits of X are known to be copies of the sign bit
1613 at all locations in the function. This is the smallest number implied
1614 by any set of X. */
1616 static void
1617 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1619 rtx_insn *insn = (rtx_insn *) data;
1620 unsigned int num;
1622 if (REG_P (x)
1623 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1624 /* If this register is undefined at the start of the file, we can't
1625 say what its contents were. */
1626 && ! REGNO_REG_SET_P
1627 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1628 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1630 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1632 if (set == 0 || GET_CODE (set) == CLOBBER)
1634 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1635 rsp->sign_bit_copies = 1;
1636 return;
1639 /* If this register is being initialized using itself, and the
1640 register is uninitialized in this basic block, and there are
1641 no LOG_LINKS which set the register, then part of the
1642 register is uninitialized. In that case we can't assume
1643 anything about the number of nonzero bits.
1645 ??? We could do better if we checked this in
1646 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1647 could avoid making assumptions about the insn which initially
1648 sets the register, while still using the information in other
1649 insns. We would have to be careful to check every insn
1650 involved in the combination. */
1652 if (insn
1653 && reg_referenced_p (x, PATTERN (insn))
1654 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1655 REGNO (x)))
1657 struct insn_link *link;
1659 FOR_EACH_LOG_LINK (link, insn)
1660 if (dead_or_set_p (link->insn, x))
1661 break;
1662 if (!link)
1664 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1665 rsp->sign_bit_copies = 1;
1666 return;
1670 /* If this is a complex assignment, see if we can convert it into a
1671 simple assignment. */
1672 set = expand_field_assignment (set);
1674 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1675 set what we know about X. */
1677 if (SET_DEST (set) == x
1678 || (paradoxical_subreg_p (SET_DEST (set))
1679 && SUBREG_REG (SET_DEST (set)) == x))
1681 rtx src = SET_SRC (set);
1683 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1684 /* If X is narrower than a word and SRC is a non-negative
1685 constant that would appear negative in the mode of X,
1686 sign-extend it for use in reg_stat[].nonzero_bits because some
1687 machines (maybe most) will actually do the sign-extension
1688 and this is the conservative approach.
1690 ??? For 2.5, try to tighten up the MD files in this regard
1691 instead of this kludge. */
1693 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1694 && CONST_INT_P (src)
1695 && INTVAL (src) > 0
1696 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1697 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1698 #endif
1700 /* Don't call nonzero_bits if it cannot change anything. */
1701 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1702 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1703 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1704 if (rsp->sign_bit_copies == 0
1705 || rsp->sign_bit_copies > num)
1706 rsp->sign_bit_copies = num;
1708 else
1710 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1711 rsp->sign_bit_copies = 1;
1716 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1717 optionally insns that were previously combined into I3 or that will be
1718 combined into the merger of INSN and I3. The order is PRED, PRED2,
1719 INSN, SUCC, SUCC2, I3.
1721 Return 0 if the combination is not allowed for any reason.
1723 If the combination is allowed, *PDEST will be set to the single
1724 destination of INSN and *PSRC to the single source, and this function
1725 will return 1. */
1727 static int
1728 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1729 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1730 rtx *pdest, rtx *psrc)
1732 int i;
1733 const_rtx set = 0;
1734 rtx src, dest;
1735 rtx_insn *p;
1736 #ifdef AUTO_INC_DEC
1737 rtx link;
1738 #endif
1739 bool all_adjacent = true;
1740 int (*is_volatile_p) (const_rtx);
1742 if (succ)
1744 if (succ2)
1746 if (next_active_insn (succ2) != i3)
1747 all_adjacent = false;
1748 if (next_active_insn (succ) != succ2)
1749 all_adjacent = false;
1751 else if (next_active_insn (succ) != i3)
1752 all_adjacent = false;
1753 if (next_active_insn (insn) != succ)
1754 all_adjacent = false;
1756 else if (next_active_insn (insn) != i3)
1757 all_adjacent = false;
1759 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1760 or a PARALLEL consisting of such a SET and CLOBBERs.
1762 If INSN has CLOBBER parallel parts, ignore them for our processing.
1763 By definition, these happen during the execution of the insn. When it
1764 is merged with another insn, all bets are off. If they are, in fact,
1765 needed and aren't also supplied in I3, they may be added by
1766 recog_for_combine. Otherwise, it won't match.
1768 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1769 note.
1771 Get the source and destination of INSN. If more than one, can't
1772 combine. */
1774 if (GET_CODE (PATTERN (insn)) == SET)
1775 set = PATTERN (insn);
1776 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1777 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1779 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1781 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1783 switch (GET_CODE (elt))
1785 /* This is important to combine floating point insns
1786 for the SH4 port. */
1787 case USE:
1788 /* Combining an isolated USE doesn't make sense.
1789 We depend here on combinable_i3pat to reject them. */
1790 /* The code below this loop only verifies that the inputs of
1791 the SET in INSN do not change. We call reg_set_between_p
1792 to verify that the REG in the USE does not change between
1793 I3 and INSN.
1794 If the USE in INSN was for a pseudo register, the matching
1795 insn pattern will likely match any register; combining this
1796 with any other USE would only be safe if we knew that the
1797 used registers have identical values, or if there was
1798 something to tell them apart, e.g. different modes. For
1799 now, we forgo such complicated tests and simply disallow
1800 combining of USES of pseudo registers with any other USE. */
1801 if (REG_P (XEXP (elt, 0))
1802 && GET_CODE (PATTERN (i3)) == PARALLEL)
1804 rtx i3pat = PATTERN (i3);
1805 int i = XVECLEN (i3pat, 0) - 1;
1806 unsigned int regno = REGNO (XEXP (elt, 0));
1810 rtx i3elt = XVECEXP (i3pat, 0, i);
1812 if (GET_CODE (i3elt) == USE
1813 && REG_P (XEXP (i3elt, 0))
1814 && (REGNO (XEXP (i3elt, 0)) == regno
1815 ? reg_set_between_p (XEXP (elt, 0),
1816 PREV_INSN (insn), i3)
1817 : regno >= FIRST_PSEUDO_REGISTER))
1818 return 0;
1820 while (--i >= 0);
1822 break;
1824 /* We can ignore CLOBBERs. */
1825 case CLOBBER:
1826 break;
1828 case SET:
1829 /* Ignore SETs whose result isn't used but not those that
1830 have side-effects. */
1831 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1832 && insn_nothrow_p (insn)
1833 && !side_effects_p (elt))
1834 break;
1836 /* If we have already found a SET, this is a second one and
1837 so we cannot combine with this insn. */
1838 if (set)
1839 return 0;
1841 set = elt;
1842 break;
1844 default:
1845 /* Anything else means we can't combine. */
1846 return 0;
1850 if (set == 0
1851 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1852 so don't do anything with it. */
1853 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1854 return 0;
1856 else
1857 return 0;
1859 if (set == 0)
1860 return 0;
1862 /* The simplification in expand_field_assignment may call back to
1863 get_last_value, so set safe guard here. */
1864 subst_low_luid = DF_INSN_LUID (insn);
1866 set = expand_field_assignment (set);
1867 src = SET_SRC (set), dest = SET_DEST (set);
1869 /* Don't eliminate a store in the stack pointer. */
1870 if (dest == stack_pointer_rtx
1871 /* Don't combine with an insn that sets a register to itself if it has
1872 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1873 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1874 /* Can't merge an ASM_OPERANDS. */
1875 || GET_CODE (src) == ASM_OPERANDS
1876 /* Can't merge a function call. */
1877 || GET_CODE (src) == CALL
1878 /* Don't eliminate a function call argument. */
1879 || (CALL_P (i3)
1880 && (find_reg_fusage (i3, USE, dest)
1881 || (REG_P (dest)
1882 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1883 && global_regs[REGNO (dest)])))
1884 /* Don't substitute into an incremented register. */
1885 || FIND_REG_INC_NOTE (i3, dest)
1886 || (succ && FIND_REG_INC_NOTE (succ, dest))
1887 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1888 /* Don't substitute into a non-local goto, this confuses CFG. */
1889 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1890 /* Make sure that DEST is not used after SUCC but before I3. */
1891 || (!all_adjacent
1892 && ((succ2
1893 && (reg_used_between_p (dest, succ2, i3)
1894 || reg_used_between_p (dest, succ, succ2)))
1895 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1896 /* Make sure that the value that is to be substituted for the register
1897 does not use any registers whose values alter in between. However,
1898 If the insns are adjacent, a use can't cross a set even though we
1899 think it might (this can happen for a sequence of insns each setting
1900 the same destination; last_set of that register might point to
1901 a NOTE). If INSN has a REG_EQUIV note, the register is always
1902 equivalent to the memory so the substitution is valid even if there
1903 are intervening stores. Also, don't move a volatile asm or
1904 UNSPEC_VOLATILE across any other insns. */
1905 || (! all_adjacent
1906 && (((!MEM_P (src)
1907 || ! find_reg_note (insn, REG_EQUIV, src))
1908 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1909 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1910 || GET_CODE (src) == UNSPEC_VOLATILE))
1911 /* Don't combine across a CALL_INSN, because that would possibly
1912 change whether the life span of some REGs crosses calls or not,
1913 and it is a pain to update that information.
1914 Exception: if source is a constant, moving it later can't hurt.
1915 Accept that as a special case. */
1916 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1917 return 0;
1919 /* DEST must either be a REG or CC0. */
1920 if (REG_P (dest))
1922 /* If register alignment is being enforced for multi-word items in all
1923 cases except for parameters, it is possible to have a register copy
1924 insn referencing a hard register that is not allowed to contain the
1925 mode being copied and which would not be valid as an operand of most
1926 insns. Eliminate this problem by not combining with such an insn.
1928 Also, on some machines we don't want to extend the life of a hard
1929 register. */
1931 if (REG_P (src)
1932 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1933 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1934 /* Don't extend the life of a hard register unless it is
1935 user variable (if we have few registers) or it can't
1936 fit into the desired register (meaning something special
1937 is going on).
1938 Also avoid substituting a return register into I3, because
1939 reload can't handle a conflict with constraints of other
1940 inputs. */
1941 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1942 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1943 return 0;
1945 else if (GET_CODE (dest) != CC0)
1946 return 0;
1949 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1950 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1951 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1953 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1955 /* If the clobber represents an earlyclobber operand, we must not
1956 substitute an expression containing the clobbered register.
1957 As we do not analyze the constraint strings here, we have to
1958 make the conservative assumption. However, if the register is
1959 a fixed hard reg, the clobber cannot represent any operand;
1960 we leave it up to the machine description to either accept or
1961 reject use-and-clobber patterns. */
1962 if (!REG_P (reg)
1963 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1964 || !fixed_regs[REGNO (reg)])
1965 if (reg_overlap_mentioned_p (reg, src))
1966 return 0;
1969 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1970 or not), reject, unless nothing volatile comes between it and I3 */
1972 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1974 /* Make sure neither succ nor succ2 contains a volatile reference. */
1975 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1976 return 0;
1977 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1978 return 0;
1979 /* We'll check insns between INSN and I3 below. */
1982 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1983 to be an explicit register variable, and was chosen for a reason. */
1985 if (GET_CODE (src) == ASM_OPERANDS
1986 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1987 return 0;
1989 /* If INSN contains volatile references (specifically volatile MEMs),
1990 we cannot combine across any other volatile references.
1991 Even if INSN doesn't contain volatile references, any intervening
1992 volatile insn might affect machine state. */
1994 is_volatile_p = volatile_refs_p (PATTERN (insn))
1995 ? volatile_refs_p
1996 : volatile_insn_p;
1998 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1999 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2000 return 0;
2002 /* If INSN contains an autoincrement or autodecrement, make sure that
2003 register is not used between there and I3, and not already used in
2004 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2005 Also insist that I3 not be a jump; if it were one
2006 and the incremented register were spilled, we would lose. */
2008 #ifdef AUTO_INC_DEC
2009 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2010 if (REG_NOTE_KIND (link) == REG_INC
2011 && (JUMP_P (i3)
2012 || reg_used_between_p (XEXP (link, 0), insn, i3)
2013 || (pred != NULL_RTX
2014 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2015 || (pred2 != NULL_RTX
2016 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2017 || (succ != NULL_RTX
2018 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2019 || (succ2 != NULL_RTX
2020 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2021 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2022 return 0;
2023 #endif
2025 #ifdef HAVE_cc0
2026 /* Don't combine an insn that follows a CC0-setting insn.
2027 An insn that uses CC0 must not be separated from the one that sets it.
2028 We do, however, allow I2 to follow a CC0-setting insn if that insn
2029 is passed as I1; in that case it will be deleted also.
2030 We also allow combining in this case if all the insns are adjacent
2031 because that would leave the two CC0 insns adjacent as well.
2032 It would be more logical to test whether CC0 occurs inside I1 or I2,
2033 but that would be much slower, and this ought to be equivalent. */
2035 p = prev_nonnote_insn (insn);
2036 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2037 && ! all_adjacent)
2038 return 0;
2039 #endif
2041 /* If we get here, we have passed all the tests and the combination is
2042 to be allowed. */
2044 *pdest = dest;
2045 *psrc = src;
2047 return 1;
2050 /* LOC is the location within I3 that contains its pattern or the component
2051 of a PARALLEL of the pattern. We validate that it is valid for combining.
2053 One problem is if I3 modifies its output, as opposed to replacing it
2054 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2055 doing so would produce an insn that is not equivalent to the original insns.
2057 Consider:
2059 (set (reg:DI 101) (reg:DI 100))
2060 (set (subreg:SI (reg:DI 101) 0) <foo>)
2062 This is NOT equivalent to:
2064 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2065 (set (reg:DI 101) (reg:DI 100))])
2067 Not only does this modify 100 (in which case it might still be valid
2068 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2070 We can also run into a problem if I2 sets a register that I1
2071 uses and I1 gets directly substituted into I3 (not via I2). In that
2072 case, we would be getting the wrong value of I2DEST into I3, so we
2073 must reject the combination. This case occurs when I2 and I1 both
2074 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2075 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2076 of a SET must prevent combination from occurring. The same situation
2077 can occur for I0, in which case I0_NOT_IN_SRC is set.
2079 Before doing the above check, we first try to expand a field assignment
2080 into a set of logical operations.
2082 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2083 we place a register that is both set and used within I3. If more than one
2084 such register is detected, we fail.
2086 Return 1 if the combination is valid, zero otherwise. */
2088 static int
2089 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2090 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2092 rtx x = *loc;
2094 if (GET_CODE (x) == SET)
2096 rtx set = x ;
2097 rtx dest = SET_DEST (set);
2098 rtx src = SET_SRC (set);
2099 rtx inner_dest = dest;
2100 rtx subdest;
2102 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2103 || GET_CODE (inner_dest) == SUBREG
2104 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2105 inner_dest = XEXP (inner_dest, 0);
2107 /* Check for the case where I3 modifies its output, as discussed
2108 above. We don't want to prevent pseudos from being combined
2109 into the address of a MEM, so only prevent the combination if
2110 i1 or i2 set the same MEM. */
2111 if ((inner_dest != dest &&
2112 (!MEM_P (inner_dest)
2113 || rtx_equal_p (i2dest, inner_dest)
2114 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2115 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2116 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2117 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2118 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2120 /* This is the same test done in can_combine_p except we can't test
2121 all_adjacent; we don't have to, since this instruction will stay
2122 in place, thus we are not considering increasing the lifetime of
2123 INNER_DEST.
2125 Also, if this insn sets a function argument, combining it with
2126 something that might need a spill could clobber a previous
2127 function argument; the all_adjacent test in can_combine_p also
2128 checks this; here, we do a more specific test for this case. */
2130 || (REG_P (inner_dest)
2131 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2132 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2133 GET_MODE (inner_dest))))
2134 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2135 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2136 return 0;
2138 /* If DEST is used in I3, it is being killed in this insn, so
2139 record that for later. We have to consider paradoxical
2140 subregs here, since they kill the whole register, but we
2141 ignore partial subregs, STRICT_LOW_PART, etc.
2142 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2143 STACK_POINTER_REGNUM, since these are always considered to be
2144 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2145 subdest = dest;
2146 if (GET_CODE (subdest) == SUBREG
2147 && (GET_MODE_SIZE (GET_MODE (subdest))
2148 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2149 subdest = SUBREG_REG (subdest);
2150 if (pi3dest_killed
2151 && REG_P (subdest)
2152 && reg_referenced_p (subdest, PATTERN (i3))
2153 && REGNO (subdest) != FRAME_POINTER_REGNUM
2154 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2155 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2156 #endif
2157 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2158 && (REGNO (subdest) != ARG_POINTER_REGNUM
2159 || ! fixed_regs [REGNO (subdest)])
2160 #endif
2161 && REGNO (subdest) != STACK_POINTER_REGNUM)
2163 if (*pi3dest_killed)
2164 return 0;
2166 *pi3dest_killed = subdest;
2170 else if (GET_CODE (x) == PARALLEL)
2172 int i;
2174 for (i = 0; i < XVECLEN (x, 0); i++)
2175 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2176 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2177 return 0;
2180 return 1;
2183 /* Return 1 if X is an arithmetic expression that contains a multiplication
2184 and division. We don't count multiplications by powers of two here. */
2186 static int
2187 contains_muldiv (rtx x)
2189 switch (GET_CODE (x))
2191 case MOD: case DIV: case UMOD: case UDIV:
2192 return 1;
2194 case MULT:
2195 return ! (CONST_INT_P (XEXP (x, 1))
2196 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2197 default:
2198 if (BINARY_P (x))
2199 return contains_muldiv (XEXP (x, 0))
2200 || contains_muldiv (XEXP (x, 1));
2202 if (UNARY_P (x))
2203 return contains_muldiv (XEXP (x, 0));
2205 return 0;
2209 /* Determine whether INSN can be used in a combination. Return nonzero if
2210 not. This is used in try_combine to detect early some cases where we
2211 can't perform combinations. */
2213 static int
2214 cant_combine_insn_p (rtx_insn *insn)
2216 rtx set;
2217 rtx src, dest;
2219 /* If this isn't really an insn, we can't do anything.
2220 This can occur when flow deletes an insn that it has merged into an
2221 auto-increment address. */
2222 if (! INSN_P (insn))
2223 return 1;
2225 /* Never combine loads and stores involving hard regs that are likely
2226 to be spilled. The register allocator can usually handle such
2227 reg-reg moves by tying. If we allow the combiner to make
2228 substitutions of likely-spilled regs, reload might die.
2229 As an exception, we allow combinations involving fixed regs; these are
2230 not available to the register allocator so there's no risk involved. */
2232 set = single_set (insn);
2233 if (! set)
2234 return 0;
2235 src = SET_SRC (set);
2236 dest = SET_DEST (set);
2237 if (GET_CODE (src) == SUBREG)
2238 src = SUBREG_REG (src);
2239 if (GET_CODE (dest) == SUBREG)
2240 dest = SUBREG_REG (dest);
2241 if (REG_P (src) && REG_P (dest)
2242 && ((HARD_REGISTER_P (src)
2243 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2244 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2245 || (HARD_REGISTER_P (dest)
2246 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2247 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2248 return 1;
2250 return 0;
2253 struct likely_spilled_retval_info
2255 unsigned regno, nregs;
2256 unsigned mask;
2259 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2260 hard registers that are known to be written to / clobbered in full. */
2261 static void
2262 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2264 struct likely_spilled_retval_info *const info =
2265 (struct likely_spilled_retval_info *) data;
2266 unsigned regno, nregs;
2267 unsigned new_mask;
2269 if (!REG_P (XEXP (set, 0)))
2270 return;
2271 regno = REGNO (x);
2272 if (regno >= info->regno + info->nregs)
2273 return;
2274 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2275 if (regno + nregs <= info->regno)
2276 return;
2277 new_mask = (2U << (nregs - 1)) - 1;
2278 if (regno < info->regno)
2279 new_mask >>= info->regno - regno;
2280 else
2281 new_mask <<= regno - info->regno;
2282 info->mask &= ~new_mask;
2285 /* Return nonzero iff part of the return value is live during INSN, and
2286 it is likely spilled. This can happen when more than one insn is needed
2287 to copy the return value, e.g. when we consider to combine into the
2288 second copy insn for a complex value. */
2290 static int
2291 likely_spilled_retval_p (rtx_insn *insn)
2293 rtx_insn *use = BB_END (this_basic_block);
2294 rtx reg;
2295 rtx_insn *p;
2296 unsigned regno, nregs;
2297 /* We assume here that no machine mode needs more than
2298 32 hard registers when the value overlaps with a register
2299 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2300 unsigned mask;
2301 struct likely_spilled_retval_info info;
2303 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2304 return 0;
2305 reg = XEXP (PATTERN (use), 0);
2306 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2307 return 0;
2308 regno = REGNO (reg);
2309 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2310 if (nregs == 1)
2311 return 0;
2312 mask = (2U << (nregs - 1)) - 1;
2314 /* Disregard parts of the return value that are set later. */
2315 info.regno = regno;
2316 info.nregs = nregs;
2317 info.mask = mask;
2318 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2319 if (INSN_P (p))
2320 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2321 mask = info.mask;
2323 /* Check if any of the (probably) live return value registers is
2324 likely spilled. */
2325 nregs --;
2328 if ((mask & 1 << nregs)
2329 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2330 return 1;
2331 } while (nregs--);
2332 return 0;
2335 /* Adjust INSN after we made a change to its destination.
2337 Changing the destination can invalidate notes that say something about
2338 the results of the insn and a LOG_LINK pointing to the insn. */
2340 static void
2341 adjust_for_new_dest (rtx_insn *insn)
2343 /* For notes, be conservative and simply remove them. */
2344 remove_reg_equal_equiv_notes (insn);
2346 /* The new insn will have a destination that was previously the destination
2347 of an insn just above it. Call distribute_links to make a LOG_LINK from
2348 the next use of that destination. */
2349 distribute_links (alloc_insn_link (insn, NULL));
2351 df_insn_rescan (insn);
2354 /* Return TRUE if combine can reuse reg X in mode MODE.
2355 ADDED_SETS is nonzero if the original set is still required. */
2356 static bool
2357 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2359 unsigned int regno;
2361 if (!REG_P (x))
2362 return false;
2364 regno = REGNO (x);
2365 /* Allow hard registers if the new mode is legal, and occupies no more
2366 registers than the old mode. */
2367 if (regno < FIRST_PSEUDO_REGISTER)
2368 return (HARD_REGNO_MODE_OK (regno, mode)
2369 && (hard_regno_nregs[regno][GET_MODE (x)]
2370 >= hard_regno_nregs[regno][mode]));
2372 /* Or a pseudo that is only used once. */
2373 return (REG_N_SETS (regno) == 1 && !added_sets
2374 && !REG_USERVAR_P (x));
2378 /* Check whether X, the destination of a set, refers to part of
2379 the register specified by REG. */
2381 static bool
2382 reg_subword_p (rtx x, rtx reg)
2384 /* Check that reg is an integer mode register. */
2385 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2386 return false;
2388 if (GET_CODE (x) == STRICT_LOW_PART
2389 || GET_CODE (x) == ZERO_EXTRACT)
2390 x = XEXP (x, 0);
2392 return GET_CODE (x) == SUBREG
2393 && SUBREG_REG (x) == reg
2394 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2397 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2398 Note that the INSN should be deleted *after* removing dead edges, so
2399 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2400 but not for a (set (pc) (label_ref FOO)). */
2402 static void
2403 update_cfg_for_uncondjump (rtx_insn *insn)
2405 basic_block bb = BLOCK_FOR_INSN (insn);
2406 gcc_assert (BB_END (bb) == insn);
2408 purge_dead_edges (bb);
2410 delete_insn (insn);
2411 if (EDGE_COUNT (bb->succs) == 1)
2413 rtx_insn *insn;
2415 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2417 /* Remove barriers from the footer if there are any. */
2418 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2419 if (BARRIER_P (insn))
2421 if (PREV_INSN (insn))
2422 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2423 else
2424 BB_FOOTER (bb) = NEXT_INSN (insn);
2425 if (NEXT_INSN (insn))
2426 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2428 else if (LABEL_P (insn))
2429 break;
2433 /* Try to combine the insns I0, I1 and I2 into I3.
2434 Here I0, I1 and I2 appear earlier than I3.
2435 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2438 If we are combining more than two insns and the resulting insn is not
2439 recognized, try splitting it into two insns. If that happens, I2 and I3
2440 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2441 Otherwise, I0, I1 and I2 are pseudo-deleted.
2443 Return 0 if the combination does not work. Then nothing is changed.
2444 If we did the combination, return the insn at which combine should
2445 resume scanning.
2447 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2448 new direct jump instruction.
2450 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2451 been I3 passed to an earlier try_combine within the same basic
2452 block. */
2454 static rtx_insn *
2455 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2456 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2458 /* New patterns for I3 and I2, respectively. */
2459 rtx newpat, newi2pat = 0;
2460 rtvec newpat_vec_with_clobbers = 0;
2461 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2462 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2463 dead. */
2464 int added_sets_0, added_sets_1, added_sets_2;
2465 /* Total number of SETs to put into I3. */
2466 int total_sets;
2467 /* Nonzero if I2's or I1's body now appears in I3. */
2468 int i2_is_used = 0, i1_is_used = 0;
2469 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2470 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2471 /* Contains I3 if the destination of I3 is used in its source, which means
2472 that the old life of I3 is being killed. If that usage is placed into
2473 I2 and not in I3, a REG_DEAD note must be made. */
2474 rtx i3dest_killed = 0;
2475 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2476 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2477 /* Copy of SET_SRC of I1 and I0, if needed. */
2478 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2479 /* Set if I2DEST was reused as a scratch register. */
2480 bool i2scratch = false;
2481 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2482 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2483 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2484 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2485 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2486 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2487 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2488 /* Notes that must be added to REG_NOTES in I3 and I2. */
2489 rtx new_i3_notes, new_i2_notes;
2490 /* Notes that we substituted I3 into I2 instead of the normal case. */
2491 int i3_subst_into_i2 = 0;
2492 /* Notes that I1, I2 or I3 is a MULT operation. */
2493 int have_mult = 0;
2494 int swap_i2i3 = 0;
2495 int changed_i3_dest = 0;
2497 int maxreg;
2498 rtx_insn *temp_insn;
2499 rtx temp_expr;
2500 struct insn_link *link;
2501 rtx other_pat = 0;
2502 rtx new_other_notes;
2503 int i;
2505 /* Only try four-insn combinations when there's high likelihood of
2506 success. Look for simple insns, such as loads of constants or
2507 binary operations involving a constant. */
2508 if (i0)
2510 int i;
2511 int ngood = 0;
2512 int nshift = 0;
2514 if (!flag_expensive_optimizations)
2515 return 0;
2517 for (i = 0; i < 4; i++)
2519 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2520 rtx set = single_set (insn);
2521 rtx src;
2522 if (!set)
2523 continue;
2524 src = SET_SRC (set);
2525 if (CONSTANT_P (src))
2527 ngood += 2;
2528 break;
2530 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2531 ngood++;
2532 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2533 || GET_CODE (src) == LSHIFTRT)
2534 nshift++;
2536 if (ngood < 2 && nshift < 2)
2537 return 0;
2540 /* Exit early if one of the insns involved can't be used for
2541 combinations. */
2542 if (cant_combine_insn_p (i3)
2543 || cant_combine_insn_p (i2)
2544 || (i1 && cant_combine_insn_p (i1))
2545 || (i0 && cant_combine_insn_p (i0))
2546 || likely_spilled_retval_p (i3))
2547 return 0;
2549 combine_attempts++;
2550 undobuf.other_insn = 0;
2552 /* Reset the hard register usage information. */
2553 CLEAR_HARD_REG_SET (newpat_used_regs);
2555 if (dump_file && (dump_flags & TDF_DETAILS))
2557 if (i0)
2558 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2559 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2560 else if (i1)
2561 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2562 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2563 else
2564 fprintf (dump_file, "\nTrying %d -> %d:\n",
2565 INSN_UID (i2), INSN_UID (i3));
2568 /* If multiple insns feed into one of I2 or I3, they can be in any
2569 order. To simplify the code below, reorder them in sequence. */
2570 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2571 temp_insn = i2, i2 = i0, i0 = temp_insn;
2572 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2573 temp_insn = i1, i1 = i0, i0 = temp_insn;
2574 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2575 temp_insn = i1, i1 = i2, i2 = temp_insn;
2577 added_links_insn = 0;
2579 /* First check for one important special case that the code below will
2580 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2581 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2582 we may be able to replace that destination with the destination of I3.
2583 This occurs in the common code where we compute both a quotient and
2584 remainder into a structure, in which case we want to do the computation
2585 directly into the structure to avoid register-register copies.
2587 Note that this case handles both multiple sets in I2 and also cases
2588 where I2 has a number of CLOBBERs inside the PARALLEL.
2590 We make very conservative checks below and only try to handle the
2591 most common cases of this. For example, we only handle the case
2592 where I2 and I3 are adjacent to avoid making difficult register
2593 usage tests. */
2595 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2596 && REG_P (SET_SRC (PATTERN (i3)))
2597 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2598 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2599 && GET_CODE (PATTERN (i2)) == PARALLEL
2600 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2601 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2602 below would need to check what is inside (and reg_overlap_mentioned_p
2603 doesn't support those codes anyway). Don't allow those destinations;
2604 the resulting insn isn't likely to be recognized anyway. */
2605 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2606 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2607 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2608 SET_DEST (PATTERN (i3)))
2609 && next_active_insn (i2) == i3)
2611 rtx p2 = PATTERN (i2);
2613 /* Make sure that the destination of I3,
2614 which we are going to substitute into one output of I2,
2615 is not used within another output of I2. We must avoid making this:
2616 (parallel [(set (mem (reg 69)) ...)
2617 (set (reg 69) ...)])
2618 which is not well-defined as to order of actions.
2619 (Besides, reload can't handle output reloads for this.)
2621 The problem can also happen if the dest of I3 is a memory ref,
2622 if another dest in I2 is an indirect memory ref. */
2623 for (i = 0; i < XVECLEN (p2, 0); i++)
2624 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2625 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2626 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2627 SET_DEST (XVECEXP (p2, 0, i))))
2628 break;
2630 if (i == XVECLEN (p2, 0))
2631 for (i = 0; i < XVECLEN (p2, 0); i++)
2632 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2633 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2635 combine_merges++;
2637 subst_insn = i3;
2638 subst_low_luid = DF_INSN_LUID (i2);
2640 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2641 i2src = SET_SRC (XVECEXP (p2, 0, i));
2642 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2643 i2dest_killed = dead_or_set_p (i2, i2dest);
2645 /* Replace the dest in I2 with our dest and make the resulting
2646 insn the new pattern for I3. Then skip to where we validate
2647 the pattern. Everything was set up above. */
2648 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2649 newpat = p2;
2650 i3_subst_into_i2 = 1;
2651 goto validate_replacement;
2655 /* If I2 is setting a pseudo to a constant and I3 is setting some
2656 sub-part of it to another constant, merge them by making a new
2657 constant. */
2658 if (i1 == 0
2659 && (temp_expr = single_set (i2)) != 0
2660 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2661 && GET_CODE (PATTERN (i3)) == SET
2662 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2663 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2665 rtx dest = SET_DEST (PATTERN (i3));
2666 int offset = -1;
2667 int width = 0;
2669 if (GET_CODE (dest) == ZERO_EXTRACT)
2671 if (CONST_INT_P (XEXP (dest, 1))
2672 && CONST_INT_P (XEXP (dest, 2)))
2674 width = INTVAL (XEXP (dest, 1));
2675 offset = INTVAL (XEXP (dest, 2));
2676 dest = XEXP (dest, 0);
2677 if (BITS_BIG_ENDIAN)
2678 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2681 else
2683 if (GET_CODE (dest) == STRICT_LOW_PART)
2684 dest = XEXP (dest, 0);
2685 width = GET_MODE_PRECISION (GET_MODE (dest));
2686 offset = 0;
2689 if (offset >= 0)
2691 /* If this is the low part, we're done. */
2692 if (subreg_lowpart_p (dest))
2694 /* Handle the case where inner is twice the size of outer. */
2695 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2696 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2697 offset += GET_MODE_PRECISION (GET_MODE (dest));
2698 /* Otherwise give up for now. */
2699 else
2700 offset = -1;
2703 if (offset >= 0)
2705 rtx inner = SET_SRC (PATTERN (i3));
2706 rtx outer = SET_SRC (temp_expr);
2708 wide_int o
2709 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2710 std::make_pair (inner, GET_MODE (dest)),
2711 offset, width);
2713 combine_merges++;
2714 subst_insn = i3;
2715 subst_low_luid = DF_INSN_LUID (i2);
2716 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2717 i2dest = SET_DEST (temp_expr);
2718 i2dest_killed = dead_or_set_p (i2, i2dest);
2720 /* Replace the source in I2 with the new constant and make the
2721 resulting insn the new pattern for I3. Then skip to where we
2722 validate the pattern. Everything was set up above. */
2723 SUBST (SET_SRC (temp_expr),
2724 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2726 newpat = PATTERN (i2);
2728 /* The dest of I3 has been replaced with the dest of I2. */
2729 changed_i3_dest = 1;
2730 goto validate_replacement;
2734 #ifndef HAVE_cc0
2735 /* If we have no I1 and I2 looks like:
2736 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2737 (set Y OP)])
2738 make up a dummy I1 that is
2739 (set Y OP)
2740 and change I2 to be
2741 (set (reg:CC X) (compare:CC Y (const_int 0)))
2743 (We can ignore any trailing CLOBBERs.)
2745 This undoes a previous combination and allows us to match a branch-and-
2746 decrement insn. */
2748 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2749 && XVECLEN (PATTERN (i2), 0) >= 2
2750 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2751 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2752 == MODE_CC)
2753 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2754 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2755 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2756 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2757 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2758 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2760 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2761 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2762 break;
2764 if (i == 1)
2766 /* We make I1 with the same INSN_UID as I2. This gives it
2767 the same DF_INSN_LUID for value tracking. Our fake I1 will
2768 never appear in the insn stream so giving it the same INSN_UID
2769 as I2 will not cause a problem. */
2771 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2772 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2773 -1, NULL_RTX);
2774 INSN_UID (i1) = INSN_UID (i2);
2776 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2777 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2778 SET_DEST (PATTERN (i1)));
2779 SUBST_LINK (LOG_LINKS (i2), alloc_insn_link (i1, LOG_LINKS (i2)));
2782 #endif
2784 /* Verify that I2 and I1 are valid for combining. */
2785 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2786 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2787 &i1dest, &i1src))
2788 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2789 &i0dest, &i0src)))
2791 undo_all ();
2792 return 0;
2795 /* Record whether I2DEST is used in I2SRC and similarly for the other
2796 cases. Knowing this will help in register status updating below. */
2797 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2798 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2799 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2800 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2801 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2802 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2803 i2dest_killed = dead_or_set_p (i2, i2dest);
2804 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2805 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2807 /* For the earlier insns, determine which of the subsequent ones they
2808 feed. */
2809 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2810 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2811 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2812 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2813 && reg_overlap_mentioned_p (i0dest, i2src))));
2815 /* Ensure that I3's pattern can be the destination of combines. */
2816 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2817 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2818 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2819 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2820 &i3dest_killed))
2822 undo_all ();
2823 return 0;
2826 /* See if any of the insns is a MULT operation. Unless one is, we will
2827 reject a combination that is, since it must be slower. Be conservative
2828 here. */
2829 if (GET_CODE (i2src) == MULT
2830 || (i1 != 0 && GET_CODE (i1src) == MULT)
2831 || (i0 != 0 && GET_CODE (i0src) == MULT)
2832 || (GET_CODE (PATTERN (i3)) == SET
2833 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2834 have_mult = 1;
2836 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2837 We used to do this EXCEPT in one case: I3 has a post-inc in an
2838 output operand. However, that exception can give rise to insns like
2839 mov r3,(r3)+
2840 which is a famous insn on the PDP-11 where the value of r3 used as the
2841 source was model-dependent. Avoid this sort of thing. */
2843 #if 0
2844 if (!(GET_CODE (PATTERN (i3)) == SET
2845 && REG_P (SET_SRC (PATTERN (i3)))
2846 && MEM_P (SET_DEST (PATTERN (i3)))
2847 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2848 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2849 /* It's not the exception. */
2850 #endif
2851 #ifdef AUTO_INC_DEC
2853 rtx link;
2854 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2855 if (REG_NOTE_KIND (link) == REG_INC
2856 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2857 || (i1 != 0
2858 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2860 undo_all ();
2861 return 0;
2864 #endif
2866 /* See if the SETs in I1 or I2 need to be kept around in the merged
2867 instruction: whenever the value set there is still needed past I3.
2868 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2870 For the SET in I1, we have two cases: if I1 and I2 independently feed
2871 into I3, the set in I1 needs to be kept around unless I1DEST dies
2872 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2873 in I1 needs to be kept around unless I1DEST dies or is set in either
2874 I2 or I3. The same considerations apply to I0. */
2876 added_sets_2 = !dead_or_set_p (i3, i2dest);
2878 if (i1)
2879 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2880 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2881 else
2882 added_sets_1 = 0;
2884 if (i0)
2885 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2886 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
2887 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
2888 && dead_or_set_p (i2, i0dest)));
2889 else
2890 added_sets_0 = 0;
2892 /* We are about to copy insns for the case where they need to be kept
2893 around. Check that they can be copied in the merged instruction. */
2895 if (targetm.cannot_copy_insn_p
2896 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2897 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2898 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2900 undo_all ();
2901 return 0;
2904 /* If the set in I2 needs to be kept around, we must make a copy of
2905 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2906 PATTERN (I2), we are only substituting for the original I1DEST, not into
2907 an already-substituted copy. This also prevents making self-referential
2908 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2909 I2DEST. */
2911 if (added_sets_2)
2913 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2914 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2915 else
2916 i2pat = copy_rtx (PATTERN (i2));
2919 if (added_sets_1)
2921 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2922 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2923 else
2924 i1pat = copy_rtx (PATTERN (i1));
2927 if (added_sets_0)
2929 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2930 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2931 else
2932 i0pat = copy_rtx (PATTERN (i0));
2935 combine_merges++;
2937 /* Substitute in the latest insn for the regs set by the earlier ones. */
2939 maxreg = max_reg_num ();
2941 subst_insn = i3;
2943 #ifndef HAVE_cc0
2944 /* Many machines that don't use CC0 have insns that can both perform an
2945 arithmetic operation and set the condition code. These operations will
2946 be represented as a PARALLEL with the first element of the vector
2947 being a COMPARE of an arithmetic operation with the constant zero.
2948 The second element of the vector will set some pseudo to the result
2949 of the same arithmetic operation. If we simplify the COMPARE, we won't
2950 match such a pattern and so will generate an extra insn. Here we test
2951 for this case, where both the comparison and the operation result are
2952 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2953 I2SRC. Later we will make the PARALLEL that contains I2. */
2955 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2956 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2957 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
2958 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2960 rtx newpat_dest;
2961 rtx *cc_use_loc = NULL;
2962 rtx_insn *cc_use_insn = NULL;
2963 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
2964 enum machine_mode compare_mode, orig_compare_mode;
2965 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
2967 newpat = PATTERN (i3);
2968 newpat_dest = SET_DEST (newpat);
2969 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
2971 if (undobuf.other_insn == 0
2972 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
2973 &cc_use_insn)))
2975 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
2976 compare_code = simplify_compare_const (compare_code,
2977 GET_MODE (i2dest), op0, &op1);
2978 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
2981 /* Do the rest only if op1 is const0_rtx, which may be the
2982 result of simplification. */
2983 if (op1 == const0_rtx)
2985 /* If a single use of the CC is found, prepare to modify it
2986 when SELECT_CC_MODE returns a new CC-class mode, or when
2987 the above simplify_compare_const() returned a new comparison
2988 operator. undobuf.other_insn is assigned the CC use insn
2989 when modifying it. */
2990 if (cc_use_loc)
2992 #ifdef SELECT_CC_MODE
2993 enum machine_mode new_mode
2994 = SELECT_CC_MODE (compare_code, op0, op1);
2995 if (new_mode != orig_compare_mode
2996 && can_change_dest_mode (SET_DEST (newpat),
2997 added_sets_2, new_mode))
2999 unsigned int regno = REGNO (newpat_dest);
3000 compare_mode = new_mode;
3001 if (regno < FIRST_PSEUDO_REGISTER)
3002 newpat_dest = gen_rtx_REG (compare_mode, regno);
3003 else
3005 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3006 newpat_dest = regno_reg_rtx[regno];
3009 #endif
3010 /* Cases for modifying the CC-using comparison. */
3011 if (compare_code != orig_compare_code
3012 /* ??? Do we need to verify the zero rtx? */
3013 && XEXP (*cc_use_loc, 1) == const0_rtx)
3015 /* Replace cc_use_loc with entire new RTX. */
3016 SUBST (*cc_use_loc,
3017 gen_rtx_fmt_ee (compare_code, compare_mode,
3018 newpat_dest, const0_rtx));
3019 undobuf.other_insn = cc_use_insn;
3021 else if (compare_mode != orig_compare_mode)
3023 /* Just replace the CC reg with a new mode. */
3024 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3025 undobuf.other_insn = cc_use_insn;
3029 /* Now we modify the current newpat:
3030 First, SET_DEST(newpat) is updated if the CC mode has been
3031 altered. For targets without SELECT_CC_MODE, this should be
3032 optimized away. */
3033 if (compare_mode != orig_compare_mode)
3034 SUBST (SET_DEST (newpat), newpat_dest);
3035 /* This is always done to propagate i2src into newpat. */
3036 SUBST (SET_SRC (newpat),
3037 gen_rtx_COMPARE (compare_mode, op0, op1));
3038 /* Create new version of i2pat if needed; the below PARALLEL
3039 creation needs this to work correctly. */
3040 if (! rtx_equal_p (i2src, op0))
3041 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3042 i2_is_used = 1;
3045 #endif
3047 if (i2_is_used == 0)
3049 /* It is possible that the source of I2 or I1 may be performing
3050 an unneeded operation, such as a ZERO_EXTEND of something
3051 that is known to have the high part zero. Handle that case
3052 by letting subst look at the inner insns.
3054 Another way to do this would be to have a function that tries
3055 to simplify a single insn instead of merging two or more
3056 insns. We don't do this because of the potential of infinite
3057 loops and because of the potential extra memory required.
3058 However, doing it the way we are is a bit of a kludge and
3059 doesn't catch all cases.
3061 But only do this if -fexpensive-optimizations since it slows
3062 things down and doesn't usually win.
3064 This is not done in the COMPARE case above because the
3065 unmodified I2PAT is used in the PARALLEL and so a pattern
3066 with a modified I2SRC would not match. */
3068 if (flag_expensive_optimizations)
3070 /* Pass pc_rtx so no substitutions are done, just
3071 simplifications. */
3072 if (i1)
3074 subst_low_luid = DF_INSN_LUID (i1);
3075 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3078 subst_low_luid = DF_INSN_LUID (i2);
3079 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3082 n_occurrences = 0; /* `subst' counts here */
3083 subst_low_luid = DF_INSN_LUID (i2);
3085 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3086 copy of I2SRC each time we substitute it, in order to avoid creating
3087 self-referential RTL when we will be substituting I1SRC for I1DEST
3088 later. Likewise if I0 feeds into I2, either directly or indirectly
3089 through I1, and I0DEST is in I0SRC. */
3090 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3091 (i1_feeds_i2_n && i1dest_in_i1src)
3092 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3093 && i0dest_in_i0src));
3094 substed_i2 = 1;
3096 /* Record whether I2's body now appears within I3's body. */
3097 i2_is_used = n_occurrences;
3100 /* If we already got a failure, don't try to do more. Otherwise, try to
3101 substitute I1 if we have it. */
3103 if (i1 && GET_CODE (newpat) != CLOBBER)
3105 /* Check that an autoincrement side-effect on I1 has not been lost.
3106 This happens if I1DEST is mentioned in I2 and dies there, and
3107 has disappeared from the new pattern. */
3108 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3109 && i1_feeds_i2_n
3110 && dead_or_set_p (i2, i1dest)
3111 && !reg_overlap_mentioned_p (i1dest, newpat))
3112 /* Before we can do this substitution, we must redo the test done
3113 above (see detailed comments there) that ensures I1DEST isn't
3114 mentioned in any SETs in NEWPAT that are field assignments. */
3115 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3116 0, 0, 0))
3118 undo_all ();
3119 return 0;
3122 n_occurrences = 0;
3123 subst_low_luid = DF_INSN_LUID (i1);
3125 /* If the following substitution will modify I1SRC, make a copy of it
3126 for the case where it is substituted for I1DEST in I2PAT later. */
3127 if (added_sets_2 && i1_feeds_i2_n)
3128 i1src_copy = copy_rtx (i1src);
3130 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3131 copy of I1SRC each time we substitute it, in order to avoid creating
3132 self-referential RTL when we will be substituting I0SRC for I0DEST
3133 later. */
3134 newpat = subst (newpat, i1dest, i1src, 0, 0,
3135 i0_feeds_i1_n && i0dest_in_i0src);
3136 substed_i1 = 1;
3138 /* Record whether I1's body now appears within I3's body. */
3139 i1_is_used = n_occurrences;
3142 /* Likewise for I0 if we have it. */
3144 if (i0 && GET_CODE (newpat) != CLOBBER)
3146 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3147 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3148 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3149 && !reg_overlap_mentioned_p (i0dest, newpat))
3150 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3151 0, 0, 0))
3153 undo_all ();
3154 return 0;
3157 /* If the following substitution will modify I0SRC, make a copy of it
3158 for the case where it is substituted for I0DEST in I1PAT later. */
3159 if (added_sets_1 && i0_feeds_i1_n)
3160 i0src_copy = copy_rtx (i0src);
3161 /* And a copy for I0DEST in I2PAT substitution. */
3162 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3163 || (i0_feeds_i2_n)))
3164 i0src_copy2 = copy_rtx (i0src);
3166 n_occurrences = 0;
3167 subst_low_luid = DF_INSN_LUID (i0);
3168 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3169 substed_i0 = 1;
3172 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3173 to count all the ways that I2SRC and I1SRC can be used. */
3174 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3175 && i2_is_used + added_sets_2 > 1)
3176 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3177 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3178 > 1))
3179 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3180 && (n_occurrences + added_sets_0
3181 + (added_sets_1 && i0_feeds_i1_n)
3182 + (added_sets_2 && i0_feeds_i2_n)
3183 > 1))
3184 /* Fail if we tried to make a new register. */
3185 || max_reg_num () != maxreg
3186 /* Fail if we couldn't do something and have a CLOBBER. */
3187 || GET_CODE (newpat) == CLOBBER
3188 /* Fail if this new pattern is a MULT and we didn't have one before
3189 at the outer level. */
3190 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3191 && ! have_mult))
3193 undo_all ();
3194 return 0;
3197 /* If the actions of the earlier insns must be kept
3198 in addition to substituting them into the latest one,
3199 we must make a new PARALLEL for the latest insn
3200 to hold additional the SETs. */
3202 if (added_sets_0 || added_sets_1 || added_sets_2)
3204 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3205 combine_extras++;
3207 if (GET_CODE (newpat) == PARALLEL)
3209 rtvec old = XVEC (newpat, 0);
3210 total_sets = XVECLEN (newpat, 0) + extra_sets;
3211 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3212 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3213 sizeof (old->elem[0]) * old->num_elem);
3215 else
3217 rtx old = newpat;
3218 total_sets = 1 + extra_sets;
3219 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3220 XVECEXP (newpat, 0, 0) = old;
3223 if (added_sets_0)
3224 XVECEXP (newpat, 0, --total_sets) = i0pat;
3226 if (added_sets_1)
3228 rtx t = i1pat;
3229 if (i0_feeds_i1_n)
3230 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3232 XVECEXP (newpat, 0, --total_sets) = t;
3234 if (added_sets_2)
3236 rtx t = i2pat;
3237 if (i1_feeds_i2_n)
3238 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3239 i0_feeds_i1_n && i0dest_in_i0src);
3240 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3241 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3243 XVECEXP (newpat, 0, --total_sets) = t;
3247 validate_replacement:
3249 /* Note which hard regs this insn has as inputs. */
3250 mark_used_regs_combine (newpat);
3252 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3253 consider splitting this pattern, we might need these clobbers. */
3254 if (i1 && GET_CODE (newpat) == PARALLEL
3255 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3257 int len = XVECLEN (newpat, 0);
3259 newpat_vec_with_clobbers = rtvec_alloc (len);
3260 for (i = 0; i < len; i++)
3261 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3264 /* Is the result of combination a valid instruction? */
3265 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3267 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3268 the second SET's destination is a register that is unused and isn't
3269 marked as an instruction that might trap in an EH region. In that case,
3270 we just need the first SET. This can occur when simplifying a divmod
3271 insn. We *must* test for this case here because the code below that
3272 splits two independent SETs doesn't handle this case correctly when it
3273 updates the register status.
3275 It's pointless doing this if we originally had two sets, one from
3276 i3, and one from i2. Combining then splitting the parallel results
3277 in the original i2 again plus an invalid insn (which we delete).
3278 The net effect is only to move instructions around, which makes
3279 debug info less accurate.
3281 Also check the case where the first SET's destination is unused.
3282 That would not cause incorrect code, but does cause an unneeded
3283 insn to remain. */
3285 if (insn_code_number < 0
3286 && !(added_sets_2 && i1 == 0)
3287 && GET_CODE (newpat) == PARALLEL
3288 && XVECLEN (newpat, 0) == 2
3289 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3290 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3291 && asm_noperands (newpat) < 0)
3293 rtx set0 = XVECEXP (newpat, 0, 0);
3294 rtx set1 = XVECEXP (newpat, 0, 1);
3296 if (((REG_P (SET_DEST (set1))
3297 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3298 || (GET_CODE (SET_DEST (set1)) == SUBREG
3299 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3300 && insn_nothrow_p (i3)
3301 && !side_effects_p (SET_SRC (set1)))
3303 newpat = set0;
3304 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3307 else if (((REG_P (SET_DEST (set0))
3308 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3309 || (GET_CODE (SET_DEST (set0)) == SUBREG
3310 && find_reg_note (i3, REG_UNUSED,
3311 SUBREG_REG (SET_DEST (set0)))))
3312 && insn_nothrow_p (i3)
3313 && !side_effects_p (SET_SRC (set0)))
3315 newpat = set1;
3316 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3318 if (insn_code_number >= 0)
3319 changed_i3_dest = 1;
3323 /* If we were combining three insns and the result is a simple SET
3324 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3325 insns. There are two ways to do this. It can be split using a
3326 machine-specific method (like when you have an addition of a large
3327 constant) or by combine in the function find_split_point. */
3329 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3330 && asm_noperands (newpat) < 0)
3332 rtx parallel, *split;
3333 rtx_insn *m_split_insn;
3335 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3336 use I2DEST as a scratch register will help. In the latter case,
3337 convert I2DEST to the mode of the source of NEWPAT if we can. */
3339 m_split_insn = combine_split_insns (newpat, i3);
3341 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3342 inputs of NEWPAT. */
3344 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3345 possible to try that as a scratch reg. This would require adding
3346 more code to make it work though. */
3348 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3350 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3352 /* First try to split using the original register as a
3353 scratch register. */
3354 parallel = gen_rtx_PARALLEL (VOIDmode,
3355 gen_rtvec (2, newpat,
3356 gen_rtx_CLOBBER (VOIDmode,
3357 i2dest)));
3358 m_split_insn = combine_split_insns (parallel, i3);
3360 /* If that didn't work, try changing the mode of I2DEST if
3361 we can. */
3362 if (m_split_insn == 0
3363 && new_mode != GET_MODE (i2dest)
3364 && new_mode != VOIDmode
3365 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3367 enum machine_mode old_mode = GET_MODE (i2dest);
3368 rtx ni2dest;
3370 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3371 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3372 else
3374 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3375 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3378 parallel = (gen_rtx_PARALLEL
3379 (VOIDmode,
3380 gen_rtvec (2, newpat,
3381 gen_rtx_CLOBBER (VOIDmode,
3382 ni2dest))));
3383 m_split_insn = combine_split_insns (parallel, i3);
3385 if (m_split_insn == 0
3386 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3388 struct undo *buf;
3390 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3391 buf = undobuf.undos;
3392 undobuf.undos = buf->next;
3393 buf->next = undobuf.frees;
3394 undobuf.frees = buf;
3398 i2scratch = m_split_insn != 0;
3401 /* If recog_for_combine has discarded clobbers, try to use them
3402 again for the split. */
3403 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3405 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3406 m_split_insn = combine_split_insns (parallel, i3);
3409 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3411 rtx m_split_pat = PATTERN (m_split_insn);
3412 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3413 if (insn_code_number >= 0)
3414 newpat = m_split_pat;
3416 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3417 && (next_nonnote_nondebug_insn (i2) == i3
3418 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3420 rtx i2set, i3set;
3421 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3422 newi2pat = PATTERN (m_split_insn);
3424 i3set = single_set (NEXT_INSN (m_split_insn));
3425 i2set = single_set (m_split_insn);
3427 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3429 /* If I2 or I3 has multiple SETs, we won't know how to track
3430 register status, so don't use these insns. If I2's destination
3431 is used between I2 and I3, we also can't use these insns. */
3433 if (i2_code_number >= 0 && i2set && i3set
3434 && (next_nonnote_nondebug_insn (i2) == i3
3435 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3436 insn_code_number = recog_for_combine (&newi3pat, i3,
3437 &new_i3_notes);
3438 if (insn_code_number >= 0)
3439 newpat = newi3pat;
3441 /* It is possible that both insns now set the destination of I3.
3442 If so, we must show an extra use of it. */
3444 if (insn_code_number >= 0)
3446 rtx new_i3_dest = SET_DEST (i3set);
3447 rtx new_i2_dest = SET_DEST (i2set);
3449 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3450 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3451 || GET_CODE (new_i3_dest) == SUBREG)
3452 new_i3_dest = XEXP (new_i3_dest, 0);
3454 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3455 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3456 || GET_CODE (new_i2_dest) == SUBREG)
3457 new_i2_dest = XEXP (new_i2_dest, 0);
3459 if (REG_P (new_i3_dest)
3460 && REG_P (new_i2_dest)
3461 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3462 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3466 /* If we can split it and use I2DEST, go ahead and see if that
3467 helps things be recognized. Verify that none of the registers
3468 are set between I2 and I3. */
3469 if (insn_code_number < 0
3470 && (split = find_split_point (&newpat, i3, false)) != 0
3471 #ifdef HAVE_cc0
3472 && REG_P (i2dest)
3473 #endif
3474 /* We need I2DEST in the proper mode. If it is a hard register
3475 or the only use of a pseudo, we can change its mode.
3476 Make sure we don't change a hard register to have a mode that
3477 isn't valid for it, or change the number of registers. */
3478 && (GET_MODE (*split) == GET_MODE (i2dest)
3479 || GET_MODE (*split) == VOIDmode
3480 || can_change_dest_mode (i2dest, added_sets_2,
3481 GET_MODE (*split)))
3482 && (next_nonnote_nondebug_insn (i2) == i3
3483 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3484 /* We can't overwrite I2DEST if its value is still used by
3485 NEWPAT. */
3486 && ! reg_referenced_p (i2dest, newpat))
3488 rtx newdest = i2dest;
3489 enum rtx_code split_code = GET_CODE (*split);
3490 enum machine_mode split_mode = GET_MODE (*split);
3491 bool subst_done = false;
3492 newi2pat = NULL_RTX;
3494 i2scratch = true;
3496 /* *SPLIT may be part of I2SRC, so make sure we have the
3497 original expression around for later debug processing.
3498 We should not need I2SRC any more in other cases. */
3499 if (MAY_HAVE_DEBUG_INSNS)
3500 i2src = copy_rtx (i2src);
3501 else
3502 i2src = NULL;
3504 /* Get NEWDEST as a register in the proper mode. We have already
3505 validated that we can do this. */
3506 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3508 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3509 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3510 else
3512 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3513 newdest = regno_reg_rtx[REGNO (i2dest)];
3517 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3518 an ASHIFT. This can occur if it was inside a PLUS and hence
3519 appeared to be a memory address. This is a kludge. */
3520 if (split_code == MULT
3521 && CONST_INT_P (XEXP (*split, 1))
3522 && INTVAL (XEXP (*split, 1)) > 0
3523 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3525 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3526 XEXP (*split, 0), GEN_INT (i)));
3527 /* Update split_code because we may not have a multiply
3528 anymore. */
3529 split_code = GET_CODE (*split);
3532 #ifdef INSN_SCHEDULING
3533 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3534 be written as a ZERO_EXTEND. */
3535 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3537 #ifdef LOAD_EXTEND_OP
3538 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3539 what it really is. */
3540 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3541 == SIGN_EXTEND)
3542 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3543 SUBREG_REG (*split)));
3544 else
3545 #endif
3546 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3547 SUBREG_REG (*split)));
3549 #endif
3551 /* Attempt to split binary operators using arithmetic identities. */
3552 if (BINARY_P (SET_SRC (newpat))
3553 && split_mode == GET_MODE (SET_SRC (newpat))
3554 && ! side_effects_p (SET_SRC (newpat)))
3556 rtx setsrc = SET_SRC (newpat);
3557 enum machine_mode mode = GET_MODE (setsrc);
3558 enum rtx_code code = GET_CODE (setsrc);
3559 rtx src_op0 = XEXP (setsrc, 0);
3560 rtx src_op1 = XEXP (setsrc, 1);
3562 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3563 if (rtx_equal_p (src_op0, src_op1))
3565 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3566 SUBST (XEXP (setsrc, 0), newdest);
3567 SUBST (XEXP (setsrc, 1), newdest);
3568 subst_done = true;
3570 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3571 else if ((code == PLUS || code == MULT)
3572 && GET_CODE (src_op0) == code
3573 && GET_CODE (XEXP (src_op0, 0)) == code
3574 && (INTEGRAL_MODE_P (mode)
3575 || (FLOAT_MODE_P (mode)
3576 && flag_unsafe_math_optimizations)))
3578 rtx p = XEXP (XEXP (src_op0, 0), 0);
3579 rtx q = XEXP (XEXP (src_op0, 0), 1);
3580 rtx r = XEXP (src_op0, 1);
3581 rtx s = src_op1;
3583 /* Split both "((X op Y) op X) op Y" and
3584 "((X op Y) op Y) op X" as "T op T" where T is
3585 "X op Y". */
3586 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3587 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3589 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3590 XEXP (src_op0, 0));
3591 SUBST (XEXP (setsrc, 0), newdest);
3592 SUBST (XEXP (setsrc, 1), newdest);
3593 subst_done = true;
3595 /* Split "((X op X) op Y) op Y)" as "T op T" where
3596 T is "X op Y". */
3597 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3599 rtx tmp = simplify_gen_binary (code, mode, p, r);
3600 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3601 SUBST (XEXP (setsrc, 0), newdest);
3602 SUBST (XEXP (setsrc, 1), newdest);
3603 subst_done = true;
3608 if (!subst_done)
3610 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3611 SUBST (*split, newdest);
3614 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3616 /* recog_for_combine might have added CLOBBERs to newi2pat.
3617 Make sure NEWPAT does not depend on the clobbered regs. */
3618 if (GET_CODE (newi2pat) == PARALLEL)
3619 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3620 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3622 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3623 if (reg_overlap_mentioned_p (reg, newpat))
3625 undo_all ();
3626 return 0;
3630 /* If the split point was a MULT and we didn't have one before,
3631 don't use one now. */
3632 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3633 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3637 /* Check for a case where we loaded from memory in a narrow mode and
3638 then sign extended it, but we need both registers. In that case,
3639 we have a PARALLEL with both loads from the same memory location.
3640 We can split this into a load from memory followed by a register-register
3641 copy. This saves at least one insn, more if register allocation can
3642 eliminate the copy.
3644 We cannot do this if the destination of the first assignment is a
3645 condition code register or cc0. We eliminate this case by making sure
3646 the SET_DEST and SET_SRC have the same mode.
3648 We cannot do this if the destination of the second assignment is
3649 a register that we have already assumed is zero-extended. Similarly
3650 for a SUBREG of such a register. */
3652 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3653 && GET_CODE (newpat) == PARALLEL
3654 && XVECLEN (newpat, 0) == 2
3655 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3656 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3657 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3658 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3659 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3660 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3661 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3662 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3663 DF_INSN_LUID (i2))
3664 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3665 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3666 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3667 (REG_P (temp_expr)
3668 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3669 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3670 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3671 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3672 != GET_MODE_MASK (word_mode))))
3673 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3674 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3675 (REG_P (temp_expr)
3676 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3677 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3678 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3679 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3680 != GET_MODE_MASK (word_mode)))))
3681 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3682 SET_SRC (XVECEXP (newpat, 0, 1)))
3683 && ! find_reg_note (i3, REG_UNUSED,
3684 SET_DEST (XVECEXP (newpat, 0, 0))))
3686 rtx ni2dest;
3688 newi2pat = XVECEXP (newpat, 0, 0);
3689 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3690 newpat = XVECEXP (newpat, 0, 1);
3691 SUBST (SET_SRC (newpat),
3692 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3693 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3695 if (i2_code_number >= 0)
3696 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3698 if (insn_code_number >= 0)
3699 swap_i2i3 = 1;
3702 /* Similarly, check for a case where we have a PARALLEL of two independent
3703 SETs but we started with three insns. In this case, we can do the sets
3704 as two separate insns. This case occurs when some SET allows two
3705 other insns to combine, but the destination of that SET is still live. */
3707 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3708 && GET_CODE (newpat) == PARALLEL
3709 && XVECLEN (newpat, 0) == 2
3710 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3711 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3712 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3713 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3714 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3715 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3716 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3717 XVECEXP (newpat, 0, 0))
3718 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3719 XVECEXP (newpat, 0, 1))
3720 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3721 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3723 rtx set0 = XVECEXP (newpat, 0, 0);
3724 rtx set1 = XVECEXP (newpat, 0, 1);
3726 /* Normally, it doesn't matter which of the two is done first,
3727 but the one that references cc0 can't be the second, and
3728 one which uses any regs/memory set in between i2 and i3 can't
3729 be first. The PARALLEL might also have been pre-existing in i3,
3730 so we need to make sure that we won't wrongly hoist a SET to i2
3731 that would conflict with a death note present in there. */
3732 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3733 && !(REG_P (SET_DEST (set1))
3734 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3735 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3736 && find_reg_note (i2, REG_DEAD,
3737 SUBREG_REG (SET_DEST (set1))))
3738 #ifdef HAVE_cc0
3739 && !reg_referenced_p (cc0_rtx, set0)
3740 #endif
3741 /* If I3 is a jump, ensure that set0 is a jump so that
3742 we do not create invalid RTL. */
3743 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3746 newi2pat = set1;
3747 newpat = set0;
3749 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3750 && !(REG_P (SET_DEST (set0))
3751 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3752 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3753 && find_reg_note (i2, REG_DEAD,
3754 SUBREG_REG (SET_DEST (set0))))
3755 #ifdef HAVE_cc0
3756 && !reg_referenced_p (cc0_rtx, set1)
3757 #endif
3758 /* If I3 is a jump, ensure that set1 is a jump so that
3759 we do not create invalid RTL. */
3760 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3763 newi2pat = set0;
3764 newpat = set1;
3766 else
3768 undo_all ();
3769 return 0;
3772 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3774 if (i2_code_number >= 0)
3776 /* recog_for_combine might have added CLOBBERs to newi2pat.
3777 Make sure NEWPAT does not depend on the clobbered regs. */
3778 if (GET_CODE (newi2pat) == PARALLEL)
3780 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3781 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3783 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3784 if (reg_overlap_mentioned_p (reg, newpat))
3786 undo_all ();
3787 return 0;
3792 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3796 /* If it still isn't recognized, fail and change things back the way they
3797 were. */
3798 if ((insn_code_number < 0
3799 /* Is the result a reasonable ASM_OPERANDS? */
3800 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3802 undo_all ();
3803 return 0;
3806 /* If we had to change another insn, make sure it is valid also. */
3807 if (undobuf.other_insn)
3809 CLEAR_HARD_REG_SET (newpat_used_regs);
3811 other_pat = PATTERN (undobuf.other_insn);
3812 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3813 &new_other_notes);
3815 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3817 undo_all ();
3818 return 0;
3822 #ifdef HAVE_cc0
3823 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3824 they are adjacent to each other or not. */
3826 rtx_insn *p = prev_nonnote_insn (i3);
3827 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3828 && sets_cc0_p (newi2pat))
3830 undo_all ();
3831 return 0;
3834 #endif
3836 /* Only allow this combination if insn_rtx_costs reports that the
3837 replacement instructions are cheaper than the originals. */
3838 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3840 undo_all ();
3841 return 0;
3844 if (MAY_HAVE_DEBUG_INSNS)
3846 struct undo *undo;
3848 for (undo = undobuf.undos; undo; undo = undo->next)
3849 if (undo->kind == UNDO_MODE)
3851 rtx reg = *undo->where.r;
3852 enum machine_mode new_mode = GET_MODE (reg);
3853 enum machine_mode old_mode = undo->old_contents.m;
3855 /* Temporarily revert mode back. */
3856 adjust_reg_mode (reg, old_mode);
3858 if (reg == i2dest && i2scratch)
3860 /* If we used i2dest as a scratch register with a
3861 different mode, substitute it for the original
3862 i2src while its original mode is temporarily
3863 restored, and then clear i2scratch so that we don't
3864 do it again later. */
3865 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3866 this_basic_block);
3867 i2scratch = false;
3868 /* Put back the new mode. */
3869 adjust_reg_mode (reg, new_mode);
3871 else
3873 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3874 rtx_insn *first, *last;
3876 if (reg == i2dest)
3878 first = i2;
3879 last = last_combined_insn;
3881 else
3883 first = i3;
3884 last = undobuf.other_insn;
3885 gcc_assert (last);
3886 if (DF_INSN_LUID (last)
3887 < DF_INSN_LUID (last_combined_insn))
3888 last = last_combined_insn;
3891 /* We're dealing with a reg that changed mode but not
3892 meaning, so we want to turn it into a subreg for
3893 the new mode. However, because of REG sharing and
3894 because its mode had already changed, we have to do
3895 it in two steps. First, replace any debug uses of
3896 reg, with its original mode temporarily restored,
3897 with this copy we have created; then, replace the
3898 copy with the SUBREG of the original shared reg,
3899 once again changed to the new mode. */
3900 propagate_for_debug (first, last, reg, tempreg,
3901 this_basic_block);
3902 adjust_reg_mode (reg, new_mode);
3903 propagate_for_debug (first, last, tempreg,
3904 lowpart_subreg (old_mode, reg, new_mode),
3905 this_basic_block);
3910 /* If we will be able to accept this, we have made a
3911 change to the destination of I3. This requires us to
3912 do a few adjustments. */
3914 if (changed_i3_dest)
3916 PATTERN (i3) = newpat;
3917 adjust_for_new_dest (i3);
3920 /* We now know that we can do this combination. Merge the insns and
3921 update the status of registers and LOG_LINKS. */
3923 if (undobuf.other_insn)
3925 rtx note, next;
3927 PATTERN (undobuf.other_insn) = other_pat;
3929 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
3930 ensure that they are still valid. Then add any non-duplicate
3931 notes added by recog_for_combine. */
3932 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3934 next = XEXP (note, 1);
3936 if ((REG_NOTE_KIND (note) == REG_DEAD
3937 && !reg_referenced_p (XEXP (note, 0),
3938 PATTERN (undobuf.other_insn)))
3939 ||(REG_NOTE_KIND (note) == REG_UNUSED
3940 && !reg_set_p (XEXP (note, 0),
3941 PATTERN (undobuf.other_insn))))
3942 remove_note (undobuf.other_insn, note);
3945 distribute_notes (new_other_notes, undobuf.other_insn,
3946 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
3947 NULL_RTX);
3950 if (swap_i2i3)
3952 rtx_insn *insn;
3953 struct insn_link *link;
3954 rtx ni2dest;
3956 /* I3 now uses what used to be its destination and which is now
3957 I2's destination. This requires us to do a few adjustments. */
3958 PATTERN (i3) = newpat;
3959 adjust_for_new_dest (i3);
3961 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3962 so we still will.
3964 However, some later insn might be using I2's dest and have
3965 a LOG_LINK pointing at I3. We must remove this link.
3966 The simplest way to remove the link is to point it at I1,
3967 which we know will be a NOTE. */
3969 /* newi2pat is usually a SET here; however, recog_for_combine might
3970 have added some clobbers. */
3971 if (GET_CODE (newi2pat) == PARALLEL)
3972 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3973 else
3974 ni2dest = SET_DEST (newi2pat);
3976 for (insn = NEXT_INSN (i3);
3977 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
3978 || insn != BB_HEAD (this_basic_block->next_bb));
3979 insn = NEXT_INSN (insn))
3981 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3983 FOR_EACH_LOG_LINK (link, insn)
3984 if (link->insn == i3)
3985 link->insn = i1;
3987 break;
3993 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3994 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
3995 rtx midnotes = 0;
3996 int from_luid;
3997 /* Compute which registers we expect to eliminate. newi2pat may be setting
3998 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3999 same as i3dest, in which case newi2pat may be setting i1dest. */
4000 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4001 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4002 || !i2dest_killed
4003 ? 0 : i2dest);
4004 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4005 || (newi2pat && reg_set_p (i1dest, newi2pat))
4006 || !i1dest_killed
4007 ? 0 : i1dest);
4008 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4009 || (newi2pat && reg_set_p (i0dest, newi2pat))
4010 || !i0dest_killed
4011 ? 0 : i0dest);
4013 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4014 clear them. */
4015 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4016 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4017 if (i1)
4018 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4019 if (i0)
4020 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4022 /* Ensure that we do not have something that should not be shared but
4023 occurs multiple times in the new insns. Check this by first
4024 resetting all the `used' flags and then copying anything is shared. */
4026 reset_used_flags (i3notes);
4027 reset_used_flags (i2notes);
4028 reset_used_flags (i1notes);
4029 reset_used_flags (i0notes);
4030 reset_used_flags (newpat);
4031 reset_used_flags (newi2pat);
4032 if (undobuf.other_insn)
4033 reset_used_flags (PATTERN (undobuf.other_insn));
4035 i3notes = copy_rtx_if_shared (i3notes);
4036 i2notes = copy_rtx_if_shared (i2notes);
4037 i1notes = copy_rtx_if_shared (i1notes);
4038 i0notes = copy_rtx_if_shared (i0notes);
4039 newpat = copy_rtx_if_shared (newpat);
4040 newi2pat = copy_rtx_if_shared (newi2pat);
4041 if (undobuf.other_insn)
4042 reset_used_flags (PATTERN (undobuf.other_insn));
4044 INSN_CODE (i3) = insn_code_number;
4045 PATTERN (i3) = newpat;
4047 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4049 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4051 reset_used_flags (call_usage);
4052 call_usage = copy_rtx (call_usage);
4054 if (substed_i2)
4056 /* I2SRC must still be meaningful at this point. Some splitting
4057 operations can invalidate I2SRC, but those operations do not
4058 apply to calls. */
4059 gcc_assert (i2src);
4060 replace_rtx (call_usage, i2dest, i2src);
4063 if (substed_i1)
4064 replace_rtx (call_usage, i1dest, i1src);
4065 if (substed_i0)
4066 replace_rtx (call_usage, i0dest, i0src);
4068 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4071 if (undobuf.other_insn)
4072 INSN_CODE (undobuf.other_insn) = other_code_number;
4074 /* We had one special case above where I2 had more than one set and
4075 we replaced a destination of one of those sets with the destination
4076 of I3. In that case, we have to update LOG_LINKS of insns later
4077 in this basic block. Note that this (expensive) case is rare.
4079 Also, in this case, we must pretend that all REG_NOTEs for I2
4080 actually came from I3, so that REG_UNUSED notes from I2 will be
4081 properly handled. */
4083 if (i3_subst_into_i2)
4085 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4086 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4087 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4088 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4089 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4090 && ! find_reg_note (i2, REG_UNUSED,
4091 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4092 for (temp_insn = NEXT_INSN (i2);
4093 temp_insn
4094 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4095 || BB_HEAD (this_basic_block) != temp_insn);
4096 temp_insn = NEXT_INSN (temp_insn))
4097 if (temp_insn != i3 && INSN_P (temp_insn))
4098 FOR_EACH_LOG_LINK (link, temp_insn)
4099 if (link->insn == i2)
4100 link->insn = i3;
4102 if (i3notes)
4104 rtx link = i3notes;
4105 while (XEXP (link, 1))
4106 link = XEXP (link, 1);
4107 XEXP (link, 1) = i2notes;
4109 else
4110 i3notes = i2notes;
4111 i2notes = 0;
4114 LOG_LINKS (i3) = NULL;
4115 REG_NOTES (i3) = 0;
4116 LOG_LINKS (i2) = NULL;
4117 REG_NOTES (i2) = 0;
4119 if (newi2pat)
4121 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4122 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4123 this_basic_block);
4124 INSN_CODE (i2) = i2_code_number;
4125 PATTERN (i2) = newi2pat;
4127 else
4129 if (MAY_HAVE_DEBUG_INSNS && i2src)
4130 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4131 this_basic_block);
4132 SET_INSN_DELETED (i2);
4135 if (i1)
4137 LOG_LINKS (i1) = NULL;
4138 REG_NOTES (i1) = 0;
4139 if (MAY_HAVE_DEBUG_INSNS)
4140 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4141 this_basic_block);
4142 SET_INSN_DELETED (i1);
4145 if (i0)
4147 LOG_LINKS (i0) = NULL;
4148 REG_NOTES (i0) = 0;
4149 if (MAY_HAVE_DEBUG_INSNS)
4150 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4151 this_basic_block);
4152 SET_INSN_DELETED (i0);
4155 /* Get death notes for everything that is now used in either I3 or
4156 I2 and used to die in a previous insn. If we built two new
4157 patterns, move from I1 to I2 then I2 to I3 so that we get the
4158 proper movement on registers that I2 modifies. */
4160 if (i0)
4161 from_luid = DF_INSN_LUID (i0);
4162 else if (i1)
4163 from_luid = DF_INSN_LUID (i1);
4164 else
4165 from_luid = DF_INSN_LUID (i2);
4166 if (newi2pat)
4167 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4168 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4170 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4171 if (i3notes)
4172 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4173 elim_i2, elim_i1, elim_i0);
4174 if (i2notes)
4175 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4176 elim_i2, elim_i1, elim_i0);
4177 if (i1notes)
4178 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4179 elim_i2, elim_i1, elim_i0);
4180 if (i0notes)
4181 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4182 elim_i2, elim_i1, elim_i0);
4183 if (midnotes)
4184 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4185 elim_i2, elim_i1, elim_i0);
4187 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4188 know these are REG_UNUSED and want them to go to the desired insn,
4189 so we always pass it as i3. */
4191 if (newi2pat && new_i2_notes)
4192 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4193 NULL_RTX);
4195 if (new_i3_notes)
4196 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4197 NULL_RTX);
4199 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4200 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4201 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4202 in that case, it might delete I2. Similarly for I2 and I1.
4203 Show an additional death due to the REG_DEAD note we make here. If
4204 we discard it in distribute_notes, we will decrement it again. */
4206 if (i3dest_killed)
4208 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4209 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4210 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4211 elim_i1, elim_i0);
4212 else
4213 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4214 elim_i2, elim_i1, elim_i0);
4217 if (i2dest_in_i2src)
4219 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4220 if (newi2pat && reg_set_p (i2dest, newi2pat))
4221 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4222 NULL_RTX, NULL_RTX);
4223 else
4224 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4225 NULL_RTX, NULL_RTX, NULL_RTX);
4228 if (i1dest_in_i1src)
4230 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4231 if (newi2pat && reg_set_p (i1dest, newi2pat))
4232 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4233 NULL_RTX, NULL_RTX);
4234 else
4235 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4236 NULL_RTX, NULL_RTX, NULL_RTX);
4239 if (i0dest_in_i0src)
4241 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4242 if (newi2pat && reg_set_p (i0dest, newi2pat))
4243 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4244 NULL_RTX, NULL_RTX);
4245 else
4246 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4247 NULL_RTX, NULL_RTX, NULL_RTX);
4250 distribute_links (i3links);
4251 distribute_links (i2links);
4252 distribute_links (i1links);
4253 distribute_links (i0links);
4255 if (REG_P (i2dest))
4257 struct insn_link *link;
4258 rtx_insn *i2_insn = 0;
4259 rtx i2_val = 0, set;
4261 /* The insn that used to set this register doesn't exist, and
4262 this life of the register may not exist either. See if one of
4263 I3's links points to an insn that sets I2DEST. If it does,
4264 that is now the last known value for I2DEST. If we don't update
4265 this and I2 set the register to a value that depended on its old
4266 contents, we will get confused. If this insn is used, thing
4267 will be set correctly in combine_instructions. */
4268 FOR_EACH_LOG_LINK (link, i3)
4269 if ((set = single_set (link->insn)) != 0
4270 && rtx_equal_p (i2dest, SET_DEST (set)))
4271 i2_insn = link->insn, i2_val = SET_SRC (set);
4273 record_value_for_reg (i2dest, i2_insn, i2_val);
4275 /* If the reg formerly set in I2 died only once and that was in I3,
4276 zero its use count so it won't make `reload' do any work. */
4277 if (! added_sets_2
4278 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4279 && ! i2dest_in_i2src)
4280 INC_REG_N_SETS (REGNO (i2dest), -1);
4283 if (i1 && REG_P (i1dest))
4285 struct insn_link *link;
4286 rtx_insn *i1_insn = 0;
4287 rtx i1_val = 0, set;
4289 FOR_EACH_LOG_LINK (link, i3)
4290 if ((set = single_set (link->insn)) != 0
4291 && rtx_equal_p (i1dest, SET_DEST (set)))
4292 i1_insn = link->insn, i1_val = SET_SRC (set);
4294 record_value_for_reg (i1dest, i1_insn, i1_val);
4296 if (! added_sets_1 && ! i1dest_in_i1src)
4297 INC_REG_N_SETS (REGNO (i1dest), -1);
4300 if (i0 && REG_P (i0dest))
4302 struct insn_link *link;
4303 rtx_insn *i0_insn = 0;
4304 rtx i0_val = 0, set;
4306 FOR_EACH_LOG_LINK (link, i3)
4307 if ((set = single_set (link->insn)) != 0
4308 && rtx_equal_p (i0dest, SET_DEST (set)))
4309 i0_insn = link->insn, i0_val = SET_SRC (set);
4311 record_value_for_reg (i0dest, i0_insn, i0_val);
4313 if (! added_sets_0 && ! i0dest_in_i0src)
4314 INC_REG_N_SETS (REGNO (i0dest), -1);
4317 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4318 been made to this insn. The order is important, because newi2pat
4319 can affect nonzero_bits of newpat. */
4320 if (newi2pat)
4321 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4322 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4325 if (undobuf.other_insn != NULL_RTX)
4327 if (dump_file)
4329 fprintf (dump_file, "modifying other_insn ");
4330 dump_insn_slim (dump_file, undobuf.other_insn);
4332 df_insn_rescan (undobuf.other_insn);
4335 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4337 if (dump_file)
4339 fprintf (dump_file, "modifying insn i0 ");
4340 dump_insn_slim (dump_file, i0);
4342 df_insn_rescan (i0);
4345 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4347 if (dump_file)
4349 fprintf (dump_file, "modifying insn i1 ");
4350 dump_insn_slim (dump_file, i1);
4352 df_insn_rescan (i1);
4355 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4357 if (dump_file)
4359 fprintf (dump_file, "modifying insn i2 ");
4360 dump_insn_slim (dump_file, i2);
4362 df_insn_rescan (i2);
4365 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4367 if (dump_file)
4369 fprintf (dump_file, "modifying insn i3 ");
4370 dump_insn_slim (dump_file, i3);
4372 df_insn_rescan (i3);
4375 /* Set new_direct_jump_p if a new return or simple jump instruction
4376 has been created. Adjust the CFG accordingly. */
4377 if (returnjump_p (i3) || any_uncondjump_p (i3))
4379 *new_direct_jump_p = 1;
4380 mark_jump_label (PATTERN (i3), i3, 0);
4381 update_cfg_for_uncondjump (i3);
4384 if (undobuf.other_insn != NULL_RTX
4385 && (returnjump_p (undobuf.other_insn)
4386 || any_uncondjump_p (undobuf.other_insn)))
4388 *new_direct_jump_p = 1;
4389 update_cfg_for_uncondjump (undobuf.other_insn);
4392 /* A noop might also need cleaning up of CFG, if it comes from the
4393 simplification of a jump. */
4394 if (JUMP_P (i3)
4395 && GET_CODE (newpat) == SET
4396 && SET_SRC (newpat) == pc_rtx
4397 && SET_DEST (newpat) == pc_rtx)
4399 *new_direct_jump_p = 1;
4400 update_cfg_for_uncondjump (i3);
4403 if (undobuf.other_insn != NULL_RTX
4404 && JUMP_P (undobuf.other_insn)
4405 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4406 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4407 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4409 *new_direct_jump_p = 1;
4410 update_cfg_for_uncondjump (undobuf.other_insn);
4413 combine_successes++;
4414 undo_commit ();
4416 if (added_links_insn
4417 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4418 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4419 return added_links_insn;
4420 else
4421 return newi2pat ? i2 : i3;
4424 /* Undo all the modifications recorded in undobuf. */
4426 static void
4427 undo_all (void)
4429 struct undo *undo, *next;
4431 for (undo = undobuf.undos; undo; undo = next)
4433 next = undo->next;
4434 switch (undo->kind)
4436 case UNDO_RTX:
4437 *undo->where.r = undo->old_contents.r;
4438 break;
4439 case UNDO_INT:
4440 *undo->where.i = undo->old_contents.i;
4441 break;
4442 case UNDO_MODE:
4443 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4444 break;
4445 case UNDO_LINKS:
4446 *undo->where.l = undo->old_contents.l;
4447 break;
4448 default:
4449 gcc_unreachable ();
4452 undo->next = undobuf.frees;
4453 undobuf.frees = undo;
4456 undobuf.undos = 0;
4459 /* We've committed to accepting the changes we made. Move all
4460 of the undos to the free list. */
4462 static void
4463 undo_commit (void)
4465 struct undo *undo, *next;
4467 for (undo = undobuf.undos; undo; undo = next)
4469 next = undo->next;
4470 undo->next = undobuf.frees;
4471 undobuf.frees = undo;
4473 undobuf.undos = 0;
4476 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4477 where we have an arithmetic expression and return that point. LOC will
4478 be inside INSN.
4480 try_combine will call this function to see if an insn can be split into
4481 two insns. */
4483 static rtx *
4484 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4486 rtx x = *loc;
4487 enum rtx_code code = GET_CODE (x);
4488 rtx *split;
4489 unsigned HOST_WIDE_INT len = 0;
4490 HOST_WIDE_INT pos = 0;
4491 int unsignedp = 0;
4492 rtx inner = NULL_RTX;
4494 /* First special-case some codes. */
4495 switch (code)
4497 case SUBREG:
4498 #ifdef INSN_SCHEDULING
4499 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4500 point. */
4501 if (MEM_P (SUBREG_REG (x)))
4502 return loc;
4503 #endif
4504 return find_split_point (&SUBREG_REG (x), insn, false);
4506 case MEM:
4507 #ifdef HAVE_lo_sum
4508 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4509 using LO_SUM and HIGH. */
4510 if (GET_CODE (XEXP (x, 0)) == CONST
4511 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4513 enum machine_mode address_mode = get_address_mode (x);
4515 SUBST (XEXP (x, 0),
4516 gen_rtx_LO_SUM (address_mode,
4517 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4518 XEXP (x, 0)));
4519 return &XEXP (XEXP (x, 0), 0);
4521 #endif
4523 /* If we have a PLUS whose second operand is a constant and the
4524 address is not valid, perhaps will can split it up using
4525 the machine-specific way to split large constants. We use
4526 the first pseudo-reg (one of the virtual regs) as a placeholder;
4527 it will not remain in the result. */
4528 if (GET_CODE (XEXP (x, 0)) == PLUS
4529 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4530 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4531 MEM_ADDR_SPACE (x)))
4533 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4534 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4535 XEXP (x, 0)),
4536 subst_insn);
4538 /* This should have produced two insns, each of which sets our
4539 placeholder. If the source of the second is a valid address,
4540 we can make put both sources together and make a split point
4541 in the middle. */
4543 if (seq
4544 && NEXT_INSN (seq) != NULL_RTX
4545 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4546 && NONJUMP_INSN_P (seq)
4547 && GET_CODE (PATTERN (seq)) == SET
4548 && SET_DEST (PATTERN (seq)) == reg
4549 && ! reg_mentioned_p (reg,
4550 SET_SRC (PATTERN (seq)))
4551 && NONJUMP_INSN_P (NEXT_INSN (seq))
4552 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4553 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4554 && memory_address_addr_space_p
4555 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4556 MEM_ADDR_SPACE (x)))
4558 rtx src1 = SET_SRC (PATTERN (seq));
4559 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4561 /* Replace the placeholder in SRC2 with SRC1. If we can
4562 find where in SRC2 it was placed, that can become our
4563 split point and we can replace this address with SRC2.
4564 Just try two obvious places. */
4566 src2 = replace_rtx (src2, reg, src1);
4567 split = 0;
4568 if (XEXP (src2, 0) == src1)
4569 split = &XEXP (src2, 0);
4570 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4571 && XEXP (XEXP (src2, 0), 0) == src1)
4572 split = &XEXP (XEXP (src2, 0), 0);
4574 if (split)
4576 SUBST (XEXP (x, 0), src2);
4577 return split;
4581 /* If that didn't work, perhaps the first operand is complex and
4582 needs to be computed separately, so make a split point there.
4583 This will occur on machines that just support REG + CONST
4584 and have a constant moved through some previous computation. */
4586 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4587 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4588 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4589 return &XEXP (XEXP (x, 0), 0);
4592 /* If we have a PLUS whose first operand is complex, try computing it
4593 separately by making a split there. */
4594 if (GET_CODE (XEXP (x, 0)) == PLUS
4595 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4596 MEM_ADDR_SPACE (x))
4597 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4598 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4599 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4600 return &XEXP (XEXP (x, 0), 0);
4601 break;
4603 case SET:
4604 #ifdef HAVE_cc0
4605 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4606 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4607 we need to put the operand into a register. So split at that
4608 point. */
4610 if (SET_DEST (x) == cc0_rtx
4611 && GET_CODE (SET_SRC (x)) != COMPARE
4612 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4613 && !OBJECT_P (SET_SRC (x))
4614 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4615 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4616 return &SET_SRC (x);
4617 #endif
4619 /* See if we can split SET_SRC as it stands. */
4620 split = find_split_point (&SET_SRC (x), insn, true);
4621 if (split && split != &SET_SRC (x))
4622 return split;
4624 /* See if we can split SET_DEST as it stands. */
4625 split = find_split_point (&SET_DEST (x), insn, false);
4626 if (split && split != &SET_DEST (x))
4627 return split;
4629 /* See if this is a bitfield assignment with everything constant. If
4630 so, this is an IOR of an AND, so split it into that. */
4631 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4632 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4633 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4634 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4635 && CONST_INT_P (SET_SRC (x))
4636 && ((INTVAL (XEXP (SET_DEST (x), 1))
4637 + INTVAL (XEXP (SET_DEST (x), 2)))
4638 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4639 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4641 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4642 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4643 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4644 rtx dest = XEXP (SET_DEST (x), 0);
4645 enum machine_mode mode = GET_MODE (dest);
4646 unsigned HOST_WIDE_INT mask
4647 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4648 rtx or_mask;
4650 if (BITS_BIG_ENDIAN)
4651 pos = GET_MODE_PRECISION (mode) - len - pos;
4653 or_mask = gen_int_mode (src << pos, mode);
4654 if (src == mask)
4655 SUBST (SET_SRC (x),
4656 simplify_gen_binary (IOR, mode, dest, or_mask));
4657 else
4659 rtx negmask = gen_int_mode (~(mask << pos), mode);
4660 SUBST (SET_SRC (x),
4661 simplify_gen_binary (IOR, mode,
4662 simplify_gen_binary (AND, mode,
4663 dest, negmask),
4664 or_mask));
4667 SUBST (SET_DEST (x), dest);
4669 split = find_split_point (&SET_SRC (x), insn, true);
4670 if (split && split != &SET_SRC (x))
4671 return split;
4674 /* Otherwise, see if this is an operation that we can split into two.
4675 If so, try to split that. */
4676 code = GET_CODE (SET_SRC (x));
4678 switch (code)
4680 case AND:
4681 /* If we are AND'ing with a large constant that is only a single
4682 bit and the result is only being used in a context where we
4683 need to know if it is zero or nonzero, replace it with a bit
4684 extraction. This will avoid the large constant, which might
4685 have taken more than one insn to make. If the constant were
4686 not a valid argument to the AND but took only one insn to make,
4687 this is no worse, but if it took more than one insn, it will
4688 be better. */
4690 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4691 && REG_P (XEXP (SET_SRC (x), 0))
4692 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4693 && REG_P (SET_DEST (x))
4694 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4695 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4696 && XEXP (*split, 0) == SET_DEST (x)
4697 && XEXP (*split, 1) == const0_rtx)
4699 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4700 XEXP (SET_SRC (x), 0),
4701 pos, NULL_RTX, 1, 1, 0, 0);
4702 if (extraction != 0)
4704 SUBST (SET_SRC (x), extraction);
4705 return find_split_point (loc, insn, false);
4708 break;
4710 case NE:
4711 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4712 is known to be on, this can be converted into a NEG of a shift. */
4713 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4714 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4715 && 1 <= (pos = exact_log2
4716 (nonzero_bits (XEXP (SET_SRC (x), 0),
4717 GET_MODE (XEXP (SET_SRC (x), 0))))))
4719 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4721 SUBST (SET_SRC (x),
4722 gen_rtx_NEG (mode,
4723 gen_rtx_LSHIFTRT (mode,
4724 XEXP (SET_SRC (x), 0),
4725 GEN_INT (pos))));
4727 split = find_split_point (&SET_SRC (x), insn, true);
4728 if (split && split != &SET_SRC (x))
4729 return split;
4731 break;
4733 case SIGN_EXTEND:
4734 inner = XEXP (SET_SRC (x), 0);
4736 /* We can't optimize if either mode is a partial integer
4737 mode as we don't know how many bits are significant
4738 in those modes. */
4739 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4740 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4741 break;
4743 pos = 0;
4744 len = GET_MODE_PRECISION (GET_MODE (inner));
4745 unsignedp = 0;
4746 break;
4748 case SIGN_EXTRACT:
4749 case ZERO_EXTRACT:
4750 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4751 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4753 inner = XEXP (SET_SRC (x), 0);
4754 len = INTVAL (XEXP (SET_SRC (x), 1));
4755 pos = INTVAL (XEXP (SET_SRC (x), 2));
4757 if (BITS_BIG_ENDIAN)
4758 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4759 unsignedp = (code == ZERO_EXTRACT);
4761 break;
4763 default:
4764 break;
4767 if (len && pos >= 0
4768 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4770 enum machine_mode mode = GET_MODE (SET_SRC (x));
4772 /* For unsigned, we have a choice of a shift followed by an
4773 AND or two shifts. Use two shifts for field sizes where the
4774 constant might be too large. We assume here that we can
4775 always at least get 8-bit constants in an AND insn, which is
4776 true for every current RISC. */
4778 if (unsignedp && len <= 8)
4780 unsigned HOST_WIDE_INT mask
4781 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4782 SUBST (SET_SRC (x),
4783 gen_rtx_AND (mode,
4784 gen_rtx_LSHIFTRT
4785 (mode, gen_lowpart (mode, inner),
4786 GEN_INT (pos)),
4787 gen_int_mode (mask, mode)));
4789 split = find_split_point (&SET_SRC (x), insn, true);
4790 if (split && split != &SET_SRC (x))
4791 return split;
4793 else
4795 SUBST (SET_SRC (x),
4796 gen_rtx_fmt_ee
4797 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4798 gen_rtx_ASHIFT (mode,
4799 gen_lowpart (mode, inner),
4800 GEN_INT (GET_MODE_PRECISION (mode)
4801 - len - pos)),
4802 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4804 split = find_split_point (&SET_SRC (x), insn, true);
4805 if (split && split != &SET_SRC (x))
4806 return split;
4810 /* See if this is a simple operation with a constant as the second
4811 operand. It might be that this constant is out of range and hence
4812 could be used as a split point. */
4813 if (BINARY_P (SET_SRC (x))
4814 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4815 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4816 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4817 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4818 return &XEXP (SET_SRC (x), 1);
4820 /* Finally, see if this is a simple operation with its first operand
4821 not in a register. The operation might require this operand in a
4822 register, so return it as a split point. We can always do this
4823 because if the first operand were another operation, we would have
4824 already found it as a split point. */
4825 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4826 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4827 return &XEXP (SET_SRC (x), 0);
4829 return 0;
4831 case AND:
4832 case IOR:
4833 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4834 it is better to write this as (not (ior A B)) so we can split it.
4835 Similarly for IOR. */
4836 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4838 SUBST (*loc,
4839 gen_rtx_NOT (GET_MODE (x),
4840 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4841 GET_MODE (x),
4842 XEXP (XEXP (x, 0), 0),
4843 XEXP (XEXP (x, 1), 0))));
4844 return find_split_point (loc, insn, set_src);
4847 /* Many RISC machines have a large set of logical insns. If the
4848 second operand is a NOT, put it first so we will try to split the
4849 other operand first. */
4850 if (GET_CODE (XEXP (x, 1)) == NOT)
4852 rtx tem = XEXP (x, 0);
4853 SUBST (XEXP (x, 0), XEXP (x, 1));
4854 SUBST (XEXP (x, 1), tem);
4856 break;
4858 case PLUS:
4859 case MINUS:
4860 /* Canonicalization can produce (minus A (mult B C)), where C is a
4861 constant. It may be better to try splitting (plus (mult B -C) A)
4862 instead if this isn't a multiply by a power of two. */
4863 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4864 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4865 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4867 enum machine_mode mode = GET_MODE (x);
4868 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4869 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4870 SUBST (*loc, gen_rtx_PLUS (mode,
4871 gen_rtx_MULT (mode,
4872 XEXP (XEXP (x, 1), 0),
4873 gen_int_mode (other_int,
4874 mode)),
4875 XEXP (x, 0)));
4876 return find_split_point (loc, insn, set_src);
4879 /* Split at a multiply-accumulate instruction. However if this is
4880 the SET_SRC, we likely do not have such an instruction and it's
4881 worthless to try this split. */
4882 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4883 return loc;
4885 default:
4886 break;
4889 /* Otherwise, select our actions depending on our rtx class. */
4890 switch (GET_RTX_CLASS (code))
4892 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4893 case RTX_TERNARY:
4894 split = find_split_point (&XEXP (x, 2), insn, false);
4895 if (split)
4896 return split;
4897 /* ... fall through ... */
4898 case RTX_BIN_ARITH:
4899 case RTX_COMM_ARITH:
4900 case RTX_COMPARE:
4901 case RTX_COMM_COMPARE:
4902 split = find_split_point (&XEXP (x, 1), insn, false);
4903 if (split)
4904 return split;
4905 /* ... fall through ... */
4906 case RTX_UNARY:
4907 /* Some machines have (and (shift ...) ...) insns. If X is not
4908 an AND, but XEXP (X, 0) is, use it as our split point. */
4909 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4910 return &XEXP (x, 0);
4912 split = find_split_point (&XEXP (x, 0), insn, false);
4913 if (split)
4914 return split;
4915 return loc;
4917 default:
4918 /* Otherwise, we don't have a split point. */
4919 return 0;
4923 /* Throughout X, replace FROM with TO, and return the result.
4924 The result is TO if X is FROM;
4925 otherwise the result is X, but its contents may have been modified.
4926 If they were modified, a record was made in undobuf so that
4927 undo_all will (among other things) return X to its original state.
4929 If the number of changes necessary is too much to record to undo,
4930 the excess changes are not made, so the result is invalid.
4931 The changes already made can still be undone.
4932 undobuf.num_undo is incremented for such changes, so by testing that
4933 the caller can tell whether the result is valid.
4935 `n_occurrences' is incremented each time FROM is replaced.
4937 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4939 IN_COND is nonzero if we are at the top level of a condition.
4941 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4942 by copying if `n_occurrences' is nonzero. */
4944 static rtx
4945 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4947 enum rtx_code code = GET_CODE (x);
4948 enum machine_mode op0_mode = VOIDmode;
4949 const char *fmt;
4950 int len, i;
4951 rtx new_rtx;
4953 /* Two expressions are equal if they are identical copies of a shared
4954 RTX or if they are both registers with the same register number
4955 and mode. */
4957 #define COMBINE_RTX_EQUAL_P(X,Y) \
4958 ((X) == (Y) \
4959 || (REG_P (X) && REG_P (Y) \
4960 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4962 /* Do not substitute into clobbers of regs -- this will never result in
4963 valid RTL. */
4964 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
4965 return x;
4967 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4969 n_occurrences++;
4970 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4973 /* If X and FROM are the same register but different modes, they
4974 will not have been seen as equal above. However, the log links code
4975 will make a LOG_LINKS entry for that case. If we do nothing, we
4976 will try to rerecognize our original insn and, when it succeeds,
4977 we will delete the feeding insn, which is incorrect.
4979 So force this insn not to match in this (rare) case. */
4980 if (! in_dest && code == REG && REG_P (from)
4981 && reg_overlap_mentioned_p (x, from))
4982 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4984 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4985 of which may contain things that can be combined. */
4986 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4987 return x;
4989 /* It is possible to have a subexpression appear twice in the insn.
4990 Suppose that FROM is a register that appears within TO.
4991 Then, after that subexpression has been scanned once by `subst',
4992 the second time it is scanned, TO may be found. If we were
4993 to scan TO here, we would find FROM within it and create a
4994 self-referent rtl structure which is completely wrong. */
4995 if (COMBINE_RTX_EQUAL_P (x, to))
4996 return to;
4998 /* Parallel asm_operands need special attention because all of the
4999 inputs are shared across the arms. Furthermore, unsharing the
5000 rtl results in recognition failures. Failure to handle this case
5001 specially can result in circular rtl.
5003 Solve this by doing a normal pass across the first entry of the
5004 parallel, and only processing the SET_DESTs of the subsequent
5005 entries. Ug. */
5007 if (code == PARALLEL
5008 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5009 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5011 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5013 /* If this substitution failed, this whole thing fails. */
5014 if (GET_CODE (new_rtx) == CLOBBER
5015 && XEXP (new_rtx, 0) == const0_rtx)
5016 return new_rtx;
5018 SUBST (XVECEXP (x, 0, 0), new_rtx);
5020 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5022 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5024 if (!REG_P (dest)
5025 && GET_CODE (dest) != CC0
5026 && GET_CODE (dest) != PC)
5028 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5030 /* If this substitution failed, this whole thing fails. */
5031 if (GET_CODE (new_rtx) == CLOBBER
5032 && XEXP (new_rtx, 0) == const0_rtx)
5033 return new_rtx;
5035 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5039 else
5041 len = GET_RTX_LENGTH (code);
5042 fmt = GET_RTX_FORMAT (code);
5044 /* We don't need to process a SET_DEST that is a register, CC0,
5045 or PC, so set up to skip this common case. All other cases
5046 where we want to suppress replacing something inside a
5047 SET_SRC are handled via the IN_DEST operand. */
5048 if (code == SET
5049 && (REG_P (SET_DEST (x))
5050 || GET_CODE (SET_DEST (x)) == CC0
5051 || GET_CODE (SET_DEST (x)) == PC))
5052 fmt = "ie";
5054 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5055 constant. */
5056 if (fmt[0] == 'e')
5057 op0_mode = GET_MODE (XEXP (x, 0));
5059 for (i = 0; i < len; i++)
5061 if (fmt[i] == 'E')
5063 int j;
5064 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5066 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5068 new_rtx = (unique_copy && n_occurrences
5069 ? copy_rtx (to) : to);
5070 n_occurrences++;
5072 else
5074 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5075 unique_copy);
5077 /* If this substitution failed, this whole thing
5078 fails. */
5079 if (GET_CODE (new_rtx) == CLOBBER
5080 && XEXP (new_rtx, 0) == const0_rtx)
5081 return new_rtx;
5084 SUBST (XVECEXP (x, i, j), new_rtx);
5087 else if (fmt[i] == 'e')
5089 /* If this is a register being set, ignore it. */
5090 new_rtx = XEXP (x, i);
5091 if (in_dest
5092 && i == 0
5093 && (((code == SUBREG || code == ZERO_EXTRACT)
5094 && REG_P (new_rtx))
5095 || code == STRICT_LOW_PART))
5098 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5100 /* In general, don't install a subreg involving two
5101 modes not tieable. It can worsen register
5102 allocation, and can even make invalid reload
5103 insns, since the reg inside may need to be copied
5104 from in the outside mode, and that may be invalid
5105 if it is an fp reg copied in integer mode.
5107 We allow two exceptions to this: It is valid if
5108 it is inside another SUBREG and the mode of that
5109 SUBREG and the mode of the inside of TO is
5110 tieable and it is valid if X is a SET that copies
5111 FROM to CC0. */
5113 if (GET_CODE (to) == SUBREG
5114 && ! MODES_TIEABLE_P (GET_MODE (to),
5115 GET_MODE (SUBREG_REG (to)))
5116 && ! (code == SUBREG
5117 && MODES_TIEABLE_P (GET_MODE (x),
5118 GET_MODE (SUBREG_REG (to))))
5119 #ifdef HAVE_cc0
5120 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5121 #endif
5123 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5125 if (code == SUBREG
5126 && REG_P (to)
5127 && REGNO (to) < FIRST_PSEUDO_REGISTER
5128 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5129 SUBREG_BYTE (x),
5130 GET_MODE (x)) < 0)
5131 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5133 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5134 n_occurrences++;
5136 else
5137 /* If we are in a SET_DEST, suppress most cases unless we
5138 have gone inside a MEM, in which case we want to
5139 simplify the address. We assume here that things that
5140 are actually part of the destination have their inner
5141 parts in the first expression. This is true for SUBREG,
5142 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5143 things aside from REG and MEM that should appear in a
5144 SET_DEST. */
5145 new_rtx = subst (XEXP (x, i), from, to,
5146 (((in_dest
5147 && (code == SUBREG || code == STRICT_LOW_PART
5148 || code == ZERO_EXTRACT))
5149 || code == SET)
5150 && i == 0),
5151 code == IF_THEN_ELSE && i == 0,
5152 unique_copy);
5154 /* If we found that we will have to reject this combination,
5155 indicate that by returning the CLOBBER ourselves, rather than
5156 an expression containing it. This will speed things up as
5157 well as prevent accidents where two CLOBBERs are considered
5158 to be equal, thus producing an incorrect simplification. */
5160 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5161 return new_rtx;
5163 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5165 enum machine_mode mode = GET_MODE (x);
5167 x = simplify_subreg (GET_MODE (x), new_rtx,
5168 GET_MODE (SUBREG_REG (x)),
5169 SUBREG_BYTE (x));
5170 if (! x)
5171 x = gen_rtx_CLOBBER (mode, const0_rtx);
5173 else if (CONST_SCALAR_INT_P (new_rtx)
5174 && GET_CODE (x) == ZERO_EXTEND)
5176 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5177 new_rtx, GET_MODE (XEXP (x, 0)));
5178 gcc_assert (x);
5180 else
5181 SUBST (XEXP (x, i), new_rtx);
5186 /* Check if we are loading something from the constant pool via float
5187 extension; in this case we would undo compress_float_constant
5188 optimization and degenerate constant load to an immediate value. */
5189 if (GET_CODE (x) == FLOAT_EXTEND
5190 && MEM_P (XEXP (x, 0))
5191 && MEM_READONLY_P (XEXP (x, 0)))
5193 rtx tmp = avoid_constant_pool_reference (x);
5194 if (x != tmp)
5195 return x;
5198 /* Try to simplify X. If the simplification changed the code, it is likely
5199 that further simplification will help, so loop, but limit the number
5200 of repetitions that will be performed. */
5202 for (i = 0; i < 4; i++)
5204 /* If X is sufficiently simple, don't bother trying to do anything
5205 with it. */
5206 if (code != CONST_INT && code != REG && code != CLOBBER)
5207 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5209 if (GET_CODE (x) == code)
5210 break;
5212 code = GET_CODE (x);
5214 /* We no longer know the original mode of operand 0 since we
5215 have changed the form of X) */
5216 op0_mode = VOIDmode;
5219 return x;
5222 /* Simplify X, a piece of RTL. We just operate on the expression at the
5223 outer level; call `subst' to simplify recursively. Return the new
5224 expression.
5226 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5227 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5228 of a condition. */
5230 static rtx
5231 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5232 int in_cond)
5234 enum rtx_code code = GET_CODE (x);
5235 enum machine_mode mode = GET_MODE (x);
5236 rtx temp;
5237 int i;
5239 /* If this is a commutative operation, put a constant last and a complex
5240 expression first. We don't need to do this for comparisons here. */
5241 if (COMMUTATIVE_ARITH_P (x)
5242 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5244 temp = XEXP (x, 0);
5245 SUBST (XEXP (x, 0), XEXP (x, 1));
5246 SUBST (XEXP (x, 1), temp);
5249 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5250 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5251 things. Check for cases where both arms are testing the same
5252 condition.
5254 Don't do anything if all operands are very simple. */
5256 if ((BINARY_P (x)
5257 && ((!OBJECT_P (XEXP (x, 0))
5258 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5259 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5260 || (!OBJECT_P (XEXP (x, 1))
5261 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5262 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5263 || (UNARY_P (x)
5264 && (!OBJECT_P (XEXP (x, 0))
5265 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5266 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5268 rtx cond, true_rtx, false_rtx;
5270 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5271 if (cond != 0
5272 /* If everything is a comparison, what we have is highly unlikely
5273 to be simpler, so don't use it. */
5274 && ! (COMPARISON_P (x)
5275 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5277 rtx cop1 = const0_rtx;
5278 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5280 if (cond_code == NE && COMPARISON_P (cond))
5281 return x;
5283 /* Simplify the alternative arms; this may collapse the true and
5284 false arms to store-flag values. Be careful to use copy_rtx
5285 here since true_rtx or false_rtx might share RTL with x as a
5286 result of the if_then_else_cond call above. */
5287 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5288 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5290 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5291 is unlikely to be simpler. */
5292 if (general_operand (true_rtx, VOIDmode)
5293 && general_operand (false_rtx, VOIDmode))
5295 enum rtx_code reversed;
5297 /* Restarting if we generate a store-flag expression will cause
5298 us to loop. Just drop through in this case. */
5300 /* If the result values are STORE_FLAG_VALUE and zero, we can
5301 just make the comparison operation. */
5302 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5303 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5304 cond, cop1);
5305 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5306 && ((reversed = reversed_comparison_code_parts
5307 (cond_code, cond, cop1, NULL))
5308 != UNKNOWN))
5309 x = simplify_gen_relational (reversed, mode, VOIDmode,
5310 cond, cop1);
5312 /* Likewise, we can make the negate of a comparison operation
5313 if the result values are - STORE_FLAG_VALUE and zero. */
5314 else if (CONST_INT_P (true_rtx)
5315 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5316 && false_rtx == const0_rtx)
5317 x = simplify_gen_unary (NEG, mode,
5318 simplify_gen_relational (cond_code,
5319 mode, VOIDmode,
5320 cond, cop1),
5321 mode);
5322 else if (CONST_INT_P (false_rtx)
5323 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5324 && true_rtx == const0_rtx
5325 && ((reversed = reversed_comparison_code_parts
5326 (cond_code, cond, cop1, NULL))
5327 != UNKNOWN))
5328 x = simplify_gen_unary (NEG, mode,
5329 simplify_gen_relational (reversed,
5330 mode, VOIDmode,
5331 cond, cop1),
5332 mode);
5333 else
5334 return gen_rtx_IF_THEN_ELSE (mode,
5335 simplify_gen_relational (cond_code,
5336 mode,
5337 VOIDmode,
5338 cond,
5339 cop1),
5340 true_rtx, false_rtx);
5342 code = GET_CODE (x);
5343 op0_mode = VOIDmode;
5348 /* Try to fold this expression in case we have constants that weren't
5349 present before. */
5350 temp = 0;
5351 switch (GET_RTX_CLASS (code))
5353 case RTX_UNARY:
5354 if (op0_mode == VOIDmode)
5355 op0_mode = GET_MODE (XEXP (x, 0));
5356 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5357 break;
5358 case RTX_COMPARE:
5359 case RTX_COMM_COMPARE:
5361 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5362 if (cmp_mode == VOIDmode)
5364 cmp_mode = GET_MODE (XEXP (x, 1));
5365 if (cmp_mode == VOIDmode)
5366 cmp_mode = op0_mode;
5368 temp = simplify_relational_operation (code, mode, cmp_mode,
5369 XEXP (x, 0), XEXP (x, 1));
5371 break;
5372 case RTX_COMM_ARITH:
5373 case RTX_BIN_ARITH:
5374 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5375 break;
5376 case RTX_BITFIELD_OPS:
5377 case RTX_TERNARY:
5378 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5379 XEXP (x, 1), XEXP (x, 2));
5380 break;
5381 default:
5382 break;
5385 if (temp)
5387 x = temp;
5388 code = GET_CODE (temp);
5389 op0_mode = VOIDmode;
5390 mode = GET_MODE (temp);
5393 /* First see if we can apply the inverse distributive law. */
5394 if (code == PLUS || code == MINUS
5395 || code == AND || code == IOR || code == XOR)
5397 x = apply_distributive_law (x);
5398 code = GET_CODE (x);
5399 op0_mode = VOIDmode;
5402 /* If CODE is an associative operation not otherwise handled, see if we
5403 can associate some operands. This can win if they are constants or
5404 if they are logically related (i.e. (a & b) & a). */
5405 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5406 || code == AND || code == IOR || code == XOR
5407 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5408 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5409 || (flag_associative_math && FLOAT_MODE_P (mode))))
5411 if (GET_CODE (XEXP (x, 0)) == code)
5413 rtx other = XEXP (XEXP (x, 0), 0);
5414 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5415 rtx inner_op1 = XEXP (x, 1);
5416 rtx inner;
5418 /* Make sure we pass the constant operand if any as the second
5419 one if this is a commutative operation. */
5420 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5422 rtx tem = inner_op0;
5423 inner_op0 = inner_op1;
5424 inner_op1 = tem;
5426 inner = simplify_binary_operation (code == MINUS ? PLUS
5427 : code == DIV ? MULT
5428 : code,
5429 mode, inner_op0, inner_op1);
5431 /* For commutative operations, try the other pair if that one
5432 didn't simplify. */
5433 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5435 other = XEXP (XEXP (x, 0), 1);
5436 inner = simplify_binary_operation (code, mode,
5437 XEXP (XEXP (x, 0), 0),
5438 XEXP (x, 1));
5441 if (inner)
5442 return simplify_gen_binary (code, mode, other, inner);
5446 /* A little bit of algebraic simplification here. */
5447 switch (code)
5449 case MEM:
5450 /* Ensure that our address has any ASHIFTs converted to MULT in case
5451 address-recognizing predicates are called later. */
5452 temp = make_compound_operation (XEXP (x, 0), MEM);
5453 SUBST (XEXP (x, 0), temp);
5454 break;
5456 case SUBREG:
5457 if (op0_mode == VOIDmode)
5458 op0_mode = GET_MODE (SUBREG_REG (x));
5460 /* See if this can be moved to simplify_subreg. */
5461 if (CONSTANT_P (SUBREG_REG (x))
5462 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5463 /* Don't call gen_lowpart if the inner mode
5464 is VOIDmode and we cannot simplify it, as SUBREG without
5465 inner mode is invalid. */
5466 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5467 || gen_lowpart_common (mode, SUBREG_REG (x))))
5468 return gen_lowpart (mode, SUBREG_REG (x));
5470 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5471 break;
5473 rtx temp;
5474 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5475 SUBREG_BYTE (x));
5476 if (temp)
5477 return temp;
5479 /* If op is known to have all lower bits zero, the result is zero. */
5480 if (!in_dest
5481 && SCALAR_INT_MODE_P (mode)
5482 && SCALAR_INT_MODE_P (op0_mode)
5483 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5484 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5485 && HWI_COMPUTABLE_MODE_P (op0_mode)
5486 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5487 & GET_MODE_MASK (mode)) == 0)
5488 return CONST0_RTX (mode);
5491 /* Don't change the mode of the MEM if that would change the meaning
5492 of the address. */
5493 if (MEM_P (SUBREG_REG (x))
5494 && (MEM_VOLATILE_P (SUBREG_REG (x))
5495 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5496 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5497 return gen_rtx_CLOBBER (mode, const0_rtx);
5499 /* Note that we cannot do any narrowing for non-constants since
5500 we might have been counting on using the fact that some bits were
5501 zero. We now do this in the SET. */
5503 break;
5505 case NEG:
5506 temp = expand_compound_operation (XEXP (x, 0));
5508 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5509 replaced by (lshiftrt X C). This will convert
5510 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5512 if (GET_CODE (temp) == ASHIFTRT
5513 && CONST_INT_P (XEXP (temp, 1))
5514 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5515 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5516 INTVAL (XEXP (temp, 1)));
5518 /* If X has only a single bit that might be nonzero, say, bit I, convert
5519 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5520 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5521 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5522 or a SUBREG of one since we'd be making the expression more
5523 complex if it was just a register. */
5525 if (!REG_P (temp)
5526 && ! (GET_CODE (temp) == SUBREG
5527 && REG_P (SUBREG_REG (temp)))
5528 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5530 rtx temp1 = simplify_shift_const
5531 (NULL_RTX, ASHIFTRT, mode,
5532 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5533 GET_MODE_PRECISION (mode) - 1 - i),
5534 GET_MODE_PRECISION (mode) - 1 - i);
5536 /* If all we did was surround TEMP with the two shifts, we
5537 haven't improved anything, so don't use it. Otherwise,
5538 we are better off with TEMP1. */
5539 if (GET_CODE (temp1) != ASHIFTRT
5540 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5541 || XEXP (XEXP (temp1, 0), 0) != temp)
5542 return temp1;
5544 break;
5546 case TRUNCATE:
5547 /* We can't handle truncation to a partial integer mode here
5548 because we don't know the real bitsize of the partial
5549 integer mode. */
5550 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5551 break;
5553 if (HWI_COMPUTABLE_MODE_P (mode))
5554 SUBST (XEXP (x, 0),
5555 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5556 GET_MODE_MASK (mode), 0));
5558 /* We can truncate a constant value and return it. */
5559 if (CONST_INT_P (XEXP (x, 0)))
5560 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5562 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5563 whose value is a comparison can be replaced with a subreg if
5564 STORE_FLAG_VALUE permits. */
5565 if (HWI_COMPUTABLE_MODE_P (mode)
5566 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5567 && (temp = get_last_value (XEXP (x, 0)))
5568 && COMPARISON_P (temp))
5569 return gen_lowpart (mode, XEXP (x, 0));
5570 break;
5572 case CONST:
5573 /* (const (const X)) can become (const X). Do it this way rather than
5574 returning the inner CONST since CONST can be shared with a
5575 REG_EQUAL note. */
5576 if (GET_CODE (XEXP (x, 0)) == CONST)
5577 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5578 break;
5580 #ifdef HAVE_lo_sum
5581 case LO_SUM:
5582 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5583 can add in an offset. find_split_point will split this address up
5584 again if it doesn't match. */
5585 if (GET_CODE (XEXP (x, 0)) == HIGH
5586 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5587 return XEXP (x, 1);
5588 break;
5589 #endif
5591 case PLUS:
5592 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5593 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5594 bit-field and can be replaced by either a sign_extend or a
5595 sign_extract. The `and' may be a zero_extend and the two
5596 <c>, -<c> constants may be reversed. */
5597 if (GET_CODE (XEXP (x, 0)) == XOR
5598 && CONST_INT_P (XEXP (x, 1))
5599 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5600 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5601 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5602 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5603 && HWI_COMPUTABLE_MODE_P (mode)
5604 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5605 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5606 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5607 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5608 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5609 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5610 == (unsigned int) i + 1))))
5611 return simplify_shift_const
5612 (NULL_RTX, ASHIFTRT, mode,
5613 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5614 XEXP (XEXP (XEXP (x, 0), 0), 0),
5615 GET_MODE_PRECISION (mode) - (i + 1)),
5616 GET_MODE_PRECISION (mode) - (i + 1));
5618 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5619 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5620 the bitsize of the mode - 1. This allows simplification of
5621 "a = (b & 8) == 0;" */
5622 if (XEXP (x, 1) == constm1_rtx
5623 && !REG_P (XEXP (x, 0))
5624 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5625 && REG_P (SUBREG_REG (XEXP (x, 0))))
5626 && nonzero_bits (XEXP (x, 0), mode) == 1)
5627 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5628 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5629 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5630 GET_MODE_PRECISION (mode) - 1),
5631 GET_MODE_PRECISION (mode) - 1);
5633 /* If we are adding two things that have no bits in common, convert
5634 the addition into an IOR. This will often be further simplified,
5635 for example in cases like ((a & 1) + (a & 2)), which can
5636 become a & 3. */
5638 if (HWI_COMPUTABLE_MODE_P (mode)
5639 && (nonzero_bits (XEXP (x, 0), mode)
5640 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5642 /* Try to simplify the expression further. */
5643 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5644 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5646 /* If we could, great. If not, do not go ahead with the IOR
5647 replacement, since PLUS appears in many special purpose
5648 address arithmetic instructions. */
5649 if (GET_CODE (temp) != CLOBBER
5650 && (GET_CODE (temp) != IOR
5651 || ((XEXP (temp, 0) != XEXP (x, 0)
5652 || XEXP (temp, 1) != XEXP (x, 1))
5653 && (XEXP (temp, 0) != XEXP (x, 1)
5654 || XEXP (temp, 1) != XEXP (x, 0)))))
5655 return temp;
5657 break;
5659 case MINUS:
5660 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5661 (and <foo> (const_int pow2-1)) */
5662 if (GET_CODE (XEXP (x, 1)) == AND
5663 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5664 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5665 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5666 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5667 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5668 break;
5670 case MULT:
5671 /* If we have (mult (plus A B) C), apply the distributive law and then
5672 the inverse distributive law to see if things simplify. This
5673 occurs mostly in addresses, often when unrolling loops. */
5675 if (GET_CODE (XEXP (x, 0)) == PLUS)
5677 rtx result = distribute_and_simplify_rtx (x, 0);
5678 if (result)
5679 return result;
5682 /* Try simplify a*(b/c) as (a*b)/c. */
5683 if (FLOAT_MODE_P (mode) && flag_associative_math
5684 && GET_CODE (XEXP (x, 0)) == DIV)
5686 rtx tem = simplify_binary_operation (MULT, mode,
5687 XEXP (XEXP (x, 0), 0),
5688 XEXP (x, 1));
5689 if (tem)
5690 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5692 break;
5694 case UDIV:
5695 /* If this is a divide by a power of two, treat it as a shift if
5696 its first operand is a shift. */
5697 if (CONST_INT_P (XEXP (x, 1))
5698 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5699 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5700 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5701 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5702 || GET_CODE (XEXP (x, 0)) == ROTATE
5703 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5704 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5705 break;
5707 case EQ: case NE:
5708 case GT: case GTU: case GE: case GEU:
5709 case LT: case LTU: case LE: case LEU:
5710 case UNEQ: case LTGT:
5711 case UNGT: case UNGE:
5712 case UNLT: case UNLE:
5713 case UNORDERED: case ORDERED:
5714 /* If the first operand is a condition code, we can't do anything
5715 with it. */
5716 if (GET_CODE (XEXP (x, 0)) == COMPARE
5717 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5718 && ! CC0_P (XEXP (x, 0))))
5720 rtx op0 = XEXP (x, 0);
5721 rtx op1 = XEXP (x, 1);
5722 enum rtx_code new_code;
5724 if (GET_CODE (op0) == COMPARE)
5725 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5727 /* Simplify our comparison, if possible. */
5728 new_code = simplify_comparison (code, &op0, &op1);
5730 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5731 if only the low-order bit is possibly nonzero in X (such as when
5732 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5733 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5734 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5735 (plus X 1).
5737 Remove any ZERO_EXTRACT we made when thinking this was a
5738 comparison. It may now be simpler to use, e.g., an AND. If a
5739 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5740 the call to make_compound_operation in the SET case.
5742 Don't apply these optimizations if the caller would
5743 prefer a comparison rather than a value.
5744 E.g., for the condition in an IF_THEN_ELSE most targets need
5745 an explicit comparison. */
5747 if (in_cond)
5750 else if (STORE_FLAG_VALUE == 1
5751 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5752 && op1 == const0_rtx
5753 && mode == GET_MODE (op0)
5754 && nonzero_bits (op0, mode) == 1)
5755 return gen_lowpart (mode,
5756 expand_compound_operation (op0));
5758 else if (STORE_FLAG_VALUE == 1
5759 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5760 && op1 == const0_rtx
5761 && mode == GET_MODE (op0)
5762 && (num_sign_bit_copies (op0, mode)
5763 == GET_MODE_PRECISION (mode)))
5765 op0 = expand_compound_operation (op0);
5766 return simplify_gen_unary (NEG, mode,
5767 gen_lowpart (mode, op0),
5768 mode);
5771 else if (STORE_FLAG_VALUE == 1
5772 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5773 && op1 == const0_rtx
5774 && mode == GET_MODE (op0)
5775 && nonzero_bits (op0, mode) == 1)
5777 op0 = expand_compound_operation (op0);
5778 return simplify_gen_binary (XOR, mode,
5779 gen_lowpart (mode, op0),
5780 const1_rtx);
5783 else if (STORE_FLAG_VALUE == 1
5784 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5785 && op1 == const0_rtx
5786 && mode == GET_MODE (op0)
5787 && (num_sign_bit_copies (op0, mode)
5788 == GET_MODE_PRECISION (mode)))
5790 op0 = expand_compound_operation (op0);
5791 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5794 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5795 those above. */
5796 if (in_cond)
5799 else if (STORE_FLAG_VALUE == -1
5800 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5801 && op1 == const0_rtx
5802 && (num_sign_bit_copies (op0, mode)
5803 == GET_MODE_PRECISION (mode)))
5804 return gen_lowpart (mode,
5805 expand_compound_operation (op0));
5807 else if (STORE_FLAG_VALUE == -1
5808 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5809 && op1 == const0_rtx
5810 && mode == GET_MODE (op0)
5811 && nonzero_bits (op0, mode) == 1)
5813 op0 = expand_compound_operation (op0);
5814 return simplify_gen_unary (NEG, mode,
5815 gen_lowpart (mode, op0),
5816 mode);
5819 else if (STORE_FLAG_VALUE == -1
5820 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5821 && op1 == const0_rtx
5822 && mode == GET_MODE (op0)
5823 && (num_sign_bit_copies (op0, mode)
5824 == GET_MODE_PRECISION (mode)))
5826 op0 = expand_compound_operation (op0);
5827 return simplify_gen_unary (NOT, mode,
5828 gen_lowpart (mode, op0),
5829 mode);
5832 /* If X is 0/1, (eq X 0) is X-1. */
5833 else if (STORE_FLAG_VALUE == -1
5834 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5835 && op1 == const0_rtx
5836 && mode == GET_MODE (op0)
5837 && nonzero_bits (op0, mode) == 1)
5839 op0 = expand_compound_operation (op0);
5840 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5843 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5844 one bit that might be nonzero, we can convert (ne x 0) to
5845 (ashift x c) where C puts the bit in the sign bit. Remove any
5846 AND with STORE_FLAG_VALUE when we are done, since we are only
5847 going to test the sign bit. */
5848 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5849 && HWI_COMPUTABLE_MODE_P (mode)
5850 && val_signbit_p (mode, STORE_FLAG_VALUE)
5851 && op1 == const0_rtx
5852 && mode == GET_MODE (op0)
5853 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5855 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5856 expand_compound_operation (op0),
5857 GET_MODE_PRECISION (mode) - 1 - i);
5858 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5859 return XEXP (x, 0);
5860 else
5861 return x;
5864 /* If the code changed, return a whole new comparison.
5865 We also need to avoid using SUBST in cases where
5866 simplify_comparison has widened a comparison with a CONST_INT,
5867 since in that case the wider CONST_INT may fail the sanity
5868 checks in do_SUBST. */
5869 if (new_code != code
5870 || (CONST_INT_P (op1)
5871 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
5872 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
5873 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5875 /* Otherwise, keep this operation, but maybe change its operands.
5876 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5877 SUBST (XEXP (x, 0), op0);
5878 SUBST (XEXP (x, 1), op1);
5880 break;
5882 case IF_THEN_ELSE:
5883 return simplify_if_then_else (x);
5885 case ZERO_EXTRACT:
5886 case SIGN_EXTRACT:
5887 case ZERO_EXTEND:
5888 case SIGN_EXTEND:
5889 /* If we are processing SET_DEST, we are done. */
5890 if (in_dest)
5891 return x;
5893 return expand_compound_operation (x);
5895 case SET:
5896 return simplify_set (x);
5898 case AND:
5899 case IOR:
5900 return simplify_logical (x);
5902 case ASHIFT:
5903 case LSHIFTRT:
5904 case ASHIFTRT:
5905 case ROTATE:
5906 case ROTATERT:
5907 /* If this is a shift by a constant amount, simplify it. */
5908 if (CONST_INT_P (XEXP (x, 1)))
5909 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5910 INTVAL (XEXP (x, 1)));
5912 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5913 SUBST (XEXP (x, 1),
5914 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5915 ((unsigned HOST_WIDE_INT) 1
5916 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5917 - 1,
5918 0));
5919 break;
5921 default:
5922 break;
5925 return x;
5928 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5930 static rtx
5931 simplify_if_then_else (rtx x)
5933 enum machine_mode mode = GET_MODE (x);
5934 rtx cond = XEXP (x, 0);
5935 rtx true_rtx = XEXP (x, 1);
5936 rtx false_rtx = XEXP (x, 2);
5937 enum rtx_code true_code = GET_CODE (cond);
5938 int comparison_p = COMPARISON_P (cond);
5939 rtx temp;
5940 int i;
5941 enum rtx_code false_code;
5942 rtx reversed;
5944 /* Simplify storing of the truth value. */
5945 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5946 return simplify_gen_relational (true_code, mode, VOIDmode,
5947 XEXP (cond, 0), XEXP (cond, 1));
5949 /* Also when the truth value has to be reversed. */
5950 if (comparison_p
5951 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5952 && (reversed = reversed_comparison (cond, mode)))
5953 return reversed;
5955 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5956 in it is being compared against certain values. Get the true and false
5957 comparisons and see if that says anything about the value of each arm. */
5959 if (comparison_p
5960 && ((false_code = reversed_comparison_code (cond, NULL))
5961 != UNKNOWN)
5962 && REG_P (XEXP (cond, 0)))
5964 HOST_WIDE_INT nzb;
5965 rtx from = XEXP (cond, 0);
5966 rtx true_val = XEXP (cond, 1);
5967 rtx false_val = true_val;
5968 int swapped = 0;
5970 /* If FALSE_CODE is EQ, swap the codes and arms. */
5972 if (false_code == EQ)
5974 swapped = 1, true_code = EQ, false_code = NE;
5975 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5978 /* If we are comparing against zero and the expression being tested has
5979 only a single bit that might be nonzero, that is its value when it is
5980 not equal to zero. Similarly if it is known to be -1 or 0. */
5982 if (true_code == EQ && true_val == const0_rtx
5983 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5985 false_code = EQ;
5986 false_val = gen_int_mode (nzb, GET_MODE (from));
5988 else if (true_code == EQ && true_val == const0_rtx
5989 && (num_sign_bit_copies (from, GET_MODE (from))
5990 == GET_MODE_PRECISION (GET_MODE (from))))
5992 false_code = EQ;
5993 false_val = constm1_rtx;
5996 /* Now simplify an arm if we know the value of the register in the
5997 branch and it is used in the arm. Be careful due to the potential
5998 of locally-shared RTL. */
6000 if (reg_mentioned_p (from, true_rtx))
6001 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6002 from, true_val),
6003 pc_rtx, pc_rtx, 0, 0, 0);
6004 if (reg_mentioned_p (from, false_rtx))
6005 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6006 from, false_val),
6007 pc_rtx, pc_rtx, 0, 0, 0);
6009 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6010 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6012 true_rtx = XEXP (x, 1);
6013 false_rtx = XEXP (x, 2);
6014 true_code = GET_CODE (cond);
6017 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6018 reversed, do so to avoid needing two sets of patterns for
6019 subtract-and-branch insns. Similarly if we have a constant in the true
6020 arm, the false arm is the same as the first operand of the comparison, or
6021 the false arm is more complicated than the true arm. */
6023 if (comparison_p
6024 && reversed_comparison_code (cond, NULL) != UNKNOWN
6025 && (true_rtx == pc_rtx
6026 || (CONSTANT_P (true_rtx)
6027 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6028 || true_rtx == const0_rtx
6029 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6030 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6031 && !OBJECT_P (false_rtx))
6032 || reg_mentioned_p (true_rtx, false_rtx)
6033 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6035 true_code = reversed_comparison_code (cond, NULL);
6036 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6037 SUBST (XEXP (x, 1), false_rtx);
6038 SUBST (XEXP (x, 2), true_rtx);
6040 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6041 cond = XEXP (x, 0);
6043 /* It is possible that the conditional has been simplified out. */
6044 true_code = GET_CODE (cond);
6045 comparison_p = COMPARISON_P (cond);
6048 /* If the two arms are identical, we don't need the comparison. */
6050 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6051 return true_rtx;
6053 /* Convert a == b ? b : a to "a". */
6054 if (true_code == EQ && ! side_effects_p (cond)
6055 && !HONOR_NANS (mode)
6056 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6057 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6058 return false_rtx;
6059 else if (true_code == NE && ! side_effects_p (cond)
6060 && !HONOR_NANS (mode)
6061 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6062 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6063 return true_rtx;
6065 /* Look for cases where we have (abs x) or (neg (abs X)). */
6067 if (GET_MODE_CLASS (mode) == MODE_INT
6068 && comparison_p
6069 && XEXP (cond, 1) == const0_rtx
6070 && GET_CODE (false_rtx) == NEG
6071 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6072 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6073 && ! side_effects_p (true_rtx))
6074 switch (true_code)
6076 case GT:
6077 case GE:
6078 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6079 case LT:
6080 case LE:
6081 return
6082 simplify_gen_unary (NEG, mode,
6083 simplify_gen_unary (ABS, mode, true_rtx, mode),
6084 mode);
6085 default:
6086 break;
6089 /* Look for MIN or MAX. */
6091 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6092 && comparison_p
6093 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6094 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6095 && ! side_effects_p (cond))
6096 switch (true_code)
6098 case GE:
6099 case GT:
6100 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6101 case LE:
6102 case LT:
6103 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6104 case GEU:
6105 case GTU:
6106 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6107 case LEU:
6108 case LTU:
6109 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6110 default:
6111 break;
6114 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6115 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6116 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6117 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6118 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6119 neither 1 or -1, but it isn't worth checking for. */
6121 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6122 && comparison_p
6123 && GET_MODE_CLASS (mode) == MODE_INT
6124 && ! side_effects_p (x))
6126 rtx t = make_compound_operation (true_rtx, SET);
6127 rtx f = make_compound_operation (false_rtx, SET);
6128 rtx cond_op0 = XEXP (cond, 0);
6129 rtx cond_op1 = XEXP (cond, 1);
6130 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6131 enum machine_mode m = mode;
6132 rtx z = 0, c1 = NULL_RTX;
6134 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6135 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6136 || GET_CODE (t) == ASHIFT
6137 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6138 && rtx_equal_p (XEXP (t, 0), f))
6139 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6141 /* If an identity-zero op is commutative, check whether there
6142 would be a match if we swapped the operands. */
6143 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6144 || GET_CODE (t) == XOR)
6145 && rtx_equal_p (XEXP (t, 1), f))
6146 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6147 else if (GET_CODE (t) == SIGN_EXTEND
6148 && (GET_CODE (XEXP (t, 0)) == PLUS
6149 || GET_CODE (XEXP (t, 0)) == MINUS
6150 || GET_CODE (XEXP (t, 0)) == IOR
6151 || GET_CODE (XEXP (t, 0)) == XOR
6152 || GET_CODE (XEXP (t, 0)) == ASHIFT
6153 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6154 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6155 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6156 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6157 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6158 && (num_sign_bit_copies (f, GET_MODE (f))
6159 > (unsigned int)
6160 (GET_MODE_PRECISION (mode)
6161 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6163 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6164 extend_op = SIGN_EXTEND;
6165 m = GET_MODE (XEXP (t, 0));
6167 else if (GET_CODE (t) == SIGN_EXTEND
6168 && (GET_CODE (XEXP (t, 0)) == PLUS
6169 || GET_CODE (XEXP (t, 0)) == IOR
6170 || GET_CODE (XEXP (t, 0)) == XOR)
6171 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6172 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6173 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6174 && (num_sign_bit_copies (f, GET_MODE (f))
6175 > (unsigned int)
6176 (GET_MODE_PRECISION (mode)
6177 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6179 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6180 extend_op = SIGN_EXTEND;
6181 m = GET_MODE (XEXP (t, 0));
6183 else if (GET_CODE (t) == ZERO_EXTEND
6184 && (GET_CODE (XEXP (t, 0)) == PLUS
6185 || GET_CODE (XEXP (t, 0)) == MINUS
6186 || GET_CODE (XEXP (t, 0)) == IOR
6187 || GET_CODE (XEXP (t, 0)) == XOR
6188 || GET_CODE (XEXP (t, 0)) == ASHIFT
6189 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6190 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6191 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6192 && HWI_COMPUTABLE_MODE_P (mode)
6193 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6194 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6195 && ((nonzero_bits (f, GET_MODE (f))
6196 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6197 == 0))
6199 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6200 extend_op = ZERO_EXTEND;
6201 m = GET_MODE (XEXP (t, 0));
6203 else if (GET_CODE (t) == ZERO_EXTEND
6204 && (GET_CODE (XEXP (t, 0)) == PLUS
6205 || GET_CODE (XEXP (t, 0)) == IOR
6206 || GET_CODE (XEXP (t, 0)) == XOR)
6207 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6208 && HWI_COMPUTABLE_MODE_P (mode)
6209 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6210 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6211 && ((nonzero_bits (f, GET_MODE (f))
6212 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6213 == 0))
6215 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6216 extend_op = ZERO_EXTEND;
6217 m = GET_MODE (XEXP (t, 0));
6220 if (z)
6222 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6223 cond_op0, cond_op1),
6224 pc_rtx, pc_rtx, 0, 0, 0);
6225 temp = simplify_gen_binary (MULT, m, temp,
6226 simplify_gen_binary (MULT, m, c1,
6227 const_true_rtx));
6228 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6229 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6231 if (extend_op != UNKNOWN)
6232 temp = simplify_gen_unary (extend_op, mode, temp, m);
6234 return temp;
6238 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6239 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6240 negation of a single bit, we can convert this operation to a shift. We
6241 can actually do this more generally, but it doesn't seem worth it. */
6243 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6244 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6245 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6246 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6247 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6248 == GET_MODE_PRECISION (mode))
6249 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6250 return
6251 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6252 gen_lowpart (mode, XEXP (cond, 0)), i);
6254 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6255 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6256 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6257 && GET_MODE (XEXP (cond, 0)) == mode
6258 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6259 == nonzero_bits (XEXP (cond, 0), mode)
6260 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6261 return XEXP (cond, 0);
6263 return x;
6266 /* Simplify X, a SET expression. Return the new expression. */
6268 static rtx
6269 simplify_set (rtx x)
6271 rtx src = SET_SRC (x);
6272 rtx dest = SET_DEST (x);
6273 enum machine_mode mode
6274 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6275 rtx_insn *other_insn;
6276 rtx *cc_use;
6278 /* (set (pc) (return)) gets written as (return). */
6279 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6280 return src;
6282 /* Now that we know for sure which bits of SRC we are using, see if we can
6283 simplify the expression for the object knowing that we only need the
6284 low-order bits. */
6286 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6288 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6289 SUBST (SET_SRC (x), src);
6292 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6293 the comparison result and try to simplify it unless we already have used
6294 undobuf.other_insn. */
6295 if ((GET_MODE_CLASS (mode) == MODE_CC
6296 || GET_CODE (src) == COMPARE
6297 || CC0_P (dest))
6298 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6299 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6300 && COMPARISON_P (*cc_use)
6301 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6303 enum rtx_code old_code = GET_CODE (*cc_use);
6304 enum rtx_code new_code;
6305 rtx op0, op1, tmp;
6306 int other_changed = 0;
6307 rtx inner_compare = NULL_RTX;
6308 enum machine_mode compare_mode = GET_MODE (dest);
6310 if (GET_CODE (src) == COMPARE)
6312 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6313 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6315 inner_compare = op0;
6316 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6319 else
6320 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6322 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6323 op0, op1);
6324 if (!tmp)
6325 new_code = old_code;
6326 else if (!CONSTANT_P (tmp))
6328 new_code = GET_CODE (tmp);
6329 op0 = XEXP (tmp, 0);
6330 op1 = XEXP (tmp, 1);
6332 else
6334 rtx pat = PATTERN (other_insn);
6335 undobuf.other_insn = other_insn;
6336 SUBST (*cc_use, tmp);
6338 /* Attempt to simplify CC user. */
6339 if (GET_CODE (pat) == SET)
6341 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6342 if (new_rtx != NULL_RTX)
6343 SUBST (SET_SRC (pat), new_rtx);
6346 /* Convert X into a no-op move. */
6347 SUBST (SET_DEST (x), pc_rtx);
6348 SUBST (SET_SRC (x), pc_rtx);
6349 return x;
6352 /* Simplify our comparison, if possible. */
6353 new_code = simplify_comparison (new_code, &op0, &op1);
6355 #ifdef SELECT_CC_MODE
6356 /* If this machine has CC modes other than CCmode, check to see if we
6357 need to use a different CC mode here. */
6358 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6359 compare_mode = GET_MODE (op0);
6360 else if (inner_compare
6361 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6362 && new_code == old_code
6363 && op0 == XEXP (inner_compare, 0)
6364 && op1 == XEXP (inner_compare, 1))
6365 compare_mode = GET_MODE (inner_compare);
6366 else
6367 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6369 #ifndef HAVE_cc0
6370 /* If the mode changed, we have to change SET_DEST, the mode in the
6371 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6372 a hard register, just build new versions with the proper mode. If it
6373 is a pseudo, we lose unless it is only time we set the pseudo, in
6374 which case we can safely change its mode. */
6375 if (compare_mode != GET_MODE (dest))
6377 if (can_change_dest_mode (dest, 0, compare_mode))
6379 unsigned int regno = REGNO (dest);
6380 rtx new_dest;
6382 if (regno < FIRST_PSEUDO_REGISTER)
6383 new_dest = gen_rtx_REG (compare_mode, regno);
6384 else
6386 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6387 new_dest = regno_reg_rtx[regno];
6390 SUBST (SET_DEST (x), new_dest);
6391 SUBST (XEXP (*cc_use, 0), new_dest);
6392 other_changed = 1;
6394 dest = new_dest;
6397 #endif /* cc0 */
6398 #endif /* SELECT_CC_MODE */
6400 /* If the code changed, we have to build a new comparison in
6401 undobuf.other_insn. */
6402 if (new_code != old_code)
6404 int other_changed_previously = other_changed;
6405 unsigned HOST_WIDE_INT mask;
6406 rtx old_cc_use = *cc_use;
6408 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6409 dest, const0_rtx));
6410 other_changed = 1;
6412 /* If the only change we made was to change an EQ into an NE or
6413 vice versa, OP0 has only one bit that might be nonzero, and OP1
6414 is zero, check if changing the user of the condition code will
6415 produce a valid insn. If it won't, we can keep the original code
6416 in that insn by surrounding our operation with an XOR. */
6418 if (((old_code == NE && new_code == EQ)
6419 || (old_code == EQ && new_code == NE))
6420 && ! other_changed_previously && op1 == const0_rtx
6421 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6422 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6424 rtx pat = PATTERN (other_insn), note = 0;
6426 if ((recog_for_combine (&pat, other_insn, &note) < 0
6427 && ! check_asm_operands (pat)))
6429 *cc_use = old_cc_use;
6430 other_changed = 0;
6432 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6433 gen_int_mode (mask,
6434 GET_MODE (op0)));
6439 if (other_changed)
6440 undobuf.other_insn = other_insn;
6442 /* Otherwise, if we didn't previously have a COMPARE in the
6443 correct mode, we need one. */
6444 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6446 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6447 src = SET_SRC (x);
6449 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6451 SUBST (SET_SRC (x), op0);
6452 src = SET_SRC (x);
6454 /* Otherwise, update the COMPARE if needed. */
6455 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6457 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6458 src = SET_SRC (x);
6461 else
6463 /* Get SET_SRC in a form where we have placed back any
6464 compound expressions. Then do the checks below. */
6465 src = make_compound_operation (src, SET);
6466 SUBST (SET_SRC (x), src);
6469 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6470 and X being a REG or (subreg (reg)), we may be able to convert this to
6471 (set (subreg:m2 x) (op)).
6473 We can always do this if M1 is narrower than M2 because that means that
6474 we only care about the low bits of the result.
6476 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6477 perform a narrower operation than requested since the high-order bits will
6478 be undefined. On machine where it is defined, this transformation is safe
6479 as long as M1 and M2 have the same number of words. */
6481 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6482 && !OBJECT_P (SUBREG_REG (src))
6483 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6484 / UNITS_PER_WORD)
6485 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6486 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6487 #ifndef WORD_REGISTER_OPERATIONS
6488 && (GET_MODE_SIZE (GET_MODE (src))
6489 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6490 #endif
6491 #ifdef CANNOT_CHANGE_MODE_CLASS
6492 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6493 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6494 GET_MODE (SUBREG_REG (src)),
6495 GET_MODE (src)))
6496 #endif
6497 && (REG_P (dest)
6498 || (GET_CODE (dest) == SUBREG
6499 && REG_P (SUBREG_REG (dest)))))
6501 SUBST (SET_DEST (x),
6502 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6503 dest));
6504 SUBST (SET_SRC (x), SUBREG_REG (src));
6506 src = SET_SRC (x), dest = SET_DEST (x);
6509 #ifdef HAVE_cc0
6510 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6511 in SRC. */
6512 if (dest == cc0_rtx
6513 && GET_CODE (src) == SUBREG
6514 && subreg_lowpart_p (src)
6515 && (GET_MODE_PRECISION (GET_MODE (src))
6516 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6518 rtx inner = SUBREG_REG (src);
6519 enum machine_mode inner_mode = GET_MODE (inner);
6521 /* Here we make sure that we don't have a sign bit on. */
6522 if (val_signbit_known_clear_p (GET_MODE (src),
6523 nonzero_bits (inner, inner_mode)))
6525 SUBST (SET_SRC (x), inner);
6526 src = SET_SRC (x);
6529 #endif
6531 #ifdef LOAD_EXTEND_OP
6532 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6533 would require a paradoxical subreg. Replace the subreg with a
6534 zero_extend to avoid the reload that would otherwise be required. */
6536 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6537 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6538 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6539 && SUBREG_BYTE (src) == 0
6540 && paradoxical_subreg_p (src)
6541 && MEM_P (SUBREG_REG (src)))
6543 SUBST (SET_SRC (x),
6544 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6545 GET_MODE (src), SUBREG_REG (src)));
6547 src = SET_SRC (x);
6549 #endif
6551 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6552 are comparing an item known to be 0 or -1 against 0, use a logical
6553 operation instead. Check for one of the arms being an IOR of the other
6554 arm with some value. We compute three terms to be IOR'ed together. In
6555 practice, at most two will be nonzero. Then we do the IOR's. */
6557 if (GET_CODE (dest) != PC
6558 && GET_CODE (src) == IF_THEN_ELSE
6559 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6560 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6561 && XEXP (XEXP (src, 0), 1) == const0_rtx
6562 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6563 #ifdef HAVE_conditional_move
6564 && ! can_conditionally_move_p (GET_MODE (src))
6565 #endif
6566 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6567 GET_MODE (XEXP (XEXP (src, 0), 0)))
6568 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6569 && ! side_effects_p (src))
6571 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6572 ? XEXP (src, 1) : XEXP (src, 2));
6573 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6574 ? XEXP (src, 2) : XEXP (src, 1));
6575 rtx term1 = const0_rtx, term2, term3;
6577 if (GET_CODE (true_rtx) == IOR
6578 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6579 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6580 else if (GET_CODE (true_rtx) == IOR
6581 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6582 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6583 else if (GET_CODE (false_rtx) == IOR
6584 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6585 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6586 else if (GET_CODE (false_rtx) == IOR
6587 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6588 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6590 term2 = simplify_gen_binary (AND, GET_MODE (src),
6591 XEXP (XEXP (src, 0), 0), true_rtx);
6592 term3 = simplify_gen_binary (AND, GET_MODE (src),
6593 simplify_gen_unary (NOT, GET_MODE (src),
6594 XEXP (XEXP (src, 0), 0),
6595 GET_MODE (src)),
6596 false_rtx);
6598 SUBST (SET_SRC (x),
6599 simplify_gen_binary (IOR, GET_MODE (src),
6600 simplify_gen_binary (IOR, GET_MODE (src),
6601 term1, term2),
6602 term3));
6604 src = SET_SRC (x);
6607 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6608 whole thing fail. */
6609 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6610 return src;
6611 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6612 return dest;
6613 else
6614 /* Convert this into a field assignment operation, if possible. */
6615 return make_field_assignment (x);
6618 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6619 result. */
6621 static rtx
6622 simplify_logical (rtx x)
6624 enum machine_mode mode = GET_MODE (x);
6625 rtx op0 = XEXP (x, 0);
6626 rtx op1 = XEXP (x, 1);
6628 switch (GET_CODE (x))
6630 case AND:
6631 /* We can call simplify_and_const_int only if we don't lose
6632 any (sign) bits when converting INTVAL (op1) to
6633 "unsigned HOST_WIDE_INT". */
6634 if (CONST_INT_P (op1)
6635 && (HWI_COMPUTABLE_MODE_P (mode)
6636 || INTVAL (op1) > 0))
6638 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6639 if (GET_CODE (x) != AND)
6640 return x;
6642 op0 = XEXP (x, 0);
6643 op1 = XEXP (x, 1);
6646 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6647 apply the distributive law and then the inverse distributive
6648 law to see if things simplify. */
6649 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6651 rtx result = distribute_and_simplify_rtx (x, 0);
6652 if (result)
6653 return result;
6655 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6657 rtx result = distribute_and_simplify_rtx (x, 1);
6658 if (result)
6659 return result;
6661 break;
6663 case IOR:
6664 /* If we have (ior (and A B) C), apply the distributive law and then
6665 the inverse distributive law to see if things simplify. */
6667 if (GET_CODE (op0) == AND)
6669 rtx result = distribute_and_simplify_rtx (x, 0);
6670 if (result)
6671 return result;
6674 if (GET_CODE (op1) == AND)
6676 rtx result = distribute_and_simplify_rtx (x, 1);
6677 if (result)
6678 return result;
6680 break;
6682 default:
6683 gcc_unreachable ();
6686 return x;
6689 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6690 operations" because they can be replaced with two more basic operations.
6691 ZERO_EXTEND is also considered "compound" because it can be replaced with
6692 an AND operation, which is simpler, though only one operation.
6694 The function expand_compound_operation is called with an rtx expression
6695 and will convert it to the appropriate shifts and AND operations,
6696 simplifying at each stage.
6698 The function make_compound_operation is called to convert an expression
6699 consisting of shifts and ANDs into the equivalent compound expression.
6700 It is the inverse of this function, loosely speaking. */
6702 static rtx
6703 expand_compound_operation (rtx x)
6705 unsigned HOST_WIDE_INT pos = 0, len;
6706 int unsignedp = 0;
6707 unsigned int modewidth;
6708 rtx tem;
6710 switch (GET_CODE (x))
6712 case ZERO_EXTEND:
6713 unsignedp = 1;
6714 case SIGN_EXTEND:
6715 /* We can't necessarily use a const_int for a multiword mode;
6716 it depends on implicitly extending the value.
6717 Since we don't know the right way to extend it,
6718 we can't tell whether the implicit way is right.
6720 Even for a mode that is no wider than a const_int,
6721 we can't win, because we need to sign extend one of its bits through
6722 the rest of it, and we don't know which bit. */
6723 if (CONST_INT_P (XEXP (x, 0)))
6724 return x;
6726 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6727 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6728 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6729 reloaded. If not for that, MEM's would very rarely be safe.
6731 Reject MODEs bigger than a word, because we might not be able
6732 to reference a two-register group starting with an arbitrary register
6733 (and currently gen_lowpart might crash for a SUBREG). */
6735 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6736 return x;
6738 /* Reject MODEs that aren't scalar integers because turning vector
6739 or complex modes into shifts causes problems. */
6741 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6742 return x;
6744 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6745 /* If the inner object has VOIDmode (the only way this can happen
6746 is if it is an ASM_OPERANDS), we can't do anything since we don't
6747 know how much masking to do. */
6748 if (len == 0)
6749 return x;
6751 break;
6753 case ZERO_EXTRACT:
6754 unsignedp = 1;
6756 /* ... fall through ... */
6758 case SIGN_EXTRACT:
6759 /* If the operand is a CLOBBER, just return it. */
6760 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6761 return XEXP (x, 0);
6763 if (!CONST_INT_P (XEXP (x, 1))
6764 || !CONST_INT_P (XEXP (x, 2))
6765 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6766 return x;
6768 /* Reject MODEs that aren't scalar integers because turning vector
6769 or complex modes into shifts causes problems. */
6771 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6772 return x;
6774 len = INTVAL (XEXP (x, 1));
6775 pos = INTVAL (XEXP (x, 2));
6777 /* This should stay within the object being extracted, fail otherwise. */
6778 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6779 return x;
6781 if (BITS_BIG_ENDIAN)
6782 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6784 break;
6786 default:
6787 return x;
6789 /* Convert sign extension to zero extension, if we know that the high
6790 bit is not set, as this is easier to optimize. It will be converted
6791 back to cheaper alternative in make_extraction. */
6792 if (GET_CODE (x) == SIGN_EXTEND
6793 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6794 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6795 & ~(((unsigned HOST_WIDE_INT)
6796 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6797 >> 1))
6798 == 0)))
6800 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6801 rtx temp2 = expand_compound_operation (temp);
6803 /* Make sure this is a profitable operation. */
6804 if (set_src_cost (x, optimize_this_for_speed_p)
6805 > set_src_cost (temp2, optimize_this_for_speed_p))
6806 return temp2;
6807 else if (set_src_cost (x, optimize_this_for_speed_p)
6808 > set_src_cost (temp, optimize_this_for_speed_p))
6809 return temp;
6810 else
6811 return x;
6814 /* We can optimize some special cases of ZERO_EXTEND. */
6815 if (GET_CODE (x) == ZERO_EXTEND)
6817 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6818 know that the last value didn't have any inappropriate bits
6819 set. */
6820 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6821 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6822 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6823 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6824 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6825 return XEXP (XEXP (x, 0), 0);
6827 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6828 if (GET_CODE (XEXP (x, 0)) == SUBREG
6829 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6830 && subreg_lowpart_p (XEXP (x, 0))
6831 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6832 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6833 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6834 return SUBREG_REG (XEXP (x, 0));
6836 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6837 is a comparison and STORE_FLAG_VALUE permits. This is like
6838 the first case, but it works even when GET_MODE (x) is larger
6839 than HOST_WIDE_INT. */
6840 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6841 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6842 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6843 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6844 <= HOST_BITS_PER_WIDE_INT)
6845 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6846 return XEXP (XEXP (x, 0), 0);
6848 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6849 if (GET_CODE (XEXP (x, 0)) == SUBREG
6850 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6851 && subreg_lowpart_p (XEXP (x, 0))
6852 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6853 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6854 <= HOST_BITS_PER_WIDE_INT)
6855 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6856 return SUBREG_REG (XEXP (x, 0));
6860 /* If we reach here, we want to return a pair of shifts. The inner
6861 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6862 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6863 logical depending on the value of UNSIGNEDP.
6865 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6866 converted into an AND of a shift.
6868 We must check for the case where the left shift would have a negative
6869 count. This can happen in a case like (x >> 31) & 255 on machines
6870 that can't shift by a constant. On those machines, we would first
6871 combine the shift with the AND to produce a variable-position
6872 extraction. Then the constant of 31 would be substituted in
6873 to produce such a position. */
6875 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6876 if (modewidth >= pos + len)
6878 enum machine_mode mode = GET_MODE (x);
6879 tem = gen_lowpart (mode, XEXP (x, 0));
6880 if (!tem || GET_CODE (tem) == CLOBBER)
6881 return x;
6882 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6883 tem, modewidth - pos - len);
6884 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6885 mode, tem, modewidth - len);
6887 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6888 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6889 simplify_shift_const (NULL_RTX, LSHIFTRT,
6890 GET_MODE (x),
6891 XEXP (x, 0), pos),
6892 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6893 else
6894 /* Any other cases we can't handle. */
6895 return x;
6897 /* If we couldn't do this for some reason, return the original
6898 expression. */
6899 if (GET_CODE (tem) == CLOBBER)
6900 return x;
6902 return tem;
6905 /* X is a SET which contains an assignment of one object into
6906 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6907 or certain SUBREGS). If possible, convert it into a series of
6908 logical operations.
6910 We half-heartedly support variable positions, but do not at all
6911 support variable lengths. */
6913 static const_rtx
6914 expand_field_assignment (const_rtx x)
6916 rtx inner;
6917 rtx pos; /* Always counts from low bit. */
6918 int len;
6919 rtx mask, cleared, masked;
6920 enum machine_mode compute_mode;
6922 /* Loop until we find something we can't simplify. */
6923 while (1)
6925 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6926 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6928 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6929 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
6930 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6932 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6933 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6935 inner = XEXP (SET_DEST (x), 0);
6936 len = INTVAL (XEXP (SET_DEST (x), 1));
6937 pos = XEXP (SET_DEST (x), 2);
6939 /* A constant position should stay within the width of INNER. */
6940 if (CONST_INT_P (pos)
6941 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
6942 break;
6944 if (BITS_BIG_ENDIAN)
6946 if (CONST_INT_P (pos))
6947 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
6948 - INTVAL (pos));
6949 else if (GET_CODE (pos) == MINUS
6950 && CONST_INT_P (XEXP (pos, 1))
6951 && (INTVAL (XEXP (pos, 1))
6952 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
6953 /* If position is ADJUST - X, new position is X. */
6954 pos = XEXP (pos, 0);
6955 else
6957 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
6958 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6959 gen_int_mode (prec - len,
6960 GET_MODE (pos)),
6961 pos);
6966 /* A SUBREG between two modes that occupy the same numbers of words
6967 can be done by moving the SUBREG to the source. */
6968 else if (GET_CODE (SET_DEST (x)) == SUBREG
6969 /* We need SUBREGs to compute nonzero_bits properly. */
6970 && nonzero_sign_valid
6971 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6972 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6973 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6974 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6976 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6977 gen_lowpart
6978 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6979 SET_SRC (x)));
6980 continue;
6982 else
6983 break;
6985 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6986 inner = SUBREG_REG (inner);
6988 compute_mode = GET_MODE (inner);
6990 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6991 if (! SCALAR_INT_MODE_P (compute_mode))
6993 enum machine_mode imode;
6995 /* Don't do anything for vector or complex integral types. */
6996 if (! FLOAT_MODE_P (compute_mode))
6997 break;
6999 /* Try to find an integral mode to pun with. */
7000 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7001 if (imode == BLKmode)
7002 break;
7004 compute_mode = imode;
7005 inner = gen_lowpart (imode, inner);
7008 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7009 if (len >= HOST_BITS_PER_WIDE_INT)
7010 break;
7012 /* Now compute the equivalent expression. Make a copy of INNER
7013 for the SET_DEST in case it is a MEM into which we will substitute;
7014 we don't want shared RTL in that case. */
7015 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7016 compute_mode);
7017 cleared = simplify_gen_binary (AND, compute_mode,
7018 simplify_gen_unary (NOT, compute_mode,
7019 simplify_gen_binary (ASHIFT,
7020 compute_mode,
7021 mask, pos),
7022 compute_mode),
7023 inner);
7024 masked = simplify_gen_binary (ASHIFT, compute_mode,
7025 simplify_gen_binary (
7026 AND, compute_mode,
7027 gen_lowpart (compute_mode, SET_SRC (x)),
7028 mask),
7029 pos);
7031 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7032 simplify_gen_binary (IOR, compute_mode,
7033 cleared, masked));
7036 return x;
7039 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7040 it is an RTX that represents the (variable) starting position; otherwise,
7041 POS is the (constant) starting bit position. Both are counted from the LSB.
7043 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7045 IN_DEST is nonzero if this is a reference in the destination of a SET.
7046 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7047 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7048 be used.
7050 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7051 ZERO_EXTRACT should be built even for bits starting at bit 0.
7053 MODE is the desired mode of the result (if IN_DEST == 0).
7055 The result is an RTX for the extraction or NULL_RTX if the target
7056 can't handle it. */
7058 static rtx
7059 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7060 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7061 int in_dest, int in_compare)
7063 /* This mode describes the size of the storage area
7064 to fetch the overall value from. Within that, we
7065 ignore the POS lowest bits, etc. */
7066 enum machine_mode is_mode = GET_MODE (inner);
7067 enum machine_mode inner_mode;
7068 enum machine_mode wanted_inner_mode;
7069 enum machine_mode wanted_inner_reg_mode = word_mode;
7070 enum machine_mode pos_mode = word_mode;
7071 enum machine_mode extraction_mode = word_mode;
7072 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7073 rtx new_rtx = 0;
7074 rtx orig_pos_rtx = pos_rtx;
7075 HOST_WIDE_INT orig_pos;
7077 if (pos_rtx && CONST_INT_P (pos_rtx))
7078 pos = INTVAL (pos_rtx), pos_rtx = 0;
7080 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7082 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7083 consider just the QI as the memory to extract from.
7084 The subreg adds or removes high bits; its mode is
7085 irrelevant to the meaning of this extraction,
7086 since POS and LEN count from the lsb. */
7087 if (MEM_P (SUBREG_REG (inner)))
7088 is_mode = GET_MODE (SUBREG_REG (inner));
7089 inner = SUBREG_REG (inner);
7091 else if (GET_CODE (inner) == ASHIFT
7092 && CONST_INT_P (XEXP (inner, 1))
7093 && pos_rtx == 0 && pos == 0
7094 && len > UINTVAL (XEXP (inner, 1)))
7096 /* We're extracting the least significant bits of an rtx
7097 (ashift X (const_int C)), where LEN > C. Extract the
7098 least significant (LEN - C) bits of X, giving an rtx
7099 whose mode is MODE, then shift it left C times. */
7100 new_rtx = make_extraction (mode, XEXP (inner, 0),
7101 0, 0, len - INTVAL (XEXP (inner, 1)),
7102 unsignedp, in_dest, in_compare);
7103 if (new_rtx != 0)
7104 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7106 else if (GET_CODE (inner) == TRUNCATE)
7107 inner = XEXP (inner, 0);
7109 inner_mode = GET_MODE (inner);
7111 /* See if this can be done without an extraction. We never can if the
7112 width of the field is not the same as that of some integer mode. For
7113 registers, we can only avoid the extraction if the position is at the
7114 low-order bit and this is either not in the destination or we have the
7115 appropriate STRICT_LOW_PART operation available.
7117 For MEM, we can avoid an extract if the field starts on an appropriate
7118 boundary and we can change the mode of the memory reference. */
7120 if (tmode != BLKmode
7121 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7122 && !MEM_P (inner)
7123 && (inner_mode == tmode
7124 || !REG_P (inner)
7125 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7126 || reg_truncated_to_mode (tmode, inner))
7127 && (! in_dest
7128 || (REG_P (inner)
7129 && have_insn_for (STRICT_LOW_PART, tmode))))
7130 || (MEM_P (inner) && pos_rtx == 0
7131 && (pos
7132 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7133 : BITS_PER_UNIT)) == 0
7134 /* We can't do this if we are widening INNER_MODE (it
7135 may not be aligned, for one thing). */
7136 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7137 && (inner_mode == tmode
7138 || (! mode_dependent_address_p (XEXP (inner, 0),
7139 MEM_ADDR_SPACE (inner))
7140 && ! MEM_VOLATILE_P (inner))))))
7142 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7143 field. If the original and current mode are the same, we need not
7144 adjust the offset. Otherwise, we do if bytes big endian.
7146 If INNER is not a MEM, get a piece consisting of just the field
7147 of interest (in this case POS % BITS_PER_WORD must be 0). */
7149 if (MEM_P (inner))
7151 HOST_WIDE_INT offset;
7153 /* POS counts from lsb, but make OFFSET count in memory order. */
7154 if (BYTES_BIG_ENDIAN)
7155 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7156 else
7157 offset = pos / BITS_PER_UNIT;
7159 new_rtx = adjust_address_nv (inner, tmode, offset);
7161 else if (REG_P (inner))
7163 if (tmode != inner_mode)
7165 /* We can't call gen_lowpart in a DEST since we
7166 always want a SUBREG (see below) and it would sometimes
7167 return a new hard register. */
7168 if (pos || in_dest)
7170 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7172 if (WORDS_BIG_ENDIAN
7173 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7174 final_word = ((GET_MODE_SIZE (inner_mode)
7175 - GET_MODE_SIZE (tmode))
7176 / UNITS_PER_WORD) - final_word;
7178 final_word *= UNITS_PER_WORD;
7179 if (BYTES_BIG_ENDIAN &&
7180 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7181 final_word += (GET_MODE_SIZE (inner_mode)
7182 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7184 /* Avoid creating invalid subregs, for example when
7185 simplifying (x>>32)&255. */
7186 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7187 return NULL_RTX;
7189 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7191 else
7192 new_rtx = gen_lowpart (tmode, inner);
7194 else
7195 new_rtx = inner;
7197 else
7198 new_rtx = force_to_mode (inner, tmode,
7199 len >= HOST_BITS_PER_WIDE_INT
7200 ? ~(unsigned HOST_WIDE_INT) 0
7201 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7204 /* If this extraction is going into the destination of a SET,
7205 make a STRICT_LOW_PART unless we made a MEM. */
7207 if (in_dest)
7208 return (MEM_P (new_rtx) ? new_rtx
7209 : (GET_CODE (new_rtx) != SUBREG
7210 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7211 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7213 if (mode == tmode)
7214 return new_rtx;
7216 if (CONST_SCALAR_INT_P (new_rtx))
7217 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7218 mode, new_rtx, tmode);
7220 /* If we know that no extraneous bits are set, and that the high
7221 bit is not set, convert the extraction to the cheaper of
7222 sign and zero extension, that are equivalent in these cases. */
7223 if (flag_expensive_optimizations
7224 && (HWI_COMPUTABLE_MODE_P (tmode)
7225 && ((nonzero_bits (new_rtx, tmode)
7226 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7227 == 0)))
7229 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7230 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7232 /* Prefer ZERO_EXTENSION, since it gives more information to
7233 backends. */
7234 if (set_src_cost (temp, optimize_this_for_speed_p)
7235 <= set_src_cost (temp1, optimize_this_for_speed_p))
7236 return temp;
7237 return temp1;
7240 /* Otherwise, sign- or zero-extend unless we already are in the
7241 proper mode. */
7243 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7244 mode, new_rtx));
7247 /* Unless this is a COMPARE or we have a funny memory reference,
7248 don't do anything with zero-extending field extracts starting at
7249 the low-order bit since they are simple AND operations. */
7250 if (pos_rtx == 0 && pos == 0 && ! in_dest
7251 && ! in_compare && unsignedp)
7252 return 0;
7254 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7255 if the position is not a constant and the length is not 1. In all
7256 other cases, we would only be going outside our object in cases when
7257 an original shift would have been undefined. */
7258 if (MEM_P (inner)
7259 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7260 || (pos_rtx != 0 && len != 1)))
7261 return 0;
7263 enum extraction_pattern pattern = (in_dest ? EP_insv
7264 : unsignedp ? EP_extzv : EP_extv);
7266 /* If INNER is not from memory, we want it to have the mode of a register
7267 extraction pattern's structure operand, or word_mode if there is no
7268 such pattern. The same applies to extraction_mode and pos_mode
7269 and their respective operands.
7271 For memory, assume that the desired extraction_mode and pos_mode
7272 are the same as for a register operation, since at present we don't
7273 have named patterns for aligned memory structures. */
7274 struct extraction_insn insn;
7275 if (get_best_reg_extraction_insn (&insn, pattern,
7276 GET_MODE_BITSIZE (inner_mode), mode))
7278 wanted_inner_reg_mode = insn.struct_mode;
7279 pos_mode = insn.pos_mode;
7280 extraction_mode = insn.field_mode;
7283 /* Never narrow an object, since that might not be safe. */
7285 if (mode != VOIDmode
7286 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7287 extraction_mode = mode;
7289 if (!MEM_P (inner))
7290 wanted_inner_mode = wanted_inner_reg_mode;
7291 else
7293 /* Be careful not to go beyond the extracted object and maintain the
7294 natural alignment of the memory. */
7295 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7296 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7297 > GET_MODE_BITSIZE (wanted_inner_mode))
7299 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7300 gcc_assert (wanted_inner_mode != VOIDmode);
7304 orig_pos = pos;
7306 if (BITS_BIG_ENDIAN)
7308 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7309 BITS_BIG_ENDIAN style. If position is constant, compute new
7310 position. Otherwise, build subtraction.
7311 Note that POS is relative to the mode of the original argument.
7312 If it's a MEM we need to recompute POS relative to that.
7313 However, if we're extracting from (or inserting into) a register,
7314 we want to recompute POS relative to wanted_inner_mode. */
7315 int width = (MEM_P (inner)
7316 ? GET_MODE_BITSIZE (is_mode)
7317 : GET_MODE_BITSIZE (wanted_inner_mode));
7319 if (pos_rtx == 0)
7320 pos = width - len - pos;
7321 else
7322 pos_rtx
7323 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7324 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7325 pos_rtx);
7326 /* POS may be less than 0 now, but we check for that below.
7327 Note that it can only be less than 0 if !MEM_P (inner). */
7330 /* If INNER has a wider mode, and this is a constant extraction, try to
7331 make it smaller and adjust the byte to point to the byte containing
7332 the value. */
7333 if (wanted_inner_mode != VOIDmode
7334 && inner_mode != wanted_inner_mode
7335 && ! pos_rtx
7336 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7337 && MEM_P (inner)
7338 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7339 && ! MEM_VOLATILE_P (inner))
7341 int offset = 0;
7343 /* The computations below will be correct if the machine is big
7344 endian in both bits and bytes or little endian in bits and bytes.
7345 If it is mixed, we must adjust. */
7347 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7348 adjust OFFSET to compensate. */
7349 if (BYTES_BIG_ENDIAN
7350 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7351 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7353 /* We can now move to the desired byte. */
7354 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7355 * GET_MODE_SIZE (wanted_inner_mode);
7356 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7358 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7359 && is_mode != wanted_inner_mode)
7360 offset = (GET_MODE_SIZE (is_mode)
7361 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7363 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7366 /* If INNER is not memory, get it into the proper mode. If we are changing
7367 its mode, POS must be a constant and smaller than the size of the new
7368 mode. */
7369 else if (!MEM_P (inner))
7371 /* On the LHS, don't create paradoxical subregs implicitely truncating
7372 the register unless TRULY_NOOP_TRUNCATION. */
7373 if (in_dest
7374 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7375 wanted_inner_mode))
7376 return NULL_RTX;
7378 if (GET_MODE (inner) != wanted_inner_mode
7379 && (pos_rtx != 0
7380 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7381 return NULL_RTX;
7383 if (orig_pos < 0)
7384 return NULL_RTX;
7386 inner = force_to_mode (inner, wanted_inner_mode,
7387 pos_rtx
7388 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7389 ? ~(unsigned HOST_WIDE_INT) 0
7390 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7391 << orig_pos),
7395 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7396 have to zero extend. Otherwise, we can just use a SUBREG. */
7397 if (pos_rtx != 0
7398 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7400 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7401 GET_MODE (pos_rtx));
7403 /* If we know that no extraneous bits are set, and that the high
7404 bit is not set, convert extraction to cheaper one - either
7405 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7406 cases. */
7407 if (flag_expensive_optimizations
7408 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7409 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7410 & ~(((unsigned HOST_WIDE_INT)
7411 GET_MODE_MASK (GET_MODE (pos_rtx)))
7412 >> 1))
7413 == 0)))
7415 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7416 GET_MODE (pos_rtx));
7418 /* Prefer ZERO_EXTENSION, since it gives more information to
7419 backends. */
7420 if (set_src_cost (temp1, optimize_this_for_speed_p)
7421 < set_src_cost (temp, optimize_this_for_speed_p))
7422 temp = temp1;
7424 pos_rtx = temp;
7427 /* Make POS_RTX unless we already have it and it is correct. If we don't
7428 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7429 be a CONST_INT. */
7430 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7431 pos_rtx = orig_pos_rtx;
7433 else if (pos_rtx == 0)
7434 pos_rtx = GEN_INT (pos);
7436 /* Make the required operation. See if we can use existing rtx. */
7437 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7438 extraction_mode, inner, GEN_INT (len), pos_rtx);
7439 if (! in_dest)
7440 new_rtx = gen_lowpart (mode, new_rtx);
7442 return new_rtx;
7445 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7446 with any other operations in X. Return X without that shift if so. */
7448 static rtx
7449 extract_left_shift (rtx x, int count)
7451 enum rtx_code code = GET_CODE (x);
7452 enum machine_mode mode = GET_MODE (x);
7453 rtx tem;
7455 switch (code)
7457 case ASHIFT:
7458 /* This is the shift itself. If it is wide enough, we will return
7459 either the value being shifted if the shift count is equal to
7460 COUNT or a shift for the difference. */
7461 if (CONST_INT_P (XEXP (x, 1))
7462 && INTVAL (XEXP (x, 1)) >= count)
7463 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7464 INTVAL (XEXP (x, 1)) - count);
7465 break;
7467 case NEG: case NOT:
7468 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7469 return simplify_gen_unary (code, mode, tem, mode);
7471 break;
7473 case PLUS: case IOR: case XOR: case AND:
7474 /* If we can safely shift this constant and we find the inner shift,
7475 make a new operation. */
7476 if (CONST_INT_P (XEXP (x, 1))
7477 && (UINTVAL (XEXP (x, 1))
7478 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7479 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7481 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7482 return simplify_gen_binary (code, mode, tem,
7483 gen_int_mode (val, mode));
7485 break;
7487 default:
7488 break;
7491 return 0;
7494 /* Look at the expression rooted at X. Look for expressions
7495 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7496 Form these expressions.
7498 Return the new rtx, usually just X.
7500 Also, for machines like the VAX that don't have logical shift insns,
7501 try to convert logical to arithmetic shift operations in cases where
7502 they are equivalent. This undoes the canonicalizations to logical
7503 shifts done elsewhere.
7505 We try, as much as possible, to re-use rtl expressions to save memory.
7507 IN_CODE says what kind of expression we are processing. Normally, it is
7508 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7509 being kludges), it is MEM. When processing the arguments of a comparison
7510 or a COMPARE against zero, it is COMPARE. */
7513 make_compound_operation (rtx x, enum rtx_code in_code)
7515 enum rtx_code code = GET_CODE (x);
7516 enum machine_mode mode = GET_MODE (x);
7517 int mode_width = GET_MODE_PRECISION (mode);
7518 rtx rhs, lhs;
7519 enum rtx_code next_code;
7520 int i, j;
7521 rtx new_rtx = 0;
7522 rtx tem;
7523 const char *fmt;
7525 /* Select the code to be used in recursive calls. Once we are inside an
7526 address, we stay there. If we have a comparison, set to COMPARE,
7527 but once inside, go back to our default of SET. */
7529 next_code = (code == MEM ? MEM
7530 : ((code == PLUS || code == MINUS)
7531 && SCALAR_INT_MODE_P (mode)) ? MEM
7532 : ((code == COMPARE || COMPARISON_P (x))
7533 && XEXP (x, 1) == const0_rtx) ? COMPARE
7534 : in_code == COMPARE ? SET : in_code);
7536 /* Process depending on the code of this operation. If NEW is set
7537 nonzero, it will be returned. */
7539 switch (code)
7541 case ASHIFT:
7542 /* Convert shifts by constants into multiplications if inside
7543 an address. */
7544 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7545 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7546 && INTVAL (XEXP (x, 1)) >= 0
7547 && SCALAR_INT_MODE_P (mode))
7549 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7550 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7552 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7553 if (GET_CODE (new_rtx) == NEG)
7555 new_rtx = XEXP (new_rtx, 0);
7556 multval = -multval;
7558 multval = trunc_int_for_mode (multval, mode);
7559 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7561 break;
7563 case PLUS:
7564 lhs = XEXP (x, 0);
7565 rhs = XEXP (x, 1);
7566 lhs = make_compound_operation (lhs, next_code);
7567 rhs = make_compound_operation (rhs, next_code);
7568 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7569 && SCALAR_INT_MODE_P (mode))
7571 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7572 XEXP (lhs, 1));
7573 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7575 else if (GET_CODE (lhs) == MULT
7576 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7578 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7579 simplify_gen_unary (NEG, mode,
7580 XEXP (lhs, 1),
7581 mode));
7582 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7584 else
7586 SUBST (XEXP (x, 0), lhs);
7587 SUBST (XEXP (x, 1), rhs);
7588 goto maybe_swap;
7590 x = gen_lowpart (mode, new_rtx);
7591 goto maybe_swap;
7593 case MINUS:
7594 lhs = XEXP (x, 0);
7595 rhs = XEXP (x, 1);
7596 lhs = make_compound_operation (lhs, next_code);
7597 rhs = make_compound_operation (rhs, next_code);
7598 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7599 && SCALAR_INT_MODE_P (mode))
7601 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7602 XEXP (rhs, 1));
7603 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7605 else if (GET_CODE (rhs) == MULT
7606 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7608 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7609 simplify_gen_unary (NEG, mode,
7610 XEXP (rhs, 1),
7611 mode));
7612 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7614 else
7616 SUBST (XEXP (x, 0), lhs);
7617 SUBST (XEXP (x, 1), rhs);
7618 return x;
7620 return gen_lowpart (mode, new_rtx);
7622 case AND:
7623 /* If the second operand is not a constant, we can't do anything
7624 with it. */
7625 if (!CONST_INT_P (XEXP (x, 1)))
7626 break;
7628 /* If the constant is a power of two minus one and the first operand
7629 is a logical right shift, make an extraction. */
7630 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7631 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7633 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7634 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7635 0, in_code == COMPARE);
7638 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7639 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7640 && subreg_lowpart_p (XEXP (x, 0))
7641 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7642 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7644 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7645 next_code);
7646 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7647 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7648 0, in_code == COMPARE);
7650 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7651 else if ((GET_CODE (XEXP (x, 0)) == XOR
7652 || GET_CODE (XEXP (x, 0)) == IOR)
7653 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7654 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7655 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7657 /* Apply the distributive law, and then try to make extractions. */
7658 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7659 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7660 XEXP (x, 1)),
7661 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7662 XEXP (x, 1)));
7663 new_rtx = make_compound_operation (new_rtx, in_code);
7666 /* If we are have (and (rotate X C) M) and C is larger than the number
7667 of bits in M, this is an extraction. */
7669 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7670 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7671 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7672 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7674 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7675 new_rtx = make_extraction (mode, new_rtx,
7676 (GET_MODE_PRECISION (mode)
7677 - INTVAL (XEXP (XEXP (x, 0), 1))),
7678 NULL_RTX, i, 1, 0, in_code == COMPARE);
7681 /* On machines without logical shifts, if the operand of the AND is
7682 a logical shift and our mask turns off all the propagated sign
7683 bits, we can replace the logical shift with an arithmetic shift. */
7684 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7685 && !have_insn_for (LSHIFTRT, mode)
7686 && have_insn_for (ASHIFTRT, mode)
7687 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7688 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7689 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7690 && mode_width <= HOST_BITS_PER_WIDE_INT)
7692 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7694 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7695 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7696 SUBST (XEXP (x, 0),
7697 gen_rtx_ASHIFTRT (mode,
7698 make_compound_operation
7699 (XEXP (XEXP (x, 0), 0), next_code),
7700 XEXP (XEXP (x, 0), 1)));
7703 /* If the constant is one less than a power of two, this might be
7704 representable by an extraction even if no shift is present.
7705 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7706 we are in a COMPARE. */
7707 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7708 new_rtx = make_extraction (mode,
7709 make_compound_operation (XEXP (x, 0),
7710 next_code),
7711 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7713 /* If we are in a comparison and this is an AND with a power of two,
7714 convert this into the appropriate bit extract. */
7715 else if (in_code == COMPARE
7716 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7717 new_rtx = make_extraction (mode,
7718 make_compound_operation (XEXP (x, 0),
7719 next_code),
7720 i, NULL_RTX, 1, 1, 0, 1);
7722 break;
7724 case LSHIFTRT:
7725 /* If the sign bit is known to be zero, replace this with an
7726 arithmetic shift. */
7727 if (have_insn_for (ASHIFTRT, mode)
7728 && ! have_insn_for (LSHIFTRT, mode)
7729 && mode_width <= HOST_BITS_PER_WIDE_INT
7730 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7732 new_rtx = gen_rtx_ASHIFTRT (mode,
7733 make_compound_operation (XEXP (x, 0),
7734 next_code),
7735 XEXP (x, 1));
7736 break;
7739 /* ... fall through ... */
7741 case ASHIFTRT:
7742 lhs = XEXP (x, 0);
7743 rhs = XEXP (x, 1);
7745 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7746 this is a SIGN_EXTRACT. */
7747 if (CONST_INT_P (rhs)
7748 && GET_CODE (lhs) == ASHIFT
7749 && CONST_INT_P (XEXP (lhs, 1))
7750 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7751 && INTVAL (XEXP (lhs, 1)) >= 0
7752 && INTVAL (rhs) < mode_width)
7754 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7755 new_rtx = make_extraction (mode, new_rtx,
7756 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7757 NULL_RTX, mode_width - INTVAL (rhs),
7758 code == LSHIFTRT, 0, in_code == COMPARE);
7759 break;
7762 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7763 If so, try to merge the shifts into a SIGN_EXTEND. We could
7764 also do this for some cases of SIGN_EXTRACT, but it doesn't
7765 seem worth the effort; the case checked for occurs on Alpha. */
7767 if (!OBJECT_P (lhs)
7768 && ! (GET_CODE (lhs) == SUBREG
7769 && (OBJECT_P (SUBREG_REG (lhs))))
7770 && CONST_INT_P (rhs)
7771 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7772 && INTVAL (rhs) < mode_width
7773 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7774 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7775 0, NULL_RTX, mode_width - INTVAL (rhs),
7776 code == LSHIFTRT, 0, in_code == COMPARE);
7778 break;
7780 case SUBREG:
7781 /* Call ourselves recursively on the inner expression. If we are
7782 narrowing the object and it has a different RTL code from
7783 what it originally did, do this SUBREG as a force_to_mode. */
7785 rtx inner = SUBREG_REG (x), simplified;
7786 enum rtx_code subreg_code = in_code;
7788 /* If in_code is COMPARE, it isn't always safe to pass it through
7789 to the recursive make_compound_operation call. */
7790 if (subreg_code == COMPARE
7791 && (!subreg_lowpart_p (x)
7792 || GET_CODE (inner) == SUBREG
7793 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7794 is (const_int 0), rather than
7795 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7796 || (GET_CODE (inner) == AND
7797 && CONST_INT_P (XEXP (inner, 1))
7798 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7799 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7800 >= GET_MODE_BITSIZE (mode))))
7801 subreg_code = SET;
7803 tem = make_compound_operation (inner, subreg_code);
7805 simplified
7806 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7807 if (simplified)
7808 tem = simplified;
7810 if (GET_CODE (tem) != GET_CODE (inner)
7811 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7812 && subreg_lowpart_p (x))
7814 rtx newer
7815 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7817 /* If we have something other than a SUBREG, we might have
7818 done an expansion, so rerun ourselves. */
7819 if (GET_CODE (newer) != SUBREG)
7820 newer = make_compound_operation (newer, in_code);
7822 /* force_to_mode can expand compounds. If it just re-expanded the
7823 compound, use gen_lowpart to convert to the desired mode. */
7824 if (rtx_equal_p (newer, x)
7825 /* Likewise if it re-expanded the compound only partially.
7826 This happens for SUBREG of ZERO_EXTRACT if they extract
7827 the same number of bits. */
7828 || (GET_CODE (newer) == SUBREG
7829 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7830 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7831 && GET_CODE (inner) == AND
7832 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7833 return gen_lowpart (GET_MODE (x), tem);
7835 return newer;
7838 if (simplified)
7839 return tem;
7841 break;
7843 default:
7844 break;
7847 if (new_rtx)
7849 x = gen_lowpart (mode, new_rtx);
7850 code = GET_CODE (x);
7853 /* Now recursively process each operand of this operation. We need to
7854 handle ZERO_EXTEND specially so that we don't lose track of the
7855 inner mode. */
7856 if (GET_CODE (x) == ZERO_EXTEND)
7858 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7859 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7860 new_rtx, GET_MODE (XEXP (x, 0)));
7861 if (tem)
7862 return tem;
7863 SUBST (XEXP (x, 0), new_rtx);
7864 return x;
7867 fmt = GET_RTX_FORMAT (code);
7868 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7869 if (fmt[i] == 'e')
7871 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7872 SUBST (XEXP (x, i), new_rtx);
7874 else if (fmt[i] == 'E')
7875 for (j = 0; j < XVECLEN (x, i); j++)
7877 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7878 SUBST (XVECEXP (x, i, j), new_rtx);
7881 maybe_swap:
7882 /* If this is a commutative operation, the changes to the operands
7883 may have made it noncanonical. */
7884 if (COMMUTATIVE_ARITH_P (x)
7885 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7887 tem = XEXP (x, 0);
7888 SUBST (XEXP (x, 0), XEXP (x, 1));
7889 SUBST (XEXP (x, 1), tem);
7892 return x;
7895 /* Given M see if it is a value that would select a field of bits
7896 within an item, but not the entire word. Return -1 if not.
7897 Otherwise, return the starting position of the field, where 0 is the
7898 low-order bit.
7900 *PLEN is set to the length of the field. */
7902 static int
7903 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7905 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7906 int pos = m ? ctz_hwi (m) : -1;
7907 int len = 0;
7909 if (pos >= 0)
7910 /* Now shift off the low-order zero bits and see if we have a
7911 power of two minus 1. */
7912 len = exact_log2 ((m >> pos) + 1);
7914 if (len <= 0)
7915 pos = -1;
7917 *plen = len;
7918 return pos;
7921 /* If X refers to a register that equals REG in value, replace these
7922 references with REG. */
7923 static rtx
7924 canon_reg_for_combine (rtx x, rtx reg)
7926 rtx op0, op1, op2;
7927 const char *fmt;
7928 int i;
7929 bool copied;
7931 enum rtx_code code = GET_CODE (x);
7932 switch (GET_RTX_CLASS (code))
7934 case RTX_UNARY:
7935 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7936 if (op0 != XEXP (x, 0))
7937 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7938 GET_MODE (reg));
7939 break;
7941 case RTX_BIN_ARITH:
7942 case RTX_COMM_ARITH:
7943 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7944 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7945 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7946 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7947 break;
7949 case RTX_COMPARE:
7950 case RTX_COMM_COMPARE:
7951 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7952 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7953 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7954 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7955 GET_MODE (op0), op0, op1);
7956 break;
7958 case RTX_TERNARY:
7959 case RTX_BITFIELD_OPS:
7960 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7961 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7962 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7963 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7964 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7965 GET_MODE (op0), op0, op1, op2);
7967 case RTX_OBJ:
7968 if (REG_P (x))
7970 if (rtx_equal_p (get_last_value (reg), x)
7971 || rtx_equal_p (reg, get_last_value (x)))
7972 return reg;
7973 else
7974 break;
7977 /* fall through */
7979 default:
7980 fmt = GET_RTX_FORMAT (code);
7981 copied = false;
7982 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7983 if (fmt[i] == 'e')
7985 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7986 if (op != XEXP (x, i))
7988 if (!copied)
7990 copied = true;
7991 x = copy_rtx (x);
7993 XEXP (x, i) = op;
7996 else if (fmt[i] == 'E')
7998 int j;
7999 for (j = 0; j < XVECLEN (x, i); j++)
8001 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8002 if (op != XVECEXP (x, i, j))
8004 if (!copied)
8006 copied = true;
8007 x = copy_rtx (x);
8009 XVECEXP (x, i, j) = op;
8014 break;
8017 return x;
8020 /* Return X converted to MODE. If the value is already truncated to
8021 MODE we can just return a subreg even though in the general case we
8022 would need an explicit truncation. */
8024 static rtx
8025 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
8027 if (!CONST_INT_P (x)
8028 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8029 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8030 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8032 /* Bit-cast X into an integer mode. */
8033 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8034 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8035 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8036 x, GET_MODE (x));
8039 return gen_lowpart (mode, x);
8042 /* See if X can be simplified knowing that we will only refer to it in
8043 MODE and will only refer to those bits that are nonzero in MASK.
8044 If other bits are being computed or if masking operations are done
8045 that select a superset of the bits in MASK, they can sometimes be
8046 ignored.
8048 Return a possibly simplified expression, but always convert X to
8049 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8051 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8052 are all off in X. This is used when X will be complemented, by either
8053 NOT, NEG, or XOR. */
8055 static rtx
8056 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
8057 int just_select)
8059 enum rtx_code code = GET_CODE (x);
8060 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8061 enum machine_mode op_mode;
8062 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8063 rtx op0, op1, temp;
8065 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8066 code below will do the wrong thing since the mode of such an
8067 expression is VOIDmode.
8069 Also do nothing if X is a CLOBBER; this can happen if X was
8070 the return value from a call to gen_lowpart. */
8071 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8072 return x;
8074 /* We want to perform the operation in its present mode unless we know
8075 that the operation is valid in MODE, in which case we do the operation
8076 in MODE. */
8077 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8078 && have_insn_for (code, mode))
8079 ? mode : GET_MODE (x));
8081 /* It is not valid to do a right-shift in a narrower mode
8082 than the one it came in with. */
8083 if ((code == LSHIFTRT || code == ASHIFTRT)
8084 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8085 op_mode = GET_MODE (x);
8087 /* Truncate MASK to fit OP_MODE. */
8088 if (op_mode)
8089 mask &= GET_MODE_MASK (op_mode);
8091 /* When we have an arithmetic operation, or a shift whose count we
8092 do not know, we need to assume that all bits up to the highest-order
8093 bit in MASK will be needed. This is how we form such a mask. */
8094 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8095 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8096 else
8097 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8098 - 1);
8100 /* Determine what bits of X are guaranteed to be (non)zero. */
8101 nonzero = nonzero_bits (x, mode);
8103 /* If none of the bits in X are needed, return a zero. */
8104 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8105 x = const0_rtx;
8107 /* If X is a CONST_INT, return a new one. Do this here since the
8108 test below will fail. */
8109 if (CONST_INT_P (x))
8111 if (SCALAR_INT_MODE_P (mode))
8112 return gen_int_mode (INTVAL (x) & mask, mode);
8113 else
8115 x = GEN_INT (INTVAL (x) & mask);
8116 return gen_lowpart_common (mode, x);
8120 /* If X is narrower than MODE and we want all the bits in X's mode, just
8121 get X in the proper mode. */
8122 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8123 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8124 return gen_lowpart (mode, x);
8126 /* We can ignore the effect of a SUBREG if it narrows the mode or
8127 if the constant masks to zero all the bits the mode doesn't have. */
8128 if (GET_CODE (x) == SUBREG
8129 && subreg_lowpart_p (x)
8130 && ((GET_MODE_SIZE (GET_MODE (x))
8131 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8132 || (0 == (mask
8133 & GET_MODE_MASK (GET_MODE (x))
8134 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8135 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8137 /* The arithmetic simplifications here only work for scalar integer modes. */
8138 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8139 return gen_lowpart_or_truncate (mode, x);
8141 switch (code)
8143 case CLOBBER:
8144 /* If X is a (clobber (const_int)), return it since we know we are
8145 generating something that won't match. */
8146 return x;
8148 case SIGN_EXTEND:
8149 case ZERO_EXTEND:
8150 case ZERO_EXTRACT:
8151 case SIGN_EXTRACT:
8152 x = expand_compound_operation (x);
8153 if (GET_CODE (x) != code)
8154 return force_to_mode (x, mode, mask, next_select);
8155 break;
8157 case TRUNCATE:
8158 /* Similarly for a truncate. */
8159 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8161 case AND:
8162 /* If this is an AND with a constant, convert it into an AND
8163 whose constant is the AND of that constant with MASK. If it
8164 remains an AND of MASK, delete it since it is redundant. */
8166 if (CONST_INT_P (XEXP (x, 1)))
8168 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8169 mask & INTVAL (XEXP (x, 1)));
8171 /* If X is still an AND, see if it is an AND with a mask that
8172 is just some low-order bits. If so, and it is MASK, we don't
8173 need it. */
8175 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8176 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8177 == mask))
8178 x = XEXP (x, 0);
8180 /* If it remains an AND, try making another AND with the bits
8181 in the mode mask that aren't in MASK turned on. If the
8182 constant in the AND is wide enough, this might make a
8183 cheaper constant. */
8185 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8186 && GET_MODE_MASK (GET_MODE (x)) != mask
8187 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8189 unsigned HOST_WIDE_INT cval
8190 = UINTVAL (XEXP (x, 1))
8191 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8192 rtx y;
8194 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8195 gen_int_mode (cval, GET_MODE (x)));
8196 if (set_src_cost (y, optimize_this_for_speed_p)
8197 < set_src_cost (x, optimize_this_for_speed_p))
8198 x = y;
8201 break;
8204 goto binop;
8206 case PLUS:
8207 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8208 low-order bits (as in an alignment operation) and FOO is already
8209 aligned to that boundary, mask C1 to that boundary as well.
8210 This may eliminate that PLUS and, later, the AND. */
8213 unsigned int width = GET_MODE_PRECISION (mode);
8214 unsigned HOST_WIDE_INT smask = mask;
8216 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8217 number, sign extend it. */
8219 if (width < HOST_BITS_PER_WIDE_INT
8220 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8221 smask |= HOST_WIDE_INT_M1U << width;
8223 if (CONST_INT_P (XEXP (x, 1))
8224 && exact_log2 (- smask) >= 0
8225 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8226 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8227 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8228 (INTVAL (XEXP (x, 1)) & smask)),
8229 mode, smask, next_select);
8232 /* ... fall through ... */
8234 case MULT:
8235 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8236 most significant bit in MASK since carries from those bits will
8237 affect the bits we are interested in. */
8238 mask = fuller_mask;
8239 goto binop;
8241 case MINUS:
8242 /* If X is (minus C Y) where C's least set bit is larger than any bit
8243 in the mask, then we may replace with (neg Y). */
8244 if (CONST_INT_P (XEXP (x, 0))
8245 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8247 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8248 GET_MODE (x));
8249 return force_to_mode (x, mode, mask, next_select);
8252 /* Similarly, if C contains every bit in the fuller_mask, then we may
8253 replace with (not Y). */
8254 if (CONST_INT_P (XEXP (x, 0))
8255 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8257 x = simplify_gen_unary (NOT, GET_MODE (x),
8258 XEXP (x, 1), GET_MODE (x));
8259 return force_to_mode (x, mode, mask, next_select);
8262 mask = fuller_mask;
8263 goto binop;
8265 case IOR:
8266 case XOR:
8267 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8268 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8269 operation which may be a bitfield extraction. Ensure that the
8270 constant we form is not wider than the mode of X. */
8272 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8273 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8274 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8275 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8276 && CONST_INT_P (XEXP (x, 1))
8277 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8278 + floor_log2 (INTVAL (XEXP (x, 1))))
8279 < GET_MODE_PRECISION (GET_MODE (x)))
8280 && (UINTVAL (XEXP (x, 1))
8281 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8283 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8284 << INTVAL (XEXP (XEXP (x, 0), 1)),
8285 GET_MODE (x));
8286 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8287 XEXP (XEXP (x, 0), 0), temp);
8288 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8289 XEXP (XEXP (x, 0), 1));
8290 return force_to_mode (x, mode, mask, next_select);
8293 binop:
8294 /* For most binary operations, just propagate into the operation and
8295 change the mode if we have an operation of that mode. */
8297 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8298 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8300 /* If we ended up truncating both operands, truncate the result of the
8301 operation instead. */
8302 if (GET_CODE (op0) == TRUNCATE
8303 && GET_CODE (op1) == TRUNCATE)
8305 op0 = XEXP (op0, 0);
8306 op1 = XEXP (op1, 0);
8309 op0 = gen_lowpart_or_truncate (op_mode, op0);
8310 op1 = gen_lowpart_or_truncate (op_mode, op1);
8312 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8313 x = simplify_gen_binary (code, op_mode, op0, op1);
8314 break;
8316 case ASHIFT:
8317 /* For left shifts, do the same, but just for the first operand.
8318 However, we cannot do anything with shifts where we cannot
8319 guarantee that the counts are smaller than the size of the mode
8320 because such a count will have a different meaning in a
8321 wider mode. */
8323 if (! (CONST_INT_P (XEXP (x, 1))
8324 && INTVAL (XEXP (x, 1)) >= 0
8325 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8326 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8327 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8328 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8329 break;
8331 /* If the shift count is a constant and we can do arithmetic in
8332 the mode of the shift, refine which bits we need. Otherwise, use the
8333 conservative form of the mask. */
8334 if (CONST_INT_P (XEXP (x, 1))
8335 && INTVAL (XEXP (x, 1)) >= 0
8336 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8337 && HWI_COMPUTABLE_MODE_P (op_mode))
8338 mask >>= INTVAL (XEXP (x, 1));
8339 else
8340 mask = fuller_mask;
8342 op0 = gen_lowpart_or_truncate (op_mode,
8343 force_to_mode (XEXP (x, 0), op_mode,
8344 mask, next_select));
8346 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8347 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8348 break;
8350 case LSHIFTRT:
8351 /* Here we can only do something if the shift count is a constant,
8352 this shift constant is valid for the host, and we can do arithmetic
8353 in OP_MODE. */
8355 if (CONST_INT_P (XEXP (x, 1))
8356 && INTVAL (XEXP (x, 1)) >= 0
8357 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8358 && HWI_COMPUTABLE_MODE_P (op_mode))
8360 rtx inner = XEXP (x, 0);
8361 unsigned HOST_WIDE_INT inner_mask;
8363 /* Select the mask of the bits we need for the shift operand. */
8364 inner_mask = mask << INTVAL (XEXP (x, 1));
8366 /* We can only change the mode of the shift if we can do arithmetic
8367 in the mode of the shift and INNER_MASK is no wider than the
8368 width of X's mode. */
8369 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8370 op_mode = GET_MODE (x);
8372 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8374 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8375 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8378 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8379 shift and AND produces only copies of the sign bit (C2 is one less
8380 than a power of two), we can do this with just a shift. */
8382 if (GET_CODE (x) == LSHIFTRT
8383 && CONST_INT_P (XEXP (x, 1))
8384 /* The shift puts one of the sign bit copies in the least significant
8385 bit. */
8386 && ((INTVAL (XEXP (x, 1))
8387 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8388 >= GET_MODE_PRECISION (GET_MODE (x)))
8389 && exact_log2 (mask + 1) >= 0
8390 /* Number of bits left after the shift must be more than the mask
8391 needs. */
8392 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8393 <= GET_MODE_PRECISION (GET_MODE (x)))
8394 /* Must be more sign bit copies than the mask needs. */
8395 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8396 >= exact_log2 (mask + 1)))
8397 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8398 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8399 - exact_log2 (mask + 1)));
8401 goto shiftrt;
8403 case ASHIFTRT:
8404 /* If we are just looking for the sign bit, we don't need this shift at
8405 all, even if it has a variable count. */
8406 if (val_signbit_p (GET_MODE (x), mask))
8407 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8409 /* If this is a shift by a constant, get a mask that contains those bits
8410 that are not copies of the sign bit. We then have two cases: If
8411 MASK only includes those bits, this can be a logical shift, which may
8412 allow simplifications. If MASK is a single-bit field not within
8413 those bits, we are requesting a copy of the sign bit and hence can
8414 shift the sign bit to the appropriate location. */
8416 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8417 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8419 int i;
8421 /* If the considered data is wider than HOST_WIDE_INT, we can't
8422 represent a mask for all its bits in a single scalar.
8423 But we only care about the lower bits, so calculate these. */
8425 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8427 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8429 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8430 is the number of bits a full-width mask would have set.
8431 We need only shift if these are fewer than nonzero can
8432 hold. If not, we must keep all bits set in nonzero. */
8434 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8435 < HOST_BITS_PER_WIDE_INT)
8436 nonzero >>= INTVAL (XEXP (x, 1))
8437 + HOST_BITS_PER_WIDE_INT
8438 - GET_MODE_PRECISION (GET_MODE (x)) ;
8440 else
8442 nonzero = GET_MODE_MASK (GET_MODE (x));
8443 nonzero >>= INTVAL (XEXP (x, 1));
8446 if ((mask & ~nonzero) == 0)
8448 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8449 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8450 if (GET_CODE (x) != ASHIFTRT)
8451 return force_to_mode (x, mode, mask, next_select);
8454 else if ((i = exact_log2 (mask)) >= 0)
8456 x = simplify_shift_const
8457 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8458 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8460 if (GET_CODE (x) != ASHIFTRT)
8461 return force_to_mode (x, mode, mask, next_select);
8465 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8466 even if the shift count isn't a constant. */
8467 if (mask == 1)
8468 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8469 XEXP (x, 0), XEXP (x, 1));
8471 shiftrt:
8473 /* If this is a zero- or sign-extension operation that just affects bits
8474 we don't care about, remove it. Be sure the call above returned
8475 something that is still a shift. */
8477 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8478 && CONST_INT_P (XEXP (x, 1))
8479 && INTVAL (XEXP (x, 1)) >= 0
8480 && (INTVAL (XEXP (x, 1))
8481 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8482 && GET_CODE (XEXP (x, 0)) == ASHIFT
8483 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8484 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8485 next_select);
8487 break;
8489 case ROTATE:
8490 case ROTATERT:
8491 /* If the shift count is constant and we can do computations
8492 in the mode of X, compute where the bits we care about are.
8493 Otherwise, we can't do anything. Don't change the mode of
8494 the shift or propagate MODE into the shift, though. */
8495 if (CONST_INT_P (XEXP (x, 1))
8496 && INTVAL (XEXP (x, 1)) >= 0)
8498 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8499 GET_MODE (x),
8500 gen_int_mode (mask, GET_MODE (x)),
8501 XEXP (x, 1));
8502 if (temp && CONST_INT_P (temp))
8503 x = simplify_gen_binary (code, GET_MODE (x),
8504 force_to_mode (XEXP (x, 0), GET_MODE (x),
8505 INTVAL (temp), next_select),
8506 XEXP (x, 1));
8508 break;
8510 case NEG:
8511 /* If we just want the low-order bit, the NEG isn't needed since it
8512 won't change the low-order bit. */
8513 if (mask == 1)
8514 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8516 /* We need any bits less significant than the most significant bit in
8517 MASK since carries from those bits will affect the bits we are
8518 interested in. */
8519 mask = fuller_mask;
8520 goto unop;
8522 case NOT:
8523 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8524 same as the XOR case above. Ensure that the constant we form is not
8525 wider than the mode of X. */
8527 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8528 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8529 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8530 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8531 < GET_MODE_PRECISION (GET_MODE (x)))
8532 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8534 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8535 GET_MODE (x));
8536 temp = simplify_gen_binary (XOR, GET_MODE (x),
8537 XEXP (XEXP (x, 0), 0), temp);
8538 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8539 temp, XEXP (XEXP (x, 0), 1));
8541 return force_to_mode (x, mode, mask, next_select);
8544 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8545 use the full mask inside the NOT. */
8546 mask = fuller_mask;
8548 unop:
8549 op0 = gen_lowpart_or_truncate (op_mode,
8550 force_to_mode (XEXP (x, 0), mode, mask,
8551 next_select));
8552 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8553 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8554 break;
8556 case NE:
8557 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8558 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8559 which is equal to STORE_FLAG_VALUE. */
8560 if ((mask & ~STORE_FLAG_VALUE) == 0
8561 && XEXP (x, 1) == const0_rtx
8562 && GET_MODE (XEXP (x, 0)) == mode
8563 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8564 && (nonzero_bits (XEXP (x, 0), mode)
8565 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8566 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8568 break;
8570 case IF_THEN_ELSE:
8571 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8572 written in a narrower mode. We play it safe and do not do so. */
8574 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8575 force_to_mode (XEXP (x, 1), mode,
8576 mask, next_select));
8577 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8578 force_to_mode (XEXP (x, 2), mode,
8579 mask, next_select));
8580 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8581 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8582 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8583 op0, op1);
8584 break;
8586 default:
8587 break;
8590 /* Ensure we return a value of the proper mode. */
8591 return gen_lowpart_or_truncate (mode, x);
8594 /* Return nonzero if X is an expression that has one of two values depending on
8595 whether some other value is zero or nonzero. In that case, we return the
8596 value that is being tested, *PTRUE is set to the value if the rtx being
8597 returned has a nonzero value, and *PFALSE is set to the other alternative.
8599 If we return zero, we set *PTRUE and *PFALSE to X. */
8601 static rtx
8602 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8604 enum machine_mode mode = GET_MODE (x);
8605 enum rtx_code code = GET_CODE (x);
8606 rtx cond0, cond1, true0, true1, false0, false1;
8607 unsigned HOST_WIDE_INT nz;
8609 /* If we are comparing a value against zero, we are done. */
8610 if ((code == NE || code == EQ)
8611 && XEXP (x, 1) == const0_rtx)
8613 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8614 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8615 return XEXP (x, 0);
8618 /* If this is a unary operation whose operand has one of two values, apply
8619 our opcode to compute those values. */
8620 else if (UNARY_P (x)
8621 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8623 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8624 *pfalse = simplify_gen_unary (code, mode, false0,
8625 GET_MODE (XEXP (x, 0)));
8626 return cond0;
8629 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8630 make can't possibly match and would suppress other optimizations. */
8631 else if (code == COMPARE)
8634 /* If this is a binary operation, see if either side has only one of two
8635 values. If either one does or if both do and they are conditional on
8636 the same value, compute the new true and false values. */
8637 else if (BINARY_P (x))
8639 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8640 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8642 if ((cond0 != 0 || cond1 != 0)
8643 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8645 /* If if_then_else_cond returned zero, then true/false are the
8646 same rtl. We must copy one of them to prevent invalid rtl
8647 sharing. */
8648 if (cond0 == 0)
8649 true0 = copy_rtx (true0);
8650 else if (cond1 == 0)
8651 true1 = copy_rtx (true1);
8653 if (COMPARISON_P (x))
8655 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8656 true0, true1);
8657 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8658 false0, false1);
8660 else
8662 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8663 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8666 return cond0 ? cond0 : cond1;
8669 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8670 operands is zero when the other is nonzero, and vice-versa,
8671 and STORE_FLAG_VALUE is 1 or -1. */
8673 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8674 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8675 || code == UMAX)
8676 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8678 rtx op0 = XEXP (XEXP (x, 0), 1);
8679 rtx op1 = XEXP (XEXP (x, 1), 1);
8681 cond0 = XEXP (XEXP (x, 0), 0);
8682 cond1 = XEXP (XEXP (x, 1), 0);
8684 if (COMPARISON_P (cond0)
8685 && COMPARISON_P (cond1)
8686 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8687 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8688 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8689 || ((swap_condition (GET_CODE (cond0))
8690 == reversed_comparison_code (cond1, NULL))
8691 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8692 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8693 && ! side_effects_p (x))
8695 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8696 *pfalse = simplify_gen_binary (MULT, mode,
8697 (code == MINUS
8698 ? simplify_gen_unary (NEG, mode,
8699 op1, mode)
8700 : op1),
8701 const_true_rtx);
8702 return cond0;
8706 /* Similarly for MULT, AND and UMIN, except that for these the result
8707 is always zero. */
8708 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8709 && (code == MULT || code == AND || code == UMIN)
8710 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8712 cond0 = XEXP (XEXP (x, 0), 0);
8713 cond1 = XEXP (XEXP (x, 1), 0);
8715 if (COMPARISON_P (cond0)
8716 && COMPARISON_P (cond1)
8717 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8718 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8719 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8720 || ((swap_condition (GET_CODE (cond0))
8721 == reversed_comparison_code (cond1, NULL))
8722 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8723 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8724 && ! side_effects_p (x))
8726 *ptrue = *pfalse = const0_rtx;
8727 return cond0;
8732 else if (code == IF_THEN_ELSE)
8734 /* If we have IF_THEN_ELSE already, extract the condition and
8735 canonicalize it if it is NE or EQ. */
8736 cond0 = XEXP (x, 0);
8737 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8738 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8739 return XEXP (cond0, 0);
8740 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8742 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8743 return XEXP (cond0, 0);
8745 else
8746 return cond0;
8749 /* If X is a SUBREG, we can narrow both the true and false values
8750 if the inner expression, if there is a condition. */
8751 else if (code == SUBREG
8752 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8753 &true0, &false0)))
8755 true0 = simplify_gen_subreg (mode, true0,
8756 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8757 false0 = simplify_gen_subreg (mode, false0,
8758 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8759 if (true0 && false0)
8761 *ptrue = true0;
8762 *pfalse = false0;
8763 return cond0;
8767 /* If X is a constant, this isn't special and will cause confusions
8768 if we treat it as such. Likewise if it is equivalent to a constant. */
8769 else if (CONSTANT_P (x)
8770 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8773 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8774 will be least confusing to the rest of the compiler. */
8775 else if (mode == BImode)
8777 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8778 return x;
8781 /* If X is known to be either 0 or -1, those are the true and
8782 false values when testing X. */
8783 else if (x == constm1_rtx || x == const0_rtx
8784 || (mode != VOIDmode
8785 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8787 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8788 return x;
8791 /* Likewise for 0 or a single bit. */
8792 else if (HWI_COMPUTABLE_MODE_P (mode)
8793 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8795 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8796 return x;
8799 /* Otherwise fail; show no condition with true and false values the same. */
8800 *ptrue = *pfalse = x;
8801 return 0;
8804 /* Return the value of expression X given the fact that condition COND
8805 is known to be true when applied to REG as its first operand and VAL
8806 as its second. X is known to not be shared and so can be modified in
8807 place.
8809 We only handle the simplest cases, and specifically those cases that
8810 arise with IF_THEN_ELSE expressions. */
8812 static rtx
8813 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8815 enum rtx_code code = GET_CODE (x);
8816 rtx temp;
8817 const char *fmt;
8818 int i, j;
8820 if (side_effects_p (x))
8821 return x;
8823 /* If either operand of the condition is a floating point value,
8824 then we have to avoid collapsing an EQ comparison. */
8825 if (cond == EQ
8826 && rtx_equal_p (x, reg)
8827 && ! FLOAT_MODE_P (GET_MODE (x))
8828 && ! FLOAT_MODE_P (GET_MODE (val)))
8829 return val;
8831 if (cond == UNEQ && rtx_equal_p (x, reg))
8832 return val;
8834 /* If X is (abs REG) and we know something about REG's relationship
8835 with zero, we may be able to simplify this. */
8837 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8838 switch (cond)
8840 case GE: case GT: case EQ:
8841 return XEXP (x, 0);
8842 case LT: case LE:
8843 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8844 XEXP (x, 0),
8845 GET_MODE (XEXP (x, 0)));
8846 default:
8847 break;
8850 /* The only other cases we handle are MIN, MAX, and comparisons if the
8851 operands are the same as REG and VAL. */
8853 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8855 if (rtx_equal_p (XEXP (x, 0), val))
8856 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8858 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8860 if (COMPARISON_P (x))
8862 if (comparison_dominates_p (cond, code))
8863 return const_true_rtx;
8865 code = reversed_comparison_code (x, NULL);
8866 if (code != UNKNOWN
8867 && comparison_dominates_p (cond, code))
8868 return const0_rtx;
8869 else
8870 return x;
8872 else if (code == SMAX || code == SMIN
8873 || code == UMIN || code == UMAX)
8875 int unsignedp = (code == UMIN || code == UMAX);
8877 /* Do not reverse the condition when it is NE or EQ.
8878 This is because we cannot conclude anything about
8879 the value of 'SMAX (x, y)' when x is not equal to y,
8880 but we can when x equals y. */
8881 if ((code == SMAX || code == UMAX)
8882 && ! (cond == EQ || cond == NE))
8883 cond = reverse_condition (cond);
8885 switch (cond)
8887 case GE: case GT:
8888 return unsignedp ? x : XEXP (x, 1);
8889 case LE: case LT:
8890 return unsignedp ? x : XEXP (x, 0);
8891 case GEU: case GTU:
8892 return unsignedp ? XEXP (x, 1) : x;
8893 case LEU: case LTU:
8894 return unsignedp ? XEXP (x, 0) : x;
8895 default:
8896 break;
8901 else if (code == SUBREG)
8903 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8904 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8906 if (SUBREG_REG (x) != r)
8908 /* We must simplify subreg here, before we lose track of the
8909 original inner_mode. */
8910 new_rtx = simplify_subreg (GET_MODE (x), r,
8911 inner_mode, SUBREG_BYTE (x));
8912 if (new_rtx)
8913 return new_rtx;
8914 else
8915 SUBST (SUBREG_REG (x), r);
8918 return x;
8920 /* We don't have to handle SIGN_EXTEND here, because even in the
8921 case of replacing something with a modeless CONST_INT, a
8922 CONST_INT is already (supposed to be) a valid sign extension for
8923 its narrower mode, which implies it's already properly
8924 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8925 story is different. */
8926 else if (code == ZERO_EXTEND)
8928 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8929 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8931 if (XEXP (x, 0) != r)
8933 /* We must simplify the zero_extend here, before we lose
8934 track of the original inner_mode. */
8935 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8936 r, inner_mode);
8937 if (new_rtx)
8938 return new_rtx;
8939 else
8940 SUBST (XEXP (x, 0), r);
8943 return x;
8946 fmt = GET_RTX_FORMAT (code);
8947 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8949 if (fmt[i] == 'e')
8950 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8951 else if (fmt[i] == 'E')
8952 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8953 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8954 cond, reg, val));
8957 return x;
8960 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8961 assignment as a field assignment. */
8963 static int
8964 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8966 if (x == y || rtx_equal_p (x, y))
8967 return 1;
8969 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8970 return 0;
8972 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8973 Note that all SUBREGs of MEM are paradoxical; otherwise they
8974 would have been rewritten. */
8975 if (MEM_P (x) && GET_CODE (y) == SUBREG
8976 && MEM_P (SUBREG_REG (y))
8977 && rtx_equal_p (SUBREG_REG (y),
8978 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8979 return 1;
8981 if (MEM_P (y) && GET_CODE (x) == SUBREG
8982 && MEM_P (SUBREG_REG (x))
8983 && rtx_equal_p (SUBREG_REG (x),
8984 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8985 return 1;
8987 /* We used to see if get_last_value of X and Y were the same but that's
8988 not correct. In one direction, we'll cause the assignment to have
8989 the wrong destination and in the case, we'll import a register into this
8990 insn that might have already have been dead. So fail if none of the
8991 above cases are true. */
8992 return 0;
8995 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8996 Return that assignment if so.
8998 We only handle the most common cases. */
9000 static rtx
9001 make_field_assignment (rtx x)
9003 rtx dest = SET_DEST (x);
9004 rtx src = SET_SRC (x);
9005 rtx assign;
9006 rtx rhs, lhs;
9007 HOST_WIDE_INT c1;
9008 HOST_WIDE_INT pos;
9009 unsigned HOST_WIDE_INT len;
9010 rtx other;
9011 enum machine_mode mode;
9013 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9014 a clear of a one-bit field. We will have changed it to
9015 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9016 for a SUBREG. */
9018 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9019 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9020 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9021 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9023 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9024 1, 1, 1, 0);
9025 if (assign != 0)
9026 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9027 return x;
9030 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9031 && subreg_lowpart_p (XEXP (src, 0))
9032 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9033 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9034 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9035 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9036 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9037 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9039 assign = make_extraction (VOIDmode, dest, 0,
9040 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9041 1, 1, 1, 0);
9042 if (assign != 0)
9043 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9044 return x;
9047 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9048 one-bit field. */
9049 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9050 && XEXP (XEXP (src, 0), 0) == const1_rtx
9051 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9053 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9054 1, 1, 1, 0);
9055 if (assign != 0)
9056 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9057 return x;
9060 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9061 SRC is an AND with all bits of that field set, then we can discard
9062 the AND. */
9063 if (GET_CODE (dest) == ZERO_EXTRACT
9064 && CONST_INT_P (XEXP (dest, 1))
9065 && GET_CODE (src) == AND
9066 && CONST_INT_P (XEXP (src, 1)))
9068 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9069 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9070 unsigned HOST_WIDE_INT ze_mask;
9072 if (width >= HOST_BITS_PER_WIDE_INT)
9073 ze_mask = -1;
9074 else
9075 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9077 /* Complete overlap. We can remove the source AND. */
9078 if ((and_mask & ze_mask) == ze_mask)
9079 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9081 /* Partial overlap. We can reduce the source AND. */
9082 if ((and_mask & ze_mask) != and_mask)
9084 mode = GET_MODE (src);
9085 src = gen_rtx_AND (mode, XEXP (src, 0),
9086 gen_int_mode (and_mask & ze_mask, mode));
9087 return gen_rtx_SET (VOIDmode, dest, src);
9091 /* The other case we handle is assignments into a constant-position
9092 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9093 a mask that has all one bits except for a group of zero bits and
9094 OTHER is known to have zeros where C1 has ones, this is such an
9095 assignment. Compute the position and length from C1. Shift OTHER
9096 to the appropriate position, force it to the required mode, and
9097 make the extraction. Check for the AND in both operands. */
9099 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9100 return x;
9102 rhs = expand_compound_operation (XEXP (src, 0));
9103 lhs = expand_compound_operation (XEXP (src, 1));
9105 if (GET_CODE (rhs) == AND
9106 && CONST_INT_P (XEXP (rhs, 1))
9107 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9108 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9109 else if (GET_CODE (lhs) == AND
9110 && CONST_INT_P (XEXP (lhs, 1))
9111 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9112 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9113 else
9114 return x;
9116 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9117 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9118 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9119 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9120 return x;
9122 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9123 if (assign == 0)
9124 return x;
9126 /* The mode to use for the source is the mode of the assignment, or of
9127 what is inside a possible STRICT_LOW_PART. */
9128 mode = (GET_CODE (assign) == STRICT_LOW_PART
9129 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9131 /* Shift OTHER right POS places and make it the source, restricting it
9132 to the proper length and mode. */
9134 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9135 GET_MODE (src),
9136 other, pos),
9137 dest);
9138 src = force_to_mode (src, mode,
9139 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9140 ? ~(unsigned HOST_WIDE_INT) 0
9141 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9144 /* If SRC is masked by an AND that does not make a difference in
9145 the value being stored, strip it. */
9146 if (GET_CODE (assign) == ZERO_EXTRACT
9147 && CONST_INT_P (XEXP (assign, 1))
9148 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9149 && GET_CODE (src) == AND
9150 && CONST_INT_P (XEXP (src, 1))
9151 && UINTVAL (XEXP (src, 1))
9152 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9153 src = XEXP (src, 0);
9155 return gen_rtx_SET (VOIDmode, assign, src);
9158 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9159 if so. */
9161 static rtx
9162 apply_distributive_law (rtx x)
9164 enum rtx_code code = GET_CODE (x);
9165 enum rtx_code inner_code;
9166 rtx lhs, rhs, other;
9167 rtx tem;
9169 /* Distributivity is not true for floating point as it can change the
9170 value. So we don't do it unless -funsafe-math-optimizations. */
9171 if (FLOAT_MODE_P (GET_MODE (x))
9172 && ! flag_unsafe_math_optimizations)
9173 return x;
9175 /* The outer operation can only be one of the following: */
9176 if (code != IOR && code != AND && code != XOR
9177 && code != PLUS && code != MINUS)
9178 return x;
9180 lhs = XEXP (x, 0);
9181 rhs = XEXP (x, 1);
9183 /* If either operand is a primitive we can't do anything, so get out
9184 fast. */
9185 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9186 return x;
9188 lhs = expand_compound_operation (lhs);
9189 rhs = expand_compound_operation (rhs);
9190 inner_code = GET_CODE (lhs);
9191 if (inner_code != GET_CODE (rhs))
9192 return x;
9194 /* See if the inner and outer operations distribute. */
9195 switch (inner_code)
9197 case LSHIFTRT:
9198 case ASHIFTRT:
9199 case AND:
9200 case IOR:
9201 /* These all distribute except over PLUS. */
9202 if (code == PLUS || code == MINUS)
9203 return x;
9204 break;
9206 case MULT:
9207 if (code != PLUS && code != MINUS)
9208 return x;
9209 break;
9211 case ASHIFT:
9212 /* This is also a multiply, so it distributes over everything. */
9213 break;
9215 /* This used to handle SUBREG, but this turned out to be counter-
9216 productive, since (subreg (op ...)) usually is not handled by
9217 insn patterns, and this "optimization" therefore transformed
9218 recognizable patterns into unrecognizable ones. Therefore the
9219 SUBREG case was removed from here.
9221 It is possible that distributing SUBREG over arithmetic operations
9222 leads to an intermediate result than can then be optimized further,
9223 e.g. by moving the outer SUBREG to the other side of a SET as done
9224 in simplify_set. This seems to have been the original intent of
9225 handling SUBREGs here.
9227 However, with current GCC this does not appear to actually happen,
9228 at least on major platforms. If some case is found where removing
9229 the SUBREG case here prevents follow-on optimizations, distributing
9230 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9232 default:
9233 return x;
9236 /* Set LHS and RHS to the inner operands (A and B in the example
9237 above) and set OTHER to the common operand (C in the example).
9238 There is only one way to do this unless the inner operation is
9239 commutative. */
9240 if (COMMUTATIVE_ARITH_P (lhs)
9241 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9242 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9243 else if (COMMUTATIVE_ARITH_P (lhs)
9244 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9245 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9246 else if (COMMUTATIVE_ARITH_P (lhs)
9247 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9248 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9249 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9250 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9251 else
9252 return x;
9254 /* Form the new inner operation, seeing if it simplifies first. */
9255 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9257 /* There is one exception to the general way of distributing:
9258 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9259 if (code == XOR && inner_code == IOR)
9261 inner_code = AND;
9262 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9265 /* We may be able to continuing distributing the result, so call
9266 ourselves recursively on the inner operation before forming the
9267 outer operation, which we return. */
9268 return simplify_gen_binary (inner_code, GET_MODE (x),
9269 apply_distributive_law (tem), other);
9272 /* See if X is of the form (* (+ A B) C), and if so convert to
9273 (+ (* A C) (* B C)) and try to simplify.
9275 Most of the time, this results in no change. However, if some of
9276 the operands are the same or inverses of each other, simplifications
9277 will result.
9279 For example, (and (ior A B) (not B)) can occur as the result of
9280 expanding a bit field assignment. When we apply the distributive
9281 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9282 which then simplifies to (and (A (not B))).
9284 Note that no checks happen on the validity of applying the inverse
9285 distributive law. This is pointless since we can do it in the
9286 few places where this routine is called.
9288 N is the index of the term that is decomposed (the arithmetic operation,
9289 i.e. (+ A B) in the first example above). !N is the index of the term that
9290 is distributed, i.e. of C in the first example above. */
9291 static rtx
9292 distribute_and_simplify_rtx (rtx x, int n)
9294 enum machine_mode mode;
9295 enum rtx_code outer_code, inner_code;
9296 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9298 /* Distributivity is not true for floating point as it can change the
9299 value. So we don't do it unless -funsafe-math-optimizations. */
9300 if (FLOAT_MODE_P (GET_MODE (x))
9301 && ! flag_unsafe_math_optimizations)
9302 return NULL_RTX;
9304 decomposed = XEXP (x, n);
9305 if (!ARITHMETIC_P (decomposed))
9306 return NULL_RTX;
9308 mode = GET_MODE (x);
9309 outer_code = GET_CODE (x);
9310 distributed = XEXP (x, !n);
9312 inner_code = GET_CODE (decomposed);
9313 inner_op0 = XEXP (decomposed, 0);
9314 inner_op1 = XEXP (decomposed, 1);
9316 /* Special case (and (xor B C) (not A)), which is equivalent to
9317 (xor (ior A B) (ior A C)) */
9318 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9320 distributed = XEXP (distributed, 0);
9321 outer_code = IOR;
9324 if (n == 0)
9326 /* Distribute the second term. */
9327 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9328 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9330 else
9332 /* Distribute the first term. */
9333 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9334 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9337 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9338 new_op0, new_op1));
9339 if (GET_CODE (tmp) != outer_code
9340 && (set_src_cost (tmp, optimize_this_for_speed_p)
9341 < set_src_cost (x, optimize_this_for_speed_p)))
9342 return tmp;
9344 return NULL_RTX;
9347 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9348 in MODE. Return an equivalent form, if different from (and VAROP
9349 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9351 static rtx
9352 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9353 unsigned HOST_WIDE_INT constop)
9355 unsigned HOST_WIDE_INT nonzero;
9356 unsigned HOST_WIDE_INT orig_constop;
9357 rtx orig_varop;
9358 int i;
9360 orig_varop = varop;
9361 orig_constop = constop;
9362 if (GET_CODE (varop) == CLOBBER)
9363 return NULL_RTX;
9365 /* Simplify VAROP knowing that we will be only looking at some of the
9366 bits in it.
9368 Note by passing in CONSTOP, we guarantee that the bits not set in
9369 CONSTOP are not significant and will never be examined. We must
9370 ensure that is the case by explicitly masking out those bits
9371 before returning. */
9372 varop = force_to_mode (varop, mode, constop, 0);
9374 /* If VAROP is a CLOBBER, we will fail so return it. */
9375 if (GET_CODE (varop) == CLOBBER)
9376 return varop;
9378 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9379 to VAROP and return the new constant. */
9380 if (CONST_INT_P (varop))
9381 return gen_int_mode (INTVAL (varop) & constop, mode);
9383 /* See what bits may be nonzero in VAROP. Unlike the general case of
9384 a call to nonzero_bits, here we don't care about bits outside
9385 MODE. */
9387 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9389 /* Turn off all bits in the constant that are known to already be zero.
9390 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9391 which is tested below. */
9393 constop &= nonzero;
9395 /* If we don't have any bits left, return zero. */
9396 if (constop == 0)
9397 return const0_rtx;
9399 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9400 a power of two, we can replace this with an ASHIFT. */
9401 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9402 && (i = exact_log2 (constop)) >= 0)
9403 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9405 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9406 or XOR, then try to apply the distributive law. This may eliminate
9407 operations if either branch can be simplified because of the AND.
9408 It may also make some cases more complex, but those cases probably
9409 won't match a pattern either with or without this. */
9411 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9412 return
9413 gen_lowpart
9414 (mode,
9415 apply_distributive_law
9416 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9417 simplify_and_const_int (NULL_RTX,
9418 GET_MODE (varop),
9419 XEXP (varop, 0),
9420 constop),
9421 simplify_and_const_int (NULL_RTX,
9422 GET_MODE (varop),
9423 XEXP (varop, 1),
9424 constop))));
9426 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9427 the AND and see if one of the operands simplifies to zero. If so, we
9428 may eliminate it. */
9430 if (GET_CODE (varop) == PLUS
9431 && exact_log2 (constop + 1) >= 0)
9433 rtx o0, o1;
9435 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9436 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9437 if (o0 == const0_rtx)
9438 return o1;
9439 if (o1 == const0_rtx)
9440 return o0;
9443 /* Make a SUBREG if necessary. If we can't make it, fail. */
9444 varop = gen_lowpart (mode, varop);
9445 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9446 return NULL_RTX;
9448 /* If we are only masking insignificant bits, return VAROP. */
9449 if (constop == nonzero)
9450 return varop;
9452 if (varop == orig_varop && constop == orig_constop)
9453 return NULL_RTX;
9455 /* Otherwise, return an AND. */
9456 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9460 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9461 in MODE.
9463 Return an equivalent form, if different from X. Otherwise, return X. If
9464 X is zero, we are to always construct the equivalent form. */
9466 static rtx
9467 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9468 unsigned HOST_WIDE_INT constop)
9470 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9471 if (tem)
9472 return tem;
9474 if (!x)
9475 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9476 gen_int_mode (constop, mode));
9477 if (GET_MODE (x) != mode)
9478 x = gen_lowpart (mode, x);
9479 return x;
9482 /* Given a REG, X, compute which bits in X can be nonzero.
9483 We don't care about bits outside of those defined in MODE.
9485 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9486 a shift, AND, or zero_extract, we can do better. */
9488 static rtx
9489 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9490 const_rtx known_x ATTRIBUTE_UNUSED,
9491 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9492 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9493 unsigned HOST_WIDE_INT *nonzero)
9495 rtx tem;
9496 reg_stat_type *rsp;
9498 /* If X is a register whose nonzero bits value is current, use it.
9499 Otherwise, if X is a register whose value we can find, use that
9500 value. Otherwise, use the previously-computed global nonzero bits
9501 for this register. */
9503 rsp = &reg_stat[REGNO (x)];
9504 if (rsp->last_set_value != 0
9505 && (rsp->last_set_mode == mode
9506 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9507 && GET_MODE_CLASS (mode) == MODE_INT))
9508 && ((rsp->last_set_label >= label_tick_ebb_start
9509 && rsp->last_set_label < label_tick)
9510 || (rsp->last_set_label == label_tick
9511 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9512 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9513 && REG_N_SETS (REGNO (x)) == 1
9514 && !REGNO_REG_SET_P
9515 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9516 REGNO (x)))))
9518 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9520 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9521 /* We don't know anything about the upper bits. */
9522 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9524 *nonzero &= mask;
9525 return NULL;
9528 tem = get_last_value (x);
9530 if (tem)
9532 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9533 /* If X is narrower than MODE and TEM is a non-negative
9534 constant that would appear negative in the mode of X,
9535 sign-extend it for use in reg_nonzero_bits because some
9536 machines (maybe most) will actually do the sign-extension
9537 and this is the conservative approach.
9539 ??? For 2.5, try to tighten up the MD files in this regard
9540 instead of this kludge. */
9542 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9543 && CONST_INT_P (tem)
9544 && INTVAL (tem) > 0
9545 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9546 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9547 #endif
9548 return tem;
9550 else if (nonzero_sign_valid && rsp->nonzero_bits)
9552 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9554 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9555 /* We don't know anything about the upper bits. */
9556 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9558 *nonzero &= mask;
9561 return NULL;
9564 /* Return the number of bits at the high-order end of X that are known to
9565 be equal to the sign bit. X will be used in mode MODE; if MODE is
9566 VOIDmode, X will be used in its own mode. The returned value will always
9567 be between 1 and the number of bits in MODE. */
9569 static rtx
9570 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9571 const_rtx known_x ATTRIBUTE_UNUSED,
9572 enum machine_mode known_mode
9573 ATTRIBUTE_UNUSED,
9574 unsigned int known_ret ATTRIBUTE_UNUSED,
9575 unsigned int *result)
9577 rtx tem;
9578 reg_stat_type *rsp;
9580 rsp = &reg_stat[REGNO (x)];
9581 if (rsp->last_set_value != 0
9582 && rsp->last_set_mode == mode
9583 && ((rsp->last_set_label >= label_tick_ebb_start
9584 && rsp->last_set_label < label_tick)
9585 || (rsp->last_set_label == label_tick
9586 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9587 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9588 && REG_N_SETS (REGNO (x)) == 1
9589 && !REGNO_REG_SET_P
9590 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9591 REGNO (x)))))
9593 *result = rsp->last_set_sign_bit_copies;
9594 return NULL;
9597 tem = get_last_value (x);
9598 if (tem != 0)
9599 return tem;
9601 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9602 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9603 *result = rsp->sign_bit_copies;
9605 return NULL;
9608 /* Return the number of "extended" bits there are in X, when interpreted
9609 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9610 unsigned quantities, this is the number of high-order zero bits.
9611 For signed quantities, this is the number of copies of the sign bit
9612 minus 1. In both case, this function returns the number of "spare"
9613 bits. For example, if two quantities for which this function returns
9614 at least 1 are added, the addition is known not to overflow.
9616 This function will always return 0 unless called during combine, which
9617 implies that it must be called from a define_split. */
9619 unsigned int
9620 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9622 if (nonzero_sign_valid == 0)
9623 return 0;
9625 return (unsignedp
9626 ? (HWI_COMPUTABLE_MODE_P (mode)
9627 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9628 - floor_log2 (nonzero_bits (x, mode)))
9629 : 0)
9630 : num_sign_bit_copies (x, mode) - 1);
9633 /* This function is called from `simplify_shift_const' to merge two
9634 outer operations. Specifically, we have already found that we need
9635 to perform operation *POP0 with constant *PCONST0 at the outermost
9636 position. We would now like to also perform OP1 with constant CONST1
9637 (with *POP0 being done last).
9639 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9640 the resulting operation. *PCOMP_P is set to 1 if we would need to
9641 complement the innermost operand, otherwise it is unchanged.
9643 MODE is the mode in which the operation will be done. No bits outside
9644 the width of this mode matter. It is assumed that the width of this mode
9645 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9647 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9648 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9649 result is simply *PCONST0.
9651 If the resulting operation cannot be expressed as one operation, we
9652 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9654 static int
9655 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
9657 enum rtx_code op0 = *pop0;
9658 HOST_WIDE_INT const0 = *pconst0;
9660 const0 &= GET_MODE_MASK (mode);
9661 const1 &= GET_MODE_MASK (mode);
9663 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9664 if (op0 == AND)
9665 const1 &= const0;
9667 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9668 if OP0 is SET. */
9670 if (op1 == UNKNOWN || op0 == SET)
9671 return 1;
9673 else if (op0 == UNKNOWN)
9674 op0 = op1, const0 = const1;
9676 else if (op0 == op1)
9678 switch (op0)
9680 case AND:
9681 const0 &= const1;
9682 break;
9683 case IOR:
9684 const0 |= const1;
9685 break;
9686 case XOR:
9687 const0 ^= const1;
9688 break;
9689 case PLUS:
9690 const0 += const1;
9691 break;
9692 case NEG:
9693 op0 = UNKNOWN;
9694 break;
9695 default:
9696 break;
9700 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9701 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9702 return 0;
9704 /* If the two constants aren't the same, we can't do anything. The
9705 remaining six cases can all be done. */
9706 else if (const0 != const1)
9707 return 0;
9709 else
9710 switch (op0)
9712 case IOR:
9713 if (op1 == AND)
9714 /* (a & b) | b == b */
9715 op0 = SET;
9716 else /* op1 == XOR */
9717 /* (a ^ b) | b == a | b */
9719 break;
9721 case XOR:
9722 if (op1 == AND)
9723 /* (a & b) ^ b == (~a) & b */
9724 op0 = AND, *pcomp_p = 1;
9725 else /* op1 == IOR */
9726 /* (a | b) ^ b == a & ~b */
9727 op0 = AND, const0 = ~const0;
9728 break;
9730 case AND:
9731 if (op1 == IOR)
9732 /* (a | b) & b == b */
9733 op0 = SET;
9734 else /* op1 == XOR */
9735 /* (a ^ b) & b) == (~a) & b */
9736 *pcomp_p = 1;
9737 break;
9738 default:
9739 break;
9742 /* Check for NO-OP cases. */
9743 const0 &= GET_MODE_MASK (mode);
9744 if (const0 == 0
9745 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9746 op0 = UNKNOWN;
9747 else if (const0 == 0 && op0 == AND)
9748 op0 = SET;
9749 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9750 && op0 == AND)
9751 op0 = UNKNOWN;
9753 *pop0 = op0;
9755 /* ??? Slightly redundant with the above mask, but not entirely.
9756 Moving this above means we'd have to sign-extend the mode mask
9757 for the final test. */
9758 if (op0 != UNKNOWN && op0 != NEG)
9759 *pconst0 = trunc_int_for_mode (const0, mode);
9761 return 1;
9764 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9765 the shift in. The original shift operation CODE is performed on OP in
9766 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9767 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9768 result of the shift is subject to operation OUTER_CODE with operand
9769 OUTER_CONST. */
9771 static enum machine_mode
9772 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9773 enum machine_mode orig_mode, enum machine_mode mode,
9774 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9776 if (orig_mode == mode)
9777 return mode;
9778 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9780 /* In general we can't perform in wider mode for right shift and rotate. */
9781 switch (code)
9783 case ASHIFTRT:
9784 /* We can still widen if the bits brought in from the left are identical
9785 to the sign bit of ORIG_MODE. */
9786 if (num_sign_bit_copies (op, mode)
9787 > (unsigned) (GET_MODE_PRECISION (mode)
9788 - GET_MODE_PRECISION (orig_mode)))
9789 return mode;
9790 return orig_mode;
9792 case LSHIFTRT:
9793 /* Similarly here but with zero bits. */
9794 if (HWI_COMPUTABLE_MODE_P (mode)
9795 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9796 return mode;
9798 /* We can also widen if the bits brought in will be masked off. This
9799 operation is performed in ORIG_MODE. */
9800 if (outer_code == AND)
9802 int care_bits = low_bitmask_len (orig_mode, outer_const);
9804 if (care_bits >= 0
9805 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9806 return mode;
9808 /* fall through */
9810 case ROTATE:
9811 return orig_mode;
9813 case ROTATERT:
9814 gcc_unreachable ();
9816 default:
9817 return mode;
9821 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9822 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9823 if we cannot simplify it. Otherwise, return a simplified value.
9825 The shift is normally computed in the widest mode we find in VAROP, as
9826 long as it isn't a different number of words than RESULT_MODE. Exceptions
9827 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9829 static rtx
9830 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9831 rtx varop, int orig_count)
9833 enum rtx_code orig_code = code;
9834 rtx orig_varop = varop;
9835 int count;
9836 enum machine_mode mode = result_mode;
9837 enum machine_mode shift_mode, tmode;
9838 unsigned int mode_words
9839 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9840 /* We form (outer_op (code varop count) (outer_const)). */
9841 enum rtx_code outer_op = UNKNOWN;
9842 HOST_WIDE_INT outer_const = 0;
9843 int complement_p = 0;
9844 rtx new_rtx, x;
9846 /* Make sure and truncate the "natural" shift on the way in. We don't
9847 want to do this inside the loop as it makes it more difficult to
9848 combine shifts. */
9849 if (SHIFT_COUNT_TRUNCATED)
9850 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9852 /* If we were given an invalid count, don't do anything except exactly
9853 what was requested. */
9855 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9856 return NULL_RTX;
9858 count = orig_count;
9860 /* Unless one of the branches of the `if' in this loop does a `continue',
9861 we will `break' the loop after the `if'. */
9863 while (count != 0)
9865 /* If we have an operand of (clobber (const_int 0)), fail. */
9866 if (GET_CODE (varop) == CLOBBER)
9867 return NULL_RTX;
9869 /* Convert ROTATERT to ROTATE. */
9870 if (code == ROTATERT)
9872 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9873 code = ROTATE;
9874 if (VECTOR_MODE_P (result_mode))
9875 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9876 else
9877 count = bitsize - count;
9880 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9881 mode, outer_op, outer_const);
9883 /* Handle cases where the count is greater than the size of the mode
9884 minus 1. For ASHIFT, use the size minus one as the count (this can
9885 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9886 take the count modulo the size. For other shifts, the result is
9887 zero.
9889 Since these shifts are being produced by the compiler by combining
9890 multiple operations, each of which are defined, we know what the
9891 result is supposed to be. */
9893 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
9895 if (code == ASHIFTRT)
9896 count = GET_MODE_PRECISION (shift_mode) - 1;
9897 else if (code == ROTATE || code == ROTATERT)
9898 count %= GET_MODE_PRECISION (shift_mode);
9899 else
9901 /* We can't simply return zero because there may be an
9902 outer op. */
9903 varop = const0_rtx;
9904 count = 0;
9905 break;
9909 /* If we discovered we had to complement VAROP, leave. Making a NOT
9910 here would cause an infinite loop. */
9911 if (complement_p)
9912 break;
9914 /* An arithmetic right shift of a quantity known to be -1 or 0
9915 is a no-op. */
9916 if (code == ASHIFTRT
9917 && (num_sign_bit_copies (varop, shift_mode)
9918 == GET_MODE_PRECISION (shift_mode)))
9920 count = 0;
9921 break;
9924 /* If we are doing an arithmetic right shift and discarding all but
9925 the sign bit copies, this is equivalent to doing a shift by the
9926 bitsize minus one. Convert it into that shift because it will often
9927 allow other simplifications. */
9929 if (code == ASHIFTRT
9930 && (count + num_sign_bit_copies (varop, shift_mode)
9931 >= GET_MODE_PRECISION (shift_mode)))
9932 count = GET_MODE_PRECISION (shift_mode) - 1;
9934 /* We simplify the tests below and elsewhere by converting
9935 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9936 `make_compound_operation' will convert it to an ASHIFTRT for
9937 those machines (such as VAX) that don't have an LSHIFTRT. */
9938 if (code == ASHIFTRT
9939 && val_signbit_known_clear_p (shift_mode,
9940 nonzero_bits (varop, shift_mode)))
9941 code = LSHIFTRT;
9943 if (((code == LSHIFTRT
9944 && HWI_COMPUTABLE_MODE_P (shift_mode)
9945 && !(nonzero_bits (varop, shift_mode) >> count))
9946 || (code == ASHIFT
9947 && HWI_COMPUTABLE_MODE_P (shift_mode)
9948 && !((nonzero_bits (varop, shift_mode) << count)
9949 & GET_MODE_MASK (shift_mode))))
9950 && !side_effects_p (varop))
9951 varop = const0_rtx;
9953 switch (GET_CODE (varop))
9955 case SIGN_EXTEND:
9956 case ZERO_EXTEND:
9957 case SIGN_EXTRACT:
9958 case ZERO_EXTRACT:
9959 new_rtx = expand_compound_operation (varop);
9960 if (new_rtx != varop)
9962 varop = new_rtx;
9963 continue;
9965 break;
9967 case MEM:
9968 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9969 minus the width of a smaller mode, we can do this with a
9970 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9971 if ((code == ASHIFTRT || code == LSHIFTRT)
9972 && ! mode_dependent_address_p (XEXP (varop, 0),
9973 MEM_ADDR_SPACE (varop))
9974 && ! MEM_VOLATILE_P (varop)
9975 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9976 MODE_INT, 1)) != BLKmode)
9978 new_rtx = adjust_address_nv (varop, tmode,
9979 BYTES_BIG_ENDIAN ? 0
9980 : count / BITS_PER_UNIT);
9982 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9983 : ZERO_EXTEND, mode, new_rtx);
9984 count = 0;
9985 continue;
9987 break;
9989 case SUBREG:
9990 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9991 the same number of words as what we've seen so far. Then store
9992 the widest mode in MODE. */
9993 if (subreg_lowpart_p (varop)
9994 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9995 > GET_MODE_SIZE (GET_MODE (varop)))
9996 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9997 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9998 == mode_words
9999 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10000 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10002 varop = SUBREG_REG (varop);
10003 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10004 mode = GET_MODE (varop);
10005 continue;
10007 break;
10009 case MULT:
10010 /* Some machines use MULT instead of ASHIFT because MULT
10011 is cheaper. But it is still better on those machines to
10012 merge two shifts into one. */
10013 if (CONST_INT_P (XEXP (varop, 1))
10014 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10016 varop
10017 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10018 XEXP (varop, 0),
10019 GEN_INT (exact_log2 (
10020 UINTVAL (XEXP (varop, 1)))));
10021 continue;
10023 break;
10025 case UDIV:
10026 /* Similar, for when divides are cheaper. */
10027 if (CONST_INT_P (XEXP (varop, 1))
10028 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10030 varop
10031 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10032 XEXP (varop, 0),
10033 GEN_INT (exact_log2 (
10034 UINTVAL (XEXP (varop, 1)))));
10035 continue;
10037 break;
10039 case ASHIFTRT:
10040 /* If we are extracting just the sign bit of an arithmetic
10041 right shift, that shift is not needed. However, the sign
10042 bit of a wider mode may be different from what would be
10043 interpreted as the sign bit in a narrower mode, so, if
10044 the result is narrower, don't discard the shift. */
10045 if (code == LSHIFTRT
10046 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10047 && (GET_MODE_BITSIZE (result_mode)
10048 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10050 varop = XEXP (varop, 0);
10051 continue;
10054 /* ... fall through ... */
10056 case LSHIFTRT:
10057 case ASHIFT:
10058 case ROTATE:
10059 /* Here we have two nested shifts. The result is usually the
10060 AND of a new shift with a mask. We compute the result below. */
10061 if (CONST_INT_P (XEXP (varop, 1))
10062 && INTVAL (XEXP (varop, 1)) >= 0
10063 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10064 && HWI_COMPUTABLE_MODE_P (result_mode)
10065 && HWI_COMPUTABLE_MODE_P (mode)
10066 && !VECTOR_MODE_P (result_mode))
10068 enum rtx_code first_code = GET_CODE (varop);
10069 unsigned int first_count = INTVAL (XEXP (varop, 1));
10070 unsigned HOST_WIDE_INT mask;
10071 rtx mask_rtx;
10073 /* We have one common special case. We can't do any merging if
10074 the inner code is an ASHIFTRT of a smaller mode. However, if
10075 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10076 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10077 we can convert it to
10078 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10079 This simplifies certain SIGN_EXTEND operations. */
10080 if (code == ASHIFT && first_code == ASHIFTRT
10081 && count == (GET_MODE_PRECISION (result_mode)
10082 - GET_MODE_PRECISION (GET_MODE (varop))))
10084 /* C3 has the low-order C1 bits zero. */
10086 mask = GET_MODE_MASK (mode)
10087 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10089 varop = simplify_and_const_int (NULL_RTX, result_mode,
10090 XEXP (varop, 0), mask);
10091 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10092 varop, count);
10093 count = first_count;
10094 code = ASHIFTRT;
10095 continue;
10098 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10099 than C1 high-order bits equal to the sign bit, we can convert
10100 this to either an ASHIFT or an ASHIFTRT depending on the
10101 two counts.
10103 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10105 if (code == ASHIFTRT && first_code == ASHIFT
10106 && GET_MODE (varop) == shift_mode
10107 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10108 > first_count))
10110 varop = XEXP (varop, 0);
10111 count -= first_count;
10112 if (count < 0)
10114 count = -count;
10115 code = ASHIFT;
10118 continue;
10121 /* There are some cases we can't do. If CODE is ASHIFTRT,
10122 we can only do this if FIRST_CODE is also ASHIFTRT.
10124 We can't do the case when CODE is ROTATE and FIRST_CODE is
10125 ASHIFTRT.
10127 If the mode of this shift is not the mode of the outer shift,
10128 we can't do this if either shift is a right shift or ROTATE.
10130 Finally, we can't do any of these if the mode is too wide
10131 unless the codes are the same.
10133 Handle the case where the shift codes are the same
10134 first. */
10136 if (code == first_code)
10138 if (GET_MODE (varop) != result_mode
10139 && (code == ASHIFTRT || code == LSHIFTRT
10140 || code == ROTATE))
10141 break;
10143 count += first_count;
10144 varop = XEXP (varop, 0);
10145 continue;
10148 if (code == ASHIFTRT
10149 || (code == ROTATE && first_code == ASHIFTRT)
10150 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10151 || (GET_MODE (varop) != result_mode
10152 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10153 || first_code == ROTATE
10154 || code == ROTATE)))
10155 break;
10157 /* To compute the mask to apply after the shift, shift the
10158 nonzero bits of the inner shift the same way the
10159 outer shift will. */
10161 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10162 result_mode);
10164 mask_rtx
10165 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10166 GEN_INT (count));
10168 /* Give up if we can't compute an outer operation to use. */
10169 if (mask_rtx == 0
10170 || !CONST_INT_P (mask_rtx)
10171 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10172 INTVAL (mask_rtx),
10173 result_mode, &complement_p))
10174 break;
10176 /* If the shifts are in the same direction, we add the
10177 counts. Otherwise, we subtract them. */
10178 if ((code == ASHIFTRT || code == LSHIFTRT)
10179 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10180 count += first_count;
10181 else
10182 count -= first_count;
10184 /* If COUNT is positive, the new shift is usually CODE,
10185 except for the two exceptions below, in which case it is
10186 FIRST_CODE. If the count is negative, FIRST_CODE should
10187 always be used */
10188 if (count > 0
10189 && ((first_code == ROTATE && code == ASHIFT)
10190 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10191 code = first_code;
10192 else if (count < 0)
10193 code = first_code, count = -count;
10195 varop = XEXP (varop, 0);
10196 continue;
10199 /* If we have (A << B << C) for any shift, we can convert this to
10200 (A << C << B). This wins if A is a constant. Only try this if
10201 B is not a constant. */
10203 else if (GET_CODE (varop) == code
10204 && CONST_INT_P (XEXP (varop, 0))
10205 && !CONST_INT_P (XEXP (varop, 1)))
10207 rtx new_rtx = simplify_const_binary_operation (code, mode,
10208 XEXP (varop, 0),
10209 GEN_INT (count));
10210 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10211 count = 0;
10212 continue;
10214 break;
10216 case NOT:
10217 if (VECTOR_MODE_P (mode))
10218 break;
10220 /* Make this fit the case below. */
10221 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10222 continue;
10224 case IOR:
10225 case AND:
10226 case XOR:
10227 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10228 with C the size of VAROP - 1 and the shift is logical if
10229 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10230 we have an (le X 0) operation. If we have an arithmetic shift
10231 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10232 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10234 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10235 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10236 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10237 && (code == LSHIFTRT || code == ASHIFTRT)
10238 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10239 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10241 count = 0;
10242 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10243 const0_rtx);
10245 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10246 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10248 continue;
10251 /* If we have (shift (logical)), move the logical to the outside
10252 to allow it to possibly combine with another logical and the
10253 shift to combine with another shift. This also canonicalizes to
10254 what a ZERO_EXTRACT looks like. Also, some machines have
10255 (and (shift)) insns. */
10257 if (CONST_INT_P (XEXP (varop, 1))
10258 /* We can't do this if we have (ashiftrt (xor)) and the
10259 constant has its sign bit set in shift_mode with shift_mode
10260 wider than result_mode. */
10261 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10262 && result_mode != shift_mode
10263 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10264 shift_mode))
10265 && (new_rtx = simplify_const_binary_operation
10266 (code, result_mode,
10267 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10268 GEN_INT (count))) != 0
10269 && CONST_INT_P (new_rtx)
10270 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10271 INTVAL (new_rtx), result_mode, &complement_p))
10273 varop = XEXP (varop, 0);
10274 continue;
10277 /* If we can't do that, try to simplify the shift in each arm of the
10278 logical expression, make a new logical expression, and apply
10279 the inverse distributive law. This also can't be done for
10280 (ashiftrt (xor)) where we've widened the shift and the constant
10281 changes the sign bit. */
10282 if (CONST_INT_P (XEXP (varop, 1))
10283 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10284 && result_mode != shift_mode
10285 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10286 shift_mode)))
10288 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10289 XEXP (varop, 0), count);
10290 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10291 XEXP (varop, 1), count);
10293 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10294 lhs, rhs);
10295 varop = apply_distributive_law (varop);
10297 count = 0;
10298 continue;
10300 break;
10302 case EQ:
10303 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10304 says that the sign bit can be tested, FOO has mode MODE, C is
10305 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10306 that may be nonzero. */
10307 if (code == LSHIFTRT
10308 && XEXP (varop, 1) == const0_rtx
10309 && GET_MODE (XEXP (varop, 0)) == result_mode
10310 && count == (GET_MODE_PRECISION (result_mode) - 1)
10311 && HWI_COMPUTABLE_MODE_P (result_mode)
10312 && STORE_FLAG_VALUE == -1
10313 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10314 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10315 &complement_p))
10317 varop = XEXP (varop, 0);
10318 count = 0;
10319 continue;
10321 break;
10323 case NEG:
10324 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10325 than the number of bits in the mode is equivalent to A. */
10326 if (code == LSHIFTRT
10327 && count == (GET_MODE_PRECISION (result_mode) - 1)
10328 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10330 varop = XEXP (varop, 0);
10331 count = 0;
10332 continue;
10335 /* NEG commutes with ASHIFT since it is multiplication. Move the
10336 NEG outside to allow shifts to combine. */
10337 if (code == ASHIFT
10338 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10339 &complement_p))
10341 varop = XEXP (varop, 0);
10342 continue;
10344 break;
10346 case PLUS:
10347 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10348 is one less than the number of bits in the mode is
10349 equivalent to (xor A 1). */
10350 if (code == LSHIFTRT
10351 && count == (GET_MODE_PRECISION (result_mode) - 1)
10352 && XEXP (varop, 1) == constm1_rtx
10353 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10354 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10355 &complement_p))
10357 count = 0;
10358 varop = XEXP (varop, 0);
10359 continue;
10362 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10363 that might be nonzero in BAR are those being shifted out and those
10364 bits are known zero in FOO, we can replace the PLUS with FOO.
10365 Similarly in the other operand order. This code occurs when
10366 we are computing the size of a variable-size array. */
10368 if ((code == ASHIFTRT || code == LSHIFTRT)
10369 && count < HOST_BITS_PER_WIDE_INT
10370 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10371 && (nonzero_bits (XEXP (varop, 1), result_mode)
10372 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10374 varop = XEXP (varop, 0);
10375 continue;
10377 else if ((code == ASHIFTRT || code == LSHIFTRT)
10378 && count < HOST_BITS_PER_WIDE_INT
10379 && HWI_COMPUTABLE_MODE_P (result_mode)
10380 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10381 >> count)
10382 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10383 & nonzero_bits (XEXP (varop, 1),
10384 result_mode)))
10386 varop = XEXP (varop, 1);
10387 continue;
10390 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10391 if (code == ASHIFT
10392 && CONST_INT_P (XEXP (varop, 1))
10393 && (new_rtx = simplify_const_binary_operation
10394 (ASHIFT, result_mode,
10395 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10396 GEN_INT (count))) != 0
10397 && CONST_INT_P (new_rtx)
10398 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10399 INTVAL (new_rtx), result_mode, &complement_p))
10401 varop = XEXP (varop, 0);
10402 continue;
10405 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10406 signbit', and attempt to change the PLUS to an XOR and move it to
10407 the outer operation as is done above in the AND/IOR/XOR case
10408 leg for shift(logical). See details in logical handling above
10409 for reasoning in doing so. */
10410 if (code == LSHIFTRT
10411 && CONST_INT_P (XEXP (varop, 1))
10412 && mode_signbit_p (result_mode, XEXP (varop, 1))
10413 && (new_rtx = simplify_const_binary_operation
10414 (code, result_mode,
10415 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10416 GEN_INT (count))) != 0
10417 && CONST_INT_P (new_rtx)
10418 && merge_outer_ops (&outer_op, &outer_const, XOR,
10419 INTVAL (new_rtx), result_mode, &complement_p))
10421 varop = XEXP (varop, 0);
10422 continue;
10425 break;
10427 case MINUS:
10428 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10429 with C the size of VAROP - 1 and the shift is logical if
10430 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10431 we have a (gt X 0) operation. If the shift is arithmetic with
10432 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10433 we have a (neg (gt X 0)) operation. */
10435 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10436 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10437 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10438 && (code == LSHIFTRT || code == ASHIFTRT)
10439 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10440 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10441 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10443 count = 0;
10444 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10445 const0_rtx);
10447 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10448 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10450 continue;
10452 break;
10454 case TRUNCATE:
10455 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10456 if the truncate does not affect the value. */
10457 if (code == LSHIFTRT
10458 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10459 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10460 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10461 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10462 - GET_MODE_PRECISION (GET_MODE (varop)))))
10464 rtx varop_inner = XEXP (varop, 0);
10466 varop_inner
10467 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10468 XEXP (varop_inner, 0),
10469 GEN_INT
10470 (count + INTVAL (XEXP (varop_inner, 1))));
10471 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10472 count = 0;
10473 continue;
10475 break;
10477 default:
10478 break;
10481 break;
10484 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10485 outer_op, outer_const);
10487 /* We have now finished analyzing the shift. The result should be
10488 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10489 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10490 to the result of the shift. OUTER_CONST is the relevant constant,
10491 but we must turn off all bits turned off in the shift. */
10493 if (outer_op == UNKNOWN
10494 && orig_code == code && orig_count == count
10495 && varop == orig_varop
10496 && shift_mode == GET_MODE (varop))
10497 return NULL_RTX;
10499 /* Make a SUBREG if necessary. If we can't make it, fail. */
10500 varop = gen_lowpart (shift_mode, varop);
10501 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10502 return NULL_RTX;
10504 /* If we have an outer operation and we just made a shift, it is
10505 possible that we could have simplified the shift were it not
10506 for the outer operation. So try to do the simplification
10507 recursively. */
10509 if (outer_op != UNKNOWN)
10510 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10511 else
10512 x = NULL_RTX;
10514 if (x == NULL_RTX)
10515 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10517 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10518 turn off all the bits that the shift would have turned off. */
10519 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10520 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10521 GET_MODE_MASK (result_mode) >> orig_count);
10523 /* Do the remainder of the processing in RESULT_MODE. */
10524 x = gen_lowpart_or_truncate (result_mode, x);
10526 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10527 operation. */
10528 if (complement_p)
10529 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10531 if (outer_op != UNKNOWN)
10533 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10534 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10535 outer_const = trunc_int_for_mode (outer_const, result_mode);
10537 if (outer_op == AND)
10538 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10539 else if (outer_op == SET)
10541 /* This means that we have determined that the result is
10542 equivalent to a constant. This should be rare. */
10543 if (!side_effects_p (x))
10544 x = GEN_INT (outer_const);
10546 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10547 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10548 else
10549 x = simplify_gen_binary (outer_op, result_mode, x,
10550 GEN_INT (outer_const));
10553 return x;
10556 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10557 The result of the shift is RESULT_MODE. If we cannot simplify it,
10558 return X or, if it is NULL, synthesize the expression with
10559 simplify_gen_binary. Otherwise, return a simplified value.
10561 The shift is normally computed in the widest mode we find in VAROP, as
10562 long as it isn't a different number of words than RESULT_MODE. Exceptions
10563 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10565 static rtx
10566 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10567 rtx varop, int count)
10569 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10570 if (tem)
10571 return tem;
10573 if (!x)
10574 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10575 if (GET_MODE (x) != result_mode)
10576 x = gen_lowpart (result_mode, x);
10577 return x;
10581 /* Like recog, but we receive the address of a pointer to a new pattern.
10582 We try to match the rtx that the pointer points to.
10583 If that fails, we may try to modify or replace the pattern,
10584 storing the replacement into the same pointer object.
10586 Modifications include deletion or addition of CLOBBERs.
10588 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10589 the CLOBBERs are placed.
10591 The value is the final insn code from the pattern ultimately matched,
10592 or -1. */
10594 static int
10595 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10597 rtx pat = *pnewpat;
10598 rtx pat_without_clobbers;
10599 int insn_code_number;
10600 int num_clobbers_to_add = 0;
10601 int i;
10602 rtx notes = NULL_RTX;
10603 rtx old_notes, old_pat;
10604 int old_icode;
10606 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10607 we use to indicate that something didn't match. If we find such a
10608 thing, force rejection. */
10609 if (GET_CODE (pat) == PARALLEL)
10610 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10611 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10612 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10613 return -1;
10615 old_pat = PATTERN (insn);
10616 old_notes = REG_NOTES (insn);
10617 PATTERN (insn) = pat;
10618 REG_NOTES (insn) = NULL_RTX;
10620 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10621 if (dump_file && (dump_flags & TDF_DETAILS))
10623 if (insn_code_number < 0)
10624 fputs ("Failed to match this instruction:\n", dump_file);
10625 else
10626 fputs ("Successfully matched this instruction:\n", dump_file);
10627 print_rtl_single (dump_file, pat);
10630 /* If it isn't, there is the possibility that we previously had an insn
10631 that clobbered some register as a side effect, but the combined
10632 insn doesn't need to do that. So try once more without the clobbers
10633 unless this represents an ASM insn. */
10635 if (insn_code_number < 0 && ! check_asm_operands (pat)
10636 && GET_CODE (pat) == PARALLEL)
10638 int pos;
10640 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10641 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10643 if (i != pos)
10644 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10645 pos++;
10648 SUBST_INT (XVECLEN (pat, 0), pos);
10650 if (pos == 1)
10651 pat = XVECEXP (pat, 0, 0);
10653 PATTERN (insn) = pat;
10654 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10655 if (dump_file && (dump_flags & TDF_DETAILS))
10657 if (insn_code_number < 0)
10658 fputs ("Failed to match this instruction:\n", dump_file);
10659 else
10660 fputs ("Successfully matched this instruction:\n", dump_file);
10661 print_rtl_single (dump_file, pat);
10665 pat_without_clobbers = pat;
10667 PATTERN (insn) = old_pat;
10668 REG_NOTES (insn) = old_notes;
10670 /* Recognize all noop sets, these will be killed by followup pass. */
10671 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10672 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10674 /* If we had any clobbers to add, make a new pattern than contains
10675 them. Then check to make sure that all of them are dead. */
10676 if (num_clobbers_to_add)
10678 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10679 rtvec_alloc (GET_CODE (pat) == PARALLEL
10680 ? (XVECLEN (pat, 0)
10681 + num_clobbers_to_add)
10682 : num_clobbers_to_add + 1));
10684 if (GET_CODE (pat) == PARALLEL)
10685 for (i = 0; i < XVECLEN (pat, 0); i++)
10686 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10687 else
10688 XVECEXP (newpat, 0, 0) = pat;
10690 add_clobbers (newpat, insn_code_number);
10692 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10693 i < XVECLEN (newpat, 0); i++)
10695 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10696 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10697 return -1;
10698 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10700 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10701 notes = alloc_reg_note (REG_UNUSED,
10702 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10705 pat = newpat;
10708 if (insn_code_number >= 0
10709 && insn_code_number != NOOP_MOVE_INSN_CODE)
10711 old_pat = PATTERN (insn);
10712 old_notes = REG_NOTES (insn);
10713 old_icode = INSN_CODE (insn);
10714 PATTERN (insn) = pat;
10715 REG_NOTES (insn) = notes;
10717 /* Allow targets to reject combined insn. */
10718 if (!targetm.legitimate_combined_insn (insn))
10720 if (dump_file && (dump_flags & TDF_DETAILS))
10721 fputs ("Instruction not appropriate for target.",
10722 dump_file);
10724 /* Callers expect recog_for_combine to strip
10725 clobbers from the pattern on failure. */
10726 pat = pat_without_clobbers;
10727 notes = NULL_RTX;
10729 insn_code_number = -1;
10732 PATTERN (insn) = old_pat;
10733 REG_NOTES (insn) = old_notes;
10734 INSN_CODE (insn) = old_icode;
10737 *pnewpat = pat;
10738 *pnotes = notes;
10740 return insn_code_number;
10743 /* Like gen_lowpart_general but for use by combine. In combine it
10744 is not possible to create any new pseudoregs. However, it is
10745 safe to create invalid memory addresses, because combine will
10746 try to recognize them and all they will do is make the combine
10747 attempt fail.
10749 If for some reason this cannot do its job, an rtx
10750 (clobber (const_int 0)) is returned.
10751 An insn containing that will not be recognized. */
10753 static rtx
10754 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10756 enum machine_mode imode = GET_MODE (x);
10757 unsigned int osize = GET_MODE_SIZE (omode);
10758 unsigned int isize = GET_MODE_SIZE (imode);
10759 rtx result;
10761 if (omode == imode)
10762 return x;
10764 /* We can only support MODE being wider than a word if X is a
10765 constant integer or has a mode the same size. */
10766 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10767 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10768 goto fail;
10770 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10771 won't know what to do. So we will strip off the SUBREG here and
10772 process normally. */
10773 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10775 x = SUBREG_REG (x);
10777 /* For use in case we fall down into the address adjustments
10778 further below, we need to adjust the known mode and size of
10779 x; imode and isize, since we just adjusted x. */
10780 imode = GET_MODE (x);
10782 if (imode == omode)
10783 return x;
10785 isize = GET_MODE_SIZE (imode);
10788 result = gen_lowpart_common (omode, x);
10790 if (result)
10791 return result;
10793 if (MEM_P (x))
10795 int offset = 0;
10797 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10798 address. */
10799 if (MEM_VOLATILE_P (x)
10800 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10801 goto fail;
10803 /* If we want to refer to something bigger than the original memref,
10804 generate a paradoxical subreg instead. That will force a reload
10805 of the original memref X. */
10806 if (isize < osize)
10807 return gen_rtx_SUBREG (omode, x, 0);
10809 if (WORDS_BIG_ENDIAN)
10810 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10812 /* Adjust the address so that the address-after-the-data is
10813 unchanged. */
10814 if (BYTES_BIG_ENDIAN)
10815 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10817 return adjust_address_nv (x, omode, offset);
10820 /* If X is a comparison operator, rewrite it in a new mode. This
10821 probably won't match, but may allow further simplifications. */
10822 else if (COMPARISON_P (x))
10823 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10825 /* If we couldn't simplify X any other way, just enclose it in a
10826 SUBREG. Normally, this SUBREG won't match, but some patterns may
10827 include an explicit SUBREG or we may simplify it further in combine. */
10828 else
10830 int offset = 0;
10831 rtx res;
10833 offset = subreg_lowpart_offset (omode, imode);
10834 if (imode == VOIDmode)
10836 imode = int_mode_for_mode (omode);
10837 x = gen_lowpart_common (imode, x);
10838 if (x == NULL)
10839 goto fail;
10841 res = simplify_gen_subreg (omode, x, imode, offset);
10842 if (res)
10843 return res;
10846 fail:
10847 return gen_rtx_CLOBBER (omode, const0_rtx);
10850 /* Try to simplify a comparison between OP0 and a constant OP1,
10851 where CODE is the comparison code that will be tested, into a
10852 (CODE OP0 const0_rtx) form.
10854 The result is a possibly different comparison code to use.
10855 *POP1 may be updated. */
10857 static enum rtx_code
10858 simplify_compare_const (enum rtx_code code, enum machine_mode mode,
10859 rtx op0, rtx *pop1)
10861 unsigned int mode_width = GET_MODE_PRECISION (mode);
10862 HOST_WIDE_INT const_op = INTVAL (*pop1);
10864 /* Get the constant we are comparing against and turn off all bits
10865 not on in our mode. */
10866 if (mode != VOIDmode)
10867 const_op = trunc_int_for_mode (const_op, mode);
10869 /* If we are comparing against a constant power of two and the value
10870 being compared can only have that single bit nonzero (e.g., it was
10871 `and'ed with that bit), we can replace this with a comparison
10872 with zero. */
10873 if (const_op
10874 && (code == EQ || code == NE || code == GE || code == GEU
10875 || code == LT || code == LTU)
10876 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10877 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
10878 && (nonzero_bits (op0, mode)
10879 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
10881 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10882 const_op = 0;
10885 /* Similarly, if we are comparing a value known to be either -1 or
10886 0 with -1, change it to the opposite comparison against zero. */
10887 if (const_op == -1
10888 && (code == EQ || code == NE || code == GT || code == LE
10889 || code == GEU || code == LTU)
10890 && num_sign_bit_copies (op0, mode) == mode_width)
10892 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10893 const_op = 0;
10896 /* Do some canonicalizations based on the comparison code. We prefer
10897 comparisons against zero and then prefer equality comparisons.
10898 If we can reduce the size of a constant, we will do that too. */
10899 switch (code)
10901 case LT:
10902 /* < C is equivalent to <= (C - 1) */
10903 if (const_op > 0)
10905 const_op -= 1;
10906 code = LE;
10907 /* ... fall through to LE case below. */
10909 else
10910 break;
10912 case LE:
10913 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10914 if (const_op < 0)
10916 const_op += 1;
10917 code = LT;
10920 /* If we are doing a <= 0 comparison on a value known to have
10921 a zero sign bit, we can replace this with == 0. */
10922 else if (const_op == 0
10923 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10924 && (nonzero_bits (op0, mode)
10925 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10926 == 0)
10927 code = EQ;
10928 break;
10930 case GE:
10931 /* >= C is equivalent to > (C - 1). */
10932 if (const_op > 0)
10934 const_op -= 1;
10935 code = GT;
10936 /* ... fall through to GT below. */
10938 else
10939 break;
10941 case GT:
10942 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10943 if (const_op < 0)
10945 const_op += 1;
10946 code = GE;
10949 /* If we are doing a > 0 comparison on a value known to have
10950 a zero sign bit, we can replace this with != 0. */
10951 else if (const_op == 0
10952 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10953 && (nonzero_bits (op0, mode)
10954 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10955 == 0)
10956 code = NE;
10957 break;
10959 case LTU:
10960 /* < C is equivalent to <= (C - 1). */
10961 if (const_op > 0)
10963 const_op -= 1;
10964 code = LEU;
10965 /* ... fall through ... */
10967 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10968 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10969 && (unsigned HOST_WIDE_INT) const_op
10970 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10972 const_op = 0;
10973 code = GE;
10974 break;
10976 else
10977 break;
10979 case LEU:
10980 /* unsigned <= 0 is equivalent to == 0 */
10981 if (const_op == 0)
10982 code = EQ;
10983 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10984 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10985 && (unsigned HOST_WIDE_INT) const_op
10986 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10988 const_op = 0;
10989 code = GE;
10991 break;
10993 case GEU:
10994 /* >= C is equivalent to > (C - 1). */
10995 if (const_op > 1)
10997 const_op -= 1;
10998 code = GTU;
10999 /* ... fall through ... */
11002 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11003 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11004 && (unsigned HOST_WIDE_INT) const_op
11005 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11007 const_op = 0;
11008 code = LT;
11009 break;
11011 else
11012 break;
11014 case GTU:
11015 /* unsigned > 0 is equivalent to != 0 */
11016 if (const_op == 0)
11017 code = NE;
11018 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11019 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11020 && (unsigned HOST_WIDE_INT) const_op
11021 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11023 const_op = 0;
11024 code = LT;
11026 break;
11028 default:
11029 break;
11032 *pop1 = GEN_INT (const_op);
11033 return code;
11036 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11037 comparison code that will be tested.
11039 The result is a possibly different comparison code to use. *POP0 and
11040 *POP1 may be updated.
11042 It is possible that we might detect that a comparison is either always
11043 true or always false. However, we do not perform general constant
11044 folding in combine, so this knowledge isn't useful. Such tautologies
11045 should have been detected earlier. Hence we ignore all such cases. */
11047 static enum rtx_code
11048 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11050 rtx op0 = *pop0;
11051 rtx op1 = *pop1;
11052 rtx tem, tem1;
11053 int i;
11054 enum machine_mode mode, tmode;
11056 /* Try a few ways of applying the same transformation to both operands. */
11057 while (1)
11059 #ifndef WORD_REGISTER_OPERATIONS
11060 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11061 so check specially. */
11062 if (code != GTU && code != GEU && code != LTU && code != LEU
11063 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11064 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11065 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11066 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11067 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11068 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11069 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11070 && CONST_INT_P (XEXP (op0, 1))
11071 && XEXP (op0, 1) == XEXP (op1, 1)
11072 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11073 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11074 && (INTVAL (XEXP (op0, 1))
11075 == (GET_MODE_PRECISION (GET_MODE (op0))
11076 - (GET_MODE_PRECISION
11077 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11079 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11080 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11082 #endif
11084 /* If both operands are the same constant shift, see if we can ignore the
11085 shift. We can if the shift is a rotate or if the bits shifted out of
11086 this shift are known to be zero for both inputs and if the type of
11087 comparison is compatible with the shift. */
11088 if (GET_CODE (op0) == GET_CODE (op1)
11089 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11090 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11091 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11092 && (code != GT && code != LT && code != GE && code != LE))
11093 || (GET_CODE (op0) == ASHIFTRT
11094 && (code != GTU && code != LTU
11095 && code != GEU && code != LEU)))
11096 && CONST_INT_P (XEXP (op0, 1))
11097 && INTVAL (XEXP (op0, 1)) >= 0
11098 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11099 && XEXP (op0, 1) == XEXP (op1, 1))
11101 enum machine_mode mode = GET_MODE (op0);
11102 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11103 int shift_count = INTVAL (XEXP (op0, 1));
11105 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11106 mask &= (mask >> shift_count) << shift_count;
11107 else if (GET_CODE (op0) == ASHIFT)
11108 mask = (mask & (mask << shift_count)) >> shift_count;
11110 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11111 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11112 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11113 else
11114 break;
11117 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11118 SUBREGs are of the same mode, and, in both cases, the AND would
11119 be redundant if the comparison was done in the narrower mode,
11120 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11121 and the operand's possibly nonzero bits are 0xffffff01; in that case
11122 if we only care about QImode, we don't need the AND). This case
11123 occurs if the output mode of an scc insn is not SImode and
11124 STORE_FLAG_VALUE == 1 (e.g., the 386).
11126 Similarly, check for a case where the AND's are ZERO_EXTEND
11127 operations from some narrower mode even though a SUBREG is not
11128 present. */
11130 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11131 && CONST_INT_P (XEXP (op0, 1))
11132 && CONST_INT_P (XEXP (op1, 1)))
11134 rtx inner_op0 = XEXP (op0, 0);
11135 rtx inner_op1 = XEXP (op1, 0);
11136 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11137 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11138 int changed = 0;
11140 if (paradoxical_subreg_p (inner_op0)
11141 && GET_CODE (inner_op1) == SUBREG
11142 && (GET_MODE (SUBREG_REG (inner_op0))
11143 == GET_MODE (SUBREG_REG (inner_op1)))
11144 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11145 <= HOST_BITS_PER_WIDE_INT)
11146 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11147 GET_MODE (SUBREG_REG (inner_op0)))))
11148 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11149 GET_MODE (SUBREG_REG (inner_op1))))))
11151 op0 = SUBREG_REG (inner_op0);
11152 op1 = SUBREG_REG (inner_op1);
11154 /* The resulting comparison is always unsigned since we masked
11155 off the original sign bit. */
11156 code = unsigned_condition (code);
11158 changed = 1;
11161 else if (c0 == c1)
11162 for (tmode = GET_CLASS_NARROWEST_MODE
11163 (GET_MODE_CLASS (GET_MODE (op0)));
11164 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11165 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11167 op0 = gen_lowpart (tmode, inner_op0);
11168 op1 = gen_lowpart (tmode, inner_op1);
11169 code = unsigned_condition (code);
11170 changed = 1;
11171 break;
11174 if (! changed)
11175 break;
11178 /* If both operands are NOT, we can strip off the outer operation
11179 and adjust the comparison code for swapped operands; similarly for
11180 NEG, except that this must be an equality comparison. */
11181 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11182 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11183 && (code == EQ || code == NE)))
11184 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11186 else
11187 break;
11190 /* If the first operand is a constant, swap the operands and adjust the
11191 comparison code appropriately, but don't do this if the second operand
11192 is already a constant integer. */
11193 if (swap_commutative_operands_p (op0, op1))
11195 tem = op0, op0 = op1, op1 = tem;
11196 code = swap_condition (code);
11199 /* We now enter a loop during which we will try to simplify the comparison.
11200 For the most part, we only are concerned with comparisons with zero,
11201 but some things may really be comparisons with zero but not start
11202 out looking that way. */
11204 while (CONST_INT_P (op1))
11206 enum machine_mode mode = GET_MODE (op0);
11207 unsigned int mode_width = GET_MODE_PRECISION (mode);
11208 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11209 int equality_comparison_p;
11210 int sign_bit_comparison_p;
11211 int unsigned_comparison_p;
11212 HOST_WIDE_INT const_op;
11214 /* We only want to handle integral modes. This catches VOIDmode,
11215 CCmode, and the floating-point modes. An exception is that we
11216 can handle VOIDmode if OP0 is a COMPARE or a comparison
11217 operation. */
11219 if (GET_MODE_CLASS (mode) != MODE_INT
11220 && ! (mode == VOIDmode
11221 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11222 break;
11224 /* Try to simplify the compare to constant, possibly changing the
11225 comparison op, and/or changing op1 to zero. */
11226 code = simplify_compare_const (code, mode, op0, &op1);
11227 const_op = INTVAL (op1);
11229 /* Compute some predicates to simplify code below. */
11231 equality_comparison_p = (code == EQ || code == NE);
11232 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11233 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11234 || code == GEU);
11236 /* If this is a sign bit comparison and we can do arithmetic in
11237 MODE, say that we will only be needing the sign bit of OP0. */
11238 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11239 op0 = force_to_mode (op0, mode,
11240 (unsigned HOST_WIDE_INT) 1
11241 << (GET_MODE_PRECISION (mode) - 1),
11244 /* Now try cases based on the opcode of OP0. If none of the cases
11245 does a "continue", we exit this loop immediately after the
11246 switch. */
11248 switch (GET_CODE (op0))
11250 case ZERO_EXTRACT:
11251 /* If we are extracting a single bit from a variable position in
11252 a constant that has only a single bit set and are comparing it
11253 with zero, we can convert this into an equality comparison
11254 between the position and the location of the single bit. */
11255 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11256 have already reduced the shift count modulo the word size. */
11257 if (!SHIFT_COUNT_TRUNCATED
11258 && CONST_INT_P (XEXP (op0, 0))
11259 && XEXP (op0, 1) == const1_rtx
11260 && equality_comparison_p && const_op == 0
11261 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11263 if (BITS_BIG_ENDIAN)
11264 i = BITS_PER_WORD - 1 - i;
11266 op0 = XEXP (op0, 2);
11267 op1 = GEN_INT (i);
11268 const_op = i;
11270 /* Result is nonzero iff shift count is equal to I. */
11271 code = reverse_condition (code);
11272 continue;
11275 /* ... fall through ... */
11277 case SIGN_EXTRACT:
11278 tem = expand_compound_operation (op0);
11279 if (tem != op0)
11281 op0 = tem;
11282 continue;
11284 break;
11286 case NOT:
11287 /* If testing for equality, we can take the NOT of the constant. */
11288 if (equality_comparison_p
11289 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11291 op0 = XEXP (op0, 0);
11292 op1 = tem;
11293 continue;
11296 /* If just looking at the sign bit, reverse the sense of the
11297 comparison. */
11298 if (sign_bit_comparison_p)
11300 op0 = XEXP (op0, 0);
11301 code = (code == GE ? LT : GE);
11302 continue;
11304 break;
11306 case NEG:
11307 /* If testing for equality, we can take the NEG of the constant. */
11308 if (equality_comparison_p
11309 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11311 op0 = XEXP (op0, 0);
11312 op1 = tem;
11313 continue;
11316 /* The remaining cases only apply to comparisons with zero. */
11317 if (const_op != 0)
11318 break;
11320 /* When X is ABS or is known positive,
11321 (neg X) is < 0 if and only if X != 0. */
11323 if (sign_bit_comparison_p
11324 && (GET_CODE (XEXP (op0, 0)) == ABS
11325 || (mode_width <= HOST_BITS_PER_WIDE_INT
11326 && (nonzero_bits (XEXP (op0, 0), mode)
11327 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11328 == 0)))
11330 op0 = XEXP (op0, 0);
11331 code = (code == LT ? NE : EQ);
11332 continue;
11335 /* If we have NEG of something whose two high-order bits are the
11336 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11337 if (num_sign_bit_copies (op0, mode) >= 2)
11339 op0 = XEXP (op0, 0);
11340 code = swap_condition (code);
11341 continue;
11343 break;
11345 case ROTATE:
11346 /* If we are testing equality and our count is a constant, we
11347 can perform the inverse operation on our RHS. */
11348 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11349 && (tem = simplify_binary_operation (ROTATERT, mode,
11350 op1, XEXP (op0, 1))) != 0)
11352 op0 = XEXP (op0, 0);
11353 op1 = tem;
11354 continue;
11357 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11358 a particular bit. Convert it to an AND of a constant of that
11359 bit. This will be converted into a ZERO_EXTRACT. */
11360 if (const_op == 0 && sign_bit_comparison_p
11361 && CONST_INT_P (XEXP (op0, 1))
11362 && mode_width <= HOST_BITS_PER_WIDE_INT)
11364 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11365 ((unsigned HOST_WIDE_INT) 1
11366 << (mode_width - 1
11367 - INTVAL (XEXP (op0, 1)))));
11368 code = (code == LT ? NE : EQ);
11369 continue;
11372 /* Fall through. */
11374 case ABS:
11375 /* ABS is ignorable inside an equality comparison with zero. */
11376 if (const_op == 0 && equality_comparison_p)
11378 op0 = XEXP (op0, 0);
11379 continue;
11381 break;
11383 case SIGN_EXTEND:
11384 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11385 (compare FOO CONST) if CONST fits in FOO's mode and we
11386 are either testing inequality or have an unsigned
11387 comparison with ZERO_EXTEND or a signed comparison with
11388 SIGN_EXTEND. But don't do it if we don't have a compare
11389 insn of the given mode, since we'd have to revert it
11390 later on, and then we wouldn't know whether to sign- or
11391 zero-extend. */
11392 mode = GET_MODE (XEXP (op0, 0));
11393 if (GET_MODE_CLASS (mode) == MODE_INT
11394 && ! unsigned_comparison_p
11395 && HWI_COMPUTABLE_MODE_P (mode)
11396 && trunc_int_for_mode (const_op, mode) == const_op
11397 && have_insn_for (COMPARE, mode))
11399 op0 = XEXP (op0, 0);
11400 continue;
11402 break;
11404 case SUBREG:
11405 /* Check for the case where we are comparing A - C1 with C2, that is
11407 (subreg:MODE (plus (A) (-C1))) op (C2)
11409 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11410 comparison in the wider mode. One of the following two conditions
11411 must be true in order for this to be valid:
11413 1. The mode extension results in the same bit pattern being added
11414 on both sides and the comparison is equality or unsigned. As
11415 C2 has been truncated to fit in MODE, the pattern can only be
11416 all 0s or all 1s.
11418 2. The mode extension results in the sign bit being copied on
11419 each side.
11421 The difficulty here is that we have predicates for A but not for
11422 (A - C1) so we need to check that C1 is within proper bounds so
11423 as to perturbate A as little as possible. */
11425 if (mode_width <= HOST_BITS_PER_WIDE_INT
11426 && subreg_lowpart_p (op0)
11427 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11428 && GET_CODE (SUBREG_REG (op0)) == PLUS
11429 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11431 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11432 rtx a = XEXP (SUBREG_REG (op0), 0);
11433 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11435 if ((c1 > 0
11436 && (unsigned HOST_WIDE_INT) c1
11437 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11438 && (equality_comparison_p || unsigned_comparison_p)
11439 /* (A - C1) zero-extends if it is positive and sign-extends
11440 if it is negative, C2 both zero- and sign-extends. */
11441 && ((0 == (nonzero_bits (a, inner_mode)
11442 & ~GET_MODE_MASK (mode))
11443 && const_op >= 0)
11444 /* (A - C1) sign-extends if it is positive and 1-extends
11445 if it is negative, C2 both sign- and 1-extends. */
11446 || (num_sign_bit_copies (a, inner_mode)
11447 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11448 - mode_width)
11449 && const_op < 0)))
11450 || ((unsigned HOST_WIDE_INT) c1
11451 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11452 /* (A - C1) always sign-extends, like C2. */
11453 && num_sign_bit_copies (a, inner_mode)
11454 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11455 - (mode_width - 1))))
11457 op0 = SUBREG_REG (op0);
11458 continue;
11462 /* If the inner mode is narrower and we are extracting the low part,
11463 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11464 if (subreg_lowpart_p (op0)
11465 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11466 /* Fall through */ ;
11467 else
11468 break;
11470 /* ... fall through ... */
11472 case ZERO_EXTEND:
11473 mode = GET_MODE (XEXP (op0, 0));
11474 if (GET_MODE_CLASS (mode) == MODE_INT
11475 && (unsigned_comparison_p || equality_comparison_p)
11476 && HWI_COMPUTABLE_MODE_P (mode)
11477 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11478 && const_op >= 0
11479 && have_insn_for (COMPARE, mode))
11481 op0 = XEXP (op0, 0);
11482 continue;
11484 break;
11486 case PLUS:
11487 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11488 this for equality comparisons due to pathological cases involving
11489 overflows. */
11490 if (equality_comparison_p
11491 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11492 op1, XEXP (op0, 1))))
11494 op0 = XEXP (op0, 0);
11495 op1 = tem;
11496 continue;
11499 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11500 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11501 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11503 op0 = XEXP (XEXP (op0, 0), 0);
11504 code = (code == LT ? EQ : NE);
11505 continue;
11507 break;
11509 case MINUS:
11510 /* We used to optimize signed comparisons against zero, but that
11511 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11512 arrive here as equality comparisons, or (GEU, LTU) are
11513 optimized away. No need to special-case them. */
11515 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11516 (eq B (minus A C)), whichever simplifies. We can only do
11517 this for equality comparisons due to pathological cases involving
11518 overflows. */
11519 if (equality_comparison_p
11520 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11521 XEXP (op0, 1), op1)))
11523 op0 = XEXP (op0, 0);
11524 op1 = tem;
11525 continue;
11528 if (equality_comparison_p
11529 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11530 XEXP (op0, 0), op1)))
11532 op0 = XEXP (op0, 1);
11533 op1 = tem;
11534 continue;
11537 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11538 of bits in X minus 1, is one iff X > 0. */
11539 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11540 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11541 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11542 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11544 op0 = XEXP (op0, 1);
11545 code = (code == GE ? LE : GT);
11546 continue;
11548 break;
11550 case XOR:
11551 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11552 if C is zero or B is a constant. */
11553 if (equality_comparison_p
11554 && 0 != (tem = simplify_binary_operation (XOR, mode,
11555 XEXP (op0, 1), op1)))
11557 op0 = XEXP (op0, 0);
11558 op1 = tem;
11559 continue;
11561 break;
11563 case EQ: case NE:
11564 case UNEQ: case LTGT:
11565 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11566 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11567 case UNORDERED: case ORDERED:
11568 /* We can't do anything if OP0 is a condition code value, rather
11569 than an actual data value. */
11570 if (const_op != 0
11571 || CC0_P (XEXP (op0, 0))
11572 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11573 break;
11575 /* Get the two operands being compared. */
11576 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11577 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11578 else
11579 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11581 /* Check for the cases where we simply want the result of the
11582 earlier test or the opposite of that result. */
11583 if (code == NE || code == EQ
11584 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11585 && (code == LT || code == GE)))
11587 enum rtx_code new_code;
11588 if (code == LT || code == NE)
11589 new_code = GET_CODE (op0);
11590 else
11591 new_code = reversed_comparison_code (op0, NULL);
11593 if (new_code != UNKNOWN)
11595 code = new_code;
11596 op0 = tem;
11597 op1 = tem1;
11598 continue;
11601 break;
11603 case IOR:
11604 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11605 iff X <= 0. */
11606 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11607 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11608 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11610 op0 = XEXP (op0, 1);
11611 code = (code == GE ? GT : LE);
11612 continue;
11614 break;
11616 case AND:
11617 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11618 will be converted to a ZERO_EXTRACT later. */
11619 if (const_op == 0 && equality_comparison_p
11620 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11621 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11623 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11624 XEXP (XEXP (op0, 0), 1));
11625 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11626 continue;
11629 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11630 zero and X is a comparison and C1 and C2 describe only bits set
11631 in STORE_FLAG_VALUE, we can compare with X. */
11632 if (const_op == 0 && equality_comparison_p
11633 && mode_width <= HOST_BITS_PER_WIDE_INT
11634 && CONST_INT_P (XEXP (op0, 1))
11635 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11636 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11637 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11638 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11640 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11641 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11642 if ((~STORE_FLAG_VALUE & mask) == 0
11643 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11644 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11645 && COMPARISON_P (tem))))
11647 op0 = XEXP (XEXP (op0, 0), 0);
11648 continue;
11652 /* If we are doing an equality comparison of an AND of a bit equal
11653 to the sign bit, replace this with a LT or GE comparison of
11654 the underlying value. */
11655 if (equality_comparison_p
11656 && const_op == 0
11657 && CONST_INT_P (XEXP (op0, 1))
11658 && mode_width <= HOST_BITS_PER_WIDE_INT
11659 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11660 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11662 op0 = XEXP (op0, 0);
11663 code = (code == EQ ? GE : LT);
11664 continue;
11667 /* If this AND operation is really a ZERO_EXTEND from a narrower
11668 mode, the constant fits within that mode, and this is either an
11669 equality or unsigned comparison, try to do this comparison in
11670 the narrower mode.
11672 Note that in:
11674 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11675 -> (ne:DI (reg:SI 4) (const_int 0))
11677 unless TRULY_NOOP_TRUNCATION allows it or the register is
11678 known to hold a value of the required mode the
11679 transformation is invalid. */
11680 if ((equality_comparison_p || unsigned_comparison_p)
11681 && CONST_INT_P (XEXP (op0, 1))
11682 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11683 & GET_MODE_MASK (mode))
11684 + 1)) >= 0
11685 && const_op >> i == 0
11686 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11687 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11688 || (REG_P (XEXP (op0, 0))
11689 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11691 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11692 continue;
11695 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11696 fits in both M1 and M2 and the SUBREG is either paradoxical
11697 or represents the low part, permute the SUBREG and the AND
11698 and try again. */
11699 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11701 unsigned HOST_WIDE_INT c1;
11702 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11703 /* Require an integral mode, to avoid creating something like
11704 (AND:SF ...). */
11705 if (SCALAR_INT_MODE_P (tmode)
11706 /* It is unsafe to commute the AND into the SUBREG if the
11707 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11708 not defined. As originally written the upper bits
11709 have a defined value due to the AND operation.
11710 However, if we commute the AND inside the SUBREG then
11711 they no longer have defined values and the meaning of
11712 the code has been changed. */
11713 && (0
11714 #ifdef WORD_REGISTER_OPERATIONS
11715 || (mode_width > GET_MODE_PRECISION (tmode)
11716 && mode_width <= BITS_PER_WORD)
11717 #endif
11718 || (mode_width <= GET_MODE_PRECISION (tmode)
11719 && subreg_lowpart_p (XEXP (op0, 0))))
11720 && CONST_INT_P (XEXP (op0, 1))
11721 && mode_width <= HOST_BITS_PER_WIDE_INT
11722 && HWI_COMPUTABLE_MODE_P (tmode)
11723 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11724 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11725 && c1 != mask
11726 && c1 != GET_MODE_MASK (tmode))
11728 op0 = simplify_gen_binary (AND, tmode,
11729 SUBREG_REG (XEXP (op0, 0)),
11730 gen_int_mode (c1, tmode));
11731 op0 = gen_lowpart (mode, op0);
11732 continue;
11736 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11737 if (const_op == 0 && equality_comparison_p
11738 && XEXP (op0, 1) == const1_rtx
11739 && GET_CODE (XEXP (op0, 0)) == NOT)
11741 op0 = simplify_and_const_int (NULL_RTX, mode,
11742 XEXP (XEXP (op0, 0), 0), 1);
11743 code = (code == NE ? EQ : NE);
11744 continue;
11747 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11748 (eq (and (lshiftrt X) 1) 0).
11749 Also handle the case where (not X) is expressed using xor. */
11750 if (const_op == 0 && equality_comparison_p
11751 && XEXP (op0, 1) == const1_rtx
11752 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11754 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11755 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11757 if (GET_CODE (shift_op) == NOT
11758 || (GET_CODE (shift_op) == XOR
11759 && CONST_INT_P (XEXP (shift_op, 1))
11760 && CONST_INT_P (shift_count)
11761 && HWI_COMPUTABLE_MODE_P (mode)
11762 && (UINTVAL (XEXP (shift_op, 1))
11763 == (unsigned HOST_WIDE_INT) 1
11764 << INTVAL (shift_count))))
11767 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11768 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11769 code = (code == NE ? EQ : NE);
11770 continue;
11773 break;
11775 case ASHIFT:
11776 /* If we have (compare (ashift FOO N) (const_int C)) and
11777 the high order N bits of FOO (N+1 if an inequality comparison)
11778 are known to be zero, we can do this by comparing FOO with C
11779 shifted right N bits so long as the low-order N bits of C are
11780 zero. */
11781 if (CONST_INT_P (XEXP (op0, 1))
11782 && INTVAL (XEXP (op0, 1)) >= 0
11783 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11784 < HOST_BITS_PER_WIDE_INT)
11785 && (((unsigned HOST_WIDE_INT) const_op
11786 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11787 - 1)) == 0)
11788 && mode_width <= HOST_BITS_PER_WIDE_INT
11789 && (nonzero_bits (XEXP (op0, 0), mode)
11790 & ~(mask >> (INTVAL (XEXP (op0, 1))
11791 + ! equality_comparison_p))) == 0)
11793 /* We must perform a logical shift, not an arithmetic one,
11794 as we want the top N bits of C to be zero. */
11795 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11797 temp >>= INTVAL (XEXP (op0, 1));
11798 op1 = gen_int_mode (temp, mode);
11799 op0 = XEXP (op0, 0);
11800 continue;
11803 /* If we are doing a sign bit comparison, it means we are testing
11804 a particular bit. Convert it to the appropriate AND. */
11805 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11806 && mode_width <= HOST_BITS_PER_WIDE_INT)
11808 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11809 ((unsigned HOST_WIDE_INT) 1
11810 << (mode_width - 1
11811 - INTVAL (XEXP (op0, 1)))));
11812 code = (code == LT ? NE : EQ);
11813 continue;
11816 /* If this an equality comparison with zero and we are shifting
11817 the low bit to the sign bit, we can convert this to an AND of the
11818 low-order bit. */
11819 if (const_op == 0 && equality_comparison_p
11820 && CONST_INT_P (XEXP (op0, 1))
11821 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11823 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11824 continue;
11826 break;
11828 case ASHIFTRT:
11829 /* If this is an equality comparison with zero, we can do this
11830 as a logical shift, which might be much simpler. */
11831 if (equality_comparison_p && const_op == 0
11832 && CONST_INT_P (XEXP (op0, 1)))
11834 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11835 XEXP (op0, 0),
11836 INTVAL (XEXP (op0, 1)));
11837 continue;
11840 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11841 do the comparison in a narrower mode. */
11842 if (! unsigned_comparison_p
11843 && CONST_INT_P (XEXP (op0, 1))
11844 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11845 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11846 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11847 MODE_INT, 1)) != BLKmode
11848 && (((unsigned HOST_WIDE_INT) const_op
11849 + (GET_MODE_MASK (tmode) >> 1) + 1)
11850 <= GET_MODE_MASK (tmode)))
11852 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11853 continue;
11856 /* Likewise if OP0 is a PLUS of a sign extension with a
11857 constant, which is usually represented with the PLUS
11858 between the shifts. */
11859 if (! unsigned_comparison_p
11860 && CONST_INT_P (XEXP (op0, 1))
11861 && GET_CODE (XEXP (op0, 0)) == PLUS
11862 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11863 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11864 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11865 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11866 MODE_INT, 1)) != BLKmode
11867 && (((unsigned HOST_WIDE_INT) const_op
11868 + (GET_MODE_MASK (tmode) >> 1) + 1)
11869 <= GET_MODE_MASK (tmode)))
11871 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11872 rtx add_const = XEXP (XEXP (op0, 0), 1);
11873 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11874 add_const, XEXP (op0, 1));
11876 op0 = simplify_gen_binary (PLUS, tmode,
11877 gen_lowpart (tmode, inner),
11878 new_const);
11879 continue;
11882 /* ... fall through ... */
11883 case LSHIFTRT:
11884 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11885 the low order N bits of FOO are known to be zero, we can do this
11886 by comparing FOO with C shifted left N bits so long as no
11887 overflow occurs. Even if the low order N bits of FOO aren't known
11888 to be zero, if the comparison is >= or < we can use the same
11889 optimization and for > or <= by setting all the low
11890 order N bits in the comparison constant. */
11891 if (CONST_INT_P (XEXP (op0, 1))
11892 && INTVAL (XEXP (op0, 1)) > 0
11893 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11894 && mode_width <= HOST_BITS_PER_WIDE_INT
11895 && (((unsigned HOST_WIDE_INT) const_op
11896 + (GET_CODE (op0) != LSHIFTRT
11897 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11898 + 1)
11899 : 0))
11900 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11902 unsigned HOST_WIDE_INT low_bits
11903 = (nonzero_bits (XEXP (op0, 0), mode)
11904 & (((unsigned HOST_WIDE_INT) 1
11905 << INTVAL (XEXP (op0, 1))) - 1));
11906 if (low_bits == 0 || !equality_comparison_p)
11908 /* If the shift was logical, then we must make the condition
11909 unsigned. */
11910 if (GET_CODE (op0) == LSHIFTRT)
11911 code = unsigned_condition (code);
11913 const_op <<= INTVAL (XEXP (op0, 1));
11914 if (low_bits != 0
11915 && (code == GT || code == GTU
11916 || code == LE || code == LEU))
11917 const_op
11918 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11919 op1 = GEN_INT (const_op);
11920 op0 = XEXP (op0, 0);
11921 continue;
11925 /* If we are using this shift to extract just the sign bit, we
11926 can replace this with an LT or GE comparison. */
11927 if (const_op == 0
11928 && (equality_comparison_p || sign_bit_comparison_p)
11929 && CONST_INT_P (XEXP (op0, 1))
11930 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11932 op0 = XEXP (op0, 0);
11933 code = (code == NE || code == GT ? LT : GE);
11934 continue;
11936 break;
11938 default:
11939 break;
11942 break;
11945 /* Now make any compound operations involved in this comparison. Then,
11946 check for an outmost SUBREG on OP0 that is not doing anything or is
11947 paradoxical. The latter transformation must only be performed when
11948 it is known that the "extra" bits will be the same in op0 and op1 or
11949 that they don't matter. There are three cases to consider:
11951 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11952 care bits and we can assume they have any convenient value. So
11953 making the transformation is safe.
11955 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11956 In this case the upper bits of op0 are undefined. We should not make
11957 the simplification in that case as we do not know the contents of
11958 those bits.
11960 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11961 UNKNOWN. In that case we know those bits are zeros or ones. We must
11962 also be sure that they are the same as the upper bits of op1.
11964 We can never remove a SUBREG for a non-equality comparison because
11965 the sign bit is in a different place in the underlying object. */
11967 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11968 op1 = make_compound_operation (op1, SET);
11970 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11971 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11972 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11973 && (code == NE || code == EQ))
11975 if (paradoxical_subreg_p (op0))
11977 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11978 implemented. */
11979 if (REG_P (SUBREG_REG (op0)))
11981 op0 = SUBREG_REG (op0);
11982 op1 = gen_lowpart (GET_MODE (op0), op1);
11985 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
11986 <= HOST_BITS_PER_WIDE_INT)
11987 && (nonzero_bits (SUBREG_REG (op0),
11988 GET_MODE (SUBREG_REG (op0)))
11989 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11991 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11993 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11994 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11995 op0 = SUBREG_REG (op0), op1 = tem;
11999 /* We now do the opposite procedure: Some machines don't have compare
12000 insns in all modes. If OP0's mode is an integer mode smaller than a
12001 word and we can't do a compare in that mode, see if there is a larger
12002 mode for which we can do the compare. There are a number of cases in
12003 which we can use the wider mode. */
12005 mode = GET_MODE (op0);
12006 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12007 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12008 && ! have_insn_for (COMPARE, mode))
12009 for (tmode = GET_MODE_WIDER_MODE (mode);
12010 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12011 tmode = GET_MODE_WIDER_MODE (tmode))
12012 if (have_insn_for (COMPARE, tmode))
12014 int zero_extended;
12016 /* If this is a test for negative, we can make an explicit
12017 test of the sign bit. Test this first so we can use
12018 a paradoxical subreg to extend OP0. */
12020 if (op1 == const0_rtx && (code == LT || code == GE)
12021 && HWI_COMPUTABLE_MODE_P (mode))
12023 unsigned HOST_WIDE_INT sign
12024 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12025 op0 = simplify_gen_binary (AND, tmode,
12026 gen_lowpart (tmode, op0),
12027 gen_int_mode (sign, tmode));
12028 code = (code == LT) ? NE : EQ;
12029 break;
12032 /* If the only nonzero bits in OP0 and OP1 are those in the
12033 narrower mode and this is an equality or unsigned comparison,
12034 we can use the wider mode. Similarly for sign-extended
12035 values, in which case it is true for all comparisons. */
12036 zero_extended = ((code == EQ || code == NE
12037 || code == GEU || code == GTU
12038 || code == LEU || code == LTU)
12039 && (nonzero_bits (op0, tmode)
12040 & ~GET_MODE_MASK (mode)) == 0
12041 && ((CONST_INT_P (op1)
12042 || (nonzero_bits (op1, tmode)
12043 & ~GET_MODE_MASK (mode)) == 0)));
12045 if (zero_extended
12046 || ((num_sign_bit_copies (op0, tmode)
12047 > (unsigned int) (GET_MODE_PRECISION (tmode)
12048 - GET_MODE_PRECISION (mode)))
12049 && (num_sign_bit_copies (op1, tmode)
12050 > (unsigned int) (GET_MODE_PRECISION (tmode)
12051 - GET_MODE_PRECISION (mode)))))
12053 /* If OP0 is an AND and we don't have an AND in MODE either,
12054 make a new AND in the proper mode. */
12055 if (GET_CODE (op0) == AND
12056 && !have_insn_for (AND, mode))
12057 op0 = simplify_gen_binary (AND, tmode,
12058 gen_lowpart (tmode,
12059 XEXP (op0, 0)),
12060 gen_lowpart (tmode,
12061 XEXP (op0, 1)));
12062 else
12064 if (zero_extended)
12066 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12067 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12069 else
12071 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12072 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12074 break;
12079 /* We may have changed the comparison operands. Re-canonicalize. */
12080 if (swap_commutative_operands_p (op0, op1))
12082 tem = op0, op0 = op1, op1 = tem;
12083 code = swap_condition (code);
12086 /* If this machine only supports a subset of valid comparisons, see if we
12087 can convert an unsupported one into a supported one. */
12088 target_canonicalize_comparison (&code, &op0, &op1, 0);
12090 *pop0 = op0;
12091 *pop1 = op1;
12093 return code;
12096 /* Utility function for record_value_for_reg. Count number of
12097 rtxs in X. */
12098 static int
12099 count_rtxs (rtx x)
12101 enum rtx_code code = GET_CODE (x);
12102 const char *fmt;
12103 int i, j, ret = 1;
12105 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12106 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12108 rtx x0 = XEXP (x, 0);
12109 rtx x1 = XEXP (x, 1);
12111 if (x0 == x1)
12112 return 1 + 2 * count_rtxs (x0);
12114 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12115 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12116 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12117 return 2 + 2 * count_rtxs (x0)
12118 + count_rtxs (x == XEXP (x1, 0)
12119 ? XEXP (x1, 1) : XEXP (x1, 0));
12121 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12122 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12123 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12124 return 2 + 2 * count_rtxs (x1)
12125 + count_rtxs (x == XEXP (x0, 0)
12126 ? XEXP (x0, 1) : XEXP (x0, 0));
12129 fmt = GET_RTX_FORMAT (code);
12130 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12131 if (fmt[i] == 'e')
12132 ret += count_rtxs (XEXP (x, i));
12133 else if (fmt[i] == 'E')
12134 for (j = 0; j < XVECLEN (x, i); j++)
12135 ret += count_rtxs (XVECEXP (x, i, j));
12137 return ret;
12140 /* Utility function for following routine. Called when X is part of a value
12141 being stored into last_set_value. Sets last_set_table_tick
12142 for each register mentioned. Similar to mention_regs in cse.c */
12144 static void
12145 update_table_tick (rtx x)
12147 enum rtx_code code = GET_CODE (x);
12148 const char *fmt = GET_RTX_FORMAT (code);
12149 int i, j;
12151 if (code == REG)
12153 unsigned int regno = REGNO (x);
12154 unsigned int endregno = END_REGNO (x);
12155 unsigned int r;
12157 for (r = regno; r < endregno; r++)
12159 reg_stat_type *rsp = &reg_stat[r];
12160 rsp->last_set_table_tick = label_tick;
12163 return;
12166 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12167 if (fmt[i] == 'e')
12169 /* Check for identical subexpressions. If x contains
12170 identical subexpression we only have to traverse one of
12171 them. */
12172 if (i == 0 && ARITHMETIC_P (x))
12174 /* Note that at this point x1 has already been
12175 processed. */
12176 rtx x0 = XEXP (x, 0);
12177 rtx x1 = XEXP (x, 1);
12179 /* If x0 and x1 are identical then there is no need to
12180 process x0. */
12181 if (x0 == x1)
12182 break;
12184 /* If x0 is identical to a subexpression of x1 then while
12185 processing x1, x0 has already been processed. Thus we
12186 are done with x. */
12187 if (ARITHMETIC_P (x1)
12188 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12189 break;
12191 /* If x1 is identical to a subexpression of x0 then we
12192 still have to process the rest of x0. */
12193 if (ARITHMETIC_P (x0)
12194 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12196 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12197 break;
12201 update_table_tick (XEXP (x, i));
12203 else if (fmt[i] == 'E')
12204 for (j = 0; j < XVECLEN (x, i); j++)
12205 update_table_tick (XVECEXP (x, i, j));
12208 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12209 are saying that the register is clobbered and we no longer know its
12210 value. If INSN is zero, don't update reg_stat[].last_set; this is
12211 only permitted with VALUE also zero and is used to invalidate the
12212 register. */
12214 static void
12215 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12217 unsigned int regno = REGNO (reg);
12218 unsigned int endregno = END_REGNO (reg);
12219 unsigned int i;
12220 reg_stat_type *rsp;
12222 /* If VALUE contains REG and we have a previous value for REG, substitute
12223 the previous value. */
12224 if (value && insn && reg_overlap_mentioned_p (reg, value))
12226 rtx tem;
12228 /* Set things up so get_last_value is allowed to see anything set up to
12229 our insn. */
12230 subst_low_luid = DF_INSN_LUID (insn);
12231 tem = get_last_value (reg);
12233 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12234 it isn't going to be useful and will take a lot of time to process,
12235 so just use the CLOBBER. */
12237 if (tem)
12239 if (ARITHMETIC_P (tem)
12240 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12241 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12242 tem = XEXP (tem, 0);
12243 else if (count_occurrences (value, reg, 1) >= 2)
12245 /* If there are two or more occurrences of REG in VALUE,
12246 prevent the value from growing too much. */
12247 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12248 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12251 value = replace_rtx (copy_rtx (value), reg, tem);
12255 /* For each register modified, show we don't know its value, that
12256 we don't know about its bitwise content, that its value has been
12257 updated, and that we don't know the location of the death of the
12258 register. */
12259 for (i = regno; i < endregno; i++)
12261 rsp = &reg_stat[i];
12263 if (insn)
12264 rsp->last_set = insn;
12266 rsp->last_set_value = 0;
12267 rsp->last_set_mode = VOIDmode;
12268 rsp->last_set_nonzero_bits = 0;
12269 rsp->last_set_sign_bit_copies = 0;
12270 rsp->last_death = 0;
12271 rsp->truncated_to_mode = VOIDmode;
12274 /* Mark registers that are being referenced in this value. */
12275 if (value)
12276 update_table_tick (value);
12278 /* Now update the status of each register being set.
12279 If someone is using this register in this block, set this register
12280 to invalid since we will get confused between the two lives in this
12281 basic block. This makes using this register always invalid. In cse, we
12282 scan the table to invalidate all entries using this register, but this
12283 is too much work for us. */
12285 for (i = regno; i < endregno; i++)
12287 rsp = &reg_stat[i];
12288 rsp->last_set_label = label_tick;
12289 if (!insn
12290 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12291 rsp->last_set_invalid = 1;
12292 else
12293 rsp->last_set_invalid = 0;
12296 /* The value being assigned might refer to X (like in "x++;"). In that
12297 case, we must replace it with (clobber (const_int 0)) to prevent
12298 infinite loops. */
12299 rsp = &reg_stat[regno];
12300 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12302 value = copy_rtx (value);
12303 if (!get_last_value_validate (&value, insn, label_tick, 1))
12304 value = 0;
12307 /* For the main register being modified, update the value, the mode, the
12308 nonzero bits, and the number of sign bit copies. */
12310 rsp->last_set_value = value;
12312 if (value)
12314 enum machine_mode mode = GET_MODE (reg);
12315 subst_low_luid = DF_INSN_LUID (insn);
12316 rsp->last_set_mode = mode;
12317 if (GET_MODE_CLASS (mode) == MODE_INT
12318 && HWI_COMPUTABLE_MODE_P (mode))
12319 mode = nonzero_bits_mode;
12320 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12321 rsp->last_set_sign_bit_copies
12322 = num_sign_bit_copies (value, GET_MODE (reg));
12326 /* Called via note_stores from record_dead_and_set_regs to handle one
12327 SET or CLOBBER in an insn. DATA is the instruction in which the
12328 set is occurring. */
12330 static void
12331 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12333 rtx_insn *record_dead_insn = (rtx_insn *) data;
12335 if (GET_CODE (dest) == SUBREG)
12336 dest = SUBREG_REG (dest);
12338 if (!record_dead_insn)
12340 if (REG_P (dest))
12341 record_value_for_reg (dest, NULL, NULL_RTX);
12342 return;
12345 if (REG_P (dest))
12347 /* If we are setting the whole register, we know its value. Otherwise
12348 show that we don't know the value. We can handle SUBREG in
12349 some cases. */
12350 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12351 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12352 else if (GET_CODE (setter) == SET
12353 && GET_CODE (SET_DEST (setter)) == SUBREG
12354 && SUBREG_REG (SET_DEST (setter)) == dest
12355 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12356 && subreg_lowpart_p (SET_DEST (setter)))
12357 record_value_for_reg (dest, record_dead_insn,
12358 gen_lowpart (GET_MODE (dest),
12359 SET_SRC (setter)));
12360 else
12361 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12363 else if (MEM_P (dest)
12364 /* Ignore pushes, they clobber nothing. */
12365 && ! push_operand (dest, GET_MODE (dest)))
12366 mem_last_set = DF_INSN_LUID (record_dead_insn);
12369 /* Update the records of when each REG was most recently set or killed
12370 for the things done by INSN. This is the last thing done in processing
12371 INSN in the combiner loop.
12373 We update reg_stat[], in particular fields last_set, last_set_value,
12374 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12375 last_death, and also the similar information mem_last_set (which insn
12376 most recently modified memory) and last_call_luid (which insn was the
12377 most recent subroutine call). */
12379 static void
12380 record_dead_and_set_regs (rtx_insn *insn)
12382 rtx link;
12383 unsigned int i;
12385 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12387 if (REG_NOTE_KIND (link) == REG_DEAD
12388 && REG_P (XEXP (link, 0)))
12390 unsigned int regno = REGNO (XEXP (link, 0));
12391 unsigned int endregno = END_REGNO (XEXP (link, 0));
12393 for (i = regno; i < endregno; i++)
12395 reg_stat_type *rsp;
12397 rsp = &reg_stat[i];
12398 rsp->last_death = insn;
12401 else if (REG_NOTE_KIND (link) == REG_INC)
12402 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12405 if (CALL_P (insn))
12407 hard_reg_set_iterator hrsi;
12408 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12410 reg_stat_type *rsp;
12412 rsp = &reg_stat[i];
12413 rsp->last_set_invalid = 1;
12414 rsp->last_set = insn;
12415 rsp->last_set_value = 0;
12416 rsp->last_set_mode = VOIDmode;
12417 rsp->last_set_nonzero_bits = 0;
12418 rsp->last_set_sign_bit_copies = 0;
12419 rsp->last_death = 0;
12420 rsp->truncated_to_mode = VOIDmode;
12423 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12425 /* We can't combine into a call pattern. Remember, though, that
12426 the return value register is set at this LUID. We could
12427 still replace a register with the return value from the
12428 wrong subroutine call! */
12429 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12431 else
12432 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12435 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12436 register present in the SUBREG, so for each such SUBREG go back and
12437 adjust nonzero and sign bit information of the registers that are
12438 known to have some zero/sign bits set.
12440 This is needed because when combine blows the SUBREGs away, the
12441 information on zero/sign bits is lost and further combines can be
12442 missed because of that. */
12444 static void
12445 record_promoted_value (rtx_insn *insn, rtx subreg)
12447 struct insn_link *links;
12448 rtx set;
12449 unsigned int regno = REGNO (SUBREG_REG (subreg));
12450 enum machine_mode mode = GET_MODE (subreg);
12452 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12453 return;
12455 for (links = LOG_LINKS (insn); links;)
12457 reg_stat_type *rsp;
12459 insn = links->insn;
12460 set = single_set (insn);
12462 if (! set || !REG_P (SET_DEST (set))
12463 || REGNO (SET_DEST (set)) != regno
12464 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12466 links = links->next;
12467 continue;
12470 rsp = &reg_stat[regno];
12471 if (rsp->last_set == insn)
12473 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12474 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12477 if (REG_P (SET_SRC (set)))
12479 regno = REGNO (SET_SRC (set));
12480 links = LOG_LINKS (insn);
12482 else
12483 break;
12487 /* Check if X, a register, is known to contain a value already
12488 truncated to MODE. In this case we can use a subreg to refer to
12489 the truncated value even though in the generic case we would need
12490 an explicit truncation. */
12492 static bool
12493 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12495 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12496 enum machine_mode truncated = rsp->truncated_to_mode;
12498 if (truncated == 0
12499 || rsp->truncation_label < label_tick_ebb_start)
12500 return false;
12501 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12502 return true;
12503 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12504 return true;
12505 return false;
12508 /* If X is a hard reg or a subreg record the mode that the register is
12509 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12510 to turn a truncate into a subreg using this information. Return true
12511 if traversing X is complete. */
12513 static bool
12514 record_truncated_value (rtx x)
12516 enum machine_mode truncated_mode;
12517 reg_stat_type *rsp;
12519 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12521 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12522 truncated_mode = GET_MODE (x);
12524 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12525 return true;
12527 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12528 return true;
12530 x = SUBREG_REG (x);
12532 /* ??? For hard-regs we now record everything. We might be able to
12533 optimize this using last_set_mode. */
12534 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12535 truncated_mode = GET_MODE (x);
12536 else
12537 return false;
12539 rsp = &reg_stat[REGNO (x)];
12540 if (rsp->truncated_to_mode == 0
12541 || rsp->truncation_label < label_tick_ebb_start
12542 || (GET_MODE_SIZE (truncated_mode)
12543 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12545 rsp->truncated_to_mode = truncated_mode;
12546 rsp->truncation_label = label_tick;
12549 return true;
12552 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12553 the modes they are used in. This can help truning TRUNCATEs into
12554 SUBREGs. */
12556 static void
12557 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12559 subrtx_var_iterator::array_type array;
12560 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12561 if (record_truncated_value (*iter))
12562 iter.skip_subrtxes ();
12565 /* Scan X for promoted SUBREGs. For each one found,
12566 note what it implies to the registers used in it. */
12568 static void
12569 check_promoted_subreg (rtx_insn *insn, rtx x)
12571 if (GET_CODE (x) == SUBREG
12572 && SUBREG_PROMOTED_VAR_P (x)
12573 && REG_P (SUBREG_REG (x)))
12574 record_promoted_value (insn, x);
12575 else
12577 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12578 int i, j;
12580 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12581 switch (format[i])
12583 case 'e':
12584 check_promoted_subreg (insn, XEXP (x, i));
12585 break;
12586 case 'V':
12587 case 'E':
12588 if (XVEC (x, i) != 0)
12589 for (j = 0; j < XVECLEN (x, i); j++)
12590 check_promoted_subreg (insn, XVECEXP (x, i, j));
12591 break;
12596 /* Verify that all the registers and memory references mentioned in *LOC are
12597 still valid. *LOC was part of a value set in INSN when label_tick was
12598 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12599 the invalid references with (clobber (const_int 0)) and return 1. This
12600 replacement is useful because we often can get useful information about
12601 the form of a value (e.g., if it was produced by a shift that always
12602 produces -1 or 0) even though we don't know exactly what registers it
12603 was produced from. */
12605 static int
12606 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12608 rtx x = *loc;
12609 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12610 int len = GET_RTX_LENGTH (GET_CODE (x));
12611 int i, j;
12613 if (REG_P (x))
12615 unsigned int regno = REGNO (x);
12616 unsigned int endregno = END_REGNO (x);
12617 unsigned int j;
12619 for (j = regno; j < endregno; j++)
12621 reg_stat_type *rsp = &reg_stat[j];
12622 if (rsp->last_set_invalid
12623 /* If this is a pseudo-register that was only set once and not
12624 live at the beginning of the function, it is always valid. */
12625 || (! (regno >= FIRST_PSEUDO_REGISTER
12626 && REG_N_SETS (regno) == 1
12627 && (!REGNO_REG_SET_P
12628 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12629 regno)))
12630 && rsp->last_set_label > tick))
12632 if (replace)
12633 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12634 return replace;
12638 return 1;
12640 /* If this is a memory reference, make sure that there were no stores after
12641 it that might have clobbered the value. We don't have alias info, so we
12642 assume any store invalidates it. Moreover, we only have local UIDs, so
12643 we also assume that there were stores in the intervening basic blocks. */
12644 else if (MEM_P (x) && !MEM_READONLY_P (x)
12645 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12647 if (replace)
12648 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12649 return replace;
12652 for (i = 0; i < len; i++)
12654 if (fmt[i] == 'e')
12656 /* Check for identical subexpressions. If x contains
12657 identical subexpression we only have to traverse one of
12658 them. */
12659 if (i == 1 && ARITHMETIC_P (x))
12661 /* Note that at this point x0 has already been checked
12662 and found valid. */
12663 rtx x0 = XEXP (x, 0);
12664 rtx x1 = XEXP (x, 1);
12666 /* If x0 and x1 are identical then x is also valid. */
12667 if (x0 == x1)
12668 return 1;
12670 /* If x1 is identical to a subexpression of x0 then
12671 while checking x0, x1 has already been checked. Thus
12672 it is valid and so as x. */
12673 if (ARITHMETIC_P (x0)
12674 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12675 return 1;
12677 /* If x0 is identical to a subexpression of x1 then x is
12678 valid iff the rest of x1 is valid. */
12679 if (ARITHMETIC_P (x1)
12680 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12681 return
12682 get_last_value_validate (&XEXP (x1,
12683 x0 == XEXP (x1, 0) ? 1 : 0),
12684 insn, tick, replace);
12687 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12688 replace) == 0)
12689 return 0;
12691 else if (fmt[i] == 'E')
12692 for (j = 0; j < XVECLEN (x, i); j++)
12693 if (get_last_value_validate (&XVECEXP (x, i, j),
12694 insn, tick, replace) == 0)
12695 return 0;
12698 /* If we haven't found a reason for it to be invalid, it is valid. */
12699 return 1;
12702 /* Get the last value assigned to X, if known. Some registers
12703 in the value may be replaced with (clobber (const_int 0)) if their value
12704 is known longer known reliably. */
12706 static rtx
12707 get_last_value (const_rtx x)
12709 unsigned int regno;
12710 rtx value;
12711 reg_stat_type *rsp;
12713 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12714 then convert it to the desired mode. If this is a paradoxical SUBREG,
12715 we cannot predict what values the "extra" bits might have. */
12716 if (GET_CODE (x) == SUBREG
12717 && subreg_lowpart_p (x)
12718 && !paradoxical_subreg_p (x)
12719 && (value = get_last_value (SUBREG_REG (x))) != 0)
12720 return gen_lowpart (GET_MODE (x), value);
12722 if (!REG_P (x))
12723 return 0;
12725 regno = REGNO (x);
12726 rsp = &reg_stat[regno];
12727 value = rsp->last_set_value;
12729 /* If we don't have a value, or if it isn't for this basic block and
12730 it's either a hard register, set more than once, or it's a live
12731 at the beginning of the function, return 0.
12733 Because if it's not live at the beginning of the function then the reg
12734 is always set before being used (is never used without being set).
12735 And, if it's set only once, and it's always set before use, then all
12736 uses must have the same last value, even if it's not from this basic
12737 block. */
12739 if (value == 0
12740 || (rsp->last_set_label < label_tick_ebb_start
12741 && (regno < FIRST_PSEUDO_REGISTER
12742 || REG_N_SETS (regno) != 1
12743 || REGNO_REG_SET_P
12744 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12745 return 0;
12747 /* If the value was set in a later insn than the ones we are processing,
12748 we can't use it even if the register was only set once. */
12749 if (rsp->last_set_label == label_tick
12750 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12751 return 0;
12753 /* If the value has all its registers valid, return it. */
12754 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12755 return value;
12757 /* Otherwise, make a copy and replace any invalid register with
12758 (clobber (const_int 0)). If that fails for some reason, return 0. */
12760 value = copy_rtx (value);
12761 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12762 return value;
12764 return 0;
12767 /* Return nonzero if expression X refers to a REG or to memory
12768 that is set in an instruction more recent than FROM_LUID. */
12770 static int
12771 use_crosses_set_p (const_rtx x, int from_luid)
12773 const char *fmt;
12774 int i;
12775 enum rtx_code code = GET_CODE (x);
12777 if (code == REG)
12779 unsigned int regno = REGNO (x);
12780 unsigned endreg = END_REGNO (x);
12782 #ifdef PUSH_ROUNDING
12783 /* Don't allow uses of the stack pointer to be moved,
12784 because we don't know whether the move crosses a push insn. */
12785 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12786 return 1;
12787 #endif
12788 for (; regno < endreg; regno++)
12790 reg_stat_type *rsp = &reg_stat[regno];
12791 if (rsp->last_set
12792 && rsp->last_set_label == label_tick
12793 && DF_INSN_LUID (rsp->last_set) > from_luid)
12794 return 1;
12796 return 0;
12799 if (code == MEM && mem_last_set > from_luid)
12800 return 1;
12802 fmt = GET_RTX_FORMAT (code);
12804 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12806 if (fmt[i] == 'E')
12808 int j;
12809 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12810 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12811 return 1;
12813 else if (fmt[i] == 'e'
12814 && use_crosses_set_p (XEXP (x, i), from_luid))
12815 return 1;
12817 return 0;
12820 /* Define three variables used for communication between the following
12821 routines. */
12823 static unsigned int reg_dead_regno, reg_dead_endregno;
12824 static int reg_dead_flag;
12826 /* Function called via note_stores from reg_dead_at_p.
12828 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12829 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12831 static void
12832 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12834 unsigned int regno, endregno;
12836 if (!REG_P (dest))
12837 return;
12839 regno = REGNO (dest);
12840 endregno = END_REGNO (dest);
12841 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12842 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12845 /* Return nonzero if REG is known to be dead at INSN.
12847 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12848 referencing REG, it is dead. If we hit a SET referencing REG, it is
12849 live. Otherwise, see if it is live or dead at the start of the basic
12850 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12851 must be assumed to be always live. */
12853 static int
12854 reg_dead_at_p (rtx reg, rtx_insn *insn)
12856 basic_block block;
12857 unsigned int i;
12859 /* Set variables for reg_dead_at_p_1. */
12860 reg_dead_regno = REGNO (reg);
12861 reg_dead_endregno = END_REGNO (reg);
12863 reg_dead_flag = 0;
12865 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12866 we allow the machine description to decide whether use-and-clobber
12867 patterns are OK. */
12868 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12870 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12871 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12872 return 0;
12875 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12876 beginning of basic block. */
12877 block = BLOCK_FOR_INSN (insn);
12878 for (;;)
12880 if (INSN_P (insn))
12882 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12883 if (reg_dead_flag)
12884 return reg_dead_flag == 1 ? 1 : 0;
12886 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12887 return 1;
12890 if (insn == BB_HEAD (block))
12891 break;
12893 insn = PREV_INSN (insn);
12896 /* Look at live-in sets for the basic block that we were in. */
12897 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12898 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12899 return 0;
12901 return 1;
12904 /* Note hard registers in X that are used. */
12906 static void
12907 mark_used_regs_combine (rtx x)
12909 RTX_CODE code = GET_CODE (x);
12910 unsigned int regno;
12911 int i;
12913 switch (code)
12915 case LABEL_REF:
12916 case SYMBOL_REF:
12917 case CONST:
12918 CASE_CONST_ANY:
12919 case PC:
12920 case ADDR_VEC:
12921 case ADDR_DIFF_VEC:
12922 case ASM_INPUT:
12923 #ifdef HAVE_cc0
12924 /* CC0 must die in the insn after it is set, so we don't need to take
12925 special note of it here. */
12926 case CC0:
12927 #endif
12928 return;
12930 case CLOBBER:
12931 /* If we are clobbering a MEM, mark any hard registers inside the
12932 address as used. */
12933 if (MEM_P (XEXP (x, 0)))
12934 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12935 return;
12937 case REG:
12938 regno = REGNO (x);
12939 /* A hard reg in a wide mode may really be multiple registers.
12940 If so, mark all of them just like the first. */
12941 if (regno < FIRST_PSEUDO_REGISTER)
12943 /* None of this applies to the stack, frame or arg pointers. */
12944 if (regno == STACK_POINTER_REGNUM
12945 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12946 || regno == HARD_FRAME_POINTER_REGNUM
12947 #endif
12948 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12949 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12950 #endif
12951 || regno == FRAME_POINTER_REGNUM)
12952 return;
12954 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12956 return;
12958 case SET:
12960 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12961 the address. */
12962 rtx testreg = SET_DEST (x);
12964 while (GET_CODE (testreg) == SUBREG
12965 || GET_CODE (testreg) == ZERO_EXTRACT
12966 || GET_CODE (testreg) == STRICT_LOW_PART)
12967 testreg = XEXP (testreg, 0);
12969 if (MEM_P (testreg))
12970 mark_used_regs_combine (XEXP (testreg, 0));
12972 mark_used_regs_combine (SET_SRC (x));
12974 return;
12976 default:
12977 break;
12980 /* Recursively scan the operands of this expression. */
12983 const char *fmt = GET_RTX_FORMAT (code);
12985 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12987 if (fmt[i] == 'e')
12988 mark_used_regs_combine (XEXP (x, i));
12989 else if (fmt[i] == 'E')
12991 int j;
12993 for (j = 0; j < XVECLEN (x, i); j++)
12994 mark_used_regs_combine (XVECEXP (x, i, j));
13000 /* Remove register number REGNO from the dead registers list of INSN.
13002 Return the note used to record the death, if there was one. */
13005 remove_death (unsigned int regno, rtx_insn *insn)
13007 rtx note = find_regno_note (insn, REG_DEAD, regno);
13009 if (note)
13010 remove_note (insn, note);
13012 return note;
13015 /* For each register (hardware or pseudo) used within expression X, if its
13016 death is in an instruction with luid between FROM_LUID (inclusive) and
13017 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13018 list headed by PNOTES.
13020 That said, don't move registers killed by maybe_kill_insn.
13022 This is done when X is being merged by combination into TO_INSN. These
13023 notes will then be distributed as needed. */
13025 static void
13026 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13027 rtx *pnotes)
13029 const char *fmt;
13030 int len, i;
13031 enum rtx_code code = GET_CODE (x);
13033 if (code == REG)
13035 unsigned int regno = REGNO (x);
13036 rtx_insn *where_dead = reg_stat[regno].last_death;
13038 /* Don't move the register if it gets killed in between from and to. */
13039 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13040 && ! reg_referenced_p (x, maybe_kill_insn))
13041 return;
13043 if (where_dead
13044 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13045 && DF_INSN_LUID (where_dead) >= from_luid
13046 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13048 rtx note = remove_death (regno, where_dead);
13050 /* It is possible for the call above to return 0. This can occur
13051 when last_death points to I2 or I1 that we combined with.
13052 In that case make a new note.
13054 We must also check for the case where X is a hard register
13055 and NOTE is a death note for a range of hard registers
13056 including X. In that case, we must put REG_DEAD notes for
13057 the remaining registers in place of NOTE. */
13059 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13060 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13061 > GET_MODE_SIZE (GET_MODE (x))))
13063 unsigned int deadregno = REGNO (XEXP (note, 0));
13064 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13065 unsigned int ourend = END_HARD_REGNO (x);
13066 unsigned int i;
13068 for (i = deadregno; i < deadend; i++)
13069 if (i < regno || i >= ourend)
13070 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13073 /* If we didn't find any note, or if we found a REG_DEAD note that
13074 covers only part of the given reg, and we have a multi-reg hard
13075 register, then to be safe we must check for REG_DEAD notes
13076 for each register other than the first. They could have
13077 their own REG_DEAD notes lying around. */
13078 else if ((note == 0
13079 || (note != 0
13080 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13081 < GET_MODE_SIZE (GET_MODE (x)))))
13082 && regno < FIRST_PSEUDO_REGISTER
13083 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13085 unsigned int ourend = END_HARD_REGNO (x);
13086 unsigned int i, offset;
13087 rtx oldnotes = 0;
13089 if (note)
13090 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13091 else
13092 offset = 1;
13094 for (i = regno + offset; i < ourend; i++)
13095 move_deaths (regno_reg_rtx[i],
13096 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13099 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13101 XEXP (note, 1) = *pnotes;
13102 *pnotes = note;
13104 else
13105 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13108 return;
13111 else if (GET_CODE (x) == SET)
13113 rtx dest = SET_DEST (x);
13115 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13117 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13118 that accesses one word of a multi-word item, some
13119 piece of everything register in the expression is used by
13120 this insn, so remove any old death. */
13121 /* ??? So why do we test for equality of the sizes? */
13123 if (GET_CODE (dest) == ZERO_EXTRACT
13124 || GET_CODE (dest) == STRICT_LOW_PART
13125 || (GET_CODE (dest) == SUBREG
13126 && (((GET_MODE_SIZE (GET_MODE (dest))
13127 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13128 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13129 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13131 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13132 return;
13135 /* If this is some other SUBREG, we know it replaces the entire
13136 value, so use that as the destination. */
13137 if (GET_CODE (dest) == SUBREG)
13138 dest = SUBREG_REG (dest);
13140 /* If this is a MEM, adjust deaths of anything used in the address.
13141 For a REG (the only other possibility), the entire value is
13142 being replaced so the old value is not used in this insn. */
13144 if (MEM_P (dest))
13145 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13146 to_insn, pnotes);
13147 return;
13150 else if (GET_CODE (x) == CLOBBER)
13151 return;
13153 len = GET_RTX_LENGTH (code);
13154 fmt = GET_RTX_FORMAT (code);
13156 for (i = 0; i < len; i++)
13158 if (fmt[i] == 'E')
13160 int j;
13161 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13162 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13163 to_insn, pnotes);
13165 else if (fmt[i] == 'e')
13166 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13170 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13171 pattern of an insn. X must be a REG. */
13173 static int
13174 reg_bitfield_target_p (rtx x, rtx body)
13176 int i;
13178 if (GET_CODE (body) == SET)
13180 rtx dest = SET_DEST (body);
13181 rtx target;
13182 unsigned int regno, tregno, endregno, endtregno;
13184 if (GET_CODE (dest) == ZERO_EXTRACT)
13185 target = XEXP (dest, 0);
13186 else if (GET_CODE (dest) == STRICT_LOW_PART)
13187 target = SUBREG_REG (XEXP (dest, 0));
13188 else
13189 return 0;
13191 if (GET_CODE (target) == SUBREG)
13192 target = SUBREG_REG (target);
13194 if (!REG_P (target))
13195 return 0;
13197 tregno = REGNO (target), regno = REGNO (x);
13198 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13199 return target == x;
13201 endtregno = end_hard_regno (GET_MODE (target), tregno);
13202 endregno = end_hard_regno (GET_MODE (x), regno);
13204 return endregno > tregno && regno < endtregno;
13207 else if (GET_CODE (body) == PARALLEL)
13208 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13209 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13210 return 1;
13212 return 0;
13215 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13216 as appropriate. I3 and I2 are the insns resulting from the combination
13217 insns including FROM (I2 may be zero).
13219 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13220 not need REG_DEAD notes because they are being substituted for. This
13221 saves searching in the most common cases.
13223 Each note in the list is either ignored or placed on some insns, depending
13224 on the type of note. */
13226 static void
13227 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13228 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13230 rtx note, next_note;
13231 rtx tem_note;
13232 rtx_insn *tem_insn;
13234 for (note = notes; note; note = next_note)
13236 rtx_insn *place = 0, *place2 = 0;
13238 next_note = XEXP (note, 1);
13239 switch (REG_NOTE_KIND (note))
13241 case REG_BR_PROB:
13242 case REG_BR_PRED:
13243 /* Doesn't matter much where we put this, as long as it's somewhere.
13244 It is preferable to keep these notes on branches, which is most
13245 likely to be i3. */
13246 place = i3;
13247 break;
13249 case REG_NON_LOCAL_GOTO:
13250 if (JUMP_P (i3))
13251 place = i3;
13252 else
13254 gcc_assert (i2 && JUMP_P (i2));
13255 place = i2;
13257 break;
13259 case REG_EH_REGION:
13260 /* These notes must remain with the call or trapping instruction. */
13261 if (CALL_P (i3))
13262 place = i3;
13263 else if (i2 && CALL_P (i2))
13264 place = i2;
13265 else
13267 gcc_assert (cfun->can_throw_non_call_exceptions);
13268 if (may_trap_p (i3))
13269 place = i3;
13270 else if (i2 && may_trap_p (i2))
13271 place = i2;
13272 /* ??? Otherwise assume we've combined things such that we
13273 can now prove that the instructions can't trap. Drop the
13274 note in this case. */
13276 break;
13278 case REG_ARGS_SIZE:
13279 /* ??? How to distribute between i3-i1. Assume i3 contains the
13280 entire adjustment. Assert i3 contains at least some adjust. */
13281 if (!noop_move_p (i3))
13283 int old_size, args_size = INTVAL (XEXP (note, 0));
13284 /* fixup_args_size_notes looks at REG_NORETURN note,
13285 so ensure the note is placed there first. */
13286 if (CALL_P (i3))
13288 rtx *np;
13289 for (np = &next_note; *np; np = &XEXP (*np, 1))
13290 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13292 rtx n = *np;
13293 *np = XEXP (n, 1);
13294 XEXP (n, 1) = REG_NOTES (i3);
13295 REG_NOTES (i3) = n;
13296 break;
13299 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13300 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13301 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13302 gcc_assert (old_size != args_size
13303 || (CALL_P (i3)
13304 && !ACCUMULATE_OUTGOING_ARGS
13305 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13307 break;
13309 case REG_NORETURN:
13310 case REG_SETJMP:
13311 case REG_TM:
13312 case REG_CALL_DECL:
13313 /* These notes must remain with the call. It should not be
13314 possible for both I2 and I3 to be a call. */
13315 if (CALL_P (i3))
13316 place = i3;
13317 else
13319 gcc_assert (i2 && CALL_P (i2));
13320 place = i2;
13322 break;
13324 case REG_UNUSED:
13325 /* Any clobbers for i3 may still exist, and so we must process
13326 REG_UNUSED notes from that insn.
13328 Any clobbers from i2 or i1 can only exist if they were added by
13329 recog_for_combine. In that case, recog_for_combine created the
13330 necessary REG_UNUSED notes. Trying to keep any original
13331 REG_UNUSED notes from these insns can cause incorrect output
13332 if it is for the same register as the original i3 dest.
13333 In that case, we will notice that the register is set in i3,
13334 and then add a REG_UNUSED note for the destination of i3, which
13335 is wrong. However, it is possible to have REG_UNUSED notes from
13336 i2 or i1 for register which were both used and clobbered, so
13337 we keep notes from i2 or i1 if they will turn into REG_DEAD
13338 notes. */
13340 /* If this register is set or clobbered in I3, put the note there
13341 unless there is one already. */
13342 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13344 if (from_insn != i3)
13345 break;
13347 if (! (REG_P (XEXP (note, 0))
13348 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13349 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13350 place = i3;
13352 /* Otherwise, if this register is used by I3, then this register
13353 now dies here, so we must put a REG_DEAD note here unless there
13354 is one already. */
13355 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13356 && ! (REG_P (XEXP (note, 0))
13357 ? find_regno_note (i3, REG_DEAD,
13358 REGNO (XEXP (note, 0)))
13359 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13361 PUT_REG_NOTE_KIND (note, REG_DEAD);
13362 place = i3;
13364 break;
13366 case REG_EQUAL:
13367 case REG_EQUIV:
13368 case REG_NOALIAS:
13369 /* These notes say something about results of an insn. We can
13370 only support them if they used to be on I3 in which case they
13371 remain on I3. Otherwise they are ignored.
13373 If the note refers to an expression that is not a constant, we
13374 must also ignore the note since we cannot tell whether the
13375 equivalence is still true. It might be possible to do
13376 slightly better than this (we only have a problem if I2DEST
13377 or I1DEST is present in the expression), but it doesn't
13378 seem worth the trouble. */
13380 if (from_insn == i3
13381 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13382 place = i3;
13383 break;
13385 case REG_INC:
13386 /* These notes say something about how a register is used. They must
13387 be present on any use of the register in I2 or I3. */
13388 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13389 place = i3;
13391 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13393 if (place)
13394 place2 = i2;
13395 else
13396 place = i2;
13398 break;
13400 case REG_LABEL_TARGET:
13401 case REG_LABEL_OPERAND:
13402 /* This can show up in several ways -- either directly in the
13403 pattern, or hidden off in the constant pool with (or without?)
13404 a REG_EQUAL note. */
13405 /* ??? Ignore the without-reg_equal-note problem for now. */
13406 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13407 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13408 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13409 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13410 place = i3;
13412 if (i2
13413 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13414 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13415 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13416 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13418 if (place)
13419 place2 = i2;
13420 else
13421 place = i2;
13424 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13425 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13426 there. */
13427 if (place && JUMP_P (place)
13428 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13429 && (JUMP_LABEL (place) == NULL
13430 || JUMP_LABEL (place) == XEXP (note, 0)))
13432 rtx label = JUMP_LABEL (place);
13434 if (!label)
13435 JUMP_LABEL (place) = XEXP (note, 0);
13436 else if (LABEL_P (label))
13437 LABEL_NUSES (label)--;
13440 if (place2 && JUMP_P (place2)
13441 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13442 && (JUMP_LABEL (place2) == NULL
13443 || JUMP_LABEL (place2) == XEXP (note, 0)))
13445 rtx label = JUMP_LABEL (place2);
13447 if (!label)
13448 JUMP_LABEL (place2) = XEXP (note, 0);
13449 else if (LABEL_P (label))
13450 LABEL_NUSES (label)--;
13451 place2 = 0;
13453 break;
13455 case REG_NONNEG:
13456 /* This note says something about the value of a register prior
13457 to the execution of an insn. It is too much trouble to see
13458 if the note is still correct in all situations. It is better
13459 to simply delete it. */
13460 break;
13462 case REG_DEAD:
13463 /* If we replaced the right hand side of FROM_INSN with a
13464 REG_EQUAL note, the original use of the dying register
13465 will not have been combined into I3 and I2. In such cases,
13466 FROM_INSN is guaranteed to be the first of the combined
13467 instructions, so we simply need to search back before
13468 FROM_INSN for the previous use or set of this register,
13469 then alter the notes there appropriately.
13471 If the register is used as an input in I3, it dies there.
13472 Similarly for I2, if it is nonzero and adjacent to I3.
13474 If the register is not used as an input in either I3 or I2
13475 and it is not one of the registers we were supposed to eliminate,
13476 there are two possibilities. We might have a non-adjacent I2
13477 or we might have somehow eliminated an additional register
13478 from a computation. For example, we might have had A & B where
13479 we discover that B will always be zero. In this case we will
13480 eliminate the reference to A.
13482 In both cases, we must search to see if we can find a previous
13483 use of A and put the death note there. */
13485 if (from_insn
13486 && from_insn == i2mod
13487 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13488 tem_insn = from_insn;
13489 else
13491 if (from_insn
13492 && CALL_P (from_insn)
13493 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13494 place = from_insn;
13495 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13496 place = i3;
13497 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13498 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13499 place = i2;
13500 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13501 && !(i2mod
13502 && reg_overlap_mentioned_p (XEXP (note, 0),
13503 i2mod_old_rhs)))
13504 || rtx_equal_p (XEXP (note, 0), elim_i1)
13505 || rtx_equal_p (XEXP (note, 0), elim_i0))
13506 break;
13507 tem_insn = i3;
13510 if (place == 0)
13512 basic_block bb = this_basic_block;
13514 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13516 if (!NONDEBUG_INSN_P (tem_insn))
13518 if (tem_insn == BB_HEAD (bb))
13519 break;
13520 continue;
13523 /* If the register is being set at TEM_INSN, see if that is all
13524 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13525 into a REG_UNUSED note instead. Don't delete sets to
13526 global register vars. */
13527 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13528 || !global_regs[REGNO (XEXP (note, 0))])
13529 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13531 rtx set = single_set (tem_insn);
13532 rtx inner_dest = 0;
13533 #ifdef HAVE_cc0
13534 rtx_insn *cc0_setter = NULL;
13535 #endif
13537 if (set != 0)
13538 for (inner_dest = SET_DEST (set);
13539 (GET_CODE (inner_dest) == STRICT_LOW_PART
13540 || GET_CODE (inner_dest) == SUBREG
13541 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13542 inner_dest = XEXP (inner_dest, 0))
13545 /* Verify that it was the set, and not a clobber that
13546 modified the register.
13548 CC0 targets must be careful to maintain setter/user
13549 pairs. If we cannot delete the setter due to side
13550 effects, mark the user with an UNUSED note instead
13551 of deleting it. */
13553 if (set != 0 && ! side_effects_p (SET_SRC (set))
13554 && rtx_equal_p (XEXP (note, 0), inner_dest)
13555 #ifdef HAVE_cc0
13556 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13557 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13558 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13559 #endif
13562 /* Move the notes and links of TEM_INSN elsewhere.
13563 This might delete other dead insns recursively.
13564 First set the pattern to something that won't use
13565 any register. */
13566 rtx old_notes = REG_NOTES (tem_insn);
13568 PATTERN (tem_insn) = pc_rtx;
13569 REG_NOTES (tem_insn) = NULL;
13571 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13572 NULL_RTX, NULL_RTX, NULL_RTX);
13573 distribute_links (LOG_LINKS (tem_insn));
13575 SET_INSN_DELETED (tem_insn);
13576 if (tem_insn == i2)
13577 i2 = NULL;
13579 #ifdef HAVE_cc0
13580 /* Delete the setter too. */
13581 if (cc0_setter)
13583 PATTERN (cc0_setter) = pc_rtx;
13584 old_notes = REG_NOTES (cc0_setter);
13585 REG_NOTES (cc0_setter) = NULL;
13587 distribute_notes (old_notes, cc0_setter,
13588 cc0_setter, NULL,
13589 NULL_RTX, NULL_RTX, NULL_RTX);
13590 distribute_links (LOG_LINKS (cc0_setter));
13592 SET_INSN_DELETED (cc0_setter);
13593 if (cc0_setter == i2)
13594 i2 = NULL;
13596 #endif
13598 else
13600 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13602 /* If there isn't already a REG_UNUSED note, put one
13603 here. Do not place a REG_DEAD note, even if
13604 the register is also used here; that would not
13605 match the algorithm used in lifetime analysis
13606 and can cause the consistency check in the
13607 scheduler to fail. */
13608 if (! find_regno_note (tem_insn, REG_UNUSED,
13609 REGNO (XEXP (note, 0))))
13610 place = tem_insn;
13611 break;
13614 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13615 || (CALL_P (tem_insn)
13616 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13618 place = tem_insn;
13620 /* If we are doing a 3->2 combination, and we have a
13621 register which formerly died in i3 and was not used
13622 by i2, which now no longer dies in i3 and is used in
13623 i2 but does not die in i2, and place is between i2
13624 and i3, then we may need to move a link from place to
13625 i2. */
13626 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13627 && from_insn
13628 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13629 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13631 struct insn_link *links = LOG_LINKS (place);
13632 LOG_LINKS (place) = NULL;
13633 distribute_links (links);
13635 break;
13638 if (tem_insn == BB_HEAD (bb))
13639 break;
13644 /* If the register is set or already dead at PLACE, we needn't do
13645 anything with this note if it is still a REG_DEAD note.
13646 We check here if it is set at all, not if is it totally replaced,
13647 which is what `dead_or_set_p' checks, so also check for it being
13648 set partially. */
13650 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13652 unsigned int regno = REGNO (XEXP (note, 0));
13653 reg_stat_type *rsp = &reg_stat[regno];
13655 if (dead_or_set_p (place, XEXP (note, 0))
13656 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13658 /* Unless the register previously died in PLACE, clear
13659 last_death. [I no longer understand why this is
13660 being done.] */
13661 if (rsp->last_death != place)
13662 rsp->last_death = 0;
13663 place = 0;
13665 else
13666 rsp->last_death = place;
13668 /* If this is a death note for a hard reg that is occupying
13669 multiple registers, ensure that we are still using all
13670 parts of the object. If we find a piece of the object
13671 that is unused, we must arrange for an appropriate REG_DEAD
13672 note to be added for it. However, we can't just emit a USE
13673 and tag the note to it, since the register might actually
13674 be dead; so we recourse, and the recursive call then finds
13675 the previous insn that used this register. */
13677 if (place && regno < FIRST_PSEUDO_REGISTER
13678 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13680 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13681 bool all_used = true;
13682 unsigned int i;
13684 for (i = regno; i < endregno; i++)
13685 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13686 && ! find_regno_fusage (place, USE, i))
13687 || dead_or_set_regno_p (place, i))
13689 all_used = false;
13690 break;
13693 if (! all_used)
13695 /* Put only REG_DEAD notes for pieces that are
13696 not already dead or set. */
13698 for (i = regno; i < endregno;
13699 i += hard_regno_nregs[i][reg_raw_mode[i]])
13701 rtx piece = regno_reg_rtx[i];
13702 basic_block bb = this_basic_block;
13704 if (! dead_or_set_p (place, piece)
13705 && ! reg_bitfield_target_p (piece,
13706 PATTERN (place)))
13708 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13709 NULL_RTX);
13711 distribute_notes (new_note, place, place,
13712 NULL, NULL_RTX, NULL_RTX,
13713 NULL_RTX);
13715 else if (! refers_to_regno_p (i, i + 1,
13716 PATTERN (place), 0)
13717 && ! find_regno_fusage (place, USE, i))
13718 for (tem_insn = PREV_INSN (place); ;
13719 tem_insn = PREV_INSN (tem_insn))
13721 if (!NONDEBUG_INSN_P (tem_insn))
13723 if (tem_insn == BB_HEAD (bb))
13724 break;
13725 continue;
13727 if (dead_or_set_p (tem_insn, piece)
13728 || reg_bitfield_target_p (piece,
13729 PATTERN (tem_insn)))
13731 add_reg_note (tem_insn, REG_UNUSED, piece);
13732 break;
13737 place = 0;
13741 break;
13743 default:
13744 /* Any other notes should not be present at this point in the
13745 compilation. */
13746 gcc_unreachable ();
13749 if (place)
13751 XEXP (note, 1) = REG_NOTES (place);
13752 REG_NOTES (place) = note;
13755 if (place2)
13756 add_shallow_copy_of_reg_note (place2, note);
13760 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13761 I3, I2, and I1 to new locations. This is also called to add a link
13762 pointing at I3 when I3's destination is changed. */
13764 static void
13765 distribute_links (struct insn_link *links)
13767 struct insn_link *link, *next_link;
13769 for (link = links; link; link = next_link)
13771 rtx_insn *place = 0;
13772 rtx_insn *insn;
13773 rtx set, reg;
13775 next_link = link->next;
13777 /* If the insn that this link points to is a NOTE or isn't a single
13778 set, ignore it. In the latter case, it isn't clear what we
13779 can do other than ignore the link, since we can't tell which
13780 register it was for. Such links wouldn't be used by combine
13781 anyway.
13783 It is not possible for the destination of the target of the link to
13784 have been changed by combine. The only potential of this is if we
13785 replace I3, I2, and I1 by I3 and I2. But in that case the
13786 destination of I2 also remains unchanged. */
13788 if (NOTE_P (link->insn)
13789 || (set = single_set (link->insn)) == 0)
13790 continue;
13792 reg = SET_DEST (set);
13793 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13794 || GET_CODE (reg) == STRICT_LOW_PART)
13795 reg = XEXP (reg, 0);
13797 /* A LOG_LINK is defined as being placed on the first insn that uses
13798 a register and points to the insn that sets the register. Start
13799 searching at the next insn after the target of the link and stop
13800 when we reach a set of the register or the end of the basic block.
13802 Note that this correctly handles the link that used to point from
13803 I3 to I2. Also note that not much searching is typically done here
13804 since most links don't point very far away. */
13806 for (insn = NEXT_INSN (link->insn);
13807 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13808 || BB_HEAD (this_basic_block->next_bb) != insn));
13809 insn = NEXT_INSN (insn))
13810 if (DEBUG_INSN_P (insn))
13811 continue;
13812 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13814 if (reg_referenced_p (reg, PATTERN (insn)))
13815 place = insn;
13816 break;
13818 else if (CALL_P (insn)
13819 && find_reg_fusage (insn, USE, reg))
13821 place = insn;
13822 break;
13824 else if (INSN_P (insn) && reg_set_p (reg, insn))
13825 break;
13827 /* If we found a place to put the link, place it there unless there
13828 is already a link to the same insn as LINK at that point. */
13830 if (place)
13832 struct insn_link *link2;
13834 FOR_EACH_LOG_LINK (link2, place)
13835 if (link2->insn == link->insn)
13836 break;
13838 if (link2 == NULL)
13840 link->next = LOG_LINKS (place);
13841 LOG_LINKS (place) = link;
13843 /* Set added_links_insn to the earliest insn we added a
13844 link to. */
13845 if (added_links_insn == 0
13846 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13847 added_links_insn = place;
13853 /* Check for any register or memory mentioned in EQUIV that is not
13854 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13855 of EXPR where some registers may have been replaced by constants. */
13857 static bool
13858 unmentioned_reg_p (rtx equiv, rtx expr)
13860 subrtx_iterator::array_type array;
13861 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
13863 const_rtx x = *iter;
13864 if ((REG_P (x) || MEM_P (x))
13865 && !reg_mentioned_p (x, expr))
13866 return true;
13868 return false;
13871 DEBUG_FUNCTION void
13872 dump_combine_stats (FILE *file)
13874 fprintf
13875 (file,
13876 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13877 combine_attempts, combine_merges, combine_extras, combine_successes);
13880 void
13881 dump_combine_total_stats (FILE *file)
13883 fprintf
13884 (file,
13885 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13886 total_attempts, total_merges, total_extras, total_successes);
13889 /* Try combining insns through substitution. */
13890 static unsigned int
13891 rest_of_handle_combine (void)
13893 int rebuild_jump_labels_after_combine;
13895 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13896 df_note_add_problem ();
13897 df_analyze ();
13899 regstat_init_n_sets_and_refs ();
13901 rebuild_jump_labels_after_combine
13902 = combine_instructions (get_insns (), max_reg_num ());
13904 /* Combining insns may have turned an indirect jump into a
13905 direct jump. Rebuild the JUMP_LABEL fields of jumping
13906 instructions. */
13907 if (rebuild_jump_labels_after_combine)
13909 timevar_push (TV_JUMP);
13910 rebuild_jump_labels (get_insns ());
13911 cleanup_cfg (0);
13912 timevar_pop (TV_JUMP);
13915 regstat_free_n_sets_and_refs ();
13916 return 0;
13919 namespace {
13921 const pass_data pass_data_combine =
13923 RTL_PASS, /* type */
13924 "combine", /* name */
13925 OPTGROUP_NONE, /* optinfo_flags */
13926 TV_COMBINE, /* tv_id */
13927 PROP_cfglayout, /* properties_required */
13928 0, /* properties_provided */
13929 0, /* properties_destroyed */
13930 0, /* todo_flags_start */
13931 TODO_df_finish, /* todo_flags_finish */
13934 class pass_combine : public rtl_opt_pass
13936 public:
13937 pass_combine (gcc::context *ctxt)
13938 : rtl_opt_pass (pass_data_combine, ctxt)
13941 /* opt_pass methods: */
13942 virtual bool gate (function *) { return (optimize > 0); }
13943 virtual unsigned int execute (function *)
13945 return rest_of_handle_combine ();
13948 }; // class pass_combine
13950 } // anon namespace
13952 rtl_opt_pass *
13953 make_pass_combine (gcc::context *ctxt)
13955 return new pass_combine (ctxt);