* inclhack.def (AAB_aix_fcntl): New fix.
[official-gcc.git] / gcc / combine.c
blob4e0a57963dc4b6f6e0b34da1aa4c4ba56af5a3c0
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "diagnostic-core.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 #include "params.h"
101 #include "tree-pass.h"
102 #include "df.h"
103 #include "valtrack.h"
104 #include "cgraph.h"
105 #include "obstack.h"
107 /* Number of attempts to combine instructions in this function. */
109 static int combine_attempts;
111 /* Number of attempts that got as far as substitution in this function. */
113 static int combine_merges;
115 /* Number of instructions combined with added SETs in this function. */
117 static int combine_extras;
119 /* Number of instructions combined in this function. */
121 static int combine_successes;
123 /* Totals over entire compilation. */
125 static int total_attempts, total_merges, total_extras, total_successes;
127 /* combine_instructions may try to replace the right hand side of the
128 second instruction with the value of an associated REG_EQUAL note
129 before throwing it at try_combine. That is problematic when there
130 is a REG_DEAD note for a register used in the old right hand side
131 and can cause distribute_notes to do wrong things. This is the
132 second instruction if it has been so modified, null otherwise. */
134 static rtx i2mod;
136 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
138 static rtx i2mod_old_rhs;
140 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
142 static rtx i2mod_new_rhs;
144 typedef struct reg_stat_struct {
145 /* Record last point of death of (hard or pseudo) register n. */
146 rtx last_death;
148 /* Record last point of modification of (hard or pseudo) register n. */
149 rtx last_set;
151 /* The next group of fields allows the recording of the last value assigned
152 to (hard or pseudo) register n. We use this information to see if an
153 operation being processed is redundant given a prior operation performed
154 on the register. For example, an `and' with a constant is redundant if
155 all the zero bits are already known to be turned off.
157 We use an approach similar to that used by cse, but change it in the
158 following ways:
160 (1) We do not want to reinitialize at each label.
161 (2) It is useful, but not critical, to know the actual value assigned
162 to a register. Often just its form is helpful.
164 Therefore, we maintain the following fields:
166 last_set_value the last value assigned
167 last_set_label records the value of label_tick when the
168 register was assigned
169 last_set_table_tick records the value of label_tick when a
170 value using the register is assigned
171 last_set_invalid set to nonzero when it is not valid
172 to use the value of this register in some
173 register's value
175 To understand the usage of these tables, it is important to understand
176 the distinction between the value in last_set_value being valid and
177 the register being validly contained in some other expression in the
178 table.
180 (The next two parameters are out of date).
182 reg_stat[i].last_set_value is valid if it is nonzero, and either
183 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
185 Register I may validly appear in any expression returned for the value
186 of another register if reg_n_sets[i] is 1. It may also appear in the
187 value for register J if reg_stat[j].last_set_invalid is zero, or
188 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
190 If an expression is found in the table containing a register which may
191 not validly appear in an expression, the register is replaced by
192 something that won't match, (clobber (const_int 0)). */
194 /* Record last value assigned to (hard or pseudo) register n. */
196 rtx last_set_value;
198 /* Record the value of label_tick when an expression involving register n
199 is placed in last_set_value. */
201 int last_set_table_tick;
203 /* Record the value of label_tick when the value for register n is placed in
204 last_set_value. */
206 int last_set_label;
208 /* These fields are maintained in parallel with last_set_value and are
209 used to store the mode in which the register was last set, the bits
210 that were known to be zero when it was last set, and the number of
211 sign bits copies it was known to have when it was last set. */
213 unsigned HOST_WIDE_INT last_set_nonzero_bits;
214 char last_set_sign_bit_copies;
215 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
217 /* Set nonzero if references to register n in expressions should not be
218 used. last_set_invalid is set nonzero when this register is being
219 assigned to and last_set_table_tick == label_tick. */
221 char last_set_invalid;
223 /* Some registers that are set more than once and used in more than one
224 basic block are nevertheless always set in similar ways. For example,
225 a QImode register may be loaded from memory in two places on a machine
226 where byte loads zero extend.
228 We record in the following fields if a register has some leading bits
229 that are always equal to the sign bit, and what we know about the
230 nonzero bits of a register, specifically which bits are known to be
231 zero.
233 If an entry is zero, it means that we don't know anything special. */
235 unsigned char sign_bit_copies;
237 unsigned HOST_WIDE_INT nonzero_bits;
239 /* Record the value of the label_tick when the last truncation
240 happened. The field truncated_to_mode is only valid if
241 truncation_label == label_tick. */
243 int truncation_label;
245 /* Record the last truncation seen for this register. If truncation
246 is not a nop to this mode we might be able to save an explicit
247 truncation if we know that value already contains a truncated
248 value. */
250 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
251 } reg_stat_type;
253 DEF_VEC_O(reg_stat_type);
254 DEF_VEC_ALLOC_O(reg_stat_type,heap);
256 static VEC(reg_stat_type,heap) *reg_stat;
258 /* Record the luid of the last insn that invalidated memory
259 (anything that writes memory, and subroutine calls, but not pushes). */
261 static int mem_last_set;
263 /* Record the luid of the last CALL_INSN
264 so we can tell whether a potential combination crosses any calls. */
266 static int last_call_luid;
268 /* When `subst' is called, this is the insn that is being modified
269 (by combining in a previous insn). The PATTERN of this insn
270 is still the old pattern partially modified and it should not be
271 looked at, but this may be used to examine the successors of the insn
272 to judge whether a simplification is valid. */
274 static rtx subst_insn;
276 /* This is the lowest LUID that `subst' is currently dealing with.
277 get_last_value will not return a value if the register was set at or
278 after this LUID. If not for this mechanism, we could get confused if
279 I2 or I1 in try_combine were an insn that used the old value of a register
280 to obtain a new value. In that case, we might erroneously get the
281 new value of the register when we wanted the old one. */
283 static int subst_low_luid;
285 /* This contains any hard registers that are used in newpat; reg_dead_at_p
286 must consider all these registers to be always live. */
288 static HARD_REG_SET newpat_used_regs;
290 /* This is an insn to which a LOG_LINKS entry has been added. If this
291 insn is the earlier than I2 or I3, combine should rescan starting at
292 that location. */
294 static rtx added_links_insn;
296 /* Basic block in which we are performing combines. */
297 static basic_block this_basic_block;
298 static bool optimize_this_for_speed_p;
301 /* Length of the currently allocated uid_insn_cost array. */
303 static int max_uid_known;
305 /* The following array records the insn_rtx_cost for every insn
306 in the instruction stream. */
308 static int *uid_insn_cost;
310 /* The following array records the LOG_LINKS for every insn in the
311 instruction stream as struct insn_link pointers. */
313 struct insn_link {
314 rtx insn;
315 struct insn_link *next;
318 static struct insn_link **uid_log_links;
320 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
321 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
323 #define FOR_EACH_LOG_LINK(L, INSN) \
324 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
326 /* Links for LOG_LINKS are allocated from this obstack. */
328 static struct obstack insn_link_obstack;
330 /* Allocate a link. */
332 static inline struct insn_link *
333 alloc_insn_link (rtx insn, struct insn_link *next)
335 struct insn_link *l
336 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
337 sizeof (struct insn_link));
338 l->insn = insn;
339 l->next = next;
340 return l;
343 /* Incremented for each basic block. */
345 static int label_tick;
347 /* Reset to label_tick for each extended basic block in scanning order. */
349 static int label_tick_ebb_start;
351 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
352 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
354 static enum machine_mode nonzero_bits_mode;
356 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
357 be safely used. It is zero while computing them and after combine has
358 completed. This former test prevents propagating values based on
359 previously set values, which can be incorrect if a variable is modified
360 in a loop. */
362 static int nonzero_sign_valid;
365 /* Record one modification to rtl structure
366 to be undone by storing old_contents into *where. */
368 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
370 struct undo
372 struct undo *next;
373 enum undo_kind kind;
374 union { rtx r; int i; enum machine_mode m; struct insn_link *l; } old_contents;
375 union { rtx *r; int *i; struct insn_link **l; } where;
378 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
379 num_undo says how many are currently recorded.
381 other_insn is nonzero if we have modified some other insn in the process
382 of working on subst_insn. It must be verified too. */
384 struct undobuf
386 struct undo *undos;
387 struct undo *frees;
388 rtx other_insn;
391 static struct undobuf undobuf;
393 /* Number of times the pseudo being substituted for
394 was found and replaced. */
396 static int n_occurrences;
398 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
399 enum machine_mode,
400 unsigned HOST_WIDE_INT,
401 unsigned HOST_WIDE_INT *);
402 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
403 enum machine_mode,
404 unsigned int, unsigned int *);
405 static void do_SUBST (rtx *, rtx);
406 static void do_SUBST_INT (int *, int);
407 static void init_reg_last (void);
408 static void setup_incoming_promotions (rtx);
409 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
410 static int cant_combine_insn_p (rtx);
411 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
412 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
413 static int contains_muldiv (rtx);
414 static rtx try_combine (rtx, rtx, rtx, rtx, int *, rtx);
415 static void undo_all (void);
416 static void undo_commit (void);
417 static rtx *find_split_point (rtx *, rtx, bool);
418 static rtx subst (rtx, rtx, rtx, int, int, int);
419 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
420 static rtx simplify_if_then_else (rtx);
421 static rtx simplify_set (rtx);
422 static rtx simplify_logical (rtx);
423 static rtx expand_compound_operation (rtx);
424 static const_rtx expand_field_assignment (const_rtx);
425 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
426 rtx, unsigned HOST_WIDE_INT, int, int, int);
427 static rtx extract_left_shift (rtx, int);
428 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
429 unsigned HOST_WIDE_INT *);
430 static rtx canon_reg_for_combine (rtx, rtx);
431 static rtx force_to_mode (rtx, enum machine_mode,
432 unsigned HOST_WIDE_INT, int);
433 static rtx if_then_else_cond (rtx, rtx *, rtx *);
434 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
435 static int rtx_equal_for_field_assignment_p (rtx, rtx);
436 static rtx make_field_assignment (rtx);
437 static rtx apply_distributive_law (rtx);
438 static rtx distribute_and_simplify_rtx (rtx, int);
439 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
440 unsigned HOST_WIDE_INT);
441 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
442 unsigned HOST_WIDE_INT);
443 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
444 HOST_WIDE_INT, enum machine_mode, int *);
445 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
446 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
447 int);
448 static int recog_for_combine (rtx *, rtx, rtx *);
449 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
450 static enum rtx_code simplify_compare_const (enum rtx_code, rtx, rtx *);
451 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
452 static void update_table_tick (rtx);
453 static void record_value_for_reg (rtx, rtx, rtx);
454 static void check_promoted_subreg (rtx, rtx);
455 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
456 static void record_dead_and_set_regs (rtx);
457 static int get_last_value_validate (rtx *, rtx, int, int);
458 static rtx get_last_value (const_rtx);
459 static int use_crosses_set_p (const_rtx, int);
460 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
461 static int reg_dead_at_p (rtx, rtx);
462 static void move_deaths (rtx, rtx, int, rtx, rtx *);
463 static int reg_bitfield_target_p (rtx, rtx);
464 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
465 static void distribute_links (struct insn_link *);
466 static void mark_used_regs_combine (rtx);
467 static void record_promoted_value (rtx, rtx);
468 static int unmentioned_reg_p_1 (rtx *, void *);
469 static bool unmentioned_reg_p (rtx, rtx);
470 static int record_truncated_value (rtx *, void *);
471 static void record_truncated_values (rtx *, void *);
472 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
473 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
476 /* It is not safe to use ordinary gen_lowpart in combine.
477 See comments in gen_lowpart_for_combine. */
478 #undef RTL_HOOKS_GEN_LOWPART
479 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
481 /* Our implementation of gen_lowpart never emits a new pseudo. */
482 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
483 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
485 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
486 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
488 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
489 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
491 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
492 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
494 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
497 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
498 PATTERN can not be split. Otherwise, it returns an insn sequence.
499 This is a wrapper around split_insns which ensures that the
500 reg_stat vector is made larger if the splitter creates a new
501 register. */
503 static rtx
504 combine_split_insns (rtx pattern, rtx insn)
506 rtx ret;
507 unsigned int nregs;
509 ret = split_insns (pattern, insn);
510 nregs = max_reg_num ();
511 if (nregs > VEC_length (reg_stat_type, reg_stat))
512 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
513 return ret;
516 /* This is used by find_single_use to locate an rtx in LOC that
517 contains exactly one use of DEST, which is typically either a REG
518 or CC0. It returns a pointer to the innermost rtx expression
519 containing DEST. Appearances of DEST that are being used to
520 totally replace it are not counted. */
522 static rtx *
523 find_single_use_1 (rtx dest, rtx *loc)
525 rtx x = *loc;
526 enum rtx_code code = GET_CODE (x);
527 rtx *result = NULL;
528 rtx *this_result;
529 int i;
530 const char *fmt;
532 switch (code)
534 case CONST:
535 case LABEL_REF:
536 case SYMBOL_REF:
537 CASE_CONST_ANY:
538 case CLOBBER:
539 return 0;
541 case SET:
542 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
543 of a REG that occupies all of the REG, the insn uses DEST if
544 it is mentioned in the destination or the source. Otherwise, we
545 need just check the source. */
546 if (GET_CODE (SET_DEST (x)) != CC0
547 && GET_CODE (SET_DEST (x)) != PC
548 && !REG_P (SET_DEST (x))
549 && ! (GET_CODE (SET_DEST (x)) == SUBREG
550 && REG_P (SUBREG_REG (SET_DEST (x)))
551 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
552 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
553 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
554 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
555 break;
557 return find_single_use_1 (dest, &SET_SRC (x));
559 case MEM:
560 case SUBREG:
561 return find_single_use_1 (dest, &XEXP (x, 0));
563 default:
564 break;
567 /* If it wasn't one of the common cases above, check each expression and
568 vector of this code. Look for a unique usage of DEST. */
570 fmt = GET_RTX_FORMAT (code);
571 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
573 if (fmt[i] == 'e')
575 if (dest == XEXP (x, i)
576 || (REG_P (dest) && REG_P (XEXP (x, i))
577 && REGNO (dest) == REGNO (XEXP (x, i))))
578 this_result = loc;
579 else
580 this_result = find_single_use_1 (dest, &XEXP (x, i));
582 if (result == NULL)
583 result = this_result;
584 else if (this_result)
585 /* Duplicate usage. */
586 return NULL;
588 else if (fmt[i] == 'E')
590 int j;
592 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
594 if (XVECEXP (x, i, j) == dest
595 || (REG_P (dest)
596 && REG_P (XVECEXP (x, i, j))
597 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
598 this_result = loc;
599 else
600 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
602 if (result == NULL)
603 result = this_result;
604 else if (this_result)
605 return NULL;
610 return result;
614 /* See if DEST, produced in INSN, is used only a single time in the
615 sequel. If so, return a pointer to the innermost rtx expression in which
616 it is used.
618 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
620 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
621 care about REG_DEAD notes or LOG_LINKS.
623 Otherwise, we find the single use by finding an insn that has a
624 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
625 only referenced once in that insn, we know that it must be the first
626 and last insn referencing DEST. */
628 static rtx *
629 find_single_use (rtx dest, rtx insn, rtx *ploc)
631 basic_block bb;
632 rtx next;
633 rtx *result;
634 struct insn_link *link;
636 #ifdef HAVE_cc0
637 if (dest == cc0_rtx)
639 next = NEXT_INSN (insn);
640 if (next == 0
641 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
642 return 0;
644 result = find_single_use_1 (dest, &PATTERN (next));
645 if (result && ploc)
646 *ploc = next;
647 return result;
649 #endif
651 if (!REG_P (dest))
652 return 0;
654 bb = BLOCK_FOR_INSN (insn);
655 for (next = NEXT_INSN (insn);
656 next && BLOCK_FOR_INSN (next) == bb;
657 next = NEXT_INSN (next))
658 if (INSN_P (next) && dead_or_set_p (next, dest))
660 FOR_EACH_LOG_LINK (link, next)
661 if (link->insn == insn)
662 break;
664 if (link)
666 result = find_single_use_1 (dest, &PATTERN (next));
667 if (ploc)
668 *ploc = next;
669 return result;
673 return 0;
676 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
677 insn. The substitution can be undone by undo_all. If INTO is already
678 set to NEWVAL, do not record this change. Because computing NEWVAL might
679 also call SUBST, we have to compute it before we put anything into
680 the undo table. */
682 static void
683 do_SUBST (rtx *into, rtx newval)
685 struct undo *buf;
686 rtx oldval = *into;
688 if (oldval == newval)
689 return;
691 /* We'd like to catch as many invalid transformations here as
692 possible. Unfortunately, there are way too many mode changes
693 that are perfectly valid, so we'd waste too much effort for
694 little gain doing the checks here. Focus on catching invalid
695 transformations involving integer constants. */
696 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
697 && CONST_INT_P (newval))
699 /* Sanity check that we're replacing oldval with a CONST_INT
700 that is a valid sign-extension for the original mode. */
701 gcc_assert (INTVAL (newval)
702 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
704 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
705 CONST_INT is not valid, because after the replacement, the
706 original mode would be gone. Unfortunately, we can't tell
707 when do_SUBST is called to replace the operand thereof, so we
708 perform this test on oldval instead, checking whether an
709 invalid replacement took place before we got here. */
710 gcc_assert (!(GET_CODE (oldval) == SUBREG
711 && CONST_INT_P (SUBREG_REG (oldval))));
712 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
713 && CONST_INT_P (XEXP (oldval, 0))));
716 if (undobuf.frees)
717 buf = undobuf.frees, undobuf.frees = buf->next;
718 else
719 buf = XNEW (struct undo);
721 buf->kind = UNDO_RTX;
722 buf->where.r = into;
723 buf->old_contents.r = oldval;
724 *into = newval;
726 buf->next = undobuf.undos, undobuf.undos = buf;
729 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
731 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
732 for the value of a HOST_WIDE_INT value (including CONST_INT) is
733 not safe. */
735 static void
736 do_SUBST_INT (int *into, int newval)
738 struct undo *buf;
739 int oldval = *into;
741 if (oldval == newval)
742 return;
744 if (undobuf.frees)
745 buf = undobuf.frees, undobuf.frees = buf->next;
746 else
747 buf = XNEW (struct undo);
749 buf->kind = UNDO_INT;
750 buf->where.i = into;
751 buf->old_contents.i = oldval;
752 *into = newval;
754 buf->next = undobuf.undos, undobuf.undos = buf;
757 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
759 /* Similar to SUBST, but just substitute the mode. This is used when
760 changing the mode of a pseudo-register, so that any other
761 references to the entry in the regno_reg_rtx array will change as
762 well. */
764 static void
765 do_SUBST_MODE (rtx *into, enum machine_mode newval)
767 struct undo *buf;
768 enum machine_mode oldval = GET_MODE (*into);
770 if (oldval == newval)
771 return;
773 if (undobuf.frees)
774 buf = undobuf.frees, undobuf.frees = buf->next;
775 else
776 buf = XNEW (struct undo);
778 buf->kind = UNDO_MODE;
779 buf->where.r = into;
780 buf->old_contents.m = oldval;
781 adjust_reg_mode (*into, newval);
783 buf->next = undobuf.undos, undobuf.undos = buf;
786 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
788 #ifndef HAVE_cc0
789 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
791 static void
792 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
794 struct undo *buf;
795 struct insn_link * oldval = *into;
797 if (oldval == newval)
798 return;
800 if (undobuf.frees)
801 buf = undobuf.frees, undobuf.frees = buf->next;
802 else
803 buf = XNEW (struct undo);
805 buf->kind = UNDO_LINKS;
806 buf->where.l = into;
807 buf->old_contents.l = oldval;
808 *into = newval;
810 buf->next = undobuf.undos, undobuf.undos = buf;
813 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
814 #endif
816 /* Subroutine of try_combine. Determine whether the replacement patterns
817 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
818 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
819 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
820 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
821 of all the instructions can be estimated and the replacements are more
822 expensive than the original sequence. */
824 static bool
825 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
826 rtx newi2pat, rtx newotherpat)
828 int i0_cost, i1_cost, i2_cost, i3_cost;
829 int new_i2_cost, new_i3_cost;
830 int old_cost, new_cost;
832 /* Lookup the original insn_rtx_costs. */
833 i2_cost = INSN_COST (i2);
834 i3_cost = INSN_COST (i3);
836 if (i1)
838 i1_cost = INSN_COST (i1);
839 if (i0)
841 i0_cost = INSN_COST (i0);
842 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
843 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
845 else
847 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
848 ? i1_cost + i2_cost + i3_cost : 0);
849 i0_cost = 0;
852 else
854 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
855 i1_cost = i0_cost = 0;
858 /* Calculate the replacement insn_rtx_costs. */
859 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
860 if (newi2pat)
862 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
863 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
864 ? new_i2_cost + new_i3_cost : 0;
866 else
868 new_cost = new_i3_cost;
869 new_i2_cost = 0;
872 if (undobuf.other_insn)
874 int old_other_cost, new_other_cost;
876 old_other_cost = INSN_COST (undobuf.other_insn);
877 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
878 if (old_other_cost > 0 && new_other_cost > 0)
880 old_cost += old_other_cost;
881 new_cost += new_other_cost;
883 else
884 old_cost = 0;
887 /* Disallow this combination if both new_cost and old_cost are greater than
888 zero, and new_cost is greater than old cost. */
889 if (old_cost > 0 && new_cost > old_cost)
891 if (dump_file)
893 if (i0)
895 fprintf (dump_file,
896 "rejecting combination of insns %d, %d, %d and %d\n",
897 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
898 INSN_UID (i3));
899 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
900 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
902 else if (i1)
904 fprintf (dump_file,
905 "rejecting combination of insns %d, %d and %d\n",
906 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
907 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
908 i1_cost, i2_cost, i3_cost, old_cost);
910 else
912 fprintf (dump_file,
913 "rejecting combination of insns %d and %d\n",
914 INSN_UID (i2), INSN_UID (i3));
915 fprintf (dump_file, "original costs %d + %d = %d\n",
916 i2_cost, i3_cost, old_cost);
919 if (newi2pat)
921 fprintf (dump_file, "replacement costs %d + %d = %d\n",
922 new_i2_cost, new_i3_cost, new_cost);
924 else
925 fprintf (dump_file, "replacement cost %d\n", new_cost);
928 return false;
931 /* Update the uid_insn_cost array with the replacement costs. */
932 INSN_COST (i2) = new_i2_cost;
933 INSN_COST (i3) = new_i3_cost;
934 if (i1)
936 INSN_COST (i1) = 0;
937 if (i0)
938 INSN_COST (i0) = 0;
941 return true;
945 /* Delete any insns that copy a register to itself. */
947 static void
948 delete_noop_moves (void)
950 rtx insn, next;
951 basic_block bb;
953 FOR_EACH_BB (bb)
955 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
957 next = NEXT_INSN (insn);
958 if (INSN_P (insn) && noop_move_p (insn))
960 if (dump_file)
961 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
963 delete_insn_and_edges (insn);
970 /* Fill in log links field for all insns. */
972 static void
973 create_log_links (void)
975 basic_block bb;
976 rtx *next_use, insn;
977 df_ref *def_vec, *use_vec;
979 next_use = XCNEWVEC (rtx, max_reg_num ());
981 /* Pass through each block from the end, recording the uses of each
982 register and establishing log links when def is encountered.
983 Note that we do not clear next_use array in order to save time,
984 so we have to test whether the use is in the same basic block as def.
986 There are a few cases below when we do not consider the definition or
987 usage -- these are taken from original flow.c did. Don't ask me why it is
988 done this way; I don't know and if it works, I don't want to know. */
990 FOR_EACH_BB (bb)
992 FOR_BB_INSNS_REVERSE (bb, insn)
994 if (!NONDEBUG_INSN_P (insn))
995 continue;
997 /* Log links are created only once. */
998 gcc_assert (!LOG_LINKS (insn));
1000 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
1002 df_ref def = *def_vec;
1003 int regno = DF_REF_REGNO (def);
1004 rtx use_insn;
1006 if (!next_use[regno])
1007 continue;
1009 /* Do not consider if it is pre/post modification in MEM. */
1010 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1011 continue;
1013 /* Do not make the log link for frame pointer. */
1014 if ((regno == FRAME_POINTER_REGNUM
1015 && (! reload_completed || frame_pointer_needed))
1016 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1017 || (regno == HARD_FRAME_POINTER_REGNUM
1018 && (! reload_completed || frame_pointer_needed))
1019 #endif
1020 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1021 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1022 #endif
1024 continue;
1026 use_insn = next_use[regno];
1027 if (BLOCK_FOR_INSN (use_insn) == bb)
1029 /* flow.c claimed:
1031 We don't build a LOG_LINK for hard registers contained
1032 in ASM_OPERANDs. If these registers get replaced,
1033 we might wind up changing the semantics of the insn,
1034 even if reload can make what appear to be valid
1035 assignments later. */
1036 if (regno >= FIRST_PSEUDO_REGISTER
1037 || asm_noperands (PATTERN (use_insn)) < 0)
1039 /* Don't add duplicate links between instructions. */
1040 struct insn_link *links;
1041 FOR_EACH_LOG_LINK (links, use_insn)
1042 if (insn == links->insn)
1043 break;
1045 if (!links)
1046 LOG_LINKS (use_insn)
1047 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1050 next_use[regno] = NULL_RTX;
1053 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1055 df_ref use = *use_vec;
1056 int regno = DF_REF_REGNO (use);
1058 /* Do not consider the usage of the stack pointer
1059 by function call. */
1060 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1061 continue;
1063 next_use[regno] = insn;
1068 free (next_use);
1071 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1072 true if we found a LOG_LINK that proves that A feeds B. This only works
1073 if there are no instructions between A and B which could have a link
1074 depending on A, since in that case we would not record a link for B.
1075 We also check the implicit dependency created by a cc0 setter/user
1076 pair. */
1078 static bool
1079 insn_a_feeds_b (rtx a, rtx b)
1081 struct insn_link *links;
1082 FOR_EACH_LOG_LINK (links, b)
1083 if (links->insn == a)
1084 return true;
1085 #ifdef HAVE_cc0
1086 if (sets_cc0_p (a))
1087 return true;
1088 #endif
1089 return false;
1092 /* Main entry point for combiner. F is the first insn of the function.
1093 NREGS is the first unused pseudo-reg number.
1095 Return nonzero if the combiner has turned an indirect jump
1096 instruction into a direct jump. */
1097 static int
1098 combine_instructions (rtx f, unsigned int nregs)
1100 rtx insn, next;
1101 #ifdef HAVE_cc0
1102 rtx prev;
1103 #endif
1104 struct insn_link *links, *nextlinks;
1105 rtx first;
1106 basic_block last_bb;
1108 int new_direct_jump_p = 0;
1110 for (first = f; first && !INSN_P (first); )
1111 first = NEXT_INSN (first);
1112 if (!first)
1113 return 0;
1115 combine_attempts = 0;
1116 combine_merges = 0;
1117 combine_extras = 0;
1118 combine_successes = 0;
1120 rtl_hooks = combine_rtl_hooks;
1122 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1124 init_recog_no_volatile ();
1126 /* Allocate array for insn info. */
1127 max_uid_known = get_max_uid ();
1128 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1129 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1130 gcc_obstack_init (&insn_link_obstack);
1132 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1134 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1135 problems when, for example, we have j <<= 1 in a loop. */
1137 nonzero_sign_valid = 0;
1138 label_tick = label_tick_ebb_start = 1;
1140 /* Scan all SETs and see if we can deduce anything about what
1141 bits are known to be zero for some registers and how many copies
1142 of the sign bit are known to exist for those registers.
1144 Also set any known values so that we can use it while searching
1145 for what bits are known to be set. */
1147 setup_incoming_promotions (first);
1148 /* Allow the entry block and the first block to fall into the same EBB.
1149 Conceptually the incoming promotions are assigned to the entry block. */
1150 last_bb = ENTRY_BLOCK_PTR;
1152 create_log_links ();
1153 FOR_EACH_BB (this_basic_block)
1155 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1156 last_call_luid = 0;
1157 mem_last_set = -1;
1159 label_tick++;
1160 if (!single_pred_p (this_basic_block)
1161 || single_pred (this_basic_block) != last_bb)
1162 label_tick_ebb_start = label_tick;
1163 last_bb = this_basic_block;
1165 FOR_BB_INSNS (this_basic_block, insn)
1166 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1168 #ifdef AUTO_INC_DEC
1169 rtx links;
1170 #endif
1172 subst_low_luid = DF_INSN_LUID (insn);
1173 subst_insn = insn;
1175 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1176 insn);
1177 record_dead_and_set_regs (insn);
1179 #ifdef AUTO_INC_DEC
1180 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1181 if (REG_NOTE_KIND (links) == REG_INC)
1182 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1183 insn);
1184 #endif
1186 /* Record the current insn_rtx_cost of this instruction. */
1187 if (NONJUMP_INSN_P (insn))
1188 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1189 optimize_this_for_speed_p);
1190 if (dump_file)
1191 fprintf(dump_file, "insn_cost %d: %d\n",
1192 INSN_UID (insn), INSN_COST (insn));
1196 nonzero_sign_valid = 1;
1198 /* Now scan all the insns in forward order. */
1199 label_tick = label_tick_ebb_start = 1;
1200 init_reg_last ();
1201 setup_incoming_promotions (first);
1202 last_bb = ENTRY_BLOCK_PTR;
1204 FOR_EACH_BB (this_basic_block)
1206 rtx last_combined_insn = NULL_RTX;
1207 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1208 last_call_luid = 0;
1209 mem_last_set = -1;
1211 label_tick++;
1212 if (!single_pred_p (this_basic_block)
1213 || single_pred (this_basic_block) != last_bb)
1214 label_tick_ebb_start = label_tick;
1215 last_bb = this_basic_block;
1217 rtl_profile_for_bb (this_basic_block);
1218 for (insn = BB_HEAD (this_basic_block);
1219 insn != NEXT_INSN (BB_END (this_basic_block));
1220 insn = next ? next : NEXT_INSN (insn))
1222 next = 0;
1223 if (NONDEBUG_INSN_P (insn))
1225 while (last_combined_insn
1226 && INSN_DELETED_P (last_combined_insn))
1227 last_combined_insn = PREV_INSN (last_combined_insn);
1228 if (last_combined_insn == NULL_RTX
1229 || BARRIER_P (last_combined_insn)
1230 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1231 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1232 last_combined_insn = insn;
1234 /* See if we know about function return values before this
1235 insn based upon SUBREG flags. */
1236 check_promoted_subreg (insn, PATTERN (insn));
1238 /* See if we can find hardregs and subreg of pseudos in
1239 narrower modes. This could help turning TRUNCATEs
1240 into SUBREGs. */
1241 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1243 /* Try this insn with each insn it links back to. */
1245 FOR_EACH_LOG_LINK (links, insn)
1246 if ((next = try_combine (insn, links->insn, NULL_RTX,
1247 NULL_RTX, &new_direct_jump_p,
1248 last_combined_insn)) != 0)
1249 goto retry;
1251 /* Try each sequence of three linked insns ending with this one. */
1253 FOR_EACH_LOG_LINK (links, insn)
1255 rtx link = links->insn;
1257 /* If the linked insn has been replaced by a note, then there
1258 is no point in pursuing this chain any further. */
1259 if (NOTE_P (link))
1260 continue;
1262 FOR_EACH_LOG_LINK (nextlinks, link)
1263 if ((next = try_combine (insn, link, nextlinks->insn,
1264 NULL_RTX, &new_direct_jump_p,
1265 last_combined_insn)) != 0)
1266 goto retry;
1269 #ifdef HAVE_cc0
1270 /* Try to combine a jump insn that uses CC0
1271 with a preceding insn that sets CC0, and maybe with its
1272 logical predecessor as well.
1273 This is how we make decrement-and-branch insns.
1274 We need this special code because data flow connections
1275 via CC0 do not get entered in LOG_LINKS. */
1277 if (JUMP_P (insn)
1278 && (prev = prev_nonnote_insn (insn)) != 0
1279 && NONJUMP_INSN_P (prev)
1280 && sets_cc0_p (PATTERN (prev)))
1282 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1283 &new_direct_jump_p,
1284 last_combined_insn)) != 0)
1285 goto retry;
1287 FOR_EACH_LOG_LINK (nextlinks, prev)
1288 if ((next = try_combine (insn, prev, nextlinks->insn,
1289 NULL_RTX, &new_direct_jump_p,
1290 last_combined_insn)) != 0)
1291 goto retry;
1294 /* Do the same for an insn that explicitly references CC0. */
1295 if (NONJUMP_INSN_P (insn)
1296 && (prev = prev_nonnote_insn (insn)) != 0
1297 && NONJUMP_INSN_P (prev)
1298 && sets_cc0_p (PATTERN (prev))
1299 && GET_CODE (PATTERN (insn)) == SET
1300 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1302 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1303 &new_direct_jump_p,
1304 last_combined_insn)) != 0)
1305 goto retry;
1307 FOR_EACH_LOG_LINK (nextlinks, prev)
1308 if ((next = try_combine (insn, prev, nextlinks->insn,
1309 NULL_RTX, &new_direct_jump_p,
1310 last_combined_insn)) != 0)
1311 goto retry;
1314 /* Finally, see if any of the insns that this insn links to
1315 explicitly references CC0. If so, try this insn, that insn,
1316 and its predecessor if it sets CC0. */
1317 FOR_EACH_LOG_LINK (links, insn)
1318 if (NONJUMP_INSN_P (links->insn)
1319 && GET_CODE (PATTERN (links->insn)) == SET
1320 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1321 && (prev = prev_nonnote_insn (links->insn)) != 0
1322 && NONJUMP_INSN_P (prev)
1323 && sets_cc0_p (PATTERN (prev))
1324 && (next = try_combine (insn, links->insn,
1325 prev, NULL_RTX, &new_direct_jump_p,
1326 last_combined_insn)) != 0)
1327 goto retry;
1328 #endif
1330 /* Try combining an insn with two different insns whose results it
1331 uses. */
1332 FOR_EACH_LOG_LINK (links, insn)
1333 for (nextlinks = links->next; nextlinks;
1334 nextlinks = nextlinks->next)
1335 if ((next = try_combine (insn, links->insn,
1336 nextlinks->insn, NULL_RTX,
1337 &new_direct_jump_p,
1338 last_combined_insn)) != 0)
1339 goto retry;
1341 /* Try four-instruction combinations. */
1342 FOR_EACH_LOG_LINK (links, insn)
1344 struct insn_link *next1;
1345 rtx link = links->insn;
1347 /* If the linked insn has been replaced by a note, then there
1348 is no point in pursuing this chain any further. */
1349 if (NOTE_P (link))
1350 continue;
1352 FOR_EACH_LOG_LINK (next1, link)
1354 rtx link1 = next1->insn;
1355 if (NOTE_P (link1))
1356 continue;
1357 /* I0 -> I1 -> I2 -> I3. */
1358 FOR_EACH_LOG_LINK (nextlinks, link1)
1359 if ((next = try_combine (insn, link, link1,
1360 nextlinks->insn,
1361 &new_direct_jump_p,
1362 last_combined_insn)) != 0)
1363 goto retry;
1364 /* I0, I1 -> I2, I2 -> I3. */
1365 for (nextlinks = next1->next; nextlinks;
1366 nextlinks = nextlinks->next)
1367 if ((next = try_combine (insn, link, link1,
1368 nextlinks->insn,
1369 &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1374 for (next1 = links->next; next1; next1 = next1->next)
1376 rtx link1 = next1->insn;
1377 if (NOTE_P (link1))
1378 continue;
1379 /* I0 -> I2; I1, I2 -> I3. */
1380 FOR_EACH_LOG_LINK (nextlinks, link)
1381 if ((next = try_combine (insn, link, link1,
1382 nextlinks->insn,
1383 &new_direct_jump_p,
1384 last_combined_insn)) != 0)
1385 goto retry;
1386 /* I0 -> I1; I1, I2 -> I3. */
1387 FOR_EACH_LOG_LINK (nextlinks, link1)
1388 if ((next = try_combine (insn, link, link1,
1389 nextlinks->insn,
1390 &new_direct_jump_p,
1391 last_combined_insn)) != 0)
1392 goto retry;
1396 /* Try this insn with each REG_EQUAL note it links back to. */
1397 FOR_EACH_LOG_LINK (links, insn)
1399 rtx set, note;
1400 rtx temp = links->insn;
1401 if ((set = single_set (temp)) != 0
1402 && (note = find_reg_equal_equiv_note (temp)) != 0
1403 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1404 /* Avoid using a register that may already been marked
1405 dead by an earlier instruction. */
1406 && ! unmentioned_reg_p (note, SET_SRC (set))
1407 && (GET_MODE (note) == VOIDmode
1408 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1409 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1411 /* Temporarily replace the set's source with the
1412 contents of the REG_EQUAL note. The insn will
1413 be deleted or recognized by try_combine. */
1414 rtx orig = SET_SRC (set);
1415 SET_SRC (set) = note;
1416 i2mod = temp;
1417 i2mod_old_rhs = copy_rtx (orig);
1418 i2mod_new_rhs = copy_rtx (note);
1419 next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1420 &new_direct_jump_p,
1421 last_combined_insn);
1422 i2mod = NULL_RTX;
1423 if (next)
1424 goto retry;
1425 SET_SRC (set) = orig;
1429 if (!NOTE_P (insn))
1430 record_dead_and_set_regs (insn);
1432 retry:
1438 default_rtl_profile ();
1439 clear_bb_flags ();
1440 new_direct_jump_p |= purge_all_dead_edges ();
1441 delete_noop_moves ();
1443 /* Clean up. */
1444 obstack_free (&insn_link_obstack, NULL);
1445 free (uid_log_links);
1446 free (uid_insn_cost);
1447 VEC_free (reg_stat_type, heap, reg_stat);
1450 struct undo *undo, *next;
1451 for (undo = undobuf.frees; undo; undo = next)
1453 next = undo->next;
1454 free (undo);
1456 undobuf.frees = 0;
1459 total_attempts += combine_attempts;
1460 total_merges += combine_merges;
1461 total_extras += combine_extras;
1462 total_successes += combine_successes;
1464 nonzero_sign_valid = 0;
1465 rtl_hooks = general_rtl_hooks;
1467 /* Make recognizer allow volatile MEMs again. */
1468 init_recog ();
1470 return new_direct_jump_p;
1473 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1475 static void
1476 init_reg_last (void)
1478 unsigned int i;
1479 reg_stat_type *p;
1481 FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
1482 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1485 /* Set up any promoted values for incoming argument registers. */
1487 static void
1488 setup_incoming_promotions (rtx first)
1490 tree arg;
1491 bool strictly_local = false;
1493 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1494 arg = DECL_CHAIN (arg))
1496 rtx x, reg = DECL_INCOMING_RTL (arg);
1497 int uns1, uns3;
1498 enum machine_mode mode1, mode2, mode3, mode4;
1500 /* Only continue if the incoming argument is in a register. */
1501 if (!REG_P (reg))
1502 continue;
1504 /* Determine, if possible, whether all call sites of the current
1505 function lie within the current compilation unit. (This does
1506 take into account the exporting of a function via taking its
1507 address, and so forth.) */
1508 strictly_local = cgraph_local_info (current_function_decl)->local;
1510 /* The mode and signedness of the argument before any promotions happen
1511 (equal to the mode of the pseudo holding it at that stage). */
1512 mode1 = TYPE_MODE (TREE_TYPE (arg));
1513 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1515 /* The mode and signedness of the argument after any source language and
1516 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1517 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1518 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1520 /* The mode and signedness of the argument as it is actually passed,
1521 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1522 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1523 TREE_TYPE (cfun->decl), 0);
1525 /* The mode of the register in which the argument is being passed. */
1526 mode4 = GET_MODE (reg);
1528 /* Eliminate sign extensions in the callee when:
1529 (a) A mode promotion has occurred; */
1530 if (mode1 == mode3)
1531 continue;
1532 /* (b) The mode of the register is the same as the mode of
1533 the argument as it is passed; */
1534 if (mode3 != mode4)
1535 continue;
1536 /* (c) There's no language level extension; */
1537 if (mode1 == mode2)
1539 /* (c.1) All callers are from the current compilation unit. If that's
1540 the case we don't have to rely on an ABI, we only have to know
1541 what we're generating right now, and we know that we will do the
1542 mode1 to mode2 promotion with the given sign. */
1543 else if (!strictly_local)
1544 continue;
1545 /* (c.2) The combination of the two promotions is useful. This is
1546 true when the signs match, or if the first promotion is unsigned.
1547 In the later case, (sign_extend (zero_extend x)) is the same as
1548 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1549 else if (uns1)
1550 uns3 = true;
1551 else if (uns3)
1552 continue;
1554 /* Record that the value was promoted from mode1 to mode3,
1555 so that any sign extension at the head of the current
1556 function may be eliminated. */
1557 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1558 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1559 record_value_for_reg (reg, first, x);
1563 /* Called via note_stores. If X is a pseudo that is narrower than
1564 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1566 If we are setting only a portion of X and we can't figure out what
1567 portion, assume all bits will be used since we don't know what will
1568 be happening.
1570 Similarly, set how many bits of X are known to be copies of the sign bit
1571 at all locations in the function. This is the smallest number implied
1572 by any set of X. */
1574 static void
1575 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1577 rtx insn = (rtx) data;
1578 unsigned int num;
1580 if (REG_P (x)
1581 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1582 /* If this register is undefined at the start of the file, we can't
1583 say what its contents were. */
1584 && ! REGNO_REG_SET_P
1585 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1586 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1588 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
1590 if (set == 0 || GET_CODE (set) == CLOBBER)
1592 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1593 rsp->sign_bit_copies = 1;
1594 return;
1597 /* If this register is being initialized using itself, and the
1598 register is uninitialized in this basic block, and there are
1599 no LOG_LINKS which set the register, then part of the
1600 register is uninitialized. In that case we can't assume
1601 anything about the number of nonzero bits.
1603 ??? We could do better if we checked this in
1604 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1605 could avoid making assumptions about the insn which initially
1606 sets the register, while still using the information in other
1607 insns. We would have to be careful to check every insn
1608 involved in the combination. */
1610 if (insn
1611 && reg_referenced_p (x, PATTERN (insn))
1612 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1613 REGNO (x)))
1615 struct insn_link *link;
1617 FOR_EACH_LOG_LINK (link, insn)
1618 if (dead_or_set_p (link->insn, x))
1619 break;
1620 if (!link)
1622 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1623 rsp->sign_bit_copies = 1;
1624 return;
1628 /* If this is a complex assignment, see if we can convert it into a
1629 simple assignment. */
1630 set = expand_field_assignment (set);
1632 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1633 set what we know about X. */
1635 if (SET_DEST (set) == x
1636 || (paradoxical_subreg_p (SET_DEST (set))
1637 && SUBREG_REG (SET_DEST (set)) == x))
1639 rtx src = SET_SRC (set);
1641 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1642 /* If X is narrower than a word and SRC is a non-negative
1643 constant that would appear negative in the mode of X,
1644 sign-extend it for use in reg_stat[].nonzero_bits because some
1645 machines (maybe most) will actually do the sign-extension
1646 and this is the conservative approach.
1648 ??? For 2.5, try to tighten up the MD files in this regard
1649 instead of this kludge. */
1651 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1652 && CONST_INT_P (src)
1653 && INTVAL (src) > 0
1654 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1655 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1656 #endif
1658 /* Don't call nonzero_bits if it cannot change anything. */
1659 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1660 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1661 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1662 if (rsp->sign_bit_copies == 0
1663 || rsp->sign_bit_copies > num)
1664 rsp->sign_bit_copies = num;
1666 else
1668 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1669 rsp->sign_bit_copies = 1;
1674 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1675 optionally insns that were previously combined into I3 or that will be
1676 combined into the merger of INSN and I3. The order is PRED, PRED2,
1677 INSN, SUCC, SUCC2, I3.
1679 Return 0 if the combination is not allowed for any reason.
1681 If the combination is allowed, *PDEST will be set to the single
1682 destination of INSN and *PSRC to the single source, and this function
1683 will return 1. */
1685 static int
1686 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1687 rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1688 rtx *pdest, rtx *psrc)
1690 int i;
1691 const_rtx set = 0;
1692 rtx src, dest;
1693 rtx p;
1694 #ifdef AUTO_INC_DEC
1695 rtx link;
1696 #endif
1697 bool all_adjacent = true;
1698 int (*is_volatile_p) (const_rtx);
1700 if (succ)
1702 if (succ2)
1704 if (next_active_insn (succ2) != i3)
1705 all_adjacent = false;
1706 if (next_active_insn (succ) != succ2)
1707 all_adjacent = false;
1709 else if (next_active_insn (succ) != i3)
1710 all_adjacent = false;
1711 if (next_active_insn (insn) != succ)
1712 all_adjacent = false;
1714 else if (next_active_insn (insn) != i3)
1715 all_adjacent = false;
1717 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1718 or a PARALLEL consisting of such a SET and CLOBBERs.
1720 If INSN has CLOBBER parallel parts, ignore them for our processing.
1721 By definition, these happen during the execution of the insn. When it
1722 is merged with another insn, all bets are off. If they are, in fact,
1723 needed and aren't also supplied in I3, they may be added by
1724 recog_for_combine. Otherwise, it won't match.
1726 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1727 note.
1729 Get the source and destination of INSN. If more than one, can't
1730 combine. */
1732 if (GET_CODE (PATTERN (insn)) == SET)
1733 set = PATTERN (insn);
1734 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1735 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1737 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1739 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1741 switch (GET_CODE (elt))
1743 /* This is important to combine floating point insns
1744 for the SH4 port. */
1745 case USE:
1746 /* Combining an isolated USE doesn't make sense.
1747 We depend here on combinable_i3pat to reject them. */
1748 /* The code below this loop only verifies that the inputs of
1749 the SET in INSN do not change. We call reg_set_between_p
1750 to verify that the REG in the USE does not change between
1751 I3 and INSN.
1752 If the USE in INSN was for a pseudo register, the matching
1753 insn pattern will likely match any register; combining this
1754 with any other USE would only be safe if we knew that the
1755 used registers have identical values, or if there was
1756 something to tell them apart, e.g. different modes. For
1757 now, we forgo such complicated tests and simply disallow
1758 combining of USES of pseudo registers with any other USE. */
1759 if (REG_P (XEXP (elt, 0))
1760 && GET_CODE (PATTERN (i3)) == PARALLEL)
1762 rtx i3pat = PATTERN (i3);
1763 int i = XVECLEN (i3pat, 0) - 1;
1764 unsigned int regno = REGNO (XEXP (elt, 0));
1768 rtx i3elt = XVECEXP (i3pat, 0, i);
1770 if (GET_CODE (i3elt) == USE
1771 && REG_P (XEXP (i3elt, 0))
1772 && (REGNO (XEXP (i3elt, 0)) == regno
1773 ? reg_set_between_p (XEXP (elt, 0),
1774 PREV_INSN (insn), i3)
1775 : regno >= FIRST_PSEUDO_REGISTER))
1776 return 0;
1778 while (--i >= 0);
1780 break;
1782 /* We can ignore CLOBBERs. */
1783 case CLOBBER:
1784 break;
1786 case SET:
1787 /* Ignore SETs whose result isn't used but not those that
1788 have side-effects. */
1789 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1790 && insn_nothrow_p (insn)
1791 && !side_effects_p (elt))
1792 break;
1794 /* If we have already found a SET, this is a second one and
1795 so we cannot combine with this insn. */
1796 if (set)
1797 return 0;
1799 set = elt;
1800 break;
1802 default:
1803 /* Anything else means we can't combine. */
1804 return 0;
1808 if (set == 0
1809 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1810 so don't do anything with it. */
1811 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1812 return 0;
1814 else
1815 return 0;
1817 if (set == 0)
1818 return 0;
1820 /* The simplification in expand_field_assignment may call back to
1821 get_last_value, so set safe guard here. */
1822 subst_low_luid = DF_INSN_LUID (insn);
1824 set = expand_field_assignment (set);
1825 src = SET_SRC (set), dest = SET_DEST (set);
1827 /* Don't eliminate a store in the stack pointer. */
1828 if (dest == stack_pointer_rtx
1829 /* Don't combine with an insn that sets a register to itself if it has
1830 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1831 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1832 /* Can't merge an ASM_OPERANDS. */
1833 || GET_CODE (src) == ASM_OPERANDS
1834 /* Can't merge a function call. */
1835 || GET_CODE (src) == CALL
1836 /* Don't eliminate a function call argument. */
1837 || (CALL_P (i3)
1838 && (find_reg_fusage (i3, USE, dest)
1839 || (REG_P (dest)
1840 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1841 && global_regs[REGNO (dest)])))
1842 /* Don't substitute into an incremented register. */
1843 || FIND_REG_INC_NOTE (i3, dest)
1844 || (succ && FIND_REG_INC_NOTE (succ, dest))
1845 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1846 /* Don't substitute into a non-local goto, this confuses CFG. */
1847 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1848 /* Make sure that DEST is not used after SUCC but before I3. */
1849 || (!all_adjacent
1850 && ((succ2
1851 && (reg_used_between_p (dest, succ2, i3)
1852 || reg_used_between_p (dest, succ, succ2)))
1853 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1854 /* Make sure that the value that is to be substituted for the register
1855 does not use any registers whose values alter in between. However,
1856 If the insns are adjacent, a use can't cross a set even though we
1857 think it might (this can happen for a sequence of insns each setting
1858 the same destination; last_set of that register might point to
1859 a NOTE). If INSN has a REG_EQUIV note, the register is always
1860 equivalent to the memory so the substitution is valid even if there
1861 are intervening stores. Also, don't move a volatile asm or
1862 UNSPEC_VOLATILE across any other insns. */
1863 || (! all_adjacent
1864 && (((!MEM_P (src)
1865 || ! find_reg_note (insn, REG_EQUIV, src))
1866 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1867 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1868 || GET_CODE (src) == UNSPEC_VOLATILE))
1869 /* Don't combine across a CALL_INSN, because that would possibly
1870 change whether the life span of some REGs crosses calls or not,
1871 and it is a pain to update that information.
1872 Exception: if source is a constant, moving it later can't hurt.
1873 Accept that as a special case. */
1874 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1875 return 0;
1877 /* DEST must either be a REG or CC0. */
1878 if (REG_P (dest))
1880 /* If register alignment is being enforced for multi-word items in all
1881 cases except for parameters, it is possible to have a register copy
1882 insn referencing a hard register that is not allowed to contain the
1883 mode being copied and which would not be valid as an operand of most
1884 insns. Eliminate this problem by not combining with such an insn.
1886 Also, on some machines we don't want to extend the life of a hard
1887 register. */
1889 if (REG_P (src)
1890 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1891 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1892 /* Don't extend the life of a hard register unless it is
1893 user variable (if we have few registers) or it can't
1894 fit into the desired register (meaning something special
1895 is going on).
1896 Also avoid substituting a return register into I3, because
1897 reload can't handle a conflict with constraints of other
1898 inputs. */
1899 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1900 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1901 return 0;
1903 else if (GET_CODE (dest) != CC0)
1904 return 0;
1907 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1908 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1909 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1911 /* Don't substitute for a register intended as a clobberable
1912 operand. */
1913 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1914 if (rtx_equal_p (reg, dest))
1915 return 0;
1917 /* If the clobber represents an earlyclobber operand, we must not
1918 substitute an expression containing the clobbered register.
1919 As we do not analyze the constraint strings here, we have to
1920 make the conservative assumption. However, if the register is
1921 a fixed hard reg, the clobber cannot represent any operand;
1922 we leave it up to the machine description to either accept or
1923 reject use-and-clobber patterns. */
1924 if (!REG_P (reg)
1925 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1926 || !fixed_regs[REGNO (reg)])
1927 if (reg_overlap_mentioned_p (reg, src))
1928 return 0;
1931 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1932 or not), reject, unless nothing volatile comes between it and I3 */
1934 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1936 /* Make sure neither succ nor succ2 contains a volatile reference. */
1937 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1938 return 0;
1939 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1940 return 0;
1941 /* We'll check insns between INSN and I3 below. */
1944 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1945 to be an explicit register variable, and was chosen for a reason. */
1947 if (GET_CODE (src) == ASM_OPERANDS
1948 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1949 return 0;
1951 /* If INSN contains volatile references (specifically volatile MEMs),
1952 we cannot combine across any other volatile references.
1953 Even if INSN doesn't contain volatile references, any intervening
1954 volatile insn might affect machine state. */
1956 is_volatile_p = volatile_refs_p (PATTERN (insn))
1957 ? volatile_refs_p
1958 : volatile_insn_p;
1960 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1961 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
1962 return 0;
1964 /* If INSN contains an autoincrement or autodecrement, make sure that
1965 register is not used between there and I3, and not already used in
1966 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1967 Also insist that I3 not be a jump; if it were one
1968 and the incremented register were spilled, we would lose. */
1970 #ifdef AUTO_INC_DEC
1971 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1972 if (REG_NOTE_KIND (link) == REG_INC
1973 && (JUMP_P (i3)
1974 || reg_used_between_p (XEXP (link, 0), insn, i3)
1975 || (pred != NULL_RTX
1976 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1977 || (pred2 != NULL_RTX
1978 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1979 || (succ != NULL_RTX
1980 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1981 || (succ2 != NULL_RTX
1982 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
1983 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1984 return 0;
1985 #endif
1987 #ifdef HAVE_cc0
1988 /* Don't combine an insn that follows a CC0-setting insn.
1989 An insn that uses CC0 must not be separated from the one that sets it.
1990 We do, however, allow I2 to follow a CC0-setting insn if that insn
1991 is passed as I1; in that case it will be deleted also.
1992 We also allow combining in this case if all the insns are adjacent
1993 because that would leave the two CC0 insns adjacent as well.
1994 It would be more logical to test whether CC0 occurs inside I1 or I2,
1995 but that would be much slower, and this ought to be equivalent. */
1997 p = prev_nonnote_insn (insn);
1998 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1999 && ! all_adjacent)
2000 return 0;
2001 #endif
2003 /* If we get here, we have passed all the tests and the combination is
2004 to be allowed. */
2006 *pdest = dest;
2007 *psrc = src;
2009 return 1;
2012 /* LOC is the location within I3 that contains its pattern or the component
2013 of a PARALLEL of the pattern. We validate that it is valid for combining.
2015 One problem is if I3 modifies its output, as opposed to replacing it
2016 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2017 doing so would produce an insn that is not equivalent to the original insns.
2019 Consider:
2021 (set (reg:DI 101) (reg:DI 100))
2022 (set (subreg:SI (reg:DI 101) 0) <foo>)
2024 This is NOT equivalent to:
2026 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2027 (set (reg:DI 101) (reg:DI 100))])
2029 Not only does this modify 100 (in which case it might still be valid
2030 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2032 We can also run into a problem if I2 sets a register that I1
2033 uses and I1 gets directly substituted into I3 (not via I2). In that
2034 case, we would be getting the wrong value of I2DEST into I3, so we
2035 must reject the combination. This case occurs when I2 and I1 both
2036 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2037 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2038 of a SET must prevent combination from occurring. The same situation
2039 can occur for I0, in which case I0_NOT_IN_SRC is set.
2041 Before doing the above check, we first try to expand a field assignment
2042 into a set of logical operations.
2044 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2045 we place a register that is both set and used within I3. If more than one
2046 such register is detected, we fail.
2048 Return 1 if the combination is valid, zero otherwise. */
2050 static int
2051 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2052 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2054 rtx x = *loc;
2056 if (GET_CODE (x) == SET)
2058 rtx set = x ;
2059 rtx dest = SET_DEST (set);
2060 rtx src = SET_SRC (set);
2061 rtx inner_dest = dest;
2062 rtx subdest;
2064 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2065 || GET_CODE (inner_dest) == SUBREG
2066 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2067 inner_dest = XEXP (inner_dest, 0);
2069 /* Check for the case where I3 modifies its output, as discussed
2070 above. We don't want to prevent pseudos from being combined
2071 into the address of a MEM, so only prevent the combination if
2072 i1 or i2 set the same MEM. */
2073 if ((inner_dest != dest &&
2074 (!MEM_P (inner_dest)
2075 || rtx_equal_p (i2dest, inner_dest)
2076 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2077 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2078 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2079 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2080 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2082 /* This is the same test done in can_combine_p except we can't test
2083 all_adjacent; we don't have to, since this instruction will stay
2084 in place, thus we are not considering increasing the lifetime of
2085 INNER_DEST.
2087 Also, if this insn sets a function argument, combining it with
2088 something that might need a spill could clobber a previous
2089 function argument; the all_adjacent test in can_combine_p also
2090 checks this; here, we do a more specific test for this case. */
2092 || (REG_P (inner_dest)
2093 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2094 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2095 GET_MODE (inner_dest))))
2096 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2097 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2098 return 0;
2100 /* If DEST is used in I3, it is being killed in this insn, so
2101 record that for later. We have to consider paradoxical
2102 subregs here, since they kill the whole register, but we
2103 ignore partial subregs, STRICT_LOW_PART, etc.
2104 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2105 STACK_POINTER_REGNUM, since these are always considered to be
2106 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2107 subdest = dest;
2108 if (GET_CODE (subdest) == SUBREG
2109 && (GET_MODE_SIZE (GET_MODE (subdest))
2110 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2111 subdest = SUBREG_REG (subdest);
2112 if (pi3dest_killed
2113 && REG_P (subdest)
2114 && reg_referenced_p (subdest, PATTERN (i3))
2115 && REGNO (subdest) != FRAME_POINTER_REGNUM
2116 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2117 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2118 #endif
2119 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2120 && (REGNO (subdest) != ARG_POINTER_REGNUM
2121 || ! fixed_regs [REGNO (subdest)])
2122 #endif
2123 && REGNO (subdest) != STACK_POINTER_REGNUM)
2125 if (*pi3dest_killed)
2126 return 0;
2128 *pi3dest_killed = subdest;
2132 else if (GET_CODE (x) == PARALLEL)
2134 int i;
2136 for (i = 0; i < XVECLEN (x, 0); i++)
2137 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2138 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2139 return 0;
2142 return 1;
2145 /* Return 1 if X is an arithmetic expression that contains a multiplication
2146 and division. We don't count multiplications by powers of two here. */
2148 static int
2149 contains_muldiv (rtx x)
2151 switch (GET_CODE (x))
2153 case MOD: case DIV: case UMOD: case UDIV:
2154 return 1;
2156 case MULT:
2157 return ! (CONST_INT_P (XEXP (x, 1))
2158 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2159 default:
2160 if (BINARY_P (x))
2161 return contains_muldiv (XEXP (x, 0))
2162 || contains_muldiv (XEXP (x, 1));
2164 if (UNARY_P (x))
2165 return contains_muldiv (XEXP (x, 0));
2167 return 0;
2171 /* Determine whether INSN can be used in a combination. Return nonzero if
2172 not. This is used in try_combine to detect early some cases where we
2173 can't perform combinations. */
2175 static int
2176 cant_combine_insn_p (rtx insn)
2178 rtx set;
2179 rtx src, dest;
2181 /* If this isn't really an insn, we can't do anything.
2182 This can occur when flow deletes an insn that it has merged into an
2183 auto-increment address. */
2184 if (! INSN_P (insn))
2185 return 1;
2187 /* Never combine loads and stores involving hard regs that are likely
2188 to be spilled. The register allocator can usually handle such
2189 reg-reg moves by tying. If we allow the combiner to make
2190 substitutions of likely-spilled regs, reload might die.
2191 As an exception, we allow combinations involving fixed regs; these are
2192 not available to the register allocator so there's no risk involved. */
2194 set = single_set (insn);
2195 if (! set)
2196 return 0;
2197 src = SET_SRC (set);
2198 dest = SET_DEST (set);
2199 if (GET_CODE (src) == SUBREG)
2200 src = SUBREG_REG (src);
2201 if (GET_CODE (dest) == SUBREG)
2202 dest = SUBREG_REG (dest);
2203 if (REG_P (src) && REG_P (dest)
2204 && ((HARD_REGISTER_P (src)
2205 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2206 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2207 || (HARD_REGISTER_P (dest)
2208 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2209 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2210 return 1;
2212 return 0;
2215 struct likely_spilled_retval_info
2217 unsigned regno, nregs;
2218 unsigned mask;
2221 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2222 hard registers that are known to be written to / clobbered in full. */
2223 static void
2224 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2226 struct likely_spilled_retval_info *const info =
2227 (struct likely_spilled_retval_info *) data;
2228 unsigned regno, nregs;
2229 unsigned new_mask;
2231 if (!REG_P (XEXP (set, 0)))
2232 return;
2233 regno = REGNO (x);
2234 if (regno >= info->regno + info->nregs)
2235 return;
2236 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2237 if (regno + nregs <= info->regno)
2238 return;
2239 new_mask = (2U << (nregs - 1)) - 1;
2240 if (regno < info->regno)
2241 new_mask >>= info->regno - regno;
2242 else
2243 new_mask <<= regno - info->regno;
2244 info->mask &= ~new_mask;
2247 /* Return nonzero iff part of the return value is live during INSN, and
2248 it is likely spilled. This can happen when more than one insn is needed
2249 to copy the return value, e.g. when we consider to combine into the
2250 second copy insn for a complex value. */
2252 static int
2253 likely_spilled_retval_p (rtx insn)
2255 rtx use = BB_END (this_basic_block);
2256 rtx reg, p;
2257 unsigned regno, nregs;
2258 /* We assume here that no machine mode needs more than
2259 32 hard registers when the value overlaps with a register
2260 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2261 unsigned mask;
2262 struct likely_spilled_retval_info info;
2264 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2265 return 0;
2266 reg = XEXP (PATTERN (use), 0);
2267 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2268 return 0;
2269 regno = REGNO (reg);
2270 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2271 if (nregs == 1)
2272 return 0;
2273 mask = (2U << (nregs - 1)) - 1;
2275 /* Disregard parts of the return value that are set later. */
2276 info.regno = regno;
2277 info.nregs = nregs;
2278 info.mask = mask;
2279 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2280 if (INSN_P (p))
2281 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2282 mask = info.mask;
2284 /* Check if any of the (probably) live return value registers is
2285 likely spilled. */
2286 nregs --;
2289 if ((mask & 1 << nregs)
2290 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2291 return 1;
2292 } while (nregs--);
2293 return 0;
2296 /* Adjust INSN after we made a change to its destination.
2298 Changing the destination can invalidate notes that say something about
2299 the results of the insn and a LOG_LINK pointing to the insn. */
2301 static void
2302 adjust_for_new_dest (rtx insn)
2304 /* For notes, be conservative and simply remove them. */
2305 remove_reg_equal_equiv_notes (insn);
2307 /* The new insn will have a destination that was previously the destination
2308 of an insn just above it. Call distribute_links to make a LOG_LINK from
2309 the next use of that destination. */
2310 distribute_links (alloc_insn_link (insn, NULL));
2312 df_insn_rescan (insn);
2315 /* Return TRUE if combine can reuse reg X in mode MODE.
2316 ADDED_SETS is nonzero if the original set is still required. */
2317 static bool
2318 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2320 unsigned int regno;
2322 if (!REG_P(x))
2323 return false;
2325 regno = REGNO (x);
2326 /* Allow hard registers if the new mode is legal, and occupies no more
2327 registers than the old mode. */
2328 if (regno < FIRST_PSEUDO_REGISTER)
2329 return (HARD_REGNO_MODE_OK (regno, mode)
2330 && (hard_regno_nregs[regno][GET_MODE (x)]
2331 >= hard_regno_nregs[regno][mode]));
2333 /* Or a pseudo that is only used once. */
2334 return (REG_N_SETS (regno) == 1 && !added_sets
2335 && !REG_USERVAR_P (x));
2339 /* Check whether X, the destination of a set, refers to part of
2340 the register specified by REG. */
2342 static bool
2343 reg_subword_p (rtx x, rtx reg)
2345 /* Check that reg is an integer mode register. */
2346 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2347 return false;
2349 if (GET_CODE (x) == STRICT_LOW_PART
2350 || GET_CODE (x) == ZERO_EXTRACT)
2351 x = XEXP (x, 0);
2353 return GET_CODE (x) == SUBREG
2354 && SUBREG_REG (x) == reg
2355 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2358 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2359 Note that the INSN should be deleted *after* removing dead edges, so
2360 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2361 but not for a (set (pc) (label_ref FOO)). */
2363 static void
2364 update_cfg_for_uncondjump (rtx insn)
2366 basic_block bb = BLOCK_FOR_INSN (insn);
2367 gcc_assert (BB_END (bb) == insn);
2369 purge_dead_edges (bb);
2371 delete_insn (insn);
2372 if (EDGE_COUNT (bb->succs) == 1)
2374 rtx insn;
2376 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2378 /* Remove barriers from the footer if there are any. */
2379 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2380 if (BARRIER_P (insn))
2382 if (PREV_INSN (insn))
2383 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2384 else
2385 BB_FOOTER (bb) = NEXT_INSN (insn);
2386 if (NEXT_INSN (insn))
2387 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2389 else if (LABEL_P (insn))
2390 break;
2394 /* Try to combine the insns I0, I1 and I2 into I3.
2395 Here I0, I1 and I2 appear earlier than I3.
2396 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2399 If we are combining more than two insns and the resulting insn is not
2400 recognized, try splitting it into two insns. If that happens, I2 and I3
2401 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2402 Otherwise, I0, I1 and I2 are pseudo-deleted.
2404 Return 0 if the combination does not work. Then nothing is changed.
2405 If we did the combination, return the insn at which combine should
2406 resume scanning.
2408 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2409 new direct jump instruction.
2411 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2412 been I3 passed to an earlier try_combine within the same basic
2413 block. */
2415 static rtx
2416 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p,
2417 rtx last_combined_insn)
2419 /* New patterns for I3 and I2, respectively. */
2420 rtx newpat, newi2pat = 0;
2421 rtvec newpat_vec_with_clobbers = 0;
2422 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2423 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2424 dead. */
2425 int added_sets_0, added_sets_1, added_sets_2;
2426 /* Total number of SETs to put into I3. */
2427 int total_sets;
2428 /* Nonzero if I2's or I1's body now appears in I3. */
2429 int i2_is_used = 0, i1_is_used = 0;
2430 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2431 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2432 /* Contains I3 if the destination of I3 is used in its source, which means
2433 that the old life of I3 is being killed. If that usage is placed into
2434 I2 and not in I3, a REG_DEAD note must be made. */
2435 rtx i3dest_killed = 0;
2436 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2437 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2438 /* Copy of SET_SRC of I1 and I0, if needed. */
2439 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2440 /* Set if I2DEST was reused as a scratch register. */
2441 bool i2scratch = false;
2442 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2443 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2444 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2445 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2446 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2447 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2448 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2449 /* Notes that must be added to REG_NOTES in I3 and I2. */
2450 rtx new_i3_notes, new_i2_notes;
2451 /* Notes that we substituted I3 into I2 instead of the normal case. */
2452 int i3_subst_into_i2 = 0;
2453 /* Notes that I1, I2 or I3 is a MULT operation. */
2454 int have_mult = 0;
2455 int swap_i2i3 = 0;
2456 int changed_i3_dest = 0;
2458 int maxreg;
2459 rtx temp;
2460 struct insn_link *link;
2461 rtx other_pat = 0;
2462 rtx new_other_notes;
2463 int i;
2465 /* Only try four-insn combinations when there's high likelihood of
2466 success. Look for simple insns, such as loads of constants or
2467 binary operations involving a constant. */
2468 if (i0)
2470 int i;
2471 int ngood = 0;
2472 int nshift = 0;
2474 if (!flag_expensive_optimizations)
2475 return 0;
2477 for (i = 0; i < 4; i++)
2479 rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2480 rtx set = single_set (insn);
2481 rtx src;
2482 if (!set)
2483 continue;
2484 src = SET_SRC (set);
2485 if (CONSTANT_P (src))
2487 ngood += 2;
2488 break;
2490 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2491 ngood++;
2492 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2493 || GET_CODE (src) == LSHIFTRT)
2494 nshift++;
2496 if (ngood < 2 && nshift < 2)
2497 return 0;
2500 /* Exit early if one of the insns involved can't be used for
2501 combinations. */
2502 if (cant_combine_insn_p (i3)
2503 || cant_combine_insn_p (i2)
2504 || (i1 && cant_combine_insn_p (i1))
2505 || (i0 && cant_combine_insn_p (i0))
2506 || likely_spilled_retval_p (i3))
2507 return 0;
2509 combine_attempts++;
2510 undobuf.other_insn = 0;
2512 /* Reset the hard register usage information. */
2513 CLEAR_HARD_REG_SET (newpat_used_regs);
2515 if (dump_file && (dump_flags & TDF_DETAILS))
2517 if (i0)
2518 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2519 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2520 else if (i1)
2521 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2522 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2523 else
2524 fprintf (dump_file, "\nTrying %d -> %d:\n",
2525 INSN_UID (i2), INSN_UID (i3));
2528 /* If multiple insns feed into one of I2 or I3, they can be in any
2529 order. To simplify the code below, reorder them in sequence. */
2530 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2531 temp = i2, i2 = i0, i0 = temp;
2532 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2533 temp = i1, i1 = i0, i0 = temp;
2534 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2535 temp = i1, i1 = i2, i2 = temp;
2537 added_links_insn = 0;
2539 /* First check for one important special case that the code below will
2540 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2541 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2542 we may be able to replace that destination with the destination of I3.
2543 This occurs in the common code where we compute both a quotient and
2544 remainder into a structure, in which case we want to do the computation
2545 directly into the structure to avoid register-register copies.
2547 Note that this case handles both multiple sets in I2 and also cases
2548 where I2 has a number of CLOBBERs inside the PARALLEL.
2550 We make very conservative checks below and only try to handle the
2551 most common cases of this. For example, we only handle the case
2552 where I2 and I3 are adjacent to avoid making difficult register
2553 usage tests. */
2555 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2556 && REG_P (SET_SRC (PATTERN (i3)))
2557 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2558 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2559 && GET_CODE (PATTERN (i2)) == PARALLEL
2560 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2561 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2562 below would need to check what is inside (and reg_overlap_mentioned_p
2563 doesn't support those codes anyway). Don't allow those destinations;
2564 the resulting insn isn't likely to be recognized anyway. */
2565 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2566 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2567 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2568 SET_DEST (PATTERN (i3)))
2569 && next_active_insn (i2) == i3)
2571 rtx p2 = PATTERN (i2);
2573 /* Make sure that the destination of I3,
2574 which we are going to substitute into one output of I2,
2575 is not used within another output of I2. We must avoid making this:
2576 (parallel [(set (mem (reg 69)) ...)
2577 (set (reg 69) ...)])
2578 which is not well-defined as to order of actions.
2579 (Besides, reload can't handle output reloads for this.)
2581 The problem can also happen if the dest of I3 is a memory ref,
2582 if another dest in I2 is an indirect memory ref. */
2583 for (i = 0; i < XVECLEN (p2, 0); i++)
2584 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2585 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2586 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2587 SET_DEST (XVECEXP (p2, 0, i))))
2588 break;
2590 if (i == XVECLEN (p2, 0))
2591 for (i = 0; i < XVECLEN (p2, 0); i++)
2592 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2593 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2595 combine_merges++;
2597 subst_insn = i3;
2598 subst_low_luid = DF_INSN_LUID (i2);
2600 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2601 i2src = SET_SRC (XVECEXP (p2, 0, i));
2602 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2603 i2dest_killed = dead_or_set_p (i2, i2dest);
2605 /* Replace the dest in I2 with our dest and make the resulting
2606 insn the new pattern for I3. Then skip to where we validate
2607 the pattern. Everything was set up above. */
2608 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2609 newpat = p2;
2610 i3_subst_into_i2 = 1;
2611 goto validate_replacement;
2615 /* If I2 is setting a pseudo to a constant and I3 is setting some
2616 sub-part of it to another constant, merge them by making a new
2617 constant. */
2618 if (i1 == 0
2619 && (temp = single_set (i2)) != 0
2620 && (CONST_INT_P (SET_SRC (temp))
2621 || CONST_DOUBLE_AS_INT_P (SET_SRC (temp)))
2622 && GET_CODE (PATTERN (i3)) == SET
2623 && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2624 || CONST_DOUBLE_AS_INT_P (SET_SRC (PATTERN (i3))))
2625 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2627 rtx dest = SET_DEST (PATTERN (i3));
2628 int offset = -1;
2629 int width = 0;
2631 if (GET_CODE (dest) == ZERO_EXTRACT)
2633 if (CONST_INT_P (XEXP (dest, 1))
2634 && CONST_INT_P (XEXP (dest, 2)))
2636 width = INTVAL (XEXP (dest, 1));
2637 offset = INTVAL (XEXP (dest, 2));
2638 dest = XEXP (dest, 0);
2639 if (BITS_BIG_ENDIAN)
2640 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2643 else
2645 if (GET_CODE (dest) == STRICT_LOW_PART)
2646 dest = XEXP (dest, 0);
2647 width = GET_MODE_PRECISION (GET_MODE (dest));
2648 offset = 0;
2651 if (offset >= 0)
2653 /* If this is the low part, we're done. */
2654 if (subreg_lowpart_p (dest))
2656 /* Handle the case where inner is twice the size of outer. */
2657 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp)))
2658 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2659 offset += GET_MODE_PRECISION (GET_MODE (dest));
2660 /* Otherwise give up for now. */
2661 else
2662 offset = -1;
2665 if (offset >= 0
2666 && (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp)))
2667 <= HOST_BITS_PER_DOUBLE_INT))
2669 double_int m, o, i;
2670 rtx inner = SET_SRC (PATTERN (i3));
2671 rtx outer = SET_SRC (temp);
2673 o = rtx_to_double_int (outer);
2674 i = rtx_to_double_int (inner);
2676 m = double_int::mask (width);
2677 i &= m;
2678 m = m.llshift (offset, HOST_BITS_PER_DOUBLE_INT);
2679 i = i.llshift (offset, HOST_BITS_PER_DOUBLE_INT);
2680 o = o.and_not (m) | i;
2682 combine_merges++;
2683 subst_insn = i3;
2684 subst_low_luid = DF_INSN_LUID (i2);
2685 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2686 i2dest = SET_DEST (temp);
2687 i2dest_killed = dead_or_set_p (i2, i2dest);
2689 /* Replace the source in I2 with the new constant and make the
2690 resulting insn the new pattern for I3. Then skip to where we
2691 validate the pattern. Everything was set up above. */
2692 SUBST (SET_SRC (temp),
2693 immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2695 newpat = PATTERN (i2);
2697 /* The dest of I3 has been replaced with the dest of I2. */
2698 changed_i3_dest = 1;
2699 goto validate_replacement;
2703 #ifndef HAVE_cc0
2704 /* If we have no I1 and I2 looks like:
2705 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2706 (set Y OP)])
2707 make up a dummy I1 that is
2708 (set Y OP)
2709 and change I2 to be
2710 (set (reg:CC X) (compare:CC Y (const_int 0)))
2712 (We can ignore any trailing CLOBBERs.)
2714 This undoes a previous combination and allows us to match a branch-and-
2715 decrement insn. */
2717 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2718 && XVECLEN (PATTERN (i2), 0) >= 2
2719 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2720 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2721 == MODE_CC)
2722 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2723 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2724 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2725 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2726 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2727 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2729 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2730 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2731 break;
2733 if (i == 1)
2735 /* We make I1 with the same INSN_UID as I2. This gives it
2736 the same DF_INSN_LUID for value tracking. Our fake I1 will
2737 never appear in the insn stream so giving it the same INSN_UID
2738 as I2 will not cause a problem. */
2740 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2741 BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2742 INSN_LOCATION (i2), -1, NULL_RTX);
2744 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2745 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2746 SET_DEST (PATTERN (i1)));
2747 SUBST_LINK (LOG_LINKS (i2), alloc_insn_link (i1, LOG_LINKS (i2)));
2750 #endif
2752 /* Verify that I2 and I1 are valid for combining. */
2753 if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2754 || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2755 &i1dest, &i1src))
2756 || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2757 &i0dest, &i0src)))
2759 undo_all ();
2760 return 0;
2763 /* Record whether I2DEST is used in I2SRC and similarly for the other
2764 cases. Knowing this will help in register status updating below. */
2765 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2766 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2767 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2768 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2769 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2770 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2771 i2dest_killed = dead_or_set_p (i2, i2dest);
2772 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2773 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2775 /* For the earlier insns, determine which of the subsequent ones they
2776 feed. */
2777 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2778 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2779 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2780 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2781 && reg_overlap_mentioned_p (i0dest, i2src))));
2783 /* Ensure that I3's pattern can be the destination of combines. */
2784 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2785 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2786 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2787 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2788 &i3dest_killed))
2790 undo_all ();
2791 return 0;
2794 /* See if any of the insns is a MULT operation. Unless one is, we will
2795 reject a combination that is, since it must be slower. Be conservative
2796 here. */
2797 if (GET_CODE (i2src) == MULT
2798 || (i1 != 0 && GET_CODE (i1src) == MULT)
2799 || (i0 != 0 && GET_CODE (i0src) == MULT)
2800 || (GET_CODE (PATTERN (i3)) == SET
2801 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2802 have_mult = 1;
2804 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2805 We used to do this EXCEPT in one case: I3 has a post-inc in an
2806 output operand. However, that exception can give rise to insns like
2807 mov r3,(r3)+
2808 which is a famous insn on the PDP-11 where the value of r3 used as the
2809 source was model-dependent. Avoid this sort of thing. */
2811 #if 0
2812 if (!(GET_CODE (PATTERN (i3)) == SET
2813 && REG_P (SET_SRC (PATTERN (i3)))
2814 && MEM_P (SET_DEST (PATTERN (i3)))
2815 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2816 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2817 /* It's not the exception. */
2818 #endif
2819 #ifdef AUTO_INC_DEC
2821 rtx link;
2822 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2823 if (REG_NOTE_KIND (link) == REG_INC
2824 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2825 || (i1 != 0
2826 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2828 undo_all ();
2829 return 0;
2832 #endif
2834 /* See if the SETs in I1 or I2 need to be kept around in the merged
2835 instruction: whenever the value set there is still needed past I3.
2836 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2838 For the SET in I1, we have two cases: If I1 and I2 independently
2839 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2840 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2841 in I1 needs to be kept around unless I1DEST dies or is set in either
2842 I2 or I3. The same consideration applies to I0. */
2844 added_sets_2 = !dead_or_set_p (i3, i2dest);
2846 if (i1)
2847 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2848 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2849 else
2850 added_sets_1 = 0;
2852 if (i0)
2853 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2854 || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
2855 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
2856 else
2857 added_sets_0 = 0;
2859 /* We are about to copy insns for the case where they need to be kept
2860 around. Check that they can be copied in the merged instruction. */
2862 if (targetm.cannot_copy_insn_p
2863 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2864 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2865 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2867 undo_all ();
2868 return 0;
2871 /* If the set in I2 needs to be kept around, we must make a copy of
2872 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2873 PATTERN (I2), we are only substituting for the original I1DEST, not into
2874 an already-substituted copy. This also prevents making self-referential
2875 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2876 I2DEST. */
2878 if (added_sets_2)
2880 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2881 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2882 else
2883 i2pat = copy_rtx (PATTERN (i2));
2886 if (added_sets_1)
2888 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2889 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2890 else
2891 i1pat = copy_rtx (PATTERN (i1));
2894 if (added_sets_0)
2896 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2897 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2898 else
2899 i0pat = copy_rtx (PATTERN (i0));
2902 combine_merges++;
2904 /* Substitute in the latest insn for the regs set by the earlier ones. */
2906 maxreg = max_reg_num ();
2908 subst_insn = i3;
2910 #ifndef HAVE_cc0
2911 /* Many machines that don't use CC0 have insns that can both perform an
2912 arithmetic operation and set the condition code. These operations will
2913 be represented as a PARALLEL with the first element of the vector
2914 being a COMPARE of an arithmetic operation with the constant zero.
2915 The second element of the vector will set some pseudo to the result
2916 of the same arithmetic operation. If we simplify the COMPARE, we won't
2917 match such a pattern and so will generate an extra insn. Here we test
2918 for this case, where both the comparison and the operation result are
2919 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2920 I2SRC. Later we will make the PARALLEL that contains I2. */
2922 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2923 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2924 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
2925 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2927 rtx newpat_dest;
2928 rtx *cc_use_loc = NULL, cc_use_insn = NULL_RTX;
2929 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
2930 enum machine_mode compare_mode, orig_compare_mode;
2931 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
2933 newpat = PATTERN (i3);
2934 newpat_dest = SET_DEST (newpat);
2935 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
2937 if (undobuf.other_insn == 0
2938 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
2939 &cc_use_insn)))
2941 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
2942 compare_code = simplify_compare_const (compare_code,
2943 op0, &op1);
2944 #ifdef CANONICALIZE_COMPARISON
2945 CANONICALIZE_COMPARISON (compare_code, op0, op1);
2946 #endif
2949 /* Do the rest only if op1 is const0_rtx, which may be the
2950 result of simplification. */
2951 if (op1 == const0_rtx)
2953 /* If a single use of the CC is found, prepare to modify it
2954 when SELECT_CC_MODE returns a new CC-class mode, or when
2955 the above simplify_compare_const() returned a new comparison
2956 operator. undobuf.other_insn is assigned the CC use insn
2957 when modifying it. */
2958 if (cc_use_loc)
2960 #ifdef SELECT_CC_MODE
2961 enum machine_mode new_mode
2962 = SELECT_CC_MODE (compare_code, op0, op1);
2963 if (new_mode != orig_compare_mode
2964 && can_change_dest_mode (SET_DEST (newpat),
2965 added_sets_2, new_mode))
2967 unsigned int regno = REGNO (newpat_dest);
2968 compare_mode = new_mode;
2969 if (regno < FIRST_PSEUDO_REGISTER)
2970 newpat_dest = gen_rtx_REG (compare_mode, regno);
2971 else
2973 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2974 newpat_dest = regno_reg_rtx[regno];
2977 #endif
2978 /* Cases for modifying the CC-using comparison. */
2979 if (compare_code != orig_compare_code
2980 /* ??? Do we need to verify the zero rtx? */
2981 && XEXP (*cc_use_loc, 1) == const0_rtx)
2983 /* Replace cc_use_loc with entire new RTX. */
2984 SUBST (*cc_use_loc,
2985 gen_rtx_fmt_ee (compare_code, compare_mode,
2986 newpat_dest, const0_rtx));
2987 undobuf.other_insn = cc_use_insn;
2989 else if (compare_mode != orig_compare_mode)
2991 /* Just replace the CC reg with a new mode. */
2992 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
2993 undobuf.other_insn = cc_use_insn;
2997 /* Now we modify the current newpat:
2998 First, SET_DEST(newpat) is updated if the CC mode has been
2999 altered. For targets without SELECT_CC_MODE, this should be
3000 optimized away. */
3001 if (compare_mode != orig_compare_mode)
3002 SUBST (SET_DEST (newpat), newpat_dest);
3003 /* This is always done to propagate i2src into newpat. */
3004 SUBST (SET_SRC (newpat),
3005 gen_rtx_COMPARE (compare_mode, op0, op1));
3006 /* Create new version of i2pat if needed; the below PARALLEL
3007 creation needs this to work correctly. */
3008 if (! rtx_equal_p (i2src, op0))
3009 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3010 i2_is_used = 1;
3013 #endif
3015 if (i2_is_used == 0)
3017 /* It is possible that the source of I2 or I1 may be performing
3018 an unneeded operation, such as a ZERO_EXTEND of something
3019 that is known to have the high part zero. Handle that case
3020 by letting subst look at the inner insns.
3022 Another way to do this would be to have a function that tries
3023 to simplify a single insn instead of merging two or more
3024 insns. We don't do this because of the potential of infinite
3025 loops and because of the potential extra memory required.
3026 However, doing it the way we are is a bit of a kludge and
3027 doesn't catch all cases.
3029 But only do this if -fexpensive-optimizations since it slows
3030 things down and doesn't usually win.
3032 This is not done in the COMPARE case above because the
3033 unmodified I2PAT is used in the PARALLEL and so a pattern
3034 with a modified I2SRC would not match. */
3036 if (flag_expensive_optimizations)
3038 /* Pass pc_rtx so no substitutions are done, just
3039 simplifications. */
3040 if (i1)
3042 subst_low_luid = DF_INSN_LUID (i1);
3043 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3046 subst_low_luid = DF_INSN_LUID (i2);
3047 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3050 n_occurrences = 0; /* `subst' counts here */
3051 subst_low_luid = DF_INSN_LUID (i2);
3053 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3054 copy of I2SRC each time we substitute it, in order to avoid creating
3055 self-referential RTL when we will be substituting I1SRC for I1DEST
3056 later. Likewise if I0 feeds into I2, either directly or indirectly
3057 through I1, and I0DEST is in I0SRC. */
3058 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3059 (i1_feeds_i2_n && i1dest_in_i1src)
3060 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3061 && i0dest_in_i0src));
3062 substed_i2 = 1;
3064 /* Record whether I2's body now appears within I3's body. */
3065 i2_is_used = n_occurrences;
3068 /* If we already got a failure, don't try to do more. Otherwise, try to
3069 substitute I1 if we have it. */
3071 if (i1 && GET_CODE (newpat) != CLOBBER)
3073 /* Check that an autoincrement side-effect on I1 has not been lost.
3074 This happens if I1DEST is mentioned in I2 and dies there, and
3075 has disappeared from the new pattern. */
3076 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3077 && i1_feeds_i2_n
3078 && dead_or_set_p (i2, i1dest)
3079 && !reg_overlap_mentioned_p (i1dest, newpat))
3080 /* Before we can do this substitution, we must redo the test done
3081 above (see detailed comments there) that ensures I1DEST isn't
3082 mentioned in any SETs in NEWPAT that are field assignments. */
3083 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3084 0, 0, 0))
3086 undo_all ();
3087 return 0;
3090 n_occurrences = 0;
3091 subst_low_luid = DF_INSN_LUID (i1);
3093 /* If the following substitution will modify I1SRC, make a copy of it
3094 for the case where it is substituted for I1DEST in I2PAT later. */
3095 if (added_sets_2 && i1_feeds_i2_n)
3096 i1src_copy = copy_rtx (i1src);
3098 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3099 copy of I1SRC each time we substitute it, in order to avoid creating
3100 self-referential RTL when we will be substituting I0SRC for I0DEST
3101 later. */
3102 newpat = subst (newpat, i1dest, i1src, 0, 0,
3103 i0_feeds_i1_n && i0dest_in_i0src);
3104 substed_i1 = 1;
3106 /* Record whether I1's body now appears within I3's body. */
3107 i1_is_used = n_occurrences;
3110 /* Likewise for I0 if we have it. */
3112 if (i0 && GET_CODE (newpat) != CLOBBER)
3114 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3115 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3116 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3117 && !reg_overlap_mentioned_p (i0dest, newpat))
3118 || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3119 0, 0, 0))
3121 undo_all ();
3122 return 0;
3125 /* If the following substitution will modify I0SRC, make a copy of it
3126 for the case where it is substituted for I0DEST in I1PAT later. */
3127 if (added_sets_1 && i0_feeds_i1_n)
3128 i0src_copy = copy_rtx (i0src);
3129 /* And a copy for I0DEST in I2PAT substitution. */
3130 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3131 || (i0_feeds_i2_n)))
3132 i0src_copy2 = copy_rtx (i0src);
3134 n_occurrences = 0;
3135 subst_low_luid = DF_INSN_LUID (i0);
3136 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3137 substed_i0 = 1;
3140 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3141 to count all the ways that I2SRC and I1SRC can be used. */
3142 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3143 && i2_is_used + added_sets_2 > 1)
3144 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3145 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3146 > 1))
3147 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3148 && (n_occurrences + added_sets_0
3149 + (added_sets_1 && i0_feeds_i1_n)
3150 + (added_sets_2 && i0_feeds_i2_n)
3151 > 1))
3152 /* Fail if we tried to make a new register. */
3153 || max_reg_num () != maxreg
3154 /* Fail if we couldn't do something and have a CLOBBER. */
3155 || GET_CODE (newpat) == CLOBBER
3156 /* Fail if this new pattern is a MULT and we didn't have one before
3157 at the outer level. */
3158 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3159 && ! have_mult))
3161 undo_all ();
3162 return 0;
3165 /* If the actions of the earlier insns must be kept
3166 in addition to substituting them into the latest one,
3167 we must make a new PARALLEL for the latest insn
3168 to hold additional the SETs. */
3170 if (added_sets_0 || added_sets_1 || added_sets_2)
3172 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3173 combine_extras++;
3175 if (GET_CODE (newpat) == PARALLEL)
3177 rtvec old = XVEC (newpat, 0);
3178 total_sets = XVECLEN (newpat, 0) + extra_sets;
3179 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3180 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3181 sizeof (old->elem[0]) * old->num_elem);
3183 else
3185 rtx old = newpat;
3186 total_sets = 1 + extra_sets;
3187 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3188 XVECEXP (newpat, 0, 0) = old;
3191 if (added_sets_0)
3192 XVECEXP (newpat, 0, --total_sets) = i0pat;
3194 if (added_sets_1)
3196 rtx t = i1pat;
3197 if (i0_feeds_i1_n)
3198 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3200 XVECEXP (newpat, 0, --total_sets) = t;
3202 if (added_sets_2)
3204 rtx t = i2pat;
3205 if (i1_feeds_i2_n)
3206 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3207 i0_feeds_i1_n && i0dest_in_i0src);
3208 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3209 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3211 XVECEXP (newpat, 0, --total_sets) = t;
3215 validate_replacement:
3217 /* Note which hard regs this insn has as inputs. */
3218 mark_used_regs_combine (newpat);
3220 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3221 consider splitting this pattern, we might need these clobbers. */
3222 if (i1 && GET_CODE (newpat) == PARALLEL
3223 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3225 int len = XVECLEN (newpat, 0);
3227 newpat_vec_with_clobbers = rtvec_alloc (len);
3228 for (i = 0; i < len; i++)
3229 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3232 /* Is the result of combination a valid instruction? */
3233 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3235 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3236 the second SET's destination is a register that is unused and isn't
3237 marked as an instruction that might trap in an EH region. In that case,
3238 we just need the first SET. This can occur when simplifying a divmod
3239 insn. We *must* test for this case here because the code below that
3240 splits two independent SETs doesn't handle this case correctly when it
3241 updates the register status.
3243 It's pointless doing this if we originally had two sets, one from
3244 i3, and one from i2. Combining then splitting the parallel results
3245 in the original i2 again plus an invalid insn (which we delete).
3246 The net effect is only to move instructions around, which makes
3247 debug info less accurate.
3249 Also check the case where the first SET's destination is unused.
3250 That would not cause incorrect code, but does cause an unneeded
3251 insn to remain. */
3253 if (insn_code_number < 0
3254 && !(added_sets_2 && i1 == 0)
3255 && GET_CODE (newpat) == PARALLEL
3256 && XVECLEN (newpat, 0) == 2
3257 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3258 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3259 && asm_noperands (newpat) < 0)
3261 rtx set0 = XVECEXP (newpat, 0, 0);
3262 rtx set1 = XVECEXP (newpat, 0, 1);
3264 if (((REG_P (SET_DEST (set1))
3265 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3266 || (GET_CODE (SET_DEST (set1)) == SUBREG
3267 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3268 && insn_nothrow_p (i3)
3269 && !side_effects_p (SET_SRC (set1)))
3271 newpat = set0;
3272 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3275 else if (((REG_P (SET_DEST (set0))
3276 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3277 || (GET_CODE (SET_DEST (set0)) == SUBREG
3278 && find_reg_note (i3, REG_UNUSED,
3279 SUBREG_REG (SET_DEST (set0)))))
3280 && insn_nothrow_p (i3)
3281 && !side_effects_p (SET_SRC (set0)))
3283 newpat = set1;
3284 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3286 if (insn_code_number >= 0)
3287 changed_i3_dest = 1;
3291 /* If we were combining three insns and the result is a simple SET
3292 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3293 insns. There are two ways to do this. It can be split using a
3294 machine-specific method (like when you have an addition of a large
3295 constant) or by combine in the function find_split_point. */
3297 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3298 && asm_noperands (newpat) < 0)
3300 rtx parallel, m_split, *split;
3302 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3303 use I2DEST as a scratch register will help. In the latter case,
3304 convert I2DEST to the mode of the source of NEWPAT if we can. */
3306 m_split = combine_split_insns (newpat, i3);
3308 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3309 inputs of NEWPAT. */
3311 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3312 possible to try that as a scratch reg. This would require adding
3313 more code to make it work though. */
3315 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3317 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3319 /* First try to split using the original register as a
3320 scratch register. */
3321 parallel = gen_rtx_PARALLEL (VOIDmode,
3322 gen_rtvec (2, newpat,
3323 gen_rtx_CLOBBER (VOIDmode,
3324 i2dest)));
3325 m_split = combine_split_insns (parallel, i3);
3327 /* If that didn't work, try changing the mode of I2DEST if
3328 we can. */
3329 if (m_split == 0
3330 && new_mode != GET_MODE (i2dest)
3331 && new_mode != VOIDmode
3332 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3334 enum machine_mode old_mode = GET_MODE (i2dest);
3335 rtx ni2dest;
3337 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3338 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3339 else
3341 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3342 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3345 parallel = (gen_rtx_PARALLEL
3346 (VOIDmode,
3347 gen_rtvec (2, newpat,
3348 gen_rtx_CLOBBER (VOIDmode,
3349 ni2dest))));
3350 m_split = combine_split_insns (parallel, i3);
3352 if (m_split == 0
3353 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3355 struct undo *buf;
3357 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3358 buf = undobuf.undos;
3359 undobuf.undos = buf->next;
3360 buf->next = undobuf.frees;
3361 undobuf.frees = buf;
3365 i2scratch = m_split != 0;
3368 /* If recog_for_combine has discarded clobbers, try to use them
3369 again for the split. */
3370 if (m_split == 0 && newpat_vec_with_clobbers)
3372 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3373 m_split = combine_split_insns (parallel, i3);
3376 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3378 m_split = PATTERN (m_split);
3379 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3380 if (insn_code_number >= 0)
3381 newpat = m_split;
3383 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3384 && (next_nonnote_nondebug_insn (i2) == i3
3385 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3387 rtx i2set, i3set;
3388 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3389 newi2pat = PATTERN (m_split);
3391 i3set = single_set (NEXT_INSN (m_split));
3392 i2set = single_set (m_split);
3394 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3396 /* If I2 or I3 has multiple SETs, we won't know how to track
3397 register status, so don't use these insns. If I2's destination
3398 is used between I2 and I3, we also can't use these insns. */
3400 if (i2_code_number >= 0 && i2set && i3set
3401 && (next_nonnote_nondebug_insn (i2) == i3
3402 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3403 insn_code_number = recog_for_combine (&newi3pat, i3,
3404 &new_i3_notes);
3405 if (insn_code_number >= 0)
3406 newpat = newi3pat;
3408 /* It is possible that both insns now set the destination of I3.
3409 If so, we must show an extra use of it. */
3411 if (insn_code_number >= 0)
3413 rtx new_i3_dest = SET_DEST (i3set);
3414 rtx new_i2_dest = SET_DEST (i2set);
3416 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3417 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3418 || GET_CODE (new_i3_dest) == SUBREG)
3419 new_i3_dest = XEXP (new_i3_dest, 0);
3421 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3422 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3423 || GET_CODE (new_i2_dest) == SUBREG)
3424 new_i2_dest = XEXP (new_i2_dest, 0);
3426 if (REG_P (new_i3_dest)
3427 && REG_P (new_i2_dest)
3428 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3429 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3433 /* If we can split it and use I2DEST, go ahead and see if that
3434 helps things be recognized. Verify that none of the registers
3435 are set between I2 and I3. */
3436 if (insn_code_number < 0
3437 && (split = find_split_point (&newpat, i3, false)) != 0
3438 #ifdef HAVE_cc0
3439 && REG_P (i2dest)
3440 #endif
3441 /* We need I2DEST in the proper mode. If it is a hard register
3442 or the only use of a pseudo, we can change its mode.
3443 Make sure we don't change a hard register to have a mode that
3444 isn't valid for it, or change the number of registers. */
3445 && (GET_MODE (*split) == GET_MODE (i2dest)
3446 || GET_MODE (*split) == VOIDmode
3447 || can_change_dest_mode (i2dest, added_sets_2,
3448 GET_MODE (*split)))
3449 && (next_nonnote_nondebug_insn (i2) == i3
3450 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3451 /* We can't overwrite I2DEST if its value is still used by
3452 NEWPAT. */
3453 && ! reg_referenced_p (i2dest, newpat))
3455 rtx newdest = i2dest;
3456 enum rtx_code split_code = GET_CODE (*split);
3457 enum machine_mode split_mode = GET_MODE (*split);
3458 bool subst_done = false;
3459 newi2pat = NULL_RTX;
3461 i2scratch = true;
3463 /* *SPLIT may be part of I2SRC, so make sure we have the
3464 original expression around for later debug processing.
3465 We should not need I2SRC any more in other cases. */
3466 if (MAY_HAVE_DEBUG_INSNS)
3467 i2src = copy_rtx (i2src);
3468 else
3469 i2src = NULL;
3471 /* Get NEWDEST as a register in the proper mode. We have already
3472 validated that we can do this. */
3473 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3475 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3476 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3477 else
3479 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3480 newdest = regno_reg_rtx[REGNO (i2dest)];
3484 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3485 an ASHIFT. This can occur if it was inside a PLUS and hence
3486 appeared to be a memory address. This is a kludge. */
3487 if (split_code == MULT
3488 && CONST_INT_P (XEXP (*split, 1))
3489 && INTVAL (XEXP (*split, 1)) > 0
3490 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3492 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3493 XEXP (*split, 0), GEN_INT (i)));
3494 /* Update split_code because we may not have a multiply
3495 anymore. */
3496 split_code = GET_CODE (*split);
3499 #ifdef INSN_SCHEDULING
3500 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3501 be written as a ZERO_EXTEND. */
3502 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3504 #ifdef LOAD_EXTEND_OP
3505 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3506 what it really is. */
3507 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3508 == SIGN_EXTEND)
3509 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3510 SUBREG_REG (*split)));
3511 else
3512 #endif
3513 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3514 SUBREG_REG (*split)));
3516 #endif
3518 /* Attempt to split binary operators using arithmetic identities. */
3519 if (BINARY_P (SET_SRC (newpat))
3520 && split_mode == GET_MODE (SET_SRC (newpat))
3521 && ! side_effects_p (SET_SRC (newpat)))
3523 rtx setsrc = SET_SRC (newpat);
3524 enum machine_mode mode = GET_MODE (setsrc);
3525 enum rtx_code code = GET_CODE (setsrc);
3526 rtx src_op0 = XEXP (setsrc, 0);
3527 rtx src_op1 = XEXP (setsrc, 1);
3529 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3530 if (rtx_equal_p (src_op0, src_op1))
3532 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3533 SUBST (XEXP (setsrc, 0), newdest);
3534 SUBST (XEXP (setsrc, 1), newdest);
3535 subst_done = true;
3537 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3538 else if ((code == PLUS || code == MULT)
3539 && GET_CODE (src_op0) == code
3540 && GET_CODE (XEXP (src_op0, 0)) == code
3541 && (INTEGRAL_MODE_P (mode)
3542 || (FLOAT_MODE_P (mode)
3543 && flag_unsafe_math_optimizations)))
3545 rtx p = XEXP (XEXP (src_op0, 0), 0);
3546 rtx q = XEXP (XEXP (src_op0, 0), 1);
3547 rtx r = XEXP (src_op0, 1);
3548 rtx s = src_op1;
3550 /* Split both "((X op Y) op X) op Y" and
3551 "((X op Y) op Y) op X" as "T op T" where T is
3552 "X op Y". */
3553 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3554 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3556 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3557 XEXP (src_op0, 0));
3558 SUBST (XEXP (setsrc, 0), newdest);
3559 SUBST (XEXP (setsrc, 1), newdest);
3560 subst_done = true;
3562 /* Split "((X op X) op Y) op Y)" as "T op T" where
3563 T is "X op Y". */
3564 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3566 rtx tmp = simplify_gen_binary (code, mode, p, r);
3567 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3568 SUBST (XEXP (setsrc, 0), newdest);
3569 SUBST (XEXP (setsrc, 1), newdest);
3570 subst_done = true;
3575 if (!subst_done)
3577 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3578 SUBST (*split, newdest);
3581 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3583 /* recog_for_combine might have added CLOBBERs to newi2pat.
3584 Make sure NEWPAT does not depend on the clobbered regs. */
3585 if (GET_CODE (newi2pat) == PARALLEL)
3586 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3587 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3589 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3590 if (reg_overlap_mentioned_p (reg, newpat))
3592 undo_all ();
3593 return 0;
3597 /* If the split point was a MULT and we didn't have one before,
3598 don't use one now. */
3599 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3600 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3604 /* Check for a case where we loaded from memory in a narrow mode and
3605 then sign extended it, but we need both registers. In that case,
3606 we have a PARALLEL with both loads from the same memory location.
3607 We can split this into a load from memory followed by a register-register
3608 copy. This saves at least one insn, more if register allocation can
3609 eliminate the copy.
3611 We cannot do this if the destination of the first assignment is a
3612 condition code register or cc0. We eliminate this case by making sure
3613 the SET_DEST and SET_SRC have the same mode.
3615 We cannot do this if the destination of the second assignment is
3616 a register that we have already assumed is zero-extended. Similarly
3617 for a SUBREG of such a register. */
3619 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3620 && GET_CODE (newpat) == PARALLEL
3621 && XVECLEN (newpat, 0) == 2
3622 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3623 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3624 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3625 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3626 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3627 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3628 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3629 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3630 DF_INSN_LUID (i2))
3631 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3632 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3633 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3634 (REG_P (temp)
3635 && VEC_index (reg_stat_type, reg_stat,
3636 REGNO (temp)).nonzero_bits != 0
3637 && GET_MODE_PRECISION (GET_MODE (temp)) < BITS_PER_WORD
3638 && GET_MODE_PRECISION (GET_MODE (temp)) < HOST_BITS_PER_INT
3639 && (VEC_index (reg_stat_type, reg_stat,
3640 REGNO (temp)).nonzero_bits
3641 != GET_MODE_MASK (word_mode))))
3642 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3643 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3644 (REG_P (temp)
3645 && VEC_index (reg_stat_type, reg_stat,
3646 REGNO (temp)).nonzero_bits != 0
3647 && GET_MODE_PRECISION (GET_MODE (temp)) < BITS_PER_WORD
3648 && GET_MODE_PRECISION (GET_MODE (temp)) < HOST_BITS_PER_INT
3649 && (VEC_index (reg_stat_type, reg_stat,
3650 REGNO (temp)).nonzero_bits
3651 != GET_MODE_MASK (word_mode)))))
3652 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3653 SET_SRC (XVECEXP (newpat, 0, 1)))
3654 && ! find_reg_note (i3, REG_UNUSED,
3655 SET_DEST (XVECEXP (newpat, 0, 0))))
3657 rtx ni2dest;
3659 newi2pat = XVECEXP (newpat, 0, 0);
3660 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3661 newpat = XVECEXP (newpat, 0, 1);
3662 SUBST (SET_SRC (newpat),
3663 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3664 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3666 if (i2_code_number >= 0)
3667 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3669 if (insn_code_number >= 0)
3670 swap_i2i3 = 1;
3673 /* Similarly, check for a case where we have a PARALLEL of two independent
3674 SETs but we started with three insns. In this case, we can do the sets
3675 as two separate insns. This case occurs when some SET allows two
3676 other insns to combine, but the destination of that SET is still live. */
3678 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3679 && GET_CODE (newpat) == PARALLEL
3680 && XVECLEN (newpat, 0) == 2
3681 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3682 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3683 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3684 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3685 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3686 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3687 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3688 XVECEXP (newpat, 0, 0))
3689 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3690 XVECEXP (newpat, 0, 1))
3691 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3692 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3694 /* Normally, it doesn't matter which of the two is done first,
3695 but the one that references cc0 can't be the second, and
3696 one which uses any regs/memory set in between i2 and i3 can't
3697 be first. */
3698 if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3699 DF_INSN_LUID (i2))
3700 #ifdef HAVE_cc0
3701 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3702 #endif
3705 newi2pat = XVECEXP (newpat, 0, 1);
3706 newpat = XVECEXP (newpat, 0, 0);
3708 else if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 0)),
3709 DF_INSN_LUID (i2))
3710 #ifdef HAVE_cc0
3711 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1))
3712 #endif
3715 newi2pat = XVECEXP (newpat, 0, 0);
3716 newpat = XVECEXP (newpat, 0, 1);
3718 else
3720 undo_all ();
3721 return 0;
3724 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3726 if (i2_code_number >= 0)
3728 /* recog_for_combine might have added CLOBBERs to newi2pat.
3729 Make sure NEWPAT does not depend on the clobbered regs. */
3730 if (GET_CODE (newi2pat) == PARALLEL)
3732 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3733 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3735 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3736 if (reg_overlap_mentioned_p (reg, newpat))
3738 undo_all ();
3739 return 0;
3744 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3748 /* If it still isn't recognized, fail and change things back the way they
3749 were. */
3750 if ((insn_code_number < 0
3751 /* Is the result a reasonable ASM_OPERANDS? */
3752 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3754 undo_all ();
3755 return 0;
3758 /* If we had to change another insn, make sure it is valid also. */
3759 if (undobuf.other_insn)
3761 CLEAR_HARD_REG_SET (newpat_used_regs);
3763 other_pat = PATTERN (undobuf.other_insn);
3764 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3765 &new_other_notes);
3767 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3769 undo_all ();
3770 return 0;
3774 #ifdef HAVE_cc0
3775 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3776 they are adjacent to each other or not. */
3778 rtx p = prev_nonnote_insn (i3);
3779 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3780 && sets_cc0_p (newi2pat))
3782 undo_all ();
3783 return 0;
3786 #endif
3788 /* Only allow this combination if insn_rtx_costs reports that the
3789 replacement instructions are cheaper than the originals. */
3790 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3792 undo_all ();
3793 return 0;
3796 if (MAY_HAVE_DEBUG_INSNS)
3798 struct undo *undo;
3800 for (undo = undobuf.undos; undo; undo = undo->next)
3801 if (undo->kind == UNDO_MODE)
3803 rtx reg = *undo->where.r;
3804 enum machine_mode new_mode = GET_MODE (reg);
3805 enum machine_mode old_mode = undo->old_contents.m;
3807 /* Temporarily revert mode back. */
3808 adjust_reg_mode (reg, old_mode);
3810 if (reg == i2dest && i2scratch)
3812 /* If we used i2dest as a scratch register with a
3813 different mode, substitute it for the original
3814 i2src while its original mode is temporarily
3815 restored, and then clear i2scratch so that we don't
3816 do it again later. */
3817 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3818 this_basic_block);
3819 i2scratch = false;
3820 /* Put back the new mode. */
3821 adjust_reg_mode (reg, new_mode);
3823 else
3825 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3826 rtx first, last;
3828 if (reg == i2dest)
3830 first = i2;
3831 last = last_combined_insn;
3833 else
3835 first = i3;
3836 last = undobuf.other_insn;
3837 gcc_assert (last);
3838 if (DF_INSN_LUID (last)
3839 < DF_INSN_LUID (last_combined_insn))
3840 last = last_combined_insn;
3843 /* We're dealing with a reg that changed mode but not
3844 meaning, so we want to turn it into a subreg for
3845 the new mode. However, because of REG sharing and
3846 because its mode had already changed, we have to do
3847 it in two steps. First, replace any debug uses of
3848 reg, with its original mode temporarily restored,
3849 with this copy we have created; then, replace the
3850 copy with the SUBREG of the original shared reg,
3851 once again changed to the new mode. */
3852 propagate_for_debug (first, last, reg, tempreg,
3853 this_basic_block);
3854 adjust_reg_mode (reg, new_mode);
3855 propagate_for_debug (first, last, tempreg,
3856 lowpart_subreg (old_mode, reg, new_mode),
3857 this_basic_block);
3862 /* If we will be able to accept this, we have made a
3863 change to the destination of I3. This requires us to
3864 do a few adjustments. */
3866 if (changed_i3_dest)
3868 PATTERN (i3) = newpat;
3869 adjust_for_new_dest (i3);
3872 /* We now know that we can do this combination. Merge the insns and
3873 update the status of registers and LOG_LINKS. */
3875 if (undobuf.other_insn)
3877 rtx note, next;
3879 PATTERN (undobuf.other_insn) = other_pat;
3881 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3882 are still valid. Then add any non-duplicate notes added by
3883 recog_for_combine. */
3884 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3886 next = XEXP (note, 1);
3888 if (REG_NOTE_KIND (note) == REG_UNUSED
3889 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3890 remove_note (undobuf.other_insn, note);
3893 distribute_notes (new_other_notes, undobuf.other_insn,
3894 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
3895 NULL_RTX);
3898 if (swap_i2i3)
3900 rtx insn;
3901 struct insn_link *link;
3902 rtx ni2dest;
3904 /* I3 now uses what used to be its destination and which is now
3905 I2's destination. This requires us to do a few adjustments. */
3906 PATTERN (i3) = newpat;
3907 adjust_for_new_dest (i3);
3909 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3910 so we still will.
3912 However, some later insn might be using I2's dest and have
3913 a LOG_LINK pointing at I3. We must remove this link.
3914 The simplest way to remove the link is to point it at I1,
3915 which we know will be a NOTE. */
3917 /* newi2pat is usually a SET here; however, recog_for_combine might
3918 have added some clobbers. */
3919 if (GET_CODE (newi2pat) == PARALLEL)
3920 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3921 else
3922 ni2dest = SET_DEST (newi2pat);
3924 for (insn = NEXT_INSN (i3);
3925 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3926 || insn != BB_HEAD (this_basic_block->next_bb));
3927 insn = NEXT_INSN (insn))
3929 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3931 FOR_EACH_LOG_LINK (link, insn)
3932 if (link->insn == i3)
3933 link->insn = i1;
3935 break;
3941 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3942 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
3943 rtx midnotes = 0;
3944 int from_luid;
3945 /* Compute which registers we expect to eliminate. newi2pat may be setting
3946 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3947 same as i3dest, in which case newi2pat may be setting i1dest. */
3948 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3949 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
3950 || !i2dest_killed
3951 ? 0 : i2dest);
3952 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
3953 || (newi2pat && reg_set_p (i1dest, newi2pat))
3954 || !i1dest_killed
3955 ? 0 : i1dest);
3956 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
3957 || (newi2pat && reg_set_p (i0dest, newi2pat))
3958 || !i0dest_killed
3959 ? 0 : i0dest);
3961 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3962 clear them. */
3963 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3964 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3965 if (i1)
3966 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3967 if (i0)
3968 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
3970 /* Ensure that we do not have something that should not be shared but
3971 occurs multiple times in the new insns. Check this by first
3972 resetting all the `used' flags and then copying anything is shared. */
3974 reset_used_flags (i3notes);
3975 reset_used_flags (i2notes);
3976 reset_used_flags (i1notes);
3977 reset_used_flags (i0notes);
3978 reset_used_flags (newpat);
3979 reset_used_flags (newi2pat);
3980 if (undobuf.other_insn)
3981 reset_used_flags (PATTERN (undobuf.other_insn));
3983 i3notes = copy_rtx_if_shared (i3notes);
3984 i2notes = copy_rtx_if_shared (i2notes);
3985 i1notes = copy_rtx_if_shared (i1notes);
3986 i0notes = copy_rtx_if_shared (i0notes);
3987 newpat = copy_rtx_if_shared (newpat);
3988 newi2pat = copy_rtx_if_shared (newi2pat);
3989 if (undobuf.other_insn)
3990 reset_used_flags (PATTERN (undobuf.other_insn));
3992 INSN_CODE (i3) = insn_code_number;
3993 PATTERN (i3) = newpat;
3995 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3997 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3999 reset_used_flags (call_usage);
4000 call_usage = copy_rtx (call_usage);
4002 if (substed_i2)
4004 /* I2SRC must still be meaningful at this point. Some splitting
4005 operations can invalidate I2SRC, but those operations do not
4006 apply to calls. */
4007 gcc_assert (i2src);
4008 replace_rtx (call_usage, i2dest, i2src);
4011 if (substed_i1)
4012 replace_rtx (call_usage, i1dest, i1src);
4013 if (substed_i0)
4014 replace_rtx (call_usage, i0dest, i0src);
4016 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4019 if (undobuf.other_insn)
4020 INSN_CODE (undobuf.other_insn) = other_code_number;
4022 /* We had one special case above where I2 had more than one set and
4023 we replaced a destination of one of those sets with the destination
4024 of I3. In that case, we have to update LOG_LINKS of insns later
4025 in this basic block. Note that this (expensive) case is rare.
4027 Also, in this case, we must pretend that all REG_NOTEs for I2
4028 actually came from I3, so that REG_UNUSED notes from I2 will be
4029 properly handled. */
4031 if (i3_subst_into_i2)
4033 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4034 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4035 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4036 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4037 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4038 && ! find_reg_note (i2, REG_UNUSED,
4039 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4040 for (temp = NEXT_INSN (i2);
4041 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4042 || BB_HEAD (this_basic_block) != temp);
4043 temp = NEXT_INSN (temp))
4044 if (temp != i3 && INSN_P (temp))
4045 FOR_EACH_LOG_LINK (link, temp)
4046 if (link->insn == i2)
4047 link->insn = i3;
4049 if (i3notes)
4051 rtx link = i3notes;
4052 while (XEXP (link, 1))
4053 link = XEXP (link, 1);
4054 XEXP (link, 1) = i2notes;
4056 else
4057 i3notes = i2notes;
4058 i2notes = 0;
4061 LOG_LINKS (i3) = NULL;
4062 REG_NOTES (i3) = 0;
4063 LOG_LINKS (i2) = NULL;
4064 REG_NOTES (i2) = 0;
4066 if (newi2pat)
4068 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4069 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4070 this_basic_block);
4071 INSN_CODE (i2) = i2_code_number;
4072 PATTERN (i2) = newi2pat;
4074 else
4076 if (MAY_HAVE_DEBUG_INSNS && i2src)
4077 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4078 this_basic_block);
4079 SET_INSN_DELETED (i2);
4082 if (i1)
4084 LOG_LINKS (i1) = NULL;
4085 REG_NOTES (i1) = 0;
4086 if (MAY_HAVE_DEBUG_INSNS)
4087 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4088 this_basic_block);
4089 SET_INSN_DELETED (i1);
4092 if (i0)
4094 LOG_LINKS (i0) = NULL;
4095 REG_NOTES (i0) = 0;
4096 if (MAY_HAVE_DEBUG_INSNS)
4097 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4098 this_basic_block);
4099 SET_INSN_DELETED (i0);
4102 /* Get death notes for everything that is now used in either I3 or
4103 I2 and used to die in a previous insn. If we built two new
4104 patterns, move from I1 to I2 then I2 to I3 so that we get the
4105 proper movement on registers that I2 modifies. */
4107 if (i0)
4108 from_luid = DF_INSN_LUID (i0);
4109 else if (i1)
4110 from_luid = DF_INSN_LUID (i1);
4111 else
4112 from_luid = DF_INSN_LUID (i2);
4113 if (newi2pat)
4114 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4115 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4117 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4118 if (i3notes)
4119 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4120 elim_i2, elim_i1, elim_i0);
4121 if (i2notes)
4122 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4123 elim_i2, elim_i1, elim_i0);
4124 if (i1notes)
4125 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4126 elim_i2, elim_i1, elim_i0);
4127 if (i0notes)
4128 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4129 elim_i2, elim_i1, elim_i0);
4130 if (midnotes)
4131 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4132 elim_i2, elim_i1, elim_i0);
4134 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4135 know these are REG_UNUSED and want them to go to the desired insn,
4136 so we always pass it as i3. */
4138 if (newi2pat && new_i2_notes)
4139 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4140 NULL_RTX);
4142 if (new_i3_notes)
4143 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4144 NULL_RTX);
4146 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4147 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4148 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4149 in that case, it might delete I2. Similarly for I2 and I1.
4150 Show an additional death due to the REG_DEAD note we make here. If
4151 we discard it in distribute_notes, we will decrement it again. */
4153 if (i3dest_killed)
4155 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4156 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4157 NULL_RTX),
4158 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
4159 else
4160 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4161 NULL_RTX),
4162 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4163 elim_i2, elim_i1, elim_i0);
4166 if (i2dest_in_i2src)
4168 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4169 if (newi2pat && reg_set_p (i2dest, newi2pat))
4170 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4171 NULL_RTX, NULL_RTX);
4172 else
4173 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4174 NULL_RTX, NULL_RTX, NULL_RTX);
4177 if (i1dest_in_i1src)
4179 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4180 if (newi2pat && reg_set_p (i1dest, newi2pat))
4181 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4182 NULL_RTX, NULL_RTX);
4183 else
4184 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4185 NULL_RTX, NULL_RTX, NULL_RTX);
4188 if (i0dest_in_i0src)
4190 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4191 if (newi2pat && reg_set_p (i0dest, newi2pat))
4192 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4193 NULL_RTX, NULL_RTX);
4194 else
4195 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4196 NULL_RTX, NULL_RTX, NULL_RTX);
4199 distribute_links (i3links);
4200 distribute_links (i2links);
4201 distribute_links (i1links);
4202 distribute_links (i0links);
4204 if (REG_P (i2dest))
4206 struct insn_link *link;
4207 rtx i2_insn = 0, i2_val = 0, set;
4209 /* The insn that used to set this register doesn't exist, and
4210 this life of the register may not exist either. See if one of
4211 I3's links points to an insn that sets I2DEST. If it does,
4212 that is now the last known value for I2DEST. If we don't update
4213 this and I2 set the register to a value that depended on its old
4214 contents, we will get confused. If this insn is used, thing
4215 will be set correctly in combine_instructions. */
4216 FOR_EACH_LOG_LINK (link, i3)
4217 if ((set = single_set (link->insn)) != 0
4218 && rtx_equal_p (i2dest, SET_DEST (set)))
4219 i2_insn = link->insn, i2_val = SET_SRC (set);
4221 record_value_for_reg (i2dest, i2_insn, i2_val);
4223 /* If the reg formerly set in I2 died only once and that was in I3,
4224 zero its use count so it won't make `reload' do any work. */
4225 if (! added_sets_2
4226 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4227 && ! i2dest_in_i2src)
4228 INC_REG_N_SETS (REGNO (i2dest), -1);
4231 if (i1 && REG_P (i1dest))
4233 struct insn_link *link;
4234 rtx i1_insn = 0, i1_val = 0, set;
4236 FOR_EACH_LOG_LINK (link, i3)
4237 if ((set = single_set (link->insn)) != 0
4238 && rtx_equal_p (i1dest, SET_DEST (set)))
4239 i1_insn = link->insn, i1_val = SET_SRC (set);
4241 record_value_for_reg (i1dest, i1_insn, i1_val);
4243 if (! added_sets_1 && ! i1dest_in_i1src)
4244 INC_REG_N_SETS (REGNO (i1dest), -1);
4247 if (i0 && REG_P (i0dest))
4249 struct insn_link *link;
4250 rtx i0_insn = 0, i0_val = 0, set;
4252 FOR_EACH_LOG_LINK (link, i3)
4253 if ((set = single_set (link->insn)) != 0
4254 && rtx_equal_p (i0dest, SET_DEST (set)))
4255 i0_insn = link->insn, i0_val = SET_SRC (set);
4257 record_value_for_reg (i0dest, i0_insn, i0_val);
4259 if (! added_sets_0 && ! i0dest_in_i0src)
4260 INC_REG_N_SETS (REGNO (i0dest), -1);
4263 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4264 been made to this insn. The order of
4265 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
4266 can affect nonzero_bits of newpat */
4267 if (newi2pat)
4268 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4269 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4272 if (undobuf.other_insn != NULL_RTX)
4274 if (dump_file)
4276 fprintf (dump_file, "modifying other_insn ");
4277 dump_insn_slim (dump_file, undobuf.other_insn);
4279 df_insn_rescan (undobuf.other_insn);
4282 if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4284 if (dump_file)
4286 fprintf (dump_file, "modifying insn i1 ");
4287 dump_insn_slim (dump_file, i0);
4289 df_insn_rescan (i0);
4292 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4294 if (dump_file)
4296 fprintf (dump_file, "modifying insn i1 ");
4297 dump_insn_slim (dump_file, i1);
4299 df_insn_rescan (i1);
4302 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4304 if (dump_file)
4306 fprintf (dump_file, "modifying insn i2 ");
4307 dump_insn_slim (dump_file, i2);
4309 df_insn_rescan (i2);
4312 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4314 if (dump_file)
4316 fprintf (dump_file, "modifying insn i3 ");
4317 dump_insn_slim (dump_file, i3);
4319 df_insn_rescan (i3);
4322 /* Set new_direct_jump_p if a new return or simple jump instruction
4323 has been created. Adjust the CFG accordingly. */
4325 if (returnjump_p (i3) || any_uncondjump_p (i3))
4327 *new_direct_jump_p = 1;
4328 mark_jump_label (PATTERN (i3), i3, 0);
4329 update_cfg_for_uncondjump (i3);
4332 if (undobuf.other_insn != NULL_RTX
4333 && (returnjump_p (undobuf.other_insn)
4334 || any_uncondjump_p (undobuf.other_insn)))
4336 *new_direct_jump_p = 1;
4337 update_cfg_for_uncondjump (undobuf.other_insn);
4340 /* A noop might also need cleaning up of CFG, if it comes from the
4341 simplification of a jump. */
4342 if (JUMP_P (i3)
4343 && GET_CODE (newpat) == SET
4344 && SET_SRC (newpat) == pc_rtx
4345 && SET_DEST (newpat) == pc_rtx)
4347 *new_direct_jump_p = 1;
4348 update_cfg_for_uncondjump (i3);
4351 if (undobuf.other_insn != NULL_RTX
4352 && JUMP_P (undobuf.other_insn)
4353 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4354 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4355 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4357 *new_direct_jump_p = 1;
4358 update_cfg_for_uncondjump (undobuf.other_insn);
4361 combine_successes++;
4362 undo_commit ();
4364 if (added_links_insn
4365 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4366 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4367 return added_links_insn;
4368 else
4369 return newi2pat ? i2 : i3;
4372 /* Undo all the modifications recorded in undobuf. */
4374 static void
4375 undo_all (void)
4377 struct undo *undo, *next;
4379 for (undo = undobuf.undos; undo; undo = next)
4381 next = undo->next;
4382 switch (undo->kind)
4384 case UNDO_RTX:
4385 *undo->where.r = undo->old_contents.r;
4386 break;
4387 case UNDO_INT:
4388 *undo->where.i = undo->old_contents.i;
4389 break;
4390 case UNDO_MODE:
4391 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4392 break;
4393 case UNDO_LINKS:
4394 *undo->where.l = undo->old_contents.l;
4395 break;
4396 default:
4397 gcc_unreachable ();
4400 undo->next = undobuf.frees;
4401 undobuf.frees = undo;
4404 undobuf.undos = 0;
4407 /* We've committed to accepting the changes we made. Move all
4408 of the undos to the free list. */
4410 static void
4411 undo_commit (void)
4413 struct undo *undo, *next;
4415 for (undo = undobuf.undos; undo; undo = next)
4417 next = undo->next;
4418 undo->next = undobuf.frees;
4419 undobuf.frees = undo;
4421 undobuf.undos = 0;
4424 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4425 where we have an arithmetic expression and return that point. LOC will
4426 be inside INSN.
4428 try_combine will call this function to see if an insn can be split into
4429 two insns. */
4431 static rtx *
4432 find_split_point (rtx *loc, rtx insn, bool set_src)
4434 rtx x = *loc;
4435 enum rtx_code code = GET_CODE (x);
4436 rtx *split;
4437 unsigned HOST_WIDE_INT len = 0;
4438 HOST_WIDE_INT pos = 0;
4439 int unsignedp = 0;
4440 rtx inner = NULL_RTX;
4442 /* First special-case some codes. */
4443 switch (code)
4445 case SUBREG:
4446 #ifdef INSN_SCHEDULING
4447 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4448 point. */
4449 if (MEM_P (SUBREG_REG (x)))
4450 return loc;
4451 #endif
4452 return find_split_point (&SUBREG_REG (x), insn, false);
4454 case MEM:
4455 #ifdef HAVE_lo_sum
4456 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4457 using LO_SUM and HIGH. */
4458 if (GET_CODE (XEXP (x, 0)) == CONST
4459 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4461 enum machine_mode address_mode = get_address_mode (x);
4463 SUBST (XEXP (x, 0),
4464 gen_rtx_LO_SUM (address_mode,
4465 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4466 XEXP (x, 0)));
4467 return &XEXP (XEXP (x, 0), 0);
4469 #endif
4471 /* If we have a PLUS whose second operand is a constant and the
4472 address is not valid, perhaps will can split it up using
4473 the machine-specific way to split large constants. We use
4474 the first pseudo-reg (one of the virtual regs) as a placeholder;
4475 it will not remain in the result. */
4476 if (GET_CODE (XEXP (x, 0)) == PLUS
4477 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4478 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4479 MEM_ADDR_SPACE (x)))
4481 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4482 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4483 XEXP (x, 0)),
4484 subst_insn);
4486 /* This should have produced two insns, each of which sets our
4487 placeholder. If the source of the second is a valid address,
4488 we can make put both sources together and make a split point
4489 in the middle. */
4491 if (seq
4492 && NEXT_INSN (seq) != NULL_RTX
4493 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4494 && NONJUMP_INSN_P (seq)
4495 && GET_CODE (PATTERN (seq)) == SET
4496 && SET_DEST (PATTERN (seq)) == reg
4497 && ! reg_mentioned_p (reg,
4498 SET_SRC (PATTERN (seq)))
4499 && NONJUMP_INSN_P (NEXT_INSN (seq))
4500 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4501 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4502 && memory_address_addr_space_p
4503 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4504 MEM_ADDR_SPACE (x)))
4506 rtx src1 = SET_SRC (PATTERN (seq));
4507 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4509 /* Replace the placeholder in SRC2 with SRC1. If we can
4510 find where in SRC2 it was placed, that can become our
4511 split point and we can replace this address with SRC2.
4512 Just try two obvious places. */
4514 src2 = replace_rtx (src2, reg, src1);
4515 split = 0;
4516 if (XEXP (src2, 0) == src1)
4517 split = &XEXP (src2, 0);
4518 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4519 && XEXP (XEXP (src2, 0), 0) == src1)
4520 split = &XEXP (XEXP (src2, 0), 0);
4522 if (split)
4524 SUBST (XEXP (x, 0), src2);
4525 return split;
4529 /* If that didn't work, perhaps the first operand is complex and
4530 needs to be computed separately, so make a split point there.
4531 This will occur on machines that just support REG + CONST
4532 and have a constant moved through some previous computation. */
4534 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4535 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4536 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4537 return &XEXP (XEXP (x, 0), 0);
4540 /* If we have a PLUS whose first operand is complex, try computing it
4541 separately by making a split there. */
4542 if (GET_CODE (XEXP (x, 0)) == PLUS
4543 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4544 MEM_ADDR_SPACE (x))
4545 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4546 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4547 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4548 return &XEXP (XEXP (x, 0), 0);
4549 break;
4551 case SET:
4552 #ifdef HAVE_cc0
4553 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4554 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4555 we need to put the operand into a register. So split at that
4556 point. */
4558 if (SET_DEST (x) == cc0_rtx
4559 && GET_CODE (SET_SRC (x)) != COMPARE
4560 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4561 && !OBJECT_P (SET_SRC (x))
4562 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4563 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4564 return &SET_SRC (x);
4565 #endif
4567 /* See if we can split SET_SRC as it stands. */
4568 split = find_split_point (&SET_SRC (x), insn, true);
4569 if (split && split != &SET_SRC (x))
4570 return split;
4572 /* See if we can split SET_DEST as it stands. */
4573 split = find_split_point (&SET_DEST (x), insn, false);
4574 if (split && split != &SET_DEST (x))
4575 return split;
4577 /* See if this is a bitfield assignment with everything constant. If
4578 so, this is an IOR of an AND, so split it into that. */
4579 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4580 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4581 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4582 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4583 && CONST_INT_P (SET_SRC (x))
4584 && ((INTVAL (XEXP (SET_DEST (x), 1))
4585 + INTVAL (XEXP (SET_DEST (x), 2)))
4586 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4587 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4589 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4590 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4591 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4592 rtx dest = XEXP (SET_DEST (x), 0);
4593 enum machine_mode mode = GET_MODE (dest);
4594 unsigned HOST_WIDE_INT mask
4595 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4596 rtx or_mask;
4598 if (BITS_BIG_ENDIAN)
4599 pos = GET_MODE_PRECISION (mode) - len - pos;
4601 or_mask = gen_int_mode (src << pos, mode);
4602 if (src == mask)
4603 SUBST (SET_SRC (x),
4604 simplify_gen_binary (IOR, mode, dest, or_mask));
4605 else
4607 rtx negmask = gen_int_mode (~(mask << pos), mode);
4608 SUBST (SET_SRC (x),
4609 simplify_gen_binary (IOR, mode,
4610 simplify_gen_binary (AND, mode,
4611 dest, negmask),
4612 or_mask));
4615 SUBST (SET_DEST (x), dest);
4617 split = find_split_point (&SET_SRC (x), insn, true);
4618 if (split && split != &SET_SRC (x))
4619 return split;
4622 /* Otherwise, see if this is an operation that we can split into two.
4623 If so, try to split that. */
4624 code = GET_CODE (SET_SRC (x));
4626 switch (code)
4628 case AND:
4629 /* If we are AND'ing with a large constant that is only a single
4630 bit and the result is only being used in a context where we
4631 need to know if it is zero or nonzero, replace it with a bit
4632 extraction. This will avoid the large constant, which might
4633 have taken more than one insn to make. If the constant were
4634 not a valid argument to the AND but took only one insn to make,
4635 this is no worse, but if it took more than one insn, it will
4636 be better. */
4638 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4639 && REG_P (XEXP (SET_SRC (x), 0))
4640 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4641 && REG_P (SET_DEST (x))
4642 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4643 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4644 && XEXP (*split, 0) == SET_DEST (x)
4645 && XEXP (*split, 1) == const0_rtx)
4647 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4648 XEXP (SET_SRC (x), 0),
4649 pos, NULL_RTX, 1, 1, 0, 0);
4650 if (extraction != 0)
4652 SUBST (SET_SRC (x), extraction);
4653 return find_split_point (loc, insn, false);
4656 break;
4658 case NE:
4659 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4660 is known to be on, this can be converted into a NEG of a shift. */
4661 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4662 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4663 && 1 <= (pos = exact_log2
4664 (nonzero_bits (XEXP (SET_SRC (x), 0),
4665 GET_MODE (XEXP (SET_SRC (x), 0))))))
4667 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4669 SUBST (SET_SRC (x),
4670 gen_rtx_NEG (mode,
4671 gen_rtx_LSHIFTRT (mode,
4672 XEXP (SET_SRC (x), 0),
4673 GEN_INT (pos))));
4675 split = find_split_point (&SET_SRC (x), insn, true);
4676 if (split && split != &SET_SRC (x))
4677 return split;
4679 break;
4681 case SIGN_EXTEND:
4682 inner = XEXP (SET_SRC (x), 0);
4684 /* We can't optimize if either mode is a partial integer
4685 mode as we don't know how many bits are significant
4686 in those modes. */
4687 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4688 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4689 break;
4691 pos = 0;
4692 len = GET_MODE_PRECISION (GET_MODE (inner));
4693 unsignedp = 0;
4694 break;
4696 case SIGN_EXTRACT:
4697 case ZERO_EXTRACT:
4698 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4699 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4701 inner = XEXP (SET_SRC (x), 0);
4702 len = INTVAL (XEXP (SET_SRC (x), 1));
4703 pos = INTVAL (XEXP (SET_SRC (x), 2));
4705 if (BITS_BIG_ENDIAN)
4706 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4707 unsignedp = (code == ZERO_EXTRACT);
4709 break;
4711 default:
4712 break;
4715 if (len && pos >= 0
4716 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4718 enum machine_mode mode = GET_MODE (SET_SRC (x));
4720 /* For unsigned, we have a choice of a shift followed by an
4721 AND or two shifts. Use two shifts for field sizes where the
4722 constant might be too large. We assume here that we can
4723 always at least get 8-bit constants in an AND insn, which is
4724 true for every current RISC. */
4726 if (unsignedp && len <= 8)
4728 SUBST (SET_SRC (x),
4729 gen_rtx_AND (mode,
4730 gen_rtx_LSHIFTRT
4731 (mode, gen_lowpart (mode, inner),
4732 GEN_INT (pos)),
4733 GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4734 - 1)));
4736 split = find_split_point (&SET_SRC (x), insn, true);
4737 if (split && split != &SET_SRC (x))
4738 return split;
4740 else
4742 SUBST (SET_SRC (x),
4743 gen_rtx_fmt_ee
4744 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4745 gen_rtx_ASHIFT (mode,
4746 gen_lowpart (mode, inner),
4747 GEN_INT (GET_MODE_PRECISION (mode)
4748 - len - pos)),
4749 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4751 split = find_split_point (&SET_SRC (x), insn, true);
4752 if (split && split != &SET_SRC (x))
4753 return split;
4757 /* See if this is a simple operation with a constant as the second
4758 operand. It might be that this constant is out of range and hence
4759 could be used as a split point. */
4760 if (BINARY_P (SET_SRC (x))
4761 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4762 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4763 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4764 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4765 return &XEXP (SET_SRC (x), 1);
4767 /* Finally, see if this is a simple operation with its first operand
4768 not in a register. The operation might require this operand in a
4769 register, so return it as a split point. We can always do this
4770 because if the first operand were another operation, we would have
4771 already found it as a split point. */
4772 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4773 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4774 return &XEXP (SET_SRC (x), 0);
4776 return 0;
4778 case AND:
4779 case IOR:
4780 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4781 it is better to write this as (not (ior A B)) so we can split it.
4782 Similarly for IOR. */
4783 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4785 SUBST (*loc,
4786 gen_rtx_NOT (GET_MODE (x),
4787 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4788 GET_MODE (x),
4789 XEXP (XEXP (x, 0), 0),
4790 XEXP (XEXP (x, 1), 0))));
4791 return find_split_point (loc, insn, set_src);
4794 /* Many RISC machines have a large set of logical insns. If the
4795 second operand is a NOT, put it first so we will try to split the
4796 other operand first. */
4797 if (GET_CODE (XEXP (x, 1)) == NOT)
4799 rtx tem = XEXP (x, 0);
4800 SUBST (XEXP (x, 0), XEXP (x, 1));
4801 SUBST (XEXP (x, 1), tem);
4803 break;
4805 case PLUS:
4806 case MINUS:
4807 /* Canonicalization can produce (minus A (mult B C)), where C is a
4808 constant. It may be better to try splitting (plus (mult B -C) A)
4809 instead if this isn't a multiply by a power of two. */
4810 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4811 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4812 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4814 enum machine_mode mode = GET_MODE (x);
4815 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4816 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4817 SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4818 XEXP (XEXP (x, 1), 0),
4819 GEN_INT (other_int)),
4820 XEXP (x, 0)));
4821 return find_split_point (loc, insn, set_src);
4824 /* Split at a multiply-accumulate instruction. However if this is
4825 the SET_SRC, we likely do not have such an instruction and it's
4826 worthless to try this split. */
4827 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4828 return loc;
4830 default:
4831 break;
4834 /* Otherwise, select our actions depending on our rtx class. */
4835 switch (GET_RTX_CLASS (code))
4837 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4838 case RTX_TERNARY:
4839 split = find_split_point (&XEXP (x, 2), insn, false);
4840 if (split)
4841 return split;
4842 /* ... fall through ... */
4843 case RTX_BIN_ARITH:
4844 case RTX_COMM_ARITH:
4845 case RTX_COMPARE:
4846 case RTX_COMM_COMPARE:
4847 split = find_split_point (&XEXP (x, 1), insn, false);
4848 if (split)
4849 return split;
4850 /* ... fall through ... */
4851 case RTX_UNARY:
4852 /* Some machines have (and (shift ...) ...) insns. If X is not
4853 an AND, but XEXP (X, 0) is, use it as our split point. */
4854 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4855 return &XEXP (x, 0);
4857 split = find_split_point (&XEXP (x, 0), insn, false);
4858 if (split)
4859 return split;
4860 return loc;
4862 default:
4863 /* Otherwise, we don't have a split point. */
4864 return 0;
4868 /* Throughout X, replace FROM with TO, and return the result.
4869 The result is TO if X is FROM;
4870 otherwise the result is X, but its contents may have been modified.
4871 If they were modified, a record was made in undobuf so that
4872 undo_all will (among other things) return X to its original state.
4874 If the number of changes necessary is too much to record to undo,
4875 the excess changes are not made, so the result is invalid.
4876 The changes already made can still be undone.
4877 undobuf.num_undo is incremented for such changes, so by testing that
4878 the caller can tell whether the result is valid.
4880 `n_occurrences' is incremented each time FROM is replaced.
4882 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4884 IN_COND is nonzero if we are at the top level of a condition.
4886 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4887 by copying if `n_occurrences' is nonzero. */
4889 static rtx
4890 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4892 enum rtx_code code = GET_CODE (x);
4893 enum machine_mode op0_mode = VOIDmode;
4894 const char *fmt;
4895 int len, i;
4896 rtx new_rtx;
4898 /* Two expressions are equal if they are identical copies of a shared
4899 RTX or if they are both registers with the same register number
4900 and mode. */
4902 #define COMBINE_RTX_EQUAL_P(X,Y) \
4903 ((X) == (Y) \
4904 || (REG_P (X) && REG_P (Y) \
4905 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4907 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4909 n_occurrences++;
4910 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4913 /* If X and FROM are the same register but different modes, they
4914 will not have been seen as equal above. However, the log links code
4915 will make a LOG_LINKS entry for that case. If we do nothing, we
4916 will try to rerecognize our original insn and, when it succeeds,
4917 we will delete the feeding insn, which is incorrect.
4919 So force this insn not to match in this (rare) case. */
4920 if (! in_dest && code == REG && REG_P (from)
4921 && reg_overlap_mentioned_p (x, from))
4922 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4924 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4925 of which may contain things that can be combined. */
4926 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4927 return x;
4929 /* It is possible to have a subexpression appear twice in the insn.
4930 Suppose that FROM is a register that appears within TO.
4931 Then, after that subexpression has been scanned once by `subst',
4932 the second time it is scanned, TO may be found. If we were
4933 to scan TO here, we would find FROM within it and create a
4934 self-referent rtl structure which is completely wrong. */
4935 if (COMBINE_RTX_EQUAL_P (x, to))
4936 return to;
4938 /* Parallel asm_operands need special attention because all of the
4939 inputs are shared across the arms. Furthermore, unsharing the
4940 rtl results in recognition failures. Failure to handle this case
4941 specially can result in circular rtl.
4943 Solve this by doing a normal pass across the first entry of the
4944 parallel, and only processing the SET_DESTs of the subsequent
4945 entries. Ug. */
4947 if (code == PARALLEL
4948 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4949 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4951 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
4953 /* If this substitution failed, this whole thing fails. */
4954 if (GET_CODE (new_rtx) == CLOBBER
4955 && XEXP (new_rtx, 0) == const0_rtx)
4956 return new_rtx;
4958 SUBST (XVECEXP (x, 0, 0), new_rtx);
4960 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4962 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4964 if (!REG_P (dest)
4965 && GET_CODE (dest) != CC0
4966 && GET_CODE (dest) != PC)
4968 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
4970 /* If this substitution failed, this whole thing fails. */
4971 if (GET_CODE (new_rtx) == CLOBBER
4972 && XEXP (new_rtx, 0) == const0_rtx)
4973 return new_rtx;
4975 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4979 else
4981 len = GET_RTX_LENGTH (code);
4982 fmt = GET_RTX_FORMAT (code);
4984 /* We don't need to process a SET_DEST that is a register, CC0,
4985 or PC, so set up to skip this common case. All other cases
4986 where we want to suppress replacing something inside a
4987 SET_SRC are handled via the IN_DEST operand. */
4988 if (code == SET
4989 && (REG_P (SET_DEST (x))
4990 || GET_CODE (SET_DEST (x)) == CC0
4991 || GET_CODE (SET_DEST (x)) == PC))
4992 fmt = "ie";
4994 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4995 constant. */
4996 if (fmt[0] == 'e')
4997 op0_mode = GET_MODE (XEXP (x, 0));
4999 for (i = 0; i < len; i++)
5001 if (fmt[i] == 'E')
5003 int j;
5004 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5006 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5008 new_rtx = (unique_copy && n_occurrences
5009 ? copy_rtx (to) : to);
5010 n_occurrences++;
5012 else
5014 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5015 unique_copy);
5017 /* If this substitution failed, this whole thing
5018 fails. */
5019 if (GET_CODE (new_rtx) == CLOBBER
5020 && XEXP (new_rtx, 0) == const0_rtx)
5021 return new_rtx;
5024 SUBST (XVECEXP (x, i, j), new_rtx);
5027 else if (fmt[i] == 'e')
5029 /* If this is a register being set, ignore it. */
5030 new_rtx = XEXP (x, i);
5031 if (in_dest
5032 && i == 0
5033 && (((code == SUBREG || code == ZERO_EXTRACT)
5034 && REG_P (new_rtx))
5035 || code == STRICT_LOW_PART))
5038 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5040 /* In general, don't install a subreg involving two
5041 modes not tieable. It can worsen register
5042 allocation, and can even make invalid reload
5043 insns, since the reg inside may need to be copied
5044 from in the outside mode, and that may be invalid
5045 if it is an fp reg copied in integer mode.
5047 We allow two exceptions to this: It is valid if
5048 it is inside another SUBREG and the mode of that
5049 SUBREG and the mode of the inside of TO is
5050 tieable and it is valid if X is a SET that copies
5051 FROM to CC0. */
5053 if (GET_CODE (to) == SUBREG
5054 && ! MODES_TIEABLE_P (GET_MODE (to),
5055 GET_MODE (SUBREG_REG (to)))
5056 && ! (code == SUBREG
5057 && MODES_TIEABLE_P (GET_MODE (x),
5058 GET_MODE (SUBREG_REG (to))))
5059 #ifdef HAVE_cc0
5060 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5061 #endif
5063 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5065 #ifdef CANNOT_CHANGE_MODE_CLASS
5066 if (code == SUBREG
5067 && REG_P (to)
5068 && REGNO (to) < FIRST_PSEUDO_REGISTER
5069 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5070 GET_MODE (to),
5071 GET_MODE (x)))
5072 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5073 #endif
5075 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5076 n_occurrences++;
5078 else
5079 /* If we are in a SET_DEST, suppress most cases unless we
5080 have gone inside a MEM, in which case we want to
5081 simplify the address. We assume here that things that
5082 are actually part of the destination have their inner
5083 parts in the first expression. This is true for SUBREG,
5084 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5085 things aside from REG and MEM that should appear in a
5086 SET_DEST. */
5087 new_rtx = subst (XEXP (x, i), from, to,
5088 (((in_dest
5089 && (code == SUBREG || code == STRICT_LOW_PART
5090 || code == ZERO_EXTRACT))
5091 || code == SET)
5092 && i == 0),
5093 code == IF_THEN_ELSE && i == 0,
5094 unique_copy);
5096 /* If we found that we will have to reject this combination,
5097 indicate that by returning the CLOBBER ourselves, rather than
5098 an expression containing it. This will speed things up as
5099 well as prevent accidents where two CLOBBERs are considered
5100 to be equal, thus producing an incorrect simplification. */
5102 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5103 return new_rtx;
5105 if (GET_CODE (x) == SUBREG
5106 && (CONST_INT_P (new_rtx) || CONST_DOUBLE_AS_INT_P (new_rtx)))
5108 enum machine_mode mode = GET_MODE (x);
5110 x = simplify_subreg (GET_MODE (x), new_rtx,
5111 GET_MODE (SUBREG_REG (x)),
5112 SUBREG_BYTE (x));
5113 if (! x)
5114 x = gen_rtx_CLOBBER (mode, const0_rtx);
5116 else if (CONST_INT_P (new_rtx)
5117 && GET_CODE (x) == ZERO_EXTEND)
5119 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5120 new_rtx, GET_MODE (XEXP (x, 0)));
5121 gcc_assert (x);
5123 else
5124 SUBST (XEXP (x, i), new_rtx);
5129 /* Check if we are loading something from the constant pool via float
5130 extension; in this case we would undo compress_float_constant
5131 optimization and degenerate constant load to an immediate value. */
5132 if (GET_CODE (x) == FLOAT_EXTEND
5133 && MEM_P (XEXP (x, 0))
5134 && MEM_READONLY_P (XEXP (x, 0)))
5136 rtx tmp = avoid_constant_pool_reference (x);
5137 if (x != tmp)
5138 return x;
5141 /* Try to simplify X. If the simplification changed the code, it is likely
5142 that further simplification will help, so loop, but limit the number
5143 of repetitions that will be performed. */
5145 for (i = 0; i < 4; i++)
5147 /* If X is sufficiently simple, don't bother trying to do anything
5148 with it. */
5149 if (code != CONST_INT && code != REG && code != CLOBBER)
5150 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5152 if (GET_CODE (x) == code)
5153 break;
5155 code = GET_CODE (x);
5157 /* We no longer know the original mode of operand 0 since we
5158 have changed the form of X) */
5159 op0_mode = VOIDmode;
5162 return x;
5165 /* Simplify X, a piece of RTL. We just operate on the expression at the
5166 outer level; call `subst' to simplify recursively. Return the new
5167 expression.
5169 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5170 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5171 of a condition. */
5173 static rtx
5174 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5175 int in_cond)
5177 enum rtx_code code = GET_CODE (x);
5178 enum machine_mode mode = GET_MODE (x);
5179 rtx temp;
5180 int i;
5182 /* If this is a commutative operation, put a constant last and a complex
5183 expression first. We don't need to do this for comparisons here. */
5184 if (COMMUTATIVE_ARITH_P (x)
5185 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5187 temp = XEXP (x, 0);
5188 SUBST (XEXP (x, 0), XEXP (x, 1));
5189 SUBST (XEXP (x, 1), temp);
5192 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5193 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5194 things. Check for cases where both arms are testing the same
5195 condition.
5197 Don't do anything if all operands are very simple. */
5199 if ((BINARY_P (x)
5200 && ((!OBJECT_P (XEXP (x, 0))
5201 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5202 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5203 || (!OBJECT_P (XEXP (x, 1))
5204 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5205 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5206 || (UNARY_P (x)
5207 && (!OBJECT_P (XEXP (x, 0))
5208 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5209 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5211 rtx cond, true_rtx, false_rtx;
5213 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5214 if (cond != 0
5215 /* If everything is a comparison, what we have is highly unlikely
5216 to be simpler, so don't use it. */
5217 && ! (COMPARISON_P (x)
5218 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5220 rtx cop1 = const0_rtx;
5221 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5223 if (cond_code == NE && COMPARISON_P (cond))
5224 return x;
5226 /* Simplify the alternative arms; this may collapse the true and
5227 false arms to store-flag values. Be careful to use copy_rtx
5228 here since true_rtx or false_rtx might share RTL with x as a
5229 result of the if_then_else_cond call above. */
5230 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5231 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5233 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5234 is unlikely to be simpler. */
5235 if (general_operand (true_rtx, VOIDmode)
5236 && general_operand (false_rtx, VOIDmode))
5238 enum rtx_code reversed;
5240 /* Restarting if we generate a store-flag expression will cause
5241 us to loop. Just drop through in this case. */
5243 /* If the result values are STORE_FLAG_VALUE and zero, we can
5244 just make the comparison operation. */
5245 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5246 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5247 cond, cop1);
5248 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5249 && ((reversed = reversed_comparison_code_parts
5250 (cond_code, cond, cop1, NULL))
5251 != UNKNOWN))
5252 x = simplify_gen_relational (reversed, mode, VOIDmode,
5253 cond, cop1);
5255 /* Likewise, we can make the negate of a comparison operation
5256 if the result values are - STORE_FLAG_VALUE and zero. */
5257 else if (CONST_INT_P (true_rtx)
5258 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5259 && false_rtx == const0_rtx)
5260 x = simplify_gen_unary (NEG, mode,
5261 simplify_gen_relational (cond_code,
5262 mode, VOIDmode,
5263 cond, cop1),
5264 mode);
5265 else if (CONST_INT_P (false_rtx)
5266 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5267 && true_rtx == const0_rtx
5268 && ((reversed = reversed_comparison_code_parts
5269 (cond_code, cond, cop1, NULL))
5270 != UNKNOWN))
5271 x = simplify_gen_unary (NEG, mode,
5272 simplify_gen_relational (reversed,
5273 mode, VOIDmode,
5274 cond, cop1),
5275 mode);
5276 else
5277 return gen_rtx_IF_THEN_ELSE (mode,
5278 simplify_gen_relational (cond_code,
5279 mode,
5280 VOIDmode,
5281 cond,
5282 cop1),
5283 true_rtx, false_rtx);
5285 code = GET_CODE (x);
5286 op0_mode = VOIDmode;
5291 /* Try to fold this expression in case we have constants that weren't
5292 present before. */
5293 temp = 0;
5294 switch (GET_RTX_CLASS (code))
5296 case RTX_UNARY:
5297 if (op0_mode == VOIDmode)
5298 op0_mode = GET_MODE (XEXP (x, 0));
5299 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5300 break;
5301 case RTX_COMPARE:
5302 case RTX_COMM_COMPARE:
5304 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5305 if (cmp_mode == VOIDmode)
5307 cmp_mode = GET_MODE (XEXP (x, 1));
5308 if (cmp_mode == VOIDmode)
5309 cmp_mode = op0_mode;
5311 temp = simplify_relational_operation (code, mode, cmp_mode,
5312 XEXP (x, 0), XEXP (x, 1));
5314 break;
5315 case RTX_COMM_ARITH:
5316 case RTX_BIN_ARITH:
5317 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5318 break;
5319 case RTX_BITFIELD_OPS:
5320 case RTX_TERNARY:
5321 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5322 XEXP (x, 1), XEXP (x, 2));
5323 break;
5324 default:
5325 break;
5328 if (temp)
5330 x = temp;
5331 code = GET_CODE (temp);
5332 op0_mode = VOIDmode;
5333 mode = GET_MODE (temp);
5336 /* First see if we can apply the inverse distributive law. */
5337 if (code == PLUS || code == MINUS
5338 || code == AND || code == IOR || code == XOR)
5340 x = apply_distributive_law (x);
5341 code = GET_CODE (x);
5342 op0_mode = VOIDmode;
5345 /* If CODE is an associative operation not otherwise handled, see if we
5346 can associate some operands. This can win if they are constants or
5347 if they are logically related (i.e. (a & b) & a). */
5348 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5349 || code == AND || code == IOR || code == XOR
5350 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5351 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5352 || (flag_associative_math && FLOAT_MODE_P (mode))))
5354 if (GET_CODE (XEXP (x, 0)) == code)
5356 rtx other = XEXP (XEXP (x, 0), 0);
5357 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5358 rtx inner_op1 = XEXP (x, 1);
5359 rtx inner;
5361 /* Make sure we pass the constant operand if any as the second
5362 one if this is a commutative operation. */
5363 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5365 rtx tem = inner_op0;
5366 inner_op0 = inner_op1;
5367 inner_op1 = tem;
5369 inner = simplify_binary_operation (code == MINUS ? PLUS
5370 : code == DIV ? MULT
5371 : code,
5372 mode, inner_op0, inner_op1);
5374 /* For commutative operations, try the other pair if that one
5375 didn't simplify. */
5376 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5378 other = XEXP (XEXP (x, 0), 1);
5379 inner = simplify_binary_operation (code, mode,
5380 XEXP (XEXP (x, 0), 0),
5381 XEXP (x, 1));
5384 if (inner)
5385 return simplify_gen_binary (code, mode, other, inner);
5389 /* A little bit of algebraic simplification here. */
5390 switch (code)
5392 case MEM:
5393 /* Ensure that our address has any ASHIFTs converted to MULT in case
5394 address-recognizing predicates are called later. */
5395 temp = make_compound_operation (XEXP (x, 0), MEM);
5396 SUBST (XEXP (x, 0), temp);
5397 break;
5399 case SUBREG:
5400 if (op0_mode == VOIDmode)
5401 op0_mode = GET_MODE (SUBREG_REG (x));
5403 /* See if this can be moved to simplify_subreg. */
5404 if (CONSTANT_P (SUBREG_REG (x))
5405 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5406 /* Don't call gen_lowpart if the inner mode
5407 is VOIDmode and we cannot simplify it, as SUBREG without
5408 inner mode is invalid. */
5409 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5410 || gen_lowpart_common (mode, SUBREG_REG (x))))
5411 return gen_lowpart (mode, SUBREG_REG (x));
5413 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5414 break;
5416 rtx temp;
5417 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5418 SUBREG_BYTE (x));
5419 if (temp)
5420 return temp;
5423 /* Don't change the mode of the MEM if that would change the meaning
5424 of the address. */
5425 if (MEM_P (SUBREG_REG (x))
5426 && (MEM_VOLATILE_P (SUBREG_REG (x))
5427 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5428 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5429 return gen_rtx_CLOBBER (mode, const0_rtx);
5431 /* Note that we cannot do any narrowing for non-constants since
5432 we might have been counting on using the fact that some bits were
5433 zero. We now do this in the SET. */
5435 break;
5437 case NEG:
5438 temp = expand_compound_operation (XEXP (x, 0));
5440 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5441 replaced by (lshiftrt X C). This will convert
5442 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5444 if (GET_CODE (temp) == ASHIFTRT
5445 && CONST_INT_P (XEXP (temp, 1))
5446 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5447 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5448 INTVAL (XEXP (temp, 1)));
5450 /* If X has only a single bit that might be nonzero, say, bit I, convert
5451 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5452 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5453 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5454 or a SUBREG of one since we'd be making the expression more
5455 complex if it was just a register. */
5457 if (!REG_P (temp)
5458 && ! (GET_CODE (temp) == SUBREG
5459 && REG_P (SUBREG_REG (temp)))
5460 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5462 rtx temp1 = simplify_shift_const
5463 (NULL_RTX, ASHIFTRT, mode,
5464 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5465 GET_MODE_PRECISION (mode) - 1 - i),
5466 GET_MODE_PRECISION (mode) - 1 - i);
5468 /* If all we did was surround TEMP with the two shifts, we
5469 haven't improved anything, so don't use it. Otherwise,
5470 we are better off with TEMP1. */
5471 if (GET_CODE (temp1) != ASHIFTRT
5472 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5473 || XEXP (XEXP (temp1, 0), 0) != temp)
5474 return temp1;
5476 break;
5478 case TRUNCATE:
5479 /* We can't handle truncation to a partial integer mode here
5480 because we don't know the real bitsize of the partial
5481 integer mode. */
5482 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5483 break;
5485 if (HWI_COMPUTABLE_MODE_P (mode))
5486 SUBST (XEXP (x, 0),
5487 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5488 GET_MODE_MASK (mode), 0));
5490 /* We can truncate a constant value and return it. */
5491 if (CONST_INT_P (XEXP (x, 0)))
5492 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5494 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5495 whose value is a comparison can be replaced with a subreg if
5496 STORE_FLAG_VALUE permits. */
5497 if (HWI_COMPUTABLE_MODE_P (mode)
5498 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5499 && (temp = get_last_value (XEXP (x, 0)))
5500 && COMPARISON_P (temp))
5501 return gen_lowpart (mode, XEXP (x, 0));
5502 break;
5504 case CONST:
5505 /* (const (const X)) can become (const X). Do it this way rather than
5506 returning the inner CONST since CONST can be shared with a
5507 REG_EQUAL note. */
5508 if (GET_CODE (XEXP (x, 0)) == CONST)
5509 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5510 break;
5512 #ifdef HAVE_lo_sum
5513 case LO_SUM:
5514 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5515 can add in an offset. find_split_point will split this address up
5516 again if it doesn't match. */
5517 if (GET_CODE (XEXP (x, 0)) == HIGH
5518 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5519 return XEXP (x, 1);
5520 break;
5521 #endif
5523 case PLUS:
5524 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5525 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5526 bit-field and can be replaced by either a sign_extend or a
5527 sign_extract. The `and' may be a zero_extend and the two
5528 <c>, -<c> constants may be reversed. */
5529 if (GET_CODE (XEXP (x, 0)) == XOR
5530 && CONST_INT_P (XEXP (x, 1))
5531 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5532 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5533 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5534 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5535 && HWI_COMPUTABLE_MODE_P (mode)
5536 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5537 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5538 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5539 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5540 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5541 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5542 == (unsigned int) i + 1))))
5543 return simplify_shift_const
5544 (NULL_RTX, ASHIFTRT, mode,
5545 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5546 XEXP (XEXP (XEXP (x, 0), 0), 0),
5547 GET_MODE_PRECISION (mode) - (i + 1)),
5548 GET_MODE_PRECISION (mode) - (i + 1));
5550 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5551 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5552 the bitsize of the mode - 1. This allows simplification of
5553 "a = (b & 8) == 0;" */
5554 if (XEXP (x, 1) == constm1_rtx
5555 && !REG_P (XEXP (x, 0))
5556 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5557 && REG_P (SUBREG_REG (XEXP (x, 0))))
5558 && nonzero_bits (XEXP (x, 0), mode) == 1)
5559 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5560 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5561 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5562 GET_MODE_PRECISION (mode) - 1),
5563 GET_MODE_PRECISION (mode) - 1);
5565 /* If we are adding two things that have no bits in common, convert
5566 the addition into an IOR. This will often be further simplified,
5567 for example in cases like ((a & 1) + (a & 2)), which can
5568 become a & 3. */
5570 if (HWI_COMPUTABLE_MODE_P (mode)
5571 && (nonzero_bits (XEXP (x, 0), mode)
5572 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5574 /* Try to simplify the expression further. */
5575 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5576 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5578 /* If we could, great. If not, do not go ahead with the IOR
5579 replacement, since PLUS appears in many special purpose
5580 address arithmetic instructions. */
5581 if (GET_CODE (temp) != CLOBBER
5582 && (GET_CODE (temp) != IOR
5583 || ((XEXP (temp, 0) != XEXP (x, 0)
5584 || XEXP (temp, 1) != XEXP (x, 1))
5585 && (XEXP (temp, 0) != XEXP (x, 1)
5586 || XEXP (temp, 1) != XEXP (x, 0)))))
5587 return temp;
5589 break;
5591 case MINUS:
5592 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5593 (and <foo> (const_int pow2-1)) */
5594 if (GET_CODE (XEXP (x, 1)) == AND
5595 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5596 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5597 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5598 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5599 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5600 break;
5602 case MULT:
5603 /* If we have (mult (plus A B) C), apply the distributive law and then
5604 the inverse distributive law to see if things simplify. This
5605 occurs mostly in addresses, often when unrolling loops. */
5607 if (GET_CODE (XEXP (x, 0)) == PLUS)
5609 rtx result = distribute_and_simplify_rtx (x, 0);
5610 if (result)
5611 return result;
5614 /* Try simplify a*(b/c) as (a*b)/c. */
5615 if (FLOAT_MODE_P (mode) && flag_associative_math
5616 && GET_CODE (XEXP (x, 0)) == DIV)
5618 rtx tem = simplify_binary_operation (MULT, mode,
5619 XEXP (XEXP (x, 0), 0),
5620 XEXP (x, 1));
5621 if (tem)
5622 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5624 break;
5626 case UDIV:
5627 /* If this is a divide by a power of two, treat it as a shift if
5628 its first operand is a shift. */
5629 if (CONST_INT_P (XEXP (x, 1))
5630 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5631 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5632 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5633 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5634 || GET_CODE (XEXP (x, 0)) == ROTATE
5635 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5636 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5637 break;
5639 case EQ: case NE:
5640 case GT: case GTU: case GE: case GEU:
5641 case LT: case LTU: case LE: case LEU:
5642 case UNEQ: case LTGT:
5643 case UNGT: case UNGE:
5644 case UNLT: case UNLE:
5645 case UNORDERED: case ORDERED:
5646 /* If the first operand is a condition code, we can't do anything
5647 with it. */
5648 if (GET_CODE (XEXP (x, 0)) == COMPARE
5649 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5650 && ! CC0_P (XEXP (x, 0))))
5652 rtx op0 = XEXP (x, 0);
5653 rtx op1 = XEXP (x, 1);
5654 enum rtx_code new_code;
5656 if (GET_CODE (op0) == COMPARE)
5657 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5659 /* Simplify our comparison, if possible. */
5660 new_code = simplify_comparison (code, &op0, &op1);
5662 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5663 if only the low-order bit is possibly nonzero in X (such as when
5664 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5665 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5666 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5667 (plus X 1).
5669 Remove any ZERO_EXTRACT we made when thinking this was a
5670 comparison. It may now be simpler to use, e.g., an AND. If a
5671 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5672 the call to make_compound_operation in the SET case.
5674 Don't apply these optimizations if the caller would
5675 prefer a comparison rather than a value.
5676 E.g., for the condition in an IF_THEN_ELSE most targets need
5677 an explicit comparison. */
5679 if (in_cond)
5682 else if (STORE_FLAG_VALUE == 1
5683 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5684 && op1 == const0_rtx
5685 && mode == GET_MODE (op0)
5686 && nonzero_bits (op0, mode) == 1)
5687 return gen_lowpart (mode,
5688 expand_compound_operation (op0));
5690 else if (STORE_FLAG_VALUE == 1
5691 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5692 && op1 == const0_rtx
5693 && mode == GET_MODE (op0)
5694 && (num_sign_bit_copies (op0, mode)
5695 == GET_MODE_PRECISION (mode)))
5697 op0 = expand_compound_operation (op0);
5698 return simplify_gen_unary (NEG, mode,
5699 gen_lowpart (mode, op0),
5700 mode);
5703 else if (STORE_FLAG_VALUE == 1
5704 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5705 && op1 == const0_rtx
5706 && mode == GET_MODE (op0)
5707 && nonzero_bits (op0, mode) == 1)
5709 op0 = expand_compound_operation (op0);
5710 return simplify_gen_binary (XOR, mode,
5711 gen_lowpart (mode, op0),
5712 const1_rtx);
5715 else if (STORE_FLAG_VALUE == 1
5716 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5717 && op1 == const0_rtx
5718 && mode == GET_MODE (op0)
5719 && (num_sign_bit_copies (op0, mode)
5720 == GET_MODE_PRECISION (mode)))
5722 op0 = expand_compound_operation (op0);
5723 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5726 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5727 those above. */
5728 if (in_cond)
5731 else if (STORE_FLAG_VALUE == -1
5732 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5733 && op1 == const0_rtx
5734 && (num_sign_bit_copies (op0, mode)
5735 == GET_MODE_PRECISION (mode)))
5736 return gen_lowpart (mode,
5737 expand_compound_operation (op0));
5739 else if (STORE_FLAG_VALUE == -1
5740 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5741 && op1 == const0_rtx
5742 && mode == GET_MODE (op0)
5743 && nonzero_bits (op0, mode) == 1)
5745 op0 = expand_compound_operation (op0);
5746 return simplify_gen_unary (NEG, mode,
5747 gen_lowpart (mode, op0),
5748 mode);
5751 else if (STORE_FLAG_VALUE == -1
5752 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5753 && op1 == const0_rtx
5754 && mode == GET_MODE (op0)
5755 && (num_sign_bit_copies (op0, mode)
5756 == GET_MODE_PRECISION (mode)))
5758 op0 = expand_compound_operation (op0);
5759 return simplify_gen_unary (NOT, mode,
5760 gen_lowpart (mode, op0),
5761 mode);
5764 /* If X is 0/1, (eq X 0) is X-1. */
5765 else if (STORE_FLAG_VALUE == -1
5766 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5767 && op1 == const0_rtx
5768 && mode == GET_MODE (op0)
5769 && nonzero_bits (op0, mode) == 1)
5771 op0 = expand_compound_operation (op0);
5772 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5775 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5776 one bit that might be nonzero, we can convert (ne x 0) to
5777 (ashift x c) where C puts the bit in the sign bit. Remove any
5778 AND with STORE_FLAG_VALUE when we are done, since we are only
5779 going to test the sign bit. */
5780 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5781 && HWI_COMPUTABLE_MODE_P (mode)
5782 && val_signbit_p (mode, STORE_FLAG_VALUE)
5783 && op1 == const0_rtx
5784 && mode == GET_MODE (op0)
5785 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5787 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5788 expand_compound_operation (op0),
5789 GET_MODE_PRECISION (mode) - 1 - i);
5790 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5791 return XEXP (x, 0);
5792 else
5793 return x;
5796 /* If the code changed, return a whole new comparison. */
5797 if (new_code != code)
5798 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5800 /* Otherwise, keep this operation, but maybe change its operands.
5801 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5802 SUBST (XEXP (x, 0), op0);
5803 SUBST (XEXP (x, 1), op1);
5805 break;
5807 case IF_THEN_ELSE:
5808 return simplify_if_then_else (x);
5810 case ZERO_EXTRACT:
5811 case SIGN_EXTRACT:
5812 case ZERO_EXTEND:
5813 case SIGN_EXTEND:
5814 /* If we are processing SET_DEST, we are done. */
5815 if (in_dest)
5816 return x;
5818 return expand_compound_operation (x);
5820 case SET:
5821 return simplify_set (x);
5823 case AND:
5824 case IOR:
5825 return simplify_logical (x);
5827 case ASHIFT:
5828 case LSHIFTRT:
5829 case ASHIFTRT:
5830 case ROTATE:
5831 case ROTATERT:
5832 /* If this is a shift by a constant amount, simplify it. */
5833 if (CONST_INT_P (XEXP (x, 1)))
5834 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5835 INTVAL (XEXP (x, 1)));
5837 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5838 SUBST (XEXP (x, 1),
5839 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5840 ((unsigned HOST_WIDE_INT) 1
5841 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5842 - 1,
5843 0));
5844 break;
5846 default:
5847 break;
5850 return x;
5853 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5855 static rtx
5856 simplify_if_then_else (rtx x)
5858 enum machine_mode mode = GET_MODE (x);
5859 rtx cond = XEXP (x, 0);
5860 rtx true_rtx = XEXP (x, 1);
5861 rtx false_rtx = XEXP (x, 2);
5862 enum rtx_code true_code = GET_CODE (cond);
5863 int comparison_p = COMPARISON_P (cond);
5864 rtx temp;
5865 int i;
5866 enum rtx_code false_code;
5867 rtx reversed;
5869 /* Simplify storing of the truth value. */
5870 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5871 return simplify_gen_relational (true_code, mode, VOIDmode,
5872 XEXP (cond, 0), XEXP (cond, 1));
5874 /* Also when the truth value has to be reversed. */
5875 if (comparison_p
5876 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5877 && (reversed = reversed_comparison (cond, mode)))
5878 return reversed;
5880 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5881 in it is being compared against certain values. Get the true and false
5882 comparisons and see if that says anything about the value of each arm. */
5884 if (comparison_p
5885 && ((false_code = reversed_comparison_code (cond, NULL))
5886 != UNKNOWN)
5887 && REG_P (XEXP (cond, 0)))
5889 HOST_WIDE_INT nzb;
5890 rtx from = XEXP (cond, 0);
5891 rtx true_val = XEXP (cond, 1);
5892 rtx false_val = true_val;
5893 int swapped = 0;
5895 /* If FALSE_CODE is EQ, swap the codes and arms. */
5897 if (false_code == EQ)
5899 swapped = 1, true_code = EQ, false_code = NE;
5900 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5903 /* If we are comparing against zero and the expression being tested has
5904 only a single bit that might be nonzero, that is its value when it is
5905 not equal to zero. Similarly if it is known to be -1 or 0. */
5907 if (true_code == EQ && true_val == const0_rtx
5908 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5910 false_code = EQ;
5911 false_val = gen_int_mode (nzb, GET_MODE (from));
5913 else if (true_code == EQ && true_val == const0_rtx
5914 && (num_sign_bit_copies (from, GET_MODE (from))
5915 == GET_MODE_PRECISION (GET_MODE (from))))
5917 false_code = EQ;
5918 false_val = constm1_rtx;
5921 /* Now simplify an arm if we know the value of the register in the
5922 branch and it is used in the arm. Be careful due to the potential
5923 of locally-shared RTL. */
5925 if (reg_mentioned_p (from, true_rtx))
5926 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5927 from, true_val),
5928 pc_rtx, pc_rtx, 0, 0, 0);
5929 if (reg_mentioned_p (from, false_rtx))
5930 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5931 from, false_val),
5932 pc_rtx, pc_rtx, 0, 0, 0);
5934 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5935 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5937 true_rtx = XEXP (x, 1);
5938 false_rtx = XEXP (x, 2);
5939 true_code = GET_CODE (cond);
5942 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5943 reversed, do so to avoid needing two sets of patterns for
5944 subtract-and-branch insns. Similarly if we have a constant in the true
5945 arm, the false arm is the same as the first operand of the comparison, or
5946 the false arm is more complicated than the true arm. */
5948 if (comparison_p
5949 && reversed_comparison_code (cond, NULL) != UNKNOWN
5950 && (true_rtx == pc_rtx
5951 || (CONSTANT_P (true_rtx)
5952 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5953 || true_rtx == const0_rtx
5954 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5955 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5956 && !OBJECT_P (false_rtx))
5957 || reg_mentioned_p (true_rtx, false_rtx)
5958 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5960 true_code = reversed_comparison_code (cond, NULL);
5961 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5962 SUBST (XEXP (x, 1), false_rtx);
5963 SUBST (XEXP (x, 2), true_rtx);
5965 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5966 cond = XEXP (x, 0);
5968 /* It is possible that the conditional has been simplified out. */
5969 true_code = GET_CODE (cond);
5970 comparison_p = COMPARISON_P (cond);
5973 /* If the two arms are identical, we don't need the comparison. */
5975 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5976 return true_rtx;
5978 /* Convert a == b ? b : a to "a". */
5979 if (true_code == EQ && ! side_effects_p (cond)
5980 && !HONOR_NANS (mode)
5981 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5982 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5983 return false_rtx;
5984 else if (true_code == NE && ! side_effects_p (cond)
5985 && !HONOR_NANS (mode)
5986 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5987 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5988 return true_rtx;
5990 /* Look for cases where we have (abs x) or (neg (abs X)). */
5992 if (GET_MODE_CLASS (mode) == MODE_INT
5993 && comparison_p
5994 && XEXP (cond, 1) == const0_rtx
5995 && GET_CODE (false_rtx) == NEG
5996 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5997 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5998 && ! side_effects_p (true_rtx))
5999 switch (true_code)
6001 case GT:
6002 case GE:
6003 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6004 case LT:
6005 case LE:
6006 return
6007 simplify_gen_unary (NEG, mode,
6008 simplify_gen_unary (ABS, mode, true_rtx, mode),
6009 mode);
6010 default:
6011 break;
6014 /* Look for MIN or MAX. */
6016 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6017 && comparison_p
6018 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6019 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6020 && ! side_effects_p (cond))
6021 switch (true_code)
6023 case GE:
6024 case GT:
6025 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6026 case LE:
6027 case LT:
6028 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6029 case GEU:
6030 case GTU:
6031 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6032 case LEU:
6033 case LTU:
6034 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6035 default:
6036 break;
6039 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6040 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6041 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6042 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6043 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6044 neither 1 or -1, but it isn't worth checking for. */
6046 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6047 && comparison_p
6048 && GET_MODE_CLASS (mode) == MODE_INT
6049 && ! side_effects_p (x))
6051 rtx t = make_compound_operation (true_rtx, SET);
6052 rtx f = make_compound_operation (false_rtx, SET);
6053 rtx cond_op0 = XEXP (cond, 0);
6054 rtx cond_op1 = XEXP (cond, 1);
6055 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6056 enum machine_mode m = mode;
6057 rtx z = 0, c1 = NULL_RTX;
6059 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6060 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6061 || GET_CODE (t) == ASHIFT
6062 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6063 && rtx_equal_p (XEXP (t, 0), f))
6064 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6066 /* If an identity-zero op is commutative, check whether there
6067 would be a match if we swapped the operands. */
6068 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6069 || GET_CODE (t) == XOR)
6070 && rtx_equal_p (XEXP (t, 1), f))
6071 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6072 else if (GET_CODE (t) == SIGN_EXTEND
6073 && (GET_CODE (XEXP (t, 0)) == PLUS
6074 || GET_CODE (XEXP (t, 0)) == MINUS
6075 || GET_CODE (XEXP (t, 0)) == IOR
6076 || GET_CODE (XEXP (t, 0)) == XOR
6077 || GET_CODE (XEXP (t, 0)) == ASHIFT
6078 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6079 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6080 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6081 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6082 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6083 && (num_sign_bit_copies (f, GET_MODE (f))
6084 > (unsigned int)
6085 (GET_MODE_PRECISION (mode)
6086 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6088 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6089 extend_op = SIGN_EXTEND;
6090 m = GET_MODE (XEXP (t, 0));
6092 else if (GET_CODE (t) == SIGN_EXTEND
6093 && (GET_CODE (XEXP (t, 0)) == PLUS
6094 || GET_CODE (XEXP (t, 0)) == IOR
6095 || GET_CODE (XEXP (t, 0)) == XOR)
6096 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6097 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6098 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6099 && (num_sign_bit_copies (f, GET_MODE (f))
6100 > (unsigned int)
6101 (GET_MODE_PRECISION (mode)
6102 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6104 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6105 extend_op = SIGN_EXTEND;
6106 m = GET_MODE (XEXP (t, 0));
6108 else if (GET_CODE (t) == ZERO_EXTEND
6109 && (GET_CODE (XEXP (t, 0)) == PLUS
6110 || GET_CODE (XEXP (t, 0)) == MINUS
6111 || GET_CODE (XEXP (t, 0)) == IOR
6112 || GET_CODE (XEXP (t, 0)) == XOR
6113 || GET_CODE (XEXP (t, 0)) == ASHIFT
6114 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6115 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6116 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6117 && HWI_COMPUTABLE_MODE_P (mode)
6118 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6119 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6120 && ((nonzero_bits (f, GET_MODE (f))
6121 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6122 == 0))
6124 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6125 extend_op = ZERO_EXTEND;
6126 m = GET_MODE (XEXP (t, 0));
6128 else if (GET_CODE (t) == ZERO_EXTEND
6129 && (GET_CODE (XEXP (t, 0)) == PLUS
6130 || GET_CODE (XEXP (t, 0)) == IOR
6131 || GET_CODE (XEXP (t, 0)) == XOR)
6132 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6133 && HWI_COMPUTABLE_MODE_P (mode)
6134 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6135 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6136 && ((nonzero_bits (f, GET_MODE (f))
6137 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6138 == 0))
6140 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6141 extend_op = ZERO_EXTEND;
6142 m = GET_MODE (XEXP (t, 0));
6145 if (z)
6147 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6148 cond_op0, cond_op1),
6149 pc_rtx, pc_rtx, 0, 0, 0);
6150 temp = simplify_gen_binary (MULT, m, temp,
6151 simplify_gen_binary (MULT, m, c1,
6152 const_true_rtx));
6153 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6154 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6156 if (extend_op != UNKNOWN)
6157 temp = simplify_gen_unary (extend_op, mode, temp, m);
6159 return temp;
6163 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6164 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6165 negation of a single bit, we can convert this operation to a shift. We
6166 can actually do this more generally, but it doesn't seem worth it. */
6168 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6169 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6170 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6171 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6172 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6173 == GET_MODE_PRECISION (mode))
6174 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6175 return
6176 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6177 gen_lowpart (mode, XEXP (cond, 0)), i);
6179 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6180 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6181 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6182 && GET_MODE (XEXP (cond, 0)) == mode
6183 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6184 == nonzero_bits (XEXP (cond, 0), mode)
6185 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6186 return XEXP (cond, 0);
6188 return x;
6191 /* Simplify X, a SET expression. Return the new expression. */
6193 static rtx
6194 simplify_set (rtx x)
6196 rtx src = SET_SRC (x);
6197 rtx dest = SET_DEST (x);
6198 enum machine_mode mode
6199 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6200 rtx other_insn;
6201 rtx *cc_use;
6203 /* (set (pc) (return)) gets written as (return). */
6204 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6205 return src;
6207 /* Now that we know for sure which bits of SRC we are using, see if we can
6208 simplify the expression for the object knowing that we only need the
6209 low-order bits. */
6211 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6213 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6214 SUBST (SET_SRC (x), src);
6217 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6218 the comparison result and try to simplify it unless we already have used
6219 undobuf.other_insn. */
6220 if ((GET_MODE_CLASS (mode) == MODE_CC
6221 || GET_CODE (src) == COMPARE
6222 || CC0_P (dest))
6223 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6224 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6225 && COMPARISON_P (*cc_use)
6226 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6228 enum rtx_code old_code = GET_CODE (*cc_use);
6229 enum rtx_code new_code;
6230 rtx op0, op1, tmp;
6231 int other_changed = 0;
6232 rtx inner_compare = NULL_RTX;
6233 enum machine_mode compare_mode = GET_MODE (dest);
6235 if (GET_CODE (src) == COMPARE)
6237 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6238 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6240 inner_compare = op0;
6241 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6244 else
6245 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6247 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6248 op0, op1);
6249 if (!tmp)
6250 new_code = old_code;
6251 else if (!CONSTANT_P (tmp))
6253 new_code = GET_CODE (tmp);
6254 op0 = XEXP (tmp, 0);
6255 op1 = XEXP (tmp, 1);
6257 else
6259 rtx pat = PATTERN (other_insn);
6260 undobuf.other_insn = other_insn;
6261 SUBST (*cc_use, tmp);
6263 /* Attempt to simplify CC user. */
6264 if (GET_CODE (pat) == SET)
6266 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6267 if (new_rtx != NULL_RTX)
6268 SUBST (SET_SRC (pat), new_rtx);
6271 /* Convert X into a no-op move. */
6272 SUBST (SET_DEST (x), pc_rtx);
6273 SUBST (SET_SRC (x), pc_rtx);
6274 return x;
6277 /* Simplify our comparison, if possible. */
6278 new_code = simplify_comparison (new_code, &op0, &op1);
6280 #ifdef SELECT_CC_MODE
6281 /* If this machine has CC modes other than CCmode, check to see if we
6282 need to use a different CC mode here. */
6283 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6284 compare_mode = GET_MODE (op0);
6285 else if (inner_compare
6286 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6287 && new_code == old_code
6288 && op0 == XEXP (inner_compare, 0)
6289 && op1 == XEXP (inner_compare, 1))
6290 compare_mode = GET_MODE (inner_compare);
6291 else
6292 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6294 #ifndef HAVE_cc0
6295 /* If the mode changed, we have to change SET_DEST, the mode in the
6296 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6297 a hard register, just build new versions with the proper mode. If it
6298 is a pseudo, we lose unless it is only time we set the pseudo, in
6299 which case we can safely change its mode. */
6300 if (compare_mode != GET_MODE (dest))
6302 if (can_change_dest_mode (dest, 0, compare_mode))
6304 unsigned int regno = REGNO (dest);
6305 rtx new_dest;
6307 if (regno < FIRST_PSEUDO_REGISTER)
6308 new_dest = gen_rtx_REG (compare_mode, regno);
6309 else
6311 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6312 new_dest = regno_reg_rtx[regno];
6315 SUBST (SET_DEST (x), new_dest);
6316 SUBST (XEXP (*cc_use, 0), new_dest);
6317 other_changed = 1;
6319 dest = new_dest;
6322 #endif /* cc0 */
6323 #endif /* SELECT_CC_MODE */
6325 /* If the code changed, we have to build a new comparison in
6326 undobuf.other_insn. */
6327 if (new_code != old_code)
6329 int other_changed_previously = other_changed;
6330 unsigned HOST_WIDE_INT mask;
6331 rtx old_cc_use = *cc_use;
6333 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6334 dest, const0_rtx));
6335 other_changed = 1;
6337 /* If the only change we made was to change an EQ into an NE or
6338 vice versa, OP0 has only one bit that might be nonzero, and OP1
6339 is zero, check if changing the user of the condition code will
6340 produce a valid insn. If it won't, we can keep the original code
6341 in that insn by surrounding our operation with an XOR. */
6343 if (((old_code == NE && new_code == EQ)
6344 || (old_code == EQ && new_code == NE))
6345 && ! other_changed_previously && op1 == const0_rtx
6346 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6347 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6349 rtx pat = PATTERN (other_insn), note = 0;
6351 if ((recog_for_combine (&pat, other_insn, &note) < 0
6352 && ! check_asm_operands (pat)))
6354 *cc_use = old_cc_use;
6355 other_changed = 0;
6357 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6358 op0, GEN_INT (mask));
6363 if (other_changed)
6364 undobuf.other_insn = other_insn;
6366 /* Otherwise, if we didn't previously have a COMPARE in the
6367 correct mode, we need one. */
6368 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6370 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6371 src = SET_SRC (x);
6373 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6375 SUBST (SET_SRC (x), op0);
6376 src = SET_SRC (x);
6378 /* Otherwise, update the COMPARE if needed. */
6379 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6381 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6382 src = SET_SRC (x);
6385 else
6387 /* Get SET_SRC in a form where we have placed back any
6388 compound expressions. Then do the checks below. */
6389 src = make_compound_operation (src, SET);
6390 SUBST (SET_SRC (x), src);
6393 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6394 and X being a REG or (subreg (reg)), we may be able to convert this to
6395 (set (subreg:m2 x) (op)).
6397 We can always do this if M1 is narrower than M2 because that means that
6398 we only care about the low bits of the result.
6400 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6401 perform a narrower operation than requested since the high-order bits will
6402 be undefined. On machine where it is defined, this transformation is safe
6403 as long as M1 and M2 have the same number of words. */
6405 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6406 && !OBJECT_P (SUBREG_REG (src))
6407 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6408 / UNITS_PER_WORD)
6409 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6410 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6411 #ifndef WORD_REGISTER_OPERATIONS
6412 && (GET_MODE_SIZE (GET_MODE (src))
6413 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6414 #endif
6415 #ifdef CANNOT_CHANGE_MODE_CLASS
6416 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6417 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6418 GET_MODE (SUBREG_REG (src)),
6419 GET_MODE (src)))
6420 #endif
6421 && (REG_P (dest)
6422 || (GET_CODE (dest) == SUBREG
6423 && REG_P (SUBREG_REG (dest)))))
6425 SUBST (SET_DEST (x),
6426 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6427 dest));
6428 SUBST (SET_SRC (x), SUBREG_REG (src));
6430 src = SET_SRC (x), dest = SET_DEST (x);
6433 #ifdef HAVE_cc0
6434 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6435 in SRC. */
6436 if (dest == cc0_rtx
6437 && GET_CODE (src) == SUBREG
6438 && subreg_lowpart_p (src)
6439 && (GET_MODE_PRECISION (GET_MODE (src))
6440 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6442 rtx inner = SUBREG_REG (src);
6443 enum machine_mode inner_mode = GET_MODE (inner);
6445 /* Here we make sure that we don't have a sign bit on. */
6446 if (val_signbit_known_clear_p (GET_MODE (src),
6447 nonzero_bits (inner, inner_mode)))
6449 SUBST (SET_SRC (x), inner);
6450 src = SET_SRC (x);
6453 #endif
6455 #ifdef LOAD_EXTEND_OP
6456 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6457 would require a paradoxical subreg. Replace the subreg with a
6458 zero_extend to avoid the reload that would otherwise be required. */
6460 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6461 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6462 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6463 && SUBREG_BYTE (src) == 0
6464 && paradoxical_subreg_p (src)
6465 && MEM_P (SUBREG_REG (src)))
6467 SUBST (SET_SRC (x),
6468 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6469 GET_MODE (src), SUBREG_REG (src)));
6471 src = SET_SRC (x);
6473 #endif
6475 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6476 are comparing an item known to be 0 or -1 against 0, use a logical
6477 operation instead. Check for one of the arms being an IOR of the other
6478 arm with some value. We compute three terms to be IOR'ed together. In
6479 practice, at most two will be nonzero. Then we do the IOR's. */
6481 if (GET_CODE (dest) != PC
6482 && GET_CODE (src) == IF_THEN_ELSE
6483 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6484 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6485 && XEXP (XEXP (src, 0), 1) == const0_rtx
6486 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6487 #ifdef HAVE_conditional_move
6488 && ! can_conditionally_move_p (GET_MODE (src))
6489 #endif
6490 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6491 GET_MODE (XEXP (XEXP (src, 0), 0)))
6492 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6493 && ! side_effects_p (src))
6495 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6496 ? XEXP (src, 1) : XEXP (src, 2));
6497 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6498 ? XEXP (src, 2) : XEXP (src, 1));
6499 rtx term1 = const0_rtx, term2, term3;
6501 if (GET_CODE (true_rtx) == IOR
6502 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6503 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6504 else if (GET_CODE (true_rtx) == IOR
6505 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6506 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6507 else if (GET_CODE (false_rtx) == IOR
6508 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6509 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6510 else if (GET_CODE (false_rtx) == IOR
6511 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6512 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6514 term2 = simplify_gen_binary (AND, GET_MODE (src),
6515 XEXP (XEXP (src, 0), 0), true_rtx);
6516 term3 = simplify_gen_binary (AND, GET_MODE (src),
6517 simplify_gen_unary (NOT, GET_MODE (src),
6518 XEXP (XEXP (src, 0), 0),
6519 GET_MODE (src)),
6520 false_rtx);
6522 SUBST (SET_SRC (x),
6523 simplify_gen_binary (IOR, GET_MODE (src),
6524 simplify_gen_binary (IOR, GET_MODE (src),
6525 term1, term2),
6526 term3));
6528 src = SET_SRC (x);
6531 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6532 whole thing fail. */
6533 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6534 return src;
6535 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6536 return dest;
6537 else
6538 /* Convert this into a field assignment operation, if possible. */
6539 return make_field_assignment (x);
6542 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6543 result. */
6545 static rtx
6546 simplify_logical (rtx x)
6548 enum machine_mode mode = GET_MODE (x);
6549 rtx op0 = XEXP (x, 0);
6550 rtx op1 = XEXP (x, 1);
6552 switch (GET_CODE (x))
6554 case AND:
6555 /* We can call simplify_and_const_int only if we don't lose
6556 any (sign) bits when converting INTVAL (op1) to
6557 "unsigned HOST_WIDE_INT". */
6558 if (CONST_INT_P (op1)
6559 && (HWI_COMPUTABLE_MODE_P (mode)
6560 || INTVAL (op1) > 0))
6562 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6563 if (GET_CODE (x) != AND)
6564 return x;
6566 op0 = XEXP (x, 0);
6567 op1 = XEXP (x, 1);
6570 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6571 apply the distributive law and then the inverse distributive
6572 law to see if things simplify. */
6573 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6575 rtx result = distribute_and_simplify_rtx (x, 0);
6576 if (result)
6577 return result;
6579 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6581 rtx result = distribute_and_simplify_rtx (x, 1);
6582 if (result)
6583 return result;
6585 break;
6587 case IOR:
6588 /* If we have (ior (and A B) C), apply the distributive law and then
6589 the inverse distributive law to see if things simplify. */
6591 if (GET_CODE (op0) == AND)
6593 rtx result = distribute_and_simplify_rtx (x, 0);
6594 if (result)
6595 return result;
6598 if (GET_CODE (op1) == AND)
6600 rtx result = distribute_and_simplify_rtx (x, 1);
6601 if (result)
6602 return result;
6604 break;
6606 default:
6607 gcc_unreachable ();
6610 return x;
6613 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6614 operations" because they can be replaced with two more basic operations.
6615 ZERO_EXTEND is also considered "compound" because it can be replaced with
6616 an AND operation, which is simpler, though only one operation.
6618 The function expand_compound_operation is called with an rtx expression
6619 and will convert it to the appropriate shifts and AND operations,
6620 simplifying at each stage.
6622 The function make_compound_operation is called to convert an expression
6623 consisting of shifts and ANDs into the equivalent compound expression.
6624 It is the inverse of this function, loosely speaking. */
6626 static rtx
6627 expand_compound_operation (rtx x)
6629 unsigned HOST_WIDE_INT pos = 0, len;
6630 int unsignedp = 0;
6631 unsigned int modewidth;
6632 rtx tem;
6634 switch (GET_CODE (x))
6636 case ZERO_EXTEND:
6637 unsignedp = 1;
6638 case SIGN_EXTEND:
6639 /* We can't necessarily use a const_int for a multiword mode;
6640 it depends on implicitly extending the value.
6641 Since we don't know the right way to extend it,
6642 we can't tell whether the implicit way is right.
6644 Even for a mode that is no wider than a const_int,
6645 we can't win, because we need to sign extend one of its bits through
6646 the rest of it, and we don't know which bit. */
6647 if (CONST_INT_P (XEXP (x, 0)))
6648 return x;
6650 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6651 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6652 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6653 reloaded. If not for that, MEM's would very rarely be safe.
6655 Reject MODEs bigger than a word, because we might not be able
6656 to reference a two-register group starting with an arbitrary register
6657 (and currently gen_lowpart might crash for a SUBREG). */
6659 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6660 return x;
6662 /* Reject MODEs that aren't scalar integers because turning vector
6663 or complex modes into shifts causes problems. */
6665 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6666 return x;
6668 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6669 /* If the inner object has VOIDmode (the only way this can happen
6670 is if it is an ASM_OPERANDS), we can't do anything since we don't
6671 know how much masking to do. */
6672 if (len == 0)
6673 return x;
6675 break;
6677 case ZERO_EXTRACT:
6678 unsignedp = 1;
6680 /* ... fall through ... */
6682 case SIGN_EXTRACT:
6683 /* If the operand is a CLOBBER, just return it. */
6684 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6685 return XEXP (x, 0);
6687 if (!CONST_INT_P (XEXP (x, 1))
6688 || !CONST_INT_P (XEXP (x, 2))
6689 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6690 return x;
6692 /* Reject MODEs that aren't scalar integers because turning vector
6693 or complex modes into shifts causes problems. */
6695 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6696 return x;
6698 len = INTVAL (XEXP (x, 1));
6699 pos = INTVAL (XEXP (x, 2));
6701 /* This should stay within the object being extracted, fail otherwise. */
6702 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6703 return x;
6705 if (BITS_BIG_ENDIAN)
6706 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6708 break;
6710 default:
6711 return x;
6713 /* Convert sign extension to zero extension, if we know that the high
6714 bit is not set, as this is easier to optimize. It will be converted
6715 back to cheaper alternative in make_extraction. */
6716 if (GET_CODE (x) == SIGN_EXTEND
6717 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6718 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6719 & ~(((unsigned HOST_WIDE_INT)
6720 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6721 >> 1))
6722 == 0)))
6724 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6725 rtx temp2 = expand_compound_operation (temp);
6727 /* Make sure this is a profitable operation. */
6728 if (set_src_cost (x, optimize_this_for_speed_p)
6729 > set_src_cost (temp2, optimize_this_for_speed_p))
6730 return temp2;
6731 else if (set_src_cost (x, optimize_this_for_speed_p)
6732 > set_src_cost (temp, optimize_this_for_speed_p))
6733 return temp;
6734 else
6735 return x;
6738 /* We can optimize some special cases of ZERO_EXTEND. */
6739 if (GET_CODE (x) == ZERO_EXTEND)
6741 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6742 know that the last value didn't have any inappropriate bits
6743 set. */
6744 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6745 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6746 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6747 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6748 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6749 return XEXP (XEXP (x, 0), 0);
6751 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6752 if (GET_CODE (XEXP (x, 0)) == SUBREG
6753 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6754 && subreg_lowpart_p (XEXP (x, 0))
6755 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6756 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6757 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6758 return SUBREG_REG (XEXP (x, 0));
6760 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6761 is a comparison and STORE_FLAG_VALUE permits. This is like
6762 the first case, but it works even when GET_MODE (x) is larger
6763 than HOST_WIDE_INT. */
6764 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6765 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6766 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6767 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6768 <= HOST_BITS_PER_WIDE_INT)
6769 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6770 return XEXP (XEXP (x, 0), 0);
6772 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6773 if (GET_CODE (XEXP (x, 0)) == SUBREG
6774 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6775 && subreg_lowpart_p (XEXP (x, 0))
6776 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6777 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6778 <= HOST_BITS_PER_WIDE_INT)
6779 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6780 return SUBREG_REG (XEXP (x, 0));
6784 /* If we reach here, we want to return a pair of shifts. The inner
6785 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6786 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6787 logical depending on the value of UNSIGNEDP.
6789 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6790 converted into an AND of a shift.
6792 We must check for the case where the left shift would have a negative
6793 count. This can happen in a case like (x >> 31) & 255 on machines
6794 that can't shift by a constant. On those machines, we would first
6795 combine the shift with the AND to produce a variable-position
6796 extraction. Then the constant of 31 would be substituted in
6797 to produce such a position. */
6799 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6800 if (modewidth >= pos + len)
6802 enum machine_mode mode = GET_MODE (x);
6803 tem = gen_lowpart (mode, XEXP (x, 0));
6804 if (!tem || GET_CODE (tem) == CLOBBER)
6805 return x;
6806 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6807 tem, modewidth - pos - len);
6808 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6809 mode, tem, modewidth - len);
6811 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6812 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6813 simplify_shift_const (NULL_RTX, LSHIFTRT,
6814 GET_MODE (x),
6815 XEXP (x, 0), pos),
6816 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6817 else
6818 /* Any other cases we can't handle. */
6819 return x;
6821 /* If we couldn't do this for some reason, return the original
6822 expression. */
6823 if (GET_CODE (tem) == CLOBBER)
6824 return x;
6826 return tem;
6829 /* X is a SET which contains an assignment of one object into
6830 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6831 or certain SUBREGS). If possible, convert it into a series of
6832 logical operations.
6834 We half-heartedly support variable positions, but do not at all
6835 support variable lengths. */
6837 static const_rtx
6838 expand_field_assignment (const_rtx x)
6840 rtx inner;
6841 rtx pos; /* Always counts from low bit. */
6842 int len;
6843 rtx mask, cleared, masked;
6844 enum machine_mode compute_mode;
6846 /* Loop until we find something we can't simplify. */
6847 while (1)
6849 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6850 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6852 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6853 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
6854 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6856 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6857 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6859 inner = XEXP (SET_DEST (x), 0);
6860 len = INTVAL (XEXP (SET_DEST (x), 1));
6861 pos = XEXP (SET_DEST (x), 2);
6863 /* A constant position should stay within the width of INNER. */
6864 if (CONST_INT_P (pos)
6865 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
6866 break;
6868 if (BITS_BIG_ENDIAN)
6870 if (CONST_INT_P (pos))
6871 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
6872 - INTVAL (pos));
6873 else if (GET_CODE (pos) == MINUS
6874 && CONST_INT_P (XEXP (pos, 1))
6875 && (INTVAL (XEXP (pos, 1))
6876 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
6877 /* If position is ADJUST - X, new position is X. */
6878 pos = XEXP (pos, 0);
6879 else
6880 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6881 GEN_INT (GET_MODE_PRECISION (
6882 GET_MODE (inner))
6883 - len),
6884 pos);
6888 /* A SUBREG between two modes that occupy the same numbers of words
6889 can be done by moving the SUBREG to the source. */
6890 else if (GET_CODE (SET_DEST (x)) == SUBREG
6891 /* We need SUBREGs to compute nonzero_bits properly. */
6892 && nonzero_sign_valid
6893 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6894 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6895 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6896 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6898 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6899 gen_lowpart
6900 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6901 SET_SRC (x)));
6902 continue;
6904 else
6905 break;
6907 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6908 inner = SUBREG_REG (inner);
6910 compute_mode = GET_MODE (inner);
6912 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6913 if (! SCALAR_INT_MODE_P (compute_mode))
6915 enum machine_mode imode;
6917 /* Don't do anything for vector or complex integral types. */
6918 if (! FLOAT_MODE_P (compute_mode))
6919 break;
6921 /* Try to find an integral mode to pun with. */
6922 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6923 if (imode == BLKmode)
6924 break;
6926 compute_mode = imode;
6927 inner = gen_lowpart (imode, inner);
6930 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6931 if (len >= HOST_BITS_PER_WIDE_INT)
6932 break;
6934 /* Now compute the equivalent expression. Make a copy of INNER
6935 for the SET_DEST in case it is a MEM into which we will substitute;
6936 we don't want shared RTL in that case. */
6937 mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
6938 cleared = simplify_gen_binary (AND, compute_mode,
6939 simplify_gen_unary (NOT, compute_mode,
6940 simplify_gen_binary (ASHIFT,
6941 compute_mode,
6942 mask, pos),
6943 compute_mode),
6944 inner);
6945 masked = simplify_gen_binary (ASHIFT, compute_mode,
6946 simplify_gen_binary (
6947 AND, compute_mode,
6948 gen_lowpart (compute_mode, SET_SRC (x)),
6949 mask),
6950 pos);
6952 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6953 simplify_gen_binary (IOR, compute_mode,
6954 cleared, masked));
6957 return x;
6960 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6961 it is an RTX that represents a variable starting position; otherwise,
6962 POS is the (constant) starting bit position (counted from the LSB).
6964 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6965 signed reference.
6967 IN_DEST is nonzero if this is a reference in the destination of a
6968 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6969 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6970 be used.
6972 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6973 ZERO_EXTRACT should be built even for bits starting at bit 0.
6975 MODE is the desired mode of the result (if IN_DEST == 0).
6977 The result is an RTX for the extraction or NULL_RTX if the target
6978 can't handle it. */
6980 static rtx
6981 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6982 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6983 int in_dest, int in_compare)
6985 /* This mode describes the size of the storage area
6986 to fetch the overall value from. Within that, we
6987 ignore the POS lowest bits, etc. */
6988 enum machine_mode is_mode = GET_MODE (inner);
6989 enum machine_mode inner_mode;
6990 enum machine_mode wanted_inner_mode;
6991 enum machine_mode wanted_inner_reg_mode = word_mode;
6992 enum machine_mode pos_mode = word_mode;
6993 enum machine_mode extraction_mode = word_mode;
6994 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6995 rtx new_rtx = 0;
6996 rtx orig_pos_rtx = pos_rtx;
6997 HOST_WIDE_INT orig_pos;
6999 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7001 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7002 consider just the QI as the memory to extract from.
7003 The subreg adds or removes high bits; its mode is
7004 irrelevant to the meaning of this extraction,
7005 since POS and LEN count from the lsb. */
7006 if (MEM_P (SUBREG_REG (inner)))
7007 is_mode = GET_MODE (SUBREG_REG (inner));
7008 inner = SUBREG_REG (inner);
7010 else if (GET_CODE (inner) == ASHIFT
7011 && CONST_INT_P (XEXP (inner, 1))
7012 && pos_rtx == 0 && pos == 0
7013 && len > UINTVAL (XEXP (inner, 1)))
7015 /* We're extracting the least significant bits of an rtx
7016 (ashift X (const_int C)), where LEN > C. Extract the
7017 least significant (LEN - C) bits of X, giving an rtx
7018 whose mode is MODE, then shift it left C times. */
7019 new_rtx = make_extraction (mode, XEXP (inner, 0),
7020 0, 0, len - INTVAL (XEXP (inner, 1)),
7021 unsignedp, in_dest, in_compare);
7022 if (new_rtx != 0)
7023 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7026 inner_mode = GET_MODE (inner);
7028 if (pos_rtx && CONST_INT_P (pos_rtx))
7029 pos = INTVAL (pos_rtx), pos_rtx = 0;
7031 /* See if this can be done without an extraction. We never can if the
7032 width of the field is not the same as that of some integer mode. For
7033 registers, we can only avoid the extraction if the position is at the
7034 low-order bit and this is either not in the destination or we have the
7035 appropriate STRICT_LOW_PART operation available.
7037 For MEM, we can avoid an extract if the field starts on an appropriate
7038 boundary and we can change the mode of the memory reference. */
7040 if (tmode != BLKmode
7041 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7042 && !MEM_P (inner)
7043 && (inner_mode == tmode
7044 || !REG_P (inner)
7045 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7046 || reg_truncated_to_mode (tmode, inner))
7047 && (! in_dest
7048 || (REG_P (inner)
7049 && have_insn_for (STRICT_LOW_PART, tmode))))
7050 || (MEM_P (inner) && pos_rtx == 0
7051 && (pos
7052 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7053 : BITS_PER_UNIT)) == 0
7054 /* We can't do this if we are widening INNER_MODE (it
7055 may not be aligned, for one thing). */
7056 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7057 && (inner_mode == tmode
7058 || (! mode_dependent_address_p (XEXP (inner, 0),
7059 MEM_ADDR_SPACE (inner))
7060 && ! MEM_VOLATILE_P (inner))))))
7062 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7063 field. If the original and current mode are the same, we need not
7064 adjust the offset. Otherwise, we do if bytes big endian.
7066 If INNER is not a MEM, get a piece consisting of just the field
7067 of interest (in this case POS % BITS_PER_WORD must be 0). */
7069 if (MEM_P (inner))
7071 HOST_WIDE_INT offset;
7073 /* POS counts from lsb, but make OFFSET count in memory order. */
7074 if (BYTES_BIG_ENDIAN)
7075 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7076 else
7077 offset = pos / BITS_PER_UNIT;
7079 new_rtx = adjust_address_nv (inner, tmode, offset);
7081 else if (REG_P (inner))
7083 if (tmode != inner_mode)
7085 /* We can't call gen_lowpart in a DEST since we
7086 always want a SUBREG (see below) and it would sometimes
7087 return a new hard register. */
7088 if (pos || in_dest)
7090 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7092 if (WORDS_BIG_ENDIAN
7093 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7094 final_word = ((GET_MODE_SIZE (inner_mode)
7095 - GET_MODE_SIZE (tmode))
7096 / UNITS_PER_WORD) - final_word;
7098 final_word *= UNITS_PER_WORD;
7099 if (BYTES_BIG_ENDIAN &&
7100 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7101 final_word += (GET_MODE_SIZE (inner_mode)
7102 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7104 /* Avoid creating invalid subregs, for example when
7105 simplifying (x>>32)&255. */
7106 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7107 return NULL_RTX;
7109 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7111 else
7112 new_rtx = gen_lowpart (tmode, inner);
7114 else
7115 new_rtx = inner;
7117 else
7118 new_rtx = force_to_mode (inner, tmode,
7119 len >= HOST_BITS_PER_WIDE_INT
7120 ? ~(unsigned HOST_WIDE_INT) 0
7121 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7124 /* If this extraction is going into the destination of a SET,
7125 make a STRICT_LOW_PART unless we made a MEM. */
7127 if (in_dest)
7128 return (MEM_P (new_rtx) ? new_rtx
7129 : (GET_CODE (new_rtx) != SUBREG
7130 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7131 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7133 if (mode == tmode)
7134 return new_rtx;
7136 if (CONST_INT_P (new_rtx) || CONST_DOUBLE_AS_INT_P (new_rtx))
7137 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7138 mode, new_rtx, tmode);
7140 /* If we know that no extraneous bits are set, and that the high
7141 bit is not set, convert the extraction to the cheaper of
7142 sign and zero extension, that are equivalent in these cases. */
7143 if (flag_expensive_optimizations
7144 && (HWI_COMPUTABLE_MODE_P (tmode)
7145 && ((nonzero_bits (new_rtx, tmode)
7146 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7147 == 0)))
7149 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7150 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7152 /* Prefer ZERO_EXTENSION, since it gives more information to
7153 backends. */
7154 if (set_src_cost (temp, optimize_this_for_speed_p)
7155 <= set_src_cost (temp1, optimize_this_for_speed_p))
7156 return temp;
7157 return temp1;
7160 /* Otherwise, sign- or zero-extend unless we already are in the
7161 proper mode. */
7163 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7164 mode, new_rtx));
7167 /* Unless this is a COMPARE or we have a funny memory reference,
7168 don't do anything with zero-extending field extracts starting at
7169 the low-order bit since they are simple AND operations. */
7170 if (pos_rtx == 0 && pos == 0 && ! in_dest
7171 && ! in_compare && unsignedp)
7172 return 0;
7174 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7175 if the position is not a constant and the length is not 1. In all
7176 other cases, we would only be going outside our object in cases when
7177 an original shift would have been undefined. */
7178 if (MEM_P (inner)
7179 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7180 || (pos_rtx != 0 && len != 1)))
7181 return 0;
7183 /* Get the mode to use should INNER not be a MEM, the mode for the position,
7184 and the mode for the result. */
7185 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7187 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7188 pos_mode = mode_for_extraction (EP_insv, 2);
7189 extraction_mode = mode_for_extraction (EP_insv, 3);
7192 if (! in_dest && unsignedp
7193 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7195 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7196 pos_mode = mode_for_extraction (EP_extzv, 3);
7197 extraction_mode = mode_for_extraction (EP_extzv, 0);
7200 if (! in_dest && ! unsignedp
7201 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7203 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7204 pos_mode = mode_for_extraction (EP_extv, 3);
7205 extraction_mode = mode_for_extraction (EP_extv, 0);
7208 /* Never narrow an object, since that might not be safe. */
7210 if (mode != VOIDmode
7211 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7212 extraction_mode = mode;
7214 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7215 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7216 pos_mode = GET_MODE (pos_rtx);
7218 /* If this is not from memory, the desired mode is the preferred mode
7219 for an extraction pattern's first input operand, or word_mode if there
7220 is none. */
7221 if (!MEM_P (inner))
7222 wanted_inner_mode = wanted_inner_reg_mode;
7223 else
7225 /* Be careful not to go beyond the extracted object and maintain the
7226 natural alignment of the memory. */
7227 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7228 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7229 > GET_MODE_BITSIZE (wanted_inner_mode))
7231 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7232 gcc_assert (wanted_inner_mode != VOIDmode);
7235 /* If we have to change the mode of memory and cannot, the desired mode
7236 is EXTRACTION_MODE. */
7237 if (inner_mode != wanted_inner_mode
7238 && (mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7239 || MEM_VOLATILE_P (inner)
7240 || pos_rtx))
7241 wanted_inner_mode = extraction_mode;
7244 orig_pos = pos;
7246 if (BITS_BIG_ENDIAN)
7248 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7249 BITS_BIG_ENDIAN style. If position is constant, compute new
7250 position. Otherwise, build subtraction.
7251 Note that POS is relative to the mode of the original argument.
7252 If it's a MEM we need to recompute POS relative to that.
7253 However, if we're extracting from (or inserting into) a register,
7254 we want to recompute POS relative to wanted_inner_mode. */
7255 int width = (MEM_P (inner)
7256 ? GET_MODE_BITSIZE (is_mode)
7257 : GET_MODE_BITSIZE (wanted_inner_mode));
7259 if (pos_rtx == 0)
7260 pos = width - len - pos;
7261 else
7262 pos_rtx
7263 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7264 /* POS may be less than 0 now, but we check for that below.
7265 Note that it can only be less than 0 if !MEM_P (inner). */
7268 /* If INNER has a wider mode, and this is a constant extraction, try to
7269 make it smaller and adjust the byte to point to the byte containing
7270 the value. */
7271 if (wanted_inner_mode != VOIDmode
7272 && inner_mode != wanted_inner_mode
7273 && ! pos_rtx
7274 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7275 && MEM_P (inner)
7276 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7277 && ! MEM_VOLATILE_P (inner))
7279 int offset = 0;
7281 /* The computations below will be correct if the machine is big
7282 endian in both bits and bytes or little endian in bits and bytes.
7283 If it is mixed, we must adjust. */
7285 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7286 adjust OFFSET to compensate. */
7287 if (BYTES_BIG_ENDIAN
7288 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7289 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7291 /* We can now move to the desired byte. */
7292 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7293 * GET_MODE_SIZE (wanted_inner_mode);
7294 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7296 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7297 && is_mode != wanted_inner_mode)
7298 offset = (GET_MODE_SIZE (is_mode)
7299 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7301 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7304 /* If INNER is not memory, get it into the proper mode. If we are changing
7305 its mode, POS must be a constant and smaller than the size of the new
7306 mode. */
7307 else if (!MEM_P (inner))
7309 /* On the LHS, don't create paradoxical subregs implicitely truncating
7310 the register unless TRULY_NOOP_TRUNCATION. */
7311 if (in_dest
7312 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7313 wanted_inner_mode))
7314 return NULL_RTX;
7316 if (GET_MODE (inner) != wanted_inner_mode
7317 && (pos_rtx != 0
7318 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7319 return NULL_RTX;
7321 if (orig_pos < 0)
7322 return NULL_RTX;
7324 inner = force_to_mode (inner, wanted_inner_mode,
7325 pos_rtx
7326 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7327 ? ~(unsigned HOST_WIDE_INT) 0
7328 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7329 << orig_pos),
7333 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7334 have to zero extend. Otherwise, we can just use a SUBREG. */
7335 if (pos_rtx != 0
7336 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7338 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7340 /* If we know that no extraneous bits are set, and that the high
7341 bit is not set, convert extraction to cheaper one - either
7342 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7343 cases. */
7344 if (flag_expensive_optimizations
7345 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7346 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7347 & ~(((unsigned HOST_WIDE_INT)
7348 GET_MODE_MASK (GET_MODE (pos_rtx)))
7349 >> 1))
7350 == 0)))
7352 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7354 /* Prefer ZERO_EXTENSION, since it gives more information to
7355 backends. */
7356 if (set_src_cost (temp1, optimize_this_for_speed_p)
7357 < set_src_cost (temp, optimize_this_for_speed_p))
7358 temp = temp1;
7360 pos_rtx = temp;
7362 else if (pos_rtx != 0
7363 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7364 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7366 /* Make POS_RTX unless we already have it and it is correct. If we don't
7367 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7368 be a CONST_INT. */
7369 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7370 pos_rtx = orig_pos_rtx;
7372 else if (pos_rtx == 0)
7373 pos_rtx = GEN_INT (pos);
7375 /* Make the required operation. See if we can use existing rtx. */
7376 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7377 extraction_mode, inner, GEN_INT (len), pos_rtx);
7378 if (! in_dest)
7379 new_rtx = gen_lowpart (mode, new_rtx);
7381 return new_rtx;
7384 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7385 with any other operations in X. Return X without that shift if so. */
7387 static rtx
7388 extract_left_shift (rtx x, int count)
7390 enum rtx_code code = GET_CODE (x);
7391 enum machine_mode mode = GET_MODE (x);
7392 rtx tem;
7394 switch (code)
7396 case ASHIFT:
7397 /* This is the shift itself. If it is wide enough, we will return
7398 either the value being shifted if the shift count is equal to
7399 COUNT or a shift for the difference. */
7400 if (CONST_INT_P (XEXP (x, 1))
7401 && INTVAL (XEXP (x, 1)) >= count)
7402 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7403 INTVAL (XEXP (x, 1)) - count);
7404 break;
7406 case NEG: case NOT:
7407 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7408 return simplify_gen_unary (code, mode, tem, mode);
7410 break;
7412 case PLUS: case IOR: case XOR: case AND:
7413 /* If we can safely shift this constant and we find the inner shift,
7414 make a new operation. */
7415 if (CONST_INT_P (XEXP (x, 1))
7416 && (UINTVAL (XEXP (x, 1))
7417 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7418 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7419 return simplify_gen_binary (code, mode, tem,
7420 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7422 break;
7424 default:
7425 break;
7428 return 0;
7431 /* Look at the expression rooted at X. Look for expressions
7432 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7433 Form these expressions.
7435 Return the new rtx, usually just X.
7437 Also, for machines like the VAX that don't have logical shift insns,
7438 try to convert logical to arithmetic shift operations in cases where
7439 they are equivalent. This undoes the canonicalizations to logical
7440 shifts done elsewhere.
7442 We try, as much as possible, to re-use rtl expressions to save memory.
7444 IN_CODE says what kind of expression we are processing. Normally, it is
7445 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7446 being kludges), it is MEM. When processing the arguments of a comparison
7447 or a COMPARE against zero, it is COMPARE. */
7450 make_compound_operation (rtx x, enum rtx_code in_code)
7452 enum rtx_code code = GET_CODE (x);
7453 enum machine_mode mode = GET_MODE (x);
7454 int mode_width = GET_MODE_PRECISION (mode);
7455 rtx rhs, lhs;
7456 enum rtx_code next_code;
7457 int i, j;
7458 rtx new_rtx = 0;
7459 rtx tem;
7460 const char *fmt;
7462 /* Select the code to be used in recursive calls. Once we are inside an
7463 address, we stay there. If we have a comparison, set to COMPARE,
7464 but once inside, go back to our default of SET. */
7466 next_code = (code == MEM ? MEM
7467 : ((code == PLUS || code == MINUS)
7468 && SCALAR_INT_MODE_P (mode)) ? MEM
7469 : ((code == COMPARE || COMPARISON_P (x))
7470 && XEXP (x, 1) == const0_rtx) ? COMPARE
7471 : in_code == COMPARE ? SET : in_code);
7473 /* Process depending on the code of this operation. If NEW is set
7474 nonzero, it will be returned. */
7476 switch (code)
7478 case ASHIFT:
7479 /* Convert shifts by constants into multiplications if inside
7480 an address. */
7481 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7482 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7483 && INTVAL (XEXP (x, 1)) >= 0
7484 && SCALAR_INT_MODE_P (mode))
7486 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7487 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7489 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7490 if (GET_CODE (new_rtx) == NEG)
7492 new_rtx = XEXP (new_rtx, 0);
7493 multval = -multval;
7495 multval = trunc_int_for_mode (multval, mode);
7496 new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7498 break;
7500 case PLUS:
7501 lhs = XEXP (x, 0);
7502 rhs = XEXP (x, 1);
7503 lhs = make_compound_operation (lhs, next_code);
7504 rhs = make_compound_operation (rhs, next_code);
7505 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7506 && SCALAR_INT_MODE_P (mode))
7508 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7509 XEXP (lhs, 1));
7510 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7512 else if (GET_CODE (lhs) == MULT
7513 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7515 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7516 simplify_gen_unary (NEG, mode,
7517 XEXP (lhs, 1),
7518 mode));
7519 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7521 else
7523 SUBST (XEXP (x, 0), lhs);
7524 SUBST (XEXP (x, 1), rhs);
7525 goto maybe_swap;
7527 x = gen_lowpart (mode, new_rtx);
7528 goto maybe_swap;
7530 case MINUS:
7531 lhs = XEXP (x, 0);
7532 rhs = XEXP (x, 1);
7533 lhs = make_compound_operation (lhs, next_code);
7534 rhs = make_compound_operation (rhs, next_code);
7535 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7536 && SCALAR_INT_MODE_P (mode))
7538 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7539 XEXP (rhs, 1));
7540 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7542 else if (GET_CODE (rhs) == MULT
7543 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7545 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7546 simplify_gen_unary (NEG, mode,
7547 XEXP (rhs, 1),
7548 mode));
7549 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7551 else
7553 SUBST (XEXP (x, 0), lhs);
7554 SUBST (XEXP (x, 1), rhs);
7555 return x;
7557 return gen_lowpart (mode, new_rtx);
7559 case AND:
7560 /* If the second operand is not a constant, we can't do anything
7561 with it. */
7562 if (!CONST_INT_P (XEXP (x, 1)))
7563 break;
7565 /* If the constant is a power of two minus one and the first operand
7566 is a logical right shift, make an extraction. */
7567 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7568 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7570 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7571 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7572 0, in_code == COMPARE);
7575 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7576 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7577 && subreg_lowpart_p (XEXP (x, 0))
7578 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7579 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7581 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7582 next_code);
7583 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7584 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7585 0, in_code == COMPARE);
7587 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7588 else if ((GET_CODE (XEXP (x, 0)) == XOR
7589 || GET_CODE (XEXP (x, 0)) == IOR)
7590 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7591 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7592 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7594 /* Apply the distributive law, and then try to make extractions. */
7595 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7596 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7597 XEXP (x, 1)),
7598 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7599 XEXP (x, 1)));
7600 new_rtx = make_compound_operation (new_rtx, in_code);
7603 /* If we are have (and (rotate X C) M) and C is larger than the number
7604 of bits in M, this is an extraction. */
7606 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7607 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7608 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7609 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7611 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7612 new_rtx = make_extraction (mode, new_rtx,
7613 (GET_MODE_PRECISION (mode)
7614 - INTVAL (XEXP (XEXP (x, 0), 1))),
7615 NULL_RTX, i, 1, 0, in_code == COMPARE);
7618 /* On machines without logical shifts, if the operand of the AND is
7619 a logical shift and our mask turns off all the propagated sign
7620 bits, we can replace the logical shift with an arithmetic shift. */
7621 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7622 && !have_insn_for (LSHIFTRT, mode)
7623 && have_insn_for (ASHIFTRT, mode)
7624 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7625 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7626 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7627 && mode_width <= HOST_BITS_PER_WIDE_INT)
7629 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7631 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7632 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7633 SUBST (XEXP (x, 0),
7634 gen_rtx_ASHIFTRT (mode,
7635 make_compound_operation
7636 (XEXP (XEXP (x, 0), 0), next_code),
7637 XEXP (XEXP (x, 0), 1)));
7640 /* If the constant is one less than a power of two, this might be
7641 representable by an extraction even if no shift is present.
7642 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7643 we are in a COMPARE. */
7644 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7645 new_rtx = make_extraction (mode,
7646 make_compound_operation (XEXP (x, 0),
7647 next_code),
7648 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7650 /* If we are in a comparison and this is an AND with a power of two,
7651 convert this into the appropriate bit extract. */
7652 else if (in_code == COMPARE
7653 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7654 new_rtx = make_extraction (mode,
7655 make_compound_operation (XEXP (x, 0),
7656 next_code),
7657 i, NULL_RTX, 1, 1, 0, 1);
7659 break;
7661 case LSHIFTRT:
7662 /* If the sign bit is known to be zero, replace this with an
7663 arithmetic shift. */
7664 if (have_insn_for (ASHIFTRT, mode)
7665 && ! have_insn_for (LSHIFTRT, mode)
7666 && mode_width <= HOST_BITS_PER_WIDE_INT
7667 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7669 new_rtx = gen_rtx_ASHIFTRT (mode,
7670 make_compound_operation (XEXP (x, 0),
7671 next_code),
7672 XEXP (x, 1));
7673 break;
7676 /* ... fall through ... */
7678 case ASHIFTRT:
7679 lhs = XEXP (x, 0);
7680 rhs = XEXP (x, 1);
7682 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7683 this is a SIGN_EXTRACT. */
7684 if (CONST_INT_P (rhs)
7685 && GET_CODE (lhs) == ASHIFT
7686 && CONST_INT_P (XEXP (lhs, 1))
7687 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7688 && INTVAL (XEXP (lhs, 1)) >= 0
7689 && INTVAL (rhs) < mode_width)
7691 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7692 new_rtx = make_extraction (mode, new_rtx,
7693 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7694 NULL_RTX, mode_width - INTVAL (rhs),
7695 code == LSHIFTRT, 0, in_code == COMPARE);
7696 break;
7699 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7700 If so, try to merge the shifts into a SIGN_EXTEND. We could
7701 also do this for some cases of SIGN_EXTRACT, but it doesn't
7702 seem worth the effort; the case checked for occurs on Alpha. */
7704 if (!OBJECT_P (lhs)
7705 && ! (GET_CODE (lhs) == SUBREG
7706 && (OBJECT_P (SUBREG_REG (lhs))))
7707 && CONST_INT_P (rhs)
7708 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7709 && INTVAL (rhs) < mode_width
7710 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7711 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7712 0, NULL_RTX, mode_width - INTVAL (rhs),
7713 code == LSHIFTRT, 0, in_code == COMPARE);
7715 break;
7717 case SUBREG:
7718 /* Call ourselves recursively on the inner expression. If we are
7719 narrowing the object and it has a different RTL code from
7720 what it originally did, do this SUBREG as a force_to_mode. */
7722 rtx inner = SUBREG_REG (x), simplified;
7724 tem = make_compound_operation (inner, in_code);
7726 simplified
7727 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7728 if (simplified)
7729 tem = simplified;
7731 if (GET_CODE (tem) != GET_CODE (inner)
7732 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7733 && subreg_lowpart_p (x))
7735 rtx newer
7736 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7738 /* If we have something other than a SUBREG, we might have
7739 done an expansion, so rerun ourselves. */
7740 if (GET_CODE (newer) != SUBREG)
7741 newer = make_compound_operation (newer, in_code);
7743 /* force_to_mode can expand compounds. If it just re-expanded the
7744 compound, use gen_lowpart to convert to the desired mode. */
7745 if (rtx_equal_p (newer, x)
7746 /* Likewise if it re-expanded the compound only partially.
7747 This happens for SUBREG of ZERO_EXTRACT if they extract
7748 the same number of bits. */
7749 || (GET_CODE (newer) == SUBREG
7750 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7751 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7752 && GET_CODE (inner) == AND
7753 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7754 return gen_lowpart (GET_MODE (x), tem);
7756 return newer;
7759 if (simplified)
7760 return tem;
7762 break;
7764 default:
7765 break;
7768 if (new_rtx)
7770 x = gen_lowpart (mode, new_rtx);
7771 code = GET_CODE (x);
7774 /* Now recursively process each operand of this operation. We need to
7775 handle ZERO_EXTEND specially so that we don't lose track of the
7776 inner mode. */
7777 if (GET_CODE (x) == ZERO_EXTEND)
7779 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7780 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7781 new_rtx, GET_MODE (XEXP (x, 0)));
7782 if (tem)
7783 return tem;
7784 SUBST (XEXP (x, 0), new_rtx);
7785 return x;
7788 fmt = GET_RTX_FORMAT (code);
7789 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7790 if (fmt[i] == 'e')
7792 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7793 SUBST (XEXP (x, i), new_rtx);
7795 else if (fmt[i] == 'E')
7796 for (j = 0; j < XVECLEN (x, i); j++)
7798 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7799 SUBST (XVECEXP (x, i, j), new_rtx);
7802 maybe_swap:
7803 /* If this is a commutative operation, the changes to the operands
7804 may have made it noncanonical. */
7805 if (COMMUTATIVE_ARITH_P (x)
7806 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7808 tem = XEXP (x, 0);
7809 SUBST (XEXP (x, 0), XEXP (x, 1));
7810 SUBST (XEXP (x, 1), tem);
7813 return x;
7816 /* Given M see if it is a value that would select a field of bits
7817 within an item, but not the entire word. Return -1 if not.
7818 Otherwise, return the starting position of the field, where 0 is the
7819 low-order bit.
7821 *PLEN is set to the length of the field. */
7823 static int
7824 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7826 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7827 int pos = m ? ctz_hwi (m) : -1;
7828 int len = 0;
7830 if (pos >= 0)
7831 /* Now shift off the low-order zero bits and see if we have a
7832 power of two minus 1. */
7833 len = exact_log2 ((m >> pos) + 1);
7835 if (len <= 0)
7836 pos = -1;
7838 *plen = len;
7839 return pos;
7842 /* If X refers to a register that equals REG in value, replace these
7843 references with REG. */
7844 static rtx
7845 canon_reg_for_combine (rtx x, rtx reg)
7847 rtx op0, op1, op2;
7848 const char *fmt;
7849 int i;
7850 bool copied;
7852 enum rtx_code code = GET_CODE (x);
7853 switch (GET_RTX_CLASS (code))
7855 case RTX_UNARY:
7856 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7857 if (op0 != XEXP (x, 0))
7858 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7859 GET_MODE (reg));
7860 break;
7862 case RTX_BIN_ARITH:
7863 case RTX_COMM_ARITH:
7864 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7865 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7866 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7867 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7868 break;
7870 case RTX_COMPARE:
7871 case RTX_COMM_COMPARE:
7872 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7873 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7874 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7875 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7876 GET_MODE (op0), op0, op1);
7877 break;
7879 case RTX_TERNARY:
7880 case RTX_BITFIELD_OPS:
7881 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7882 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7883 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7884 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7885 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7886 GET_MODE (op0), op0, op1, op2);
7888 case RTX_OBJ:
7889 if (REG_P (x))
7891 if (rtx_equal_p (get_last_value (reg), x)
7892 || rtx_equal_p (reg, get_last_value (x)))
7893 return reg;
7894 else
7895 break;
7898 /* fall through */
7900 default:
7901 fmt = GET_RTX_FORMAT (code);
7902 copied = false;
7903 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7904 if (fmt[i] == 'e')
7906 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7907 if (op != XEXP (x, i))
7909 if (!copied)
7911 copied = true;
7912 x = copy_rtx (x);
7914 XEXP (x, i) = op;
7917 else if (fmt[i] == 'E')
7919 int j;
7920 for (j = 0; j < XVECLEN (x, i); j++)
7922 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7923 if (op != XVECEXP (x, i, j))
7925 if (!copied)
7927 copied = true;
7928 x = copy_rtx (x);
7930 XVECEXP (x, i, j) = op;
7935 break;
7938 return x;
7941 /* Return X converted to MODE. If the value is already truncated to
7942 MODE we can just return a subreg even though in the general case we
7943 would need an explicit truncation. */
7945 static rtx
7946 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7948 if (!CONST_INT_P (x)
7949 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7950 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
7951 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7953 /* Bit-cast X into an integer mode. */
7954 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7955 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7956 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7957 x, GET_MODE (x));
7960 return gen_lowpart (mode, x);
7963 /* See if X can be simplified knowing that we will only refer to it in
7964 MODE and will only refer to those bits that are nonzero in MASK.
7965 If other bits are being computed or if masking operations are done
7966 that select a superset of the bits in MASK, they can sometimes be
7967 ignored.
7969 Return a possibly simplified expression, but always convert X to
7970 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7972 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7973 are all off in X. This is used when X will be complemented, by either
7974 NOT, NEG, or XOR. */
7976 static rtx
7977 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7978 int just_select)
7980 enum rtx_code code = GET_CODE (x);
7981 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7982 enum machine_mode op_mode;
7983 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7984 rtx op0, op1, temp;
7986 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7987 code below will do the wrong thing since the mode of such an
7988 expression is VOIDmode.
7990 Also do nothing if X is a CLOBBER; this can happen if X was
7991 the return value from a call to gen_lowpart. */
7992 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7993 return x;
7995 /* We want to perform the operation is its present mode unless we know
7996 that the operation is valid in MODE, in which case we do the operation
7997 in MODE. */
7998 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7999 && have_insn_for (code, mode))
8000 ? mode : GET_MODE (x));
8002 /* It is not valid to do a right-shift in a narrower mode
8003 than the one it came in with. */
8004 if ((code == LSHIFTRT || code == ASHIFTRT)
8005 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8006 op_mode = GET_MODE (x);
8008 /* Truncate MASK to fit OP_MODE. */
8009 if (op_mode)
8010 mask &= GET_MODE_MASK (op_mode);
8012 /* When we have an arithmetic operation, or a shift whose count we
8013 do not know, we need to assume that all bits up to the highest-order
8014 bit in MASK will be needed. This is how we form such a mask. */
8015 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8016 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8017 else
8018 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8019 - 1);
8021 /* Determine what bits of X are guaranteed to be (non)zero. */
8022 nonzero = nonzero_bits (x, mode);
8024 /* If none of the bits in X are needed, return a zero. */
8025 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8026 x = const0_rtx;
8028 /* If X is a CONST_INT, return a new one. Do this here since the
8029 test below will fail. */
8030 if (CONST_INT_P (x))
8032 if (SCALAR_INT_MODE_P (mode))
8033 return gen_int_mode (INTVAL (x) & mask, mode);
8034 else
8036 x = GEN_INT (INTVAL (x) & mask);
8037 return gen_lowpart_common (mode, x);
8041 /* If X is narrower than MODE and we want all the bits in X's mode, just
8042 get X in the proper mode. */
8043 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8044 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8045 return gen_lowpart (mode, x);
8047 /* We can ignore the effect of a SUBREG if it narrows the mode or
8048 if the constant masks to zero all the bits the mode doesn't have. */
8049 if (GET_CODE (x) == SUBREG
8050 && subreg_lowpart_p (x)
8051 && ((GET_MODE_SIZE (GET_MODE (x))
8052 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8053 || (0 == (mask
8054 & GET_MODE_MASK (GET_MODE (x))
8055 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8056 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8058 /* The arithmetic simplifications here only work for scalar integer modes. */
8059 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8060 return gen_lowpart_or_truncate (mode, x);
8062 switch (code)
8064 case CLOBBER:
8065 /* If X is a (clobber (const_int)), return it since we know we are
8066 generating something that won't match. */
8067 return x;
8069 case SIGN_EXTEND:
8070 case ZERO_EXTEND:
8071 case ZERO_EXTRACT:
8072 case SIGN_EXTRACT:
8073 x = expand_compound_operation (x);
8074 if (GET_CODE (x) != code)
8075 return force_to_mode (x, mode, mask, next_select);
8076 break;
8078 case TRUNCATE:
8079 /* Similarly for a truncate. */
8080 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8082 case AND:
8083 /* If this is an AND with a constant, convert it into an AND
8084 whose constant is the AND of that constant with MASK. If it
8085 remains an AND of MASK, delete it since it is redundant. */
8087 if (CONST_INT_P (XEXP (x, 1)))
8089 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8090 mask & INTVAL (XEXP (x, 1)));
8092 /* If X is still an AND, see if it is an AND with a mask that
8093 is just some low-order bits. If so, and it is MASK, we don't
8094 need it. */
8096 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8097 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8098 == mask))
8099 x = XEXP (x, 0);
8101 /* If it remains an AND, try making another AND with the bits
8102 in the mode mask that aren't in MASK turned on. If the
8103 constant in the AND is wide enough, this might make a
8104 cheaper constant. */
8106 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8107 && GET_MODE_MASK (GET_MODE (x)) != mask
8108 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8110 unsigned HOST_WIDE_INT cval
8111 = UINTVAL (XEXP (x, 1))
8112 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8113 int width = GET_MODE_PRECISION (GET_MODE (x));
8114 rtx y;
8116 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8117 number, sign extend it. */
8118 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8119 && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8120 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8122 y = simplify_gen_binary (AND, GET_MODE (x),
8123 XEXP (x, 0), GEN_INT (cval));
8124 if (set_src_cost (y, optimize_this_for_speed_p)
8125 < set_src_cost (x, optimize_this_for_speed_p))
8126 x = y;
8129 break;
8132 goto binop;
8134 case PLUS:
8135 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8136 low-order bits (as in an alignment operation) and FOO is already
8137 aligned to that boundary, mask C1 to that boundary as well.
8138 This may eliminate that PLUS and, later, the AND. */
8141 unsigned int width = GET_MODE_PRECISION (mode);
8142 unsigned HOST_WIDE_INT smask = mask;
8144 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8145 number, sign extend it. */
8147 if (width < HOST_BITS_PER_WIDE_INT
8148 && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8149 smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8151 if (CONST_INT_P (XEXP (x, 1))
8152 && exact_log2 (- smask) >= 0
8153 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8154 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8155 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8156 (INTVAL (XEXP (x, 1)) & smask)),
8157 mode, smask, next_select);
8160 /* ... fall through ... */
8162 case MULT:
8163 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8164 most significant bit in MASK since carries from those bits will
8165 affect the bits we are interested in. */
8166 mask = fuller_mask;
8167 goto binop;
8169 case MINUS:
8170 /* If X is (minus C Y) where C's least set bit is larger than any bit
8171 in the mask, then we may replace with (neg Y). */
8172 if (CONST_INT_P (XEXP (x, 0))
8173 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8174 & -INTVAL (XEXP (x, 0))))
8175 > mask))
8177 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8178 GET_MODE (x));
8179 return force_to_mode (x, mode, mask, next_select);
8182 /* Similarly, if C contains every bit in the fuller_mask, then we may
8183 replace with (not Y). */
8184 if (CONST_INT_P (XEXP (x, 0))
8185 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8187 x = simplify_gen_unary (NOT, GET_MODE (x),
8188 XEXP (x, 1), GET_MODE (x));
8189 return force_to_mode (x, mode, mask, next_select);
8192 mask = fuller_mask;
8193 goto binop;
8195 case IOR:
8196 case XOR:
8197 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8198 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8199 operation which may be a bitfield extraction. Ensure that the
8200 constant we form is not wider than the mode of X. */
8202 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8203 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8204 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8205 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8206 && CONST_INT_P (XEXP (x, 1))
8207 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8208 + floor_log2 (INTVAL (XEXP (x, 1))))
8209 < GET_MODE_PRECISION (GET_MODE (x)))
8210 && (UINTVAL (XEXP (x, 1))
8211 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8213 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8214 << INTVAL (XEXP (XEXP (x, 0), 1)));
8215 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8216 XEXP (XEXP (x, 0), 0), temp);
8217 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8218 XEXP (XEXP (x, 0), 1));
8219 return force_to_mode (x, mode, mask, next_select);
8222 binop:
8223 /* For most binary operations, just propagate into the operation and
8224 change the mode if we have an operation of that mode. */
8226 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8227 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8229 /* If we ended up truncating both operands, truncate the result of the
8230 operation instead. */
8231 if (GET_CODE (op0) == TRUNCATE
8232 && GET_CODE (op1) == TRUNCATE)
8234 op0 = XEXP (op0, 0);
8235 op1 = XEXP (op1, 0);
8238 op0 = gen_lowpart_or_truncate (op_mode, op0);
8239 op1 = gen_lowpart_or_truncate (op_mode, op1);
8241 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8242 x = simplify_gen_binary (code, op_mode, op0, op1);
8243 break;
8245 case ASHIFT:
8246 /* For left shifts, do the same, but just for the first operand.
8247 However, we cannot do anything with shifts where we cannot
8248 guarantee that the counts are smaller than the size of the mode
8249 because such a count will have a different meaning in a
8250 wider mode. */
8252 if (! (CONST_INT_P (XEXP (x, 1))
8253 && INTVAL (XEXP (x, 1)) >= 0
8254 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8255 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8256 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8257 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8258 break;
8260 /* If the shift count is a constant and we can do arithmetic in
8261 the mode of the shift, refine which bits we need. Otherwise, use the
8262 conservative form of the mask. */
8263 if (CONST_INT_P (XEXP (x, 1))
8264 && INTVAL (XEXP (x, 1)) >= 0
8265 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8266 && HWI_COMPUTABLE_MODE_P (op_mode))
8267 mask >>= INTVAL (XEXP (x, 1));
8268 else
8269 mask = fuller_mask;
8271 op0 = gen_lowpart_or_truncate (op_mode,
8272 force_to_mode (XEXP (x, 0), op_mode,
8273 mask, next_select));
8275 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8276 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8277 break;
8279 case LSHIFTRT:
8280 /* Here we can only do something if the shift count is a constant,
8281 this shift constant is valid for the host, and we can do arithmetic
8282 in OP_MODE. */
8284 if (CONST_INT_P (XEXP (x, 1))
8285 && INTVAL (XEXP (x, 1)) >= 0
8286 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8287 && HWI_COMPUTABLE_MODE_P (op_mode))
8289 rtx inner = XEXP (x, 0);
8290 unsigned HOST_WIDE_INT inner_mask;
8292 /* Select the mask of the bits we need for the shift operand. */
8293 inner_mask = mask << INTVAL (XEXP (x, 1));
8295 /* We can only change the mode of the shift if we can do arithmetic
8296 in the mode of the shift and INNER_MASK is no wider than the
8297 width of X's mode. */
8298 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8299 op_mode = GET_MODE (x);
8301 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8303 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8304 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8307 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8308 shift and AND produces only copies of the sign bit (C2 is one less
8309 than a power of two), we can do this with just a shift. */
8311 if (GET_CODE (x) == LSHIFTRT
8312 && CONST_INT_P (XEXP (x, 1))
8313 /* The shift puts one of the sign bit copies in the least significant
8314 bit. */
8315 && ((INTVAL (XEXP (x, 1))
8316 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8317 >= GET_MODE_PRECISION (GET_MODE (x)))
8318 && exact_log2 (mask + 1) >= 0
8319 /* Number of bits left after the shift must be more than the mask
8320 needs. */
8321 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8322 <= GET_MODE_PRECISION (GET_MODE (x)))
8323 /* Must be more sign bit copies than the mask needs. */
8324 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8325 >= exact_log2 (mask + 1)))
8326 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8327 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8328 - exact_log2 (mask + 1)));
8330 goto shiftrt;
8332 case ASHIFTRT:
8333 /* If we are just looking for the sign bit, we don't need this shift at
8334 all, even if it has a variable count. */
8335 if (val_signbit_p (GET_MODE (x), mask))
8336 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8338 /* If this is a shift by a constant, get a mask that contains those bits
8339 that are not copies of the sign bit. We then have two cases: If
8340 MASK only includes those bits, this can be a logical shift, which may
8341 allow simplifications. If MASK is a single-bit field not within
8342 those bits, we are requesting a copy of the sign bit and hence can
8343 shift the sign bit to the appropriate location. */
8345 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8346 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8348 int i;
8350 /* If the considered data is wider than HOST_WIDE_INT, we can't
8351 represent a mask for all its bits in a single scalar.
8352 But we only care about the lower bits, so calculate these. */
8354 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8356 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8358 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8359 is the number of bits a full-width mask would have set.
8360 We need only shift if these are fewer than nonzero can
8361 hold. If not, we must keep all bits set in nonzero. */
8363 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8364 < HOST_BITS_PER_WIDE_INT)
8365 nonzero >>= INTVAL (XEXP (x, 1))
8366 + HOST_BITS_PER_WIDE_INT
8367 - GET_MODE_PRECISION (GET_MODE (x)) ;
8369 else
8371 nonzero = GET_MODE_MASK (GET_MODE (x));
8372 nonzero >>= INTVAL (XEXP (x, 1));
8375 if ((mask & ~nonzero) == 0)
8377 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8378 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8379 if (GET_CODE (x) != ASHIFTRT)
8380 return force_to_mode (x, mode, mask, next_select);
8383 else if ((i = exact_log2 (mask)) >= 0)
8385 x = simplify_shift_const
8386 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8387 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8389 if (GET_CODE (x) != ASHIFTRT)
8390 return force_to_mode (x, mode, mask, next_select);
8394 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8395 even if the shift count isn't a constant. */
8396 if (mask == 1)
8397 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8398 XEXP (x, 0), XEXP (x, 1));
8400 shiftrt:
8402 /* If this is a zero- or sign-extension operation that just affects bits
8403 we don't care about, remove it. Be sure the call above returned
8404 something that is still a shift. */
8406 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8407 && CONST_INT_P (XEXP (x, 1))
8408 && INTVAL (XEXP (x, 1)) >= 0
8409 && (INTVAL (XEXP (x, 1))
8410 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8411 && GET_CODE (XEXP (x, 0)) == ASHIFT
8412 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8413 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8414 next_select);
8416 break;
8418 case ROTATE:
8419 case ROTATERT:
8420 /* If the shift count is constant and we can do computations
8421 in the mode of X, compute where the bits we care about are.
8422 Otherwise, we can't do anything. Don't change the mode of
8423 the shift or propagate MODE into the shift, though. */
8424 if (CONST_INT_P (XEXP (x, 1))
8425 && INTVAL (XEXP (x, 1)) >= 0)
8427 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8428 GET_MODE (x), GEN_INT (mask),
8429 XEXP (x, 1));
8430 if (temp && CONST_INT_P (temp))
8431 SUBST (XEXP (x, 0),
8432 force_to_mode (XEXP (x, 0), GET_MODE (x),
8433 INTVAL (temp), next_select));
8435 break;
8437 case NEG:
8438 /* If we just want the low-order bit, the NEG isn't needed since it
8439 won't change the low-order bit. */
8440 if (mask == 1)
8441 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8443 /* We need any bits less significant than the most significant bit in
8444 MASK since carries from those bits will affect the bits we are
8445 interested in. */
8446 mask = fuller_mask;
8447 goto unop;
8449 case NOT:
8450 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8451 same as the XOR case above. Ensure that the constant we form is not
8452 wider than the mode of X. */
8454 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8455 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8456 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8457 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8458 < GET_MODE_PRECISION (GET_MODE (x)))
8459 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8461 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8462 GET_MODE (x));
8463 temp = simplify_gen_binary (XOR, GET_MODE (x),
8464 XEXP (XEXP (x, 0), 0), temp);
8465 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8466 temp, XEXP (XEXP (x, 0), 1));
8468 return force_to_mode (x, mode, mask, next_select);
8471 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8472 use the full mask inside the NOT. */
8473 mask = fuller_mask;
8475 unop:
8476 op0 = gen_lowpart_or_truncate (op_mode,
8477 force_to_mode (XEXP (x, 0), mode, mask,
8478 next_select));
8479 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8480 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8481 break;
8483 case NE:
8484 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8485 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8486 which is equal to STORE_FLAG_VALUE. */
8487 if ((mask & ~STORE_FLAG_VALUE) == 0
8488 && XEXP (x, 1) == const0_rtx
8489 && GET_MODE (XEXP (x, 0)) == mode
8490 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8491 && (nonzero_bits (XEXP (x, 0), mode)
8492 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8493 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8495 break;
8497 case IF_THEN_ELSE:
8498 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8499 written in a narrower mode. We play it safe and do not do so. */
8501 SUBST (XEXP (x, 1),
8502 gen_lowpart_or_truncate (GET_MODE (x),
8503 force_to_mode (XEXP (x, 1), mode,
8504 mask, next_select)));
8505 SUBST (XEXP (x, 2),
8506 gen_lowpart_or_truncate (GET_MODE (x),
8507 force_to_mode (XEXP (x, 2), mode,
8508 mask, next_select)));
8509 break;
8511 default:
8512 break;
8515 /* Ensure we return a value of the proper mode. */
8516 return gen_lowpart_or_truncate (mode, x);
8519 /* Return nonzero if X is an expression that has one of two values depending on
8520 whether some other value is zero or nonzero. In that case, we return the
8521 value that is being tested, *PTRUE is set to the value if the rtx being
8522 returned has a nonzero value, and *PFALSE is set to the other alternative.
8524 If we return zero, we set *PTRUE and *PFALSE to X. */
8526 static rtx
8527 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8529 enum machine_mode mode = GET_MODE (x);
8530 enum rtx_code code = GET_CODE (x);
8531 rtx cond0, cond1, true0, true1, false0, false1;
8532 unsigned HOST_WIDE_INT nz;
8534 /* If we are comparing a value against zero, we are done. */
8535 if ((code == NE || code == EQ)
8536 && XEXP (x, 1) == const0_rtx)
8538 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8539 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8540 return XEXP (x, 0);
8543 /* If this is a unary operation whose operand has one of two values, apply
8544 our opcode to compute those values. */
8545 else if (UNARY_P (x)
8546 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8548 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8549 *pfalse = simplify_gen_unary (code, mode, false0,
8550 GET_MODE (XEXP (x, 0)));
8551 return cond0;
8554 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8555 make can't possibly match and would suppress other optimizations. */
8556 else if (code == COMPARE)
8559 /* If this is a binary operation, see if either side has only one of two
8560 values. If either one does or if both do and they are conditional on
8561 the same value, compute the new true and false values. */
8562 else if (BINARY_P (x))
8564 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8565 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8567 if ((cond0 != 0 || cond1 != 0)
8568 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8570 /* If if_then_else_cond returned zero, then true/false are the
8571 same rtl. We must copy one of them to prevent invalid rtl
8572 sharing. */
8573 if (cond0 == 0)
8574 true0 = copy_rtx (true0);
8575 else if (cond1 == 0)
8576 true1 = copy_rtx (true1);
8578 if (COMPARISON_P (x))
8580 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8581 true0, true1);
8582 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8583 false0, false1);
8585 else
8587 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8588 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8591 return cond0 ? cond0 : cond1;
8594 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8595 operands is zero when the other is nonzero, and vice-versa,
8596 and STORE_FLAG_VALUE is 1 or -1. */
8598 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8599 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8600 || code == UMAX)
8601 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8603 rtx op0 = XEXP (XEXP (x, 0), 1);
8604 rtx op1 = XEXP (XEXP (x, 1), 1);
8606 cond0 = XEXP (XEXP (x, 0), 0);
8607 cond1 = XEXP (XEXP (x, 1), 0);
8609 if (COMPARISON_P (cond0)
8610 && COMPARISON_P (cond1)
8611 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8612 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8613 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8614 || ((swap_condition (GET_CODE (cond0))
8615 == reversed_comparison_code (cond1, NULL))
8616 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8617 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8618 && ! side_effects_p (x))
8620 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8621 *pfalse = simplify_gen_binary (MULT, mode,
8622 (code == MINUS
8623 ? simplify_gen_unary (NEG, mode,
8624 op1, mode)
8625 : op1),
8626 const_true_rtx);
8627 return cond0;
8631 /* Similarly for MULT, AND and UMIN, except that for these the result
8632 is always zero. */
8633 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8634 && (code == MULT || code == AND || code == UMIN)
8635 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8637 cond0 = XEXP (XEXP (x, 0), 0);
8638 cond1 = XEXP (XEXP (x, 1), 0);
8640 if (COMPARISON_P (cond0)
8641 && COMPARISON_P (cond1)
8642 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8643 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8644 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8645 || ((swap_condition (GET_CODE (cond0))
8646 == reversed_comparison_code (cond1, NULL))
8647 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8648 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8649 && ! side_effects_p (x))
8651 *ptrue = *pfalse = const0_rtx;
8652 return cond0;
8657 else if (code == IF_THEN_ELSE)
8659 /* If we have IF_THEN_ELSE already, extract the condition and
8660 canonicalize it if it is NE or EQ. */
8661 cond0 = XEXP (x, 0);
8662 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8663 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8664 return XEXP (cond0, 0);
8665 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8667 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8668 return XEXP (cond0, 0);
8670 else
8671 return cond0;
8674 /* If X is a SUBREG, we can narrow both the true and false values
8675 if the inner expression, if there is a condition. */
8676 else if (code == SUBREG
8677 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8678 &true0, &false0)))
8680 true0 = simplify_gen_subreg (mode, true0,
8681 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8682 false0 = simplify_gen_subreg (mode, false0,
8683 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8684 if (true0 && false0)
8686 *ptrue = true0;
8687 *pfalse = false0;
8688 return cond0;
8692 /* If X is a constant, this isn't special and will cause confusions
8693 if we treat it as such. Likewise if it is equivalent to a constant. */
8694 else if (CONSTANT_P (x)
8695 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8698 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8699 will be least confusing to the rest of the compiler. */
8700 else if (mode == BImode)
8702 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8703 return x;
8706 /* If X is known to be either 0 or -1, those are the true and
8707 false values when testing X. */
8708 else if (x == constm1_rtx || x == const0_rtx
8709 || (mode != VOIDmode
8710 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8712 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8713 return x;
8716 /* Likewise for 0 or a single bit. */
8717 else if (HWI_COMPUTABLE_MODE_P (mode)
8718 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8720 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8721 return x;
8724 /* Otherwise fail; show no condition with true and false values the same. */
8725 *ptrue = *pfalse = x;
8726 return 0;
8729 /* Return the value of expression X given the fact that condition COND
8730 is known to be true when applied to REG as its first operand and VAL
8731 as its second. X is known to not be shared and so can be modified in
8732 place.
8734 We only handle the simplest cases, and specifically those cases that
8735 arise with IF_THEN_ELSE expressions. */
8737 static rtx
8738 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8740 enum rtx_code code = GET_CODE (x);
8741 rtx temp;
8742 const char *fmt;
8743 int i, j;
8745 if (side_effects_p (x))
8746 return x;
8748 /* If either operand of the condition is a floating point value,
8749 then we have to avoid collapsing an EQ comparison. */
8750 if (cond == EQ
8751 && rtx_equal_p (x, reg)
8752 && ! FLOAT_MODE_P (GET_MODE (x))
8753 && ! FLOAT_MODE_P (GET_MODE (val)))
8754 return val;
8756 if (cond == UNEQ && rtx_equal_p (x, reg))
8757 return val;
8759 /* If X is (abs REG) and we know something about REG's relationship
8760 with zero, we may be able to simplify this. */
8762 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8763 switch (cond)
8765 case GE: case GT: case EQ:
8766 return XEXP (x, 0);
8767 case LT: case LE:
8768 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8769 XEXP (x, 0),
8770 GET_MODE (XEXP (x, 0)));
8771 default:
8772 break;
8775 /* The only other cases we handle are MIN, MAX, and comparisons if the
8776 operands are the same as REG and VAL. */
8778 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8780 if (rtx_equal_p (XEXP (x, 0), val))
8781 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8783 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8785 if (COMPARISON_P (x))
8787 if (comparison_dominates_p (cond, code))
8788 return const_true_rtx;
8790 code = reversed_comparison_code (x, NULL);
8791 if (code != UNKNOWN
8792 && comparison_dominates_p (cond, code))
8793 return const0_rtx;
8794 else
8795 return x;
8797 else if (code == SMAX || code == SMIN
8798 || code == UMIN || code == UMAX)
8800 int unsignedp = (code == UMIN || code == UMAX);
8802 /* Do not reverse the condition when it is NE or EQ.
8803 This is because we cannot conclude anything about
8804 the value of 'SMAX (x, y)' when x is not equal to y,
8805 but we can when x equals y. */
8806 if ((code == SMAX || code == UMAX)
8807 && ! (cond == EQ || cond == NE))
8808 cond = reverse_condition (cond);
8810 switch (cond)
8812 case GE: case GT:
8813 return unsignedp ? x : XEXP (x, 1);
8814 case LE: case LT:
8815 return unsignedp ? x : XEXP (x, 0);
8816 case GEU: case GTU:
8817 return unsignedp ? XEXP (x, 1) : x;
8818 case LEU: case LTU:
8819 return unsignedp ? XEXP (x, 0) : x;
8820 default:
8821 break;
8826 else if (code == SUBREG)
8828 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8829 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8831 if (SUBREG_REG (x) != r)
8833 /* We must simplify subreg here, before we lose track of the
8834 original inner_mode. */
8835 new_rtx = simplify_subreg (GET_MODE (x), r,
8836 inner_mode, SUBREG_BYTE (x));
8837 if (new_rtx)
8838 return new_rtx;
8839 else
8840 SUBST (SUBREG_REG (x), r);
8843 return x;
8845 /* We don't have to handle SIGN_EXTEND here, because even in the
8846 case of replacing something with a modeless CONST_INT, a
8847 CONST_INT is already (supposed to be) a valid sign extension for
8848 its narrower mode, which implies it's already properly
8849 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8850 story is different. */
8851 else if (code == ZERO_EXTEND)
8853 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8854 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8856 if (XEXP (x, 0) != r)
8858 /* We must simplify the zero_extend here, before we lose
8859 track of the original inner_mode. */
8860 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8861 r, inner_mode);
8862 if (new_rtx)
8863 return new_rtx;
8864 else
8865 SUBST (XEXP (x, 0), r);
8868 return x;
8871 fmt = GET_RTX_FORMAT (code);
8872 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8874 if (fmt[i] == 'e')
8875 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8876 else if (fmt[i] == 'E')
8877 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8878 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8879 cond, reg, val));
8882 return x;
8885 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8886 assignment as a field assignment. */
8888 static int
8889 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8891 if (x == y || rtx_equal_p (x, y))
8892 return 1;
8894 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8895 return 0;
8897 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8898 Note that all SUBREGs of MEM are paradoxical; otherwise they
8899 would have been rewritten. */
8900 if (MEM_P (x) && GET_CODE (y) == SUBREG
8901 && MEM_P (SUBREG_REG (y))
8902 && rtx_equal_p (SUBREG_REG (y),
8903 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8904 return 1;
8906 if (MEM_P (y) && GET_CODE (x) == SUBREG
8907 && MEM_P (SUBREG_REG (x))
8908 && rtx_equal_p (SUBREG_REG (x),
8909 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8910 return 1;
8912 /* We used to see if get_last_value of X and Y were the same but that's
8913 not correct. In one direction, we'll cause the assignment to have
8914 the wrong destination and in the case, we'll import a register into this
8915 insn that might have already have been dead. So fail if none of the
8916 above cases are true. */
8917 return 0;
8920 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8921 Return that assignment if so.
8923 We only handle the most common cases. */
8925 static rtx
8926 make_field_assignment (rtx x)
8928 rtx dest = SET_DEST (x);
8929 rtx src = SET_SRC (x);
8930 rtx assign;
8931 rtx rhs, lhs;
8932 HOST_WIDE_INT c1;
8933 HOST_WIDE_INT pos;
8934 unsigned HOST_WIDE_INT len;
8935 rtx other;
8936 enum machine_mode mode;
8938 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8939 a clear of a one-bit field. We will have changed it to
8940 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8941 for a SUBREG. */
8943 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8944 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8945 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8946 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8948 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8949 1, 1, 1, 0);
8950 if (assign != 0)
8951 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8952 return x;
8955 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8956 && subreg_lowpart_p (XEXP (src, 0))
8957 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8958 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8959 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8960 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8961 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8962 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8964 assign = make_extraction (VOIDmode, dest, 0,
8965 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8966 1, 1, 1, 0);
8967 if (assign != 0)
8968 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8969 return x;
8972 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8973 one-bit field. */
8974 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8975 && XEXP (XEXP (src, 0), 0) == const1_rtx
8976 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8978 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8979 1, 1, 1, 0);
8980 if (assign != 0)
8981 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8982 return x;
8985 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8986 SRC is an AND with all bits of that field set, then we can discard
8987 the AND. */
8988 if (GET_CODE (dest) == ZERO_EXTRACT
8989 && CONST_INT_P (XEXP (dest, 1))
8990 && GET_CODE (src) == AND
8991 && CONST_INT_P (XEXP (src, 1)))
8993 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8994 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8995 unsigned HOST_WIDE_INT ze_mask;
8997 if (width >= HOST_BITS_PER_WIDE_INT)
8998 ze_mask = -1;
8999 else
9000 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9002 /* Complete overlap. We can remove the source AND. */
9003 if ((and_mask & ze_mask) == ze_mask)
9004 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9006 /* Partial overlap. We can reduce the source AND. */
9007 if ((and_mask & ze_mask) != and_mask)
9009 mode = GET_MODE (src);
9010 src = gen_rtx_AND (mode, XEXP (src, 0),
9011 gen_int_mode (and_mask & ze_mask, mode));
9012 return gen_rtx_SET (VOIDmode, dest, src);
9016 /* The other case we handle is assignments into a constant-position
9017 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9018 a mask that has all one bits except for a group of zero bits and
9019 OTHER is known to have zeros where C1 has ones, this is such an
9020 assignment. Compute the position and length from C1. Shift OTHER
9021 to the appropriate position, force it to the required mode, and
9022 make the extraction. Check for the AND in both operands. */
9024 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9025 return x;
9027 rhs = expand_compound_operation (XEXP (src, 0));
9028 lhs = expand_compound_operation (XEXP (src, 1));
9030 if (GET_CODE (rhs) == AND
9031 && CONST_INT_P (XEXP (rhs, 1))
9032 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9033 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9034 else if (GET_CODE (lhs) == AND
9035 && CONST_INT_P (XEXP (lhs, 1))
9036 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9037 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9038 else
9039 return x;
9041 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9042 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9043 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9044 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9045 return x;
9047 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9048 if (assign == 0)
9049 return x;
9051 /* The mode to use for the source is the mode of the assignment, or of
9052 what is inside a possible STRICT_LOW_PART. */
9053 mode = (GET_CODE (assign) == STRICT_LOW_PART
9054 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9056 /* Shift OTHER right POS places and make it the source, restricting it
9057 to the proper length and mode. */
9059 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9060 GET_MODE (src),
9061 other, pos),
9062 dest);
9063 src = force_to_mode (src, mode,
9064 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9065 ? ~(unsigned HOST_WIDE_INT) 0
9066 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9069 /* If SRC is masked by an AND that does not make a difference in
9070 the value being stored, strip it. */
9071 if (GET_CODE (assign) == ZERO_EXTRACT
9072 && CONST_INT_P (XEXP (assign, 1))
9073 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9074 && GET_CODE (src) == AND
9075 && CONST_INT_P (XEXP (src, 1))
9076 && UINTVAL (XEXP (src, 1))
9077 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9078 src = XEXP (src, 0);
9080 return gen_rtx_SET (VOIDmode, assign, src);
9083 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9084 if so. */
9086 static rtx
9087 apply_distributive_law (rtx x)
9089 enum rtx_code code = GET_CODE (x);
9090 enum rtx_code inner_code;
9091 rtx lhs, rhs, other;
9092 rtx tem;
9094 /* Distributivity is not true for floating point as it can change the
9095 value. So we don't do it unless -funsafe-math-optimizations. */
9096 if (FLOAT_MODE_P (GET_MODE (x))
9097 && ! flag_unsafe_math_optimizations)
9098 return x;
9100 /* The outer operation can only be one of the following: */
9101 if (code != IOR && code != AND && code != XOR
9102 && code != PLUS && code != MINUS)
9103 return x;
9105 lhs = XEXP (x, 0);
9106 rhs = XEXP (x, 1);
9108 /* If either operand is a primitive we can't do anything, so get out
9109 fast. */
9110 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9111 return x;
9113 lhs = expand_compound_operation (lhs);
9114 rhs = expand_compound_operation (rhs);
9115 inner_code = GET_CODE (lhs);
9116 if (inner_code != GET_CODE (rhs))
9117 return x;
9119 /* See if the inner and outer operations distribute. */
9120 switch (inner_code)
9122 case LSHIFTRT:
9123 case ASHIFTRT:
9124 case AND:
9125 case IOR:
9126 /* These all distribute except over PLUS. */
9127 if (code == PLUS || code == MINUS)
9128 return x;
9129 break;
9131 case MULT:
9132 if (code != PLUS && code != MINUS)
9133 return x;
9134 break;
9136 case ASHIFT:
9137 /* This is also a multiply, so it distributes over everything. */
9138 break;
9140 /* This used to handle SUBREG, but this turned out to be counter-
9141 productive, since (subreg (op ...)) usually is not handled by
9142 insn patterns, and this "optimization" therefore transformed
9143 recognizable patterns into unrecognizable ones. Therefore the
9144 SUBREG case was removed from here.
9146 It is possible that distributing SUBREG over arithmetic operations
9147 leads to an intermediate result than can then be optimized further,
9148 e.g. by moving the outer SUBREG to the other side of a SET as done
9149 in simplify_set. This seems to have been the original intent of
9150 handling SUBREGs here.
9152 However, with current GCC this does not appear to actually happen,
9153 at least on major platforms. If some case is found where removing
9154 the SUBREG case here prevents follow-on optimizations, distributing
9155 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9157 default:
9158 return x;
9161 /* Set LHS and RHS to the inner operands (A and B in the example
9162 above) and set OTHER to the common operand (C in the example).
9163 There is only one way to do this unless the inner operation is
9164 commutative. */
9165 if (COMMUTATIVE_ARITH_P (lhs)
9166 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9167 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9168 else if (COMMUTATIVE_ARITH_P (lhs)
9169 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9170 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9171 else if (COMMUTATIVE_ARITH_P (lhs)
9172 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9173 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9174 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9175 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9176 else
9177 return x;
9179 /* Form the new inner operation, seeing if it simplifies first. */
9180 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9182 /* There is one exception to the general way of distributing:
9183 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9184 if (code == XOR && inner_code == IOR)
9186 inner_code = AND;
9187 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9190 /* We may be able to continuing distributing the result, so call
9191 ourselves recursively on the inner operation before forming the
9192 outer operation, which we return. */
9193 return simplify_gen_binary (inner_code, GET_MODE (x),
9194 apply_distributive_law (tem), other);
9197 /* See if X is of the form (* (+ A B) C), and if so convert to
9198 (+ (* A C) (* B C)) and try to simplify.
9200 Most of the time, this results in no change. However, if some of
9201 the operands are the same or inverses of each other, simplifications
9202 will result.
9204 For example, (and (ior A B) (not B)) can occur as the result of
9205 expanding a bit field assignment. When we apply the distributive
9206 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9207 which then simplifies to (and (A (not B))).
9209 Note that no checks happen on the validity of applying the inverse
9210 distributive law. This is pointless since we can do it in the
9211 few places where this routine is called.
9213 N is the index of the term that is decomposed (the arithmetic operation,
9214 i.e. (+ A B) in the first example above). !N is the index of the term that
9215 is distributed, i.e. of C in the first example above. */
9216 static rtx
9217 distribute_and_simplify_rtx (rtx x, int n)
9219 enum machine_mode mode;
9220 enum rtx_code outer_code, inner_code;
9221 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9223 /* Distributivity is not true for floating point as it can change the
9224 value. So we don't do it unless -funsafe-math-optimizations. */
9225 if (FLOAT_MODE_P (GET_MODE (x))
9226 && ! flag_unsafe_math_optimizations)
9227 return NULL_RTX;
9229 decomposed = XEXP (x, n);
9230 if (!ARITHMETIC_P (decomposed))
9231 return NULL_RTX;
9233 mode = GET_MODE (x);
9234 outer_code = GET_CODE (x);
9235 distributed = XEXP (x, !n);
9237 inner_code = GET_CODE (decomposed);
9238 inner_op0 = XEXP (decomposed, 0);
9239 inner_op1 = XEXP (decomposed, 1);
9241 /* Special case (and (xor B C) (not A)), which is equivalent to
9242 (xor (ior A B) (ior A C)) */
9243 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9245 distributed = XEXP (distributed, 0);
9246 outer_code = IOR;
9249 if (n == 0)
9251 /* Distribute the second term. */
9252 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9253 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9255 else
9257 /* Distribute the first term. */
9258 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9259 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9262 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9263 new_op0, new_op1));
9264 if (GET_CODE (tmp) != outer_code
9265 && (set_src_cost (tmp, optimize_this_for_speed_p)
9266 < set_src_cost (x, optimize_this_for_speed_p)))
9267 return tmp;
9269 return NULL_RTX;
9272 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9273 in MODE. Return an equivalent form, if different from (and VAROP
9274 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9276 static rtx
9277 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9278 unsigned HOST_WIDE_INT constop)
9280 unsigned HOST_WIDE_INT nonzero;
9281 unsigned HOST_WIDE_INT orig_constop;
9282 rtx orig_varop;
9283 int i;
9285 orig_varop = varop;
9286 orig_constop = constop;
9287 if (GET_CODE (varop) == CLOBBER)
9288 return NULL_RTX;
9290 /* Simplify VAROP knowing that we will be only looking at some of the
9291 bits in it.
9293 Note by passing in CONSTOP, we guarantee that the bits not set in
9294 CONSTOP are not significant and will never be examined. We must
9295 ensure that is the case by explicitly masking out those bits
9296 before returning. */
9297 varop = force_to_mode (varop, mode, constop, 0);
9299 /* If VAROP is a CLOBBER, we will fail so return it. */
9300 if (GET_CODE (varop) == CLOBBER)
9301 return varop;
9303 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9304 to VAROP and return the new constant. */
9305 if (CONST_INT_P (varop))
9306 return gen_int_mode (INTVAL (varop) & constop, mode);
9308 /* See what bits may be nonzero in VAROP. Unlike the general case of
9309 a call to nonzero_bits, here we don't care about bits outside
9310 MODE. */
9312 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9314 /* Turn off all bits in the constant that are known to already be zero.
9315 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9316 which is tested below. */
9318 constop &= nonzero;
9320 /* If we don't have any bits left, return zero. */
9321 if (constop == 0)
9322 return const0_rtx;
9324 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9325 a power of two, we can replace this with an ASHIFT. */
9326 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9327 && (i = exact_log2 (constop)) >= 0)
9328 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9330 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9331 or XOR, then try to apply the distributive law. This may eliminate
9332 operations if either branch can be simplified because of the AND.
9333 It may also make some cases more complex, but those cases probably
9334 won't match a pattern either with or without this. */
9336 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9337 return
9338 gen_lowpart
9339 (mode,
9340 apply_distributive_law
9341 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9342 simplify_and_const_int (NULL_RTX,
9343 GET_MODE (varop),
9344 XEXP (varop, 0),
9345 constop),
9346 simplify_and_const_int (NULL_RTX,
9347 GET_MODE (varop),
9348 XEXP (varop, 1),
9349 constop))));
9351 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9352 the AND and see if one of the operands simplifies to zero. If so, we
9353 may eliminate it. */
9355 if (GET_CODE (varop) == PLUS
9356 && exact_log2 (constop + 1) >= 0)
9358 rtx o0, o1;
9360 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9361 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9362 if (o0 == const0_rtx)
9363 return o1;
9364 if (o1 == const0_rtx)
9365 return o0;
9368 /* Make a SUBREG if necessary. If we can't make it, fail. */
9369 varop = gen_lowpart (mode, varop);
9370 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9371 return NULL_RTX;
9373 /* If we are only masking insignificant bits, return VAROP. */
9374 if (constop == nonzero)
9375 return varop;
9377 if (varop == orig_varop && constop == orig_constop)
9378 return NULL_RTX;
9380 /* Otherwise, return an AND. */
9381 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9385 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9386 in MODE.
9388 Return an equivalent form, if different from X. Otherwise, return X. If
9389 X is zero, we are to always construct the equivalent form. */
9391 static rtx
9392 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9393 unsigned HOST_WIDE_INT constop)
9395 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9396 if (tem)
9397 return tem;
9399 if (!x)
9400 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9401 gen_int_mode (constop, mode));
9402 if (GET_MODE (x) != mode)
9403 x = gen_lowpart (mode, x);
9404 return x;
9407 /* Given a REG, X, compute which bits in X can be nonzero.
9408 We don't care about bits outside of those defined in MODE.
9410 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9411 a shift, AND, or zero_extract, we can do better. */
9413 static rtx
9414 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9415 const_rtx known_x ATTRIBUTE_UNUSED,
9416 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9417 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9418 unsigned HOST_WIDE_INT *nonzero)
9420 rtx tem;
9421 reg_stat_type *rsp;
9423 /* If X is a register whose nonzero bits value is current, use it.
9424 Otherwise, if X is a register whose value we can find, use that
9425 value. Otherwise, use the previously-computed global nonzero bits
9426 for this register. */
9428 rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
9429 if (rsp->last_set_value != 0
9430 && (rsp->last_set_mode == mode
9431 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9432 && GET_MODE_CLASS (mode) == MODE_INT))
9433 && ((rsp->last_set_label >= label_tick_ebb_start
9434 && rsp->last_set_label < label_tick)
9435 || (rsp->last_set_label == label_tick
9436 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9437 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9438 && REG_N_SETS (REGNO (x)) == 1
9439 && !REGNO_REG_SET_P
9440 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9442 *nonzero &= rsp->last_set_nonzero_bits;
9443 return NULL;
9446 tem = get_last_value (x);
9448 if (tem)
9450 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9451 /* If X is narrower than MODE and TEM is a non-negative
9452 constant that would appear negative in the mode of X,
9453 sign-extend it for use in reg_nonzero_bits because some
9454 machines (maybe most) will actually do the sign-extension
9455 and this is the conservative approach.
9457 ??? For 2.5, try to tighten up the MD files in this regard
9458 instead of this kludge. */
9460 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9461 && CONST_INT_P (tem)
9462 && INTVAL (tem) > 0
9463 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9464 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9465 #endif
9466 return tem;
9468 else if (nonzero_sign_valid && rsp->nonzero_bits)
9470 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9472 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9473 /* We don't know anything about the upper bits. */
9474 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9475 *nonzero &= mask;
9478 return NULL;
9481 /* Return the number of bits at the high-order end of X that are known to
9482 be equal to the sign bit. X will be used in mode MODE; if MODE is
9483 VOIDmode, X will be used in its own mode. The returned value will always
9484 be between 1 and the number of bits in MODE. */
9486 static rtx
9487 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9488 const_rtx known_x ATTRIBUTE_UNUSED,
9489 enum machine_mode known_mode
9490 ATTRIBUTE_UNUSED,
9491 unsigned int known_ret ATTRIBUTE_UNUSED,
9492 unsigned int *result)
9494 rtx tem;
9495 reg_stat_type *rsp;
9497 rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
9498 if (rsp->last_set_value != 0
9499 && rsp->last_set_mode == mode
9500 && ((rsp->last_set_label >= label_tick_ebb_start
9501 && rsp->last_set_label < label_tick)
9502 || (rsp->last_set_label == label_tick
9503 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9504 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9505 && REG_N_SETS (REGNO (x)) == 1
9506 && !REGNO_REG_SET_P
9507 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9509 *result = rsp->last_set_sign_bit_copies;
9510 return NULL;
9513 tem = get_last_value (x);
9514 if (tem != 0)
9515 return tem;
9517 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9518 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9519 *result = rsp->sign_bit_copies;
9521 return NULL;
9524 /* Return the number of "extended" bits there are in X, when interpreted
9525 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9526 unsigned quantities, this is the number of high-order zero bits.
9527 For signed quantities, this is the number of copies of the sign bit
9528 minus 1. In both case, this function returns the number of "spare"
9529 bits. For example, if two quantities for which this function returns
9530 at least 1 are added, the addition is known not to overflow.
9532 This function will always return 0 unless called during combine, which
9533 implies that it must be called from a define_split. */
9535 unsigned int
9536 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9538 if (nonzero_sign_valid == 0)
9539 return 0;
9541 return (unsignedp
9542 ? (HWI_COMPUTABLE_MODE_P (mode)
9543 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9544 - floor_log2 (nonzero_bits (x, mode)))
9545 : 0)
9546 : num_sign_bit_copies (x, mode) - 1);
9549 /* This function is called from `simplify_shift_const' to merge two
9550 outer operations. Specifically, we have already found that we need
9551 to perform operation *POP0 with constant *PCONST0 at the outermost
9552 position. We would now like to also perform OP1 with constant CONST1
9553 (with *POP0 being done last).
9555 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9556 the resulting operation. *PCOMP_P is set to 1 if we would need to
9557 complement the innermost operand, otherwise it is unchanged.
9559 MODE is the mode in which the operation will be done. No bits outside
9560 the width of this mode matter. It is assumed that the width of this mode
9561 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9563 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9564 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9565 result is simply *PCONST0.
9567 If the resulting operation cannot be expressed as one operation, we
9568 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9570 static int
9571 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)
9573 enum rtx_code op0 = *pop0;
9574 HOST_WIDE_INT const0 = *pconst0;
9576 const0 &= GET_MODE_MASK (mode);
9577 const1 &= GET_MODE_MASK (mode);
9579 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9580 if (op0 == AND)
9581 const1 &= const0;
9583 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9584 if OP0 is SET. */
9586 if (op1 == UNKNOWN || op0 == SET)
9587 return 1;
9589 else if (op0 == UNKNOWN)
9590 op0 = op1, const0 = const1;
9592 else if (op0 == op1)
9594 switch (op0)
9596 case AND:
9597 const0 &= const1;
9598 break;
9599 case IOR:
9600 const0 |= const1;
9601 break;
9602 case XOR:
9603 const0 ^= const1;
9604 break;
9605 case PLUS:
9606 const0 += const1;
9607 break;
9608 case NEG:
9609 op0 = UNKNOWN;
9610 break;
9611 default:
9612 break;
9616 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9617 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9618 return 0;
9620 /* If the two constants aren't the same, we can't do anything. The
9621 remaining six cases can all be done. */
9622 else if (const0 != const1)
9623 return 0;
9625 else
9626 switch (op0)
9628 case IOR:
9629 if (op1 == AND)
9630 /* (a & b) | b == b */
9631 op0 = SET;
9632 else /* op1 == XOR */
9633 /* (a ^ b) | b == a | b */
9635 break;
9637 case XOR:
9638 if (op1 == AND)
9639 /* (a & b) ^ b == (~a) & b */
9640 op0 = AND, *pcomp_p = 1;
9641 else /* op1 == IOR */
9642 /* (a | b) ^ b == a & ~b */
9643 op0 = AND, const0 = ~const0;
9644 break;
9646 case AND:
9647 if (op1 == IOR)
9648 /* (a | b) & b == b */
9649 op0 = SET;
9650 else /* op1 == XOR */
9651 /* (a ^ b) & b) == (~a) & b */
9652 *pcomp_p = 1;
9653 break;
9654 default:
9655 break;
9658 /* Check for NO-OP cases. */
9659 const0 &= GET_MODE_MASK (mode);
9660 if (const0 == 0
9661 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9662 op0 = UNKNOWN;
9663 else if (const0 == 0 && op0 == AND)
9664 op0 = SET;
9665 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9666 && op0 == AND)
9667 op0 = UNKNOWN;
9669 *pop0 = op0;
9671 /* ??? Slightly redundant with the above mask, but not entirely.
9672 Moving this above means we'd have to sign-extend the mode mask
9673 for the final test. */
9674 if (op0 != UNKNOWN && op0 != NEG)
9675 *pconst0 = trunc_int_for_mode (const0, mode);
9677 return 1;
9680 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9681 the shift in. The original shift operation CODE is performed on OP in
9682 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9683 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9684 result of the shift is subject to operation OUTER_CODE with operand
9685 OUTER_CONST. */
9687 static enum machine_mode
9688 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9689 enum machine_mode orig_mode, enum machine_mode mode,
9690 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9692 if (orig_mode == mode)
9693 return mode;
9694 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9696 /* In general we can't perform in wider mode for right shift and rotate. */
9697 switch (code)
9699 case ASHIFTRT:
9700 /* We can still widen if the bits brought in from the left are identical
9701 to the sign bit of ORIG_MODE. */
9702 if (num_sign_bit_copies (op, mode)
9703 > (unsigned) (GET_MODE_PRECISION (mode)
9704 - GET_MODE_PRECISION (orig_mode)))
9705 return mode;
9706 return orig_mode;
9708 case LSHIFTRT:
9709 /* Similarly here but with zero bits. */
9710 if (HWI_COMPUTABLE_MODE_P (mode)
9711 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9712 return mode;
9714 /* We can also widen if the bits brought in will be masked off. This
9715 operation is performed in ORIG_MODE. */
9716 if (outer_code == AND)
9718 int care_bits = low_bitmask_len (orig_mode, outer_const);
9720 if (care_bits >= 0
9721 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9722 return mode;
9724 /* fall through */
9726 case ROTATE:
9727 return orig_mode;
9729 case ROTATERT:
9730 gcc_unreachable ();
9732 default:
9733 return mode;
9737 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9738 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9739 if we cannot simplify it. Otherwise, return a simplified value.
9741 The shift is normally computed in the widest mode we find in VAROP, as
9742 long as it isn't a different number of words than RESULT_MODE. Exceptions
9743 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9745 static rtx
9746 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9747 rtx varop, int orig_count)
9749 enum rtx_code orig_code = code;
9750 rtx orig_varop = varop;
9751 int count;
9752 enum machine_mode mode = result_mode;
9753 enum machine_mode shift_mode, tmode;
9754 unsigned int mode_words
9755 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9756 /* We form (outer_op (code varop count) (outer_const)). */
9757 enum rtx_code outer_op = UNKNOWN;
9758 HOST_WIDE_INT outer_const = 0;
9759 int complement_p = 0;
9760 rtx new_rtx, x;
9762 /* Make sure and truncate the "natural" shift on the way in. We don't
9763 want to do this inside the loop as it makes it more difficult to
9764 combine shifts. */
9765 if (SHIFT_COUNT_TRUNCATED)
9766 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9768 /* If we were given an invalid count, don't do anything except exactly
9769 what was requested. */
9771 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9772 return NULL_RTX;
9774 count = orig_count;
9776 /* Unless one of the branches of the `if' in this loop does a `continue',
9777 we will `break' the loop after the `if'. */
9779 while (count != 0)
9781 /* If we have an operand of (clobber (const_int 0)), fail. */
9782 if (GET_CODE (varop) == CLOBBER)
9783 return NULL_RTX;
9785 /* Convert ROTATERT to ROTATE. */
9786 if (code == ROTATERT)
9788 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9789 code = ROTATE;
9790 if (VECTOR_MODE_P (result_mode))
9791 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9792 else
9793 count = bitsize - count;
9796 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9797 mode, outer_op, outer_const);
9799 /* Handle cases where the count is greater than the size of the mode
9800 minus 1. For ASHIFT, use the size minus one as the count (this can
9801 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9802 take the count modulo the size. For other shifts, the result is
9803 zero.
9805 Since these shifts are being produced by the compiler by combining
9806 multiple operations, each of which are defined, we know what the
9807 result is supposed to be. */
9809 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
9811 if (code == ASHIFTRT)
9812 count = GET_MODE_PRECISION (shift_mode) - 1;
9813 else if (code == ROTATE || code == ROTATERT)
9814 count %= GET_MODE_PRECISION (shift_mode);
9815 else
9817 /* We can't simply return zero because there may be an
9818 outer op. */
9819 varop = const0_rtx;
9820 count = 0;
9821 break;
9825 /* If we discovered we had to complement VAROP, leave. Making a NOT
9826 here would cause an infinite loop. */
9827 if (complement_p)
9828 break;
9830 /* An arithmetic right shift of a quantity known to be -1 or 0
9831 is a no-op. */
9832 if (code == ASHIFTRT
9833 && (num_sign_bit_copies (varop, shift_mode)
9834 == GET_MODE_PRECISION (shift_mode)))
9836 count = 0;
9837 break;
9840 /* If we are doing an arithmetic right shift and discarding all but
9841 the sign bit copies, this is equivalent to doing a shift by the
9842 bitsize minus one. Convert it into that shift because it will often
9843 allow other simplifications. */
9845 if (code == ASHIFTRT
9846 && (count + num_sign_bit_copies (varop, shift_mode)
9847 >= GET_MODE_PRECISION (shift_mode)))
9848 count = GET_MODE_PRECISION (shift_mode) - 1;
9850 /* We simplify the tests below and elsewhere by converting
9851 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9852 `make_compound_operation' will convert it to an ASHIFTRT for
9853 those machines (such as VAX) that don't have an LSHIFTRT. */
9854 if (code == ASHIFTRT
9855 && val_signbit_known_clear_p (shift_mode,
9856 nonzero_bits (varop, shift_mode)))
9857 code = LSHIFTRT;
9859 if (((code == LSHIFTRT
9860 && HWI_COMPUTABLE_MODE_P (shift_mode)
9861 && !(nonzero_bits (varop, shift_mode) >> count))
9862 || (code == ASHIFT
9863 && HWI_COMPUTABLE_MODE_P (shift_mode)
9864 && !((nonzero_bits (varop, shift_mode) << count)
9865 & GET_MODE_MASK (shift_mode))))
9866 && !side_effects_p (varop))
9867 varop = const0_rtx;
9869 switch (GET_CODE (varop))
9871 case SIGN_EXTEND:
9872 case ZERO_EXTEND:
9873 case SIGN_EXTRACT:
9874 case ZERO_EXTRACT:
9875 new_rtx = expand_compound_operation (varop);
9876 if (new_rtx != varop)
9878 varop = new_rtx;
9879 continue;
9881 break;
9883 case MEM:
9884 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9885 minus the width of a smaller mode, we can do this with a
9886 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9887 if ((code == ASHIFTRT || code == LSHIFTRT)
9888 && ! mode_dependent_address_p (XEXP (varop, 0),
9889 MEM_ADDR_SPACE (varop))
9890 && ! MEM_VOLATILE_P (varop)
9891 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9892 MODE_INT, 1)) != BLKmode)
9894 new_rtx = adjust_address_nv (varop, tmode,
9895 BYTES_BIG_ENDIAN ? 0
9896 : count / BITS_PER_UNIT);
9898 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9899 : ZERO_EXTEND, mode, new_rtx);
9900 count = 0;
9901 continue;
9903 break;
9905 case SUBREG:
9906 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9907 the same number of words as what we've seen so far. Then store
9908 the widest mode in MODE. */
9909 if (subreg_lowpart_p (varop)
9910 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9911 > GET_MODE_SIZE (GET_MODE (varop)))
9912 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9913 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9914 == mode_words
9915 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
9916 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
9918 varop = SUBREG_REG (varop);
9919 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9920 mode = GET_MODE (varop);
9921 continue;
9923 break;
9925 case MULT:
9926 /* Some machines use MULT instead of ASHIFT because MULT
9927 is cheaper. But it is still better on those machines to
9928 merge two shifts into one. */
9929 if (CONST_INT_P (XEXP (varop, 1))
9930 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9932 varop
9933 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9934 XEXP (varop, 0),
9935 GEN_INT (exact_log2 (
9936 UINTVAL (XEXP (varop, 1)))));
9937 continue;
9939 break;
9941 case UDIV:
9942 /* Similar, for when divides are cheaper. */
9943 if (CONST_INT_P (XEXP (varop, 1))
9944 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9946 varop
9947 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9948 XEXP (varop, 0),
9949 GEN_INT (exact_log2 (
9950 UINTVAL (XEXP (varop, 1)))));
9951 continue;
9953 break;
9955 case ASHIFTRT:
9956 /* If we are extracting just the sign bit of an arithmetic
9957 right shift, that shift is not needed. However, the sign
9958 bit of a wider mode may be different from what would be
9959 interpreted as the sign bit in a narrower mode, so, if
9960 the result is narrower, don't discard the shift. */
9961 if (code == LSHIFTRT
9962 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9963 && (GET_MODE_BITSIZE (result_mode)
9964 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9966 varop = XEXP (varop, 0);
9967 continue;
9970 /* ... fall through ... */
9972 case LSHIFTRT:
9973 case ASHIFT:
9974 case ROTATE:
9975 /* Here we have two nested shifts. The result is usually the
9976 AND of a new shift with a mask. We compute the result below. */
9977 if (CONST_INT_P (XEXP (varop, 1))
9978 && INTVAL (XEXP (varop, 1)) >= 0
9979 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
9980 && HWI_COMPUTABLE_MODE_P (result_mode)
9981 && HWI_COMPUTABLE_MODE_P (mode)
9982 && !VECTOR_MODE_P (result_mode))
9984 enum rtx_code first_code = GET_CODE (varop);
9985 unsigned int first_count = INTVAL (XEXP (varop, 1));
9986 unsigned HOST_WIDE_INT mask;
9987 rtx mask_rtx;
9989 /* We have one common special case. We can't do any merging if
9990 the inner code is an ASHIFTRT of a smaller mode. However, if
9991 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9992 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9993 we can convert it to
9994 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
9995 This simplifies certain SIGN_EXTEND operations. */
9996 if (code == ASHIFT && first_code == ASHIFTRT
9997 && count == (GET_MODE_PRECISION (result_mode)
9998 - GET_MODE_PRECISION (GET_MODE (varop))))
10000 /* C3 has the low-order C1 bits zero. */
10002 mask = GET_MODE_MASK (mode)
10003 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10005 varop = simplify_and_const_int (NULL_RTX, result_mode,
10006 XEXP (varop, 0), mask);
10007 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10008 varop, count);
10009 count = first_count;
10010 code = ASHIFTRT;
10011 continue;
10014 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10015 than C1 high-order bits equal to the sign bit, we can convert
10016 this to either an ASHIFT or an ASHIFTRT depending on the
10017 two counts.
10019 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10021 if (code == ASHIFTRT && first_code == ASHIFT
10022 && GET_MODE (varop) == shift_mode
10023 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10024 > first_count))
10026 varop = XEXP (varop, 0);
10027 count -= first_count;
10028 if (count < 0)
10030 count = -count;
10031 code = ASHIFT;
10034 continue;
10037 /* There are some cases we can't do. If CODE is ASHIFTRT,
10038 we can only do this if FIRST_CODE is also ASHIFTRT.
10040 We can't do the case when CODE is ROTATE and FIRST_CODE is
10041 ASHIFTRT.
10043 If the mode of this shift is not the mode of the outer shift,
10044 we can't do this if either shift is a right shift or ROTATE.
10046 Finally, we can't do any of these if the mode is too wide
10047 unless the codes are the same.
10049 Handle the case where the shift codes are the same
10050 first. */
10052 if (code == first_code)
10054 if (GET_MODE (varop) != result_mode
10055 && (code == ASHIFTRT || code == LSHIFTRT
10056 || code == ROTATE))
10057 break;
10059 count += first_count;
10060 varop = XEXP (varop, 0);
10061 continue;
10064 if (code == ASHIFTRT
10065 || (code == ROTATE && first_code == ASHIFTRT)
10066 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10067 || (GET_MODE (varop) != result_mode
10068 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10069 || first_code == ROTATE
10070 || code == ROTATE)))
10071 break;
10073 /* To compute the mask to apply after the shift, shift the
10074 nonzero bits of the inner shift the same way the
10075 outer shift will. */
10077 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10079 mask_rtx
10080 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10081 GEN_INT (count));
10083 /* Give up if we can't compute an outer operation to use. */
10084 if (mask_rtx == 0
10085 || !CONST_INT_P (mask_rtx)
10086 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10087 INTVAL (mask_rtx),
10088 result_mode, &complement_p))
10089 break;
10091 /* If the shifts are in the same direction, we add the
10092 counts. Otherwise, we subtract them. */
10093 if ((code == ASHIFTRT || code == LSHIFTRT)
10094 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10095 count += first_count;
10096 else
10097 count -= first_count;
10099 /* If COUNT is positive, the new shift is usually CODE,
10100 except for the two exceptions below, in which case it is
10101 FIRST_CODE. If the count is negative, FIRST_CODE should
10102 always be used */
10103 if (count > 0
10104 && ((first_code == ROTATE && code == ASHIFT)
10105 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10106 code = first_code;
10107 else if (count < 0)
10108 code = first_code, count = -count;
10110 varop = XEXP (varop, 0);
10111 continue;
10114 /* If we have (A << B << C) for any shift, we can convert this to
10115 (A << C << B). This wins if A is a constant. Only try this if
10116 B is not a constant. */
10118 else if (GET_CODE (varop) == code
10119 && CONST_INT_P (XEXP (varop, 0))
10120 && !CONST_INT_P (XEXP (varop, 1)))
10122 rtx new_rtx = simplify_const_binary_operation (code, mode,
10123 XEXP (varop, 0),
10124 GEN_INT (count));
10125 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10126 count = 0;
10127 continue;
10129 break;
10131 case NOT:
10132 if (VECTOR_MODE_P (mode))
10133 break;
10135 /* Make this fit the case below. */
10136 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10137 continue;
10139 case IOR:
10140 case AND:
10141 case XOR:
10142 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10143 with C the size of VAROP - 1 and the shift is logical if
10144 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10145 we have an (le X 0) operation. If we have an arithmetic shift
10146 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10147 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10149 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10150 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10151 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10152 && (code == LSHIFTRT || code == ASHIFTRT)
10153 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10154 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10156 count = 0;
10157 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10158 const0_rtx);
10160 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10161 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10163 continue;
10166 /* If we have (shift (logical)), move the logical to the outside
10167 to allow it to possibly combine with another logical and the
10168 shift to combine with another shift. This also canonicalizes to
10169 what a ZERO_EXTRACT looks like. Also, some machines have
10170 (and (shift)) insns. */
10172 if (CONST_INT_P (XEXP (varop, 1))
10173 /* We can't do this if we have (ashiftrt (xor)) and the
10174 constant has its sign bit set in shift_mode. */
10175 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10176 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10177 shift_mode))
10178 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10179 XEXP (varop, 1),
10180 GEN_INT (count))) != 0
10181 && CONST_INT_P (new_rtx)
10182 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10183 INTVAL (new_rtx), result_mode, &complement_p))
10185 varop = XEXP (varop, 0);
10186 continue;
10189 /* If we can't do that, try to simplify the shift in each arm of the
10190 logical expression, make a new logical expression, and apply
10191 the inverse distributive law. This also can't be done
10192 for some (ashiftrt (xor)). */
10193 if (CONST_INT_P (XEXP (varop, 1))
10194 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10195 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10196 shift_mode)))
10198 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10199 XEXP (varop, 0), count);
10200 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10201 XEXP (varop, 1), count);
10203 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10204 lhs, rhs);
10205 varop = apply_distributive_law (varop);
10207 count = 0;
10208 continue;
10210 break;
10212 case EQ:
10213 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10214 says that the sign bit can be tested, FOO has mode MODE, C is
10215 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10216 that may be nonzero. */
10217 if (code == LSHIFTRT
10218 && XEXP (varop, 1) == const0_rtx
10219 && GET_MODE (XEXP (varop, 0)) == result_mode
10220 && count == (GET_MODE_PRECISION (result_mode) - 1)
10221 && HWI_COMPUTABLE_MODE_P (result_mode)
10222 && STORE_FLAG_VALUE == -1
10223 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10224 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10225 &complement_p))
10227 varop = XEXP (varop, 0);
10228 count = 0;
10229 continue;
10231 break;
10233 case NEG:
10234 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10235 than the number of bits in the mode is equivalent to A. */
10236 if (code == LSHIFTRT
10237 && count == (GET_MODE_PRECISION (result_mode) - 1)
10238 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10240 varop = XEXP (varop, 0);
10241 count = 0;
10242 continue;
10245 /* NEG commutes with ASHIFT since it is multiplication. Move the
10246 NEG outside to allow shifts to combine. */
10247 if (code == ASHIFT
10248 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10249 &complement_p))
10251 varop = XEXP (varop, 0);
10252 continue;
10254 break;
10256 case PLUS:
10257 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10258 is one less than the number of bits in the mode is
10259 equivalent to (xor A 1). */
10260 if (code == LSHIFTRT
10261 && count == (GET_MODE_PRECISION (result_mode) - 1)
10262 && XEXP (varop, 1) == constm1_rtx
10263 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10264 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10265 &complement_p))
10267 count = 0;
10268 varop = XEXP (varop, 0);
10269 continue;
10272 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10273 that might be nonzero in BAR are those being shifted out and those
10274 bits are known zero in FOO, we can replace the PLUS with FOO.
10275 Similarly in the other operand order. This code occurs when
10276 we are computing the size of a variable-size array. */
10278 if ((code == ASHIFTRT || code == LSHIFTRT)
10279 && count < HOST_BITS_PER_WIDE_INT
10280 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10281 && (nonzero_bits (XEXP (varop, 1), result_mode)
10282 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10284 varop = XEXP (varop, 0);
10285 continue;
10287 else if ((code == ASHIFTRT || code == LSHIFTRT)
10288 && count < HOST_BITS_PER_WIDE_INT
10289 && HWI_COMPUTABLE_MODE_P (result_mode)
10290 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10291 >> count)
10292 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10293 & nonzero_bits (XEXP (varop, 1),
10294 result_mode)))
10296 varop = XEXP (varop, 1);
10297 continue;
10300 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10301 if (code == ASHIFT
10302 && CONST_INT_P (XEXP (varop, 1))
10303 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10304 XEXP (varop, 1),
10305 GEN_INT (count))) != 0
10306 && CONST_INT_P (new_rtx)
10307 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10308 INTVAL (new_rtx), result_mode, &complement_p))
10310 varop = XEXP (varop, 0);
10311 continue;
10314 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10315 signbit', and attempt to change the PLUS to an XOR and move it to
10316 the outer operation as is done above in the AND/IOR/XOR case
10317 leg for shift(logical). See details in logical handling above
10318 for reasoning in doing so. */
10319 if (code == LSHIFTRT
10320 && CONST_INT_P (XEXP (varop, 1))
10321 && mode_signbit_p (result_mode, XEXP (varop, 1))
10322 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10323 XEXP (varop, 1),
10324 GEN_INT (count))) != 0
10325 && CONST_INT_P (new_rtx)
10326 && merge_outer_ops (&outer_op, &outer_const, XOR,
10327 INTVAL (new_rtx), result_mode, &complement_p))
10329 varop = XEXP (varop, 0);
10330 continue;
10333 break;
10335 case MINUS:
10336 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10337 with C the size of VAROP - 1 and the shift is logical if
10338 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10339 we have a (gt X 0) operation. If the shift is arithmetic with
10340 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10341 we have a (neg (gt X 0)) operation. */
10343 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10344 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10345 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10346 && (code == LSHIFTRT || code == ASHIFTRT)
10347 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10348 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10349 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10351 count = 0;
10352 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10353 const0_rtx);
10355 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10356 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10358 continue;
10360 break;
10362 case TRUNCATE:
10363 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10364 if the truncate does not affect the value. */
10365 if (code == LSHIFTRT
10366 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10367 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10368 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10369 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10370 - GET_MODE_PRECISION (GET_MODE (varop)))))
10372 rtx varop_inner = XEXP (varop, 0);
10374 varop_inner
10375 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10376 XEXP (varop_inner, 0),
10377 GEN_INT
10378 (count + INTVAL (XEXP (varop_inner, 1))));
10379 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10380 count = 0;
10381 continue;
10383 break;
10385 default:
10386 break;
10389 break;
10392 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10393 outer_op, outer_const);
10395 /* We have now finished analyzing the shift. The result should be
10396 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10397 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10398 to the result of the shift. OUTER_CONST is the relevant constant,
10399 but we must turn off all bits turned off in the shift. */
10401 if (outer_op == UNKNOWN
10402 && orig_code == code && orig_count == count
10403 && varop == orig_varop
10404 && shift_mode == GET_MODE (varop))
10405 return NULL_RTX;
10407 /* Make a SUBREG if necessary. If we can't make it, fail. */
10408 varop = gen_lowpart (shift_mode, varop);
10409 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10410 return NULL_RTX;
10412 /* If we have an outer operation and we just made a shift, it is
10413 possible that we could have simplified the shift were it not
10414 for the outer operation. So try to do the simplification
10415 recursively. */
10417 if (outer_op != UNKNOWN)
10418 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10419 else
10420 x = NULL_RTX;
10422 if (x == NULL_RTX)
10423 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10425 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10426 turn off all the bits that the shift would have turned off. */
10427 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10428 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10429 GET_MODE_MASK (result_mode) >> orig_count);
10431 /* Do the remainder of the processing in RESULT_MODE. */
10432 x = gen_lowpart_or_truncate (result_mode, x);
10434 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10435 operation. */
10436 if (complement_p)
10437 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10439 if (outer_op != UNKNOWN)
10441 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10442 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10443 outer_const = trunc_int_for_mode (outer_const, result_mode);
10445 if (outer_op == AND)
10446 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10447 else if (outer_op == SET)
10449 /* This means that we have determined that the result is
10450 equivalent to a constant. This should be rare. */
10451 if (!side_effects_p (x))
10452 x = GEN_INT (outer_const);
10454 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10455 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10456 else
10457 x = simplify_gen_binary (outer_op, result_mode, x,
10458 GEN_INT (outer_const));
10461 return x;
10464 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10465 The result of the shift is RESULT_MODE. If we cannot simplify it,
10466 return X or, if it is NULL, synthesize the expression with
10467 simplify_gen_binary. Otherwise, return a simplified value.
10469 The shift is normally computed in the widest mode we find in VAROP, as
10470 long as it isn't a different number of words than RESULT_MODE. Exceptions
10471 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10473 static rtx
10474 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10475 rtx varop, int count)
10477 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10478 if (tem)
10479 return tem;
10481 if (!x)
10482 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10483 if (GET_MODE (x) != result_mode)
10484 x = gen_lowpart (result_mode, x);
10485 return x;
10489 /* Like recog, but we receive the address of a pointer to a new pattern.
10490 We try to match the rtx that the pointer points to.
10491 If that fails, we may try to modify or replace the pattern,
10492 storing the replacement into the same pointer object.
10494 Modifications include deletion or addition of CLOBBERs.
10496 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10497 the CLOBBERs are placed.
10499 The value is the final insn code from the pattern ultimately matched,
10500 or -1. */
10502 static int
10503 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10505 rtx pat = *pnewpat;
10506 rtx pat_without_clobbers;
10507 int insn_code_number;
10508 int num_clobbers_to_add = 0;
10509 int i;
10510 rtx notes = NULL_RTX;
10511 rtx old_notes, old_pat;
10512 int old_icode;
10514 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10515 we use to indicate that something didn't match. If we find such a
10516 thing, force rejection. */
10517 if (GET_CODE (pat) == PARALLEL)
10518 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10519 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10520 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10521 return -1;
10523 old_pat = PATTERN (insn);
10524 old_notes = REG_NOTES (insn);
10525 PATTERN (insn) = pat;
10526 REG_NOTES (insn) = NULL_RTX;
10528 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10529 if (dump_file && (dump_flags & TDF_DETAILS))
10531 if (insn_code_number < 0)
10532 fputs ("Failed to match this instruction:\n", dump_file);
10533 else
10534 fputs ("Successfully matched this instruction:\n", dump_file);
10535 print_rtl_single (dump_file, pat);
10538 /* If it isn't, there is the possibility that we previously had an insn
10539 that clobbered some register as a side effect, but the combined
10540 insn doesn't need to do that. So try once more without the clobbers
10541 unless this represents an ASM insn. */
10543 if (insn_code_number < 0 && ! check_asm_operands (pat)
10544 && GET_CODE (pat) == PARALLEL)
10546 int pos;
10548 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10549 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10551 if (i != pos)
10552 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10553 pos++;
10556 SUBST_INT (XVECLEN (pat, 0), pos);
10558 if (pos == 1)
10559 pat = XVECEXP (pat, 0, 0);
10561 PATTERN (insn) = pat;
10562 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10563 if (dump_file && (dump_flags & TDF_DETAILS))
10565 if (insn_code_number < 0)
10566 fputs ("Failed to match this instruction:\n", dump_file);
10567 else
10568 fputs ("Successfully matched this instruction:\n", dump_file);
10569 print_rtl_single (dump_file, pat);
10573 pat_without_clobbers = pat;
10575 PATTERN (insn) = old_pat;
10576 REG_NOTES (insn) = old_notes;
10578 /* Recognize all noop sets, these will be killed by followup pass. */
10579 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10580 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10582 /* If we had any clobbers to add, make a new pattern than contains
10583 them. Then check to make sure that all of them are dead. */
10584 if (num_clobbers_to_add)
10586 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10587 rtvec_alloc (GET_CODE (pat) == PARALLEL
10588 ? (XVECLEN (pat, 0)
10589 + num_clobbers_to_add)
10590 : num_clobbers_to_add + 1));
10592 if (GET_CODE (pat) == PARALLEL)
10593 for (i = 0; i < XVECLEN (pat, 0); i++)
10594 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10595 else
10596 XVECEXP (newpat, 0, 0) = pat;
10598 add_clobbers (newpat, insn_code_number);
10600 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10601 i < XVECLEN (newpat, 0); i++)
10603 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10604 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10605 return -1;
10606 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10608 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10609 notes = alloc_reg_note (REG_UNUSED,
10610 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10613 pat = newpat;
10616 if (insn_code_number >= 0
10617 && insn_code_number != NOOP_MOVE_INSN_CODE)
10619 old_pat = PATTERN (insn);
10620 old_notes = REG_NOTES (insn);
10621 old_icode = INSN_CODE (insn);
10622 PATTERN (insn) = pat;
10623 REG_NOTES (insn) = notes;
10625 /* Allow targets to reject combined insn. */
10626 if (!targetm.legitimate_combined_insn (insn))
10628 if (dump_file && (dump_flags & TDF_DETAILS))
10629 fputs ("Instruction not appropriate for target.",
10630 dump_file);
10632 /* Callers expect recog_for_combine to strip
10633 clobbers from the pattern on failure. */
10634 pat = pat_without_clobbers;
10635 notes = NULL_RTX;
10637 insn_code_number = -1;
10640 PATTERN (insn) = old_pat;
10641 REG_NOTES (insn) = old_notes;
10642 INSN_CODE (insn) = old_icode;
10645 *pnewpat = pat;
10646 *pnotes = notes;
10648 return insn_code_number;
10651 /* Like gen_lowpart_general but for use by combine. In combine it
10652 is not possible to create any new pseudoregs. However, it is
10653 safe to create invalid memory addresses, because combine will
10654 try to recognize them and all they will do is make the combine
10655 attempt fail.
10657 If for some reason this cannot do its job, an rtx
10658 (clobber (const_int 0)) is returned.
10659 An insn containing that will not be recognized. */
10661 static rtx
10662 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10664 enum machine_mode imode = GET_MODE (x);
10665 unsigned int osize = GET_MODE_SIZE (omode);
10666 unsigned int isize = GET_MODE_SIZE (imode);
10667 rtx result;
10669 if (omode == imode)
10670 return x;
10672 /* We can only support MODE being wider than a word if X is a
10673 constant integer or has a mode the same size. */
10674 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10675 && ! ((CONST_INT_P (x) || CONST_DOUBLE_AS_INT_P (x))
10676 || isize == osize))
10677 goto fail;
10679 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10680 won't know what to do. So we will strip off the SUBREG here and
10681 process normally. */
10682 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10684 x = SUBREG_REG (x);
10686 /* For use in case we fall down into the address adjustments
10687 further below, we need to adjust the known mode and size of
10688 x; imode and isize, since we just adjusted x. */
10689 imode = GET_MODE (x);
10691 if (imode == omode)
10692 return x;
10694 isize = GET_MODE_SIZE (imode);
10697 result = gen_lowpart_common (omode, x);
10699 if (result)
10700 return result;
10702 if (MEM_P (x))
10704 int offset = 0;
10706 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10707 address. */
10708 if (MEM_VOLATILE_P (x)
10709 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10710 goto fail;
10712 /* If we want to refer to something bigger than the original memref,
10713 generate a paradoxical subreg instead. That will force a reload
10714 of the original memref X. */
10715 if (isize < osize)
10716 return gen_rtx_SUBREG (omode, x, 0);
10718 if (WORDS_BIG_ENDIAN)
10719 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10721 /* Adjust the address so that the address-after-the-data is
10722 unchanged. */
10723 if (BYTES_BIG_ENDIAN)
10724 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10726 return adjust_address_nv (x, omode, offset);
10729 /* If X is a comparison operator, rewrite it in a new mode. This
10730 probably won't match, but may allow further simplifications. */
10731 else if (COMPARISON_P (x))
10732 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10734 /* If we couldn't simplify X any other way, just enclose it in a
10735 SUBREG. Normally, this SUBREG won't match, but some patterns may
10736 include an explicit SUBREG or we may simplify it further in combine. */
10737 else
10739 int offset = 0;
10740 rtx res;
10742 offset = subreg_lowpart_offset (omode, imode);
10743 if (imode == VOIDmode)
10745 imode = int_mode_for_mode (omode);
10746 x = gen_lowpart_common (imode, x);
10747 if (x == NULL)
10748 goto fail;
10750 res = simplify_gen_subreg (omode, x, imode, offset);
10751 if (res)
10752 return res;
10755 fail:
10756 return gen_rtx_CLOBBER (omode, const0_rtx);
10759 /* Try to simplify a comparison between OP0 and a constant OP1,
10760 where CODE is the comparison code that will be tested, into a
10761 (CODE OP0 const0_rtx) form.
10763 The result is a possibly different comparison code to use.
10764 *POP1 may be updated. */
10766 static enum rtx_code
10767 simplify_compare_const (enum rtx_code code, rtx op0, rtx *pop1)
10769 enum machine_mode mode = GET_MODE (op0);
10770 unsigned int mode_width = GET_MODE_PRECISION (mode);
10771 HOST_WIDE_INT const_op = INTVAL (*pop1);
10773 /* Get the constant we are comparing against and turn off all bits
10774 not on in our mode. */
10775 if (mode != VOIDmode)
10776 const_op = trunc_int_for_mode (const_op, mode);
10778 /* If we are comparing against a constant power of two and the value
10779 being compared can only have that single bit nonzero (e.g., it was
10780 `and'ed with that bit), we can replace this with a comparison
10781 with zero. */
10782 if (const_op
10783 && (code == EQ || code == NE || code == GE || code == GEU
10784 || code == LT || code == LTU)
10785 && mode_width <= HOST_BITS_PER_WIDE_INT
10786 && exact_log2 (const_op) >= 0
10787 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10789 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10790 const_op = 0;
10793 /* Similarly, if we are comparing a value known to be either -1 or
10794 0 with -1, change it to the opposite comparison against zero. */
10795 if (const_op == -1
10796 && (code == EQ || code == NE || code == GT || code == LE
10797 || code == GEU || code == LTU)
10798 && num_sign_bit_copies (op0, mode) == mode_width)
10800 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10801 const_op = 0;
10804 /* Do some canonicalizations based on the comparison code. We prefer
10805 comparisons against zero and then prefer equality comparisons.
10806 If we can reduce the size of a constant, we will do that too. */
10807 switch (code)
10809 case LT:
10810 /* < C is equivalent to <= (C - 1) */
10811 if (const_op > 0)
10813 const_op -= 1;
10814 code = LE;
10815 /* ... fall through to LE case below. */
10817 else
10818 break;
10820 case LE:
10821 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10822 if (const_op < 0)
10824 const_op += 1;
10825 code = LT;
10828 /* If we are doing a <= 0 comparison on a value known to have
10829 a zero sign bit, we can replace this with == 0. */
10830 else if (const_op == 0
10831 && mode_width <= HOST_BITS_PER_WIDE_INT
10832 && (nonzero_bits (op0, mode)
10833 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10834 == 0)
10835 code = EQ;
10836 break;
10838 case GE:
10839 /* >= C is equivalent to > (C - 1). */
10840 if (const_op > 0)
10842 const_op -= 1;
10843 code = GT;
10844 /* ... fall through to GT below. */
10846 else
10847 break;
10849 case GT:
10850 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10851 if (const_op < 0)
10853 const_op += 1;
10854 code = GE;
10857 /* If we are doing a > 0 comparison on a value known to have
10858 a zero sign bit, we can replace this with != 0. */
10859 else if (const_op == 0
10860 && mode_width <= HOST_BITS_PER_WIDE_INT
10861 && (nonzero_bits (op0, mode)
10862 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10863 == 0)
10864 code = NE;
10865 break;
10867 case LTU:
10868 /* < C is equivalent to <= (C - 1). */
10869 if (const_op > 0)
10871 const_op -= 1;
10872 code = LEU;
10873 /* ... fall through ... */
10875 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10876 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10877 && (unsigned HOST_WIDE_INT) const_op
10878 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10880 const_op = 0;
10881 code = GE;
10882 break;
10884 else
10885 break;
10887 case LEU:
10888 /* unsigned <= 0 is equivalent to == 0 */
10889 if (const_op == 0)
10890 code = EQ;
10891 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10892 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10893 && (unsigned HOST_WIDE_INT) const_op
10894 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10896 const_op = 0;
10897 code = GE;
10899 break;
10901 case GEU:
10902 /* >= C is equivalent to > (C - 1). */
10903 if (const_op > 1)
10905 const_op -= 1;
10906 code = GTU;
10907 /* ... fall through ... */
10910 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10911 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10912 && (unsigned HOST_WIDE_INT) const_op
10913 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10915 const_op = 0;
10916 code = LT;
10917 break;
10919 else
10920 break;
10922 case GTU:
10923 /* unsigned > 0 is equivalent to != 0 */
10924 if (const_op == 0)
10925 code = NE;
10926 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10927 else if (mode_width <= HOST_BITS_PER_WIDE_INT
10928 && (unsigned HOST_WIDE_INT) const_op
10929 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10931 const_op = 0;
10932 code = LT;
10934 break;
10936 default:
10937 break;
10940 *pop1 = GEN_INT (const_op);
10941 return code;
10944 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10945 comparison code that will be tested.
10947 The result is a possibly different comparison code to use. *POP0 and
10948 *POP1 may be updated.
10950 It is possible that we might detect that a comparison is either always
10951 true or always false. However, we do not perform general constant
10952 folding in combine, so this knowledge isn't useful. Such tautologies
10953 should have been detected earlier. Hence we ignore all such cases. */
10955 static enum rtx_code
10956 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10958 rtx op0 = *pop0;
10959 rtx op1 = *pop1;
10960 rtx tem, tem1;
10961 int i;
10962 enum machine_mode mode, tmode;
10964 /* Try a few ways of applying the same transformation to both operands. */
10965 while (1)
10967 #ifndef WORD_REGISTER_OPERATIONS
10968 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10969 so check specially. */
10970 if (code != GTU && code != GEU && code != LTU && code != LEU
10971 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10972 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10973 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10974 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10975 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10976 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10977 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10978 && CONST_INT_P (XEXP (op0, 1))
10979 && XEXP (op0, 1) == XEXP (op1, 1)
10980 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10981 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10982 && (INTVAL (XEXP (op0, 1))
10983 == (GET_MODE_PRECISION (GET_MODE (op0))
10984 - (GET_MODE_PRECISION
10985 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10987 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10988 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10990 #endif
10992 /* If both operands are the same constant shift, see if we can ignore the
10993 shift. We can if the shift is a rotate or if the bits shifted out of
10994 this shift are known to be zero for both inputs and if the type of
10995 comparison is compatible with the shift. */
10996 if (GET_CODE (op0) == GET_CODE (op1)
10997 && HWI_COMPUTABLE_MODE_P (GET_MODE(op0))
10998 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10999 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11000 && (code != GT && code != LT && code != GE && code != LE))
11001 || (GET_CODE (op0) == ASHIFTRT
11002 && (code != GTU && code != LTU
11003 && code != GEU && code != LEU)))
11004 && CONST_INT_P (XEXP (op0, 1))
11005 && INTVAL (XEXP (op0, 1)) >= 0
11006 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11007 && XEXP (op0, 1) == XEXP (op1, 1))
11009 enum machine_mode mode = GET_MODE (op0);
11010 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11011 int shift_count = INTVAL (XEXP (op0, 1));
11013 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11014 mask &= (mask >> shift_count) << shift_count;
11015 else if (GET_CODE (op0) == ASHIFT)
11016 mask = (mask & (mask << shift_count)) >> shift_count;
11018 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11019 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11020 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11021 else
11022 break;
11025 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11026 SUBREGs are of the same mode, and, in both cases, the AND would
11027 be redundant if the comparison was done in the narrower mode,
11028 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11029 and the operand's possibly nonzero bits are 0xffffff01; in that case
11030 if we only care about QImode, we don't need the AND). This case
11031 occurs if the output mode of an scc insn is not SImode and
11032 STORE_FLAG_VALUE == 1 (e.g., the 386).
11034 Similarly, check for a case where the AND's are ZERO_EXTEND
11035 operations from some narrower mode even though a SUBREG is not
11036 present. */
11038 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11039 && CONST_INT_P (XEXP (op0, 1))
11040 && CONST_INT_P (XEXP (op1, 1)))
11042 rtx inner_op0 = XEXP (op0, 0);
11043 rtx inner_op1 = XEXP (op1, 0);
11044 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11045 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11046 int changed = 0;
11048 if (paradoxical_subreg_p (inner_op0)
11049 && GET_CODE (inner_op1) == SUBREG
11050 && (GET_MODE (SUBREG_REG (inner_op0))
11051 == GET_MODE (SUBREG_REG (inner_op1)))
11052 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11053 <= HOST_BITS_PER_WIDE_INT)
11054 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11055 GET_MODE (SUBREG_REG (inner_op0)))))
11056 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11057 GET_MODE (SUBREG_REG (inner_op1))))))
11059 op0 = SUBREG_REG (inner_op0);
11060 op1 = SUBREG_REG (inner_op1);
11062 /* The resulting comparison is always unsigned since we masked
11063 off the original sign bit. */
11064 code = unsigned_condition (code);
11066 changed = 1;
11069 else if (c0 == c1)
11070 for (tmode = GET_CLASS_NARROWEST_MODE
11071 (GET_MODE_CLASS (GET_MODE (op0)));
11072 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11073 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11075 op0 = gen_lowpart (tmode, inner_op0);
11076 op1 = gen_lowpart (tmode, inner_op1);
11077 code = unsigned_condition (code);
11078 changed = 1;
11079 break;
11082 if (! changed)
11083 break;
11086 /* If both operands are NOT, we can strip off the outer operation
11087 and adjust the comparison code for swapped operands; similarly for
11088 NEG, except that this must be an equality comparison. */
11089 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11090 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11091 && (code == EQ || code == NE)))
11092 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11094 else
11095 break;
11098 /* If the first operand is a constant, swap the operands and adjust the
11099 comparison code appropriately, but don't do this if the second operand
11100 is already a constant integer. */
11101 if (swap_commutative_operands_p (op0, op1))
11103 tem = op0, op0 = op1, op1 = tem;
11104 code = swap_condition (code);
11107 /* We now enter a loop during which we will try to simplify the comparison.
11108 For the most part, we only are concerned with comparisons with zero,
11109 but some things may really be comparisons with zero but not start
11110 out looking that way. */
11112 while (CONST_INT_P (op1))
11114 enum machine_mode mode = GET_MODE (op0);
11115 unsigned int mode_width = GET_MODE_PRECISION (mode);
11116 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11117 int equality_comparison_p;
11118 int sign_bit_comparison_p;
11119 int unsigned_comparison_p;
11120 HOST_WIDE_INT const_op;
11122 /* We only want to handle integral modes. This catches VOIDmode,
11123 CCmode, and the floating-point modes. An exception is that we
11124 can handle VOIDmode if OP0 is a COMPARE or a comparison
11125 operation. */
11127 if (GET_MODE_CLASS (mode) != MODE_INT
11128 && ! (mode == VOIDmode
11129 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11130 break;
11132 /* Try to simplify the compare to constant, possibly changing the
11133 comparison op, and/or changing op1 to zero. */
11134 code = simplify_compare_const (code, op0, &op1);
11135 const_op = INTVAL (op1);
11137 /* Compute some predicates to simplify code below. */
11139 equality_comparison_p = (code == EQ || code == NE);
11140 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11141 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11142 || code == GEU);
11144 /* If this is a sign bit comparison and we can do arithmetic in
11145 MODE, say that we will only be needing the sign bit of OP0. */
11146 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11147 op0 = force_to_mode (op0, mode,
11148 (unsigned HOST_WIDE_INT) 1
11149 << (GET_MODE_PRECISION (mode) - 1),
11152 /* Now try cases based on the opcode of OP0. If none of the cases
11153 does a "continue", we exit this loop immediately after the
11154 switch. */
11156 switch (GET_CODE (op0))
11158 case ZERO_EXTRACT:
11159 /* If we are extracting a single bit from a variable position in
11160 a constant that has only a single bit set and are comparing it
11161 with zero, we can convert this into an equality comparison
11162 between the position and the location of the single bit. */
11163 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11164 have already reduced the shift count modulo the word size. */
11165 if (!SHIFT_COUNT_TRUNCATED
11166 && CONST_INT_P (XEXP (op0, 0))
11167 && XEXP (op0, 1) == const1_rtx
11168 && equality_comparison_p && const_op == 0
11169 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11171 if (BITS_BIG_ENDIAN)
11173 enum machine_mode new_mode
11174 = mode_for_extraction (EP_extzv, 1);
11175 if (new_mode == MAX_MACHINE_MODE)
11176 i = BITS_PER_WORD - 1 - i;
11177 else
11179 mode = new_mode;
11180 i = (GET_MODE_PRECISION (mode) - 1 - i);
11184 op0 = XEXP (op0, 2);
11185 op1 = GEN_INT (i);
11186 const_op = i;
11188 /* Result is nonzero iff shift count is equal to I. */
11189 code = reverse_condition (code);
11190 continue;
11193 /* ... fall through ... */
11195 case SIGN_EXTRACT:
11196 tem = expand_compound_operation (op0);
11197 if (tem != op0)
11199 op0 = tem;
11200 continue;
11202 break;
11204 case NOT:
11205 /* If testing for equality, we can take the NOT of the constant. */
11206 if (equality_comparison_p
11207 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11209 op0 = XEXP (op0, 0);
11210 op1 = tem;
11211 continue;
11214 /* If just looking at the sign bit, reverse the sense of the
11215 comparison. */
11216 if (sign_bit_comparison_p)
11218 op0 = XEXP (op0, 0);
11219 code = (code == GE ? LT : GE);
11220 continue;
11222 break;
11224 case NEG:
11225 /* If testing for equality, we can take the NEG of the constant. */
11226 if (equality_comparison_p
11227 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11229 op0 = XEXP (op0, 0);
11230 op1 = tem;
11231 continue;
11234 /* The remaining cases only apply to comparisons with zero. */
11235 if (const_op != 0)
11236 break;
11238 /* When X is ABS or is known positive,
11239 (neg X) is < 0 if and only if X != 0. */
11241 if (sign_bit_comparison_p
11242 && (GET_CODE (XEXP (op0, 0)) == ABS
11243 || (mode_width <= HOST_BITS_PER_WIDE_INT
11244 && (nonzero_bits (XEXP (op0, 0), mode)
11245 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11246 == 0)))
11248 op0 = XEXP (op0, 0);
11249 code = (code == LT ? NE : EQ);
11250 continue;
11253 /* If we have NEG of something whose two high-order bits are the
11254 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11255 if (num_sign_bit_copies (op0, mode) >= 2)
11257 op0 = XEXP (op0, 0);
11258 code = swap_condition (code);
11259 continue;
11261 break;
11263 case ROTATE:
11264 /* If we are testing equality and our count is a constant, we
11265 can perform the inverse operation on our RHS. */
11266 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11267 && (tem = simplify_binary_operation (ROTATERT, mode,
11268 op1, XEXP (op0, 1))) != 0)
11270 op0 = XEXP (op0, 0);
11271 op1 = tem;
11272 continue;
11275 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11276 a particular bit. Convert it to an AND of a constant of that
11277 bit. This will be converted into a ZERO_EXTRACT. */
11278 if (const_op == 0 && sign_bit_comparison_p
11279 && CONST_INT_P (XEXP (op0, 1))
11280 && mode_width <= HOST_BITS_PER_WIDE_INT)
11282 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11283 ((unsigned HOST_WIDE_INT) 1
11284 << (mode_width - 1
11285 - INTVAL (XEXP (op0, 1)))));
11286 code = (code == LT ? NE : EQ);
11287 continue;
11290 /* Fall through. */
11292 case ABS:
11293 /* ABS is ignorable inside an equality comparison with zero. */
11294 if (const_op == 0 && equality_comparison_p)
11296 op0 = XEXP (op0, 0);
11297 continue;
11299 break;
11301 case SIGN_EXTEND:
11302 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11303 (compare FOO CONST) if CONST fits in FOO's mode and we
11304 are either testing inequality or have an unsigned
11305 comparison with ZERO_EXTEND or a signed comparison with
11306 SIGN_EXTEND. But don't do it if we don't have a compare
11307 insn of the given mode, since we'd have to revert it
11308 later on, and then we wouldn't know whether to sign- or
11309 zero-extend. */
11310 mode = GET_MODE (XEXP (op0, 0));
11311 if (GET_MODE_CLASS (mode) == MODE_INT
11312 && ! unsigned_comparison_p
11313 && HWI_COMPUTABLE_MODE_P (mode)
11314 && trunc_int_for_mode (const_op, mode) == const_op
11315 && have_insn_for (COMPARE, mode))
11317 op0 = XEXP (op0, 0);
11318 continue;
11320 break;
11322 case SUBREG:
11323 /* Check for the case where we are comparing A - C1 with C2, that is
11325 (subreg:MODE (plus (A) (-C1))) op (C2)
11327 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11328 comparison in the wider mode. One of the following two conditions
11329 must be true in order for this to be valid:
11331 1. The mode extension results in the same bit pattern being added
11332 on both sides and the comparison is equality or unsigned. As
11333 C2 has been truncated to fit in MODE, the pattern can only be
11334 all 0s or all 1s.
11336 2. The mode extension results in the sign bit being copied on
11337 each side.
11339 The difficulty here is that we have predicates for A but not for
11340 (A - C1) so we need to check that C1 is within proper bounds so
11341 as to perturbate A as little as possible. */
11343 if (mode_width <= HOST_BITS_PER_WIDE_INT
11344 && subreg_lowpart_p (op0)
11345 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11346 && GET_CODE (SUBREG_REG (op0)) == PLUS
11347 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11349 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11350 rtx a = XEXP (SUBREG_REG (op0), 0);
11351 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11353 if ((c1 > 0
11354 && (unsigned HOST_WIDE_INT) c1
11355 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11356 && (equality_comparison_p || unsigned_comparison_p)
11357 /* (A - C1) zero-extends if it is positive and sign-extends
11358 if it is negative, C2 both zero- and sign-extends. */
11359 && ((0 == (nonzero_bits (a, inner_mode)
11360 & ~GET_MODE_MASK (mode))
11361 && const_op >= 0)
11362 /* (A - C1) sign-extends if it is positive and 1-extends
11363 if it is negative, C2 both sign- and 1-extends. */
11364 || (num_sign_bit_copies (a, inner_mode)
11365 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11366 - mode_width)
11367 && const_op < 0)))
11368 || ((unsigned HOST_WIDE_INT) c1
11369 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11370 /* (A - C1) always sign-extends, like C2. */
11371 && num_sign_bit_copies (a, inner_mode)
11372 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11373 - (mode_width - 1))))
11375 op0 = SUBREG_REG (op0);
11376 continue;
11380 /* If the inner mode is narrower and we are extracting the low part,
11381 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11382 if (subreg_lowpart_p (op0)
11383 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11384 /* Fall through */ ;
11385 else
11386 break;
11388 /* ... fall through ... */
11390 case ZERO_EXTEND:
11391 mode = GET_MODE (XEXP (op0, 0));
11392 if (GET_MODE_CLASS (mode) == MODE_INT
11393 && (unsigned_comparison_p || equality_comparison_p)
11394 && HWI_COMPUTABLE_MODE_P (mode)
11395 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11396 && const_op >= 0
11397 && have_insn_for (COMPARE, mode))
11399 op0 = XEXP (op0, 0);
11400 continue;
11402 break;
11404 case PLUS:
11405 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11406 this for equality comparisons due to pathological cases involving
11407 overflows. */
11408 if (equality_comparison_p
11409 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11410 op1, XEXP (op0, 1))))
11412 op0 = XEXP (op0, 0);
11413 op1 = tem;
11414 continue;
11417 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11418 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11419 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11421 op0 = XEXP (XEXP (op0, 0), 0);
11422 code = (code == LT ? EQ : NE);
11423 continue;
11425 break;
11427 case MINUS:
11428 /* We used to optimize signed comparisons against zero, but that
11429 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11430 arrive here as equality comparisons, or (GEU, LTU) are
11431 optimized away. No need to special-case them. */
11433 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11434 (eq B (minus A C)), whichever simplifies. We can only do
11435 this for equality comparisons due to pathological cases involving
11436 overflows. */
11437 if (equality_comparison_p
11438 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11439 XEXP (op0, 1), op1)))
11441 op0 = XEXP (op0, 0);
11442 op1 = tem;
11443 continue;
11446 if (equality_comparison_p
11447 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11448 XEXP (op0, 0), op1)))
11450 op0 = XEXP (op0, 1);
11451 op1 = tem;
11452 continue;
11455 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11456 of bits in X minus 1, is one iff X > 0. */
11457 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11458 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11459 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11460 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11462 op0 = XEXP (op0, 1);
11463 code = (code == GE ? LE : GT);
11464 continue;
11466 break;
11468 case XOR:
11469 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11470 if C is zero or B is a constant. */
11471 if (equality_comparison_p
11472 && 0 != (tem = simplify_binary_operation (XOR, mode,
11473 XEXP (op0, 1), op1)))
11475 op0 = XEXP (op0, 0);
11476 op1 = tem;
11477 continue;
11479 break;
11481 case EQ: case NE:
11482 case UNEQ: case LTGT:
11483 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11484 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11485 case UNORDERED: case ORDERED:
11486 /* We can't do anything if OP0 is a condition code value, rather
11487 than an actual data value. */
11488 if (const_op != 0
11489 || CC0_P (XEXP (op0, 0))
11490 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11491 break;
11493 /* Get the two operands being compared. */
11494 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11495 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11496 else
11497 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11499 /* Check for the cases where we simply want the result of the
11500 earlier test or the opposite of that result. */
11501 if (code == NE || code == EQ
11502 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11503 && (code == LT || code == GE)))
11505 enum rtx_code new_code;
11506 if (code == LT || code == NE)
11507 new_code = GET_CODE (op0);
11508 else
11509 new_code = reversed_comparison_code (op0, NULL);
11511 if (new_code != UNKNOWN)
11513 code = new_code;
11514 op0 = tem;
11515 op1 = tem1;
11516 continue;
11519 break;
11521 case IOR:
11522 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11523 iff X <= 0. */
11524 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11525 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11526 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11528 op0 = XEXP (op0, 1);
11529 code = (code == GE ? GT : LE);
11530 continue;
11532 break;
11534 case AND:
11535 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11536 will be converted to a ZERO_EXTRACT later. */
11537 if (const_op == 0 && equality_comparison_p
11538 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11539 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11541 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11542 XEXP (XEXP (op0, 0), 1));
11543 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11544 continue;
11547 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11548 zero and X is a comparison and C1 and C2 describe only bits set
11549 in STORE_FLAG_VALUE, we can compare with X. */
11550 if (const_op == 0 && equality_comparison_p
11551 && mode_width <= HOST_BITS_PER_WIDE_INT
11552 && CONST_INT_P (XEXP (op0, 1))
11553 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11554 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11555 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11556 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11558 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11559 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11560 if ((~STORE_FLAG_VALUE & mask) == 0
11561 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11562 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11563 && COMPARISON_P (tem))))
11565 op0 = XEXP (XEXP (op0, 0), 0);
11566 continue;
11570 /* If we are doing an equality comparison of an AND of a bit equal
11571 to the sign bit, replace this with a LT or GE comparison of
11572 the underlying value. */
11573 if (equality_comparison_p
11574 && const_op == 0
11575 && CONST_INT_P (XEXP (op0, 1))
11576 && mode_width <= HOST_BITS_PER_WIDE_INT
11577 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11578 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11580 op0 = XEXP (op0, 0);
11581 code = (code == EQ ? GE : LT);
11582 continue;
11585 /* If this AND operation is really a ZERO_EXTEND from a narrower
11586 mode, the constant fits within that mode, and this is either an
11587 equality or unsigned comparison, try to do this comparison in
11588 the narrower mode.
11590 Note that in:
11592 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11593 -> (ne:DI (reg:SI 4) (const_int 0))
11595 unless TRULY_NOOP_TRUNCATION allows it or the register is
11596 known to hold a value of the required mode the
11597 transformation is invalid. */
11598 if ((equality_comparison_p || unsigned_comparison_p)
11599 && CONST_INT_P (XEXP (op0, 1))
11600 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11601 & GET_MODE_MASK (mode))
11602 + 1)) >= 0
11603 && const_op >> i == 0
11604 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11605 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11606 || (REG_P (XEXP (op0, 0))
11607 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11609 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11610 continue;
11613 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11614 fits in both M1 and M2 and the SUBREG is either paradoxical
11615 or represents the low part, permute the SUBREG and the AND
11616 and try again. */
11617 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11619 unsigned HOST_WIDE_INT c1;
11620 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11621 /* Require an integral mode, to avoid creating something like
11622 (AND:SF ...). */
11623 if (SCALAR_INT_MODE_P (tmode)
11624 /* It is unsafe to commute the AND into the SUBREG if the
11625 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11626 not defined. As originally written the upper bits
11627 have a defined value due to the AND operation.
11628 However, if we commute the AND inside the SUBREG then
11629 they no longer have defined values and the meaning of
11630 the code has been changed. */
11631 && (0
11632 #ifdef WORD_REGISTER_OPERATIONS
11633 || (mode_width > GET_MODE_PRECISION (tmode)
11634 && mode_width <= BITS_PER_WORD)
11635 #endif
11636 || (mode_width <= GET_MODE_PRECISION (tmode)
11637 && subreg_lowpart_p (XEXP (op0, 0))))
11638 && CONST_INT_P (XEXP (op0, 1))
11639 && mode_width <= HOST_BITS_PER_WIDE_INT
11640 && HWI_COMPUTABLE_MODE_P (tmode)
11641 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11642 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11643 && c1 != mask
11644 && c1 != GET_MODE_MASK (tmode))
11646 op0 = simplify_gen_binary (AND, tmode,
11647 SUBREG_REG (XEXP (op0, 0)),
11648 gen_int_mode (c1, tmode));
11649 op0 = gen_lowpart (mode, op0);
11650 continue;
11654 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11655 if (const_op == 0 && equality_comparison_p
11656 && XEXP (op0, 1) == const1_rtx
11657 && GET_CODE (XEXP (op0, 0)) == NOT)
11659 op0 = simplify_and_const_int (NULL_RTX, mode,
11660 XEXP (XEXP (op0, 0), 0), 1);
11661 code = (code == NE ? EQ : NE);
11662 continue;
11665 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11666 (eq (and (lshiftrt X) 1) 0).
11667 Also handle the case where (not X) is expressed using xor. */
11668 if (const_op == 0 && equality_comparison_p
11669 && XEXP (op0, 1) == const1_rtx
11670 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11672 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11673 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11675 if (GET_CODE (shift_op) == NOT
11676 || (GET_CODE (shift_op) == XOR
11677 && CONST_INT_P (XEXP (shift_op, 1))
11678 && CONST_INT_P (shift_count)
11679 && HWI_COMPUTABLE_MODE_P (mode)
11680 && (UINTVAL (XEXP (shift_op, 1))
11681 == (unsigned HOST_WIDE_INT) 1
11682 << INTVAL (shift_count))))
11685 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11686 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11687 code = (code == NE ? EQ : NE);
11688 continue;
11691 break;
11693 case ASHIFT:
11694 /* If we have (compare (ashift FOO N) (const_int C)) and
11695 the high order N bits of FOO (N+1 if an inequality comparison)
11696 are known to be zero, we can do this by comparing FOO with C
11697 shifted right N bits so long as the low-order N bits of C are
11698 zero. */
11699 if (CONST_INT_P (XEXP (op0, 1))
11700 && INTVAL (XEXP (op0, 1)) >= 0
11701 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11702 < HOST_BITS_PER_WIDE_INT)
11703 && (((unsigned HOST_WIDE_INT) const_op
11704 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11705 - 1)) == 0)
11706 && mode_width <= HOST_BITS_PER_WIDE_INT
11707 && (nonzero_bits (XEXP (op0, 0), mode)
11708 & ~(mask >> (INTVAL (XEXP (op0, 1))
11709 + ! equality_comparison_p))) == 0)
11711 /* We must perform a logical shift, not an arithmetic one,
11712 as we want the top N bits of C to be zero. */
11713 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11715 temp >>= INTVAL (XEXP (op0, 1));
11716 op1 = gen_int_mode (temp, mode);
11717 op0 = XEXP (op0, 0);
11718 continue;
11721 /* If we are doing a sign bit comparison, it means we are testing
11722 a particular bit. Convert it to the appropriate AND. */
11723 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11724 && mode_width <= HOST_BITS_PER_WIDE_INT)
11726 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11727 ((unsigned HOST_WIDE_INT) 1
11728 << (mode_width - 1
11729 - INTVAL (XEXP (op0, 1)))));
11730 code = (code == LT ? NE : EQ);
11731 continue;
11734 /* If this an equality comparison with zero and we are shifting
11735 the low bit to the sign bit, we can convert this to an AND of the
11736 low-order bit. */
11737 if (const_op == 0 && equality_comparison_p
11738 && CONST_INT_P (XEXP (op0, 1))
11739 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11741 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11742 continue;
11744 break;
11746 case ASHIFTRT:
11747 /* If this is an equality comparison with zero, we can do this
11748 as a logical shift, which might be much simpler. */
11749 if (equality_comparison_p && const_op == 0
11750 && CONST_INT_P (XEXP (op0, 1)))
11752 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11753 XEXP (op0, 0),
11754 INTVAL (XEXP (op0, 1)));
11755 continue;
11758 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11759 do the comparison in a narrower mode. */
11760 if (! unsigned_comparison_p
11761 && CONST_INT_P (XEXP (op0, 1))
11762 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11763 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11764 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11765 MODE_INT, 1)) != BLKmode
11766 && (((unsigned HOST_WIDE_INT) const_op
11767 + (GET_MODE_MASK (tmode) >> 1) + 1)
11768 <= GET_MODE_MASK (tmode)))
11770 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11771 continue;
11774 /* Likewise if OP0 is a PLUS of a sign extension with a
11775 constant, which is usually represented with the PLUS
11776 between the shifts. */
11777 if (! unsigned_comparison_p
11778 && CONST_INT_P (XEXP (op0, 1))
11779 && GET_CODE (XEXP (op0, 0)) == PLUS
11780 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11781 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11782 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11783 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11784 MODE_INT, 1)) != BLKmode
11785 && (((unsigned HOST_WIDE_INT) const_op
11786 + (GET_MODE_MASK (tmode) >> 1) + 1)
11787 <= GET_MODE_MASK (tmode)))
11789 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11790 rtx add_const = XEXP (XEXP (op0, 0), 1);
11791 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11792 add_const, XEXP (op0, 1));
11794 op0 = simplify_gen_binary (PLUS, tmode,
11795 gen_lowpart (tmode, inner),
11796 new_const);
11797 continue;
11800 /* ... fall through ... */
11801 case LSHIFTRT:
11802 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11803 the low order N bits of FOO are known to be zero, we can do this
11804 by comparing FOO with C shifted left N bits so long as no
11805 overflow occurs. Even if the low order N bits of FOO aren't known
11806 to be zero, if the comparison is >= or < we can use the same
11807 optimization and for > or <= by setting all the low
11808 order N bits in the comparison constant. */
11809 if (CONST_INT_P (XEXP (op0, 1))
11810 && INTVAL (XEXP (op0, 1)) > 0
11811 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11812 && mode_width <= HOST_BITS_PER_WIDE_INT
11813 && (((unsigned HOST_WIDE_INT) const_op
11814 + (GET_CODE (op0) != LSHIFTRT
11815 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11816 + 1)
11817 : 0))
11818 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11820 unsigned HOST_WIDE_INT low_bits
11821 = (nonzero_bits (XEXP (op0, 0), mode)
11822 & (((unsigned HOST_WIDE_INT) 1
11823 << INTVAL (XEXP (op0, 1))) - 1));
11824 if (low_bits == 0 || !equality_comparison_p)
11826 /* If the shift was logical, then we must make the condition
11827 unsigned. */
11828 if (GET_CODE (op0) == LSHIFTRT)
11829 code = unsigned_condition (code);
11831 const_op <<= INTVAL (XEXP (op0, 1));
11832 if (low_bits != 0
11833 && (code == GT || code == GTU
11834 || code == LE || code == LEU))
11835 const_op
11836 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11837 op1 = GEN_INT (const_op);
11838 op0 = XEXP (op0, 0);
11839 continue;
11843 /* If we are using this shift to extract just the sign bit, we
11844 can replace this with an LT or GE comparison. */
11845 if (const_op == 0
11846 && (equality_comparison_p || sign_bit_comparison_p)
11847 && CONST_INT_P (XEXP (op0, 1))
11848 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11850 op0 = XEXP (op0, 0);
11851 code = (code == NE || code == GT ? LT : GE);
11852 continue;
11854 break;
11856 default:
11857 break;
11860 break;
11863 /* Now make any compound operations involved in this comparison. Then,
11864 check for an outmost SUBREG on OP0 that is not doing anything or is
11865 paradoxical. The latter transformation must only be performed when
11866 it is known that the "extra" bits will be the same in op0 and op1 or
11867 that they don't matter. There are three cases to consider:
11869 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11870 care bits and we can assume they have any convenient value. So
11871 making the transformation is safe.
11873 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11874 In this case the upper bits of op0 are undefined. We should not make
11875 the simplification in that case as we do not know the contents of
11876 those bits.
11878 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11879 UNKNOWN. In that case we know those bits are zeros or ones. We must
11880 also be sure that they are the same as the upper bits of op1.
11882 We can never remove a SUBREG for a non-equality comparison because
11883 the sign bit is in a different place in the underlying object. */
11885 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11886 op1 = make_compound_operation (op1, SET);
11888 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11889 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11890 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11891 && (code == NE || code == EQ))
11893 if (paradoxical_subreg_p (op0))
11895 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11896 implemented. */
11897 if (REG_P (SUBREG_REG (op0)))
11899 op0 = SUBREG_REG (op0);
11900 op1 = gen_lowpart (GET_MODE (op0), op1);
11903 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
11904 <= HOST_BITS_PER_WIDE_INT)
11905 && (nonzero_bits (SUBREG_REG (op0),
11906 GET_MODE (SUBREG_REG (op0)))
11907 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11909 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11911 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11912 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11913 op0 = SUBREG_REG (op0), op1 = tem;
11917 /* We now do the opposite procedure: Some machines don't have compare
11918 insns in all modes. If OP0's mode is an integer mode smaller than a
11919 word and we can't do a compare in that mode, see if there is a larger
11920 mode for which we can do the compare. There are a number of cases in
11921 which we can use the wider mode. */
11923 mode = GET_MODE (op0);
11924 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11925 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11926 && ! have_insn_for (COMPARE, mode))
11927 for (tmode = GET_MODE_WIDER_MODE (mode);
11928 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
11929 tmode = GET_MODE_WIDER_MODE (tmode))
11930 if (have_insn_for (COMPARE, tmode))
11932 int zero_extended;
11934 /* If this is a test for negative, we can make an explicit
11935 test of the sign bit. Test this first so we can use
11936 a paradoxical subreg to extend OP0. */
11938 if (op1 == const0_rtx && (code == LT || code == GE)
11939 && HWI_COMPUTABLE_MODE_P (mode))
11941 op0 = simplify_gen_binary (AND, tmode,
11942 gen_lowpart (tmode, op0),
11943 GEN_INT ((unsigned HOST_WIDE_INT) 1
11944 << (GET_MODE_BITSIZE (mode)
11945 - 1)));
11946 code = (code == LT) ? NE : EQ;
11947 break;
11950 /* If the only nonzero bits in OP0 and OP1 are those in the
11951 narrower mode and this is an equality or unsigned comparison,
11952 we can use the wider mode. Similarly for sign-extended
11953 values, in which case it is true for all comparisons. */
11954 zero_extended = ((code == EQ || code == NE
11955 || code == GEU || code == GTU
11956 || code == LEU || code == LTU)
11957 && (nonzero_bits (op0, tmode)
11958 & ~GET_MODE_MASK (mode)) == 0
11959 && ((CONST_INT_P (op1)
11960 || (nonzero_bits (op1, tmode)
11961 & ~GET_MODE_MASK (mode)) == 0)));
11963 if (zero_extended
11964 || ((num_sign_bit_copies (op0, tmode)
11965 > (unsigned int) (GET_MODE_PRECISION (tmode)
11966 - GET_MODE_PRECISION (mode)))
11967 && (num_sign_bit_copies (op1, tmode)
11968 > (unsigned int) (GET_MODE_PRECISION (tmode)
11969 - GET_MODE_PRECISION (mode)))))
11971 /* If OP0 is an AND and we don't have an AND in MODE either,
11972 make a new AND in the proper mode. */
11973 if (GET_CODE (op0) == AND
11974 && !have_insn_for (AND, mode))
11975 op0 = simplify_gen_binary (AND, tmode,
11976 gen_lowpart (tmode,
11977 XEXP (op0, 0)),
11978 gen_lowpart (tmode,
11979 XEXP (op0, 1)));
11980 else
11982 if (zero_extended)
11984 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11985 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11987 else
11989 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11990 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11992 break;
11997 #ifdef CANONICALIZE_COMPARISON
11998 /* If this machine only supports a subset of valid comparisons, see if we
11999 can convert an unsupported one into a supported one. */
12000 CANONICALIZE_COMPARISON (code, op0, op1);
12001 #endif
12003 *pop0 = op0;
12004 *pop1 = op1;
12006 return code;
12009 /* Utility function for record_value_for_reg. Count number of
12010 rtxs in X. */
12011 static int
12012 count_rtxs (rtx x)
12014 enum rtx_code code = GET_CODE (x);
12015 const char *fmt;
12016 int i, j, ret = 1;
12018 if (GET_RTX_CLASS (code) == '2'
12019 || GET_RTX_CLASS (code) == 'c')
12021 rtx x0 = XEXP (x, 0);
12022 rtx x1 = XEXP (x, 1);
12024 if (x0 == x1)
12025 return 1 + 2 * count_rtxs (x0);
12027 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
12028 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
12029 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12030 return 2 + 2 * count_rtxs (x0)
12031 + count_rtxs (x == XEXP (x1, 0)
12032 ? XEXP (x1, 1) : XEXP (x1, 0));
12034 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
12035 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
12036 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12037 return 2 + 2 * count_rtxs (x1)
12038 + count_rtxs (x == XEXP (x0, 0)
12039 ? XEXP (x0, 1) : XEXP (x0, 0));
12042 fmt = GET_RTX_FORMAT (code);
12043 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12044 if (fmt[i] == 'e')
12045 ret += count_rtxs (XEXP (x, i));
12046 else if (fmt[i] == 'E')
12047 for (j = 0; j < XVECLEN (x, i); j++)
12048 ret += count_rtxs (XVECEXP (x, i, j));
12050 return ret;
12053 /* Utility function for following routine. Called when X is part of a value
12054 being stored into last_set_value. Sets last_set_table_tick
12055 for each register mentioned. Similar to mention_regs in cse.c */
12057 static void
12058 update_table_tick (rtx x)
12060 enum rtx_code code = GET_CODE (x);
12061 const char *fmt = GET_RTX_FORMAT (code);
12062 int i, j;
12064 if (code == REG)
12066 unsigned int regno = REGNO (x);
12067 unsigned int endregno = END_REGNO (x);
12068 unsigned int r;
12070 for (r = regno; r < endregno; r++)
12072 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, r);
12073 rsp->last_set_table_tick = label_tick;
12076 return;
12079 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12080 if (fmt[i] == 'e')
12082 /* Check for identical subexpressions. If x contains
12083 identical subexpression we only have to traverse one of
12084 them. */
12085 if (i == 0 && ARITHMETIC_P (x))
12087 /* Note that at this point x1 has already been
12088 processed. */
12089 rtx x0 = XEXP (x, 0);
12090 rtx x1 = XEXP (x, 1);
12092 /* If x0 and x1 are identical then there is no need to
12093 process x0. */
12094 if (x0 == x1)
12095 break;
12097 /* If x0 is identical to a subexpression of x1 then while
12098 processing x1, x0 has already been processed. Thus we
12099 are done with x. */
12100 if (ARITHMETIC_P (x1)
12101 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12102 break;
12104 /* If x1 is identical to a subexpression of x0 then we
12105 still have to process the rest of x0. */
12106 if (ARITHMETIC_P (x0)
12107 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12109 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12110 break;
12114 update_table_tick (XEXP (x, i));
12116 else if (fmt[i] == 'E')
12117 for (j = 0; j < XVECLEN (x, i); j++)
12118 update_table_tick (XVECEXP (x, i, j));
12121 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12122 are saying that the register is clobbered and we no longer know its
12123 value. If INSN is zero, don't update reg_stat[].last_set; this is
12124 only permitted with VALUE also zero and is used to invalidate the
12125 register. */
12127 static void
12128 record_value_for_reg (rtx reg, rtx insn, rtx value)
12130 unsigned int regno = REGNO (reg);
12131 unsigned int endregno = END_REGNO (reg);
12132 unsigned int i;
12133 reg_stat_type *rsp;
12135 /* If VALUE contains REG and we have a previous value for REG, substitute
12136 the previous value. */
12137 if (value && insn && reg_overlap_mentioned_p (reg, value))
12139 rtx tem;
12141 /* Set things up so get_last_value is allowed to see anything set up to
12142 our insn. */
12143 subst_low_luid = DF_INSN_LUID (insn);
12144 tem = get_last_value (reg);
12146 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12147 it isn't going to be useful and will take a lot of time to process,
12148 so just use the CLOBBER. */
12150 if (tem)
12152 if (ARITHMETIC_P (tem)
12153 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12154 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12155 tem = XEXP (tem, 0);
12156 else if (count_occurrences (value, reg, 1) >= 2)
12158 /* If there are two or more occurrences of REG in VALUE,
12159 prevent the value from growing too much. */
12160 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12161 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12164 value = replace_rtx (copy_rtx (value), reg, tem);
12168 /* For each register modified, show we don't know its value, that
12169 we don't know about its bitwise content, that its value has been
12170 updated, and that we don't know the location of the death of the
12171 register. */
12172 for (i = regno; i < endregno; i++)
12174 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12176 if (insn)
12177 rsp->last_set = insn;
12179 rsp->last_set_value = 0;
12180 rsp->last_set_mode = VOIDmode;
12181 rsp->last_set_nonzero_bits = 0;
12182 rsp->last_set_sign_bit_copies = 0;
12183 rsp->last_death = 0;
12184 rsp->truncated_to_mode = VOIDmode;
12187 /* Mark registers that are being referenced in this value. */
12188 if (value)
12189 update_table_tick (value);
12191 /* Now update the status of each register being set.
12192 If someone is using this register in this block, set this register
12193 to invalid since we will get confused between the two lives in this
12194 basic block. This makes using this register always invalid. In cse, we
12195 scan the table to invalidate all entries using this register, but this
12196 is too much work for us. */
12198 for (i = regno; i < endregno; i++)
12200 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12201 rsp->last_set_label = label_tick;
12202 if (!insn
12203 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12204 rsp->last_set_invalid = 1;
12205 else
12206 rsp->last_set_invalid = 0;
12209 /* The value being assigned might refer to X (like in "x++;"). In that
12210 case, we must replace it with (clobber (const_int 0)) to prevent
12211 infinite loops. */
12212 rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12213 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12215 value = copy_rtx (value);
12216 if (!get_last_value_validate (&value, insn, label_tick, 1))
12217 value = 0;
12220 /* For the main register being modified, update the value, the mode, the
12221 nonzero bits, and the number of sign bit copies. */
12223 rsp->last_set_value = value;
12225 if (value)
12227 enum machine_mode mode = GET_MODE (reg);
12228 subst_low_luid = DF_INSN_LUID (insn);
12229 rsp->last_set_mode = mode;
12230 if (GET_MODE_CLASS (mode) == MODE_INT
12231 && HWI_COMPUTABLE_MODE_P (mode))
12232 mode = nonzero_bits_mode;
12233 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12234 rsp->last_set_sign_bit_copies
12235 = num_sign_bit_copies (value, GET_MODE (reg));
12239 /* Called via note_stores from record_dead_and_set_regs to handle one
12240 SET or CLOBBER in an insn. DATA is the instruction in which the
12241 set is occurring. */
12243 static void
12244 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12246 rtx record_dead_insn = (rtx) data;
12248 if (GET_CODE (dest) == SUBREG)
12249 dest = SUBREG_REG (dest);
12251 if (!record_dead_insn)
12253 if (REG_P (dest))
12254 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12255 return;
12258 if (REG_P (dest))
12260 /* If we are setting the whole register, we know its value. Otherwise
12261 show that we don't know the value. We can handle SUBREG in
12262 some cases. */
12263 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12264 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12265 else if (GET_CODE (setter) == SET
12266 && GET_CODE (SET_DEST (setter)) == SUBREG
12267 && SUBREG_REG (SET_DEST (setter)) == dest
12268 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12269 && subreg_lowpart_p (SET_DEST (setter)))
12270 record_value_for_reg (dest, record_dead_insn,
12271 gen_lowpart (GET_MODE (dest),
12272 SET_SRC (setter)));
12273 else
12274 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12276 else if (MEM_P (dest)
12277 /* Ignore pushes, they clobber nothing. */
12278 && ! push_operand (dest, GET_MODE (dest)))
12279 mem_last_set = DF_INSN_LUID (record_dead_insn);
12282 /* Update the records of when each REG was most recently set or killed
12283 for the things done by INSN. This is the last thing done in processing
12284 INSN in the combiner loop.
12286 We update reg_stat[], in particular fields last_set, last_set_value,
12287 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12288 last_death, and also the similar information mem_last_set (which insn
12289 most recently modified memory) and last_call_luid (which insn was the
12290 most recent subroutine call). */
12292 static void
12293 record_dead_and_set_regs (rtx insn)
12295 rtx link;
12296 unsigned int i;
12298 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12300 if (REG_NOTE_KIND (link) == REG_DEAD
12301 && REG_P (XEXP (link, 0)))
12303 unsigned int regno = REGNO (XEXP (link, 0));
12304 unsigned int endregno = END_REGNO (XEXP (link, 0));
12306 for (i = regno; i < endregno; i++)
12308 reg_stat_type *rsp;
12310 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12311 rsp->last_death = insn;
12314 else if (REG_NOTE_KIND (link) == REG_INC)
12315 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12318 if (CALL_P (insn))
12320 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12321 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12323 reg_stat_type *rsp;
12325 rsp = &VEC_index (reg_stat_type, reg_stat, i);
12326 rsp->last_set_invalid = 1;
12327 rsp->last_set = insn;
12328 rsp->last_set_value = 0;
12329 rsp->last_set_mode = VOIDmode;
12330 rsp->last_set_nonzero_bits = 0;
12331 rsp->last_set_sign_bit_copies = 0;
12332 rsp->last_death = 0;
12333 rsp->truncated_to_mode = VOIDmode;
12336 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12338 /* We can't combine into a call pattern. Remember, though, that
12339 the return value register is set at this LUID. We could
12340 still replace a register with the return value from the
12341 wrong subroutine call! */
12342 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12344 else
12345 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12348 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12349 register present in the SUBREG, so for each such SUBREG go back and
12350 adjust nonzero and sign bit information of the registers that are
12351 known to have some zero/sign bits set.
12353 This is needed because when combine blows the SUBREGs away, the
12354 information on zero/sign bits is lost and further combines can be
12355 missed because of that. */
12357 static void
12358 record_promoted_value (rtx insn, rtx subreg)
12360 struct insn_link *links;
12361 rtx set;
12362 unsigned int regno = REGNO (SUBREG_REG (subreg));
12363 enum machine_mode mode = GET_MODE (subreg);
12365 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12366 return;
12368 for (links = LOG_LINKS (insn); links;)
12370 reg_stat_type *rsp;
12372 insn = links->insn;
12373 set = single_set (insn);
12375 if (! set || !REG_P (SET_DEST (set))
12376 || REGNO (SET_DEST (set)) != regno
12377 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12379 links = links->next;
12380 continue;
12383 rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12384 if (rsp->last_set == insn)
12386 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12387 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12390 if (REG_P (SET_SRC (set)))
12392 regno = REGNO (SET_SRC (set));
12393 links = LOG_LINKS (insn);
12395 else
12396 break;
12400 /* Check if X, a register, is known to contain a value already
12401 truncated to MODE. In this case we can use a subreg to refer to
12402 the truncated value even though in the generic case we would need
12403 an explicit truncation. */
12405 static bool
12406 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12408 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
12409 enum machine_mode truncated = rsp->truncated_to_mode;
12411 if (truncated == 0
12412 || rsp->truncation_label < label_tick_ebb_start)
12413 return false;
12414 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12415 return true;
12416 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12417 return true;
12418 return false;
12421 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
12422 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
12423 might be able to turn a truncate into a subreg using this information.
12424 Return -1 if traversing *P is complete or 0 otherwise. */
12426 static int
12427 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12429 rtx x = *p;
12430 enum machine_mode truncated_mode;
12431 reg_stat_type *rsp;
12433 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12435 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12436 truncated_mode = GET_MODE (x);
12438 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12439 return -1;
12441 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12442 return -1;
12444 x = SUBREG_REG (x);
12446 /* ??? For hard-regs we now record everything. We might be able to
12447 optimize this using last_set_mode. */
12448 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12449 truncated_mode = GET_MODE (x);
12450 else
12451 return 0;
12453 rsp = &VEC_index (reg_stat_type, reg_stat, REGNO (x));
12454 if (rsp->truncated_to_mode == 0
12455 || rsp->truncation_label < label_tick_ebb_start
12456 || (GET_MODE_SIZE (truncated_mode)
12457 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12459 rsp->truncated_to_mode = truncated_mode;
12460 rsp->truncation_label = label_tick;
12463 return -1;
12466 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12467 the modes they are used in. This can help truning TRUNCATEs into
12468 SUBREGs. */
12470 static void
12471 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12473 for_each_rtx (x, record_truncated_value, NULL);
12476 /* Scan X for promoted SUBREGs. For each one found,
12477 note what it implies to the registers used in it. */
12479 static void
12480 check_promoted_subreg (rtx insn, rtx x)
12482 if (GET_CODE (x) == SUBREG
12483 && SUBREG_PROMOTED_VAR_P (x)
12484 && REG_P (SUBREG_REG (x)))
12485 record_promoted_value (insn, x);
12486 else
12488 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12489 int i, j;
12491 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12492 switch (format[i])
12494 case 'e':
12495 check_promoted_subreg (insn, XEXP (x, i));
12496 break;
12497 case 'V':
12498 case 'E':
12499 if (XVEC (x, i) != 0)
12500 for (j = 0; j < XVECLEN (x, i); j++)
12501 check_promoted_subreg (insn, XVECEXP (x, i, j));
12502 break;
12507 /* Verify that all the registers and memory references mentioned in *LOC are
12508 still valid. *LOC was part of a value set in INSN when label_tick was
12509 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12510 the invalid references with (clobber (const_int 0)) and return 1. This
12511 replacement is useful because we often can get useful information about
12512 the form of a value (e.g., if it was produced by a shift that always
12513 produces -1 or 0) even though we don't know exactly what registers it
12514 was produced from. */
12516 static int
12517 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12519 rtx x = *loc;
12520 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12521 int len = GET_RTX_LENGTH (GET_CODE (x));
12522 int i, j;
12524 if (REG_P (x))
12526 unsigned int regno = REGNO (x);
12527 unsigned int endregno = END_REGNO (x);
12528 unsigned int j;
12530 for (j = regno; j < endregno; j++)
12532 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, j);
12533 if (rsp->last_set_invalid
12534 /* If this is a pseudo-register that was only set once and not
12535 live at the beginning of the function, it is always valid. */
12536 || (! (regno >= FIRST_PSEUDO_REGISTER
12537 && REG_N_SETS (regno) == 1
12538 && (!REGNO_REG_SET_P
12539 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12540 && rsp->last_set_label > tick))
12542 if (replace)
12543 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12544 return replace;
12548 return 1;
12550 /* If this is a memory reference, make sure that there were no stores after
12551 it that might have clobbered the value. We don't have alias info, so we
12552 assume any store invalidates it. Moreover, we only have local UIDs, so
12553 we also assume that there were stores in the intervening basic blocks. */
12554 else if (MEM_P (x) && !MEM_READONLY_P (x)
12555 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12557 if (replace)
12558 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12559 return replace;
12562 for (i = 0; i < len; i++)
12564 if (fmt[i] == 'e')
12566 /* Check for identical subexpressions. If x contains
12567 identical subexpression we only have to traverse one of
12568 them. */
12569 if (i == 1 && ARITHMETIC_P (x))
12571 /* Note that at this point x0 has already been checked
12572 and found valid. */
12573 rtx x0 = XEXP (x, 0);
12574 rtx x1 = XEXP (x, 1);
12576 /* If x0 and x1 are identical then x is also valid. */
12577 if (x0 == x1)
12578 return 1;
12580 /* If x1 is identical to a subexpression of x0 then
12581 while checking x0, x1 has already been checked. Thus
12582 it is valid and so as x. */
12583 if (ARITHMETIC_P (x0)
12584 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12585 return 1;
12587 /* If x0 is identical to a subexpression of x1 then x is
12588 valid iff the rest of x1 is valid. */
12589 if (ARITHMETIC_P (x1)
12590 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12591 return
12592 get_last_value_validate (&XEXP (x1,
12593 x0 == XEXP (x1, 0) ? 1 : 0),
12594 insn, tick, replace);
12597 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12598 replace) == 0)
12599 return 0;
12601 else if (fmt[i] == 'E')
12602 for (j = 0; j < XVECLEN (x, i); j++)
12603 if (get_last_value_validate (&XVECEXP (x, i, j),
12604 insn, tick, replace) == 0)
12605 return 0;
12608 /* If we haven't found a reason for it to be invalid, it is valid. */
12609 return 1;
12612 /* Get the last value assigned to X, if known. Some registers
12613 in the value may be replaced with (clobber (const_int 0)) if their value
12614 is known longer known reliably. */
12616 static rtx
12617 get_last_value (const_rtx x)
12619 unsigned int regno;
12620 rtx value;
12621 reg_stat_type *rsp;
12623 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12624 then convert it to the desired mode. If this is a paradoxical SUBREG,
12625 we cannot predict what values the "extra" bits might have. */
12626 if (GET_CODE (x) == SUBREG
12627 && subreg_lowpart_p (x)
12628 && !paradoxical_subreg_p (x)
12629 && (value = get_last_value (SUBREG_REG (x))) != 0)
12630 return gen_lowpart (GET_MODE (x), value);
12632 if (!REG_P (x))
12633 return 0;
12635 regno = REGNO (x);
12636 rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12637 value = rsp->last_set_value;
12639 /* If we don't have a value, or if it isn't for this basic block and
12640 it's either a hard register, set more than once, or it's a live
12641 at the beginning of the function, return 0.
12643 Because if it's not live at the beginning of the function then the reg
12644 is always set before being used (is never used without being set).
12645 And, if it's set only once, and it's always set before use, then all
12646 uses must have the same last value, even if it's not from this basic
12647 block. */
12649 if (value == 0
12650 || (rsp->last_set_label < label_tick_ebb_start
12651 && (regno < FIRST_PSEUDO_REGISTER
12652 || REG_N_SETS (regno) != 1
12653 || REGNO_REG_SET_P
12654 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12655 return 0;
12657 /* If the value was set in a later insn than the ones we are processing,
12658 we can't use it even if the register was only set once. */
12659 if (rsp->last_set_label == label_tick
12660 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12661 return 0;
12663 /* If the value has all its registers valid, return it. */
12664 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12665 return value;
12667 /* Otherwise, make a copy and replace any invalid register with
12668 (clobber (const_int 0)). If that fails for some reason, return 0. */
12670 value = copy_rtx (value);
12671 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12672 return value;
12674 return 0;
12677 /* Return nonzero if expression X refers to a REG or to memory
12678 that is set in an instruction more recent than FROM_LUID. */
12680 static int
12681 use_crosses_set_p (const_rtx x, int from_luid)
12683 const char *fmt;
12684 int i;
12685 enum rtx_code code = GET_CODE (x);
12687 if (code == REG)
12689 unsigned int regno = REGNO (x);
12690 unsigned endreg = END_REGNO (x);
12692 #ifdef PUSH_ROUNDING
12693 /* Don't allow uses of the stack pointer to be moved,
12694 because we don't know whether the move crosses a push insn. */
12695 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12696 return 1;
12697 #endif
12698 for (; regno < endreg; regno++)
12700 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, regno);
12701 if (rsp->last_set
12702 && rsp->last_set_label == label_tick
12703 && DF_INSN_LUID (rsp->last_set) > from_luid)
12704 return 1;
12706 return 0;
12709 if (code == MEM && mem_last_set > from_luid)
12710 return 1;
12712 fmt = GET_RTX_FORMAT (code);
12714 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12716 if (fmt[i] == 'E')
12718 int j;
12719 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12720 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12721 return 1;
12723 else if (fmt[i] == 'e'
12724 && use_crosses_set_p (XEXP (x, i), from_luid))
12725 return 1;
12727 return 0;
12730 /* Define three variables used for communication between the following
12731 routines. */
12733 static unsigned int reg_dead_regno, reg_dead_endregno;
12734 static int reg_dead_flag;
12736 /* Function called via note_stores from reg_dead_at_p.
12738 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12739 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12741 static void
12742 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12744 unsigned int regno, endregno;
12746 if (!REG_P (dest))
12747 return;
12749 regno = REGNO (dest);
12750 endregno = END_REGNO (dest);
12751 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12752 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12755 /* Return nonzero if REG is known to be dead at INSN.
12757 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12758 referencing REG, it is dead. If we hit a SET referencing REG, it is
12759 live. Otherwise, see if it is live or dead at the start of the basic
12760 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12761 must be assumed to be always live. */
12763 static int
12764 reg_dead_at_p (rtx reg, rtx insn)
12766 basic_block block;
12767 unsigned int i;
12769 /* Set variables for reg_dead_at_p_1. */
12770 reg_dead_regno = REGNO (reg);
12771 reg_dead_endregno = END_REGNO (reg);
12773 reg_dead_flag = 0;
12775 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12776 we allow the machine description to decide whether use-and-clobber
12777 patterns are OK. */
12778 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12780 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12781 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12782 return 0;
12785 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12786 beginning of basic block. */
12787 block = BLOCK_FOR_INSN (insn);
12788 for (;;)
12790 if (INSN_P (insn))
12792 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12793 if (reg_dead_flag)
12794 return reg_dead_flag == 1 ? 1 : 0;
12796 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12797 return 1;
12800 if (insn == BB_HEAD (block))
12801 break;
12803 insn = PREV_INSN (insn);
12806 /* Look at live-in sets for the basic block that we were in. */
12807 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12808 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12809 return 0;
12811 return 1;
12814 /* Note hard registers in X that are used. */
12816 static void
12817 mark_used_regs_combine (rtx x)
12819 RTX_CODE code = GET_CODE (x);
12820 unsigned int regno;
12821 int i;
12823 switch (code)
12825 case LABEL_REF:
12826 case SYMBOL_REF:
12827 case CONST:
12828 CASE_CONST_ANY:
12829 case PC:
12830 case ADDR_VEC:
12831 case ADDR_DIFF_VEC:
12832 case ASM_INPUT:
12833 #ifdef HAVE_cc0
12834 /* CC0 must die in the insn after it is set, so we don't need to take
12835 special note of it here. */
12836 case CC0:
12837 #endif
12838 return;
12840 case CLOBBER:
12841 /* If we are clobbering a MEM, mark any hard registers inside the
12842 address as used. */
12843 if (MEM_P (XEXP (x, 0)))
12844 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12845 return;
12847 case REG:
12848 regno = REGNO (x);
12849 /* A hard reg in a wide mode may really be multiple registers.
12850 If so, mark all of them just like the first. */
12851 if (regno < FIRST_PSEUDO_REGISTER)
12853 /* None of this applies to the stack, frame or arg pointers. */
12854 if (regno == STACK_POINTER_REGNUM
12855 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12856 || regno == HARD_FRAME_POINTER_REGNUM
12857 #endif
12858 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12859 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12860 #endif
12861 || regno == FRAME_POINTER_REGNUM)
12862 return;
12864 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12866 return;
12868 case SET:
12870 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12871 the address. */
12872 rtx testreg = SET_DEST (x);
12874 while (GET_CODE (testreg) == SUBREG
12875 || GET_CODE (testreg) == ZERO_EXTRACT
12876 || GET_CODE (testreg) == STRICT_LOW_PART)
12877 testreg = XEXP (testreg, 0);
12879 if (MEM_P (testreg))
12880 mark_used_regs_combine (XEXP (testreg, 0));
12882 mark_used_regs_combine (SET_SRC (x));
12884 return;
12886 default:
12887 break;
12890 /* Recursively scan the operands of this expression. */
12893 const char *fmt = GET_RTX_FORMAT (code);
12895 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12897 if (fmt[i] == 'e')
12898 mark_used_regs_combine (XEXP (x, i));
12899 else if (fmt[i] == 'E')
12901 int j;
12903 for (j = 0; j < XVECLEN (x, i); j++)
12904 mark_used_regs_combine (XVECEXP (x, i, j));
12910 /* Remove register number REGNO from the dead registers list of INSN.
12912 Return the note used to record the death, if there was one. */
12915 remove_death (unsigned int regno, rtx insn)
12917 rtx note = find_regno_note (insn, REG_DEAD, regno);
12919 if (note)
12920 remove_note (insn, note);
12922 return note;
12925 /* For each register (hardware or pseudo) used within expression X, if its
12926 death is in an instruction with luid between FROM_LUID (inclusive) and
12927 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12928 list headed by PNOTES.
12930 That said, don't move registers killed by maybe_kill_insn.
12932 This is done when X is being merged by combination into TO_INSN. These
12933 notes will then be distributed as needed. */
12935 static void
12936 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12937 rtx *pnotes)
12939 const char *fmt;
12940 int len, i;
12941 enum rtx_code code = GET_CODE (x);
12943 if (code == REG)
12945 unsigned int regno = REGNO (x);
12946 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno).last_death;
12948 /* Don't move the register if it gets killed in between from and to. */
12949 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12950 && ! reg_referenced_p (x, maybe_kill_insn))
12951 return;
12953 if (where_dead
12954 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12955 && DF_INSN_LUID (where_dead) >= from_luid
12956 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12958 rtx note = remove_death (regno, where_dead);
12960 /* It is possible for the call above to return 0. This can occur
12961 when last_death points to I2 or I1 that we combined with.
12962 In that case make a new note.
12964 We must also check for the case where X is a hard register
12965 and NOTE is a death note for a range of hard registers
12966 including X. In that case, we must put REG_DEAD notes for
12967 the remaining registers in place of NOTE. */
12969 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12970 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12971 > GET_MODE_SIZE (GET_MODE (x))))
12973 unsigned int deadregno = REGNO (XEXP (note, 0));
12974 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12975 unsigned int ourend = END_HARD_REGNO (x);
12976 unsigned int i;
12978 for (i = deadregno; i < deadend; i++)
12979 if (i < regno || i >= ourend)
12980 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12983 /* If we didn't find any note, or if we found a REG_DEAD note that
12984 covers only part of the given reg, and we have a multi-reg hard
12985 register, then to be safe we must check for REG_DEAD notes
12986 for each register other than the first. They could have
12987 their own REG_DEAD notes lying around. */
12988 else if ((note == 0
12989 || (note != 0
12990 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12991 < GET_MODE_SIZE (GET_MODE (x)))))
12992 && regno < FIRST_PSEUDO_REGISTER
12993 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12995 unsigned int ourend = END_HARD_REGNO (x);
12996 unsigned int i, offset;
12997 rtx oldnotes = 0;
12999 if (note)
13000 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13001 else
13002 offset = 1;
13004 for (i = regno + offset; i < ourend; i++)
13005 move_deaths (regno_reg_rtx[i],
13006 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13009 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13011 XEXP (note, 1) = *pnotes;
13012 *pnotes = note;
13014 else
13015 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13018 return;
13021 else if (GET_CODE (x) == SET)
13023 rtx dest = SET_DEST (x);
13025 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13027 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13028 that accesses one word of a multi-word item, some
13029 piece of everything register in the expression is used by
13030 this insn, so remove any old death. */
13031 /* ??? So why do we test for equality of the sizes? */
13033 if (GET_CODE (dest) == ZERO_EXTRACT
13034 || GET_CODE (dest) == STRICT_LOW_PART
13035 || (GET_CODE (dest) == SUBREG
13036 && (((GET_MODE_SIZE (GET_MODE (dest))
13037 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13038 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13039 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13041 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13042 return;
13045 /* If this is some other SUBREG, we know it replaces the entire
13046 value, so use that as the destination. */
13047 if (GET_CODE (dest) == SUBREG)
13048 dest = SUBREG_REG (dest);
13050 /* If this is a MEM, adjust deaths of anything used in the address.
13051 For a REG (the only other possibility), the entire value is
13052 being replaced so the old value is not used in this insn. */
13054 if (MEM_P (dest))
13055 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13056 to_insn, pnotes);
13057 return;
13060 else if (GET_CODE (x) == CLOBBER)
13061 return;
13063 len = GET_RTX_LENGTH (code);
13064 fmt = GET_RTX_FORMAT (code);
13066 for (i = 0; i < len; i++)
13068 if (fmt[i] == 'E')
13070 int j;
13071 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13072 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13073 to_insn, pnotes);
13075 else if (fmt[i] == 'e')
13076 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13080 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13081 pattern of an insn. X must be a REG. */
13083 static int
13084 reg_bitfield_target_p (rtx x, rtx body)
13086 int i;
13088 if (GET_CODE (body) == SET)
13090 rtx dest = SET_DEST (body);
13091 rtx target;
13092 unsigned int regno, tregno, endregno, endtregno;
13094 if (GET_CODE (dest) == ZERO_EXTRACT)
13095 target = XEXP (dest, 0);
13096 else if (GET_CODE (dest) == STRICT_LOW_PART)
13097 target = SUBREG_REG (XEXP (dest, 0));
13098 else
13099 return 0;
13101 if (GET_CODE (target) == SUBREG)
13102 target = SUBREG_REG (target);
13104 if (!REG_P (target))
13105 return 0;
13107 tregno = REGNO (target), regno = REGNO (x);
13108 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13109 return target == x;
13111 endtregno = end_hard_regno (GET_MODE (target), tregno);
13112 endregno = end_hard_regno (GET_MODE (x), regno);
13114 return endregno > tregno && regno < endtregno;
13117 else if (GET_CODE (body) == PARALLEL)
13118 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13119 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13120 return 1;
13122 return 0;
13125 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13126 as appropriate. I3 and I2 are the insns resulting from the combination
13127 insns including FROM (I2 may be zero).
13129 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13130 not need REG_DEAD notes because they are being substituted for. This
13131 saves searching in the most common cases.
13133 Each note in the list is either ignored or placed on some insns, depending
13134 on the type of note. */
13136 static void
13137 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13138 rtx elim_i1, rtx elim_i0)
13140 rtx note, next_note;
13141 rtx tem;
13143 for (note = notes; note; note = next_note)
13145 rtx place = 0, place2 = 0;
13147 next_note = XEXP (note, 1);
13148 switch (REG_NOTE_KIND (note))
13150 case REG_BR_PROB:
13151 case REG_BR_PRED:
13152 /* Doesn't matter much where we put this, as long as it's somewhere.
13153 It is preferable to keep these notes on branches, which is most
13154 likely to be i3. */
13155 place = i3;
13156 break;
13158 case REG_NON_LOCAL_GOTO:
13159 if (JUMP_P (i3))
13160 place = i3;
13161 else
13163 gcc_assert (i2 && JUMP_P (i2));
13164 place = i2;
13166 break;
13168 case REG_EH_REGION:
13169 /* These notes must remain with the call or trapping instruction. */
13170 if (CALL_P (i3))
13171 place = i3;
13172 else if (i2 && CALL_P (i2))
13173 place = i2;
13174 else
13176 gcc_assert (cfun->can_throw_non_call_exceptions);
13177 if (may_trap_p (i3))
13178 place = i3;
13179 else if (i2 && may_trap_p (i2))
13180 place = i2;
13181 /* ??? Otherwise assume we've combined things such that we
13182 can now prove that the instructions can't trap. Drop the
13183 note in this case. */
13185 break;
13187 case REG_ARGS_SIZE:
13188 /* ??? How to distribute between i3-i1. Assume i3 contains the
13189 entire adjustment. Assert i3 contains at least some adjust. */
13190 if (!noop_move_p (i3))
13192 int old_size, args_size = INTVAL (XEXP (note, 0));
13193 /* fixup_args_size_notes looks at REG_NORETURN note,
13194 so ensure the note is placed there first. */
13195 if (CALL_P (i3))
13197 rtx *np;
13198 for (np = &next_note; *np; np = &XEXP (*np, 1))
13199 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13201 rtx n = *np;
13202 *np = XEXP (n, 1);
13203 XEXP (n, 1) = REG_NOTES (i3);
13204 REG_NOTES (i3) = n;
13205 break;
13208 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13209 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13210 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13211 gcc_assert (old_size != args_size
13212 || (CALL_P (i3)
13213 && !ACCUMULATE_OUTGOING_ARGS
13214 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13216 break;
13218 case REG_NORETURN:
13219 case REG_SETJMP:
13220 case REG_TM:
13221 /* These notes must remain with the call. It should not be
13222 possible for both I2 and I3 to be a call. */
13223 if (CALL_P (i3))
13224 place = i3;
13225 else
13227 gcc_assert (i2 && CALL_P (i2));
13228 place = i2;
13230 break;
13232 case REG_UNUSED:
13233 /* Any clobbers for i3 may still exist, and so we must process
13234 REG_UNUSED notes from that insn.
13236 Any clobbers from i2 or i1 can only exist if they were added by
13237 recog_for_combine. In that case, recog_for_combine created the
13238 necessary REG_UNUSED notes. Trying to keep any original
13239 REG_UNUSED notes from these insns can cause incorrect output
13240 if it is for the same register as the original i3 dest.
13241 In that case, we will notice that the register is set in i3,
13242 and then add a REG_UNUSED note for the destination of i3, which
13243 is wrong. However, it is possible to have REG_UNUSED notes from
13244 i2 or i1 for register which were both used and clobbered, so
13245 we keep notes from i2 or i1 if they will turn into REG_DEAD
13246 notes. */
13248 /* If this register is set or clobbered in I3, put the note there
13249 unless there is one already. */
13250 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13252 if (from_insn != i3)
13253 break;
13255 if (! (REG_P (XEXP (note, 0))
13256 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13257 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13258 place = i3;
13260 /* Otherwise, if this register is used by I3, then this register
13261 now dies here, so we must put a REG_DEAD note here unless there
13262 is one already. */
13263 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13264 && ! (REG_P (XEXP (note, 0))
13265 ? find_regno_note (i3, REG_DEAD,
13266 REGNO (XEXP (note, 0)))
13267 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13269 PUT_REG_NOTE_KIND (note, REG_DEAD);
13270 place = i3;
13272 break;
13274 case REG_EQUAL:
13275 case REG_EQUIV:
13276 case REG_NOALIAS:
13277 /* These notes say something about results of an insn. We can
13278 only support them if they used to be on I3 in which case they
13279 remain on I3. Otherwise they are ignored.
13281 If the note refers to an expression that is not a constant, we
13282 must also ignore the note since we cannot tell whether the
13283 equivalence is still true. It might be possible to do
13284 slightly better than this (we only have a problem if I2DEST
13285 or I1DEST is present in the expression), but it doesn't
13286 seem worth the trouble. */
13288 if (from_insn == i3
13289 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13290 place = i3;
13291 break;
13293 case REG_INC:
13294 /* These notes say something about how a register is used. They must
13295 be present on any use of the register in I2 or I3. */
13296 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13297 place = i3;
13299 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13301 if (place)
13302 place2 = i2;
13303 else
13304 place = i2;
13306 break;
13308 case REG_LABEL_TARGET:
13309 case REG_LABEL_OPERAND:
13310 /* This can show up in several ways -- either directly in the
13311 pattern, or hidden off in the constant pool with (or without?)
13312 a REG_EQUAL note. */
13313 /* ??? Ignore the without-reg_equal-note problem for now. */
13314 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13315 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13316 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13317 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13318 place = i3;
13320 if (i2
13321 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13322 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13323 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13324 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13326 if (place)
13327 place2 = i2;
13328 else
13329 place = i2;
13332 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13333 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13334 there. */
13335 if (place && JUMP_P (place)
13336 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13337 && (JUMP_LABEL (place) == NULL
13338 || JUMP_LABEL (place) == XEXP (note, 0)))
13340 rtx label = JUMP_LABEL (place);
13342 if (!label)
13343 JUMP_LABEL (place) = XEXP (note, 0);
13344 else if (LABEL_P (label))
13345 LABEL_NUSES (label)--;
13348 if (place2 && JUMP_P (place2)
13349 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13350 && (JUMP_LABEL (place2) == NULL
13351 || JUMP_LABEL (place2) == XEXP (note, 0)))
13353 rtx label = JUMP_LABEL (place2);
13355 if (!label)
13356 JUMP_LABEL (place2) = XEXP (note, 0);
13357 else if (LABEL_P (label))
13358 LABEL_NUSES (label)--;
13359 place2 = 0;
13361 break;
13363 case REG_NONNEG:
13364 /* This note says something about the value of a register prior
13365 to the execution of an insn. It is too much trouble to see
13366 if the note is still correct in all situations. It is better
13367 to simply delete it. */
13368 break;
13370 case REG_DEAD:
13371 /* If we replaced the right hand side of FROM_INSN with a
13372 REG_EQUAL note, the original use of the dying register
13373 will not have been combined into I3 and I2. In such cases,
13374 FROM_INSN is guaranteed to be the first of the combined
13375 instructions, so we simply need to search back before
13376 FROM_INSN for the previous use or set of this register,
13377 then alter the notes there appropriately.
13379 If the register is used as an input in I3, it dies there.
13380 Similarly for I2, if it is nonzero and adjacent to I3.
13382 If the register is not used as an input in either I3 or I2
13383 and it is not one of the registers we were supposed to eliminate,
13384 there are two possibilities. We might have a non-adjacent I2
13385 or we might have somehow eliminated an additional register
13386 from a computation. For example, we might have had A & B where
13387 we discover that B will always be zero. In this case we will
13388 eliminate the reference to A.
13390 In both cases, we must search to see if we can find a previous
13391 use of A and put the death note there. */
13393 if (from_insn
13394 && from_insn == i2mod
13395 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13396 tem = from_insn;
13397 else
13399 if (from_insn
13400 && CALL_P (from_insn)
13401 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13402 place = from_insn;
13403 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13404 place = i3;
13405 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13406 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13407 place = i2;
13408 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13409 && !(i2mod
13410 && reg_overlap_mentioned_p (XEXP (note, 0),
13411 i2mod_old_rhs)))
13412 || rtx_equal_p (XEXP (note, 0), elim_i1)
13413 || rtx_equal_p (XEXP (note, 0), elim_i0))
13414 break;
13415 tem = i3;
13418 if (place == 0)
13420 basic_block bb = this_basic_block;
13422 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13424 if (!NONDEBUG_INSN_P (tem))
13426 if (tem == BB_HEAD (bb))
13427 break;
13428 continue;
13431 /* If the register is being set at TEM, see if that is all
13432 TEM is doing. If so, delete TEM. Otherwise, make this
13433 into a REG_UNUSED note instead. Don't delete sets to
13434 global register vars. */
13435 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13436 || !global_regs[REGNO (XEXP (note, 0))])
13437 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13439 rtx set = single_set (tem);
13440 rtx inner_dest = 0;
13441 #ifdef HAVE_cc0
13442 rtx cc0_setter = NULL_RTX;
13443 #endif
13445 if (set != 0)
13446 for (inner_dest = SET_DEST (set);
13447 (GET_CODE (inner_dest) == STRICT_LOW_PART
13448 || GET_CODE (inner_dest) == SUBREG
13449 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13450 inner_dest = XEXP (inner_dest, 0))
13453 /* Verify that it was the set, and not a clobber that
13454 modified the register.
13456 CC0 targets must be careful to maintain setter/user
13457 pairs. If we cannot delete the setter due to side
13458 effects, mark the user with an UNUSED note instead
13459 of deleting it. */
13461 if (set != 0 && ! side_effects_p (SET_SRC (set))
13462 && rtx_equal_p (XEXP (note, 0), inner_dest)
13463 #ifdef HAVE_cc0
13464 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13465 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13466 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13467 #endif
13470 /* Move the notes and links of TEM elsewhere.
13471 This might delete other dead insns recursively.
13472 First set the pattern to something that won't use
13473 any register. */
13474 rtx old_notes = REG_NOTES (tem);
13476 PATTERN (tem) = pc_rtx;
13477 REG_NOTES (tem) = NULL;
13479 distribute_notes (old_notes, tem, tem, NULL_RTX,
13480 NULL_RTX, NULL_RTX, NULL_RTX);
13481 distribute_links (LOG_LINKS (tem));
13483 SET_INSN_DELETED (tem);
13484 if (tem == i2)
13485 i2 = NULL_RTX;
13487 #ifdef HAVE_cc0
13488 /* Delete the setter too. */
13489 if (cc0_setter)
13491 PATTERN (cc0_setter) = pc_rtx;
13492 old_notes = REG_NOTES (cc0_setter);
13493 REG_NOTES (cc0_setter) = NULL;
13495 distribute_notes (old_notes, cc0_setter,
13496 cc0_setter, NULL_RTX,
13497 NULL_RTX, NULL_RTX, NULL_RTX);
13498 distribute_links (LOG_LINKS (cc0_setter));
13500 SET_INSN_DELETED (cc0_setter);
13501 if (cc0_setter == i2)
13502 i2 = NULL_RTX;
13504 #endif
13506 else
13508 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13510 /* If there isn't already a REG_UNUSED note, put one
13511 here. Do not place a REG_DEAD note, even if
13512 the register is also used here; that would not
13513 match the algorithm used in lifetime analysis
13514 and can cause the consistency check in the
13515 scheduler to fail. */
13516 if (! find_regno_note (tem, REG_UNUSED,
13517 REGNO (XEXP (note, 0))))
13518 place = tem;
13519 break;
13522 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13523 || (CALL_P (tem)
13524 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13526 place = tem;
13528 /* If we are doing a 3->2 combination, and we have a
13529 register which formerly died in i3 and was not used
13530 by i2, which now no longer dies in i3 and is used in
13531 i2 but does not die in i2, and place is between i2
13532 and i3, then we may need to move a link from place to
13533 i2. */
13534 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13535 && from_insn
13536 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13537 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13539 struct insn_link *links = LOG_LINKS (place);
13540 LOG_LINKS (place) = NULL;
13541 distribute_links (links);
13543 break;
13546 if (tem == BB_HEAD (bb))
13547 break;
13552 /* If the register is set or already dead at PLACE, we needn't do
13553 anything with this note if it is still a REG_DEAD note.
13554 We check here if it is set at all, not if is it totally replaced,
13555 which is what `dead_or_set_p' checks, so also check for it being
13556 set partially. */
13558 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13560 unsigned int regno = REGNO (XEXP (note, 0));
13561 reg_stat_type *rsp = &VEC_index (reg_stat_type, reg_stat, regno);
13563 if (dead_or_set_p (place, XEXP (note, 0))
13564 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13566 /* Unless the register previously died in PLACE, clear
13567 last_death. [I no longer understand why this is
13568 being done.] */
13569 if (rsp->last_death != place)
13570 rsp->last_death = 0;
13571 place = 0;
13573 else
13574 rsp->last_death = place;
13576 /* If this is a death note for a hard reg that is occupying
13577 multiple registers, ensure that we are still using all
13578 parts of the object. If we find a piece of the object
13579 that is unused, we must arrange for an appropriate REG_DEAD
13580 note to be added for it. However, we can't just emit a USE
13581 and tag the note to it, since the register might actually
13582 be dead; so we recourse, and the recursive call then finds
13583 the previous insn that used this register. */
13585 if (place && regno < FIRST_PSEUDO_REGISTER
13586 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13588 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13589 int all_used = 1;
13590 unsigned int i;
13592 for (i = regno; i < endregno; i++)
13593 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13594 && ! find_regno_fusage (place, USE, i))
13595 || dead_or_set_regno_p (place, i))
13596 all_used = 0;
13598 if (! all_used)
13600 /* Put only REG_DEAD notes for pieces that are
13601 not already dead or set. */
13603 for (i = regno; i < endregno;
13604 i += hard_regno_nregs[i][reg_raw_mode[i]])
13606 rtx piece = regno_reg_rtx[i];
13607 basic_block bb = this_basic_block;
13609 if (! dead_or_set_p (place, piece)
13610 && ! reg_bitfield_target_p (piece,
13611 PATTERN (place)))
13613 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13614 NULL_RTX);
13616 distribute_notes (new_note, place, place,
13617 NULL_RTX, NULL_RTX, NULL_RTX,
13618 NULL_RTX);
13620 else if (! refers_to_regno_p (i, i + 1,
13621 PATTERN (place), 0)
13622 && ! find_regno_fusage (place, USE, i))
13623 for (tem = PREV_INSN (place); ;
13624 tem = PREV_INSN (tem))
13626 if (!NONDEBUG_INSN_P (tem))
13628 if (tem == BB_HEAD (bb))
13629 break;
13630 continue;
13632 if (dead_or_set_p (tem, piece)
13633 || reg_bitfield_target_p (piece,
13634 PATTERN (tem)))
13636 add_reg_note (tem, REG_UNUSED, piece);
13637 break;
13643 place = 0;
13647 break;
13649 default:
13650 /* Any other notes should not be present at this point in the
13651 compilation. */
13652 gcc_unreachable ();
13655 if (place)
13657 XEXP (note, 1) = REG_NOTES (place);
13658 REG_NOTES (place) = note;
13661 if (place2)
13662 add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13666 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13667 I3, I2, and I1 to new locations. This is also called to add a link
13668 pointing at I3 when I3's destination is changed. */
13670 static void
13671 distribute_links (struct insn_link *links)
13673 struct insn_link *link, *next_link;
13675 for (link = links; link; link = next_link)
13677 rtx place = 0;
13678 rtx insn;
13679 rtx set, reg;
13681 next_link = link->next;
13683 /* If the insn that this link points to is a NOTE or isn't a single
13684 set, ignore it. In the latter case, it isn't clear what we
13685 can do other than ignore the link, since we can't tell which
13686 register it was for. Such links wouldn't be used by combine
13687 anyway.
13689 It is not possible for the destination of the target of the link to
13690 have been changed by combine. The only potential of this is if we
13691 replace I3, I2, and I1 by I3 and I2. But in that case the
13692 destination of I2 also remains unchanged. */
13694 if (NOTE_P (link->insn)
13695 || (set = single_set (link->insn)) == 0)
13696 continue;
13698 reg = SET_DEST (set);
13699 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13700 || GET_CODE (reg) == STRICT_LOW_PART)
13701 reg = XEXP (reg, 0);
13703 /* A LOG_LINK is defined as being placed on the first insn that uses
13704 a register and points to the insn that sets the register. Start
13705 searching at the next insn after the target of the link and stop
13706 when we reach a set of the register or the end of the basic block.
13708 Note that this correctly handles the link that used to point from
13709 I3 to I2. Also note that not much searching is typically done here
13710 since most links don't point very far away. */
13712 for (insn = NEXT_INSN (link->insn);
13713 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13714 || BB_HEAD (this_basic_block->next_bb) != insn));
13715 insn = NEXT_INSN (insn))
13716 if (DEBUG_INSN_P (insn))
13717 continue;
13718 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13720 if (reg_referenced_p (reg, PATTERN (insn)))
13721 place = insn;
13722 break;
13724 else if (CALL_P (insn)
13725 && find_reg_fusage (insn, USE, reg))
13727 place = insn;
13728 break;
13730 else if (INSN_P (insn) && reg_set_p (reg, insn))
13731 break;
13733 /* If we found a place to put the link, place it there unless there
13734 is already a link to the same insn as LINK at that point. */
13736 if (place)
13738 struct insn_link *link2;
13740 FOR_EACH_LOG_LINK (link2, place)
13741 if (link2->insn == link->insn)
13742 break;
13744 if (link2 == NULL)
13746 link->next = LOG_LINKS (place);
13747 LOG_LINKS (place) = link;
13749 /* Set added_links_insn to the earliest insn we added a
13750 link to. */
13751 if (added_links_insn == 0
13752 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13753 added_links_insn = place;
13759 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13760 Check whether the expression pointer to by LOC is a register or
13761 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13762 Otherwise return zero. */
13764 static int
13765 unmentioned_reg_p_1 (rtx *loc, void *expr)
13767 rtx x = *loc;
13769 if (x != NULL_RTX
13770 && (REG_P (x) || MEM_P (x))
13771 && ! reg_mentioned_p (x, (rtx) expr))
13772 return 1;
13773 return 0;
13776 /* Check for any register or memory mentioned in EQUIV that is not
13777 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13778 of EXPR where some registers may have been replaced by constants. */
13780 static bool
13781 unmentioned_reg_p (rtx equiv, rtx expr)
13783 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13786 DEBUG_FUNCTION void
13787 dump_combine_stats (FILE *file)
13789 fprintf
13790 (file,
13791 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13792 combine_attempts, combine_merges, combine_extras, combine_successes);
13795 void
13796 dump_combine_total_stats (FILE *file)
13798 fprintf
13799 (file,
13800 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13801 total_attempts, total_merges, total_extras, total_successes);
13804 static bool
13805 gate_handle_combine (void)
13807 return (optimize > 0);
13810 /* Try combining insns through substitution. */
13811 static unsigned int
13812 rest_of_handle_combine (void)
13814 int rebuild_jump_labels_after_combine;
13816 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13817 df_note_add_problem ();
13818 df_analyze ();
13820 regstat_init_n_sets_and_refs ();
13822 rebuild_jump_labels_after_combine
13823 = combine_instructions (get_insns (), max_reg_num ());
13825 /* Combining insns may have turned an indirect jump into a
13826 direct jump. Rebuild the JUMP_LABEL fields of jumping
13827 instructions. */
13828 if (rebuild_jump_labels_after_combine)
13830 timevar_push (TV_JUMP);
13831 rebuild_jump_labels (get_insns ());
13832 cleanup_cfg (0);
13833 timevar_pop (TV_JUMP);
13836 regstat_free_n_sets_and_refs ();
13837 return 0;
13840 struct rtl_opt_pass pass_combine =
13843 RTL_PASS,
13844 "combine", /* name */
13845 gate_handle_combine, /* gate */
13846 rest_of_handle_combine, /* execute */
13847 NULL, /* sub */
13848 NULL, /* next */
13849 0, /* static_pass_number */
13850 TV_COMBINE, /* tv_id */
13851 PROP_cfglayout, /* properties_required */
13852 0, /* properties_provided */
13853 0, /* properties_destroyed */
13854 0, /* todo_flags_start */
13855 TODO_df_finish | TODO_verify_rtl_sharing |
13856 TODO_ggc_collect, /* todo_flags_finish */