* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / combine.c
blobbce5545099381441f5b68762ccbbac271cbe568d
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 /* Don't substitute for a register intended as a clobberable
1954 operand. */
1955 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1956 if (rtx_equal_p (reg, dest))
1957 return 0;
1959 /* If the clobber represents an earlyclobber operand, we must not
1960 substitute an expression containing the clobbered register.
1961 As we do not analyze the constraint strings here, we have to
1962 make the conservative assumption. However, if the register is
1963 a fixed hard reg, the clobber cannot represent any operand;
1964 we leave it up to the machine description to either accept or
1965 reject use-and-clobber patterns. */
1966 if (!REG_P (reg)
1967 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1968 || !fixed_regs[REGNO (reg)])
1969 if (reg_overlap_mentioned_p (reg, src))
1970 return 0;
1973 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1974 or not), reject, unless nothing volatile comes between it and I3 */
1976 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1978 /* Make sure neither succ nor succ2 contains a volatile reference. */
1979 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1980 return 0;
1981 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1982 return 0;
1983 /* We'll check insns between INSN and I3 below. */
1986 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1987 to be an explicit register variable, and was chosen for a reason. */
1989 if (GET_CODE (src) == ASM_OPERANDS
1990 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1991 return 0;
1993 /* If INSN contains volatile references (specifically volatile MEMs),
1994 we cannot combine across any other volatile references.
1995 Even if INSN doesn't contain volatile references, any intervening
1996 volatile insn might affect machine state. */
1998 is_volatile_p = volatile_refs_p (PATTERN (insn))
1999 ? volatile_refs_p
2000 : volatile_insn_p;
2002 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2003 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2004 return 0;
2006 /* If INSN contains an autoincrement or autodecrement, make sure that
2007 register is not used between there and I3, and not already used in
2008 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2009 Also insist that I3 not be a jump; if it were one
2010 and the incremented register were spilled, we would lose. */
2012 #ifdef AUTO_INC_DEC
2013 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2014 if (REG_NOTE_KIND (link) == REG_INC
2015 && (JUMP_P (i3)
2016 || reg_used_between_p (XEXP (link, 0), insn, i3)
2017 || (pred != NULL_RTX
2018 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2019 || (pred2 != NULL_RTX
2020 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2021 || (succ != NULL_RTX
2022 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2023 || (succ2 != NULL_RTX
2024 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2025 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2026 return 0;
2027 #endif
2029 #ifdef HAVE_cc0
2030 /* Don't combine an insn that follows a CC0-setting insn.
2031 An insn that uses CC0 must not be separated from the one that sets it.
2032 We do, however, allow I2 to follow a CC0-setting insn if that insn
2033 is passed as I1; in that case it will be deleted also.
2034 We also allow combining in this case if all the insns are adjacent
2035 because that would leave the two CC0 insns adjacent as well.
2036 It would be more logical to test whether CC0 occurs inside I1 or I2,
2037 but that would be much slower, and this ought to be equivalent. */
2039 p = prev_nonnote_insn (insn);
2040 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2041 && ! all_adjacent)
2042 return 0;
2043 #endif
2045 /* If we get here, we have passed all the tests and the combination is
2046 to be allowed. */
2048 *pdest = dest;
2049 *psrc = src;
2051 return 1;
2054 /* LOC is the location within I3 that contains its pattern or the component
2055 of a PARALLEL of the pattern. We validate that it is valid for combining.
2057 One problem is if I3 modifies its output, as opposed to replacing it
2058 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2059 doing so would produce an insn that is not equivalent to the original insns.
2061 Consider:
2063 (set (reg:DI 101) (reg:DI 100))
2064 (set (subreg:SI (reg:DI 101) 0) <foo>)
2066 This is NOT equivalent to:
2068 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2069 (set (reg:DI 101) (reg:DI 100))])
2071 Not only does this modify 100 (in which case it might still be valid
2072 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2074 We can also run into a problem if I2 sets a register that I1
2075 uses and I1 gets directly substituted into I3 (not via I2). In that
2076 case, we would be getting the wrong value of I2DEST into I3, so we
2077 must reject the combination. This case occurs when I2 and I1 both
2078 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2079 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2080 of a SET must prevent combination from occurring. The same situation
2081 can occur for I0, in which case I0_NOT_IN_SRC is set.
2083 Before doing the above check, we first try to expand a field assignment
2084 into a set of logical operations.
2086 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2087 we place a register that is both set and used within I3. If more than one
2088 such register is detected, we fail.
2090 Return 1 if the combination is valid, zero otherwise. */
2092 static int
2093 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2094 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2096 rtx x = *loc;
2098 if (GET_CODE (x) == SET)
2100 rtx set = x ;
2101 rtx dest = SET_DEST (set);
2102 rtx src = SET_SRC (set);
2103 rtx inner_dest = dest;
2104 rtx subdest;
2106 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2107 || GET_CODE (inner_dest) == SUBREG
2108 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2109 inner_dest = XEXP (inner_dest, 0);
2111 /* Check for the case where I3 modifies its output, as discussed
2112 above. We don't want to prevent pseudos from being combined
2113 into the address of a MEM, so only prevent the combination if
2114 i1 or i2 set the same MEM. */
2115 if ((inner_dest != dest &&
2116 (!MEM_P (inner_dest)
2117 || rtx_equal_p (i2dest, inner_dest)
2118 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2119 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2120 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2121 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2122 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2124 /* This is the same test done in can_combine_p except we can't test
2125 all_adjacent; we don't have to, since this instruction will stay
2126 in place, thus we are not considering increasing the lifetime of
2127 INNER_DEST.
2129 Also, if this insn sets a function argument, combining it with
2130 something that might need a spill could clobber a previous
2131 function argument; the all_adjacent test in can_combine_p also
2132 checks this; here, we do a more specific test for this case. */
2134 || (REG_P (inner_dest)
2135 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2136 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2137 GET_MODE (inner_dest))))
2138 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2139 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2140 return 0;
2142 /* If DEST is used in I3, it is being killed in this insn, so
2143 record that for later. We have to consider paradoxical
2144 subregs here, since they kill the whole register, but we
2145 ignore partial subregs, STRICT_LOW_PART, etc.
2146 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2147 STACK_POINTER_REGNUM, since these are always considered to be
2148 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2149 subdest = dest;
2150 if (GET_CODE (subdest) == SUBREG
2151 && (GET_MODE_SIZE (GET_MODE (subdest))
2152 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2153 subdest = SUBREG_REG (subdest);
2154 if (pi3dest_killed
2155 && REG_P (subdest)
2156 && reg_referenced_p (subdest, PATTERN (i3))
2157 && REGNO (subdest) != FRAME_POINTER_REGNUM
2158 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2159 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2160 #endif
2161 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2162 && (REGNO (subdest) != ARG_POINTER_REGNUM
2163 || ! fixed_regs [REGNO (subdest)])
2164 #endif
2165 && REGNO (subdest) != STACK_POINTER_REGNUM)
2167 if (*pi3dest_killed)
2168 return 0;
2170 *pi3dest_killed = subdest;
2174 else if (GET_CODE (x) == PARALLEL)
2176 int i;
2178 for (i = 0; i < XVECLEN (x, 0); i++)
2179 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2180 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2181 return 0;
2184 return 1;
2187 /* Return 1 if X is an arithmetic expression that contains a multiplication
2188 and division. We don't count multiplications by powers of two here. */
2190 static int
2191 contains_muldiv (rtx x)
2193 switch (GET_CODE (x))
2195 case MOD: case DIV: case UMOD: case UDIV:
2196 return 1;
2198 case MULT:
2199 return ! (CONST_INT_P (XEXP (x, 1))
2200 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2201 default:
2202 if (BINARY_P (x))
2203 return contains_muldiv (XEXP (x, 0))
2204 || contains_muldiv (XEXP (x, 1));
2206 if (UNARY_P (x))
2207 return contains_muldiv (XEXP (x, 0));
2209 return 0;
2213 /* Determine whether INSN can be used in a combination. Return nonzero if
2214 not. This is used in try_combine to detect early some cases where we
2215 can't perform combinations. */
2217 static int
2218 cant_combine_insn_p (rtx_insn *insn)
2220 rtx set;
2221 rtx src, dest;
2223 /* If this isn't really an insn, we can't do anything.
2224 This can occur when flow deletes an insn that it has merged into an
2225 auto-increment address. */
2226 if (! INSN_P (insn))
2227 return 1;
2229 /* Never combine loads and stores involving hard regs that are likely
2230 to be spilled. The register allocator can usually handle such
2231 reg-reg moves by tying. If we allow the combiner to make
2232 substitutions of likely-spilled regs, reload might die.
2233 As an exception, we allow combinations involving fixed regs; these are
2234 not available to the register allocator so there's no risk involved. */
2236 set = single_set (insn);
2237 if (! set)
2238 return 0;
2239 src = SET_SRC (set);
2240 dest = SET_DEST (set);
2241 if (GET_CODE (src) == SUBREG)
2242 src = SUBREG_REG (src);
2243 if (GET_CODE (dest) == SUBREG)
2244 dest = SUBREG_REG (dest);
2245 if (REG_P (src) && REG_P (dest)
2246 && ((HARD_REGISTER_P (src)
2247 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2248 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2249 || (HARD_REGISTER_P (dest)
2250 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2251 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2252 return 1;
2254 return 0;
2257 struct likely_spilled_retval_info
2259 unsigned regno, nregs;
2260 unsigned mask;
2263 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2264 hard registers that are known to be written to / clobbered in full. */
2265 static void
2266 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2268 struct likely_spilled_retval_info *const info =
2269 (struct likely_spilled_retval_info *) data;
2270 unsigned regno, nregs;
2271 unsigned new_mask;
2273 if (!REG_P (XEXP (set, 0)))
2274 return;
2275 regno = REGNO (x);
2276 if (regno >= info->regno + info->nregs)
2277 return;
2278 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2279 if (regno + nregs <= info->regno)
2280 return;
2281 new_mask = (2U << (nregs - 1)) - 1;
2282 if (regno < info->regno)
2283 new_mask >>= info->regno - regno;
2284 else
2285 new_mask <<= regno - info->regno;
2286 info->mask &= ~new_mask;
2289 /* Return nonzero iff part of the return value is live during INSN, and
2290 it is likely spilled. This can happen when more than one insn is needed
2291 to copy the return value, e.g. when we consider to combine into the
2292 second copy insn for a complex value. */
2294 static int
2295 likely_spilled_retval_p (rtx_insn *insn)
2297 rtx_insn *use = BB_END (this_basic_block);
2298 rtx reg;
2299 rtx_insn *p;
2300 unsigned regno, nregs;
2301 /* We assume here that no machine mode needs more than
2302 32 hard registers when the value overlaps with a register
2303 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2304 unsigned mask;
2305 struct likely_spilled_retval_info info;
2307 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2308 return 0;
2309 reg = XEXP (PATTERN (use), 0);
2310 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2311 return 0;
2312 regno = REGNO (reg);
2313 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2314 if (nregs == 1)
2315 return 0;
2316 mask = (2U << (nregs - 1)) - 1;
2318 /* Disregard parts of the return value that are set later. */
2319 info.regno = regno;
2320 info.nregs = nregs;
2321 info.mask = mask;
2322 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2323 if (INSN_P (p))
2324 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2325 mask = info.mask;
2327 /* Check if any of the (probably) live return value registers is
2328 likely spilled. */
2329 nregs --;
2332 if ((mask & 1 << nregs)
2333 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2334 return 1;
2335 } while (nregs--);
2336 return 0;
2339 /* Adjust INSN after we made a change to its destination.
2341 Changing the destination can invalidate notes that say something about
2342 the results of the insn and a LOG_LINK pointing to the insn. */
2344 static void
2345 adjust_for_new_dest (rtx_insn *insn)
2347 /* For notes, be conservative and simply remove them. */
2348 remove_reg_equal_equiv_notes (insn);
2350 /* The new insn will have a destination that was previously the destination
2351 of an insn just above it. Call distribute_links to make a LOG_LINK from
2352 the next use of that destination. */
2353 distribute_links (alloc_insn_link (insn, NULL));
2355 df_insn_rescan (insn);
2358 /* Return TRUE if combine can reuse reg X in mode MODE.
2359 ADDED_SETS is nonzero if the original set is still required. */
2360 static bool
2361 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2363 unsigned int regno;
2365 if (!REG_P (x))
2366 return false;
2368 regno = REGNO (x);
2369 /* Allow hard registers if the new mode is legal, and occupies no more
2370 registers than the old mode. */
2371 if (regno < FIRST_PSEUDO_REGISTER)
2372 return (HARD_REGNO_MODE_OK (regno, mode)
2373 && (hard_regno_nregs[regno][GET_MODE (x)]
2374 >= hard_regno_nregs[regno][mode]));
2376 /* Or a pseudo that is only used once. */
2377 return (REG_N_SETS (regno) == 1 && !added_sets
2378 && !REG_USERVAR_P (x));
2382 /* Check whether X, the destination of a set, refers to part of
2383 the register specified by REG. */
2385 static bool
2386 reg_subword_p (rtx x, rtx reg)
2388 /* Check that reg is an integer mode register. */
2389 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2390 return false;
2392 if (GET_CODE (x) == STRICT_LOW_PART
2393 || GET_CODE (x) == ZERO_EXTRACT)
2394 x = XEXP (x, 0);
2396 return GET_CODE (x) == SUBREG
2397 && SUBREG_REG (x) == reg
2398 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2401 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2402 Note that the INSN should be deleted *after* removing dead edges, so
2403 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2404 but not for a (set (pc) (label_ref FOO)). */
2406 static void
2407 update_cfg_for_uncondjump (rtx_insn *insn)
2409 basic_block bb = BLOCK_FOR_INSN (insn);
2410 gcc_assert (BB_END (bb) == insn);
2412 purge_dead_edges (bb);
2414 delete_insn (insn);
2415 if (EDGE_COUNT (bb->succs) == 1)
2417 rtx_insn *insn;
2419 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2421 /* Remove barriers from the footer if there are any. */
2422 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2423 if (BARRIER_P (insn))
2425 if (PREV_INSN (insn))
2426 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2427 else
2428 BB_FOOTER (bb) = NEXT_INSN (insn);
2429 if (NEXT_INSN (insn))
2430 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2432 else if (LABEL_P (insn))
2433 break;
2437 /* Try to combine the insns I0, I1 and I2 into I3.
2438 Here I0, I1 and I2 appear earlier than I3.
2439 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2442 If we are combining more than two insns and the resulting insn is not
2443 recognized, try splitting it into two insns. If that happens, I2 and I3
2444 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2445 Otherwise, I0, I1 and I2 are pseudo-deleted.
2447 Return 0 if the combination does not work. Then nothing is changed.
2448 If we did the combination, return the insn at which combine should
2449 resume scanning.
2451 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2452 new direct jump instruction.
2454 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2455 been I3 passed to an earlier try_combine within the same basic
2456 block. */
2458 static rtx_insn *
2459 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2460 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2462 /* New patterns for I3 and I2, respectively. */
2463 rtx newpat, newi2pat = 0;
2464 rtvec newpat_vec_with_clobbers = 0;
2465 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2466 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2467 dead. */
2468 int added_sets_0, added_sets_1, added_sets_2;
2469 /* Total number of SETs to put into I3. */
2470 int total_sets;
2471 /* Nonzero if I2's or I1's body now appears in I3. */
2472 int i2_is_used = 0, i1_is_used = 0;
2473 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2474 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2475 /* Contains I3 if the destination of I3 is used in its source, which means
2476 that the old life of I3 is being killed. If that usage is placed into
2477 I2 and not in I3, a REG_DEAD note must be made. */
2478 rtx i3dest_killed = 0;
2479 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2480 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2481 /* Copy of SET_SRC of I1 and I0, if needed. */
2482 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2483 /* Set if I2DEST was reused as a scratch register. */
2484 bool i2scratch = false;
2485 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2486 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2487 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2488 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2489 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2490 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2491 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2492 /* Notes that must be added to REG_NOTES in I3 and I2. */
2493 rtx new_i3_notes, new_i2_notes;
2494 /* Notes that we substituted I3 into I2 instead of the normal case. */
2495 int i3_subst_into_i2 = 0;
2496 /* Notes that I1, I2 or I3 is a MULT operation. */
2497 int have_mult = 0;
2498 int swap_i2i3 = 0;
2499 int changed_i3_dest = 0;
2501 int maxreg;
2502 rtx_insn *temp_insn;
2503 rtx temp_expr;
2504 struct insn_link *link;
2505 rtx other_pat = 0;
2506 rtx new_other_notes;
2507 int i;
2509 /* Only try four-insn combinations when there's high likelihood of
2510 success. Look for simple insns, such as loads of constants or
2511 binary operations involving a constant. */
2512 if (i0)
2514 int i;
2515 int ngood = 0;
2516 int nshift = 0;
2518 if (!flag_expensive_optimizations)
2519 return 0;
2521 for (i = 0; i < 4; i++)
2523 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2524 rtx set = single_set (insn);
2525 rtx src;
2526 if (!set)
2527 continue;
2528 src = SET_SRC (set);
2529 if (CONSTANT_P (src))
2531 ngood += 2;
2532 break;
2534 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2535 ngood++;
2536 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2537 || GET_CODE (src) == LSHIFTRT)
2538 nshift++;
2540 if (ngood < 2 && nshift < 2)
2541 return 0;
2544 /* Exit early if one of the insns involved can't be used for
2545 combinations. */
2546 if (cant_combine_insn_p (i3)
2547 || cant_combine_insn_p (i2)
2548 || (i1 && cant_combine_insn_p (i1))
2549 || (i0 && cant_combine_insn_p (i0))
2550 || likely_spilled_retval_p (i3))
2551 return 0;
2553 combine_attempts++;
2554 undobuf.other_insn = 0;
2556 /* Reset the hard register usage information. */
2557 CLEAR_HARD_REG_SET (newpat_used_regs);
2559 if (dump_file && (dump_flags & TDF_DETAILS))
2561 if (i0)
2562 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2563 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2564 else if (i1)
2565 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2566 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2567 else
2568 fprintf (dump_file, "\nTrying %d -> %d:\n",
2569 INSN_UID (i2), INSN_UID (i3));
2572 /* If multiple insns feed into one of I2 or I3, they can be in any
2573 order. To simplify the code below, reorder them in sequence. */
2574 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2575 temp_insn = i2, i2 = i0, i0 = temp_insn;
2576 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2577 temp_insn = i1, i1 = i0, i0 = temp_insn;
2578 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2579 temp_insn = i1, i1 = i2, i2 = temp_insn;
2581 added_links_insn = 0;
2583 /* First check for one important special case that the code below will
2584 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2585 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2586 we may be able to replace that destination with the destination of I3.
2587 This occurs in the common code where we compute both a quotient and
2588 remainder into a structure, in which case we want to do the computation
2589 directly into the structure to avoid register-register copies.
2591 Note that this case handles both multiple sets in I2 and also cases
2592 where I2 has a number of CLOBBERs inside the PARALLEL.
2594 We make very conservative checks below and only try to handle the
2595 most common cases of this. For example, we only handle the case
2596 where I2 and I3 are adjacent to avoid making difficult register
2597 usage tests. */
2599 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2600 && REG_P (SET_SRC (PATTERN (i3)))
2601 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2602 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2603 && GET_CODE (PATTERN (i2)) == PARALLEL
2604 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2605 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2606 below would need to check what is inside (and reg_overlap_mentioned_p
2607 doesn't support those codes anyway). Don't allow those destinations;
2608 the resulting insn isn't likely to be recognized anyway. */
2609 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2610 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2611 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2612 SET_DEST (PATTERN (i3)))
2613 && next_active_insn (i2) == i3)
2615 rtx p2 = PATTERN (i2);
2617 /* Make sure that the destination of I3,
2618 which we are going to substitute into one output of I2,
2619 is not used within another output of I2. We must avoid making this:
2620 (parallel [(set (mem (reg 69)) ...)
2621 (set (reg 69) ...)])
2622 which is not well-defined as to order of actions.
2623 (Besides, reload can't handle output reloads for this.)
2625 The problem can also happen if the dest of I3 is a memory ref,
2626 if another dest in I2 is an indirect memory ref. */
2627 for (i = 0; i < XVECLEN (p2, 0); i++)
2628 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2629 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2630 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2631 SET_DEST (XVECEXP (p2, 0, i))))
2632 break;
2634 if (i == XVECLEN (p2, 0))
2635 for (i = 0; i < XVECLEN (p2, 0); i++)
2636 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2637 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2639 combine_merges++;
2641 subst_insn = i3;
2642 subst_low_luid = DF_INSN_LUID (i2);
2644 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2645 i2src = SET_SRC (XVECEXP (p2, 0, i));
2646 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2647 i2dest_killed = dead_or_set_p (i2, i2dest);
2649 /* Replace the dest in I2 with our dest and make the resulting
2650 insn the new pattern for I3. Then skip to where we validate
2651 the pattern. Everything was set up above. */
2652 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2653 newpat = p2;
2654 i3_subst_into_i2 = 1;
2655 goto validate_replacement;
2659 /* If I2 is setting a pseudo to a constant and I3 is setting some
2660 sub-part of it to another constant, merge them by making a new
2661 constant. */
2662 if (i1 == 0
2663 && (temp_expr = single_set (i2)) != 0
2664 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2665 && GET_CODE (PATTERN (i3)) == SET
2666 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2667 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2669 rtx dest = SET_DEST (PATTERN (i3));
2670 int offset = -1;
2671 int width = 0;
2673 if (GET_CODE (dest) == ZERO_EXTRACT)
2675 if (CONST_INT_P (XEXP (dest, 1))
2676 && CONST_INT_P (XEXP (dest, 2)))
2678 width = INTVAL (XEXP (dest, 1));
2679 offset = INTVAL (XEXP (dest, 2));
2680 dest = XEXP (dest, 0);
2681 if (BITS_BIG_ENDIAN)
2682 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2685 else
2687 if (GET_CODE (dest) == STRICT_LOW_PART)
2688 dest = XEXP (dest, 0);
2689 width = GET_MODE_PRECISION (GET_MODE (dest));
2690 offset = 0;
2693 if (offset >= 0)
2695 /* If this is the low part, we're done. */
2696 if (subreg_lowpart_p (dest))
2698 /* Handle the case where inner is twice the size of outer. */
2699 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2700 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2701 offset += GET_MODE_PRECISION (GET_MODE (dest));
2702 /* Otherwise give up for now. */
2703 else
2704 offset = -1;
2707 if (offset >= 0)
2709 rtx inner = SET_SRC (PATTERN (i3));
2710 rtx outer = SET_SRC (temp_expr);
2712 wide_int o
2713 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2714 std::make_pair (inner, GET_MODE (dest)),
2715 offset, width);
2717 combine_merges++;
2718 subst_insn = i3;
2719 subst_low_luid = DF_INSN_LUID (i2);
2720 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2721 i2dest = SET_DEST (temp_expr);
2722 i2dest_killed = dead_or_set_p (i2, i2dest);
2724 /* Replace the source in I2 with the new constant and make the
2725 resulting insn the new pattern for I3. Then skip to where we
2726 validate the pattern. Everything was set up above. */
2727 SUBST (SET_SRC (temp_expr),
2728 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2730 newpat = PATTERN (i2);
2732 /* The dest of I3 has been replaced with the dest of I2. */
2733 changed_i3_dest = 1;
2734 goto validate_replacement;
2738 #ifndef HAVE_cc0
2739 /* If we have no I1 and I2 looks like:
2740 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2741 (set Y OP)])
2742 make up a dummy I1 that is
2743 (set Y OP)
2744 and change I2 to be
2745 (set (reg:CC X) (compare:CC Y (const_int 0)))
2747 (We can ignore any trailing CLOBBERs.)
2749 This undoes a previous combination and allows us to match a branch-and-
2750 decrement insn. */
2752 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2753 && XVECLEN (PATTERN (i2), 0) >= 2
2754 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2755 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2756 == MODE_CC)
2757 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2758 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2759 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2760 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2761 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2762 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2764 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2765 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2766 break;
2768 if (i == 1)
2770 /* We make I1 with the same INSN_UID as I2. This gives it
2771 the same DF_INSN_LUID for value tracking. Our fake I1 will
2772 never appear in the insn stream so giving it the same INSN_UID
2773 as I2 will not cause a problem. */
2775 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2776 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2777 -1, NULL_RTX);
2778 INSN_UID (i1) = INSN_UID (i2);
2780 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2781 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2782 SET_DEST (PATTERN (i1)));
2783 SUBST_LINK (LOG_LINKS (i2), alloc_insn_link (i1, LOG_LINKS (i2)));
2786 #endif
2788 /* Verify that I2 and I1 are valid for combining. */
2789 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2790 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2791 &i1dest, &i1src))
2792 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2793 &i0dest, &i0src)))
2795 undo_all ();
2796 return 0;
2799 /* Record whether I2DEST is used in I2SRC and similarly for the other
2800 cases. Knowing this will help in register status updating below. */
2801 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2802 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2803 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2804 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2805 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2806 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2807 i2dest_killed = dead_or_set_p (i2, i2dest);
2808 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2809 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2811 /* For the earlier insns, determine which of the subsequent ones they
2812 feed. */
2813 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2814 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2815 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2816 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2817 && reg_overlap_mentioned_p (i0dest, i2src))));
2819 /* Ensure that I3's pattern can be the destination of combines. */
2820 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2821 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2822 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2823 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2824 &i3dest_killed))
2826 undo_all ();
2827 return 0;
2830 /* See if any of the insns is a MULT operation. Unless one is, we will
2831 reject a combination that is, since it must be slower. Be conservative
2832 here. */
2833 if (GET_CODE (i2src) == MULT
2834 || (i1 != 0 && GET_CODE (i1src) == MULT)
2835 || (i0 != 0 && GET_CODE (i0src) == MULT)
2836 || (GET_CODE (PATTERN (i3)) == SET
2837 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2838 have_mult = 1;
2840 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2841 We used to do this EXCEPT in one case: I3 has a post-inc in an
2842 output operand. However, that exception can give rise to insns like
2843 mov r3,(r3)+
2844 which is a famous insn on the PDP-11 where the value of r3 used as the
2845 source was model-dependent. Avoid this sort of thing. */
2847 #if 0
2848 if (!(GET_CODE (PATTERN (i3)) == SET
2849 && REG_P (SET_SRC (PATTERN (i3)))
2850 && MEM_P (SET_DEST (PATTERN (i3)))
2851 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2852 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2853 /* It's not the exception. */
2854 #endif
2855 #ifdef AUTO_INC_DEC
2857 rtx link;
2858 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2859 if (REG_NOTE_KIND (link) == REG_INC
2860 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2861 || (i1 != 0
2862 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2864 undo_all ();
2865 return 0;
2868 #endif
2870 /* See if the SETs in I1 or I2 need to be kept around in the merged
2871 instruction: whenever the value set there is still needed past I3.
2872 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2874 For the SET in I1, we have two cases: if I1 and I2 independently feed
2875 into I3, the set in I1 needs to be kept around unless I1DEST dies
2876 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2877 in I1 needs to be kept around unless I1DEST dies or is set in either
2878 I2 or I3. The same considerations apply to I0. */
2880 added_sets_2 = !dead_or_set_p (i3, i2dest);
2882 if (i1)
2883 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2884 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2885 else
2886 added_sets_1 = 0;
2888 if (i0)
2889 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2890 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
2891 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
2892 && dead_or_set_p (i2, i0dest)));
2893 else
2894 added_sets_0 = 0;
2896 /* We are about to copy insns for the case where they need to be kept
2897 around. Check that they can be copied in the merged instruction. */
2899 if (targetm.cannot_copy_insn_p
2900 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2901 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2902 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2904 undo_all ();
2905 return 0;
2908 /* If the set in I2 needs to be kept around, we must make a copy of
2909 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2910 PATTERN (I2), we are only substituting for the original I1DEST, not into
2911 an already-substituted copy. This also prevents making self-referential
2912 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2913 I2DEST. */
2915 if (added_sets_2)
2917 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2918 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2919 else
2920 i2pat = copy_rtx (PATTERN (i2));
2923 if (added_sets_1)
2925 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2926 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2927 else
2928 i1pat = copy_rtx (PATTERN (i1));
2931 if (added_sets_0)
2933 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2934 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2935 else
2936 i0pat = copy_rtx (PATTERN (i0));
2939 combine_merges++;
2941 /* Substitute in the latest insn for the regs set by the earlier ones. */
2943 maxreg = max_reg_num ();
2945 subst_insn = i3;
2947 #ifndef HAVE_cc0
2948 /* Many machines that don't use CC0 have insns that can both perform an
2949 arithmetic operation and set the condition code. These operations will
2950 be represented as a PARALLEL with the first element of the vector
2951 being a COMPARE of an arithmetic operation with the constant zero.
2952 The second element of the vector will set some pseudo to the result
2953 of the same arithmetic operation. If we simplify the COMPARE, we won't
2954 match such a pattern and so will generate an extra insn. Here we test
2955 for this case, where both the comparison and the operation result are
2956 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2957 I2SRC. Later we will make the PARALLEL that contains I2. */
2959 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2960 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2961 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
2962 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2964 rtx newpat_dest;
2965 rtx *cc_use_loc = NULL;
2966 rtx_insn *cc_use_insn = NULL;
2967 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
2968 enum machine_mode compare_mode, orig_compare_mode;
2969 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
2971 newpat = PATTERN (i3);
2972 newpat_dest = SET_DEST (newpat);
2973 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
2975 if (undobuf.other_insn == 0
2976 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
2977 &cc_use_insn)))
2979 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
2980 compare_code = simplify_compare_const (compare_code,
2981 GET_MODE (i2dest), op0, &op1);
2982 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
2985 /* Do the rest only if op1 is const0_rtx, which may be the
2986 result of simplification. */
2987 if (op1 == const0_rtx)
2989 /* If a single use of the CC is found, prepare to modify it
2990 when SELECT_CC_MODE returns a new CC-class mode, or when
2991 the above simplify_compare_const() returned a new comparison
2992 operator. undobuf.other_insn is assigned the CC use insn
2993 when modifying it. */
2994 if (cc_use_loc)
2996 #ifdef SELECT_CC_MODE
2997 enum machine_mode new_mode
2998 = SELECT_CC_MODE (compare_code, op0, op1);
2999 if (new_mode != orig_compare_mode
3000 && can_change_dest_mode (SET_DEST (newpat),
3001 added_sets_2, new_mode))
3003 unsigned int regno = REGNO (newpat_dest);
3004 compare_mode = new_mode;
3005 if (regno < FIRST_PSEUDO_REGISTER)
3006 newpat_dest = gen_rtx_REG (compare_mode, regno);
3007 else
3009 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3010 newpat_dest = regno_reg_rtx[regno];
3013 #endif
3014 /* Cases for modifying the CC-using comparison. */
3015 if (compare_code != orig_compare_code
3016 /* ??? Do we need to verify the zero rtx? */
3017 && XEXP (*cc_use_loc, 1) == const0_rtx)
3019 /* Replace cc_use_loc with entire new RTX. */
3020 SUBST (*cc_use_loc,
3021 gen_rtx_fmt_ee (compare_code, compare_mode,
3022 newpat_dest, const0_rtx));
3023 undobuf.other_insn = cc_use_insn;
3025 else if (compare_mode != orig_compare_mode)
3027 /* Just replace the CC reg with a new mode. */
3028 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3029 undobuf.other_insn = cc_use_insn;
3033 /* Now we modify the current newpat:
3034 First, SET_DEST(newpat) is updated if the CC mode has been
3035 altered. For targets without SELECT_CC_MODE, this should be
3036 optimized away. */
3037 if (compare_mode != orig_compare_mode)
3038 SUBST (SET_DEST (newpat), newpat_dest);
3039 /* This is always done to propagate i2src into newpat. */
3040 SUBST (SET_SRC (newpat),
3041 gen_rtx_COMPARE (compare_mode, op0, op1));
3042 /* Create new version of i2pat if needed; the below PARALLEL
3043 creation needs this to work correctly. */
3044 if (! rtx_equal_p (i2src, op0))
3045 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3046 i2_is_used = 1;
3049 #endif
3051 if (i2_is_used == 0)
3053 /* It is possible that the source of I2 or I1 may be performing
3054 an unneeded operation, such as a ZERO_EXTEND of something
3055 that is known to have the high part zero. Handle that case
3056 by letting subst look at the inner insns.
3058 Another way to do this would be to have a function that tries
3059 to simplify a single insn instead of merging two or more
3060 insns. We don't do this because of the potential of infinite
3061 loops and because of the potential extra memory required.
3062 However, doing it the way we are is a bit of a kludge and
3063 doesn't catch all cases.
3065 But only do this if -fexpensive-optimizations since it slows
3066 things down and doesn't usually win.
3068 This is not done in the COMPARE case above because the
3069 unmodified I2PAT is used in the PARALLEL and so a pattern
3070 with a modified I2SRC would not match. */
3072 if (flag_expensive_optimizations)
3074 /* Pass pc_rtx so no substitutions are done, just
3075 simplifications. */
3076 if (i1)
3078 subst_low_luid = DF_INSN_LUID (i1);
3079 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3082 subst_low_luid = DF_INSN_LUID (i2);
3083 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3086 n_occurrences = 0; /* `subst' counts here */
3087 subst_low_luid = DF_INSN_LUID (i2);
3089 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3090 copy of I2SRC each time we substitute it, in order to avoid creating
3091 self-referential RTL when we will be substituting I1SRC for I1DEST
3092 later. Likewise if I0 feeds into I2, either directly or indirectly
3093 through I1, and I0DEST is in I0SRC. */
3094 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3095 (i1_feeds_i2_n && i1dest_in_i1src)
3096 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3097 && i0dest_in_i0src));
3098 substed_i2 = 1;
3100 /* Record whether I2's body now appears within I3's body. */
3101 i2_is_used = n_occurrences;
3104 /* If we already got a failure, don't try to do more. Otherwise, try to
3105 substitute I1 if we have it. */
3107 if (i1 && GET_CODE (newpat) != CLOBBER)
3109 /* Check that an autoincrement side-effect on I1 has not been lost.
3110 This happens if I1DEST is mentioned in I2 and dies there, and
3111 has disappeared from the new pattern. */
3112 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3113 && i1_feeds_i2_n
3114 && dead_or_set_p (i2, i1dest)
3115 && !reg_overlap_mentioned_p (i1dest, newpat))
3116 /* Before we can do this substitution, we must redo the test done
3117 above (see detailed comments there) that ensures I1DEST isn't
3118 mentioned in any SETs in NEWPAT that are field assignments. */
3119 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3120 0, 0, 0))
3122 undo_all ();
3123 return 0;
3126 n_occurrences = 0;
3127 subst_low_luid = DF_INSN_LUID (i1);
3129 /* If the following substitution will modify I1SRC, make a copy of it
3130 for the case where it is substituted for I1DEST in I2PAT later. */
3131 if (added_sets_2 && i1_feeds_i2_n)
3132 i1src_copy = copy_rtx (i1src);
3134 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3135 copy of I1SRC each time we substitute it, in order to avoid creating
3136 self-referential RTL when we will be substituting I0SRC for I0DEST
3137 later. */
3138 newpat = subst (newpat, i1dest, i1src, 0, 0,
3139 i0_feeds_i1_n && i0dest_in_i0src);
3140 substed_i1 = 1;
3142 /* Record whether I1's body now appears within I3's body. */
3143 i1_is_used = n_occurrences;
3146 /* Likewise for I0 if we have it. */
3148 if (i0 && GET_CODE (newpat) != CLOBBER)
3150 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3151 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3152 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3153 && !reg_overlap_mentioned_p (i0dest, newpat))
3154 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3155 0, 0, 0))
3157 undo_all ();
3158 return 0;
3161 /* If the following substitution will modify I0SRC, make a copy of it
3162 for the case where it is substituted for I0DEST in I1PAT later. */
3163 if (added_sets_1 && i0_feeds_i1_n)
3164 i0src_copy = copy_rtx (i0src);
3165 /* And a copy for I0DEST in I2PAT substitution. */
3166 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3167 || (i0_feeds_i2_n)))
3168 i0src_copy2 = copy_rtx (i0src);
3170 n_occurrences = 0;
3171 subst_low_luid = DF_INSN_LUID (i0);
3172 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3173 substed_i0 = 1;
3176 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3177 to count all the ways that I2SRC and I1SRC can be used. */
3178 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3179 && i2_is_used + added_sets_2 > 1)
3180 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3181 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3182 > 1))
3183 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3184 && (n_occurrences + added_sets_0
3185 + (added_sets_1 && i0_feeds_i1_n)
3186 + (added_sets_2 && i0_feeds_i2_n)
3187 > 1))
3188 /* Fail if we tried to make a new register. */
3189 || max_reg_num () != maxreg
3190 /* Fail if we couldn't do something and have a CLOBBER. */
3191 || GET_CODE (newpat) == CLOBBER
3192 /* Fail if this new pattern is a MULT and we didn't have one before
3193 at the outer level. */
3194 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3195 && ! have_mult))
3197 undo_all ();
3198 return 0;
3201 /* If the actions of the earlier insns must be kept
3202 in addition to substituting them into the latest one,
3203 we must make a new PARALLEL for the latest insn
3204 to hold additional the SETs. */
3206 if (added_sets_0 || added_sets_1 || added_sets_2)
3208 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3209 combine_extras++;
3211 if (GET_CODE (newpat) == PARALLEL)
3213 rtvec old = XVEC (newpat, 0);
3214 total_sets = XVECLEN (newpat, 0) + extra_sets;
3215 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3216 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3217 sizeof (old->elem[0]) * old->num_elem);
3219 else
3221 rtx old = newpat;
3222 total_sets = 1 + extra_sets;
3223 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3224 XVECEXP (newpat, 0, 0) = old;
3227 if (added_sets_0)
3228 XVECEXP (newpat, 0, --total_sets) = i0pat;
3230 if (added_sets_1)
3232 rtx t = i1pat;
3233 if (i0_feeds_i1_n)
3234 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3236 XVECEXP (newpat, 0, --total_sets) = t;
3238 if (added_sets_2)
3240 rtx t = i2pat;
3241 if (i1_feeds_i2_n)
3242 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3243 i0_feeds_i1_n && i0dest_in_i0src);
3244 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3245 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3247 XVECEXP (newpat, 0, --total_sets) = t;
3251 validate_replacement:
3253 /* Note which hard regs this insn has as inputs. */
3254 mark_used_regs_combine (newpat);
3256 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3257 consider splitting this pattern, we might need these clobbers. */
3258 if (i1 && GET_CODE (newpat) == PARALLEL
3259 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3261 int len = XVECLEN (newpat, 0);
3263 newpat_vec_with_clobbers = rtvec_alloc (len);
3264 for (i = 0; i < len; i++)
3265 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3268 /* Is the result of combination a valid instruction? */
3269 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3271 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3272 the second SET's destination is a register that is unused and isn't
3273 marked as an instruction that might trap in an EH region. In that case,
3274 we just need the first SET. This can occur when simplifying a divmod
3275 insn. We *must* test for this case here because the code below that
3276 splits two independent SETs doesn't handle this case correctly when it
3277 updates the register status.
3279 It's pointless doing this if we originally had two sets, one from
3280 i3, and one from i2. Combining then splitting the parallel results
3281 in the original i2 again plus an invalid insn (which we delete).
3282 The net effect is only to move instructions around, which makes
3283 debug info less accurate.
3285 Also check the case where the first SET's destination is unused.
3286 That would not cause incorrect code, but does cause an unneeded
3287 insn to remain. */
3289 if (insn_code_number < 0
3290 && !(added_sets_2 && i1 == 0)
3291 && GET_CODE (newpat) == PARALLEL
3292 && XVECLEN (newpat, 0) == 2
3293 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3294 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3295 && asm_noperands (newpat) < 0)
3297 rtx set0 = XVECEXP (newpat, 0, 0);
3298 rtx set1 = XVECEXP (newpat, 0, 1);
3300 if (((REG_P (SET_DEST (set1))
3301 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3302 || (GET_CODE (SET_DEST (set1)) == SUBREG
3303 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3304 && insn_nothrow_p (i3)
3305 && !side_effects_p (SET_SRC (set1)))
3307 newpat = set0;
3308 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3311 else if (((REG_P (SET_DEST (set0))
3312 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3313 || (GET_CODE (SET_DEST (set0)) == SUBREG
3314 && find_reg_note (i3, REG_UNUSED,
3315 SUBREG_REG (SET_DEST (set0)))))
3316 && insn_nothrow_p (i3)
3317 && !side_effects_p (SET_SRC (set0)))
3319 newpat = set1;
3320 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3322 if (insn_code_number >= 0)
3323 changed_i3_dest = 1;
3327 /* If we were combining three insns and the result is a simple SET
3328 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3329 insns. There are two ways to do this. It can be split using a
3330 machine-specific method (like when you have an addition of a large
3331 constant) or by combine in the function find_split_point. */
3333 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3334 && asm_noperands (newpat) < 0)
3336 rtx parallel, *split;
3337 rtx_insn *m_split_insn;
3339 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3340 use I2DEST as a scratch register will help. In the latter case,
3341 convert I2DEST to the mode of the source of NEWPAT if we can. */
3343 m_split_insn = combine_split_insns (newpat, i3);
3345 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3346 inputs of NEWPAT. */
3348 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3349 possible to try that as a scratch reg. This would require adding
3350 more code to make it work though. */
3352 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3354 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3356 /* First try to split using the original register as a
3357 scratch register. */
3358 parallel = gen_rtx_PARALLEL (VOIDmode,
3359 gen_rtvec (2, newpat,
3360 gen_rtx_CLOBBER (VOIDmode,
3361 i2dest)));
3362 m_split_insn = combine_split_insns (parallel, i3);
3364 /* If that didn't work, try changing the mode of I2DEST if
3365 we can. */
3366 if (m_split_insn == 0
3367 && new_mode != GET_MODE (i2dest)
3368 && new_mode != VOIDmode
3369 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3371 enum machine_mode old_mode = GET_MODE (i2dest);
3372 rtx ni2dest;
3374 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3375 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3376 else
3378 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3379 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3382 parallel = (gen_rtx_PARALLEL
3383 (VOIDmode,
3384 gen_rtvec (2, newpat,
3385 gen_rtx_CLOBBER (VOIDmode,
3386 ni2dest))));
3387 m_split_insn = combine_split_insns (parallel, i3);
3389 if (m_split_insn == 0
3390 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3392 struct undo *buf;
3394 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3395 buf = undobuf.undos;
3396 undobuf.undos = buf->next;
3397 buf->next = undobuf.frees;
3398 undobuf.frees = buf;
3402 i2scratch = m_split_insn != 0;
3405 /* If recog_for_combine has discarded clobbers, try to use them
3406 again for the split. */
3407 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3409 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3410 m_split_insn = combine_split_insns (parallel, i3);
3413 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3415 rtx m_split_pat = PATTERN (m_split_insn);
3416 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3417 if (insn_code_number >= 0)
3418 newpat = m_split_pat;
3420 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3421 && (next_nonnote_nondebug_insn (i2) == i3
3422 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3424 rtx i2set, i3set;
3425 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3426 newi2pat = PATTERN (m_split_insn);
3428 i3set = single_set (NEXT_INSN (m_split_insn));
3429 i2set = single_set (m_split_insn);
3431 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3433 /* If I2 or I3 has multiple SETs, we won't know how to track
3434 register status, so don't use these insns. If I2's destination
3435 is used between I2 and I3, we also can't use these insns. */
3437 if (i2_code_number >= 0 && i2set && i3set
3438 && (next_nonnote_nondebug_insn (i2) == i3
3439 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3440 insn_code_number = recog_for_combine (&newi3pat, i3,
3441 &new_i3_notes);
3442 if (insn_code_number >= 0)
3443 newpat = newi3pat;
3445 /* It is possible that both insns now set the destination of I3.
3446 If so, we must show an extra use of it. */
3448 if (insn_code_number >= 0)
3450 rtx new_i3_dest = SET_DEST (i3set);
3451 rtx new_i2_dest = SET_DEST (i2set);
3453 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3454 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3455 || GET_CODE (new_i3_dest) == SUBREG)
3456 new_i3_dest = XEXP (new_i3_dest, 0);
3458 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3459 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3460 || GET_CODE (new_i2_dest) == SUBREG)
3461 new_i2_dest = XEXP (new_i2_dest, 0);
3463 if (REG_P (new_i3_dest)
3464 && REG_P (new_i2_dest)
3465 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3466 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3470 /* If we can split it and use I2DEST, go ahead and see if that
3471 helps things be recognized. Verify that none of the registers
3472 are set between I2 and I3. */
3473 if (insn_code_number < 0
3474 && (split = find_split_point (&newpat, i3, false)) != 0
3475 #ifdef HAVE_cc0
3476 && REG_P (i2dest)
3477 #endif
3478 /* We need I2DEST in the proper mode. If it is a hard register
3479 or the only use of a pseudo, we can change its mode.
3480 Make sure we don't change a hard register to have a mode that
3481 isn't valid for it, or change the number of registers. */
3482 && (GET_MODE (*split) == GET_MODE (i2dest)
3483 || GET_MODE (*split) == VOIDmode
3484 || can_change_dest_mode (i2dest, added_sets_2,
3485 GET_MODE (*split)))
3486 && (next_nonnote_nondebug_insn (i2) == i3
3487 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3488 /* We can't overwrite I2DEST if its value is still used by
3489 NEWPAT. */
3490 && ! reg_referenced_p (i2dest, newpat))
3492 rtx newdest = i2dest;
3493 enum rtx_code split_code = GET_CODE (*split);
3494 enum machine_mode split_mode = GET_MODE (*split);
3495 bool subst_done = false;
3496 newi2pat = NULL_RTX;
3498 i2scratch = true;
3500 /* *SPLIT may be part of I2SRC, so make sure we have the
3501 original expression around for later debug processing.
3502 We should not need I2SRC any more in other cases. */
3503 if (MAY_HAVE_DEBUG_INSNS)
3504 i2src = copy_rtx (i2src);
3505 else
3506 i2src = NULL;
3508 /* Get NEWDEST as a register in the proper mode. We have already
3509 validated that we can do this. */
3510 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3512 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3513 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3514 else
3516 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3517 newdest = regno_reg_rtx[REGNO (i2dest)];
3521 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3522 an ASHIFT. This can occur if it was inside a PLUS and hence
3523 appeared to be a memory address. This is a kludge. */
3524 if (split_code == MULT
3525 && CONST_INT_P (XEXP (*split, 1))
3526 && INTVAL (XEXP (*split, 1)) > 0
3527 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3529 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3530 XEXP (*split, 0), GEN_INT (i)));
3531 /* Update split_code because we may not have a multiply
3532 anymore. */
3533 split_code = GET_CODE (*split);
3536 #ifdef INSN_SCHEDULING
3537 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3538 be written as a ZERO_EXTEND. */
3539 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3541 #ifdef LOAD_EXTEND_OP
3542 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3543 what it really is. */
3544 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3545 == SIGN_EXTEND)
3546 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3547 SUBREG_REG (*split)));
3548 else
3549 #endif
3550 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3551 SUBREG_REG (*split)));
3553 #endif
3555 /* Attempt to split binary operators using arithmetic identities. */
3556 if (BINARY_P (SET_SRC (newpat))
3557 && split_mode == GET_MODE (SET_SRC (newpat))
3558 && ! side_effects_p (SET_SRC (newpat)))
3560 rtx setsrc = SET_SRC (newpat);
3561 enum machine_mode mode = GET_MODE (setsrc);
3562 enum rtx_code code = GET_CODE (setsrc);
3563 rtx src_op0 = XEXP (setsrc, 0);
3564 rtx src_op1 = XEXP (setsrc, 1);
3566 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3567 if (rtx_equal_p (src_op0, src_op1))
3569 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3570 SUBST (XEXP (setsrc, 0), newdest);
3571 SUBST (XEXP (setsrc, 1), newdest);
3572 subst_done = true;
3574 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3575 else if ((code == PLUS || code == MULT)
3576 && GET_CODE (src_op0) == code
3577 && GET_CODE (XEXP (src_op0, 0)) == code
3578 && (INTEGRAL_MODE_P (mode)
3579 || (FLOAT_MODE_P (mode)
3580 && flag_unsafe_math_optimizations)))
3582 rtx p = XEXP (XEXP (src_op0, 0), 0);
3583 rtx q = XEXP (XEXP (src_op0, 0), 1);
3584 rtx r = XEXP (src_op0, 1);
3585 rtx s = src_op1;
3587 /* Split both "((X op Y) op X) op Y" and
3588 "((X op Y) op Y) op X" as "T op T" where T is
3589 "X op Y". */
3590 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3591 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3593 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3594 XEXP (src_op0, 0));
3595 SUBST (XEXP (setsrc, 0), newdest);
3596 SUBST (XEXP (setsrc, 1), newdest);
3597 subst_done = true;
3599 /* Split "((X op X) op Y) op Y)" as "T op T" where
3600 T is "X op Y". */
3601 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3603 rtx tmp = simplify_gen_binary (code, mode, p, r);
3604 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3605 SUBST (XEXP (setsrc, 0), newdest);
3606 SUBST (XEXP (setsrc, 1), newdest);
3607 subst_done = true;
3612 if (!subst_done)
3614 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3615 SUBST (*split, newdest);
3618 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3620 /* recog_for_combine might have added CLOBBERs to newi2pat.
3621 Make sure NEWPAT does not depend on the clobbered regs. */
3622 if (GET_CODE (newi2pat) == PARALLEL)
3623 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3624 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3626 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3627 if (reg_overlap_mentioned_p (reg, newpat))
3629 undo_all ();
3630 return 0;
3634 /* If the split point was a MULT and we didn't have one before,
3635 don't use one now. */
3636 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3637 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3641 /* Check for a case where we loaded from memory in a narrow mode and
3642 then sign extended it, but we need both registers. In that case,
3643 we have a PARALLEL with both loads from the same memory location.
3644 We can split this into a load from memory followed by a register-register
3645 copy. This saves at least one insn, more if register allocation can
3646 eliminate the copy.
3648 We cannot do this if the destination of the first assignment is a
3649 condition code register or cc0. We eliminate this case by making sure
3650 the SET_DEST and SET_SRC have the same mode.
3652 We cannot do this if the destination of the second assignment is
3653 a register that we have already assumed is zero-extended. Similarly
3654 for a SUBREG of such a register. */
3656 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3657 && GET_CODE (newpat) == PARALLEL
3658 && XVECLEN (newpat, 0) == 2
3659 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3660 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3661 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3662 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3663 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3664 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3665 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3666 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3667 DF_INSN_LUID (i2))
3668 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3669 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3670 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3671 (REG_P (temp_expr)
3672 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3673 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3674 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3675 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3676 != GET_MODE_MASK (word_mode))))
3677 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3678 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3679 (REG_P (temp_expr)
3680 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3681 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3682 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3683 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3684 != GET_MODE_MASK (word_mode)))))
3685 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3686 SET_SRC (XVECEXP (newpat, 0, 1)))
3687 && ! find_reg_note (i3, REG_UNUSED,
3688 SET_DEST (XVECEXP (newpat, 0, 0))))
3690 rtx ni2dest;
3692 newi2pat = XVECEXP (newpat, 0, 0);
3693 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3694 newpat = XVECEXP (newpat, 0, 1);
3695 SUBST (SET_SRC (newpat),
3696 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3697 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3699 if (i2_code_number >= 0)
3700 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3702 if (insn_code_number >= 0)
3703 swap_i2i3 = 1;
3706 /* Similarly, check for a case where we have a PARALLEL of two independent
3707 SETs but we started with three insns. In this case, we can do the sets
3708 as two separate insns. This case occurs when some SET allows two
3709 other insns to combine, but the destination of that SET is still live. */
3711 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3712 && GET_CODE (newpat) == PARALLEL
3713 && XVECLEN (newpat, 0) == 2
3714 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3715 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3716 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3717 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3718 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3719 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3720 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3721 XVECEXP (newpat, 0, 0))
3722 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3723 XVECEXP (newpat, 0, 1))
3724 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3725 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3727 rtx set0 = XVECEXP (newpat, 0, 0);
3728 rtx set1 = XVECEXP (newpat, 0, 1);
3730 /* Normally, it doesn't matter which of the two is done first,
3731 but the one that references cc0 can't be the second, and
3732 one which uses any regs/memory set in between i2 and i3 can't
3733 be first. The PARALLEL might also have been pre-existing in i3,
3734 so we need to make sure that we won't wrongly hoist a SET to i2
3735 that would conflict with a death note present in there. */
3736 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3737 && !(REG_P (SET_DEST (set1))
3738 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3739 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3740 && find_reg_note (i2, REG_DEAD,
3741 SUBREG_REG (SET_DEST (set1))))
3742 #ifdef HAVE_cc0
3743 && !reg_referenced_p (cc0_rtx, set0)
3744 #endif
3745 /* If I3 is a jump, ensure that set0 is a jump so that
3746 we do not create invalid RTL. */
3747 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3750 newi2pat = set1;
3751 newpat = set0;
3753 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3754 && !(REG_P (SET_DEST (set0))
3755 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3756 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3757 && find_reg_note (i2, REG_DEAD,
3758 SUBREG_REG (SET_DEST (set0))))
3759 #ifdef HAVE_cc0
3760 && !reg_referenced_p (cc0_rtx, set1)
3761 #endif
3762 /* If I3 is a jump, ensure that set1 is a jump so that
3763 we do not create invalid RTL. */
3764 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3767 newi2pat = set0;
3768 newpat = set1;
3770 else
3772 undo_all ();
3773 return 0;
3776 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3778 if (i2_code_number >= 0)
3780 /* recog_for_combine might have added CLOBBERs to newi2pat.
3781 Make sure NEWPAT does not depend on the clobbered regs. */
3782 if (GET_CODE (newi2pat) == PARALLEL)
3784 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3785 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3787 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3788 if (reg_overlap_mentioned_p (reg, newpat))
3790 undo_all ();
3791 return 0;
3796 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3800 /* If it still isn't recognized, fail and change things back the way they
3801 were. */
3802 if ((insn_code_number < 0
3803 /* Is the result a reasonable ASM_OPERANDS? */
3804 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3806 undo_all ();
3807 return 0;
3810 /* If we had to change another insn, make sure it is valid also. */
3811 if (undobuf.other_insn)
3813 CLEAR_HARD_REG_SET (newpat_used_regs);
3815 other_pat = PATTERN (undobuf.other_insn);
3816 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3817 &new_other_notes);
3819 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3821 undo_all ();
3822 return 0;
3826 #ifdef HAVE_cc0
3827 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3828 they are adjacent to each other or not. */
3830 rtx_insn *p = prev_nonnote_insn (i3);
3831 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3832 && sets_cc0_p (newi2pat))
3834 undo_all ();
3835 return 0;
3838 #endif
3840 /* Only allow this combination if insn_rtx_costs reports that the
3841 replacement instructions are cheaper than the originals. */
3842 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3844 undo_all ();
3845 return 0;
3848 if (MAY_HAVE_DEBUG_INSNS)
3850 struct undo *undo;
3852 for (undo = undobuf.undos; undo; undo = undo->next)
3853 if (undo->kind == UNDO_MODE)
3855 rtx reg = *undo->where.r;
3856 enum machine_mode new_mode = GET_MODE (reg);
3857 enum machine_mode old_mode = undo->old_contents.m;
3859 /* Temporarily revert mode back. */
3860 adjust_reg_mode (reg, old_mode);
3862 if (reg == i2dest && i2scratch)
3864 /* If we used i2dest as a scratch register with a
3865 different mode, substitute it for the original
3866 i2src while its original mode is temporarily
3867 restored, and then clear i2scratch so that we don't
3868 do it again later. */
3869 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3870 this_basic_block);
3871 i2scratch = false;
3872 /* Put back the new mode. */
3873 adjust_reg_mode (reg, new_mode);
3875 else
3877 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3878 rtx_insn *first, *last;
3880 if (reg == i2dest)
3882 first = i2;
3883 last = last_combined_insn;
3885 else
3887 first = i3;
3888 last = undobuf.other_insn;
3889 gcc_assert (last);
3890 if (DF_INSN_LUID (last)
3891 < DF_INSN_LUID (last_combined_insn))
3892 last = last_combined_insn;
3895 /* We're dealing with a reg that changed mode but not
3896 meaning, so we want to turn it into a subreg for
3897 the new mode. However, because of REG sharing and
3898 because its mode had already changed, we have to do
3899 it in two steps. First, replace any debug uses of
3900 reg, with its original mode temporarily restored,
3901 with this copy we have created; then, replace the
3902 copy with the SUBREG of the original shared reg,
3903 once again changed to the new mode. */
3904 propagate_for_debug (first, last, reg, tempreg,
3905 this_basic_block);
3906 adjust_reg_mode (reg, new_mode);
3907 propagate_for_debug (first, last, tempreg,
3908 lowpart_subreg (old_mode, reg, new_mode),
3909 this_basic_block);
3914 /* If we will be able to accept this, we have made a
3915 change to the destination of I3. This requires us to
3916 do a few adjustments. */
3918 if (changed_i3_dest)
3920 PATTERN (i3) = newpat;
3921 adjust_for_new_dest (i3);
3924 /* We now know that we can do this combination. Merge the insns and
3925 update the status of registers and LOG_LINKS. */
3927 if (undobuf.other_insn)
3929 rtx note, next;
3931 PATTERN (undobuf.other_insn) = other_pat;
3933 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
3934 ensure that they are still valid. Then add any non-duplicate
3935 notes added by recog_for_combine. */
3936 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3938 next = XEXP (note, 1);
3940 if ((REG_NOTE_KIND (note) == REG_DEAD
3941 && !reg_referenced_p (XEXP (note, 0),
3942 PATTERN (undobuf.other_insn)))
3943 ||(REG_NOTE_KIND (note) == REG_UNUSED
3944 && !reg_set_p (XEXP (note, 0),
3945 PATTERN (undobuf.other_insn))))
3946 remove_note (undobuf.other_insn, note);
3949 distribute_notes (new_other_notes, undobuf.other_insn,
3950 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
3951 NULL_RTX);
3954 if (swap_i2i3)
3956 rtx_insn *insn;
3957 struct insn_link *link;
3958 rtx ni2dest;
3960 /* I3 now uses what used to be its destination and which is now
3961 I2's destination. This requires us to do a few adjustments. */
3962 PATTERN (i3) = newpat;
3963 adjust_for_new_dest (i3);
3965 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3966 so we still will.
3968 However, some later insn might be using I2's dest and have
3969 a LOG_LINK pointing at I3. We must remove this link.
3970 The simplest way to remove the link is to point it at I1,
3971 which we know will be a NOTE. */
3973 /* newi2pat is usually a SET here; however, recog_for_combine might
3974 have added some clobbers. */
3975 if (GET_CODE (newi2pat) == PARALLEL)
3976 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3977 else
3978 ni2dest = SET_DEST (newi2pat);
3980 for (insn = NEXT_INSN (i3);
3981 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
3982 || insn != BB_HEAD (this_basic_block->next_bb));
3983 insn = NEXT_INSN (insn))
3985 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3987 FOR_EACH_LOG_LINK (link, insn)
3988 if (link->insn == i3)
3989 link->insn = i1;
3991 break;
3997 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3998 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
3999 rtx midnotes = 0;
4000 int from_luid;
4001 /* Compute which registers we expect to eliminate. newi2pat may be setting
4002 either i3dest or i2dest, so we must check it. Also, i1dest may be the
4003 same as i3dest, in which case newi2pat may be setting i1dest. */
4004 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4005 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4006 || !i2dest_killed
4007 ? 0 : i2dest);
4008 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4009 || (newi2pat && reg_set_p (i1dest, newi2pat))
4010 || !i1dest_killed
4011 ? 0 : i1dest);
4012 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4013 || (newi2pat && reg_set_p (i0dest, newi2pat))
4014 || !i0dest_killed
4015 ? 0 : i0dest);
4017 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4018 clear them. */
4019 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4020 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4021 if (i1)
4022 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4023 if (i0)
4024 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4026 /* Ensure that we do not have something that should not be shared but
4027 occurs multiple times in the new insns. Check this by first
4028 resetting all the `used' flags and then copying anything is shared. */
4030 reset_used_flags (i3notes);
4031 reset_used_flags (i2notes);
4032 reset_used_flags (i1notes);
4033 reset_used_flags (i0notes);
4034 reset_used_flags (newpat);
4035 reset_used_flags (newi2pat);
4036 if (undobuf.other_insn)
4037 reset_used_flags (PATTERN (undobuf.other_insn));
4039 i3notes = copy_rtx_if_shared (i3notes);
4040 i2notes = copy_rtx_if_shared (i2notes);
4041 i1notes = copy_rtx_if_shared (i1notes);
4042 i0notes = copy_rtx_if_shared (i0notes);
4043 newpat = copy_rtx_if_shared (newpat);
4044 newi2pat = copy_rtx_if_shared (newi2pat);
4045 if (undobuf.other_insn)
4046 reset_used_flags (PATTERN (undobuf.other_insn));
4048 INSN_CODE (i3) = insn_code_number;
4049 PATTERN (i3) = newpat;
4051 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4053 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4055 reset_used_flags (call_usage);
4056 call_usage = copy_rtx (call_usage);
4058 if (substed_i2)
4060 /* I2SRC must still be meaningful at this point. Some splitting
4061 operations can invalidate I2SRC, but those operations do not
4062 apply to calls. */
4063 gcc_assert (i2src);
4064 replace_rtx (call_usage, i2dest, i2src);
4067 if (substed_i1)
4068 replace_rtx (call_usage, i1dest, i1src);
4069 if (substed_i0)
4070 replace_rtx (call_usage, i0dest, i0src);
4072 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4075 if (undobuf.other_insn)
4076 INSN_CODE (undobuf.other_insn) = other_code_number;
4078 /* We had one special case above where I2 had more than one set and
4079 we replaced a destination of one of those sets with the destination
4080 of I3. In that case, we have to update LOG_LINKS of insns later
4081 in this basic block. Note that this (expensive) case is rare.
4083 Also, in this case, we must pretend that all REG_NOTEs for I2
4084 actually came from I3, so that REG_UNUSED notes from I2 will be
4085 properly handled. */
4087 if (i3_subst_into_i2)
4089 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4090 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4091 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4092 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4093 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4094 && ! find_reg_note (i2, REG_UNUSED,
4095 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4096 for (temp_insn = NEXT_INSN (i2);
4097 temp_insn
4098 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4099 || BB_HEAD (this_basic_block) != temp_insn);
4100 temp_insn = NEXT_INSN (temp_insn))
4101 if (temp_insn != i3 && INSN_P (temp_insn))
4102 FOR_EACH_LOG_LINK (link, temp_insn)
4103 if (link->insn == i2)
4104 link->insn = i3;
4106 if (i3notes)
4108 rtx link = i3notes;
4109 while (XEXP (link, 1))
4110 link = XEXP (link, 1);
4111 XEXP (link, 1) = i2notes;
4113 else
4114 i3notes = i2notes;
4115 i2notes = 0;
4118 LOG_LINKS (i3) = NULL;
4119 REG_NOTES (i3) = 0;
4120 LOG_LINKS (i2) = NULL;
4121 REG_NOTES (i2) = 0;
4123 if (newi2pat)
4125 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4126 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4127 this_basic_block);
4128 INSN_CODE (i2) = i2_code_number;
4129 PATTERN (i2) = newi2pat;
4131 else
4133 if (MAY_HAVE_DEBUG_INSNS && i2src)
4134 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4135 this_basic_block);
4136 SET_INSN_DELETED (i2);
4139 if (i1)
4141 LOG_LINKS (i1) = NULL;
4142 REG_NOTES (i1) = 0;
4143 if (MAY_HAVE_DEBUG_INSNS)
4144 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4145 this_basic_block);
4146 SET_INSN_DELETED (i1);
4149 if (i0)
4151 LOG_LINKS (i0) = NULL;
4152 REG_NOTES (i0) = 0;
4153 if (MAY_HAVE_DEBUG_INSNS)
4154 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4155 this_basic_block);
4156 SET_INSN_DELETED (i0);
4159 /* Get death notes for everything that is now used in either I3 or
4160 I2 and used to die in a previous insn. If we built two new
4161 patterns, move from I1 to I2 then I2 to I3 so that we get the
4162 proper movement on registers that I2 modifies. */
4164 if (i0)
4165 from_luid = DF_INSN_LUID (i0);
4166 else if (i1)
4167 from_luid = DF_INSN_LUID (i1);
4168 else
4169 from_luid = DF_INSN_LUID (i2);
4170 if (newi2pat)
4171 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4172 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4174 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4175 if (i3notes)
4176 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4177 elim_i2, elim_i1, elim_i0);
4178 if (i2notes)
4179 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4180 elim_i2, elim_i1, elim_i0);
4181 if (i1notes)
4182 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4183 elim_i2, elim_i1, elim_i0);
4184 if (i0notes)
4185 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4186 elim_i2, elim_i1, elim_i0);
4187 if (midnotes)
4188 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4189 elim_i2, elim_i1, elim_i0);
4191 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4192 know these are REG_UNUSED and want them to go to the desired insn,
4193 so we always pass it as i3. */
4195 if (newi2pat && new_i2_notes)
4196 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4197 NULL_RTX);
4199 if (new_i3_notes)
4200 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4201 NULL_RTX);
4203 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4204 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4205 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4206 in that case, it might delete I2. Similarly for I2 and I1.
4207 Show an additional death due to the REG_DEAD note we make here. If
4208 we discard it in distribute_notes, we will decrement it again. */
4210 if (i3dest_killed)
4212 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4213 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4214 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4215 elim_i1, elim_i0);
4216 else
4217 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4218 elim_i2, elim_i1, elim_i0);
4221 if (i2dest_in_i2src)
4223 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4224 if (newi2pat && reg_set_p (i2dest, newi2pat))
4225 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4226 NULL_RTX, NULL_RTX);
4227 else
4228 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4229 NULL_RTX, NULL_RTX, NULL_RTX);
4232 if (i1dest_in_i1src)
4234 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4235 if (newi2pat && reg_set_p (i1dest, newi2pat))
4236 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4237 NULL_RTX, NULL_RTX);
4238 else
4239 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4240 NULL_RTX, NULL_RTX, NULL_RTX);
4243 if (i0dest_in_i0src)
4245 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4246 if (newi2pat && reg_set_p (i0dest, newi2pat))
4247 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4248 NULL_RTX, NULL_RTX);
4249 else
4250 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4251 NULL_RTX, NULL_RTX, NULL_RTX);
4254 distribute_links (i3links);
4255 distribute_links (i2links);
4256 distribute_links (i1links);
4257 distribute_links (i0links);
4259 if (REG_P (i2dest))
4261 struct insn_link *link;
4262 rtx_insn *i2_insn = 0;
4263 rtx i2_val = 0, set;
4265 /* The insn that used to set this register doesn't exist, and
4266 this life of the register may not exist either. See if one of
4267 I3's links points to an insn that sets I2DEST. If it does,
4268 that is now the last known value for I2DEST. If we don't update
4269 this and I2 set the register to a value that depended on its old
4270 contents, we will get confused. If this insn is used, thing
4271 will be set correctly in combine_instructions. */
4272 FOR_EACH_LOG_LINK (link, i3)
4273 if ((set = single_set (link->insn)) != 0
4274 && rtx_equal_p (i2dest, SET_DEST (set)))
4275 i2_insn = link->insn, i2_val = SET_SRC (set);
4277 record_value_for_reg (i2dest, i2_insn, i2_val);
4279 /* If the reg formerly set in I2 died only once and that was in I3,
4280 zero its use count so it won't make `reload' do any work. */
4281 if (! added_sets_2
4282 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4283 && ! i2dest_in_i2src)
4284 INC_REG_N_SETS (REGNO (i2dest), -1);
4287 if (i1 && REG_P (i1dest))
4289 struct insn_link *link;
4290 rtx_insn *i1_insn = 0;
4291 rtx i1_val = 0, set;
4293 FOR_EACH_LOG_LINK (link, i3)
4294 if ((set = single_set (link->insn)) != 0
4295 && rtx_equal_p (i1dest, SET_DEST (set)))
4296 i1_insn = link->insn, i1_val = SET_SRC (set);
4298 record_value_for_reg (i1dest, i1_insn, i1_val);
4300 if (! added_sets_1 && ! i1dest_in_i1src)
4301 INC_REG_N_SETS (REGNO (i1dest), -1);
4304 if (i0 && REG_P (i0dest))
4306 struct insn_link *link;
4307 rtx_insn *i0_insn = 0;
4308 rtx i0_val = 0, set;
4310 FOR_EACH_LOG_LINK (link, i3)
4311 if ((set = single_set (link->insn)) != 0
4312 && rtx_equal_p (i0dest, SET_DEST (set)))
4313 i0_insn = link->insn, i0_val = SET_SRC (set);
4315 record_value_for_reg (i0dest, i0_insn, i0_val);
4317 if (! added_sets_0 && ! i0dest_in_i0src)
4318 INC_REG_N_SETS (REGNO (i0dest), -1);
4321 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4322 been made to this insn. The order is important, because newi2pat
4323 can affect nonzero_bits of newpat. */
4324 if (newi2pat)
4325 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4326 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4329 if (undobuf.other_insn != NULL_RTX)
4331 if (dump_file)
4333 fprintf (dump_file, "modifying other_insn ");
4334 dump_insn_slim (dump_file, undobuf.other_insn);
4336 df_insn_rescan (undobuf.other_insn);
4339 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4341 if (dump_file)
4343 fprintf (dump_file, "modifying insn i0 ");
4344 dump_insn_slim (dump_file, i0);
4346 df_insn_rescan (i0);
4349 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4351 if (dump_file)
4353 fprintf (dump_file, "modifying insn i1 ");
4354 dump_insn_slim (dump_file, i1);
4356 df_insn_rescan (i1);
4359 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4361 if (dump_file)
4363 fprintf (dump_file, "modifying insn i2 ");
4364 dump_insn_slim (dump_file, i2);
4366 df_insn_rescan (i2);
4369 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4371 if (dump_file)
4373 fprintf (dump_file, "modifying insn i3 ");
4374 dump_insn_slim (dump_file, i3);
4376 df_insn_rescan (i3);
4379 /* Set new_direct_jump_p if a new return or simple jump instruction
4380 has been created. Adjust the CFG accordingly. */
4381 if (returnjump_p (i3) || any_uncondjump_p (i3))
4383 *new_direct_jump_p = 1;
4384 mark_jump_label (PATTERN (i3), i3, 0);
4385 update_cfg_for_uncondjump (i3);
4388 if (undobuf.other_insn != NULL_RTX
4389 && (returnjump_p (undobuf.other_insn)
4390 || any_uncondjump_p (undobuf.other_insn)))
4392 *new_direct_jump_p = 1;
4393 update_cfg_for_uncondjump (undobuf.other_insn);
4396 /* A noop might also need cleaning up of CFG, if it comes from the
4397 simplification of a jump. */
4398 if (JUMP_P (i3)
4399 && GET_CODE (newpat) == SET
4400 && SET_SRC (newpat) == pc_rtx
4401 && SET_DEST (newpat) == pc_rtx)
4403 *new_direct_jump_p = 1;
4404 update_cfg_for_uncondjump (i3);
4407 if (undobuf.other_insn != NULL_RTX
4408 && JUMP_P (undobuf.other_insn)
4409 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4410 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4411 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4413 *new_direct_jump_p = 1;
4414 update_cfg_for_uncondjump (undobuf.other_insn);
4417 combine_successes++;
4418 undo_commit ();
4420 if (added_links_insn
4421 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4422 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4423 return added_links_insn;
4424 else
4425 return newi2pat ? i2 : i3;
4428 /* Undo all the modifications recorded in undobuf. */
4430 static void
4431 undo_all (void)
4433 struct undo *undo, *next;
4435 for (undo = undobuf.undos; undo; undo = next)
4437 next = undo->next;
4438 switch (undo->kind)
4440 case UNDO_RTX:
4441 *undo->where.r = undo->old_contents.r;
4442 break;
4443 case UNDO_INT:
4444 *undo->where.i = undo->old_contents.i;
4445 break;
4446 case UNDO_MODE:
4447 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4448 break;
4449 case UNDO_LINKS:
4450 *undo->where.l = undo->old_contents.l;
4451 break;
4452 default:
4453 gcc_unreachable ();
4456 undo->next = undobuf.frees;
4457 undobuf.frees = undo;
4460 undobuf.undos = 0;
4463 /* We've committed to accepting the changes we made. Move all
4464 of the undos to the free list. */
4466 static void
4467 undo_commit (void)
4469 struct undo *undo, *next;
4471 for (undo = undobuf.undos; undo; undo = next)
4473 next = undo->next;
4474 undo->next = undobuf.frees;
4475 undobuf.frees = undo;
4477 undobuf.undos = 0;
4480 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4481 where we have an arithmetic expression and return that point. LOC will
4482 be inside INSN.
4484 try_combine will call this function to see if an insn can be split into
4485 two insns. */
4487 static rtx *
4488 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4490 rtx x = *loc;
4491 enum rtx_code code = GET_CODE (x);
4492 rtx *split;
4493 unsigned HOST_WIDE_INT len = 0;
4494 HOST_WIDE_INT pos = 0;
4495 int unsignedp = 0;
4496 rtx inner = NULL_RTX;
4498 /* First special-case some codes. */
4499 switch (code)
4501 case SUBREG:
4502 #ifdef INSN_SCHEDULING
4503 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4504 point. */
4505 if (MEM_P (SUBREG_REG (x)))
4506 return loc;
4507 #endif
4508 return find_split_point (&SUBREG_REG (x), insn, false);
4510 case MEM:
4511 #ifdef HAVE_lo_sum
4512 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4513 using LO_SUM and HIGH. */
4514 if (GET_CODE (XEXP (x, 0)) == CONST
4515 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4517 enum machine_mode address_mode = get_address_mode (x);
4519 SUBST (XEXP (x, 0),
4520 gen_rtx_LO_SUM (address_mode,
4521 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4522 XEXP (x, 0)));
4523 return &XEXP (XEXP (x, 0), 0);
4525 #endif
4527 /* If we have a PLUS whose second operand is a constant and the
4528 address is not valid, perhaps will can split it up using
4529 the machine-specific way to split large constants. We use
4530 the first pseudo-reg (one of the virtual regs) as a placeholder;
4531 it will not remain in the result. */
4532 if (GET_CODE (XEXP (x, 0)) == PLUS
4533 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4534 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4535 MEM_ADDR_SPACE (x)))
4537 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4538 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4539 XEXP (x, 0)),
4540 subst_insn);
4542 /* This should have produced two insns, each of which sets our
4543 placeholder. If the source of the second is a valid address,
4544 we can make put both sources together and make a split point
4545 in the middle. */
4547 if (seq
4548 && NEXT_INSN (seq) != NULL_RTX
4549 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4550 && NONJUMP_INSN_P (seq)
4551 && GET_CODE (PATTERN (seq)) == SET
4552 && SET_DEST (PATTERN (seq)) == reg
4553 && ! reg_mentioned_p (reg,
4554 SET_SRC (PATTERN (seq)))
4555 && NONJUMP_INSN_P (NEXT_INSN (seq))
4556 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4557 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4558 && memory_address_addr_space_p
4559 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4560 MEM_ADDR_SPACE (x)))
4562 rtx src1 = SET_SRC (PATTERN (seq));
4563 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4565 /* Replace the placeholder in SRC2 with SRC1. If we can
4566 find where in SRC2 it was placed, that can become our
4567 split point and we can replace this address with SRC2.
4568 Just try two obvious places. */
4570 src2 = replace_rtx (src2, reg, src1);
4571 split = 0;
4572 if (XEXP (src2, 0) == src1)
4573 split = &XEXP (src2, 0);
4574 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4575 && XEXP (XEXP (src2, 0), 0) == src1)
4576 split = &XEXP (XEXP (src2, 0), 0);
4578 if (split)
4580 SUBST (XEXP (x, 0), src2);
4581 return split;
4585 /* If that didn't work, perhaps the first operand is complex and
4586 needs to be computed separately, so make a split point there.
4587 This will occur on machines that just support REG + CONST
4588 and have a constant moved through some previous computation. */
4590 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4591 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4592 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4593 return &XEXP (XEXP (x, 0), 0);
4596 /* If we have a PLUS whose first operand is complex, try computing it
4597 separately by making a split there. */
4598 if (GET_CODE (XEXP (x, 0)) == PLUS
4599 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4600 MEM_ADDR_SPACE (x))
4601 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4602 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4603 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4604 return &XEXP (XEXP (x, 0), 0);
4605 break;
4607 case SET:
4608 #ifdef HAVE_cc0
4609 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4610 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4611 we need to put the operand into a register. So split at that
4612 point. */
4614 if (SET_DEST (x) == cc0_rtx
4615 && GET_CODE (SET_SRC (x)) != COMPARE
4616 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4617 && !OBJECT_P (SET_SRC (x))
4618 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4619 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4620 return &SET_SRC (x);
4621 #endif
4623 /* See if we can split SET_SRC as it stands. */
4624 split = find_split_point (&SET_SRC (x), insn, true);
4625 if (split && split != &SET_SRC (x))
4626 return split;
4628 /* See if we can split SET_DEST as it stands. */
4629 split = find_split_point (&SET_DEST (x), insn, false);
4630 if (split && split != &SET_DEST (x))
4631 return split;
4633 /* See if this is a bitfield assignment with everything constant. If
4634 so, this is an IOR of an AND, so split it into that. */
4635 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4636 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4637 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4638 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4639 && CONST_INT_P (SET_SRC (x))
4640 && ((INTVAL (XEXP (SET_DEST (x), 1))
4641 + INTVAL (XEXP (SET_DEST (x), 2)))
4642 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4643 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4645 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4646 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4647 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4648 rtx dest = XEXP (SET_DEST (x), 0);
4649 enum machine_mode mode = GET_MODE (dest);
4650 unsigned HOST_WIDE_INT mask
4651 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4652 rtx or_mask;
4654 if (BITS_BIG_ENDIAN)
4655 pos = GET_MODE_PRECISION (mode) - len - pos;
4657 or_mask = gen_int_mode (src << pos, mode);
4658 if (src == mask)
4659 SUBST (SET_SRC (x),
4660 simplify_gen_binary (IOR, mode, dest, or_mask));
4661 else
4663 rtx negmask = gen_int_mode (~(mask << pos), mode);
4664 SUBST (SET_SRC (x),
4665 simplify_gen_binary (IOR, mode,
4666 simplify_gen_binary (AND, mode,
4667 dest, negmask),
4668 or_mask));
4671 SUBST (SET_DEST (x), dest);
4673 split = find_split_point (&SET_SRC (x), insn, true);
4674 if (split && split != &SET_SRC (x))
4675 return split;
4678 /* Otherwise, see if this is an operation that we can split into two.
4679 If so, try to split that. */
4680 code = GET_CODE (SET_SRC (x));
4682 switch (code)
4684 case AND:
4685 /* If we are AND'ing with a large constant that is only a single
4686 bit and the result is only being used in a context where we
4687 need to know if it is zero or nonzero, replace it with a bit
4688 extraction. This will avoid the large constant, which might
4689 have taken more than one insn to make. If the constant were
4690 not a valid argument to the AND but took only one insn to make,
4691 this is no worse, but if it took more than one insn, it will
4692 be better. */
4694 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4695 && REG_P (XEXP (SET_SRC (x), 0))
4696 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4697 && REG_P (SET_DEST (x))
4698 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4699 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4700 && XEXP (*split, 0) == SET_DEST (x)
4701 && XEXP (*split, 1) == const0_rtx)
4703 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4704 XEXP (SET_SRC (x), 0),
4705 pos, NULL_RTX, 1, 1, 0, 0);
4706 if (extraction != 0)
4708 SUBST (SET_SRC (x), extraction);
4709 return find_split_point (loc, insn, false);
4712 break;
4714 case NE:
4715 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4716 is known to be on, this can be converted into a NEG of a shift. */
4717 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4718 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4719 && 1 <= (pos = exact_log2
4720 (nonzero_bits (XEXP (SET_SRC (x), 0),
4721 GET_MODE (XEXP (SET_SRC (x), 0))))))
4723 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4725 SUBST (SET_SRC (x),
4726 gen_rtx_NEG (mode,
4727 gen_rtx_LSHIFTRT (mode,
4728 XEXP (SET_SRC (x), 0),
4729 GEN_INT (pos))));
4731 split = find_split_point (&SET_SRC (x), insn, true);
4732 if (split && split != &SET_SRC (x))
4733 return split;
4735 break;
4737 case SIGN_EXTEND:
4738 inner = XEXP (SET_SRC (x), 0);
4740 /* We can't optimize if either mode is a partial integer
4741 mode as we don't know how many bits are significant
4742 in those modes. */
4743 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4744 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4745 break;
4747 pos = 0;
4748 len = GET_MODE_PRECISION (GET_MODE (inner));
4749 unsignedp = 0;
4750 break;
4752 case SIGN_EXTRACT:
4753 case ZERO_EXTRACT:
4754 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4755 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4757 inner = XEXP (SET_SRC (x), 0);
4758 len = INTVAL (XEXP (SET_SRC (x), 1));
4759 pos = INTVAL (XEXP (SET_SRC (x), 2));
4761 if (BITS_BIG_ENDIAN)
4762 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4763 unsignedp = (code == ZERO_EXTRACT);
4765 break;
4767 default:
4768 break;
4771 if (len && pos >= 0
4772 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4774 enum machine_mode mode = GET_MODE (SET_SRC (x));
4776 /* For unsigned, we have a choice of a shift followed by an
4777 AND or two shifts. Use two shifts for field sizes where the
4778 constant might be too large. We assume here that we can
4779 always at least get 8-bit constants in an AND insn, which is
4780 true for every current RISC. */
4782 if (unsignedp && len <= 8)
4784 unsigned HOST_WIDE_INT mask
4785 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4786 SUBST (SET_SRC (x),
4787 gen_rtx_AND (mode,
4788 gen_rtx_LSHIFTRT
4789 (mode, gen_lowpart (mode, inner),
4790 GEN_INT (pos)),
4791 gen_int_mode (mask, mode)));
4793 split = find_split_point (&SET_SRC (x), insn, true);
4794 if (split && split != &SET_SRC (x))
4795 return split;
4797 else
4799 SUBST (SET_SRC (x),
4800 gen_rtx_fmt_ee
4801 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4802 gen_rtx_ASHIFT (mode,
4803 gen_lowpart (mode, inner),
4804 GEN_INT (GET_MODE_PRECISION (mode)
4805 - len - pos)),
4806 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4808 split = find_split_point (&SET_SRC (x), insn, true);
4809 if (split && split != &SET_SRC (x))
4810 return split;
4814 /* See if this is a simple operation with a constant as the second
4815 operand. It might be that this constant is out of range and hence
4816 could be used as a split point. */
4817 if (BINARY_P (SET_SRC (x))
4818 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4819 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4820 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4821 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4822 return &XEXP (SET_SRC (x), 1);
4824 /* Finally, see if this is a simple operation with its first operand
4825 not in a register. The operation might require this operand in a
4826 register, so return it as a split point. We can always do this
4827 because if the first operand were another operation, we would have
4828 already found it as a split point. */
4829 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4830 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4831 return &XEXP (SET_SRC (x), 0);
4833 return 0;
4835 case AND:
4836 case IOR:
4837 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4838 it is better to write this as (not (ior A B)) so we can split it.
4839 Similarly for IOR. */
4840 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4842 SUBST (*loc,
4843 gen_rtx_NOT (GET_MODE (x),
4844 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4845 GET_MODE (x),
4846 XEXP (XEXP (x, 0), 0),
4847 XEXP (XEXP (x, 1), 0))));
4848 return find_split_point (loc, insn, set_src);
4851 /* Many RISC machines have a large set of logical insns. If the
4852 second operand is a NOT, put it first so we will try to split the
4853 other operand first. */
4854 if (GET_CODE (XEXP (x, 1)) == NOT)
4856 rtx tem = XEXP (x, 0);
4857 SUBST (XEXP (x, 0), XEXP (x, 1));
4858 SUBST (XEXP (x, 1), tem);
4860 break;
4862 case PLUS:
4863 case MINUS:
4864 /* Canonicalization can produce (minus A (mult B C)), where C is a
4865 constant. It may be better to try splitting (plus (mult B -C) A)
4866 instead if this isn't a multiply by a power of two. */
4867 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4868 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4869 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4871 enum machine_mode mode = GET_MODE (x);
4872 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4873 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4874 SUBST (*loc, gen_rtx_PLUS (mode,
4875 gen_rtx_MULT (mode,
4876 XEXP (XEXP (x, 1), 0),
4877 gen_int_mode (other_int,
4878 mode)),
4879 XEXP (x, 0)));
4880 return find_split_point (loc, insn, set_src);
4883 /* Split at a multiply-accumulate instruction. However if this is
4884 the SET_SRC, we likely do not have such an instruction and it's
4885 worthless to try this split. */
4886 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4887 return loc;
4889 default:
4890 break;
4893 /* Otherwise, select our actions depending on our rtx class. */
4894 switch (GET_RTX_CLASS (code))
4896 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4897 case RTX_TERNARY:
4898 split = find_split_point (&XEXP (x, 2), insn, false);
4899 if (split)
4900 return split;
4901 /* ... fall through ... */
4902 case RTX_BIN_ARITH:
4903 case RTX_COMM_ARITH:
4904 case RTX_COMPARE:
4905 case RTX_COMM_COMPARE:
4906 split = find_split_point (&XEXP (x, 1), insn, false);
4907 if (split)
4908 return split;
4909 /* ... fall through ... */
4910 case RTX_UNARY:
4911 /* Some machines have (and (shift ...) ...) insns. If X is not
4912 an AND, but XEXP (X, 0) is, use it as our split point. */
4913 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4914 return &XEXP (x, 0);
4916 split = find_split_point (&XEXP (x, 0), insn, false);
4917 if (split)
4918 return split;
4919 return loc;
4921 default:
4922 /* Otherwise, we don't have a split point. */
4923 return 0;
4927 /* Throughout X, replace FROM with TO, and return the result.
4928 The result is TO if X is FROM;
4929 otherwise the result is X, but its contents may have been modified.
4930 If they were modified, a record was made in undobuf so that
4931 undo_all will (among other things) return X to its original state.
4933 If the number of changes necessary is too much to record to undo,
4934 the excess changes are not made, so the result is invalid.
4935 The changes already made can still be undone.
4936 undobuf.num_undo is incremented for such changes, so by testing that
4937 the caller can tell whether the result is valid.
4939 `n_occurrences' is incremented each time FROM is replaced.
4941 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4943 IN_COND is nonzero if we are at the top level of a condition.
4945 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4946 by copying if `n_occurrences' is nonzero. */
4948 static rtx
4949 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4951 enum rtx_code code = GET_CODE (x);
4952 enum machine_mode op0_mode = VOIDmode;
4953 const char *fmt;
4954 int len, i;
4955 rtx new_rtx;
4957 /* Two expressions are equal if they are identical copies of a shared
4958 RTX or if they are both registers with the same register number
4959 and mode. */
4961 #define COMBINE_RTX_EQUAL_P(X,Y) \
4962 ((X) == (Y) \
4963 || (REG_P (X) && REG_P (Y) \
4964 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4966 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4968 n_occurrences++;
4969 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4972 /* If X and FROM are the same register but different modes, they
4973 will not have been seen as equal above. However, the log links code
4974 will make a LOG_LINKS entry for that case. If we do nothing, we
4975 will try to rerecognize our original insn and, when it succeeds,
4976 we will delete the feeding insn, which is incorrect.
4978 So force this insn not to match in this (rare) case. */
4979 if (! in_dest && code == REG && REG_P (from)
4980 && reg_overlap_mentioned_p (x, from))
4981 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4983 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4984 of which may contain things that can be combined. */
4985 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4986 return x;
4988 /* It is possible to have a subexpression appear twice in the insn.
4989 Suppose that FROM is a register that appears within TO.
4990 Then, after that subexpression has been scanned once by `subst',
4991 the second time it is scanned, TO may be found. If we were
4992 to scan TO here, we would find FROM within it and create a
4993 self-referent rtl structure which is completely wrong. */
4994 if (COMBINE_RTX_EQUAL_P (x, to))
4995 return to;
4997 /* Parallel asm_operands need special attention because all of the
4998 inputs are shared across the arms. Furthermore, unsharing the
4999 rtl results in recognition failures. Failure to handle this case
5000 specially can result in circular rtl.
5002 Solve this by doing a normal pass across the first entry of the
5003 parallel, and only processing the SET_DESTs of the subsequent
5004 entries. Ug. */
5006 if (code == PARALLEL
5007 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5008 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5010 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5012 /* If this substitution failed, this whole thing fails. */
5013 if (GET_CODE (new_rtx) == CLOBBER
5014 && XEXP (new_rtx, 0) == const0_rtx)
5015 return new_rtx;
5017 SUBST (XVECEXP (x, 0, 0), new_rtx);
5019 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5021 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5023 if (!REG_P (dest)
5024 && GET_CODE (dest) != CC0
5025 && GET_CODE (dest) != PC)
5027 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5029 /* If this substitution failed, this whole thing fails. */
5030 if (GET_CODE (new_rtx) == CLOBBER
5031 && XEXP (new_rtx, 0) == const0_rtx)
5032 return new_rtx;
5034 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5038 else
5040 len = GET_RTX_LENGTH (code);
5041 fmt = GET_RTX_FORMAT (code);
5043 /* We don't need to process a SET_DEST that is a register, CC0,
5044 or PC, so set up to skip this common case. All other cases
5045 where we want to suppress replacing something inside a
5046 SET_SRC are handled via the IN_DEST operand. */
5047 if (code == SET
5048 && (REG_P (SET_DEST (x))
5049 || GET_CODE (SET_DEST (x)) == CC0
5050 || GET_CODE (SET_DEST (x)) == PC))
5051 fmt = "ie";
5053 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5054 constant. */
5055 if (fmt[0] == 'e')
5056 op0_mode = GET_MODE (XEXP (x, 0));
5058 for (i = 0; i < len; i++)
5060 if (fmt[i] == 'E')
5062 int j;
5063 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5065 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5067 new_rtx = (unique_copy && n_occurrences
5068 ? copy_rtx (to) : to);
5069 n_occurrences++;
5071 else
5073 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5074 unique_copy);
5076 /* If this substitution failed, this whole thing
5077 fails. */
5078 if (GET_CODE (new_rtx) == CLOBBER
5079 && XEXP (new_rtx, 0) == const0_rtx)
5080 return new_rtx;
5083 SUBST (XVECEXP (x, i, j), new_rtx);
5086 else if (fmt[i] == 'e')
5088 /* If this is a register being set, ignore it. */
5089 new_rtx = XEXP (x, i);
5090 if (in_dest
5091 && i == 0
5092 && (((code == SUBREG || code == ZERO_EXTRACT)
5093 && REG_P (new_rtx))
5094 || code == STRICT_LOW_PART))
5097 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5099 /* In general, don't install a subreg involving two
5100 modes not tieable. It can worsen register
5101 allocation, and can even make invalid reload
5102 insns, since the reg inside may need to be copied
5103 from in the outside mode, and that may be invalid
5104 if it is an fp reg copied in integer mode.
5106 We allow two exceptions to this: It is valid if
5107 it is inside another SUBREG and the mode of that
5108 SUBREG and the mode of the inside of TO is
5109 tieable and it is valid if X is a SET that copies
5110 FROM to CC0. */
5112 if (GET_CODE (to) == SUBREG
5113 && ! MODES_TIEABLE_P (GET_MODE (to),
5114 GET_MODE (SUBREG_REG (to)))
5115 && ! (code == SUBREG
5116 && MODES_TIEABLE_P (GET_MODE (x),
5117 GET_MODE (SUBREG_REG (to))))
5118 #ifdef HAVE_cc0
5119 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5120 #endif
5122 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5124 #ifdef CANNOT_CHANGE_MODE_CLASS
5125 if (code == SUBREG
5126 && REG_P (to)
5127 && REGNO (to) < FIRST_PSEUDO_REGISTER
5128 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5129 GET_MODE (to),
5130 GET_MODE (x)))
5131 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5132 #endif
5134 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5135 n_occurrences++;
5137 else
5138 /* If we are in a SET_DEST, suppress most cases unless we
5139 have gone inside a MEM, in which case we want to
5140 simplify the address. We assume here that things that
5141 are actually part of the destination have their inner
5142 parts in the first expression. This is true for SUBREG,
5143 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5144 things aside from REG and MEM that should appear in a
5145 SET_DEST. */
5146 new_rtx = subst (XEXP (x, i), from, to,
5147 (((in_dest
5148 && (code == SUBREG || code == STRICT_LOW_PART
5149 || code == ZERO_EXTRACT))
5150 || code == SET)
5151 && i == 0),
5152 code == IF_THEN_ELSE && i == 0,
5153 unique_copy);
5155 /* If we found that we will have to reject this combination,
5156 indicate that by returning the CLOBBER ourselves, rather than
5157 an expression containing it. This will speed things up as
5158 well as prevent accidents where two CLOBBERs are considered
5159 to be equal, thus producing an incorrect simplification. */
5161 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5162 return new_rtx;
5164 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5166 enum machine_mode mode = GET_MODE (x);
5168 x = simplify_subreg (GET_MODE (x), new_rtx,
5169 GET_MODE (SUBREG_REG (x)),
5170 SUBREG_BYTE (x));
5171 if (! x)
5172 x = gen_rtx_CLOBBER (mode, const0_rtx);
5174 else if (CONST_SCALAR_INT_P (new_rtx)
5175 && GET_CODE (x) == ZERO_EXTEND)
5177 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5178 new_rtx, GET_MODE (XEXP (x, 0)));
5179 gcc_assert (x);
5181 else
5182 SUBST (XEXP (x, i), new_rtx);
5187 /* Check if we are loading something from the constant pool via float
5188 extension; in this case we would undo compress_float_constant
5189 optimization and degenerate constant load to an immediate value. */
5190 if (GET_CODE (x) == FLOAT_EXTEND
5191 && MEM_P (XEXP (x, 0))
5192 && MEM_READONLY_P (XEXP (x, 0)))
5194 rtx tmp = avoid_constant_pool_reference (x);
5195 if (x != tmp)
5196 return x;
5199 /* Try to simplify X. If the simplification changed the code, it is likely
5200 that further simplification will help, so loop, but limit the number
5201 of repetitions that will be performed. */
5203 for (i = 0; i < 4; i++)
5205 /* If X is sufficiently simple, don't bother trying to do anything
5206 with it. */
5207 if (code != CONST_INT && code != REG && code != CLOBBER)
5208 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5210 if (GET_CODE (x) == code)
5211 break;
5213 code = GET_CODE (x);
5215 /* We no longer know the original mode of operand 0 since we
5216 have changed the form of X) */
5217 op0_mode = VOIDmode;
5220 return x;
5223 /* Simplify X, a piece of RTL. We just operate on the expression at the
5224 outer level; call `subst' to simplify recursively. Return the new
5225 expression.
5227 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5228 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5229 of a condition. */
5231 static rtx
5232 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5233 int in_cond)
5235 enum rtx_code code = GET_CODE (x);
5236 enum machine_mode mode = GET_MODE (x);
5237 rtx temp;
5238 int i;
5240 /* If this is a commutative operation, put a constant last and a complex
5241 expression first. We don't need to do this for comparisons here. */
5242 if (COMMUTATIVE_ARITH_P (x)
5243 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5245 temp = XEXP (x, 0);
5246 SUBST (XEXP (x, 0), XEXP (x, 1));
5247 SUBST (XEXP (x, 1), temp);
5250 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5251 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5252 things. Check for cases where both arms are testing the same
5253 condition.
5255 Don't do anything if all operands are very simple. */
5257 if ((BINARY_P (x)
5258 && ((!OBJECT_P (XEXP (x, 0))
5259 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5260 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5261 || (!OBJECT_P (XEXP (x, 1))
5262 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5263 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5264 || (UNARY_P (x)
5265 && (!OBJECT_P (XEXP (x, 0))
5266 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5267 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5269 rtx cond, true_rtx, false_rtx;
5271 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5272 if (cond != 0
5273 /* If everything is a comparison, what we have is highly unlikely
5274 to be simpler, so don't use it. */
5275 && ! (COMPARISON_P (x)
5276 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5278 rtx cop1 = const0_rtx;
5279 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5281 if (cond_code == NE && COMPARISON_P (cond))
5282 return x;
5284 /* Simplify the alternative arms; this may collapse the true and
5285 false arms to store-flag values. Be careful to use copy_rtx
5286 here since true_rtx or false_rtx might share RTL with x as a
5287 result of the if_then_else_cond call above. */
5288 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5289 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5291 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5292 is unlikely to be simpler. */
5293 if (general_operand (true_rtx, VOIDmode)
5294 && general_operand (false_rtx, VOIDmode))
5296 enum rtx_code reversed;
5298 /* Restarting if we generate a store-flag expression will cause
5299 us to loop. Just drop through in this case. */
5301 /* If the result values are STORE_FLAG_VALUE and zero, we can
5302 just make the comparison operation. */
5303 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5304 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5305 cond, cop1);
5306 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5307 && ((reversed = reversed_comparison_code_parts
5308 (cond_code, cond, cop1, NULL))
5309 != UNKNOWN))
5310 x = simplify_gen_relational (reversed, mode, VOIDmode,
5311 cond, cop1);
5313 /* Likewise, we can make the negate of a comparison operation
5314 if the result values are - STORE_FLAG_VALUE and zero. */
5315 else if (CONST_INT_P (true_rtx)
5316 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5317 && false_rtx == const0_rtx)
5318 x = simplify_gen_unary (NEG, mode,
5319 simplify_gen_relational (cond_code,
5320 mode, VOIDmode,
5321 cond, cop1),
5322 mode);
5323 else if (CONST_INT_P (false_rtx)
5324 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5325 && true_rtx == const0_rtx
5326 && ((reversed = reversed_comparison_code_parts
5327 (cond_code, cond, cop1, NULL))
5328 != UNKNOWN))
5329 x = simplify_gen_unary (NEG, mode,
5330 simplify_gen_relational (reversed,
5331 mode, VOIDmode,
5332 cond, cop1),
5333 mode);
5334 else
5335 return gen_rtx_IF_THEN_ELSE (mode,
5336 simplify_gen_relational (cond_code,
5337 mode,
5338 VOIDmode,
5339 cond,
5340 cop1),
5341 true_rtx, false_rtx);
5343 code = GET_CODE (x);
5344 op0_mode = VOIDmode;
5349 /* Try to fold this expression in case we have constants that weren't
5350 present before. */
5351 temp = 0;
5352 switch (GET_RTX_CLASS (code))
5354 case RTX_UNARY:
5355 if (op0_mode == VOIDmode)
5356 op0_mode = GET_MODE (XEXP (x, 0));
5357 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5358 break;
5359 case RTX_COMPARE:
5360 case RTX_COMM_COMPARE:
5362 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5363 if (cmp_mode == VOIDmode)
5365 cmp_mode = GET_MODE (XEXP (x, 1));
5366 if (cmp_mode == VOIDmode)
5367 cmp_mode = op0_mode;
5369 temp = simplify_relational_operation (code, mode, cmp_mode,
5370 XEXP (x, 0), XEXP (x, 1));
5372 break;
5373 case RTX_COMM_ARITH:
5374 case RTX_BIN_ARITH:
5375 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5376 break;
5377 case RTX_BITFIELD_OPS:
5378 case RTX_TERNARY:
5379 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5380 XEXP (x, 1), XEXP (x, 2));
5381 break;
5382 default:
5383 break;
5386 if (temp)
5388 x = temp;
5389 code = GET_CODE (temp);
5390 op0_mode = VOIDmode;
5391 mode = GET_MODE (temp);
5394 /* First see if we can apply the inverse distributive law. */
5395 if (code == PLUS || code == MINUS
5396 || code == AND || code == IOR || code == XOR)
5398 x = apply_distributive_law (x);
5399 code = GET_CODE (x);
5400 op0_mode = VOIDmode;
5403 /* If CODE is an associative operation not otherwise handled, see if we
5404 can associate some operands. This can win if they are constants or
5405 if they are logically related (i.e. (a & b) & a). */
5406 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5407 || code == AND || code == IOR || code == XOR
5408 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5409 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5410 || (flag_associative_math && FLOAT_MODE_P (mode))))
5412 if (GET_CODE (XEXP (x, 0)) == code)
5414 rtx other = XEXP (XEXP (x, 0), 0);
5415 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5416 rtx inner_op1 = XEXP (x, 1);
5417 rtx inner;
5419 /* Make sure we pass the constant operand if any as the second
5420 one if this is a commutative operation. */
5421 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5423 rtx tem = inner_op0;
5424 inner_op0 = inner_op1;
5425 inner_op1 = tem;
5427 inner = simplify_binary_operation (code == MINUS ? PLUS
5428 : code == DIV ? MULT
5429 : code,
5430 mode, inner_op0, inner_op1);
5432 /* For commutative operations, try the other pair if that one
5433 didn't simplify. */
5434 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5436 other = XEXP (XEXP (x, 0), 1);
5437 inner = simplify_binary_operation (code, mode,
5438 XEXP (XEXP (x, 0), 0),
5439 XEXP (x, 1));
5442 if (inner)
5443 return simplify_gen_binary (code, mode, other, inner);
5447 /* A little bit of algebraic simplification here. */
5448 switch (code)
5450 case MEM:
5451 /* Ensure that our address has any ASHIFTs converted to MULT in case
5452 address-recognizing predicates are called later. */
5453 temp = make_compound_operation (XEXP (x, 0), MEM);
5454 SUBST (XEXP (x, 0), temp);
5455 break;
5457 case SUBREG:
5458 if (op0_mode == VOIDmode)
5459 op0_mode = GET_MODE (SUBREG_REG (x));
5461 /* See if this can be moved to simplify_subreg. */
5462 if (CONSTANT_P (SUBREG_REG (x))
5463 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5464 /* Don't call gen_lowpart if the inner mode
5465 is VOIDmode and we cannot simplify it, as SUBREG without
5466 inner mode is invalid. */
5467 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5468 || gen_lowpart_common (mode, SUBREG_REG (x))))
5469 return gen_lowpart (mode, SUBREG_REG (x));
5471 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5472 break;
5474 rtx temp;
5475 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5476 SUBREG_BYTE (x));
5477 if (temp)
5478 return temp;
5480 /* If op is known to have all lower bits zero, the result is zero. */
5481 if (!in_dest
5482 && SCALAR_INT_MODE_P (mode)
5483 && SCALAR_INT_MODE_P (op0_mode)
5484 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5485 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5486 && HWI_COMPUTABLE_MODE_P (op0_mode)
5487 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5488 & GET_MODE_MASK (mode)) == 0)
5489 return CONST0_RTX (mode);
5492 /* Don't change the mode of the MEM if that would change the meaning
5493 of the address. */
5494 if (MEM_P (SUBREG_REG (x))
5495 && (MEM_VOLATILE_P (SUBREG_REG (x))
5496 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5497 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5498 return gen_rtx_CLOBBER (mode, const0_rtx);
5500 /* Note that we cannot do any narrowing for non-constants since
5501 we might have been counting on using the fact that some bits were
5502 zero. We now do this in the SET. */
5504 break;
5506 case NEG:
5507 temp = expand_compound_operation (XEXP (x, 0));
5509 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5510 replaced by (lshiftrt X C). This will convert
5511 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5513 if (GET_CODE (temp) == ASHIFTRT
5514 && CONST_INT_P (XEXP (temp, 1))
5515 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5516 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5517 INTVAL (XEXP (temp, 1)));
5519 /* If X has only a single bit that might be nonzero, say, bit I, convert
5520 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5521 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5522 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5523 or a SUBREG of one since we'd be making the expression more
5524 complex if it was just a register. */
5526 if (!REG_P (temp)
5527 && ! (GET_CODE (temp) == SUBREG
5528 && REG_P (SUBREG_REG (temp)))
5529 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5531 rtx temp1 = simplify_shift_const
5532 (NULL_RTX, ASHIFTRT, mode,
5533 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5534 GET_MODE_PRECISION (mode) - 1 - i),
5535 GET_MODE_PRECISION (mode) - 1 - i);
5537 /* If all we did was surround TEMP with the two shifts, we
5538 haven't improved anything, so don't use it. Otherwise,
5539 we are better off with TEMP1. */
5540 if (GET_CODE (temp1) != ASHIFTRT
5541 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5542 || XEXP (XEXP (temp1, 0), 0) != temp)
5543 return temp1;
5545 break;
5547 case TRUNCATE:
5548 /* We can't handle truncation to a partial integer mode here
5549 because we don't know the real bitsize of the partial
5550 integer mode. */
5551 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5552 break;
5554 if (HWI_COMPUTABLE_MODE_P (mode))
5555 SUBST (XEXP (x, 0),
5556 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5557 GET_MODE_MASK (mode), 0));
5559 /* We can truncate a constant value and return it. */
5560 if (CONST_INT_P (XEXP (x, 0)))
5561 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5563 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5564 whose value is a comparison can be replaced with a subreg if
5565 STORE_FLAG_VALUE permits. */
5566 if (HWI_COMPUTABLE_MODE_P (mode)
5567 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5568 && (temp = get_last_value (XEXP (x, 0)))
5569 && COMPARISON_P (temp))
5570 return gen_lowpart (mode, XEXP (x, 0));
5571 break;
5573 case CONST:
5574 /* (const (const X)) can become (const X). Do it this way rather than
5575 returning the inner CONST since CONST can be shared with a
5576 REG_EQUAL note. */
5577 if (GET_CODE (XEXP (x, 0)) == CONST)
5578 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5579 break;
5581 #ifdef HAVE_lo_sum
5582 case LO_SUM:
5583 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5584 can add in an offset. find_split_point will split this address up
5585 again if it doesn't match. */
5586 if (GET_CODE (XEXP (x, 0)) == HIGH
5587 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5588 return XEXP (x, 1);
5589 break;
5590 #endif
5592 case PLUS:
5593 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5594 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5595 bit-field and can be replaced by either a sign_extend or a
5596 sign_extract. The `and' may be a zero_extend and the two
5597 <c>, -<c> constants may be reversed. */
5598 if (GET_CODE (XEXP (x, 0)) == XOR
5599 && CONST_INT_P (XEXP (x, 1))
5600 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5601 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5602 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5603 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5604 && HWI_COMPUTABLE_MODE_P (mode)
5605 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5606 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5607 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5608 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5609 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5610 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5611 == (unsigned int) i + 1))))
5612 return simplify_shift_const
5613 (NULL_RTX, ASHIFTRT, mode,
5614 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5615 XEXP (XEXP (XEXP (x, 0), 0), 0),
5616 GET_MODE_PRECISION (mode) - (i + 1)),
5617 GET_MODE_PRECISION (mode) - (i + 1));
5619 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5620 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5621 the bitsize of the mode - 1. This allows simplification of
5622 "a = (b & 8) == 0;" */
5623 if (XEXP (x, 1) == constm1_rtx
5624 && !REG_P (XEXP (x, 0))
5625 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5626 && REG_P (SUBREG_REG (XEXP (x, 0))))
5627 && nonzero_bits (XEXP (x, 0), mode) == 1)
5628 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5629 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5630 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5631 GET_MODE_PRECISION (mode) - 1),
5632 GET_MODE_PRECISION (mode) - 1);
5634 /* If we are adding two things that have no bits in common, convert
5635 the addition into an IOR. This will often be further simplified,
5636 for example in cases like ((a & 1) + (a & 2)), which can
5637 become a & 3. */
5639 if (HWI_COMPUTABLE_MODE_P (mode)
5640 && (nonzero_bits (XEXP (x, 0), mode)
5641 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5643 /* Try to simplify the expression further. */
5644 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5645 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5647 /* If we could, great. If not, do not go ahead with the IOR
5648 replacement, since PLUS appears in many special purpose
5649 address arithmetic instructions. */
5650 if (GET_CODE (temp) != CLOBBER
5651 && (GET_CODE (temp) != IOR
5652 || ((XEXP (temp, 0) != XEXP (x, 0)
5653 || XEXP (temp, 1) != XEXP (x, 1))
5654 && (XEXP (temp, 0) != XEXP (x, 1)
5655 || XEXP (temp, 1) != XEXP (x, 0)))))
5656 return temp;
5658 break;
5660 case MINUS:
5661 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5662 (and <foo> (const_int pow2-1)) */
5663 if (GET_CODE (XEXP (x, 1)) == AND
5664 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5665 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5666 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5667 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5668 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5669 break;
5671 case MULT:
5672 /* If we have (mult (plus A B) C), apply the distributive law and then
5673 the inverse distributive law to see if things simplify. This
5674 occurs mostly in addresses, often when unrolling loops. */
5676 if (GET_CODE (XEXP (x, 0)) == PLUS)
5678 rtx result = distribute_and_simplify_rtx (x, 0);
5679 if (result)
5680 return result;
5683 /* Try simplify a*(b/c) as (a*b)/c. */
5684 if (FLOAT_MODE_P (mode) && flag_associative_math
5685 && GET_CODE (XEXP (x, 0)) == DIV)
5687 rtx tem = simplify_binary_operation (MULT, mode,
5688 XEXP (XEXP (x, 0), 0),
5689 XEXP (x, 1));
5690 if (tem)
5691 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5693 break;
5695 case UDIV:
5696 /* If this is a divide by a power of two, treat it as a shift if
5697 its first operand is a shift. */
5698 if (CONST_INT_P (XEXP (x, 1))
5699 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5700 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5701 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5702 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5703 || GET_CODE (XEXP (x, 0)) == ROTATE
5704 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5705 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5706 break;
5708 case EQ: case NE:
5709 case GT: case GTU: case GE: case GEU:
5710 case LT: case LTU: case LE: case LEU:
5711 case UNEQ: case LTGT:
5712 case UNGT: case UNGE:
5713 case UNLT: case UNLE:
5714 case UNORDERED: case ORDERED:
5715 /* If the first operand is a condition code, we can't do anything
5716 with it. */
5717 if (GET_CODE (XEXP (x, 0)) == COMPARE
5718 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5719 && ! CC0_P (XEXP (x, 0))))
5721 rtx op0 = XEXP (x, 0);
5722 rtx op1 = XEXP (x, 1);
5723 enum rtx_code new_code;
5725 if (GET_CODE (op0) == COMPARE)
5726 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5728 /* Simplify our comparison, if possible. */
5729 new_code = simplify_comparison (code, &op0, &op1);
5731 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5732 if only the low-order bit is possibly nonzero in X (such as when
5733 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5734 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5735 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5736 (plus X 1).
5738 Remove any ZERO_EXTRACT we made when thinking this was a
5739 comparison. It may now be simpler to use, e.g., an AND. If a
5740 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5741 the call to make_compound_operation in the SET case.
5743 Don't apply these optimizations if the caller would
5744 prefer a comparison rather than a value.
5745 E.g., for the condition in an IF_THEN_ELSE most targets need
5746 an explicit comparison. */
5748 if (in_cond)
5751 else if (STORE_FLAG_VALUE == 1
5752 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5753 && op1 == const0_rtx
5754 && mode == GET_MODE (op0)
5755 && nonzero_bits (op0, mode) == 1)
5756 return gen_lowpart (mode,
5757 expand_compound_operation (op0));
5759 else if (STORE_FLAG_VALUE == 1
5760 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5761 && op1 == const0_rtx
5762 && mode == GET_MODE (op0)
5763 && (num_sign_bit_copies (op0, mode)
5764 == GET_MODE_PRECISION (mode)))
5766 op0 = expand_compound_operation (op0);
5767 return simplify_gen_unary (NEG, mode,
5768 gen_lowpart (mode, op0),
5769 mode);
5772 else if (STORE_FLAG_VALUE == 1
5773 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5774 && op1 == const0_rtx
5775 && mode == GET_MODE (op0)
5776 && nonzero_bits (op0, mode) == 1)
5778 op0 = expand_compound_operation (op0);
5779 return simplify_gen_binary (XOR, mode,
5780 gen_lowpart (mode, op0),
5781 const1_rtx);
5784 else if (STORE_FLAG_VALUE == 1
5785 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5786 && op1 == const0_rtx
5787 && mode == GET_MODE (op0)
5788 && (num_sign_bit_copies (op0, mode)
5789 == GET_MODE_PRECISION (mode)))
5791 op0 = expand_compound_operation (op0);
5792 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5795 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5796 those above. */
5797 if (in_cond)
5800 else if (STORE_FLAG_VALUE == -1
5801 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5802 && op1 == const0_rtx
5803 && (num_sign_bit_copies (op0, mode)
5804 == GET_MODE_PRECISION (mode)))
5805 return gen_lowpart (mode,
5806 expand_compound_operation (op0));
5808 else if (STORE_FLAG_VALUE == -1
5809 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5810 && op1 == const0_rtx
5811 && mode == GET_MODE (op0)
5812 && nonzero_bits (op0, mode) == 1)
5814 op0 = expand_compound_operation (op0);
5815 return simplify_gen_unary (NEG, mode,
5816 gen_lowpart (mode, op0),
5817 mode);
5820 else if (STORE_FLAG_VALUE == -1
5821 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5822 && op1 == const0_rtx
5823 && mode == GET_MODE (op0)
5824 && (num_sign_bit_copies (op0, mode)
5825 == GET_MODE_PRECISION (mode)))
5827 op0 = expand_compound_operation (op0);
5828 return simplify_gen_unary (NOT, mode,
5829 gen_lowpart (mode, op0),
5830 mode);
5833 /* If X is 0/1, (eq X 0) is X-1. */
5834 else if (STORE_FLAG_VALUE == -1
5835 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5836 && op1 == const0_rtx
5837 && mode == GET_MODE (op0)
5838 && nonzero_bits (op0, mode) == 1)
5840 op0 = expand_compound_operation (op0);
5841 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5844 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5845 one bit that might be nonzero, we can convert (ne x 0) to
5846 (ashift x c) where C puts the bit in the sign bit. Remove any
5847 AND with STORE_FLAG_VALUE when we are done, since we are only
5848 going to test the sign bit. */
5849 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5850 && HWI_COMPUTABLE_MODE_P (mode)
5851 && val_signbit_p (mode, STORE_FLAG_VALUE)
5852 && op1 == const0_rtx
5853 && mode == GET_MODE (op0)
5854 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5856 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5857 expand_compound_operation (op0),
5858 GET_MODE_PRECISION (mode) - 1 - i);
5859 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5860 return XEXP (x, 0);
5861 else
5862 return x;
5865 /* If the code changed, return a whole new comparison.
5866 We also need to avoid using SUBST in cases where
5867 simplify_comparison has widened a comparison with a CONST_INT,
5868 since in that case the wider CONST_INT may fail the sanity
5869 checks in do_SUBST. */
5870 if (new_code != code
5871 || (CONST_INT_P (op1)
5872 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
5873 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
5874 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5876 /* Otherwise, keep this operation, but maybe change its operands.
5877 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5878 SUBST (XEXP (x, 0), op0);
5879 SUBST (XEXP (x, 1), op1);
5881 break;
5883 case IF_THEN_ELSE:
5884 return simplify_if_then_else (x);
5886 case ZERO_EXTRACT:
5887 case SIGN_EXTRACT:
5888 case ZERO_EXTEND:
5889 case SIGN_EXTEND:
5890 /* If we are processing SET_DEST, we are done. */
5891 if (in_dest)
5892 return x;
5894 return expand_compound_operation (x);
5896 case SET:
5897 return simplify_set (x);
5899 case AND:
5900 case IOR:
5901 return simplify_logical (x);
5903 case ASHIFT:
5904 case LSHIFTRT:
5905 case ASHIFTRT:
5906 case ROTATE:
5907 case ROTATERT:
5908 /* If this is a shift by a constant amount, simplify it. */
5909 if (CONST_INT_P (XEXP (x, 1)))
5910 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5911 INTVAL (XEXP (x, 1)));
5913 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5914 SUBST (XEXP (x, 1),
5915 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5916 ((unsigned HOST_WIDE_INT) 1
5917 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5918 - 1,
5919 0));
5920 break;
5922 default:
5923 break;
5926 return x;
5929 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5931 static rtx
5932 simplify_if_then_else (rtx x)
5934 enum machine_mode mode = GET_MODE (x);
5935 rtx cond = XEXP (x, 0);
5936 rtx true_rtx = XEXP (x, 1);
5937 rtx false_rtx = XEXP (x, 2);
5938 enum rtx_code true_code = GET_CODE (cond);
5939 int comparison_p = COMPARISON_P (cond);
5940 rtx temp;
5941 int i;
5942 enum rtx_code false_code;
5943 rtx reversed;
5945 /* Simplify storing of the truth value. */
5946 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5947 return simplify_gen_relational (true_code, mode, VOIDmode,
5948 XEXP (cond, 0), XEXP (cond, 1));
5950 /* Also when the truth value has to be reversed. */
5951 if (comparison_p
5952 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5953 && (reversed = reversed_comparison (cond, mode)))
5954 return reversed;
5956 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5957 in it is being compared against certain values. Get the true and false
5958 comparisons and see if that says anything about the value of each arm. */
5960 if (comparison_p
5961 && ((false_code = reversed_comparison_code (cond, NULL))
5962 != UNKNOWN)
5963 && REG_P (XEXP (cond, 0)))
5965 HOST_WIDE_INT nzb;
5966 rtx from = XEXP (cond, 0);
5967 rtx true_val = XEXP (cond, 1);
5968 rtx false_val = true_val;
5969 int swapped = 0;
5971 /* If FALSE_CODE is EQ, swap the codes and arms. */
5973 if (false_code == EQ)
5975 swapped = 1, true_code = EQ, false_code = NE;
5976 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5979 /* If we are comparing against zero and the expression being tested has
5980 only a single bit that might be nonzero, that is its value when it is
5981 not equal to zero. Similarly if it is known to be -1 or 0. */
5983 if (true_code == EQ && true_val == const0_rtx
5984 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5986 false_code = EQ;
5987 false_val = gen_int_mode (nzb, GET_MODE (from));
5989 else if (true_code == EQ && true_val == const0_rtx
5990 && (num_sign_bit_copies (from, GET_MODE (from))
5991 == GET_MODE_PRECISION (GET_MODE (from))))
5993 false_code = EQ;
5994 false_val = constm1_rtx;
5997 /* Now simplify an arm if we know the value of the register in the
5998 branch and it is used in the arm. Be careful due to the potential
5999 of locally-shared RTL. */
6001 if (reg_mentioned_p (from, true_rtx))
6002 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6003 from, true_val),
6004 pc_rtx, pc_rtx, 0, 0, 0);
6005 if (reg_mentioned_p (from, false_rtx))
6006 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6007 from, false_val),
6008 pc_rtx, pc_rtx, 0, 0, 0);
6010 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6011 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6013 true_rtx = XEXP (x, 1);
6014 false_rtx = XEXP (x, 2);
6015 true_code = GET_CODE (cond);
6018 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6019 reversed, do so to avoid needing two sets of patterns for
6020 subtract-and-branch insns. Similarly if we have a constant in the true
6021 arm, the false arm is the same as the first operand of the comparison, or
6022 the false arm is more complicated than the true arm. */
6024 if (comparison_p
6025 && reversed_comparison_code (cond, NULL) != UNKNOWN
6026 && (true_rtx == pc_rtx
6027 || (CONSTANT_P (true_rtx)
6028 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6029 || true_rtx == const0_rtx
6030 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6031 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6032 && !OBJECT_P (false_rtx))
6033 || reg_mentioned_p (true_rtx, false_rtx)
6034 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6036 true_code = reversed_comparison_code (cond, NULL);
6037 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6038 SUBST (XEXP (x, 1), false_rtx);
6039 SUBST (XEXP (x, 2), true_rtx);
6041 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6042 cond = XEXP (x, 0);
6044 /* It is possible that the conditional has been simplified out. */
6045 true_code = GET_CODE (cond);
6046 comparison_p = COMPARISON_P (cond);
6049 /* If the two arms are identical, we don't need the comparison. */
6051 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6052 return true_rtx;
6054 /* Convert a == b ? b : a to "a". */
6055 if (true_code == EQ && ! side_effects_p (cond)
6056 && !HONOR_NANS (mode)
6057 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6058 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6059 return false_rtx;
6060 else if (true_code == NE && ! side_effects_p (cond)
6061 && !HONOR_NANS (mode)
6062 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6063 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6064 return true_rtx;
6066 /* Look for cases where we have (abs x) or (neg (abs X)). */
6068 if (GET_MODE_CLASS (mode) == MODE_INT
6069 && comparison_p
6070 && XEXP (cond, 1) == const0_rtx
6071 && GET_CODE (false_rtx) == NEG
6072 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6073 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6074 && ! side_effects_p (true_rtx))
6075 switch (true_code)
6077 case GT:
6078 case GE:
6079 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6080 case LT:
6081 case LE:
6082 return
6083 simplify_gen_unary (NEG, mode,
6084 simplify_gen_unary (ABS, mode, true_rtx, mode),
6085 mode);
6086 default:
6087 break;
6090 /* Look for MIN or MAX. */
6092 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6093 && comparison_p
6094 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6095 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6096 && ! side_effects_p (cond))
6097 switch (true_code)
6099 case GE:
6100 case GT:
6101 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6102 case LE:
6103 case LT:
6104 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6105 case GEU:
6106 case GTU:
6107 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6108 case LEU:
6109 case LTU:
6110 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6111 default:
6112 break;
6115 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6116 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6117 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6118 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6119 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6120 neither 1 or -1, but it isn't worth checking for. */
6122 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6123 && comparison_p
6124 && GET_MODE_CLASS (mode) == MODE_INT
6125 && ! side_effects_p (x))
6127 rtx t = make_compound_operation (true_rtx, SET);
6128 rtx f = make_compound_operation (false_rtx, SET);
6129 rtx cond_op0 = XEXP (cond, 0);
6130 rtx cond_op1 = XEXP (cond, 1);
6131 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6132 enum machine_mode m = mode;
6133 rtx z = 0, c1 = NULL_RTX;
6135 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6136 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6137 || GET_CODE (t) == ASHIFT
6138 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6139 && rtx_equal_p (XEXP (t, 0), f))
6140 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6142 /* If an identity-zero op is commutative, check whether there
6143 would be a match if we swapped the operands. */
6144 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6145 || GET_CODE (t) == XOR)
6146 && rtx_equal_p (XEXP (t, 1), f))
6147 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6148 else if (GET_CODE (t) == SIGN_EXTEND
6149 && (GET_CODE (XEXP (t, 0)) == PLUS
6150 || GET_CODE (XEXP (t, 0)) == MINUS
6151 || GET_CODE (XEXP (t, 0)) == IOR
6152 || GET_CODE (XEXP (t, 0)) == XOR
6153 || GET_CODE (XEXP (t, 0)) == ASHIFT
6154 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6155 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6156 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6157 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6158 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6159 && (num_sign_bit_copies (f, GET_MODE (f))
6160 > (unsigned int)
6161 (GET_MODE_PRECISION (mode)
6162 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6164 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6165 extend_op = SIGN_EXTEND;
6166 m = GET_MODE (XEXP (t, 0));
6168 else if (GET_CODE (t) == SIGN_EXTEND
6169 && (GET_CODE (XEXP (t, 0)) == PLUS
6170 || GET_CODE (XEXP (t, 0)) == IOR
6171 || GET_CODE (XEXP (t, 0)) == XOR)
6172 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6173 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6174 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6175 && (num_sign_bit_copies (f, GET_MODE (f))
6176 > (unsigned int)
6177 (GET_MODE_PRECISION (mode)
6178 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6180 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6181 extend_op = SIGN_EXTEND;
6182 m = GET_MODE (XEXP (t, 0));
6184 else if (GET_CODE (t) == ZERO_EXTEND
6185 && (GET_CODE (XEXP (t, 0)) == PLUS
6186 || GET_CODE (XEXP (t, 0)) == MINUS
6187 || GET_CODE (XEXP (t, 0)) == IOR
6188 || GET_CODE (XEXP (t, 0)) == XOR
6189 || GET_CODE (XEXP (t, 0)) == ASHIFT
6190 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6191 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6192 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6193 && HWI_COMPUTABLE_MODE_P (mode)
6194 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6195 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6196 && ((nonzero_bits (f, GET_MODE (f))
6197 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6198 == 0))
6200 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6201 extend_op = ZERO_EXTEND;
6202 m = GET_MODE (XEXP (t, 0));
6204 else if (GET_CODE (t) == ZERO_EXTEND
6205 && (GET_CODE (XEXP (t, 0)) == PLUS
6206 || GET_CODE (XEXP (t, 0)) == IOR
6207 || GET_CODE (XEXP (t, 0)) == XOR)
6208 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6209 && HWI_COMPUTABLE_MODE_P (mode)
6210 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6211 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6212 && ((nonzero_bits (f, GET_MODE (f))
6213 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6214 == 0))
6216 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6217 extend_op = ZERO_EXTEND;
6218 m = GET_MODE (XEXP (t, 0));
6221 if (z)
6223 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6224 cond_op0, cond_op1),
6225 pc_rtx, pc_rtx, 0, 0, 0);
6226 temp = simplify_gen_binary (MULT, m, temp,
6227 simplify_gen_binary (MULT, m, c1,
6228 const_true_rtx));
6229 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6230 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6232 if (extend_op != UNKNOWN)
6233 temp = simplify_gen_unary (extend_op, mode, temp, m);
6235 return temp;
6239 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6240 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6241 negation of a single bit, we can convert this operation to a shift. We
6242 can actually do this more generally, but it doesn't seem worth it. */
6244 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6245 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6246 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6247 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6248 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6249 == GET_MODE_PRECISION (mode))
6250 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6251 return
6252 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6253 gen_lowpart (mode, XEXP (cond, 0)), i);
6255 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6256 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6257 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6258 && GET_MODE (XEXP (cond, 0)) == mode
6259 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6260 == nonzero_bits (XEXP (cond, 0), mode)
6261 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6262 return XEXP (cond, 0);
6264 return x;
6267 /* Simplify X, a SET expression. Return the new expression. */
6269 static rtx
6270 simplify_set (rtx x)
6272 rtx src = SET_SRC (x);
6273 rtx dest = SET_DEST (x);
6274 enum machine_mode mode
6275 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6276 rtx_insn *other_insn;
6277 rtx *cc_use;
6279 /* (set (pc) (return)) gets written as (return). */
6280 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6281 return src;
6283 /* Now that we know for sure which bits of SRC we are using, see if we can
6284 simplify the expression for the object knowing that we only need the
6285 low-order bits. */
6287 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6289 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6290 SUBST (SET_SRC (x), src);
6293 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6294 the comparison result and try to simplify it unless we already have used
6295 undobuf.other_insn. */
6296 if ((GET_MODE_CLASS (mode) == MODE_CC
6297 || GET_CODE (src) == COMPARE
6298 || CC0_P (dest))
6299 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6300 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6301 && COMPARISON_P (*cc_use)
6302 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6304 enum rtx_code old_code = GET_CODE (*cc_use);
6305 enum rtx_code new_code;
6306 rtx op0, op1, tmp;
6307 int other_changed = 0;
6308 rtx inner_compare = NULL_RTX;
6309 enum machine_mode compare_mode = GET_MODE (dest);
6311 if (GET_CODE (src) == COMPARE)
6313 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6314 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6316 inner_compare = op0;
6317 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6320 else
6321 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6323 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6324 op0, op1);
6325 if (!tmp)
6326 new_code = old_code;
6327 else if (!CONSTANT_P (tmp))
6329 new_code = GET_CODE (tmp);
6330 op0 = XEXP (tmp, 0);
6331 op1 = XEXP (tmp, 1);
6333 else
6335 rtx pat = PATTERN (other_insn);
6336 undobuf.other_insn = other_insn;
6337 SUBST (*cc_use, tmp);
6339 /* Attempt to simplify CC user. */
6340 if (GET_CODE (pat) == SET)
6342 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6343 if (new_rtx != NULL_RTX)
6344 SUBST (SET_SRC (pat), new_rtx);
6347 /* Convert X into a no-op move. */
6348 SUBST (SET_DEST (x), pc_rtx);
6349 SUBST (SET_SRC (x), pc_rtx);
6350 return x;
6353 /* Simplify our comparison, if possible. */
6354 new_code = simplify_comparison (new_code, &op0, &op1);
6356 #ifdef SELECT_CC_MODE
6357 /* If this machine has CC modes other than CCmode, check to see if we
6358 need to use a different CC mode here. */
6359 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6360 compare_mode = GET_MODE (op0);
6361 else if (inner_compare
6362 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6363 && new_code == old_code
6364 && op0 == XEXP (inner_compare, 0)
6365 && op1 == XEXP (inner_compare, 1))
6366 compare_mode = GET_MODE (inner_compare);
6367 else
6368 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6370 #ifndef HAVE_cc0
6371 /* If the mode changed, we have to change SET_DEST, the mode in the
6372 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6373 a hard register, just build new versions with the proper mode. If it
6374 is a pseudo, we lose unless it is only time we set the pseudo, in
6375 which case we can safely change its mode. */
6376 if (compare_mode != GET_MODE (dest))
6378 if (can_change_dest_mode (dest, 0, compare_mode))
6380 unsigned int regno = REGNO (dest);
6381 rtx new_dest;
6383 if (regno < FIRST_PSEUDO_REGISTER)
6384 new_dest = gen_rtx_REG (compare_mode, regno);
6385 else
6387 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6388 new_dest = regno_reg_rtx[regno];
6391 SUBST (SET_DEST (x), new_dest);
6392 SUBST (XEXP (*cc_use, 0), new_dest);
6393 other_changed = 1;
6395 dest = new_dest;
6398 #endif /* cc0 */
6399 #endif /* SELECT_CC_MODE */
6401 /* If the code changed, we have to build a new comparison in
6402 undobuf.other_insn. */
6403 if (new_code != old_code)
6405 int other_changed_previously = other_changed;
6406 unsigned HOST_WIDE_INT mask;
6407 rtx old_cc_use = *cc_use;
6409 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6410 dest, const0_rtx));
6411 other_changed = 1;
6413 /* If the only change we made was to change an EQ into an NE or
6414 vice versa, OP0 has only one bit that might be nonzero, and OP1
6415 is zero, check if changing the user of the condition code will
6416 produce a valid insn. If it won't, we can keep the original code
6417 in that insn by surrounding our operation with an XOR. */
6419 if (((old_code == NE && new_code == EQ)
6420 || (old_code == EQ && new_code == NE))
6421 && ! other_changed_previously && op1 == const0_rtx
6422 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6423 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6425 rtx pat = PATTERN (other_insn), note = 0;
6427 if ((recog_for_combine (&pat, other_insn, &note) < 0
6428 && ! check_asm_operands (pat)))
6430 *cc_use = old_cc_use;
6431 other_changed = 0;
6433 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6434 gen_int_mode (mask,
6435 GET_MODE (op0)));
6440 if (other_changed)
6441 undobuf.other_insn = other_insn;
6443 /* Otherwise, if we didn't previously have a COMPARE in the
6444 correct mode, we need one. */
6445 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6447 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6448 src = SET_SRC (x);
6450 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6452 SUBST (SET_SRC (x), op0);
6453 src = SET_SRC (x);
6455 /* Otherwise, update the COMPARE if needed. */
6456 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6458 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6459 src = SET_SRC (x);
6462 else
6464 /* Get SET_SRC in a form where we have placed back any
6465 compound expressions. Then do the checks below. */
6466 src = make_compound_operation (src, SET);
6467 SUBST (SET_SRC (x), src);
6470 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6471 and X being a REG or (subreg (reg)), we may be able to convert this to
6472 (set (subreg:m2 x) (op)).
6474 We can always do this if M1 is narrower than M2 because that means that
6475 we only care about the low bits of the result.
6477 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6478 perform a narrower operation than requested since the high-order bits will
6479 be undefined. On machine where it is defined, this transformation is safe
6480 as long as M1 and M2 have the same number of words. */
6482 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6483 && !OBJECT_P (SUBREG_REG (src))
6484 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6485 / UNITS_PER_WORD)
6486 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6487 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6488 #ifndef WORD_REGISTER_OPERATIONS
6489 && (GET_MODE_SIZE (GET_MODE (src))
6490 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6491 #endif
6492 #ifdef CANNOT_CHANGE_MODE_CLASS
6493 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6494 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6495 GET_MODE (SUBREG_REG (src)),
6496 GET_MODE (src)))
6497 #endif
6498 && (REG_P (dest)
6499 || (GET_CODE (dest) == SUBREG
6500 && REG_P (SUBREG_REG (dest)))))
6502 SUBST (SET_DEST (x),
6503 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6504 dest));
6505 SUBST (SET_SRC (x), SUBREG_REG (src));
6507 src = SET_SRC (x), dest = SET_DEST (x);
6510 #ifdef HAVE_cc0
6511 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6512 in SRC. */
6513 if (dest == cc0_rtx
6514 && GET_CODE (src) == SUBREG
6515 && subreg_lowpart_p (src)
6516 && (GET_MODE_PRECISION (GET_MODE (src))
6517 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6519 rtx inner = SUBREG_REG (src);
6520 enum machine_mode inner_mode = GET_MODE (inner);
6522 /* Here we make sure that we don't have a sign bit on. */
6523 if (val_signbit_known_clear_p (GET_MODE (src),
6524 nonzero_bits (inner, inner_mode)))
6526 SUBST (SET_SRC (x), inner);
6527 src = SET_SRC (x);
6530 #endif
6532 #ifdef LOAD_EXTEND_OP
6533 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6534 would require a paradoxical subreg. Replace the subreg with a
6535 zero_extend to avoid the reload that would otherwise be required. */
6537 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6538 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6539 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6540 && SUBREG_BYTE (src) == 0
6541 && paradoxical_subreg_p (src)
6542 && MEM_P (SUBREG_REG (src)))
6544 SUBST (SET_SRC (x),
6545 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6546 GET_MODE (src), SUBREG_REG (src)));
6548 src = SET_SRC (x);
6550 #endif
6552 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6553 are comparing an item known to be 0 or -1 against 0, use a logical
6554 operation instead. Check for one of the arms being an IOR of the other
6555 arm with some value. We compute three terms to be IOR'ed together. In
6556 practice, at most two will be nonzero. Then we do the IOR's. */
6558 if (GET_CODE (dest) != PC
6559 && GET_CODE (src) == IF_THEN_ELSE
6560 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6561 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6562 && XEXP (XEXP (src, 0), 1) == const0_rtx
6563 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6564 #ifdef HAVE_conditional_move
6565 && ! can_conditionally_move_p (GET_MODE (src))
6566 #endif
6567 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6568 GET_MODE (XEXP (XEXP (src, 0), 0)))
6569 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6570 && ! side_effects_p (src))
6572 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6573 ? XEXP (src, 1) : XEXP (src, 2));
6574 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6575 ? XEXP (src, 2) : XEXP (src, 1));
6576 rtx term1 = const0_rtx, term2, term3;
6578 if (GET_CODE (true_rtx) == IOR
6579 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6580 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6581 else if (GET_CODE (true_rtx) == IOR
6582 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6583 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6584 else if (GET_CODE (false_rtx) == IOR
6585 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6586 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6587 else if (GET_CODE (false_rtx) == IOR
6588 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6589 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6591 term2 = simplify_gen_binary (AND, GET_MODE (src),
6592 XEXP (XEXP (src, 0), 0), true_rtx);
6593 term3 = simplify_gen_binary (AND, GET_MODE (src),
6594 simplify_gen_unary (NOT, GET_MODE (src),
6595 XEXP (XEXP (src, 0), 0),
6596 GET_MODE (src)),
6597 false_rtx);
6599 SUBST (SET_SRC (x),
6600 simplify_gen_binary (IOR, GET_MODE (src),
6601 simplify_gen_binary (IOR, GET_MODE (src),
6602 term1, term2),
6603 term3));
6605 src = SET_SRC (x);
6608 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6609 whole thing fail. */
6610 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6611 return src;
6612 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6613 return dest;
6614 else
6615 /* Convert this into a field assignment operation, if possible. */
6616 return make_field_assignment (x);
6619 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6620 result. */
6622 static rtx
6623 simplify_logical (rtx x)
6625 enum machine_mode mode = GET_MODE (x);
6626 rtx op0 = XEXP (x, 0);
6627 rtx op1 = XEXP (x, 1);
6629 switch (GET_CODE (x))
6631 case AND:
6632 /* We can call simplify_and_const_int only if we don't lose
6633 any (sign) bits when converting INTVAL (op1) to
6634 "unsigned HOST_WIDE_INT". */
6635 if (CONST_INT_P (op1)
6636 && (HWI_COMPUTABLE_MODE_P (mode)
6637 || INTVAL (op1) > 0))
6639 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6640 if (GET_CODE (x) != AND)
6641 return x;
6643 op0 = XEXP (x, 0);
6644 op1 = XEXP (x, 1);
6647 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6648 apply the distributive law and then the inverse distributive
6649 law to see if things simplify. */
6650 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6652 rtx result = distribute_and_simplify_rtx (x, 0);
6653 if (result)
6654 return result;
6656 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6658 rtx result = distribute_and_simplify_rtx (x, 1);
6659 if (result)
6660 return result;
6662 break;
6664 case IOR:
6665 /* If we have (ior (and A B) C), apply the distributive law and then
6666 the inverse distributive law to see if things simplify. */
6668 if (GET_CODE (op0) == AND)
6670 rtx result = distribute_and_simplify_rtx (x, 0);
6671 if (result)
6672 return result;
6675 if (GET_CODE (op1) == AND)
6677 rtx result = distribute_and_simplify_rtx (x, 1);
6678 if (result)
6679 return result;
6681 break;
6683 default:
6684 gcc_unreachable ();
6687 return x;
6690 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6691 operations" because they can be replaced with two more basic operations.
6692 ZERO_EXTEND is also considered "compound" because it can be replaced with
6693 an AND operation, which is simpler, though only one operation.
6695 The function expand_compound_operation is called with an rtx expression
6696 and will convert it to the appropriate shifts and AND operations,
6697 simplifying at each stage.
6699 The function make_compound_operation is called to convert an expression
6700 consisting of shifts and ANDs into the equivalent compound expression.
6701 It is the inverse of this function, loosely speaking. */
6703 static rtx
6704 expand_compound_operation (rtx x)
6706 unsigned HOST_WIDE_INT pos = 0, len;
6707 int unsignedp = 0;
6708 unsigned int modewidth;
6709 rtx tem;
6711 switch (GET_CODE (x))
6713 case ZERO_EXTEND:
6714 unsignedp = 1;
6715 case SIGN_EXTEND:
6716 /* We can't necessarily use a const_int for a multiword mode;
6717 it depends on implicitly extending the value.
6718 Since we don't know the right way to extend it,
6719 we can't tell whether the implicit way is right.
6721 Even for a mode that is no wider than a const_int,
6722 we can't win, because we need to sign extend one of its bits through
6723 the rest of it, and we don't know which bit. */
6724 if (CONST_INT_P (XEXP (x, 0)))
6725 return x;
6727 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6728 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6729 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6730 reloaded. If not for that, MEM's would very rarely be safe.
6732 Reject MODEs bigger than a word, because we might not be able
6733 to reference a two-register group starting with an arbitrary register
6734 (and currently gen_lowpart might crash for a SUBREG). */
6736 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6737 return x;
6739 /* Reject MODEs that aren't scalar integers because turning vector
6740 or complex modes into shifts causes problems. */
6742 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6743 return x;
6745 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6746 /* If the inner object has VOIDmode (the only way this can happen
6747 is if it is an ASM_OPERANDS), we can't do anything since we don't
6748 know how much masking to do. */
6749 if (len == 0)
6750 return x;
6752 break;
6754 case ZERO_EXTRACT:
6755 unsignedp = 1;
6757 /* ... fall through ... */
6759 case SIGN_EXTRACT:
6760 /* If the operand is a CLOBBER, just return it. */
6761 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6762 return XEXP (x, 0);
6764 if (!CONST_INT_P (XEXP (x, 1))
6765 || !CONST_INT_P (XEXP (x, 2))
6766 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6767 return x;
6769 /* Reject MODEs that aren't scalar integers because turning vector
6770 or complex modes into shifts causes problems. */
6772 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6773 return x;
6775 len = INTVAL (XEXP (x, 1));
6776 pos = INTVAL (XEXP (x, 2));
6778 /* This should stay within the object being extracted, fail otherwise. */
6779 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6780 return x;
6782 if (BITS_BIG_ENDIAN)
6783 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6785 break;
6787 default:
6788 return x;
6790 /* Convert sign extension to zero extension, if we know that the high
6791 bit is not set, as this is easier to optimize. It will be converted
6792 back to cheaper alternative in make_extraction. */
6793 if (GET_CODE (x) == SIGN_EXTEND
6794 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6795 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6796 & ~(((unsigned HOST_WIDE_INT)
6797 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6798 >> 1))
6799 == 0)))
6801 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6802 rtx temp2 = expand_compound_operation (temp);
6804 /* Make sure this is a profitable operation. */
6805 if (set_src_cost (x, optimize_this_for_speed_p)
6806 > set_src_cost (temp2, optimize_this_for_speed_p))
6807 return temp2;
6808 else if (set_src_cost (x, optimize_this_for_speed_p)
6809 > set_src_cost (temp, optimize_this_for_speed_p))
6810 return temp;
6811 else
6812 return x;
6815 /* We can optimize some special cases of ZERO_EXTEND. */
6816 if (GET_CODE (x) == ZERO_EXTEND)
6818 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6819 know that the last value didn't have any inappropriate bits
6820 set. */
6821 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6822 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6823 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6824 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6825 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6826 return XEXP (XEXP (x, 0), 0);
6828 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6829 if (GET_CODE (XEXP (x, 0)) == SUBREG
6830 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6831 && subreg_lowpart_p (XEXP (x, 0))
6832 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6833 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6834 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6835 return SUBREG_REG (XEXP (x, 0));
6837 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6838 is a comparison and STORE_FLAG_VALUE permits. This is like
6839 the first case, but it works even when GET_MODE (x) is larger
6840 than HOST_WIDE_INT. */
6841 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6842 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6843 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6844 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6845 <= HOST_BITS_PER_WIDE_INT)
6846 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6847 return XEXP (XEXP (x, 0), 0);
6849 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6850 if (GET_CODE (XEXP (x, 0)) == SUBREG
6851 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6852 && subreg_lowpart_p (XEXP (x, 0))
6853 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6854 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6855 <= HOST_BITS_PER_WIDE_INT)
6856 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6857 return SUBREG_REG (XEXP (x, 0));
6861 /* If we reach here, we want to return a pair of shifts. The inner
6862 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6863 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6864 logical depending on the value of UNSIGNEDP.
6866 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6867 converted into an AND of a shift.
6869 We must check for the case where the left shift would have a negative
6870 count. This can happen in a case like (x >> 31) & 255 on machines
6871 that can't shift by a constant. On those machines, we would first
6872 combine the shift with the AND to produce a variable-position
6873 extraction. Then the constant of 31 would be substituted in
6874 to produce such a position. */
6876 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6877 if (modewidth >= pos + len)
6879 enum machine_mode mode = GET_MODE (x);
6880 tem = gen_lowpart (mode, XEXP (x, 0));
6881 if (!tem || GET_CODE (tem) == CLOBBER)
6882 return x;
6883 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6884 tem, modewidth - pos - len);
6885 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6886 mode, tem, modewidth - len);
6888 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6889 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6890 simplify_shift_const (NULL_RTX, LSHIFTRT,
6891 GET_MODE (x),
6892 XEXP (x, 0), pos),
6893 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6894 else
6895 /* Any other cases we can't handle. */
6896 return x;
6898 /* If we couldn't do this for some reason, return the original
6899 expression. */
6900 if (GET_CODE (tem) == CLOBBER)
6901 return x;
6903 return tem;
6906 /* X is a SET which contains an assignment of one object into
6907 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6908 or certain SUBREGS). If possible, convert it into a series of
6909 logical operations.
6911 We half-heartedly support variable positions, but do not at all
6912 support variable lengths. */
6914 static const_rtx
6915 expand_field_assignment (const_rtx x)
6917 rtx inner;
6918 rtx pos; /* Always counts from low bit. */
6919 int len;
6920 rtx mask, cleared, masked;
6921 enum machine_mode compute_mode;
6923 /* Loop until we find something we can't simplify. */
6924 while (1)
6926 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6927 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6929 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6930 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
6931 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6933 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6934 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6936 inner = XEXP (SET_DEST (x), 0);
6937 len = INTVAL (XEXP (SET_DEST (x), 1));
6938 pos = XEXP (SET_DEST (x), 2);
6940 /* A constant position should stay within the width of INNER. */
6941 if (CONST_INT_P (pos)
6942 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
6943 break;
6945 if (BITS_BIG_ENDIAN)
6947 if (CONST_INT_P (pos))
6948 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
6949 - INTVAL (pos));
6950 else if (GET_CODE (pos) == MINUS
6951 && CONST_INT_P (XEXP (pos, 1))
6952 && (INTVAL (XEXP (pos, 1))
6953 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
6954 /* If position is ADJUST - X, new position is X. */
6955 pos = XEXP (pos, 0);
6956 else
6958 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
6959 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6960 gen_int_mode (prec - len,
6961 GET_MODE (pos)),
6962 pos);
6967 /* A SUBREG between two modes that occupy the same numbers of words
6968 can be done by moving the SUBREG to the source. */
6969 else if (GET_CODE (SET_DEST (x)) == SUBREG
6970 /* We need SUBREGs to compute nonzero_bits properly. */
6971 && nonzero_sign_valid
6972 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6973 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6974 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6975 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6977 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6978 gen_lowpart
6979 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6980 SET_SRC (x)));
6981 continue;
6983 else
6984 break;
6986 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6987 inner = SUBREG_REG (inner);
6989 compute_mode = GET_MODE (inner);
6991 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6992 if (! SCALAR_INT_MODE_P (compute_mode))
6994 enum machine_mode imode;
6996 /* Don't do anything for vector or complex integral types. */
6997 if (! FLOAT_MODE_P (compute_mode))
6998 break;
7000 /* Try to find an integral mode to pun with. */
7001 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7002 if (imode == BLKmode)
7003 break;
7005 compute_mode = imode;
7006 inner = gen_lowpart (imode, inner);
7009 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7010 if (len >= HOST_BITS_PER_WIDE_INT)
7011 break;
7013 /* Now compute the equivalent expression. Make a copy of INNER
7014 for the SET_DEST in case it is a MEM into which we will substitute;
7015 we don't want shared RTL in that case. */
7016 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7017 compute_mode);
7018 cleared = simplify_gen_binary (AND, compute_mode,
7019 simplify_gen_unary (NOT, compute_mode,
7020 simplify_gen_binary (ASHIFT,
7021 compute_mode,
7022 mask, pos),
7023 compute_mode),
7024 inner);
7025 masked = simplify_gen_binary (ASHIFT, compute_mode,
7026 simplify_gen_binary (
7027 AND, compute_mode,
7028 gen_lowpart (compute_mode, SET_SRC (x)),
7029 mask),
7030 pos);
7032 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7033 simplify_gen_binary (IOR, compute_mode,
7034 cleared, masked));
7037 return x;
7040 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7041 it is an RTX that represents the (variable) starting position; otherwise,
7042 POS is the (constant) starting bit position. Both are counted from the LSB.
7044 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7046 IN_DEST is nonzero if this is a reference in the destination of a SET.
7047 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7048 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7049 be used.
7051 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7052 ZERO_EXTRACT should be built even for bits starting at bit 0.
7054 MODE is the desired mode of the result (if IN_DEST == 0).
7056 The result is an RTX for the extraction or NULL_RTX if the target
7057 can't handle it. */
7059 static rtx
7060 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7061 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7062 int in_dest, int in_compare)
7064 /* This mode describes the size of the storage area
7065 to fetch the overall value from. Within that, we
7066 ignore the POS lowest bits, etc. */
7067 enum machine_mode is_mode = GET_MODE (inner);
7068 enum machine_mode inner_mode;
7069 enum machine_mode wanted_inner_mode;
7070 enum machine_mode wanted_inner_reg_mode = word_mode;
7071 enum machine_mode pos_mode = word_mode;
7072 enum machine_mode extraction_mode = word_mode;
7073 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7074 rtx new_rtx = 0;
7075 rtx orig_pos_rtx = pos_rtx;
7076 HOST_WIDE_INT orig_pos;
7078 if (pos_rtx && CONST_INT_P (pos_rtx))
7079 pos = INTVAL (pos_rtx), pos_rtx = 0;
7081 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7083 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7084 consider just the QI as the memory to extract from.
7085 The subreg adds or removes high bits; its mode is
7086 irrelevant to the meaning of this extraction,
7087 since POS and LEN count from the lsb. */
7088 if (MEM_P (SUBREG_REG (inner)))
7089 is_mode = GET_MODE (SUBREG_REG (inner));
7090 inner = SUBREG_REG (inner);
7092 else if (GET_CODE (inner) == ASHIFT
7093 && CONST_INT_P (XEXP (inner, 1))
7094 && pos_rtx == 0 && pos == 0
7095 && len > UINTVAL (XEXP (inner, 1)))
7097 /* We're extracting the least significant bits of an rtx
7098 (ashift X (const_int C)), where LEN > C. Extract the
7099 least significant (LEN - C) bits of X, giving an rtx
7100 whose mode is MODE, then shift it left C times. */
7101 new_rtx = make_extraction (mode, XEXP (inner, 0),
7102 0, 0, len - INTVAL (XEXP (inner, 1)),
7103 unsignedp, in_dest, in_compare);
7104 if (new_rtx != 0)
7105 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7107 else if (GET_CODE (inner) == TRUNCATE)
7108 inner = XEXP (inner, 0);
7110 inner_mode = GET_MODE (inner);
7112 /* See if this can be done without an extraction. We never can if the
7113 width of the field is not the same as that of some integer mode. For
7114 registers, we can only avoid the extraction if the position is at the
7115 low-order bit and this is either not in the destination or we have the
7116 appropriate STRICT_LOW_PART operation available.
7118 For MEM, we can avoid an extract if the field starts on an appropriate
7119 boundary and we can change the mode of the memory reference. */
7121 if (tmode != BLKmode
7122 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7123 && !MEM_P (inner)
7124 && (inner_mode == tmode
7125 || !REG_P (inner)
7126 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7127 || reg_truncated_to_mode (tmode, inner))
7128 && (! in_dest
7129 || (REG_P (inner)
7130 && have_insn_for (STRICT_LOW_PART, tmode))))
7131 || (MEM_P (inner) && pos_rtx == 0
7132 && (pos
7133 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7134 : BITS_PER_UNIT)) == 0
7135 /* We can't do this if we are widening INNER_MODE (it
7136 may not be aligned, for one thing). */
7137 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7138 && (inner_mode == tmode
7139 || (! mode_dependent_address_p (XEXP (inner, 0),
7140 MEM_ADDR_SPACE (inner))
7141 && ! MEM_VOLATILE_P (inner))))))
7143 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7144 field. If the original and current mode are the same, we need not
7145 adjust the offset. Otherwise, we do if bytes big endian.
7147 If INNER is not a MEM, get a piece consisting of just the field
7148 of interest (in this case POS % BITS_PER_WORD must be 0). */
7150 if (MEM_P (inner))
7152 HOST_WIDE_INT offset;
7154 /* POS counts from lsb, but make OFFSET count in memory order. */
7155 if (BYTES_BIG_ENDIAN)
7156 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7157 else
7158 offset = pos / BITS_PER_UNIT;
7160 new_rtx = adjust_address_nv (inner, tmode, offset);
7162 else if (REG_P (inner))
7164 if (tmode != inner_mode)
7166 /* We can't call gen_lowpart in a DEST since we
7167 always want a SUBREG (see below) and it would sometimes
7168 return a new hard register. */
7169 if (pos || in_dest)
7171 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7173 if (WORDS_BIG_ENDIAN
7174 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7175 final_word = ((GET_MODE_SIZE (inner_mode)
7176 - GET_MODE_SIZE (tmode))
7177 / UNITS_PER_WORD) - final_word;
7179 final_word *= UNITS_PER_WORD;
7180 if (BYTES_BIG_ENDIAN &&
7181 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7182 final_word += (GET_MODE_SIZE (inner_mode)
7183 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7185 /* Avoid creating invalid subregs, for example when
7186 simplifying (x>>32)&255. */
7187 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7188 return NULL_RTX;
7190 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7192 else
7193 new_rtx = gen_lowpart (tmode, inner);
7195 else
7196 new_rtx = inner;
7198 else
7199 new_rtx = force_to_mode (inner, tmode,
7200 len >= HOST_BITS_PER_WIDE_INT
7201 ? ~(unsigned HOST_WIDE_INT) 0
7202 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7205 /* If this extraction is going into the destination of a SET,
7206 make a STRICT_LOW_PART unless we made a MEM. */
7208 if (in_dest)
7209 return (MEM_P (new_rtx) ? new_rtx
7210 : (GET_CODE (new_rtx) != SUBREG
7211 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7212 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7214 if (mode == tmode)
7215 return new_rtx;
7217 if (CONST_SCALAR_INT_P (new_rtx))
7218 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7219 mode, new_rtx, tmode);
7221 /* If we know that no extraneous bits are set, and that the high
7222 bit is not set, convert the extraction to the cheaper of
7223 sign and zero extension, that are equivalent in these cases. */
7224 if (flag_expensive_optimizations
7225 && (HWI_COMPUTABLE_MODE_P (tmode)
7226 && ((nonzero_bits (new_rtx, tmode)
7227 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7228 == 0)))
7230 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7231 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7233 /* Prefer ZERO_EXTENSION, since it gives more information to
7234 backends. */
7235 if (set_src_cost (temp, optimize_this_for_speed_p)
7236 <= set_src_cost (temp1, optimize_this_for_speed_p))
7237 return temp;
7238 return temp1;
7241 /* Otherwise, sign- or zero-extend unless we already are in the
7242 proper mode. */
7244 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7245 mode, new_rtx));
7248 /* Unless this is a COMPARE or we have a funny memory reference,
7249 don't do anything with zero-extending field extracts starting at
7250 the low-order bit since they are simple AND operations. */
7251 if (pos_rtx == 0 && pos == 0 && ! in_dest
7252 && ! in_compare && unsignedp)
7253 return 0;
7255 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7256 if the position is not a constant and the length is not 1. In all
7257 other cases, we would only be going outside our object in cases when
7258 an original shift would have been undefined. */
7259 if (MEM_P (inner)
7260 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7261 || (pos_rtx != 0 && len != 1)))
7262 return 0;
7264 enum extraction_pattern pattern = (in_dest ? EP_insv
7265 : unsignedp ? EP_extzv : EP_extv);
7267 /* If INNER is not from memory, we want it to have the mode of a register
7268 extraction pattern's structure operand, or word_mode if there is no
7269 such pattern. The same applies to extraction_mode and pos_mode
7270 and their respective operands.
7272 For memory, assume that the desired extraction_mode and pos_mode
7273 are the same as for a register operation, since at present we don't
7274 have named patterns for aligned memory structures. */
7275 struct extraction_insn insn;
7276 if (get_best_reg_extraction_insn (&insn, pattern,
7277 GET_MODE_BITSIZE (inner_mode), mode))
7279 wanted_inner_reg_mode = insn.struct_mode;
7280 pos_mode = insn.pos_mode;
7281 extraction_mode = insn.field_mode;
7284 /* Never narrow an object, since that might not be safe. */
7286 if (mode != VOIDmode
7287 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7288 extraction_mode = mode;
7290 if (!MEM_P (inner))
7291 wanted_inner_mode = wanted_inner_reg_mode;
7292 else
7294 /* Be careful not to go beyond the extracted object and maintain the
7295 natural alignment of the memory. */
7296 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7297 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7298 > GET_MODE_BITSIZE (wanted_inner_mode))
7300 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7301 gcc_assert (wanted_inner_mode != VOIDmode);
7305 orig_pos = pos;
7307 if (BITS_BIG_ENDIAN)
7309 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7310 BITS_BIG_ENDIAN style. If position is constant, compute new
7311 position. Otherwise, build subtraction.
7312 Note that POS is relative to the mode of the original argument.
7313 If it's a MEM we need to recompute POS relative to that.
7314 However, if we're extracting from (or inserting into) a register,
7315 we want to recompute POS relative to wanted_inner_mode. */
7316 int width = (MEM_P (inner)
7317 ? GET_MODE_BITSIZE (is_mode)
7318 : GET_MODE_BITSIZE (wanted_inner_mode));
7320 if (pos_rtx == 0)
7321 pos = width - len - pos;
7322 else
7323 pos_rtx
7324 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7325 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7326 pos_rtx);
7327 /* POS may be less than 0 now, but we check for that below.
7328 Note that it can only be less than 0 if !MEM_P (inner). */
7331 /* If INNER has a wider mode, and this is a constant extraction, try to
7332 make it smaller and adjust the byte to point to the byte containing
7333 the value. */
7334 if (wanted_inner_mode != VOIDmode
7335 && inner_mode != wanted_inner_mode
7336 && ! pos_rtx
7337 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7338 && MEM_P (inner)
7339 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7340 && ! MEM_VOLATILE_P (inner))
7342 int offset = 0;
7344 /* The computations below will be correct if the machine is big
7345 endian in both bits and bytes or little endian in bits and bytes.
7346 If it is mixed, we must adjust. */
7348 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7349 adjust OFFSET to compensate. */
7350 if (BYTES_BIG_ENDIAN
7351 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7352 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7354 /* We can now move to the desired byte. */
7355 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7356 * GET_MODE_SIZE (wanted_inner_mode);
7357 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7359 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7360 && is_mode != wanted_inner_mode)
7361 offset = (GET_MODE_SIZE (is_mode)
7362 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7364 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7367 /* If INNER is not memory, get it into the proper mode. If we are changing
7368 its mode, POS must be a constant and smaller than the size of the new
7369 mode. */
7370 else if (!MEM_P (inner))
7372 /* On the LHS, don't create paradoxical subregs implicitely truncating
7373 the register unless TRULY_NOOP_TRUNCATION. */
7374 if (in_dest
7375 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7376 wanted_inner_mode))
7377 return NULL_RTX;
7379 if (GET_MODE (inner) != wanted_inner_mode
7380 && (pos_rtx != 0
7381 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7382 return NULL_RTX;
7384 if (orig_pos < 0)
7385 return NULL_RTX;
7387 inner = force_to_mode (inner, wanted_inner_mode,
7388 pos_rtx
7389 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7390 ? ~(unsigned HOST_WIDE_INT) 0
7391 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7392 << orig_pos),
7396 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7397 have to zero extend. Otherwise, we can just use a SUBREG. */
7398 if (pos_rtx != 0
7399 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7401 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7402 GET_MODE (pos_rtx));
7404 /* If we know that no extraneous bits are set, and that the high
7405 bit is not set, convert extraction to cheaper one - either
7406 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7407 cases. */
7408 if (flag_expensive_optimizations
7409 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7410 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7411 & ~(((unsigned HOST_WIDE_INT)
7412 GET_MODE_MASK (GET_MODE (pos_rtx)))
7413 >> 1))
7414 == 0)))
7416 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7417 GET_MODE (pos_rtx));
7419 /* Prefer ZERO_EXTENSION, since it gives more information to
7420 backends. */
7421 if (set_src_cost (temp1, optimize_this_for_speed_p)
7422 < set_src_cost (temp, optimize_this_for_speed_p))
7423 temp = temp1;
7425 pos_rtx = temp;
7428 /* Make POS_RTX unless we already have it and it is correct. If we don't
7429 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7430 be a CONST_INT. */
7431 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7432 pos_rtx = orig_pos_rtx;
7434 else if (pos_rtx == 0)
7435 pos_rtx = GEN_INT (pos);
7437 /* Make the required operation. See if we can use existing rtx. */
7438 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7439 extraction_mode, inner, GEN_INT (len), pos_rtx);
7440 if (! in_dest)
7441 new_rtx = gen_lowpart (mode, new_rtx);
7443 return new_rtx;
7446 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7447 with any other operations in X. Return X without that shift if so. */
7449 static rtx
7450 extract_left_shift (rtx x, int count)
7452 enum rtx_code code = GET_CODE (x);
7453 enum machine_mode mode = GET_MODE (x);
7454 rtx tem;
7456 switch (code)
7458 case ASHIFT:
7459 /* This is the shift itself. If it is wide enough, we will return
7460 either the value being shifted if the shift count is equal to
7461 COUNT or a shift for the difference. */
7462 if (CONST_INT_P (XEXP (x, 1))
7463 && INTVAL (XEXP (x, 1)) >= count)
7464 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7465 INTVAL (XEXP (x, 1)) - count);
7466 break;
7468 case NEG: case NOT:
7469 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7470 return simplify_gen_unary (code, mode, tem, mode);
7472 break;
7474 case PLUS: case IOR: case XOR: case AND:
7475 /* If we can safely shift this constant and we find the inner shift,
7476 make a new operation. */
7477 if (CONST_INT_P (XEXP (x, 1))
7478 && (UINTVAL (XEXP (x, 1))
7479 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7480 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7482 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7483 return simplify_gen_binary (code, mode, tem,
7484 gen_int_mode (val, mode));
7486 break;
7488 default:
7489 break;
7492 return 0;
7495 /* Look at the expression rooted at X. Look for expressions
7496 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7497 Form these expressions.
7499 Return the new rtx, usually just X.
7501 Also, for machines like the VAX that don't have logical shift insns,
7502 try to convert logical to arithmetic shift operations in cases where
7503 they are equivalent. This undoes the canonicalizations to logical
7504 shifts done elsewhere.
7506 We try, as much as possible, to re-use rtl expressions to save memory.
7508 IN_CODE says what kind of expression we are processing. Normally, it is
7509 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7510 being kludges), it is MEM. When processing the arguments of a comparison
7511 or a COMPARE against zero, it is COMPARE. */
7514 make_compound_operation (rtx x, enum rtx_code in_code)
7516 enum rtx_code code = GET_CODE (x);
7517 enum machine_mode mode = GET_MODE (x);
7518 int mode_width = GET_MODE_PRECISION (mode);
7519 rtx rhs, lhs;
7520 enum rtx_code next_code;
7521 int i, j;
7522 rtx new_rtx = 0;
7523 rtx tem;
7524 const char *fmt;
7526 /* Select the code to be used in recursive calls. Once we are inside an
7527 address, we stay there. If we have a comparison, set to COMPARE,
7528 but once inside, go back to our default of SET. */
7530 next_code = (code == MEM ? MEM
7531 : ((code == PLUS || code == MINUS)
7532 && SCALAR_INT_MODE_P (mode)) ? MEM
7533 : ((code == COMPARE || COMPARISON_P (x))
7534 && XEXP (x, 1) == const0_rtx) ? COMPARE
7535 : in_code == COMPARE ? SET : in_code);
7537 /* Process depending on the code of this operation. If NEW is set
7538 nonzero, it will be returned. */
7540 switch (code)
7542 case ASHIFT:
7543 /* Convert shifts by constants into multiplications if inside
7544 an address. */
7545 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7546 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7547 && INTVAL (XEXP (x, 1)) >= 0
7548 && SCALAR_INT_MODE_P (mode))
7550 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7551 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7553 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7554 if (GET_CODE (new_rtx) == NEG)
7556 new_rtx = XEXP (new_rtx, 0);
7557 multval = -multval;
7559 multval = trunc_int_for_mode (multval, mode);
7560 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7562 break;
7564 case PLUS:
7565 lhs = XEXP (x, 0);
7566 rhs = XEXP (x, 1);
7567 lhs = make_compound_operation (lhs, next_code);
7568 rhs = make_compound_operation (rhs, next_code);
7569 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7570 && SCALAR_INT_MODE_P (mode))
7572 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7573 XEXP (lhs, 1));
7574 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7576 else if (GET_CODE (lhs) == MULT
7577 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7579 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7580 simplify_gen_unary (NEG, mode,
7581 XEXP (lhs, 1),
7582 mode));
7583 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7585 else
7587 SUBST (XEXP (x, 0), lhs);
7588 SUBST (XEXP (x, 1), rhs);
7589 goto maybe_swap;
7591 x = gen_lowpart (mode, new_rtx);
7592 goto maybe_swap;
7594 case MINUS:
7595 lhs = XEXP (x, 0);
7596 rhs = XEXP (x, 1);
7597 lhs = make_compound_operation (lhs, next_code);
7598 rhs = make_compound_operation (rhs, next_code);
7599 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7600 && SCALAR_INT_MODE_P (mode))
7602 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7603 XEXP (rhs, 1));
7604 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7606 else if (GET_CODE (rhs) == MULT
7607 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7609 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7610 simplify_gen_unary (NEG, mode,
7611 XEXP (rhs, 1),
7612 mode));
7613 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7615 else
7617 SUBST (XEXP (x, 0), lhs);
7618 SUBST (XEXP (x, 1), rhs);
7619 return x;
7621 return gen_lowpart (mode, new_rtx);
7623 case AND:
7624 /* If the second operand is not a constant, we can't do anything
7625 with it. */
7626 if (!CONST_INT_P (XEXP (x, 1)))
7627 break;
7629 /* If the constant is a power of two minus one and the first operand
7630 is a logical right shift, make an extraction. */
7631 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7632 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7634 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7635 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7636 0, in_code == COMPARE);
7639 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7640 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7641 && subreg_lowpart_p (XEXP (x, 0))
7642 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7643 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7645 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7646 next_code);
7647 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7648 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7649 0, in_code == COMPARE);
7651 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7652 else if ((GET_CODE (XEXP (x, 0)) == XOR
7653 || GET_CODE (XEXP (x, 0)) == IOR)
7654 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7655 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7656 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7658 /* Apply the distributive law, and then try to make extractions. */
7659 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7660 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7661 XEXP (x, 1)),
7662 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7663 XEXP (x, 1)));
7664 new_rtx = make_compound_operation (new_rtx, in_code);
7667 /* If we are have (and (rotate X C) M) and C is larger than the number
7668 of bits in M, this is an extraction. */
7670 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7671 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7672 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7673 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7675 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7676 new_rtx = make_extraction (mode, new_rtx,
7677 (GET_MODE_PRECISION (mode)
7678 - INTVAL (XEXP (XEXP (x, 0), 1))),
7679 NULL_RTX, i, 1, 0, in_code == COMPARE);
7682 /* On machines without logical shifts, if the operand of the AND is
7683 a logical shift and our mask turns off all the propagated sign
7684 bits, we can replace the logical shift with an arithmetic shift. */
7685 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7686 && !have_insn_for (LSHIFTRT, mode)
7687 && have_insn_for (ASHIFTRT, mode)
7688 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7689 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7690 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7691 && mode_width <= HOST_BITS_PER_WIDE_INT)
7693 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7695 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7696 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7697 SUBST (XEXP (x, 0),
7698 gen_rtx_ASHIFTRT (mode,
7699 make_compound_operation
7700 (XEXP (XEXP (x, 0), 0), next_code),
7701 XEXP (XEXP (x, 0), 1)));
7704 /* If the constant is one less than a power of two, this might be
7705 representable by an extraction even if no shift is present.
7706 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7707 we are in a COMPARE. */
7708 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7709 new_rtx = make_extraction (mode,
7710 make_compound_operation (XEXP (x, 0),
7711 next_code),
7712 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7714 /* If we are in a comparison and this is an AND with a power of two,
7715 convert this into the appropriate bit extract. */
7716 else if (in_code == COMPARE
7717 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7718 new_rtx = make_extraction (mode,
7719 make_compound_operation (XEXP (x, 0),
7720 next_code),
7721 i, NULL_RTX, 1, 1, 0, 1);
7723 break;
7725 case LSHIFTRT:
7726 /* If the sign bit is known to be zero, replace this with an
7727 arithmetic shift. */
7728 if (have_insn_for (ASHIFTRT, mode)
7729 && ! have_insn_for (LSHIFTRT, mode)
7730 && mode_width <= HOST_BITS_PER_WIDE_INT
7731 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7733 new_rtx = gen_rtx_ASHIFTRT (mode,
7734 make_compound_operation (XEXP (x, 0),
7735 next_code),
7736 XEXP (x, 1));
7737 break;
7740 /* ... fall through ... */
7742 case ASHIFTRT:
7743 lhs = XEXP (x, 0);
7744 rhs = XEXP (x, 1);
7746 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7747 this is a SIGN_EXTRACT. */
7748 if (CONST_INT_P (rhs)
7749 && GET_CODE (lhs) == ASHIFT
7750 && CONST_INT_P (XEXP (lhs, 1))
7751 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7752 && INTVAL (XEXP (lhs, 1)) >= 0
7753 && INTVAL (rhs) < mode_width)
7755 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7756 new_rtx = make_extraction (mode, new_rtx,
7757 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7758 NULL_RTX, mode_width - INTVAL (rhs),
7759 code == LSHIFTRT, 0, in_code == COMPARE);
7760 break;
7763 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7764 If so, try to merge the shifts into a SIGN_EXTEND. We could
7765 also do this for some cases of SIGN_EXTRACT, but it doesn't
7766 seem worth the effort; the case checked for occurs on Alpha. */
7768 if (!OBJECT_P (lhs)
7769 && ! (GET_CODE (lhs) == SUBREG
7770 && (OBJECT_P (SUBREG_REG (lhs))))
7771 && CONST_INT_P (rhs)
7772 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7773 && INTVAL (rhs) < mode_width
7774 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7775 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7776 0, NULL_RTX, mode_width - INTVAL (rhs),
7777 code == LSHIFTRT, 0, in_code == COMPARE);
7779 break;
7781 case SUBREG:
7782 /* Call ourselves recursively on the inner expression. If we are
7783 narrowing the object and it has a different RTL code from
7784 what it originally did, do this SUBREG as a force_to_mode. */
7786 rtx inner = SUBREG_REG (x), simplified;
7787 enum rtx_code subreg_code = in_code;
7789 /* If in_code is COMPARE, it isn't always safe to pass it through
7790 to the recursive make_compound_operation call. */
7791 if (subreg_code == COMPARE
7792 && (!subreg_lowpart_p (x)
7793 || GET_CODE (inner) == SUBREG
7794 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7795 is (const_int 0), rather than
7796 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7797 || (GET_CODE (inner) == AND
7798 && CONST_INT_P (XEXP (inner, 1))
7799 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7800 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7801 >= GET_MODE_BITSIZE (mode))))
7802 subreg_code = SET;
7804 tem = make_compound_operation (inner, subreg_code);
7806 simplified
7807 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7808 if (simplified)
7809 tem = simplified;
7811 if (GET_CODE (tem) != GET_CODE (inner)
7812 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7813 && subreg_lowpart_p (x))
7815 rtx newer
7816 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7818 /* If we have something other than a SUBREG, we might have
7819 done an expansion, so rerun ourselves. */
7820 if (GET_CODE (newer) != SUBREG)
7821 newer = make_compound_operation (newer, in_code);
7823 /* force_to_mode can expand compounds. If it just re-expanded the
7824 compound, use gen_lowpart to convert to the desired mode. */
7825 if (rtx_equal_p (newer, x)
7826 /* Likewise if it re-expanded the compound only partially.
7827 This happens for SUBREG of ZERO_EXTRACT if they extract
7828 the same number of bits. */
7829 || (GET_CODE (newer) == SUBREG
7830 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7831 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7832 && GET_CODE (inner) == AND
7833 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7834 return gen_lowpart (GET_MODE (x), tem);
7836 return newer;
7839 if (simplified)
7840 return tem;
7842 break;
7844 default:
7845 break;
7848 if (new_rtx)
7850 x = gen_lowpart (mode, new_rtx);
7851 code = GET_CODE (x);
7854 /* Now recursively process each operand of this operation. We need to
7855 handle ZERO_EXTEND specially so that we don't lose track of the
7856 inner mode. */
7857 if (GET_CODE (x) == ZERO_EXTEND)
7859 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7860 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7861 new_rtx, GET_MODE (XEXP (x, 0)));
7862 if (tem)
7863 return tem;
7864 SUBST (XEXP (x, 0), new_rtx);
7865 return x;
7868 fmt = GET_RTX_FORMAT (code);
7869 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7870 if (fmt[i] == 'e')
7872 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7873 SUBST (XEXP (x, i), new_rtx);
7875 else if (fmt[i] == 'E')
7876 for (j = 0; j < XVECLEN (x, i); j++)
7878 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7879 SUBST (XVECEXP (x, i, j), new_rtx);
7882 maybe_swap:
7883 /* If this is a commutative operation, the changes to the operands
7884 may have made it noncanonical. */
7885 if (COMMUTATIVE_ARITH_P (x)
7886 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7888 tem = XEXP (x, 0);
7889 SUBST (XEXP (x, 0), XEXP (x, 1));
7890 SUBST (XEXP (x, 1), tem);
7893 return x;
7896 /* Given M see if it is a value that would select a field of bits
7897 within an item, but not the entire word. Return -1 if not.
7898 Otherwise, return the starting position of the field, where 0 is the
7899 low-order bit.
7901 *PLEN is set to the length of the field. */
7903 static int
7904 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7906 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7907 int pos = m ? ctz_hwi (m) : -1;
7908 int len = 0;
7910 if (pos >= 0)
7911 /* Now shift off the low-order zero bits and see if we have a
7912 power of two minus 1. */
7913 len = exact_log2 ((m >> pos) + 1);
7915 if (len <= 0)
7916 pos = -1;
7918 *plen = len;
7919 return pos;
7922 /* If X refers to a register that equals REG in value, replace these
7923 references with REG. */
7924 static rtx
7925 canon_reg_for_combine (rtx x, rtx reg)
7927 rtx op0, op1, op2;
7928 const char *fmt;
7929 int i;
7930 bool copied;
7932 enum rtx_code code = GET_CODE (x);
7933 switch (GET_RTX_CLASS (code))
7935 case RTX_UNARY:
7936 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7937 if (op0 != XEXP (x, 0))
7938 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7939 GET_MODE (reg));
7940 break;
7942 case RTX_BIN_ARITH:
7943 case RTX_COMM_ARITH:
7944 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7945 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7946 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7947 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7948 break;
7950 case RTX_COMPARE:
7951 case RTX_COMM_COMPARE:
7952 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7953 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7954 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7955 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7956 GET_MODE (op0), op0, op1);
7957 break;
7959 case RTX_TERNARY:
7960 case RTX_BITFIELD_OPS:
7961 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7962 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7963 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7964 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7965 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7966 GET_MODE (op0), op0, op1, op2);
7968 case RTX_OBJ:
7969 if (REG_P (x))
7971 if (rtx_equal_p (get_last_value (reg), x)
7972 || rtx_equal_p (reg, get_last_value (x)))
7973 return reg;
7974 else
7975 break;
7978 /* fall through */
7980 default:
7981 fmt = GET_RTX_FORMAT (code);
7982 copied = false;
7983 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7984 if (fmt[i] == 'e')
7986 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7987 if (op != XEXP (x, i))
7989 if (!copied)
7991 copied = true;
7992 x = copy_rtx (x);
7994 XEXP (x, i) = op;
7997 else if (fmt[i] == 'E')
7999 int j;
8000 for (j = 0; j < XVECLEN (x, i); j++)
8002 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8003 if (op != XVECEXP (x, i, j))
8005 if (!copied)
8007 copied = true;
8008 x = copy_rtx (x);
8010 XVECEXP (x, i, j) = op;
8015 break;
8018 return x;
8021 /* Return X converted to MODE. If the value is already truncated to
8022 MODE we can just return a subreg even though in the general case we
8023 would need an explicit truncation. */
8025 static rtx
8026 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
8028 if (!CONST_INT_P (x)
8029 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8030 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8031 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8033 /* Bit-cast X into an integer mode. */
8034 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8035 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8036 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8037 x, GET_MODE (x));
8040 return gen_lowpart (mode, x);
8043 /* See if X can be simplified knowing that we will only refer to it in
8044 MODE and will only refer to those bits that are nonzero in MASK.
8045 If other bits are being computed or if masking operations are done
8046 that select a superset of the bits in MASK, they can sometimes be
8047 ignored.
8049 Return a possibly simplified expression, but always convert X to
8050 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8052 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8053 are all off in X. This is used when X will be complemented, by either
8054 NOT, NEG, or XOR. */
8056 static rtx
8057 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
8058 int just_select)
8060 enum rtx_code code = GET_CODE (x);
8061 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8062 enum machine_mode op_mode;
8063 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8064 rtx op0, op1, temp;
8066 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8067 code below will do the wrong thing since the mode of such an
8068 expression is VOIDmode.
8070 Also do nothing if X is a CLOBBER; this can happen if X was
8071 the return value from a call to gen_lowpart. */
8072 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8073 return x;
8075 /* We want to perform the operation in its present mode unless we know
8076 that the operation is valid in MODE, in which case we do the operation
8077 in MODE. */
8078 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8079 && have_insn_for (code, mode))
8080 ? mode : GET_MODE (x));
8082 /* It is not valid to do a right-shift in a narrower mode
8083 than the one it came in with. */
8084 if ((code == LSHIFTRT || code == ASHIFTRT)
8085 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8086 op_mode = GET_MODE (x);
8088 /* Truncate MASK to fit OP_MODE. */
8089 if (op_mode)
8090 mask &= GET_MODE_MASK (op_mode);
8092 /* When we have an arithmetic operation, or a shift whose count we
8093 do not know, we need to assume that all bits up to the highest-order
8094 bit in MASK will be needed. This is how we form such a mask. */
8095 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8096 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8097 else
8098 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8099 - 1);
8101 /* Determine what bits of X are guaranteed to be (non)zero. */
8102 nonzero = nonzero_bits (x, mode);
8104 /* If none of the bits in X are needed, return a zero. */
8105 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8106 x = const0_rtx;
8108 /* If X is a CONST_INT, return a new one. Do this here since the
8109 test below will fail. */
8110 if (CONST_INT_P (x))
8112 if (SCALAR_INT_MODE_P (mode))
8113 return gen_int_mode (INTVAL (x) & mask, mode);
8114 else
8116 x = GEN_INT (INTVAL (x) & mask);
8117 return gen_lowpart_common (mode, x);
8121 /* If X is narrower than MODE and we want all the bits in X's mode, just
8122 get X in the proper mode. */
8123 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8124 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8125 return gen_lowpart (mode, x);
8127 /* We can ignore the effect of a SUBREG if it narrows the mode or
8128 if the constant masks to zero all the bits the mode doesn't have. */
8129 if (GET_CODE (x) == SUBREG
8130 && subreg_lowpart_p (x)
8131 && ((GET_MODE_SIZE (GET_MODE (x))
8132 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8133 || (0 == (mask
8134 & GET_MODE_MASK (GET_MODE (x))
8135 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8136 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8138 /* The arithmetic simplifications here only work for scalar integer modes. */
8139 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8140 return gen_lowpart_or_truncate (mode, x);
8142 switch (code)
8144 case CLOBBER:
8145 /* If X is a (clobber (const_int)), return it since we know we are
8146 generating something that won't match. */
8147 return x;
8149 case SIGN_EXTEND:
8150 case ZERO_EXTEND:
8151 case ZERO_EXTRACT:
8152 case SIGN_EXTRACT:
8153 x = expand_compound_operation (x);
8154 if (GET_CODE (x) != code)
8155 return force_to_mode (x, mode, mask, next_select);
8156 break;
8158 case TRUNCATE:
8159 /* Similarly for a truncate. */
8160 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8162 case AND:
8163 /* If this is an AND with a constant, convert it into an AND
8164 whose constant is the AND of that constant with MASK. If it
8165 remains an AND of MASK, delete it since it is redundant. */
8167 if (CONST_INT_P (XEXP (x, 1)))
8169 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8170 mask & INTVAL (XEXP (x, 1)));
8172 /* If X is still an AND, see if it is an AND with a mask that
8173 is just some low-order bits. If so, and it is MASK, we don't
8174 need it. */
8176 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8177 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8178 == mask))
8179 x = XEXP (x, 0);
8181 /* If it remains an AND, try making another AND with the bits
8182 in the mode mask that aren't in MASK turned on. If the
8183 constant in the AND is wide enough, this might make a
8184 cheaper constant. */
8186 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8187 && GET_MODE_MASK (GET_MODE (x)) != mask
8188 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8190 unsigned HOST_WIDE_INT cval
8191 = UINTVAL (XEXP (x, 1))
8192 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8193 rtx y;
8195 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8196 gen_int_mode (cval, GET_MODE (x)));
8197 if (set_src_cost (y, optimize_this_for_speed_p)
8198 < set_src_cost (x, optimize_this_for_speed_p))
8199 x = y;
8202 break;
8205 goto binop;
8207 case PLUS:
8208 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8209 low-order bits (as in an alignment operation) and FOO is already
8210 aligned to that boundary, mask C1 to that boundary as well.
8211 This may eliminate that PLUS and, later, the AND. */
8214 unsigned int width = GET_MODE_PRECISION (mode);
8215 unsigned HOST_WIDE_INT smask = mask;
8217 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8218 number, sign extend it. */
8220 if (width < HOST_BITS_PER_WIDE_INT
8221 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8222 smask |= HOST_WIDE_INT_M1U << width;
8224 if (CONST_INT_P (XEXP (x, 1))
8225 && exact_log2 (- smask) >= 0
8226 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8227 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8228 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8229 (INTVAL (XEXP (x, 1)) & smask)),
8230 mode, smask, next_select);
8233 /* ... fall through ... */
8235 case MULT:
8236 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8237 most significant bit in MASK since carries from those bits will
8238 affect the bits we are interested in. */
8239 mask = fuller_mask;
8240 goto binop;
8242 case MINUS:
8243 /* If X is (minus C Y) where C's least set bit is larger than any bit
8244 in the mask, then we may replace with (neg Y). */
8245 if (CONST_INT_P (XEXP (x, 0))
8246 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8248 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8249 GET_MODE (x));
8250 return force_to_mode (x, mode, mask, next_select);
8253 /* Similarly, if C contains every bit in the fuller_mask, then we may
8254 replace with (not Y). */
8255 if (CONST_INT_P (XEXP (x, 0))
8256 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8258 x = simplify_gen_unary (NOT, GET_MODE (x),
8259 XEXP (x, 1), GET_MODE (x));
8260 return force_to_mode (x, mode, mask, next_select);
8263 mask = fuller_mask;
8264 goto binop;
8266 case IOR:
8267 case XOR:
8268 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8269 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8270 operation which may be a bitfield extraction. Ensure that the
8271 constant we form is not wider than the mode of X. */
8273 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8274 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8275 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8276 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8277 && CONST_INT_P (XEXP (x, 1))
8278 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8279 + floor_log2 (INTVAL (XEXP (x, 1))))
8280 < GET_MODE_PRECISION (GET_MODE (x)))
8281 && (UINTVAL (XEXP (x, 1))
8282 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8284 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8285 << INTVAL (XEXP (XEXP (x, 0), 1)),
8286 GET_MODE (x));
8287 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8288 XEXP (XEXP (x, 0), 0), temp);
8289 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8290 XEXP (XEXP (x, 0), 1));
8291 return force_to_mode (x, mode, mask, next_select);
8294 binop:
8295 /* For most binary operations, just propagate into the operation and
8296 change the mode if we have an operation of that mode. */
8298 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8299 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8301 /* If we ended up truncating both operands, truncate the result of the
8302 operation instead. */
8303 if (GET_CODE (op0) == TRUNCATE
8304 && GET_CODE (op1) == TRUNCATE)
8306 op0 = XEXP (op0, 0);
8307 op1 = XEXP (op1, 0);
8310 op0 = gen_lowpart_or_truncate (op_mode, op0);
8311 op1 = gen_lowpart_or_truncate (op_mode, op1);
8313 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8314 x = simplify_gen_binary (code, op_mode, op0, op1);
8315 break;
8317 case ASHIFT:
8318 /* For left shifts, do the same, but just for the first operand.
8319 However, we cannot do anything with shifts where we cannot
8320 guarantee that the counts are smaller than the size of the mode
8321 because such a count will have a different meaning in a
8322 wider mode. */
8324 if (! (CONST_INT_P (XEXP (x, 1))
8325 && INTVAL (XEXP (x, 1)) >= 0
8326 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8327 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8328 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8329 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8330 break;
8332 /* If the shift count is a constant and we can do arithmetic in
8333 the mode of the shift, refine which bits we need. Otherwise, use the
8334 conservative form of the mask. */
8335 if (CONST_INT_P (XEXP (x, 1))
8336 && INTVAL (XEXP (x, 1)) >= 0
8337 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8338 && HWI_COMPUTABLE_MODE_P (op_mode))
8339 mask >>= INTVAL (XEXP (x, 1));
8340 else
8341 mask = fuller_mask;
8343 op0 = gen_lowpart_or_truncate (op_mode,
8344 force_to_mode (XEXP (x, 0), op_mode,
8345 mask, next_select));
8347 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8348 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8349 break;
8351 case LSHIFTRT:
8352 /* Here we can only do something if the shift count is a constant,
8353 this shift constant is valid for the host, and we can do arithmetic
8354 in OP_MODE. */
8356 if (CONST_INT_P (XEXP (x, 1))
8357 && INTVAL (XEXP (x, 1)) >= 0
8358 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8359 && HWI_COMPUTABLE_MODE_P (op_mode))
8361 rtx inner = XEXP (x, 0);
8362 unsigned HOST_WIDE_INT inner_mask;
8364 /* Select the mask of the bits we need for the shift operand. */
8365 inner_mask = mask << INTVAL (XEXP (x, 1));
8367 /* We can only change the mode of the shift if we can do arithmetic
8368 in the mode of the shift and INNER_MASK is no wider than the
8369 width of X's mode. */
8370 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8371 op_mode = GET_MODE (x);
8373 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8375 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8376 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8379 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8380 shift and AND produces only copies of the sign bit (C2 is one less
8381 than a power of two), we can do this with just a shift. */
8383 if (GET_CODE (x) == LSHIFTRT
8384 && CONST_INT_P (XEXP (x, 1))
8385 /* The shift puts one of the sign bit copies in the least significant
8386 bit. */
8387 && ((INTVAL (XEXP (x, 1))
8388 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8389 >= GET_MODE_PRECISION (GET_MODE (x)))
8390 && exact_log2 (mask + 1) >= 0
8391 /* Number of bits left after the shift must be more than the mask
8392 needs. */
8393 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8394 <= GET_MODE_PRECISION (GET_MODE (x)))
8395 /* Must be more sign bit copies than the mask needs. */
8396 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8397 >= exact_log2 (mask + 1)))
8398 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8399 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8400 - exact_log2 (mask + 1)));
8402 goto shiftrt;
8404 case ASHIFTRT:
8405 /* If we are just looking for the sign bit, we don't need this shift at
8406 all, even if it has a variable count. */
8407 if (val_signbit_p (GET_MODE (x), mask))
8408 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8410 /* If this is a shift by a constant, get a mask that contains those bits
8411 that are not copies of the sign bit. We then have two cases: If
8412 MASK only includes those bits, this can be a logical shift, which may
8413 allow simplifications. If MASK is a single-bit field not within
8414 those bits, we are requesting a copy of the sign bit and hence can
8415 shift the sign bit to the appropriate location. */
8417 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8418 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8420 int i;
8422 /* If the considered data is wider than HOST_WIDE_INT, we can't
8423 represent a mask for all its bits in a single scalar.
8424 But we only care about the lower bits, so calculate these. */
8426 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8428 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8430 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8431 is the number of bits a full-width mask would have set.
8432 We need only shift if these are fewer than nonzero can
8433 hold. If not, we must keep all bits set in nonzero. */
8435 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8436 < HOST_BITS_PER_WIDE_INT)
8437 nonzero >>= INTVAL (XEXP (x, 1))
8438 + HOST_BITS_PER_WIDE_INT
8439 - GET_MODE_PRECISION (GET_MODE (x)) ;
8441 else
8443 nonzero = GET_MODE_MASK (GET_MODE (x));
8444 nonzero >>= INTVAL (XEXP (x, 1));
8447 if ((mask & ~nonzero) == 0)
8449 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8450 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8451 if (GET_CODE (x) != ASHIFTRT)
8452 return force_to_mode (x, mode, mask, next_select);
8455 else if ((i = exact_log2 (mask)) >= 0)
8457 x = simplify_shift_const
8458 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8459 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8461 if (GET_CODE (x) != ASHIFTRT)
8462 return force_to_mode (x, mode, mask, next_select);
8466 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8467 even if the shift count isn't a constant. */
8468 if (mask == 1)
8469 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8470 XEXP (x, 0), XEXP (x, 1));
8472 shiftrt:
8474 /* If this is a zero- or sign-extension operation that just affects bits
8475 we don't care about, remove it. Be sure the call above returned
8476 something that is still a shift. */
8478 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8479 && CONST_INT_P (XEXP (x, 1))
8480 && INTVAL (XEXP (x, 1)) >= 0
8481 && (INTVAL (XEXP (x, 1))
8482 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8483 && GET_CODE (XEXP (x, 0)) == ASHIFT
8484 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8485 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8486 next_select);
8488 break;
8490 case ROTATE:
8491 case ROTATERT:
8492 /* If the shift count is constant and we can do computations
8493 in the mode of X, compute where the bits we care about are.
8494 Otherwise, we can't do anything. Don't change the mode of
8495 the shift or propagate MODE into the shift, though. */
8496 if (CONST_INT_P (XEXP (x, 1))
8497 && INTVAL (XEXP (x, 1)) >= 0)
8499 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8500 GET_MODE (x),
8501 gen_int_mode (mask, GET_MODE (x)),
8502 XEXP (x, 1));
8503 if (temp && CONST_INT_P (temp))
8504 x = simplify_gen_binary (code, GET_MODE (x),
8505 force_to_mode (XEXP (x, 0), GET_MODE (x),
8506 INTVAL (temp), next_select),
8507 XEXP (x, 1));
8509 break;
8511 case NEG:
8512 /* If we just want the low-order bit, the NEG isn't needed since it
8513 won't change the low-order bit. */
8514 if (mask == 1)
8515 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8517 /* We need any bits less significant than the most significant bit in
8518 MASK since carries from those bits will affect the bits we are
8519 interested in. */
8520 mask = fuller_mask;
8521 goto unop;
8523 case NOT:
8524 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8525 same as the XOR case above. Ensure that the constant we form is not
8526 wider than the mode of X. */
8528 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8529 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8530 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8531 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8532 < GET_MODE_PRECISION (GET_MODE (x)))
8533 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8535 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8536 GET_MODE (x));
8537 temp = simplify_gen_binary (XOR, GET_MODE (x),
8538 XEXP (XEXP (x, 0), 0), temp);
8539 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8540 temp, XEXP (XEXP (x, 0), 1));
8542 return force_to_mode (x, mode, mask, next_select);
8545 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8546 use the full mask inside the NOT. */
8547 mask = fuller_mask;
8549 unop:
8550 op0 = gen_lowpart_or_truncate (op_mode,
8551 force_to_mode (XEXP (x, 0), mode, mask,
8552 next_select));
8553 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8554 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8555 break;
8557 case NE:
8558 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8559 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8560 which is equal to STORE_FLAG_VALUE. */
8561 if ((mask & ~STORE_FLAG_VALUE) == 0
8562 && XEXP (x, 1) == const0_rtx
8563 && GET_MODE (XEXP (x, 0)) == mode
8564 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8565 && (nonzero_bits (XEXP (x, 0), mode)
8566 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8567 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8569 break;
8571 case IF_THEN_ELSE:
8572 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8573 written in a narrower mode. We play it safe and do not do so. */
8575 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8576 force_to_mode (XEXP (x, 1), mode,
8577 mask, next_select));
8578 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8579 force_to_mode (XEXP (x, 2), mode,
8580 mask, next_select));
8581 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8582 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8583 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8584 op0, op1);
8585 break;
8587 default:
8588 break;
8591 /* Ensure we return a value of the proper mode. */
8592 return gen_lowpart_or_truncate (mode, x);
8595 /* Return nonzero if X is an expression that has one of two values depending on
8596 whether some other value is zero or nonzero. In that case, we return the
8597 value that is being tested, *PTRUE is set to the value if the rtx being
8598 returned has a nonzero value, and *PFALSE is set to the other alternative.
8600 If we return zero, we set *PTRUE and *PFALSE to X. */
8602 static rtx
8603 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8605 enum machine_mode mode = GET_MODE (x);
8606 enum rtx_code code = GET_CODE (x);
8607 rtx cond0, cond1, true0, true1, false0, false1;
8608 unsigned HOST_WIDE_INT nz;
8610 /* If we are comparing a value against zero, we are done. */
8611 if ((code == NE || code == EQ)
8612 && XEXP (x, 1) == const0_rtx)
8614 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8615 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8616 return XEXP (x, 0);
8619 /* If this is a unary operation whose operand has one of two values, apply
8620 our opcode to compute those values. */
8621 else if (UNARY_P (x)
8622 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8624 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8625 *pfalse = simplify_gen_unary (code, mode, false0,
8626 GET_MODE (XEXP (x, 0)));
8627 return cond0;
8630 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8631 make can't possibly match and would suppress other optimizations. */
8632 else if (code == COMPARE)
8635 /* If this is a binary operation, see if either side has only one of two
8636 values. If either one does or if both do and they are conditional on
8637 the same value, compute the new true and false values. */
8638 else if (BINARY_P (x))
8640 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8641 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8643 if ((cond0 != 0 || cond1 != 0)
8644 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8646 /* If if_then_else_cond returned zero, then true/false are the
8647 same rtl. We must copy one of them to prevent invalid rtl
8648 sharing. */
8649 if (cond0 == 0)
8650 true0 = copy_rtx (true0);
8651 else if (cond1 == 0)
8652 true1 = copy_rtx (true1);
8654 if (COMPARISON_P (x))
8656 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8657 true0, true1);
8658 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8659 false0, false1);
8661 else
8663 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8664 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8667 return cond0 ? cond0 : cond1;
8670 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8671 operands is zero when the other is nonzero, and vice-versa,
8672 and STORE_FLAG_VALUE is 1 or -1. */
8674 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8675 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8676 || code == UMAX)
8677 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8679 rtx op0 = XEXP (XEXP (x, 0), 1);
8680 rtx op1 = XEXP (XEXP (x, 1), 1);
8682 cond0 = XEXP (XEXP (x, 0), 0);
8683 cond1 = XEXP (XEXP (x, 1), 0);
8685 if (COMPARISON_P (cond0)
8686 && COMPARISON_P (cond1)
8687 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8688 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8689 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8690 || ((swap_condition (GET_CODE (cond0))
8691 == reversed_comparison_code (cond1, NULL))
8692 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8693 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8694 && ! side_effects_p (x))
8696 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8697 *pfalse = simplify_gen_binary (MULT, mode,
8698 (code == MINUS
8699 ? simplify_gen_unary (NEG, mode,
8700 op1, mode)
8701 : op1),
8702 const_true_rtx);
8703 return cond0;
8707 /* Similarly for MULT, AND and UMIN, except that for these the result
8708 is always zero. */
8709 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8710 && (code == MULT || code == AND || code == UMIN)
8711 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8713 cond0 = XEXP (XEXP (x, 0), 0);
8714 cond1 = XEXP (XEXP (x, 1), 0);
8716 if (COMPARISON_P (cond0)
8717 && COMPARISON_P (cond1)
8718 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8719 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8720 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8721 || ((swap_condition (GET_CODE (cond0))
8722 == reversed_comparison_code (cond1, NULL))
8723 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8724 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8725 && ! side_effects_p (x))
8727 *ptrue = *pfalse = const0_rtx;
8728 return cond0;
8733 else if (code == IF_THEN_ELSE)
8735 /* If we have IF_THEN_ELSE already, extract the condition and
8736 canonicalize it if it is NE or EQ. */
8737 cond0 = XEXP (x, 0);
8738 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8739 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8740 return XEXP (cond0, 0);
8741 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8743 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8744 return XEXP (cond0, 0);
8746 else
8747 return cond0;
8750 /* If X is a SUBREG, we can narrow both the true and false values
8751 if the inner expression, if there is a condition. */
8752 else if (code == SUBREG
8753 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8754 &true0, &false0)))
8756 true0 = simplify_gen_subreg (mode, true0,
8757 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8758 false0 = simplify_gen_subreg (mode, false0,
8759 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8760 if (true0 && false0)
8762 *ptrue = true0;
8763 *pfalse = false0;
8764 return cond0;
8768 /* If X is a constant, this isn't special and will cause confusions
8769 if we treat it as such. Likewise if it is equivalent to a constant. */
8770 else if (CONSTANT_P (x)
8771 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8774 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8775 will be least confusing to the rest of the compiler. */
8776 else if (mode == BImode)
8778 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8779 return x;
8782 /* If X is known to be either 0 or -1, those are the true and
8783 false values when testing X. */
8784 else if (x == constm1_rtx || x == const0_rtx
8785 || (mode != VOIDmode
8786 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8788 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8789 return x;
8792 /* Likewise for 0 or a single bit. */
8793 else if (HWI_COMPUTABLE_MODE_P (mode)
8794 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8796 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8797 return x;
8800 /* Otherwise fail; show no condition with true and false values the same. */
8801 *ptrue = *pfalse = x;
8802 return 0;
8805 /* Return the value of expression X given the fact that condition COND
8806 is known to be true when applied to REG as its first operand and VAL
8807 as its second. X is known to not be shared and so can be modified in
8808 place.
8810 We only handle the simplest cases, and specifically those cases that
8811 arise with IF_THEN_ELSE expressions. */
8813 static rtx
8814 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8816 enum rtx_code code = GET_CODE (x);
8817 rtx temp;
8818 const char *fmt;
8819 int i, j;
8821 if (side_effects_p (x))
8822 return x;
8824 /* If either operand of the condition is a floating point value,
8825 then we have to avoid collapsing an EQ comparison. */
8826 if (cond == EQ
8827 && rtx_equal_p (x, reg)
8828 && ! FLOAT_MODE_P (GET_MODE (x))
8829 && ! FLOAT_MODE_P (GET_MODE (val)))
8830 return val;
8832 if (cond == UNEQ && rtx_equal_p (x, reg))
8833 return val;
8835 /* If X is (abs REG) and we know something about REG's relationship
8836 with zero, we may be able to simplify this. */
8838 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8839 switch (cond)
8841 case GE: case GT: case EQ:
8842 return XEXP (x, 0);
8843 case LT: case LE:
8844 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8845 XEXP (x, 0),
8846 GET_MODE (XEXP (x, 0)));
8847 default:
8848 break;
8851 /* The only other cases we handle are MIN, MAX, and comparisons if the
8852 operands are the same as REG and VAL. */
8854 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8856 if (rtx_equal_p (XEXP (x, 0), val))
8857 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8859 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8861 if (COMPARISON_P (x))
8863 if (comparison_dominates_p (cond, code))
8864 return const_true_rtx;
8866 code = reversed_comparison_code (x, NULL);
8867 if (code != UNKNOWN
8868 && comparison_dominates_p (cond, code))
8869 return const0_rtx;
8870 else
8871 return x;
8873 else if (code == SMAX || code == SMIN
8874 || code == UMIN || code == UMAX)
8876 int unsignedp = (code == UMIN || code == UMAX);
8878 /* Do not reverse the condition when it is NE or EQ.
8879 This is because we cannot conclude anything about
8880 the value of 'SMAX (x, y)' when x is not equal to y,
8881 but we can when x equals y. */
8882 if ((code == SMAX || code == UMAX)
8883 && ! (cond == EQ || cond == NE))
8884 cond = reverse_condition (cond);
8886 switch (cond)
8888 case GE: case GT:
8889 return unsignedp ? x : XEXP (x, 1);
8890 case LE: case LT:
8891 return unsignedp ? x : XEXP (x, 0);
8892 case GEU: case GTU:
8893 return unsignedp ? XEXP (x, 1) : x;
8894 case LEU: case LTU:
8895 return unsignedp ? XEXP (x, 0) : x;
8896 default:
8897 break;
8902 else if (code == SUBREG)
8904 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8905 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8907 if (SUBREG_REG (x) != r)
8909 /* We must simplify subreg here, before we lose track of the
8910 original inner_mode. */
8911 new_rtx = simplify_subreg (GET_MODE (x), r,
8912 inner_mode, SUBREG_BYTE (x));
8913 if (new_rtx)
8914 return new_rtx;
8915 else
8916 SUBST (SUBREG_REG (x), r);
8919 return x;
8921 /* We don't have to handle SIGN_EXTEND here, because even in the
8922 case of replacing something with a modeless CONST_INT, a
8923 CONST_INT is already (supposed to be) a valid sign extension for
8924 its narrower mode, which implies it's already properly
8925 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8926 story is different. */
8927 else if (code == ZERO_EXTEND)
8929 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8930 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8932 if (XEXP (x, 0) != r)
8934 /* We must simplify the zero_extend here, before we lose
8935 track of the original inner_mode. */
8936 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8937 r, inner_mode);
8938 if (new_rtx)
8939 return new_rtx;
8940 else
8941 SUBST (XEXP (x, 0), r);
8944 return x;
8947 fmt = GET_RTX_FORMAT (code);
8948 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8950 if (fmt[i] == 'e')
8951 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8952 else if (fmt[i] == 'E')
8953 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8954 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8955 cond, reg, val));
8958 return x;
8961 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8962 assignment as a field assignment. */
8964 static int
8965 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8967 if (x == y || rtx_equal_p (x, y))
8968 return 1;
8970 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8971 return 0;
8973 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8974 Note that all SUBREGs of MEM are paradoxical; otherwise they
8975 would have been rewritten. */
8976 if (MEM_P (x) && GET_CODE (y) == SUBREG
8977 && MEM_P (SUBREG_REG (y))
8978 && rtx_equal_p (SUBREG_REG (y),
8979 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8980 return 1;
8982 if (MEM_P (y) && GET_CODE (x) == SUBREG
8983 && MEM_P (SUBREG_REG (x))
8984 && rtx_equal_p (SUBREG_REG (x),
8985 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8986 return 1;
8988 /* We used to see if get_last_value of X and Y were the same but that's
8989 not correct. In one direction, we'll cause the assignment to have
8990 the wrong destination and in the case, we'll import a register into this
8991 insn that might have already have been dead. So fail if none of the
8992 above cases are true. */
8993 return 0;
8996 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8997 Return that assignment if so.
8999 We only handle the most common cases. */
9001 static rtx
9002 make_field_assignment (rtx x)
9004 rtx dest = SET_DEST (x);
9005 rtx src = SET_SRC (x);
9006 rtx assign;
9007 rtx rhs, lhs;
9008 HOST_WIDE_INT c1;
9009 HOST_WIDE_INT pos;
9010 unsigned HOST_WIDE_INT len;
9011 rtx other;
9012 enum machine_mode mode;
9014 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9015 a clear of a one-bit field. We will have changed it to
9016 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9017 for a SUBREG. */
9019 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9020 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9021 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9022 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9024 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9025 1, 1, 1, 0);
9026 if (assign != 0)
9027 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9028 return x;
9031 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9032 && subreg_lowpart_p (XEXP (src, 0))
9033 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9034 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9035 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9036 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9037 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9038 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9040 assign = make_extraction (VOIDmode, dest, 0,
9041 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9042 1, 1, 1, 0);
9043 if (assign != 0)
9044 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9045 return x;
9048 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9049 one-bit field. */
9050 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9051 && XEXP (XEXP (src, 0), 0) == const1_rtx
9052 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9054 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9055 1, 1, 1, 0);
9056 if (assign != 0)
9057 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9058 return x;
9061 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9062 SRC is an AND with all bits of that field set, then we can discard
9063 the AND. */
9064 if (GET_CODE (dest) == ZERO_EXTRACT
9065 && CONST_INT_P (XEXP (dest, 1))
9066 && GET_CODE (src) == AND
9067 && CONST_INT_P (XEXP (src, 1)))
9069 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9070 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9071 unsigned HOST_WIDE_INT ze_mask;
9073 if (width >= HOST_BITS_PER_WIDE_INT)
9074 ze_mask = -1;
9075 else
9076 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9078 /* Complete overlap. We can remove the source AND. */
9079 if ((and_mask & ze_mask) == ze_mask)
9080 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9082 /* Partial overlap. We can reduce the source AND. */
9083 if ((and_mask & ze_mask) != and_mask)
9085 mode = GET_MODE (src);
9086 src = gen_rtx_AND (mode, XEXP (src, 0),
9087 gen_int_mode (and_mask & ze_mask, mode));
9088 return gen_rtx_SET (VOIDmode, dest, src);
9092 /* The other case we handle is assignments into a constant-position
9093 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9094 a mask that has all one bits except for a group of zero bits and
9095 OTHER is known to have zeros where C1 has ones, this is such an
9096 assignment. Compute the position and length from C1. Shift OTHER
9097 to the appropriate position, force it to the required mode, and
9098 make the extraction. Check for the AND in both operands. */
9100 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9101 return x;
9103 rhs = expand_compound_operation (XEXP (src, 0));
9104 lhs = expand_compound_operation (XEXP (src, 1));
9106 if (GET_CODE (rhs) == AND
9107 && CONST_INT_P (XEXP (rhs, 1))
9108 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9109 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9110 else if (GET_CODE (lhs) == AND
9111 && CONST_INT_P (XEXP (lhs, 1))
9112 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9113 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9114 else
9115 return x;
9117 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9118 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9119 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9120 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9121 return x;
9123 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9124 if (assign == 0)
9125 return x;
9127 /* The mode to use for the source is the mode of the assignment, or of
9128 what is inside a possible STRICT_LOW_PART. */
9129 mode = (GET_CODE (assign) == STRICT_LOW_PART
9130 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9132 /* Shift OTHER right POS places and make it the source, restricting it
9133 to the proper length and mode. */
9135 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9136 GET_MODE (src),
9137 other, pos),
9138 dest);
9139 src = force_to_mode (src, mode,
9140 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9141 ? ~(unsigned HOST_WIDE_INT) 0
9142 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9145 /* If SRC is masked by an AND that does not make a difference in
9146 the value being stored, strip it. */
9147 if (GET_CODE (assign) == ZERO_EXTRACT
9148 && CONST_INT_P (XEXP (assign, 1))
9149 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9150 && GET_CODE (src) == AND
9151 && CONST_INT_P (XEXP (src, 1))
9152 && UINTVAL (XEXP (src, 1))
9153 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9154 src = XEXP (src, 0);
9156 return gen_rtx_SET (VOIDmode, assign, src);
9159 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9160 if so. */
9162 static rtx
9163 apply_distributive_law (rtx x)
9165 enum rtx_code code = GET_CODE (x);
9166 enum rtx_code inner_code;
9167 rtx lhs, rhs, other;
9168 rtx tem;
9170 /* Distributivity is not true for floating point as it can change the
9171 value. So we don't do it unless -funsafe-math-optimizations. */
9172 if (FLOAT_MODE_P (GET_MODE (x))
9173 && ! flag_unsafe_math_optimizations)
9174 return x;
9176 /* The outer operation can only be one of the following: */
9177 if (code != IOR && code != AND && code != XOR
9178 && code != PLUS && code != MINUS)
9179 return x;
9181 lhs = XEXP (x, 0);
9182 rhs = XEXP (x, 1);
9184 /* If either operand is a primitive we can't do anything, so get out
9185 fast. */
9186 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9187 return x;
9189 lhs = expand_compound_operation (lhs);
9190 rhs = expand_compound_operation (rhs);
9191 inner_code = GET_CODE (lhs);
9192 if (inner_code != GET_CODE (rhs))
9193 return x;
9195 /* See if the inner and outer operations distribute. */
9196 switch (inner_code)
9198 case LSHIFTRT:
9199 case ASHIFTRT:
9200 case AND:
9201 case IOR:
9202 /* These all distribute except over PLUS. */
9203 if (code == PLUS || code == MINUS)
9204 return x;
9205 break;
9207 case MULT:
9208 if (code != PLUS && code != MINUS)
9209 return x;
9210 break;
9212 case ASHIFT:
9213 /* This is also a multiply, so it distributes over everything. */
9214 break;
9216 /* This used to handle SUBREG, but this turned out to be counter-
9217 productive, since (subreg (op ...)) usually is not handled by
9218 insn patterns, and this "optimization" therefore transformed
9219 recognizable patterns into unrecognizable ones. Therefore the
9220 SUBREG case was removed from here.
9222 It is possible that distributing SUBREG over arithmetic operations
9223 leads to an intermediate result than can then be optimized further,
9224 e.g. by moving the outer SUBREG to the other side of a SET as done
9225 in simplify_set. This seems to have been the original intent of
9226 handling SUBREGs here.
9228 However, with current GCC this does not appear to actually happen,
9229 at least on major platforms. If some case is found where removing
9230 the SUBREG case here prevents follow-on optimizations, distributing
9231 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9233 default:
9234 return x;
9237 /* Set LHS and RHS to the inner operands (A and B in the example
9238 above) and set OTHER to the common operand (C in the example).
9239 There is only one way to do this unless the inner operation is
9240 commutative. */
9241 if (COMMUTATIVE_ARITH_P (lhs)
9242 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9243 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9244 else if (COMMUTATIVE_ARITH_P (lhs)
9245 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9246 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9247 else if (COMMUTATIVE_ARITH_P (lhs)
9248 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9249 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9250 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9251 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9252 else
9253 return x;
9255 /* Form the new inner operation, seeing if it simplifies first. */
9256 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9258 /* There is one exception to the general way of distributing:
9259 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9260 if (code == XOR && inner_code == IOR)
9262 inner_code = AND;
9263 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9266 /* We may be able to continuing distributing the result, so call
9267 ourselves recursively on the inner operation before forming the
9268 outer operation, which we return. */
9269 return simplify_gen_binary (inner_code, GET_MODE (x),
9270 apply_distributive_law (tem), other);
9273 /* See if X is of the form (* (+ A B) C), and if so convert to
9274 (+ (* A C) (* B C)) and try to simplify.
9276 Most of the time, this results in no change. However, if some of
9277 the operands are the same or inverses of each other, simplifications
9278 will result.
9280 For example, (and (ior A B) (not B)) can occur as the result of
9281 expanding a bit field assignment. When we apply the distributive
9282 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9283 which then simplifies to (and (A (not B))).
9285 Note that no checks happen on the validity of applying the inverse
9286 distributive law. This is pointless since we can do it in the
9287 few places where this routine is called.
9289 N is the index of the term that is decomposed (the arithmetic operation,
9290 i.e. (+ A B) in the first example above). !N is the index of the term that
9291 is distributed, i.e. of C in the first example above. */
9292 static rtx
9293 distribute_and_simplify_rtx (rtx x, int n)
9295 enum machine_mode mode;
9296 enum rtx_code outer_code, inner_code;
9297 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9299 /* Distributivity is not true for floating point as it can change the
9300 value. So we don't do it unless -funsafe-math-optimizations. */
9301 if (FLOAT_MODE_P (GET_MODE (x))
9302 && ! flag_unsafe_math_optimizations)
9303 return NULL_RTX;
9305 decomposed = XEXP (x, n);
9306 if (!ARITHMETIC_P (decomposed))
9307 return NULL_RTX;
9309 mode = GET_MODE (x);
9310 outer_code = GET_CODE (x);
9311 distributed = XEXP (x, !n);
9313 inner_code = GET_CODE (decomposed);
9314 inner_op0 = XEXP (decomposed, 0);
9315 inner_op1 = XEXP (decomposed, 1);
9317 /* Special case (and (xor B C) (not A)), which is equivalent to
9318 (xor (ior A B) (ior A C)) */
9319 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9321 distributed = XEXP (distributed, 0);
9322 outer_code = IOR;
9325 if (n == 0)
9327 /* Distribute the second term. */
9328 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9329 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9331 else
9333 /* Distribute the first term. */
9334 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9335 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9338 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9339 new_op0, new_op1));
9340 if (GET_CODE (tmp) != outer_code
9341 && (set_src_cost (tmp, optimize_this_for_speed_p)
9342 < set_src_cost (x, optimize_this_for_speed_p)))
9343 return tmp;
9345 return NULL_RTX;
9348 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9349 in MODE. Return an equivalent form, if different from (and VAROP
9350 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9352 static rtx
9353 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9354 unsigned HOST_WIDE_INT constop)
9356 unsigned HOST_WIDE_INT nonzero;
9357 unsigned HOST_WIDE_INT orig_constop;
9358 rtx orig_varop;
9359 int i;
9361 orig_varop = varop;
9362 orig_constop = constop;
9363 if (GET_CODE (varop) == CLOBBER)
9364 return NULL_RTX;
9366 /* Simplify VAROP knowing that we will be only looking at some of the
9367 bits in it.
9369 Note by passing in CONSTOP, we guarantee that the bits not set in
9370 CONSTOP are not significant and will never be examined. We must
9371 ensure that is the case by explicitly masking out those bits
9372 before returning. */
9373 varop = force_to_mode (varop, mode, constop, 0);
9375 /* If VAROP is a CLOBBER, we will fail so return it. */
9376 if (GET_CODE (varop) == CLOBBER)
9377 return varop;
9379 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9380 to VAROP and return the new constant. */
9381 if (CONST_INT_P (varop))
9382 return gen_int_mode (INTVAL (varop) & constop, mode);
9384 /* See what bits may be nonzero in VAROP. Unlike the general case of
9385 a call to nonzero_bits, here we don't care about bits outside
9386 MODE. */
9388 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9390 /* Turn off all bits in the constant that are known to already be zero.
9391 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9392 which is tested below. */
9394 constop &= nonzero;
9396 /* If we don't have any bits left, return zero. */
9397 if (constop == 0)
9398 return const0_rtx;
9400 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9401 a power of two, we can replace this with an ASHIFT. */
9402 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9403 && (i = exact_log2 (constop)) >= 0)
9404 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9406 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9407 or XOR, then try to apply the distributive law. This may eliminate
9408 operations if either branch can be simplified because of the AND.
9409 It may also make some cases more complex, but those cases probably
9410 won't match a pattern either with or without this. */
9412 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9413 return
9414 gen_lowpart
9415 (mode,
9416 apply_distributive_law
9417 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9418 simplify_and_const_int (NULL_RTX,
9419 GET_MODE (varop),
9420 XEXP (varop, 0),
9421 constop),
9422 simplify_and_const_int (NULL_RTX,
9423 GET_MODE (varop),
9424 XEXP (varop, 1),
9425 constop))));
9427 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9428 the AND and see if one of the operands simplifies to zero. If so, we
9429 may eliminate it. */
9431 if (GET_CODE (varop) == PLUS
9432 && exact_log2 (constop + 1) >= 0)
9434 rtx o0, o1;
9436 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9437 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9438 if (o0 == const0_rtx)
9439 return o1;
9440 if (o1 == const0_rtx)
9441 return o0;
9444 /* Make a SUBREG if necessary. If we can't make it, fail. */
9445 varop = gen_lowpart (mode, varop);
9446 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9447 return NULL_RTX;
9449 /* If we are only masking insignificant bits, return VAROP. */
9450 if (constop == nonzero)
9451 return varop;
9453 if (varop == orig_varop && constop == orig_constop)
9454 return NULL_RTX;
9456 /* Otherwise, return an AND. */
9457 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9461 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9462 in MODE.
9464 Return an equivalent form, if different from X. Otherwise, return X. If
9465 X is zero, we are to always construct the equivalent form. */
9467 static rtx
9468 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9469 unsigned HOST_WIDE_INT constop)
9471 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9472 if (tem)
9473 return tem;
9475 if (!x)
9476 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9477 gen_int_mode (constop, mode));
9478 if (GET_MODE (x) != mode)
9479 x = gen_lowpart (mode, x);
9480 return x;
9483 /* Given a REG, X, compute which bits in X can be nonzero.
9484 We don't care about bits outside of those defined in MODE.
9486 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9487 a shift, AND, or zero_extract, we can do better. */
9489 static rtx
9490 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9491 const_rtx known_x ATTRIBUTE_UNUSED,
9492 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9493 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9494 unsigned HOST_WIDE_INT *nonzero)
9496 rtx tem;
9497 reg_stat_type *rsp;
9499 /* If X is a register whose nonzero bits value is current, use it.
9500 Otherwise, if X is a register whose value we can find, use that
9501 value. Otherwise, use the previously-computed global nonzero bits
9502 for this register. */
9504 rsp = &reg_stat[REGNO (x)];
9505 if (rsp->last_set_value != 0
9506 && (rsp->last_set_mode == mode
9507 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9508 && GET_MODE_CLASS (mode) == MODE_INT))
9509 && ((rsp->last_set_label >= label_tick_ebb_start
9510 && rsp->last_set_label < label_tick)
9511 || (rsp->last_set_label == label_tick
9512 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9513 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9514 && REG_N_SETS (REGNO (x)) == 1
9515 && !REGNO_REG_SET_P
9516 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9517 REGNO (x)))))
9519 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9521 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9522 /* We don't know anything about the upper bits. */
9523 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9525 *nonzero &= mask;
9526 return NULL;
9529 tem = get_last_value (x);
9531 if (tem)
9533 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9534 /* If X is narrower than MODE and TEM is a non-negative
9535 constant that would appear negative in the mode of X,
9536 sign-extend it for use in reg_nonzero_bits because some
9537 machines (maybe most) will actually do the sign-extension
9538 and this is the conservative approach.
9540 ??? For 2.5, try to tighten up the MD files in this regard
9541 instead of this kludge. */
9543 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9544 && CONST_INT_P (tem)
9545 && INTVAL (tem) > 0
9546 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9547 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9548 #endif
9549 return tem;
9551 else if (nonzero_sign_valid && rsp->nonzero_bits)
9553 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9555 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9556 /* We don't know anything about the upper bits. */
9557 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9559 *nonzero &= mask;
9562 return NULL;
9565 /* Return the number of bits at the high-order end of X that are known to
9566 be equal to the sign bit. X will be used in mode MODE; if MODE is
9567 VOIDmode, X will be used in its own mode. The returned value will always
9568 be between 1 and the number of bits in MODE. */
9570 static rtx
9571 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9572 const_rtx known_x ATTRIBUTE_UNUSED,
9573 enum machine_mode known_mode
9574 ATTRIBUTE_UNUSED,
9575 unsigned int known_ret ATTRIBUTE_UNUSED,
9576 unsigned int *result)
9578 rtx tem;
9579 reg_stat_type *rsp;
9581 rsp = &reg_stat[REGNO (x)];
9582 if (rsp->last_set_value != 0
9583 && rsp->last_set_mode == mode
9584 && ((rsp->last_set_label >= label_tick_ebb_start
9585 && rsp->last_set_label < label_tick)
9586 || (rsp->last_set_label == label_tick
9587 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9588 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9589 && REG_N_SETS (REGNO (x)) == 1
9590 && !REGNO_REG_SET_P
9591 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9592 REGNO (x)))))
9594 *result = rsp->last_set_sign_bit_copies;
9595 return NULL;
9598 tem = get_last_value (x);
9599 if (tem != 0)
9600 return tem;
9602 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9603 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9604 *result = rsp->sign_bit_copies;
9606 return NULL;
9609 /* Return the number of "extended" bits there are in X, when interpreted
9610 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9611 unsigned quantities, this is the number of high-order zero bits.
9612 For signed quantities, this is the number of copies of the sign bit
9613 minus 1. In both case, this function returns the number of "spare"
9614 bits. For example, if two quantities for which this function returns
9615 at least 1 are added, the addition is known not to overflow.
9617 This function will always return 0 unless called during combine, which
9618 implies that it must be called from a define_split. */
9620 unsigned int
9621 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9623 if (nonzero_sign_valid == 0)
9624 return 0;
9626 return (unsignedp
9627 ? (HWI_COMPUTABLE_MODE_P (mode)
9628 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9629 - floor_log2 (nonzero_bits (x, mode)))
9630 : 0)
9631 : num_sign_bit_copies (x, mode) - 1);
9634 /* This function is called from `simplify_shift_const' to merge two
9635 outer operations. Specifically, we have already found that we need
9636 to perform operation *POP0 with constant *PCONST0 at the outermost
9637 position. We would now like to also perform OP1 with constant CONST1
9638 (with *POP0 being done last).
9640 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9641 the resulting operation. *PCOMP_P is set to 1 if we would need to
9642 complement the innermost operand, otherwise it is unchanged.
9644 MODE is the mode in which the operation will be done. No bits outside
9645 the width of this mode matter. It is assumed that the width of this mode
9646 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9648 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9649 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9650 result is simply *PCONST0.
9652 If the resulting operation cannot be expressed as one operation, we
9653 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9655 static int
9656 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)
9658 enum rtx_code op0 = *pop0;
9659 HOST_WIDE_INT const0 = *pconst0;
9661 const0 &= GET_MODE_MASK (mode);
9662 const1 &= GET_MODE_MASK (mode);
9664 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9665 if (op0 == AND)
9666 const1 &= const0;
9668 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9669 if OP0 is SET. */
9671 if (op1 == UNKNOWN || op0 == SET)
9672 return 1;
9674 else if (op0 == UNKNOWN)
9675 op0 = op1, const0 = const1;
9677 else if (op0 == op1)
9679 switch (op0)
9681 case AND:
9682 const0 &= const1;
9683 break;
9684 case IOR:
9685 const0 |= const1;
9686 break;
9687 case XOR:
9688 const0 ^= const1;
9689 break;
9690 case PLUS:
9691 const0 += const1;
9692 break;
9693 case NEG:
9694 op0 = UNKNOWN;
9695 break;
9696 default:
9697 break;
9701 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9702 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9703 return 0;
9705 /* If the two constants aren't the same, we can't do anything. The
9706 remaining six cases can all be done. */
9707 else if (const0 != const1)
9708 return 0;
9710 else
9711 switch (op0)
9713 case IOR:
9714 if (op1 == AND)
9715 /* (a & b) | b == b */
9716 op0 = SET;
9717 else /* op1 == XOR */
9718 /* (a ^ b) | b == a | b */
9720 break;
9722 case XOR:
9723 if (op1 == AND)
9724 /* (a & b) ^ b == (~a) & b */
9725 op0 = AND, *pcomp_p = 1;
9726 else /* op1 == IOR */
9727 /* (a | b) ^ b == a & ~b */
9728 op0 = AND, const0 = ~const0;
9729 break;
9731 case AND:
9732 if (op1 == IOR)
9733 /* (a | b) & b == b */
9734 op0 = SET;
9735 else /* op1 == XOR */
9736 /* (a ^ b) & b) == (~a) & b */
9737 *pcomp_p = 1;
9738 break;
9739 default:
9740 break;
9743 /* Check for NO-OP cases. */
9744 const0 &= GET_MODE_MASK (mode);
9745 if (const0 == 0
9746 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9747 op0 = UNKNOWN;
9748 else if (const0 == 0 && op0 == AND)
9749 op0 = SET;
9750 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9751 && op0 == AND)
9752 op0 = UNKNOWN;
9754 *pop0 = op0;
9756 /* ??? Slightly redundant with the above mask, but not entirely.
9757 Moving this above means we'd have to sign-extend the mode mask
9758 for the final test. */
9759 if (op0 != UNKNOWN && op0 != NEG)
9760 *pconst0 = trunc_int_for_mode (const0, mode);
9762 return 1;
9765 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9766 the shift in. The original shift operation CODE is performed on OP in
9767 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9768 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9769 result of the shift is subject to operation OUTER_CODE with operand
9770 OUTER_CONST. */
9772 static enum machine_mode
9773 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9774 enum machine_mode orig_mode, enum machine_mode mode,
9775 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9777 if (orig_mode == mode)
9778 return mode;
9779 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9781 /* In general we can't perform in wider mode for right shift and rotate. */
9782 switch (code)
9784 case ASHIFTRT:
9785 /* We can still widen if the bits brought in from the left are identical
9786 to the sign bit of ORIG_MODE. */
9787 if (num_sign_bit_copies (op, mode)
9788 > (unsigned) (GET_MODE_PRECISION (mode)
9789 - GET_MODE_PRECISION (orig_mode)))
9790 return mode;
9791 return orig_mode;
9793 case LSHIFTRT:
9794 /* Similarly here but with zero bits. */
9795 if (HWI_COMPUTABLE_MODE_P (mode)
9796 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9797 return mode;
9799 /* We can also widen if the bits brought in will be masked off. This
9800 operation is performed in ORIG_MODE. */
9801 if (outer_code == AND)
9803 int care_bits = low_bitmask_len (orig_mode, outer_const);
9805 if (care_bits >= 0
9806 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9807 return mode;
9809 /* fall through */
9811 case ROTATE:
9812 return orig_mode;
9814 case ROTATERT:
9815 gcc_unreachable ();
9817 default:
9818 return mode;
9822 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9823 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9824 if we cannot simplify it. Otherwise, return a simplified value.
9826 The shift is normally computed in the widest mode we find in VAROP, as
9827 long as it isn't a different number of words than RESULT_MODE. Exceptions
9828 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9830 static rtx
9831 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9832 rtx varop, int orig_count)
9834 enum rtx_code orig_code = code;
9835 rtx orig_varop = varop;
9836 int count;
9837 enum machine_mode mode = result_mode;
9838 enum machine_mode shift_mode, tmode;
9839 unsigned int mode_words
9840 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9841 /* We form (outer_op (code varop count) (outer_const)). */
9842 enum rtx_code outer_op = UNKNOWN;
9843 HOST_WIDE_INT outer_const = 0;
9844 int complement_p = 0;
9845 rtx new_rtx, x;
9847 /* Make sure and truncate the "natural" shift on the way in. We don't
9848 want to do this inside the loop as it makes it more difficult to
9849 combine shifts. */
9850 if (SHIFT_COUNT_TRUNCATED)
9851 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9853 /* If we were given an invalid count, don't do anything except exactly
9854 what was requested. */
9856 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9857 return NULL_RTX;
9859 count = orig_count;
9861 /* Unless one of the branches of the `if' in this loop does a `continue',
9862 we will `break' the loop after the `if'. */
9864 while (count != 0)
9866 /* If we have an operand of (clobber (const_int 0)), fail. */
9867 if (GET_CODE (varop) == CLOBBER)
9868 return NULL_RTX;
9870 /* Convert ROTATERT to ROTATE. */
9871 if (code == ROTATERT)
9873 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9874 code = ROTATE;
9875 if (VECTOR_MODE_P (result_mode))
9876 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9877 else
9878 count = bitsize - count;
9881 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9882 mode, outer_op, outer_const);
9884 /* Handle cases where the count is greater than the size of the mode
9885 minus 1. For ASHIFT, use the size minus one as the count (this can
9886 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9887 take the count modulo the size. For other shifts, the result is
9888 zero.
9890 Since these shifts are being produced by the compiler by combining
9891 multiple operations, each of which are defined, we know what the
9892 result is supposed to be. */
9894 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
9896 if (code == ASHIFTRT)
9897 count = GET_MODE_PRECISION (shift_mode) - 1;
9898 else if (code == ROTATE || code == ROTATERT)
9899 count %= GET_MODE_PRECISION (shift_mode);
9900 else
9902 /* We can't simply return zero because there may be an
9903 outer op. */
9904 varop = const0_rtx;
9905 count = 0;
9906 break;
9910 /* If we discovered we had to complement VAROP, leave. Making a NOT
9911 here would cause an infinite loop. */
9912 if (complement_p)
9913 break;
9915 /* An arithmetic right shift of a quantity known to be -1 or 0
9916 is a no-op. */
9917 if (code == ASHIFTRT
9918 && (num_sign_bit_copies (varop, shift_mode)
9919 == GET_MODE_PRECISION (shift_mode)))
9921 count = 0;
9922 break;
9925 /* If we are doing an arithmetic right shift and discarding all but
9926 the sign bit copies, this is equivalent to doing a shift by the
9927 bitsize minus one. Convert it into that shift because it will often
9928 allow other simplifications. */
9930 if (code == ASHIFTRT
9931 && (count + num_sign_bit_copies (varop, shift_mode)
9932 >= GET_MODE_PRECISION (shift_mode)))
9933 count = GET_MODE_PRECISION (shift_mode) - 1;
9935 /* We simplify the tests below and elsewhere by converting
9936 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9937 `make_compound_operation' will convert it to an ASHIFTRT for
9938 those machines (such as VAX) that don't have an LSHIFTRT. */
9939 if (code == ASHIFTRT
9940 && val_signbit_known_clear_p (shift_mode,
9941 nonzero_bits (varop, shift_mode)))
9942 code = LSHIFTRT;
9944 if (((code == LSHIFTRT
9945 && HWI_COMPUTABLE_MODE_P (shift_mode)
9946 && !(nonzero_bits (varop, shift_mode) >> count))
9947 || (code == ASHIFT
9948 && HWI_COMPUTABLE_MODE_P (shift_mode)
9949 && !((nonzero_bits (varop, shift_mode) << count)
9950 & GET_MODE_MASK (shift_mode))))
9951 && !side_effects_p (varop))
9952 varop = const0_rtx;
9954 switch (GET_CODE (varop))
9956 case SIGN_EXTEND:
9957 case ZERO_EXTEND:
9958 case SIGN_EXTRACT:
9959 case ZERO_EXTRACT:
9960 new_rtx = expand_compound_operation (varop);
9961 if (new_rtx != varop)
9963 varop = new_rtx;
9964 continue;
9966 break;
9968 case MEM:
9969 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9970 minus the width of a smaller mode, we can do this with a
9971 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9972 if ((code == ASHIFTRT || code == LSHIFTRT)
9973 && ! mode_dependent_address_p (XEXP (varop, 0),
9974 MEM_ADDR_SPACE (varop))
9975 && ! MEM_VOLATILE_P (varop)
9976 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9977 MODE_INT, 1)) != BLKmode)
9979 new_rtx = adjust_address_nv (varop, tmode,
9980 BYTES_BIG_ENDIAN ? 0
9981 : count / BITS_PER_UNIT);
9983 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9984 : ZERO_EXTEND, mode, new_rtx);
9985 count = 0;
9986 continue;
9988 break;
9990 case SUBREG:
9991 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9992 the same number of words as what we've seen so far. Then store
9993 the widest mode in MODE. */
9994 if (subreg_lowpart_p (varop)
9995 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9996 > GET_MODE_SIZE (GET_MODE (varop)))
9997 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9998 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9999 == mode_words
10000 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10001 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10003 varop = SUBREG_REG (varop);
10004 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10005 mode = GET_MODE (varop);
10006 continue;
10008 break;
10010 case MULT:
10011 /* Some machines use MULT instead of ASHIFT because MULT
10012 is cheaper. But it is still better on those machines to
10013 merge two shifts into one. */
10014 if (CONST_INT_P (XEXP (varop, 1))
10015 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10017 varop
10018 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10019 XEXP (varop, 0),
10020 GEN_INT (exact_log2 (
10021 UINTVAL (XEXP (varop, 1)))));
10022 continue;
10024 break;
10026 case UDIV:
10027 /* Similar, for when divides are cheaper. */
10028 if (CONST_INT_P (XEXP (varop, 1))
10029 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10031 varop
10032 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10033 XEXP (varop, 0),
10034 GEN_INT (exact_log2 (
10035 UINTVAL (XEXP (varop, 1)))));
10036 continue;
10038 break;
10040 case ASHIFTRT:
10041 /* If we are extracting just the sign bit of an arithmetic
10042 right shift, that shift is not needed. However, the sign
10043 bit of a wider mode may be different from what would be
10044 interpreted as the sign bit in a narrower mode, so, if
10045 the result is narrower, don't discard the shift. */
10046 if (code == LSHIFTRT
10047 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10048 && (GET_MODE_BITSIZE (result_mode)
10049 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10051 varop = XEXP (varop, 0);
10052 continue;
10055 /* ... fall through ... */
10057 case LSHIFTRT:
10058 case ASHIFT:
10059 case ROTATE:
10060 /* Here we have two nested shifts. The result is usually the
10061 AND of a new shift with a mask. We compute the result below. */
10062 if (CONST_INT_P (XEXP (varop, 1))
10063 && INTVAL (XEXP (varop, 1)) >= 0
10064 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10065 && HWI_COMPUTABLE_MODE_P (result_mode)
10066 && HWI_COMPUTABLE_MODE_P (mode)
10067 && !VECTOR_MODE_P (result_mode))
10069 enum rtx_code first_code = GET_CODE (varop);
10070 unsigned int first_count = INTVAL (XEXP (varop, 1));
10071 unsigned HOST_WIDE_INT mask;
10072 rtx mask_rtx;
10074 /* We have one common special case. We can't do any merging if
10075 the inner code is an ASHIFTRT of a smaller mode. However, if
10076 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10077 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10078 we can convert it to
10079 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10080 This simplifies certain SIGN_EXTEND operations. */
10081 if (code == ASHIFT && first_code == ASHIFTRT
10082 && count == (GET_MODE_PRECISION (result_mode)
10083 - GET_MODE_PRECISION (GET_MODE (varop))))
10085 /* C3 has the low-order C1 bits zero. */
10087 mask = GET_MODE_MASK (mode)
10088 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10090 varop = simplify_and_const_int (NULL_RTX, result_mode,
10091 XEXP (varop, 0), mask);
10092 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10093 varop, count);
10094 count = first_count;
10095 code = ASHIFTRT;
10096 continue;
10099 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10100 than C1 high-order bits equal to the sign bit, we can convert
10101 this to either an ASHIFT or an ASHIFTRT depending on the
10102 two counts.
10104 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10106 if (code == ASHIFTRT && first_code == ASHIFT
10107 && GET_MODE (varop) == shift_mode
10108 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10109 > first_count))
10111 varop = XEXP (varop, 0);
10112 count -= first_count;
10113 if (count < 0)
10115 count = -count;
10116 code = ASHIFT;
10119 continue;
10122 /* There are some cases we can't do. If CODE is ASHIFTRT,
10123 we can only do this if FIRST_CODE is also ASHIFTRT.
10125 We can't do the case when CODE is ROTATE and FIRST_CODE is
10126 ASHIFTRT.
10128 If the mode of this shift is not the mode of the outer shift,
10129 we can't do this if either shift is a right shift or ROTATE.
10131 Finally, we can't do any of these if the mode is too wide
10132 unless the codes are the same.
10134 Handle the case where the shift codes are the same
10135 first. */
10137 if (code == first_code)
10139 if (GET_MODE (varop) != result_mode
10140 && (code == ASHIFTRT || code == LSHIFTRT
10141 || code == ROTATE))
10142 break;
10144 count += first_count;
10145 varop = XEXP (varop, 0);
10146 continue;
10149 if (code == ASHIFTRT
10150 || (code == ROTATE && first_code == ASHIFTRT)
10151 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10152 || (GET_MODE (varop) != result_mode
10153 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10154 || first_code == ROTATE
10155 || code == ROTATE)))
10156 break;
10158 /* To compute the mask to apply after the shift, shift the
10159 nonzero bits of the inner shift the same way the
10160 outer shift will. */
10162 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10163 result_mode);
10165 mask_rtx
10166 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10167 GEN_INT (count));
10169 /* Give up if we can't compute an outer operation to use. */
10170 if (mask_rtx == 0
10171 || !CONST_INT_P (mask_rtx)
10172 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10173 INTVAL (mask_rtx),
10174 result_mode, &complement_p))
10175 break;
10177 /* If the shifts are in the same direction, we add the
10178 counts. Otherwise, we subtract them. */
10179 if ((code == ASHIFTRT || code == LSHIFTRT)
10180 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10181 count += first_count;
10182 else
10183 count -= first_count;
10185 /* If COUNT is positive, the new shift is usually CODE,
10186 except for the two exceptions below, in which case it is
10187 FIRST_CODE. If the count is negative, FIRST_CODE should
10188 always be used */
10189 if (count > 0
10190 && ((first_code == ROTATE && code == ASHIFT)
10191 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10192 code = first_code;
10193 else if (count < 0)
10194 code = first_code, count = -count;
10196 varop = XEXP (varop, 0);
10197 continue;
10200 /* If we have (A << B << C) for any shift, we can convert this to
10201 (A << C << B). This wins if A is a constant. Only try this if
10202 B is not a constant. */
10204 else if (GET_CODE (varop) == code
10205 && CONST_INT_P (XEXP (varop, 0))
10206 && !CONST_INT_P (XEXP (varop, 1)))
10208 rtx new_rtx = simplify_const_binary_operation (code, mode,
10209 XEXP (varop, 0),
10210 GEN_INT (count));
10211 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10212 count = 0;
10213 continue;
10215 break;
10217 case NOT:
10218 if (VECTOR_MODE_P (mode))
10219 break;
10221 /* Make this fit the case below. */
10222 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10223 continue;
10225 case IOR:
10226 case AND:
10227 case XOR:
10228 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10229 with C the size of VAROP - 1 and the shift is logical if
10230 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10231 we have an (le X 0) operation. If we have an arithmetic shift
10232 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10233 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10235 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10236 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10237 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10238 && (code == LSHIFTRT || code == ASHIFTRT)
10239 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10240 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10242 count = 0;
10243 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10244 const0_rtx);
10246 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10247 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10249 continue;
10252 /* If we have (shift (logical)), move the logical to the outside
10253 to allow it to possibly combine with another logical and the
10254 shift to combine with another shift. This also canonicalizes to
10255 what a ZERO_EXTRACT looks like. Also, some machines have
10256 (and (shift)) insns. */
10258 if (CONST_INT_P (XEXP (varop, 1))
10259 /* We can't do this if we have (ashiftrt (xor)) and the
10260 constant has its sign bit set in shift_mode. */
10261 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10262 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10263 shift_mode))
10264 && (new_rtx = simplify_const_binary_operation
10265 (code, result_mode,
10266 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10267 GEN_INT (count))) != 0
10268 && CONST_INT_P (new_rtx)
10269 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10270 INTVAL (new_rtx), result_mode, &complement_p))
10272 varop = XEXP (varop, 0);
10273 continue;
10276 /* If we can't do that, try to simplify the shift in each arm of the
10277 logical expression, make a new logical expression, and apply
10278 the inverse distributive law. This also can't be done
10279 for some (ashiftrt (xor)). */
10280 if (CONST_INT_P (XEXP (varop, 1))
10281 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10282 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10283 shift_mode)))
10285 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10286 XEXP (varop, 0), count);
10287 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10288 XEXP (varop, 1), count);
10290 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10291 lhs, rhs);
10292 varop = apply_distributive_law (varop);
10294 count = 0;
10295 continue;
10297 break;
10299 case EQ:
10300 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10301 says that the sign bit can be tested, FOO has mode MODE, C is
10302 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10303 that may be nonzero. */
10304 if (code == LSHIFTRT
10305 && XEXP (varop, 1) == const0_rtx
10306 && GET_MODE (XEXP (varop, 0)) == result_mode
10307 && count == (GET_MODE_PRECISION (result_mode) - 1)
10308 && HWI_COMPUTABLE_MODE_P (result_mode)
10309 && STORE_FLAG_VALUE == -1
10310 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10311 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10312 &complement_p))
10314 varop = XEXP (varop, 0);
10315 count = 0;
10316 continue;
10318 break;
10320 case NEG:
10321 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10322 than the number of bits in the mode is equivalent to A. */
10323 if (code == LSHIFTRT
10324 && count == (GET_MODE_PRECISION (result_mode) - 1)
10325 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10327 varop = XEXP (varop, 0);
10328 count = 0;
10329 continue;
10332 /* NEG commutes with ASHIFT since it is multiplication. Move the
10333 NEG outside to allow shifts to combine. */
10334 if (code == ASHIFT
10335 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10336 &complement_p))
10338 varop = XEXP (varop, 0);
10339 continue;
10341 break;
10343 case PLUS:
10344 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10345 is one less than the number of bits in the mode is
10346 equivalent to (xor A 1). */
10347 if (code == LSHIFTRT
10348 && count == (GET_MODE_PRECISION (result_mode) - 1)
10349 && XEXP (varop, 1) == constm1_rtx
10350 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10351 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10352 &complement_p))
10354 count = 0;
10355 varop = XEXP (varop, 0);
10356 continue;
10359 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10360 that might be nonzero in BAR are those being shifted out and those
10361 bits are known zero in FOO, we can replace the PLUS with FOO.
10362 Similarly in the other operand order. This code occurs when
10363 we are computing the size of a variable-size array. */
10365 if ((code == ASHIFTRT || code == LSHIFTRT)
10366 && count < HOST_BITS_PER_WIDE_INT
10367 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10368 && (nonzero_bits (XEXP (varop, 1), result_mode)
10369 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10371 varop = XEXP (varop, 0);
10372 continue;
10374 else if ((code == ASHIFTRT || code == LSHIFTRT)
10375 && count < HOST_BITS_PER_WIDE_INT
10376 && HWI_COMPUTABLE_MODE_P (result_mode)
10377 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10378 >> count)
10379 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10380 & nonzero_bits (XEXP (varop, 1),
10381 result_mode)))
10383 varop = XEXP (varop, 1);
10384 continue;
10387 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10388 if (code == ASHIFT
10389 && CONST_INT_P (XEXP (varop, 1))
10390 && (new_rtx = simplify_const_binary_operation
10391 (ASHIFT, result_mode,
10392 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10393 GEN_INT (count))) != 0
10394 && CONST_INT_P (new_rtx)
10395 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10396 INTVAL (new_rtx), result_mode, &complement_p))
10398 varop = XEXP (varop, 0);
10399 continue;
10402 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10403 signbit', and attempt to change the PLUS to an XOR and move it to
10404 the outer operation as is done above in the AND/IOR/XOR case
10405 leg for shift(logical). See details in logical handling above
10406 for reasoning in doing so. */
10407 if (code == LSHIFTRT
10408 && CONST_INT_P (XEXP (varop, 1))
10409 && mode_signbit_p (result_mode, XEXP (varop, 1))
10410 && (new_rtx = simplify_const_binary_operation
10411 (code, result_mode,
10412 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10413 GEN_INT (count))) != 0
10414 && CONST_INT_P (new_rtx)
10415 && merge_outer_ops (&outer_op, &outer_const, XOR,
10416 INTVAL (new_rtx), result_mode, &complement_p))
10418 varop = XEXP (varop, 0);
10419 continue;
10422 break;
10424 case MINUS:
10425 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10426 with C the size of VAROP - 1 and the shift is logical if
10427 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10428 we have a (gt X 0) operation. If the shift is arithmetic with
10429 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10430 we have a (neg (gt X 0)) operation. */
10432 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10433 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10434 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10435 && (code == LSHIFTRT || code == ASHIFTRT)
10436 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10437 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10438 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10440 count = 0;
10441 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10442 const0_rtx);
10444 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10445 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10447 continue;
10449 break;
10451 case TRUNCATE:
10452 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10453 if the truncate does not affect the value. */
10454 if (code == LSHIFTRT
10455 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10456 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10457 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10458 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10459 - GET_MODE_PRECISION (GET_MODE (varop)))))
10461 rtx varop_inner = XEXP (varop, 0);
10463 varop_inner
10464 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10465 XEXP (varop_inner, 0),
10466 GEN_INT
10467 (count + INTVAL (XEXP (varop_inner, 1))));
10468 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10469 count = 0;
10470 continue;
10472 break;
10474 default:
10475 break;
10478 break;
10481 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10482 outer_op, outer_const);
10484 /* We have now finished analyzing the shift. The result should be
10485 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10486 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10487 to the result of the shift. OUTER_CONST is the relevant constant,
10488 but we must turn off all bits turned off in the shift. */
10490 if (outer_op == UNKNOWN
10491 && orig_code == code && orig_count == count
10492 && varop == orig_varop
10493 && shift_mode == GET_MODE (varop))
10494 return NULL_RTX;
10496 /* Make a SUBREG if necessary. If we can't make it, fail. */
10497 varop = gen_lowpart (shift_mode, varop);
10498 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10499 return NULL_RTX;
10501 /* If we have an outer operation and we just made a shift, it is
10502 possible that we could have simplified the shift were it not
10503 for the outer operation. So try to do the simplification
10504 recursively. */
10506 if (outer_op != UNKNOWN)
10507 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10508 else
10509 x = NULL_RTX;
10511 if (x == NULL_RTX)
10512 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10514 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10515 turn off all the bits that the shift would have turned off. */
10516 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10517 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10518 GET_MODE_MASK (result_mode) >> orig_count);
10520 /* Do the remainder of the processing in RESULT_MODE. */
10521 x = gen_lowpart_or_truncate (result_mode, x);
10523 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10524 operation. */
10525 if (complement_p)
10526 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10528 if (outer_op != UNKNOWN)
10530 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10531 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10532 outer_const = trunc_int_for_mode (outer_const, result_mode);
10534 if (outer_op == AND)
10535 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10536 else if (outer_op == SET)
10538 /* This means that we have determined that the result is
10539 equivalent to a constant. This should be rare. */
10540 if (!side_effects_p (x))
10541 x = GEN_INT (outer_const);
10543 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10544 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10545 else
10546 x = simplify_gen_binary (outer_op, result_mode, x,
10547 GEN_INT (outer_const));
10550 return x;
10553 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10554 The result of the shift is RESULT_MODE. If we cannot simplify it,
10555 return X or, if it is NULL, synthesize the expression with
10556 simplify_gen_binary. Otherwise, return a simplified value.
10558 The shift is normally computed in the widest mode we find in VAROP, as
10559 long as it isn't a different number of words than RESULT_MODE. Exceptions
10560 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10562 static rtx
10563 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10564 rtx varop, int count)
10566 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10567 if (tem)
10568 return tem;
10570 if (!x)
10571 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10572 if (GET_MODE (x) != result_mode)
10573 x = gen_lowpart (result_mode, x);
10574 return x;
10578 /* Like recog, but we receive the address of a pointer to a new pattern.
10579 We try to match the rtx that the pointer points to.
10580 If that fails, we may try to modify or replace the pattern,
10581 storing the replacement into the same pointer object.
10583 Modifications include deletion or addition of CLOBBERs.
10585 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10586 the CLOBBERs are placed.
10588 The value is the final insn code from the pattern ultimately matched,
10589 or -1. */
10591 static int
10592 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10594 rtx pat = *pnewpat;
10595 rtx pat_without_clobbers;
10596 int insn_code_number;
10597 int num_clobbers_to_add = 0;
10598 int i;
10599 rtx notes = NULL_RTX;
10600 rtx old_notes, old_pat;
10601 int old_icode;
10603 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10604 we use to indicate that something didn't match. If we find such a
10605 thing, force rejection. */
10606 if (GET_CODE (pat) == PARALLEL)
10607 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10608 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10609 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10610 return -1;
10612 old_pat = PATTERN (insn);
10613 old_notes = REG_NOTES (insn);
10614 PATTERN (insn) = pat;
10615 REG_NOTES (insn) = NULL_RTX;
10617 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10618 if (dump_file && (dump_flags & TDF_DETAILS))
10620 if (insn_code_number < 0)
10621 fputs ("Failed to match this instruction:\n", dump_file);
10622 else
10623 fputs ("Successfully matched this instruction:\n", dump_file);
10624 print_rtl_single (dump_file, pat);
10627 /* If it isn't, there is the possibility that we previously had an insn
10628 that clobbered some register as a side effect, but the combined
10629 insn doesn't need to do that. So try once more without the clobbers
10630 unless this represents an ASM insn. */
10632 if (insn_code_number < 0 && ! check_asm_operands (pat)
10633 && GET_CODE (pat) == PARALLEL)
10635 int pos;
10637 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10638 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10640 if (i != pos)
10641 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10642 pos++;
10645 SUBST_INT (XVECLEN (pat, 0), pos);
10647 if (pos == 1)
10648 pat = XVECEXP (pat, 0, 0);
10650 PATTERN (insn) = pat;
10651 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10652 if (dump_file && (dump_flags & TDF_DETAILS))
10654 if (insn_code_number < 0)
10655 fputs ("Failed to match this instruction:\n", dump_file);
10656 else
10657 fputs ("Successfully matched this instruction:\n", dump_file);
10658 print_rtl_single (dump_file, pat);
10662 pat_without_clobbers = pat;
10664 PATTERN (insn) = old_pat;
10665 REG_NOTES (insn) = old_notes;
10667 /* Recognize all noop sets, these will be killed by followup pass. */
10668 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10669 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10671 /* If we had any clobbers to add, make a new pattern than contains
10672 them. Then check to make sure that all of them are dead. */
10673 if (num_clobbers_to_add)
10675 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10676 rtvec_alloc (GET_CODE (pat) == PARALLEL
10677 ? (XVECLEN (pat, 0)
10678 + num_clobbers_to_add)
10679 : num_clobbers_to_add + 1));
10681 if (GET_CODE (pat) == PARALLEL)
10682 for (i = 0; i < XVECLEN (pat, 0); i++)
10683 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10684 else
10685 XVECEXP (newpat, 0, 0) = pat;
10687 add_clobbers (newpat, insn_code_number);
10689 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10690 i < XVECLEN (newpat, 0); i++)
10692 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10693 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10694 return -1;
10695 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10697 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10698 notes = alloc_reg_note (REG_UNUSED,
10699 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10702 pat = newpat;
10705 if (insn_code_number >= 0
10706 && insn_code_number != NOOP_MOVE_INSN_CODE)
10708 old_pat = PATTERN (insn);
10709 old_notes = REG_NOTES (insn);
10710 old_icode = INSN_CODE (insn);
10711 PATTERN (insn) = pat;
10712 REG_NOTES (insn) = notes;
10714 /* Allow targets to reject combined insn. */
10715 if (!targetm.legitimate_combined_insn (insn))
10717 if (dump_file && (dump_flags & TDF_DETAILS))
10718 fputs ("Instruction not appropriate for target.",
10719 dump_file);
10721 /* Callers expect recog_for_combine to strip
10722 clobbers from the pattern on failure. */
10723 pat = pat_without_clobbers;
10724 notes = NULL_RTX;
10726 insn_code_number = -1;
10729 PATTERN (insn) = old_pat;
10730 REG_NOTES (insn) = old_notes;
10731 INSN_CODE (insn) = old_icode;
10734 *pnewpat = pat;
10735 *pnotes = notes;
10737 return insn_code_number;
10740 /* Like gen_lowpart_general but for use by combine. In combine it
10741 is not possible to create any new pseudoregs. However, it is
10742 safe to create invalid memory addresses, because combine will
10743 try to recognize them and all they will do is make the combine
10744 attempt fail.
10746 If for some reason this cannot do its job, an rtx
10747 (clobber (const_int 0)) is returned.
10748 An insn containing that will not be recognized. */
10750 static rtx
10751 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10753 enum machine_mode imode = GET_MODE (x);
10754 unsigned int osize = GET_MODE_SIZE (omode);
10755 unsigned int isize = GET_MODE_SIZE (imode);
10756 rtx result;
10758 if (omode == imode)
10759 return x;
10761 /* We can only support MODE being wider than a word if X is a
10762 constant integer or has a mode the same size. */
10763 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10764 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10765 goto fail;
10767 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10768 won't know what to do. So we will strip off the SUBREG here and
10769 process normally. */
10770 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10772 x = SUBREG_REG (x);
10774 /* For use in case we fall down into the address adjustments
10775 further below, we need to adjust the known mode and size of
10776 x; imode and isize, since we just adjusted x. */
10777 imode = GET_MODE (x);
10779 if (imode == omode)
10780 return x;
10782 isize = GET_MODE_SIZE (imode);
10785 result = gen_lowpart_common (omode, x);
10787 if (result)
10788 return result;
10790 if (MEM_P (x))
10792 int offset = 0;
10794 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10795 address. */
10796 if (MEM_VOLATILE_P (x)
10797 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10798 goto fail;
10800 /* If we want to refer to something bigger than the original memref,
10801 generate a paradoxical subreg instead. That will force a reload
10802 of the original memref X. */
10803 if (isize < osize)
10804 return gen_rtx_SUBREG (omode, x, 0);
10806 if (WORDS_BIG_ENDIAN)
10807 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10809 /* Adjust the address so that the address-after-the-data is
10810 unchanged. */
10811 if (BYTES_BIG_ENDIAN)
10812 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10814 return adjust_address_nv (x, omode, offset);
10817 /* If X is a comparison operator, rewrite it in a new mode. This
10818 probably won't match, but may allow further simplifications. */
10819 else if (COMPARISON_P (x))
10820 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10822 /* If we couldn't simplify X any other way, just enclose it in a
10823 SUBREG. Normally, this SUBREG won't match, but some patterns may
10824 include an explicit SUBREG or we may simplify it further in combine. */
10825 else
10827 int offset = 0;
10828 rtx res;
10830 offset = subreg_lowpart_offset (omode, imode);
10831 if (imode == VOIDmode)
10833 imode = int_mode_for_mode (omode);
10834 x = gen_lowpart_common (imode, x);
10835 if (x == NULL)
10836 goto fail;
10838 res = simplify_gen_subreg (omode, x, imode, offset);
10839 if (res)
10840 return res;
10843 fail:
10844 return gen_rtx_CLOBBER (omode, const0_rtx);
10847 /* Try to simplify a comparison between OP0 and a constant OP1,
10848 where CODE is the comparison code that will be tested, into a
10849 (CODE OP0 const0_rtx) form.
10851 The result is a possibly different comparison code to use.
10852 *POP1 may be updated. */
10854 static enum rtx_code
10855 simplify_compare_const (enum rtx_code code, enum machine_mode mode,
10856 rtx op0, rtx *pop1)
10858 unsigned int mode_width = GET_MODE_PRECISION (mode);
10859 HOST_WIDE_INT const_op = INTVAL (*pop1);
10861 /* Get the constant we are comparing against and turn off all bits
10862 not on in our mode. */
10863 if (mode != VOIDmode)
10864 const_op = trunc_int_for_mode (const_op, mode);
10866 /* If we are comparing against a constant power of two and the value
10867 being compared can only have that single bit nonzero (e.g., it was
10868 `and'ed with that bit), we can replace this with a comparison
10869 with zero. */
10870 if (const_op
10871 && (code == EQ || code == NE || code == GE || code == GEU
10872 || code == LT || code == LTU)
10873 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10874 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
10875 && (nonzero_bits (op0, mode)
10876 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
10878 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10879 const_op = 0;
10882 /* Similarly, if we are comparing a value known to be either -1 or
10883 0 with -1, change it to the opposite comparison against zero. */
10884 if (const_op == -1
10885 && (code == EQ || code == NE || code == GT || code == LE
10886 || code == GEU || code == LTU)
10887 && num_sign_bit_copies (op0, mode) == mode_width)
10889 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10890 const_op = 0;
10893 /* Do some canonicalizations based on the comparison code. We prefer
10894 comparisons against zero and then prefer equality comparisons.
10895 If we can reduce the size of a constant, we will do that too. */
10896 switch (code)
10898 case LT:
10899 /* < C is equivalent to <= (C - 1) */
10900 if (const_op > 0)
10902 const_op -= 1;
10903 code = LE;
10904 /* ... fall through to LE case below. */
10906 else
10907 break;
10909 case LE:
10910 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10911 if (const_op < 0)
10913 const_op += 1;
10914 code = LT;
10917 /* If we are doing a <= 0 comparison on a value known to have
10918 a zero sign bit, we can replace this with == 0. */
10919 else if (const_op == 0
10920 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10921 && (nonzero_bits (op0, mode)
10922 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10923 == 0)
10924 code = EQ;
10925 break;
10927 case GE:
10928 /* >= C is equivalent to > (C - 1). */
10929 if (const_op > 0)
10931 const_op -= 1;
10932 code = GT;
10933 /* ... fall through to GT below. */
10935 else
10936 break;
10938 case GT:
10939 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10940 if (const_op < 0)
10942 const_op += 1;
10943 code = GE;
10946 /* If we are doing a > 0 comparison on a value known to have
10947 a zero sign bit, we can replace this with != 0. */
10948 else if (const_op == 0
10949 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10950 && (nonzero_bits (op0, mode)
10951 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10952 == 0)
10953 code = NE;
10954 break;
10956 case LTU:
10957 /* < C is equivalent to <= (C - 1). */
10958 if (const_op > 0)
10960 const_op -= 1;
10961 code = LEU;
10962 /* ... fall through ... */
10964 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10965 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10966 && (unsigned HOST_WIDE_INT) const_op
10967 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10969 const_op = 0;
10970 code = GE;
10971 break;
10973 else
10974 break;
10976 case LEU:
10977 /* unsigned <= 0 is equivalent to == 0 */
10978 if (const_op == 0)
10979 code = EQ;
10980 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10981 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10982 && (unsigned HOST_WIDE_INT) const_op
10983 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10985 const_op = 0;
10986 code = GE;
10988 break;
10990 case GEU:
10991 /* >= C is equivalent to > (C - 1). */
10992 if (const_op > 1)
10994 const_op -= 1;
10995 code = GTU;
10996 /* ... fall through ... */
10999 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11000 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11001 && (unsigned HOST_WIDE_INT) const_op
11002 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11004 const_op = 0;
11005 code = LT;
11006 break;
11008 else
11009 break;
11011 case GTU:
11012 /* unsigned > 0 is equivalent to != 0 */
11013 if (const_op == 0)
11014 code = NE;
11015 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11016 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11017 && (unsigned HOST_WIDE_INT) const_op
11018 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11020 const_op = 0;
11021 code = LT;
11023 break;
11025 default:
11026 break;
11029 *pop1 = GEN_INT (const_op);
11030 return code;
11033 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11034 comparison code that will be tested.
11036 The result is a possibly different comparison code to use. *POP0 and
11037 *POP1 may be updated.
11039 It is possible that we might detect that a comparison is either always
11040 true or always false. However, we do not perform general constant
11041 folding in combine, so this knowledge isn't useful. Such tautologies
11042 should have been detected earlier. Hence we ignore all such cases. */
11044 static enum rtx_code
11045 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11047 rtx op0 = *pop0;
11048 rtx op1 = *pop1;
11049 rtx tem, tem1;
11050 int i;
11051 enum machine_mode mode, tmode;
11053 /* Try a few ways of applying the same transformation to both operands. */
11054 while (1)
11056 #ifndef WORD_REGISTER_OPERATIONS
11057 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11058 so check specially. */
11059 if (code != GTU && code != GEU && code != LTU && code != LEU
11060 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11061 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11062 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11063 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11064 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11065 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11066 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11067 && CONST_INT_P (XEXP (op0, 1))
11068 && XEXP (op0, 1) == XEXP (op1, 1)
11069 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11070 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11071 && (INTVAL (XEXP (op0, 1))
11072 == (GET_MODE_PRECISION (GET_MODE (op0))
11073 - (GET_MODE_PRECISION
11074 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11076 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11077 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11079 #endif
11081 /* If both operands are the same constant shift, see if we can ignore the
11082 shift. We can if the shift is a rotate or if the bits shifted out of
11083 this shift are known to be zero for both inputs and if the type of
11084 comparison is compatible with the shift. */
11085 if (GET_CODE (op0) == GET_CODE (op1)
11086 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11087 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11088 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11089 && (code != GT && code != LT && code != GE && code != LE))
11090 || (GET_CODE (op0) == ASHIFTRT
11091 && (code != GTU && code != LTU
11092 && code != GEU && code != LEU)))
11093 && CONST_INT_P (XEXP (op0, 1))
11094 && INTVAL (XEXP (op0, 1)) >= 0
11095 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11096 && XEXP (op0, 1) == XEXP (op1, 1))
11098 enum machine_mode mode = GET_MODE (op0);
11099 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11100 int shift_count = INTVAL (XEXP (op0, 1));
11102 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11103 mask &= (mask >> shift_count) << shift_count;
11104 else if (GET_CODE (op0) == ASHIFT)
11105 mask = (mask & (mask << shift_count)) >> shift_count;
11107 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11108 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11109 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11110 else
11111 break;
11114 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11115 SUBREGs are of the same mode, and, in both cases, the AND would
11116 be redundant if the comparison was done in the narrower mode,
11117 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11118 and the operand's possibly nonzero bits are 0xffffff01; in that case
11119 if we only care about QImode, we don't need the AND). This case
11120 occurs if the output mode of an scc insn is not SImode and
11121 STORE_FLAG_VALUE == 1 (e.g., the 386).
11123 Similarly, check for a case where the AND's are ZERO_EXTEND
11124 operations from some narrower mode even though a SUBREG is not
11125 present. */
11127 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11128 && CONST_INT_P (XEXP (op0, 1))
11129 && CONST_INT_P (XEXP (op1, 1)))
11131 rtx inner_op0 = XEXP (op0, 0);
11132 rtx inner_op1 = XEXP (op1, 0);
11133 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11134 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11135 int changed = 0;
11137 if (paradoxical_subreg_p (inner_op0)
11138 && GET_CODE (inner_op1) == SUBREG
11139 && (GET_MODE (SUBREG_REG (inner_op0))
11140 == GET_MODE (SUBREG_REG (inner_op1)))
11141 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11142 <= HOST_BITS_PER_WIDE_INT)
11143 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11144 GET_MODE (SUBREG_REG (inner_op0)))))
11145 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11146 GET_MODE (SUBREG_REG (inner_op1))))))
11148 op0 = SUBREG_REG (inner_op0);
11149 op1 = SUBREG_REG (inner_op1);
11151 /* The resulting comparison is always unsigned since we masked
11152 off the original sign bit. */
11153 code = unsigned_condition (code);
11155 changed = 1;
11158 else if (c0 == c1)
11159 for (tmode = GET_CLASS_NARROWEST_MODE
11160 (GET_MODE_CLASS (GET_MODE (op0)));
11161 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11162 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11164 op0 = gen_lowpart (tmode, inner_op0);
11165 op1 = gen_lowpart (tmode, inner_op1);
11166 code = unsigned_condition (code);
11167 changed = 1;
11168 break;
11171 if (! changed)
11172 break;
11175 /* If both operands are NOT, we can strip off the outer operation
11176 and adjust the comparison code for swapped operands; similarly for
11177 NEG, except that this must be an equality comparison. */
11178 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11179 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11180 && (code == EQ || code == NE)))
11181 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11183 else
11184 break;
11187 /* If the first operand is a constant, swap the operands and adjust the
11188 comparison code appropriately, but don't do this if the second operand
11189 is already a constant integer. */
11190 if (swap_commutative_operands_p (op0, op1))
11192 tem = op0, op0 = op1, op1 = tem;
11193 code = swap_condition (code);
11196 /* We now enter a loop during which we will try to simplify the comparison.
11197 For the most part, we only are concerned with comparisons with zero,
11198 but some things may really be comparisons with zero but not start
11199 out looking that way. */
11201 while (CONST_INT_P (op1))
11203 enum machine_mode mode = GET_MODE (op0);
11204 unsigned int mode_width = GET_MODE_PRECISION (mode);
11205 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11206 int equality_comparison_p;
11207 int sign_bit_comparison_p;
11208 int unsigned_comparison_p;
11209 HOST_WIDE_INT const_op;
11211 /* We only want to handle integral modes. This catches VOIDmode,
11212 CCmode, and the floating-point modes. An exception is that we
11213 can handle VOIDmode if OP0 is a COMPARE or a comparison
11214 operation. */
11216 if (GET_MODE_CLASS (mode) != MODE_INT
11217 && ! (mode == VOIDmode
11218 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11219 break;
11221 /* Try to simplify the compare to constant, possibly changing the
11222 comparison op, and/or changing op1 to zero. */
11223 code = simplify_compare_const (code, mode, op0, &op1);
11224 const_op = INTVAL (op1);
11226 /* Compute some predicates to simplify code below. */
11228 equality_comparison_p = (code == EQ || code == NE);
11229 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11230 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11231 || code == GEU);
11233 /* If this is a sign bit comparison and we can do arithmetic in
11234 MODE, say that we will only be needing the sign bit of OP0. */
11235 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11236 op0 = force_to_mode (op0, mode,
11237 (unsigned HOST_WIDE_INT) 1
11238 << (GET_MODE_PRECISION (mode) - 1),
11241 /* Now try cases based on the opcode of OP0. If none of the cases
11242 does a "continue", we exit this loop immediately after the
11243 switch. */
11245 switch (GET_CODE (op0))
11247 case ZERO_EXTRACT:
11248 /* If we are extracting a single bit from a variable position in
11249 a constant that has only a single bit set and are comparing it
11250 with zero, we can convert this into an equality comparison
11251 between the position and the location of the single bit. */
11252 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11253 have already reduced the shift count modulo the word size. */
11254 if (!SHIFT_COUNT_TRUNCATED
11255 && CONST_INT_P (XEXP (op0, 0))
11256 && XEXP (op0, 1) == const1_rtx
11257 && equality_comparison_p && const_op == 0
11258 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11260 if (BITS_BIG_ENDIAN)
11261 i = BITS_PER_WORD - 1 - i;
11263 op0 = XEXP (op0, 2);
11264 op1 = GEN_INT (i);
11265 const_op = i;
11267 /* Result is nonzero iff shift count is equal to I. */
11268 code = reverse_condition (code);
11269 continue;
11272 /* ... fall through ... */
11274 case SIGN_EXTRACT:
11275 tem = expand_compound_operation (op0);
11276 if (tem != op0)
11278 op0 = tem;
11279 continue;
11281 break;
11283 case NOT:
11284 /* If testing for equality, we can take the NOT of the constant. */
11285 if (equality_comparison_p
11286 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11288 op0 = XEXP (op0, 0);
11289 op1 = tem;
11290 continue;
11293 /* If just looking at the sign bit, reverse the sense of the
11294 comparison. */
11295 if (sign_bit_comparison_p)
11297 op0 = XEXP (op0, 0);
11298 code = (code == GE ? LT : GE);
11299 continue;
11301 break;
11303 case NEG:
11304 /* If testing for equality, we can take the NEG of the constant. */
11305 if (equality_comparison_p
11306 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11308 op0 = XEXP (op0, 0);
11309 op1 = tem;
11310 continue;
11313 /* The remaining cases only apply to comparisons with zero. */
11314 if (const_op != 0)
11315 break;
11317 /* When X is ABS or is known positive,
11318 (neg X) is < 0 if and only if X != 0. */
11320 if (sign_bit_comparison_p
11321 && (GET_CODE (XEXP (op0, 0)) == ABS
11322 || (mode_width <= HOST_BITS_PER_WIDE_INT
11323 && (nonzero_bits (XEXP (op0, 0), mode)
11324 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11325 == 0)))
11327 op0 = XEXP (op0, 0);
11328 code = (code == LT ? NE : EQ);
11329 continue;
11332 /* If we have NEG of something whose two high-order bits are the
11333 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11334 if (num_sign_bit_copies (op0, mode) >= 2)
11336 op0 = XEXP (op0, 0);
11337 code = swap_condition (code);
11338 continue;
11340 break;
11342 case ROTATE:
11343 /* If we are testing equality and our count is a constant, we
11344 can perform the inverse operation on our RHS. */
11345 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11346 && (tem = simplify_binary_operation (ROTATERT, mode,
11347 op1, XEXP (op0, 1))) != 0)
11349 op0 = XEXP (op0, 0);
11350 op1 = tem;
11351 continue;
11354 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11355 a particular bit. Convert it to an AND of a constant of that
11356 bit. This will be converted into a ZERO_EXTRACT. */
11357 if (const_op == 0 && sign_bit_comparison_p
11358 && CONST_INT_P (XEXP (op0, 1))
11359 && mode_width <= HOST_BITS_PER_WIDE_INT)
11361 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11362 ((unsigned HOST_WIDE_INT) 1
11363 << (mode_width - 1
11364 - INTVAL (XEXP (op0, 1)))));
11365 code = (code == LT ? NE : EQ);
11366 continue;
11369 /* Fall through. */
11371 case ABS:
11372 /* ABS is ignorable inside an equality comparison with zero. */
11373 if (const_op == 0 && equality_comparison_p)
11375 op0 = XEXP (op0, 0);
11376 continue;
11378 break;
11380 case SIGN_EXTEND:
11381 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11382 (compare FOO CONST) if CONST fits in FOO's mode and we
11383 are either testing inequality or have an unsigned
11384 comparison with ZERO_EXTEND or a signed comparison with
11385 SIGN_EXTEND. But don't do it if we don't have a compare
11386 insn of the given mode, since we'd have to revert it
11387 later on, and then we wouldn't know whether to sign- or
11388 zero-extend. */
11389 mode = GET_MODE (XEXP (op0, 0));
11390 if (GET_MODE_CLASS (mode) == MODE_INT
11391 && ! unsigned_comparison_p
11392 && HWI_COMPUTABLE_MODE_P (mode)
11393 && trunc_int_for_mode (const_op, mode) == const_op
11394 && have_insn_for (COMPARE, mode))
11396 op0 = XEXP (op0, 0);
11397 continue;
11399 break;
11401 case SUBREG:
11402 /* Check for the case where we are comparing A - C1 with C2, that is
11404 (subreg:MODE (plus (A) (-C1))) op (C2)
11406 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11407 comparison in the wider mode. One of the following two conditions
11408 must be true in order for this to be valid:
11410 1. The mode extension results in the same bit pattern being added
11411 on both sides and the comparison is equality or unsigned. As
11412 C2 has been truncated to fit in MODE, the pattern can only be
11413 all 0s or all 1s.
11415 2. The mode extension results in the sign bit being copied on
11416 each side.
11418 The difficulty here is that we have predicates for A but not for
11419 (A - C1) so we need to check that C1 is within proper bounds so
11420 as to perturbate A as little as possible. */
11422 if (mode_width <= HOST_BITS_PER_WIDE_INT
11423 && subreg_lowpart_p (op0)
11424 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11425 && GET_CODE (SUBREG_REG (op0)) == PLUS
11426 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11428 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11429 rtx a = XEXP (SUBREG_REG (op0), 0);
11430 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11432 if ((c1 > 0
11433 && (unsigned HOST_WIDE_INT) c1
11434 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11435 && (equality_comparison_p || unsigned_comparison_p)
11436 /* (A - C1) zero-extends if it is positive and sign-extends
11437 if it is negative, C2 both zero- and sign-extends. */
11438 && ((0 == (nonzero_bits (a, inner_mode)
11439 & ~GET_MODE_MASK (mode))
11440 && const_op >= 0)
11441 /* (A - C1) sign-extends if it is positive and 1-extends
11442 if it is negative, C2 both sign- and 1-extends. */
11443 || (num_sign_bit_copies (a, inner_mode)
11444 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11445 - mode_width)
11446 && const_op < 0)))
11447 || ((unsigned HOST_WIDE_INT) c1
11448 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11449 /* (A - C1) always sign-extends, like C2. */
11450 && num_sign_bit_copies (a, inner_mode)
11451 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11452 - (mode_width - 1))))
11454 op0 = SUBREG_REG (op0);
11455 continue;
11459 /* If the inner mode is narrower and we are extracting the low part,
11460 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11461 if (subreg_lowpart_p (op0)
11462 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11463 /* Fall through */ ;
11464 else
11465 break;
11467 /* ... fall through ... */
11469 case ZERO_EXTEND:
11470 mode = GET_MODE (XEXP (op0, 0));
11471 if (GET_MODE_CLASS (mode) == MODE_INT
11472 && (unsigned_comparison_p || equality_comparison_p)
11473 && HWI_COMPUTABLE_MODE_P (mode)
11474 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11475 && const_op >= 0
11476 && have_insn_for (COMPARE, mode))
11478 op0 = XEXP (op0, 0);
11479 continue;
11481 break;
11483 case PLUS:
11484 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11485 this for equality comparisons due to pathological cases involving
11486 overflows. */
11487 if (equality_comparison_p
11488 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11489 op1, XEXP (op0, 1))))
11491 op0 = XEXP (op0, 0);
11492 op1 = tem;
11493 continue;
11496 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11497 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11498 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11500 op0 = XEXP (XEXP (op0, 0), 0);
11501 code = (code == LT ? EQ : NE);
11502 continue;
11504 break;
11506 case MINUS:
11507 /* We used to optimize signed comparisons against zero, but that
11508 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11509 arrive here as equality comparisons, or (GEU, LTU) are
11510 optimized away. No need to special-case them. */
11512 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11513 (eq B (minus A C)), whichever simplifies. We can only do
11514 this for equality comparisons due to pathological cases involving
11515 overflows. */
11516 if (equality_comparison_p
11517 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11518 XEXP (op0, 1), op1)))
11520 op0 = XEXP (op0, 0);
11521 op1 = tem;
11522 continue;
11525 if (equality_comparison_p
11526 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11527 XEXP (op0, 0), op1)))
11529 op0 = XEXP (op0, 1);
11530 op1 = tem;
11531 continue;
11534 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11535 of bits in X minus 1, is one iff X > 0. */
11536 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11537 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11538 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11539 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11541 op0 = XEXP (op0, 1);
11542 code = (code == GE ? LE : GT);
11543 continue;
11545 break;
11547 case XOR:
11548 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11549 if C is zero or B is a constant. */
11550 if (equality_comparison_p
11551 && 0 != (tem = simplify_binary_operation (XOR, mode,
11552 XEXP (op0, 1), op1)))
11554 op0 = XEXP (op0, 0);
11555 op1 = tem;
11556 continue;
11558 break;
11560 case EQ: case NE:
11561 case UNEQ: case LTGT:
11562 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11563 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11564 case UNORDERED: case ORDERED:
11565 /* We can't do anything if OP0 is a condition code value, rather
11566 than an actual data value. */
11567 if (const_op != 0
11568 || CC0_P (XEXP (op0, 0))
11569 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11570 break;
11572 /* Get the two operands being compared. */
11573 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11574 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11575 else
11576 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11578 /* Check for the cases where we simply want the result of the
11579 earlier test or the opposite of that result. */
11580 if (code == NE || code == EQ
11581 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11582 && (code == LT || code == GE)))
11584 enum rtx_code new_code;
11585 if (code == LT || code == NE)
11586 new_code = GET_CODE (op0);
11587 else
11588 new_code = reversed_comparison_code (op0, NULL);
11590 if (new_code != UNKNOWN)
11592 code = new_code;
11593 op0 = tem;
11594 op1 = tem1;
11595 continue;
11598 break;
11600 case IOR:
11601 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11602 iff X <= 0. */
11603 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11604 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11605 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11607 op0 = XEXP (op0, 1);
11608 code = (code == GE ? GT : LE);
11609 continue;
11611 break;
11613 case AND:
11614 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11615 will be converted to a ZERO_EXTRACT later. */
11616 if (const_op == 0 && equality_comparison_p
11617 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11618 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11620 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11621 XEXP (XEXP (op0, 0), 1));
11622 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11623 continue;
11626 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11627 zero and X is a comparison and C1 and C2 describe only bits set
11628 in STORE_FLAG_VALUE, we can compare with X. */
11629 if (const_op == 0 && equality_comparison_p
11630 && mode_width <= HOST_BITS_PER_WIDE_INT
11631 && CONST_INT_P (XEXP (op0, 1))
11632 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11633 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11634 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11635 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11637 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11638 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11639 if ((~STORE_FLAG_VALUE & mask) == 0
11640 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11641 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11642 && COMPARISON_P (tem))))
11644 op0 = XEXP (XEXP (op0, 0), 0);
11645 continue;
11649 /* If we are doing an equality comparison of an AND of a bit equal
11650 to the sign bit, replace this with a LT or GE comparison of
11651 the underlying value. */
11652 if (equality_comparison_p
11653 && const_op == 0
11654 && CONST_INT_P (XEXP (op0, 1))
11655 && mode_width <= HOST_BITS_PER_WIDE_INT
11656 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11657 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11659 op0 = XEXP (op0, 0);
11660 code = (code == EQ ? GE : LT);
11661 continue;
11664 /* If this AND operation is really a ZERO_EXTEND from a narrower
11665 mode, the constant fits within that mode, and this is either an
11666 equality or unsigned comparison, try to do this comparison in
11667 the narrower mode.
11669 Note that in:
11671 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11672 -> (ne:DI (reg:SI 4) (const_int 0))
11674 unless TRULY_NOOP_TRUNCATION allows it or the register is
11675 known to hold a value of the required mode the
11676 transformation is invalid. */
11677 if ((equality_comparison_p || unsigned_comparison_p)
11678 && CONST_INT_P (XEXP (op0, 1))
11679 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11680 & GET_MODE_MASK (mode))
11681 + 1)) >= 0
11682 && const_op >> i == 0
11683 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11684 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11685 || (REG_P (XEXP (op0, 0))
11686 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11688 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11689 continue;
11692 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11693 fits in both M1 and M2 and the SUBREG is either paradoxical
11694 or represents the low part, permute the SUBREG and the AND
11695 and try again. */
11696 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11698 unsigned HOST_WIDE_INT c1;
11699 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11700 /* Require an integral mode, to avoid creating something like
11701 (AND:SF ...). */
11702 if (SCALAR_INT_MODE_P (tmode)
11703 /* It is unsafe to commute the AND into the SUBREG if the
11704 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11705 not defined. As originally written the upper bits
11706 have a defined value due to the AND operation.
11707 However, if we commute the AND inside the SUBREG then
11708 they no longer have defined values and the meaning of
11709 the code has been changed. */
11710 && (0
11711 #ifdef WORD_REGISTER_OPERATIONS
11712 || (mode_width > GET_MODE_PRECISION (tmode)
11713 && mode_width <= BITS_PER_WORD)
11714 #endif
11715 || (mode_width <= GET_MODE_PRECISION (tmode)
11716 && subreg_lowpart_p (XEXP (op0, 0))))
11717 && CONST_INT_P (XEXP (op0, 1))
11718 && mode_width <= HOST_BITS_PER_WIDE_INT
11719 && HWI_COMPUTABLE_MODE_P (tmode)
11720 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11721 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11722 && c1 != mask
11723 && c1 != GET_MODE_MASK (tmode))
11725 op0 = simplify_gen_binary (AND, tmode,
11726 SUBREG_REG (XEXP (op0, 0)),
11727 gen_int_mode (c1, tmode));
11728 op0 = gen_lowpart (mode, op0);
11729 continue;
11733 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11734 if (const_op == 0 && equality_comparison_p
11735 && XEXP (op0, 1) == const1_rtx
11736 && GET_CODE (XEXP (op0, 0)) == NOT)
11738 op0 = simplify_and_const_int (NULL_RTX, mode,
11739 XEXP (XEXP (op0, 0), 0), 1);
11740 code = (code == NE ? EQ : NE);
11741 continue;
11744 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11745 (eq (and (lshiftrt X) 1) 0).
11746 Also handle the case where (not X) is expressed using xor. */
11747 if (const_op == 0 && equality_comparison_p
11748 && XEXP (op0, 1) == const1_rtx
11749 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11751 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11752 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11754 if (GET_CODE (shift_op) == NOT
11755 || (GET_CODE (shift_op) == XOR
11756 && CONST_INT_P (XEXP (shift_op, 1))
11757 && CONST_INT_P (shift_count)
11758 && HWI_COMPUTABLE_MODE_P (mode)
11759 && (UINTVAL (XEXP (shift_op, 1))
11760 == (unsigned HOST_WIDE_INT) 1
11761 << INTVAL (shift_count))))
11764 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11765 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11766 code = (code == NE ? EQ : NE);
11767 continue;
11770 break;
11772 case ASHIFT:
11773 /* If we have (compare (ashift FOO N) (const_int C)) and
11774 the high order N bits of FOO (N+1 if an inequality comparison)
11775 are known to be zero, we can do this by comparing FOO with C
11776 shifted right N bits so long as the low-order N bits of C are
11777 zero. */
11778 if (CONST_INT_P (XEXP (op0, 1))
11779 && INTVAL (XEXP (op0, 1)) >= 0
11780 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11781 < HOST_BITS_PER_WIDE_INT)
11782 && (((unsigned HOST_WIDE_INT) const_op
11783 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11784 - 1)) == 0)
11785 && mode_width <= HOST_BITS_PER_WIDE_INT
11786 && (nonzero_bits (XEXP (op0, 0), mode)
11787 & ~(mask >> (INTVAL (XEXP (op0, 1))
11788 + ! equality_comparison_p))) == 0)
11790 /* We must perform a logical shift, not an arithmetic one,
11791 as we want the top N bits of C to be zero. */
11792 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11794 temp >>= INTVAL (XEXP (op0, 1));
11795 op1 = gen_int_mode (temp, mode);
11796 op0 = XEXP (op0, 0);
11797 continue;
11800 /* If we are doing a sign bit comparison, it means we are testing
11801 a particular bit. Convert it to the appropriate AND. */
11802 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11803 && mode_width <= HOST_BITS_PER_WIDE_INT)
11805 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11806 ((unsigned HOST_WIDE_INT) 1
11807 << (mode_width - 1
11808 - INTVAL (XEXP (op0, 1)))));
11809 code = (code == LT ? NE : EQ);
11810 continue;
11813 /* If this an equality comparison with zero and we are shifting
11814 the low bit to the sign bit, we can convert this to an AND of the
11815 low-order bit. */
11816 if (const_op == 0 && equality_comparison_p
11817 && CONST_INT_P (XEXP (op0, 1))
11818 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11820 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11821 continue;
11823 break;
11825 case ASHIFTRT:
11826 /* If this is an equality comparison with zero, we can do this
11827 as a logical shift, which might be much simpler. */
11828 if (equality_comparison_p && const_op == 0
11829 && CONST_INT_P (XEXP (op0, 1)))
11831 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11832 XEXP (op0, 0),
11833 INTVAL (XEXP (op0, 1)));
11834 continue;
11837 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11838 do the comparison in a narrower mode. */
11839 if (! unsigned_comparison_p
11840 && CONST_INT_P (XEXP (op0, 1))
11841 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11842 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11843 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11844 MODE_INT, 1)) != BLKmode
11845 && (((unsigned HOST_WIDE_INT) const_op
11846 + (GET_MODE_MASK (tmode) >> 1) + 1)
11847 <= GET_MODE_MASK (tmode)))
11849 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11850 continue;
11853 /* Likewise if OP0 is a PLUS of a sign extension with a
11854 constant, which is usually represented with the PLUS
11855 between the shifts. */
11856 if (! unsigned_comparison_p
11857 && CONST_INT_P (XEXP (op0, 1))
11858 && GET_CODE (XEXP (op0, 0)) == PLUS
11859 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11860 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11861 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11862 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11863 MODE_INT, 1)) != BLKmode
11864 && (((unsigned HOST_WIDE_INT) const_op
11865 + (GET_MODE_MASK (tmode) >> 1) + 1)
11866 <= GET_MODE_MASK (tmode)))
11868 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11869 rtx add_const = XEXP (XEXP (op0, 0), 1);
11870 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11871 add_const, XEXP (op0, 1));
11873 op0 = simplify_gen_binary (PLUS, tmode,
11874 gen_lowpart (tmode, inner),
11875 new_const);
11876 continue;
11879 /* ... fall through ... */
11880 case LSHIFTRT:
11881 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11882 the low order N bits of FOO are known to be zero, we can do this
11883 by comparing FOO with C shifted left N bits so long as no
11884 overflow occurs. Even if the low order N bits of FOO aren't known
11885 to be zero, if the comparison is >= or < we can use the same
11886 optimization and for > or <= by setting all the low
11887 order N bits in the comparison constant. */
11888 if (CONST_INT_P (XEXP (op0, 1))
11889 && INTVAL (XEXP (op0, 1)) > 0
11890 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11891 && mode_width <= HOST_BITS_PER_WIDE_INT
11892 && (((unsigned HOST_WIDE_INT) const_op
11893 + (GET_CODE (op0) != LSHIFTRT
11894 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11895 + 1)
11896 : 0))
11897 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11899 unsigned HOST_WIDE_INT low_bits
11900 = (nonzero_bits (XEXP (op0, 0), mode)
11901 & (((unsigned HOST_WIDE_INT) 1
11902 << INTVAL (XEXP (op0, 1))) - 1));
11903 if (low_bits == 0 || !equality_comparison_p)
11905 /* If the shift was logical, then we must make the condition
11906 unsigned. */
11907 if (GET_CODE (op0) == LSHIFTRT)
11908 code = unsigned_condition (code);
11910 const_op <<= INTVAL (XEXP (op0, 1));
11911 if (low_bits != 0
11912 && (code == GT || code == GTU
11913 || code == LE || code == LEU))
11914 const_op
11915 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11916 op1 = GEN_INT (const_op);
11917 op0 = XEXP (op0, 0);
11918 continue;
11922 /* If we are using this shift to extract just the sign bit, we
11923 can replace this with an LT or GE comparison. */
11924 if (const_op == 0
11925 && (equality_comparison_p || sign_bit_comparison_p)
11926 && CONST_INT_P (XEXP (op0, 1))
11927 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11929 op0 = XEXP (op0, 0);
11930 code = (code == NE || code == GT ? LT : GE);
11931 continue;
11933 break;
11935 default:
11936 break;
11939 break;
11942 /* Now make any compound operations involved in this comparison. Then,
11943 check for an outmost SUBREG on OP0 that is not doing anything or is
11944 paradoxical. The latter transformation must only be performed when
11945 it is known that the "extra" bits will be the same in op0 and op1 or
11946 that they don't matter. There are three cases to consider:
11948 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11949 care bits and we can assume they have any convenient value. So
11950 making the transformation is safe.
11952 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11953 In this case the upper bits of op0 are undefined. We should not make
11954 the simplification in that case as we do not know the contents of
11955 those bits.
11957 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11958 UNKNOWN. In that case we know those bits are zeros or ones. We must
11959 also be sure that they are the same as the upper bits of op1.
11961 We can never remove a SUBREG for a non-equality comparison because
11962 the sign bit is in a different place in the underlying object. */
11964 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11965 op1 = make_compound_operation (op1, SET);
11967 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11968 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11969 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11970 && (code == NE || code == EQ))
11972 if (paradoxical_subreg_p (op0))
11974 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11975 implemented. */
11976 if (REG_P (SUBREG_REG (op0)))
11978 op0 = SUBREG_REG (op0);
11979 op1 = gen_lowpart (GET_MODE (op0), op1);
11982 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
11983 <= HOST_BITS_PER_WIDE_INT)
11984 && (nonzero_bits (SUBREG_REG (op0),
11985 GET_MODE (SUBREG_REG (op0)))
11986 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11988 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11990 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11991 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11992 op0 = SUBREG_REG (op0), op1 = tem;
11996 /* We now do the opposite procedure: Some machines don't have compare
11997 insns in all modes. If OP0's mode is an integer mode smaller than a
11998 word and we can't do a compare in that mode, see if there is a larger
11999 mode for which we can do the compare. There are a number of cases in
12000 which we can use the wider mode. */
12002 mode = GET_MODE (op0);
12003 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12004 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12005 && ! have_insn_for (COMPARE, mode))
12006 for (tmode = GET_MODE_WIDER_MODE (mode);
12007 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12008 tmode = GET_MODE_WIDER_MODE (tmode))
12009 if (have_insn_for (COMPARE, tmode))
12011 int zero_extended;
12013 /* If this is a test for negative, we can make an explicit
12014 test of the sign bit. Test this first so we can use
12015 a paradoxical subreg to extend OP0. */
12017 if (op1 == const0_rtx && (code == LT || code == GE)
12018 && HWI_COMPUTABLE_MODE_P (mode))
12020 unsigned HOST_WIDE_INT sign
12021 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12022 op0 = simplify_gen_binary (AND, tmode,
12023 gen_lowpart (tmode, op0),
12024 gen_int_mode (sign, tmode));
12025 code = (code == LT) ? NE : EQ;
12026 break;
12029 /* If the only nonzero bits in OP0 and OP1 are those in the
12030 narrower mode and this is an equality or unsigned comparison,
12031 we can use the wider mode. Similarly for sign-extended
12032 values, in which case it is true for all comparisons. */
12033 zero_extended = ((code == EQ || code == NE
12034 || code == GEU || code == GTU
12035 || code == LEU || code == LTU)
12036 && (nonzero_bits (op0, tmode)
12037 & ~GET_MODE_MASK (mode)) == 0
12038 && ((CONST_INT_P (op1)
12039 || (nonzero_bits (op1, tmode)
12040 & ~GET_MODE_MASK (mode)) == 0)));
12042 if (zero_extended
12043 || ((num_sign_bit_copies (op0, tmode)
12044 > (unsigned int) (GET_MODE_PRECISION (tmode)
12045 - GET_MODE_PRECISION (mode)))
12046 && (num_sign_bit_copies (op1, tmode)
12047 > (unsigned int) (GET_MODE_PRECISION (tmode)
12048 - GET_MODE_PRECISION (mode)))))
12050 /* If OP0 is an AND and we don't have an AND in MODE either,
12051 make a new AND in the proper mode. */
12052 if (GET_CODE (op0) == AND
12053 && !have_insn_for (AND, mode))
12054 op0 = simplify_gen_binary (AND, tmode,
12055 gen_lowpart (tmode,
12056 XEXP (op0, 0)),
12057 gen_lowpart (tmode,
12058 XEXP (op0, 1)));
12059 else
12061 if (zero_extended)
12063 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12064 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12066 else
12068 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12069 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12071 break;
12076 /* We may have changed the comparison operands. Re-canonicalize. */
12077 if (swap_commutative_operands_p (op0, op1))
12079 tem = op0, op0 = op1, op1 = tem;
12080 code = swap_condition (code);
12083 /* If this machine only supports a subset of valid comparisons, see if we
12084 can convert an unsupported one into a supported one. */
12085 target_canonicalize_comparison (&code, &op0, &op1, 0);
12087 *pop0 = op0;
12088 *pop1 = op1;
12090 return code;
12093 /* Utility function for record_value_for_reg. Count number of
12094 rtxs in X. */
12095 static int
12096 count_rtxs (rtx x)
12098 enum rtx_code code = GET_CODE (x);
12099 const char *fmt;
12100 int i, j, ret = 1;
12102 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12103 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12105 rtx x0 = XEXP (x, 0);
12106 rtx x1 = XEXP (x, 1);
12108 if (x0 == x1)
12109 return 1 + 2 * count_rtxs (x0);
12111 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12112 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12113 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12114 return 2 + 2 * count_rtxs (x0)
12115 + count_rtxs (x == XEXP (x1, 0)
12116 ? XEXP (x1, 1) : XEXP (x1, 0));
12118 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12119 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12120 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12121 return 2 + 2 * count_rtxs (x1)
12122 + count_rtxs (x == XEXP (x0, 0)
12123 ? XEXP (x0, 1) : XEXP (x0, 0));
12126 fmt = GET_RTX_FORMAT (code);
12127 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12128 if (fmt[i] == 'e')
12129 ret += count_rtxs (XEXP (x, i));
12130 else if (fmt[i] == 'E')
12131 for (j = 0; j < XVECLEN (x, i); j++)
12132 ret += count_rtxs (XVECEXP (x, i, j));
12134 return ret;
12137 /* Utility function for following routine. Called when X is part of a value
12138 being stored into last_set_value. Sets last_set_table_tick
12139 for each register mentioned. Similar to mention_regs in cse.c */
12141 static void
12142 update_table_tick (rtx x)
12144 enum rtx_code code = GET_CODE (x);
12145 const char *fmt = GET_RTX_FORMAT (code);
12146 int i, j;
12148 if (code == REG)
12150 unsigned int regno = REGNO (x);
12151 unsigned int endregno = END_REGNO (x);
12152 unsigned int r;
12154 for (r = regno; r < endregno; r++)
12156 reg_stat_type *rsp = &reg_stat[r];
12157 rsp->last_set_table_tick = label_tick;
12160 return;
12163 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12164 if (fmt[i] == 'e')
12166 /* Check for identical subexpressions. If x contains
12167 identical subexpression we only have to traverse one of
12168 them. */
12169 if (i == 0 && ARITHMETIC_P (x))
12171 /* Note that at this point x1 has already been
12172 processed. */
12173 rtx x0 = XEXP (x, 0);
12174 rtx x1 = XEXP (x, 1);
12176 /* If x0 and x1 are identical then there is no need to
12177 process x0. */
12178 if (x0 == x1)
12179 break;
12181 /* If x0 is identical to a subexpression of x1 then while
12182 processing x1, x0 has already been processed. Thus we
12183 are done with x. */
12184 if (ARITHMETIC_P (x1)
12185 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12186 break;
12188 /* If x1 is identical to a subexpression of x0 then we
12189 still have to process the rest of x0. */
12190 if (ARITHMETIC_P (x0)
12191 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12193 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12194 break;
12198 update_table_tick (XEXP (x, i));
12200 else if (fmt[i] == 'E')
12201 for (j = 0; j < XVECLEN (x, i); j++)
12202 update_table_tick (XVECEXP (x, i, j));
12205 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12206 are saying that the register is clobbered and we no longer know its
12207 value. If INSN is zero, don't update reg_stat[].last_set; this is
12208 only permitted with VALUE also zero and is used to invalidate the
12209 register. */
12211 static void
12212 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12214 unsigned int regno = REGNO (reg);
12215 unsigned int endregno = END_REGNO (reg);
12216 unsigned int i;
12217 reg_stat_type *rsp;
12219 /* If VALUE contains REG and we have a previous value for REG, substitute
12220 the previous value. */
12221 if (value && insn && reg_overlap_mentioned_p (reg, value))
12223 rtx tem;
12225 /* Set things up so get_last_value is allowed to see anything set up to
12226 our insn. */
12227 subst_low_luid = DF_INSN_LUID (insn);
12228 tem = get_last_value (reg);
12230 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12231 it isn't going to be useful and will take a lot of time to process,
12232 so just use the CLOBBER. */
12234 if (tem)
12236 if (ARITHMETIC_P (tem)
12237 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12238 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12239 tem = XEXP (tem, 0);
12240 else if (count_occurrences (value, reg, 1) >= 2)
12242 /* If there are two or more occurrences of REG in VALUE,
12243 prevent the value from growing too much. */
12244 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12245 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12248 value = replace_rtx (copy_rtx (value), reg, tem);
12252 /* For each register modified, show we don't know its value, that
12253 we don't know about its bitwise content, that its value has been
12254 updated, and that we don't know the location of the death of the
12255 register. */
12256 for (i = regno; i < endregno; i++)
12258 rsp = &reg_stat[i];
12260 if (insn)
12261 rsp->last_set = insn;
12263 rsp->last_set_value = 0;
12264 rsp->last_set_mode = VOIDmode;
12265 rsp->last_set_nonzero_bits = 0;
12266 rsp->last_set_sign_bit_copies = 0;
12267 rsp->last_death = 0;
12268 rsp->truncated_to_mode = VOIDmode;
12271 /* Mark registers that are being referenced in this value. */
12272 if (value)
12273 update_table_tick (value);
12275 /* Now update the status of each register being set.
12276 If someone is using this register in this block, set this register
12277 to invalid since we will get confused between the two lives in this
12278 basic block. This makes using this register always invalid. In cse, we
12279 scan the table to invalidate all entries using this register, but this
12280 is too much work for us. */
12282 for (i = regno; i < endregno; i++)
12284 rsp = &reg_stat[i];
12285 rsp->last_set_label = label_tick;
12286 if (!insn
12287 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12288 rsp->last_set_invalid = 1;
12289 else
12290 rsp->last_set_invalid = 0;
12293 /* The value being assigned might refer to X (like in "x++;"). In that
12294 case, we must replace it with (clobber (const_int 0)) to prevent
12295 infinite loops. */
12296 rsp = &reg_stat[regno];
12297 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12299 value = copy_rtx (value);
12300 if (!get_last_value_validate (&value, insn, label_tick, 1))
12301 value = 0;
12304 /* For the main register being modified, update the value, the mode, the
12305 nonzero bits, and the number of sign bit copies. */
12307 rsp->last_set_value = value;
12309 if (value)
12311 enum machine_mode mode = GET_MODE (reg);
12312 subst_low_luid = DF_INSN_LUID (insn);
12313 rsp->last_set_mode = mode;
12314 if (GET_MODE_CLASS (mode) == MODE_INT
12315 && HWI_COMPUTABLE_MODE_P (mode))
12316 mode = nonzero_bits_mode;
12317 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12318 rsp->last_set_sign_bit_copies
12319 = num_sign_bit_copies (value, GET_MODE (reg));
12323 /* Called via note_stores from record_dead_and_set_regs to handle one
12324 SET or CLOBBER in an insn. DATA is the instruction in which the
12325 set is occurring. */
12327 static void
12328 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12330 rtx_insn *record_dead_insn = (rtx_insn *) data;
12332 if (GET_CODE (dest) == SUBREG)
12333 dest = SUBREG_REG (dest);
12335 if (!record_dead_insn)
12337 if (REG_P (dest))
12338 record_value_for_reg (dest, NULL, NULL_RTX);
12339 return;
12342 if (REG_P (dest))
12344 /* If we are setting the whole register, we know its value. Otherwise
12345 show that we don't know the value. We can handle SUBREG in
12346 some cases. */
12347 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12348 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12349 else if (GET_CODE (setter) == SET
12350 && GET_CODE (SET_DEST (setter)) == SUBREG
12351 && SUBREG_REG (SET_DEST (setter)) == dest
12352 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12353 && subreg_lowpart_p (SET_DEST (setter)))
12354 record_value_for_reg (dest, record_dead_insn,
12355 gen_lowpart (GET_MODE (dest),
12356 SET_SRC (setter)));
12357 else
12358 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12360 else if (MEM_P (dest)
12361 /* Ignore pushes, they clobber nothing. */
12362 && ! push_operand (dest, GET_MODE (dest)))
12363 mem_last_set = DF_INSN_LUID (record_dead_insn);
12366 /* Update the records of when each REG was most recently set or killed
12367 for the things done by INSN. This is the last thing done in processing
12368 INSN in the combiner loop.
12370 We update reg_stat[], in particular fields last_set, last_set_value,
12371 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12372 last_death, and also the similar information mem_last_set (which insn
12373 most recently modified memory) and last_call_luid (which insn was the
12374 most recent subroutine call). */
12376 static void
12377 record_dead_and_set_regs (rtx_insn *insn)
12379 rtx link;
12380 unsigned int i;
12382 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12384 if (REG_NOTE_KIND (link) == REG_DEAD
12385 && REG_P (XEXP (link, 0)))
12387 unsigned int regno = REGNO (XEXP (link, 0));
12388 unsigned int endregno = END_REGNO (XEXP (link, 0));
12390 for (i = regno; i < endregno; i++)
12392 reg_stat_type *rsp;
12394 rsp = &reg_stat[i];
12395 rsp->last_death = insn;
12398 else if (REG_NOTE_KIND (link) == REG_INC)
12399 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12402 if (CALL_P (insn))
12404 hard_reg_set_iterator hrsi;
12405 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12407 reg_stat_type *rsp;
12409 rsp = &reg_stat[i];
12410 rsp->last_set_invalid = 1;
12411 rsp->last_set = insn;
12412 rsp->last_set_value = 0;
12413 rsp->last_set_mode = VOIDmode;
12414 rsp->last_set_nonzero_bits = 0;
12415 rsp->last_set_sign_bit_copies = 0;
12416 rsp->last_death = 0;
12417 rsp->truncated_to_mode = VOIDmode;
12420 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12422 /* We can't combine into a call pattern. Remember, though, that
12423 the return value register is set at this LUID. We could
12424 still replace a register with the return value from the
12425 wrong subroutine call! */
12426 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12428 else
12429 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12432 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12433 register present in the SUBREG, so for each such SUBREG go back and
12434 adjust nonzero and sign bit information of the registers that are
12435 known to have some zero/sign bits set.
12437 This is needed because when combine blows the SUBREGs away, the
12438 information on zero/sign bits is lost and further combines can be
12439 missed because of that. */
12441 static void
12442 record_promoted_value (rtx_insn *insn, rtx subreg)
12444 struct insn_link *links;
12445 rtx set;
12446 unsigned int regno = REGNO (SUBREG_REG (subreg));
12447 enum machine_mode mode = GET_MODE (subreg);
12449 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12450 return;
12452 for (links = LOG_LINKS (insn); links;)
12454 reg_stat_type *rsp;
12456 insn = links->insn;
12457 set = single_set (insn);
12459 if (! set || !REG_P (SET_DEST (set))
12460 || REGNO (SET_DEST (set)) != regno
12461 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12463 links = links->next;
12464 continue;
12467 rsp = &reg_stat[regno];
12468 if (rsp->last_set == insn)
12470 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12471 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12474 if (REG_P (SET_SRC (set)))
12476 regno = REGNO (SET_SRC (set));
12477 links = LOG_LINKS (insn);
12479 else
12480 break;
12484 /* Check if X, a register, is known to contain a value already
12485 truncated to MODE. In this case we can use a subreg to refer to
12486 the truncated value even though in the generic case we would need
12487 an explicit truncation. */
12489 static bool
12490 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12492 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12493 enum machine_mode truncated = rsp->truncated_to_mode;
12495 if (truncated == 0
12496 || rsp->truncation_label < label_tick_ebb_start)
12497 return false;
12498 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12499 return true;
12500 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12501 return true;
12502 return false;
12505 /* If X is a hard reg or a subreg record the mode that the register is
12506 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12507 to turn a truncate into a subreg using this information. Return true
12508 if traversing X is complete. */
12510 static bool
12511 record_truncated_value (rtx x)
12513 enum machine_mode truncated_mode;
12514 reg_stat_type *rsp;
12516 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12518 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12519 truncated_mode = GET_MODE (x);
12521 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12522 return true;
12524 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12525 return true;
12527 x = SUBREG_REG (x);
12529 /* ??? For hard-regs we now record everything. We might be able to
12530 optimize this using last_set_mode. */
12531 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12532 truncated_mode = GET_MODE (x);
12533 else
12534 return false;
12536 rsp = &reg_stat[REGNO (x)];
12537 if (rsp->truncated_to_mode == 0
12538 || rsp->truncation_label < label_tick_ebb_start
12539 || (GET_MODE_SIZE (truncated_mode)
12540 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12542 rsp->truncated_to_mode = truncated_mode;
12543 rsp->truncation_label = label_tick;
12546 return true;
12549 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12550 the modes they are used in. This can help truning TRUNCATEs into
12551 SUBREGs. */
12553 static void
12554 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12556 subrtx_var_iterator::array_type array;
12557 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12558 if (record_truncated_value (*iter))
12559 iter.skip_subrtxes ();
12562 /* Scan X for promoted SUBREGs. For each one found,
12563 note what it implies to the registers used in it. */
12565 static void
12566 check_promoted_subreg (rtx_insn *insn, rtx x)
12568 if (GET_CODE (x) == SUBREG
12569 && SUBREG_PROMOTED_VAR_P (x)
12570 && REG_P (SUBREG_REG (x)))
12571 record_promoted_value (insn, x);
12572 else
12574 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12575 int i, j;
12577 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12578 switch (format[i])
12580 case 'e':
12581 check_promoted_subreg (insn, XEXP (x, i));
12582 break;
12583 case 'V':
12584 case 'E':
12585 if (XVEC (x, i) != 0)
12586 for (j = 0; j < XVECLEN (x, i); j++)
12587 check_promoted_subreg (insn, XVECEXP (x, i, j));
12588 break;
12593 /* Verify that all the registers and memory references mentioned in *LOC are
12594 still valid. *LOC was part of a value set in INSN when label_tick was
12595 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12596 the invalid references with (clobber (const_int 0)) and return 1. This
12597 replacement is useful because we often can get useful information about
12598 the form of a value (e.g., if it was produced by a shift that always
12599 produces -1 or 0) even though we don't know exactly what registers it
12600 was produced from. */
12602 static int
12603 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12605 rtx x = *loc;
12606 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12607 int len = GET_RTX_LENGTH (GET_CODE (x));
12608 int i, j;
12610 if (REG_P (x))
12612 unsigned int regno = REGNO (x);
12613 unsigned int endregno = END_REGNO (x);
12614 unsigned int j;
12616 for (j = regno; j < endregno; j++)
12618 reg_stat_type *rsp = &reg_stat[j];
12619 if (rsp->last_set_invalid
12620 /* If this is a pseudo-register that was only set once and not
12621 live at the beginning of the function, it is always valid. */
12622 || (! (regno >= FIRST_PSEUDO_REGISTER
12623 && REG_N_SETS (regno) == 1
12624 && (!REGNO_REG_SET_P
12625 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12626 regno)))
12627 && rsp->last_set_label > tick))
12629 if (replace)
12630 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12631 return replace;
12635 return 1;
12637 /* If this is a memory reference, make sure that there were no stores after
12638 it that might have clobbered the value. We don't have alias info, so we
12639 assume any store invalidates it. Moreover, we only have local UIDs, so
12640 we also assume that there were stores in the intervening basic blocks. */
12641 else if (MEM_P (x) && !MEM_READONLY_P (x)
12642 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12644 if (replace)
12645 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12646 return replace;
12649 for (i = 0; i < len; i++)
12651 if (fmt[i] == 'e')
12653 /* Check for identical subexpressions. If x contains
12654 identical subexpression we only have to traverse one of
12655 them. */
12656 if (i == 1 && ARITHMETIC_P (x))
12658 /* Note that at this point x0 has already been checked
12659 and found valid. */
12660 rtx x0 = XEXP (x, 0);
12661 rtx x1 = XEXP (x, 1);
12663 /* If x0 and x1 are identical then x is also valid. */
12664 if (x0 == x1)
12665 return 1;
12667 /* If x1 is identical to a subexpression of x0 then
12668 while checking x0, x1 has already been checked. Thus
12669 it is valid and so as x. */
12670 if (ARITHMETIC_P (x0)
12671 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12672 return 1;
12674 /* If x0 is identical to a subexpression of x1 then x is
12675 valid iff the rest of x1 is valid. */
12676 if (ARITHMETIC_P (x1)
12677 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12678 return
12679 get_last_value_validate (&XEXP (x1,
12680 x0 == XEXP (x1, 0) ? 1 : 0),
12681 insn, tick, replace);
12684 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12685 replace) == 0)
12686 return 0;
12688 else if (fmt[i] == 'E')
12689 for (j = 0; j < XVECLEN (x, i); j++)
12690 if (get_last_value_validate (&XVECEXP (x, i, j),
12691 insn, tick, replace) == 0)
12692 return 0;
12695 /* If we haven't found a reason for it to be invalid, it is valid. */
12696 return 1;
12699 /* Get the last value assigned to X, if known. Some registers
12700 in the value may be replaced with (clobber (const_int 0)) if their value
12701 is known longer known reliably. */
12703 static rtx
12704 get_last_value (const_rtx x)
12706 unsigned int regno;
12707 rtx value;
12708 reg_stat_type *rsp;
12710 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12711 then convert it to the desired mode. If this is a paradoxical SUBREG,
12712 we cannot predict what values the "extra" bits might have. */
12713 if (GET_CODE (x) == SUBREG
12714 && subreg_lowpart_p (x)
12715 && !paradoxical_subreg_p (x)
12716 && (value = get_last_value (SUBREG_REG (x))) != 0)
12717 return gen_lowpart (GET_MODE (x), value);
12719 if (!REG_P (x))
12720 return 0;
12722 regno = REGNO (x);
12723 rsp = &reg_stat[regno];
12724 value = rsp->last_set_value;
12726 /* If we don't have a value, or if it isn't for this basic block and
12727 it's either a hard register, set more than once, or it's a live
12728 at the beginning of the function, return 0.
12730 Because if it's not live at the beginning of the function then the reg
12731 is always set before being used (is never used without being set).
12732 And, if it's set only once, and it's always set before use, then all
12733 uses must have the same last value, even if it's not from this basic
12734 block. */
12736 if (value == 0
12737 || (rsp->last_set_label < label_tick_ebb_start
12738 && (regno < FIRST_PSEUDO_REGISTER
12739 || REG_N_SETS (regno) != 1
12740 || REGNO_REG_SET_P
12741 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12742 return 0;
12744 /* If the value was set in a later insn than the ones we are processing,
12745 we can't use it even if the register was only set once. */
12746 if (rsp->last_set_label == label_tick
12747 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12748 return 0;
12750 /* If the value has all its registers valid, return it. */
12751 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12752 return value;
12754 /* Otherwise, make a copy and replace any invalid register with
12755 (clobber (const_int 0)). If that fails for some reason, return 0. */
12757 value = copy_rtx (value);
12758 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12759 return value;
12761 return 0;
12764 /* Return nonzero if expression X refers to a REG or to memory
12765 that is set in an instruction more recent than FROM_LUID. */
12767 static int
12768 use_crosses_set_p (const_rtx x, int from_luid)
12770 const char *fmt;
12771 int i;
12772 enum rtx_code code = GET_CODE (x);
12774 if (code == REG)
12776 unsigned int regno = REGNO (x);
12777 unsigned endreg = END_REGNO (x);
12779 #ifdef PUSH_ROUNDING
12780 /* Don't allow uses of the stack pointer to be moved,
12781 because we don't know whether the move crosses a push insn. */
12782 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12783 return 1;
12784 #endif
12785 for (; regno < endreg; regno++)
12787 reg_stat_type *rsp = &reg_stat[regno];
12788 if (rsp->last_set
12789 && rsp->last_set_label == label_tick
12790 && DF_INSN_LUID (rsp->last_set) > from_luid)
12791 return 1;
12793 return 0;
12796 if (code == MEM && mem_last_set > from_luid)
12797 return 1;
12799 fmt = GET_RTX_FORMAT (code);
12801 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12803 if (fmt[i] == 'E')
12805 int j;
12806 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12807 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12808 return 1;
12810 else if (fmt[i] == 'e'
12811 && use_crosses_set_p (XEXP (x, i), from_luid))
12812 return 1;
12814 return 0;
12817 /* Define three variables used for communication between the following
12818 routines. */
12820 static unsigned int reg_dead_regno, reg_dead_endregno;
12821 static int reg_dead_flag;
12823 /* Function called via note_stores from reg_dead_at_p.
12825 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12826 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12828 static void
12829 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12831 unsigned int regno, endregno;
12833 if (!REG_P (dest))
12834 return;
12836 regno = REGNO (dest);
12837 endregno = END_REGNO (dest);
12838 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12839 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12842 /* Return nonzero if REG is known to be dead at INSN.
12844 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12845 referencing REG, it is dead. If we hit a SET referencing REG, it is
12846 live. Otherwise, see if it is live or dead at the start of the basic
12847 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12848 must be assumed to be always live. */
12850 static int
12851 reg_dead_at_p (rtx reg, rtx_insn *insn)
12853 basic_block block;
12854 unsigned int i;
12856 /* Set variables for reg_dead_at_p_1. */
12857 reg_dead_regno = REGNO (reg);
12858 reg_dead_endregno = END_REGNO (reg);
12860 reg_dead_flag = 0;
12862 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12863 we allow the machine description to decide whether use-and-clobber
12864 patterns are OK. */
12865 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12867 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12868 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12869 return 0;
12872 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12873 beginning of basic block. */
12874 block = BLOCK_FOR_INSN (insn);
12875 for (;;)
12877 if (INSN_P (insn))
12879 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12880 if (reg_dead_flag)
12881 return reg_dead_flag == 1 ? 1 : 0;
12883 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12884 return 1;
12887 if (insn == BB_HEAD (block))
12888 break;
12890 insn = PREV_INSN (insn);
12893 /* Look at live-in sets for the basic block that we were in. */
12894 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12895 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12896 return 0;
12898 return 1;
12901 /* Note hard registers in X that are used. */
12903 static void
12904 mark_used_regs_combine (rtx x)
12906 RTX_CODE code = GET_CODE (x);
12907 unsigned int regno;
12908 int i;
12910 switch (code)
12912 case LABEL_REF:
12913 case SYMBOL_REF:
12914 case CONST:
12915 CASE_CONST_ANY:
12916 case PC:
12917 case ADDR_VEC:
12918 case ADDR_DIFF_VEC:
12919 case ASM_INPUT:
12920 #ifdef HAVE_cc0
12921 /* CC0 must die in the insn after it is set, so we don't need to take
12922 special note of it here. */
12923 case CC0:
12924 #endif
12925 return;
12927 case CLOBBER:
12928 /* If we are clobbering a MEM, mark any hard registers inside the
12929 address as used. */
12930 if (MEM_P (XEXP (x, 0)))
12931 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12932 return;
12934 case REG:
12935 regno = REGNO (x);
12936 /* A hard reg in a wide mode may really be multiple registers.
12937 If so, mark all of them just like the first. */
12938 if (regno < FIRST_PSEUDO_REGISTER)
12940 /* None of this applies to the stack, frame or arg pointers. */
12941 if (regno == STACK_POINTER_REGNUM
12942 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12943 || regno == HARD_FRAME_POINTER_REGNUM
12944 #endif
12945 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12946 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12947 #endif
12948 || regno == FRAME_POINTER_REGNUM)
12949 return;
12951 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12953 return;
12955 case SET:
12957 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12958 the address. */
12959 rtx testreg = SET_DEST (x);
12961 while (GET_CODE (testreg) == SUBREG
12962 || GET_CODE (testreg) == ZERO_EXTRACT
12963 || GET_CODE (testreg) == STRICT_LOW_PART)
12964 testreg = XEXP (testreg, 0);
12966 if (MEM_P (testreg))
12967 mark_used_regs_combine (XEXP (testreg, 0));
12969 mark_used_regs_combine (SET_SRC (x));
12971 return;
12973 default:
12974 break;
12977 /* Recursively scan the operands of this expression. */
12980 const char *fmt = GET_RTX_FORMAT (code);
12982 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12984 if (fmt[i] == 'e')
12985 mark_used_regs_combine (XEXP (x, i));
12986 else if (fmt[i] == 'E')
12988 int j;
12990 for (j = 0; j < XVECLEN (x, i); j++)
12991 mark_used_regs_combine (XVECEXP (x, i, j));
12997 /* Remove register number REGNO from the dead registers list of INSN.
12999 Return the note used to record the death, if there was one. */
13002 remove_death (unsigned int regno, rtx_insn *insn)
13004 rtx note = find_regno_note (insn, REG_DEAD, regno);
13006 if (note)
13007 remove_note (insn, note);
13009 return note;
13012 /* For each register (hardware or pseudo) used within expression X, if its
13013 death is in an instruction with luid between FROM_LUID (inclusive) and
13014 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13015 list headed by PNOTES.
13017 That said, don't move registers killed by maybe_kill_insn.
13019 This is done when X is being merged by combination into TO_INSN. These
13020 notes will then be distributed as needed. */
13022 static void
13023 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13024 rtx *pnotes)
13026 const char *fmt;
13027 int len, i;
13028 enum rtx_code code = GET_CODE (x);
13030 if (code == REG)
13032 unsigned int regno = REGNO (x);
13033 rtx_insn *where_dead = reg_stat[regno].last_death;
13035 /* Don't move the register if it gets killed in between from and to. */
13036 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13037 && ! reg_referenced_p (x, maybe_kill_insn))
13038 return;
13040 if (where_dead
13041 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13042 && DF_INSN_LUID (where_dead) >= from_luid
13043 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13045 rtx note = remove_death (regno, where_dead);
13047 /* It is possible for the call above to return 0. This can occur
13048 when last_death points to I2 or I1 that we combined with.
13049 In that case make a new note.
13051 We must also check for the case where X is a hard register
13052 and NOTE is a death note for a range of hard registers
13053 including X. In that case, we must put REG_DEAD notes for
13054 the remaining registers in place of NOTE. */
13056 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13057 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13058 > GET_MODE_SIZE (GET_MODE (x))))
13060 unsigned int deadregno = REGNO (XEXP (note, 0));
13061 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13062 unsigned int ourend = END_HARD_REGNO (x);
13063 unsigned int i;
13065 for (i = deadregno; i < deadend; i++)
13066 if (i < regno || i >= ourend)
13067 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13070 /* If we didn't find any note, or if we found a REG_DEAD note that
13071 covers only part of the given reg, and we have a multi-reg hard
13072 register, then to be safe we must check for REG_DEAD notes
13073 for each register other than the first. They could have
13074 their own REG_DEAD notes lying around. */
13075 else if ((note == 0
13076 || (note != 0
13077 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13078 < GET_MODE_SIZE (GET_MODE (x)))))
13079 && regno < FIRST_PSEUDO_REGISTER
13080 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13082 unsigned int ourend = END_HARD_REGNO (x);
13083 unsigned int i, offset;
13084 rtx oldnotes = 0;
13086 if (note)
13087 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13088 else
13089 offset = 1;
13091 for (i = regno + offset; i < ourend; i++)
13092 move_deaths (regno_reg_rtx[i],
13093 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13096 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13098 XEXP (note, 1) = *pnotes;
13099 *pnotes = note;
13101 else
13102 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13105 return;
13108 else if (GET_CODE (x) == SET)
13110 rtx dest = SET_DEST (x);
13112 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13114 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13115 that accesses one word of a multi-word item, some
13116 piece of everything register in the expression is used by
13117 this insn, so remove any old death. */
13118 /* ??? So why do we test for equality of the sizes? */
13120 if (GET_CODE (dest) == ZERO_EXTRACT
13121 || GET_CODE (dest) == STRICT_LOW_PART
13122 || (GET_CODE (dest) == SUBREG
13123 && (((GET_MODE_SIZE (GET_MODE (dest))
13124 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13125 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13126 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13128 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13129 return;
13132 /* If this is some other SUBREG, we know it replaces the entire
13133 value, so use that as the destination. */
13134 if (GET_CODE (dest) == SUBREG)
13135 dest = SUBREG_REG (dest);
13137 /* If this is a MEM, adjust deaths of anything used in the address.
13138 For a REG (the only other possibility), the entire value is
13139 being replaced so the old value is not used in this insn. */
13141 if (MEM_P (dest))
13142 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13143 to_insn, pnotes);
13144 return;
13147 else if (GET_CODE (x) == CLOBBER)
13148 return;
13150 len = GET_RTX_LENGTH (code);
13151 fmt = GET_RTX_FORMAT (code);
13153 for (i = 0; i < len; i++)
13155 if (fmt[i] == 'E')
13157 int j;
13158 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13159 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13160 to_insn, pnotes);
13162 else if (fmt[i] == 'e')
13163 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13167 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13168 pattern of an insn. X must be a REG. */
13170 static int
13171 reg_bitfield_target_p (rtx x, rtx body)
13173 int i;
13175 if (GET_CODE (body) == SET)
13177 rtx dest = SET_DEST (body);
13178 rtx target;
13179 unsigned int regno, tregno, endregno, endtregno;
13181 if (GET_CODE (dest) == ZERO_EXTRACT)
13182 target = XEXP (dest, 0);
13183 else if (GET_CODE (dest) == STRICT_LOW_PART)
13184 target = SUBREG_REG (XEXP (dest, 0));
13185 else
13186 return 0;
13188 if (GET_CODE (target) == SUBREG)
13189 target = SUBREG_REG (target);
13191 if (!REG_P (target))
13192 return 0;
13194 tregno = REGNO (target), regno = REGNO (x);
13195 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13196 return target == x;
13198 endtregno = end_hard_regno (GET_MODE (target), tregno);
13199 endregno = end_hard_regno (GET_MODE (x), regno);
13201 return endregno > tregno && regno < endtregno;
13204 else if (GET_CODE (body) == PARALLEL)
13205 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13206 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13207 return 1;
13209 return 0;
13212 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13213 as appropriate. I3 and I2 are the insns resulting from the combination
13214 insns including FROM (I2 may be zero).
13216 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13217 not need REG_DEAD notes because they are being substituted for. This
13218 saves searching in the most common cases.
13220 Each note in the list is either ignored or placed on some insns, depending
13221 on the type of note. */
13223 static void
13224 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13225 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13227 rtx note, next_note;
13228 rtx tem_note;
13229 rtx_insn *tem_insn;
13231 for (note = notes; note; note = next_note)
13233 rtx_insn *place = 0, *place2 = 0;
13235 next_note = XEXP (note, 1);
13236 switch (REG_NOTE_KIND (note))
13238 case REG_BR_PROB:
13239 case REG_BR_PRED:
13240 /* Doesn't matter much where we put this, as long as it's somewhere.
13241 It is preferable to keep these notes on branches, which is most
13242 likely to be i3. */
13243 place = i3;
13244 break;
13246 case REG_NON_LOCAL_GOTO:
13247 if (JUMP_P (i3))
13248 place = i3;
13249 else
13251 gcc_assert (i2 && JUMP_P (i2));
13252 place = i2;
13254 break;
13256 case REG_EH_REGION:
13257 /* These notes must remain with the call or trapping instruction. */
13258 if (CALL_P (i3))
13259 place = i3;
13260 else if (i2 && CALL_P (i2))
13261 place = i2;
13262 else
13264 gcc_assert (cfun->can_throw_non_call_exceptions);
13265 if (may_trap_p (i3))
13266 place = i3;
13267 else if (i2 && may_trap_p (i2))
13268 place = i2;
13269 /* ??? Otherwise assume we've combined things such that we
13270 can now prove that the instructions can't trap. Drop the
13271 note in this case. */
13273 break;
13275 case REG_ARGS_SIZE:
13276 /* ??? How to distribute between i3-i1. Assume i3 contains the
13277 entire adjustment. Assert i3 contains at least some adjust. */
13278 if (!noop_move_p (i3))
13280 int old_size, args_size = INTVAL (XEXP (note, 0));
13281 /* fixup_args_size_notes looks at REG_NORETURN note,
13282 so ensure the note is placed there first. */
13283 if (CALL_P (i3))
13285 rtx *np;
13286 for (np = &next_note; *np; np = &XEXP (*np, 1))
13287 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13289 rtx n = *np;
13290 *np = XEXP (n, 1);
13291 XEXP (n, 1) = REG_NOTES (i3);
13292 REG_NOTES (i3) = n;
13293 break;
13296 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13297 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13298 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13299 gcc_assert (old_size != args_size
13300 || (CALL_P (i3)
13301 && !ACCUMULATE_OUTGOING_ARGS
13302 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13304 break;
13306 case REG_NORETURN:
13307 case REG_SETJMP:
13308 case REG_TM:
13309 case REG_CALL_DECL:
13310 /* These notes must remain with the call. It should not be
13311 possible for both I2 and I3 to be a call. */
13312 if (CALL_P (i3))
13313 place = i3;
13314 else
13316 gcc_assert (i2 && CALL_P (i2));
13317 place = i2;
13319 break;
13321 case REG_UNUSED:
13322 /* Any clobbers for i3 may still exist, and so we must process
13323 REG_UNUSED notes from that insn.
13325 Any clobbers from i2 or i1 can only exist if they were added by
13326 recog_for_combine. In that case, recog_for_combine created the
13327 necessary REG_UNUSED notes. Trying to keep any original
13328 REG_UNUSED notes from these insns can cause incorrect output
13329 if it is for the same register as the original i3 dest.
13330 In that case, we will notice that the register is set in i3,
13331 and then add a REG_UNUSED note for the destination of i3, which
13332 is wrong. However, it is possible to have REG_UNUSED notes from
13333 i2 or i1 for register which were both used and clobbered, so
13334 we keep notes from i2 or i1 if they will turn into REG_DEAD
13335 notes. */
13337 /* If this register is set or clobbered in I3, put the note there
13338 unless there is one already. */
13339 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13341 if (from_insn != i3)
13342 break;
13344 if (! (REG_P (XEXP (note, 0))
13345 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13346 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13347 place = i3;
13349 /* Otherwise, if this register is used by I3, then this register
13350 now dies here, so we must put a REG_DEAD note here unless there
13351 is one already. */
13352 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13353 && ! (REG_P (XEXP (note, 0))
13354 ? find_regno_note (i3, REG_DEAD,
13355 REGNO (XEXP (note, 0)))
13356 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13358 PUT_REG_NOTE_KIND (note, REG_DEAD);
13359 place = i3;
13361 break;
13363 case REG_EQUAL:
13364 case REG_EQUIV:
13365 case REG_NOALIAS:
13366 /* These notes say something about results of an insn. We can
13367 only support them if they used to be on I3 in which case they
13368 remain on I3. Otherwise they are ignored.
13370 If the note refers to an expression that is not a constant, we
13371 must also ignore the note since we cannot tell whether the
13372 equivalence is still true. It might be possible to do
13373 slightly better than this (we only have a problem if I2DEST
13374 or I1DEST is present in the expression), but it doesn't
13375 seem worth the trouble. */
13377 if (from_insn == i3
13378 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13379 place = i3;
13380 break;
13382 case REG_INC:
13383 /* These notes say something about how a register is used. They must
13384 be present on any use of the register in I2 or I3. */
13385 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13386 place = i3;
13388 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13390 if (place)
13391 place2 = i2;
13392 else
13393 place = i2;
13395 break;
13397 case REG_LABEL_TARGET:
13398 case REG_LABEL_OPERAND:
13399 /* This can show up in several ways -- either directly in the
13400 pattern, or hidden off in the constant pool with (or without?)
13401 a REG_EQUAL note. */
13402 /* ??? Ignore the without-reg_equal-note problem for now. */
13403 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13404 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13405 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13406 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13407 place = i3;
13409 if (i2
13410 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13411 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13412 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13413 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13415 if (place)
13416 place2 = i2;
13417 else
13418 place = i2;
13421 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13422 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13423 there. */
13424 if (place && JUMP_P (place)
13425 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13426 && (JUMP_LABEL (place) == NULL
13427 || JUMP_LABEL (place) == XEXP (note, 0)))
13429 rtx label = JUMP_LABEL (place);
13431 if (!label)
13432 JUMP_LABEL (place) = XEXP (note, 0);
13433 else if (LABEL_P (label))
13434 LABEL_NUSES (label)--;
13437 if (place2 && JUMP_P (place2)
13438 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13439 && (JUMP_LABEL (place2) == NULL
13440 || JUMP_LABEL (place2) == XEXP (note, 0)))
13442 rtx label = JUMP_LABEL (place2);
13444 if (!label)
13445 JUMP_LABEL (place2) = XEXP (note, 0);
13446 else if (LABEL_P (label))
13447 LABEL_NUSES (label)--;
13448 place2 = 0;
13450 break;
13452 case REG_NONNEG:
13453 /* This note says something about the value of a register prior
13454 to the execution of an insn. It is too much trouble to see
13455 if the note is still correct in all situations. It is better
13456 to simply delete it. */
13457 break;
13459 case REG_DEAD:
13460 /* If we replaced the right hand side of FROM_INSN with a
13461 REG_EQUAL note, the original use of the dying register
13462 will not have been combined into I3 and I2. In such cases,
13463 FROM_INSN is guaranteed to be the first of the combined
13464 instructions, so we simply need to search back before
13465 FROM_INSN for the previous use or set of this register,
13466 then alter the notes there appropriately.
13468 If the register is used as an input in I3, it dies there.
13469 Similarly for I2, if it is nonzero and adjacent to I3.
13471 If the register is not used as an input in either I3 or I2
13472 and it is not one of the registers we were supposed to eliminate,
13473 there are two possibilities. We might have a non-adjacent I2
13474 or we might have somehow eliminated an additional register
13475 from a computation. For example, we might have had A & B where
13476 we discover that B will always be zero. In this case we will
13477 eliminate the reference to A.
13479 In both cases, we must search to see if we can find a previous
13480 use of A and put the death note there. */
13482 if (from_insn
13483 && from_insn == i2mod
13484 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13485 tem_insn = from_insn;
13486 else
13488 if (from_insn
13489 && CALL_P (from_insn)
13490 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13491 place = from_insn;
13492 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13493 place = i3;
13494 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13495 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13496 place = i2;
13497 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13498 && !(i2mod
13499 && reg_overlap_mentioned_p (XEXP (note, 0),
13500 i2mod_old_rhs)))
13501 || rtx_equal_p (XEXP (note, 0), elim_i1)
13502 || rtx_equal_p (XEXP (note, 0), elim_i0))
13503 break;
13504 tem_insn = i3;
13507 if (place == 0)
13509 basic_block bb = this_basic_block;
13511 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13513 if (!NONDEBUG_INSN_P (tem_insn))
13515 if (tem_insn == BB_HEAD (bb))
13516 break;
13517 continue;
13520 /* If the register is being set at TEM_INSN, see if that is all
13521 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13522 into a REG_UNUSED note instead. Don't delete sets to
13523 global register vars. */
13524 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13525 || !global_regs[REGNO (XEXP (note, 0))])
13526 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13528 rtx set = single_set (tem_insn);
13529 rtx inner_dest = 0;
13530 #ifdef HAVE_cc0
13531 rtx_insn *cc0_setter = NULL;
13532 #endif
13534 if (set != 0)
13535 for (inner_dest = SET_DEST (set);
13536 (GET_CODE (inner_dest) == STRICT_LOW_PART
13537 || GET_CODE (inner_dest) == SUBREG
13538 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13539 inner_dest = XEXP (inner_dest, 0))
13542 /* Verify that it was the set, and not a clobber that
13543 modified the register.
13545 CC0 targets must be careful to maintain setter/user
13546 pairs. If we cannot delete the setter due to side
13547 effects, mark the user with an UNUSED note instead
13548 of deleting it. */
13550 if (set != 0 && ! side_effects_p (SET_SRC (set))
13551 && rtx_equal_p (XEXP (note, 0), inner_dest)
13552 #ifdef HAVE_cc0
13553 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13554 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13555 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13556 #endif
13559 /* Move the notes and links of TEM_INSN elsewhere.
13560 This might delete other dead insns recursively.
13561 First set the pattern to something that won't use
13562 any register. */
13563 rtx old_notes = REG_NOTES (tem_insn);
13565 PATTERN (tem_insn) = pc_rtx;
13566 REG_NOTES (tem_insn) = NULL;
13568 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13569 NULL_RTX, NULL_RTX, NULL_RTX);
13570 distribute_links (LOG_LINKS (tem_insn));
13572 SET_INSN_DELETED (tem_insn);
13573 if (tem_insn == i2)
13574 i2 = NULL;
13576 #ifdef HAVE_cc0
13577 /* Delete the setter too. */
13578 if (cc0_setter)
13580 PATTERN (cc0_setter) = pc_rtx;
13581 old_notes = REG_NOTES (cc0_setter);
13582 REG_NOTES (cc0_setter) = NULL;
13584 distribute_notes (old_notes, cc0_setter,
13585 cc0_setter, NULL,
13586 NULL_RTX, NULL_RTX, NULL_RTX);
13587 distribute_links (LOG_LINKS (cc0_setter));
13589 SET_INSN_DELETED (cc0_setter);
13590 if (cc0_setter == i2)
13591 i2 = NULL;
13593 #endif
13595 else
13597 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13599 /* If there isn't already a REG_UNUSED note, put one
13600 here. Do not place a REG_DEAD note, even if
13601 the register is also used here; that would not
13602 match the algorithm used in lifetime analysis
13603 and can cause the consistency check in the
13604 scheduler to fail. */
13605 if (! find_regno_note (tem_insn, REG_UNUSED,
13606 REGNO (XEXP (note, 0))))
13607 place = tem_insn;
13608 break;
13611 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13612 || (CALL_P (tem_insn)
13613 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13615 place = tem_insn;
13617 /* If we are doing a 3->2 combination, and we have a
13618 register which formerly died in i3 and was not used
13619 by i2, which now no longer dies in i3 and is used in
13620 i2 but does not die in i2, and place is between i2
13621 and i3, then we may need to move a link from place to
13622 i2. */
13623 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13624 && from_insn
13625 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13626 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13628 struct insn_link *links = LOG_LINKS (place);
13629 LOG_LINKS (place) = NULL;
13630 distribute_links (links);
13632 break;
13635 if (tem_insn == BB_HEAD (bb))
13636 break;
13641 /* If the register is set or already dead at PLACE, we needn't do
13642 anything with this note if it is still a REG_DEAD note.
13643 We check here if it is set at all, not if is it totally replaced,
13644 which is what `dead_or_set_p' checks, so also check for it being
13645 set partially. */
13647 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13649 unsigned int regno = REGNO (XEXP (note, 0));
13650 reg_stat_type *rsp = &reg_stat[regno];
13652 if (dead_or_set_p (place, XEXP (note, 0))
13653 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13655 /* Unless the register previously died in PLACE, clear
13656 last_death. [I no longer understand why this is
13657 being done.] */
13658 if (rsp->last_death != place)
13659 rsp->last_death = 0;
13660 place = 0;
13662 else
13663 rsp->last_death = place;
13665 /* If this is a death note for a hard reg that is occupying
13666 multiple registers, ensure that we are still using all
13667 parts of the object. If we find a piece of the object
13668 that is unused, we must arrange for an appropriate REG_DEAD
13669 note to be added for it. However, we can't just emit a USE
13670 and tag the note to it, since the register might actually
13671 be dead; so we recourse, and the recursive call then finds
13672 the previous insn that used this register. */
13674 if (place && regno < FIRST_PSEUDO_REGISTER
13675 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13677 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13678 bool all_used = true;
13679 unsigned int i;
13681 for (i = regno; i < endregno; i++)
13682 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13683 && ! find_regno_fusage (place, USE, i))
13684 || dead_or_set_regno_p (place, i))
13686 all_used = false;
13687 break;
13690 if (! all_used)
13692 /* Put only REG_DEAD notes for pieces that are
13693 not already dead or set. */
13695 for (i = regno; i < endregno;
13696 i += hard_regno_nregs[i][reg_raw_mode[i]])
13698 rtx piece = regno_reg_rtx[i];
13699 basic_block bb = this_basic_block;
13701 if (! dead_or_set_p (place, piece)
13702 && ! reg_bitfield_target_p (piece,
13703 PATTERN (place)))
13705 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13706 NULL_RTX);
13708 distribute_notes (new_note, place, place,
13709 NULL, NULL_RTX, NULL_RTX,
13710 NULL_RTX);
13712 else if (! refers_to_regno_p (i, i + 1,
13713 PATTERN (place), 0)
13714 && ! find_regno_fusage (place, USE, i))
13715 for (tem_insn = PREV_INSN (place); ;
13716 tem_insn = PREV_INSN (tem_insn))
13718 if (!NONDEBUG_INSN_P (tem_insn))
13720 if (tem_insn == BB_HEAD (bb))
13721 break;
13722 continue;
13724 if (dead_or_set_p (tem_insn, piece)
13725 || reg_bitfield_target_p (piece,
13726 PATTERN (tem_insn)))
13728 add_reg_note (tem_insn, REG_UNUSED, piece);
13729 break;
13734 place = 0;
13738 break;
13740 default:
13741 /* Any other notes should not be present at this point in the
13742 compilation. */
13743 gcc_unreachable ();
13746 if (place)
13748 XEXP (note, 1) = REG_NOTES (place);
13749 REG_NOTES (place) = note;
13752 if (place2)
13753 add_shallow_copy_of_reg_note (place2, note);
13757 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13758 I3, I2, and I1 to new locations. This is also called to add a link
13759 pointing at I3 when I3's destination is changed. */
13761 static void
13762 distribute_links (struct insn_link *links)
13764 struct insn_link *link, *next_link;
13766 for (link = links; link; link = next_link)
13768 rtx_insn *place = 0;
13769 rtx_insn *insn;
13770 rtx set, reg;
13772 next_link = link->next;
13774 /* If the insn that this link points to is a NOTE or isn't a single
13775 set, ignore it. In the latter case, it isn't clear what we
13776 can do other than ignore the link, since we can't tell which
13777 register it was for. Such links wouldn't be used by combine
13778 anyway.
13780 It is not possible for the destination of the target of the link to
13781 have been changed by combine. The only potential of this is if we
13782 replace I3, I2, and I1 by I3 and I2. But in that case the
13783 destination of I2 also remains unchanged. */
13785 if (NOTE_P (link->insn)
13786 || (set = single_set (link->insn)) == 0)
13787 continue;
13789 reg = SET_DEST (set);
13790 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13791 || GET_CODE (reg) == STRICT_LOW_PART)
13792 reg = XEXP (reg, 0);
13794 /* A LOG_LINK is defined as being placed on the first insn that uses
13795 a register and points to the insn that sets the register. Start
13796 searching at the next insn after the target of the link and stop
13797 when we reach a set of the register or the end of the basic block.
13799 Note that this correctly handles the link that used to point from
13800 I3 to I2. Also note that not much searching is typically done here
13801 since most links don't point very far away. */
13803 for (insn = NEXT_INSN (link->insn);
13804 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13805 || BB_HEAD (this_basic_block->next_bb) != insn));
13806 insn = NEXT_INSN (insn))
13807 if (DEBUG_INSN_P (insn))
13808 continue;
13809 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13811 if (reg_referenced_p (reg, PATTERN (insn)))
13812 place = insn;
13813 break;
13815 else if (CALL_P (insn)
13816 && find_reg_fusage (insn, USE, reg))
13818 place = insn;
13819 break;
13821 else if (INSN_P (insn) && reg_set_p (reg, insn))
13822 break;
13824 /* If we found a place to put the link, place it there unless there
13825 is already a link to the same insn as LINK at that point. */
13827 if (place)
13829 struct insn_link *link2;
13831 FOR_EACH_LOG_LINK (link2, place)
13832 if (link2->insn == link->insn)
13833 break;
13835 if (link2 == NULL)
13837 link->next = LOG_LINKS (place);
13838 LOG_LINKS (place) = link;
13840 /* Set added_links_insn to the earliest insn we added a
13841 link to. */
13842 if (added_links_insn == 0
13843 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13844 added_links_insn = place;
13850 /* Check for any register or memory mentioned in EQUIV that is not
13851 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13852 of EXPR where some registers may have been replaced by constants. */
13854 static bool
13855 unmentioned_reg_p (rtx equiv, rtx expr)
13857 subrtx_iterator::array_type array;
13858 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
13860 const_rtx x = *iter;
13861 if ((REG_P (x) || MEM_P (x))
13862 && !reg_mentioned_p (x, expr))
13863 return true;
13865 return false;
13868 DEBUG_FUNCTION void
13869 dump_combine_stats (FILE *file)
13871 fprintf
13872 (file,
13873 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13874 combine_attempts, combine_merges, combine_extras, combine_successes);
13877 void
13878 dump_combine_total_stats (FILE *file)
13880 fprintf
13881 (file,
13882 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13883 total_attempts, total_merges, total_extras, total_successes);
13886 /* Try combining insns through substitution. */
13887 static unsigned int
13888 rest_of_handle_combine (void)
13890 int rebuild_jump_labels_after_combine;
13892 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13893 df_note_add_problem ();
13894 df_analyze ();
13896 regstat_init_n_sets_and_refs ();
13898 rebuild_jump_labels_after_combine
13899 = combine_instructions (get_insns (), max_reg_num ());
13901 /* Combining insns may have turned an indirect jump into a
13902 direct jump. Rebuild the JUMP_LABEL fields of jumping
13903 instructions. */
13904 if (rebuild_jump_labels_after_combine)
13906 timevar_push (TV_JUMP);
13907 rebuild_jump_labels (get_insns ());
13908 cleanup_cfg (0);
13909 timevar_pop (TV_JUMP);
13912 regstat_free_n_sets_and_refs ();
13913 return 0;
13916 namespace {
13918 const pass_data pass_data_combine =
13920 RTL_PASS, /* type */
13921 "combine", /* name */
13922 OPTGROUP_NONE, /* optinfo_flags */
13923 TV_COMBINE, /* tv_id */
13924 PROP_cfglayout, /* properties_required */
13925 0, /* properties_provided */
13926 0, /* properties_destroyed */
13927 0, /* todo_flags_start */
13928 TODO_df_finish, /* todo_flags_finish */
13931 class pass_combine : public rtl_opt_pass
13933 public:
13934 pass_combine (gcc::context *ctxt)
13935 : rtl_opt_pass (pass_data_combine, ctxt)
13938 /* opt_pass methods: */
13939 virtual bool gate (function *) { return (optimize > 0); }
13940 virtual unsigned int execute (function *)
13942 return rest_of_handle_combine ();
13945 }; // class pass_combine
13947 } // anon namespace
13949 rtl_opt_pass *
13950 make_pass_combine (gcc::context *ctxt)
13952 return new pass_combine (ctxt);