* config/bfin/bfin.h (TARGET_CPU_CPP_BUILTINS): Define
[official-gcc/alias-decl.git] / gcc / combine.c
blobc794e11857f813df22c4c1bf162958e8975e34ac
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
4 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 "real.h"
96 #include "toplev.h"
97 #include "target.h"
98 #include "optabs.h"
99 #include "insn-codes.h"
100 #include "rtlhooks-def.h"
101 /* Include output.h for dump_file. */
102 #include "output.h"
103 #include "params.h"
104 #include "timevar.h"
105 #include "tree-pass.h"
106 #include "df.h"
108 /* Number of attempts to combine instructions in this function. */
110 static int combine_attempts;
112 /* Number of attempts that got as far as substitution in this function. */
114 static int combine_merges;
116 /* Number of instructions combined with added SETs in this function. */
118 static int combine_extras;
120 /* Number of instructions combined in this function. */
122 static int combine_successes;
124 /* Totals over entire compilation. */
126 static int total_attempts, total_merges, total_extras, total_successes;
128 /* combine_instructions may try to replace the right hand side of the
129 second instruction with the value of an associated REG_EQUAL note
130 before throwing it at try_combine. That is problematic when there
131 is a REG_DEAD note for a register used in the old right hand side
132 and can cause distribute_notes to do wrong things. This is the
133 second instruction if it has been so modified, null otherwise. */
135 static rtx i2mod;
137 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
139 static rtx i2mod_old_rhs;
141 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
143 static rtx i2mod_new_rhs;
145 typedef struct reg_stat_struct {
146 /* Record last point of death of (hard or pseudo) register n. */
147 rtx last_death;
149 /* Record last point of modification of (hard or pseudo) register n. */
150 rtx last_set;
152 /* The next group of fields allows the recording of the last value assigned
153 to (hard or pseudo) register n. We use this information to see if an
154 operation being processed is redundant given a prior operation performed
155 on the register. For example, an `and' with a constant is redundant if
156 all the zero bits are already known to be turned off.
158 We use an approach similar to that used by cse, but change it in the
159 following ways:
161 (1) We do not want to reinitialize at each label.
162 (2) It is useful, but not critical, to know the actual value assigned
163 to a register. Often just its form is helpful.
165 Therefore, we maintain the following fields:
167 last_set_value the last value assigned
168 last_set_label records the value of label_tick when the
169 register was assigned
170 last_set_table_tick records the value of label_tick when a
171 value using the register is assigned
172 last_set_invalid set to nonzero when it is not valid
173 to use the value of this register in some
174 register's value
176 To understand the usage of these tables, it is important to understand
177 the distinction between the value in last_set_value being valid and
178 the register being validly contained in some other expression in the
179 table.
181 (The next two parameters are out of date).
183 reg_stat[i].last_set_value is valid if it is nonzero, and either
184 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186 Register I may validly appear in any expression returned for the value
187 of another register if reg_n_sets[i] is 1. It may also appear in the
188 value for register J if reg_stat[j].last_set_invalid is zero, or
189 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191 If an expression is found in the table containing a register which may
192 not validly appear in an expression, the register is replaced by
193 something that won't match, (clobber (const_int 0)). */
195 /* Record last value assigned to (hard or pseudo) register n. */
197 rtx last_set_value;
199 /* Record the value of label_tick when an expression involving register n
200 is placed in last_set_value. */
202 int last_set_table_tick;
204 /* Record the value of label_tick when the value for register n is placed in
205 last_set_value. */
207 int last_set_label;
209 /* These fields are maintained in parallel with last_set_value and are
210 used to store the mode in which the register was last set, the bits
211 that were known to be zero when it was last set, and the number of
212 sign bits copies it was known to have when it was last set. */
214 unsigned HOST_WIDE_INT last_set_nonzero_bits;
215 char last_set_sign_bit_copies;
216 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
218 /* Set nonzero if references to register n in expressions should not be
219 used. last_set_invalid is set nonzero when this register is being
220 assigned to and last_set_table_tick == label_tick. */
222 char last_set_invalid;
224 /* Some registers that are set more than once and used in more than one
225 basic block are nevertheless always set in similar ways. For example,
226 a QImode register may be loaded from memory in two places on a machine
227 where byte loads zero extend.
229 We record in the following fields if a register has some leading bits
230 that are always equal to the sign bit, and what we know about the
231 nonzero bits of a register, specifically which bits are known to be
232 zero.
234 If an entry is zero, it means that we don't know anything special. */
236 unsigned char sign_bit_copies;
238 unsigned HOST_WIDE_INT nonzero_bits;
240 /* Record the value of the label_tick when the last truncation
241 happened. The field truncated_to_mode is only valid if
242 truncation_label == label_tick. */
244 int truncation_label;
246 /* Record the last truncation seen for this register. If truncation
247 is not a nop to this mode we might be able to save an explicit
248 truncation if we know that value already contains a truncated
249 value. */
251 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
252 } reg_stat_type;
254 DEF_VEC_O(reg_stat_type);
255 DEF_VEC_ALLOC_O(reg_stat_type,heap);
257 static VEC(reg_stat_type,heap) *reg_stat;
259 /* Record the luid of the last insn that invalidated memory
260 (anything that writes memory, and subroutine calls, but not pushes). */
262 static int mem_last_set;
264 /* Record the luid of the last CALL_INSN
265 so we can tell whether a potential combination crosses any calls. */
267 static int last_call_luid;
269 /* When `subst' is called, this is the insn that is being modified
270 (by combining in a previous insn). The PATTERN of this insn
271 is still the old pattern partially modified and it should not be
272 looked at, but this may be used to examine the successors of the insn
273 to judge whether a simplification is valid. */
275 static rtx subst_insn;
277 /* This is the lowest LUID that `subst' is currently dealing with.
278 get_last_value will not return a value if the register was set at or
279 after this LUID. If not for this mechanism, we could get confused if
280 I2 or I1 in try_combine were an insn that used the old value of a register
281 to obtain a new value. In that case, we might erroneously get the
282 new value of the register when we wanted the old one. */
284 static int subst_low_luid;
286 /* This contains any hard registers that are used in newpat; reg_dead_at_p
287 must consider all these registers to be always live. */
289 static HARD_REG_SET newpat_used_regs;
291 /* This is an insn to which a LOG_LINKS entry has been added. If this
292 insn is the earlier than I2 or I3, combine should rescan starting at
293 that location. */
295 static rtx added_links_insn;
297 /* Basic block in which we are performing combines. */
298 static basic_block this_basic_block;
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 an INSN_LIST rtx. */
313 static rtx *uid_log_links;
315 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
316 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
318 /* Incremented for each basic block. */
320 static int label_tick;
322 /* Reset to label_tick for each label. */
324 static int label_tick_ebb_start;
326 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
327 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
329 static enum machine_mode nonzero_bits_mode;
331 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
332 be safely used. It is zero while computing them and after combine has
333 completed. This former test prevents propagating values based on
334 previously set values, which can be incorrect if a variable is modified
335 in a loop. */
337 static int nonzero_sign_valid;
340 /* Record one modification to rtl structure
341 to be undone by storing old_contents into *where. */
343 struct undo
345 struct undo *next;
346 enum { UNDO_RTX, UNDO_INT, UNDO_MODE } kind;
347 union { rtx r; int i; enum machine_mode m; } old_contents;
348 union { rtx *r; int *i; } where;
351 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
352 num_undo says how many are currently recorded.
354 other_insn is nonzero if we have modified some other insn in the process
355 of working on subst_insn. It must be verified too. */
357 struct undobuf
359 struct undo *undos;
360 struct undo *frees;
361 rtx other_insn;
364 static struct undobuf undobuf;
366 /* Number of times the pseudo being substituted for
367 was found and replaced. */
369 static int n_occurrences;
371 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
372 enum machine_mode,
373 unsigned HOST_WIDE_INT,
374 unsigned HOST_WIDE_INT *);
375 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
376 enum machine_mode,
377 unsigned int, unsigned int *);
378 static void do_SUBST (rtx *, rtx);
379 static void do_SUBST_INT (int *, int);
380 static void init_reg_last (void);
381 static void setup_incoming_promotions (rtx);
382 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
383 static int cant_combine_insn_p (rtx);
384 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
385 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
386 static int contains_muldiv (rtx);
387 static rtx try_combine (rtx, rtx, rtx, int *);
388 static void undo_all (void);
389 static void undo_commit (void);
390 static rtx *find_split_point (rtx *, rtx);
391 static rtx subst (rtx, rtx, rtx, int, int);
392 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
393 static rtx simplify_if_then_else (rtx);
394 static rtx simplify_set (rtx);
395 static rtx simplify_logical (rtx);
396 static rtx expand_compound_operation (rtx);
397 static const_rtx expand_field_assignment (const_rtx);
398 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
399 rtx, unsigned HOST_WIDE_INT, int, int, int);
400 static rtx extract_left_shift (rtx, int);
401 static rtx make_compound_operation (rtx, enum rtx_code);
402 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
403 unsigned HOST_WIDE_INT *);
404 static rtx canon_reg_for_combine (rtx, rtx);
405 static rtx force_to_mode (rtx, enum machine_mode,
406 unsigned HOST_WIDE_INT, int);
407 static rtx if_then_else_cond (rtx, rtx *, rtx *);
408 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
409 static int rtx_equal_for_field_assignment_p (rtx, rtx);
410 static rtx make_field_assignment (rtx);
411 static rtx apply_distributive_law (rtx);
412 static rtx distribute_and_simplify_rtx (rtx, int);
413 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
414 unsigned HOST_WIDE_INT);
415 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
416 unsigned HOST_WIDE_INT);
417 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
418 HOST_WIDE_INT, enum machine_mode, int *);
419 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
420 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
421 int);
422 static int recog_for_combine (rtx *, rtx, rtx *);
423 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
424 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
425 static void update_table_tick (rtx);
426 static void record_value_for_reg (rtx, rtx, rtx);
427 static void check_conversions (rtx, rtx);
428 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
429 static void record_dead_and_set_regs (rtx);
430 static int get_last_value_validate (rtx *, rtx, int, int);
431 static rtx get_last_value (const_rtx);
432 static int use_crosses_set_p (const_rtx, int);
433 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
434 static int reg_dead_at_p (rtx, rtx);
435 static void move_deaths (rtx, rtx, int, rtx, rtx *);
436 static int reg_bitfield_target_p (rtx, rtx);
437 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
438 static void distribute_links (rtx);
439 static void mark_used_regs_combine (rtx);
440 static void record_promoted_value (rtx, rtx);
441 static int unmentioned_reg_p_1 (rtx *, void *);
442 static bool unmentioned_reg_p (rtx, rtx);
443 static void record_truncated_value (rtx);
444 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
445 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
448 /* It is not safe to use ordinary gen_lowpart in combine.
449 See comments in gen_lowpart_for_combine. */
450 #undef RTL_HOOKS_GEN_LOWPART
451 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
453 /* Our implementation of gen_lowpart never emits a new pseudo. */
454 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
455 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
457 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
458 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
460 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
461 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
463 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
464 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
466 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
469 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
470 PATTERN can not be split. Otherwise, it returns an insn sequence.
471 This is a wrapper around split_insns which ensures that the
472 reg_stat vector is made larger if the splitter creates a new
473 register. */
475 static rtx
476 combine_split_insns (rtx pattern, rtx insn)
478 rtx ret;
479 unsigned int nregs;
481 ret = split_insns (pattern, insn);
482 nregs = max_reg_num ();
483 if (nregs > VEC_length (reg_stat_type, reg_stat))
484 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
485 return ret;
488 /* This is used by find_single_use to locate an rtx in LOC that
489 contains exactly one use of DEST, which is typically either a REG
490 or CC0. It returns a pointer to the innermost rtx expression
491 containing DEST. Appearances of DEST that are being used to
492 totally replace it are not counted. */
494 static rtx *
495 find_single_use_1 (rtx dest, rtx *loc)
497 rtx x = *loc;
498 enum rtx_code code = GET_CODE (x);
499 rtx *result = NULL;
500 rtx *this_result;
501 int i;
502 const char *fmt;
504 switch (code)
506 case CONST_INT:
507 case CONST:
508 case LABEL_REF:
509 case SYMBOL_REF:
510 case CONST_DOUBLE:
511 case CONST_VECTOR:
512 case CLOBBER:
513 return 0;
515 case SET:
516 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
517 of a REG that occupies all of the REG, the insn uses DEST if
518 it is mentioned in the destination or the source. Otherwise, we
519 need just check the source. */
520 if (GET_CODE (SET_DEST (x)) != CC0
521 && GET_CODE (SET_DEST (x)) != PC
522 && !REG_P (SET_DEST (x))
523 && ! (GET_CODE (SET_DEST (x)) == SUBREG
524 && REG_P (SUBREG_REG (SET_DEST (x)))
525 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
526 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
527 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
528 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
529 break;
531 return find_single_use_1 (dest, &SET_SRC (x));
533 case MEM:
534 case SUBREG:
535 return find_single_use_1 (dest, &XEXP (x, 0));
537 default:
538 break;
541 /* If it wasn't one of the common cases above, check each expression and
542 vector of this code. Look for a unique usage of DEST. */
544 fmt = GET_RTX_FORMAT (code);
545 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
547 if (fmt[i] == 'e')
549 if (dest == XEXP (x, i)
550 || (REG_P (dest) && REG_P (XEXP (x, i))
551 && REGNO (dest) == REGNO (XEXP (x, i))))
552 this_result = loc;
553 else
554 this_result = find_single_use_1 (dest, &XEXP (x, i));
556 if (result == NULL)
557 result = this_result;
558 else if (this_result)
559 /* Duplicate usage. */
560 return NULL;
562 else if (fmt[i] == 'E')
564 int j;
566 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
568 if (XVECEXP (x, i, j) == dest
569 || (REG_P (dest)
570 && REG_P (XVECEXP (x, i, j))
571 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
572 this_result = loc;
573 else
574 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
576 if (result == NULL)
577 result = this_result;
578 else if (this_result)
579 return NULL;
584 return result;
588 /* See if DEST, produced in INSN, is used only a single time in the
589 sequel. If so, return a pointer to the innermost rtx expression in which
590 it is used.
592 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
594 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
595 care about REG_DEAD notes or LOG_LINKS.
597 Otherwise, we find the single use by finding an insn that has a
598 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
599 only referenced once in that insn, we know that it must be the first
600 and last insn referencing DEST. */
602 static rtx *
603 find_single_use (rtx dest, rtx insn, rtx *ploc)
605 rtx next;
606 rtx *result;
607 rtx link;
609 #ifdef HAVE_cc0
610 if (dest == cc0_rtx)
612 next = NEXT_INSN (insn);
613 if (next == 0
614 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
615 return 0;
617 result = find_single_use_1 (dest, &PATTERN (next));
618 if (result && ploc)
619 *ploc = next;
620 return result;
622 #endif
624 if (!REG_P (dest))
625 return 0;
627 for (next = next_nonnote_insn (insn);
628 next != 0 && !LABEL_P (next);
629 next = next_nonnote_insn (next))
630 if (INSN_P (next) && dead_or_set_p (next, dest))
632 for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
633 if (XEXP (link, 0) == insn)
634 break;
636 if (link)
638 result = find_single_use_1 (dest, &PATTERN (next));
639 if (ploc)
640 *ploc = next;
641 return result;
645 return 0;
648 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
649 insn. The substitution can be undone by undo_all. If INTO is already
650 set to NEWVAL, do not record this change. Because computing NEWVAL might
651 also call SUBST, we have to compute it before we put anything into
652 the undo table. */
654 static void
655 do_SUBST (rtx *into, rtx newval)
657 struct undo *buf;
658 rtx oldval = *into;
660 if (oldval == newval)
661 return;
663 /* We'd like to catch as many invalid transformations here as
664 possible. Unfortunately, there are way too many mode changes
665 that are perfectly valid, so we'd waste too much effort for
666 little gain doing the checks here. Focus on catching invalid
667 transformations involving integer constants. */
668 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
669 && GET_CODE (newval) == CONST_INT)
671 /* Sanity check that we're replacing oldval with a CONST_INT
672 that is a valid sign-extension for the original mode. */
673 gcc_assert (INTVAL (newval)
674 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
676 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
677 CONST_INT is not valid, because after the replacement, the
678 original mode would be gone. Unfortunately, we can't tell
679 when do_SUBST is called to replace the operand thereof, so we
680 perform this test on oldval instead, checking whether an
681 invalid replacement took place before we got here. */
682 gcc_assert (!(GET_CODE (oldval) == SUBREG
683 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
684 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
685 && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
688 if (undobuf.frees)
689 buf = undobuf.frees, undobuf.frees = buf->next;
690 else
691 buf = XNEW (struct undo);
693 buf->kind = UNDO_RTX;
694 buf->where.r = into;
695 buf->old_contents.r = oldval;
696 *into = newval;
698 buf->next = undobuf.undos, undobuf.undos = buf;
701 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
703 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
704 for the value of a HOST_WIDE_INT value (including CONST_INT) is
705 not safe. */
707 static void
708 do_SUBST_INT (int *into, int newval)
710 struct undo *buf;
711 int oldval = *into;
713 if (oldval == newval)
714 return;
716 if (undobuf.frees)
717 buf = undobuf.frees, undobuf.frees = buf->next;
718 else
719 buf = XNEW (struct undo);
721 buf->kind = UNDO_INT;
722 buf->where.i = into;
723 buf->old_contents.i = oldval;
724 *into = newval;
726 buf->next = undobuf.undos, undobuf.undos = buf;
729 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
731 /* Similar to SUBST, but just substitute the mode. This is used when
732 changing the mode of a pseudo-register, so that any other
733 references to the entry in the regno_reg_rtx array will change as
734 well. */
736 static void
737 do_SUBST_MODE (rtx *into, enum machine_mode newval)
739 struct undo *buf;
740 enum machine_mode oldval = GET_MODE (*into);
742 if (oldval == newval)
743 return;
745 if (undobuf.frees)
746 buf = undobuf.frees, undobuf.frees = buf->next;
747 else
748 buf = XNEW (struct undo);
750 buf->kind = UNDO_MODE;
751 buf->where.r = into;
752 buf->old_contents.m = oldval;
753 PUT_MODE (*into, newval);
755 buf->next = undobuf.undos, undobuf.undos = buf;
758 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
760 /* Subroutine of try_combine. Determine whether the combine replacement
761 patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
762 insn_rtx_cost that the original instruction sequence I1, I2, I3 and
763 undobuf.other_insn. Note that I1 and/or NEWI2PAT may be NULL_RTX.
764 NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX. This
765 function returns false, if the costs of all instructions can be
766 estimated, and the replacements are more expensive than the original
767 sequence. */
769 static bool
770 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
771 rtx newotherpat)
773 int i1_cost, i2_cost, i3_cost;
774 int new_i2_cost, new_i3_cost;
775 int old_cost, new_cost;
777 /* Lookup the original insn_rtx_costs. */
778 i2_cost = INSN_COST (i2);
779 i3_cost = INSN_COST (i3);
781 if (i1)
783 i1_cost = INSN_COST (i1);
784 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
785 ? i1_cost + i2_cost + i3_cost : 0;
787 else
789 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
790 i1_cost = 0;
793 /* Calculate the replacement insn_rtx_costs. */
794 new_i3_cost = insn_rtx_cost (newpat);
795 if (newi2pat)
797 new_i2_cost = insn_rtx_cost (newi2pat);
798 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
799 ? new_i2_cost + new_i3_cost : 0;
801 else
803 new_cost = new_i3_cost;
804 new_i2_cost = 0;
807 if (undobuf.other_insn)
809 int old_other_cost, new_other_cost;
811 old_other_cost = INSN_COST (undobuf.other_insn);
812 new_other_cost = insn_rtx_cost (newotherpat);
813 if (old_other_cost > 0 && new_other_cost > 0)
815 old_cost += old_other_cost;
816 new_cost += new_other_cost;
818 else
819 old_cost = 0;
822 /* Disallow this recombination if both new_cost and old_cost are
823 greater than zero, and new_cost is greater than old cost. */
824 if (old_cost > 0
825 && new_cost > old_cost)
827 if (dump_file)
829 if (i1)
831 fprintf (dump_file,
832 "rejecting combination of insns %d, %d and %d\n",
833 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
834 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
835 i1_cost, i2_cost, i3_cost, old_cost);
837 else
839 fprintf (dump_file,
840 "rejecting combination of insns %d and %d\n",
841 INSN_UID (i2), INSN_UID (i3));
842 fprintf (dump_file, "original costs %d + %d = %d\n",
843 i2_cost, i3_cost, old_cost);
846 if (newi2pat)
848 fprintf (dump_file, "replacement costs %d + %d = %d\n",
849 new_i2_cost, new_i3_cost, new_cost);
851 else
852 fprintf (dump_file, "replacement cost %d\n", new_cost);
855 return false;
858 /* Update the uid_insn_cost array with the replacement costs. */
859 INSN_COST (i2) = new_i2_cost;
860 INSN_COST (i3) = new_i3_cost;
861 if (i1)
862 INSN_COST (i1) = 0;
864 return true;
868 /* Delete any insns that copy a register to itself. */
870 static void
871 delete_noop_moves (void)
873 rtx insn, next;
874 basic_block bb;
876 FOR_EACH_BB (bb)
878 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
880 next = NEXT_INSN (insn);
881 if (INSN_P (insn) && noop_move_p (insn))
883 rtx note;
885 /* If we're about to remove the first insn of a libcall
886 then move the libcall note to the next real insn and
887 update the retval note. */
888 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
889 && XEXP (note, 0) != insn)
891 rtx new_libcall_insn = next_real_insn (insn);
892 rtx retval_note = find_reg_note (XEXP (note, 0),
893 REG_RETVAL, NULL_RTX);
894 REG_NOTES (new_libcall_insn)
895 = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
896 REG_NOTES (new_libcall_insn));
897 XEXP (retval_note, 0) = new_libcall_insn;
900 if (dump_file)
901 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
903 delete_insn_and_edges (insn);
910 /* Fill in log links field for all insns. */
912 static void
913 create_log_links (void)
915 basic_block bb;
916 rtx *next_use, insn;
917 struct df_ref **def_vec, **use_vec;
919 next_use = XCNEWVEC (rtx, max_reg_num ());
921 /* Pass through each block from the end, recording the uses of each
922 register and establishing log links when def is encountered.
923 Note that we do not clear next_use array in order to save time,
924 so we have to test whether the use is in the same basic block as def.
926 There are a few cases below when we do not consider the definition or
927 usage -- these are taken from original flow.c did. Don't ask me why it is
928 done this way; I don't know and if it works, I don't want to know. */
930 FOR_EACH_BB (bb)
932 FOR_BB_INSNS_REVERSE (bb, insn)
934 if (!INSN_P (insn))
935 continue;
937 /* Log links are created only once. */
938 gcc_assert (!LOG_LINKS (insn));
940 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
942 struct df_ref *def = *def_vec;
943 int regno = DF_REF_REGNO (def);
944 rtx use_insn;
946 if (!next_use[regno])
947 continue;
949 /* Do not consider if it is pre/post modification in MEM. */
950 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
951 continue;
953 /* Do not make the log link for frame pointer. */
954 if ((regno == FRAME_POINTER_REGNUM
955 && (! reload_completed || frame_pointer_needed))
956 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
957 || (regno == HARD_FRAME_POINTER_REGNUM
958 && (! reload_completed || frame_pointer_needed))
959 #endif
960 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
961 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
962 #endif
964 continue;
966 use_insn = next_use[regno];
967 if (BLOCK_FOR_INSN (use_insn) == bb)
969 /* flow.c claimed:
971 We don't build a LOG_LINK for hard registers contained
972 in ASM_OPERANDs. If these registers get replaced,
973 we might wind up changing the semantics of the insn,
974 even if reload can make what appear to be valid
975 assignments later. */
976 if (regno >= FIRST_PSEUDO_REGISTER
977 || asm_noperands (PATTERN (use_insn)) < 0)
978 LOG_LINKS (use_insn) =
979 alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
981 next_use[regno] = NULL_RTX;
984 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
986 struct df_ref *use = *use_vec;
987 int regno = DF_REF_REGNO (use);
989 /* Do not consider the usage of the stack pointer
990 by function call. */
991 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
992 continue;
994 next_use[regno] = insn;
999 free (next_use);
1002 /* Clear LOG_LINKS fields of insns. */
1004 static void
1005 clear_log_links (void)
1007 rtx insn;
1009 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1010 if (INSN_P (insn))
1011 free_INSN_LIST_list (&LOG_LINKS (insn));
1017 /* Main entry point for combiner. F is the first insn of the function.
1018 NREGS is the first unused pseudo-reg number.
1020 Return nonzero if the combiner has turned an indirect jump
1021 instruction into a direct jump. */
1022 static int
1023 combine_instructions (rtx f, unsigned int nregs)
1025 rtx insn, next;
1026 #ifdef HAVE_cc0
1027 rtx prev;
1028 #endif
1029 rtx links, nextlinks;
1030 rtx first;
1032 int new_direct_jump_p = 0;
1034 for (first = f; first && !INSN_P (first); )
1035 first = NEXT_INSN (first);
1036 if (!first)
1037 return 0;
1039 combine_attempts = 0;
1040 combine_merges = 0;
1041 combine_extras = 0;
1042 combine_successes = 0;
1044 rtl_hooks = combine_rtl_hooks;
1046 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1048 init_recog_no_volatile ();
1050 /* Allocate array for insn info. */
1051 max_uid_known = get_max_uid ();
1052 uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1053 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1055 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1057 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1058 problems when, for example, we have j <<= 1 in a loop. */
1060 nonzero_sign_valid = 0;
1062 /* Scan all SETs and see if we can deduce anything about what
1063 bits are known to be zero for some registers and how many copies
1064 of the sign bit are known to exist for those registers.
1066 Also set any known values so that we can use it while searching
1067 for what bits are known to be set. */
1069 label_tick = label_tick_ebb_start = 1;
1071 setup_incoming_promotions (first);
1073 create_log_links ();
1074 FOR_EACH_BB (this_basic_block)
1076 last_call_luid = 0;
1077 mem_last_set = -1;
1078 label_tick++;
1079 FOR_BB_INSNS (this_basic_block, insn)
1080 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1082 subst_low_luid = DF_INSN_LUID (insn);
1083 subst_insn = insn;
1085 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1086 insn);
1087 record_dead_and_set_regs (insn);
1089 #ifdef AUTO_INC_DEC
1090 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1091 if (REG_NOTE_KIND (links) == REG_INC)
1092 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1093 insn);
1094 #endif
1096 /* Record the current insn_rtx_cost of this instruction. */
1097 if (NONJUMP_INSN_P (insn))
1098 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn));
1099 if (dump_file)
1100 fprintf(dump_file, "insn_cost %d: %d\n",
1101 INSN_UID (insn), INSN_COST (insn));
1103 else if (LABEL_P (insn))
1104 label_tick_ebb_start = label_tick;
1107 nonzero_sign_valid = 1;
1109 /* Now scan all the insns in forward order. */
1111 label_tick = label_tick_ebb_start = 1;
1112 init_reg_last ();
1113 setup_incoming_promotions (first);
1115 FOR_EACH_BB (this_basic_block)
1117 last_call_luid = 0;
1118 mem_last_set = -1;
1119 label_tick++;
1120 for (insn = BB_HEAD (this_basic_block);
1121 insn != NEXT_INSN (BB_END (this_basic_block));
1122 insn = next ? next : NEXT_INSN (insn))
1124 next = 0;
1125 if (INSN_P (insn))
1127 /* See if we know about function return values before this
1128 insn based upon SUBREG flags. */
1129 check_conversions (insn, PATTERN (insn));
1131 /* Try this insn with each insn it links back to. */
1133 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1134 if ((next = try_combine (insn, XEXP (links, 0),
1135 NULL_RTX, &new_direct_jump_p)) != 0)
1136 goto retry;
1138 /* Try each sequence of three linked insns ending with this one. */
1140 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1142 rtx link = XEXP (links, 0);
1144 /* If the linked insn has been replaced by a note, then there
1145 is no point in pursuing this chain any further. */
1146 if (NOTE_P (link))
1147 continue;
1149 for (nextlinks = LOG_LINKS (link);
1150 nextlinks;
1151 nextlinks = XEXP (nextlinks, 1))
1152 if ((next = try_combine (insn, link,
1153 XEXP (nextlinks, 0),
1154 &new_direct_jump_p)) != 0)
1155 goto retry;
1158 #ifdef HAVE_cc0
1159 /* Try to combine a jump insn that uses CC0
1160 with a preceding insn that sets CC0, and maybe with its
1161 logical predecessor as well.
1162 This is how we make decrement-and-branch insns.
1163 We need this special code because data flow connections
1164 via CC0 do not get entered in LOG_LINKS. */
1166 if (JUMP_P (insn)
1167 && (prev = prev_nonnote_insn (insn)) != 0
1168 && NONJUMP_INSN_P (prev)
1169 && sets_cc0_p (PATTERN (prev)))
1171 if ((next = try_combine (insn, prev,
1172 NULL_RTX, &new_direct_jump_p)) != 0)
1173 goto retry;
1175 for (nextlinks = LOG_LINKS (prev); nextlinks;
1176 nextlinks = XEXP (nextlinks, 1))
1177 if ((next = try_combine (insn, prev,
1178 XEXP (nextlinks, 0),
1179 &new_direct_jump_p)) != 0)
1180 goto retry;
1183 /* Do the same for an insn that explicitly references CC0. */
1184 if (NONJUMP_INSN_P (insn)
1185 && (prev = prev_nonnote_insn (insn)) != 0
1186 && NONJUMP_INSN_P (prev)
1187 && sets_cc0_p (PATTERN (prev))
1188 && GET_CODE (PATTERN (insn)) == SET
1189 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1191 if ((next = try_combine (insn, prev,
1192 NULL_RTX, &new_direct_jump_p)) != 0)
1193 goto retry;
1195 for (nextlinks = LOG_LINKS (prev); nextlinks;
1196 nextlinks = XEXP (nextlinks, 1))
1197 if ((next = try_combine (insn, prev,
1198 XEXP (nextlinks, 0),
1199 &new_direct_jump_p)) != 0)
1200 goto retry;
1203 /* Finally, see if any of the insns that this insn links to
1204 explicitly references CC0. If so, try this insn, that insn,
1205 and its predecessor if it sets CC0. */
1206 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1207 if (NONJUMP_INSN_P (XEXP (links, 0))
1208 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1209 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1210 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1211 && NONJUMP_INSN_P (prev)
1212 && sets_cc0_p (PATTERN (prev))
1213 && (next = try_combine (insn, XEXP (links, 0),
1214 prev, &new_direct_jump_p)) != 0)
1215 goto retry;
1216 #endif
1218 /* Try combining an insn with two different insns whose results it
1219 uses. */
1220 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1221 for (nextlinks = XEXP (links, 1); nextlinks;
1222 nextlinks = XEXP (nextlinks, 1))
1223 if ((next = try_combine (insn, XEXP (links, 0),
1224 XEXP (nextlinks, 0),
1225 &new_direct_jump_p)) != 0)
1226 goto retry;
1228 /* Try this insn with each REG_EQUAL note it links back to. */
1229 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1231 rtx set, note;
1232 rtx temp = XEXP (links, 0);
1233 if ((set = single_set (temp)) != 0
1234 && (note = find_reg_equal_equiv_note (temp)) != 0
1235 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1236 /* Avoid using a register that may already been marked
1237 dead by an earlier instruction. */
1238 && ! unmentioned_reg_p (note, SET_SRC (set))
1239 && (GET_MODE (note) == VOIDmode
1240 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1241 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1243 /* Temporarily replace the set's source with the
1244 contents of the REG_EQUAL note. The insn will
1245 be deleted or recognized by try_combine. */
1246 rtx orig = SET_SRC (set);
1247 SET_SRC (set) = note;
1248 i2mod = temp;
1249 i2mod_old_rhs = copy_rtx (orig);
1250 i2mod_new_rhs = copy_rtx (note);
1251 next = try_combine (insn, i2mod, NULL_RTX,
1252 &new_direct_jump_p);
1253 i2mod = NULL_RTX;
1254 if (next)
1255 goto retry;
1256 SET_SRC (set) = orig;
1260 if (!NOTE_P (insn))
1261 record_dead_and_set_regs (insn);
1263 retry:
1266 else if (LABEL_P (insn))
1267 label_tick_ebb_start = label_tick;
1271 clear_log_links ();
1272 clear_bb_flags ();
1273 new_direct_jump_p |= purge_all_dead_edges ();
1274 delete_noop_moves ();
1276 /* Clean up. */
1277 free (uid_log_links);
1278 free (uid_insn_cost);
1279 VEC_free (reg_stat_type, heap, reg_stat);
1282 struct undo *undo, *next;
1283 for (undo = undobuf.frees; undo; undo = next)
1285 next = undo->next;
1286 free (undo);
1288 undobuf.frees = 0;
1291 total_attempts += combine_attempts;
1292 total_merges += combine_merges;
1293 total_extras += combine_extras;
1294 total_successes += combine_successes;
1296 nonzero_sign_valid = 0;
1297 rtl_hooks = general_rtl_hooks;
1299 /* Make recognizer allow volatile MEMs again. */
1300 init_recog ();
1302 return new_direct_jump_p;
1305 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1307 static void
1308 init_reg_last (void)
1310 unsigned int i;
1311 reg_stat_type *p;
1313 for (i = 0; VEC_iterate (reg_stat_type, reg_stat, i, p); ++i)
1314 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1317 /* Set up any promoted values for incoming argument registers. */
1319 static void
1320 setup_incoming_promotions (rtx first)
1322 tree arg;
1324 if (!targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
1325 return;
1327 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1328 arg = TREE_CHAIN (arg))
1330 rtx reg = DECL_INCOMING_RTL (arg);
1332 if (!REG_P (reg))
1333 continue;
1335 if (TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
1337 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
1338 int uns = TYPE_UNSIGNED (TREE_TYPE (arg));
1340 mode = promote_mode (TREE_TYPE (arg), mode, &uns, 1);
1341 if (mode == GET_MODE (reg) && mode != DECL_MODE (arg))
1343 rtx x;
1344 x = gen_rtx_CLOBBER (DECL_MODE (arg), const0_rtx);
1345 x = gen_rtx_fmt_e ((uns ? ZERO_EXTEND : SIGN_EXTEND), mode, x);
1346 record_value_for_reg (reg, first, x);
1352 /* Called via note_stores. If X is a pseudo that is narrower than
1353 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1355 If we are setting only a portion of X and we can't figure out what
1356 portion, assume all bits will be used since we don't know what will
1357 be happening.
1359 Similarly, set how many bits of X are known to be copies of the sign bit
1360 at all locations in the function. This is the smallest number implied
1361 by any set of X. */
1363 static void
1364 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1366 rtx insn = (rtx) data;
1367 unsigned int num;
1369 if (REG_P (x)
1370 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1371 /* If this register is undefined at the start of the file, we can't
1372 say what its contents were. */
1373 && ! REGNO_REG_SET_P
1374 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1375 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1377 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1379 if (set == 0 || GET_CODE (set) == CLOBBER)
1381 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1382 rsp->sign_bit_copies = 1;
1383 return;
1386 /* If this register is being initialized using itself, and the
1387 register is uninitialized in this basic block, and there are
1388 no LOG_LINKS which set the register, then part of the
1389 register is uninitialized. In that case we can't assume
1390 anything about the number of nonzero bits.
1392 ??? We could do better if we checked this in
1393 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1394 could avoid making assumptions about the insn which initially
1395 sets the register, while still using the information in other
1396 insns. We would have to be careful to check every insn
1397 involved in the combination. */
1399 if (insn
1400 && reg_referenced_p (x, PATTERN (insn))
1401 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1402 REGNO (x)))
1404 rtx link;
1406 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1408 if (dead_or_set_p (XEXP (link, 0), x))
1409 break;
1411 if (!link)
1413 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1414 rsp->sign_bit_copies = 1;
1415 return;
1419 /* If this is a complex assignment, see if we can convert it into a
1420 simple assignment. */
1421 set = expand_field_assignment (set);
1423 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1424 set what we know about X. */
1426 if (SET_DEST (set) == x
1427 || (GET_CODE (SET_DEST (set)) == SUBREG
1428 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1429 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1430 && SUBREG_REG (SET_DEST (set)) == x))
1432 rtx src = SET_SRC (set);
1434 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1435 /* If X is narrower than a word and SRC is a non-negative
1436 constant that would appear negative in the mode of X,
1437 sign-extend it for use in reg_stat[].nonzero_bits because some
1438 machines (maybe most) will actually do the sign-extension
1439 and this is the conservative approach.
1441 ??? For 2.5, try to tighten up the MD files in this regard
1442 instead of this kludge. */
1444 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1445 && GET_CODE (src) == CONST_INT
1446 && INTVAL (src) > 0
1447 && 0 != (INTVAL (src)
1448 & ((HOST_WIDE_INT) 1
1449 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1450 src = GEN_INT (INTVAL (src)
1451 | ((HOST_WIDE_INT) (-1)
1452 << GET_MODE_BITSIZE (GET_MODE (x))));
1453 #endif
1455 /* Don't call nonzero_bits if it cannot change anything. */
1456 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1457 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1458 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1459 if (rsp->sign_bit_copies == 0
1460 || rsp->sign_bit_copies > num)
1461 rsp->sign_bit_copies = num;
1463 else
1465 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1466 rsp->sign_bit_copies = 1;
1471 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1472 insns that were previously combined into I3 or that will be combined
1473 into the merger of INSN and I3.
1475 Return 0 if the combination is not allowed for any reason.
1477 If the combination is allowed, *PDEST will be set to the single
1478 destination of INSN and *PSRC to the single source, and this function
1479 will return 1. */
1481 static int
1482 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1483 rtx *pdest, rtx *psrc)
1485 int i;
1486 const_rtx set = 0;
1487 rtx src, dest;
1488 rtx p;
1489 #ifdef AUTO_INC_DEC
1490 rtx link;
1491 #endif
1492 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1493 && next_active_insn (succ) == i3)
1494 : next_active_insn (insn) == i3);
1496 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1497 or a PARALLEL consisting of such a SET and CLOBBERs.
1499 If INSN has CLOBBER parallel parts, ignore them for our processing.
1500 By definition, these happen during the execution of the insn. When it
1501 is merged with another insn, all bets are off. If they are, in fact,
1502 needed and aren't also supplied in I3, they may be added by
1503 recog_for_combine. Otherwise, it won't match.
1505 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1506 note.
1508 Get the source and destination of INSN. If more than one, can't
1509 combine. */
1511 if (GET_CODE (PATTERN (insn)) == SET)
1512 set = PATTERN (insn);
1513 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1514 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1516 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1518 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1519 rtx note;
1521 switch (GET_CODE (elt))
1523 /* This is important to combine floating point insns
1524 for the SH4 port. */
1525 case USE:
1526 /* Combining an isolated USE doesn't make sense.
1527 We depend here on combinable_i3pat to reject them. */
1528 /* The code below this loop only verifies that the inputs of
1529 the SET in INSN do not change. We call reg_set_between_p
1530 to verify that the REG in the USE does not change between
1531 I3 and INSN.
1532 If the USE in INSN was for a pseudo register, the matching
1533 insn pattern will likely match any register; combining this
1534 with any other USE would only be safe if we knew that the
1535 used registers have identical values, or if there was
1536 something to tell them apart, e.g. different modes. For
1537 now, we forgo such complicated tests and simply disallow
1538 combining of USES of pseudo registers with any other USE. */
1539 if (REG_P (XEXP (elt, 0))
1540 && GET_CODE (PATTERN (i3)) == PARALLEL)
1542 rtx i3pat = PATTERN (i3);
1543 int i = XVECLEN (i3pat, 0) - 1;
1544 unsigned int regno = REGNO (XEXP (elt, 0));
1548 rtx i3elt = XVECEXP (i3pat, 0, i);
1550 if (GET_CODE (i3elt) == USE
1551 && REG_P (XEXP (i3elt, 0))
1552 && (REGNO (XEXP (i3elt, 0)) == regno
1553 ? reg_set_between_p (XEXP (elt, 0),
1554 PREV_INSN (insn), i3)
1555 : regno >= FIRST_PSEUDO_REGISTER))
1556 return 0;
1558 while (--i >= 0);
1560 break;
1562 /* We can ignore CLOBBERs. */
1563 case CLOBBER:
1564 break;
1566 case SET:
1567 /* Ignore SETs whose result isn't used but not those that
1568 have side-effects. */
1569 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1570 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1571 || INTVAL (XEXP (note, 0)) <= 0)
1572 && ! side_effects_p (elt))
1573 break;
1575 /* If we have already found a SET, this is a second one and
1576 so we cannot combine with this insn. */
1577 if (set)
1578 return 0;
1580 set = elt;
1581 break;
1583 default:
1584 /* Anything else means we can't combine. */
1585 return 0;
1589 if (set == 0
1590 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1591 so don't do anything with it. */
1592 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1593 return 0;
1595 else
1596 return 0;
1598 if (set == 0)
1599 return 0;
1601 set = expand_field_assignment (set);
1602 src = SET_SRC (set), dest = SET_DEST (set);
1604 /* Don't eliminate a store in the stack pointer. */
1605 if (dest == stack_pointer_rtx
1606 /* Don't combine with an insn that sets a register to itself if it has
1607 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1608 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1609 /* Can't merge an ASM_OPERANDS. */
1610 || GET_CODE (src) == ASM_OPERANDS
1611 /* Can't merge a function call. */
1612 || GET_CODE (src) == CALL
1613 /* Don't eliminate a function call argument. */
1614 || (CALL_P (i3)
1615 && (find_reg_fusage (i3, USE, dest)
1616 || (REG_P (dest)
1617 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1618 && global_regs[REGNO (dest)])))
1619 /* Don't substitute into an incremented register. */
1620 || FIND_REG_INC_NOTE (i3, dest)
1621 || (succ && FIND_REG_INC_NOTE (succ, dest))
1622 /* Don't substitute into a non-local goto, this confuses CFG. */
1623 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1624 #if 0
1625 /* Don't combine the end of a libcall into anything. */
1626 /* ??? This gives worse code, and appears to be unnecessary, since no
1627 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1628 use REG_RETVAL notes for noconflict blocks, but other code here
1629 makes sure that those insns don't disappear. */
1630 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1631 #endif
1632 /* Make sure that DEST is not used after SUCC but before I3. */
1633 || (succ && ! all_adjacent
1634 && reg_used_between_p (dest, succ, i3))
1635 /* Make sure that the value that is to be substituted for the register
1636 does not use any registers whose values alter in between. However,
1637 If the insns are adjacent, a use can't cross a set even though we
1638 think it might (this can happen for a sequence of insns each setting
1639 the same destination; last_set of that register might point to
1640 a NOTE). If INSN has a REG_EQUIV note, the register is always
1641 equivalent to the memory so the substitution is valid even if there
1642 are intervening stores. Also, don't move a volatile asm or
1643 UNSPEC_VOLATILE across any other insns. */
1644 || (! all_adjacent
1645 && (((!MEM_P (src)
1646 || ! find_reg_note (insn, REG_EQUIV, src))
1647 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1648 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1649 || GET_CODE (src) == UNSPEC_VOLATILE))
1650 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1651 better register allocation by not doing the combine. */
1652 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1653 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1654 /* Don't combine across a CALL_INSN, because that would possibly
1655 change whether the life span of some REGs crosses calls or not,
1656 and it is a pain to update that information.
1657 Exception: if source is a constant, moving it later can't hurt.
1658 Accept that special case, because it helps -fforce-addr a lot. */
1659 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1660 return 0;
1662 /* DEST must either be a REG or CC0. */
1663 if (REG_P (dest))
1665 /* If register alignment is being enforced for multi-word items in all
1666 cases except for parameters, it is possible to have a register copy
1667 insn referencing a hard register that is not allowed to contain the
1668 mode being copied and which would not be valid as an operand of most
1669 insns. Eliminate this problem by not combining with such an insn.
1671 Also, on some machines we don't want to extend the life of a hard
1672 register. */
1674 if (REG_P (src)
1675 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1676 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1677 /* Don't extend the life of a hard register unless it is
1678 user variable (if we have few registers) or it can't
1679 fit into the desired register (meaning something special
1680 is going on).
1681 Also avoid substituting a return register into I3, because
1682 reload can't handle a conflict with constraints of other
1683 inputs. */
1684 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1685 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1686 return 0;
1688 else if (GET_CODE (dest) != CC0)
1689 return 0;
1692 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1693 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1694 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1696 /* Don't substitute for a register intended as a clobberable
1697 operand. */
1698 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1699 if (rtx_equal_p (reg, dest))
1700 return 0;
1702 /* If the clobber represents an earlyclobber operand, we must not
1703 substitute an expression containing the clobbered register.
1704 As we do not analyze the constraint strings here, we have to
1705 make the conservative assumption. However, if the register is
1706 a fixed hard reg, the clobber cannot represent any operand;
1707 we leave it up to the machine description to either accept or
1708 reject use-and-clobber patterns. */
1709 if (!REG_P (reg)
1710 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1711 || !fixed_regs[REGNO (reg)])
1712 if (reg_overlap_mentioned_p (reg, src))
1713 return 0;
1716 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1717 or not), reject, unless nothing volatile comes between it and I3 */
1719 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1721 /* Make sure succ doesn't contain a volatile reference. */
1722 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1723 return 0;
1725 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1726 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1727 return 0;
1730 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1731 to be an explicit register variable, and was chosen for a reason. */
1733 if (GET_CODE (src) == ASM_OPERANDS
1734 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1735 return 0;
1737 /* If there are any volatile insns between INSN and I3, reject, because
1738 they might affect machine state. */
1740 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1741 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1742 return 0;
1744 /* If INSN contains an autoincrement or autodecrement, make sure that
1745 register is not used between there and I3, and not already used in
1746 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1747 Also insist that I3 not be a jump; if it were one
1748 and the incremented register were spilled, we would lose. */
1750 #ifdef AUTO_INC_DEC
1751 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1752 if (REG_NOTE_KIND (link) == REG_INC
1753 && (JUMP_P (i3)
1754 || reg_used_between_p (XEXP (link, 0), insn, i3)
1755 || (pred != NULL_RTX
1756 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1757 || (succ != NULL_RTX
1758 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1759 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1760 return 0;
1761 #endif
1763 #ifdef HAVE_cc0
1764 /* Don't combine an insn that follows a CC0-setting insn.
1765 An insn that uses CC0 must not be separated from the one that sets it.
1766 We do, however, allow I2 to follow a CC0-setting insn if that insn
1767 is passed as I1; in that case it will be deleted also.
1768 We also allow combining in this case if all the insns are adjacent
1769 because that would leave the two CC0 insns adjacent as well.
1770 It would be more logical to test whether CC0 occurs inside I1 or I2,
1771 but that would be much slower, and this ought to be equivalent. */
1773 p = prev_nonnote_insn (insn);
1774 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1775 && ! all_adjacent)
1776 return 0;
1777 #endif
1779 /* If we get here, we have passed all the tests and the combination is
1780 to be allowed. */
1782 *pdest = dest;
1783 *psrc = src;
1785 return 1;
1788 /* LOC is the location within I3 that contains its pattern or the component
1789 of a PARALLEL of the pattern. We validate that it is valid for combining.
1791 One problem is if I3 modifies its output, as opposed to replacing it
1792 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1793 so would produce an insn that is not equivalent to the original insns.
1795 Consider:
1797 (set (reg:DI 101) (reg:DI 100))
1798 (set (subreg:SI (reg:DI 101) 0) <foo>)
1800 This is NOT equivalent to:
1802 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1803 (set (reg:DI 101) (reg:DI 100))])
1805 Not only does this modify 100 (in which case it might still be valid
1806 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1808 We can also run into a problem if I2 sets a register that I1
1809 uses and I1 gets directly substituted into I3 (not via I2). In that
1810 case, we would be getting the wrong value of I2DEST into I3, so we
1811 must reject the combination. This case occurs when I2 and I1 both
1812 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1813 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1814 of a SET must prevent combination from occurring.
1816 Before doing the above check, we first try to expand a field assignment
1817 into a set of logical operations.
1819 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1820 we place a register that is both set and used within I3. If more than one
1821 such register is detected, we fail.
1823 Return 1 if the combination is valid, zero otherwise. */
1825 static int
1826 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1827 int i1_not_in_src, rtx *pi3dest_killed)
1829 rtx x = *loc;
1831 if (GET_CODE (x) == SET)
1833 rtx set = x ;
1834 rtx dest = SET_DEST (set);
1835 rtx src = SET_SRC (set);
1836 rtx inner_dest = dest;
1837 rtx subdest;
1839 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1840 || GET_CODE (inner_dest) == SUBREG
1841 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1842 inner_dest = XEXP (inner_dest, 0);
1844 /* Check for the case where I3 modifies its output, as discussed
1845 above. We don't want to prevent pseudos from being combined
1846 into the address of a MEM, so only prevent the combination if
1847 i1 or i2 set the same MEM. */
1848 if ((inner_dest != dest &&
1849 (!MEM_P (inner_dest)
1850 || rtx_equal_p (i2dest, inner_dest)
1851 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1852 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1853 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1855 /* This is the same test done in can_combine_p except we can't test
1856 all_adjacent; we don't have to, since this instruction will stay
1857 in place, thus we are not considering increasing the lifetime of
1858 INNER_DEST.
1860 Also, if this insn sets a function argument, combining it with
1861 something that might need a spill could clobber a previous
1862 function argument; the all_adjacent test in can_combine_p also
1863 checks this; here, we do a more specific test for this case. */
1865 || (REG_P (inner_dest)
1866 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1867 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1868 GET_MODE (inner_dest))))
1869 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1870 return 0;
1872 /* If DEST is used in I3, it is being killed in this insn, so
1873 record that for later. We have to consider paradoxical
1874 subregs here, since they kill the whole register, but we
1875 ignore partial subregs, STRICT_LOW_PART, etc.
1876 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1877 STACK_POINTER_REGNUM, since these are always considered to be
1878 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1879 subdest = dest;
1880 if (GET_CODE (subdest) == SUBREG
1881 && (GET_MODE_SIZE (GET_MODE (subdest))
1882 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1883 subdest = SUBREG_REG (subdest);
1884 if (pi3dest_killed
1885 && REG_P (subdest)
1886 && reg_referenced_p (subdest, PATTERN (i3))
1887 && REGNO (subdest) != FRAME_POINTER_REGNUM
1888 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1889 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1890 #endif
1891 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1892 && (REGNO (subdest) != ARG_POINTER_REGNUM
1893 || ! fixed_regs [REGNO (subdest)])
1894 #endif
1895 && REGNO (subdest) != STACK_POINTER_REGNUM)
1897 if (*pi3dest_killed)
1898 return 0;
1900 *pi3dest_killed = subdest;
1904 else if (GET_CODE (x) == PARALLEL)
1906 int i;
1908 for (i = 0; i < XVECLEN (x, 0); i++)
1909 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1910 i1_not_in_src, pi3dest_killed))
1911 return 0;
1914 return 1;
1917 /* Return 1 if X is an arithmetic expression that contains a multiplication
1918 and division. We don't count multiplications by powers of two here. */
1920 static int
1921 contains_muldiv (rtx x)
1923 switch (GET_CODE (x))
1925 case MOD: case DIV: case UMOD: case UDIV:
1926 return 1;
1928 case MULT:
1929 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1930 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1931 default:
1932 if (BINARY_P (x))
1933 return contains_muldiv (XEXP (x, 0))
1934 || contains_muldiv (XEXP (x, 1));
1936 if (UNARY_P (x))
1937 return contains_muldiv (XEXP (x, 0));
1939 return 0;
1943 /* Determine whether INSN can be used in a combination. Return nonzero if
1944 not. This is used in try_combine to detect early some cases where we
1945 can't perform combinations. */
1947 static int
1948 cant_combine_insn_p (rtx insn)
1950 rtx set;
1951 rtx src, dest;
1953 /* If this isn't really an insn, we can't do anything.
1954 This can occur when flow deletes an insn that it has merged into an
1955 auto-increment address. */
1956 if (! INSN_P (insn))
1957 return 1;
1959 /* Never combine loads and stores involving hard regs that are likely
1960 to be spilled. The register allocator can usually handle such
1961 reg-reg moves by tying. If we allow the combiner to make
1962 substitutions of likely-spilled regs, reload might die.
1963 As an exception, we allow combinations involving fixed regs; these are
1964 not available to the register allocator so there's no risk involved. */
1966 set = single_set (insn);
1967 if (! set)
1968 return 0;
1969 src = SET_SRC (set);
1970 dest = SET_DEST (set);
1971 if (GET_CODE (src) == SUBREG)
1972 src = SUBREG_REG (src);
1973 if (GET_CODE (dest) == SUBREG)
1974 dest = SUBREG_REG (dest);
1975 if (REG_P (src) && REG_P (dest)
1976 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1977 && ! fixed_regs[REGNO (src)]
1978 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1979 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1980 && ! fixed_regs[REGNO (dest)]
1981 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1982 return 1;
1984 return 0;
1987 struct likely_spilled_retval_info
1989 unsigned regno, nregs;
1990 unsigned mask;
1993 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
1994 hard registers that are known to be written to / clobbered in full. */
1995 static void
1996 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
1998 struct likely_spilled_retval_info *info = data;
1999 unsigned regno, nregs;
2000 unsigned new_mask;
2002 if (!REG_P (XEXP (set, 0)))
2003 return;
2004 regno = REGNO (x);
2005 if (regno >= info->regno + info->nregs)
2006 return;
2007 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2008 if (regno + nregs <= info->regno)
2009 return;
2010 new_mask = (2U << (nregs - 1)) - 1;
2011 if (regno < info->regno)
2012 new_mask >>= info->regno - regno;
2013 else
2014 new_mask <<= regno - info->regno;
2015 info->mask &= ~new_mask;
2018 /* Return nonzero iff part of the return value is live during INSN, and
2019 it is likely spilled. This can happen when more than one insn is needed
2020 to copy the return value, e.g. when we consider to combine into the
2021 second copy insn for a complex value. */
2023 static int
2024 likely_spilled_retval_p (rtx insn)
2026 rtx use = BB_END (this_basic_block);
2027 rtx reg, p;
2028 unsigned regno, nregs;
2029 /* We assume here that no machine mode needs more than
2030 32 hard registers when the value overlaps with a register
2031 for which FUNCTION_VALUE_REGNO_P is true. */
2032 unsigned mask;
2033 struct likely_spilled_retval_info info;
2035 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2036 return 0;
2037 reg = XEXP (PATTERN (use), 0);
2038 if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
2039 return 0;
2040 regno = REGNO (reg);
2041 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2042 if (nregs == 1)
2043 return 0;
2044 mask = (2U << (nregs - 1)) - 1;
2046 /* Disregard parts of the return value that are set later. */
2047 info.regno = regno;
2048 info.nregs = nregs;
2049 info.mask = mask;
2050 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2051 if (INSN_P (p))
2052 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2053 mask = info.mask;
2055 /* Check if any of the (probably) live return value registers is
2056 likely spilled. */
2057 nregs --;
2060 if ((mask & 1 << nregs)
2061 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2062 return 1;
2063 } while (nregs--);
2064 return 0;
2067 /* Adjust INSN after we made a change to its destination.
2069 Changing the destination can invalidate notes that say something about
2070 the results of the insn and a LOG_LINK pointing to the insn. */
2072 static void
2073 adjust_for_new_dest (rtx insn)
2075 /* For notes, be conservative and simply remove them. */
2076 remove_reg_equal_equiv_notes (insn);
2078 /* The new insn will have a destination that was previously the destination
2079 of an insn just above it. Call distribute_links to make a LOG_LINK from
2080 the next use of that destination. */
2081 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2083 df_insn_rescan (insn);
2086 /* Return TRUE if combine can reuse reg X in mode MODE.
2087 ADDED_SETS is nonzero if the original set is still required. */
2088 static bool
2089 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2091 unsigned int regno;
2093 if (!REG_P(x))
2094 return false;
2096 regno = REGNO (x);
2097 /* Allow hard registers if the new mode is legal, and occupies no more
2098 registers than the old mode. */
2099 if (regno < FIRST_PSEUDO_REGISTER)
2100 return (HARD_REGNO_MODE_OK (regno, mode)
2101 && (hard_regno_nregs[regno][GET_MODE (x)]
2102 >= hard_regno_nregs[regno][mode]));
2104 /* Or a pseudo that is only used once. */
2105 return (REG_N_SETS (regno) == 1 && !added_sets
2106 && !REG_USERVAR_P (x));
2110 /* Check whether X, the destination of a set, refers to part of
2111 the register specified by REG. */
2113 static bool
2114 reg_subword_p (rtx x, rtx reg)
2116 /* Check that reg is an integer mode register. */
2117 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2118 return false;
2120 if (GET_CODE (x) == STRICT_LOW_PART
2121 || GET_CODE (x) == ZERO_EXTRACT)
2122 x = XEXP (x, 0);
2124 return GET_CODE (x) == SUBREG
2125 && SUBREG_REG (x) == reg
2126 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2130 /* Try to combine the insns I1 and I2 into I3.
2131 Here I1 and I2 appear earlier than I3.
2132 I1 can be zero; then we combine just I2 into I3.
2134 If we are combining three insns and the resulting insn is not recognized,
2135 try splitting it into two insns. If that happens, I2 and I3 are retained
2136 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
2137 are pseudo-deleted.
2139 Return 0 if the combination does not work. Then nothing is changed.
2140 If we did the combination, return the insn at which combine should
2141 resume scanning.
2143 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2144 new direct jump instruction. */
2146 static rtx
2147 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2149 /* New patterns for I3 and I2, respectively. */
2150 rtx newpat, newi2pat = 0;
2151 rtvec newpat_vec_with_clobbers = 0;
2152 int substed_i2 = 0, substed_i1 = 0;
2153 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
2154 int added_sets_1, added_sets_2;
2155 /* Total number of SETs to put into I3. */
2156 int total_sets;
2157 /* Nonzero if I2's body now appears in I3. */
2158 int i2_is_used;
2159 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2160 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2161 /* Contains I3 if the destination of I3 is used in its source, which means
2162 that the old life of I3 is being killed. If that usage is placed into
2163 I2 and not in I3, a REG_DEAD note must be made. */
2164 rtx i3dest_killed = 0;
2165 /* SET_DEST and SET_SRC of I2 and I1. */
2166 rtx i2dest, i2src, i1dest = 0, i1src = 0;
2167 /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases. */
2168 rtx i1pat = 0, i2pat = 0;
2169 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2170 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2171 int i2dest_killed = 0, i1dest_killed = 0;
2172 int i1_feeds_i3 = 0;
2173 /* Notes that must be added to REG_NOTES in I3 and I2. */
2174 rtx new_i3_notes, new_i2_notes;
2175 /* Notes that we substituted I3 into I2 instead of the normal case. */
2176 int i3_subst_into_i2 = 0;
2177 /* Notes that I1, I2 or I3 is a MULT operation. */
2178 int have_mult = 0;
2179 int swap_i2i3 = 0;
2181 int maxreg;
2182 rtx temp;
2183 rtx link;
2184 rtx other_pat = 0;
2185 rtx new_other_notes;
2186 int i;
2188 /* Exit early if one of the insns involved can't be used for
2189 combinations. */
2190 if (cant_combine_insn_p (i3)
2191 || cant_combine_insn_p (i2)
2192 || (i1 && cant_combine_insn_p (i1))
2193 || likely_spilled_retval_p (i3)
2194 /* We also can't do anything if I3 has a
2195 REG_LIBCALL note since we don't want to disrupt the contiguity of a
2196 libcall. */
2197 #if 0
2198 /* ??? This gives worse code, and appears to be unnecessary, since no
2199 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
2200 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
2201 #endif
2203 return 0;
2205 combine_attempts++;
2206 undobuf.other_insn = 0;
2208 /* Reset the hard register usage information. */
2209 CLEAR_HARD_REG_SET (newpat_used_regs);
2211 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
2212 code below, set I1 to be the earlier of the two insns. */
2213 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2214 temp = i1, i1 = i2, i2 = temp;
2216 added_links_insn = 0;
2218 /* First check for one important special-case that the code below will
2219 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2220 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2221 we may be able to replace that destination with the destination of I3.
2222 This occurs in the common code where we compute both a quotient and
2223 remainder into a structure, in which case we want to do the computation
2224 directly into the structure to avoid register-register copies.
2226 Note that this case handles both multiple sets in I2 and also
2227 cases where I2 has a number of CLOBBER or PARALLELs.
2229 We make very conservative checks below and only try to handle the
2230 most common cases of this. For example, we only handle the case
2231 where I2 and I3 are adjacent to avoid making difficult register
2232 usage tests. */
2234 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2235 && REG_P (SET_SRC (PATTERN (i3)))
2236 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2237 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2238 && GET_CODE (PATTERN (i2)) == PARALLEL
2239 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2240 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2241 below would need to check what is inside (and reg_overlap_mentioned_p
2242 doesn't support those codes anyway). Don't allow those destinations;
2243 the resulting insn isn't likely to be recognized anyway. */
2244 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2245 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2246 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2247 SET_DEST (PATTERN (i3)))
2248 && next_real_insn (i2) == i3)
2250 rtx p2 = PATTERN (i2);
2252 /* Make sure that the destination of I3,
2253 which we are going to substitute into one output of I2,
2254 is not used within another output of I2. We must avoid making this:
2255 (parallel [(set (mem (reg 69)) ...)
2256 (set (reg 69) ...)])
2257 which is not well-defined as to order of actions.
2258 (Besides, reload can't handle output reloads for this.)
2260 The problem can also happen if the dest of I3 is a memory ref,
2261 if another dest in I2 is an indirect memory ref. */
2262 for (i = 0; i < XVECLEN (p2, 0); i++)
2263 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2264 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2265 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2266 SET_DEST (XVECEXP (p2, 0, i))))
2267 break;
2269 if (i == XVECLEN (p2, 0))
2270 for (i = 0; i < XVECLEN (p2, 0); i++)
2271 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2272 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2273 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2275 combine_merges++;
2277 subst_insn = i3;
2278 subst_low_luid = DF_INSN_LUID (i2);
2280 added_sets_2 = added_sets_1 = 0;
2281 i2dest = SET_SRC (PATTERN (i3));
2282 i2dest_killed = dead_or_set_p (i2, i2dest);
2284 /* Replace the dest in I2 with our dest and make the resulting
2285 insn the new pattern for I3. Then skip to where we
2286 validate the pattern. Everything was set up above. */
2287 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2288 SET_DEST (PATTERN (i3)));
2290 newpat = p2;
2291 i3_subst_into_i2 = 1;
2292 goto validate_replacement;
2296 /* If I2 is setting a pseudo to a constant and I3 is setting some
2297 sub-part of it to another constant, merge them by making a new
2298 constant. */
2299 if (i1 == 0
2300 && (temp = single_set (i2)) != 0
2301 && (GET_CODE (SET_SRC (temp)) == CONST_INT
2302 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2303 && GET_CODE (PATTERN (i3)) == SET
2304 && (GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT
2305 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2306 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2308 rtx dest = SET_DEST (PATTERN (i3));
2309 int offset = -1;
2310 int width = 0;
2312 if (GET_CODE (dest) == ZERO_EXTRACT)
2314 if (GET_CODE (XEXP (dest, 1)) == CONST_INT
2315 && GET_CODE (XEXP (dest, 2)) == CONST_INT)
2317 width = INTVAL (XEXP (dest, 1));
2318 offset = INTVAL (XEXP (dest, 2));
2319 dest = XEXP (dest, 0);
2320 if (BITS_BIG_ENDIAN)
2321 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2324 else
2326 if (GET_CODE (dest) == STRICT_LOW_PART)
2327 dest = XEXP (dest, 0);
2328 width = GET_MODE_BITSIZE (GET_MODE (dest));
2329 offset = 0;
2332 if (offset >= 0)
2334 /* If this is the low part, we're done. */
2335 if (subreg_lowpart_p (dest))
2337 /* Handle the case where inner is twice the size of outer. */
2338 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2339 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2340 offset += GET_MODE_BITSIZE (GET_MODE (dest));
2341 /* Otherwise give up for now. */
2342 else
2343 offset = -1;
2346 if (offset >= 0
2347 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2348 <= HOST_BITS_PER_WIDE_INT * 2))
2350 HOST_WIDE_INT mhi, ohi, ihi;
2351 HOST_WIDE_INT mlo, olo, ilo;
2352 rtx inner = SET_SRC (PATTERN (i3));
2353 rtx outer = SET_SRC (temp);
2355 if (GET_CODE (outer) == CONST_INT)
2357 olo = INTVAL (outer);
2358 ohi = olo < 0 ? -1 : 0;
2360 else
2362 olo = CONST_DOUBLE_LOW (outer);
2363 ohi = CONST_DOUBLE_HIGH (outer);
2366 if (GET_CODE (inner) == CONST_INT)
2368 ilo = INTVAL (inner);
2369 ihi = ilo < 0 ? -1 : 0;
2371 else
2373 ilo = CONST_DOUBLE_LOW (inner);
2374 ihi = CONST_DOUBLE_HIGH (inner);
2377 if (width < HOST_BITS_PER_WIDE_INT)
2379 mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
2380 mhi = 0;
2382 else if (width < HOST_BITS_PER_WIDE_INT * 2)
2384 mhi = ((unsigned HOST_WIDE_INT) 1
2385 << (width - HOST_BITS_PER_WIDE_INT)) - 1;
2386 mlo = -1;
2388 else
2390 mlo = -1;
2391 mhi = -1;
2394 ilo &= mlo;
2395 ihi &= mhi;
2397 if (offset >= HOST_BITS_PER_WIDE_INT)
2399 mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
2400 mlo = 0;
2401 ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
2402 ilo = 0;
2404 else if (offset > 0)
2406 mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
2407 >> (HOST_BITS_PER_WIDE_INT - offset));
2408 mlo = mlo << offset;
2409 ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
2410 >> (HOST_BITS_PER_WIDE_INT - offset));
2411 ilo = ilo << offset;
2414 olo = (olo & ~mlo) | ilo;
2415 ohi = (ohi & ~mhi) | ihi;
2417 combine_merges++;
2418 subst_insn = i3;
2419 subst_low_luid = DF_INSN_LUID (i2);
2420 added_sets_2 = added_sets_1 = 0;
2421 i2dest = SET_DEST (temp);
2422 i2dest_killed = dead_or_set_p (i2, i2dest);
2424 SUBST (SET_SRC (temp),
2425 immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
2427 newpat = PATTERN (i2);
2428 goto validate_replacement;
2432 #ifndef HAVE_cc0
2433 /* If we have no I1 and I2 looks like:
2434 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2435 (set Y OP)])
2436 make up a dummy I1 that is
2437 (set Y OP)
2438 and change I2 to be
2439 (set (reg:CC X) (compare:CC Y (const_int 0)))
2441 (We can ignore any trailing CLOBBERs.)
2443 This undoes a previous combination and allows us to match a branch-and-
2444 decrement insn. */
2446 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2447 && XVECLEN (PATTERN (i2), 0) >= 2
2448 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2449 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2450 == MODE_CC)
2451 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2452 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2453 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2454 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2455 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2456 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2458 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2459 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2460 break;
2462 if (i == 1)
2464 /* We make I1 with the same INSN_UID as I2. This gives it
2465 the same DF_INSN_LUID for value tracking. Our fake I1 will
2466 never appear in the insn stream so giving it the same INSN_UID
2467 as I2 will not cause a problem. */
2469 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2470 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2471 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2473 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2474 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2475 SET_DEST (PATTERN (i1)));
2478 #endif
2480 /* Verify that I2 and I1 are valid for combining. */
2481 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2482 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2484 undo_all ();
2485 return 0;
2488 /* Record whether I2DEST is used in I2SRC and similarly for the other
2489 cases. Knowing this will help in register status updating below. */
2490 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2491 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2492 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2493 i2dest_killed = dead_or_set_p (i2, i2dest);
2494 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2496 /* See if I1 directly feeds into I3. It does if I1DEST is not used
2497 in I2SRC. */
2498 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2500 /* Ensure that I3's pattern can be the destination of combines. */
2501 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2502 i1 && i2dest_in_i1src && i1_feeds_i3,
2503 &i3dest_killed))
2505 undo_all ();
2506 return 0;
2509 /* See if any of the insns is a MULT operation. Unless one is, we will
2510 reject a combination that is, since it must be slower. Be conservative
2511 here. */
2512 if (GET_CODE (i2src) == MULT
2513 || (i1 != 0 && GET_CODE (i1src) == MULT)
2514 || (GET_CODE (PATTERN (i3)) == SET
2515 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2516 have_mult = 1;
2518 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2519 We used to do this EXCEPT in one case: I3 has a post-inc in an
2520 output operand. However, that exception can give rise to insns like
2521 mov r3,(r3)+
2522 which is a famous insn on the PDP-11 where the value of r3 used as the
2523 source was model-dependent. Avoid this sort of thing. */
2525 #if 0
2526 if (!(GET_CODE (PATTERN (i3)) == SET
2527 && REG_P (SET_SRC (PATTERN (i3)))
2528 && MEM_P (SET_DEST (PATTERN (i3)))
2529 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2530 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2531 /* It's not the exception. */
2532 #endif
2533 #ifdef AUTO_INC_DEC
2534 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2535 if (REG_NOTE_KIND (link) == REG_INC
2536 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2537 || (i1 != 0
2538 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2540 undo_all ();
2541 return 0;
2543 #endif
2545 /* See if the SETs in I1 or I2 need to be kept around in the merged
2546 instruction: whenever the value set there is still needed past I3.
2547 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2549 For the SET in I1, we have two cases: If I1 and I2 independently
2550 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2551 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2552 in I1 needs to be kept around unless I1DEST dies or is set in either
2553 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
2554 I1DEST. If so, we know I1 feeds into I2. */
2556 added_sets_2 = ! dead_or_set_p (i3, i2dest);
2558 added_sets_1
2559 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2560 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2562 /* If the set in I2 needs to be kept around, we must make a copy of
2563 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2564 PATTERN (I2), we are only substituting for the original I1DEST, not into
2565 an already-substituted copy. This also prevents making self-referential
2566 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2567 I2DEST. */
2569 if (added_sets_2)
2571 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2572 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2573 else
2574 i2pat = copy_rtx (PATTERN (i2));
2577 if (added_sets_1)
2579 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2580 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2581 else
2582 i1pat = copy_rtx (PATTERN (i1));
2585 combine_merges++;
2587 /* Substitute in the latest insn for the regs set by the earlier ones. */
2589 maxreg = max_reg_num ();
2591 subst_insn = i3;
2593 #ifndef HAVE_cc0
2594 /* Many machines that don't use CC0 have insns that can both perform an
2595 arithmetic operation and set the condition code. These operations will
2596 be represented as a PARALLEL with the first element of the vector
2597 being a COMPARE of an arithmetic operation with the constant zero.
2598 The second element of the vector will set some pseudo to the result
2599 of the same arithmetic operation. If we simplify the COMPARE, we won't
2600 match such a pattern and so will generate an extra insn. Here we test
2601 for this case, where both the comparison and the operation result are
2602 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2603 I2SRC. Later we will make the PARALLEL that contains I2. */
2605 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2606 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2607 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2608 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2610 #ifdef SELECT_CC_MODE
2611 rtx *cc_use;
2612 enum machine_mode compare_mode;
2613 #endif
2615 newpat = PATTERN (i3);
2616 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2618 i2_is_used = 1;
2620 #ifdef SELECT_CC_MODE
2621 /* See if a COMPARE with the operand we substituted in should be done
2622 with the mode that is currently being used. If not, do the same
2623 processing we do in `subst' for a SET; namely, if the destination
2624 is used only once, try to replace it with a register of the proper
2625 mode and also replace the COMPARE. */
2626 if (undobuf.other_insn == 0
2627 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2628 &undobuf.other_insn))
2629 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2630 i2src, const0_rtx))
2631 != GET_MODE (SET_DEST (newpat))))
2633 if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2634 compare_mode))
2636 unsigned int regno = REGNO (SET_DEST (newpat));
2637 rtx new_dest;
2639 if (regno < FIRST_PSEUDO_REGISTER)
2640 new_dest = gen_rtx_REG (compare_mode, regno);
2641 else
2643 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2644 new_dest = regno_reg_rtx[regno];
2647 SUBST (SET_DEST (newpat), new_dest);
2648 SUBST (XEXP (*cc_use, 0), new_dest);
2649 SUBST (SET_SRC (newpat),
2650 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2652 else
2653 undobuf.other_insn = 0;
2655 #endif
2657 else
2658 #endif
2660 /* It is possible that the source of I2 or I1 may be performing
2661 an unneeded operation, such as a ZERO_EXTEND of something
2662 that is known to have the high part zero. Handle that case
2663 by letting subst look at the innermost one of them.
2665 Another way to do this would be to have a function that tries
2666 to simplify a single insn instead of merging two or more
2667 insns. We don't do this because of the potential of infinite
2668 loops and because of the potential extra memory required.
2669 However, doing it the way we are is a bit of a kludge and
2670 doesn't catch all cases.
2672 But only do this if -fexpensive-optimizations since it slows
2673 things down and doesn't usually win.
2675 This is not done in the COMPARE case above because the
2676 unmodified I2PAT is used in the PARALLEL and so a pattern
2677 with a modified I2SRC would not match. */
2679 if (flag_expensive_optimizations)
2681 /* Pass pc_rtx so no substitutions are done, just
2682 simplifications. */
2683 if (i1)
2685 subst_low_luid = DF_INSN_LUID (i1);
2686 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2688 else
2690 subst_low_luid = DF_INSN_LUID (i2);
2691 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2695 n_occurrences = 0; /* `subst' counts here */
2697 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2698 need to make a unique copy of I2SRC each time we substitute it
2699 to avoid self-referential rtl. */
2701 subst_low_luid = DF_INSN_LUID (i2);
2702 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2703 ! i1_feeds_i3 && i1dest_in_i1src);
2704 substed_i2 = 1;
2706 /* Record whether i2's body now appears within i3's body. */
2707 i2_is_used = n_occurrences;
2710 /* If we already got a failure, don't try to do more. Otherwise,
2711 try to substitute in I1 if we have it. */
2713 if (i1 && GET_CODE (newpat) != CLOBBER)
2715 /* Before we can do this substitution, we must redo the test done
2716 above (see detailed comments there) that ensures that I1DEST
2717 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2719 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2720 0, (rtx*) 0))
2722 undo_all ();
2723 return 0;
2726 n_occurrences = 0;
2727 subst_low_luid = DF_INSN_LUID (i1);
2728 newpat = subst (newpat, i1dest, i1src, 0, 0);
2729 substed_i1 = 1;
2732 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2733 to count all the ways that I2SRC and I1SRC can be used. */
2734 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2735 && i2_is_used + added_sets_2 > 1)
2736 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2737 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2738 > 1))
2739 /* Fail if we tried to make a new register. */
2740 || max_reg_num () != maxreg
2741 /* Fail if we couldn't do something and have a CLOBBER. */
2742 || GET_CODE (newpat) == CLOBBER
2743 /* Fail if this new pattern is a MULT and we didn't have one before
2744 at the outer level. */
2745 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2746 && ! have_mult))
2748 undo_all ();
2749 return 0;
2752 /* If the actions of the earlier insns must be kept
2753 in addition to substituting them into the latest one,
2754 we must make a new PARALLEL for the latest insn
2755 to hold additional the SETs. */
2757 if (added_sets_1 || added_sets_2)
2759 combine_extras++;
2761 if (GET_CODE (newpat) == PARALLEL)
2763 rtvec old = XVEC (newpat, 0);
2764 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2765 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2766 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2767 sizeof (old->elem[0]) * old->num_elem);
2769 else
2771 rtx old = newpat;
2772 total_sets = 1 + added_sets_1 + added_sets_2;
2773 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2774 XVECEXP (newpat, 0, 0) = old;
2777 if (added_sets_1)
2778 XVECEXP (newpat, 0, --total_sets) = i1pat;
2780 if (added_sets_2)
2782 /* If there is no I1, use I2's body as is. We used to also not do
2783 the subst call below if I2 was substituted into I3,
2784 but that could lose a simplification. */
2785 if (i1 == 0)
2786 XVECEXP (newpat, 0, --total_sets) = i2pat;
2787 else
2788 /* See comment where i2pat is assigned. */
2789 XVECEXP (newpat, 0, --total_sets)
2790 = subst (i2pat, i1dest, i1src, 0, 0);
2794 /* We come here when we are replacing a destination in I2 with the
2795 destination of I3. */
2796 validate_replacement:
2798 /* Note which hard regs this insn has as inputs. */
2799 mark_used_regs_combine (newpat);
2801 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2802 consider splitting this pattern, we might need these clobbers. */
2803 if (i1 && GET_CODE (newpat) == PARALLEL
2804 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2806 int len = XVECLEN (newpat, 0);
2808 newpat_vec_with_clobbers = rtvec_alloc (len);
2809 for (i = 0; i < len; i++)
2810 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2813 /* Is the result of combination a valid instruction? */
2814 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2816 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2817 the second SET's destination is a register that is unused and isn't
2818 marked as an instruction that might trap in an EH region. In that case,
2819 we just need the first SET. This can occur when simplifying a divmod
2820 insn. We *must* test for this case here because the code below that
2821 splits two independent SETs doesn't handle this case correctly when it
2822 updates the register status.
2824 It's pointless doing this if we originally had two sets, one from
2825 i3, and one from i2. Combining then splitting the parallel results
2826 in the original i2 again plus an invalid insn (which we delete).
2827 The net effect is only to move instructions around, which makes
2828 debug info less accurate.
2830 Also check the case where the first SET's destination is unused.
2831 That would not cause incorrect code, but does cause an unneeded
2832 insn to remain. */
2834 if (insn_code_number < 0
2835 && !(added_sets_2 && i1 == 0)
2836 && GET_CODE (newpat) == PARALLEL
2837 && XVECLEN (newpat, 0) == 2
2838 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2839 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2840 && asm_noperands (newpat) < 0)
2842 rtx set0 = XVECEXP (newpat, 0, 0);
2843 rtx set1 = XVECEXP (newpat, 0, 1);
2844 rtx note;
2846 if (((REG_P (SET_DEST (set1))
2847 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2848 || (GET_CODE (SET_DEST (set1)) == SUBREG
2849 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2850 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2851 || INTVAL (XEXP (note, 0)) <= 0)
2852 && ! side_effects_p (SET_SRC (set1)))
2854 newpat = set0;
2855 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2858 else if (((REG_P (SET_DEST (set0))
2859 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2860 || (GET_CODE (SET_DEST (set0)) == SUBREG
2861 && find_reg_note (i3, REG_UNUSED,
2862 SUBREG_REG (SET_DEST (set0)))))
2863 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2864 || INTVAL (XEXP (note, 0)) <= 0)
2865 && ! side_effects_p (SET_SRC (set0)))
2867 newpat = set1;
2868 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2870 if (insn_code_number >= 0)
2872 /* If we will be able to accept this, we have made a
2873 change to the destination of I3. This requires us to
2874 do a few adjustments. */
2876 PATTERN (i3) = newpat;
2877 adjust_for_new_dest (i3);
2882 /* If we were combining three insns and the result is a simple SET
2883 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2884 insns. There are two ways to do this. It can be split using a
2885 machine-specific method (like when you have an addition of a large
2886 constant) or by combine in the function find_split_point. */
2888 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2889 && asm_noperands (newpat) < 0)
2891 rtx parallel, m_split, *split;
2893 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2894 use I2DEST as a scratch register will help. In the latter case,
2895 convert I2DEST to the mode of the source of NEWPAT if we can. */
2897 m_split = combine_split_insns (newpat, i3);
2899 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2900 inputs of NEWPAT. */
2902 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2903 possible to try that as a scratch reg. This would require adding
2904 more code to make it work though. */
2906 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
2908 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2910 /* First try to split using the original register as a
2911 scratch register. */
2912 parallel = gen_rtx_PARALLEL (VOIDmode,
2913 gen_rtvec (2, newpat,
2914 gen_rtx_CLOBBER (VOIDmode,
2915 i2dest)));
2916 m_split = combine_split_insns (parallel, i3);
2918 /* If that didn't work, try changing the mode of I2DEST if
2919 we can. */
2920 if (m_split == 0
2921 && new_mode != GET_MODE (i2dest)
2922 && new_mode != VOIDmode
2923 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2925 enum machine_mode old_mode = GET_MODE (i2dest);
2926 rtx ni2dest;
2928 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2929 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
2930 else
2932 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
2933 ni2dest = regno_reg_rtx[REGNO (i2dest)];
2936 parallel = (gen_rtx_PARALLEL
2937 (VOIDmode,
2938 gen_rtvec (2, newpat,
2939 gen_rtx_CLOBBER (VOIDmode,
2940 ni2dest))));
2941 m_split = combine_split_insns (parallel, i3);
2943 if (m_split == 0
2944 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2946 struct undo *buf;
2948 PUT_MODE (regno_reg_rtx[REGNO (i2dest)], old_mode);
2949 buf = undobuf.undos;
2950 undobuf.undos = buf->next;
2951 buf->next = undobuf.frees;
2952 undobuf.frees = buf;
2957 /* If recog_for_combine has discarded clobbers, try to use them
2958 again for the split. */
2959 if (m_split == 0 && newpat_vec_with_clobbers)
2961 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
2962 m_split = combine_split_insns (parallel, i3);
2965 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2967 m_split = PATTERN (m_split);
2968 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2969 if (insn_code_number >= 0)
2970 newpat = m_split;
2972 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2973 && (next_real_insn (i2) == i3
2974 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
2976 rtx i2set, i3set;
2977 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2978 newi2pat = PATTERN (m_split);
2980 i3set = single_set (NEXT_INSN (m_split));
2981 i2set = single_set (m_split);
2983 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2985 /* If I2 or I3 has multiple SETs, we won't know how to track
2986 register status, so don't use these insns. If I2's destination
2987 is used between I2 and I3, we also can't use these insns. */
2989 if (i2_code_number >= 0 && i2set && i3set
2990 && (next_real_insn (i2) == i3
2991 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2992 insn_code_number = recog_for_combine (&newi3pat, i3,
2993 &new_i3_notes);
2994 if (insn_code_number >= 0)
2995 newpat = newi3pat;
2997 /* It is possible that both insns now set the destination of I3.
2998 If so, we must show an extra use of it. */
3000 if (insn_code_number >= 0)
3002 rtx new_i3_dest = SET_DEST (i3set);
3003 rtx new_i2_dest = SET_DEST (i2set);
3005 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3006 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3007 || GET_CODE (new_i3_dest) == SUBREG)
3008 new_i3_dest = XEXP (new_i3_dest, 0);
3010 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3011 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3012 || GET_CODE (new_i2_dest) == SUBREG)
3013 new_i2_dest = XEXP (new_i2_dest, 0);
3015 if (REG_P (new_i3_dest)
3016 && REG_P (new_i2_dest)
3017 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3018 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3022 /* If we can split it and use I2DEST, go ahead and see if that
3023 helps things be recognized. Verify that none of the registers
3024 are set between I2 and I3. */
3025 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
3026 #ifdef HAVE_cc0
3027 && REG_P (i2dest)
3028 #endif
3029 /* We need I2DEST in the proper mode. If it is a hard register
3030 or the only use of a pseudo, we can change its mode.
3031 Make sure we don't change a hard register to have a mode that
3032 isn't valid for it, or change the number of registers. */
3033 && (GET_MODE (*split) == GET_MODE (i2dest)
3034 || GET_MODE (*split) == VOIDmode
3035 || can_change_dest_mode (i2dest, added_sets_2,
3036 GET_MODE (*split)))
3037 && (next_real_insn (i2) == i3
3038 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3039 /* We can't overwrite I2DEST if its value is still used by
3040 NEWPAT. */
3041 && ! reg_referenced_p (i2dest, newpat))
3043 rtx newdest = i2dest;
3044 enum rtx_code split_code = GET_CODE (*split);
3045 enum machine_mode split_mode = GET_MODE (*split);
3046 bool subst_done = false;
3047 newi2pat = NULL_RTX;
3049 /* Get NEWDEST as a register in the proper mode. We have already
3050 validated that we can do this. */
3051 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3053 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3054 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3055 else
3057 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3058 newdest = regno_reg_rtx[REGNO (i2dest)];
3062 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3063 an ASHIFT. This can occur if it was inside a PLUS and hence
3064 appeared to be a memory address. This is a kludge. */
3065 if (split_code == MULT
3066 && GET_CODE (XEXP (*split, 1)) == CONST_INT
3067 && INTVAL (XEXP (*split, 1)) > 0
3068 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3070 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3071 XEXP (*split, 0), GEN_INT (i)));
3072 /* Update split_code because we may not have a multiply
3073 anymore. */
3074 split_code = GET_CODE (*split);
3077 #ifdef INSN_SCHEDULING
3078 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3079 be written as a ZERO_EXTEND. */
3080 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3082 #ifdef LOAD_EXTEND_OP
3083 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3084 what it really is. */
3085 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3086 == SIGN_EXTEND)
3087 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3088 SUBREG_REG (*split)));
3089 else
3090 #endif
3091 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3092 SUBREG_REG (*split)));
3094 #endif
3096 /* Attempt to split binary operators using arithmetic identities. */
3097 if (BINARY_P (SET_SRC (newpat))
3098 && split_mode == GET_MODE (SET_SRC (newpat))
3099 && ! side_effects_p (SET_SRC (newpat)))
3101 rtx setsrc = SET_SRC (newpat);
3102 enum machine_mode mode = GET_MODE (setsrc);
3103 enum rtx_code code = GET_CODE (setsrc);
3104 rtx src_op0 = XEXP (setsrc, 0);
3105 rtx src_op1 = XEXP (setsrc, 1);
3107 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3108 if (rtx_equal_p (src_op0, src_op1))
3110 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3111 SUBST (XEXP (setsrc, 0), newdest);
3112 SUBST (XEXP (setsrc, 1), newdest);
3113 subst_done = true;
3115 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3116 else if ((code == PLUS || code == MULT)
3117 && GET_CODE (src_op0) == code
3118 && GET_CODE (XEXP (src_op0, 0)) == code
3119 && (INTEGRAL_MODE_P (mode)
3120 || (FLOAT_MODE_P (mode)
3121 && flag_unsafe_math_optimizations)))
3123 rtx p = XEXP (XEXP (src_op0, 0), 0);
3124 rtx q = XEXP (XEXP (src_op0, 0), 1);
3125 rtx r = XEXP (src_op0, 1);
3126 rtx s = src_op1;
3128 /* Split both "((X op Y) op X) op Y" and
3129 "((X op Y) op Y) op X" as "T op T" where T is
3130 "X op Y". */
3131 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3132 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3134 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3135 XEXP (src_op0, 0));
3136 SUBST (XEXP (setsrc, 0), newdest);
3137 SUBST (XEXP (setsrc, 1), newdest);
3138 subst_done = true;
3140 /* Split "((X op X) op Y) op Y)" as "T op T" where
3141 T is "X op Y". */
3142 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3144 rtx tmp = simplify_gen_binary (code, mode, p, r);
3145 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3146 SUBST (XEXP (setsrc, 0), newdest);
3147 SUBST (XEXP (setsrc, 1), newdest);
3148 subst_done = true;
3153 if (!subst_done)
3155 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3156 SUBST (*split, newdest);
3159 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3161 /* recog_for_combine might have added CLOBBERs to newi2pat.
3162 Make sure NEWPAT does not depend on the clobbered regs. */
3163 if (GET_CODE (newi2pat) == PARALLEL)
3164 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3165 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3167 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3168 if (reg_overlap_mentioned_p (reg, newpat))
3170 undo_all ();
3171 return 0;
3175 /* If the split point was a MULT and we didn't have one before,
3176 don't use one now. */
3177 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3178 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3182 /* Check for a case where we loaded from memory in a narrow mode and
3183 then sign extended it, but we need both registers. In that case,
3184 we have a PARALLEL with both loads from the same memory location.
3185 We can split this into a load from memory followed by a register-register
3186 copy. This saves at least one insn, more if register allocation can
3187 eliminate the copy.
3189 We cannot do this if the destination of the first assignment is a
3190 condition code register or cc0. We eliminate this case by making sure
3191 the SET_DEST and SET_SRC have the same mode.
3193 We cannot do this if the destination of the second assignment is
3194 a register that we have already assumed is zero-extended. Similarly
3195 for a SUBREG of such a register. */
3197 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3198 && GET_CODE (newpat) == PARALLEL
3199 && XVECLEN (newpat, 0) == 2
3200 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3201 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3202 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3203 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3204 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3205 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3206 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3207 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3208 DF_INSN_LUID (i2))
3209 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3210 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3211 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3212 (REG_P (temp)
3213 && VEC_index (reg_stat_type, reg_stat,
3214 REGNO (temp))->nonzero_bits != 0
3215 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3216 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3217 && (VEC_index (reg_stat_type, reg_stat,
3218 REGNO (temp))->nonzero_bits
3219 != GET_MODE_MASK (word_mode))))
3220 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3221 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3222 (REG_P (temp)
3223 && VEC_index (reg_stat_type, reg_stat,
3224 REGNO (temp))->nonzero_bits != 0
3225 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3226 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3227 && (VEC_index (reg_stat_type, reg_stat,
3228 REGNO (temp))->nonzero_bits
3229 != GET_MODE_MASK (word_mode)))))
3230 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3231 SET_SRC (XVECEXP (newpat, 0, 1)))
3232 && ! find_reg_note (i3, REG_UNUSED,
3233 SET_DEST (XVECEXP (newpat, 0, 0))))
3235 rtx ni2dest;
3237 newi2pat = XVECEXP (newpat, 0, 0);
3238 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3239 newpat = XVECEXP (newpat, 0, 1);
3240 SUBST (SET_SRC (newpat),
3241 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3242 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3244 if (i2_code_number >= 0)
3245 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3247 if (insn_code_number >= 0)
3248 swap_i2i3 = 1;
3251 /* Similarly, check for a case where we have a PARALLEL of two independent
3252 SETs but we started with three insns. In this case, we can do the sets
3253 as two separate insns. This case occurs when some SET allows two
3254 other insns to combine, but the destination of that SET is still live. */
3256 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3257 && GET_CODE (newpat) == PARALLEL
3258 && XVECLEN (newpat, 0) == 2
3259 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3260 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3261 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3262 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3263 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3264 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3265 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3266 DF_INSN_LUID (i2))
3267 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3268 XVECEXP (newpat, 0, 0))
3269 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3270 XVECEXP (newpat, 0, 1))
3271 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3272 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3273 #ifdef HAVE_cc0
3274 /* We cannot split the parallel into two sets if both sets
3275 reference cc0. */
3276 && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3277 && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3278 #endif
3281 /* Normally, it doesn't matter which of the two is done first,
3282 but it does if one references cc0. In that case, it has to
3283 be first. */
3284 #ifdef HAVE_cc0
3285 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3287 newi2pat = XVECEXP (newpat, 0, 0);
3288 newpat = XVECEXP (newpat, 0, 1);
3290 else
3291 #endif
3293 newi2pat = XVECEXP (newpat, 0, 1);
3294 newpat = XVECEXP (newpat, 0, 0);
3297 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3299 if (i2_code_number >= 0)
3300 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3303 /* If it still isn't recognized, fail and change things back the way they
3304 were. */
3305 if ((insn_code_number < 0
3306 /* Is the result a reasonable ASM_OPERANDS? */
3307 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3309 undo_all ();
3310 return 0;
3313 /* If we had to change another insn, make sure it is valid also. */
3314 if (undobuf.other_insn)
3316 CLEAR_HARD_REG_SET (newpat_used_regs);
3318 other_pat = PATTERN (undobuf.other_insn);
3319 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3320 &new_other_notes);
3322 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3324 undo_all ();
3325 return 0;
3329 #ifdef HAVE_cc0
3330 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3331 they are adjacent to each other or not. */
3333 rtx p = prev_nonnote_insn (i3);
3334 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3335 && sets_cc0_p (newi2pat))
3337 undo_all ();
3338 return 0;
3341 #endif
3343 /* Only allow this combination if insn_rtx_costs reports that the
3344 replacement instructions are cheaper than the originals. */
3345 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3347 undo_all ();
3348 return 0;
3351 /* We now know that we can do this combination. Merge the insns and
3352 update the status of registers and LOG_LINKS. */
3354 if (undobuf.other_insn)
3356 rtx note, next;
3358 PATTERN (undobuf.other_insn) = other_pat;
3360 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3361 are still valid. Then add any non-duplicate notes added by
3362 recog_for_combine. */
3363 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3365 next = XEXP (note, 1);
3367 if (REG_NOTE_KIND (note) == REG_UNUSED
3368 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3369 remove_note (undobuf.other_insn, note);
3372 distribute_notes (new_other_notes, undobuf.other_insn,
3373 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3376 if (swap_i2i3)
3378 rtx insn;
3379 rtx link;
3380 rtx ni2dest;
3382 /* I3 now uses what used to be its destination and which is now
3383 I2's destination. This requires us to do a few adjustments. */
3384 PATTERN (i3) = newpat;
3385 adjust_for_new_dest (i3);
3387 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3388 so we still will.
3390 However, some later insn might be using I2's dest and have
3391 a LOG_LINK pointing at I3. We must remove this link.
3392 The simplest way to remove the link is to point it at I1,
3393 which we know will be a NOTE. */
3395 /* newi2pat is usually a SET here; however, recog_for_combine might
3396 have added some clobbers. */
3397 if (GET_CODE (newi2pat) == PARALLEL)
3398 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3399 else
3400 ni2dest = SET_DEST (newi2pat);
3402 for (insn = NEXT_INSN (i3);
3403 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3404 || insn != BB_HEAD (this_basic_block->next_bb));
3405 insn = NEXT_INSN (insn))
3407 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3409 for (link = LOG_LINKS (insn); link;
3410 link = XEXP (link, 1))
3411 if (XEXP (link, 0) == i3)
3412 XEXP (link, 0) = i1;
3414 break;
3420 rtx i3notes, i2notes, i1notes = 0;
3421 rtx i3links, i2links, i1links = 0;
3422 rtx midnotes = 0;
3423 unsigned int regno;
3424 /* Compute which registers we expect to eliminate. newi2pat may be setting
3425 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3426 same as i3dest, in which case newi2pat may be setting i1dest. */
3427 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3428 || i2dest_in_i2src || i2dest_in_i1src
3429 || !i2dest_killed
3430 ? 0 : i2dest);
3431 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3432 || (newi2pat && reg_set_p (i1dest, newi2pat))
3433 || !i1dest_killed
3434 ? 0 : i1dest);
3436 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3437 clear them. */
3438 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3439 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3440 if (i1)
3441 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3443 /* Ensure that we do not have something that should not be shared but
3444 occurs multiple times in the new insns. Check this by first
3445 resetting all the `used' flags and then copying anything is shared. */
3447 reset_used_flags (i3notes);
3448 reset_used_flags (i2notes);
3449 reset_used_flags (i1notes);
3450 reset_used_flags (newpat);
3451 reset_used_flags (newi2pat);
3452 if (undobuf.other_insn)
3453 reset_used_flags (PATTERN (undobuf.other_insn));
3455 i3notes = copy_rtx_if_shared (i3notes);
3456 i2notes = copy_rtx_if_shared (i2notes);
3457 i1notes = copy_rtx_if_shared (i1notes);
3458 newpat = copy_rtx_if_shared (newpat);
3459 newi2pat = copy_rtx_if_shared (newi2pat);
3460 if (undobuf.other_insn)
3461 reset_used_flags (PATTERN (undobuf.other_insn));
3463 INSN_CODE (i3) = insn_code_number;
3464 PATTERN (i3) = newpat;
3466 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3468 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3470 reset_used_flags (call_usage);
3471 call_usage = copy_rtx (call_usage);
3473 if (substed_i2)
3474 replace_rtx (call_usage, i2dest, i2src);
3476 if (substed_i1)
3477 replace_rtx (call_usage, i1dest, i1src);
3479 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3482 if (undobuf.other_insn)
3483 INSN_CODE (undobuf.other_insn) = other_code_number;
3485 /* We had one special case above where I2 had more than one set and
3486 we replaced a destination of one of those sets with the destination
3487 of I3. In that case, we have to update LOG_LINKS of insns later
3488 in this basic block. Note that this (expensive) case is rare.
3490 Also, in this case, we must pretend that all REG_NOTEs for I2
3491 actually came from I3, so that REG_UNUSED notes from I2 will be
3492 properly handled. */
3494 if (i3_subst_into_i2)
3496 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3497 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3498 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3499 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3500 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3501 && ! find_reg_note (i2, REG_UNUSED,
3502 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3503 for (temp = NEXT_INSN (i2);
3504 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3505 || BB_HEAD (this_basic_block) != temp);
3506 temp = NEXT_INSN (temp))
3507 if (temp != i3 && INSN_P (temp))
3508 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3509 if (XEXP (link, 0) == i2)
3510 XEXP (link, 0) = i3;
3512 if (i3notes)
3514 rtx link = i3notes;
3515 while (XEXP (link, 1))
3516 link = XEXP (link, 1);
3517 XEXP (link, 1) = i2notes;
3519 else
3520 i3notes = i2notes;
3521 i2notes = 0;
3524 LOG_LINKS (i3) = 0;
3525 REG_NOTES (i3) = 0;
3526 LOG_LINKS (i2) = 0;
3527 REG_NOTES (i2) = 0;
3529 if (newi2pat)
3531 INSN_CODE (i2) = i2_code_number;
3532 PATTERN (i2) = newi2pat;
3534 else
3535 SET_INSN_DELETED (i2);
3537 if (i1)
3539 LOG_LINKS (i1) = 0;
3540 REG_NOTES (i1) = 0;
3541 SET_INSN_DELETED (i1);
3544 /* Get death notes for everything that is now used in either I3 or
3545 I2 and used to die in a previous insn. If we built two new
3546 patterns, move from I1 to I2 then I2 to I3 so that we get the
3547 proper movement on registers that I2 modifies. */
3549 if (newi2pat)
3551 move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3552 move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3554 else
3555 move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3556 i3, &midnotes);
3558 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
3559 if (i3notes)
3560 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3561 elim_i2, elim_i1);
3562 if (i2notes)
3563 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3564 elim_i2, elim_i1);
3565 if (i1notes)
3566 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3567 elim_i2, elim_i1);
3568 if (midnotes)
3569 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3570 elim_i2, elim_i1);
3572 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
3573 know these are REG_UNUSED and want them to go to the desired insn,
3574 so we always pass it as i3. */
3576 if (newi2pat && new_i2_notes)
3577 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3579 if (new_i3_notes)
3580 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3582 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
3583 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
3584 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
3585 in that case, it might delete I2. Similarly for I2 and I1.
3586 Show an additional death due to the REG_DEAD note we make here. If
3587 we discard it in distribute_notes, we will decrement it again. */
3589 if (i3dest_killed)
3591 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3592 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3593 NULL_RTX),
3594 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3595 else
3596 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3597 NULL_RTX),
3598 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3599 elim_i2, elim_i1);
3602 if (i2dest_in_i2src)
3604 if (newi2pat && reg_set_p (i2dest, newi2pat))
3605 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3606 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3607 else
3608 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3609 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3610 NULL_RTX, NULL_RTX);
3613 if (i1dest_in_i1src)
3615 if (newi2pat && reg_set_p (i1dest, newi2pat))
3616 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3617 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3618 else
3619 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3620 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3621 NULL_RTX, NULL_RTX);
3624 distribute_links (i3links);
3625 distribute_links (i2links);
3626 distribute_links (i1links);
3628 if (REG_P (i2dest))
3630 rtx link;
3631 rtx i2_insn = 0, i2_val = 0, set;
3633 /* The insn that used to set this register doesn't exist, and
3634 this life of the register may not exist either. See if one of
3635 I3's links points to an insn that sets I2DEST. If it does,
3636 that is now the last known value for I2DEST. If we don't update
3637 this and I2 set the register to a value that depended on its old
3638 contents, we will get confused. If this insn is used, thing
3639 will be set correctly in combine_instructions. */
3641 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3642 if ((set = single_set (XEXP (link, 0))) != 0
3643 && rtx_equal_p (i2dest, SET_DEST (set)))
3644 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3646 record_value_for_reg (i2dest, i2_insn, i2_val);
3648 /* If the reg formerly set in I2 died only once and that was in I3,
3649 zero its use count so it won't make `reload' do any work. */
3650 if (! added_sets_2
3651 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3652 && ! i2dest_in_i2src)
3654 regno = REGNO (i2dest);
3655 INC_REG_N_SETS (regno, -1);
3659 if (i1 && REG_P (i1dest))
3661 rtx link;
3662 rtx i1_insn = 0, i1_val = 0, set;
3664 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3665 if ((set = single_set (XEXP (link, 0))) != 0
3666 && rtx_equal_p (i1dest, SET_DEST (set)))
3667 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3669 record_value_for_reg (i1dest, i1_insn, i1_val);
3671 regno = REGNO (i1dest);
3672 if (! added_sets_1 && ! i1dest_in_i1src)
3673 INC_REG_N_SETS (regno, -1);
3676 /* Update reg_stat[].nonzero_bits et al for any changes that may have
3677 been made to this insn. The order of
3678 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
3679 can affect nonzero_bits of newpat */
3680 if (newi2pat)
3681 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3682 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3684 /* Set new_direct_jump_p if a new return or simple jump instruction
3685 has been created.
3687 If I3 is now an unconditional jump, ensure that it has a
3688 BARRIER following it since it may have initially been a
3689 conditional jump. It may also be the last nonnote insn. */
3691 if (returnjump_p (i3) || any_uncondjump_p (i3))
3693 *new_direct_jump_p = 1;
3694 mark_jump_label (PATTERN (i3), i3, 0);
3696 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3697 || !BARRIER_P (temp))
3698 emit_barrier_after (i3);
3701 if (undobuf.other_insn != NULL_RTX
3702 && (returnjump_p (undobuf.other_insn)
3703 || any_uncondjump_p (undobuf.other_insn)))
3705 *new_direct_jump_p = 1;
3707 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3708 || !BARRIER_P (temp))
3709 emit_barrier_after (undobuf.other_insn);
3712 /* An NOOP jump does not need barrier, but it does need cleaning up
3713 of CFG. */
3714 if (GET_CODE (newpat) == SET
3715 && SET_SRC (newpat) == pc_rtx
3716 && SET_DEST (newpat) == pc_rtx)
3717 *new_direct_jump_p = 1;
3720 if (undobuf.other_insn != NULL_RTX)
3722 if (dump_file)
3724 fprintf (dump_file, "modifying other_insn ");
3725 dump_insn_slim (dump_file, undobuf.other_insn);
3727 df_insn_rescan (undobuf.other_insn);
3730 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3732 if (dump_file)
3734 fprintf (dump_file, "modifying insn i1 ");
3735 dump_insn_slim (dump_file, i1);
3737 df_insn_rescan (i1);
3740 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3742 if (dump_file)
3744 fprintf (dump_file, "modifying insn i2 ");
3745 dump_insn_slim (dump_file, i2);
3747 df_insn_rescan (i2);
3750 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
3752 if (dump_file)
3754 fprintf (dump_file, "modifying insn i3 ");
3755 dump_insn_slim (dump_file, i3);
3757 df_insn_rescan (i3);
3760 combine_successes++;
3761 undo_commit ();
3763 if (added_links_insn
3764 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
3765 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
3766 return added_links_insn;
3767 else
3768 return newi2pat ? i2 : i3;
3771 /* Undo all the modifications recorded in undobuf. */
3773 static void
3774 undo_all (void)
3776 struct undo *undo, *next;
3778 for (undo = undobuf.undos; undo; undo = next)
3780 next = undo->next;
3781 switch (undo->kind)
3783 case UNDO_RTX:
3784 *undo->where.r = undo->old_contents.r;
3785 break;
3786 case UNDO_INT:
3787 *undo->where.i = undo->old_contents.i;
3788 break;
3789 case UNDO_MODE:
3790 PUT_MODE (*undo->where.r, undo->old_contents.m);
3791 break;
3792 default:
3793 gcc_unreachable ();
3796 undo->next = undobuf.frees;
3797 undobuf.frees = undo;
3800 undobuf.undos = 0;
3803 /* We've committed to accepting the changes we made. Move all
3804 of the undos to the free list. */
3806 static void
3807 undo_commit (void)
3809 struct undo *undo, *next;
3811 for (undo = undobuf.undos; undo; undo = next)
3813 next = undo->next;
3814 undo->next = undobuf.frees;
3815 undobuf.frees = undo;
3817 undobuf.undos = 0;
3820 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3821 where we have an arithmetic expression and return that point. LOC will
3822 be inside INSN.
3824 try_combine will call this function to see if an insn can be split into
3825 two insns. */
3827 static rtx *
3828 find_split_point (rtx *loc, rtx insn)
3830 rtx x = *loc;
3831 enum rtx_code code = GET_CODE (x);
3832 rtx *split;
3833 unsigned HOST_WIDE_INT len = 0;
3834 HOST_WIDE_INT pos = 0;
3835 int unsignedp = 0;
3836 rtx inner = NULL_RTX;
3838 /* First special-case some codes. */
3839 switch (code)
3841 case SUBREG:
3842 #ifdef INSN_SCHEDULING
3843 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3844 point. */
3845 if (MEM_P (SUBREG_REG (x)))
3846 return loc;
3847 #endif
3848 return find_split_point (&SUBREG_REG (x), insn);
3850 case MEM:
3851 #ifdef HAVE_lo_sum
3852 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3853 using LO_SUM and HIGH. */
3854 if (GET_CODE (XEXP (x, 0)) == CONST
3855 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3857 SUBST (XEXP (x, 0),
3858 gen_rtx_LO_SUM (Pmode,
3859 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3860 XEXP (x, 0)));
3861 return &XEXP (XEXP (x, 0), 0);
3863 #endif
3865 /* If we have a PLUS whose second operand is a constant and the
3866 address is not valid, perhaps will can split it up using
3867 the machine-specific way to split large constants. We use
3868 the first pseudo-reg (one of the virtual regs) as a placeholder;
3869 it will not remain in the result. */
3870 if (GET_CODE (XEXP (x, 0)) == PLUS
3871 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3872 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3874 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3875 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
3876 XEXP (x, 0)),
3877 subst_insn);
3879 /* This should have produced two insns, each of which sets our
3880 placeholder. If the source of the second is a valid address,
3881 we can make put both sources together and make a split point
3882 in the middle. */
3884 if (seq
3885 && NEXT_INSN (seq) != NULL_RTX
3886 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3887 && NONJUMP_INSN_P (seq)
3888 && GET_CODE (PATTERN (seq)) == SET
3889 && SET_DEST (PATTERN (seq)) == reg
3890 && ! reg_mentioned_p (reg,
3891 SET_SRC (PATTERN (seq)))
3892 && NONJUMP_INSN_P (NEXT_INSN (seq))
3893 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3894 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3895 && memory_address_p (GET_MODE (x),
3896 SET_SRC (PATTERN (NEXT_INSN (seq)))))
3898 rtx src1 = SET_SRC (PATTERN (seq));
3899 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3901 /* Replace the placeholder in SRC2 with SRC1. If we can
3902 find where in SRC2 it was placed, that can become our
3903 split point and we can replace this address with SRC2.
3904 Just try two obvious places. */
3906 src2 = replace_rtx (src2, reg, src1);
3907 split = 0;
3908 if (XEXP (src2, 0) == src1)
3909 split = &XEXP (src2, 0);
3910 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3911 && XEXP (XEXP (src2, 0), 0) == src1)
3912 split = &XEXP (XEXP (src2, 0), 0);
3914 if (split)
3916 SUBST (XEXP (x, 0), src2);
3917 return split;
3921 /* If that didn't work, perhaps the first operand is complex and
3922 needs to be computed separately, so make a split point there.
3923 This will occur on machines that just support REG + CONST
3924 and have a constant moved through some previous computation. */
3926 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3927 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3928 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3929 return &XEXP (XEXP (x, 0), 0);
3931 break;
3933 case SET:
3934 #ifdef HAVE_cc0
3935 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3936 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3937 we need to put the operand into a register. So split at that
3938 point. */
3940 if (SET_DEST (x) == cc0_rtx
3941 && GET_CODE (SET_SRC (x)) != COMPARE
3942 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3943 && !OBJECT_P (SET_SRC (x))
3944 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3945 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3946 return &SET_SRC (x);
3947 #endif
3949 /* See if we can split SET_SRC as it stands. */
3950 split = find_split_point (&SET_SRC (x), insn);
3951 if (split && split != &SET_SRC (x))
3952 return split;
3954 /* See if we can split SET_DEST as it stands. */
3955 split = find_split_point (&SET_DEST (x), insn);
3956 if (split && split != &SET_DEST (x))
3957 return split;
3959 /* See if this is a bitfield assignment with everything constant. If
3960 so, this is an IOR of an AND, so split it into that. */
3961 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3962 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3963 <= HOST_BITS_PER_WIDE_INT)
3964 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3965 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3966 && GET_CODE (SET_SRC (x)) == CONST_INT
3967 && ((INTVAL (XEXP (SET_DEST (x), 1))
3968 + INTVAL (XEXP (SET_DEST (x), 2)))
3969 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3970 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3972 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3973 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3974 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3975 rtx dest = XEXP (SET_DEST (x), 0);
3976 enum machine_mode mode = GET_MODE (dest);
3977 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3978 rtx or_mask;
3980 if (BITS_BIG_ENDIAN)
3981 pos = GET_MODE_BITSIZE (mode) - len - pos;
3983 or_mask = gen_int_mode (src << pos, mode);
3984 if (src == mask)
3985 SUBST (SET_SRC (x),
3986 simplify_gen_binary (IOR, mode, dest, or_mask));
3987 else
3989 rtx negmask = gen_int_mode (~(mask << pos), mode);
3990 SUBST (SET_SRC (x),
3991 simplify_gen_binary (IOR, mode,
3992 simplify_gen_binary (AND, mode,
3993 dest, negmask),
3994 or_mask));
3997 SUBST (SET_DEST (x), dest);
3999 split = find_split_point (&SET_SRC (x), insn);
4000 if (split && split != &SET_SRC (x))
4001 return split;
4004 /* Otherwise, see if this is an operation that we can split into two.
4005 If so, try to split that. */
4006 code = GET_CODE (SET_SRC (x));
4008 switch (code)
4010 case AND:
4011 /* If we are AND'ing with a large constant that is only a single
4012 bit and the result is only being used in a context where we
4013 need to know if it is zero or nonzero, replace it with a bit
4014 extraction. This will avoid the large constant, which might
4015 have taken more than one insn to make. If the constant were
4016 not a valid argument to the AND but took only one insn to make,
4017 this is no worse, but if it took more than one insn, it will
4018 be better. */
4020 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4021 && REG_P (XEXP (SET_SRC (x), 0))
4022 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4023 && REG_P (SET_DEST (x))
4024 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4025 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4026 && XEXP (*split, 0) == SET_DEST (x)
4027 && XEXP (*split, 1) == const0_rtx)
4029 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4030 XEXP (SET_SRC (x), 0),
4031 pos, NULL_RTX, 1, 1, 0, 0);
4032 if (extraction != 0)
4034 SUBST (SET_SRC (x), extraction);
4035 return find_split_point (loc, insn);
4038 break;
4040 case NE:
4041 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4042 is known to be on, this can be converted into a NEG of a shift. */
4043 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4044 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4045 && 1 <= (pos = exact_log2
4046 (nonzero_bits (XEXP (SET_SRC (x), 0),
4047 GET_MODE (XEXP (SET_SRC (x), 0))))))
4049 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4051 SUBST (SET_SRC (x),
4052 gen_rtx_NEG (mode,
4053 gen_rtx_LSHIFTRT (mode,
4054 XEXP (SET_SRC (x), 0),
4055 GEN_INT (pos))));
4057 split = find_split_point (&SET_SRC (x), insn);
4058 if (split && split != &SET_SRC (x))
4059 return split;
4061 break;
4063 case SIGN_EXTEND:
4064 inner = XEXP (SET_SRC (x), 0);
4066 /* We can't optimize if either mode is a partial integer
4067 mode as we don't know how many bits are significant
4068 in those modes. */
4069 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4070 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4071 break;
4073 pos = 0;
4074 len = GET_MODE_BITSIZE (GET_MODE (inner));
4075 unsignedp = 0;
4076 break;
4078 case SIGN_EXTRACT:
4079 case ZERO_EXTRACT:
4080 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4081 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
4083 inner = XEXP (SET_SRC (x), 0);
4084 len = INTVAL (XEXP (SET_SRC (x), 1));
4085 pos = INTVAL (XEXP (SET_SRC (x), 2));
4087 if (BITS_BIG_ENDIAN)
4088 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4089 unsignedp = (code == ZERO_EXTRACT);
4091 break;
4093 default:
4094 break;
4097 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4099 enum machine_mode mode = GET_MODE (SET_SRC (x));
4101 /* For unsigned, we have a choice of a shift followed by an
4102 AND or two shifts. Use two shifts for field sizes where the
4103 constant might be too large. We assume here that we can
4104 always at least get 8-bit constants in an AND insn, which is
4105 true for every current RISC. */
4107 if (unsignedp && len <= 8)
4109 SUBST (SET_SRC (x),
4110 gen_rtx_AND (mode,
4111 gen_rtx_LSHIFTRT
4112 (mode, gen_lowpart (mode, inner),
4113 GEN_INT (pos)),
4114 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4116 split = find_split_point (&SET_SRC (x), insn);
4117 if (split && split != &SET_SRC (x))
4118 return split;
4120 else
4122 SUBST (SET_SRC (x),
4123 gen_rtx_fmt_ee
4124 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4125 gen_rtx_ASHIFT (mode,
4126 gen_lowpart (mode, inner),
4127 GEN_INT (GET_MODE_BITSIZE (mode)
4128 - len - pos)),
4129 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4131 split = find_split_point (&SET_SRC (x), insn);
4132 if (split && split != &SET_SRC (x))
4133 return split;
4137 /* See if this is a simple operation with a constant as the second
4138 operand. It might be that this constant is out of range and hence
4139 could be used as a split point. */
4140 if (BINARY_P (SET_SRC (x))
4141 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4142 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4143 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4144 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4145 return &XEXP (SET_SRC (x), 1);
4147 /* Finally, see if this is a simple operation with its first operand
4148 not in a register. The operation might require this operand in a
4149 register, so return it as a split point. We can always do this
4150 because if the first operand were another operation, we would have
4151 already found it as a split point. */
4152 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4153 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4154 return &XEXP (SET_SRC (x), 0);
4156 return 0;
4158 case AND:
4159 case IOR:
4160 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4161 it is better to write this as (not (ior A B)) so we can split it.
4162 Similarly for IOR. */
4163 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4165 SUBST (*loc,
4166 gen_rtx_NOT (GET_MODE (x),
4167 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4168 GET_MODE (x),
4169 XEXP (XEXP (x, 0), 0),
4170 XEXP (XEXP (x, 1), 0))));
4171 return find_split_point (loc, insn);
4174 /* Many RISC machines have a large set of logical insns. If the
4175 second operand is a NOT, put it first so we will try to split the
4176 other operand first. */
4177 if (GET_CODE (XEXP (x, 1)) == NOT)
4179 rtx tem = XEXP (x, 0);
4180 SUBST (XEXP (x, 0), XEXP (x, 1));
4181 SUBST (XEXP (x, 1), tem);
4183 break;
4185 default:
4186 break;
4189 /* Otherwise, select our actions depending on our rtx class. */
4190 switch (GET_RTX_CLASS (code))
4192 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4193 case RTX_TERNARY:
4194 split = find_split_point (&XEXP (x, 2), insn);
4195 if (split)
4196 return split;
4197 /* ... fall through ... */
4198 case RTX_BIN_ARITH:
4199 case RTX_COMM_ARITH:
4200 case RTX_COMPARE:
4201 case RTX_COMM_COMPARE:
4202 split = find_split_point (&XEXP (x, 1), insn);
4203 if (split)
4204 return split;
4205 /* ... fall through ... */
4206 case RTX_UNARY:
4207 /* Some machines have (and (shift ...) ...) insns. If X is not
4208 an AND, but XEXP (X, 0) is, use it as our split point. */
4209 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4210 return &XEXP (x, 0);
4212 split = find_split_point (&XEXP (x, 0), insn);
4213 if (split)
4214 return split;
4215 return loc;
4217 default:
4218 /* Otherwise, we don't have a split point. */
4219 return 0;
4223 /* Throughout X, replace FROM with TO, and return the result.
4224 The result is TO if X is FROM;
4225 otherwise the result is X, but its contents may have been modified.
4226 If they were modified, a record was made in undobuf so that
4227 undo_all will (among other things) return X to its original state.
4229 If the number of changes necessary is too much to record to undo,
4230 the excess changes are not made, so the result is invalid.
4231 The changes already made can still be undone.
4232 undobuf.num_undo is incremented for such changes, so by testing that
4233 the caller can tell whether the result is valid.
4235 `n_occurrences' is incremented each time FROM is replaced.
4237 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4239 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4240 by copying if `n_occurrences' is nonzero. */
4242 static rtx
4243 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4245 enum rtx_code code = GET_CODE (x);
4246 enum machine_mode op0_mode = VOIDmode;
4247 const char *fmt;
4248 int len, i;
4249 rtx new;
4251 /* Two expressions are equal if they are identical copies of a shared
4252 RTX or if they are both registers with the same register number
4253 and mode. */
4255 #define COMBINE_RTX_EQUAL_P(X,Y) \
4256 ((X) == (Y) \
4257 || (REG_P (X) && REG_P (Y) \
4258 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4260 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4262 n_occurrences++;
4263 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4266 /* If X and FROM are the same register but different modes, they
4267 will not have been seen as equal above. However, the log links code
4268 will make a LOG_LINKS entry for that case. If we do nothing, we
4269 will try to rerecognize our original insn and, when it succeeds,
4270 we will delete the feeding insn, which is incorrect.
4272 So force this insn not to match in this (rare) case. */
4273 if (! in_dest && code == REG && REG_P (from)
4274 && reg_overlap_mentioned_p (x, from))
4275 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4277 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4278 of which may contain things that can be combined. */
4279 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4280 return x;
4282 /* It is possible to have a subexpression appear twice in the insn.
4283 Suppose that FROM is a register that appears within TO.
4284 Then, after that subexpression has been scanned once by `subst',
4285 the second time it is scanned, TO may be found. If we were
4286 to scan TO here, we would find FROM within it and create a
4287 self-referent rtl structure which is completely wrong. */
4288 if (COMBINE_RTX_EQUAL_P (x, to))
4289 return to;
4291 /* Parallel asm_operands need special attention because all of the
4292 inputs are shared across the arms. Furthermore, unsharing the
4293 rtl results in recognition failures. Failure to handle this case
4294 specially can result in circular rtl.
4296 Solve this by doing a normal pass across the first entry of the
4297 parallel, and only processing the SET_DESTs of the subsequent
4298 entries. Ug. */
4300 if (code == PARALLEL
4301 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4302 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4304 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4306 /* If this substitution failed, this whole thing fails. */
4307 if (GET_CODE (new) == CLOBBER
4308 && XEXP (new, 0) == const0_rtx)
4309 return new;
4311 SUBST (XVECEXP (x, 0, 0), new);
4313 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4315 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4317 if (!REG_P (dest)
4318 && GET_CODE (dest) != CC0
4319 && GET_CODE (dest) != PC)
4321 new = subst (dest, from, to, 0, unique_copy);
4323 /* If this substitution failed, this whole thing fails. */
4324 if (GET_CODE (new) == CLOBBER
4325 && XEXP (new, 0) == const0_rtx)
4326 return new;
4328 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
4332 else
4334 len = GET_RTX_LENGTH (code);
4335 fmt = GET_RTX_FORMAT (code);
4337 /* We don't need to process a SET_DEST that is a register, CC0,
4338 or PC, so set up to skip this common case. All other cases
4339 where we want to suppress replacing something inside a
4340 SET_SRC are handled via the IN_DEST operand. */
4341 if (code == SET
4342 && (REG_P (SET_DEST (x))
4343 || GET_CODE (SET_DEST (x)) == CC0
4344 || GET_CODE (SET_DEST (x)) == PC))
4345 fmt = "ie";
4347 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4348 constant. */
4349 if (fmt[0] == 'e')
4350 op0_mode = GET_MODE (XEXP (x, 0));
4352 for (i = 0; i < len; i++)
4354 if (fmt[i] == 'E')
4356 int j;
4357 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4359 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4361 new = (unique_copy && n_occurrences
4362 ? copy_rtx (to) : to);
4363 n_occurrences++;
4365 else
4367 new = subst (XVECEXP (x, i, j), from, to, 0,
4368 unique_copy);
4370 /* If this substitution failed, this whole thing
4371 fails. */
4372 if (GET_CODE (new) == CLOBBER
4373 && XEXP (new, 0) == const0_rtx)
4374 return new;
4377 SUBST (XVECEXP (x, i, j), new);
4380 else if (fmt[i] == 'e')
4382 /* If this is a register being set, ignore it. */
4383 new = XEXP (x, i);
4384 if (in_dest
4385 && i == 0
4386 && (((code == SUBREG || code == ZERO_EXTRACT)
4387 && REG_P (new))
4388 || code == STRICT_LOW_PART))
4391 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4393 /* In general, don't install a subreg involving two
4394 modes not tieable. It can worsen register
4395 allocation, and can even make invalid reload
4396 insns, since the reg inside may need to be copied
4397 from in the outside mode, and that may be invalid
4398 if it is an fp reg copied in integer mode.
4400 We allow two exceptions to this: It is valid if
4401 it is inside another SUBREG and the mode of that
4402 SUBREG and the mode of the inside of TO is
4403 tieable and it is valid if X is a SET that copies
4404 FROM to CC0. */
4406 if (GET_CODE (to) == SUBREG
4407 && ! MODES_TIEABLE_P (GET_MODE (to),
4408 GET_MODE (SUBREG_REG (to)))
4409 && ! (code == SUBREG
4410 && MODES_TIEABLE_P (GET_MODE (x),
4411 GET_MODE (SUBREG_REG (to))))
4412 #ifdef HAVE_cc0
4413 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4414 #endif
4416 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4418 #ifdef CANNOT_CHANGE_MODE_CLASS
4419 if (code == SUBREG
4420 && REG_P (to)
4421 && REGNO (to) < FIRST_PSEUDO_REGISTER
4422 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4423 GET_MODE (to),
4424 GET_MODE (x)))
4425 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4426 #endif
4428 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4429 n_occurrences++;
4431 else
4432 /* If we are in a SET_DEST, suppress most cases unless we
4433 have gone inside a MEM, in which case we want to
4434 simplify the address. We assume here that things that
4435 are actually part of the destination have their inner
4436 parts in the first expression. This is true for SUBREG,
4437 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4438 things aside from REG and MEM that should appear in a
4439 SET_DEST. */
4440 new = subst (XEXP (x, i), from, to,
4441 (((in_dest
4442 && (code == SUBREG || code == STRICT_LOW_PART
4443 || code == ZERO_EXTRACT))
4444 || code == SET)
4445 && i == 0), unique_copy);
4447 /* If we found that we will have to reject this combination,
4448 indicate that by returning the CLOBBER ourselves, rather than
4449 an expression containing it. This will speed things up as
4450 well as prevent accidents where two CLOBBERs are considered
4451 to be equal, thus producing an incorrect simplification. */
4453 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
4454 return new;
4456 if (GET_CODE (x) == SUBREG
4457 && (GET_CODE (new) == CONST_INT
4458 || GET_CODE (new) == CONST_DOUBLE))
4460 enum machine_mode mode = GET_MODE (x);
4462 x = simplify_subreg (GET_MODE (x), new,
4463 GET_MODE (SUBREG_REG (x)),
4464 SUBREG_BYTE (x));
4465 if (! x)
4466 x = gen_rtx_CLOBBER (mode, const0_rtx);
4468 else if (GET_CODE (new) == CONST_INT
4469 && GET_CODE (x) == ZERO_EXTEND)
4471 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4472 new, GET_MODE (XEXP (x, 0)));
4473 gcc_assert (x);
4475 else
4476 SUBST (XEXP (x, i), new);
4481 /* Try to simplify X. If the simplification changed the code, it is likely
4482 that further simplification will help, so loop, but limit the number
4483 of repetitions that will be performed. */
4485 for (i = 0; i < 4; i++)
4487 /* If X is sufficiently simple, don't bother trying to do anything
4488 with it. */
4489 if (code != CONST_INT && code != REG && code != CLOBBER)
4490 x = combine_simplify_rtx (x, op0_mode, in_dest);
4492 if (GET_CODE (x) == code)
4493 break;
4495 code = GET_CODE (x);
4497 /* We no longer know the original mode of operand 0 since we
4498 have changed the form of X) */
4499 op0_mode = VOIDmode;
4502 return x;
4505 /* Simplify X, a piece of RTL. We just operate on the expression at the
4506 outer level; call `subst' to simplify recursively. Return the new
4507 expression.
4509 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
4510 if we are inside a SET_DEST. */
4512 static rtx
4513 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4515 enum rtx_code code = GET_CODE (x);
4516 enum machine_mode mode = GET_MODE (x);
4517 rtx temp;
4518 int i;
4520 /* If this is a commutative operation, put a constant last and a complex
4521 expression first. We don't need to do this for comparisons here. */
4522 if (COMMUTATIVE_ARITH_P (x)
4523 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4525 temp = XEXP (x, 0);
4526 SUBST (XEXP (x, 0), XEXP (x, 1));
4527 SUBST (XEXP (x, 1), temp);
4530 /* If this is a simple operation applied to an IF_THEN_ELSE, try
4531 applying it to the arms of the IF_THEN_ELSE. This often simplifies
4532 things. Check for cases where both arms are testing the same
4533 condition.
4535 Don't do anything if all operands are very simple. */
4537 if ((BINARY_P (x)
4538 && ((!OBJECT_P (XEXP (x, 0))
4539 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4540 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4541 || (!OBJECT_P (XEXP (x, 1))
4542 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4543 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4544 || (UNARY_P (x)
4545 && (!OBJECT_P (XEXP (x, 0))
4546 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4547 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4549 rtx cond, true_rtx, false_rtx;
4551 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4552 if (cond != 0
4553 /* If everything is a comparison, what we have is highly unlikely
4554 to be simpler, so don't use it. */
4555 && ! (COMPARISON_P (x)
4556 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4558 rtx cop1 = const0_rtx;
4559 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4561 if (cond_code == NE && COMPARISON_P (cond))
4562 return x;
4564 /* Simplify the alternative arms; this may collapse the true and
4565 false arms to store-flag values. Be careful to use copy_rtx
4566 here since true_rtx or false_rtx might share RTL with x as a
4567 result of the if_then_else_cond call above. */
4568 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4569 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4571 /* If true_rtx and false_rtx are not general_operands, an if_then_else
4572 is unlikely to be simpler. */
4573 if (general_operand (true_rtx, VOIDmode)
4574 && general_operand (false_rtx, VOIDmode))
4576 enum rtx_code reversed;
4578 /* Restarting if we generate a store-flag expression will cause
4579 us to loop. Just drop through in this case. */
4581 /* If the result values are STORE_FLAG_VALUE and zero, we can
4582 just make the comparison operation. */
4583 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4584 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4585 cond, cop1);
4586 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4587 && ((reversed = reversed_comparison_code_parts
4588 (cond_code, cond, cop1, NULL))
4589 != UNKNOWN))
4590 x = simplify_gen_relational (reversed, mode, VOIDmode,
4591 cond, cop1);
4593 /* Likewise, we can make the negate of a comparison operation
4594 if the result values are - STORE_FLAG_VALUE and zero. */
4595 else if (GET_CODE (true_rtx) == CONST_INT
4596 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4597 && false_rtx == const0_rtx)
4598 x = simplify_gen_unary (NEG, mode,
4599 simplify_gen_relational (cond_code,
4600 mode, VOIDmode,
4601 cond, cop1),
4602 mode);
4603 else if (GET_CODE (false_rtx) == CONST_INT
4604 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4605 && true_rtx == const0_rtx
4606 && ((reversed = reversed_comparison_code_parts
4607 (cond_code, cond, cop1, NULL))
4608 != UNKNOWN))
4609 x = simplify_gen_unary (NEG, mode,
4610 simplify_gen_relational (reversed,
4611 mode, VOIDmode,
4612 cond, cop1),
4613 mode);
4614 else
4615 return gen_rtx_IF_THEN_ELSE (mode,
4616 simplify_gen_relational (cond_code,
4617 mode,
4618 VOIDmode,
4619 cond,
4620 cop1),
4621 true_rtx, false_rtx);
4623 code = GET_CODE (x);
4624 op0_mode = VOIDmode;
4629 /* Try to fold this expression in case we have constants that weren't
4630 present before. */
4631 temp = 0;
4632 switch (GET_RTX_CLASS (code))
4634 case RTX_UNARY:
4635 if (op0_mode == VOIDmode)
4636 op0_mode = GET_MODE (XEXP (x, 0));
4637 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4638 break;
4639 case RTX_COMPARE:
4640 case RTX_COMM_COMPARE:
4642 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4643 if (cmp_mode == VOIDmode)
4645 cmp_mode = GET_MODE (XEXP (x, 1));
4646 if (cmp_mode == VOIDmode)
4647 cmp_mode = op0_mode;
4649 temp = simplify_relational_operation (code, mode, cmp_mode,
4650 XEXP (x, 0), XEXP (x, 1));
4652 break;
4653 case RTX_COMM_ARITH:
4654 case RTX_BIN_ARITH:
4655 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4656 break;
4657 case RTX_BITFIELD_OPS:
4658 case RTX_TERNARY:
4659 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4660 XEXP (x, 1), XEXP (x, 2));
4661 break;
4662 default:
4663 break;
4666 if (temp)
4668 x = temp;
4669 code = GET_CODE (temp);
4670 op0_mode = VOIDmode;
4671 mode = GET_MODE (temp);
4674 /* First see if we can apply the inverse distributive law. */
4675 if (code == PLUS || code == MINUS
4676 || code == AND || code == IOR || code == XOR)
4678 x = apply_distributive_law (x);
4679 code = GET_CODE (x);
4680 op0_mode = VOIDmode;
4683 /* If CODE is an associative operation not otherwise handled, see if we
4684 can associate some operands. This can win if they are constants or
4685 if they are logically related (i.e. (a & b) & a). */
4686 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4687 || code == AND || code == IOR || code == XOR
4688 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4689 && ((INTEGRAL_MODE_P (mode) && code != DIV)
4690 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
4692 if (GET_CODE (XEXP (x, 0)) == code)
4694 rtx other = XEXP (XEXP (x, 0), 0);
4695 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4696 rtx inner_op1 = XEXP (x, 1);
4697 rtx inner;
4699 /* Make sure we pass the constant operand if any as the second
4700 one if this is a commutative operation. */
4701 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4703 rtx tem = inner_op0;
4704 inner_op0 = inner_op1;
4705 inner_op1 = tem;
4707 inner = simplify_binary_operation (code == MINUS ? PLUS
4708 : code == DIV ? MULT
4709 : code,
4710 mode, inner_op0, inner_op1);
4712 /* For commutative operations, try the other pair if that one
4713 didn't simplify. */
4714 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4716 other = XEXP (XEXP (x, 0), 1);
4717 inner = simplify_binary_operation (code, mode,
4718 XEXP (XEXP (x, 0), 0),
4719 XEXP (x, 1));
4722 if (inner)
4723 return simplify_gen_binary (code, mode, other, inner);
4727 /* A little bit of algebraic simplification here. */
4728 switch (code)
4730 case MEM:
4731 /* Ensure that our address has any ASHIFTs converted to MULT in case
4732 address-recognizing predicates are called later. */
4733 temp = make_compound_operation (XEXP (x, 0), MEM);
4734 SUBST (XEXP (x, 0), temp);
4735 break;
4737 case SUBREG:
4738 if (op0_mode == VOIDmode)
4739 op0_mode = GET_MODE (SUBREG_REG (x));
4741 /* See if this can be moved to simplify_subreg. */
4742 if (CONSTANT_P (SUBREG_REG (x))
4743 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4744 /* Don't call gen_lowpart if the inner mode
4745 is VOIDmode and we cannot simplify it, as SUBREG without
4746 inner mode is invalid. */
4747 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4748 || gen_lowpart_common (mode, SUBREG_REG (x))))
4749 return gen_lowpart (mode, SUBREG_REG (x));
4751 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4752 break;
4754 rtx temp;
4755 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4756 SUBREG_BYTE (x));
4757 if (temp)
4758 return temp;
4761 /* Don't change the mode of the MEM if that would change the meaning
4762 of the address. */
4763 if (MEM_P (SUBREG_REG (x))
4764 && (MEM_VOLATILE_P (SUBREG_REG (x))
4765 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4766 return gen_rtx_CLOBBER (mode, const0_rtx);
4768 /* Note that we cannot do any narrowing for non-constants since
4769 we might have been counting on using the fact that some bits were
4770 zero. We now do this in the SET. */
4772 break;
4774 case NEG:
4775 temp = expand_compound_operation (XEXP (x, 0));
4777 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4778 replaced by (lshiftrt X C). This will convert
4779 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4781 if (GET_CODE (temp) == ASHIFTRT
4782 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4783 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4784 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4785 INTVAL (XEXP (temp, 1)));
4787 /* If X has only a single bit that might be nonzero, say, bit I, convert
4788 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4789 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4790 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4791 or a SUBREG of one since we'd be making the expression more
4792 complex if it was just a register. */
4794 if (!REG_P (temp)
4795 && ! (GET_CODE (temp) == SUBREG
4796 && REG_P (SUBREG_REG (temp)))
4797 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4799 rtx temp1 = simplify_shift_const
4800 (NULL_RTX, ASHIFTRT, mode,
4801 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4802 GET_MODE_BITSIZE (mode) - 1 - i),
4803 GET_MODE_BITSIZE (mode) - 1 - i);
4805 /* If all we did was surround TEMP with the two shifts, we
4806 haven't improved anything, so don't use it. Otherwise,
4807 we are better off with TEMP1. */
4808 if (GET_CODE (temp1) != ASHIFTRT
4809 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4810 || XEXP (XEXP (temp1, 0), 0) != temp)
4811 return temp1;
4813 break;
4815 case TRUNCATE:
4816 /* We can't handle truncation to a partial integer mode here
4817 because we don't know the real bitsize of the partial
4818 integer mode. */
4819 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4820 break;
4822 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4823 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4824 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4825 SUBST (XEXP (x, 0),
4826 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4827 GET_MODE_MASK (mode), 0));
4829 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4830 whose value is a comparison can be replaced with a subreg if
4831 STORE_FLAG_VALUE permits. */
4832 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4833 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4834 && (temp = get_last_value (XEXP (x, 0)))
4835 && COMPARISON_P (temp))
4836 return gen_lowpart (mode, XEXP (x, 0));
4837 break;
4839 #ifdef HAVE_cc0
4840 case COMPARE:
4841 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4842 using cc0, in which case we want to leave it as a COMPARE
4843 so we can distinguish it from a register-register-copy. */
4844 if (XEXP (x, 1) == const0_rtx)
4845 return XEXP (x, 0);
4847 /* x - 0 is the same as x unless x's mode has signed zeros and
4848 allows rounding towards -infinity. Under those conditions,
4849 0 - 0 is -0. */
4850 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4851 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4852 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4853 return XEXP (x, 0);
4854 break;
4855 #endif
4857 case CONST:
4858 /* (const (const X)) can become (const X). Do it this way rather than
4859 returning the inner CONST since CONST can be shared with a
4860 REG_EQUAL note. */
4861 if (GET_CODE (XEXP (x, 0)) == CONST)
4862 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4863 break;
4865 #ifdef HAVE_lo_sum
4866 case LO_SUM:
4867 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4868 can add in an offset. find_split_point will split this address up
4869 again if it doesn't match. */
4870 if (GET_CODE (XEXP (x, 0)) == HIGH
4871 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4872 return XEXP (x, 1);
4873 break;
4874 #endif
4876 case PLUS:
4877 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4878 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4879 bit-field and can be replaced by either a sign_extend or a
4880 sign_extract. The `and' may be a zero_extend and the two
4881 <c>, -<c> constants may be reversed. */
4882 if (GET_CODE (XEXP (x, 0)) == XOR
4883 && GET_CODE (XEXP (x, 1)) == CONST_INT
4884 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4885 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4886 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4887 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4888 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4889 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4890 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4891 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4892 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4893 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4894 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4895 == (unsigned int) i + 1))))
4896 return simplify_shift_const
4897 (NULL_RTX, ASHIFTRT, mode,
4898 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4899 XEXP (XEXP (XEXP (x, 0), 0), 0),
4900 GET_MODE_BITSIZE (mode) - (i + 1)),
4901 GET_MODE_BITSIZE (mode) - (i + 1));
4903 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4904 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4905 the bitsize of the mode - 1. This allows simplification of
4906 "a = (b & 8) == 0;" */
4907 if (XEXP (x, 1) == constm1_rtx
4908 && !REG_P (XEXP (x, 0))
4909 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4910 && REG_P (SUBREG_REG (XEXP (x, 0))))
4911 && nonzero_bits (XEXP (x, 0), mode) == 1)
4912 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4913 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4914 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4915 GET_MODE_BITSIZE (mode) - 1),
4916 GET_MODE_BITSIZE (mode) - 1);
4918 /* If we are adding two things that have no bits in common, convert
4919 the addition into an IOR. This will often be further simplified,
4920 for example in cases like ((a & 1) + (a & 2)), which can
4921 become a & 3. */
4923 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4924 && (nonzero_bits (XEXP (x, 0), mode)
4925 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4927 /* Try to simplify the expression further. */
4928 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4929 temp = combine_simplify_rtx (tor, mode, in_dest);
4931 /* If we could, great. If not, do not go ahead with the IOR
4932 replacement, since PLUS appears in many special purpose
4933 address arithmetic instructions. */
4934 if (GET_CODE (temp) != CLOBBER && temp != tor)
4935 return temp;
4937 break;
4939 case MINUS:
4940 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4941 (and <foo> (const_int pow2-1)) */
4942 if (GET_CODE (XEXP (x, 1)) == AND
4943 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4944 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4945 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4946 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4947 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4948 break;
4950 case MULT:
4951 /* If we have (mult (plus A B) C), apply the distributive law and then
4952 the inverse distributive law to see if things simplify. This
4953 occurs mostly in addresses, often when unrolling loops. */
4955 if (GET_CODE (XEXP (x, 0)) == PLUS)
4957 rtx result = distribute_and_simplify_rtx (x, 0);
4958 if (result)
4959 return result;
4962 /* Try simplify a*(b/c) as (a*b)/c. */
4963 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4964 && GET_CODE (XEXP (x, 0)) == DIV)
4966 rtx tem = simplify_binary_operation (MULT, mode,
4967 XEXP (XEXP (x, 0), 0),
4968 XEXP (x, 1));
4969 if (tem)
4970 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4972 break;
4974 case UDIV:
4975 /* If this is a divide by a power of two, treat it as a shift if
4976 its first operand is a shift. */
4977 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4978 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4979 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4980 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4981 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4982 || GET_CODE (XEXP (x, 0)) == ROTATE
4983 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4984 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4985 break;
4987 case EQ: case NE:
4988 case GT: case GTU: case GE: case GEU:
4989 case LT: case LTU: case LE: case LEU:
4990 case UNEQ: case LTGT:
4991 case UNGT: case UNGE:
4992 case UNLT: case UNLE:
4993 case UNORDERED: case ORDERED:
4994 /* If the first operand is a condition code, we can't do anything
4995 with it. */
4996 if (GET_CODE (XEXP (x, 0)) == COMPARE
4997 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4998 && ! CC0_P (XEXP (x, 0))))
5000 rtx op0 = XEXP (x, 0);
5001 rtx op1 = XEXP (x, 1);
5002 enum rtx_code new_code;
5004 if (GET_CODE (op0) == COMPARE)
5005 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5007 /* Simplify our comparison, if possible. */
5008 new_code = simplify_comparison (code, &op0, &op1);
5010 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5011 if only the low-order bit is possibly nonzero in X (such as when
5012 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5013 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5014 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5015 (plus X 1).
5017 Remove any ZERO_EXTRACT we made when thinking this was a
5018 comparison. It may now be simpler to use, e.g., an AND. If a
5019 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5020 the call to make_compound_operation in the SET case. */
5022 if (STORE_FLAG_VALUE == 1
5023 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5024 && op1 == const0_rtx
5025 && mode == GET_MODE (op0)
5026 && nonzero_bits (op0, mode) == 1)
5027 return gen_lowpart (mode,
5028 expand_compound_operation (op0));
5030 else if (STORE_FLAG_VALUE == 1
5031 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5032 && op1 == const0_rtx
5033 && mode == GET_MODE (op0)
5034 && (num_sign_bit_copies (op0, mode)
5035 == GET_MODE_BITSIZE (mode)))
5037 op0 = expand_compound_operation (op0);
5038 return simplify_gen_unary (NEG, mode,
5039 gen_lowpart (mode, op0),
5040 mode);
5043 else if (STORE_FLAG_VALUE == 1
5044 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5045 && op1 == const0_rtx
5046 && mode == GET_MODE (op0)
5047 && nonzero_bits (op0, mode) == 1)
5049 op0 = expand_compound_operation (op0);
5050 return simplify_gen_binary (XOR, mode,
5051 gen_lowpart (mode, op0),
5052 const1_rtx);
5055 else if (STORE_FLAG_VALUE == 1
5056 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5057 && op1 == const0_rtx
5058 && mode == GET_MODE (op0)
5059 && (num_sign_bit_copies (op0, mode)
5060 == GET_MODE_BITSIZE (mode)))
5062 op0 = expand_compound_operation (op0);
5063 return plus_constant (gen_lowpart (mode, op0), 1);
5066 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5067 those above. */
5068 if (STORE_FLAG_VALUE == -1
5069 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5070 && op1 == const0_rtx
5071 && (num_sign_bit_copies (op0, mode)
5072 == GET_MODE_BITSIZE (mode)))
5073 return gen_lowpart (mode,
5074 expand_compound_operation (op0));
5076 else if (STORE_FLAG_VALUE == -1
5077 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5078 && op1 == const0_rtx
5079 && mode == GET_MODE (op0)
5080 && nonzero_bits (op0, mode) == 1)
5082 op0 = expand_compound_operation (op0);
5083 return simplify_gen_unary (NEG, mode,
5084 gen_lowpart (mode, op0),
5085 mode);
5088 else if (STORE_FLAG_VALUE == -1
5089 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5090 && op1 == const0_rtx
5091 && mode == GET_MODE (op0)
5092 && (num_sign_bit_copies (op0, mode)
5093 == GET_MODE_BITSIZE (mode)))
5095 op0 = expand_compound_operation (op0);
5096 return simplify_gen_unary (NOT, mode,
5097 gen_lowpart (mode, op0),
5098 mode);
5101 /* If X is 0/1, (eq X 0) is X-1. */
5102 else if (STORE_FLAG_VALUE == -1
5103 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5104 && op1 == const0_rtx
5105 && mode == GET_MODE (op0)
5106 && nonzero_bits (op0, mode) == 1)
5108 op0 = expand_compound_operation (op0);
5109 return plus_constant (gen_lowpart (mode, op0), -1);
5112 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5113 one bit that might be nonzero, we can convert (ne x 0) to
5114 (ashift x c) where C puts the bit in the sign bit. Remove any
5115 AND with STORE_FLAG_VALUE when we are done, since we are only
5116 going to test the sign bit. */
5117 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5118 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5119 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5120 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5121 && op1 == const0_rtx
5122 && mode == GET_MODE (op0)
5123 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5125 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5126 expand_compound_operation (op0),
5127 GET_MODE_BITSIZE (mode) - 1 - i);
5128 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5129 return XEXP (x, 0);
5130 else
5131 return x;
5134 /* If the code changed, return a whole new comparison. */
5135 if (new_code != code)
5136 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5138 /* Otherwise, keep this operation, but maybe change its operands.
5139 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5140 SUBST (XEXP (x, 0), op0);
5141 SUBST (XEXP (x, 1), op1);
5143 break;
5145 case IF_THEN_ELSE:
5146 return simplify_if_then_else (x);
5148 case ZERO_EXTRACT:
5149 case SIGN_EXTRACT:
5150 case ZERO_EXTEND:
5151 case SIGN_EXTEND:
5152 /* If we are processing SET_DEST, we are done. */
5153 if (in_dest)
5154 return x;
5156 return expand_compound_operation (x);
5158 case SET:
5159 return simplify_set (x);
5161 case AND:
5162 case IOR:
5163 return simplify_logical (x);
5165 case ASHIFT:
5166 case LSHIFTRT:
5167 case ASHIFTRT:
5168 case ROTATE:
5169 case ROTATERT:
5170 /* If this is a shift by a constant amount, simplify it. */
5171 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5172 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5173 INTVAL (XEXP (x, 1)));
5175 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5176 SUBST (XEXP (x, 1),
5177 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5178 ((HOST_WIDE_INT) 1
5179 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5180 - 1,
5181 0));
5182 break;
5184 default:
5185 break;
5188 return x;
5191 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5193 static rtx
5194 simplify_if_then_else (rtx x)
5196 enum machine_mode mode = GET_MODE (x);
5197 rtx cond = XEXP (x, 0);
5198 rtx true_rtx = XEXP (x, 1);
5199 rtx false_rtx = XEXP (x, 2);
5200 enum rtx_code true_code = GET_CODE (cond);
5201 int comparison_p = COMPARISON_P (cond);
5202 rtx temp;
5203 int i;
5204 enum rtx_code false_code;
5205 rtx reversed;
5207 /* Simplify storing of the truth value. */
5208 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5209 return simplify_gen_relational (true_code, mode, VOIDmode,
5210 XEXP (cond, 0), XEXP (cond, 1));
5212 /* Also when the truth value has to be reversed. */
5213 if (comparison_p
5214 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5215 && (reversed = reversed_comparison (cond, mode)))
5216 return reversed;
5218 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5219 in it is being compared against certain values. Get the true and false
5220 comparisons and see if that says anything about the value of each arm. */
5222 if (comparison_p
5223 && ((false_code = reversed_comparison_code (cond, NULL))
5224 != UNKNOWN)
5225 && REG_P (XEXP (cond, 0)))
5227 HOST_WIDE_INT nzb;
5228 rtx from = XEXP (cond, 0);
5229 rtx true_val = XEXP (cond, 1);
5230 rtx false_val = true_val;
5231 int swapped = 0;
5233 /* If FALSE_CODE is EQ, swap the codes and arms. */
5235 if (false_code == EQ)
5237 swapped = 1, true_code = EQ, false_code = NE;
5238 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5241 /* If we are comparing against zero and the expression being tested has
5242 only a single bit that might be nonzero, that is its value when it is
5243 not equal to zero. Similarly if it is known to be -1 or 0. */
5245 if (true_code == EQ && true_val == const0_rtx
5246 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5248 false_code = EQ;
5249 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5251 else if (true_code == EQ && true_val == const0_rtx
5252 && (num_sign_bit_copies (from, GET_MODE (from))
5253 == GET_MODE_BITSIZE (GET_MODE (from))))
5255 false_code = EQ;
5256 false_val = constm1_rtx;
5259 /* Now simplify an arm if we know the value of the register in the
5260 branch and it is used in the arm. Be careful due to the potential
5261 of locally-shared RTL. */
5263 if (reg_mentioned_p (from, true_rtx))
5264 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5265 from, true_val),
5266 pc_rtx, pc_rtx, 0, 0);
5267 if (reg_mentioned_p (from, false_rtx))
5268 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5269 from, false_val),
5270 pc_rtx, pc_rtx, 0, 0);
5272 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5273 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5275 true_rtx = XEXP (x, 1);
5276 false_rtx = XEXP (x, 2);
5277 true_code = GET_CODE (cond);
5280 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5281 reversed, do so to avoid needing two sets of patterns for
5282 subtract-and-branch insns. Similarly if we have a constant in the true
5283 arm, the false arm is the same as the first operand of the comparison, or
5284 the false arm is more complicated than the true arm. */
5286 if (comparison_p
5287 && reversed_comparison_code (cond, NULL) != UNKNOWN
5288 && (true_rtx == pc_rtx
5289 || (CONSTANT_P (true_rtx)
5290 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
5291 || true_rtx == const0_rtx
5292 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5293 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5294 && !OBJECT_P (false_rtx))
5295 || reg_mentioned_p (true_rtx, false_rtx)
5296 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5298 true_code = reversed_comparison_code (cond, NULL);
5299 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5300 SUBST (XEXP (x, 1), false_rtx);
5301 SUBST (XEXP (x, 2), true_rtx);
5303 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5304 cond = XEXP (x, 0);
5306 /* It is possible that the conditional has been simplified out. */
5307 true_code = GET_CODE (cond);
5308 comparison_p = COMPARISON_P (cond);
5311 /* If the two arms are identical, we don't need the comparison. */
5313 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5314 return true_rtx;
5316 /* Convert a == b ? b : a to "a". */
5317 if (true_code == EQ && ! side_effects_p (cond)
5318 && !HONOR_NANS (mode)
5319 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5320 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5321 return false_rtx;
5322 else if (true_code == NE && ! side_effects_p (cond)
5323 && !HONOR_NANS (mode)
5324 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5325 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5326 return true_rtx;
5328 /* Look for cases where we have (abs x) or (neg (abs X)). */
5330 if (GET_MODE_CLASS (mode) == MODE_INT
5331 && GET_CODE (false_rtx) == NEG
5332 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5333 && comparison_p
5334 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5335 && ! side_effects_p (true_rtx))
5336 switch (true_code)
5338 case GT:
5339 case GE:
5340 return simplify_gen_unary (ABS, mode, true_rtx, mode);
5341 case LT:
5342 case LE:
5343 return
5344 simplify_gen_unary (NEG, mode,
5345 simplify_gen_unary (ABS, mode, true_rtx, mode),
5346 mode);
5347 default:
5348 break;
5351 /* Look for MIN or MAX. */
5353 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5354 && comparison_p
5355 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5356 && rtx_equal_p (XEXP (cond, 1), false_rtx)
5357 && ! side_effects_p (cond))
5358 switch (true_code)
5360 case GE:
5361 case GT:
5362 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5363 case LE:
5364 case LT:
5365 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5366 case GEU:
5367 case GTU:
5368 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5369 case LEU:
5370 case LTU:
5371 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5372 default:
5373 break;
5376 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5377 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5378 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5379 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5380 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5381 neither 1 or -1, but it isn't worth checking for. */
5383 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5384 && comparison_p
5385 && GET_MODE_CLASS (mode) == MODE_INT
5386 && ! side_effects_p (x))
5388 rtx t = make_compound_operation (true_rtx, SET);
5389 rtx f = make_compound_operation (false_rtx, SET);
5390 rtx cond_op0 = XEXP (cond, 0);
5391 rtx cond_op1 = XEXP (cond, 1);
5392 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5393 enum machine_mode m = mode;
5394 rtx z = 0, c1 = NULL_RTX;
5396 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5397 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5398 || GET_CODE (t) == ASHIFT
5399 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5400 && rtx_equal_p (XEXP (t, 0), f))
5401 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5403 /* If an identity-zero op is commutative, check whether there
5404 would be a match if we swapped the operands. */
5405 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5406 || GET_CODE (t) == XOR)
5407 && rtx_equal_p (XEXP (t, 1), f))
5408 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5409 else if (GET_CODE (t) == SIGN_EXTEND
5410 && (GET_CODE (XEXP (t, 0)) == PLUS
5411 || GET_CODE (XEXP (t, 0)) == MINUS
5412 || GET_CODE (XEXP (t, 0)) == IOR
5413 || GET_CODE (XEXP (t, 0)) == XOR
5414 || GET_CODE (XEXP (t, 0)) == ASHIFT
5415 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5416 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5417 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5418 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5419 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5420 && (num_sign_bit_copies (f, GET_MODE (f))
5421 > (unsigned int)
5422 (GET_MODE_BITSIZE (mode)
5423 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5425 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5426 extend_op = SIGN_EXTEND;
5427 m = GET_MODE (XEXP (t, 0));
5429 else if (GET_CODE (t) == SIGN_EXTEND
5430 && (GET_CODE (XEXP (t, 0)) == PLUS
5431 || GET_CODE (XEXP (t, 0)) == IOR
5432 || GET_CODE (XEXP (t, 0)) == XOR)
5433 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5434 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5435 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5436 && (num_sign_bit_copies (f, GET_MODE (f))
5437 > (unsigned int)
5438 (GET_MODE_BITSIZE (mode)
5439 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5441 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5442 extend_op = SIGN_EXTEND;
5443 m = GET_MODE (XEXP (t, 0));
5445 else if (GET_CODE (t) == ZERO_EXTEND
5446 && (GET_CODE (XEXP (t, 0)) == PLUS
5447 || GET_CODE (XEXP (t, 0)) == MINUS
5448 || GET_CODE (XEXP (t, 0)) == IOR
5449 || GET_CODE (XEXP (t, 0)) == XOR
5450 || GET_CODE (XEXP (t, 0)) == ASHIFT
5451 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5452 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5453 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5454 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5455 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5456 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5457 && ((nonzero_bits (f, GET_MODE (f))
5458 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5459 == 0))
5461 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5462 extend_op = ZERO_EXTEND;
5463 m = GET_MODE (XEXP (t, 0));
5465 else if (GET_CODE (t) == ZERO_EXTEND
5466 && (GET_CODE (XEXP (t, 0)) == PLUS
5467 || GET_CODE (XEXP (t, 0)) == IOR
5468 || GET_CODE (XEXP (t, 0)) == XOR)
5469 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5470 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5471 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5472 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5473 && ((nonzero_bits (f, GET_MODE (f))
5474 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5475 == 0))
5477 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5478 extend_op = ZERO_EXTEND;
5479 m = GET_MODE (XEXP (t, 0));
5482 if (z)
5484 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5485 cond_op0, cond_op1),
5486 pc_rtx, pc_rtx, 0, 0);
5487 temp = simplify_gen_binary (MULT, m, temp,
5488 simplify_gen_binary (MULT, m, c1,
5489 const_true_rtx));
5490 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5491 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5493 if (extend_op != UNKNOWN)
5494 temp = simplify_gen_unary (extend_op, mode, temp, m);
5496 return temp;
5500 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5501 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5502 negation of a single bit, we can convert this operation to a shift. We
5503 can actually do this more generally, but it doesn't seem worth it. */
5505 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5506 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5507 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5508 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5509 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5510 == GET_MODE_BITSIZE (mode))
5511 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5512 return
5513 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5514 gen_lowpart (mode, XEXP (cond, 0)), i);
5516 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5517 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5518 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5519 && GET_MODE (XEXP (cond, 0)) == mode
5520 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5521 == nonzero_bits (XEXP (cond, 0), mode)
5522 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5523 return XEXP (cond, 0);
5525 return x;
5528 /* Simplify X, a SET expression. Return the new expression. */
5530 static rtx
5531 simplify_set (rtx x)
5533 rtx src = SET_SRC (x);
5534 rtx dest = SET_DEST (x);
5535 enum machine_mode mode
5536 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5537 rtx other_insn;
5538 rtx *cc_use;
5540 /* (set (pc) (return)) gets written as (return). */
5541 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5542 return src;
5544 /* Now that we know for sure which bits of SRC we are using, see if we can
5545 simplify the expression for the object knowing that we only need the
5546 low-order bits. */
5548 if (GET_MODE_CLASS (mode) == MODE_INT
5549 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5551 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5552 SUBST (SET_SRC (x), src);
5555 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5556 the comparison result and try to simplify it unless we already have used
5557 undobuf.other_insn. */
5558 if ((GET_MODE_CLASS (mode) == MODE_CC
5559 || GET_CODE (src) == COMPARE
5560 || CC0_P (dest))
5561 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5562 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5563 && COMPARISON_P (*cc_use)
5564 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5566 enum rtx_code old_code = GET_CODE (*cc_use);
5567 enum rtx_code new_code;
5568 rtx op0, op1, tmp;
5569 int other_changed = 0;
5570 enum machine_mode compare_mode = GET_MODE (dest);
5572 if (GET_CODE (src) == COMPARE)
5573 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5574 else
5575 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5577 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5578 op0, op1);
5579 if (!tmp)
5580 new_code = old_code;
5581 else if (!CONSTANT_P (tmp))
5583 new_code = GET_CODE (tmp);
5584 op0 = XEXP (tmp, 0);
5585 op1 = XEXP (tmp, 1);
5587 else
5589 rtx pat = PATTERN (other_insn);
5590 undobuf.other_insn = other_insn;
5591 SUBST (*cc_use, tmp);
5593 /* Attempt to simplify CC user. */
5594 if (GET_CODE (pat) == SET)
5596 rtx new = simplify_rtx (SET_SRC (pat));
5597 if (new != NULL_RTX)
5598 SUBST (SET_SRC (pat), new);
5601 /* Convert X into a no-op move. */
5602 SUBST (SET_DEST (x), pc_rtx);
5603 SUBST (SET_SRC (x), pc_rtx);
5604 return x;
5607 /* Simplify our comparison, if possible. */
5608 new_code = simplify_comparison (new_code, &op0, &op1);
5610 #ifdef SELECT_CC_MODE
5611 /* If this machine has CC modes other than CCmode, check to see if we
5612 need to use a different CC mode here. */
5613 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5614 compare_mode = GET_MODE (op0);
5615 else
5616 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5618 #ifndef HAVE_cc0
5619 /* If the mode changed, we have to change SET_DEST, the mode in the
5620 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5621 a hard register, just build new versions with the proper mode. If it
5622 is a pseudo, we lose unless it is only time we set the pseudo, in
5623 which case we can safely change its mode. */
5624 if (compare_mode != GET_MODE (dest))
5626 if (can_change_dest_mode (dest, 0, compare_mode))
5628 unsigned int regno = REGNO (dest);
5629 rtx new_dest;
5631 if (regno < FIRST_PSEUDO_REGISTER)
5632 new_dest = gen_rtx_REG (compare_mode, regno);
5633 else
5635 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5636 new_dest = regno_reg_rtx[regno];
5639 SUBST (SET_DEST (x), new_dest);
5640 SUBST (XEXP (*cc_use, 0), new_dest);
5641 other_changed = 1;
5643 dest = new_dest;
5646 #endif /* cc0 */
5647 #endif /* SELECT_CC_MODE */
5649 /* If the code changed, we have to build a new comparison in
5650 undobuf.other_insn. */
5651 if (new_code != old_code)
5653 int other_changed_previously = other_changed;
5654 unsigned HOST_WIDE_INT mask;
5656 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5657 dest, const0_rtx));
5658 other_changed = 1;
5660 /* If the only change we made was to change an EQ into an NE or
5661 vice versa, OP0 has only one bit that might be nonzero, and OP1
5662 is zero, check if changing the user of the condition code will
5663 produce a valid insn. If it won't, we can keep the original code
5664 in that insn by surrounding our operation with an XOR. */
5666 if (((old_code == NE && new_code == EQ)
5667 || (old_code == EQ && new_code == NE))
5668 && ! other_changed_previously && op1 == const0_rtx
5669 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5670 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5672 rtx pat = PATTERN (other_insn), note = 0;
5674 if ((recog_for_combine (&pat, other_insn, &note) < 0
5675 && ! check_asm_operands (pat)))
5677 PUT_CODE (*cc_use, old_code);
5678 other_changed = 0;
5680 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5681 op0, GEN_INT (mask));
5686 if (other_changed)
5687 undobuf.other_insn = other_insn;
5689 #ifdef HAVE_cc0
5690 /* If we are now comparing against zero, change our source if
5691 needed. If we do not use cc0, we always have a COMPARE. */
5692 if (op1 == const0_rtx && dest == cc0_rtx)
5694 SUBST (SET_SRC (x), op0);
5695 src = op0;
5697 else
5698 #endif
5700 /* Otherwise, if we didn't previously have a COMPARE in the
5701 correct mode, we need one. */
5702 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5704 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5705 src = SET_SRC (x);
5707 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5709 SUBST (SET_SRC (x), op0);
5710 src = SET_SRC (x);
5712 /* Otherwise, update the COMPARE if needed. */
5713 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
5715 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5716 src = SET_SRC (x);
5719 else
5721 /* Get SET_SRC in a form where we have placed back any
5722 compound expressions. Then do the checks below. */
5723 src = make_compound_operation (src, SET);
5724 SUBST (SET_SRC (x), src);
5727 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5728 and X being a REG or (subreg (reg)), we may be able to convert this to
5729 (set (subreg:m2 x) (op)).
5731 We can always do this if M1 is narrower than M2 because that means that
5732 we only care about the low bits of the result.
5734 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5735 perform a narrower operation than requested since the high-order bits will
5736 be undefined. On machine where it is defined, this transformation is safe
5737 as long as M1 and M2 have the same number of words. */
5739 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5740 && !OBJECT_P (SUBREG_REG (src))
5741 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5742 / UNITS_PER_WORD)
5743 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5744 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5745 #ifndef WORD_REGISTER_OPERATIONS
5746 && (GET_MODE_SIZE (GET_MODE (src))
5747 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5748 #endif
5749 #ifdef CANNOT_CHANGE_MODE_CLASS
5750 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5751 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5752 GET_MODE (SUBREG_REG (src)),
5753 GET_MODE (src)))
5754 #endif
5755 && (REG_P (dest)
5756 || (GET_CODE (dest) == SUBREG
5757 && REG_P (SUBREG_REG (dest)))))
5759 SUBST (SET_DEST (x),
5760 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5761 dest));
5762 SUBST (SET_SRC (x), SUBREG_REG (src));
5764 src = SET_SRC (x), dest = SET_DEST (x);
5767 #ifdef HAVE_cc0
5768 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5769 in SRC. */
5770 if (dest == cc0_rtx
5771 && GET_CODE (src) == SUBREG
5772 && subreg_lowpart_p (src)
5773 && (GET_MODE_BITSIZE (GET_MODE (src))
5774 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5776 rtx inner = SUBREG_REG (src);
5777 enum machine_mode inner_mode = GET_MODE (inner);
5779 /* Here we make sure that we don't have a sign bit on. */
5780 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5781 && (nonzero_bits (inner, inner_mode)
5782 < ((unsigned HOST_WIDE_INT) 1
5783 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5785 SUBST (SET_SRC (x), inner);
5786 src = SET_SRC (x);
5789 #endif
5791 #ifdef LOAD_EXTEND_OP
5792 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5793 would require a paradoxical subreg. Replace the subreg with a
5794 zero_extend to avoid the reload that would otherwise be required. */
5796 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5797 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5798 && SUBREG_BYTE (src) == 0
5799 && (GET_MODE_SIZE (GET_MODE (src))
5800 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5801 && MEM_P (SUBREG_REG (src)))
5803 SUBST (SET_SRC (x),
5804 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5805 GET_MODE (src), SUBREG_REG (src)));
5807 src = SET_SRC (x);
5809 #endif
5811 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5812 are comparing an item known to be 0 or -1 against 0, use a logical
5813 operation instead. Check for one of the arms being an IOR of the other
5814 arm with some value. We compute three terms to be IOR'ed together. In
5815 practice, at most two will be nonzero. Then we do the IOR's. */
5817 if (GET_CODE (dest) != PC
5818 && GET_CODE (src) == IF_THEN_ELSE
5819 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5820 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5821 && XEXP (XEXP (src, 0), 1) == const0_rtx
5822 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5823 #ifdef HAVE_conditional_move
5824 && ! can_conditionally_move_p (GET_MODE (src))
5825 #endif
5826 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5827 GET_MODE (XEXP (XEXP (src, 0), 0)))
5828 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5829 && ! side_effects_p (src))
5831 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5832 ? XEXP (src, 1) : XEXP (src, 2));
5833 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5834 ? XEXP (src, 2) : XEXP (src, 1));
5835 rtx term1 = const0_rtx, term2, term3;
5837 if (GET_CODE (true_rtx) == IOR
5838 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5839 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5840 else if (GET_CODE (true_rtx) == IOR
5841 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5842 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5843 else if (GET_CODE (false_rtx) == IOR
5844 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5845 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5846 else if (GET_CODE (false_rtx) == IOR
5847 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5848 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5850 term2 = simplify_gen_binary (AND, GET_MODE (src),
5851 XEXP (XEXP (src, 0), 0), true_rtx);
5852 term3 = simplify_gen_binary (AND, GET_MODE (src),
5853 simplify_gen_unary (NOT, GET_MODE (src),
5854 XEXP (XEXP (src, 0), 0),
5855 GET_MODE (src)),
5856 false_rtx);
5858 SUBST (SET_SRC (x),
5859 simplify_gen_binary (IOR, GET_MODE (src),
5860 simplify_gen_binary (IOR, GET_MODE (src),
5861 term1, term2),
5862 term3));
5864 src = SET_SRC (x);
5867 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5868 whole thing fail. */
5869 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5870 return src;
5871 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5872 return dest;
5873 else
5874 /* Convert this into a field assignment operation, if possible. */
5875 return make_field_assignment (x);
5878 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5879 result. */
5881 static rtx
5882 simplify_logical (rtx x)
5884 enum machine_mode mode = GET_MODE (x);
5885 rtx op0 = XEXP (x, 0);
5886 rtx op1 = XEXP (x, 1);
5888 switch (GET_CODE (x))
5890 case AND:
5891 /* We can call simplify_and_const_int only if we don't lose
5892 any (sign) bits when converting INTVAL (op1) to
5893 "unsigned HOST_WIDE_INT". */
5894 if (GET_CODE (op1) == CONST_INT
5895 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5896 || INTVAL (op1) > 0))
5898 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5899 if (GET_CODE (x) != AND)
5900 return x;
5902 op0 = XEXP (x, 0);
5903 op1 = XEXP (x, 1);
5906 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5907 apply the distributive law and then the inverse distributive
5908 law to see if things simplify. */
5909 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5911 rtx result = distribute_and_simplify_rtx (x, 0);
5912 if (result)
5913 return result;
5915 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5917 rtx result = distribute_and_simplify_rtx (x, 1);
5918 if (result)
5919 return result;
5921 break;
5923 case IOR:
5924 /* If we have (ior (and A B) C), apply the distributive law and then
5925 the inverse distributive law to see if things simplify. */
5927 if (GET_CODE (op0) == AND)
5929 rtx result = distribute_and_simplify_rtx (x, 0);
5930 if (result)
5931 return result;
5934 if (GET_CODE (op1) == AND)
5936 rtx result = distribute_and_simplify_rtx (x, 1);
5937 if (result)
5938 return result;
5940 break;
5942 default:
5943 gcc_unreachable ();
5946 return x;
5949 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5950 operations" because they can be replaced with two more basic operations.
5951 ZERO_EXTEND is also considered "compound" because it can be replaced with
5952 an AND operation, which is simpler, though only one operation.
5954 The function expand_compound_operation is called with an rtx expression
5955 and will convert it to the appropriate shifts and AND operations,
5956 simplifying at each stage.
5958 The function make_compound_operation is called to convert an expression
5959 consisting of shifts and ANDs into the equivalent compound expression.
5960 It is the inverse of this function, loosely speaking. */
5962 static rtx
5963 expand_compound_operation (rtx x)
5965 unsigned HOST_WIDE_INT pos = 0, len;
5966 int unsignedp = 0;
5967 unsigned int modewidth;
5968 rtx tem;
5970 switch (GET_CODE (x))
5972 case ZERO_EXTEND:
5973 unsignedp = 1;
5974 case SIGN_EXTEND:
5975 /* We can't necessarily use a const_int for a multiword mode;
5976 it depends on implicitly extending the value.
5977 Since we don't know the right way to extend it,
5978 we can't tell whether the implicit way is right.
5980 Even for a mode that is no wider than a const_int,
5981 we can't win, because we need to sign extend one of its bits through
5982 the rest of it, and we don't know which bit. */
5983 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5984 return x;
5986 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5987 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5988 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5989 reloaded. If not for that, MEM's would very rarely be safe.
5991 Reject MODEs bigger than a word, because we might not be able
5992 to reference a two-register group starting with an arbitrary register
5993 (and currently gen_lowpart might crash for a SUBREG). */
5995 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5996 return x;
5998 /* Reject MODEs that aren't scalar integers because turning vector
5999 or complex modes into shifts causes problems. */
6001 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6002 return x;
6004 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6005 /* If the inner object has VOIDmode (the only way this can happen
6006 is if it is an ASM_OPERANDS), we can't do anything since we don't
6007 know how much masking to do. */
6008 if (len == 0)
6009 return x;
6011 break;
6013 case ZERO_EXTRACT:
6014 unsignedp = 1;
6016 /* ... fall through ... */
6018 case SIGN_EXTRACT:
6019 /* If the operand is a CLOBBER, just return it. */
6020 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6021 return XEXP (x, 0);
6023 if (GET_CODE (XEXP (x, 1)) != CONST_INT
6024 || GET_CODE (XEXP (x, 2)) != CONST_INT
6025 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6026 return x;
6028 /* Reject MODEs that aren't scalar integers because turning vector
6029 or complex modes into shifts causes problems. */
6031 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6032 return x;
6034 len = INTVAL (XEXP (x, 1));
6035 pos = INTVAL (XEXP (x, 2));
6037 /* This should stay within the object being extracted, fail otherwise. */
6038 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6039 return x;
6041 if (BITS_BIG_ENDIAN)
6042 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6044 break;
6046 default:
6047 return x;
6049 /* Convert sign extension to zero extension, if we know that the high
6050 bit is not set, as this is easier to optimize. It will be converted
6051 back to cheaper alternative in make_extraction. */
6052 if (GET_CODE (x) == SIGN_EXTEND
6053 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6054 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6055 & ~(((unsigned HOST_WIDE_INT)
6056 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6057 >> 1))
6058 == 0)))
6060 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6061 rtx temp2 = expand_compound_operation (temp);
6063 /* Make sure this is a profitable operation. */
6064 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
6065 return temp2;
6066 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
6067 return temp;
6068 else
6069 return x;
6072 /* We can optimize some special cases of ZERO_EXTEND. */
6073 if (GET_CODE (x) == ZERO_EXTEND)
6075 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6076 know that the last value didn't have any inappropriate bits
6077 set. */
6078 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6079 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6080 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6081 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6082 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6083 return XEXP (XEXP (x, 0), 0);
6085 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6086 if (GET_CODE (XEXP (x, 0)) == SUBREG
6087 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6088 && subreg_lowpart_p (XEXP (x, 0))
6089 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6090 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6091 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6092 return SUBREG_REG (XEXP (x, 0));
6094 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6095 is a comparison and STORE_FLAG_VALUE permits. This is like
6096 the first case, but it works even when GET_MODE (x) is larger
6097 than HOST_WIDE_INT. */
6098 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6099 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6100 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6101 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6102 <= HOST_BITS_PER_WIDE_INT)
6103 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6104 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6105 return XEXP (XEXP (x, 0), 0);
6107 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6108 if (GET_CODE (XEXP (x, 0)) == SUBREG
6109 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6110 && subreg_lowpart_p (XEXP (x, 0))
6111 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6112 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6113 <= HOST_BITS_PER_WIDE_INT)
6114 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6115 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6116 return SUBREG_REG (XEXP (x, 0));
6120 /* If we reach here, we want to return a pair of shifts. The inner
6121 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6122 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6123 logical depending on the value of UNSIGNEDP.
6125 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6126 converted into an AND of a shift.
6128 We must check for the case where the left shift would have a negative
6129 count. This can happen in a case like (x >> 31) & 255 on machines
6130 that can't shift by a constant. On those machines, we would first
6131 combine the shift with the AND to produce a variable-position
6132 extraction. Then the constant of 31 would be substituted in to produce
6133 a such a position. */
6135 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6136 if (modewidth + len >= pos)
6138 enum machine_mode mode = GET_MODE (x);
6139 tem = gen_lowpart (mode, XEXP (x, 0));
6140 if (!tem || GET_CODE (tem) == CLOBBER)
6141 return x;
6142 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6143 tem, modewidth - pos - len);
6144 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6145 mode, tem, modewidth - len);
6147 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6148 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6149 simplify_shift_const (NULL_RTX, LSHIFTRT,
6150 GET_MODE (x),
6151 XEXP (x, 0), pos),
6152 ((HOST_WIDE_INT) 1 << len) - 1);
6153 else
6154 /* Any other cases we can't handle. */
6155 return x;
6157 /* If we couldn't do this for some reason, return the original
6158 expression. */
6159 if (GET_CODE (tem) == CLOBBER)
6160 return x;
6162 return tem;
6165 /* X is a SET which contains an assignment of one object into
6166 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6167 or certain SUBREGS). If possible, convert it into a series of
6168 logical operations.
6170 We half-heartedly support variable positions, but do not at all
6171 support variable lengths. */
6173 static const_rtx
6174 expand_field_assignment (const_rtx x)
6176 rtx inner;
6177 rtx pos; /* Always counts from low bit. */
6178 int len;
6179 rtx mask, cleared, masked;
6180 enum machine_mode compute_mode;
6182 /* Loop until we find something we can't simplify. */
6183 while (1)
6185 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6186 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6188 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6189 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6190 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6192 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6193 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6195 inner = XEXP (SET_DEST (x), 0);
6196 len = INTVAL (XEXP (SET_DEST (x), 1));
6197 pos = XEXP (SET_DEST (x), 2);
6199 /* A constant position should stay within the width of INNER. */
6200 if (GET_CODE (pos) == CONST_INT
6201 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6202 break;
6204 if (BITS_BIG_ENDIAN)
6206 if (GET_CODE (pos) == CONST_INT)
6207 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6208 - INTVAL (pos));
6209 else if (GET_CODE (pos) == MINUS
6210 && GET_CODE (XEXP (pos, 1)) == CONST_INT
6211 && (INTVAL (XEXP (pos, 1))
6212 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6213 /* If position is ADJUST - X, new position is X. */
6214 pos = XEXP (pos, 0);
6215 else
6216 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6217 GEN_INT (GET_MODE_BITSIZE (
6218 GET_MODE (inner))
6219 - len),
6220 pos);
6224 /* A SUBREG between two modes that occupy the same numbers of words
6225 can be done by moving the SUBREG to the source. */
6226 else if (GET_CODE (SET_DEST (x)) == SUBREG
6227 /* We need SUBREGs to compute nonzero_bits properly. */
6228 && nonzero_sign_valid
6229 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6230 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6231 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6232 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6234 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6235 gen_lowpart
6236 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6237 SET_SRC (x)));
6238 continue;
6240 else
6241 break;
6243 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6244 inner = SUBREG_REG (inner);
6246 compute_mode = GET_MODE (inner);
6248 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6249 if (! SCALAR_INT_MODE_P (compute_mode))
6251 enum machine_mode imode;
6253 /* Don't do anything for vector or complex integral types. */
6254 if (! FLOAT_MODE_P (compute_mode))
6255 break;
6257 /* Try to find an integral mode to pun with. */
6258 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6259 if (imode == BLKmode)
6260 break;
6262 compute_mode = imode;
6263 inner = gen_lowpart (imode, inner);
6266 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6267 if (len >= HOST_BITS_PER_WIDE_INT)
6268 break;
6270 /* Now compute the equivalent expression. Make a copy of INNER
6271 for the SET_DEST in case it is a MEM into which we will substitute;
6272 we don't want shared RTL in that case. */
6273 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6274 cleared = simplify_gen_binary (AND, compute_mode,
6275 simplify_gen_unary (NOT, compute_mode,
6276 simplify_gen_binary (ASHIFT,
6277 compute_mode,
6278 mask, pos),
6279 compute_mode),
6280 inner);
6281 masked = simplify_gen_binary (ASHIFT, compute_mode,
6282 simplify_gen_binary (
6283 AND, compute_mode,
6284 gen_lowpart (compute_mode, SET_SRC (x)),
6285 mask),
6286 pos);
6288 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6289 simplify_gen_binary (IOR, compute_mode,
6290 cleared, masked));
6293 return x;
6296 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6297 it is an RTX that represents a variable starting position; otherwise,
6298 POS is the (constant) starting bit position (counted from the LSB).
6300 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6301 signed reference.
6303 IN_DEST is nonzero if this is a reference in the destination of a
6304 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6305 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6306 be used.
6308 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6309 ZERO_EXTRACT should be built even for bits starting at bit 0.
6311 MODE is the desired mode of the result (if IN_DEST == 0).
6313 The result is an RTX for the extraction or NULL_RTX if the target
6314 can't handle it. */
6316 static rtx
6317 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6318 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6319 int in_dest, int in_compare)
6321 /* This mode describes the size of the storage area
6322 to fetch the overall value from. Within that, we
6323 ignore the POS lowest bits, etc. */
6324 enum machine_mode is_mode = GET_MODE (inner);
6325 enum machine_mode inner_mode;
6326 enum machine_mode wanted_inner_mode;
6327 enum machine_mode wanted_inner_reg_mode = word_mode;
6328 enum machine_mode pos_mode = word_mode;
6329 enum machine_mode extraction_mode = word_mode;
6330 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6331 rtx new = 0;
6332 rtx orig_pos_rtx = pos_rtx;
6333 HOST_WIDE_INT orig_pos;
6335 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6337 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6338 consider just the QI as the memory to extract from.
6339 The subreg adds or removes high bits; its mode is
6340 irrelevant to the meaning of this extraction,
6341 since POS and LEN count from the lsb. */
6342 if (MEM_P (SUBREG_REG (inner)))
6343 is_mode = GET_MODE (SUBREG_REG (inner));
6344 inner = SUBREG_REG (inner);
6346 else if (GET_CODE (inner) == ASHIFT
6347 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6348 && pos_rtx == 0 && pos == 0
6349 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6351 /* We're extracting the least significant bits of an rtx
6352 (ashift X (const_int C)), where LEN > C. Extract the
6353 least significant (LEN - C) bits of X, giving an rtx
6354 whose mode is MODE, then shift it left C times. */
6355 new = make_extraction (mode, XEXP (inner, 0),
6356 0, 0, len - INTVAL (XEXP (inner, 1)),
6357 unsignedp, in_dest, in_compare);
6358 if (new != 0)
6359 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6362 inner_mode = GET_MODE (inner);
6364 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6365 pos = INTVAL (pos_rtx), pos_rtx = 0;
6367 /* See if this can be done without an extraction. We never can if the
6368 width of the field is not the same as that of some integer mode. For
6369 registers, we can only avoid the extraction if the position is at the
6370 low-order bit and this is either not in the destination or we have the
6371 appropriate STRICT_LOW_PART operation available.
6373 For MEM, we can avoid an extract if the field starts on an appropriate
6374 boundary and we can change the mode of the memory reference. */
6376 if (tmode != BLKmode
6377 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6378 && !MEM_P (inner)
6379 && (inner_mode == tmode
6380 || !REG_P (inner)
6381 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6382 GET_MODE_BITSIZE (inner_mode))
6383 || reg_truncated_to_mode (tmode, inner))
6384 && (! in_dest
6385 || (REG_P (inner)
6386 && have_insn_for (STRICT_LOW_PART, tmode))))
6387 || (MEM_P (inner) && pos_rtx == 0
6388 && (pos
6389 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6390 : BITS_PER_UNIT)) == 0
6391 /* We can't do this if we are widening INNER_MODE (it
6392 may not be aligned, for one thing). */
6393 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6394 && (inner_mode == tmode
6395 || (! mode_dependent_address_p (XEXP (inner, 0))
6396 && ! MEM_VOLATILE_P (inner))))))
6398 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6399 field. If the original and current mode are the same, we need not
6400 adjust the offset. Otherwise, we do if bytes big endian.
6402 If INNER is not a MEM, get a piece consisting of just the field
6403 of interest (in this case POS % BITS_PER_WORD must be 0). */
6405 if (MEM_P (inner))
6407 HOST_WIDE_INT offset;
6409 /* POS counts from lsb, but make OFFSET count in memory order. */
6410 if (BYTES_BIG_ENDIAN)
6411 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6412 else
6413 offset = pos / BITS_PER_UNIT;
6415 new = adjust_address_nv (inner, tmode, offset);
6417 else if (REG_P (inner))
6419 if (tmode != inner_mode)
6421 /* We can't call gen_lowpart in a DEST since we
6422 always want a SUBREG (see below) and it would sometimes
6423 return a new hard register. */
6424 if (pos || in_dest)
6426 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6428 if (WORDS_BIG_ENDIAN
6429 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6430 final_word = ((GET_MODE_SIZE (inner_mode)
6431 - GET_MODE_SIZE (tmode))
6432 / UNITS_PER_WORD) - final_word;
6434 final_word *= UNITS_PER_WORD;
6435 if (BYTES_BIG_ENDIAN &&
6436 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6437 final_word += (GET_MODE_SIZE (inner_mode)
6438 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6440 /* Avoid creating invalid subregs, for example when
6441 simplifying (x>>32)&255. */
6442 if (!validate_subreg (tmode, inner_mode, inner, final_word))
6443 return NULL_RTX;
6445 new = gen_rtx_SUBREG (tmode, inner, final_word);
6447 else
6448 new = gen_lowpart (tmode, inner);
6450 else
6451 new = inner;
6453 else
6454 new = force_to_mode (inner, tmode,
6455 len >= HOST_BITS_PER_WIDE_INT
6456 ? ~(unsigned HOST_WIDE_INT) 0
6457 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6460 /* If this extraction is going into the destination of a SET,
6461 make a STRICT_LOW_PART unless we made a MEM. */
6463 if (in_dest)
6464 return (MEM_P (new) ? new
6465 : (GET_CODE (new) != SUBREG
6466 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6467 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6469 if (mode == tmode)
6470 return new;
6472 if (GET_CODE (new) == CONST_INT)
6473 return gen_int_mode (INTVAL (new), mode);
6475 /* If we know that no extraneous bits are set, and that the high
6476 bit is not set, convert the extraction to the cheaper of
6477 sign and zero extension, that are equivalent in these cases. */
6478 if (flag_expensive_optimizations
6479 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6480 && ((nonzero_bits (new, tmode)
6481 & ~(((unsigned HOST_WIDE_INT)
6482 GET_MODE_MASK (tmode))
6483 >> 1))
6484 == 0)))
6486 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6487 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6489 /* Prefer ZERO_EXTENSION, since it gives more information to
6490 backends. */
6491 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6492 return temp;
6493 return temp1;
6496 /* Otherwise, sign- or zero-extend unless we already are in the
6497 proper mode. */
6499 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6500 mode, new));
6503 /* Unless this is a COMPARE or we have a funny memory reference,
6504 don't do anything with zero-extending field extracts starting at
6505 the low-order bit since they are simple AND operations. */
6506 if (pos_rtx == 0 && pos == 0 && ! in_dest
6507 && ! in_compare && unsignedp)
6508 return 0;
6510 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6511 if the position is not a constant and the length is not 1. In all
6512 other cases, we would only be going outside our object in cases when
6513 an original shift would have been undefined. */
6514 if (MEM_P (inner)
6515 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6516 || (pos_rtx != 0 && len != 1)))
6517 return 0;
6519 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6520 and the mode for the result. */
6521 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6523 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6524 pos_mode = mode_for_extraction (EP_insv, 2);
6525 extraction_mode = mode_for_extraction (EP_insv, 3);
6528 if (! in_dest && unsignedp
6529 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6531 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6532 pos_mode = mode_for_extraction (EP_extzv, 3);
6533 extraction_mode = mode_for_extraction (EP_extzv, 0);
6536 if (! in_dest && ! unsignedp
6537 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6539 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6540 pos_mode = mode_for_extraction (EP_extv, 3);
6541 extraction_mode = mode_for_extraction (EP_extv, 0);
6544 /* Never narrow an object, since that might not be safe. */
6546 if (mode != VOIDmode
6547 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6548 extraction_mode = mode;
6550 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6551 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6552 pos_mode = GET_MODE (pos_rtx);
6554 /* If this is not from memory, the desired mode is the preferred mode
6555 for an extraction pattern's first input operand, or word_mode if there
6556 is none. */
6557 if (!MEM_P (inner))
6558 wanted_inner_mode = wanted_inner_reg_mode;
6559 else
6561 /* Be careful not to go beyond the extracted object and maintain the
6562 natural alignment of the memory. */
6563 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6564 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6565 > GET_MODE_BITSIZE (wanted_inner_mode))
6567 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6568 gcc_assert (wanted_inner_mode != VOIDmode);
6571 /* If we have to change the mode of memory and cannot, the desired mode
6572 is EXTRACTION_MODE. */
6573 if (inner_mode != wanted_inner_mode
6574 && (mode_dependent_address_p (XEXP (inner, 0))
6575 || MEM_VOLATILE_P (inner)
6576 || pos_rtx))
6577 wanted_inner_mode = extraction_mode;
6580 orig_pos = pos;
6582 if (BITS_BIG_ENDIAN)
6584 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6585 BITS_BIG_ENDIAN style. If position is constant, compute new
6586 position. Otherwise, build subtraction.
6587 Note that POS is relative to the mode of the original argument.
6588 If it's a MEM we need to recompute POS relative to that.
6589 However, if we're extracting from (or inserting into) a register,
6590 we want to recompute POS relative to wanted_inner_mode. */
6591 int width = (MEM_P (inner)
6592 ? GET_MODE_BITSIZE (is_mode)
6593 : GET_MODE_BITSIZE (wanted_inner_mode));
6595 if (pos_rtx == 0)
6596 pos = width - len - pos;
6597 else
6598 pos_rtx
6599 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6600 /* POS may be less than 0 now, but we check for that below.
6601 Note that it can only be less than 0 if !MEM_P (inner). */
6604 /* If INNER has a wider mode, and this is a constant extraction, try to
6605 make it smaller and adjust the byte to point to the byte containing
6606 the value. */
6607 if (wanted_inner_mode != VOIDmode
6608 && inner_mode != wanted_inner_mode
6609 && ! pos_rtx
6610 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6611 && MEM_P (inner)
6612 && ! mode_dependent_address_p (XEXP (inner, 0))
6613 && ! MEM_VOLATILE_P (inner))
6615 int offset = 0;
6617 /* The computations below will be correct if the machine is big
6618 endian in both bits and bytes or little endian in bits and bytes.
6619 If it is mixed, we must adjust. */
6621 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6622 adjust OFFSET to compensate. */
6623 if (BYTES_BIG_ENDIAN
6624 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6625 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6627 /* We can now move to the desired byte. */
6628 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6629 * GET_MODE_SIZE (wanted_inner_mode);
6630 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6632 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6633 && is_mode != wanted_inner_mode)
6634 offset = (GET_MODE_SIZE (is_mode)
6635 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6637 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6640 /* If INNER is not memory, we can always get it into the proper mode. If we
6641 are changing its mode, POS must be a constant and smaller than the size
6642 of the new mode. */
6643 else if (!MEM_P (inner))
6645 if (GET_MODE (inner) != wanted_inner_mode
6646 && (pos_rtx != 0
6647 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6648 return 0;
6650 if (orig_pos < 0)
6651 return 0;
6653 inner = force_to_mode (inner, wanted_inner_mode,
6654 pos_rtx
6655 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6656 ? ~(unsigned HOST_WIDE_INT) 0
6657 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6658 << orig_pos),
6662 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6663 have to zero extend. Otherwise, we can just use a SUBREG. */
6664 if (pos_rtx != 0
6665 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6667 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6669 /* If we know that no extraneous bits are set, and that the high
6670 bit is not set, convert extraction to cheaper one - either
6671 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6672 cases. */
6673 if (flag_expensive_optimizations
6674 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6675 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6676 & ~(((unsigned HOST_WIDE_INT)
6677 GET_MODE_MASK (GET_MODE (pos_rtx)))
6678 >> 1))
6679 == 0)))
6681 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6683 /* Prefer ZERO_EXTENSION, since it gives more information to
6684 backends. */
6685 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6686 temp = temp1;
6688 pos_rtx = temp;
6690 else if (pos_rtx != 0
6691 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6692 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6694 /* Make POS_RTX unless we already have it and it is correct. If we don't
6695 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6696 be a CONST_INT. */
6697 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6698 pos_rtx = orig_pos_rtx;
6700 else if (pos_rtx == 0)
6701 pos_rtx = GEN_INT (pos);
6703 /* Make the required operation. See if we can use existing rtx. */
6704 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6705 extraction_mode, inner, GEN_INT (len), pos_rtx);
6706 if (! in_dest)
6707 new = gen_lowpart (mode, new);
6709 return new;
6712 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6713 with any other operations in X. Return X without that shift if so. */
6715 static rtx
6716 extract_left_shift (rtx x, int count)
6718 enum rtx_code code = GET_CODE (x);
6719 enum machine_mode mode = GET_MODE (x);
6720 rtx tem;
6722 switch (code)
6724 case ASHIFT:
6725 /* This is the shift itself. If it is wide enough, we will return
6726 either the value being shifted if the shift count is equal to
6727 COUNT or a shift for the difference. */
6728 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6729 && INTVAL (XEXP (x, 1)) >= count)
6730 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6731 INTVAL (XEXP (x, 1)) - count);
6732 break;
6734 case NEG: case NOT:
6735 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6736 return simplify_gen_unary (code, mode, tem, mode);
6738 break;
6740 case PLUS: case IOR: case XOR: case AND:
6741 /* If we can safely shift this constant and we find the inner shift,
6742 make a new operation. */
6743 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6744 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6745 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6746 return simplify_gen_binary (code, mode, tem,
6747 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6749 break;
6751 default:
6752 break;
6755 return 0;
6758 /* Look at the expression rooted at X. Look for expressions
6759 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6760 Form these expressions.
6762 Return the new rtx, usually just X.
6764 Also, for machines like the VAX that don't have logical shift insns,
6765 try to convert logical to arithmetic shift operations in cases where
6766 they are equivalent. This undoes the canonicalizations to logical
6767 shifts done elsewhere.
6769 We try, as much as possible, to re-use rtl expressions to save memory.
6771 IN_CODE says what kind of expression we are processing. Normally, it is
6772 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6773 being kludges), it is MEM. When processing the arguments of a comparison
6774 or a COMPARE against zero, it is COMPARE. */
6776 static rtx
6777 make_compound_operation (rtx x, enum rtx_code in_code)
6779 enum rtx_code code = GET_CODE (x);
6780 enum machine_mode mode = GET_MODE (x);
6781 int mode_width = GET_MODE_BITSIZE (mode);
6782 rtx rhs, lhs;
6783 enum rtx_code next_code;
6784 int i;
6785 rtx new = 0;
6786 rtx tem;
6787 const char *fmt;
6789 /* Select the code to be used in recursive calls. Once we are inside an
6790 address, we stay there. If we have a comparison, set to COMPARE,
6791 but once inside, go back to our default of SET. */
6793 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6794 : ((code == COMPARE || COMPARISON_P (x))
6795 && XEXP (x, 1) == const0_rtx) ? COMPARE
6796 : in_code == COMPARE ? SET : in_code);
6798 /* Process depending on the code of this operation. If NEW is set
6799 nonzero, it will be returned. */
6801 switch (code)
6803 case ASHIFT:
6804 /* Convert shifts by constants into multiplications if inside
6805 an address. */
6806 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6807 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6808 && INTVAL (XEXP (x, 1)) >= 0)
6810 new = make_compound_operation (XEXP (x, 0), next_code);
6811 new = gen_rtx_MULT (mode, new,
6812 GEN_INT ((HOST_WIDE_INT) 1
6813 << INTVAL (XEXP (x, 1))));
6815 break;
6817 case AND:
6818 /* If the second operand is not a constant, we can't do anything
6819 with it. */
6820 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6821 break;
6823 /* If the constant is a power of two minus one and the first operand
6824 is a logical right shift, make an extraction. */
6825 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6826 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6828 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6829 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6830 0, in_code == COMPARE);
6833 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6834 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6835 && subreg_lowpart_p (XEXP (x, 0))
6836 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6837 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6839 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6840 next_code);
6841 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6842 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6843 0, in_code == COMPARE);
6845 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6846 else if ((GET_CODE (XEXP (x, 0)) == XOR
6847 || GET_CODE (XEXP (x, 0)) == IOR)
6848 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6849 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6850 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6852 /* Apply the distributive law, and then try to make extractions. */
6853 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6854 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6855 XEXP (x, 1)),
6856 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6857 XEXP (x, 1)));
6858 new = make_compound_operation (new, in_code);
6861 /* If we are have (and (rotate X C) M) and C is larger than the number
6862 of bits in M, this is an extraction. */
6864 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6865 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6866 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6867 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6869 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6870 new = make_extraction (mode, new,
6871 (GET_MODE_BITSIZE (mode)
6872 - INTVAL (XEXP (XEXP (x, 0), 1))),
6873 NULL_RTX, i, 1, 0, in_code == COMPARE);
6876 /* On machines without logical shifts, if the operand of the AND is
6877 a logical shift and our mask turns off all the propagated sign
6878 bits, we can replace the logical shift with an arithmetic shift. */
6879 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6880 && !have_insn_for (LSHIFTRT, mode)
6881 && have_insn_for (ASHIFTRT, mode)
6882 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6883 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6884 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6885 && mode_width <= HOST_BITS_PER_WIDE_INT)
6887 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6889 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6890 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6891 SUBST (XEXP (x, 0),
6892 gen_rtx_ASHIFTRT (mode,
6893 make_compound_operation
6894 (XEXP (XEXP (x, 0), 0), next_code),
6895 XEXP (XEXP (x, 0), 1)));
6898 /* If the constant is one less than a power of two, this might be
6899 representable by an extraction even if no shift is present.
6900 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6901 we are in a COMPARE. */
6902 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6903 new = make_extraction (mode,
6904 make_compound_operation (XEXP (x, 0),
6905 next_code),
6906 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6908 /* If we are in a comparison and this is an AND with a power of two,
6909 convert this into the appropriate bit extract. */
6910 else if (in_code == COMPARE
6911 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6912 new = make_extraction (mode,
6913 make_compound_operation (XEXP (x, 0),
6914 next_code),
6915 i, NULL_RTX, 1, 1, 0, 1);
6917 break;
6919 case LSHIFTRT:
6920 /* If the sign bit is known to be zero, replace this with an
6921 arithmetic shift. */
6922 if (have_insn_for (ASHIFTRT, mode)
6923 && ! have_insn_for (LSHIFTRT, mode)
6924 && mode_width <= HOST_BITS_PER_WIDE_INT
6925 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6927 new = gen_rtx_ASHIFTRT (mode,
6928 make_compound_operation (XEXP (x, 0),
6929 next_code),
6930 XEXP (x, 1));
6931 break;
6934 /* ... fall through ... */
6936 case ASHIFTRT:
6937 lhs = XEXP (x, 0);
6938 rhs = XEXP (x, 1);
6940 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6941 this is a SIGN_EXTRACT. */
6942 if (GET_CODE (rhs) == CONST_INT
6943 && GET_CODE (lhs) == ASHIFT
6944 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6945 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6947 new = make_compound_operation (XEXP (lhs, 0), next_code);
6948 new = make_extraction (mode, new,
6949 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6950 NULL_RTX, mode_width - INTVAL (rhs),
6951 code == LSHIFTRT, 0, in_code == COMPARE);
6952 break;
6955 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6956 If so, try to merge the shifts into a SIGN_EXTEND. We could
6957 also do this for some cases of SIGN_EXTRACT, but it doesn't
6958 seem worth the effort; the case checked for occurs on Alpha. */
6960 if (!OBJECT_P (lhs)
6961 && ! (GET_CODE (lhs) == SUBREG
6962 && (OBJECT_P (SUBREG_REG (lhs))))
6963 && GET_CODE (rhs) == CONST_INT
6964 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6965 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6966 new = make_extraction (mode, make_compound_operation (new, next_code),
6967 0, NULL_RTX, mode_width - INTVAL (rhs),
6968 code == LSHIFTRT, 0, in_code == COMPARE);
6970 break;
6972 case SUBREG:
6973 /* Call ourselves recursively on the inner expression. If we are
6974 narrowing the object and it has a different RTL code from
6975 what it originally did, do this SUBREG as a force_to_mode. */
6977 tem = make_compound_operation (SUBREG_REG (x), in_code);
6980 rtx simplified;
6981 simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6982 SUBREG_BYTE (x));
6984 if (simplified)
6985 tem = simplified;
6987 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6988 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6989 && subreg_lowpart_p (x))
6991 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6994 /* If we have something other than a SUBREG, we might have
6995 done an expansion, so rerun ourselves. */
6996 if (GET_CODE (newer) != SUBREG)
6997 newer = make_compound_operation (newer, in_code);
6999 return newer;
7002 if (simplified)
7003 return tem;
7005 break;
7007 default:
7008 break;
7011 if (new)
7013 x = gen_lowpart (mode, new);
7014 code = GET_CODE (x);
7017 /* Now recursively process each operand of this operation. */
7018 fmt = GET_RTX_FORMAT (code);
7019 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7020 if (fmt[i] == 'e')
7022 new = make_compound_operation (XEXP (x, i), next_code);
7023 SUBST (XEXP (x, i), new);
7026 /* If this is a commutative operation, the changes to the operands
7027 may have made it noncanonical. */
7028 if (COMMUTATIVE_ARITH_P (x)
7029 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7031 tem = XEXP (x, 0);
7032 SUBST (XEXP (x, 0), XEXP (x, 1));
7033 SUBST (XEXP (x, 1), tem);
7036 return x;
7039 /* Given M see if it is a value that would select a field of bits
7040 within an item, but not the entire word. Return -1 if not.
7041 Otherwise, return the starting position of the field, where 0 is the
7042 low-order bit.
7044 *PLEN is set to the length of the field. */
7046 static int
7047 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7049 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7050 int pos = exact_log2 (m & -m);
7051 int len = 0;
7053 if (pos >= 0)
7054 /* Now shift off the low-order zero bits and see if we have a
7055 power of two minus 1. */
7056 len = exact_log2 ((m >> pos) + 1);
7058 if (len <= 0)
7059 pos = -1;
7061 *plen = len;
7062 return pos;
7065 /* If X refers to a register that equals REG in value, replace these
7066 references with REG. */
7067 static rtx
7068 canon_reg_for_combine (rtx x, rtx reg)
7070 rtx op0, op1, op2;
7071 const char *fmt;
7072 int i;
7073 bool copied;
7075 enum rtx_code code = GET_CODE (x);
7076 switch (GET_RTX_CLASS (code))
7078 case RTX_UNARY:
7079 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7080 if (op0 != XEXP (x, 0))
7081 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7082 GET_MODE (reg));
7083 break;
7085 case RTX_BIN_ARITH:
7086 case RTX_COMM_ARITH:
7087 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7088 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7089 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7090 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7091 break;
7093 case RTX_COMPARE:
7094 case RTX_COMM_COMPARE:
7095 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7096 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7097 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7098 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7099 GET_MODE (op0), op0, op1);
7100 break;
7102 case RTX_TERNARY:
7103 case RTX_BITFIELD_OPS:
7104 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7105 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7106 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7107 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7108 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7109 GET_MODE (op0), op0, op1, op2);
7111 case RTX_OBJ:
7112 if (REG_P (x))
7114 if (rtx_equal_p (get_last_value (reg), x)
7115 || rtx_equal_p (reg, get_last_value (x)))
7116 return reg;
7117 else
7118 break;
7121 /* fall through */
7123 default:
7124 fmt = GET_RTX_FORMAT (code);
7125 copied = false;
7126 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7127 if (fmt[i] == 'e')
7129 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7130 if (op != XEXP (x, i))
7132 if (!copied)
7134 copied = true;
7135 x = copy_rtx (x);
7137 XEXP (x, i) = op;
7140 else if (fmt[i] == 'E')
7142 int j;
7143 for (j = 0; j < XVECLEN (x, i); j++)
7145 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7146 if (op != XVECEXP (x, i, j))
7148 if (!copied)
7150 copied = true;
7151 x = copy_rtx (x);
7153 XVECEXP (x, i, j) = op;
7158 break;
7161 return x;
7164 /* Return X converted to MODE. If the value is already truncated to
7165 MODE we can just return a subreg even though in the general case we
7166 would need an explicit truncation. */
7168 static rtx
7169 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7171 if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
7172 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7173 GET_MODE_BITSIZE (GET_MODE (x)))
7174 || (REG_P (x) && reg_truncated_to_mode (mode, x)))
7175 return gen_lowpart (mode, x);
7176 else
7177 return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
7180 /* See if X can be simplified knowing that we will only refer to it in
7181 MODE and will only refer to those bits that are nonzero in MASK.
7182 If other bits are being computed or if masking operations are done
7183 that select a superset of the bits in MASK, they can sometimes be
7184 ignored.
7186 Return a possibly simplified expression, but always convert X to
7187 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7189 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7190 are all off in X. This is used when X will be complemented, by either
7191 NOT, NEG, or XOR. */
7193 static rtx
7194 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7195 int just_select)
7197 enum rtx_code code = GET_CODE (x);
7198 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7199 enum machine_mode op_mode;
7200 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7201 rtx op0, op1, temp;
7203 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7204 code below will do the wrong thing since the mode of such an
7205 expression is VOIDmode.
7207 Also do nothing if X is a CLOBBER; this can happen if X was
7208 the return value from a call to gen_lowpart. */
7209 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7210 return x;
7212 /* We want to perform the operation is its present mode unless we know
7213 that the operation is valid in MODE, in which case we do the operation
7214 in MODE. */
7215 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7216 && have_insn_for (code, mode))
7217 ? mode : GET_MODE (x));
7219 /* It is not valid to do a right-shift in a narrower mode
7220 than the one it came in with. */
7221 if ((code == LSHIFTRT || code == ASHIFTRT)
7222 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7223 op_mode = GET_MODE (x);
7225 /* Truncate MASK to fit OP_MODE. */
7226 if (op_mode)
7227 mask &= GET_MODE_MASK (op_mode);
7229 /* When we have an arithmetic operation, or a shift whose count we
7230 do not know, we need to assume that all bits up to the highest-order
7231 bit in MASK will be needed. This is how we form such a mask. */
7232 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7233 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7234 else
7235 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7236 - 1);
7238 /* Determine what bits of X are guaranteed to be (non)zero. */
7239 nonzero = nonzero_bits (x, mode);
7241 /* If none of the bits in X are needed, return a zero. */
7242 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7243 x = const0_rtx;
7245 /* If X is a CONST_INT, return a new one. Do this here since the
7246 test below will fail. */
7247 if (GET_CODE (x) == CONST_INT)
7249 if (SCALAR_INT_MODE_P (mode))
7250 return gen_int_mode (INTVAL (x) & mask, mode);
7251 else
7253 x = GEN_INT (INTVAL (x) & mask);
7254 return gen_lowpart_common (mode, x);
7258 /* If X is narrower than MODE and we want all the bits in X's mode, just
7259 get X in the proper mode. */
7260 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7261 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7262 return gen_lowpart (mode, x);
7264 switch (code)
7266 case CLOBBER:
7267 /* If X is a (clobber (const_int)), return it since we know we are
7268 generating something that won't match. */
7269 return x;
7271 case SIGN_EXTEND:
7272 case ZERO_EXTEND:
7273 case ZERO_EXTRACT:
7274 case SIGN_EXTRACT:
7275 x = expand_compound_operation (x);
7276 if (GET_CODE (x) != code)
7277 return force_to_mode (x, mode, mask, next_select);
7278 break;
7280 case SUBREG:
7281 if (subreg_lowpart_p (x)
7282 /* We can ignore the effect of this SUBREG if it narrows the mode or
7283 if the constant masks to zero all the bits the mode doesn't
7284 have. */
7285 && ((GET_MODE_SIZE (GET_MODE (x))
7286 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7287 || (0 == (mask
7288 & GET_MODE_MASK (GET_MODE (x))
7289 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7290 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7291 break;
7293 case AND:
7294 /* If this is an AND with a constant, convert it into an AND
7295 whose constant is the AND of that constant with MASK. If it
7296 remains an AND of MASK, delete it since it is redundant. */
7298 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7300 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7301 mask & INTVAL (XEXP (x, 1)));
7303 /* If X is still an AND, see if it is an AND with a mask that
7304 is just some low-order bits. If so, and it is MASK, we don't
7305 need it. */
7307 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7308 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7309 == mask))
7310 x = XEXP (x, 0);
7312 /* If it remains an AND, try making another AND with the bits
7313 in the mode mask that aren't in MASK turned on. If the
7314 constant in the AND is wide enough, this might make a
7315 cheaper constant. */
7317 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7318 && GET_MODE_MASK (GET_MODE (x)) != mask
7319 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7321 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7322 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7323 int width = GET_MODE_BITSIZE (GET_MODE (x));
7324 rtx y;
7326 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7327 number, sign extend it. */
7328 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7329 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7330 cval |= (HOST_WIDE_INT) -1 << width;
7332 y = simplify_gen_binary (AND, GET_MODE (x),
7333 XEXP (x, 0), GEN_INT (cval));
7334 if (rtx_cost (y, SET) < rtx_cost (x, SET))
7335 x = y;
7338 break;
7341 goto binop;
7343 case PLUS:
7344 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7345 low-order bits (as in an alignment operation) and FOO is already
7346 aligned to that boundary, mask C1 to that boundary as well.
7347 This may eliminate that PLUS and, later, the AND. */
7350 unsigned int width = GET_MODE_BITSIZE (mode);
7351 unsigned HOST_WIDE_INT smask = mask;
7353 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7354 number, sign extend it. */
7356 if (width < HOST_BITS_PER_WIDE_INT
7357 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7358 smask |= (HOST_WIDE_INT) -1 << width;
7360 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7361 && exact_log2 (- smask) >= 0
7362 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7363 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7364 return force_to_mode (plus_constant (XEXP (x, 0),
7365 (INTVAL (XEXP (x, 1)) & smask)),
7366 mode, smask, next_select);
7369 /* ... fall through ... */
7371 case MULT:
7372 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7373 most significant bit in MASK since carries from those bits will
7374 affect the bits we are interested in. */
7375 mask = fuller_mask;
7376 goto binop;
7378 case MINUS:
7379 /* If X is (minus C Y) where C's least set bit is larger than any bit
7380 in the mask, then we may replace with (neg Y). */
7381 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7382 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7383 & -INTVAL (XEXP (x, 0))))
7384 > mask))
7386 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7387 GET_MODE (x));
7388 return force_to_mode (x, mode, mask, next_select);
7391 /* Similarly, if C contains every bit in the fuller_mask, then we may
7392 replace with (not Y). */
7393 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7394 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7395 == INTVAL (XEXP (x, 0))))
7397 x = simplify_gen_unary (NOT, GET_MODE (x),
7398 XEXP (x, 1), GET_MODE (x));
7399 return force_to_mode (x, mode, mask, next_select);
7402 mask = fuller_mask;
7403 goto binop;
7405 case IOR:
7406 case XOR:
7407 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7408 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7409 operation which may be a bitfield extraction. Ensure that the
7410 constant we form is not wider than the mode of X. */
7412 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7413 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7414 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7415 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7416 && GET_CODE (XEXP (x, 1)) == CONST_INT
7417 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7418 + floor_log2 (INTVAL (XEXP (x, 1))))
7419 < GET_MODE_BITSIZE (GET_MODE (x)))
7420 && (INTVAL (XEXP (x, 1))
7421 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7423 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7424 << INTVAL (XEXP (XEXP (x, 0), 1)));
7425 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7426 XEXP (XEXP (x, 0), 0), temp);
7427 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7428 XEXP (XEXP (x, 0), 1));
7429 return force_to_mode (x, mode, mask, next_select);
7432 binop:
7433 /* For most binary operations, just propagate into the operation and
7434 change the mode if we have an operation of that mode. */
7436 op0 = gen_lowpart_or_truncate (op_mode,
7437 force_to_mode (XEXP (x, 0), mode, mask,
7438 next_select));
7439 op1 = gen_lowpart_or_truncate (op_mode,
7440 force_to_mode (XEXP (x, 1), mode, mask,
7441 next_select));
7443 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7444 x = simplify_gen_binary (code, op_mode, op0, op1);
7445 break;
7447 case ASHIFT:
7448 /* For left shifts, do the same, but just for the first operand.
7449 However, we cannot do anything with shifts where we cannot
7450 guarantee that the counts are smaller than the size of the mode
7451 because such a count will have a different meaning in a
7452 wider mode. */
7454 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7455 && INTVAL (XEXP (x, 1)) >= 0
7456 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7457 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7458 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7459 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7460 break;
7462 /* If the shift count is a constant and we can do arithmetic in
7463 the mode of the shift, refine which bits we need. Otherwise, use the
7464 conservative form of the mask. */
7465 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7466 && INTVAL (XEXP (x, 1)) >= 0
7467 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7468 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7469 mask >>= INTVAL (XEXP (x, 1));
7470 else
7471 mask = fuller_mask;
7473 op0 = gen_lowpart_or_truncate (op_mode,
7474 force_to_mode (XEXP (x, 0), op_mode,
7475 mask, next_select));
7477 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7478 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7479 break;
7481 case LSHIFTRT:
7482 /* Here we can only do something if the shift count is a constant,
7483 this shift constant is valid for the host, and we can do arithmetic
7484 in OP_MODE. */
7486 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7487 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7488 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7490 rtx inner = XEXP (x, 0);
7491 unsigned HOST_WIDE_INT inner_mask;
7493 /* Select the mask of the bits we need for the shift operand. */
7494 inner_mask = mask << INTVAL (XEXP (x, 1));
7496 /* We can only change the mode of the shift if we can do arithmetic
7497 in the mode of the shift and INNER_MASK is no wider than the
7498 width of X's mode. */
7499 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7500 op_mode = GET_MODE (x);
7502 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7504 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7505 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7508 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7509 shift and AND produces only copies of the sign bit (C2 is one less
7510 than a power of two), we can do this with just a shift. */
7512 if (GET_CODE (x) == LSHIFTRT
7513 && GET_CODE (XEXP (x, 1)) == CONST_INT
7514 /* The shift puts one of the sign bit copies in the least significant
7515 bit. */
7516 && ((INTVAL (XEXP (x, 1))
7517 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7518 >= GET_MODE_BITSIZE (GET_MODE (x)))
7519 && exact_log2 (mask + 1) >= 0
7520 /* Number of bits left after the shift must be more than the mask
7521 needs. */
7522 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7523 <= GET_MODE_BITSIZE (GET_MODE (x)))
7524 /* Must be more sign bit copies than the mask needs. */
7525 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7526 >= exact_log2 (mask + 1)))
7527 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7528 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7529 - exact_log2 (mask + 1)));
7531 goto shiftrt;
7533 case ASHIFTRT:
7534 /* If we are just looking for the sign bit, we don't need this shift at
7535 all, even if it has a variable count. */
7536 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7537 && (mask == ((unsigned HOST_WIDE_INT) 1
7538 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7539 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7541 /* If this is a shift by a constant, get a mask that contains those bits
7542 that are not copies of the sign bit. We then have two cases: If
7543 MASK only includes those bits, this can be a logical shift, which may
7544 allow simplifications. If MASK is a single-bit field not within
7545 those bits, we are requesting a copy of the sign bit and hence can
7546 shift the sign bit to the appropriate location. */
7548 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7549 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7551 int i;
7553 /* If the considered data is wider than HOST_WIDE_INT, we can't
7554 represent a mask for all its bits in a single scalar.
7555 But we only care about the lower bits, so calculate these. */
7557 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7559 nonzero = ~(HOST_WIDE_INT) 0;
7561 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7562 is the number of bits a full-width mask would have set.
7563 We need only shift if these are fewer than nonzero can
7564 hold. If not, we must keep all bits set in nonzero. */
7566 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7567 < HOST_BITS_PER_WIDE_INT)
7568 nonzero >>= INTVAL (XEXP (x, 1))
7569 + HOST_BITS_PER_WIDE_INT
7570 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7572 else
7574 nonzero = GET_MODE_MASK (GET_MODE (x));
7575 nonzero >>= INTVAL (XEXP (x, 1));
7578 if ((mask & ~nonzero) == 0)
7580 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7581 XEXP (x, 0), INTVAL (XEXP (x, 1)));
7582 if (GET_CODE (x) != ASHIFTRT)
7583 return force_to_mode (x, mode, mask, next_select);
7586 else if ((i = exact_log2 (mask)) >= 0)
7588 x = simplify_shift_const
7589 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7590 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7592 if (GET_CODE (x) != ASHIFTRT)
7593 return force_to_mode (x, mode, mask, next_select);
7597 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7598 even if the shift count isn't a constant. */
7599 if (mask == 1)
7600 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7601 XEXP (x, 0), XEXP (x, 1));
7603 shiftrt:
7605 /* If this is a zero- or sign-extension operation that just affects bits
7606 we don't care about, remove it. Be sure the call above returned
7607 something that is still a shift. */
7609 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7610 && GET_CODE (XEXP (x, 1)) == CONST_INT
7611 && INTVAL (XEXP (x, 1)) >= 0
7612 && (INTVAL (XEXP (x, 1))
7613 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7614 && GET_CODE (XEXP (x, 0)) == ASHIFT
7615 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7616 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7617 next_select);
7619 break;
7621 case ROTATE:
7622 case ROTATERT:
7623 /* If the shift count is constant and we can do computations
7624 in the mode of X, compute where the bits we care about are.
7625 Otherwise, we can't do anything. Don't change the mode of
7626 the shift or propagate MODE into the shift, though. */
7627 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7628 && INTVAL (XEXP (x, 1)) >= 0)
7630 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7631 GET_MODE (x), GEN_INT (mask),
7632 XEXP (x, 1));
7633 if (temp && GET_CODE (temp) == CONST_INT)
7634 SUBST (XEXP (x, 0),
7635 force_to_mode (XEXP (x, 0), GET_MODE (x),
7636 INTVAL (temp), next_select));
7638 break;
7640 case NEG:
7641 /* If we just want the low-order bit, the NEG isn't needed since it
7642 won't change the low-order bit. */
7643 if (mask == 1)
7644 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7646 /* We need any bits less significant than the most significant bit in
7647 MASK since carries from those bits will affect the bits we are
7648 interested in. */
7649 mask = fuller_mask;
7650 goto unop;
7652 case NOT:
7653 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7654 same as the XOR case above. Ensure that the constant we form is not
7655 wider than the mode of X. */
7657 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7658 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7659 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7660 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7661 < GET_MODE_BITSIZE (GET_MODE (x)))
7662 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7664 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7665 GET_MODE (x));
7666 temp = simplify_gen_binary (XOR, GET_MODE (x),
7667 XEXP (XEXP (x, 0), 0), temp);
7668 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7669 temp, XEXP (XEXP (x, 0), 1));
7671 return force_to_mode (x, mode, mask, next_select);
7674 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7675 use the full mask inside the NOT. */
7676 mask = fuller_mask;
7678 unop:
7679 op0 = gen_lowpart_or_truncate (op_mode,
7680 force_to_mode (XEXP (x, 0), mode, mask,
7681 next_select));
7682 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7683 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7684 break;
7686 case NE:
7687 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7688 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7689 which is equal to STORE_FLAG_VALUE. */
7690 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7691 && GET_MODE (XEXP (x, 0)) == mode
7692 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7693 && (nonzero_bits (XEXP (x, 0), mode)
7694 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7695 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7697 break;
7699 case IF_THEN_ELSE:
7700 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7701 written in a narrower mode. We play it safe and do not do so. */
7703 SUBST (XEXP (x, 1),
7704 gen_lowpart_or_truncate (GET_MODE (x),
7705 force_to_mode (XEXP (x, 1), mode,
7706 mask, next_select)));
7707 SUBST (XEXP (x, 2),
7708 gen_lowpart_or_truncate (GET_MODE (x),
7709 force_to_mode (XEXP (x, 2), mode,
7710 mask, next_select)));
7711 break;
7713 default:
7714 break;
7717 /* Ensure we return a value of the proper mode. */
7718 return gen_lowpart_or_truncate (mode, x);
7721 /* Return nonzero if X is an expression that has one of two values depending on
7722 whether some other value is zero or nonzero. In that case, we return the
7723 value that is being tested, *PTRUE is set to the value if the rtx being
7724 returned has a nonzero value, and *PFALSE is set to the other alternative.
7726 If we return zero, we set *PTRUE and *PFALSE to X. */
7728 static rtx
7729 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7731 enum machine_mode mode = GET_MODE (x);
7732 enum rtx_code code = GET_CODE (x);
7733 rtx cond0, cond1, true0, true1, false0, false1;
7734 unsigned HOST_WIDE_INT nz;
7736 /* If we are comparing a value against zero, we are done. */
7737 if ((code == NE || code == EQ)
7738 && XEXP (x, 1) == const0_rtx)
7740 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7741 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7742 return XEXP (x, 0);
7745 /* If this is a unary operation whose operand has one of two values, apply
7746 our opcode to compute those values. */
7747 else if (UNARY_P (x)
7748 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7750 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7751 *pfalse = simplify_gen_unary (code, mode, false0,
7752 GET_MODE (XEXP (x, 0)));
7753 return cond0;
7756 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7757 make can't possibly match and would suppress other optimizations. */
7758 else if (code == COMPARE)
7761 /* If this is a binary operation, see if either side has only one of two
7762 values. If either one does or if both do and they are conditional on
7763 the same value, compute the new true and false values. */
7764 else if (BINARY_P (x))
7766 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7767 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7769 if ((cond0 != 0 || cond1 != 0)
7770 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7772 /* If if_then_else_cond returned zero, then true/false are the
7773 same rtl. We must copy one of them to prevent invalid rtl
7774 sharing. */
7775 if (cond0 == 0)
7776 true0 = copy_rtx (true0);
7777 else if (cond1 == 0)
7778 true1 = copy_rtx (true1);
7780 if (COMPARISON_P (x))
7782 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7783 true0, true1);
7784 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7785 false0, false1);
7787 else
7789 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7790 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7793 return cond0 ? cond0 : cond1;
7796 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7797 operands is zero when the other is nonzero, and vice-versa,
7798 and STORE_FLAG_VALUE is 1 or -1. */
7800 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7801 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7802 || code == UMAX)
7803 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7805 rtx op0 = XEXP (XEXP (x, 0), 1);
7806 rtx op1 = XEXP (XEXP (x, 1), 1);
7808 cond0 = XEXP (XEXP (x, 0), 0);
7809 cond1 = XEXP (XEXP (x, 1), 0);
7811 if (COMPARISON_P (cond0)
7812 && COMPARISON_P (cond1)
7813 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7814 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7815 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7816 || ((swap_condition (GET_CODE (cond0))
7817 == reversed_comparison_code (cond1, NULL))
7818 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7819 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7820 && ! side_effects_p (x))
7822 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7823 *pfalse = simplify_gen_binary (MULT, mode,
7824 (code == MINUS
7825 ? simplify_gen_unary (NEG, mode,
7826 op1, mode)
7827 : op1),
7828 const_true_rtx);
7829 return cond0;
7833 /* Similarly for MULT, AND and UMIN, except that for these the result
7834 is always zero. */
7835 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7836 && (code == MULT || code == AND || code == UMIN)
7837 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7839 cond0 = XEXP (XEXP (x, 0), 0);
7840 cond1 = XEXP (XEXP (x, 1), 0);
7842 if (COMPARISON_P (cond0)
7843 && COMPARISON_P (cond1)
7844 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7845 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7846 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7847 || ((swap_condition (GET_CODE (cond0))
7848 == reversed_comparison_code (cond1, NULL))
7849 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7850 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7851 && ! side_effects_p (x))
7853 *ptrue = *pfalse = const0_rtx;
7854 return cond0;
7859 else if (code == IF_THEN_ELSE)
7861 /* If we have IF_THEN_ELSE already, extract the condition and
7862 canonicalize it if it is NE or EQ. */
7863 cond0 = XEXP (x, 0);
7864 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7865 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7866 return XEXP (cond0, 0);
7867 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7869 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7870 return XEXP (cond0, 0);
7872 else
7873 return cond0;
7876 /* If X is a SUBREG, we can narrow both the true and false values
7877 if the inner expression, if there is a condition. */
7878 else if (code == SUBREG
7879 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7880 &true0, &false0)))
7882 true0 = simplify_gen_subreg (mode, true0,
7883 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7884 false0 = simplify_gen_subreg (mode, false0,
7885 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7886 if (true0 && false0)
7888 *ptrue = true0;
7889 *pfalse = false0;
7890 return cond0;
7894 /* If X is a constant, this isn't special and will cause confusions
7895 if we treat it as such. Likewise if it is equivalent to a constant. */
7896 else if (CONSTANT_P (x)
7897 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7900 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7901 will be least confusing to the rest of the compiler. */
7902 else if (mode == BImode)
7904 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7905 return x;
7908 /* If X is known to be either 0 or -1, those are the true and
7909 false values when testing X. */
7910 else if (x == constm1_rtx || x == const0_rtx
7911 || (mode != VOIDmode
7912 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7914 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7915 return x;
7918 /* Likewise for 0 or a single bit. */
7919 else if (SCALAR_INT_MODE_P (mode)
7920 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7921 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7923 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7924 return x;
7927 /* Otherwise fail; show no condition with true and false values the same. */
7928 *ptrue = *pfalse = x;
7929 return 0;
7932 /* Return the value of expression X given the fact that condition COND
7933 is known to be true when applied to REG as its first operand and VAL
7934 as its second. X is known to not be shared and so can be modified in
7935 place.
7937 We only handle the simplest cases, and specifically those cases that
7938 arise with IF_THEN_ELSE expressions. */
7940 static rtx
7941 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7943 enum rtx_code code = GET_CODE (x);
7944 rtx temp;
7945 const char *fmt;
7946 int i, j;
7948 if (side_effects_p (x))
7949 return x;
7951 /* If either operand of the condition is a floating point value,
7952 then we have to avoid collapsing an EQ comparison. */
7953 if (cond == EQ
7954 && rtx_equal_p (x, reg)
7955 && ! FLOAT_MODE_P (GET_MODE (x))
7956 && ! FLOAT_MODE_P (GET_MODE (val)))
7957 return val;
7959 if (cond == UNEQ && rtx_equal_p (x, reg))
7960 return val;
7962 /* If X is (abs REG) and we know something about REG's relationship
7963 with zero, we may be able to simplify this. */
7965 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7966 switch (cond)
7968 case GE: case GT: case EQ:
7969 return XEXP (x, 0);
7970 case LT: case LE:
7971 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7972 XEXP (x, 0),
7973 GET_MODE (XEXP (x, 0)));
7974 default:
7975 break;
7978 /* The only other cases we handle are MIN, MAX, and comparisons if the
7979 operands are the same as REG and VAL. */
7981 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7983 if (rtx_equal_p (XEXP (x, 0), val))
7984 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7986 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7988 if (COMPARISON_P (x))
7990 if (comparison_dominates_p (cond, code))
7991 return const_true_rtx;
7993 code = reversed_comparison_code (x, NULL);
7994 if (code != UNKNOWN
7995 && comparison_dominates_p (cond, code))
7996 return const0_rtx;
7997 else
7998 return x;
8000 else if (code == SMAX || code == SMIN
8001 || code == UMIN || code == UMAX)
8003 int unsignedp = (code == UMIN || code == UMAX);
8005 /* Do not reverse the condition when it is NE or EQ.
8006 This is because we cannot conclude anything about
8007 the value of 'SMAX (x, y)' when x is not equal to y,
8008 but we can when x equals y. */
8009 if ((code == SMAX || code == UMAX)
8010 && ! (cond == EQ || cond == NE))
8011 cond = reverse_condition (cond);
8013 switch (cond)
8015 case GE: case GT:
8016 return unsignedp ? x : XEXP (x, 1);
8017 case LE: case LT:
8018 return unsignedp ? x : XEXP (x, 0);
8019 case GEU: case GTU:
8020 return unsignedp ? XEXP (x, 1) : x;
8021 case LEU: case LTU:
8022 return unsignedp ? XEXP (x, 0) : x;
8023 default:
8024 break;
8029 else if (code == SUBREG)
8031 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8032 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
8034 if (SUBREG_REG (x) != r)
8036 /* We must simplify subreg here, before we lose track of the
8037 original inner_mode. */
8038 new = simplify_subreg (GET_MODE (x), r,
8039 inner_mode, SUBREG_BYTE (x));
8040 if (new)
8041 return new;
8042 else
8043 SUBST (SUBREG_REG (x), r);
8046 return x;
8048 /* We don't have to handle SIGN_EXTEND here, because even in the
8049 case of replacing something with a modeless CONST_INT, a
8050 CONST_INT is already (supposed to be) a valid sign extension for
8051 its narrower mode, which implies it's already properly
8052 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8053 story is different. */
8054 else if (code == ZERO_EXTEND)
8056 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8057 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
8059 if (XEXP (x, 0) != r)
8061 /* We must simplify the zero_extend here, before we lose
8062 track of the original inner_mode. */
8063 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8064 r, inner_mode);
8065 if (new)
8066 return new;
8067 else
8068 SUBST (XEXP (x, 0), r);
8071 return x;
8074 fmt = GET_RTX_FORMAT (code);
8075 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8077 if (fmt[i] == 'e')
8078 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8079 else if (fmt[i] == 'E')
8080 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8081 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8082 cond, reg, val));
8085 return x;
8088 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8089 assignment as a field assignment. */
8091 static int
8092 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8094 if (x == y || rtx_equal_p (x, y))
8095 return 1;
8097 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8098 return 0;
8100 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8101 Note that all SUBREGs of MEM are paradoxical; otherwise they
8102 would have been rewritten. */
8103 if (MEM_P (x) && GET_CODE (y) == SUBREG
8104 && MEM_P (SUBREG_REG (y))
8105 && rtx_equal_p (SUBREG_REG (y),
8106 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8107 return 1;
8109 if (MEM_P (y) && GET_CODE (x) == SUBREG
8110 && MEM_P (SUBREG_REG (x))
8111 && rtx_equal_p (SUBREG_REG (x),
8112 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8113 return 1;
8115 /* We used to see if get_last_value of X and Y were the same but that's
8116 not correct. In one direction, we'll cause the assignment to have
8117 the wrong destination and in the case, we'll import a register into this
8118 insn that might have already have been dead. So fail if none of the
8119 above cases are true. */
8120 return 0;
8123 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8124 Return that assignment if so.
8126 We only handle the most common cases. */
8128 static rtx
8129 make_field_assignment (rtx x)
8131 rtx dest = SET_DEST (x);
8132 rtx src = SET_SRC (x);
8133 rtx assign;
8134 rtx rhs, lhs;
8135 HOST_WIDE_INT c1;
8136 HOST_WIDE_INT pos;
8137 unsigned HOST_WIDE_INT len;
8138 rtx other;
8139 enum machine_mode mode;
8141 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8142 a clear of a one-bit field. We will have changed it to
8143 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8144 for a SUBREG. */
8146 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8147 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
8148 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8149 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8151 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8152 1, 1, 1, 0);
8153 if (assign != 0)
8154 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8155 return x;
8158 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8159 && subreg_lowpart_p (XEXP (src, 0))
8160 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8161 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8162 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8163 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
8164 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8165 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8167 assign = make_extraction (VOIDmode, dest, 0,
8168 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8169 1, 1, 1, 0);
8170 if (assign != 0)
8171 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8172 return x;
8175 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8176 one-bit field. */
8177 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8178 && XEXP (XEXP (src, 0), 0) == const1_rtx
8179 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8181 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8182 1, 1, 1, 0);
8183 if (assign != 0)
8184 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8185 return x;
8188 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8189 SRC is an AND with all bits of that field set, then we can discard
8190 the AND. */
8191 if (GET_CODE (dest) == ZERO_EXTRACT
8192 && GET_CODE (XEXP (dest, 1)) == CONST_INT
8193 && GET_CODE (src) == AND
8194 && GET_CODE (XEXP (src, 1)) == CONST_INT)
8196 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8197 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8198 unsigned HOST_WIDE_INT ze_mask;
8200 if (width >= HOST_BITS_PER_WIDE_INT)
8201 ze_mask = -1;
8202 else
8203 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8205 /* Complete overlap. We can remove the source AND. */
8206 if ((and_mask & ze_mask) == ze_mask)
8207 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8209 /* Partial overlap. We can reduce the source AND. */
8210 if ((and_mask & ze_mask) != and_mask)
8212 mode = GET_MODE (src);
8213 src = gen_rtx_AND (mode, XEXP (src, 0),
8214 gen_int_mode (and_mask & ze_mask, mode));
8215 return gen_rtx_SET (VOIDmode, dest, src);
8219 /* The other case we handle is assignments into a constant-position
8220 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
8221 a mask that has all one bits except for a group of zero bits and
8222 OTHER is known to have zeros where C1 has ones, this is such an
8223 assignment. Compute the position and length from C1. Shift OTHER
8224 to the appropriate position, force it to the required mode, and
8225 make the extraction. Check for the AND in both operands. */
8227 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8228 return x;
8230 rhs = expand_compound_operation (XEXP (src, 0));
8231 lhs = expand_compound_operation (XEXP (src, 1));
8233 if (GET_CODE (rhs) == AND
8234 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
8235 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8236 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8237 else if (GET_CODE (lhs) == AND
8238 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
8239 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8240 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8241 else
8242 return x;
8244 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8245 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8246 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8247 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8248 return x;
8250 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8251 if (assign == 0)
8252 return x;
8254 /* The mode to use for the source is the mode of the assignment, or of
8255 what is inside a possible STRICT_LOW_PART. */
8256 mode = (GET_CODE (assign) == STRICT_LOW_PART
8257 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8259 /* Shift OTHER right POS places and make it the source, restricting it
8260 to the proper length and mode. */
8262 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8263 GET_MODE (src),
8264 other, pos),
8265 dest);
8266 src = force_to_mode (src, mode,
8267 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8268 ? ~(unsigned HOST_WIDE_INT) 0
8269 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8272 /* If SRC is masked by an AND that does not make a difference in
8273 the value being stored, strip it. */
8274 if (GET_CODE (assign) == ZERO_EXTRACT
8275 && GET_CODE (XEXP (assign, 1)) == CONST_INT
8276 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8277 && GET_CODE (src) == AND
8278 && GET_CODE (XEXP (src, 1)) == CONST_INT
8279 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8280 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8281 src = XEXP (src, 0);
8283 return gen_rtx_SET (VOIDmode, assign, src);
8286 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8287 if so. */
8289 static rtx
8290 apply_distributive_law (rtx x)
8292 enum rtx_code code = GET_CODE (x);
8293 enum rtx_code inner_code;
8294 rtx lhs, rhs, other;
8295 rtx tem;
8297 /* Distributivity is not true for floating point as it can change the
8298 value. So we don't do it unless -funsafe-math-optimizations. */
8299 if (FLOAT_MODE_P (GET_MODE (x))
8300 && ! flag_unsafe_math_optimizations)
8301 return x;
8303 /* The outer operation can only be one of the following: */
8304 if (code != IOR && code != AND && code != XOR
8305 && code != PLUS && code != MINUS)
8306 return x;
8308 lhs = XEXP (x, 0);
8309 rhs = XEXP (x, 1);
8311 /* If either operand is a primitive we can't do anything, so get out
8312 fast. */
8313 if (OBJECT_P (lhs) || OBJECT_P (rhs))
8314 return x;
8316 lhs = expand_compound_operation (lhs);
8317 rhs = expand_compound_operation (rhs);
8318 inner_code = GET_CODE (lhs);
8319 if (inner_code != GET_CODE (rhs))
8320 return x;
8322 /* See if the inner and outer operations distribute. */
8323 switch (inner_code)
8325 case LSHIFTRT:
8326 case ASHIFTRT:
8327 case AND:
8328 case IOR:
8329 /* These all distribute except over PLUS. */
8330 if (code == PLUS || code == MINUS)
8331 return x;
8332 break;
8334 case MULT:
8335 if (code != PLUS && code != MINUS)
8336 return x;
8337 break;
8339 case ASHIFT:
8340 /* This is also a multiply, so it distributes over everything. */
8341 break;
8343 case SUBREG:
8344 /* Non-paradoxical SUBREGs distributes over all operations,
8345 provided the inner modes and byte offsets are the same, this
8346 is an extraction of a low-order part, we don't convert an fp
8347 operation to int or vice versa, this is not a vector mode,
8348 and we would not be converting a single-word operation into a
8349 multi-word operation. The latter test is not required, but
8350 it prevents generating unneeded multi-word operations. Some
8351 of the previous tests are redundant given the latter test,
8352 but are retained because they are required for correctness.
8354 We produce the result slightly differently in this case. */
8356 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8357 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8358 || ! subreg_lowpart_p (lhs)
8359 || (GET_MODE_CLASS (GET_MODE (lhs))
8360 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8361 || (GET_MODE_SIZE (GET_MODE (lhs))
8362 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8363 || VECTOR_MODE_P (GET_MODE (lhs))
8364 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8365 /* Result might need to be truncated. Don't change mode if
8366 explicit truncation is needed. */
8367 || !TRULY_NOOP_TRUNCATION
8368 (GET_MODE_BITSIZE (GET_MODE (x)),
8369 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8370 return x;
8372 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8373 SUBREG_REG (lhs), SUBREG_REG (rhs));
8374 return gen_lowpart (GET_MODE (x), tem);
8376 default:
8377 return x;
8380 /* Set LHS and RHS to the inner operands (A and B in the example
8381 above) and set OTHER to the common operand (C in the example).
8382 There is only one way to do this unless the inner operation is
8383 commutative. */
8384 if (COMMUTATIVE_ARITH_P (lhs)
8385 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8386 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8387 else if (COMMUTATIVE_ARITH_P (lhs)
8388 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8389 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8390 else if (COMMUTATIVE_ARITH_P (lhs)
8391 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8392 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8393 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8394 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8395 else
8396 return x;
8398 /* Form the new inner operation, seeing if it simplifies first. */
8399 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8401 /* There is one exception to the general way of distributing:
8402 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8403 if (code == XOR && inner_code == IOR)
8405 inner_code = AND;
8406 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8409 /* We may be able to continuing distributing the result, so call
8410 ourselves recursively on the inner operation before forming the
8411 outer operation, which we return. */
8412 return simplify_gen_binary (inner_code, GET_MODE (x),
8413 apply_distributive_law (tem), other);
8416 /* See if X is of the form (* (+ A B) C), and if so convert to
8417 (+ (* A C) (* B C)) and try to simplify.
8419 Most of the time, this results in no change. However, if some of
8420 the operands are the same or inverses of each other, simplifications
8421 will result.
8423 For example, (and (ior A B) (not B)) can occur as the result of
8424 expanding a bit field assignment. When we apply the distributive
8425 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8426 which then simplifies to (and (A (not B))).
8428 Note that no checks happen on the validity of applying the inverse
8429 distributive law. This is pointless since we can do it in the
8430 few places where this routine is called.
8432 N is the index of the term that is decomposed (the arithmetic operation,
8433 i.e. (+ A B) in the first example above). !N is the index of the term that
8434 is distributed, i.e. of C in the first example above. */
8435 static rtx
8436 distribute_and_simplify_rtx (rtx x, int n)
8438 enum machine_mode mode;
8439 enum rtx_code outer_code, inner_code;
8440 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8442 decomposed = XEXP (x, n);
8443 if (!ARITHMETIC_P (decomposed))
8444 return NULL_RTX;
8446 mode = GET_MODE (x);
8447 outer_code = GET_CODE (x);
8448 distributed = XEXP (x, !n);
8450 inner_code = GET_CODE (decomposed);
8451 inner_op0 = XEXP (decomposed, 0);
8452 inner_op1 = XEXP (decomposed, 1);
8454 /* Special case (and (xor B C) (not A)), which is equivalent to
8455 (xor (ior A B) (ior A C)) */
8456 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8458 distributed = XEXP (distributed, 0);
8459 outer_code = IOR;
8462 if (n == 0)
8464 /* Distribute the second term. */
8465 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8466 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8468 else
8470 /* Distribute the first term. */
8471 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8472 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8475 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8476 new_op0, new_op1));
8477 if (GET_CODE (tmp) != outer_code
8478 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8479 return tmp;
8481 return NULL_RTX;
8484 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8485 in MODE. Return an equivalent form, if different from (and VAROP
8486 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
8488 static rtx
8489 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8490 unsigned HOST_WIDE_INT constop)
8492 unsigned HOST_WIDE_INT nonzero;
8493 unsigned HOST_WIDE_INT orig_constop;
8494 rtx orig_varop;
8495 int i;
8497 orig_varop = varop;
8498 orig_constop = constop;
8499 if (GET_CODE (varop) == CLOBBER)
8500 return NULL_RTX;
8502 /* Simplify VAROP knowing that we will be only looking at some of the
8503 bits in it.
8505 Note by passing in CONSTOP, we guarantee that the bits not set in
8506 CONSTOP are not significant and will never be examined. We must
8507 ensure that is the case by explicitly masking out those bits
8508 before returning. */
8509 varop = force_to_mode (varop, mode, constop, 0);
8511 /* If VAROP is a CLOBBER, we will fail so return it. */
8512 if (GET_CODE (varop) == CLOBBER)
8513 return varop;
8515 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8516 to VAROP and return the new constant. */
8517 if (GET_CODE (varop) == CONST_INT)
8518 return gen_int_mode (INTVAL (varop) & constop, mode);
8520 /* See what bits may be nonzero in VAROP. Unlike the general case of
8521 a call to nonzero_bits, here we don't care about bits outside
8522 MODE. */
8524 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8526 /* Turn off all bits in the constant that are known to already be zero.
8527 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8528 which is tested below. */
8530 constop &= nonzero;
8532 /* If we don't have any bits left, return zero. */
8533 if (constop == 0)
8534 return const0_rtx;
8536 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8537 a power of two, we can replace this with an ASHIFT. */
8538 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8539 && (i = exact_log2 (constop)) >= 0)
8540 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8542 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8543 or XOR, then try to apply the distributive law. This may eliminate
8544 operations if either branch can be simplified because of the AND.
8545 It may also make some cases more complex, but those cases probably
8546 won't match a pattern either with or without this. */
8548 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8549 return
8550 gen_lowpart
8551 (mode,
8552 apply_distributive_law
8553 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8554 simplify_and_const_int (NULL_RTX,
8555 GET_MODE (varop),
8556 XEXP (varop, 0),
8557 constop),
8558 simplify_and_const_int (NULL_RTX,
8559 GET_MODE (varop),
8560 XEXP (varop, 1),
8561 constop))));
8563 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8564 the AND and see if one of the operands simplifies to zero. If so, we
8565 may eliminate it. */
8567 if (GET_CODE (varop) == PLUS
8568 && exact_log2 (constop + 1) >= 0)
8570 rtx o0, o1;
8572 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8573 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8574 if (o0 == const0_rtx)
8575 return o1;
8576 if (o1 == const0_rtx)
8577 return o0;
8580 /* Make a SUBREG if necessary. If we can't make it, fail. */
8581 varop = gen_lowpart (mode, varop);
8582 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8583 return NULL_RTX;
8585 /* If we are only masking insignificant bits, return VAROP. */
8586 if (constop == nonzero)
8587 return varop;
8589 if (varop == orig_varop && constop == orig_constop)
8590 return NULL_RTX;
8592 /* Otherwise, return an AND. */
8593 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8597 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8598 in MODE.
8600 Return an equivalent form, if different from X. Otherwise, return X. If
8601 X is zero, we are to always construct the equivalent form. */
8603 static rtx
8604 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8605 unsigned HOST_WIDE_INT constop)
8607 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8608 if (tem)
8609 return tem;
8611 if (!x)
8612 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8613 gen_int_mode (constop, mode));
8614 if (GET_MODE (x) != mode)
8615 x = gen_lowpart (mode, x);
8616 return x;
8619 /* Given a REG, X, compute which bits in X can be nonzero.
8620 We don't care about bits outside of those defined in MODE.
8622 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8623 a shift, AND, or zero_extract, we can do better. */
8625 static rtx
8626 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8627 const_rtx known_x ATTRIBUTE_UNUSED,
8628 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8629 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8630 unsigned HOST_WIDE_INT *nonzero)
8632 rtx tem;
8633 reg_stat_type *rsp;
8635 /* If X is a register whose nonzero bits value is current, use it.
8636 Otherwise, if X is a register whose value we can find, use that
8637 value. Otherwise, use the previously-computed global nonzero bits
8638 for this register. */
8640 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8641 if (rsp->last_set_value != 0
8642 && (rsp->last_set_mode == mode
8643 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8644 && GET_MODE_CLASS (mode) == MODE_INT))
8645 && ((rsp->last_set_label >= label_tick_ebb_start
8646 && rsp->last_set_label < label_tick)
8647 || (rsp->last_set_label == label_tick
8648 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8649 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8650 && REG_N_SETS (REGNO (x)) == 1
8651 && !REGNO_REG_SET_P
8652 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8654 *nonzero &= rsp->last_set_nonzero_bits;
8655 return NULL;
8658 tem = get_last_value (x);
8660 if (tem)
8662 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8663 /* If X is narrower than MODE and TEM is a non-negative
8664 constant that would appear negative in the mode of X,
8665 sign-extend it for use in reg_nonzero_bits because some
8666 machines (maybe most) will actually do the sign-extension
8667 and this is the conservative approach.
8669 ??? For 2.5, try to tighten up the MD files in this regard
8670 instead of this kludge. */
8672 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8673 && GET_CODE (tem) == CONST_INT
8674 && INTVAL (tem) > 0
8675 && 0 != (INTVAL (tem)
8676 & ((HOST_WIDE_INT) 1
8677 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8678 tem = GEN_INT (INTVAL (tem)
8679 | ((HOST_WIDE_INT) (-1)
8680 << GET_MODE_BITSIZE (GET_MODE (x))));
8681 #endif
8682 return tem;
8684 else if (nonzero_sign_valid && rsp->nonzero_bits)
8686 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
8688 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8689 /* We don't know anything about the upper bits. */
8690 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8691 *nonzero &= mask;
8694 return NULL;
8697 /* Return the number of bits at the high-order end of X that are known to
8698 be equal to the sign bit. X will be used in mode MODE; if MODE is
8699 VOIDmode, X will be used in its own mode. The returned value will always
8700 be between 1 and the number of bits in MODE. */
8702 static rtx
8703 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
8704 const_rtx known_x ATTRIBUTE_UNUSED,
8705 enum machine_mode known_mode
8706 ATTRIBUTE_UNUSED,
8707 unsigned int known_ret ATTRIBUTE_UNUSED,
8708 unsigned int *result)
8710 rtx tem;
8711 reg_stat_type *rsp;
8713 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8714 if (rsp->last_set_value != 0
8715 && rsp->last_set_mode == mode
8716 && ((rsp->last_set_label >= label_tick_ebb_start
8717 && rsp->last_set_label < label_tick)
8718 || (rsp->last_set_label == label_tick
8719 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8720 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8721 && REG_N_SETS (REGNO (x)) == 1
8722 && !REGNO_REG_SET_P
8723 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8725 *result = rsp->last_set_sign_bit_copies;
8726 return NULL;
8729 tem = get_last_value (x);
8730 if (tem != 0)
8731 return tem;
8733 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
8734 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8735 *result = rsp->sign_bit_copies;
8737 return NULL;
8740 /* Return the number of "extended" bits there are in X, when interpreted
8741 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8742 unsigned quantities, this is the number of high-order zero bits.
8743 For signed quantities, this is the number of copies of the sign bit
8744 minus 1. In both case, this function returns the number of "spare"
8745 bits. For example, if two quantities for which this function returns
8746 at least 1 are added, the addition is known not to overflow.
8748 This function will always return 0 unless called during combine, which
8749 implies that it must be called from a define_split. */
8751 unsigned int
8752 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
8754 if (nonzero_sign_valid == 0)
8755 return 0;
8757 return (unsignedp
8758 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8759 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8760 - floor_log2 (nonzero_bits (x, mode)))
8761 : 0)
8762 : num_sign_bit_copies (x, mode) - 1);
8765 /* This function is called from `simplify_shift_const' to merge two
8766 outer operations. Specifically, we have already found that we need
8767 to perform operation *POP0 with constant *PCONST0 at the outermost
8768 position. We would now like to also perform OP1 with constant CONST1
8769 (with *POP0 being done last).
8771 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8772 the resulting operation. *PCOMP_P is set to 1 if we would need to
8773 complement the innermost operand, otherwise it is unchanged.
8775 MODE is the mode in which the operation will be done. No bits outside
8776 the width of this mode matter. It is assumed that the width of this mode
8777 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8779 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8780 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8781 result is simply *PCONST0.
8783 If the resulting operation cannot be expressed as one operation, we
8784 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8786 static int
8787 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)
8789 enum rtx_code op0 = *pop0;
8790 HOST_WIDE_INT const0 = *pconst0;
8792 const0 &= GET_MODE_MASK (mode);
8793 const1 &= GET_MODE_MASK (mode);
8795 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8796 if (op0 == AND)
8797 const1 &= const0;
8799 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8800 if OP0 is SET. */
8802 if (op1 == UNKNOWN || op0 == SET)
8803 return 1;
8805 else if (op0 == UNKNOWN)
8806 op0 = op1, const0 = const1;
8808 else if (op0 == op1)
8810 switch (op0)
8812 case AND:
8813 const0 &= const1;
8814 break;
8815 case IOR:
8816 const0 |= const1;
8817 break;
8818 case XOR:
8819 const0 ^= const1;
8820 break;
8821 case PLUS:
8822 const0 += const1;
8823 break;
8824 case NEG:
8825 op0 = UNKNOWN;
8826 break;
8827 default:
8828 break;
8832 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8833 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8834 return 0;
8836 /* If the two constants aren't the same, we can't do anything. The
8837 remaining six cases can all be done. */
8838 else if (const0 != const1)
8839 return 0;
8841 else
8842 switch (op0)
8844 case IOR:
8845 if (op1 == AND)
8846 /* (a & b) | b == b */
8847 op0 = SET;
8848 else /* op1 == XOR */
8849 /* (a ^ b) | b == a | b */
8851 break;
8853 case XOR:
8854 if (op1 == AND)
8855 /* (a & b) ^ b == (~a) & b */
8856 op0 = AND, *pcomp_p = 1;
8857 else /* op1 == IOR */
8858 /* (a | b) ^ b == a & ~b */
8859 op0 = AND, const0 = ~const0;
8860 break;
8862 case AND:
8863 if (op1 == IOR)
8864 /* (a | b) & b == b */
8865 op0 = SET;
8866 else /* op1 == XOR */
8867 /* (a ^ b) & b) == (~a) & b */
8868 *pcomp_p = 1;
8869 break;
8870 default:
8871 break;
8874 /* Check for NO-OP cases. */
8875 const0 &= GET_MODE_MASK (mode);
8876 if (const0 == 0
8877 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8878 op0 = UNKNOWN;
8879 else if (const0 == 0 && op0 == AND)
8880 op0 = SET;
8881 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8882 && op0 == AND)
8883 op0 = UNKNOWN;
8885 /* ??? Slightly redundant with the above mask, but not entirely.
8886 Moving this above means we'd have to sign-extend the mode mask
8887 for the final test. */
8888 const0 = trunc_int_for_mode (const0, mode);
8890 *pop0 = op0;
8891 *pconst0 = const0;
8893 return 1;
8896 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8897 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
8898 simplify it. Otherwise, return a simplified value.
8900 The shift is normally computed in the widest mode we find in VAROP, as
8901 long as it isn't a different number of words than RESULT_MODE. Exceptions
8902 are ASHIFTRT and ROTATE, which are always done in their original mode. */
8904 static rtx
8905 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8906 rtx varop, int orig_count)
8908 enum rtx_code orig_code = code;
8909 rtx orig_varop = varop;
8910 int count;
8911 enum machine_mode mode = result_mode;
8912 enum machine_mode shift_mode, tmode;
8913 unsigned int mode_words
8914 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8915 /* We form (outer_op (code varop count) (outer_const)). */
8916 enum rtx_code outer_op = UNKNOWN;
8917 HOST_WIDE_INT outer_const = 0;
8918 int complement_p = 0;
8919 rtx new, x;
8921 /* Make sure and truncate the "natural" shift on the way in. We don't
8922 want to do this inside the loop as it makes it more difficult to
8923 combine shifts. */
8924 if (SHIFT_COUNT_TRUNCATED)
8925 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8927 /* If we were given an invalid count, don't do anything except exactly
8928 what was requested. */
8930 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8931 return NULL_RTX;
8933 count = orig_count;
8935 /* Unless one of the branches of the `if' in this loop does a `continue',
8936 we will `break' the loop after the `if'. */
8938 while (count != 0)
8940 /* If we have an operand of (clobber (const_int 0)), fail. */
8941 if (GET_CODE (varop) == CLOBBER)
8942 return NULL_RTX;
8944 /* If we discovered we had to complement VAROP, leave. Making a NOT
8945 here would cause an infinite loop. */
8946 if (complement_p)
8947 break;
8949 /* Convert ROTATERT to ROTATE. */
8950 if (code == ROTATERT)
8952 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8953 code = ROTATE;
8954 if (VECTOR_MODE_P (result_mode))
8955 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8956 else
8957 count = bitsize - count;
8960 /* We need to determine what mode we will do the shift in. If the
8961 shift is a right shift or a ROTATE, we must always do it in the mode
8962 it was originally done in. Otherwise, we can do it in MODE, the
8963 widest mode encountered. */
8964 shift_mode
8965 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8966 ? result_mode : mode);
8968 /* Handle cases where the count is greater than the size of the mode
8969 minus 1. For ASHIFT, use the size minus one as the count (this can
8970 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8971 take the count modulo the size. For other shifts, the result is
8972 zero.
8974 Since these shifts are being produced by the compiler by combining
8975 multiple operations, each of which are defined, we know what the
8976 result is supposed to be. */
8978 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
8980 if (code == ASHIFTRT)
8981 count = GET_MODE_BITSIZE (shift_mode) - 1;
8982 else if (code == ROTATE || code == ROTATERT)
8983 count %= GET_MODE_BITSIZE (shift_mode);
8984 else
8986 /* We can't simply return zero because there may be an
8987 outer op. */
8988 varop = const0_rtx;
8989 count = 0;
8990 break;
8994 /* An arithmetic right shift of a quantity known to be -1 or 0
8995 is a no-op. */
8996 if (code == ASHIFTRT
8997 && (num_sign_bit_copies (varop, shift_mode)
8998 == GET_MODE_BITSIZE (shift_mode)))
9000 count = 0;
9001 break;
9004 /* If we are doing an arithmetic right shift and discarding all but
9005 the sign bit copies, this is equivalent to doing a shift by the
9006 bitsize minus one. Convert it into that shift because it will often
9007 allow other simplifications. */
9009 if (code == ASHIFTRT
9010 && (count + num_sign_bit_copies (varop, shift_mode)
9011 >= GET_MODE_BITSIZE (shift_mode)))
9012 count = GET_MODE_BITSIZE (shift_mode) - 1;
9014 /* We simplify the tests below and elsewhere by converting
9015 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9016 `make_compound_operation' will convert it to an ASHIFTRT for
9017 those machines (such as VAX) that don't have an LSHIFTRT. */
9018 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9019 && code == ASHIFTRT
9020 && ((nonzero_bits (varop, shift_mode)
9021 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9022 == 0))
9023 code = LSHIFTRT;
9025 if (((code == LSHIFTRT
9026 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9027 && !(nonzero_bits (varop, shift_mode) >> count))
9028 || (code == ASHIFT
9029 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9030 && !((nonzero_bits (varop, shift_mode) << count)
9031 & GET_MODE_MASK (shift_mode))))
9032 && !side_effects_p (varop))
9033 varop = const0_rtx;
9035 switch (GET_CODE (varop))
9037 case SIGN_EXTEND:
9038 case ZERO_EXTEND:
9039 case SIGN_EXTRACT:
9040 case ZERO_EXTRACT:
9041 new = expand_compound_operation (varop);
9042 if (new != varop)
9044 varop = new;
9045 continue;
9047 break;
9049 case MEM:
9050 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9051 minus the width of a smaller mode, we can do this with a
9052 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9053 if ((code == ASHIFTRT || code == LSHIFTRT)
9054 && ! mode_dependent_address_p (XEXP (varop, 0))
9055 && ! MEM_VOLATILE_P (varop)
9056 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9057 MODE_INT, 1)) != BLKmode)
9059 new = adjust_address_nv (varop, tmode,
9060 BYTES_BIG_ENDIAN ? 0
9061 : count / BITS_PER_UNIT);
9063 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9064 : ZERO_EXTEND, mode, new);
9065 count = 0;
9066 continue;
9068 break;
9070 case SUBREG:
9071 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9072 the same number of words as what we've seen so far. Then store
9073 the widest mode in MODE. */
9074 if (subreg_lowpart_p (varop)
9075 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9076 > GET_MODE_SIZE (GET_MODE (varop)))
9077 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9078 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9079 == mode_words)
9081 varop = SUBREG_REG (varop);
9082 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9083 mode = GET_MODE (varop);
9084 continue;
9086 break;
9088 case MULT:
9089 /* Some machines use MULT instead of ASHIFT because MULT
9090 is cheaper. But it is still better on those machines to
9091 merge two shifts into one. */
9092 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9093 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9095 varop
9096 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9097 XEXP (varop, 0),
9098 GEN_INT (exact_log2 (
9099 INTVAL (XEXP (varop, 1)))));
9100 continue;
9102 break;
9104 case UDIV:
9105 /* Similar, for when divides are cheaper. */
9106 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9107 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9109 varop
9110 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9111 XEXP (varop, 0),
9112 GEN_INT (exact_log2 (
9113 INTVAL (XEXP (varop, 1)))));
9114 continue;
9116 break;
9118 case ASHIFTRT:
9119 /* If we are extracting just the sign bit of an arithmetic
9120 right shift, that shift is not needed. However, the sign
9121 bit of a wider mode may be different from what would be
9122 interpreted as the sign bit in a narrower mode, so, if
9123 the result is narrower, don't discard the shift. */
9124 if (code == LSHIFTRT
9125 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9126 && (GET_MODE_BITSIZE (result_mode)
9127 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9129 varop = XEXP (varop, 0);
9130 continue;
9133 /* ... fall through ... */
9135 case LSHIFTRT:
9136 case ASHIFT:
9137 case ROTATE:
9138 /* Here we have two nested shifts. The result is usually the
9139 AND of a new shift with a mask. We compute the result below. */
9140 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9141 && INTVAL (XEXP (varop, 1)) >= 0
9142 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9143 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9144 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9145 && !VECTOR_MODE_P (result_mode))
9147 enum rtx_code first_code = GET_CODE (varop);
9148 unsigned int first_count = INTVAL (XEXP (varop, 1));
9149 unsigned HOST_WIDE_INT mask;
9150 rtx mask_rtx;
9152 /* We have one common special case. We can't do any merging if
9153 the inner code is an ASHIFTRT of a smaller mode. However, if
9154 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9155 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9156 we can convert it to
9157 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9158 This simplifies certain SIGN_EXTEND operations. */
9159 if (code == ASHIFT && first_code == ASHIFTRT
9160 && count == (GET_MODE_BITSIZE (result_mode)
9161 - GET_MODE_BITSIZE (GET_MODE (varop))))
9163 /* C3 has the low-order C1 bits zero. */
9165 mask = (GET_MODE_MASK (mode)
9166 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9168 varop = simplify_and_const_int (NULL_RTX, result_mode,
9169 XEXP (varop, 0), mask);
9170 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9171 varop, count);
9172 count = first_count;
9173 code = ASHIFTRT;
9174 continue;
9177 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9178 than C1 high-order bits equal to the sign bit, we can convert
9179 this to either an ASHIFT or an ASHIFTRT depending on the
9180 two counts.
9182 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9184 if (code == ASHIFTRT && first_code == ASHIFT
9185 && GET_MODE (varop) == shift_mode
9186 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9187 > first_count))
9189 varop = XEXP (varop, 0);
9190 count -= first_count;
9191 if (count < 0)
9193 count = -count;
9194 code = ASHIFT;
9197 continue;
9200 /* There are some cases we can't do. If CODE is ASHIFTRT,
9201 we can only do this if FIRST_CODE is also ASHIFTRT.
9203 We can't do the case when CODE is ROTATE and FIRST_CODE is
9204 ASHIFTRT.
9206 If the mode of this shift is not the mode of the outer shift,
9207 we can't do this if either shift is a right shift or ROTATE.
9209 Finally, we can't do any of these if the mode is too wide
9210 unless the codes are the same.
9212 Handle the case where the shift codes are the same
9213 first. */
9215 if (code == first_code)
9217 if (GET_MODE (varop) != result_mode
9218 && (code == ASHIFTRT || code == LSHIFTRT
9219 || code == ROTATE))
9220 break;
9222 count += first_count;
9223 varop = XEXP (varop, 0);
9224 continue;
9227 if (code == ASHIFTRT
9228 || (code == ROTATE && first_code == ASHIFTRT)
9229 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9230 || (GET_MODE (varop) != result_mode
9231 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9232 || first_code == ROTATE
9233 || code == ROTATE)))
9234 break;
9236 /* To compute the mask to apply after the shift, shift the
9237 nonzero bits of the inner shift the same way the
9238 outer shift will. */
9240 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9242 mask_rtx
9243 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9244 GEN_INT (count));
9246 /* Give up if we can't compute an outer operation to use. */
9247 if (mask_rtx == 0
9248 || GET_CODE (mask_rtx) != CONST_INT
9249 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9250 INTVAL (mask_rtx),
9251 result_mode, &complement_p))
9252 break;
9254 /* If the shifts are in the same direction, we add the
9255 counts. Otherwise, we subtract them. */
9256 if ((code == ASHIFTRT || code == LSHIFTRT)
9257 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9258 count += first_count;
9259 else
9260 count -= first_count;
9262 /* If COUNT is positive, the new shift is usually CODE,
9263 except for the two exceptions below, in which case it is
9264 FIRST_CODE. If the count is negative, FIRST_CODE should
9265 always be used */
9266 if (count > 0
9267 && ((first_code == ROTATE && code == ASHIFT)
9268 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9269 code = first_code;
9270 else if (count < 0)
9271 code = first_code, count = -count;
9273 varop = XEXP (varop, 0);
9274 continue;
9277 /* If we have (A << B << C) for any shift, we can convert this to
9278 (A << C << B). This wins if A is a constant. Only try this if
9279 B is not a constant. */
9281 else if (GET_CODE (varop) == code
9282 && GET_CODE (XEXP (varop, 0)) == CONST_INT
9283 && GET_CODE (XEXP (varop, 1)) != CONST_INT)
9285 rtx new = simplify_const_binary_operation (code, mode,
9286 XEXP (varop, 0),
9287 GEN_INT (count));
9288 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9289 count = 0;
9290 continue;
9292 break;
9294 case NOT:
9295 /* Make this fit the case below. */
9296 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9297 GEN_INT (GET_MODE_MASK (mode)));
9298 continue;
9300 case IOR:
9301 case AND:
9302 case XOR:
9303 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9304 with C the size of VAROP - 1 and the shift is logical if
9305 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9306 we have an (le X 0) operation. If we have an arithmetic shift
9307 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9308 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9310 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9311 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9312 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9313 && (code == LSHIFTRT || code == ASHIFTRT)
9314 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9315 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9317 count = 0;
9318 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9319 const0_rtx);
9321 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9322 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9324 continue;
9327 /* If we have (shift (logical)), move the logical to the outside
9328 to allow it to possibly combine with another logical and the
9329 shift to combine with another shift. This also canonicalizes to
9330 what a ZERO_EXTRACT looks like. Also, some machines have
9331 (and (shift)) insns. */
9333 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9334 /* We can't do this if we have (ashiftrt (xor)) and the
9335 constant has its sign bit set in shift_mode. */
9336 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9337 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9338 shift_mode))
9339 && (new = simplify_const_binary_operation (code, result_mode,
9340 XEXP (varop, 1),
9341 GEN_INT (count))) != 0
9342 && GET_CODE (new) == CONST_INT
9343 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9344 INTVAL (new), result_mode, &complement_p))
9346 varop = XEXP (varop, 0);
9347 continue;
9350 /* If we can't do that, try to simplify the shift in each arm of the
9351 logical expression, make a new logical expression, and apply
9352 the inverse distributive law. This also can't be done
9353 for some (ashiftrt (xor)). */
9354 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9355 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9356 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9357 shift_mode)))
9359 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9360 XEXP (varop, 0), count);
9361 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9362 XEXP (varop, 1), count);
9364 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9365 lhs, rhs);
9366 varop = apply_distributive_law (varop);
9368 count = 0;
9369 continue;
9371 break;
9373 case EQ:
9374 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9375 says that the sign bit can be tested, FOO has mode MODE, C is
9376 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9377 that may be nonzero. */
9378 if (code == LSHIFTRT
9379 && XEXP (varop, 1) == const0_rtx
9380 && GET_MODE (XEXP (varop, 0)) == result_mode
9381 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9382 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9383 && STORE_FLAG_VALUE == -1
9384 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9385 && merge_outer_ops (&outer_op, &outer_const, XOR,
9386 (HOST_WIDE_INT) 1, result_mode,
9387 &complement_p))
9389 varop = XEXP (varop, 0);
9390 count = 0;
9391 continue;
9393 break;
9395 case NEG:
9396 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9397 than the number of bits in the mode is equivalent to A. */
9398 if (code == LSHIFTRT
9399 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9400 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9402 varop = XEXP (varop, 0);
9403 count = 0;
9404 continue;
9407 /* NEG commutes with ASHIFT since it is multiplication. Move the
9408 NEG outside to allow shifts to combine. */
9409 if (code == ASHIFT
9410 && merge_outer_ops (&outer_op, &outer_const, NEG,
9411 (HOST_WIDE_INT) 0, result_mode,
9412 &complement_p))
9414 varop = XEXP (varop, 0);
9415 continue;
9417 break;
9419 case PLUS:
9420 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9421 is one less than the number of bits in the mode is
9422 equivalent to (xor A 1). */
9423 if (code == LSHIFTRT
9424 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9425 && XEXP (varop, 1) == constm1_rtx
9426 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9427 && merge_outer_ops (&outer_op, &outer_const, XOR,
9428 (HOST_WIDE_INT) 1, result_mode,
9429 &complement_p))
9431 count = 0;
9432 varop = XEXP (varop, 0);
9433 continue;
9436 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9437 that might be nonzero in BAR are those being shifted out and those
9438 bits are known zero in FOO, we can replace the PLUS with FOO.
9439 Similarly in the other operand order. This code occurs when
9440 we are computing the size of a variable-size array. */
9442 if ((code == ASHIFTRT || code == LSHIFTRT)
9443 && count < HOST_BITS_PER_WIDE_INT
9444 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9445 && (nonzero_bits (XEXP (varop, 1), result_mode)
9446 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9448 varop = XEXP (varop, 0);
9449 continue;
9451 else if ((code == ASHIFTRT || code == LSHIFTRT)
9452 && count < HOST_BITS_PER_WIDE_INT
9453 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9454 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9455 >> count)
9456 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9457 & nonzero_bits (XEXP (varop, 1),
9458 result_mode)))
9460 varop = XEXP (varop, 1);
9461 continue;
9464 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9465 if (code == ASHIFT
9466 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9467 && (new = simplify_const_binary_operation (ASHIFT, result_mode,
9468 XEXP (varop, 1),
9469 GEN_INT (count))) != 0
9470 && GET_CODE (new) == CONST_INT
9471 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9472 INTVAL (new), result_mode, &complement_p))
9474 varop = XEXP (varop, 0);
9475 continue;
9478 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9479 signbit', and attempt to change the PLUS to an XOR and move it to
9480 the outer operation as is done above in the AND/IOR/XOR case
9481 leg for shift(logical). See details in logical handling above
9482 for reasoning in doing so. */
9483 if (code == LSHIFTRT
9484 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9485 && mode_signbit_p (result_mode, XEXP (varop, 1))
9486 && (new = simplify_const_binary_operation (code, result_mode,
9487 XEXP (varop, 1),
9488 GEN_INT (count))) != 0
9489 && GET_CODE (new) == CONST_INT
9490 && merge_outer_ops (&outer_op, &outer_const, XOR,
9491 INTVAL (new), result_mode, &complement_p))
9493 varop = XEXP (varop, 0);
9494 continue;
9497 break;
9499 case MINUS:
9500 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9501 with C the size of VAROP - 1 and the shift is logical if
9502 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9503 we have a (gt X 0) operation. If the shift is arithmetic with
9504 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9505 we have a (neg (gt X 0)) operation. */
9507 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9508 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9509 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9510 && (code == LSHIFTRT || code == ASHIFTRT)
9511 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9512 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9513 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9515 count = 0;
9516 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9517 const0_rtx);
9519 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9520 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9522 continue;
9524 break;
9526 case TRUNCATE:
9527 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9528 if the truncate does not affect the value. */
9529 if (code == LSHIFTRT
9530 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9531 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9532 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9533 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9534 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9536 rtx varop_inner = XEXP (varop, 0);
9538 varop_inner
9539 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9540 XEXP (varop_inner, 0),
9541 GEN_INT
9542 (count + INTVAL (XEXP (varop_inner, 1))));
9543 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9544 count = 0;
9545 continue;
9547 break;
9549 default:
9550 break;
9553 break;
9556 /* We need to determine what mode to do the shift in. If the shift is
9557 a right shift or ROTATE, we must always do it in the mode it was
9558 originally done in. Otherwise, we can do it in MODE, the widest mode
9559 encountered. The code we care about is that of the shift that will
9560 actually be done, not the shift that was originally requested. */
9561 shift_mode
9562 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9563 ? result_mode : mode);
9565 /* We have now finished analyzing the shift. The result should be
9566 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9567 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9568 to the result of the shift. OUTER_CONST is the relevant constant,
9569 but we must turn off all bits turned off in the shift. */
9571 if (outer_op == UNKNOWN
9572 && orig_code == code && orig_count == count
9573 && varop == orig_varop
9574 && shift_mode == GET_MODE (varop))
9575 return NULL_RTX;
9577 /* Make a SUBREG if necessary. If we can't make it, fail. */
9578 varop = gen_lowpart (shift_mode, varop);
9579 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9580 return NULL_RTX;
9582 /* If we have an outer operation and we just made a shift, it is
9583 possible that we could have simplified the shift were it not
9584 for the outer operation. So try to do the simplification
9585 recursively. */
9587 if (outer_op != UNKNOWN)
9588 x = simplify_shift_const_1 (code, shift_mode, varop, count);
9589 else
9590 x = NULL_RTX;
9592 if (x == NULL_RTX)
9593 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9595 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9596 turn off all the bits that the shift would have turned off. */
9597 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9598 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9599 GET_MODE_MASK (result_mode) >> orig_count);
9601 /* Do the remainder of the processing in RESULT_MODE. */
9602 x = gen_lowpart_or_truncate (result_mode, x);
9604 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9605 operation. */
9606 if (complement_p)
9607 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9609 if (outer_op != UNKNOWN)
9611 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9612 outer_const = trunc_int_for_mode (outer_const, result_mode);
9614 if (outer_op == AND)
9615 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9616 else if (outer_op == SET)
9618 /* This means that we have determined that the result is
9619 equivalent to a constant. This should be rare. */
9620 if (!side_effects_p (x))
9621 x = GEN_INT (outer_const);
9623 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9624 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9625 else
9626 x = simplify_gen_binary (outer_op, result_mode, x,
9627 GEN_INT (outer_const));
9630 return x;
9633 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9634 The result of the shift is RESULT_MODE. If we cannot simplify it,
9635 return X or, if it is NULL, synthesize the expression with
9636 simplify_gen_binary. Otherwise, return a simplified value.
9638 The shift is normally computed in the widest mode we find in VAROP, as
9639 long as it isn't a different number of words than RESULT_MODE. Exceptions
9640 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9642 static rtx
9643 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9644 rtx varop, int count)
9646 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9647 if (tem)
9648 return tem;
9650 if (!x)
9651 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9652 if (GET_MODE (x) != result_mode)
9653 x = gen_lowpart (result_mode, x);
9654 return x;
9658 /* Like recog, but we receive the address of a pointer to a new pattern.
9659 We try to match the rtx that the pointer points to.
9660 If that fails, we may try to modify or replace the pattern,
9661 storing the replacement into the same pointer object.
9663 Modifications include deletion or addition of CLOBBERs.
9665 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9666 the CLOBBERs are placed.
9668 The value is the final insn code from the pattern ultimately matched,
9669 or -1. */
9671 static int
9672 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9674 rtx pat = *pnewpat;
9675 int insn_code_number;
9676 int num_clobbers_to_add = 0;
9677 int i;
9678 rtx notes = 0;
9679 rtx old_notes, old_pat;
9681 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9682 we use to indicate that something didn't match. If we find such a
9683 thing, force rejection. */
9684 if (GET_CODE (pat) == PARALLEL)
9685 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9686 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9687 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9688 return -1;
9690 old_pat = PATTERN (insn);
9691 old_notes = REG_NOTES (insn);
9692 PATTERN (insn) = pat;
9693 REG_NOTES (insn) = 0;
9695 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9696 if (dump_file && (dump_flags & TDF_DETAILS))
9698 if (insn_code_number < 0)
9699 fputs ("Failed to match this instruction:\n", dump_file);
9700 else
9701 fputs ("Successfully matched this instruction:\n", dump_file);
9702 print_rtl_single (dump_file, pat);
9705 /* If it isn't, there is the possibility that we previously had an insn
9706 that clobbered some register as a side effect, but the combined
9707 insn doesn't need to do that. So try once more without the clobbers
9708 unless this represents an ASM insn. */
9710 if (insn_code_number < 0 && ! check_asm_operands (pat)
9711 && GET_CODE (pat) == PARALLEL)
9713 int pos;
9715 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9716 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9718 if (i != pos)
9719 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9720 pos++;
9723 SUBST_INT (XVECLEN (pat, 0), pos);
9725 if (pos == 1)
9726 pat = XVECEXP (pat, 0, 0);
9728 PATTERN (insn) = pat;
9729 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9730 if (dump_file && (dump_flags & TDF_DETAILS))
9732 if (insn_code_number < 0)
9733 fputs ("Failed to match this instruction:\n", dump_file);
9734 else
9735 fputs ("Successfully matched this instruction:\n", dump_file);
9736 print_rtl_single (dump_file, pat);
9739 PATTERN (insn) = old_pat;
9740 REG_NOTES (insn) = old_notes;
9742 /* Recognize all noop sets, these will be killed by followup pass. */
9743 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9744 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9746 /* If we had any clobbers to add, make a new pattern than contains
9747 them. Then check to make sure that all of them are dead. */
9748 if (num_clobbers_to_add)
9750 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9751 rtvec_alloc (GET_CODE (pat) == PARALLEL
9752 ? (XVECLEN (pat, 0)
9753 + num_clobbers_to_add)
9754 : num_clobbers_to_add + 1));
9756 if (GET_CODE (pat) == PARALLEL)
9757 for (i = 0; i < XVECLEN (pat, 0); i++)
9758 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9759 else
9760 XVECEXP (newpat, 0, 0) = pat;
9762 add_clobbers (newpat, insn_code_number);
9764 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9765 i < XVECLEN (newpat, 0); i++)
9767 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9768 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9769 return -1;
9770 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
9772 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
9773 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9774 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9777 pat = newpat;
9780 *pnewpat = pat;
9781 *pnotes = notes;
9783 return insn_code_number;
9786 /* Like gen_lowpart_general but for use by combine. In combine it
9787 is not possible to create any new pseudoregs. However, it is
9788 safe to create invalid memory addresses, because combine will
9789 try to recognize them and all they will do is make the combine
9790 attempt fail.
9792 If for some reason this cannot do its job, an rtx
9793 (clobber (const_int 0)) is returned.
9794 An insn containing that will not be recognized. */
9796 static rtx
9797 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9799 enum machine_mode imode = GET_MODE (x);
9800 unsigned int osize = GET_MODE_SIZE (omode);
9801 unsigned int isize = GET_MODE_SIZE (imode);
9802 rtx result;
9804 if (omode == imode)
9805 return x;
9807 /* Return identity if this is a CONST or symbolic reference. */
9808 if (omode == Pmode
9809 && (GET_CODE (x) == CONST
9810 || GET_CODE (x) == SYMBOL_REF
9811 || GET_CODE (x) == LABEL_REF))
9812 return x;
9814 /* We can only support MODE being wider than a word if X is a
9815 constant integer or has a mode the same size. */
9816 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9817 && ! ((imode == VOIDmode
9818 && (GET_CODE (x) == CONST_INT
9819 || GET_CODE (x) == CONST_DOUBLE))
9820 || isize == osize))
9821 goto fail;
9823 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9824 won't know what to do. So we will strip off the SUBREG here and
9825 process normally. */
9826 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9828 x = SUBREG_REG (x);
9830 /* For use in case we fall down into the address adjustments
9831 further below, we need to adjust the known mode and size of
9832 x; imode and isize, since we just adjusted x. */
9833 imode = GET_MODE (x);
9835 if (imode == omode)
9836 return x;
9838 isize = GET_MODE_SIZE (imode);
9841 result = gen_lowpart_common (omode, x);
9843 if (result)
9844 return result;
9846 if (MEM_P (x))
9848 int offset = 0;
9850 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9851 address. */
9852 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9853 goto fail;
9855 /* If we want to refer to something bigger than the original memref,
9856 generate a paradoxical subreg instead. That will force a reload
9857 of the original memref X. */
9858 if (isize < osize)
9859 return gen_rtx_SUBREG (omode, x, 0);
9861 if (WORDS_BIG_ENDIAN)
9862 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9864 /* Adjust the address so that the address-after-the-data is
9865 unchanged. */
9866 if (BYTES_BIG_ENDIAN)
9867 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9869 return adjust_address_nv (x, omode, offset);
9872 /* If X is a comparison operator, rewrite it in a new mode. This
9873 probably won't match, but may allow further simplifications. */
9874 else if (COMPARISON_P (x))
9875 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9877 /* If we couldn't simplify X any other way, just enclose it in a
9878 SUBREG. Normally, this SUBREG won't match, but some patterns may
9879 include an explicit SUBREG or we may simplify it further in combine. */
9880 else
9882 int offset = 0;
9883 rtx res;
9885 offset = subreg_lowpart_offset (omode, imode);
9886 if (imode == VOIDmode)
9888 imode = int_mode_for_mode (omode);
9889 x = gen_lowpart_common (imode, x);
9890 if (x == NULL)
9891 goto fail;
9893 res = simplify_gen_subreg (omode, x, imode, offset);
9894 if (res)
9895 return res;
9898 fail:
9899 return gen_rtx_CLOBBER (imode, const0_rtx);
9902 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9903 comparison code that will be tested.
9905 The result is a possibly different comparison code to use. *POP0 and
9906 *POP1 may be updated.
9908 It is possible that we might detect that a comparison is either always
9909 true or always false. However, we do not perform general constant
9910 folding in combine, so this knowledge isn't useful. Such tautologies
9911 should have been detected earlier. Hence we ignore all such cases. */
9913 static enum rtx_code
9914 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9916 rtx op0 = *pop0;
9917 rtx op1 = *pop1;
9918 rtx tem, tem1;
9919 int i;
9920 enum machine_mode mode, tmode;
9922 /* Try a few ways of applying the same transformation to both operands. */
9923 while (1)
9925 #ifndef WORD_REGISTER_OPERATIONS
9926 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9927 so check specially. */
9928 if (code != GTU && code != GEU && code != LTU && code != LEU
9929 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9930 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9931 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9932 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9933 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9934 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9935 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9936 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9937 && XEXP (op0, 1) == XEXP (op1, 1)
9938 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9939 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9940 && (INTVAL (XEXP (op0, 1))
9941 == (GET_MODE_BITSIZE (GET_MODE (op0))
9942 - (GET_MODE_BITSIZE
9943 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9945 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9946 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9948 #endif
9950 /* If both operands are the same constant shift, see if we can ignore the
9951 shift. We can if the shift is a rotate or if the bits shifted out of
9952 this shift are known to be zero for both inputs and if the type of
9953 comparison is compatible with the shift. */
9954 if (GET_CODE (op0) == GET_CODE (op1)
9955 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9956 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9957 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9958 && (code != GT && code != LT && code != GE && code != LE))
9959 || (GET_CODE (op0) == ASHIFTRT
9960 && (code != GTU && code != LTU
9961 && code != GEU && code != LEU)))
9962 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9963 && INTVAL (XEXP (op0, 1)) >= 0
9964 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9965 && XEXP (op0, 1) == XEXP (op1, 1))
9967 enum machine_mode mode = GET_MODE (op0);
9968 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9969 int shift_count = INTVAL (XEXP (op0, 1));
9971 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9972 mask &= (mask >> shift_count) << shift_count;
9973 else if (GET_CODE (op0) == ASHIFT)
9974 mask = (mask & (mask << shift_count)) >> shift_count;
9976 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9977 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9978 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9979 else
9980 break;
9983 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9984 SUBREGs are of the same mode, and, in both cases, the AND would
9985 be redundant if the comparison was done in the narrower mode,
9986 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9987 and the operand's possibly nonzero bits are 0xffffff01; in that case
9988 if we only care about QImode, we don't need the AND). This case
9989 occurs if the output mode of an scc insn is not SImode and
9990 STORE_FLAG_VALUE == 1 (e.g., the 386).
9992 Similarly, check for a case where the AND's are ZERO_EXTEND
9993 operations from some narrower mode even though a SUBREG is not
9994 present. */
9996 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9997 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9998 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10000 rtx inner_op0 = XEXP (op0, 0);
10001 rtx inner_op1 = XEXP (op1, 0);
10002 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10003 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10004 int changed = 0;
10006 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10007 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10008 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10009 && (GET_MODE (SUBREG_REG (inner_op0))
10010 == GET_MODE (SUBREG_REG (inner_op1)))
10011 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10012 <= HOST_BITS_PER_WIDE_INT)
10013 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10014 GET_MODE (SUBREG_REG (inner_op0)))))
10015 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10016 GET_MODE (SUBREG_REG (inner_op1))))))
10018 op0 = SUBREG_REG (inner_op0);
10019 op1 = SUBREG_REG (inner_op1);
10021 /* The resulting comparison is always unsigned since we masked
10022 off the original sign bit. */
10023 code = unsigned_condition (code);
10025 changed = 1;
10028 else if (c0 == c1)
10029 for (tmode = GET_CLASS_NARROWEST_MODE
10030 (GET_MODE_CLASS (GET_MODE (op0)));
10031 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10032 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10034 op0 = gen_lowpart (tmode, inner_op0);
10035 op1 = gen_lowpart (tmode, inner_op1);
10036 code = unsigned_condition (code);
10037 changed = 1;
10038 break;
10041 if (! changed)
10042 break;
10045 /* If both operands are NOT, we can strip off the outer operation
10046 and adjust the comparison code for swapped operands; similarly for
10047 NEG, except that this must be an equality comparison. */
10048 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10049 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10050 && (code == EQ || code == NE)))
10051 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10053 else
10054 break;
10057 /* If the first operand is a constant, swap the operands and adjust the
10058 comparison code appropriately, but don't do this if the second operand
10059 is already a constant integer. */
10060 if (swap_commutative_operands_p (op0, op1))
10062 tem = op0, op0 = op1, op1 = tem;
10063 code = swap_condition (code);
10066 /* We now enter a loop during which we will try to simplify the comparison.
10067 For the most part, we only are concerned with comparisons with zero,
10068 but some things may really be comparisons with zero but not start
10069 out looking that way. */
10071 while (GET_CODE (op1) == CONST_INT)
10073 enum machine_mode mode = GET_MODE (op0);
10074 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10075 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10076 int equality_comparison_p;
10077 int sign_bit_comparison_p;
10078 int unsigned_comparison_p;
10079 HOST_WIDE_INT const_op;
10081 /* We only want to handle integral modes. This catches VOIDmode,
10082 CCmode, and the floating-point modes. An exception is that we
10083 can handle VOIDmode if OP0 is a COMPARE or a comparison
10084 operation. */
10086 if (GET_MODE_CLASS (mode) != MODE_INT
10087 && ! (mode == VOIDmode
10088 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10089 break;
10091 /* Get the constant we are comparing against and turn off all bits
10092 not on in our mode. */
10093 const_op = INTVAL (op1);
10094 if (mode != VOIDmode)
10095 const_op = trunc_int_for_mode (const_op, mode);
10096 op1 = GEN_INT (const_op);
10098 /* If we are comparing against a constant power of two and the value
10099 being compared can only have that single bit nonzero (e.g., it was
10100 `and'ed with that bit), we can replace this with a comparison
10101 with zero. */
10102 if (const_op
10103 && (code == EQ || code == NE || code == GE || code == GEU
10104 || code == LT || code == LTU)
10105 && mode_width <= HOST_BITS_PER_WIDE_INT
10106 && exact_log2 (const_op) >= 0
10107 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10109 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10110 op1 = const0_rtx, const_op = 0;
10113 /* Similarly, if we are comparing a value known to be either -1 or
10114 0 with -1, change it to the opposite comparison against zero. */
10116 if (const_op == -1
10117 && (code == EQ || code == NE || code == GT || code == LE
10118 || code == GEU || code == LTU)
10119 && num_sign_bit_copies (op0, mode) == mode_width)
10121 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10122 op1 = const0_rtx, const_op = 0;
10125 /* Do some canonicalizations based on the comparison code. We prefer
10126 comparisons against zero and then prefer equality comparisons.
10127 If we can reduce the size of a constant, we will do that too. */
10129 switch (code)
10131 case LT:
10132 /* < C is equivalent to <= (C - 1) */
10133 if (const_op > 0)
10135 const_op -= 1;
10136 op1 = GEN_INT (const_op);
10137 code = LE;
10138 /* ... fall through to LE case below. */
10140 else
10141 break;
10143 case LE:
10144 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10145 if (const_op < 0)
10147 const_op += 1;
10148 op1 = GEN_INT (const_op);
10149 code = LT;
10152 /* If we are doing a <= 0 comparison on a value known to have
10153 a zero sign bit, we can replace this with == 0. */
10154 else if (const_op == 0
10155 && mode_width <= HOST_BITS_PER_WIDE_INT
10156 && (nonzero_bits (op0, mode)
10157 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10158 code = EQ;
10159 break;
10161 case GE:
10162 /* >= C is equivalent to > (C - 1). */
10163 if (const_op > 0)
10165 const_op -= 1;
10166 op1 = GEN_INT (const_op);
10167 code = GT;
10168 /* ... fall through to GT below. */
10170 else
10171 break;
10173 case GT:
10174 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10175 if (const_op < 0)
10177 const_op += 1;
10178 op1 = GEN_INT (const_op);
10179 code = GE;
10182 /* If we are doing a > 0 comparison on a value known to have
10183 a zero sign bit, we can replace this with != 0. */
10184 else if (const_op == 0
10185 && mode_width <= HOST_BITS_PER_WIDE_INT
10186 && (nonzero_bits (op0, mode)
10187 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10188 code = NE;
10189 break;
10191 case LTU:
10192 /* < C is equivalent to <= (C - 1). */
10193 if (const_op > 0)
10195 const_op -= 1;
10196 op1 = GEN_INT (const_op);
10197 code = LEU;
10198 /* ... fall through ... */
10201 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10202 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10203 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10205 const_op = 0, op1 = const0_rtx;
10206 code = GE;
10207 break;
10209 else
10210 break;
10212 case LEU:
10213 /* unsigned <= 0 is equivalent to == 0 */
10214 if (const_op == 0)
10215 code = EQ;
10217 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10218 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10219 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10221 const_op = 0, op1 = const0_rtx;
10222 code = GE;
10224 break;
10226 case GEU:
10227 /* >= C is equivalent to > (C - 1). */
10228 if (const_op > 1)
10230 const_op -= 1;
10231 op1 = GEN_INT (const_op);
10232 code = GTU;
10233 /* ... fall through ... */
10236 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10237 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10238 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10240 const_op = 0, op1 = const0_rtx;
10241 code = LT;
10242 break;
10244 else
10245 break;
10247 case GTU:
10248 /* unsigned > 0 is equivalent to != 0 */
10249 if (const_op == 0)
10250 code = NE;
10252 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10253 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10254 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10256 const_op = 0, op1 = const0_rtx;
10257 code = LT;
10259 break;
10261 default:
10262 break;
10265 /* Compute some predicates to simplify code below. */
10267 equality_comparison_p = (code == EQ || code == NE);
10268 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10269 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10270 || code == GEU);
10272 /* If this is a sign bit comparison and we can do arithmetic in
10273 MODE, say that we will only be needing the sign bit of OP0. */
10274 if (sign_bit_comparison_p
10275 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10276 op0 = force_to_mode (op0, mode,
10277 ((HOST_WIDE_INT) 1
10278 << (GET_MODE_BITSIZE (mode) - 1)),
10281 /* Now try cases based on the opcode of OP0. If none of the cases
10282 does a "continue", we exit this loop immediately after the
10283 switch. */
10285 switch (GET_CODE (op0))
10287 case ZERO_EXTRACT:
10288 /* If we are extracting a single bit from a variable position in
10289 a constant that has only a single bit set and are comparing it
10290 with zero, we can convert this into an equality comparison
10291 between the position and the location of the single bit. */
10292 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10293 have already reduced the shift count modulo the word size. */
10294 if (!SHIFT_COUNT_TRUNCATED
10295 && GET_CODE (XEXP (op0, 0)) == CONST_INT
10296 && XEXP (op0, 1) == const1_rtx
10297 && equality_comparison_p && const_op == 0
10298 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10300 if (BITS_BIG_ENDIAN)
10302 enum machine_mode new_mode
10303 = mode_for_extraction (EP_extzv, 1);
10304 if (new_mode == MAX_MACHINE_MODE)
10305 i = BITS_PER_WORD - 1 - i;
10306 else
10308 mode = new_mode;
10309 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10313 op0 = XEXP (op0, 2);
10314 op1 = GEN_INT (i);
10315 const_op = i;
10317 /* Result is nonzero iff shift count is equal to I. */
10318 code = reverse_condition (code);
10319 continue;
10322 /* ... fall through ... */
10324 case SIGN_EXTRACT:
10325 tem = expand_compound_operation (op0);
10326 if (tem != op0)
10328 op0 = tem;
10329 continue;
10331 break;
10333 case NOT:
10334 /* If testing for equality, we can take the NOT of the constant. */
10335 if (equality_comparison_p
10336 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10338 op0 = XEXP (op0, 0);
10339 op1 = tem;
10340 continue;
10343 /* If just looking at the sign bit, reverse the sense of the
10344 comparison. */
10345 if (sign_bit_comparison_p)
10347 op0 = XEXP (op0, 0);
10348 code = (code == GE ? LT : GE);
10349 continue;
10351 break;
10353 case NEG:
10354 /* If testing for equality, we can take the NEG of the constant. */
10355 if (equality_comparison_p
10356 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10358 op0 = XEXP (op0, 0);
10359 op1 = tem;
10360 continue;
10363 /* The remaining cases only apply to comparisons with zero. */
10364 if (const_op != 0)
10365 break;
10367 /* When X is ABS or is known positive,
10368 (neg X) is < 0 if and only if X != 0. */
10370 if (sign_bit_comparison_p
10371 && (GET_CODE (XEXP (op0, 0)) == ABS
10372 || (mode_width <= HOST_BITS_PER_WIDE_INT
10373 && (nonzero_bits (XEXP (op0, 0), mode)
10374 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10376 op0 = XEXP (op0, 0);
10377 code = (code == LT ? NE : EQ);
10378 continue;
10381 /* If we have NEG of something whose two high-order bits are the
10382 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10383 if (num_sign_bit_copies (op0, mode) >= 2)
10385 op0 = XEXP (op0, 0);
10386 code = swap_condition (code);
10387 continue;
10389 break;
10391 case ROTATE:
10392 /* If we are testing equality and our count is a constant, we
10393 can perform the inverse operation on our RHS. */
10394 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10395 && (tem = simplify_binary_operation (ROTATERT, mode,
10396 op1, XEXP (op0, 1))) != 0)
10398 op0 = XEXP (op0, 0);
10399 op1 = tem;
10400 continue;
10403 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10404 a particular bit. Convert it to an AND of a constant of that
10405 bit. This will be converted into a ZERO_EXTRACT. */
10406 if (const_op == 0 && sign_bit_comparison_p
10407 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10408 && mode_width <= HOST_BITS_PER_WIDE_INT)
10410 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10411 ((HOST_WIDE_INT) 1
10412 << (mode_width - 1
10413 - INTVAL (XEXP (op0, 1)))));
10414 code = (code == LT ? NE : EQ);
10415 continue;
10418 /* Fall through. */
10420 case ABS:
10421 /* ABS is ignorable inside an equality comparison with zero. */
10422 if (const_op == 0 && equality_comparison_p)
10424 op0 = XEXP (op0, 0);
10425 continue;
10427 break;
10429 case SIGN_EXTEND:
10430 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10431 (compare FOO CONST) if CONST fits in FOO's mode and we
10432 are either testing inequality or have an unsigned
10433 comparison with ZERO_EXTEND or a signed comparison with
10434 SIGN_EXTEND. But don't do it if we don't have a compare
10435 insn of the given mode, since we'd have to revert it
10436 later on, and then we wouldn't know whether to sign- or
10437 zero-extend. */
10438 mode = GET_MODE (XEXP (op0, 0));
10439 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10440 && ! unsigned_comparison_p
10441 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10442 && ((unsigned HOST_WIDE_INT) const_op
10443 < (((unsigned HOST_WIDE_INT) 1
10444 << (GET_MODE_BITSIZE (mode) - 1))))
10445 && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10447 op0 = XEXP (op0, 0);
10448 continue;
10450 break;
10452 case SUBREG:
10453 /* Check for the case where we are comparing A - C1 with C2, that is
10455 (subreg:MODE (plus (A) (-C1))) op (C2)
10457 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10458 comparison in the wider mode. One of the following two conditions
10459 must be true in order for this to be valid:
10461 1. The mode extension results in the same bit pattern being added
10462 on both sides and the comparison is equality or unsigned. As
10463 C2 has been truncated to fit in MODE, the pattern can only be
10464 all 0s or all 1s.
10466 2. The mode extension results in the sign bit being copied on
10467 each side.
10469 The difficulty here is that we have predicates for A but not for
10470 (A - C1) so we need to check that C1 is within proper bounds so
10471 as to perturbate A as little as possible. */
10473 if (mode_width <= HOST_BITS_PER_WIDE_INT
10474 && subreg_lowpart_p (op0)
10475 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10476 && GET_CODE (SUBREG_REG (op0)) == PLUS
10477 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10479 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10480 rtx a = XEXP (SUBREG_REG (op0), 0);
10481 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10483 if ((c1 > 0
10484 && (unsigned HOST_WIDE_INT) c1
10485 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10486 && (equality_comparison_p || unsigned_comparison_p)
10487 /* (A - C1) zero-extends if it is positive and sign-extends
10488 if it is negative, C2 both zero- and sign-extends. */
10489 && ((0 == (nonzero_bits (a, inner_mode)
10490 & ~GET_MODE_MASK (mode))
10491 && const_op >= 0)
10492 /* (A - C1) sign-extends if it is positive and 1-extends
10493 if it is negative, C2 both sign- and 1-extends. */
10494 || (num_sign_bit_copies (a, inner_mode)
10495 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10496 - mode_width)
10497 && const_op < 0)))
10498 || ((unsigned HOST_WIDE_INT) c1
10499 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10500 /* (A - C1) always sign-extends, like C2. */
10501 && num_sign_bit_copies (a, inner_mode)
10502 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10503 - (mode_width - 1))))
10505 op0 = SUBREG_REG (op0);
10506 continue;
10510 /* If the inner mode is narrower and we are extracting the low part,
10511 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10512 if (subreg_lowpart_p (op0)
10513 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10514 /* Fall through */ ;
10515 else
10516 break;
10518 /* ... fall through ... */
10520 case ZERO_EXTEND:
10521 mode = GET_MODE (XEXP (op0, 0));
10522 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10523 && (unsigned_comparison_p || equality_comparison_p)
10524 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10525 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10526 && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10528 op0 = XEXP (op0, 0);
10529 continue;
10531 break;
10533 case PLUS:
10534 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10535 this for equality comparisons due to pathological cases involving
10536 overflows. */
10537 if (equality_comparison_p
10538 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10539 op1, XEXP (op0, 1))))
10541 op0 = XEXP (op0, 0);
10542 op1 = tem;
10543 continue;
10546 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10547 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10548 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10550 op0 = XEXP (XEXP (op0, 0), 0);
10551 code = (code == LT ? EQ : NE);
10552 continue;
10554 break;
10556 case MINUS:
10557 /* We used to optimize signed comparisons against zero, but that
10558 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10559 arrive here as equality comparisons, or (GEU, LTU) are
10560 optimized away. No need to special-case them. */
10562 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10563 (eq B (minus A C)), whichever simplifies. We can only do
10564 this for equality comparisons due to pathological cases involving
10565 overflows. */
10566 if (equality_comparison_p
10567 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10568 XEXP (op0, 1), op1)))
10570 op0 = XEXP (op0, 0);
10571 op1 = tem;
10572 continue;
10575 if (equality_comparison_p
10576 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10577 XEXP (op0, 0), op1)))
10579 op0 = XEXP (op0, 1);
10580 op1 = tem;
10581 continue;
10584 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10585 of bits in X minus 1, is one iff X > 0. */
10586 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10587 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10588 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10589 == mode_width - 1
10590 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10592 op0 = XEXP (op0, 1);
10593 code = (code == GE ? LE : GT);
10594 continue;
10596 break;
10598 case XOR:
10599 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10600 if C is zero or B is a constant. */
10601 if (equality_comparison_p
10602 && 0 != (tem = simplify_binary_operation (XOR, mode,
10603 XEXP (op0, 1), op1)))
10605 op0 = XEXP (op0, 0);
10606 op1 = tem;
10607 continue;
10609 break;
10611 case EQ: case NE:
10612 case UNEQ: case LTGT:
10613 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10614 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10615 case UNORDERED: case ORDERED:
10616 /* We can't do anything if OP0 is a condition code value, rather
10617 than an actual data value. */
10618 if (const_op != 0
10619 || CC0_P (XEXP (op0, 0))
10620 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10621 break;
10623 /* Get the two operands being compared. */
10624 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10625 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10626 else
10627 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10629 /* Check for the cases where we simply want the result of the
10630 earlier test or the opposite of that result. */
10631 if (code == NE || code == EQ
10632 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10633 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10634 && (STORE_FLAG_VALUE
10635 & (((HOST_WIDE_INT) 1
10636 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10637 && (code == LT || code == GE)))
10639 enum rtx_code new_code;
10640 if (code == LT || code == NE)
10641 new_code = GET_CODE (op0);
10642 else
10643 new_code = reversed_comparison_code (op0, NULL);
10645 if (new_code != UNKNOWN)
10647 code = new_code;
10648 op0 = tem;
10649 op1 = tem1;
10650 continue;
10653 break;
10655 case IOR:
10656 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10657 iff X <= 0. */
10658 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10659 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10660 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10662 op0 = XEXP (op0, 1);
10663 code = (code == GE ? GT : LE);
10664 continue;
10666 break;
10668 case AND:
10669 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10670 will be converted to a ZERO_EXTRACT later. */
10671 if (const_op == 0 && equality_comparison_p
10672 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10673 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10675 op0 = simplify_and_const_int
10676 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10677 XEXP (op0, 1),
10678 XEXP (XEXP (op0, 0), 1)),
10679 (HOST_WIDE_INT) 1);
10680 continue;
10683 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10684 zero and X is a comparison and C1 and C2 describe only bits set
10685 in STORE_FLAG_VALUE, we can compare with X. */
10686 if (const_op == 0 && equality_comparison_p
10687 && mode_width <= HOST_BITS_PER_WIDE_INT
10688 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10689 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10690 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10691 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10692 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10694 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10695 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10696 if ((~STORE_FLAG_VALUE & mask) == 0
10697 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10698 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10699 && COMPARISON_P (tem))))
10701 op0 = XEXP (XEXP (op0, 0), 0);
10702 continue;
10706 /* If we are doing an equality comparison of an AND of a bit equal
10707 to the sign bit, replace this with a LT or GE comparison of
10708 the underlying value. */
10709 if (equality_comparison_p
10710 && const_op == 0
10711 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10712 && mode_width <= HOST_BITS_PER_WIDE_INT
10713 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10714 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10716 op0 = XEXP (op0, 0);
10717 code = (code == EQ ? GE : LT);
10718 continue;
10721 /* If this AND operation is really a ZERO_EXTEND from a narrower
10722 mode, the constant fits within that mode, and this is either an
10723 equality or unsigned comparison, try to do this comparison in
10724 the narrower mode.
10726 Note that in:
10728 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10729 -> (ne:DI (reg:SI 4) (const_int 0))
10731 unless TRULY_NOOP_TRUNCATION allows it or the register is
10732 known to hold a value of the required mode the
10733 transformation is invalid. */
10734 if ((equality_comparison_p || unsigned_comparison_p)
10735 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10736 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10737 & GET_MODE_MASK (mode))
10738 + 1)) >= 0
10739 && const_op >> i == 0
10740 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10741 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10742 GET_MODE_BITSIZE (GET_MODE (op0)))
10743 || (REG_P (XEXP (op0, 0))
10744 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10746 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10747 continue;
10750 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10751 fits in both M1 and M2 and the SUBREG is either paradoxical
10752 or represents the low part, permute the SUBREG and the AND
10753 and try again. */
10754 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10756 unsigned HOST_WIDE_INT c1;
10757 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10758 /* Require an integral mode, to avoid creating something like
10759 (AND:SF ...). */
10760 if (SCALAR_INT_MODE_P (tmode)
10761 /* It is unsafe to commute the AND into the SUBREG if the
10762 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10763 not defined. As originally written the upper bits
10764 have a defined value due to the AND operation.
10765 However, if we commute the AND inside the SUBREG then
10766 they no longer have defined values and the meaning of
10767 the code has been changed. */
10768 && (0
10769 #ifdef WORD_REGISTER_OPERATIONS
10770 || (mode_width > GET_MODE_BITSIZE (tmode)
10771 && mode_width <= BITS_PER_WORD)
10772 #endif
10773 || (mode_width <= GET_MODE_BITSIZE (tmode)
10774 && subreg_lowpart_p (XEXP (op0, 0))))
10775 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10776 && mode_width <= HOST_BITS_PER_WIDE_INT
10777 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10778 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10779 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10780 && c1 != mask
10781 && c1 != GET_MODE_MASK (tmode))
10783 op0 = simplify_gen_binary (AND, tmode,
10784 SUBREG_REG (XEXP (op0, 0)),
10785 gen_int_mode (c1, tmode));
10786 op0 = gen_lowpart (mode, op0);
10787 continue;
10791 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10792 if (const_op == 0 && equality_comparison_p
10793 && XEXP (op0, 1) == const1_rtx
10794 && GET_CODE (XEXP (op0, 0)) == NOT)
10796 op0 = simplify_and_const_int
10797 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10798 code = (code == NE ? EQ : NE);
10799 continue;
10802 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10803 (eq (and (lshiftrt X) 1) 0).
10804 Also handle the case where (not X) is expressed using xor. */
10805 if (const_op == 0 && equality_comparison_p
10806 && XEXP (op0, 1) == const1_rtx
10807 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10809 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10810 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10812 if (GET_CODE (shift_op) == NOT
10813 || (GET_CODE (shift_op) == XOR
10814 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10815 && GET_CODE (shift_count) == CONST_INT
10816 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10817 && (INTVAL (XEXP (shift_op, 1))
10818 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10820 op0 = simplify_and_const_int
10821 (NULL_RTX, mode,
10822 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10823 (HOST_WIDE_INT) 1);
10824 code = (code == NE ? EQ : NE);
10825 continue;
10828 break;
10830 case ASHIFT:
10831 /* If we have (compare (ashift FOO N) (const_int C)) and
10832 the high order N bits of FOO (N+1 if an inequality comparison)
10833 are known to be zero, we can do this by comparing FOO with C
10834 shifted right N bits so long as the low-order N bits of C are
10835 zero. */
10836 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10837 && INTVAL (XEXP (op0, 1)) >= 0
10838 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10839 < HOST_BITS_PER_WIDE_INT)
10840 && ((const_op
10841 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10842 && mode_width <= HOST_BITS_PER_WIDE_INT
10843 && (nonzero_bits (XEXP (op0, 0), mode)
10844 & ~(mask >> (INTVAL (XEXP (op0, 1))
10845 + ! equality_comparison_p))) == 0)
10847 /* We must perform a logical shift, not an arithmetic one,
10848 as we want the top N bits of C to be zero. */
10849 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10851 temp >>= INTVAL (XEXP (op0, 1));
10852 op1 = gen_int_mode (temp, mode);
10853 op0 = XEXP (op0, 0);
10854 continue;
10857 /* If we are doing a sign bit comparison, it means we are testing
10858 a particular bit. Convert it to the appropriate AND. */
10859 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10860 && mode_width <= HOST_BITS_PER_WIDE_INT)
10862 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10863 ((HOST_WIDE_INT) 1
10864 << (mode_width - 1
10865 - INTVAL (XEXP (op0, 1)))));
10866 code = (code == LT ? NE : EQ);
10867 continue;
10870 /* If this an equality comparison with zero and we are shifting
10871 the low bit to the sign bit, we can convert this to an AND of the
10872 low-order bit. */
10873 if (const_op == 0 && equality_comparison_p
10874 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10875 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10876 == mode_width - 1)
10878 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10879 (HOST_WIDE_INT) 1);
10880 continue;
10882 break;
10884 case ASHIFTRT:
10885 /* If this is an equality comparison with zero, we can do this
10886 as a logical shift, which might be much simpler. */
10887 if (equality_comparison_p && const_op == 0
10888 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10890 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10891 XEXP (op0, 0),
10892 INTVAL (XEXP (op0, 1)));
10893 continue;
10896 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10897 do the comparison in a narrower mode. */
10898 if (! unsigned_comparison_p
10899 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10900 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10901 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10902 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10903 MODE_INT, 1)) != BLKmode
10904 && (((unsigned HOST_WIDE_INT) const_op
10905 + (GET_MODE_MASK (tmode) >> 1) + 1)
10906 <= GET_MODE_MASK (tmode)))
10908 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10909 continue;
10912 /* Likewise if OP0 is a PLUS of a sign extension with a
10913 constant, which is usually represented with the PLUS
10914 between the shifts. */
10915 if (! unsigned_comparison_p
10916 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10917 && GET_CODE (XEXP (op0, 0)) == PLUS
10918 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10919 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10920 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10921 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10922 MODE_INT, 1)) != BLKmode
10923 && (((unsigned HOST_WIDE_INT) const_op
10924 + (GET_MODE_MASK (tmode) >> 1) + 1)
10925 <= GET_MODE_MASK (tmode)))
10927 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10928 rtx add_const = XEXP (XEXP (op0, 0), 1);
10929 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10930 add_const, XEXP (op0, 1));
10932 op0 = simplify_gen_binary (PLUS, tmode,
10933 gen_lowpart (tmode, inner),
10934 new_const);
10935 continue;
10938 /* ... fall through ... */
10939 case LSHIFTRT:
10940 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10941 the low order N bits of FOO are known to be zero, we can do this
10942 by comparing FOO with C shifted left N bits so long as no
10943 overflow occurs. */
10944 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10945 && INTVAL (XEXP (op0, 1)) >= 0
10946 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10947 && mode_width <= HOST_BITS_PER_WIDE_INT
10948 && (nonzero_bits (XEXP (op0, 0), mode)
10949 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10950 && (((unsigned HOST_WIDE_INT) const_op
10951 + (GET_CODE (op0) != LSHIFTRT
10952 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10953 + 1)
10954 : 0))
10955 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10957 /* If the shift was logical, then we must make the condition
10958 unsigned. */
10959 if (GET_CODE (op0) == LSHIFTRT)
10960 code = unsigned_condition (code);
10962 const_op <<= INTVAL (XEXP (op0, 1));
10963 op1 = GEN_INT (const_op);
10964 op0 = XEXP (op0, 0);
10965 continue;
10968 /* If we are using this shift to extract just the sign bit, we
10969 can replace this with an LT or GE comparison. */
10970 if (const_op == 0
10971 && (equality_comparison_p || sign_bit_comparison_p)
10972 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10973 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10974 == mode_width - 1)
10976 op0 = XEXP (op0, 0);
10977 code = (code == NE || code == GT ? LT : GE);
10978 continue;
10980 break;
10982 default:
10983 break;
10986 break;
10989 /* Now make any compound operations involved in this comparison. Then,
10990 check for an outmost SUBREG on OP0 that is not doing anything or is
10991 paradoxical. The latter transformation must only be performed when
10992 it is known that the "extra" bits will be the same in op0 and op1 or
10993 that they don't matter. There are three cases to consider:
10995 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10996 care bits and we can assume they have any convenient value. So
10997 making the transformation is safe.
10999 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11000 In this case the upper bits of op0 are undefined. We should not make
11001 the simplification in that case as we do not know the contents of
11002 those bits.
11004 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11005 UNKNOWN. In that case we know those bits are zeros or ones. We must
11006 also be sure that they are the same as the upper bits of op1.
11008 We can never remove a SUBREG for a non-equality comparison because
11009 the sign bit is in a different place in the underlying object. */
11011 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11012 op1 = make_compound_operation (op1, SET);
11014 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11015 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11016 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11017 && (code == NE || code == EQ))
11019 if (GET_MODE_SIZE (GET_MODE (op0))
11020 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11022 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11023 implemented. */
11024 if (REG_P (SUBREG_REG (op0)))
11026 op0 = SUBREG_REG (op0);
11027 op1 = gen_lowpart (GET_MODE (op0), op1);
11030 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11031 <= HOST_BITS_PER_WIDE_INT)
11032 && (nonzero_bits (SUBREG_REG (op0),
11033 GET_MODE (SUBREG_REG (op0)))
11034 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11036 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11038 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11039 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11040 op0 = SUBREG_REG (op0), op1 = tem;
11044 /* We now do the opposite procedure: Some machines don't have compare
11045 insns in all modes. If OP0's mode is an integer mode smaller than a
11046 word and we can't do a compare in that mode, see if there is a larger
11047 mode for which we can do the compare. There are a number of cases in
11048 which we can use the wider mode. */
11050 mode = GET_MODE (op0);
11051 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11052 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11053 && ! have_insn_for (COMPARE, mode))
11054 for (tmode = GET_MODE_WIDER_MODE (mode);
11055 (tmode != VOIDmode
11056 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11057 tmode = GET_MODE_WIDER_MODE (tmode))
11058 if (have_insn_for (COMPARE, tmode))
11060 int zero_extended;
11062 /* If the only nonzero bits in OP0 and OP1 are those in the
11063 narrower mode and this is an equality or unsigned comparison,
11064 we can use the wider mode. Similarly for sign-extended
11065 values, in which case it is true for all comparisons. */
11066 zero_extended = ((code == EQ || code == NE
11067 || code == GEU || code == GTU
11068 || code == LEU || code == LTU)
11069 && (nonzero_bits (op0, tmode)
11070 & ~GET_MODE_MASK (mode)) == 0
11071 && ((GET_CODE (op1) == CONST_INT
11072 || (nonzero_bits (op1, tmode)
11073 & ~GET_MODE_MASK (mode)) == 0)));
11075 if (zero_extended
11076 || ((num_sign_bit_copies (op0, tmode)
11077 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11078 - GET_MODE_BITSIZE (mode)))
11079 && (num_sign_bit_copies (op1, tmode)
11080 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11081 - GET_MODE_BITSIZE (mode)))))
11083 /* If OP0 is an AND and we don't have an AND in MODE either,
11084 make a new AND in the proper mode. */
11085 if (GET_CODE (op0) == AND
11086 && !have_insn_for (AND, mode))
11087 op0 = simplify_gen_binary (AND, tmode,
11088 gen_lowpart (tmode,
11089 XEXP (op0, 0)),
11090 gen_lowpart (tmode,
11091 XEXP (op0, 1)));
11093 op0 = gen_lowpart (tmode, op0);
11094 if (zero_extended && GET_CODE (op1) == CONST_INT)
11095 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11096 op1 = gen_lowpart (tmode, op1);
11097 break;
11100 /* If this is a test for negative, we can make an explicit
11101 test of the sign bit. */
11103 if (op1 == const0_rtx && (code == LT || code == GE)
11104 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11106 op0 = simplify_gen_binary (AND, tmode,
11107 gen_lowpart (tmode, op0),
11108 GEN_INT ((HOST_WIDE_INT) 1
11109 << (GET_MODE_BITSIZE (mode)
11110 - 1)));
11111 code = (code == LT) ? NE : EQ;
11112 break;
11116 #ifdef CANONICALIZE_COMPARISON
11117 /* If this machine only supports a subset of valid comparisons, see if we
11118 can convert an unsupported one into a supported one. */
11119 CANONICALIZE_COMPARISON (code, op0, op1);
11120 #endif
11122 *pop0 = op0;
11123 *pop1 = op1;
11125 return code;
11128 /* Utility function for record_value_for_reg. Count number of
11129 rtxs in X. */
11130 static int
11131 count_rtxs (rtx x)
11133 enum rtx_code code = GET_CODE (x);
11134 const char *fmt;
11135 int i, ret = 1;
11137 if (GET_RTX_CLASS (code) == '2'
11138 || GET_RTX_CLASS (code) == 'c')
11140 rtx x0 = XEXP (x, 0);
11141 rtx x1 = XEXP (x, 1);
11143 if (x0 == x1)
11144 return 1 + 2 * count_rtxs (x0);
11146 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11147 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11148 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11149 return 2 + 2 * count_rtxs (x0)
11150 + count_rtxs (x == XEXP (x1, 0)
11151 ? XEXP (x1, 1) : XEXP (x1, 0));
11153 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11154 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11155 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11156 return 2 + 2 * count_rtxs (x1)
11157 + count_rtxs (x == XEXP (x0, 0)
11158 ? XEXP (x0, 1) : XEXP (x0, 0));
11161 fmt = GET_RTX_FORMAT (code);
11162 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11163 if (fmt[i] == 'e')
11164 ret += count_rtxs (XEXP (x, i));
11166 return ret;
11169 /* Utility function for following routine. Called when X is part of a value
11170 being stored into last_set_value. Sets last_set_table_tick
11171 for each register mentioned. Similar to mention_regs in cse.c */
11173 static void
11174 update_table_tick (rtx x)
11176 enum rtx_code code = GET_CODE (x);
11177 const char *fmt = GET_RTX_FORMAT (code);
11178 int i;
11180 if (code == REG)
11182 unsigned int regno = REGNO (x);
11183 unsigned int endregno = END_REGNO (x);
11184 unsigned int r;
11186 for (r = regno; r < endregno; r++)
11188 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11189 rsp->last_set_table_tick = label_tick;
11192 return;
11195 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11196 /* Note that we can't have an "E" in values stored; see
11197 get_last_value_validate. */
11198 if (fmt[i] == 'e')
11200 /* Check for identical subexpressions. If x contains
11201 identical subexpression we only have to traverse one of
11202 them. */
11203 if (i == 0 && ARITHMETIC_P (x))
11205 /* Note that at this point x1 has already been
11206 processed. */
11207 rtx x0 = XEXP (x, 0);
11208 rtx x1 = XEXP (x, 1);
11210 /* If x0 and x1 are identical then there is no need to
11211 process x0. */
11212 if (x0 == x1)
11213 break;
11215 /* If x0 is identical to a subexpression of x1 then while
11216 processing x1, x0 has already been processed. Thus we
11217 are done with x. */
11218 if (ARITHMETIC_P (x1)
11219 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11220 break;
11222 /* If x1 is identical to a subexpression of x0 then we
11223 still have to process the rest of x0. */
11224 if (ARITHMETIC_P (x0)
11225 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11227 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11228 break;
11232 update_table_tick (XEXP (x, i));
11236 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11237 are saying that the register is clobbered and we no longer know its
11238 value. If INSN is zero, don't update reg_stat[].last_set; this is
11239 only permitted with VALUE also zero and is used to invalidate the
11240 register. */
11242 static void
11243 record_value_for_reg (rtx reg, rtx insn, rtx value)
11245 unsigned int regno = REGNO (reg);
11246 unsigned int endregno = END_REGNO (reg);
11247 unsigned int i;
11248 reg_stat_type *rsp;
11250 /* If VALUE contains REG and we have a previous value for REG, substitute
11251 the previous value. */
11252 if (value && insn && reg_overlap_mentioned_p (reg, value))
11254 rtx tem;
11256 /* Set things up so get_last_value is allowed to see anything set up to
11257 our insn. */
11258 subst_low_luid = DF_INSN_LUID (insn);
11259 tem = get_last_value (reg);
11261 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11262 it isn't going to be useful and will take a lot of time to process,
11263 so just use the CLOBBER. */
11265 if (tem)
11267 if (ARITHMETIC_P (tem)
11268 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11269 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11270 tem = XEXP (tem, 0);
11271 else if (count_occurrences (value, reg, 1) >= 2)
11273 /* If there are two or more occurrences of REG in VALUE,
11274 prevent the value from growing too much. */
11275 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11276 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11279 value = replace_rtx (copy_rtx (value), reg, tem);
11283 /* For each register modified, show we don't know its value, that
11284 we don't know about its bitwise content, that its value has been
11285 updated, and that we don't know the location of the death of the
11286 register. */
11287 for (i = regno; i < endregno; i++)
11289 rsp = VEC_index (reg_stat_type, reg_stat, i);
11291 if (insn)
11292 rsp->last_set = insn;
11294 rsp->last_set_value = 0;
11295 rsp->last_set_mode = 0;
11296 rsp->last_set_nonzero_bits = 0;
11297 rsp->last_set_sign_bit_copies = 0;
11298 rsp->last_death = 0;
11299 rsp->truncated_to_mode = 0;
11302 /* Mark registers that are being referenced in this value. */
11303 if (value)
11304 update_table_tick (value);
11306 /* Now update the status of each register being set.
11307 If someone is using this register in this block, set this register
11308 to invalid since we will get confused between the two lives in this
11309 basic block. This makes using this register always invalid. In cse, we
11310 scan the table to invalidate all entries using this register, but this
11311 is too much work for us. */
11313 for (i = regno; i < endregno; i++)
11315 rsp = VEC_index (reg_stat_type, reg_stat, i);
11316 rsp->last_set_label = label_tick;
11317 if (!insn
11318 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11319 rsp->last_set_invalid = 1;
11320 else
11321 rsp->last_set_invalid = 0;
11324 /* The value being assigned might refer to X (like in "x++;"). In that
11325 case, we must replace it with (clobber (const_int 0)) to prevent
11326 infinite loops. */
11327 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11328 if (value && ! get_last_value_validate (&value, insn,
11329 rsp->last_set_label, 0))
11331 value = copy_rtx (value);
11332 if (! get_last_value_validate (&value, insn,
11333 rsp->last_set_label, 1))
11334 value = 0;
11337 /* For the main register being modified, update the value, the mode, the
11338 nonzero bits, and the number of sign bit copies. */
11340 rsp->last_set_value = value;
11342 if (value)
11344 enum machine_mode mode = GET_MODE (reg);
11345 subst_low_luid = DF_INSN_LUID (insn);
11346 rsp->last_set_mode = mode;
11347 if (GET_MODE_CLASS (mode) == MODE_INT
11348 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11349 mode = nonzero_bits_mode;
11350 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11351 rsp->last_set_sign_bit_copies
11352 = num_sign_bit_copies (value, GET_MODE (reg));
11356 /* Called via note_stores from record_dead_and_set_regs to handle one
11357 SET or CLOBBER in an insn. DATA is the instruction in which the
11358 set is occurring. */
11360 static void
11361 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11363 rtx record_dead_insn = (rtx) data;
11365 if (GET_CODE (dest) == SUBREG)
11366 dest = SUBREG_REG (dest);
11368 if (!record_dead_insn)
11370 if (REG_P (dest))
11371 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11372 return;
11375 if (REG_P (dest))
11377 /* If we are setting the whole register, we know its value. Otherwise
11378 show that we don't know the value. We can handle SUBREG in
11379 some cases. */
11380 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11381 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11382 else if (GET_CODE (setter) == SET
11383 && GET_CODE (SET_DEST (setter)) == SUBREG
11384 && SUBREG_REG (SET_DEST (setter)) == dest
11385 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11386 && subreg_lowpart_p (SET_DEST (setter)))
11387 record_value_for_reg (dest, record_dead_insn,
11388 gen_lowpart (GET_MODE (dest),
11389 SET_SRC (setter)));
11390 else
11391 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11393 else if (MEM_P (dest)
11394 /* Ignore pushes, they clobber nothing. */
11395 && ! push_operand (dest, GET_MODE (dest)))
11396 mem_last_set = DF_INSN_LUID (record_dead_insn);
11399 /* Update the records of when each REG was most recently set or killed
11400 for the things done by INSN. This is the last thing done in processing
11401 INSN in the combiner loop.
11403 We update reg_stat[], in particular fields last_set, last_set_value,
11404 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11405 last_death, and also the similar information mem_last_set (which insn
11406 most recently modified memory) and last_call_luid (which insn was the
11407 most recent subroutine call). */
11409 static void
11410 record_dead_and_set_regs (rtx insn)
11412 rtx link;
11413 unsigned int i;
11415 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11417 if (REG_NOTE_KIND (link) == REG_DEAD
11418 && REG_P (XEXP (link, 0)))
11420 unsigned int regno = REGNO (XEXP (link, 0));
11421 unsigned int endregno = END_REGNO (XEXP (link, 0));
11423 for (i = regno; i < endregno; i++)
11425 reg_stat_type *rsp;
11427 rsp = VEC_index (reg_stat_type, reg_stat, i);
11428 rsp->last_death = insn;
11431 else if (REG_NOTE_KIND (link) == REG_INC)
11432 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11435 if (CALL_P (insn))
11437 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11438 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11440 reg_stat_type *rsp;
11442 rsp = VEC_index (reg_stat_type, reg_stat, i);
11443 rsp->last_set_invalid = 1;
11444 rsp->last_set = insn;
11445 rsp->last_set_value = 0;
11446 rsp->last_set_mode = 0;
11447 rsp->last_set_nonzero_bits = 0;
11448 rsp->last_set_sign_bit_copies = 0;
11449 rsp->last_death = 0;
11450 rsp->truncated_to_mode = 0;
11453 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11455 /* We can't combine into a call pattern. Remember, though, that
11456 the return value register is set at this LUID. We could
11457 still replace a register with the return value from the
11458 wrong subroutine call! */
11459 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11461 else
11462 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11465 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11466 register present in the SUBREG, so for each such SUBREG go back and
11467 adjust nonzero and sign bit information of the registers that are
11468 known to have some zero/sign bits set.
11470 This is needed because when combine blows the SUBREGs away, the
11471 information on zero/sign bits is lost and further combines can be
11472 missed because of that. */
11474 static void
11475 record_promoted_value (rtx insn, rtx subreg)
11477 rtx links, set;
11478 unsigned int regno = REGNO (SUBREG_REG (subreg));
11479 enum machine_mode mode = GET_MODE (subreg);
11481 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11482 return;
11484 for (links = LOG_LINKS (insn); links;)
11486 reg_stat_type *rsp;
11488 insn = XEXP (links, 0);
11489 set = single_set (insn);
11491 if (! set || !REG_P (SET_DEST (set))
11492 || REGNO (SET_DEST (set)) != regno
11493 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11495 links = XEXP (links, 1);
11496 continue;
11499 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11500 if (rsp->last_set == insn)
11502 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11503 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11506 if (REG_P (SET_SRC (set)))
11508 regno = REGNO (SET_SRC (set));
11509 links = LOG_LINKS (insn);
11511 else
11512 break;
11516 /* Check if X, a register, is known to contain a value already
11517 truncated to MODE. In this case we can use a subreg to refer to
11518 the truncated value even though in the generic case we would need
11519 an explicit truncation. */
11521 static bool
11522 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11524 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11525 enum machine_mode truncated = rsp->truncated_to_mode;
11527 if (truncated == 0
11528 || rsp->truncation_label < label_tick_ebb_start)
11529 return false;
11530 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11531 return true;
11532 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11533 GET_MODE_BITSIZE (truncated)))
11534 return true;
11535 return false;
11538 /* X is a REG or a SUBREG. If X is some sort of a truncation record
11539 it. For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11540 a truncate into a subreg using this information. */
11542 static void
11543 record_truncated_value (rtx x)
11545 enum machine_mode truncated_mode;
11546 reg_stat_type *rsp;
11548 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11550 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11551 truncated_mode = GET_MODE (x);
11553 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11554 return;
11556 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11557 GET_MODE_BITSIZE (original_mode)))
11558 return;
11560 x = SUBREG_REG (x);
11562 /* ??? For hard-regs we now record everything. We might be able to
11563 optimize this using last_set_mode. */
11564 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11565 truncated_mode = GET_MODE (x);
11566 else
11567 return;
11569 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11570 if (rsp->truncated_to_mode == 0
11571 || rsp->truncation_label < label_tick_ebb_start
11572 || (GET_MODE_SIZE (truncated_mode)
11573 < GET_MODE_SIZE (rsp->truncated_to_mode)))
11575 rsp->truncated_to_mode = truncated_mode;
11576 rsp->truncation_label = label_tick;
11580 /* Scan X for promoted SUBREGs and truncated REGs. For each one
11581 found, note what it implies to the registers used in it. */
11583 static void
11584 check_conversions (rtx insn, rtx x)
11586 if (GET_CODE (x) == SUBREG || REG_P (x))
11588 if (GET_CODE (x) == SUBREG
11589 && SUBREG_PROMOTED_VAR_P (x)
11590 && REG_P (SUBREG_REG (x)))
11591 record_promoted_value (insn, x);
11593 record_truncated_value (x);
11595 else
11597 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11598 int i, j;
11600 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11601 switch (format[i])
11603 case 'e':
11604 check_conversions (insn, XEXP (x, i));
11605 break;
11606 case 'V':
11607 case 'E':
11608 if (XVEC (x, i) != 0)
11609 for (j = 0; j < XVECLEN (x, i); j++)
11610 check_conversions (insn, XVECEXP (x, i, j));
11611 break;
11616 /* Utility routine for the following function. Verify that all the registers
11617 mentioned in *LOC are valid when *LOC was part of a value set when
11618 label_tick == TICK. Return 0 if some are not.
11620 If REPLACE is nonzero, replace the invalid reference with
11621 (clobber (const_int 0)) and return 1. This replacement is useful because
11622 we often can get useful information about the form of a value (e.g., if
11623 it was produced by a shift that always produces -1 or 0) even though
11624 we don't know exactly what registers it was produced from. */
11626 static int
11627 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11629 rtx x = *loc;
11630 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11631 int len = GET_RTX_LENGTH (GET_CODE (x));
11632 int i;
11634 if (REG_P (x))
11636 unsigned int regno = REGNO (x);
11637 unsigned int endregno = END_REGNO (x);
11638 unsigned int j;
11640 for (j = regno; j < endregno; j++)
11642 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
11643 if (rsp->last_set_invalid
11644 /* If this is a pseudo-register that was only set once and not
11645 live at the beginning of the function, it is always valid. */
11646 || (! (regno >= FIRST_PSEUDO_REGISTER
11647 && REG_N_SETS (regno) == 1
11648 && (!REGNO_REG_SET_P
11649 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
11650 && rsp->last_set_label > tick))
11652 if (replace)
11653 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11654 return replace;
11658 return 1;
11660 /* If this is a memory reference, make sure that there were
11661 no stores after it that might have clobbered the value. We don't
11662 have alias info, so we assume any store invalidates it. */
11663 else if (MEM_P (x) && !MEM_READONLY_P (x)
11664 && DF_INSN_LUID (insn) <= mem_last_set)
11666 if (replace)
11667 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11668 return replace;
11671 for (i = 0; i < len; i++)
11673 if (fmt[i] == 'e')
11675 /* Check for identical subexpressions. If x contains
11676 identical subexpression we only have to traverse one of
11677 them. */
11678 if (i == 1 && ARITHMETIC_P (x))
11680 /* Note that at this point x0 has already been checked
11681 and found valid. */
11682 rtx x0 = XEXP (x, 0);
11683 rtx x1 = XEXP (x, 1);
11685 /* If x0 and x1 are identical then x is also valid. */
11686 if (x0 == x1)
11687 return 1;
11689 /* If x1 is identical to a subexpression of x0 then
11690 while checking x0, x1 has already been checked. Thus
11691 it is valid and so as x. */
11692 if (ARITHMETIC_P (x0)
11693 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11694 return 1;
11696 /* If x0 is identical to a subexpression of x1 then x is
11697 valid iff the rest of x1 is valid. */
11698 if (ARITHMETIC_P (x1)
11699 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11700 return
11701 get_last_value_validate (&XEXP (x1,
11702 x0 == XEXP (x1, 0) ? 1 : 0),
11703 insn, tick, replace);
11706 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11707 replace) == 0)
11708 return 0;
11710 /* Don't bother with these. They shouldn't occur anyway. */
11711 else if (fmt[i] == 'E')
11712 return 0;
11715 /* If we haven't found a reason for it to be invalid, it is valid. */
11716 return 1;
11719 /* Get the last value assigned to X, if known. Some registers
11720 in the value may be replaced with (clobber (const_int 0)) if their value
11721 is known longer known reliably. */
11723 static rtx
11724 get_last_value (const_rtx x)
11726 unsigned int regno;
11727 rtx value;
11728 reg_stat_type *rsp;
11730 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11731 then convert it to the desired mode. If this is a paradoxical SUBREG,
11732 we cannot predict what values the "extra" bits might have. */
11733 if (GET_CODE (x) == SUBREG
11734 && subreg_lowpart_p (x)
11735 && (GET_MODE_SIZE (GET_MODE (x))
11736 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11737 && (value = get_last_value (SUBREG_REG (x))) != 0)
11738 return gen_lowpart (GET_MODE (x), value);
11740 if (!REG_P (x))
11741 return 0;
11743 regno = REGNO (x);
11744 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11745 value = rsp->last_set_value;
11747 /* If we don't have a value, or if it isn't for this basic block and
11748 it's either a hard register, set more than once, or it's a live
11749 at the beginning of the function, return 0.
11751 Because if it's not live at the beginning of the function then the reg
11752 is always set before being used (is never used without being set).
11753 And, if it's set only once, and it's always set before use, then all
11754 uses must have the same last value, even if it's not from this basic
11755 block. */
11757 if (value == 0
11758 || (rsp->last_set_label < label_tick_ebb_start
11759 && (regno < FIRST_PSEUDO_REGISTER
11760 || REG_N_SETS (regno) != 1
11761 || REGNO_REG_SET_P
11762 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
11763 return 0;
11765 /* If the value was set in a later insn than the ones we are processing,
11766 we can't use it even if the register was only set once. */
11767 if (rsp->last_set_label == label_tick
11768 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
11769 return 0;
11771 /* If the value has all its registers valid, return it. */
11772 if (get_last_value_validate (&value, rsp->last_set,
11773 rsp->last_set_label, 0))
11774 return value;
11776 /* Otherwise, make a copy and replace any invalid register with
11777 (clobber (const_int 0)). If that fails for some reason, return 0. */
11779 value = copy_rtx (value);
11780 if (get_last_value_validate (&value, rsp->last_set,
11781 rsp->last_set_label, 1))
11782 return value;
11784 return 0;
11787 /* Return nonzero if expression X refers to a REG or to memory
11788 that is set in an instruction more recent than FROM_LUID. */
11790 static int
11791 use_crosses_set_p (const_rtx x, int from_luid)
11793 const char *fmt;
11794 int i;
11795 enum rtx_code code = GET_CODE (x);
11797 if (code == REG)
11799 unsigned int regno = REGNO (x);
11800 unsigned endreg = END_REGNO (x);
11802 #ifdef PUSH_ROUNDING
11803 /* Don't allow uses of the stack pointer to be moved,
11804 because we don't know whether the move crosses a push insn. */
11805 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11806 return 1;
11807 #endif
11808 for (; regno < endreg; regno++)
11810 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
11811 if (rsp->last_set
11812 && rsp->last_set_label == label_tick
11813 && DF_INSN_LUID (rsp->last_set) > from_luid)
11814 return 1;
11816 return 0;
11819 if (code == MEM && mem_last_set > from_luid)
11820 return 1;
11822 fmt = GET_RTX_FORMAT (code);
11824 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11826 if (fmt[i] == 'E')
11828 int j;
11829 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11830 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
11831 return 1;
11833 else if (fmt[i] == 'e'
11834 && use_crosses_set_p (XEXP (x, i), from_luid))
11835 return 1;
11837 return 0;
11840 /* Define three variables used for communication between the following
11841 routines. */
11843 static unsigned int reg_dead_regno, reg_dead_endregno;
11844 static int reg_dead_flag;
11846 /* Function called via note_stores from reg_dead_at_p.
11848 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11849 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11851 static void
11852 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
11854 unsigned int regno, endregno;
11856 if (!REG_P (dest))
11857 return;
11859 regno = REGNO (dest);
11860 endregno = END_REGNO (dest);
11861 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11862 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11865 /* Return nonzero if REG is known to be dead at INSN.
11867 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11868 referencing REG, it is dead. If we hit a SET referencing REG, it is
11869 live. Otherwise, see if it is live or dead at the start of the basic
11870 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11871 must be assumed to be always live. */
11873 static int
11874 reg_dead_at_p (rtx reg, rtx insn)
11876 basic_block block;
11877 unsigned int i;
11879 /* Set variables for reg_dead_at_p_1. */
11880 reg_dead_regno = REGNO (reg);
11881 reg_dead_endregno = END_REGNO (reg);
11883 reg_dead_flag = 0;
11885 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11886 we allow the machine description to decide whether use-and-clobber
11887 patterns are OK. */
11888 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11890 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11891 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11892 return 0;
11895 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11896 beginning of function. */
11897 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11898 insn = prev_nonnote_insn (insn))
11900 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11901 if (reg_dead_flag)
11902 return reg_dead_flag == 1 ? 1 : 0;
11904 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11905 return 1;
11908 /* Get the basic block that we were in. */
11909 if (insn == 0)
11910 block = ENTRY_BLOCK_PTR->next_bb;
11911 else
11913 FOR_EACH_BB (block)
11914 if (insn == BB_HEAD (block))
11915 break;
11917 if (block == EXIT_BLOCK_PTR)
11918 return 0;
11921 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11922 if (REGNO_REG_SET_P (df_get_live_in (block), i))
11923 return 0;
11925 return 1;
11928 /* Note hard registers in X that are used. */
11930 static void
11931 mark_used_regs_combine (rtx x)
11933 RTX_CODE code = GET_CODE (x);
11934 unsigned int regno;
11935 int i;
11937 switch (code)
11939 case LABEL_REF:
11940 case SYMBOL_REF:
11941 case CONST_INT:
11942 case CONST:
11943 case CONST_DOUBLE:
11944 case CONST_VECTOR:
11945 case PC:
11946 case ADDR_VEC:
11947 case ADDR_DIFF_VEC:
11948 case ASM_INPUT:
11949 #ifdef HAVE_cc0
11950 /* CC0 must die in the insn after it is set, so we don't need to take
11951 special note of it here. */
11952 case CC0:
11953 #endif
11954 return;
11956 case CLOBBER:
11957 /* If we are clobbering a MEM, mark any hard registers inside the
11958 address as used. */
11959 if (MEM_P (XEXP (x, 0)))
11960 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11961 return;
11963 case REG:
11964 regno = REGNO (x);
11965 /* A hard reg in a wide mode may really be multiple registers.
11966 If so, mark all of them just like the first. */
11967 if (regno < FIRST_PSEUDO_REGISTER)
11969 /* None of this applies to the stack, frame or arg pointers. */
11970 if (regno == STACK_POINTER_REGNUM
11971 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11972 || regno == HARD_FRAME_POINTER_REGNUM
11973 #endif
11974 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11975 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11976 #endif
11977 || regno == FRAME_POINTER_REGNUM)
11978 return;
11980 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
11982 return;
11984 case SET:
11986 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11987 the address. */
11988 rtx testreg = SET_DEST (x);
11990 while (GET_CODE (testreg) == SUBREG
11991 || GET_CODE (testreg) == ZERO_EXTRACT
11992 || GET_CODE (testreg) == STRICT_LOW_PART)
11993 testreg = XEXP (testreg, 0);
11995 if (MEM_P (testreg))
11996 mark_used_regs_combine (XEXP (testreg, 0));
11998 mark_used_regs_combine (SET_SRC (x));
12000 return;
12002 default:
12003 break;
12006 /* Recursively scan the operands of this expression. */
12009 const char *fmt = GET_RTX_FORMAT (code);
12011 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12013 if (fmt[i] == 'e')
12014 mark_used_regs_combine (XEXP (x, i));
12015 else if (fmt[i] == 'E')
12017 int j;
12019 for (j = 0; j < XVECLEN (x, i); j++)
12020 mark_used_regs_combine (XVECEXP (x, i, j));
12026 /* Remove register number REGNO from the dead registers list of INSN.
12028 Return the note used to record the death, if there was one. */
12031 remove_death (unsigned int regno, rtx insn)
12033 rtx note = find_regno_note (insn, REG_DEAD, regno);
12035 if (note)
12036 remove_note (insn, note);
12038 return note;
12041 /* For each register (hardware or pseudo) used within expression X, if its
12042 death is in an instruction with luid between FROM_LUID (inclusive) and
12043 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12044 list headed by PNOTES.
12046 That said, don't move registers killed by maybe_kill_insn.
12048 This is done when X is being merged by combination into TO_INSN. These
12049 notes will then be distributed as needed. */
12051 static void
12052 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12053 rtx *pnotes)
12055 const char *fmt;
12056 int len, i;
12057 enum rtx_code code = GET_CODE (x);
12059 if (code == REG)
12061 unsigned int regno = REGNO (x);
12062 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12064 /* Don't move the register if it gets killed in between from and to. */
12065 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12066 && ! reg_referenced_p (x, maybe_kill_insn))
12067 return;
12069 if (where_dead
12070 && DF_INSN_LUID (where_dead) >= from_luid
12071 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12073 rtx note = remove_death (regno, where_dead);
12075 /* It is possible for the call above to return 0. This can occur
12076 when last_death points to I2 or I1 that we combined with.
12077 In that case make a new note.
12079 We must also check for the case where X is a hard register
12080 and NOTE is a death note for a range of hard registers
12081 including X. In that case, we must put REG_DEAD notes for
12082 the remaining registers in place of NOTE. */
12084 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12085 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12086 > GET_MODE_SIZE (GET_MODE (x))))
12088 unsigned int deadregno = REGNO (XEXP (note, 0));
12089 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12090 unsigned int ourend = END_HARD_REGNO (x);
12091 unsigned int i;
12093 for (i = deadregno; i < deadend; i++)
12094 if (i < regno || i >= ourend)
12095 REG_NOTES (where_dead)
12096 = gen_rtx_EXPR_LIST (REG_DEAD,
12097 regno_reg_rtx[i],
12098 REG_NOTES (where_dead));
12101 /* If we didn't find any note, or if we found a REG_DEAD note that
12102 covers only part of the given reg, and we have a multi-reg hard
12103 register, then to be safe we must check for REG_DEAD notes
12104 for each register other than the first. They could have
12105 their own REG_DEAD notes lying around. */
12106 else if ((note == 0
12107 || (note != 0
12108 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12109 < GET_MODE_SIZE (GET_MODE (x)))))
12110 && regno < FIRST_PSEUDO_REGISTER
12111 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12113 unsigned int ourend = END_HARD_REGNO (x);
12114 unsigned int i, offset;
12115 rtx oldnotes = 0;
12117 if (note)
12118 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12119 else
12120 offset = 1;
12122 for (i = regno + offset; i < ourend; i++)
12123 move_deaths (regno_reg_rtx[i],
12124 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12127 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12129 XEXP (note, 1) = *pnotes;
12130 *pnotes = note;
12132 else
12133 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12136 return;
12139 else if (GET_CODE (x) == SET)
12141 rtx dest = SET_DEST (x);
12143 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12145 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12146 that accesses one word of a multi-word item, some
12147 piece of everything register in the expression is used by
12148 this insn, so remove any old death. */
12149 /* ??? So why do we test for equality of the sizes? */
12151 if (GET_CODE (dest) == ZERO_EXTRACT
12152 || GET_CODE (dest) == STRICT_LOW_PART
12153 || (GET_CODE (dest) == SUBREG
12154 && (((GET_MODE_SIZE (GET_MODE (dest))
12155 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12156 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12157 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12159 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12160 return;
12163 /* If this is some other SUBREG, we know it replaces the entire
12164 value, so use that as the destination. */
12165 if (GET_CODE (dest) == SUBREG)
12166 dest = SUBREG_REG (dest);
12168 /* If this is a MEM, adjust deaths of anything used in the address.
12169 For a REG (the only other possibility), the entire value is
12170 being replaced so the old value is not used in this insn. */
12172 if (MEM_P (dest))
12173 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12174 to_insn, pnotes);
12175 return;
12178 else if (GET_CODE (x) == CLOBBER)
12179 return;
12181 len = GET_RTX_LENGTH (code);
12182 fmt = GET_RTX_FORMAT (code);
12184 for (i = 0; i < len; i++)
12186 if (fmt[i] == 'E')
12188 int j;
12189 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12190 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12191 to_insn, pnotes);
12193 else if (fmt[i] == 'e')
12194 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12198 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12199 pattern of an insn. X must be a REG. */
12201 static int
12202 reg_bitfield_target_p (rtx x, rtx body)
12204 int i;
12206 if (GET_CODE (body) == SET)
12208 rtx dest = SET_DEST (body);
12209 rtx target;
12210 unsigned int regno, tregno, endregno, endtregno;
12212 if (GET_CODE (dest) == ZERO_EXTRACT)
12213 target = XEXP (dest, 0);
12214 else if (GET_CODE (dest) == STRICT_LOW_PART)
12215 target = SUBREG_REG (XEXP (dest, 0));
12216 else
12217 return 0;
12219 if (GET_CODE (target) == SUBREG)
12220 target = SUBREG_REG (target);
12222 if (!REG_P (target))
12223 return 0;
12225 tregno = REGNO (target), regno = REGNO (x);
12226 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12227 return target == x;
12229 endtregno = end_hard_regno (GET_MODE (target), tregno);
12230 endregno = end_hard_regno (GET_MODE (x), regno);
12232 return endregno > tregno && regno < endtregno;
12235 else if (GET_CODE (body) == PARALLEL)
12236 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12237 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12238 return 1;
12240 return 0;
12243 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12244 as appropriate. I3 and I2 are the insns resulting from the combination
12245 insns including FROM (I2 may be zero).
12247 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12248 not need REG_DEAD notes because they are being substituted for. This
12249 saves searching in the most common cases.
12251 Each note in the list is either ignored or placed on some insns, depending
12252 on the type of note. */
12254 static void
12255 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12256 rtx elim_i1)
12258 rtx note, next_note;
12259 rtx tem;
12261 for (note = notes; note; note = next_note)
12263 rtx place = 0, place2 = 0;
12265 next_note = XEXP (note, 1);
12266 switch (REG_NOTE_KIND (note))
12268 case REG_BR_PROB:
12269 case REG_BR_PRED:
12270 /* Doesn't matter much where we put this, as long as it's somewhere.
12271 It is preferable to keep these notes on branches, which is most
12272 likely to be i3. */
12273 place = i3;
12274 break;
12276 case REG_VALUE_PROFILE:
12277 /* Just get rid of this note, as it is unused later anyway. */
12278 break;
12280 case REG_NON_LOCAL_GOTO:
12281 if (JUMP_P (i3))
12282 place = i3;
12283 else
12285 gcc_assert (i2 && JUMP_P (i2));
12286 place = i2;
12288 break;
12290 case REG_EH_REGION:
12291 /* These notes must remain with the call or trapping instruction. */
12292 if (CALL_P (i3))
12293 place = i3;
12294 else if (i2 && CALL_P (i2))
12295 place = i2;
12296 else
12298 gcc_assert (flag_non_call_exceptions);
12299 if (may_trap_p (i3))
12300 place = i3;
12301 else if (i2 && may_trap_p (i2))
12302 place = i2;
12303 /* ??? Otherwise assume we've combined things such that we
12304 can now prove that the instructions can't trap. Drop the
12305 note in this case. */
12307 break;
12309 case REG_NORETURN:
12310 case REG_SETJMP:
12311 /* These notes must remain with the call. It should not be
12312 possible for both I2 and I3 to be a call. */
12313 if (CALL_P (i3))
12314 place = i3;
12315 else
12317 gcc_assert (i2 && CALL_P (i2));
12318 place = i2;
12320 break;
12322 case REG_UNUSED:
12323 /* Any clobbers for i3 may still exist, and so we must process
12324 REG_UNUSED notes from that insn.
12326 Any clobbers from i2 or i1 can only exist if they were added by
12327 recog_for_combine. In that case, recog_for_combine created the
12328 necessary REG_UNUSED notes. Trying to keep any original
12329 REG_UNUSED notes from these insns can cause incorrect output
12330 if it is for the same register as the original i3 dest.
12331 In that case, we will notice that the register is set in i3,
12332 and then add a REG_UNUSED note for the destination of i3, which
12333 is wrong. However, it is possible to have REG_UNUSED notes from
12334 i2 or i1 for register which were both used and clobbered, so
12335 we keep notes from i2 or i1 if they will turn into REG_DEAD
12336 notes. */
12338 /* If this register is set or clobbered in I3, put the note there
12339 unless there is one already. */
12340 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12342 if (from_insn != i3)
12343 break;
12345 if (! (REG_P (XEXP (note, 0))
12346 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12347 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12348 place = i3;
12350 /* Otherwise, if this register is used by I3, then this register
12351 now dies here, so we must put a REG_DEAD note here unless there
12352 is one already. */
12353 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12354 && ! (REG_P (XEXP (note, 0))
12355 ? find_regno_note (i3, REG_DEAD,
12356 REGNO (XEXP (note, 0)))
12357 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12359 PUT_REG_NOTE_KIND (note, REG_DEAD);
12360 place = i3;
12362 break;
12364 case REG_EQUAL:
12365 case REG_EQUIV:
12366 case REG_NOALIAS:
12367 /* These notes say something about results of an insn. We can
12368 only support them if they used to be on I3 in which case they
12369 remain on I3. Otherwise they are ignored.
12371 If the note refers to an expression that is not a constant, we
12372 must also ignore the note since we cannot tell whether the
12373 equivalence is still true. It might be possible to do
12374 slightly better than this (we only have a problem if I2DEST
12375 or I1DEST is present in the expression), but it doesn't
12376 seem worth the trouble. */
12378 if (from_insn == i3
12379 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12380 place = i3;
12381 break;
12383 case REG_INC:
12384 case REG_NO_CONFLICT:
12385 /* These notes say something about how a register is used. They must
12386 be present on any use of the register in I2 or I3. */
12387 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12388 place = i3;
12390 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12392 if (place)
12393 place2 = i2;
12394 else
12395 place = i2;
12397 break;
12399 case REG_LABEL:
12400 /* This can show up in several ways -- either directly in the
12401 pattern, or hidden off in the constant pool with (or without?)
12402 a REG_EQUAL note. */
12403 /* ??? Ignore the without-reg_equal-note problem for now. */
12404 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12405 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12406 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12407 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12408 place = i3;
12410 if (i2
12411 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12412 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12413 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12414 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12416 if (place)
12417 place2 = i2;
12418 else
12419 place = i2;
12422 /* Don't attach REG_LABEL note to a JUMP_INSN. Add
12423 a JUMP_LABEL instead or decrement LABEL_NUSES. */
12424 if (place && JUMP_P (place))
12426 rtx label = JUMP_LABEL (place);
12428 if (!label)
12429 JUMP_LABEL (place) = XEXP (note, 0);
12430 else
12432 gcc_assert (label == XEXP (note, 0));
12433 if (LABEL_P (label))
12434 LABEL_NUSES (label)--;
12436 place = 0;
12438 if (place2 && JUMP_P (place2))
12440 rtx label = JUMP_LABEL (place2);
12442 if (!label)
12443 JUMP_LABEL (place2) = XEXP (note, 0);
12444 else
12446 gcc_assert (label == XEXP (note, 0));
12447 if (LABEL_P (label))
12448 LABEL_NUSES (label)--;
12450 place2 = 0;
12452 break;
12454 case REG_NONNEG:
12455 /* This note says something about the value of a register prior
12456 to the execution of an insn. It is too much trouble to see
12457 if the note is still correct in all situations. It is better
12458 to simply delete it. */
12459 break;
12461 case REG_LIBCALL_ID:
12462 /* If the insn previously containing this note still exists,
12463 put it back where it was. Otherwise move it to the previous
12464 insn. */
12465 if (!NOTE_P (from_insn))
12466 place = from_insn;
12467 else
12468 place = prev_real_insn (from_insn);
12469 break;
12470 case REG_RETVAL:
12471 /* If the insn previously containing this note still exists,
12472 put it back where it was. Otherwise move it to the previous
12473 insn. Adjust the corresponding REG_LIBCALL note. */
12474 if (!NOTE_P (from_insn))
12475 place = from_insn;
12476 else
12478 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12479 place = prev_real_insn (from_insn);
12480 if (tem && place)
12481 XEXP (tem, 0) = place;
12482 /* If we're deleting the last remaining instruction of a
12483 libcall sequence, don't add the notes. */
12484 else if (XEXP (note, 0) == from_insn)
12485 tem = place = 0;
12486 /* Don't add the dangling REG_RETVAL note. */
12487 else if (! tem)
12488 place = 0;
12490 break;
12492 case REG_LIBCALL:
12493 /* This is handled similarly to REG_RETVAL. */
12494 if (!NOTE_P (from_insn))
12495 place = from_insn;
12496 else
12498 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12499 place = next_real_insn (from_insn);
12500 if (tem && place)
12501 XEXP (tem, 0) = place;
12502 /* If we're deleting the last remaining instruction of a
12503 libcall sequence, don't add the notes. */
12504 else if (XEXP (note, 0) == from_insn)
12505 tem = place = 0;
12506 /* Don't add the dangling REG_LIBCALL note. */
12507 else if (! tem)
12508 place = 0;
12510 break;
12512 case REG_DEAD:
12513 /* If we replaced the right hand side of FROM_INSN with a
12514 REG_EQUAL note, the original use of the dying register
12515 will not have been combined into I3 and I2. In such cases,
12516 FROM_INSN is guaranteed to be the first of the combined
12517 instructions, so we simply need to search back before
12518 FROM_INSN for the previous use or set of this register,
12519 then alter the notes there appropriately.
12521 If the register is used as an input in I3, it dies there.
12522 Similarly for I2, if it is nonzero and adjacent to I3.
12524 If the register is not used as an input in either I3 or I2
12525 and it is not one of the registers we were supposed to eliminate,
12526 there are two possibilities. We might have a non-adjacent I2
12527 or we might have somehow eliminated an additional register
12528 from a computation. For example, we might have had A & B where
12529 we discover that B will always be zero. In this case we will
12530 eliminate the reference to A.
12532 In both cases, we must search to see if we can find a previous
12533 use of A and put the death note there. */
12535 if (from_insn
12536 && from_insn == i2mod
12537 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12538 tem = from_insn;
12539 else
12541 if (from_insn
12542 && CALL_P (from_insn)
12543 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12544 place = from_insn;
12545 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12546 place = i3;
12547 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12548 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12549 place = i2;
12550 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12551 && !(i2mod
12552 && reg_overlap_mentioned_p (XEXP (note, 0),
12553 i2mod_old_rhs)))
12554 || rtx_equal_p (XEXP (note, 0), elim_i1))
12555 break;
12556 tem = i3;
12559 if (place == 0)
12561 basic_block bb = this_basic_block;
12563 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12565 if (! INSN_P (tem))
12567 if (tem == BB_HEAD (bb))
12568 break;
12569 continue;
12572 /* If the register is being set at TEM, see if that is all
12573 TEM is doing. If so, delete TEM. Otherwise, make this
12574 into a REG_UNUSED note instead. Don't delete sets to
12575 global register vars. */
12576 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12577 || !global_regs[REGNO (XEXP (note, 0))])
12578 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12580 rtx set = single_set (tem);
12581 rtx inner_dest = 0;
12582 #ifdef HAVE_cc0
12583 rtx cc0_setter = NULL_RTX;
12584 #endif
12586 if (set != 0)
12587 for (inner_dest = SET_DEST (set);
12588 (GET_CODE (inner_dest) == STRICT_LOW_PART
12589 || GET_CODE (inner_dest) == SUBREG
12590 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12591 inner_dest = XEXP (inner_dest, 0))
12594 /* Verify that it was the set, and not a clobber that
12595 modified the register.
12597 CC0 targets must be careful to maintain setter/user
12598 pairs. If we cannot delete the setter due to side
12599 effects, mark the user with an UNUSED note instead
12600 of deleting it. */
12602 if (set != 0 && ! side_effects_p (SET_SRC (set))
12603 && rtx_equal_p (XEXP (note, 0), inner_dest)
12604 #ifdef HAVE_cc0
12605 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12606 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12607 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12608 #endif
12611 /* Move the notes and links of TEM elsewhere.
12612 This might delete other dead insns recursively.
12613 First set the pattern to something that won't use
12614 any register. */
12615 rtx old_notes = REG_NOTES (tem);
12617 PATTERN (tem) = pc_rtx;
12618 REG_NOTES (tem) = NULL;
12620 distribute_notes (old_notes, tem, tem, NULL_RTX,
12621 NULL_RTX, NULL_RTX);
12622 distribute_links (LOG_LINKS (tem));
12624 SET_INSN_DELETED (tem);
12626 #ifdef HAVE_cc0
12627 /* Delete the setter too. */
12628 if (cc0_setter)
12630 PATTERN (cc0_setter) = pc_rtx;
12631 old_notes = REG_NOTES (cc0_setter);
12632 REG_NOTES (cc0_setter) = NULL;
12634 distribute_notes (old_notes, cc0_setter,
12635 cc0_setter, NULL_RTX,
12636 NULL_RTX, NULL_RTX);
12637 distribute_links (LOG_LINKS (cc0_setter));
12639 SET_INSN_DELETED (cc0_setter);
12641 #endif
12643 else
12645 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12647 /* If there isn't already a REG_UNUSED note, put one
12648 here. Do not place a REG_DEAD note, even if
12649 the register is also used here; that would not
12650 match the algorithm used in lifetime analysis
12651 and can cause the consistency check in the
12652 scheduler to fail. */
12653 if (! find_regno_note (tem, REG_UNUSED,
12654 REGNO (XEXP (note, 0))))
12655 place = tem;
12656 break;
12659 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12660 || (CALL_P (tem)
12661 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12663 place = tem;
12665 /* If we are doing a 3->2 combination, and we have a
12666 register which formerly died in i3 and was not used
12667 by i2, which now no longer dies in i3 and is used in
12668 i2 but does not die in i2, and place is between i2
12669 and i3, then we may need to move a link from place to
12670 i2. */
12671 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
12672 && from_insn
12673 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
12674 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12676 rtx links = LOG_LINKS (place);
12677 LOG_LINKS (place) = 0;
12678 distribute_links (links);
12680 break;
12683 if (tem == BB_HEAD (bb))
12684 break;
12689 /* If the register is set or already dead at PLACE, we needn't do
12690 anything with this note if it is still a REG_DEAD note.
12691 We check here if it is set at all, not if is it totally replaced,
12692 which is what `dead_or_set_p' checks, so also check for it being
12693 set partially. */
12695 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12697 unsigned int regno = REGNO (XEXP (note, 0));
12698 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12700 if (dead_or_set_p (place, XEXP (note, 0))
12701 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12703 /* Unless the register previously died in PLACE, clear
12704 last_death. [I no longer understand why this is
12705 being done.] */
12706 if (rsp->last_death != place)
12707 rsp->last_death = 0;
12708 place = 0;
12710 else
12711 rsp->last_death = place;
12713 /* If this is a death note for a hard reg that is occupying
12714 multiple registers, ensure that we are still using all
12715 parts of the object. If we find a piece of the object
12716 that is unused, we must arrange for an appropriate REG_DEAD
12717 note to be added for it. However, we can't just emit a USE
12718 and tag the note to it, since the register might actually
12719 be dead; so we recourse, and the recursive call then finds
12720 the previous insn that used this register. */
12722 if (place && regno < FIRST_PSEUDO_REGISTER
12723 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12725 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
12726 int all_used = 1;
12727 unsigned int i;
12729 for (i = regno; i < endregno; i++)
12730 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12731 && ! find_regno_fusage (place, USE, i))
12732 || dead_or_set_regno_p (place, i))
12733 all_used = 0;
12735 if (! all_used)
12737 /* Put only REG_DEAD notes for pieces that are
12738 not already dead or set. */
12740 for (i = regno; i < endregno;
12741 i += hard_regno_nregs[i][reg_raw_mode[i]])
12743 rtx piece = regno_reg_rtx[i];
12744 basic_block bb = this_basic_block;
12746 if (! dead_or_set_p (place, piece)
12747 && ! reg_bitfield_target_p (piece,
12748 PATTERN (place)))
12750 rtx new_note
12751 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12753 distribute_notes (new_note, place, place,
12754 NULL_RTX, NULL_RTX, NULL_RTX);
12756 else if (! refers_to_regno_p (i, i + 1,
12757 PATTERN (place), 0)
12758 && ! find_regno_fusage (place, USE, i))
12759 for (tem = PREV_INSN (place); ;
12760 tem = PREV_INSN (tem))
12762 if (! INSN_P (tem))
12764 if (tem == BB_HEAD (bb))
12765 break;
12766 continue;
12768 if (dead_or_set_p (tem, piece)
12769 || reg_bitfield_target_p (piece,
12770 PATTERN (tem)))
12772 REG_NOTES (tem)
12773 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12774 REG_NOTES (tem));
12775 break;
12781 place = 0;
12785 break;
12787 default:
12788 /* Any other notes should not be present at this point in the
12789 compilation. */
12790 gcc_unreachable ();
12793 if (place)
12795 XEXP (note, 1) = REG_NOTES (place);
12796 REG_NOTES (place) = note;
12799 if (place2)
12800 REG_NOTES (place2)
12801 = gen_rtx_fmt_ee (GET_CODE (note), REG_NOTE_KIND (note),
12802 XEXP (note, 0), REG_NOTES (place2));
12806 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12807 I3, I2, and I1 to new locations. This is also called to add a link
12808 pointing at I3 when I3's destination is changed. */
12810 static void
12811 distribute_links (rtx links)
12813 rtx link, next_link;
12815 for (link = links; link; link = next_link)
12817 rtx place = 0;
12818 rtx insn;
12819 rtx set, reg;
12821 next_link = XEXP (link, 1);
12823 /* If the insn that this link points to is a NOTE or isn't a single
12824 set, ignore it. In the latter case, it isn't clear what we
12825 can do other than ignore the link, since we can't tell which
12826 register it was for. Such links wouldn't be used by combine
12827 anyway.
12829 It is not possible for the destination of the target of the link to
12830 have been changed by combine. The only potential of this is if we
12831 replace I3, I2, and I1 by I3 and I2. But in that case the
12832 destination of I2 also remains unchanged. */
12834 if (NOTE_P (XEXP (link, 0))
12835 || (set = single_set (XEXP (link, 0))) == 0)
12836 continue;
12838 reg = SET_DEST (set);
12839 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12840 || GET_CODE (reg) == STRICT_LOW_PART)
12841 reg = XEXP (reg, 0);
12843 /* A LOG_LINK is defined as being placed on the first insn that uses
12844 a register and points to the insn that sets the register. Start
12845 searching at the next insn after the target of the link and stop
12846 when we reach a set of the register or the end of the basic block.
12848 Note that this correctly handles the link that used to point from
12849 I3 to I2. Also note that not much searching is typically done here
12850 since most links don't point very far away. */
12852 for (insn = NEXT_INSN (XEXP (link, 0));
12853 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12854 || BB_HEAD (this_basic_block->next_bb) != insn));
12855 insn = NEXT_INSN (insn))
12856 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12858 if (reg_referenced_p (reg, PATTERN (insn)))
12859 place = insn;
12860 break;
12862 else if (CALL_P (insn)
12863 && find_reg_fusage (insn, USE, reg))
12865 place = insn;
12866 break;
12868 else if (INSN_P (insn) && reg_set_p (reg, insn))
12869 break;
12871 /* If we found a place to put the link, place it there unless there
12872 is already a link to the same insn as LINK at that point. */
12874 if (place)
12876 rtx link2;
12878 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12879 if (XEXP (link2, 0) == XEXP (link, 0))
12880 break;
12882 if (link2 == 0)
12884 XEXP (link, 1) = LOG_LINKS (place);
12885 LOG_LINKS (place) = link;
12887 /* Set added_links_insn to the earliest insn we added a
12888 link to. */
12889 if (added_links_insn == 0
12890 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
12891 added_links_insn = place;
12897 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12898 Check whether the expression pointer to by LOC is a register or
12899 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12900 Otherwise return zero. */
12902 static int
12903 unmentioned_reg_p_1 (rtx *loc, void *expr)
12905 rtx x = *loc;
12907 if (x != NULL_RTX
12908 && (REG_P (x) || MEM_P (x))
12909 && ! reg_mentioned_p (x, (rtx) expr))
12910 return 1;
12911 return 0;
12914 /* Check for any register or memory mentioned in EQUIV that is not
12915 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12916 of EXPR where some registers may have been replaced by constants. */
12918 static bool
12919 unmentioned_reg_p (rtx equiv, rtx expr)
12921 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12924 void
12925 dump_combine_stats (FILE *file)
12927 fprintf
12928 (file,
12929 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12930 combine_attempts, combine_merges, combine_extras, combine_successes);
12933 void
12934 dump_combine_total_stats (FILE *file)
12936 fprintf
12937 (file,
12938 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12939 total_attempts, total_merges, total_extras, total_successes);
12942 static bool
12943 gate_handle_combine (void)
12945 return (optimize > 0);
12948 /* Try combining insns through substitution. */
12949 static unsigned int
12950 rest_of_handle_combine (void)
12952 int rebuild_jump_labels_after_combine;
12954 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
12955 df_note_add_problem ();
12956 df_analyze ();
12958 regstat_init_n_sets_and_refs ();
12960 rebuild_jump_labels_after_combine
12961 = combine_instructions (get_insns (), max_reg_num ());
12963 /* Combining insns may have turned an indirect jump into a
12964 direct jump. Rebuild the JUMP_LABEL fields of jumping
12965 instructions. */
12966 if (rebuild_jump_labels_after_combine)
12968 timevar_push (TV_JUMP);
12969 rebuild_jump_labels (get_insns ());
12970 cleanup_cfg (0);
12971 timevar_pop (TV_JUMP);
12974 regstat_free_n_sets_and_refs ();
12975 return 0;
12978 struct tree_opt_pass pass_combine =
12980 "combine", /* name */
12981 gate_handle_combine, /* gate */
12982 rest_of_handle_combine, /* execute */
12983 NULL, /* sub */
12984 NULL, /* next */
12985 0, /* static_pass_number */
12986 TV_COMBINE, /* tv_id */
12987 0, /* properties_required */
12988 0, /* properties_provided */
12989 0, /* properties_destroyed */
12990 0, /* todo_flags_start */
12991 TODO_dump_func |
12992 TODO_df_finish |
12993 TODO_ggc_collect, /* todo_flags_finish */
12994 'c' /* letter */