In libobjc/: 2011-06-03 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / combine.c
blob8af86f2c545aa25af204ff3ed76652a24b520521
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 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 output.h for dump_file. */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105 #include "df.h"
106 #include "cgraph.h"
107 #include "obstack.h"
109 /* Number of attempts to combine instructions in this function. */
111 static int combine_attempts;
113 /* Number of attempts that got as far as substitution in this function. */
115 static int combine_merges;
117 /* Number of instructions combined with added SETs in this function. */
119 static int combine_extras;
121 /* Number of instructions combined in this function. */
123 static int combine_successes;
125 /* Totals over entire compilation. */
127 static int total_attempts, total_merges, total_extras, total_successes;
129 /* combine_instructions may try to replace the right hand side of the
130 second instruction with the value of an associated REG_EQUAL note
131 before throwing it at try_combine. That is problematic when there
132 is a REG_DEAD note for a register used in the old right hand side
133 and can cause distribute_notes to do wrong things. This is the
134 second instruction if it has been so modified, null otherwise. */
136 static rtx i2mod;
138 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
140 static rtx i2mod_old_rhs;
142 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
144 static rtx i2mod_new_rhs;
146 typedef struct reg_stat_struct {
147 /* Record last point of death of (hard or pseudo) register n. */
148 rtx last_death;
150 /* Record last point of modification of (hard or pseudo) register n. */
151 rtx last_set;
153 /* The next group of fields allows the recording of the last value assigned
154 to (hard or pseudo) register n. We use this information to see if an
155 operation being processed is redundant given a prior operation performed
156 on the register. For example, an `and' with a constant is redundant if
157 all the zero bits are already known to be turned off.
159 We use an approach similar to that used by cse, but change it in the
160 following ways:
162 (1) We do not want to reinitialize at each label.
163 (2) It is useful, but not critical, to know the actual value assigned
164 to a register. Often just its form is helpful.
166 Therefore, we maintain the following fields:
168 last_set_value the last value assigned
169 last_set_label records the value of label_tick when the
170 register was assigned
171 last_set_table_tick records the value of label_tick when a
172 value using the register is assigned
173 last_set_invalid set to nonzero when it is not valid
174 to use the value of this register in some
175 register's value
177 To understand the usage of these tables, it is important to understand
178 the distinction between the value in last_set_value being valid and
179 the register being validly contained in some other expression in the
180 table.
182 (The next two parameters are out of date).
184 reg_stat[i].last_set_value is valid if it is nonzero, and either
185 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
187 Register I may validly appear in any expression returned for the value
188 of another register if reg_n_sets[i] is 1. It may also appear in the
189 value for register J if reg_stat[j].last_set_invalid is zero, or
190 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
192 If an expression is found in the table containing a register which may
193 not validly appear in an expression, the register is replaced by
194 something that won't match, (clobber (const_int 0)). */
196 /* Record last value assigned to (hard or pseudo) register n. */
198 rtx last_set_value;
200 /* Record the value of label_tick when an expression involving register n
201 is placed in last_set_value. */
203 int last_set_table_tick;
205 /* Record the value of label_tick when the value for register n is placed in
206 last_set_value. */
208 int last_set_label;
210 /* These fields are maintained in parallel with last_set_value and are
211 used to store the mode in which the register was last set, the bits
212 that were known to be zero when it was last set, and the number of
213 sign bits copies it was known to have when it was last set. */
215 unsigned HOST_WIDE_INT last_set_nonzero_bits;
216 char last_set_sign_bit_copies;
217 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
219 /* Set nonzero if references to register n in expressions should not be
220 used. last_set_invalid is set nonzero when this register is being
221 assigned to and last_set_table_tick == label_tick. */
223 char last_set_invalid;
225 /* Some registers that are set more than once and used in more than one
226 basic block are nevertheless always set in similar ways. For example,
227 a QImode register may be loaded from memory in two places on a machine
228 where byte loads zero extend.
230 We record in the following fields if a register has some leading bits
231 that are always equal to the sign bit, and what we know about the
232 nonzero bits of a register, specifically which bits are known to be
233 zero.
235 If an entry is zero, it means that we don't know anything special. */
237 unsigned char sign_bit_copies;
239 unsigned HOST_WIDE_INT nonzero_bits;
241 /* Record the value of the label_tick when the last truncation
242 happened. The field truncated_to_mode is only valid if
243 truncation_label == label_tick. */
245 int truncation_label;
247 /* Record the last truncation seen for this register. If truncation
248 is not a nop to this mode we might be able to save an explicit
249 truncation if we know that value already contains a truncated
250 value. */
252 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
253 } reg_stat_type;
255 DEF_VEC_O(reg_stat_type);
256 DEF_VEC_ALLOC_O(reg_stat_type,heap);
258 static VEC(reg_stat_type,heap) *reg_stat;
260 /* Record the luid of the last insn that invalidated memory
261 (anything that writes memory, and subroutine calls, but not pushes). */
263 static int mem_last_set;
265 /* Record the luid of the last CALL_INSN
266 so we can tell whether a potential combination crosses any calls. */
268 static int last_call_luid;
270 /* When `subst' is called, this is the insn that is being modified
271 (by combining in a previous insn). The PATTERN of this insn
272 is still the old pattern partially modified and it should not be
273 looked at, but this may be used to examine the successors of the insn
274 to judge whether a simplification is valid. */
276 static rtx subst_insn;
278 /* This is the lowest LUID that `subst' is currently dealing with.
279 get_last_value will not return a value if the register was set at or
280 after this LUID. If not for this mechanism, we could get confused if
281 I2 or I1 in try_combine were an insn that used the old value of a register
282 to obtain a new value. In that case, we might erroneously get the
283 new value of the register when we wanted the old one. */
285 static int subst_low_luid;
287 /* This contains any hard registers that are used in newpat; reg_dead_at_p
288 must consider all these registers to be always live. */
290 static HARD_REG_SET newpat_used_regs;
292 /* This is an insn to which a LOG_LINKS entry has been added. If this
293 insn is the earlier than I2 or I3, combine should rescan starting at
294 that location. */
296 static rtx added_links_insn;
298 /* Basic block in which we are performing combines. */
299 static basic_block this_basic_block;
300 static bool optimize_this_for_speed_p;
303 /* Length of the currently allocated uid_insn_cost array. */
305 static int max_uid_known;
307 /* The following array records the insn_rtx_cost for every insn
308 in the instruction stream. */
310 static int *uid_insn_cost;
312 /* The following array records the LOG_LINKS for every insn in the
313 instruction stream as struct insn_link pointers. */
315 struct insn_link {
316 rtx insn;
317 struct insn_link *next;
320 static struct insn_link **uid_log_links;
322 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
323 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
325 #define FOR_EACH_LOG_LINK(L, INSN) \
326 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
328 /* Links for LOG_LINKS are allocated from this obstack. */
330 static struct obstack insn_link_obstack;
332 /* Allocate a link. */
334 static inline struct insn_link *
335 alloc_insn_link (rtx insn, struct insn_link *next)
337 struct insn_link *l
338 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
339 sizeof (struct insn_link));
340 l->insn = insn;
341 l->next = next;
342 return l;
345 /* Incremented for each basic block. */
347 static int label_tick;
349 /* Reset to label_tick for each extended basic block in scanning order. */
351 static int label_tick_ebb_start;
353 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
354 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
356 static enum machine_mode nonzero_bits_mode;
358 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
359 be safely used. It is zero while computing them and after combine has
360 completed. This former test prevents propagating values based on
361 previously set values, which can be incorrect if a variable is modified
362 in a loop. */
364 static int nonzero_sign_valid;
367 /* Record one modification to rtl structure
368 to be undone by storing old_contents into *where. */
370 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE };
372 struct undo
374 struct undo *next;
375 enum undo_kind kind;
376 union { rtx r; int i; enum machine_mode m; } old_contents;
377 union { rtx *r; int *i; } where;
380 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
381 num_undo says how many are currently recorded.
383 other_insn is nonzero if we have modified some other insn in the process
384 of working on subst_insn. It must be verified too. */
386 struct undobuf
388 struct undo *undos;
389 struct undo *frees;
390 rtx other_insn;
393 static struct undobuf undobuf;
395 /* Number of times the pseudo being substituted for
396 was found and replaced. */
398 static int n_occurrences;
400 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
401 enum machine_mode,
402 unsigned HOST_WIDE_INT,
403 unsigned HOST_WIDE_INT *);
404 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
405 enum machine_mode,
406 unsigned int, unsigned int *);
407 static void do_SUBST (rtx *, rtx);
408 static void do_SUBST_INT (int *, int);
409 static void init_reg_last (void);
410 static void setup_incoming_promotions (rtx);
411 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
412 static int cant_combine_insn_p (rtx);
413 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
414 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
415 static int contains_muldiv (rtx);
416 static rtx try_combine (rtx, rtx, rtx, rtx, int *, rtx);
417 static void undo_all (void);
418 static void undo_commit (void);
419 static rtx *find_split_point (rtx *, rtx, bool);
420 static rtx subst (rtx, rtx, rtx, int, int, int);
421 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
422 static rtx simplify_if_then_else (rtx);
423 static rtx simplify_set (rtx);
424 static rtx simplify_logical (rtx);
425 static rtx expand_compound_operation (rtx);
426 static const_rtx expand_field_assignment (const_rtx);
427 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
428 rtx, unsigned HOST_WIDE_INT, int, int, int);
429 static rtx extract_left_shift (rtx, int);
430 static rtx make_compound_operation (rtx, enum rtx_code);
431 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
432 unsigned HOST_WIDE_INT *);
433 static rtx canon_reg_for_combine (rtx, rtx);
434 static rtx force_to_mode (rtx, enum machine_mode,
435 unsigned HOST_WIDE_INT, int);
436 static rtx if_then_else_cond (rtx, rtx *, rtx *);
437 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
438 static int rtx_equal_for_field_assignment_p (rtx, rtx);
439 static rtx make_field_assignment (rtx);
440 static rtx apply_distributive_law (rtx);
441 static rtx distribute_and_simplify_rtx (rtx, int);
442 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
443 unsigned HOST_WIDE_INT);
444 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
445 unsigned HOST_WIDE_INT);
446 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
447 HOST_WIDE_INT, enum machine_mode, int *);
448 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
449 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
450 int);
451 static int recog_for_combine (rtx *, rtx, rtx *);
452 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
453 static enum rtx_code simplify_compare_const (enum rtx_code, rtx, rtx *);
454 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
455 static void update_table_tick (rtx);
456 static void record_value_for_reg (rtx, rtx, rtx);
457 static void check_promoted_subreg (rtx, rtx);
458 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
459 static void record_dead_and_set_regs (rtx);
460 static int get_last_value_validate (rtx *, rtx, int, int);
461 static rtx get_last_value (const_rtx);
462 static int use_crosses_set_p (const_rtx, int);
463 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
464 static int reg_dead_at_p (rtx, rtx);
465 static void move_deaths (rtx, rtx, int, rtx, rtx *);
466 static int reg_bitfield_target_p (rtx, rtx);
467 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
468 static void distribute_links (struct insn_link *);
469 static void mark_used_regs_combine (rtx);
470 static void record_promoted_value (rtx, rtx);
471 static int unmentioned_reg_p_1 (rtx *, void *);
472 static bool unmentioned_reg_p (rtx, rtx);
473 static int record_truncated_value (rtx *, void *);
474 static void record_truncated_values (rtx *, void *);
475 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
476 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
479 /* It is not safe to use ordinary gen_lowpart in combine.
480 See comments in gen_lowpart_for_combine. */
481 #undef RTL_HOOKS_GEN_LOWPART
482 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
484 /* Our implementation of gen_lowpart never emits a new pseudo. */
485 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
486 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
488 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
489 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
491 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
492 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
494 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
495 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
497 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
500 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
501 PATTERN can not be split. Otherwise, it returns an insn sequence.
502 This is a wrapper around split_insns which ensures that the
503 reg_stat vector is made larger if the splitter creates a new
504 register. */
506 static rtx
507 combine_split_insns (rtx pattern, rtx insn)
509 rtx ret;
510 unsigned int nregs;
512 ret = split_insns (pattern, insn);
513 nregs = max_reg_num ();
514 if (nregs > VEC_length (reg_stat_type, reg_stat))
515 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
516 return ret;
519 /* This is used by find_single_use to locate an rtx in LOC that
520 contains exactly one use of DEST, which is typically either a REG
521 or CC0. It returns a pointer to the innermost rtx expression
522 containing DEST. Appearances of DEST that are being used to
523 totally replace it are not counted. */
525 static rtx *
526 find_single_use_1 (rtx dest, rtx *loc)
528 rtx x = *loc;
529 enum rtx_code code = GET_CODE (x);
530 rtx *result = NULL;
531 rtx *this_result;
532 int i;
533 const char *fmt;
535 switch (code)
537 case CONST_INT:
538 case CONST:
539 case LABEL_REF:
540 case SYMBOL_REF:
541 case CONST_DOUBLE:
542 case CONST_VECTOR:
543 case CLOBBER:
544 return 0;
546 case SET:
547 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
548 of a REG that occupies all of the REG, the insn uses DEST if
549 it is mentioned in the destination or the source. Otherwise, we
550 need just check the source. */
551 if (GET_CODE (SET_DEST (x)) != CC0
552 && GET_CODE (SET_DEST (x)) != PC
553 && !REG_P (SET_DEST (x))
554 && ! (GET_CODE (SET_DEST (x)) == SUBREG
555 && REG_P (SUBREG_REG (SET_DEST (x)))
556 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
557 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
558 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
559 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
560 break;
562 return find_single_use_1 (dest, &SET_SRC (x));
564 case MEM:
565 case SUBREG:
566 return find_single_use_1 (dest, &XEXP (x, 0));
568 default:
569 break;
572 /* If it wasn't one of the common cases above, check each expression and
573 vector of this code. Look for a unique usage of DEST. */
575 fmt = GET_RTX_FORMAT (code);
576 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
578 if (fmt[i] == 'e')
580 if (dest == XEXP (x, i)
581 || (REG_P (dest) && REG_P (XEXP (x, i))
582 && REGNO (dest) == REGNO (XEXP (x, i))))
583 this_result = loc;
584 else
585 this_result = find_single_use_1 (dest, &XEXP (x, i));
587 if (result == NULL)
588 result = this_result;
589 else if (this_result)
590 /* Duplicate usage. */
591 return NULL;
593 else if (fmt[i] == 'E')
595 int j;
597 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
599 if (XVECEXP (x, i, j) == dest
600 || (REG_P (dest)
601 && REG_P (XVECEXP (x, i, j))
602 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
603 this_result = loc;
604 else
605 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
607 if (result == NULL)
608 result = this_result;
609 else if (this_result)
610 return NULL;
615 return result;
619 /* See if DEST, produced in INSN, is used only a single time in the
620 sequel. If so, return a pointer to the innermost rtx expression in which
621 it is used.
623 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
625 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
626 care about REG_DEAD notes or LOG_LINKS.
628 Otherwise, we find the single use by finding an insn that has a
629 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
630 only referenced once in that insn, we know that it must be the first
631 and last insn referencing DEST. */
633 static rtx *
634 find_single_use (rtx dest, rtx insn, rtx *ploc)
636 basic_block bb;
637 rtx next;
638 rtx *result;
639 struct insn_link *link;
641 #ifdef HAVE_cc0
642 if (dest == cc0_rtx)
644 next = NEXT_INSN (insn);
645 if (next == 0
646 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
647 return 0;
649 result = find_single_use_1 (dest, &PATTERN (next));
650 if (result && ploc)
651 *ploc = next;
652 return result;
654 #endif
656 if (!REG_P (dest))
657 return 0;
659 bb = BLOCK_FOR_INSN (insn);
660 for (next = NEXT_INSN (insn);
661 next && BLOCK_FOR_INSN (next) == bb;
662 next = NEXT_INSN (next))
663 if (INSN_P (next) && dead_or_set_p (next, dest))
665 FOR_EACH_LOG_LINK (link, next)
666 if (link->insn == insn)
667 break;
669 if (link)
671 result = find_single_use_1 (dest, &PATTERN (next));
672 if (ploc)
673 *ploc = next;
674 return result;
678 return 0;
681 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
682 insn. The substitution can be undone by undo_all. If INTO is already
683 set to NEWVAL, do not record this change. Because computing NEWVAL might
684 also call SUBST, we have to compute it before we put anything into
685 the undo table. */
687 static void
688 do_SUBST (rtx *into, rtx newval)
690 struct undo *buf;
691 rtx oldval = *into;
693 if (oldval == newval)
694 return;
696 /* We'd like to catch as many invalid transformations here as
697 possible. Unfortunately, there are way too many mode changes
698 that are perfectly valid, so we'd waste too much effort for
699 little gain doing the checks here. Focus on catching invalid
700 transformations involving integer constants. */
701 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
702 && CONST_INT_P (newval))
704 /* Sanity check that we're replacing oldval with a CONST_INT
705 that is a valid sign-extension for the original mode. */
706 gcc_assert (INTVAL (newval)
707 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
709 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
710 CONST_INT is not valid, because after the replacement, the
711 original mode would be gone. Unfortunately, we can't tell
712 when do_SUBST is called to replace the operand thereof, so we
713 perform this test on oldval instead, checking whether an
714 invalid replacement took place before we got here. */
715 gcc_assert (!(GET_CODE (oldval) == SUBREG
716 && CONST_INT_P (SUBREG_REG (oldval))));
717 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
718 && CONST_INT_P (XEXP (oldval, 0))));
721 if (undobuf.frees)
722 buf = undobuf.frees, undobuf.frees = buf->next;
723 else
724 buf = XNEW (struct undo);
726 buf->kind = UNDO_RTX;
727 buf->where.r = into;
728 buf->old_contents.r = oldval;
729 *into = newval;
731 buf->next = undobuf.undos, undobuf.undos = buf;
734 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
736 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
737 for the value of a HOST_WIDE_INT value (including CONST_INT) is
738 not safe. */
740 static void
741 do_SUBST_INT (int *into, int newval)
743 struct undo *buf;
744 int oldval = *into;
746 if (oldval == newval)
747 return;
749 if (undobuf.frees)
750 buf = undobuf.frees, undobuf.frees = buf->next;
751 else
752 buf = XNEW (struct undo);
754 buf->kind = UNDO_INT;
755 buf->where.i = into;
756 buf->old_contents.i = oldval;
757 *into = newval;
759 buf->next = undobuf.undos, undobuf.undos = buf;
762 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
764 /* Similar to SUBST, but just substitute the mode. This is used when
765 changing the mode of a pseudo-register, so that any other
766 references to the entry in the regno_reg_rtx array will change as
767 well. */
769 static void
770 do_SUBST_MODE (rtx *into, enum machine_mode newval)
772 struct undo *buf;
773 enum machine_mode oldval = GET_MODE (*into);
775 if (oldval == newval)
776 return;
778 if (undobuf.frees)
779 buf = undobuf.frees, undobuf.frees = buf->next;
780 else
781 buf = XNEW (struct undo);
783 buf->kind = UNDO_MODE;
784 buf->where.r = into;
785 buf->old_contents.m = oldval;
786 adjust_reg_mode (*into, newval);
788 buf->next = undobuf.undos, undobuf.undos = buf;
791 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
793 /* Subroutine of try_combine. Determine whether the replacement patterns
794 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
795 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
796 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
797 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
798 of all the instructions can be estimated and the replacements are more
799 expensive than the original sequence. */
801 static bool
802 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
803 rtx newi2pat, rtx newotherpat)
805 int i0_cost, i1_cost, i2_cost, i3_cost;
806 int new_i2_cost, new_i3_cost;
807 int old_cost, new_cost;
809 /* Lookup the original insn_rtx_costs. */
810 i2_cost = INSN_COST (i2);
811 i3_cost = INSN_COST (i3);
813 if (i1)
815 i1_cost = INSN_COST (i1);
816 if (i0)
818 i0_cost = INSN_COST (i0);
819 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
820 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
822 else
824 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
825 ? i1_cost + i2_cost + i3_cost : 0);
826 i0_cost = 0;
829 else
831 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
832 i1_cost = i0_cost = 0;
835 /* Calculate the replacement insn_rtx_costs. */
836 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
837 if (newi2pat)
839 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
840 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
841 ? new_i2_cost + new_i3_cost : 0;
843 else
845 new_cost = new_i3_cost;
846 new_i2_cost = 0;
849 if (undobuf.other_insn)
851 int old_other_cost, new_other_cost;
853 old_other_cost = INSN_COST (undobuf.other_insn);
854 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
855 if (old_other_cost > 0 && new_other_cost > 0)
857 old_cost += old_other_cost;
858 new_cost += new_other_cost;
860 else
861 old_cost = 0;
864 /* Disallow this combination if both new_cost and old_cost are greater than
865 zero, and new_cost is greater than old cost. */
866 if (old_cost > 0 && new_cost > old_cost)
868 if (dump_file)
870 if (i0)
872 fprintf (dump_file,
873 "rejecting combination of insns %d, %d, %d and %d\n",
874 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
875 INSN_UID (i3));
876 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
877 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
879 else if (i1)
881 fprintf (dump_file,
882 "rejecting combination of insns %d, %d and %d\n",
883 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
884 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
885 i1_cost, i2_cost, i3_cost, old_cost);
887 else
889 fprintf (dump_file,
890 "rejecting combination of insns %d and %d\n",
891 INSN_UID (i2), INSN_UID (i3));
892 fprintf (dump_file, "original costs %d + %d = %d\n",
893 i2_cost, i3_cost, old_cost);
896 if (newi2pat)
898 fprintf (dump_file, "replacement costs %d + %d = %d\n",
899 new_i2_cost, new_i3_cost, new_cost);
901 else
902 fprintf (dump_file, "replacement cost %d\n", new_cost);
905 return false;
908 /* Update the uid_insn_cost array with the replacement costs. */
909 INSN_COST (i2) = new_i2_cost;
910 INSN_COST (i3) = new_i3_cost;
911 if (i1)
913 INSN_COST (i1) = 0;
914 if (i0)
915 INSN_COST (i0) = 0;
918 return true;
922 /* Delete any insns that copy a register to itself. */
924 static void
925 delete_noop_moves (void)
927 rtx insn, next;
928 basic_block bb;
930 FOR_EACH_BB (bb)
932 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
934 next = NEXT_INSN (insn);
935 if (INSN_P (insn) && noop_move_p (insn))
937 if (dump_file)
938 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
940 delete_insn_and_edges (insn);
947 /* Fill in log links field for all insns. */
949 static void
950 create_log_links (void)
952 basic_block bb;
953 rtx *next_use, insn;
954 df_ref *def_vec, *use_vec;
956 next_use = XCNEWVEC (rtx, max_reg_num ());
958 /* Pass through each block from the end, recording the uses of each
959 register and establishing log links when def is encountered.
960 Note that we do not clear next_use array in order to save time,
961 so we have to test whether the use is in the same basic block as def.
963 There are a few cases below when we do not consider the definition or
964 usage -- these are taken from original flow.c did. Don't ask me why it is
965 done this way; I don't know and if it works, I don't want to know. */
967 FOR_EACH_BB (bb)
969 FOR_BB_INSNS_REVERSE (bb, insn)
971 if (!NONDEBUG_INSN_P (insn))
972 continue;
974 /* Log links are created only once. */
975 gcc_assert (!LOG_LINKS (insn));
977 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
979 df_ref def = *def_vec;
980 int regno = DF_REF_REGNO (def);
981 rtx use_insn;
983 if (!next_use[regno])
984 continue;
986 /* Do not consider if it is pre/post modification in MEM. */
987 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
988 continue;
990 /* Do not make the log link for frame pointer. */
991 if ((regno == FRAME_POINTER_REGNUM
992 && (! reload_completed || frame_pointer_needed))
993 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
994 || (regno == HARD_FRAME_POINTER_REGNUM
995 && (! reload_completed || frame_pointer_needed))
996 #endif
997 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
998 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
999 #endif
1001 continue;
1003 use_insn = next_use[regno];
1004 if (BLOCK_FOR_INSN (use_insn) == bb)
1006 /* flow.c claimed:
1008 We don't build a LOG_LINK for hard registers contained
1009 in ASM_OPERANDs. If these registers get replaced,
1010 we might wind up changing the semantics of the insn,
1011 even if reload can make what appear to be valid
1012 assignments later. */
1013 if (regno >= FIRST_PSEUDO_REGISTER
1014 || asm_noperands (PATTERN (use_insn)) < 0)
1016 /* Don't add duplicate links between instructions. */
1017 struct insn_link *links;
1018 FOR_EACH_LOG_LINK (links, use_insn)
1019 if (insn == links->insn)
1020 break;
1022 if (!links)
1023 LOG_LINKS (use_insn)
1024 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1027 next_use[regno] = NULL_RTX;
1030 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1032 df_ref use = *use_vec;
1033 int regno = DF_REF_REGNO (use);
1035 /* Do not consider the usage of the stack pointer
1036 by function call. */
1037 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1038 continue;
1040 next_use[regno] = insn;
1045 free (next_use);
1048 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1049 true if we found a LOG_LINK that proves that A feeds B. This only works
1050 if there are no instructions between A and B which could have a link
1051 depending on A, since in that case we would not record a link for B.
1052 We also check the implicit dependency created by a cc0 setter/user
1053 pair. */
1055 static bool
1056 insn_a_feeds_b (rtx a, rtx b)
1058 struct insn_link *links;
1059 FOR_EACH_LOG_LINK (links, b)
1060 if (links->insn == a)
1061 return true;
1062 #ifdef HAVE_cc0
1063 if (sets_cc0_p (a))
1064 return true;
1065 #endif
1066 return false;
1069 /* Main entry point for combiner. F is the first insn of the function.
1070 NREGS is the first unused pseudo-reg number.
1072 Return nonzero if the combiner has turned an indirect jump
1073 instruction into a direct jump. */
1074 static int
1075 combine_instructions (rtx f, unsigned int nregs)
1077 rtx insn, next;
1078 #ifdef HAVE_cc0
1079 rtx prev;
1080 #endif
1081 struct insn_link *links, *nextlinks;
1082 rtx first;
1083 basic_block last_bb;
1085 int new_direct_jump_p = 0;
1087 for (first = f; first && !INSN_P (first); )
1088 first = NEXT_INSN (first);
1089 if (!first)
1090 return 0;
1092 combine_attempts = 0;
1093 combine_merges = 0;
1094 combine_extras = 0;
1095 combine_successes = 0;
1097 rtl_hooks = combine_rtl_hooks;
1099 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1101 init_recog_no_volatile ();
1103 /* Allocate array for insn info. */
1104 max_uid_known = get_max_uid ();
1105 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1106 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1107 gcc_obstack_init (&insn_link_obstack);
1109 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1111 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1112 problems when, for example, we have j <<= 1 in a loop. */
1114 nonzero_sign_valid = 0;
1115 label_tick = label_tick_ebb_start = 1;
1117 /* Scan all SETs and see if we can deduce anything about what
1118 bits are known to be zero for some registers and how many copies
1119 of the sign bit are known to exist for those registers.
1121 Also set any known values so that we can use it while searching
1122 for what bits are known to be set. */
1124 setup_incoming_promotions (first);
1125 /* Allow the entry block and the first block to fall into the same EBB.
1126 Conceptually the incoming promotions are assigned to the entry block. */
1127 last_bb = ENTRY_BLOCK_PTR;
1129 create_log_links ();
1130 FOR_EACH_BB (this_basic_block)
1132 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1133 last_call_luid = 0;
1134 mem_last_set = -1;
1136 label_tick++;
1137 if (!single_pred_p (this_basic_block)
1138 || single_pred (this_basic_block) != last_bb)
1139 label_tick_ebb_start = label_tick;
1140 last_bb = this_basic_block;
1142 FOR_BB_INSNS (this_basic_block, insn)
1143 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1145 #ifdef AUTO_INC_DEC
1146 rtx links;
1147 #endif
1149 subst_low_luid = DF_INSN_LUID (insn);
1150 subst_insn = insn;
1152 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1153 insn);
1154 record_dead_and_set_regs (insn);
1156 #ifdef AUTO_INC_DEC
1157 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1158 if (REG_NOTE_KIND (links) == REG_INC)
1159 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1160 insn);
1161 #endif
1163 /* Record the current insn_rtx_cost of this instruction. */
1164 if (NONJUMP_INSN_P (insn))
1165 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1166 optimize_this_for_speed_p);
1167 if (dump_file)
1168 fprintf(dump_file, "insn_cost %d: %d\n",
1169 INSN_UID (insn), INSN_COST (insn));
1173 nonzero_sign_valid = 1;
1175 /* Now scan all the insns in forward order. */
1176 label_tick = label_tick_ebb_start = 1;
1177 init_reg_last ();
1178 setup_incoming_promotions (first);
1179 last_bb = ENTRY_BLOCK_PTR;
1181 FOR_EACH_BB (this_basic_block)
1183 rtx last_combined_insn = NULL_RTX;
1184 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1185 last_call_luid = 0;
1186 mem_last_set = -1;
1188 label_tick++;
1189 if (!single_pred_p (this_basic_block)
1190 || single_pred (this_basic_block) != last_bb)
1191 label_tick_ebb_start = label_tick;
1192 last_bb = this_basic_block;
1194 rtl_profile_for_bb (this_basic_block);
1195 for (insn = BB_HEAD (this_basic_block);
1196 insn != NEXT_INSN (BB_END (this_basic_block));
1197 insn = next ? next : NEXT_INSN (insn))
1199 next = 0;
1200 if (NONDEBUG_INSN_P (insn))
1202 while (last_combined_insn
1203 && INSN_DELETED_P (last_combined_insn))
1204 last_combined_insn = PREV_INSN (last_combined_insn);
1205 if (last_combined_insn == NULL_RTX
1206 || BARRIER_P (last_combined_insn)
1207 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1208 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1209 last_combined_insn = insn;
1211 /* See if we know about function return values before this
1212 insn based upon SUBREG flags. */
1213 check_promoted_subreg (insn, PATTERN (insn));
1215 /* See if we can find hardregs and subreg of pseudos in
1216 narrower modes. This could help turning TRUNCATEs
1217 into SUBREGs. */
1218 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1220 /* Try this insn with each insn it links back to. */
1222 FOR_EACH_LOG_LINK (links, insn)
1223 if ((next = try_combine (insn, links->insn, NULL_RTX,
1224 NULL_RTX, &new_direct_jump_p,
1225 last_combined_insn)) != 0)
1226 goto retry;
1228 /* Try each sequence of three linked insns ending with this one. */
1230 FOR_EACH_LOG_LINK (links, insn)
1232 rtx link = links->insn;
1234 /* If the linked insn has been replaced by a note, then there
1235 is no point in pursuing this chain any further. */
1236 if (NOTE_P (link))
1237 continue;
1239 FOR_EACH_LOG_LINK (nextlinks, link)
1240 if ((next = try_combine (insn, link, nextlinks->insn,
1241 NULL_RTX, &new_direct_jump_p,
1242 last_combined_insn)) != 0)
1243 goto retry;
1246 #ifdef HAVE_cc0
1247 /* Try to combine a jump insn that uses CC0
1248 with a preceding insn that sets CC0, and maybe with its
1249 logical predecessor as well.
1250 This is how we make decrement-and-branch insns.
1251 We need this special code because data flow connections
1252 via CC0 do not get entered in LOG_LINKS. */
1254 if (JUMP_P (insn)
1255 && (prev = prev_nonnote_insn (insn)) != 0
1256 && NONJUMP_INSN_P (prev)
1257 && sets_cc0_p (PATTERN (prev)))
1259 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1260 &new_direct_jump_p,
1261 last_combined_insn)) != 0)
1262 goto retry;
1264 FOR_EACH_LOG_LINK (nextlinks, prev)
1265 if ((next = try_combine (insn, prev, nextlinks->insn,
1266 NULL_RTX, &new_direct_jump_p,
1267 last_combined_insn)) != 0)
1268 goto retry;
1271 /* Do the same for an insn that explicitly references CC0. */
1272 if (NONJUMP_INSN_P (insn)
1273 && (prev = prev_nonnote_insn (insn)) != 0
1274 && NONJUMP_INSN_P (prev)
1275 && sets_cc0_p (PATTERN (prev))
1276 && GET_CODE (PATTERN (insn)) == SET
1277 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1279 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1280 &new_direct_jump_p,
1281 last_combined_insn)) != 0)
1282 goto retry;
1284 FOR_EACH_LOG_LINK (nextlinks, prev)
1285 if ((next = try_combine (insn, prev, nextlinks->insn,
1286 NULL_RTX, &new_direct_jump_p,
1287 last_combined_insn)) != 0)
1288 goto retry;
1291 /* Finally, see if any of the insns that this insn links to
1292 explicitly references CC0. If so, try this insn, that insn,
1293 and its predecessor if it sets CC0. */
1294 FOR_EACH_LOG_LINK (links, insn)
1295 if (NONJUMP_INSN_P (links->insn)
1296 && GET_CODE (PATTERN (links->insn)) == SET
1297 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1298 && (prev = prev_nonnote_insn (links->insn)) != 0
1299 && NONJUMP_INSN_P (prev)
1300 && sets_cc0_p (PATTERN (prev))
1301 && (next = try_combine (insn, links->insn,
1302 prev, NULL_RTX, &new_direct_jump_p,
1303 last_combined_insn)) != 0)
1304 goto retry;
1305 #endif
1307 /* Try combining an insn with two different insns whose results it
1308 uses. */
1309 FOR_EACH_LOG_LINK (links, insn)
1310 for (nextlinks = links->next; nextlinks;
1311 nextlinks = nextlinks->next)
1312 if ((next = try_combine (insn, links->insn,
1313 nextlinks->insn, NULL_RTX,
1314 &new_direct_jump_p,
1315 last_combined_insn)) != 0)
1316 goto retry;
1318 /* Try four-instruction combinations. */
1319 FOR_EACH_LOG_LINK (links, insn)
1321 struct insn_link *next1;
1322 rtx link = links->insn;
1324 /* If the linked insn has been replaced by a note, then there
1325 is no point in pursuing this chain any further. */
1326 if (NOTE_P (link))
1327 continue;
1329 FOR_EACH_LOG_LINK (next1, link)
1331 rtx link1 = next1->insn;
1332 if (NOTE_P (link1))
1333 continue;
1334 /* I0 -> I1 -> I2 -> I3. */
1335 FOR_EACH_LOG_LINK (nextlinks, link1)
1336 if ((next = try_combine (insn, link, link1,
1337 nextlinks->insn,
1338 &new_direct_jump_p,
1339 last_combined_insn)) != 0)
1340 goto retry;
1341 /* I0, I1 -> I2, I2 -> I3. */
1342 for (nextlinks = next1->next; nextlinks;
1343 nextlinks = nextlinks->next)
1344 if ((next = try_combine (insn, link, link1,
1345 nextlinks->insn,
1346 &new_direct_jump_p,
1347 last_combined_insn)) != 0)
1348 goto retry;
1351 for (next1 = links->next; next1; next1 = next1->next)
1353 rtx link1 = next1->insn;
1354 if (NOTE_P (link1))
1355 continue;
1356 /* I0 -> I2; I1, I2 -> I3. */
1357 FOR_EACH_LOG_LINK (nextlinks, link)
1358 if ((next = try_combine (insn, link, link1,
1359 nextlinks->insn,
1360 &new_direct_jump_p,
1361 last_combined_insn)) != 0)
1362 goto retry;
1363 /* I0 -> I1; I1, I2 -> I3. */
1364 FOR_EACH_LOG_LINK (nextlinks, link1)
1365 if ((next = try_combine (insn, link, link1,
1366 nextlinks->insn,
1367 &new_direct_jump_p,
1368 last_combined_insn)) != 0)
1369 goto retry;
1373 /* Try this insn with each REG_EQUAL note it links back to. */
1374 FOR_EACH_LOG_LINK (links, insn)
1376 rtx set, note;
1377 rtx temp = links->insn;
1378 if ((set = single_set (temp)) != 0
1379 && (note = find_reg_equal_equiv_note (temp)) != 0
1380 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1381 /* Avoid using a register that may already been marked
1382 dead by an earlier instruction. */
1383 && ! unmentioned_reg_p (note, SET_SRC (set))
1384 && (GET_MODE (note) == VOIDmode
1385 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1386 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1388 /* Temporarily replace the set's source with the
1389 contents of the REG_EQUAL note. The insn will
1390 be deleted or recognized by try_combine. */
1391 rtx orig = SET_SRC (set);
1392 SET_SRC (set) = note;
1393 i2mod = temp;
1394 i2mod_old_rhs = copy_rtx (orig);
1395 i2mod_new_rhs = copy_rtx (note);
1396 next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1397 &new_direct_jump_p,
1398 last_combined_insn);
1399 i2mod = NULL_RTX;
1400 if (next)
1401 goto retry;
1402 SET_SRC (set) = orig;
1406 if (!NOTE_P (insn))
1407 record_dead_and_set_regs (insn);
1409 retry:
1415 default_rtl_profile ();
1416 clear_bb_flags ();
1417 new_direct_jump_p |= purge_all_dead_edges ();
1418 delete_noop_moves ();
1420 /* Clean up. */
1421 obstack_free (&insn_link_obstack, NULL);
1422 free (uid_log_links);
1423 free (uid_insn_cost);
1424 VEC_free (reg_stat_type, heap, reg_stat);
1427 struct undo *undo, *next;
1428 for (undo = undobuf.frees; undo; undo = next)
1430 next = undo->next;
1431 free (undo);
1433 undobuf.frees = 0;
1436 total_attempts += combine_attempts;
1437 total_merges += combine_merges;
1438 total_extras += combine_extras;
1439 total_successes += combine_successes;
1441 nonzero_sign_valid = 0;
1442 rtl_hooks = general_rtl_hooks;
1444 /* Make recognizer allow volatile MEMs again. */
1445 init_recog ();
1447 return new_direct_jump_p;
1450 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1452 static void
1453 init_reg_last (void)
1455 unsigned int i;
1456 reg_stat_type *p;
1458 FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
1459 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1462 /* Set up any promoted values for incoming argument registers. */
1464 static void
1465 setup_incoming_promotions (rtx first)
1467 tree arg;
1468 bool strictly_local = false;
1470 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1471 arg = DECL_CHAIN (arg))
1473 rtx x, reg = DECL_INCOMING_RTL (arg);
1474 int uns1, uns3;
1475 enum machine_mode mode1, mode2, mode3, mode4;
1477 /* Only continue if the incoming argument is in a register. */
1478 if (!REG_P (reg))
1479 continue;
1481 /* Determine, if possible, whether all call sites of the current
1482 function lie within the current compilation unit. (This does
1483 take into account the exporting of a function via taking its
1484 address, and so forth.) */
1485 strictly_local = cgraph_local_info (current_function_decl)->local;
1487 /* The mode and signedness of the argument before any promotions happen
1488 (equal to the mode of the pseudo holding it at that stage). */
1489 mode1 = TYPE_MODE (TREE_TYPE (arg));
1490 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1492 /* The mode and signedness of the argument after any source language and
1493 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1494 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1495 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1497 /* The mode and signedness of the argument as it is actually passed,
1498 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1499 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1500 TREE_TYPE (cfun->decl), 0);
1502 /* The mode of the register in which the argument is being passed. */
1503 mode4 = GET_MODE (reg);
1505 /* Eliminate sign extensions in the callee when:
1506 (a) A mode promotion has occurred; */
1507 if (mode1 == mode3)
1508 continue;
1509 /* (b) The mode of the register is the same as the mode of
1510 the argument as it is passed; */
1511 if (mode3 != mode4)
1512 continue;
1513 /* (c) There's no language level extension; */
1514 if (mode1 == mode2)
1516 /* (c.1) All callers are from the current compilation unit. If that's
1517 the case we don't have to rely on an ABI, we only have to know
1518 what we're generating right now, and we know that we will do the
1519 mode1 to mode2 promotion with the given sign. */
1520 else if (!strictly_local)
1521 continue;
1522 /* (c.2) The combination of the two promotions is useful. This is
1523 true when the signs match, or if the first promotion is unsigned.
1524 In the later case, (sign_extend (zero_extend x)) is the same as
1525 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1526 else if (uns1)
1527 uns3 = true;
1528 else if (uns3)
1529 continue;
1531 /* Record that the value was promoted from mode1 to mode3,
1532 so that any sign extension at the head of the current
1533 function may be eliminated. */
1534 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1535 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1536 record_value_for_reg (reg, first, x);
1540 /* Called via note_stores. If X is a pseudo that is narrower than
1541 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1543 If we are setting only a portion of X and we can't figure out what
1544 portion, assume all bits will be used since we don't know what will
1545 be happening.
1547 Similarly, set how many bits of X are known to be copies of the sign bit
1548 at all locations in the function. This is the smallest number implied
1549 by any set of X. */
1551 static void
1552 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1554 rtx insn = (rtx) data;
1555 unsigned int num;
1557 if (REG_P (x)
1558 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1559 /* If this register is undefined at the start of the file, we can't
1560 say what its contents were. */
1561 && ! REGNO_REG_SET_P
1562 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1563 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1565 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1567 if (set == 0 || GET_CODE (set) == CLOBBER)
1569 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1570 rsp->sign_bit_copies = 1;
1571 return;
1574 /* If this register is being initialized using itself, and the
1575 register is uninitialized in this basic block, and there are
1576 no LOG_LINKS which set the register, then part of the
1577 register is uninitialized. In that case we can't assume
1578 anything about the number of nonzero bits.
1580 ??? We could do better if we checked this in
1581 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1582 could avoid making assumptions about the insn which initially
1583 sets the register, while still using the information in other
1584 insns. We would have to be careful to check every insn
1585 involved in the combination. */
1587 if (insn
1588 && reg_referenced_p (x, PATTERN (insn))
1589 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1590 REGNO (x)))
1592 struct insn_link *link;
1594 FOR_EACH_LOG_LINK (link, insn)
1595 if (dead_or_set_p (link->insn, x))
1596 break;
1597 if (!link)
1599 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1600 rsp->sign_bit_copies = 1;
1601 return;
1605 /* If this is a complex assignment, see if we can convert it into a
1606 simple assignment. */
1607 set = expand_field_assignment (set);
1609 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1610 set what we know about X. */
1612 if (SET_DEST (set) == x
1613 || (GET_CODE (SET_DEST (set)) == SUBREG
1614 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1615 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1616 && SUBREG_REG (SET_DEST (set)) == x))
1618 rtx src = SET_SRC (set);
1620 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1621 /* If X is narrower than a word and SRC is a non-negative
1622 constant that would appear negative in the mode of X,
1623 sign-extend it for use in reg_stat[].nonzero_bits because some
1624 machines (maybe most) will actually do the sign-extension
1625 and this is the conservative approach.
1627 ??? For 2.5, try to tighten up the MD files in this regard
1628 instead of this kludge. */
1630 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1631 && CONST_INT_P (src)
1632 && INTVAL (src) > 0
1633 && 0 != (UINTVAL (src)
1634 & ((unsigned HOST_WIDE_INT) 1
1635 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1636 src = GEN_INT (UINTVAL (src)
1637 | ((unsigned HOST_WIDE_INT) (-1)
1638 << GET_MODE_BITSIZE (GET_MODE (x))));
1639 #endif
1641 /* Don't call nonzero_bits if it cannot change anything. */
1642 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1643 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1644 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1645 if (rsp->sign_bit_copies == 0
1646 || rsp->sign_bit_copies > num)
1647 rsp->sign_bit_copies = num;
1649 else
1651 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1652 rsp->sign_bit_copies = 1;
1657 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1658 optionally insns that were previously combined into I3 or that will be
1659 combined into the merger of INSN and I3. The order is PRED, PRED2,
1660 INSN, SUCC, SUCC2, I3.
1662 Return 0 if the combination is not allowed for any reason.
1664 If the combination is allowed, *PDEST will be set to the single
1665 destination of INSN and *PSRC to the single source, and this function
1666 will return 1. */
1668 static int
1669 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1670 rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1671 rtx *pdest, rtx *psrc)
1673 int i;
1674 const_rtx set = 0;
1675 rtx src, dest;
1676 rtx p;
1677 #ifdef AUTO_INC_DEC
1678 rtx link;
1679 #endif
1680 bool all_adjacent = true;
1682 if (succ)
1684 if (succ2)
1686 if (next_active_insn (succ2) != i3)
1687 all_adjacent = false;
1688 if (next_active_insn (succ) != succ2)
1689 all_adjacent = false;
1691 else if (next_active_insn (succ) != i3)
1692 all_adjacent = false;
1693 if (next_active_insn (insn) != succ)
1694 all_adjacent = false;
1696 else if (next_active_insn (insn) != i3)
1697 all_adjacent = false;
1699 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1700 or a PARALLEL consisting of such a SET and CLOBBERs.
1702 If INSN has CLOBBER parallel parts, ignore them for our processing.
1703 By definition, these happen during the execution of the insn. When it
1704 is merged with another insn, all bets are off. If they are, in fact,
1705 needed and aren't also supplied in I3, they may be added by
1706 recog_for_combine. Otherwise, it won't match.
1708 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1709 note.
1711 Get the source and destination of INSN. If more than one, can't
1712 combine. */
1714 if (GET_CODE (PATTERN (insn)) == SET)
1715 set = PATTERN (insn);
1716 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1717 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1719 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1721 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1723 switch (GET_CODE (elt))
1725 /* This is important to combine floating point insns
1726 for the SH4 port. */
1727 case USE:
1728 /* Combining an isolated USE doesn't make sense.
1729 We depend here on combinable_i3pat to reject them. */
1730 /* The code below this loop only verifies that the inputs of
1731 the SET in INSN do not change. We call reg_set_between_p
1732 to verify that the REG in the USE does not change between
1733 I3 and INSN.
1734 If the USE in INSN was for a pseudo register, the matching
1735 insn pattern will likely match any register; combining this
1736 with any other USE would only be safe if we knew that the
1737 used registers have identical values, or if there was
1738 something to tell them apart, e.g. different modes. For
1739 now, we forgo such complicated tests and simply disallow
1740 combining of USES of pseudo registers with any other USE. */
1741 if (REG_P (XEXP (elt, 0))
1742 && GET_CODE (PATTERN (i3)) == PARALLEL)
1744 rtx i3pat = PATTERN (i3);
1745 int i = XVECLEN (i3pat, 0) - 1;
1746 unsigned int regno = REGNO (XEXP (elt, 0));
1750 rtx i3elt = XVECEXP (i3pat, 0, i);
1752 if (GET_CODE (i3elt) == USE
1753 && REG_P (XEXP (i3elt, 0))
1754 && (REGNO (XEXP (i3elt, 0)) == regno
1755 ? reg_set_between_p (XEXP (elt, 0),
1756 PREV_INSN (insn), i3)
1757 : regno >= FIRST_PSEUDO_REGISTER))
1758 return 0;
1760 while (--i >= 0);
1762 break;
1764 /* We can ignore CLOBBERs. */
1765 case CLOBBER:
1766 break;
1768 case SET:
1769 /* Ignore SETs whose result isn't used but not those that
1770 have side-effects. */
1771 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1772 && insn_nothrow_p (insn)
1773 && !side_effects_p (elt))
1774 break;
1776 /* If we have already found a SET, this is a second one and
1777 so we cannot combine with this insn. */
1778 if (set)
1779 return 0;
1781 set = elt;
1782 break;
1784 default:
1785 /* Anything else means we can't combine. */
1786 return 0;
1790 if (set == 0
1791 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1792 so don't do anything with it. */
1793 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1794 return 0;
1796 else
1797 return 0;
1799 if (set == 0)
1800 return 0;
1802 set = expand_field_assignment (set);
1803 src = SET_SRC (set), dest = SET_DEST (set);
1805 /* Don't eliminate a store in the stack pointer. */
1806 if (dest == stack_pointer_rtx
1807 /* Don't combine with an insn that sets a register to itself if it has
1808 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1809 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1810 /* Can't merge an ASM_OPERANDS. */
1811 || GET_CODE (src) == ASM_OPERANDS
1812 /* Can't merge a function call. */
1813 || GET_CODE (src) == CALL
1814 /* Don't eliminate a function call argument. */
1815 || (CALL_P (i3)
1816 && (find_reg_fusage (i3, USE, dest)
1817 || (REG_P (dest)
1818 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1819 && global_regs[REGNO (dest)])))
1820 /* Don't substitute into an incremented register. */
1821 || FIND_REG_INC_NOTE (i3, dest)
1822 || (succ && FIND_REG_INC_NOTE (succ, dest))
1823 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1824 /* Don't substitute into a non-local goto, this confuses CFG. */
1825 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1826 /* Make sure that DEST is not used after SUCC but before I3. */
1827 || (!all_adjacent
1828 && ((succ2
1829 && (reg_used_between_p (dest, succ2, i3)
1830 || reg_used_between_p (dest, succ, succ2)))
1831 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1832 /* Make sure that the value that is to be substituted for the register
1833 does not use any registers whose values alter in between. However,
1834 If the insns are adjacent, a use can't cross a set even though we
1835 think it might (this can happen for a sequence of insns each setting
1836 the same destination; last_set of that register might point to
1837 a NOTE). If INSN has a REG_EQUIV note, the register is always
1838 equivalent to the memory so the substitution is valid even if there
1839 are intervening stores. Also, don't move a volatile asm or
1840 UNSPEC_VOLATILE across any other insns. */
1841 || (! all_adjacent
1842 && (((!MEM_P (src)
1843 || ! find_reg_note (insn, REG_EQUIV, src))
1844 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1845 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1846 || GET_CODE (src) == UNSPEC_VOLATILE))
1847 /* Don't combine across a CALL_INSN, because that would possibly
1848 change whether the life span of some REGs crosses calls or not,
1849 and it is a pain to update that information.
1850 Exception: if source is a constant, moving it later can't hurt.
1851 Accept that as a special case. */
1852 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1853 return 0;
1855 /* DEST must either be a REG or CC0. */
1856 if (REG_P (dest))
1858 /* If register alignment is being enforced for multi-word items in all
1859 cases except for parameters, it is possible to have a register copy
1860 insn referencing a hard register that is not allowed to contain the
1861 mode being copied and which would not be valid as an operand of most
1862 insns. Eliminate this problem by not combining with such an insn.
1864 Also, on some machines we don't want to extend the life of a hard
1865 register. */
1867 if (REG_P (src)
1868 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1869 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1870 /* Don't extend the life of a hard register unless it is
1871 user variable (if we have few registers) or it can't
1872 fit into the desired register (meaning something special
1873 is going on).
1874 Also avoid substituting a return register into I3, because
1875 reload can't handle a conflict with constraints of other
1876 inputs. */
1877 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1878 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1879 return 0;
1881 else if (GET_CODE (dest) != CC0)
1882 return 0;
1885 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1886 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1887 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1889 /* Don't substitute for a register intended as a clobberable
1890 operand. */
1891 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1892 if (rtx_equal_p (reg, dest))
1893 return 0;
1895 /* If the clobber represents an earlyclobber operand, we must not
1896 substitute an expression containing the clobbered register.
1897 As we do not analyze the constraint strings here, we have to
1898 make the conservative assumption. However, if the register is
1899 a fixed hard reg, the clobber cannot represent any operand;
1900 we leave it up to the machine description to either accept or
1901 reject use-and-clobber patterns. */
1902 if (!REG_P (reg)
1903 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1904 || !fixed_regs[REGNO (reg)])
1905 if (reg_overlap_mentioned_p (reg, src))
1906 return 0;
1909 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1910 or not), reject, unless nothing volatile comes between it and I3 */
1912 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1914 /* Make sure neither succ nor succ2 contains a volatile reference. */
1915 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1916 return 0;
1917 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1918 return 0;
1919 /* We'll check insns between INSN and I3 below. */
1922 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1923 to be an explicit register variable, and was chosen for a reason. */
1925 if (GET_CODE (src) == ASM_OPERANDS
1926 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1927 return 0;
1929 /* If there are any volatile insns between INSN and I3, reject, because
1930 they might affect machine state. */
1932 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1933 if (INSN_P (p) && p != succ && p != succ2 && volatile_insn_p (PATTERN (p)))
1934 return 0;
1936 /* If INSN contains an autoincrement or autodecrement, make sure that
1937 register is not used between there and I3, and not already used in
1938 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1939 Also insist that I3 not be a jump; if it were one
1940 and the incremented register were spilled, we would lose. */
1942 #ifdef AUTO_INC_DEC
1943 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1944 if (REG_NOTE_KIND (link) == REG_INC
1945 && (JUMP_P (i3)
1946 || reg_used_between_p (XEXP (link, 0), insn, i3)
1947 || (pred != NULL_RTX
1948 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1949 || (pred2 != NULL_RTX
1950 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1951 || (succ != NULL_RTX
1952 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1953 || (succ2 != NULL_RTX
1954 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
1955 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1956 return 0;
1957 #endif
1959 #ifdef HAVE_cc0
1960 /* Don't combine an insn that follows a CC0-setting insn.
1961 An insn that uses CC0 must not be separated from the one that sets it.
1962 We do, however, allow I2 to follow a CC0-setting insn if that insn
1963 is passed as I1; in that case it will be deleted also.
1964 We also allow combining in this case if all the insns are adjacent
1965 because that would leave the two CC0 insns adjacent as well.
1966 It would be more logical to test whether CC0 occurs inside I1 or I2,
1967 but that would be much slower, and this ought to be equivalent. */
1969 p = prev_nonnote_insn (insn);
1970 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1971 && ! all_adjacent)
1972 return 0;
1973 #endif
1975 /* If we get here, we have passed all the tests and the combination is
1976 to be allowed. */
1978 *pdest = dest;
1979 *psrc = src;
1981 return 1;
1984 /* LOC is the location within I3 that contains its pattern or the component
1985 of a PARALLEL of the pattern. We validate that it is valid for combining.
1987 One problem is if I3 modifies its output, as opposed to replacing it
1988 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
1989 doing so would produce an insn that is not equivalent to the original insns.
1991 Consider:
1993 (set (reg:DI 101) (reg:DI 100))
1994 (set (subreg:SI (reg:DI 101) 0) <foo>)
1996 This is NOT equivalent to:
1998 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1999 (set (reg:DI 101) (reg:DI 100))])
2001 Not only does this modify 100 (in which case it might still be valid
2002 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2004 We can also run into a problem if I2 sets a register that I1
2005 uses and I1 gets directly substituted into I3 (not via I2). In that
2006 case, we would be getting the wrong value of I2DEST into I3, so we
2007 must reject the combination. This case occurs when I2 and I1 both
2008 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2009 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2010 of a SET must prevent combination from occurring. The same situation
2011 can occur for I0, in which case I0_NOT_IN_SRC is set.
2013 Before doing the above check, we first try to expand a field assignment
2014 into a set of logical operations.
2016 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2017 we place a register that is both set and used within I3. If more than one
2018 such register is detected, we fail.
2020 Return 1 if the combination is valid, zero otherwise. */
2022 static int
2023 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2024 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2026 rtx x = *loc;
2028 if (GET_CODE (x) == SET)
2030 rtx set = x ;
2031 rtx dest = SET_DEST (set);
2032 rtx src = SET_SRC (set);
2033 rtx inner_dest = dest;
2034 rtx subdest;
2036 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2037 || GET_CODE (inner_dest) == SUBREG
2038 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2039 inner_dest = XEXP (inner_dest, 0);
2041 /* Check for the case where I3 modifies its output, as discussed
2042 above. We don't want to prevent pseudos from being combined
2043 into the address of a MEM, so only prevent the combination if
2044 i1 or i2 set the same MEM. */
2045 if ((inner_dest != dest &&
2046 (!MEM_P (inner_dest)
2047 || rtx_equal_p (i2dest, inner_dest)
2048 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2049 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2050 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2051 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2052 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2054 /* This is the same test done in can_combine_p except we can't test
2055 all_adjacent; we don't have to, since this instruction will stay
2056 in place, thus we are not considering increasing the lifetime of
2057 INNER_DEST.
2059 Also, if this insn sets a function argument, combining it with
2060 something that might need a spill could clobber a previous
2061 function argument; the all_adjacent test in can_combine_p also
2062 checks this; here, we do a more specific test for this case. */
2064 || (REG_P (inner_dest)
2065 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2066 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2067 GET_MODE (inner_dest))))
2068 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2069 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2070 return 0;
2072 /* If DEST is used in I3, it is being killed in this insn, so
2073 record that for later. We have to consider paradoxical
2074 subregs here, since they kill the whole register, but we
2075 ignore partial subregs, STRICT_LOW_PART, etc.
2076 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2077 STACK_POINTER_REGNUM, since these are always considered to be
2078 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2079 subdest = dest;
2080 if (GET_CODE (subdest) == SUBREG
2081 && (GET_MODE_SIZE (GET_MODE (subdest))
2082 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2083 subdest = SUBREG_REG (subdest);
2084 if (pi3dest_killed
2085 && REG_P (subdest)
2086 && reg_referenced_p (subdest, PATTERN (i3))
2087 && REGNO (subdest) != FRAME_POINTER_REGNUM
2088 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2089 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2090 #endif
2091 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2092 && (REGNO (subdest) != ARG_POINTER_REGNUM
2093 || ! fixed_regs [REGNO (subdest)])
2094 #endif
2095 && REGNO (subdest) != STACK_POINTER_REGNUM)
2097 if (*pi3dest_killed)
2098 return 0;
2100 *pi3dest_killed = subdest;
2104 else if (GET_CODE (x) == PARALLEL)
2106 int i;
2108 for (i = 0; i < XVECLEN (x, 0); i++)
2109 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2110 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2111 return 0;
2114 return 1;
2117 /* Return 1 if X is an arithmetic expression that contains a multiplication
2118 and division. We don't count multiplications by powers of two here. */
2120 static int
2121 contains_muldiv (rtx x)
2123 switch (GET_CODE (x))
2125 case MOD: case DIV: case UMOD: case UDIV:
2126 return 1;
2128 case MULT:
2129 return ! (CONST_INT_P (XEXP (x, 1))
2130 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2131 default:
2132 if (BINARY_P (x))
2133 return contains_muldiv (XEXP (x, 0))
2134 || contains_muldiv (XEXP (x, 1));
2136 if (UNARY_P (x))
2137 return contains_muldiv (XEXP (x, 0));
2139 return 0;
2143 /* Determine whether INSN can be used in a combination. Return nonzero if
2144 not. This is used in try_combine to detect early some cases where we
2145 can't perform combinations. */
2147 static int
2148 cant_combine_insn_p (rtx insn)
2150 rtx set;
2151 rtx src, dest;
2153 /* If this isn't really an insn, we can't do anything.
2154 This can occur when flow deletes an insn that it has merged into an
2155 auto-increment address. */
2156 if (! INSN_P (insn))
2157 return 1;
2159 /* Never combine loads and stores involving hard regs that are likely
2160 to be spilled. The register allocator can usually handle such
2161 reg-reg moves by tying. If we allow the combiner to make
2162 substitutions of likely-spilled regs, reload might die.
2163 As an exception, we allow combinations involving fixed regs; these are
2164 not available to the register allocator so there's no risk involved. */
2166 set = single_set (insn);
2167 if (! set)
2168 return 0;
2169 src = SET_SRC (set);
2170 dest = SET_DEST (set);
2171 if (GET_CODE (src) == SUBREG)
2172 src = SUBREG_REG (src);
2173 if (GET_CODE (dest) == SUBREG)
2174 dest = SUBREG_REG (dest);
2175 if (REG_P (src) && REG_P (dest)
2176 && ((HARD_REGISTER_P (src)
2177 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2178 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2179 || (HARD_REGISTER_P (dest)
2180 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2181 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2182 return 1;
2184 return 0;
2187 struct likely_spilled_retval_info
2189 unsigned regno, nregs;
2190 unsigned mask;
2193 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2194 hard registers that are known to be written to / clobbered in full. */
2195 static void
2196 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2198 struct likely_spilled_retval_info *const info =
2199 (struct likely_spilled_retval_info *) data;
2200 unsigned regno, nregs;
2201 unsigned new_mask;
2203 if (!REG_P (XEXP (set, 0)))
2204 return;
2205 regno = REGNO (x);
2206 if (regno >= info->regno + info->nregs)
2207 return;
2208 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2209 if (regno + nregs <= info->regno)
2210 return;
2211 new_mask = (2U << (nregs - 1)) - 1;
2212 if (regno < info->regno)
2213 new_mask >>= info->regno - regno;
2214 else
2215 new_mask <<= regno - info->regno;
2216 info->mask &= ~new_mask;
2219 /* Return nonzero iff part of the return value is live during INSN, and
2220 it is likely spilled. This can happen when more than one insn is needed
2221 to copy the return value, e.g. when we consider to combine into the
2222 second copy insn for a complex value. */
2224 static int
2225 likely_spilled_retval_p (rtx insn)
2227 rtx use = BB_END (this_basic_block);
2228 rtx reg, p;
2229 unsigned regno, nregs;
2230 /* We assume here that no machine mode needs more than
2231 32 hard registers when the value overlaps with a register
2232 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2233 unsigned mask;
2234 struct likely_spilled_retval_info info;
2236 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2237 return 0;
2238 reg = XEXP (PATTERN (use), 0);
2239 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2240 return 0;
2241 regno = REGNO (reg);
2242 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2243 if (nregs == 1)
2244 return 0;
2245 mask = (2U << (nregs - 1)) - 1;
2247 /* Disregard parts of the return value that are set later. */
2248 info.regno = regno;
2249 info.nregs = nregs;
2250 info.mask = mask;
2251 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2252 if (INSN_P (p))
2253 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2254 mask = info.mask;
2256 /* Check if any of the (probably) live return value registers is
2257 likely spilled. */
2258 nregs --;
2261 if ((mask & 1 << nregs)
2262 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2263 return 1;
2264 } while (nregs--);
2265 return 0;
2268 /* Adjust INSN after we made a change to its destination.
2270 Changing the destination can invalidate notes that say something about
2271 the results of the insn and a LOG_LINK pointing to the insn. */
2273 static void
2274 adjust_for_new_dest (rtx insn)
2276 /* For notes, be conservative and simply remove them. */
2277 remove_reg_equal_equiv_notes (insn);
2279 /* The new insn will have a destination that was previously the destination
2280 of an insn just above it. Call distribute_links to make a LOG_LINK from
2281 the next use of that destination. */
2282 distribute_links (alloc_insn_link (insn, NULL));
2284 df_insn_rescan (insn);
2287 /* Return TRUE if combine can reuse reg X in mode MODE.
2288 ADDED_SETS is nonzero if the original set is still required. */
2289 static bool
2290 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2292 unsigned int regno;
2294 if (!REG_P(x))
2295 return false;
2297 regno = REGNO (x);
2298 /* Allow hard registers if the new mode is legal, and occupies no more
2299 registers than the old mode. */
2300 if (regno < FIRST_PSEUDO_REGISTER)
2301 return (HARD_REGNO_MODE_OK (regno, mode)
2302 && (hard_regno_nregs[regno][GET_MODE (x)]
2303 >= hard_regno_nregs[regno][mode]));
2305 /* Or a pseudo that is only used once. */
2306 return (REG_N_SETS (regno) == 1 && !added_sets
2307 && !REG_USERVAR_P (x));
2311 /* Check whether X, the destination of a set, refers to part of
2312 the register specified by REG. */
2314 static bool
2315 reg_subword_p (rtx x, rtx reg)
2317 /* Check that reg is an integer mode register. */
2318 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2319 return false;
2321 if (GET_CODE (x) == STRICT_LOW_PART
2322 || GET_CODE (x) == ZERO_EXTRACT)
2323 x = XEXP (x, 0);
2325 return GET_CODE (x) == SUBREG
2326 && SUBREG_REG (x) == reg
2327 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2330 #ifdef AUTO_INC_DEC
2331 /* Replace auto-increment addressing modes with explicit operations to access
2332 the same addresses without modifying the corresponding registers. */
2334 static rtx
2335 cleanup_auto_inc_dec (rtx src, enum machine_mode mem_mode)
2337 rtx x = src;
2338 const RTX_CODE code = GET_CODE (x);
2339 int i;
2340 const char *fmt;
2342 switch (code)
2344 case REG:
2345 case CONST_INT:
2346 case CONST_DOUBLE:
2347 case CONST_FIXED:
2348 case CONST_VECTOR:
2349 case SYMBOL_REF:
2350 case CODE_LABEL:
2351 case PC:
2352 case CC0:
2353 case SCRATCH:
2354 /* SCRATCH must be shared because they represent distinct values. */
2355 return x;
2356 case CLOBBER:
2357 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
2358 return x;
2359 break;
2361 case CONST:
2362 if (shared_const_p (x))
2363 return x;
2364 break;
2366 case MEM:
2367 mem_mode = GET_MODE (x);
2368 break;
2370 case PRE_INC:
2371 case PRE_DEC:
2372 gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
2373 return gen_rtx_PLUS (GET_MODE (x),
2374 cleanup_auto_inc_dec (XEXP (x, 0), mem_mode),
2375 GEN_INT (code == PRE_INC
2376 ? GET_MODE_SIZE (mem_mode)
2377 : -GET_MODE_SIZE (mem_mode)));
2379 case POST_INC:
2380 case POST_DEC:
2381 case PRE_MODIFY:
2382 case POST_MODIFY:
2383 return cleanup_auto_inc_dec (code == PRE_MODIFY
2384 ? XEXP (x, 1) : XEXP (x, 0),
2385 mem_mode);
2387 default:
2388 break;
2391 /* Copy the various flags, fields, and other information. We assume
2392 that all fields need copying, and then clear the fields that should
2393 not be copied. That is the sensible default behavior, and forces
2394 us to explicitly document why we are *not* copying a flag. */
2395 x = shallow_copy_rtx (x);
2397 /* We do not copy the USED flag, which is used as a mark bit during
2398 walks over the RTL. */
2399 RTX_FLAG (x, used) = 0;
2401 /* We do not copy FRAME_RELATED for INSNs. */
2402 if (INSN_P (x))
2403 RTX_FLAG (x, frame_related) = 0;
2405 fmt = GET_RTX_FORMAT (code);
2406 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2407 if (fmt[i] == 'e')
2408 XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), mem_mode);
2409 else if (fmt[i] == 'E' || fmt[i] == 'V')
2411 int j;
2412 XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
2413 for (j = 0; j < XVECLEN (x, i); j++)
2414 XVECEXP (x, i, j)
2415 = cleanup_auto_inc_dec (XVECEXP (src, i, j), mem_mode);
2418 return x;
2420 #endif
2422 /* Auxiliary data structure for propagate_for_debug_stmt. */
2424 struct rtx_subst_pair
2426 rtx to;
2427 bool adjusted;
2430 /* DATA points to an rtx_subst_pair. Return the value that should be
2431 substituted. */
2433 static rtx
2434 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2436 struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2438 if (!rtx_equal_p (from, old_rtx))
2439 return NULL_RTX;
2440 if (!pair->adjusted)
2442 pair->adjusted = true;
2443 #ifdef AUTO_INC_DEC
2444 pair->to = cleanup_auto_inc_dec (pair->to, VOIDmode);
2445 #else
2446 pair->to = copy_rtx (pair->to);
2447 #endif
2448 pair->to = make_compound_operation (pair->to, SET);
2449 return pair->to;
2451 return copy_rtx (pair->to);
2454 /* Replace all the occurrences of DEST with SRC in DEBUG_INSNs between INSN
2455 and LAST, not including INSN, but including LAST. Also stop at the end
2456 of THIS_BASIC_BLOCK. */
2458 static void
2459 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src)
2461 rtx next, loc, end = NEXT_INSN (BB_END (this_basic_block));
2463 struct rtx_subst_pair p;
2464 p.to = src;
2465 p.adjusted = false;
2467 next = NEXT_INSN (insn);
2468 last = NEXT_INSN (last);
2469 while (next != last && next != end)
2471 insn = next;
2472 next = NEXT_INSN (insn);
2473 if (DEBUG_INSN_P (insn))
2475 loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2476 dest, propagate_for_debug_subst, &p);
2477 if (loc == INSN_VAR_LOCATION_LOC (insn))
2478 continue;
2479 INSN_VAR_LOCATION_LOC (insn) = loc;
2480 df_insn_rescan (insn);
2485 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2486 Note that the INSN should be deleted *after* removing dead edges, so
2487 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2488 but not for a (set (pc) (label_ref FOO)). */
2490 static void
2491 update_cfg_for_uncondjump (rtx insn)
2493 basic_block bb = BLOCK_FOR_INSN (insn);
2494 gcc_assert (BB_END (bb) == insn);
2496 purge_dead_edges (bb);
2498 delete_insn (insn);
2499 if (EDGE_COUNT (bb->succs) == 1)
2501 rtx insn;
2503 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2505 /* Remove barriers from the footer if there are any. */
2506 for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
2507 if (BARRIER_P (insn))
2509 if (PREV_INSN (insn))
2510 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2511 else
2512 bb->il.rtl->footer = NEXT_INSN (insn);
2513 if (NEXT_INSN (insn))
2514 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2516 else if (LABEL_P (insn))
2517 break;
2521 /* Try to combine the insns I0, I1 and I2 into I3.
2522 Here I0, I1 and I2 appear earlier than I3.
2523 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2526 If we are combining more than two insns and the resulting insn is not
2527 recognized, try splitting it into two insns. If that happens, I2 and I3
2528 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2529 Otherwise, I0, I1 and I2 are pseudo-deleted.
2531 Return 0 if the combination does not work. Then nothing is changed.
2532 If we did the combination, return the insn at which combine should
2533 resume scanning.
2535 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2536 new direct jump instruction.
2538 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2539 been I3 passed to an earlier try_combine within the same basic
2540 block. */
2542 static rtx
2543 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p,
2544 rtx last_combined_insn)
2546 /* New patterns for I3 and I2, respectively. */
2547 rtx newpat, newi2pat = 0;
2548 rtvec newpat_vec_with_clobbers = 0;
2549 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2550 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2551 dead. */
2552 int added_sets_0, added_sets_1, added_sets_2;
2553 /* Total number of SETs to put into I3. */
2554 int total_sets;
2555 /* Nonzero if I2's or I1's body now appears in I3. */
2556 int i2_is_used = 0, i1_is_used = 0;
2557 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2558 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2559 /* Contains I3 if the destination of I3 is used in its source, which means
2560 that the old life of I3 is being killed. If that usage is placed into
2561 I2 and not in I3, a REG_DEAD note must be made. */
2562 rtx i3dest_killed = 0;
2563 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2564 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2565 /* Copy of SET_SRC of I1, if needed. */
2566 rtx i1src_copy = 0;
2567 /* Set if I2DEST was reused as a scratch register. */
2568 bool i2scratch = false;
2569 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2570 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2571 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2572 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2573 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2574 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2575 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2576 /* Notes that must be added to REG_NOTES in I3 and I2. */
2577 rtx new_i3_notes, new_i2_notes;
2578 /* Notes that we substituted I3 into I2 instead of the normal case. */
2579 int i3_subst_into_i2 = 0;
2580 /* Notes that I1, I2 or I3 is a MULT operation. */
2581 int have_mult = 0;
2582 int swap_i2i3 = 0;
2583 int changed_i3_dest = 0;
2585 int maxreg;
2586 rtx temp;
2587 struct insn_link *link;
2588 rtx other_pat = 0;
2589 rtx new_other_notes;
2590 int i;
2592 /* Only try four-insn combinations when there's high likelihood of
2593 success. Look for simple insns, such as loads of constants or
2594 binary operations involving a constant. */
2595 if (i0)
2597 int i;
2598 int ngood = 0;
2599 int nshift = 0;
2601 if (!flag_expensive_optimizations)
2602 return 0;
2604 for (i = 0; i < 4; i++)
2606 rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2607 rtx set = single_set (insn);
2608 rtx src;
2609 if (!set)
2610 continue;
2611 src = SET_SRC (set);
2612 if (CONSTANT_P (src))
2614 ngood += 2;
2615 break;
2617 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2618 ngood++;
2619 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2620 || GET_CODE (src) == LSHIFTRT)
2621 nshift++;
2623 if (ngood < 2 && nshift < 2)
2624 return 0;
2627 /* Exit early if one of the insns involved can't be used for
2628 combinations. */
2629 if (cant_combine_insn_p (i3)
2630 || cant_combine_insn_p (i2)
2631 || (i1 && cant_combine_insn_p (i1))
2632 || (i0 && cant_combine_insn_p (i0))
2633 || likely_spilled_retval_p (i3))
2634 return 0;
2636 combine_attempts++;
2637 undobuf.other_insn = 0;
2639 /* Reset the hard register usage information. */
2640 CLEAR_HARD_REG_SET (newpat_used_regs);
2642 if (dump_file && (dump_flags & TDF_DETAILS))
2644 if (i0)
2645 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2646 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2647 else if (i1)
2648 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2649 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2650 else
2651 fprintf (dump_file, "\nTrying %d -> %d:\n",
2652 INSN_UID (i2), INSN_UID (i3));
2655 /* If multiple insns feed into one of I2 or I3, they can be in any
2656 order. To simplify the code below, reorder them in sequence. */
2657 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2658 temp = i2, i2 = i0, i0 = temp;
2659 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2660 temp = i1, i1 = i0, i0 = temp;
2661 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2662 temp = i1, i1 = i2, i2 = temp;
2664 added_links_insn = 0;
2666 /* First check for one important special case that the code below will
2667 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2668 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2669 we may be able to replace that destination with the destination of I3.
2670 This occurs in the common code where we compute both a quotient and
2671 remainder into a structure, in which case we want to do the computation
2672 directly into the structure to avoid register-register copies.
2674 Note that this case handles both multiple sets in I2 and also cases
2675 where I2 has a number of CLOBBERs inside the PARALLEL.
2677 We make very conservative checks below and only try to handle the
2678 most common cases of this. For example, we only handle the case
2679 where I2 and I3 are adjacent to avoid making difficult register
2680 usage tests. */
2682 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2683 && REG_P (SET_SRC (PATTERN (i3)))
2684 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2685 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2686 && GET_CODE (PATTERN (i2)) == PARALLEL
2687 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2688 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2689 below would need to check what is inside (and reg_overlap_mentioned_p
2690 doesn't support those codes anyway). Don't allow those destinations;
2691 the resulting insn isn't likely to be recognized anyway. */
2692 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2693 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2694 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2695 SET_DEST (PATTERN (i3)))
2696 && next_active_insn (i2) == i3)
2698 rtx p2 = PATTERN (i2);
2700 /* Make sure that the destination of I3,
2701 which we are going to substitute into one output of I2,
2702 is not used within another output of I2. We must avoid making this:
2703 (parallel [(set (mem (reg 69)) ...)
2704 (set (reg 69) ...)])
2705 which is not well-defined as to order of actions.
2706 (Besides, reload can't handle output reloads for this.)
2708 The problem can also happen if the dest of I3 is a memory ref,
2709 if another dest in I2 is an indirect memory ref. */
2710 for (i = 0; i < XVECLEN (p2, 0); i++)
2711 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2712 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2713 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2714 SET_DEST (XVECEXP (p2, 0, i))))
2715 break;
2717 if (i == XVECLEN (p2, 0))
2718 for (i = 0; i < XVECLEN (p2, 0); i++)
2719 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2720 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2722 combine_merges++;
2724 subst_insn = i3;
2725 subst_low_luid = DF_INSN_LUID (i2);
2727 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2728 i2src = SET_SRC (XVECEXP (p2, 0, i));
2729 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2730 i2dest_killed = dead_or_set_p (i2, i2dest);
2732 /* Replace the dest in I2 with our dest and make the resulting
2733 insn the new pattern for I3. Then skip to where we validate
2734 the pattern. Everything was set up above. */
2735 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2736 newpat = p2;
2737 i3_subst_into_i2 = 1;
2738 goto validate_replacement;
2742 /* If I2 is setting a pseudo to a constant and I3 is setting some
2743 sub-part of it to another constant, merge them by making a new
2744 constant. */
2745 if (i1 == 0
2746 && (temp = single_set (i2)) != 0
2747 && (CONST_INT_P (SET_SRC (temp))
2748 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2749 && GET_CODE (PATTERN (i3)) == SET
2750 && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2751 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2752 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2754 rtx dest = SET_DEST (PATTERN (i3));
2755 int offset = -1;
2756 int width = 0;
2758 if (GET_CODE (dest) == ZERO_EXTRACT)
2760 if (CONST_INT_P (XEXP (dest, 1))
2761 && CONST_INT_P (XEXP (dest, 2)))
2763 width = INTVAL (XEXP (dest, 1));
2764 offset = INTVAL (XEXP (dest, 2));
2765 dest = XEXP (dest, 0);
2766 if (BITS_BIG_ENDIAN)
2767 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2770 else
2772 if (GET_CODE (dest) == STRICT_LOW_PART)
2773 dest = XEXP (dest, 0);
2774 width = GET_MODE_BITSIZE (GET_MODE (dest));
2775 offset = 0;
2778 if (offset >= 0)
2780 /* If this is the low part, we're done. */
2781 if (subreg_lowpart_p (dest))
2783 /* Handle the case where inner is twice the size of outer. */
2784 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2785 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2786 offset += GET_MODE_BITSIZE (GET_MODE (dest));
2787 /* Otherwise give up for now. */
2788 else
2789 offset = -1;
2792 if (offset >= 0
2793 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2794 <= HOST_BITS_PER_DOUBLE_INT))
2796 double_int m, o, i;
2797 rtx inner = SET_SRC (PATTERN (i3));
2798 rtx outer = SET_SRC (temp);
2800 o = rtx_to_double_int (outer);
2801 i = rtx_to_double_int (inner);
2803 m = double_int_mask (width);
2804 i = double_int_and (i, m);
2805 m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2806 i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2807 o = double_int_ior (double_int_and_not (o, m), i);
2809 combine_merges++;
2810 subst_insn = i3;
2811 subst_low_luid = DF_INSN_LUID (i2);
2812 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2813 i2dest = SET_DEST (temp);
2814 i2dest_killed = dead_or_set_p (i2, i2dest);
2816 /* Replace the source in I2 with the new constant and make the
2817 resulting insn the new pattern for I3. Then skip to where we
2818 validate the pattern. Everything was set up above. */
2819 SUBST (SET_SRC (temp),
2820 immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2822 newpat = PATTERN (i2);
2824 /* The dest of I3 has been replaced with the dest of I2. */
2825 changed_i3_dest = 1;
2826 goto validate_replacement;
2830 #ifndef HAVE_cc0
2831 /* If we have no I1 and I2 looks like:
2832 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2833 (set Y OP)])
2834 make up a dummy I1 that is
2835 (set Y OP)
2836 and change I2 to be
2837 (set (reg:CC X) (compare:CC Y (const_int 0)))
2839 (We can ignore any trailing CLOBBERs.)
2841 This undoes a previous combination and allows us to match a branch-and-
2842 decrement insn. */
2844 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2845 && XVECLEN (PATTERN (i2), 0) >= 2
2846 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2847 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2848 == MODE_CC)
2849 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2850 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2851 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2852 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2853 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2854 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2856 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2857 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2858 break;
2860 if (i == 1)
2862 /* We make I1 with the same INSN_UID as I2. This gives it
2863 the same DF_INSN_LUID for value tracking. Our fake I1 will
2864 never appear in the insn stream so giving it the same INSN_UID
2865 as I2 will not cause a problem. */
2867 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2868 BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2869 INSN_LOCATOR (i2), -1, NULL_RTX);
2871 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2872 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2873 SET_DEST (PATTERN (i1)));
2876 #endif
2878 /* Verify that I2 and I1 are valid for combining. */
2879 if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2880 || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2881 &i1dest, &i1src))
2882 || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2883 &i0dest, &i0src)))
2885 undo_all ();
2886 return 0;
2889 /* Record whether I2DEST is used in I2SRC and similarly for the other
2890 cases. Knowing this will help in register status updating below. */
2891 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2892 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2893 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2894 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2895 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2896 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2897 i2dest_killed = dead_or_set_p (i2, i2dest);
2898 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2899 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2901 /* For the earlier insns, determine which of the subsequent ones they
2902 feed. */
2903 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2904 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2905 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2906 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2907 && reg_overlap_mentioned_p (i0dest, i2src))));
2909 /* Ensure that I3's pattern can be the destination of combines. */
2910 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2911 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2912 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2913 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2914 &i3dest_killed))
2916 undo_all ();
2917 return 0;
2920 /* See if any of the insns is a MULT operation. Unless one is, we will
2921 reject a combination that is, since it must be slower. Be conservative
2922 here. */
2923 if (GET_CODE (i2src) == MULT
2924 || (i1 != 0 && GET_CODE (i1src) == MULT)
2925 || (i0 != 0 && GET_CODE (i0src) == MULT)
2926 || (GET_CODE (PATTERN (i3)) == SET
2927 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2928 have_mult = 1;
2930 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2931 We used to do this EXCEPT in one case: I3 has a post-inc in an
2932 output operand. However, that exception can give rise to insns like
2933 mov r3,(r3)+
2934 which is a famous insn on the PDP-11 where the value of r3 used as the
2935 source was model-dependent. Avoid this sort of thing. */
2937 #if 0
2938 if (!(GET_CODE (PATTERN (i3)) == SET
2939 && REG_P (SET_SRC (PATTERN (i3)))
2940 && MEM_P (SET_DEST (PATTERN (i3)))
2941 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2942 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2943 /* It's not the exception. */
2944 #endif
2945 #ifdef AUTO_INC_DEC
2947 rtx link;
2948 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2949 if (REG_NOTE_KIND (link) == REG_INC
2950 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2951 || (i1 != 0
2952 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2954 undo_all ();
2955 return 0;
2958 #endif
2960 /* See if the SETs in I1 or I2 need to be kept around in the merged
2961 instruction: whenever the value set there is still needed past I3.
2962 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2964 For the SET in I1, we have two cases: If I1 and I2 independently
2965 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2966 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2967 in I1 needs to be kept around unless I1DEST dies or is set in either
2968 I2 or I3. The same consideration applies to I0. */
2970 added_sets_2 = !dead_or_set_p (i3, i2dest);
2972 if (i1)
2973 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2974 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2975 else
2976 added_sets_1 = 0;
2978 if (i0)
2979 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2980 || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
2981 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
2982 else
2983 added_sets_0 = 0;
2985 /* We are about to copy insns for the case where they need to be kept
2986 around. Check that they can be copied in the merged instruction. */
2988 if (targetm.cannot_copy_insn_p
2989 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2990 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2991 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2993 undo_all ();
2994 return 0;
2997 /* If the set in I2 needs to be kept around, we must make a copy of
2998 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2999 PATTERN (I2), we are only substituting for the original I1DEST, not into
3000 an already-substituted copy. This also prevents making self-referential
3001 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3002 I2DEST. */
3004 if (added_sets_2)
3006 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3007 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3008 else
3009 i2pat = copy_rtx (PATTERN (i2));
3012 if (added_sets_1)
3014 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3015 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3016 else
3017 i1pat = copy_rtx (PATTERN (i1));
3020 if (added_sets_0)
3022 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3023 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3024 else
3025 i0pat = copy_rtx (PATTERN (i0));
3028 combine_merges++;
3030 /* Substitute in the latest insn for the regs set by the earlier ones. */
3032 maxreg = max_reg_num ();
3034 subst_insn = i3;
3036 #ifndef HAVE_cc0
3037 /* Many machines that don't use CC0 have insns that can both perform an
3038 arithmetic operation and set the condition code. These operations will
3039 be represented as a PARALLEL with the first element of the vector
3040 being a COMPARE of an arithmetic operation with the constant zero.
3041 The second element of the vector will set some pseudo to the result
3042 of the same arithmetic operation. If we simplify the COMPARE, we won't
3043 match such a pattern and so will generate an extra insn. Here we test
3044 for this case, where both the comparison and the operation result are
3045 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3046 I2SRC. Later we will make the PARALLEL that contains I2. */
3048 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3049 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3050 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3051 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3053 rtx newpat_dest;
3054 rtx *cc_use_loc = NULL, cc_use_insn = NULL_RTX;
3055 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3056 enum machine_mode compare_mode, orig_compare_mode;
3057 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3059 newpat = PATTERN (i3);
3060 newpat_dest = SET_DEST (newpat);
3061 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3063 if (undobuf.other_insn == 0
3064 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3065 &cc_use_insn)))
3067 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3068 compare_code = simplify_compare_const (compare_code,
3069 op0, &op1);
3070 #ifdef CANONICALIZE_COMPARISON
3071 CANONICALIZE_COMPARISON (compare_code, op0, op1);
3072 #endif
3075 /* Do the rest only if op1 is const0_rtx, which may be the
3076 result of simplification. */
3077 if (op1 == const0_rtx)
3079 /* If a single use of the CC is found, prepare to modify it
3080 when SELECT_CC_MODE returns a new CC-class mode, or when
3081 the above simplify_compare_const() returned a new comparison
3082 operator. undobuf.other_insn is assigned the CC use insn
3083 when modifying it. */
3084 if (cc_use_loc)
3086 #ifdef SELECT_CC_MODE
3087 enum machine_mode new_mode
3088 = SELECT_CC_MODE (compare_code, op0, op1);
3089 if (new_mode != orig_compare_mode
3090 && can_change_dest_mode (SET_DEST (newpat),
3091 added_sets_2, new_mode))
3093 unsigned int regno = REGNO (newpat_dest);
3094 compare_mode = new_mode;
3095 if (regno < FIRST_PSEUDO_REGISTER)
3096 newpat_dest = gen_rtx_REG (compare_mode, regno);
3097 else
3099 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3100 newpat_dest = regno_reg_rtx[regno];
3103 #endif
3104 /* Cases for modifying the CC-using comparison. */
3105 if (compare_code != orig_compare_code
3106 /* ??? Do we need to verify the zero rtx? */
3107 && XEXP (*cc_use_loc, 1) == const0_rtx)
3109 /* Replace cc_use_loc with entire new RTX. */
3110 SUBST (*cc_use_loc,
3111 gen_rtx_fmt_ee (compare_code, compare_mode,
3112 newpat_dest, const0_rtx));
3113 undobuf.other_insn = cc_use_insn;
3115 else if (compare_mode != orig_compare_mode)
3117 /* Just replace the CC reg with a new mode. */
3118 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3119 undobuf.other_insn = cc_use_insn;
3123 /* Now we modify the current newpat:
3124 First, SET_DEST(newpat) is updated if the CC mode has been
3125 altered. For targets without SELECT_CC_MODE, this should be
3126 optimized away. */
3127 if (compare_mode != orig_compare_mode)
3128 SUBST (SET_DEST (newpat), newpat_dest);
3129 /* This is always done to propagate i2src into newpat. */
3130 SUBST (SET_SRC (newpat),
3131 gen_rtx_COMPARE (compare_mode, op0, op1));
3132 /* Create new version of i2pat if needed; the below PARALLEL
3133 creation needs this to work correctly. */
3134 if (! rtx_equal_p (i2src, op0))
3135 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3136 i2_is_used = 1;
3139 #endif
3141 if (i2_is_used == 0)
3143 /* It is possible that the source of I2 or I1 may be performing
3144 an unneeded operation, such as a ZERO_EXTEND of something
3145 that is known to have the high part zero. Handle that case
3146 by letting subst look at the inner insns.
3148 Another way to do this would be to have a function that tries
3149 to simplify a single insn instead of merging two or more
3150 insns. We don't do this because of the potential of infinite
3151 loops and because of the potential extra memory required.
3152 However, doing it the way we are is a bit of a kludge and
3153 doesn't catch all cases.
3155 But only do this if -fexpensive-optimizations since it slows
3156 things down and doesn't usually win.
3158 This is not done in the COMPARE case above because the
3159 unmodified I2PAT is used in the PARALLEL and so a pattern
3160 with a modified I2SRC would not match. */
3162 if (flag_expensive_optimizations)
3164 /* Pass pc_rtx so no substitutions are done, just
3165 simplifications. */
3166 if (i1)
3168 subst_low_luid = DF_INSN_LUID (i1);
3169 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3172 subst_low_luid = DF_INSN_LUID (i2);
3173 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3176 n_occurrences = 0; /* `subst' counts here */
3177 subst_low_luid = DF_INSN_LUID (i2);
3179 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3180 copy of I2SRC each time we substitute it, in order to avoid creating
3181 self-referential RTL when we will be substituting I1SRC for I1DEST
3182 later. Likewise if I0 feeds into I2, either directly or indirectly
3183 through I1, and I0DEST is in I0SRC. */
3184 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3185 (i1_feeds_i2_n && i1dest_in_i1src)
3186 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3187 && i0dest_in_i0src));
3188 substed_i2 = 1;
3190 /* Record whether I2's body now appears within I3's body. */
3191 i2_is_used = n_occurrences;
3194 /* If we already got a failure, don't try to do more. Otherwise, try to
3195 substitute I1 if we have it. */
3197 if (i1 && GET_CODE (newpat) != CLOBBER)
3199 /* Check that an autoincrement side-effect on I1 has not been lost.
3200 This happens if I1DEST is mentioned in I2 and dies there, and
3201 has disappeared from the new pattern. */
3202 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3203 && i1_feeds_i2_n
3204 && dead_or_set_p (i2, i1dest)
3205 && !reg_overlap_mentioned_p (i1dest, newpat))
3206 /* Before we can do this substitution, we must redo the test done
3207 above (see detailed comments there) that ensures I1DEST isn't
3208 mentioned in any SETs in NEWPAT that are field assignments. */
3209 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3210 0, 0, 0))
3212 undo_all ();
3213 return 0;
3216 n_occurrences = 0;
3217 subst_low_luid = DF_INSN_LUID (i1);
3219 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3220 copy of I1SRC each time we substitute it, in order to avoid creating
3221 self-referential RTL when we will be substituting I0SRC for I0DEST
3222 later. */
3223 newpat = subst (newpat, i1dest, i1src, 0, 0,
3224 i0_feeds_i1_n && i0dest_in_i0src);
3225 substed_i1 = 1;
3227 /* Record whether I1's body now appears within I3's body. */
3228 i1_is_used = n_occurrences;
3231 /* Likewise for I0 if we have it. */
3233 if (i0 && GET_CODE (newpat) != CLOBBER)
3235 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3236 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3237 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3238 && !reg_overlap_mentioned_p (i0dest, newpat))
3239 || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3240 0, 0, 0))
3242 undo_all ();
3243 return 0;
3246 /* If the following substitution will modify I1SRC, make a copy of it
3247 for the case where it is substituted for I1DEST in I2PAT later. */
3248 if (i0_feeds_i1_n && added_sets_2 && i1_feeds_i2_n)
3249 i1src_copy = copy_rtx (i1src);
3251 n_occurrences = 0;
3252 subst_low_luid = DF_INSN_LUID (i0);
3253 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3254 substed_i0 = 1;
3257 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3258 to count all the ways that I2SRC and I1SRC can be used. */
3259 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3260 && i2_is_used + added_sets_2 > 1)
3261 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3262 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3263 > 1))
3264 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3265 && (n_occurrences + added_sets_0
3266 + (added_sets_1 && i0_feeds_i1_n)
3267 + (added_sets_2 && i0_feeds_i2_n)
3268 > 1))
3269 /* Fail if we tried to make a new register. */
3270 || max_reg_num () != maxreg
3271 /* Fail if we couldn't do something and have a CLOBBER. */
3272 || GET_CODE (newpat) == CLOBBER
3273 /* Fail if this new pattern is a MULT and we didn't have one before
3274 at the outer level. */
3275 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3276 && ! have_mult))
3278 undo_all ();
3279 return 0;
3282 /* If the actions of the earlier insns must be kept
3283 in addition to substituting them into the latest one,
3284 we must make a new PARALLEL for the latest insn
3285 to hold additional the SETs. */
3287 if (added_sets_0 || added_sets_1 || added_sets_2)
3289 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3290 combine_extras++;
3292 if (GET_CODE (newpat) == PARALLEL)
3294 rtvec old = XVEC (newpat, 0);
3295 total_sets = XVECLEN (newpat, 0) + extra_sets;
3296 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3297 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3298 sizeof (old->elem[0]) * old->num_elem);
3300 else
3302 rtx old = newpat;
3303 total_sets = 1 + extra_sets;
3304 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3305 XVECEXP (newpat, 0, 0) = old;
3308 if (added_sets_0)
3309 XVECEXP (newpat, 0, --total_sets) = i0pat;
3311 if (added_sets_1)
3313 rtx t = i1pat;
3314 if (i0_feeds_i1_n)
3315 t = subst (t, i0dest, i0src, 0, 0, 0);
3317 XVECEXP (newpat, 0, --total_sets) = t;
3319 if (added_sets_2)
3321 rtx t = i2pat;
3322 if (i1_feeds_i2_n)
3323 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3324 i0_feeds_i1_n && i0dest_in_i0src);
3325 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3326 t = subst (t, i0dest, i0src, 0, 0, 0);
3328 XVECEXP (newpat, 0, --total_sets) = t;
3332 validate_replacement:
3334 /* Note which hard regs this insn has as inputs. */
3335 mark_used_regs_combine (newpat);
3337 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3338 consider splitting this pattern, we might need these clobbers. */
3339 if (i1 && GET_CODE (newpat) == PARALLEL
3340 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3342 int len = XVECLEN (newpat, 0);
3344 newpat_vec_with_clobbers = rtvec_alloc (len);
3345 for (i = 0; i < len; i++)
3346 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3349 /* Is the result of combination a valid instruction? */
3350 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3352 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3353 the second SET's destination is a register that is unused and isn't
3354 marked as an instruction that might trap in an EH region. In that case,
3355 we just need the first SET. This can occur when simplifying a divmod
3356 insn. We *must* test for this case here because the code below that
3357 splits two independent SETs doesn't handle this case correctly when it
3358 updates the register status.
3360 It's pointless doing this if we originally had two sets, one from
3361 i3, and one from i2. Combining then splitting the parallel results
3362 in the original i2 again plus an invalid insn (which we delete).
3363 The net effect is only to move instructions around, which makes
3364 debug info less accurate.
3366 Also check the case where the first SET's destination is unused.
3367 That would not cause incorrect code, but does cause an unneeded
3368 insn to remain. */
3370 if (insn_code_number < 0
3371 && !(added_sets_2 && i1 == 0)
3372 && GET_CODE (newpat) == PARALLEL
3373 && XVECLEN (newpat, 0) == 2
3374 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3375 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3376 && asm_noperands (newpat) < 0)
3378 rtx set0 = XVECEXP (newpat, 0, 0);
3379 rtx set1 = XVECEXP (newpat, 0, 1);
3381 if (((REG_P (SET_DEST (set1))
3382 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3383 || (GET_CODE (SET_DEST (set1)) == SUBREG
3384 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3385 && insn_nothrow_p (i3)
3386 && !side_effects_p (SET_SRC (set1)))
3388 newpat = set0;
3389 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3392 else if (((REG_P (SET_DEST (set0))
3393 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3394 || (GET_CODE (SET_DEST (set0)) == SUBREG
3395 && find_reg_note (i3, REG_UNUSED,
3396 SUBREG_REG (SET_DEST (set0)))))
3397 && insn_nothrow_p (i3)
3398 && !side_effects_p (SET_SRC (set0)))
3400 newpat = set1;
3401 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3403 if (insn_code_number >= 0)
3404 changed_i3_dest = 1;
3408 /* If we were combining three insns and the result is a simple SET
3409 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3410 insns. There are two ways to do this. It can be split using a
3411 machine-specific method (like when you have an addition of a large
3412 constant) or by combine in the function find_split_point. */
3414 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3415 && asm_noperands (newpat) < 0)
3417 rtx parallel, m_split, *split;
3419 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3420 use I2DEST as a scratch register will help. In the latter case,
3421 convert I2DEST to the mode of the source of NEWPAT if we can. */
3423 m_split = combine_split_insns (newpat, i3);
3425 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3426 inputs of NEWPAT. */
3428 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3429 possible to try that as a scratch reg. This would require adding
3430 more code to make it work though. */
3432 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3434 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3436 /* First try to split using the original register as a
3437 scratch register. */
3438 parallel = gen_rtx_PARALLEL (VOIDmode,
3439 gen_rtvec (2, newpat,
3440 gen_rtx_CLOBBER (VOIDmode,
3441 i2dest)));
3442 m_split = combine_split_insns (parallel, i3);
3444 /* If that didn't work, try changing the mode of I2DEST if
3445 we can. */
3446 if (m_split == 0
3447 && new_mode != GET_MODE (i2dest)
3448 && new_mode != VOIDmode
3449 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3451 enum machine_mode old_mode = GET_MODE (i2dest);
3452 rtx ni2dest;
3454 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3455 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3456 else
3458 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3459 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3462 parallel = (gen_rtx_PARALLEL
3463 (VOIDmode,
3464 gen_rtvec (2, newpat,
3465 gen_rtx_CLOBBER (VOIDmode,
3466 ni2dest))));
3467 m_split = combine_split_insns (parallel, i3);
3469 if (m_split == 0
3470 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3472 struct undo *buf;
3474 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3475 buf = undobuf.undos;
3476 undobuf.undos = buf->next;
3477 buf->next = undobuf.frees;
3478 undobuf.frees = buf;
3482 i2scratch = m_split != 0;
3485 /* If recog_for_combine has discarded clobbers, try to use them
3486 again for the split. */
3487 if (m_split == 0 && newpat_vec_with_clobbers)
3489 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3490 m_split = combine_split_insns (parallel, i3);
3493 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3495 m_split = PATTERN (m_split);
3496 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3497 if (insn_code_number >= 0)
3498 newpat = m_split;
3500 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3501 && (next_nonnote_nondebug_insn (i2) == i3
3502 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3504 rtx i2set, i3set;
3505 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3506 newi2pat = PATTERN (m_split);
3508 i3set = single_set (NEXT_INSN (m_split));
3509 i2set = single_set (m_split);
3511 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3513 /* If I2 or I3 has multiple SETs, we won't know how to track
3514 register status, so don't use these insns. If I2's destination
3515 is used between I2 and I3, we also can't use these insns. */
3517 if (i2_code_number >= 0 && i2set && i3set
3518 && (next_nonnote_nondebug_insn (i2) == i3
3519 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3520 insn_code_number = recog_for_combine (&newi3pat, i3,
3521 &new_i3_notes);
3522 if (insn_code_number >= 0)
3523 newpat = newi3pat;
3525 /* It is possible that both insns now set the destination of I3.
3526 If so, we must show an extra use of it. */
3528 if (insn_code_number >= 0)
3530 rtx new_i3_dest = SET_DEST (i3set);
3531 rtx new_i2_dest = SET_DEST (i2set);
3533 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3534 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3535 || GET_CODE (new_i3_dest) == SUBREG)
3536 new_i3_dest = XEXP (new_i3_dest, 0);
3538 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3539 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3540 || GET_CODE (new_i2_dest) == SUBREG)
3541 new_i2_dest = XEXP (new_i2_dest, 0);
3543 if (REG_P (new_i3_dest)
3544 && REG_P (new_i2_dest)
3545 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3546 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3550 /* If we can split it and use I2DEST, go ahead and see if that
3551 helps things be recognized. Verify that none of the registers
3552 are set between I2 and I3. */
3553 if (insn_code_number < 0
3554 && (split = find_split_point (&newpat, i3, false)) != 0
3555 #ifdef HAVE_cc0
3556 && REG_P (i2dest)
3557 #endif
3558 /* We need I2DEST in the proper mode. If it is a hard register
3559 or the only use of a pseudo, we can change its mode.
3560 Make sure we don't change a hard register to have a mode that
3561 isn't valid for it, or change the number of registers. */
3562 && (GET_MODE (*split) == GET_MODE (i2dest)
3563 || GET_MODE (*split) == VOIDmode
3564 || can_change_dest_mode (i2dest, added_sets_2,
3565 GET_MODE (*split)))
3566 && (next_nonnote_nondebug_insn (i2) == i3
3567 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3568 /* We can't overwrite I2DEST if its value is still used by
3569 NEWPAT. */
3570 && ! reg_referenced_p (i2dest, newpat))
3572 rtx newdest = i2dest;
3573 enum rtx_code split_code = GET_CODE (*split);
3574 enum machine_mode split_mode = GET_MODE (*split);
3575 bool subst_done = false;
3576 newi2pat = NULL_RTX;
3578 i2scratch = true;
3580 /* *SPLIT may be part of I2SRC, so make sure we have the
3581 original expression around for later debug processing.
3582 We should not need I2SRC any more in other cases. */
3583 if (MAY_HAVE_DEBUG_INSNS)
3584 i2src = copy_rtx (i2src);
3585 else
3586 i2src = NULL;
3588 /* Get NEWDEST as a register in the proper mode. We have already
3589 validated that we can do this. */
3590 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3592 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3593 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3594 else
3596 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3597 newdest = regno_reg_rtx[REGNO (i2dest)];
3601 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3602 an ASHIFT. This can occur if it was inside a PLUS and hence
3603 appeared to be a memory address. This is a kludge. */
3604 if (split_code == MULT
3605 && CONST_INT_P (XEXP (*split, 1))
3606 && INTVAL (XEXP (*split, 1)) > 0
3607 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3609 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3610 XEXP (*split, 0), GEN_INT (i)));
3611 /* Update split_code because we may not have a multiply
3612 anymore. */
3613 split_code = GET_CODE (*split);
3616 #ifdef INSN_SCHEDULING
3617 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3618 be written as a ZERO_EXTEND. */
3619 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3621 #ifdef LOAD_EXTEND_OP
3622 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3623 what it really is. */
3624 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3625 == SIGN_EXTEND)
3626 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3627 SUBREG_REG (*split)));
3628 else
3629 #endif
3630 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3631 SUBREG_REG (*split)));
3633 #endif
3635 /* Attempt to split binary operators using arithmetic identities. */
3636 if (BINARY_P (SET_SRC (newpat))
3637 && split_mode == GET_MODE (SET_SRC (newpat))
3638 && ! side_effects_p (SET_SRC (newpat)))
3640 rtx setsrc = SET_SRC (newpat);
3641 enum machine_mode mode = GET_MODE (setsrc);
3642 enum rtx_code code = GET_CODE (setsrc);
3643 rtx src_op0 = XEXP (setsrc, 0);
3644 rtx src_op1 = XEXP (setsrc, 1);
3646 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3647 if (rtx_equal_p (src_op0, src_op1))
3649 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3650 SUBST (XEXP (setsrc, 0), newdest);
3651 SUBST (XEXP (setsrc, 1), newdest);
3652 subst_done = true;
3654 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3655 else if ((code == PLUS || code == MULT)
3656 && GET_CODE (src_op0) == code
3657 && GET_CODE (XEXP (src_op0, 0)) == code
3658 && (INTEGRAL_MODE_P (mode)
3659 || (FLOAT_MODE_P (mode)
3660 && flag_unsafe_math_optimizations)))
3662 rtx p = XEXP (XEXP (src_op0, 0), 0);
3663 rtx q = XEXP (XEXP (src_op0, 0), 1);
3664 rtx r = XEXP (src_op0, 1);
3665 rtx s = src_op1;
3667 /* Split both "((X op Y) op X) op Y" and
3668 "((X op Y) op Y) op X" as "T op T" where T is
3669 "X op Y". */
3670 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3671 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3673 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3674 XEXP (src_op0, 0));
3675 SUBST (XEXP (setsrc, 0), newdest);
3676 SUBST (XEXP (setsrc, 1), newdest);
3677 subst_done = true;
3679 /* Split "((X op X) op Y) op Y)" as "T op T" where
3680 T is "X op Y". */
3681 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3683 rtx tmp = simplify_gen_binary (code, mode, p, r);
3684 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3685 SUBST (XEXP (setsrc, 0), newdest);
3686 SUBST (XEXP (setsrc, 1), newdest);
3687 subst_done = true;
3692 if (!subst_done)
3694 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3695 SUBST (*split, newdest);
3698 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3700 /* recog_for_combine might have added CLOBBERs to newi2pat.
3701 Make sure NEWPAT does not depend on the clobbered regs. */
3702 if (GET_CODE (newi2pat) == PARALLEL)
3703 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3704 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3706 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3707 if (reg_overlap_mentioned_p (reg, newpat))
3709 undo_all ();
3710 return 0;
3714 /* If the split point was a MULT and we didn't have one before,
3715 don't use one now. */
3716 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3717 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3721 /* Check for a case where we loaded from memory in a narrow mode and
3722 then sign extended it, but we need both registers. In that case,
3723 we have a PARALLEL with both loads from the same memory location.
3724 We can split this into a load from memory followed by a register-register
3725 copy. This saves at least one insn, more if register allocation can
3726 eliminate the copy.
3728 We cannot do this if the destination of the first assignment is a
3729 condition code register or cc0. We eliminate this case by making sure
3730 the SET_DEST and SET_SRC have the same mode.
3732 We cannot do this if the destination of the second assignment is
3733 a register that we have already assumed is zero-extended. Similarly
3734 for a SUBREG of such a register. */
3736 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3737 && GET_CODE (newpat) == PARALLEL
3738 && XVECLEN (newpat, 0) == 2
3739 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3740 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3741 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3742 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3743 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3744 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3745 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3746 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3747 DF_INSN_LUID (i2))
3748 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3749 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3750 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3751 (REG_P (temp)
3752 && VEC_index (reg_stat_type, reg_stat,
3753 REGNO (temp))->nonzero_bits != 0
3754 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3755 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3756 && (VEC_index (reg_stat_type, reg_stat,
3757 REGNO (temp))->nonzero_bits
3758 != GET_MODE_MASK (word_mode))))
3759 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3760 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3761 (REG_P (temp)
3762 && VEC_index (reg_stat_type, reg_stat,
3763 REGNO (temp))->nonzero_bits != 0
3764 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3765 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3766 && (VEC_index (reg_stat_type, reg_stat,
3767 REGNO (temp))->nonzero_bits
3768 != GET_MODE_MASK (word_mode)))))
3769 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3770 SET_SRC (XVECEXP (newpat, 0, 1)))
3771 && ! find_reg_note (i3, REG_UNUSED,
3772 SET_DEST (XVECEXP (newpat, 0, 0))))
3774 rtx ni2dest;
3776 newi2pat = XVECEXP (newpat, 0, 0);
3777 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3778 newpat = XVECEXP (newpat, 0, 1);
3779 SUBST (SET_SRC (newpat),
3780 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3781 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3783 if (i2_code_number >= 0)
3784 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3786 if (insn_code_number >= 0)
3787 swap_i2i3 = 1;
3790 /* Similarly, check for a case where we have a PARALLEL of two independent
3791 SETs but we started with three insns. In this case, we can do the sets
3792 as two separate insns. This case occurs when some SET allows two
3793 other insns to combine, but the destination of that SET is still live. */
3795 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3796 && GET_CODE (newpat) == PARALLEL
3797 && XVECLEN (newpat, 0) == 2
3798 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3799 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3800 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3801 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3802 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3803 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3804 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3805 XVECEXP (newpat, 0, 0))
3806 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3807 XVECEXP (newpat, 0, 1))
3808 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3809 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3811 /* Normally, it doesn't matter which of the two is done first,
3812 but the one that references cc0 can't be the second, and
3813 one which uses any regs/memory set in between i2 and i3 can't
3814 be first. */
3815 if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3816 DF_INSN_LUID (i2))
3817 #ifdef HAVE_cc0
3818 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3819 #endif
3822 newi2pat = XVECEXP (newpat, 0, 1);
3823 newpat = XVECEXP (newpat, 0, 0);
3825 else if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 0)),
3826 DF_INSN_LUID (i2))
3827 #ifdef HAVE_cc0
3828 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1))
3829 #endif
3832 newi2pat = XVECEXP (newpat, 0, 0);
3833 newpat = XVECEXP (newpat, 0, 1);
3835 else
3837 undo_all ();
3838 return 0;
3841 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3843 if (i2_code_number >= 0)
3845 /* recog_for_combine might have added CLOBBERs to newi2pat.
3846 Make sure NEWPAT does not depend on the clobbered regs. */
3847 if (GET_CODE (newi2pat) == PARALLEL)
3849 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3850 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3852 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3853 if (reg_overlap_mentioned_p (reg, newpat))
3855 undo_all ();
3856 return 0;
3861 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3865 /* If it still isn't recognized, fail and change things back the way they
3866 were. */
3867 if ((insn_code_number < 0
3868 /* Is the result a reasonable ASM_OPERANDS? */
3869 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3871 undo_all ();
3872 return 0;
3875 /* If we had to change another insn, make sure it is valid also. */
3876 if (undobuf.other_insn)
3878 CLEAR_HARD_REG_SET (newpat_used_regs);
3880 other_pat = PATTERN (undobuf.other_insn);
3881 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3882 &new_other_notes);
3884 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3886 undo_all ();
3887 return 0;
3891 #ifdef HAVE_cc0
3892 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3893 they are adjacent to each other or not. */
3895 rtx p = prev_nonnote_insn (i3);
3896 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3897 && sets_cc0_p (newi2pat))
3899 undo_all ();
3900 return 0;
3903 #endif
3905 /* Only allow this combination if insn_rtx_costs reports that the
3906 replacement instructions are cheaper than the originals. */
3907 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3909 undo_all ();
3910 return 0;
3913 if (MAY_HAVE_DEBUG_INSNS)
3915 struct undo *undo;
3917 for (undo = undobuf.undos; undo; undo = undo->next)
3918 if (undo->kind == UNDO_MODE)
3920 rtx reg = *undo->where.r;
3921 enum machine_mode new_mode = GET_MODE (reg);
3922 enum machine_mode old_mode = undo->old_contents.m;
3924 /* Temporarily revert mode back. */
3925 adjust_reg_mode (reg, old_mode);
3927 if (reg == i2dest && i2scratch)
3929 /* If we used i2dest as a scratch register with a
3930 different mode, substitute it for the original
3931 i2src while its original mode is temporarily
3932 restored, and then clear i2scratch so that we don't
3933 do it again later. */
3934 propagate_for_debug (i2, last_combined_insn, reg, i2src);
3935 i2scratch = false;
3936 /* Put back the new mode. */
3937 adjust_reg_mode (reg, new_mode);
3939 else
3941 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3942 rtx first, last;
3944 if (reg == i2dest)
3946 first = i2;
3947 last = last_combined_insn;
3949 else
3951 first = i3;
3952 last = undobuf.other_insn;
3953 gcc_assert (last);
3954 if (DF_INSN_LUID (last)
3955 < DF_INSN_LUID (last_combined_insn))
3956 last = last_combined_insn;
3959 /* We're dealing with a reg that changed mode but not
3960 meaning, so we want to turn it into a subreg for
3961 the new mode. However, because of REG sharing and
3962 because its mode had already changed, we have to do
3963 it in two steps. First, replace any debug uses of
3964 reg, with its original mode temporarily restored,
3965 with this copy we have created; then, replace the
3966 copy with the SUBREG of the original shared reg,
3967 once again changed to the new mode. */
3968 propagate_for_debug (first, last, reg, tempreg);
3969 adjust_reg_mode (reg, new_mode);
3970 propagate_for_debug (first, last, tempreg,
3971 lowpart_subreg (old_mode, reg, new_mode));
3976 /* If we will be able to accept this, we have made a
3977 change to the destination of I3. This requires us to
3978 do a few adjustments. */
3980 if (changed_i3_dest)
3982 PATTERN (i3) = newpat;
3983 adjust_for_new_dest (i3);
3986 /* We now know that we can do this combination. Merge the insns and
3987 update the status of registers and LOG_LINKS. */
3989 if (undobuf.other_insn)
3991 rtx note, next;
3993 PATTERN (undobuf.other_insn) = other_pat;
3995 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3996 are still valid. Then add any non-duplicate notes added by
3997 recog_for_combine. */
3998 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4000 next = XEXP (note, 1);
4002 if (REG_NOTE_KIND (note) == REG_UNUSED
4003 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
4004 remove_note (undobuf.other_insn, note);
4007 distribute_notes (new_other_notes, undobuf.other_insn,
4008 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
4009 NULL_RTX);
4012 if (swap_i2i3)
4014 rtx insn;
4015 struct insn_link *link;
4016 rtx ni2dest;
4018 /* I3 now uses what used to be its destination and which is now
4019 I2's destination. This requires us to do a few adjustments. */
4020 PATTERN (i3) = newpat;
4021 adjust_for_new_dest (i3);
4023 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4024 so we still will.
4026 However, some later insn might be using I2's dest and have
4027 a LOG_LINK pointing at I3. We must remove this link.
4028 The simplest way to remove the link is to point it at I1,
4029 which we know will be a NOTE. */
4031 /* newi2pat is usually a SET here; however, recog_for_combine might
4032 have added some clobbers. */
4033 if (GET_CODE (newi2pat) == PARALLEL)
4034 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4035 else
4036 ni2dest = SET_DEST (newi2pat);
4038 for (insn = NEXT_INSN (i3);
4039 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4040 || insn != BB_HEAD (this_basic_block->next_bb));
4041 insn = NEXT_INSN (insn))
4043 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4045 FOR_EACH_LOG_LINK (link, insn)
4046 if (link->insn == i3)
4047 link->insn = i1;
4049 break;
4055 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4056 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4057 rtx midnotes = 0;
4058 int from_luid;
4059 /* Compute which registers we expect to eliminate. newi2pat may be setting
4060 either i3dest or i2dest, so we must check it. Also, i1dest may be the
4061 same as i3dest, in which case newi2pat may be setting i1dest. */
4062 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4063 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4064 || !i2dest_killed
4065 ? 0 : i2dest);
4066 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4067 || (newi2pat && reg_set_p (i1dest, newi2pat))
4068 || !i1dest_killed
4069 ? 0 : i1dest);
4070 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4071 || (newi2pat && reg_set_p (i0dest, newi2pat))
4072 || !i0dest_killed
4073 ? 0 : i0dest);
4075 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4076 clear them. */
4077 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4078 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4079 if (i1)
4080 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4081 if (i0)
4082 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4084 /* Ensure that we do not have something that should not be shared but
4085 occurs multiple times in the new insns. Check this by first
4086 resetting all the `used' flags and then copying anything is shared. */
4088 reset_used_flags (i3notes);
4089 reset_used_flags (i2notes);
4090 reset_used_flags (i1notes);
4091 reset_used_flags (i0notes);
4092 reset_used_flags (newpat);
4093 reset_used_flags (newi2pat);
4094 if (undobuf.other_insn)
4095 reset_used_flags (PATTERN (undobuf.other_insn));
4097 i3notes = copy_rtx_if_shared (i3notes);
4098 i2notes = copy_rtx_if_shared (i2notes);
4099 i1notes = copy_rtx_if_shared (i1notes);
4100 i0notes = copy_rtx_if_shared (i0notes);
4101 newpat = copy_rtx_if_shared (newpat);
4102 newi2pat = copy_rtx_if_shared (newi2pat);
4103 if (undobuf.other_insn)
4104 reset_used_flags (PATTERN (undobuf.other_insn));
4106 INSN_CODE (i3) = insn_code_number;
4107 PATTERN (i3) = newpat;
4109 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4111 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4113 reset_used_flags (call_usage);
4114 call_usage = copy_rtx (call_usage);
4116 if (substed_i2)
4118 /* I2SRC must still be meaningful at this point. Some splitting
4119 operations can invalidate I2SRC, but those operations do not
4120 apply to calls. */
4121 gcc_assert (i2src);
4122 replace_rtx (call_usage, i2dest, i2src);
4125 if (substed_i1)
4126 replace_rtx (call_usage, i1dest, i1src);
4127 if (substed_i0)
4128 replace_rtx (call_usage, i0dest, i0src);
4130 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4133 if (undobuf.other_insn)
4134 INSN_CODE (undobuf.other_insn) = other_code_number;
4136 /* We had one special case above where I2 had more than one set and
4137 we replaced a destination of one of those sets with the destination
4138 of I3. In that case, we have to update LOG_LINKS of insns later
4139 in this basic block. Note that this (expensive) case is rare.
4141 Also, in this case, we must pretend that all REG_NOTEs for I2
4142 actually came from I3, so that REG_UNUSED notes from I2 will be
4143 properly handled. */
4145 if (i3_subst_into_i2)
4147 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4148 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4149 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4150 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4151 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4152 && ! find_reg_note (i2, REG_UNUSED,
4153 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4154 for (temp = NEXT_INSN (i2);
4155 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4156 || BB_HEAD (this_basic_block) != temp);
4157 temp = NEXT_INSN (temp))
4158 if (temp != i3 && INSN_P (temp))
4159 FOR_EACH_LOG_LINK (link, temp)
4160 if (link->insn == i2)
4161 link->insn = i3;
4163 if (i3notes)
4165 rtx link = i3notes;
4166 while (XEXP (link, 1))
4167 link = XEXP (link, 1);
4168 XEXP (link, 1) = i2notes;
4170 else
4171 i3notes = i2notes;
4172 i2notes = 0;
4175 LOG_LINKS (i3) = NULL;
4176 REG_NOTES (i3) = 0;
4177 LOG_LINKS (i2) = NULL;
4178 REG_NOTES (i2) = 0;
4180 if (newi2pat)
4182 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4183 propagate_for_debug (i2, last_combined_insn, i2dest, i2src);
4184 INSN_CODE (i2) = i2_code_number;
4185 PATTERN (i2) = newi2pat;
4187 else
4189 if (MAY_HAVE_DEBUG_INSNS && i2src)
4190 propagate_for_debug (i2, last_combined_insn, i2dest, i2src);
4191 SET_INSN_DELETED (i2);
4194 if (i1)
4196 LOG_LINKS (i1) = NULL;
4197 REG_NOTES (i1) = 0;
4198 if (MAY_HAVE_DEBUG_INSNS)
4199 propagate_for_debug (i1, last_combined_insn, i1dest, i1src);
4200 SET_INSN_DELETED (i1);
4203 if (i0)
4205 LOG_LINKS (i0) = NULL;
4206 REG_NOTES (i0) = 0;
4207 if (MAY_HAVE_DEBUG_INSNS)
4208 propagate_for_debug (i0, last_combined_insn, i0dest, i0src);
4209 SET_INSN_DELETED (i0);
4212 /* Get death notes for everything that is now used in either I3 or
4213 I2 and used to die in a previous insn. If we built two new
4214 patterns, move from I1 to I2 then I2 to I3 so that we get the
4215 proper movement on registers that I2 modifies. */
4217 if (i0)
4218 from_luid = DF_INSN_LUID (i0);
4219 else if (i1)
4220 from_luid = DF_INSN_LUID (i1);
4221 else
4222 from_luid = DF_INSN_LUID (i2);
4223 if (newi2pat)
4224 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4225 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4227 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4228 if (i3notes)
4229 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4230 elim_i2, elim_i1, elim_i0);
4231 if (i2notes)
4232 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4233 elim_i2, elim_i1, elim_i0);
4234 if (i1notes)
4235 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4236 elim_i2, elim_i1, elim_i0);
4237 if (i0notes)
4238 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4239 elim_i2, elim_i1, elim_i0);
4240 if (midnotes)
4241 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4242 elim_i2, elim_i1, elim_i0);
4244 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4245 know these are REG_UNUSED and want them to go to the desired insn,
4246 so we always pass it as i3. */
4248 if (newi2pat && new_i2_notes)
4249 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4250 NULL_RTX);
4252 if (new_i3_notes)
4253 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4254 NULL_RTX);
4256 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4257 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4258 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4259 in that case, it might delete I2. Similarly for I2 and I1.
4260 Show an additional death due to the REG_DEAD note we make here. If
4261 we discard it in distribute_notes, we will decrement it again. */
4263 if (i3dest_killed)
4265 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4266 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4267 NULL_RTX),
4268 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
4269 else
4270 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4271 NULL_RTX),
4272 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4273 elim_i2, elim_i1, elim_i0);
4276 if (i2dest_in_i2src)
4278 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4279 if (newi2pat && reg_set_p (i2dest, newi2pat))
4280 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4281 NULL_RTX, NULL_RTX);
4282 else
4283 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4284 NULL_RTX, NULL_RTX, NULL_RTX);
4287 if (i1dest_in_i1src)
4289 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4290 if (newi2pat && reg_set_p (i1dest, newi2pat))
4291 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4292 NULL_RTX, NULL_RTX);
4293 else
4294 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4295 NULL_RTX, NULL_RTX, NULL_RTX);
4298 if (i0dest_in_i0src)
4300 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4301 if (newi2pat && reg_set_p (i0dest, newi2pat))
4302 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4303 NULL_RTX, NULL_RTX);
4304 else
4305 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4306 NULL_RTX, NULL_RTX, NULL_RTX);
4309 distribute_links (i3links);
4310 distribute_links (i2links);
4311 distribute_links (i1links);
4312 distribute_links (i0links);
4314 if (REG_P (i2dest))
4316 struct insn_link *link;
4317 rtx i2_insn = 0, i2_val = 0, set;
4319 /* The insn that used to set this register doesn't exist, and
4320 this life of the register may not exist either. See if one of
4321 I3's links points to an insn that sets I2DEST. If it does,
4322 that is now the last known value for I2DEST. If we don't update
4323 this and I2 set the register to a value that depended on its old
4324 contents, we will get confused. If this insn is used, thing
4325 will be set correctly in combine_instructions. */
4326 FOR_EACH_LOG_LINK (link, i3)
4327 if ((set = single_set (link->insn)) != 0
4328 && rtx_equal_p (i2dest, SET_DEST (set)))
4329 i2_insn = link->insn, i2_val = SET_SRC (set);
4331 record_value_for_reg (i2dest, i2_insn, i2_val);
4333 /* If the reg formerly set in I2 died only once and that was in I3,
4334 zero its use count so it won't make `reload' do any work. */
4335 if (! added_sets_2
4336 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4337 && ! i2dest_in_i2src)
4338 INC_REG_N_SETS (REGNO (i2dest), -1);
4341 if (i1 && REG_P (i1dest))
4343 struct insn_link *link;
4344 rtx i1_insn = 0, i1_val = 0, set;
4346 FOR_EACH_LOG_LINK (link, i3)
4347 if ((set = single_set (link->insn)) != 0
4348 && rtx_equal_p (i1dest, SET_DEST (set)))
4349 i1_insn = link->insn, i1_val = SET_SRC (set);
4351 record_value_for_reg (i1dest, i1_insn, i1_val);
4353 if (! added_sets_1 && ! i1dest_in_i1src)
4354 INC_REG_N_SETS (REGNO (i1dest), -1);
4357 if (i0 && REG_P (i0dest))
4359 struct insn_link *link;
4360 rtx i0_insn = 0, i0_val = 0, set;
4362 FOR_EACH_LOG_LINK (link, i3)
4363 if ((set = single_set (link->insn)) != 0
4364 && rtx_equal_p (i0dest, SET_DEST (set)))
4365 i0_insn = link->insn, i0_val = SET_SRC (set);
4367 record_value_for_reg (i0dest, i0_insn, i0_val);
4369 if (! added_sets_0 && ! i0dest_in_i0src)
4370 INC_REG_N_SETS (REGNO (i0dest), -1);
4373 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4374 been made to this insn. The order of
4375 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
4376 can affect nonzero_bits of newpat */
4377 if (newi2pat)
4378 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4379 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4382 if (undobuf.other_insn != NULL_RTX)
4384 if (dump_file)
4386 fprintf (dump_file, "modifying other_insn ");
4387 dump_insn_slim (dump_file, undobuf.other_insn);
4389 df_insn_rescan (undobuf.other_insn);
4392 if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4394 if (dump_file)
4396 fprintf (dump_file, "modifying insn i1 ");
4397 dump_insn_slim (dump_file, i0);
4399 df_insn_rescan (i0);
4402 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4404 if (dump_file)
4406 fprintf (dump_file, "modifying insn i1 ");
4407 dump_insn_slim (dump_file, i1);
4409 df_insn_rescan (i1);
4412 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4414 if (dump_file)
4416 fprintf (dump_file, "modifying insn i2 ");
4417 dump_insn_slim (dump_file, i2);
4419 df_insn_rescan (i2);
4422 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4424 if (dump_file)
4426 fprintf (dump_file, "modifying insn i3 ");
4427 dump_insn_slim (dump_file, i3);
4429 df_insn_rescan (i3);
4432 /* Set new_direct_jump_p if a new return or simple jump instruction
4433 has been created. Adjust the CFG accordingly. */
4435 if (returnjump_p (i3) || any_uncondjump_p (i3))
4437 *new_direct_jump_p = 1;
4438 mark_jump_label (PATTERN (i3), i3, 0);
4439 update_cfg_for_uncondjump (i3);
4442 if (undobuf.other_insn != NULL_RTX
4443 && (returnjump_p (undobuf.other_insn)
4444 || any_uncondjump_p (undobuf.other_insn)))
4446 *new_direct_jump_p = 1;
4447 update_cfg_for_uncondjump (undobuf.other_insn);
4450 /* A noop might also need cleaning up of CFG, if it comes from the
4451 simplification of a jump. */
4452 if (JUMP_P (i3)
4453 && GET_CODE (newpat) == SET
4454 && SET_SRC (newpat) == pc_rtx
4455 && SET_DEST (newpat) == pc_rtx)
4457 *new_direct_jump_p = 1;
4458 update_cfg_for_uncondjump (i3);
4461 if (undobuf.other_insn != NULL_RTX
4462 && JUMP_P (undobuf.other_insn)
4463 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4464 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4465 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4467 *new_direct_jump_p = 1;
4468 update_cfg_for_uncondjump (undobuf.other_insn);
4471 combine_successes++;
4472 undo_commit ();
4474 if (added_links_insn
4475 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4476 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4477 return added_links_insn;
4478 else
4479 return newi2pat ? i2 : i3;
4482 /* Undo all the modifications recorded in undobuf. */
4484 static void
4485 undo_all (void)
4487 struct undo *undo, *next;
4489 for (undo = undobuf.undos; undo; undo = next)
4491 next = undo->next;
4492 switch (undo->kind)
4494 case UNDO_RTX:
4495 *undo->where.r = undo->old_contents.r;
4496 break;
4497 case UNDO_INT:
4498 *undo->where.i = undo->old_contents.i;
4499 break;
4500 case UNDO_MODE:
4501 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4502 break;
4503 default:
4504 gcc_unreachable ();
4507 undo->next = undobuf.frees;
4508 undobuf.frees = undo;
4511 undobuf.undos = 0;
4514 /* We've committed to accepting the changes we made. Move all
4515 of the undos to the free list. */
4517 static void
4518 undo_commit (void)
4520 struct undo *undo, *next;
4522 for (undo = undobuf.undos; undo; undo = next)
4524 next = undo->next;
4525 undo->next = undobuf.frees;
4526 undobuf.frees = undo;
4528 undobuf.undos = 0;
4531 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4532 where we have an arithmetic expression and return that point. LOC will
4533 be inside INSN.
4535 try_combine will call this function to see if an insn can be split into
4536 two insns. */
4538 static rtx *
4539 find_split_point (rtx *loc, rtx insn, bool set_src)
4541 rtx x = *loc;
4542 enum rtx_code code = GET_CODE (x);
4543 rtx *split;
4544 unsigned HOST_WIDE_INT len = 0;
4545 HOST_WIDE_INT pos = 0;
4546 int unsignedp = 0;
4547 rtx inner = NULL_RTX;
4549 /* First special-case some codes. */
4550 switch (code)
4552 case SUBREG:
4553 #ifdef INSN_SCHEDULING
4554 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4555 point. */
4556 if (MEM_P (SUBREG_REG (x)))
4557 return loc;
4558 #endif
4559 return find_split_point (&SUBREG_REG (x), insn, false);
4561 case MEM:
4562 #ifdef HAVE_lo_sum
4563 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4564 using LO_SUM and HIGH. */
4565 if (GET_CODE (XEXP (x, 0)) == CONST
4566 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4568 enum machine_mode address_mode
4569 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4571 SUBST (XEXP (x, 0),
4572 gen_rtx_LO_SUM (address_mode,
4573 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4574 XEXP (x, 0)));
4575 return &XEXP (XEXP (x, 0), 0);
4577 #endif
4579 /* If we have a PLUS whose second operand is a constant and the
4580 address is not valid, perhaps will can split it up using
4581 the machine-specific way to split large constants. We use
4582 the first pseudo-reg (one of the virtual regs) as a placeholder;
4583 it will not remain in the result. */
4584 if (GET_CODE (XEXP (x, 0)) == PLUS
4585 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4586 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4587 MEM_ADDR_SPACE (x)))
4589 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4590 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4591 XEXP (x, 0)),
4592 subst_insn);
4594 /* This should have produced two insns, each of which sets our
4595 placeholder. If the source of the second is a valid address,
4596 we can make put both sources together and make a split point
4597 in the middle. */
4599 if (seq
4600 && NEXT_INSN (seq) != NULL_RTX
4601 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4602 && NONJUMP_INSN_P (seq)
4603 && GET_CODE (PATTERN (seq)) == SET
4604 && SET_DEST (PATTERN (seq)) == reg
4605 && ! reg_mentioned_p (reg,
4606 SET_SRC (PATTERN (seq)))
4607 && NONJUMP_INSN_P (NEXT_INSN (seq))
4608 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4609 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4610 && memory_address_addr_space_p
4611 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4612 MEM_ADDR_SPACE (x)))
4614 rtx src1 = SET_SRC (PATTERN (seq));
4615 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4617 /* Replace the placeholder in SRC2 with SRC1. If we can
4618 find where in SRC2 it was placed, that can become our
4619 split point and we can replace this address with SRC2.
4620 Just try two obvious places. */
4622 src2 = replace_rtx (src2, reg, src1);
4623 split = 0;
4624 if (XEXP (src2, 0) == src1)
4625 split = &XEXP (src2, 0);
4626 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4627 && XEXP (XEXP (src2, 0), 0) == src1)
4628 split = &XEXP (XEXP (src2, 0), 0);
4630 if (split)
4632 SUBST (XEXP (x, 0), src2);
4633 return split;
4637 /* If that didn't work, perhaps the first operand is complex and
4638 needs to be computed separately, so make a split point there.
4639 This will occur on machines that just support REG + CONST
4640 and have a constant moved through some previous computation. */
4642 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4643 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4644 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4645 return &XEXP (XEXP (x, 0), 0);
4648 /* If we have a PLUS whose first operand is complex, try computing it
4649 separately by making a split there. */
4650 if (GET_CODE (XEXP (x, 0)) == PLUS
4651 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4652 MEM_ADDR_SPACE (x))
4653 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4654 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4655 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4656 return &XEXP (XEXP (x, 0), 0);
4657 break;
4659 case SET:
4660 #ifdef HAVE_cc0
4661 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4662 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4663 we need to put the operand into a register. So split at that
4664 point. */
4666 if (SET_DEST (x) == cc0_rtx
4667 && GET_CODE (SET_SRC (x)) != COMPARE
4668 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4669 && !OBJECT_P (SET_SRC (x))
4670 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4671 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4672 return &SET_SRC (x);
4673 #endif
4675 /* See if we can split SET_SRC as it stands. */
4676 split = find_split_point (&SET_SRC (x), insn, true);
4677 if (split && split != &SET_SRC (x))
4678 return split;
4680 /* See if we can split SET_DEST as it stands. */
4681 split = find_split_point (&SET_DEST (x), insn, false);
4682 if (split && split != &SET_DEST (x))
4683 return split;
4685 /* See if this is a bitfield assignment with everything constant. If
4686 so, this is an IOR of an AND, so split it into that. */
4687 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4688 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4689 <= HOST_BITS_PER_WIDE_INT)
4690 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4691 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4692 && CONST_INT_P (SET_SRC (x))
4693 && ((INTVAL (XEXP (SET_DEST (x), 1))
4694 + INTVAL (XEXP (SET_DEST (x), 2)))
4695 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4696 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4698 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4699 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4700 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4701 rtx dest = XEXP (SET_DEST (x), 0);
4702 enum machine_mode mode = GET_MODE (dest);
4703 unsigned HOST_WIDE_INT mask
4704 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4705 rtx or_mask;
4707 if (BITS_BIG_ENDIAN)
4708 pos = GET_MODE_BITSIZE (mode) - len - pos;
4710 or_mask = gen_int_mode (src << pos, mode);
4711 if (src == mask)
4712 SUBST (SET_SRC (x),
4713 simplify_gen_binary (IOR, mode, dest, or_mask));
4714 else
4716 rtx negmask = gen_int_mode (~(mask << pos), mode);
4717 SUBST (SET_SRC (x),
4718 simplify_gen_binary (IOR, mode,
4719 simplify_gen_binary (AND, mode,
4720 dest, negmask),
4721 or_mask));
4724 SUBST (SET_DEST (x), dest);
4726 split = find_split_point (&SET_SRC (x), insn, true);
4727 if (split && split != &SET_SRC (x))
4728 return split;
4731 /* Otherwise, see if this is an operation that we can split into two.
4732 If so, try to split that. */
4733 code = GET_CODE (SET_SRC (x));
4735 switch (code)
4737 case AND:
4738 /* If we are AND'ing with a large constant that is only a single
4739 bit and the result is only being used in a context where we
4740 need to know if it is zero or nonzero, replace it with a bit
4741 extraction. This will avoid the large constant, which might
4742 have taken more than one insn to make. If the constant were
4743 not a valid argument to the AND but took only one insn to make,
4744 this is no worse, but if it took more than one insn, it will
4745 be better. */
4747 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4748 && REG_P (XEXP (SET_SRC (x), 0))
4749 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4750 && REG_P (SET_DEST (x))
4751 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4752 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4753 && XEXP (*split, 0) == SET_DEST (x)
4754 && XEXP (*split, 1) == const0_rtx)
4756 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4757 XEXP (SET_SRC (x), 0),
4758 pos, NULL_RTX, 1, 1, 0, 0);
4759 if (extraction != 0)
4761 SUBST (SET_SRC (x), extraction);
4762 return find_split_point (loc, insn, false);
4765 break;
4767 case NE:
4768 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4769 is known to be on, this can be converted into a NEG of a shift. */
4770 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4771 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4772 && 1 <= (pos = exact_log2
4773 (nonzero_bits (XEXP (SET_SRC (x), 0),
4774 GET_MODE (XEXP (SET_SRC (x), 0))))))
4776 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4778 SUBST (SET_SRC (x),
4779 gen_rtx_NEG (mode,
4780 gen_rtx_LSHIFTRT (mode,
4781 XEXP (SET_SRC (x), 0),
4782 GEN_INT (pos))));
4784 split = find_split_point (&SET_SRC (x), insn, true);
4785 if (split && split != &SET_SRC (x))
4786 return split;
4788 break;
4790 case SIGN_EXTEND:
4791 inner = XEXP (SET_SRC (x), 0);
4793 /* We can't optimize if either mode is a partial integer
4794 mode as we don't know how many bits are significant
4795 in those modes. */
4796 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4797 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4798 break;
4800 pos = 0;
4801 len = GET_MODE_BITSIZE (GET_MODE (inner));
4802 unsignedp = 0;
4803 break;
4805 case SIGN_EXTRACT:
4806 case ZERO_EXTRACT:
4807 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4808 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4810 inner = XEXP (SET_SRC (x), 0);
4811 len = INTVAL (XEXP (SET_SRC (x), 1));
4812 pos = INTVAL (XEXP (SET_SRC (x), 2));
4814 if (BITS_BIG_ENDIAN)
4815 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4816 unsignedp = (code == ZERO_EXTRACT);
4818 break;
4820 default:
4821 break;
4824 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4826 enum machine_mode mode = GET_MODE (SET_SRC (x));
4828 /* For unsigned, we have a choice of a shift followed by an
4829 AND or two shifts. Use two shifts for field sizes where the
4830 constant might be too large. We assume here that we can
4831 always at least get 8-bit constants in an AND insn, which is
4832 true for every current RISC. */
4834 if (unsignedp && len <= 8)
4836 SUBST (SET_SRC (x),
4837 gen_rtx_AND (mode,
4838 gen_rtx_LSHIFTRT
4839 (mode, gen_lowpart (mode, inner),
4840 GEN_INT (pos)),
4841 GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4842 - 1)));
4844 split = find_split_point (&SET_SRC (x), insn, true);
4845 if (split && split != &SET_SRC (x))
4846 return split;
4848 else
4850 SUBST (SET_SRC (x),
4851 gen_rtx_fmt_ee
4852 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4853 gen_rtx_ASHIFT (mode,
4854 gen_lowpart (mode, inner),
4855 GEN_INT (GET_MODE_BITSIZE (mode)
4856 - len - pos)),
4857 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4859 split = find_split_point (&SET_SRC (x), insn, true);
4860 if (split && split != &SET_SRC (x))
4861 return split;
4865 /* See if this is a simple operation with a constant as the second
4866 operand. It might be that this constant is out of range and hence
4867 could be used as a split point. */
4868 if (BINARY_P (SET_SRC (x))
4869 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4870 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4871 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4872 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4873 return &XEXP (SET_SRC (x), 1);
4875 /* Finally, see if this is a simple operation with its first operand
4876 not in a register. The operation might require this operand in a
4877 register, so return it as a split point. We can always do this
4878 because if the first operand were another operation, we would have
4879 already found it as a split point. */
4880 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4881 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4882 return &XEXP (SET_SRC (x), 0);
4884 return 0;
4886 case AND:
4887 case IOR:
4888 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4889 it is better to write this as (not (ior A B)) so we can split it.
4890 Similarly for IOR. */
4891 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4893 SUBST (*loc,
4894 gen_rtx_NOT (GET_MODE (x),
4895 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4896 GET_MODE (x),
4897 XEXP (XEXP (x, 0), 0),
4898 XEXP (XEXP (x, 1), 0))));
4899 return find_split_point (loc, insn, set_src);
4902 /* Many RISC machines have a large set of logical insns. If the
4903 second operand is a NOT, put it first so we will try to split the
4904 other operand first. */
4905 if (GET_CODE (XEXP (x, 1)) == NOT)
4907 rtx tem = XEXP (x, 0);
4908 SUBST (XEXP (x, 0), XEXP (x, 1));
4909 SUBST (XEXP (x, 1), tem);
4911 break;
4913 case PLUS:
4914 case MINUS:
4915 /* Canonicalization can produce (minus A (mult B C)), where C is a
4916 constant. It may be better to try splitting (plus (mult B -C) A)
4917 instead if this isn't a multiply by a power of two. */
4918 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4919 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4920 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4922 enum machine_mode mode = GET_MODE (x);
4923 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4924 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4925 SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4926 XEXP (XEXP (x, 1), 0),
4927 GEN_INT (other_int)),
4928 XEXP (x, 0)));
4929 return find_split_point (loc, insn, set_src);
4932 /* Split at a multiply-accumulate instruction. However if this is
4933 the SET_SRC, we likely do not have such an instruction and it's
4934 worthless to try this split. */
4935 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4936 return loc;
4938 default:
4939 break;
4942 /* Otherwise, select our actions depending on our rtx class. */
4943 switch (GET_RTX_CLASS (code))
4945 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4946 case RTX_TERNARY:
4947 split = find_split_point (&XEXP (x, 2), insn, false);
4948 if (split)
4949 return split;
4950 /* ... fall through ... */
4951 case RTX_BIN_ARITH:
4952 case RTX_COMM_ARITH:
4953 case RTX_COMPARE:
4954 case RTX_COMM_COMPARE:
4955 split = find_split_point (&XEXP (x, 1), insn, false);
4956 if (split)
4957 return split;
4958 /* ... fall through ... */
4959 case RTX_UNARY:
4960 /* Some machines have (and (shift ...) ...) insns. If X is not
4961 an AND, but XEXP (X, 0) is, use it as our split point. */
4962 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4963 return &XEXP (x, 0);
4965 split = find_split_point (&XEXP (x, 0), insn, false);
4966 if (split)
4967 return split;
4968 return loc;
4970 default:
4971 /* Otherwise, we don't have a split point. */
4972 return 0;
4976 /* Throughout X, replace FROM with TO, and return the result.
4977 The result is TO if X is FROM;
4978 otherwise the result is X, but its contents may have been modified.
4979 If they were modified, a record was made in undobuf so that
4980 undo_all will (among other things) return X to its original state.
4982 If the number of changes necessary is too much to record to undo,
4983 the excess changes are not made, so the result is invalid.
4984 The changes already made can still be undone.
4985 undobuf.num_undo is incremented for such changes, so by testing that
4986 the caller can tell whether the result is valid.
4988 `n_occurrences' is incremented each time FROM is replaced.
4990 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4992 IN_COND is nonzero if we are at the top level of a condition.
4994 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4995 by copying if `n_occurrences' is nonzero. */
4997 static rtx
4998 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5000 enum rtx_code code = GET_CODE (x);
5001 enum machine_mode op0_mode = VOIDmode;
5002 const char *fmt;
5003 int len, i;
5004 rtx new_rtx;
5006 /* Two expressions are equal if they are identical copies of a shared
5007 RTX or if they are both registers with the same register number
5008 and mode. */
5010 #define COMBINE_RTX_EQUAL_P(X,Y) \
5011 ((X) == (Y) \
5012 || (REG_P (X) && REG_P (Y) \
5013 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5015 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5017 n_occurrences++;
5018 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5021 /* If X and FROM are the same register but different modes, they
5022 will not have been seen as equal above. However, the log links code
5023 will make a LOG_LINKS entry for that case. If we do nothing, we
5024 will try to rerecognize our original insn and, when it succeeds,
5025 we will delete the feeding insn, which is incorrect.
5027 So force this insn not to match in this (rare) case. */
5028 if (! in_dest && code == REG && REG_P (from)
5029 && reg_overlap_mentioned_p (x, from))
5030 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5032 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5033 of which may contain things that can be combined. */
5034 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5035 return x;
5037 /* It is possible to have a subexpression appear twice in the insn.
5038 Suppose that FROM is a register that appears within TO.
5039 Then, after that subexpression has been scanned once by `subst',
5040 the second time it is scanned, TO may be found. If we were
5041 to scan TO here, we would find FROM within it and create a
5042 self-referent rtl structure which is completely wrong. */
5043 if (COMBINE_RTX_EQUAL_P (x, to))
5044 return to;
5046 /* Parallel asm_operands need special attention because all of the
5047 inputs are shared across the arms. Furthermore, unsharing the
5048 rtl results in recognition failures. Failure to handle this case
5049 specially can result in circular rtl.
5051 Solve this by doing a normal pass across the first entry of the
5052 parallel, and only processing the SET_DESTs of the subsequent
5053 entries. Ug. */
5055 if (code == PARALLEL
5056 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5057 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5059 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5061 /* If this substitution failed, this whole thing fails. */
5062 if (GET_CODE (new_rtx) == CLOBBER
5063 && XEXP (new_rtx, 0) == const0_rtx)
5064 return new_rtx;
5066 SUBST (XVECEXP (x, 0, 0), new_rtx);
5068 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5070 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5072 if (!REG_P (dest)
5073 && GET_CODE (dest) != CC0
5074 && GET_CODE (dest) != PC)
5076 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5078 /* If this substitution failed, this whole thing fails. */
5079 if (GET_CODE (new_rtx) == CLOBBER
5080 && XEXP (new_rtx, 0) == const0_rtx)
5081 return new_rtx;
5083 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5087 else
5089 len = GET_RTX_LENGTH (code);
5090 fmt = GET_RTX_FORMAT (code);
5092 /* We don't need to process a SET_DEST that is a register, CC0,
5093 or PC, so set up to skip this common case. All other cases
5094 where we want to suppress replacing something inside a
5095 SET_SRC are handled via the IN_DEST operand. */
5096 if (code == SET
5097 && (REG_P (SET_DEST (x))
5098 || GET_CODE (SET_DEST (x)) == CC0
5099 || GET_CODE (SET_DEST (x)) == PC))
5100 fmt = "ie";
5102 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5103 constant. */
5104 if (fmt[0] == 'e')
5105 op0_mode = GET_MODE (XEXP (x, 0));
5107 for (i = 0; i < len; i++)
5109 if (fmt[i] == 'E')
5111 int j;
5112 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5114 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5116 new_rtx = (unique_copy && n_occurrences
5117 ? copy_rtx (to) : to);
5118 n_occurrences++;
5120 else
5122 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5123 unique_copy);
5125 /* If this substitution failed, this whole thing
5126 fails. */
5127 if (GET_CODE (new_rtx) == CLOBBER
5128 && XEXP (new_rtx, 0) == const0_rtx)
5129 return new_rtx;
5132 SUBST (XVECEXP (x, i, j), new_rtx);
5135 else if (fmt[i] == 'e')
5137 /* If this is a register being set, ignore it. */
5138 new_rtx = XEXP (x, i);
5139 if (in_dest
5140 && i == 0
5141 && (((code == SUBREG || code == ZERO_EXTRACT)
5142 && REG_P (new_rtx))
5143 || code == STRICT_LOW_PART))
5146 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5148 /* In general, don't install a subreg involving two
5149 modes not tieable. It can worsen register
5150 allocation, and can even make invalid reload
5151 insns, since the reg inside may need to be copied
5152 from in the outside mode, and that may be invalid
5153 if it is an fp reg copied in integer mode.
5155 We allow two exceptions to this: It is valid if
5156 it is inside another SUBREG and the mode of that
5157 SUBREG and the mode of the inside of TO is
5158 tieable and it is valid if X is a SET that copies
5159 FROM to CC0. */
5161 if (GET_CODE (to) == SUBREG
5162 && ! MODES_TIEABLE_P (GET_MODE (to),
5163 GET_MODE (SUBREG_REG (to)))
5164 && ! (code == SUBREG
5165 && MODES_TIEABLE_P (GET_MODE (x),
5166 GET_MODE (SUBREG_REG (to))))
5167 #ifdef HAVE_cc0
5168 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5169 #endif
5171 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5173 #ifdef CANNOT_CHANGE_MODE_CLASS
5174 if (code == SUBREG
5175 && REG_P (to)
5176 && REGNO (to) < FIRST_PSEUDO_REGISTER
5177 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5178 GET_MODE (to),
5179 GET_MODE (x)))
5180 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5181 #endif
5183 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5184 n_occurrences++;
5186 else
5187 /* If we are in a SET_DEST, suppress most cases unless we
5188 have gone inside a MEM, in which case we want to
5189 simplify the address. We assume here that things that
5190 are actually part of the destination have their inner
5191 parts in the first expression. This is true for SUBREG,
5192 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5193 things aside from REG and MEM that should appear in a
5194 SET_DEST. */
5195 new_rtx = subst (XEXP (x, i), from, to,
5196 (((in_dest
5197 && (code == SUBREG || code == STRICT_LOW_PART
5198 || code == ZERO_EXTRACT))
5199 || code == SET)
5200 && i == 0),
5201 code == IF_THEN_ELSE && i == 0,
5202 unique_copy);
5204 /* If we found that we will have to reject this combination,
5205 indicate that by returning the CLOBBER ourselves, rather than
5206 an expression containing it. This will speed things up as
5207 well as prevent accidents where two CLOBBERs are considered
5208 to be equal, thus producing an incorrect simplification. */
5210 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5211 return new_rtx;
5213 if (GET_CODE (x) == SUBREG
5214 && (CONST_INT_P (new_rtx)
5215 || GET_CODE (new_rtx) == CONST_DOUBLE))
5217 enum machine_mode mode = GET_MODE (x);
5219 x = simplify_subreg (GET_MODE (x), new_rtx,
5220 GET_MODE (SUBREG_REG (x)),
5221 SUBREG_BYTE (x));
5222 if (! x)
5223 x = gen_rtx_CLOBBER (mode, const0_rtx);
5225 else if (CONST_INT_P (new_rtx)
5226 && GET_CODE (x) == ZERO_EXTEND)
5228 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5229 new_rtx, GET_MODE (XEXP (x, 0)));
5230 gcc_assert (x);
5232 else
5233 SUBST (XEXP (x, i), new_rtx);
5238 /* Check if we are loading something from the constant pool via float
5239 extension; in this case we would undo compress_float_constant
5240 optimization and degenerate constant load to an immediate value. */
5241 if (GET_CODE (x) == FLOAT_EXTEND
5242 && MEM_P (XEXP (x, 0))
5243 && MEM_READONLY_P (XEXP (x, 0)))
5245 rtx tmp = avoid_constant_pool_reference (x);
5246 if (x != tmp)
5247 return x;
5250 /* Try to simplify X. If the simplification changed the code, it is likely
5251 that further simplification will help, so loop, but limit the number
5252 of repetitions that will be performed. */
5254 for (i = 0; i < 4; i++)
5256 /* If X is sufficiently simple, don't bother trying to do anything
5257 with it. */
5258 if (code != CONST_INT && code != REG && code != CLOBBER)
5259 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5261 if (GET_CODE (x) == code)
5262 break;
5264 code = GET_CODE (x);
5266 /* We no longer know the original mode of operand 0 since we
5267 have changed the form of X) */
5268 op0_mode = VOIDmode;
5271 return x;
5274 /* Simplify X, a piece of RTL. We just operate on the expression at the
5275 outer level; call `subst' to simplify recursively. Return the new
5276 expression.
5278 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5279 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5280 of a condition. */
5282 static rtx
5283 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5284 int in_cond)
5286 enum rtx_code code = GET_CODE (x);
5287 enum machine_mode mode = GET_MODE (x);
5288 rtx temp;
5289 int i;
5291 /* If this is a commutative operation, put a constant last and a complex
5292 expression first. We don't need to do this for comparisons here. */
5293 if (COMMUTATIVE_ARITH_P (x)
5294 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5296 temp = XEXP (x, 0);
5297 SUBST (XEXP (x, 0), XEXP (x, 1));
5298 SUBST (XEXP (x, 1), temp);
5301 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5302 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5303 things. Check for cases where both arms are testing the same
5304 condition.
5306 Don't do anything if all operands are very simple. */
5308 if ((BINARY_P (x)
5309 && ((!OBJECT_P (XEXP (x, 0))
5310 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5311 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5312 || (!OBJECT_P (XEXP (x, 1))
5313 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5314 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5315 || (UNARY_P (x)
5316 && (!OBJECT_P (XEXP (x, 0))
5317 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5318 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5320 rtx cond, true_rtx, false_rtx;
5322 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5323 if (cond != 0
5324 /* If everything is a comparison, what we have is highly unlikely
5325 to be simpler, so don't use it. */
5326 && ! (COMPARISON_P (x)
5327 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5329 rtx cop1 = const0_rtx;
5330 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5332 if (cond_code == NE && COMPARISON_P (cond))
5333 return x;
5335 /* Simplify the alternative arms; this may collapse the true and
5336 false arms to store-flag values. Be careful to use copy_rtx
5337 here since true_rtx or false_rtx might share RTL with x as a
5338 result of the if_then_else_cond call above. */
5339 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5340 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5342 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5343 is unlikely to be simpler. */
5344 if (general_operand (true_rtx, VOIDmode)
5345 && general_operand (false_rtx, VOIDmode))
5347 enum rtx_code reversed;
5349 /* Restarting if we generate a store-flag expression will cause
5350 us to loop. Just drop through in this case. */
5352 /* If the result values are STORE_FLAG_VALUE and zero, we can
5353 just make the comparison operation. */
5354 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5355 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5356 cond, cop1);
5357 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5358 && ((reversed = reversed_comparison_code_parts
5359 (cond_code, cond, cop1, NULL))
5360 != UNKNOWN))
5361 x = simplify_gen_relational (reversed, mode, VOIDmode,
5362 cond, cop1);
5364 /* Likewise, we can make the negate of a comparison operation
5365 if the result values are - STORE_FLAG_VALUE and zero. */
5366 else if (CONST_INT_P (true_rtx)
5367 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5368 && false_rtx == const0_rtx)
5369 x = simplify_gen_unary (NEG, mode,
5370 simplify_gen_relational (cond_code,
5371 mode, VOIDmode,
5372 cond, cop1),
5373 mode);
5374 else if (CONST_INT_P (false_rtx)
5375 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5376 && true_rtx == const0_rtx
5377 && ((reversed = reversed_comparison_code_parts
5378 (cond_code, cond, cop1, NULL))
5379 != UNKNOWN))
5380 x = simplify_gen_unary (NEG, mode,
5381 simplify_gen_relational (reversed,
5382 mode, VOIDmode,
5383 cond, cop1),
5384 mode);
5385 else
5386 return gen_rtx_IF_THEN_ELSE (mode,
5387 simplify_gen_relational (cond_code,
5388 mode,
5389 VOIDmode,
5390 cond,
5391 cop1),
5392 true_rtx, false_rtx);
5394 code = GET_CODE (x);
5395 op0_mode = VOIDmode;
5400 /* Try to fold this expression in case we have constants that weren't
5401 present before. */
5402 temp = 0;
5403 switch (GET_RTX_CLASS (code))
5405 case RTX_UNARY:
5406 if (op0_mode == VOIDmode)
5407 op0_mode = GET_MODE (XEXP (x, 0));
5408 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5409 break;
5410 case RTX_COMPARE:
5411 case RTX_COMM_COMPARE:
5413 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5414 if (cmp_mode == VOIDmode)
5416 cmp_mode = GET_MODE (XEXP (x, 1));
5417 if (cmp_mode == VOIDmode)
5418 cmp_mode = op0_mode;
5420 temp = simplify_relational_operation (code, mode, cmp_mode,
5421 XEXP (x, 0), XEXP (x, 1));
5423 break;
5424 case RTX_COMM_ARITH:
5425 case RTX_BIN_ARITH:
5426 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5427 break;
5428 case RTX_BITFIELD_OPS:
5429 case RTX_TERNARY:
5430 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5431 XEXP (x, 1), XEXP (x, 2));
5432 break;
5433 default:
5434 break;
5437 if (temp)
5439 x = temp;
5440 code = GET_CODE (temp);
5441 op0_mode = VOIDmode;
5442 mode = GET_MODE (temp);
5445 /* First see if we can apply the inverse distributive law. */
5446 if (code == PLUS || code == MINUS
5447 || code == AND || code == IOR || code == XOR)
5449 x = apply_distributive_law (x);
5450 code = GET_CODE (x);
5451 op0_mode = VOIDmode;
5454 /* If CODE is an associative operation not otherwise handled, see if we
5455 can associate some operands. This can win if they are constants or
5456 if they are logically related (i.e. (a & b) & a). */
5457 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5458 || code == AND || code == IOR || code == XOR
5459 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5460 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5461 || (flag_associative_math && FLOAT_MODE_P (mode))))
5463 if (GET_CODE (XEXP (x, 0)) == code)
5465 rtx other = XEXP (XEXP (x, 0), 0);
5466 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5467 rtx inner_op1 = XEXP (x, 1);
5468 rtx inner;
5470 /* Make sure we pass the constant operand if any as the second
5471 one if this is a commutative operation. */
5472 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5474 rtx tem = inner_op0;
5475 inner_op0 = inner_op1;
5476 inner_op1 = tem;
5478 inner = simplify_binary_operation (code == MINUS ? PLUS
5479 : code == DIV ? MULT
5480 : code,
5481 mode, inner_op0, inner_op1);
5483 /* For commutative operations, try the other pair if that one
5484 didn't simplify. */
5485 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5487 other = XEXP (XEXP (x, 0), 1);
5488 inner = simplify_binary_operation (code, mode,
5489 XEXP (XEXP (x, 0), 0),
5490 XEXP (x, 1));
5493 if (inner)
5494 return simplify_gen_binary (code, mode, other, inner);
5498 /* A little bit of algebraic simplification here. */
5499 switch (code)
5501 case MEM:
5502 /* Ensure that our address has any ASHIFTs converted to MULT in case
5503 address-recognizing predicates are called later. */
5504 temp = make_compound_operation (XEXP (x, 0), MEM);
5505 SUBST (XEXP (x, 0), temp);
5506 break;
5508 case SUBREG:
5509 if (op0_mode == VOIDmode)
5510 op0_mode = GET_MODE (SUBREG_REG (x));
5512 /* See if this can be moved to simplify_subreg. */
5513 if (CONSTANT_P (SUBREG_REG (x))
5514 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5515 /* Don't call gen_lowpart if the inner mode
5516 is VOIDmode and we cannot simplify it, as SUBREG without
5517 inner mode is invalid. */
5518 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5519 || gen_lowpart_common (mode, SUBREG_REG (x))))
5520 return gen_lowpart (mode, SUBREG_REG (x));
5522 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5523 break;
5525 rtx temp;
5526 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5527 SUBREG_BYTE (x));
5528 if (temp)
5529 return temp;
5532 /* Don't change the mode of the MEM if that would change the meaning
5533 of the address. */
5534 if (MEM_P (SUBREG_REG (x))
5535 && (MEM_VOLATILE_P (SUBREG_REG (x))
5536 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5537 return gen_rtx_CLOBBER (mode, const0_rtx);
5539 /* Note that we cannot do any narrowing for non-constants since
5540 we might have been counting on using the fact that some bits were
5541 zero. We now do this in the SET. */
5543 break;
5545 case NEG:
5546 temp = expand_compound_operation (XEXP (x, 0));
5548 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5549 replaced by (lshiftrt X C). This will convert
5550 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5552 if (GET_CODE (temp) == ASHIFTRT
5553 && CONST_INT_P (XEXP (temp, 1))
5554 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5555 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5556 INTVAL (XEXP (temp, 1)));
5558 /* If X has only a single bit that might be nonzero, say, bit I, convert
5559 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5560 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5561 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5562 or a SUBREG of one since we'd be making the expression more
5563 complex if it was just a register. */
5565 if (!REG_P (temp)
5566 && ! (GET_CODE (temp) == SUBREG
5567 && REG_P (SUBREG_REG (temp)))
5568 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5570 rtx temp1 = simplify_shift_const
5571 (NULL_RTX, ASHIFTRT, mode,
5572 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5573 GET_MODE_BITSIZE (mode) - 1 - i),
5574 GET_MODE_BITSIZE (mode) - 1 - i);
5576 /* If all we did was surround TEMP with the two shifts, we
5577 haven't improved anything, so don't use it. Otherwise,
5578 we are better off with TEMP1. */
5579 if (GET_CODE (temp1) != ASHIFTRT
5580 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5581 || XEXP (XEXP (temp1, 0), 0) != temp)
5582 return temp1;
5584 break;
5586 case TRUNCATE:
5587 /* We can't handle truncation to a partial integer mode here
5588 because we don't know the real bitsize of the partial
5589 integer mode. */
5590 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5591 break;
5593 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5594 SUBST (XEXP (x, 0),
5595 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5596 GET_MODE_MASK (mode), 0));
5598 /* We can truncate a constant value and return it. */
5599 if (CONST_INT_P (XEXP (x, 0)))
5600 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5602 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5603 whose value is a comparison can be replaced with a subreg if
5604 STORE_FLAG_VALUE permits. */
5605 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5606 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5607 && (temp = get_last_value (XEXP (x, 0)))
5608 && COMPARISON_P (temp))
5609 return gen_lowpart (mode, XEXP (x, 0));
5610 break;
5612 case CONST:
5613 /* (const (const X)) can become (const X). Do it this way rather than
5614 returning the inner CONST since CONST can be shared with a
5615 REG_EQUAL note. */
5616 if (GET_CODE (XEXP (x, 0)) == CONST)
5617 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5618 break;
5620 #ifdef HAVE_lo_sum
5621 case LO_SUM:
5622 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5623 can add in an offset. find_split_point will split this address up
5624 again if it doesn't match. */
5625 if (GET_CODE (XEXP (x, 0)) == HIGH
5626 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5627 return XEXP (x, 1);
5628 break;
5629 #endif
5631 case PLUS:
5632 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5633 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5634 bit-field and can be replaced by either a sign_extend or a
5635 sign_extract. The `and' may be a zero_extend and the two
5636 <c>, -<c> constants may be reversed. */
5637 if (GET_CODE (XEXP (x, 0)) == XOR
5638 && CONST_INT_P (XEXP (x, 1))
5639 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5640 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5641 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5642 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5643 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5644 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5645 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5646 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5647 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5648 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5649 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5650 == (unsigned int) i + 1))))
5651 return simplify_shift_const
5652 (NULL_RTX, ASHIFTRT, mode,
5653 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5654 XEXP (XEXP (XEXP (x, 0), 0), 0),
5655 GET_MODE_BITSIZE (mode) - (i + 1)),
5656 GET_MODE_BITSIZE (mode) - (i + 1));
5658 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5659 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5660 the bitsize of the mode - 1. This allows simplification of
5661 "a = (b & 8) == 0;" */
5662 if (XEXP (x, 1) == constm1_rtx
5663 && !REG_P (XEXP (x, 0))
5664 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5665 && REG_P (SUBREG_REG (XEXP (x, 0))))
5666 && nonzero_bits (XEXP (x, 0), mode) == 1)
5667 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5668 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5669 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5670 GET_MODE_BITSIZE (mode) - 1),
5671 GET_MODE_BITSIZE (mode) - 1);
5673 /* If we are adding two things that have no bits in common, convert
5674 the addition into an IOR. This will often be further simplified,
5675 for example in cases like ((a & 1) + (a & 2)), which can
5676 become a & 3. */
5678 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5679 && (nonzero_bits (XEXP (x, 0), mode)
5680 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5682 /* Try to simplify the expression further. */
5683 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5684 temp = combine_simplify_rtx (tor, mode, in_dest, 0);
5686 /* If we could, great. If not, do not go ahead with the IOR
5687 replacement, since PLUS appears in many special purpose
5688 address arithmetic instructions. */
5689 if (GET_CODE (temp) != CLOBBER && temp != tor)
5690 return temp;
5692 break;
5694 case MINUS:
5695 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5696 (and <foo> (const_int pow2-1)) */
5697 if (GET_CODE (XEXP (x, 1)) == AND
5698 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5699 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5700 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5701 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5702 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5703 break;
5705 case MULT:
5706 /* If we have (mult (plus A B) C), apply the distributive law and then
5707 the inverse distributive law to see if things simplify. This
5708 occurs mostly in addresses, often when unrolling loops. */
5710 if (GET_CODE (XEXP (x, 0)) == PLUS)
5712 rtx result = distribute_and_simplify_rtx (x, 0);
5713 if (result)
5714 return result;
5717 /* Try simplify a*(b/c) as (a*b)/c. */
5718 if (FLOAT_MODE_P (mode) && flag_associative_math
5719 && GET_CODE (XEXP (x, 0)) == DIV)
5721 rtx tem = simplify_binary_operation (MULT, mode,
5722 XEXP (XEXP (x, 0), 0),
5723 XEXP (x, 1));
5724 if (tem)
5725 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5727 break;
5729 case UDIV:
5730 /* If this is a divide by a power of two, treat it as a shift if
5731 its first operand is a shift. */
5732 if (CONST_INT_P (XEXP (x, 1))
5733 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5734 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5735 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5736 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5737 || GET_CODE (XEXP (x, 0)) == ROTATE
5738 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5739 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5740 break;
5742 case EQ: case NE:
5743 case GT: case GTU: case GE: case GEU:
5744 case LT: case LTU: case LE: case LEU:
5745 case UNEQ: case LTGT:
5746 case UNGT: case UNGE:
5747 case UNLT: case UNLE:
5748 case UNORDERED: case ORDERED:
5749 /* If the first operand is a condition code, we can't do anything
5750 with it. */
5751 if (GET_CODE (XEXP (x, 0)) == COMPARE
5752 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5753 && ! CC0_P (XEXP (x, 0))))
5755 rtx op0 = XEXP (x, 0);
5756 rtx op1 = XEXP (x, 1);
5757 enum rtx_code new_code;
5759 if (GET_CODE (op0) == COMPARE)
5760 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5762 /* Simplify our comparison, if possible. */
5763 new_code = simplify_comparison (code, &op0, &op1);
5765 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5766 if only the low-order bit is possibly nonzero in X (such as when
5767 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5768 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5769 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5770 (plus X 1).
5772 Remove any ZERO_EXTRACT we made when thinking this was a
5773 comparison. It may now be simpler to use, e.g., an AND. If a
5774 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5775 the call to make_compound_operation in the SET case.
5777 Don't apply these optimizations if the caller would
5778 prefer a comparison rather than a value.
5779 E.g., for the condition in an IF_THEN_ELSE most targets need
5780 an explicit comparison. */
5782 if (in_cond)
5785 else if (STORE_FLAG_VALUE == 1
5786 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5787 && op1 == const0_rtx
5788 && mode == GET_MODE (op0)
5789 && nonzero_bits (op0, mode) == 1)
5790 return gen_lowpart (mode,
5791 expand_compound_operation (op0));
5793 else if (STORE_FLAG_VALUE == 1
5794 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5795 && op1 == const0_rtx
5796 && mode == GET_MODE (op0)
5797 && (num_sign_bit_copies (op0, mode)
5798 == GET_MODE_BITSIZE (mode)))
5800 op0 = expand_compound_operation (op0);
5801 return simplify_gen_unary (NEG, mode,
5802 gen_lowpart (mode, op0),
5803 mode);
5806 else if (STORE_FLAG_VALUE == 1
5807 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5808 && op1 == const0_rtx
5809 && mode == GET_MODE (op0)
5810 && nonzero_bits (op0, mode) == 1)
5812 op0 = expand_compound_operation (op0);
5813 return simplify_gen_binary (XOR, mode,
5814 gen_lowpart (mode, op0),
5815 const1_rtx);
5818 else if (STORE_FLAG_VALUE == 1
5819 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5820 && op1 == const0_rtx
5821 && mode == GET_MODE (op0)
5822 && (num_sign_bit_copies (op0, mode)
5823 == GET_MODE_BITSIZE (mode)))
5825 op0 = expand_compound_operation (op0);
5826 return plus_constant (gen_lowpart (mode, op0), 1);
5829 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5830 those above. */
5831 if (in_cond)
5834 else if (STORE_FLAG_VALUE == -1
5835 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5836 && op1 == const0_rtx
5837 && (num_sign_bit_copies (op0, mode)
5838 == GET_MODE_BITSIZE (mode)))
5839 return gen_lowpart (mode,
5840 expand_compound_operation (op0));
5842 else if (STORE_FLAG_VALUE == -1
5843 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5844 && op1 == const0_rtx
5845 && mode == GET_MODE (op0)
5846 && nonzero_bits (op0, mode) == 1)
5848 op0 = expand_compound_operation (op0);
5849 return simplify_gen_unary (NEG, mode,
5850 gen_lowpart (mode, op0),
5851 mode);
5854 else if (STORE_FLAG_VALUE == -1
5855 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5856 && op1 == const0_rtx
5857 && mode == GET_MODE (op0)
5858 && (num_sign_bit_copies (op0, mode)
5859 == GET_MODE_BITSIZE (mode)))
5861 op0 = expand_compound_operation (op0);
5862 return simplify_gen_unary (NOT, mode,
5863 gen_lowpart (mode, op0),
5864 mode);
5867 /* If X is 0/1, (eq X 0) is X-1. */
5868 else if (STORE_FLAG_VALUE == -1
5869 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5870 && op1 == const0_rtx
5871 && mode == GET_MODE (op0)
5872 && nonzero_bits (op0, mode) == 1)
5874 op0 = expand_compound_operation (op0);
5875 return plus_constant (gen_lowpart (mode, op0), -1);
5878 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5879 one bit that might be nonzero, we can convert (ne x 0) to
5880 (ashift x c) where C puts the bit in the sign bit. Remove any
5881 AND with STORE_FLAG_VALUE when we are done, since we are only
5882 going to test the sign bit. */
5883 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5884 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5885 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5886 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5887 && op1 == const0_rtx
5888 && mode == GET_MODE (op0)
5889 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5891 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5892 expand_compound_operation (op0),
5893 GET_MODE_BITSIZE (mode) - 1 - i);
5894 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5895 return XEXP (x, 0);
5896 else
5897 return x;
5900 /* If the code changed, return a whole new comparison. */
5901 if (new_code != code)
5902 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5904 /* Otherwise, keep this operation, but maybe change its operands.
5905 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5906 SUBST (XEXP (x, 0), op0);
5907 SUBST (XEXP (x, 1), op1);
5909 break;
5911 case IF_THEN_ELSE:
5912 return simplify_if_then_else (x);
5914 case ZERO_EXTRACT:
5915 case SIGN_EXTRACT:
5916 case ZERO_EXTEND:
5917 case SIGN_EXTEND:
5918 /* If we are processing SET_DEST, we are done. */
5919 if (in_dest)
5920 return x;
5922 return expand_compound_operation (x);
5924 case SET:
5925 return simplify_set (x);
5927 case AND:
5928 case IOR:
5929 return simplify_logical (x);
5931 case ASHIFT:
5932 case LSHIFTRT:
5933 case ASHIFTRT:
5934 case ROTATE:
5935 case ROTATERT:
5936 /* If this is a shift by a constant amount, simplify it. */
5937 if (CONST_INT_P (XEXP (x, 1)))
5938 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5939 INTVAL (XEXP (x, 1)));
5941 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5942 SUBST (XEXP (x, 1),
5943 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5944 ((unsigned HOST_WIDE_INT) 1
5945 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5946 - 1,
5947 0));
5948 break;
5950 default:
5951 break;
5954 return x;
5957 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5959 static rtx
5960 simplify_if_then_else (rtx x)
5962 enum machine_mode mode = GET_MODE (x);
5963 rtx cond = XEXP (x, 0);
5964 rtx true_rtx = XEXP (x, 1);
5965 rtx false_rtx = XEXP (x, 2);
5966 enum rtx_code true_code = GET_CODE (cond);
5967 int comparison_p = COMPARISON_P (cond);
5968 rtx temp;
5969 int i;
5970 enum rtx_code false_code;
5971 rtx reversed;
5973 /* Simplify storing of the truth value. */
5974 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5975 return simplify_gen_relational (true_code, mode, VOIDmode,
5976 XEXP (cond, 0), XEXP (cond, 1));
5978 /* Also when the truth value has to be reversed. */
5979 if (comparison_p
5980 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5981 && (reversed = reversed_comparison (cond, mode)))
5982 return reversed;
5984 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5985 in it is being compared against certain values. Get the true and false
5986 comparisons and see if that says anything about the value of each arm. */
5988 if (comparison_p
5989 && ((false_code = reversed_comparison_code (cond, NULL))
5990 != UNKNOWN)
5991 && REG_P (XEXP (cond, 0)))
5993 HOST_WIDE_INT nzb;
5994 rtx from = XEXP (cond, 0);
5995 rtx true_val = XEXP (cond, 1);
5996 rtx false_val = true_val;
5997 int swapped = 0;
5999 /* If FALSE_CODE is EQ, swap the codes and arms. */
6001 if (false_code == EQ)
6003 swapped = 1, true_code = EQ, false_code = NE;
6004 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6007 /* If we are comparing against zero and the expression being tested has
6008 only a single bit that might be nonzero, that is its value when it is
6009 not equal to zero. Similarly if it is known to be -1 or 0. */
6011 if (true_code == EQ && true_val == const0_rtx
6012 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6014 false_code = EQ;
6015 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
6017 else if (true_code == EQ && true_val == const0_rtx
6018 && (num_sign_bit_copies (from, GET_MODE (from))
6019 == GET_MODE_BITSIZE (GET_MODE (from))))
6021 false_code = EQ;
6022 false_val = constm1_rtx;
6025 /* Now simplify an arm if we know the value of the register in the
6026 branch and it is used in the arm. Be careful due to the potential
6027 of locally-shared RTL. */
6029 if (reg_mentioned_p (from, true_rtx))
6030 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6031 from, true_val),
6032 pc_rtx, pc_rtx, 0, 0, 0);
6033 if (reg_mentioned_p (from, false_rtx))
6034 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6035 from, false_val),
6036 pc_rtx, pc_rtx, 0, 0, 0);
6038 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6039 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6041 true_rtx = XEXP (x, 1);
6042 false_rtx = XEXP (x, 2);
6043 true_code = GET_CODE (cond);
6046 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6047 reversed, do so to avoid needing two sets of patterns for
6048 subtract-and-branch insns. Similarly if we have a constant in the true
6049 arm, the false arm is the same as the first operand of the comparison, or
6050 the false arm is more complicated than the true arm. */
6052 if (comparison_p
6053 && reversed_comparison_code (cond, NULL) != UNKNOWN
6054 && (true_rtx == pc_rtx
6055 || (CONSTANT_P (true_rtx)
6056 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6057 || true_rtx == const0_rtx
6058 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6059 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6060 && !OBJECT_P (false_rtx))
6061 || reg_mentioned_p (true_rtx, false_rtx)
6062 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6064 true_code = reversed_comparison_code (cond, NULL);
6065 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6066 SUBST (XEXP (x, 1), false_rtx);
6067 SUBST (XEXP (x, 2), true_rtx);
6069 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6070 cond = XEXP (x, 0);
6072 /* It is possible that the conditional has been simplified out. */
6073 true_code = GET_CODE (cond);
6074 comparison_p = COMPARISON_P (cond);
6077 /* If the two arms are identical, we don't need the comparison. */
6079 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6080 return true_rtx;
6082 /* Convert a == b ? b : a to "a". */
6083 if (true_code == EQ && ! side_effects_p (cond)
6084 && !HONOR_NANS (mode)
6085 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6086 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6087 return false_rtx;
6088 else if (true_code == NE && ! side_effects_p (cond)
6089 && !HONOR_NANS (mode)
6090 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6091 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6092 return true_rtx;
6094 /* Look for cases where we have (abs x) or (neg (abs X)). */
6096 if (GET_MODE_CLASS (mode) == MODE_INT
6097 && comparison_p
6098 && XEXP (cond, 1) == const0_rtx
6099 && GET_CODE (false_rtx) == NEG
6100 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6101 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6102 && ! side_effects_p (true_rtx))
6103 switch (true_code)
6105 case GT:
6106 case GE:
6107 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6108 case LT:
6109 case LE:
6110 return
6111 simplify_gen_unary (NEG, mode,
6112 simplify_gen_unary (ABS, mode, true_rtx, mode),
6113 mode);
6114 default:
6115 break;
6118 /* Look for MIN or MAX. */
6120 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6121 && comparison_p
6122 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6123 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6124 && ! side_effects_p (cond))
6125 switch (true_code)
6127 case GE:
6128 case GT:
6129 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6130 case LE:
6131 case LT:
6132 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6133 case GEU:
6134 case GTU:
6135 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6136 case LEU:
6137 case LTU:
6138 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6139 default:
6140 break;
6143 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6144 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6145 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6146 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6147 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6148 neither 1 or -1, but it isn't worth checking for. */
6150 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6151 && comparison_p
6152 && GET_MODE_CLASS (mode) == MODE_INT
6153 && ! side_effects_p (x))
6155 rtx t = make_compound_operation (true_rtx, SET);
6156 rtx f = make_compound_operation (false_rtx, SET);
6157 rtx cond_op0 = XEXP (cond, 0);
6158 rtx cond_op1 = XEXP (cond, 1);
6159 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6160 enum machine_mode m = mode;
6161 rtx z = 0, c1 = NULL_RTX;
6163 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6164 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6165 || GET_CODE (t) == ASHIFT
6166 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6167 && rtx_equal_p (XEXP (t, 0), f))
6168 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6170 /* If an identity-zero op is commutative, check whether there
6171 would be a match if we swapped the operands. */
6172 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6173 || GET_CODE (t) == XOR)
6174 && rtx_equal_p (XEXP (t, 1), f))
6175 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6176 else if (GET_CODE (t) == SIGN_EXTEND
6177 && (GET_CODE (XEXP (t, 0)) == PLUS
6178 || GET_CODE (XEXP (t, 0)) == MINUS
6179 || GET_CODE (XEXP (t, 0)) == IOR
6180 || GET_CODE (XEXP (t, 0)) == XOR
6181 || GET_CODE (XEXP (t, 0)) == ASHIFT
6182 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6183 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6184 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6185 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6186 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6187 && (num_sign_bit_copies (f, GET_MODE (f))
6188 > (unsigned int)
6189 (GET_MODE_BITSIZE (mode)
6190 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6192 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6193 extend_op = SIGN_EXTEND;
6194 m = GET_MODE (XEXP (t, 0));
6196 else if (GET_CODE (t) == SIGN_EXTEND
6197 && (GET_CODE (XEXP (t, 0)) == PLUS
6198 || GET_CODE (XEXP (t, 0)) == IOR
6199 || GET_CODE (XEXP (t, 0)) == XOR)
6200 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6201 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6202 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6203 && (num_sign_bit_copies (f, GET_MODE (f))
6204 > (unsigned int)
6205 (GET_MODE_BITSIZE (mode)
6206 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6208 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6209 extend_op = SIGN_EXTEND;
6210 m = GET_MODE (XEXP (t, 0));
6212 else if (GET_CODE (t) == ZERO_EXTEND
6213 && (GET_CODE (XEXP (t, 0)) == PLUS
6214 || GET_CODE (XEXP (t, 0)) == MINUS
6215 || GET_CODE (XEXP (t, 0)) == IOR
6216 || GET_CODE (XEXP (t, 0)) == XOR
6217 || GET_CODE (XEXP (t, 0)) == ASHIFT
6218 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6219 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6220 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6221 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6222 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6223 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6224 && ((nonzero_bits (f, GET_MODE (f))
6225 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6226 == 0))
6228 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6229 extend_op = ZERO_EXTEND;
6230 m = GET_MODE (XEXP (t, 0));
6232 else if (GET_CODE (t) == ZERO_EXTEND
6233 && (GET_CODE (XEXP (t, 0)) == PLUS
6234 || GET_CODE (XEXP (t, 0)) == IOR
6235 || GET_CODE (XEXP (t, 0)) == XOR)
6236 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6237 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6238 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6239 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6240 && ((nonzero_bits (f, GET_MODE (f))
6241 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6242 == 0))
6244 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6245 extend_op = ZERO_EXTEND;
6246 m = GET_MODE (XEXP (t, 0));
6249 if (z)
6251 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6252 cond_op0, cond_op1),
6253 pc_rtx, pc_rtx, 0, 0, 0);
6254 temp = simplify_gen_binary (MULT, m, temp,
6255 simplify_gen_binary (MULT, m, c1,
6256 const_true_rtx));
6257 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6258 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6260 if (extend_op != UNKNOWN)
6261 temp = simplify_gen_unary (extend_op, mode, temp, m);
6263 return temp;
6267 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6268 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6269 negation of a single bit, we can convert this operation to a shift. We
6270 can actually do this more generally, but it doesn't seem worth it. */
6272 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6273 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6274 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6275 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6276 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6277 == GET_MODE_BITSIZE (mode))
6278 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6279 return
6280 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6281 gen_lowpart (mode, XEXP (cond, 0)), i);
6283 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6284 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6285 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6286 && GET_MODE (XEXP (cond, 0)) == mode
6287 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6288 == nonzero_bits (XEXP (cond, 0), mode)
6289 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6290 return XEXP (cond, 0);
6292 return x;
6295 /* Simplify X, a SET expression. Return the new expression. */
6297 static rtx
6298 simplify_set (rtx x)
6300 rtx src = SET_SRC (x);
6301 rtx dest = SET_DEST (x);
6302 enum machine_mode mode
6303 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6304 rtx other_insn;
6305 rtx *cc_use;
6307 /* (set (pc) (return)) gets written as (return). */
6308 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
6309 return src;
6311 /* Now that we know for sure which bits of SRC we are using, see if we can
6312 simplify the expression for the object knowing that we only need the
6313 low-order bits. */
6315 if (GET_MODE_CLASS (mode) == MODE_INT
6316 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6318 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6319 SUBST (SET_SRC (x), src);
6322 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6323 the comparison result and try to simplify it unless we already have used
6324 undobuf.other_insn. */
6325 if ((GET_MODE_CLASS (mode) == MODE_CC
6326 || GET_CODE (src) == COMPARE
6327 || CC0_P (dest))
6328 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6329 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6330 && COMPARISON_P (*cc_use)
6331 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6333 enum rtx_code old_code = GET_CODE (*cc_use);
6334 enum rtx_code new_code;
6335 rtx op0, op1, tmp;
6336 int other_changed = 0;
6337 rtx inner_compare = NULL_RTX;
6338 enum machine_mode compare_mode = GET_MODE (dest);
6340 if (GET_CODE (src) == COMPARE)
6342 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6343 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6345 inner_compare = op0;
6346 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6349 else
6350 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6352 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6353 op0, op1);
6354 if (!tmp)
6355 new_code = old_code;
6356 else if (!CONSTANT_P (tmp))
6358 new_code = GET_CODE (tmp);
6359 op0 = XEXP (tmp, 0);
6360 op1 = XEXP (tmp, 1);
6362 else
6364 rtx pat = PATTERN (other_insn);
6365 undobuf.other_insn = other_insn;
6366 SUBST (*cc_use, tmp);
6368 /* Attempt to simplify CC user. */
6369 if (GET_CODE (pat) == SET)
6371 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6372 if (new_rtx != NULL_RTX)
6373 SUBST (SET_SRC (pat), new_rtx);
6376 /* Convert X into a no-op move. */
6377 SUBST (SET_DEST (x), pc_rtx);
6378 SUBST (SET_SRC (x), pc_rtx);
6379 return x;
6382 /* Simplify our comparison, if possible. */
6383 new_code = simplify_comparison (new_code, &op0, &op1);
6385 #ifdef SELECT_CC_MODE
6386 /* If this machine has CC modes other than CCmode, check to see if we
6387 need to use a different CC mode here. */
6388 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6389 compare_mode = GET_MODE (op0);
6390 else if (inner_compare
6391 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6392 && new_code == old_code
6393 && op0 == XEXP (inner_compare, 0)
6394 && op1 == XEXP (inner_compare, 1))
6395 compare_mode = GET_MODE (inner_compare);
6396 else
6397 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6399 #ifndef HAVE_cc0
6400 /* If the mode changed, we have to change SET_DEST, the mode in the
6401 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6402 a hard register, just build new versions with the proper mode. If it
6403 is a pseudo, we lose unless it is only time we set the pseudo, in
6404 which case we can safely change its mode. */
6405 if (compare_mode != GET_MODE (dest))
6407 if (can_change_dest_mode (dest, 0, compare_mode))
6409 unsigned int regno = REGNO (dest);
6410 rtx new_dest;
6412 if (regno < FIRST_PSEUDO_REGISTER)
6413 new_dest = gen_rtx_REG (compare_mode, regno);
6414 else
6416 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6417 new_dest = regno_reg_rtx[regno];
6420 SUBST (SET_DEST (x), new_dest);
6421 SUBST (XEXP (*cc_use, 0), new_dest);
6422 other_changed = 1;
6424 dest = new_dest;
6427 #endif /* cc0 */
6428 #endif /* SELECT_CC_MODE */
6430 /* If the code changed, we have to build a new comparison in
6431 undobuf.other_insn. */
6432 if (new_code != old_code)
6434 int other_changed_previously = other_changed;
6435 unsigned HOST_WIDE_INT mask;
6436 rtx old_cc_use = *cc_use;
6438 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6439 dest, const0_rtx));
6440 other_changed = 1;
6442 /* If the only change we made was to change an EQ into an NE or
6443 vice versa, OP0 has only one bit that might be nonzero, and OP1
6444 is zero, check if changing the user of the condition code will
6445 produce a valid insn. If it won't, we can keep the original code
6446 in that insn by surrounding our operation with an XOR. */
6448 if (((old_code == NE && new_code == EQ)
6449 || (old_code == EQ && new_code == NE))
6450 && ! other_changed_previously && op1 == const0_rtx
6451 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
6452 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6454 rtx pat = PATTERN (other_insn), note = 0;
6456 if ((recog_for_combine (&pat, other_insn, &note) < 0
6457 && ! check_asm_operands (pat)))
6459 *cc_use = old_cc_use;
6460 other_changed = 0;
6462 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6463 op0, GEN_INT (mask));
6468 if (other_changed)
6469 undobuf.other_insn = other_insn;
6471 /* Otherwise, if we didn't previously have a COMPARE in the
6472 correct mode, we need one. */
6473 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6475 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6476 src = SET_SRC (x);
6478 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6480 SUBST (SET_SRC (x), op0);
6481 src = SET_SRC (x);
6483 /* Otherwise, update the COMPARE if needed. */
6484 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6486 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6487 src = SET_SRC (x);
6490 else
6492 /* Get SET_SRC in a form where we have placed back any
6493 compound expressions. Then do the checks below. */
6494 src = make_compound_operation (src, SET);
6495 SUBST (SET_SRC (x), src);
6498 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6499 and X being a REG or (subreg (reg)), we may be able to convert this to
6500 (set (subreg:m2 x) (op)).
6502 We can always do this if M1 is narrower than M2 because that means that
6503 we only care about the low bits of the result.
6505 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6506 perform a narrower operation than requested since the high-order bits will
6507 be undefined. On machine where it is defined, this transformation is safe
6508 as long as M1 and M2 have the same number of words. */
6510 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6511 && !OBJECT_P (SUBREG_REG (src))
6512 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6513 / UNITS_PER_WORD)
6514 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6515 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6516 #ifndef WORD_REGISTER_OPERATIONS
6517 && (GET_MODE_SIZE (GET_MODE (src))
6518 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6519 #endif
6520 #ifdef CANNOT_CHANGE_MODE_CLASS
6521 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6522 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6523 GET_MODE (SUBREG_REG (src)),
6524 GET_MODE (src)))
6525 #endif
6526 && (REG_P (dest)
6527 || (GET_CODE (dest) == SUBREG
6528 && REG_P (SUBREG_REG (dest)))))
6530 SUBST (SET_DEST (x),
6531 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6532 dest));
6533 SUBST (SET_SRC (x), SUBREG_REG (src));
6535 src = SET_SRC (x), dest = SET_DEST (x);
6538 #ifdef HAVE_cc0
6539 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6540 in SRC. */
6541 if (dest == cc0_rtx
6542 && GET_CODE (src) == SUBREG
6543 && subreg_lowpart_p (src)
6544 && (GET_MODE_BITSIZE (GET_MODE (src))
6545 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6547 rtx inner = SUBREG_REG (src);
6548 enum machine_mode inner_mode = GET_MODE (inner);
6550 /* Here we make sure that we don't have a sign bit on. */
6551 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6552 && (nonzero_bits (inner, inner_mode)
6553 < ((unsigned HOST_WIDE_INT) 1
6554 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6556 SUBST (SET_SRC (x), inner);
6557 src = SET_SRC (x);
6560 #endif
6562 #ifdef LOAD_EXTEND_OP
6563 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6564 would require a paradoxical subreg. Replace the subreg with a
6565 zero_extend to avoid the reload that would otherwise be required. */
6567 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6568 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6569 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6570 && SUBREG_BYTE (src) == 0
6571 && (GET_MODE_SIZE (GET_MODE (src))
6572 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6573 && MEM_P (SUBREG_REG (src)))
6575 SUBST (SET_SRC (x),
6576 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6577 GET_MODE (src), SUBREG_REG (src)));
6579 src = SET_SRC (x);
6581 #endif
6583 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6584 are comparing an item known to be 0 or -1 against 0, use a logical
6585 operation instead. Check for one of the arms being an IOR of the other
6586 arm with some value. We compute three terms to be IOR'ed together. In
6587 practice, at most two will be nonzero. Then we do the IOR's. */
6589 if (GET_CODE (dest) != PC
6590 && GET_CODE (src) == IF_THEN_ELSE
6591 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6592 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6593 && XEXP (XEXP (src, 0), 1) == const0_rtx
6594 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6595 #ifdef HAVE_conditional_move
6596 && ! can_conditionally_move_p (GET_MODE (src))
6597 #endif
6598 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6599 GET_MODE (XEXP (XEXP (src, 0), 0)))
6600 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6601 && ! side_effects_p (src))
6603 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6604 ? XEXP (src, 1) : XEXP (src, 2));
6605 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6606 ? XEXP (src, 2) : XEXP (src, 1));
6607 rtx term1 = const0_rtx, term2, term3;
6609 if (GET_CODE (true_rtx) == IOR
6610 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6611 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6612 else if (GET_CODE (true_rtx) == IOR
6613 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6614 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6615 else if (GET_CODE (false_rtx) == IOR
6616 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6617 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6618 else if (GET_CODE (false_rtx) == IOR
6619 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6620 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6622 term2 = simplify_gen_binary (AND, GET_MODE (src),
6623 XEXP (XEXP (src, 0), 0), true_rtx);
6624 term3 = simplify_gen_binary (AND, GET_MODE (src),
6625 simplify_gen_unary (NOT, GET_MODE (src),
6626 XEXP (XEXP (src, 0), 0),
6627 GET_MODE (src)),
6628 false_rtx);
6630 SUBST (SET_SRC (x),
6631 simplify_gen_binary (IOR, GET_MODE (src),
6632 simplify_gen_binary (IOR, GET_MODE (src),
6633 term1, term2),
6634 term3));
6636 src = SET_SRC (x);
6639 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6640 whole thing fail. */
6641 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6642 return src;
6643 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6644 return dest;
6645 else
6646 /* Convert this into a field assignment operation, if possible. */
6647 return make_field_assignment (x);
6650 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6651 result. */
6653 static rtx
6654 simplify_logical (rtx x)
6656 enum machine_mode mode = GET_MODE (x);
6657 rtx op0 = XEXP (x, 0);
6658 rtx op1 = XEXP (x, 1);
6660 switch (GET_CODE (x))
6662 case AND:
6663 /* We can call simplify_and_const_int only if we don't lose
6664 any (sign) bits when converting INTVAL (op1) to
6665 "unsigned HOST_WIDE_INT". */
6666 if (CONST_INT_P (op1)
6667 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6668 || INTVAL (op1) > 0))
6670 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6671 if (GET_CODE (x) != AND)
6672 return x;
6674 op0 = XEXP (x, 0);
6675 op1 = XEXP (x, 1);
6678 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6679 apply the distributive law and then the inverse distributive
6680 law to see if things simplify. */
6681 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6683 rtx result = distribute_and_simplify_rtx (x, 0);
6684 if (result)
6685 return result;
6687 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6689 rtx result = distribute_and_simplify_rtx (x, 1);
6690 if (result)
6691 return result;
6693 break;
6695 case IOR:
6696 /* If we have (ior (and A B) C), apply the distributive law and then
6697 the inverse distributive law to see if things simplify. */
6699 if (GET_CODE (op0) == AND)
6701 rtx result = distribute_and_simplify_rtx (x, 0);
6702 if (result)
6703 return result;
6706 if (GET_CODE (op1) == AND)
6708 rtx result = distribute_and_simplify_rtx (x, 1);
6709 if (result)
6710 return result;
6712 break;
6714 default:
6715 gcc_unreachable ();
6718 return x;
6721 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6722 operations" because they can be replaced with two more basic operations.
6723 ZERO_EXTEND is also considered "compound" because it can be replaced with
6724 an AND operation, which is simpler, though only one operation.
6726 The function expand_compound_operation is called with an rtx expression
6727 and will convert it to the appropriate shifts and AND operations,
6728 simplifying at each stage.
6730 The function make_compound_operation is called to convert an expression
6731 consisting of shifts and ANDs into the equivalent compound expression.
6732 It is the inverse of this function, loosely speaking. */
6734 static rtx
6735 expand_compound_operation (rtx x)
6737 unsigned HOST_WIDE_INT pos = 0, len;
6738 int unsignedp = 0;
6739 unsigned int modewidth;
6740 rtx tem;
6742 switch (GET_CODE (x))
6744 case ZERO_EXTEND:
6745 unsignedp = 1;
6746 case SIGN_EXTEND:
6747 /* We can't necessarily use a const_int for a multiword mode;
6748 it depends on implicitly extending the value.
6749 Since we don't know the right way to extend it,
6750 we can't tell whether the implicit way is right.
6752 Even for a mode that is no wider than a const_int,
6753 we can't win, because we need to sign extend one of its bits through
6754 the rest of it, and we don't know which bit. */
6755 if (CONST_INT_P (XEXP (x, 0)))
6756 return x;
6758 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6759 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6760 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6761 reloaded. If not for that, MEM's would very rarely be safe.
6763 Reject MODEs bigger than a word, because we might not be able
6764 to reference a two-register group starting with an arbitrary register
6765 (and currently gen_lowpart might crash for a SUBREG). */
6767 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6768 return x;
6770 /* Reject MODEs that aren't scalar integers because turning vector
6771 or complex modes into shifts causes problems. */
6773 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6774 return x;
6776 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6777 /* If the inner object has VOIDmode (the only way this can happen
6778 is if it is an ASM_OPERANDS), we can't do anything since we don't
6779 know how much masking to do. */
6780 if (len == 0)
6781 return x;
6783 break;
6785 case ZERO_EXTRACT:
6786 unsignedp = 1;
6788 /* ... fall through ... */
6790 case SIGN_EXTRACT:
6791 /* If the operand is a CLOBBER, just return it. */
6792 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6793 return XEXP (x, 0);
6795 if (!CONST_INT_P (XEXP (x, 1))
6796 || !CONST_INT_P (XEXP (x, 2))
6797 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6798 return x;
6800 /* Reject MODEs that aren't scalar integers because turning vector
6801 or complex modes into shifts causes problems. */
6803 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6804 return x;
6806 len = INTVAL (XEXP (x, 1));
6807 pos = INTVAL (XEXP (x, 2));
6809 /* This should stay within the object being extracted, fail otherwise. */
6810 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6811 return x;
6813 if (BITS_BIG_ENDIAN)
6814 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6816 break;
6818 default:
6819 return x;
6821 /* Convert sign extension to zero extension, if we know that the high
6822 bit is not set, as this is easier to optimize. It will be converted
6823 back to cheaper alternative in make_extraction. */
6824 if (GET_CODE (x) == SIGN_EXTEND
6825 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6826 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6827 & ~(((unsigned HOST_WIDE_INT)
6828 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6829 >> 1))
6830 == 0)))
6832 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6833 rtx temp2 = expand_compound_operation (temp);
6835 /* Make sure this is a profitable operation. */
6836 if (rtx_cost (x, SET, optimize_this_for_speed_p)
6837 > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6838 return temp2;
6839 else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6840 > rtx_cost (temp, SET, optimize_this_for_speed_p))
6841 return temp;
6842 else
6843 return x;
6846 /* We can optimize some special cases of ZERO_EXTEND. */
6847 if (GET_CODE (x) == ZERO_EXTEND)
6849 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6850 know that the last value didn't have any inappropriate bits
6851 set. */
6852 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6853 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6854 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6855 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6856 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6857 return XEXP (XEXP (x, 0), 0);
6859 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6860 if (GET_CODE (XEXP (x, 0)) == SUBREG
6861 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6862 && subreg_lowpart_p (XEXP (x, 0))
6863 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6864 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6865 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6866 return SUBREG_REG (XEXP (x, 0));
6868 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6869 is a comparison and STORE_FLAG_VALUE permits. This is like
6870 the first case, but it works even when GET_MODE (x) is larger
6871 than HOST_WIDE_INT. */
6872 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6873 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6874 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6875 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6876 <= HOST_BITS_PER_WIDE_INT)
6877 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6878 return XEXP (XEXP (x, 0), 0);
6880 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6881 if (GET_CODE (XEXP (x, 0)) == SUBREG
6882 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6883 && subreg_lowpart_p (XEXP (x, 0))
6884 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6885 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6886 <= HOST_BITS_PER_WIDE_INT)
6887 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6888 return SUBREG_REG (XEXP (x, 0));
6892 /* If we reach here, we want to return a pair of shifts. The inner
6893 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6894 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6895 logical depending on the value of UNSIGNEDP.
6897 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6898 converted into an AND of a shift.
6900 We must check for the case where the left shift would have a negative
6901 count. This can happen in a case like (x >> 31) & 255 on machines
6902 that can't shift by a constant. On those machines, we would first
6903 combine the shift with the AND to produce a variable-position
6904 extraction. Then the constant of 31 would be substituted in
6905 to produce such a position. */
6907 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6908 if (modewidth >= pos + len)
6910 enum machine_mode mode = GET_MODE (x);
6911 tem = gen_lowpart (mode, XEXP (x, 0));
6912 if (!tem || GET_CODE (tem) == CLOBBER)
6913 return x;
6914 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6915 tem, modewidth - pos - len);
6916 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6917 mode, tem, modewidth - len);
6919 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6920 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6921 simplify_shift_const (NULL_RTX, LSHIFTRT,
6922 GET_MODE (x),
6923 XEXP (x, 0), pos),
6924 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6925 else
6926 /* Any other cases we can't handle. */
6927 return x;
6929 /* If we couldn't do this for some reason, return the original
6930 expression. */
6931 if (GET_CODE (tem) == CLOBBER)
6932 return x;
6934 return tem;
6937 /* X is a SET which contains an assignment of one object into
6938 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6939 or certain SUBREGS). If possible, convert it into a series of
6940 logical operations.
6942 We half-heartedly support variable positions, but do not at all
6943 support variable lengths. */
6945 static const_rtx
6946 expand_field_assignment (const_rtx x)
6948 rtx inner;
6949 rtx pos; /* Always counts from low bit. */
6950 int len;
6951 rtx mask, cleared, masked;
6952 enum machine_mode compute_mode;
6954 /* Loop until we find something we can't simplify. */
6955 while (1)
6957 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6958 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6960 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6961 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6962 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6964 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6965 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6967 inner = XEXP (SET_DEST (x), 0);
6968 len = INTVAL (XEXP (SET_DEST (x), 1));
6969 pos = XEXP (SET_DEST (x), 2);
6971 /* A constant position should stay within the width of INNER. */
6972 if (CONST_INT_P (pos)
6973 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6974 break;
6976 if (BITS_BIG_ENDIAN)
6978 if (CONST_INT_P (pos))
6979 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6980 - INTVAL (pos));
6981 else if (GET_CODE (pos) == MINUS
6982 && CONST_INT_P (XEXP (pos, 1))
6983 && (INTVAL (XEXP (pos, 1))
6984 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6985 /* If position is ADJUST - X, new position is X. */
6986 pos = XEXP (pos, 0);
6987 else
6988 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6989 GEN_INT (GET_MODE_BITSIZE (
6990 GET_MODE (inner))
6991 - len),
6992 pos);
6996 /* A SUBREG between two modes that occupy the same numbers of words
6997 can be done by moving the SUBREG to the source. */
6998 else if (GET_CODE (SET_DEST (x)) == SUBREG
6999 /* We need SUBREGs to compute nonzero_bits properly. */
7000 && nonzero_sign_valid
7001 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7002 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7003 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7004 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7006 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7007 gen_lowpart
7008 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7009 SET_SRC (x)));
7010 continue;
7012 else
7013 break;
7015 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7016 inner = SUBREG_REG (inner);
7018 compute_mode = GET_MODE (inner);
7020 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7021 if (! SCALAR_INT_MODE_P (compute_mode))
7023 enum machine_mode imode;
7025 /* Don't do anything for vector or complex integral types. */
7026 if (! FLOAT_MODE_P (compute_mode))
7027 break;
7029 /* Try to find an integral mode to pun with. */
7030 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7031 if (imode == BLKmode)
7032 break;
7034 compute_mode = imode;
7035 inner = gen_lowpart (imode, inner);
7038 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7039 if (len >= HOST_BITS_PER_WIDE_INT)
7040 break;
7042 /* Now compute the equivalent expression. Make a copy of INNER
7043 for the SET_DEST in case it is a MEM into which we will substitute;
7044 we don't want shared RTL in that case. */
7045 mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
7046 cleared = simplify_gen_binary (AND, compute_mode,
7047 simplify_gen_unary (NOT, compute_mode,
7048 simplify_gen_binary (ASHIFT,
7049 compute_mode,
7050 mask, pos),
7051 compute_mode),
7052 inner);
7053 masked = simplify_gen_binary (ASHIFT, compute_mode,
7054 simplify_gen_binary (
7055 AND, compute_mode,
7056 gen_lowpart (compute_mode, SET_SRC (x)),
7057 mask),
7058 pos);
7060 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7061 simplify_gen_binary (IOR, compute_mode,
7062 cleared, masked));
7065 return x;
7068 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7069 it is an RTX that represents a variable starting position; otherwise,
7070 POS is the (constant) starting bit position (counted from the LSB).
7072 UNSIGNEDP is nonzero for an unsigned reference and zero for a
7073 signed reference.
7075 IN_DEST is nonzero if this is a reference in the destination of a
7076 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7077 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7078 be used.
7080 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7081 ZERO_EXTRACT should be built even for bits starting at bit 0.
7083 MODE is the desired mode of the result (if IN_DEST == 0).
7085 The result is an RTX for the extraction or NULL_RTX if the target
7086 can't handle it. */
7088 static rtx
7089 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7090 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7091 int in_dest, int in_compare)
7093 /* This mode describes the size of the storage area
7094 to fetch the overall value from. Within that, we
7095 ignore the POS lowest bits, etc. */
7096 enum machine_mode is_mode = GET_MODE (inner);
7097 enum machine_mode inner_mode;
7098 enum machine_mode wanted_inner_mode;
7099 enum machine_mode wanted_inner_reg_mode = word_mode;
7100 enum machine_mode pos_mode = word_mode;
7101 enum machine_mode extraction_mode = word_mode;
7102 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7103 rtx new_rtx = 0;
7104 rtx orig_pos_rtx = pos_rtx;
7105 HOST_WIDE_INT orig_pos;
7107 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7109 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7110 consider just the QI as the memory to extract from.
7111 The subreg adds or removes high bits; its mode is
7112 irrelevant to the meaning of this extraction,
7113 since POS and LEN count from the lsb. */
7114 if (MEM_P (SUBREG_REG (inner)))
7115 is_mode = GET_MODE (SUBREG_REG (inner));
7116 inner = SUBREG_REG (inner);
7118 else if (GET_CODE (inner) == ASHIFT
7119 && CONST_INT_P (XEXP (inner, 1))
7120 && pos_rtx == 0 && pos == 0
7121 && len > UINTVAL (XEXP (inner, 1)))
7123 /* We're extracting the least significant bits of an rtx
7124 (ashift X (const_int C)), where LEN > C. Extract the
7125 least significant (LEN - C) bits of X, giving an rtx
7126 whose mode is MODE, then shift it left C times. */
7127 new_rtx = make_extraction (mode, XEXP (inner, 0),
7128 0, 0, len - INTVAL (XEXP (inner, 1)),
7129 unsignedp, in_dest, in_compare);
7130 if (new_rtx != 0)
7131 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7134 inner_mode = GET_MODE (inner);
7136 if (pos_rtx && CONST_INT_P (pos_rtx))
7137 pos = INTVAL (pos_rtx), pos_rtx = 0;
7139 /* See if this can be done without an extraction. We never can if the
7140 width of the field is not the same as that of some integer mode. For
7141 registers, we can only avoid the extraction if the position is at the
7142 low-order bit and this is either not in the destination or we have the
7143 appropriate STRICT_LOW_PART operation available.
7145 For MEM, we can avoid an extract if the field starts on an appropriate
7146 boundary and we can change the mode of the memory reference. */
7148 if (tmode != BLKmode
7149 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7150 && !MEM_P (inner)
7151 && (inner_mode == tmode
7152 || !REG_P (inner)
7153 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
7154 GET_MODE_BITSIZE (inner_mode))
7155 || reg_truncated_to_mode (tmode, inner))
7156 && (! in_dest
7157 || (REG_P (inner)
7158 && have_insn_for (STRICT_LOW_PART, tmode))))
7159 || (MEM_P (inner) && pos_rtx == 0
7160 && (pos
7161 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7162 : BITS_PER_UNIT)) == 0
7163 /* We can't do this if we are widening INNER_MODE (it
7164 may not be aligned, for one thing). */
7165 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
7166 && (inner_mode == tmode
7167 || (! mode_dependent_address_p (XEXP (inner, 0))
7168 && ! MEM_VOLATILE_P (inner))))))
7170 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7171 field. If the original and current mode are the same, we need not
7172 adjust the offset. Otherwise, we do if bytes big endian.
7174 If INNER is not a MEM, get a piece consisting of just the field
7175 of interest (in this case POS % BITS_PER_WORD must be 0). */
7177 if (MEM_P (inner))
7179 HOST_WIDE_INT offset;
7181 /* POS counts from lsb, but make OFFSET count in memory order. */
7182 if (BYTES_BIG_ENDIAN)
7183 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
7184 else
7185 offset = pos / BITS_PER_UNIT;
7187 new_rtx = adjust_address_nv (inner, tmode, offset);
7189 else if (REG_P (inner))
7191 if (tmode != inner_mode)
7193 /* We can't call gen_lowpart in a DEST since we
7194 always want a SUBREG (see below) and it would sometimes
7195 return a new hard register. */
7196 if (pos || in_dest)
7198 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7200 if (WORDS_BIG_ENDIAN
7201 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7202 final_word = ((GET_MODE_SIZE (inner_mode)
7203 - GET_MODE_SIZE (tmode))
7204 / UNITS_PER_WORD) - final_word;
7206 final_word *= UNITS_PER_WORD;
7207 if (BYTES_BIG_ENDIAN &&
7208 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7209 final_word += (GET_MODE_SIZE (inner_mode)
7210 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7212 /* Avoid creating invalid subregs, for example when
7213 simplifying (x>>32)&255. */
7214 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7215 return NULL_RTX;
7217 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7219 else
7220 new_rtx = gen_lowpart (tmode, inner);
7222 else
7223 new_rtx = inner;
7225 else
7226 new_rtx = force_to_mode (inner, tmode,
7227 len >= HOST_BITS_PER_WIDE_INT
7228 ? ~(unsigned HOST_WIDE_INT) 0
7229 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7232 /* If this extraction is going into the destination of a SET,
7233 make a STRICT_LOW_PART unless we made a MEM. */
7235 if (in_dest)
7236 return (MEM_P (new_rtx) ? new_rtx
7237 : (GET_CODE (new_rtx) != SUBREG
7238 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7239 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7241 if (mode == tmode)
7242 return new_rtx;
7244 if (CONST_INT_P (new_rtx)
7245 || GET_CODE (new_rtx) == CONST_DOUBLE)
7246 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7247 mode, new_rtx, tmode);
7249 /* If we know that no extraneous bits are set, and that the high
7250 bit is not set, convert the extraction to the cheaper of
7251 sign and zero extension, that are equivalent in these cases. */
7252 if (flag_expensive_optimizations
7253 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
7254 && ((nonzero_bits (new_rtx, tmode)
7255 & ~(((unsigned HOST_WIDE_INT)
7256 GET_MODE_MASK (tmode))
7257 >> 1))
7258 == 0)))
7260 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7261 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7263 /* Prefer ZERO_EXTENSION, since it gives more information to
7264 backends. */
7265 if (rtx_cost (temp, SET, optimize_this_for_speed_p)
7266 <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
7267 return temp;
7268 return temp1;
7271 /* Otherwise, sign- or zero-extend unless we already are in the
7272 proper mode. */
7274 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7275 mode, new_rtx));
7278 /* Unless this is a COMPARE or we have a funny memory reference,
7279 don't do anything with zero-extending field extracts starting at
7280 the low-order bit since they are simple AND operations. */
7281 if (pos_rtx == 0 && pos == 0 && ! in_dest
7282 && ! in_compare && unsignedp)
7283 return 0;
7285 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7286 if the position is not a constant and the length is not 1. In all
7287 other cases, we would only be going outside our object in cases when
7288 an original shift would have been undefined. */
7289 if (MEM_P (inner)
7290 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
7291 || (pos_rtx != 0 && len != 1)))
7292 return 0;
7294 /* Get the mode to use should INNER not be a MEM, the mode for the position,
7295 and the mode for the result. */
7296 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7298 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7299 pos_mode = mode_for_extraction (EP_insv, 2);
7300 extraction_mode = mode_for_extraction (EP_insv, 3);
7303 if (! in_dest && unsignedp
7304 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7306 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7307 pos_mode = mode_for_extraction (EP_extzv, 3);
7308 extraction_mode = mode_for_extraction (EP_extzv, 0);
7311 if (! in_dest && ! unsignedp
7312 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7314 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7315 pos_mode = mode_for_extraction (EP_extv, 3);
7316 extraction_mode = mode_for_extraction (EP_extv, 0);
7319 /* Never narrow an object, since that might not be safe. */
7321 if (mode != VOIDmode
7322 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7323 extraction_mode = mode;
7325 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7326 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7327 pos_mode = GET_MODE (pos_rtx);
7329 /* If this is not from memory, the desired mode is the preferred mode
7330 for an extraction pattern's first input operand, or word_mode if there
7331 is none. */
7332 if (!MEM_P (inner))
7333 wanted_inner_mode = wanted_inner_reg_mode;
7334 else
7336 /* Be careful not to go beyond the extracted object and maintain the
7337 natural alignment of the memory. */
7338 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7339 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7340 > GET_MODE_BITSIZE (wanted_inner_mode))
7342 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7343 gcc_assert (wanted_inner_mode != VOIDmode);
7346 /* If we have to change the mode of memory and cannot, the desired mode
7347 is EXTRACTION_MODE. */
7348 if (inner_mode != wanted_inner_mode
7349 && (mode_dependent_address_p (XEXP (inner, 0))
7350 || MEM_VOLATILE_P (inner)
7351 || pos_rtx))
7352 wanted_inner_mode = extraction_mode;
7355 orig_pos = pos;
7357 if (BITS_BIG_ENDIAN)
7359 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7360 BITS_BIG_ENDIAN style. If position is constant, compute new
7361 position. Otherwise, build subtraction.
7362 Note that POS is relative to the mode of the original argument.
7363 If it's a MEM we need to recompute POS relative to that.
7364 However, if we're extracting from (or inserting into) a register,
7365 we want to recompute POS relative to wanted_inner_mode. */
7366 int width = (MEM_P (inner)
7367 ? GET_MODE_BITSIZE (is_mode)
7368 : GET_MODE_BITSIZE (wanted_inner_mode));
7370 if (pos_rtx == 0)
7371 pos = width - len - pos;
7372 else
7373 pos_rtx
7374 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7375 /* POS may be less than 0 now, but we check for that below.
7376 Note that it can only be less than 0 if !MEM_P (inner). */
7379 /* If INNER has a wider mode, and this is a constant extraction, try to
7380 make it smaller and adjust the byte to point to the byte containing
7381 the value. */
7382 if (wanted_inner_mode != VOIDmode
7383 && inner_mode != wanted_inner_mode
7384 && ! pos_rtx
7385 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7386 && MEM_P (inner)
7387 && ! mode_dependent_address_p (XEXP (inner, 0))
7388 && ! MEM_VOLATILE_P (inner))
7390 int offset = 0;
7392 /* The computations below will be correct if the machine is big
7393 endian in both bits and bytes or little endian in bits and bytes.
7394 If it is mixed, we must adjust. */
7396 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7397 adjust OFFSET to compensate. */
7398 if (BYTES_BIG_ENDIAN
7399 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7400 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7402 /* We can now move to the desired byte. */
7403 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7404 * GET_MODE_SIZE (wanted_inner_mode);
7405 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7407 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7408 && is_mode != wanted_inner_mode)
7409 offset = (GET_MODE_SIZE (is_mode)
7410 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7412 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7415 /* If INNER is not memory, get it into the proper mode. If we are changing
7416 its mode, POS must be a constant and smaller than the size of the new
7417 mode. */
7418 else if (!MEM_P (inner))
7420 /* On the LHS, don't create paradoxical subregs implicitely truncating
7421 the register unless TRULY_NOOP_TRUNCATION. */
7422 if (in_dest
7423 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
7424 GET_MODE_BITSIZE (wanted_inner_mode)))
7425 return NULL_RTX;
7427 if (GET_MODE (inner) != wanted_inner_mode
7428 && (pos_rtx != 0
7429 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7430 return NULL_RTX;
7432 if (orig_pos < 0)
7433 return NULL_RTX;
7435 inner = force_to_mode (inner, wanted_inner_mode,
7436 pos_rtx
7437 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7438 ? ~(unsigned HOST_WIDE_INT) 0
7439 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7440 << orig_pos),
7444 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7445 have to zero extend. Otherwise, we can just use a SUBREG. */
7446 if (pos_rtx != 0
7447 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7449 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7451 /* If we know that no extraneous bits are set, and that the high
7452 bit is not set, convert extraction to cheaper one - either
7453 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7454 cases. */
7455 if (flag_expensive_optimizations
7456 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
7457 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7458 & ~(((unsigned HOST_WIDE_INT)
7459 GET_MODE_MASK (GET_MODE (pos_rtx)))
7460 >> 1))
7461 == 0)))
7463 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7465 /* Prefer ZERO_EXTENSION, since it gives more information to
7466 backends. */
7467 if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
7468 < rtx_cost (temp, SET, optimize_this_for_speed_p))
7469 temp = temp1;
7471 pos_rtx = temp;
7473 else if (pos_rtx != 0
7474 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7475 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7477 /* Make POS_RTX unless we already have it and it is correct. If we don't
7478 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7479 be a CONST_INT. */
7480 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7481 pos_rtx = orig_pos_rtx;
7483 else if (pos_rtx == 0)
7484 pos_rtx = GEN_INT (pos);
7486 /* Make the required operation. See if we can use existing rtx. */
7487 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7488 extraction_mode, inner, GEN_INT (len), pos_rtx);
7489 if (! in_dest)
7490 new_rtx = gen_lowpart (mode, new_rtx);
7492 return new_rtx;
7495 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7496 with any other operations in X. Return X without that shift if so. */
7498 static rtx
7499 extract_left_shift (rtx x, int count)
7501 enum rtx_code code = GET_CODE (x);
7502 enum machine_mode mode = GET_MODE (x);
7503 rtx tem;
7505 switch (code)
7507 case ASHIFT:
7508 /* This is the shift itself. If it is wide enough, we will return
7509 either the value being shifted if the shift count is equal to
7510 COUNT or a shift for the difference. */
7511 if (CONST_INT_P (XEXP (x, 1))
7512 && INTVAL (XEXP (x, 1)) >= count)
7513 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7514 INTVAL (XEXP (x, 1)) - count);
7515 break;
7517 case NEG: case NOT:
7518 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7519 return simplify_gen_unary (code, mode, tem, mode);
7521 break;
7523 case PLUS: case IOR: case XOR: case AND:
7524 /* If we can safely shift this constant and we find the inner shift,
7525 make a new operation. */
7526 if (CONST_INT_P (XEXP (x, 1))
7527 && (UINTVAL (XEXP (x, 1))
7528 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7529 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7530 return simplify_gen_binary (code, mode, tem,
7531 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7533 break;
7535 default:
7536 break;
7539 return 0;
7542 /* Look at the expression rooted at X. Look for expressions
7543 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7544 Form these expressions.
7546 Return the new rtx, usually just X.
7548 Also, for machines like the VAX that don't have logical shift insns,
7549 try to convert logical to arithmetic shift operations in cases where
7550 they are equivalent. This undoes the canonicalizations to logical
7551 shifts done elsewhere.
7553 We try, as much as possible, to re-use rtl expressions to save memory.
7555 IN_CODE says what kind of expression we are processing. Normally, it is
7556 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7557 being kludges), it is MEM. When processing the arguments of a comparison
7558 or a COMPARE against zero, it is COMPARE. */
7560 static rtx
7561 make_compound_operation (rtx x, enum rtx_code in_code)
7563 enum rtx_code code = GET_CODE (x);
7564 enum machine_mode mode = GET_MODE (x);
7565 int mode_width = GET_MODE_BITSIZE (mode);
7566 rtx rhs, lhs;
7567 enum rtx_code next_code;
7568 int i, j;
7569 rtx new_rtx = 0;
7570 rtx tem;
7571 const char *fmt;
7573 /* Select the code to be used in recursive calls. Once we are inside an
7574 address, we stay there. If we have a comparison, set to COMPARE,
7575 but once inside, go back to our default of SET. */
7577 next_code = (code == MEM ? MEM
7578 : ((code == PLUS || code == MINUS)
7579 && SCALAR_INT_MODE_P (mode)) ? MEM
7580 : ((code == COMPARE || COMPARISON_P (x))
7581 && XEXP (x, 1) == const0_rtx) ? COMPARE
7582 : in_code == COMPARE ? SET : in_code);
7584 /* Process depending on the code of this operation. If NEW is set
7585 nonzero, it will be returned. */
7587 switch (code)
7589 case ASHIFT:
7590 /* Convert shifts by constants into multiplications if inside
7591 an address. */
7592 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7593 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7594 && INTVAL (XEXP (x, 1)) >= 0
7595 && SCALAR_INT_MODE_P (mode))
7597 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7598 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7600 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7601 if (GET_CODE (new_rtx) == NEG)
7603 new_rtx = XEXP (new_rtx, 0);
7604 multval = -multval;
7606 multval = trunc_int_for_mode (multval, mode);
7607 new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7609 break;
7611 case PLUS:
7612 lhs = XEXP (x, 0);
7613 rhs = XEXP (x, 1);
7614 lhs = make_compound_operation (lhs, next_code);
7615 rhs = make_compound_operation (rhs, next_code);
7616 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7617 && SCALAR_INT_MODE_P (mode))
7619 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7620 XEXP (lhs, 1));
7621 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7623 else if (GET_CODE (lhs) == MULT
7624 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7626 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7627 simplify_gen_unary (NEG, mode,
7628 XEXP (lhs, 1),
7629 mode));
7630 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7632 else
7634 SUBST (XEXP (x, 0), lhs);
7635 SUBST (XEXP (x, 1), rhs);
7636 goto maybe_swap;
7638 x = gen_lowpart (mode, new_rtx);
7639 goto maybe_swap;
7641 case MINUS:
7642 lhs = XEXP (x, 0);
7643 rhs = XEXP (x, 1);
7644 lhs = make_compound_operation (lhs, next_code);
7645 rhs = make_compound_operation (rhs, next_code);
7646 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7647 && SCALAR_INT_MODE_P (mode))
7649 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7650 XEXP (rhs, 1));
7651 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7653 else if (GET_CODE (rhs) == MULT
7654 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7656 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7657 simplify_gen_unary (NEG, mode,
7658 XEXP (rhs, 1),
7659 mode));
7660 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7662 else
7664 SUBST (XEXP (x, 0), lhs);
7665 SUBST (XEXP (x, 1), rhs);
7666 return x;
7668 return gen_lowpart (mode, new_rtx);
7670 case AND:
7671 /* If the second operand is not a constant, we can't do anything
7672 with it. */
7673 if (!CONST_INT_P (XEXP (x, 1)))
7674 break;
7676 /* If the constant is a power of two minus one and the first operand
7677 is a logical right shift, make an extraction. */
7678 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7679 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7681 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7682 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7683 0, in_code == COMPARE);
7686 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7687 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7688 && subreg_lowpart_p (XEXP (x, 0))
7689 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7690 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7692 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7693 next_code);
7694 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7695 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7696 0, in_code == COMPARE);
7698 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7699 else if ((GET_CODE (XEXP (x, 0)) == XOR
7700 || GET_CODE (XEXP (x, 0)) == IOR)
7701 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7702 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7703 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7705 /* Apply the distributive law, and then try to make extractions. */
7706 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7707 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7708 XEXP (x, 1)),
7709 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7710 XEXP (x, 1)));
7711 new_rtx = make_compound_operation (new_rtx, in_code);
7714 /* If we are have (and (rotate X C) M) and C is larger than the number
7715 of bits in M, this is an extraction. */
7717 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7718 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7719 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7720 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7722 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7723 new_rtx = make_extraction (mode, new_rtx,
7724 (GET_MODE_BITSIZE (mode)
7725 - INTVAL (XEXP (XEXP (x, 0), 1))),
7726 NULL_RTX, i, 1, 0, in_code == COMPARE);
7729 /* On machines without logical shifts, if the operand of the AND is
7730 a logical shift and our mask turns off all the propagated sign
7731 bits, we can replace the logical shift with an arithmetic shift. */
7732 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7733 && !have_insn_for (LSHIFTRT, mode)
7734 && have_insn_for (ASHIFTRT, mode)
7735 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7736 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7737 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7738 && mode_width <= HOST_BITS_PER_WIDE_INT)
7740 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7742 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7743 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7744 SUBST (XEXP (x, 0),
7745 gen_rtx_ASHIFTRT (mode,
7746 make_compound_operation
7747 (XEXP (XEXP (x, 0), 0), next_code),
7748 XEXP (XEXP (x, 0), 1)));
7751 /* If the constant is one less than a power of two, this might be
7752 representable by an extraction even if no shift is present.
7753 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7754 we are in a COMPARE. */
7755 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7756 new_rtx = make_extraction (mode,
7757 make_compound_operation (XEXP (x, 0),
7758 next_code),
7759 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7761 /* If we are in a comparison and this is an AND with a power of two,
7762 convert this into the appropriate bit extract. */
7763 else if (in_code == COMPARE
7764 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7765 new_rtx = make_extraction (mode,
7766 make_compound_operation (XEXP (x, 0),
7767 next_code),
7768 i, NULL_RTX, 1, 1, 0, 1);
7770 break;
7772 case LSHIFTRT:
7773 /* If the sign bit is known to be zero, replace this with an
7774 arithmetic shift. */
7775 if (have_insn_for (ASHIFTRT, mode)
7776 && ! have_insn_for (LSHIFTRT, mode)
7777 && mode_width <= HOST_BITS_PER_WIDE_INT
7778 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7780 new_rtx = gen_rtx_ASHIFTRT (mode,
7781 make_compound_operation (XEXP (x, 0),
7782 next_code),
7783 XEXP (x, 1));
7784 break;
7787 /* ... fall through ... */
7789 case ASHIFTRT:
7790 lhs = XEXP (x, 0);
7791 rhs = XEXP (x, 1);
7793 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7794 this is a SIGN_EXTRACT. */
7795 if (CONST_INT_P (rhs)
7796 && GET_CODE (lhs) == ASHIFT
7797 && CONST_INT_P (XEXP (lhs, 1))
7798 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7799 && INTVAL (rhs) < mode_width)
7801 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7802 new_rtx = make_extraction (mode, new_rtx,
7803 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7804 NULL_RTX, mode_width - INTVAL (rhs),
7805 code == LSHIFTRT, 0, in_code == COMPARE);
7806 break;
7809 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7810 If so, try to merge the shifts into a SIGN_EXTEND. We could
7811 also do this for some cases of SIGN_EXTRACT, but it doesn't
7812 seem worth the effort; the case checked for occurs on Alpha. */
7814 if (!OBJECT_P (lhs)
7815 && ! (GET_CODE (lhs) == SUBREG
7816 && (OBJECT_P (SUBREG_REG (lhs))))
7817 && CONST_INT_P (rhs)
7818 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7819 && INTVAL (rhs) < mode_width
7820 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7821 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7822 0, NULL_RTX, mode_width - INTVAL (rhs),
7823 code == LSHIFTRT, 0, in_code == COMPARE);
7825 break;
7827 case SUBREG:
7828 /* Call ourselves recursively on the inner expression. If we are
7829 narrowing the object and it has a different RTL code from
7830 what it originally did, do this SUBREG as a force_to_mode. */
7832 rtx inner = SUBREG_REG (x), simplified;
7834 tem = make_compound_operation (inner, in_code);
7836 simplified
7837 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7838 if (simplified)
7839 tem = simplified;
7841 if (GET_CODE (tem) != GET_CODE (inner)
7842 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7843 && subreg_lowpart_p (x))
7845 rtx newer
7846 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7848 /* If we have something other than a SUBREG, we might have
7849 done an expansion, so rerun ourselves. */
7850 if (GET_CODE (newer) != SUBREG)
7851 newer = make_compound_operation (newer, in_code);
7853 /* force_to_mode can expand compounds. If it just re-expanded the
7854 compound, use gen_lowpart to convert to the desired mode. */
7855 if (rtx_equal_p (newer, x)
7856 /* Likewise if it re-expanded the compound only partially.
7857 This happens for SUBREG of ZERO_EXTRACT if they extract
7858 the same number of bits. */
7859 || (GET_CODE (newer) == SUBREG
7860 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7861 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7862 && GET_CODE (inner) == AND
7863 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7864 return gen_lowpart (GET_MODE (x), tem);
7866 return newer;
7869 if (simplified)
7870 return tem;
7872 break;
7874 default:
7875 break;
7878 if (new_rtx)
7880 x = gen_lowpart (mode, new_rtx);
7881 code = GET_CODE (x);
7884 /* Now recursively process each operand of this operation. */
7885 fmt = GET_RTX_FORMAT (code);
7886 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7887 if (fmt[i] == 'e')
7889 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7890 SUBST (XEXP (x, i), new_rtx);
7892 else if (fmt[i] == 'E')
7893 for (j = 0; j < XVECLEN (x, i); j++)
7895 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7896 SUBST (XVECEXP (x, i, j), new_rtx);
7899 maybe_swap:
7900 /* If this is a commutative operation, the changes to the operands
7901 may have made it noncanonical. */
7902 if (COMMUTATIVE_ARITH_P (x)
7903 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7905 tem = XEXP (x, 0);
7906 SUBST (XEXP (x, 0), XEXP (x, 1));
7907 SUBST (XEXP (x, 1), tem);
7910 return x;
7913 /* Given M see if it is a value that would select a field of bits
7914 within an item, but not the entire word. Return -1 if not.
7915 Otherwise, return the starting position of the field, where 0 is the
7916 low-order bit.
7918 *PLEN is set to the length of the field. */
7920 static int
7921 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7923 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7924 int pos = m ? ctz_hwi (m) : -1;
7925 int len = 0;
7927 if (pos >= 0)
7928 /* Now shift off the low-order zero bits and see if we have a
7929 power of two minus 1. */
7930 len = exact_log2 ((m >> pos) + 1);
7932 if (len <= 0)
7933 pos = -1;
7935 *plen = len;
7936 return pos;
7939 /* If X refers to a register that equals REG in value, replace these
7940 references with REG. */
7941 static rtx
7942 canon_reg_for_combine (rtx x, rtx reg)
7944 rtx op0, op1, op2;
7945 const char *fmt;
7946 int i;
7947 bool copied;
7949 enum rtx_code code = GET_CODE (x);
7950 switch (GET_RTX_CLASS (code))
7952 case RTX_UNARY:
7953 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7954 if (op0 != XEXP (x, 0))
7955 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7956 GET_MODE (reg));
7957 break;
7959 case RTX_BIN_ARITH:
7960 case RTX_COMM_ARITH:
7961 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7962 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7963 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7964 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7965 break;
7967 case RTX_COMPARE:
7968 case RTX_COMM_COMPARE:
7969 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7970 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7971 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7972 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7973 GET_MODE (op0), op0, op1);
7974 break;
7976 case RTX_TERNARY:
7977 case RTX_BITFIELD_OPS:
7978 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7979 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7980 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7981 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7982 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7983 GET_MODE (op0), op0, op1, op2);
7985 case RTX_OBJ:
7986 if (REG_P (x))
7988 if (rtx_equal_p (get_last_value (reg), x)
7989 || rtx_equal_p (reg, get_last_value (x)))
7990 return reg;
7991 else
7992 break;
7995 /* fall through */
7997 default:
7998 fmt = GET_RTX_FORMAT (code);
7999 copied = false;
8000 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8001 if (fmt[i] == 'e')
8003 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8004 if (op != XEXP (x, i))
8006 if (!copied)
8008 copied = true;
8009 x = copy_rtx (x);
8011 XEXP (x, i) = op;
8014 else if (fmt[i] == 'E')
8016 int j;
8017 for (j = 0; j < XVECLEN (x, i); j++)
8019 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8020 if (op != XVECEXP (x, i, j))
8022 if (!copied)
8024 copied = true;
8025 x = copy_rtx (x);
8027 XVECEXP (x, i, j) = op;
8032 break;
8035 return x;
8038 /* Return X converted to MODE. If the value is already truncated to
8039 MODE we can just return a subreg even though in the general case we
8040 would need an explicit truncation. */
8042 static rtx
8043 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
8045 if (!CONST_INT_P (x)
8046 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8047 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8048 GET_MODE_BITSIZE (GET_MODE (x)))
8049 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8051 /* Bit-cast X into an integer mode. */
8052 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8053 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8054 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8055 x, GET_MODE (x));
8058 return gen_lowpart (mode, x);
8061 /* See if X can be simplified knowing that we will only refer to it in
8062 MODE and will only refer to those bits that are nonzero in MASK.
8063 If other bits are being computed or if masking operations are done
8064 that select a superset of the bits in MASK, they can sometimes be
8065 ignored.
8067 Return a possibly simplified expression, but always convert X to
8068 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8070 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8071 are all off in X. This is used when X will be complemented, by either
8072 NOT, NEG, or XOR. */
8074 static rtx
8075 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
8076 int just_select)
8078 enum rtx_code code = GET_CODE (x);
8079 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8080 enum machine_mode op_mode;
8081 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8082 rtx op0, op1, temp;
8084 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8085 code below will do the wrong thing since the mode of such an
8086 expression is VOIDmode.
8088 Also do nothing if X is a CLOBBER; this can happen if X was
8089 the return value from a call to gen_lowpart. */
8090 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8091 return x;
8093 /* We want to perform the operation is its present mode unless we know
8094 that the operation is valid in MODE, in which case we do the operation
8095 in MODE. */
8096 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8097 && have_insn_for (code, mode))
8098 ? mode : GET_MODE (x));
8100 /* It is not valid to do a right-shift in a narrower mode
8101 than the one it came in with. */
8102 if ((code == LSHIFTRT || code == ASHIFTRT)
8103 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
8104 op_mode = GET_MODE (x);
8106 /* Truncate MASK to fit OP_MODE. */
8107 if (op_mode)
8108 mask &= GET_MODE_MASK (op_mode);
8110 /* When we have an arithmetic operation, or a shift whose count we
8111 do not know, we need to assume that all bits up to the highest-order
8112 bit in MASK will be needed. This is how we form such a mask. */
8113 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8114 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8115 else
8116 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8117 - 1);
8119 /* Determine what bits of X are guaranteed to be (non)zero. */
8120 nonzero = nonzero_bits (x, mode);
8122 /* If none of the bits in X are needed, return a zero. */
8123 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8124 x = const0_rtx;
8126 /* If X is a CONST_INT, return a new one. Do this here since the
8127 test below will fail. */
8128 if (CONST_INT_P (x))
8130 if (SCALAR_INT_MODE_P (mode))
8131 return gen_int_mode (INTVAL (x) & mask, mode);
8132 else
8134 x = GEN_INT (INTVAL (x) & mask);
8135 return gen_lowpart_common (mode, x);
8139 /* If X is narrower than MODE and we want all the bits in X's mode, just
8140 get X in the proper mode. */
8141 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8142 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8143 return gen_lowpart (mode, x);
8145 /* We can ignore the effect of a SUBREG if it narrows the mode or
8146 if the constant masks to zero all the bits the mode doesn't have. */
8147 if (GET_CODE (x) == SUBREG
8148 && subreg_lowpart_p (x)
8149 && ((GET_MODE_SIZE (GET_MODE (x))
8150 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8151 || (0 == (mask
8152 & GET_MODE_MASK (GET_MODE (x))
8153 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8154 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8156 /* The arithmetic simplifications here only work for scalar integer modes. */
8157 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8158 return gen_lowpart_or_truncate (mode, x);
8160 switch (code)
8162 case CLOBBER:
8163 /* If X is a (clobber (const_int)), return it since we know we are
8164 generating something that won't match. */
8165 return x;
8167 case SIGN_EXTEND:
8168 case ZERO_EXTEND:
8169 case ZERO_EXTRACT:
8170 case SIGN_EXTRACT:
8171 x = expand_compound_operation (x);
8172 if (GET_CODE (x) != code)
8173 return force_to_mode (x, mode, mask, next_select);
8174 break;
8176 case TRUNCATE:
8177 /* Similarly for a truncate. */
8178 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8180 case AND:
8181 /* If this is an AND with a constant, convert it into an AND
8182 whose constant is the AND of that constant with MASK. If it
8183 remains an AND of MASK, delete it since it is redundant. */
8185 if (CONST_INT_P (XEXP (x, 1)))
8187 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8188 mask & INTVAL (XEXP (x, 1)));
8190 /* If X is still an AND, see if it is an AND with a mask that
8191 is just some low-order bits. If so, and it is MASK, we don't
8192 need it. */
8194 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8195 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8196 == mask))
8197 x = XEXP (x, 0);
8199 /* If it remains an AND, try making another AND with the bits
8200 in the mode mask that aren't in MASK turned on. If the
8201 constant in the AND is wide enough, this might make a
8202 cheaper constant. */
8204 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8205 && GET_MODE_MASK (GET_MODE (x)) != mask
8206 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
8208 unsigned HOST_WIDE_INT cval
8209 = UINTVAL (XEXP (x, 1))
8210 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8211 int width = GET_MODE_BITSIZE (GET_MODE (x));
8212 rtx y;
8214 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8215 number, sign extend it. */
8216 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8217 && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8218 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8220 y = simplify_gen_binary (AND, GET_MODE (x),
8221 XEXP (x, 0), GEN_INT (cval));
8222 if (rtx_cost (y, SET, optimize_this_for_speed_p)
8223 < rtx_cost (x, SET, optimize_this_for_speed_p))
8224 x = y;
8227 break;
8230 goto binop;
8232 case PLUS:
8233 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8234 low-order bits (as in an alignment operation) and FOO is already
8235 aligned to that boundary, mask C1 to that boundary as well.
8236 This may eliminate that PLUS and, later, the AND. */
8239 unsigned int width = GET_MODE_BITSIZE (mode);
8240 unsigned HOST_WIDE_INT smask = mask;
8242 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8243 number, sign extend it. */
8245 if (width < HOST_BITS_PER_WIDE_INT
8246 && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8247 smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8249 if (CONST_INT_P (XEXP (x, 1))
8250 && exact_log2 (- smask) >= 0
8251 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8252 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8253 return force_to_mode (plus_constant (XEXP (x, 0),
8254 (INTVAL (XEXP (x, 1)) & smask)),
8255 mode, smask, next_select);
8258 /* ... fall through ... */
8260 case MULT:
8261 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8262 most significant bit in MASK since carries from those bits will
8263 affect the bits we are interested in. */
8264 mask = fuller_mask;
8265 goto binop;
8267 case MINUS:
8268 /* If X is (minus C Y) where C's least set bit is larger than any bit
8269 in the mask, then we may replace with (neg Y). */
8270 if (CONST_INT_P (XEXP (x, 0))
8271 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8272 & -INTVAL (XEXP (x, 0))))
8273 > mask))
8275 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8276 GET_MODE (x));
8277 return force_to_mode (x, mode, mask, next_select);
8280 /* Similarly, if C contains every bit in the fuller_mask, then we may
8281 replace with (not Y). */
8282 if (CONST_INT_P (XEXP (x, 0))
8283 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8285 x = simplify_gen_unary (NOT, GET_MODE (x),
8286 XEXP (x, 1), GET_MODE (x));
8287 return force_to_mode (x, mode, mask, next_select);
8290 mask = fuller_mask;
8291 goto binop;
8293 case IOR:
8294 case XOR:
8295 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8296 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8297 operation which may be a bitfield extraction. Ensure that the
8298 constant we form is not wider than the mode of X. */
8300 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8301 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8302 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8303 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8304 && CONST_INT_P (XEXP (x, 1))
8305 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8306 + floor_log2 (INTVAL (XEXP (x, 1))))
8307 < GET_MODE_BITSIZE (GET_MODE (x)))
8308 && (UINTVAL (XEXP (x, 1))
8309 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8311 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8312 << INTVAL (XEXP (XEXP (x, 0), 1)));
8313 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8314 XEXP (XEXP (x, 0), 0), temp);
8315 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8316 XEXP (XEXP (x, 0), 1));
8317 return force_to_mode (x, mode, mask, next_select);
8320 binop:
8321 /* For most binary operations, just propagate into the operation and
8322 change the mode if we have an operation of that mode. */
8324 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8325 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8327 /* If we ended up truncating both operands, truncate the result of the
8328 operation instead. */
8329 if (GET_CODE (op0) == TRUNCATE
8330 && GET_CODE (op1) == TRUNCATE)
8332 op0 = XEXP (op0, 0);
8333 op1 = XEXP (op1, 0);
8336 op0 = gen_lowpart_or_truncate (op_mode, op0);
8337 op1 = gen_lowpart_or_truncate (op_mode, op1);
8339 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8340 x = simplify_gen_binary (code, op_mode, op0, op1);
8341 break;
8343 case ASHIFT:
8344 /* For left shifts, do the same, but just for the first operand.
8345 However, we cannot do anything with shifts where we cannot
8346 guarantee that the counts are smaller than the size of the mode
8347 because such a count will have a different meaning in a
8348 wider mode. */
8350 if (! (CONST_INT_P (XEXP (x, 1))
8351 && INTVAL (XEXP (x, 1)) >= 0
8352 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
8353 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8354 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8355 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
8356 break;
8358 /* If the shift count is a constant and we can do arithmetic in
8359 the mode of the shift, refine which bits we need. Otherwise, use the
8360 conservative form of the mask. */
8361 if (CONST_INT_P (XEXP (x, 1))
8362 && INTVAL (XEXP (x, 1)) >= 0
8363 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
8364 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8365 mask >>= INTVAL (XEXP (x, 1));
8366 else
8367 mask = fuller_mask;
8369 op0 = gen_lowpart_or_truncate (op_mode,
8370 force_to_mode (XEXP (x, 0), op_mode,
8371 mask, next_select));
8373 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8374 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8375 break;
8377 case LSHIFTRT:
8378 /* Here we can only do something if the shift count is a constant,
8379 this shift constant is valid for the host, and we can do arithmetic
8380 in OP_MODE. */
8382 if (CONST_INT_P (XEXP (x, 1))
8383 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8384 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8386 rtx inner = XEXP (x, 0);
8387 unsigned HOST_WIDE_INT inner_mask;
8389 /* Select the mask of the bits we need for the shift operand. */
8390 inner_mask = mask << INTVAL (XEXP (x, 1));
8392 /* We can only change the mode of the shift if we can do arithmetic
8393 in the mode of the shift and INNER_MASK is no wider than the
8394 width of X's mode. */
8395 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8396 op_mode = GET_MODE (x);
8398 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8400 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8401 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8404 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8405 shift and AND produces only copies of the sign bit (C2 is one less
8406 than a power of two), we can do this with just a shift. */
8408 if (GET_CODE (x) == LSHIFTRT
8409 && CONST_INT_P (XEXP (x, 1))
8410 /* The shift puts one of the sign bit copies in the least significant
8411 bit. */
8412 && ((INTVAL (XEXP (x, 1))
8413 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8414 >= GET_MODE_BITSIZE (GET_MODE (x)))
8415 && exact_log2 (mask + 1) >= 0
8416 /* Number of bits left after the shift must be more than the mask
8417 needs. */
8418 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8419 <= GET_MODE_BITSIZE (GET_MODE (x)))
8420 /* Must be more sign bit copies than the mask needs. */
8421 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8422 >= exact_log2 (mask + 1)))
8423 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8424 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
8425 - exact_log2 (mask + 1)));
8427 goto shiftrt;
8429 case ASHIFTRT:
8430 /* If we are just looking for the sign bit, we don't need this shift at
8431 all, even if it has a variable count. */
8432 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8433 && (mask == ((unsigned HOST_WIDE_INT) 1
8434 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8435 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8437 /* If this is a shift by a constant, get a mask that contains those bits
8438 that are not copies of the sign bit. We then have two cases: If
8439 MASK only includes those bits, this can be a logical shift, which may
8440 allow simplifications. If MASK is a single-bit field not within
8441 those bits, we are requesting a copy of the sign bit and hence can
8442 shift the sign bit to the appropriate location. */
8444 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8445 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8447 int i;
8449 /* If the considered data is wider than HOST_WIDE_INT, we can't
8450 represent a mask for all its bits in a single scalar.
8451 But we only care about the lower bits, so calculate these. */
8453 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8455 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8457 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8458 is the number of bits a full-width mask would have set.
8459 We need only shift if these are fewer than nonzero can
8460 hold. If not, we must keep all bits set in nonzero. */
8462 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8463 < HOST_BITS_PER_WIDE_INT)
8464 nonzero >>= INTVAL (XEXP (x, 1))
8465 + HOST_BITS_PER_WIDE_INT
8466 - GET_MODE_BITSIZE (GET_MODE (x)) ;
8468 else
8470 nonzero = GET_MODE_MASK (GET_MODE (x));
8471 nonzero >>= INTVAL (XEXP (x, 1));
8474 if ((mask & ~nonzero) == 0)
8476 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8477 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8478 if (GET_CODE (x) != ASHIFTRT)
8479 return force_to_mode (x, mode, mask, next_select);
8482 else if ((i = exact_log2 (mask)) >= 0)
8484 x = simplify_shift_const
8485 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8486 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
8488 if (GET_CODE (x) != ASHIFTRT)
8489 return force_to_mode (x, mode, mask, next_select);
8493 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8494 even if the shift count isn't a constant. */
8495 if (mask == 1)
8496 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8497 XEXP (x, 0), XEXP (x, 1));
8499 shiftrt:
8501 /* If this is a zero- or sign-extension operation that just affects bits
8502 we don't care about, remove it. Be sure the call above returned
8503 something that is still a shift. */
8505 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8506 && CONST_INT_P (XEXP (x, 1))
8507 && INTVAL (XEXP (x, 1)) >= 0
8508 && (INTVAL (XEXP (x, 1))
8509 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
8510 && GET_CODE (XEXP (x, 0)) == ASHIFT
8511 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8512 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8513 next_select);
8515 break;
8517 case ROTATE:
8518 case ROTATERT:
8519 /* If the shift count is constant and we can do computations
8520 in the mode of X, compute where the bits we care about are.
8521 Otherwise, we can't do anything. Don't change the mode of
8522 the shift or propagate MODE into the shift, though. */
8523 if (CONST_INT_P (XEXP (x, 1))
8524 && INTVAL (XEXP (x, 1)) >= 0)
8526 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8527 GET_MODE (x), GEN_INT (mask),
8528 XEXP (x, 1));
8529 if (temp && CONST_INT_P (temp))
8530 SUBST (XEXP (x, 0),
8531 force_to_mode (XEXP (x, 0), GET_MODE (x),
8532 INTVAL (temp), next_select));
8534 break;
8536 case NEG:
8537 /* If we just want the low-order bit, the NEG isn't needed since it
8538 won't change the low-order bit. */
8539 if (mask == 1)
8540 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8542 /* We need any bits less significant than the most significant bit in
8543 MASK since carries from those bits will affect the bits we are
8544 interested in. */
8545 mask = fuller_mask;
8546 goto unop;
8548 case NOT:
8549 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8550 same as the XOR case above. Ensure that the constant we form is not
8551 wider than the mode of X. */
8553 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8554 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8555 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8556 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8557 < GET_MODE_BITSIZE (GET_MODE (x)))
8558 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8560 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8561 GET_MODE (x));
8562 temp = simplify_gen_binary (XOR, GET_MODE (x),
8563 XEXP (XEXP (x, 0), 0), temp);
8564 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8565 temp, XEXP (XEXP (x, 0), 1));
8567 return force_to_mode (x, mode, mask, next_select);
8570 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8571 use the full mask inside the NOT. */
8572 mask = fuller_mask;
8574 unop:
8575 op0 = gen_lowpart_or_truncate (op_mode,
8576 force_to_mode (XEXP (x, 0), mode, mask,
8577 next_select));
8578 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8579 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8580 break;
8582 case NE:
8583 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8584 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8585 which is equal to STORE_FLAG_VALUE. */
8586 if ((mask & ~STORE_FLAG_VALUE) == 0
8587 && XEXP (x, 1) == const0_rtx
8588 && GET_MODE (XEXP (x, 0)) == mode
8589 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8590 && (nonzero_bits (XEXP (x, 0), mode)
8591 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8592 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8594 break;
8596 case IF_THEN_ELSE:
8597 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8598 written in a narrower mode. We play it safe and do not do so. */
8600 SUBST (XEXP (x, 1),
8601 gen_lowpart_or_truncate (GET_MODE (x),
8602 force_to_mode (XEXP (x, 1), mode,
8603 mask, next_select)));
8604 SUBST (XEXP (x, 2),
8605 gen_lowpart_or_truncate (GET_MODE (x),
8606 force_to_mode (XEXP (x, 2), mode,
8607 mask, next_select)));
8608 break;
8610 default:
8611 break;
8614 /* Ensure we return a value of the proper mode. */
8615 return gen_lowpart_or_truncate (mode, x);
8618 /* Return nonzero if X is an expression that has one of two values depending on
8619 whether some other value is zero or nonzero. In that case, we return the
8620 value that is being tested, *PTRUE is set to the value if the rtx being
8621 returned has a nonzero value, and *PFALSE is set to the other alternative.
8623 If we return zero, we set *PTRUE and *PFALSE to X. */
8625 static rtx
8626 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8628 enum machine_mode mode = GET_MODE (x);
8629 enum rtx_code code = GET_CODE (x);
8630 rtx cond0, cond1, true0, true1, false0, false1;
8631 unsigned HOST_WIDE_INT nz;
8633 /* If we are comparing a value against zero, we are done. */
8634 if ((code == NE || code == EQ)
8635 && XEXP (x, 1) == const0_rtx)
8637 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8638 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8639 return XEXP (x, 0);
8642 /* If this is a unary operation whose operand has one of two values, apply
8643 our opcode to compute those values. */
8644 else if (UNARY_P (x)
8645 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8647 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8648 *pfalse = simplify_gen_unary (code, mode, false0,
8649 GET_MODE (XEXP (x, 0)));
8650 return cond0;
8653 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8654 make can't possibly match and would suppress other optimizations. */
8655 else if (code == COMPARE)
8658 /* If this is a binary operation, see if either side has only one of two
8659 values. If either one does or if both do and they are conditional on
8660 the same value, compute the new true and false values. */
8661 else if (BINARY_P (x))
8663 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8664 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8666 if ((cond0 != 0 || cond1 != 0)
8667 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8669 /* If if_then_else_cond returned zero, then true/false are the
8670 same rtl. We must copy one of them to prevent invalid rtl
8671 sharing. */
8672 if (cond0 == 0)
8673 true0 = copy_rtx (true0);
8674 else if (cond1 == 0)
8675 true1 = copy_rtx (true1);
8677 if (COMPARISON_P (x))
8679 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8680 true0, true1);
8681 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8682 false0, false1);
8684 else
8686 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8687 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8690 return cond0 ? cond0 : cond1;
8693 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8694 operands is zero when the other is nonzero, and vice-versa,
8695 and STORE_FLAG_VALUE is 1 or -1. */
8697 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8698 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8699 || code == UMAX)
8700 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8702 rtx op0 = XEXP (XEXP (x, 0), 1);
8703 rtx op1 = XEXP (XEXP (x, 1), 1);
8705 cond0 = XEXP (XEXP (x, 0), 0);
8706 cond1 = XEXP (XEXP (x, 1), 0);
8708 if (COMPARISON_P (cond0)
8709 && COMPARISON_P (cond1)
8710 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8711 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8712 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8713 || ((swap_condition (GET_CODE (cond0))
8714 == reversed_comparison_code (cond1, NULL))
8715 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8716 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8717 && ! side_effects_p (x))
8719 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8720 *pfalse = simplify_gen_binary (MULT, mode,
8721 (code == MINUS
8722 ? simplify_gen_unary (NEG, mode,
8723 op1, mode)
8724 : op1),
8725 const_true_rtx);
8726 return cond0;
8730 /* Similarly for MULT, AND and UMIN, except that for these the result
8731 is always zero. */
8732 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8733 && (code == MULT || code == AND || code == UMIN)
8734 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8736 cond0 = XEXP (XEXP (x, 0), 0);
8737 cond1 = XEXP (XEXP (x, 1), 0);
8739 if (COMPARISON_P (cond0)
8740 && COMPARISON_P (cond1)
8741 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8742 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8743 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8744 || ((swap_condition (GET_CODE (cond0))
8745 == reversed_comparison_code (cond1, NULL))
8746 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8747 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8748 && ! side_effects_p (x))
8750 *ptrue = *pfalse = const0_rtx;
8751 return cond0;
8756 else if (code == IF_THEN_ELSE)
8758 /* If we have IF_THEN_ELSE already, extract the condition and
8759 canonicalize it if it is NE or EQ. */
8760 cond0 = XEXP (x, 0);
8761 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8762 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8763 return XEXP (cond0, 0);
8764 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8766 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8767 return XEXP (cond0, 0);
8769 else
8770 return cond0;
8773 /* If X is a SUBREG, we can narrow both the true and false values
8774 if the inner expression, if there is a condition. */
8775 else if (code == SUBREG
8776 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8777 &true0, &false0)))
8779 true0 = simplify_gen_subreg (mode, true0,
8780 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8781 false0 = simplify_gen_subreg (mode, false0,
8782 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8783 if (true0 && false0)
8785 *ptrue = true0;
8786 *pfalse = false0;
8787 return cond0;
8791 /* If X is a constant, this isn't special and will cause confusions
8792 if we treat it as such. Likewise if it is equivalent to a constant. */
8793 else if (CONSTANT_P (x)
8794 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8797 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8798 will be least confusing to the rest of the compiler. */
8799 else if (mode == BImode)
8801 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8802 return x;
8805 /* If X is known to be either 0 or -1, those are the true and
8806 false values when testing X. */
8807 else if (x == constm1_rtx || x == const0_rtx
8808 || (mode != VOIDmode
8809 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8811 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8812 return x;
8815 /* Likewise for 0 or a single bit. */
8816 else if (SCALAR_INT_MODE_P (mode)
8817 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8818 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8820 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8821 return x;
8824 /* Otherwise fail; show no condition with true and false values the same. */
8825 *ptrue = *pfalse = x;
8826 return 0;
8829 /* Return the value of expression X given the fact that condition COND
8830 is known to be true when applied to REG as its first operand and VAL
8831 as its second. X is known to not be shared and so can be modified in
8832 place.
8834 We only handle the simplest cases, and specifically those cases that
8835 arise with IF_THEN_ELSE expressions. */
8837 static rtx
8838 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8840 enum rtx_code code = GET_CODE (x);
8841 rtx temp;
8842 const char *fmt;
8843 int i, j;
8845 if (side_effects_p (x))
8846 return x;
8848 /* If either operand of the condition is a floating point value,
8849 then we have to avoid collapsing an EQ comparison. */
8850 if (cond == EQ
8851 && rtx_equal_p (x, reg)
8852 && ! FLOAT_MODE_P (GET_MODE (x))
8853 && ! FLOAT_MODE_P (GET_MODE (val)))
8854 return val;
8856 if (cond == UNEQ && rtx_equal_p (x, reg))
8857 return val;
8859 /* If X is (abs REG) and we know something about REG's relationship
8860 with zero, we may be able to simplify this. */
8862 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8863 switch (cond)
8865 case GE: case GT: case EQ:
8866 return XEXP (x, 0);
8867 case LT: case LE:
8868 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8869 XEXP (x, 0),
8870 GET_MODE (XEXP (x, 0)));
8871 default:
8872 break;
8875 /* The only other cases we handle are MIN, MAX, and comparisons if the
8876 operands are the same as REG and VAL. */
8878 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8880 if (rtx_equal_p (XEXP (x, 0), val))
8881 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8883 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8885 if (COMPARISON_P (x))
8887 if (comparison_dominates_p (cond, code))
8888 return const_true_rtx;
8890 code = reversed_comparison_code (x, NULL);
8891 if (code != UNKNOWN
8892 && comparison_dominates_p (cond, code))
8893 return const0_rtx;
8894 else
8895 return x;
8897 else if (code == SMAX || code == SMIN
8898 || code == UMIN || code == UMAX)
8900 int unsignedp = (code == UMIN || code == UMAX);
8902 /* Do not reverse the condition when it is NE or EQ.
8903 This is because we cannot conclude anything about
8904 the value of 'SMAX (x, y)' when x is not equal to y,
8905 but we can when x equals y. */
8906 if ((code == SMAX || code == UMAX)
8907 && ! (cond == EQ || cond == NE))
8908 cond = reverse_condition (cond);
8910 switch (cond)
8912 case GE: case GT:
8913 return unsignedp ? x : XEXP (x, 1);
8914 case LE: case LT:
8915 return unsignedp ? x : XEXP (x, 0);
8916 case GEU: case GTU:
8917 return unsignedp ? XEXP (x, 1) : x;
8918 case LEU: case LTU:
8919 return unsignedp ? XEXP (x, 0) : x;
8920 default:
8921 break;
8926 else if (code == SUBREG)
8928 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8929 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8931 if (SUBREG_REG (x) != r)
8933 /* We must simplify subreg here, before we lose track of the
8934 original inner_mode. */
8935 new_rtx = simplify_subreg (GET_MODE (x), r,
8936 inner_mode, SUBREG_BYTE (x));
8937 if (new_rtx)
8938 return new_rtx;
8939 else
8940 SUBST (SUBREG_REG (x), r);
8943 return x;
8945 /* We don't have to handle SIGN_EXTEND here, because even in the
8946 case of replacing something with a modeless CONST_INT, a
8947 CONST_INT is already (supposed to be) a valid sign extension for
8948 its narrower mode, which implies it's already properly
8949 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8950 story is different. */
8951 else if (code == ZERO_EXTEND)
8953 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8954 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8956 if (XEXP (x, 0) != r)
8958 /* We must simplify the zero_extend here, before we lose
8959 track of the original inner_mode. */
8960 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8961 r, inner_mode);
8962 if (new_rtx)
8963 return new_rtx;
8964 else
8965 SUBST (XEXP (x, 0), r);
8968 return x;
8971 fmt = GET_RTX_FORMAT (code);
8972 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8974 if (fmt[i] == 'e')
8975 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8976 else if (fmt[i] == 'E')
8977 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8978 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8979 cond, reg, val));
8982 return x;
8985 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8986 assignment as a field assignment. */
8988 static int
8989 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8991 if (x == y || rtx_equal_p (x, y))
8992 return 1;
8994 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8995 return 0;
8997 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8998 Note that all SUBREGs of MEM are paradoxical; otherwise they
8999 would have been rewritten. */
9000 if (MEM_P (x) && GET_CODE (y) == SUBREG
9001 && MEM_P (SUBREG_REG (y))
9002 && rtx_equal_p (SUBREG_REG (y),
9003 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9004 return 1;
9006 if (MEM_P (y) && GET_CODE (x) == SUBREG
9007 && MEM_P (SUBREG_REG (x))
9008 && rtx_equal_p (SUBREG_REG (x),
9009 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9010 return 1;
9012 /* We used to see if get_last_value of X and Y were the same but that's
9013 not correct. In one direction, we'll cause the assignment to have
9014 the wrong destination and in the case, we'll import a register into this
9015 insn that might have already have been dead. So fail if none of the
9016 above cases are true. */
9017 return 0;
9020 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9021 Return that assignment if so.
9023 We only handle the most common cases. */
9025 static rtx
9026 make_field_assignment (rtx x)
9028 rtx dest = SET_DEST (x);
9029 rtx src = SET_SRC (x);
9030 rtx assign;
9031 rtx rhs, lhs;
9032 HOST_WIDE_INT c1;
9033 HOST_WIDE_INT pos;
9034 unsigned HOST_WIDE_INT len;
9035 rtx other;
9036 enum machine_mode mode;
9038 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9039 a clear of a one-bit field. We will have changed it to
9040 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9041 for a SUBREG. */
9043 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9044 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9045 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9046 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9048 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9049 1, 1, 1, 0);
9050 if (assign != 0)
9051 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9052 return x;
9055 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9056 && subreg_lowpart_p (XEXP (src, 0))
9057 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9058 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9059 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9060 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9061 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9062 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9064 assign = make_extraction (VOIDmode, dest, 0,
9065 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9066 1, 1, 1, 0);
9067 if (assign != 0)
9068 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9069 return x;
9072 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9073 one-bit field. */
9074 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9075 && XEXP (XEXP (src, 0), 0) == const1_rtx
9076 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9078 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9079 1, 1, 1, 0);
9080 if (assign != 0)
9081 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9082 return x;
9085 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9086 SRC is an AND with all bits of that field set, then we can discard
9087 the AND. */
9088 if (GET_CODE (dest) == ZERO_EXTRACT
9089 && CONST_INT_P (XEXP (dest, 1))
9090 && GET_CODE (src) == AND
9091 && CONST_INT_P (XEXP (src, 1)))
9093 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9094 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9095 unsigned HOST_WIDE_INT ze_mask;
9097 if (width >= HOST_BITS_PER_WIDE_INT)
9098 ze_mask = -1;
9099 else
9100 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9102 /* Complete overlap. We can remove the source AND. */
9103 if ((and_mask & ze_mask) == ze_mask)
9104 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9106 /* Partial overlap. We can reduce the source AND. */
9107 if ((and_mask & ze_mask) != and_mask)
9109 mode = GET_MODE (src);
9110 src = gen_rtx_AND (mode, XEXP (src, 0),
9111 gen_int_mode (and_mask & ze_mask, mode));
9112 return gen_rtx_SET (VOIDmode, dest, src);
9116 /* The other case we handle is assignments into a constant-position
9117 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9118 a mask that has all one bits except for a group of zero bits and
9119 OTHER is known to have zeros where C1 has ones, this is such an
9120 assignment. Compute the position and length from C1. Shift OTHER
9121 to the appropriate position, force it to the required mode, and
9122 make the extraction. Check for the AND in both operands. */
9124 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9125 return x;
9127 rhs = expand_compound_operation (XEXP (src, 0));
9128 lhs = expand_compound_operation (XEXP (src, 1));
9130 if (GET_CODE (rhs) == AND
9131 && CONST_INT_P (XEXP (rhs, 1))
9132 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9133 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9134 else if (GET_CODE (lhs) == AND
9135 && CONST_INT_P (XEXP (lhs, 1))
9136 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9137 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9138 else
9139 return x;
9141 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9142 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
9143 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9144 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9145 return x;
9147 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9148 if (assign == 0)
9149 return x;
9151 /* The mode to use for the source is the mode of the assignment, or of
9152 what is inside a possible STRICT_LOW_PART. */
9153 mode = (GET_CODE (assign) == STRICT_LOW_PART
9154 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9156 /* Shift OTHER right POS places and make it the source, restricting it
9157 to the proper length and mode. */
9159 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9160 GET_MODE (src),
9161 other, pos),
9162 dest);
9163 src = force_to_mode (src, mode,
9164 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
9165 ? ~(unsigned HOST_WIDE_INT) 0
9166 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9169 /* If SRC is masked by an AND that does not make a difference in
9170 the value being stored, strip it. */
9171 if (GET_CODE (assign) == ZERO_EXTRACT
9172 && CONST_INT_P (XEXP (assign, 1))
9173 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9174 && GET_CODE (src) == AND
9175 && CONST_INT_P (XEXP (src, 1))
9176 && UINTVAL (XEXP (src, 1))
9177 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9178 src = XEXP (src, 0);
9180 return gen_rtx_SET (VOIDmode, assign, src);
9183 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9184 if so. */
9186 static rtx
9187 apply_distributive_law (rtx x)
9189 enum rtx_code code = GET_CODE (x);
9190 enum rtx_code inner_code;
9191 rtx lhs, rhs, other;
9192 rtx tem;
9194 /* Distributivity is not true for floating point as it can change the
9195 value. So we don't do it unless -funsafe-math-optimizations. */
9196 if (FLOAT_MODE_P (GET_MODE (x))
9197 && ! flag_unsafe_math_optimizations)
9198 return x;
9200 /* The outer operation can only be one of the following: */
9201 if (code != IOR && code != AND && code != XOR
9202 && code != PLUS && code != MINUS)
9203 return x;
9205 lhs = XEXP (x, 0);
9206 rhs = XEXP (x, 1);
9208 /* If either operand is a primitive we can't do anything, so get out
9209 fast. */
9210 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9211 return x;
9213 lhs = expand_compound_operation (lhs);
9214 rhs = expand_compound_operation (rhs);
9215 inner_code = GET_CODE (lhs);
9216 if (inner_code != GET_CODE (rhs))
9217 return x;
9219 /* See if the inner and outer operations distribute. */
9220 switch (inner_code)
9222 case LSHIFTRT:
9223 case ASHIFTRT:
9224 case AND:
9225 case IOR:
9226 /* These all distribute except over PLUS. */
9227 if (code == PLUS || code == MINUS)
9228 return x;
9229 break;
9231 case MULT:
9232 if (code != PLUS && code != MINUS)
9233 return x;
9234 break;
9236 case ASHIFT:
9237 /* This is also a multiply, so it distributes over everything. */
9238 break;
9240 case SUBREG:
9241 /* Non-paradoxical SUBREGs distributes over all operations,
9242 provided the inner modes and byte offsets are the same, this
9243 is an extraction of a low-order part, we don't convert an fp
9244 operation to int or vice versa, this is not a vector mode,
9245 and we would not be converting a single-word operation into a
9246 multi-word operation. The latter test is not required, but
9247 it prevents generating unneeded multi-word operations. Some
9248 of the previous tests are redundant given the latter test,
9249 but are retained because they are required for correctness.
9251 We produce the result slightly differently in this case. */
9253 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
9254 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
9255 || ! subreg_lowpart_p (lhs)
9256 || (GET_MODE_CLASS (GET_MODE (lhs))
9257 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
9258 || (GET_MODE_SIZE (GET_MODE (lhs))
9259 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
9260 || VECTOR_MODE_P (GET_MODE (lhs))
9261 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
9262 /* Result might need to be truncated. Don't change mode if
9263 explicit truncation is needed. */
9264 || !TRULY_NOOP_TRUNCATION
9265 (GET_MODE_BITSIZE (GET_MODE (x)),
9266 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
9267 return x;
9269 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
9270 SUBREG_REG (lhs), SUBREG_REG (rhs));
9271 return gen_lowpart (GET_MODE (x), tem);
9273 default:
9274 return x;
9277 /* Set LHS and RHS to the inner operands (A and B in the example
9278 above) and set OTHER to the common operand (C in the example).
9279 There is only one way to do this unless the inner operation is
9280 commutative. */
9281 if (COMMUTATIVE_ARITH_P (lhs)
9282 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9283 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9284 else if (COMMUTATIVE_ARITH_P (lhs)
9285 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9286 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9287 else if (COMMUTATIVE_ARITH_P (lhs)
9288 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9289 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9290 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9291 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9292 else
9293 return x;
9295 /* Form the new inner operation, seeing if it simplifies first. */
9296 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9298 /* There is one exception to the general way of distributing:
9299 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9300 if (code == XOR && inner_code == IOR)
9302 inner_code = AND;
9303 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9306 /* We may be able to continuing distributing the result, so call
9307 ourselves recursively on the inner operation before forming the
9308 outer operation, which we return. */
9309 return simplify_gen_binary (inner_code, GET_MODE (x),
9310 apply_distributive_law (tem), other);
9313 /* See if X is of the form (* (+ A B) C), and if so convert to
9314 (+ (* A C) (* B C)) and try to simplify.
9316 Most of the time, this results in no change. However, if some of
9317 the operands are the same or inverses of each other, simplifications
9318 will result.
9320 For example, (and (ior A B) (not B)) can occur as the result of
9321 expanding a bit field assignment. When we apply the distributive
9322 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9323 which then simplifies to (and (A (not B))).
9325 Note that no checks happen on the validity of applying the inverse
9326 distributive law. This is pointless since we can do it in the
9327 few places where this routine is called.
9329 N is the index of the term that is decomposed (the arithmetic operation,
9330 i.e. (+ A B) in the first example above). !N is the index of the term that
9331 is distributed, i.e. of C in the first example above. */
9332 static rtx
9333 distribute_and_simplify_rtx (rtx x, int n)
9335 enum machine_mode mode;
9336 enum rtx_code outer_code, inner_code;
9337 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9339 /* Distributivity is not true for floating point as it can change the
9340 value. So we don't do it unless -funsafe-math-optimizations. */
9341 if (FLOAT_MODE_P (GET_MODE (x))
9342 && ! flag_unsafe_math_optimizations)
9343 return NULL_RTX;
9345 decomposed = XEXP (x, n);
9346 if (!ARITHMETIC_P (decomposed))
9347 return NULL_RTX;
9349 mode = GET_MODE (x);
9350 outer_code = GET_CODE (x);
9351 distributed = XEXP (x, !n);
9353 inner_code = GET_CODE (decomposed);
9354 inner_op0 = XEXP (decomposed, 0);
9355 inner_op1 = XEXP (decomposed, 1);
9357 /* Special case (and (xor B C) (not A)), which is equivalent to
9358 (xor (ior A B) (ior A C)) */
9359 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9361 distributed = XEXP (distributed, 0);
9362 outer_code = IOR;
9365 if (n == 0)
9367 /* Distribute the second term. */
9368 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9369 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9371 else
9373 /* Distribute the first term. */
9374 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9375 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9378 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9379 new_op0, new_op1));
9380 if (GET_CODE (tmp) != outer_code
9381 && rtx_cost (tmp, SET, optimize_this_for_speed_p)
9382 < rtx_cost (x, SET, optimize_this_for_speed_p))
9383 return tmp;
9385 return NULL_RTX;
9388 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9389 in MODE. Return an equivalent form, if different from (and VAROP
9390 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9392 static rtx
9393 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9394 unsigned HOST_WIDE_INT constop)
9396 unsigned HOST_WIDE_INT nonzero;
9397 unsigned HOST_WIDE_INT orig_constop;
9398 rtx orig_varop;
9399 int i;
9401 orig_varop = varop;
9402 orig_constop = constop;
9403 if (GET_CODE (varop) == CLOBBER)
9404 return NULL_RTX;
9406 /* Simplify VAROP knowing that we will be only looking at some of the
9407 bits in it.
9409 Note by passing in CONSTOP, we guarantee that the bits not set in
9410 CONSTOP are not significant and will never be examined. We must
9411 ensure that is the case by explicitly masking out those bits
9412 before returning. */
9413 varop = force_to_mode (varop, mode, constop, 0);
9415 /* If VAROP is a CLOBBER, we will fail so return it. */
9416 if (GET_CODE (varop) == CLOBBER)
9417 return varop;
9419 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9420 to VAROP and return the new constant. */
9421 if (CONST_INT_P (varop))
9422 return gen_int_mode (INTVAL (varop) & constop, mode);
9424 /* See what bits may be nonzero in VAROP. Unlike the general case of
9425 a call to nonzero_bits, here we don't care about bits outside
9426 MODE. */
9428 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9430 /* Turn off all bits in the constant that are known to already be zero.
9431 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9432 which is tested below. */
9434 constop &= nonzero;
9436 /* If we don't have any bits left, return zero. */
9437 if (constop == 0)
9438 return const0_rtx;
9440 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9441 a power of two, we can replace this with an ASHIFT. */
9442 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9443 && (i = exact_log2 (constop)) >= 0)
9444 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9446 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9447 or XOR, then try to apply the distributive law. This may eliminate
9448 operations if either branch can be simplified because of the AND.
9449 It may also make some cases more complex, but those cases probably
9450 won't match a pattern either with or without this. */
9452 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9453 return
9454 gen_lowpart
9455 (mode,
9456 apply_distributive_law
9457 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9458 simplify_and_const_int (NULL_RTX,
9459 GET_MODE (varop),
9460 XEXP (varop, 0),
9461 constop),
9462 simplify_and_const_int (NULL_RTX,
9463 GET_MODE (varop),
9464 XEXP (varop, 1),
9465 constop))));
9467 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9468 the AND and see if one of the operands simplifies to zero. If so, we
9469 may eliminate it. */
9471 if (GET_CODE (varop) == PLUS
9472 && exact_log2 (constop + 1) >= 0)
9474 rtx o0, o1;
9476 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9477 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9478 if (o0 == const0_rtx)
9479 return o1;
9480 if (o1 == const0_rtx)
9481 return o0;
9484 /* Make a SUBREG if necessary. If we can't make it, fail. */
9485 varop = gen_lowpart (mode, varop);
9486 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9487 return NULL_RTX;
9489 /* If we are only masking insignificant bits, return VAROP. */
9490 if (constop == nonzero)
9491 return varop;
9493 if (varop == orig_varop && constop == orig_constop)
9494 return NULL_RTX;
9496 /* Otherwise, return an AND. */
9497 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9501 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9502 in MODE.
9504 Return an equivalent form, if different from X. Otherwise, return X. If
9505 X is zero, we are to always construct the equivalent form. */
9507 static rtx
9508 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9509 unsigned HOST_WIDE_INT constop)
9511 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9512 if (tem)
9513 return tem;
9515 if (!x)
9516 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9517 gen_int_mode (constop, mode));
9518 if (GET_MODE (x) != mode)
9519 x = gen_lowpart (mode, x);
9520 return x;
9523 /* Given a REG, X, compute which bits in X can be nonzero.
9524 We don't care about bits outside of those defined in MODE.
9526 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9527 a shift, AND, or zero_extract, we can do better. */
9529 static rtx
9530 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9531 const_rtx known_x ATTRIBUTE_UNUSED,
9532 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9533 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9534 unsigned HOST_WIDE_INT *nonzero)
9536 rtx tem;
9537 reg_stat_type *rsp;
9539 /* If X is a register whose nonzero bits value is current, use it.
9540 Otherwise, if X is a register whose value we can find, use that
9541 value. Otherwise, use the previously-computed global nonzero bits
9542 for this register. */
9544 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9545 if (rsp->last_set_value != 0
9546 && (rsp->last_set_mode == mode
9547 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9548 && GET_MODE_CLASS (mode) == MODE_INT))
9549 && ((rsp->last_set_label >= label_tick_ebb_start
9550 && rsp->last_set_label < label_tick)
9551 || (rsp->last_set_label == label_tick
9552 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9553 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9554 && REG_N_SETS (REGNO (x)) == 1
9555 && !REGNO_REG_SET_P
9556 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9558 *nonzero &= rsp->last_set_nonzero_bits;
9559 return NULL;
9562 tem = get_last_value (x);
9564 if (tem)
9566 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9567 /* If X is narrower than MODE and TEM is a non-negative
9568 constant that would appear negative in the mode of X,
9569 sign-extend it for use in reg_nonzero_bits because some
9570 machines (maybe most) will actually do the sign-extension
9571 and this is the conservative approach.
9573 ??? For 2.5, try to tighten up the MD files in this regard
9574 instead of this kludge. */
9576 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9577 && CONST_INT_P (tem)
9578 && INTVAL (tem) > 0
9579 && 0 != (UINTVAL (tem)
9580 & ((unsigned HOST_WIDE_INT) 1
9581 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9582 tem = GEN_INT (UINTVAL (tem)
9583 | ((unsigned HOST_WIDE_INT) (-1)
9584 << GET_MODE_BITSIZE (GET_MODE (x))));
9585 #endif
9586 return tem;
9588 else if (nonzero_sign_valid && rsp->nonzero_bits)
9590 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9592 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9593 /* We don't know anything about the upper bits. */
9594 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9595 *nonzero &= mask;
9598 return NULL;
9601 /* Return the number of bits at the high-order end of X that are known to
9602 be equal to the sign bit. X will be used in mode MODE; if MODE is
9603 VOIDmode, X will be used in its own mode. The returned value will always
9604 be between 1 and the number of bits in MODE. */
9606 static rtx
9607 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9608 const_rtx known_x ATTRIBUTE_UNUSED,
9609 enum machine_mode known_mode
9610 ATTRIBUTE_UNUSED,
9611 unsigned int known_ret ATTRIBUTE_UNUSED,
9612 unsigned int *result)
9614 rtx tem;
9615 reg_stat_type *rsp;
9617 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9618 if (rsp->last_set_value != 0
9619 && rsp->last_set_mode == mode
9620 && ((rsp->last_set_label >= label_tick_ebb_start
9621 && rsp->last_set_label < label_tick)
9622 || (rsp->last_set_label == label_tick
9623 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9624 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9625 && REG_N_SETS (REGNO (x)) == 1
9626 && !REGNO_REG_SET_P
9627 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9629 *result = rsp->last_set_sign_bit_copies;
9630 return NULL;
9633 tem = get_last_value (x);
9634 if (tem != 0)
9635 return tem;
9637 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9638 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9639 *result = rsp->sign_bit_copies;
9641 return NULL;
9644 /* Return the number of "extended" bits there are in X, when interpreted
9645 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9646 unsigned quantities, this is the number of high-order zero bits.
9647 For signed quantities, this is the number of copies of the sign bit
9648 minus 1. In both case, this function returns the number of "spare"
9649 bits. For example, if two quantities for which this function returns
9650 at least 1 are added, the addition is known not to overflow.
9652 This function will always return 0 unless called during combine, which
9653 implies that it must be called from a define_split. */
9655 unsigned int
9656 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9658 if (nonzero_sign_valid == 0)
9659 return 0;
9661 return (unsignedp
9662 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9663 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9664 - floor_log2 (nonzero_bits (x, mode)))
9665 : 0)
9666 : num_sign_bit_copies (x, mode) - 1);
9669 /* This function is called from `simplify_shift_const' to merge two
9670 outer operations. Specifically, we have already found that we need
9671 to perform operation *POP0 with constant *PCONST0 at the outermost
9672 position. We would now like to also perform OP1 with constant CONST1
9673 (with *POP0 being done last).
9675 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9676 the resulting operation. *PCOMP_P is set to 1 if we would need to
9677 complement the innermost operand, otherwise it is unchanged.
9679 MODE is the mode in which the operation will be done. No bits outside
9680 the width of this mode matter. It is assumed that the width of this mode
9681 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9683 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9684 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9685 result is simply *PCONST0.
9687 If the resulting operation cannot be expressed as one operation, we
9688 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9690 static int
9691 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)
9693 enum rtx_code op0 = *pop0;
9694 HOST_WIDE_INT const0 = *pconst0;
9696 const0 &= GET_MODE_MASK (mode);
9697 const1 &= GET_MODE_MASK (mode);
9699 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9700 if (op0 == AND)
9701 const1 &= const0;
9703 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9704 if OP0 is SET. */
9706 if (op1 == UNKNOWN || op0 == SET)
9707 return 1;
9709 else if (op0 == UNKNOWN)
9710 op0 = op1, const0 = const1;
9712 else if (op0 == op1)
9714 switch (op0)
9716 case AND:
9717 const0 &= const1;
9718 break;
9719 case IOR:
9720 const0 |= const1;
9721 break;
9722 case XOR:
9723 const0 ^= const1;
9724 break;
9725 case PLUS:
9726 const0 += const1;
9727 break;
9728 case NEG:
9729 op0 = UNKNOWN;
9730 break;
9731 default:
9732 break;
9736 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9737 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9738 return 0;
9740 /* If the two constants aren't the same, we can't do anything. The
9741 remaining six cases can all be done. */
9742 else if (const0 != const1)
9743 return 0;
9745 else
9746 switch (op0)
9748 case IOR:
9749 if (op1 == AND)
9750 /* (a & b) | b == b */
9751 op0 = SET;
9752 else /* op1 == XOR */
9753 /* (a ^ b) | b == a | b */
9755 break;
9757 case XOR:
9758 if (op1 == AND)
9759 /* (a & b) ^ b == (~a) & b */
9760 op0 = AND, *pcomp_p = 1;
9761 else /* op1 == IOR */
9762 /* (a | b) ^ b == a & ~b */
9763 op0 = AND, const0 = ~const0;
9764 break;
9766 case AND:
9767 if (op1 == IOR)
9768 /* (a | b) & b == b */
9769 op0 = SET;
9770 else /* op1 == XOR */
9771 /* (a ^ b) & b) == (~a) & b */
9772 *pcomp_p = 1;
9773 break;
9774 default:
9775 break;
9778 /* Check for NO-OP cases. */
9779 const0 &= GET_MODE_MASK (mode);
9780 if (const0 == 0
9781 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9782 op0 = UNKNOWN;
9783 else if (const0 == 0 && op0 == AND)
9784 op0 = SET;
9785 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9786 && op0 == AND)
9787 op0 = UNKNOWN;
9789 *pop0 = op0;
9791 /* ??? Slightly redundant with the above mask, but not entirely.
9792 Moving this above means we'd have to sign-extend the mode mask
9793 for the final test. */
9794 if (op0 != UNKNOWN && op0 != NEG)
9795 *pconst0 = trunc_int_for_mode (const0, mode);
9797 return 1;
9800 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9801 the shift in. The original shift operation CODE is performed on OP in
9802 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9803 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9804 result of the shift is subject to operation OUTER_CODE with operand
9805 OUTER_CONST. */
9807 static enum machine_mode
9808 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9809 enum machine_mode orig_mode, enum machine_mode mode,
9810 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9812 if (orig_mode == mode)
9813 return mode;
9814 gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9816 /* In general we can't perform in wider mode for right shift and rotate. */
9817 switch (code)
9819 case ASHIFTRT:
9820 /* We can still widen if the bits brought in from the left are identical
9821 to the sign bit of ORIG_MODE. */
9822 if (num_sign_bit_copies (op, mode)
9823 > (unsigned) (GET_MODE_BITSIZE (mode)
9824 - GET_MODE_BITSIZE (orig_mode)))
9825 return mode;
9826 return orig_mode;
9828 case LSHIFTRT:
9829 /* Similarly here but with zero bits. */
9830 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9831 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9832 return mode;
9834 /* We can also widen if the bits brought in will be masked off. This
9835 operation is performed in ORIG_MODE. */
9836 if (outer_code == AND)
9838 int care_bits = low_bitmask_len (orig_mode, outer_const);
9840 if (care_bits >= 0
9841 && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9842 return mode;
9844 /* fall through */
9846 case ROTATE:
9847 return orig_mode;
9849 case ROTATERT:
9850 gcc_unreachable ();
9852 default:
9853 return mode;
9857 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9858 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
9859 simplify it. Otherwise, return a simplified value.
9861 The shift is normally computed in the widest mode we find in VAROP, as
9862 long as it isn't a different number of words than RESULT_MODE. Exceptions
9863 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9865 static rtx
9866 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9867 rtx varop, int orig_count)
9869 enum rtx_code orig_code = code;
9870 rtx orig_varop = varop;
9871 int count;
9872 enum machine_mode mode = result_mode;
9873 enum machine_mode shift_mode, tmode;
9874 unsigned int mode_words
9875 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9876 /* We form (outer_op (code varop count) (outer_const)). */
9877 enum rtx_code outer_op = UNKNOWN;
9878 HOST_WIDE_INT outer_const = 0;
9879 int complement_p = 0;
9880 rtx new_rtx, x;
9882 /* Make sure and truncate the "natural" shift on the way in. We don't
9883 want to do this inside the loop as it makes it more difficult to
9884 combine shifts. */
9885 if (SHIFT_COUNT_TRUNCATED)
9886 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9888 /* If we were given an invalid count, don't do anything except exactly
9889 what was requested. */
9891 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9892 return NULL_RTX;
9894 count = orig_count;
9896 /* Unless one of the branches of the `if' in this loop does a `continue',
9897 we will `break' the loop after the `if'. */
9899 while (count != 0)
9901 /* If we have an operand of (clobber (const_int 0)), fail. */
9902 if (GET_CODE (varop) == CLOBBER)
9903 return NULL_RTX;
9905 /* Convert ROTATERT to ROTATE. */
9906 if (code == ROTATERT)
9908 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9909 code = ROTATE;
9910 if (VECTOR_MODE_P (result_mode))
9911 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9912 else
9913 count = bitsize - count;
9916 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9917 mode, outer_op, outer_const);
9919 /* Handle cases where the count is greater than the size of the mode
9920 minus 1. For ASHIFT, use the size minus one as the count (this can
9921 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9922 take the count modulo the size. For other shifts, the result is
9923 zero.
9925 Since these shifts are being produced by the compiler by combining
9926 multiple operations, each of which are defined, we know what the
9927 result is supposed to be. */
9929 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9931 if (code == ASHIFTRT)
9932 count = GET_MODE_BITSIZE (shift_mode) - 1;
9933 else if (code == ROTATE || code == ROTATERT)
9934 count %= GET_MODE_BITSIZE (shift_mode);
9935 else
9937 /* We can't simply return zero because there may be an
9938 outer op. */
9939 varop = const0_rtx;
9940 count = 0;
9941 break;
9945 /* If we discovered we had to complement VAROP, leave. Making a NOT
9946 here would cause an infinite loop. */
9947 if (complement_p)
9948 break;
9950 /* An arithmetic right shift of a quantity known to be -1 or 0
9951 is a no-op. */
9952 if (code == ASHIFTRT
9953 && (num_sign_bit_copies (varop, shift_mode)
9954 == GET_MODE_BITSIZE (shift_mode)))
9956 count = 0;
9957 break;
9960 /* If we are doing an arithmetic right shift and discarding all but
9961 the sign bit copies, this is equivalent to doing a shift by the
9962 bitsize minus one. Convert it into that shift because it will often
9963 allow other simplifications. */
9965 if (code == ASHIFTRT
9966 && (count + num_sign_bit_copies (varop, shift_mode)
9967 >= GET_MODE_BITSIZE (shift_mode)))
9968 count = GET_MODE_BITSIZE (shift_mode) - 1;
9970 /* We simplify the tests below and elsewhere by converting
9971 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9972 `make_compound_operation' will convert it to an ASHIFTRT for
9973 those machines (such as VAX) that don't have an LSHIFTRT. */
9974 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9975 && code == ASHIFTRT
9976 && ((nonzero_bits (varop, shift_mode)
9977 & ((unsigned HOST_WIDE_INT) 1
9978 << (GET_MODE_BITSIZE (shift_mode) - 1))) == 0))
9979 code = LSHIFTRT;
9981 if (((code == LSHIFTRT
9982 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9983 && !(nonzero_bits (varop, shift_mode) >> count))
9984 || (code == ASHIFT
9985 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9986 && !((nonzero_bits (varop, shift_mode) << count)
9987 & GET_MODE_MASK (shift_mode))))
9988 && !side_effects_p (varop))
9989 varop = const0_rtx;
9991 switch (GET_CODE (varop))
9993 case SIGN_EXTEND:
9994 case ZERO_EXTEND:
9995 case SIGN_EXTRACT:
9996 case ZERO_EXTRACT:
9997 new_rtx = expand_compound_operation (varop);
9998 if (new_rtx != varop)
10000 varop = new_rtx;
10001 continue;
10003 break;
10005 case MEM:
10006 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10007 minus the width of a smaller mode, we can do this with a
10008 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10009 if ((code == ASHIFTRT || code == LSHIFTRT)
10010 && ! mode_dependent_address_p (XEXP (varop, 0))
10011 && ! MEM_VOLATILE_P (varop)
10012 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10013 MODE_INT, 1)) != BLKmode)
10015 new_rtx = adjust_address_nv (varop, tmode,
10016 BYTES_BIG_ENDIAN ? 0
10017 : count / BITS_PER_UNIT);
10019 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10020 : ZERO_EXTEND, mode, new_rtx);
10021 count = 0;
10022 continue;
10024 break;
10026 case SUBREG:
10027 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10028 the same number of words as what we've seen so far. Then store
10029 the widest mode in MODE. */
10030 if (subreg_lowpart_p (varop)
10031 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10032 > GET_MODE_SIZE (GET_MODE (varop)))
10033 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10034 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10035 == mode_words
10036 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10037 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10039 varop = SUBREG_REG (varop);
10040 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10041 mode = GET_MODE (varop);
10042 continue;
10044 break;
10046 case MULT:
10047 /* Some machines use MULT instead of ASHIFT because MULT
10048 is cheaper. But it is still better on those machines to
10049 merge two shifts into one. */
10050 if (CONST_INT_P (XEXP (varop, 1))
10051 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10053 varop
10054 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10055 XEXP (varop, 0),
10056 GEN_INT (exact_log2 (
10057 UINTVAL (XEXP (varop, 1)))));
10058 continue;
10060 break;
10062 case UDIV:
10063 /* Similar, for when divides are cheaper. */
10064 if (CONST_INT_P (XEXP (varop, 1))
10065 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10067 varop
10068 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10069 XEXP (varop, 0),
10070 GEN_INT (exact_log2 (
10071 UINTVAL (XEXP (varop, 1)))));
10072 continue;
10074 break;
10076 case ASHIFTRT:
10077 /* If we are extracting just the sign bit of an arithmetic
10078 right shift, that shift is not needed. However, the sign
10079 bit of a wider mode may be different from what would be
10080 interpreted as the sign bit in a narrower mode, so, if
10081 the result is narrower, don't discard the shift. */
10082 if (code == LSHIFTRT
10083 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10084 && (GET_MODE_BITSIZE (result_mode)
10085 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10087 varop = XEXP (varop, 0);
10088 continue;
10091 /* ... fall through ... */
10093 case LSHIFTRT:
10094 case ASHIFT:
10095 case ROTATE:
10096 /* Here we have two nested shifts. The result is usually the
10097 AND of a new shift with a mask. We compute the result below. */
10098 if (CONST_INT_P (XEXP (varop, 1))
10099 && INTVAL (XEXP (varop, 1)) >= 0
10100 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
10101 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10102 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10103 && !VECTOR_MODE_P (result_mode))
10105 enum rtx_code first_code = GET_CODE (varop);
10106 unsigned int first_count = INTVAL (XEXP (varop, 1));
10107 unsigned HOST_WIDE_INT mask;
10108 rtx mask_rtx;
10110 /* We have one common special case. We can't do any merging if
10111 the inner code is an ASHIFTRT of a smaller mode. However, if
10112 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10113 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10114 we can convert it to
10115 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
10116 This simplifies certain SIGN_EXTEND operations. */
10117 if (code == ASHIFT && first_code == ASHIFTRT
10118 && count == (GET_MODE_BITSIZE (result_mode)
10119 - GET_MODE_BITSIZE (GET_MODE (varop))))
10121 /* C3 has the low-order C1 bits zero. */
10123 mask = GET_MODE_MASK (mode)
10124 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10126 varop = simplify_and_const_int (NULL_RTX, result_mode,
10127 XEXP (varop, 0), mask);
10128 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10129 varop, count);
10130 count = first_count;
10131 code = ASHIFTRT;
10132 continue;
10135 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10136 than C1 high-order bits equal to the sign bit, we can convert
10137 this to either an ASHIFT or an ASHIFTRT depending on the
10138 two counts.
10140 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10142 if (code == ASHIFTRT && first_code == ASHIFT
10143 && GET_MODE (varop) == shift_mode
10144 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10145 > first_count))
10147 varop = XEXP (varop, 0);
10148 count -= first_count;
10149 if (count < 0)
10151 count = -count;
10152 code = ASHIFT;
10155 continue;
10158 /* There are some cases we can't do. If CODE is ASHIFTRT,
10159 we can only do this if FIRST_CODE is also ASHIFTRT.
10161 We can't do the case when CODE is ROTATE and FIRST_CODE is
10162 ASHIFTRT.
10164 If the mode of this shift is not the mode of the outer shift,
10165 we can't do this if either shift is a right shift or ROTATE.
10167 Finally, we can't do any of these if the mode is too wide
10168 unless the codes are the same.
10170 Handle the case where the shift codes are the same
10171 first. */
10173 if (code == first_code)
10175 if (GET_MODE (varop) != result_mode
10176 && (code == ASHIFTRT || code == LSHIFTRT
10177 || code == ROTATE))
10178 break;
10180 count += first_count;
10181 varop = XEXP (varop, 0);
10182 continue;
10185 if (code == ASHIFTRT
10186 || (code == ROTATE && first_code == ASHIFTRT)
10187 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
10188 || (GET_MODE (varop) != result_mode
10189 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10190 || first_code == ROTATE
10191 || code == ROTATE)))
10192 break;
10194 /* To compute the mask to apply after the shift, shift the
10195 nonzero bits of the inner shift the same way the
10196 outer shift will. */
10198 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10200 mask_rtx
10201 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10202 GEN_INT (count));
10204 /* Give up if we can't compute an outer operation to use. */
10205 if (mask_rtx == 0
10206 || !CONST_INT_P (mask_rtx)
10207 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10208 INTVAL (mask_rtx),
10209 result_mode, &complement_p))
10210 break;
10212 /* If the shifts are in the same direction, we add the
10213 counts. Otherwise, we subtract them. */
10214 if ((code == ASHIFTRT || code == LSHIFTRT)
10215 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10216 count += first_count;
10217 else
10218 count -= first_count;
10220 /* If COUNT is positive, the new shift is usually CODE,
10221 except for the two exceptions below, in which case it is
10222 FIRST_CODE. If the count is negative, FIRST_CODE should
10223 always be used */
10224 if (count > 0
10225 && ((first_code == ROTATE && code == ASHIFT)
10226 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10227 code = first_code;
10228 else if (count < 0)
10229 code = first_code, count = -count;
10231 varop = XEXP (varop, 0);
10232 continue;
10235 /* If we have (A << B << C) for any shift, we can convert this to
10236 (A << C << B). This wins if A is a constant. Only try this if
10237 B is not a constant. */
10239 else if (GET_CODE (varop) == code
10240 && CONST_INT_P (XEXP (varop, 0))
10241 && !CONST_INT_P (XEXP (varop, 1)))
10243 rtx new_rtx = simplify_const_binary_operation (code, mode,
10244 XEXP (varop, 0),
10245 GEN_INT (count));
10246 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10247 count = 0;
10248 continue;
10250 break;
10252 case NOT:
10253 if (VECTOR_MODE_P (mode))
10254 break;
10256 /* Make this fit the case below. */
10257 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
10258 GEN_INT (GET_MODE_MASK (mode)));
10259 continue;
10261 case IOR:
10262 case AND:
10263 case XOR:
10264 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10265 with C the size of VAROP - 1 and the shift is logical if
10266 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10267 we have an (le X 0) operation. If we have an arithmetic shift
10268 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10269 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10271 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10272 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10273 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10274 && (code == LSHIFTRT || code == ASHIFTRT)
10275 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10276 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10278 count = 0;
10279 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10280 const0_rtx);
10282 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10283 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10285 continue;
10288 /* If we have (shift (logical)), move the logical to the outside
10289 to allow it to possibly combine with another logical and the
10290 shift to combine with another shift. This also canonicalizes to
10291 what a ZERO_EXTRACT looks like. Also, some machines have
10292 (and (shift)) insns. */
10294 if (CONST_INT_P (XEXP (varop, 1))
10295 /* We can't do this if we have (ashiftrt (xor)) and the
10296 constant has its sign bit set in shift_mode. */
10297 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10298 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10299 shift_mode))
10300 && (new_rtx = simplify_const_binary_operation (code, 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, GET_CODE (varop),
10305 INTVAL (new_rtx), result_mode, &complement_p))
10307 varop = XEXP (varop, 0);
10308 continue;
10311 /* If we can't do that, try to simplify the shift in each arm of the
10312 logical expression, make a new logical expression, and apply
10313 the inverse distributive law. This also can't be done
10314 for some (ashiftrt (xor)). */
10315 if (CONST_INT_P (XEXP (varop, 1))
10316 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10317 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10318 shift_mode)))
10320 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10321 XEXP (varop, 0), count);
10322 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10323 XEXP (varop, 1), count);
10325 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10326 lhs, rhs);
10327 varop = apply_distributive_law (varop);
10329 count = 0;
10330 continue;
10332 break;
10334 case EQ:
10335 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10336 says that the sign bit can be tested, FOO has mode MODE, C is
10337 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
10338 that may be nonzero. */
10339 if (code == LSHIFTRT
10340 && XEXP (varop, 1) == const0_rtx
10341 && GET_MODE (XEXP (varop, 0)) == result_mode
10342 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10343 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10344 && STORE_FLAG_VALUE == -1
10345 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10346 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10347 &complement_p))
10349 varop = XEXP (varop, 0);
10350 count = 0;
10351 continue;
10353 break;
10355 case NEG:
10356 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10357 than the number of bits in the mode is equivalent to A. */
10358 if (code == LSHIFTRT
10359 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10360 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10362 varop = XEXP (varop, 0);
10363 count = 0;
10364 continue;
10367 /* NEG commutes with ASHIFT since it is multiplication. Move the
10368 NEG outside to allow shifts to combine. */
10369 if (code == ASHIFT
10370 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10371 &complement_p))
10373 varop = XEXP (varop, 0);
10374 continue;
10376 break;
10378 case PLUS:
10379 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10380 is one less than the number of bits in the mode is
10381 equivalent to (xor A 1). */
10382 if (code == LSHIFTRT
10383 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10384 && XEXP (varop, 1) == constm1_rtx
10385 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10386 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10387 &complement_p))
10389 count = 0;
10390 varop = XEXP (varop, 0);
10391 continue;
10394 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10395 that might be nonzero in BAR are those being shifted out and those
10396 bits are known zero in FOO, we can replace the PLUS with FOO.
10397 Similarly in the other operand order. This code occurs when
10398 we are computing the size of a variable-size array. */
10400 if ((code == ASHIFTRT || code == LSHIFTRT)
10401 && count < HOST_BITS_PER_WIDE_INT
10402 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10403 && (nonzero_bits (XEXP (varop, 1), result_mode)
10404 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10406 varop = XEXP (varop, 0);
10407 continue;
10409 else if ((code == ASHIFTRT || code == LSHIFTRT)
10410 && count < HOST_BITS_PER_WIDE_INT
10411 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10412 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10413 >> count)
10414 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10415 & nonzero_bits (XEXP (varop, 1),
10416 result_mode)))
10418 varop = XEXP (varop, 1);
10419 continue;
10422 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10423 if (code == ASHIFT
10424 && CONST_INT_P (XEXP (varop, 1))
10425 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10426 XEXP (varop, 1),
10427 GEN_INT (count))) != 0
10428 && CONST_INT_P (new_rtx)
10429 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10430 INTVAL (new_rtx), result_mode, &complement_p))
10432 varop = XEXP (varop, 0);
10433 continue;
10436 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10437 signbit', and attempt to change the PLUS to an XOR and move it to
10438 the outer operation as is done above in the AND/IOR/XOR case
10439 leg for shift(logical). See details in logical handling above
10440 for reasoning in doing so. */
10441 if (code == LSHIFTRT
10442 && CONST_INT_P (XEXP (varop, 1))
10443 && mode_signbit_p (result_mode, XEXP (varop, 1))
10444 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10445 XEXP (varop, 1),
10446 GEN_INT (count))) != 0
10447 && CONST_INT_P (new_rtx)
10448 && merge_outer_ops (&outer_op, &outer_const, XOR,
10449 INTVAL (new_rtx), result_mode, &complement_p))
10451 varop = XEXP (varop, 0);
10452 continue;
10455 break;
10457 case MINUS:
10458 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10459 with C the size of VAROP - 1 and the shift is logical if
10460 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10461 we have a (gt X 0) operation. If the shift is arithmetic with
10462 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10463 we have a (neg (gt X 0)) operation. */
10465 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10466 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10467 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10468 && (code == LSHIFTRT || code == ASHIFTRT)
10469 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10470 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10471 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10473 count = 0;
10474 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10475 const0_rtx);
10477 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10478 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10480 continue;
10482 break;
10484 case TRUNCATE:
10485 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10486 if the truncate does not affect the value. */
10487 if (code == LSHIFTRT
10488 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10489 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10490 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10491 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
10492 - GET_MODE_BITSIZE (GET_MODE (varop)))))
10494 rtx varop_inner = XEXP (varop, 0);
10496 varop_inner
10497 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10498 XEXP (varop_inner, 0),
10499 GEN_INT
10500 (count + INTVAL (XEXP (varop_inner, 1))));
10501 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10502 count = 0;
10503 continue;
10505 break;
10507 default:
10508 break;
10511 break;
10514 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10515 outer_op, outer_const);
10517 /* We have now finished analyzing the shift. The result should be
10518 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10519 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10520 to the result of the shift. OUTER_CONST is the relevant constant,
10521 but we must turn off all bits turned off in the shift. */
10523 if (outer_op == UNKNOWN
10524 && orig_code == code && orig_count == count
10525 && varop == orig_varop
10526 && shift_mode == GET_MODE (varop))
10527 return NULL_RTX;
10529 /* Make a SUBREG if necessary. If we can't make it, fail. */
10530 varop = gen_lowpart (shift_mode, varop);
10531 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10532 return NULL_RTX;
10534 /* If we have an outer operation and we just made a shift, it is
10535 possible that we could have simplified the shift were it not
10536 for the outer operation. So try to do the simplification
10537 recursively. */
10539 if (outer_op != UNKNOWN)
10540 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10541 else
10542 x = NULL_RTX;
10544 if (x == NULL_RTX)
10545 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10547 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10548 turn off all the bits that the shift would have turned off. */
10549 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10550 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10551 GET_MODE_MASK (result_mode) >> orig_count);
10553 /* Do the remainder of the processing in RESULT_MODE. */
10554 x = gen_lowpart_or_truncate (result_mode, x);
10556 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10557 operation. */
10558 if (complement_p)
10559 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10561 if (outer_op != UNKNOWN)
10563 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10564 && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10565 outer_const = trunc_int_for_mode (outer_const, result_mode);
10567 if (outer_op == AND)
10568 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10569 else if (outer_op == SET)
10571 /* This means that we have determined that the result is
10572 equivalent to a constant. This should be rare. */
10573 if (!side_effects_p (x))
10574 x = GEN_INT (outer_const);
10576 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10577 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10578 else
10579 x = simplify_gen_binary (outer_op, result_mode, x,
10580 GEN_INT (outer_const));
10583 return x;
10586 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10587 The result of the shift is RESULT_MODE. If we cannot simplify it,
10588 return X or, if it is NULL, synthesize the expression with
10589 simplify_gen_binary. Otherwise, return a simplified value.
10591 The shift is normally computed in the widest mode we find in VAROP, as
10592 long as it isn't a different number of words than RESULT_MODE. Exceptions
10593 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10595 static rtx
10596 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10597 rtx varop, int count)
10599 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10600 if (tem)
10601 return tem;
10603 if (!x)
10604 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10605 if (GET_MODE (x) != result_mode)
10606 x = gen_lowpart (result_mode, x);
10607 return x;
10611 /* Like recog, but we receive the address of a pointer to a new pattern.
10612 We try to match the rtx that the pointer points to.
10613 If that fails, we may try to modify or replace the pattern,
10614 storing the replacement into the same pointer object.
10616 Modifications include deletion or addition of CLOBBERs.
10618 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10619 the CLOBBERs are placed.
10621 The value is the final insn code from the pattern ultimately matched,
10622 or -1. */
10624 static int
10625 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10627 rtx pat = *pnewpat;
10628 int insn_code_number;
10629 int num_clobbers_to_add = 0;
10630 int i;
10631 rtx notes = 0;
10632 rtx old_notes, old_pat;
10634 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10635 we use to indicate that something didn't match. If we find such a
10636 thing, force rejection. */
10637 if (GET_CODE (pat) == PARALLEL)
10638 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10639 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10640 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10641 return -1;
10643 old_pat = PATTERN (insn);
10644 old_notes = REG_NOTES (insn);
10645 PATTERN (insn) = pat;
10646 REG_NOTES (insn) = 0;
10648 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10649 if (dump_file && (dump_flags & TDF_DETAILS))
10651 if (insn_code_number < 0)
10652 fputs ("Failed to match this instruction:\n", dump_file);
10653 else
10654 fputs ("Successfully matched this instruction:\n", dump_file);
10655 print_rtl_single (dump_file, pat);
10658 /* If it isn't, there is the possibility that we previously had an insn
10659 that clobbered some register as a side effect, but the combined
10660 insn doesn't need to do that. So try once more without the clobbers
10661 unless this represents an ASM insn. */
10663 if (insn_code_number < 0 && ! check_asm_operands (pat)
10664 && GET_CODE (pat) == PARALLEL)
10666 int pos;
10668 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10669 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10671 if (i != pos)
10672 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10673 pos++;
10676 SUBST_INT (XVECLEN (pat, 0), pos);
10678 if (pos == 1)
10679 pat = XVECEXP (pat, 0, 0);
10681 PATTERN (insn) = pat;
10682 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10683 if (dump_file && (dump_flags & TDF_DETAILS))
10685 if (insn_code_number < 0)
10686 fputs ("Failed to match this instruction:\n", dump_file);
10687 else
10688 fputs ("Successfully matched this instruction:\n", dump_file);
10689 print_rtl_single (dump_file, pat);
10692 PATTERN (insn) = old_pat;
10693 REG_NOTES (insn) = old_notes;
10695 /* Recognize all noop sets, these will be killed by followup pass. */
10696 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10697 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10699 /* If we had any clobbers to add, make a new pattern than contains
10700 them. Then check to make sure that all of them are dead. */
10701 if (num_clobbers_to_add)
10703 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10704 rtvec_alloc (GET_CODE (pat) == PARALLEL
10705 ? (XVECLEN (pat, 0)
10706 + num_clobbers_to_add)
10707 : num_clobbers_to_add + 1));
10709 if (GET_CODE (pat) == PARALLEL)
10710 for (i = 0; i < XVECLEN (pat, 0); i++)
10711 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10712 else
10713 XVECEXP (newpat, 0, 0) = pat;
10715 add_clobbers (newpat, insn_code_number);
10717 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10718 i < XVECLEN (newpat, 0); i++)
10720 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10721 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10722 return -1;
10723 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10725 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10726 notes = alloc_reg_note (REG_UNUSED,
10727 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10730 pat = newpat;
10733 *pnewpat = pat;
10734 *pnotes = notes;
10736 return insn_code_number;
10739 /* Like gen_lowpart_general but for use by combine. In combine it
10740 is not possible to create any new pseudoregs. However, it is
10741 safe to create invalid memory addresses, because combine will
10742 try to recognize them and all they will do is make the combine
10743 attempt fail.
10745 If for some reason this cannot do its job, an rtx
10746 (clobber (const_int 0)) is returned.
10747 An insn containing that will not be recognized. */
10749 static rtx
10750 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10752 enum machine_mode imode = GET_MODE (x);
10753 unsigned int osize = GET_MODE_SIZE (omode);
10754 unsigned int isize = GET_MODE_SIZE (imode);
10755 rtx result;
10757 if (omode == imode)
10758 return x;
10760 /* Return identity if this is a CONST or symbolic reference. */
10761 if (omode == Pmode
10762 && (GET_CODE (x) == CONST
10763 || GET_CODE (x) == SYMBOL_REF
10764 || GET_CODE (x) == LABEL_REF))
10765 return x;
10767 /* We can only support MODE being wider than a word if X is a
10768 constant integer or has a mode the same size. */
10769 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10770 && ! ((imode == VOIDmode
10771 && (CONST_INT_P (x)
10772 || GET_CODE (x) == CONST_DOUBLE))
10773 || isize == osize))
10774 goto fail;
10776 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10777 won't know what to do. So we will strip off the SUBREG here and
10778 process normally. */
10779 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10781 x = SUBREG_REG (x);
10783 /* For use in case we fall down into the address adjustments
10784 further below, we need to adjust the known mode and size of
10785 x; imode and isize, since we just adjusted x. */
10786 imode = GET_MODE (x);
10788 if (imode == omode)
10789 return x;
10791 isize = GET_MODE_SIZE (imode);
10794 result = gen_lowpart_common (omode, x);
10796 if (result)
10797 return result;
10799 if (MEM_P (x))
10801 int offset = 0;
10803 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10804 address. */
10805 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10806 goto fail;
10808 /* If we want to refer to something bigger than the original memref,
10809 generate a paradoxical subreg instead. That will force a reload
10810 of the original memref X. */
10811 if (isize < osize)
10812 return gen_rtx_SUBREG (omode, x, 0);
10814 if (WORDS_BIG_ENDIAN)
10815 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10817 /* Adjust the address so that the address-after-the-data is
10818 unchanged. */
10819 if (BYTES_BIG_ENDIAN)
10820 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10822 return adjust_address_nv (x, omode, offset);
10825 /* If X is a comparison operator, rewrite it in a new mode. This
10826 probably won't match, but may allow further simplifications. */
10827 else if (COMPARISON_P (x))
10828 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10830 /* If we couldn't simplify X any other way, just enclose it in a
10831 SUBREG. Normally, this SUBREG won't match, but some patterns may
10832 include an explicit SUBREG or we may simplify it further in combine. */
10833 else
10835 int offset = 0;
10836 rtx res;
10838 offset = subreg_lowpart_offset (omode, imode);
10839 if (imode == VOIDmode)
10841 imode = int_mode_for_mode (omode);
10842 x = gen_lowpart_common (imode, x);
10843 if (x == NULL)
10844 goto fail;
10846 res = simplify_gen_subreg (omode, x, imode, offset);
10847 if (res)
10848 return res;
10851 fail:
10852 return gen_rtx_CLOBBER (omode, const0_rtx);
10855 /* Try to simplify a comparison between OP0 and a constant OP1,
10856 where CODE is the comparison code that will be tested, into a
10857 (CODE OP0 const0_rtx) form.
10859 The result is a possibly different comparison code to use.
10860 *POP1 may be updated. */
10862 static enum rtx_code
10863 simplify_compare_const (enum rtx_code code, rtx op0, rtx *pop1)
10865 enum machine_mode mode = GET_MODE (op0);
10866 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10867 HOST_WIDE_INT const_op = INTVAL (*pop1);
10869 /* Get the constant we are comparing against and turn off all bits
10870 not on in our mode. */
10871 if (mode != VOIDmode)
10872 const_op = trunc_int_for_mode (const_op, mode);
10874 /* If we are comparing against a constant power of two and the value
10875 being compared can only have that single bit nonzero (e.g., it was
10876 `and'ed with that bit), we can replace this with a comparison
10877 with zero. */
10878 if (const_op
10879 && (code == EQ || code == NE || code == GE || code == GEU
10880 || code == LT || code == LTU)
10881 && mode_width <= HOST_BITS_PER_WIDE_INT
10882 && exact_log2 (const_op) >= 0
10883 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10885 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10886 const_op = 0;
10889 /* Similarly, if we are comparing a value known to be either -1 or
10890 0 with -1, change it to the opposite comparison against zero. */
10891 if (const_op == -1
10892 && (code == EQ || code == NE || code == GT || code == LE
10893 || code == GEU || code == LTU)
10894 && num_sign_bit_copies (op0, mode) == mode_width)
10896 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10897 const_op = 0;
10900 /* Do some canonicalizations based on the comparison code. We prefer
10901 comparisons against zero and then prefer equality comparisons.
10902 If we can reduce the size of a constant, we will do that too. */
10903 switch (code)
10905 case LT:
10906 /* < C is equivalent to <= (C - 1) */
10907 if (const_op > 0)
10909 const_op -= 1;
10910 code = LE;
10911 /* ... fall through to LE case below. */
10913 else
10914 break;
10916 case LE:
10917 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10918 if (const_op < 0)
10920 const_op += 1;
10921 code = LT;
10924 /* If we are doing a <= 0 comparison on a value known to have
10925 a zero sign bit, we can replace this with == 0. */
10926 else if (const_op == 0
10927 && mode_width <= HOST_BITS_PER_WIDE_INT
10928 && (nonzero_bits (op0, mode)
10929 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10930 == 0)
10931 code = EQ;
10932 break;
10934 case GE:
10935 /* >= C is equivalent to > (C - 1). */
10936 if (const_op > 0)
10938 const_op -= 1;
10939 code = GT;
10940 /* ... fall through to GT below. */
10942 else
10943 break;
10945 case GT:
10946 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10947 if (const_op < 0)
10949 const_op += 1;
10950 code = GE;
10953 /* If we are doing a > 0 comparison on a value known to have
10954 a zero sign bit, we can replace this with != 0. */
10955 else if (const_op == 0
10956 && mode_width <= HOST_BITS_PER_WIDE_INT
10957 && (nonzero_bits (op0, mode)
10958 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10959 == 0)
10960 code = NE;
10961 break;
10963 case LTU:
10964 /* < C is equivalent to <= (C - 1). */
10965 if (const_op > 0)
10967 const_op -= 1;
10968 code = LEU;
10969 /* ... fall through ... */
10971 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10972 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10973 && (unsigned HOST_WIDE_INT) const_op
10974 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10976 const_op = 0;
10977 code = GE;
10978 break;
10980 else
10981 break;
10983 case LEU:
10984 /* unsigned <= 0 is equivalent to == 0 */
10985 if (const_op == 0)
10986 code = EQ;
10987 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10988 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10989 && (unsigned HOST_WIDE_INT) const_op
10990 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10992 const_op = 0;
10993 code = GE;
10995 break;
10997 case GEU:
10998 /* >= C is equivalent to > (C - 1). */
10999 if (const_op > 1)
11001 const_op -= 1;
11002 code = GTU;
11003 /* ... fall through ... */
11006 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11007 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11008 && (unsigned HOST_WIDE_INT) const_op
11009 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11011 const_op = 0;
11012 code = LT;
11013 break;
11015 else
11016 break;
11018 case GTU:
11019 /* unsigned > 0 is equivalent to != 0 */
11020 if (const_op == 0)
11021 code = NE;
11022 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11023 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11024 && (unsigned HOST_WIDE_INT) const_op
11025 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11027 const_op = 0;
11028 code = LT;
11030 break;
11032 default:
11033 break;
11036 *pop1 = GEN_INT (const_op);
11037 return code;
11040 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11041 comparison code that will be tested.
11043 The result is a possibly different comparison code to use. *POP0 and
11044 *POP1 may be updated.
11046 It is possible that we might detect that a comparison is either always
11047 true or always false. However, we do not perform general constant
11048 folding in combine, so this knowledge isn't useful. Such tautologies
11049 should have been detected earlier. Hence we ignore all such cases. */
11051 static enum rtx_code
11052 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11054 rtx op0 = *pop0;
11055 rtx op1 = *pop1;
11056 rtx tem, tem1;
11057 int i;
11058 enum machine_mode mode, tmode;
11060 /* Try a few ways of applying the same transformation to both operands. */
11061 while (1)
11063 #ifndef WORD_REGISTER_OPERATIONS
11064 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11065 so check specially. */
11066 if (code != GTU && code != GEU && code != LTU && code != LEU
11067 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11068 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11069 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11070 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11071 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11072 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11073 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11074 && CONST_INT_P (XEXP (op0, 1))
11075 && XEXP (op0, 1) == XEXP (op1, 1)
11076 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11077 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11078 && (INTVAL (XEXP (op0, 1))
11079 == (GET_MODE_BITSIZE (GET_MODE (op0))
11080 - (GET_MODE_BITSIZE
11081 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11083 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11084 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11086 #endif
11088 /* If both operands are the same constant shift, see if we can ignore the
11089 shift. We can if the shift is a rotate or if the bits shifted out of
11090 this shift are known to be zero for both inputs and if the type of
11091 comparison is compatible with the shift. */
11092 if (GET_CODE (op0) == GET_CODE (op1)
11093 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11094 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11095 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11096 && (code != GT && code != LT && code != GE && code != LE))
11097 || (GET_CODE (op0) == ASHIFTRT
11098 && (code != GTU && code != LTU
11099 && code != GEU && code != LEU)))
11100 && CONST_INT_P (XEXP (op0, 1))
11101 && INTVAL (XEXP (op0, 1)) >= 0
11102 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11103 && XEXP (op0, 1) == XEXP (op1, 1))
11105 enum machine_mode mode = GET_MODE (op0);
11106 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11107 int shift_count = INTVAL (XEXP (op0, 1));
11109 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11110 mask &= (mask >> shift_count) << shift_count;
11111 else if (GET_CODE (op0) == ASHIFT)
11112 mask = (mask & (mask << shift_count)) >> shift_count;
11114 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11115 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11116 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11117 else
11118 break;
11121 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11122 SUBREGs are of the same mode, and, in both cases, the AND would
11123 be redundant if the comparison was done in the narrower mode,
11124 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11125 and the operand's possibly nonzero bits are 0xffffff01; in that case
11126 if we only care about QImode, we don't need the AND). This case
11127 occurs if the output mode of an scc insn is not SImode and
11128 STORE_FLAG_VALUE == 1 (e.g., the 386).
11130 Similarly, check for a case where the AND's are ZERO_EXTEND
11131 operations from some narrower mode even though a SUBREG is not
11132 present. */
11134 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11135 && CONST_INT_P (XEXP (op0, 1))
11136 && CONST_INT_P (XEXP (op1, 1)))
11138 rtx inner_op0 = XEXP (op0, 0);
11139 rtx inner_op1 = XEXP (op1, 0);
11140 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11141 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11142 int changed = 0;
11144 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
11145 && (GET_MODE_SIZE (GET_MODE (inner_op0))
11146 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
11147 && (GET_MODE (SUBREG_REG (inner_op0))
11148 == GET_MODE (SUBREG_REG (inner_op1)))
11149 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
11150 <= HOST_BITS_PER_WIDE_INT)
11151 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11152 GET_MODE (SUBREG_REG (inner_op0)))))
11153 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11154 GET_MODE (SUBREG_REG (inner_op1))))))
11156 op0 = SUBREG_REG (inner_op0);
11157 op1 = SUBREG_REG (inner_op1);
11159 /* The resulting comparison is always unsigned since we masked
11160 off the original sign bit. */
11161 code = unsigned_condition (code);
11163 changed = 1;
11166 else if (c0 == c1)
11167 for (tmode = GET_CLASS_NARROWEST_MODE
11168 (GET_MODE_CLASS (GET_MODE (op0)));
11169 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11170 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11172 op0 = gen_lowpart (tmode, inner_op0);
11173 op1 = gen_lowpart (tmode, inner_op1);
11174 code = unsigned_condition (code);
11175 changed = 1;
11176 break;
11179 if (! changed)
11180 break;
11183 /* If both operands are NOT, we can strip off the outer operation
11184 and adjust the comparison code for swapped operands; similarly for
11185 NEG, except that this must be an equality comparison. */
11186 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11187 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11188 && (code == EQ || code == NE)))
11189 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11191 else
11192 break;
11195 /* If the first operand is a constant, swap the operands and adjust the
11196 comparison code appropriately, but don't do this if the second operand
11197 is already a constant integer. */
11198 if (swap_commutative_operands_p (op0, op1))
11200 tem = op0, op0 = op1, op1 = tem;
11201 code = swap_condition (code);
11204 /* We now enter a loop during which we will try to simplify the comparison.
11205 For the most part, we only are concerned with comparisons with zero,
11206 but some things may really be comparisons with zero but not start
11207 out looking that way. */
11209 while (CONST_INT_P (op1))
11211 enum machine_mode mode = GET_MODE (op0);
11212 unsigned int mode_width = GET_MODE_BITSIZE (mode);
11213 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11214 int equality_comparison_p;
11215 int sign_bit_comparison_p;
11216 int unsigned_comparison_p;
11217 HOST_WIDE_INT const_op;
11219 /* We only want to handle integral modes. This catches VOIDmode,
11220 CCmode, and the floating-point modes. An exception is that we
11221 can handle VOIDmode if OP0 is a COMPARE or a comparison
11222 operation. */
11224 if (GET_MODE_CLASS (mode) != MODE_INT
11225 && ! (mode == VOIDmode
11226 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11227 break;
11229 /* Try to simplify the compare to constant, possibly changing the
11230 comparison op, and/or changing op1 to zero. */
11231 code = simplify_compare_const (code, op0, &op1);
11232 const_op = INTVAL (op1);
11234 /* Compute some predicates to simplify code below. */
11236 equality_comparison_p = (code == EQ || code == NE);
11237 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11238 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11239 || code == GEU);
11241 /* If this is a sign bit comparison and we can do arithmetic in
11242 MODE, say that we will only be needing the sign bit of OP0. */
11243 if (sign_bit_comparison_p
11244 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11245 op0 = force_to_mode (op0, mode,
11246 (unsigned HOST_WIDE_INT) 1
11247 << (GET_MODE_BITSIZE (mode) - 1),
11250 /* Now try cases based on the opcode of OP0. If none of the cases
11251 does a "continue", we exit this loop immediately after the
11252 switch. */
11254 switch (GET_CODE (op0))
11256 case ZERO_EXTRACT:
11257 /* If we are extracting a single bit from a variable position in
11258 a constant that has only a single bit set and are comparing it
11259 with zero, we can convert this into an equality comparison
11260 between the position and the location of the single bit. */
11261 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11262 have already reduced the shift count modulo the word size. */
11263 if (!SHIFT_COUNT_TRUNCATED
11264 && CONST_INT_P (XEXP (op0, 0))
11265 && XEXP (op0, 1) == const1_rtx
11266 && equality_comparison_p && const_op == 0
11267 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11269 if (BITS_BIG_ENDIAN)
11271 enum machine_mode new_mode
11272 = mode_for_extraction (EP_extzv, 1);
11273 if (new_mode == MAX_MACHINE_MODE)
11274 i = BITS_PER_WORD - 1 - i;
11275 else
11277 mode = new_mode;
11278 i = (GET_MODE_BITSIZE (mode) - 1 - i);
11282 op0 = XEXP (op0, 2);
11283 op1 = GEN_INT (i);
11284 const_op = i;
11286 /* Result is nonzero iff shift count is equal to I. */
11287 code = reverse_condition (code);
11288 continue;
11291 /* ... fall through ... */
11293 case SIGN_EXTRACT:
11294 tem = expand_compound_operation (op0);
11295 if (tem != op0)
11297 op0 = tem;
11298 continue;
11300 break;
11302 case NOT:
11303 /* If testing for equality, we can take the NOT of the constant. */
11304 if (equality_comparison_p
11305 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11307 op0 = XEXP (op0, 0);
11308 op1 = tem;
11309 continue;
11312 /* If just looking at the sign bit, reverse the sense of the
11313 comparison. */
11314 if (sign_bit_comparison_p)
11316 op0 = XEXP (op0, 0);
11317 code = (code == GE ? LT : GE);
11318 continue;
11320 break;
11322 case NEG:
11323 /* If testing for equality, we can take the NEG of the constant. */
11324 if (equality_comparison_p
11325 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11327 op0 = XEXP (op0, 0);
11328 op1 = tem;
11329 continue;
11332 /* The remaining cases only apply to comparisons with zero. */
11333 if (const_op != 0)
11334 break;
11336 /* When X is ABS or is known positive,
11337 (neg X) is < 0 if and only if X != 0. */
11339 if (sign_bit_comparison_p
11340 && (GET_CODE (XEXP (op0, 0)) == ABS
11341 || (mode_width <= HOST_BITS_PER_WIDE_INT
11342 && (nonzero_bits (XEXP (op0, 0), mode)
11343 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11344 == 0)))
11346 op0 = XEXP (op0, 0);
11347 code = (code == LT ? NE : EQ);
11348 continue;
11351 /* If we have NEG of something whose two high-order bits are the
11352 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11353 if (num_sign_bit_copies (op0, mode) >= 2)
11355 op0 = XEXP (op0, 0);
11356 code = swap_condition (code);
11357 continue;
11359 break;
11361 case ROTATE:
11362 /* If we are testing equality and our count is a constant, we
11363 can perform the inverse operation on our RHS. */
11364 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11365 && (tem = simplify_binary_operation (ROTATERT, mode,
11366 op1, XEXP (op0, 1))) != 0)
11368 op0 = XEXP (op0, 0);
11369 op1 = tem;
11370 continue;
11373 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11374 a particular bit. Convert it to an AND of a constant of that
11375 bit. This will be converted into a ZERO_EXTRACT. */
11376 if (const_op == 0 && sign_bit_comparison_p
11377 && CONST_INT_P (XEXP (op0, 1))
11378 && mode_width <= HOST_BITS_PER_WIDE_INT)
11380 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11381 ((unsigned HOST_WIDE_INT) 1
11382 << (mode_width - 1
11383 - INTVAL (XEXP (op0, 1)))));
11384 code = (code == LT ? NE : EQ);
11385 continue;
11388 /* Fall through. */
11390 case ABS:
11391 /* ABS is ignorable inside an equality comparison with zero. */
11392 if (const_op == 0 && equality_comparison_p)
11394 op0 = XEXP (op0, 0);
11395 continue;
11397 break;
11399 case SIGN_EXTEND:
11400 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11401 (compare FOO CONST) if CONST fits in FOO's mode and we
11402 are either testing inequality or have an unsigned
11403 comparison with ZERO_EXTEND or a signed comparison with
11404 SIGN_EXTEND. But don't do it if we don't have a compare
11405 insn of the given mode, since we'd have to revert it
11406 later on, and then we wouldn't know whether to sign- or
11407 zero-extend. */
11408 mode = GET_MODE (XEXP (op0, 0));
11409 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11410 && ! unsigned_comparison_p
11411 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11412 && ((unsigned HOST_WIDE_INT) const_op
11413 < (((unsigned HOST_WIDE_INT) 1
11414 << (GET_MODE_BITSIZE (mode) - 1))))
11415 && have_insn_for (COMPARE, mode))
11417 op0 = XEXP (op0, 0);
11418 continue;
11420 break;
11422 case SUBREG:
11423 /* Check for the case where we are comparing A - C1 with C2, that is
11425 (subreg:MODE (plus (A) (-C1))) op (C2)
11427 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11428 comparison in the wider mode. One of the following two conditions
11429 must be true in order for this to be valid:
11431 1. The mode extension results in the same bit pattern being added
11432 on both sides and the comparison is equality or unsigned. As
11433 C2 has been truncated to fit in MODE, the pattern can only be
11434 all 0s or all 1s.
11436 2. The mode extension results in the sign bit being copied on
11437 each side.
11439 The difficulty here is that we have predicates for A but not for
11440 (A - C1) so we need to check that C1 is within proper bounds so
11441 as to perturbate A as little as possible. */
11443 if (mode_width <= HOST_BITS_PER_WIDE_INT
11444 && subreg_lowpart_p (op0)
11445 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
11446 && GET_CODE (SUBREG_REG (op0)) == PLUS
11447 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11449 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11450 rtx a = XEXP (SUBREG_REG (op0), 0);
11451 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11453 if ((c1 > 0
11454 && (unsigned HOST_WIDE_INT) c1
11455 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11456 && (equality_comparison_p || unsigned_comparison_p)
11457 /* (A - C1) zero-extends if it is positive and sign-extends
11458 if it is negative, C2 both zero- and sign-extends. */
11459 && ((0 == (nonzero_bits (a, inner_mode)
11460 & ~GET_MODE_MASK (mode))
11461 && const_op >= 0)
11462 /* (A - C1) sign-extends if it is positive and 1-extends
11463 if it is negative, C2 both sign- and 1-extends. */
11464 || (num_sign_bit_copies (a, inner_mode)
11465 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11466 - mode_width)
11467 && const_op < 0)))
11468 || ((unsigned HOST_WIDE_INT) c1
11469 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11470 /* (A - C1) always sign-extends, like C2. */
11471 && num_sign_bit_copies (a, inner_mode)
11472 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11473 - (mode_width - 1))))
11475 op0 = SUBREG_REG (op0);
11476 continue;
11480 /* If the inner mode is narrower and we are extracting the low part,
11481 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11482 if (subreg_lowpart_p (op0)
11483 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
11484 /* Fall through */ ;
11485 else
11486 break;
11488 /* ... fall through ... */
11490 case ZERO_EXTEND:
11491 mode = GET_MODE (XEXP (op0, 0));
11492 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11493 && (unsigned_comparison_p || equality_comparison_p)
11494 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11495 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
11496 && have_insn_for (COMPARE, mode))
11498 op0 = XEXP (op0, 0);
11499 continue;
11501 break;
11503 case PLUS:
11504 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11505 this for equality comparisons due to pathological cases involving
11506 overflows. */
11507 if (equality_comparison_p
11508 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11509 op1, XEXP (op0, 1))))
11511 op0 = XEXP (op0, 0);
11512 op1 = tem;
11513 continue;
11516 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11517 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11518 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11520 op0 = XEXP (XEXP (op0, 0), 0);
11521 code = (code == LT ? EQ : NE);
11522 continue;
11524 break;
11526 case MINUS:
11527 /* We used to optimize signed comparisons against zero, but that
11528 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11529 arrive here as equality comparisons, or (GEU, LTU) are
11530 optimized away. No need to special-case them. */
11532 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11533 (eq B (minus A C)), whichever simplifies. We can only do
11534 this for equality comparisons due to pathological cases involving
11535 overflows. */
11536 if (equality_comparison_p
11537 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11538 XEXP (op0, 1), op1)))
11540 op0 = XEXP (op0, 0);
11541 op1 = tem;
11542 continue;
11545 if (equality_comparison_p
11546 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11547 XEXP (op0, 0), op1)))
11549 op0 = XEXP (op0, 1);
11550 op1 = tem;
11551 continue;
11554 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11555 of bits in X minus 1, is one iff X > 0. */
11556 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11557 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11558 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11559 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11561 op0 = XEXP (op0, 1);
11562 code = (code == GE ? LE : GT);
11563 continue;
11565 break;
11567 case XOR:
11568 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11569 if C is zero or B is a constant. */
11570 if (equality_comparison_p
11571 && 0 != (tem = simplify_binary_operation (XOR, mode,
11572 XEXP (op0, 1), op1)))
11574 op0 = XEXP (op0, 0);
11575 op1 = tem;
11576 continue;
11578 break;
11580 case EQ: case NE:
11581 case UNEQ: case LTGT:
11582 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11583 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11584 case UNORDERED: case ORDERED:
11585 /* We can't do anything if OP0 is a condition code value, rather
11586 than an actual data value. */
11587 if (const_op != 0
11588 || CC0_P (XEXP (op0, 0))
11589 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11590 break;
11592 /* Get the two operands being compared. */
11593 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11594 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11595 else
11596 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11598 /* Check for the cases where we simply want the result of the
11599 earlier test or the opposite of that result. */
11600 if (code == NE || code == EQ
11601 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11602 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11603 && (STORE_FLAG_VALUE
11604 & (((unsigned HOST_WIDE_INT) 1
11605 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11606 && (code == LT || code == GE)))
11608 enum rtx_code new_code;
11609 if (code == LT || code == NE)
11610 new_code = GET_CODE (op0);
11611 else
11612 new_code = reversed_comparison_code (op0, NULL);
11614 if (new_code != UNKNOWN)
11616 code = new_code;
11617 op0 = tem;
11618 op1 = tem1;
11619 continue;
11622 break;
11624 case IOR:
11625 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11626 iff X <= 0. */
11627 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11628 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11629 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11631 op0 = XEXP (op0, 1);
11632 code = (code == GE ? GT : LE);
11633 continue;
11635 break;
11637 case AND:
11638 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11639 will be converted to a ZERO_EXTRACT later. */
11640 if (const_op == 0 && equality_comparison_p
11641 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11642 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11644 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11645 XEXP (XEXP (op0, 0), 1));
11646 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11647 continue;
11650 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11651 zero and X is a comparison and C1 and C2 describe only bits set
11652 in STORE_FLAG_VALUE, we can compare with X. */
11653 if (const_op == 0 && equality_comparison_p
11654 && mode_width <= HOST_BITS_PER_WIDE_INT
11655 && CONST_INT_P (XEXP (op0, 1))
11656 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11657 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11658 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11659 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11661 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11662 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11663 if ((~STORE_FLAG_VALUE & mask) == 0
11664 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11665 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11666 && COMPARISON_P (tem))))
11668 op0 = XEXP (XEXP (op0, 0), 0);
11669 continue;
11673 /* If we are doing an equality comparison of an AND of a bit equal
11674 to the sign bit, replace this with a LT or GE comparison of
11675 the underlying value. */
11676 if (equality_comparison_p
11677 && const_op == 0
11678 && CONST_INT_P (XEXP (op0, 1))
11679 && mode_width <= HOST_BITS_PER_WIDE_INT
11680 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11681 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11683 op0 = XEXP (op0, 0);
11684 code = (code == EQ ? GE : LT);
11685 continue;
11688 /* If this AND operation is really a ZERO_EXTEND from a narrower
11689 mode, the constant fits within that mode, and this is either an
11690 equality or unsigned comparison, try to do this comparison in
11691 the narrower mode.
11693 Note that in:
11695 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11696 -> (ne:DI (reg:SI 4) (const_int 0))
11698 unless TRULY_NOOP_TRUNCATION allows it or the register is
11699 known to hold a value of the required mode the
11700 transformation is invalid. */
11701 if ((equality_comparison_p || unsigned_comparison_p)
11702 && CONST_INT_P (XEXP (op0, 1))
11703 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11704 & GET_MODE_MASK (mode))
11705 + 1)) >= 0
11706 && const_op >> i == 0
11707 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11708 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11709 GET_MODE_BITSIZE (GET_MODE (op0)))
11710 || (REG_P (XEXP (op0, 0))
11711 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11713 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11714 continue;
11717 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11718 fits in both M1 and M2 and the SUBREG is either paradoxical
11719 or represents the low part, permute the SUBREG and the AND
11720 and try again. */
11721 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11723 unsigned HOST_WIDE_INT c1;
11724 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11725 /* Require an integral mode, to avoid creating something like
11726 (AND:SF ...). */
11727 if (SCALAR_INT_MODE_P (tmode)
11728 /* It is unsafe to commute the AND into the SUBREG if the
11729 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11730 not defined. As originally written the upper bits
11731 have a defined value due to the AND operation.
11732 However, if we commute the AND inside the SUBREG then
11733 they no longer have defined values and the meaning of
11734 the code has been changed. */
11735 && (0
11736 #ifdef WORD_REGISTER_OPERATIONS
11737 || (mode_width > GET_MODE_BITSIZE (tmode)
11738 && mode_width <= BITS_PER_WORD)
11739 #endif
11740 || (mode_width <= GET_MODE_BITSIZE (tmode)
11741 && subreg_lowpart_p (XEXP (op0, 0))))
11742 && CONST_INT_P (XEXP (op0, 1))
11743 && mode_width <= HOST_BITS_PER_WIDE_INT
11744 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11745 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11746 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11747 && c1 != mask
11748 && c1 != GET_MODE_MASK (tmode))
11750 op0 = simplify_gen_binary (AND, tmode,
11751 SUBREG_REG (XEXP (op0, 0)),
11752 gen_int_mode (c1, tmode));
11753 op0 = gen_lowpart (mode, op0);
11754 continue;
11758 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11759 if (const_op == 0 && equality_comparison_p
11760 && XEXP (op0, 1) == const1_rtx
11761 && GET_CODE (XEXP (op0, 0)) == NOT)
11763 op0 = simplify_and_const_int (NULL_RTX, mode,
11764 XEXP (XEXP (op0, 0), 0), 1);
11765 code = (code == NE ? EQ : NE);
11766 continue;
11769 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11770 (eq (and (lshiftrt X) 1) 0).
11771 Also handle the case where (not X) is expressed using xor. */
11772 if (const_op == 0 && equality_comparison_p
11773 && XEXP (op0, 1) == const1_rtx
11774 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11776 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11777 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11779 if (GET_CODE (shift_op) == NOT
11780 || (GET_CODE (shift_op) == XOR
11781 && CONST_INT_P (XEXP (shift_op, 1))
11782 && CONST_INT_P (shift_count)
11783 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11784 && (UINTVAL (XEXP (shift_op, 1))
11785 == (unsigned HOST_WIDE_INT) 1
11786 << INTVAL (shift_count))))
11789 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11790 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11791 code = (code == NE ? EQ : NE);
11792 continue;
11795 break;
11797 case ASHIFT:
11798 /* If we have (compare (ashift FOO N) (const_int C)) and
11799 the high order N bits of FOO (N+1 if an inequality comparison)
11800 are known to be zero, we can do this by comparing FOO with C
11801 shifted right N bits so long as the low-order N bits of C are
11802 zero. */
11803 if (CONST_INT_P (XEXP (op0, 1))
11804 && INTVAL (XEXP (op0, 1)) >= 0
11805 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11806 < HOST_BITS_PER_WIDE_INT)
11807 && (((unsigned HOST_WIDE_INT) const_op
11808 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11809 - 1)) == 0)
11810 && mode_width <= HOST_BITS_PER_WIDE_INT
11811 && (nonzero_bits (XEXP (op0, 0), mode)
11812 & ~(mask >> (INTVAL (XEXP (op0, 1))
11813 + ! equality_comparison_p))) == 0)
11815 /* We must perform a logical shift, not an arithmetic one,
11816 as we want the top N bits of C to be zero. */
11817 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11819 temp >>= INTVAL (XEXP (op0, 1));
11820 op1 = gen_int_mode (temp, mode);
11821 op0 = XEXP (op0, 0);
11822 continue;
11825 /* If we are doing a sign bit comparison, it means we are testing
11826 a particular bit. Convert it to the appropriate AND. */
11827 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11828 && mode_width <= HOST_BITS_PER_WIDE_INT)
11830 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11831 ((unsigned HOST_WIDE_INT) 1
11832 << (mode_width - 1
11833 - INTVAL (XEXP (op0, 1)))));
11834 code = (code == LT ? NE : EQ);
11835 continue;
11838 /* If this an equality comparison with zero and we are shifting
11839 the low bit to the sign bit, we can convert this to an AND of the
11840 low-order bit. */
11841 if (const_op == 0 && equality_comparison_p
11842 && CONST_INT_P (XEXP (op0, 1))
11843 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11845 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11846 continue;
11848 break;
11850 case ASHIFTRT:
11851 /* If this is an equality comparison with zero, we can do this
11852 as a logical shift, which might be much simpler. */
11853 if (equality_comparison_p && const_op == 0
11854 && CONST_INT_P (XEXP (op0, 1)))
11856 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11857 XEXP (op0, 0),
11858 INTVAL (XEXP (op0, 1)));
11859 continue;
11862 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11863 do the comparison in a narrower mode. */
11864 if (! unsigned_comparison_p
11865 && CONST_INT_P (XEXP (op0, 1))
11866 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11867 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11868 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11869 MODE_INT, 1)) != BLKmode
11870 && (((unsigned HOST_WIDE_INT) const_op
11871 + (GET_MODE_MASK (tmode) >> 1) + 1)
11872 <= GET_MODE_MASK (tmode)))
11874 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11875 continue;
11878 /* Likewise if OP0 is a PLUS of a sign extension with a
11879 constant, which is usually represented with the PLUS
11880 between the shifts. */
11881 if (! unsigned_comparison_p
11882 && CONST_INT_P (XEXP (op0, 1))
11883 && GET_CODE (XEXP (op0, 0)) == PLUS
11884 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11885 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11886 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11887 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11888 MODE_INT, 1)) != BLKmode
11889 && (((unsigned HOST_WIDE_INT) const_op
11890 + (GET_MODE_MASK (tmode) >> 1) + 1)
11891 <= GET_MODE_MASK (tmode)))
11893 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11894 rtx add_const = XEXP (XEXP (op0, 0), 1);
11895 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11896 add_const, XEXP (op0, 1));
11898 op0 = simplify_gen_binary (PLUS, tmode,
11899 gen_lowpart (tmode, inner),
11900 new_const);
11901 continue;
11904 /* ... fall through ... */
11905 case LSHIFTRT:
11906 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11907 the low order N bits of FOO are known to be zero, we can do this
11908 by comparing FOO with C shifted left N bits so long as no
11909 overflow occurs. Even if the low order N bits of FOO aren't known
11910 to be zero, if the comparison is >= or < we can use the same
11911 optimization and for > or <= by setting all the low
11912 order N bits in the comparison constant. */
11913 if (CONST_INT_P (XEXP (op0, 1))
11914 && INTVAL (XEXP (op0, 1)) > 0
11915 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11916 && mode_width <= HOST_BITS_PER_WIDE_INT
11917 && (((unsigned HOST_WIDE_INT) const_op
11918 + (GET_CODE (op0) != LSHIFTRT
11919 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11920 + 1)
11921 : 0))
11922 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11924 unsigned HOST_WIDE_INT low_bits
11925 = (nonzero_bits (XEXP (op0, 0), mode)
11926 & (((unsigned HOST_WIDE_INT) 1
11927 << INTVAL (XEXP (op0, 1))) - 1));
11928 if (low_bits == 0 || !equality_comparison_p)
11930 /* If the shift was logical, then we must make the condition
11931 unsigned. */
11932 if (GET_CODE (op0) == LSHIFTRT)
11933 code = unsigned_condition (code);
11935 const_op <<= INTVAL (XEXP (op0, 1));
11936 if (low_bits != 0
11937 && (code == GT || code == GTU
11938 || code == LE || code == LEU))
11939 const_op
11940 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11941 op1 = GEN_INT (const_op);
11942 op0 = XEXP (op0, 0);
11943 continue;
11947 /* If we are using this shift to extract just the sign bit, we
11948 can replace this with an LT or GE comparison. */
11949 if (const_op == 0
11950 && (equality_comparison_p || sign_bit_comparison_p)
11951 && CONST_INT_P (XEXP (op0, 1))
11952 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11954 op0 = XEXP (op0, 0);
11955 code = (code == NE || code == GT ? LT : GE);
11956 continue;
11958 break;
11960 default:
11961 break;
11964 break;
11967 /* Now make any compound operations involved in this comparison. Then,
11968 check for an outmost SUBREG on OP0 that is not doing anything or is
11969 paradoxical. The latter transformation must only be performed when
11970 it is known that the "extra" bits will be the same in op0 and op1 or
11971 that they don't matter. There are three cases to consider:
11973 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11974 care bits and we can assume they have any convenient value. So
11975 making the transformation is safe.
11977 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11978 In this case the upper bits of op0 are undefined. We should not make
11979 the simplification in that case as we do not know the contents of
11980 those bits.
11982 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11983 UNKNOWN. In that case we know those bits are zeros or ones. We must
11984 also be sure that they are the same as the upper bits of op1.
11986 We can never remove a SUBREG for a non-equality comparison because
11987 the sign bit is in a different place in the underlying object. */
11989 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11990 op1 = make_compound_operation (op1, SET);
11992 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11993 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11994 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11995 && (code == NE || code == EQ))
11997 if (GET_MODE_SIZE (GET_MODE (op0))
11998 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
12000 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12001 implemented. */
12002 if (REG_P (SUBREG_REG (op0)))
12004 op0 = SUBREG_REG (op0);
12005 op1 = gen_lowpart (GET_MODE (op0), op1);
12008 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
12009 <= HOST_BITS_PER_WIDE_INT)
12010 && (nonzero_bits (SUBREG_REG (op0),
12011 GET_MODE (SUBREG_REG (op0)))
12012 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12014 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12016 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12017 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12018 op0 = SUBREG_REG (op0), op1 = tem;
12022 /* We now do the opposite procedure: Some machines don't have compare
12023 insns in all modes. If OP0's mode is an integer mode smaller than a
12024 word and we can't do a compare in that mode, see if there is a larger
12025 mode for which we can do the compare. There are a number of cases in
12026 which we can use the wider mode. */
12028 mode = GET_MODE (op0);
12029 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12030 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12031 && ! have_insn_for (COMPARE, mode))
12032 for (tmode = GET_MODE_WIDER_MODE (mode);
12033 (tmode != VOIDmode
12034 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
12035 tmode = GET_MODE_WIDER_MODE (tmode))
12036 if (have_insn_for (COMPARE, tmode))
12038 int zero_extended;
12040 /* If this is a test for negative, we can make an explicit
12041 test of the sign bit. Test this first so we can use
12042 a paradoxical subreg to extend OP0. */
12044 if (op1 == const0_rtx && (code == LT || code == GE)
12045 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12047 op0 = simplify_gen_binary (AND, tmode,
12048 gen_lowpart (tmode, op0),
12049 GEN_INT ((unsigned HOST_WIDE_INT) 1
12050 << (GET_MODE_BITSIZE (mode)
12051 - 1)));
12052 code = (code == LT) ? NE : EQ;
12053 break;
12056 /* If the only nonzero bits in OP0 and OP1 are those in the
12057 narrower mode and this is an equality or unsigned comparison,
12058 we can use the wider mode. Similarly for sign-extended
12059 values, in which case it is true for all comparisons. */
12060 zero_extended = ((code == EQ || code == NE
12061 || code == GEU || code == GTU
12062 || code == LEU || code == LTU)
12063 && (nonzero_bits (op0, tmode)
12064 & ~GET_MODE_MASK (mode)) == 0
12065 && ((CONST_INT_P (op1)
12066 || (nonzero_bits (op1, tmode)
12067 & ~GET_MODE_MASK (mode)) == 0)));
12069 if (zero_extended
12070 || ((num_sign_bit_copies (op0, tmode)
12071 > (unsigned int) (GET_MODE_BITSIZE (tmode)
12072 - GET_MODE_BITSIZE (mode)))
12073 && (num_sign_bit_copies (op1, tmode)
12074 > (unsigned int) (GET_MODE_BITSIZE (tmode)
12075 - GET_MODE_BITSIZE (mode)))))
12077 /* If OP0 is an AND and we don't have an AND in MODE either,
12078 make a new AND in the proper mode. */
12079 if (GET_CODE (op0) == AND
12080 && !have_insn_for (AND, mode))
12081 op0 = simplify_gen_binary (AND, tmode,
12082 gen_lowpart (tmode,
12083 XEXP (op0, 0)),
12084 gen_lowpart (tmode,
12085 XEXP (op0, 1)));
12086 else
12088 if (zero_extended)
12090 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12091 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12093 else
12095 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12096 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12098 break;
12103 #ifdef CANONICALIZE_COMPARISON
12104 /* If this machine only supports a subset of valid comparisons, see if we
12105 can convert an unsupported one into a supported one. */
12106 CANONICALIZE_COMPARISON (code, op0, op1);
12107 #endif
12109 *pop0 = op0;
12110 *pop1 = op1;
12112 return code;
12115 /* Utility function for record_value_for_reg. Count number of
12116 rtxs in X. */
12117 static int
12118 count_rtxs (rtx x)
12120 enum rtx_code code = GET_CODE (x);
12121 const char *fmt;
12122 int i, j, ret = 1;
12124 if (GET_RTX_CLASS (code) == '2'
12125 || GET_RTX_CLASS (code) == 'c')
12127 rtx x0 = XEXP (x, 0);
12128 rtx x1 = XEXP (x, 1);
12130 if (x0 == x1)
12131 return 1 + 2 * count_rtxs (x0);
12133 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
12134 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
12135 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12136 return 2 + 2 * count_rtxs (x0)
12137 + count_rtxs (x == XEXP (x1, 0)
12138 ? XEXP (x1, 1) : XEXP (x1, 0));
12140 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
12141 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
12142 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12143 return 2 + 2 * count_rtxs (x1)
12144 + count_rtxs (x == XEXP (x0, 0)
12145 ? XEXP (x0, 1) : XEXP (x0, 0));
12148 fmt = GET_RTX_FORMAT (code);
12149 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12150 if (fmt[i] == 'e')
12151 ret += count_rtxs (XEXP (x, i));
12152 else if (fmt[i] == 'E')
12153 for (j = 0; j < XVECLEN (x, i); j++)
12154 ret += count_rtxs (XVECEXP (x, i, j));
12156 return ret;
12159 /* Utility function for following routine. Called when X is part of a value
12160 being stored into last_set_value. Sets last_set_table_tick
12161 for each register mentioned. Similar to mention_regs in cse.c */
12163 static void
12164 update_table_tick (rtx x)
12166 enum rtx_code code = GET_CODE (x);
12167 const char *fmt = GET_RTX_FORMAT (code);
12168 int i, j;
12170 if (code == REG)
12172 unsigned int regno = REGNO (x);
12173 unsigned int endregno = END_REGNO (x);
12174 unsigned int r;
12176 for (r = regno; r < endregno; r++)
12178 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
12179 rsp->last_set_table_tick = label_tick;
12182 return;
12185 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12186 if (fmt[i] == 'e')
12188 /* Check for identical subexpressions. If x contains
12189 identical subexpression we only have to traverse one of
12190 them. */
12191 if (i == 0 && ARITHMETIC_P (x))
12193 /* Note that at this point x1 has already been
12194 processed. */
12195 rtx x0 = XEXP (x, 0);
12196 rtx x1 = XEXP (x, 1);
12198 /* If x0 and x1 are identical then there is no need to
12199 process x0. */
12200 if (x0 == x1)
12201 break;
12203 /* If x0 is identical to a subexpression of x1 then while
12204 processing x1, x0 has already been processed. Thus we
12205 are done with x. */
12206 if (ARITHMETIC_P (x1)
12207 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12208 break;
12210 /* If x1 is identical to a subexpression of x0 then we
12211 still have to process the rest of x0. */
12212 if (ARITHMETIC_P (x0)
12213 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12215 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12216 break;
12220 update_table_tick (XEXP (x, i));
12222 else if (fmt[i] == 'E')
12223 for (j = 0; j < XVECLEN (x, i); j++)
12224 update_table_tick (XVECEXP (x, i, j));
12227 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12228 are saying that the register is clobbered and we no longer know its
12229 value. If INSN is zero, don't update reg_stat[].last_set; this is
12230 only permitted with VALUE also zero and is used to invalidate the
12231 register. */
12233 static void
12234 record_value_for_reg (rtx reg, rtx insn, rtx value)
12236 unsigned int regno = REGNO (reg);
12237 unsigned int endregno = END_REGNO (reg);
12238 unsigned int i;
12239 reg_stat_type *rsp;
12241 /* If VALUE contains REG and we have a previous value for REG, substitute
12242 the previous value. */
12243 if (value && insn && reg_overlap_mentioned_p (reg, value))
12245 rtx tem;
12247 /* Set things up so get_last_value is allowed to see anything set up to
12248 our insn. */
12249 subst_low_luid = DF_INSN_LUID (insn);
12250 tem = get_last_value (reg);
12252 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12253 it isn't going to be useful and will take a lot of time to process,
12254 so just use the CLOBBER. */
12256 if (tem)
12258 if (ARITHMETIC_P (tem)
12259 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12260 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12261 tem = XEXP (tem, 0);
12262 else if (count_occurrences (value, reg, 1) >= 2)
12264 /* If there are two or more occurrences of REG in VALUE,
12265 prevent the value from growing too much. */
12266 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12267 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12270 value = replace_rtx (copy_rtx (value), reg, tem);
12274 /* For each register modified, show we don't know its value, that
12275 we don't know about its bitwise content, that its value has been
12276 updated, and that we don't know the location of the death of the
12277 register. */
12278 for (i = regno; i < endregno; i++)
12280 rsp = VEC_index (reg_stat_type, reg_stat, i);
12282 if (insn)
12283 rsp->last_set = insn;
12285 rsp->last_set_value = 0;
12286 rsp->last_set_mode = VOIDmode;
12287 rsp->last_set_nonzero_bits = 0;
12288 rsp->last_set_sign_bit_copies = 0;
12289 rsp->last_death = 0;
12290 rsp->truncated_to_mode = VOIDmode;
12293 /* Mark registers that are being referenced in this value. */
12294 if (value)
12295 update_table_tick (value);
12297 /* Now update the status of each register being set.
12298 If someone is using this register in this block, set this register
12299 to invalid since we will get confused between the two lives in this
12300 basic block. This makes using this register always invalid. In cse, we
12301 scan the table to invalidate all entries using this register, but this
12302 is too much work for us. */
12304 for (i = regno; i < endregno; i++)
12306 rsp = VEC_index (reg_stat_type, reg_stat, i);
12307 rsp->last_set_label = label_tick;
12308 if (!insn
12309 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12310 rsp->last_set_invalid = 1;
12311 else
12312 rsp->last_set_invalid = 0;
12315 /* The value being assigned might refer to X (like in "x++;"). In that
12316 case, we must replace it with (clobber (const_int 0)) to prevent
12317 infinite loops. */
12318 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12319 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12321 value = copy_rtx (value);
12322 if (!get_last_value_validate (&value, insn, label_tick, 1))
12323 value = 0;
12326 /* For the main register being modified, update the value, the mode, the
12327 nonzero bits, and the number of sign bit copies. */
12329 rsp->last_set_value = value;
12331 if (value)
12333 enum machine_mode mode = GET_MODE (reg);
12334 subst_low_luid = DF_INSN_LUID (insn);
12335 rsp->last_set_mode = mode;
12336 if (GET_MODE_CLASS (mode) == MODE_INT
12337 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12338 mode = nonzero_bits_mode;
12339 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12340 rsp->last_set_sign_bit_copies
12341 = num_sign_bit_copies (value, GET_MODE (reg));
12345 /* Called via note_stores from record_dead_and_set_regs to handle one
12346 SET or CLOBBER in an insn. DATA is the instruction in which the
12347 set is occurring. */
12349 static void
12350 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12352 rtx record_dead_insn = (rtx) data;
12354 if (GET_CODE (dest) == SUBREG)
12355 dest = SUBREG_REG (dest);
12357 if (!record_dead_insn)
12359 if (REG_P (dest))
12360 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12361 return;
12364 if (REG_P (dest))
12366 /* If we are setting the whole register, we know its value. Otherwise
12367 show that we don't know the value. We can handle SUBREG in
12368 some cases. */
12369 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12370 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12371 else if (GET_CODE (setter) == SET
12372 && GET_CODE (SET_DEST (setter)) == SUBREG
12373 && SUBREG_REG (SET_DEST (setter)) == dest
12374 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
12375 && subreg_lowpart_p (SET_DEST (setter)))
12376 record_value_for_reg (dest, record_dead_insn,
12377 gen_lowpart (GET_MODE (dest),
12378 SET_SRC (setter)));
12379 else
12380 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12382 else if (MEM_P (dest)
12383 /* Ignore pushes, they clobber nothing. */
12384 && ! push_operand (dest, GET_MODE (dest)))
12385 mem_last_set = DF_INSN_LUID (record_dead_insn);
12388 /* Update the records of when each REG was most recently set or killed
12389 for the things done by INSN. This is the last thing done in processing
12390 INSN in the combiner loop.
12392 We update reg_stat[], in particular fields last_set, last_set_value,
12393 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12394 last_death, and also the similar information mem_last_set (which insn
12395 most recently modified memory) and last_call_luid (which insn was the
12396 most recent subroutine call). */
12398 static void
12399 record_dead_and_set_regs (rtx insn)
12401 rtx link;
12402 unsigned int i;
12404 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12406 if (REG_NOTE_KIND (link) == REG_DEAD
12407 && REG_P (XEXP (link, 0)))
12409 unsigned int regno = REGNO (XEXP (link, 0));
12410 unsigned int endregno = END_REGNO (XEXP (link, 0));
12412 for (i = regno; i < endregno; i++)
12414 reg_stat_type *rsp;
12416 rsp = VEC_index (reg_stat_type, reg_stat, i);
12417 rsp->last_death = insn;
12420 else if (REG_NOTE_KIND (link) == REG_INC)
12421 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12424 if (CALL_P (insn))
12426 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12427 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12429 reg_stat_type *rsp;
12431 rsp = VEC_index (reg_stat_type, reg_stat, i);
12432 rsp->last_set_invalid = 1;
12433 rsp->last_set = insn;
12434 rsp->last_set_value = 0;
12435 rsp->last_set_mode = VOIDmode;
12436 rsp->last_set_nonzero_bits = 0;
12437 rsp->last_set_sign_bit_copies = 0;
12438 rsp->last_death = 0;
12439 rsp->truncated_to_mode = VOIDmode;
12442 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12444 /* We can't combine into a call pattern. Remember, though, that
12445 the return value register is set at this LUID. We could
12446 still replace a register with the return value from the
12447 wrong subroutine call! */
12448 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12450 else
12451 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12454 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12455 register present in the SUBREG, so for each such SUBREG go back and
12456 adjust nonzero and sign bit information of the registers that are
12457 known to have some zero/sign bits set.
12459 This is needed because when combine blows the SUBREGs away, the
12460 information on zero/sign bits is lost and further combines can be
12461 missed because of that. */
12463 static void
12464 record_promoted_value (rtx insn, rtx subreg)
12466 struct insn_link *links;
12467 rtx set;
12468 unsigned int regno = REGNO (SUBREG_REG (subreg));
12469 enum machine_mode mode = GET_MODE (subreg);
12471 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
12472 return;
12474 for (links = LOG_LINKS (insn); links;)
12476 reg_stat_type *rsp;
12478 insn = links->insn;
12479 set = single_set (insn);
12481 if (! set || !REG_P (SET_DEST (set))
12482 || REGNO (SET_DEST (set)) != regno
12483 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12485 links = links->next;
12486 continue;
12489 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12490 if (rsp->last_set == insn)
12492 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12493 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12496 if (REG_P (SET_SRC (set)))
12498 regno = REGNO (SET_SRC (set));
12499 links = LOG_LINKS (insn);
12501 else
12502 break;
12506 /* Check if X, a register, is known to contain a value already
12507 truncated to MODE. In this case we can use a subreg to refer to
12508 the truncated value even though in the generic case we would need
12509 an explicit truncation. */
12511 static bool
12512 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12514 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12515 enum machine_mode truncated = rsp->truncated_to_mode;
12517 if (truncated == 0
12518 || rsp->truncation_label < label_tick_ebb_start)
12519 return false;
12520 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12521 return true;
12522 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
12523 GET_MODE_BITSIZE (truncated)))
12524 return true;
12525 return false;
12528 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
12529 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
12530 might be able to turn a truncate into a subreg using this information.
12531 Return -1 if traversing *P is complete or 0 otherwise. */
12533 static int
12534 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12536 rtx x = *p;
12537 enum machine_mode truncated_mode;
12538 reg_stat_type *rsp;
12540 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12542 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12543 truncated_mode = GET_MODE (x);
12545 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12546 return -1;
12548 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
12549 GET_MODE_BITSIZE (original_mode)))
12550 return -1;
12552 x = SUBREG_REG (x);
12554 /* ??? For hard-regs we now record everything. We might be able to
12555 optimize this using last_set_mode. */
12556 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12557 truncated_mode = GET_MODE (x);
12558 else
12559 return 0;
12561 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12562 if (rsp->truncated_to_mode == 0
12563 || rsp->truncation_label < label_tick_ebb_start
12564 || (GET_MODE_SIZE (truncated_mode)
12565 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12567 rsp->truncated_to_mode = truncated_mode;
12568 rsp->truncation_label = label_tick;
12571 return -1;
12574 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12575 the modes they are used in. This can help truning TRUNCATEs into
12576 SUBREGs. */
12578 static void
12579 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12581 for_each_rtx (x, record_truncated_value, NULL);
12584 /* Scan X for promoted SUBREGs. For each one found,
12585 note what it implies to the registers used in it. */
12587 static void
12588 check_promoted_subreg (rtx insn, rtx x)
12590 if (GET_CODE (x) == SUBREG
12591 && SUBREG_PROMOTED_VAR_P (x)
12592 && REG_P (SUBREG_REG (x)))
12593 record_promoted_value (insn, x);
12594 else
12596 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12597 int i, j;
12599 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12600 switch (format[i])
12602 case 'e':
12603 check_promoted_subreg (insn, XEXP (x, i));
12604 break;
12605 case 'V':
12606 case 'E':
12607 if (XVEC (x, i) != 0)
12608 for (j = 0; j < XVECLEN (x, i); j++)
12609 check_promoted_subreg (insn, XVECEXP (x, i, j));
12610 break;
12615 /* Verify that all the registers and memory references mentioned in *LOC are
12616 still valid. *LOC was part of a value set in INSN when label_tick was
12617 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12618 the invalid references with (clobber (const_int 0)) and return 1. This
12619 replacement is useful because we often can get useful information about
12620 the form of a value (e.g., if it was produced by a shift that always
12621 produces -1 or 0) even though we don't know exactly what registers it
12622 was produced from. */
12624 static int
12625 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12627 rtx x = *loc;
12628 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12629 int len = GET_RTX_LENGTH (GET_CODE (x));
12630 int i, j;
12632 if (REG_P (x))
12634 unsigned int regno = REGNO (x);
12635 unsigned int endregno = END_REGNO (x);
12636 unsigned int j;
12638 for (j = regno; j < endregno; j++)
12640 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12641 if (rsp->last_set_invalid
12642 /* If this is a pseudo-register that was only set once and not
12643 live at the beginning of the function, it is always valid. */
12644 || (! (regno >= FIRST_PSEUDO_REGISTER
12645 && REG_N_SETS (regno) == 1
12646 && (!REGNO_REG_SET_P
12647 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12648 && rsp->last_set_label > tick))
12650 if (replace)
12651 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12652 return replace;
12656 return 1;
12658 /* If this is a memory reference, make sure that there were no stores after
12659 it that might have clobbered the value. We don't have alias info, so we
12660 assume any store invalidates it. Moreover, we only have local UIDs, so
12661 we also assume that there were stores in the intervening basic blocks. */
12662 else if (MEM_P (x) && !MEM_READONLY_P (x)
12663 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12665 if (replace)
12666 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12667 return replace;
12670 for (i = 0; i < len; i++)
12672 if (fmt[i] == 'e')
12674 /* Check for identical subexpressions. If x contains
12675 identical subexpression we only have to traverse one of
12676 them. */
12677 if (i == 1 && ARITHMETIC_P (x))
12679 /* Note that at this point x0 has already been checked
12680 and found valid. */
12681 rtx x0 = XEXP (x, 0);
12682 rtx x1 = XEXP (x, 1);
12684 /* If x0 and x1 are identical then x is also valid. */
12685 if (x0 == x1)
12686 return 1;
12688 /* If x1 is identical to a subexpression of x0 then
12689 while checking x0, x1 has already been checked. Thus
12690 it is valid and so as x. */
12691 if (ARITHMETIC_P (x0)
12692 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12693 return 1;
12695 /* If x0 is identical to a subexpression of x1 then x is
12696 valid iff the rest of x1 is valid. */
12697 if (ARITHMETIC_P (x1)
12698 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12699 return
12700 get_last_value_validate (&XEXP (x1,
12701 x0 == XEXP (x1, 0) ? 1 : 0),
12702 insn, tick, replace);
12705 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12706 replace) == 0)
12707 return 0;
12709 else if (fmt[i] == 'E')
12710 for (j = 0; j < XVECLEN (x, i); j++)
12711 if (get_last_value_validate (&XVECEXP (x, i, j),
12712 insn, tick, replace) == 0)
12713 return 0;
12716 /* If we haven't found a reason for it to be invalid, it is valid. */
12717 return 1;
12720 /* Get the last value assigned to X, if known. Some registers
12721 in the value may be replaced with (clobber (const_int 0)) if their value
12722 is known longer known reliably. */
12724 static rtx
12725 get_last_value (const_rtx x)
12727 unsigned int regno;
12728 rtx value;
12729 reg_stat_type *rsp;
12731 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12732 then convert it to the desired mode. If this is a paradoxical SUBREG,
12733 we cannot predict what values the "extra" bits might have. */
12734 if (GET_CODE (x) == SUBREG
12735 && subreg_lowpart_p (x)
12736 && (GET_MODE_SIZE (GET_MODE (x))
12737 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12738 && (value = get_last_value (SUBREG_REG (x))) != 0)
12739 return gen_lowpart (GET_MODE (x), value);
12741 if (!REG_P (x))
12742 return 0;
12744 regno = REGNO (x);
12745 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12746 value = rsp->last_set_value;
12748 /* If we don't have a value, or if it isn't for this basic block and
12749 it's either a hard register, set more than once, or it's a live
12750 at the beginning of the function, return 0.
12752 Because if it's not live at the beginning of the function then the reg
12753 is always set before being used (is never used without being set).
12754 And, if it's set only once, and it's always set before use, then all
12755 uses must have the same last value, even if it's not from this basic
12756 block. */
12758 if (value == 0
12759 || (rsp->last_set_label < label_tick_ebb_start
12760 && (regno < FIRST_PSEUDO_REGISTER
12761 || REG_N_SETS (regno) != 1
12762 || REGNO_REG_SET_P
12763 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12764 return 0;
12766 /* If the value was set in a later insn than the ones we are processing,
12767 we can't use it even if the register was only set once. */
12768 if (rsp->last_set_label == label_tick
12769 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12770 return 0;
12772 /* If the value has all its registers valid, return it. */
12773 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12774 return value;
12776 /* Otherwise, make a copy and replace any invalid register with
12777 (clobber (const_int 0)). If that fails for some reason, return 0. */
12779 value = copy_rtx (value);
12780 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12781 return value;
12783 return 0;
12786 /* Return nonzero if expression X refers to a REG or to memory
12787 that is set in an instruction more recent than FROM_LUID. */
12789 static int
12790 use_crosses_set_p (const_rtx x, int from_luid)
12792 const char *fmt;
12793 int i;
12794 enum rtx_code code = GET_CODE (x);
12796 if (code == REG)
12798 unsigned int regno = REGNO (x);
12799 unsigned endreg = END_REGNO (x);
12801 #ifdef PUSH_ROUNDING
12802 /* Don't allow uses of the stack pointer to be moved,
12803 because we don't know whether the move crosses a push insn. */
12804 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12805 return 1;
12806 #endif
12807 for (; regno < endreg; regno++)
12809 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12810 if (rsp->last_set
12811 && rsp->last_set_label == label_tick
12812 && DF_INSN_LUID (rsp->last_set) > from_luid)
12813 return 1;
12815 return 0;
12818 if (code == MEM && mem_last_set > from_luid)
12819 return 1;
12821 fmt = GET_RTX_FORMAT (code);
12823 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12825 if (fmt[i] == 'E')
12827 int j;
12828 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12829 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12830 return 1;
12832 else if (fmt[i] == 'e'
12833 && use_crosses_set_p (XEXP (x, i), from_luid))
12834 return 1;
12836 return 0;
12839 /* Define three variables used for communication between the following
12840 routines. */
12842 static unsigned int reg_dead_regno, reg_dead_endregno;
12843 static int reg_dead_flag;
12845 /* Function called via note_stores from reg_dead_at_p.
12847 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12848 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12850 static void
12851 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12853 unsigned int regno, endregno;
12855 if (!REG_P (dest))
12856 return;
12858 regno = REGNO (dest);
12859 endregno = END_REGNO (dest);
12860 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12861 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12864 /* Return nonzero if REG is known to be dead at INSN.
12866 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12867 referencing REG, it is dead. If we hit a SET referencing REG, it is
12868 live. Otherwise, see if it is live or dead at the start of the basic
12869 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12870 must be assumed to be always live. */
12872 static int
12873 reg_dead_at_p (rtx reg, rtx insn)
12875 basic_block block;
12876 unsigned int i;
12878 /* Set variables for reg_dead_at_p_1. */
12879 reg_dead_regno = REGNO (reg);
12880 reg_dead_endregno = END_REGNO (reg);
12882 reg_dead_flag = 0;
12884 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12885 we allow the machine description to decide whether use-and-clobber
12886 patterns are OK. */
12887 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12889 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12890 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12891 return 0;
12894 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12895 beginning of basic block. */
12896 block = BLOCK_FOR_INSN (insn);
12897 for (;;)
12899 if (INSN_P (insn))
12901 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12902 if (reg_dead_flag)
12903 return reg_dead_flag == 1 ? 1 : 0;
12905 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12906 return 1;
12909 if (insn == BB_HEAD (block))
12910 break;
12912 insn = PREV_INSN (insn);
12915 /* Look at live-in sets for the basic block that we were in. */
12916 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12917 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12918 return 0;
12920 return 1;
12923 /* Note hard registers in X that are used. */
12925 static void
12926 mark_used_regs_combine (rtx x)
12928 RTX_CODE code = GET_CODE (x);
12929 unsigned int regno;
12930 int i;
12932 switch (code)
12934 case LABEL_REF:
12935 case SYMBOL_REF:
12936 case CONST_INT:
12937 case CONST:
12938 case CONST_DOUBLE:
12939 case CONST_VECTOR:
12940 case PC:
12941 case ADDR_VEC:
12942 case ADDR_DIFF_VEC:
12943 case ASM_INPUT:
12944 #ifdef HAVE_cc0
12945 /* CC0 must die in the insn after it is set, so we don't need to take
12946 special note of it here. */
12947 case CC0:
12948 #endif
12949 return;
12951 case CLOBBER:
12952 /* If we are clobbering a MEM, mark any hard registers inside the
12953 address as used. */
12954 if (MEM_P (XEXP (x, 0)))
12955 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12956 return;
12958 case REG:
12959 regno = REGNO (x);
12960 /* A hard reg in a wide mode may really be multiple registers.
12961 If so, mark all of them just like the first. */
12962 if (regno < FIRST_PSEUDO_REGISTER)
12964 /* None of this applies to the stack, frame or arg pointers. */
12965 if (regno == STACK_POINTER_REGNUM
12966 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12967 || regno == HARD_FRAME_POINTER_REGNUM
12968 #endif
12969 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12970 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12971 #endif
12972 || regno == FRAME_POINTER_REGNUM)
12973 return;
12975 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12977 return;
12979 case SET:
12981 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12982 the address. */
12983 rtx testreg = SET_DEST (x);
12985 while (GET_CODE (testreg) == SUBREG
12986 || GET_CODE (testreg) == ZERO_EXTRACT
12987 || GET_CODE (testreg) == STRICT_LOW_PART)
12988 testreg = XEXP (testreg, 0);
12990 if (MEM_P (testreg))
12991 mark_used_regs_combine (XEXP (testreg, 0));
12993 mark_used_regs_combine (SET_SRC (x));
12995 return;
12997 default:
12998 break;
13001 /* Recursively scan the operands of this expression. */
13004 const char *fmt = GET_RTX_FORMAT (code);
13006 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13008 if (fmt[i] == 'e')
13009 mark_used_regs_combine (XEXP (x, i));
13010 else if (fmt[i] == 'E')
13012 int j;
13014 for (j = 0; j < XVECLEN (x, i); j++)
13015 mark_used_regs_combine (XVECEXP (x, i, j));
13021 /* Remove register number REGNO from the dead registers list of INSN.
13023 Return the note used to record the death, if there was one. */
13026 remove_death (unsigned int regno, rtx insn)
13028 rtx note = find_regno_note (insn, REG_DEAD, regno);
13030 if (note)
13031 remove_note (insn, note);
13033 return note;
13036 /* For each register (hardware or pseudo) used within expression X, if its
13037 death is in an instruction with luid between FROM_LUID (inclusive) and
13038 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13039 list headed by PNOTES.
13041 That said, don't move registers killed by maybe_kill_insn.
13043 This is done when X is being merged by combination into TO_INSN. These
13044 notes will then be distributed as needed. */
13046 static void
13047 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
13048 rtx *pnotes)
13050 const char *fmt;
13051 int len, i;
13052 enum rtx_code code = GET_CODE (x);
13054 if (code == REG)
13056 unsigned int regno = REGNO (x);
13057 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
13059 /* Don't move the register if it gets killed in between from and to. */
13060 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13061 && ! reg_referenced_p (x, maybe_kill_insn))
13062 return;
13064 if (where_dead
13065 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13066 && DF_INSN_LUID (where_dead) >= from_luid
13067 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13069 rtx note = remove_death (regno, where_dead);
13071 /* It is possible for the call above to return 0. This can occur
13072 when last_death points to I2 or I1 that we combined with.
13073 In that case make a new note.
13075 We must also check for the case where X is a hard register
13076 and NOTE is a death note for a range of hard registers
13077 including X. In that case, we must put REG_DEAD notes for
13078 the remaining registers in place of NOTE. */
13080 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13081 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13082 > GET_MODE_SIZE (GET_MODE (x))))
13084 unsigned int deadregno = REGNO (XEXP (note, 0));
13085 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13086 unsigned int ourend = END_HARD_REGNO (x);
13087 unsigned int i;
13089 for (i = deadregno; i < deadend; i++)
13090 if (i < regno || i >= ourend)
13091 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13094 /* If we didn't find any note, or if we found a REG_DEAD note that
13095 covers only part of the given reg, and we have a multi-reg hard
13096 register, then to be safe we must check for REG_DEAD notes
13097 for each register other than the first. They could have
13098 their own REG_DEAD notes lying around. */
13099 else if ((note == 0
13100 || (note != 0
13101 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13102 < GET_MODE_SIZE (GET_MODE (x)))))
13103 && regno < FIRST_PSEUDO_REGISTER
13104 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13106 unsigned int ourend = END_HARD_REGNO (x);
13107 unsigned int i, offset;
13108 rtx oldnotes = 0;
13110 if (note)
13111 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13112 else
13113 offset = 1;
13115 for (i = regno + offset; i < ourend; i++)
13116 move_deaths (regno_reg_rtx[i],
13117 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13120 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13122 XEXP (note, 1) = *pnotes;
13123 *pnotes = note;
13125 else
13126 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13129 return;
13132 else if (GET_CODE (x) == SET)
13134 rtx dest = SET_DEST (x);
13136 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13138 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13139 that accesses one word of a multi-word item, some
13140 piece of everything register in the expression is used by
13141 this insn, so remove any old death. */
13142 /* ??? So why do we test for equality of the sizes? */
13144 if (GET_CODE (dest) == ZERO_EXTRACT
13145 || GET_CODE (dest) == STRICT_LOW_PART
13146 || (GET_CODE (dest) == SUBREG
13147 && (((GET_MODE_SIZE (GET_MODE (dest))
13148 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13149 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13150 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13152 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13153 return;
13156 /* If this is some other SUBREG, we know it replaces the entire
13157 value, so use that as the destination. */
13158 if (GET_CODE (dest) == SUBREG)
13159 dest = SUBREG_REG (dest);
13161 /* If this is a MEM, adjust deaths of anything used in the address.
13162 For a REG (the only other possibility), the entire value is
13163 being replaced so the old value is not used in this insn. */
13165 if (MEM_P (dest))
13166 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13167 to_insn, pnotes);
13168 return;
13171 else if (GET_CODE (x) == CLOBBER)
13172 return;
13174 len = GET_RTX_LENGTH (code);
13175 fmt = GET_RTX_FORMAT (code);
13177 for (i = 0; i < len; i++)
13179 if (fmt[i] == 'E')
13181 int j;
13182 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13183 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13184 to_insn, pnotes);
13186 else if (fmt[i] == 'e')
13187 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13191 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13192 pattern of an insn. X must be a REG. */
13194 static int
13195 reg_bitfield_target_p (rtx x, rtx body)
13197 int i;
13199 if (GET_CODE (body) == SET)
13201 rtx dest = SET_DEST (body);
13202 rtx target;
13203 unsigned int regno, tregno, endregno, endtregno;
13205 if (GET_CODE (dest) == ZERO_EXTRACT)
13206 target = XEXP (dest, 0);
13207 else if (GET_CODE (dest) == STRICT_LOW_PART)
13208 target = SUBREG_REG (XEXP (dest, 0));
13209 else
13210 return 0;
13212 if (GET_CODE (target) == SUBREG)
13213 target = SUBREG_REG (target);
13215 if (!REG_P (target))
13216 return 0;
13218 tregno = REGNO (target), regno = REGNO (x);
13219 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13220 return target == x;
13222 endtregno = end_hard_regno (GET_MODE (target), tregno);
13223 endregno = end_hard_regno (GET_MODE (x), regno);
13225 return endregno > tregno && regno < endtregno;
13228 else if (GET_CODE (body) == PARALLEL)
13229 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13230 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13231 return 1;
13233 return 0;
13236 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13237 as appropriate. I3 and I2 are the insns resulting from the combination
13238 insns including FROM (I2 may be zero).
13240 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13241 not need REG_DEAD notes because they are being substituted for. This
13242 saves searching in the most common cases.
13244 Each note in the list is either ignored or placed on some insns, depending
13245 on the type of note. */
13247 static void
13248 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13249 rtx elim_i1, rtx elim_i0)
13251 rtx note, next_note;
13252 rtx tem;
13254 for (note = notes; note; note = next_note)
13256 rtx place = 0, place2 = 0;
13258 next_note = XEXP (note, 1);
13259 switch (REG_NOTE_KIND (note))
13261 case REG_BR_PROB:
13262 case REG_BR_PRED:
13263 /* Doesn't matter much where we put this, as long as it's somewhere.
13264 It is preferable to keep these notes on branches, which is most
13265 likely to be i3. */
13266 place = i3;
13267 break;
13269 case REG_NON_LOCAL_GOTO:
13270 if (JUMP_P (i3))
13271 place = i3;
13272 else
13274 gcc_assert (i2 && JUMP_P (i2));
13275 place = i2;
13277 break;
13279 case REG_EH_REGION:
13280 /* These notes must remain with the call or trapping instruction. */
13281 if (CALL_P (i3))
13282 place = i3;
13283 else if (i2 && CALL_P (i2))
13284 place = i2;
13285 else
13287 gcc_assert (cfun->can_throw_non_call_exceptions);
13288 if (may_trap_p (i3))
13289 place = i3;
13290 else if (i2 && may_trap_p (i2))
13291 place = i2;
13292 /* ??? Otherwise assume we've combined things such that we
13293 can now prove that the instructions can't trap. Drop the
13294 note in this case. */
13296 break;
13298 case REG_NORETURN:
13299 case REG_SETJMP:
13300 /* These notes must remain with the call. It should not be
13301 possible for both I2 and I3 to be a call. */
13302 if (CALL_P (i3))
13303 place = i3;
13304 else
13306 gcc_assert (i2 && CALL_P (i2));
13307 place = i2;
13309 break;
13311 case REG_UNUSED:
13312 /* Any clobbers for i3 may still exist, and so we must process
13313 REG_UNUSED notes from that insn.
13315 Any clobbers from i2 or i1 can only exist if they were added by
13316 recog_for_combine. In that case, recog_for_combine created the
13317 necessary REG_UNUSED notes. Trying to keep any original
13318 REG_UNUSED notes from these insns can cause incorrect output
13319 if it is for the same register as the original i3 dest.
13320 In that case, we will notice that the register is set in i3,
13321 and then add a REG_UNUSED note for the destination of i3, which
13322 is wrong. However, it is possible to have REG_UNUSED notes from
13323 i2 or i1 for register which were both used and clobbered, so
13324 we keep notes from i2 or i1 if they will turn into REG_DEAD
13325 notes. */
13327 /* If this register is set or clobbered in I3, put the note there
13328 unless there is one already. */
13329 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13331 if (from_insn != i3)
13332 break;
13334 if (! (REG_P (XEXP (note, 0))
13335 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13336 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13337 place = i3;
13339 /* Otherwise, if this register is used by I3, then this register
13340 now dies here, so we must put a REG_DEAD note here unless there
13341 is one already. */
13342 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13343 && ! (REG_P (XEXP (note, 0))
13344 ? find_regno_note (i3, REG_DEAD,
13345 REGNO (XEXP (note, 0)))
13346 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13348 PUT_REG_NOTE_KIND (note, REG_DEAD);
13349 place = i3;
13351 break;
13353 case REG_EQUAL:
13354 case REG_EQUIV:
13355 case REG_NOALIAS:
13356 /* These notes say something about results of an insn. We can
13357 only support them if they used to be on I3 in which case they
13358 remain on I3. Otherwise they are ignored.
13360 If the note refers to an expression that is not a constant, we
13361 must also ignore the note since we cannot tell whether the
13362 equivalence is still true. It might be possible to do
13363 slightly better than this (we only have a problem if I2DEST
13364 or I1DEST is present in the expression), but it doesn't
13365 seem worth the trouble. */
13367 if (from_insn == i3
13368 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13369 place = i3;
13370 break;
13372 case REG_INC:
13373 /* These notes say something about how a register is used. They must
13374 be present on any use of the register in I2 or I3. */
13375 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13376 place = i3;
13378 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13380 if (place)
13381 place2 = i2;
13382 else
13383 place = i2;
13385 break;
13387 case REG_LABEL_TARGET:
13388 case REG_LABEL_OPERAND:
13389 /* This can show up in several ways -- either directly in the
13390 pattern, or hidden off in the constant pool with (or without?)
13391 a REG_EQUAL note. */
13392 /* ??? Ignore the without-reg_equal-note problem for now. */
13393 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13394 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13395 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13396 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13397 place = i3;
13399 if (i2
13400 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13401 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13402 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13403 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13405 if (place)
13406 place2 = i2;
13407 else
13408 place = i2;
13411 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13412 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13413 there. */
13414 if (place && JUMP_P (place)
13415 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13416 && (JUMP_LABEL (place) == NULL
13417 || JUMP_LABEL (place) == XEXP (note, 0)))
13419 rtx label = JUMP_LABEL (place);
13421 if (!label)
13422 JUMP_LABEL (place) = XEXP (note, 0);
13423 else if (LABEL_P (label))
13424 LABEL_NUSES (label)--;
13427 if (place2 && JUMP_P (place2)
13428 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13429 && (JUMP_LABEL (place2) == NULL
13430 || JUMP_LABEL (place2) == XEXP (note, 0)))
13432 rtx label = JUMP_LABEL (place2);
13434 if (!label)
13435 JUMP_LABEL (place2) = XEXP (note, 0);
13436 else if (LABEL_P (label))
13437 LABEL_NUSES (label)--;
13438 place2 = 0;
13440 break;
13442 case REG_NONNEG:
13443 /* This note says something about the value of a register prior
13444 to the execution of an insn. It is too much trouble to see
13445 if the note is still correct in all situations. It is better
13446 to simply delete it. */
13447 break;
13449 case REG_DEAD:
13450 /* If we replaced the right hand side of FROM_INSN with a
13451 REG_EQUAL note, the original use of the dying register
13452 will not have been combined into I3 and I2. In such cases,
13453 FROM_INSN is guaranteed to be the first of the combined
13454 instructions, so we simply need to search back before
13455 FROM_INSN for the previous use or set of this register,
13456 then alter the notes there appropriately.
13458 If the register is used as an input in I3, it dies there.
13459 Similarly for I2, if it is nonzero and adjacent to I3.
13461 If the register is not used as an input in either I3 or I2
13462 and it is not one of the registers we were supposed to eliminate,
13463 there are two possibilities. We might have a non-adjacent I2
13464 or we might have somehow eliminated an additional register
13465 from a computation. For example, we might have had A & B where
13466 we discover that B will always be zero. In this case we will
13467 eliminate the reference to A.
13469 In both cases, we must search to see if we can find a previous
13470 use of A and put the death note there. */
13472 if (from_insn
13473 && from_insn == i2mod
13474 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13475 tem = from_insn;
13476 else
13478 if (from_insn
13479 && CALL_P (from_insn)
13480 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13481 place = from_insn;
13482 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13483 place = i3;
13484 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13485 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13486 place = i2;
13487 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13488 && !(i2mod
13489 && reg_overlap_mentioned_p (XEXP (note, 0),
13490 i2mod_old_rhs)))
13491 || rtx_equal_p (XEXP (note, 0), elim_i1)
13492 || rtx_equal_p (XEXP (note, 0), elim_i0))
13493 break;
13494 tem = i3;
13497 if (place == 0)
13499 basic_block bb = this_basic_block;
13501 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13503 if (!NONDEBUG_INSN_P (tem))
13505 if (tem == BB_HEAD (bb))
13506 break;
13507 continue;
13510 /* If the register is being set at TEM, see if that is all
13511 TEM is doing. If so, delete TEM. Otherwise, make this
13512 into a REG_UNUSED note instead. Don't delete sets to
13513 global register vars. */
13514 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13515 || !global_regs[REGNO (XEXP (note, 0))])
13516 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13518 rtx set = single_set (tem);
13519 rtx inner_dest = 0;
13520 #ifdef HAVE_cc0
13521 rtx cc0_setter = NULL_RTX;
13522 #endif
13524 if (set != 0)
13525 for (inner_dest = SET_DEST (set);
13526 (GET_CODE (inner_dest) == STRICT_LOW_PART
13527 || GET_CODE (inner_dest) == SUBREG
13528 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13529 inner_dest = XEXP (inner_dest, 0))
13532 /* Verify that it was the set, and not a clobber that
13533 modified the register.
13535 CC0 targets must be careful to maintain setter/user
13536 pairs. If we cannot delete the setter due to side
13537 effects, mark the user with an UNUSED note instead
13538 of deleting it. */
13540 if (set != 0 && ! side_effects_p (SET_SRC (set))
13541 && rtx_equal_p (XEXP (note, 0), inner_dest)
13542 #ifdef HAVE_cc0
13543 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13544 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13545 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13546 #endif
13549 /* Move the notes and links of TEM elsewhere.
13550 This might delete other dead insns recursively.
13551 First set the pattern to something that won't use
13552 any register. */
13553 rtx old_notes = REG_NOTES (tem);
13555 PATTERN (tem) = pc_rtx;
13556 REG_NOTES (tem) = NULL;
13558 distribute_notes (old_notes, tem, tem, NULL_RTX,
13559 NULL_RTX, NULL_RTX, NULL_RTX);
13560 distribute_links (LOG_LINKS (tem));
13562 SET_INSN_DELETED (tem);
13563 if (tem == i2)
13564 i2 = NULL_RTX;
13566 #ifdef HAVE_cc0
13567 /* Delete the setter too. */
13568 if (cc0_setter)
13570 PATTERN (cc0_setter) = pc_rtx;
13571 old_notes = REG_NOTES (cc0_setter);
13572 REG_NOTES (cc0_setter) = NULL;
13574 distribute_notes (old_notes, cc0_setter,
13575 cc0_setter, NULL_RTX,
13576 NULL_RTX, NULL_RTX, NULL_RTX);
13577 distribute_links (LOG_LINKS (cc0_setter));
13579 SET_INSN_DELETED (cc0_setter);
13580 if (cc0_setter == i2)
13581 i2 = NULL_RTX;
13583 #endif
13585 else
13587 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13589 /* If there isn't already a REG_UNUSED note, put one
13590 here. Do not place a REG_DEAD note, even if
13591 the register is also used here; that would not
13592 match the algorithm used in lifetime analysis
13593 and can cause the consistency check in the
13594 scheduler to fail. */
13595 if (! find_regno_note (tem, REG_UNUSED,
13596 REGNO (XEXP (note, 0))))
13597 place = tem;
13598 break;
13601 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13602 || (CALL_P (tem)
13603 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13605 place = tem;
13607 /* If we are doing a 3->2 combination, and we have a
13608 register which formerly died in i3 and was not used
13609 by i2, which now no longer dies in i3 and is used in
13610 i2 but does not die in i2, and place is between i2
13611 and i3, then we may need to move a link from place to
13612 i2. */
13613 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13614 && from_insn
13615 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13616 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13618 struct insn_link *links = LOG_LINKS (place);
13619 LOG_LINKS (place) = NULL;
13620 distribute_links (links);
13622 break;
13625 if (tem == BB_HEAD (bb))
13626 break;
13631 /* If the register is set or already dead at PLACE, we needn't do
13632 anything with this note if it is still a REG_DEAD note.
13633 We check here if it is set at all, not if is it totally replaced,
13634 which is what `dead_or_set_p' checks, so also check for it being
13635 set partially. */
13637 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13639 unsigned int regno = REGNO (XEXP (note, 0));
13640 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13642 if (dead_or_set_p (place, XEXP (note, 0))
13643 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13645 /* Unless the register previously died in PLACE, clear
13646 last_death. [I no longer understand why this is
13647 being done.] */
13648 if (rsp->last_death != place)
13649 rsp->last_death = 0;
13650 place = 0;
13652 else
13653 rsp->last_death = place;
13655 /* If this is a death note for a hard reg that is occupying
13656 multiple registers, ensure that we are still using all
13657 parts of the object. If we find a piece of the object
13658 that is unused, we must arrange for an appropriate REG_DEAD
13659 note to be added for it. However, we can't just emit a USE
13660 and tag the note to it, since the register might actually
13661 be dead; so we recourse, and the recursive call then finds
13662 the previous insn that used this register. */
13664 if (place && regno < FIRST_PSEUDO_REGISTER
13665 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13667 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13668 int all_used = 1;
13669 unsigned int i;
13671 for (i = regno; i < endregno; i++)
13672 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13673 && ! find_regno_fusage (place, USE, i))
13674 || dead_or_set_regno_p (place, i))
13675 all_used = 0;
13677 if (! all_used)
13679 /* Put only REG_DEAD notes for pieces that are
13680 not already dead or set. */
13682 for (i = regno; i < endregno;
13683 i += hard_regno_nregs[i][reg_raw_mode[i]])
13685 rtx piece = regno_reg_rtx[i];
13686 basic_block bb = this_basic_block;
13688 if (! dead_or_set_p (place, piece)
13689 && ! reg_bitfield_target_p (piece,
13690 PATTERN (place)))
13692 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13693 NULL_RTX);
13695 distribute_notes (new_note, place, place,
13696 NULL_RTX, NULL_RTX, NULL_RTX,
13697 NULL_RTX);
13699 else if (! refers_to_regno_p (i, i + 1,
13700 PATTERN (place), 0)
13701 && ! find_regno_fusage (place, USE, i))
13702 for (tem = PREV_INSN (place); ;
13703 tem = PREV_INSN (tem))
13705 if (!NONDEBUG_INSN_P (tem))
13707 if (tem == BB_HEAD (bb))
13708 break;
13709 continue;
13711 if (dead_or_set_p (tem, piece)
13712 || reg_bitfield_target_p (piece,
13713 PATTERN (tem)))
13715 add_reg_note (tem, REG_UNUSED, piece);
13716 break;
13722 place = 0;
13726 break;
13728 default:
13729 /* Any other notes should not be present at this point in the
13730 compilation. */
13731 gcc_unreachable ();
13734 if (place)
13736 XEXP (note, 1) = REG_NOTES (place);
13737 REG_NOTES (place) = note;
13740 if (place2)
13741 add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13745 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13746 I3, I2, and I1 to new locations. This is also called to add a link
13747 pointing at I3 when I3's destination is changed. */
13749 static void
13750 distribute_links (struct insn_link *links)
13752 struct insn_link *link, *next_link;
13754 for (link = links; link; link = next_link)
13756 rtx place = 0;
13757 rtx insn;
13758 rtx set, reg;
13760 next_link = link->next;
13762 /* If the insn that this link points to is a NOTE or isn't a single
13763 set, ignore it. In the latter case, it isn't clear what we
13764 can do other than ignore the link, since we can't tell which
13765 register it was for. Such links wouldn't be used by combine
13766 anyway.
13768 It is not possible for the destination of the target of the link to
13769 have been changed by combine. The only potential of this is if we
13770 replace I3, I2, and I1 by I3 and I2. But in that case the
13771 destination of I2 also remains unchanged. */
13773 if (NOTE_P (link->insn)
13774 || (set = single_set (link->insn)) == 0)
13775 continue;
13777 reg = SET_DEST (set);
13778 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13779 || GET_CODE (reg) == STRICT_LOW_PART)
13780 reg = XEXP (reg, 0);
13782 /* A LOG_LINK is defined as being placed on the first insn that uses
13783 a register and points to the insn that sets the register. Start
13784 searching at the next insn after the target of the link and stop
13785 when we reach a set of the register or the end of the basic block.
13787 Note that this correctly handles the link that used to point from
13788 I3 to I2. Also note that not much searching is typically done here
13789 since most links don't point very far away. */
13791 for (insn = NEXT_INSN (link->insn);
13792 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13793 || BB_HEAD (this_basic_block->next_bb) != insn));
13794 insn = NEXT_INSN (insn))
13795 if (DEBUG_INSN_P (insn))
13796 continue;
13797 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13799 if (reg_referenced_p (reg, PATTERN (insn)))
13800 place = insn;
13801 break;
13803 else if (CALL_P (insn)
13804 && find_reg_fusage (insn, USE, reg))
13806 place = insn;
13807 break;
13809 else if (INSN_P (insn) && reg_set_p (reg, insn))
13810 break;
13812 /* If we found a place to put the link, place it there unless there
13813 is already a link to the same insn as LINK at that point. */
13815 if (place)
13817 struct insn_link *link2;
13819 FOR_EACH_LOG_LINK (link2, place)
13820 if (link2->insn == link->insn)
13821 break;
13823 if (link2 == NULL)
13825 link->next = LOG_LINKS (place);
13826 LOG_LINKS (place) = link;
13828 /* Set added_links_insn to the earliest insn we added a
13829 link to. */
13830 if (added_links_insn == 0
13831 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13832 added_links_insn = place;
13838 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13839 Check whether the expression pointer to by LOC is a register or
13840 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13841 Otherwise return zero. */
13843 static int
13844 unmentioned_reg_p_1 (rtx *loc, void *expr)
13846 rtx x = *loc;
13848 if (x != NULL_RTX
13849 && (REG_P (x) || MEM_P (x))
13850 && ! reg_mentioned_p (x, (rtx) expr))
13851 return 1;
13852 return 0;
13855 /* Check for any register or memory mentioned in EQUIV that is not
13856 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13857 of EXPR where some registers may have been replaced by constants. */
13859 static bool
13860 unmentioned_reg_p (rtx equiv, rtx expr)
13862 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13865 void
13866 dump_combine_stats (FILE *file)
13868 fprintf
13869 (file,
13870 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13871 combine_attempts, combine_merges, combine_extras, combine_successes);
13874 void
13875 dump_combine_total_stats (FILE *file)
13877 fprintf
13878 (file,
13879 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13880 total_attempts, total_merges, total_extras, total_successes);
13883 static bool
13884 gate_handle_combine (void)
13886 return (optimize > 0);
13889 /* Try combining insns through substitution. */
13890 static unsigned int
13891 rest_of_handle_combine (void)
13893 int rebuild_jump_labels_after_combine;
13895 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13896 df_note_add_problem ();
13897 df_analyze ();
13899 regstat_init_n_sets_and_refs ();
13901 rebuild_jump_labels_after_combine
13902 = combine_instructions (get_insns (), max_reg_num ());
13904 /* Combining insns may have turned an indirect jump into a
13905 direct jump. Rebuild the JUMP_LABEL fields of jumping
13906 instructions. */
13907 if (rebuild_jump_labels_after_combine)
13909 timevar_push (TV_JUMP);
13910 rebuild_jump_labels (get_insns ());
13911 cleanup_cfg (0);
13912 timevar_pop (TV_JUMP);
13915 regstat_free_n_sets_and_refs ();
13916 return 0;
13919 struct rtl_opt_pass pass_combine =
13922 RTL_PASS,
13923 "combine", /* name */
13924 gate_handle_combine, /* gate */
13925 rest_of_handle_combine, /* execute */
13926 NULL, /* sub */
13927 NULL, /* next */
13928 0, /* static_pass_number */
13929 TV_COMBINE, /* tv_id */
13930 PROP_cfglayout, /* properties_required */
13931 0, /* properties_provided */
13932 0, /* properties_destroyed */
13933 0, /* todo_flags_start */
13934 TODO_dump_func |
13935 TODO_df_finish | TODO_verify_rtl_sharing |
13936 TODO_ggc_collect, /* todo_flags_finish */