PR c++/18747
[official-gcc.git] / gcc / combine.c
blob18b79623b1caef3b422d03a3c7e93c6cb9885032
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
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 "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "diagnostic-core.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 #include "params.h"
101 #include "tree-pass.h"
102 #include "df.h"
103 #include "valtrack.h"
104 #include "cgraph.h"
105 #include "obstack.h"
107 /* Number of attempts to combine instructions in this function. */
109 static int combine_attempts;
111 /* Number of attempts that got as far as substitution in this function. */
113 static int combine_merges;
115 /* Number of instructions combined with added SETs in this function. */
117 static int combine_extras;
119 /* Number of instructions combined in this function. */
121 static int combine_successes;
123 /* Totals over entire compilation. */
125 static int total_attempts, total_merges, total_extras, total_successes;
127 /* combine_instructions may try to replace the right hand side of the
128 second instruction with the value of an associated REG_EQUAL note
129 before throwing it at try_combine. That is problematic when there
130 is a REG_DEAD note for a register used in the old right hand side
131 and can cause distribute_notes to do wrong things. This is the
132 second instruction if it has been so modified, null otherwise. */
134 static rtx i2mod;
136 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
138 static rtx i2mod_old_rhs;
140 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
142 static rtx i2mod_new_rhs;
144 typedef struct reg_stat_struct {
145 /* Record last point of death of (hard or pseudo) register n. */
146 rtx last_death;
148 /* Record last point of modification of (hard or pseudo) register n. */
149 rtx last_set;
151 /* The next group of fields allows the recording of the last value assigned
152 to (hard or pseudo) register n. We use this information to see if an
153 operation being processed is redundant given a prior operation performed
154 on the register. For example, an `and' with a constant is redundant if
155 all the zero bits are already known to be turned off.
157 We use an approach similar to that used by cse, but change it in the
158 following ways:
160 (1) We do not want to reinitialize at each label.
161 (2) It is useful, but not critical, to know the actual value assigned
162 to a register. Often just its form is helpful.
164 Therefore, we maintain the following fields:
166 last_set_value the last value assigned
167 last_set_label records the value of label_tick when the
168 register was assigned
169 last_set_table_tick records the value of label_tick when a
170 value using the register is assigned
171 last_set_invalid set to nonzero when it is not valid
172 to use the value of this register in some
173 register's value
175 To understand the usage of these tables, it is important to understand
176 the distinction between the value in last_set_value being valid and
177 the register being validly contained in some other expression in the
178 table.
180 (The next two parameters are out of date).
182 reg_stat[i].last_set_value is valid if it is nonzero, and either
183 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
185 Register I may validly appear in any expression returned for the value
186 of another register if reg_n_sets[i] is 1. It may also appear in the
187 value for register J if reg_stat[j].last_set_invalid is zero, or
188 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
190 If an expression is found in the table containing a register which may
191 not validly appear in an expression, the register is replaced by
192 something that won't match, (clobber (const_int 0)). */
194 /* Record last value assigned to (hard or pseudo) register n. */
196 rtx last_set_value;
198 /* Record the value of label_tick when an expression involving register n
199 is placed in last_set_value. */
201 int last_set_table_tick;
203 /* Record the value of label_tick when the value for register n is placed in
204 last_set_value. */
206 int last_set_label;
208 /* These fields are maintained in parallel with last_set_value and are
209 used to store the mode in which the register was last set, the bits
210 that were known to be zero when it was last set, and the number of
211 sign bits copies it was known to have when it was last set. */
213 unsigned HOST_WIDE_INT last_set_nonzero_bits;
214 char last_set_sign_bit_copies;
215 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
217 /* Set nonzero if references to register n in expressions should not be
218 used. last_set_invalid is set nonzero when this register is being
219 assigned to and last_set_table_tick == label_tick. */
221 char last_set_invalid;
223 /* Some registers that are set more than once and used in more than one
224 basic block are nevertheless always set in similar ways. For example,
225 a QImode register may be loaded from memory in two places on a machine
226 where byte loads zero extend.
228 We record in the following fields if a register has some leading bits
229 that are always equal to the sign bit, and what we know about the
230 nonzero bits of a register, specifically which bits are known to be
231 zero.
233 If an entry is zero, it means that we don't know anything special. */
235 unsigned char sign_bit_copies;
237 unsigned HOST_WIDE_INT nonzero_bits;
239 /* Record the value of the label_tick when the last truncation
240 happened. The field truncated_to_mode is only valid if
241 truncation_label == label_tick. */
243 int truncation_label;
245 /* Record the last truncation seen for this register. If truncation
246 is not a nop to this mode we might be able to save an explicit
247 truncation if we know that value already contains a truncated
248 value. */
250 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
251 } reg_stat_type;
253 DEF_VEC_O(reg_stat_type);
254 DEF_VEC_ALLOC_O(reg_stat_type,heap);
256 static VEC(reg_stat_type,heap) *reg_stat;
258 /* Record the luid of the last insn that invalidated memory
259 (anything that writes memory, and subroutine calls, but not pushes). */
261 static int mem_last_set;
263 /* Record the luid of the last CALL_INSN
264 so we can tell whether a potential combination crosses any calls. */
266 static int last_call_luid;
268 /* When `subst' is called, this is the insn that is being modified
269 (by combining in a previous insn). The PATTERN of this insn
270 is still the old pattern partially modified and it should not be
271 looked at, but this may be used to examine the successors of the insn
272 to judge whether a simplification is valid. */
274 static rtx subst_insn;
276 /* This is the lowest LUID that `subst' is currently dealing with.
277 get_last_value will not return a value if the register was set at or
278 after this LUID. If not for this mechanism, we could get confused if
279 I2 or I1 in try_combine were an insn that used the old value of a register
280 to obtain a new value. In that case, we might erroneously get the
281 new value of the register when we wanted the old one. */
283 static int subst_low_luid;
285 /* This contains any hard registers that are used in newpat; reg_dead_at_p
286 must consider all these registers to be always live. */
288 static HARD_REG_SET newpat_used_regs;
290 /* This is an insn to which a LOG_LINKS entry has been added. If this
291 insn is the earlier than I2 or I3, combine should rescan starting at
292 that location. */
294 static rtx added_links_insn;
296 /* Basic block in which we are performing combines. */
297 static basic_block this_basic_block;
298 static bool optimize_this_for_speed_p;
301 /* Length of the currently allocated uid_insn_cost array. */
303 static int max_uid_known;
305 /* The following array records the insn_rtx_cost for every insn
306 in the instruction stream. */
308 static int *uid_insn_cost;
310 /* The following array records the LOG_LINKS for every insn in the
311 instruction stream as struct insn_link pointers. */
313 struct insn_link {
314 rtx insn;
315 struct insn_link *next;
318 static struct insn_link **uid_log_links;
320 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
321 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
323 #define FOR_EACH_LOG_LINK(L, INSN) \
324 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
326 /* Links for LOG_LINKS are allocated from this obstack. */
328 static struct obstack insn_link_obstack;
330 /* Allocate a link. */
332 static inline struct insn_link *
333 alloc_insn_link (rtx insn, struct insn_link *next)
335 struct insn_link *l
336 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
337 sizeof (struct insn_link));
338 l->insn = insn;
339 l->next = next;
340 return l;
343 /* Incremented for each basic block. */
345 static int label_tick;
347 /* Reset to label_tick for each extended basic block in scanning order. */
349 static int label_tick_ebb_start;
351 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
352 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
354 static enum machine_mode nonzero_bits_mode;
356 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
357 be safely used. It is zero while computing them and after combine has
358 completed. This former test prevents propagating values based on
359 previously set values, which can be incorrect if a variable is modified
360 in a loop. */
362 static int nonzero_sign_valid;
365 /* Record one modification to rtl structure
366 to be undone by storing old_contents into *where. */
368 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
370 struct undo
372 struct undo *next;
373 enum undo_kind kind;
374 union { rtx r; int i; enum machine_mode m; struct insn_link *l; } old_contents;
375 union { rtx *r; int *i; struct insn_link **l; } where;
378 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
379 num_undo says how many are currently recorded.
381 other_insn is nonzero if we have modified some other insn in the process
382 of working on subst_insn. It must be verified too. */
384 struct undobuf
386 struct undo *undos;
387 struct undo *frees;
388 rtx other_insn;
391 static struct undobuf undobuf;
393 /* Number of times the pseudo being substituted for
394 was found and replaced. */
396 static int n_occurrences;
398 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
399 enum machine_mode,
400 unsigned HOST_WIDE_INT,
401 unsigned HOST_WIDE_INT *);
402 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
403 enum machine_mode,
404 unsigned int, unsigned int *);
405 static void do_SUBST (rtx *, rtx);
406 static void do_SUBST_INT (int *, int);
407 static void init_reg_last (void);
408 static void setup_incoming_promotions (rtx);
409 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
410 static int cant_combine_insn_p (rtx);
411 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
412 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
413 static int contains_muldiv (rtx);
414 static rtx try_combine (rtx, rtx, rtx, rtx, int *, rtx);
415 static void undo_all (void);
416 static void undo_commit (void);
417 static rtx *find_split_point (rtx *, rtx, bool);
418 static rtx subst (rtx, rtx, rtx, int, int, int);
419 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
420 static rtx simplify_if_then_else (rtx);
421 static rtx simplify_set (rtx);
422 static rtx simplify_logical (rtx);
423 static rtx expand_compound_operation (rtx);
424 static const_rtx expand_field_assignment (const_rtx);
425 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
426 rtx, unsigned HOST_WIDE_INT, int, int, int);
427 static rtx extract_left_shift (rtx, int);
428 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
429 unsigned HOST_WIDE_INT *);
430 static rtx canon_reg_for_combine (rtx, rtx);
431 static rtx force_to_mode (rtx, enum machine_mode,
432 unsigned HOST_WIDE_INT, int);
433 static rtx if_then_else_cond (rtx, rtx *, rtx *);
434 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
435 static int rtx_equal_for_field_assignment_p (rtx, rtx);
436 static rtx make_field_assignment (rtx);
437 static rtx apply_distributive_law (rtx);
438 static rtx distribute_and_simplify_rtx (rtx, int);
439 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
440 unsigned HOST_WIDE_INT);
441 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
442 unsigned HOST_WIDE_INT);
443 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
444 HOST_WIDE_INT, enum machine_mode, int *);
445 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
446 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
447 int);
448 static int recog_for_combine (rtx *, rtx, rtx *);
449 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
450 static enum rtx_code simplify_compare_const (enum rtx_code, 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 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
498 PATTERN can not be split. Otherwise, it returns an insn sequence.
499 This is a wrapper around split_insns which ensures that the
500 reg_stat vector is made larger if the splitter creates a new
501 register. */
503 static rtx
504 combine_split_insns (rtx pattern, rtx insn)
506 rtx ret;
507 unsigned int nregs;
509 ret = split_insns (pattern, insn);
510 nregs = max_reg_num ();
511 if (nregs > VEC_length (reg_stat_type, reg_stat))
512 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
513 return ret;
516 /* This is used by find_single_use to locate an rtx in LOC that
517 contains exactly one use of DEST, which is typically either a REG
518 or CC0. It returns a pointer to the innermost rtx expression
519 containing DEST. Appearances of DEST that are being used to
520 totally replace it are not counted. */
522 static rtx *
523 find_single_use_1 (rtx dest, rtx *loc)
525 rtx x = *loc;
526 enum rtx_code code = GET_CODE (x);
527 rtx *result = NULL;
528 rtx *this_result;
529 int i;
530 const char *fmt;
532 switch (code)
534 case CONST:
535 case LABEL_REF:
536 case SYMBOL_REF:
537 CASE_CONST_ANY:
538 case CLOBBER:
539 return 0;
541 case SET:
542 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
543 of a REG that occupies all of the REG, the insn uses DEST if
544 it is mentioned in the destination or the source. Otherwise, we
545 need just check the source. */
546 if (GET_CODE (SET_DEST (x)) != CC0
547 && GET_CODE (SET_DEST (x)) != PC
548 && !REG_P (SET_DEST (x))
549 && ! (GET_CODE (SET_DEST (x)) == SUBREG
550 && REG_P (SUBREG_REG (SET_DEST (x)))
551 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
552 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
553 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
554 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
555 break;
557 return find_single_use_1 (dest, &SET_SRC (x));
559 case MEM:
560 case SUBREG:
561 return find_single_use_1 (dest, &XEXP (x, 0));
563 default:
564 break;
567 /* If it wasn't one of the common cases above, check each expression and
568 vector of this code. Look for a unique usage of DEST. */
570 fmt = GET_RTX_FORMAT (code);
571 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
573 if (fmt[i] == 'e')
575 if (dest == XEXP (x, i)
576 || (REG_P (dest) && REG_P (XEXP (x, i))
577 && REGNO (dest) == REGNO (XEXP (x, i))))
578 this_result = loc;
579 else
580 this_result = find_single_use_1 (dest, &XEXP (x, i));
582 if (result == NULL)
583 result = this_result;
584 else if (this_result)
585 /* Duplicate usage. */
586 return NULL;
588 else if (fmt[i] == 'E')
590 int j;
592 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
594 if (XVECEXP (x, i, j) == dest
595 || (REG_P (dest)
596 && REG_P (XVECEXP (x, i, j))
597 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
598 this_result = loc;
599 else
600 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
602 if (result == NULL)
603 result = this_result;
604 else if (this_result)
605 return NULL;
610 return result;
614 /* See if DEST, produced in INSN, is used only a single time in the
615 sequel. If so, return a pointer to the innermost rtx expression in which
616 it is used.
618 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
620 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
621 care about REG_DEAD notes or LOG_LINKS.
623 Otherwise, we find the single use by finding an insn that has a
624 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
625 only referenced once in that insn, we know that it must be the first
626 and last insn referencing DEST. */
628 static rtx *
629 find_single_use (rtx dest, rtx insn, rtx *ploc)
631 basic_block bb;
632 rtx next;
633 rtx *result;
634 struct insn_link *link;
636 #ifdef HAVE_cc0
637 if (dest == cc0_rtx)
639 next = NEXT_INSN (insn);
640 if (next == 0
641 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
642 return 0;
644 result = find_single_use_1 (dest, &PATTERN (next));
645 if (result && ploc)
646 *ploc = next;
647 return result;
649 #endif
651 if (!REG_P (dest))
652 return 0;
654 bb = BLOCK_FOR_INSN (insn);
655 for (next = NEXT_INSN (insn);
656 next && BLOCK_FOR_INSN (next) == bb;
657 next = NEXT_INSN (next))
658 if (INSN_P (next) && dead_or_set_p (next, dest))
660 FOR_EACH_LOG_LINK (link, next)
661 if (link->insn == insn)
662 break;
664 if (link)
666 result = find_single_use_1 (dest, &PATTERN (next));
667 if (ploc)
668 *ploc = next;
669 return result;
673 return 0;
676 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
677 insn. The substitution can be undone by undo_all. If INTO is already
678 set to NEWVAL, do not record this change. Because computing NEWVAL might
679 also call SUBST, we have to compute it before we put anything into
680 the undo table. */
682 static void
683 do_SUBST (rtx *into, rtx newval)
685 struct undo *buf;
686 rtx oldval = *into;
688 if (oldval == newval)
689 return;
691 /* We'd like to catch as many invalid transformations here as
692 possible. Unfortunately, there are way too many mode changes
693 that are perfectly valid, so we'd waste too much effort for
694 little gain doing the checks here. Focus on catching invalid
695 transformations involving integer constants. */
696 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
697 && CONST_INT_P (newval))
699 /* Sanity check that we're replacing oldval with a CONST_INT
700 that is a valid sign-extension for the original mode. */
701 gcc_assert (INTVAL (newval)
702 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
704 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
705 CONST_INT is not valid, because after the replacement, the
706 original mode would be gone. Unfortunately, we can't tell
707 when do_SUBST is called to replace the operand thereof, so we
708 perform this test on oldval instead, checking whether an
709 invalid replacement took place before we got here. */
710 gcc_assert (!(GET_CODE (oldval) == SUBREG
711 && CONST_INT_P (SUBREG_REG (oldval))));
712 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
713 && CONST_INT_P (XEXP (oldval, 0))));
716 if (undobuf.frees)
717 buf = undobuf.frees, undobuf.frees = buf->next;
718 else
719 buf = XNEW (struct undo);
721 buf->kind = UNDO_RTX;
722 buf->where.r = into;
723 buf->old_contents.r = oldval;
724 *into = newval;
726 buf->next = undobuf.undos, undobuf.undos = buf;
729 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
731 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
732 for the value of a HOST_WIDE_INT value (including CONST_INT) is
733 not safe. */
735 static void
736 do_SUBST_INT (int *into, int newval)
738 struct undo *buf;
739 int oldval = *into;
741 if (oldval == newval)
742 return;
744 if (undobuf.frees)
745 buf = undobuf.frees, undobuf.frees = buf->next;
746 else
747 buf = XNEW (struct undo);
749 buf->kind = UNDO_INT;
750 buf->where.i = into;
751 buf->old_contents.i = oldval;
752 *into = newval;
754 buf->next = undobuf.undos, undobuf.undos = buf;
757 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
759 /* Similar to SUBST, but just substitute the mode. This is used when
760 changing the mode of a pseudo-register, so that any other
761 references to the entry in the regno_reg_rtx array will change as
762 well. */
764 static void
765 do_SUBST_MODE (rtx *into, enum machine_mode newval)
767 struct undo *buf;
768 enum machine_mode oldval = GET_MODE (*into);
770 if (oldval == newval)
771 return;
773 if (undobuf.frees)
774 buf = undobuf.frees, undobuf.frees = buf->next;
775 else
776 buf = XNEW (struct undo);
778 buf->kind = UNDO_MODE;
779 buf->where.r = into;
780 buf->old_contents.m = oldval;
781 adjust_reg_mode (*into, newval);
783 buf->next = undobuf.undos, undobuf.undos = buf;
786 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
788 #ifndef HAVE_cc0
789 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
791 static void
792 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
794 struct undo *buf;
795 struct insn_link * oldval = *into;
797 if (oldval == newval)
798 return;
800 if (undobuf.frees)
801 buf = undobuf.frees, undobuf.frees = buf->next;
802 else
803 buf = XNEW (struct undo);
805 buf->kind = UNDO_LINKS;
806 buf->where.l = into;
807 buf->old_contents.l = oldval;
808 *into = newval;
810 buf->next = undobuf.undos, undobuf.undos = buf;
813 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
814 #endif
816 /* Subroutine of try_combine. Determine whether the replacement patterns
817 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
818 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
819 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
820 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
821 of all the instructions can be estimated and the replacements are more
822 expensive than the original sequence. */
824 static bool
825 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
826 rtx newi2pat, rtx newotherpat)
828 int i0_cost, i1_cost, i2_cost, i3_cost;
829 int new_i2_cost, new_i3_cost;
830 int old_cost, new_cost;
832 /* Lookup the original insn_rtx_costs. */
833 i2_cost = INSN_COST (i2);
834 i3_cost = INSN_COST (i3);
836 if (i1)
838 i1_cost = INSN_COST (i1);
839 if (i0)
841 i0_cost = INSN_COST (i0);
842 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
843 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
845 else
847 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
848 ? i1_cost + i2_cost + i3_cost : 0);
849 i0_cost = 0;
852 else
854 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
855 i1_cost = i0_cost = 0;
858 /* Calculate the replacement insn_rtx_costs. */
859 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
860 if (newi2pat)
862 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
863 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
864 ? new_i2_cost + new_i3_cost : 0;
866 else
868 new_cost = new_i3_cost;
869 new_i2_cost = 0;
872 if (undobuf.other_insn)
874 int old_other_cost, new_other_cost;
876 old_other_cost = INSN_COST (undobuf.other_insn);
877 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
878 if (old_other_cost > 0 && new_other_cost > 0)
880 old_cost += old_other_cost;
881 new_cost += new_other_cost;
883 else
884 old_cost = 0;
887 /* Disallow this combination if both new_cost and old_cost are greater than
888 zero, and new_cost is greater than old cost. */
889 if (old_cost > 0 && new_cost > old_cost)
891 if (dump_file)
893 if (i0)
895 fprintf (dump_file,
896 "rejecting combination of insns %d, %d, %d and %d\n",
897 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
898 INSN_UID (i3));
899 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
900 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
902 else if (i1)
904 fprintf (dump_file,
905 "rejecting combination of insns %d, %d and %d\n",
906 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
907 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
908 i1_cost, i2_cost, i3_cost, old_cost);
910 else
912 fprintf (dump_file,
913 "rejecting combination of insns %d and %d\n",
914 INSN_UID (i2), INSN_UID (i3));
915 fprintf (dump_file, "original costs %d + %d = %d\n",
916 i2_cost, i3_cost, old_cost);
919 if (newi2pat)
921 fprintf (dump_file, "replacement costs %d + %d = %d\n",
922 new_i2_cost, new_i3_cost, new_cost);
924 else
925 fprintf (dump_file, "replacement cost %d\n", new_cost);
928 return false;
931 /* Update the uid_insn_cost array with the replacement costs. */
932 INSN_COST (i2) = new_i2_cost;
933 INSN_COST (i3) = new_i3_cost;
934 if (i1)
936 INSN_COST (i1) = 0;
937 if (i0)
938 INSN_COST (i0) = 0;
941 return true;
945 /* Delete any insns that copy a register to itself. */
947 static void
948 delete_noop_moves (void)
950 rtx insn, next;
951 basic_block bb;
953 FOR_EACH_BB (bb)
955 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
957 next = NEXT_INSN (insn);
958 if (INSN_P (insn) && noop_move_p (insn))
960 if (dump_file)
961 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
963 delete_insn_and_edges (insn);
970 /* Fill in log links field for all insns. */
972 static void
973 create_log_links (void)
975 basic_block bb;
976 rtx *next_use, insn;
977 df_ref *def_vec, *use_vec;
979 next_use = XCNEWVEC (rtx, max_reg_num ());
981 /* Pass through each block from the end, recording the uses of each
982 register and establishing log links when def is encountered.
983 Note that we do not clear next_use array in order to save time,
984 so we have to test whether the use is in the same basic block as def.
986 There are a few cases below when we do not consider the definition or
987 usage -- these are taken from original flow.c did. Don't ask me why it is
988 done this way; I don't know and if it works, I don't want to know. */
990 FOR_EACH_BB (bb)
992 FOR_BB_INSNS_REVERSE (bb, insn)
994 if (!NONDEBUG_INSN_P (insn))
995 continue;
997 /* Log links are created only once. */
998 gcc_assert (!LOG_LINKS (insn));
1000 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
1002 df_ref def = *def_vec;
1003 int regno = DF_REF_REGNO (def);
1004 rtx use_insn;
1006 if (!next_use[regno])
1007 continue;
1009 /* Do not consider if it is pre/post modification in MEM. */
1010 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1011 continue;
1013 /* Do not make the log link for frame pointer. */
1014 if ((regno == FRAME_POINTER_REGNUM
1015 && (! reload_completed || frame_pointer_needed))
1016 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1017 || (regno == HARD_FRAME_POINTER_REGNUM
1018 && (! reload_completed || frame_pointer_needed))
1019 #endif
1020 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1021 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1022 #endif
1024 continue;
1026 use_insn = next_use[regno];
1027 if (BLOCK_FOR_INSN (use_insn) == bb)
1029 /* flow.c claimed:
1031 We don't build a LOG_LINK for hard registers contained
1032 in ASM_OPERANDs. If these registers get replaced,
1033 we might wind up changing the semantics of the insn,
1034 even if reload can make what appear to be valid
1035 assignments later. */
1036 if (regno >= FIRST_PSEUDO_REGISTER
1037 || asm_noperands (PATTERN (use_insn)) < 0)
1039 /* Don't add duplicate links between instructions. */
1040 struct insn_link *links;
1041 FOR_EACH_LOG_LINK (links, use_insn)
1042 if (insn == links->insn)
1043 break;
1045 if (!links)
1046 LOG_LINKS (use_insn)
1047 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1050 next_use[regno] = NULL_RTX;
1053 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1055 df_ref use = *use_vec;
1056 int regno = DF_REF_REGNO (use);
1058 /* Do not consider the usage of the stack pointer
1059 by function call. */
1060 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1061 continue;
1063 next_use[regno] = insn;
1068 free (next_use);
1071 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1072 true if we found a LOG_LINK that proves that A feeds B. This only works
1073 if there are no instructions between A and B which could have a link
1074 depending on A, since in that case we would not record a link for B.
1075 We also check the implicit dependency created by a cc0 setter/user
1076 pair. */
1078 static bool
1079 insn_a_feeds_b (rtx a, rtx b)
1081 struct insn_link *links;
1082 FOR_EACH_LOG_LINK (links, b)
1083 if (links->insn == a)
1084 return true;
1085 #ifdef HAVE_cc0
1086 if (sets_cc0_p (a))
1087 return true;
1088 #endif
1089 return false;
1092 /* Main entry point for combiner. F is the first insn of the function.
1093 NREGS is the first unused pseudo-reg number.
1095 Return nonzero if the combiner has turned an indirect jump
1096 instruction into a direct jump. */
1097 static int
1098 combine_instructions (rtx f, unsigned int nregs)
1100 rtx insn, next;
1101 #ifdef HAVE_cc0
1102 rtx prev;
1103 #endif
1104 struct insn_link *links, *nextlinks;
1105 rtx first;
1106 basic_block last_bb;
1108 int new_direct_jump_p = 0;
1110 for (first = f; first && !INSN_P (first); )
1111 first = NEXT_INSN (first);
1112 if (!first)
1113 return 0;
1115 combine_attempts = 0;
1116 combine_merges = 0;
1117 combine_extras = 0;
1118 combine_successes = 0;
1120 rtl_hooks = combine_rtl_hooks;
1122 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1124 init_recog_no_volatile ();
1126 /* Allocate array for insn info. */
1127 max_uid_known = get_max_uid ();
1128 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1129 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1130 gcc_obstack_init (&insn_link_obstack);
1132 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1134 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1135 problems when, for example, we have j <<= 1 in a loop. */
1137 nonzero_sign_valid = 0;
1138 label_tick = label_tick_ebb_start = 1;
1140 /* Scan all SETs and see if we can deduce anything about what
1141 bits are known to be zero for some registers and how many copies
1142 of the sign bit are known to exist for those registers.
1144 Also set any known values so that we can use it while searching
1145 for what bits are known to be set. */
1147 setup_incoming_promotions (first);
1148 /* Allow the entry block and the first block to fall into the same EBB.
1149 Conceptually the incoming promotions are assigned to the entry block. */
1150 last_bb = ENTRY_BLOCK_PTR;
1152 create_log_links ();
1153 FOR_EACH_BB (this_basic_block)
1155 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1156 last_call_luid = 0;
1157 mem_last_set = -1;
1159 label_tick++;
1160 if (!single_pred_p (this_basic_block)
1161 || single_pred (this_basic_block) != last_bb)
1162 label_tick_ebb_start = label_tick;
1163 last_bb = this_basic_block;
1165 FOR_BB_INSNS (this_basic_block, insn)
1166 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1168 #ifdef AUTO_INC_DEC
1169 rtx links;
1170 #endif
1172 subst_low_luid = DF_INSN_LUID (insn);
1173 subst_insn = insn;
1175 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1176 insn);
1177 record_dead_and_set_regs (insn);
1179 #ifdef AUTO_INC_DEC
1180 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1181 if (REG_NOTE_KIND (links) == REG_INC)
1182 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1183 insn);
1184 #endif
1186 /* Record the current insn_rtx_cost of this instruction. */
1187 if (NONJUMP_INSN_P (insn))
1188 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1189 optimize_this_for_speed_p);
1190 if (dump_file)
1191 fprintf(dump_file, "insn_cost %d: %d\n",
1192 INSN_UID (insn), INSN_COST (insn));
1196 nonzero_sign_valid = 1;
1198 /* Now scan all the insns in forward order. */
1199 label_tick = label_tick_ebb_start = 1;
1200 init_reg_last ();
1201 setup_incoming_promotions (first);
1202 last_bb = ENTRY_BLOCK_PTR;
1204 FOR_EACH_BB (this_basic_block)
1206 rtx last_combined_insn = NULL_RTX;
1207 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1208 last_call_luid = 0;
1209 mem_last_set = -1;
1211 label_tick++;
1212 if (!single_pred_p (this_basic_block)
1213 || single_pred (this_basic_block) != last_bb)
1214 label_tick_ebb_start = label_tick;
1215 last_bb = this_basic_block;
1217 rtl_profile_for_bb (this_basic_block);
1218 for (insn = BB_HEAD (this_basic_block);
1219 insn != NEXT_INSN (BB_END (this_basic_block));
1220 insn = next ? next : NEXT_INSN (insn))
1222 next = 0;
1223 if (NONDEBUG_INSN_P (insn))
1225 while (last_combined_insn
1226 && INSN_DELETED_P (last_combined_insn))
1227 last_combined_insn = PREV_INSN (last_combined_insn);
1228 if (last_combined_insn == NULL_RTX
1229 || BARRIER_P (last_combined_insn)
1230 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1231 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1232 last_combined_insn = insn;
1234 /* See if we know about function return values before this
1235 insn based upon SUBREG flags. */
1236 check_promoted_subreg (insn, PATTERN (insn));
1238 /* See if we can find hardregs and subreg of pseudos in
1239 narrower modes. This could help turning TRUNCATEs
1240 into SUBREGs. */
1241 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1243 /* Try this insn with each insn it links back to. */
1245 FOR_EACH_LOG_LINK (links, insn)
1246 if ((next = try_combine (insn, links->insn, NULL_RTX,
1247 NULL_RTX, &new_direct_jump_p,
1248 last_combined_insn)) != 0)
1249 goto retry;
1251 /* Try each sequence of three linked insns ending with this one. */
1253 FOR_EACH_LOG_LINK (links, insn)
1255 rtx link = links->insn;
1257 /* If the linked insn has been replaced by a note, then there
1258 is no point in pursuing this chain any further. */
1259 if (NOTE_P (link))
1260 continue;
1262 FOR_EACH_LOG_LINK (nextlinks, link)
1263 if ((next = try_combine (insn, link, nextlinks->insn,
1264 NULL_RTX, &new_direct_jump_p,
1265 last_combined_insn)) != 0)
1266 goto retry;
1269 #ifdef HAVE_cc0
1270 /* Try to combine a jump insn that uses CC0
1271 with a preceding insn that sets CC0, and maybe with its
1272 logical predecessor as well.
1273 This is how we make decrement-and-branch insns.
1274 We need this special code because data flow connections
1275 via CC0 do not get entered in LOG_LINKS. */
1277 if (JUMP_P (insn)
1278 && (prev = prev_nonnote_insn (insn)) != 0
1279 && NONJUMP_INSN_P (prev)
1280 && sets_cc0_p (PATTERN (prev)))
1282 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1283 &new_direct_jump_p,
1284 last_combined_insn)) != 0)
1285 goto retry;
1287 FOR_EACH_LOG_LINK (nextlinks, prev)
1288 if ((next = try_combine (insn, prev, nextlinks->insn,
1289 NULL_RTX, &new_direct_jump_p,
1290 last_combined_insn)) != 0)
1291 goto retry;
1294 /* Do the same for an insn that explicitly references CC0. */
1295 if (NONJUMP_INSN_P (insn)
1296 && (prev = prev_nonnote_insn (insn)) != 0
1297 && NONJUMP_INSN_P (prev)
1298 && sets_cc0_p (PATTERN (prev))
1299 && GET_CODE (PATTERN (insn)) == SET
1300 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1302 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1303 &new_direct_jump_p,
1304 last_combined_insn)) != 0)
1305 goto retry;
1307 FOR_EACH_LOG_LINK (nextlinks, prev)
1308 if ((next = try_combine (insn, prev, nextlinks->insn,
1309 NULL_RTX, &new_direct_jump_p,
1310 last_combined_insn)) != 0)
1311 goto retry;
1314 /* Finally, see if any of the insns that this insn links to
1315 explicitly references CC0. If so, try this insn, that insn,
1316 and its predecessor if it sets CC0. */
1317 FOR_EACH_LOG_LINK (links, insn)
1318 if (NONJUMP_INSN_P (links->insn)
1319 && GET_CODE (PATTERN (links->insn)) == SET
1320 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1321 && (prev = prev_nonnote_insn (links->insn)) != 0
1322 && NONJUMP_INSN_P (prev)
1323 && sets_cc0_p (PATTERN (prev))
1324 && (next = try_combine (insn, links->insn,
1325 prev, NULL_RTX, &new_direct_jump_p,
1326 last_combined_insn)) != 0)
1327 goto retry;
1328 #endif
1330 /* Try combining an insn with two different insns whose results it
1331 uses. */
1332 FOR_EACH_LOG_LINK (links, insn)
1333 for (nextlinks = links->next; nextlinks;
1334 nextlinks = nextlinks->next)
1335 if ((next = try_combine (insn, links->insn,
1336 nextlinks->insn, NULL_RTX,
1337 &new_direct_jump_p,
1338 last_combined_insn)) != 0)
1339 goto retry;
1341 /* Try four-instruction combinations. */
1342 FOR_EACH_LOG_LINK (links, insn)
1344 struct insn_link *next1;
1345 rtx link = links->insn;
1347 /* If the linked insn has been replaced by a note, then there
1348 is no point in pursuing this chain any further. */
1349 if (NOTE_P (link))
1350 continue;
1352 FOR_EACH_LOG_LINK (next1, link)
1354 rtx link1 = next1->insn;
1355 if (NOTE_P (link1))
1356 continue;
1357 /* I0 -> I1 -> I2 -> I3. */
1358 FOR_EACH_LOG_LINK (nextlinks, link1)
1359 if ((next = try_combine (insn, link, link1,
1360 nextlinks->insn,
1361 &new_direct_jump_p,
1362 last_combined_insn)) != 0)
1363 goto retry;
1364 /* I0, I1 -> I2, I2 -> I3. */
1365 for (nextlinks = next1->next; nextlinks;
1366 nextlinks = nextlinks->next)
1367 if ((next = try_combine (insn, link, link1,
1368 nextlinks->insn,
1369 &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1374 for (next1 = links->next; next1; next1 = next1->next)
1376 rtx link1 = next1->insn;
1377 if (NOTE_P (link1))
1378 continue;
1379 /* I0 -> I2; I1, I2 -> I3. */
1380 FOR_EACH_LOG_LINK (nextlinks, link)
1381 if ((next = try_combine (insn, link, link1,
1382 nextlinks->insn,
1383 &new_direct_jump_p,
1384 last_combined_insn)) != 0)
1385 goto retry;
1386 /* I0 -> I1; I1, I2 -> I3. */
1387 FOR_EACH_LOG_LINK (nextlinks, link1)
1388 if ((next = try_combine (insn, link, link1,
1389 nextlinks->insn,
1390 &new_direct_jump_p,
1391 last_combined_insn)) != 0)
1392 goto retry;
1396 /* Try this insn with each REG_EQUAL note it links back to. */
1397 FOR_EACH_LOG_LINK (links, insn)
1399 rtx set, note;
1400 rtx temp = links->insn;
1401 if ((set = single_set (temp)) != 0
1402 && (note = find_reg_equal_equiv_note (temp)) != 0
1403 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1404 /* Avoid using a register that may already been marked
1405 dead by an earlier instruction. */
1406 && ! unmentioned_reg_p (note, SET_SRC (set))
1407 && (GET_MODE (note) == VOIDmode
1408 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1409 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1411 /* Temporarily replace the set's source with the
1412 contents of the REG_EQUAL note. The insn will
1413 be deleted or recognized by try_combine. */
1414 rtx orig = SET_SRC (set);
1415 SET_SRC (set) = note;
1416 i2mod = temp;
1417 i2mod_old_rhs = copy_rtx (orig);
1418 i2mod_new_rhs = copy_rtx (note);
1419 next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1420 &new_direct_jump_p,
1421 last_combined_insn);
1422 i2mod = NULL_RTX;
1423 if (next)
1424 goto retry;
1425 SET_SRC (set) = orig;
1429 if (!NOTE_P (insn))
1430 record_dead_and_set_regs (insn);
1432 retry:
1438 default_rtl_profile ();
1439 clear_bb_flags ();
1440 new_direct_jump_p |= purge_all_dead_edges ();
1441 delete_noop_moves ();
1443 /* Clean up. */
1444 obstack_free (&insn_link_obstack, NULL);
1445 free (uid_log_links);
1446 free (uid_insn_cost);
1447 VEC_free (reg_stat_type, heap, reg_stat);
1450 struct undo *undo, *next;
1451 for (undo = undobuf.frees; undo; undo = next)
1453 next = undo->next;
1454 free (undo);
1456 undobuf.frees = 0;
1459 total_attempts += combine_attempts;
1460 total_merges += combine_merges;
1461 total_extras += combine_extras;
1462 total_successes += combine_successes;
1464 nonzero_sign_valid = 0;
1465 rtl_hooks = general_rtl_hooks;
1467 /* Make recognizer allow volatile MEMs again. */
1468 init_recog ();
1470 return new_direct_jump_p;
1473 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1475 static void
1476 init_reg_last (void)
1478 unsigned int i;
1479 reg_stat_type *p;
1481 FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
1482 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1485 /* Set up any promoted values for incoming argument registers. */
1487 static void
1488 setup_incoming_promotions (rtx first)
1490 tree arg;
1491 bool strictly_local = false;
1493 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1494 arg = DECL_CHAIN (arg))
1496 rtx x, reg = DECL_INCOMING_RTL (arg);
1497 int uns1, uns3;
1498 enum machine_mode mode1, mode2, mode3, mode4;
1500 /* Only continue if the incoming argument is in a register. */
1501 if (!REG_P (reg))
1502 continue;
1504 /* Determine, if possible, whether all call sites of the current
1505 function lie within the current compilation unit. (This does
1506 take into account the exporting of a function via taking its
1507 address, and so forth.) */
1508 strictly_local = cgraph_local_info (current_function_decl)->local;
1510 /* The mode and signedness of the argument before any promotions happen
1511 (equal to the mode of the pseudo holding it at that stage). */
1512 mode1 = TYPE_MODE (TREE_TYPE (arg));
1513 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1515 /* The mode and signedness of the argument after any source language and
1516 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1517 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1518 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1520 /* The mode and signedness of the argument as it is actually passed,
1521 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1522 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1523 TREE_TYPE (cfun->decl), 0);
1525 /* The mode of the register in which the argument is being passed. */
1526 mode4 = GET_MODE (reg);
1528 /* Eliminate sign extensions in the callee when:
1529 (a) A mode promotion has occurred; */
1530 if (mode1 == mode3)
1531 continue;
1532 /* (b) The mode of the register is the same as the mode of
1533 the argument as it is passed; */
1534 if (mode3 != mode4)
1535 continue;
1536 /* (c) There's no language level extension; */
1537 if (mode1 == mode2)
1539 /* (c.1) All callers are from the current compilation unit. If that's
1540 the case we don't have to rely on an ABI, we only have to know
1541 what we're generating right now, and we know that we will do the
1542 mode1 to mode2 promotion with the given sign. */
1543 else if (!strictly_local)
1544 continue;
1545 /* (c.2) The combination of the two promotions is useful. This is
1546 true when the signs match, or if the first promotion is unsigned.
1547 In the later case, (sign_extend (zero_extend x)) is the same as
1548 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1549 else if (uns1)
1550 uns3 = true;
1551 else if (uns3)
1552 continue;
1554 /* Record that the value was promoted from mode1 to mode3,
1555 so that any sign extension at the head of the current
1556 function may be eliminated. */
1557 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1558 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1559 record_value_for_reg (reg, first, x);
1563 /* Called via note_stores. If X is a pseudo that is narrower than
1564 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1566 If we are setting only a portion of X and we can't figure out what
1567 portion, assume all bits will be used since we don't know what will
1568 be happening.
1570 Similarly, set how many bits of X are known to be copies of the sign bit
1571 at all locations in the function. This is the smallest number implied
1572 by any set of X. */
1574 static void
1575 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1577 rtx insn = (rtx) data;
1578 unsigned int num;
1580 if (REG_P (x)
1581 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1582 /* If this register is undefined at the start of the file, we can't
1583 say what its contents were. */
1584 && ! REGNO_REG_SET_P
1585 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1586 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1588 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
1590 if (set == 0 || GET_CODE (set) == CLOBBER)
1592 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1593 rsp->sign_bit_copies = 1;
1594 return;
1597 /* If this register is being initialized using itself, and the
1598 register is uninitialized in this basic block, and there are
1599 no LOG_LINKS which set the register, then part of the
1600 register is uninitialized. In that case we can't assume
1601 anything about the number of nonzero bits.
1603 ??? We could do better if we checked this in
1604 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1605 could avoid making assumptions about the insn which initially
1606 sets the register, while still using the information in other
1607 insns. We would have to be careful to check every insn
1608 involved in the combination. */
1610 if (insn
1611 && reg_referenced_p (x, PATTERN (insn))
1612 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1613 REGNO (x)))
1615 struct insn_link *link;
1617 FOR_EACH_LOG_LINK (link, insn)
1618 if (dead_or_set_p (link->insn, x))
1619 break;
1620 if (!link)
1622 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1623 rsp->sign_bit_copies = 1;
1624 return;
1628 /* If this is a complex assignment, see if we can convert it into a
1629 simple assignment. */
1630 set = expand_field_assignment (set);
1632 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1633 set what we know about X. */
1635 if (SET_DEST (set) == x
1636 || (paradoxical_subreg_p (SET_DEST (set))
1637 && SUBREG_REG (SET_DEST (set)) == x))
1639 rtx src = SET_SRC (set);
1641 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1642 /* If X is narrower than a word and SRC is a non-negative
1643 constant that would appear negative in the mode of X,
1644 sign-extend it for use in reg_stat[].nonzero_bits because some
1645 machines (maybe most) will actually do the sign-extension
1646 and this is the conservative approach.
1648 ??? For 2.5, try to tighten up the MD files in this regard
1649 instead of this kludge. */
1651 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1652 && CONST_INT_P (src)
1653 && INTVAL (src) > 0
1654 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1655 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1656 #endif
1658 /* Don't call nonzero_bits if it cannot change anything. */
1659 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1660 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1661 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1662 if (rsp->sign_bit_copies == 0
1663 || rsp->sign_bit_copies > num)
1664 rsp->sign_bit_copies = num;
1666 else
1668 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1669 rsp->sign_bit_copies = 1;
1674 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1675 optionally insns that were previously combined into I3 or that will be
1676 combined into the merger of INSN and I3. The order is PRED, PRED2,
1677 INSN, SUCC, SUCC2, I3.
1679 Return 0 if the combination is not allowed for any reason.
1681 If the combination is allowed, *PDEST will be set to the single
1682 destination of INSN and *PSRC to the single source, and this function
1683 will return 1. */
1685 static int
1686 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1687 rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1688 rtx *pdest, rtx *psrc)
1690 int i;
1691 const_rtx set = 0;
1692 rtx src, dest;
1693 rtx p;
1694 #ifdef AUTO_INC_DEC
1695 rtx link;
1696 #endif
1697 bool all_adjacent = true;
1698 int (*is_volatile_p) (const_rtx);
1700 if (succ)
1702 if (succ2)
1704 if (next_active_insn (succ2) != i3)
1705 all_adjacent = false;
1706 if (next_active_insn (succ) != succ2)
1707 all_adjacent = false;
1709 else if (next_active_insn (succ) != i3)
1710 all_adjacent = false;
1711 if (next_active_insn (insn) != succ)
1712 all_adjacent = false;
1714 else if (next_active_insn (insn) != i3)
1715 all_adjacent = false;
1717 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1718 or a PARALLEL consisting of such a SET and CLOBBERs.
1720 If INSN has CLOBBER parallel parts, ignore them for our processing.
1721 By definition, these happen during the execution of the insn. When it
1722 is merged with another insn, all bets are off. If they are, in fact,
1723 needed and aren't also supplied in I3, they may be added by
1724 recog_for_combine. Otherwise, it won't match.
1726 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1727 note.
1729 Get the source and destination of INSN. If more than one, can't
1730 combine. */
1732 if (GET_CODE (PATTERN (insn)) == SET)
1733 set = PATTERN (insn);
1734 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1735 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1737 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1739 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1741 switch (GET_CODE (elt))
1743 /* This is important to combine floating point insns
1744 for the SH4 port. */
1745 case USE:
1746 /* Combining an isolated USE doesn't make sense.
1747 We depend here on combinable_i3pat to reject them. */
1748 /* The code below this loop only verifies that the inputs of
1749 the SET in INSN do not change. We call reg_set_between_p
1750 to verify that the REG in the USE does not change between
1751 I3 and INSN.
1752 If the USE in INSN was for a pseudo register, the matching
1753 insn pattern will likely match any register; combining this
1754 with any other USE would only be safe if we knew that the
1755 used registers have identical values, or if there was
1756 something to tell them apart, e.g. different modes. For
1757 now, we forgo such complicated tests and simply disallow
1758 combining of USES of pseudo registers with any other USE. */
1759 if (REG_P (XEXP (elt, 0))
1760 && GET_CODE (PATTERN (i3)) == PARALLEL)
1762 rtx i3pat = PATTERN (i3);
1763 int i = XVECLEN (i3pat, 0) - 1;
1764 unsigned int regno = REGNO (XEXP (elt, 0));
1768 rtx i3elt = XVECEXP (i3pat, 0, i);
1770 if (GET_CODE (i3elt) == USE
1771 && REG_P (XEXP (i3elt, 0))
1772 && (REGNO (XEXP (i3elt, 0)) == regno
1773 ? reg_set_between_p (XEXP (elt, 0),
1774 PREV_INSN (insn), i3)
1775 : regno >= FIRST_PSEUDO_REGISTER))
1776 return 0;
1778 while (--i >= 0);
1780 break;
1782 /* We can ignore CLOBBERs. */
1783 case CLOBBER:
1784 break;
1786 case SET:
1787 /* Ignore SETs whose result isn't used but not those that
1788 have side-effects. */
1789 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1790 && insn_nothrow_p (insn)
1791 && !side_effects_p (elt))
1792 break;
1794 /* If we have already found a SET, this is a second one and
1795 so we cannot combine with this insn. */
1796 if (set)
1797 return 0;
1799 set = elt;
1800 break;
1802 default:
1803 /* Anything else means we can't combine. */
1804 return 0;
1808 if (set == 0
1809 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1810 so don't do anything with it. */
1811 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1812 return 0;
1814 else
1815 return 0;
1817 if (set == 0)
1818 return 0;
1820 /* The simplification in expand_field_assignment may call back to
1821 get_last_value, so set safe guard here. */
1822 subst_low_luid = DF_INSN_LUID (insn);
1824 set = expand_field_assignment (set);
1825 src = SET_SRC (set), dest = SET_DEST (set);
1827 /* Don't eliminate a store in the stack pointer. */
1828 if (dest == stack_pointer_rtx
1829 /* Don't combine with an insn that sets a register to itself if it has
1830 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1831 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1832 /* Can't merge an ASM_OPERANDS. */
1833 || GET_CODE (src) == ASM_OPERANDS
1834 /* Can't merge a function call. */
1835 || GET_CODE (src) == CALL
1836 /* Don't eliminate a function call argument. */
1837 || (CALL_P (i3)
1838 && (find_reg_fusage (i3, USE, dest)
1839 || (REG_P (dest)
1840 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1841 && global_regs[REGNO (dest)])))
1842 /* Don't substitute into an incremented register. */
1843 || FIND_REG_INC_NOTE (i3, dest)
1844 || (succ && FIND_REG_INC_NOTE (succ, dest))
1845 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1846 /* Don't substitute into a non-local goto, this confuses CFG. */
1847 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1848 /* Make sure that DEST is not used after SUCC but before I3. */
1849 || (!all_adjacent
1850 && ((succ2
1851 && (reg_used_between_p (dest, succ2, i3)
1852 || reg_used_between_p (dest, succ, succ2)))
1853 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1854 /* Make sure that the value that is to be substituted for the register
1855 does not use any registers whose values alter in between. However,
1856 If the insns are adjacent, a use can't cross a set even though we
1857 think it might (this can happen for a sequence of insns each setting
1858 the same destination; last_set of that register might point to
1859 a NOTE). If INSN has a REG_EQUIV note, the register is always
1860 equivalent to the memory so the substitution is valid even if there
1861 are intervening stores. Also, don't move a volatile asm or
1862 UNSPEC_VOLATILE across any other insns. */
1863 || (! all_adjacent
1864 && (((!MEM_P (src)
1865 || ! find_reg_note (insn, REG_EQUIV, src))
1866 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1867 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1868 || GET_CODE (src) == UNSPEC_VOLATILE))
1869 /* Don't combine across a CALL_INSN, because that would possibly
1870 change whether the life span of some REGs crosses calls or not,
1871 and it is a pain to update that information.
1872 Exception: if source is a constant, moving it later can't hurt.
1873 Accept that as a special case. */
1874 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1875 return 0;
1877 /* DEST must either be a REG or CC0. */
1878 if (REG_P (dest))
1880 /* If register alignment is being enforced for multi-word items in all
1881 cases except for parameters, it is possible to have a register copy
1882 insn referencing a hard register that is not allowed to contain the
1883 mode being copied and which would not be valid as an operand of most
1884 insns. Eliminate this problem by not combining with such an insn.
1886 Also, on some machines we don't want to extend the life of a hard
1887 register. */
1889 if (REG_P (src)
1890 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1891 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1892 /* Don't extend the life of a hard register unless it is
1893 user variable (if we have few registers) or it can't
1894 fit into the desired register (meaning something special
1895 is going on).
1896 Also avoid substituting a return register into I3, because
1897 reload can't handle a conflict with constraints of other
1898 inputs. */
1899 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1900 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1901 return 0;
1903 else if (GET_CODE (dest) != CC0)
1904 return 0;
1907 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1908 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1909 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1911 /* Don't substitute for a register intended as a clobberable
1912 operand. */
1913 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1914 if (rtx_equal_p (reg, dest))
1915 return 0;
1917 /* If the clobber represents an earlyclobber operand, we must not
1918 substitute an expression containing the clobbered register.
1919 As we do not analyze the constraint strings here, we have to
1920 make the conservative assumption. However, if the register is
1921 a fixed hard reg, the clobber cannot represent any operand;
1922 we leave it up to the machine description to either accept or
1923 reject use-and-clobber patterns. */
1924 if (!REG_P (reg)
1925 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1926 || !fixed_regs[REGNO (reg)])
1927 if (reg_overlap_mentioned_p (reg, src))
1928 return 0;
1931 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1932 or not), reject, unless nothing volatile comes between it and I3 */
1934 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1936 /* Make sure neither succ nor succ2 contains a volatile reference. */
1937 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1938 return 0;
1939 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1940 return 0;
1941 /* We'll check insns between INSN and I3 below. */
1944 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1945 to be an explicit register variable, and was chosen for a reason. */
1947 if (GET_CODE (src) == ASM_OPERANDS
1948 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1949 return 0;
1951 /* If INSN contains volatile references (specifically volatile MEMs),
1952 we cannot combine across any other volatile references.
1953 Even if INSN doesn't contain volatile references, any intervening
1954 volatile insn might affect machine state. */
1956 is_volatile_p = volatile_refs_p (PATTERN (insn))
1957 ? volatile_refs_p
1958 : volatile_insn_p;
1960 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1961 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
1962 return 0;
1964 /* If INSN contains an autoincrement or autodecrement, make sure that
1965 register is not used between there and I3, and not already used in
1966 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1967 Also insist that I3 not be a jump; if it were one
1968 and the incremented register were spilled, we would lose. */
1970 #ifdef AUTO_INC_DEC
1971 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1972 if (REG_NOTE_KIND (link) == REG_INC
1973 && (JUMP_P (i3)
1974 || reg_used_between_p (XEXP (link, 0), insn, i3)
1975 || (pred != NULL_RTX
1976 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1977 || (pred2 != NULL_RTX
1978 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1979 || (succ != NULL_RTX
1980 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1981 || (succ2 != NULL_RTX
1982 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
1983 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1984 return 0;
1985 #endif
1987 #ifdef HAVE_cc0
1988 /* Don't combine an insn that follows a CC0-setting insn.
1989 An insn that uses CC0 must not be separated from the one that sets it.
1990 We do, however, allow I2 to follow a CC0-setting insn if that insn
1991 is passed as I1; in that case it will be deleted also.
1992 We also allow combining in this case if all the insns are adjacent
1993 because that would leave the two CC0 insns adjacent as well.
1994 It would be more logical to test whether CC0 occurs inside I1 or I2,
1995 but that would be much slower, and this ought to be equivalent. */
1997 p = prev_nonnote_insn (insn);
1998 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1999 && ! all_adjacent)
2000 return 0;
2001 #endif
2003 /* If we get here, we have passed all the tests and the combination is
2004 to be allowed. */
2006 *pdest = dest;
2007 *psrc = src;
2009 return 1;
2012 /* LOC is the location within I3 that contains its pattern or the component
2013 of a PARALLEL of the pattern. We validate that it is valid for combining.
2015 One problem is if I3 modifies its output, as opposed to replacing it
2016 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2017 doing so would produce an insn that is not equivalent to the original insns.
2019 Consider:
2021 (set (reg:DI 101) (reg:DI 100))
2022 (set (subreg:SI (reg:DI 101) 0) <foo>)
2024 This is NOT equivalent to:
2026 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2027 (set (reg:DI 101) (reg:DI 100))])
2029 Not only does this modify 100 (in which case it might still be valid
2030 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2032 We can also run into a problem if I2 sets a register that I1
2033 uses and I1 gets directly substituted into I3 (not via I2). In that
2034 case, we would be getting the wrong value of I2DEST into I3, so we
2035 must reject the combination. This case occurs when I2 and I1 both
2036 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2037 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2038 of a SET must prevent combination from occurring. The same situation
2039 can occur for I0, in which case I0_NOT_IN_SRC is set.
2041 Before doing the above check, we first try to expand a field assignment
2042 into a set of logical operations.
2044 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2045 we place a register that is both set and used within I3. If more than one
2046 such register is detected, we fail.
2048 Return 1 if the combination is valid, zero otherwise. */
2050 static int
2051 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2052 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2054 rtx x = *loc;
2056 if (GET_CODE (x) == SET)
2058 rtx set = x ;
2059 rtx dest = SET_DEST (set);
2060 rtx src = SET_SRC (set);
2061 rtx inner_dest = dest;
2062 rtx subdest;
2064 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2065 || GET_CODE (inner_dest) == SUBREG
2066 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2067 inner_dest = XEXP (inner_dest, 0);
2069 /* Check for the case where I3 modifies its output, as discussed
2070 above. We don't want to prevent pseudos from being combined
2071 into the address of a MEM, so only prevent the combination if
2072 i1 or i2 set the same MEM. */
2073 if ((inner_dest != dest &&
2074 (!MEM_P (inner_dest)
2075 || rtx_equal_p (i2dest, inner_dest)
2076 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2077 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2078 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2079 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2080 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2082 /* This is the same test done in can_combine_p except we can't test
2083 all_adjacent; we don't have to, since this instruction will stay
2084 in place, thus we are not considering increasing the lifetime of
2085 INNER_DEST.
2087 Also, if this insn sets a function argument, combining it with
2088 something that might need a spill could clobber a previous
2089 function argument; the all_adjacent test in can_combine_p also
2090 checks this; here, we do a more specific test for this case. */
2092 || (REG_P (inner_dest)
2093 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2094 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2095 GET_MODE (inner_dest))))
2096 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2097 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2098 return 0;
2100 /* If DEST is used in I3, it is being killed in this insn, so
2101 record that for later. We have to consider paradoxical
2102 subregs here, since they kill the whole register, but we
2103 ignore partial subregs, STRICT_LOW_PART, etc.
2104 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2105 STACK_POINTER_REGNUM, since these are always considered to be
2106 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2107 subdest = dest;
2108 if (GET_CODE (subdest) == SUBREG
2109 && (GET_MODE_SIZE (GET_MODE (subdest))
2110 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2111 subdest = SUBREG_REG (subdest);
2112 if (pi3dest_killed
2113 && REG_P (subdest)
2114 && reg_referenced_p (subdest, PATTERN (i3))
2115 && REGNO (subdest) != FRAME_POINTER_REGNUM
2116 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2117 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2118 #endif
2119 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2120 && (REGNO (subdest) != ARG_POINTER_REGNUM
2121 || ! fixed_regs [REGNO (subdest)])
2122 #endif
2123 && REGNO (subdest) != STACK_POINTER_REGNUM)
2125 if (*pi3dest_killed)
2126 return 0;
2128 *pi3dest_killed = subdest;
2132 else if (GET_CODE (x) == PARALLEL)
2134 int i;
2136 for (i = 0; i < XVECLEN (x, 0); i++)
2137 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2138 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2139 return 0;
2142 return 1;
2145 /* Return 1 if X is an arithmetic expression that contains a multiplication
2146 and division. We don't count multiplications by powers of two here. */
2148 static int
2149 contains_muldiv (rtx x)
2151 switch (GET_CODE (x))
2153 case MOD: case DIV: case UMOD: case UDIV:
2154 return 1;
2156 case MULT:
2157 return ! (CONST_INT_P (XEXP (x, 1))
2158 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2159 default:
2160 if (BINARY_P (x))
2161 return contains_muldiv (XEXP (x, 0))
2162 || contains_muldiv (XEXP (x, 1));
2164 if (UNARY_P (x))
2165 return contains_muldiv (XEXP (x, 0));
2167 return 0;
2171 /* Determine whether INSN can be used in a combination. Return nonzero if
2172 not. This is used in try_combine to detect early some cases where we
2173 can't perform combinations. */
2175 static int
2176 cant_combine_insn_p (rtx insn)
2178 rtx set;
2179 rtx src, dest;
2181 /* If this isn't really an insn, we can't do anything.
2182 This can occur when flow deletes an insn that it has merged into an
2183 auto-increment address. */
2184 if (! INSN_P (insn))
2185 return 1;
2187 /* Never combine loads and stores involving hard regs that are likely
2188 to be spilled. The register allocator can usually handle such
2189 reg-reg moves by tying. If we allow the combiner to make
2190 substitutions of likely-spilled regs, reload might die.
2191 As an exception, we allow combinations involving fixed regs; these are
2192 not available to the register allocator so there's no risk involved. */
2194 set = single_set (insn);
2195 if (! set)
2196 return 0;
2197 src = SET_SRC (set);
2198 dest = SET_DEST (set);
2199 if (GET_CODE (src) == SUBREG)
2200 src = SUBREG_REG (src);
2201 if (GET_CODE (dest) == SUBREG)
2202 dest = SUBREG_REG (dest);
2203 if (REG_P (src) && REG_P (dest)
2204 && ((HARD_REGISTER_P (src)
2205 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2206 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2207 || (HARD_REGISTER_P (dest)
2208 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2209 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2210 return 1;
2212 return 0;
2215 struct likely_spilled_retval_info
2217 unsigned regno, nregs;
2218 unsigned mask;
2221 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2222 hard registers that are known to be written to / clobbered in full. */
2223 static void
2224 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2226 struct likely_spilled_retval_info *const info =
2227 (struct likely_spilled_retval_info *) data;
2228 unsigned regno, nregs;
2229 unsigned new_mask;
2231 if (!REG_P (XEXP (set, 0)))
2232 return;
2233 regno = REGNO (x);
2234 if (regno >= info->regno + info->nregs)
2235 return;
2236 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2237 if (regno + nregs <= info->regno)
2238 return;
2239 new_mask = (2U << (nregs - 1)) - 1;
2240 if (regno < info->regno)
2241 new_mask >>= info->regno - regno;
2242 else
2243 new_mask <<= regno - info->regno;
2244 info->mask &= ~new_mask;
2247 /* Return nonzero iff part of the return value is live during INSN, and
2248 it is likely spilled. This can happen when more than one insn is needed
2249 to copy the return value, e.g. when we consider to combine into the
2250 second copy insn for a complex value. */
2252 static int
2253 likely_spilled_retval_p (rtx insn)
2255 rtx use = BB_END (this_basic_block);
2256 rtx reg, p;
2257 unsigned regno, nregs;
2258 /* We assume here that no machine mode needs more than
2259 32 hard registers when the value overlaps with a register
2260 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2261 unsigned mask;
2262 struct likely_spilled_retval_info info;
2264 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2265 return 0;
2266 reg = XEXP (PATTERN (use), 0);
2267 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2268 return 0;
2269 regno = REGNO (reg);
2270 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2271 if (nregs == 1)
2272 return 0;
2273 mask = (2U << (nregs - 1)) - 1;
2275 /* Disregard parts of the return value that are set later. */
2276 info.regno = regno;
2277 info.nregs = nregs;
2278 info.mask = mask;
2279 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2280 if (INSN_P (p))
2281 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2282 mask = info.mask;
2284 /* Check if any of the (probably) live return value registers is
2285 likely spilled. */
2286 nregs --;
2289 if ((mask & 1 << nregs)
2290 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2291 return 1;
2292 } while (nregs--);
2293 return 0;
2296 /* Adjust INSN after we made a change to its destination.
2298 Changing the destination can invalidate notes that say something about
2299 the results of the insn and a LOG_LINK pointing to the insn. */
2301 static void
2302 adjust_for_new_dest (rtx insn)
2304 /* For notes, be conservative and simply remove them. */
2305 remove_reg_equal_equiv_notes (insn);
2307 /* The new insn will have a destination that was previously the destination
2308 of an insn just above it. Call distribute_links to make a LOG_LINK from
2309 the next use of that destination. */
2310 distribute_links (alloc_insn_link (insn, NULL));
2312 df_insn_rescan (insn);
2315 /* Return TRUE if combine can reuse reg X in mode MODE.
2316 ADDED_SETS is nonzero if the original set is still required. */
2317 static bool
2318 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2320 unsigned int regno;
2322 if (!REG_P(x))
2323 return false;
2325 regno = REGNO (x);
2326 /* Allow hard registers if the new mode is legal, and occupies no more
2327 registers than the old mode. */
2328 if (regno < FIRST_PSEUDO_REGISTER)
2329 return (HARD_REGNO_MODE_OK (regno, mode)
2330 && (hard_regno_nregs[regno][GET_MODE (x)]
2331 >= hard_regno_nregs[regno][mode]));
2333 /* Or a pseudo that is only used once. */
2334 return (REG_N_SETS (regno) == 1 && !added_sets
2335 && !REG_USERVAR_P (x));
2339 /* Check whether X, the destination of a set, refers to part of
2340 the register specified by REG. */
2342 static bool
2343 reg_subword_p (rtx x, rtx reg)
2345 /* Check that reg is an integer mode register. */
2346 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2347 return false;
2349 if (GET_CODE (x) == STRICT_LOW_PART
2350 || GET_CODE (x) == ZERO_EXTRACT)
2351 x = XEXP (x, 0);
2353 return GET_CODE (x) == SUBREG
2354 && SUBREG_REG (x) == reg
2355 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2358 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2359 Note that the INSN should be deleted *after* removing dead edges, so
2360 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2361 but not for a (set (pc) (label_ref FOO)). */
2363 static void
2364 update_cfg_for_uncondjump (rtx insn)
2366 basic_block bb = BLOCK_FOR_INSN (insn);
2367 gcc_assert (BB_END (bb) == insn);
2369 purge_dead_edges (bb);
2371 delete_insn (insn);
2372 if (EDGE_COUNT (bb->succs) == 1)
2374 rtx insn;
2376 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2378 /* Remove barriers from the footer if there are any. */
2379 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2380 if (BARRIER_P (insn))
2382 if (PREV_INSN (insn))
2383 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2384 else
2385 BB_FOOTER (bb) = NEXT_INSN (insn);
2386 if (NEXT_INSN (insn))
2387 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2389 else if (LABEL_P (insn))
2390 break;
2394 /* Try to combine the insns I0, I1 and I2 into I3.
2395 Here I0, I1 and I2 appear earlier than I3.
2396 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2399 If we are combining more than two insns and the resulting insn is not
2400 recognized, try splitting it into two insns. If that happens, I2 and I3
2401 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2402 Otherwise, I0, I1 and I2 are pseudo-deleted.
2404 Return 0 if the combination does not work. Then nothing is changed.
2405 If we did the combination, return the insn at which combine should
2406 resume scanning.
2408 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2409 new direct jump instruction.
2411 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2412 been I3 passed to an earlier try_combine within the same basic
2413 block. */
2415 static rtx
2416 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p,
2417 rtx last_combined_insn)
2419 /* New patterns for I3 and I2, respectively. */
2420 rtx newpat, newi2pat = 0;
2421 rtvec newpat_vec_with_clobbers = 0;
2422 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2423 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2424 dead. */
2425 int added_sets_0, added_sets_1, added_sets_2;
2426 /* Total number of SETs to put into I3. */
2427 int total_sets;
2428 /* Nonzero if I2's or I1's body now appears in I3. */
2429 int i2_is_used = 0, i1_is_used = 0;
2430 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2431 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2432 /* Contains I3 if the destination of I3 is used in its source, which means
2433 that the old life of I3 is being killed. If that usage is placed into
2434 I2 and not in I3, a REG_DEAD note must be made. */
2435 rtx i3dest_killed = 0;
2436 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2437 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2438 /* Copy of SET_SRC of I1 and I0, if needed. */
2439 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2440 /* Set if I2DEST was reused as a scratch register. */
2441 bool i2scratch = false;
2442 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2443 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2444 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2445 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2446 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2447 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2448 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2449 /* Notes that must be added to REG_NOTES in I3 and I2. */
2450 rtx new_i3_notes, new_i2_notes;
2451 /* Notes that we substituted I3 into I2 instead of the normal case. */
2452 int i3_subst_into_i2 = 0;
2453 /* Notes that I1, I2 or I3 is a MULT operation. */
2454 int have_mult = 0;
2455 int swap_i2i3 = 0;
2456 int changed_i3_dest = 0;
2458 int maxreg;
2459 rtx temp;
2460 struct insn_link *link;
2461 rtx other_pat = 0;
2462 rtx new_other_notes;
2463 int i;
2465 /* Only try four-insn combinations when there's high likelihood of
2466 success. Look for simple insns, such as loads of constants or
2467 binary operations involving a constant. */
2468 if (i0)
2470 int i;
2471 int ngood = 0;
2472 int nshift = 0;
2474 if (!flag_expensive_optimizations)
2475 return 0;
2477 for (i = 0; i < 4; i++)
2479 rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2480 rtx set = single_set (insn);
2481 rtx src;
2482 if (!set)
2483 continue;
2484 src = SET_SRC (set);
2485 if (CONSTANT_P (src))
2487 ngood += 2;
2488 break;
2490 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2491 ngood++;
2492 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2493 || GET_CODE (src) == LSHIFTRT)
2494 nshift++;
2496 if (ngood < 2 && nshift < 2)
2497 return 0;
2500 /* Exit early if one of the insns involved can't be used for
2501 combinations. */
2502 if (cant_combine_insn_p (i3)
2503 || cant_combine_insn_p (i2)
2504 || (i1 && cant_combine_insn_p (i1))
2505 || (i0 && cant_combine_insn_p (i0))
2506 || likely_spilled_retval_p (i3))
2507 return 0;
2509 combine_attempts++;
2510 undobuf.other_insn = 0;
2512 /* Reset the hard register usage information. */
2513 CLEAR_HARD_REG_SET (newpat_used_regs);
2515 if (dump_file && (dump_flags & TDF_DETAILS))
2517 if (i0)
2518 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2519 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2520 else if (i1)
2521 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2522 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2523 else
2524 fprintf (dump_file, "\nTrying %d -> %d:\n",
2525 INSN_UID (i2), INSN_UID (i3));
2528 /* If multiple insns feed into one of I2 or I3, they can be in any
2529 order. To simplify the code below, reorder them in sequence. */
2530 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2531 temp = i2, i2 = i0, i0 = temp;
2532 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2533 temp = i1, i1 = i0, i0 = temp;
2534 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2535 temp = i1, i1 = i2, i2 = temp;
2537 added_links_insn = 0;
2539 /* First check for one important special case that the code below will
2540 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2541 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2542 we may be able to replace that destination with the destination of I3.
2543 This occurs in the common code where we compute both a quotient and
2544 remainder into a structure, in which case we want to do the computation
2545 directly into the structure to avoid register-register copies.
2547 Note that this case handles both multiple sets in I2 and also cases
2548 where I2 has a number of CLOBBERs inside the PARALLEL.
2550 We make very conservative checks below and only try to handle the
2551 most common cases of this. For example, we only handle the case
2552 where I2 and I3 are adjacent to avoid making difficult register
2553 usage tests. */
2555 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2556 && REG_P (SET_SRC (PATTERN (i3)))
2557 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2558 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2559 && GET_CODE (PATTERN (i2)) == PARALLEL
2560 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2561 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2562 below would need to check what is inside (and reg_overlap_mentioned_p
2563 doesn't support those codes anyway). Don't allow those destinations;
2564 the resulting insn isn't likely to be recognized anyway. */
2565 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2566 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2567 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2568 SET_DEST (PATTERN (i3)))
2569 && next_active_insn (i2) == i3)
2571 rtx p2 = PATTERN (i2);
2573 /* Make sure that the destination of I3,
2574 which we are going to substitute into one output of I2,
2575 is not used within another output of I2. We must avoid making this:
2576 (parallel [(set (mem (reg 69)) ...)
2577 (set (reg 69) ...)])
2578 which is not well-defined as to order of actions.
2579 (Besides, reload can't handle output reloads for this.)
2581 The problem can also happen if the dest of I3 is a memory ref,
2582 if another dest in I2 is an indirect memory ref. */
2583 for (i = 0; i < XVECLEN (p2, 0); i++)
2584 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2585 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2586 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2587 SET_DEST (XVECEXP (p2, 0, i))))
2588 break;
2590 if (i == XVECLEN (p2, 0))
2591 for (i = 0; i < XVECLEN (p2, 0); i++)
2592 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2593 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2595 combine_merges++;
2597 subst_insn = i3;
2598 subst_low_luid = DF_INSN_LUID (i2);
2600 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2601 i2src = SET_SRC (XVECEXP (p2, 0, i));
2602 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2603 i2dest_killed = dead_or_set_p (i2, i2dest);
2605 /* Replace the dest in I2 with our dest and make the resulting
2606 insn the new pattern for I3. Then skip to where we validate
2607 the pattern. Everything was set up above. */
2608 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2609 newpat = p2;
2610 i3_subst_into_i2 = 1;
2611 goto validate_replacement;
2615 /* If I2 is setting a pseudo to a constant and I3 is setting some
2616 sub-part of it to another constant, merge them by making a new
2617 constant. */
2618 if (i1 == 0
2619 && (temp = single_set (i2)) != 0
2620 && (CONST_INT_P (SET_SRC (temp))
2621 || CONST_DOUBLE_AS_INT_P (SET_SRC (temp)))
2622 && GET_CODE (PATTERN (i3)) == SET
2623 && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2624 || CONST_DOUBLE_AS_INT_P (SET_SRC (PATTERN (i3))))
2625 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2627 rtx dest = SET_DEST (PATTERN (i3));
2628 int offset = -1;
2629 int width = 0;
2631 if (GET_CODE (dest) == ZERO_EXTRACT)
2633 if (CONST_INT_P (XEXP (dest, 1))
2634 && CONST_INT_P (XEXP (dest, 2)))
2636 width = INTVAL (XEXP (dest, 1));
2637 offset = INTVAL (XEXP (dest, 2));
2638 dest = XEXP (dest, 0);
2639 if (BITS_BIG_ENDIAN)
2640 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2643 else
2645 if (GET_CODE (dest) == STRICT_LOW_PART)
2646 dest = XEXP (dest, 0);
2647 width = GET_MODE_PRECISION (GET_MODE (dest));
2648 offset = 0;
2651 if (offset >= 0)
2653 /* If this is the low part, we're done. */
2654 if (subreg_lowpart_p (dest))
2656 /* Handle the case where inner is twice the size of outer. */
2657 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp)))
2658 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2659 offset += GET_MODE_PRECISION (GET_MODE (dest));
2660 /* Otherwise give up for now. */
2661 else
2662 offset = -1;
2665 if (offset >= 0
2666 && (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp)))
2667 <= HOST_BITS_PER_DOUBLE_INT))
2669 double_int m, o, i;
2670 rtx inner = SET_SRC (PATTERN (i3));
2671 rtx outer = SET_SRC (temp);
2673 o = rtx_to_double_int (outer);
2674 i = rtx_to_double_int (inner);
2676 m = double_int_mask (width);
2677 i = double_int_and (i, m);
2678 m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2679 i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2680 o = double_int_ior (double_int_and_not (o, m), i);
2682 combine_merges++;
2683 subst_insn = i3;
2684 subst_low_luid = DF_INSN_LUID (i2);
2685 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2686 i2dest = SET_DEST (temp);
2687 i2dest_killed = dead_or_set_p (i2, i2dest);
2689 /* Replace the source in I2 with the new constant and make the
2690 resulting insn the new pattern for I3. Then skip to where we
2691 validate the pattern. Everything was set up above. */
2692 SUBST (SET_SRC (temp),
2693 immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2695 newpat = PATTERN (i2);
2697 /* The dest of I3 has been replaced with the dest of I2. */
2698 changed_i3_dest = 1;
2699 goto validate_replacement;
2703 #ifndef HAVE_cc0
2704 /* If we have no I1 and I2 looks like:
2705 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2706 (set Y OP)])
2707 make up a dummy I1 that is
2708 (set Y OP)
2709 and change I2 to be
2710 (set (reg:CC X) (compare:CC Y (const_int 0)))
2712 (We can ignore any trailing CLOBBERs.)
2714 This undoes a previous combination and allows us to match a branch-and-
2715 decrement insn. */
2717 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2718 && XVECLEN (PATTERN (i2), 0) >= 2
2719 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2720 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2721 == MODE_CC)
2722 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2723 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2724 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2725 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2726 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2727 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2729 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2730 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2731 break;
2733 if (i == 1)
2735 /* We make I1 with the same INSN_UID as I2. This gives it
2736 the same DF_INSN_LUID for value tracking. Our fake I1 will
2737 never appear in the insn stream so giving it the same INSN_UID
2738 as I2 will not cause a problem. */
2740 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2741 BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2742 INSN_LOCATOR (i2), -1, NULL_RTX);
2744 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2745 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2746 SET_DEST (PATTERN (i1)));
2747 SUBST_LINK (LOG_LINKS (i2), alloc_insn_link (i1, LOG_LINKS (i2)));
2750 #endif
2752 /* Verify that I2 and I1 are valid for combining. */
2753 if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2754 || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2755 &i1dest, &i1src))
2756 || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2757 &i0dest, &i0src)))
2759 undo_all ();
2760 return 0;
2763 /* Record whether I2DEST is used in I2SRC and similarly for the other
2764 cases. Knowing this will help in register status updating below. */
2765 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2766 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2767 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2768 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2769 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2770 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2771 i2dest_killed = dead_or_set_p (i2, i2dest);
2772 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2773 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2775 /* For the earlier insns, determine which of the subsequent ones they
2776 feed. */
2777 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2778 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2779 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2780 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2781 && reg_overlap_mentioned_p (i0dest, i2src))));
2783 /* Ensure that I3's pattern can be the destination of combines. */
2784 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2785 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2786 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2787 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2788 &i3dest_killed))
2790 undo_all ();
2791 return 0;
2794 /* See if any of the insns is a MULT operation. Unless one is, we will
2795 reject a combination that is, since it must be slower. Be conservative
2796 here. */
2797 if (GET_CODE (i2src) == MULT
2798 || (i1 != 0 && GET_CODE (i1src) == MULT)
2799 || (i0 != 0 && GET_CODE (i0src) == MULT)
2800 || (GET_CODE (PATTERN (i3)) == SET
2801 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2802 have_mult = 1;
2804 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2805 We used to do this EXCEPT in one case: I3 has a post-inc in an
2806 output operand. However, that exception can give rise to insns like
2807 mov r3,(r3)+
2808 which is a famous insn on the PDP-11 where the value of r3 used as the
2809 source was model-dependent. Avoid this sort of thing. */
2811 #if 0
2812 if (!(GET_CODE (PATTERN (i3)) == SET
2813 && REG_P (SET_SRC (PATTERN (i3)))
2814 && MEM_P (SET_DEST (PATTERN (i3)))
2815 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2816 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2817 /* It's not the exception. */
2818 #endif
2819 #ifdef AUTO_INC_DEC
2821 rtx link;
2822 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2823 if (REG_NOTE_KIND (link) == REG_INC
2824 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2825 || (i1 != 0
2826 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2828 undo_all ();
2829 return 0;
2832 #endif
2834 /* See if the SETs in I1 or I2 need to be kept around in the merged
2835 instruction: whenever the value set there is still needed past I3.
2836 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2838 For the SET in I1, we have two cases: If I1 and I2 independently
2839 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2840 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2841 in I1 needs to be kept around unless I1DEST dies or is set in either
2842 I2 or I3. The same consideration applies to I0. */
2844 added_sets_2 = !dead_or_set_p (i3, i2dest);
2846 if (i1)
2847 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2848 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2849 else
2850 added_sets_1 = 0;
2852 if (i0)
2853 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2854 || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
2855 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
2856 else
2857 added_sets_0 = 0;
2859 /* We are about to copy insns for the case where they need to be kept
2860 around. Check that they can be copied in the merged instruction. */
2862 if (targetm.cannot_copy_insn_p
2863 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2864 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2865 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2867 undo_all ();
2868 return 0;
2871 /* If the set in I2 needs to be kept around, we must make a copy of
2872 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2873 PATTERN (I2), we are only substituting for the original I1DEST, not into
2874 an already-substituted copy. This also prevents making self-referential
2875 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2876 I2DEST. */
2878 if (added_sets_2)
2880 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2881 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2882 else
2883 i2pat = copy_rtx (PATTERN (i2));
2886 if (added_sets_1)
2888 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2889 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2890 else
2891 i1pat = copy_rtx (PATTERN (i1));
2894 if (added_sets_0)
2896 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2897 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2898 else
2899 i0pat = copy_rtx (PATTERN (i0));
2902 combine_merges++;
2904 /* Substitute in the latest insn for the regs set by the earlier ones. */
2906 maxreg = max_reg_num ();
2908 subst_insn = i3;
2910 #ifndef HAVE_cc0
2911 /* Many machines that don't use CC0 have insns that can both perform an
2912 arithmetic operation and set the condition code. These operations will
2913 be represented as a PARALLEL with the first element of the vector
2914 being a COMPARE of an arithmetic operation with the constant zero.
2915 The second element of the vector will set some pseudo to the result
2916 of the same arithmetic operation. If we simplify the COMPARE, we won't
2917 match such a pattern and so will generate an extra insn. Here we test
2918 for this case, where both the comparison and the operation result are
2919 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2920 I2SRC. Later we will make the PARALLEL that contains I2. */
2922 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2923 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2924 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
2925 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2927 rtx newpat_dest;
2928 rtx *cc_use_loc = NULL, cc_use_insn = NULL_RTX;
2929 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
2930 enum machine_mode compare_mode, orig_compare_mode;
2931 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
2933 newpat = PATTERN (i3);
2934 newpat_dest = SET_DEST (newpat);
2935 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
2937 if (undobuf.other_insn == 0
2938 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
2939 &cc_use_insn)))
2941 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
2942 compare_code = simplify_compare_const (compare_code,
2943 op0, &op1);
2944 #ifdef CANONICALIZE_COMPARISON
2945 CANONICALIZE_COMPARISON (compare_code, op0, op1);
2946 #endif
2949 /* Do the rest only if op1 is const0_rtx, which may be the
2950 result of simplification. */
2951 if (op1 == const0_rtx)
2953 /* If a single use of the CC is found, prepare to modify it
2954 when SELECT_CC_MODE returns a new CC-class mode, or when
2955 the above simplify_compare_const() returned a new comparison
2956 operator. undobuf.other_insn is assigned the CC use insn
2957 when modifying it. */
2958 if (cc_use_loc)
2960 #ifdef SELECT_CC_MODE
2961 enum machine_mode new_mode
2962 = SELECT_CC_MODE (compare_code, op0, op1);
2963 if (new_mode != orig_compare_mode
2964 && can_change_dest_mode (SET_DEST (newpat),
2965 added_sets_2, new_mode))
2967 unsigned int regno = REGNO (newpat_dest);
2968 compare_mode = new_mode;
2969 if (regno < FIRST_PSEUDO_REGISTER)
2970 newpat_dest = gen_rtx_REG (compare_mode, regno);
2971 else
2973 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2974 newpat_dest = regno_reg_rtx[regno];
2977 #endif
2978 /* Cases for modifying the CC-using comparison. */
2979 if (compare_code != orig_compare_code
2980 /* ??? Do we need to verify the zero rtx? */
2981 && XEXP (*cc_use_loc, 1) == const0_rtx)
2983 /* Replace cc_use_loc with entire new RTX. */
2984 SUBST (*cc_use_loc,
2985 gen_rtx_fmt_ee (compare_code, compare_mode,
2986 newpat_dest, const0_rtx));
2987 undobuf.other_insn = cc_use_insn;
2989 else if (compare_mode != orig_compare_mode)
2991 /* Just replace the CC reg with a new mode. */
2992 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
2993 undobuf.other_insn = cc_use_insn;
2997 /* Now we modify the current newpat:
2998 First, SET_DEST(newpat) is updated if the CC mode has been
2999 altered. For targets without SELECT_CC_MODE, this should be
3000 optimized away. */
3001 if (compare_mode != orig_compare_mode)
3002 SUBST (SET_DEST (newpat), newpat_dest);
3003 /* This is always done to propagate i2src into newpat. */
3004 SUBST (SET_SRC (newpat),
3005 gen_rtx_COMPARE (compare_mode, op0, op1));
3006 /* Create new version of i2pat if needed; the below PARALLEL
3007 creation needs this to work correctly. */
3008 if (! rtx_equal_p (i2src, op0))
3009 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3010 i2_is_used = 1;
3013 #endif
3015 if (i2_is_used == 0)
3017 /* It is possible that the source of I2 or I1 may be performing
3018 an unneeded operation, such as a ZERO_EXTEND of something
3019 that is known to have the high part zero. Handle that case
3020 by letting subst look at the inner insns.
3022 Another way to do this would be to have a function that tries
3023 to simplify a single insn instead of merging two or more
3024 insns. We don't do this because of the potential of infinite
3025 loops and because of the potential extra memory required.
3026 However, doing it the way we are is a bit of a kludge and
3027 doesn't catch all cases.
3029 But only do this if -fexpensive-optimizations since it slows
3030 things down and doesn't usually win.
3032 This is not done in the COMPARE case above because the
3033 unmodified I2PAT is used in the PARALLEL and so a pattern
3034 with a modified I2SRC would not match. */
3036 if (flag_expensive_optimizations)
3038 /* Pass pc_rtx so no substitutions are done, just
3039 simplifications. */
3040 if (i1)
3042 subst_low_luid = DF_INSN_LUID (i1);
3043 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3046 subst_low_luid = DF_INSN_LUID (i2);
3047 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3050 n_occurrences = 0; /* `subst' counts here */
3051 subst_low_luid = DF_INSN_LUID (i2);
3053 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3054 copy of I2SRC each time we substitute it, in order to avoid creating
3055 self-referential RTL when we will be substituting I1SRC for I1DEST
3056 later. Likewise if I0 feeds into I2, either directly or indirectly
3057 through I1, and I0DEST is in I0SRC. */
3058 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3059 (i1_feeds_i2_n && i1dest_in_i1src)
3060 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3061 && i0dest_in_i0src));
3062 substed_i2 = 1;
3064 /* Record whether I2's body now appears within I3's body. */
3065 i2_is_used = n_occurrences;
3068 /* If we already got a failure, don't try to do more. Otherwise, try to
3069 substitute I1 if we have it. */
3071 if (i1 && GET_CODE (newpat) != CLOBBER)
3073 /* Check that an autoincrement side-effect on I1 has not been lost.
3074 This happens if I1DEST is mentioned in I2 and dies there, and
3075 has disappeared from the new pattern. */
3076 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3077 && i1_feeds_i2_n
3078 && dead_or_set_p (i2, i1dest)
3079 && !reg_overlap_mentioned_p (i1dest, newpat))
3080 /* Before we can do this substitution, we must redo the test done
3081 above (see detailed comments there) that ensures I1DEST isn't
3082 mentioned in any SETs in NEWPAT that are field assignments. */
3083 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3084 0, 0, 0))
3086 undo_all ();
3087 return 0;
3090 n_occurrences = 0;
3091 subst_low_luid = DF_INSN_LUID (i1);
3093 /* If the following substitution will modify I1SRC, make a copy of it
3094 for the case where it is substituted for I1DEST in I2PAT later. */
3095 if (added_sets_2 && i1_feeds_i2_n)
3096 i1src_copy = copy_rtx (i1src);
3098 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3099 copy of I1SRC each time we substitute it, in order to avoid creating
3100 self-referential RTL when we will be substituting I0SRC for I0DEST
3101 later. */
3102 newpat = subst (newpat, i1dest, i1src, 0, 0,
3103 i0_feeds_i1_n && i0dest_in_i0src);
3104 substed_i1 = 1;
3106 /* Record whether I1's body now appears within I3's body. */
3107 i1_is_used = n_occurrences;
3110 /* Likewise for I0 if we have it. */
3112 if (i0 && GET_CODE (newpat) != CLOBBER)
3114 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3115 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3116 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3117 && !reg_overlap_mentioned_p (i0dest, newpat))
3118 || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3119 0, 0, 0))
3121 undo_all ();
3122 return 0;
3125 /* If the following substitution will modify I0SRC, make a copy of it
3126 for the case where it is substituted for I0DEST in I1PAT later. */
3127 if (added_sets_1 && i0_feeds_i1_n)
3128 i0src_copy = copy_rtx (i0src);
3129 /* And a copy for I0DEST in I2PAT substitution. */
3130 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3131 || (i0_feeds_i2_n)))
3132 i0src_copy2 = copy_rtx (i0src);
3134 n_occurrences = 0;
3135 subst_low_luid = DF_INSN_LUID (i0);
3136 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3137 substed_i0 = 1;
3140 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3141 to count all the ways that I2SRC and I1SRC can be used. */
3142 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3143 && i2_is_used + added_sets_2 > 1)
3144 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3145 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3146 > 1))
3147 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3148 && (n_occurrences + added_sets_0
3149 + (added_sets_1 && i0_feeds_i1_n)
3150 + (added_sets_2 && i0_feeds_i2_n)
3151 > 1))
3152 /* Fail if we tried to make a new register. */
3153 || max_reg_num () != maxreg
3154 /* Fail if we couldn't do something and have a CLOBBER. */
3155 || GET_CODE (newpat) == CLOBBER
3156 /* Fail if this new pattern is a MULT and we didn't have one before
3157 at the outer level. */
3158 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3159 && ! have_mult))
3161 undo_all ();
3162 return 0;
3165 /* If the actions of the earlier insns must be kept
3166 in addition to substituting them into the latest one,
3167 we must make a new PARALLEL for the latest insn
3168 to hold additional the SETs. */
3170 if (added_sets_0 || added_sets_1 || added_sets_2)
3172 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3173 combine_extras++;
3175 if (GET_CODE (newpat) == PARALLEL)
3177 rtvec old = XVEC (newpat, 0);
3178 total_sets = XVECLEN (newpat, 0) + extra_sets;
3179 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3180 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3181 sizeof (old->elem[0]) * old->num_elem);
3183 else
3185 rtx old = newpat;
3186 total_sets = 1 + extra_sets;
3187 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3188 XVECEXP (newpat, 0, 0) = old;
3191 if (added_sets_0)
3192 XVECEXP (newpat, 0, --total_sets) = i0pat;
3194 if (added_sets_1)
3196 rtx t = i1pat;
3197 if (i0_feeds_i1_n)
3198 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3200 XVECEXP (newpat, 0, --total_sets) = t;
3202 if (added_sets_2)
3204 rtx t = i2pat;
3205 if (i1_feeds_i2_n)
3206 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3207 i0_feeds_i1_n && i0dest_in_i0src);
3208 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3209 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3211 XVECEXP (newpat, 0, --total_sets) = t;
3215 validate_replacement:
3217 /* Note which hard regs this insn has as inputs. */
3218 mark_used_regs_combine (newpat);
3220 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3221 consider splitting this pattern, we might need these clobbers. */
3222 if (i1 && GET_CODE (newpat) == PARALLEL
3223 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3225 int len = XVECLEN (newpat, 0);
3227 newpat_vec_with_clobbers = rtvec_alloc (len);
3228 for (i = 0; i < len; i++)
3229 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3232 /* Is the result of combination a valid instruction? */
3233 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3235 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3236 the second SET's destination is a register that is unused and isn't
3237 marked as an instruction that might trap in an EH region. In that case,
3238 we just need the first SET. This can occur when simplifying a divmod
3239 insn. We *must* test for this case here because the code below that
3240 splits two independent SETs doesn't handle this case correctly when it
3241 updates the register status.
3243 It's pointless doing this if we originally had two sets, one from
3244 i3, and one from i2. Combining then splitting the parallel results
3245 in the original i2 again plus an invalid insn (which we delete).
3246 The net effect is only to move instructions around, which makes
3247 debug info less accurate.
3249 Also check the case where the first SET's destination is unused.
3250 That would not cause incorrect code, but does cause an unneeded
3251 insn to remain. */
3253 if (insn_code_number < 0
3254 && !(added_sets_2 && i1 == 0)
3255 && GET_CODE (newpat) == PARALLEL
3256 && XVECLEN (newpat, 0) == 2
3257 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3258 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3259 && asm_noperands (newpat) < 0)
3261 rtx set0 = XVECEXP (newpat, 0, 0);
3262 rtx set1 = XVECEXP (newpat, 0, 1);
3264 if (((REG_P (SET_DEST (set1))
3265 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3266 || (GET_CODE (SET_DEST (set1)) == SUBREG
3267 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3268 && insn_nothrow_p (i3)
3269 && !side_effects_p (SET_SRC (set1)))
3271 newpat = set0;
3272 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3275 else if (((REG_P (SET_DEST (set0))
3276 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3277 || (GET_CODE (SET_DEST (set0)) == SUBREG
3278 && find_reg_note (i3, REG_UNUSED,
3279 SUBREG_REG (SET_DEST (set0)))))
3280 && insn_nothrow_p (i3)
3281 && !side_effects_p (SET_SRC (set0)))
3283 newpat = set1;
3284 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3286 if (insn_code_number >= 0)
3287 changed_i3_dest = 1;
3291 /* If we were combining three insns and the result is a simple SET
3292 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3293 insns. There are two ways to do this. It can be split using a
3294 machine-specific method (like when you have an addition of a large
3295 constant) or by combine in the function find_split_point. */
3297 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3298 && asm_noperands (newpat) < 0)
3300 rtx parallel, m_split, *split;
3302 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3303 use I2DEST as a scratch register will help. In the latter case,
3304 convert I2DEST to the mode of the source of NEWPAT if we can. */
3306 m_split = combine_split_insns (newpat, i3);
3308 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3309 inputs of NEWPAT. */
3311 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3312 possible to try that as a scratch reg. This would require adding
3313 more code to make it work though. */
3315 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3317 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3319 /* First try to split using the original register as a
3320 scratch register. */
3321 parallel = gen_rtx_PARALLEL (VOIDmode,
3322 gen_rtvec (2, newpat,
3323 gen_rtx_CLOBBER (VOIDmode,
3324 i2dest)));
3325 m_split = combine_split_insns (parallel, i3);
3327 /* If that didn't work, try changing the mode of I2DEST if
3328 we can. */
3329 if (m_split == 0
3330 && new_mode != GET_MODE (i2dest)
3331 && new_mode != VOIDmode
3332 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3334 enum machine_mode old_mode = GET_MODE (i2dest);
3335 rtx ni2dest;
3337 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3338 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3339 else
3341 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3342 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3345 parallel = (gen_rtx_PARALLEL
3346 (VOIDmode,
3347 gen_rtvec (2, newpat,
3348 gen_rtx_CLOBBER (VOIDmode,
3349 ni2dest))));
3350 m_split = combine_split_insns (parallel, i3);
3352 if (m_split == 0
3353 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3355 struct undo *buf;
3357 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3358 buf = undobuf.undos;
3359 undobuf.undos = buf->next;
3360 buf->next = undobuf.frees;
3361 undobuf.frees = buf;
3365 i2scratch = m_split != 0;
3368 /* If recog_for_combine has discarded clobbers, try to use them
3369 again for the split. */
3370 if (m_split == 0 && newpat_vec_with_clobbers)
3372 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3373 m_split = combine_split_insns (parallel, i3);
3376 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3378 m_split = PATTERN (m_split);
3379 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3380 if (insn_code_number >= 0)
3381 newpat = m_split;
3383 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3384 && (next_nonnote_nondebug_insn (i2) == i3
3385 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3387 rtx i2set, i3set;
3388 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3389 newi2pat = PATTERN (m_split);
3391 i3set = single_set (NEXT_INSN (m_split));
3392 i2set = single_set (m_split);
3394 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3396 /* If I2 or I3 has multiple SETs, we won't know how to track
3397 register status, so don't use these insns. If I2's destination
3398 is used between I2 and I3, we also can't use these insns. */
3400 if (i2_code_number >= 0 && i2set && i3set
3401 && (next_nonnote_nondebug_insn (i2) == i3
3402 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3403 insn_code_number = recog_for_combine (&newi3pat, i3,
3404 &new_i3_notes);
3405 if (insn_code_number >= 0)
3406 newpat = newi3pat;
3408 /* It is possible that both insns now set the destination of I3.
3409 If so, we must show an extra use of it. */
3411 if (insn_code_number >= 0)
3413 rtx new_i3_dest = SET_DEST (i3set);
3414 rtx new_i2_dest = SET_DEST (i2set);
3416 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3417 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3418 || GET_CODE (new_i3_dest) == SUBREG)
3419 new_i3_dest = XEXP (new_i3_dest, 0);
3421 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3422 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3423 || GET_CODE (new_i2_dest) == SUBREG)
3424 new_i2_dest = XEXP (new_i2_dest, 0);
3426 if (REG_P (new_i3_dest)
3427 && REG_P (new_i2_dest)
3428 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3429 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3433 /* If we can split it and use I2DEST, go ahead and see if that
3434 helps things be recognized. Verify that none of the registers
3435 are set between I2 and I3. */
3436 if (insn_code_number < 0
3437 && (split = find_split_point (&newpat, i3, false)) != 0
3438 #ifdef HAVE_cc0
3439 && REG_P (i2dest)
3440 #endif
3441 /* We need I2DEST in the proper mode. If it is a hard register
3442 or the only use of a pseudo, we can change its mode.
3443 Make sure we don't change a hard register to have a mode that
3444 isn't valid for it, or change the number of registers. */
3445 && (GET_MODE (*split) == GET_MODE (i2dest)
3446 || GET_MODE (*split) == VOIDmode
3447 || can_change_dest_mode (i2dest, added_sets_2,
3448 GET_MODE (*split)))
3449 && (next_nonnote_nondebug_insn (i2) == i3
3450 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3451 /* We can't overwrite I2DEST if its value is still used by
3452 NEWPAT. */
3453 && ! reg_referenced_p (i2dest, newpat))
3455 rtx newdest = i2dest;
3456 enum rtx_code split_code = GET_CODE (*split);
3457 enum machine_mode split_mode = GET_MODE (*split);
3458 bool subst_done = false;
3459 newi2pat = NULL_RTX;
3461 i2scratch = true;
3463 /* *SPLIT may be part of I2SRC, so make sure we have the
3464 original expression around for later debug processing.
3465 We should not need I2SRC any more in other cases. */
3466 if (MAY_HAVE_DEBUG_INSNS)
3467 i2src = copy_rtx (i2src);
3468 else
3469 i2src = NULL;
3471 /* Get NEWDEST as a register in the proper mode. We have already
3472 validated that we can do this. */
3473 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3475 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3476 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3477 else
3479 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3480 newdest = regno_reg_rtx[REGNO (i2dest)];
3484 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3485 an ASHIFT. This can occur if it was inside a PLUS and hence
3486 appeared to be a memory address. This is a kludge. */
3487 if (split_code == MULT
3488 && CONST_INT_P (XEXP (*split, 1))
3489 && INTVAL (XEXP (*split, 1)) > 0
3490 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3492 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3493 XEXP (*split, 0), GEN_INT (i)));
3494 /* Update split_code because we may not have a multiply
3495 anymore. */
3496 split_code = GET_CODE (*split);
3499 #ifdef INSN_SCHEDULING
3500 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3501 be written as a ZERO_EXTEND. */
3502 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3504 #ifdef LOAD_EXTEND_OP
3505 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3506 what it really is. */
3507 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3508 == SIGN_EXTEND)
3509 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3510 SUBREG_REG (*split)));
3511 else
3512 #endif
3513 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3514 SUBREG_REG (*split)));
3516 #endif
3518 /* Attempt to split binary operators using arithmetic identities. */
3519 if (BINARY_P (SET_SRC (newpat))
3520 && split_mode == GET_MODE (SET_SRC (newpat))
3521 && ! side_effects_p (SET_SRC (newpat)))
3523 rtx setsrc = SET_SRC (newpat);
3524 enum machine_mode mode = GET_MODE (setsrc);
3525 enum rtx_code code = GET_CODE (setsrc);
3526 rtx src_op0 = XEXP (setsrc, 0);
3527 rtx src_op1 = XEXP (setsrc, 1);
3529 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3530 if (rtx_equal_p (src_op0, src_op1))
3532 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3533 SUBST (XEXP (setsrc, 0), newdest);
3534 SUBST (XEXP (setsrc, 1), newdest);
3535 subst_done = true;
3537 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3538 else if ((code == PLUS || code == MULT)
3539 && GET_CODE (src_op0) == code
3540 && GET_CODE (XEXP (src_op0, 0)) == code
3541 && (INTEGRAL_MODE_P (mode)
3542 || (FLOAT_MODE_P (mode)
3543 && flag_unsafe_math_optimizations)))
3545 rtx p = XEXP (XEXP (src_op0, 0), 0);
3546 rtx q = XEXP (XEXP (src_op0, 0), 1);
3547 rtx r = XEXP (src_op0, 1);
3548 rtx s = src_op1;
3550 /* Split both "((X op Y) op X) op Y" and
3551 "((X op Y) op Y) op X" as "T op T" where T is
3552 "X op Y". */
3553 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3554 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3556 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3557 XEXP (src_op0, 0));
3558 SUBST (XEXP (setsrc, 0), newdest);
3559 SUBST (XEXP (setsrc, 1), newdest);
3560 subst_done = true;
3562 /* Split "((X op X) op Y) op Y)" as "T op T" where
3563 T is "X op Y". */
3564 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3566 rtx tmp = simplify_gen_binary (code, mode, p, r);
3567 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3568 SUBST (XEXP (setsrc, 0), newdest);
3569 SUBST (XEXP (setsrc, 1), newdest);
3570 subst_done = true;
3575 if (!subst_done)
3577 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3578 SUBST (*split, newdest);
3581 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3583 /* recog_for_combine might have added CLOBBERs to newi2pat.
3584 Make sure NEWPAT does not depend on the clobbered regs. */
3585 if (GET_CODE (newi2pat) == PARALLEL)
3586 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3587 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3589 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3590 if (reg_overlap_mentioned_p (reg, newpat))
3592 undo_all ();
3593 return 0;
3597 /* If the split point was a MULT and we didn't have one before,
3598 don't use one now. */
3599 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3600 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3604 /* Check for a case where we loaded from memory in a narrow mode and
3605 then sign extended it, but we need both registers. In that case,
3606 we have a PARALLEL with both loads from the same memory location.
3607 We can split this into a load from memory followed by a register-register
3608 copy. This saves at least one insn, more if register allocation can
3609 eliminate the copy.
3611 We cannot do this if the destination of the first assignment is a
3612 condition code register or cc0. We eliminate this case by making sure
3613 the SET_DEST and SET_SRC have the same mode.
3615 We cannot do this if the destination of the second assignment is
3616 a register that we have already assumed is zero-extended. Similarly
3617 for a SUBREG of such a register. */
3619 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3620 && GET_CODE (newpat) == PARALLEL
3621 && XVECLEN (newpat, 0) == 2
3622 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3623 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3624 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3625 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3626 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3627 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3628 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3629 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3630 DF_INSN_LUID (i2))
3631 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3632 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3633 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3634 (REG_P (temp)
3635 && VEC_index (reg_stat_type, reg_stat,
3636 REGNO (temp)).nonzero_bits != 0
3637 && GET_MODE_PRECISION (GET_MODE (temp)) < BITS_PER_WORD
3638 && GET_MODE_PRECISION (GET_MODE (temp)) < HOST_BITS_PER_INT
3639 && (VEC_index (reg_stat_type, reg_stat,
3640 REGNO (temp)).nonzero_bits
3641 != GET_MODE_MASK (word_mode))))
3642 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3643 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3644 (REG_P (temp)
3645 && VEC_index (reg_stat_type, reg_stat,
3646 REGNO (temp)).nonzero_bits != 0
3647 && GET_MODE_PRECISION (GET_MODE (temp)) < BITS_PER_WORD
3648 && GET_MODE_PRECISION (GET_MODE (temp)) < HOST_BITS_PER_INT
3649 && (VEC_index (reg_stat_type, reg_stat,
3650 REGNO (temp)).nonzero_bits
3651 != GET_MODE_MASK (word_mode)))))
3652 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3653 SET_SRC (XVECEXP (newpat, 0, 1)))
3654 && ! find_reg_note (i3, REG_UNUSED,
3655 SET_DEST (XVECEXP (newpat, 0, 0))))
3657 rtx ni2dest;
3659 newi2pat = XVECEXP (newpat, 0, 0);
3660 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3661 newpat = XVECEXP (newpat, 0, 1);
3662 SUBST (SET_SRC (newpat),
3663 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3664 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3666 if (i2_code_number >= 0)
3667 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3669 if (insn_code_number >= 0)
3670 swap_i2i3 = 1;
3673 /* Similarly, check for a case where we have a PARALLEL of two independent
3674 SETs but we started with three insns. In this case, we can do the sets
3675 as two separate insns. This case occurs when some SET allows two
3676 other insns to combine, but the destination of that SET is still live. */
3678 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3679 && GET_CODE (newpat) == PARALLEL
3680 && XVECLEN (newpat, 0) == 2
3681 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3682 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3683 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3684 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3685 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3686 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3687 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3688 XVECEXP (newpat, 0, 0))
3689 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3690 XVECEXP (newpat, 0, 1))
3691 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3692 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3694 /* Normally, it doesn't matter which of the two is done first,
3695 but the one that references cc0 can't be the second, and
3696 one which uses any regs/memory set in between i2 and i3 can't
3697 be first. */
3698 if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3699 DF_INSN_LUID (i2))
3700 #ifdef HAVE_cc0
3701 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3702 #endif
3705 newi2pat = XVECEXP (newpat, 0, 1);
3706 newpat = XVECEXP (newpat, 0, 0);
3708 else if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 0)),
3709 DF_INSN_LUID (i2))
3710 #ifdef HAVE_cc0
3711 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1))
3712 #endif
3715 newi2pat = XVECEXP (newpat, 0, 0);
3716 newpat = XVECEXP (newpat, 0, 1);
3718 else
3720 undo_all ();
3721 return 0;
3724 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3726 if (i2_code_number >= 0)
3728 /* recog_for_combine might have added CLOBBERs to newi2pat.
3729 Make sure NEWPAT does not depend on the clobbered regs. */
3730 if (GET_CODE (newi2pat) == PARALLEL)
3732 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3733 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3735 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3736 if (reg_overlap_mentioned_p (reg, newpat))
3738 undo_all ();
3739 return 0;
3744 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3748 /* If it still isn't recognized, fail and change things back the way they
3749 were. */
3750 if ((insn_code_number < 0
3751 /* Is the result a reasonable ASM_OPERANDS? */
3752 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3754 undo_all ();
3755 return 0;
3758 /* If we had to change another insn, make sure it is valid also. */
3759 if (undobuf.other_insn)
3761 CLEAR_HARD_REG_SET (newpat_used_regs);
3763 other_pat = PATTERN (undobuf.other_insn);
3764 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3765 &new_other_notes);
3767 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3769 undo_all ();
3770 return 0;
3774 #ifdef HAVE_cc0
3775 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3776 they are adjacent to each other or not. */
3778 rtx p = prev_nonnote_insn (i3);
3779 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3780 && sets_cc0_p (newi2pat))
3782 undo_all ();
3783 return 0;
3786 #endif
3788 /* Only allow this combination if insn_rtx_costs reports that the
3789 replacement instructions are cheaper than the originals. */
3790 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3792 undo_all ();
3793 return 0;
3796 if (MAY_HAVE_DEBUG_INSNS)
3798 struct undo *undo;
3800 for (undo = undobuf.undos; undo; undo = undo->next)
3801 if (undo->kind == UNDO_MODE)
3803 rtx reg = *undo->where.r;
3804 enum machine_mode new_mode = GET_MODE (reg);
3805 enum machine_mode old_mode = undo->old_contents.m;
3807 /* Temporarily revert mode back. */
3808 adjust_reg_mode (reg, old_mode);
3810 if (reg == i2dest && i2scratch)
3812 /* If we used i2dest as a scratch register with a
3813 different mode, substitute it for the original
3814 i2src while its original mode is temporarily
3815 restored, and then clear i2scratch so that we don't
3816 do it again later. */
3817 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3818 this_basic_block);
3819 i2scratch = false;
3820 /* Put back the new mode. */
3821 adjust_reg_mode (reg, new_mode);
3823 else
3825 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3826 rtx first, last;
3828 if (reg == i2dest)
3830 first = i2;
3831 last = last_combined_insn;
3833 else
3835 first = i3;
3836 last = undobuf.other_insn;
3837 gcc_assert (last);
3838 if (DF_INSN_LUID (last)
3839 < DF_INSN_LUID (last_combined_insn))
3840 last = last_combined_insn;
3843 /* We're dealing with a reg that changed mode but not
3844 meaning, so we want to turn it into a subreg for
3845 the new mode. However, because of REG sharing and
3846 because its mode had already changed, we have to do
3847 it in two steps. First, replace any debug uses of
3848 reg, with its original mode temporarily restored,
3849 with this copy we have created; then, replace the
3850 copy with the SUBREG of the original shared reg,
3851 once again changed to the new mode. */
3852 propagate_for_debug (first, last, reg, tempreg,
3853 this_basic_block);
3854 adjust_reg_mode (reg, new_mode);
3855 propagate_for_debug (first, last, tempreg,
3856 lowpart_subreg (old_mode, reg, new_mode),
3857 this_basic_block);
3862 /* If we will be able to accept this, we have made a
3863 change to the destination of I3. This requires us to
3864 do a few adjustments. */
3866 if (changed_i3_dest)
3868 PATTERN (i3) = newpat;
3869 adjust_for_new_dest (i3);
3872 /* We now know that we can do this combination. Merge the insns and
3873 update the status of registers and LOG_LINKS. */
3875 if (undobuf.other_insn)
3877 rtx note, next;
3879 PATTERN (undobuf.other_insn) = other_pat;
3881 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3882 are still valid. Then add any non-duplicate notes added by
3883 recog_for_combine. */
3884 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3886 next = XEXP (note, 1);
3888 if (REG_NOTE_KIND (note) == REG_UNUSED
3889 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3890 remove_note (undobuf.other_insn, note);
3893 distribute_notes (new_other_notes, undobuf.other_insn,
3894 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
3895 NULL_RTX);
3898 if (swap_i2i3)
3900 rtx insn;
3901 struct insn_link *link;
3902 rtx ni2dest;
3904 /* I3 now uses what used to be its destination and which is now
3905 I2's destination. This requires us to do a few adjustments. */
3906 PATTERN (i3) = newpat;
3907 adjust_for_new_dest (i3);
3909 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3910 so we still will.
3912 However, some later insn might be using I2's dest and have
3913 a LOG_LINK pointing at I3. We must remove this link.
3914 The simplest way to remove the link is to point it at I1,
3915 which we know will be a NOTE. */
3917 /* newi2pat is usually a SET here; however, recog_for_combine might
3918 have added some clobbers. */
3919 if (GET_CODE (newi2pat) == PARALLEL)
3920 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3921 else
3922 ni2dest = SET_DEST (newi2pat);
3924 for (insn = NEXT_INSN (i3);
3925 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3926 || insn != BB_HEAD (this_basic_block->next_bb));
3927 insn = NEXT_INSN (insn))
3929 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3931 FOR_EACH_LOG_LINK (link, insn)
3932 if (link->insn == i3)
3933 link->insn = i1;
3935 break;
3941 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3942 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
3943 rtx midnotes = 0;
3944 int from_luid;
3945 /* Compute which registers we expect to eliminate. newi2pat may be setting
3946 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3947 same as i3dest, in which case newi2pat may be setting i1dest. */
3948 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3949 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
3950 || !i2dest_killed
3951 ? 0 : i2dest);
3952 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
3953 || (newi2pat && reg_set_p (i1dest, newi2pat))
3954 || !i1dest_killed
3955 ? 0 : i1dest);
3956 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
3957 || (newi2pat && reg_set_p (i0dest, newi2pat))
3958 || !i0dest_killed
3959 ? 0 : i0dest);
3961 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3962 clear them. */
3963 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3964 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3965 if (i1)
3966 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3967 if (i0)
3968 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
3970 /* Ensure that we do not have something that should not be shared but
3971 occurs multiple times in the new insns. Check this by first
3972 resetting all the `used' flags and then copying anything is shared. */
3974 reset_used_flags (i3notes);
3975 reset_used_flags (i2notes);
3976 reset_used_flags (i1notes);
3977 reset_used_flags (i0notes);
3978 reset_used_flags (newpat);
3979 reset_used_flags (newi2pat);
3980 if (undobuf.other_insn)
3981 reset_used_flags (PATTERN (undobuf.other_insn));
3983 i3notes = copy_rtx_if_shared (i3notes);
3984 i2notes = copy_rtx_if_shared (i2notes);
3985 i1notes = copy_rtx_if_shared (i1notes);
3986 i0notes = copy_rtx_if_shared (i0notes);
3987 newpat = copy_rtx_if_shared (newpat);
3988 newi2pat = copy_rtx_if_shared (newi2pat);
3989 if (undobuf.other_insn)
3990 reset_used_flags (PATTERN (undobuf.other_insn));
3992 INSN_CODE (i3) = insn_code_number;
3993 PATTERN (i3) = newpat;
3995 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3997 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3999 reset_used_flags (call_usage);
4000 call_usage = copy_rtx (call_usage);
4002 if (substed_i2)
4004 /* I2SRC must still be meaningful at this point. Some splitting
4005 operations can invalidate I2SRC, but those operations do not
4006 apply to calls. */
4007 gcc_assert (i2src);
4008 replace_rtx (call_usage, i2dest, i2src);
4011 if (substed_i1)
4012 replace_rtx (call_usage, i1dest, i1src);
4013 if (substed_i0)
4014 replace_rtx (call_usage, i0dest, i0src);
4016 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4019 if (undobuf.other_insn)
4020 INSN_CODE (undobuf.other_insn) = other_code_number;
4022 /* We had one special case above where I2 had more than one set and
4023 we replaced a destination of one of those sets with the destination
4024 of I3. In that case, we have to update LOG_LINKS of insns later
4025 in this basic block. Note that this (expensive) case is rare.
4027 Also, in this case, we must pretend that all REG_NOTEs for I2
4028 actually came from I3, so that REG_UNUSED notes from I2 will be
4029 properly handled. */
4031 if (i3_subst_into_i2)
4033 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4034 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4035 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4036 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4037 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4038 && ! find_reg_note (i2, REG_UNUSED,
4039 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4040 for (temp = NEXT_INSN (i2);
4041 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4042 || BB_HEAD (this_basic_block) != temp);
4043 temp = NEXT_INSN (temp))
4044 if (temp != i3 && INSN_P (temp))
4045 FOR_EACH_LOG_LINK (link, temp)
4046 if (link->insn == i2)
4047 link->insn = i3;
4049 if (i3notes)
4051 rtx link = i3notes;
4052 while (XEXP (link, 1))
4053 link = XEXP (link, 1);
4054 XEXP (link, 1) = i2notes;
4056 else
4057 i3notes = i2notes;
4058 i2notes = 0;
4061 LOG_LINKS (i3) = NULL;
4062 REG_NOTES (i3) = 0;
4063 LOG_LINKS (i2) = NULL;
4064 REG_NOTES (i2) = 0;
4066 if (newi2pat)
4068 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4069 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4070 this_basic_block);
4071 INSN_CODE (i2) = i2_code_number;
4072 PATTERN (i2) = newi2pat;
4074 else
4076 if (MAY_HAVE_DEBUG_INSNS && i2src)
4077 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4078 this_basic_block);
4079 SET_INSN_DELETED (i2);
4082 if (i1)
4084 LOG_LINKS (i1) = NULL;
4085 REG_NOTES (i1) = 0;
4086 if (MAY_HAVE_DEBUG_INSNS)
4087 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4088 this_basic_block);
4089 SET_INSN_DELETED (i1);
4092 if (i0)
4094 LOG_LINKS (i0) = NULL;
4095 REG_NOTES (i0) = 0;
4096 if (MAY_HAVE_DEBUG_INSNS)
4097 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4098 this_basic_block);
4099 SET_INSN_DELETED (i0);
4102 /* Get death notes for everything that is now used in either I3 or
4103 I2 and used to die in a previous insn. If we built two new
4104 patterns, move from I1 to I2 then I2 to I3 so that we get the
4105 proper movement on registers that I2 modifies. */
4107 if (i0)
4108 from_luid = DF_INSN_LUID (i0);
4109 else if (i1)
4110 from_luid = DF_INSN_LUID (i1);
4111 else
4112 from_luid = DF_INSN_LUID (i2);
4113 if (newi2pat)
4114 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4115 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4117 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4118 if (i3notes)
4119 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4120 elim_i2, elim_i1, elim_i0);
4121 if (i2notes)
4122 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4123 elim_i2, elim_i1, elim_i0);
4124 if (i1notes)
4125 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4126 elim_i2, elim_i1, elim_i0);
4127 if (i0notes)
4128 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4129 elim_i2, elim_i1, elim_i0);
4130 if (midnotes)
4131 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4132 elim_i2, elim_i1, elim_i0);
4134 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4135 know these are REG_UNUSED and want them to go to the desired insn,
4136 so we always pass it as i3. */
4138 if (newi2pat && new_i2_notes)
4139 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4140 NULL_RTX);
4142 if (new_i3_notes)
4143 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4144 NULL_RTX);
4146 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4147 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4148 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4149 in that case, it might delete I2. Similarly for I2 and I1.
4150 Show an additional death due to the REG_DEAD note we make here. If
4151 we discard it in distribute_notes, we will decrement it again. */
4153 if (i3dest_killed)
4155 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4156 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4157 NULL_RTX),
4158 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
4159 else
4160 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4161 NULL_RTX),
4162 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4163 elim_i2, elim_i1, elim_i0);
4166 if (i2dest_in_i2src)
4168 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4169 if (newi2pat && reg_set_p (i2dest, newi2pat))
4170 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4171 NULL_RTX, NULL_RTX);
4172 else
4173 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4174 NULL_RTX, NULL_RTX, NULL_RTX);
4177 if (i1dest_in_i1src)
4179 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4180 if (newi2pat && reg_set_p (i1dest, newi2pat))
4181 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4182 NULL_RTX, NULL_RTX);
4183 else
4184 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4185 NULL_RTX, NULL_RTX, NULL_RTX);
4188 if (i0dest_in_i0src)
4190 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4191 if (newi2pat && reg_set_p (i0dest, newi2pat))
4192 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4193 NULL_RTX, NULL_RTX);
4194 else
4195 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4196 NULL_RTX, NULL_RTX, NULL_RTX);
4199 distribute_links (i3links);
4200 distribute_links (i2links);
4201 distribute_links (i1links);
4202 distribute_links (i0links);
4204 if (REG_P (i2dest))
4206 struct insn_link *link;
4207 rtx i2_insn = 0, i2_val = 0, set;
4209 /* The insn that used to set this register doesn't exist, and
4210 this life of the register may not exist either. See if one of
4211 I3's links points to an insn that sets I2DEST. If it does,
4212 that is now the last known value for I2DEST. If we don't update
4213 this and I2 set the register to a value that depended on its old
4214 contents, we will get confused. If this insn is used, thing
4215 will be set correctly in combine_instructions. */
4216 FOR_EACH_LOG_LINK (link, i3)
4217 if ((set = single_set (link->insn)) != 0
4218 && rtx_equal_p (i2dest, SET_DEST (set)))
4219 i2_insn = link->insn, i2_val = SET_SRC (set);
4221 record_value_for_reg (i2dest, i2_insn, i2_val);
4223 /* If the reg formerly set in I2 died only once and that was in I3,
4224 zero its use count so it won't make `reload' do any work. */
4225 if (! added_sets_2
4226 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4227 && ! i2dest_in_i2src)
4228 INC_REG_N_SETS (REGNO (i2dest), -1);
4231 if (i1 && REG_P (i1dest))
4233 struct insn_link *link;
4234 rtx i1_insn = 0, i1_val = 0, set;
4236 FOR_EACH_LOG_LINK (link, i3)
4237 if ((set = single_set (link->insn)) != 0
4238 && rtx_equal_p (i1dest, SET_DEST (set)))
4239 i1_insn = link->insn, i1_val = SET_SRC (set);
4241 record_value_for_reg (i1dest, i1_insn, i1_val);
4243 if (! added_sets_1 && ! i1dest_in_i1src)
4244 INC_REG_N_SETS (REGNO (i1dest), -1);
4247 if (i0 && REG_P (i0dest))
4249 struct insn_link *link;
4250 rtx i0_insn = 0, i0_val = 0, set;
4252 FOR_EACH_LOG_LINK (link, i3)
4253 if ((set = single_set (link->insn)) != 0
4254 && rtx_equal_p (i0dest, SET_DEST (set)))
4255 i0_insn = link->insn, i0_val = SET_SRC (set);
4257 record_value_for_reg (i0dest, i0_insn, i0_val);
4259 if (! added_sets_0 && ! i0dest_in_i0src)
4260 INC_REG_N_SETS (REGNO (i0dest), -1);
4263 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4264 been made to this insn. The order of
4265 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
4266 can affect nonzero_bits of newpat */
4267 if (newi2pat)
4268 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4269 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4272 if (undobuf.other_insn != NULL_RTX)
4274 if (dump_file)
4276 fprintf (dump_file, "modifying other_insn ");
4277 dump_insn_slim (dump_file, undobuf.other_insn);
4279 df_insn_rescan (undobuf.other_insn);
4282 if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4284 if (dump_file)
4286 fprintf (dump_file, "modifying insn i1 ");
4287 dump_insn_slim (dump_file, i0);
4289 df_insn_rescan (i0);
4292 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4294 if (dump_file)
4296 fprintf (dump_file, "modifying insn i1 ");
4297 dump_insn_slim (dump_file, i1);
4299 df_insn_rescan (i1);
4302 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4304 if (dump_file)
4306 fprintf (dump_file, "modifying insn i2 ");
4307 dump_insn_slim (dump_file, i2);
4309 df_insn_rescan (i2);
4312 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4314 if (dump_file)
4316 fprintf (dump_file, "modifying insn i3 ");
4317 dump_insn_slim (dump_file, i3);
4319 df_insn_rescan (i3);
4322 /* Set new_direct_jump_p if a new return or simple jump instruction
4323 has been created. Adjust the CFG accordingly. */
4325 if (returnjump_p (i3) || any_uncondjump_p (i3))
4327 *new_direct_jump_p = 1;
4328 mark_jump_label (PATTERN (i3), i3, 0);
4329 update_cfg_for_uncondjump (i3);
4332 if (undobuf.other_insn != NULL_RTX
4333 && (returnjump_p (undobuf.other_insn)
4334 || any_uncondjump_p (undobuf.other_insn)))
4336 *new_direct_jump_p = 1;
4337 update_cfg_for_uncondjump (undobuf.other_insn);
4340 /* A noop might also need cleaning up of CFG, if it comes from the
4341 simplification of a jump. */
4342 if (JUMP_P (i3)
4343 && GET_CODE (newpat) == SET
4344 && SET_SRC (newpat) == pc_rtx
4345 && SET_DEST (newpat) == pc_rtx)
4347 *new_direct_jump_p = 1;
4348 update_cfg_for_uncondjump (i3);
4351 if (undobuf.other_insn != NULL_RTX
4352 && JUMP_P (undobuf.other_insn)
4353 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4354 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4355 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4357 *new_direct_jump_p = 1;
4358 update_cfg_for_uncondjump (undobuf.other_insn);
4361 combine_successes++;
4362 undo_commit ();
4364 if (added_links_insn
4365 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4366 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4367 return added_links_insn;
4368 else
4369 return newi2pat ? i2 : i3;
4372 /* Undo all the modifications recorded in undobuf. */
4374 static void
4375 undo_all (void)
4377 struct undo *undo, *next;
4379 for (undo = undobuf.undos; undo; undo = next)
4381 next = undo->next;
4382 switch (undo->kind)
4384 case UNDO_RTX:
4385 *undo->where.r = undo->old_contents.r;
4386 break;
4387 case UNDO_INT:
4388 *undo->where.i = undo->old_contents.i;
4389 break;
4390 case UNDO_MODE:
4391 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4392 break;
4393 case UNDO_LINKS:
4394 *undo->where.l = undo->old_contents.l;
4395 break;
4396 default:
4397 gcc_unreachable ();
4400 undo->next = undobuf.frees;
4401 undobuf.frees = undo;
4404 undobuf.undos = 0;
4407 /* We've committed to accepting the changes we made. Move all
4408 of the undos to the free list. */
4410 static void
4411 undo_commit (void)
4413 struct undo *undo, *next;
4415 for (undo = undobuf.undos; undo; undo = next)
4417 next = undo->next;
4418 undo->next = undobuf.frees;
4419 undobuf.frees = undo;
4421 undobuf.undos = 0;
4424 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4425 where we have an arithmetic expression and return that point. LOC will
4426 be inside INSN.
4428 try_combine will call this function to see if an insn can be split into
4429 two insns. */
4431 static rtx *
4432 find_split_point (rtx *loc, rtx insn, bool set_src)
4434 rtx x = *loc;
4435 enum rtx_code code = GET_CODE (x);
4436 rtx *split;
4437 unsigned HOST_WIDE_INT len = 0;
4438 HOST_WIDE_INT pos = 0;
4439 int unsignedp = 0;
4440 rtx inner = NULL_RTX;
4442 /* First special-case some codes. */
4443 switch (code)
4445 case SUBREG:
4446 #ifdef INSN_SCHEDULING
4447 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4448 point. */
4449 if (MEM_P (SUBREG_REG (x)))
4450 return loc;
4451 #endif
4452 return find_split_point (&SUBREG_REG (x), insn, false);
4454 case MEM:
4455 #ifdef HAVE_lo_sum
4456 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4457 using LO_SUM and HIGH. */
4458 if (GET_CODE (XEXP (x, 0)) == CONST
4459 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4461 enum machine_mode address_mode = get_address_mode (x);
4463 SUBST (XEXP (x, 0),
4464 gen_rtx_LO_SUM (address_mode,
4465 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4466 XEXP (x, 0)));
4467 return &XEXP (XEXP (x, 0), 0);
4469 #endif
4471 /* If we have a PLUS whose second operand is a constant and the
4472 address is not valid, perhaps will can split it up using
4473 the machine-specific way to split large constants. We use
4474 the first pseudo-reg (one of the virtual regs) as a placeholder;
4475 it will not remain in the result. */
4476 if (GET_CODE (XEXP (x, 0)) == PLUS
4477 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4478 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4479 MEM_ADDR_SPACE (x)))
4481 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4482 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4483 XEXP (x, 0)),
4484 subst_insn);
4486 /* This should have produced two insns, each of which sets our
4487 placeholder. If the source of the second is a valid address,
4488 we can make put both sources together and make a split point
4489 in the middle. */
4491 if (seq
4492 && NEXT_INSN (seq) != NULL_RTX
4493 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4494 && NONJUMP_INSN_P (seq)
4495 && GET_CODE (PATTERN (seq)) == SET
4496 && SET_DEST (PATTERN (seq)) == reg
4497 && ! reg_mentioned_p (reg,
4498 SET_SRC (PATTERN (seq)))
4499 && NONJUMP_INSN_P (NEXT_INSN (seq))
4500 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4501 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4502 && memory_address_addr_space_p
4503 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4504 MEM_ADDR_SPACE (x)))
4506 rtx src1 = SET_SRC (PATTERN (seq));
4507 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4509 /* Replace the placeholder in SRC2 with SRC1. If we can
4510 find where in SRC2 it was placed, that can become our
4511 split point and we can replace this address with SRC2.
4512 Just try two obvious places. */
4514 src2 = replace_rtx (src2, reg, src1);
4515 split = 0;
4516 if (XEXP (src2, 0) == src1)
4517 split = &XEXP (src2, 0);
4518 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4519 && XEXP (XEXP (src2, 0), 0) == src1)
4520 split = &XEXP (XEXP (src2, 0), 0);
4522 if (split)
4524 SUBST (XEXP (x, 0), src2);
4525 return split;
4529 /* If that didn't work, perhaps the first operand is complex and
4530 needs to be computed separately, so make a split point there.
4531 This will occur on machines that just support REG + CONST
4532 and have a constant moved through some previous computation. */
4534 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4535 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4536 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4537 return &XEXP (XEXP (x, 0), 0);
4540 /* If we have a PLUS whose first operand is complex, try computing it
4541 separately by making a split there. */
4542 if (GET_CODE (XEXP (x, 0)) == PLUS
4543 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4544 MEM_ADDR_SPACE (x))
4545 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4546 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4547 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4548 return &XEXP (XEXP (x, 0), 0);
4549 break;
4551 case SET:
4552 #ifdef HAVE_cc0
4553 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4554 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4555 we need to put the operand into a register. So split at that
4556 point. */
4558 if (SET_DEST (x) == cc0_rtx
4559 && GET_CODE (SET_SRC (x)) != COMPARE
4560 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4561 && !OBJECT_P (SET_SRC (x))
4562 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4563 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4564 return &SET_SRC (x);
4565 #endif
4567 /* See if we can split SET_SRC as it stands. */
4568 split = find_split_point (&SET_SRC (x), insn, true);
4569 if (split && split != &SET_SRC (x))
4570 return split;
4572 /* See if we can split SET_DEST as it stands. */
4573 split = find_split_point (&SET_DEST (x), insn, false);
4574 if (split && split != &SET_DEST (x))
4575 return split;
4577 /* See if this is a bitfield assignment with everything constant. If
4578 so, this is an IOR of an AND, so split it into that. */
4579 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4580 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4581 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4582 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4583 && CONST_INT_P (SET_SRC (x))
4584 && ((INTVAL (XEXP (SET_DEST (x), 1))
4585 + INTVAL (XEXP (SET_DEST (x), 2)))
4586 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4587 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4589 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4590 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4591 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4592 rtx dest = XEXP (SET_DEST (x), 0);
4593 enum machine_mode mode = GET_MODE (dest);
4594 unsigned HOST_WIDE_INT mask
4595 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4596 rtx or_mask;
4598 if (BITS_BIG_ENDIAN)
4599 pos = GET_MODE_PRECISION (mode) - len - pos;
4601 or_mask = gen_int_mode (src << pos, mode);
4602 if (src == mask)
4603 SUBST (SET_SRC (x),
4604 simplify_gen_binary (IOR, mode, dest, or_mask));
4605 else
4607 rtx negmask = gen_int_mode (~(mask << pos), mode);
4608 SUBST (SET_SRC (x),
4609 simplify_gen_binary (IOR, mode,
4610 simplify_gen_binary (AND, mode,
4611 dest, negmask),
4612 or_mask));
4615 SUBST (SET_DEST (x), dest);
4617 split = find_split_point (&SET_SRC (x), insn, true);
4618 if (split && split != &SET_SRC (x))
4619 return split;
4622 /* Otherwise, see if this is an operation that we can split into two.
4623 If so, try to split that. */
4624 code = GET_CODE (SET_SRC (x));
4626 switch (code)
4628 case AND:
4629 /* If we are AND'ing with a large constant that is only a single
4630 bit and the result is only being used in a context where we
4631 need to know if it is zero or nonzero, replace it with a bit
4632 extraction. This will avoid the large constant, which might
4633 have taken more than one insn to make. If the constant were
4634 not a valid argument to the AND but took only one insn to make,
4635 this is no worse, but if it took more than one insn, it will
4636 be better. */
4638 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4639 && REG_P (XEXP (SET_SRC (x), 0))
4640 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4641 && REG_P (SET_DEST (x))
4642 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4643 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4644 && XEXP (*split, 0) == SET_DEST (x)
4645 && XEXP (*split, 1) == const0_rtx)
4647 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4648 XEXP (SET_SRC (x), 0),
4649 pos, NULL_RTX, 1, 1, 0, 0);
4650 if (extraction != 0)
4652 SUBST (SET_SRC (x), extraction);
4653 return find_split_point (loc, insn, false);
4656 break;
4658 case NE:
4659 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4660 is known to be on, this can be converted into a NEG of a shift. */
4661 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4662 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4663 && 1 <= (pos = exact_log2
4664 (nonzero_bits (XEXP (SET_SRC (x), 0),
4665 GET_MODE (XEXP (SET_SRC (x), 0))))))
4667 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4669 SUBST (SET_SRC (x),
4670 gen_rtx_NEG (mode,
4671 gen_rtx_LSHIFTRT (mode,
4672 XEXP (SET_SRC (x), 0),
4673 GEN_INT (pos))));
4675 split = find_split_point (&SET_SRC (x), insn, true);
4676 if (split && split != &SET_SRC (x))
4677 return split;
4679 break;
4681 case SIGN_EXTEND:
4682 inner = XEXP (SET_SRC (x), 0);
4684 /* We can't optimize if either mode is a partial integer
4685 mode as we don't know how many bits are significant
4686 in those modes. */
4687 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4688 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4689 break;
4691 pos = 0;
4692 len = GET_MODE_PRECISION (GET_MODE (inner));
4693 unsignedp = 0;
4694 break;
4696 case SIGN_EXTRACT:
4697 case ZERO_EXTRACT:
4698 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4699 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4701 inner = XEXP (SET_SRC (x), 0);
4702 len = INTVAL (XEXP (SET_SRC (x), 1));
4703 pos = INTVAL (XEXP (SET_SRC (x), 2));
4705 if (BITS_BIG_ENDIAN)
4706 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4707 unsignedp = (code == ZERO_EXTRACT);
4709 break;
4711 default:
4712 break;
4715 if (len && pos >= 0
4716 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4718 enum machine_mode mode = GET_MODE (SET_SRC (x));
4720 /* For unsigned, we have a choice of a shift followed by an
4721 AND or two shifts. Use two shifts for field sizes where the
4722 constant might be too large. We assume here that we can
4723 always at least get 8-bit constants in an AND insn, which is
4724 true for every current RISC. */
4726 if (unsignedp && len <= 8)
4728 SUBST (SET_SRC (x),
4729 gen_rtx_AND (mode,
4730 gen_rtx_LSHIFTRT
4731 (mode, gen_lowpart (mode, inner),
4732 GEN_INT (pos)),
4733 GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4734 - 1)));
4736 split = find_split_point (&SET_SRC (x), insn, true);
4737 if (split && split != &SET_SRC (x))
4738 return split;
4740 else
4742 SUBST (SET_SRC (x),
4743 gen_rtx_fmt_ee
4744 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4745 gen_rtx_ASHIFT (mode,
4746 gen_lowpart (mode, inner),
4747 GEN_INT (GET_MODE_PRECISION (mode)
4748 - len - pos)),
4749 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4751 split = find_split_point (&SET_SRC (x), insn, true);
4752 if (split && split != &SET_SRC (x))
4753 return split;
4757 /* See if this is a simple operation with a constant as the second
4758 operand. It might be that this constant is out of range and hence
4759 could be used as a split point. */
4760 if (BINARY_P (SET_SRC (x))
4761 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4762 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4763 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4764 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4765 return &XEXP (SET_SRC (x), 1);
4767 /* Finally, see if this is a simple operation with its first operand
4768 not in a register. The operation might require this operand in a
4769 register, so return it as a split point. We can always do this
4770 because if the first operand were another operation, we would have
4771 already found it as a split point. */
4772 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4773 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4774 return &XEXP (SET_SRC (x), 0);
4776 return 0;
4778 case AND:
4779 case IOR:
4780 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4781 it is better to write this as (not (ior A B)) so we can split it.
4782 Similarly for IOR. */
4783 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4785 SUBST (*loc,
4786 gen_rtx_NOT (GET_MODE (x),
4787 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4788 GET_MODE (x),
4789 XEXP (XEXP (x, 0), 0),
4790 XEXP (XEXP (x, 1), 0))));
4791 return find_split_point (loc, insn, set_src);
4794 /* Many RISC machines have a large set of logical insns. If the
4795 second operand is a NOT, put it first so we will try to split the
4796 other operand first. */
4797 if (GET_CODE (XEXP (x, 1)) == NOT)
4799 rtx tem = XEXP (x, 0);
4800 SUBST (XEXP (x, 0), XEXP (x, 1));
4801 SUBST (XEXP (x, 1), tem);
4803 break;
4805 case PLUS:
4806 case MINUS:
4807 /* Canonicalization can produce (minus A (mult B C)), where C is a
4808 constant. It may be better to try splitting (plus (mult B -C) A)
4809 instead if this isn't a multiply by a power of two. */
4810 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4811 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4812 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4814 enum machine_mode mode = GET_MODE (x);
4815 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4816 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4817 SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4818 XEXP (XEXP (x, 1), 0),
4819 GEN_INT (other_int)),
4820 XEXP (x, 0)));
4821 return find_split_point (loc, insn, set_src);
4824 /* Split at a multiply-accumulate instruction. However if this is
4825 the SET_SRC, we likely do not have such an instruction and it's
4826 worthless to try this split. */
4827 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4828 return loc;
4830 default:
4831 break;
4834 /* Otherwise, select our actions depending on our rtx class. */
4835 switch (GET_RTX_CLASS (code))
4837 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4838 case RTX_TERNARY:
4839 split = find_split_point (&XEXP (x, 2), insn, false);
4840 if (split)
4841 return split;
4842 /* ... fall through ... */
4843 case RTX_BIN_ARITH:
4844 case RTX_COMM_ARITH:
4845 case RTX_COMPARE:
4846 case RTX_COMM_COMPARE:
4847 split = find_split_point (&XEXP (x, 1), insn, false);
4848 if (split)
4849 return split;
4850 /* ... fall through ... */
4851 case RTX_UNARY:
4852 /* Some machines have (and (shift ...) ...) insns. If X is not
4853 an AND, but XEXP (X, 0) is, use it as our split point. */
4854 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4855 return &XEXP (x, 0);
4857 split = find_split_point (&XEXP (x, 0), insn, false);
4858 if (split)
4859 return split;
4860 return loc;
4862 default:
4863 /* Otherwise, we don't have a split point. */
4864 return 0;
4868 /* Throughout X, replace FROM with TO, and return the result.
4869 The result is TO if X is FROM;
4870 otherwise the result is X, but its contents may have been modified.
4871 If they were modified, a record was made in undobuf so that
4872 undo_all will (among other things) return X to its original state.
4874 If the number of changes necessary is too much to record to undo,
4875 the excess changes are not made, so the result is invalid.
4876 The changes already made can still be undone.
4877 undobuf.num_undo is incremented for such changes, so by testing that
4878 the caller can tell whether the result is valid.
4880 `n_occurrences' is incremented each time FROM is replaced.
4882 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4884 IN_COND is nonzero if we are at the top level of a condition.
4886 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4887 by copying if `n_occurrences' is nonzero. */
4889 static rtx
4890 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4892 enum rtx_code code = GET_CODE (x);
4893 enum machine_mode op0_mode = VOIDmode;
4894 const char *fmt;
4895 int len, i;
4896 rtx new_rtx;
4898 /* Two expressions are equal if they are identical copies of a shared
4899 RTX or if they are both registers with the same register number
4900 and mode. */
4902 #define COMBINE_RTX_EQUAL_P(X,Y) \
4903 ((X) == (Y) \
4904 || (REG_P (X) && REG_P (Y) \
4905 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4907 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4909 n_occurrences++;
4910 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4913 /* If X and FROM are the same register but different modes, they
4914 will not have been seen as equal above. However, the log links code
4915 will make a LOG_LINKS entry for that case. If we do nothing, we
4916 will try to rerecognize our original insn and, when it succeeds,
4917 we will delete the feeding insn, which is incorrect.
4919 So force this insn not to match in this (rare) case. */
4920 if (! in_dest && code == REG && REG_P (from)
4921 && reg_overlap_mentioned_p (x, from))
4922 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4924 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4925 of which may contain things that can be combined. */
4926 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4927 return x;
4929 /* It is possible to have a subexpression appear twice in the insn.
4930 Suppose that FROM is a register that appears within TO.
4931 Then, after that subexpression has been scanned once by `subst',
4932 the second time it is scanned, TO may be found. If we were
4933 to scan TO here, we would find FROM within it and create a
4934 self-referent rtl structure which is completely wrong. */
4935 if (COMBINE_RTX_EQUAL_P (x, to))
4936 return to;
4938 /* Parallel asm_operands need special attention because all of the
4939 inputs are shared across the arms. Furthermore, unsharing the
4940 rtl results in recognition failures. Failure to handle this case
4941 specially can result in circular rtl.
4943 Solve this by doing a normal pass across the first entry of the
4944 parallel, and only processing the SET_DESTs of the subsequent
4945 entries. Ug. */
4947 if (code == PARALLEL
4948 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4949 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4951 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
4953 /* If this substitution failed, this whole thing fails. */
4954 if (GET_CODE (new_rtx) == CLOBBER
4955 && XEXP (new_rtx, 0) == const0_rtx)
4956 return new_rtx;
4958 SUBST (XVECEXP (x, 0, 0), new_rtx);
4960 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4962 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4964 if (!REG_P (dest)
4965 && GET_CODE (dest) != CC0
4966 && GET_CODE (dest) != PC)
4968 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
4970 /* If this substitution failed, this whole thing fails. */
4971 if (GET_CODE (new_rtx) == CLOBBER
4972 && XEXP (new_rtx, 0) == const0_rtx)
4973 return new_rtx;
4975 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4979 else
4981 len = GET_RTX_LENGTH (code);
4982 fmt = GET_RTX_FORMAT (code);
4984 /* We don't need to process a SET_DEST that is a register, CC0,
4985 or PC, so set up to skip this common case. All other cases
4986 where we want to suppress replacing something inside a
4987 SET_SRC are handled via the IN_DEST operand. */
4988 if (code == SET
4989 && (REG_P (SET_DEST (x))
4990 || GET_CODE (SET_DEST (x)) == CC0
4991 || GET_CODE (SET_DEST (x)) == PC))
4992 fmt = "ie";
4994 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4995 constant. */
4996 if (fmt[0] == 'e')
4997 op0_mode = GET_MODE (XEXP (x, 0));
4999 for (i = 0; i < len; i++)
5001 if (fmt[i] == 'E')
5003 int j;
5004 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5006 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5008 new_rtx = (unique_copy && n_occurrences
5009 ? copy_rtx (to) : to);
5010 n_occurrences++;
5012 else
5014 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5015 unique_copy);
5017 /* If this substitution failed, this whole thing
5018 fails. */
5019 if (GET_CODE (new_rtx) == CLOBBER
5020 && XEXP (new_rtx, 0) == const0_rtx)
5021 return new_rtx;
5024 SUBST (XVECEXP (x, i, j), new_rtx);
5027 else if (fmt[i] == 'e')
5029 /* If this is a register being set, ignore it. */
5030 new_rtx = XEXP (x, i);
5031 if (in_dest
5032 && i == 0
5033 && (((code == SUBREG || code == ZERO_EXTRACT)
5034 && REG_P (new_rtx))
5035 || code == STRICT_LOW_PART))
5038 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5040 /* In general, don't install a subreg involving two
5041 modes not tieable. It can worsen register
5042 allocation, and can even make invalid reload
5043 insns, since the reg inside may need to be copied
5044 from in the outside mode, and that may be invalid
5045 if it is an fp reg copied in integer mode.
5047 We allow two exceptions to this: It is valid if
5048 it is inside another SUBREG and the mode of that
5049 SUBREG and the mode of the inside of TO is
5050 tieable and it is valid if X is a SET that copies
5051 FROM to CC0. */
5053 if (GET_CODE (to) == SUBREG
5054 && ! MODES_TIEABLE_P (GET_MODE (to),
5055 GET_MODE (SUBREG_REG (to)))
5056 && ! (code == SUBREG
5057 && MODES_TIEABLE_P (GET_MODE (x),
5058 GET_MODE (SUBREG_REG (to))))
5059 #ifdef HAVE_cc0
5060 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5061 #endif
5063 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5065 #ifdef CANNOT_CHANGE_MODE_CLASS
5066 if (code == SUBREG
5067 && REG_P (to)
5068 && REGNO (to) < FIRST_PSEUDO_REGISTER
5069 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5070 GET_MODE (to),
5071 GET_MODE (x)))
5072 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5073 #endif
5075 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5076 n_occurrences++;
5078 else
5079 /* If we are in a SET_DEST, suppress most cases unless we
5080 have gone inside a MEM, in which case we want to
5081 simplify the address. We assume here that things that
5082 are actually part of the destination have their inner
5083 parts in the first expression. This is true for SUBREG,
5084 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5085 things aside from REG and MEM that should appear in a
5086 SET_DEST. */
5087 new_rtx = subst (XEXP (x, i), from, to,
5088 (((in_dest
5089 && (code == SUBREG || code == STRICT_LOW_PART
5090 || code == ZERO_EXTRACT))
5091 || code == SET)
5092 && i == 0),
5093 code == IF_THEN_ELSE && i == 0,
5094 unique_copy);
5096 /* If we found that we will have to reject this combination,
5097 indicate that by returning the CLOBBER ourselves, rather than
5098 an expression containing it. This will speed things up as
5099 well as prevent accidents where two CLOBBERs are considered
5100 to be equal, thus producing an incorrect simplification. */
5102 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5103 return new_rtx;
5105 if (GET_CODE (x) == SUBREG
5106 && (CONST_INT_P (new_rtx) || CONST_DOUBLE_AS_INT_P (new_rtx)))
5108 enum machine_mode mode = GET_MODE (x);
5110 x = simplify_subreg (GET_MODE (x), new_rtx,
5111 GET_MODE (SUBREG_REG (x)),
5112 SUBREG_BYTE (x));
5113 if (! x)
5114 x = gen_rtx_CLOBBER (mode, const0_rtx);
5116 else if (CONST_INT_P (new_rtx)
5117 && GET_CODE (x) == ZERO_EXTEND)
5119 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5120 new_rtx, GET_MODE (XEXP (x, 0)));
5121 gcc_assert (x);
5123 else
5124 SUBST (XEXP (x, i), new_rtx);
5129 /* Check if we are loading something from the constant pool via float
5130 extension; in this case we would undo compress_float_constant
5131 optimization and degenerate constant load to an immediate value. */
5132 if (GET_CODE (x) == FLOAT_EXTEND
5133 && MEM_P (XEXP (x, 0))
5134 && MEM_READONLY_P (XEXP (x, 0)))
5136 rtx tmp = avoid_constant_pool_reference (x);
5137 if (x != tmp)
5138 return x;
5141 /* Try to simplify X. If the simplification changed the code, it is likely
5142 that further simplification will help, so loop, but limit the number
5143 of repetitions that will be performed. */
5145 for (i = 0; i < 4; i++)
5147 /* If X is sufficiently simple, don't bother trying to do anything
5148 with it. */
5149 if (code != CONST_INT && code != REG && code != CLOBBER)
5150 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5152 if (GET_CODE (x) == code)
5153 break;
5155 code = GET_CODE (x);
5157 /* We no longer know the original mode of operand 0 since we
5158 have changed the form of X) */
5159 op0_mode = VOIDmode;
5162 return x;
5165 /* Simplify X, a piece of RTL. We just operate on the expression at the
5166 outer level; call `subst' to simplify recursively. Return the new
5167 expression.
5169 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5170 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5171 of a condition. */
5173 static rtx
5174 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5175 int in_cond)
5177 enum rtx_code code = GET_CODE (x);
5178 enum machine_mode mode = GET_MODE (x);
5179 rtx temp;
5180 int i;
5182 /* If this is a commutative operation, put a constant last and a complex
5183 expression first. We don't need to do this for comparisons here. */
5184 if (COMMUTATIVE_ARITH_P (x)
5185 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5187 temp = XEXP (x, 0);
5188 SUBST (XEXP (x, 0), XEXP (x, 1));
5189 SUBST (XEXP (x, 1), temp);
5192 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5193 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5194 things. Check for cases where both arms are testing the same
5195 condition.
5197 Don't do anything if all operands are very simple. */
5199 if ((BINARY_P (x)
5200 && ((!OBJECT_P (XEXP (x, 0))
5201 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5202 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5203 || (!OBJECT_P (XEXP (x, 1))
5204 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5205 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5206 || (UNARY_P (x)
5207 && (!OBJECT_P (XEXP (x, 0))
5208 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5209 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5211 rtx cond, true_rtx, false_rtx;
5213 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5214 if (cond != 0
5215 /* If everything is a comparison, what we have is highly unlikely
5216 to be simpler, so don't use it. */
5217 && ! (COMPARISON_P (x)
5218 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5220 rtx cop1 = const0_rtx;
5221 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5223 if (cond_code == NE && COMPARISON_P (cond))
5224 return x;
5226 /* Simplify the alternative arms; this may collapse the true and
5227 false arms to store-flag values. Be careful to use copy_rtx
5228 here since true_rtx or false_rtx might share RTL with x as a
5229 result of the if_then_else_cond call above. */
5230 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5231 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5233 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5234 is unlikely to be simpler. */
5235 if (general_operand (true_rtx, VOIDmode)
5236 && general_operand (false_rtx, VOIDmode))
5238 enum rtx_code reversed;
5240 /* Restarting if we generate a store-flag expression will cause
5241 us to loop. Just drop through in this case. */
5243 /* If the result values are STORE_FLAG_VALUE and zero, we can
5244 just make the comparison operation. */
5245 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5246 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5247 cond, cop1);
5248 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5249 && ((reversed = reversed_comparison_code_parts
5250 (cond_code, cond, cop1, NULL))
5251 != UNKNOWN))
5252 x = simplify_gen_relational (reversed, mode, VOIDmode,
5253 cond, cop1);
5255 /* Likewise, we can make the negate of a comparison operation
5256 if the result values are - STORE_FLAG_VALUE and zero. */
5257 else if (CONST_INT_P (true_rtx)
5258 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5259 && false_rtx == const0_rtx)
5260 x = simplify_gen_unary (NEG, mode,
5261 simplify_gen_relational (cond_code,
5262 mode, VOIDmode,
5263 cond, cop1),
5264 mode);
5265 else if (CONST_INT_P (false_rtx)
5266 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5267 && true_rtx == const0_rtx
5268 && ((reversed = reversed_comparison_code_parts
5269 (cond_code, cond, cop1, NULL))
5270 != UNKNOWN))
5271 x = simplify_gen_unary (NEG, mode,
5272 simplify_gen_relational (reversed,
5273 mode, VOIDmode,
5274 cond, cop1),
5275 mode);
5276 else
5277 return gen_rtx_IF_THEN_ELSE (mode,
5278 simplify_gen_relational (cond_code,
5279 mode,
5280 VOIDmode,
5281 cond,
5282 cop1),
5283 true_rtx, false_rtx);
5285 code = GET_CODE (x);
5286 op0_mode = VOIDmode;
5291 /* Try to fold this expression in case we have constants that weren't
5292 present before. */
5293 temp = 0;
5294 switch (GET_RTX_CLASS (code))
5296 case RTX_UNARY:
5297 if (op0_mode == VOIDmode)
5298 op0_mode = GET_MODE (XEXP (x, 0));
5299 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5300 break;
5301 case RTX_COMPARE:
5302 case RTX_COMM_COMPARE:
5304 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5305 if (cmp_mode == VOIDmode)
5307 cmp_mode = GET_MODE (XEXP (x, 1));
5308 if (cmp_mode == VOIDmode)
5309 cmp_mode = op0_mode;
5311 temp = simplify_relational_operation (code, mode, cmp_mode,
5312 XEXP (x, 0), XEXP (x, 1));
5314 break;
5315 case RTX_COMM_ARITH:
5316 case RTX_BIN_ARITH:
5317 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5318 break;
5319 case RTX_BITFIELD_OPS:
5320 case RTX_TERNARY:
5321 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5322 XEXP (x, 1), XEXP (x, 2));
5323 break;
5324 default:
5325 break;
5328 if (temp)
5330 x = temp;
5331 code = GET_CODE (temp);
5332 op0_mode = VOIDmode;
5333 mode = GET_MODE (temp);
5336 /* First see if we can apply the inverse distributive law. */
5337 if (code == PLUS || code == MINUS
5338 || code == AND || code == IOR || code == XOR)
5340 x = apply_distributive_law (x);
5341 code = GET_CODE (x);
5342 op0_mode = VOIDmode;
5345 /* If CODE is an associative operation not otherwise handled, see if we
5346 can associate some operands. This can win if they are constants or
5347 if they are logically related (i.e. (a & b) & a). */
5348 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5349 || code == AND || code == IOR || code == XOR
5350 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5351 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5352 || (flag_associative_math && FLOAT_MODE_P (mode))))
5354 if (GET_CODE (XEXP (x, 0)) == code)
5356 rtx other = XEXP (XEXP (x, 0), 0);
5357 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5358 rtx inner_op1 = XEXP (x, 1);
5359 rtx inner;
5361 /* Make sure we pass the constant operand if any as the second
5362 one if this is a commutative operation. */
5363 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5365 rtx tem = inner_op0;
5366 inner_op0 = inner_op1;
5367 inner_op1 = tem;
5369 inner = simplify_binary_operation (code == MINUS ? PLUS
5370 : code == DIV ? MULT
5371 : code,
5372 mode, inner_op0, inner_op1);
5374 /* For commutative operations, try the other pair if that one
5375 didn't simplify. */
5376 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5378 other = XEXP (XEXP (x, 0), 1);
5379 inner = simplify_binary_operation (code, mode,
5380 XEXP (XEXP (x, 0), 0),
5381 XEXP (x, 1));
5384 if (inner)
5385 return simplify_gen_binary (code, mode, other, inner);
5389 /* A little bit of algebraic simplification here. */
5390 switch (code)
5392 case MEM:
5393 /* Ensure that our address has any ASHIFTs converted to MULT in case
5394 address-recognizing predicates are called later. */
5395 temp = make_compound_operation (XEXP (x, 0), MEM);
5396 SUBST (XEXP (x, 0), temp);
5397 break;
5399 case SUBREG:
5400 if (op0_mode == VOIDmode)
5401 op0_mode = GET_MODE (SUBREG_REG (x));
5403 /* See if this can be moved to simplify_subreg. */
5404 if (CONSTANT_P (SUBREG_REG (x))
5405 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5406 /* Don't call gen_lowpart if the inner mode
5407 is VOIDmode and we cannot simplify it, as SUBREG without
5408 inner mode is invalid. */
5409 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5410 || gen_lowpart_common (mode, SUBREG_REG (x))))
5411 return gen_lowpart (mode, SUBREG_REG (x));
5413 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5414 break;
5416 rtx temp;
5417 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5418 SUBREG_BYTE (x));
5419 if (temp)
5420 return temp;
5423 /* Don't change the mode of the MEM if that would change the meaning
5424 of the address. */
5425 if (MEM_P (SUBREG_REG (x))
5426 && (MEM_VOLATILE_P (SUBREG_REG (x))
5427 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5428 return gen_rtx_CLOBBER (mode, const0_rtx);
5430 /* Note that we cannot do any narrowing for non-constants since
5431 we might have been counting on using the fact that some bits were
5432 zero. We now do this in the SET. */
5434 break;
5436 case NEG:
5437 temp = expand_compound_operation (XEXP (x, 0));
5439 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5440 replaced by (lshiftrt X C). This will convert
5441 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5443 if (GET_CODE (temp) == ASHIFTRT
5444 && CONST_INT_P (XEXP (temp, 1))
5445 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5446 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5447 INTVAL (XEXP (temp, 1)));
5449 /* If X has only a single bit that might be nonzero, say, bit I, convert
5450 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5451 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5452 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5453 or a SUBREG of one since we'd be making the expression more
5454 complex if it was just a register. */
5456 if (!REG_P (temp)
5457 && ! (GET_CODE (temp) == SUBREG
5458 && REG_P (SUBREG_REG (temp)))
5459 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5461 rtx temp1 = simplify_shift_const
5462 (NULL_RTX, ASHIFTRT, mode,
5463 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5464 GET_MODE_PRECISION (mode) - 1 - i),
5465 GET_MODE_PRECISION (mode) - 1 - i);
5467 /* If all we did was surround TEMP with the two shifts, we
5468 haven't improved anything, so don't use it. Otherwise,
5469 we are better off with TEMP1. */
5470 if (GET_CODE (temp1) != ASHIFTRT
5471 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5472 || XEXP (XEXP (temp1, 0), 0) != temp)
5473 return temp1;
5475 break;
5477 case TRUNCATE:
5478 /* We can't handle truncation to a partial integer mode here
5479 because we don't know the real bitsize of the partial
5480 integer mode. */
5481 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5482 break;
5484 if (HWI_COMPUTABLE_MODE_P (mode))
5485 SUBST (XEXP (x, 0),
5486 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5487 GET_MODE_MASK (mode), 0));
5489 /* We can truncate a constant value and return it. */
5490 if (CONST_INT_P (XEXP (x, 0)))
5491 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5493 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5494 whose value is a comparison can be replaced with a subreg if
5495 STORE_FLAG_VALUE permits. */
5496 if (HWI_COMPUTABLE_MODE_P (mode)
5497 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5498 && (temp = get_last_value (XEXP (x, 0)))
5499 && COMPARISON_P (temp))
5500 return gen_lowpart (mode, XEXP (x, 0));
5501 break;
5503 case CONST:
5504 /* (const (const X)) can become (const X). Do it this way rather than
5505 returning the inner CONST since CONST can be shared with a
5506 REG_EQUAL note. */
5507 if (GET_CODE (XEXP (x, 0)) == CONST)
5508 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5509 break;
5511 #ifdef HAVE_lo_sum
5512 case LO_SUM:
5513 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5514 can add in an offset. find_split_point will split this address up
5515 again if it doesn't match. */
5516 if (GET_CODE (XEXP (x, 0)) == HIGH
5517 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5518 return XEXP (x, 1);
5519 break;
5520 #endif
5522 case PLUS:
5523 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5524 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5525 bit-field and can be replaced by either a sign_extend or a
5526 sign_extract. The `and' may be a zero_extend and the two
5527 <c>, -<c> constants may be reversed. */
5528 if (GET_CODE (XEXP (x, 0)) == XOR
5529 && CONST_INT_P (XEXP (x, 1))
5530 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5531 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5532 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5533 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5534 && HWI_COMPUTABLE_MODE_P (mode)
5535 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5536 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5537 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5538 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5539 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5540 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5541 == (unsigned int) i + 1))))
5542 return simplify_shift_const
5543 (NULL_RTX, ASHIFTRT, mode,
5544 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5545 XEXP (XEXP (XEXP (x, 0), 0), 0),
5546 GET_MODE_PRECISION (mode) - (i + 1)),
5547 GET_MODE_PRECISION (mode) - (i + 1));
5549 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5550 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5551 the bitsize of the mode - 1. This allows simplification of
5552 "a = (b & 8) == 0;" */
5553 if (XEXP (x, 1) == constm1_rtx
5554 && !REG_P (XEXP (x, 0))
5555 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5556 && REG_P (SUBREG_REG (XEXP (x, 0))))
5557 && nonzero_bits (XEXP (x, 0), mode) == 1)
5558 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5559 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5560 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5561 GET_MODE_PRECISION (mode) - 1),
5562 GET_MODE_PRECISION (mode) - 1);
5564 /* If we are adding two things that have no bits in common, convert
5565 the addition into an IOR. This will often be further simplified,
5566 for example in cases like ((a & 1) + (a & 2)), which can
5567 become a & 3. */
5569 if (HWI_COMPUTABLE_MODE_P (mode)
5570 && (nonzero_bits (XEXP (x, 0), mode)
5571 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5573 /* Try to simplify the expression further. */
5574 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5575 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5577 /* If we could, great. If not, do not go ahead with the IOR
5578 replacement, since PLUS appears in many special purpose
5579 address arithmetic instructions. */
5580 if (GET_CODE (temp) != CLOBBER
5581 && (GET_CODE (temp) != IOR
5582 || ((XEXP (temp, 0) != XEXP (x, 0)
5583 || XEXP (temp, 1) != XEXP (x, 1))
5584 && (XEXP (temp, 0) != XEXP (x, 1)
5585 || XEXP (temp, 1) != XEXP (x, 0)))))
5586 return temp;
5588 break;
5590 case MINUS:
5591 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5592 (and <foo> (const_int pow2-1)) */
5593 if (GET_CODE (XEXP (x, 1)) == AND
5594 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5595 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5596 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5597 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5598 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5599 break;
5601 case MULT:
5602 /* If we have (mult (plus A B) C), apply the distributive law and then
5603 the inverse distributive law to see if things simplify. This
5604 occurs mostly in addresses, often when unrolling loops. */
5606 if (GET_CODE (XEXP (x, 0)) == PLUS)
5608 rtx result = distribute_and_simplify_rtx (x, 0);
5609 if (result)
5610 return result;
5613 /* Try simplify a*(b/c) as (a*b)/c. */
5614 if (FLOAT_MODE_P (mode) && flag_associative_math
5615 && GET_CODE (XEXP (x, 0)) == DIV)
5617 rtx tem = simplify_binary_operation (MULT, mode,
5618 XEXP (XEXP (x, 0), 0),
5619 XEXP (x, 1));
5620 if (tem)
5621 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5623 break;
5625 case UDIV:
5626 /* If this is a divide by a power of two, treat it as a shift if
5627 its first operand is a shift. */
5628 if (CONST_INT_P (XEXP (x, 1))
5629 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5630 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5631 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5632 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5633 || GET_CODE (XEXP (x, 0)) == ROTATE
5634 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5635 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5636 break;
5638 case EQ: case NE:
5639 case GT: case GTU: case GE: case GEU:
5640 case LT: case LTU: case LE: case LEU:
5641 case UNEQ: case LTGT:
5642 case UNGT: case UNGE:
5643 case UNLT: case UNLE:
5644 case UNORDERED: case ORDERED:
5645 /* If the first operand is a condition code, we can't do anything
5646 with it. */
5647 if (GET_CODE (XEXP (x, 0)) == COMPARE
5648 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5649 && ! CC0_P (XEXP (x, 0))))
5651 rtx op0 = XEXP (x, 0);
5652 rtx op1 = XEXP (x, 1);
5653 enum rtx_code new_code;
5655 if (GET_CODE (op0) == COMPARE)
5656 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5658 /* Simplify our comparison, if possible. */
5659 new_code = simplify_comparison (code, &op0, &op1);
5661 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5662 if only the low-order bit is possibly nonzero in X (such as when
5663 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5664 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5665 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5666 (plus X 1).
5668 Remove any ZERO_EXTRACT we made when thinking this was a
5669 comparison. It may now be simpler to use, e.g., an AND. If a
5670 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5671 the call to make_compound_operation in the SET case.
5673 Don't apply these optimizations if the caller would
5674 prefer a comparison rather than a value.
5675 E.g., for the condition in an IF_THEN_ELSE most targets need
5676 an explicit comparison. */
5678 if (in_cond)
5681 else if (STORE_FLAG_VALUE == 1
5682 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5683 && op1 == const0_rtx
5684 && mode == GET_MODE (op0)
5685 && nonzero_bits (op0, mode) == 1)
5686 return gen_lowpart (mode,
5687 expand_compound_operation (op0));
5689 else if (STORE_FLAG_VALUE == 1
5690 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5691 && op1 == const0_rtx
5692 && mode == GET_MODE (op0)
5693 && (num_sign_bit_copies (op0, mode)
5694 == GET_MODE_PRECISION (mode)))
5696 op0 = expand_compound_operation (op0);
5697 return simplify_gen_unary (NEG, mode,
5698 gen_lowpart (mode, op0),
5699 mode);
5702 else if (STORE_FLAG_VALUE == 1
5703 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5704 && op1 == const0_rtx
5705 && mode == GET_MODE (op0)
5706 && nonzero_bits (op0, mode) == 1)
5708 op0 = expand_compound_operation (op0);
5709 return simplify_gen_binary (XOR, mode,
5710 gen_lowpart (mode, op0),
5711 const1_rtx);
5714 else if (STORE_FLAG_VALUE == 1
5715 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5716 && op1 == const0_rtx
5717 && mode == GET_MODE (op0)
5718 && (num_sign_bit_copies (op0, mode)
5719 == GET_MODE_PRECISION (mode)))
5721 op0 = expand_compound_operation (op0);
5722 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5725 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5726 those above. */
5727 if (in_cond)
5730 else if (STORE_FLAG_VALUE == -1
5731 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5732 && op1 == const0_rtx
5733 && (num_sign_bit_copies (op0, mode)
5734 == GET_MODE_PRECISION (mode)))
5735 return gen_lowpart (mode,
5736 expand_compound_operation (op0));
5738 else if (STORE_FLAG_VALUE == -1
5739 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5740 && op1 == const0_rtx
5741 && mode == GET_MODE (op0)
5742 && nonzero_bits (op0, mode) == 1)
5744 op0 = expand_compound_operation (op0);
5745 return simplify_gen_unary (NEG, mode,
5746 gen_lowpart (mode, op0),
5747 mode);
5750 else if (STORE_FLAG_VALUE == -1
5751 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5752 && op1 == const0_rtx
5753 && mode == GET_MODE (op0)
5754 && (num_sign_bit_copies (op0, mode)
5755 == GET_MODE_PRECISION (mode)))
5757 op0 = expand_compound_operation (op0);
5758 return simplify_gen_unary (NOT, mode,
5759 gen_lowpart (mode, op0),
5760 mode);
5763 /* If X is 0/1, (eq X 0) is X-1. */
5764 else if (STORE_FLAG_VALUE == -1
5765 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5766 && op1 == const0_rtx
5767 && mode == GET_MODE (op0)
5768 && nonzero_bits (op0, mode) == 1)
5770 op0 = expand_compound_operation (op0);
5771 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5774 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5775 one bit that might be nonzero, we can convert (ne x 0) to
5776 (ashift x c) where C puts the bit in the sign bit. Remove any
5777 AND with STORE_FLAG_VALUE when we are done, since we are only
5778 going to test the sign bit. */
5779 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5780 && HWI_COMPUTABLE_MODE_P (mode)
5781 && val_signbit_p (mode, STORE_FLAG_VALUE)
5782 && op1 == const0_rtx
5783 && mode == GET_MODE (op0)
5784 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5786 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5787 expand_compound_operation (op0),
5788 GET_MODE_PRECISION (mode) - 1 - i);
5789 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5790 return XEXP (x, 0);
5791 else
5792 return x;
5795 /* If the code changed, return a whole new comparison. */
5796 if (new_code != code)
5797 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5799 /* Otherwise, keep this operation, but maybe change its operands.
5800 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5801 SUBST (XEXP (x, 0), op0);
5802 SUBST (XEXP (x, 1), op1);
5804 break;
5806 case IF_THEN_ELSE:
5807 return simplify_if_then_else (x);
5809 case ZERO_EXTRACT:
5810 case SIGN_EXTRACT:
5811 case ZERO_EXTEND:
5812 case SIGN_EXTEND:
5813 /* If we are processing SET_DEST, we are done. */
5814 if (in_dest)
5815 return x;
5817 return expand_compound_operation (x);
5819 case SET:
5820 return simplify_set (x);
5822 case AND:
5823 case IOR:
5824 return simplify_logical (x);
5826 case ASHIFT:
5827 case LSHIFTRT:
5828 case ASHIFTRT:
5829 case ROTATE:
5830 case ROTATERT:
5831 /* If this is a shift by a constant amount, simplify it. */
5832 if (CONST_INT_P (XEXP (x, 1)))
5833 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5834 INTVAL (XEXP (x, 1)));
5836 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5837 SUBST (XEXP (x, 1),
5838 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5839 ((unsigned HOST_WIDE_INT) 1
5840 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5841 - 1,
5842 0));
5843 break;
5845 default:
5846 break;
5849 return x;
5852 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5854 static rtx
5855 simplify_if_then_else (rtx x)
5857 enum machine_mode mode = GET_MODE (x);
5858 rtx cond = XEXP (x, 0);
5859 rtx true_rtx = XEXP (x, 1);
5860 rtx false_rtx = XEXP (x, 2);
5861 enum rtx_code true_code = GET_CODE (cond);
5862 int comparison_p = COMPARISON_P (cond);
5863 rtx temp;
5864 int i;
5865 enum rtx_code false_code;
5866 rtx reversed;
5868 /* Simplify storing of the truth value. */
5869 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5870 return simplify_gen_relational (true_code, mode, VOIDmode,
5871 XEXP (cond, 0), XEXP (cond, 1));
5873 /* Also when the truth value has to be reversed. */
5874 if (comparison_p
5875 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5876 && (reversed = reversed_comparison (cond, mode)))
5877 return reversed;
5879 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5880 in it is being compared against certain values. Get the true and false
5881 comparisons and see if that says anything about the value of each arm. */
5883 if (comparison_p
5884 && ((false_code = reversed_comparison_code (cond, NULL))
5885 != UNKNOWN)
5886 && REG_P (XEXP (cond, 0)))
5888 HOST_WIDE_INT nzb;
5889 rtx from = XEXP (cond, 0);
5890 rtx true_val = XEXP (cond, 1);
5891 rtx false_val = true_val;
5892 int swapped = 0;
5894 /* If FALSE_CODE is EQ, swap the codes and arms. */
5896 if (false_code == EQ)
5898 swapped = 1, true_code = EQ, false_code = NE;
5899 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5902 /* If we are comparing against zero and the expression being tested has
5903 only a single bit that might be nonzero, that is its value when it is
5904 not equal to zero. Similarly if it is known to be -1 or 0. */
5906 if (true_code == EQ && true_val == const0_rtx
5907 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5909 false_code = EQ;
5910 false_val = gen_int_mode (nzb, GET_MODE (from));
5912 else if (true_code == EQ && true_val == const0_rtx
5913 && (num_sign_bit_copies (from, GET_MODE (from))
5914 == GET_MODE_PRECISION (GET_MODE (from))))
5916 false_code = EQ;
5917 false_val = constm1_rtx;
5920 /* Now simplify an arm if we know the value of the register in the
5921 branch and it is used in the arm. Be careful due to the potential
5922 of locally-shared RTL. */
5924 if (reg_mentioned_p (from, true_rtx))
5925 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5926 from, true_val),
5927 pc_rtx, pc_rtx, 0, 0, 0);
5928 if (reg_mentioned_p (from, false_rtx))
5929 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5930 from, false_val),
5931 pc_rtx, pc_rtx, 0, 0, 0);
5933 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5934 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5936 true_rtx = XEXP (x, 1);
5937 false_rtx = XEXP (x, 2);
5938 true_code = GET_CODE (cond);
5941 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5942 reversed, do so to avoid needing two sets of patterns for
5943 subtract-and-branch insns. Similarly if we have a constant in the true
5944 arm, the false arm is the same as the first operand of the comparison, or
5945 the false arm is more complicated than the true arm. */
5947 if (comparison_p
5948 && reversed_comparison_code (cond, NULL) != UNKNOWN
5949 && (true_rtx == pc_rtx
5950 || (CONSTANT_P (true_rtx)
5951 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5952 || true_rtx == const0_rtx
5953 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5954 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5955 && !OBJECT_P (false_rtx))
5956 || reg_mentioned_p (true_rtx, false_rtx)
5957 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5959 true_code = reversed_comparison_code (cond, NULL);
5960 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5961 SUBST (XEXP (x, 1), false_rtx);
5962 SUBST (XEXP (x, 2), true_rtx);
5964 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5965 cond = XEXP (x, 0);
5967 /* It is possible that the conditional has been simplified out. */
5968 true_code = GET_CODE (cond);
5969 comparison_p = COMPARISON_P (cond);
5972 /* If the two arms are identical, we don't need the comparison. */
5974 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5975 return true_rtx;
5977 /* Convert a == b ? b : a to "a". */
5978 if (true_code == EQ && ! side_effects_p (cond)
5979 && !HONOR_NANS (mode)
5980 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5981 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5982 return false_rtx;
5983 else if (true_code == NE && ! side_effects_p (cond)
5984 && !HONOR_NANS (mode)
5985 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5986 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5987 return true_rtx;
5989 /* Look for cases where we have (abs x) or (neg (abs X)). */
5991 if (GET_MODE_CLASS (mode) == MODE_INT
5992 && comparison_p
5993 && XEXP (cond, 1) == const0_rtx
5994 && GET_CODE (false_rtx) == NEG
5995 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5996 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5997 && ! side_effects_p (true_rtx))
5998 switch (true_code)
6000 case GT:
6001 case GE:
6002 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6003 case LT:
6004 case LE:
6005 return
6006 simplify_gen_unary (NEG, mode,
6007 simplify_gen_unary (ABS, mode, true_rtx, mode),
6008 mode);
6009 default:
6010 break;
6013 /* Look for MIN or MAX. */
6015 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6016 && comparison_p
6017 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6018 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6019 && ! side_effects_p (cond))
6020 switch (true_code)
6022 case GE:
6023 case GT:
6024 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6025 case LE:
6026 case LT:
6027 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6028 case GEU:
6029 case GTU:
6030 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6031 case LEU:
6032 case LTU:
6033 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6034 default:
6035 break;
6038 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6039 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6040 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6041 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6042 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6043 neither 1 or -1, but it isn't worth checking for. */
6045 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6046 && comparison_p
6047 && GET_MODE_CLASS (mode) == MODE_INT
6048 && ! side_effects_p (x))
6050 rtx t = make_compound_operation (true_rtx, SET);
6051 rtx f = make_compound_operation (false_rtx, SET);
6052 rtx cond_op0 = XEXP (cond, 0);
6053 rtx cond_op1 = XEXP (cond, 1);
6054 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6055 enum machine_mode m = mode;
6056 rtx z = 0, c1 = NULL_RTX;
6058 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6059 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6060 || GET_CODE (t) == ASHIFT
6061 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6062 && rtx_equal_p (XEXP (t, 0), f))
6063 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6065 /* If an identity-zero op is commutative, check whether there
6066 would be a match if we swapped the operands. */
6067 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6068 || GET_CODE (t) == XOR)
6069 && rtx_equal_p (XEXP (t, 1), f))
6070 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6071 else if (GET_CODE (t) == SIGN_EXTEND
6072 && (GET_CODE (XEXP (t, 0)) == PLUS
6073 || GET_CODE (XEXP (t, 0)) == MINUS
6074 || GET_CODE (XEXP (t, 0)) == IOR
6075 || GET_CODE (XEXP (t, 0)) == XOR
6076 || GET_CODE (XEXP (t, 0)) == ASHIFT
6077 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6078 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6079 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6080 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6081 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6082 && (num_sign_bit_copies (f, GET_MODE (f))
6083 > (unsigned int)
6084 (GET_MODE_PRECISION (mode)
6085 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6087 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6088 extend_op = SIGN_EXTEND;
6089 m = GET_MODE (XEXP (t, 0));
6091 else if (GET_CODE (t) == SIGN_EXTEND
6092 && (GET_CODE (XEXP (t, 0)) == PLUS
6093 || GET_CODE (XEXP (t, 0)) == IOR
6094 || GET_CODE (XEXP (t, 0)) == XOR)
6095 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6096 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6097 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6098 && (num_sign_bit_copies (f, GET_MODE (f))
6099 > (unsigned int)
6100 (GET_MODE_PRECISION (mode)
6101 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6103 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6104 extend_op = SIGN_EXTEND;
6105 m = GET_MODE (XEXP (t, 0));
6107 else if (GET_CODE (t) == ZERO_EXTEND
6108 && (GET_CODE (XEXP (t, 0)) == PLUS
6109 || GET_CODE (XEXP (t, 0)) == MINUS
6110 || GET_CODE (XEXP (t, 0)) == IOR
6111 || GET_CODE (XEXP (t, 0)) == XOR
6112 || GET_CODE (XEXP (t, 0)) == ASHIFT
6113 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6114 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6115 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6116 && HWI_COMPUTABLE_MODE_P (mode)
6117 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6118 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6119 && ((nonzero_bits (f, GET_MODE (f))
6120 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6121 == 0))
6123 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6124 extend_op = ZERO_EXTEND;
6125 m = GET_MODE (XEXP (t, 0));
6127 else if (GET_CODE (t) == ZERO_EXTEND
6128 && (GET_CODE (XEXP (t, 0)) == PLUS
6129 || GET_CODE (XEXP (t, 0)) == IOR
6130 || GET_CODE (XEXP (t, 0)) == XOR)
6131 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6132 && HWI_COMPUTABLE_MODE_P (mode)
6133 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6134 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6135 && ((nonzero_bits (f, GET_MODE (f))
6136 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6137 == 0))
6139 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6140 extend_op = ZERO_EXTEND;
6141 m = GET_MODE (XEXP (t, 0));
6144 if (z)
6146 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6147 cond_op0, cond_op1),
6148 pc_rtx, pc_rtx, 0, 0, 0);
6149 temp = simplify_gen_binary (MULT, m, temp,
6150 simplify_gen_binary (MULT, m, c1,
6151 const_true_rtx));
6152 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6153 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6155 if (extend_op != UNKNOWN)
6156 temp = simplify_gen_unary (extend_op, mode, temp, m);
6158 return temp;
6162 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6163 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6164 negation of a single bit, we can convert this operation to a shift. We
6165 can actually do this more generally, but it doesn't seem worth it. */
6167 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6168 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6169 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6170 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6171 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6172 == GET_MODE_PRECISION (mode))
6173 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6174 return
6175 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6176 gen_lowpart (mode, XEXP (cond, 0)), i);
6178 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6179 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6180 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6181 && GET_MODE (XEXP (cond, 0)) == mode
6182 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6183 == nonzero_bits (XEXP (cond, 0), mode)
6184 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6185 return XEXP (cond, 0);
6187 return x;
6190 /* Simplify X, a SET expression. Return the new expression. */
6192 static rtx
6193 simplify_set (rtx x)
6195 rtx src = SET_SRC (x);
6196 rtx dest = SET_DEST (x);
6197 enum machine_mode mode
6198 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6199 rtx other_insn;
6200 rtx *cc_use;
6202 /* (set (pc) (return)) gets written as (return). */
6203 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6204 return src;
6206 /* Now that we know for sure which bits of SRC we are using, see if we can
6207 simplify the expression for the object knowing that we only need the
6208 low-order bits. */
6210 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6212 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6213 SUBST (SET_SRC (x), src);
6216 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6217 the comparison result and try to simplify it unless we already have used
6218 undobuf.other_insn. */
6219 if ((GET_MODE_CLASS (mode) == MODE_CC
6220 || GET_CODE (src) == COMPARE
6221 || CC0_P (dest))
6222 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6223 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6224 && COMPARISON_P (*cc_use)
6225 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6227 enum rtx_code old_code = GET_CODE (*cc_use);
6228 enum rtx_code new_code;
6229 rtx op0, op1, tmp;
6230 int other_changed = 0;
6231 rtx inner_compare = NULL_RTX;
6232 enum machine_mode compare_mode = GET_MODE (dest);
6234 if (GET_CODE (src) == COMPARE)
6236 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6237 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6239 inner_compare = op0;
6240 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6243 else
6244 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6246 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6247 op0, op1);
6248 if (!tmp)
6249 new_code = old_code;
6250 else if (!CONSTANT_P (tmp))
6252 new_code = GET_CODE (tmp);
6253 op0 = XEXP (tmp, 0);
6254 op1 = XEXP (tmp, 1);
6256 else
6258 rtx pat = PATTERN (other_insn);
6259 undobuf.other_insn = other_insn;
6260 SUBST (*cc_use, tmp);
6262 /* Attempt to simplify CC user. */
6263 if (GET_CODE (pat) == SET)
6265 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6266 if (new_rtx != NULL_RTX)
6267 SUBST (SET_SRC (pat), new_rtx);
6270 /* Convert X into a no-op move. */
6271 SUBST (SET_DEST (x), pc_rtx);
6272 SUBST (SET_SRC (x), pc_rtx);
6273 return x;
6276 /* Simplify our comparison, if possible. */
6277 new_code = simplify_comparison (new_code, &op0, &op1);
6279 #ifdef SELECT_CC_MODE
6280 /* If this machine has CC modes other than CCmode, check to see if we
6281 need to use a different CC mode here. */
6282 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6283 compare_mode = GET_MODE (op0);
6284 else if (inner_compare
6285 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6286 && new_code == old_code
6287 && op0 == XEXP (inner_compare, 0)
6288 && op1 == XEXP (inner_compare, 1))
6289 compare_mode = GET_MODE (inner_compare);
6290 else
6291 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6293 #ifndef HAVE_cc0
6294 /* If the mode changed, we have to change SET_DEST, the mode in the
6295 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6296 a hard register, just build new versions with the proper mode. If it
6297 is a pseudo, we lose unless it is only time we set the pseudo, in
6298 which case we can safely change its mode. */
6299 if (compare_mode != GET_MODE (dest))
6301 if (can_change_dest_mode (dest, 0, compare_mode))
6303 unsigned int regno = REGNO (dest);
6304 rtx new_dest;
6306 if (regno < FIRST_PSEUDO_REGISTER)
6307 new_dest = gen_rtx_REG (compare_mode, regno);
6308 else
6310 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6311 new_dest = regno_reg_rtx[regno];
6314 SUBST (SET_DEST (x), new_dest);
6315 SUBST (XEXP (*cc_use, 0), new_dest);
6316 other_changed = 1;
6318 dest = new_dest;
6321 #endif /* cc0 */
6322 #endif /* SELECT_CC_MODE */
6324 /* If the code changed, we have to build a new comparison in
6325 undobuf.other_insn. */
6326 if (new_code != old_code)
6328 int other_changed_previously = other_changed;
6329 unsigned HOST_WIDE_INT mask;
6330 rtx old_cc_use = *cc_use;
6332 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6333 dest, const0_rtx));
6334 other_changed = 1;
6336 /* If the only change we made was to change an EQ into an NE or
6337 vice versa, OP0 has only one bit that might be nonzero, and OP1
6338 is zero, check if changing the user of the condition code will
6339 produce a valid insn. If it won't, we can keep the original code
6340 in that insn by surrounding our operation with an XOR. */
6342 if (((old_code == NE && new_code == EQ)
6343 || (old_code == EQ && new_code == NE))
6344 && ! other_changed_previously && op1 == const0_rtx
6345 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6346 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6348 rtx pat = PATTERN (other_insn), note = 0;
6350 if ((recog_for_combine (&pat, other_insn, &note) < 0
6351 && ! check_asm_operands (pat)))
6353 *cc_use = old_cc_use;
6354 other_changed = 0;
6356 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6357 op0, GEN_INT (mask));
6362 if (other_changed)
6363 undobuf.other_insn = other_insn;
6365 /* Otherwise, if we didn't previously have a COMPARE in the
6366 correct mode, we need one. */
6367 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6369 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6370 src = SET_SRC (x);
6372 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6374 SUBST (SET_SRC (x), op0);
6375 src = SET_SRC (x);
6377 /* Otherwise, update the COMPARE if needed. */
6378 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6380 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6381 src = SET_SRC (x);
6384 else
6386 /* Get SET_SRC in a form where we have placed back any
6387 compound expressions. Then do the checks below. */
6388 src = make_compound_operation (src, SET);
6389 SUBST (SET_SRC (x), src);
6392 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6393 and X being a REG or (subreg (reg)), we may be able to convert this to
6394 (set (subreg:m2 x) (op)).
6396 We can always do this if M1 is narrower than M2 because that means that
6397 we only care about the low bits of the result.
6399 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6400 perform a narrower operation than requested since the high-order bits will
6401 be undefined. On machine where it is defined, this transformation is safe
6402 as long as M1 and M2 have the same number of words. */
6404 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6405 && !OBJECT_P (SUBREG_REG (src))
6406 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6407 / UNITS_PER_WORD)
6408 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6409 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6410 #ifndef WORD_REGISTER_OPERATIONS
6411 && (GET_MODE_SIZE (GET_MODE (src))
6412 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6413 #endif
6414 #ifdef CANNOT_CHANGE_MODE_CLASS
6415 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6416 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6417 GET_MODE (SUBREG_REG (src)),
6418 GET_MODE (src)))
6419 #endif
6420 && (REG_P (dest)
6421 || (GET_CODE (dest) == SUBREG
6422 && REG_P (SUBREG_REG (dest)))))
6424 SUBST (SET_DEST (x),
6425 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6426 dest));
6427 SUBST (SET_SRC (x), SUBREG_REG (src));
6429 src = SET_SRC (x), dest = SET_DEST (x);
6432 #ifdef HAVE_cc0
6433 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6434 in SRC. */
6435 if (dest == cc0_rtx
6436 && GET_CODE (src) == SUBREG
6437 && subreg_lowpart_p (src)
6438 && (GET_MODE_PRECISION (GET_MODE (src))
6439 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6441 rtx inner = SUBREG_REG (src);
6442 enum machine_mode inner_mode = GET_MODE (inner);
6444 /* Here we make sure that we don't have a sign bit on. */
6445 if (val_signbit_known_clear_p (GET_MODE (src),
6446 nonzero_bits (inner, inner_mode)))
6448 SUBST (SET_SRC (x), inner);
6449 src = SET_SRC (x);
6452 #endif
6454 #ifdef LOAD_EXTEND_OP
6455 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6456 would require a paradoxical subreg. Replace the subreg with a
6457 zero_extend to avoid the reload that would otherwise be required. */
6459 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6460 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6461 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6462 && SUBREG_BYTE (src) == 0
6463 && paradoxical_subreg_p (src)
6464 && MEM_P (SUBREG_REG (src)))
6466 SUBST (SET_SRC (x),
6467 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6468 GET_MODE (src), SUBREG_REG (src)));
6470 src = SET_SRC (x);
6472 #endif
6474 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6475 are comparing an item known to be 0 or -1 against 0, use a logical
6476 operation instead. Check for one of the arms being an IOR of the other
6477 arm with some value. We compute three terms to be IOR'ed together. In
6478 practice, at most two will be nonzero. Then we do the IOR's. */
6480 if (GET_CODE (dest) != PC
6481 && GET_CODE (src) == IF_THEN_ELSE
6482 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6483 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6484 && XEXP (XEXP (src, 0), 1) == const0_rtx
6485 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6486 #ifdef HAVE_conditional_move
6487 && ! can_conditionally_move_p (GET_MODE (src))
6488 #endif
6489 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6490 GET_MODE (XEXP (XEXP (src, 0), 0)))
6491 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6492 && ! side_effects_p (src))
6494 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6495 ? XEXP (src, 1) : XEXP (src, 2));
6496 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6497 ? XEXP (src, 2) : XEXP (src, 1));
6498 rtx term1 = const0_rtx, term2, term3;
6500 if (GET_CODE (true_rtx) == IOR
6501 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6502 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6503 else if (GET_CODE (true_rtx) == IOR
6504 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6505 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6506 else if (GET_CODE (false_rtx) == IOR
6507 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6508 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6509 else if (GET_CODE (false_rtx) == IOR
6510 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6511 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6513 term2 = simplify_gen_binary (AND, GET_MODE (src),
6514 XEXP (XEXP (src, 0), 0), true_rtx);
6515 term3 = simplify_gen_binary (AND, GET_MODE (src),
6516 simplify_gen_unary (NOT, GET_MODE (src),
6517 XEXP (XEXP (src, 0), 0),
6518 GET_MODE (src)),
6519 false_rtx);
6521 SUBST (SET_SRC (x),
6522 simplify_gen_binary (IOR, GET_MODE (src),
6523 simplify_gen_binary (IOR, GET_MODE (src),
6524 term1, term2),
6525 term3));
6527 src = SET_SRC (x);
6530 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6531 whole thing fail. */
6532 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6533 return src;
6534 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6535 return dest;
6536 else
6537 /* Convert this into a field assignment operation, if possible. */
6538 return make_field_assignment (x);
6541 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6542 result. */
6544 static rtx
6545 simplify_logical (rtx x)
6547 enum machine_mode mode = GET_MODE (x);
6548 rtx op0 = XEXP (x, 0);
6549 rtx op1 = XEXP (x, 1);
6551 switch (GET_CODE (x))
6553 case AND:
6554 /* We can call simplify_and_const_int only if we don't lose
6555 any (sign) bits when converting INTVAL (op1) to
6556 "unsigned HOST_WIDE_INT". */
6557 if (CONST_INT_P (op1)
6558 && (HWI_COMPUTABLE_MODE_P (mode)
6559 || INTVAL (op1) > 0))
6561 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6562 if (GET_CODE (x) != AND)
6563 return x;
6565 op0 = XEXP (x, 0);
6566 op1 = XEXP (x, 1);
6569 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6570 apply the distributive law and then the inverse distributive
6571 law to see if things simplify. */
6572 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6574 rtx result = distribute_and_simplify_rtx (x, 0);
6575 if (result)
6576 return result;
6578 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6580 rtx result = distribute_and_simplify_rtx (x, 1);
6581 if (result)
6582 return result;
6584 break;
6586 case IOR:
6587 /* If we have (ior (and A B) C), apply the distributive law and then
6588 the inverse distributive law to see if things simplify. */
6590 if (GET_CODE (op0) == AND)
6592 rtx result = distribute_and_simplify_rtx (x, 0);
6593 if (result)
6594 return result;
6597 if (GET_CODE (op1) == AND)
6599 rtx result = distribute_and_simplify_rtx (x, 1);
6600 if (result)
6601 return result;
6603 break;
6605 default:
6606 gcc_unreachable ();
6609 return x;
6612 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6613 operations" because they can be replaced with two more basic operations.
6614 ZERO_EXTEND is also considered "compound" because it can be replaced with
6615 an AND operation, which is simpler, though only one operation.
6617 The function expand_compound_operation is called with an rtx expression
6618 and will convert it to the appropriate shifts and AND operations,
6619 simplifying at each stage.
6621 The function make_compound_operation is called to convert an expression
6622 consisting of shifts and ANDs into the equivalent compound expression.
6623 It is the inverse of this function, loosely speaking. */
6625 static rtx
6626 expand_compound_operation (rtx x)
6628 unsigned HOST_WIDE_INT pos = 0, len;
6629 int unsignedp = 0;
6630 unsigned int modewidth;
6631 rtx tem;
6633 switch (GET_CODE (x))
6635 case ZERO_EXTEND:
6636 unsignedp = 1;
6637 case SIGN_EXTEND:
6638 /* We can't necessarily use a const_int for a multiword mode;
6639 it depends on implicitly extending the value.
6640 Since we don't know the right way to extend it,
6641 we can't tell whether the implicit way is right.
6643 Even for a mode that is no wider than a const_int,
6644 we can't win, because we need to sign extend one of its bits through
6645 the rest of it, and we don't know which bit. */
6646 if (CONST_INT_P (XEXP (x, 0)))
6647 return x;
6649 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6650 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6651 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6652 reloaded. If not for that, MEM's would very rarely be safe.
6654 Reject MODEs bigger than a word, because we might not be able
6655 to reference a two-register group starting with an arbitrary register
6656 (and currently gen_lowpart might crash for a SUBREG). */
6658 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6659 return x;
6661 /* Reject MODEs that aren't scalar integers because turning vector
6662 or complex modes into shifts causes problems. */
6664 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6665 return x;
6667 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6668 /* If the inner object has VOIDmode (the only way this can happen
6669 is if it is an ASM_OPERANDS), we can't do anything since we don't
6670 know how much masking to do. */
6671 if (len == 0)
6672 return x;
6674 break;
6676 case ZERO_EXTRACT:
6677 unsignedp = 1;
6679 /* ... fall through ... */
6681 case SIGN_EXTRACT:
6682 /* If the operand is a CLOBBER, just return it. */
6683 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6684 return XEXP (x, 0);
6686 if (!CONST_INT_P (XEXP (x, 1))
6687 || !CONST_INT_P (XEXP (x, 2))
6688 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6689 return x;
6691 /* Reject MODEs that aren't scalar integers because turning vector
6692 or complex modes into shifts causes problems. */
6694 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6695 return x;
6697 len = INTVAL (XEXP (x, 1));
6698 pos = INTVAL (XEXP (x, 2));
6700 /* This should stay within the object being extracted, fail otherwise. */
6701 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6702 return x;
6704 if (BITS_BIG_ENDIAN)
6705 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6707 break;
6709 default:
6710 return x;
6712 /* Convert sign extension to zero extension, if we know that the high
6713 bit is not set, as this is easier to optimize. It will be converted
6714 back to cheaper alternative in make_extraction. */
6715 if (GET_CODE (x) == SIGN_EXTEND
6716 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6717 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6718 & ~(((unsigned HOST_WIDE_INT)
6719 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6720 >> 1))
6721 == 0)))
6723 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6724 rtx temp2 = expand_compound_operation (temp);
6726 /* Make sure this is a profitable operation. */
6727 if (set_src_cost (x, optimize_this_for_speed_p)
6728 > set_src_cost (temp2, optimize_this_for_speed_p))
6729 return temp2;
6730 else if (set_src_cost (x, optimize_this_for_speed_p)
6731 > set_src_cost (temp, optimize_this_for_speed_p))
6732 return temp;
6733 else
6734 return x;
6737 /* We can optimize some special cases of ZERO_EXTEND. */
6738 if (GET_CODE (x) == ZERO_EXTEND)
6740 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6741 know that the last value didn't have any inappropriate bits
6742 set. */
6743 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6744 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6745 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6746 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6747 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6748 return XEXP (XEXP (x, 0), 0);
6750 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6751 if (GET_CODE (XEXP (x, 0)) == SUBREG
6752 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6753 && subreg_lowpart_p (XEXP (x, 0))
6754 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6755 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6756 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6757 return SUBREG_REG (XEXP (x, 0));
6759 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6760 is a comparison and STORE_FLAG_VALUE permits. This is like
6761 the first case, but it works even when GET_MODE (x) is larger
6762 than HOST_WIDE_INT. */
6763 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6764 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6765 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6766 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6767 <= HOST_BITS_PER_WIDE_INT)
6768 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6769 return XEXP (XEXP (x, 0), 0);
6771 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6772 if (GET_CODE (XEXP (x, 0)) == SUBREG
6773 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6774 && subreg_lowpart_p (XEXP (x, 0))
6775 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6776 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6777 <= HOST_BITS_PER_WIDE_INT)
6778 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6779 return SUBREG_REG (XEXP (x, 0));
6783 /* If we reach here, we want to return a pair of shifts. The inner
6784 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6785 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6786 logical depending on the value of UNSIGNEDP.
6788 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6789 converted into an AND of a shift.
6791 We must check for the case where the left shift would have a negative
6792 count. This can happen in a case like (x >> 31) & 255 on machines
6793 that can't shift by a constant. On those machines, we would first
6794 combine the shift with the AND to produce a variable-position
6795 extraction. Then the constant of 31 would be substituted in
6796 to produce such a position. */
6798 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6799 if (modewidth >= pos + len)
6801 enum machine_mode mode = GET_MODE (x);
6802 tem = gen_lowpart (mode, XEXP (x, 0));
6803 if (!tem || GET_CODE (tem) == CLOBBER)
6804 return x;
6805 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6806 tem, modewidth - pos - len);
6807 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6808 mode, tem, modewidth - len);
6810 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6811 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6812 simplify_shift_const (NULL_RTX, LSHIFTRT,
6813 GET_MODE (x),
6814 XEXP (x, 0), pos),
6815 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6816 else
6817 /* Any other cases we can't handle. */
6818 return x;
6820 /* If we couldn't do this for some reason, return the original
6821 expression. */
6822 if (GET_CODE (tem) == CLOBBER)
6823 return x;
6825 return tem;
6828 /* X is a SET which contains an assignment of one object into
6829 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6830 or certain SUBREGS). If possible, convert it into a series of
6831 logical operations.
6833 We half-heartedly support variable positions, but do not at all
6834 support variable lengths. */
6836 static const_rtx
6837 expand_field_assignment (const_rtx x)
6839 rtx inner;
6840 rtx pos; /* Always counts from low bit. */
6841 int len;
6842 rtx mask, cleared, masked;
6843 enum machine_mode compute_mode;
6845 /* Loop until we find something we can't simplify. */
6846 while (1)
6848 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6849 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6851 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6852 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
6853 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6855 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6856 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6858 inner = XEXP (SET_DEST (x), 0);
6859 len = INTVAL (XEXP (SET_DEST (x), 1));
6860 pos = XEXP (SET_DEST (x), 2);
6862 /* A constant position should stay within the width of INNER. */
6863 if (CONST_INT_P (pos)
6864 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
6865 break;
6867 if (BITS_BIG_ENDIAN)
6869 if (CONST_INT_P (pos))
6870 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
6871 - INTVAL (pos));
6872 else if (GET_CODE (pos) == MINUS
6873 && CONST_INT_P (XEXP (pos, 1))
6874 && (INTVAL (XEXP (pos, 1))
6875 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
6876 /* If position is ADJUST - X, new position is X. */
6877 pos = XEXP (pos, 0);
6878 else
6879 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6880 GEN_INT (GET_MODE_PRECISION (
6881 GET_MODE (inner))
6882 - len),
6883 pos);
6887 /* A SUBREG between two modes that occupy the same numbers of words
6888 can be done by moving the SUBREG to the source. */
6889 else if (GET_CODE (SET_DEST (x)) == SUBREG
6890 /* We need SUBREGs to compute nonzero_bits properly. */
6891 && nonzero_sign_valid
6892 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6893 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6894 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6895 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6897 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6898 gen_lowpart
6899 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6900 SET_SRC (x)));
6901 continue;
6903 else
6904 break;
6906 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6907 inner = SUBREG_REG (inner);
6909 compute_mode = GET_MODE (inner);
6911 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6912 if (! SCALAR_INT_MODE_P (compute_mode))
6914 enum machine_mode imode;
6916 /* Don't do anything for vector or complex integral types. */
6917 if (! FLOAT_MODE_P (compute_mode))
6918 break;
6920 /* Try to find an integral mode to pun with. */
6921 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6922 if (imode == BLKmode)
6923 break;
6925 compute_mode = imode;
6926 inner = gen_lowpart (imode, inner);
6929 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6930 if (len >= HOST_BITS_PER_WIDE_INT)
6931 break;
6933 /* Now compute the equivalent expression. Make a copy of INNER
6934 for the SET_DEST in case it is a MEM into which we will substitute;
6935 we don't want shared RTL in that case. */
6936 mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
6937 cleared = simplify_gen_binary (AND, compute_mode,
6938 simplify_gen_unary (NOT, compute_mode,
6939 simplify_gen_binary (ASHIFT,
6940 compute_mode,
6941 mask, pos),
6942 compute_mode),
6943 inner);
6944 masked = simplify_gen_binary (ASHIFT, compute_mode,
6945 simplify_gen_binary (
6946 AND, compute_mode,
6947 gen_lowpart (compute_mode, SET_SRC (x)),
6948 mask),
6949 pos);
6951 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6952 simplify_gen_binary (IOR, compute_mode,
6953 cleared, masked));
6956 return x;
6959 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6960 it is an RTX that represents a variable starting position; otherwise,
6961 POS is the (constant) starting bit position (counted from the LSB).
6963 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6964 signed reference.
6966 IN_DEST is nonzero if this is a reference in the destination of a
6967 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6968 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6969 be used.
6971 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6972 ZERO_EXTRACT should be built even for bits starting at bit 0.
6974 MODE is the desired mode of the result (if IN_DEST == 0).
6976 The result is an RTX for the extraction or NULL_RTX if the target
6977 can't handle it. */
6979 static rtx
6980 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6981 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6982 int in_dest, int in_compare)
6984 /* This mode describes the size of the storage area
6985 to fetch the overall value from. Within that, we
6986 ignore the POS lowest bits, etc. */
6987 enum machine_mode is_mode = GET_MODE (inner);
6988 enum machine_mode inner_mode;
6989 enum machine_mode wanted_inner_mode;
6990 enum machine_mode wanted_inner_reg_mode = word_mode;
6991 enum machine_mode pos_mode = word_mode;
6992 enum machine_mode extraction_mode = word_mode;
6993 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6994 rtx new_rtx = 0;
6995 rtx orig_pos_rtx = pos_rtx;
6996 HOST_WIDE_INT orig_pos;
6998 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7000 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7001 consider just the QI as the memory to extract from.
7002 The subreg adds or removes high bits; its mode is
7003 irrelevant to the meaning of this extraction,
7004 since POS and LEN count from the lsb. */
7005 if (MEM_P (SUBREG_REG (inner)))
7006 is_mode = GET_MODE (SUBREG_REG (inner));
7007 inner = SUBREG_REG (inner);
7009 else if (GET_CODE (inner) == ASHIFT
7010 && CONST_INT_P (XEXP (inner, 1))
7011 && pos_rtx == 0 && pos == 0
7012 && len > UINTVAL (XEXP (inner, 1)))
7014 /* We're extracting the least significant bits of an rtx
7015 (ashift X (const_int C)), where LEN > C. Extract the
7016 least significant (LEN - C) bits of X, giving an rtx
7017 whose mode is MODE, then shift it left C times. */
7018 new_rtx = make_extraction (mode, XEXP (inner, 0),
7019 0, 0, len - INTVAL (XEXP (inner, 1)),
7020 unsignedp, in_dest, in_compare);
7021 if (new_rtx != 0)
7022 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7025 inner_mode = GET_MODE (inner);
7027 if (pos_rtx && CONST_INT_P (pos_rtx))
7028 pos = INTVAL (pos_rtx), pos_rtx = 0;
7030 /* See if this can be done without an extraction. We never can if the
7031 width of the field is not the same as that of some integer mode. For
7032 registers, we can only avoid the extraction if the position is at the
7033 low-order bit and this is either not in the destination or we have the
7034 appropriate STRICT_LOW_PART operation available.
7036 For MEM, we can avoid an extract if the field starts on an appropriate
7037 boundary and we can change the mode of the memory reference. */
7039 if (tmode != BLKmode
7040 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7041 && !MEM_P (inner)
7042 && (inner_mode == tmode
7043 || !REG_P (inner)
7044 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7045 || reg_truncated_to_mode (tmode, inner))
7046 && (! in_dest
7047 || (REG_P (inner)
7048 && have_insn_for (STRICT_LOW_PART, tmode))))
7049 || (MEM_P (inner) && pos_rtx == 0
7050 && (pos
7051 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7052 : BITS_PER_UNIT)) == 0
7053 /* We can't do this if we are widening INNER_MODE (it
7054 may not be aligned, for one thing). */
7055 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7056 && (inner_mode == tmode
7057 || (! mode_dependent_address_p (XEXP (inner, 0))
7058 && ! MEM_VOLATILE_P (inner))))))
7060 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7061 field. If the original and current mode are the same, we need not
7062 adjust the offset. Otherwise, we do if bytes big endian.
7064 If INNER is not a MEM, get a piece consisting of just the field
7065 of interest (in this case POS % BITS_PER_WORD must be 0). */
7067 if (MEM_P (inner))
7069 HOST_WIDE_INT offset;
7071 /* POS counts from lsb, but make OFFSET count in memory order. */
7072 if (BYTES_BIG_ENDIAN)
7073 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7074 else
7075 offset = pos / BITS_PER_UNIT;
7077 new_rtx = adjust_address_nv (inner, tmode, offset);
7079 else if (REG_P (inner))
7081 if (tmode != inner_mode)
7083 /* We can't call gen_lowpart in a DEST since we
7084 always want a SUBREG (see below) and it would sometimes
7085 return a new hard register. */
7086 if (pos || in_dest)
7088 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7090 if (WORDS_BIG_ENDIAN
7091 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7092 final_word = ((GET_MODE_SIZE (inner_mode)
7093 - GET_MODE_SIZE (tmode))
7094 / UNITS_PER_WORD) - final_word;
7096 final_word *= UNITS_PER_WORD;
7097 if (BYTES_BIG_ENDIAN &&
7098 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7099 final_word += (GET_MODE_SIZE (inner_mode)
7100 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7102 /* Avoid creating invalid subregs, for example when
7103 simplifying (x>>32)&255. */
7104 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7105 return NULL_RTX;
7107 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7109 else
7110 new_rtx = gen_lowpart (tmode, inner);
7112 else
7113 new_rtx = inner;
7115 else
7116 new_rtx = force_to_mode (inner, tmode,
7117 len >= HOST_BITS_PER_WIDE_INT
7118 ? ~(unsigned HOST_WIDE_INT) 0
7119 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7122 /* If this extraction is going into the destination of a SET,
7123 make a STRICT_LOW_PART unless we made a MEM. */
7125 if (in_dest)
7126 return (MEM_P (new_rtx) ? new_rtx
7127 : (GET_CODE (new_rtx) != SUBREG
7128 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7129 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7131 if (mode == tmode)
7132 return new_rtx;
7134 if (CONST_INT_P (new_rtx) || CONST_DOUBLE_AS_INT_P (new_rtx))
7135 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7136 mode, new_rtx, tmode);
7138 /* If we know that no extraneous bits are set, and that the high
7139 bit is not set, convert the extraction to the cheaper of
7140 sign and zero extension, that are equivalent in these cases. */
7141 if (flag_expensive_optimizations
7142 && (HWI_COMPUTABLE_MODE_P (tmode)
7143 && ((nonzero_bits (new_rtx, tmode)
7144 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7145 == 0)))
7147 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7148 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7150 /* Prefer ZERO_EXTENSION, since it gives more information to
7151 backends. */
7152 if (set_src_cost (temp, optimize_this_for_speed_p)
7153 <= set_src_cost (temp1, optimize_this_for_speed_p))
7154 return temp;
7155 return temp1;
7158 /* Otherwise, sign- or zero-extend unless we already are in the
7159 proper mode. */
7161 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7162 mode, new_rtx));
7165 /* Unless this is a COMPARE or we have a funny memory reference,
7166 don't do anything with zero-extending field extracts starting at
7167 the low-order bit since they are simple AND operations. */
7168 if (pos_rtx == 0 && pos == 0 && ! in_dest
7169 && ! in_compare && unsignedp)
7170 return 0;
7172 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7173 if the position is not a constant and the length is not 1. In all
7174 other cases, we would only be going outside our object in cases when
7175 an original shift would have been undefined. */
7176 if (MEM_P (inner)
7177 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7178 || (pos_rtx != 0 && len != 1)))
7179 return 0;
7181 /* Get the mode to use should INNER not be a MEM, the mode for the position,
7182 and the mode for the result. */
7183 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7185 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7186 pos_mode = mode_for_extraction (EP_insv, 2);
7187 extraction_mode = mode_for_extraction (EP_insv, 3);
7190 if (! in_dest && unsignedp
7191 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7193 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7194 pos_mode = mode_for_extraction (EP_extzv, 3);
7195 extraction_mode = mode_for_extraction (EP_extzv, 0);
7198 if (! in_dest && ! unsignedp
7199 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7201 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7202 pos_mode = mode_for_extraction (EP_extv, 3);
7203 extraction_mode = mode_for_extraction (EP_extv, 0);
7206 /* Never narrow an object, since that might not be safe. */
7208 if (mode != VOIDmode
7209 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7210 extraction_mode = mode;
7212 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7213 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7214 pos_mode = GET_MODE (pos_rtx);
7216 /* If this is not from memory, the desired mode is the preferred mode
7217 for an extraction pattern's first input operand, or word_mode if there
7218 is none. */
7219 if (!MEM_P (inner))
7220 wanted_inner_mode = wanted_inner_reg_mode;
7221 else
7223 /* Be careful not to go beyond the extracted object and maintain the
7224 natural alignment of the memory. */
7225 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7226 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7227 > GET_MODE_BITSIZE (wanted_inner_mode))
7229 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7230 gcc_assert (wanted_inner_mode != VOIDmode);
7233 /* If we have to change the mode of memory and cannot, the desired mode
7234 is EXTRACTION_MODE. */
7235 if (inner_mode != wanted_inner_mode
7236 && (mode_dependent_address_p (XEXP (inner, 0))
7237 || MEM_VOLATILE_P (inner)
7238 || pos_rtx))
7239 wanted_inner_mode = extraction_mode;
7242 orig_pos = pos;
7244 if (BITS_BIG_ENDIAN)
7246 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7247 BITS_BIG_ENDIAN style. If position is constant, compute new
7248 position. Otherwise, build subtraction.
7249 Note that POS is relative to the mode of the original argument.
7250 If it's a MEM we need to recompute POS relative to that.
7251 However, if we're extracting from (or inserting into) a register,
7252 we want to recompute POS relative to wanted_inner_mode. */
7253 int width = (MEM_P (inner)
7254 ? GET_MODE_BITSIZE (is_mode)
7255 : GET_MODE_BITSIZE (wanted_inner_mode));
7257 if (pos_rtx == 0)
7258 pos = width - len - pos;
7259 else
7260 pos_rtx
7261 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7262 /* POS may be less than 0 now, but we check for that below.
7263 Note that it can only be less than 0 if !MEM_P (inner). */
7266 /* If INNER has a wider mode, and this is a constant extraction, try to
7267 make it smaller and adjust the byte to point to the byte containing
7268 the value. */
7269 if (wanted_inner_mode != VOIDmode
7270 && inner_mode != wanted_inner_mode
7271 && ! pos_rtx
7272 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7273 && MEM_P (inner)
7274 && ! mode_dependent_address_p (XEXP (inner, 0))
7275 && ! MEM_VOLATILE_P (inner))
7277 int offset = 0;
7279 /* The computations below will be correct if the machine is big
7280 endian in both bits and bytes or little endian in bits and bytes.
7281 If it is mixed, we must adjust. */
7283 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7284 adjust OFFSET to compensate. */
7285 if (BYTES_BIG_ENDIAN
7286 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7287 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7289 /* We can now move to the desired byte. */
7290 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7291 * GET_MODE_SIZE (wanted_inner_mode);
7292 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7294 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7295 && is_mode != wanted_inner_mode)
7296 offset = (GET_MODE_SIZE (is_mode)
7297 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7299 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7302 /* If INNER is not memory, get it into the proper mode. If we are changing
7303 its mode, POS must be a constant and smaller than the size of the new
7304 mode. */
7305 else if (!MEM_P (inner))
7307 /* On the LHS, don't create paradoxical subregs implicitely truncating
7308 the register unless TRULY_NOOP_TRUNCATION. */
7309 if (in_dest
7310 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7311 wanted_inner_mode))
7312 return NULL_RTX;
7314 if (GET_MODE (inner) != wanted_inner_mode
7315 && (pos_rtx != 0
7316 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7317 return NULL_RTX;
7319 if (orig_pos < 0)
7320 return NULL_RTX;
7322 inner = force_to_mode (inner, wanted_inner_mode,
7323 pos_rtx
7324 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7325 ? ~(unsigned HOST_WIDE_INT) 0
7326 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7327 << orig_pos),
7331 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7332 have to zero extend. Otherwise, we can just use a SUBREG. */
7333 if (pos_rtx != 0
7334 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7336 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7338 /* If we know that no extraneous bits are set, and that the high
7339 bit is not set, convert extraction to cheaper one - either
7340 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7341 cases. */
7342 if (flag_expensive_optimizations
7343 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7344 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7345 & ~(((unsigned HOST_WIDE_INT)
7346 GET_MODE_MASK (GET_MODE (pos_rtx)))
7347 >> 1))
7348 == 0)))
7350 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7352 /* Prefer ZERO_EXTENSION, since it gives more information to
7353 backends. */
7354 if (set_src_cost (temp1, optimize_this_for_speed_p)
7355 < set_src_cost (temp, optimize_this_for_speed_p))
7356 temp = temp1;
7358 pos_rtx = temp;
7360 else if (pos_rtx != 0
7361 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7362 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7364 /* Make POS_RTX unless we already have it and it is correct. If we don't
7365 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7366 be a CONST_INT. */
7367 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7368 pos_rtx = orig_pos_rtx;
7370 else if (pos_rtx == 0)
7371 pos_rtx = GEN_INT (pos);
7373 /* Make the required operation. See if we can use existing rtx. */
7374 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7375 extraction_mode, inner, GEN_INT (len), pos_rtx);
7376 if (! in_dest)
7377 new_rtx = gen_lowpart (mode, new_rtx);
7379 return new_rtx;
7382 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7383 with any other operations in X. Return X without that shift if so. */
7385 static rtx
7386 extract_left_shift (rtx x, int count)
7388 enum rtx_code code = GET_CODE (x);
7389 enum machine_mode mode = GET_MODE (x);
7390 rtx tem;
7392 switch (code)
7394 case ASHIFT:
7395 /* This is the shift itself. If it is wide enough, we will return
7396 either the value being shifted if the shift count is equal to
7397 COUNT or a shift for the difference. */
7398 if (CONST_INT_P (XEXP (x, 1))
7399 && INTVAL (XEXP (x, 1)) >= count)
7400 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7401 INTVAL (XEXP (x, 1)) - count);
7402 break;
7404 case NEG: case NOT:
7405 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7406 return simplify_gen_unary (code, mode, tem, mode);
7408 break;
7410 case PLUS: case IOR: case XOR: case AND:
7411 /* If we can safely shift this constant and we find the inner shift,
7412 make a new operation. */
7413 if (CONST_INT_P (XEXP (x, 1))
7414 && (UINTVAL (XEXP (x, 1))
7415 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7416 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7417 return simplify_gen_binary (code, mode, tem,
7418 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7420 break;
7422 default:
7423 break;
7426 return 0;
7429 /* Look at the expression rooted at X. Look for expressions
7430 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7431 Form these expressions.
7433 Return the new rtx, usually just X.
7435 Also, for machines like the VAX that don't have logical shift insns,
7436 try to convert logical to arithmetic shift operations in cases where
7437 they are equivalent. This undoes the canonicalizations to logical
7438 shifts done elsewhere.
7440 We try, as much as possible, to re-use rtl expressions to save memory.
7442 IN_CODE says what kind of expression we are processing. Normally, it is
7443 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7444 being kludges), it is MEM. When processing the arguments of a comparison
7445 or a COMPARE against zero, it is COMPARE. */
7448 make_compound_operation (rtx x, enum rtx_code in_code)
7450 enum rtx_code code = GET_CODE (x);
7451 enum machine_mode mode = GET_MODE (x);
7452 int mode_width = GET_MODE_PRECISION (mode);
7453 rtx rhs, lhs;
7454 enum rtx_code next_code;
7455 int i, j;
7456 rtx new_rtx = 0;
7457 rtx tem;
7458 const char *fmt;
7460 /* Select the code to be used in recursive calls. Once we are inside an
7461 address, we stay there. If we have a comparison, set to COMPARE,
7462 but once inside, go back to our default of SET. */
7464 next_code = (code == MEM ? MEM
7465 : ((code == PLUS || code == MINUS)
7466 && SCALAR_INT_MODE_P (mode)) ? MEM
7467 : ((code == COMPARE || COMPARISON_P (x))
7468 && XEXP (x, 1) == const0_rtx) ? COMPARE
7469 : in_code == COMPARE ? SET : in_code);
7471 /* Process depending on the code of this operation. If NEW is set
7472 nonzero, it will be returned. */
7474 switch (code)
7476 case ASHIFT:
7477 /* Convert shifts by constants into multiplications if inside
7478 an address. */
7479 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7480 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7481 && INTVAL (XEXP (x, 1)) >= 0
7482 && SCALAR_INT_MODE_P (mode))
7484 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7485 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7487 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7488 if (GET_CODE (new_rtx) == NEG)
7490 new_rtx = XEXP (new_rtx, 0);
7491 multval = -multval;
7493 multval = trunc_int_for_mode (multval, mode);
7494 new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7496 break;
7498 case PLUS:
7499 lhs = XEXP (x, 0);
7500 rhs = XEXP (x, 1);
7501 lhs = make_compound_operation (lhs, next_code);
7502 rhs = make_compound_operation (rhs, next_code);
7503 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7504 && SCALAR_INT_MODE_P (mode))
7506 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7507 XEXP (lhs, 1));
7508 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7510 else if (GET_CODE (lhs) == MULT
7511 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7513 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7514 simplify_gen_unary (NEG, mode,
7515 XEXP (lhs, 1),
7516 mode));
7517 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7519 else
7521 SUBST (XEXP (x, 0), lhs);
7522 SUBST (XEXP (x, 1), rhs);
7523 goto maybe_swap;
7525 x = gen_lowpart (mode, new_rtx);
7526 goto maybe_swap;
7528 case MINUS:
7529 lhs = XEXP (x, 0);
7530 rhs = XEXP (x, 1);
7531 lhs = make_compound_operation (lhs, next_code);
7532 rhs = make_compound_operation (rhs, next_code);
7533 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7534 && SCALAR_INT_MODE_P (mode))
7536 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7537 XEXP (rhs, 1));
7538 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7540 else if (GET_CODE (rhs) == MULT
7541 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7543 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7544 simplify_gen_unary (NEG, mode,
7545 XEXP (rhs, 1),
7546 mode));
7547 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7549 else
7551 SUBST (XEXP (x, 0), lhs);
7552 SUBST (XEXP (x, 1), rhs);
7553 return x;
7555 return gen_lowpart (mode, new_rtx);
7557 case AND:
7558 /* If the second operand is not a constant, we can't do anything
7559 with it. */
7560 if (!CONST_INT_P (XEXP (x, 1)))
7561 break;
7563 /* If the constant is a power of two minus one and the first operand
7564 is a logical right shift, make an extraction. */
7565 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7566 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7568 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7569 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7570 0, in_code == COMPARE);
7573 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7574 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7575 && subreg_lowpart_p (XEXP (x, 0))
7576 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7577 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7579 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7580 next_code);
7581 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7582 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7583 0, in_code == COMPARE);
7585 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7586 else if ((GET_CODE (XEXP (x, 0)) == XOR
7587 || GET_CODE (XEXP (x, 0)) == IOR)
7588 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7589 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7590 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7592 /* Apply the distributive law, and then try to make extractions. */
7593 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7594 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7595 XEXP (x, 1)),
7596 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7597 XEXP (x, 1)));
7598 new_rtx = make_compound_operation (new_rtx, in_code);
7601 /* If we are have (and (rotate X C) M) and C is larger than the number
7602 of bits in M, this is an extraction. */
7604 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7605 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7606 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7607 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7609 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7610 new_rtx = make_extraction (mode, new_rtx,
7611 (GET_MODE_PRECISION (mode)
7612 - INTVAL (XEXP (XEXP (x, 0), 1))),
7613 NULL_RTX, i, 1, 0, in_code == COMPARE);
7616 /* On machines without logical shifts, if the operand of the AND is
7617 a logical shift and our mask turns off all the propagated sign
7618 bits, we can replace the logical shift with an arithmetic shift. */
7619 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7620 && !have_insn_for (LSHIFTRT, mode)
7621 && have_insn_for (ASHIFTRT, mode)
7622 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7623 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7624 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7625 && mode_width <= HOST_BITS_PER_WIDE_INT)
7627 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7629 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7630 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7631 SUBST (XEXP (x, 0),
7632 gen_rtx_ASHIFTRT (mode,
7633 make_compound_operation
7634 (XEXP (XEXP (x, 0), 0), next_code),
7635 XEXP (XEXP (x, 0), 1)));
7638 /* If the constant is one less than a power of two, this might be
7639 representable by an extraction even if no shift is present.
7640 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7641 we are in a COMPARE. */
7642 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7643 new_rtx = make_extraction (mode,
7644 make_compound_operation (XEXP (x, 0),
7645 next_code),
7646 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7648 /* If we are in a comparison and this is an AND with a power of two,
7649 convert this into the appropriate bit extract. */
7650 else if (in_code == COMPARE
7651 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7652 new_rtx = make_extraction (mode,
7653 make_compound_operation (XEXP (x, 0),
7654 next_code),
7655 i, NULL_RTX, 1, 1, 0, 1);
7657 break;
7659 case LSHIFTRT:
7660 /* If the sign bit is known to be zero, replace this with an
7661 arithmetic shift. */
7662 if (have_insn_for (ASHIFTRT, mode)
7663 && ! have_insn_for (LSHIFTRT, mode)
7664 && mode_width <= HOST_BITS_PER_WIDE_INT
7665 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7667 new_rtx = gen_rtx_ASHIFTRT (mode,
7668 make_compound_operation (XEXP (x, 0),
7669 next_code),
7670 XEXP (x, 1));
7671 break;
7674 /* ... fall through ... */
7676 case ASHIFTRT:
7677 lhs = XEXP (x, 0);
7678 rhs = XEXP (x, 1);
7680 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7681 this is a SIGN_EXTRACT. */
7682 if (CONST_INT_P (rhs)
7683 && GET_CODE (lhs) == ASHIFT
7684 && CONST_INT_P (XEXP (lhs, 1))
7685 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7686 && INTVAL (XEXP (lhs, 1)) >= 0
7687 && INTVAL (rhs) < mode_width)
7689 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7690 new_rtx = make_extraction (mode, new_rtx,
7691 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7692 NULL_RTX, mode_width - INTVAL (rhs),
7693 code == LSHIFTRT, 0, in_code == COMPARE);
7694 break;
7697 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7698 If so, try to merge the shifts into a SIGN_EXTEND. We could
7699 also do this for some cases of SIGN_EXTRACT, but it doesn't
7700 seem worth the effort; the case checked for occurs on Alpha. */
7702 if (!OBJECT_P (lhs)
7703 && ! (GET_CODE (lhs) == SUBREG
7704 && (OBJECT_P (SUBREG_REG (lhs))))
7705 && CONST_INT_P (rhs)
7706 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7707 && INTVAL (rhs) < mode_width
7708 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7709 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7710 0, NULL_RTX, mode_width - INTVAL (rhs),
7711 code == LSHIFTRT, 0, in_code == COMPARE);
7713 break;
7715 case SUBREG:
7716 /* Call ourselves recursively on the inner expression. If we are
7717 narrowing the object and it has a different RTL code from
7718 what it originally did, do this SUBREG as a force_to_mode. */
7720 rtx inner = SUBREG_REG (x), simplified;
7722 tem = make_compound_operation (inner, in_code);
7724 simplified
7725 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7726 if (simplified)
7727 tem = simplified;
7729 if (GET_CODE (tem) != GET_CODE (inner)
7730 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7731 && subreg_lowpart_p (x))
7733 rtx newer
7734 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7736 /* If we have something other than a SUBREG, we might have
7737 done an expansion, so rerun ourselves. */
7738 if (GET_CODE (newer) != SUBREG)
7739 newer = make_compound_operation (newer, in_code);
7741 /* force_to_mode can expand compounds. If it just re-expanded the
7742 compound, use gen_lowpart to convert to the desired mode. */
7743 if (rtx_equal_p (newer, x)
7744 /* Likewise if it re-expanded the compound only partially.
7745 This happens for SUBREG of ZERO_EXTRACT if they extract
7746 the same number of bits. */
7747 || (GET_CODE (newer) == SUBREG
7748 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7749 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7750 && GET_CODE (inner) == AND
7751 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7752 return gen_lowpart (GET_MODE (x), tem);
7754 return newer;
7757 if (simplified)
7758 return tem;
7760 break;
7762 default:
7763 break;
7766 if (new_rtx)
7768 x = gen_lowpart (mode, new_rtx);
7769 code = GET_CODE (x);
7772 /* Now recursively process each operand of this operation. We need to
7773 handle ZERO_EXTEND specially so that we don't lose track of the
7774 inner mode. */
7775 if (GET_CODE (x) == ZERO_EXTEND)
7777 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7778 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7779 new_rtx, GET_MODE (XEXP (x, 0)));
7780 if (tem)
7781 return tem;
7782 SUBST (XEXP (x, 0), new_rtx);
7783 return x;
7786 fmt = GET_RTX_FORMAT (code);
7787 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7788 if (fmt[i] == 'e')
7790 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7791 SUBST (XEXP (x, i), new_rtx);
7793 else if (fmt[i] == 'E')
7794 for (j = 0; j < XVECLEN (x, i); j++)
7796 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7797 SUBST (XVECEXP (x, i, j), new_rtx);
7800 maybe_swap:
7801 /* If this is a commutative operation, the changes to the operands
7802 may have made it noncanonical. */
7803 if (COMMUTATIVE_ARITH_P (x)
7804 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7806 tem = XEXP (x, 0);
7807 SUBST (XEXP (x, 0), XEXP (x, 1));
7808 SUBST (XEXP (x, 1), tem);
7811 return x;
7814 /* Given M see if it is a value that would select a field of bits
7815 within an item, but not the entire word. Return -1 if not.
7816 Otherwise, return the starting position of the field, where 0 is the
7817 low-order bit.
7819 *PLEN is set to the length of the field. */
7821 static int
7822 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7824 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7825 int pos = m ? ctz_hwi (m) : -1;
7826 int len = 0;
7828 if (pos >= 0)
7829 /* Now shift off the low-order zero bits and see if we have a
7830 power of two minus 1. */
7831 len = exact_log2 ((m >> pos) + 1);
7833 if (len <= 0)
7834 pos = -1;
7836 *plen = len;
7837 return pos;
7840 /* If X refers to a register that equals REG in value, replace these
7841 references with REG. */
7842 static rtx
7843 canon_reg_for_combine (rtx x, rtx reg)
7845 rtx op0, op1, op2;
7846 const char *fmt;
7847 int i;
7848 bool copied;
7850 enum rtx_code code = GET_CODE (x);
7851 switch (GET_RTX_CLASS (code))
7853 case RTX_UNARY:
7854 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7855 if (op0 != XEXP (x, 0))
7856 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7857 GET_MODE (reg));
7858 break;
7860 case RTX_BIN_ARITH:
7861 case RTX_COMM_ARITH:
7862 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7863 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7864 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7865 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7866 break;
7868 case RTX_COMPARE:
7869 case RTX_COMM_COMPARE:
7870 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7871 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7872 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7873 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7874 GET_MODE (op0), op0, op1);
7875 break;
7877 case RTX_TERNARY:
7878 case RTX_BITFIELD_OPS:
7879 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7880 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7881 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7882 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7883 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7884 GET_MODE (op0), op0, op1, op2);
7886 case RTX_OBJ:
7887 if (REG_P (x))
7889 if (rtx_equal_p (get_last_value (reg), x)
7890 || rtx_equal_p (reg, get_last_value (x)))
7891 return reg;
7892 else
7893 break;
7896 /* fall through */
7898 default:
7899 fmt = GET_RTX_FORMAT (code);
7900 copied = false;
7901 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7902 if (fmt[i] == 'e')
7904 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7905 if (op != XEXP (x, i))
7907 if (!copied)
7909 copied = true;
7910 x = copy_rtx (x);
7912 XEXP (x, i) = op;
7915 else if (fmt[i] == 'E')
7917 int j;
7918 for (j = 0; j < XVECLEN (x, i); j++)
7920 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7921 if (op != XVECEXP (x, i, j))
7923 if (!copied)
7925 copied = true;
7926 x = copy_rtx (x);
7928 XVECEXP (x, i, j) = op;
7933 break;
7936 return x;
7939 /* Return X converted to MODE. If the value is already truncated to
7940 MODE we can just return a subreg even though in the general case we
7941 would need an explicit truncation. */
7943 static rtx
7944 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7946 if (!CONST_INT_P (x)
7947 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7948 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
7949 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7951 /* Bit-cast X into an integer mode. */
7952 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7953 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7954 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7955 x, GET_MODE (x));
7958 return gen_lowpart (mode, x);
7961 /* See if X can be simplified knowing that we will only refer to it in
7962 MODE and will only refer to those bits that are nonzero in MASK.
7963 If other bits are being computed or if masking operations are done
7964 that select a superset of the bits in MASK, they can sometimes be
7965 ignored.
7967 Return a possibly simplified expression, but always convert X to
7968 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7970 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7971 are all off in X. This is used when X will be complemented, by either
7972 NOT, NEG, or XOR. */
7974 static rtx
7975 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7976 int just_select)
7978 enum rtx_code code = GET_CODE (x);
7979 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7980 enum machine_mode op_mode;
7981 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7982 rtx op0, op1, temp;
7984 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7985 code below will do the wrong thing since the mode of such an
7986 expression is VOIDmode.
7988 Also do nothing if X is a CLOBBER; this can happen if X was
7989 the return value from a call to gen_lowpart. */
7990 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7991 return x;
7993 /* We want to perform the operation is its present mode unless we know
7994 that the operation is valid in MODE, in which case we do the operation
7995 in MODE. */
7996 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7997 && have_insn_for (code, mode))
7998 ? mode : GET_MODE (x));
8000 /* It is not valid to do a right-shift in a narrower mode
8001 than the one it came in with. */
8002 if ((code == LSHIFTRT || code == ASHIFTRT)
8003 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8004 op_mode = GET_MODE (x);
8006 /* Truncate MASK to fit OP_MODE. */
8007 if (op_mode)
8008 mask &= GET_MODE_MASK (op_mode);
8010 /* When we have an arithmetic operation, or a shift whose count we
8011 do not know, we need to assume that all bits up to the highest-order
8012 bit in MASK will be needed. This is how we form such a mask. */
8013 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8014 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8015 else
8016 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8017 - 1);
8019 /* Determine what bits of X are guaranteed to be (non)zero. */
8020 nonzero = nonzero_bits (x, mode);
8022 /* If none of the bits in X are needed, return a zero. */
8023 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8024 x = const0_rtx;
8026 /* If X is a CONST_INT, return a new one. Do this here since the
8027 test below will fail. */
8028 if (CONST_INT_P (x))
8030 if (SCALAR_INT_MODE_P (mode))
8031 return gen_int_mode (INTVAL (x) & mask, mode);
8032 else
8034 x = GEN_INT (INTVAL (x) & mask);
8035 return gen_lowpart_common (mode, x);
8039 /* If X is narrower than MODE and we want all the bits in X's mode, just
8040 get X in the proper mode. */
8041 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8042 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8043 return gen_lowpart (mode, x);
8045 /* We can ignore the effect of a SUBREG if it narrows the mode or
8046 if the constant masks to zero all the bits the mode doesn't have. */
8047 if (GET_CODE (x) == SUBREG
8048 && subreg_lowpart_p (x)
8049 && ((GET_MODE_SIZE (GET_MODE (x))
8050 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8051 || (0 == (mask
8052 & GET_MODE_MASK (GET_MODE (x))
8053 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8054 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8056 /* The arithmetic simplifications here only work for scalar integer modes. */
8057 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8058 return gen_lowpart_or_truncate (mode, x);
8060 switch (code)
8062 case CLOBBER:
8063 /* If X is a (clobber (const_int)), return it since we know we are
8064 generating something that won't match. */
8065 return x;
8067 case SIGN_EXTEND:
8068 case ZERO_EXTEND:
8069 case ZERO_EXTRACT:
8070 case SIGN_EXTRACT:
8071 x = expand_compound_operation (x);
8072 if (GET_CODE (x) != code)
8073 return force_to_mode (x, mode, mask, next_select);
8074 break;
8076 case TRUNCATE:
8077 /* Similarly for a truncate. */
8078 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8080 case AND:
8081 /* If this is an AND with a constant, convert it into an AND
8082 whose constant is the AND of that constant with MASK. If it
8083 remains an AND of MASK, delete it since it is redundant. */
8085 if (CONST_INT_P (XEXP (x, 1)))
8087 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8088 mask & INTVAL (XEXP (x, 1)));
8090 /* If X is still an AND, see if it is an AND with a mask that
8091 is just some low-order bits. If so, and it is MASK, we don't
8092 need it. */
8094 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8095 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8096 == mask))
8097 x = XEXP (x, 0);
8099 /* If it remains an AND, try making another AND with the bits
8100 in the mode mask that aren't in MASK turned on. If the
8101 constant in the AND is wide enough, this might make a
8102 cheaper constant. */
8104 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8105 && GET_MODE_MASK (GET_MODE (x)) != mask
8106 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8108 unsigned HOST_WIDE_INT cval
8109 = UINTVAL (XEXP (x, 1))
8110 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8111 int width = GET_MODE_PRECISION (GET_MODE (x));
8112 rtx y;
8114 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8115 number, sign extend it. */
8116 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8117 && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8118 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8120 y = simplify_gen_binary (AND, GET_MODE (x),
8121 XEXP (x, 0), GEN_INT (cval));
8122 if (set_src_cost (y, optimize_this_for_speed_p)
8123 < set_src_cost (x, optimize_this_for_speed_p))
8124 x = y;
8127 break;
8130 goto binop;
8132 case PLUS:
8133 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8134 low-order bits (as in an alignment operation) and FOO is already
8135 aligned to that boundary, mask C1 to that boundary as well.
8136 This may eliminate that PLUS and, later, the AND. */
8139 unsigned int width = GET_MODE_PRECISION (mode);
8140 unsigned HOST_WIDE_INT smask = mask;
8142 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8143 number, sign extend it. */
8145 if (width < HOST_BITS_PER_WIDE_INT
8146 && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8147 smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8149 if (CONST_INT_P (XEXP (x, 1))
8150 && exact_log2 (- smask) >= 0
8151 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8152 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8153 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8154 (INTVAL (XEXP (x, 1)) & smask)),
8155 mode, smask, next_select);
8158 /* ... fall through ... */
8160 case MULT:
8161 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8162 most significant bit in MASK since carries from those bits will
8163 affect the bits we are interested in. */
8164 mask = fuller_mask;
8165 goto binop;
8167 case MINUS:
8168 /* If X is (minus C Y) where C's least set bit is larger than any bit
8169 in the mask, then we may replace with (neg Y). */
8170 if (CONST_INT_P (XEXP (x, 0))
8171 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8172 & -INTVAL (XEXP (x, 0))))
8173 > mask))
8175 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8176 GET_MODE (x));
8177 return force_to_mode (x, mode, mask, next_select);
8180 /* Similarly, if C contains every bit in the fuller_mask, then we may
8181 replace with (not Y). */
8182 if (CONST_INT_P (XEXP (x, 0))
8183 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8185 x = simplify_gen_unary (NOT, GET_MODE (x),
8186 XEXP (x, 1), GET_MODE (x));
8187 return force_to_mode (x, mode, mask, next_select);
8190 mask = fuller_mask;
8191 goto binop;
8193 case IOR:
8194 case XOR:
8195 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8196 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8197 operation which may be a bitfield extraction. Ensure that the
8198 constant we form is not wider than the mode of X. */
8200 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8201 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8202 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8203 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8204 && CONST_INT_P (XEXP (x, 1))
8205 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8206 + floor_log2 (INTVAL (XEXP (x, 1))))
8207 < GET_MODE_PRECISION (GET_MODE (x)))
8208 && (UINTVAL (XEXP (x, 1))
8209 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8211 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8212 << INTVAL (XEXP (XEXP (x, 0), 1)));
8213 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8214 XEXP (XEXP (x, 0), 0), temp);
8215 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8216 XEXP (XEXP (x, 0), 1));
8217 return force_to_mode (x, mode, mask, next_select);
8220 binop:
8221 /* For most binary operations, just propagate into the operation and
8222 change the mode if we have an operation of that mode. */
8224 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8225 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8227 /* If we ended up truncating both operands, truncate the result of the
8228 operation instead. */
8229 if (GET_CODE (op0) == TRUNCATE
8230 && GET_CODE (op1) == TRUNCATE)
8232 op0 = XEXP (op0, 0);
8233 op1 = XEXP (op1, 0);
8236 op0 = gen_lowpart_or_truncate (op_mode, op0);
8237 op1 = gen_lowpart_or_truncate (op_mode, op1);
8239 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8240 x = simplify_gen_binary (code, op_mode, op0, op1);
8241 break;
8243 case ASHIFT:
8244 /* For left shifts, do the same, but just for the first operand.
8245 However, we cannot do anything with shifts where we cannot
8246 guarantee that the counts are smaller than the size of the mode
8247 because such a count will have a different meaning in a
8248 wider mode. */
8250 if (! (CONST_INT_P (XEXP (x, 1))
8251 && INTVAL (XEXP (x, 1)) >= 0
8252 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8253 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8254 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8255 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8256 break;
8258 /* If the shift count is a constant and we can do arithmetic in
8259 the mode of the shift, refine which bits we need. Otherwise, use the
8260 conservative form of the mask. */
8261 if (CONST_INT_P (XEXP (x, 1))
8262 && INTVAL (XEXP (x, 1)) >= 0
8263 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8264 && HWI_COMPUTABLE_MODE_P (op_mode))
8265 mask >>= INTVAL (XEXP (x, 1));
8266 else
8267 mask = fuller_mask;
8269 op0 = gen_lowpart_or_truncate (op_mode,
8270 force_to_mode (XEXP (x, 0), op_mode,
8271 mask, next_select));
8273 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8274 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8275 break;
8277 case LSHIFTRT:
8278 /* Here we can only do something if the shift count is a constant,
8279 this shift constant is valid for the host, and we can do arithmetic
8280 in OP_MODE. */
8282 if (CONST_INT_P (XEXP (x, 1))
8283 && INTVAL (XEXP (x, 1)) >= 0
8284 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8285 && HWI_COMPUTABLE_MODE_P (op_mode))
8287 rtx inner = XEXP (x, 0);
8288 unsigned HOST_WIDE_INT inner_mask;
8290 /* Select the mask of the bits we need for the shift operand. */
8291 inner_mask = mask << INTVAL (XEXP (x, 1));
8293 /* We can only change the mode of the shift if we can do arithmetic
8294 in the mode of the shift and INNER_MASK is no wider than the
8295 width of X's mode. */
8296 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8297 op_mode = GET_MODE (x);
8299 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8301 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8302 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8305 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8306 shift and AND produces only copies of the sign bit (C2 is one less
8307 than a power of two), we can do this with just a shift. */
8309 if (GET_CODE (x) == LSHIFTRT
8310 && CONST_INT_P (XEXP (x, 1))
8311 /* The shift puts one of the sign bit copies in the least significant
8312 bit. */
8313 && ((INTVAL (XEXP (x, 1))
8314 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8315 >= GET_MODE_PRECISION (GET_MODE (x)))
8316 && exact_log2 (mask + 1) >= 0
8317 /* Number of bits left after the shift must be more than the mask
8318 needs. */
8319 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8320 <= GET_MODE_PRECISION (GET_MODE (x)))
8321 /* Must be more sign bit copies than the mask needs. */
8322 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8323 >= exact_log2 (mask + 1)))
8324 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8325 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8326 - exact_log2 (mask + 1)));
8328 goto shiftrt;
8330 case ASHIFTRT:
8331 /* If we are just looking for the sign bit, we don't need this shift at
8332 all, even if it has a variable count. */
8333 if (val_signbit_p (GET_MODE (x), mask))
8334 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8336 /* If this is a shift by a constant, get a mask that contains those bits
8337 that are not copies of the sign bit. We then have two cases: If
8338 MASK only includes those bits, this can be a logical shift, which may
8339 allow simplifications. If MASK is a single-bit field not within
8340 those bits, we are requesting a copy of the sign bit and hence can
8341 shift the sign bit to the appropriate location. */
8343 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8344 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8346 int i;
8348 /* If the considered data is wider than HOST_WIDE_INT, we can't
8349 represent a mask for all its bits in a single scalar.
8350 But we only care about the lower bits, so calculate these. */
8352 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8354 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8356 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8357 is the number of bits a full-width mask would have set.
8358 We need only shift if these are fewer than nonzero can
8359 hold. If not, we must keep all bits set in nonzero. */
8361 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8362 < HOST_BITS_PER_WIDE_INT)
8363 nonzero >>= INTVAL (XEXP (x, 1))
8364 + HOST_BITS_PER_WIDE_INT
8365 - GET_MODE_PRECISION (GET_MODE (x)) ;
8367 else
8369 nonzero = GET_MODE_MASK (GET_MODE (x));
8370 nonzero >>= INTVAL (XEXP (x, 1));
8373 if ((mask & ~nonzero) == 0)
8375 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8376 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8377 if (GET_CODE (x) != ASHIFTRT)
8378 return force_to_mode (x, mode, mask, next_select);
8381 else if ((i = exact_log2 (mask)) >= 0)
8383 x = simplify_shift_const
8384 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8385 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8387 if (GET_CODE (x) != ASHIFTRT)
8388 return force_to_mode (x, mode, mask, next_select);
8392 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8393 even if the shift count isn't a constant. */
8394 if (mask == 1)
8395 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8396 XEXP (x, 0), XEXP (x, 1));
8398 shiftrt:
8400 /* If this is a zero- or sign-extension operation that just affects bits
8401 we don't care about, remove it. Be sure the call above returned
8402 something that is still a shift. */
8404 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8405 && CONST_INT_P (XEXP (x, 1))
8406 && INTVAL (XEXP (x, 1)) >= 0
8407 && (INTVAL (XEXP (x, 1))
8408 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8409 && GET_CODE (XEXP (x, 0)) == ASHIFT
8410 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8411 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8412 next_select);
8414 break;
8416 case ROTATE:
8417 case ROTATERT:
8418 /* If the shift count is constant and we can do computations
8419 in the mode of X, compute where the bits we care about are.
8420 Otherwise, we can't do anything. Don't change the mode of
8421 the shift or propagate MODE into the shift, though. */
8422 if (CONST_INT_P (XEXP (x, 1))
8423 && INTVAL (XEXP (x, 1)) >= 0)
8425 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8426 GET_MODE (x), GEN_INT (mask),
8427 XEXP (x, 1));
8428 if (temp && CONST_INT_P (temp))
8429 SUBST (XEXP (x, 0),
8430 force_to_mode (XEXP (x, 0), GET_MODE (x),
8431 INTVAL (temp), next_select));
8433 break;
8435 case NEG:
8436 /* If we just want the low-order bit, the NEG isn't needed since it
8437 won't change the low-order bit. */
8438 if (mask == 1)
8439 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8441 /* We need any bits less significant than the most significant bit in
8442 MASK since carries from those bits will affect the bits we are
8443 interested in. */
8444 mask = fuller_mask;
8445 goto unop;
8447 case NOT:
8448 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8449 same as the XOR case above. Ensure that the constant we form is not
8450 wider than the mode of X. */
8452 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8453 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8454 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8455 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8456 < GET_MODE_PRECISION (GET_MODE (x)))
8457 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8459 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8460 GET_MODE (x));
8461 temp = simplify_gen_binary (XOR, GET_MODE (x),
8462 XEXP (XEXP (x, 0), 0), temp);
8463 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8464 temp, XEXP (XEXP (x, 0), 1));
8466 return force_to_mode (x, mode, mask, next_select);
8469 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8470 use the full mask inside the NOT. */
8471 mask = fuller_mask;
8473 unop:
8474 op0 = gen_lowpart_or_truncate (op_mode,
8475 force_to_mode (XEXP (x, 0), mode, mask,
8476 next_select));
8477 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8478 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8479 break;
8481 case NE:
8482 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8483 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8484 which is equal to STORE_FLAG_VALUE. */
8485 if ((mask & ~STORE_FLAG_VALUE) == 0
8486 && XEXP (x, 1) == const0_rtx
8487 && GET_MODE (XEXP (x, 0)) == mode
8488 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8489 && (nonzero_bits (XEXP (x, 0), mode)
8490 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8491 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8493 break;
8495 case IF_THEN_ELSE:
8496 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8497 written in a narrower mode. We play it safe and do not do so. */
8499 SUBST (XEXP (x, 1),
8500 gen_lowpart_or_truncate (GET_MODE (x),
8501 force_to_mode (XEXP (x, 1), mode,
8502 mask, next_select)));
8503 SUBST (XEXP (x, 2),
8504 gen_lowpart_or_truncate (GET_MODE (x),
8505 force_to_mode (XEXP (x, 2), mode,
8506 mask, next_select)));
8507 break;
8509 default:
8510 break;
8513 /* Ensure we return a value of the proper mode. */
8514 return gen_lowpart_or_truncate (mode, x);
8517 /* Return nonzero if X is an expression that has one of two values depending on
8518 whether some other value is zero or nonzero. In that case, we return the
8519 value that is being tested, *PTRUE is set to the value if the rtx being
8520 returned has a nonzero value, and *PFALSE is set to the other alternative.
8522 If we return zero, we set *PTRUE and *PFALSE to X. */
8524 static rtx
8525 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8527 enum machine_mode mode = GET_MODE (x);
8528 enum rtx_code code = GET_CODE (x);
8529 rtx cond0, cond1, true0, true1, false0, false1;
8530 unsigned HOST_WIDE_INT nz;
8532 /* If we are comparing a value against zero, we are done. */
8533 if ((code == NE || code == EQ)
8534 && XEXP (x, 1) == const0_rtx)
8536 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8537 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8538 return XEXP (x, 0);
8541 /* If this is a unary operation whose operand has one of two values, apply
8542 our opcode to compute those values. */
8543 else if (UNARY_P (x)
8544 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8546 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8547 *pfalse = simplify_gen_unary (code, mode, false0,
8548 GET_MODE (XEXP (x, 0)));
8549 return cond0;
8552 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8553 make can't possibly match and would suppress other optimizations. */
8554 else if (code == COMPARE)
8557 /* If this is a binary operation, see if either side has only one of two
8558 values. If either one does or if both do and they are conditional on
8559 the same value, compute the new true and false values. */
8560 else if (BINARY_P (x))
8562 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8563 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8565 if ((cond0 != 0 || cond1 != 0)
8566 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8568 /* If if_then_else_cond returned zero, then true/false are the
8569 same rtl. We must copy one of them to prevent invalid rtl
8570 sharing. */
8571 if (cond0 == 0)
8572 true0 = copy_rtx (true0);
8573 else if (cond1 == 0)
8574 true1 = copy_rtx (true1);
8576 if (COMPARISON_P (x))
8578 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8579 true0, true1);
8580 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8581 false0, false1);
8583 else
8585 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8586 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8589 return cond0 ? cond0 : cond1;
8592 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8593 operands is zero when the other is nonzero, and vice-versa,
8594 and STORE_FLAG_VALUE is 1 or -1. */
8596 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8597 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8598 || code == UMAX)
8599 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8601 rtx op0 = XEXP (XEXP (x, 0), 1);
8602 rtx op1 = XEXP (XEXP (x, 1), 1);
8604 cond0 = XEXP (XEXP (x, 0), 0);
8605 cond1 = XEXP (XEXP (x, 1), 0);
8607 if (COMPARISON_P (cond0)
8608 && COMPARISON_P (cond1)
8609 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8610 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8611 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8612 || ((swap_condition (GET_CODE (cond0))
8613 == reversed_comparison_code (cond1, NULL))
8614 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8615 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8616 && ! side_effects_p (x))
8618 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8619 *pfalse = simplify_gen_binary (MULT, mode,
8620 (code == MINUS
8621 ? simplify_gen_unary (NEG, mode,
8622 op1, mode)
8623 : op1),
8624 const_true_rtx);
8625 return cond0;
8629 /* Similarly for MULT, AND and UMIN, except that for these the result
8630 is always zero. */
8631 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8632 && (code == MULT || code == AND || code == UMIN)
8633 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8635 cond0 = XEXP (XEXP (x, 0), 0);
8636 cond1 = XEXP (XEXP (x, 1), 0);
8638 if (COMPARISON_P (cond0)
8639 && COMPARISON_P (cond1)
8640 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8641 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8642 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8643 || ((swap_condition (GET_CODE (cond0))
8644 == reversed_comparison_code (cond1, NULL))
8645 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8646 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8647 && ! side_effects_p (x))
8649 *ptrue = *pfalse = const0_rtx;
8650 return cond0;
8655 else if (code == IF_THEN_ELSE)
8657 /* If we have IF_THEN_ELSE already, extract the condition and
8658 canonicalize it if it is NE or EQ. */
8659 cond0 = XEXP (x, 0);
8660 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8661 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8662 return XEXP (cond0, 0);
8663 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8665 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8666 return XEXP (cond0, 0);
8668 else
8669 return cond0;
8672 /* If X is a SUBREG, we can narrow both the true and false values
8673 if the inner expression, if there is a condition. */
8674 else if (code == SUBREG
8675 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8676 &true0, &false0)))
8678 true0 = simplify_gen_subreg (mode, true0,
8679 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8680 false0 = simplify_gen_subreg (mode, false0,
8681 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8682 if (true0 && false0)
8684 *ptrue = true0;
8685 *pfalse = false0;
8686 return cond0;
8690 /* If X is a constant, this isn't special and will cause confusions
8691 if we treat it as such. Likewise if it is equivalent to a constant. */
8692 else if (CONSTANT_P (x)
8693 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8696 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8697 will be least confusing to the rest of the compiler. */
8698 else if (mode == BImode)
8700 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8701 return x;
8704 /* If X is known to be either 0 or -1, those are the true and
8705 false values when testing X. */
8706 else if (x == constm1_rtx || x == const0_rtx
8707 || (mode != VOIDmode
8708 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8710 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8711 return x;
8714 /* Likewise for 0 or a single bit. */
8715 else if (HWI_COMPUTABLE_MODE_P (mode)
8716 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8718 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8719 return x;
8722 /* Otherwise fail; show no condition with true and false values the same. */
8723 *ptrue = *pfalse = x;
8724 return 0;
8727 /* Return the value of expression X given the fact that condition COND
8728 is known to be true when applied to REG as its first operand and VAL
8729 as its second. X is known to not be shared and so can be modified in
8730 place.
8732 We only handle the simplest cases, and specifically those cases that
8733 arise with IF_THEN_ELSE expressions. */
8735 static rtx
8736 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8738 enum rtx_code code = GET_CODE (x);
8739 rtx temp;
8740 const char *fmt;
8741 int i, j;
8743 if (side_effects_p (x))
8744 return x;
8746 /* If either operand of the condition is a floating point value,
8747 then we have to avoid collapsing an EQ comparison. */
8748 if (cond == EQ
8749 && rtx_equal_p (x, reg)
8750 && ! FLOAT_MODE_P (GET_MODE (x))
8751 && ! FLOAT_MODE_P (GET_MODE (val)))
8752 return val;
8754 if (cond == UNEQ && rtx_equal_p (x, reg))
8755 return val;
8757 /* If X is (abs REG) and we know something about REG's relationship
8758 with zero, we may be able to simplify this. */
8760 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8761 switch (cond)
8763 case GE: case GT: case EQ:
8764 return XEXP (x, 0);
8765 case LT: case LE:
8766 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8767 XEXP (x, 0),
8768 GET_MODE (XEXP (x, 0)));
8769 default:
8770 break;
8773 /* The only other cases we handle are MIN, MAX, and comparisons if the
8774 operands are the same as REG and VAL. */
8776 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8778 if (rtx_equal_p (XEXP (x, 0), val))
8779 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8781 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8783 if (COMPARISON_P (x))
8785 if (comparison_dominates_p (cond, code))
8786 return const_true_rtx;
8788 code = reversed_comparison_code (x, NULL);
8789 if (code != UNKNOWN
8790 && comparison_dominates_p (cond, code))
8791 return const0_rtx;
8792 else
8793 return x;
8795 else if (code == SMAX || code == SMIN
8796 || code == UMIN || code == UMAX)
8798 int unsignedp = (code == UMIN || code == UMAX);
8800 /* Do not reverse the condition when it is NE or EQ.
8801 This is because we cannot conclude anything about
8802 the value of 'SMAX (x, y)' when x is not equal to y,
8803 but we can when x equals y. */
8804 if ((code == SMAX || code == UMAX)
8805 && ! (cond == EQ || cond == NE))
8806 cond = reverse_condition (cond);
8808 switch (cond)
8810 case GE: case GT:
8811 return unsignedp ? x : XEXP (x, 1);
8812 case LE: case LT:
8813 return unsignedp ? x : XEXP (x, 0);
8814 case GEU: case GTU:
8815 return unsignedp ? XEXP (x, 1) : x;
8816 case LEU: case LTU:
8817 return unsignedp ? XEXP (x, 0) : x;
8818 default:
8819 break;
8824 else if (code == SUBREG)
8826 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8827 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8829 if (SUBREG_REG (x) != r)
8831 /* We must simplify subreg here, before we lose track of the
8832 original inner_mode. */
8833 new_rtx = simplify_subreg (GET_MODE (x), r,
8834 inner_mode, SUBREG_BYTE (x));
8835 if (new_rtx)
8836 return new_rtx;
8837 else
8838 SUBST (SUBREG_REG (x), r);
8841 return x;
8843 /* We don't have to handle SIGN_EXTEND here, because even in the
8844 case of replacing something with a modeless CONST_INT, a
8845 CONST_INT is already (supposed to be) a valid sign extension for
8846 its narrower mode, which implies it's already properly
8847 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8848 story is different. */
8849 else if (code == ZERO_EXTEND)
8851 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8852 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8854 if (XEXP (x, 0) != r)
8856 /* We must simplify the zero_extend here, before we lose
8857 track of the original inner_mode. */
8858 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8859 r, inner_mode);
8860 if (new_rtx)
8861 return new_rtx;
8862 else
8863 SUBST (XEXP (x, 0), r);
8866 return x;
8869 fmt = GET_RTX_FORMAT (code);
8870 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8872 if (fmt[i] == 'e')
8873 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8874 else if (fmt[i] == 'E')
8875 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8876 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8877 cond, reg, val));
8880 return x;
8883 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8884 assignment as a field assignment. */
8886 static int
8887 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8889 if (x == y || rtx_equal_p (x, y))
8890 return 1;
8892 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8893 return 0;
8895 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8896 Note that all SUBREGs of MEM are paradoxical; otherwise they
8897 would have been rewritten. */
8898 if (MEM_P (x) && GET_CODE (y) == SUBREG
8899 && MEM_P (SUBREG_REG (y))
8900 && rtx_equal_p (SUBREG_REG (y),
8901 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8902 return 1;
8904 if (MEM_P (y) && GET_CODE (x) == SUBREG
8905 && MEM_P (SUBREG_REG (x))
8906 && rtx_equal_p (SUBREG_REG (x),
8907 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8908 return 1;
8910 /* We used to see if get_last_value of X and Y were the same but that's
8911 not correct. In one direction, we'll cause the assignment to have
8912 the wrong destination and in the case, we'll import a register into this
8913 insn that might have already have been dead. So fail if none of the
8914 above cases are true. */
8915 return 0;
8918 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8919 Return that assignment if so.
8921 We only handle the most common cases. */
8923 static rtx
8924 make_field_assignment (rtx x)
8926 rtx dest = SET_DEST (x);
8927 rtx src = SET_SRC (x);
8928 rtx assign;
8929 rtx rhs, lhs;
8930 HOST_WIDE_INT c1;
8931 HOST_WIDE_INT pos;
8932 unsigned HOST_WIDE_INT len;
8933 rtx other;
8934 enum machine_mode mode;
8936 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8937 a clear of a one-bit field. We will have changed it to
8938 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8939 for a SUBREG. */
8941 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8942 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8943 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8944 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8946 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8947 1, 1, 1, 0);
8948 if (assign != 0)
8949 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8950 return x;
8953 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8954 && subreg_lowpart_p (XEXP (src, 0))
8955 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8956 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8957 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8958 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8959 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8960 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8962 assign = make_extraction (VOIDmode, dest, 0,
8963 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8964 1, 1, 1, 0);
8965 if (assign != 0)
8966 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8967 return x;
8970 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8971 one-bit field. */
8972 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8973 && XEXP (XEXP (src, 0), 0) == const1_rtx
8974 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8976 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8977 1, 1, 1, 0);
8978 if (assign != 0)
8979 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8980 return x;
8983 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8984 SRC is an AND with all bits of that field set, then we can discard
8985 the AND. */
8986 if (GET_CODE (dest) == ZERO_EXTRACT
8987 && CONST_INT_P (XEXP (dest, 1))
8988 && GET_CODE (src) == AND
8989 && CONST_INT_P (XEXP (src, 1)))
8991 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8992 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8993 unsigned HOST_WIDE_INT ze_mask;
8995 if (width >= HOST_BITS_PER_WIDE_INT)
8996 ze_mask = -1;
8997 else
8998 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9000 /* Complete overlap. We can remove the source AND. */
9001 if ((and_mask & ze_mask) == ze_mask)
9002 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9004 /* Partial overlap. We can reduce the source AND. */
9005 if ((and_mask & ze_mask) != and_mask)
9007 mode = GET_MODE (src);
9008 src = gen_rtx_AND (mode, XEXP (src, 0),
9009 gen_int_mode (and_mask & ze_mask, mode));
9010 return gen_rtx_SET (VOIDmode, dest, src);
9014 /* The other case we handle is assignments into a constant-position
9015 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9016 a mask that has all one bits except for a group of zero bits and
9017 OTHER is known to have zeros where C1 has ones, this is such an
9018 assignment. Compute the position and length from C1. Shift OTHER
9019 to the appropriate position, force it to the required mode, and
9020 make the extraction. Check for the AND in both operands. */
9022 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9023 return x;
9025 rhs = expand_compound_operation (XEXP (src, 0));
9026 lhs = expand_compound_operation (XEXP (src, 1));
9028 if (GET_CODE (rhs) == AND
9029 && CONST_INT_P (XEXP (rhs, 1))
9030 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9031 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9032 else if (GET_CODE (lhs) == AND
9033 && CONST_INT_P (XEXP (lhs, 1))
9034 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9035 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9036 else
9037 return x;
9039 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9040 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9041 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9042 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9043 return x;
9045 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9046 if (assign == 0)
9047 return x;
9049 /* The mode to use for the source is the mode of the assignment, or of
9050 what is inside a possible STRICT_LOW_PART. */
9051 mode = (GET_CODE (assign) == STRICT_LOW_PART
9052 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9054 /* Shift OTHER right POS places and make it the source, restricting it
9055 to the proper length and mode. */
9057 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9058 GET_MODE (src),
9059 other, pos),
9060 dest);
9061 src = force_to_mode (src, mode,
9062 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9063 ? ~(unsigned HOST_WIDE_INT) 0
9064 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9067 /* If SRC is masked by an AND that does not make a difference in
9068 the value being stored, strip it. */
9069 if (GET_CODE (assign) == ZERO_EXTRACT
9070 && CONST_INT_P (XEXP (assign, 1))
9071 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9072 && GET_CODE (src) == AND
9073 && CONST_INT_P (XEXP (src, 1))
9074 && UINTVAL (XEXP (src, 1))
9075 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9076 src = XEXP (src, 0);
9078 return gen_rtx_SET (VOIDmode, assign, src);
9081 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9082 if so. */
9084 static rtx
9085 apply_distributive_law (rtx x)
9087 enum rtx_code code = GET_CODE (x);
9088 enum rtx_code inner_code;
9089 rtx lhs, rhs, other;
9090 rtx tem;
9092 /* Distributivity is not true for floating point as it can change the
9093 value. So we don't do it unless -funsafe-math-optimizations. */
9094 if (FLOAT_MODE_P (GET_MODE (x))
9095 && ! flag_unsafe_math_optimizations)
9096 return x;
9098 /* The outer operation can only be one of the following: */
9099 if (code != IOR && code != AND && code != XOR
9100 && code != PLUS && code != MINUS)
9101 return x;
9103 lhs = XEXP (x, 0);
9104 rhs = XEXP (x, 1);
9106 /* If either operand is a primitive we can't do anything, so get out
9107 fast. */
9108 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9109 return x;
9111 lhs = expand_compound_operation (lhs);
9112 rhs = expand_compound_operation (rhs);
9113 inner_code = GET_CODE (lhs);
9114 if (inner_code != GET_CODE (rhs))
9115 return x;
9117 /* See if the inner and outer operations distribute. */
9118 switch (inner_code)
9120 case LSHIFTRT:
9121 case ASHIFTRT:
9122 case AND:
9123 case IOR:
9124 /* These all distribute except over PLUS. */
9125 if (code == PLUS || code == MINUS)
9126 return x;
9127 break;
9129 case MULT:
9130 if (code != PLUS && code != MINUS)
9131 return x;
9132 break;
9134 case ASHIFT:
9135 /* This is also a multiply, so it distributes over everything. */
9136 break;
9138 /* This used to handle SUBREG, but this turned out to be counter-
9139 productive, since (subreg (op ...)) usually is not handled by
9140 insn patterns, and this "optimization" therefore transformed
9141 recognizable patterns into unrecognizable ones. Therefore the
9142 SUBREG case was removed from here.
9144 It is possible that distributing SUBREG over arithmetic operations
9145 leads to an intermediate result than can then be optimized further,
9146 e.g. by moving the outer SUBREG to the other side of a SET as done
9147 in simplify_set. This seems to have been the original intent of
9148 handling SUBREGs here.
9150 However, with current GCC this does not appear to actually happen,
9151 at least on major platforms. If some case is found where removing
9152 the SUBREG case here prevents follow-on optimizations, distributing
9153 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9155 default:
9156 return x;
9159 /* Set LHS and RHS to the inner operands (A and B in the example
9160 above) and set OTHER to the common operand (C in the example).
9161 There is only one way to do this unless the inner operation is
9162 commutative. */
9163 if (COMMUTATIVE_ARITH_P (lhs)
9164 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9165 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9166 else if (COMMUTATIVE_ARITH_P (lhs)
9167 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9168 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9169 else if (COMMUTATIVE_ARITH_P (lhs)
9170 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9171 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9172 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9173 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9174 else
9175 return x;
9177 /* Form the new inner operation, seeing if it simplifies first. */
9178 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9180 /* There is one exception to the general way of distributing:
9181 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9182 if (code == XOR && inner_code == IOR)
9184 inner_code = AND;
9185 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9188 /* We may be able to continuing distributing the result, so call
9189 ourselves recursively on the inner operation before forming the
9190 outer operation, which we return. */
9191 return simplify_gen_binary (inner_code, GET_MODE (x),
9192 apply_distributive_law (tem), other);
9195 /* See if X is of the form (* (+ A B) C), and if so convert to
9196 (+ (* A C) (* B C)) and try to simplify.
9198 Most of the time, this results in no change. However, if some of
9199 the operands are the same or inverses of each other, simplifications
9200 will result.
9202 For example, (and (ior A B) (not B)) can occur as the result of
9203 expanding a bit field assignment. When we apply the distributive
9204 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9205 which then simplifies to (and (A (not B))).
9207 Note that no checks happen on the validity of applying the inverse
9208 distributive law. This is pointless since we can do it in the
9209 few places where this routine is called.
9211 N is the index of the term that is decomposed (the arithmetic operation,
9212 i.e. (+ A B) in the first example above). !N is the index of the term that
9213 is distributed, i.e. of C in the first example above. */
9214 static rtx
9215 distribute_and_simplify_rtx (rtx x, int n)
9217 enum machine_mode mode;
9218 enum rtx_code outer_code, inner_code;
9219 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9221 /* Distributivity is not true for floating point as it can change the
9222 value. So we don't do it unless -funsafe-math-optimizations. */
9223 if (FLOAT_MODE_P (GET_MODE (x))
9224 && ! flag_unsafe_math_optimizations)
9225 return NULL_RTX;
9227 decomposed = XEXP (x, n);
9228 if (!ARITHMETIC_P (decomposed))
9229 return NULL_RTX;
9231 mode = GET_MODE (x);
9232 outer_code = GET_CODE (x);
9233 distributed = XEXP (x, !n);
9235 inner_code = GET_CODE (decomposed);
9236 inner_op0 = XEXP (decomposed, 0);
9237 inner_op1 = XEXP (decomposed, 1);
9239 /* Special case (and (xor B C) (not A)), which is equivalent to
9240 (xor (ior A B) (ior A C)) */
9241 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9243 distributed = XEXP (distributed, 0);
9244 outer_code = IOR;
9247 if (n == 0)
9249 /* Distribute the second term. */
9250 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9251 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9253 else
9255 /* Distribute the first term. */
9256 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9257 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9260 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9261 new_op0, new_op1));
9262 if (GET_CODE (tmp) != outer_code
9263 && (set_src_cost (tmp, optimize_this_for_speed_p)
9264 < set_src_cost (x, optimize_this_for_speed_p)))
9265 return tmp;
9267 return NULL_RTX;
9270 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9271 in MODE. Return an equivalent form, if different from (and VAROP
9272 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9274 static rtx
9275 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9276 unsigned HOST_WIDE_INT constop)
9278 unsigned HOST_WIDE_INT nonzero;
9279 unsigned HOST_WIDE_INT orig_constop;
9280 rtx orig_varop;
9281 int i;
9283 orig_varop = varop;
9284 orig_constop = constop;
9285 if (GET_CODE (varop) == CLOBBER)
9286 return NULL_RTX;
9288 /* Simplify VAROP knowing that we will be only looking at some of the
9289 bits in it.
9291 Note by passing in CONSTOP, we guarantee that the bits not set in
9292 CONSTOP are not significant and will never be examined. We must
9293 ensure that is the case by explicitly masking out those bits
9294 before returning. */
9295 varop = force_to_mode (varop, mode, constop, 0);
9297 /* If VAROP is a CLOBBER, we will fail so return it. */
9298 if (GET_CODE (varop) == CLOBBER)
9299 return varop;
9301 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9302 to VAROP and return the new constant. */
9303 if (CONST_INT_P (varop))
9304 return gen_int_mode (INTVAL (varop) & constop, mode);
9306 /* See what bits may be nonzero in VAROP. Unlike the general case of
9307 a call to nonzero_bits, here we don't care about bits outside
9308 MODE. */
9310 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9312 /* Turn off all bits in the constant that are known to already be zero.
9313 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9314 which is tested below. */
9316 constop &= nonzero;
9318 /* If we don't have any bits left, return zero. */
9319 if (constop == 0)
9320 return const0_rtx;
9322 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9323 a power of two, we can replace this with an ASHIFT. */
9324 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9325 && (i = exact_log2 (constop)) >= 0)
9326 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9328 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9329 or XOR, then try to apply the distributive law. This may eliminate
9330 operations if either branch can be simplified because of the AND.
9331 It may also make some cases more complex, but those cases probably
9332 won't match a pattern either with or without this. */
9334 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9335 return
9336 gen_lowpart
9337 (mode,
9338 apply_distributive_law
9339 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9340 simplify_and_const_int (NULL_RTX,
9341 GET_MODE (varop),
9342 XEXP (varop, 0),
9343 constop),
9344 simplify_and_const_int (NULL_RTX,
9345 GET_MODE (varop),
9346 XEXP (varop, 1),
9347 constop))));
9349 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9350 the AND and see if one of the operands simplifies to zero. If so, we
9351 may eliminate it. */
9353 if (GET_CODE (varop) == PLUS
9354 && exact_log2 (constop + 1) >= 0)
9356 rtx o0, o1;
9358 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9359 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9360 if (o0 == const0_rtx)
9361 return o1;
9362 if (o1 == const0_rtx)
9363 return o0;
9366 /* Make a SUBREG if necessary. If we can't make it, fail. */
9367 varop = gen_lowpart (mode, varop);
9368 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9369 return NULL_RTX;
9371 /* If we are only masking insignificant bits, return VAROP. */
9372 if (constop == nonzero)
9373 return varop;
9375 if (varop == orig_varop && constop == orig_constop)
9376 return NULL_RTX;
9378 /* Otherwise, return an AND. */
9379 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9383 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9384 in MODE.
9386 Return an equivalent form, if different from X. Otherwise, return X. If
9387 X is zero, we are to always construct the equivalent form. */
9389 static rtx
9390 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9391 unsigned HOST_WIDE_INT constop)
9393 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9394 if (tem)
9395 return tem;
9397 if (!x)
9398 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9399 gen_int_mode (constop, mode));
9400 if (GET_MODE (x) != mode)
9401 x = gen_lowpart (mode, x);
9402 return x;
9405 /* Given a REG, X, compute which bits in X can be nonzero.
9406 We don't care about bits outside of those defined in MODE.
9408 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9409 a shift, AND, or zero_extract, we can do better. */
9411 static rtx
9412 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9413 const_rtx known_x ATTRIBUTE_UNUSED,
9414 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9415 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9416 unsigned HOST_WIDE_INT *nonzero)
9418 rtx tem;
9419 reg_stat_type *rsp;
9421 /* If X is a register whose nonzero bits value is current, use it.
9422 Otherwise, if X is a register whose value we can find, use that
9423 value. Otherwise, use the previously-computed global nonzero bits
9424 for this register. */
9426 rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
9427 if (rsp->last_set_value != 0
9428 && (rsp->last_set_mode == mode
9429 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9430 && GET_MODE_CLASS (mode) == MODE_INT))
9431 && ((rsp->last_set_label >= label_tick_ebb_start
9432 && rsp->last_set_label < label_tick)
9433 || (rsp->last_set_label == label_tick
9434 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9435 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9436 && REG_N_SETS (REGNO (x)) == 1
9437 && !REGNO_REG_SET_P
9438 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9440 *nonzero &= rsp->last_set_nonzero_bits;
9441 return NULL;
9444 tem = get_last_value (x);
9446 if (tem)
9448 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9449 /* If X is narrower than MODE and TEM is a non-negative
9450 constant that would appear negative in the mode of X,
9451 sign-extend it for use in reg_nonzero_bits because some
9452 machines (maybe most) will actually do the sign-extension
9453 and this is the conservative approach.
9455 ??? For 2.5, try to tighten up the MD files in this regard
9456 instead of this kludge. */
9458 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9459 && CONST_INT_P (tem)
9460 && INTVAL (tem) > 0
9461 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9462 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9463 #endif
9464 return tem;
9466 else if (nonzero_sign_valid && rsp->nonzero_bits)
9468 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9470 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9471 /* We don't know anything about the upper bits. */
9472 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9473 *nonzero &= mask;
9476 return NULL;
9479 /* Return the number of bits at the high-order end of X that are known to
9480 be equal to the sign bit. X will be used in mode MODE; if MODE is
9481 VOIDmode, X will be used in its own mode. The returned value will always
9482 be between 1 and the number of bits in MODE. */
9484 static rtx
9485 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9486 const_rtx known_x ATTRIBUTE_UNUSED,
9487 enum machine_mode known_mode
9488 ATTRIBUTE_UNUSED,
9489 unsigned int known_ret ATTRIBUTE_UNUSED,
9490 unsigned int *result)
9492 rtx tem;
9493 reg_stat_type *rsp;
9495 rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
9496 if (rsp->last_set_value != 0
9497 && rsp->last_set_mode == mode
9498 && ((rsp->last_set_label >= label_tick_ebb_start
9499 && rsp->last_set_label < label_tick)
9500 || (rsp->last_set_label == label_tick
9501 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9502 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9503 && REG_N_SETS (REGNO (x)) == 1
9504 && !REGNO_REG_SET_P
9505 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9507 *result = rsp->last_set_sign_bit_copies;
9508 return NULL;
9511 tem = get_last_value (x);
9512 if (tem != 0)
9513 return tem;
9515 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9516 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9517 *result = rsp->sign_bit_copies;
9519 return NULL;
9522 /* Return the number of "extended" bits there are in X, when interpreted
9523 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9524 unsigned quantities, this is the number of high-order zero bits.
9525 For signed quantities, this is the number of copies of the sign bit
9526 minus 1. In both case, this function returns the number of "spare"
9527 bits. For example, if two quantities for which this function returns
9528 at least 1 are added, the addition is known not to overflow.
9530 This function will always return 0 unless called during combine, which
9531 implies that it must be called from a define_split. */
9533 unsigned int
9534 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9536 if (nonzero_sign_valid == 0)
9537 return 0;
9539 return (unsignedp
9540 ? (HWI_COMPUTABLE_MODE_P (mode)
9541 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9542 - floor_log2 (nonzero_bits (x, mode)))
9543 : 0)
9544 : num_sign_bit_copies (x, mode) - 1);
9547 /* This function is called from `simplify_shift_const' to merge two
9548 outer operations. Specifically, we have already found that we need
9549 to perform operation *POP0 with constant *PCONST0 at the outermost
9550 position. We would now like to also perform OP1 with constant CONST1
9551 (with *POP0 being done last).
9553 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9554 the resulting operation. *PCOMP_P is set to 1 if we would need to
9555 complement the innermost operand, otherwise it is unchanged.
9557 MODE is the mode in which the operation will be done. No bits outside
9558 the width of this mode matter. It is assumed that the width of this mode
9559 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9561 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9562 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9563 result is simply *PCONST0.
9565 If the resulting operation cannot be expressed as one operation, we
9566 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9568 static int
9569 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)
9571 enum rtx_code op0 = *pop0;
9572 HOST_WIDE_INT const0 = *pconst0;
9574 const0 &= GET_MODE_MASK (mode);
9575 const1 &= GET_MODE_MASK (mode);
9577 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9578 if (op0 == AND)
9579 const1 &= const0;
9581 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9582 if OP0 is SET. */
9584 if (op1 == UNKNOWN || op0 == SET)
9585 return 1;
9587 else if (op0 == UNKNOWN)
9588 op0 = op1, const0 = const1;
9590 else if (op0 == op1)
9592 switch (op0)
9594 case AND:
9595 const0 &= const1;
9596 break;
9597 case IOR:
9598 const0 |= const1;
9599 break;
9600 case XOR:
9601 const0 ^= const1;
9602 break;
9603 case PLUS:
9604 const0 += const1;
9605 break;
9606 case NEG:
9607 op0 = UNKNOWN;
9608 break;
9609 default:
9610 break;
9614 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9615 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9616 return 0;
9618 /* If the two constants aren't the same, we can't do anything. The
9619 remaining six cases can all be done. */
9620 else if (const0 != const1)
9621 return 0;
9623 else
9624 switch (op0)
9626 case IOR:
9627 if (op1 == AND)
9628 /* (a & b) | b == b */
9629 op0 = SET;
9630 else /* op1 == XOR */
9631 /* (a ^ b) | b == a | b */
9633 break;
9635 case XOR:
9636 if (op1 == AND)
9637 /* (a & b) ^ b == (~a) & b */
9638 op0 = AND, *pcomp_p = 1;
9639 else /* op1 == IOR */
9640 /* (a | b) ^ b == a & ~b */
9641 op0 = AND, const0 = ~const0;
9642 break;
9644 case AND:
9645 if (op1 == IOR)
9646 /* (a | b) & b == b */
9647 op0 = SET;
9648 else /* op1 == XOR */
9649 /* (a ^ b) & b) == (~a) & b */
9650 *pcomp_p = 1;
9651 break;
9652 default:
9653 break;
9656 /* Check for NO-OP cases. */
9657 const0 &= GET_MODE_MASK (mode);
9658 if (const0 == 0
9659 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9660 op0 = UNKNOWN;
9661 else if (const0 == 0 && op0 == AND)
9662 op0 = SET;
9663 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9664 && op0 == AND)
9665 op0 = UNKNOWN;
9667 *pop0 = op0;
9669 /* ??? Slightly redundant with the above mask, but not entirely.
9670 Moving this above means we'd have to sign-extend the mode mask
9671 for the final test. */
9672 if (op0 != UNKNOWN && op0 != NEG)
9673 *pconst0 = trunc_int_for_mode (const0, mode);
9675 return 1;
9678 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9679 the shift in. The original shift operation CODE is performed on OP in
9680 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9681 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9682 result of the shift is subject to operation OUTER_CODE with operand
9683 OUTER_CONST. */
9685 static enum machine_mode
9686 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9687 enum machine_mode orig_mode, enum machine_mode mode,
9688 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9690 if (orig_mode == mode)
9691 return mode;
9692 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9694 /* In general we can't perform in wider mode for right shift and rotate. */
9695 switch (code)
9697 case ASHIFTRT:
9698 /* We can still widen if the bits brought in from the left are identical
9699 to the sign bit of ORIG_MODE. */
9700 if (num_sign_bit_copies (op, mode)
9701 > (unsigned) (GET_MODE_PRECISION (mode)
9702 - GET_MODE_PRECISION (orig_mode)))
9703 return mode;
9704 return orig_mode;
9706 case LSHIFTRT:
9707 /* Similarly here but with zero bits. */
9708 if (HWI_COMPUTABLE_MODE_P (mode)
9709 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9710 return mode;
9712 /* We can also widen if the bits brought in will be masked off. This
9713 operation is performed in ORIG_MODE. */
9714 if (outer_code == AND)
9716 int care_bits = low_bitmask_len (orig_mode, outer_const);
9718 if (care_bits >= 0
9719 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9720 return mode;
9722 /* fall through */
9724 case ROTATE:
9725 return orig_mode;
9727 case ROTATERT:
9728 gcc_unreachable ();
9730 default:
9731 return mode;
9735 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9736 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9737 if we cannot simplify it. Otherwise, return a simplified value.
9739 The shift is normally computed in the widest mode we find in VAROP, as
9740 long as it isn't a different number of words than RESULT_MODE. Exceptions
9741 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9743 static rtx
9744 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9745 rtx varop, int orig_count)
9747 enum rtx_code orig_code = code;
9748 rtx orig_varop = varop;
9749 int count;
9750 enum machine_mode mode = result_mode;
9751 enum machine_mode shift_mode, tmode;
9752 unsigned int mode_words
9753 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9754 /* We form (outer_op (code varop count) (outer_const)). */
9755 enum rtx_code outer_op = UNKNOWN;
9756 HOST_WIDE_INT outer_const = 0;
9757 int complement_p = 0;
9758 rtx new_rtx, x;
9760 /* Make sure and truncate the "natural" shift on the way in. We don't
9761 want to do this inside the loop as it makes it more difficult to
9762 combine shifts. */
9763 if (SHIFT_COUNT_TRUNCATED)
9764 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9766 /* If we were given an invalid count, don't do anything except exactly
9767 what was requested. */
9769 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9770 return NULL_RTX;
9772 count = orig_count;
9774 /* Unless one of the branches of the `if' in this loop does a `continue',
9775 we will `break' the loop after the `if'. */
9777 while (count != 0)
9779 /* If we have an operand of (clobber (const_int 0)), fail. */
9780 if (GET_CODE (varop) == CLOBBER)
9781 return NULL_RTX;
9783 /* Convert ROTATERT to ROTATE. */
9784 if (code == ROTATERT)
9786 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9787 code = ROTATE;
9788 if (VECTOR_MODE_P (result_mode))
9789 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9790 else
9791 count = bitsize - count;
9794 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9795 mode, outer_op, outer_const);
9797 /* Handle cases where the count is greater than the size of the mode
9798 minus 1. For ASHIFT, use the size minus one as the count (this can
9799 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9800 take the count modulo the size. For other shifts, the result is
9801 zero.
9803 Since these shifts are being produced by the compiler by combining
9804 multiple operations, each of which are defined, we know what the
9805 result is supposed to be. */
9807 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
9809 if (code == ASHIFTRT)
9810 count = GET_MODE_PRECISION (shift_mode) - 1;
9811 else if (code == ROTATE || code == ROTATERT)
9812 count %= GET_MODE_PRECISION (shift_mode);
9813 else
9815 /* We can't simply return zero because there may be an
9816 outer op. */
9817 varop = const0_rtx;
9818 count = 0;
9819 break;
9823 /* If we discovered we had to complement VAROP, leave. Making a NOT
9824 here would cause an infinite loop. */
9825 if (complement_p)
9826 break;
9828 /* An arithmetic right shift of a quantity known to be -1 or 0
9829 is a no-op. */
9830 if (code == ASHIFTRT
9831 && (num_sign_bit_copies (varop, shift_mode)
9832 == GET_MODE_PRECISION (shift_mode)))
9834 count = 0;
9835 break;
9838 /* If we are doing an arithmetic right shift and discarding all but
9839 the sign bit copies, this is equivalent to doing a shift by the
9840 bitsize minus one. Convert it into that shift because it will often
9841 allow other simplifications. */
9843 if (code == ASHIFTRT
9844 && (count + num_sign_bit_copies (varop, shift_mode)
9845 >= GET_MODE_PRECISION (shift_mode)))
9846 count = GET_MODE_PRECISION (shift_mode) - 1;
9848 /* We simplify the tests below and elsewhere by converting
9849 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9850 `make_compound_operation' will convert it to an ASHIFTRT for
9851 those machines (such as VAX) that don't have an LSHIFTRT. */
9852 if (code == ASHIFTRT
9853 && val_signbit_known_clear_p (shift_mode,
9854 nonzero_bits (varop, shift_mode)))
9855 code = LSHIFTRT;
9857 if (((code == LSHIFTRT
9858 && HWI_COMPUTABLE_MODE_P (shift_mode)
9859 && !(nonzero_bits (varop, shift_mode) >> count))
9860 || (code == ASHIFT
9861 && HWI_COMPUTABLE_MODE_P (shift_mode)
9862 && !((nonzero_bits (varop, shift_mode) << count)
9863 & GET_MODE_MASK (shift_mode))))
9864 && !side_effects_p (varop))
9865 varop = const0_rtx;
9867 switch (GET_CODE (varop))
9869 case SIGN_EXTEND:
9870 case ZERO_EXTEND:
9871 case SIGN_EXTRACT:
9872 case ZERO_EXTRACT:
9873 new_rtx = expand_compound_operation (varop);
9874 if (new_rtx != varop)
9876 varop = new_rtx;
9877 continue;
9879 break;
9881 case MEM:
9882 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9883 minus the width of a smaller mode, we can do this with a
9884 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9885 if ((code == ASHIFTRT || code == LSHIFTRT)
9886 && ! mode_dependent_address_p (XEXP (varop, 0))
9887 && ! MEM_VOLATILE_P (varop)
9888 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9889 MODE_INT, 1)) != BLKmode)
9891 new_rtx = adjust_address_nv (varop, tmode,
9892 BYTES_BIG_ENDIAN ? 0
9893 : count / BITS_PER_UNIT);
9895 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9896 : ZERO_EXTEND, mode, new_rtx);
9897 count = 0;
9898 continue;
9900 break;
9902 case SUBREG:
9903 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9904 the same number of words as what we've seen so far. Then store
9905 the widest mode in MODE. */
9906 if (subreg_lowpart_p (varop)
9907 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9908 > GET_MODE_SIZE (GET_MODE (varop)))
9909 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9910 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9911 == mode_words
9912 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
9913 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
9915 varop = SUBREG_REG (varop);
9916 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9917 mode = GET_MODE (varop);
9918 continue;
9920 break;
9922 case MULT:
9923 /* Some machines use MULT instead of ASHIFT because MULT
9924 is cheaper. But it is still better on those machines to
9925 merge two shifts into one. */
9926 if (CONST_INT_P (XEXP (varop, 1))
9927 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9929 varop
9930 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9931 XEXP (varop, 0),
9932 GEN_INT (exact_log2 (
9933 UINTVAL (XEXP (varop, 1)))));
9934 continue;
9936 break;
9938 case UDIV:
9939 /* Similar, for when divides are cheaper. */
9940 if (CONST_INT_P (XEXP (varop, 1))
9941 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9943 varop
9944 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9945 XEXP (varop, 0),
9946 GEN_INT (exact_log2 (
9947 UINTVAL (XEXP (varop, 1)))));
9948 continue;
9950 break;
9952 case ASHIFTRT:
9953 /* If we are extracting just the sign bit of an arithmetic
9954 right shift, that shift is not needed. However, the sign
9955 bit of a wider mode may be different from what would be
9956 interpreted as the sign bit in a narrower mode, so, if
9957 the result is narrower, don't discard the shift. */
9958 if (code == LSHIFTRT
9959 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9960 && (GET_MODE_BITSIZE (result_mode)
9961 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9963 varop = XEXP (varop, 0);
9964 continue;
9967 /* ... fall through ... */
9969 case LSHIFTRT:
9970 case ASHIFT:
9971 case ROTATE:
9972 /* Here we have two nested shifts. The result is usually the
9973 AND of a new shift with a mask. We compute the result below. */
9974 if (CONST_INT_P (XEXP (varop, 1))
9975 && INTVAL (XEXP (varop, 1)) >= 0
9976 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
9977 && HWI_COMPUTABLE_MODE_P (result_mode)
9978 && HWI_COMPUTABLE_MODE_P (mode)
9979 && !VECTOR_MODE_P (result_mode))
9981 enum rtx_code first_code = GET_CODE (varop);
9982 unsigned int first_count = INTVAL (XEXP (varop, 1));
9983 unsigned HOST_WIDE_INT mask;
9984 rtx mask_rtx;
9986 /* We have one common special case. We can't do any merging if
9987 the inner code is an ASHIFTRT of a smaller mode. However, if
9988 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9989 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9990 we can convert it to
9991 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
9992 This simplifies certain SIGN_EXTEND operations. */
9993 if (code == ASHIFT && first_code == ASHIFTRT
9994 && count == (GET_MODE_PRECISION (result_mode)
9995 - GET_MODE_PRECISION (GET_MODE (varop))))
9997 /* C3 has the low-order C1 bits zero. */
9999 mask = GET_MODE_MASK (mode)
10000 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10002 varop = simplify_and_const_int (NULL_RTX, result_mode,
10003 XEXP (varop, 0), mask);
10004 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10005 varop, count);
10006 count = first_count;
10007 code = ASHIFTRT;
10008 continue;
10011 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10012 than C1 high-order bits equal to the sign bit, we can convert
10013 this to either an ASHIFT or an ASHIFTRT depending on the
10014 two counts.
10016 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10018 if (code == ASHIFTRT && first_code == ASHIFT
10019 && GET_MODE (varop) == shift_mode
10020 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10021 > first_count))
10023 varop = XEXP (varop, 0);
10024 count -= first_count;
10025 if (count < 0)
10027 count = -count;
10028 code = ASHIFT;
10031 continue;
10034 /* There are some cases we can't do. If CODE is ASHIFTRT,
10035 we can only do this if FIRST_CODE is also ASHIFTRT.
10037 We can't do the case when CODE is ROTATE and FIRST_CODE is
10038 ASHIFTRT.
10040 If the mode of this shift is not the mode of the outer shift,
10041 we can't do this if either shift is a right shift or ROTATE.
10043 Finally, we can't do any of these if the mode is too wide
10044 unless the codes are the same.
10046 Handle the case where the shift codes are the same
10047 first. */
10049 if (code == first_code)
10051 if (GET_MODE (varop) != result_mode
10052 && (code == ASHIFTRT || code == LSHIFTRT
10053 || code == ROTATE))
10054 break;
10056 count += first_count;
10057 varop = XEXP (varop, 0);
10058 continue;
10061 if (code == ASHIFTRT
10062 || (code == ROTATE && first_code == ASHIFTRT)
10063 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10064 || (GET_MODE (varop) != result_mode
10065 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10066 || first_code == ROTATE
10067 || code == ROTATE)))
10068 break;
10070 /* To compute the mask to apply after the shift, shift the
10071 nonzero bits of the inner shift the same way the
10072 outer shift will. */
10074 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10076 mask_rtx
10077 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10078 GEN_INT (count));
10080 /* Give up if we can't compute an outer operation to use. */
10081 if (mask_rtx == 0
10082 || !CONST_INT_P (mask_rtx)
10083 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10084 INTVAL (mask_rtx),
10085 result_mode, &complement_p))
10086 break;
10088 /* If the shifts are in the same direction, we add the
10089 counts. Otherwise, we subtract them. */
10090 if ((code == ASHIFTRT || code == LSHIFTRT)
10091 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10092 count += first_count;
10093 else
10094 count -= first_count;
10096 /* If COUNT is positive, the new shift is usually CODE,
10097 except for the two exceptions below, in which case it is
10098 FIRST_CODE. If the count is negative, FIRST_CODE should
10099 always be used */
10100 if (count > 0
10101 && ((first_code == ROTATE && code == ASHIFT)
10102 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10103 code = first_code;
10104 else if (count < 0)
10105 code = first_code, count = -count;
10107 varop = XEXP (varop, 0);
10108 continue;
10111 /* If we have (A << B << C) for any shift, we can convert this to
10112 (A << C << B). This wins if A is a constant. Only try this if
10113 B is not a constant. */
10115 else if (GET_CODE (varop) == code
10116 && CONST_INT_P (XEXP (varop, 0))
10117 && !CONST_INT_P (XEXP (varop, 1)))
10119 rtx new_rtx = simplify_const_binary_operation (code, mode,
10120 XEXP (varop, 0),
10121 GEN_INT (count));
10122 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10123 count = 0;
10124 continue;
10126 break;
10128 case NOT:
10129 if (VECTOR_MODE_P (mode))
10130 break;
10132 /* Make this fit the case below. */
10133 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10134 continue;
10136 case IOR:
10137 case AND:
10138 case XOR:
10139 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10140 with C the size of VAROP - 1 and the shift is logical if
10141 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10142 we have an (le X 0) operation. If we have an arithmetic shift
10143 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10144 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10146 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10147 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10148 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10149 && (code == LSHIFTRT || code == ASHIFTRT)
10150 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10151 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10153 count = 0;
10154 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10155 const0_rtx);
10157 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10158 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10160 continue;
10163 /* If we have (shift (logical)), move the logical to the outside
10164 to allow it to possibly combine with another logical and the
10165 shift to combine with another shift. This also canonicalizes to
10166 what a ZERO_EXTRACT looks like. Also, some machines have
10167 (and (shift)) insns. */
10169 if (CONST_INT_P (XEXP (varop, 1))
10170 /* We can't do this if we have (ashiftrt (xor)) and the
10171 constant has its sign bit set in shift_mode. */
10172 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10173 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10174 shift_mode))
10175 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10176 XEXP (varop, 1),
10177 GEN_INT (count))) != 0
10178 && CONST_INT_P (new_rtx)
10179 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10180 INTVAL (new_rtx), result_mode, &complement_p))
10182 varop = XEXP (varop, 0);
10183 continue;
10186 /* If we can't do that, try to simplify the shift in each arm of the
10187 logical expression, make a new logical expression, and apply
10188 the inverse distributive law. This also can't be done
10189 for some (ashiftrt (xor)). */
10190 if (CONST_INT_P (XEXP (varop, 1))
10191 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10192 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10193 shift_mode)))
10195 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10196 XEXP (varop, 0), count);
10197 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10198 XEXP (varop, 1), count);
10200 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10201 lhs, rhs);
10202 varop = apply_distributive_law (varop);
10204 count = 0;
10205 continue;
10207 break;
10209 case EQ:
10210 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10211 says that the sign bit can be tested, FOO has mode MODE, C is
10212 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10213 that may be nonzero. */
10214 if (code == LSHIFTRT
10215 && XEXP (varop, 1) == const0_rtx
10216 && GET_MODE (XEXP (varop, 0)) == result_mode
10217 && count == (GET_MODE_PRECISION (result_mode) - 1)
10218 && HWI_COMPUTABLE_MODE_P (result_mode)
10219 && STORE_FLAG_VALUE == -1
10220 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10221 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10222 &complement_p))
10224 varop = XEXP (varop, 0);
10225 count = 0;
10226 continue;
10228 break;
10230 case NEG:
10231 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10232 than the number of bits in the mode is equivalent to A. */
10233 if (code == LSHIFTRT
10234 && count == (GET_MODE_PRECISION (result_mode) - 1)
10235 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10237 varop = XEXP (varop, 0);
10238 count = 0;
10239 continue;
10242 /* NEG commutes with ASHIFT since it is multiplication. Move the
10243 NEG outside to allow shifts to combine. */
10244 if (code == ASHIFT
10245 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10246 &complement_p))
10248 varop = XEXP (varop, 0);
10249 continue;
10251 break;
10253 case PLUS:
10254 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10255 is one less than the number of bits in the mode is
10256 equivalent to (xor A 1). */
10257 if (code == LSHIFTRT
10258 && count == (GET_MODE_PRECISION (result_mode) - 1)
10259 && XEXP (varop, 1) == constm1_rtx
10260 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10261 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10262 &complement_p))
10264 count = 0;
10265 varop = XEXP (varop, 0);
10266 continue;
10269 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10270 that might be nonzero in BAR are those being shifted out and those
10271 bits are known zero in FOO, we can replace the PLUS with FOO.
10272 Similarly in the other operand order. This code occurs when
10273 we are computing the size of a variable-size array. */
10275 if ((code == ASHIFTRT || code == LSHIFTRT)
10276 && count < HOST_BITS_PER_WIDE_INT
10277 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10278 && (nonzero_bits (XEXP (varop, 1), result_mode)
10279 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10281 varop = XEXP (varop, 0);
10282 continue;
10284 else if ((code == ASHIFTRT || code == LSHIFTRT)
10285 && count < HOST_BITS_PER_WIDE_INT
10286 && HWI_COMPUTABLE_MODE_P (result_mode)
10287 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10288 >> count)
10289 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10290 & nonzero_bits (XEXP (varop, 1),
10291 result_mode)))
10293 varop = XEXP (varop, 1);
10294 continue;
10297 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10298 if (code == ASHIFT
10299 && CONST_INT_P (XEXP (varop, 1))
10300 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10301 XEXP (varop, 1),
10302 GEN_INT (count))) != 0
10303 && CONST_INT_P (new_rtx)
10304 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10305 INTVAL (new_rtx), result_mode, &complement_p))
10307 varop = XEXP (varop, 0);
10308 continue;
10311 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10312 signbit', and attempt to change the PLUS to an XOR and move it to
10313 the outer operation as is done above in the AND/IOR/XOR case
10314 leg for shift(logical). See details in logical handling above
10315 for reasoning in doing so. */
10316 if (code == LSHIFTRT
10317 && CONST_INT_P (XEXP (varop, 1))
10318 && mode_signbit_p (result_mode, XEXP (varop, 1))
10319 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10320 XEXP (varop, 1),
10321 GEN_INT (count))) != 0
10322 && CONST_INT_P (new_rtx)
10323 && merge_outer_ops (&outer_op, &outer_const, XOR,
10324 INTVAL (new_rtx), result_mode, &complement_p))
10326 varop = XEXP (varop, 0);
10327 continue;
10330 break;
10332 case MINUS:
10333 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10334 with C the size of VAROP - 1 and the shift is logical if
10335 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10336 we have a (gt X 0) operation. If the shift is arithmetic with
10337 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10338 we have a (neg (gt X 0)) operation. */
10340 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10341 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10342 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10343 && (code == LSHIFTRT || code == ASHIFTRT)
10344 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10345 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10346 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10348 count = 0;
10349 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10350 const0_rtx);
10352 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10353 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10355 continue;
10357 break;
10359 case TRUNCATE:
10360 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10361 if the truncate does not affect the value. */
10362 if (code == LSHIFTRT
10363 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10364 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10365 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10366 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10367 - GET_MODE_PRECISION (GET_MODE (varop)))))
10369 rtx varop_inner = XEXP (varop, 0);
10371 varop_inner
10372 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10373 XEXP (varop_inner, 0),
10374 GEN_INT
10375 (count + INTVAL (XEXP (varop_inner, 1))));
10376 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10377 count = 0;
10378 continue;
10380 break;
10382 default:
10383 break;
10386 break;
10389 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10390 outer_op, outer_const);
10392 /* We have now finished analyzing the shift. The result should be
10393 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10394 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10395 to the result of the shift. OUTER_CONST is the relevant constant,
10396 but we must turn off all bits turned off in the shift. */
10398 if (outer_op == UNKNOWN
10399 && orig_code == code && orig_count == count
10400 && varop == orig_varop
10401 && shift_mode == GET_MODE (varop))
10402 return NULL_RTX;
10404 /* Make a SUBREG if necessary. If we can't make it, fail. */
10405 varop = gen_lowpart (shift_mode, varop);
10406 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10407 return NULL_RTX;
10409 /* If we have an outer operation and we just made a shift, it is
10410 possible that we could have simplified the shift were it not
10411 for the outer operation. So try to do the simplification
10412 recursively. */
10414 if (outer_op != UNKNOWN)
10415 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10416 else
10417 x = NULL_RTX;
10419 if (x == NULL_RTX)
10420 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10422 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10423 turn off all the bits that the shift would have turned off. */
10424 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10425 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10426 GET_MODE_MASK (result_mode) >> orig_count);
10428 /* Do the remainder of the processing in RESULT_MODE. */
10429 x = gen_lowpart_or_truncate (result_mode, x);
10431 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10432 operation. */
10433 if (complement_p)
10434 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10436 if (outer_op != UNKNOWN)
10438 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10439 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10440 outer_const = trunc_int_for_mode (outer_const, result_mode);
10442 if (outer_op == AND)
10443 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10444 else if (outer_op == SET)
10446 /* This means that we have determined that the result is
10447 equivalent to a constant. This should be rare. */
10448 if (!side_effects_p (x))
10449 x = GEN_INT (outer_const);
10451 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10452 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10453 else
10454 x = simplify_gen_binary (outer_op, result_mode, x,
10455 GEN_INT (outer_const));
10458 return x;
10461 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10462 The result of the shift is RESULT_MODE. If we cannot simplify it,
10463 return X or, if it is NULL, synthesize the expression with
10464 simplify_gen_binary. Otherwise, return a simplified value.
10466 The shift is normally computed in the widest mode we find in VAROP, as
10467 long as it isn't a different number of words than RESULT_MODE. Exceptions
10468 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10470 static rtx
10471 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10472 rtx varop, int count)
10474 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10475 if (tem)
10476 return tem;
10478 if (!x)
10479 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10480 if (GET_MODE (x) != result_mode)
10481 x = gen_lowpart (result_mode, x);
10482 return x;
10486 /* Like recog, but we receive the address of a pointer to a new pattern.
10487 We try to match the rtx that the pointer points to.
10488 If that fails, we may try to modify or replace the pattern,
10489 storing the replacement into the same pointer object.
10491 Modifications include deletion or addition of CLOBBERs.
10493 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10494 the CLOBBERs are placed.
10496 The value is the final insn code from the pattern ultimately matched,
10497 or -1. */
10499 static int
10500 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10502 rtx pat = *pnewpat;
10503 int insn_code_number;
10504 int num_clobbers_to_add = 0;
10505 int i;
10506 rtx notes = 0;
10507 rtx old_notes, old_pat;
10509 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10510 we use to indicate that something didn't match. If we find such a
10511 thing, force rejection. */
10512 if (GET_CODE (pat) == PARALLEL)
10513 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10514 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10515 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10516 return -1;
10518 old_pat = PATTERN (insn);
10519 old_notes = REG_NOTES (insn);
10520 PATTERN (insn) = pat;
10521 REG_NOTES (insn) = 0;
10523 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10524 if (dump_file && (dump_flags & TDF_DETAILS))
10526 if (insn_code_number < 0)
10527 fputs ("Failed to match this instruction:\n", dump_file);
10528 else
10529 fputs ("Successfully matched this instruction:\n", dump_file);
10530 print_rtl_single (dump_file, pat);
10533 /* If it isn't, there is the possibility that we previously had an insn
10534 that clobbered some register as a side effect, but the combined
10535 insn doesn't need to do that. So try once more without the clobbers
10536 unless this represents an ASM insn. */
10538 if (insn_code_number < 0 && ! check_asm_operands (pat)
10539 && GET_CODE (pat) == PARALLEL)
10541 int pos;
10543 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10544 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10546 if (i != pos)
10547 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10548 pos++;
10551 SUBST_INT (XVECLEN (pat, 0), pos);
10553 if (pos == 1)
10554 pat = XVECEXP (pat, 0, 0);
10556 PATTERN (insn) = pat;
10557 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10558 if (dump_file && (dump_flags & TDF_DETAILS))
10560 if (insn_code_number < 0)
10561 fputs ("Failed to match this instruction:\n", dump_file);
10562 else
10563 fputs ("Successfully matched this instruction:\n", dump_file);
10564 print_rtl_single (dump_file, pat);
10567 PATTERN (insn) = old_pat;
10568 REG_NOTES (insn) = old_notes;
10570 /* Recognize all noop sets, these will be killed by followup pass. */
10571 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10572 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10574 /* If we had any clobbers to add, make a new pattern than contains
10575 them. Then check to make sure that all of them are dead. */
10576 if (num_clobbers_to_add)
10578 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10579 rtvec_alloc (GET_CODE (pat) == PARALLEL
10580 ? (XVECLEN (pat, 0)
10581 + num_clobbers_to_add)
10582 : num_clobbers_to_add + 1));
10584 if (GET_CODE (pat) == PARALLEL)
10585 for (i = 0; i < XVECLEN (pat, 0); i++)
10586 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10587 else
10588 XVECEXP (newpat, 0, 0) = pat;
10590 add_clobbers (newpat, insn_code_number);
10592 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10593 i < XVECLEN (newpat, 0); i++)
10595 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10596 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10597 return -1;
10598 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10600 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10601 notes = alloc_reg_note (REG_UNUSED,
10602 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10605 pat = newpat;
10608 *pnewpat = pat;
10609 *pnotes = notes;
10611 return insn_code_number;
10614 /* Like gen_lowpart_general but for use by combine. In combine it
10615 is not possible to create any new pseudoregs. However, it is
10616 safe to create invalid memory addresses, because combine will
10617 try to recognize them and all they will do is make the combine
10618 attempt fail.
10620 If for some reason this cannot do its job, an rtx
10621 (clobber (const_int 0)) is returned.
10622 An insn containing that will not be recognized. */
10624 static rtx
10625 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10627 enum machine_mode imode = GET_MODE (x);
10628 unsigned int osize = GET_MODE_SIZE (omode);
10629 unsigned int isize = GET_MODE_SIZE (imode);
10630 rtx result;
10632 if (omode == imode)
10633 return x;
10635 /* We can only support MODE being wider than a word if X is a
10636 constant integer or has a mode the same size. */
10637 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10638 && ! ((CONST_INT_P (x) || CONST_DOUBLE_AS_INT_P (x))
10639 || isize == osize))
10640 goto fail;
10642 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10643 won't know what to do. So we will strip off the SUBREG here and
10644 process normally. */
10645 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10647 x = SUBREG_REG (x);
10649 /* For use in case we fall down into the address adjustments
10650 further below, we need to adjust the known mode and size of
10651 x; imode and isize, since we just adjusted x. */
10652 imode = GET_MODE (x);
10654 if (imode == omode)
10655 return x;
10657 isize = GET_MODE_SIZE (imode);
10660 result = gen_lowpart_common (omode, x);
10662 if (result)
10663 return result;
10665 if (MEM_P (x))
10667 int offset = 0;
10669 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10670 address. */
10671 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10672 goto fail;
10674 /* If we want to refer to something bigger than the original memref,
10675 generate a paradoxical subreg instead. That will force a reload
10676 of the original memref X. */
10677 if (isize < osize)
10678 return gen_rtx_SUBREG (omode, x, 0);
10680 if (WORDS_BIG_ENDIAN)
10681 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10683 /* Adjust the address so that the address-after-the-data is
10684 unchanged. */
10685 if (BYTES_BIG_ENDIAN)
10686 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10688 return adjust_address_nv (x, omode, offset);
10691 /* If X is a comparison operator, rewrite it in a new mode. This
10692 probably won't match, but may allow further simplifications. */
10693 else if (COMPARISON_P (x))
10694 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10696 /* If we couldn't simplify X any other way, just enclose it in a
10697 SUBREG. Normally, this SUBREG won't match, but some patterns may
10698 include an explicit SUBREG or we may simplify it further in combine. */
10699 else
10701 int offset = 0;
10702 rtx res;
10704 offset = subreg_lowpart_offset (omode, imode);
10705 if (imode == VOIDmode)
10707 imode = int_mode_for_mode (omode);
10708 x = gen_lowpart_common (imode, x);
10709 if (x == NULL)
10710 goto fail;
10712 res = simplify_gen_subreg (omode, x, imode, offset);
10713 if (res)
10714 return res;
10717 fail:
10718 return gen_rtx_CLOBBER (omode, const0_rtx);
10721 /* Try to simplify a comparison between OP0 and a constant OP1,
10722 where CODE is the comparison code that will be tested, into a
10723 (CODE OP0 const0_rtx) form.
10725 The result is a possibly different comparison code to use.
10726 *POP1 may be updated. */
10728 static enum rtx_code
10729 simplify_compare_const (enum rtx_code code, rtx op0, rtx *pop1)
10731 enum machine_mode mode = GET_MODE (op0);
10732 unsigned int mode_width = GET_MODE_PRECISION (mode);
10733 HOST_WIDE_INT const_op = INTVAL (*pop1);
10735 /* Get the constant we are comparing against and turn off all bits
10736 not on in our mode. */
10737 if (mode != VOIDmode)
10738 const_op = trunc_int_for_mode (const_op, mode);
10740 /* If we are comparing against a constant power of two and the value
10741 being compared can only have that single bit nonzero (e.g., it was
10742 `and'ed with that bit), we can replace this with a comparison
10743 with zero. */
10744 if (const_op
10745 && (code == EQ || code == NE || code == GE || code == GEU
10746 || code == LT || code == LTU)
10747 && mode_width <= HOST_BITS_PER_WIDE_INT
10748 && exact_log2 (const_op) >= 0
10749 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10751 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10752 const_op = 0;
10755 /* Similarly, if we are comparing a value known to be either -1 or
10756 0 with -1, change it to the opposite comparison against zero. */
10757 if (const_op == -1
10758 && (code == EQ || code == NE || code == GT || code == LE
10759 || code == GEU || code == LTU)
10760 && num_sign_bit_copies (op0, mode) == mode_width)
10762 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10763 const_op = 0;
10766 /* Do some canonicalizations based on the comparison code. We prefer
10767 comparisons against zero and then prefer equality comparisons.
10768 If we can reduce the size of a constant, we will do that too. */
10769 switch (code)
10771 case LT:
10772 /* < C is equivalent to <= (C - 1) */
10773 if (const_op > 0)
10775 const_op -= 1;
10776 code = LE;
10777 /* ... fall through to LE case below. */
10779 else
10780 break;
10782 case LE:
10783 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10784 if (const_op < 0)
10786 const_op += 1;
10787 code = LT;
10790 /* If we are doing a <= 0 comparison on a value known to have
10791 a zero sign bit, we can replace this with == 0. */
10792 else if (const_op == 0
10793 && mode_width <= HOST_BITS_PER_WIDE_INT
10794 && (nonzero_bits (op0, mode)
10795 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10796 == 0)
10797 code = EQ;
10798 break;
10800 case GE:
10801 /* >= C is equivalent to > (C - 1). */
10802 if (const_op > 0)
10804 const_op -= 1;
10805 code = GT;
10806 /* ... fall through to GT below. */
10808 else
10809 break;
10811 case GT:
10812 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10813 if (const_op < 0)
10815 const_op += 1;
10816 code = GE;
10819 /* If we are doing a > 0 comparison on a value known to have
10820 a zero sign bit, we can replace this with != 0. */
10821 else if (const_op == 0
10822 && mode_width <= HOST_BITS_PER_WIDE_INT
10823 && (nonzero_bits (op0, mode)
10824 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10825 == 0)
10826 code = NE;
10827 break;
10829 case LTU:
10830 /* < C is equivalent to <= (C - 1). */
10831 if (const_op > 0)
10833 const_op -= 1;
10834 code = LEU;
10835 /* ... fall through ... */
10837 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10838 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10839 && (unsigned HOST_WIDE_INT) const_op
10840 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10842 const_op = 0;
10843 code = GE;
10844 break;
10846 else
10847 break;
10849 case LEU:
10850 /* unsigned <= 0 is equivalent to == 0 */
10851 if (const_op == 0)
10852 code = EQ;
10853 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10854 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10855 && (unsigned HOST_WIDE_INT) const_op
10856 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10858 const_op = 0;
10859 code = GE;
10861 break;
10863 case GEU:
10864 /* >= C is equivalent to > (C - 1). */
10865 if (const_op > 1)
10867 const_op -= 1;
10868 code = GTU;
10869 /* ... fall through ... */
10872 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10873 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10874 && (unsigned HOST_WIDE_INT) const_op
10875 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10877 const_op = 0;
10878 code = LT;
10879 break;
10881 else
10882 break;
10884 case GTU:
10885 /* unsigned > 0 is equivalent to != 0 */
10886 if (const_op == 0)
10887 code = NE;
10888 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10889 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10890 && (unsigned HOST_WIDE_INT) const_op
10891 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10893 const_op = 0;
10894 code = LT;
10896 break;
10898 default:
10899 break;
10902 *pop1 = GEN_INT (const_op);
10903 return code;
10906 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10907 comparison code that will be tested.
10909 The result is a possibly different comparison code to use. *POP0 and
10910 *POP1 may be updated.
10912 It is possible that we might detect that a comparison is either always
10913 true or always false. However, we do not perform general constant
10914 folding in combine, so this knowledge isn't useful. Such tautologies
10915 should have been detected earlier. Hence we ignore all such cases. */
10917 static enum rtx_code
10918 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10920 rtx op0 = *pop0;
10921 rtx op1 = *pop1;
10922 rtx tem, tem1;
10923 int i;
10924 enum machine_mode mode, tmode;
10926 /* Try a few ways of applying the same transformation to both operands. */
10927 while (1)
10929 #ifndef WORD_REGISTER_OPERATIONS
10930 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10931 so check specially. */
10932 if (code != GTU && code != GEU && code != LTU && code != LEU
10933 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10934 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10935 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10936 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10937 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10938 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10939 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10940 && CONST_INT_P (XEXP (op0, 1))
10941 && XEXP (op0, 1) == XEXP (op1, 1)
10942 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10943 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10944 && (INTVAL (XEXP (op0, 1))
10945 == (GET_MODE_PRECISION (GET_MODE (op0))
10946 - (GET_MODE_PRECISION
10947 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10949 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10950 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10952 #endif
10954 /* If both operands are the same constant shift, see if we can ignore the
10955 shift. We can if the shift is a rotate or if the bits shifted out of
10956 this shift are known to be zero for both inputs and if the type of
10957 comparison is compatible with the shift. */
10958 if (GET_CODE (op0) == GET_CODE (op1)
10959 && HWI_COMPUTABLE_MODE_P (GET_MODE(op0))
10960 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10961 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10962 && (code != GT && code != LT && code != GE && code != LE))
10963 || (GET_CODE (op0) == ASHIFTRT
10964 && (code != GTU && code != LTU
10965 && code != GEU && code != LEU)))
10966 && CONST_INT_P (XEXP (op0, 1))
10967 && INTVAL (XEXP (op0, 1)) >= 0
10968 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10969 && XEXP (op0, 1) == XEXP (op1, 1))
10971 enum machine_mode mode = GET_MODE (op0);
10972 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10973 int shift_count = INTVAL (XEXP (op0, 1));
10975 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10976 mask &= (mask >> shift_count) << shift_count;
10977 else if (GET_CODE (op0) == ASHIFT)
10978 mask = (mask & (mask << shift_count)) >> shift_count;
10980 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10981 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10982 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10983 else
10984 break;
10987 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10988 SUBREGs are of the same mode, and, in both cases, the AND would
10989 be redundant if the comparison was done in the narrower mode,
10990 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10991 and the operand's possibly nonzero bits are 0xffffff01; in that case
10992 if we only care about QImode, we don't need the AND). This case
10993 occurs if the output mode of an scc insn is not SImode and
10994 STORE_FLAG_VALUE == 1 (e.g., the 386).
10996 Similarly, check for a case where the AND's are ZERO_EXTEND
10997 operations from some narrower mode even though a SUBREG is not
10998 present. */
11000 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11001 && CONST_INT_P (XEXP (op0, 1))
11002 && CONST_INT_P (XEXP (op1, 1)))
11004 rtx inner_op0 = XEXP (op0, 0);
11005 rtx inner_op1 = XEXP (op1, 0);
11006 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11007 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11008 int changed = 0;
11010 if (paradoxical_subreg_p (inner_op0)
11011 && GET_CODE (inner_op1) == SUBREG
11012 && (GET_MODE (SUBREG_REG (inner_op0))
11013 == GET_MODE (SUBREG_REG (inner_op1)))
11014 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11015 <= HOST_BITS_PER_WIDE_INT)
11016 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11017 GET_MODE (SUBREG_REG (inner_op0)))))
11018 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11019 GET_MODE (SUBREG_REG (inner_op1))))))
11021 op0 = SUBREG_REG (inner_op0);
11022 op1 = SUBREG_REG (inner_op1);
11024 /* The resulting comparison is always unsigned since we masked
11025 off the original sign bit. */
11026 code = unsigned_condition (code);
11028 changed = 1;
11031 else if (c0 == c1)
11032 for (tmode = GET_CLASS_NARROWEST_MODE
11033 (GET_MODE_CLASS (GET_MODE (op0)));
11034 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11035 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11037 op0 = gen_lowpart (tmode, inner_op0);
11038 op1 = gen_lowpart (tmode, inner_op1);
11039 code = unsigned_condition (code);
11040 changed = 1;
11041 break;
11044 if (! changed)
11045 break;
11048 /* If both operands are NOT, we can strip off the outer operation
11049 and adjust the comparison code for swapped operands; similarly for
11050 NEG, except that this must be an equality comparison. */
11051 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11052 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11053 && (code == EQ || code == NE)))
11054 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11056 else
11057 break;
11060 /* If the first operand is a constant, swap the operands and adjust the
11061 comparison code appropriately, but don't do this if the second operand
11062 is already a constant integer. */
11063 if (swap_commutative_operands_p (op0, op1))
11065 tem = op0, op0 = op1, op1 = tem;
11066 code = swap_condition (code);
11069 /* We now enter a loop during which we will try to simplify the comparison.
11070 For the most part, we only are concerned with comparisons with zero,
11071 but some things may really be comparisons with zero but not start
11072 out looking that way. */
11074 while (CONST_INT_P (op1))
11076 enum machine_mode mode = GET_MODE (op0);
11077 unsigned int mode_width = GET_MODE_PRECISION (mode);
11078 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11079 int equality_comparison_p;
11080 int sign_bit_comparison_p;
11081 int unsigned_comparison_p;
11082 HOST_WIDE_INT const_op;
11084 /* We only want to handle integral modes. This catches VOIDmode,
11085 CCmode, and the floating-point modes. An exception is that we
11086 can handle VOIDmode if OP0 is a COMPARE or a comparison
11087 operation. */
11089 if (GET_MODE_CLASS (mode) != MODE_INT
11090 && ! (mode == VOIDmode
11091 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11092 break;
11094 /* Try to simplify the compare to constant, possibly changing the
11095 comparison op, and/or changing op1 to zero. */
11096 code = simplify_compare_const (code, op0, &op1);
11097 const_op = INTVAL (op1);
11099 /* Compute some predicates to simplify code below. */
11101 equality_comparison_p = (code == EQ || code == NE);
11102 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11103 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11104 || code == GEU);
11106 /* If this is a sign bit comparison and we can do arithmetic in
11107 MODE, say that we will only be needing the sign bit of OP0. */
11108 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11109 op0 = force_to_mode (op0, mode,
11110 (unsigned HOST_WIDE_INT) 1
11111 << (GET_MODE_PRECISION (mode) - 1),
11114 /* Now try cases based on the opcode of OP0. If none of the cases
11115 does a "continue", we exit this loop immediately after the
11116 switch. */
11118 switch (GET_CODE (op0))
11120 case ZERO_EXTRACT:
11121 /* If we are extracting a single bit from a variable position in
11122 a constant that has only a single bit set and are comparing it
11123 with zero, we can convert this into an equality comparison
11124 between the position and the location of the single bit. */
11125 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11126 have already reduced the shift count modulo the word size. */
11127 if (!SHIFT_COUNT_TRUNCATED
11128 && CONST_INT_P (XEXP (op0, 0))
11129 && XEXP (op0, 1) == const1_rtx
11130 && equality_comparison_p && const_op == 0
11131 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11133 if (BITS_BIG_ENDIAN)
11135 enum machine_mode new_mode
11136 = mode_for_extraction (EP_extzv, 1);
11137 if (new_mode == MAX_MACHINE_MODE)
11138 i = BITS_PER_WORD - 1 - i;
11139 else
11141 mode = new_mode;
11142 i = (GET_MODE_PRECISION (mode) - 1 - i);
11146 op0 = XEXP (op0, 2);
11147 op1 = GEN_INT (i);
11148 const_op = i;
11150 /* Result is nonzero iff shift count is equal to I. */
11151 code = reverse_condition (code);
11152 continue;
11155 /* ... fall through ... */
11157 case SIGN_EXTRACT:
11158 tem = expand_compound_operation (op0);
11159 if (tem != op0)
11161 op0 = tem;
11162 continue;
11164 break;
11166 case NOT:
11167 /* If testing for equality, we can take the NOT of the constant. */
11168 if (equality_comparison_p
11169 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11171 op0 = XEXP (op0, 0);
11172 op1 = tem;
11173 continue;
11176 /* If just looking at the sign bit, reverse the sense of the
11177 comparison. */
11178 if (sign_bit_comparison_p)
11180 op0 = XEXP (op0, 0);
11181 code = (code == GE ? LT : GE);
11182 continue;
11184 break;
11186 case NEG:
11187 /* If testing for equality, we can take the NEG of the constant. */
11188 if (equality_comparison_p
11189 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11191 op0 = XEXP (op0, 0);
11192 op1 = tem;
11193 continue;
11196 /* The remaining cases only apply to comparisons with zero. */
11197 if (const_op != 0)
11198 break;
11200 /* When X is ABS or is known positive,
11201 (neg X) is < 0 if and only if X != 0. */
11203 if (sign_bit_comparison_p
11204 && (GET_CODE (XEXP (op0, 0)) == ABS
11205 || (mode_width <= HOST_BITS_PER_WIDE_INT
11206 && (nonzero_bits (XEXP (op0, 0), mode)
11207 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11208 == 0)))
11210 op0 = XEXP (op0, 0);
11211 code = (code == LT ? NE : EQ);
11212 continue;
11215 /* If we have NEG of something whose two high-order bits are the
11216 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11217 if (num_sign_bit_copies (op0, mode) >= 2)
11219 op0 = XEXP (op0, 0);
11220 code = swap_condition (code);
11221 continue;
11223 break;
11225 case ROTATE:
11226 /* If we are testing equality and our count is a constant, we
11227 can perform the inverse operation on our RHS. */
11228 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11229 && (tem = simplify_binary_operation (ROTATERT, mode,
11230 op1, XEXP (op0, 1))) != 0)
11232 op0 = XEXP (op0, 0);
11233 op1 = tem;
11234 continue;
11237 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11238 a particular bit. Convert it to an AND of a constant of that
11239 bit. This will be converted into a ZERO_EXTRACT. */
11240 if (const_op == 0 && sign_bit_comparison_p
11241 && CONST_INT_P (XEXP (op0, 1))
11242 && mode_width <= HOST_BITS_PER_WIDE_INT)
11244 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11245 ((unsigned HOST_WIDE_INT) 1
11246 << (mode_width - 1
11247 - INTVAL (XEXP (op0, 1)))));
11248 code = (code == LT ? NE : EQ);
11249 continue;
11252 /* Fall through. */
11254 case ABS:
11255 /* ABS is ignorable inside an equality comparison with zero. */
11256 if (const_op == 0 && equality_comparison_p)
11258 op0 = XEXP (op0, 0);
11259 continue;
11261 break;
11263 case SIGN_EXTEND:
11264 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11265 (compare FOO CONST) if CONST fits in FOO's mode and we
11266 are either testing inequality or have an unsigned
11267 comparison with ZERO_EXTEND or a signed comparison with
11268 SIGN_EXTEND. But don't do it if we don't have a compare
11269 insn of the given mode, since we'd have to revert it
11270 later on, and then we wouldn't know whether to sign- or
11271 zero-extend. */
11272 mode = GET_MODE (XEXP (op0, 0));
11273 if (GET_MODE_CLASS (mode) == MODE_INT
11274 && ! unsigned_comparison_p
11275 && HWI_COMPUTABLE_MODE_P (mode)
11276 && trunc_int_for_mode (const_op, mode) == const_op
11277 && have_insn_for (COMPARE, mode))
11279 op0 = XEXP (op0, 0);
11280 continue;
11282 break;
11284 case SUBREG:
11285 /* Check for the case where we are comparing A - C1 with C2, that is
11287 (subreg:MODE (plus (A) (-C1))) op (C2)
11289 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11290 comparison in the wider mode. One of the following two conditions
11291 must be true in order for this to be valid:
11293 1. The mode extension results in the same bit pattern being added
11294 on both sides and the comparison is equality or unsigned. As
11295 C2 has been truncated to fit in MODE, the pattern can only be
11296 all 0s or all 1s.
11298 2. The mode extension results in the sign bit being copied on
11299 each side.
11301 The difficulty here is that we have predicates for A but not for
11302 (A - C1) so we need to check that C1 is within proper bounds so
11303 as to perturbate A as little as possible. */
11305 if (mode_width <= HOST_BITS_PER_WIDE_INT
11306 && subreg_lowpart_p (op0)
11307 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11308 && GET_CODE (SUBREG_REG (op0)) == PLUS
11309 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11311 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11312 rtx a = XEXP (SUBREG_REG (op0), 0);
11313 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11315 if ((c1 > 0
11316 && (unsigned HOST_WIDE_INT) c1
11317 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11318 && (equality_comparison_p || unsigned_comparison_p)
11319 /* (A - C1) zero-extends if it is positive and sign-extends
11320 if it is negative, C2 both zero- and sign-extends. */
11321 && ((0 == (nonzero_bits (a, inner_mode)
11322 & ~GET_MODE_MASK (mode))
11323 && const_op >= 0)
11324 /* (A - C1) sign-extends if it is positive and 1-extends
11325 if it is negative, C2 both sign- and 1-extends. */
11326 || (num_sign_bit_copies (a, inner_mode)
11327 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11328 - mode_width)
11329 && const_op < 0)))
11330 || ((unsigned HOST_WIDE_INT) c1
11331 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11332 /* (A - C1) always sign-extends, like C2. */
11333 && num_sign_bit_copies (a, inner_mode)
11334 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11335 - (mode_width - 1))))
11337 op0 = SUBREG_REG (op0);
11338 continue;
11342 /* If the inner mode is narrower and we are extracting the low part,
11343 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11344 if (subreg_lowpart_p (op0)
11345 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11346 /* Fall through */ ;
11347 else
11348 break;
11350 /* ... fall through ... */
11352 case ZERO_EXTEND:
11353 mode = GET_MODE (XEXP (op0, 0));
11354 if (GET_MODE_CLASS (mode) == MODE_INT
11355 && (unsigned_comparison_p || equality_comparison_p)
11356 && HWI_COMPUTABLE_MODE_P (mode)
11357 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11358 && const_op >= 0
11359 && have_insn_for (COMPARE, mode))
11361 op0 = XEXP (op0, 0);
11362 continue;
11364 break;
11366 case PLUS:
11367 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11368 this for equality comparisons due to pathological cases involving
11369 overflows. */
11370 if (equality_comparison_p
11371 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11372 op1, XEXP (op0, 1))))
11374 op0 = XEXP (op0, 0);
11375 op1 = tem;
11376 continue;
11379 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11380 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11381 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11383 op0 = XEXP (XEXP (op0, 0), 0);
11384 code = (code == LT ? EQ : NE);
11385 continue;
11387 break;
11389 case MINUS:
11390 /* We used to optimize signed comparisons against zero, but that
11391 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11392 arrive here as equality comparisons, or (GEU, LTU) are
11393 optimized away. No need to special-case them. */
11395 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11396 (eq B (minus A C)), whichever simplifies. We can only do
11397 this for equality comparisons due to pathological cases involving
11398 overflows. */
11399 if (equality_comparison_p
11400 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11401 XEXP (op0, 1), op1)))
11403 op0 = XEXP (op0, 0);
11404 op1 = tem;
11405 continue;
11408 if (equality_comparison_p
11409 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11410 XEXP (op0, 0), op1)))
11412 op0 = XEXP (op0, 1);
11413 op1 = tem;
11414 continue;
11417 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11418 of bits in X minus 1, is one iff X > 0. */
11419 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11420 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11421 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11422 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11424 op0 = XEXP (op0, 1);
11425 code = (code == GE ? LE : GT);
11426 continue;
11428 break;
11430 case XOR:
11431 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11432 if C is zero or B is a constant. */
11433 if (equality_comparison_p
11434 && 0 != (tem = simplify_binary_operation (XOR, mode,
11435 XEXP (op0, 1), op1)))
11437 op0 = XEXP (op0, 0);
11438 op1 = tem;
11439 continue;
11441 break;
11443 case EQ: case NE:
11444 case UNEQ: case LTGT:
11445 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11446 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11447 case UNORDERED: case ORDERED:
11448 /* We can't do anything if OP0 is a condition code value, rather
11449 than an actual data value. */
11450 if (const_op != 0
11451 || CC0_P (XEXP (op0, 0))
11452 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11453 break;
11455 /* Get the two operands being compared. */
11456 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11457 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11458 else
11459 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11461 /* Check for the cases where we simply want the result of the
11462 earlier test or the opposite of that result. */
11463 if (code == NE || code == EQ
11464 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11465 && (code == LT || code == GE)))
11467 enum rtx_code new_code;
11468 if (code == LT || code == NE)
11469 new_code = GET_CODE (op0);
11470 else
11471 new_code = reversed_comparison_code (op0, NULL);
11473 if (new_code != UNKNOWN)
11475 code = new_code;
11476 op0 = tem;
11477 op1 = tem1;
11478 continue;
11481 break;
11483 case IOR:
11484 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11485 iff X <= 0. */
11486 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11487 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11488 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11490 op0 = XEXP (op0, 1);
11491 code = (code == GE ? GT : LE);
11492 continue;
11494 break;
11496 case AND:
11497 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11498 will be converted to a ZERO_EXTRACT later. */
11499 if (const_op == 0 && equality_comparison_p
11500 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11501 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11503 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11504 XEXP (XEXP (op0, 0), 1));
11505 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11506 continue;
11509 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11510 zero and X is a comparison and C1 and C2 describe only bits set
11511 in STORE_FLAG_VALUE, we can compare with X. */
11512 if (const_op == 0 && equality_comparison_p
11513 && mode_width <= HOST_BITS_PER_WIDE_INT
11514 && CONST_INT_P (XEXP (op0, 1))
11515 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11516 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11517 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11518 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11520 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11521 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11522 if ((~STORE_FLAG_VALUE & mask) == 0
11523 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11524 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11525 && COMPARISON_P (tem))))
11527 op0 = XEXP (XEXP (op0, 0), 0);
11528 continue;
11532 /* If we are doing an equality comparison of an AND of a bit equal
11533 to the sign bit, replace this with a LT or GE comparison of
11534 the underlying value. */
11535 if (equality_comparison_p
11536 && const_op == 0
11537 && CONST_INT_P (XEXP (op0, 1))
11538 && mode_width <= HOST_BITS_PER_WIDE_INT
11539 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11540 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11542 op0 = XEXP (op0, 0);
11543 code = (code == EQ ? GE : LT);
11544 continue;
11547 /* If this AND operation is really a ZERO_EXTEND from a narrower
11548 mode, the constant fits within that mode, and this is either an
11549 equality or unsigned comparison, try to do this comparison in
11550 the narrower mode.
11552 Note that in:
11554 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11555 -> (ne:DI (reg:SI 4) (const_int 0))
11557 unless TRULY_NOOP_TRUNCATION allows it or the register is
11558 known to hold a value of the required mode the
11559 transformation is invalid. */
11560 if ((equality_comparison_p || unsigned_comparison_p)
11561 && CONST_INT_P (XEXP (op0, 1))
11562 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11563 & GET_MODE_MASK (mode))
11564 + 1)) >= 0
11565 && const_op >> i == 0
11566 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11567 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11568 || (REG_P (XEXP (op0, 0))
11569 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11571 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11572 continue;
11575 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11576 fits in both M1 and M2 and the SUBREG is either paradoxical
11577 or represents the low part, permute the SUBREG and the AND
11578 and try again. */
11579 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11581 unsigned HOST_WIDE_INT c1;
11582 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11583 /* Require an integral mode, to avoid creating something like
11584 (AND:SF ...). */
11585 if (SCALAR_INT_MODE_P (tmode)
11586 /* It is unsafe to commute the AND into the SUBREG if the
11587 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11588 not defined. As originally written the upper bits
11589 have a defined value due to the AND operation.
11590 However, if we commute the AND inside the SUBREG then
11591 they no longer have defined values and the meaning of
11592 the code has been changed. */
11593 && (0
11594 #ifdef WORD_REGISTER_OPERATIONS
11595 || (mode_width > GET_MODE_PRECISION (tmode)
11596 && mode_width <= BITS_PER_WORD)
11597 #endif
11598 || (mode_width <= GET_MODE_PRECISION (tmode)
11599 && subreg_lowpart_p (XEXP (op0, 0))))
11600 && CONST_INT_P (XEXP (op0, 1))
11601 && mode_width <= HOST_BITS_PER_WIDE_INT
11602 && HWI_COMPUTABLE_MODE_P (tmode)
11603 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11604 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11605 && c1 != mask
11606 && c1 != GET_MODE_MASK (tmode))
11608 op0 = simplify_gen_binary (AND, tmode,
11609 SUBREG_REG (XEXP (op0, 0)),
11610 gen_int_mode (c1, tmode));
11611 op0 = gen_lowpart (mode, op0);
11612 continue;
11616 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11617 if (const_op == 0 && equality_comparison_p
11618 && XEXP (op0, 1) == const1_rtx
11619 && GET_CODE (XEXP (op0, 0)) == NOT)
11621 op0 = simplify_and_const_int (NULL_RTX, mode,
11622 XEXP (XEXP (op0, 0), 0), 1);
11623 code = (code == NE ? EQ : NE);
11624 continue;
11627 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11628 (eq (and (lshiftrt X) 1) 0).
11629 Also handle the case where (not X) is expressed using xor. */
11630 if (const_op == 0 && equality_comparison_p
11631 && XEXP (op0, 1) == const1_rtx
11632 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11634 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11635 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11637 if (GET_CODE (shift_op) == NOT
11638 || (GET_CODE (shift_op) == XOR
11639 && CONST_INT_P (XEXP (shift_op, 1))
11640 && CONST_INT_P (shift_count)
11641 && HWI_COMPUTABLE_MODE_P (mode)
11642 && (UINTVAL (XEXP (shift_op, 1))
11643 == (unsigned HOST_WIDE_INT) 1
11644 << INTVAL (shift_count))))
11647 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11648 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11649 code = (code == NE ? EQ : NE);
11650 continue;
11653 break;
11655 case ASHIFT:
11656 /* If we have (compare (ashift FOO N) (const_int C)) and
11657 the high order N bits of FOO (N+1 if an inequality comparison)
11658 are known to be zero, we can do this by comparing FOO with C
11659 shifted right N bits so long as the low-order N bits of C are
11660 zero. */
11661 if (CONST_INT_P (XEXP (op0, 1))
11662 && INTVAL (XEXP (op0, 1)) >= 0
11663 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11664 < HOST_BITS_PER_WIDE_INT)
11665 && (((unsigned HOST_WIDE_INT) const_op
11666 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11667 - 1)) == 0)
11668 && mode_width <= HOST_BITS_PER_WIDE_INT
11669 && (nonzero_bits (XEXP (op0, 0), mode)
11670 & ~(mask >> (INTVAL (XEXP (op0, 1))
11671 + ! equality_comparison_p))) == 0)
11673 /* We must perform a logical shift, not an arithmetic one,
11674 as we want the top N bits of C to be zero. */
11675 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11677 temp >>= INTVAL (XEXP (op0, 1));
11678 op1 = gen_int_mode (temp, mode);
11679 op0 = XEXP (op0, 0);
11680 continue;
11683 /* If we are doing a sign bit comparison, it means we are testing
11684 a particular bit. Convert it to the appropriate AND. */
11685 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11686 && mode_width <= HOST_BITS_PER_WIDE_INT)
11688 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11689 ((unsigned HOST_WIDE_INT) 1
11690 << (mode_width - 1
11691 - INTVAL (XEXP (op0, 1)))));
11692 code = (code == LT ? NE : EQ);
11693 continue;
11696 /* If this an equality comparison with zero and we are shifting
11697 the low bit to the sign bit, we can convert this to an AND of the
11698 low-order bit. */
11699 if (const_op == 0 && equality_comparison_p
11700 && CONST_INT_P (XEXP (op0, 1))
11701 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11703 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11704 continue;
11706 break;
11708 case ASHIFTRT:
11709 /* If this is an equality comparison with zero, we can do this
11710 as a logical shift, which might be much simpler. */
11711 if (equality_comparison_p && const_op == 0
11712 && CONST_INT_P (XEXP (op0, 1)))
11714 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11715 XEXP (op0, 0),
11716 INTVAL (XEXP (op0, 1)));
11717 continue;
11720 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11721 do the comparison in a narrower mode. */
11722 if (! unsigned_comparison_p
11723 && CONST_INT_P (XEXP (op0, 1))
11724 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11725 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11726 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11727 MODE_INT, 1)) != BLKmode
11728 && (((unsigned HOST_WIDE_INT) const_op
11729 + (GET_MODE_MASK (tmode) >> 1) + 1)
11730 <= GET_MODE_MASK (tmode)))
11732 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11733 continue;
11736 /* Likewise if OP0 is a PLUS of a sign extension with a
11737 constant, which is usually represented with the PLUS
11738 between the shifts. */
11739 if (! unsigned_comparison_p
11740 && CONST_INT_P (XEXP (op0, 1))
11741 && GET_CODE (XEXP (op0, 0)) == PLUS
11742 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11743 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11744 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11745 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11746 MODE_INT, 1)) != BLKmode
11747 && (((unsigned HOST_WIDE_INT) const_op
11748 + (GET_MODE_MASK (tmode) >> 1) + 1)
11749 <= GET_MODE_MASK (tmode)))
11751 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11752 rtx add_const = XEXP (XEXP (op0, 0), 1);
11753 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11754 add_const, XEXP (op0, 1));
11756 op0 = simplify_gen_binary (PLUS, tmode,
11757 gen_lowpart (tmode, inner),
11758 new_const);
11759 continue;
11762 /* ... fall through ... */
11763 case LSHIFTRT:
11764 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11765 the low order N bits of FOO are known to be zero, we can do this
11766 by comparing FOO with C shifted left N bits so long as no
11767 overflow occurs. Even if the low order N bits of FOO aren't known
11768 to be zero, if the comparison is >= or < we can use the same
11769 optimization and for > or <= by setting all the low
11770 order N bits in the comparison constant. */
11771 if (CONST_INT_P (XEXP (op0, 1))
11772 && INTVAL (XEXP (op0, 1)) > 0
11773 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11774 && mode_width <= HOST_BITS_PER_WIDE_INT
11775 && (((unsigned HOST_WIDE_INT) const_op
11776 + (GET_CODE (op0) != LSHIFTRT
11777 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11778 + 1)
11779 : 0))
11780 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11782 unsigned HOST_WIDE_INT low_bits
11783 = (nonzero_bits (XEXP (op0, 0), mode)
11784 & (((unsigned HOST_WIDE_INT) 1
11785 << INTVAL (XEXP (op0, 1))) - 1));
11786 if (low_bits == 0 || !equality_comparison_p)
11788 /* If the shift was logical, then we must make the condition
11789 unsigned. */
11790 if (GET_CODE (op0) == LSHIFTRT)
11791 code = unsigned_condition (code);
11793 const_op <<= INTVAL (XEXP (op0, 1));
11794 if (low_bits != 0
11795 && (code == GT || code == GTU
11796 || code == LE || code == LEU))
11797 const_op
11798 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11799 op1 = GEN_INT (const_op);
11800 op0 = XEXP (op0, 0);
11801 continue;
11805 /* If we are using this shift to extract just the sign bit, we
11806 can replace this with an LT or GE comparison. */
11807 if (const_op == 0
11808 && (equality_comparison_p || sign_bit_comparison_p)
11809 && CONST_INT_P (XEXP (op0, 1))
11810 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11812 op0 = XEXP (op0, 0);
11813 code = (code == NE || code == GT ? LT : GE);
11814 continue;
11816 break;
11818 default:
11819 break;
11822 break;
11825 /* Now make any compound operations involved in this comparison. Then,
11826 check for an outmost SUBREG on OP0 that is not doing anything or is
11827 paradoxical. The latter transformation must only be performed when
11828 it is known that the "extra" bits will be the same in op0 and op1 or
11829 that they don't matter. There are three cases to consider:
11831 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11832 care bits and we can assume they have any convenient value. So
11833 making the transformation is safe.
11835 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11836 In this case the upper bits of op0 are undefined. We should not make
11837 the simplification in that case as we do not know the contents of
11838 those bits.
11840 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11841 UNKNOWN. In that case we know those bits are zeros or ones. We must
11842 also be sure that they are the same as the upper bits of op1.
11844 We can never remove a SUBREG for a non-equality comparison because
11845 the sign bit is in a different place in the underlying object. */
11847 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11848 op1 = make_compound_operation (op1, SET);
11850 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11851 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11852 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11853 && (code == NE || code == EQ))
11855 if (paradoxical_subreg_p (op0))
11857 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11858 implemented. */
11859 if (REG_P (SUBREG_REG (op0)))
11861 op0 = SUBREG_REG (op0);
11862 op1 = gen_lowpart (GET_MODE (op0), op1);
11865 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
11866 <= HOST_BITS_PER_WIDE_INT)
11867 && (nonzero_bits (SUBREG_REG (op0),
11868 GET_MODE (SUBREG_REG (op0)))
11869 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11871 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11873 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11874 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11875 op0 = SUBREG_REG (op0), op1 = tem;
11879 /* We now do the opposite procedure: Some machines don't have compare
11880 insns in all modes. If OP0's mode is an integer mode smaller than a
11881 word and we can't do a compare in that mode, see if there is a larger
11882 mode for which we can do the compare. There are a number of cases in
11883 which we can use the wider mode. */
11885 mode = GET_MODE (op0);
11886 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11887 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11888 && ! have_insn_for (COMPARE, mode))
11889 for (tmode = GET_MODE_WIDER_MODE (mode);
11890 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
11891 tmode = GET_MODE_WIDER_MODE (tmode))
11892 if (have_insn_for (COMPARE, tmode))
11894 int zero_extended;
11896 /* If this is a test for negative, we can make an explicit
11897 test of the sign bit. Test this first so we can use
11898 a paradoxical subreg to extend OP0. */
11900 if (op1 == const0_rtx && (code == LT || code == GE)
11901 && HWI_COMPUTABLE_MODE_P (mode))
11903 op0 = simplify_gen_binary (AND, tmode,
11904 gen_lowpart (tmode, op0),
11905 GEN_INT ((unsigned HOST_WIDE_INT) 1
11906 << (GET_MODE_BITSIZE (mode)
11907 - 1)));
11908 code = (code == LT) ? NE : EQ;
11909 break;
11912 /* If the only nonzero bits in OP0 and OP1 are those in the
11913 narrower mode and this is an equality or unsigned comparison,
11914 we can use the wider mode. Similarly for sign-extended
11915 values, in which case it is true for all comparisons. */
11916 zero_extended = ((code == EQ || code == NE
11917 || code == GEU || code == GTU
11918 || code == LEU || code == LTU)
11919 && (nonzero_bits (op0, tmode)
11920 & ~GET_MODE_MASK (mode)) == 0
11921 && ((CONST_INT_P (op1)
11922 || (nonzero_bits (op1, tmode)
11923 & ~GET_MODE_MASK (mode)) == 0)));
11925 if (zero_extended
11926 || ((num_sign_bit_copies (op0, tmode)
11927 > (unsigned int) (GET_MODE_PRECISION (tmode)
11928 - GET_MODE_PRECISION (mode)))
11929 && (num_sign_bit_copies (op1, tmode)
11930 > (unsigned int) (GET_MODE_PRECISION (tmode)
11931 - GET_MODE_PRECISION (mode)))))
11933 /* If OP0 is an AND and we don't have an AND in MODE either,
11934 make a new AND in the proper mode. */
11935 if (GET_CODE (op0) == AND
11936 && !have_insn_for (AND, mode))
11937 op0 = simplify_gen_binary (AND, tmode,
11938 gen_lowpart (tmode,
11939 XEXP (op0, 0)),
11940 gen_lowpart (tmode,
11941 XEXP (op0, 1)));
11942 else
11944 if (zero_extended)
11946 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11947 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11949 else
11951 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11952 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11954 break;
11959 #ifdef CANONICALIZE_COMPARISON
11960 /* If this machine only supports a subset of valid comparisons, see if we
11961 can convert an unsupported one into a supported one. */
11962 CANONICALIZE_COMPARISON (code, op0, op1);
11963 #endif
11965 *pop0 = op0;
11966 *pop1 = op1;
11968 return code;
11971 /* Utility function for record_value_for_reg. Count number of
11972 rtxs in X. */
11973 static int
11974 count_rtxs (rtx x)
11976 enum rtx_code code = GET_CODE (x);
11977 const char *fmt;
11978 int i, j, ret = 1;
11980 if (GET_RTX_CLASS (code) == '2'
11981 || GET_RTX_CLASS (code) == 'c')
11983 rtx x0 = XEXP (x, 0);
11984 rtx x1 = XEXP (x, 1);
11986 if (x0 == x1)
11987 return 1 + 2 * count_rtxs (x0);
11989 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11990 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11991 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11992 return 2 + 2 * count_rtxs (x0)
11993 + count_rtxs (x == XEXP (x1, 0)
11994 ? XEXP (x1, 1) : XEXP (x1, 0));
11996 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11997 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11998 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11999 return 2 + 2 * count_rtxs (x1)
12000 + count_rtxs (x == XEXP (x0, 0)
12001 ? XEXP (x0, 1) : XEXP (x0, 0));
12004 fmt = GET_RTX_FORMAT (code);
12005 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12006 if (fmt[i] == 'e')
12007 ret += count_rtxs (XEXP (x, i));
12008 else if (fmt[i] == 'E')
12009 for (j = 0; j < XVECLEN (x, i); j++)
12010 ret += count_rtxs (XVECEXP (x, i, j));
12012 return ret;
12015 /* Utility function for following routine. Called when X is part of a value
12016 being stored into last_set_value. Sets last_set_table_tick
12017 for each register mentioned. Similar to mention_regs in cse.c */
12019 static void
12020 update_table_tick (rtx x)
12022 enum rtx_code code = GET_CODE (x);
12023 const char *fmt = GET_RTX_FORMAT (code);
12024 int i, j;
12026 if (code == REG)
12028 unsigned int regno = REGNO (x);
12029 unsigned int endregno = END_REGNO (x);
12030 unsigned int r;
12032 for (r = regno; r < endregno; r++)
12034 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, r);
12035 rsp->last_set_table_tick = label_tick;
12038 return;
12041 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12042 if (fmt[i] == 'e')
12044 /* Check for identical subexpressions. If x contains
12045 identical subexpression we only have to traverse one of
12046 them. */
12047 if (i == 0 && ARITHMETIC_P (x))
12049 /* Note that at this point x1 has already been
12050 processed. */
12051 rtx x0 = XEXP (x, 0);
12052 rtx x1 = XEXP (x, 1);
12054 /* If x0 and x1 are identical then there is no need to
12055 process x0. */
12056 if (x0 == x1)
12057 break;
12059 /* If x0 is identical to a subexpression of x1 then while
12060 processing x1, x0 has already been processed. Thus we
12061 are done with x. */
12062 if (ARITHMETIC_P (x1)
12063 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12064 break;
12066 /* If x1 is identical to a subexpression of x0 then we
12067 still have to process the rest of x0. */
12068 if (ARITHMETIC_P (x0)
12069 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12071 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12072 break;
12076 update_table_tick (XEXP (x, i));
12078 else if (fmt[i] == 'E')
12079 for (j = 0; j < XVECLEN (x, i); j++)
12080 update_table_tick (XVECEXP (x, i, j));
12083 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12084 are saying that the register is clobbered and we no longer know its
12085 value. If INSN is zero, don't update reg_stat[].last_set; this is
12086 only permitted with VALUE also zero and is used to invalidate the
12087 register. */
12089 static void
12090 record_value_for_reg (rtx reg, rtx insn, rtx value)
12092 unsigned int regno = REGNO (reg);
12093 unsigned int endregno = END_REGNO (reg);
12094 unsigned int i;
12095 reg_stat_type *rsp;
12097 /* If VALUE contains REG and we have a previous value for REG, substitute
12098 the previous value. */
12099 if (value && insn && reg_overlap_mentioned_p (reg, value))
12101 rtx tem;
12103 /* Set things up so get_last_value is allowed to see anything set up to
12104 our insn. */
12105 subst_low_luid = DF_INSN_LUID (insn);
12106 tem = get_last_value (reg);
12108 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12109 it isn't going to be useful and will take a lot of time to process,
12110 so just use the CLOBBER. */
12112 if (tem)
12114 if (ARITHMETIC_P (tem)
12115 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12116 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12117 tem = XEXP (tem, 0);
12118 else if (count_occurrences (value, reg, 1) >= 2)
12120 /* If there are two or more occurrences of REG in VALUE,
12121 prevent the value from growing too much. */
12122 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12123 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12126 value = replace_rtx (copy_rtx (value), reg, tem);
12130 /* For each register modified, show we don't know its value, that
12131 we don't know about its bitwise content, that its value has been
12132 updated, and that we don't know the location of the death of the
12133 register. */
12134 for (i = regno; i < endregno; i++)
12136 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12138 if (insn)
12139 rsp->last_set = insn;
12141 rsp->last_set_value = 0;
12142 rsp->last_set_mode = VOIDmode;
12143 rsp->last_set_nonzero_bits = 0;
12144 rsp->last_set_sign_bit_copies = 0;
12145 rsp->last_death = 0;
12146 rsp->truncated_to_mode = VOIDmode;
12149 /* Mark registers that are being referenced in this value. */
12150 if (value)
12151 update_table_tick (value);
12153 /* Now update the status of each register being set.
12154 If someone is using this register in this block, set this register
12155 to invalid since we will get confused between the two lives in this
12156 basic block. This makes using this register always invalid. In cse, we
12157 scan the table to invalidate all entries using this register, but this
12158 is too much work for us. */
12160 for (i = regno; i < endregno; i++)
12162 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12163 rsp->last_set_label = label_tick;
12164 if (!insn
12165 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12166 rsp->last_set_invalid = 1;
12167 else
12168 rsp->last_set_invalid = 0;
12171 /* The value being assigned might refer to X (like in "x++;"). In that
12172 case, we must replace it with (clobber (const_int 0)) to prevent
12173 infinite loops. */
12174 rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12175 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12177 value = copy_rtx (value);
12178 if (!get_last_value_validate (&value, insn, label_tick, 1))
12179 value = 0;
12182 /* For the main register being modified, update the value, the mode, the
12183 nonzero bits, and the number of sign bit copies. */
12185 rsp->last_set_value = value;
12187 if (value)
12189 enum machine_mode mode = GET_MODE (reg);
12190 subst_low_luid = DF_INSN_LUID (insn);
12191 rsp->last_set_mode = mode;
12192 if (GET_MODE_CLASS (mode) == MODE_INT
12193 && HWI_COMPUTABLE_MODE_P (mode))
12194 mode = nonzero_bits_mode;
12195 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12196 rsp->last_set_sign_bit_copies
12197 = num_sign_bit_copies (value, GET_MODE (reg));
12201 /* Called via note_stores from record_dead_and_set_regs to handle one
12202 SET or CLOBBER in an insn. DATA is the instruction in which the
12203 set is occurring. */
12205 static void
12206 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12208 rtx record_dead_insn = (rtx) data;
12210 if (GET_CODE (dest) == SUBREG)
12211 dest = SUBREG_REG (dest);
12213 if (!record_dead_insn)
12215 if (REG_P (dest))
12216 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12217 return;
12220 if (REG_P (dest))
12222 /* If we are setting the whole register, we know its value. Otherwise
12223 show that we don't know the value. We can handle SUBREG in
12224 some cases. */
12225 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12226 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12227 else if (GET_CODE (setter) == SET
12228 && GET_CODE (SET_DEST (setter)) == SUBREG
12229 && SUBREG_REG (SET_DEST (setter)) == dest
12230 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12231 && subreg_lowpart_p (SET_DEST (setter)))
12232 record_value_for_reg (dest, record_dead_insn,
12233 gen_lowpart (GET_MODE (dest),
12234 SET_SRC (setter)));
12235 else
12236 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12238 else if (MEM_P (dest)
12239 /* Ignore pushes, they clobber nothing. */
12240 && ! push_operand (dest, GET_MODE (dest)))
12241 mem_last_set = DF_INSN_LUID (record_dead_insn);
12244 /* Update the records of when each REG was most recently set or killed
12245 for the things done by INSN. This is the last thing done in processing
12246 INSN in the combiner loop.
12248 We update reg_stat[], in particular fields last_set, last_set_value,
12249 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12250 last_death, and also the similar information mem_last_set (which insn
12251 most recently modified memory) and last_call_luid (which insn was the
12252 most recent subroutine call). */
12254 static void
12255 record_dead_and_set_regs (rtx insn)
12257 rtx link;
12258 unsigned int i;
12260 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12262 if (REG_NOTE_KIND (link) == REG_DEAD
12263 && REG_P (XEXP (link, 0)))
12265 unsigned int regno = REGNO (XEXP (link, 0));
12266 unsigned int endregno = END_REGNO (XEXP (link, 0));
12268 for (i = regno; i < endregno; i++)
12270 reg_stat_type *rsp;
12272 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12273 rsp->last_death = insn;
12276 else if (REG_NOTE_KIND (link) == REG_INC)
12277 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12280 if (CALL_P (insn))
12282 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12283 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12285 reg_stat_type *rsp;
12287 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12288 rsp->last_set_invalid = 1;
12289 rsp->last_set = insn;
12290 rsp->last_set_value = 0;
12291 rsp->last_set_mode = VOIDmode;
12292 rsp->last_set_nonzero_bits = 0;
12293 rsp->last_set_sign_bit_copies = 0;
12294 rsp->last_death = 0;
12295 rsp->truncated_to_mode = VOIDmode;
12298 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12300 /* We can't combine into a call pattern. Remember, though, that
12301 the return value register is set at this LUID. We could
12302 still replace a register with the return value from the
12303 wrong subroutine call! */
12304 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12306 else
12307 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12310 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12311 register present in the SUBREG, so for each such SUBREG go back and
12312 adjust nonzero and sign bit information of the registers that are
12313 known to have some zero/sign bits set.
12315 This is needed because when combine blows the SUBREGs away, the
12316 information on zero/sign bits is lost and further combines can be
12317 missed because of that. */
12319 static void
12320 record_promoted_value (rtx insn, rtx subreg)
12322 struct insn_link *links;
12323 rtx set;
12324 unsigned int regno = REGNO (SUBREG_REG (subreg));
12325 enum machine_mode mode = GET_MODE (subreg);
12327 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12328 return;
12330 for (links = LOG_LINKS (insn); links;)
12332 reg_stat_type *rsp;
12334 insn = links->insn;
12335 set = single_set (insn);
12337 if (! set || !REG_P (SET_DEST (set))
12338 || REGNO (SET_DEST (set)) != regno
12339 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12341 links = links->next;
12342 continue;
12345 rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12346 if (rsp->last_set == insn)
12348 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12349 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12352 if (REG_P (SET_SRC (set)))
12354 regno = REGNO (SET_SRC (set));
12355 links = LOG_LINKS (insn);
12357 else
12358 break;
12362 /* Check if X, a register, is known to contain a value already
12363 truncated to MODE. In this case we can use a subreg to refer to
12364 the truncated value even though in the generic case we would need
12365 an explicit truncation. */
12367 static bool
12368 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12370 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
12371 enum machine_mode truncated = rsp->truncated_to_mode;
12373 if (truncated == 0
12374 || rsp->truncation_label < label_tick_ebb_start)
12375 return false;
12376 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12377 return true;
12378 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12379 return true;
12380 return false;
12383 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
12384 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
12385 might be able to turn a truncate into a subreg using this information.
12386 Return -1 if traversing *P is complete or 0 otherwise. */
12388 static int
12389 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12391 rtx x = *p;
12392 enum machine_mode truncated_mode;
12393 reg_stat_type *rsp;
12395 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12397 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12398 truncated_mode = GET_MODE (x);
12400 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12401 return -1;
12403 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12404 return -1;
12406 x = SUBREG_REG (x);
12408 /* ??? For hard-regs we now record everything. We might be able to
12409 optimize this using last_set_mode. */
12410 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12411 truncated_mode = GET_MODE (x);
12412 else
12413 return 0;
12415 rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
12416 if (rsp->truncated_to_mode == 0
12417 || rsp->truncation_label < label_tick_ebb_start
12418 || (GET_MODE_SIZE (truncated_mode)
12419 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12421 rsp->truncated_to_mode = truncated_mode;
12422 rsp->truncation_label = label_tick;
12425 return -1;
12428 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12429 the modes they are used in. This can help truning TRUNCATEs into
12430 SUBREGs. */
12432 static void
12433 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12435 for_each_rtx (x, record_truncated_value, NULL);
12438 /* Scan X for promoted SUBREGs. For each one found,
12439 note what it implies to the registers used in it. */
12441 static void
12442 check_promoted_subreg (rtx insn, rtx x)
12444 if (GET_CODE (x) == SUBREG
12445 && SUBREG_PROMOTED_VAR_P (x)
12446 && REG_P (SUBREG_REG (x)))
12447 record_promoted_value (insn, x);
12448 else
12450 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12451 int i, j;
12453 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12454 switch (format[i])
12456 case 'e':
12457 check_promoted_subreg (insn, XEXP (x, i));
12458 break;
12459 case 'V':
12460 case 'E':
12461 if (XVEC (x, i) != 0)
12462 for (j = 0; j < XVECLEN (x, i); j++)
12463 check_promoted_subreg (insn, XVECEXP (x, i, j));
12464 break;
12469 /* Verify that all the registers and memory references mentioned in *LOC are
12470 still valid. *LOC was part of a value set in INSN when label_tick was
12471 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12472 the invalid references with (clobber (const_int 0)) and return 1. This
12473 replacement is useful because we often can get useful information about
12474 the form of a value (e.g., if it was produced by a shift that always
12475 produces -1 or 0) even though we don't know exactly what registers it
12476 was produced from. */
12478 static int
12479 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12481 rtx x = *loc;
12482 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12483 int len = GET_RTX_LENGTH (GET_CODE (x));
12484 int i, j;
12486 if (REG_P (x))
12488 unsigned int regno = REGNO (x);
12489 unsigned int endregno = END_REGNO (x);
12490 unsigned int j;
12492 for (j = regno; j < endregno; j++)
12494 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, j);
12495 if (rsp->last_set_invalid
12496 /* If this is a pseudo-register that was only set once and not
12497 live at the beginning of the function, it is always valid. */
12498 || (! (regno >= FIRST_PSEUDO_REGISTER
12499 && REG_N_SETS (regno) == 1
12500 && (!REGNO_REG_SET_P
12501 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12502 && rsp->last_set_label > tick))
12504 if (replace)
12505 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12506 return replace;
12510 return 1;
12512 /* If this is a memory reference, make sure that there were no stores after
12513 it that might have clobbered the value. We don't have alias info, so we
12514 assume any store invalidates it. Moreover, we only have local UIDs, so
12515 we also assume that there were stores in the intervening basic blocks. */
12516 else if (MEM_P (x) && !MEM_READONLY_P (x)
12517 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12519 if (replace)
12520 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12521 return replace;
12524 for (i = 0; i < len; i++)
12526 if (fmt[i] == 'e')
12528 /* Check for identical subexpressions. If x contains
12529 identical subexpression we only have to traverse one of
12530 them. */
12531 if (i == 1 && ARITHMETIC_P (x))
12533 /* Note that at this point x0 has already been checked
12534 and found valid. */
12535 rtx x0 = XEXP (x, 0);
12536 rtx x1 = XEXP (x, 1);
12538 /* If x0 and x1 are identical then x is also valid. */
12539 if (x0 == x1)
12540 return 1;
12542 /* If x1 is identical to a subexpression of x0 then
12543 while checking x0, x1 has already been checked. Thus
12544 it is valid and so as x. */
12545 if (ARITHMETIC_P (x0)
12546 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12547 return 1;
12549 /* If x0 is identical to a subexpression of x1 then x is
12550 valid iff the rest of x1 is valid. */
12551 if (ARITHMETIC_P (x1)
12552 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12553 return
12554 get_last_value_validate (&XEXP (x1,
12555 x0 == XEXP (x1, 0) ? 1 : 0),
12556 insn, tick, replace);
12559 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12560 replace) == 0)
12561 return 0;
12563 else if (fmt[i] == 'E')
12564 for (j = 0; j < XVECLEN (x, i); j++)
12565 if (get_last_value_validate (&XVECEXP (x, i, j),
12566 insn, tick, replace) == 0)
12567 return 0;
12570 /* If we haven't found a reason for it to be invalid, it is valid. */
12571 return 1;
12574 /* Get the last value assigned to X, if known. Some registers
12575 in the value may be replaced with (clobber (const_int 0)) if their value
12576 is known longer known reliably. */
12578 static rtx
12579 get_last_value (const_rtx x)
12581 unsigned int regno;
12582 rtx value;
12583 reg_stat_type *rsp;
12585 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12586 then convert it to the desired mode. If this is a paradoxical SUBREG,
12587 we cannot predict what values the "extra" bits might have. */
12588 if (GET_CODE (x) == SUBREG
12589 && subreg_lowpart_p (x)
12590 && !paradoxical_subreg_p (x)
12591 && (value = get_last_value (SUBREG_REG (x))) != 0)
12592 return gen_lowpart (GET_MODE (x), value);
12594 if (!REG_P (x))
12595 return 0;
12597 regno = REGNO (x);
12598 rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12599 value = rsp->last_set_value;
12601 /* If we don't have a value, or if it isn't for this basic block and
12602 it's either a hard register, set more than once, or it's a live
12603 at the beginning of the function, return 0.
12605 Because if it's not live at the beginning of the function then the reg
12606 is always set before being used (is never used without being set).
12607 And, if it's set only once, and it's always set before use, then all
12608 uses must have the same last value, even if it's not from this basic
12609 block. */
12611 if (value == 0
12612 || (rsp->last_set_label < label_tick_ebb_start
12613 && (regno < FIRST_PSEUDO_REGISTER
12614 || REG_N_SETS (regno) != 1
12615 || REGNO_REG_SET_P
12616 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12617 return 0;
12619 /* If the value was set in a later insn than the ones we are processing,
12620 we can't use it even if the register was only set once. */
12621 if (rsp->last_set_label == label_tick
12622 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12623 return 0;
12625 /* If the value has all its registers valid, return it. */
12626 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12627 return value;
12629 /* Otherwise, make a copy and replace any invalid register with
12630 (clobber (const_int 0)). If that fails for some reason, return 0. */
12632 value = copy_rtx (value);
12633 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12634 return value;
12636 return 0;
12639 /* Return nonzero if expression X refers to a REG or to memory
12640 that is set in an instruction more recent than FROM_LUID. */
12642 static int
12643 use_crosses_set_p (const_rtx x, int from_luid)
12645 const char *fmt;
12646 int i;
12647 enum rtx_code code = GET_CODE (x);
12649 if (code == REG)
12651 unsigned int regno = REGNO (x);
12652 unsigned endreg = END_REGNO (x);
12654 #ifdef PUSH_ROUNDING
12655 /* Don't allow uses of the stack pointer to be moved,
12656 because we don't know whether the move crosses a push insn. */
12657 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12658 return 1;
12659 #endif
12660 for (; regno < endreg; regno++)
12662 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12663 if (rsp->last_set
12664 && rsp->last_set_label == label_tick
12665 && DF_INSN_LUID (rsp->last_set) > from_luid)
12666 return 1;
12668 return 0;
12671 if (code == MEM && mem_last_set > from_luid)
12672 return 1;
12674 fmt = GET_RTX_FORMAT (code);
12676 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12678 if (fmt[i] == 'E')
12680 int j;
12681 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12682 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12683 return 1;
12685 else if (fmt[i] == 'e'
12686 && use_crosses_set_p (XEXP (x, i), from_luid))
12687 return 1;
12689 return 0;
12692 /* Define three variables used for communication between the following
12693 routines. */
12695 static unsigned int reg_dead_regno, reg_dead_endregno;
12696 static int reg_dead_flag;
12698 /* Function called via note_stores from reg_dead_at_p.
12700 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12701 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12703 static void
12704 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12706 unsigned int regno, endregno;
12708 if (!REG_P (dest))
12709 return;
12711 regno = REGNO (dest);
12712 endregno = END_REGNO (dest);
12713 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12714 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12717 /* Return nonzero if REG is known to be dead at INSN.
12719 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12720 referencing REG, it is dead. If we hit a SET referencing REG, it is
12721 live. Otherwise, see if it is live or dead at the start of the basic
12722 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12723 must be assumed to be always live. */
12725 static int
12726 reg_dead_at_p (rtx reg, rtx insn)
12728 basic_block block;
12729 unsigned int i;
12731 /* Set variables for reg_dead_at_p_1. */
12732 reg_dead_regno = REGNO (reg);
12733 reg_dead_endregno = END_REGNO (reg);
12735 reg_dead_flag = 0;
12737 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12738 we allow the machine description to decide whether use-and-clobber
12739 patterns are OK. */
12740 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12742 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12743 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12744 return 0;
12747 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12748 beginning of basic block. */
12749 block = BLOCK_FOR_INSN (insn);
12750 for (;;)
12752 if (INSN_P (insn))
12754 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12755 if (reg_dead_flag)
12756 return reg_dead_flag == 1 ? 1 : 0;
12758 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12759 return 1;
12762 if (insn == BB_HEAD (block))
12763 break;
12765 insn = PREV_INSN (insn);
12768 /* Look at live-in sets for the basic block that we were in. */
12769 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12770 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12771 return 0;
12773 return 1;
12776 /* Note hard registers in X that are used. */
12778 static void
12779 mark_used_regs_combine (rtx x)
12781 RTX_CODE code = GET_CODE (x);
12782 unsigned int regno;
12783 int i;
12785 switch (code)
12787 case LABEL_REF:
12788 case SYMBOL_REF:
12789 case CONST:
12790 CASE_CONST_ANY:
12791 case PC:
12792 case ADDR_VEC:
12793 case ADDR_DIFF_VEC:
12794 case ASM_INPUT:
12795 #ifdef HAVE_cc0
12796 /* CC0 must die in the insn after it is set, so we don't need to take
12797 special note of it here. */
12798 case CC0:
12799 #endif
12800 return;
12802 case CLOBBER:
12803 /* If we are clobbering a MEM, mark any hard registers inside the
12804 address as used. */
12805 if (MEM_P (XEXP (x, 0)))
12806 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12807 return;
12809 case REG:
12810 regno = REGNO (x);
12811 /* A hard reg in a wide mode may really be multiple registers.
12812 If so, mark all of them just like the first. */
12813 if (regno < FIRST_PSEUDO_REGISTER)
12815 /* None of this applies to the stack, frame or arg pointers. */
12816 if (regno == STACK_POINTER_REGNUM
12817 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12818 || regno == HARD_FRAME_POINTER_REGNUM
12819 #endif
12820 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12821 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12822 #endif
12823 || regno == FRAME_POINTER_REGNUM)
12824 return;
12826 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12828 return;
12830 case SET:
12832 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12833 the address. */
12834 rtx testreg = SET_DEST (x);
12836 while (GET_CODE (testreg) == SUBREG
12837 || GET_CODE (testreg) == ZERO_EXTRACT
12838 || GET_CODE (testreg) == STRICT_LOW_PART)
12839 testreg = XEXP (testreg, 0);
12841 if (MEM_P (testreg))
12842 mark_used_regs_combine (XEXP (testreg, 0));
12844 mark_used_regs_combine (SET_SRC (x));
12846 return;
12848 default:
12849 break;
12852 /* Recursively scan the operands of this expression. */
12855 const char *fmt = GET_RTX_FORMAT (code);
12857 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12859 if (fmt[i] == 'e')
12860 mark_used_regs_combine (XEXP (x, i));
12861 else if (fmt[i] == 'E')
12863 int j;
12865 for (j = 0; j < XVECLEN (x, i); j++)
12866 mark_used_regs_combine (XVECEXP (x, i, j));
12872 /* Remove register number REGNO from the dead registers list of INSN.
12874 Return the note used to record the death, if there was one. */
12877 remove_death (unsigned int regno, rtx insn)
12879 rtx note = find_regno_note (insn, REG_DEAD, regno);
12881 if (note)
12882 remove_note (insn, note);
12884 return note;
12887 /* For each register (hardware or pseudo) used within expression X, if its
12888 death is in an instruction with luid between FROM_LUID (inclusive) and
12889 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12890 list headed by PNOTES.
12892 That said, don't move registers killed by maybe_kill_insn.
12894 This is done when X is being merged by combination into TO_INSN. These
12895 notes will then be distributed as needed. */
12897 static void
12898 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12899 rtx *pnotes)
12901 const char *fmt;
12902 int len, i;
12903 enum rtx_code code = GET_CODE (x);
12905 if (code == REG)
12907 unsigned int regno = REGNO (x);
12908 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno).last_death;
12910 /* Don't move the register if it gets killed in between from and to. */
12911 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12912 && ! reg_referenced_p (x, maybe_kill_insn))
12913 return;
12915 if (where_dead
12916 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12917 && DF_INSN_LUID (where_dead) >= from_luid
12918 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12920 rtx note = remove_death (regno, where_dead);
12922 /* It is possible for the call above to return 0. This can occur
12923 when last_death points to I2 or I1 that we combined with.
12924 In that case make a new note.
12926 We must also check for the case where X is a hard register
12927 and NOTE is a death note for a range of hard registers
12928 including X. In that case, we must put REG_DEAD notes for
12929 the remaining registers in place of NOTE. */
12931 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12932 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12933 > GET_MODE_SIZE (GET_MODE (x))))
12935 unsigned int deadregno = REGNO (XEXP (note, 0));
12936 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12937 unsigned int ourend = END_HARD_REGNO (x);
12938 unsigned int i;
12940 for (i = deadregno; i < deadend; i++)
12941 if (i < regno || i >= ourend)
12942 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12945 /* If we didn't find any note, or if we found a REG_DEAD note that
12946 covers only part of the given reg, and we have a multi-reg hard
12947 register, then to be safe we must check for REG_DEAD notes
12948 for each register other than the first. They could have
12949 their own REG_DEAD notes lying around. */
12950 else if ((note == 0
12951 || (note != 0
12952 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12953 < GET_MODE_SIZE (GET_MODE (x)))))
12954 && regno < FIRST_PSEUDO_REGISTER
12955 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12957 unsigned int ourend = END_HARD_REGNO (x);
12958 unsigned int i, offset;
12959 rtx oldnotes = 0;
12961 if (note)
12962 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12963 else
12964 offset = 1;
12966 for (i = regno + offset; i < ourend; i++)
12967 move_deaths (regno_reg_rtx[i],
12968 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12971 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12973 XEXP (note, 1) = *pnotes;
12974 *pnotes = note;
12976 else
12977 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
12980 return;
12983 else if (GET_CODE (x) == SET)
12985 rtx dest = SET_DEST (x);
12987 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12989 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12990 that accesses one word of a multi-word item, some
12991 piece of everything register in the expression is used by
12992 this insn, so remove any old death. */
12993 /* ??? So why do we test for equality of the sizes? */
12995 if (GET_CODE (dest) == ZERO_EXTRACT
12996 || GET_CODE (dest) == STRICT_LOW_PART
12997 || (GET_CODE (dest) == SUBREG
12998 && (((GET_MODE_SIZE (GET_MODE (dest))
12999 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13000 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13001 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13003 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13004 return;
13007 /* If this is some other SUBREG, we know it replaces the entire
13008 value, so use that as the destination. */
13009 if (GET_CODE (dest) == SUBREG)
13010 dest = SUBREG_REG (dest);
13012 /* If this is a MEM, adjust deaths of anything used in the address.
13013 For a REG (the only other possibility), the entire value is
13014 being replaced so the old value is not used in this insn. */
13016 if (MEM_P (dest))
13017 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13018 to_insn, pnotes);
13019 return;
13022 else if (GET_CODE (x) == CLOBBER)
13023 return;
13025 len = GET_RTX_LENGTH (code);
13026 fmt = GET_RTX_FORMAT (code);
13028 for (i = 0; i < len; i++)
13030 if (fmt[i] == 'E')
13032 int j;
13033 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13034 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13035 to_insn, pnotes);
13037 else if (fmt[i] == 'e')
13038 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13042 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13043 pattern of an insn. X must be a REG. */
13045 static int
13046 reg_bitfield_target_p (rtx x, rtx body)
13048 int i;
13050 if (GET_CODE (body) == SET)
13052 rtx dest = SET_DEST (body);
13053 rtx target;
13054 unsigned int regno, tregno, endregno, endtregno;
13056 if (GET_CODE (dest) == ZERO_EXTRACT)
13057 target = XEXP (dest, 0);
13058 else if (GET_CODE (dest) == STRICT_LOW_PART)
13059 target = SUBREG_REG (XEXP (dest, 0));
13060 else
13061 return 0;
13063 if (GET_CODE (target) == SUBREG)
13064 target = SUBREG_REG (target);
13066 if (!REG_P (target))
13067 return 0;
13069 tregno = REGNO (target), regno = REGNO (x);
13070 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13071 return target == x;
13073 endtregno = end_hard_regno (GET_MODE (target), tregno);
13074 endregno = end_hard_regno (GET_MODE (x), regno);
13076 return endregno > tregno && regno < endtregno;
13079 else if (GET_CODE (body) == PARALLEL)
13080 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13081 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13082 return 1;
13084 return 0;
13087 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13088 as appropriate. I3 and I2 are the insns resulting from the combination
13089 insns including FROM (I2 may be zero).
13091 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13092 not need REG_DEAD notes because they are being substituted for. This
13093 saves searching in the most common cases.
13095 Each note in the list is either ignored or placed on some insns, depending
13096 on the type of note. */
13098 static void
13099 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13100 rtx elim_i1, rtx elim_i0)
13102 rtx note, next_note;
13103 rtx tem;
13105 for (note = notes; note; note = next_note)
13107 rtx place = 0, place2 = 0;
13109 next_note = XEXP (note, 1);
13110 switch (REG_NOTE_KIND (note))
13112 case REG_BR_PROB:
13113 case REG_BR_PRED:
13114 /* Doesn't matter much where we put this, as long as it's somewhere.
13115 It is preferable to keep these notes on branches, which is most
13116 likely to be i3. */
13117 place = i3;
13118 break;
13120 case REG_NON_LOCAL_GOTO:
13121 if (JUMP_P (i3))
13122 place = i3;
13123 else
13125 gcc_assert (i2 && JUMP_P (i2));
13126 place = i2;
13128 break;
13130 case REG_EH_REGION:
13131 /* These notes must remain with the call or trapping instruction. */
13132 if (CALL_P (i3))
13133 place = i3;
13134 else if (i2 && CALL_P (i2))
13135 place = i2;
13136 else
13138 gcc_assert (cfun->can_throw_non_call_exceptions);
13139 if (may_trap_p (i3))
13140 place = i3;
13141 else if (i2 && may_trap_p (i2))
13142 place = i2;
13143 /* ??? Otherwise assume we've combined things such that we
13144 can now prove that the instructions can't trap. Drop the
13145 note in this case. */
13147 break;
13149 case REG_ARGS_SIZE:
13150 /* ??? How to distribute between i3-i1. Assume i3 contains the
13151 entire adjustment. Assert i3 contains at least some adjust. */
13152 if (!noop_move_p (i3))
13154 int old_size, args_size = INTVAL (XEXP (note, 0));
13155 /* fixup_args_size_notes looks at REG_NORETURN note,
13156 so ensure the note is placed there first. */
13157 if (CALL_P (i3))
13159 rtx *np;
13160 for (np = &next_note; *np; np = &XEXP (*np, 1))
13161 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13163 rtx n = *np;
13164 *np = XEXP (n, 1);
13165 XEXP (n, 1) = REG_NOTES (i3);
13166 REG_NOTES (i3) = n;
13167 break;
13170 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13171 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13172 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13173 gcc_assert (old_size != args_size
13174 || (CALL_P (i3)
13175 && !ACCUMULATE_OUTGOING_ARGS
13176 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13178 break;
13180 case REG_NORETURN:
13181 case REG_SETJMP:
13182 case REG_TM:
13183 /* These notes must remain with the call. It should not be
13184 possible for both I2 and I3 to be a call. */
13185 if (CALL_P (i3))
13186 place = i3;
13187 else
13189 gcc_assert (i2 && CALL_P (i2));
13190 place = i2;
13192 break;
13194 case REG_UNUSED:
13195 /* Any clobbers for i3 may still exist, and so we must process
13196 REG_UNUSED notes from that insn.
13198 Any clobbers from i2 or i1 can only exist if they were added by
13199 recog_for_combine. In that case, recog_for_combine created the
13200 necessary REG_UNUSED notes. Trying to keep any original
13201 REG_UNUSED notes from these insns can cause incorrect output
13202 if it is for the same register as the original i3 dest.
13203 In that case, we will notice that the register is set in i3,
13204 and then add a REG_UNUSED note for the destination of i3, which
13205 is wrong. However, it is possible to have REG_UNUSED notes from
13206 i2 or i1 for register which were both used and clobbered, so
13207 we keep notes from i2 or i1 if they will turn into REG_DEAD
13208 notes. */
13210 /* If this register is set or clobbered in I3, put the note there
13211 unless there is one already. */
13212 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13214 if (from_insn != i3)
13215 break;
13217 if (! (REG_P (XEXP (note, 0))
13218 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13219 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13220 place = i3;
13222 /* Otherwise, if this register is used by I3, then this register
13223 now dies here, so we must put a REG_DEAD note here unless there
13224 is one already. */
13225 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13226 && ! (REG_P (XEXP (note, 0))
13227 ? find_regno_note (i3, REG_DEAD,
13228 REGNO (XEXP (note, 0)))
13229 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13231 PUT_REG_NOTE_KIND (note, REG_DEAD);
13232 place = i3;
13234 break;
13236 case REG_EQUAL:
13237 case REG_EQUIV:
13238 case REG_NOALIAS:
13239 /* These notes say something about results of an insn. We can
13240 only support them if they used to be on I3 in which case they
13241 remain on I3. Otherwise they are ignored.
13243 If the note refers to an expression that is not a constant, we
13244 must also ignore the note since we cannot tell whether the
13245 equivalence is still true. It might be possible to do
13246 slightly better than this (we only have a problem if I2DEST
13247 or I1DEST is present in the expression), but it doesn't
13248 seem worth the trouble. */
13250 if (from_insn == i3
13251 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13252 place = i3;
13253 break;
13255 case REG_INC:
13256 /* These notes say something about how a register is used. They must
13257 be present on any use of the register in I2 or I3. */
13258 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13259 place = i3;
13261 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13263 if (place)
13264 place2 = i2;
13265 else
13266 place = i2;
13268 break;
13270 case REG_LABEL_TARGET:
13271 case REG_LABEL_OPERAND:
13272 /* This can show up in several ways -- either directly in the
13273 pattern, or hidden off in the constant pool with (or without?)
13274 a REG_EQUAL note. */
13275 /* ??? Ignore the without-reg_equal-note problem for now. */
13276 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13277 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13278 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13279 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13280 place = i3;
13282 if (i2
13283 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13284 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13285 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13286 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13288 if (place)
13289 place2 = i2;
13290 else
13291 place = i2;
13294 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13295 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13296 there. */
13297 if (place && JUMP_P (place)
13298 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13299 && (JUMP_LABEL (place) == NULL
13300 || JUMP_LABEL (place) == XEXP (note, 0)))
13302 rtx label = JUMP_LABEL (place);
13304 if (!label)
13305 JUMP_LABEL (place) = XEXP (note, 0);
13306 else if (LABEL_P (label))
13307 LABEL_NUSES (label)--;
13310 if (place2 && JUMP_P (place2)
13311 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13312 && (JUMP_LABEL (place2) == NULL
13313 || JUMP_LABEL (place2) == XEXP (note, 0)))
13315 rtx label = JUMP_LABEL (place2);
13317 if (!label)
13318 JUMP_LABEL (place2) = XEXP (note, 0);
13319 else if (LABEL_P (label))
13320 LABEL_NUSES (label)--;
13321 place2 = 0;
13323 break;
13325 case REG_NONNEG:
13326 /* This note says something about the value of a register prior
13327 to the execution of an insn. It is too much trouble to see
13328 if the note is still correct in all situations. It is better
13329 to simply delete it. */
13330 break;
13332 case REG_DEAD:
13333 /* If we replaced the right hand side of FROM_INSN with a
13334 REG_EQUAL note, the original use of the dying register
13335 will not have been combined into I3 and I2. In such cases,
13336 FROM_INSN is guaranteed to be the first of the combined
13337 instructions, so we simply need to search back before
13338 FROM_INSN for the previous use or set of this register,
13339 then alter the notes there appropriately.
13341 If the register is used as an input in I3, it dies there.
13342 Similarly for I2, if it is nonzero and adjacent to I3.
13344 If the register is not used as an input in either I3 or I2
13345 and it is not one of the registers we were supposed to eliminate,
13346 there are two possibilities. We might have a non-adjacent I2
13347 or we might have somehow eliminated an additional register
13348 from a computation. For example, we might have had A & B where
13349 we discover that B will always be zero. In this case we will
13350 eliminate the reference to A.
13352 In both cases, we must search to see if we can find a previous
13353 use of A and put the death note there. */
13355 if (from_insn
13356 && from_insn == i2mod
13357 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13358 tem = from_insn;
13359 else
13361 if (from_insn
13362 && CALL_P (from_insn)
13363 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13364 place = from_insn;
13365 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13366 place = i3;
13367 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13368 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13369 place = i2;
13370 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13371 && !(i2mod
13372 && reg_overlap_mentioned_p (XEXP (note, 0),
13373 i2mod_old_rhs)))
13374 || rtx_equal_p (XEXP (note, 0), elim_i1)
13375 || rtx_equal_p (XEXP (note, 0), elim_i0))
13376 break;
13377 tem = i3;
13380 if (place == 0)
13382 basic_block bb = this_basic_block;
13384 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13386 if (!NONDEBUG_INSN_P (tem))
13388 if (tem == BB_HEAD (bb))
13389 break;
13390 continue;
13393 /* If the register is being set at TEM, see if that is all
13394 TEM is doing. If so, delete TEM. Otherwise, make this
13395 into a REG_UNUSED note instead. Don't delete sets to
13396 global register vars. */
13397 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13398 || !global_regs[REGNO (XEXP (note, 0))])
13399 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13401 rtx set = single_set (tem);
13402 rtx inner_dest = 0;
13403 #ifdef HAVE_cc0
13404 rtx cc0_setter = NULL_RTX;
13405 #endif
13407 if (set != 0)
13408 for (inner_dest = SET_DEST (set);
13409 (GET_CODE (inner_dest) == STRICT_LOW_PART
13410 || GET_CODE (inner_dest) == SUBREG
13411 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13412 inner_dest = XEXP (inner_dest, 0))
13415 /* Verify that it was the set, and not a clobber that
13416 modified the register.
13418 CC0 targets must be careful to maintain setter/user
13419 pairs. If we cannot delete the setter due to side
13420 effects, mark the user with an UNUSED note instead
13421 of deleting it. */
13423 if (set != 0 && ! side_effects_p (SET_SRC (set))
13424 && rtx_equal_p (XEXP (note, 0), inner_dest)
13425 #ifdef HAVE_cc0
13426 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13427 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13428 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13429 #endif
13432 /* Move the notes and links of TEM elsewhere.
13433 This might delete other dead insns recursively.
13434 First set the pattern to something that won't use
13435 any register. */
13436 rtx old_notes = REG_NOTES (tem);
13438 PATTERN (tem) = pc_rtx;
13439 REG_NOTES (tem) = NULL;
13441 distribute_notes (old_notes, tem, tem, NULL_RTX,
13442 NULL_RTX, NULL_RTX, NULL_RTX);
13443 distribute_links (LOG_LINKS (tem));
13445 SET_INSN_DELETED (tem);
13446 if (tem == i2)
13447 i2 = NULL_RTX;
13449 #ifdef HAVE_cc0
13450 /* Delete the setter too. */
13451 if (cc0_setter)
13453 PATTERN (cc0_setter) = pc_rtx;
13454 old_notes = REG_NOTES (cc0_setter);
13455 REG_NOTES (cc0_setter) = NULL;
13457 distribute_notes (old_notes, cc0_setter,
13458 cc0_setter, NULL_RTX,
13459 NULL_RTX, NULL_RTX, NULL_RTX);
13460 distribute_links (LOG_LINKS (cc0_setter));
13462 SET_INSN_DELETED (cc0_setter);
13463 if (cc0_setter == i2)
13464 i2 = NULL_RTX;
13466 #endif
13468 else
13470 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13472 /* If there isn't already a REG_UNUSED note, put one
13473 here. Do not place a REG_DEAD note, even if
13474 the register is also used here; that would not
13475 match the algorithm used in lifetime analysis
13476 and can cause the consistency check in the
13477 scheduler to fail. */
13478 if (! find_regno_note (tem, REG_UNUSED,
13479 REGNO (XEXP (note, 0))))
13480 place = tem;
13481 break;
13484 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13485 || (CALL_P (tem)
13486 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13488 place = tem;
13490 /* If we are doing a 3->2 combination, and we have a
13491 register which formerly died in i3 and was not used
13492 by i2, which now no longer dies in i3 and is used in
13493 i2 but does not die in i2, and place is between i2
13494 and i3, then we may need to move a link from place to
13495 i2. */
13496 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13497 && from_insn
13498 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13499 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13501 struct insn_link *links = LOG_LINKS (place);
13502 LOG_LINKS (place) = NULL;
13503 distribute_links (links);
13505 break;
13508 if (tem == BB_HEAD (bb))
13509 break;
13514 /* If the register is set or already dead at PLACE, we needn't do
13515 anything with this note if it is still a REG_DEAD note.
13516 We check here if it is set at all, not if is it totally replaced,
13517 which is what `dead_or_set_p' checks, so also check for it being
13518 set partially. */
13520 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13522 unsigned int regno = REGNO (XEXP (note, 0));
13523 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, regno);
13525 if (dead_or_set_p (place, XEXP (note, 0))
13526 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13528 /* Unless the register previously died in PLACE, clear
13529 last_death. [I no longer understand why this is
13530 being done.] */
13531 if (rsp->last_death != place)
13532 rsp->last_death = 0;
13533 place = 0;
13535 else
13536 rsp->last_death = place;
13538 /* If this is a death note for a hard reg that is occupying
13539 multiple registers, ensure that we are still using all
13540 parts of the object. If we find a piece of the object
13541 that is unused, we must arrange for an appropriate REG_DEAD
13542 note to be added for it. However, we can't just emit a USE
13543 and tag the note to it, since the register might actually
13544 be dead; so we recourse, and the recursive call then finds
13545 the previous insn that used this register. */
13547 if (place && regno < FIRST_PSEUDO_REGISTER
13548 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13550 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13551 int all_used = 1;
13552 unsigned int i;
13554 for (i = regno; i < endregno; i++)
13555 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13556 && ! find_regno_fusage (place, USE, i))
13557 || dead_or_set_regno_p (place, i))
13558 all_used = 0;
13560 if (! all_used)
13562 /* Put only REG_DEAD notes for pieces that are
13563 not already dead or set. */
13565 for (i = regno; i < endregno;
13566 i += hard_regno_nregs[i][reg_raw_mode[i]])
13568 rtx piece = regno_reg_rtx[i];
13569 basic_block bb = this_basic_block;
13571 if (! dead_or_set_p (place, piece)
13572 && ! reg_bitfield_target_p (piece,
13573 PATTERN (place)))
13575 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13576 NULL_RTX);
13578 distribute_notes (new_note, place, place,
13579 NULL_RTX, NULL_RTX, NULL_RTX,
13580 NULL_RTX);
13582 else if (! refers_to_regno_p (i, i + 1,
13583 PATTERN (place), 0)
13584 && ! find_regno_fusage (place, USE, i))
13585 for (tem = PREV_INSN (place); ;
13586 tem = PREV_INSN (tem))
13588 if (!NONDEBUG_INSN_P (tem))
13590 if (tem == BB_HEAD (bb))
13591 break;
13592 continue;
13594 if (dead_or_set_p (tem, piece)
13595 || reg_bitfield_target_p (piece,
13596 PATTERN (tem)))
13598 add_reg_note (tem, REG_UNUSED, piece);
13599 break;
13605 place = 0;
13609 break;
13611 default:
13612 /* Any other notes should not be present at this point in the
13613 compilation. */
13614 gcc_unreachable ();
13617 if (place)
13619 XEXP (note, 1) = REG_NOTES (place);
13620 REG_NOTES (place) = note;
13623 if (place2)
13624 add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13628 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13629 I3, I2, and I1 to new locations. This is also called to add a link
13630 pointing at I3 when I3's destination is changed. */
13632 static void
13633 distribute_links (struct insn_link *links)
13635 struct insn_link *link, *next_link;
13637 for (link = links; link; link = next_link)
13639 rtx place = 0;
13640 rtx insn;
13641 rtx set, reg;
13643 next_link = link->next;
13645 /* If the insn that this link points to is a NOTE or isn't a single
13646 set, ignore it. In the latter case, it isn't clear what we
13647 can do other than ignore the link, since we can't tell which
13648 register it was for. Such links wouldn't be used by combine
13649 anyway.
13651 It is not possible for the destination of the target of the link to
13652 have been changed by combine. The only potential of this is if we
13653 replace I3, I2, and I1 by I3 and I2. But in that case the
13654 destination of I2 also remains unchanged. */
13656 if (NOTE_P (link->insn)
13657 || (set = single_set (link->insn)) == 0)
13658 continue;
13660 reg = SET_DEST (set);
13661 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13662 || GET_CODE (reg) == STRICT_LOW_PART)
13663 reg = XEXP (reg, 0);
13665 /* A LOG_LINK is defined as being placed on the first insn that uses
13666 a register and points to the insn that sets the register. Start
13667 searching at the next insn after the target of the link and stop
13668 when we reach a set of the register or the end of the basic block.
13670 Note that this correctly handles the link that used to point from
13671 I3 to I2. Also note that not much searching is typically done here
13672 since most links don't point very far away. */
13674 for (insn = NEXT_INSN (link->insn);
13675 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13676 || BB_HEAD (this_basic_block->next_bb) != insn));
13677 insn = NEXT_INSN (insn))
13678 if (DEBUG_INSN_P (insn))
13679 continue;
13680 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13682 if (reg_referenced_p (reg, PATTERN (insn)))
13683 place = insn;
13684 break;
13686 else if (CALL_P (insn)
13687 && find_reg_fusage (insn, USE, reg))
13689 place = insn;
13690 break;
13692 else if (INSN_P (insn) && reg_set_p (reg, insn))
13693 break;
13695 /* If we found a place to put the link, place it there unless there
13696 is already a link to the same insn as LINK at that point. */
13698 if (place)
13700 struct insn_link *link2;
13702 FOR_EACH_LOG_LINK (link2, place)
13703 if (link2->insn == link->insn)
13704 break;
13706 if (link2 == NULL)
13708 link->next = LOG_LINKS (place);
13709 LOG_LINKS (place) = link;
13711 /* Set added_links_insn to the earliest insn we added a
13712 link to. */
13713 if (added_links_insn == 0
13714 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13715 added_links_insn = place;
13721 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13722 Check whether the expression pointer to by LOC is a register or
13723 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13724 Otherwise return zero. */
13726 static int
13727 unmentioned_reg_p_1 (rtx *loc, void *expr)
13729 rtx x = *loc;
13731 if (x != NULL_RTX
13732 && (REG_P (x) || MEM_P (x))
13733 && ! reg_mentioned_p (x, (rtx) expr))
13734 return 1;
13735 return 0;
13738 /* Check for any register or memory mentioned in EQUIV that is not
13739 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13740 of EXPR where some registers may have been replaced by constants. */
13742 static bool
13743 unmentioned_reg_p (rtx equiv, rtx expr)
13745 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13748 DEBUG_FUNCTION void
13749 dump_combine_stats (FILE *file)
13751 fprintf
13752 (file,
13753 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13754 combine_attempts, combine_merges, combine_extras, combine_successes);
13757 void
13758 dump_combine_total_stats (FILE *file)
13760 fprintf
13761 (file,
13762 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13763 total_attempts, total_merges, total_extras, total_successes);
13766 static bool
13767 gate_handle_combine (void)
13769 return (optimize > 0);
13772 /* Try combining insns through substitution. */
13773 static unsigned int
13774 rest_of_handle_combine (void)
13776 int rebuild_jump_labels_after_combine;
13778 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13779 df_note_add_problem ();
13780 df_analyze ();
13782 regstat_init_n_sets_and_refs ();
13784 rebuild_jump_labels_after_combine
13785 = combine_instructions (get_insns (), max_reg_num ());
13787 /* Combining insns may have turned an indirect jump into a
13788 direct jump. Rebuild the JUMP_LABEL fields of jumping
13789 instructions. */
13790 if (rebuild_jump_labels_after_combine)
13792 timevar_push (TV_JUMP);
13793 rebuild_jump_labels (get_insns ());
13794 cleanup_cfg (0);
13795 timevar_pop (TV_JUMP);
13798 regstat_free_n_sets_and_refs ();
13799 return 0;
13802 struct rtl_opt_pass pass_combine =
13805 RTL_PASS,
13806 "combine", /* name */
13807 gate_handle_combine, /* gate */
13808 rest_of_handle_combine, /* execute */
13809 NULL, /* sub */
13810 NULL, /* next */
13811 0, /* static_pass_number */
13812 TV_COMBINE, /* tv_id */
13813 PROP_cfglayout, /* properties_required */
13814 0, /* properties_provided */
13815 0, /* properties_destroyed */
13816 0, /* todo_flags_start */
13817 TODO_df_finish | TODO_verify_rtl_sharing |
13818 TODO_ggc_collect, /* todo_flags_finish */