2015-06-03 Hale Wang <hale.wang@arm.com>
[official-gcc.git] / embedded-4_9-branch / gcc / combine.c
blob7df85f24847044d58cb31eb1d7ca647399a43fe0
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"
108 /* Number of attempts to combine instructions in this function. */
110 static int combine_attempts;
112 /* Number of attempts that got as far as substitution in this function. */
114 static int combine_merges;
116 /* Number of instructions combined with added SETs in this function. */
118 static int combine_extras;
120 /* Number of instructions combined in this function. */
122 static int combine_successes;
124 /* Totals over entire compilation. */
126 static int total_attempts, total_merges, total_extras, total_successes;
128 /* combine_instructions may try to replace the right hand side of the
129 second instruction with the value of an associated REG_EQUAL note
130 before throwing it at try_combine. That is problematic when there
131 is a REG_DEAD note for a register used in the old right hand side
132 and can cause distribute_notes to do wrong things. This is the
133 second instruction if it has been so modified, null otherwise. */
135 static rtx i2mod;
137 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
139 static rtx i2mod_old_rhs;
141 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
143 static rtx i2mod_new_rhs;
145 typedef struct reg_stat_struct {
146 /* Record last point of death of (hard or pseudo) register n. */
147 rtx last_death;
149 /* Record last point of modification of (hard or pseudo) register n. */
150 rtx last_set;
152 /* The next group of fields allows the recording of the last value assigned
153 to (hard or pseudo) register n. We use this information to see if an
154 operation being processed is redundant given a prior operation performed
155 on the register. For example, an `and' with a constant is redundant if
156 all the zero bits are already known to be turned off.
158 We use an approach similar to that used by cse, but change it in the
159 following ways:
161 (1) We do not want to reinitialize at each label.
162 (2) It is useful, but not critical, to know the actual value assigned
163 to a register. Often just its form is helpful.
165 Therefore, we maintain the following fields:
167 last_set_value the last value assigned
168 last_set_label records the value of label_tick when the
169 register was assigned
170 last_set_table_tick records the value of label_tick when a
171 value using the register is assigned
172 last_set_invalid set to nonzero when it is not valid
173 to use the value of this register in some
174 register's value
176 To understand the usage of these tables, it is important to understand
177 the distinction between the value in last_set_value being valid and
178 the register being validly contained in some other expression in the
179 table.
181 (The next two parameters are out of date).
183 reg_stat[i].last_set_value is valid if it is nonzero, and either
184 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186 Register I may validly appear in any expression returned for the value
187 of another register if reg_n_sets[i] is 1. It may also appear in the
188 value for register J if reg_stat[j].last_set_invalid is zero, or
189 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191 If an expression is found in the table containing a register which may
192 not validly appear in an expression, the register is replaced by
193 something that won't match, (clobber (const_int 0)). */
195 /* Record last value assigned to (hard or pseudo) register n. */
197 rtx last_set_value;
199 /* Record the value of label_tick when an expression involving register n
200 is placed in last_set_value. */
202 int last_set_table_tick;
204 /* Record the value of label_tick when the value for register n is placed in
205 last_set_value. */
207 int last_set_label;
209 /* These fields are maintained in parallel with last_set_value and are
210 used to store the mode in which the register was last set, the bits
211 that were known to be zero when it was last set, and the number of
212 sign bits copies it was known to have when it was last set. */
214 unsigned HOST_WIDE_INT last_set_nonzero_bits;
215 char last_set_sign_bit_copies;
216 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
218 /* Set nonzero if references to register n in expressions should not be
219 used. last_set_invalid is set nonzero when this register is being
220 assigned to and last_set_table_tick == label_tick. */
222 char last_set_invalid;
224 /* Some registers that are set more than once and used in more than one
225 basic block are nevertheless always set in similar ways. For example,
226 a QImode register may be loaded from memory in two places on a machine
227 where byte loads zero extend.
229 We record in the following fields if a register has some leading bits
230 that are always equal to the sign bit, and what we know about the
231 nonzero bits of a register, specifically which bits are known to be
232 zero.
234 If an entry is zero, it means that we don't know anything special. */
236 unsigned char sign_bit_copies;
238 unsigned HOST_WIDE_INT nonzero_bits;
240 /* Record the value of the label_tick when the last truncation
241 happened. The field truncated_to_mode is only valid if
242 truncation_label == label_tick. */
244 int truncation_label;
246 /* Record the last truncation seen for this register. If truncation
247 is not a nop to this mode we might be able to save an explicit
248 truncation if we know that value already contains a truncated
249 value. */
251 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
252 } reg_stat_type;
255 static vec<reg_stat_type> reg_stat;
257 /* Record the luid of the last insn that invalidated memory
258 (anything that writes memory, and subroutine calls, but not pushes). */
260 static int mem_last_set;
262 /* Record the luid of the last CALL_INSN
263 so we can tell whether a potential combination crosses any calls. */
265 static int last_call_luid;
267 /* When `subst' is called, this is the insn that is being modified
268 (by combining in a previous insn). The PATTERN of this insn
269 is still the old pattern partially modified and it should not be
270 looked at, but this may be used to examine the successors of the insn
271 to judge whether a simplification is valid. */
273 static rtx subst_insn;
275 /* This is the lowest LUID that `subst' is currently dealing with.
276 get_last_value will not return a value if the register was set at or
277 after this LUID. If not for this mechanism, we could get confused if
278 I2 or I1 in try_combine were an insn that used the old value of a register
279 to obtain a new value. In that case, we might erroneously get the
280 new value of the register when we wanted the old one. */
282 static int subst_low_luid;
284 /* This contains any hard registers that are used in newpat; reg_dead_at_p
285 must consider all these registers to be always live. */
287 static HARD_REG_SET newpat_used_regs;
289 /* This is an insn to which a LOG_LINKS entry has been added. If this
290 insn is the earlier than I2 or I3, combine should rescan starting at
291 that location. */
293 static rtx added_links_insn;
295 /* Basic block in which we are performing combines. */
296 static basic_block this_basic_block;
297 static bool optimize_this_for_speed_p;
300 /* Length of the currently allocated uid_insn_cost array. */
302 static int max_uid_known;
304 /* The following array records the insn_rtx_cost for every insn
305 in the instruction stream. */
307 static int *uid_insn_cost;
309 /* The following array records the LOG_LINKS for every insn in the
310 instruction stream as struct insn_link pointers. */
312 struct insn_link {
313 rtx insn;
314 struct insn_link *next;
317 static struct insn_link **uid_log_links;
319 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
320 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
322 #define FOR_EACH_LOG_LINK(L, INSN) \
323 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
325 /* Links for LOG_LINKS are allocated from this obstack. */
327 static struct obstack insn_link_obstack;
329 /* Allocate a link. */
331 static inline struct insn_link *
332 alloc_insn_link (rtx insn, struct insn_link *next)
334 struct insn_link *l
335 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
336 sizeof (struct insn_link));
337 l->insn = insn;
338 l->next = next;
339 return l;
342 /* Incremented for each basic block. */
344 static int label_tick;
346 /* Reset to label_tick for each extended basic block in scanning order. */
348 static int label_tick_ebb_start;
350 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
351 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
353 static enum machine_mode nonzero_bits_mode;
355 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
356 be safely used. It is zero while computing them and after combine has
357 completed. This former test prevents propagating values based on
358 previously set values, which can be incorrect if a variable is modified
359 in a loop. */
361 static int nonzero_sign_valid;
364 /* Record one modification to rtl structure
365 to be undone by storing old_contents into *where. */
367 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
369 struct undo
371 struct undo *next;
372 enum undo_kind kind;
373 union { rtx r; int i; enum machine_mode m; struct insn_link *l; } old_contents;
374 union { rtx *r; int *i; struct insn_link **l; } where;
377 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
378 num_undo says how many are currently recorded.
380 other_insn is nonzero if we have modified some other insn in the process
381 of working on subst_insn. It must be verified too. */
383 struct undobuf
385 struct undo *undos;
386 struct undo *frees;
387 rtx other_insn;
390 static struct undobuf undobuf;
392 /* Number of times the pseudo being substituted for
393 was found and replaced. */
395 static int n_occurrences;
397 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
398 enum machine_mode,
399 unsigned HOST_WIDE_INT,
400 unsigned HOST_WIDE_INT *);
401 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
402 enum machine_mode,
403 unsigned int, unsigned int *);
404 static void do_SUBST (rtx *, rtx);
405 static void do_SUBST_INT (int *, int);
406 static void init_reg_last (void);
407 static void setup_incoming_promotions (rtx);
408 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
409 static int cant_combine_insn_p (rtx);
410 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
411 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
412 static int contains_muldiv (rtx);
413 static rtx try_combine (rtx, rtx, rtx, rtx, int *, rtx);
414 static void undo_all (void);
415 static void undo_commit (void);
416 static rtx *find_split_point (rtx *, rtx, bool);
417 static rtx subst (rtx, rtx, rtx, int, int, int);
418 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
419 static rtx simplify_if_then_else (rtx);
420 static rtx simplify_set (rtx);
421 static rtx simplify_logical (rtx);
422 static rtx expand_compound_operation (rtx);
423 static const_rtx expand_field_assignment (const_rtx);
424 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
425 rtx, unsigned HOST_WIDE_INT, int, int, int);
426 static rtx extract_left_shift (rtx, int);
427 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
428 unsigned HOST_WIDE_INT *);
429 static rtx canon_reg_for_combine (rtx, rtx);
430 static rtx force_to_mode (rtx, enum machine_mode,
431 unsigned HOST_WIDE_INT, int);
432 static rtx if_then_else_cond (rtx, rtx *, rtx *);
433 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
434 static int rtx_equal_for_field_assignment_p (rtx, rtx);
435 static rtx make_field_assignment (rtx);
436 static rtx apply_distributive_law (rtx);
437 static rtx distribute_and_simplify_rtx (rtx, int);
438 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
439 unsigned HOST_WIDE_INT);
440 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
441 unsigned HOST_WIDE_INT);
442 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
443 HOST_WIDE_INT, enum machine_mode, int *);
444 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
445 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
446 int);
447 static int recog_for_combine (rtx *, rtx, rtx *);
448 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
449 static enum rtx_code simplify_compare_const (enum rtx_code, enum machine_mode,
450 rtx, rtx *);
451 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
452 static void update_table_tick (rtx);
453 static void record_value_for_reg (rtx, rtx, rtx);
454 static void check_promoted_subreg (rtx, rtx);
455 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
456 static void record_dead_and_set_regs (rtx);
457 static int get_last_value_validate (rtx *, rtx, int, int);
458 static rtx get_last_value (const_rtx);
459 static int use_crosses_set_p (const_rtx, int);
460 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
461 static int reg_dead_at_p (rtx, rtx);
462 static void move_deaths (rtx, rtx, int, rtx, rtx *);
463 static int reg_bitfield_target_p (rtx, rtx);
464 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
465 static void distribute_links (struct insn_link *);
466 static void mark_used_regs_combine (rtx);
467 static void record_promoted_value (rtx, rtx);
468 static int unmentioned_reg_p_1 (rtx *, void *);
469 static bool unmentioned_reg_p (rtx, rtx);
470 static int record_truncated_value (rtx *, void *);
471 static void record_truncated_values (rtx *, void *);
472 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
473 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
476 /* It is not safe to use ordinary gen_lowpart in combine.
477 See comments in gen_lowpart_for_combine. */
478 #undef RTL_HOOKS_GEN_LOWPART
479 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
481 /* Our implementation of gen_lowpart never emits a new pseudo. */
482 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
483 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
485 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
486 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
488 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
489 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
491 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
492 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
494 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
497 /* Convenience wrapper for the canonicalize_comparison target hook.
498 Target hooks cannot use enum rtx_code. */
499 static inline void
500 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
501 bool op0_preserve_value)
503 int code_int = (int)*code;
504 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
505 *code = (enum rtx_code)code_int;
508 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
509 PATTERN can not be split. Otherwise, it returns an insn sequence.
510 This is a wrapper around split_insns which ensures that the
511 reg_stat vector is made larger if the splitter creates a new
512 register. */
514 static rtx
515 combine_split_insns (rtx pattern, rtx insn)
517 rtx ret;
518 unsigned int nregs;
520 ret = split_insns (pattern, insn);
521 nregs = max_reg_num ();
522 if (nregs > reg_stat.length ())
523 reg_stat.safe_grow_cleared (nregs);
524 return ret;
527 /* This is used by find_single_use to locate an rtx in LOC that
528 contains exactly one use of DEST, which is typically either a REG
529 or CC0. It returns a pointer to the innermost rtx expression
530 containing DEST. Appearances of DEST that are being used to
531 totally replace it are not counted. */
533 static rtx *
534 find_single_use_1 (rtx dest, rtx *loc)
536 rtx x = *loc;
537 enum rtx_code code = GET_CODE (x);
538 rtx *result = NULL;
539 rtx *this_result;
540 int i;
541 const char *fmt;
543 switch (code)
545 case CONST:
546 case LABEL_REF:
547 case SYMBOL_REF:
548 CASE_CONST_ANY:
549 case CLOBBER:
550 return 0;
552 case SET:
553 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
554 of a REG that occupies all of the REG, the insn uses DEST if
555 it is mentioned in the destination or the source. Otherwise, we
556 need just check the source. */
557 if (GET_CODE (SET_DEST (x)) != CC0
558 && GET_CODE (SET_DEST (x)) != PC
559 && !REG_P (SET_DEST (x))
560 && ! (GET_CODE (SET_DEST (x)) == SUBREG
561 && REG_P (SUBREG_REG (SET_DEST (x)))
562 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
563 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
564 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
565 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
566 break;
568 return find_single_use_1 (dest, &SET_SRC (x));
570 case MEM:
571 case SUBREG:
572 return find_single_use_1 (dest, &XEXP (x, 0));
574 default:
575 break;
578 /* If it wasn't one of the common cases above, check each expression and
579 vector of this code. Look for a unique usage of DEST. */
581 fmt = GET_RTX_FORMAT (code);
582 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
584 if (fmt[i] == 'e')
586 if (dest == XEXP (x, i)
587 || (REG_P (dest) && REG_P (XEXP (x, i))
588 && REGNO (dest) == REGNO (XEXP (x, i))))
589 this_result = loc;
590 else
591 this_result = find_single_use_1 (dest, &XEXP (x, i));
593 if (result == NULL)
594 result = this_result;
595 else if (this_result)
596 /* Duplicate usage. */
597 return NULL;
599 else if (fmt[i] == 'E')
601 int j;
603 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
605 if (XVECEXP (x, i, j) == dest
606 || (REG_P (dest)
607 && REG_P (XVECEXP (x, i, j))
608 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
609 this_result = loc;
610 else
611 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
613 if (result == NULL)
614 result = this_result;
615 else if (this_result)
616 return NULL;
621 return result;
625 /* See if DEST, produced in INSN, is used only a single time in the
626 sequel. If so, return a pointer to the innermost rtx expression in which
627 it is used.
629 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
631 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
632 care about REG_DEAD notes or LOG_LINKS.
634 Otherwise, we find the single use by finding an insn that has a
635 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
636 only referenced once in that insn, we know that it must be the first
637 and last insn referencing DEST. */
639 static rtx *
640 find_single_use (rtx dest, rtx insn, rtx *ploc)
642 basic_block bb;
643 rtx next;
644 rtx *result;
645 struct insn_link *link;
647 #ifdef HAVE_cc0
648 if (dest == cc0_rtx)
650 next = NEXT_INSN (insn);
651 if (next == 0
652 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
653 return 0;
655 result = find_single_use_1 (dest, &PATTERN (next));
656 if (result && ploc)
657 *ploc = next;
658 return result;
660 #endif
662 if (!REG_P (dest))
663 return 0;
665 bb = BLOCK_FOR_INSN (insn);
666 for (next = NEXT_INSN (insn);
667 next && BLOCK_FOR_INSN (next) == bb;
668 next = NEXT_INSN (next))
669 if (INSN_P (next) && dead_or_set_p (next, dest))
671 FOR_EACH_LOG_LINK (link, next)
672 if (link->insn == insn)
673 break;
675 if (link)
677 result = find_single_use_1 (dest, &PATTERN (next));
678 if (ploc)
679 *ploc = next;
680 return result;
684 return 0;
687 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
688 insn. The substitution can be undone by undo_all. If INTO is already
689 set to NEWVAL, do not record this change. Because computing NEWVAL might
690 also call SUBST, we have to compute it before we put anything into
691 the undo table. */
693 static void
694 do_SUBST (rtx *into, rtx newval)
696 struct undo *buf;
697 rtx oldval = *into;
699 if (oldval == newval)
700 return;
702 /* We'd like to catch as many invalid transformations here as
703 possible. Unfortunately, there are way too many mode changes
704 that are perfectly valid, so we'd waste too much effort for
705 little gain doing the checks here. Focus on catching invalid
706 transformations involving integer constants. */
707 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
708 && CONST_INT_P (newval))
710 /* Sanity check that we're replacing oldval with a CONST_INT
711 that is a valid sign-extension for the original mode. */
712 gcc_assert (INTVAL (newval)
713 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
715 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
716 CONST_INT is not valid, because after the replacement, the
717 original mode would be gone. Unfortunately, we can't tell
718 when do_SUBST is called to replace the operand thereof, so we
719 perform this test on oldval instead, checking whether an
720 invalid replacement took place before we got here. */
721 gcc_assert (!(GET_CODE (oldval) == SUBREG
722 && CONST_INT_P (SUBREG_REG (oldval))));
723 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
724 && CONST_INT_P (XEXP (oldval, 0))));
727 if (undobuf.frees)
728 buf = undobuf.frees, undobuf.frees = buf->next;
729 else
730 buf = XNEW (struct undo);
732 buf->kind = UNDO_RTX;
733 buf->where.r = into;
734 buf->old_contents.r = oldval;
735 *into = newval;
737 buf->next = undobuf.undos, undobuf.undos = buf;
740 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
742 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
743 for the value of a HOST_WIDE_INT value (including CONST_INT) is
744 not safe. */
746 static void
747 do_SUBST_INT (int *into, int newval)
749 struct undo *buf;
750 int oldval = *into;
752 if (oldval == newval)
753 return;
755 if (undobuf.frees)
756 buf = undobuf.frees, undobuf.frees = buf->next;
757 else
758 buf = XNEW (struct undo);
760 buf->kind = UNDO_INT;
761 buf->where.i = into;
762 buf->old_contents.i = oldval;
763 *into = newval;
765 buf->next = undobuf.undos, undobuf.undos = buf;
768 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
770 /* Similar to SUBST, but just substitute the mode. This is used when
771 changing the mode of a pseudo-register, so that any other
772 references to the entry in the regno_reg_rtx array will change as
773 well. */
775 static void
776 do_SUBST_MODE (rtx *into, enum machine_mode newval)
778 struct undo *buf;
779 enum machine_mode oldval = GET_MODE (*into);
781 if (oldval == newval)
782 return;
784 if (undobuf.frees)
785 buf = undobuf.frees, undobuf.frees = buf->next;
786 else
787 buf = XNEW (struct undo);
789 buf->kind = UNDO_MODE;
790 buf->where.r = into;
791 buf->old_contents.m = oldval;
792 adjust_reg_mode (*into, newval);
794 buf->next = undobuf.undos, undobuf.undos = buf;
797 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
799 #ifndef HAVE_cc0
800 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
802 static void
803 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
805 struct undo *buf;
806 struct insn_link * oldval = *into;
808 if (oldval == newval)
809 return;
811 if (undobuf.frees)
812 buf = undobuf.frees, undobuf.frees = buf->next;
813 else
814 buf = XNEW (struct undo);
816 buf->kind = UNDO_LINKS;
817 buf->where.l = into;
818 buf->old_contents.l = oldval;
819 *into = newval;
821 buf->next = undobuf.undos, undobuf.undos = buf;
824 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
825 #endif
827 /* Subroutine of try_combine. Determine whether the replacement patterns
828 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
829 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
830 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
831 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
832 of all the instructions can be estimated and the replacements are more
833 expensive than the original sequence. */
835 static bool
836 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
837 rtx newi2pat, rtx newotherpat)
839 int i0_cost, i1_cost, i2_cost, i3_cost;
840 int new_i2_cost, new_i3_cost;
841 int old_cost, new_cost;
843 /* Lookup the original insn_rtx_costs. */
844 i2_cost = INSN_COST (i2);
845 i3_cost = INSN_COST (i3);
847 if (i1)
849 i1_cost = INSN_COST (i1);
850 if (i0)
852 i0_cost = INSN_COST (i0);
853 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
854 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
856 else
858 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
859 ? i1_cost + i2_cost + i3_cost : 0);
860 i0_cost = 0;
863 else
865 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
866 i1_cost = i0_cost = 0;
869 /* Calculate the replacement insn_rtx_costs. */
870 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
871 if (newi2pat)
873 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
874 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
875 ? new_i2_cost + new_i3_cost : 0;
877 else
879 new_cost = new_i3_cost;
880 new_i2_cost = 0;
883 if (undobuf.other_insn)
885 int old_other_cost, new_other_cost;
887 old_other_cost = INSN_COST (undobuf.other_insn);
888 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
889 if (old_other_cost > 0 && new_other_cost > 0)
891 old_cost += old_other_cost;
892 new_cost += new_other_cost;
894 else
895 old_cost = 0;
898 /* Disallow this combination if both new_cost and old_cost are greater than
899 zero, and new_cost is greater than old cost. */
900 if (old_cost > 0 && new_cost > old_cost)
902 if (dump_file)
904 if (i0)
906 fprintf (dump_file,
907 "rejecting combination of insns %d, %d, %d and %d\n",
908 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
909 INSN_UID (i3));
910 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
911 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
913 else if (i1)
915 fprintf (dump_file,
916 "rejecting combination of insns %d, %d and %d\n",
917 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
918 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
919 i1_cost, i2_cost, i3_cost, old_cost);
921 else
923 fprintf (dump_file,
924 "rejecting combination of insns %d and %d\n",
925 INSN_UID (i2), INSN_UID (i3));
926 fprintf (dump_file, "original costs %d + %d = %d\n",
927 i2_cost, i3_cost, old_cost);
930 if (newi2pat)
932 fprintf (dump_file, "replacement costs %d + %d = %d\n",
933 new_i2_cost, new_i3_cost, new_cost);
935 else
936 fprintf (dump_file, "replacement cost %d\n", new_cost);
939 return false;
942 /* Update the uid_insn_cost array with the replacement costs. */
943 INSN_COST (i2) = new_i2_cost;
944 INSN_COST (i3) = new_i3_cost;
945 if (i1)
947 INSN_COST (i1) = 0;
948 if (i0)
949 INSN_COST (i0) = 0;
952 return true;
956 /* Delete any insns that copy a register to itself. */
958 static void
959 delete_noop_moves (void)
961 rtx insn, next;
962 basic_block bb;
964 FOR_EACH_BB_FN (bb, cfun)
966 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
968 next = NEXT_INSN (insn);
969 if (INSN_P (insn) && noop_move_p (insn))
971 if (dump_file)
972 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
974 delete_insn_and_edges (insn);
981 /* Fill in log links field for all insns. */
983 static void
984 create_log_links (void)
986 basic_block bb;
987 rtx *next_use, insn;
988 df_ref *def_vec, *use_vec;
990 next_use = XCNEWVEC (rtx, max_reg_num ());
992 /* Pass through each block from the end, recording the uses of each
993 register and establishing log links when def is encountered.
994 Note that we do not clear next_use array in order to save time,
995 so we have to test whether the use is in the same basic block as def.
997 There are a few cases below when we do not consider the definition or
998 usage -- these are taken from original flow.c did. Don't ask me why it is
999 done this way; I don't know and if it works, I don't want to know. */
1001 FOR_EACH_BB_FN (bb, cfun)
1003 FOR_BB_INSNS_REVERSE (bb, insn)
1005 if (!NONDEBUG_INSN_P (insn))
1006 continue;
1008 /* Log links are created only once. */
1009 gcc_assert (!LOG_LINKS (insn));
1011 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
1013 df_ref def = *def_vec;
1014 int regno = DF_REF_REGNO (def);
1015 rtx use_insn;
1017 if (!next_use[regno])
1018 continue;
1020 /* Do not consider if it is pre/post modification in MEM. */
1021 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1022 continue;
1024 /* Do not make the log link for frame pointer. */
1025 if ((regno == FRAME_POINTER_REGNUM
1026 && (! reload_completed || frame_pointer_needed))
1027 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1028 || (regno == HARD_FRAME_POINTER_REGNUM
1029 && (! reload_completed || frame_pointer_needed))
1030 #endif
1031 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1032 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1033 #endif
1035 continue;
1037 use_insn = next_use[regno];
1038 if (BLOCK_FOR_INSN (use_insn) == bb)
1040 /* flow.c claimed:
1042 We don't build a LOG_LINK for hard registers contained
1043 in ASM_OPERANDs. If these registers get replaced,
1044 we might wind up changing the semantics of the insn,
1045 even if reload can make what appear to be valid
1046 assignments later. */
1047 if (regno >= FIRST_PSEUDO_REGISTER
1048 || asm_noperands (PATTERN (use_insn)) < 0)
1050 /* Don't add duplicate links between instructions. */
1051 struct insn_link *links;
1052 FOR_EACH_LOG_LINK (links, use_insn)
1053 if (insn == links->insn)
1054 break;
1056 if (!links)
1057 LOG_LINKS (use_insn)
1058 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1061 next_use[regno] = NULL_RTX;
1064 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1066 df_ref use = *use_vec;
1067 int regno = DF_REF_REGNO (use);
1069 /* Do not consider the usage of the stack pointer
1070 by function call. */
1071 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1072 continue;
1074 next_use[regno] = insn;
1079 free (next_use);
1082 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1083 true if we found a LOG_LINK that proves that A feeds B. This only works
1084 if there are no instructions between A and B which could have a link
1085 depending on A, since in that case we would not record a link for B.
1086 We also check the implicit dependency created by a cc0 setter/user
1087 pair. */
1089 static bool
1090 insn_a_feeds_b (rtx a, rtx b)
1092 struct insn_link *links;
1093 FOR_EACH_LOG_LINK (links, b)
1094 if (links->insn == a)
1095 return true;
1096 #ifdef HAVE_cc0
1097 if (sets_cc0_p (a))
1098 return true;
1099 #endif
1100 return false;
1103 /* Main entry point for combiner. F is the first insn of the function.
1104 NREGS is the first unused pseudo-reg number.
1106 Return nonzero if the combiner has turned an indirect jump
1107 instruction into a direct jump. */
1108 static int
1109 combine_instructions (rtx f, unsigned int nregs)
1111 rtx insn, next;
1112 #ifdef HAVE_cc0
1113 rtx prev;
1114 #endif
1115 struct insn_link *links, *nextlinks;
1116 rtx first;
1117 basic_block last_bb;
1119 int new_direct_jump_p = 0;
1121 for (first = f; first && !INSN_P (first); )
1122 first = NEXT_INSN (first);
1123 if (!first)
1124 return 0;
1126 combine_attempts = 0;
1127 combine_merges = 0;
1128 combine_extras = 0;
1129 combine_successes = 0;
1131 rtl_hooks = combine_rtl_hooks;
1133 reg_stat.safe_grow_cleared (nregs);
1135 init_recog_no_volatile ();
1137 /* Allocate array for insn info. */
1138 max_uid_known = get_max_uid ();
1139 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1140 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1141 gcc_obstack_init (&insn_link_obstack);
1143 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1145 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1146 problems when, for example, we have j <<= 1 in a loop. */
1148 nonzero_sign_valid = 0;
1149 label_tick = label_tick_ebb_start = 1;
1151 /* Scan all SETs and see if we can deduce anything about what
1152 bits are known to be zero for some registers and how many copies
1153 of the sign bit are known to exist for those registers.
1155 Also set any known values so that we can use it while searching
1156 for what bits are known to be set. */
1158 setup_incoming_promotions (first);
1159 /* Allow the entry block and the first block to fall into the same EBB.
1160 Conceptually the incoming promotions are assigned to the entry block. */
1161 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1163 create_log_links ();
1164 FOR_EACH_BB_FN (this_basic_block, cfun)
1166 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1167 last_call_luid = 0;
1168 mem_last_set = -1;
1170 label_tick++;
1171 if (!single_pred_p (this_basic_block)
1172 || single_pred (this_basic_block) != last_bb)
1173 label_tick_ebb_start = label_tick;
1174 last_bb = this_basic_block;
1176 FOR_BB_INSNS (this_basic_block, insn)
1177 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1179 #ifdef AUTO_INC_DEC
1180 rtx links;
1181 #endif
1183 subst_low_luid = DF_INSN_LUID (insn);
1184 subst_insn = insn;
1186 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1187 insn);
1188 record_dead_and_set_regs (insn);
1190 #ifdef AUTO_INC_DEC
1191 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1192 if (REG_NOTE_KIND (links) == REG_INC)
1193 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1194 insn);
1195 #endif
1197 /* Record the current insn_rtx_cost of this instruction. */
1198 if (NONJUMP_INSN_P (insn))
1199 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1200 optimize_this_for_speed_p);
1201 if (dump_file)
1202 fprintf (dump_file, "insn_cost %d: %d\n",
1203 INSN_UID (insn), INSN_COST (insn));
1207 nonzero_sign_valid = 1;
1209 /* Now scan all the insns in forward order. */
1210 label_tick = label_tick_ebb_start = 1;
1211 init_reg_last ();
1212 setup_incoming_promotions (first);
1213 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1215 FOR_EACH_BB_FN (this_basic_block, cfun)
1217 rtx last_combined_insn = NULL_RTX;
1218 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1219 last_call_luid = 0;
1220 mem_last_set = -1;
1222 label_tick++;
1223 if (!single_pred_p (this_basic_block)
1224 || single_pred (this_basic_block) != last_bb)
1225 label_tick_ebb_start = label_tick;
1226 last_bb = this_basic_block;
1228 rtl_profile_for_bb (this_basic_block);
1229 for (insn = BB_HEAD (this_basic_block);
1230 insn != NEXT_INSN (BB_END (this_basic_block));
1231 insn = next ? next : NEXT_INSN (insn))
1233 next = 0;
1234 if (NONDEBUG_INSN_P (insn))
1236 while (last_combined_insn
1237 && INSN_DELETED_P (last_combined_insn))
1238 last_combined_insn = PREV_INSN (last_combined_insn);
1239 if (last_combined_insn == NULL_RTX
1240 || BARRIER_P (last_combined_insn)
1241 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1242 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1243 last_combined_insn = insn;
1245 /* See if we know about function return values before this
1246 insn based upon SUBREG flags. */
1247 check_promoted_subreg (insn, PATTERN (insn));
1249 /* See if we can find hardregs and subreg of pseudos in
1250 narrower modes. This could help turning TRUNCATEs
1251 into SUBREGs. */
1252 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1254 /* Try this insn with each insn it links back to. */
1256 FOR_EACH_LOG_LINK (links, insn)
1257 if ((next = try_combine (insn, links->insn, NULL_RTX,
1258 NULL_RTX, &new_direct_jump_p,
1259 last_combined_insn)) != 0)
1260 goto retry;
1262 /* Try each sequence of three linked insns ending with this one. */
1264 FOR_EACH_LOG_LINK (links, insn)
1266 rtx link = links->insn;
1268 /* If the linked insn has been replaced by a note, then there
1269 is no point in pursuing this chain any further. */
1270 if (NOTE_P (link))
1271 continue;
1273 FOR_EACH_LOG_LINK (nextlinks, link)
1274 if ((next = try_combine (insn, link, nextlinks->insn,
1275 NULL_RTX, &new_direct_jump_p,
1276 last_combined_insn)) != 0)
1277 goto retry;
1280 #ifdef HAVE_cc0
1281 /* Try to combine a jump insn that uses CC0
1282 with a preceding insn that sets CC0, and maybe with its
1283 logical predecessor as well.
1284 This is how we make decrement-and-branch insns.
1285 We need this special code because data flow connections
1286 via CC0 do not get entered in LOG_LINKS. */
1288 if (JUMP_P (insn)
1289 && (prev = prev_nonnote_insn (insn)) != 0
1290 && NONJUMP_INSN_P (prev)
1291 && sets_cc0_p (PATTERN (prev)))
1293 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1294 &new_direct_jump_p,
1295 last_combined_insn)) != 0)
1296 goto retry;
1298 FOR_EACH_LOG_LINK (nextlinks, prev)
1299 if ((next = try_combine (insn, prev, nextlinks->insn,
1300 NULL_RTX, &new_direct_jump_p,
1301 last_combined_insn)) != 0)
1302 goto retry;
1305 /* Do the same for an insn that explicitly references CC0. */
1306 if (NONJUMP_INSN_P (insn)
1307 && (prev = prev_nonnote_insn (insn)) != 0
1308 && NONJUMP_INSN_P (prev)
1309 && sets_cc0_p (PATTERN (prev))
1310 && GET_CODE (PATTERN (insn)) == SET
1311 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1313 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1314 &new_direct_jump_p,
1315 last_combined_insn)) != 0)
1316 goto retry;
1318 FOR_EACH_LOG_LINK (nextlinks, prev)
1319 if ((next = try_combine (insn, prev, nextlinks->insn,
1320 NULL_RTX, &new_direct_jump_p,
1321 last_combined_insn)) != 0)
1322 goto retry;
1325 /* Finally, see if any of the insns that this insn links to
1326 explicitly references CC0. If so, try this insn, that insn,
1327 and its predecessor if it sets CC0. */
1328 FOR_EACH_LOG_LINK (links, insn)
1329 if (NONJUMP_INSN_P (links->insn)
1330 && GET_CODE (PATTERN (links->insn)) == SET
1331 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1332 && (prev = prev_nonnote_insn (links->insn)) != 0
1333 && NONJUMP_INSN_P (prev)
1334 && sets_cc0_p (PATTERN (prev))
1335 && (next = try_combine (insn, links->insn,
1336 prev, NULL_RTX, &new_direct_jump_p,
1337 last_combined_insn)) != 0)
1338 goto retry;
1339 #endif
1341 /* Try combining an insn with two different insns whose results it
1342 uses. */
1343 FOR_EACH_LOG_LINK (links, insn)
1344 for (nextlinks = links->next; nextlinks;
1345 nextlinks = nextlinks->next)
1346 if ((next = try_combine (insn, links->insn,
1347 nextlinks->insn, NULL_RTX,
1348 &new_direct_jump_p,
1349 last_combined_insn)) != 0)
1350 goto retry;
1352 /* Try four-instruction combinations. */
1353 FOR_EACH_LOG_LINK (links, insn)
1355 struct insn_link *next1;
1356 rtx link = links->insn;
1358 /* If the linked insn has been replaced by a note, then there
1359 is no point in pursuing this chain any further. */
1360 if (NOTE_P (link))
1361 continue;
1363 FOR_EACH_LOG_LINK (next1, link)
1365 rtx link1 = next1->insn;
1366 if (NOTE_P (link1))
1367 continue;
1368 /* I0 -> I1 -> I2 -> I3. */
1369 FOR_EACH_LOG_LINK (nextlinks, link1)
1370 if ((next = try_combine (insn, link, link1,
1371 nextlinks->insn,
1372 &new_direct_jump_p,
1373 last_combined_insn)) != 0)
1374 goto retry;
1375 /* I0, I1 -> I2, I2 -> I3. */
1376 for (nextlinks = next1->next; nextlinks;
1377 nextlinks = nextlinks->next)
1378 if ((next = try_combine (insn, link, link1,
1379 nextlinks->insn,
1380 &new_direct_jump_p,
1381 last_combined_insn)) != 0)
1382 goto retry;
1385 for (next1 = links->next; next1; next1 = next1->next)
1387 rtx link1 = next1->insn;
1388 if (NOTE_P (link1))
1389 continue;
1390 /* I0 -> I2; I1, I2 -> I3. */
1391 FOR_EACH_LOG_LINK (nextlinks, link)
1392 if ((next = try_combine (insn, link, link1,
1393 nextlinks->insn,
1394 &new_direct_jump_p,
1395 last_combined_insn)) != 0)
1396 goto retry;
1397 /* I0 -> I1; I1, I2 -> I3. */
1398 FOR_EACH_LOG_LINK (nextlinks, link1)
1399 if ((next = try_combine (insn, link, link1,
1400 nextlinks->insn,
1401 &new_direct_jump_p,
1402 last_combined_insn)) != 0)
1403 goto retry;
1407 /* Try this insn with each REG_EQUAL note it links back to. */
1408 FOR_EACH_LOG_LINK (links, insn)
1410 rtx set, note;
1411 rtx temp = links->insn;
1412 if ((set = single_set (temp)) != 0
1413 && (note = find_reg_equal_equiv_note (temp)) != 0
1414 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1415 /* Avoid using a register that may already been marked
1416 dead by an earlier instruction. */
1417 && ! unmentioned_reg_p (note, SET_SRC (set))
1418 && (GET_MODE (note) == VOIDmode
1419 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1420 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1422 /* Temporarily replace the set's source with the
1423 contents of the REG_EQUAL note. The insn will
1424 be deleted or recognized by try_combine. */
1425 rtx orig = SET_SRC (set);
1426 SET_SRC (set) = note;
1427 i2mod = temp;
1428 i2mod_old_rhs = copy_rtx (orig);
1429 i2mod_new_rhs = copy_rtx (note);
1430 next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1431 &new_direct_jump_p,
1432 last_combined_insn);
1433 i2mod = NULL_RTX;
1434 if (next)
1435 goto retry;
1436 SET_SRC (set) = orig;
1440 if (!NOTE_P (insn))
1441 record_dead_and_set_regs (insn);
1443 retry:
1449 default_rtl_profile ();
1450 clear_bb_flags ();
1451 new_direct_jump_p |= purge_all_dead_edges ();
1452 delete_noop_moves ();
1454 /* Clean up. */
1455 obstack_free (&insn_link_obstack, NULL);
1456 free (uid_log_links);
1457 free (uid_insn_cost);
1458 reg_stat.release ();
1461 struct undo *undo, *next;
1462 for (undo = undobuf.frees; undo; undo = next)
1464 next = undo->next;
1465 free (undo);
1467 undobuf.frees = 0;
1470 total_attempts += combine_attempts;
1471 total_merges += combine_merges;
1472 total_extras += combine_extras;
1473 total_successes += combine_successes;
1475 nonzero_sign_valid = 0;
1476 rtl_hooks = general_rtl_hooks;
1478 /* Make recognizer allow volatile MEMs again. */
1479 init_recog ();
1481 return new_direct_jump_p;
1484 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1486 static void
1487 init_reg_last (void)
1489 unsigned int i;
1490 reg_stat_type *p;
1492 FOR_EACH_VEC_ELT (reg_stat, i, p)
1493 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1496 /* Set up any promoted values for incoming argument registers. */
1498 static void
1499 setup_incoming_promotions (rtx first)
1501 tree arg;
1502 bool strictly_local = false;
1504 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1505 arg = DECL_CHAIN (arg))
1507 rtx x, reg = DECL_INCOMING_RTL (arg);
1508 int uns1, uns3;
1509 enum machine_mode mode1, mode2, mode3, mode4;
1511 /* Only continue if the incoming argument is in a register. */
1512 if (!REG_P (reg))
1513 continue;
1515 /* Determine, if possible, whether all call sites of the current
1516 function lie within the current compilation unit. (This does
1517 take into account the exporting of a function via taking its
1518 address, and so forth.) */
1519 strictly_local = cgraph_local_info (current_function_decl)->local;
1521 /* The mode and signedness of the argument before any promotions happen
1522 (equal to the mode of the pseudo holding it at that stage). */
1523 mode1 = TYPE_MODE (TREE_TYPE (arg));
1524 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1526 /* The mode and signedness of the argument after any source language and
1527 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1528 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1529 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1531 /* The mode and signedness of the argument as it is actually passed,
1532 see assign_parm_setup_reg in function.c. */
1533 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1534 TREE_TYPE (cfun->decl), 0);
1536 /* The mode of the register in which the argument is being passed. */
1537 mode4 = GET_MODE (reg);
1539 /* Eliminate sign extensions in the callee when:
1540 (a) A mode promotion has occurred; */
1541 if (mode1 == mode3)
1542 continue;
1543 /* (b) The mode of the register is the same as the mode of
1544 the argument as it is passed; */
1545 if (mode3 != mode4)
1546 continue;
1547 /* (c) There's no language level extension; */
1548 if (mode1 == mode2)
1550 /* (c.1) All callers are from the current compilation unit. If that's
1551 the case we don't have to rely on an ABI, we only have to know
1552 what we're generating right now, and we know that we will do the
1553 mode1 to mode2 promotion with the given sign. */
1554 else if (!strictly_local)
1555 continue;
1556 /* (c.2) The combination of the two promotions is useful. This is
1557 true when the signs match, or if the first promotion is unsigned.
1558 In the later case, (sign_extend (zero_extend x)) is the same as
1559 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1560 else if (uns1)
1561 uns3 = true;
1562 else if (uns3)
1563 continue;
1565 /* Record that the value was promoted from mode1 to mode3,
1566 so that any sign extension at the head of the current
1567 function may be eliminated. */
1568 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1569 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1570 record_value_for_reg (reg, first, x);
1574 /* Called via note_stores. If X is a pseudo that is narrower than
1575 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1577 If we are setting only a portion of X and we can't figure out what
1578 portion, assume all bits will be used since we don't know what will
1579 be happening.
1581 Similarly, set how many bits of X are known to be copies of the sign bit
1582 at all locations in the function. This is the smallest number implied
1583 by any set of X. */
1585 static void
1586 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1588 rtx insn = (rtx) data;
1589 unsigned int num;
1591 if (REG_P (x)
1592 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1593 /* If this register is undefined at the start of the file, we can't
1594 say what its contents were. */
1595 && ! REGNO_REG_SET_P
1596 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1597 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1599 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1601 if (set == 0 || GET_CODE (set) == CLOBBER)
1603 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1604 rsp->sign_bit_copies = 1;
1605 return;
1608 /* If this register is being initialized using itself, and the
1609 register is uninitialized in this basic block, and there are
1610 no LOG_LINKS which set the register, then part of the
1611 register is uninitialized. In that case we can't assume
1612 anything about the number of nonzero bits.
1614 ??? We could do better if we checked this in
1615 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1616 could avoid making assumptions about the insn which initially
1617 sets the register, while still using the information in other
1618 insns. We would have to be careful to check every insn
1619 involved in the combination. */
1621 if (insn
1622 && reg_referenced_p (x, PATTERN (insn))
1623 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1624 REGNO (x)))
1626 struct insn_link *link;
1628 FOR_EACH_LOG_LINK (link, insn)
1629 if (dead_or_set_p (link->insn, x))
1630 break;
1631 if (!link)
1633 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1634 rsp->sign_bit_copies = 1;
1635 return;
1639 /* If this is a complex assignment, see if we can convert it into a
1640 simple assignment. */
1641 set = expand_field_assignment (set);
1643 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1644 set what we know about X. */
1646 if (SET_DEST (set) == x
1647 || (paradoxical_subreg_p (SET_DEST (set))
1648 && SUBREG_REG (SET_DEST (set)) == x))
1650 rtx src = SET_SRC (set);
1652 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1653 /* If X is narrower than a word and SRC is a non-negative
1654 constant that would appear negative in the mode of X,
1655 sign-extend it for use in reg_stat[].nonzero_bits because some
1656 machines (maybe most) will actually do the sign-extension
1657 and this is the conservative approach.
1659 ??? For 2.5, try to tighten up the MD files in this regard
1660 instead of this kludge. */
1662 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1663 && CONST_INT_P (src)
1664 && INTVAL (src) > 0
1665 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1666 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1667 #endif
1669 /* Don't call nonzero_bits if it cannot change anything. */
1670 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1671 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1672 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1673 if (rsp->sign_bit_copies == 0
1674 || rsp->sign_bit_copies > num)
1675 rsp->sign_bit_copies = num;
1677 else
1679 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1680 rsp->sign_bit_copies = 1;
1685 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1686 optionally insns that were previously combined into I3 or that will be
1687 combined into the merger of INSN and I3. The order is PRED, PRED2,
1688 INSN, SUCC, SUCC2, I3.
1690 Return 0 if the combination is not allowed for any reason.
1692 If the combination is allowed, *PDEST will be set to the single
1693 destination of INSN and *PSRC to the single source, and this function
1694 will return 1. */
1696 static int
1697 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1698 rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1699 rtx *pdest, rtx *psrc)
1701 int i;
1702 const_rtx set = 0;
1703 rtx src, dest;
1704 rtx p;
1705 #ifdef AUTO_INC_DEC
1706 rtx link;
1707 #endif
1708 bool all_adjacent = true;
1709 int (*is_volatile_p) (const_rtx);
1711 if (succ)
1713 if (succ2)
1715 if (next_active_insn (succ2) != i3)
1716 all_adjacent = false;
1717 if (next_active_insn (succ) != succ2)
1718 all_adjacent = false;
1720 else if (next_active_insn (succ) != i3)
1721 all_adjacent = false;
1722 if (next_active_insn (insn) != succ)
1723 all_adjacent = false;
1725 else if (next_active_insn (insn) != i3)
1726 all_adjacent = false;
1728 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1729 or a PARALLEL consisting of such a SET and CLOBBERs.
1731 If INSN has CLOBBER parallel parts, ignore them for our processing.
1732 By definition, these happen during the execution of the insn. When it
1733 is merged with another insn, all bets are off. If they are, in fact,
1734 needed and aren't also supplied in I3, they may be added by
1735 recog_for_combine. Otherwise, it won't match.
1737 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1738 note.
1740 Get the source and destination of INSN. If more than one, can't
1741 combine. */
1743 if (GET_CODE (PATTERN (insn)) == SET)
1744 set = PATTERN (insn);
1745 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1746 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1748 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1750 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1752 switch (GET_CODE (elt))
1754 /* This is important to combine floating point insns
1755 for the SH4 port. */
1756 case USE:
1757 /* Combining an isolated USE doesn't make sense.
1758 We depend here on combinable_i3pat to reject them. */
1759 /* The code below this loop only verifies that the inputs of
1760 the SET in INSN do not change. We call reg_set_between_p
1761 to verify that the REG in the USE does not change between
1762 I3 and INSN.
1763 If the USE in INSN was for a pseudo register, the matching
1764 insn pattern will likely match any register; combining this
1765 with any other USE would only be safe if we knew that the
1766 used registers have identical values, or if there was
1767 something to tell them apart, e.g. different modes. For
1768 now, we forgo such complicated tests and simply disallow
1769 combining of USES of pseudo registers with any other USE. */
1770 if (REG_P (XEXP (elt, 0))
1771 && GET_CODE (PATTERN (i3)) == PARALLEL)
1773 rtx i3pat = PATTERN (i3);
1774 int i = XVECLEN (i3pat, 0) - 1;
1775 unsigned int regno = REGNO (XEXP (elt, 0));
1779 rtx i3elt = XVECEXP (i3pat, 0, i);
1781 if (GET_CODE (i3elt) == USE
1782 && REG_P (XEXP (i3elt, 0))
1783 && (REGNO (XEXP (i3elt, 0)) == regno
1784 ? reg_set_between_p (XEXP (elt, 0),
1785 PREV_INSN (insn), i3)
1786 : regno >= FIRST_PSEUDO_REGISTER))
1787 return 0;
1789 while (--i >= 0);
1791 break;
1793 /* We can ignore CLOBBERs. */
1794 case CLOBBER:
1795 break;
1797 case SET:
1798 /* Ignore SETs whose result isn't used but not those that
1799 have side-effects. */
1800 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1801 && insn_nothrow_p (insn)
1802 && !side_effects_p (elt))
1803 break;
1805 /* If we have already found a SET, this is a second one and
1806 so we cannot combine with this insn. */
1807 if (set)
1808 return 0;
1810 set = elt;
1811 break;
1813 default:
1814 /* Anything else means we can't combine. */
1815 return 0;
1819 if (set == 0
1820 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1821 so don't do anything with it. */
1822 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1823 return 0;
1825 else
1826 return 0;
1828 if (set == 0)
1829 return 0;
1831 /* The simplification in expand_field_assignment may call back to
1832 get_last_value, so set safe guard here. */
1833 subst_low_luid = DF_INSN_LUID (insn);
1835 set = expand_field_assignment (set);
1836 src = SET_SRC (set), dest = SET_DEST (set);
1838 /* Do not eliminate user-specified register if it is in an
1839 asm input because we may break the register asm usage defined
1840 in GCC manual if allow to do so.
1841 Be aware that this may cover more cases than we expect but this
1842 should be harmless. */
1843 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1844 && extract_asm_operands (PATTERN (i3)))
1845 return 0;
1847 /* Don't eliminate a store in the stack pointer. */
1848 if (dest == stack_pointer_rtx
1849 /* Don't combine with an insn that sets a register to itself if it has
1850 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1851 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1852 /* Can't merge an ASM_OPERANDS. */
1853 || GET_CODE (src) == ASM_OPERANDS
1854 /* Can't merge a function call. */
1855 || GET_CODE (src) == CALL
1856 /* Don't eliminate a function call argument. */
1857 || (CALL_P (i3)
1858 && (find_reg_fusage (i3, USE, dest)
1859 || (REG_P (dest)
1860 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1861 && global_regs[REGNO (dest)])))
1862 /* Don't substitute into an incremented register. */
1863 || FIND_REG_INC_NOTE (i3, dest)
1864 || (succ && FIND_REG_INC_NOTE (succ, dest))
1865 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1866 /* Don't substitute into a non-local goto, this confuses CFG. */
1867 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1868 /* Make sure that DEST is not used after SUCC but before I3. */
1869 || (!all_adjacent
1870 && ((succ2
1871 && (reg_used_between_p (dest, succ2, i3)
1872 || reg_used_between_p (dest, succ, succ2)))
1873 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1874 /* Make sure that the value that is to be substituted for the register
1875 does not use any registers whose values alter in between. However,
1876 If the insns are adjacent, a use can't cross a set even though we
1877 think it might (this can happen for a sequence of insns each setting
1878 the same destination; last_set of that register might point to
1879 a NOTE). If INSN has a REG_EQUIV note, the register is always
1880 equivalent to the memory so the substitution is valid even if there
1881 are intervening stores. Also, don't move a volatile asm or
1882 UNSPEC_VOLATILE across any other insns. */
1883 || (! all_adjacent
1884 && (((!MEM_P (src)
1885 || ! find_reg_note (insn, REG_EQUIV, src))
1886 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1887 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1888 || GET_CODE (src) == UNSPEC_VOLATILE))
1889 /* Don't combine across a CALL_INSN, because that would possibly
1890 change whether the life span of some REGs crosses calls or not,
1891 and it is a pain to update that information.
1892 Exception: if source is a constant, moving it later can't hurt.
1893 Accept that as a special case. */
1894 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1895 return 0;
1897 /* DEST must either be a REG or CC0. */
1898 if (REG_P (dest))
1900 /* If register alignment is being enforced for multi-word items in all
1901 cases except for parameters, it is possible to have a register copy
1902 insn referencing a hard register that is not allowed to contain the
1903 mode being copied and which would not be valid as an operand of most
1904 insns. Eliminate this problem by not combining with such an insn.
1906 Also, on some machines we don't want to extend the life of a hard
1907 register. */
1909 if (REG_P (src)
1910 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1911 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1912 /* Don't extend the life of a hard register unless it is
1913 user variable (if we have few registers) or it can't
1914 fit into the desired register (meaning something special
1915 is going on).
1916 Also avoid substituting a return register into I3, because
1917 reload can't handle a conflict with constraints of other
1918 inputs. */
1919 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1920 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1921 return 0;
1923 else if (GET_CODE (dest) != CC0)
1924 return 0;
1927 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1928 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1929 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1931 /* Don't substitute for a register intended as a clobberable
1932 operand. */
1933 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1934 if (rtx_equal_p (reg, dest))
1935 return 0;
1937 /* If the clobber represents an earlyclobber operand, we must not
1938 substitute an expression containing the clobbered register.
1939 As we do not analyze the constraint strings here, we have to
1940 make the conservative assumption. However, if the register is
1941 a fixed hard reg, the clobber cannot represent any operand;
1942 we leave it up to the machine description to either accept or
1943 reject use-and-clobber patterns. */
1944 if (!REG_P (reg)
1945 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1946 || !fixed_regs[REGNO (reg)])
1947 if (reg_overlap_mentioned_p (reg, src))
1948 return 0;
1951 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1952 or not), reject, unless nothing volatile comes between it and I3 */
1954 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1956 /* Make sure neither succ nor succ2 contains a volatile reference. */
1957 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1958 return 0;
1959 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1960 return 0;
1961 /* We'll check insns between INSN and I3 below. */
1964 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1965 to be an explicit register variable, and was chosen for a reason. */
1967 if (GET_CODE (src) == ASM_OPERANDS
1968 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1969 return 0;
1971 /* If INSN contains volatile references (specifically volatile MEMs),
1972 we cannot combine across any other volatile references.
1973 Even if INSN doesn't contain volatile references, any intervening
1974 volatile insn might affect machine state. */
1976 is_volatile_p = volatile_refs_p (PATTERN (insn))
1977 ? volatile_refs_p
1978 : volatile_insn_p;
1980 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1981 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
1982 return 0;
1984 /* If INSN contains an autoincrement or autodecrement, make sure that
1985 register is not used between there and I3, and not already used in
1986 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1987 Also insist that I3 not be a jump; if it were one
1988 and the incremented register were spilled, we would lose. */
1990 #ifdef AUTO_INC_DEC
1991 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1992 if (REG_NOTE_KIND (link) == REG_INC
1993 && (JUMP_P (i3)
1994 || reg_used_between_p (XEXP (link, 0), insn, i3)
1995 || (pred != NULL_RTX
1996 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1997 || (pred2 != NULL_RTX
1998 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1999 || (succ != NULL_RTX
2000 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2001 || (succ2 != NULL_RTX
2002 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2003 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2004 return 0;
2005 #endif
2007 #ifdef HAVE_cc0
2008 /* Don't combine an insn that follows a CC0-setting insn.
2009 An insn that uses CC0 must not be separated from the one that sets it.
2010 We do, however, allow I2 to follow a CC0-setting insn if that insn
2011 is passed as I1; in that case it will be deleted also.
2012 We also allow combining in this case if all the insns are adjacent
2013 because that would leave the two CC0 insns adjacent as well.
2014 It would be more logical to test whether CC0 occurs inside I1 or I2,
2015 but that would be much slower, and this ought to be equivalent. */
2017 p = prev_nonnote_insn (insn);
2018 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2019 && ! all_adjacent)
2020 return 0;
2021 #endif
2023 /* If we get here, we have passed all the tests and the combination is
2024 to be allowed. */
2026 *pdest = dest;
2027 *psrc = src;
2029 return 1;
2032 /* LOC is the location within I3 that contains its pattern or the component
2033 of a PARALLEL of the pattern. We validate that it is valid for combining.
2035 One problem is if I3 modifies its output, as opposed to replacing it
2036 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2037 doing so would produce an insn that is not equivalent to the original insns.
2039 Consider:
2041 (set (reg:DI 101) (reg:DI 100))
2042 (set (subreg:SI (reg:DI 101) 0) <foo>)
2044 This is NOT equivalent to:
2046 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2047 (set (reg:DI 101) (reg:DI 100))])
2049 Not only does this modify 100 (in which case it might still be valid
2050 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2052 We can also run into a problem if I2 sets a register that I1
2053 uses and I1 gets directly substituted into I3 (not via I2). In that
2054 case, we would be getting the wrong value of I2DEST into I3, so we
2055 must reject the combination. This case occurs when I2 and I1 both
2056 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2057 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2058 of a SET must prevent combination from occurring. The same situation
2059 can occur for I0, in which case I0_NOT_IN_SRC is set.
2061 Before doing the above check, we first try to expand a field assignment
2062 into a set of logical operations.
2064 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2065 we place a register that is both set and used within I3. If more than one
2066 such register is detected, we fail.
2068 Return 1 if the combination is valid, zero otherwise. */
2070 static int
2071 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2072 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2074 rtx x = *loc;
2076 if (GET_CODE (x) == SET)
2078 rtx set = x ;
2079 rtx dest = SET_DEST (set);
2080 rtx src = SET_SRC (set);
2081 rtx inner_dest = dest;
2082 rtx subdest;
2084 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2085 || GET_CODE (inner_dest) == SUBREG
2086 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2087 inner_dest = XEXP (inner_dest, 0);
2089 /* Check for the case where I3 modifies its output, as discussed
2090 above. We don't want to prevent pseudos from being combined
2091 into the address of a MEM, so only prevent the combination if
2092 i1 or i2 set the same MEM. */
2093 if ((inner_dest != dest &&
2094 (!MEM_P (inner_dest)
2095 || rtx_equal_p (i2dest, inner_dest)
2096 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2097 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2098 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2099 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2100 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2102 /* This is the same test done in can_combine_p except we can't test
2103 all_adjacent; we don't have to, since this instruction will stay
2104 in place, thus we are not considering increasing the lifetime of
2105 INNER_DEST.
2107 Also, if this insn sets a function argument, combining it with
2108 something that might need a spill could clobber a previous
2109 function argument; the all_adjacent test in can_combine_p also
2110 checks this; here, we do a more specific test for this case. */
2112 || (REG_P (inner_dest)
2113 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2114 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2115 GET_MODE (inner_dest))))
2116 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2117 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2118 return 0;
2120 /* If DEST is used in I3, it is being killed in this insn, so
2121 record that for later. We have to consider paradoxical
2122 subregs here, since they kill the whole register, but we
2123 ignore partial subregs, STRICT_LOW_PART, etc.
2124 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2125 STACK_POINTER_REGNUM, since these are always considered to be
2126 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2127 subdest = dest;
2128 if (GET_CODE (subdest) == SUBREG
2129 && (GET_MODE_SIZE (GET_MODE (subdest))
2130 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2131 subdest = SUBREG_REG (subdest);
2132 if (pi3dest_killed
2133 && REG_P (subdest)
2134 && reg_referenced_p (subdest, PATTERN (i3))
2135 && REGNO (subdest) != FRAME_POINTER_REGNUM
2136 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2137 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2138 #endif
2139 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2140 && (REGNO (subdest) != ARG_POINTER_REGNUM
2141 || ! fixed_regs [REGNO (subdest)])
2142 #endif
2143 && REGNO (subdest) != STACK_POINTER_REGNUM)
2145 if (*pi3dest_killed)
2146 return 0;
2148 *pi3dest_killed = subdest;
2152 else if (GET_CODE (x) == PARALLEL)
2154 int i;
2156 for (i = 0; i < XVECLEN (x, 0); i++)
2157 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2158 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2159 return 0;
2162 return 1;
2165 /* Return 1 if X is an arithmetic expression that contains a multiplication
2166 and division. We don't count multiplications by powers of two here. */
2168 static int
2169 contains_muldiv (rtx x)
2171 switch (GET_CODE (x))
2173 case MOD: case DIV: case UMOD: case UDIV:
2174 return 1;
2176 case MULT:
2177 return ! (CONST_INT_P (XEXP (x, 1))
2178 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2179 default:
2180 if (BINARY_P (x))
2181 return contains_muldiv (XEXP (x, 0))
2182 || contains_muldiv (XEXP (x, 1));
2184 if (UNARY_P (x))
2185 return contains_muldiv (XEXP (x, 0));
2187 return 0;
2191 /* Determine whether INSN can be used in a combination. Return nonzero if
2192 not. This is used in try_combine to detect early some cases where we
2193 can't perform combinations. */
2195 static int
2196 cant_combine_insn_p (rtx insn)
2198 rtx set;
2199 rtx src, dest;
2201 /* If this isn't really an insn, we can't do anything.
2202 This can occur when flow deletes an insn that it has merged into an
2203 auto-increment address. */
2204 if (! INSN_P (insn))
2205 return 1;
2207 /* Never combine loads and stores involving hard regs that are likely
2208 to be spilled. The register allocator can usually handle such
2209 reg-reg moves by tying. If we allow the combiner to make
2210 substitutions of likely-spilled regs, reload might die.
2211 As an exception, we allow combinations involving fixed regs; these are
2212 not available to the register allocator so there's no risk involved. */
2214 set = single_set (insn);
2215 if (! set)
2216 return 0;
2217 src = SET_SRC (set);
2218 dest = SET_DEST (set);
2219 if (GET_CODE (src) == SUBREG)
2220 src = SUBREG_REG (src);
2221 if (GET_CODE (dest) == SUBREG)
2222 dest = SUBREG_REG (dest);
2223 if (REG_P (src) && REG_P (dest)
2224 && ((HARD_REGISTER_P (src)
2225 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2226 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2227 || (HARD_REGISTER_P (dest)
2228 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2229 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2230 return 1;
2232 return 0;
2235 struct likely_spilled_retval_info
2237 unsigned regno, nregs;
2238 unsigned mask;
2241 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2242 hard registers that are known to be written to / clobbered in full. */
2243 static void
2244 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2246 struct likely_spilled_retval_info *const info =
2247 (struct likely_spilled_retval_info *) data;
2248 unsigned regno, nregs;
2249 unsigned new_mask;
2251 if (!REG_P (XEXP (set, 0)))
2252 return;
2253 regno = REGNO (x);
2254 if (regno >= info->regno + info->nregs)
2255 return;
2256 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2257 if (regno + nregs <= info->regno)
2258 return;
2259 new_mask = (2U << (nregs - 1)) - 1;
2260 if (regno < info->regno)
2261 new_mask >>= info->regno - regno;
2262 else
2263 new_mask <<= regno - info->regno;
2264 info->mask &= ~new_mask;
2267 /* Return nonzero iff part of the return value is live during INSN, and
2268 it is likely spilled. This can happen when more than one insn is needed
2269 to copy the return value, e.g. when we consider to combine into the
2270 second copy insn for a complex value. */
2272 static int
2273 likely_spilled_retval_p (rtx insn)
2275 rtx use = BB_END (this_basic_block);
2276 rtx reg, p;
2277 unsigned regno, nregs;
2278 /* We assume here that no machine mode needs more than
2279 32 hard registers when the value overlaps with a register
2280 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2281 unsigned mask;
2282 struct likely_spilled_retval_info info;
2284 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2285 return 0;
2286 reg = XEXP (PATTERN (use), 0);
2287 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2288 return 0;
2289 regno = REGNO (reg);
2290 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2291 if (nregs == 1)
2292 return 0;
2293 mask = (2U << (nregs - 1)) - 1;
2295 /* Disregard parts of the return value that are set later. */
2296 info.regno = regno;
2297 info.nregs = nregs;
2298 info.mask = mask;
2299 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2300 if (INSN_P (p))
2301 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2302 mask = info.mask;
2304 /* Check if any of the (probably) live return value registers is
2305 likely spilled. */
2306 nregs --;
2309 if ((mask & 1 << nregs)
2310 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2311 return 1;
2312 } while (nregs--);
2313 return 0;
2316 /* Adjust INSN after we made a change to its destination.
2318 Changing the destination can invalidate notes that say something about
2319 the results of the insn and a LOG_LINK pointing to the insn. */
2321 static void
2322 adjust_for_new_dest (rtx insn)
2324 /* For notes, be conservative and simply remove them. */
2325 remove_reg_equal_equiv_notes (insn);
2327 /* The new insn will have a destination that was previously the destination
2328 of an insn just above it. Call distribute_links to make a LOG_LINK from
2329 the next use of that destination. */
2330 distribute_links (alloc_insn_link (insn, NULL));
2332 df_insn_rescan (insn);
2335 /* Return TRUE if combine can reuse reg X in mode MODE.
2336 ADDED_SETS is nonzero if the original set is still required. */
2337 static bool
2338 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2340 unsigned int regno;
2342 if (!REG_P (x))
2343 return false;
2345 regno = REGNO (x);
2346 /* Allow hard registers if the new mode is legal, and occupies no more
2347 registers than the old mode. */
2348 if (regno < FIRST_PSEUDO_REGISTER)
2349 return (HARD_REGNO_MODE_OK (regno, mode)
2350 && (hard_regno_nregs[regno][GET_MODE (x)]
2351 >= hard_regno_nregs[regno][mode]));
2353 /* Or a pseudo that is only used once. */
2354 return (REG_N_SETS (regno) == 1 && !added_sets
2355 && !REG_USERVAR_P (x));
2359 /* Check whether X, the destination of a set, refers to part of
2360 the register specified by REG. */
2362 static bool
2363 reg_subword_p (rtx x, rtx reg)
2365 /* Check that reg is an integer mode register. */
2366 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2367 return false;
2369 if (GET_CODE (x) == STRICT_LOW_PART
2370 || GET_CODE (x) == ZERO_EXTRACT)
2371 x = XEXP (x, 0);
2373 return GET_CODE (x) == SUBREG
2374 && SUBREG_REG (x) == reg
2375 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2378 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2379 Note that the INSN should be deleted *after* removing dead edges, so
2380 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2381 but not for a (set (pc) (label_ref FOO)). */
2383 static void
2384 update_cfg_for_uncondjump (rtx insn)
2386 basic_block bb = BLOCK_FOR_INSN (insn);
2387 gcc_assert (BB_END (bb) == insn);
2389 purge_dead_edges (bb);
2391 delete_insn (insn);
2392 if (EDGE_COUNT (bb->succs) == 1)
2394 rtx insn;
2396 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2398 /* Remove barriers from the footer if there are any. */
2399 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2400 if (BARRIER_P (insn))
2402 if (PREV_INSN (insn))
2403 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2404 else
2405 BB_FOOTER (bb) = NEXT_INSN (insn);
2406 if (NEXT_INSN (insn))
2407 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2409 else if (LABEL_P (insn))
2410 break;
2414 /* Try to combine the insns I0, I1 and I2 into I3.
2415 Here I0, I1 and I2 appear earlier than I3.
2416 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2419 If we are combining more than two insns and the resulting insn is not
2420 recognized, try splitting it into two insns. If that happens, I2 and I3
2421 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2422 Otherwise, I0, I1 and I2 are pseudo-deleted.
2424 Return 0 if the combination does not work. Then nothing is changed.
2425 If we did the combination, return the insn at which combine should
2426 resume scanning.
2428 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2429 new direct jump instruction.
2431 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2432 been I3 passed to an earlier try_combine within the same basic
2433 block. */
2435 static rtx
2436 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p,
2437 rtx last_combined_insn)
2439 /* New patterns for I3 and I2, respectively. */
2440 rtx newpat, newi2pat = 0;
2441 rtvec newpat_vec_with_clobbers = 0;
2442 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2443 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2444 dead. */
2445 int added_sets_0, added_sets_1, added_sets_2;
2446 /* Total number of SETs to put into I3. */
2447 int total_sets;
2448 /* Nonzero if I2's or I1's body now appears in I3. */
2449 int i2_is_used = 0, i1_is_used = 0;
2450 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2451 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2452 /* Contains I3 if the destination of I3 is used in its source, which means
2453 that the old life of I3 is being killed. If that usage is placed into
2454 I2 and not in I3, a REG_DEAD note must be made. */
2455 rtx i3dest_killed = 0;
2456 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2457 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2458 /* Copy of SET_SRC of I1 and I0, if needed. */
2459 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2460 /* Set if I2DEST was reused as a scratch register. */
2461 bool i2scratch = false;
2462 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2463 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2464 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2465 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2466 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2467 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2468 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2469 /* Notes that must be added to REG_NOTES in I3 and I2. */
2470 rtx new_i3_notes, new_i2_notes;
2471 /* Notes that we substituted I3 into I2 instead of the normal case. */
2472 int i3_subst_into_i2 = 0;
2473 /* Notes that I1, I2 or I3 is a MULT operation. */
2474 int have_mult = 0;
2475 int swap_i2i3 = 0;
2476 int changed_i3_dest = 0;
2478 int maxreg;
2479 rtx temp;
2480 struct insn_link *link;
2481 rtx other_pat = 0;
2482 rtx new_other_notes;
2483 int i;
2485 /* Only try four-insn combinations when there's high likelihood of
2486 success. Look for simple insns, such as loads of constants or
2487 binary operations involving a constant. */
2488 if (i0)
2490 int i;
2491 int ngood = 0;
2492 int nshift = 0;
2494 if (!flag_expensive_optimizations)
2495 return 0;
2497 for (i = 0; i < 4; i++)
2499 rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2500 rtx set = single_set (insn);
2501 rtx src;
2502 if (!set)
2503 continue;
2504 src = SET_SRC (set);
2505 if (CONSTANT_P (src))
2507 ngood += 2;
2508 break;
2510 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2511 ngood++;
2512 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2513 || GET_CODE (src) == LSHIFTRT)
2514 nshift++;
2516 if (ngood < 2 && nshift < 2)
2517 return 0;
2520 /* Exit early if one of the insns involved can't be used for
2521 combinations. */
2522 if (cant_combine_insn_p (i3)
2523 || cant_combine_insn_p (i2)
2524 || (i1 && cant_combine_insn_p (i1))
2525 || (i0 && cant_combine_insn_p (i0))
2526 || likely_spilled_retval_p (i3))
2527 return 0;
2529 combine_attempts++;
2530 undobuf.other_insn = 0;
2532 /* Reset the hard register usage information. */
2533 CLEAR_HARD_REG_SET (newpat_used_regs);
2535 if (dump_file && (dump_flags & TDF_DETAILS))
2537 if (i0)
2538 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2539 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2540 else if (i1)
2541 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2542 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2543 else
2544 fprintf (dump_file, "\nTrying %d -> %d:\n",
2545 INSN_UID (i2), INSN_UID (i3));
2548 /* If multiple insns feed into one of I2 or I3, they can be in any
2549 order. To simplify the code below, reorder them in sequence. */
2550 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2551 temp = i2, i2 = i0, i0 = temp;
2552 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2553 temp = i1, i1 = i0, i0 = temp;
2554 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2555 temp = i1, i1 = i2, i2 = temp;
2557 added_links_insn = 0;
2559 /* First check for one important special case that the code below will
2560 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2561 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2562 we may be able to replace that destination with the destination of I3.
2563 This occurs in the common code where we compute both a quotient and
2564 remainder into a structure, in which case we want to do the computation
2565 directly into the structure to avoid register-register copies.
2567 Note that this case handles both multiple sets in I2 and also cases
2568 where I2 has a number of CLOBBERs inside the PARALLEL.
2570 We make very conservative checks below and only try to handle the
2571 most common cases of this. For example, we only handle the case
2572 where I2 and I3 are adjacent to avoid making difficult register
2573 usage tests. */
2575 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2576 && REG_P (SET_SRC (PATTERN (i3)))
2577 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2578 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2579 && GET_CODE (PATTERN (i2)) == PARALLEL
2580 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2581 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2582 below would need to check what is inside (and reg_overlap_mentioned_p
2583 doesn't support those codes anyway). Don't allow those destinations;
2584 the resulting insn isn't likely to be recognized anyway. */
2585 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2586 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2587 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2588 SET_DEST (PATTERN (i3)))
2589 && next_active_insn (i2) == i3)
2591 rtx p2 = PATTERN (i2);
2593 /* Make sure that the destination of I3,
2594 which we are going to substitute into one output of I2,
2595 is not used within another output of I2. We must avoid making this:
2596 (parallel [(set (mem (reg 69)) ...)
2597 (set (reg 69) ...)])
2598 which is not well-defined as to order of actions.
2599 (Besides, reload can't handle output reloads for this.)
2601 The problem can also happen if the dest of I3 is a memory ref,
2602 if another dest in I2 is an indirect memory ref. */
2603 for (i = 0; i < XVECLEN (p2, 0); i++)
2604 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2605 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2606 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2607 SET_DEST (XVECEXP (p2, 0, i))))
2608 break;
2610 if (i == XVECLEN (p2, 0))
2611 for (i = 0; i < XVECLEN (p2, 0); i++)
2612 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2613 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2615 combine_merges++;
2617 subst_insn = i3;
2618 subst_low_luid = DF_INSN_LUID (i2);
2620 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2621 i2src = SET_SRC (XVECEXP (p2, 0, i));
2622 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2623 i2dest_killed = dead_or_set_p (i2, i2dest);
2625 /* Replace the dest in I2 with our dest and make the resulting
2626 insn the new pattern for I3. Then skip to where we validate
2627 the pattern. Everything was set up above. */
2628 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2629 newpat = p2;
2630 i3_subst_into_i2 = 1;
2631 goto validate_replacement;
2635 /* If I2 is setting a pseudo to a constant and I3 is setting some
2636 sub-part of it to another constant, merge them by making a new
2637 constant. */
2638 if (i1 == 0
2639 && (temp = single_set (i2)) != 0
2640 && CONST_SCALAR_INT_P (SET_SRC (temp))
2641 && GET_CODE (PATTERN (i3)) == SET
2642 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2643 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2645 rtx dest = SET_DEST (PATTERN (i3));
2646 int offset = -1;
2647 int width = 0;
2649 if (GET_CODE (dest) == ZERO_EXTRACT)
2651 if (CONST_INT_P (XEXP (dest, 1))
2652 && CONST_INT_P (XEXP (dest, 2)))
2654 width = INTVAL (XEXP (dest, 1));
2655 offset = INTVAL (XEXP (dest, 2));
2656 dest = XEXP (dest, 0);
2657 if (BITS_BIG_ENDIAN)
2658 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2661 else
2663 if (GET_CODE (dest) == STRICT_LOW_PART)
2664 dest = XEXP (dest, 0);
2665 width = GET_MODE_PRECISION (GET_MODE (dest));
2666 offset = 0;
2669 if (offset >= 0)
2671 /* If this is the low part, we're done. */
2672 if (subreg_lowpart_p (dest))
2674 /* Handle the case where inner is twice the size of outer. */
2675 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp)))
2676 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2677 offset += GET_MODE_PRECISION (GET_MODE (dest));
2678 /* Otherwise give up for now. */
2679 else
2680 offset = -1;
2683 if (offset >= 0
2684 && (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp)))
2685 <= HOST_BITS_PER_DOUBLE_INT))
2687 double_int m, o, i;
2688 rtx inner = SET_SRC (PATTERN (i3));
2689 rtx outer = SET_SRC (temp);
2691 o = rtx_to_double_int (outer);
2692 i = rtx_to_double_int (inner);
2694 m = double_int::mask (width);
2695 i &= m;
2696 m = m.llshift (offset, HOST_BITS_PER_DOUBLE_INT);
2697 i = i.llshift (offset, HOST_BITS_PER_DOUBLE_INT);
2698 o = o.and_not (m) | i;
2700 combine_merges++;
2701 subst_insn = i3;
2702 subst_low_luid = DF_INSN_LUID (i2);
2703 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2704 i2dest = SET_DEST (temp);
2705 i2dest_killed = dead_or_set_p (i2, i2dest);
2707 /* Replace the source in I2 with the new constant and make the
2708 resulting insn the new pattern for I3. Then skip to where we
2709 validate the pattern. Everything was set up above. */
2710 SUBST (SET_SRC (temp),
2711 immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2713 newpat = PATTERN (i2);
2715 /* The dest of I3 has been replaced with the dest of I2. */
2716 changed_i3_dest = 1;
2717 goto validate_replacement;
2721 #ifndef HAVE_cc0
2722 /* If we have no I1 and I2 looks like:
2723 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2724 (set Y OP)])
2725 make up a dummy I1 that is
2726 (set Y OP)
2727 and change I2 to be
2728 (set (reg:CC X) (compare:CC Y (const_int 0)))
2730 (We can ignore any trailing CLOBBERs.)
2732 This undoes a previous combination and allows us to match a branch-and-
2733 decrement insn. */
2735 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2736 && XVECLEN (PATTERN (i2), 0) >= 2
2737 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2738 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2739 == MODE_CC)
2740 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2741 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2742 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2743 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2744 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2745 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2747 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2748 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2749 break;
2751 if (i == 1)
2753 /* We make I1 with the same INSN_UID as I2. This gives it
2754 the same DF_INSN_LUID for value tracking. Our fake I1 will
2755 never appear in the insn stream so giving it the same INSN_UID
2756 as I2 will not cause a problem. */
2758 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2759 BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2760 INSN_LOCATION (i2), -1, NULL_RTX);
2762 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2763 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2764 SET_DEST (PATTERN (i1)));
2765 SUBST_LINK (LOG_LINKS (i2), alloc_insn_link (i1, LOG_LINKS (i2)));
2768 #endif
2770 /* Verify that I2 and I1 are valid for combining. */
2771 if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2772 || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2773 &i1dest, &i1src))
2774 || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2775 &i0dest, &i0src)))
2777 undo_all ();
2778 return 0;
2781 /* Record whether I2DEST is used in I2SRC and similarly for the other
2782 cases. Knowing this will help in register status updating below. */
2783 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2784 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2785 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2786 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2787 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2788 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2789 i2dest_killed = dead_or_set_p (i2, i2dest);
2790 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2791 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2793 /* For the earlier insns, determine which of the subsequent ones they
2794 feed. */
2795 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2796 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2797 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2798 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2799 && reg_overlap_mentioned_p (i0dest, i2src))));
2801 /* Ensure that I3's pattern can be the destination of combines. */
2802 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2803 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2804 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2805 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2806 &i3dest_killed))
2808 undo_all ();
2809 return 0;
2812 /* See if any of the insns is a MULT operation. Unless one is, we will
2813 reject a combination that is, since it must be slower. Be conservative
2814 here. */
2815 if (GET_CODE (i2src) == MULT
2816 || (i1 != 0 && GET_CODE (i1src) == MULT)
2817 || (i0 != 0 && GET_CODE (i0src) == MULT)
2818 || (GET_CODE (PATTERN (i3)) == SET
2819 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2820 have_mult = 1;
2822 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2823 We used to do this EXCEPT in one case: I3 has a post-inc in an
2824 output operand. However, that exception can give rise to insns like
2825 mov r3,(r3)+
2826 which is a famous insn on the PDP-11 where the value of r3 used as the
2827 source was model-dependent. Avoid this sort of thing. */
2829 #if 0
2830 if (!(GET_CODE (PATTERN (i3)) == SET
2831 && REG_P (SET_SRC (PATTERN (i3)))
2832 && MEM_P (SET_DEST (PATTERN (i3)))
2833 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2834 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2835 /* It's not the exception. */
2836 #endif
2837 #ifdef AUTO_INC_DEC
2839 rtx link;
2840 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2841 if (REG_NOTE_KIND (link) == REG_INC
2842 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2843 || (i1 != 0
2844 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2846 undo_all ();
2847 return 0;
2850 #endif
2852 /* See if the SETs in I1 or I2 need to be kept around in the merged
2853 instruction: whenever the value set there is still needed past I3.
2854 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2856 For the SET in I1, we have two cases: if I1 and I2 independently feed
2857 into I3, the set in I1 needs to be kept around unless I1DEST dies
2858 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2859 in I1 needs to be kept around unless I1DEST dies or is set in either
2860 I2 or I3. The same considerations apply to I0. */
2862 added_sets_2 = !dead_or_set_p (i3, i2dest);
2864 if (i1)
2865 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2866 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2867 else
2868 added_sets_1 = 0;
2870 if (i0)
2871 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2872 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
2873 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
2874 && dead_or_set_p (i2, i0dest)));
2875 else
2876 added_sets_0 = 0;
2878 /* We are about to copy insns for the case where they need to be kept
2879 around. Check that they can be copied in the merged instruction. */
2881 if (targetm.cannot_copy_insn_p
2882 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2883 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2884 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2886 undo_all ();
2887 return 0;
2890 /* If the set in I2 needs to be kept around, we must make a copy of
2891 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2892 PATTERN (I2), we are only substituting for the original I1DEST, not into
2893 an already-substituted copy. This also prevents making self-referential
2894 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2895 I2DEST. */
2897 if (added_sets_2)
2899 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2900 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2901 else
2902 i2pat = copy_rtx (PATTERN (i2));
2905 if (added_sets_1)
2907 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2908 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2909 else
2910 i1pat = copy_rtx (PATTERN (i1));
2913 if (added_sets_0)
2915 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2916 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2917 else
2918 i0pat = copy_rtx (PATTERN (i0));
2921 combine_merges++;
2923 /* Substitute in the latest insn for the regs set by the earlier ones. */
2925 maxreg = max_reg_num ();
2927 subst_insn = i3;
2929 #ifndef HAVE_cc0
2930 /* Many machines that don't use CC0 have insns that can both perform an
2931 arithmetic operation and set the condition code. These operations will
2932 be represented as a PARALLEL with the first element of the vector
2933 being a COMPARE of an arithmetic operation with the constant zero.
2934 The second element of the vector will set some pseudo to the result
2935 of the same arithmetic operation. If we simplify the COMPARE, we won't
2936 match such a pattern and so will generate an extra insn. Here we test
2937 for this case, where both the comparison and the operation result are
2938 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2939 I2SRC. Later we will make the PARALLEL that contains I2. */
2941 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2942 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2943 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
2944 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2946 rtx newpat_dest;
2947 rtx *cc_use_loc = NULL, cc_use_insn = NULL_RTX;
2948 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
2949 enum machine_mode compare_mode, orig_compare_mode;
2950 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
2952 newpat = PATTERN (i3);
2953 newpat_dest = SET_DEST (newpat);
2954 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
2956 if (undobuf.other_insn == 0
2957 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
2958 &cc_use_insn)))
2960 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
2961 compare_code = simplify_compare_const (compare_code,
2962 GET_MODE (i2dest), op0, &op1);
2963 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
2966 /* Do the rest only if op1 is const0_rtx, which may be the
2967 result of simplification. */
2968 if (op1 == const0_rtx)
2970 /* If a single use of the CC is found, prepare to modify it
2971 when SELECT_CC_MODE returns a new CC-class mode, or when
2972 the above simplify_compare_const() returned a new comparison
2973 operator. undobuf.other_insn is assigned the CC use insn
2974 when modifying it. */
2975 if (cc_use_loc)
2977 #ifdef SELECT_CC_MODE
2978 enum machine_mode new_mode
2979 = SELECT_CC_MODE (compare_code, op0, op1);
2980 if (new_mode != orig_compare_mode
2981 && can_change_dest_mode (SET_DEST (newpat),
2982 added_sets_2, new_mode))
2984 unsigned int regno = REGNO (newpat_dest);
2985 compare_mode = new_mode;
2986 if (regno < FIRST_PSEUDO_REGISTER)
2987 newpat_dest = gen_rtx_REG (compare_mode, regno);
2988 else
2990 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2991 newpat_dest = regno_reg_rtx[regno];
2994 #endif
2995 /* Cases for modifying the CC-using comparison. */
2996 if (compare_code != orig_compare_code
2997 /* ??? Do we need to verify the zero rtx? */
2998 && XEXP (*cc_use_loc, 1) == const0_rtx)
3000 /* Replace cc_use_loc with entire new RTX. */
3001 SUBST (*cc_use_loc,
3002 gen_rtx_fmt_ee (compare_code, compare_mode,
3003 newpat_dest, const0_rtx));
3004 undobuf.other_insn = cc_use_insn;
3006 else if (compare_mode != orig_compare_mode)
3008 /* Just replace the CC reg with a new mode. */
3009 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3010 undobuf.other_insn = cc_use_insn;
3014 /* Now we modify the current newpat:
3015 First, SET_DEST(newpat) is updated if the CC mode has been
3016 altered. For targets without SELECT_CC_MODE, this should be
3017 optimized away. */
3018 if (compare_mode != orig_compare_mode)
3019 SUBST (SET_DEST (newpat), newpat_dest);
3020 /* This is always done to propagate i2src into newpat. */
3021 SUBST (SET_SRC (newpat),
3022 gen_rtx_COMPARE (compare_mode, op0, op1));
3023 /* Create new version of i2pat if needed; the below PARALLEL
3024 creation needs this to work correctly. */
3025 if (! rtx_equal_p (i2src, op0))
3026 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3027 i2_is_used = 1;
3030 #endif
3032 if (i2_is_used == 0)
3034 /* It is possible that the source of I2 or I1 may be performing
3035 an unneeded operation, such as a ZERO_EXTEND of something
3036 that is known to have the high part zero. Handle that case
3037 by letting subst look at the inner insns.
3039 Another way to do this would be to have a function that tries
3040 to simplify a single insn instead of merging two or more
3041 insns. We don't do this because of the potential of infinite
3042 loops and because of the potential extra memory required.
3043 However, doing it the way we are is a bit of a kludge and
3044 doesn't catch all cases.
3046 But only do this if -fexpensive-optimizations since it slows
3047 things down and doesn't usually win.
3049 This is not done in the COMPARE case above because the
3050 unmodified I2PAT is used in the PARALLEL and so a pattern
3051 with a modified I2SRC would not match. */
3053 if (flag_expensive_optimizations)
3055 /* Pass pc_rtx so no substitutions are done, just
3056 simplifications. */
3057 if (i1)
3059 subst_low_luid = DF_INSN_LUID (i1);
3060 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3063 subst_low_luid = DF_INSN_LUID (i2);
3064 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3067 n_occurrences = 0; /* `subst' counts here */
3068 subst_low_luid = DF_INSN_LUID (i2);
3070 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3071 copy of I2SRC each time we substitute it, in order to avoid creating
3072 self-referential RTL when we will be substituting I1SRC for I1DEST
3073 later. Likewise if I0 feeds into I2, either directly or indirectly
3074 through I1, and I0DEST is in I0SRC. */
3075 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3076 (i1_feeds_i2_n && i1dest_in_i1src)
3077 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3078 && i0dest_in_i0src));
3079 substed_i2 = 1;
3081 /* Record whether I2's body now appears within I3's body. */
3082 i2_is_used = n_occurrences;
3085 /* If we already got a failure, don't try to do more. Otherwise, try to
3086 substitute I1 if we have it. */
3088 if (i1 && GET_CODE (newpat) != CLOBBER)
3090 /* Check that an autoincrement side-effect on I1 has not been lost.
3091 This happens if I1DEST is mentioned in I2 and dies there, and
3092 has disappeared from the new pattern. */
3093 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3094 && i1_feeds_i2_n
3095 && dead_or_set_p (i2, i1dest)
3096 && !reg_overlap_mentioned_p (i1dest, newpat))
3097 /* Before we can do this substitution, we must redo the test done
3098 above (see detailed comments there) that ensures I1DEST isn't
3099 mentioned in any SETs in NEWPAT that are field assignments. */
3100 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3101 0, 0, 0))
3103 undo_all ();
3104 return 0;
3107 n_occurrences = 0;
3108 subst_low_luid = DF_INSN_LUID (i1);
3110 /* If the following substitution will modify I1SRC, make a copy of it
3111 for the case where it is substituted for I1DEST in I2PAT later. */
3112 if (added_sets_2 && i1_feeds_i2_n)
3113 i1src_copy = copy_rtx (i1src);
3115 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3116 copy of I1SRC each time we substitute it, in order to avoid creating
3117 self-referential RTL when we will be substituting I0SRC for I0DEST
3118 later. */
3119 newpat = subst (newpat, i1dest, i1src, 0, 0,
3120 i0_feeds_i1_n && i0dest_in_i0src);
3121 substed_i1 = 1;
3123 /* Record whether I1's body now appears within I3's body. */
3124 i1_is_used = n_occurrences;
3127 /* Likewise for I0 if we have it. */
3129 if (i0 && GET_CODE (newpat) != CLOBBER)
3131 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3132 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3133 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3134 && !reg_overlap_mentioned_p (i0dest, newpat))
3135 || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3136 0, 0, 0))
3138 undo_all ();
3139 return 0;
3142 /* If the following substitution will modify I0SRC, make a copy of it
3143 for the case where it is substituted for I0DEST in I1PAT later. */
3144 if (added_sets_1 && i0_feeds_i1_n)
3145 i0src_copy = copy_rtx (i0src);
3146 /* And a copy for I0DEST in I2PAT substitution. */
3147 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3148 || (i0_feeds_i2_n)))
3149 i0src_copy2 = copy_rtx (i0src);
3151 n_occurrences = 0;
3152 subst_low_luid = DF_INSN_LUID (i0);
3153 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3154 substed_i0 = 1;
3157 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3158 to count all the ways that I2SRC and I1SRC can be used. */
3159 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3160 && i2_is_used + added_sets_2 > 1)
3161 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3162 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3163 > 1))
3164 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3165 && (n_occurrences + added_sets_0
3166 + (added_sets_1 && i0_feeds_i1_n)
3167 + (added_sets_2 && i0_feeds_i2_n)
3168 > 1))
3169 /* Fail if we tried to make a new register. */
3170 || max_reg_num () != maxreg
3171 /* Fail if we couldn't do something and have a CLOBBER. */
3172 || GET_CODE (newpat) == CLOBBER
3173 /* Fail if this new pattern is a MULT and we didn't have one before
3174 at the outer level. */
3175 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3176 && ! have_mult))
3178 undo_all ();
3179 return 0;
3182 /* If the actions of the earlier insns must be kept
3183 in addition to substituting them into the latest one,
3184 we must make a new PARALLEL for the latest insn
3185 to hold additional the SETs. */
3187 if (added_sets_0 || added_sets_1 || added_sets_2)
3189 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3190 combine_extras++;
3192 if (GET_CODE (newpat) == PARALLEL)
3194 rtvec old = XVEC (newpat, 0);
3195 total_sets = XVECLEN (newpat, 0) + extra_sets;
3196 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3197 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3198 sizeof (old->elem[0]) * old->num_elem);
3200 else
3202 rtx old = newpat;
3203 total_sets = 1 + extra_sets;
3204 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3205 XVECEXP (newpat, 0, 0) = old;
3208 if (added_sets_0)
3209 XVECEXP (newpat, 0, --total_sets) = i0pat;
3211 if (added_sets_1)
3213 rtx t = i1pat;
3214 if (i0_feeds_i1_n)
3215 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3217 XVECEXP (newpat, 0, --total_sets) = t;
3219 if (added_sets_2)
3221 rtx t = i2pat;
3222 if (i1_feeds_i2_n)
3223 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3224 i0_feeds_i1_n && i0dest_in_i0src);
3225 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3226 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3228 XVECEXP (newpat, 0, --total_sets) = t;
3232 validate_replacement:
3234 /* Note which hard regs this insn has as inputs. */
3235 mark_used_regs_combine (newpat);
3237 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3238 consider splitting this pattern, we might need these clobbers. */
3239 if (i1 && GET_CODE (newpat) == PARALLEL
3240 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3242 int len = XVECLEN (newpat, 0);
3244 newpat_vec_with_clobbers = rtvec_alloc (len);
3245 for (i = 0; i < len; i++)
3246 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3249 /* Is the result of combination a valid instruction? */
3250 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3252 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3253 the second SET's destination is a register that is unused and isn't
3254 marked as an instruction that might trap in an EH region. In that case,
3255 we just need the first SET. This can occur when simplifying a divmod
3256 insn. We *must* test for this case here because the code below that
3257 splits two independent SETs doesn't handle this case correctly when it
3258 updates the register status.
3260 It's pointless doing this if we originally had two sets, one from
3261 i3, and one from i2. Combining then splitting the parallel results
3262 in the original i2 again plus an invalid insn (which we delete).
3263 The net effect is only to move instructions around, which makes
3264 debug info less accurate.
3266 Also check the case where the first SET's destination is unused.
3267 That would not cause incorrect code, but does cause an unneeded
3268 insn to remain. */
3270 if (insn_code_number < 0
3271 && !(added_sets_2 && i1 == 0)
3272 && GET_CODE (newpat) == PARALLEL
3273 && XVECLEN (newpat, 0) == 2
3274 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3275 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3276 && asm_noperands (newpat) < 0)
3278 rtx set0 = XVECEXP (newpat, 0, 0);
3279 rtx set1 = XVECEXP (newpat, 0, 1);
3281 if (((REG_P (SET_DEST (set1))
3282 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3283 || (GET_CODE (SET_DEST (set1)) == SUBREG
3284 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3285 && insn_nothrow_p (i3)
3286 && !side_effects_p (SET_SRC (set1)))
3288 newpat = set0;
3289 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3292 else if (((REG_P (SET_DEST (set0))
3293 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3294 || (GET_CODE (SET_DEST (set0)) == SUBREG
3295 && find_reg_note (i3, REG_UNUSED,
3296 SUBREG_REG (SET_DEST (set0)))))
3297 && insn_nothrow_p (i3)
3298 && !side_effects_p (SET_SRC (set0)))
3300 newpat = set1;
3301 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3303 if (insn_code_number >= 0)
3304 changed_i3_dest = 1;
3308 /* If we were combining three insns and the result is a simple SET
3309 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3310 insns. There are two ways to do this. It can be split using a
3311 machine-specific method (like when you have an addition of a large
3312 constant) or by combine in the function find_split_point. */
3314 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3315 && asm_noperands (newpat) < 0)
3317 rtx parallel, m_split, *split;
3319 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3320 use I2DEST as a scratch register will help. In the latter case,
3321 convert I2DEST to the mode of the source of NEWPAT if we can. */
3323 m_split = combine_split_insns (newpat, i3);
3325 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3326 inputs of NEWPAT. */
3328 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3329 possible to try that as a scratch reg. This would require adding
3330 more code to make it work though. */
3332 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3334 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3336 /* First try to split using the original register as a
3337 scratch register. */
3338 parallel = gen_rtx_PARALLEL (VOIDmode,
3339 gen_rtvec (2, newpat,
3340 gen_rtx_CLOBBER (VOIDmode,
3341 i2dest)));
3342 m_split = combine_split_insns (parallel, i3);
3344 /* If that didn't work, try changing the mode of I2DEST if
3345 we can. */
3346 if (m_split == 0
3347 && new_mode != GET_MODE (i2dest)
3348 && new_mode != VOIDmode
3349 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3351 enum machine_mode old_mode = GET_MODE (i2dest);
3352 rtx ni2dest;
3354 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3355 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3356 else
3358 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3359 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3362 parallel = (gen_rtx_PARALLEL
3363 (VOIDmode,
3364 gen_rtvec (2, newpat,
3365 gen_rtx_CLOBBER (VOIDmode,
3366 ni2dest))));
3367 m_split = combine_split_insns (parallel, i3);
3369 if (m_split == 0
3370 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3372 struct undo *buf;
3374 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3375 buf = undobuf.undos;
3376 undobuf.undos = buf->next;
3377 buf->next = undobuf.frees;
3378 undobuf.frees = buf;
3382 i2scratch = m_split != 0;
3385 /* If recog_for_combine has discarded clobbers, try to use them
3386 again for the split. */
3387 if (m_split == 0 && newpat_vec_with_clobbers)
3389 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3390 m_split = combine_split_insns (parallel, i3);
3393 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3395 m_split = PATTERN (m_split);
3396 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3397 if (insn_code_number >= 0)
3398 newpat = m_split;
3400 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3401 && (next_nonnote_nondebug_insn (i2) == i3
3402 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3404 rtx i2set, i3set;
3405 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3406 newi2pat = PATTERN (m_split);
3408 i3set = single_set (NEXT_INSN (m_split));
3409 i2set = single_set (m_split);
3411 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3413 /* If I2 or I3 has multiple SETs, we won't know how to track
3414 register status, so don't use these insns. If I2's destination
3415 is used between I2 and I3, we also can't use these insns. */
3417 if (i2_code_number >= 0 && i2set && i3set
3418 && (next_nonnote_nondebug_insn (i2) == i3
3419 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3420 insn_code_number = recog_for_combine (&newi3pat, i3,
3421 &new_i3_notes);
3422 if (insn_code_number >= 0)
3423 newpat = newi3pat;
3425 /* It is possible that both insns now set the destination of I3.
3426 If so, we must show an extra use of it. */
3428 if (insn_code_number >= 0)
3430 rtx new_i3_dest = SET_DEST (i3set);
3431 rtx new_i2_dest = SET_DEST (i2set);
3433 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3434 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3435 || GET_CODE (new_i3_dest) == SUBREG)
3436 new_i3_dest = XEXP (new_i3_dest, 0);
3438 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3439 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3440 || GET_CODE (new_i2_dest) == SUBREG)
3441 new_i2_dest = XEXP (new_i2_dest, 0);
3443 if (REG_P (new_i3_dest)
3444 && REG_P (new_i2_dest)
3445 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3446 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3450 /* If we can split it and use I2DEST, go ahead and see if that
3451 helps things be recognized. Verify that none of the registers
3452 are set between I2 and I3. */
3453 if (insn_code_number < 0
3454 && (split = find_split_point (&newpat, i3, false)) != 0
3455 #ifdef HAVE_cc0
3456 && REG_P (i2dest)
3457 #endif
3458 /* We need I2DEST in the proper mode. If it is a hard register
3459 or the only use of a pseudo, we can change its mode.
3460 Make sure we don't change a hard register to have a mode that
3461 isn't valid for it, or change the number of registers. */
3462 && (GET_MODE (*split) == GET_MODE (i2dest)
3463 || GET_MODE (*split) == VOIDmode
3464 || can_change_dest_mode (i2dest, added_sets_2,
3465 GET_MODE (*split)))
3466 && (next_nonnote_nondebug_insn (i2) == i3
3467 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3468 /* We can't overwrite I2DEST if its value is still used by
3469 NEWPAT. */
3470 && ! reg_referenced_p (i2dest, newpat))
3472 rtx newdest = i2dest;
3473 enum rtx_code split_code = GET_CODE (*split);
3474 enum machine_mode split_mode = GET_MODE (*split);
3475 bool subst_done = false;
3476 newi2pat = NULL_RTX;
3478 i2scratch = true;
3480 /* *SPLIT may be part of I2SRC, so make sure we have the
3481 original expression around for later debug processing.
3482 We should not need I2SRC any more in other cases. */
3483 if (MAY_HAVE_DEBUG_INSNS)
3484 i2src = copy_rtx (i2src);
3485 else
3486 i2src = NULL;
3488 /* Get NEWDEST as a register in the proper mode. We have already
3489 validated that we can do this. */
3490 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3492 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3493 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3494 else
3496 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3497 newdest = regno_reg_rtx[REGNO (i2dest)];
3501 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3502 an ASHIFT. This can occur if it was inside a PLUS and hence
3503 appeared to be a memory address. This is a kludge. */
3504 if (split_code == MULT
3505 && CONST_INT_P (XEXP (*split, 1))
3506 && INTVAL (XEXP (*split, 1)) > 0
3507 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3509 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3510 XEXP (*split, 0), GEN_INT (i)));
3511 /* Update split_code because we may not have a multiply
3512 anymore. */
3513 split_code = GET_CODE (*split);
3516 #ifdef INSN_SCHEDULING
3517 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3518 be written as a ZERO_EXTEND. */
3519 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3521 #ifdef LOAD_EXTEND_OP
3522 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3523 what it really is. */
3524 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3525 == SIGN_EXTEND)
3526 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3527 SUBREG_REG (*split)));
3528 else
3529 #endif
3530 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3531 SUBREG_REG (*split)));
3533 #endif
3535 /* Attempt to split binary operators using arithmetic identities. */
3536 if (BINARY_P (SET_SRC (newpat))
3537 && split_mode == GET_MODE (SET_SRC (newpat))
3538 && ! side_effects_p (SET_SRC (newpat)))
3540 rtx setsrc = SET_SRC (newpat);
3541 enum machine_mode mode = GET_MODE (setsrc);
3542 enum rtx_code code = GET_CODE (setsrc);
3543 rtx src_op0 = XEXP (setsrc, 0);
3544 rtx src_op1 = XEXP (setsrc, 1);
3546 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3547 if (rtx_equal_p (src_op0, src_op1))
3549 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3550 SUBST (XEXP (setsrc, 0), newdest);
3551 SUBST (XEXP (setsrc, 1), newdest);
3552 subst_done = true;
3554 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3555 else if ((code == PLUS || code == MULT)
3556 && GET_CODE (src_op0) == code
3557 && GET_CODE (XEXP (src_op0, 0)) == code
3558 && (INTEGRAL_MODE_P (mode)
3559 || (FLOAT_MODE_P (mode)
3560 && flag_unsafe_math_optimizations)))
3562 rtx p = XEXP (XEXP (src_op0, 0), 0);
3563 rtx q = XEXP (XEXP (src_op0, 0), 1);
3564 rtx r = XEXP (src_op0, 1);
3565 rtx s = src_op1;
3567 /* Split both "((X op Y) op X) op Y" and
3568 "((X op Y) op Y) op X" as "T op T" where T is
3569 "X op Y". */
3570 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3571 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3573 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3574 XEXP (src_op0, 0));
3575 SUBST (XEXP (setsrc, 0), newdest);
3576 SUBST (XEXP (setsrc, 1), newdest);
3577 subst_done = true;
3579 /* Split "((X op X) op Y) op Y)" as "T op T" where
3580 T is "X op Y". */
3581 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3583 rtx tmp = simplify_gen_binary (code, mode, p, r);
3584 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3585 SUBST (XEXP (setsrc, 0), newdest);
3586 SUBST (XEXP (setsrc, 1), newdest);
3587 subst_done = true;
3592 if (!subst_done)
3594 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3595 SUBST (*split, newdest);
3598 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3600 /* recog_for_combine might have added CLOBBERs to newi2pat.
3601 Make sure NEWPAT does not depend on the clobbered regs. */
3602 if (GET_CODE (newi2pat) == PARALLEL)
3603 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3604 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3606 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3607 if (reg_overlap_mentioned_p (reg, newpat))
3609 undo_all ();
3610 return 0;
3614 /* If the split point was a MULT and we didn't have one before,
3615 don't use one now. */
3616 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3617 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3621 /* Check for a case where we loaded from memory in a narrow mode and
3622 then sign extended it, but we need both registers. In that case,
3623 we have a PARALLEL with both loads from the same memory location.
3624 We can split this into a load from memory followed by a register-register
3625 copy. This saves at least one insn, more if register allocation can
3626 eliminate the copy.
3628 We cannot do this if the destination of the first assignment is a
3629 condition code register or cc0. We eliminate this case by making sure
3630 the SET_DEST and SET_SRC have the same mode.
3632 We cannot do this if the destination of the second assignment is
3633 a register that we have already assumed is zero-extended. Similarly
3634 for a SUBREG of such a register. */
3636 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3637 && GET_CODE (newpat) == PARALLEL
3638 && XVECLEN (newpat, 0) == 2
3639 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3640 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3641 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3642 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3643 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3644 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3645 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3646 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3647 DF_INSN_LUID (i2))
3648 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3649 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3650 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3651 (REG_P (temp)
3652 && reg_stat[REGNO (temp)].nonzero_bits != 0
3653 && GET_MODE_PRECISION (GET_MODE (temp)) < BITS_PER_WORD
3654 && GET_MODE_PRECISION (GET_MODE (temp)) < HOST_BITS_PER_INT
3655 && (reg_stat[REGNO (temp)].nonzero_bits
3656 != GET_MODE_MASK (word_mode))))
3657 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3658 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3659 (REG_P (temp)
3660 && reg_stat[REGNO (temp)].nonzero_bits != 0
3661 && GET_MODE_PRECISION (GET_MODE (temp)) < BITS_PER_WORD
3662 && GET_MODE_PRECISION (GET_MODE (temp)) < HOST_BITS_PER_INT
3663 && (reg_stat[REGNO (temp)].nonzero_bits
3664 != GET_MODE_MASK (word_mode)))))
3665 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3666 SET_SRC (XVECEXP (newpat, 0, 1)))
3667 && ! find_reg_note (i3, REG_UNUSED,
3668 SET_DEST (XVECEXP (newpat, 0, 0))))
3670 rtx ni2dest;
3672 newi2pat = XVECEXP (newpat, 0, 0);
3673 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3674 newpat = XVECEXP (newpat, 0, 1);
3675 SUBST (SET_SRC (newpat),
3676 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3677 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3679 if (i2_code_number >= 0)
3680 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3682 if (insn_code_number >= 0)
3683 swap_i2i3 = 1;
3686 /* Similarly, check for a case where we have a PARALLEL of two independent
3687 SETs but we started with three insns. In this case, we can do the sets
3688 as two separate insns. This case occurs when some SET allows two
3689 other insns to combine, but the destination of that SET is still live. */
3691 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3692 && GET_CODE (newpat) == PARALLEL
3693 && XVECLEN (newpat, 0) == 2
3694 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3695 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3696 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3697 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3698 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3699 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3700 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3701 XVECEXP (newpat, 0, 0))
3702 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3703 XVECEXP (newpat, 0, 1))
3704 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3705 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3707 rtx set0 = XVECEXP (newpat, 0, 0);
3708 rtx set1 = XVECEXP (newpat, 0, 1);
3710 /* Normally, it doesn't matter which of the two is done first,
3711 but the one that references cc0 can't be the second, and
3712 one which uses any regs/memory set in between i2 and i3 can't
3713 be first. The PARALLEL might also have been pre-existing in i3,
3714 so we need to make sure that we won't wrongly hoist a SET to i2
3715 that would conflict with a death note present in there. */
3716 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3717 && !(REG_P (SET_DEST (set1))
3718 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3719 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3720 && find_reg_note (i2, REG_DEAD,
3721 SUBREG_REG (SET_DEST (set1))))
3722 #ifdef HAVE_cc0
3723 && !reg_referenced_p (cc0_rtx, set0)
3724 #endif
3725 /* If I3 is a jump, ensure that set0 is a jump so that
3726 we do not create invalid RTL. */
3727 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3730 newi2pat = set1;
3731 newpat = set0;
3733 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3734 && !(REG_P (SET_DEST (set0))
3735 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3736 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3737 && find_reg_note (i2, REG_DEAD,
3738 SUBREG_REG (SET_DEST (set0))))
3739 #ifdef HAVE_cc0
3740 && !reg_referenced_p (cc0_rtx, set1)
3741 #endif
3742 /* If I3 is a jump, ensure that set1 is a jump so that
3743 we do not create invalid RTL. */
3744 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3747 newi2pat = set0;
3748 newpat = set1;
3750 else
3752 undo_all ();
3753 return 0;
3756 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3758 if (i2_code_number >= 0)
3760 /* recog_for_combine might have added CLOBBERs to newi2pat.
3761 Make sure NEWPAT does not depend on the clobbered regs. */
3762 if (GET_CODE (newi2pat) == PARALLEL)
3764 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3765 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3767 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3768 if (reg_overlap_mentioned_p (reg, newpat))
3770 undo_all ();
3771 return 0;
3776 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3780 /* If it still isn't recognized, fail and change things back the way they
3781 were. */
3782 if ((insn_code_number < 0
3783 /* Is the result a reasonable ASM_OPERANDS? */
3784 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3786 undo_all ();
3787 return 0;
3790 /* If we had to change another insn, make sure it is valid also. */
3791 if (undobuf.other_insn)
3793 CLEAR_HARD_REG_SET (newpat_used_regs);
3795 other_pat = PATTERN (undobuf.other_insn);
3796 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3797 &new_other_notes);
3799 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3801 undo_all ();
3802 return 0;
3806 #ifdef HAVE_cc0
3807 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3808 they are adjacent to each other or not. */
3810 rtx p = prev_nonnote_insn (i3);
3811 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3812 && sets_cc0_p (newi2pat))
3814 undo_all ();
3815 return 0;
3818 #endif
3820 /* Only allow this combination if insn_rtx_costs reports that the
3821 replacement instructions are cheaper than the originals. */
3822 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3824 undo_all ();
3825 return 0;
3828 if (MAY_HAVE_DEBUG_INSNS)
3830 struct undo *undo;
3832 for (undo = undobuf.undos; undo; undo = undo->next)
3833 if (undo->kind == UNDO_MODE)
3835 rtx reg = *undo->where.r;
3836 enum machine_mode new_mode = GET_MODE (reg);
3837 enum machine_mode old_mode = undo->old_contents.m;
3839 /* Temporarily revert mode back. */
3840 adjust_reg_mode (reg, old_mode);
3842 if (reg == i2dest && i2scratch)
3844 /* If we used i2dest as a scratch register with a
3845 different mode, substitute it for the original
3846 i2src while its original mode is temporarily
3847 restored, and then clear i2scratch so that we don't
3848 do it again later. */
3849 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3850 this_basic_block);
3851 i2scratch = false;
3852 /* Put back the new mode. */
3853 adjust_reg_mode (reg, new_mode);
3855 else
3857 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3858 rtx first, last;
3860 if (reg == i2dest)
3862 first = i2;
3863 last = last_combined_insn;
3865 else
3867 first = i3;
3868 last = undobuf.other_insn;
3869 gcc_assert (last);
3870 if (DF_INSN_LUID (last)
3871 < DF_INSN_LUID (last_combined_insn))
3872 last = last_combined_insn;
3875 /* We're dealing with a reg that changed mode but not
3876 meaning, so we want to turn it into a subreg for
3877 the new mode. However, because of REG sharing and
3878 because its mode had already changed, we have to do
3879 it in two steps. First, replace any debug uses of
3880 reg, with its original mode temporarily restored,
3881 with this copy we have created; then, replace the
3882 copy with the SUBREG of the original shared reg,
3883 once again changed to the new mode. */
3884 propagate_for_debug (first, last, reg, tempreg,
3885 this_basic_block);
3886 adjust_reg_mode (reg, new_mode);
3887 propagate_for_debug (first, last, tempreg,
3888 lowpart_subreg (old_mode, reg, new_mode),
3889 this_basic_block);
3894 /* If we will be able to accept this, we have made a
3895 change to the destination of I3. This requires us to
3896 do a few adjustments. */
3898 if (changed_i3_dest)
3900 PATTERN (i3) = newpat;
3901 adjust_for_new_dest (i3);
3904 /* We now know that we can do this combination. Merge the insns and
3905 update the status of registers and LOG_LINKS. */
3907 if (undobuf.other_insn)
3909 rtx note, next;
3911 PATTERN (undobuf.other_insn) = other_pat;
3913 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
3914 ensure that they are still valid. Then add any non-duplicate
3915 notes added by recog_for_combine. */
3916 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3918 next = XEXP (note, 1);
3920 if ((REG_NOTE_KIND (note) == REG_DEAD
3921 && !reg_referenced_p (XEXP (note, 0),
3922 PATTERN (undobuf.other_insn)))
3923 ||(REG_NOTE_KIND (note) == REG_UNUSED
3924 && !reg_set_p (XEXP (note, 0),
3925 PATTERN (undobuf.other_insn))))
3926 remove_note (undobuf.other_insn, note);
3929 distribute_notes (new_other_notes, undobuf.other_insn,
3930 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
3931 NULL_RTX);
3934 if (swap_i2i3)
3936 rtx insn;
3937 struct insn_link *link;
3938 rtx ni2dest;
3940 /* I3 now uses what used to be its destination and which is now
3941 I2's destination. This requires us to do a few adjustments. */
3942 PATTERN (i3) = newpat;
3943 adjust_for_new_dest (i3);
3945 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3946 so we still will.
3948 However, some later insn might be using I2's dest and have
3949 a LOG_LINK pointing at I3. We must remove this link.
3950 The simplest way to remove the link is to point it at I1,
3951 which we know will be a NOTE. */
3953 /* newi2pat is usually a SET here; however, recog_for_combine might
3954 have added some clobbers. */
3955 if (GET_CODE (newi2pat) == PARALLEL)
3956 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3957 else
3958 ni2dest = SET_DEST (newi2pat);
3960 for (insn = NEXT_INSN (i3);
3961 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
3962 || insn != BB_HEAD (this_basic_block->next_bb));
3963 insn = NEXT_INSN (insn))
3965 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3967 FOR_EACH_LOG_LINK (link, insn)
3968 if (link->insn == i3)
3969 link->insn = i1;
3971 break;
3977 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3978 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
3979 rtx midnotes = 0;
3980 int from_luid;
3981 /* Compute which registers we expect to eliminate. newi2pat may be setting
3982 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3983 same as i3dest, in which case newi2pat may be setting i1dest. */
3984 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3985 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
3986 || !i2dest_killed
3987 ? 0 : i2dest);
3988 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
3989 || (newi2pat && reg_set_p (i1dest, newi2pat))
3990 || !i1dest_killed
3991 ? 0 : i1dest);
3992 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
3993 || (newi2pat && reg_set_p (i0dest, newi2pat))
3994 || !i0dest_killed
3995 ? 0 : i0dest);
3997 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3998 clear them. */
3999 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4000 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4001 if (i1)
4002 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4003 if (i0)
4004 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4006 /* Ensure that we do not have something that should not be shared but
4007 occurs multiple times in the new insns. Check this by first
4008 resetting all the `used' flags and then copying anything is shared. */
4010 reset_used_flags (i3notes);
4011 reset_used_flags (i2notes);
4012 reset_used_flags (i1notes);
4013 reset_used_flags (i0notes);
4014 reset_used_flags (newpat);
4015 reset_used_flags (newi2pat);
4016 if (undobuf.other_insn)
4017 reset_used_flags (PATTERN (undobuf.other_insn));
4019 i3notes = copy_rtx_if_shared (i3notes);
4020 i2notes = copy_rtx_if_shared (i2notes);
4021 i1notes = copy_rtx_if_shared (i1notes);
4022 i0notes = copy_rtx_if_shared (i0notes);
4023 newpat = copy_rtx_if_shared (newpat);
4024 newi2pat = copy_rtx_if_shared (newi2pat);
4025 if (undobuf.other_insn)
4026 reset_used_flags (PATTERN (undobuf.other_insn));
4028 INSN_CODE (i3) = insn_code_number;
4029 PATTERN (i3) = newpat;
4031 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4033 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4035 reset_used_flags (call_usage);
4036 call_usage = copy_rtx (call_usage);
4038 if (substed_i2)
4040 /* I2SRC must still be meaningful at this point. Some splitting
4041 operations can invalidate I2SRC, but those operations do not
4042 apply to calls. */
4043 gcc_assert (i2src);
4044 replace_rtx (call_usage, i2dest, i2src);
4047 if (substed_i1)
4048 replace_rtx (call_usage, i1dest, i1src);
4049 if (substed_i0)
4050 replace_rtx (call_usage, i0dest, i0src);
4052 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4055 if (undobuf.other_insn)
4056 INSN_CODE (undobuf.other_insn) = other_code_number;
4058 /* We had one special case above where I2 had more than one set and
4059 we replaced a destination of one of those sets with the destination
4060 of I3. In that case, we have to update LOG_LINKS of insns later
4061 in this basic block. Note that this (expensive) case is rare.
4063 Also, in this case, we must pretend that all REG_NOTEs for I2
4064 actually came from I3, so that REG_UNUSED notes from I2 will be
4065 properly handled. */
4067 if (i3_subst_into_i2)
4069 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4070 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4071 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4072 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4073 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4074 && ! find_reg_note (i2, REG_UNUSED,
4075 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4076 for (temp = NEXT_INSN (i2);
4077 temp
4078 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4079 || BB_HEAD (this_basic_block) != temp);
4080 temp = NEXT_INSN (temp))
4081 if (temp != i3 && INSN_P (temp))
4082 FOR_EACH_LOG_LINK (link, temp)
4083 if (link->insn == i2)
4084 link->insn = i3;
4086 if (i3notes)
4088 rtx link = i3notes;
4089 while (XEXP (link, 1))
4090 link = XEXP (link, 1);
4091 XEXP (link, 1) = i2notes;
4093 else
4094 i3notes = i2notes;
4095 i2notes = 0;
4098 LOG_LINKS (i3) = NULL;
4099 REG_NOTES (i3) = 0;
4100 LOG_LINKS (i2) = NULL;
4101 REG_NOTES (i2) = 0;
4103 if (newi2pat)
4105 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4106 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4107 this_basic_block);
4108 INSN_CODE (i2) = i2_code_number;
4109 PATTERN (i2) = newi2pat;
4111 else
4113 if (MAY_HAVE_DEBUG_INSNS && i2src)
4114 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4115 this_basic_block);
4116 SET_INSN_DELETED (i2);
4119 if (i1)
4121 LOG_LINKS (i1) = NULL;
4122 REG_NOTES (i1) = 0;
4123 if (MAY_HAVE_DEBUG_INSNS)
4124 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4125 this_basic_block);
4126 SET_INSN_DELETED (i1);
4129 if (i0)
4131 LOG_LINKS (i0) = NULL;
4132 REG_NOTES (i0) = 0;
4133 if (MAY_HAVE_DEBUG_INSNS)
4134 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4135 this_basic_block);
4136 SET_INSN_DELETED (i0);
4139 /* Get death notes for everything that is now used in either I3 or
4140 I2 and used to die in a previous insn. If we built two new
4141 patterns, move from I1 to I2 then I2 to I3 so that we get the
4142 proper movement on registers that I2 modifies. */
4144 if (i0)
4145 from_luid = DF_INSN_LUID (i0);
4146 else if (i1)
4147 from_luid = DF_INSN_LUID (i1);
4148 else
4149 from_luid = DF_INSN_LUID (i2);
4150 if (newi2pat)
4151 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4152 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4154 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4155 if (i3notes)
4156 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4157 elim_i2, elim_i1, elim_i0);
4158 if (i2notes)
4159 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4160 elim_i2, elim_i1, elim_i0);
4161 if (i1notes)
4162 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4163 elim_i2, elim_i1, elim_i0);
4164 if (i0notes)
4165 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4166 elim_i2, elim_i1, elim_i0);
4167 if (midnotes)
4168 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4169 elim_i2, elim_i1, elim_i0);
4171 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4172 know these are REG_UNUSED and want them to go to the desired insn,
4173 so we always pass it as i3. */
4175 if (newi2pat && new_i2_notes)
4176 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4177 NULL_RTX);
4179 if (new_i3_notes)
4180 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4181 NULL_RTX);
4183 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4184 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4185 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4186 in that case, it might delete I2. Similarly for I2 and I1.
4187 Show an additional death due to the REG_DEAD note we make here. If
4188 we discard it in distribute_notes, we will decrement it again. */
4190 if (i3dest_killed)
4192 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4193 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4194 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, elim_i2,
4195 elim_i1, elim_i0);
4196 else
4197 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4198 elim_i2, elim_i1, elim_i0);
4201 if (i2dest_in_i2src)
4203 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4204 if (newi2pat && reg_set_p (i2dest, newi2pat))
4205 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4206 NULL_RTX, NULL_RTX);
4207 else
4208 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4209 NULL_RTX, NULL_RTX, NULL_RTX);
4212 if (i1dest_in_i1src)
4214 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4215 if (newi2pat && reg_set_p (i1dest, newi2pat))
4216 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4217 NULL_RTX, NULL_RTX);
4218 else
4219 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4220 NULL_RTX, NULL_RTX, NULL_RTX);
4223 if (i0dest_in_i0src)
4225 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4226 if (newi2pat && reg_set_p (i0dest, newi2pat))
4227 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4228 NULL_RTX, NULL_RTX);
4229 else
4230 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4231 NULL_RTX, NULL_RTX, NULL_RTX);
4234 distribute_links (i3links);
4235 distribute_links (i2links);
4236 distribute_links (i1links);
4237 distribute_links (i0links);
4239 if (REG_P (i2dest))
4241 struct insn_link *link;
4242 rtx i2_insn = 0, i2_val = 0, set;
4244 /* The insn that used to set this register doesn't exist, and
4245 this life of the register may not exist either. See if one of
4246 I3's links points to an insn that sets I2DEST. If it does,
4247 that is now the last known value for I2DEST. If we don't update
4248 this and I2 set the register to a value that depended on its old
4249 contents, we will get confused. If this insn is used, thing
4250 will be set correctly in combine_instructions. */
4251 FOR_EACH_LOG_LINK (link, i3)
4252 if ((set = single_set (link->insn)) != 0
4253 && rtx_equal_p (i2dest, SET_DEST (set)))
4254 i2_insn = link->insn, i2_val = SET_SRC (set);
4256 record_value_for_reg (i2dest, i2_insn, i2_val);
4258 /* If the reg formerly set in I2 died only once and that was in I3,
4259 zero its use count so it won't make `reload' do any work. */
4260 if (! added_sets_2
4261 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4262 && ! i2dest_in_i2src)
4263 INC_REG_N_SETS (REGNO (i2dest), -1);
4266 if (i1 && REG_P (i1dest))
4268 struct insn_link *link;
4269 rtx i1_insn = 0, i1_val = 0, set;
4271 FOR_EACH_LOG_LINK (link, i3)
4272 if ((set = single_set (link->insn)) != 0
4273 && rtx_equal_p (i1dest, SET_DEST (set)))
4274 i1_insn = link->insn, i1_val = SET_SRC (set);
4276 record_value_for_reg (i1dest, i1_insn, i1_val);
4278 if (! added_sets_1 && ! i1dest_in_i1src)
4279 INC_REG_N_SETS (REGNO (i1dest), -1);
4282 if (i0 && REG_P (i0dest))
4284 struct insn_link *link;
4285 rtx i0_insn = 0, i0_val = 0, set;
4287 FOR_EACH_LOG_LINK (link, i3)
4288 if ((set = single_set (link->insn)) != 0
4289 && rtx_equal_p (i0dest, SET_DEST (set)))
4290 i0_insn = link->insn, i0_val = SET_SRC (set);
4292 record_value_for_reg (i0dest, i0_insn, i0_val);
4294 if (! added_sets_0 && ! i0dest_in_i0src)
4295 INC_REG_N_SETS (REGNO (i0dest), -1);
4298 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4299 been made to this insn. The order is important, because newi2pat
4300 can affect nonzero_bits of newpat. */
4301 if (newi2pat)
4302 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4303 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4306 if (undobuf.other_insn != NULL_RTX)
4308 if (dump_file)
4310 fprintf (dump_file, "modifying other_insn ");
4311 dump_insn_slim (dump_file, undobuf.other_insn);
4313 df_insn_rescan (undobuf.other_insn);
4316 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4318 if (dump_file)
4320 fprintf (dump_file, "modifying insn i0 ");
4321 dump_insn_slim (dump_file, i0);
4323 df_insn_rescan (i0);
4326 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4328 if (dump_file)
4330 fprintf (dump_file, "modifying insn i1 ");
4331 dump_insn_slim (dump_file, i1);
4333 df_insn_rescan (i1);
4336 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4338 if (dump_file)
4340 fprintf (dump_file, "modifying insn i2 ");
4341 dump_insn_slim (dump_file, i2);
4343 df_insn_rescan (i2);
4346 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4348 if (dump_file)
4350 fprintf (dump_file, "modifying insn i3 ");
4351 dump_insn_slim (dump_file, i3);
4353 df_insn_rescan (i3);
4356 /* Set new_direct_jump_p if a new return or simple jump instruction
4357 has been created. Adjust the CFG accordingly. */
4358 if (returnjump_p (i3) || any_uncondjump_p (i3))
4360 *new_direct_jump_p = 1;
4361 mark_jump_label (PATTERN (i3), i3, 0);
4362 update_cfg_for_uncondjump (i3);
4365 if (undobuf.other_insn != NULL_RTX
4366 && (returnjump_p (undobuf.other_insn)
4367 || any_uncondjump_p (undobuf.other_insn)))
4369 *new_direct_jump_p = 1;
4370 update_cfg_for_uncondjump (undobuf.other_insn);
4373 /* A noop might also need cleaning up of CFG, if it comes from the
4374 simplification of a jump. */
4375 if (JUMP_P (i3)
4376 && GET_CODE (newpat) == SET
4377 && SET_SRC (newpat) == pc_rtx
4378 && SET_DEST (newpat) == pc_rtx)
4380 *new_direct_jump_p = 1;
4381 update_cfg_for_uncondjump (i3);
4384 if (undobuf.other_insn != NULL_RTX
4385 && JUMP_P (undobuf.other_insn)
4386 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4387 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4388 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4390 *new_direct_jump_p = 1;
4391 update_cfg_for_uncondjump (undobuf.other_insn);
4394 combine_successes++;
4395 undo_commit ();
4397 if (added_links_insn
4398 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4399 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4400 return added_links_insn;
4401 else
4402 return newi2pat ? i2 : i3;
4405 /* Undo all the modifications recorded in undobuf. */
4407 static void
4408 undo_all (void)
4410 struct undo *undo, *next;
4412 for (undo = undobuf.undos; undo; undo = next)
4414 next = undo->next;
4415 switch (undo->kind)
4417 case UNDO_RTX:
4418 *undo->where.r = undo->old_contents.r;
4419 break;
4420 case UNDO_INT:
4421 *undo->where.i = undo->old_contents.i;
4422 break;
4423 case UNDO_MODE:
4424 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4425 break;
4426 case UNDO_LINKS:
4427 *undo->where.l = undo->old_contents.l;
4428 break;
4429 default:
4430 gcc_unreachable ();
4433 undo->next = undobuf.frees;
4434 undobuf.frees = undo;
4437 undobuf.undos = 0;
4440 /* We've committed to accepting the changes we made. Move all
4441 of the undos to the free list. */
4443 static void
4444 undo_commit (void)
4446 struct undo *undo, *next;
4448 for (undo = undobuf.undos; undo; undo = next)
4450 next = undo->next;
4451 undo->next = undobuf.frees;
4452 undobuf.frees = undo;
4454 undobuf.undos = 0;
4457 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4458 where we have an arithmetic expression and return that point. LOC will
4459 be inside INSN.
4461 try_combine will call this function to see if an insn can be split into
4462 two insns. */
4464 static rtx *
4465 find_split_point (rtx *loc, rtx insn, bool set_src)
4467 rtx x = *loc;
4468 enum rtx_code code = GET_CODE (x);
4469 rtx *split;
4470 unsigned HOST_WIDE_INT len = 0;
4471 HOST_WIDE_INT pos = 0;
4472 int unsignedp = 0;
4473 rtx inner = NULL_RTX;
4475 /* First special-case some codes. */
4476 switch (code)
4478 case SUBREG:
4479 #ifdef INSN_SCHEDULING
4480 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4481 point. */
4482 if (MEM_P (SUBREG_REG (x)))
4483 return loc;
4484 #endif
4485 return find_split_point (&SUBREG_REG (x), insn, false);
4487 case MEM:
4488 #ifdef HAVE_lo_sum
4489 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4490 using LO_SUM and HIGH. */
4491 if (GET_CODE (XEXP (x, 0)) == CONST
4492 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4494 enum machine_mode address_mode = get_address_mode (x);
4496 SUBST (XEXP (x, 0),
4497 gen_rtx_LO_SUM (address_mode,
4498 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4499 XEXP (x, 0)));
4500 return &XEXP (XEXP (x, 0), 0);
4502 #endif
4504 /* If we have a PLUS whose second operand is a constant and the
4505 address is not valid, perhaps will can split it up using
4506 the machine-specific way to split large constants. We use
4507 the first pseudo-reg (one of the virtual regs) as a placeholder;
4508 it will not remain in the result. */
4509 if (GET_CODE (XEXP (x, 0)) == PLUS
4510 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4511 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4512 MEM_ADDR_SPACE (x)))
4514 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4515 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4516 XEXP (x, 0)),
4517 subst_insn);
4519 /* This should have produced two insns, each of which sets our
4520 placeholder. If the source of the second is a valid address,
4521 we can make put both sources together and make a split point
4522 in the middle. */
4524 if (seq
4525 && NEXT_INSN (seq) != NULL_RTX
4526 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4527 && NONJUMP_INSN_P (seq)
4528 && GET_CODE (PATTERN (seq)) == SET
4529 && SET_DEST (PATTERN (seq)) == reg
4530 && ! reg_mentioned_p (reg,
4531 SET_SRC (PATTERN (seq)))
4532 && NONJUMP_INSN_P (NEXT_INSN (seq))
4533 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4534 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4535 && memory_address_addr_space_p
4536 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4537 MEM_ADDR_SPACE (x)))
4539 rtx src1 = SET_SRC (PATTERN (seq));
4540 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4542 /* Replace the placeholder in SRC2 with SRC1. If we can
4543 find where in SRC2 it was placed, that can become our
4544 split point and we can replace this address with SRC2.
4545 Just try two obvious places. */
4547 src2 = replace_rtx (src2, reg, src1);
4548 split = 0;
4549 if (XEXP (src2, 0) == src1)
4550 split = &XEXP (src2, 0);
4551 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4552 && XEXP (XEXP (src2, 0), 0) == src1)
4553 split = &XEXP (XEXP (src2, 0), 0);
4555 if (split)
4557 SUBST (XEXP (x, 0), src2);
4558 return split;
4562 /* If that didn't work, perhaps the first operand is complex and
4563 needs to be computed separately, so make a split point there.
4564 This will occur on machines that just support REG + CONST
4565 and have a constant moved through some previous computation. */
4567 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4568 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4569 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4570 return &XEXP (XEXP (x, 0), 0);
4573 /* If we have a PLUS whose first operand is complex, try computing it
4574 separately by making a split there. */
4575 if (GET_CODE (XEXP (x, 0)) == PLUS
4576 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4577 MEM_ADDR_SPACE (x))
4578 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4579 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4580 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4581 return &XEXP (XEXP (x, 0), 0);
4582 break;
4584 case SET:
4585 #ifdef HAVE_cc0
4586 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4587 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4588 we need to put the operand into a register. So split at that
4589 point. */
4591 if (SET_DEST (x) == cc0_rtx
4592 && GET_CODE (SET_SRC (x)) != COMPARE
4593 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4594 && !OBJECT_P (SET_SRC (x))
4595 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4596 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4597 return &SET_SRC (x);
4598 #endif
4600 /* See if we can split SET_SRC as it stands. */
4601 split = find_split_point (&SET_SRC (x), insn, true);
4602 if (split && split != &SET_SRC (x))
4603 return split;
4605 /* See if we can split SET_DEST as it stands. */
4606 split = find_split_point (&SET_DEST (x), insn, false);
4607 if (split && split != &SET_DEST (x))
4608 return split;
4610 /* See if this is a bitfield assignment with everything constant. If
4611 so, this is an IOR of an AND, so split it into that. */
4612 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4613 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4614 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4615 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4616 && CONST_INT_P (SET_SRC (x))
4617 && ((INTVAL (XEXP (SET_DEST (x), 1))
4618 + INTVAL (XEXP (SET_DEST (x), 2)))
4619 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4620 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4622 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4623 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4624 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4625 rtx dest = XEXP (SET_DEST (x), 0);
4626 enum machine_mode mode = GET_MODE (dest);
4627 unsigned HOST_WIDE_INT mask
4628 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4629 rtx or_mask;
4631 if (BITS_BIG_ENDIAN)
4632 pos = GET_MODE_PRECISION (mode) - len - pos;
4634 or_mask = gen_int_mode (src << pos, mode);
4635 if (src == mask)
4636 SUBST (SET_SRC (x),
4637 simplify_gen_binary (IOR, mode, dest, or_mask));
4638 else
4640 rtx negmask = gen_int_mode (~(mask << pos), mode);
4641 SUBST (SET_SRC (x),
4642 simplify_gen_binary (IOR, mode,
4643 simplify_gen_binary (AND, mode,
4644 dest, negmask),
4645 or_mask));
4648 SUBST (SET_DEST (x), dest);
4650 split = find_split_point (&SET_SRC (x), insn, true);
4651 if (split && split != &SET_SRC (x))
4652 return split;
4655 /* Otherwise, see if this is an operation that we can split into two.
4656 If so, try to split that. */
4657 code = GET_CODE (SET_SRC (x));
4659 switch (code)
4661 case AND:
4662 /* If we are AND'ing with a large constant that is only a single
4663 bit and the result is only being used in a context where we
4664 need to know if it is zero or nonzero, replace it with a bit
4665 extraction. This will avoid the large constant, which might
4666 have taken more than one insn to make. If the constant were
4667 not a valid argument to the AND but took only one insn to make,
4668 this is no worse, but if it took more than one insn, it will
4669 be better. */
4671 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4672 && REG_P (XEXP (SET_SRC (x), 0))
4673 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4674 && REG_P (SET_DEST (x))
4675 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4676 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4677 && XEXP (*split, 0) == SET_DEST (x)
4678 && XEXP (*split, 1) == const0_rtx)
4680 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4681 XEXP (SET_SRC (x), 0),
4682 pos, NULL_RTX, 1, 1, 0, 0);
4683 if (extraction != 0)
4685 SUBST (SET_SRC (x), extraction);
4686 return find_split_point (loc, insn, false);
4689 break;
4691 case NE:
4692 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4693 is known to be on, this can be converted into a NEG of a shift. */
4694 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4695 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4696 && 1 <= (pos = exact_log2
4697 (nonzero_bits (XEXP (SET_SRC (x), 0),
4698 GET_MODE (XEXP (SET_SRC (x), 0))))))
4700 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4702 SUBST (SET_SRC (x),
4703 gen_rtx_NEG (mode,
4704 gen_rtx_LSHIFTRT (mode,
4705 XEXP (SET_SRC (x), 0),
4706 GEN_INT (pos))));
4708 split = find_split_point (&SET_SRC (x), insn, true);
4709 if (split && split != &SET_SRC (x))
4710 return split;
4712 break;
4714 case SIGN_EXTEND:
4715 inner = XEXP (SET_SRC (x), 0);
4717 /* We can't optimize if either mode is a partial integer
4718 mode as we don't know how many bits are significant
4719 in those modes. */
4720 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4721 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4722 break;
4724 pos = 0;
4725 len = GET_MODE_PRECISION (GET_MODE (inner));
4726 unsignedp = 0;
4727 break;
4729 case SIGN_EXTRACT:
4730 case ZERO_EXTRACT:
4731 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4732 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4734 inner = XEXP (SET_SRC (x), 0);
4735 len = INTVAL (XEXP (SET_SRC (x), 1));
4736 pos = INTVAL (XEXP (SET_SRC (x), 2));
4738 if (BITS_BIG_ENDIAN)
4739 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4740 unsignedp = (code == ZERO_EXTRACT);
4742 break;
4744 default:
4745 break;
4748 if (len && pos >= 0
4749 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4751 enum machine_mode mode = GET_MODE (SET_SRC (x));
4753 /* For unsigned, we have a choice of a shift followed by an
4754 AND or two shifts. Use two shifts for field sizes where the
4755 constant might be too large. We assume here that we can
4756 always at least get 8-bit constants in an AND insn, which is
4757 true for every current RISC. */
4759 if (unsignedp && len <= 8)
4761 unsigned HOST_WIDE_INT mask
4762 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4763 SUBST (SET_SRC (x),
4764 gen_rtx_AND (mode,
4765 gen_rtx_LSHIFTRT
4766 (mode, gen_lowpart (mode, inner),
4767 GEN_INT (pos)),
4768 gen_int_mode (mask, mode)));
4770 split = find_split_point (&SET_SRC (x), insn, true);
4771 if (split && split != &SET_SRC (x))
4772 return split;
4774 else
4776 SUBST (SET_SRC (x),
4777 gen_rtx_fmt_ee
4778 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4779 gen_rtx_ASHIFT (mode,
4780 gen_lowpart (mode, inner),
4781 GEN_INT (GET_MODE_PRECISION (mode)
4782 - len - pos)),
4783 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4785 split = find_split_point (&SET_SRC (x), insn, true);
4786 if (split && split != &SET_SRC (x))
4787 return split;
4791 /* See if this is a simple operation with a constant as the second
4792 operand. It might be that this constant is out of range and hence
4793 could be used as a split point. */
4794 if (BINARY_P (SET_SRC (x))
4795 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4796 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4797 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4798 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4799 return &XEXP (SET_SRC (x), 1);
4801 /* Finally, see if this is a simple operation with its first operand
4802 not in a register. The operation might require this operand in a
4803 register, so return it as a split point. We can always do this
4804 because if the first operand were another operation, we would have
4805 already found it as a split point. */
4806 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4807 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4808 return &XEXP (SET_SRC (x), 0);
4810 return 0;
4812 case AND:
4813 case IOR:
4814 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4815 it is better to write this as (not (ior A B)) so we can split it.
4816 Similarly for IOR. */
4817 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4819 SUBST (*loc,
4820 gen_rtx_NOT (GET_MODE (x),
4821 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4822 GET_MODE (x),
4823 XEXP (XEXP (x, 0), 0),
4824 XEXP (XEXP (x, 1), 0))));
4825 return find_split_point (loc, insn, set_src);
4828 /* Many RISC machines have a large set of logical insns. If the
4829 second operand is a NOT, put it first so we will try to split the
4830 other operand first. */
4831 if (GET_CODE (XEXP (x, 1)) == NOT)
4833 rtx tem = XEXP (x, 0);
4834 SUBST (XEXP (x, 0), XEXP (x, 1));
4835 SUBST (XEXP (x, 1), tem);
4837 break;
4839 case PLUS:
4840 case MINUS:
4841 /* Canonicalization can produce (minus A (mult B C)), where C is a
4842 constant. It may be better to try splitting (plus (mult B -C) A)
4843 instead if this isn't a multiply by a power of two. */
4844 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4845 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4846 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4848 enum machine_mode mode = GET_MODE (x);
4849 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4850 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4851 SUBST (*loc, gen_rtx_PLUS (mode,
4852 gen_rtx_MULT (mode,
4853 XEXP (XEXP (x, 1), 0),
4854 gen_int_mode (other_int,
4855 mode)),
4856 XEXP (x, 0)));
4857 return find_split_point (loc, insn, set_src);
4860 /* Split at a multiply-accumulate instruction. However if this is
4861 the SET_SRC, we likely do not have such an instruction and it's
4862 worthless to try this split. */
4863 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4864 return loc;
4866 default:
4867 break;
4870 /* Otherwise, select our actions depending on our rtx class. */
4871 switch (GET_RTX_CLASS (code))
4873 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4874 case RTX_TERNARY:
4875 split = find_split_point (&XEXP (x, 2), insn, false);
4876 if (split)
4877 return split;
4878 /* ... fall through ... */
4879 case RTX_BIN_ARITH:
4880 case RTX_COMM_ARITH:
4881 case RTX_COMPARE:
4882 case RTX_COMM_COMPARE:
4883 split = find_split_point (&XEXP (x, 1), insn, false);
4884 if (split)
4885 return split;
4886 /* ... fall through ... */
4887 case RTX_UNARY:
4888 /* Some machines have (and (shift ...) ...) insns. If X is not
4889 an AND, but XEXP (X, 0) is, use it as our split point. */
4890 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4891 return &XEXP (x, 0);
4893 split = find_split_point (&XEXP (x, 0), insn, false);
4894 if (split)
4895 return split;
4896 return loc;
4898 default:
4899 /* Otherwise, we don't have a split point. */
4900 return 0;
4904 /* Throughout X, replace FROM with TO, and return the result.
4905 The result is TO if X is FROM;
4906 otherwise the result is X, but its contents may have been modified.
4907 If they were modified, a record was made in undobuf so that
4908 undo_all will (among other things) return X to its original state.
4910 If the number of changes necessary is too much to record to undo,
4911 the excess changes are not made, so the result is invalid.
4912 The changes already made can still be undone.
4913 undobuf.num_undo is incremented for such changes, so by testing that
4914 the caller can tell whether the result is valid.
4916 `n_occurrences' is incremented each time FROM is replaced.
4918 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4920 IN_COND is nonzero if we are at the top level of a condition.
4922 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4923 by copying if `n_occurrences' is nonzero. */
4925 static rtx
4926 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4928 enum rtx_code code = GET_CODE (x);
4929 enum machine_mode op0_mode = VOIDmode;
4930 const char *fmt;
4931 int len, i;
4932 rtx new_rtx;
4934 /* Two expressions are equal if they are identical copies of a shared
4935 RTX or if they are both registers with the same register number
4936 and mode. */
4938 #define COMBINE_RTX_EQUAL_P(X,Y) \
4939 ((X) == (Y) \
4940 || (REG_P (X) && REG_P (Y) \
4941 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4943 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4945 n_occurrences++;
4946 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4949 /* If X and FROM are the same register but different modes, they
4950 will not have been seen as equal above. However, the log links code
4951 will make a LOG_LINKS entry for that case. If we do nothing, we
4952 will try to rerecognize our original insn and, when it succeeds,
4953 we will delete the feeding insn, which is incorrect.
4955 So force this insn not to match in this (rare) case. */
4956 if (! in_dest && code == REG && REG_P (from)
4957 && reg_overlap_mentioned_p (x, from))
4958 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4960 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4961 of which may contain things that can be combined. */
4962 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4963 return x;
4965 /* It is possible to have a subexpression appear twice in the insn.
4966 Suppose that FROM is a register that appears within TO.
4967 Then, after that subexpression has been scanned once by `subst',
4968 the second time it is scanned, TO may be found. If we were
4969 to scan TO here, we would find FROM within it and create a
4970 self-referent rtl structure which is completely wrong. */
4971 if (COMBINE_RTX_EQUAL_P (x, to))
4972 return to;
4974 /* Parallel asm_operands need special attention because all of the
4975 inputs are shared across the arms. Furthermore, unsharing the
4976 rtl results in recognition failures. Failure to handle this case
4977 specially can result in circular rtl.
4979 Solve this by doing a normal pass across the first entry of the
4980 parallel, and only processing the SET_DESTs of the subsequent
4981 entries. Ug. */
4983 if (code == PARALLEL
4984 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4985 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4987 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
4989 /* If this substitution failed, this whole thing fails. */
4990 if (GET_CODE (new_rtx) == CLOBBER
4991 && XEXP (new_rtx, 0) == const0_rtx)
4992 return new_rtx;
4994 SUBST (XVECEXP (x, 0, 0), new_rtx);
4996 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4998 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5000 if (!REG_P (dest)
5001 && GET_CODE (dest) != CC0
5002 && GET_CODE (dest) != PC)
5004 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5006 /* If this substitution failed, this whole thing fails. */
5007 if (GET_CODE (new_rtx) == CLOBBER
5008 && XEXP (new_rtx, 0) == const0_rtx)
5009 return new_rtx;
5011 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5015 else
5017 len = GET_RTX_LENGTH (code);
5018 fmt = GET_RTX_FORMAT (code);
5020 /* We don't need to process a SET_DEST that is a register, CC0,
5021 or PC, so set up to skip this common case. All other cases
5022 where we want to suppress replacing something inside a
5023 SET_SRC are handled via the IN_DEST operand. */
5024 if (code == SET
5025 && (REG_P (SET_DEST (x))
5026 || GET_CODE (SET_DEST (x)) == CC0
5027 || GET_CODE (SET_DEST (x)) == PC))
5028 fmt = "ie";
5030 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5031 constant. */
5032 if (fmt[0] == 'e')
5033 op0_mode = GET_MODE (XEXP (x, 0));
5035 for (i = 0; i < len; i++)
5037 if (fmt[i] == 'E')
5039 int j;
5040 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5042 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5044 new_rtx = (unique_copy && n_occurrences
5045 ? copy_rtx (to) : to);
5046 n_occurrences++;
5048 else
5050 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5051 unique_copy);
5053 /* If this substitution failed, this whole thing
5054 fails. */
5055 if (GET_CODE (new_rtx) == CLOBBER
5056 && XEXP (new_rtx, 0) == const0_rtx)
5057 return new_rtx;
5060 SUBST (XVECEXP (x, i, j), new_rtx);
5063 else if (fmt[i] == 'e')
5065 /* If this is a register being set, ignore it. */
5066 new_rtx = XEXP (x, i);
5067 if (in_dest
5068 && i == 0
5069 && (((code == SUBREG || code == ZERO_EXTRACT)
5070 && REG_P (new_rtx))
5071 || code == STRICT_LOW_PART))
5074 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5076 /* In general, don't install a subreg involving two
5077 modes not tieable. It can worsen register
5078 allocation, and can even make invalid reload
5079 insns, since the reg inside may need to be copied
5080 from in the outside mode, and that may be invalid
5081 if it is an fp reg copied in integer mode.
5083 We allow two exceptions to this: It is valid if
5084 it is inside another SUBREG and the mode of that
5085 SUBREG and the mode of the inside of TO is
5086 tieable and it is valid if X is a SET that copies
5087 FROM to CC0. */
5089 if (GET_CODE (to) == SUBREG
5090 && ! MODES_TIEABLE_P (GET_MODE (to),
5091 GET_MODE (SUBREG_REG (to)))
5092 && ! (code == SUBREG
5093 && MODES_TIEABLE_P (GET_MODE (x),
5094 GET_MODE (SUBREG_REG (to))))
5095 #ifdef HAVE_cc0
5096 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5097 #endif
5099 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5101 #ifdef CANNOT_CHANGE_MODE_CLASS
5102 if (code == SUBREG
5103 && REG_P (to)
5104 && REGNO (to) < FIRST_PSEUDO_REGISTER
5105 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5106 GET_MODE (to),
5107 GET_MODE (x)))
5108 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5109 #endif
5111 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5112 n_occurrences++;
5114 else
5115 /* If we are in a SET_DEST, suppress most cases unless we
5116 have gone inside a MEM, in which case we want to
5117 simplify the address. We assume here that things that
5118 are actually part of the destination have their inner
5119 parts in the first expression. This is true for SUBREG,
5120 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5121 things aside from REG and MEM that should appear in a
5122 SET_DEST. */
5123 new_rtx = subst (XEXP (x, i), from, to,
5124 (((in_dest
5125 && (code == SUBREG || code == STRICT_LOW_PART
5126 || code == ZERO_EXTRACT))
5127 || code == SET)
5128 && i == 0),
5129 code == IF_THEN_ELSE && i == 0,
5130 unique_copy);
5132 /* If we found that we will have to reject this combination,
5133 indicate that by returning the CLOBBER ourselves, rather than
5134 an expression containing it. This will speed things up as
5135 well as prevent accidents where two CLOBBERs are considered
5136 to be equal, thus producing an incorrect simplification. */
5138 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5139 return new_rtx;
5141 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5143 enum machine_mode mode = GET_MODE (x);
5145 x = simplify_subreg (GET_MODE (x), new_rtx,
5146 GET_MODE (SUBREG_REG (x)),
5147 SUBREG_BYTE (x));
5148 if (! x)
5149 x = gen_rtx_CLOBBER (mode, const0_rtx);
5151 else if (CONST_INT_P (new_rtx)
5152 && GET_CODE (x) == ZERO_EXTEND)
5154 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5155 new_rtx, GET_MODE (XEXP (x, 0)));
5156 gcc_assert (x);
5158 else
5159 SUBST (XEXP (x, i), new_rtx);
5164 /* Check if we are loading something from the constant pool via float
5165 extension; in this case we would undo compress_float_constant
5166 optimization and degenerate constant load to an immediate value. */
5167 if (GET_CODE (x) == FLOAT_EXTEND
5168 && MEM_P (XEXP (x, 0))
5169 && MEM_READONLY_P (XEXP (x, 0)))
5171 rtx tmp = avoid_constant_pool_reference (x);
5172 if (x != tmp)
5173 return x;
5176 /* Try to simplify X. If the simplification changed the code, it is likely
5177 that further simplification will help, so loop, but limit the number
5178 of repetitions that will be performed. */
5180 for (i = 0; i < 4; i++)
5182 /* If X is sufficiently simple, don't bother trying to do anything
5183 with it. */
5184 if (code != CONST_INT && code != REG && code != CLOBBER)
5185 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5187 if (GET_CODE (x) == code)
5188 break;
5190 code = GET_CODE (x);
5192 /* We no longer know the original mode of operand 0 since we
5193 have changed the form of X) */
5194 op0_mode = VOIDmode;
5197 return x;
5200 /* Simplify X, a piece of RTL. We just operate on the expression at the
5201 outer level; call `subst' to simplify recursively. Return the new
5202 expression.
5204 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5205 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5206 of a condition. */
5208 static rtx
5209 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5210 int in_cond)
5212 enum rtx_code code = GET_CODE (x);
5213 enum machine_mode mode = GET_MODE (x);
5214 rtx temp;
5215 int i;
5217 /* If this is a commutative operation, put a constant last and a complex
5218 expression first. We don't need to do this for comparisons here. */
5219 if (COMMUTATIVE_ARITH_P (x)
5220 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5222 temp = XEXP (x, 0);
5223 SUBST (XEXP (x, 0), XEXP (x, 1));
5224 SUBST (XEXP (x, 1), temp);
5227 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5228 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5229 things. Check for cases where both arms are testing the same
5230 condition.
5232 Don't do anything if all operands are very simple. */
5234 if ((BINARY_P (x)
5235 && ((!OBJECT_P (XEXP (x, 0))
5236 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5237 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5238 || (!OBJECT_P (XEXP (x, 1))
5239 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5240 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5241 || (UNARY_P (x)
5242 && (!OBJECT_P (XEXP (x, 0))
5243 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5244 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5246 rtx cond, true_rtx, false_rtx;
5248 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5249 if (cond != 0
5250 /* If everything is a comparison, what we have is highly unlikely
5251 to be simpler, so don't use it. */
5252 && ! (COMPARISON_P (x)
5253 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5255 rtx cop1 = const0_rtx;
5256 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5258 if (cond_code == NE && COMPARISON_P (cond))
5259 return x;
5261 /* Simplify the alternative arms; this may collapse the true and
5262 false arms to store-flag values. Be careful to use copy_rtx
5263 here since true_rtx or false_rtx might share RTL with x as a
5264 result of the if_then_else_cond call above. */
5265 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5266 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5268 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5269 is unlikely to be simpler. */
5270 if (general_operand (true_rtx, VOIDmode)
5271 && general_operand (false_rtx, VOIDmode))
5273 enum rtx_code reversed;
5275 /* Restarting if we generate a store-flag expression will cause
5276 us to loop. Just drop through in this case. */
5278 /* If the result values are STORE_FLAG_VALUE and zero, we can
5279 just make the comparison operation. */
5280 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5281 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5282 cond, cop1);
5283 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5284 && ((reversed = reversed_comparison_code_parts
5285 (cond_code, cond, cop1, NULL))
5286 != UNKNOWN))
5287 x = simplify_gen_relational (reversed, mode, VOIDmode,
5288 cond, cop1);
5290 /* Likewise, we can make the negate of a comparison operation
5291 if the result values are - STORE_FLAG_VALUE and zero. */
5292 else if (CONST_INT_P (true_rtx)
5293 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5294 && false_rtx == const0_rtx)
5295 x = simplify_gen_unary (NEG, mode,
5296 simplify_gen_relational (cond_code,
5297 mode, VOIDmode,
5298 cond, cop1),
5299 mode);
5300 else if (CONST_INT_P (false_rtx)
5301 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5302 && true_rtx == const0_rtx
5303 && ((reversed = reversed_comparison_code_parts
5304 (cond_code, cond, cop1, NULL))
5305 != UNKNOWN))
5306 x = simplify_gen_unary (NEG, mode,
5307 simplify_gen_relational (reversed,
5308 mode, VOIDmode,
5309 cond, cop1),
5310 mode);
5311 else
5312 return gen_rtx_IF_THEN_ELSE (mode,
5313 simplify_gen_relational (cond_code,
5314 mode,
5315 VOIDmode,
5316 cond,
5317 cop1),
5318 true_rtx, false_rtx);
5320 code = GET_CODE (x);
5321 op0_mode = VOIDmode;
5326 /* Try to fold this expression in case we have constants that weren't
5327 present before. */
5328 temp = 0;
5329 switch (GET_RTX_CLASS (code))
5331 case RTX_UNARY:
5332 if (op0_mode == VOIDmode)
5333 op0_mode = GET_MODE (XEXP (x, 0));
5334 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5335 break;
5336 case RTX_COMPARE:
5337 case RTX_COMM_COMPARE:
5339 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5340 if (cmp_mode == VOIDmode)
5342 cmp_mode = GET_MODE (XEXP (x, 1));
5343 if (cmp_mode == VOIDmode)
5344 cmp_mode = op0_mode;
5346 temp = simplify_relational_operation (code, mode, cmp_mode,
5347 XEXP (x, 0), XEXP (x, 1));
5349 break;
5350 case RTX_COMM_ARITH:
5351 case RTX_BIN_ARITH:
5352 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5353 break;
5354 case RTX_BITFIELD_OPS:
5355 case RTX_TERNARY:
5356 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5357 XEXP (x, 1), XEXP (x, 2));
5358 break;
5359 default:
5360 break;
5363 if (temp)
5365 x = temp;
5366 code = GET_CODE (temp);
5367 op0_mode = VOIDmode;
5368 mode = GET_MODE (temp);
5371 /* First see if we can apply the inverse distributive law. */
5372 if (code == PLUS || code == MINUS
5373 || code == AND || code == IOR || code == XOR)
5375 x = apply_distributive_law (x);
5376 code = GET_CODE (x);
5377 op0_mode = VOIDmode;
5380 /* If CODE is an associative operation not otherwise handled, see if we
5381 can associate some operands. This can win if they are constants or
5382 if they are logically related (i.e. (a & b) & a). */
5383 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5384 || code == AND || code == IOR || code == XOR
5385 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5386 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5387 || (flag_associative_math && FLOAT_MODE_P (mode))))
5389 if (GET_CODE (XEXP (x, 0)) == code)
5391 rtx other = XEXP (XEXP (x, 0), 0);
5392 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5393 rtx inner_op1 = XEXP (x, 1);
5394 rtx inner;
5396 /* Make sure we pass the constant operand if any as the second
5397 one if this is a commutative operation. */
5398 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5400 rtx tem = inner_op0;
5401 inner_op0 = inner_op1;
5402 inner_op1 = tem;
5404 inner = simplify_binary_operation (code == MINUS ? PLUS
5405 : code == DIV ? MULT
5406 : code,
5407 mode, inner_op0, inner_op1);
5409 /* For commutative operations, try the other pair if that one
5410 didn't simplify. */
5411 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5413 other = XEXP (XEXP (x, 0), 1);
5414 inner = simplify_binary_operation (code, mode,
5415 XEXP (XEXP (x, 0), 0),
5416 XEXP (x, 1));
5419 if (inner)
5420 return simplify_gen_binary (code, mode, other, inner);
5424 /* A little bit of algebraic simplification here. */
5425 switch (code)
5427 case MEM:
5428 /* Ensure that our address has any ASHIFTs converted to MULT in case
5429 address-recognizing predicates are called later. */
5430 temp = make_compound_operation (XEXP (x, 0), MEM);
5431 SUBST (XEXP (x, 0), temp);
5432 break;
5434 case SUBREG:
5435 if (op0_mode == VOIDmode)
5436 op0_mode = GET_MODE (SUBREG_REG (x));
5438 /* See if this can be moved to simplify_subreg. */
5439 if (CONSTANT_P (SUBREG_REG (x))
5440 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5441 /* Don't call gen_lowpart if the inner mode
5442 is VOIDmode and we cannot simplify it, as SUBREG without
5443 inner mode is invalid. */
5444 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5445 || gen_lowpart_common (mode, SUBREG_REG (x))))
5446 return gen_lowpart (mode, SUBREG_REG (x));
5448 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5449 break;
5451 rtx temp;
5452 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5453 SUBREG_BYTE (x));
5454 if (temp)
5455 return temp;
5457 /* If op is known to have all lower bits zero, the result is zero. */
5458 if (!in_dest
5459 && SCALAR_INT_MODE_P (mode)
5460 && SCALAR_INT_MODE_P (op0_mode)
5461 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5462 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5463 && HWI_COMPUTABLE_MODE_P (op0_mode)
5464 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5465 & GET_MODE_MASK (mode)) == 0)
5466 return CONST0_RTX (mode);
5469 /* Don't change the mode of the MEM if that would change the meaning
5470 of the address. */
5471 if (MEM_P (SUBREG_REG (x))
5472 && (MEM_VOLATILE_P (SUBREG_REG (x))
5473 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5474 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5475 return gen_rtx_CLOBBER (mode, const0_rtx);
5477 /* Note that we cannot do any narrowing for non-constants since
5478 we might have been counting on using the fact that some bits were
5479 zero. We now do this in the SET. */
5481 break;
5483 case NEG:
5484 temp = expand_compound_operation (XEXP (x, 0));
5486 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5487 replaced by (lshiftrt X C). This will convert
5488 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5490 if (GET_CODE (temp) == ASHIFTRT
5491 && CONST_INT_P (XEXP (temp, 1))
5492 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5493 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5494 INTVAL (XEXP (temp, 1)));
5496 /* If X has only a single bit that might be nonzero, say, bit I, convert
5497 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5498 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5499 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5500 or a SUBREG of one since we'd be making the expression more
5501 complex if it was just a register. */
5503 if (!REG_P (temp)
5504 && ! (GET_CODE (temp) == SUBREG
5505 && REG_P (SUBREG_REG (temp)))
5506 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5508 rtx temp1 = simplify_shift_const
5509 (NULL_RTX, ASHIFTRT, mode,
5510 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5511 GET_MODE_PRECISION (mode) - 1 - i),
5512 GET_MODE_PRECISION (mode) - 1 - i);
5514 /* If all we did was surround TEMP with the two shifts, we
5515 haven't improved anything, so don't use it. Otherwise,
5516 we are better off with TEMP1. */
5517 if (GET_CODE (temp1) != ASHIFTRT
5518 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5519 || XEXP (XEXP (temp1, 0), 0) != temp)
5520 return temp1;
5522 break;
5524 case TRUNCATE:
5525 /* We can't handle truncation to a partial integer mode here
5526 because we don't know the real bitsize of the partial
5527 integer mode. */
5528 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5529 break;
5531 if (HWI_COMPUTABLE_MODE_P (mode))
5532 SUBST (XEXP (x, 0),
5533 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5534 GET_MODE_MASK (mode), 0));
5536 /* We can truncate a constant value and return it. */
5537 if (CONST_INT_P (XEXP (x, 0)))
5538 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5540 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5541 whose value is a comparison can be replaced with a subreg if
5542 STORE_FLAG_VALUE permits. */
5543 if (HWI_COMPUTABLE_MODE_P (mode)
5544 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5545 && (temp = get_last_value (XEXP (x, 0)))
5546 && COMPARISON_P (temp))
5547 return gen_lowpart (mode, XEXP (x, 0));
5548 break;
5550 case CONST:
5551 /* (const (const X)) can become (const X). Do it this way rather than
5552 returning the inner CONST since CONST can be shared with a
5553 REG_EQUAL note. */
5554 if (GET_CODE (XEXP (x, 0)) == CONST)
5555 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5556 break;
5558 #ifdef HAVE_lo_sum
5559 case LO_SUM:
5560 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5561 can add in an offset. find_split_point will split this address up
5562 again if it doesn't match. */
5563 if (GET_CODE (XEXP (x, 0)) == HIGH
5564 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5565 return XEXP (x, 1);
5566 break;
5567 #endif
5569 case PLUS:
5570 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5571 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5572 bit-field and can be replaced by either a sign_extend or a
5573 sign_extract. The `and' may be a zero_extend and the two
5574 <c>, -<c> constants may be reversed. */
5575 if (GET_CODE (XEXP (x, 0)) == XOR
5576 && CONST_INT_P (XEXP (x, 1))
5577 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5578 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5579 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5580 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5581 && HWI_COMPUTABLE_MODE_P (mode)
5582 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5583 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5584 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5585 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5586 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5587 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5588 == (unsigned int) i + 1))))
5589 return simplify_shift_const
5590 (NULL_RTX, ASHIFTRT, mode,
5591 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5592 XEXP (XEXP (XEXP (x, 0), 0), 0),
5593 GET_MODE_PRECISION (mode) - (i + 1)),
5594 GET_MODE_PRECISION (mode) - (i + 1));
5596 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5597 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5598 the bitsize of the mode - 1. This allows simplification of
5599 "a = (b & 8) == 0;" */
5600 if (XEXP (x, 1) == constm1_rtx
5601 && !REG_P (XEXP (x, 0))
5602 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5603 && REG_P (SUBREG_REG (XEXP (x, 0))))
5604 && nonzero_bits (XEXP (x, 0), mode) == 1)
5605 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5606 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5607 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5608 GET_MODE_PRECISION (mode) - 1),
5609 GET_MODE_PRECISION (mode) - 1);
5611 /* If we are adding two things that have no bits in common, convert
5612 the addition into an IOR. This will often be further simplified,
5613 for example in cases like ((a & 1) + (a & 2)), which can
5614 become a & 3. */
5616 if (HWI_COMPUTABLE_MODE_P (mode)
5617 && (nonzero_bits (XEXP (x, 0), mode)
5618 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5620 /* Try to simplify the expression further. */
5621 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5622 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5624 /* If we could, great. If not, do not go ahead with the IOR
5625 replacement, since PLUS appears in many special purpose
5626 address arithmetic instructions. */
5627 if (GET_CODE (temp) != CLOBBER
5628 && (GET_CODE (temp) != IOR
5629 || ((XEXP (temp, 0) != XEXP (x, 0)
5630 || XEXP (temp, 1) != XEXP (x, 1))
5631 && (XEXP (temp, 0) != XEXP (x, 1)
5632 || XEXP (temp, 1) != XEXP (x, 0)))))
5633 return temp;
5635 break;
5637 case MINUS:
5638 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5639 (and <foo> (const_int pow2-1)) */
5640 if (GET_CODE (XEXP (x, 1)) == AND
5641 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5642 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5643 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5644 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5645 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5646 break;
5648 case MULT:
5649 /* If we have (mult (plus A B) C), apply the distributive law and then
5650 the inverse distributive law to see if things simplify. This
5651 occurs mostly in addresses, often when unrolling loops. */
5653 if (GET_CODE (XEXP (x, 0)) == PLUS)
5655 rtx result = distribute_and_simplify_rtx (x, 0);
5656 if (result)
5657 return result;
5660 /* Try simplify a*(b/c) as (a*b)/c. */
5661 if (FLOAT_MODE_P (mode) && flag_associative_math
5662 && GET_CODE (XEXP (x, 0)) == DIV)
5664 rtx tem = simplify_binary_operation (MULT, mode,
5665 XEXP (XEXP (x, 0), 0),
5666 XEXP (x, 1));
5667 if (tem)
5668 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5670 break;
5672 case UDIV:
5673 /* If this is a divide by a power of two, treat it as a shift if
5674 its first operand is a shift. */
5675 if (CONST_INT_P (XEXP (x, 1))
5676 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5677 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5678 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5679 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5680 || GET_CODE (XEXP (x, 0)) == ROTATE
5681 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5682 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5683 break;
5685 case EQ: case NE:
5686 case GT: case GTU: case GE: case GEU:
5687 case LT: case LTU: case LE: case LEU:
5688 case UNEQ: case LTGT:
5689 case UNGT: case UNGE:
5690 case UNLT: case UNLE:
5691 case UNORDERED: case ORDERED:
5692 /* If the first operand is a condition code, we can't do anything
5693 with it. */
5694 if (GET_CODE (XEXP (x, 0)) == COMPARE
5695 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5696 && ! CC0_P (XEXP (x, 0))))
5698 rtx op0 = XEXP (x, 0);
5699 rtx op1 = XEXP (x, 1);
5700 enum rtx_code new_code;
5702 if (GET_CODE (op0) == COMPARE)
5703 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5705 /* Simplify our comparison, if possible. */
5706 new_code = simplify_comparison (code, &op0, &op1);
5708 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5709 if only the low-order bit is possibly nonzero in X (such as when
5710 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5711 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5712 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5713 (plus X 1).
5715 Remove any ZERO_EXTRACT we made when thinking this was a
5716 comparison. It may now be simpler to use, e.g., an AND. If a
5717 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5718 the call to make_compound_operation in the SET case.
5720 Don't apply these optimizations if the caller would
5721 prefer a comparison rather than a value.
5722 E.g., for the condition in an IF_THEN_ELSE most targets need
5723 an explicit comparison. */
5725 if (in_cond)
5728 else if (STORE_FLAG_VALUE == 1
5729 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5730 && op1 == const0_rtx
5731 && mode == GET_MODE (op0)
5732 && nonzero_bits (op0, mode) == 1)
5733 return gen_lowpart (mode,
5734 expand_compound_operation (op0));
5736 else if (STORE_FLAG_VALUE == 1
5737 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5738 && op1 == const0_rtx
5739 && mode == GET_MODE (op0)
5740 && (num_sign_bit_copies (op0, mode)
5741 == GET_MODE_PRECISION (mode)))
5743 op0 = expand_compound_operation (op0);
5744 return simplify_gen_unary (NEG, mode,
5745 gen_lowpart (mode, op0),
5746 mode);
5749 else if (STORE_FLAG_VALUE == 1
5750 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5751 && op1 == const0_rtx
5752 && mode == GET_MODE (op0)
5753 && nonzero_bits (op0, mode) == 1)
5755 op0 = expand_compound_operation (op0);
5756 return simplify_gen_binary (XOR, mode,
5757 gen_lowpart (mode, op0),
5758 const1_rtx);
5761 else if (STORE_FLAG_VALUE == 1
5762 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5763 && op1 == const0_rtx
5764 && mode == GET_MODE (op0)
5765 && (num_sign_bit_copies (op0, mode)
5766 == GET_MODE_PRECISION (mode)))
5768 op0 = expand_compound_operation (op0);
5769 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5772 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5773 those above. */
5774 if (in_cond)
5777 else if (STORE_FLAG_VALUE == -1
5778 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5779 && op1 == const0_rtx
5780 && (num_sign_bit_copies (op0, mode)
5781 == GET_MODE_PRECISION (mode)))
5782 return gen_lowpart (mode,
5783 expand_compound_operation (op0));
5785 else if (STORE_FLAG_VALUE == -1
5786 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5787 && op1 == const0_rtx
5788 && mode == GET_MODE (op0)
5789 && nonzero_bits (op0, mode) == 1)
5791 op0 = expand_compound_operation (op0);
5792 return simplify_gen_unary (NEG, mode,
5793 gen_lowpart (mode, op0),
5794 mode);
5797 else if (STORE_FLAG_VALUE == -1
5798 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5799 && op1 == const0_rtx
5800 && mode == GET_MODE (op0)
5801 && (num_sign_bit_copies (op0, mode)
5802 == GET_MODE_PRECISION (mode)))
5804 op0 = expand_compound_operation (op0);
5805 return simplify_gen_unary (NOT, mode,
5806 gen_lowpart (mode, op0),
5807 mode);
5810 /* If X is 0/1, (eq X 0) is X-1. */
5811 else if (STORE_FLAG_VALUE == -1
5812 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5813 && op1 == const0_rtx
5814 && mode == GET_MODE (op0)
5815 && nonzero_bits (op0, mode) == 1)
5817 op0 = expand_compound_operation (op0);
5818 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5821 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5822 one bit that might be nonzero, we can convert (ne x 0) to
5823 (ashift x c) where C puts the bit in the sign bit. Remove any
5824 AND with STORE_FLAG_VALUE when we are done, since we are only
5825 going to test the sign bit. */
5826 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5827 && HWI_COMPUTABLE_MODE_P (mode)
5828 && val_signbit_p (mode, STORE_FLAG_VALUE)
5829 && op1 == const0_rtx
5830 && mode == GET_MODE (op0)
5831 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5833 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5834 expand_compound_operation (op0),
5835 GET_MODE_PRECISION (mode) - 1 - i);
5836 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5837 return XEXP (x, 0);
5838 else
5839 return x;
5842 /* If the code changed, return a whole new comparison.
5843 We also need to avoid using SUBST in cases where
5844 simplify_comparison has widened a comparison with a CONST_INT,
5845 since in that case the wider CONST_INT may fail the sanity
5846 checks in do_SUBST. */
5847 if (new_code != code
5848 || (CONST_INT_P (op1)
5849 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
5850 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
5851 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5853 /* Otherwise, keep this operation, but maybe change its operands.
5854 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5855 SUBST (XEXP (x, 0), op0);
5856 SUBST (XEXP (x, 1), op1);
5858 break;
5860 case IF_THEN_ELSE:
5861 return simplify_if_then_else (x);
5863 case ZERO_EXTRACT:
5864 case SIGN_EXTRACT:
5865 case ZERO_EXTEND:
5866 case SIGN_EXTEND:
5867 /* If we are processing SET_DEST, we are done. */
5868 if (in_dest)
5869 return x;
5871 return expand_compound_operation (x);
5873 case SET:
5874 return simplify_set (x);
5876 case AND:
5877 case IOR:
5878 return simplify_logical (x);
5880 case ASHIFT:
5881 case LSHIFTRT:
5882 case ASHIFTRT:
5883 case ROTATE:
5884 case ROTATERT:
5885 /* If this is a shift by a constant amount, simplify it. */
5886 if (CONST_INT_P (XEXP (x, 1)))
5887 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5888 INTVAL (XEXP (x, 1)));
5890 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5891 SUBST (XEXP (x, 1),
5892 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5893 ((unsigned HOST_WIDE_INT) 1
5894 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5895 - 1,
5896 0));
5897 break;
5899 default:
5900 break;
5903 return x;
5906 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5908 static rtx
5909 simplify_if_then_else (rtx x)
5911 enum machine_mode mode = GET_MODE (x);
5912 rtx cond = XEXP (x, 0);
5913 rtx true_rtx = XEXP (x, 1);
5914 rtx false_rtx = XEXP (x, 2);
5915 enum rtx_code true_code = GET_CODE (cond);
5916 int comparison_p = COMPARISON_P (cond);
5917 rtx temp;
5918 int i;
5919 enum rtx_code false_code;
5920 rtx reversed;
5922 /* Simplify storing of the truth value. */
5923 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5924 return simplify_gen_relational (true_code, mode, VOIDmode,
5925 XEXP (cond, 0), XEXP (cond, 1));
5927 /* Also when the truth value has to be reversed. */
5928 if (comparison_p
5929 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5930 && (reversed = reversed_comparison (cond, mode)))
5931 return reversed;
5933 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5934 in it is being compared against certain values. Get the true and false
5935 comparisons and see if that says anything about the value of each arm. */
5937 if (comparison_p
5938 && ((false_code = reversed_comparison_code (cond, NULL))
5939 != UNKNOWN)
5940 && REG_P (XEXP (cond, 0)))
5942 HOST_WIDE_INT nzb;
5943 rtx from = XEXP (cond, 0);
5944 rtx true_val = XEXP (cond, 1);
5945 rtx false_val = true_val;
5946 int swapped = 0;
5948 /* If FALSE_CODE is EQ, swap the codes and arms. */
5950 if (false_code == EQ)
5952 swapped = 1, true_code = EQ, false_code = NE;
5953 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5956 /* If we are comparing against zero and the expression being tested has
5957 only a single bit that might be nonzero, that is its value when it is
5958 not equal to zero. Similarly if it is known to be -1 or 0. */
5960 if (true_code == EQ && true_val == const0_rtx
5961 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5963 false_code = EQ;
5964 false_val = gen_int_mode (nzb, GET_MODE (from));
5966 else if (true_code == EQ && true_val == const0_rtx
5967 && (num_sign_bit_copies (from, GET_MODE (from))
5968 == GET_MODE_PRECISION (GET_MODE (from))))
5970 false_code = EQ;
5971 false_val = constm1_rtx;
5974 /* Now simplify an arm if we know the value of the register in the
5975 branch and it is used in the arm. Be careful due to the potential
5976 of locally-shared RTL. */
5978 if (reg_mentioned_p (from, true_rtx))
5979 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5980 from, true_val),
5981 pc_rtx, pc_rtx, 0, 0, 0);
5982 if (reg_mentioned_p (from, false_rtx))
5983 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5984 from, false_val),
5985 pc_rtx, pc_rtx, 0, 0, 0);
5987 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5988 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5990 true_rtx = XEXP (x, 1);
5991 false_rtx = XEXP (x, 2);
5992 true_code = GET_CODE (cond);
5995 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5996 reversed, do so to avoid needing two sets of patterns for
5997 subtract-and-branch insns. Similarly if we have a constant in the true
5998 arm, the false arm is the same as the first operand of the comparison, or
5999 the false arm is more complicated than the true arm. */
6001 if (comparison_p
6002 && reversed_comparison_code (cond, NULL) != UNKNOWN
6003 && (true_rtx == pc_rtx
6004 || (CONSTANT_P (true_rtx)
6005 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6006 || true_rtx == const0_rtx
6007 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6008 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6009 && !OBJECT_P (false_rtx))
6010 || reg_mentioned_p (true_rtx, false_rtx)
6011 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6013 true_code = reversed_comparison_code (cond, NULL);
6014 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6015 SUBST (XEXP (x, 1), false_rtx);
6016 SUBST (XEXP (x, 2), true_rtx);
6018 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6019 cond = XEXP (x, 0);
6021 /* It is possible that the conditional has been simplified out. */
6022 true_code = GET_CODE (cond);
6023 comparison_p = COMPARISON_P (cond);
6026 /* If the two arms are identical, we don't need the comparison. */
6028 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6029 return true_rtx;
6031 /* Convert a == b ? b : a to "a". */
6032 if (true_code == EQ && ! side_effects_p (cond)
6033 && !HONOR_NANS (mode)
6034 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6035 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6036 return false_rtx;
6037 else if (true_code == NE && ! side_effects_p (cond)
6038 && !HONOR_NANS (mode)
6039 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6040 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6041 return true_rtx;
6043 /* Look for cases where we have (abs x) or (neg (abs X)). */
6045 if (GET_MODE_CLASS (mode) == MODE_INT
6046 && comparison_p
6047 && XEXP (cond, 1) == const0_rtx
6048 && GET_CODE (false_rtx) == NEG
6049 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6050 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6051 && ! side_effects_p (true_rtx))
6052 switch (true_code)
6054 case GT:
6055 case GE:
6056 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6057 case LT:
6058 case LE:
6059 return
6060 simplify_gen_unary (NEG, mode,
6061 simplify_gen_unary (ABS, mode, true_rtx, mode),
6062 mode);
6063 default:
6064 break;
6067 /* Look for MIN or MAX. */
6069 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6070 && comparison_p
6071 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6072 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6073 && ! side_effects_p (cond))
6074 switch (true_code)
6076 case GE:
6077 case GT:
6078 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6079 case LE:
6080 case LT:
6081 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6082 case GEU:
6083 case GTU:
6084 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6085 case LEU:
6086 case LTU:
6087 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6088 default:
6089 break;
6092 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6093 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6094 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6095 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6096 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6097 neither 1 or -1, but it isn't worth checking for. */
6099 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6100 && comparison_p
6101 && GET_MODE_CLASS (mode) == MODE_INT
6102 && ! side_effects_p (x))
6104 rtx t = make_compound_operation (true_rtx, SET);
6105 rtx f = make_compound_operation (false_rtx, SET);
6106 rtx cond_op0 = XEXP (cond, 0);
6107 rtx cond_op1 = XEXP (cond, 1);
6108 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6109 enum machine_mode m = mode;
6110 rtx z = 0, c1 = NULL_RTX;
6112 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6113 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6114 || GET_CODE (t) == ASHIFT
6115 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6116 && rtx_equal_p (XEXP (t, 0), f))
6117 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6119 /* If an identity-zero op is commutative, check whether there
6120 would be a match if we swapped the operands. */
6121 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6122 || GET_CODE (t) == XOR)
6123 && rtx_equal_p (XEXP (t, 1), f))
6124 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6125 else if (GET_CODE (t) == SIGN_EXTEND
6126 && (GET_CODE (XEXP (t, 0)) == PLUS
6127 || GET_CODE (XEXP (t, 0)) == MINUS
6128 || GET_CODE (XEXP (t, 0)) == IOR
6129 || GET_CODE (XEXP (t, 0)) == XOR
6130 || GET_CODE (XEXP (t, 0)) == ASHIFT
6131 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6132 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6133 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6134 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6135 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6136 && (num_sign_bit_copies (f, GET_MODE (f))
6137 > (unsigned int)
6138 (GET_MODE_PRECISION (mode)
6139 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6141 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6142 extend_op = SIGN_EXTEND;
6143 m = GET_MODE (XEXP (t, 0));
6145 else if (GET_CODE (t) == SIGN_EXTEND
6146 && (GET_CODE (XEXP (t, 0)) == PLUS
6147 || GET_CODE (XEXP (t, 0)) == IOR
6148 || GET_CODE (XEXP (t, 0)) == XOR)
6149 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6150 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6151 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6152 && (num_sign_bit_copies (f, GET_MODE (f))
6153 > (unsigned int)
6154 (GET_MODE_PRECISION (mode)
6155 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6157 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6158 extend_op = SIGN_EXTEND;
6159 m = GET_MODE (XEXP (t, 0));
6161 else if (GET_CODE (t) == ZERO_EXTEND
6162 && (GET_CODE (XEXP (t, 0)) == PLUS
6163 || GET_CODE (XEXP (t, 0)) == MINUS
6164 || GET_CODE (XEXP (t, 0)) == IOR
6165 || GET_CODE (XEXP (t, 0)) == XOR
6166 || GET_CODE (XEXP (t, 0)) == ASHIFT
6167 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6168 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6169 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6170 && HWI_COMPUTABLE_MODE_P (mode)
6171 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6172 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6173 && ((nonzero_bits (f, GET_MODE (f))
6174 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6175 == 0))
6177 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6178 extend_op = ZERO_EXTEND;
6179 m = GET_MODE (XEXP (t, 0));
6181 else if (GET_CODE (t) == ZERO_EXTEND
6182 && (GET_CODE (XEXP (t, 0)) == PLUS
6183 || GET_CODE (XEXP (t, 0)) == IOR
6184 || GET_CODE (XEXP (t, 0)) == XOR)
6185 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6186 && HWI_COMPUTABLE_MODE_P (mode)
6187 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6188 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6189 && ((nonzero_bits (f, GET_MODE (f))
6190 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6191 == 0))
6193 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6194 extend_op = ZERO_EXTEND;
6195 m = GET_MODE (XEXP (t, 0));
6198 if (z)
6200 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6201 cond_op0, cond_op1),
6202 pc_rtx, pc_rtx, 0, 0, 0);
6203 temp = simplify_gen_binary (MULT, m, temp,
6204 simplify_gen_binary (MULT, m, c1,
6205 const_true_rtx));
6206 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6207 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6209 if (extend_op != UNKNOWN)
6210 temp = simplify_gen_unary (extend_op, mode, temp, m);
6212 return temp;
6216 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6217 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6218 negation of a single bit, we can convert this operation to a shift. We
6219 can actually do this more generally, but it doesn't seem worth it. */
6221 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6222 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6223 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6224 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6225 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6226 == GET_MODE_PRECISION (mode))
6227 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6228 return
6229 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6230 gen_lowpart (mode, XEXP (cond, 0)), i);
6232 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6233 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6234 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6235 && GET_MODE (XEXP (cond, 0)) == mode
6236 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6237 == nonzero_bits (XEXP (cond, 0), mode)
6238 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6239 return XEXP (cond, 0);
6241 return x;
6244 /* Simplify X, a SET expression. Return the new expression. */
6246 static rtx
6247 simplify_set (rtx x)
6249 rtx src = SET_SRC (x);
6250 rtx dest = SET_DEST (x);
6251 enum machine_mode mode
6252 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6253 rtx other_insn;
6254 rtx *cc_use;
6256 /* (set (pc) (return)) gets written as (return). */
6257 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6258 return src;
6260 /* Now that we know for sure which bits of SRC we are using, see if we can
6261 simplify the expression for the object knowing that we only need the
6262 low-order bits. */
6264 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6266 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6267 SUBST (SET_SRC (x), src);
6270 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6271 the comparison result and try to simplify it unless we already have used
6272 undobuf.other_insn. */
6273 if ((GET_MODE_CLASS (mode) == MODE_CC
6274 || GET_CODE (src) == COMPARE
6275 || CC0_P (dest))
6276 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6277 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6278 && COMPARISON_P (*cc_use)
6279 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6281 enum rtx_code old_code = GET_CODE (*cc_use);
6282 enum rtx_code new_code;
6283 rtx op0, op1, tmp;
6284 int other_changed = 0;
6285 rtx inner_compare = NULL_RTX;
6286 enum machine_mode compare_mode = GET_MODE (dest);
6288 if (GET_CODE (src) == COMPARE)
6290 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6291 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6293 inner_compare = op0;
6294 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6297 else
6298 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6300 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6301 op0, op1);
6302 if (!tmp)
6303 new_code = old_code;
6304 else if (!CONSTANT_P (tmp))
6306 new_code = GET_CODE (tmp);
6307 op0 = XEXP (tmp, 0);
6308 op1 = XEXP (tmp, 1);
6310 else
6312 rtx pat = PATTERN (other_insn);
6313 undobuf.other_insn = other_insn;
6314 SUBST (*cc_use, tmp);
6316 /* Attempt to simplify CC user. */
6317 if (GET_CODE (pat) == SET)
6319 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6320 if (new_rtx != NULL_RTX)
6321 SUBST (SET_SRC (pat), new_rtx);
6324 /* Convert X into a no-op move. */
6325 SUBST (SET_DEST (x), pc_rtx);
6326 SUBST (SET_SRC (x), pc_rtx);
6327 return x;
6330 /* Simplify our comparison, if possible. */
6331 new_code = simplify_comparison (new_code, &op0, &op1);
6333 #ifdef SELECT_CC_MODE
6334 /* If this machine has CC modes other than CCmode, check to see if we
6335 need to use a different CC mode here. */
6336 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6337 compare_mode = GET_MODE (op0);
6338 else if (inner_compare
6339 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6340 && new_code == old_code
6341 && op0 == XEXP (inner_compare, 0)
6342 && op1 == XEXP (inner_compare, 1))
6343 compare_mode = GET_MODE (inner_compare);
6344 else
6345 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6347 #ifndef HAVE_cc0
6348 /* If the mode changed, we have to change SET_DEST, the mode in the
6349 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6350 a hard register, just build new versions with the proper mode. If it
6351 is a pseudo, we lose unless it is only time we set the pseudo, in
6352 which case we can safely change its mode. */
6353 if (compare_mode != GET_MODE (dest))
6355 if (can_change_dest_mode (dest, 0, compare_mode))
6357 unsigned int regno = REGNO (dest);
6358 rtx new_dest;
6360 if (regno < FIRST_PSEUDO_REGISTER)
6361 new_dest = gen_rtx_REG (compare_mode, regno);
6362 else
6364 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6365 new_dest = regno_reg_rtx[regno];
6368 SUBST (SET_DEST (x), new_dest);
6369 SUBST (XEXP (*cc_use, 0), new_dest);
6370 other_changed = 1;
6372 dest = new_dest;
6375 #endif /* cc0 */
6376 #endif /* SELECT_CC_MODE */
6378 /* If the code changed, we have to build a new comparison in
6379 undobuf.other_insn. */
6380 if (new_code != old_code)
6382 int other_changed_previously = other_changed;
6383 unsigned HOST_WIDE_INT mask;
6384 rtx old_cc_use = *cc_use;
6386 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6387 dest, const0_rtx));
6388 other_changed = 1;
6390 /* If the only change we made was to change an EQ into an NE or
6391 vice versa, OP0 has only one bit that might be nonzero, and OP1
6392 is zero, check if changing the user of the condition code will
6393 produce a valid insn. If it won't, we can keep the original code
6394 in that insn by surrounding our operation with an XOR. */
6396 if (((old_code == NE && new_code == EQ)
6397 || (old_code == EQ && new_code == NE))
6398 && ! other_changed_previously && op1 == const0_rtx
6399 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6400 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6402 rtx pat = PATTERN (other_insn), note = 0;
6404 if ((recog_for_combine (&pat, other_insn, &note) < 0
6405 && ! check_asm_operands (pat)))
6407 *cc_use = old_cc_use;
6408 other_changed = 0;
6410 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6411 gen_int_mode (mask,
6412 GET_MODE (op0)));
6417 if (other_changed)
6418 undobuf.other_insn = other_insn;
6420 /* Otherwise, if we didn't previously have a COMPARE in the
6421 correct mode, we need one. */
6422 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6424 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6425 src = SET_SRC (x);
6427 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6429 SUBST (SET_SRC (x), op0);
6430 src = SET_SRC (x);
6432 /* Otherwise, update the COMPARE if needed. */
6433 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6435 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6436 src = SET_SRC (x);
6439 else
6441 /* Get SET_SRC in a form where we have placed back any
6442 compound expressions. Then do the checks below. */
6443 src = make_compound_operation (src, SET);
6444 SUBST (SET_SRC (x), src);
6447 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6448 and X being a REG or (subreg (reg)), we may be able to convert this to
6449 (set (subreg:m2 x) (op)).
6451 We can always do this if M1 is narrower than M2 because that means that
6452 we only care about the low bits of the result.
6454 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6455 perform a narrower operation than requested since the high-order bits will
6456 be undefined. On machine where it is defined, this transformation is safe
6457 as long as M1 and M2 have the same number of words. */
6459 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6460 && !OBJECT_P (SUBREG_REG (src))
6461 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6462 / UNITS_PER_WORD)
6463 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6464 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6465 #ifndef WORD_REGISTER_OPERATIONS
6466 && (GET_MODE_SIZE (GET_MODE (src))
6467 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6468 #endif
6469 #ifdef CANNOT_CHANGE_MODE_CLASS
6470 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6471 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6472 GET_MODE (SUBREG_REG (src)),
6473 GET_MODE (src)))
6474 #endif
6475 && (REG_P (dest)
6476 || (GET_CODE (dest) == SUBREG
6477 && REG_P (SUBREG_REG (dest)))))
6479 SUBST (SET_DEST (x),
6480 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6481 dest));
6482 SUBST (SET_SRC (x), SUBREG_REG (src));
6484 src = SET_SRC (x), dest = SET_DEST (x);
6487 #ifdef HAVE_cc0
6488 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6489 in SRC. */
6490 if (dest == cc0_rtx
6491 && GET_CODE (src) == SUBREG
6492 && subreg_lowpart_p (src)
6493 && (GET_MODE_PRECISION (GET_MODE (src))
6494 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6496 rtx inner = SUBREG_REG (src);
6497 enum machine_mode inner_mode = GET_MODE (inner);
6499 /* Here we make sure that we don't have a sign bit on. */
6500 if (val_signbit_known_clear_p (GET_MODE (src),
6501 nonzero_bits (inner, inner_mode)))
6503 SUBST (SET_SRC (x), inner);
6504 src = SET_SRC (x);
6507 #endif
6509 #ifdef LOAD_EXTEND_OP
6510 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6511 would require a paradoxical subreg. Replace the subreg with a
6512 zero_extend to avoid the reload that would otherwise be required. */
6514 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6515 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6516 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6517 && SUBREG_BYTE (src) == 0
6518 && paradoxical_subreg_p (src)
6519 && MEM_P (SUBREG_REG (src)))
6521 SUBST (SET_SRC (x),
6522 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6523 GET_MODE (src), SUBREG_REG (src)));
6525 src = SET_SRC (x);
6527 #endif
6529 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6530 are comparing an item known to be 0 or -1 against 0, use a logical
6531 operation instead. Check for one of the arms being an IOR of the other
6532 arm with some value. We compute three terms to be IOR'ed together. In
6533 practice, at most two will be nonzero. Then we do the IOR's. */
6535 if (GET_CODE (dest) != PC
6536 && GET_CODE (src) == IF_THEN_ELSE
6537 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6538 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6539 && XEXP (XEXP (src, 0), 1) == const0_rtx
6540 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6541 #ifdef HAVE_conditional_move
6542 && ! can_conditionally_move_p (GET_MODE (src))
6543 #endif
6544 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6545 GET_MODE (XEXP (XEXP (src, 0), 0)))
6546 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6547 && ! side_effects_p (src))
6549 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6550 ? XEXP (src, 1) : XEXP (src, 2));
6551 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6552 ? XEXP (src, 2) : XEXP (src, 1));
6553 rtx term1 = const0_rtx, term2, term3;
6555 if (GET_CODE (true_rtx) == IOR
6556 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6557 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6558 else if (GET_CODE (true_rtx) == IOR
6559 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6560 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6561 else if (GET_CODE (false_rtx) == IOR
6562 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6563 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6564 else if (GET_CODE (false_rtx) == IOR
6565 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6566 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6568 term2 = simplify_gen_binary (AND, GET_MODE (src),
6569 XEXP (XEXP (src, 0), 0), true_rtx);
6570 term3 = simplify_gen_binary (AND, GET_MODE (src),
6571 simplify_gen_unary (NOT, GET_MODE (src),
6572 XEXP (XEXP (src, 0), 0),
6573 GET_MODE (src)),
6574 false_rtx);
6576 SUBST (SET_SRC (x),
6577 simplify_gen_binary (IOR, GET_MODE (src),
6578 simplify_gen_binary (IOR, GET_MODE (src),
6579 term1, term2),
6580 term3));
6582 src = SET_SRC (x);
6585 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6586 whole thing fail. */
6587 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6588 return src;
6589 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6590 return dest;
6591 else
6592 /* Convert this into a field assignment operation, if possible. */
6593 return make_field_assignment (x);
6596 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6597 result. */
6599 static rtx
6600 simplify_logical (rtx x)
6602 enum machine_mode mode = GET_MODE (x);
6603 rtx op0 = XEXP (x, 0);
6604 rtx op1 = XEXP (x, 1);
6606 switch (GET_CODE (x))
6608 case AND:
6609 /* We can call simplify_and_const_int only if we don't lose
6610 any (sign) bits when converting INTVAL (op1) to
6611 "unsigned HOST_WIDE_INT". */
6612 if (CONST_INT_P (op1)
6613 && (HWI_COMPUTABLE_MODE_P (mode)
6614 || INTVAL (op1) > 0))
6616 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6617 if (GET_CODE (x) != AND)
6618 return x;
6620 op0 = XEXP (x, 0);
6621 op1 = XEXP (x, 1);
6624 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6625 apply the distributive law and then the inverse distributive
6626 law to see if things simplify. */
6627 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6629 rtx result = distribute_and_simplify_rtx (x, 0);
6630 if (result)
6631 return result;
6633 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6635 rtx result = distribute_and_simplify_rtx (x, 1);
6636 if (result)
6637 return result;
6639 break;
6641 case IOR:
6642 /* If we have (ior (and A B) C), apply the distributive law and then
6643 the inverse distributive law to see if things simplify. */
6645 if (GET_CODE (op0) == AND)
6647 rtx result = distribute_and_simplify_rtx (x, 0);
6648 if (result)
6649 return result;
6652 if (GET_CODE (op1) == AND)
6654 rtx result = distribute_and_simplify_rtx (x, 1);
6655 if (result)
6656 return result;
6658 break;
6660 default:
6661 gcc_unreachable ();
6664 return x;
6667 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6668 operations" because they can be replaced with two more basic operations.
6669 ZERO_EXTEND is also considered "compound" because it can be replaced with
6670 an AND operation, which is simpler, though only one operation.
6672 The function expand_compound_operation is called with an rtx expression
6673 and will convert it to the appropriate shifts and AND operations,
6674 simplifying at each stage.
6676 The function make_compound_operation is called to convert an expression
6677 consisting of shifts and ANDs into the equivalent compound expression.
6678 It is the inverse of this function, loosely speaking. */
6680 static rtx
6681 expand_compound_operation (rtx x)
6683 unsigned HOST_WIDE_INT pos = 0, len;
6684 int unsignedp = 0;
6685 unsigned int modewidth;
6686 rtx tem;
6688 switch (GET_CODE (x))
6690 case ZERO_EXTEND:
6691 unsignedp = 1;
6692 case SIGN_EXTEND:
6693 /* We can't necessarily use a const_int for a multiword mode;
6694 it depends on implicitly extending the value.
6695 Since we don't know the right way to extend it,
6696 we can't tell whether the implicit way is right.
6698 Even for a mode that is no wider than a const_int,
6699 we can't win, because we need to sign extend one of its bits through
6700 the rest of it, and we don't know which bit. */
6701 if (CONST_INT_P (XEXP (x, 0)))
6702 return x;
6704 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6705 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6706 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6707 reloaded. If not for that, MEM's would very rarely be safe.
6709 Reject MODEs bigger than a word, because we might not be able
6710 to reference a two-register group starting with an arbitrary register
6711 (and currently gen_lowpart might crash for a SUBREG). */
6713 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6714 return x;
6716 /* Reject MODEs that aren't scalar integers because turning vector
6717 or complex modes into shifts causes problems. */
6719 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6720 return x;
6722 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6723 /* If the inner object has VOIDmode (the only way this can happen
6724 is if it is an ASM_OPERANDS), we can't do anything since we don't
6725 know how much masking to do. */
6726 if (len == 0)
6727 return x;
6729 break;
6731 case ZERO_EXTRACT:
6732 unsignedp = 1;
6734 /* ... fall through ... */
6736 case SIGN_EXTRACT:
6737 /* If the operand is a CLOBBER, just return it. */
6738 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6739 return XEXP (x, 0);
6741 if (!CONST_INT_P (XEXP (x, 1))
6742 || !CONST_INT_P (XEXP (x, 2))
6743 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6744 return x;
6746 /* Reject MODEs that aren't scalar integers because turning vector
6747 or complex modes into shifts causes problems. */
6749 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6750 return x;
6752 len = INTVAL (XEXP (x, 1));
6753 pos = INTVAL (XEXP (x, 2));
6755 /* This should stay within the object being extracted, fail otherwise. */
6756 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6757 return x;
6759 if (BITS_BIG_ENDIAN)
6760 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6762 break;
6764 default:
6765 return x;
6767 /* Convert sign extension to zero extension, if we know that the high
6768 bit is not set, as this is easier to optimize. It will be converted
6769 back to cheaper alternative in make_extraction. */
6770 if (GET_CODE (x) == SIGN_EXTEND
6771 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6772 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6773 & ~(((unsigned HOST_WIDE_INT)
6774 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6775 >> 1))
6776 == 0)))
6778 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6779 rtx temp2 = expand_compound_operation (temp);
6781 /* Make sure this is a profitable operation. */
6782 if (set_src_cost (x, optimize_this_for_speed_p)
6783 > set_src_cost (temp2, optimize_this_for_speed_p))
6784 return temp2;
6785 else if (set_src_cost (x, optimize_this_for_speed_p)
6786 > set_src_cost (temp, optimize_this_for_speed_p))
6787 return temp;
6788 else
6789 return x;
6792 /* We can optimize some special cases of ZERO_EXTEND. */
6793 if (GET_CODE (x) == ZERO_EXTEND)
6795 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6796 know that the last value didn't have any inappropriate bits
6797 set. */
6798 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6799 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6800 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6801 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6802 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6803 return XEXP (XEXP (x, 0), 0);
6805 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6806 if (GET_CODE (XEXP (x, 0)) == SUBREG
6807 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6808 && subreg_lowpart_p (XEXP (x, 0))
6809 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6810 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6811 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6812 return SUBREG_REG (XEXP (x, 0));
6814 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6815 is a comparison and STORE_FLAG_VALUE permits. This is like
6816 the first case, but it works even when GET_MODE (x) is larger
6817 than HOST_WIDE_INT. */
6818 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6819 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6820 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6821 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6822 <= HOST_BITS_PER_WIDE_INT)
6823 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6824 return XEXP (XEXP (x, 0), 0);
6826 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6827 if (GET_CODE (XEXP (x, 0)) == SUBREG
6828 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6829 && subreg_lowpart_p (XEXP (x, 0))
6830 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6831 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6832 <= HOST_BITS_PER_WIDE_INT)
6833 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6834 return SUBREG_REG (XEXP (x, 0));
6838 /* If we reach here, we want to return a pair of shifts. The inner
6839 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6840 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6841 logical depending on the value of UNSIGNEDP.
6843 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6844 converted into an AND of a shift.
6846 We must check for the case where the left shift would have a negative
6847 count. This can happen in a case like (x >> 31) & 255 on machines
6848 that can't shift by a constant. On those machines, we would first
6849 combine the shift with the AND to produce a variable-position
6850 extraction. Then the constant of 31 would be substituted in
6851 to produce such a position. */
6853 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6854 if (modewidth >= pos + len)
6856 enum machine_mode mode = GET_MODE (x);
6857 tem = gen_lowpart (mode, XEXP (x, 0));
6858 if (!tem || GET_CODE (tem) == CLOBBER)
6859 return x;
6860 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6861 tem, modewidth - pos - len);
6862 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6863 mode, tem, modewidth - len);
6865 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6866 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6867 simplify_shift_const (NULL_RTX, LSHIFTRT,
6868 GET_MODE (x),
6869 XEXP (x, 0), pos),
6870 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6871 else
6872 /* Any other cases we can't handle. */
6873 return x;
6875 /* If we couldn't do this for some reason, return the original
6876 expression. */
6877 if (GET_CODE (tem) == CLOBBER)
6878 return x;
6880 return tem;
6883 /* X is a SET which contains an assignment of one object into
6884 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6885 or certain SUBREGS). If possible, convert it into a series of
6886 logical operations.
6888 We half-heartedly support variable positions, but do not at all
6889 support variable lengths. */
6891 static const_rtx
6892 expand_field_assignment (const_rtx x)
6894 rtx inner;
6895 rtx pos; /* Always counts from low bit. */
6896 int len;
6897 rtx mask, cleared, masked;
6898 enum machine_mode compute_mode;
6900 /* Loop until we find something we can't simplify. */
6901 while (1)
6903 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6904 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6906 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6907 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
6908 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6910 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6911 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6913 inner = XEXP (SET_DEST (x), 0);
6914 len = INTVAL (XEXP (SET_DEST (x), 1));
6915 pos = XEXP (SET_DEST (x), 2);
6917 /* A constant position should stay within the width of INNER. */
6918 if (CONST_INT_P (pos)
6919 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
6920 break;
6922 if (BITS_BIG_ENDIAN)
6924 if (CONST_INT_P (pos))
6925 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
6926 - INTVAL (pos));
6927 else if (GET_CODE (pos) == MINUS
6928 && CONST_INT_P (XEXP (pos, 1))
6929 && (INTVAL (XEXP (pos, 1))
6930 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
6931 /* If position is ADJUST - X, new position is X. */
6932 pos = XEXP (pos, 0);
6933 else
6935 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
6936 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6937 gen_int_mode (prec - len,
6938 GET_MODE (pos)),
6939 pos);
6944 /* A SUBREG between two modes that occupy the same numbers of words
6945 can be done by moving the SUBREG to the source. */
6946 else if (GET_CODE (SET_DEST (x)) == SUBREG
6947 /* We need SUBREGs to compute nonzero_bits properly. */
6948 && nonzero_sign_valid
6949 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6950 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6951 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6952 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6954 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6955 gen_lowpart
6956 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6957 SET_SRC (x)));
6958 continue;
6960 else
6961 break;
6963 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6964 inner = SUBREG_REG (inner);
6966 compute_mode = GET_MODE (inner);
6968 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6969 if (! SCALAR_INT_MODE_P (compute_mode))
6971 enum machine_mode imode;
6973 /* Don't do anything for vector or complex integral types. */
6974 if (! FLOAT_MODE_P (compute_mode))
6975 break;
6977 /* Try to find an integral mode to pun with. */
6978 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6979 if (imode == BLKmode)
6980 break;
6982 compute_mode = imode;
6983 inner = gen_lowpart (imode, inner);
6986 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6987 if (len >= HOST_BITS_PER_WIDE_INT)
6988 break;
6990 /* Now compute the equivalent expression. Make a copy of INNER
6991 for the SET_DEST in case it is a MEM into which we will substitute;
6992 we don't want shared RTL in that case. */
6993 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
6994 compute_mode);
6995 cleared = simplify_gen_binary (AND, compute_mode,
6996 simplify_gen_unary (NOT, compute_mode,
6997 simplify_gen_binary (ASHIFT,
6998 compute_mode,
6999 mask, pos),
7000 compute_mode),
7001 inner);
7002 masked = simplify_gen_binary (ASHIFT, compute_mode,
7003 simplify_gen_binary (
7004 AND, compute_mode,
7005 gen_lowpart (compute_mode, SET_SRC (x)),
7006 mask),
7007 pos);
7009 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7010 simplify_gen_binary (IOR, compute_mode,
7011 cleared, masked));
7014 return x;
7017 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7018 it is an RTX that represents the (variable) starting position; otherwise,
7019 POS is the (constant) starting bit position. Both are counted from the LSB.
7021 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7023 IN_DEST is nonzero if this is a reference in the destination of a SET.
7024 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7025 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7026 be used.
7028 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7029 ZERO_EXTRACT should be built even for bits starting at bit 0.
7031 MODE is the desired mode of the result (if IN_DEST == 0).
7033 The result is an RTX for the extraction or NULL_RTX if the target
7034 can't handle it. */
7036 static rtx
7037 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7038 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7039 int in_dest, int in_compare)
7041 /* This mode describes the size of the storage area
7042 to fetch the overall value from. Within that, we
7043 ignore the POS lowest bits, etc. */
7044 enum machine_mode is_mode = GET_MODE (inner);
7045 enum machine_mode inner_mode;
7046 enum machine_mode wanted_inner_mode;
7047 enum machine_mode wanted_inner_reg_mode = word_mode;
7048 enum machine_mode pos_mode = word_mode;
7049 enum machine_mode extraction_mode = word_mode;
7050 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7051 rtx new_rtx = 0;
7052 rtx orig_pos_rtx = pos_rtx;
7053 HOST_WIDE_INT orig_pos;
7055 if (pos_rtx && CONST_INT_P (pos_rtx))
7056 pos = INTVAL (pos_rtx), pos_rtx = 0;
7058 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7060 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7061 consider just the QI as the memory to extract from.
7062 The subreg adds or removes high bits; its mode is
7063 irrelevant to the meaning of this extraction,
7064 since POS and LEN count from the lsb. */
7065 if (MEM_P (SUBREG_REG (inner)))
7066 is_mode = GET_MODE (SUBREG_REG (inner));
7067 inner = SUBREG_REG (inner);
7069 else if (GET_CODE (inner) == ASHIFT
7070 && CONST_INT_P (XEXP (inner, 1))
7071 && pos_rtx == 0 && pos == 0
7072 && len > UINTVAL (XEXP (inner, 1)))
7074 /* We're extracting the least significant bits of an rtx
7075 (ashift X (const_int C)), where LEN > C. Extract the
7076 least significant (LEN - C) bits of X, giving an rtx
7077 whose mode is MODE, then shift it left C times. */
7078 new_rtx = make_extraction (mode, XEXP (inner, 0),
7079 0, 0, len - INTVAL (XEXP (inner, 1)),
7080 unsignedp, in_dest, in_compare);
7081 if (new_rtx != 0)
7082 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7084 else if (GET_CODE (inner) == TRUNCATE)
7085 inner = XEXP (inner, 0);
7087 inner_mode = GET_MODE (inner);
7089 /* See if this can be done without an extraction. We never can if the
7090 width of the field is not the same as that of some integer mode. For
7091 registers, we can only avoid the extraction if the position is at the
7092 low-order bit and this is either not in the destination or we have the
7093 appropriate STRICT_LOW_PART operation available.
7095 For MEM, we can avoid an extract if the field starts on an appropriate
7096 boundary and we can change the mode of the memory reference. */
7098 if (tmode != BLKmode
7099 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7100 && !MEM_P (inner)
7101 && (inner_mode == tmode
7102 || !REG_P (inner)
7103 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7104 || reg_truncated_to_mode (tmode, inner))
7105 && (! in_dest
7106 || (REG_P (inner)
7107 && have_insn_for (STRICT_LOW_PART, tmode))))
7108 || (MEM_P (inner) && pos_rtx == 0
7109 && (pos
7110 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7111 : BITS_PER_UNIT)) == 0
7112 /* We can't do this if we are widening INNER_MODE (it
7113 may not be aligned, for one thing). */
7114 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7115 && (inner_mode == tmode
7116 || (! mode_dependent_address_p (XEXP (inner, 0),
7117 MEM_ADDR_SPACE (inner))
7118 && ! MEM_VOLATILE_P (inner))))))
7120 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7121 field. If the original and current mode are the same, we need not
7122 adjust the offset. Otherwise, we do if bytes big endian.
7124 If INNER is not a MEM, get a piece consisting of just the field
7125 of interest (in this case POS % BITS_PER_WORD must be 0). */
7127 if (MEM_P (inner))
7129 HOST_WIDE_INT offset;
7131 /* POS counts from lsb, but make OFFSET count in memory order. */
7132 if (BYTES_BIG_ENDIAN)
7133 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7134 else
7135 offset = pos / BITS_PER_UNIT;
7137 new_rtx = adjust_address_nv (inner, tmode, offset);
7139 else if (REG_P (inner))
7141 if (tmode != inner_mode)
7143 /* We can't call gen_lowpart in a DEST since we
7144 always want a SUBREG (see below) and it would sometimes
7145 return a new hard register. */
7146 if (pos || in_dest)
7148 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7150 if (WORDS_BIG_ENDIAN
7151 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7152 final_word = ((GET_MODE_SIZE (inner_mode)
7153 - GET_MODE_SIZE (tmode))
7154 / UNITS_PER_WORD) - final_word;
7156 final_word *= UNITS_PER_WORD;
7157 if (BYTES_BIG_ENDIAN &&
7158 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7159 final_word += (GET_MODE_SIZE (inner_mode)
7160 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7162 /* Avoid creating invalid subregs, for example when
7163 simplifying (x>>32)&255. */
7164 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7165 return NULL_RTX;
7167 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7169 else
7170 new_rtx = gen_lowpart (tmode, inner);
7172 else
7173 new_rtx = inner;
7175 else
7176 new_rtx = force_to_mode (inner, tmode,
7177 len >= HOST_BITS_PER_WIDE_INT
7178 ? ~(unsigned HOST_WIDE_INT) 0
7179 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7182 /* If this extraction is going into the destination of a SET,
7183 make a STRICT_LOW_PART unless we made a MEM. */
7185 if (in_dest)
7186 return (MEM_P (new_rtx) ? new_rtx
7187 : (GET_CODE (new_rtx) != SUBREG
7188 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7189 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7191 if (mode == tmode)
7192 return new_rtx;
7194 if (CONST_SCALAR_INT_P (new_rtx))
7195 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7196 mode, new_rtx, tmode);
7198 /* If we know that no extraneous bits are set, and that the high
7199 bit is not set, convert the extraction to the cheaper of
7200 sign and zero extension, that are equivalent in these cases. */
7201 if (flag_expensive_optimizations
7202 && (HWI_COMPUTABLE_MODE_P (tmode)
7203 && ((nonzero_bits (new_rtx, tmode)
7204 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7205 == 0)))
7207 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7208 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7210 /* Prefer ZERO_EXTENSION, since it gives more information to
7211 backends. */
7212 if (set_src_cost (temp, optimize_this_for_speed_p)
7213 <= set_src_cost (temp1, optimize_this_for_speed_p))
7214 return temp;
7215 return temp1;
7218 /* Otherwise, sign- or zero-extend unless we already are in the
7219 proper mode. */
7221 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7222 mode, new_rtx));
7225 /* Unless this is a COMPARE or we have a funny memory reference,
7226 don't do anything with zero-extending field extracts starting at
7227 the low-order bit since they are simple AND operations. */
7228 if (pos_rtx == 0 && pos == 0 && ! in_dest
7229 && ! in_compare && unsignedp)
7230 return 0;
7232 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7233 if the position is not a constant and the length is not 1. In all
7234 other cases, we would only be going outside our object in cases when
7235 an original shift would have been undefined. */
7236 if (MEM_P (inner)
7237 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7238 || (pos_rtx != 0 && len != 1)))
7239 return 0;
7241 enum extraction_pattern pattern = (in_dest ? EP_insv
7242 : unsignedp ? EP_extzv : EP_extv);
7244 /* If INNER is not from memory, we want it to have the mode of a register
7245 extraction pattern's structure operand, or word_mode if there is no
7246 such pattern. The same applies to extraction_mode and pos_mode
7247 and their respective operands.
7249 For memory, assume that the desired extraction_mode and pos_mode
7250 are the same as for a register operation, since at present we don't
7251 have named patterns for aligned memory structures. */
7252 struct extraction_insn insn;
7253 if (get_best_reg_extraction_insn (&insn, pattern,
7254 GET_MODE_BITSIZE (inner_mode), mode))
7256 wanted_inner_reg_mode = insn.struct_mode;
7257 pos_mode = insn.pos_mode;
7258 extraction_mode = insn.field_mode;
7261 /* Never narrow an object, since that might not be safe. */
7263 if (mode != VOIDmode
7264 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7265 extraction_mode = mode;
7267 if (!MEM_P (inner))
7268 wanted_inner_mode = wanted_inner_reg_mode;
7269 else
7271 /* Be careful not to go beyond the extracted object and maintain the
7272 natural alignment of the memory. */
7273 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7274 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7275 > GET_MODE_BITSIZE (wanted_inner_mode))
7277 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7278 gcc_assert (wanted_inner_mode != VOIDmode);
7282 orig_pos = pos;
7284 if (BITS_BIG_ENDIAN)
7286 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7287 BITS_BIG_ENDIAN style. If position is constant, compute new
7288 position. Otherwise, build subtraction.
7289 Note that POS is relative to the mode of the original argument.
7290 If it's a MEM we need to recompute POS relative to that.
7291 However, if we're extracting from (or inserting into) a register,
7292 we want to recompute POS relative to wanted_inner_mode. */
7293 int width = (MEM_P (inner)
7294 ? GET_MODE_BITSIZE (is_mode)
7295 : GET_MODE_BITSIZE (wanted_inner_mode));
7297 if (pos_rtx == 0)
7298 pos = width - len - pos;
7299 else
7300 pos_rtx
7301 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7302 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7303 pos_rtx);
7304 /* POS may be less than 0 now, but we check for that below.
7305 Note that it can only be less than 0 if !MEM_P (inner). */
7308 /* If INNER has a wider mode, and this is a constant extraction, try to
7309 make it smaller and adjust the byte to point to the byte containing
7310 the value. */
7311 if (wanted_inner_mode != VOIDmode
7312 && inner_mode != wanted_inner_mode
7313 && ! pos_rtx
7314 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7315 && MEM_P (inner)
7316 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7317 && ! MEM_VOLATILE_P (inner))
7319 int offset = 0;
7321 /* The computations below will be correct if the machine is big
7322 endian in both bits and bytes or little endian in bits and bytes.
7323 If it is mixed, we must adjust. */
7325 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7326 adjust OFFSET to compensate. */
7327 if (BYTES_BIG_ENDIAN
7328 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7329 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7331 /* We can now move to the desired byte. */
7332 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7333 * GET_MODE_SIZE (wanted_inner_mode);
7334 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7336 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7337 && is_mode != wanted_inner_mode)
7338 offset = (GET_MODE_SIZE (is_mode)
7339 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7341 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7344 /* If INNER is not memory, get it into the proper mode. If we are changing
7345 its mode, POS must be a constant and smaller than the size of the new
7346 mode. */
7347 else if (!MEM_P (inner))
7349 /* On the LHS, don't create paradoxical subregs implicitely truncating
7350 the register unless TRULY_NOOP_TRUNCATION. */
7351 if (in_dest
7352 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7353 wanted_inner_mode))
7354 return NULL_RTX;
7356 if (GET_MODE (inner) != wanted_inner_mode
7357 && (pos_rtx != 0
7358 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7359 return NULL_RTX;
7361 if (orig_pos < 0)
7362 return NULL_RTX;
7364 inner = force_to_mode (inner, wanted_inner_mode,
7365 pos_rtx
7366 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7367 ? ~(unsigned HOST_WIDE_INT) 0
7368 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7369 << orig_pos),
7373 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7374 have to zero extend. Otherwise, we can just use a SUBREG. */
7375 if (pos_rtx != 0
7376 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7378 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7379 GET_MODE (pos_rtx));
7381 /* If we know that no extraneous bits are set, and that the high
7382 bit is not set, convert extraction to cheaper one - either
7383 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7384 cases. */
7385 if (flag_expensive_optimizations
7386 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7387 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7388 & ~(((unsigned HOST_WIDE_INT)
7389 GET_MODE_MASK (GET_MODE (pos_rtx)))
7390 >> 1))
7391 == 0)))
7393 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7394 GET_MODE (pos_rtx));
7396 /* Prefer ZERO_EXTENSION, since it gives more information to
7397 backends. */
7398 if (set_src_cost (temp1, optimize_this_for_speed_p)
7399 < set_src_cost (temp, optimize_this_for_speed_p))
7400 temp = temp1;
7402 pos_rtx = temp;
7405 /* Make POS_RTX unless we already have it and it is correct. If we don't
7406 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7407 be a CONST_INT. */
7408 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7409 pos_rtx = orig_pos_rtx;
7411 else if (pos_rtx == 0)
7412 pos_rtx = GEN_INT (pos);
7414 /* Make the required operation. See if we can use existing rtx. */
7415 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7416 extraction_mode, inner, GEN_INT (len), pos_rtx);
7417 if (! in_dest)
7418 new_rtx = gen_lowpart (mode, new_rtx);
7420 return new_rtx;
7423 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7424 with any other operations in X. Return X without that shift if so. */
7426 static rtx
7427 extract_left_shift (rtx x, int count)
7429 enum rtx_code code = GET_CODE (x);
7430 enum machine_mode mode = GET_MODE (x);
7431 rtx tem;
7433 switch (code)
7435 case ASHIFT:
7436 /* This is the shift itself. If it is wide enough, we will return
7437 either the value being shifted if the shift count is equal to
7438 COUNT or a shift for the difference. */
7439 if (CONST_INT_P (XEXP (x, 1))
7440 && INTVAL (XEXP (x, 1)) >= count)
7441 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7442 INTVAL (XEXP (x, 1)) - count);
7443 break;
7445 case NEG: case NOT:
7446 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7447 return simplify_gen_unary (code, mode, tem, mode);
7449 break;
7451 case PLUS: case IOR: case XOR: case AND:
7452 /* If we can safely shift this constant and we find the inner shift,
7453 make a new operation. */
7454 if (CONST_INT_P (XEXP (x, 1))
7455 && (UINTVAL (XEXP (x, 1))
7456 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7457 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7459 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7460 return simplify_gen_binary (code, mode, tem,
7461 gen_int_mode (val, mode));
7463 break;
7465 default:
7466 break;
7469 return 0;
7472 /* Look at the expression rooted at X. Look for expressions
7473 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7474 Form these expressions.
7476 Return the new rtx, usually just X.
7478 Also, for machines like the VAX that don't have logical shift insns,
7479 try to convert logical to arithmetic shift operations in cases where
7480 they are equivalent. This undoes the canonicalizations to logical
7481 shifts done elsewhere.
7483 We try, as much as possible, to re-use rtl expressions to save memory.
7485 IN_CODE says what kind of expression we are processing. Normally, it is
7486 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7487 being kludges), it is MEM. When processing the arguments of a comparison
7488 or a COMPARE against zero, it is COMPARE. */
7491 make_compound_operation (rtx x, enum rtx_code in_code)
7493 enum rtx_code code = GET_CODE (x);
7494 enum machine_mode mode = GET_MODE (x);
7495 int mode_width = GET_MODE_PRECISION (mode);
7496 rtx rhs, lhs;
7497 enum rtx_code next_code;
7498 int i, j;
7499 rtx new_rtx = 0;
7500 rtx tem;
7501 const char *fmt;
7503 /* Select the code to be used in recursive calls. Once we are inside an
7504 address, we stay there. If we have a comparison, set to COMPARE,
7505 but once inside, go back to our default of SET. */
7507 next_code = (code == MEM ? MEM
7508 : ((code == PLUS || code == MINUS)
7509 && SCALAR_INT_MODE_P (mode)) ? MEM
7510 : ((code == COMPARE || COMPARISON_P (x))
7511 && XEXP (x, 1) == const0_rtx) ? COMPARE
7512 : in_code == COMPARE ? SET : in_code);
7514 /* Process depending on the code of this operation. If NEW is set
7515 nonzero, it will be returned. */
7517 switch (code)
7519 case ASHIFT:
7520 /* Convert shifts by constants into multiplications if inside
7521 an address. */
7522 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7523 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7524 && INTVAL (XEXP (x, 1)) >= 0
7525 && SCALAR_INT_MODE_P (mode))
7527 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7528 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7530 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7531 if (GET_CODE (new_rtx) == NEG)
7533 new_rtx = XEXP (new_rtx, 0);
7534 multval = -multval;
7536 multval = trunc_int_for_mode (multval, mode);
7537 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7539 break;
7541 case PLUS:
7542 lhs = XEXP (x, 0);
7543 rhs = XEXP (x, 1);
7544 lhs = make_compound_operation (lhs, next_code);
7545 rhs = make_compound_operation (rhs, next_code);
7546 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7547 && SCALAR_INT_MODE_P (mode))
7549 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7550 XEXP (lhs, 1));
7551 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7553 else if (GET_CODE (lhs) == MULT
7554 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7556 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7557 simplify_gen_unary (NEG, mode,
7558 XEXP (lhs, 1),
7559 mode));
7560 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7562 else
7564 SUBST (XEXP (x, 0), lhs);
7565 SUBST (XEXP (x, 1), rhs);
7566 goto maybe_swap;
7568 x = gen_lowpart (mode, new_rtx);
7569 goto maybe_swap;
7571 case MINUS:
7572 lhs = XEXP (x, 0);
7573 rhs = XEXP (x, 1);
7574 lhs = make_compound_operation (lhs, next_code);
7575 rhs = make_compound_operation (rhs, next_code);
7576 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7577 && SCALAR_INT_MODE_P (mode))
7579 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7580 XEXP (rhs, 1));
7581 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7583 else if (GET_CODE (rhs) == MULT
7584 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7586 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7587 simplify_gen_unary (NEG, mode,
7588 XEXP (rhs, 1),
7589 mode));
7590 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7592 else
7594 SUBST (XEXP (x, 0), lhs);
7595 SUBST (XEXP (x, 1), rhs);
7596 return x;
7598 return gen_lowpart (mode, new_rtx);
7600 case AND:
7601 /* If the second operand is not a constant, we can't do anything
7602 with it. */
7603 if (!CONST_INT_P (XEXP (x, 1)))
7604 break;
7606 /* If the constant is a power of two minus one and the first operand
7607 is a logical right shift, make an extraction. */
7608 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7609 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7611 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7612 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7613 0, in_code == COMPARE);
7616 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7617 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7618 && subreg_lowpart_p (XEXP (x, 0))
7619 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7620 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7622 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7623 next_code);
7624 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7625 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7626 0, in_code == COMPARE);
7628 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7629 else if ((GET_CODE (XEXP (x, 0)) == XOR
7630 || GET_CODE (XEXP (x, 0)) == IOR)
7631 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7632 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7633 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7635 /* Apply the distributive law, and then try to make extractions. */
7636 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7637 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7638 XEXP (x, 1)),
7639 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7640 XEXP (x, 1)));
7641 new_rtx = make_compound_operation (new_rtx, in_code);
7644 /* If we are have (and (rotate X C) M) and C is larger than the number
7645 of bits in M, this is an extraction. */
7647 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7648 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7649 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7650 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7652 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7653 new_rtx = make_extraction (mode, new_rtx,
7654 (GET_MODE_PRECISION (mode)
7655 - INTVAL (XEXP (XEXP (x, 0), 1))),
7656 NULL_RTX, i, 1, 0, in_code == COMPARE);
7659 /* On machines without logical shifts, if the operand of the AND is
7660 a logical shift and our mask turns off all the propagated sign
7661 bits, we can replace the logical shift with an arithmetic shift. */
7662 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7663 && !have_insn_for (LSHIFTRT, mode)
7664 && have_insn_for (ASHIFTRT, mode)
7665 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7666 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7667 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7668 && mode_width <= HOST_BITS_PER_WIDE_INT)
7670 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7672 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7673 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7674 SUBST (XEXP (x, 0),
7675 gen_rtx_ASHIFTRT (mode,
7676 make_compound_operation
7677 (XEXP (XEXP (x, 0), 0), next_code),
7678 XEXP (XEXP (x, 0), 1)));
7681 /* If the constant is one less than a power of two, this might be
7682 representable by an extraction even if no shift is present.
7683 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7684 we are in a COMPARE. */
7685 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7686 new_rtx = make_extraction (mode,
7687 make_compound_operation (XEXP (x, 0),
7688 next_code),
7689 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7691 /* If we are in a comparison and this is an AND with a power of two,
7692 convert this into the appropriate bit extract. */
7693 else if (in_code == COMPARE
7694 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7695 new_rtx = make_extraction (mode,
7696 make_compound_operation (XEXP (x, 0),
7697 next_code),
7698 i, NULL_RTX, 1, 1, 0, 1);
7700 break;
7702 case LSHIFTRT:
7703 /* If the sign bit is known to be zero, replace this with an
7704 arithmetic shift. */
7705 if (have_insn_for (ASHIFTRT, mode)
7706 && ! have_insn_for (LSHIFTRT, mode)
7707 && mode_width <= HOST_BITS_PER_WIDE_INT
7708 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7710 new_rtx = gen_rtx_ASHIFTRT (mode,
7711 make_compound_operation (XEXP (x, 0),
7712 next_code),
7713 XEXP (x, 1));
7714 break;
7717 /* ... fall through ... */
7719 case ASHIFTRT:
7720 lhs = XEXP (x, 0);
7721 rhs = XEXP (x, 1);
7723 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7724 this is a SIGN_EXTRACT. */
7725 if (CONST_INT_P (rhs)
7726 && GET_CODE (lhs) == ASHIFT
7727 && CONST_INT_P (XEXP (lhs, 1))
7728 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7729 && INTVAL (XEXP (lhs, 1)) >= 0
7730 && INTVAL (rhs) < mode_width)
7732 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7733 new_rtx = make_extraction (mode, new_rtx,
7734 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7735 NULL_RTX, mode_width - INTVAL (rhs),
7736 code == LSHIFTRT, 0, in_code == COMPARE);
7737 break;
7740 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7741 If so, try to merge the shifts into a SIGN_EXTEND. We could
7742 also do this for some cases of SIGN_EXTRACT, but it doesn't
7743 seem worth the effort; the case checked for occurs on Alpha. */
7745 if (!OBJECT_P (lhs)
7746 && ! (GET_CODE (lhs) == SUBREG
7747 && (OBJECT_P (SUBREG_REG (lhs))))
7748 && CONST_INT_P (rhs)
7749 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7750 && INTVAL (rhs) < mode_width
7751 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7752 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7753 0, NULL_RTX, mode_width - INTVAL (rhs),
7754 code == LSHIFTRT, 0, in_code == COMPARE);
7756 break;
7758 case SUBREG:
7759 /* Call ourselves recursively on the inner expression. If we are
7760 narrowing the object and it has a different RTL code from
7761 what it originally did, do this SUBREG as a force_to_mode. */
7763 rtx inner = SUBREG_REG (x), simplified;
7764 enum rtx_code subreg_code = in_code;
7766 /* If in_code is COMPARE, it isn't always safe to pass it through
7767 to the recursive make_compound_operation call. */
7768 if (subreg_code == COMPARE
7769 && (!subreg_lowpart_p (x)
7770 || GET_CODE (inner) == SUBREG
7771 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7772 is (const_int 0), rather than
7773 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7774 || (GET_CODE (inner) == AND
7775 && CONST_INT_P (XEXP (inner, 1))
7776 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7777 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7778 >= GET_MODE_BITSIZE (mode))))
7779 subreg_code = SET;
7781 tem = make_compound_operation (inner, subreg_code);
7783 simplified
7784 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7785 if (simplified)
7786 tem = simplified;
7788 if (GET_CODE (tem) != GET_CODE (inner)
7789 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7790 && subreg_lowpart_p (x))
7792 rtx newer
7793 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7795 /* If we have something other than a SUBREG, we might have
7796 done an expansion, so rerun ourselves. */
7797 if (GET_CODE (newer) != SUBREG)
7798 newer = make_compound_operation (newer, in_code);
7800 /* force_to_mode can expand compounds. If it just re-expanded the
7801 compound, use gen_lowpart to convert to the desired mode. */
7802 if (rtx_equal_p (newer, x)
7803 /* Likewise if it re-expanded the compound only partially.
7804 This happens for SUBREG of ZERO_EXTRACT if they extract
7805 the same number of bits. */
7806 || (GET_CODE (newer) == SUBREG
7807 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7808 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7809 && GET_CODE (inner) == AND
7810 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7811 return gen_lowpart (GET_MODE (x), tem);
7813 return newer;
7816 if (simplified)
7817 return tem;
7819 break;
7821 default:
7822 break;
7825 if (new_rtx)
7827 x = gen_lowpart (mode, new_rtx);
7828 code = GET_CODE (x);
7831 /* Now recursively process each operand of this operation. We need to
7832 handle ZERO_EXTEND specially so that we don't lose track of the
7833 inner mode. */
7834 if (GET_CODE (x) == ZERO_EXTEND)
7836 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7837 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7838 new_rtx, GET_MODE (XEXP (x, 0)));
7839 if (tem)
7840 return tem;
7841 SUBST (XEXP (x, 0), new_rtx);
7842 return x;
7845 fmt = GET_RTX_FORMAT (code);
7846 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7847 if (fmt[i] == 'e')
7849 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7850 SUBST (XEXP (x, i), new_rtx);
7852 else if (fmt[i] == 'E')
7853 for (j = 0; j < XVECLEN (x, i); j++)
7855 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7856 SUBST (XVECEXP (x, i, j), new_rtx);
7859 maybe_swap:
7860 /* If this is a commutative operation, the changes to the operands
7861 may have made it noncanonical. */
7862 if (COMMUTATIVE_ARITH_P (x)
7863 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7865 tem = XEXP (x, 0);
7866 SUBST (XEXP (x, 0), XEXP (x, 1));
7867 SUBST (XEXP (x, 1), tem);
7870 return x;
7873 /* Given M see if it is a value that would select a field of bits
7874 within an item, but not the entire word. Return -1 if not.
7875 Otherwise, return the starting position of the field, where 0 is the
7876 low-order bit.
7878 *PLEN is set to the length of the field. */
7880 static int
7881 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7883 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7884 int pos = m ? ctz_hwi (m) : -1;
7885 int len = 0;
7887 if (pos >= 0)
7888 /* Now shift off the low-order zero bits and see if we have a
7889 power of two minus 1. */
7890 len = exact_log2 ((m >> pos) + 1);
7892 if (len <= 0)
7893 pos = -1;
7895 *plen = len;
7896 return pos;
7899 /* If X refers to a register that equals REG in value, replace these
7900 references with REG. */
7901 static rtx
7902 canon_reg_for_combine (rtx x, rtx reg)
7904 rtx op0, op1, op2;
7905 const char *fmt;
7906 int i;
7907 bool copied;
7909 enum rtx_code code = GET_CODE (x);
7910 switch (GET_RTX_CLASS (code))
7912 case RTX_UNARY:
7913 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7914 if (op0 != XEXP (x, 0))
7915 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7916 GET_MODE (reg));
7917 break;
7919 case RTX_BIN_ARITH:
7920 case RTX_COMM_ARITH:
7921 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7922 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7923 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7924 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7925 break;
7927 case RTX_COMPARE:
7928 case RTX_COMM_COMPARE:
7929 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7930 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7931 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7932 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7933 GET_MODE (op0), op0, op1);
7934 break;
7936 case RTX_TERNARY:
7937 case RTX_BITFIELD_OPS:
7938 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7939 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7940 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7941 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7942 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7943 GET_MODE (op0), op0, op1, op2);
7945 case RTX_OBJ:
7946 if (REG_P (x))
7948 if (rtx_equal_p (get_last_value (reg), x)
7949 || rtx_equal_p (reg, get_last_value (x)))
7950 return reg;
7951 else
7952 break;
7955 /* fall through */
7957 default:
7958 fmt = GET_RTX_FORMAT (code);
7959 copied = false;
7960 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7961 if (fmt[i] == 'e')
7963 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7964 if (op != XEXP (x, i))
7966 if (!copied)
7968 copied = true;
7969 x = copy_rtx (x);
7971 XEXP (x, i) = op;
7974 else if (fmt[i] == 'E')
7976 int j;
7977 for (j = 0; j < XVECLEN (x, i); j++)
7979 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7980 if (op != XVECEXP (x, i, j))
7982 if (!copied)
7984 copied = true;
7985 x = copy_rtx (x);
7987 XVECEXP (x, i, j) = op;
7992 break;
7995 return x;
7998 /* Return X converted to MODE. If the value is already truncated to
7999 MODE we can just return a subreg even though in the general case we
8000 would need an explicit truncation. */
8002 static rtx
8003 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
8005 if (!CONST_INT_P (x)
8006 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8007 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8008 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8010 /* Bit-cast X into an integer mode. */
8011 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8012 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8013 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8014 x, GET_MODE (x));
8017 return gen_lowpart (mode, x);
8020 /* See if X can be simplified knowing that we will only refer to it in
8021 MODE and will only refer to those bits that are nonzero in MASK.
8022 If other bits are being computed or if masking operations are done
8023 that select a superset of the bits in MASK, they can sometimes be
8024 ignored.
8026 Return a possibly simplified expression, but always convert X to
8027 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8029 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8030 are all off in X. This is used when X will be complemented, by either
8031 NOT, NEG, or XOR. */
8033 static rtx
8034 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
8035 int just_select)
8037 enum rtx_code code = GET_CODE (x);
8038 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8039 enum machine_mode op_mode;
8040 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8041 rtx op0, op1, temp;
8043 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8044 code below will do the wrong thing since the mode of such an
8045 expression is VOIDmode.
8047 Also do nothing if X is a CLOBBER; this can happen if X was
8048 the return value from a call to gen_lowpart. */
8049 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8050 return x;
8052 /* We want to perform the operation in its present mode unless we know
8053 that the operation is valid in MODE, in which case we do the operation
8054 in MODE. */
8055 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8056 && have_insn_for (code, mode))
8057 ? mode : GET_MODE (x));
8059 /* It is not valid to do a right-shift in a narrower mode
8060 than the one it came in with. */
8061 if ((code == LSHIFTRT || code == ASHIFTRT)
8062 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8063 op_mode = GET_MODE (x);
8065 /* Truncate MASK to fit OP_MODE. */
8066 if (op_mode)
8067 mask &= GET_MODE_MASK (op_mode);
8069 /* When we have an arithmetic operation, or a shift whose count we
8070 do not know, we need to assume that all bits up to the highest-order
8071 bit in MASK will be needed. This is how we form such a mask. */
8072 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8073 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8074 else
8075 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8076 - 1);
8078 /* Determine what bits of X are guaranteed to be (non)zero. */
8079 nonzero = nonzero_bits (x, mode);
8081 /* If none of the bits in X are needed, return a zero. */
8082 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8083 x = const0_rtx;
8085 /* If X is a CONST_INT, return a new one. Do this here since the
8086 test below will fail. */
8087 if (CONST_INT_P (x))
8089 if (SCALAR_INT_MODE_P (mode))
8090 return gen_int_mode (INTVAL (x) & mask, mode);
8091 else
8093 x = GEN_INT (INTVAL (x) & mask);
8094 return gen_lowpart_common (mode, x);
8098 /* If X is narrower than MODE and we want all the bits in X's mode, just
8099 get X in the proper mode. */
8100 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8101 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8102 return gen_lowpart (mode, x);
8104 /* We can ignore the effect of a SUBREG if it narrows the mode or
8105 if the constant masks to zero all the bits the mode doesn't have. */
8106 if (GET_CODE (x) == SUBREG
8107 && subreg_lowpart_p (x)
8108 && ((GET_MODE_SIZE (GET_MODE (x))
8109 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8110 || (0 == (mask
8111 & GET_MODE_MASK (GET_MODE (x))
8112 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8113 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8115 /* The arithmetic simplifications here only work for scalar integer modes. */
8116 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8117 return gen_lowpart_or_truncate (mode, x);
8119 switch (code)
8121 case CLOBBER:
8122 /* If X is a (clobber (const_int)), return it since we know we are
8123 generating something that won't match. */
8124 return x;
8126 case SIGN_EXTEND:
8127 case ZERO_EXTEND:
8128 case ZERO_EXTRACT:
8129 case SIGN_EXTRACT:
8130 x = expand_compound_operation (x);
8131 if (GET_CODE (x) != code)
8132 return force_to_mode (x, mode, mask, next_select);
8133 break;
8135 case TRUNCATE:
8136 /* Similarly for a truncate. */
8137 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8139 case AND:
8140 /* If this is an AND with a constant, convert it into an AND
8141 whose constant is the AND of that constant with MASK. If it
8142 remains an AND of MASK, delete it since it is redundant. */
8144 if (CONST_INT_P (XEXP (x, 1)))
8146 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8147 mask & INTVAL (XEXP (x, 1)));
8149 /* If X is still an AND, see if it is an AND with a mask that
8150 is just some low-order bits. If so, and it is MASK, we don't
8151 need it. */
8153 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8154 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8155 == mask))
8156 x = XEXP (x, 0);
8158 /* If it remains an AND, try making another AND with the bits
8159 in the mode mask that aren't in MASK turned on. If the
8160 constant in the AND is wide enough, this might make a
8161 cheaper constant. */
8163 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8164 && GET_MODE_MASK (GET_MODE (x)) != mask
8165 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8167 unsigned HOST_WIDE_INT cval
8168 = UINTVAL (XEXP (x, 1))
8169 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8170 rtx y;
8172 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8173 gen_int_mode (cval, GET_MODE (x)));
8174 if (set_src_cost (y, optimize_this_for_speed_p)
8175 < set_src_cost (x, optimize_this_for_speed_p))
8176 x = y;
8179 break;
8182 goto binop;
8184 case PLUS:
8185 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8186 low-order bits (as in an alignment operation) and FOO is already
8187 aligned to that boundary, mask C1 to that boundary as well.
8188 This may eliminate that PLUS and, later, the AND. */
8191 unsigned int width = GET_MODE_PRECISION (mode);
8192 unsigned HOST_WIDE_INT smask = mask;
8194 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8195 number, sign extend it. */
8197 if (width < HOST_BITS_PER_WIDE_INT
8198 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8199 smask |= HOST_WIDE_INT_M1U << width;
8201 if (CONST_INT_P (XEXP (x, 1))
8202 && exact_log2 (- smask) >= 0
8203 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8204 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8205 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8206 (INTVAL (XEXP (x, 1)) & smask)),
8207 mode, smask, next_select);
8210 /* ... fall through ... */
8212 case MULT:
8213 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8214 most significant bit in MASK since carries from those bits will
8215 affect the bits we are interested in. */
8216 mask = fuller_mask;
8217 goto binop;
8219 case MINUS:
8220 /* If X is (minus C Y) where C's least set bit is larger than any bit
8221 in the mask, then we may replace with (neg Y). */
8222 if (CONST_INT_P (XEXP (x, 0))
8223 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8225 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8226 GET_MODE (x));
8227 return force_to_mode (x, mode, mask, next_select);
8230 /* Similarly, if C contains every bit in the fuller_mask, then we may
8231 replace with (not Y). */
8232 if (CONST_INT_P (XEXP (x, 0))
8233 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8235 x = simplify_gen_unary (NOT, GET_MODE (x),
8236 XEXP (x, 1), GET_MODE (x));
8237 return force_to_mode (x, mode, mask, next_select);
8240 mask = fuller_mask;
8241 goto binop;
8243 case IOR:
8244 case XOR:
8245 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8246 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8247 operation which may be a bitfield extraction. Ensure that the
8248 constant we form is not wider than the mode of X. */
8250 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8251 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8252 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8253 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8254 && CONST_INT_P (XEXP (x, 1))
8255 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8256 + floor_log2 (INTVAL (XEXP (x, 1))))
8257 < GET_MODE_PRECISION (GET_MODE (x)))
8258 && (UINTVAL (XEXP (x, 1))
8259 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8261 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8262 << INTVAL (XEXP (XEXP (x, 0), 1)),
8263 GET_MODE (x));
8264 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8265 XEXP (XEXP (x, 0), 0), temp);
8266 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8267 XEXP (XEXP (x, 0), 1));
8268 return force_to_mode (x, mode, mask, next_select);
8271 binop:
8272 /* For most binary operations, just propagate into the operation and
8273 change the mode if we have an operation of that mode. */
8275 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8276 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8278 /* If we ended up truncating both operands, truncate the result of the
8279 operation instead. */
8280 if (GET_CODE (op0) == TRUNCATE
8281 && GET_CODE (op1) == TRUNCATE)
8283 op0 = XEXP (op0, 0);
8284 op1 = XEXP (op1, 0);
8287 op0 = gen_lowpart_or_truncate (op_mode, op0);
8288 op1 = gen_lowpart_or_truncate (op_mode, op1);
8290 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8291 x = simplify_gen_binary (code, op_mode, op0, op1);
8292 break;
8294 case ASHIFT:
8295 /* For left shifts, do the same, but just for the first operand.
8296 However, we cannot do anything with shifts where we cannot
8297 guarantee that the counts are smaller than the size of the mode
8298 because such a count will have a different meaning in a
8299 wider mode. */
8301 if (! (CONST_INT_P (XEXP (x, 1))
8302 && INTVAL (XEXP (x, 1)) >= 0
8303 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8304 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8305 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8306 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8307 break;
8309 /* If the shift count is a constant and we can do arithmetic in
8310 the mode of the shift, refine which bits we need. Otherwise, use the
8311 conservative form of the mask. */
8312 if (CONST_INT_P (XEXP (x, 1))
8313 && INTVAL (XEXP (x, 1)) >= 0
8314 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8315 && HWI_COMPUTABLE_MODE_P (op_mode))
8316 mask >>= INTVAL (XEXP (x, 1));
8317 else
8318 mask = fuller_mask;
8320 op0 = gen_lowpart_or_truncate (op_mode,
8321 force_to_mode (XEXP (x, 0), op_mode,
8322 mask, next_select));
8324 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8325 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8326 break;
8328 case LSHIFTRT:
8329 /* Here we can only do something if the shift count is a constant,
8330 this shift constant is valid for the host, and we can do arithmetic
8331 in OP_MODE. */
8333 if (CONST_INT_P (XEXP (x, 1))
8334 && INTVAL (XEXP (x, 1)) >= 0
8335 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8336 && HWI_COMPUTABLE_MODE_P (op_mode))
8338 rtx inner = XEXP (x, 0);
8339 unsigned HOST_WIDE_INT inner_mask;
8341 /* Select the mask of the bits we need for the shift operand. */
8342 inner_mask = mask << INTVAL (XEXP (x, 1));
8344 /* We can only change the mode of the shift if we can do arithmetic
8345 in the mode of the shift and INNER_MASK is no wider than the
8346 width of X's mode. */
8347 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8348 op_mode = GET_MODE (x);
8350 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8352 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8353 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8356 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8357 shift and AND produces only copies of the sign bit (C2 is one less
8358 than a power of two), we can do this with just a shift. */
8360 if (GET_CODE (x) == LSHIFTRT
8361 && CONST_INT_P (XEXP (x, 1))
8362 /* The shift puts one of the sign bit copies in the least significant
8363 bit. */
8364 && ((INTVAL (XEXP (x, 1))
8365 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8366 >= GET_MODE_PRECISION (GET_MODE (x)))
8367 && exact_log2 (mask + 1) >= 0
8368 /* Number of bits left after the shift must be more than the mask
8369 needs. */
8370 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8371 <= GET_MODE_PRECISION (GET_MODE (x)))
8372 /* Must be more sign bit copies than the mask needs. */
8373 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8374 >= exact_log2 (mask + 1)))
8375 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8376 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8377 - exact_log2 (mask + 1)));
8379 goto shiftrt;
8381 case ASHIFTRT:
8382 /* If we are just looking for the sign bit, we don't need this shift at
8383 all, even if it has a variable count. */
8384 if (val_signbit_p (GET_MODE (x), mask))
8385 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8387 /* If this is a shift by a constant, get a mask that contains those bits
8388 that are not copies of the sign bit. We then have two cases: If
8389 MASK only includes those bits, this can be a logical shift, which may
8390 allow simplifications. If MASK is a single-bit field not within
8391 those bits, we are requesting a copy of the sign bit and hence can
8392 shift the sign bit to the appropriate location. */
8394 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8395 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8397 int i;
8399 /* If the considered data is wider than HOST_WIDE_INT, we can't
8400 represent a mask for all its bits in a single scalar.
8401 But we only care about the lower bits, so calculate these. */
8403 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8405 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8407 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8408 is the number of bits a full-width mask would have set.
8409 We need only shift if these are fewer than nonzero can
8410 hold. If not, we must keep all bits set in nonzero. */
8412 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8413 < HOST_BITS_PER_WIDE_INT)
8414 nonzero >>= INTVAL (XEXP (x, 1))
8415 + HOST_BITS_PER_WIDE_INT
8416 - GET_MODE_PRECISION (GET_MODE (x)) ;
8418 else
8420 nonzero = GET_MODE_MASK (GET_MODE (x));
8421 nonzero >>= INTVAL (XEXP (x, 1));
8424 if ((mask & ~nonzero) == 0)
8426 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8427 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8428 if (GET_CODE (x) != ASHIFTRT)
8429 return force_to_mode (x, mode, mask, next_select);
8432 else if ((i = exact_log2 (mask)) >= 0)
8434 x = simplify_shift_const
8435 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8436 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8438 if (GET_CODE (x) != ASHIFTRT)
8439 return force_to_mode (x, mode, mask, next_select);
8443 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8444 even if the shift count isn't a constant. */
8445 if (mask == 1)
8446 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8447 XEXP (x, 0), XEXP (x, 1));
8449 shiftrt:
8451 /* If this is a zero- or sign-extension operation that just affects bits
8452 we don't care about, remove it. Be sure the call above returned
8453 something that is still a shift. */
8455 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8456 && CONST_INT_P (XEXP (x, 1))
8457 && INTVAL (XEXP (x, 1)) >= 0
8458 && (INTVAL (XEXP (x, 1))
8459 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8460 && GET_CODE (XEXP (x, 0)) == ASHIFT
8461 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8462 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8463 next_select);
8465 break;
8467 case ROTATE:
8468 case ROTATERT:
8469 /* If the shift count is constant and we can do computations
8470 in the mode of X, compute where the bits we care about are.
8471 Otherwise, we can't do anything. Don't change the mode of
8472 the shift or propagate MODE into the shift, though. */
8473 if (CONST_INT_P (XEXP (x, 1))
8474 && INTVAL (XEXP (x, 1)) >= 0)
8476 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8477 GET_MODE (x),
8478 gen_int_mode (mask, GET_MODE (x)),
8479 XEXP (x, 1));
8480 if (temp && CONST_INT_P (temp))
8481 x = simplify_gen_binary (code, GET_MODE (x),
8482 force_to_mode (XEXP (x, 0), GET_MODE (x),
8483 INTVAL (temp), next_select),
8484 XEXP (x, 1));
8486 break;
8488 case NEG:
8489 /* If we just want the low-order bit, the NEG isn't needed since it
8490 won't change the low-order bit. */
8491 if (mask == 1)
8492 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8494 /* We need any bits less significant than the most significant bit in
8495 MASK since carries from those bits will affect the bits we are
8496 interested in. */
8497 mask = fuller_mask;
8498 goto unop;
8500 case NOT:
8501 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8502 same as the XOR case above. Ensure that the constant we form is not
8503 wider than the mode of X. */
8505 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8506 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8507 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8508 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8509 < GET_MODE_PRECISION (GET_MODE (x)))
8510 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8512 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8513 GET_MODE (x));
8514 temp = simplify_gen_binary (XOR, GET_MODE (x),
8515 XEXP (XEXP (x, 0), 0), temp);
8516 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8517 temp, XEXP (XEXP (x, 0), 1));
8519 return force_to_mode (x, mode, mask, next_select);
8522 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8523 use the full mask inside the NOT. */
8524 mask = fuller_mask;
8526 unop:
8527 op0 = gen_lowpart_or_truncate (op_mode,
8528 force_to_mode (XEXP (x, 0), mode, mask,
8529 next_select));
8530 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8531 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8532 break;
8534 case NE:
8535 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8536 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8537 which is equal to STORE_FLAG_VALUE. */
8538 if ((mask & ~STORE_FLAG_VALUE) == 0
8539 && XEXP (x, 1) == const0_rtx
8540 && GET_MODE (XEXP (x, 0)) == mode
8541 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8542 && (nonzero_bits (XEXP (x, 0), mode)
8543 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8544 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8546 break;
8548 case IF_THEN_ELSE:
8549 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8550 written in a narrower mode. We play it safe and do not do so. */
8552 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8553 force_to_mode (XEXP (x, 1), mode,
8554 mask, next_select));
8555 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8556 force_to_mode (XEXP (x, 2), mode,
8557 mask, next_select));
8558 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8559 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8560 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8561 op0, op1);
8562 break;
8564 default:
8565 break;
8568 /* Ensure we return a value of the proper mode. */
8569 return gen_lowpart_or_truncate (mode, x);
8572 /* Return nonzero if X is an expression that has one of two values depending on
8573 whether some other value is zero or nonzero. In that case, we return the
8574 value that is being tested, *PTRUE is set to the value if the rtx being
8575 returned has a nonzero value, and *PFALSE is set to the other alternative.
8577 If we return zero, we set *PTRUE and *PFALSE to X. */
8579 static rtx
8580 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8582 enum machine_mode mode = GET_MODE (x);
8583 enum rtx_code code = GET_CODE (x);
8584 rtx cond0, cond1, true0, true1, false0, false1;
8585 unsigned HOST_WIDE_INT nz;
8587 /* If we are comparing a value against zero, we are done. */
8588 if ((code == NE || code == EQ)
8589 && XEXP (x, 1) == const0_rtx)
8591 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8592 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8593 return XEXP (x, 0);
8596 /* If this is a unary operation whose operand has one of two values, apply
8597 our opcode to compute those values. */
8598 else if (UNARY_P (x)
8599 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8601 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8602 *pfalse = simplify_gen_unary (code, mode, false0,
8603 GET_MODE (XEXP (x, 0)));
8604 return cond0;
8607 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8608 make can't possibly match and would suppress other optimizations. */
8609 else if (code == COMPARE)
8612 /* If this is a binary operation, see if either side has only one of two
8613 values. If either one does or if both do and they are conditional on
8614 the same value, compute the new true and false values. */
8615 else if (BINARY_P (x))
8617 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8618 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8620 if ((cond0 != 0 || cond1 != 0)
8621 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8623 /* If if_then_else_cond returned zero, then true/false are the
8624 same rtl. We must copy one of them to prevent invalid rtl
8625 sharing. */
8626 if (cond0 == 0)
8627 true0 = copy_rtx (true0);
8628 else if (cond1 == 0)
8629 true1 = copy_rtx (true1);
8631 if (COMPARISON_P (x))
8633 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8634 true0, true1);
8635 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8636 false0, false1);
8638 else
8640 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8641 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8644 return cond0 ? cond0 : cond1;
8647 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8648 operands is zero when the other is nonzero, and vice-versa,
8649 and STORE_FLAG_VALUE is 1 or -1. */
8651 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8652 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8653 || code == UMAX)
8654 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8656 rtx op0 = XEXP (XEXP (x, 0), 1);
8657 rtx op1 = XEXP (XEXP (x, 1), 1);
8659 cond0 = XEXP (XEXP (x, 0), 0);
8660 cond1 = XEXP (XEXP (x, 1), 0);
8662 if (COMPARISON_P (cond0)
8663 && COMPARISON_P (cond1)
8664 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8665 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8666 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8667 || ((swap_condition (GET_CODE (cond0))
8668 == reversed_comparison_code (cond1, NULL))
8669 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8670 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8671 && ! side_effects_p (x))
8673 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8674 *pfalse = simplify_gen_binary (MULT, mode,
8675 (code == MINUS
8676 ? simplify_gen_unary (NEG, mode,
8677 op1, mode)
8678 : op1),
8679 const_true_rtx);
8680 return cond0;
8684 /* Similarly for MULT, AND and UMIN, except that for these the result
8685 is always zero. */
8686 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8687 && (code == MULT || code == AND || code == UMIN)
8688 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8690 cond0 = XEXP (XEXP (x, 0), 0);
8691 cond1 = XEXP (XEXP (x, 1), 0);
8693 if (COMPARISON_P (cond0)
8694 && COMPARISON_P (cond1)
8695 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8696 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8697 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8698 || ((swap_condition (GET_CODE (cond0))
8699 == reversed_comparison_code (cond1, NULL))
8700 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8701 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8702 && ! side_effects_p (x))
8704 *ptrue = *pfalse = const0_rtx;
8705 return cond0;
8710 else if (code == IF_THEN_ELSE)
8712 /* If we have IF_THEN_ELSE already, extract the condition and
8713 canonicalize it if it is NE or EQ. */
8714 cond0 = XEXP (x, 0);
8715 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8716 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8717 return XEXP (cond0, 0);
8718 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8720 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8721 return XEXP (cond0, 0);
8723 else
8724 return cond0;
8727 /* If X is a SUBREG, we can narrow both the true and false values
8728 if the inner expression, if there is a condition. */
8729 else if (code == SUBREG
8730 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8731 &true0, &false0)))
8733 true0 = simplify_gen_subreg (mode, true0,
8734 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8735 false0 = simplify_gen_subreg (mode, false0,
8736 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8737 if (true0 && false0)
8739 *ptrue = true0;
8740 *pfalse = false0;
8741 return cond0;
8745 /* If X is a constant, this isn't special and will cause confusions
8746 if we treat it as such. Likewise if it is equivalent to a constant. */
8747 else if (CONSTANT_P (x)
8748 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8751 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8752 will be least confusing to the rest of the compiler. */
8753 else if (mode == BImode)
8755 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8756 return x;
8759 /* If X is known to be either 0 or -1, those are the true and
8760 false values when testing X. */
8761 else if (x == constm1_rtx || x == const0_rtx
8762 || (mode != VOIDmode
8763 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8765 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8766 return x;
8769 /* Likewise for 0 or a single bit. */
8770 else if (HWI_COMPUTABLE_MODE_P (mode)
8771 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8773 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8774 return x;
8777 /* Otherwise fail; show no condition with true and false values the same. */
8778 *ptrue = *pfalse = x;
8779 return 0;
8782 /* Return the value of expression X given the fact that condition COND
8783 is known to be true when applied to REG as its first operand and VAL
8784 as its second. X is known to not be shared and so can be modified in
8785 place.
8787 We only handle the simplest cases, and specifically those cases that
8788 arise with IF_THEN_ELSE expressions. */
8790 static rtx
8791 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8793 enum rtx_code code = GET_CODE (x);
8794 rtx temp;
8795 const char *fmt;
8796 int i, j;
8798 if (side_effects_p (x))
8799 return x;
8801 /* If either operand of the condition is a floating point value,
8802 then we have to avoid collapsing an EQ comparison. */
8803 if (cond == EQ
8804 && rtx_equal_p (x, reg)
8805 && ! FLOAT_MODE_P (GET_MODE (x))
8806 && ! FLOAT_MODE_P (GET_MODE (val)))
8807 return val;
8809 if (cond == UNEQ && rtx_equal_p (x, reg))
8810 return val;
8812 /* If X is (abs REG) and we know something about REG's relationship
8813 with zero, we may be able to simplify this. */
8815 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8816 switch (cond)
8818 case GE: case GT: case EQ:
8819 return XEXP (x, 0);
8820 case LT: case LE:
8821 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8822 XEXP (x, 0),
8823 GET_MODE (XEXP (x, 0)));
8824 default:
8825 break;
8828 /* The only other cases we handle are MIN, MAX, and comparisons if the
8829 operands are the same as REG and VAL. */
8831 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8833 if (rtx_equal_p (XEXP (x, 0), val))
8834 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8836 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8838 if (COMPARISON_P (x))
8840 if (comparison_dominates_p (cond, code))
8841 return const_true_rtx;
8843 code = reversed_comparison_code (x, NULL);
8844 if (code != UNKNOWN
8845 && comparison_dominates_p (cond, code))
8846 return const0_rtx;
8847 else
8848 return x;
8850 else if (code == SMAX || code == SMIN
8851 || code == UMIN || code == UMAX)
8853 int unsignedp = (code == UMIN || code == UMAX);
8855 /* Do not reverse the condition when it is NE or EQ.
8856 This is because we cannot conclude anything about
8857 the value of 'SMAX (x, y)' when x is not equal to y,
8858 but we can when x equals y. */
8859 if ((code == SMAX || code == UMAX)
8860 && ! (cond == EQ || cond == NE))
8861 cond = reverse_condition (cond);
8863 switch (cond)
8865 case GE: case GT:
8866 return unsignedp ? x : XEXP (x, 1);
8867 case LE: case LT:
8868 return unsignedp ? x : XEXP (x, 0);
8869 case GEU: case GTU:
8870 return unsignedp ? XEXP (x, 1) : x;
8871 case LEU: case LTU:
8872 return unsignedp ? XEXP (x, 0) : x;
8873 default:
8874 break;
8879 else if (code == SUBREG)
8881 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8882 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8884 if (SUBREG_REG (x) != r)
8886 /* We must simplify subreg here, before we lose track of the
8887 original inner_mode. */
8888 new_rtx = simplify_subreg (GET_MODE (x), r,
8889 inner_mode, SUBREG_BYTE (x));
8890 if (new_rtx)
8891 return new_rtx;
8892 else
8893 SUBST (SUBREG_REG (x), r);
8896 return x;
8898 /* We don't have to handle SIGN_EXTEND here, because even in the
8899 case of replacing something with a modeless CONST_INT, a
8900 CONST_INT is already (supposed to be) a valid sign extension for
8901 its narrower mode, which implies it's already properly
8902 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8903 story is different. */
8904 else if (code == ZERO_EXTEND)
8906 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8907 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8909 if (XEXP (x, 0) != r)
8911 /* We must simplify the zero_extend here, before we lose
8912 track of the original inner_mode. */
8913 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8914 r, inner_mode);
8915 if (new_rtx)
8916 return new_rtx;
8917 else
8918 SUBST (XEXP (x, 0), r);
8921 return x;
8924 fmt = GET_RTX_FORMAT (code);
8925 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8927 if (fmt[i] == 'e')
8928 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8929 else if (fmt[i] == 'E')
8930 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8931 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8932 cond, reg, val));
8935 return x;
8938 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8939 assignment as a field assignment. */
8941 static int
8942 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8944 if (x == y || rtx_equal_p (x, y))
8945 return 1;
8947 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8948 return 0;
8950 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8951 Note that all SUBREGs of MEM are paradoxical; otherwise they
8952 would have been rewritten. */
8953 if (MEM_P (x) && GET_CODE (y) == SUBREG
8954 && MEM_P (SUBREG_REG (y))
8955 && rtx_equal_p (SUBREG_REG (y),
8956 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8957 return 1;
8959 if (MEM_P (y) && GET_CODE (x) == SUBREG
8960 && MEM_P (SUBREG_REG (x))
8961 && rtx_equal_p (SUBREG_REG (x),
8962 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8963 return 1;
8965 /* We used to see if get_last_value of X and Y were the same but that's
8966 not correct. In one direction, we'll cause the assignment to have
8967 the wrong destination and in the case, we'll import a register into this
8968 insn that might have already have been dead. So fail if none of the
8969 above cases are true. */
8970 return 0;
8973 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8974 Return that assignment if so.
8976 We only handle the most common cases. */
8978 static rtx
8979 make_field_assignment (rtx x)
8981 rtx dest = SET_DEST (x);
8982 rtx src = SET_SRC (x);
8983 rtx assign;
8984 rtx rhs, lhs;
8985 HOST_WIDE_INT c1;
8986 HOST_WIDE_INT pos;
8987 unsigned HOST_WIDE_INT len;
8988 rtx other;
8989 enum machine_mode mode;
8991 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8992 a clear of a one-bit field. We will have changed it to
8993 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8994 for a SUBREG. */
8996 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8997 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8998 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8999 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9001 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9002 1, 1, 1, 0);
9003 if (assign != 0)
9004 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9005 return x;
9008 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9009 && subreg_lowpart_p (XEXP (src, 0))
9010 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9011 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9012 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9013 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9014 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9015 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9017 assign = make_extraction (VOIDmode, dest, 0,
9018 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9019 1, 1, 1, 0);
9020 if (assign != 0)
9021 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9022 return x;
9025 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9026 one-bit field. */
9027 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9028 && XEXP (XEXP (src, 0), 0) == const1_rtx
9029 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9031 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9032 1, 1, 1, 0);
9033 if (assign != 0)
9034 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9035 return x;
9038 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9039 SRC is an AND with all bits of that field set, then we can discard
9040 the AND. */
9041 if (GET_CODE (dest) == ZERO_EXTRACT
9042 && CONST_INT_P (XEXP (dest, 1))
9043 && GET_CODE (src) == AND
9044 && CONST_INT_P (XEXP (src, 1)))
9046 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9047 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9048 unsigned HOST_WIDE_INT ze_mask;
9050 if (width >= HOST_BITS_PER_WIDE_INT)
9051 ze_mask = -1;
9052 else
9053 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9055 /* Complete overlap. We can remove the source AND. */
9056 if ((and_mask & ze_mask) == ze_mask)
9057 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9059 /* Partial overlap. We can reduce the source AND. */
9060 if ((and_mask & ze_mask) != and_mask)
9062 mode = GET_MODE (src);
9063 src = gen_rtx_AND (mode, XEXP (src, 0),
9064 gen_int_mode (and_mask & ze_mask, mode));
9065 return gen_rtx_SET (VOIDmode, dest, src);
9069 /* The other case we handle is assignments into a constant-position
9070 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9071 a mask that has all one bits except for a group of zero bits and
9072 OTHER is known to have zeros where C1 has ones, this is such an
9073 assignment. Compute the position and length from C1. Shift OTHER
9074 to the appropriate position, force it to the required mode, and
9075 make the extraction. Check for the AND in both operands. */
9077 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9078 return x;
9080 rhs = expand_compound_operation (XEXP (src, 0));
9081 lhs = expand_compound_operation (XEXP (src, 1));
9083 if (GET_CODE (rhs) == AND
9084 && CONST_INT_P (XEXP (rhs, 1))
9085 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9086 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9087 else if (GET_CODE (lhs) == AND
9088 && CONST_INT_P (XEXP (lhs, 1))
9089 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9090 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9091 else
9092 return x;
9094 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9095 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9096 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9097 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9098 return x;
9100 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9101 if (assign == 0)
9102 return x;
9104 /* The mode to use for the source is the mode of the assignment, or of
9105 what is inside a possible STRICT_LOW_PART. */
9106 mode = (GET_CODE (assign) == STRICT_LOW_PART
9107 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9109 /* Shift OTHER right POS places and make it the source, restricting it
9110 to the proper length and mode. */
9112 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9113 GET_MODE (src),
9114 other, pos),
9115 dest);
9116 src = force_to_mode (src, mode,
9117 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9118 ? ~(unsigned HOST_WIDE_INT) 0
9119 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9122 /* If SRC is masked by an AND that does not make a difference in
9123 the value being stored, strip it. */
9124 if (GET_CODE (assign) == ZERO_EXTRACT
9125 && CONST_INT_P (XEXP (assign, 1))
9126 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9127 && GET_CODE (src) == AND
9128 && CONST_INT_P (XEXP (src, 1))
9129 && UINTVAL (XEXP (src, 1))
9130 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9131 src = XEXP (src, 0);
9133 return gen_rtx_SET (VOIDmode, assign, src);
9136 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9137 if so. */
9139 static rtx
9140 apply_distributive_law (rtx x)
9142 enum rtx_code code = GET_CODE (x);
9143 enum rtx_code inner_code;
9144 rtx lhs, rhs, other;
9145 rtx tem;
9147 /* Distributivity is not true for floating point as it can change the
9148 value. So we don't do it unless -funsafe-math-optimizations. */
9149 if (FLOAT_MODE_P (GET_MODE (x))
9150 && ! flag_unsafe_math_optimizations)
9151 return x;
9153 /* The outer operation can only be one of the following: */
9154 if (code != IOR && code != AND && code != XOR
9155 && code != PLUS && code != MINUS)
9156 return x;
9158 lhs = XEXP (x, 0);
9159 rhs = XEXP (x, 1);
9161 /* If either operand is a primitive we can't do anything, so get out
9162 fast. */
9163 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9164 return x;
9166 lhs = expand_compound_operation (lhs);
9167 rhs = expand_compound_operation (rhs);
9168 inner_code = GET_CODE (lhs);
9169 if (inner_code != GET_CODE (rhs))
9170 return x;
9172 /* See if the inner and outer operations distribute. */
9173 switch (inner_code)
9175 case LSHIFTRT:
9176 case ASHIFTRT:
9177 case AND:
9178 case IOR:
9179 /* These all distribute except over PLUS. */
9180 if (code == PLUS || code == MINUS)
9181 return x;
9182 break;
9184 case MULT:
9185 if (code != PLUS && code != MINUS)
9186 return x;
9187 break;
9189 case ASHIFT:
9190 /* This is also a multiply, so it distributes over everything. */
9191 break;
9193 /* This used to handle SUBREG, but this turned out to be counter-
9194 productive, since (subreg (op ...)) usually is not handled by
9195 insn patterns, and this "optimization" therefore transformed
9196 recognizable patterns into unrecognizable ones. Therefore the
9197 SUBREG case was removed from here.
9199 It is possible that distributing SUBREG over arithmetic operations
9200 leads to an intermediate result than can then be optimized further,
9201 e.g. by moving the outer SUBREG to the other side of a SET as done
9202 in simplify_set. This seems to have been the original intent of
9203 handling SUBREGs here.
9205 However, with current GCC this does not appear to actually happen,
9206 at least on major platforms. If some case is found where removing
9207 the SUBREG case here prevents follow-on optimizations, distributing
9208 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9210 default:
9211 return x;
9214 /* Set LHS and RHS to the inner operands (A and B in the example
9215 above) and set OTHER to the common operand (C in the example).
9216 There is only one way to do this unless the inner operation is
9217 commutative. */
9218 if (COMMUTATIVE_ARITH_P (lhs)
9219 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9220 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9221 else if (COMMUTATIVE_ARITH_P (lhs)
9222 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9223 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9224 else if (COMMUTATIVE_ARITH_P (lhs)
9225 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9226 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9227 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9228 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9229 else
9230 return x;
9232 /* Form the new inner operation, seeing if it simplifies first. */
9233 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9235 /* There is one exception to the general way of distributing:
9236 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9237 if (code == XOR && inner_code == IOR)
9239 inner_code = AND;
9240 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9243 /* We may be able to continuing distributing the result, so call
9244 ourselves recursively on the inner operation before forming the
9245 outer operation, which we return. */
9246 return simplify_gen_binary (inner_code, GET_MODE (x),
9247 apply_distributive_law (tem), other);
9250 /* See if X is of the form (* (+ A B) C), and if so convert to
9251 (+ (* A C) (* B C)) and try to simplify.
9253 Most of the time, this results in no change. However, if some of
9254 the operands are the same or inverses of each other, simplifications
9255 will result.
9257 For example, (and (ior A B) (not B)) can occur as the result of
9258 expanding a bit field assignment. When we apply the distributive
9259 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9260 which then simplifies to (and (A (not B))).
9262 Note that no checks happen on the validity of applying the inverse
9263 distributive law. This is pointless since we can do it in the
9264 few places where this routine is called.
9266 N is the index of the term that is decomposed (the arithmetic operation,
9267 i.e. (+ A B) in the first example above). !N is the index of the term that
9268 is distributed, i.e. of C in the first example above. */
9269 static rtx
9270 distribute_and_simplify_rtx (rtx x, int n)
9272 enum machine_mode mode;
9273 enum rtx_code outer_code, inner_code;
9274 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9276 /* Distributivity is not true for floating point as it can change the
9277 value. So we don't do it unless -funsafe-math-optimizations. */
9278 if (FLOAT_MODE_P (GET_MODE (x))
9279 && ! flag_unsafe_math_optimizations)
9280 return NULL_RTX;
9282 decomposed = XEXP (x, n);
9283 if (!ARITHMETIC_P (decomposed))
9284 return NULL_RTX;
9286 mode = GET_MODE (x);
9287 outer_code = GET_CODE (x);
9288 distributed = XEXP (x, !n);
9290 inner_code = GET_CODE (decomposed);
9291 inner_op0 = XEXP (decomposed, 0);
9292 inner_op1 = XEXP (decomposed, 1);
9294 /* Special case (and (xor B C) (not A)), which is equivalent to
9295 (xor (ior A B) (ior A C)) */
9296 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9298 distributed = XEXP (distributed, 0);
9299 outer_code = IOR;
9302 if (n == 0)
9304 /* Distribute the second term. */
9305 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9306 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9308 else
9310 /* Distribute the first term. */
9311 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9312 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9315 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9316 new_op0, new_op1));
9317 if (GET_CODE (tmp) != outer_code
9318 && (set_src_cost (tmp, optimize_this_for_speed_p)
9319 < set_src_cost (x, optimize_this_for_speed_p)))
9320 return tmp;
9322 return NULL_RTX;
9325 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9326 in MODE. Return an equivalent form, if different from (and VAROP
9327 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9329 static rtx
9330 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9331 unsigned HOST_WIDE_INT constop)
9333 unsigned HOST_WIDE_INT nonzero;
9334 unsigned HOST_WIDE_INT orig_constop;
9335 rtx orig_varop;
9336 int i;
9338 orig_varop = varop;
9339 orig_constop = constop;
9340 if (GET_CODE (varop) == CLOBBER)
9341 return NULL_RTX;
9343 /* Simplify VAROP knowing that we will be only looking at some of the
9344 bits in it.
9346 Note by passing in CONSTOP, we guarantee that the bits not set in
9347 CONSTOP are not significant and will never be examined. We must
9348 ensure that is the case by explicitly masking out those bits
9349 before returning. */
9350 varop = force_to_mode (varop, mode, constop, 0);
9352 /* If VAROP is a CLOBBER, we will fail so return it. */
9353 if (GET_CODE (varop) == CLOBBER)
9354 return varop;
9356 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9357 to VAROP and return the new constant. */
9358 if (CONST_INT_P (varop))
9359 return gen_int_mode (INTVAL (varop) & constop, mode);
9361 /* See what bits may be nonzero in VAROP. Unlike the general case of
9362 a call to nonzero_bits, here we don't care about bits outside
9363 MODE. */
9365 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9367 /* Turn off all bits in the constant that are known to already be zero.
9368 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9369 which is tested below. */
9371 constop &= nonzero;
9373 /* If we don't have any bits left, return zero. */
9374 if (constop == 0)
9375 return const0_rtx;
9377 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9378 a power of two, we can replace this with an ASHIFT. */
9379 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9380 && (i = exact_log2 (constop)) >= 0)
9381 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9383 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9384 or XOR, then try to apply the distributive law. This may eliminate
9385 operations if either branch can be simplified because of the AND.
9386 It may also make some cases more complex, but those cases probably
9387 won't match a pattern either with or without this. */
9389 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9390 return
9391 gen_lowpart
9392 (mode,
9393 apply_distributive_law
9394 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9395 simplify_and_const_int (NULL_RTX,
9396 GET_MODE (varop),
9397 XEXP (varop, 0),
9398 constop),
9399 simplify_and_const_int (NULL_RTX,
9400 GET_MODE (varop),
9401 XEXP (varop, 1),
9402 constop))));
9404 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9405 the AND and see if one of the operands simplifies to zero. If so, we
9406 may eliminate it. */
9408 if (GET_CODE (varop) == PLUS
9409 && exact_log2 (constop + 1) >= 0)
9411 rtx o0, o1;
9413 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9414 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9415 if (o0 == const0_rtx)
9416 return o1;
9417 if (o1 == const0_rtx)
9418 return o0;
9421 /* Make a SUBREG if necessary. If we can't make it, fail. */
9422 varop = gen_lowpart (mode, varop);
9423 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9424 return NULL_RTX;
9426 /* If we are only masking insignificant bits, return VAROP. */
9427 if (constop == nonzero)
9428 return varop;
9430 if (varop == orig_varop && constop == orig_constop)
9431 return NULL_RTX;
9433 /* Otherwise, return an AND. */
9434 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9438 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9439 in MODE.
9441 Return an equivalent form, if different from X. Otherwise, return X. If
9442 X is zero, we are to always construct the equivalent form. */
9444 static rtx
9445 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9446 unsigned HOST_WIDE_INT constop)
9448 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9449 if (tem)
9450 return tem;
9452 if (!x)
9453 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9454 gen_int_mode (constop, mode));
9455 if (GET_MODE (x) != mode)
9456 x = gen_lowpart (mode, x);
9457 return x;
9460 /* Given a REG, X, compute which bits in X can be nonzero.
9461 We don't care about bits outside of those defined in MODE.
9463 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9464 a shift, AND, or zero_extract, we can do better. */
9466 static rtx
9467 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9468 const_rtx known_x ATTRIBUTE_UNUSED,
9469 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9470 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9471 unsigned HOST_WIDE_INT *nonzero)
9473 rtx tem;
9474 reg_stat_type *rsp;
9476 /* If X is a register whose nonzero bits value is current, use it.
9477 Otherwise, if X is a register whose value we can find, use that
9478 value. Otherwise, use the previously-computed global nonzero bits
9479 for this register. */
9481 rsp = &reg_stat[REGNO (x)];
9482 if (rsp->last_set_value != 0
9483 && (rsp->last_set_mode == mode
9484 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9485 && GET_MODE_CLASS (mode) == MODE_INT))
9486 && ((rsp->last_set_label >= label_tick_ebb_start
9487 && rsp->last_set_label < label_tick)
9488 || (rsp->last_set_label == label_tick
9489 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9490 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9491 && REG_N_SETS (REGNO (x)) == 1
9492 && !REGNO_REG_SET_P
9493 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9494 REGNO (x)))))
9496 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9498 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9499 /* We don't know anything about the upper bits. */
9500 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9502 *nonzero &= mask;
9503 return NULL;
9506 tem = get_last_value (x);
9508 if (tem)
9510 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9511 /* If X is narrower than MODE and TEM is a non-negative
9512 constant that would appear negative in the mode of X,
9513 sign-extend it for use in reg_nonzero_bits because some
9514 machines (maybe most) will actually do the sign-extension
9515 and this is the conservative approach.
9517 ??? For 2.5, try to tighten up the MD files in this regard
9518 instead of this kludge. */
9520 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9521 && CONST_INT_P (tem)
9522 && INTVAL (tem) > 0
9523 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9524 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9525 #endif
9526 return tem;
9528 else if (nonzero_sign_valid && rsp->nonzero_bits)
9530 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9532 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9533 /* We don't know anything about the upper bits. */
9534 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9536 *nonzero &= mask;
9539 return NULL;
9542 /* Return the number of bits at the high-order end of X that are known to
9543 be equal to the sign bit. X will be used in mode MODE; if MODE is
9544 VOIDmode, X will be used in its own mode. The returned value will always
9545 be between 1 and the number of bits in MODE. */
9547 static rtx
9548 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9549 const_rtx known_x ATTRIBUTE_UNUSED,
9550 enum machine_mode known_mode
9551 ATTRIBUTE_UNUSED,
9552 unsigned int known_ret ATTRIBUTE_UNUSED,
9553 unsigned int *result)
9555 rtx tem;
9556 reg_stat_type *rsp;
9558 rsp = &reg_stat[REGNO (x)];
9559 if (rsp->last_set_value != 0
9560 && rsp->last_set_mode == mode
9561 && ((rsp->last_set_label >= label_tick_ebb_start
9562 && rsp->last_set_label < label_tick)
9563 || (rsp->last_set_label == label_tick
9564 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9565 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9566 && REG_N_SETS (REGNO (x)) == 1
9567 && !REGNO_REG_SET_P
9568 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9569 REGNO (x)))))
9571 *result = rsp->last_set_sign_bit_copies;
9572 return NULL;
9575 tem = get_last_value (x);
9576 if (tem != 0)
9577 return tem;
9579 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9580 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9581 *result = rsp->sign_bit_copies;
9583 return NULL;
9586 /* Return the number of "extended" bits there are in X, when interpreted
9587 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9588 unsigned quantities, this is the number of high-order zero bits.
9589 For signed quantities, this is the number of copies of the sign bit
9590 minus 1. In both case, this function returns the number of "spare"
9591 bits. For example, if two quantities for which this function returns
9592 at least 1 are added, the addition is known not to overflow.
9594 This function will always return 0 unless called during combine, which
9595 implies that it must be called from a define_split. */
9597 unsigned int
9598 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9600 if (nonzero_sign_valid == 0)
9601 return 0;
9603 return (unsignedp
9604 ? (HWI_COMPUTABLE_MODE_P (mode)
9605 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9606 - floor_log2 (nonzero_bits (x, mode)))
9607 : 0)
9608 : num_sign_bit_copies (x, mode) - 1);
9611 /* This function is called from `simplify_shift_const' to merge two
9612 outer operations. Specifically, we have already found that we need
9613 to perform operation *POP0 with constant *PCONST0 at the outermost
9614 position. We would now like to also perform OP1 with constant CONST1
9615 (with *POP0 being done last).
9617 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9618 the resulting operation. *PCOMP_P is set to 1 if we would need to
9619 complement the innermost operand, otherwise it is unchanged.
9621 MODE is the mode in which the operation will be done. No bits outside
9622 the width of this mode matter. It is assumed that the width of this mode
9623 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9625 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9626 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9627 result is simply *PCONST0.
9629 If the resulting operation cannot be expressed as one operation, we
9630 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9632 static int
9633 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)
9635 enum rtx_code op0 = *pop0;
9636 HOST_WIDE_INT const0 = *pconst0;
9638 const0 &= GET_MODE_MASK (mode);
9639 const1 &= GET_MODE_MASK (mode);
9641 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9642 if (op0 == AND)
9643 const1 &= const0;
9645 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9646 if OP0 is SET. */
9648 if (op1 == UNKNOWN || op0 == SET)
9649 return 1;
9651 else if (op0 == UNKNOWN)
9652 op0 = op1, const0 = const1;
9654 else if (op0 == op1)
9656 switch (op0)
9658 case AND:
9659 const0 &= const1;
9660 break;
9661 case IOR:
9662 const0 |= const1;
9663 break;
9664 case XOR:
9665 const0 ^= const1;
9666 break;
9667 case PLUS:
9668 const0 += const1;
9669 break;
9670 case NEG:
9671 op0 = UNKNOWN;
9672 break;
9673 default:
9674 break;
9678 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9679 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9680 return 0;
9682 /* If the two constants aren't the same, we can't do anything. The
9683 remaining six cases can all be done. */
9684 else if (const0 != const1)
9685 return 0;
9687 else
9688 switch (op0)
9690 case IOR:
9691 if (op1 == AND)
9692 /* (a & b) | b == b */
9693 op0 = SET;
9694 else /* op1 == XOR */
9695 /* (a ^ b) | b == a | b */
9697 break;
9699 case XOR:
9700 if (op1 == AND)
9701 /* (a & b) ^ b == (~a) & b */
9702 op0 = AND, *pcomp_p = 1;
9703 else /* op1 == IOR */
9704 /* (a | b) ^ b == a & ~b */
9705 op0 = AND, const0 = ~const0;
9706 break;
9708 case AND:
9709 if (op1 == IOR)
9710 /* (a | b) & b == b */
9711 op0 = SET;
9712 else /* op1 == XOR */
9713 /* (a ^ b) & b) == (~a) & b */
9714 *pcomp_p = 1;
9715 break;
9716 default:
9717 break;
9720 /* Check for NO-OP cases. */
9721 const0 &= GET_MODE_MASK (mode);
9722 if (const0 == 0
9723 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9724 op0 = UNKNOWN;
9725 else if (const0 == 0 && op0 == AND)
9726 op0 = SET;
9727 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9728 && op0 == AND)
9729 op0 = UNKNOWN;
9731 *pop0 = op0;
9733 /* ??? Slightly redundant with the above mask, but not entirely.
9734 Moving this above means we'd have to sign-extend the mode mask
9735 for the final test. */
9736 if (op0 != UNKNOWN && op0 != NEG)
9737 *pconst0 = trunc_int_for_mode (const0, mode);
9739 return 1;
9742 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9743 the shift in. The original shift operation CODE is performed on OP in
9744 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9745 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9746 result of the shift is subject to operation OUTER_CODE with operand
9747 OUTER_CONST. */
9749 static enum machine_mode
9750 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9751 enum machine_mode orig_mode, enum machine_mode mode,
9752 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9754 if (orig_mode == mode)
9755 return mode;
9756 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9758 /* In general we can't perform in wider mode for right shift and rotate. */
9759 switch (code)
9761 case ASHIFTRT:
9762 /* We can still widen if the bits brought in from the left are identical
9763 to the sign bit of ORIG_MODE. */
9764 if (num_sign_bit_copies (op, mode)
9765 > (unsigned) (GET_MODE_PRECISION (mode)
9766 - GET_MODE_PRECISION (orig_mode)))
9767 return mode;
9768 return orig_mode;
9770 case LSHIFTRT:
9771 /* Similarly here but with zero bits. */
9772 if (HWI_COMPUTABLE_MODE_P (mode)
9773 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9774 return mode;
9776 /* We can also widen if the bits brought in will be masked off. This
9777 operation is performed in ORIG_MODE. */
9778 if (outer_code == AND)
9780 int care_bits = low_bitmask_len (orig_mode, outer_const);
9782 if (care_bits >= 0
9783 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9784 return mode;
9786 /* fall through */
9788 case ROTATE:
9789 return orig_mode;
9791 case ROTATERT:
9792 gcc_unreachable ();
9794 default:
9795 return mode;
9799 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9800 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9801 if we cannot simplify it. Otherwise, return a simplified value.
9803 The shift is normally computed in the widest mode we find in VAROP, as
9804 long as it isn't a different number of words than RESULT_MODE. Exceptions
9805 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9807 static rtx
9808 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9809 rtx varop, int orig_count)
9811 enum rtx_code orig_code = code;
9812 rtx orig_varop = varop;
9813 int count;
9814 enum machine_mode mode = result_mode;
9815 enum machine_mode shift_mode, tmode;
9816 unsigned int mode_words
9817 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9818 /* We form (outer_op (code varop count) (outer_const)). */
9819 enum rtx_code outer_op = UNKNOWN;
9820 HOST_WIDE_INT outer_const = 0;
9821 int complement_p = 0;
9822 rtx new_rtx, x;
9824 /* Make sure and truncate the "natural" shift on the way in. We don't
9825 want to do this inside the loop as it makes it more difficult to
9826 combine shifts. */
9827 if (SHIFT_COUNT_TRUNCATED)
9828 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9830 /* If we were given an invalid count, don't do anything except exactly
9831 what was requested. */
9833 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9834 return NULL_RTX;
9836 count = orig_count;
9838 /* Unless one of the branches of the `if' in this loop does a `continue',
9839 we will `break' the loop after the `if'. */
9841 while (count != 0)
9843 /* If we have an operand of (clobber (const_int 0)), fail. */
9844 if (GET_CODE (varop) == CLOBBER)
9845 return NULL_RTX;
9847 /* Convert ROTATERT to ROTATE. */
9848 if (code == ROTATERT)
9850 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9851 code = ROTATE;
9852 if (VECTOR_MODE_P (result_mode))
9853 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9854 else
9855 count = bitsize - count;
9858 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9859 mode, outer_op, outer_const);
9861 /* Handle cases where the count is greater than the size of the mode
9862 minus 1. For ASHIFT, use the size minus one as the count (this can
9863 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9864 take the count modulo the size. For other shifts, the result is
9865 zero.
9867 Since these shifts are being produced by the compiler by combining
9868 multiple operations, each of which are defined, we know what the
9869 result is supposed to be. */
9871 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
9873 if (code == ASHIFTRT)
9874 count = GET_MODE_PRECISION (shift_mode) - 1;
9875 else if (code == ROTATE || code == ROTATERT)
9876 count %= GET_MODE_PRECISION (shift_mode);
9877 else
9879 /* We can't simply return zero because there may be an
9880 outer op. */
9881 varop = const0_rtx;
9882 count = 0;
9883 break;
9887 /* If we discovered we had to complement VAROP, leave. Making a NOT
9888 here would cause an infinite loop. */
9889 if (complement_p)
9890 break;
9892 /* An arithmetic right shift of a quantity known to be -1 or 0
9893 is a no-op. */
9894 if (code == ASHIFTRT
9895 && (num_sign_bit_copies (varop, shift_mode)
9896 == GET_MODE_PRECISION (shift_mode)))
9898 count = 0;
9899 break;
9902 /* If we are doing an arithmetic right shift and discarding all but
9903 the sign bit copies, this is equivalent to doing a shift by the
9904 bitsize minus one. Convert it into that shift because it will often
9905 allow other simplifications. */
9907 if (code == ASHIFTRT
9908 && (count + num_sign_bit_copies (varop, shift_mode)
9909 >= GET_MODE_PRECISION (shift_mode)))
9910 count = GET_MODE_PRECISION (shift_mode) - 1;
9912 /* We simplify the tests below and elsewhere by converting
9913 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9914 `make_compound_operation' will convert it to an ASHIFTRT for
9915 those machines (such as VAX) that don't have an LSHIFTRT. */
9916 if (code == ASHIFTRT
9917 && val_signbit_known_clear_p (shift_mode,
9918 nonzero_bits (varop, shift_mode)))
9919 code = LSHIFTRT;
9921 if (((code == LSHIFTRT
9922 && HWI_COMPUTABLE_MODE_P (shift_mode)
9923 && !(nonzero_bits (varop, shift_mode) >> count))
9924 || (code == ASHIFT
9925 && HWI_COMPUTABLE_MODE_P (shift_mode)
9926 && !((nonzero_bits (varop, shift_mode) << count)
9927 & GET_MODE_MASK (shift_mode))))
9928 && !side_effects_p (varop))
9929 varop = const0_rtx;
9931 switch (GET_CODE (varop))
9933 case SIGN_EXTEND:
9934 case ZERO_EXTEND:
9935 case SIGN_EXTRACT:
9936 case ZERO_EXTRACT:
9937 new_rtx = expand_compound_operation (varop);
9938 if (new_rtx != varop)
9940 varop = new_rtx;
9941 continue;
9943 break;
9945 case MEM:
9946 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9947 minus the width of a smaller mode, we can do this with a
9948 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9949 if ((code == ASHIFTRT || code == LSHIFTRT)
9950 && ! mode_dependent_address_p (XEXP (varop, 0),
9951 MEM_ADDR_SPACE (varop))
9952 && ! MEM_VOLATILE_P (varop)
9953 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9954 MODE_INT, 1)) != BLKmode)
9956 new_rtx = adjust_address_nv (varop, tmode,
9957 BYTES_BIG_ENDIAN ? 0
9958 : count / BITS_PER_UNIT);
9960 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9961 : ZERO_EXTEND, mode, new_rtx);
9962 count = 0;
9963 continue;
9965 break;
9967 case SUBREG:
9968 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9969 the same number of words as what we've seen so far. Then store
9970 the widest mode in MODE. */
9971 if (subreg_lowpart_p (varop)
9972 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9973 > GET_MODE_SIZE (GET_MODE (varop)))
9974 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9975 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9976 == mode_words
9977 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
9978 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
9980 varop = SUBREG_REG (varop);
9981 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9982 mode = GET_MODE (varop);
9983 continue;
9985 break;
9987 case MULT:
9988 /* Some machines use MULT instead of ASHIFT because MULT
9989 is cheaper. But it is still better on those machines to
9990 merge two shifts into one. */
9991 if (CONST_INT_P (XEXP (varop, 1))
9992 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9994 varop
9995 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9996 XEXP (varop, 0),
9997 GEN_INT (exact_log2 (
9998 UINTVAL (XEXP (varop, 1)))));
9999 continue;
10001 break;
10003 case UDIV:
10004 /* Similar, for when divides are cheaper. */
10005 if (CONST_INT_P (XEXP (varop, 1))
10006 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10008 varop
10009 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10010 XEXP (varop, 0),
10011 GEN_INT (exact_log2 (
10012 UINTVAL (XEXP (varop, 1)))));
10013 continue;
10015 break;
10017 case ASHIFTRT:
10018 /* If we are extracting just the sign bit of an arithmetic
10019 right shift, that shift is not needed. However, the sign
10020 bit of a wider mode may be different from what would be
10021 interpreted as the sign bit in a narrower mode, so, if
10022 the result is narrower, don't discard the shift. */
10023 if (code == LSHIFTRT
10024 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10025 && (GET_MODE_BITSIZE (result_mode)
10026 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10028 varop = XEXP (varop, 0);
10029 continue;
10032 /* ... fall through ... */
10034 case LSHIFTRT:
10035 case ASHIFT:
10036 case ROTATE:
10037 /* Here we have two nested shifts. The result is usually the
10038 AND of a new shift with a mask. We compute the result below. */
10039 if (CONST_INT_P (XEXP (varop, 1))
10040 && INTVAL (XEXP (varop, 1)) >= 0
10041 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10042 && HWI_COMPUTABLE_MODE_P (result_mode)
10043 && HWI_COMPUTABLE_MODE_P (mode)
10044 && !VECTOR_MODE_P (result_mode))
10046 enum rtx_code first_code = GET_CODE (varop);
10047 unsigned int first_count = INTVAL (XEXP (varop, 1));
10048 unsigned HOST_WIDE_INT mask;
10049 rtx mask_rtx;
10051 /* We have one common special case. We can't do any merging if
10052 the inner code is an ASHIFTRT of a smaller mode. However, if
10053 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10054 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10055 we can convert it to
10056 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10057 This simplifies certain SIGN_EXTEND operations. */
10058 if (code == ASHIFT && first_code == ASHIFTRT
10059 && count == (GET_MODE_PRECISION (result_mode)
10060 - GET_MODE_PRECISION (GET_MODE (varop))))
10062 /* C3 has the low-order C1 bits zero. */
10064 mask = GET_MODE_MASK (mode)
10065 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10067 varop = simplify_and_const_int (NULL_RTX, result_mode,
10068 XEXP (varop, 0), mask);
10069 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10070 varop, count);
10071 count = first_count;
10072 code = ASHIFTRT;
10073 continue;
10076 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10077 than C1 high-order bits equal to the sign bit, we can convert
10078 this to either an ASHIFT or an ASHIFTRT depending on the
10079 two counts.
10081 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10083 if (code == ASHIFTRT && first_code == ASHIFT
10084 && GET_MODE (varop) == shift_mode
10085 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10086 > first_count))
10088 varop = XEXP (varop, 0);
10089 count -= first_count;
10090 if (count < 0)
10092 count = -count;
10093 code = ASHIFT;
10096 continue;
10099 /* There are some cases we can't do. If CODE is ASHIFTRT,
10100 we can only do this if FIRST_CODE is also ASHIFTRT.
10102 We can't do the case when CODE is ROTATE and FIRST_CODE is
10103 ASHIFTRT.
10105 If the mode of this shift is not the mode of the outer shift,
10106 we can't do this if either shift is a right shift or ROTATE.
10108 Finally, we can't do any of these if the mode is too wide
10109 unless the codes are the same.
10111 Handle the case where the shift codes are the same
10112 first. */
10114 if (code == first_code)
10116 if (GET_MODE (varop) != result_mode
10117 && (code == ASHIFTRT || code == LSHIFTRT
10118 || code == ROTATE))
10119 break;
10121 count += first_count;
10122 varop = XEXP (varop, 0);
10123 continue;
10126 if (code == ASHIFTRT
10127 || (code == ROTATE && first_code == ASHIFTRT)
10128 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10129 || (GET_MODE (varop) != result_mode
10130 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10131 || first_code == ROTATE
10132 || code == ROTATE)))
10133 break;
10135 /* To compute the mask to apply after the shift, shift the
10136 nonzero bits of the inner shift the same way the
10137 outer shift will. */
10139 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10140 result_mode);
10142 mask_rtx
10143 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10144 GEN_INT (count));
10146 /* Give up if we can't compute an outer operation to use. */
10147 if (mask_rtx == 0
10148 || !CONST_INT_P (mask_rtx)
10149 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10150 INTVAL (mask_rtx),
10151 result_mode, &complement_p))
10152 break;
10154 /* If the shifts are in the same direction, we add the
10155 counts. Otherwise, we subtract them. */
10156 if ((code == ASHIFTRT || code == LSHIFTRT)
10157 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10158 count += first_count;
10159 else
10160 count -= first_count;
10162 /* If COUNT is positive, the new shift is usually CODE,
10163 except for the two exceptions below, in which case it is
10164 FIRST_CODE. If the count is negative, FIRST_CODE should
10165 always be used */
10166 if (count > 0
10167 && ((first_code == ROTATE && code == ASHIFT)
10168 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10169 code = first_code;
10170 else if (count < 0)
10171 code = first_code, count = -count;
10173 varop = XEXP (varop, 0);
10174 continue;
10177 /* If we have (A << B << C) for any shift, we can convert this to
10178 (A << C << B). This wins if A is a constant. Only try this if
10179 B is not a constant. */
10181 else if (GET_CODE (varop) == code
10182 && CONST_INT_P (XEXP (varop, 0))
10183 && !CONST_INT_P (XEXP (varop, 1)))
10185 rtx new_rtx = simplify_const_binary_operation (code, mode,
10186 XEXP (varop, 0),
10187 GEN_INT (count));
10188 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10189 count = 0;
10190 continue;
10192 break;
10194 case NOT:
10195 if (VECTOR_MODE_P (mode))
10196 break;
10198 /* Make this fit the case below. */
10199 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10200 continue;
10202 case IOR:
10203 case AND:
10204 case XOR:
10205 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10206 with C the size of VAROP - 1 and the shift is logical if
10207 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10208 we have an (le X 0) operation. If we have an arithmetic shift
10209 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10210 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10212 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10213 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10214 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10215 && (code == LSHIFTRT || code == ASHIFTRT)
10216 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10217 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10219 count = 0;
10220 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10221 const0_rtx);
10223 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10224 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10226 continue;
10229 /* If we have (shift (logical)), move the logical to the outside
10230 to allow it to possibly combine with another logical and the
10231 shift to combine with another shift. This also canonicalizes to
10232 what a ZERO_EXTRACT looks like. Also, some machines have
10233 (and (shift)) insns. */
10235 if (CONST_INT_P (XEXP (varop, 1))
10236 /* We can't do this if we have (ashiftrt (xor)) and the
10237 constant has its sign bit set in shift_mode. */
10238 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10239 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10240 shift_mode))
10241 && (new_rtx = simplify_const_binary_operation
10242 (code, result_mode,
10243 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10244 GEN_INT (count))) != 0
10245 && CONST_INT_P (new_rtx)
10246 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10247 INTVAL (new_rtx), result_mode, &complement_p))
10249 varop = XEXP (varop, 0);
10250 continue;
10253 /* If we can't do that, try to simplify the shift in each arm of the
10254 logical expression, make a new logical expression, and apply
10255 the inverse distributive law. This also can't be done
10256 for some (ashiftrt (xor)). */
10257 if (CONST_INT_P (XEXP (varop, 1))
10258 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10259 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10260 shift_mode)))
10262 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10263 XEXP (varop, 0), count);
10264 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10265 XEXP (varop, 1), count);
10267 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10268 lhs, rhs);
10269 varop = apply_distributive_law (varop);
10271 count = 0;
10272 continue;
10274 break;
10276 case EQ:
10277 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10278 says that the sign bit can be tested, FOO has mode MODE, C is
10279 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10280 that may be nonzero. */
10281 if (code == LSHIFTRT
10282 && XEXP (varop, 1) == const0_rtx
10283 && GET_MODE (XEXP (varop, 0)) == result_mode
10284 && count == (GET_MODE_PRECISION (result_mode) - 1)
10285 && HWI_COMPUTABLE_MODE_P (result_mode)
10286 && STORE_FLAG_VALUE == -1
10287 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10288 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10289 &complement_p))
10291 varop = XEXP (varop, 0);
10292 count = 0;
10293 continue;
10295 break;
10297 case NEG:
10298 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10299 than the number of bits in the mode is equivalent to A. */
10300 if (code == LSHIFTRT
10301 && count == (GET_MODE_PRECISION (result_mode) - 1)
10302 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10304 varop = XEXP (varop, 0);
10305 count = 0;
10306 continue;
10309 /* NEG commutes with ASHIFT since it is multiplication. Move the
10310 NEG outside to allow shifts to combine. */
10311 if (code == ASHIFT
10312 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10313 &complement_p))
10315 varop = XEXP (varop, 0);
10316 continue;
10318 break;
10320 case PLUS:
10321 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10322 is one less than the number of bits in the mode is
10323 equivalent to (xor A 1). */
10324 if (code == LSHIFTRT
10325 && count == (GET_MODE_PRECISION (result_mode) - 1)
10326 && XEXP (varop, 1) == constm1_rtx
10327 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10328 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10329 &complement_p))
10331 count = 0;
10332 varop = XEXP (varop, 0);
10333 continue;
10336 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10337 that might be nonzero in BAR are those being shifted out and those
10338 bits are known zero in FOO, we can replace the PLUS with FOO.
10339 Similarly in the other operand order. This code occurs when
10340 we are computing the size of a variable-size array. */
10342 if ((code == ASHIFTRT || code == LSHIFTRT)
10343 && count < HOST_BITS_PER_WIDE_INT
10344 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10345 && (nonzero_bits (XEXP (varop, 1), result_mode)
10346 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10348 varop = XEXP (varop, 0);
10349 continue;
10351 else if ((code == ASHIFTRT || code == LSHIFTRT)
10352 && count < HOST_BITS_PER_WIDE_INT
10353 && HWI_COMPUTABLE_MODE_P (result_mode)
10354 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10355 >> count)
10356 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10357 & nonzero_bits (XEXP (varop, 1),
10358 result_mode)))
10360 varop = XEXP (varop, 1);
10361 continue;
10364 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10365 if (code == ASHIFT
10366 && CONST_INT_P (XEXP (varop, 1))
10367 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10368 XEXP (varop, 1),
10369 GEN_INT (count))) != 0
10370 && CONST_INT_P (new_rtx)
10371 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10372 INTVAL (new_rtx), result_mode, &complement_p))
10374 varop = XEXP (varop, 0);
10375 continue;
10378 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10379 signbit', and attempt to change the PLUS to an XOR and move it to
10380 the outer operation as is done above in the AND/IOR/XOR case
10381 leg for shift(logical). See details in logical handling above
10382 for reasoning in doing so. */
10383 if (code == LSHIFTRT
10384 && CONST_INT_P (XEXP (varop, 1))
10385 && mode_signbit_p (result_mode, XEXP (varop, 1))
10386 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10387 XEXP (varop, 1),
10388 GEN_INT (count))) != 0
10389 && CONST_INT_P (new_rtx)
10390 && merge_outer_ops (&outer_op, &outer_const, XOR,
10391 INTVAL (new_rtx), result_mode, &complement_p))
10393 varop = XEXP (varop, 0);
10394 continue;
10397 break;
10399 case MINUS:
10400 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10401 with C the size of VAROP - 1 and the shift is logical if
10402 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10403 we have a (gt X 0) operation. If the shift is arithmetic with
10404 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10405 we have a (neg (gt X 0)) operation. */
10407 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10408 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10409 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10410 && (code == LSHIFTRT || code == ASHIFTRT)
10411 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10412 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10413 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10415 count = 0;
10416 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10417 const0_rtx);
10419 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10420 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10422 continue;
10424 break;
10426 case TRUNCATE:
10427 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10428 if the truncate does not affect the value. */
10429 if (code == LSHIFTRT
10430 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10431 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10432 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10433 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10434 - GET_MODE_PRECISION (GET_MODE (varop)))))
10436 rtx varop_inner = XEXP (varop, 0);
10438 varop_inner
10439 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10440 XEXP (varop_inner, 0),
10441 GEN_INT
10442 (count + INTVAL (XEXP (varop_inner, 1))));
10443 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10444 count = 0;
10445 continue;
10447 break;
10449 default:
10450 break;
10453 break;
10456 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10457 outer_op, outer_const);
10459 /* We have now finished analyzing the shift. The result should be
10460 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10461 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10462 to the result of the shift. OUTER_CONST is the relevant constant,
10463 but we must turn off all bits turned off in the shift. */
10465 if (outer_op == UNKNOWN
10466 && orig_code == code && orig_count == count
10467 && varop == orig_varop
10468 && shift_mode == GET_MODE (varop))
10469 return NULL_RTX;
10471 /* Make a SUBREG if necessary. If we can't make it, fail. */
10472 varop = gen_lowpart (shift_mode, varop);
10473 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10474 return NULL_RTX;
10476 /* If we have an outer operation and we just made a shift, it is
10477 possible that we could have simplified the shift were it not
10478 for the outer operation. So try to do the simplification
10479 recursively. */
10481 if (outer_op != UNKNOWN)
10482 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10483 else
10484 x = NULL_RTX;
10486 if (x == NULL_RTX)
10487 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10489 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10490 turn off all the bits that the shift would have turned off. */
10491 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10492 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10493 GET_MODE_MASK (result_mode) >> orig_count);
10495 /* Do the remainder of the processing in RESULT_MODE. */
10496 x = gen_lowpart_or_truncate (result_mode, x);
10498 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10499 operation. */
10500 if (complement_p)
10501 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10503 if (outer_op != UNKNOWN)
10505 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10506 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10507 outer_const = trunc_int_for_mode (outer_const, result_mode);
10509 if (outer_op == AND)
10510 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10511 else if (outer_op == SET)
10513 /* This means that we have determined that the result is
10514 equivalent to a constant. This should be rare. */
10515 if (!side_effects_p (x))
10516 x = GEN_INT (outer_const);
10518 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10519 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10520 else
10521 x = simplify_gen_binary (outer_op, result_mode, x,
10522 GEN_INT (outer_const));
10525 return x;
10528 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10529 The result of the shift is RESULT_MODE. If we cannot simplify it,
10530 return X or, if it is NULL, synthesize the expression with
10531 simplify_gen_binary. Otherwise, return a simplified value.
10533 The shift is normally computed in the widest mode we find in VAROP, as
10534 long as it isn't a different number of words than RESULT_MODE. Exceptions
10535 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10537 static rtx
10538 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10539 rtx varop, int count)
10541 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10542 if (tem)
10543 return tem;
10545 if (!x)
10546 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10547 if (GET_MODE (x) != result_mode)
10548 x = gen_lowpart (result_mode, x);
10549 return x;
10553 /* Like recog, but we receive the address of a pointer to a new pattern.
10554 We try to match the rtx that the pointer points to.
10555 If that fails, we may try to modify or replace the pattern,
10556 storing the replacement into the same pointer object.
10558 Modifications include deletion or addition of CLOBBERs.
10560 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10561 the CLOBBERs are placed.
10563 The value is the final insn code from the pattern ultimately matched,
10564 or -1. */
10566 static int
10567 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10569 rtx pat = *pnewpat;
10570 rtx pat_without_clobbers;
10571 int insn_code_number;
10572 int num_clobbers_to_add = 0;
10573 int i;
10574 rtx notes = NULL_RTX;
10575 rtx old_notes, old_pat;
10576 int old_icode;
10578 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10579 we use to indicate that something didn't match. If we find such a
10580 thing, force rejection. */
10581 if (GET_CODE (pat) == PARALLEL)
10582 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10583 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10584 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10585 return -1;
10587 old_pat = PATTERN (insn);
10588 old_notes = REG_NOTES (insn);
10589 PATTERN (insn) = pat;
10590 REG_NOTES (insn) = NULL_RTX;
10592 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10593 if (dump_file && (dump_flags & TDF_DETAILS))
10595 if (insn_code_number < 0)
10596 fputs ("Failed to match this instruction:\n", dump_file);
10597 else
10598 fputs ("Successfully matched this instruction:\n", dump_file);
10599 print_rtl_single (dump_file, pat);
10602 /* If it isn't, there is the possibility that we previously had an insn
10603 that clobbered some register as a side effect, but the combined
10604 insn doesn't need to do that. So try once more without the clobbers
10605 unless this represents an ASM insn. */
10607 if (insn_code_number < 0 && ! check_asm_operands (pat)
10608 && GET_CODE (pat) == PARALLEL)
10610 int pos;
10612 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10613 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10615 if (i != pos)
10616 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10617 pos++;
10620 SUBST_INT (XVECLEN (pat, 0), pos);
10622 if (pos == 1)
10623 pat = XVECEXP (pat, 0, 0);
10625 PATTERN (insn) = pat;
10626 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10627 if (dump_file && (dump_flags & TDF_DETAILS))
10629 if (insn_code_number < 0)
10630 fputs ("Failed to match this instruction:\n", dump_file);
10631 else
10632 fputs ("Successfully matched this instruction:\n", dump_file);
10633 print_rtl_single (dump_file, pat);
10637 pat_without_clobbers = pat;
10639 PATTERN (insn) = old_pat;
10640 REG_NOTES (insn) = old_notes;
10642 /* Recognize all noop sets, these will be killed by followup pass. */
10643 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10644 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10646 /* If we had any clobbers to add, make a new pattern than contains
10647 them. Then check to make sure that all of them are dead. */
10648 if (num_clobbers_to_add)
10650 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10651 rtvec_alloc (GET_CODE (pat) == PARALLEL
10652 ? (XVECLEN (pat, 0)
10653 + num_clobbers_to_add)
10654 : num_clobbers_to_add + 1));
10656 if (GET_CODE (pat) == PARALLEL)
10657 for (i = 0; i < XVECLEN (pat, 0); i++)
10658 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10659 else
10660 XVECEXP (newpat, 0, 0) = pat;
10662 add_clobbers (newpat, insn_code_number);
10664 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10665 i < XVECLEN (newpat, 0); i++)
10667 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10668 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10669 return -1;
10670 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10672 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10673 notes = alloc_reg_note (REG_UNUSED,
10674 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10677 pat = newpat;
10680 if (insn_code_number >= 0
10681 && insn_code_number != NOOP_MOVE_INSN_CODE)
10683 old_pat = PATTERN (insn);
10684 old_notes = REG_NOTES (insn);
10685 old_icode = INSN_CODE (insn);
10686 PATTERN (insn) = pat;
10687 REG_NOTES (insn) = notes;
10689 /* Allow targets to reject combined insn. */
10690 if (!targetm.legitimate_combined_insn (insn))
10692 if (dump_file && (dump_flags & TDF_DETAILS))
10693 fputs ("Instruction not appropriate for target.",
10694 dump_file);
10696 /* Callers expect recog_for_combine to strip
10697 clobbers from the pattern on failure. */
10698 pat = pat_without_clobbers;
10699 notes = NULL_RTX;
10701 insn_code_number = -1;
10704 PATTERN (insn) = old_pat;
10705 REG_NOTES (insn) = old_notes;
10706 INSN_CODE (insn) = old_icode;
10709 *pnewpat = pat;
10710 *pnotes = notes;
10712 return insn_code_number;
10715 /* Like gen_lowpart_general but for use by combine. In combine it
10716 is not possible to create any new pseudoregs. However, it is
10717 safe to create invalid memory addresses, because combine will
10718 try to recognize them and all they will do is make the combine
10719 attempt fail.
10721 If for some reason this cannot do its job, an rtx
10722 (clobber (const_int 0)) is returned.
10723 An insn containing that will not be recognized. */
10725 static rtx
10726 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10728 enum machine_mode imode = GET_MODE (x);
10729 unsigned int osize = GET_MODE_SIZE (omode);
10730 unsigned int isize = GET_MODE_SIZE (imode);
10731 rtx result;
10733 if (omode == imode)
10734 return x;
10736 /* We can only support MODE being wider than a word if X is a
10737 constant integer or has a mode the same size. */
10738 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10739 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10740 goto fail;
10742 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10743 won't know what to do. So we will strip off the SUBREG here and
10744 process normally. */
10745 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10747 x = SUBREG_REG (x);
10749 /* For use in case we fall down into the address adjustments
10750 further below, we need to adjust the known mode and size of
10751 x; imode and isize, since we just adjusted x. */
10752 imode = GET_MODE (x);
10754 if (imode == omode)
10755 return x;
10757 isize = GET_MODE_SIZE (imode);
10760 result = gen_lowpart_common (omode, x);
10762 if (result)
10763 return result;
10765 if (MEM_P (x))
10767 int offset = 0;
10769 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10770 address. */
10771 if (MEM_VOLATILE_P (x)
10772 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10773 goto fail;
10775 /* If we want to refer to something bigger than the original memref,
10776 generate a paradoxical subreg instead. That will force a reload
10777 of the original memref X. */
10778 if (isize < osize)
10779 return gen_rtx_SUBREG (omode, x, 0);
10781 if (WORDS_BIG_ENDIAN)
10782 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10784 /* Adjust the address so that the address-after-the-data is
10785 unchanged. */
10786 if (BYTES_BIG_ENDIAN)
10787 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10789 return adjust_address_nv (x, omode, offset);
10792 /* If X is a comparison operator, rewrite it in a new mode. This
10793 probably won't match, but may allow further simplifications. */
10794 else if (COMPARISON_P (x))
10795 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10797 /* If we couldn't simplify X any other way, just enclose it in a
10798 SUBREG. Normally, this SUBREG won't match, but some patterns may
10799 include an explicit SUBREG or we may simplify it further in combine. */
10800 else
10802 int offset = 0;
10803 rtx res;
10805 offset = subreg_lowpart_offset (omode, imode);
10806 if (imode == VOIDmode)
10808 imode = int_mode_for_mode (omode);
10809 x = gen_lowpart_common (imode, x);
10810 if (x == NULL)
10811 goto fail;
10813 res = simplify_gen_subreg (omode, x, imode, offset);
10814 if (res)
10815 return res;
10818 fail:
10819 return gen_rtx_CLOBBER (omode, const0_rtx);
10822 /* Try to simplify a comparison between OP0 and a constant OP1,
10823 where CODE is the comparison code that will be tested, into a
10824 (CODE OP0 const0_rtx) form.
10826 The result is a possibly different comparison code to use.
10827 *POP1 may be updated. */
10829 static enum rtx_code
10830 simplify_compare_const (enum rtx_code code, enum machine_mode mode,
10831 rtx op0, rtx *pop1)
10833 unsigned int mode_width = GET_MODE_PRECISION (mode);
10834 HOST_WIDE_INT const_op = INTVAL (*pop1);
10836 /* Get the constant we are comparing against and turn off all bits
10837 not on in our mode. */
10838 if (mode != VOIDmode)
10839 const_op = trunc_int_for_mode (const_op, mode);
10841 /* If we are comparing against a constant power of two and the value
10842 being compared can only have that single bit nonzero (e.g., it was
10843 `and'ed with that bit), we can replace this with a comparison
10844 with zero. */
10845 if (const_op
10846 && (code == EQ || code == NE || code == GE || code == GEU
10847 || code == LT || code == LTU)
10848 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10849 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
10850 && (nonzero_bits (op0, mode)
10851 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
10853 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10854 const_op = 0;
10857 /* Similarly, if we are comparing a value known to be either -1 or
10858 0 with -1, change it to the opposite comparison against zero. */
10859 if (const_op == -1
10860 && (code == EQ || code == NE || code == GT || code == LE
10861 || code == GEU || code == LTU)
10862 && num_sign_bit_copies (op0, mode) == mode_width)
10864 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10865 const_op = 0;
10868 /* Do some canonicalizations based on the comparison code. We prefer
10869 comparisons against zero and then prefer equality comparisons.
10870 If we can reduce the size of a constant, we will do that too. */
10871 switch (code)
10873 case LT:
10874 /* < C is equivalent to <= (C - 1) */
10875 if (const_op > 0)
10877 const_op -= 1;
10878 code = LE;
10879 /* ... fall through to LE case below. */
10881 else
10882 break;
10884 case LE:
10885 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10886 if (const_op < 0)
10888 const_op += 1;
10889 code = LT;
10892 /* If we are doing a <= 0 comparison on a value known to have
10893 a zero sign bit, we can replace this with == 0. */
10894 else if (const_op == 0
10895 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10896 && (nonzero_bits (op0, mode)
10897 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10898 == 0)
10899 code = EQ;
10900 break;
10902 case GE:
10903 /* >= C is equivalent to > (C - 1). */
10904 if (const_op > 0)
10906 const_op -= 1;
10907 code = GT;
10908 /* ... fall through to GT below. */
10910 else
10911 break;
10913 case GT:
10914 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10915 if (const_op < 0)
10917 const_op += 1;
10918 code = GE;
10921 /* If we are doing a > 0 comparison on a value known to have
10922 a zero sign bit, we can replace this with != 0. */
10923 else if (const_op == 0
10924 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10925 && (nonzero_bits (op0, mode)
10926 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10927 == 0)
10928 code = NE;
10929 break;
10931 case LTU:
10932 /* < C is equivalent to <= (C - 1). */
10933 if (const_op > 0)
10935 const_op -= 1;
10936 code = LEU;
10937 /* ... fall through ... */
10939 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10940 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10941 && (unsigned HOST_WIDE_INT) const_op
10942 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10944 const_op = 0;
10945 code = GE;
10946 break;
10948 else
10949 break;
10951 case LEU:
10952 /* unsigned <= 0 is equivalent to == 0 */
10953 if (const_op == 0)
10954 code = EQ;
10955 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10956 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10957 && (unsigned HOST_WIDE_INT) const_op
10958 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10960 const_op = 0;
10961 code = GE;
10963 break;
10965 case GEU:
10966 /* >= C is equivalent to > (C - 1). */
10967 if (const_op > 1)
10969 const_op -= 1;
10970 code = GTU;
10971 /* ... fall through ... */
10974 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10975 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10976 && (unsigned HOST_WIDE_INT) const_op
10977 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10979 const_op = 0;
10980 code = LT;
10981 break;
10983 else
10984 break;
10986 case GTU:
10987 /* unsigned > 0 is equivalent to != 0 */
10988 if (const_op == 0)
10989 code = NE;
10990 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10991 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10992 && (unsigned HOST_WIDE_INT) const_op
10993 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10995 const_op = 0;
10996 code = LT;
10998 break;
11000 default:
11001 break;
11004 *pop1 = GEN_INT (const_op);
11005 return code;
11008 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11009 comparison code that will be tested.
11011 The result is a possibly different comparison code to use. *POP0 and
11012 *POP1 may be updated.
11014 It is possible that we might detect that a comparison is either always
11015 true or always false. However, we do not perform general constant
11016 folding in combine, so this knowledge isn't useful. Such tautologies
11017 should have been detected earlier. Hence we ignore all such cases. */
11019 static enum rtx_code
11020 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11022 rtx op0 = *pop0;
11023 rtx op1 = *pop1;
11024 rtx tem, tem1;
11025 int i;
11026 enum machine_mode mode, tmode;
11028 /* Try a few ways of applying the same transformation to both operands. */
11029 while (1)
11031 #ifndef WORD_REGISTER_OPERATIONS
11032 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11033 so check specially. */
11034 if (code != GTU && code != GEU && code != LTU && code != LEU
11035 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11036 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11037 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11038 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11039 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11040 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11041 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11042 && CONST_INT_P (XEXP (op0, 1))
11043 && XEXP (op0, 1) == XEXP (op1, 1)
11044 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11045 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11046 && (INTVAL (XEXP (op0, 1))
11047 == (GET_MODE_PRECISION (GET_MODE (op0))
11048 - (GET_MODE_PRECISION
11049 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11051 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11052 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11054 #endif
11056 /* If both operands are the same constant shift, see if we can ignore the
11057 shift. We can if the shift is a rotate or if the bits shifted out of
11058 this shift are known to be zero for both inputs and if the type of
11059 comparison is compatible with the shift. */
11060 if (GET_CODE (op0) == GET_CODE (op1)
11061 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11062 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11063 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11064 && (code != GT && code != LT && code != GE && code != LE))
11065 || (GET_CODE (op0) == ASHIFTRT
11066 && (code != GTU && code != LTU
11067 && code != GEU && code != LEU)))
11068 && CONST_INT_P (XEXP (op0, 1))
11069 && INTVAL (XEXP (op0, 1)) >= 0
11070 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11071 && XEXP (op0, 1) == XEXP (op1, 1))
11073 enum machine_mode mode = GET_MODE (op0);
11074 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11075 int shift_count = INTVAL (XEXP (op0, 1));
11077 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11078 mask &= (mask >> shift_count) << shift_count;
11079 else if (GET_CODE (op0) == ASHIFT)
11080 mask = (mask & (mask << shift_count)) >> shift_count;
11082 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11083 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11084 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11085 else
11086 break;
11089 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11090 SUBREGs are of the same mode, and, in both cases, the AND would
11091 be redundant if the comparison was done in the narrower mode,
11092 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11093 and the operand's possibly nonzero bits are 0xffffff01; in that case
11094 if we only care about QImode, we don't need the AND). This case
11095 occurs if the output mode of an scc insn is not SImode and
11096 STORE_FLAG_VALUE == 1 (e.g., the 386).
11098 Similarly, check for a case where the AND's are ZERO_EXTEND
11099 operations from some narrower mode even though a SUBREG is not
11100 present. */
11102 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11103 && CONST_INT_P (XEXP (op0, 1))
11104 && CONST_INT_P (XEXP (op1, 1)))
11106 rtx inner_op0 = XEXP (op0, 0);
11107 rtx inner_op1 = XEXP (op1, 0);
11108 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11109 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11110 int changed = 0;
11112 if (paradoxical_subreg_p (inner_op0)
11113 && GET_CODE (inner_op1) == SUBREG
11114 && (GET_MODE (SUBREG_REG (inner_op0))
11115 == GET_MODE (SUBREG_REG (inner_op1)))
11116 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11117 <= HOST_BITS_PER_WIDE_INT)
11118 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11119 GET_MODE (SUBREG_REG (inner_op0)))))
11120 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11121 GET_MODE (SUBREG_REG (inner_op1))))))
11123 op0 = SUBREG_REG (inner_op0);
11124 op1 = SUBREG_REG (inner_op1);
11126 /* The resulting comparison is always unsigned since we masked
11127 off the original sign bit. */
11128 code = unsigned_condition (code);
11130 changed = 1;
11133 else if (c0 == c1)
11134 for (tmode = GET_CLASS_NARROWEST_MODE
11135 (GET_MODE_CLASS (GET_MODE (op0)));
11136 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11137 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11139 op0 = gen_lowpart (tmode, inner_op0);
11140 op1 = gen_lowpart (tmode, inner_op1);
11141 code = unsigned_condition (code);
11142 changed = 1;
11143 break;
11146 if (! changed)
11147 break;
11150 /* If both operands are NOT, we can strip off the outer operation
11151 and adjust the comparison code for swapped operands; similarly for
11152 NEG, except that this must be an equality comparison. */
11153 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11154 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11155 && (code == EQ || code == NE)))
11156 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11158 else
11159 break;
11162 /* If the first operand is a constant, swap the operands and adjust the
11163 comparison code appropriately, but don't do this if the second operand
11164 is already a constant integer. */
11165 if (swap_commutative_operands_p (op0, op1))
11167 tem = op0, op0 = op1, op1 = tem;
11168 code = swap_condition (code);
11171 /* We now enter a loop during which we will try to simplify the comparison.
11172 For the most part, we only are concerned with comparisons with zero,
11173 but some things may really be comparisons with zero but not start
11174 out looking that way. */
11176 while (CONST_INT_P (op1))
11178 enum machine_mode mode = GET_MODE (op0);
11179 unsigned int mode_width = GET_MODE_PRECISION (mode);
11180 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11181 int equality_comparison_p;
11182 int sign_bit_comparison_p;
11183 int unsigned_comparison_p;
11184 HOST_WIDE_INT const_op;
11186 /* We only want to handle integral modes. This catches VOIDmode,
11187 CCmode, and the floating-point modes. An exception is that we
11188 can handle VOIDmode if OP0 is a COMPARE or a comparison
11189 operation. */
11191 if (GET_MODE_CLASS (mode) != MODE_INT
11192 && ! (mode == VOIDmode
11193 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11194 break;
11196 /* Try to simplify the compare to constant, possibly changing the
11197 comparison op, and/or changing op1 to zero. */
11198 code = simplify_compare_const (code, mode, op0, &op1);
11199 const_op = INTVAL (op1);
11201 /* Compute some predicates to simplify code below. */
11203 equality_comparison_p = (code == EQ || code == NE);
11204 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11205 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11206 || code == GEU);
11208 /* If this is a sign bit comparison and we can do arithmetic in
11209 MODE, say that we will only be needing the sign bit of OP0. */
11210 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11211 op0 = force_to_mode (op0, mode,
11212 (unsigned HOST_WIDE_INT) 1
11213 << (GET_MODE_PRECISION (mode) - 1),
11216 /* Now try cases based on the opcode of OP0. If none of the cases
11217 does a "continue", we exit this loop immediately after the
11218 switch. */
11220 switch (GET_CODE (op0))
11222 case ZERO_EXTRACT:
11223 /* If we are extracting a single bit from a variable position in
11224 a constant that has only a single bit set and are comparing it
11225 with zero, we can convert this into an equality comparison
11226 between the position and the location of the single bit. */
11227 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11228 have already reduced the shift count modulo the word size. */
11229 if (!SHIFT_COUNT_TRUNCATED
11230 && CONST_INT_P (XEXP (op0, 0))
11231 && XEXP (op0, 1) == const1_rtx
11232 && equality_comparison_p && const_op == 0
11233 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11235 if (BITS_BIG_ENDIAN)
11236 i = BITS_PER_WORD - 1 - i;
11238 op0 = XEXP (op0, 2);
11239 op1 = GEN_INT (i);
11240 const_op = i;
11242 /* Result is nonzero iff shift count is equal to I. */
11243 code = reverse_condition (code);
11244 continue;
11247 /* ... fall through ... */
11249 case SIGN_EXTRACT:
11250 tem = expand_compound_operation (op0);
11251 if (tem != op0)
11253 op0 = tem;
11254 continue;
11256 break;
11258 case NOT:
11259 /* If testing for equality, we can take the NOT of the constant. */
11260 if (equality_comparison_p
11261 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11263 op0 = XEXP (op0, 0);
11264 op1 = tem;
11265 continue;
11268 /* If just looking at the sign bit, reverse the sense of the
11269 comparison. */
11270 if (sign_bit_comparison_p)
11272 op0 = XEXP (op0, 0);
11273 code = (code == GE ? LT : GE);
11274 continue;
11276 break;
11278 case NEG:
11279 /* If testing for equality, we can take the NEG of the constant. */
11280 if (equality_comparison_p
11281 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11283 op0 = XEXP (op0, 0);
11284 op1 = tem;
11285 continue;
11288 /* The remaining cases only apply to comparisons with zero. */
11289 if (const_op != 0)
11290 break;
11292 /* When X is ABS or is known positive,
11293 (neg X) is < 0 if and only if X != 0. */
11295 if (sign_bit_comparison_p
11296 && (GET_CODE (XEXP (op0, 0)) == ABS
11297 || (mode_width <= HOST_BITS_PER_WIDE_INT
11298 && (nonzero_bits (XEXP (op0, 0), mode)
11299 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11300 == 0)))
11302 op0 = XEXP (op0, 0);
11303 code = (code == LT ? NE : EQ);
11304 continue;
11307 /* If we have NEG of something whose two high-order bits are the
11308 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11309 if (num_sign_bit_copies (op0, mode) >= 2)
11311 op0 = XEXP (op0, 0);
11312 code = swap_condition (code);
11313 continue;
11315 break;
11317 case ROTATE:
11318 /* If we are testing equality and our count is a constant, we
11319 can perform the inverse operation on our RHS. */
11320 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11321 && (tem = simplify_binary_operation (ROTATERT, mode,
11322 op1, XEXP (op0, 1))) != 0)
11324 op0 = XEXP (op0, 0);
11325 op1 = tem;
11326 continue;
11329 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11330 a particular bit. Convert it to an AND of a constant of that
11331 bit. This will be converted into a ZERO_EXTRACT. */
11332 if (const_op == 0 && sign_bit_comparison_p
11333 && CONST_INT_P (XEXP (op0, 1))
11334 && mode_width <= HOST_BITS_PER_WIDE_INT)
11336 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11337 ((unsigned HOST_WIDE_INT) 1
11338 << (mode_width - 1
11339 - INTVAL (XEXP (op0, 1)))));
11340 code = (code == LT ? NE : EQ);
11341 continue;
11344 /* Fall through. */
11346 case ABS:
11347 /* ABS is ignorable inside an equality comparison with zero. */
11348 if (const_op == 0 && equality_comparison_p)
11350 op0 = XEXP (op0, 0);
11351 continue;
11353 break;
11355 case SIGN_EXTEND:
11356 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11357 (compare FOO CONST) if CONST fits in FOO's mode and we
11358 are either testing inequality or have an unsigned
11359 comparison with ZERO_EXTEND or a signed comparison with
11360 SIGN_EXTEND. But don't do it if we don't have a compare
11361 insn of the given mode, since we'd have to revert it
11362 later on, and then we wouldn't know whether to sign- or
11363 zero-extend. */
11364 mode = GET_MODE (XEXP (op0, 0));
11365 if (GET_MODE_CLASS (mode) == MODE_INT
11366 && ! unsigned_comparison_p
11367 && HWI_COMPUTABLE_MODE_P (mode)
11368 && trunc_int_for_mode (const_op, mode) == const_op
11369 && have_insn_for (COMPARE, mode))
11371 op0 = XEXP (op0, 0);
11372 continue;
11374 break;
11376 case SUBREG:
11377 /* Check for the case where we are comparing A - C1 with C2, that is
11379 (subreg:MODE (plus (A) (-C1))) op (C2)
11381 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11382 comparison in the wider mode. One of the following two conditions
11383 must be true in order for this to be valid:
11385 1. The mode extension results in the same bit pattern being added
11386 on both sides and the comparison is equality or unsigned. As
11387 C2 has been truncated to fit in MODE, the pattern can only be
11388 all 0s or all 1s.
11390 2. The mode extension results in the sign bit being copied on
11391 each side.
11393 The difficulty here is that we have predicates for A but not for
11394 (A - C1) so we need to check that C1 is within proper bounds so
11395 as to perturbate A as little as possible. */
11397 if (mode_width <= HOST_BITS_PER_WIDE_INT
11398 && subreg_lowpart_p (op0)
11399 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11400 && GET_CODE (SUBREG_REG (op0)) == PLUS
11401 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11403 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11404 rtx a = XEXP (SUBREG_REG (op0), 0);
11405 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11407 if ((c1 > 0
11408 && (unsigned HOST_WIDE_INT) c1
11409 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11410 && (equality_comparison_p || unsigned_comparison_p)
11411 /* (A - C1) zero-extends if it is positive and sign-extends
11412 if it is negative, C2 both zero- and sign-extends. */
11413 && ((0 == (nonzero_bits (a, inner_mode)
11414 & ~GET_MODE_MASK (mode))
11415 && const_op >= 0)
11416 /* (A - C1) sign-extends if it is positive and 1-extends
11417 if it is negative, C2 both sign- and 1-extends. */
11418 || (num_sign_bit_copies (a, inner_mode)
11419 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11420 - mode_width)
11421 && const_op < 0)))
11422 || ((unsigned HOST_WIDE_INT) c1
11423 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11424 /* (A - C1) always sign-extends, like C2. */
11425 && num_sign_bit_copies (a, inner_mode)
11426 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11427 - (mode_width - 1))))
11429 op0 = SUBREG_REG (op0);
11430 continue;
11434 /* If the inner mode is narrower and we are extracting the low part,
11435 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11436 if (subreg_lowpart_p (op0)
11437 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11438 /* Fall through */ ;
11439 else
11440 break;
11442 /* ... fall through ... */
11444 case ZERO_EXTEND:
11445 mode = GET_MODE (XEXP (op0, 0));
11446 if (GET_MODE_CLASS (mode) == MODE_INT
11447 && (unsigned_comparison_p || equality_comparison_p)
11448 && HWI_COMPUTABLE_MODE_P (mode)
11449 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11450 && const_op >= 0
11451 && have_insn_for (COMPARE, mode))
11453 op0 = XEXP (op0, 0);
11454 continue;
11456 break;
11458 case PLUS:
11459 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11460 this for equality comparisons due to pathological cases involving
11461 overflows. */
11462 if (equality_comparison_p
11463 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11464 op1, XEXP (op0, 1))))
11466 op0 = XEXP (op0, 0);
11467 op1 = tem;
11468 continue;
11471 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11472 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11473 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11475 op0 = XEXP (XEXP (op0, 0), 0);
11476 code = (code == LT ? EQ : NE);
11477 continue;
11479 break;
11481 case MINUS:
11482 /* We used to optimize signed comparisons against zero, but that
11483 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11484 arrive here as equality comparisons, or (GEU, LTU) are
11485 optimized away. No need to special-case them. */
11487 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11488 (eq B (minus A C)), whichever simplifies. We can only do
11489 this for equality comparisons due to pathological cases involving
11490 overflows. */
11491 if (equality_comparison_p
11492 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11493 XEXP (op0, 1), op1)))
11495 op0 = XEXP (op0, 0);
11496 op1 = tem;
11497 continue;
11500 if (equality_comparison_p
11501 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11502 XEXP (op0, 0), op1)))
11504 op0 = XEXP (op0, 1);
11505 op1 = tem;
11506 continue;
11509 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11510 of bits in X minus 1, is one iff X > 0. */
11511 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11512 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11513 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11514 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11516 op0 = XEXP (op0, 1);
11517 code = (code == GE ? LE : GT);
11518 continue;
11520 break;
11522 case XOR:
11523 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11524 if C is zero or B is a constant. */
11525 if (equality_comparison_p
11526 && 0 != (tem = simplify_binary_operation (XOR, mode,
11527 XEXP (op0, 1), op1)))
11529 op0 = XEXP (op0, 0);
11530 op1 = tem;
11531 continue;
11533 break;
11535 case EQ: case NE:
11536 case UNEQ: case LTGT:
11537 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11538 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11539 case UNORDERED: case ORDERED:
11540 /* We can't do anything if OP0 is a condition code value, rather
11541 than an actual data value. */
11542 if (const_op != 0
11543 || CC0_P (XEXP (op0, 0))
11544 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11545 break;
11547 /* Get the two operands being compared. */
11548 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11549 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11550 else
11551 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11553 /* Check for the cases where we simply want the result of the
11554 earlier test or the opposite of that result. */
11555 if (code == NE || code == EQ
11556 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11557 && (code == LT || code == GE)))
11559 enum rtx_code new_code;
11560 if (code == LT || code == NE)
11561 new_code = GET_CODE (op0);
11562 else
11563 new_code = reversed_comparison_code (op0, NULL);
11565 if (new_code != UNKNOWN)
11567 code = new_code;
11568 op0 = tem;
11569 op1 = tem1;
11570 continue;
11573 break;
11575 case IOR:
11576 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11577 iff X <= 0. */
11578 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11579 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11580 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11582 op0 = XEXP (op0, 1);
11583 code = (code == GE ? GT : LE);
11584 continue;
11586 break;
11588 case AND:
11589 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11590 will be converted to a ZERO_EXTRACT later. */
11591 if (const_op == 0 && equality_comparison_p
11592 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11593 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11595 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11596 XEXP (XEXP (op0, 0), 1));
11597 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11598 continue;
11601 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11602 zero and X is a comparison and C1 and C2 describe only bits set
11603 in STORE_FLAG_VALUE, we can compare with X. */
11604 if (const_op == 0 && equality_comparison_p
11605 && mode_width <= HOST_BITS_PER_WIDE_INT
11606 && CONST_INT_P (XEXP (op0, 1))
11607 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11608 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11609 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11610 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11612 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11613 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11614 if ((~STORE_FLAG_VALUE & mask) == 0
11615 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11616 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11617 && COMPARISON_P (tem))))
11619 op0 = XEXP (XEXP (op0, 0), 0);
11620 continue;
11624 /* If we are doing an equality comparison of an AND of a bit equal
11625 to the sign bit, replace this with a LT or GE comparison of
11626 the underlying value. */
11627 if (equality_comparison_p
11628 && const_op == 0
11629 && CONST_INT_P (XEXP (op0, 1))
11630 && mode_width <= HOST_BITS_PER_WIDE_INT
11631 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11632 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11634 op0 = XEXP (op0, 0);
11635 code = (code == EQ ? GE : LT);
11636 continue;
11639 /* If this AND operation is really a ZERO_EXTEND from a narrower
11640 mode, the constant fits within that mode, and this is either an
11641 equality or unsigned comparison, try to do this comparison in
11642 the narrower mode.
11644 Note that in:
11646 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11647 -> (ne:DI (reg:SI 4) (const_int 0))
11649 unless TRULY_NOOP_TRUNCATION allows it or the register is
11650 known to hold a value of the required mode the
11651 transformation is invalid. */
11652 if ((equality_comparison_p || unsigned_comparison_p)
11653 && CONST_INT_P (XEXP (op0, 1))
11654 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11655 & GET_MODE_MASK (mode))
11656 + 1)) >= 0
11657 && const_op >> i == 0
11658 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11659 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11660 || (REG_P (XEXP (op0, 0))
11661 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11663 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11664 continue;
11667 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11668 fits in both M1 and M2 and the SUBREG is either paradoxical
11669 or represents the low part, permute the SUBREG and the AND
11670 and try again. */
11671 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11673 unsigned HOST_WIDE_INT c1;
11674 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11675 /* Require an integral mode, to avoid creating something like
11676 (AND:SF ...). */
11677 if (SCALAR_INT_MODE_P (tmode)
11678 /* It is unsafe to commute the AND into the SUBREG if the
11679 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11680 not defined. As originally written the upper bits
11681 have a defined value due to the AND operation.
11682 However, if we commute the AND inside the SUBREG then
11683 they no longer have defined values and the meaning of
11684 the code has been changed. */
11685 && (0
11686 #ifdef WORD_REGISTER_OPERATIONS
11687 || (mode_width > GET_MODE_PRECISION (tmode)
11688 && mode_width <= BITS_PER_WORD)
11689 #endif
11690 || (mode_width <= GET_MODE_PRECISION (tmode)
11691 && subreg_lowpart_p (XEXP (op0, 0))))
11692 && CONST_INT_P (XEXP (op0, 1))
11693 && mode_width <= HOST_BITS_PER_WIDE_INT
11694 && HWI_COMPUTABLE_MODE_P (tmode)
11695 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11696 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11697 && c1 != mask
11698 && c1 != GET_MODE_MASK (tmode))
11700 op0 = simplify_gen_binary (AND, tmode,
11701 SUBREG_REG (XEXP (op0, 0)),
11702 gen_int_mode (c1, tmode));
11703 op0 = gen_lowpart (mode, op0);
11704 continue;
11708 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11709 if (const_op == 0 && equality_comparison_p
11710 && XEXP (op0, 1) == const1_rtx
11711 && GET_CODE (XEXP (op0, 0)) == NOT)
11713 op0 = simplify_and_const_int (NULL_RTX, mode,
11714 XEXP (XEXP (op0, 0), 0), 1);
11715 code = (code == NE ? EQ : NE);
11716 continue;
11719 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11720 (eq (and (lshiftrt X) 1) 0).
11721 Also handle the case where (not X) is expressed using xor. */
11722 if (const_op == 0 && equality_comparison_p
11723 && XEXP (op0, 1) == const1_rtx
11724 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11726 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11727 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11729 if (GET_CODE (shift_op) == NOT
11730 || (GET_CODE (shift_op) == XOR
11731 && CONST_INT_P (XEXP (shift_op, 1))
11732 && CONST_INT_P (shift_count)
11733 && HWI_COMPUTABLE_MODE_P (mode)
11734 && (UINTVAL (XEXP (shift_op, 1))
11735 == (unsigned HOST_WIDE_INT) 1
11736 << INTVAL (shift_count))))
11739 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11740 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11741 code = (code == NE ? EQ : NE);
11742 continue;
11745 break;
11747 case ASHIFT:
11748 /* If we have (compare (ashift FOO N) (const_int C)) and
11749 the high order N bits of FOO (N+1 if an inequality comparison)
11750 are known to be zero, we can do this by comparing FOO with C
11751 shifted right N bits so long as the low-order N bits of C are
11752 zero. */
11753 if (CONST_INT_P (XEXP (op0, 1))
11754 && INTVAL (XEXP (op0, 1)) >= 0
11755 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11756 < HOST_BITS_PER_WIDE_INT)
11757 && (((unsigned HOST_WIDE_INT) const_op
11758 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11759 - 1)) == 0)
11760 && mode_width <= HOST_BITS_PER_WIDE_INT
11761 && (nonzero_bits (XEXP (op0, 0), mode)
11762 & ~(mask >> (INTVAL (XEXP (op0, 1))
11763 + ! equality_comparison_p))) == 0)
11765 /* We must perform a logical shift, not an arithmetic one,
11766 as we want the top N bits of C to be zero. */
11767 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11769 temp >>= INTVAL (XEXP (op0, 1));
11770 op1 = gen_int_mode (temp, mode);
11771 op0 = XEXP (op0, 0);
11772 continue;
11775 /* If we are doing a sign bit comparison, it means we are testing
11776 a particular bit. Convert it to the appropriate AND. */
11777 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11778 && mode_width <= HOST_BITS_PER_WIDE_INT)
11780 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11781 ((unsigned HOST_WIDE_INT) 1
11782 << (mode_width - 1
11783 - INTVAL (XEXP (op0, 1)))));
11784 code = (code == LT ? NE : EQ);
11785 continue;
11788 /* If this an equality comparison with zero and we are shifting
11789 the low bit to the sign bit, we can convert this to an AND of the
11790 low-order bit. */
11791 if (const_op == 0 && equality_comparison_p
11792 && CONST_INT_P (XEXP (op0, 1))
11793 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11795 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11796 continue;
11798 break;
11800 case ASHIFTRT:
11801 /* If this is an equality comparison with zero, we can do this
11802 as a logical shift, which might be much simpler. */
11803 if (equality_comparison_p && const_op == 0
11804 && CONST_INT_P (XEXP (op0, 1)))
11806 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11807 XEXP (op0, 0),
11808 INTVAL (XEXP (op0, 1)));
11809 continue;
11812 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11813 do the comparison in a narrower mode. */
11814 if (! unsigned_comparison_p
11815 && CONST_INT_P (XEXP (op0, 1))
11816 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11817 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11818 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11819 MODE_INT, 1)) != BLKmode
11820 && (((unsigned HOST_WIDE_INT) const_op
11821 + (GET_MODE_MASK (tmode) >> 1) + 1)
11822 <= GET_MODE_MASK (tmode)))
11824 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11825 continue;
11828 /* Likewise if OP0 is a PLUS of a sign extension with a
11829 constant, which is usually represented with the PLUS
11830 between the shifts. */
11831 if (! unsigned_comparison_p
11832 && CONST_INT_P (XEXP (op0, 1))
11833 && GET_CODE (XEXP (op0, 0)) == PLUS
11834 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11835 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11836 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11837 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11838 MODE_INT, 1)) != BLKmode
11839 && (((unsigned HOST_WIDE_INT) const_op
11840 + (GET_MODE_MASK (tmode) >> 1) + 1)
11841 <= GET_MODE_MASK (tmode)))
11843 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11844 rtx add_const = XEXP (XEXP (op0, 0), 1);
11845 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11846 add_const, XEXP (op0, 1));
11848 op0 = simplify_gen_binary (PLUS, tmode,
11849 gen_lowpart (tmode, inner),
11850 new_const);
11851 continue;
11854 /* ... fall through ... */
11855 case LSHIFTRT:
11856 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11857 the low order N bits of FOO are known to be zero, we can do this
11858 by comparing FOO with C shifted left N bits so long as no
11859 overflow occurs. Even if the low order N bits of FOO aren't known
11860 to be zero, if the comparison is >= or < we can use the same
11861 optimization and for > or <= by setting all the low
11862 order N bits in the comparison constant. */
11863 if (CONST_INT_P (XEXP (op0, 1))
11864 && INTVAL (XEXP (op0, 1)) > 0
11865 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11866 && mode_width <= HOST_BITS_PER_WIDE_INT
11867 && (((unsigned HOST_WIDE_INT) const_op
11868 + (GET_CODE (op0) != LSHIFTRT
11869 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11870 + 1)
11871 : 0))
11872 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11874 unsigned HOST_WIDE_INT low_bits
11875 = (nonzero_bits (XEXP (op0, 0), mode)
11876 & (((unsigned HOST_WIDE_INT) 1
11877 << INTVAL (XEXP (op0, 1))) - 1));
11878 if (low_bits == 0 || !equality_comparison_p)
11880 /* If the shift was logical, then we must make the condition
11881 unsigned. */
11882 if (GET_CODE (op0) == LSHIFTRT)
11883 code = unsigned_condition (code);
11885 const_op <<= INTVAL (XEXP (op0, 1));
11886 if (low_bits != 0
11887 && (code == GT || code == GTU
11888 || code == LE || code == LEU))
11889 const_op
11890 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11891 op1 = GEN_INT (const_op);
11892 op0 = XEXP (op0, 0);
11893 continue;
11897 /* If we are using this shift to extract just the sign bit, we
11898 can replace this with an LT or GE comparison. */
11899 if (const_op == 0
11900 && (equality_comparison_p || sign_bit_comparison_p)
11901 && CONST_INT_P (XEXP (op0, 1))
11902 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11904 op0 = XEXP (op0, 0);
11905 code = (code == NE || code == GT ? LT : GE);
11906 continue;
11908 break;
11910 default:
11911 break;
11914 break;
11917 /* Now make any compound operations involved in this comparison. Then,
11918 check for an outmost SUBREG on OP0 that is not doing anything or is
11919 paradoxical. The latter transformation must only be performed when
11920 it is known that the "extra" bits will be the same in op0 and op1 or
11921 that they don't matter. There are three cases to consider:
11923 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11924 care bits and we can assume they have any convenient value. So
11925 making the transformation is safe.
11927 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11928 In this case the upper bits of op0 are undefined. We should not make
11929 the simplification in that case as we do not know the contents of
11930 those bits.
11932 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11933 UNKNOWN. In that case we know those bits are zeros or ones. We must
11934 also be sure that they are the same as the upper bits of op1.
11936 We can never remove a SUBREG for a non-equality comparison because
11937 the sign bit is in a different place in the underlying object. */
11939 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11940 op1 = make_compound_operation (op1, SET);
11942 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11943 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11944 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11945 && (code == NE || code == EQ))
11947 if (paradoxical_subreg_p (op0))
11949 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11950 implemented. */
11951 if (REG_P (SUBREG_REG (op0)))
11953 op0 = SUBREG_REG (op0);
11954 op1 = gen_lowpart (GET_MODE (op0), op1);
11957 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
11958 <= HOST_BITS_PER_WIDE_INT)
11959 && (nonzero_bits (SUBREG_REG (op0),
11960 GET_MODE (SUBREG_REG (op0)))
11961 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11963 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11965 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11966 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11967 op0 = SUBREG_REG (op0), op1 = tem;
11971 /* We now do the opposite procedure: Some machines don't have compare
11972 insns in all modes. If OP0's mode is an integer mode smaller than a
11973 word and we can't do a compare in that mode, see if there is a larger
11974 mode for which we can do the compare. There are a number of cases in
11975 which we can use the wider mode. */
11977 mode = GET_MODE (op0);
11978 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11979 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11980 && ! have_insn_for (COMPARE, mode))
11981 for (tmode = GET_MODE_WIDER_MODE (mode);
11982 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
11983 tmode = GET_MODE_WIDER_MODE (tmode))
11984 if (have_insn_for (COMPARE, tmode))
11986 int zero_extended;
11988 /* If this is a test for negative, we can make an explicit
11989 test of the sign bit. Test this first so we can use
11990 a paradoxical subreg to extend OP0. */
11992 if (op1 == const0_rtx && (code == LT || code == GE)
11993 && HWI_COMPUTABLE_MODE_P (mode))
11995 unsigned HOST_WIDE_INT sign
11996 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
11997 op0 = simplify_gen_binary (AND, tmode,
11998 gen_lowpart (tmode, op0),
11999 gen_int_mode (sign, tmode));
12000 code = (code == LT) ? NE : EQ;
12001 break;
12004 /* If the only nonzero bits in OP0 and OP1 are those in the
12005 narrower mode and this is an equality or unsigned comparison,
12006 we can use the wider mode. Similarly for sign-extended
12007 values, in which case it is true for all comparisons. */
12008 zero_extended = ((code == EQ || code == NE
12009 || code == GEU || code == GTU
12010 || code == LEU || code == LTU)
12011 && (nonzero_bits (op0, tmode)
12012 & ~GET_MODE_MASK (mode)) == 0
12013 && ((CONST_INT_P (op1)
12014 || (nonzero_bits (op1, tmode)
12015 & ~GET_MODE_MASK (mode)) == 0)));
12017 if (zero_extended
12018 || ((num_sign_bit_copies (op0, tmode)
12019 > (unsigned int) (GET_MODE_PRECISION (tmode)
12020 - GET_MODE_PRECISION (mode)))
12021 && (num_sign_bit_copies (op1, tmode)
12022 > (unsigned int) (GET_MODE_PRECISION (tmode)
12023 - GET_MODE_PRECISION (mode)))))
12025 /* If OP0 is an AND and we don't have an AND in MODE either,
12026 make a new AND in the proper mode. */
12027 if (GET_CODE (op0) == AND
12028 && !have_insn_for (AND, mode))
12029 op0 = simplify_gen_binary (AND, tmode,
12030 gen_lowpart (tmode,
12031 XEXP (op0, 0)),
12032 gen_lowpart (tmode,
12033 XEXP (op0, 1)));
12034 else
12036 if (zero_extended)
12038 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12039 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12041 else
12043 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12044 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12046 break;
12051 /* We may have changed the comparison operands. Re-canonicalize. */
12052 if (swap_commutative_operands_p (op0, op1))
12054 tem = op0, op0 = op1, op1 = tem;
12055 code = swap_condition (code);
12058 /* If this machine only supports a subset of valid comparisons, see if we
12059 can convert an unsupported one into a supported one. */
12060 target_canonicalize_comparison (&code, &op0, &op1, 0);
12062 *pop0 = op0;
12063 *pop1 = op1;
12065 return code;
12068 /* Utility function for record_value_for_reg. Count number of
12069 rtxs in X. */
12070 static int
12071 count_rtxs (rtx x)
12073 enum rtx_code code = GET_CODE (x);
12074 const char *fmt;
12075 int i, j, ret = 1;
12077 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12078 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12080 rtx x0 = XEXP (x, 0);
12081 rtx x1 = XEXP (x, 1);
12083 if (x0 == x1)
12084 return 1 + 2 * count_rtxs (x0);
12086 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12087 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12088 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12089 return 2 + 2 * count_rtxs (x0)
12090 + count_rtxs (x == XEXP (x1, 0)
12091 ? XEXP (x1, 1) : XEXP (x1, 0));
12093 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12094 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12095 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12096 return 2 + 2 * count_rtxs (x1)
12097 + count_rtxs (x == XEXP (x0, 0)
12098 ? XEXP (x0, 1) : XEXP (x0, 0));
12101 fmt = GET_RTX_FORMAT (code);
12102 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12103 if (fmt[i] == 'e')
12104 ret += count_rtxs (XEXP (x, i));
12105 else if (fmt[i] == 'E')
12106 for (j = 0; j < XVECLEN (x, i); j++)
12107 ret += count_rtxs (XVECEXP (x, i, j));
12109 return ret;
12112 /* Utility function for following routine. Called when X is part of a value
12113 being stored into last_set_value. Sets last_set_table_tick
12114 for each register mentioned. Similar to mention_regs in cse.c */
12116 static void
12117 update_table_tick (rtx x)
12119 enum rtx_code code = GET_CODE (x);
12120 const char *fmt = GET_RTX_FORMAT (code);
12121 int i, j;
12123 if (code == REG)
12125 unsigned int regno = REGNO (x);
12126 unsigned int endregno = END_REGNO (x);
12127 unsigned int r;
12129 for (r = regno; r < endregno; r++)
12131 reg_stat_type *rsp = &reg_stat[r];
12132 rsp->last_set_table_tick = label_tick;
12135 return;
12138 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12139 if (fmt[i] == 'e')
12141 /* Check for identical subexpressions. If x contains
12142 identical subexpression we only have to traverse one of
12143 them. */
12144 if (i == 0 && ARITHMETIC_P (x))
12146 /* Note that at this point x1 has already been
12147 processed. */
12148 rtx x0 = XEXP (x, 0);
12149 rtx x1 = XEXP (x, 1);
12151 /* If x0 and x1 are identical then there is no need to
12152 process x0. */
12153 if (x0 == x1)
12154 break;
12156 /* If x0 is identical to a subexpression of x1 then while
12157 processing x1, x0 has already been processed. Thus we
12158 are done with x. */
12159 if (ARITHMETIC_P (x1)
12160 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12161 break;
12163 /* If x1 is identical to a subexpression of x0 then we
12164 still have to process the rest of x0. */
12165 if (ARITHMETIC_P (x0)
12166 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12168 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12169 break;
12173 update_table_tick (XEXP (x, i));
12175 else if (fmt[i] == 'E')
12176 for (j = 0; j < XVECLEN (x, i); j++)
12177 update_table_tick (XVECEXP (x, i, j));
12180 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12181 are saying that the register is clobbered and we no longer know its
12182 value. If INSN is zero, don't update reg_stat[].last_set; this is
12183 only permitted with VALUE also zero and is used to invalidate the
12184 register. */
12186 static void
12187 record_value_for_reg (rtx reg, rtx insn, rtx value)
12189 unsigned int regno = REGNO (reg);
12190 unsigned int endregno = END_REGNO (reg);
12191 unsigned int i;
12192 reg_stat_type *rsp;
12194 /* If VALUE contains REG and we have a previous value for REG, substitute
12195 the previous value. */
12196 if (value && insn && reg_overlap_mentioned_p (reg, value))
12198 rtx tem;
12200 /* Set things up so get_last_value is allowed to see anything set up to
12201 our insn. */
12202 subst_low_luid = DF_INSN_LUID (insn);
12203 tem = get_last_value (reg);
12205 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12206 it isn't going to be useful and will take a lot of time to process,
12207 so just use the CLOBBER. */
12209 if (tem)
12211 if (ARITHMETIC_P (tem)
12212 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12213 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12214 tem = XEXP (tem, 0);
12215 else if (count_occurrences (value, reg, 1) >= 2)
12217 /* If there are two or more occurrences of REG in VALUE,
12218 prevent the value from growing too much. */
12219 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12220 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12223 value = replace_rtx (copy_rtx (value), reg, tem);
12227 /* For each register modified, show we don't know its value, that
12228 we don't know about its bitwise content, that its value has been
12229 updated, and that we don't know the location of the death of the
12230 register. */
12231 for (i = regno; i < endregno; i++)
12233 rsp = &reg_stat[i];
12235 if (insn)
12236 rsp->last_set = insn;
12238 rsp->last_set_value = 0;
12239 rsp->last_set_mode = VOIDmode;
12240 rsp->last_set_nonzero_bits = 0;
12241 rsp->last_set_sign_bit_copies = 0;
12242 rsp->last_death = 0;
12243 rsp->truncated_to_mode = VOIDmode;
12246 /* Mark registers that are being referenced in this value. */
12247 if (value)
12248 update_table_tick (value);
12250 /* Now update the status of each register being set.
12251 If someone is using this register in this block, set this register
12252 to invalid since we will get confused between the two lives in this
12253 basic block. This makes using this register always invalid. In cse, we
12254 scan the table to invalidate all entries using this register, but this
12255 is too much work for us. */
12257 for (i = regno; i < endregno; i++)
12259 rsp = &reg_stat[i];
12260 rsp->last_set_label = label_tick;
12261 if (!insn
12262 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12263 rsp->last_set_invalid = 1;
12264 else
12265 rsp->last_set_invalid = 0;
12268 /* The value being assigned might refer to X (like in "x++;"). In that
12269 case, we must replace it with (clobber (const_int 0)) to prevent
12270 infinite loops. */
12271 rsp = &reg_stat[regno];
12272 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12274 value = copy_rtx (value);
12275 if (!get_last_value_validate (&value, insn, label_tick, 1))
12276 value = 0;
12279 /* For the main register being modified, update the value, the mode, the
12280 nonzero bits, and the number of sign bit copies. */
12282 rsp->last_set_value = value;
12284 if (value)
12286 enum machine_mode mode = GET_MODE (reg);
12287 subst_low_luid = DF_INSN_LUID (insn);
12288 rsp->last_set_mode = mode;
12289 if (GET_MODE_CLASS (mode) == MODE_INT
12290 && HWI_COMPUTABLE_MODE_P (mode))
12291 mode = nonzero_bits_mode;
12292 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12293 rsp->last_set_sign_bit_copies
12294 = num_sign_bit_copies (value, GET_MODE (reg));
12298 /* Called via note_stores from record_dead_and_set_regs to handle one
12299 SET or CLOBBER in an insn. DATA is the instruction in which the
12300 set is occurring. */
12302 static void
12303 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12305 rtx record_dead_insn = (rtx) data;
12307 if (GET_CODE (dest) == SUBREG)
12308 dest = SUBREG_REG (dest);
12310 if (!record_dead_insn)
12312 if (REG_P (dest))
12313 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12314 return;
12317 if (REG_P (dest))
12319 /* If we are setting the whole register, we know its value. Otherwise
12320 show that we don't know the value. We can handle SUBREG in
12321 some cases. */
12322 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12323 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12324 else if (GET_CODE (setter) == SET
12325 && GET_CODE (SET_DEST (setter)) == SUBREG
12326 && SUBREG_REG (SET_DEST (setter)) == dest
12327 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12328 && subreg_lowpart_p (SET_DEST (setter)))
12329 record_value_for_reg (dest, record_dead_insn,
12330 gen_lowpart (GET_MODE (dest),
12331 SET_SRC (setter)));
12332 else
12333 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12335 else if (MEM_P (dest)
12336 /* Ignore pushes, they clobber nothing. */
12337 && ! push_operand (dest, GET_MODE (dest)))
12338 mem_last_set = DF_INSN_LUID (record_dead_insn);
12341 /* Update the records of when each REG was most recently set or killed
12342 for the things done by INSN. This is the last thing done in processing
12343 INSN in the combiner loop.
12345 We update reg_stat[], in particular fields last_set, last_set_value,
12346 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12347 last_death, and also the similar information mem_last_set (which insn
12348 most recently modified memory) and last_call_luid (which insn was the
12349 most recent subroutine call). */
12351 static void
12352 record_dead_and_set_regs (rtx insn)
12354 rtx link;
12355 unsigned int i;
12357 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12359 if (REG_NOTE_KIND (link) == REG_DEAD
12360 && REG_P (XEXP (link, 0)))
12362 unsigned int regno = REGNO (XEXP (link, 0));
12363 unsigned int endregno = END_REGNO (XEXP (link, 0));
12365 for (i = regno; i < endregno; i++)
12367 reg_stat_type *rsp;
12369 rsp = &reg_stat[i];
12370 rsp->last_death = insn;
12373 else if (REG_NOTE_KIND (link) == REG_INC)
12374 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12377 if (CALL_P (insn))
12379 hard_reg_set_iterator hrsi;
12380 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12382 reg_stat_type *rsp;
12384 rsp = &reg_stat[i];
12385 rsp->last_set_invalid = 1;
12386 rsp->last_set = insn;
12387 rsp->last_set_value = 0;
12388 rsp->last_set_mode = VOIDmode;
12389 rsp->last_set_nonzero_bits = 0;
12390 rsp->last_set_sign_bit_copies = 0;
12391 rsp->last_death = 0;
12392 rsp->truncated_to_mode = VOIDmode;
12395 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12397 /* We can't combine into a call pattern. Remember, though, that
12398 the return value register is set at this LUID. We could
12399 still replace a register with the return value from the
12400 wrong subroutine call! */
12401 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12403 else
12404 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12407 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12408 register present in the SUBREG, so for each such SUBREG go back and
12409 adjust nonzero and sign bit information of the registers that are
12410 known to have some zero/sign bits set.
12412 This is needed because when combine blows the SUBREGs away, the
12413 information on zero/sign bits is lost and further combines can be
12414 missed because of that. */
12416 static void
12417 record_promoted_value (rtx insn, rtx subreg)
12419 struct insn_link *links;
12420 rtx set;
12421 unsigned int regno = REGNO (SUBREG_REG (subreg));
12422 enum machine_mode mode = GET_MODE (subreg);
12424 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12425 return;
12427 for (links = LOG_LINKS (insn); links;)
12429 reg_stat_type *rsp;
12431 insn = links->insn;
12432 set = single_set (insn);
12434 if (! set || !REG_P (SET_DEST (set))
12435 || REGNO (SET_DEST (set)) != regno
12436 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12438 links = links->next;
12439 continue;
12442 rsp = &reg_stat[regno];
12443 if (rsp->last_set == insn)
12445 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12446 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12449 if (REG_P (SET_SRC (set)))
12451 regno = REGNO (SET_SRC (set));
12452 links = LOG_LINKS (insn);
12454 else
12455 break;
12459 /* Check if X, a register, is known to contain a value already
12460 truncated to MODE. In this case we can use a subreg to refer to
12461 the truncated value even though in the generic case we would need
12462 an explicit truncation. */
12464 static bool
12465 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12467 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12468 enum machine_mode truncated = rsp->truncated_to_mode;
12470 if (truncated == 0
12471 || rsp->truncation_label < label_tick_ebb_start)
12472 return false;
12473 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12474 return true;
12475 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12476 return true;
12477 return false;
12480 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
12481 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
12482 might be able to turn a truncate into a subreg using this information.
12483 Return -1 if traversing *P is complete or 0 otherwise. */
12485 static int
12486 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12488 rtx x = *p;
12489 enum machine_mode truncated_mode;
12490 reg_stat_type *rsp;
12492 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12494 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12495 truncated_mode = GET_MODE (x);
12497 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12498 return -1;
12500 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12501 return -1;
12503 x = SUBREG_REG (x);
12505 /* ??? For hard-regs we now record everything. We might be able to
12506 optimize this using last_set_mode. */
12507 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12508 truncated_mode = GET_MODE (x);
12509 else
12510 return 0;
12512 rsp = &reg_stat[REGNO (x)];
12513 if (rsp->truncated_to_mode == 0
12514 || rsp->truncation_label < label_tick_ebb_start
12515 || (GET_MODE_SIZE (truncated_mode)
12516 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12518 rsp->truncated_to_mode = truncated_mode;
12519 rsp->truncation_label = label_tick;
12522 return -1;
12525 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12526 the modes they are used in. This can help truning TRUNCATEs into
12527 SUBREGs. */
12529 static void
12530 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12532 for_each_rtx (x, record_truncated_value, NULL);
12535 /* Scan X for promoted SUBREGs. For each one found,
12536 note what it implies to the registers used in it. */
12538 static void
12539 check_promoted_subreg (rtx insn, rtx x)
12541 if (GET_CODE (x) == SUBREG
12542 && SUBREG_PROMOTED_VAR_P (x)
12543 && REG_P (SUBREG_REG (x)))
12544 record_promoted_value (insn, x);
12545 else
12547 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12548 int i, j;
12550 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12551 switch (format[i])
12553 case 'e':
12554 check_promoted_subreg (insn, XEXP (x, i));
12555 break;
12556 case 'V':
12557 case 'E':
12558 if (XVEC (x, i) != 0)
12559 for (j = 0; j < XVECLEN (x, i); j++)
12560 check_promoted_subreg (insn, XVECEXP (x, i, j));
12561 break;
12566 /* Verify that all the registers and memory references mentioned in *LOC are
12567 still valid. *LOC was part of a value set in INSN when label_tick was
12568 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12569 the invalid references with (clobber (const_int 0)) and return 1. This
12570 replacement is useful because we often can get useful information about
12571 the form of a value (e.g., if it was produced by a shift that always
12572 produces -1 or 0) even though we don't know exactly what registers it
12573 was produced from. */
12575 static int
12576 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12578 rtx x = *loc;
12579 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12580 int len = GET_RTX_LENGTH (GET_CODE (x));
12581 int i, j;
12583 if (REG_P (x))
12585 unsigned int regno = REGNO (x);
12586 unsigned int endregno = END_REGNO (x);
12587 unsigned int j;
12589 for (j = regno; j < endregno; j++)
12591 reg_stat_type *rsp = &reg_stat[j];
12592 if (rsp->last_set_invalid
12593 /* If this is a pseudo-register that was only set once and not
12594 live at the beginning of the function, it is always valid. */
12595 || (! (regno >= FIRST_PSEUDO_REGISTER
12596 && REG_N_SETS (regno) == 1
12597 && (!REGNO_REG_SET_P
12598 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12599 regno)))
12600 && rsp->last_set_label > tick))
12602 if (replace)
12603 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12604 return replace;
12608 return 1;
12610 /* If this is a memory reference, make sure that there were no stores after
12611 it that might have clobbered the value. We don't have alias info, so we
12612 assume any store invalidates it. Moreover, we only have local UIDs, so
12613 we also assume that there were stores in the intervening basic blocks. */
12614 else if (MEM_P (x) && !MEM_READONLY_P (x)
12615 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12617 if (replace)
12618 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12619 return replace;
12622 for (i = 0; i < len; i++)
12624 if (fmt[i] == 'e')
12626 /* Check for identical subexpressions. If x contains
12627 identical subexpression we only have to traverse one of
12628 them. */
12629 if (i == 1 && ARITHMETIC_P (x))
12631 /* Note that at this point x0 has already been checked
12632 and found valid. */
12633 rtx x0 = XEXP (x, 0);
12634 rtx x1 = XEXP (x, 1);
12636 /* If x0 and x1 are identical then x is also valid. */
12637 if (x0 == x1)
12638 return 1;
12640 /* If x1 is identical to a subexpression of x0 then
12641 while checking x0, x1 has already been checked. Thus
12642 it is valid and so as x. */
12643 if (ARITHMETIC_P (x0)
12644 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12645 return 1;
12647 /* If x0 is identical to a subexpression of x1 then x is
12648 valid iff the rest of x1 is valid. */
12649 if (ARITHMETIC_P (x1)
12650 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12651 return
12652 get_last_value_validate (&XEXP (x1,
12653 x0 == XEXP (x1, 0) ? 1 : 0),
12654 insn, tick, replace);
12657 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12658 replace) == 0)
12659 return 0;
12661 else if (fmt[i] == 'E')
12662 for (j = 0; j < XVECLEN (x, i); j++)
12663 if (get_last_value_validate (&XVECEXP (x, i, j),
12664 insn, tick, replace) == 0)
12665 return 0;
12668 /* If we haven't found a reason for it to be invalid, it is valid. */
12669 return 1;
12672 /* Get the last value assigned to X, if known. Some registers
12673 in the value may be replaced with (clobber (const_int 0)) if their value
12674 is known longer known reliably. */
12676 static rtx
12677 get_last_value (const_rtx x)
12679 unsigned int regno;
12680 rtx value;
12681 reg_stat_type *rsp;
12683 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12684 then convert it to the desired mode. If this is a paradoxical SUBREG,
12685 we cannot predict what values the "extra" bits might have. */
12686 if (GET_CODE (x) == SUBREG
12687 && subreg_lowpart_p (x)
12688 && !paradoxical_subreg_p (x)
12689 && (value = get_last_value (SUBREG_REG (x))) != 0)
12690 return gen_lowpart (GET_MODE (x), value);
12692 if (!REG_P (x))
12693 return 0;
12695 regno = REGNO (x);
12696 rsp = &reg_stat[regno];
12697 value = rsp->last_set_value;
12699 /* If we don't have a value, or if it isn't for this basic block and
12700 it's either a hard register, set more than once, or it's a live
12701 at the beginning of the function, return 0.
12703 Because if it's not live at the beginning of the function then the reg
12704 is always set before being used (is never used without being set).
12705 And, if it's set only once, and it's always set before use, then all
12706 uses must have the same last value, even if it's not from this basic
12707 block. */
12709 if (value == 0
12710 || (rsp->last_set_label < label_tick_ebb_start
12711 && (regno < FIRST_PSEUDO_REGISTER
12712 || REG_N_SETS (regno) != 1
12713 || REGNO_REG_SET_P
12714 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12715 return 0;
12717 /* If the value was set in a later insn than the ones we are processing,
12718 we can't use it even if the register was only set once. */
12719 if (rsp->last_set_label == label_tick
12720 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12721 return 0;
12723 /* If the value has all its registers valid, return it. */
12724 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12725 return value;
12727 /* Otherwise, make a copy and replace any invalid register with
12728 (clobber (const_int 0)). If that fails for some reason, return 0. */
12730 value = copy_rtx (value);
12731 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12732 return value;
12734 return 0;
12737 /* Return nonzero if expression X refers to a REG or to memory
12738 that is set in an instruction more recent than FROM_LUID. */
12740 static int
12741 use_crosses_set_p (const_rtx x, int from_luid)
12743 const char *fmt;
12744 int i;
12745 enum rtx_code code = GET_CODE (x);
12747 if (code == REG)
12749 unsigned int regno = REGNO (x);
12750 unsigned endreg = END_REGNO (x);
12752 #ifdef PUSH_ROUNDING
12753 /* Don't allow uses of the stack pointer to be moved,
12754 because we don't know whether the move crosses a push insn. */
12755 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12756 return 1;
12757 #endif
12758 for (; regno < endreg; regno++)
12760 reg_stat_type *rsp = &reg_stat[regno];
12761 if (rsp->last_set
12762 && rsp->last_set_label == label_tick
12763 && DF_INSN_LUID (rsp->last_set) > from_luid)
12764 return 1;
12766 return 0;
12769 if (code == MEM && mem_last_set > from_luid)
12770 return 1;
12772 fmt = GET_RTX_FORMAT (code);
12774 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12776 if (fmt[i] == 'E')
12778 int j;
12779 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12780 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12781 return 1;
12783 else if (fmt[i] == 'e'
12784 && use_crosses_set_p (XEXP (x, i), from_luid))
12785 return 1;
12787 return 0;
12790 /* Define three variables used for communication between the following
12791 routines. */
12793 static unsigned int reg_dead_regno, reg_dead_endregno;
12794 static int reg_dead_flag;
12796 /* Function called via note_stores from reg_dead_at_p.
12798 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12799 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12801 static void
12802 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12804 unsigned int regno, endregno;
12806 if (!REG_P (dest))
12807 return;
12809 regno = REGNO (dest);
12810 endregno = END_REGNO (dest);
12811 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12812 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12815 /* Return nonzero if REG is known to be dead at INSN.
12817 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12818 referencing REG, it is dead. If we hit a SET referencing REG, it is
12819 live. Otherwise, see if it is live or dead at the start of the basic
12820 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12821 must be assumed to be always live. */
12823 static int
12824 reg_dead_at_p (rtx reg, rtx insn)
12826 basic_block block;
12827 unsigned int i;
12829 /* Set variables for reg_dead_at_p_1. */
12830 reg_dead_regno = REGNO (reg);
12831 reg_dead_endregno = END_REGNO (reg);
12833 reg_dead_flag = 0;
12835 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12836 we allow the machine description to decide whether use-and-clobber
12837 patterns are OK. */
12838 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12840 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12841 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12842 return 0;
12845 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12846 beginning of basic block. */
12847 block = BLOCK_FOR_INSN (insn);
12848 for (;;)
12850 if (INSN_P (insn))
12852 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12853 if (reg_dead_flag)
12854 return reg_dead_flag == 1 ? 1 : 0;
12856 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12857 return 1;
12860 if (insn == BB_HEAD (block))
12861 break;
12863 insn = PREV_INSN (insn);
12866 /* Look at live-in sets for the basic block that we were in. */
12867 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12868 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12869 return 0;
12871 return 1;
12874 /* Note hard registers in X that are used. */
12876 static void
12877 mark_used_regs_combine (rtx x)
12879 RTX_CODE code = GET_CODE (x);
12880 unsigned int regno;
12881 int i;
12883 switch (code)
12885 case LABEL_REF:
12886 case SYMBOL_REF:
12887 case CONST:
12888 CASE_CONST_ANY:
12889 case PC:
12890 case ADDR_VEC:
12891 case ADDR_DIFF_VEC:
12892 case ASM_INPUT:
12893 #ifdef HAVE_cc0
12894 /* CC0 must die in the insn after it is set, so we don't need to take
12895 special note of it here. */
12896 case CC0:
12897 #endif
12898 return;
12900 case CLOBBER:
12901 /* If we are clobbering a MEM, mark any hard registers inside the
12902 address as used. */
12903 if (MEM_P (XEXP (x, 0)))
12904 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12905 return;
12907 case REG:
12908 regno = REGNO (x);
12909 /* A hard reg in a wide mode may really be multiple registers.
12910 If so, mark all of them just like the first. */
12911 if (regno < FIRST_PSEUDO_REGISTER)
12913 /* None of this applies to the stack, frame or arg pointers. */
12914 if (regno == STACK_POINTER_REGNUM
12915 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12916 || regno == HARD_FRAME_POINTER_REGNUM
12917 #endif
12918 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12919 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12920 #endif
12921 || regno == FRAME_POINTER_REGNUM)
12922 return;
12924 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12926 return;
12928 case SET:
12930 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12931 the address. */
12932 rtx testreg = SET_DEST (x);
12934 while (GET_CODE (testreg) == SUBREG
12935 || GET_CODE (testreg) == ZERO_EXTRACT
12936 || GET_CODE (testreg) == STRICT_LOW_PART)
12937 testreg = XEXP (testreg, 0);
12939 if (MEM_P (testreg))
12940 mark_used_regs_combine (XEXP (testreg, 0));
12942 mark_used_regs_combine (SET_SRC (x));
12944 return;
12946 default:
12947 break;
12950 /* Recursively scan the operands of this expression. */
12953 const char *fmt = GET_RTX_FORMAT (code);
12955 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12957 if (fmt[i] == 'e')
12958 mark_used_regs_combine (XEXP (x, i));
12959 else if (fmt[i] == 'E')
12961 int j;
12963 for (j = 0; j < XVECLEN (x, i); j++)
12964 mark_used_regs_combine (XVECEXP (x, i, j));
12970 /* Remove register number REGNO from the dead registers list of INSN.
12972 Return the note used to record the death, if there was one. */
12975 remove_death (unsigned int regno, rtx insn)
12977 rtx note = find_regno_note (insn, REG_DEAD, regno);
12979 if (note)
12980 remove_note (insn, note);
12982 return note;
12985 /* For each register (hardware or pseudo) used within expression X, if its
12986 death is in an instruction with luid between FROM_LUID (inclusive) and
12987 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12988 list headed by PNOTES.
12990 That said, don't move registers killed by maybe_kill_insn.
12992 This is done when X is being merged by combination into TO_INSN. These
12993 notes will then be distributed as needed. */
12995 static void
12996 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12997 rtx *pnotes)
12999 const char *fmt;
13000 int len, i;
13001 enum rtx_code code = GET_CODE (x);
13003 if (code == REG)
13005 unsigned int regno = REGNO (x);
13006 rtx where_dead = reg_stat[regno].last_death;
13008 /* Don't move the register if it gets killed in between from and to. */
13009 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13010 && ! reg_referenced_p (x, maybe_kill_insn))
13011 return;
13013 if (where_dead
13014 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13015 && DF_INSN_LUID (where_dead) >= from_luid
13016 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13018 rtx note = remove_death (regno, where_dead);
13020 /* It is possible for the call above to return 0. This can occur
13021 when last_death points to I2 or I1 that we combined with.
13022 In that case make a new note.
13024 We must also check for the case where X is a hard register
13025 and NOTE is a death note for a range of hard registers
13026 including X. In that case, we must put REG_DEAD notes for
13027 the remaining registers in place of NOTE. */
13029 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13030 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13031 > GET_MODE_SIZE (GET_MODE (x))))
13033 unsigned int deadregno = REGNO (XEXP (note, 0));
13034 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13035 unsigned int ourend = END_HARD_REGNO (x);
13036 unsigned int i;
13038 for (i = deadregno; i < deadend; i++)
13039 if (i < regno || i >= ourend)
13040 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13043 /* If we didn't find any note, or if we found a REG_DEAD note that
13044 covers only part of the given reg, and we have a multi-reg hard
13045 register, then to be safe we must check for REG_DEAD notes
13046 for each register other than the first. They could have
13047 their own REG_DEAD notes lying around. */
13048 else if ((note == 0
13049 || (note != 0
13050 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13051 < GET_MODE_SIZE (GET_MODE (x)))))
13052 && regno < FIRST_PSEUDO_REGISTER
13053 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13055 unsigned int ourend = END_HARD_REGNO (x);
13056 unsigned int i, offset;
13057 rtx oldnotes = 0;
13059 if (note)
13060 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13061 else
13062 offset = 1;
13064 for (i = regno + offset; i < ourend; i++)
13065 move_deaths (regno_reg_rtx[i],
13066 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13069 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13071 XEXP (note, 1) = *pnotes;
13072 *pnotes = note;
13074 else
13075 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13078 return;
13081 else if (GET_CODE (x) == SET)
13083 rtx dest = SET_DEST (x);
13085 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13087 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13088 that accesses one word of a multi-word item, some
13089 piece of everything register in the expression is used by
13090 this insn, so remove any old death. */
13091 /* ??? So why do we test for equality of the sizes? */
13093 if (GET_CODE (dest) == ZERO_EXTRACT
13094 || GET_CODE (dest) == STRICT_LOW_PART
13095 || (GET_CODE (dest) == SUBREG
13096 && (((GET_MODE_SIZE (GET_MODE (dest))
13097 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13098 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13099 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13101 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13102 return;
13105 /* If this is some other SUBREG, we know it replaces the entire
13106 value, so use that as the destination. */
13107 if (GET_CODE (dest) == SUBREG)
13108 dest = SUBREG_REG (dest);
13110 /* If this is a MEM, adjust deaths of anything used in the address.
13111 For a REG (the only other possibility), the entire value is
13112 being replaced so the old value is not used in this insn. */
13114 if (MEM_P (dest))
13115 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13116 to_insn, pnotes);
13117 return;
13120 else if (GET_CODE (x) == CLOBBER)
13121 return;
13123 len = GET_RTX_LENGTH (code);
13124 fmt = GET_RTX_FORMAT (code);
13126 for (i = 0; i < len; i++)
13128 if (fmt[i] == 'E')
13130 int j;
13131 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13132 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13133 to_insn, pnotes);
13135 else if (fmt[i] == 'e')
13136 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13140 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13141 pattern of an insn. X must be a REG. */
13143 static int
13144 reg_bitfield_target_p (rtx x, rtx body)
13146 int i;
13148 if (GET_CODE (body) == SET)
13150 rtx dest = SET_DEST (body);
13151 rtx target;
13152 unsigned int regno, tregno, endregno, endtregno;
13154 if (GET_CODE (dest) == ZERO_EXTRACT)
13155 target = XEXP (dest, 0);
13156 else if (GET_CODE (dest) == STRICT_LOW_PART)
13157 target = SUBREG_REG (XEXP (dest, 0));
13158 else
13159 return 0;
13161 if (GET_CODE (target) == SUBREG)
13162 target = SUBREG_REG (target);
13164 if (!REG_P (target))
13165 return 0;
13167 tregno = REGNO (target), regno = REGNO (x);
13168 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13169 return target == x;
13171 endtregno = end_hard_regno (GET_MODE (target), tregno);
13172 endregno = end_hard_regno (GET_MODE (x), regno);
13174 return endregno > tregno && regno < endtregno;
13177 else if (GET_CODE (body) == PARALLEL)
13178 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13179 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13180 return 1;
13182 return 0;
13185 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13186 as appropriate. I3 and I2 are the insns resulting from the combination
13187 insns including FROM (I2 may be zero).
13189 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13190 not need REG_DEAD notes because they are being substituted for. This
13191 saves searching in the most common cases.
13193 Each note in the list is either ignored or placed on some insns, depending
13194 on the type of note. */
13196 static void
13197 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13198 rtx elim_i1, rtx elim_i0)
13200 rtx note, next_note;
13201 rtx tem;
13203 for (note = notes; note; note = next_note)
13205 rtx place = 0, place2 = 0;
13207 next_note = XEXP (note, 1);
13208 switch (REG_NOTE_KIND (note))
13210 case REG_BR_PROB:
13211 case REG_BR_PRED:
13212 /* Doesn't matter much where we put this, as long as it's somewhere.
13213 It is preferable to keep these notes on branches, which is most
13214 likely to be i3. */
13215 place = i3;
13216 break;
13218 case REG_NON_LOCAL_GOTO:
13219 if (JUMP_P (i3))
13220 place = i3;
13221 else
13223 gcc_assert (i2 && JUMP_P (i2));
13224 place = i2;
13226 break;
13228 case REG_EH_REGION:
13229 /* These notes must remain with the call or trapping instruction. */
13230 if (CALL_P (i3))
13231 place = i3;
13232 else if (i2 && CALL_P (i2))
13233 place = i2;
13234 else
13236 gcc_assert (cfun->can_throw_non_call_exceptions);
13237 if (may_trap_p (i3))
13238 place = i3;
13239 else if (i2 && may_trap_p (i2))
13240 place = i2;
13241 /* ??? Otherwise assume we've combined things such that we
13242 can now prove that the instructions can't trap. Drop the
13243 note in this case. */
13245 break;
13247 case REG_ARGS_SIZE:
13248 /* ??? How to distribute between i3-i1. Assume i3 contains the
13249 entire adjustment. Assert i3 contains at least some adjust. */
13250 if (!noop_move_p (i3))
13252 int old_size, args_size = INTVAL (XEXP (note, 0));
13253 /* fixup_args_size_notes looks at REG_NORETURN note,
13254 so ensure the note is placed there first. */
13255 if (CALL_P (i3))
13257 rtx *np;
13258 for (np = &next_note; *np; np = &XEXP (*np, 1))
13259 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13261 rtx n = *np;
13262 *np = XEXP (n, 1);
13263 XEXP (n, 1) = REG_NOTES (i3);
13264 REG_NOTES (i3) = n;
13265 break;
13268 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13269 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13270 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13271 gcc_assert (old_size != args_size
13272 || (CALL_P (i3)
13273 && !ACCUMULATE_OUTGOING_ARGS
13274 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13276 break;
13278 case REG_NORETURN:
13279 case REG_SETJMP:
13280 case REG_TM:
13281 /* These notes must remain with the call. It should not be
13282 possible for both I2 and I3 to be a call. */
13283 if (CALL_P (i3))
13284 place = i3;
13285 else
13287 gcc_assert (i2 && CALL_P (i2));
13288 place = i2;
13290 break;
13292 case REG_UNUSED:
13293 /* Any clobbers for i3 may still exist, and so we must process
13294 REG_UNUSED notes from that insn.
13296 Any clobbers from i2 or i1 can only exist if they were added by
13297 recog_for_combine. In that case, recog_for_combine created the
13298 necessary REG_UNUSED notes. Trying to keep any original
13299 REG_UNUSED notes from these insns can cause incorrect output
13300 if it is for the same register as the original i3 dest.
13301 In that case, we will notice that the register is set in i3,
13302 and then add a REG_UNUSED note for the destination of i3, which
13303 is wrong. However, it is possible to have REG_UNUSED notes from
13304 i2 or i1 for register which were both used and clobbered, so
13305 we keep notes from i2 or i1 if they will turn into REG_DEAD
13306 notes. */
13308 /* If this register is set or clobbered in I3, put the note there
13309 unless there is one already. */
13310 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13312 if (from_insn != i3)
13313 break;
13315 if (! (REG_P (XEXP (note, 0))
13316 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13317 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13318 place = i3;
13320 /* Otherwise, if this register is used by I3, then this register
13321 now dies here, so we must put a REG_DEAD note here unless there
13322 is one already. */
13323 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13324 && ! (REG_P (XEXP (note, 0))
13325 ? find_regno_note (i3, REG_DEAD,
13326 REGNO (XEXP (note, 0)))
13327 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13329 PUT_REG_NOTE_KIND (note, REG_DEAD);
13330 place = i3;
13332 break;
13334 case REG_EQUAL:
13335 case REG_EQUIV:
13336 case REG_NOALIAS:
13337 /* These notes say something about results of an insn. We can
13338 only support them if they used to be on I3 in which case they
13339 remain on I3. Otherwise they are ignored.
13341 If the note refers to an expression that is not a constant, we
13342 must also ignore the note since we cannot tell whether the
13343 equivalence is still true. It might be possible to do
13344 slightly better than this (we only have a problem if I2DEST
13345 or I1DEST is present in the expression), but it doesn't
13346 seem worth the trouble. */
13348 if (from_insn == i3
13349 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13350 place = i3;
13351 break;
13353 case REG_INC:
13354 /* These notes say something about how a register is used. They must
13355 be present on any use of the register in I2 or I3. */
13356 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13357 place = i3;
13359 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13361 if (place)
13362 place2 = i2;
13363 else
13364 place = i2;
13366 break;
13368 case REG_LABEL_TARGET:
13369 case REG_LABEL_OPERAND:
13370 /* This can show up in several ways -- either directly in the
13371 pattern, or hidden off in the constant pool with (or without?)
13372 a REG_EQUAL note. */
13373 /* ??? Ignore the without-reg_equal-note problem for now. */
13374 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13375 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13376 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13377 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13378 place = i3;
13380 if (i2
13381 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13382 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13383 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13384 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13386 if (place)
13387 place2 = i2;
13388 else
13389 place = i2;
13392 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13393 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13394 there. */
13395 if (place && JUMP_P (place)
13396 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13397 && (JUMP_LABEL (place) == NULL
13398 || JUMP_LABEL (place) == XEXP (note, 0)))
13400 rtx label = JUMP_LABEL (place);
13402 if (!label)
13403 JUMP_LABEL (place) = XEXP (note, 0);
13404 else if (LABEL_P (label))
13405 LABEL_NUSES (label)--;
13408 if (place2 && JUMP_P (place2)
13409 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13410 && (JUMP_LABEL (place2) == NULL
13411 || JUMP_LABEL (place2) == XEXP (note, 0)))
13413 rtx label = JUMP_LABEL (place2);
13415 if (!label)
13416 JUMP_LABEL (place2) = XEXP (note, 0);
13417 else if (LABEL_P (label))
13418 LABEL_NUSES (label)--;
13419 place2 = 0;
13421 break;
13423 case REG_NONNEG:
13424 /* This note says something about the value of a register prior
13425 to the execution of an insn. It is too much trouble to see
13426 if the note is still correct in all situations. It is better
13427 to simply delete it. */
13428 break;
13430 case REG_DEAD:
13431 /* If we replaced the right hand side of FROM_INSN with a
13432 REG_EQUAL note, the original use of the dying register
13433 will not have been combined into I3 and I2. In such cases,
13434 FROM_INSN is guaranteed to be the first of the combined
13435 instructions, so we simply need to search back before
13436 FROM_INSN for the previous use or set of this register,
13437 then alter the notes there appropriately.
13439 If the register is used as an input in I3, it dies there.
13440 Similarly for I2, if it is nonzero and adjacent to I3.
13442 If the register is not used as an input in either I3 or I2
13443 and it is not one of the registers we were supposed to eliminate,
13444 there are two possibilities. We might have a non-adjacent I2
13445 or we might have somehow eliminated an additional register
13446 from a computation. For example, we might have had A & B where
13447 we discover that B will always be zero. In this case we will
13448 eliminate the reference to A.
13450 In both cases, we must search to see if we can find a previous
13451 use of A and put the death note there. */
13453 if (from_insn
13454 && from_insn == i2mod
13455 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13456 tem = from_insn;
13457 else
13459 if (from_insn
13460 && CALL_P (from_insn)
13461 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13462 place = from_insn;
13463 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13464 place = i3;
13465 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13466 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13467 place = i2;
13468 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13469 && !(i2mod
13470 && reg_overlap_mentioned_p (XEXP (note, 0),
13471 i2mod_old_rhs)))
13472 || rtx_equal_p (XEXP (note, 0), elim_i1)
13473 || rtx_equal_p (XEXP (note, 0), elim_i0))
13474 break;
13475 tem = i3;
13478 if (place == 0)
13480 basic_block bb = this_basic_block;
13482 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13484 if (!NONDEBUG_INSN_P (tem))
13486 if (tem == BB_HEAD (bb))
13487 break;
13488 continue;
13491 /* If the register is being set at TEM, see if that is all
13492 TEM is doing. If so, delete TEM. Otherwise, make this
13493 into a REG_UNUSED note instead. Don't delete sets to
13494 global register vars. */
13495 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13496 || !global_regs[REGNO (XEXP (note, 0))])
13497 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13499 rtx set = single_set (tem);
13500 rtx inner_dest = 0;
13501 #ifdef HAVE_cc0
13502 rtx cc0_setter = NULL_RTX;
13503 #endif
13505 if (set != 0)
13506 for (inner_dest = SET_DEST (set);
13507 (GET_CODE (inner_dest) == STRICT_LOW_PART
13508 || GET_CODE (inner_dest) == SUBREG
13509 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13510 inner_dest = XEXP (inner_dest, 0))
13513 /* Verify that it was the set, and not a clobber that
13514 modified the register.
13516 CC0 targets must be careful to maintain setter/user
13517 pairs. If we cannot delete the setter due to side
13518 effects, mark the user with an UNUSED note instead
13519 of deleting it. */
13521 if (set != 0 && ! side_effects_p (SET_SRC (set))
13522 && rtx_equal_p (XEXP (note, 0), inner_dest)
13523 #ifdef HAVE_cc0
13524 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13525 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13526 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13527 #endif
13530 /* Move the notes and links of TEM elsewhere.
13531 This might delete other dead insns recursively.
13532 First set the pattern to something that won't use
13533 any register. */
13534 rtx old_notes = REG_NOTES (tem);
13536 PATTERN (tem) = pc_rtx;
13537 REG_NOTES (tem) = NULL;
13539 distribute_notes (old_notes, tem, tem, NULL_RTX,
13540 NULL_RTX, NULL_RTX, NULL_RTX);
13541 distribute_links (LOG_LINKS (tem));
13543 SET_INSN_DELETED (tem);
13544 if (tem == i2)
13545 i2 = NULL_RTX;
13547 #ifdef HAVE_cc0
13548 /* Delete the setter too. */
13549 if (cc0_setter)
13551 PATTERN (cc0_setter) = pc_rtx;
13552 old_notes = REG_NOTES (cc0_setter);
13553 REG_NOTES (cc0_setter) = NULL;
13555 distribute_notes (old_notes, cc0_setter,
13556 cc0_setter, NULL_RTX,
13557 NULL_RTX, NULL_RTX, NULL_RTX);
13558 distribute_links (LOG_LINKS (cc0_setter));
13560 SET_INSN_DELETED (cc0_setter);
13561 if (cc0_setter == i2)
13562 i2 = NULL_RTX;
13564 #endif
13566 else
13568 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13570 /* If there isn't already a REG_UNUSED note, put one
13571 here. Do not place a REG_DEAD note, even if
13572 the register is also used here; that would not
13573 match the algorithm used in lifetime analysis
13574 and can cause the consistency check in the
13575 scheduler to fail. */
13576 if (! find_regno_note (tem, REG_UNUSED,
13577 REGNO (XEXP (note, 0))))
13578 place = tem;
13579 break;
13582 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13583 || (CALL_P (tem)
13584 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13586 place = tem;
13588 /* If we are doing a 3->2 combination, and we have a
13589 register which formerly died in i3 and was not used
13590 by i2, which now no longer dies in i3 and is used in
13591 i2 but does not die in i2, and place is between i2
13592 and i3, then we may need to move a link from place to
13593 i2. */
13594 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13595 && from_insn
13596 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13597 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13599 struct insn_link *links = LOG_LINKS (place);
13600 LOG_LINKS (place) = NULL;
13601 distribute_links (links);
13603 break;
13606 if (tem == BB_HEAD (bb))
13607 break;
13612 /* If the register is set or already dead at PLACE, we needn't do
13613 anything with this note if it is still a REG_DEAD note.
13614 We check here if it is set at all, not if is it totally replaced,
13615 which is what `dead_or_set_p' checks, so also check for it being
13616 set partially. */
13618 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13620 unsigned int regno = REGNO (XEXP (note, 0));
13621 reg_stat_type *rsp = &reg_stat[regno];
13623 if (dead_or_set_p (place, XEXP (note, 0))
13624 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13626 /* Unless the register previously died in PLACE, clear
13627 last_death. [I no longer understand why this is
13628 being done.] */
13629 if (rsp->last_death != place)
13630 rsp->last_death = 0;
13631 place = 0;
13633 else
13634 rsp->last_death = place;
13636 /* If this is a death note for a hard reg that is occupying
13637 multiple registers, ensure that we are still using all
13638 parts of the object. If we find a piece of the object
13639 that is unused, we must arrange for an appropriate REG_DEAD
13640 note to be added for it. However, we can't just emit a USE
13641 and tag the note to it, since the register might actually
13642 be dead; so we recourse, and the recursive call then finds
13643 the previous insn that used this register. */
13645 if (place && regno < FIRST_PSEUDO_REGISTER
13646 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13648 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13649 bool all_used = true;
13650 unsigned int i;
13652 for (i = regno; i < endregno; i++)
13653 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13654 && ! find_regno_fusage (place, USE, i))
13655 || dead_or_set_regno_p (place, i))
13657 all_used = false;
13658 break;
13661 if (! all_used)
13663 /* Put only REG_DEAD notes for pieces that are
13664 not already dead or set. */
13666 for (i = regno; i < endregno;
13667 i += hard_regno_nregs[i][reg_raw_mode[i]])
13669 rtx piece = regno_reg_rtx[i];
13670 basic_block bb = this_basic_block;
13672 if (! dead_or_set_p (place, piece)
13673 && ! reg_bitfield_target_p (piece,
13674 PATTERN (place)))
13676 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13677 NULL_RTX);
13679 distribute_notes (new_note, place, place,
13680 NULL_RTX, NULL_RTX, NULL_RTX,
13681 NULL_RTX);
13683 else if (! refers_to_regno_p (i, i + 1,
13684 PATTERN (place), 0)
13685 && ! find_regno_fusage (place, USE, i))
13686 for (tem = PREV_INSN (place); ;
13687 tem = PREV_INSN (tem))
13689 if (!NONDEBUG_INSN_P (tem))
13691 if (tem == BB_HEAD (bb))
13692 break;
13693 continue;
13695 if (dead_or_set_p (tem, piece)
13696 || reg_bitfield_target_p (piece,
13697 PATTERN (tem)))
13699 add_reg_note (tem, REG_UNUSED, piece);
13700 break;
13705 place = 0;
13709 break;
13711 default:
13712 /* Any other notes should not be present at this point in the
13713 compilation. */
13714 gcc_unreachable ();
13717 if (place)
13719 XEXP (note, 1) = REG_NOTES (place);
13720 REG_NOTES (place) = note;
13723 if (place2)
13724 add_shallow_copy_of_reg_note (place2, note);
13728 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13729 I3, I2, and I1 to new locations. This is also called to add a link
13730 pointing at I3 when I3's destination is changed. */
13732 static void
13733 distribute_links (struct insn_link *links)
13735 struct insn_link *link, *next_link;
13737 for (link = links; link; link = next_link)
13739 rtx place = 0;
13740 rtx insn;
13741 rtx set, reg;
13743 next_link = link->next;
13745 /* If the insn that this link points to is a NOTE or isn't a single
13746 set, ignore it. In the latter case, it isn't clear what we
13747 can do other than ignore the link, since we can't tell which
13748 register it was for. Such links wouldn't be used by combine
13749 anyway.
13751 It is not possible for the destination of the target of the link to
13752 have been changed by combine. The only potential of this is if we
13753 replace I3, I2, and I1 by I3 and I2. But in that case the
13754 destination of I2 also remains unchanged. */
13756 if (NOTE_P (link->insn)
13757 || (set = single_set (link->insn)) == 0)
13758 continue;
13760 reg = SET_DEST (set);
13761 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13762 || GET_CODE (reg) == STRICT_LOW_PART)
13763 reg = XEXP (reg, 0);
13765 /* A LOG_LINK is defined as being placed on the first insn that uses
13766 a register and points to the insn that sets the register. Start
13767 searching at the next insn after the target of the link and stop
13768 when we reach a set of the register or the end of the basic block.
13770 Note that this correctly handles the link that used to point from
13771 I3 to I2. Also note that not much searching is typically done here
13772 since most links don't point very far away. */
13774 for (insn = NEXT_INSN (link->insn);
13775 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13776 || BB_HEAD (this_basic_block->next_bb) != insn));
13777 insn = NEXT_INSN (insn))
13778 if (DEBUG_INSN_P (insn))
13779 continue;
13780 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13782 if (reg_referenced_p (reg, PATTERN (insn)))
13783 place = insn;
13784 break;
13786 else if (CALL_P (insn)
13787 && find_reg_fusage (insn, USE, reg))
13789 place = insn;
13790 break;
13792 else if (INSN_P (insn) && reg_set_p (reg, insn))
13793 break;
13795 /* If we found a place to put the link, place it there unless there
13796 is already a link to the same insn as LINK at that point. */
13798 if (place)
13800 struct insn_link *link2;
13802 FOR_EACH_LOG_LINK (link2, place)
13803 if (link2->insn == link->insn)
13804 break;
13806 if (link2 == NULL)
13808 link->next = LOG_LINKS (place);
13809 LOG_LINKS (place) = link;
13811 /* Set added_links_insn to the earliest insn we added a
13812 link to. */
13813 if (added_links_insn == 0
13814 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13815 added_links_insn = place;
13821 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13822 Check whether the expression pointer to by LOC is a register or
13823 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13824 Otherwise return zero. */
13826 static int
13827 unmentioned_reg_p_1 (rtx *loc, void *expr)
13829 rtx x = *loc;
13831 if (x != NULL_RTX
13832 && (REG_P (x) || MEM_P (x))
13833 && ! reg_mentioned_p (x, (rtx) expr))
13834 return 1;
13835 return 0;
13838 /* Check for any register or memory mentioned in EQUIV that is not
13839 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13840 of EXPR where some registers may have been replaced by constants. */
13842 static bool
13843 unmentioned_reg_p (rtx equiv, rtx expr)
13845 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13848 DEBUG_FUNCTION void
13849 dump_combine_stats (FILE *file)
13851 fprintf
13852 (file,
13853 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13854 combine_attempts, combine_merges, combine_extras, combine_successes);
13857 void
13858 dump_combine_total_stats (FILE *file)
13860 fprintf
13861 (file,
13862 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13863 total_attempts, total_merges, total_extras, total_successes);
13866 static bool
13867 gate_handle_combine (void)
13869 return (optimize > 0);
13872 /* Try combining insns through substitution. */
13873 static unsigned int
13874 rest_of_handle_combine (void)
13876 int rebuild_jump_labels_after_combine;
13878 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13879 df_note_add_problem ();
13880 df_analyze ();
13882 regstat_init_n_sets_and_refs ();
13884 rebuild_jump_labels_after_combine
13885 = combine_instructions (get_insns (), max_reg_num ());
13887 /* Combining insns may have turned an indirect jump into a
13888 direct jump. Rebuild the JUMP_LABEL fields of jumping
13889 instructions. */
13890 if (rebuild_jump_labels_after_combine)
13892 timevar_push (TV_JUMP);
13893 rebuild_jump_labels (get_insns ());
13894 cleanup_cfg (0);
13895 timevar_pop (TV_JUMP);
13898 regstat_free_n_sets_and_refs ();
13899 return 0;
13902 namespace {
13904 const pass_data pass_data_combine =
13906 RTL_PASS, /* type */
13907 "combine", /* name */
13908 OPTGROUP_NONE, /* optinfo_flags */
13909 true, /* has_gate */
13910 true, /* has_execute */
13911 TV_COMBINE, /* tv_id */
13912 PROP_cfglayout, /* properties_required */
13913 0, /* properties_provided */
13914 0, /* properties_destroyed */
13915 0, /* todo_flags_start */
13916 ( TODO_df_finish | TODO_verify_rtl_sharing ), /* todo_flags_finish */
13919 class pass_combine : public rtl_opt_pass
13921 public:
13922 pass_combine (gcc::context *ctxt)
13923 : rtl_opt_pass (pass_data_combine, ctxt)
13926 /* opt_pass methods: */
13927 bool gate () { return gate_handle_combine (); }
13928 unsigned int execute () { return rest_of_handle_combine (); }
13930 }; // class pass_combine
13932 } // anon namespace
13934 rtl_opt_pass *
13935 make_pass_combine (gcc::context *ctxt)
13937 return new pass_combine (ctxt);