PR middle-end/27945
[official-gcc.git] / gcc / combine.c
blob319c5325870760afae1bcbd7b923d5cc665b7194
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 /* Check if we are loading something from the constant pool via float
4482 extension; in this case we would undo compress_float_constant
4483 optimization and degenerate constant load to an immediate value. */
4484 if (GET_CODE (x) == FLOAT_EXTEND
4485 && MEM_P (XEXP (x, 0))
4486 && MEM_READONLY_P (XEXP (x, 0)))
4488 rtx tmp = avoid_constant_pool_reference (x);
4489 if (x != tmp)
4490 return x;
4493 /* Try to simplify X. If the simplification changed the code, it is likely
4494 that further simplification will help, so loop, but limit the number
4495 of repetitions that will be performed. */
4497 for (i = 0; i < 4; i++)
4499 /* If X is sufficiently simple, don't bother trying to do anything
4500 with it. */
4501 if (code != CONST_INT && code != REG && code != CLOBBER)
4502 x = combine_simplify_rtx (x, op0_mode, in_dest);
4504 if (GET_CODE (x) == code)
4505 break;
4507 code = GET_CODE (x);
4509 /* We no longer know the original mode of operand 0 since we
4510 have changed the form of X) */
4511 op0_mode = VOIDmode;
4514 return x;
4517 /* Simplify X, a piece of RTL. We just operate on the expression at the
4518 outer level; call `subst' to simplify recursively. Return the new
4519 expression.
4521 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
4522 if we are inside a SET_DEST. */
4524 static rtx
4525 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4527 enum rtx_code code = GET_CODE (x);
4528 enum machine_mode mode = GET_MODE (x);
4529 rtx temp;
4530 int i;
4532 /* If this is a commutative operation, put a constant last and a complex
4533 expression first. We don't need to do this for comparisons here. */
4534 if (COMMUTATIVE_ARITH_P (x)
4535 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4537 temp = XEXP (x, 0);
4538 SUBST (XEXP (x, 0), XEXP (x, 1));
4539 SUBST (XEXP (x, 1), temp);
4542 /* If this is a simple operation applied to an IF_THEN_ELSE, try
4543 applying it to the arms of the IF_THEN_ELSE. This often simplifies
4544 things. Check for cases where both arms are testing the same
4545 condition.
4547 Don't do anything if all operands are very simple. */
4549 if ((BINARY_P (x)
4550 && ((!OBJECT_P (XEXP (x, 0))
4551 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4552 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4553 || (!OBJECT_P (XEXP (x, 1))
4554 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4555 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4556 || (UNARY_P (x)
4557 && (!OBJECT_P (XEXP (x, 0))
4558 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4559 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4561 rtx cond, true_rtx, false_rtx;
4563 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4564 if (cond != 0
4565 /* If everything is a comparison, what we have is highly unlikely
4566 to be simpler, so don't use it. */
4567 && ! (COMPARISON_P (x)
4568 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4570 rtx cop1 = const0_rtx;
4571 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4573 if (cond_code == NE && COMPARISON_P (cond))
4574 return x;
4576 /* Simplify the alternative arms; this may collapse the true and
4577 false arms to store-flag values. Be careful to use copy_rtx
4578 here since true_rtx or false_rtx might share RTL with x as a
4579 result of the if_then_else_cond call above. */
4580 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4581 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4583 /* If true_rtx and false_rtx are not general_operands, an if_then_else
4584 is unlikely to be simpler. */
4585 if (general_operand (true_rtx, VOIDmode)
4586 && general_operand (false_rtx, VOIDmode))
4588 enum rtx_code reversed;
4590 /* Restarting if we generate a store-flag expression will cause
4591 us to loop. Just drop through in this case. */
4593 /* If the result values are STORE_FLAG_VALUE and zero, we can
4594 just make the comparison operation. */
4595 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4596 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4597 cond, cop1);
4598 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4599 && ((reversed = reversed_comparison_code_parts
4600 (cond_code, cond, cop1, NULL))
4601 != UNKNOWN))
4602 x = simplify_gen_relational (reversed, mode, VOIDmode,
4603 cond, cop1);
4605 /* Likewise, we can make the negate of a comparison operation
4606 if the result values are - STORE_FLAG_VALUE and zero. */
4607 else if (GET_CODE (true_rtx) == CONST_INT
4608 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4609 && false_rtx == const0_rtx)
4610 x = simplify_gen_unary (NEG, mode,
4611 simplify_gen_relational (cond_code,
4612 mode, VOIDmode,
4613 cond, cop1),
4614 mode);
4615 else if (GET_CODE (false_rtx) == CONST_INT
4616 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4617 && true_rtx == const0_rtx
4618 && ((reversed = reversed_comparison_code_parts
4619 (cond_code, cond, cop1, NULL))
4620 != UNKNOWN))
4621 x = simplify_gen_unary (NEG, mode,
4622 simplify_gen_relational (reversed,
4623 mode, VOIDmode,
4624 cond, cop1),
4625 mode);
4626 else
4627 return gen_rtx_IF_THEN_ELSE (mode,
4628 simplify_gen_relational (cond_code,
4629 mode,
4630 VOIDmode,
4631 cond,
4632 cop1),
4633 true_rtx, false_rtx);
4635 code = GET_CODE (x);
4636 op0_mode = VOIDmode;
4641 /* Try to fold this expression in case we have constants that weren't
4642 present before. */
4643 temp = 0;
4644 switch (GET_RTX_CLASS (code))
4646 case RTX_UNARY:
4647 if (op0_mode == VOIDmode)
4648 op0_mode = GET_MODE (XEXP (x, 0));
4649 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4650 break;
4651 case RTX_COMPARE:
4652 case RTX_COMM_COMPARE:
4654 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4655 if (cmp_mode == VOIDmode)
4657 cmp_mode = GET_MODE (XEXP (x, 1));
4658 if (cmp_mode == VOIDmode)
4659 cmp_mode = op0_mode;
4661 temp = simplify_relational_operation (code, mode, cmp_mode,
4662 XEXP (x, 0), XEXP (x, 1));
4664 break;
4665 case RTX_COMM_ARITH:
4666 case RTX_BIN_ARITH:
4667 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4668 break;
4669 case RTX_BITFIELD_OPS:
4670 case RTX_TERNARY:
4671 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4672 XEXP (x, 1), XEXP (x, 2));
4673 break;
4674 default:
4675 break;
4678 if (temp)
4680 x = temp;
4681 code = GET_CODE (temp);
4682 op0_mode = VOIDmode;
4683 mode = GET_MODE (temp);
4686 /* First see if we can apply the inverse distributive law. */
4687 if (code == PLUS || code == MINUS
4688 || code == AND || code == IOR || code == XOR)
4690 x = apply_distributive_law (x);
4691 code = GET_CODE (x);
4692 op0_mode = VOIDmode;
4695 /* If CODE is an associative operation not otherwise handled, see if we
4696 can associate some operands. This can win if they are constants or
4697 if they are logically related (i.e. (a & b) & a). */
4698 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4699 || code == AND || code == IOR || code == XOR
4700 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4701 && ((INTEGRAL_MODE_P (mode) && code != DIV)
4702 || (flag_associative_math && FLOAT_MODE_P (mode))))
4704 if (GET_CODE (XEXP (x, 0)) == code)
4706 rtx other = XEXP (XEXP (x, 0), 0);
4707 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4708 rtx inner_op1 = XEXP (x, 1);
4709 rtx inner;
4711 /* Make sure we pass the constant operand if any as the second
4712 one if this is a commutative operation. */
4713 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4715 rtx tem = inner_op0;
4716 inner_op0 = inner_op1;
4717 inner_op1 = tem;
4719 inner = simplify_binary_operation (code == MINUS ? PLUS
4720 : code == DIV ? MULT
4721 : code,
4722 mode, inner_op0, inner_op1);
4724 /* For commutative operations, try the other pair if that one
4725 didn't simplify. */
4726 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4728 other = XEXP (XEXP (x, 0), 1);
4729 inner = simplify_binary_operation (code, mode,
4730 XEXP (XEXP (x, 0), 0),
4731 XEXP (x, 1));
4734 if (inner)
4735 return simplify_gen_binary (code, mode, other, inner);
4739 /* A little bit of algebraic simplification here. */
4740 switch (code)
4742 case MEM:
4743 /* Ensure that our address has any ASHIFTs converted to MULT in case
4744 address-recognizing predicates are called later. */
4745 temp = make_compound_operation (XEXP (x, 0), MEM);
4746 SUBST (XEXP (x, 0), temp);
4747 break;
4749 case SUBREG:
4750 if (op0_mode == VOIDmode)
4751 op0_mode = GET_MODE (SUBREG_REG (x));
4753 /* See if this can be moved to simplify_subreg. */
4754 if (CONSTANT_P (SUBREG_REG (x))
4755 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4756 /* Don't call gen_lowpart if the inner mode
4757 is VOIDmode and we cannot simplify it, as SUBREG without
4758 inner mode is invalid. */
4759 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4760 || gen_lowpart_common (mode, SUBREG_REG (x))))
4761 return gen_lowpart (mode, SUBREG_REG (x));
4763 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4764 break;
4766 rtx temp;
4767 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4768 SUBREG_BYTE (x));
4769 if (temp)
4770 return temp;
4773 /* Don't change the mode of the MEM if that would change the meaning
4774 of the address. */
4775 if (MEM_P (SUBREG_REG (x))
4776 && (MEM_VOLATILE_P (SUBREG_REG (x))
4777 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4778 return gen_rtx_CLOBBER (mode, const0_rtx);
4780 /* Note that we cannot do any narrowing for non-constants since
4781 we might have been counting on using the fact that some bits were
4782 zero. We now do this in the SET. */
4784 break;
4786 case NEG:
4787 temp = expand_compound_operation (XEXP (x, 0));
4789 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4790 replaced by (lshiftrt X C). This will convert
4791 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4793 if (GET_CODE (temp) == ASHIFTRT
4794 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4795 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4796 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4797 INTVAL (XEXP (temp, 1)));
4799 /* If X has only a single bit that might be nonzero, say, bit I, convert
4800 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4801 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4802 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4803 or a SUBREG of one since we'd be making the expression more
4804 complex if it was just a register. */
4806 if (!REG_P (temp)
4807 && ! (GET_CODE (temp) == SUBREG
4808 && REG_P (SUBREG_REG (temp)))
4809 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4811 rtx temp1 = simplify_shift_const
4812 (NULL_RTX, ASHIFTRT, mode,
4813 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4814 GET_MODE_BITSIZE (mode) - 1 - i),
4815 GET_MODE_BITSIZE (mode) - 1 - i);
4817 /* If all we did was surround TEMP with the two shifts, we
4818 haven't improved anything, so don't use it. Otherwise,
4819 we are better off with TEMP1. */
4820 if (GET_CODE (temp1) != ASHIFTRT
4821 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4822 || XEXP (XEXP (temp1, 0), 0) != temp)
4823 return temp1;
4825 break;
4827 case TRUNCATE:
4828 /* We can't handle truncation to a partial integer mode here
4829 because we don't know the real bitsize of the partial
4830 integer mode. */
4831 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4832 break;
4834 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4835 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4836 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4837 SUBST (XEXP (x, 0),
4838 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4839 GET_MODE_MASK (mode), 0));
4841 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4842 whose value is a comparison can be replaced with a subreg if
4843 STORE_FLAG_VALUE permits. */
4844 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4845 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4846 && (temp = get_last_value (XEXP (x, 0)))
4847 && COMPARISON_P (temp))
4848 return gen_lowpart (mode, XEXP (x, 0));
4849 break;
4851 #ifdef HAVE_cc0
4852 case COMPARE:
4853 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4854 using cc0, in which case we want to leave it as a COMPARE
4855 so we can distinguish it from a register-register-copy. */
4856 if (XEXP (x, 1) == const0_rtx)
4857 return XEXP (x, 0);
4859 /* x - 0 is the same as x unless x's mode has signed zeros and
4860 allows rounding towards -infinity. Under those conditions,
4861 0 - 0 is -0. */
4862 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4863 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4864 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4865 return XEXP (x, 0);
4866 break;
4867 #endif
4869 case CONST:
4870 /* (const (const X)) can become (const X). Do it this way rather than
4871 returning the inner CONST since CONST can be shared with a
4872 REG_EQUAL note. */
4873 if (GET_CODE (XEXP (x, 0)) == CONST)
4874 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4875 break;
4877 #ifdef HAVE_lo_sum
4878 case LO_SUM:
4879 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4880 can add in an offset. find_split_point will split this address up
4881 again if it doesn't match. */
4882 if (GET_CODE (XEXP (x, 0)) == HIGH
4883 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4884 return XEXP (x, 1);
4885 break;
4886 #endif
4888 case PLUS:
4889 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4890 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4891 bit-field and can be replaced by either a sign_extend or a
4892 sign_extract. The `and' may be a zero_extend and the two
4893 <c>, -<c> constants may be reversed. */
4894 if (GET_CODE (XEXP (x, 0)) == XOR
4895 && GET_CODE (XEXP (x, 1)) == CONST_INT
4896 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4897 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4898 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4899 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4900 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4901 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4902 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4903 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4904 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4905 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4906 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4907 == (unsigned int) i + 1))))
4908 return simplify_shift_const
4909 (NULL_RTX, ASHIFTRT, mode,
4910 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4911 XEXP (XEXP (XEXP (x, 0), 0), 0),
4912 GET_MODE_BITSIZE (mode) - (i + 1)),
4913 GET_MODE_BITSIZE (mode) - (i + 1));
4915 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4916 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4917 the bitsize of the mode - 1. This allows simplification of
4918 "a = (b & 8) == 0;" */
4919 if (XEXP (x, 1) == constm1_rtx
4920 && !REG_P (XEXP (x, 0))
4921 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4922 && REG_P (SUBREG_REG (XEXP (x, 0))))
4923 && nonzero_bits (XEXP (x, 0), mode) == 1)
4924 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4925 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4926 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4927 GET_MODE_BITSIZE (mode) - 1),
4928 GET_MODE_BITSIZE (mode) - 1);
4930 /* If we are adding two things that have no bits in common, convert
4931 the addition into an IOR. This will often be further simplified,
4932 for example in cases like ((a & 1) + (a & 2)), which can
4933 become a & 3. */
4935 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4936 && (nonzero_bits (XEXP (x, 0), mode)
4937 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4939 /* Try to simplify the expression further. */
4940 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4941 temp = combine_simplify_rtx (tor, mode, in_dest);
4943 /* If we could, great. If not, do not go ahead with the IOR
4944 replacement, since PLUS appears in many special purpose
4945 address arithmetic instructions. */
4946 if (GET_CODE (temp) != CLOBBER && temp != tor)
4947 return temp;
4949 break;
4951 case MINUS:
4952 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4953 (and <foo> (const_int pow2-1)) */
4954 if (GET_CODE (XEXP (x, 1)) == AND
4955 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4956 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4957 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4958 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4959 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4960 break;
4962 case MULT:
4963 /* If we have (mult (plus A B) C), apply the distributive law and then
4964 the inverse distributive law to see if things simplify. This
4965 occurs mostly in addresses, often when unrolling loops. */
4967 if (GET_CODE (XEXP (x, 0)) == PLUS)
4969 rtx result = distribute_and_simplify_rtx (x, 0);
4970 if (result)
4971 return result;
4974 /* Try simplify a*(b/c) as (a*b)/c. */
4975 if (FLOAT_MODE_P (mode) && flag_associative_math
4976 && GET_CODE (XEXP (x, 0)) == DIV)
4978 rtx tem = simplify_binary_operation (MULT, mode,
4979 XEXP (XEXP (x, 0), 0),
4980 XEXP (x, 1));
4981 if (tem)
4982 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4984 break;
4986 case UDIV:
4987 /* If this is a divide by a power of two, treat it as a shift if
4988 its first operand is a shift. */
4989 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4990 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4991 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4992 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4993 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4994 || GET_CODE (XEXP (x, 0)) == ROTATE
4995 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4996 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4997 break;
4999 case EQ: case NE:
5000 case GT: case GTU: case GE: case GEU:
5001 case LT: case LTU: case LE: case LEU:
5002 case UNEQ: case LTGT:
5003 case UNGT: case UNGE:
5004 case UNLT: case UNLE:
5005 case UNORDERED: case ORDERED:
5006 /* If the first operand is a condition code, we can't do anything
5007 with it. */
5008 if (GET_CODE (XEXP (x, 0)) == COMPARE
5009 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5010 && ! CC0_P (XEXP (x, 0))))
5012 rtx op0 = XEXP (x, 0);
5013 rtx op1 = XEXP (x, 1);
5014 enum rtx_code new_code;
5016 if (GET_CODE (op0) == COMPARE)
5017 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5019 /* Simplify our comparison, if possible. */
5020 new_code = simplify_comparison (code, &op0, &op1);
5022 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5023 if only the low-order bit is possibly nonzero in X (such as when
5024 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5025 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5026 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5027 (plus X 1).
5029 Remove any ZERO_EXTRACT we made when thinking this was a
5030 comparison. It may now be simpler to use, e.g., an AND. If a
5031 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5032 the call to make_compound_operation in the SET case. */
5034 if (STORE_FLAG_VALUE == 1
5035 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5036 && op1 == const0_rtx
5037 && mode == GET_MODE (op0)
5038 && nonzero_bits (op0, mode) == 1)
5039 return gen_lowpart (mode,
5040 expand_compound_operation (op0));
5042 else if (STORE_FLAG_VALUE == 1
5043 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5044 && op1 == const0_rtx
5045 && mode == GET_MODE (op0)
5046 && (num_sign_bit_copies (op0, mode)
5047 == GET_MODE_BITSIZE (mode)))
5049 op0 = expand_compound_operation (op0);
5050 return simplify_gen_unary (NEG, mode,
5051 gen_lowpart (mode, op0),
5052 mode);
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 && nonzero_bits (op0, mode) == 1)
5061 op0 = expand_compound_operation (op0);
5062 return simplify_gen_binary (XOR, mode,
5063 gen_lowpart (mode, op0),
5064 const1_rtx);
5067 else if (STORE_FLAG_VALUE == 1
5068 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5069 && op1 == const0_rtx
5070 && mode == GET_MODE (op0)
5071 && (num_sign_bit_copies (op0, mode)
5072 == GET_MODE_BITSIZE (mode)))
5074 op0 = expand_compound_operation (op0);
5075 return plus_constant (gen_lowpart (mode, op0), 1);
5078 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5079 those above. */
5080 if (STORE_FLAG_VALUE == -1
5081 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5082 && op1 == const0_rtx
5083 && (num_sign_bit_copies (op0, mode)
5084 == GET_MODE_BITSIZE (mode)))
5085 return gen_lowpart (mode,
5086 expand_compound_operation (op0));
5088 else if (STORE_FLAG_VALUE == -1
5089 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5090 && op1 == const0_rtx
5091 && mode == GET_MODE (op0)
5092 && nonzero_bits (op0, mode) == 1)
5094 op0 = expand_compound_operation (op0);
5095 return simplify_gen_unary (NEG, mode,
5096 gen_lowpart (mode, op0),
5097 mode);
5100 else if (STORE_FLAG_VALUE == -1
5101 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5102 && op1 == const0_rtx
5103 && mode == GET_MODE (op0)
5104 && (num_sign_bit_copies (op0, mode)
5105 == GET_MODE_BITSIZE (mode)))
5107 op0 = expand_compound_operation (op0);
5108 return simplify_gen_unary (NOT, mode,
5109 gen_lowpart (mode, op0),
5110 mode);
5113 /* If X is 0/1, (eq X 0) is X-1. */
5114 else if (STORE_FLAG_VALUE == -1
5115 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5116 && op1 == const0_rtx
5117 && mode == GET_MODE (op0)
5118 && nonzero_bits (op0, mode) == 1)
5120 op0 = expand_compound_operation (op0);
5121 return plus_constant (gen_lowpart (mode, op0), -1);
5124 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5125 one bit that might be nonzero, we can convert (ne x 0) to
5126 (ashift x c) where C puts the bit in the sign bit. Remove any
5127 AND with STORE_FLAG_VALUE when we are done, since we are only
5128 going to test the sign bit. */
5129 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5130 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5131 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5132 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5133 && op1 == const0_rtx
5134 && mode == GET_MODE (op0)
5135 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5137 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5138 expand_compound_operation (op0),
5139 GET_MODE_BITSIZE (mode) - 1 - i);
5140 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5141 return XEXP (x, 0);
5142 else
5143 return x;
5146 /* If the code changed, return a whole new comparison. */
5147 if (new_code != code)
5148 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5150 /* Otherwise, keep this operation, but maybe change its operands.
5151 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5152 SUBST (XEXP (x, 0), op0);
5153 SUBST (XEXP (x, 1), op1);
5155 break;
5157 case IF_THEN_ELSE:
5158 return simplify_if_then_else (x);
5160 case ZERO_EXTRACT:
5161 case SIGN_EXTRACT:
5162 case ZERO_EXTEND:
5163 case SIGN_EXTEND:
5164 /* If we are processing SET_DEST, we are done. */
5165 if (in_dest)
5166 return x;
5168 return expand_compound_operation (x);
5170 case SET:
5171 return simplify_set (x);
5173 case AND:
5174 case IOR:
5175 return simplify_logical (x);
5177 case ASHIFT:
5178 case LSHIFTRT:
5179 case ASHIFTRT:
5180 case ROTATE:
5181 case ROTATERT:
5182 /* If this is a shift by a constant amount, simplify it. */
5183 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5184 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5185 INTVAL (XEXP (x, 1)));
5187 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5188 SUBST (XEXP (x, 1),
5189 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5190 ((HOST_WIDE_INT) 1
5191 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5192 - 1,
5193 0));
5194 break;
5196 default:
5197 break;
5200 return x;
5203 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5205 static rtx
5206 simplify_if_then_else (rtx x)
5208 enum machine_mode mode = GET_MODE (x);
5209 rtx cond = XEXP (x, 0);
5210 rtx true_rtx = XEXP (x, 1);
5211 rtx false_rtx = XEXP (x, 2);
5212 enum rtx_code true_code = GET_CODE (cond);
5213 int comparison_p = COMPARISON_P (cond);
5214 rtx temp;
5215 int i;
5216 enum rtx_code false_code;
5217 rtx reversed;
5219 /* Simplify storing of the truth value. */
5220 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5221 return simplify_gen_relational (true_code, mode, VOIDmode,
5222 XEXP (cond, 0), XEXP (cond, 1));
5224 /* Also when the truth value has to be reversed. */
5225 if (comparison_p
5226 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5227 && (reversed = reversed_comparison (cond, mode)))
5228 return reversed;
5230 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5231 in it is being compared against certain values. Get the true and false
5232 comparisons and see if that says anything about the value of each arm. */
5234 if (comparison_p
5235 && ((false_code = reversed_comparison_code (cond, NULL))
5236 != UNKNOWN)
5237 && REG_P (XEXP (cond, 0)))
5239 HOST_WIDE_INT nzb;
5240 rtx from = XEXP (cond, 0);
5241 rtx true_val = XEXP (cond, 1);
5242 rtx false_val = true_val;
5243 int swapped = 0;
5245 /* If FALSE_CODE is EQ, swap the codes and arms. */
5247 if (false_code == EQ)
5249 swapped = 1, true_code = EQ, false_code = NE;
5250 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5253 /* If we are comparing against zero and the expression being tested has
5254 only a single bit that might be nonzero, that is its value when it is
5255 not equal to zero. Similarly if it is known to be -1 or 0. */
5257 if (true_code == EQ && true_val == const0_rtx
5258 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5260 false_code = EQ;
5261 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5263 else if (true_code == EQ && true_val == const0_rtx
5264 && (num_sign_bit_copies (from, GET_MODE (from))
5265 == GET_MODE_BITSIZE (GET_MODE (from))))
5267 false_code = EQ;
5268 false_val = constm1_rtx;
5271 /* Now simplify an arm if we know the value of the register in the
5272 branch and it is used in the arm. Be careful due to the potential
5273 of locally-shared RTL. */
5275 if (reg_mentioned_p (from, true_rtx))
5276 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5277 from, true_val),
5278 pc_rtx, pc_rtx, 0, 0);
5279 if (reg_mentioned_p (from, false_rtx))
5280 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5281 from, false_val),
5282 pc_rtx, pc_rtx, 0, 0);
5284 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5285 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5287 true_rtx = XEXP (x, 1);
5288 false_rtx = XEXP (x, 2);
5289 true_code = GET_CODE (cond);
5292 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5293 reversed, do so to avoid needing two sets of patterns for
5294 subtract-and-branch insns. Similarly if we have a constant in the true
5295 arm, the false arm is the same as the first operand of the comparison, or
5296 the false arm is more complicated than the true arm. */
5298 if (comparison_p
5299 && reversed_comparison_code (cond, NULL) != UNKNOWN
5300 && (true_rtx == pc_rtx
5301 || (CONSTANT_P (true_rtx)
5302 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
5303 || true_rtx == const0_rtx
5304 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5305 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5306 && !OBJECT_P (false_rtx))
5307 || reg_mentioned_p (true_rtx, false_rtx)
5308 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5310 true_code = reversed_comparison_code (cond, NULL);
5311 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5312 SUBST (XEXP (x, 1), false_rtx);
5313 SUBST (XEXP (x, 2), true_rtx);
5315 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5316 cond = XEXP (x, 0);
5318 /* It is possible that the conditional has been simplified out. */
5319 true_code = GET_CODE (cond);
5320 comparison_p = COMPARISON_P (cond);
5323 /* If the two arms are identical, we don't need the comparison. */
5325 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5326 return true_rtx;
5328 /* Convert a == b ? b : a to "a". */
5329 if (true_code == EQ && ! side_effects_p (cond)
5330 && !HONOR_NANS (mode)
5331 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5332 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5333 return false_rtx;
5334 else if (true_code == NE && ! side_effects_p (cond)
5335 && !HONOR_NANS (mode)
5336 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5337 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5338 return true_rtx;
5340 /* Look for cases where we have (abs x) or (neg (abs X)). */
5342 if (GET_MODE_CLASS (mode) == MODE_INT
5343 && GET_CODE (false_rtx) == NEG
5344 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5345 && comparison_p
5346 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5347 && ! side_effects_p (true_rtx))
5348 switch (true_code)
5350 case GT:
5351 case GE:
5352 return simplify_gen_unary (ABS, mode, true_rtx, mode);
5353 case LT:
5354 case LE:
5355 return
5356 simplify_gen_unary (NEG, mode,
5357 simplify_gen_unary (ABS, mode, true_rtx, mode),
5358 mode);
5359 default:
5360 break;
5363 /* Look for MIN or MAX. */
5365 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5366 && comparison_p
5367 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5368 && rtx_equal_p (XEXP (cond, 1), false_rtx)
5369 && ! side_effects_p (cond))
5370 switch (true_code)
5372 case GE:
5373 case GT:
5374 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5375 case LE:
5376 case LT:
5377 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5378 case GEU:
5379 case GTU:
5380 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5381 case LEU:
5382 case LTU:
5383 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5384 default:
5385 break;
5388 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5389 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5390 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5391 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5392 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5393 neither 1 or -1, but it isn't worth checking for. */
5395 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5396 && comparison_p
5397 && GET_MODE_CLASS (mode) == MODE_INT
5398 && ! side_effects_p (x))
5400 rtx t = make_compound_operation (true_rtx, SET);
5401 rtx f = make_compound_operation (false_rtx, SET);
5402 rtx cond_op0 = XEXP (cond, 0);
5403 rtx cond_op1 = XEXP (cond, 1);
5404 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5405 enum machine_mode m = mode;
5406 rtx z = 0, c1 = NULL_RTX;
5408 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5409 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5410 || GET_CODE (t) == ASHIFT
5411 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5412 && rtx_equal_p (XEXP (t, 0), f))
5413 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5415 /* If an identity-zero op is commutative, check whether there
5416 would be a match if we swapped the operands. */
5417 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5418 || GET_CODE (t) == XOR)
5419 && rtx_equal_p (XEXP (t, 1), f))
5420 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5421 else if (GET_CODE (t) == SIGN_EXTEND
5422 && (GET_CODE (XEXP (t, 0)) == PLUS
5423 || GET_CODE (XEXP (t, 0)) == MINUS
5424 || GET_CODE (XEXP (t, 0)) == IOR
5425 || GET_CODE (XEXP (t, 0)) == XOR
5426 || GET_CODE (XEXP (t, 0)) == ASHIFT
5427 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5428 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5429 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5430 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5431 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5432 && (num_sign_bit_copies (f, GET_MODE (f))
5433 > (unsigned int)
5434 (GET_MODE_BITSIZE (mode)
5435 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5437 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5438 extend_op = SIGN_EXTEND;
5439 m = GET_MODE (XEXP (t, 0));
5441 else if (GET_CODE (t) == SIGN_EXTEND
5442 && (GET_CODE (XEXP (t, 0)) == PLUS
5443 || GET_CODE (XEXP (t, 0)) == IOR
5444 || GET_CODE (XEXP (t, 0)) == XOR)
5445 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5446 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5447 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5448 && (num_sign_bit_copies (f, GET_MODE (f))
5449 > (unsigned int)
5450 (GET_MODE_BITSIZE (mode)
5451 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5453 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5454 extend_op = SIGN_EXTEND;
5455 m = GET_MODE (XEXP (t, 0));
5457 else if (GET_CODE (t) == ZERO_EXTEND
5458 && (GET_CODE (XEXP (t, 0)) == PLUS
5459 || GET_CODE (XEXP (t, 0)) == MINUS
5460 || GET_CODE (XEXP (t, 0)) == IOR
5461 || GET_CODE (XEXP (t, 0)) == XOR
5462 || GET_CODE (XEXP (t, 0)) == ASHIFT
5463 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5464 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5465 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5466 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5467 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5468 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5469 && ((nonzero_bits (f, GET_MODE (f))
5470 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5471 == 0))
5473 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5474 extend_op = ZERO_EXTEND;
5475 m = GET_MODE (XEXP (t, 0));
5477 else if (GET_CODE (t) == ZERO_EXTEND
5478 && (GET_CODE (XEXP (t, 0)) == PLUS
5479 || GET_CODE (XEXP (t, 0)) == IOR
5480 || GET_CODE (XEXP (t, 0)) == XOR)
5481 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5482 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5483 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5484 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5485 && ((nonzero_bits (f, GET_MODE (f))
5486 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5487 == 0))
5489 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5490 extend_op = ZERO_EXTEND;
5491 m = GET_MODE (XEXP (t, 0));
5494 if (z)
5496 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5497 cond_op0, cond_op1),
5498 pc_rtx, pc_rtx, 0, 0);
5499 temp = simplify_gen_binary (MULT, m, temp,
5500 simplify_gen_binary (MULT, m, c1,
5501 const_true_rtx));
5502 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5503 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5505 if (extend_op != UNKNOWN)
5506 temp = simplify_gen_unary (extend_op, mode, temp, m);
5508 return temp;
5512 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5513 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5514 negation of a single bit, we can convert this operation to a shift. We
5515 can actually do this more generally, but it doesn't seem worth it. */
5517 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5518 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5519 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5520 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5521 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5522 == GET_MODE_BITSIZE (mode))
5523 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5524 return
5525 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5526 gen_lowpart (mode, XEXP (cond, 0)), i);
5528 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5529 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5530 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5531 && GET_MODE (XEXP (cond, 0)) == mode
5532 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5533 == nonzero_bits (XEXP (cond, 0), mode)
5534 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5535 return XEXP (cond, 0);
5537 return x;
5540 /* Simplify X, a SET expression. Return the new expression. */
5542 static rtx
5543 simplify_set (rtx x)
5545 rtx src = SET_SRC (x);
5546 rtx dest = SET_DEST (x);
5547 enum machine_mode mode
5548 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5549 rtx other_insn;
5550 rtx *cc_use;
5552 /* (set (pc) (return)) gets written as (return). */
5553 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5554 return src;
5556 /* Now that we know for sure which bits of SRC we are using, see if we can
5557 simplify the expression for the object knowing that we only need the
5558 low-order bits. */
5560 if (GET_MODE_CLASS (mode) == MODE_INT
5561 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5563 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5564 SUBST (SET_SRC (x), src);
5567 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5568 the comparison result and try to simplify it unless we already have used
5569 undobuf.other_insn. */
5570 if ((GET_MODE_CLASS (mode) == MODE_CC
5571 || GET_CODE (src) == COMPARE
5572 || CC0_P (dest))
5573 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5574 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5575 && COMPARISON_P (*cc_use)
5576 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5578 enum rtx_code old_code = GET_CODE (*cc_use);
5579 enum rtx_code new_code;
5580 rtx op0, op1, tmp;
5581 int other_changed = 0;
5582 enum machine_mode compare_mode = GET_MODE (dest);
5584 if (GET_CODE (src) == COMPARE)
5585 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5586 else
5587 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5589 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5590 op0, op1);
5591 if (!tmp)
5592 new_code = old_code;
5593 else if (!CONSTANT_P (tmp))
5595 new_code = GET_CODE (tmp);
5596 op0 = XEXP (tmp, 0);
5597 op1 = XEXP (tmp, 1);
5599 else
5601 rtx pat = PATTERN (other_insn);
5602 undobuf.other_insn = other_insn;
5603 SUBST (*cc_use, tmp);
5605 /* Attempt to simplify CC user. */
5606 if (GET_CODE (pat) == SET)
5608 rtx new = simplify_rtx (SET_SRC (pat));
5609 if (new != NULL_RTX)
5610 SUBST (SET_SRC (pat), new);
5613 /* Convert X into a no-op move. */
5614 SUBST (SET_DEST (x), pc_rtx);
5615 SUBST (SET_SRC (x), pc_rtx);
5616 return x;
5619 /* Simplify our comparison, if possible. */
5620 new_code = simplify_comparison (new_code, &op0, &op1);
5622 #ifdef SELECT_CC_MODE
5623 /* If this machine has CC modes other than CCmode, check to see if we
5624 need to use a different CC mode here. */
5625 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5626 compare_mode = GET_MODE (op0);
5627 else
5628 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5630 #ifndef HAVE_cc0
5631 /* If the mode changed, we have to change SET_DEST, the mode in the
5632 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5633 a hard register, just build new versions with the proper mode. If it
5634 is a pseudo, we lose unless it is only time we set the pseudo, in
5635 which case we can safely change its mode. */
5636 if (compare_mode != GET_MODE (dest))
5638 if (can_change_dest_mode (dest, 0, compare_mode))
5640 unsigned int regno = REGNO (dest);
5641 rtx new_dest;
5643 if (regno < FIRST_PSEUDO_REGISTER)
5644 new_dest = gen_rtx_REG (compare_mode, regno);
5645 else
5647 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5648 new_dest = regno_reg_rtx[regno];
5651 SUBST (SET_DEST (x), new_dest);
5652 SUBST (XEXP (*cc_use, 0), new_dest);
5653 other_changed = 1;
5655 dest = new_dest;
5658 #endif /* cc0 */
5659 #endif /* SELECT_CC_MODE */
5661 /* If the code changed, we have to build a new comparison in
5662 undobuf.other_insn. */
5663 if (new_code != old_code)
5665 int other_changed_previously = other_changed;
5666 unsigned HOST_WIDE_INT mask;
5668 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5669 dest, const0_rtx));
5670 other_changed = 1;
5672 /* If the only change we made was to change an EQ into an NE or
5673 vice versa, OP0 has only one bit that might be nonzero, and OP1
5674 is zero, check if changing the user of the condition code will
5675 produce a valid insn. If it won't, we can keep the original code
5676 in that insn by surrounding our operation with an XOR. */
5678 if (((old_code == NE && new_code == EQ)
5679 || (old_code == EQ && new_code == NE))
5680 && ! other_changed_previously && op1 == const0_rtx
5681 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5682 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5684 rtx pat = PATTERN (other_insn), note = 0;
5686 if ((recog_for_combine (&pat, other_insn, &note) < 0
5687 && ! check_asm_operands (pat)))
5689 PUT_CODE (*cc_use, old_code);
5690 other_changed = 0;
5692 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5693 op0, GEN_INT (mask));
5698 if (other_changed)
5699 undobuf.other_insn = other_insn;
5701 #ifdef HAVE_cc0
5702 /* If we are now comparing against zero, change our source if
5703 needed. If we do not use cc0, we always have a COMPARE. */
5704 if (op1 == const0_rtx && dest == cc0_rtx)
5706 SUBST (SET_SRC (x), op0);
5707 src = op0;
5709 else
5710 #endif
5712 /* Otherwise, if we didn't previously have a COMPARE in the
5713 correct mode, we need one. */
5714 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5716 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5717 src = SET_SRC (x);
5719 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5721 SUBST (SET_SRC (x), op0);
5722 src = SET_SRC (x);
5724 /* Otherwise, update the COMPARE if needed. */
5725 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
5727 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5728 src = SET_SRC (x);
5731 else
5733 /* Get SET_SRC in a form where we have placed back any
5734 compound expressions. Then do the checks below. */
5735 src = make_compound_operation (src, SET);
5736 SUBST (SET_SRC (x), src);
5739 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5740 and X being a REG or (subreg (reg)), we may be able to convert this to
5741 (set (subreg:m2 x) (op)).
5743 We can always do this if M1 is narrower than M2 because that means that
5744 we only care about the low bits of the result.
5746 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5747 perform a narrower operation than requested since the high-order bits will
5748 be undefined. On machine where it is defined, this transformation is safe
5749 as long as M1 and M2 have the same number of words. */
5751 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5752 && !OBJECT_P (SUBREG_REG (src))
5753 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5754 / UNITS_PER_WORD)
5755 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5756 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5757 #ifndef WORD_REGISTER_OPERATIONS
5758 && (GET_MODE_SIZE (GET_MODE (src))
5759 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5760 #endif
5761 #ifdef CANNOT_CHANGE_MODE_CLASS
5762 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5763 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5764 GET_MODE (SUBREG_REG (src)),
5765 GET_MODE (src)))
5766 #endif
5767 && (REG_P (dest)
5768 || (GET_CODE (dest) == SUBREG
5769 && REG_P (SUBREG_REG (dest)))))
5771 SUBST (SET_DEST (x),
5772 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5773 dest));
5774 SUBST (SET_SRC (x), SUBREG_REG (src));
5776 src = SET_SRC (x), dest = SET_DEST (x);
5779 #ifdef HAVE_cc0
5780 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5781 in SRC. */
5782 if (dest == cc0_rtx
5783 && GET_CODE (src) == SUBREG
5784 && subreg_lowpart_p (src)
5785 && (GET_MODE_BITSIZE (GET_MODE (src))
5786 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5788 rtx inner = SUBREG_REG (src);
5789 enum machine_mode inner_mode = GET_MODE (inner);
5791 /* Here we make sure that we don't have a sign bit on. */
5792 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5793 && (nonzero_bits (inner, inner_mode)
5794 < ((unsigned HOST_WIDE_INT) 1
5795 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5797 SUBST (SET_SRC (x), inner);
5798 src = SET_SRC (x);
5801 #endif
5803 #ifdef LOAD_EXTEND_OP
5804 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5805 would require a paradoxical subreg. Replace the subreg with a
5806 zero_extend to avoid the reload that would otherwise be required. */
5808 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5809 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5810 && SUBREG_BYTE (src) == 0
5811 && (GET_MODE_SIZE (GET_MODE (src))
5812 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5813 && MEM_P (SUBREG_REG (src)))
5815 SUBST (SET_SRC (x),
5816 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5817 GET_MODE (src), SUBREG_REG (src)));
5819 src = SET_SRC (x);
5821 #endif
5823 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5824 are comparing an item known to be 0 or -1 against 0, use a logical
5825 operation instead. Check for one of the arms being an IOR of the other
5826 arm with some value. We compute three terms to be IOR'ed together. In
5827 practice, at most two will be nonzero. Then we do the IOR's. */
5829 if (GET_CODE (dest) != PC
5830 && GET_CODE (src) == IF_THEN_ELSE
5831 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5832 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5833 && XEXP (XEXP (src, 0), 1) == const0_rtx
5834 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5835 #ifdef HAVE_conditional_move
5836 && ! can_conditionally_move_p (GET_MODE (src))
5837 #endif
5838 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5839 GET_MODE (XEXP (XEXP (src, 0), 0)))
5840 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5841 && ! side_effects_p (src))
5843 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5844 ? XEXP (src, 1) : XEXP (src, 2));
5845 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5846 ? XEXP (src, 2) : XEXP (src, 1));
5847 rtx term1 = const0_rtx, term2, term3;
5849 if (GET_CODE (true_rtx) == IOR
5850 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5851 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5852 else if (GET_CODE (true_rtx) == IOR
5853 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5854 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5855 else if (GET_CODE (false_rtx) == IOR
5856 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5857 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5858 else if (GET_CODE (false_rtx) == IOR
5859 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5860 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5862 term2 = simplify_gen_binary (AND, GET_MODE (src),
5863 XEXP (XEXP (src, 0), 0), true_rtx);
5864 term3 = simplify_gen_binary (AND, GET_MODE (src),
5865 simplify_gen_unary (NOT, GET_MODE (src),
5866 XEXP (XEXP (src, 0), 0),
5867 GET_MODE (src)),
5868 false_rtx);
5870 SUBST (SET_SRC (x),
5871 simplify_gen_binary (IOR, GET_MODE (src),
5872 simplify_gen_binary (IOR, GET_MODE (src),
5873 term1, term2),
5874 term3));
5876 src = SET_SRC (x);
5879 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5880 whole thing fail. */
5881 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5882 return src;
5883 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5884 return dest;
5885 else
5886 /* Convert this into a field assignment operation, if possible. */
5887 return make_field_assignment (x);
5890 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5891 result. */
5893 static rtx
5894 simplify_logical (rtx x)
5896 enum machine_mode mode = GET_MODE (x);
5897 rtx op0 = XEXP (x, 0);
5898 rtx op1 = XEXP (x, 1);
5900 switch (GET_CODE (x))
5902 case AND:
5903 /* We can call simplify_and_const_int only if we don't lose
5904 any (sign) bits when converting INTVAL (op1) to
5905 "unsigned HOST_WIDE_INT". */
5906 if (GET_CODE (op1) == CONST_INT
5907 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5908 || INTVAL (op1) > 0))
5910 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5911 if (GET_CODE (x) != AND)
5912 return x;
5914 op0 = XEXP (x, 0);
5915 op1 = XEXP (x, 1);
5918 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5919 apply the distributive law and then the inverse distributive
5920 law to see if things simplify. */
5921 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5923 rtx result = distribute_and_simplify_rtx (x, 0);
5924 if (result)
5925 return result;
5927 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5929 rtx result = distribute_and_simplify_rtx (x, 1);
5930 if (result)
5931 return result;
5933 break;
5935 case IOR:
5936 /* If we have (ior (and A B) C), apply the distributive law and then
5937 the inverse distributive law to see if things simplify. */
5939 if (GET_CODE (op0) == AND)
5941 rtx result = distribute_and_simplify_rtx (x, 0);
5942 if (result)
5943 return result;
5946 if (GET_CODE (op1) == AND)
5948 rtx result = distribute_and_simplify_rtx (x, 1);
5949 if (result)
5950 return result;
5952 break;
5954 default:
5955 gcc_unreachable ();
5958 return x;
5961 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5962 operations" because they can be replaced with two more basic operations.
5963 ZERO_EXTEND is also considered "compound" because it can be replaced with
5964 an AND operation, which is simpler, though only one operation.
5966 The function expand_compound_operation is called with an rtx expression
5967 and will convert it to the appropriate shifts and AND operations,
5968 simplifying at each stage.
5970 The function make_compound_operation is called to convert an expression
5971 consisting of shifts and ANDs into the equivalent compound expression.
5972 It is the inverse of this function, loosely speaking. */
5974 static rtx
5975 expand_compound_operation (rtx x)
5977 unsigned HOST_WIDE_INT pos = 0, len;
5978 int unsignedp = 0;
5979 unsigned int modewidth;
5980 rtx tem;
5982 switch (GET_CODE (x))
5984 case ZERO_EXTEND:
5985 unsignedp = 1;
5986 case SIGN_EXTEND:
5987 /* We can't necessarily use a const_int for a multiword mode;
5988 it depends on implicitly extending the value.
5989 Since we don't know the right way to extend it,
5990 we can't tell whether the implicit way is right.
5992 Even for a mode that is no wider than a const_int,
5993 we can't win, because we need to sign extend one of its bits through
5994 the rest of it, and we don't know which bit. */
5995 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5996 return x;
5998 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5999 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6000 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6001 reloaded. If not for that, MEM's would very rarely be safe.
6003 Reject MODEs bigger than a word, because we might not be able
6004 to reference a two-register group starting with an arbitrary register
6005 (and currently gen_lowpart might crash for a SUBREG). */
6007 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6008 return x;
6010 /* Reject MODEs that aren't scalar integers because turning vector
6011 or complex modes into shifts causes problems. */
6013 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6014 return x;
6016 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6017 /* If the inner object has VOIDmode (the only way this can happen
6018 is if it is an ASM_OPERANDS), we can't do anything since we don't
6019 know how much masking to do. */
6020 if (len == 0)
6021 return x;
6023 break;
6025 case ZERO_EXTRACT:
6026 unsignedp = 1;
6028 /* ... fall through ... */
6030 case SIGN_EXTRACT:
6031 /* If the operand is a CLOBBER, just return it. */
6032 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6033 return XEXP (x, 0);
6035 if (GET_CODE (XEXP (x, 1)) != CONST_INT
6036 || GET_CODE (XEXP (x, 2)) != CONST_INT
6037 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6038 return x;
6040 /* Reject MODEs that aren't scalar integers because turning vector
6041 or complex modes into shifts causes problems. */
6043 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6044 return x;
6046 len = INTVAL (XEXP (x, 1));
6047 pos = INTVAL (XEXP (x, 2));
6049 /* This should stay within the object being extracted, fail otherwise. */
6050 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6051 return x;
6053 if (BITS_BIG_ENDIAN)
6054 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6056 break;
6058 default:
6059 return x;
6061 /* Convert sign extension to zero extension, if we know that the high
6062 bit is not set, as this is easier to optimize. It will be converted
6063 back to cheaper alternative in make_extraction. */
6064 if (GET_CODE (x) == SIGN_EXTEND
6065 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6066 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6067 & ~(((unsigned HOST_WIDE_INT)
6068 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6069 >> 1))
6070 == 0)))
6072 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6073 rtx temp2 = expand_compound_operation (temp);
6075 /* Make sure this is a profitable operation. */
6076 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
6077 return temp2;
6078 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
6079 return temp;
6080 else
6081 return x;
6084 /* We can optimize some special cases of ZERO_EXTEND. */
6085 if (GET_CODE (x) == ZERO_EXTEND)
6087 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6088 know that the last value didn't have any inappropriate bits
6089 set. */
6090 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6091 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6092 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6093 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6094 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6095 return XEXP (XEXP (x, 0), 0);
6097 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6098 if (GET_CODE (XEXP (x, 0)) == SUBREG
6099 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6100 && subreg_lowpart_p (XEXP (x, 0))
6101 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6102 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6103 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6104 return SUBREG_REG (XEXP (x, 0));
6106 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6107 is a comparison and STORE_FLAG_VALUE permits. This is like
6108 the first case, but it works even when GET_MODE (x) is larger
6109 than HOST_WIDE_INT. */
6110 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6111 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6112 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6113 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6114 <= HOST_BITS_PER_WIDE_INT)
6115 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6116 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6117 return XEXP (XEXP (x, 0), 0);
6119 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6120 if (GET_CODE (XEXP (x, 0)) == SUBREG
6121 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6122 && subreg_lowpart_p (XEXP (x, 0))
6123 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6124 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6125 <= HOST_BITS_PER_WIDE_INT)
6126 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6127 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6128 return SUBREG_REG (XEXP (x, 0));
6132 /* If we reach here, we want to return a pair of shifts. The inner
6133 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6134 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6135 logical depending on the value of UNSIGNEDP.
6137 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6138 converted into an AND of a shift.
6140 We must check for the case where the left shift would have a negative
6141 count. This can happen in a case like (x >> 31) & 255 on machines
6142 that can't shift by a constant. On those machines, we would first
6143 combine the shift with the AND to produce a variable-position
6144 extraction. Then the constant of 31 would be substituted in to produce
6145 a such a position. */
6147 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6148 if (modewidth + len >= pos)
6150 enum machine_mode mode = GET_MODE (x);
6151 tem = gen_lowpart (mode, XEXP (x, 0));
6152 if (!tem || GET_CODE (tem) == CLOBBER)
6153 return x;
6154 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6155 tem, modewidth - pos - len);
6156 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6157 mode, tem, modewidth - len);
6159 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6160 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6161 simplify_shift_const (NULL_RTX, LSHIFTRT,
6162 GET_MODE (x),
6163 XEXP (x, 0), pos),
6164 ((HOST_WIDE_INT) 1 << len) - 1);
6165 else
6166 /* Any other cases we can't handle. */
6167 return x;
6169 /* If we couldn't do this for some reason, return the original
6170 expression. */
6171 if (GET_CODE (tem) == CLOBBER)
6172 return x;
6174 return tem;
6177 /* X is a SET which contains an assignment of one object into
6178 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6179 or certain SUBREGS). If possible, convert it into a series of
6180 logical operations.
6182 We half-heartedly support variable positions, but do not at all
6183 support variable lengths. */
6185 static const_rtx
6186 expand_field_assignment (const_rtx x)
6188 rtx inner;
6189 rtx pos; /* Always counts from low bit. */
6190 int len;
6191 rtx mask, cleared, masked;
6192 enum machine_mode compute_mode;
6194 /* Loop until we find something we can't simplify. */
6195 while (1)
6197 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6198 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6200 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6201 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6202 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6204 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6205 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6207 inner = XEXP (SET_DEST (x), 0);
6208 len = INTVAL (XEXP (SET_DEST (x), 1));
6209 pos = XEXP (SET_DEST (x), 2);
6211 /* A constant position should stay within the width of INNER. */
6212 if (GET_CODE (pos) == CONST_INT
6213 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6214 break;
6216 if (BITS_BIG_ENDIAN)
6218 if (GET_CODE (pos) == CONST_INT)
6219 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6220 - INTVAL (pos));
6221 else if (GET_CODE (pos) == MINUS
6222 && GET_CODE (XEXP (pos, 1)) == CONST_INT
6223 && (INTVAL (XEXP (pos, 1))
6224 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6225 /* If position is ADJUST - X, new position is X. */
6226 pos = XEXP (pos, 0);
6227 else
6228 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6229 GEN_INT (GET_MODE_BITSIZE (
6230 GET_MODE (inner))
6231 - len),
6232 pos);
6236 /* A SUBREG between two modes that occupy the same numbers of words
6237 can be done by moving the SUBREG to the source. */
6238 else if (GET_CODE (SET_DEST (x)) == SUBREG
6239 /* We need SUBREGs to compute nonzero_bits properly. */
6240 && nonzero_sign_valid
6241 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6242 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6243 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6244 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6246 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6247 gen_lowpart
6248 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6249 SET_SRC (x)));
6250 continue;
6252 else
6253 break;
6255 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6256 inner = SUBREG_REG (inner);
6258 compute_mode = GET_MODE (inner);
6260 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6261 if (! SCALAR_INT_MODE_P (compute_mode))
6263 enum machine_mode imode;
6265 /* Don't do anything for vector or complex integral types. */
6266 if (! FLOAT_MODE_P (compute_mode))
6267 break;
6269 /* Try to find an integral mode to pun with. */
6270 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6271 if (imode == BLKmode)
6272 break;
6274 compute_mode = imode;
6275 inner = gen_lowpart (imode, inner);
6278 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6279 if (len >= HOST_BITS_PER_WIDE_INT)
6280 break;
6282 /* Now compute the equivalent expression. Make a copy of INNER
6283 for the SET_DEST in case it is a MEM into which we will substitute;
6284 we don't want shared RTL in that case. */
6285 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6286 cleared = simplify_gen_binary (AND, compute_mode,
6287 simplify_gen_unary (NOT, compute_mode,
6288 simplify_gen_binary (ASHIFT,
6289 compute_mode,
6290 mask, pos),
6291 compute_mode),
6292 inner);
6293 masked = simplify_gen_binary (ASHIFT, compute_mode,
6294 simplify_gen_binary (
6295 AND, compute_mode,
6296 gen_lowpart (compute_mode, SET_SRC (x)),
6297 mask),
6298 pos);
6300 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6301 simplify_gen_binary (IOR, compute_mode,
6302 cleared, masked));
6305 return x;
6308 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6309 it is an RTX that represents a variable starting position; otherwise,
6310 POS is the (constant) starting bit position (counted from the LSB).
6312 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6313 signed reference.
6315 IN_DEST is nonzero if this is a reference in the destination of a
6316 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6317 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6318 be used.
6320 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6321 ZERO_EXTRACT should be built even for bits starting at bit 0.
6323 MODE is the desired mode of the result (if IN_DEST == 0).
6325 The result is an RTX for the extraction or NULL_RTX if the target
6326 can't handle it. */
6328 static rtx
6329 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6330 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6331 int in_dest, int in_compare)
6333 /* This mode describes the size of the storage area
6334 to fetch the overall value from. Within that, we
6335 ignore the POS lowest bits, etc. */
6336 enum machine_mode is_mode = GET_MODE (inner);
6337 enum machine_mode inner_mode;
6338 enum machine_mode wanted_inner_mode;
6339 enum machine_mode wanted_inner_reg_mode = word_mode;
6340 enum machine_mode pos_mode = word_mode;
6341 enum machine_mode extraction_mode = word_mode;
6342 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6343 rtx new = 0;
6344 rtx orig_pos_rtx = pos_rtx;
6345 HOST_WIDE_INT orig_pos;
6347 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6349 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6350 consider just the QI as the memory to extract from.
6351 The subreg adds or removes high bits; its mode is
6352 irrelevant to the meaning of this extraction,
6353 since POS and LEN count from the lsb. */
6354 if (MEM_P (SUBREG_REG (inner)))
6355 is_mode = GET_MODE (SUBREG_REG (inner));
6356 inner = SUBREG_REG (inner);
6358 else if (GET_CODE (inner) == ASHIFT
6359 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6360 && pos_rtx == 0 && pos == 0
6361 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6363 /* We're extracting the least significant bits of an rtx
6364 (ashift X (const_int C)), where LEN > C. Extract the
6365 least significant (LEN - C) bits of X, giving an rtx
6366 whose mode is MODE, then shift it left C times. */
6367 new = make_extraction (mode, XEXP (inner, 0),
6368 0, 0, len - INTVAL (XEXP (inner, 1)),
6369 unsignedp, in_dest, in_compare);
6370 if (new != 0)
6371 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6374 inner_mode = GET_MODE (inner);
6376 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6377 pos = INTVAL (pos_rtx), pos_rtx = 0;
6379 /* See if this can be done without an extraction. We never can if the
6380 width of the field is not the same as that of some integer mode. For
6381 registers, we can only avoid the extraction if the position is at the
6382 low-order bit and this is either not in the destination or we have the
6383 appropriate STRICT_LOW_PART operation available.
6385 For MEM, we can avoid an extract if the field starts on an appropriate
6386 boundary and we can change the mode of the memory reference. */
6388 if (tmode != BLKmode
6389 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6390 && !MEM_P (inner)
6391 && (inner_mode == tmode
6392 || !REG_P (inner)
6393 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6394 GET_MODE_BITSIZE (inner_mode))
6395 || reg_truncated_to_mode (tmode, inner))
6396 && (! in_dest
6397 || (REG_P (inner)
6398 && have_insn_for (STRICT_LOW_PART, tmode))))
6399 || (MEM_P (inner) && pos_rtx == 0
6400 && (pos
6401 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6402 : BITS_PER_UNIT)) == 0
6403 /* We can't do this if we are widening INNER_MODE (it
6404 may not be aligned, for one thing). */
6405 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6406 && (inner_mode == tmode
6407 || (! mode_dependent_address_p (XEXP (inner, 0))
6408 && ! MEM_VOLATILE_P (inner))))))
6410 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6411 field. If the original and current mode are the same, we need not
6412 adjust the offset. Otherwise, we do if bytes big endian.
6414 If INNER is not a MEM, get a piece consisting of just the field
6415 of interest (in this case POS % BITS_PER_WORD must be 0). */
6417 if (MEM_P (inner))
6419 HOST_WIDE_INT offset;
6421 /* POS counts from lsb, but make OFFSET count in memory order. */
6422 if (BYTES_BIG_ENDIAN)
6423 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6424 else
6425 offset = pos / BITS_PER_UNIT;
6427 new = adjust_address_nv (inner, tmode, offset);
6429 else if (REG_P (inner))
6431 if (tmode != inner_mode)
6433 /* We can't call gen_lowpart in a DEST since we
6434 always want a SUBREG (see below) and it would sometimes
6435 return a new hard register. */
6436 if (pos || in_dest)
6438 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6440 if (WORDS_BIG_ENDIAN
6441 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6442 final_word = ((GET_MODE_SIZE (inner_mode)
6443 - GET_MODE_SIZE (tmode))
6444 / UNITS_PER_WORD) - final_word;
6446 final_word *= UNITS_PER_WORD;
6447 if (BYTES_BIG_ENDIAN &&
6448 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6449 final_word += (GET_MODE_SIZE (inner_mode)
6450 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6452 /* Avoid creating invalid subregs, for example when
6453 simplifying (x>>32)&255. */
6454 if (!validate_subreg (tmode, inner_mode, inner, final_word))
6455 return NULL_RTX;
6457 new = gen_rtx_SUBREG (tmode, inner, final_word);
6459 else
6460 new = gen_lowpart (tmode, inner);
6462 else
6463 new = inner;
6465 else
6466 new = force_to_mode (inner, tmode,
6467 len >= HOST_BITS_PER_WIDE_INT
6468 ? ~(unsigned HOST_WIDE_INT) 0
6469 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6472 /* If this extraction is going into the destination of a SET,
6473 make a STRICT_LOW_PART unless we made a MEM. */
6475 if (in_dest)
6476 return (MEM_P (new) ? new
6477 : (GET_CODE (new) != SUBREG
6478 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6479 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6481 if (mode == tmode)
6482 return new;
6484 if (GET_CODE (new) == CONST_INT)
6485 return gen_int_mode (INTVAL (new), mode);
6487 /* If we know that no extraneous bits are set, and that the high
6488 bit is not set, convert the extraction to the cheaper of
6489 sign and zero extension, that are equivalent in these cases. */
6490 if (flag_expensive_optimizations
6491 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6492 && ((nonzero_bits (new, tmode)
6493 & ~(((unsigned HOST_WIDE_INT)
6494 GET_MODE_MASK (tmode))
6495 >> 1))
6496 == 0)))
6498 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6499 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6501 /* Prefer ZERO_EXTENSION, since it gives more information to
6502 backends. */
6503 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6504 return temp;
6505 return temp1;
6508 /* Otherwise, sign- or zero-extend unless we already are in the
6509 proper mode. */
6511 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6512 mode, new));
6515 /* Unless this is a COMPARE or we have a funny memory reference,
6516 don't do anything with zero-extending field extracts starting at
6517 the low-order bit since they are simple AND operations. */
6518 if (pos_rtx == 0 && pos == 0 && ! in_dest
6519 && ! in_compare && unsignedp)
6520 return 0;
6522 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6523 if the position is not a constant and the length is not 1. In all
6524 other cases, we would only be going outside our object in cases when
6525 an original shift would have been undefined. */
6526 if (MEM_P (inner)
6527 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6528 || (pos_rtx != 0 && len != 1)))
6529 return 0;
6531 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6532 and the mode for the result. */
6533 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6535 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6536 pos_mode = mode_for_extraction (EP_insv, 2);
6537 extraction_mode = mode_for_extraction (EP_insv, 3);
6540 if (! in_dest && unsignedp
6541 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6543 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6544 pos_mode = mode_for_extraction (EP_extzv, 3);
6545 extraction_mode = mode_for_extraction (EP_extzv, 0);
6548 if (! in_dest && ! unsignedp
6549 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6551 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6552 pos_mode = mode_for_extraction (EP_extv, 3);
6553 extraction_mode = mode_for_extraction (EP_extv, 0);
6556 /* Never narrow an object, since that might not be safe. */
6558 if (mode != VOIDmode
6559 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6560 extraction_mode = mode;
6562 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6563 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6564 pos_mode = GET_MODE (pos_rtx);
6566 /* If this is not from memory, the desired mode is the preferred mode
6567 for an extraction pattern's first input operand, or word_mode if there
6568 is none. */
6569 if (!MEM_P (inner))
6570 wanted_inner_mode = wanted_inner_reg_mode;
6571 else
6573 /* Be careful not to go beyond the extracted object and maintain the
6574 natural alignment of the memory. */
6575 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6576 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6577 > GET_MODE_BITSIZE (wanted_inner_mode))
6579 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6580 gcc_assert (wanted_inner_mode != VOIDmode);
6583 /* If we have to change the mode of memory and cannot, the desired mode
6584 is EXTRACTION_MODE. */
6585 if (inner_mode != wanted_inner_mode
6586 && (mode_dependent_address_p (XEXP (inner, 0))
6587 || MEM_VOLATILE_P (inner)
6588 || pos_rtx))
6589 wanted_inner_mode = extraction_mode;
6592 orig_pos = pos;
6594 if (BITS_BIG_ENDIAN)
6596 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6597 BITS_BIG_ENDIAN style. If position is constant, compute new
6598 position. Otherwise, build subtraction.
6599 Note that POS is relative to the mode of the original argument.
6600 If it's a MEM we need to recompute POS relative to that.
6601 However, if we're extracting from (or inserting into) a register,
6602 we want to recompute POS relative to wanted_inner_mode. */
6603 int width = (MEM_P (inner)
6604 ? GET_MODE_BITSIZE (is_mode)
6605 : GET_MODE_BITSIZE (wanted_inner_mode));
6607 if (pos_rtx == 0)
6608 pos = width - len - pos;
6609 else
6610 pos_rtx
6611 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6612 /* POS may be less than 0 now, but we check for that below.
6613 Note that it can only be less than 0 if !MEM_P (inner). */
6616 /* If INNER has a wider mode, and this is a constant extraction, try to
6617 make it smaller and adjust the byte to point to the byte containing
6618 the value. */
6619 if (wanted_inner_mode != VOIDmode
6620 && inner_mode != wanted_inner_mode
6621 && ! pos_rtx
6622 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6623 && MEM_P (inner)
6624 && ! mode_dependent_address_p (XEXP (inner, 0))
6625 && ! MEM_VOLATILE_P (inner))
6627 int offset = 0;
6629 /* The computations below will be correct if the machine is big
6630 endian in both bits and bytes or little endian in bits and bytes.
6631 If it is mixed, we must adjust. */
6633 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6634 adjust OFFSET to compensate. */
6635 if (BYTES_BIG_ENDIAN
6636 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6637 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6639 /* We can now move to the desired byte. */
6640 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6641 * GET_MODE_SIZE (wanted_inner_mode);
6642 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6644 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6645 && is_mode != wanted_inner_mode)
6646 offset = (GET_MODE_SIZE (is_mode)
6647 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6649 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6652 /* If INNER is not memory, we can always get it into the proper mode. If we
6653 are changing its mode, POS must be a constant and smaller than the size
6654 of the new mode. */
6655 else if (!MEM_P (inner))
6657 if (GET_MODE (inner) != wanted_inner_mode
6658 && (pos_rtx != 0
6659 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6660 return 0;
6662 if (orig_pos < 0)
6663 return 0;
6665 inner = force_to_mode (inner, wanted_inner_mode,
6666 pos_rtx
6667 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6668 ? ~(unsigned HOST_WIDE_INT) 0
6669 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6670 << orig_pos),
6674 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6675 have to zero extend. Otherwise, we can just use a SUBREG. */
6676 if (pos_rtx != 0
6677 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6679 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6681 /* If we know that no extraneous bits are set, and that the high
6682 bit is not set, convert extraction to cheaper one - either
6683 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6684 cases. */
6685 if (flag_expensive_optimizations
6686 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6687 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6688 & ~(((unsigned HOST_WIDE_INT)
6689 GET_MODE_MASK (GET_MODE (pos_rtx)))
6690 >> 1))
6691 == 0)))
6693 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6695 /* Prefer ZERO_EXTENSION, since it gives more information to
6696 backends. */
6697 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6698 temp = temp1;
6700 pos_rtx = temp;
6702 else if (pos_rtx != 0
6703 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6704 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6706 /* Make POS_RTX unless we already have it and it is correct. If we don't
6707 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6708 be a CONST_INT. */
6709 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6710 pos_rtx = orig_pos_rtx;
6712 else if (pos_rtx == 0)
6713 pos_rtx = GEN_INT (pos);
6715 /* Make the required operation. See if we can use existing rtx. */
6716 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6717 extraction_mode, inner, GEN_INT (len), pos_rtx);
6718 if (! in_dest)
6719 new = gen_lowpart (mode, new);
6721 return new;
6724 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6725 with any other operations in X. Return X without that shift if so. */
6727 static rtx
6728 extract_left_shift (rtx x, int count)
6730 enum rtx_code code = GET_CODE (x);
6731 enum machine_mode mode = GET_MODE (x);
6732 rtx tem;
6734 switch (code)
6736 case ASHIFT:
6737 /* This is the shift itself. If it is wide enough, we will return
6738 either the value being shifted if the shift count is equal to
6739 COUNT or a shift for the difference. */
6740 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6741 && INTVAL (XEXP (x, 1)) >= count)
6742 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6743 INTVAL (XEXP (x, 1)) - count);
6744 break;
6746 case NEG: case NOT:
6747 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6748 return simplify_gen_unary (code, mode, tem, mode);
6750 break;
6752 case PLUS: case IOR: case XOR: case AND:
6753 /* If we can safely shift this constant and we find the inner shift,
6754 make a new operation. */
6755 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6756 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6757 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6758 return simplify_gen_binary (code, mode, tem,
6759 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6761 break;
6763 default:
6764 break;
6767 return 0;
6770 /* Look at the expression rooted at X. Look for expressions
6771 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6772 Form these expressions.
6774 Return the new rtx, usually just X.
6776 Also, for machines like the VAX that don't have logical shift insns,
6777 try to convert logical to arithmetic shift operations in cases where
6778 they are equivalent. This undoes the canonicalizations to logical
6779 shifts done elsewhere.
6781 We try, as much as possible, to re-use rtl expressions to save memory.
6783 IN_CODE says what kind of expression we are processing. Normally, it is
6784 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6785 being kludges), it is MEM. When processing the arguments of a comparison
6786 or a COMPARE against zero, it is COMPARE. */
6788 static rtx
6789 make_compound_operation (rtx x, enum rtx_code in_code)
6791 enum rtx_code code = GET_CODE (x);
6792 enum machine_mode mode = GET_MODE (x);
6793 int mode_width = GET_MODE_BITSIZE (mode);
6794 rtx rhs, lhs;
6795 enum rtx_code next_code;
6796 int i;
6797 rtx new = 0;
6798 rtx tem;
6799 const char *fmt;
6801 /* Select the code to be used in recursive calls. Once we are inside an
6802 address, we stay there. If we have a comparison, set to COMPARE,
6803 but once inside, go back to our default of SET. */
6805 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6806 : ((code == COMPARE || COMPARISON_P (x))
6807 && XEXP (x, 1) == const0_rtx) ? COMPARE
6808 : in_code == COMPARE ? SET : in_code);
6810 /* Process depending on the code of this operation. If NEW is set
6811 nonzero, it will be returned. */
6813 switch (code)
6815 case ASHIFT:
6816 /* Convert shifts by constants into multiplications if inside
6817 an address. */
6818 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6819 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6820 && INTVAL (XEXP (x, 1)) >= 0)
6822 new = make_compound_operation (XEXP (x, 0), next_code);
6823 new = gen_rtx_MULT (mode, new,
6824 GEN_INT ((HOST_WIDE_INT) 1
6825 << INTVAL (XEXP (x, 1))));
6827 break;
6829 case AND:
6830 /* If the second operand is not a constant, we can't do anything
6831 with it. */
6832 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6833 break;
6835 /* If the constant is a power of two minus one and the first operand
6836 is a logical right shift, make an extraction. */
6837 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6838 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6840 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6841 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6842 0, in_code == COMPARE);
6845 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6846 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6847 && subreg_lowpart_p (XEXP (x, 0))
6848 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6849 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6851 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6852 next_code);
6853 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6854 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6855 0, in_code == COMPARE);
6857 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6858 else if ((GET_CODE (XEXP (x, 0)) == XOR
6859 || GET_CODE (XEXP (x, 0)) == IOR)
6860 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6861 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6862 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6864 /* Apply the distributive law, and then try to make extractions. */
6865 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6866 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6867 XEXP (x, 1)),
6868 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6869 XEXP (x, 1)));
6870 new = make_compound_operation (new, in_code);
6873 /* If we are have (and (rotate X C) M) and C is larger than the number
6874 of bits in M, this is an extraction. */
6876 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6877 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6878 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6879 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6881 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6882 new = make_extraction (mode, new,
6883 (GET_MODE_BITSIZE (mode)
6884 - INTVAL (XEXP (XEXP (x, 0), 1))),
6885 NULL_RTX, i, 1, 0, in_code == COMPARE);
6888 /* On machines without logical shifts, if the operand of the AND is
6889 a logical shift and our mask turns off all the propagated sign
6890 bits, we can replace the logical shift with an arithmetic shift. */
6891 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6892 && !have_insn_for (LSHIFTRT, mode)
6893 && have_insn_for (ASHIFTRT, mode)
6894 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6895 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6896 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6897 && mode_width <= HOST_BITS_PER_WIDE_INT)
6899 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6901 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6902 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6903 SUBST (XEXP (x, 0),
6904 gen_rtx_ASHIFTRT (mode,
6905 make_compound_operation
6906 (XEXP (XEXP (x, 0), 0), next_code),
6907 XEXP (XEXP (x, 0), 1)));
6910 /* If the constant is one less than a power of two, this might be
6911 representable by an extraction even if no shift is present.
6912 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6913 we are in a COMPARE. */
6914 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6915 new = make_extraction (mode,
6916 make_compound_operation (XEXP (x, 0),
6917 next_code),
6918 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6920 /* If we are in a comparison and this is an AND with a power of two,
6921 convert this into the appropriate bit extract. */
6922 else if (in_code == COMPARE
6923 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6924 new = make_extraction (mode,
6925 make_compound_operation (XEXP (x, 0),
6926 next_code),
6927 i, NULL_RTX, 1, 1, 0, 1);
6929 break;
6931 case LSHIFTRT:
6932 /* If the sign bit is known to be zero, replace this with an
6933 arithmetic shift. */
6934 if (have_insn_for (ASHIFTRT, mode)
6935 && ! have_insn_for (LSHIFTRT, mode)
6936 && mode_width <= HOST_BITS_PER_WIDE_INT
6937 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6939 new = gen_rtx_ASHIFTRT (mode,
6940 make_compound_operation (XEXP (x, 0),
6941 next_code),
6942 XEXP (x, 1));
6943 break;
6946 /* ... fall through ... */
6948 case ASHIFTRT:
6949 lhs = XEXP (x, 0);
6950 rhs = XEXP (x, 1);
6952 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6953 this is a SIGN_EXTRACT. */
6954 if (GET_CODE (rhs) == CONST_INT
6955 && GET_CODE (lhs) == ASHIFT
6956 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6957 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6959 new = make_compound_operation (XEXP (lhs, 0), next_code);
6960 new = make_extraction (mode, new,
6961 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6962 NULL_RTX, mode_width - INTVAL (rhs),
6963 code == LSHIFTRT, 0, in_code == COMPARE);
6964 break;
6967 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6968 If so, try to merge the shifts into a SIGN_EXTEND. We could
6969 also do this for some cases of SIGN_EXTRACT, but it doesn't
6970 seem worth the effort; the case checked for occurs on Alpha. */
6972 if (!OBJECT_P (lhs)
6973 && ! (GET_CODE (lhs) == SUBREG
6974 && (OBJECT_P (SUBREG_REG (lhs))))
6975 && GET_CODE (rhs) == CONST_INT
6976 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6977 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6978 new = make_extraction (mode, make_compound_operation (new, next_code),
6979 0, NULL_RTX, mode_width - INTVAL (rhs),
6980 code == LSHIFTRT, 0, in_code == COMPARE);
6982 break;
6984 case SUBREG:
6985 /* Call ourselves recursively on the inner expression. If we are
6986 narrowing the object and it has a different RTL code from
6987 what it originally did, do this SUBREG as a force_to_mode. */
6989 tem = make_compound_operation (SUBREG_REG (x), in_code);
6992 rtx simplified;
6993 simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6994 SUBREG_BYTE (x));
6996 if (simplified)
6997 tem = simplified;
6999 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
7000 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
7001 && subreg_lowpart_p (x))
7003 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
7006 /* If we have something other than a SUBREG, we might have
7007 done an expansion, so rerun ourselves. */
7008 if (GET_CODE (newer) != SUBREG)
7009 newer = make_compound_operation (newer, in_code);
7011 return newer;
7014 if (simplified)
7015 return tem;
7017 break;
7019 default:
7020 break;
7023 if (new)
7025 x = gen_lowpart (mode, new);
7026 code = GET_CODE (x);
7029 /* Now recursively process each operand of this operation. */
7030 fmt = GET_RTX_FORMAT (code);
7031 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7032 if (fmt[i] == 'e')
7034 new = make_compound_operation (XEXP (x, i), next_code);
7035 SUBST (XEXP (x, i), new);
7038 /* If this is a commutative operation, the changes to the operands
7039 may have made it noncanonical. */
7040 if (COMMUTATIVE_ARITH_P (x)
7041 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7043 tem = XEXP (x, 0);
7044 SUBST (XEXP (x, 0), XEXP (x, 1));
7045 SUBST (XEXP (x, 1), tem);
7048 return x;
7051 /* Given M see if it is a value that would select a field of bits
7052 within an item, but not the entire word. Return -1 if not.
7053 Otherwise, return the starting position of the field, where 0 is the
7054 low-order bit.
7056 *PLEN is set to the length of the field. */
7058 static int
7059 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7061 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7062 int pos = exact_log2 (m & -m);
7063 int len = 0;
7065 if (pos >= 0)
7066 /* Now shift off the low-order zero bits and see if we have a
7067 power of two minus 1. */
7068 len = exact_log2 ((m >> pos) + 1);
7070 if (len <= 0)
7071 pos = -1;
7073 *plen = len;
7074 return pos;
7077 /* If X refers to a register that equals REG in value, replace these
7078 references with REG. */
7079 static rtx
7080 canon_reg_for_combine (rtx x, rtx reg)
7082 rtx op0, op1, op2;
7083 const char *fmt;
7084 int i;
7085 bool copied;
7087 enum rtx_code code = GET_CODE (x);
7088 switch (GET_RTX_CLASS (code))
7090 case RTX_UNARY:
7091 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7092 if (op0 != XEXP (x, 0))
7093 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7094 GET_MODE (reg));
7095 break;
7097 case RTX_BIN_ARITH:
7098 case RTX_COMM_ARITH:
7099 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7100 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7101 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7102 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7103 break;
7105 case RTX_COMPARE:
7106 case RTX_COMM_COMPARE:
7107 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7108 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7109 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7110 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7111 GET_MODE (op0), op0, op1);
7112 break;
7114 case RTX_TERNARY:
7115 case RTX_BITFIELD_OPS:
7116 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7117 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7118 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7119 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7120 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7121 GET_MODE (op0), op0, op1, op2);
7123 case RTX_OBJ:
7124 if (REG_P (x))
7126 if (rtx_equal_p (get_last_value (reg), x)
7127 || rtx_equal_p (reg, get_last_value (x)))
7128 return reg;
7129 else
7130 break;
7133 /* fall through */
7135 default:
7136 fmt = GET_RTX_FORMAT (code);
7137 copied = false;
7138 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7139 if (fmt[i] == 'e')
7141 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7142 if (op != XEXP (x, i))
7144 if (!copied)
7146 copied = true;
7147 x = copy_rtx (x);
7149 XEXP (x, i) = op;
7152 else if (fmt[i] == 'E')
7154 int j;
7155 for (j = 0; j < XVECLEN (x, i); j++)
7157 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7158 if (op != XVECEXP (x, i, j))
7160 if (!copied)
7162 copied = true;
7163 x = copy_rtx (x);
7165 XVECEXP (x, i, j) = op;
7170 break;
7173 return x;
7176 /* Return X converted to MODE. If the value is already truncated to
7177 MODE we can just return a subreg even though in the general case we
7178 would need an explicit truncation. */
7180 static rtx
7181 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7183 if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
7184 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7185 GET_MODE_BITSIZE (GET_MODE (x)))
7186 || (REG_P (x) && reg_truncated_to_mode (mode, x)))
7187 return gen_lowpart (mode, x);
7188 else
7189 return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
7192 /* See if X can be simplified knowing that we will only refer to it in
7193 MODE and will only refer to those bits that are nonzero in MASK.
7194 If other bits are being computed or if masking operations are done
7195 that select a superset of the bits in MASK, they can sometimes be
7196 ignored.
7198 Return a possibly simplified expression, but always convert X to
7199 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7201 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7202 are all off in X. This is used when X will be complemented, by either
7203 NOT, NEG, or XOR. */
7205 static rtx
7206 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7207 int just_select)
7209 enum rtx_code code = GET_CODE (x);
7210 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7211 enum machine_mode op_mode;
7212 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7213 rtx op0, op1, temp;
7215 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7216 code below will do the wrong thing since the mode of such an
7217 expression is VOIDmode.
7219 Also do nothing if X is a CLOBBER; this can happen if X was
7220 the return value from a call to gen_lowpart. */
7221 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7222 return x;
7224 /* We want to perform the operation is its present mode unless we know
7225 that the operation is valid in MODE, in which case we do the operation
7226 in MODE. */
7227 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7228 && have_insn_for (code, mode))
7229 ? mode : GET_MODE (x));
7231 /* It is not valid to do a right-shift in a narrower mode
7232 than the one it came in with. */
7233 if ((code == LSHIFTRT || code == ASHIFTRT)
7234 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7235 op_mode = GET_MODE (x);
7237 /* Truncate MASK to fit OP_MODE. */
7238 if (op_mode)
7239 mask &= GET_MODE_MASK (op_mode);
7241 /* When we have an arithmetic operation, or a shift whose count we
7242 do not know, we need to assume that all bits up to the highest-order
7243 bit in MASK will be needed. This is how we form such a mask. */
7244 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7245 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7246 else
7247 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7248 - 1);
7250 /* Determine what bits of X are guaranteed to be (non)zero. */
7251 nonzero = nonzero_bits (x, mode);
7253 /* If none of the bits in X are needed, return a zero. */
7254 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7255 x = const0_rtx;
7257 /* If X is a CONST_INT, return a new one. Do this here since the
7258 test below will fail. */
7259 if (GET_CODE (x) == CONST_INT)
7261 if (SCALAR_INT_MODE_P (mode))
7262 return gen_int_mode (INTVAL (x) & mask, mode);
7263 else
7265 x = GEN_INT (INTVAL (x) & mask);
7266 return gen_lowpart_common (mode, x);
7270 /* If X is narrower than MODE and we want all the bits in X's mode, just
7271 get X in the proper mode. */
7272 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7273 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7274 return gen_lowpart (mode, x);
7276 switch (code)
7278 case CLOBBER:
7279 /* If X is a (clobber (const_int)), return it since we know we are
7280 generating something that won't match. */
7281 return x;
7283 case SIGN_EXTEND:
7284 case ZERO_EXTEND:
7285 case ZERO_EXTRACT:
7286 case SIGN_EXTRACT:
7287 x = expand_compound_operation (x);
7288 if (GET_CODE (x) != code)
7289 return force_to_mode (x, mode, mask, next_select);
7290 break;
7292 case SUBREG:
7293 if (subreg_lowpart_p (x)
7294 /* We can ignore the effect of this SUBREG if it narrows the mode or
7295 if the constant masks to zero all the bits the mode doesn't
7296 have. */
7297 && ((GET_MODE_SIZE (GET_MODE (x))
7298 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7299 || (0 == (mask
7300 & GET_MODE_MASK (GET_MODE (x))
7301 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7302 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7303 break;
7305 case AND:
7306 /* If this is an AND with a constant, convert it into an AND
7307 whose constant is the AND of that constant with MASK. If it
7308 remains an AND of MASK, delete it since it is redundant. */
7310 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7312 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7313 mask & INTVAL (XEXP (x, 1)));
7315 /* If X is still an AND, see if it is an AND with a mask that
7316 is just some low-order bits. If so, and it is MASK, we don't
7317 need it. */
7319 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7320 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7321 == mask))
7322 x = XEXP (x, 0);
7324 /* If it remains an AND, try making another AND with the bits
7325 in the mode mask that aren't in MASK turned on. If the
7326 constant in the AND is wide enough, this might make a
7327 cheaper constant. */
7329 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7330 && GET_MODE_MASK (GET_MODE (x)) != mask
7331 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7333 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7334 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7335 int width = GET_MODE_BITSIZE (GET_MODE (x));
7336 rtx y;
7338 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7339 number, sign extend it. */
7340 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7341 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7342 cval |= (HOST_WIDE_INT) -1 << width;
7344 y = simplify_gen_binary (AND, GET_MODE (x),
7345 XEXP (x, 0), GEN_INT (cval));
7346 if (rtx_cost (y, SET) < rtx_cost (x, SET))
7347 x = y;
7350 break;
7353 goto binop;
7355 case PLUS:
7356 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7357 low-order bits (as in an alignment operation) and FOO is already
7358 aligned to that boundary, mask C1 to that boundary as well.
7359 This may eliminate that PLUS and, later, the AND. */
7362 unsigned int width = GET_MODE_BITSIZE (mode);
7363 unsigned HOST_WIDE_INT smask = mask;
7365 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7366 number, sign extend it. */
7368 if (width < HOST_BITS_PER_WIDE_INT
7369 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7370 smask |= (HOST_WIDE_INT) -1 << width;
7372 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7373 && exact_log2 (- smask) >= 0
7374 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7375 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7376 return force_to_mode (plus_constant (XEXP (x, 0),
7377 (INTVAL (XEXP (x, 1)) & smask)),
7378 mode, smask, next_select);
7381 /* ... fall through ... */
7383 case MULT:
7384 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7385 most significant bit in MASK since carries from those bits will
7386 affect the bits we are interested in. */
7387 mask = fuller_mask;
7388 goto binop;
7390 case MINUS:
7391 /* If X is (minus C Y) where C's least set bit is larger than any bit
7392 in the mask, then we may replace with (neg Y). */
7393 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7394 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7395 & -INTVAL (XEXP (x, 0))))
7396 > mask))
7398 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7399 GET_MODE (x));
7400 return force_to_mode (x, mode, mask, next_select);
7403 /* Similarly, if C contains every bit in the fuller_mask, then we may
7404 replace with (not Y). */
7405 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7406 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7407 == INTVAL (XEXP (x, 0))))
7409 x = simplify_gen_unary (NOT, GET_MODE (x),
7410 XEXP (x, 1), GET_MODE (x));
7411 return force_to_mode (x, mode, mask, next_select);
7414 mask = fuller_mask;
7415 goto binop;
7417 case IOR:
7418 case XOR:
7419 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7420 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7421 operation which may be a bitfield extraction. Ensure that the
7422 constant we form is not wider than the mode of X. */
7424 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7425 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7426 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7427 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7428 && GET_CODE (XEXP (x, 1)) == CONST_INT
7429 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7430 + floor_log2 (INTVAL (XEXP (x, 1))))
7431 < GET_MODE_BITSIZE (GET_MODE (x)))
7432 && (INTVAL (XEXP (x, 1))
7433 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7435 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7436 << INTVAL (XEXP (XEXP (x, 0), 1)));
7437 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7438 XEXP (XEXP (x, 0), 0), temp);
7439 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7440 XEXP (XEXP (x, 0), 1));
7441 return force_to_mode (x, mode, mask, next_select);
7444 binop:
7445 /* For most binary operations, just propagate into the operation and
7446 change the mode if we have an operation of that mode. */
7448 op0 = gen_lowpart_or_truncate (op_mode,
7449 force_to_mode (XEXP (x, 0), mode, mask,
7450 next_select));
7451 op1 = gen_lowpart_or_truncate (op_mode,
7452 force_to_mode (XEXP (x, 1), mode, mask,
7453 next_select));
7455 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7456 x = simplify_gen_binary (code, op_mode, op0, op1);
7457 break;
7459 case ASHIFT:
7460 /* For left shifts, do the same, but just for the first operand.
7461 However, we cannot do anything with shifts where we cannot
7462 guarantee that the counts are smaller than the size of the mode
7463 because such a count will have a different meaning in a
7464 wider mode. */
7466 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7467 && INTVAL (XEXP (x, 1)) >= 0
7468 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7469 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7470 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7471 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7472 break;
7474 /* If the shift count is a constant and we can do arithmetic in
7475 the mode of the shift, refine which bits we need. Otherwise, use the
7476 conservative form of the mask. */
7477 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7478 && INTVAL (XEXP (x, 1)) >= 0
7479 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7480 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7481 mask >>= INTVAL (XEXP (x, 1));
7482 else
7483 mask = fuller_mask;
7485 op0 = gen_lowpart_or_truncate (op_mode,
7486 force_to_mode (XEXP (x, 0), op_mode,
7487 mask, next_select));
7489 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7490 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7491 break;
7493 case LSHIFTRT:
7494 /* Here we can only do something if the shift count is a constant,
7495 this shift constant is valid for the host, and we can do arithmetic
7496 in OP_MODE. */
7498 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7499 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7500 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7502 rtx inner = XEXP (x, 0);
7503 unsigned HOST_WIDE_INT inner_mask;
7505 /* Select the mask of the bits we need for the shift operand. */
7506 inner_mask = mask << INTVAL (XEXP (x, 1));
7508 /* We can only change the mode of the shift if we can do arithmetic
7509 in the mode of the shift and INNER_MASK is no wider than the
7510 width of X's mode. */
7511 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7512 op_mode = GET_MODE (x);
7514 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7516 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7517 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7520 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7521 shift and AND produces only copies of the sign bit (C2 is one less
7522 than a power of two), we can do this with just a shift. */
7524 if (GET_CODE (x) == LSHIFTRT
7525 && GET_CODE (XEXP (x, 1)) == CONST_INT
7526 /* The shift puts one of the sign bit copies in the least significant
7527 bit. */
7528 && ((INTVAL (XEXP (x, 1))
7529 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7530 >= GET_MODE_BITSIZE (GET_MODE (x)))
7531 && exact_log2 (mask + 1) >= 0
7532 /* Number of bits left after the shift must be more than the mask
7533 needs. */
7534 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7535 <= GET_MODE_BITSIZE (GET_MODE (x)))
7536 /* Must be more sign bit copies than the mask needs. */
7537 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7538 >= exact_log2 (mask + 1)))
7539 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7540 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7541 - exact_log2 (mask + 1)));
7543 goto shiftrt;
7545 case ASHIFTRT:
7546 /* If we are just looking for the sign bit, we don't need this shift at
7547 all, even if it has a variable count. */
7548 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7549 && (mask == ((unsigned HOST_WIDE_INT) 1
7550 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7551 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7553 /* If this is a shift by a constant, get a mask that contains those bits
7554 that are not copies of the sign bit. We then have two cases: If
7555 MASK only includes those bits, this can be a logical shift, which may
7556 allow simplifications. If MASK is a single-bit field not within
7557 those bits, we are requesting a copy of the sign bit and hence can
7558 shift the sign bit to the appropriate location. */
7560 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7561 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7563 int i;
7565 /* If the considered data is wider than HOST_WIDE_INT, we can't
7566 represent a mask for all its bits in a single scalar.
7567 But we only care about the lower bits, so calculate these. */
7569 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7571 nonzero = ~(HOST_WIDE_INT) 0;
7573 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7574 is the number of bits a full-width mask would have set.
7575 We need only shift if these are fewer than nonzero can
7576 hold. If not, we must keep all bits set in nonzero. */
7578 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7579 < HOST_BITS_PER_WIDE_INT)
7580 nonzero >>= INTVAL (XEXP (x, 1))
7581 + HOST_BITS_PER_WIDE_INT
7582 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7584 else
7586 nonzero = GET_MODE_MASK (GET_MODE (x));
7587 nonzero >>= INTVAL (XEXP (x, 1));
7590 if ((mask & ~nonzero) == 0)
7592 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7593 XEXP (x, 0), INTVAL (XEXP (x, 1)));
7594 if (GET_CODE (x) != ASHIFTRT)
7595 return force_to_mode (x, mode, mask, next_select);
7598 else if ((i = exact_log2 (mask)) >= 0)
7600 x = simplify_shift_const
7601 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7602 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7604 if (GET_CODE (x) != ASHIFTRT)
7605 return force_to_mode (x, mode, mask, next_select);
7609 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7610 even if the shift count isn't a constant. */
7611 if (mask == 1)
7612 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7613 XEXP (x, 0), XEXP (x, 1));
7615 shiftrt:
7617 /* If this is a zero- or sign-extension operation that just affects bits
7618 we don't care about, remove it. Be sure the call above returned
7619 something that is still a shift. */
7621 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7622 && GET_CODE (XEXP (x, 1)) == CONST_INT
7623 && INTVAL (XEXP (x, 1)) >= 0
7624 && (INTVAL (XEXP (x, 1))
7625 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7626 && GET_CODE (XEXP (x, 0)) == ASHIFT
7627 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7628 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7629 next_select);
7631 break;
7633 case ROTATE:
7634 case ROTATERT:
7635 /* If the shift count is constant and we can do computations
7636 in the mode of X, compute where the bits we care about are.
7637 Otherwise, we can't do anything. Don't change the mode of
7638 the shift or propagate MODE into the shift, though. */
7639 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7640 && INTVAL (XEXP (x, 1)) >= 0)
7642 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7643 GET_MODE (x), GEN_INT (mask),
7644 XEXP (x, 1));
7645 if (temp && GET_CODE (temp) == CONST_INT)
7646 SUBST (XEXP (x, 0),
7647 force_to_mode (XEXP (x, 0), GET_MODE (x),
7648 INTVAL (temp), next_select));
7650 break;
7652 case NEG:
7653 /* If we just want the low-order bit, the NEG isn't needed since it
7654 won't change the low-order bit. */
7655 if (mask == 1)
7656 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7658 /* We need any bits less significant than the most significant bit in
7659 MASK since carries from those bits will affect the bits we are
7660 interested in. */
7661 mask = fuller_mask;
7662 goto unop;
7664 case NOT:
7665 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7666 same as the XOR case above. Ensure that the constant we form is not
7667 wider than the mode of X. */
7669 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7670 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7671 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7672 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7673 < GET_MODE_BITSIZE (GET_MODE (x)))
7674 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7676 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7677 GET_MODE (x));
7678 temp = simplify_gen_binary (XOR, GET_MODE (x),
7679 XEXP (XEXP (x, 0), 0), temp);
7680 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7681 temp, XEXP (XEXP (x, 0), 1));
7683 return force_to_mode (x, mode, mask, next_select);
7686 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7687 use the full mask inside the NOT. */
7688 mask = fuller_mask;
7690 unop:
7691 op0 = gen_lowpart_or_truncate (op_mode,
7692 force_to_mode (XEXP (x, 0), mode, mask,
7693 next_select));
7694 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7695 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7696 break;
7698 case NE:
7699 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7700 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7701 which is equal to STORE_FLAG_VALUE. */
7702 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7703 && GET_MODE (XEXP (x, 0)) == mode
7704 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7705 && (nonzero_bits (XEXP (x, 0), mode)
7706 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7707 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7709 break;
7711 case IF_THEN_ELSE:
7712 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7713 written in a narrower mode. We play it safe and do not do so. */
7715 SUBST (XEXP (x, 1),
7716 gen_lowpart_or_truncate (GET_MODE (x),
7717 force_to_mode (XEXP (x, 1), mode,
7718 mask, next_select)));
7719 SUBST (XEXP (x, 2),
7720 gen_lowpart_or_truncate (GET_MODE (x),
7721 force_to_mode (XEXP (x, 2), mode,
7722 mask, next_select)));
7723 break;
7725 default:
7726 break;
7729 /* Ensure we return a value of the proper mode. */
7730 return gen_lowpart_or_truncate (mode, x);
7733 /* Return nonzero if X is an expression that has one of two values depending on
7734 whether some other value is zero or nonzero. In that case, we return the
7735 value that is being tested, *PTRUE is set to the value if the rtx being
7736 returned has a nonzero value, and *PFALSE is set to the other alternative.
7738 If we return zero, we set *PTRUE and *PFALSE to X. */
7740 static rtx
7741 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7743 enum machine_mode mode = GET_MODE (x);
7744 enum rtx_code code = GET_CODE (x);
7745 rtx cond0, cond1, true0, true1, false0, false1;
7746 unsigned HOST_WIDE_INT nz;
7748 /* If we are comparing a value against zero, we are done. */
7749 if ((code == NE || code == EQ)
7750 && XEXP (x, 1) == const0_rtx)
7752 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7753 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7754 return XEXP (x, 0);
7757 /* If this is a unary operation whose operand has one of two values, apply
7758 our opcode to compute those values. */
7759 else if (UNARY_P (x)
7760 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7762 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7763 *pfalse = simplify_gen_unary (code, mode, false0,
7764 GET_MODE (XEXP (x, 0)));
7765 return cond0;
7768 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7769 make can't possibly match and would suppress other optimizations. */
7770 else if (code == COMPARE)
7773 /* If this is a binary operation, see if either side has only one of two
7774 values. If either one does or if both do and they are conditional on
7775 the same value, compute the new true and false values. */
7776 else if (BINARY_P (x))
7778 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7779 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7781 if ((cond0 != 0 || cond1 != 0)
7782 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7784 /* If if_then_else_cond returned zero, then true/false are the
7785 same rtl. We must copy one of them to prevent invalid rtl
7786 sharing. */
7787 if (cond0 == 0)
7788 true0 = copy_rtx (true0);
7789 else if (cond1 == 0)
7790 true1 = copy_rtx (true1);
7792 if (COMPARISON_P (x))
7794 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7795 true0, true1);
7796 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7797 false0, false1);
7799 else
7801 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7802 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7805 return cond0 ? cond0 : cond1;
7808 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7809 operands is zero when the other is nonzero, and vice-versa,
7810 and STORE_FLAG_VALUE is 1 or -1. */
7812 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7813 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7814 || code == UMAX)
7815 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7817 rtx op0 = XEXP (XEXP (x, 0), 1);
7818 rtx op1 = XEXP (XEXP (x, 1), 1);
7820 cond0 = XEXP (XEXP (x, 0), 0);
7821 cond1 = XEXP (XEXP (x, 1), 0);
7823 if (COMPARISON_P (cond0)
7824 && COMPARISON_P (cond1)
7825 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7826 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7827 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7828 || ((swap_condition (GET_CODE (cond0))
7829 == reversed_comparison_code (cond1, NULL))
7830 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7831 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7832 && ! side_effects_p (x))
7834 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7835 *pfalse = simplify_gen_binary (MULT, mode,
7836 (code == MINUS
7837 ? simplify_gen_unary (NEG, mode,
7838 op1, mode)
7839 : op1),
7840 const_true_rtx);
7841 return cond0;
7845 /* Similarly for MULT, AND and UMIN, except that for these the result
7846 is always zero. */
7847 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7848 && (code == MULT || code == AND || code == UMIN)
7849 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7851 cond0 = XEXP (XEXP (x, 0), 0);
7852 cond1 = XEXP (XEXP (x, 1), 0);
7854 if (COMPARISON_P (cond0)
7855 && COMPARISON_P (cond1)
7856 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7857 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7858 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7859 || ((swap_condition (GET_CODE (cond0))
7860 == reversed_comparison_code (cond1, NULL))
7861 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7862 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7863 && ! side_effects_p (x))
7865 *ptrue = *pfalse = const0_rtx;
7866 return cond0;
7871 else if (code == IF_THEN_ELSE)
7873 /* If we have IF_THEN_ELSE already, extract the condition and
7874 canonicalize it if it is NE or EQ. */
7875 cond0 = XEXP (x, 0);
7876 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7877 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7878 return XEXP (cond0, 0);
7879 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7881 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7882 return XEXP (cond0, 0);
7884 else
7885 return cond0;
7888 /* If X is a SUBREG, we can narrow both the true and false values
7889 if the inner expression, if there is a condition. */
7890 else if (code == SUBREG
7891 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7892 &true0, &false0)))
7894 true0 = simplify_gen_subreg (mode, true0,
7895 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7896 false0 = simplify_gen_subreg (mode, false0,
7897 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7898 if (true0 && false0)
7900 *ptrue = true0;
7901 *pfalse = false0;
7902 return cond0;
7906 /* If X is a constant, this isn't special and will cause confusions
7907 if we treat it as such. Likewise if it is equivalent to a constant. */
7908 else if (CONSTANT_P (x)
7909 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7912 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7913 will be least confusing to the rest of the compiler. */
7914 else if (mode == BImode)
7916 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7917 return x;
7920 /* If X is known to be either 0 or -1, those are the true and
7921 false values when testing X. */
7922 else if (x == constm1_rtx || x == const0_rtx
7923 || (mode != VOIDmode
7924 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7926 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7927 return x;
7930 /* Likewise for 0 or a single bit. */
7931 else if (SCALAR_INT_MODE_P (mode)
7932 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7933 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7935 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7936 return x;
7939 /* Otherwise fail; show no condition with true and false values the same. */
7940 *ptrue = *pfalse = x;
7941 return 0;
7944 /* Return the value of expression X given the fact that condition COND
7945 is known to be true when applied to REG as its first operand and VAL
7946 as its second. X is known to not be shared and so can be modified in
7947 place.
7949 We only handle the simplest cases, and specifically those cases that
7950 arise with IF_THEN_ELSE expressions. */
7952 static rtx
7953 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7955 enum rtx_code code = GET_CODE (x);
7956 rtx temp;
7957 const char *fmt;
7958 int i, j;
7960 if (side_effects_p (x))
7961 return x;
7963 /* If either operand of the condition is a floating point value,
7964 then we have to avoid collapsing an EQ comparison. */
7965 if (cond == EQ
7966 && rtx_equal_p (x, reg)
7967 && ! FLOAT_MODE_P (GET_MODE (x))
7968 && ! FLOAT_MODE_P (GET_MODE (val)))
7969 return val;
7971 if (cond == UNEQ && rtx_equal_p (x, reg))
7972 return val;
7974 /* If X is (abs REG) and we know something about REG's relationship
7975 with zero, we may be able to simplify this. */
7977 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7978 switch (cond)
7980 case GE: case GT: case EQ:
7981 return XEXP (x, 0);
7982 case LT: case LE:
7983 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7984 XEXP (x, 0),
7985 GET_MODE (XEXP (x, 0)));
7986 default:
7987 break;
7990 /* The only other cases we handle are MIN, MAX, and comparisons if the
7991 operands are the same as REG and VAL. */
7993 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7995 if (rtx_equal_p (XEXP (x, 0), val))
7996 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7998 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8000 if (COMPARISON_P (x))
8002 if (comparison_dominates_p (cond, code))
8003 return const_true_rtx;
8005 code = reversed_comparison_code (x, NULL);
8006 if (code != UNKNOWN
8007 && comparison_dominates_p (cond, code))
8008 return const0_rtx;
8009 else
8010 return x;
8012 else if (code == SMAX || code == SMIN
8013 || code == UMIN || code == UMAX)
8015 int unsignedp = (code == UMIN || code == UMAX);
8017 /* Do not reverse the condition when it is NE or EQ.
8018 This is because we cannot conclude anything about
8019 the value of 'SMAX (x, y)' when x is not equal to y,
8020 but we can when x equals y. */
8021 if ((code == SMAX || code == UMAX)
8022 && ! (cond == EQ || cond == NE))
8023 cond = reverse_condition (cond);
8025 switch (cond)
8027 case GE: case GT:
8028 return unsignedp ? x : XEXP (x, 1);
8029 case LE: case LT:
8030 return unsignedp ? x : XEXP (x, 0);
8031 case GEU: case GTU:
8032 return unsignedp ? XEXP (x, 1) : x;
8033 case LEU: case LTU:
8034 return unsignedp ? XEXP (x, 0) : x;
8035 default:
8036 break;
8041 else if (code == SUBREG)
8043 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8044 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
8046 if (SUBREG_REG (x) != r)
8048 /* We must simplify subreg here, before we lose track of the
8049 original inner_mode. */
8050 new = simplify_subreg (GET_MODE (x), r,
8051 inner_mode, SUBREG_BYTE (x));
8052 if (new)
8053 return new;
8054 else
8055 SUBST (SUBREG_REG (x), r);
8058 return x;
8060 /* We don't have to handle SIGN_EXTEND here, because even in the
8061 case of replacing something with a modeless CONST_INT, a
8062 CONST_INT is already (supposed to be) a valid sign extension for
8063 its narrower mode, which implies it's already properly
8064 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8065 story is different. */
8066 else if (code == ZERO_EXTEND)
8068 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8069 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
8071 if (XEXP (x, 0) != r)
8073 /* We must simplify the zero_extend here, before we lose
8074 track of the original inner_mode. */
8075 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8076 r, inner_mode);
8077 if (new)
8078 return new;
8079 else
8080 SUBST (XEXP (x, 0), r);
8083 return x;
8086 fmt = GET_RTX_FORMAT (code);
8087 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8089 if (fmt[i] == 'e')
8090 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8091 else if (fmt[i] == 'E')
8092 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8093 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8094 cond, reg, val));
8097 return x;
8100 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8101 assignment as a field assignment. */
8103 static int
8104 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8106 if (x == y || rtx_equal_p (x, y))
8107 return 1;
8109 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8110 return 0;
8112 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8113 Note that all SUBREGs of MEM are paradoxical; otherwise they
8114 would have been rewritten. */
8115 if (MEM_P (x) && GET_CODE (y) == SUBREG
8116 && MEM_P (SUBREG_REG (y))
8117 && rtx_equal_p (SUBREG_REG (y),
8118 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8119 return 1;
8121 if (MEM_P (y) && GET_CODE (x) == SUBREG
8122 && MEM_P (SUBREG_REG (x))
8123 && rtx_equal_p (SUBREG_REG (x),
8124 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8125 return 1;
8127 /* We used to see if get_last_value of X and Y were the same but that's
8128 not correct. In one direction, we'll cause the assignment to have
8129 the wrong destination and in the case, we'll import a register into this
8130 insn that might have already have been dead. So fail if none of the
8131 above cases are true. */
8132 return 0;
8135 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8136 Return that assignment if so.
8138 We only handle the most common cases. */
8140 static rtx
8141 make_field_assignment (rtx x)
8143 rtx dest = SET_DEST (x);
8144 rtx src = SET_SRC (x);
8145 rtx assign;
8146 rtx rhs, lhs;
8147 HOST_WIDE_INT c1;
8148 HOST_WIDE_INT pos;
8149 unsigned HOST_WIDE_INT len;
8150 rtx other;
8151 enum machine_mode mode;
8153 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8154 a clear of a one-bit field. We will have changed it to
8155 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8156 for a SUBREG. */
8158 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8159 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
8160 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8161 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8163 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8164 1, 1, 1, 0);
8165 if (assign != 0)
8166 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8167 return x;
8170 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8171 && subreg_lowpart_p (XEXP (src, 0))
8172 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8173 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8174 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8175 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
8176 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8177 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8179 assign = make_extraction (VOIDmode, dest, 0,
8180 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8181 1, 1, 1, 0);
8182 if (assign != 0)
8183 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8184 return x;
8187 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8188 one-bit field. */
8189 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8190 && XEXP (XEXP (src, 0), 0) == const1_rtx
8191 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8193 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8194 1, 1, 1, 0);
8195 if (assign != 0)
8196 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8197 return x;
8200 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8201 SRC is an AND with all bits of that field set, then we can discard
8202 the AND. */
8203 if (GET_CODE (dest) == ZERO_EXTRACT
8204 && GET_CODE (XEXP (dest, 1)) == CONST_INT
8205 && GET_CODE (src) == AND
8206 && GET_CODE (XEXP (src, 1)) == CONST_INT)
8208 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8209 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8210 unsigned HOST_WIDE_INT ze_mask;
8212 if (width >= HOST_BITS_PER_WIDE_INT)
8213 ze_mask = -1;
8214 else
8215 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8217 /* Complete overlap. We can remove the source AND. */
8218 if ((and_mask & ze_mask) == ze_mask)
8219 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8221 /* Partial overlap. We can reduce the source AND. */
8222 if ((and_mask & ze_mask) != and_mask)
8224 mode = GET_MODE (src);
8225 src = gen_rtx_AND (mode, XEXP (src, 0),
8226 gen_int_mode (and_mask & ze_mask, mode));
8227 return gen_rtx_SET (VOIDmode, dest, src);
8231 /* The other case we handle is assignments into a constant-position
8232 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
8233 a mask that has all one bits except for a group of zero bits and
8234 OTHER is known to have zeros where C1 has ones, this is such an
8235 assignment. Compute the position and length from C1. Shift OTHER
8236 to the appropriate position, force it to the required mode, and
8237 make the extraction. Check for the AND in both operands. */
8239 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8240 return x;
8242 rhs = expand_compound_operation (XEXP (src, 0));
8243 lhs = expand_compound_operation (XEXP (src, 1));
8245 if (GET_CODE (rhs) == AND
8246 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
8247 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8248 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8249 else if (GET_CODE (lhs) == AND
8250 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
8251 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8252 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8253 else
8254 return x;
8256 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8257 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8258 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8259 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8260 return x;
8262 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8263 if (assign == 0)
8264 return x;
8266 /* The mode to use for the source is the mode of the assignment, or of
8267 what is inside a possible STRICT_LOW_PART. */
8268 mode = (GET_CODE (assign) == STRICT_LOW_PART
8269 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8271 /* Shift OTHER right POS places and make it the source, restricting it
8272 to the proper length and mode. */
8274 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8275 GET_MODE (src),
8276 other, pos),
8277 dest);
8278 src = force_to_mode (src, mode,
8279 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8280 ? ~(unsigned HOST_WIDE_INT) 0
8281 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8284 /* If SRC is masked by an AND that does not make a difference in
8285 the value being stored, strip it. */
8286 if (GET_CODE (assign) == ZERO_EXTRACT
8287 && GET_CODE (XEXP (assign, 1)) == CONST_INT
8288 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8289 && GET_CODE (src) == AND
8290 && GET_CODE (XEXP (src, 1)) == CONST_INT
8291 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8292 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8293 src = XEXP (src, 0);
8295 return gen_rtx_SET (VOIDmode, assign, src);
8298 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8299 if so. */
8301 static rtx
8302 apply_distributive_law (rtx x)
8304 enum rtx_code code = GET_CODE (x);
8305 enum rtx_code inner_code;
8306 rtx lhs, rhs, other;
8307 rtx tem;
8309 /* Distributivity is not true for floating point as it can change the
8310 value. So we don't do it unless -funsafe-math-optimizations. */
8311 if (FLOAT_MODE_P (GET_MODE (x))
8312 && ! flag_unsafe_math_optimizations)
8313 return x;
8315 /* The outer operation can only be one of the following: */
8316 if (code != IOR && code != AND && code != XOR
8317 && code != PLUS && code != MINUS)
8318 return x;
8320 lhs = XEXP (x, 0);
8321 rhs = XEXP (x, 1);
8323 /* If either operand is a primitive we can't do anything, so get out
8324 fast. */
8325 if (OBJECT_P (lhs) || OBJECT_P (rhs))
8326 return x;
8328 lhs = expand_compound_operation (lhs);
8329 rhs = expand_compound_operation (rhs);
8330 inner_code = GET_CODE (lhs);
8331 if (inner_code != GET_CODE (rhs))
8332 return x;
8334 /* See if the inner and outer operations distribute. */
8335 switch (inner_code)
8337 case LSHIFTRT:
8338 case ASHIFTRT:
8339 case AND:
8340 case IOR:
8341 /* These all distribute except over PLUS. */
8342 if (code == PLUS || code == MINUS)
8343 return x;
8344 break;
8346 case MULT:
8347 if (code != PLUS && code != MINUS)
8348 return x;
8349 break;
8351 case ASHIFT:
8352 /* This is also a multiply, so it distributes over everything. */
8353 break;
8355 case SUBREG:
8356 /* Non-paradoxical SUBREGs distributes over all operations,
8357 provided the inner modes and byte offsets are the same, this
8358 is an extraction of a low-order part, we don't convert an fp
8359 operation to int or vice versa, this is not a vector mode,
8360 and we would not be converting a single-word operation into a
8361 multi-word operation. The latter test is not required, but
8362 it prevents generating unneeded multi-word operations. Some
8363 of the previous tests are redundant given the latter test,
8364 but are retained because they are required for correctness.
8366 We produce the result slightly differently in this case. */
8368 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8369 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8370 || ! subreg_lowpart_p (lhs)
8371 || (GET_MODE_CLASS (GET_MODE (lhs))
8372 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8373 || (GET_MODE_SIZE (GET_MODE (lhs))
8374 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8375 || VECTOR_MODE_P (GET_MODE (lhs))
8376 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8377 /* Result might need to be truncated. Don't change mode if
8378 explicit truncation is needed. */
8379 || !TRULY_NOOP_TRUNCATION
8380 (GET_MODE_BITSIZE (GET_MODE (x)),
8381 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8382 return x;
8384 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8385 SUBREG_REG (lhs), SUBREG_REG (rhs));
8386 return gen_lowpart (GET_MODE (x), tem);
8388 default:
8389 return x;
8392 /* Set LHS and RHS to the inner operands (A and B in the example
8393 above) and set OTHER to the common operand (C in the example).
8394 There is only one way to do this unless the inner operation is
8395 commutative. */
8396 if (COMMUTATIVE_ARITH_P (lhs)
8397 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8398 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8399 else if (COMMUTATIVE_ARITH_P (lhs)
8400 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8401 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8402 else if (COMMUTATIVE_ARITH_P (lhs)
8403 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8404 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8405 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8406 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8407 else
8408 return x;
8410 /* Form the new inner operation, seeing if it simplifies first. */
8411 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8413 /* There is one exception to the general way of distributing:
8414 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8415 if (code == XOR && inner_code == IOR)
8417 inner_code = AND;
8418 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8421 /* We may be able to continuing distributing the result, so call
8422 ourselves recursively on the inner operation before forming the
8423 outer operation, which we return. */
8424 return simplify_gen_binary (inner_code, GET_MODE (x),
8425 apply_distributive_law (tem), other);
8428 /* See if X is of the form (* (+ A B) C), and if so convert to
8429 (+ (* A C) (* B C)) and try to simplify.
8431 Most of the time, this results in no change. However, if some of
8432 the operands are the same or inverses of each other, simplifications
8433 will result.
8435 For example, (and (ior A B) (not B)) can occur as the result of
8436 expanding a bit field assignment. When we apply the distributive
8437 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8438 which then simplifies to (and (A (not B))).
8440 Note that no checks happen on the validity of applying the inverse
8441 distributive law. This is pointless since we can do it in the
8442 few places where this routine is called.
8444 N is the index of the term that is decomposed (the arithmetic operation,
8445 i.e. (+ A B) in the first example above). !N is the index of the term that
8446 is distributed, i.e. of C in the first example above. */
8447 static rtx
8448 distribute_and_simplify_rtx (rtx x, int n)
8450 enum machine_mode mode;
8451 enum rtx_code outer_code, inner_code;
8452 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8454 decomposed = XEXP (x, n);
8455 if (!ARITHMETIC_P (decomposed))
8456 return NULL_RTX;
8458 mode = GET_MODE (x);
8459 outer_code = GET_CODE (x);
8460 distributed = XEXP (x, !n);
8462 inner_code = GET_CODE (decomposed);
8463 inner_op0 = XEXP (decomposed, 0);
8464 inner_op1 = XEXP (decomposed, 1);
8466 /* Special case (and (xor B C) (not A)), which is equivalent to
8467 (xor (ior A B) (ior A C)) */
8468 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8470 distributed = XEXP (distributed, 0);
8471 outer_code = IOR;
8474 if (n == 0)
8476 /* Distribute the second term. */
8477 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8478 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8480 else
8482 /* Distribute the first term. */
8483 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8484 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8487 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8488 new_op0, new_op1));
8489 if (GET_CODE (tmp) != outer_code
8490 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8491 return tmp;
8493 return NULL_RTX;
8496 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8497 in MODE. Return an equivalent form, if different from (and VAROP
8498 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
8500 static rtx
8501 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8502 unsigned HOST_WIDE_INT constop)
8504 unsigned HOST_WIDE_INT nonzero;
8505 unsigned HOST_WIDE_INT orig_constop;
8506 rtx orig_varop;
8507 int i;
8509 orig_varop = varop;
8510 orig_constop = constop;
8511 if (GET_CODE (varop) == CLOBBER)
8512 return NULL_RTX;
8514 /* Simplify VAROP knowing that we will be only looking at some of the
8515 bits in it.
8517 Note by passing in CONSTOP, we guarantee that the bits not set in
8518 CONSTOP are not significant and will never be examined. We must
8519 ensure that is the case by explicitly masking out those bits
8520 before returning. */
8521 varop = force_to_mode (varop, mode, constop, 0);
8523 /* If VAROP is a CLOBBER, we will fail so return it. */
8524 if (GET_CODE (varop) == CLOBBER)
8525 return varop;
8527 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8528 to VAROP and return the new constant. */
8529 if (GET_CODE (varop) == CONST_INT)
8530 return gen_int_mode (INTVAL (varop) & constop, mode);
8532 /* See what bits may be nonzero in VAROP. Unlike the general case of
8533 a call to nonzero_bits, here we don't care about bits outside
8534 MODE. */
8536 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8538 /* Turn off all bits in the constant that are known to already be zero.
8539 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8540 which is tested below. */
8542 constop &= nonzero;
8544 /* If we don't have any bits left, return zero. */
8545 if (constop == 0)
8546 return const0_rtx;
8548 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8549 a power of two, we can replace this with an ASHIFT. */
8550 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8551 && (i = exact_log2 (constop)) >= 0)
8552 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8554 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8555 or XOR, then try to apply the distributive law. This may eliminate
8556 operations if either branch can be simplified because of the AND.
8557 It may also make some cases more complex, but those cases probably
8558 won't match a pattern either with or without this. */
8560 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8561 return
8562 gen_lowpart
8563 (mode,
8564 apply_distributive_law
8565 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8566 simplify_and_const_int (NULL_RTX,
8567 GET_MODE (varop),
8568 XEXP (varop, 0),
8569 constop),
8570 simplify_and_const_int (NULL_RTX,
8571 GET_MODE (varop),
8572 XEXP (varop, 1),
8573 constop))));
8575 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8576 the AND and see if one of the operands simplifies to zero. If so, we
8577 may eliminate it. */
8579 if (GET_CODE (varop) == PLUS
8580 && exact_log2 (constop + 1) >= 0)
8582 rtx o0, o1;
8584 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8585 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8586 if (o0 == const0_rtx)
8587 return o1;
8588 if (o1 == const0_rtx)
8589 return o0;
8592 /* Make a SUBREG if necessary. If we can't make it, fail. */
8593 varop = gen_lowpart (mode, varop);
8594 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8595 return NULL_RTX;
8597 /* If we are only masking insignificant bits, return VAROP. */
8598 if (constop == nonzero)
8599 return varop;
8601 if (varop == orig_varop && constop == orig_constop)
8602 return NULL_RTX;
8604 /* Otherwise, return an AND. */
8605 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8609 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8610 in MODE.
8612 Return an equivalent form, if different from X. Otherwise, return X. If
8613 X is zero, we are to always construct the equivalent form. */
8615 static rtx
8616 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8617 unsigned HOST_WIDE_INT constop)
8619 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8620 if (tem)
8621 return tem;
8623 if (!x)
8624 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8625 gen_int_mode (constop, mode));
8626 if (GET_MODE (x) != mode)
8627 x = gen_lowpart (mode, x);
8628 return x;
8631 /* Given a REG, X, compute which bits in X can be nonzero.
8632 We don't care about bits outside of those defined in MODE.
8634 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8635 a shift, AND, or zero_extract, we can do better. */
8637 static rtx
8638 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8639 const_rtx known_x ATTRIBUTE_UNUSED,
8640 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8641 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8642 unsigned HOST_WIDE_INT *nonzero)
8644 rtx tem;
8645 reg_stat_type *rsp;
8647 /* If X is a register whose nonzero bits value is current, use it.
8648 Otherwise, if X is a register whose value we can find, use that
8649 value. Otherwise, use the previously-computed global nonzero bits
8650 for this register. */
8652 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8653 if (rsp->last_set_value != 0
8654 && (rsp->last_set_mode == mode
8655 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8656 && GET_MODE_CLASS (mode) == MODE_INT))
8657 && ((rsp->last_set_label >= label_tick_ebb_start
8658 && rsp->last_set_label < label_tick)
8659 || (rsp->last_set_label == label_tick
8660 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8661 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8662 && REG_N_SETS (REGNO (x)) == 1
8663 && !REGNO_REG_SET_P
8664 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8666 *nonzero &= rsp->last_set_nonzero_bits;
8667 return NULL;
8670 tem = get_last_value (x);
8672 if (tem)
8674 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8675 /* If X is narrower than MODE and TEM is a non-negative
8676 constant that would appear negative in the mode of X,
8677 sign-extend it for use in reg_nonzero_bits because some
8678 machines (maybe most) will actually do the sign-extension
8679 and this is the conservative approach.
8681 ??? For 2.5, try to tighten up the MD files in this regard
8682 instead of this kludge. */
8684 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8685 && GET_CODE (tem) == CONST_INT
8686 && INTVAL (tem) > 0
8687 && 0 != (INTVAL (tem)
8688 & ((HOST_WIDE_INT) 1
8689 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8690 tem = GEN_INT (INTVAL (tem)
8691 | ((HOST_WIDE_INT) (-1)
8692 << GET_MODE_BITSIZE (GET_MODE (x))));
8693 #endif
8694 return tem;
8696 else if (nonzero_sign_valid && rsp->nonzero_bits)
8698 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
8700 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8701 /* We don't know anything about the upper bits. */
8702 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8703 *nonzero &= mask;
8706 return NULL;
8709 /* Return the number of bits at the high-order end of X that are known to
8710 be equal to the sign bit. X will be used in mode MODE; if MODE is
8711 VOIDmode, X will be used in its own mode. The returned value will always
8712 be between 1 and the number of bits in MODE. */
8714 static rtx
8715 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
8716 const_rtx known_x ATTRIBUTE_UNUSED,
8717 enum machine_mode known_mode
8718 ATTRIBUTE_UNUSED,
8719 unsigned int known_ret ATTRIBUTE_UNUSED,
8720 unsigned int *result)
8722 rtx tem;
8723 reg_stat_type *rsp;
8725 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8726 if (rsp->last_set_value != 0
8727 && rsp->last_set_mode == mode
8728 && ((rsp->last_set_label >= label_tick_ebb_start
8729 && rsp->last_set_label < label_tick)
8730 || (rsp->last_set_label == label_tick
8731 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8732 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8733 && REG_N_SETS (REGNO (x)) == 1
8734 && !REGNO_REG_SET_P
8735 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8737 *result = rsp->last_set_sign_bit_copies;
8738 return NULL;
8741 tem = get_last_value (x);
8742 if (tem != 0)
8743 return tem;
8745 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
8746 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8747 *result = rsp->sign_bit_copies;
8749 return NULL;
8752 /* Return the number of "extended" bits there are in X, when interpreted
8753 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8754 unsigned quantities, this is the number of high-order zero bits.
8755 For signed quantities, this is the number of copies of the sign bit
8756 minus 1. In both case, this function returns the number of "spare"
8757 bits. For example, if two quantities for which this function returns
8758 at least 1 are added, the addition is known not to overflow.
8760 This function will always return 0 unless called during combine, which
8761 implies that it must be called from a define_split. */
8763 unsigned int
8764 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
8766 if (nonzero_sign_valid == 0)
8767 return 0;
8769 return (unsignedp
8770 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8771 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8772 - floor_log2 (nonzero_bits (x, mode)))
8773 : 0)
8774 : num_sign_bit_copies (x, mode) - 1);
8777 /* This function is called from `simplify_shift_const' to merge two
8778 outer operations. Specifically, we have already found that we need
8779 to perform operation *POP0 with constant *PCONST0 at the outermost
8780 position. We would now like to also perform OP1 with constant CONST1
8781 (with *POP0 being done last).
8783 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8784 the resulting operation. *PCOMP_P is set to 1 if we would need to
8785 complement the innermost operand, otherwise it is unchanged.
8787 MODE is the mode in which the operation will be done. No bits outside
8788 the width of this mode matter. It is assumed that the width of this mode
8789 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8791 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8792 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8793 result is simply *PCONST0.
8795 If the resulting operation cannot be expressed as one operation, we
8796 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8798 static int
8799 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)
8801 enum rtx_code op0 = *pop0;
8802 HOST_WIDE_INT const0 = *pconst0;
8804 const0 &= GET_MODE_MASK (mode);
8805 const1 &= GET_MODE_MASK (mode);
8807 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8808 if (op0 == AND)
8809 const1 &= const0;
8811 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8812 if OP0 is SET. */
8814 if (op1 == UNKNOWN || op0 == SET)
8815 return 1;
8817 else if (op0 == UNKNOWN)
8818 op0 = op1, const0 = const1;
8820 else if (op0 == op1)
8822 switch (op0)
8824 case AND:
8825 const0 &= const1;
8826 break;
8827 case IOR:
8828 const0 |= const1;
8829 break;
8830 case XOR:
8831 const0 ^= const1;
8832 break;
8833 case PLUS:
8834 const0 += const1;
8835 break;
8836 case NEG:
8837 op0 = UNKNOWN;
8838 break;
8839 default:
8840 break;
8844 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8845 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8846 return 0;
8848 /* If the two constants aren't the same, we can't do anything. The
8849 remaining six cases can all be done. */
8850 else if (const0 != const1)
8851 return 0;
8853 else
8854 switch (op0)
8856 case IOR:
8857 if (op1 == AND)
8858 /* (a & b) | b == b */
8859 op0 = SET;
8860 else /* op1 == XOR */
8861 /* (a ^ b) | b == a | b */
8863 break;
8865 case XOR:
8866 if (op1 == AND)
8867 /* (a & b) ^ b == (~a) & b */
8868 op0 = AND, *pcomp_p = 1;
8869 else /* op1 == IOR */
8870 /* (a | b) ^ b == a & ~b */
8871 op0 = AND, const0 = ~const0;
8872 break;
8874 case AND:
8875 if (op1 == IOR)
8876 /* (a | b) & b == b */
8877 op0 = SET;
8878 else /* op1 == XOR */
8879 /* (a ^ b) & b) == (~a) & b */
8880 *pcomp_p = 1;
8881 break;
8882 default:
8883 break;
8886 /* Check for NO-OP cases. */
8887 const0 &= GET_MODE_MASK (mode);
8888 if (const0 == 0
8889 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8890 op0 = UNKNOWN;
8891 else if (const0 == 0 && op0 == AND)
8892 op0 = SET;
8893 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8894 && op0 == AND)
8895 op0 = UNKNOWN;
8897 /* ??? Slightly redundant with the above mask, but not entirely.
8898 Moving this above means we'd have to sign-extend the mode mask
8899 for the final test. */
8900 const0 = trunc_int_for_mode (const0, mode);
8902 *pop0 = op0;
8903 *pconst0 = const0;
8905 return 1;
8908 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8909 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
8910 simplify it. Otherwise, return a simplified value.
8912 The shift is normally computed in the widest mode we find in VAROP, as
8913 long as it isn't a different number of words than RESULT_MODE. Exceptions
8914 are ASHIFTRT and ROTATE, which are always done in their original mode. */
8916 static rtx
8917 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8918 rtx varop, int orig_count)
8920 enum rtx_code orig_code = code;
8921 rtx orig_varop = varop;
8922 int count;
8923 enum machine_mode mode = result_mode;
8924 enum machine_mode shift_mode, tmode;
8925 unsigned int mode_words
8926 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8927 /* We form (outer_op (code varop count) (outer_const)). */
8928 enum rtx_code outer_op = UNKNOWN;
8929 HOST_WIDE_INT outer_const = 0;
8930 int complement_p = 0;
8931 rtx new, x;
8933 /* Make sure and truncate the "natural" shift on the way in. We don't
8934 want to do this inside the loop as it makes it more difficult to
8935 combine shifts. */
8936 if (SHIFT_COUNT_TRUNCATED)
8937 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8939 /* If we were given an invalid count, don't do anything except exactly
8940 what was requested. */
8942 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8943 return NULL_RTX;
8945 count = orig_count;
8947 /* Unless one of the branches of the `if' in this loop does a `continue',
8948 we will `break' the loop after the `if'. */
8950 while (count != 0)
8952 /* If we have an operand of (clobber (const_int 0)), fail. */
8953 if (GET_CODE (varop) == CLOBBER)
8954 return NULL_RTX;
8956 /* If we discovered we had to complement VAROP, leave. Making a NOT
8957 here would cause an infinite loop. */
8958 if (complement_p)
8959 break;
8961 /* Convert ROTATERT to ROTATE. */
8962 if (code == ROTATERT)
8964 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8965 code = ROTATE;
8966 if (VECTOR_MODE_P (result_mode))
8967 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8968 else
8969 count = bitsize - count;
8972 /* We need to determine what mode we will do the shift in. If the
8973 shift is a right shift or a ROTATE, we must always do it in the mode
8974 it was originally done in. Otherwise, we can do it in MODE, the
8975 widest mode encountered. */
8976 shift_mode
8977 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8978 ? result_mode : mode);
8980 /* Handle cases where the count is greater than the size of the mode
8981 minus 1. For ASHIFT, use the size minus one as the count (this can
8982 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8983 take the count modulo the size. For other shifts, the result is
8984 zero.
8986 Since these shifts are being produced by the compiler by combining
8987 multiple operations, each of which are defined, we know what the
8988 result is supposed to be. */
8990 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
8992 if (code == ASHIFTRT)
8993 count = GET_MODE_BITSIZE (shift_mode) - 1;
8994 else if (code == ROTATE || code == ROTATERT)
8995 count %= GET_MODE_BITSIZE (shift_mode);
8996 else
8998 /* We can't simply return zero because there may be an
8999 outer op. */
9000 varop = const0_rtx;
9001 count = 0;
9002 break;
9006 /* An arithmetic right shift of a quantity known to be -1 or 0
9007 is a no-op. */
9008 if (code == ASHIFTRT
9009 && (num_sign_bit_copies (varop, shift_mode)
9010 == GET_MODE_BITSIZE (shift_mode)))
9012 count = 0;
9013 break;
9016 /* If we are doing an arithmetic right shift and discarding all but
9017 the sign bit copies, this is equivalent to doing a shift by the
9018 bitsize minus one. Convert it into that shift because it will often
9019 allow other simplifications. */
9021 if (code == ASHIFTRT
9022 && (count + num_sign_bit_copies (varop, shift_mode)
9023 >= GET_MODE_BITSIZE (shift_mode)))
9024 count = GET_MODE_BITSIZE (shift_mode) - 1;
9026 /* We simplify the tests below and elsewhere by converting
9027 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9028 `make_compound_operation' will convert it to an ASHIFTRT for
9029 those machines (such as VAX) that don't have an LSHIFTRT. */
9030 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9031 && code == ASHIFTRT
9032 && ((nonzero_bits (varop, shift_mode)
9033 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9034 == 0))
9035 code = LSHIFTRT;
9037 if (((code == LSHIFTRT
9038 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9039 && !(nonzero_bits (varop, shift_mode) >> count))
9040 || (code == ASHIFT
9041 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9042 && !((nonzero_bits (varop, shift_mode) << count)
9043 & GET_MODE_MASK (shift_mode))))
9044 && !side_effects_p (varop))
9045 varop = const0_rtx;
9047 switch (GET_CODE (varop))
9049 case SIGN_EXTEND:
9050 case ZERO_EXTEND:
9051 case SIGN_EXTRACT:
9052 case ZERO_EXTRACT:
9053 new = expand_compound_operation (varop);
9054 if (new != varop)
9056 varop = new;
9057 continue;
9059 break;
9061 case MEM:
9062 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9063 minus the width of a smaller mode, we can do this with a
9064 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9065 if ((code == ASHIFTRT || code == LSHIFTRT)
9066 && ! mode_dependent_address_p (XEXP (varop, 0))
9067 && ! MEM_VOLATILE_P (varop)
9068 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9069 MODE_INT, 1)) != BLKmode)
9071 new = adjust_address_nv (varop, tmode,
9072 BYTES_BIG_ENDIAN ? 0
9073 : count / BITS_PER_UNIT);
9075 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9076 : ZERO_EXTEND, mode, new);
9077 count = 0;
9078 continue;
9080 break;
9082 case SUBREG:
9083 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9084 the same number of words as what we've seen so far. Then store
9085 the widest mode in MODE. */
9086 if (subreg_lowpart_p (varop)
9087 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9088 > GET_MODE_SIZE (GET_MODE (varop)))
9089 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9090 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9091 == mode_words)
9093 varop = SUBREG_REG (varop);
9094 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9095 mode = GET_MODE (varop);
9096 continue;
9098 break;
9100 case MULT:
9101 /* Some machines use MULT instead of ASHIFT because MULT
9102 is cheaper. But it is still better on those machines to
9103 merge two shifts into one. */
9104 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9105 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9107 varop
9108 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9109 XEXP (varop, 0),
9110 GEN_INT (exact_log2 (
9111 INTVAL (XEXP (varop, 1)))));
9112 continue;
9114 break;
9116 case UDIV:
9117 /* Similar, for when divides are cheaper. */
9118 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9119 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9121 varop
9122 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9123 XEXP (varop, 0),
9124 GEN_INT (exact_log2 (
9125 INTVAL (XEXP (varop, 1)))));
9126 continue;
9128 break;
9130 case ASHIFTRT:
9131 /* If we are extracting just the sign bit of an arithmetic
9132 right shift, that shift is not needed. However, the sign
9133 bit of a wider mode may be different from what would be
9134 interpreted as the sign bit in a narrower mode, so, if
9135 the result is narrower, don't discard the shift. */
9136 if (code == LSHIFTRT
9137 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9138 && (GET_MODE_BITSIZE (result_mode)
9139 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9141 varop = XEXP (varop, 0);
9142 continue;
9145 /* ... fall through ... */
9147 case LSHIFTRT:
9148 case ASHIFT:
9149 case ROTATE:
9150 /* Here we have two nested shifts. The result is usually the
9151 AND of a new shift with a mask. We compute the result below. */
9152 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9153 && INTVAL (XEXP (varop, 1)) >= 0
9154 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9155 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9156 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9157 && !VECTOR_MODE_P (result_mode))
9159 enum rtx_code first_code = GET_CODE (varop);
9160 unsigned int first_count = INTVAL (XEXP (varop, 1));
9161 unsigned HOST_WIDE_INT mask;
9162 rtx mask_rtx;
9164 /* We have one common special case. We can't do any merging if
9165 the inner code is an ASHIFTRT of a smaller mode. However, if
9166 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9167 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9168 we can convert it to
9169 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9170 This simplifies certain SIGN_EXTEND operations. */
9171 if (code == ASHIFT && first_code == ASHIFTRT
9172 && count == (GET_MODE_BITSIZE (result_mode)
9173 - GET_MODE_BITSIZE (GET_MODE (varop))))
9175 /* C3 has the low-order C1 bits zero. */
9177 mask = (GET_MODE_MASK (mode)
9178 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9180 varop = simplify_and_const_int (NULL_RTX, result_mode,
9181 XEXP (varop, 0), mask);
9182 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9183 varop, count);
9184 count = first_count;
9185 code = ASHIFTRT;
9186 continue;
9189 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9190 than C1 high-order bits equal to the sign bit, we can convert
9191 this to either an ASHIFT or an ASHIFTRT depending on the
9192 two counts.
9194 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9196 if (code == ASHIFTRT && first_code == ASHIFT
9197 && GET_MODE (varop) == shift_mode
9198 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9199 > first_count))
9201 varop = XEXP (varop, 0);
9202 count -= first_count;
9203 if (count < 0)
9205 count = -count;
9206 code = ASHIFT;
9209 continue;
9212 /* There are some cases we can't do. If CODE is ASHIFTRT,
9213 we can only do this if FIRST_CODE is also ASHIFTRT.
9215 We can't do the case when CODE is ROTATE and FIRST_CODE is
9216 ASHIFTRT.
9218 If the mode of this shift is not the mode of the outer shift,
9219 we can't do this if either shift is a right shift or ROTATE.
9221 Finally, we can't do any of these if the mode is too wide
9222 unless the codes are the same.
9224 Handle the case where the shift codes are the same
9225 first. */
9227 if (code == first_code)
9229 if (GET_MODE (varop) != result_mode
9230 && (code == ASHIFTRT || code == LSHIFTRT
9231 || code == ROTATE))
9232 break;
9234 count += first_count;
9235 varop = XEXP (varop, 0);
9236 continue;
9239 if (code == ASHIFTRT
9240 || (code == ROTATE && first_code == ASHIFTRT)
9241 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9242 || (GET_MODE (varop) != result_mode
9243 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9244 || first_code == ROTATE
9245 || code == ROTATE)))
9246 break;
9248 /* To compute the mask to apply after the shift, shift the
9249 nonzero bits of the inner shift the same way the
9250 outer shift will. */
9252 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9254 mask_rtx
9255 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9256 GEN_INT (count));
9258 /* Give up if we can't compute an outer operation to use. */
9259 if (mask_rtx == 0
9260 || GET_CODE (mask_rtx) != CONST_INT
9261 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9262 INTVAL (mask_rtx),
9263 result_mode, &complement_p))
9264 break;
9266 /* If the shifts are in the same direction, we add the
9267 counts. Otherwise, we subtract them. */
9268 if ((code == ASHIFTRT || code == LSHIFTRT)
9269 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9270 count += first_count;
9271 else
9272 count -= first_count;
9274 /* If COUNT is positive, the new shift is usually CODE,
9275 except for the two exceptions below, in which case it is
9276 FIRST_CODE. If the count is negative, FIRST_CODE should
9277 always be used */
9278 if (count > 0
9279 && ((first_code == ROTATE && code == ASHIFT)
9280 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9281 code = first_code;
9282 else if (count < 0)
9283 code = first_code, count = -count;
9285 varop = XEXP (varop, 0);
9286 continue;
9289 /* If we have (A << B << C) for any shift, we can convert this to
9290 (A << C << B). This wins if A is a constant. Only try this if
9291 B is not a constant. */
9293 else if (GET_CODE (varop) == code
9294 && GET_CODE (XEXP (varop, 0)) == CONST_INT
9295 && GET_CODE (XEXP (varop, 1)) != CONST_INT)
9297 rtx new = simplify_const_binary_operation (code, mode,
9298 XEXP (varop, 0),
9299 GEN_INT (count));
9300 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9301 count = 0;
9302 continue;
9304 break;
9306 case NOT:
9307 /* Make this fit the case below. */
9308 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9309 GEN_INT (GET_MODE_MASK (mode)));
9310 continue;
9312 case IOR:
9313 case AND:
9314 case XOR:
9315 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9316 with C the size of VAROP - 1 and the shift is logical if
9317 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9318 we have an (le X 0) operation. If we have an arithmetic shift
9319 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9320 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9322 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9323 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9324 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9325 && (code == LSHIFTRT || code == ASHIFTRT)
9326 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9327 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9329 count = 0;
9330 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9331 const0_rtx);
9333 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9334 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9336 continue;
9339 /* If we have (shift (logical)), move the logical to the outside
9340 to allow it to possibly combine with another logical and the
9341 shift to combine with another shift. This also canonicalizes to
9342 what a ZERO_EXTRACT looks like. Also, some machines have
9343 (and (shift)) insns. */
9345 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9346 /* We can't do this if we have (ashiftrt (xor)) and the
9347 constant has its sign bit set in shift_mode. */
9348 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9349 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9350 shift_mode))
9351 && (new = simplify_const_binary_operation (code, result_mode,
9352 XEXP (varop, 1),
9353 GEN_INT (count))) != 0
9354 && GET_CODE (new) == CONST_INT
9355 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9356 INTVAL (new), result_mode, &complement_p))
9358 varop = XEXP (varop, 0);
9359 continue;
9362 /* If we can't do that, try to simplify the shift in each arm of the
9363 logical expression, make a new logical expression, and apply
9364 the inverse distributive law. This also can't be done
9365 for some (ashiftrt (xor)). */
9366 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9367 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9368 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9369 shift_mode)))
9371 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9372 XEXP (varop, 0), count);
9373 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9374 XEXP (varop, 1), count);
9376 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9377 lhs, rhs);
9378 varop = apply_distributive_law (varop);
9380 count = 0;
9381 continue;
9383 break;
9385 case EQ:
9386 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9387 says that the sign bit can be tested, FOO has mode MODE, C is
9388 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9389 that may be nonzero. */
9390 if (code == LSHIFTRT
9391 && XEXP (varop, 1) == const0_rtx
9392 && GET_MODE (XEXP (varop, 0)) == result_mode
9393 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9394 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9395 && STORE_FLAG_VALUE == -1
9396 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9397 && merge_outer_ops (&outer_op, &outer_const, XOR,
9398 (HOST_WIDE_INT) 1, result_mode,
9399 &complement_p))
9401 varop = XEXP (varop, 0);
9402 count = 0;
9403 continue;
9405 break;
9407 case NEG:
9408 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9409 than the number of bits in the mode is equivalent to A. */
9410 if (code == LSHIFTRT
9411 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9412 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9414 varop = XEXP (varop, 0);
9415 count = 0;
9416 continue;
9419 /* NEG commutes with ASHIFT since it is multiplication. Move the
9420 NEG outside to allow shifts to combine. */
9421 if (code == ASHIFT
9422 && merge_outer_ops (&outer_op, &outer_const, NEG,
9423 (HOST_WIDE_INT) 0, result_mode,
9424 &complement_p))
9426 varop = XEXP (varop, 0);
9427 continue;
9429 break;
9431 case PLUS:
9432 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9433 is one less than the number of bits in the mode is
9434 equivalent to (xor A 1). */
9435 if (code == LSHIFTRT
9436 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9437 && XEXP (varop, 1) == constm1_rtx
9438 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9439 && merge_outer_ops (&outer_op, &outer_const, XOR,
9440 (HOST_WIDE_INT) 1, result_mode,
9441 &complement_p))
9443 count = 0;
9444 varop = XEXP (varop, 0);
9445 continue;
9448 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9449 that might be nonzero in BAR are those being shifted out and those
9450 bits are known zero in FOO, we can replace the PLUS with FOO.
9451 Similarly in the other operand order. This code occurs when
9452 we are computing the size of a variable-size array. */
9454 if ((code == ASHIFTRT || code == LSHIFTRT)
9455 && count < HOST_BITS_PER_WIDE_INT
9456 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9457 && (nonzero_bits (XEXP (varop, 1), result_mode)
9458 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9460 varop = XEXP (varop, 0);
9461 continue;
9463 else if ((code == ASHIFTRT || code == LSHIFTRT)
9464 && count < HOST_BITS_PER_WIDE_INT
9465 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9466 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9467 >> count)
9468 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9469 & nonzero_bits (XEXP (varop, 1),
9470 result_mode)))
9472 varop = XEXP (varop, 1);
9473 continue;
9476 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9477 if (code == ASHIFT
9478 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9479 && (new = simplify_const_binary_operation (ASHIFT, result_mode,
9480 XEXP (varop, 1),
9481 GEN_INT (count))) != 0
9482 && GET_CODE (new) == CONST_INT
9483 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9484 INTVAL (new), result_mode, &complement_p))
9486 varop = XEXP (varop, 0);
9487 continue;
9490 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9491 signbit', and attempt to change the PLUS to an XOR and move it to
9492 the outer operation as is done above in the AND/IOR/XOR case
9493 leg for shift(logical). See details in logical handling above
9494 for reasoning in doing so. */
9495 if (code == LSHIFTRT
9496 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9497 && mode_signbit_p (result_mode, XEXP (varop, 1))
9498 && (new = simplify_const_binary_operation (code, result_mode,
9499 XEXP (varop, 1),
9500 GEN_INT (count))) != 0
9501 && GET_CODE (new) == CONST_INT
9502 && merge_outer_ops (&outer_op, &outer_const, XOR,
9503 INTVAL (new), result_mode, &complement_p))
9505 varop = XEXP (varop, 0);
9506 continue;
9509 break;
9511 case MINUS:
9512 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9513 with C the size of VAROP - 1 and the shift is logical if
9514 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9515 we have a (gt X 0) operation. If the shift is arithmetic with
9516 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9517 we have a (neg (gt X 0)) operation. */
9519 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9520 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9521 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9522 && (code == LSHIFTRT || code == ASHIFTRT)
9523 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9524 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9525 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9527 count = 0;
9528 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9529 const0_rtx);
9531 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9532 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9534 continue;
9536 break;
9538 case TRUNCATE:
9539 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9540 if the truncate does not affect the value. */
9541 if (code == LSHIFTRT
9542 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9543 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9544 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9545 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9546 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9548 rtx varop_inner = XEXP (varop, 0);
9550 varop_inner
9551 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9552 XEXP (varop_inner, 0),
9553 GEN_INT
9554 (count + INTVAL (XEXP (varop_inner, 1))));
9555 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9556 count = 0;
9557 continue;
9559 break;
9561 default:
9562 break;
9565 break;
9568 /* We need to determine what mode to do the shift in. If the shift is
9569 a right shift or ROTATE, we must always do it in the mode it was
9570 originally done in. Otherwise, we can do it in MODE, the widest mode
9571 encountered. The code we care about is that of the shift that will
9572 actually be done, not the shift that was originally requested. */
9573 shift_mode
9574 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9575 ? result_mode : mode);
9577 /* We have now finished analyzing the shift. The result should be
9578 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9579 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9580 to the result of the shift. OUTER_CONST is the relevant constant,
9581 but we must turn off all bits turned off in the shift. */
9583 if (outer_op == UNKNOWN
9584 && orig_code == code && orig_count == count
9585 && varop == orig_varop
9586 && shift_mode == GET_MODE (varop))
9587 return NULL_RTX;
9589 /* Make a SUBREG if necessary. If we can't make it, fail. */
9590 varop = gen_lowpart (shift_mode, varop);
9591 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9592 return NULL_RTX;
9594 /* If we have an outer operation and we just made a shift, it is
9595 possible that we could have simplified the shift were it not
9596 for the outer operation. So try to do the simplification
9597 recursively. */
9599 if (outer_op != UNKNOWN)
9600 x = simplify_shift_const_1 (code, shift_mode, varop, count);
9601 else
9602 x = NULL_RTX;
9604 if (x == NULL_RTX)
9605 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9607 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9608 turn off all the bits that the shift would have turned off. */
9609 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9610 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9611 GET_MODE_MASK (result_mode) >> orig_count);
9613 /* Do the remainder of the processing in RESULT_MODE. */
9614 x = gen_lowpart_or_truncate (result_mode, x);
9616 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9617 operation. */
9618 if (complement_p)
9619 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9621 if (outer_op != UNKNOWN)
9623 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9624 outer_const = trunc_int_for_mode (outer_const, result_mode);
9626 if (outer_op == AND)
9627 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9628 else if (outer_op == SET)
9630 /* This means that we have determined that the result is
9631 equivalent to a constant. This should be rare. */
9632 if (!side_effects_p (x))
9633 x = GEN_INT (outer_const);
9635 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9636 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9637 else
9638 x = simplify_gen_binary (outer_op, result_mode, x,
9639 GEN_INT (outer_const));
9642 return x;
9645 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9646 The result of the shift is RESULT_MODE. If we cannot simplify it,
9647 return X or, if it is NULL, synthesize the expression with
9648 simplify_gen_binary. Otherwise, return a simplified value.
9650 The shift is normally computed in the widest mode we find in VAROP, as
9651 long as it isn't a different number of words than RESULT_MODE. Exceptions
9652 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9654 static rtx
9655 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9656 rtx varop, int count)
9658 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9659 if (tem)
9660 return tem;
9662 if (!x)
9663 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9664 if (GET_MODE (x) != result_mode)
9665 x = gen_lowpart (result_mode, x);
9666 return x;
9670 /* Like recog, but we receive the address of a pointer to a new pattern.
9671 We try to match the rtx that the pointer points to.
9672 If that fails, we may try to modify or replace the pattern,
9673 storing the replacement into the same pointer object.
9675 Modifications include deletion or addition of CLOBBERs.
9677 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9678 the CLOBBERs are placed.
9680 The value is the final insn code from the pattern ultimately matched,
9681 or -1. */
9683 static int
9684 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9686 rtx pat = *pnewpat;
9687 int insn_code_number;
9688 int num_clobbers_to_add = 0;
9689 int i;
9690 rtx notes = 0;
9691 rtx old_notes, old_pat;
9693 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9694 we use to indicate that something didn't match. If we find such a
9695 thing, force rejection. */
9696 if (GET_CODE (pat) == PARALLEL)
9697 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9698 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9699 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9700 return -1;
9702 old_pat = PATTERN (insn);
9703 old_notes = REG_NOTES (insn);
9704 PATTERN (insn) = pat;
9705 REG_NOTES (insn) = 0;
9707 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9708 if (dump_file && (dump_flags & TDF_DETAILS))
9710 if (insn_code_number < 0)
9711 fputs ("Failed to match this instruction:\n", dump_file);
9712 else
9713 fputs ("Successfully matched this instruction:\n", dump_file);
9714 print_rtl_single (dump_file, pat);
9717 /* If it isn't, there is the possibility that we previously had an insn
9718 that clobbered some register as a side effect, but the combined
9719 insn doesn't need to do that. So try once more without the clobbers
9720 unless this represents an ASM insn. */
9722 if (insn_code_number < 0 && ! check_asm_operands (pat)
9723 && GET_CODE (pat) == PARALLEL)
9725 int pos;
9727 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9728 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9730 if (i != pos)
9731 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9732 pos++;
9735 SUBST_INT (XVECLEN (pat, 0), pos);
9737 if (pos == 1)
9738 pat = XVECEXP (pat, 0, 0);
9740 PATTERN (insn) = pat;
9741 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9742 if (dump_file && (dump_flags & TDF_DETAILS))
9744 if (insn_code_number < 0)
9745 fputs ("Failed to match this instruction:\n", dump_file);
9746 else
9747 fputs ("Successfully matched this instruction:\n", dump_file);
9748 print_rtl_single (dump_file, pat);
9751 PATTERN (insn) = old_pat;
9752 REG_NOTES (insn) = old_notes;
9754 /* Recognize all noop sets, these will be killed by followup pass. */
9755 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9756 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9758 /* If we had any clobbers to add, make a new pattern than contains
9759 them. Then check to make sure that all of them are dead. */
9760 if (num_clobbers_to_add)
9762 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9763 rtvec_alloc (GET_CODE (pat) == PARALLEL
9764 ? (XVECLEN (pat, 0)
9765 + num_clobbers_to_add)
9766 : num_clobbers_to_add + 1));
9768 if (GET_CODE (pat) == PARALLEL)
9769 for (i = 0; i < XVECLEN (pat, 0); i++)
9770 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9771 else
9772 XVECEXP (newpat, 0, 0) = pat;
9774 add_clobbers (newpat, insn_code_number);
9776 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9777 i < XVECLEN (newpat, 0); i++)
9779 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9780 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9781 return -1;
9782 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
9784 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
9785 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9786 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9789 pat = newpat;
9792 *pnewpat = pat;
9793 *pnotes = notes;
9795 return insn_code_number;
9798 /* Like gen_lowpart_general but for use by combine. In combine it
9799 is not possible to create any new pseudoregs. However, it is
9800 safe to create invalid memory addresses, because combine will
9801 try to recognize them and all they will do is make the combine
9802 attempt fail.
9804 If for some reason this cannot do its job, an rtx
9805 (clobber (const_int 0)) is returned.
9806 An insn containing that will not be recognized. */
9808 static rtx
9809 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9811 enum machine_mode imode = GET_MODE (x);
9812 unsigned int osize = GET_MODE_SIZE (omode);
9813 unsigned int isize = GET_MODE_SIZE (imode);
9814 rtx result;
9816 if (omode == imode)
9817 return x;
9819 /* Return identity if this is a CONST or symbolic reference. */
9820 if (omode == Pmode
9821 && (GET_CODE (x) == CONST
9822 || GET_CODE (x) == SYMBOL_REF
9823 || GET_CODE (x) == LABEL_REF))
9824 return x;
9826 /* We can only support MODE being wider than a word if X is a
9827 constant integer or has a mode the same size. */
9828 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9829 && ! ((imode == VOIDmode
9830 && (GET_CODE (x) == CONST_INT
9831 || GET_CODE (x) == CONST_DOUBLE))
9832 || isize == osize))
9833 goto fail;
9835 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9836 won't know what to do. So we will strip off the SUBREG here and
9837 process normally. */
9838 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9840 x = SUBREG_REG (x);
9842 /* For use in case we fall down into the address adjustments
9843 further below, we need to adjust the known mode and size of
9844 x; imode and isize, since we just adjusted x. */
9845 imode = GET_MODE (x);
9847 if (imode == omode)
9848 return x;
9850 isize = GET_MODE_SIZE (imode);
9853 result = gen_lowpart_common (omode, x);
9855 if (result)
9856 return result;
9858 if (MEM_P (x))
9860 int offset = 0;
9862 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9863 address. */
9864 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9865 goto fail;
9867 /* If we want to refer to something bigger than the original memref,
9868 generate a paradoxical subreg instead. That will force a reload
9869 of the original memref X. */
9870 if (isize < osize)
9871 return gen_rtx_SUBREG (omode, x, 0);
9873 if (WORDS_BIG_ENDIAN)
9874 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9876 /* Adjust the address so that the address-after-the-data is
9877 unchanged. */
9878 if (BYTES_BIG_ENDIAN)
9879 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9881 return adjust_address_nv (x, omode, offset);
9884 /* If X is a comparison operator, rewrite it in a new mode. This
9885 probably won't match, but may allow further simplifications. */
9886 else if (COMPARISON_P (x))
9887 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9889 /* If we couldn't simplify X any other way, just enclose it in a
9890 SUBREG. Normally, this SUBREG won't match, but some patterns may
9891 include an explicit SUBREG or we may simplify it further in combine. */
9892 else
9894 int offset = 0;
9895 rtx res;
9897 offset = subreg_lowpart_offset (omode, imode);
9898 if (imode == VOIDmode)
9900 imode = int_mode_for_mode (omode);
9901 x = gen_lowpart_common (imode, x);
9902 if (x == NULL)
9903 goto fail;
9905 res = simplify_gen_subreg (omode, x, imode, offset);
9906 if (res)
9907 return res;
9910 fail:
9911 return gen_rtx_CLOBBER (imode, const0_rtx);
9914 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9915 comparison code that will be tested.
9917 The result is a possibly different comparison code to use. *POP0 and
9918 *POP1 may be updated.
9920 It is possible that we might detect that a comparison is either always
9921 true or always false. However, we do not perform general constant
9922 folding in combine, so this knowledge isn't useful. Such tautologies
9923 should have been detected earlier. Hence we ignore all such cases. */
9925 static enum rtx_code
9926 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9928 rtx op0 = *pop0;
9929 rtx op1 = *pop1;
9930 rtx tem, tem1;
9931 int i;
9932 enum machine_mode mode, tmode;
9934 /* Try a few ways of applying the same transformation to both operands. */
9935 while (1)
9937 #ifndef WORD_REGISTER_OPERATIONS
9938 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9939 so check specially. */
9940 if (code != GTU && code != GEU && code != LTU && code != LEU
9941 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9942 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9943 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9944 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9945 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9946 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9947 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9948 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9949 && XEXP (op0, 1) == XEXP (op1, 1)
9950 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9951 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9952 && (INTVAL (XEXP (op0, 1))
9953 == (GET_MODE_BITSIZE (GET_MODE (op0))
9954 - (GET_MODE_BITSIZE
9955 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9957 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9958 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9960 #endif
9962 /* If both operands are the same constant shift, see if we can ignore the
9963 shift. We can if the shift is a rotate or if the bits shifted out of
9964 this shift are known to be zero for both inputs and if the type of
9965 comparison is compatible with the shift. */
9966 if (GET_CODE (op0) == GET_CODE (op1)
9967 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9968 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9969 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9970 && (code != GT && code != LT && code != GE && code != LE))
9971 || (GET_CODE (op0) == ASHIFTRT
9972 && (code != GTU && code != LTU
9973 && code != GEU && code != LEU)))
9974 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9975 && INTVAL (XEXP (op0, 1)) >= 0
9976 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9977 && XEXP (op0, 1) == XEXP (op1, 1))
9979 enum machine_mode mode = GET_MODE (op0);
9980 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9981 int shift_count = INTVAL (XEXP (op0, 1));
9983 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9984 mask &= (mask >> shift_count) << shift_count;
9985 else if (GET_CODE (op0) == ASHIFT)
9986 mask = (mask & (mask << shift_count)) >> shift_count;
9988 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9989 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9990 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9991 else
9992 break;
9995 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9996 SUBREGs are of the same mode, and, in both cases, the AND would
9997 be redundant if the comparison was done in the narrower mode,
9998 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9999 and the operand's possibly nonzero bits are 0xffffff01; in that case
10000 if we only care about QImode, we don't need the AND). This case
10001 occurs if the output mode of an scc insn is not SImode and
10002 STORE_FLAG_VALUE == 1 (e.g., the 386).
10004 Similarly, check for a case where the AND's are ZERO_EXTEND
10005 operations from some narrower mode even though a SUBREG is not
10006 present. */
10008 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10009 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10010 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10012 rtx inner_op0 = XEXP (op0, 0);
10013 rtx inner_op1 = XEXP (op1, 0);
10014 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10015 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10016 int changed = 0;
10018 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10019 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10020 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10021 && (GET_MODE (SUBREG_REG (inner_op0))
10022 == GET_MODE (SUBREG_REG (inner_op1)))
10023 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10024 <= HOST_BITS_PER_WIDE_INT)
10025 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10026 GET_MODE (SUBREG_REG (inner_op0)))))
10027 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10028 GET_MODE (SUBREG_REG (inner_op1))))))
10030 op0 = SUBREG_REG (inner_op0);
10031 op1 = SUBREG_REG (inner_op1);
10033 /* The resulting comparison is always unsigned since we masked
10034 off the original sign bit. */
10035 code = unsigned_condition (code);
10037 changed = 1;
10040 else if (c0 == c1)
10041 for (tmode = GET_CLASS_NARROWEST_MODE
10042 (GET_MODE_CLASS (GET_MODE (op0)));
10043 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10044 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10046 op0 = gen_lowpart (tmode, inner_op0);
10047 op1 = gen_lowpart (tmode, inner_op1);
10048 code = unsigned_condition (code);
10049 changed = 1;
10050 break;
10053 if (! changed)
10054 break;
10057 /* If both operands are NOT, we can strip off the outer operation
10058 and adjust the comparison code for swapped operands; similarly for
10059 NEG, except that this must be an equality comparison. */
10060 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10061 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10062 && (code == EQ || code == NE)))
10063 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10065 else
10066 break;
10069 /* If the first operand is a constant, swap the operands and adjust the
10070 comparison code appropriately, but don't do this if the second operand
10071 is already a constant integer. */
10072 if (swap_commutative_operands_p (op0, op1))
10074 tem = op0, op0 = op1, op1 = tem;
10075 code = swap_condition (code);
10078 /* We now enter a loop during which we will try to simplify the comparison.
10079 For the most part, we only are concerned with comparisons with zero,
10080 but some things may really be comparisons with zero but not start
10081 out looking that way. */
10083 while (GET_CODE (op1) == CONST_INT)
10085 enum machine_mode mode = GET_MODE (op0);
10086 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10087 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10088 int equality_comparison_p;
10089 int sign_bit_comparison_p;
10090 int unsigned_comparison_p;
10091 HOST_WIDE_INT const_op;
10093 /* We only want to handle integral modes. This catches VOIDmode,
10094 CCmode, and the floating-point modes. An exception is that we
10095 can handle VOIDmode if OP0 is a COMPARE or a comparison
10096 operation. */
10098 if (GET_MODE_CLASS (mode) != MODE_INT
10099 && ! (mode == VOIDmode
10100 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10101 break;
10103 /* Get the constant we are comparing against and turn off all bits
10104 not on in our mode. */
10105 const_op = INTVAL (op1);
10106 if (mode != VOIDmode)
10107 const_op = trunc_int_for_mode (const_op, mode);
10108 op1 = GEN_INT (const_op);
10110 /* If we are comparing against a constant power of two and the value
10111 being compared can only have that single bit nonzero (e.g., it was
10112 `and'ed with that bit), we can replace this with a comparison
10113 with zero. */
10114 if (const_op
10115 && (code == EQ || code == NE || code == GE || code == GEU
10116 || code == LT || code == LTU)
10117 && mode_width <= HOST_BITS_PER_WIDE_INT
10118 && exact_log2 (const_op) >= 0
10119 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10121 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10122 op1 = const0_rtx, const_op = 0;
10125 /* Similarly, if we are comparing a value known to be either -1 or
10126 0 with -1, change it to the opposite comparison against zero. */
10128 if (const_op == -1
10129 && (code == EQ || code == NE || code == GT || code == LE
10130 || code == GEU || code == LTU)
10131 && num_sign_bit_copies (op0, mode) == mode_width)
10133 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10134 op1 = const0_rtx, const_op = 0;
10137 /* Do some canonicalizations based on the comparison code. We prefer
10138 comparisons against zero and then prefer equality comparisons.
10139 If we can reduce the size of a constant, we will do that too. */
10141 switch (code)
10143 case LT:
10144 /* < C is equivalent to <= (C - 1) */
10145 if (const_op > 0)
10147 const_op -= 1;
10148 op1 = GEN_INT (const_op);
10149 code = LE;
10150 /* ... fall through to LE case below. */
10152 else
10153 break;
10155 case LE:
10156 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10157 if (const_op < 0)
10159 const_op += 1;
10160 op1 = GEN_INT (const_op);
10161 code = LT;
10164 /* If we are doing a <= 0 comparison on a value known to have
10165 a zero sign bit, we can replace this with == 0. */
10166 else if (const_op == 0
10167 && mode_width <= HOST_BITS_PER_WIDE_INT
10168 && (nonzero_bits (op0, mode)
10169 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10170 code = EQ;
10171 break;
10173 case GE:
10174 /* >= C is equivalent to > (C - 1). */
10175 if (const_op > 0)
10177 const_op -= 1;
10178 op1 = GEN_INT (const_op);
10179 code = GT;
10180 /* ... fall through to GT below. */
10182 else
10183 break;
10185 case GT:
10186 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10187 if (const_op < 0)
10189 const_op += 1;
10190 op1 = GEN_INT (const_op);
10191 code = GE;
10194 /* If we are doing a > 0 comparison on a value known to have
10195 a zero sign bit, we can replace this with != 0. */
10196 else if (const_op == 0
10197 && mode_width <= HOST_BITS_PER_WIDE_INT
10198 && (nonzero_bits (op0, mode)
10199 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10200 code = NE;
10201 break;
10203 case LTU:
10204 /* < C is equivalent to <= (C - 1). */
10205 if (const_op > 0)
10207 const_op -= 1;
10208 op1 = GEN_INT (const_op);
10209 code = LEU;
10210 /* ... fall through ... */
10213 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10214 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10215 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10217 const_op = 0, op1 = const0_rtx;
10218 code = GE;
10219 break;
10221 else
10222 break;
10224 case LEU:
10225 /* unsigned <= 0 is equivalent to == 0 */
10226 if (const_op == 0)
10227 code = EQ;
10229 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10230 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10231 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10233 const_op = 0, op1 = const0_rtx;
10234 code = GE;
10236 break;
10238 case GEU:
10239 /* >= C is equivalent to > (C - 1). */
10240 if (const_op > 1)
10242 const_op -= 1;
10243 op1 = GEN_INT (const_op);
10244 code = GTU;
10245 /* ... fall through ... */
10248 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10249 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10250 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10252 const_op = 0, op1 = const0_rtx;
10253 code = LT;
10254 break;
10256 else
10257 break;
10259 case GTU:
10260 /* unsigned > 0 is equivalent to != 0 */
10261 if (const_op == 0)
10262 code = NE;
10264 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10265 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10266 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10268 const_op = 0, op1 = const0_rtx;
10269 code = LT;
10271 break;
10273 default:
10274 break;
10277 /* Compute some predicates to simplify code below. */
10279 equality_comparison_p = (code == EQ || code == NE);
10280 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10281 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10282 || code == GEU);
10284 /* If this is a sign bit comparison and we can do arithmetic in
10285 MODE, say that we will only be needing the sign bit of OP0. */
10286 if (sign_bit_comparison_p
10287 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10288 op0 = force_to_mode (op0, mode,
10289 ((HOST_WIDE_INT) 1
10290 << (GET_MODE_BITSIZE (mode) - 1)),
10293 /* Now try cases based on the opcode of OP0. If none of the cases
10294 does a "continue", we exit this loop immediately after the
10295 switch. */
10297 switch (GET_CODE (op0))
10299 case ZERO_EXTRACT:
10300 /* If we are extracting a single bit from a variable position in
10301 a constant that has only a single bit set and are comparing it
10302 with zero, we can convert this into an equality comparison
10303 between the position and the location of the single bit. */
10304 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10305 have already reduced the shift count modulo the word size. */
10306 if (!SHIFT_COUNT_TRUNCATED
10307 && GET_CODE (XEXP (op0, 0)) == CONST_INT
10308 && XEXP (op0, 1) == const1_rtx
10309 && equality_comparison_p && const_op == 0
10310 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10312 if (BITS_BIG_ENDIAN)
10314 enum machine_mode new_mode
10315 = mode_for_extraction (EP_extzv, 1);
10316 if (new_mode == MAX_MACHINE_MODE)
10317 i = BITS_PER_WORD - 1 - i;
10318 else
10320 mode = new_mode;
10321 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10325 op0 = XEXP (op0, 2);
10326 op1 = GEN_INT (i);
10327 const_op = i;
10329 /* Result is nonzero iff shift count is equal to I. */
10330 code = reverse_condition (code);
10331 continue;
10334 /* ... fall through ... */
10336 case SIGN_EXTRACT:
10337 tem = expand_compound_operation (op0);
10338 if (tem != op0)
10340 op0 = tem;
10341 continue;
10343 break;
10345 case NOT:
10346 /* If testing for equality, we can take the NOT of the constant. */
10347 if (equality_comparison_p
10348 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10350 op0 = XEXP (op0, 0);
10351 op1 = tem;
10352 continue;
10355 /* If just looking at the sign bit, reverse the sense of the
10356 comparison. */
10357 if (sign_bit_comparison_p)
10359 op0 = XEXP (op0, 0);
10360 code = (code == GE ? LT : GE);
10361 continue;
10363 break;
10365 case NEG:
10366 /* If testing for equality, we can take the NEG of the constant. */
10367 if (equality_comparison_p
10368 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10370 op0 = XEXP (op0, 0);
10371 op1 = tem;
10372 continue;
10375 /* The remaining cases only apply to comparisons with zero. */
10376 if (const_op != 0)
10377 break;
10379 /* When X is ABS or is known positive,
10380 (neg X) is < 0 if and only if X != 0. */
10382 if (sign_bit_comparison_p
10383 && (GET_CODE (XEXP (op0, 0)) == ABS
10384 || (mode_width <= HOST_BITS_PER_WIDE_INT
10385 && (nonzero_bits (XEXP (op0, 0), mode)
10386 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10388 op0 = XEXP (op0, 0);
10389 code = (code == LT ? NE : EQ);
10390 continue;
10393 /* If we have NEG of something whose two high-order bits are the
10394 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10395 if (num_sign_bit_copies (op0, mode) >= 2)
10397 op0 = XEXP (op0, 0);
10398 code = swap_condition (code);
10399 continue;
10401 break;
10403 case ROTATE:
10404 /* If we are testing equality and our count is a constant, we
10405 can perform the inverse operation on our RHS. */
10406 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10407 && (tem = simplify_binary_operation (ROTATERT, mode,
10408 op1, XEXP (op0, 1))) != 0)
10410 op0 = XEXP (op0, 0);
10411 op1 = tem;
10412 continue;
10415 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10416 a particular bit. Convert it to an AND of a constant of that
10417 bit. This will be converted into a ZERO_EXTRACT. */
10418 if (const_op == 0 && sign_bit_comparison_p
10419 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10420 && mode_width <= HOST_BITS_PER_WIDE_INT)
10422 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10423 ((HOST_WIDE_INT) 1
10424 << (mode_width - 1
10425 - INTVAL (XEXP (op0, 1)))));
10426 code = (code == LT ? NE : EQ);
10427 continue;
10430 /* Fall through. */
10432 case ABS:
10433 /* ABS is ignorable inside an equality comparison with zero. */
10434 if (const_op == 0 && equality_comparison_p)
10436 op0 = XEXP (op0, 0);
10437 continue;
10439 break;
10441 case SIGN_EXTEND:
10442 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10443 (compare FOO CONST) if CONST fits in FOO's mode and we
10444 are either testing inequality or have an unsigned
10445 comparison with ZERO_EXTEND or a signed comparison with
10446 SIGN_EXTEND. But don't do it if we don't have a compare
10447 insn of the given mode, since we'd have to revert it
10448 later on, and then we wouldn't know whether to sign- or
10449 zero-extend. */
10450 mode = GET_MODE (XEXP (op0, 0));
10451 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10452 && ! unsigned_comparison_p
10453 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10454 && ((unsigned HOST_WIDE_INT) const_op
10455 < (((unsigned HOST_WIDE_INT) 1
10456 << (GET_MODE_BITSIZE (mode) - 1))))
10457 && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10459 op0 = XEXP (op0, 0);
10460 continue;
10462 break;
10464 case SUBREG:
10465 /* Check for the case where we are comparing A - C1 with C2, that is
10467 (subreg:MODE (plus (A) (-C1))) op (C2)
10469 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10470 comparison in the wider mode. One of the following two conditions
10471 must be true in order for this to be valid:
10473 1. The mode extension results in the same bit pattern being added
10474 on both sides and the comparison is equality or unsigned. As
10475 C2 has been truncated to fit in MODE, the pattern can only be
10476 all 0s or all 1s.
10478 2. The mode extension results in the sign bit being copied on
10479 each side.
10481 The difficulty here is that we have predicates for A but not for
10482 (A - C1) so we need to check that C1 is within proper bounds so
10483 as to perturbate A as little as possible. */
10485 if (mode_width <= HOST_BITS_PER_WIDE_INT
10486 && subreg_lowpart_p (op0)
10487 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10488 && GET_CODE (SUBREG_REG (op0)) == PLUS
10489 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10491 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10492 rtx a = XEXP (SUBREG_REG (op0), 0);
10493 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10495 if ((c1 > 0
10496 && (unsigned HOST_WIDE_INT) c1
10497 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10498 && (equality_comparison_p || unsigned_comparison_p)
10499 /* (A - C1) zero-extends if it is positive and sign-extends
10500 if it is negative, C2 both zero- and sign-extends. */
10501 && ((0 == (nonzero_bits (a, inner_mode)
10502 & ~GET_MODE_MASK (mode))
10503 && const_op >= 0)
10504 /* (A - C1) sign-extends if it is positive and 1-extends
10505 if it is negative, C2 both sign- and 1-extends. */
10506 || (num_sign_bit_copies (a, inner_mode)
10507 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10508 - mode_width)
10509 && const_op < 0)))
10510 || ((unsigned HOST_WIDE_INT) c1
10511 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10512 /* (A - C1) always sign-extends, like C2. */
10513 && num_sign_bit_copies (a, inner_mode)
10514 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10515 - (mode_width - 1))))
10517 op0 = SUBREG_REG (op0);
10518 continue;
10522 /* If the inner mode is narrower and we are extracting the low part,
10523 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10524 if (subreg_lowpart_p (op0)
10525 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10526 /* Fall through */ ;
10527 else
10528 break;
10530 /* ... fall through ... */
10532 case ZERO_EXTEND:
10533 mode = GET_MODE (XEXP (op0, 0));
10534 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10535 && (unsigned_comparison_p || equality_comparison_p)
10536 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10537 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10538 && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10540 op0 = XEXP (op0, 0);
10541 continue;
10543 break;
10545 case PLUS:
10546 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10547 this for equality comparisons due to pathological cases involving
10548 overflows. */
10549 if (equality_comparison_p
10550 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10551 op1, XEXP (op0, 1))))
10553 op0 = XEXP (op0, 0);
10554 op1 = tem;
10555 continue;
10558 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10559 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10560 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10562 op0 = XEXP (XEXP (op0, 0), 0);
10563 code = (code == LT ? EQ : NE);
10564 continue;
10566 break;
10568 case MINUS:
10569 /* We used to optimize signed comparisons against zero, but that
10570 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10571 arrive here as equality comparisons, or (GEU, LTU) are
10572 optimized away. No need to special-case them. */
10574 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10575 (eq B (minus A C)), whichever simplifies. We can only do
10576 this for equality comparisons due to pathological cases involving
10577 overflows. */
10578 if (equality_comparison_p
10579 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10580 XEXP (op0, 1), op1)))
10582 op0 = XEXP (op0, 0);
10583 op1 = tem;
10584 continue;
10587 if (equality_comparison_p
10588 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10589 XEXP (op0, 0), op1)))
10591 op0 = XEXP (op0, 1);
10592 op1 = tem;
10593 continue;
10596 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10597 of bits in X minus 1, is one iff X > 0. */
10598 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10599 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10600 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10601 == mode_width - 1
10602 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10604 op0 = XEXP (op0, 1);
10605 code = (code == GE ? LE : GT);
10606 continue;
10608 break;
10610 case XOR:
10611 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10612 if C is zero or B is a constant. */
10613 if (equality_comparison_p
10614 && 0 != (tem = simplify_binary_operation (XOR, mode,
10615 XEXP (op0, 1), op1)))
10617 op0 = XEXP (op0, 0);
10618 op1 = tem;
10619 continue;
10621 break;
10623 case EQ: case NE:
10624 case UNEQ: case LTGT:
10625 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10626 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10627 case UNORDERED: case ORDERED:
10628 /* We can't do anything if OP0 is a condition code value, rather
10629 than an actual data value. */
10630 if (const_op != 0
10631 || CC0_P (XEXP (op0, 0))
10632 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10633 break;
10635 /* Get the two operands being compared. */
10636 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10637 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10638 else
10639 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10641 /* Check for the cases where we simply want the result of the
10642 earlier test or the opposite of that result. */
10643 if (code == NE || code == EQ
10644 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10645 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10646 && (STORE_FLAG_VALUE
10647 & (((HOST_WIDE_INT) 1
10648 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10649 && (code == LT || code == GE)))
10651 enum rtx_code new_code;
10652 if (code == LT || code == NE)
10653 new_code = GET_CODE (op0);
10654 else
10655 new_code = reversed_comparison_code (op0, NULL);
10657 if (new_code != UNKNOWN)
10659 code = new_code;
10660 op0 = tem;
10661 op1 = tem1;
10662 continue;
10665 break;
10667 case IOR:
10668 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10669 iff X <= 0. */
10670 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10671 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10672 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10674 op0 = XEXP (op0, 1);
10675 code = (code == GE ? GT : LE);
10676 continue;
10678 break;
10680 case AND:
10681 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10682 will be converted to a ZERO_EXTRACT later. */
10683 if (const_op == 0 && equality_comparison_p
10684 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10685 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10687 op0 = simplify_and_const_int
10688 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10689 XEXP (op0, 1),
10690 XEXP (XEXP (op0, 0), 1)),
10691 (HOST_WIDE_INT) 1);
10692 continue;
10695 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10696 zero and X is a comparison and C1 and C2 describe only bits set
10697 in STORE_FLAG_VALUE, we can compare with X. */
10698 if (const_op == 0 && equality_comparison_p
10699 && mode_width <= HOST_BITS_PER_WIDE_INT
10700 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10701 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10702 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10703 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10704 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10706 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10707 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10708 if ((~STORE_FLAG_VALUE & mask) == 0
10709 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10710 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10711 && COMPARISON_P (tem))))
10713 op0 = XEXP (XEXP (op0, 0), 0);
10714 continue;
10718 /* If we are doing an equality comparison of an AND of a bit equal
10719 to the sign bit, replace this with a LT or GE comparison of
10720 the underlying value. */
10721 if (equality_comparison_p
10722 && const_op == 0
10723 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10724 && mode_width <= HOST_BITS_PER_WIDE_INT
10725 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10726 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10728 op0 = XEXP (op0, 0);
10729 code = (code == EQ ? GE : LT);
10730 continue;
10733 /* If this AND operation is really a ZERO_EXTEND from a narrower
10734 mode, the constant fits within that mode, and this is either an
10735 equality or unsigned comparison, try to do this comparison in
10736 the narrower mode.
10738 Note that in:
10740 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10741 -> (ne:DI (reg:SI 4) (const_int 0))
10743 unless TRULY_NOOP_TRUNCATION allows it or the register is
10744 known to hold a value of the required mode the
10745 transformation is invalid. */
10746 if ((equality_comparison_p || unsigned_comparison_p)
10747 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10748 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10749 & GET_MODE_MASK (mode))
10750 + 1)) >= 0
10751 && const_op >> i == 0
10752 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10753 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10754 GET_MODE_BITSIZE (GET_MODE (op0)))
10755 || (REG_P (XEXP (op0, 0))
10756 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10758 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10759 continue;
10762 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10763 fits in both M1 and M2 and the SUBREG is either paradoxical
10764 or represents the low part, permute the SUBREG and the AND
10765 and try again. */
10766 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10768 unsigned HOST_WIDE_INT c1;
10769 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10770 /* Require an integral mode, to avoid creating something like
10771 (AND:SF ...). */
10772 if (SCALAR_INT_MODE_P (tmode)
10773 /* It is unsafe to commute the AND into the SUBREG if the
10774 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10775 not defined. As originally written the upper bits
10776 have a defined value due to the AND operation.
10777 However, if we commute the AND inside the SUBREG then
10778 they no longer have defined values and the meaning of
10779 the code has been changed. */
10780 && (0
10781 #ifdef WORD_REGISTER_OPERATIONS
10782 || (mode_width > GET_MODE_BITSIZE (tmode)
10783 && mode_width <= BITS_PER_WORD)
10784 #endif
10785 || (mode_width <= GET_MODE_BITSIZE (tmode)
10786 && subreg_lowpart_p (XEXP (op0, 0))))
10787 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10788 && mode_width <= HOST_BITS_PER_WIDE_INT
10789 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10790 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10791 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10792 && c1 != mask
10793 && c1 != GET_MODE_MASK (tmode))
10795 op0 = simplify_gen_binary (AND, tmode,
10796 SUBREG_REG (XEXP (op0, 0)),
10797 gen_int_mode (c1, tmode));
10798 op0 = gen_lowpart (mode, op0);
10799 continue;
10803 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10804 if (const_op == 0 && equality_comparison_p
10805 && XEXP (op0, 1) == const1_rtx
10806 && GET_CODE (XEXP (op0, 0)) == NOT)
10808 op0 = simplify_and_const_int
10809 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10810 code = (code == NE ? EQ : NE);
10811 continue;
10814 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10815 (eq (and (lshiftrt X) 1) 0).
10816 Also handle the case where (not X) is expressed using xor. */
10817 if (const_op == 0 && equality_comparison_p
10818 && XEXP (op0, 1) == const1_rtx
10819 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10821 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10822 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10824 if (GET_CODE (shift_op) == NOT
10825 || (GET_CODE (shift_op) == XOR
10826 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10827 && GET_CODE (shift_count) == CONST_INT
10828 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10829 && (INTVAL (XEXP (shift_op, 1))
10830 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10832 op0 = simplify_and_const_int
10833 (NULL_RTX, mode,
10834 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10835 (HOST_WIDE_INT) 1);
10836 code = (code == NE ? EQ : NE);
10837 continue;
10840 break;
10842 case ASHIFT:
10843 /* If we have (compare (ashift FOO N) (const_int C)) and
10844 the high order N bits of FOO (N+1 if an inequality comparison)
10845 are known to be zero, we can do this by comparing FOO with C
10846 shifted right N bits so long as the low-order N bits of C are
10847 zero. */
10848 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10849 && INTVAL (XEXP (op0, 1)) >= 0
10850 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10851 < HOST_BITS_PER_WIDE_INT)
10852 && ((const_op
10853 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10854 && mode_width <= HOST_BITS_PER_WIDE_INT
10855 && (nonzero_bits (XEXP (op0, 0), mode)
10856 & ~(mask >> (INTVAL (XEXP (op0, 1))
10857 + ! equality_comparison_p))) == 0)
10859 /* We must perform a logical shift, not an arithmetic one,
10860 as we want the top N bits of C to be zero. */
10861 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10863 temp >>= INTVAL (XEXP (op0, 1));
10864 op1 = gen_int_mode (temp, mode);
10865 op0 = XEXP (op0, 0);
10866 continue;
10869 /* If we are doing a sign bit comparison, it means we are testing
10870 a particular bit. Convert it to the appropriate AND. */
10871 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10872 && mode_width <= HOST_BITS_PER_WIDE_INT)
10874 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10875 ((HOST_WIDE_INT) 1
10876 << (mode_width - 1
10877 - INTVAL (XEXP (op0, 1)))));
10878 code = (code == LT ? NE : EQ);
10879 continue;
10882 /* If this an equality comparison with zero and we are shifting
10883 the low bit to the sign bit, we can convert this to an AND of the
10884 low-order bit. */
10885 if (const_op == 0 && equality_comparison_p
10886 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10887 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10888 == mode_width - 1)
10890 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10891 (HOST_WIDE_INT) 1);
10892 continue;
10894 break;
10896 case ASHIFTRT:
10897 /* If this is an equality comparison with zero, we can do this
10898 as a logical shift, which might be much simpler. */
10899 if (equality_comparison_p && const_op == 0
10900 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10902 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10903 XEXP (op0, 0),
10904 INTVAL (XEXP (op0, 1)));
10905 continue;
10908 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10909 do the comparison in a narrower mode. */
10910 if (! unsigned_comparison_p
10911 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10912 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10913 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10914 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10915 MODE_INT, 1)) != BLKmode
10916 && (((unsigned HOST_WIDE_INT) const_op
10917 + (GET_MODE_MASK (tmode) >> 1) + 1)
10918 <= GET_MODE_MASK (tmode)))
10920 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10921 continue;
10924 /* Likewise if OP0 is a PLUS of a sign extension with a
10925 constant, which is usually represented with the PLUS
10926 between the shifts. */
10927 if (! unsigned_comparison_p
10928 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10929 && GET_CODE (XEXP (op0, 0)) == PLUS
10930 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10931 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10932 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10933 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10934 MODE_INT, 1)) != BLKmode
10935 && (((unsigned HOST_WIDE_INT) const_op
10936 + (GET_MODE_MASK (tmode) >> 1) + 1)
10937 <= GET_MODE_MASK (tmode)))
10939 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10940 rtx add_const = XEXP (XEXP (op0, 0), 1);
10941 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10942 add_const, XEXP (op0, 1));
10944 op0 = simplify_gen_binary (PLUS, tmode,
10945 gen_lowpart (tmode, inner),
10946 new_const);
10947 continue;
10950 /* ... fall through ... */
10951 case LSHIFTRT:
10952 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10953 the low order N bits of FOO are known to be zero, we can do this
10954 by comparing FOO with C shifted left N bits so long as no
10955 overflow occurs. */
10956 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10957 && INTVAL (XEXP (op0, 1)) >= 0
10958 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10959 && mode_width <= HOST_BITS_PER_WIDE_INT
10960 && (nonzero_bits (XEXP (op0, 0), mode)
10961 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10962 && (((unsigned HOST_WIDE_INT) const_op
10963 + (GET_CODE (op0) != LSHIFTRT
10964 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10965 + 1)
10966 : 0))
10967 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10969 /* If the shift was logical, then we must make the condition
10970 unsigned. */
10971 if (GET_CODE (op0) == LSHIFTRT)
10972 code = unsigned_condition (code);
10974 const_op <<= INTVAL (XEXP (op0, 1));
10975 op1 = GEN_INT (const_op);
10976 op0 = XEXP (op0, 0);
10977 continue;
10980 /* If we are using this shift to extract just the sign bit, we
10981 can replace this with an LT or GE comparison. */
10982 if (const_op == 0
10983 && (equality_comparison_p || sign_bit_comparison_p)
10984 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10985 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10986 == mode_width - 1)
10988 op0 = XEXP (op0, 0);
10989 code = (code == NE || code == GT ? LT : GE);
10990 continue;
10992 break;
10994 default:
10995 break;
10998 break;
11001 /* Now make any compound operations involved in this comparison. Then,
11002 check for an outmost SUBREG on OP0 that is not doing anything or is
11003 paradoxical. The latter transformation must only be performed when
11004 it is known that the "extra" bits will be the same in op0 and op1 or
11005 that they don't matter. There are three cases to consider:
11007 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11008 care bits and we can assume they have any convenient value. So
11009 making the transformation is safe.
11011 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11012 In this case the upper bits of op0 are undefined. We should not make
11013 the simplification in that case as we do not know the contents of
11014 those bits.
11016 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11017 UNKNOWN. In that case we know those bits are zeros or ones. We must
11018 also be sure that they are the same as the upper bits of op1.
11020 We can never remove a SUBREG for a non-equality comparison because
11021 the sign bit is in a different place in the underlying object. */
11023 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11024 op1 = make_compound_operation (op1, SET);
11026 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11027 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11028 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11029 && (code == NE || code == EQ))
11031 if (GET_MODE_SIZE (GET_MODE (op0))
11032 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11034 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11035 implemented. */
11036 if (REG_P (SUBREG_REG (op0)))
11038 op0 = SUBREG_REG (op0);
11039 op1 = gen_lowpart (GET_MODE (op0), op1);
11042 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11043 <= HOST_BITS_PER_WIDE_INT)
11044 && (nonzero_bits (SUBREG_REG (op0),
11045 GET_MODE (SUBREG_REG (op0)))
11046 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11048 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11050 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11051 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11052 op0 = SUBREG_REG (op0), op1 = tem;
11056 /* We now do the opposite procedure: Some machines don't have compare
11057 insns in all modes. If OP0's mode is an integer mode smaller than a
11058 word and we can't do a compare in that mode, see if there is a larger
11059 mode for which we can do the compare. There are a number of cases in
11060 which we can use the wider mode. */
11062 mode = GET_MODE (op0);
11063 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11064 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11065 && ! have_insn_for (COMPARE, mode))
11066 for (tmode = GET_MODE_WIDER_MODE (mode);
11067 (tmode != VOIDmode
11068 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11069 tmode = GET_MODE_WIDER_MODE (tmode))
11070 if (have_insn_for (COMPARE, tmode))
11072 int zero_extended;
11074 /* If the only nonzero bits in OP0 and OP1 are those in the
11075 narrower mode and this is an equality or unsigned comparison,
11076 we can use the wider mode. Similarly for sign-extended
11077 values, in which case it is true for all comparisons. */
11078 zero_extended = ((code == EQ || code == NE
11079 || code == GEU || code == GTU
11080 || code == LEU || code == LTU)
11081 && (nonzero_bits (op0, tmode)
11082 & ~GET_MODE_MASK (mode)) == 0
11083 && ((GET_CODE (op1) == CONST_INT
11084 || (nonzero_bits (op1, tmode)
11085 & ~GET_MODE_MASK (mode)) == 0)));
11087 if (zero_extended
11088 || ((num_sign_bit_copies (op0, tmode)
11089 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11090 - GET_MODE_BITSIZE (mode)))
11091 && (num_sign_bit_copies (op1, tmode)
11092 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11093 - GET_MODE_BITSIZE (mode)))))
11095 /* If OP0 is an AND and we don't have an AND in MODE either,
11096 make a new AND in the proper mode. */
11097 if (GET_CODE (op0) == AND
11098 && !have_insn_for (AND, mode))
11099 op0 = simplify_gen_binary (AND, tmode,
11100 gen_lowpart (tmode,
11101 XEXP (op0, 0)),
11102 gen_lowpart (tmode,
11103 XEXP (op0, 1)));
11105 op0 = gen_lowpart (tmode, op0);
11106 if (zero_extended && GET_CODE (op1) == CONST_INT)
11107 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11108 op1 = gen_lowpart (tmode, op1);
11109 break;
11112 /* If this is a test for negative, we can make an explicit
11113 test of the sign bit. */
11115 if (op1 == const0_rtx && (code == LT || code == GE)
11116 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11118 op0 = simplify_gen_binary (AND, tmode,
11119 gen_lowpart (tmode, op0),
11120 GEN_INT ((HOST_WIDE_INT) 1
11121 << (GET_MODE_BITSIZE (mode)
11122 - 1)));
11123 code = (code == LT) ? NE : EQ;
11124 break;
11128 #ifdef CANONICALIZE_COMPARISON
11129 /* If this machine only supports a subset of valid comparisons, see if we
11130 can convert an unsupported one into a supported one. */
11131 CANONICALIZE_COMPARISON (code, op0, op1);
11132 #endif
11134 *pop0 = op0;
11135 *pop1 = op1;
11137 return code;
11140 /* Utility function for record_value_for_reg. Count number of
11141 rtxs in X. */
11142 static int
11143 count_rtxs (rtx x)
11145 enum rtx_code code = GET_CODE (x);
11146 const char *fmt;
11147 int i, ret = 1;
11149 if (GET_RTX_CLASS (code) == '2'
11150 || GET_RTX_CLASS (code) == 'c')
11152 rtx x0 = XEXP (x, 0);
11153 rtx x1 = XEXP (x, 1);
11155 if (x0 == x1)
11156 return 1 + 2 * count_rtxs (x0);
11158 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11159 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11160 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11161 return 2 + 2 * count_rtxs (x0)
11162 + count_rtxs (x == XEXP (x1, 0)
11163 ? XEXP (x1, 1) : XEXP (x1, 0));
11165 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11166 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11167 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11168 return 2 + 2 * count_rtxs (x1)
11169 + count_rtxs (x == XEXP (x0, 0)
11170 ? XEXP (x0, 1) : XEXP (x0, 0));
11173 fmt = GET_RTX_FORMAT (code);
11174 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11175 if (fmt[i] == 'e')
11176 ret += count_rtxs (XEXP (x, i));
11178 return ret;
11181 /* Utility function for following routine. Called when X is part of a value
11182 being stored into last_set_value. Sets last_set_table_tick
11183 for each register mentioned. Similar to mention_regs in cse.c */
11185 static void
11186 update_table_tick (rtx x)
11188 enum rtx_code code = GET_CODE (x);
11189 const char *fmt = GET_RTX_FORMAT (code);
11190 int i;
11192 if (code == REG)
11194 unsigned int regno = REGNO (x);
11195 unsigned int endregno = END_REGNO (x);
11196 unsigned int r;
11198 for (r = regno; r < endregno; r++)
11200 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11201 rsp->last_set_table_tick = label_tick;
11204 return;
11207 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11208 /* Note that we can't have an "E" in values stored; see
11209 get_last_value_validate. */
11210 if (fmt[i] == 'e')
11212 /* Check for identical subexpressions. If x contains
11213 identical subexpression we only have to traverse one of
11214 them. */
11215 if (i == 0 && ARITHMETIC_P (x))
11217 /* Note that at this point x1 has already been
11218 processed. */
11219 rtx x0 = XEXP (x, 0);
11220 rtx x1 = XEXP (x, 1);
11222 /* If x0 and x1 are identical then there is no need to
11223 process x0. */
11224 if (x0 == x1)
11225 break;
11227 /* If x0 is identical to a subexpression of x1 then while
11228 processing x1, x0 has already been processed. Thus we
11229 are done with x. */
11230 if (ARITHMETIC_P (x1)
11231 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11232 break;
11234 /* If x1 is identical to a subexpression of x0 then we
11235 still have to process the rest of x0. */
11236 if (ARITHMETIC_P (x0)
11237 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11239 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11240 break;
11244 update_table_tick (XEXP (x, i));
11248 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11249 are saying that the register is clobbered and we no longer know its
11250 value. If INSN is zero, don't update reg_stat[].last_set; this is
11251 only permitted with VALUE also zero and is used to invalidate the
11252 register. */
11254 static void
11255 record_value_for_reg (rtx reg, rtx insn, rtx value)
11257 unsigned int regno = REGNO (reg);
11258 unsigned int endregno = END_REGNO (reg);
11259 unsigned int i;
11260 reg_stat_type *rsp;
11262 /* If VALUE contains REG and we have a previous value for REG, substitute
11263 the previous value. */
11264 if (value && insn && reg_overlap_mentioned_p (reg, value))
11266 rtx tem;
11268 /* Set things up so get_last_value is allowed to see anything set up to
11269 our insn. */
11270 subst_low_luid = DF_INSN_LUID (insn);
11271 tem = get_last_value (reg);
11273 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11274 it isn't going to be useful and will take a lot of time to process,
11275 so just use the CLOBBER. */
11277 if (tem)
11279 if (ARITHMETIC_P (tem)
11280 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11281 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11282 tem = XEXP (tem, 0);
11283 else if (count_occurrences (value, reg, 1) >= 2)
11285 /* If there are two or more occurrences of REG in VALUE,
11286 prevent the value from growing too much. */
11287 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11288 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11291 value = replace_rtx (copy_rtx (value), reg, tem);
11295 /* For each register modified, show we don't know its value, that
11296 we don't know about its bitwise content, that its value has been
11297 updated, and that we don't know the location of the death of the
11298 register. */
11299 for (i = regno; i < endregno; i++)
11301 rsp = VEC_index (reg_stat_type, reg_stat, i);
11303 if (insn)
11304 rsp->last_set = insn;
11306 rsp->last_set_value = 0;
11307 rsp->last_set_mode = 0;
11308 rsp->last_set_nonzero_bits = 0;
11309 rsp->last_set_sign_bit_copies = 0;
11310 rsp->last_death = 0;
11311 rsp->truncated_to_mode = 0;
11314 /* Mark registers that are being referenced in this value. */
11315 if (value)
11316 update_table_tick (value);
11318 /* Now update the status of each register being set.
11319 If someone is using this register in this block, set this register
11320 to invalid since we will get confused between the two lives in this
11321 basic block. This makes using this register always invalid. In cse, we
11322 scan the table to invalidate all entries using this register, but this
11323 is too much work for us. */
11325 for (i = regno; i < endregno; i++)
11327 rsp = VEC_index (reg_stat_type, reg_stat, i);
11328 rsp->last_set_label = label_tick;
11329 if (!insn
11330 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11331 rsp->last_set_invalid = 1;
11332 else
11333 rsp->last_set_invalid = 0;
11336 /* The value being assigned might refer to X (like in "x++;"). In that
11337 case, we must replace it with (clobber (const_int 0)) to prevent
11338 infinite loops. */
11339 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11340 if (value && ! get_last_value_validate (&value, insn,
11341 rsp->last_set_label, 0))
11343 value = copy_rtx (value);
11344 if (! get_last_value_validate (&value, insn,
11345 rsp->last_set_label, 1))
11346 value = 0;
11349 /* For the main register being modified, update the value, the mode, the
11350 nonzero bits, and the number of sign bit copies. */
11352 rsp->last_set_value = value;
11354 if (value)
11356 enum machine_mode mode = GET_MODE (reg);
11357 subst_low_luid = DF_INSN_LUID (insn);
11358 rsp->last_set_mode = mode;
11359 if (GET_MODE_CLASS (mode) == MODE_INT
11360 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11361 mode = nonzero_bits_mode;
11362 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11363 rsp->last_set_sign_bit_copies
11364 = num_sign_bit_copies (value, GET_MODE (reg));
11368 /* Called via note_stores from record_dead_and_set_regs to handle one
11369 SET or CLOBBER in an insn. DATA is the instruction in which the
11370 set is occurring. */
11372 static void
11373 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11375 rtx record_dead_insn = (rtx) data;
11377 if (GET_CODE (dest) == SUBREG)
11378 dest = SUBREG_REG (dest);
11380 if (!record_dead_insn)
11382 if (REG_P (dest))
11383 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11384 return;
11387 if (REG_P (dest))
11389 /* If we are setting the whole register, we know its value. Otherwise
11390 show that we don't know the value. We can handle SUBREG in
11391 some cases. */
11392 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11393 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11394 else if (GET_CODE (setter) == SET
11395 && GET_CODE (SET_DEST (setter)) == SUBREG
11396 && SUBREG_REG (SET_DEST (setter)) == dest
11397 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11398 && subreg_lowpart_p (SET_DEST (setter)))
11399 record_value_for_reg (dest, record_dead_insn,
11400 gen_lowpart (GET_MODE (dest),
11401 SET_SRC (setter)));
11402 else
11403 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11405 else if (MEM_P (dest)
11406 /* Ignore pushes, they clobber nothing. */
11407 && ! push_operand (dest, GET_MODE (dest)))
11408 mem_last_set = DF_INSN_LUID (record_dead_insn);
11411 /* Update the records of when each REG was most recently set or killed
11412 for the things done by INSN. This is the last thing done in processing
11413 INSN in the combiner loop.
11415 We update reg_stat[], in particular fields last_set, last_set_value,
11416 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11417 last_death, and also the similar information mem_last_set (which insn
11418 most recently modified memory) and last_call_luid (which insn was the
11419 most recent subroutine call). */
11421 static void
11422 record_dead_and_set_regs (rtx insn)
11424 rtx link;
11425 unsigned int i;
11427 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11429 if (REG_NOTE_KIND (link) == REG_DEAD
11430 && REG_P (XEXP (link, 0)))
11432 unsigned int regno = REGNO (XEXP (link, 0));
11433 unsigned int endregno = END_REGNO (XEXP (link, 0));
11435 for (i = regno; i < endregno; i++)
11437 reg_stat_type *rsp;
11439 rsp = VEC_index (reg_stat_type, reg_stat, i);
11440 rsp->last_death = insn;
11443 else if (REG_NOTE_KIND (link) == REG_INC)
11444 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11447 if (CALL_P (insn))
11449 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11450 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11452 reg_stat_type *rsp;
11454 rsp = VEC_index (reg_stat_type, reg_stat, i);
11455 rsp->last_set_invalid = 1;
11456 rsp->last_set = insn;
11457 rsp->last_set_value = 0;
11458 rsp->last_set_mode = 0;
11459 rsp->last_set_nonzero_bits = 0;
11460 rsp->last_set_sign_bit_copies = 0;
11461 rsp->last_death = 0;
11462 rsp->truncated_to_mode = 0;
11465 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11467 /* We can't combine into a call pattern. Remember, though, that
11468 the return value register is set at this LUID. We could
11469 still replace a register with the return value from the
11470 wrong subroutine call! */
11471 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11473 else
11474 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11477 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11478 register present in the SUBREG, so for each such SUBREG go back and
11479 adjust nonzero and sign bit information of the registers that are
11480 known to have some zero/sign bits set.
11482 This is needed because when combine blows the SUBREGs away, the
11483 information on zero/sign bits is lost and further combines can be
11484 missed because of that. */
11486 static void
11487 record_promoted_value (rtx insn, rtx subreg)
11489 rtx links, set;
11490 unsigned int regno = REGNO (SUBREG_REG (subreg));
11491 enum machine_mode mode = GET_MODE (subreg);
11493 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11494 return;
11496 for (links = LOG_LINKS (insn); links;)
11498 reg_stat_type *rsp;
11500 insn = XEXP (links, 0);
11501 set = single_set (insn);
11503 if (! set || !REG_P (SET_DEST (set))
11504 || REGNO (SET_DEST (set)) != regno
11505 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11507 links = XEXP (links, 1);
11508 continue;
11511 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11512 if (rsp->last_set == insn)
11514 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11515 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11518 if (REG_P (SET_SRC (set)))
11520 regno = REGNO (SET_SRC (set));
11521 links = LOG_LINKS (insn);
11523 else
11524 break;
11528 /* Check if X, a register, is known to contain a value already
11529 truncated to MODE. In this case we can use a subreg to refer to
11530 the truncated value even though in the generic case we would need
11531 an explicit truncation. */
11533 static bool
11534 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11536 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11537 enum machine_mode truncated = rsp->truncated_to_mode;
11539 if (truncated == 0
11540 || rsp->truncation_label < label_tick_ebb_start)
11541 return false;
11542 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11543 return true;
11544 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11545 GET_MODE_BITSIZE (truncated)))
11546 return true;
11547 return false;
11550 /* X is a REG or a SUBREG. If X is some sort of a truncation record
11551 it. For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11552 a truncate into a subreg using this information. */
11554 static void
11555 record_truncated_value (rtx x)
11557 enum machine_mode truncated_mode;
11558 reg_stat_type *rsp;
11560 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11562 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11563 truncated_mode = GET_MODE (x);
11565 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11566 return;
11568 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11569 GET_MODE_BITSIZE (original_mode)))
11570 return;
11572 x = SUBREG_REG (x);
11574 /* ??? For hard-regs we now record everything. We might be able to
11575 optimize this using last_set_mode. */
11576 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11577 truncated_mode = GET_MODE (x);
11578 else
11579 return;
11581 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11582 if (rsp->truncated_to_mode == 0
11583 || rsp->truncation_label < label_tick_ebb_start
11584 || (GET_MODE_SIZE (truncated_mode)
11585 < GET_MODE_SIZE (rsp->truncated_to_mode)))
11587 rsp->truncated_to_mode = truncated_mode;
11588 rsp->truncation_label = label_tick;
11592 /* Scan X for promoted SUBREGs and truncated REGs. For each one
11593 found, note what it implies to the registers used in it. */
11595 static void
11596 check_conversions (rtx insn, rtx x)
11598 if (GET_CODE (x) == SUBREG || REG_P (x))
11600 if (GET_CODE (x) == SUBREG
11601 && SUBREG_PROMOTED_VAR_P (x)
11602 && REG_P (SUBREG_REG (x)))
11603 record_promoted_value (insn, x);
11605 record_truncated_value (x);
11607 else
11609 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11610 int i, j;
11612 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11613 switch (format[i])
11615 case 'e':
11616 check_conversions (insn, XEXP (x, i));
11617 break;
11618 case 'V':
11619 case 'E':
11620 if (XVEC (x, i) != 0)
11621 for (j = 0; j < XVECLEN (x, i); j++)
11622 check_conversions (insn, XVECEXP (x, i, j));
11623 break;
11628 /* Utility routine for the following function. Verify that all the registers
11629 mentioned in *LOC are valid when *LOC was part of a value set when
11630 label_tick == TICK. Return 0 if some are not.
11632 If REPLACE is nonzero, replace the invalid reference with
11633 (clobber (const_int 0)) and return 1. This replacement is useful because
11634 we often can get useful information about the form of a value (e.g., if
11635 it was produced by a shift that always produces -1 or 0) even though
11636 we don't know exactly what registers it was produced from. */
11638 static int
11639 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11641 rtx x = *loc;
11642 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11643 int len = GET_RTX_LENGTH (GET_CODE (x));
11644 int i;
11646 if (REG_P (x))
11648 unsigned int regno = REGNO (x);
11649 unsigned int endregno = END_REGNO (x);
11650 unsigned int j;
11652 for (j = regno; j < endregno; j++)
11654 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
11655 if (rsp->last_set_invalid
11656 /* If this is a pseudo-register that was only set once and not
11657 live at the beginning of the function, it is always valid. */
11658 || (! (regno >= FIRST_PSEUDO_REGISTER
11659 && REG_N_SETS (regno) == 1
11660 && (!REGNO_REG_SET_P
11661 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
11662 && rsp->last_set_label > tick))
11664 if (replace)
11665 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11666 return replace;
11670 return 1;
11672 /* If this is a memory reference, make sure that there were
11673 no stores after it that might have clobbered the value. We don't
11674 have alias info, so we assume any store invalidates it. */
11675 else if (MEM_P (x) && !MEM_READONLY_P (x)
11676 && DF_INSN_LUID (insn) <= mem_last_set)
11678 if (replace)
11679 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11680 return replace;
11683 for (i = 0; i < len; i++)
11685 if (fmt[i] == 'e')
11687 /* Check for identical subexpressions. If x contains
11688 identical subexpression we only have to traverse one of
11689 them. */
11690 if (i == 1 && ARITHMETIC_P (x))
11692 /* Note that at this point x0 has already been checked
11693 and found valid. */
11694 rtx x0 = XEXP (x, 0);
11695 rtx x1 = XEXP (x, 1);
11697 /* If x0 and x1 are identical then x is also valid. */
11698 if (x0 == x1)
11699 return 1;
11701 /* If x1 is identical to a subexpression of x0 then
11702 while checking x0, x1 has already been checked. Thus
11703 it is valid and so as x. */
11704 if (ARITHMETIC_P (x0)
11705 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11706 return 1;
11708 /* If x0 is identical to a subexpression of x1 then x is
11709 valid iff the rest of x1 is valid. */
11710 if (ARITHMETIC_P (x1)
11711 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11712 return
11713 get_last_value_validate (&XEXP (x1,
11714 x0 == XEXP (x1, 0) ? 1 : 0),
11715 insn, tick, replace);
11718 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11719 replace) == 0)
11720 return 0;
11722 /* Don't bother with these. They shouldn't occur anyway. */
11723 else if (fmt[i] == 'E')
11724 return 0;
11727 /* If we haven't found a reason for it to be invalid, it is valid. */
11728 return 1;
11731 /* Get the last value assigned to X, if known. Some registers
11732 in the value may be replaced with (clobber (const_int 0)) if their value
11733 is known longer known reliably. */
11735 static rtx
11736 get_last_value (const_rtx x)
11738 unsigned int regno;
11739 rtx value;
11740 reg_stat_type *rsp;
11742 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11743 then convert it to the desired mode. If this is a paradoxical SUBREG,
11744 we cannot predict what values the "extra" bits might have. */
11745 if (GET_CODE (x) == SUBREG
11746 && subreg_lowpart_p (x)
11747 && (GET_MODE_SIZE (GET_MODE (x))
11748 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11749 && (value = get_last_value (SUBREG_REG (x))) != 0)
11750 return gen_lowpart (GET_MODE (x), value);
11752 if (!REG_P (x))
11753 return 0;
11755 regno = REGNO (x);
11756 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11757 value = rsp->last_set_value;
11759 /* If we don't have a value, or if it isn't for this basic block and
11760 it's either a hard register, set more than once, or it's a live
11761 at the beginning of the function, return 0.
11763 Because if it's not live at the beginning of the function then the reg
11764 is always set before being used (is never used without being set).
11765 And, if it's set only once, and it's always set before use, then all
11766 uses must have the same last value, even if it's not from this basic
11767 block. */
11769 if (value == 0
11770 || (rsp->last_set_label < label_tick_ebb_start
11771 && (regno < FIRST_PSEUDO_REGISTER
11772 || REG_N_SETS (regno) != 1
11773 || REGNO_REG_SET_P
11774 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
11775 return 0;
11777 /* If the value was set in a later insn than the ones we are processing,
11778 we can't use it even if the register was only set once. */
11779 if (rsp->last_set_label == label_tick
11780 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
11781 return 0;
11783 /* If the value has all its registers valid, return it. */
11784 if (get_last_value_validate (&value, rsp->last_set,
11785 rsp->last_set_label, 0))
11786 return value;
11788 /* Otherwise, make a copy and replace any invalid register with
11789 (clobber (const_int 0)). If that fails for some reason, return 0. */
11791 value = copy_rtx (value);
11792 if (get_last_value_validate (&value, rsp->last_set,
11793 rsp->last_set_label, 1))
11794 return value;
11796 return 0;
11799 /* Return nonzero if expression X refers to a REG or to memory
11800 that is set in an instruction more recent than FROM_LUID. */
11802 static int
11803 use_crosses_set_p (const_rtx x, int from_luid)
11805 const char *fmt;
11806 int i;
11807 enum rtx_code code = GET_CODE (x);
11809 if (code == REG)
11811 unsigned int regno = REGNO (x);
11812 unsigned endreg = END_REGNO (x);
11814 #ifdef PUSH_ROUNDING
11815 /* Don't allow uses of the stack pointer to be moved,
11816 because we don't know whether the move crosses a push insn. */
11817 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11818 return 1;
11819 #endif
11820 for (; regno < endreg; regno++)
11822 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
11823 if (rsp->last_set
11824 && rsp->last_set_label == label_tick
11825 && DF_INSN_LUID (rsp->last_set) > from_luid)
11826 return 1;
11828 return 0;
11831 if (code == MEM && mem_last_set > from_luid)
11832 return 1;
11834 fmt = GET_RTX_FORMAT (code);
11836 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11838 if (fmt[i] == 'E')
11840 int j;
11841 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11842 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
11843 return 1;
11845 else if (fmt[i] == 'e'
11846 && use_crosses_set_p (XEXP (x, i), from_luid))
11847 return 1;
11849 return 0;
11852 /* Define three variables used for communication between the following
11853 routines. */
11855 static unsigned int reg_dead_regno, reg_dead_endregno;
11856 static int reg_dead_flag;
11858 /* Function called via note_stores from reg_dead_at_p.
11860 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11861 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11863 static void
11864 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
11866 unsigned int regno, endregno;
11868 if (!REG_P (dest))
11869 return;
11871 regno = REGNO (dest);
11872 endregno = END_REGNO (dest);
11873 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11874 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11877 /* Return nonzero if REG is known to be dead at INSN.
11879 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11880 referencing REG, it is dead. If we hit a SET referencing REG, it is
11881 live. Otherwise, see if it is live or dead at the start of the basic
11882 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11883 must be assumed to be always live. */
11885 static int
11886 reg_dead_at_p (rtx reg, rtx insn)
11888 basic_block block;
11889 unsigned int i;
11891 /* Set variables for reg_dead_at_p_1. */
11892 reg_dead_regno = REGNO (reg);
11893 reg_dead_endregno = END_REGNO (reg);
11895 reg_dead_flag = 0;
11897 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11898 we allow the machine description to decide whether use-and-clobber
11899 patterns are OK. */
11900 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11902 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11903 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11904 return 0;
11907 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11908 beginning of function. */
11909 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11910 insn = prev_nonnote_insn (insn))
11912 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11913 if (reg_dead_flag)
11914 return reg_dead_flag == 1 ? 1 : 0;
11916 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11917 return 1;
11920 /* Get the basic block that we were in. */
11921 if (insn == 0)
11922 block = ENTRY_BLOCK_PTR->next_bb;
11923 else
11925 FOR_EACH_BB (block)
11926 if (insn == BB_HEAD (block))
11927 break;
11929 if (block == EXIT_BLOCK_PTR)
11930 return 0;
11933 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11934 if (REGNO_REG_SET_P (df_get_live_in (block), i))
11935 return 0;
11937 return 1;
11940 /* Note hard registers in X that are used. */
11942 static void
11943 mark_used_regs_combine (rtx x)
11945 RTX_CODE code = GET_CODE (x);
11946 unsigned int regno;
11947 int i;
11949 switch (code)
11951 case LABEL_REF:
11952 case SYMBOL_REF:
11953 case CONST_INT:
11954 case CONST:
11955 case CONST_DOUBLE:
11956 case CONST_VECTOR:
11957 case PC:
11958 case ADDR_VEC:
11959 case ADDR_DIFF_VEC:
11960 case ASM_INPUT:
11961 #ifdef HAVE_cc0
11962 /* CC0 must die in the insn after it is set, so we don't need to take
11963 special note of it here. */
11964 case CC0:
11965 #endif
11966 return;
11968 case CLOBBER:
11969 /* If we are clobbering a MEM, mark any hard registers inside the
11970 address as used. */
11971 if (MEM_P (XEXP (x, 0)))
11972 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11973 return;
11975 case REG:
11976 regno = REGNO (x);
11977 /* A hard reg in a wide mode may really be multiple registers.
11978 If so, mark all of them just like the first. */
11979 if (regno < FIRST_PSEUDO_REGISTER)
11981 /* None of this applies to the stack, frame or arg pointers. */
11982 if (regno == STACK_POINTER_REGNUM
11983 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11984 || regno == HARD_FRAME_POINTER_REGNUM
11985 #endif
11986 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11987 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11988 #endif
11989 || regno == FRAME_POINTER_REGNUM)
11990 return;
11992 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
11994 return;
11996 case SET:
11998 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11999 the address. */
12000 rtx testreg = SET_DEST (x);
12002 while (GET_CODE (testreg) == SUBREG
12003 || GET_CODE (testreg) == ZERO_EXTRACT
12004 || GET_CODE (testreg) == STRICT_LOW_PART)
12005 testreg = XEXP (testreg, 0);
12007 if (MEM_P (testreg))
12008 mark_used_regs_combine (XEXP (testreg, 0));
12010 mark_used_regs_combine (SET_SRC (x));
12012 return;
12014 default:
12015 break;
12018 /* Recursively scan the operands of this expression. */
12021 const char *fmt = GET_RTX_FORMAT (code);
12023 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12025 if (fmt[i] == 'e')
12026 mark_used_regs_combine (XEXP (x, i));
12027 else if (fmt[i] == 'E')
12029 int j;
12031 for (j = 0; j < XVECLEN (x, i); j++)
12032 mark_used_regs_combine (XVECEXP (x, i, j));
12038 /* Remove register number REGNO from the dead registers list of INSN.
12040 Return the note used to record the death, if there was one. */
12043 remove_death (unsigned int regno, rtx insn)
12045 rtx note = find_regno_note (insn, REG_DEAD, regno);
12047 if (note)
12048 remove_note (insn, note);
12050 return note;
12053 /* For each register (hardware or pseudo) used within expression X, if its
12054 death is in an instruction with luid between FROM_LUID (inclusive) and
12055 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12056 list headed by PNOTES.
12058 That said, don't move registers killed by maybe_kill_insn.
12060 This is done when X is being merged by combination into TO_INSN. These
12061 notes will then be distributed as needed. */
12063 static void
12064 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12065 rtx *pnotes)
12067 const char *fmt;
12068 int len, i;
12069 enum rtx_code code = GET_CODE (x);
12071 if (code == REG)
12073 unsigned int regno = REGNO (x);
12074 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12076 /* Don't move the register if it gets killed in between from and to. */
12077 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12078 && ! reg_referenced_p (x, maybe_kill_insn))
12079 return;
12081 if (where_dead
12082 && DF_INSN_LUID (where_dead) >= from_luid
12083 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12085 rtx note = remove_death (regno, where_dead);
12087 /* It is possible for the call above to return 0. This can occur
12088 when last_death points to I2 or I1 that we combined with.
12089 In that case make a new note.
12091 We must also check for the case where X is a hard register
12092 and NOTE is a death note for a range of hard registers
12093 including X. In that case, we must put REG_DEAD notes for
12094 the remaining registers in place of NOTE. */
12096 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12097 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12098 > GET_MODE_SIZE (GET_MODE (x))))
12100 unsigned int deadregno = REGNO (XEXP (note, 0));
12101 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12102 unsigned int ourend = END_HARD_REGNO (x);
12103 unsigned int i;
12105 for (i = deadregno; i < deadend; i++)
12106 if (i < regno || i >= ourend)
12107 REG_NOTES (where_dead)
12108 = gen_rtx_EXPR_LIST (REG_DEAD,
12109 regno_reg_rtx[i],
12110 REG_NOTES (where_dead));
12113 /* If we didn't find any note, or if we found a REG_DEAD note that
12114 covers only part of the given reg, and we have a multi-reg hard
12115 register, then to be safe we must check for REG_DEAD notes
12116 for each register other than the first. They could have
12117 their own REG_DEAD notes lying around. */
12118 else if ((note == 0
12119 || (note != 0
12120 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12121 < GET_MODE_SIZE (GET_MODE (x)))))
12122 && regno < FIRST_PSEUDO_REGISTER
12123 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12125 unsigned int ourend = END_HARD_REGNO (x);
12126 unsigned int i, offset;
12127 rtx oldnotes = 0;
12129 if (note)
12130 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12131 else
12132 offset = 1;
12134 for (i = regno + offset; i < ourend; i++)
12135 move_deaths (regno_reg_rtx[i],
12136 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12139 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12141 XEXP (note, 1) = *pnotes;
12142 *pnotes = note;
12144 else
12145 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12148 return;
12151 else if (GET_CODE (x) == SET)
12153 rtx dest = SET_DEST (x);
12155 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12157 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12158 that accesses one word of a multi-word item, some
12159 piece of everything register in the expression is used by
12160 this insn, so remove any old death. */
12161 /* ??? So why do we test for equality of the sizes? */
12163 if (GET_CODE (dest) == ZERO_EXTRACT
12164 || GET_CODE (dest) == STRICT_LOW_PART
12165 || (GET_CODE (dest) == SUBREG
12166 && (((GET_MODE_SIZE (GET_MODE (dest))
12167 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12168 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12169 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12171 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12172 return;
12175 /* If this is some other SUBREG, we know it replaces the entire
12176 value, so use that as the destination. */
12177 if (GET_CODE (dest) == SUBREG)
12178 dest = SUBREG_REG (dest);
12180 /* If this is a MEM, adjust deaths of anything used in the address.
12181 For a REG (the only other possibility), the entire value is
12182 being replaced so the old value is not used in this insn. */
12184 if (MEM_P (dest))
12185 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12186 to_insn, pnotes);
12187 return;
12190 else if (GET_CODE (x) == CLOBBER)
12191 return;
12193 len = GET_RTX_LENGTH (code);
12194 fmt = GET_RTX_FORMAT (code);
12196 for (i = 0; i < len; i++)
12198 if (fmt[i] == 'E')
12200 int j;
12201 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12202 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12203 to_insn, pnotes);
12205 else if (fmt[i] == 'e')
12206 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12210 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12211 pattern of an insn. X must be a REG. */
12213 static int
12214 reg_bitfield_target_p (rtx x, rtx body)
12216 int i;
12218 if (GET_CODE (body) == SET)
12220 rtx dest = SET_DEST (body);
12221 rtx target;
12222 unsigned int regno, tregno, endregno, endtregno;
12224 if (GET_CODE (dest) == ZERO_EXTRACT)
12225 target = XEXP (dest, 0);
12226 else if (GET_CODE (dest) == STRICT_LOW_PART)
12227 target = SUBREG_REG (XEXP (dest, 0));
12228 else
12229 return 0;
12231 if (GET_CODE (target) == SUBREG)
12232 target = SUBREG_REG (target);
12234 if (!REG_P (target))
12235 return 0;
12237 tregno = REGNO (target), regno = REGNO (x);
12238 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12239 return target == x;
12241 endtregno = end_hard_regno (GET_MODE (target), tregno);
12242 endregno = end_hard_regno (GET_MODE (x), regno);
12244 return endregno > tregno && regno < endtregno;
12247 else if (GET_CODE (body) == PARALLEL)
12248 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12249 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12250 return 1;
12252 return 0;
12255 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12256 as appropriate. I3 and I2 are the insns resulting from the combination
12257 insns including FROM (I2 may be zero).
12259 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12260 not need REG_DEAD notes because they are being substituted for. This
12261 saves searching in the most common cases.
12263 Each note in the list is either ignored or placed on some insns, depending
12264 on the type of note. */
12266 static void
12267 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12268 rtx elim_i1)
12270 rtx note, next_note;
12271 rtx tem;
12273 for (note = notes; note; note = next_note)
12275 rtx place = 0, place2 = 0;
12277 next_note = XEXP (note, 1);
12278 switch (REG_NOTE_KIND (note))
12280 case REG_BR_PROB:
12281 case REG_BR_PRED:
12282 /* Doesn't matter much where we put this, as long as it's somewhere.
12283 It is preferable to keep these notes on branches, which is most
12284 likely to be i3. */
12285 place = i3;
12286 break;
12288 case REG_VALUE_PROFILE:
12289 /* Just get rid of this note, as it is unused later anyway. */
12290 break;
12292 case REG_NON_LOCAL_GOTO:
12293 if (JUMP_P (i3))
12294 place = i3;
12295 else
12297 gcc_assert (i2 && JUMP_P (i2));
12298 place = i2;
12300 break;
12302 case REG_EH_REGION:
12303 /* These notes must remain with the call or trapping instruction. */
12304 if (CALL_P (i3))
12305 place = i3;
12306 else if (i2 && CALL_P (i2))
12307 place = i2;
12308 else
12310 gcc_assert (flag_non_call_exceptions);
12311 if (may_trap_p (i3))
12312 place = i3;
12313 else if (i2 && may_trap_p (i2))
12314 place = i2;
12315 /* ??? Otherwise assume we've combined things such that we
12316 can now prove that the instructions can't trap. Drop the
12317 note in this case. */
12319 break;
12321 case REG_NORETURN:
12322 case REG_SETJMP:
12323 /* These notes must remain with the call. It should not be
12324 possible for both I2 and I3 to be a call. */
12325 if (CALL_P (i3))
12326 place = i3;
12327 else
12329 gcc_assert (i2 && CALL_P (i2));
12330 place = i2;
12332 break;
12334 case REG_UNUSED:
12335 /* Any clobbers for i3 may still exist, and so we must process
12336 REG_UNUSED notes from that insn.
12338 Any clobbers from i2 or i1 can only exist if they were added by
12339 recog_for_combine. In that case, recog_for_combine created the
12340 necessary REG_UNUSED notes. Trying to keep any original
12341 REG_UNUSED notes from these insns can cause incorrect output
12342 if it is for the same register as the original i3 dest.
12343 In that case, we will notice that the register is set in i3,
12344 and then add a REG_UNUSED note for the destination of i3, which
12345 is wrong. However, it is possible to have REG_UNUSED notes from
12346 i2 or i1 for register which were both used and clobbered, so
12347 we keep notes from i2 or i1 if they will turn into REG_DEAD
12348 notes. */
12350 /* If this register is set or clobbered in I3, put the note there
12351 unless there is one already. */
12352 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12354 if (from_insn != i3)
12355 break;
12357 if (! (REG_P (XEXP (note, 0))
12358 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12359 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12360 place = i3;
12362 /* Otherwise, if this register is used by I3, then this register
12363 now dies here, so we must put a REG_DEAD note here unless there
12364 is one already. */
12365 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12366 && ! (REG_P (XEXP (note, 0))
12367 ? find_regno_note (i3, REG_DEAD,
12368 REGNO (XEXP (note, 0)))
12369 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12371 PUT_REG_NOTE_KIND (note, REG_DEAD);
12372 place = i3;
12374 break;
12376 case REG_EQUAL:
12377 case REG_EQUIV:
12378 case REG_NOALIAS:
12379 /* These notes say something about results of an insn. We can
12380 only support them if they used to be on I3 in which case they
12381 remain on I3. Otherwise they are ignored.
12383 If the note refers to an expression that is not a constant, we
12384 must also ignore the note since we cannot tell whether the
12385 equivalence is still true. It might be possible to do
12386 slightly better than this (we only have a problem if I2DEST
12387 or I1DEST is present in the expression), but it doesn't
12388 seem worth the trouble. */
12390 if (from_insn == i3
12391 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12392 place = i3;
12393 break;
12395 case REG_INC:
12396 case REG_NO_CONFLICT:
12397 /* These notes say something about how a register is used. They must
12398 be present on any use of the register in I2 or I3. */
12399 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12400 place = i3;
12402 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12404 if (place)
12405 place2 = i2;
12406 else
12407 place = i2;
12409 break;
12411 case REG_LABEL_TARGET:
12412 case REG_LABEL_OPERAND:
12413 /* This can show up in several ways -- either directly in the
12414 pattern, or hidden off in the constant pool with (or without?)
12415 a REG_EQUAL note. */
12416 /* ??? Ignore the without-reg_equal-note problem for now. */
12417 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12418 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12419 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12420 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12421 place = i3;
12423 if (i2
12424 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12425 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12426 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12427 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12429 if (place)
12430 place2 = i2;
12431 else
12432 place = i2;
12435 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
12436 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
12437 there. */
12438 if (place && JUMP_P (place)
12439 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12440 && (JUMP_LABEL (place) == NULL
12441 || JUMP_LABEL (place) == XEXP (note, 0)))
12443 rtx label = JUMP_LABEL (place);
12445 if (!label)
12446 JUMP_LABEL (place) = XEXP (note, 0);
12447 else if (LABEL_P (label))
12448 LABEL_NUSES (label)--;
12451 if (place2 && JUMP_P (place2)
12452 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12453 && (JUMP_LABEL (place2) == NULL
12454 || JUMP_LABEL (place2) == XEXP (note, 0)))
12456 rtx label = JUMP_LABEL (place2);
12458 if (!label)
12459 JUMP_LABEL (place2) = XEXP (note, 0);
12460 else if (LABEL_P (label))
12461 LABEL_NUSES (label)--;
12462 place2 = 0;
12464 break;
12466 case REG_NONNEG:
12467 /* This note says something about the value of a register prior
12468 to the execution of an insn. It is too much trouble to see
12469 if the note is still correct in all situations. It is better
12470 to simply delete it. */
12471 break;
12473 case REG_LIBCALL_ID:
12474 /* If the insn previously containing this note still exists,
12475 put it back where it was. Otherwise move it to the previous
12476 insn. */
12477 if (!NOTE_P (from_insn))
12478 place = from_insn;
12479 else
12480 place = prev_real_insn (from_insn);
12481 break;
12482 case REG_RETVAL:
12483 /* If the insn previously containing this note still exists,
12484 put it back where it was. Otherwise move it to the previous
12485 insn. Adjust the corresponding REG_LIBCALL note. */
12486 if (!NOTE_P (from_insn))
12487 place = from_insn;
12488 else
12490 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12491 place = prev_real_insn (from_insn);
12492 if (tem && place)
12493 XEXP (tem, 0) = place;
12494 /* If we're deleting the last remaining instruction of a
12495 libcall sequence, don't add the notes. */
12496 else if (XEXP (note, 0) == from_insn)
12497 tem = place = 0;
12498 /* Don't add the dangling REG_RETVAL note. */
12499 else if (! tem)
12500 place = 0;
12502 break;
12504 case REG_LIBCALL:
12505 /* This is handled similarly to REG_RETVAL. */
12506 if (!NOTE_P (from_insn))
12507 place = from_insn;
12508 else
12510 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12511 place = next_real_insn (from_insn);
12512 if (tem && place)
12513 XEXP (tem, 0) = place;
12514 /* If we're deleting the last remaining instruction of a
12515 libcall sequence, don't add the notes. */
12516 else if (XEXP (note, 0) == from_insn)
12517 tem = place = 0;
12518 /* Don't add the dangling REG_LIBCALL note. */
12519 else if (! tem)
12520 place = 0;
12522 break;
12524 case REG_DEAD:
12525 /* If we replaced the right hand side of FROM_INSN with a
12526 REG_EQUAL note, the original use of the dying register
12527 will not have been combined into I3 and I2. In such cases,
12528 FROM_INSN is guaranteed to be the first of the combined
12529 instructions, so we simply need to search back before
12530 FROM_INSN for the previous use or set of this register,
12531 then alter the notes there appropriately.
12533 If the register is used as an input in I3, it dies there.
12534 Similarly for I2, if it is nonzero and adjacent to I3.
12536 If the register is not used as an input in either I3 or I2
12537 and it is not one of the registers we were supposed to eliminate,
12538 there are two possibilities. We might have a non-adjacent I2
12539 or we might have somehow eliminated an additional register
12540 from a computation. For example, we might have had A & B where
12541 we discover that B will always be zero. In this case we will
12542 eliminate the reference to A.
12544 In both cases, we must search to see if we can find a previous
12545 use of A and put the death note there. */
12547 if (from_insn
12548 && from_insn == i2mod
12549 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12550 tem = from_insn;
12551 else
12553 if (from_insn
12554 && CALL_P (from_insn)
12555 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12556 place = from_insn;
12557 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12558 place = i3;
12559 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12560 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12561 place = i2;
12562 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12563 && !(i2mod
12564 && reg_overlap_mentioned_p (XEXP (note, 0),
12565 i2mod_old_rhs)))
12566 || rtx_equal_p (XEXP (note, 0), elim_i1))
12567 break;
12568 tem = i3;
12571 if (place == 0)
12573 basic_block bb = this_basic_block;
12575 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12577 if (! INSN_P (tem))
12579 if (tem == BB_HEAD (bb))
12580 break;
12581 continue;
12584 /* If the register is being set at TEM, see if that is all
12585 TEM is doing. If so, delete TEM. Otherwise, make this
12586 into a REG_UNUSED note instead. Don't delete sets to
12587 global register vars. */
12588 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12589 || !global_regs[REGNO (XEXP (note, 0))])
12590 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12592 rtx set = single_set (tem);
12593 rtx inner_dest = 0;
12594 #ifdef HAVE_cc0
12595 rtx cc0_setter = NULL_RTX;
12596 #endif
12598 if (set != 0)
12599 for (inner_dest = SET_DEST (set);
12600 (GET_CODE (inner_dest) == STRICT_LOW_PART
12601 || GET_CODE (inner_dest) == SUBREG
12602 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12603 inner_dest = XEXP (inner_dest, 0))
12606 /* Verify that it was the set, and not a clobber that
12607 modified the register.
12609 CC0 targets must be careful to maintain setter/user
12610 pairs. If we cannot delete the setter due to side
12611 effects, mark the user with an UNUSED note instead
12612 of deleting it. */
12614 if (set != 0 && ! side_effects_p (SET_SRC (set))
12615 && rtx_equal_p (XEXP (note, 0), inner_dest)
12616 #ifdef HAVE_cc0
12617 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12618 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12619 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12620 #endif
12623 /* Move the notes and links of TEM elsewhere.
12624 This might delete other dead insns recursively.
12625 First set the pattern to something that won't use
12626 any register. */
12627 rtx old_notes = REG_NOTES (tem);
12629 PATTERN (tem) = pc_rtx;
12630 REG_NOTES (tem) = NULL;
12632 distribute_notes (old_notes, tem, tem, NULL_RTX,
12633 NULL_RTX, NULL_RTX);
12634 distribute_links (LOG_LINKS (tem));
12636 SET_INSN_DELETED (tem);
12638 #ifdef HAVE_cc0
12639 /* Delete the setter too. */
12640 if (cc0_setter)
12642 PATTERN (cc0_setter) = pc_rtx;
12643 old_notes = REG_NOTES (cc0_setter);
12644 REG_NOTES (cc0_setter) = NULL;
12646 distribute_notes (old_notes, cc0_setter,
12647 cc0_setter, NULL_RTX,
12648 NULL_RTX, NULL_RTX);
12649 distribute_links (LOG_LINKS (cc0_setter));
12651 SET_INSN_DELETED (cc0_setter);
12653 #endif
12655 else
12657 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12659 /* If there isn't already a REG_UNUSED note, put one
12660 here. Do not place a REG_DEAD note, even if
12661 the register is also used here; that would not
12662 match the algorithm used in lifetime analysis
12663 and can cause the consistency check in the
12664 scheduler to fail. */
12665 if (! find_regno_note (tem, REG_UNUSED,
12666 REGNO (XEXP (note, 0))))
12667 place = tem;
12668 break;
12671 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12672 || (CALL_P (tem)
12673 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12675 place = tem;
12677 /* If we are doing a 3->2 combination, and we have a
12678 register which formerly died in i3 and was not used
12679 by i2, which now no longer dies in i3 and is used in
12680 i2 but does not die in i2, and place is between i2
12681 and i3, then we may need to move a link from place to
12682 i2. */
12683 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
12684 && from_insn
12685 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
12686 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12688 rtx links = LOG_LINKS (place);
12689 LOG_LINKS (place) = 0;
12690 distribute_links (links);
12692 break;
12695 if (tem == BB_HEAD (bb))
12696 break;
12701 /* If the register is set or already dead at PLACE, we needn't do
12702 anything with this note if it is still a REG_DEAD note.
12703 We check here if it is set at all, not if is it totally replaced,
12704 which is what `dead_or_set_p' checks, so also check for it being
12705 set partially. */
12707 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12709 unsigned int regno = REGNO (XEXP (note, 0));
12710 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12712 if (dead_or_set_p (place, XEXP (note, 0))
12713 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12715 /* Unless the register previously died in PLACE, clear
12716 last_death. [I no longer understand why this is
12717 being done.] */
12718 if (rsp->last_death != place)
12719 rsp->last_death = 0;
12720 place = 0;
12722 else
12723 rsp->last_death = place;
12725 /* If this is a death note for a hard reg that is occupying
12726 multiple registers, ensure that we are still using all
12727 parts of the object. If we find a piece of the object
12728 that is unused, we must arrange for an appropriate REG_DEAD
12729 note to be added for it. However, we can't just emit a USE
12730 and tag the note to it, since the register might actually
12731 be dead; so we recourse, and the recursive call then finds
12732 the previous insn that used this register. */
12734 if (place && regno < FIRST_PSEUDO_REGISTER
12735 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12737 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
12738 int all_used = 1;
12739 unsigned int i;
12741 for (i = regno; i < endregno; i++)
12742 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12743 && ! find_regno_fusage (place, USE, i))
12744 || dead_or_set_regno_p (place, i))
12745 all_used = 0;
12747 if (! all_used)
12749 /* Put only REG_DEAD notes for pieces that are
12750 not already dead or set. */
12752 for (i = regno; i < endregno;
12753 i += hard_regno_nregs[i][reg_raw_mode[i]])
12755 rtx piece = regno_reg_rtx[i];
12756 basic_block bb = this_basic_block;
12758 if (! dead_or_set_p (place, piece)
12759 && ! reg_bitfield_target_p (piece,
12760 PATTERN (place)))
12762 rtx new_note
12763 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12765 distribute_notes (new_note, place, place,
12766 NULL_RTX, NULL_RTX, NULL_RTX);
12768 else if (! refers_to_regno_p (i, i + 1,
12769 PATTERN (place), 0)
12770 && ! find_regno_fusage (place, USE, i))
12771 for (tem = PREV_INSN (place); ;
12772 tem = PREV_INSN (tem))
12774 if (! INSN_P (tem))
12776 if (tem == BB_HEAD (bb))
12777 break;
12778 continue;
12780 if (dead_or_set_p (tem, piece)
12781 || reg_bitfield_target_p (piece,
12782 PATTERN (tem)))
12784 REG_NOTES (tem)
12785 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12786 REG_NOTES (tem));
12787 break;
12793 place = 0;
12797 break;
12799 default:
12800 /* Any other notes should not be present at this point in the
12801 compilation. */
12802 gcc_unreachable ();
12805 if (place)
12807 XEXP (note, 1) = REG_NOTES (place);
12808 REG_NOTES (place) = note;
12811 if (place2)
12812 REG_NOTES (place2)
12813 = gen_rtx_fmt_ee (GET_CODE (note), REG_NOTE_KIND (note),
12814 XEXP (note, 0), REG_NOTES (place2));
12818 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12819 I3, I2, and I1 to new locations. This is also called to add a link
12820 pointing at I3 when I3's destination is changed. */
12822 static void
12823 distribute_links (rtx links)
12825 rtx link, next_link;
12827 for (link = links; link; link = next_link)
12829 rtx place = 0;
12830 rtx insn;
12831 rtx set, reg;
12833 next_link = XEXP (link, 1);
12835 /* If the insn that this link points to is a NOTE or isn't a single
12836 set, ignore it. In the latter case, it isn't clear what we
12837 can do other than ignore the link, since we can't tell which
12838 register it was for. Such links wouldn't be used by combine
12839 anyway.
12841 It is not possible for the destination of the target of the link to
12842 have been changed by combine. The only potential of this is if we
12843 replace I3, I2, and I1 by I3 and I2. But in that case the
12844 destination of I2 also remains unchanged. */
12846 if (NOTE_P (XEXP (link, 0))
12847 || (set = single_set (XEXP (link, 0))) == 0)
12848 continue;
12850 reg = SET_DEST (set);
12851 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12852 || GET_CODE (reg) == STRICT_LOW_PART)
12853 reg = XEXP (reg, 0);
12855 /* A LOG_LINK is defined as being placed on the first insn that uses
12856 a register and points to the insn that sets the register. Start
12857 searching at the next insn after the target of the link and stop
12858 when we reach a set of the register or the end of the basic block.
12860 Note that this correctly handles the link that used to point from
12861 I3 to I2. Also note that not much searching is typically done here
12862 since most links don't point very far away. */
12864 for (insn = NEXT_INSN (XEXP (link, 0));
12865 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12866 || BB_HEAD (this_basic_block->next_bb) != insn));
12867 insn = NEXT_INSN (insn))
12868 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12870 if (reg_referenced_p (reg, PATTERN (insn)))
12871 place = insn;
12872 break;
12874 else if (CALL_P (insn)
12875 && find_reg_fusage (insn, USE, reg))
12877 place = insn;
12878 break;
12880 else if (INSN_P (insn) && reg_set_p (reg, insn))
12881 break;
12883 /* If we found a place to put the link, place it there unless there
12884 is already a link to the same insn as LINK at that point. */
12886 if (place)
12888 rtx link2;
12890 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12891 if (XEXP (link2, 0) == XEXP (link, 0))
12892 break;
12894 if (link2 == 0)
12896 XEXP (link, 1) = LOG_LINKS (place);
12897 LOG_LINKS (place) = link;
12899 /* Set added_links_insn to the earliest insn we added a
12900 link to. */
12901 if (added_links_insn == 0
12902 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
12903 added_links_insn = place;
12909 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12910 Check whether the expression pointer to by LOC is a register or
12911 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12912 Otherwise return zero. */
12914 static int
12915 unmentioned_reg_p_1 (rtx *loc, void *expr)
12917 rtx x = *loc;
12919 if (x != NULL_RTX
12920 && (REG_P (x) || MEM_P (x))
12921 && ! reg_mentioned_p (x, (rtx) expr))
12922 return 1;
12923 return 0;
12926 /* Check for any register or memory mentioned in EQUIV that is not
12927 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12928 of EXPR where some registers may have been replaced by constants. */
12930 static bool
12931 unmentioned_reg_p (rtx equiv, rtx expr)
12933 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12936 void
12937 dump_combine_stats (FILE *file)
12939 fprintf
12940 (file,
12941 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12942 combine_attempts, combine_merges, combine_extras, combine_successes);
12945 void
12946 dump_combine_total_stats (FILE *file)
12948 fprintf
12949 (file,
12950 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12951 total_attempts, total_merges, total_extras, total_successes);
12954 static bool
12955 gate_handle_combine (void)
12957 return (optimize > 0);
12960 /* Try combining insns through substitution. */
12961 static unsigned int
12962 rest_of_handle_combine (void)
12964 int rebuild_jump_labels_after_combine;
12966 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
12967 df_note_add_problem ();
12968 df_analyze ();
12970 regstat_init_n_sets_and_refs ();
12972 rebuild_jump_labels_after_combine
12973 = combine_instructions (get_insns (), max_reg_num ());
12975 /* Combining insns may have turned an indirect jump into a
12976 direct jump. Rebuild the JUMP_LABEL fields of jumping
12977 instructions. */
12978 if (rebuild_jump_labels_after_combine)
12980 timevar_push (TV_JUMP);
12981 rebuild_jump_labels (get_insns ());
12982 cleanup_cfg (0);
12983 timevar_pop (TV_JUMP);
12986 regstat_free_n_sets_and_refs ();
12987 return 0;
12990 struct tree_opt_pass pass_combine =
12992 "combine", /* name */
12993 gate_handle_combine, /* gate */
12994 rest_of_handle_combine, /* execute */
12995 NULL, /* sub */
12996 NULL, /* next */
12997 0, /* static_pass_number */
12998 TV_COMBINE, /* tv_id */
12999 0, /* properties_required */
13000 0, /* properties_provided */
13001 0, /* properties_destroyed */
13002 0, /* todo_flags_start */
13003 TODO_dump_func |
13004 TODO_df_finish | TODO_verify_rtl_sharing |
13005 TODO_ggc_collect, /* todo_flags_finish */
13006 'c' /* letter */