fixed bug in suite grammar
[official-gcc.git] / gcc / combine.c
blobd9d68384a2a65e4912149a55ca5bbb8c682d33e6
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "diagnostic-core.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file. */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105 #include "df.h"
106 #include "cgraph.h"
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;
299 static bool optimize_this_for_speed_p;
302 /* Length of the currently allocated uid_insn_cost array. */
304 static int max_uid_known;
306 /* The following array records the insn_rtx_cost for every insn
307 in the instruction stream. */
309 static int *uid_insn_cost;
311 /* The following array records the LOG_LINKS for every insn in the
312 instruction stream as an INSN_LIST rtx. */
314 static rtx *uid_log_links;
316 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
317 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
319 /* Incremented for each basic block. */
321 static int label_tick;
323 /* Reset to label_tick for each extended basic block in scanning order. */
325 static int label_tick_ebb_start;
327 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
328 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
330 static enum machine_mode nonzero_bits_mode;
332 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
333 be safely used. It is zero while computing them and after combine has
334 completed. This former test prevents propagating values based on
335 previously set values, which can be incorrect if a variable is modified
336 in a loop. */
338 static int nonzero_sign_valid;
341 /* Record one modification to rtl structure
342 to be undone by storing old_contents into *where. */
344 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE };
346 struct undo
348 struct undo *next;
349 enum undo_kind kind;
350 union { rtx r; int i; enum machine_mode m; } old_contents;
351 union { rtx *r; int *i; } where;
354 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
355 num_undo says how many are currently recorded.
357 other_insn is nonzero if we have modified some other insn in the process
358 of working on subst_insn. It must be verified too. */
360 struct undobuf
362 struct undo *undos;
363 struct undo *frees;
364 rtx other_insn;
367 static struct undobuf undobuf;
369 /* Number of times the pseudo being substituted for
370 was found and replaced. */
372 static int n_occurrences;
374 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
375 enum machine_mode,
376 unsigned HOST_WIDE_INT,
377 unsigned HOST_WIDE_INT *);
378 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
379 enum machine_mode,
380 unsigned int, unsigned int *);
381 static void do_SUBST (rtx *, rtx);
382 static void do_SUBST_INT (int *, int);
383 static void init_reg_last (void);
384 static void setup_incoming_promotions (rtx);
385 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
386 static int cant_combine_insn_p (rtx);
387 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
388 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
389 static int contains_muldiv (rtx);
390 static rtx try_combine (rtx, rtx, rtx, rtx, int *);
391 static void undo_all (void);
392 static void undo_commit (void);
393 static rtx *find_split_point (rtx *, rtx, bool);
394 static rtx subst (rtx, rtx, rtx, int, int);
395 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
396 static rtx simplify_if_then_else (rtx);
397 static rtx simplify_set (rtx);
398 static rtx simplify_logical (rtx);
399 static rtx expand_compound_operation (rtx);
400 static const_rtx expand_field_assignment (const_rtx);
401 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
402 rtx, unsigned HOST_WIDE_INT, int, int, int);
403 static rtx extract_left_shift (rtx, int);
404 static rtx make_compound_operation (rtx, enum rtx_code);
405 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
406 unsigned HOST_WIDE_INT *);
407 static rtx canon_reg_for_combine (rtx, rtx);
408 static rtx force_to_mode (rtx, enum machine_mode,
409 unsigned HOST_WIDE_INT, int);
410 static rtx if_then_else_cond (rtx, rtx *, rtx *);
411 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
412 static int rtx_equal_for_field_assignment_p (rtx, rtx);
413 static rtx make_field_assignment (rtx);
414 static rtx apply_distributive_law (rtx);
415 static rtx distribute_and_simplify_rtx (rtx, int);
416 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
417 unsigned HOST_WIDE_INT);
418 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
419 unsigned HOST_WIDE_INT);
420 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
421 HOST_WIDE_INT, enum machine_mode, int *);
422 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
423 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
424 int);
425 static int recog_for_combine (rtx *, rtx, rtx *);
426 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
427 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
428 static void update_table_tick (rtx);
429 static void record_value_for_reg (rtx, rtx, rtx);
430 static void check_promoted_subreg (rtx, rtx);
431 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
432 static void record_dead_and_set_regs (rtx);
433 static int get_last_value_validate (rtx *, rtx, int, int);
434 static rtx get_last_value (const_rtx);
435 static int use_crosses_set_p (const_rtx, int);
436 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
437 static int reg_dead_at_p (rtx, rtx);
438 static void move_deaths (rtx, rtx, int, rtx, rtx *);
439 static int reg_bitfield_target_p (rtx, rtx);
440 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
441 static void distribute_links (rtx);
442 static void mark_used_regs_combine (rtx);
443 static void record_promoted_value (rtx, rtx);
444 static int unmentioned_reg_p_1 (rtx *, void *);
445 static bool unmentioned_reg_p (rtx, rtx);
446 static int record_truncated_value (rtx *, void *);
447 static void record_truncated_values (rtx *, void *);
448 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
449 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
452 /* It is not safe to use ordinary gen_lowpart in combine.
453 See comments in gen_lowpart_for_combine. */
454 #undef RTL_HOOKS_GEN_LOWPART
455 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
457 /* Our implementation of gen_lowpart never emits a new pseudo. */
458 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
459 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
461 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
462 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
464 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
465 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
467 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
468 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
470 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
473 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
474 PATTERN can not be split. Otherwise, it returns an insn sequence.
475 This is a wrapper around split_insns which ensures that the
476 reg_stat vector is made larger if the splitter creates a new
477 register. */
479 static rtx
480 combine_split_insns (rtx pattern, rtx insn)
482 rtx ret;
483 unsigned int nregs;
485 ret = split_insns (pattern, insn);
486 nregs = max_reg_num ();
487 if (nregs > VEC_length (reg_stat_type, reg_stat))
488 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
489 return ret;
492 /* This is used by find_single_use to locate an rtx in LOC that
493 contains exactly one use of DEST, which is typically either a REG
494 or CC0. It returns a pointer to the innermost rtx expression
495 containing DEST. Appearances of DEST that are being used to
496 totally replace it are not counted. */
498 static rtx *
499 find_single_use_1 (rtx dest, rtx *loc)
501 rtx x = *loc;
502 enum rtx_code code = GET_CODE (x);
503 rtx *result = NULL;
504 rtx *this_result;
505 int i;
506 const char *fmt;
508 switch (code)
510 case CONST_INT:
511 case CONST:
512 case LABEL_REF:
513 case SYMBOL_REF:
514 case CONST_DOUBLE:
515 case CONST_VECTOR:
516 case CLOBBER:
517 return 0;
519 case SET:
520 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
521 of a REG that occupies all of the REG, the insn uses DEST if
522 it is mentioned in the destination or the source. Otherwise, we
523 need just check the source. */
524 if (GET_CODE (SET_DEST (x)) != CC0
525 && GET_CODE (SET_DEST (x)) != PC
526 && !REG_P (SET_DEST (x))
527 && ! (GET_CODE (SET_DEST (x)) == SUBREG
528 && REG_P (SUBREG_REG (SET_DEST (x)))
529 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
530 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
531 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
532 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
533 break;
535 return find_single_use_1 (dest, &SET_SRC (x));
537 case MEM:
538 case SUBREG:
539 return find_single_use_1 (dest, &XEXP (x, 0));
541 default:
542 break;
545 /* If it wasn't one of the common cases above, check each expression and
546 vector of this code. Look for a unique usage of DEST. */
548 fmt = GET_RTX_FORMAT (code);
549 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
551 if (fmt[i] == 'e')
553 if (dest == XEXP (x, i)
554 || (REG_P (dest) && REG_P (XEXP (x, i))
555 && REGNO (dest) == REGNO (XEXP (x, i))))
556 this_result = loc;
557 else
558 this_result = find_single_use_1 (dest, &XEXP (x, i));
560 if (result == NULL)
561 result = this_result;
562 else if (this_result)
563 /* Duplicate usage. */
564 return NULL;
566 else if (fmt[i] == 'E')
568 int j;
570 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
572 if (XVECEXP (x, i, j) == dest
573 || (REG_P (dest)
574 && REG_P (XVECEXP (x, i, j))
575 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
576 this_result = loc;
577 else
578 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
580 if (result == NULL)
581 result = this_result;
582 else if (this_result)
583 return NULL;
588 return result;
592 /* See if DEST, produced in INSN, is used only a single time in the
593 sequel. If so, return a pointer to the innermost rtx expression in which
594 it is used.
596 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
598 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
599 care about REG_DEAD notes or LOG_LINKS.
601 Otherwise, we find the single use by finding an insn that has a
602 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
603 only referenced once in that insn, we know that it must be the first
604 and last insn referencing DEST. */
606 static rtx *
607 find_single_use (rtx dest, rtx insn, rtx *ploc)
609 basic_block bb;
610 rtx next;
611 rtx *result;
612 rtx link;
614 #ifdef HAVE_cc0
615 if (dest == cc0_rtx)
617 next = NEXT_INSN (insn);
618 if (next == 0
619 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
620 return 0;
622 result = find_single_use_1 (dest, &PATTERN (next));
623 if (result && ploc)
624 *ploc = next;
625 return result;
627 #endif
629 if (!REG_P (dest))
630 return 0;
632 bb = BLOCK_FOR_INSN (insn);
633 for (next = NEXT_INSN (insn);
634 next && BLOCK_FOR_INSN (next) == bb;
635 next = NEXT_INSN (next))
636 if (INSN_P (next) && dead_or_set_p (next, dest))
638 for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
639 if (XEXP (link, 0) == insn)
640 break;
642 if (link)
644 result = find_single_use_1 (dest, &PATTERN (next));
645 if (ploc)
646 *ploc = next;
647 return result;
651 return 0;
654 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
655 insn. The substitution can be undone by undo_all. If INTO is already
656 set to NEWVAL, do not record this change. Because computing NEWVAL might
657 also call SUBST, we have to compute it before we put anything into
658 the undo table. */
660 static void
661 do_SUBST (rtx *into, rtx newval)
663 struct undo *buf;
664 rtx oldval = *into;
666 if (oldval == newval)
667 return;
669 /* We'd like to catch as many invalid transformations here as
670 possible. Unfortunately, there are way too many mode changes
671 that are perfectly valid, so we'd waste too much effort for
672 little gain doing the checks here. Focus on catching invalid
673 transformations involving integer constants. */
674 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
675 && CONST_INT_P (newval))
677 /* Sanity check that we're replacing oldval with a CONST_INT
678 that is a valid sign-extension for the original mode. */
679 gcc_assert (INTVAL (newval)
680 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
682 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
683 CONST_INT is not valid, because after the replacement, the
684 original mode would be gone. Unfortunately, we can't tell
685 when do_SUBST is called to replace the operand thereof, so we
686 perform this test on oldval instead, checking whether an
687 invalid replacement took place before we got here. */
688 gcc_assert (!(GET_CODE (oldval) == SUBREG
689 && CONST_INT_P (SUBREG_REG (oldval))));
690 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
691 && CONST_INT_P (XEXP (oldval, 0))));
694 if (undobuf.frees)
695 buf = undobuf.frees, undobuf.frees = buf->next;
696 else
697 buf = XNEW (struct undo);
699 buf->kind = UNDO_RTX;
700 buf->where.r = into;
701 buf->old_contents.r = oldval;
702 *into = newval;
704 buf->next = undobuf.undos, undobuf.undos = buf;
707 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
709 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
710 for the value of a HOST_WIDE_INT value (including CONST_INT) is
711 not safe. */
713 static void
714 do_SUBST_INT (int *into, int newval)
716 struct undo *buf;
717 int oldval = *into;
719 if (oldval == newval)
720 return;
722 if (undobuf.frees)
723 buf = undobuf.frees, undobuf.frees = buf->next;
724 else
725 buf = XNEW (struct undo);
727 buf->kind = UNDO_INT;
728 buf->where.i = into;
729 buf->old_contents.i = oldval;
730 *into = newval;
732 buf->next = undobuf.undos, undobuf.undos = buf;
735 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
737 /* Similar to SUBST, but just substitute the mode. This is used when
738 changing the mode of a pseudo-register, so that any other
739 references to the entry in the regno_reg_rtx array will change as
740 well. */
742 static void
743 do_SUBST_MODE (rtx *into, enum machine_mode newval)
745 struct undo *buf;
746 enum machine_mode oldval = GET_MODE (*into);
748 if (oldval == newval)
749 return;
751 if (undobuf.frees)
752 buf = undobuf.frees, undobuf.frees = buf->next;
753 else
754 buf = XNEW (struct undo);
756 buf->kind = UNDO_MODE;
757 buf->where.r = into;
758 buf->old_contents.m = oldval;
759 adjust_reg_mode (*into, newval);
761 buf->next = undobuf.undos, undobuf.undos = buf;
764 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
766 /* Subroutine of try_combine. Determine whether the combine replacement
767 patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
768 insn_rtx_cost that the original instruction sequence I0, I1, I2, I3 and
769 undobuf.other_insn. Note that I1 and/or NEWI2PAT may be NULL_RTX.
770 NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX. This
771 function returns false, if the costs of all instructions can be
772 estimated, and the replacements are more expensive than the original
773 sequence. */
775 static bool
776 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
777 rtx newi2pat, rtx newotherpat)
779 int i0_cost, i1_cost, i2_cost, i3_cost;
780 int new_i2_cost, new_i3_cost;
781 int old_cost, new_cost;
783 /* Lookup the original insn_rtx_costs. */
784 i2_cost = INSN_COST (i2);
785 i3_cost = INSN_COST (i3);
787 if (i1)
789 i1_cost = INSN_COST (i1);
790 if (i0)
792 i0_cost = INSN_COST (i0);
793 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
794 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
796 else
798 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
799 ? i1_cost + i2_cost + i3_cost : 0);
800 i0_cost = 0;
803 else
805 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
806 i1_cost = i0_cost = 0;
809 /* Calculate the replacement insn_rtx_costs. */
810 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
811 if (newi2pat)
813 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
814 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
815 ? new_i2_cost + new_i3_cost : 0;
817 else
819 new_cost = new_i3_cost;
820 new_i2_cost = 0;
823 if (undobuf.other_insn)
825 int old_other_cost, new_other_cost;
827 old_other_cost = INSN_COST (undobuf.other_insn);
828 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
829 if (old_other_cost > 0 && new_other_cost > 0)
831 old_cost += old_other_cost;
832 new_cost += new_other_cost;
834 else
835 old_cost = 0;
838 /* Disallow this recombination if both new_cost and old_cost are
839 greater than zero, and new_cost is greater than old cost. */
840 if (old_cost > 0
841 && new_cost > old_cost)
843 if (dump_file)
845 if (i0)
847 fprintf (dump_file,
848 "rejecting combination of insns %d, %d, %d and %d\n",
849 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
850 INSN_UID (i3));
851 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
852 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
854 else if (i1)
856 fprintf (dump_file,
857 "rejecting combination of insns %d, %d and %d\n",
858 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
859 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
860 i1_cost, i2_cost, i3_cost, old_cost);
862 else
864 fprintf (dump_file,
865 "rejecting combination of insns %d and %d\n",
866 INSN_UID (i2), INSN_UID (i3));
867 fprintf (dump_file, "original costs %d + %d = %d\n",
868 i2_cost, i3_cost, old_cost);
871 if (newi2pat)
873 fprintf (dump_file, "replacement costs %d + %d = %d\n",
874 new_i2_cost, new_i3_cost, new_cost);
876 else
877 fprintf (dump_file, "replacement cost %d\n", new_cost);
880 return false;
883 /* Update the uid_insn_cost array with the replacement costs. */
884 INSN_COST (i2) = new_i2_cost;
885 INSN_COST (i3) = new_i3_cost;
886 if (i1)
887 INSN_COST (i1) = 0;
889 return true;
893 /* Delete any insns that copy a register to itself. */
895 static void
896 delete_noop_moves (void)
898 rtx insn, next;
899 basic_block bb;
901 FOR_EACH_BB (bb)
903 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
905 next = NEXT_INSN (insn);
906 if (INSN_P (insn) && noop_move_p (insn))
908 if (dump_file)
909 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
911 delete_insn_and_edges (insn);
918 /* Fill in log links field for all insns. */
920 static void
921 create_log_links (void)
923 basic_block bb;
924 rtx *next_use, insn;
925 df_ref *def_vec, *use_vec;
927 next_use = XCNEWVEC (rtx, max_reg_num ());
929 /* Pass through each block from the end, recording the uses of each
930 register and establishing log links when def is encountered.
931 Note that we do not clear next_use array in order to save time,
932 so we have to test whether the use is in the same basic block as def.
934 There are a few cases below when we do not consider the definition or
935 usage -- these are taken from original flow.c did. Don't ask me why it is
936 done this way; I don't know and if it works, I don't want to know. */
938 FOR_EACH_BB (bb)
940 FOR_BB_INSNS_REVERSE (bb, insn)
942 if (!NONDEBUG_INSN_P (insn))
943 continue;
945 /* Log links are created only once. */
946 gcc_assert (!LOG_LINKS (insn));
948 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
950 df_ref def = *def_vec;
951 int regno = DF_REF_REGNO (def);
952 rtx use_insn;
954 if (!next_use[regno])
955 continue;
957 /* Do not consider if it is pre/post modification in MEM. */
958 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
959 continue;
961 /* Do not make the log link for frame pointer. */
962 if ((regno == FRAME_POINTER_REGNUM
963 && (! reload_completed || frame_pointer_needed))
964 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
965 || (regno == HARD_FRAME_POINTER_REGNUM
966 && (! reload_completed || frame_pointer_needed))
967 #endif
968 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
969 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
970 #endif
972 continue;
974 use_insn = next_use[regno];
975 if (BLOCK_FOR_INSN (use_insn) == bb)
977 /* flow.c claimed:
979 We don't build a LOG_LINK for hard registers contained
980 in ASM_OPERANDs. If these registers get replaced,
981 we might wind up changing the semantics of the insn,
982 even if reload can make what appear to be valid
983 assignments later. */
984 if (regno >= FIRST_PSEUDO_REGISTER
985 || asm_noperands (PATTERN (use_insn)) < 0)
987 /* Don't add duplicate links between instructions. */
988 rtx links;
989 for (links = LOG_LINKS (use_insn); links;
990 links = XEXP (links, 1))
991 if (insn == XEXP (links, 0))
992 break;
994 if (!links)
995 LOG_LINKS (use_insn) =
996 alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
999 next_use[regno] = NULL_RTX;
1002 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1004 df_ref use = *use_vec;
1005 int regno = DF_REF_REGNO (use);
1007 /* Do not consider the usage of the stack pointer
1008 by function call. */
1009 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1010 continue;
1012 next_use[regno] = insn;
1017 free (next_use);
1020 /* Clear LOG_LINKS fields of insns. */
1022 static void
1023 clear_log_links (void)
1025 rtx insn;
1027 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1028 if (INSN_P (insn))
1029 free_INSN_LIST_list (&LOG_LINKS (insn));
1032 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1033 true if we found a LOG_LINK that proves that A feeds B. This only works
1034 if there are no instructions between A and B which could have a link
1035 depending on A, since in that case we would not record a link for B. */
1037 static bool
1038 insn_a_feeds_b (rtx a, rtx b)
1040 rtx links;
1041 for (links = LOG_LINKS (b); links; links = XEXP (links, 1))
1042 if (XEXP (links, 0) == a)
1043 return true;
1044 return false;
1047 /* Main entry point for combiner. F is the first insn of the function.
1048 NREGS is the first unused pseudo-reg number.
1050 Return nonzero if the combiner has turned an indirect jump
1051 instruction into a direct jump. */
1052 static int
1053 combine_instructions (rtx f, unsigned int nregs)
1055 rtx insn, next;
1056 #ifdef HAVE_cc0
1057 rtx prev;
1058 #endif
1059 rtx links, nextlinks;
1060 rtx first;
1061 basic_block last_bb;
1063 int new_direct_jump_p = 0;
1065 for (first = f; first && !INSN_P (first); )
1066 first = NEXT_INSN (first);
1067 if (!first)
1068 return 0;
1070 combine_attempts = 0;
1071 combine_merges = 0;
1072 combine_extras = 0;
1073 combine_successes = 0;
1075 rtl_hooks = combine_rtl_hooks;
1077 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1079 init_recog_no_volatile ();
1081 /* Allocate array for insn info. */
1082 max_uid_known = get_max_uid ();
1083 uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1084 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1086 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1088 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1089 problems when, for example, we have j <<= 1 in a loop. */
1091 nonzero_sign_valid = 0;
1092 label_tick = label_tick_ebb_start = 1;
1094 /* Scan all SETs and see if we can deduce anything about what
1095 bits are known to be zero for some registers and how many copies
1096 of the sign bit are known to exist for those registers.
1098 Also set any known values so that we can use it while searching
1099 for what bits are known to be set. */
1101 setup_incoming_promotions (first);
1102 /* Allow the entry block and the first block to fall into the same EBB.
1103 Conceptually the incoming promotions are assigned to the entry block. */
1104 last_bb = ENTRY_BLOCK_PTR;
1106 create_log_links ();
1107 FOR_EACH_BB (this_basic_block)
1109 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1110 last_call_luid = 0;
1111 mem_last_set = -1;
1113 label_tick++;
1114 if (!single_pred_p (this_basic_block)
1115 || single_pred (this_basic_block) != last_bb)
1116 label_tick_ebb_start = label_tick;
1117 last_bb = this_basic_block;
1119 FOR_BB_INSNS (this_basic_block, insn)
1120 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1122 subst_low_luid = DF_INSN_LUID (insn);
1123 subst_insn = insn;
1125 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1126 insn);
1127 record_dead_and_set_regs (insn);
1129 #ifdef AUTO_INC_DEC
1130 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1131 if (REG_NOTE_KIND (links) == REG_INC)
1132 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1133 insn);
1134 #endif
1136 /* Record the current insn_rtx_cost of this instruction. */
1137 if (NONJUMP_INSN_P (insn))
1138 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1139 optimize_this_for_speed_p);
1140 if (dump_file)
1141 fprintf(dump_file, "insn_cost %d: %d\n",
1142 INSN_UID (insn), INSN_COST (insn));
1146 nonzero_sign_valid = 1;
1148 /* Now scan all the insns in forward order. */
1149 label_tick = label_tick_ebb_start = 1;
1150 init_reg_last ();
1151 setup_incoming_promotions (first);
1152 last_bb = ENTRY_BLOCK_PTR;
1154 FOR_EACH_BB (this_basic_block)
1156 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1157 last_call_luid = 0;
1158 mem_last_set = -1;
1160 label_tick++;
1161 if (!single_pred_p (this_basic_block)
1162 || single_pred (this_basic_block) != last_bb)
1163 label_tick_ebb_start = label_tick;
1164 last_bb = this_basic_block;
1166 rtl_profile_for_bb (this_basic_block);
1167 for (insn = BB_HEAD (this_basic_block);
1168 insn != NEXT_INSN (BB_END (this_basic_block));
1169 insn = next ? next : NEXT_INSN (insn))
1171 next = 0;
1172 if (NONDEBUG_INSN_P (insn))
1174 /* See if we know about function return values before this
1175 insn based upon SUBREG flags. */
1176 check_promoted_subreg (insn, PATTERN (insn));
1178 /* See if we can find hardregs and subreg of pseudos in
1179 narrower modes. This could help turning TRUNCATEs
1180 into SUBREGs. */
1181 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1183 /* Try this insn with each insn it links back to. */
1185 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1186 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX,
1187 NULL_RTX, &new_direct_jump_p)) != 0)
1188 goto retry;
1190 /* Try each sequence of three linked insns ending with this one. */
1192 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1194 rtx link = XEXP (links, 0);
1196 /* If the linked insn has been replaced by a note, then there
1197 is no point in pursuing this chain any further. */
1198 if (NOTE_P (link))
1199 continue;
1201 for (nextlinks = LOG_LINKS (link);
1202 nextlinks;
1203 nextlinks = XEXP (nextlinks, 1))
1204 if ((next = try_combine (insn, link, XEXP (nextlinks, 0),
1205 NULL_RTX,
1206 &new_direct_jump_p)) != 0)
1207 goto retry;
1210 #ifdef HAVE_cc0
1211 /* Try to combine a jump insn that uses CC0
1212 with a preceding insn that sets CC0, and maybe with its
1213 logical predecessor as well.
1214 This is how we make decrement-and-branch insns.
1215 We need this special code because data flow connections
1216 via CC0 do not get entered in LOG_LINKS. */
1218 if (JUMP_P (insn)
1219 && (prev = prev_nonnote_insn (insn)) != 0
1220 && NONJUMP_INSN_P (prev)
1221 && sets_cc0_p (PATTERN (prev)))
1223 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1224 &new_direct_jump_p)) != 0)
1225 goto retry;
1227 for (nextlinks = LOG_LINKS (prev); nextlinks;
1228 nextlinks = XEXP (nextlinks, 1))
1229 if ((next = try_combine (insn, prev, XEXP (nextlinks, 0),
1230 NULL_RTX,
1231 &new_direct_jump_p)) != 0)
1232 goto retry;
1235 /* Do the same for an insn that explicitly references CC0. */
1236 if (NONJUMP_INSN_P (insn)
1237 && (prev = prev_nonnote_insn (insn)) != 0
1238 && NONJUMP_INSN_P (prev)
1239 && sets_cc0_p (PATTERN (prev))
1240 && GET_CODE (PATTERN (insn)) == SET
1241 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1243 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1244 &new_direct_jump_p)) != 0)
1245 goto retry;
1247 for (nextlinks = LOG_LINKS (prev); nextlinks;
1248 nextlinks = XEXP (nextlinks, 1))
1249 if ((next = try_combine (insn, prev, XEXP (nextlinks, 0),
1250 NULL_RTX,
1251 &new_direct_jump_p)) != 0)
1252 goto retry;
1255 /* Finally, see if any of the insns that this insn links to
1256 explicitly references CC0. If so, try this insn, that insn,
1257 and its predecessor if it sets CC0. */
1258 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1259 if (NONJUMP_INSN_P (XEXP (links, 0))
1260 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1261 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1262 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1263 && NONJUMP_INSN_P (prev)
1264 && sets_cc0_p (PATTERN (prev))
1265 && (next = try_combine (insn, XEXP (links, 0),
1266 prev, NULL_RTX,
1267 &new_direct_jump_p)) != 0)
1268 goto retry;
1269 #endif
1271 /* Try combining an insn with two different insns whose results it
1272 uses. */
1273 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1274 for (nextlinks = XEXP (links, 1); nextlinks;
1275 nextlinks = XEXP (nextlinks, 1))
1276 if ((next = try_combine (insn, XEXP (links, 0),
1277 XEXP (nextlinks, 0), NULL_RTX,
1278 &new_direct_jump_p)) != 0)
1279 goto retry;
1281 /* Try four-instruction combinations. */
1282 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1284 rtx next1;
1285 rtx link = XEXP (links, 0);
1287 /* If the linked insn has been replaced by a note, then there
1288 is no point in pursuing this chain any further. */
1289 if (NOTE_P (link))
1290 continue;
1292 for (next1 = LOG_LINKS (link); next1; next1 = XEXP (next1, 1))
1294 rtx link1 = XEXP (next1, 0);
1295 if (NOTE_P (link1))
1296 continue;
1297 /* I0 -> I1 -> I2 -> I3. */
1298 for (nextlinks = LOG_LINKS (link1); nextlinks;
1299 nextlinks = XEXP (nextlinks, 1))
1300 if ((next = try_combine (insn, link, link1,
1301 XEXP (nextlinks, 0),
1302 &new_direct_jump_p)) != 0)
1303 goto retry;
1304 /* I0, I1 -> I2, I2 -> I3. */
1305 for (nextlinks = XEXP (next1, 1); nextlinks;
1306 nextlinks = XEXP (nextlinks, 1))
1307 if ((next = try_combine (insn, link, link1,
1308 XEXP (nextlinks, 0),
1309 &new_direct_jump_p)) != 0)
1310 goto retry;
1313 for (next1 = XEXP (links, 1); next1; next1 = XEXP (next1, 1))
1315 rtx link1 = XEXP (next1, 0);
1316 if (NOTE_P (link1))
1317 continue;
1318 /* I0 -> I2; I1, I2 -> I3. */
1319 for (nextlinks = LOG_LINKS (link); nextlinks;
1320 nextlinks = XEXP (nextlinks, 1))
1321 if ((next = try_combine (insn, link, link1,
1322 XEXP (nextlinks, 0),
1323 &new_direct_jump_p)) != 0)
1324 goto retry;
1325 /* I0 -> I1; I1, I2 -> I3. */
1326 for (nextlinks = LOG_LINKS (link1); nextlinks;
1327 nextlinks = XEXP (nextlinks, 1))
1328 if ((next = try_combine (insn, link, link1,
1329 XEXP (nextlinks, 0),
1330 &new_direct_jump_p)) != 0)
1331 goto retry;
1335 /* Try this insn with each REG_EQUAL note it links back to. */
1336 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1338 rtx set, note;
1339 rtx temp = XEXP (links, 0);
1340 if ((set = single_set (temp)) != 0
1341 && (note = find_reg_equal_equiv_note (temp)) != 0
1342 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1343 /* Avoid using a register that may already been marked
1344 dead by an earlier instruction. */
1345 && ! unmentioned_reg_p (note, SET_SRC (set))
1346 && (GET_MODE (note) == VOIDmode
1347 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1348 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1350 /* Temporarily replace the set's source with the
1351 contents of the REG_EQUAL note. The insn will
1352 be deleted or recognized by try_combine. */
1353 rtx orig = SET_SRC (set);
1354 SET_SRC (set) = note;
1355 i2mod = temp;
1356 i2mod_old_rhs = copy_rtx (orig);
1357 i2mod_new_rhs = copy_rtx (note);
1358 next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1359 &new_direct_jump_p);
1360 i2mod = NULL_RTX;
1361 if (next)
1362 goto retry;
1363 SET_SRC (set) = orig;
1367 if (!NOTE_P (insn))
1368 record_dead_and_set_regs (insn);
1370 retry:
1376 default_rtl_profile ();
1377 clear_log_links ();
1378 clear_bb_flags ();
1379 new_direct_jump_p |= purge_all_dead_edges ();
1380 delete_noop_moves ();
1382 /* Clean up. */
1383 free (uid_log_links);
1384 free (uid_insn_cost);
1385 VEC_free (reg_stat_type, heap, reg_stat);
1388 struct undo *undo, *next;
1389 for (undo = undobuf.frees; undo; undo = next)
1391 next = undo->next;
1392 free (undo);
1394 undobuf.frees = 0;
1397 total_attempts += combine_attempts;
1398 total_merges += combine_merges;
1399 total_extras += combine_extras;
1400 total_successes += combine_successes;
1402 nonzero_sign_valid = 0;
1403 rtl_hooks = general_rtl_hooks;
1405 /* Make recognizer allow volatile MEMs again. */
1406 init_recog ();
1408 return new_direct_jump_p;
1411 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1413 static void
1414 init_reg_last (void)
1416 unsigned int i;
1417 reg_stat_type *p;
1419 FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
1420 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1423 /* Set up any promoted values for incoming argument registers. */
1425 static void
1426 setup_incoming_promotions (rtx first)
1428 tree arg;
1429 bool strictly_local = false;
1431 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1432 arg = DECL_CHAIN (arg))
1434 rtx x, reg = DECL_INCOMING_RTL (arg);
1435 int uns1, uns3;
1436 enum machine_mode mode1, mode2, mode3, mode4;
1438 /* Only continue if the incoming argument is in a register. */
1439 if (!REG_P (reg))
1440 continue;
1442 /* Determine, if possible, whether all call sites of the current
1443 function lie within the current compilation unit. (This does
1444 take into account the exporting of a function via taking its
1445 address, and so forth.) */
1446 strictly_local = cgraph_local_info (current_function_decl)->local;
1448 /* The mode and signedness of the argument before any promotions happen
1449 (equal to the mode of the pseudo holding it at that stage). */
1450 mode1 = TYPE_MODE (TREE_TYPE (arg));
1451 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1453 /* The mode and signedness of the argument after any source language and
1454 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1455 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1456 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1458 /* The mode and signedness of the argument as it is actually passed,
1459 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1460 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1461 TREE_TYPE (cfun->decl), 0);
1463 /* The mode of the register in which the argument is being passed. */
1464 mode4 = GET_MODE (reg);
1466 /* Eliminate sign extensions in the callee when:
1467 (a) A mode promotion has occurred; */
1468 if (mode1 == mode3)
1469 continue;
1470 /* (b) The mode of the register is the same as the mode of
1471 the argument as it is passed; */
1472 if (mode3 != mode4)
1473 continue;
1474 /* (c) There's no language level extension; */
1475 if (mode1 == mode2)
1477 /* (c.1) All callers are from the current compilation unit. If that's
1478 the case we don't have to rely on an ABI, we only have to know
1479 what we're generating right now, and we know that we will do the
1480 mode1 to mode2 promotion with the given sign. */
1481 else if (!strictly_local)
1482 continue;
1483 /* (c.2) The combination of the two promotions is useful. This is
1484 true when the signs match, or if the first promotion is unsigned.
1485 In the later case, (sign_extend (zero_extend x)) is the same as
1486 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1487 else if (uns1)
1488 uns3 = true;
1489 else if (uns3)
1490 continue;
1492 /* Record that the value was promoted from mode1 to mode3,
1493 so that any sign extension at the head of the current
1494 function may be eliminated. */
1495 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1496 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1497 record_value_for_reg (reg, first, x);
1501 /* Called via note_stores. If X is a pseudo that is narrower than
1502 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1504 If we are setting only a portion of X and we can't figure out what
1505 portion, assume all bits will be used since we don't know what will
1506 be happening.
1508 Similarly, set how many bits of X are known to be copies of the sign bit
1509 at all locations in the function. This is the smallest number implied
1510 by any set of X. */
1512 static void
1513 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1515 rtx insn = (rtx) data;
1516 unsigned int num;
1518 if (REG_P (x)
1519 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1520 /* If this register is undefined at the start of the file, we can't
1521 say what its contents were. */
1522 && ! REGNO_REG_SET_P
1523 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1524 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1526 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1528 if (set == 0 || GET_CODE (set) == CLOBBER)
1530 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1531 rsp->sign_bit_copies = 1;
1532 return;
1535 /* If this register is being initialized using itself, and the
1536 register is uninitialized in this basic block, and there are
1537 no LOG_LINKS which set the register, then part of the
1538 register is uninitialized. In that case we can't assume
1539 anything about the number of nonzero bits.
1541 ??? We could do better if we checked this in
1542 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1543 could avoid making assumptions about the insn which initially
1544 sets the register, while still using the information in other
1545 insns. We would have to be careful to check every insn
1546 involved in the combination. */
1548 if (insn
1549 && reg_referenced_p (x, PATTERN (insn))
1550 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1551 REGNO (x)))
1553 rtx link;
1555 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1557 if (dead_or_set_p (XEXP (link, 0), x))
1558 break;
1560 if (!link)
1562 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1563 rsp->sign_bit_copies = 1;
1564 return;
1568 /* If this is a complex assignment, see if we can convert it into a
1569 simple assignment. */
1570 set = expand_field_assignment (set);
1572 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1573 set what we know about X. */
1575 if (SET_DEST (set) == x
1576 || (GET_CODE (SET_DEST (set)) == SUBREG
1577 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1578 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1579 && SUBREG_REG (SET_DEST (set)) == x))
1581 rtx src = SET_SRC (set);
1583 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1584 /* If X is narrower than a word and SRC is a non-negative
1585 constant that would appear negative in the mode of X,
1586 sign-extend it for use in reg_stat[].nonzero_bits because some
1587 machines (maybe most) will actually do the sign-extension
1588 and this is the conservative approach.
1590 ??? For 2.5, try to tighten up the MD files in this regard
1591 instead of this kludge. */
1593 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1594 && CONST_INT_P (src)
1595 && INTVAL (src) > 0
1596 && 0 != (UINTVAL (src)
1597 & ((unsigned HOST_WIDE_INT) 1
1598 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1599 src = GEN_INT (UINTVAL (src)
1600 | ((unsigned HOST_WIDE_INT) (-1)
1601 << GET_MODE_BITSIZE (GET_MODE (x))));
1602 #endif
1604 /* Don't call nonzero_bits if it cannot change anything. */
1605 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1606 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1607 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1608 if (rsp->sign_bit_copies == 0
1609 || rsp->sign_bit_copies > num)
1610 rsp->sign_bit_copies = num;
1612 else
1614 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1615 rsp->sign_bit_copies = 1;
1620 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1621 optionally insns that were previously combined into I3 or that will be
1622 combined into the merger of INSN and I3. The order is PRED, PRED2,
1623 INSN, SUCC, SUCC2, I3.
1625 Return 0 if the combination is not allowed for any reason.
1627 If the combination is allowed, *PDEST will be set to the single
1628 destination of INSN and *PSRC to the single source, and this function
1629 will return 1. */
1631 static int
1632 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1633 rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1634 rtx *pdest, rtx *psrc)
1636 int i;
1637 const_rtx set = 0;
1638 rtx src, dest;
1639 rtx p;
1640 #ifdef AUTO_INC_DEC
1641 rtx link;
1642 #endif
1643 bool all_adjacent = true;
1645 if (succ)
1647 if (succ2)
1649 if (next_active_insn (succ2) != i3)
1650 all_adjacent = false;
1651 if (next_active_insn (succ) != succ2)
1652 all_adjacent = false;
1654 else if (next_active_insn (succ) != i3)
1655 all_adjacent = false;
1656 if (next_active_insn (insn) != succ)
1657 all_adjacent = false;
1659 else if (next_active_insn (insn) != i3)
1660 all_adjacent = false;
1662 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1663 or a PARALLEL consisting of such a SET and CLOBBERs.
1665 If INSN has CLOBBER parallel parts, ignore them for our processing.
1666 By definition, these happen during the execution of the insn. When it
1667 is merged with another insn, all bets are off. If they are, in fact,
1668 needed and aren't also supplied in I3, they may be added by
1669 recog_for_combine. Otherwise, it won't match.
1671 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1672 note.
1674 Get the source and destination of INSN. If more than one, can't
1675 combine. */
1677 if (GET_CODE (PATTERN (insn)) == SET)
1678 set = PATTERN (insn);
1679 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1680 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1682 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1684 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1686 switch (GET_CODE (elt))
1688 /* This is important to combine floating point insns
1689 for the SH4 port. */
1690 case USE:
1691 /* Combining an isolated USE doesn't make sense.
1692 We depend here on combinable_i3pat to reject them. */
1693 /* The code below this loop only verifies that the inputs of
1694 the SET in INSN do not change. We call reg_set_between_p
1695 to verify that the REG in the USE does not change between
1696 I3 and INSN.
1697 If the USE in INSN was for a pseudo register, the matching
1698 insn pattern will likely match any register; combining this
1699 with any other USE would only be safe if we knew that the
1700 used registers have identical values, or if there was
1701 something to tell them apart, e.g. different modes. For
1702 now, we forgo such complicated tests and simply disallow
1703 combining of USES of pseudo registers with any other USE. */
1704 if (REG_P (XEXP (elt, 0))
1705 && GET_CODE (PATTERN (i3)) == PARALLEL)
1707 rtx i3pat = PATTERN (i3);
1708 int i = XVECLEN (i3pat, 0) - 1;
1709 unsigned int regno = REGNO (XEXP (elt, 0));
1713 rtx i3elt = XVECEXP (i3pat, 0, i);
1715 if (GET_CODE (i3elt) == USE
1716 && REG_P (XEXP (i3elt, 0))
1717 && (REGNO (XEXP (i3elt, 0)) == regno
1718 ? reg_set_between_p (XEXP (elt, 0),
1719 PREV_INSN (insn), i3)
1720 : regno >= FIRST_PSEUDO_REGISTER))
1721 return 0;
1723 while (--i >= 0);
1725 break;
1727 /* We can ignore CLOBBERs. */
1728 case CLOBBER:
1729 break;
1731 case SET:
1732 /* Ignore SETs whose result isn't used but not those that
1733 have side-effects. */
1734 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1735 && insn_nothrow_p (insn)
1736 && !side_effects_p (elt))
1737 break;
1739 /* If we have already found a SET, this is a second one and
1740 so we cannot combine with this insn. */
1741 if (set)
1742 return 0;
1744 set = elt;
1745 break;
1747 default:
1748 /* Anything else means we can't combine. */
1749 return 0;
1753 if (set == 0
1754 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1755 so don't do anything with it. */
1756 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1757 return 0;
1759 else
1760 return 0;
1762 if (set == 0)
1763 return 0;
1765 set = expand_field_assignment (set);
1766 src = SET_SRC (set), dest = SET_DEST (set);
1768 /* Don't eliminate a store in the stack pointer. */
1769 if (dest == stack_pointer_rtx
1770 /* Don't combine with an insn that sets a register to itself if it has
1771 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1772 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1773 /* Can't merge an ASM_OPERANDS. */
1774 || GET_CODE (src) == ASM_OPERANDS
1775 /* Can't merge a function call. */
1776 || GET_CODE (src) == CALL
1777 /* Don't eliminate a function call argument. */
1778 || (CALL_P (i3)
1779 && (find_reg_fusage (i3, USE, dest)
1780 || (REG_P (dest)
1781 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1782 && global_regs[REGNO (dest)])))
1783 /* Don't substitute into an incremented register. */
1784 || FIND_REG_INC_NOTE (i3, dest)
1785 || (succ && FIND_REG_INC_NOTE (succ, dest))
1786 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1787 /* Don't substitute into a non-local goto, this confuses CFG. */
1788 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1789 /* Make sure that DEST is not used after SUCC but before I3. */
1790 || (!all_adjacent
1791 && ((succ2
1792 && (reg_used_between_p (dest, succ2, i3)
1793 || reg_used_between_p (dest, succ, succ2)))
1794 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1795 /* Make sure that the value that is to be substituted for the register
1796 does not use any registers whose values alter in between. However,
1797 If the insns are adjacent, a use can't cross a set even though we
1798 think it might (this can happen for a sequence of insns each setting
1799 the same destination; last_set of that register might point to
1800 a NOTE). If INSN has a REG_EQUIV note, the register is always
1801 equivalent to the memory so the substitution is valid even if there
1802 are intervening stores. Also, don't move a volatile asm or
1803 UNSPEC_VOLATILE across any other insns. */
1804 || (! all_adjacent
1805 && (((!MEM_P (src)
1806 || ! find_reg_note (insn, REG_EQUIV, src))
1807 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1808 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1809 || GET_CODE (src) == UNSPEC_VOLATILE))
1810 /* Don't combine across a CALL_INSN, because that would possibly
1811 change whether the life span of some REGs crosses calls or not,
1812 and it is a pain to update that information.
1813 Exception: if source is a constant, moving it later can't hurt.
1814 Accept that as a special case. */
1815 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1816 return 0;
1818 /* DEST must either be a REG or CC0. */
1819 if (REG_P (dest))
1821 /* If register alignment is being enforced for multi-word items in all
1822 cases except for parameters, it is possible to have a register copy
1823 insn referencing a hard register that is not allowed to contain the
1824 mode being copied and which would not be valid as an operand of most
1825 insns. Eliminate this problem by not combining with such an insn.
1827 Also, on some machines we don't want to extend the life of a hard
1828 register. */
1830 if (REG_P (src)
1831 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1832 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1833 /* Don't extend the life of a hard register unless it is
1834 user variable (if we have few registers) or it can't
1835 fit into the desired register (meaning something special
1836 is going on).
1837 Also avoid substituting a return register into I3, because
1838 reload can't handle a conflict with constraints of other
1839 inputs. */
1840 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1841 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1842 return 0;
1844 else if (GET_CODE (dest) != CC0)
1845 return 0;
1848 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1849 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1850 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1852 /* Don't substitute for a register intended as a clobberable
1853 operand. */
1854 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1855 if (rtx_equal_p (reg, dest))
1856 return 0;
1858 /* If the clobber represents an earlyclobber operand, we must not
1859 substitute an expression containing the clobbered register.
1860 As we do not analyze the constraint strings here, we have to
1861 make the conservative assumption. However, if the register is
1862 a fixed hard reg, the clobber cannot represent any operand;
1863 we leave it up to the machine description to either accept or
1864 reject use-and-clobber patterns. */
1865 if (!REG_P (reg)
1866 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1867 || !fixed_regs[REGNO (reg)])
1868 if (reg_overlap_mentioned_p (reg, src))
1869 return 0;
1872 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1873 or not), reject, unless nothing volatile comes between it and I3 */
1875 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1877 /* Make sure neither succ nor succ2 contains a volatile reference. */
1878 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1879 return 0;
1880 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1881 return 0;
1882 /* We'll check insns between INSN and I3 below. */
1885 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1886 to be an explicit register variable, and was chosen for a reason. */
1888 if (GET_CODE (src) == ASM_OPERANDS
1889 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1890 return 0;
1892 /* If there are any volatile insns between INSN and I3, reject, because
1893 they might affect machine state. */
1895 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1896 if (INSN_P (p) && p != succ && p != succ2 && volatile_insn_p (PATTERN (p)))
1897 return 0;
1899 /* If INSN contains an autoincrement or autodecrement, make sure that
1900 register is not used between there and I3, and not already used in
1901 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1902 Also insist that I3 not be a jump; if it were one
1903 and the incremented register were spilled, we would lose. */
1905 #ifdef AUTO_INC_DEC
1906 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1907 if (REG_NOTE_KIND (link) == REG_INC
1908 && (JUMP_P (i3)
1909 || reg_used_between_p (XEXP (link, 0), insn, i3)
1910 || (pred != NULL_RTX
1911 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1912 || (pred2 != NULL_RTX
1913 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1914 || (succ != NULL_RTX
1915 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1916 || (succ2 != NULL_RTX
1917 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
1918 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1919 return 0;
1920 #endif
1922 #ifdef HAVE_cc0
1923 /* Don't combine an insn that follows a CC0-setting insn.
1924 An insn that uses CC0 must not be separated from the one that sets it.
1925 We do, however, allow I2 to follow a CC0-setting insn if that insn
1926 is passed as I1; in that case it will be deleted also.
1927 We also allow combining in this case if all the insns are adjacent
1928 because that would leave the two CC0 insns adjacent as well.
1929 It would be more logical to test whether CC0 occurs inside I1 or I2,
1930 but that would be much slower, and this ought to be equivalent. */
1932 p = prev_nonnote_insn (insn);
1933 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1934 && ! all_adjacent)
1935 return 0;
1936 #endif
1938 /* If we get here, we have passed all the tests and the combination is
1939 to be allowed. */
1941 *pdest = dest;
1942 *psrc = src;
1944 return 1;
1947 /* LOC is the location within I3 that contains its pattern or the component
1948 of a PARALLEL of the pattern. We validate that it is valid for combining.
1950 One problem is if I3 modifies its output, as opposed to replacing it
1951 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
1952 doing so would produce an insn that is not equivalent to the original insns.
1954 Consider:
1956 (set (reg:DI 101) (reg:DI 100))
1957 (set (subreg:SI (reg:DI 101) 0) <foo>)
1959 This is NOT equivalent to:
1961 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1962 (set (reg:DI 101) (reg:DI 100))])
1964 Not only does this modify 100 (in which case it might still be valid
1965 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1967 We can also run into a problem if I2 sets a register that I1
1968 uses and I1 gets directly substituted into I3 (not via I2). In that
1969 case, we would be getting the wrong value of I2DEST into I3, so we
1970 must reject the combination. This case occurs when I2 and I1 both
1971 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1972 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1973 of a SET must prevent combination from occurring. The same situation
1974 can occur for I0, in which case I0_NOT_IN_SRC is set.
1976 Before doing the above check, we first try to expand a field assignment
1977 into a set of logical operations.
1979 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1980 we place a register that is both set and used within I3. If more than one
1981 such register is detected, we fail.
1983 Return 1 if the combination is valid, zero otherwise. */
1985 static int
1986 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
1987 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
1989 rtx x = *loc;
1991 if (GET_CODE (x) == SET)
1993 rtx set = x ;
1994 rtx dest = SET_DEST (set);
1995 rtx src = SET_SRC (set);
1996 rtx inner_dest = dest;
1997 rtx subdest;
1999 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2000 || GET_CODE (inner_dest) == SUBREG
2001 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2002 inner_dest = XEXP (inner_dest, 0);
2004 /* Check for the case where I3 modifies its output, as discussed
2005 above. We don't want to prevent pseudos from being combined
2006 into the address of a MEM, so only prevent the combination if
2007 i1 or i2 set the same MEM. */
2008 if ((inner_dest != dest &&
2009 (!MEM_P (inner_dest)
2010 || rtx_equal_p (i2dest, inner_dest)
2011 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2012 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2013 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2014 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2015 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2017 /* This is the same test done in can_combine_p except we can't test
2018 all_adjacent; we don't have to, since this instruction will stay
2019 in place, thus we are not considering increasing the lifetime of
2020 INNER_DEST.
2022 Also, if this insn sets a function argument, combining it with
2023 something that might need a spill could clobber a previous
2024 function argument; the all_adjacent test in can_combine_p also
2025 checks this; here, we do a more specific test for this case. */
2027 || (REG_P (inner_dest)
2028 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2029 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2030 GET_MODE (inner_dest))))
2031 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2032 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2033 return 0;
2035 /* If DEST is used in I3, it is being killed in this insn, so
2036 record that for later. We have to consider paradoxical
2037 subregs here, since they kill the whole register, but we
2038 ignore partial subregs, STRICT_LOW_PART, etc.
2039 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2040 STACK_POINTER_REGNUM, since these are always considered to be
2041 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2042 subdest = dest;
2043 if (GET_CODE (subdest) == SUBREG
2044 && (GET_MODE_SIZE (GET_MODE (subdest))
2045 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2046 subdest = SUBREG_REG (subdest);
2047 if (pi3dest_killed
2048 && REG_P (subdest)
2049 && reg_referenced_p (subdest, PATTERN (i3))
2050 && REGNO (subdest) != FRAME_POINTER_REGNUM
2051 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2052 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2053 #endif
2054 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2055 && (REGNO (subdest) != ARG_POINTER_REGNUM
2056 || ! fixed_regs [REGNO (subdest)])
2057 #endif
2058 && REGNO (subdest) != STACK_POINTER_REGNUM)
2060 if (*pi3dest_killed)
2061 return 0;
2063 *pi3dest_killed = subdest;
2067 else if (GET_CODE (x) == PARALLEL)
2069 int i;
2071 for (i = 0; i < XVECLEN (x, 0); i++)
2072 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2073 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2074 return 0;
2077 return 1;
2080 /* Return 1 if X is an arithmetic expression that contains a multiplication
2081 and division. We don't count multiplications by powers of two here. */
2083 static int
2084 contains_muldiv (rtx x)
2086 switch (GET_CODE (x))
2088 case MOD: case DIV: case UMOD: case UDIV:
2089 return 1;
2091 case MULT:
2092 return ! (CONST_INT_P (XEXP (x, 1))
2093 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2094 default:
2095 if (BINARY_P (x))
2096 return contains_muldiv (XEXP (x, 0))
2097 || contains_muldiv (XEXP (x, 1));
2099 if (UNARY_P (x))
2100 return contains_muldiv (XEXP (x, 0));
2102 return 0;
2106 /* Determine whether INSN can be used in a combination. Return nonzero if
2107 not. This is used in try_combine to detect early some cases where we
2108 can't perform combinations. */
2110 static int
2111 cant_combine_insn_p (rtx insn)
2113 rtx set;
2114 rtx src, dest;
2116 /* If this isn't really an insn, we can't do anything.
2117 This can occur when flow deletes an insn that it has merged into an
2118 auto-increment address. */
2119 if (! INSN_P (insn))
2120 return 1;
2122 /* Never combine loads and stores involving hard regs that are likely
2123 to be spilled. The register allocator can usually handle such
2124 reg-reg moves by tying. If we allow the combiner to make
2125 substitutions of likely-spilled regs, reload might die.
2126 As an exception, we allow combinations involving fixed regs; these are
2127 not available to the register allocator so there's no risk involved. */
2129 set = single_set (insn);
2130 if (! set)
2131 return 0;
2132 src = SET_SRC (set);
2133 dest = SET_DEST (set);
2134 if (GET_CODE (src) == SUBREG)
2135 src = SUBREG_REG (src);
2136 if (GET_CODE (dest) == SUBREG)
2137 dest = SUBREG_REG (dest);
2138 if (REG_P (src) && REG_P (dest)
2139 && ((HARD_REGISTER_P (src)
2140 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2141 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2142 || (HARD_REGISTER_P (dest)
2143 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2144 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2145 return 1;
2147 return 0;
2150 struct likely_spilled_retval_info
2152 unsigned regno, nregs;
2153 unsigned mask;
2156 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2157 hard registers that are known to be written to / clobbered in full. */
2158 static void
2159 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2161 struct likely_spilled_retval_info *const info =
2162 (struct likely_spilled_retval_info *) data;
2163 unsigned regno, nregs;
2164 unsigned new_mask;
2166 if (!REG_P (XEXP (set, 0)))
2167 return;
2168 regno = REGNO (x);
2169 if (regno >= info->regno + info->nregs)
2170 return;
2171 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2172 if (regno + nregs <= info->regno)
2173 return;
2174 new_mask = (2U << (nregs - 1)) - 1;
2175 if (regno < info->regno)
2176 new_mask >>= info->regno - regno;
2177 else
2178 new_mask <<= regno - info->regno;
2179 info->mask &= ~new_mask;
2182 /* Return nonzero iff part of the return value is live during INSN, and
2183 it is likely spilled. This can happen when more than one insn is needed
2184 to copy the return value, e.g. when we consider to combine into the
2185 second copy insn for a complex value. */
2187 static int
2188 likely_spilled_retval_p (rtx insn)
2190 rtx use = BB_END (this_basic_block);
2191 rtx reg, p;
2192 unsigned regno, nregs;
2193 /* We assume here that no machine mode needs more than
2194 32 hard registers when the value overlaps with a register
2195 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2196 unsigned mask;
2197 struct likely_spilled_retval_info info;
2199 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2200 return 0;
2201 reg = XEXP (PATTERN (use), 0);
2202 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2203 return 0;
2204 regno = REGNO (reg);
2205 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2206 if (nregs == 1)
2207 return 0;
2208 mask = (2U << (nregs - 1)) - 1;
2210 /* Disregard parts of the return value that are set later. */
2211 info.regno = regno;
2212 info.nregs = nregs;
2213 info.mask = mask;
2214 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2215 if (INSN_P (p))
2216 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2217 mask = info.mask;
2219 /* Check if any of the (probably) live return value registers is
2220 likely spilled. */
2221 nregs --;
2224 if ((mask & 1 << nregs)
2225 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2226 return 1;
2227 } while (nregs--);
2228 return 0;
2231 /* Adjust INSN after we made a change to its destination.
2233 Changing the destination can invalidate notes that say something about
2234 the results of the insn and a LOG_LINK pointing to the insn. */
2236 static void
2237 adjust_for_new_dest (rtx insn)
2239 /* For notes, be conservative and simply remove them. */
2240 remove_reg_equal_equiv_notes (insn);
2242 /* The new insn will have a destination that was previously the destination
2243 of an insn just above it. Call distribute_links to make a LOG_LINK from
2244 the next use of that destination. */
2245 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2247 df_insn_rescan (insn);
2250 /* Return TRUE if combine can reuse reg X in mode MODE.
2251 ADDED_SETS is nonzero if the original set is still required. */
2252 static bool
2253 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2255 unsigned int regno;
2257 if (!REG_P(x))
2258 return false;
2260 regno = REGNO (x);
2261 /* Allow hard registers if the new mode is legal, and occupies no more
2262 registers than the old mode. */
2263 if (regno < FIRST_PSEUDO_REGISTER)
2264 return (HARD_REGNO_MODE_OK (regno, mode)
2265 && (hard_regno_nregs[regno][GET_MODE (x)]
2266 >= hard_regno_nregs[regno][mode]));
2268 /* Or a pseudo that is only used once. */
2269 return (REG_N_SETS (regno) == 1 && !added_sets
2270 && !REG_USERVAR_P (x));
2274 /* Check whether X, the destination of a set, refers to part of
2275 the register specified by REG. */
2277 static bool
2278 reg_subword_p (rtx x, rtx reg)
2280 /* Check that reg is an integer mode register. */
2281 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2282 return false;
2284 if (GET_CODE (x) == STRICT_LOW_PART
2285 || GET_CODE (x) == ZERO_EXTRACT)
2286 x = XEXP (x, 0);
2288 return GET_CODE (x) == SUBREG
2289 && SUBREG_REG (x) == reg
2290 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2293 #ifdef AUTO_INC_DEC
2294 /* Replace auto-increment addressing modes with explicit operations to access
2295 the same addresses without modifying the corresponding registers. */
2297 static rtx
2298 cleanup_auto_inc_dec (rtx src, enum machine_mode mem_mode)
2300 rtx x = src;
2301 const RTX_CODE code = GET_CODE (x);
2302 int i;
2303 const char *fmt;
2305 switch (code)
2307 case REG:
2308 case CONST_INT:
2309 case CONST_DOUBLE:
2310 case CONST_FIXED:
2311 case CONST_VECTOR:
2312 case SYMBOL_REF:
2313 case CODE_LABEL:
2314 case PC:
2315 case CC0:
2316 case SCRATCH:
2317 /* SCRATCH must be shared because they represent distinct values. */
2318 return x;
2319 case CLOBBER:
2320 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
2321 return x;
2322 break;
2324 case CONST:
2325 if (shared_const_p (x))
2326 return x;
2327 break;
2329 case MEM:
2330 mem_mode = GET_MODE (x);
2331 break;
2333 case PRE_INC:
2334 case PRE_DEC:
2335 gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
2336 return gen_rtx_PLUS (GET_MODE (x),
2337 cleanup_auto_inc_dec (XEXP (x, 0), mem_mode),
2338 GEN_INT (code == PRE_INC
2339 ? GET_MODE_SIZE (mem_mode)
2340 : -GET_MODE_SIZE (mem_mode)));
2342 case POST_INC:
2343 case POST_DEC:
2344 case PRE_MODIFY:
2345 case POST_MODIFY:
2346 return cleanup_auto_inc_dec (code == PRE_MODIFY
2347 ? XEXP (x, 1) : XEXP (x, 0),
2348 mem_mode);
2350 default:
2351 break;
2354 /* Copy the various flags, fields, and other information. We assume
2355 that all fields need copying, and then clear the fields that should
2356 not be copied. That is the sensible default behavior, and forces
2357 us to explicitly document why we are *not* copying a flag. */
2358 x = shallow_copy_rtx (x);
2360 /* We do not copy the USED flag, which is used as a mark bit during
2361 walks over the RTL. */
2362 RTX_FLAG (x, used) = 0;
2364 /* We do not copy FRAME_RELATED for INSNs. */
2365 if (INSN_P (x))
2366 RTX_FLAG (x, frame_related) = 0;
2368 fmt = GET_RTX_FORMAT (code);
2369 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2370 if (fmt[i] == 'e')
2371 XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), mem_mode);
2372 else if (fmt[i] == 'E' || fmt[i] == 'V')
2374 int j;
2375 XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
2376 for (j = 0; j < XVECLEN (x, i); j++)
2377 XVECEXP (x, i, j)
2378 = cleanup_auto_inc_dec (XVECEXP (src, i, j), mem_mode);
2381 return x;
2383 #endif
2385 /* Auxiliary data structure for propagate_for_debug_stmt. */
2387 struct rtx_subst_pair
2389 rtx to;
2390 bool adjusted;
2393 /* DATA points to an rtx_subst_pair. Return the value that should be
2394 substituted. */
2396 static rtx
2397 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2399 struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2401 if (!rtx_equal_p (from, old_rtx))
2402 return NULL_RTX;
2403 if (!pair->adjusted)
2405 pair->adjusted = true;
2406 #ifdef AUTO_INC_DEC
2407 pair->to = cleanup_auto_inc_dec (pair->to, VOIDmode);
2408 #else
2409 pair->to = copy_rtx (pair->to);
2410 #endif
2411 pair->to = make_compound_operation (pair->to, SET);
2412 return pair->to;
2414 return copy_rtx (pair->to);
2417 /* Replace all the occurrences of DEST with SRC in DEBUG_INSNs between INSN
2418 and LAST. */
2420 static void
2421 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src)
2423 rtx next, loc;
2425 struct rtx_subst_pair p;
2426 p.to = src;
2427 p.adjusted = false;
2429 next = NEXT_INSN (insn);
2430 while (next != last)
2432 insn = next;
2433 next = NEXT_INSN (insn);
2434 if (DEBUG_INSN_P (insn))
2436 loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2437 dest, propagate_for_debug_subst, &p);
2438 if (loc == INSN_VAR_LOCATION_LOC (insn))
2439 continue;
2440 INSN_VAR_LOCATION_LOC (insn) = loc;
2441 df_insn_rescan (insn);
2446 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2447 Note that the INSN should be deleted *after* removing dead edges, so
2448 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2449 but not for a (set (pc) (label_ref FOO)). */
2451 static void
2452 update_cfg_for_uncondjump (rtx insn)
2454 basic_block bb = BLOCK_FOR_INSN (insn);
2455 bool at_end = (BB_END (bb) == insn);
2457 if (at_end)
2458 purge_dead_edges (bb);
2460 delete_insn (insn);
2461 if (at_end && EDGE_COUNT (bb->succs) == 1)
2463 rtx insn;
2465 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2467 /* Remove barriers from the footer if there are any. */
2468 for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
2469 if (BARRIER_P (insn))
2471 if (PREV_INSN (insn))
2472 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2473 else
2474 bb->il.rtl->footer = NEXT_INSN (insn);
2475 if (NEXT_INSN (insn))
2476 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2478 else if (LABEL_P (insn))
2479 break;
2483 /* Try to combine the insns I0, I1 and I2 into I3.
2484 Here I0, I1 and I2 appear earlier than I3.
2485 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2488 If we are combining more than two insns and the resulting insn is not
2489 recognized, try splitting it into two insns. If that happens, I2 and I3
2490 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2491 Otherwise, I0, I1 and I2 are pseudo-deleted.
2493 Return 0 if the combination does not work. Then nothing is changed.
2494 If we did the combination, return the insn at which combine should
2495 resume scanning.
2497 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2498 new direct jump instruction. */
2500 static rtx
2501 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p)
2503 /* New patterns for I3 and I2, respectively. */
2504 rtx newpat, newi2pat = 0;
2505 rtvec newpat_vec_with_clobbers = 0;
2506 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2507 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2508 dead. */
2509 int added_sets_0, added_sets_1, added_sets_2;
2510 /* Total number of SETs to put into I3. */
2511 int total_sets;
2512 /* Nonzero if I2's or I1's body now appears in I3. */
2513 int i2_is_used = 0, i1_is_used = 0;
2514 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2515 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2516 /* Contains I3 if the destination of I3 is used in its source, which means
2517 that the old life of I3 is being killed. If that usage is placed into
2518 I2 and not in I3, a REG_DEAD note must be made. */
2519 rtx i3dest_killed = 0;
2520 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2521 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2522 /* Copy of SET_SRC of I1, if needed. */
2523 rtx i1src_copy = 0;
2524 /* Set if I2DEST was reused as a scratch register. */
2525 bool i2scratch = false;
2526 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2527 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2528 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2529 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2530 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2531 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2532 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2533 /* Notes that must be added to REG_NOTES in I3 and I2. */
2534 rtx new_i3_notes, new_i2_notes;
2535 /* Notes that we substituted I3 into I2 instead of the normal case. */
2536 int i3_subst_into_i2 = 0;
2537 /* Notes that I1, I2 or I3 is a MULT operation. */
2538 int have_mult = 0;
2539 int swap_i2i3 = 0;
2540 int changed_i3_dest = 0;
2542 int maxreg;
2543 rtx temp;
2544 rtx link;
2545 rtx other_pat = 0;
2546 rtx new_other_notes;
2547 int i;
2549 /* Only try four-insn combinations when there's high likelihood of
2550 success. Look for simple insns, such as loads of constants or
2551 binary operations involving a constant. */
2552 if (i0)
2554 int i;
2555 int ngood = 0;
2556 int nshift = 0;
2558 if (!flag_expensive_optimizations)
2559 return 0;
2561 for (i = 0; i < 4; i++)
2563 rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2564 rtx set = single_set (insn);
2565 rtx src;
2566 if (!set)
2567 continue;
2568 src = SET_SRC (set);
2569 if (CONSTANT_P (src))
2571 ngood += 2;
2572 break;
2574 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2575 ngood++;
2576 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2577 || GET_CODE (src) == LSHIFTRT)
2578 nshift++;
2580 if (ngood < 2 && nshift < 2)
2581 return 0;
2584 /* Exit early if one of the insns involved can't be used for
2585 combinations. */
2586 if (cant_combine_insn_p (i3)
2587 || cant_combine_insn_p (i2)
2588 || (i1 && cant_combine_insn_p (i1))
2589 || (i0 && cant_combine_insn_p (i0))
2590 || likely_spilled_retval_p (i3))
2591 return 0;
2593 combine_attempts++;
2594 undobuf.other_insn = 0;
2596 /* Reset the hard register usage information. */
2597 CLEAR_HARD_REG_SET (newpat_used_regs);
2599 if (dump_file && (dump_flags & TDF_DETAILS))
2601 if (i0)
2602 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2603 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2604 else if (i1)
2605 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2606 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2607 else
2608 fprintf (dump_file, "\nTrying %d -> %d:\n",
2609 INSN_UID (i2), INSN_UID (i3));
2612 /* If multiple insns feed into one of I2 or I3, they can be in any
2613 order. To simplify the code below, reorder them in sequence. */
2614 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2615 temp = i2, i2 = i0, i0 = temp;
2616 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2617 temp = i1, i1 = i0, i0 = temp;
2618 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2619 temp = i1, i1 = i2, i2 = temp;
2621 added_links_insn = 0;
2623 /* First check for one important special case that the code below will
2624 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2625 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2626 we may be able to replace that destination with the destination of I3.
2627 This occurs in the common code where we compute both a quotient and
2628 remainder into a structure, in which case we want to do the computation
2629 directly into the structure to avoid register-register copies.
2631 Note that this case handles both multiple sets in I2 and also cases
2632 where I2 has a number of CLOBBERs inside the PARALLEL.
2634 We make very conservative checks below and only try to handle the
2635 most common cases of this. For example, we only handle the case
2636 where I2 and I3 are adjacent to avoid making difficult register
2637 usage tests. */
2639 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2640 && REG_P (SET_SRC (PATTERN (i3)))
2641 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2642 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2643 && GET_CODE (PATTERN (i2)) == PARALLEL
2644 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2645 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2646 below would need to check what is inside (and reg_overlap_mentioned_p
2647 doesn't support those codes anyway). Don't allow those destinations;
2648 the resulting insn isn't likely to be recognized anyway. */
2649 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2650 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2651 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2652 SET_DEST (PATTERN (i3)))
2653 && next_active_insn (i2) == i3)
2655 rtx p2 = PATTERN (i2);
2657 /* Make sure that the destination of I3,
2658 which we are going to substitute into one output of I2,
2659 is not used within another output of I2. We must avoid making this:
2660 (parallel [(set (mem (reg 69)) ...)
2661 (set (reg 69) ...)])
2662 which is not well-defined as to order of actions.
2663 (Besides, reload can't handle output reloads for this.)
2665 The problem can also happen if the dest of I3 is a memory ref,
2666 if another dest in I2 is an indirect memory ref. */
2667 for (i = 0; i < XVECLEN (p2, 0); i++)
2668 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2669 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2670 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2671 SET_DEST (XVECEXP (p2, 0, i))))
2672 break;
2674 if (i == XVECLEN (p2, 0))
2675 for (i = 0; i < XVECLEN (p2, 0); i++)
2676 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2677 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2679 combine_merges++;
2681 subst_insn = i3;
2682 subst_low_luid = DF_INSN_LUID (i2);
2684 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2685 i2src = SET_SRC (XVECEXP (p2, 0, i));
2686 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2687 i2dest_killed = dead_or_set_p (i2, i2dest);
2689 /* Replace the dest in I2 with our dest and make the resulting
2690 insn the new pattern for I3. Then skip to where we validate
2691 the pattern. Everything was set up above. */
2692 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2693 newpat = p2;
2694 i3_subst_into_i2 = 1;
2695 goto validate_replacement;
2699 /* If I2 is setting a pseudo to a constant and I3 is setting some
2700 sub-part of it to another constant, merge them by making a new
2701 constant. */
2702 if (i1 == 0
2703 && (temp = single_set (i2)) != 0
2704 && (CONST_INT_P (SET_SRC (temp))
2705 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2706 && GET_CODE (PATTERN (i3)) == SET
2707 && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2708 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2709 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2711 rtx dest = SET_DEST (PATTERN (i3));
2712 int offset = -1;
2713 int width = 0;
2715 if (GET_CODE (dest) == ZERO_EXTRACT)
2717 if (CONST_INT_P (XEXP (dest, 1))
2718 && CONST_INT_P (XEXP (dest, 2)))
2720 width = INTVAL (XEXP (dest, 1));
2721 offset = INTVAL (XEXP (dest, 2));
2722 dest = XEXP (dest, 0);
2723 if (BITS_BIG_ENDIAN)
2724 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2727 else
2729 if (GET_CODE (dest) == STRICT_LOW_PART)
2730 dest = XEXP (dest, 0);
2731 width = GET_MODE_BITSIZE (GET_MODE (dest));
2732 offset = 0;
2735 if (offset >= 0)
2737 /* If this is the low part, we're done. */
2738 if (subreg_lowpart_p (dest))
2740 /* Handle the case where inner is twice the size of outer. */
2741 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2742 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2743 offset += GET_MODE_BITSIZE (GET_MODE (dest));
2744 /* Otherwise give up for now. */
2745 else
2746 offset = -1;
2749 if (offset >= 0
2750 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2751 <= HOST_BITS_PER_DOUBLE_INT))
2753 double_int m, o, i;
2754 rtx inner = SET_SRC (PATTERN (i3));
2755 rtx outer = SET_SRC (temp);
2757 o = rtx_to_double_int (outer);
2758 i = rtx_to_double_int (inner);
2760 m = double_int_mask (width);
2761 i = double_int_and (i, m);
2762 m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2763 i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2764 o = double_int_ior (double_int_and_not (o, m), i);
2766 combine_merges++;
2767 subst_insn = i3;
2768 subst_low_luid = DF_INSN_LUID (i2);
2769 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2770 i2dest = SET_DEST (temp);
2771 i2dest_killed = dead_or_set_p (i2, i2dest);
2773 /* Replace the source in I2 with the new constant and make the
2774 resulting insn the new pattern for I3. Then skip to where we
2775 validate the pattern. Everything was set up above. */
2776 SUBST (SET_SRC (temp),
2777 immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2779 newpat = PATTERN (i2);
2781 /* The dest of I3 has been replaced with the dest of I2. */
2782 changed_i3_dest = 1;
2783 goto validate_replacement;
2787 #ifndef HAVE_cc0
2788 /* If we have no I1 and I2 looks like:
2789 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2790 (set Y OP)])
2791 make up a dummy I1 that is
2792 (set Y OP)
2793 and change I2 to be
2794 (set (reg:CC X) (compare:CC Y (const_int 0)))
2796 (We can ignore any trailing CLOBBERs.)
2798 This undoes a previous combination and allows us to match a branch-and-
2799 decrement insn. */
2801 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2802 && XVECLEN (PATTERN (i2), 0) >= 2
2803 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2804 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2805 == MODE_CC)
2806 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2807 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2808 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2809 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2810 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2811 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2813 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2814 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2815 break;
2817 if (i == 1)
2819 /* We make I1 with the same INSN_UID as I2. This gives it
2820 the same DF_INSN_LUID for value tracking. Our fake I1 will
2821 never appear in the insn stream so giving it the same INSN_UID
2822 as I2 will not cause a problem. */
2824 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2825 BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2826 INSN_LOCATOR (i2), -1, NULL_RTX);
2828 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2829 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2830 SET_DEST (PATTERN (i1)));
2833 #endif
2835 /* Verify that I2 and I1 are valid for combining. */
2836 if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2837 || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2838 &i1dest, &i1src))
2839 || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2840 &i0dest, &i0src)))
2842 undo_all ();
2843 return 0;
2846 /* Record whether I2DEST is used in I2SRC and similarly for the other
2847 cases. Knowing this will help in register status updating below. */
2848 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2849 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2850 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2851 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2852 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2853 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2854 i2dest_killed = dead_or_set_p (i2, i2dest);
2855 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2856 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2858 /* For the earlier insns, determine which of the subsequent ones they
2859 feed. */
2860 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2861 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2862 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2863 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2864 && reg_overlap_mentioned_p (i0dest, i2src))));
2866 /* Ensure that I3's pattern can be the destination of combines. */
2867 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2868 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2869 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2870 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2871 &i3dest_killed))
2873 undo_all ();
2874 return 0;
2877 /* See if any of the insns is a MULT operation. Unless one is, we will
2878 reject a combination that is, since it must be slower. Be conservative
2879 here. */
2880 if (GET_CODE (i2src) == MULT
2881 || (i1 != 0 && GET_CODE (i1src) == MULT)
2882 || (i0 != 0 && GET_CODE (i0src) == MULT)
2883 || (GET_CODE (PATTERN (i3)) == SET
2884 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2885 have_mult = 1;
2887 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2888 We used to do this EXCEPT in one case: I3 has a post-inc in an
2889 output operand. However, that exception can give rise to insns like
2890 mov r3,(r3)+
2891 which is a famous insn on the PDP-11 where the value of r3 used as the
2892 source was model-dependent. Avoid this sort of thing. */
2894 #if 0
2895 if (!(GET_CODE (PATTERN (i3)) == SET
2896 && REG_P (SET_SRC (PATTERN (i3)))
2897 && MEM_P (SET_DEST (PATTERN (i3)))
2898 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2899 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2900 /* It's not the exception. */
2901 #endif
2902 #ifdef AUTO_INC_DEC
2903 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2904 if (REG_NOTE_KIND (link) == REG_INC
2905 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2906 || (i1 != 0
2907 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2909 undo_all ();
2910 return 0;
2912 #endif
2914 /* See if the SETs in I1 or I2 need to be kept around in the merged
2915 instruction: whenever the value set there is still needed past I3.
2916 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2918 For the SET in I1, we have two cases: If I1 and I2 independently
2919 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2920 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2921 in I1 needs to be kept around unless I1DEST dies or is set in either
2922 I2 or I3. The same consideration applies to I0. */
2924 added_sets_2 = !dead_or_set_p (i3, i2dest);
2926 if (i1)
2927 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2928 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2929 else
2930 added_sets_1 = 0;
2932 if (i0)
2933 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2934 || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
2935 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
2936 else
2937 added_sets_0 = 0;
2939 /* We are about to copy insns for the case where they need to be kept
2940 around. Check that they can be copied in the merged instruction. */
2942 if (targetm.cannot_copy_insn_p
2943 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2944 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2945 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2947 undo_all ();
2948 return 0;
2951 /* If the set in I2 needs to be kept around, we must make a copy of
2952 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2953 PATTERN (I2), we are only substituting for the original I1DEST, not into
2954 an already-substituted copy. This also prevents making self-referential
2955 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2956 I2DEST. */
2958 if (added_sets_2)
2960 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2961 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2962 else
2963 i2pat = copy_rtx (PATTERN (i2));
2966 if (added_sets_1)
2968 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2969 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2970 else
2971 i1pat = copy_rtx (PATTERN (i1));
2974 if (added_sets_0)
2976 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2977 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2978 else
2979 i0pat = copy_rtx (PATTERN (i0));
2982 combine_merges++;
2984 /* Substitute in the latest insn for the regs set by the earlier ones. */
2986 maxreg = max_reg_num ();
2988 subst_insn = i3;
2990 #ifndef HAVE_cc0
2991 /* Many machines that don't use CC0 have insns that can both perform an
2992 arithmetic operation and set the condition code. These operations will
2993 be represented as a PARALLEL with the first element of the vector
2994 being a COMPARE of an arithmetic operation with the constant zero.
2995 The second element of the vector will set some pseudo to the result
2996 of the same arithmetic operation. If we simplify the COMPARE, we won't
2997 match such a pattern and so will generate an extra insn. Here we test
2998 for this case, where both the comparison and the operation result are
2999 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3000 I2SRC. Later we will make the PARALLEL that contains I2. */
3002 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3003 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3004 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
3005 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3007 #ifdef SELECT_CC_MODE
3008 rtx *cc_use;
3009 enum machine_mode compare_mode;
3010 #endif
3012 newpat = PATTERN (i3);
3013 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
3015 i2_is_used = 1;
3017 #ifdef SELECT_CC_MODE
3018 /* See if a COMPARE with the operand we substituted in should be done
3019 with the mode that is currently being used. If not, do the same
3020 processing we do in `subst' for a SET; namely, if the destination
3021 is used only once, try to replace it with a register of the proper
3022 mode and also replace the COMPARE. */
3023 if (undobuf.other_insn == 0
3024 && (cc_use = find_single_use (SET_DEST (newpat), i3,
3025 &undobuf.other_insn))
3026 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
3027 i2src, const0_rtx))
3028 != GET_MODE (SET_DEST (newpat))))
3030 if (can_change_dest_mode (SET_DEST (newpat), added_sets_2,
3031 compare_mode))
3033 unsigned int regno = REGNO (SET_DEST (newpat));
3034 rtx new_dest;
3036 if (regno < FIRST_PSEUDO_REGISTER)
3037 new_dest = gen_rtx_REG (compare_mode, regno);
3038 else
3040 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3041 new_dest = regno_reg_rtx[regno];
3044 SUBST (SET_DEST (newpat), new_dest);
3045 SUBST (XEXP (*cc_use, 0), new_dest);
3046 SUBST (SET_SRC (newpat),
3047 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
3049 else
3050 undobuf.other_insn = 0;
3052 #endif
3054 else
3055 #endif
3057 /* It is possible that the source of I2 or I1 may be performing
3058 an unneeded operation, such as a ZERO_EXTEND of something
3059 that is known to have the high part zero. Handle that case
3060 by letting subst look at the innermost one of them.
3062 Another way to do this would be to have a function that tries
3063 to simplify a single insn instead of merging two or more
3064 insns. We don't do this because of the potential of infinite
3065 loops and because of the potential extra memory required.
3066 However, doing it the way we are is a bit of a kludge and
3067 doesn't catch all cases.
3069 But only do this if -fexpensive-optimizations since it slows
3070 things down and doesn't usually win.
3072 This is not done in the COMPARE case above because the
3073 unmodified I2PAT is used in the PARALLEL and so a pattern
3074 with a modified I2SRC would not match. */
3076 if (flag_expensive_optimizations)
3078 /* Pass pc_rtx so no substitutions are done, just
3079 simplifications. */
3080 if (i1)
3082 subst_low_luid = DF_INSN_LUID (i1);
3083 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
3085 else
3087 subst_low_luid = DF_INSN_LUID (i2);
3088 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
3092 n_occurrences = 0; /* `subst' counts here */
3093 subst_low_luid = DF_INSN_LUID (i2);
3095 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3096 copy of I2SRC each time we substitute it, in order to avoid creating
3097 self-referential RTL when we will be substituting I1SRC for I1DEST
3098 later. Likewise if I0 feeds into I2, either directly or indirectly
3099 through I1, and I0DEST is in I0SRC. */
3100 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
3101 (i1_feeds_i2_n && i1dest_in_i1src)
3102 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3103 && i0dest_in_i0src));
3104 substed_i2 = 1;
3106 /* Record whether I2's body now appears within I3's body. */
3107 i2_is_used = n_occurrences;
3110 /* If we already got a failure, don't try to do more. Otherwise, try to
3111 substitute I1 if we have it. */
3113 if (i1 && GET_CODE (newpat) != CLOBBER)
3115 /* Check that an autoincrement side-effect on I1 has not been lost.
3116 This happens if I1DEST is mentioned in I2 and dies there, and
3117 has disappeared from the new pattern. */
3118 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3119 && i1_feeds_i2_n
3120 && dead_or_set_p (i2, i1dest)
3121 && !reg_overlap_mentioned_p (i1dest, newpat))
3122 /* Before we can do this substitution, we must redo the test done
3123 above (see detailed comments there) that ensures I1DEST isn't
3124 mentioned in any SETs in NEWPAT that are field assignments. */
3125 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3126 0, 0, 0))
3128 undo_all ();
3129 return 0;
3132 n_occurrences = 0;
3133 subst_low_luid = DF_INSN_LUID (i1);
3135 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3136 copy of I1SRC each time we substitute it, in order to avoid creating
3137 self-referential RTL when we will be substituting I0SRC for I0DEST
3138 later. */
3139 newpat = subst (newpat, i1dest, i1src, 0,
3140 i0_feeds_i1_n && i0dest_in_i0src);
3141 substed_i1 = 1;
3143 /* Record whether I1's body now appears within I3's body. */
3144 i1_is_used = n_occurrences;
3147 /* Likewise for I0 if we have it. */
3149 if (i0 && GET_CODE (newpat) != CLOBBER)
3151 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3152 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3153 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3154 && !reg_overlap_mentioned_p (i0dest, newpat))
3155 || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3156 0, 0, 0))
3158 undo_all ();
3159 return 0;
3162 /* If the following substitution will modify I1SRC, make a copy of it
3163 for the case where it is substituted for I1DEST in I2PAT later. */
3164 if (i0_feeds_i1_n && added_sets_2 && i1_feeds_i2_n)
3165 i1src_copy = copy_rtx (i1src);
3167 n_occurrences = 0;
3168 subst_low_luid = DF_INSN_LUID (i0);
3169 newpat = subst (newpat, i0dest, i0src, 0, 0);
3170 substed_i0 = 1;
3173 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3174 to count all the ways that I2SRC and I1SRC can be used. */
3175 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3176 && i2_is_used + added_sets_2 > 1)
3177 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3178 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3179 > 1))
3180 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3181 && (n_occurrences + added_sets_0
3182 + (added_sets_1 && i0_feeds_i1_n)
3183 + (added_sets_2 && i0_feeds_i2_n)
3184 > 1))
3185 /* Fail if we tried to make a new register. */
3186 || max_reg_num () != maxreg
3187 /* Fail if we couldn't do something and have a CLOBBER. */
3188 || GET_CODE (newpat) == CLOBBER
3189 /* Fail if this new pattern is a MULT and we didn't have one before
3190 at the outer level. */
3191 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3192 && ! have_mult))
3194 undo_all ();
3195 return 0;
3198 /* If the actions of the earlier insns must be kept
3199 in addition to substituting them into the latest one,
3200 we must make a new PARALLEL for the latest insn
3201 to hold additional the SETs. */
3203 if (added_sets_0 || added_sets_1 || added_sets_2)
3205 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3206 combine_extras++;
3208 if (GET_CODE (newpat) == PARALLEL)
3210 rtvec old = XVEC (newpat, 0);
3211 total_sets = XVECLEN (newpat, 0) + extra_sets;
3212 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3213 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3214 sizeof (old->elem[0]) * old->num_elem);
3216 else
3218 rtx old = newpat;
3219 total_sets = 1 + extra_sets;
3220 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3221 XVECEXP (newpat, 0, 0) = old;
3224 if (added_sets_0)
3225 XVECEXP (newpat, 0, --total_sets) = i0pat;
3227 if (added_sets_1)
3229 rtx t = i1pat;
3230 if (i0_feeds_i1_n)
3231 t = subst (t, i0dest, i0src, 0, 0);
3233 XVECEXP (newpat, 0, --total_sets) = t;
3235 if (added_sets_2)
3237 rtx t = i2pat;
3238 if (i1_feeds_i2_n)
3239 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0,
3240 i0_feeds_i1_n && i0dest_in_i0src);
3241 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3242 t = subst (t, i0dest, i0src, 0, 0);
3244 XVECEXP (newpat, 0, --total_sets) = t;
3248 validate_replacement:
3250 /* Note which hard regs this insn has as inputs. */
3251 mark_used_regs_combine (newpat);
3253 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3254 consider splitting this pattern, we might need these clobbers. */
3255 if (i1 && GET_CODE (newpat) == PARALLEL
3256 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3258 int len = XVECLEN (newpat, 0);
3260 newpat_vec_with_clobbers = rtvec_alloc (len);
3261 for (i = 0; i < len; i++)
3262 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3265 /* Is the result of combination a valid instruction? */
3266 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3268 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3269 the second SET's destination is a register that is unused and isn't
3270 marked as an instruction that might trap in an EH region. In that case,
3271 we just need the first SET. This can occur when simplifying a divmod
3272 insn. We *must* test for this case here because the code below that
3273 splits two independent SETs doesn't handle this case correctly when it
3274 updates the register status.
3276 It's pointless doing this if we originally had two sets, one from
3277 i3, and one from i2. Combining then splitting the parallel results
3278 in the original i2 again plus an invalid insn (which we delete).
3279 The net effect is only to move instructions around, which makes
3280 debug info less accurate.
3282 Also check the case where the first SET's destination is unused.
3283 That would not cause incorrect code, but does cause an unneeded
3284 insn to remain. */
3286 if (insn_code_number < 0
3287 && !(added_sets_2 && i1 == 0)
3288 && GET_CODE (newpat) == PARALLEL
3289 && XVECLEN (newpat, 0) == 2
3290 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3291 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3292 && asm_noperands (newpat) < 0)
3294 rtx set0 = XVECEXP (newpat, 0, 0);
3295 rtx set1 = XVECEXP (newpat, 0, 1);
3297 if (((REG_P (SET_DEST (set1))
3298 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3299 || (GET_CODE (SET_DEST (set1)) == SUBREG
3300 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3301 && insn_nothrow_p (i3)
3302 && !side_effects_p (SET_SRC (set1)))
3304 newpat = set0;
3305 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3308 else if (((REG_P (SET_DEST (set0))
3309 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3310 || (GET_CODE (SET_DEST (set0)) == SUBREG
3311 && find_reg_note (i3, REG_UNUSED,
3312 SUBREG_REG (SET_DEST (set0)))))
3313 && insn_nothrow_p (i3)
3314 && !side_effects_p (SET_SRC (set0)))
3316 newpat = set1;
3317 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3319 if (insn_code_number >= 0)
3320 changed_i3_dest = 1;
3324 /* If we were combining three insns and the result is a simple SET
3325 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3326 insns. There are two ways to do this. It can be split using a
3327 machine-specific method (like when you have an addition of a large
3328 constant) or by combine in the function find_split_point. */
3330 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3331 && asm_noperands (newpat) < 0)
3333 rtx parallel, m_split, *split;
3335 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3336 use I2DEST as a scratch register will help. In the latter case,
3337 convert I2DEST to the mode of the source of NEWPAT if we can. */
3339 m_split = combine_split_insns (newpat, i3);
3341 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3342 inputs of NEWPAT. */
3344 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3345 possible to try that as a scratch reg. This would require adding
3346 more code to make it work though. */
3348 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3350 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3352 /* First try to split using the original register as a
3353 scratch register. */
3354 parallel = gen_rtx_PARALLEL (VOIDmode,
3355 gen_rtvec (2, newpat,
3356 gen_rtx_CLOBBER (VOIDmode,
3357 i2dest)));
3358 m_split = combine_split_insns (parallel, i3);
3360 /* If that didn't work, try changing the mode of I2DEST if
3361 we can. */
3362 if (m_split == 0
3363 && new_mode != GET_MODE (i2dest)
3364 && new_mode != VOIDmode
3365 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3367 enum machine_mode old_mode = GET_MODE (i2dest);
3368 rtx ni2dest;
3370 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3371 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3372 else
3374 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3375 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3378 parallel = (gen_rtx_PARALLEL
3379 (VOIDmode,
3380 gen_rtvec (2, newpat,
3381 gen_rtx_CLOBBER (VOIDmode,
3382 ni2dest))));
3383 m_split = combine_split_insns (parallel, i3);
3385 if (m_split == 0
3386 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3388 struct undo *buf;
3390 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3391 buf = undobuf.undos;
3392 undobuf.undos = buf->next;
3393 buf->next = undobuf.frees;
3394 undobuf.frees = buf;
3398 i2scratch = m_split != 0;
3401 /* If recog_for_combine has discarded clobbers, try to use them
3402 again for the split. */
3403 if (m_split == 0 && newpat_vec_with_clobbers)
3405 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3406 m_split = combine_split_insns (parallel, i3);
3409 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3411 m_split = PATTERN (m_split);
3412 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3413 if (insn_code_number >= 0)
3414 newpat = m_split;
3416 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3417 && (next_real_insn (i2) == i3
3418 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3420 rtx i2set, i3set;
3421 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3422 newi2pat = PATTERN (m_split);
3424 i3set = single_set (NEXT_INSN (m_split));
3425 i2set = single_set (m_split);
3427 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3429 /* If I2 or I3 has multiple SETs, we won't know how to track
3430 register status, so don't use these insns. If I2's destination
3431 is used between I2 and I3, we also can't use these insns. */
3433 if (i2_code_number >= 0 && i2set && i3set
3434 && (next_real_insn (i2) == i3
3435 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3436 insn_code_number = recog_for_combine (&newi3pat, i3,
3437 &new_i3_notes);
3438 if (insn_code_number >= 0)
3439 newpat = newi3pat;
3441 /* It is possible that both insns now set the destination of I3.
3442 If so, we must show an extra use of it. */
3444 if (insn_code_number >= 0)
3446 rtx new_i3_dest = SET_DEST (i3set);
3447 rtx new_i2_dest = SET_DEST (i2set);
3449 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3450 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3451 || GET_CODE (new_i3_dest) == SUBREG)
3452 new_i3_dest = XEXP (new_i3_dest, 0);
3454 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3455 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3456 || GET_CODE (new_i2_dest) == SUBREG)
3457 new_i2_dest = XEXP (new_i2_dest, 0);
3459 if (REG_P (new_i3_dest)
3460 && REG_P (new_i2_dest)
3461 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3462 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3466 /* If we can split it and use I2DEST, go ahead and see if that
3467 helps things be recognized. Verify that none of the registers
3468 are set between I2 and I3. */
3469 if (insn_code_number < 0
3470 && (split = find_split_point (&newpat, i3, false)) != 0
3471 #ifdef HAVE_cc0
3472 && REG_P (i2dest)
3473 #endif
3474 /* We need I2DEST in the proper mode. If it is a hard register
3475 or the only use of a pseudo, we can change its mode.
3476 Make sure we don't change a hard register to have a mode that
3477 isn't valid for it, or change the number of registers. */
3478 && (GET_MODE (*split) == GET_MODE (i2dest)
3479 || GET_MODE (*split) == VOIDmode
3480 || can_change_dest_mode (i2dest, added_sets_2,
3481 GET_MODE (*split)))
3482 && (next_real_insn (i2) == i3
3483 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3484 /* We can't overwrite I2DEST if its value is still used by
3485 NEWPAT. */
3486 && ! reg_referenced_p (i2dest, newpat))
3488 rtx newdest = i2dest;
3489 enum rtx_code split_code = GET_CODE (*split);
3490 enum machine_mode split_mode = GET_MODE (*split);
3491 bool subst_done = false;
3492 newi2pat = NULL_RTX;
3494 i2scratch = true;
3496 /* *SPLIT may be part of I2SRC, so make sure we have the
3497 original expression around for later debug processing.
3498 We should not need I2SRC any more in other cases. */
3499 if (MAY_HAVE_DEBUG_INSNS)
3500 i2src = copy_rtx (i2src);
3501 else
3502 i2src = NULL;
3504 /* Get NEWDEST as a register in the proper mode. We have already
3505 validated that we can do this. */
3506 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3508 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3509 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3510 else
3512 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3513 newdest = regno_reg_rtx[REGNO (i2dest)];
3517 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3518 an ASHIFT. This can occur if it was inside a PLUS and hence
3519 appeared to be a memory address. This is a kludge. */
3520 if (split_code == MULT
3521 && CONST_INT_P (XEXP (*split, 1))
3522 && INTVAL (XEXP (*split, 1)) > 0
3523 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3525 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3526 XEXP (*split, 0), GEN_INT (i)));
3527 /* Update split_code because we may not have a multiply
3528 anymore. */
3529 split_code = GET_CODE (*split);
3532 #ifdef INSN_SCHEDULING
3533 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3534 be written as a ZERO_EXTEND. */
3535 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3537 #ifdef LOAD_EXTEND_OP
3538 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3539 what it really is. */
3540 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3541 == SIGN_EXTEND)
3542 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3543 SUBREG_REG (*split)));
3544 else
3545 #endif
3546 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3547 SUBREG_REG (*split)));
3549 #endif
3551 /* Attempt to split binary operators using arithmetic identities. */
3552 if (BINARY_P (SET_SRC (newpat))
3553 && split_mode == GET_MODE (SET_SRC (newpat))
3554 && ! side_effects_p (SET_SRC (newpat)))
3556 rtx setsrc = SET_SRC (newpat);
3557 enum machine_mode mode = GET_MODE (setsrc);
3558 enum rtx_code code = GET_CODE (setsrc);
3559 rtx src_op0 = XEXP (setsrc, 0);
3560 rtx src_op1 = XEXP (setsrc, 1);
3562 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3563 if (rtx_equal_p (src_op0, src_op1))
3565 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3566 SUBST (XEXP (setsrc, 0), newdest);
3567 SUBST (XEXP (setsrc, 1), newdest);
3568 subst_done = true;
3570 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3571 else if ((code == PLUS || code == MULT)
3572 && GET_CODE (src_op0) == code
3573 && GET_CODE (XEXP (src_op0, 0)) == code
3574 && (INTEGRAL_MODE_P (mode)
3575 || (FLOAT_MODE_P (mode)
3576 && flag_unsafe_math_optimizations)))
3578 rtx p = XEXP (XEXP (src_op0, 0), 0);
3579 rtx q = XEXP (XEXP (src_op0, 0), 1);
3580 rtx r = XEXP (src_op0, 1);
3581 rtx s = src_op1;
3583 /* Split both "((X op Y) op X) op Y" and
3584 "((X op Y) op Y) op X" as "T op T" where T is
3585 "X op Y". */
3586 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3587 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3589 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3590 XEXP (src_op0, 0));
3591 SUBST (XEXP (setsrc, 0), newdest);
3592 SUBST (XEXP (setsrc, 1), newdest);
3593 subst_done = true;
3595 /* Split "((X op X) op Y) op Y)" as "T op T" where
3596 T is "X op Y". */
3597 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3599 rtx tmp = simplify_gen_binary (code, mode, p, r);
3600 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3601 SUBST (XEXP (setsrc, 0), newdest);
3602 SUBST (XEXP (setsrc, 1), newdest);
3603 subst_done = true;
3608 if (!subst_done)
3610 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3611 SUBST (*split, newdest);
3614 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3616 /* recog_for_combine might have added CLOBBERs to newi2pat.
3617 Make sure NEWPAT does not depend on the clobbered regs. */
3618 if (GET_CODE (newi2pat) == PARALLEL)
3619 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3620 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3622 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3623 if (reg_overlap_mentioned_p (reg, newpat))
3625 undo_all ();
3626 return 0;
3630 /* If the split point was a MULT and we didn't have one before,
3631 don't use one now. */
3632 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3633 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3637 /* Check for a case where we loaded from memory in a narrow mode and
3638 then sign extended it, but we need both registers. In that case,
3639 we have a PARALLEL with both loads from the same memory location.
3640 We can split this into a load from memory followed by a register-register
3641 copy. This saves at least one insn, more if register allocation can
3642 eliminate the copy.
3644 We cannot do this if the destination of the first assignment is a
3645 condition code register or cc0. We eliminate this case by making sure
3646 the SET_DEST and SET_SRC have the same mode.
3648 We cannot do this if the destination of the second assignment is
3649 a register that we have already assumed is zero-extended. Similarly
3650 for a SUBREG of such a register. */
3652 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3653 && GET_CODE (newpat) == PARALLEL
3654 && XVECLEN (newpat, 0) == 2
3655 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3656 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3657 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3658 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3659 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3660 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3661 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3662 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3663 DF_INSN_LUID (i2))
3664 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3665 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3666 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3667 (REG_P (temp)
3668 && VEC_index (reg_stat_type, reg_stat,
3669 REGNO (temp))->nonzero_bits != 0
3670 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3671 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3672 && (VEC_index (reg_stat_type, reg_stat,
3673 REGNO (temp))->nonzero_bits
3674 != GET_MODE_MASK (word_mode))))
3675 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3676 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3677 (REG_P (temp)
3678 && VEC_index (reg_stat_type, reg_stat,
3679 REGNO (temp))->nonzero_bits != 0
3680 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3681 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3682 && (VEC_index (reg_stat_type, reg_stat,
3683 REGNO (temp))->nonzero_bits
3684 != GET_MODE_MASK (word_mode)))))
3685 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3686 SET_SRC (XVECEXP (newpat, 0, 1)))
3687 && ! find_reg_note (i3, REG_UNUSED,
3688 SET_DEST (XVECEXP (newpat, 0, 0))))
3690 rtx ni2dest;
3692 newi2pat = XVECEXP (newpat, 0, 0);
3693 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3694 newpat = XVECEXP (newpat, 0, 1);
3695 SUBST (SET_SRC (newpat),
3696 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3697 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3699 if (i2_code_number >= 0)
3700 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3702 if (insn_code_number >= 0)
3703 swap_i2i3 = 1;
3706 /* Similarly, check for a case where we have a PARALLEL of two independent
3707 SETs but we started with three insns. In this case, we can do the sets
3708 as two separate insns. This case occurs when some SET allows two
3709 other insns to combine, but the destination of that SET is still live. */
3711 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3712 && GET_CODE (newpat) == PARALLEL
3713 && XVECLEN (newpat, 0) == 2
3714 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3715 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3716 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3717 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3718 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3719 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3720 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3721 XVECEXP (newpat, 0, 0))
3722 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3723 XVECEXP (newpat, 0, 1))
3724 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3725 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3727 /* Normally, it doesn't matter which of the two is done first,
3728 but the one that references cc0 can't be the second, and
3729 one which uses any regs/memory set in between i2 and i3 can't
3730 be first. */
3731 if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3732 DF_INSN_LUID (i2))
3733 #ifdef HAVE_cc0
3734 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3735 #endif
3738 newi2pat = XVECEXP (newpat, 0, 1);
3739 newpat = XVECEXP (newpat, 0, 0);
3741 else if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 0)),
3742 DF_INSN_LUID (i2))
3743 #ifdef HAVE_cc0
3744 && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1))
3745 #endif
3748 newi2pat = XVECEXP (newpat, 0, 0);
3749 newpat = XVECEXP (newpat, 0, 1);
3751 else
3753 undo_all ();
3754 return 0;
3757 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3759 if (i2_code_number >= 0)
3761 /* recog_for_combine might have added CLOBBERs to newi2pat.
3762 Make sure NEWPAT does not depend on the clobbered regs. */
3763 if (GET_CODE (newi2pat) == PARALLEL)
3765 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3766 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3768 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3769 if (reg_overlap_mentioned_p (reg, newpat))
3771 undo_all ();
3772 return 0;
3777 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3781 /* If it still isn't recognized, fail and change things back the way they
3782 were. */
3783 if ((insn_code_number < 0
3784 /* Is the result a reasonable ASM_OPERANDS? */
3785 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3787 undo_all ();
3788 return 0;
3791 /* If we had to change another insn, make sure it is valid also. */
3792 if (undobuf.other_insn)
3794 CLEAR_HARD_REG_SET (newpat_used_regs);
3796 other_pat = PATTERN (undobuf.other_insn);
3797 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3798 &new_other_notes);
3800 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3802 undo_all ();
3803 return 0;
3807 #ifdef HAVE_cc0
3808 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3809 they are adjacent to each other or not. */
3811 rtx p = prev_nonnote_insn (i3);
3812 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3813 && sets_cc0_p (newi2pat))
3815 undo_all ();
3816 return 0;
3819 #endif
3821 /* Only allow this combination if insn_rtx_costs reports that the
3822 replacement instructions are cheaper than the originals. */
3823 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3825 undo_all ();
3826 return 0;
3829 if (MAY_HAVE_DEBUG_INSNS)
3831 struct undo *undo;
3833 for (undo = undobuf.undos; undo; undo = undo->next)
3834 if (undo->kind == UNDO_MODE)
3836 rtx reg = *undo->where.r;
3837 enum machine_mode new_mode = GET_MODE (reg);
3838 enum machine_mode old_mode = undo->old_contents.m;
3840 /* Temporarily revert mode back. */
3841 adjust_reg_mode (reg, old_mode);
3843 if (reg == i2dest && i2scratch)
3845 /* If we used i2dest as a scratch register with a
3846 different mode, substitute it for the original
3847 i2src while its original mode is temporarily
3848 restored, and then clear i2scratch so that we don't
3849 do it again later. */
3850 propagate_for_debug (i2, i3, reg, i2src);
3851 i2scratch = false;
3852 /* Put back the new mode. */
3853 adjust_reg_mode (reg, new_mode);
3855 else
3857 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3858 rtx first, last;
3860 if (reg == i2dest)
3862 first = i2;
3863 last = i3;
3865 else
3867 first = i3;
3868 last = undobuf.other_insn;
3869 gcc_assert (last);
3872 /* We're dealing with a reg that changed mode but not
3873 meaning, so we want to turn it into a subreg for
3874 the new mode. However, because of REG sharing and
3875 because its mode had already changed, we have to do
3876 it in two steps. First, replace any debug uses of
3877 reg, with its original mode temporarily restored,
3878 with this copy we have created; then, replace the
3879 copy with the SUBREG of the original shared reg,
3880 once again changed to the new mode. */
3881 propagate_for_debug (first, last, reg, tempreg);
3882 adjust_reg_mode (reg, new_mode);
3883 propagate_for_debug (first, last, tempreg,
3884 lowpart_subreg (old_mode, reg, new_mode));
3889 /* If we will be able to accept this, we have made a
3890 change to the destination of I3. This requires us to
3891 do a few adjustments. */
3893 if (changed_i3_dest)
3895 PATTERN (i3) = newpat;
3896 adjust_for_new_dest (i3);
3899 /* We now know that we can do this combination. Merge the insns and
3900 update the status of registers and LOG_LINKS. */
3902 if (undobuf.other_insn)
3904 rtx note, next;
3906 PATTERN (undobuf.other_insn) = other_pat;
3908 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3909 are still valid. Then add any non-duplicate notes added by
3910 recog_for_combine. */
3911 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3913 next = XEXP (note, 1);
3915 if (REG_NOTE_KIND (note) == REG_UNUSED
3916 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3917 remove_note (undobuf.other_insn, note);
3920 distribute_notes (new_other_notes, undobuf.other_insn,
3921 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
3922 NULL_RTX);
3925 if (swap_i2i3)
3927 rtx insn;
3928 rtx link;
3929 rtx ni2dest;
3931 /* I3 now uses what used to be its destination and which is now
3932 I2's destination. This requires us to do a few adjustments. */
3933 PATTERN (i3) = newpat;
3934 adjust_for_new_dest (i3);
3936 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3937 so we still will.
3939 However, some later insn might be using I2's dest and have
3940 a LOG_LINK pointing at I3. We must remove this link.
3941 The simplest way to remove the link is to point it at I1,
3942 which we know will be a NOTE. */
3944 /* newi2pat is usually a SET here; however, recog_for_combine might
3945 have added some clobbers. */
3946 if (GET_CODE (newi2pat) == PARALLEL)
3947 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3948 else
3949 ni2dest = SET_DEST (newi2pat);
3951 for (insn = NEXT_INSN (i3);
3952 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3953 || insn != BB_HEAD (this_basic_block->next_bb));
3954 insn = NEXT_INSN (insn))
3956 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3958 for (link = LOG_LINKS (insn); link;
3959 link = XEXP (link, 1))
3960 if (XEXP (link, 0) == i3)
3961 XEXP (link, 0) = i1;
3963 break;
3969 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3970 rtx i3links, i2links, i1links = 0, i0links = 0;
3971 rtx midnotes = 0;
3972 int from_luid;
3973 unsigned int regno;
3974 /* Compute which registers we expect to eliminate. newi2pat may be setting
3975 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3976 same as i3dest, in which case newi2pat may be setting i1dest. */
3977 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3978 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
3979 || !i2dest_killed
3980 ? 0 : i2dest);
3981 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
3982 || (newi2pat && reg_set_p (i1dest, newi2pat))
3983 || !i1dest_killed
3984 ? 0 : i1dest);
3985 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
3986 || (newi2pat && reg_set_p (i0dest, newi2pat))
3987 || !i0dest_killed
3988 ? 0 : i0dest);
3990 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3991 clear them. */
3992 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3993 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3994 if (i1)
3995 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3996 if (i0)
3997 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
3999 /* Ensure that we do not have something that should not be shared but
4000 occurs multiple times in the new insns. Check this by first
4001 resetting all the `used' flags and then copying anything is shared. */
4003 reset_used_flags (i3notes);
4004 reset_used_flags (i2notes);
4005 reset_used_flags (i1notes);
4006 reset_used_flags (i0notes);
4007 reset_used_flags (newpat);
4008 reset_used_flags (newi2pat);
4009 if (undobuf.other_insn)
4010 reset_used_flags (PATTERN (undobuf.other_insn));
4012 i3notes = copy_rtx_if_shared (i3notes);
4013 i2notes = copy_rtx_if_shared (i2notes);
4014 i1notes = copy_rtx_if_shared (i1notes);
4015 i0notes = copy_rtx_if_shared (i0notes);
4016 newpat = copy_rtx_if_shared (newpat);
4017 newi2pat = copy_rtx_if_shared (newi2pat);
4018 if (undobuf.other_insn)
4019 reset_used_flags (PATTERN (undobuf.other_insn));
4021 INSN_CODE (i3) = insn_code_number;
4022 PATTERN (i3) = newpat;
4024 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4026 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4028 reset_used_flags (call_usage);
4029 call_usage = copy_rtx (call_usage);
4031 if (substed_i2)
4033 /* I2SRC must still be meaningful at this point. Some splitting
4034 operations can invalidate I2SRC, but those operations do not
4035 apply to calls. */
4036 gcc_assert (i2src);
4037 replace_rtx (call_usage, i2dest, i2src);
4040 if (substed_i1)
4041 replace_rtx (call_usage, i1dest, i1src);
4042 if (substed_i0)
4043 replace_rtx (call_usage, i0dest, i0src);
4045 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4048 if (undobuf.other_insn)
4049 INSN_CODE (undobuf.other_insn) = other_code_number;
4051 /* We had one special case above where I2 had more than one set and
4052 we replaced a destination of one of those sets with the destination
4053 of I3. In that case, we have to update LOG_LINKS of insns later
4054 in this basic block. Note that this (expensive) case is rare.
4056 Also, in this case, we must pretend that all REG_NOTEs for I2
4057 actually came from I3, so that REG_UNUSED notes from I2 will be
4058 properly handled. */
4060 if (i3_subst_into_i2)
4062 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4063 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4064 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4065 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4066 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4067 && ! find_reg_note (i2, REG_UNUSED,
4068 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4069 for (temp = NEXT_INSN (i2);
4070 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4071 || BB_HEAD (this_basic_block) != temp);
4072 temp = NEXT_INSN (temp))
4073 if (temp != i3 && INSN_P (temp))
4074 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
4075 if (XEXP (link, 0) == i2)
4076 XEXP (link, 0) = i3;
4078 if (i3notes)
4080 rtx link = i3notes;
4081 while (XEXP (link, 1))
4082 link = XEXP (link, 1);
4083 XEXP (link, 1) = i2notes;
4085 else
4086 i3notes = i2notes;
4087 i2notes = 0;
4090 LOG_LINKS (i3) = 0;
4091 REG_NOTES (i3) = 0;
4092 LOG_LINKS (i2) = 0;
4093 REG_NOTES (i2) = 0;
4095 if (newi2pat)
4097 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4098 propagate_for_debug (i2, i3, i2dest, i2src);
4099 INSN_CODE (i2) = i2_code_number;
4100 PATTERN (i2) = newi2pat;
4102 else
4104 if (MAY_HAVE_DEBUG_INSNS && i2src)
4105 propagate_for_debug (i2, i3, i2dest, i2src);
4106 SET_INSN_DELETED (i2);
4109 if (i1)
4111 LOG_LINKS (i1) = 0;
4112 REG_NOTES (i1) = 0;
4113 if (MAY_HAVE_DEBUG_INSNS)
4114 propagate_for_debug (i1, i3, i1dest, i1src);
4115 SET_INSN_DELETED (i1);
4118 if (i0)
4120 LOG_LINKS (i0) = 0;
4121 REG_NOTES (i0) = 0;
4122 if (MAY_HAVE_DEBUG_INSNS)
4123 propagate_for_debug (i0, i3, i0dest, i0src);
4124 SET_INSN_DELETED (i0);
4127 /* Get death notes for everything that is now used in either I3 or
4128 I2 and used to die in a previous insn. If we built two new
4129 patterns, move from I1 to I2 then I2 to I3 so that we get the
4130 proper movement on registers that I2 modifies. */
4132 if (i0)
4133 from_luid = DF_INSN_LUID (i0);
4134 else if (i1)
4135 from_luid = DF_INSN_LUID (i1);
4136 else
4137 from_luid = DF_INSN_LUID (i2);
4138 if (newi2pat)
4139 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4140 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4142 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4143 if (i3notes)
4144 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4145 elim_i2, elim_i1, elim_i0);
4146 if (i2notes)
4147 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4148 elim_i2, elim_i1, elim_i0);
4149 if (i1notes)
4150 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4151 elim_i2, elim_i1, elim_i0);
4152 if (i0notes)
4153 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4154 elim_i2, elim_i1, elim_i0);
4155 if (midnotes)
4156 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4157 elim_i2, elim_i1, elim_i0);
4159 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4160 know these are REG_UNUSED and want them to go to the desired insn,
4161 so we always pass it as i3. */
4163 if (newi2pat && new_i2_notes)
4164 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4165 NULL_RTX);
4167 if (new_i3_notes)
4168 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4169 NULL_RTX);
4171 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4172 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4173 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4174 in that case, it might delete I2. Similarly for I2 and I1.
4175 Show an additional death due to the REG_DEAD note we make here. If
4176 we discard it in distribute_notes, we will decrement it again. */
4178 if (i3dest_killed)
4180 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4181 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4182 NULL_RTX),
4183 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
4184 else
4185 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4186 NULL_RTX),
4187 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4188 elim_i2, elim_i1, elim_i0);
4191 if (i2dest_in_i2src)
4193 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4194 if (newi2pat && reg_set_p (i2dest, newi2pat))
4195 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4196 NULL_RTX, NULL_RTX);
4197 else
4198 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4199 NULL_RTX, NULL_RTX, NULL_RTX);
4202 if (i1dest_in_i1src)
4204 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4205 if (newi2pat && reg_set_p (i1dest, newi2pat))
4206 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4207 NULL_RTX, NULL_RTX);
4208 else
4209 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4210 NULL_RTX, NULL_RTX, NULL_RTX);
4213 if (i0dest_in_i0src)
4215 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4216 if (newi2pat && reg_set_p (i0dest, newi2pat))
4217 distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4218 NULL_RTX, NULL_RTX);
4219 else
4220 distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4221 NULL_RTX, NULL_RTX, NULL_RTX);
4224 distribute_links (i3links);
4225 distribute_links (i2links);
4226 distribute_links (i1links);
4227 distribute_links (i0links);
4229 if (REG_P (i2dest))
4231 rtx link;
4232 rtx i2_insn = 0, i2_val = 0, set;
4234 /* The insn that used to set this register doesn't exist, and
4235 this life of the register may not exist either. See if one of
4236 I3's links points to an insn that sets I2DEST. If it does,
4237 that is now the last known value for I2DEST. If we don't update
4238 this and I2 set the register to a value that depended on its old
4239 contents, we will get confused. If this insn is used, thing
4240 will be set correctly in combine_instructions. */
4242 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
4243 if ((set = single_set (XEXP (link, 0))) != 0
4244 && rtx_equal_p (i2dest, SET_DEST (set)))
4245 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
4247 record_value_for_reg (i2dest, i2_insn, i2_val);
4249 /* If the reg formerly set in I2 died only once and that was in I3,
4250 zero its use count so it won't make `reload' do any work. */
4251 if (! added_sets_2
4252 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4253 && ! i2dest_in_i2src)
4255 regno = REGNO (i2dest);
4256 INC_REG_N_SETS (regno, -1);
4260 if (i1 && REG_P (i1dest))
4262 rtx link;
4263 rtx i1_insn = 0, i1_val = 0, set;
4265 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
4266 if ((set = single_set (XEXP (link, 0))) != 0
4267 && rtx_equal_p (i1dest, SET_DEST (set)))
4268 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
4270 record_value_for_reg (i1dest, i1_insn, i1_val);
4272 regno = REGNO (i1dest);
4273 if (! added_sets_1 && ! i1dest_in_i1src)
4274 INC_REG_N_SETS (regno, -1);
4277 if (i0 && REG_P (i0dest))
4279 rtx link;
4280 rtx i0_insn = 0, i0_val = 0, set;
4282 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
4283 if ((set = single_set (XEXP (link, 0))) != 0
4284 && rtx_equal_p (i0dest, SET_DEST (set)))
4285 i0_insn = XEXP (link, 0), i0_val = SET_SRC (set);
4287 record_value_for_reg (i0dest, i0_insn, i0_val);
4289 regno = REGNO (i0dest);
4290 if (! added_sets_0 && ! i0dest_in_i0src)
4291 INC_REG_N_SETS (regno, -1);
4294 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4295 been made to this insn. The order of
4296 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
4297 can affect nonzero_bits of newpat */
4298 if (newi2pat)
4299 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4300 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4303 if (undobuf.other_insn != NULL_RTX)
4305 if (dump_file)
4307 fprintf (dump_file, "modifying other_insn ");
4308 dump_insn_slim (dump_file, undobuf.other_insn);
4310 df_insn_rescan (undobuf.other_insn);
4313 if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4315 if (dump_file)
4317 fprintf (dump_file, "modifying insn i1 ");
4318 dump_insn_slim (dump_file, i0);
4320 df_insn_rescan (i0);
4323 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4325 if (dump_file)
4327 fprintf (dump_file, "modifying insn i1 ");
4328 dump_insn_slim (dump_file, i1);
4330 df_insn_rescan (i1);
4333 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4335 if (dump_file)
4337 fprintf (dump_file, "modifying insn i2 ");
4338 dump_insn_slim (dump_file, i2);
4340 df_insn_rescan (i2);
4343 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4345 if (dump_file)
4347 fprintf (dump_file, "modifying insn i3 ");
4348 dump_insn_slim (dump_file, i3);
4350 df_insn_rescan (i3);
4353 /* Set new_direct_jump_p if a new return or simple jump instruction
4354 has been created. Adjust the CFG accordingly. */
4356 if (returnjump_p (i3) || any_uncondjump_p (i3))
4358 *new_direct_jump_p = 1;
4359 mark_jump_label (PATTERN (i3), i3, 0);
4360 update_cfg_for_uncondjump (i3);
4363 if (undobuf.other_insn != NULL_RTX
4364 && (returnjump_p (undobuf.other_insn)
4365 || any_uncondjump_p (undobuf.other_insn)))
4367 *new_direct_jump_p = 1;
4368 update_cfg_for_uncondjump (undobuf.other_insn);
4371 /* A noop might also need cleaning up of CFG, if it comes from the
4372 simplification of a jump. */
4373 if (GET_CODE (newpat) == SET
4374 && SET_SRC (newpat) == pc_rtx
4375 && SET_DEST (newpat) == pc_rtx)
4377 *new_direct_jump_p = 1;
4378 update_cfg_for_uncondjump (i3);
4381 combine_successes++;
4382 undo_commit ();
4384 if (added_links_insn
4385 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4386 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4387 return added_links_insn;
4388 else
4389 return newi2pat ? i2 : i3;
4392 /* Undo all the modifications recorded in undobuf. */
4394 static void
4395 undo_all (void)
4397 struct undo *undo, *next;
4399 for (undo = undobuf.undos; undo; undo = next)
4401 next = undo->next;
4402 switch (undo->kind)
4404 case UNDO_RTX:
4405 *undo->where.r = undo->old_contents.r;
4406 break;
4407 case UNDO_INT:
4408 *undo->where.i = undo->old_contents.i;
4409 break;
4410 case UNDO_MODE:
4411 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4412 break;
4413 default:
4414 gcc_unreachable ();
4417 undo->next = undobuf.frees;
4418 undobuf.frees = undo;
4421 undobuf.undos = 0;
4424 /* We've committed to accepting the changes we made. Move all
4425 of the undos to the free list. */
4427 static void
4428 undo_commit (void)
4430 struct undo *undo, *next;
4432 for (undo = undobuf.undos; undo; undo = next)
4434 next = undo->next;
4435 undo->next = undobuf.frees;
4436 undobuf.frees = undo;
4438 undobuf.undos = 0;
4441 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4442 where we have an arithmetic expression and return that point. LOC will
4443 be inside INSN.
4445 try_combine will call this function to see if an insn can be split into
4446 two insns. */
4448 static rtx *
4449 find_split_point (rtx *loc, rtx insn, bool set_src)
4451 rtx x = *loc;
4452 enum rtx_code code = GET_CODE (x);
4453 rtx *split;
4454 unsigned HOST_WIDE_INT len = 0;
4455 HOST_WIDE_INT pos = 0;
4456 int unsignedp = 0;
4457 rtx inner = NULL_RTX;
4459 /* First special-case some codes. */
4460 switch (code)
4462 case SUBREG:
4463 #ifdef INSN_SCHEDULING
4464 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4465 point. */
4466 if (MEM_P (SUBREG_REG (x)))
4467 return loc;
4468 #endif
4469 return find_split_point (&SUBREG_REG (x), insn, false);
4471 case MEM:
4472 #ifdef HAVE_lo_sum
4473 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4474 using LO_SUM and HIGH. */
4475 if (GET_CODE (XEXP (x, 0)) == CONST
4476 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4478 enum machine_mode address_mode
4479 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4481 SUBST (XEXP (x, 0),
4482 gen_rtx_LO_SUM (address_mode,
4483 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4484 XEXP (x, 0)));
4485 return &XEXP (XEXP (x, 0), 0);
4487 #endif
4489 /* If we have a PLUS whose second operand is a constant and the
4490 address is not valid, perhaps will can split it up using
4491 the machine-specific way to split large constants. We use
4492 the first pseudo-reg (one of the virtual regs) as a placeholder;
4493 it will not remain in the result. */
4494 if (GET_CODE (XEXP (x, 0)) == PLUS
4495 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4496 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4497 MEM_ADDR_SPACE (x)))
4499 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4500 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4501 XEXP (x, 0)),
4502 subst_insn);
4504 /* This should have produced two insns, each of which sets our
4505 placeholder. If the source of the second is a valid address,
4506 we can make put both sources together and make a split point
4507 in the middle. */
4509 if (seq
4510 && NEXT_INSN (seq) != NULL_RTX
4511 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4512 && NONJUMP_INSN_P (seq)
4513 && GET_CODE (PATTERN (seq)) == SET
4514 && SET_DEST (PATTERN (seq)) == reg
4515 && ! reg_mentioned_p (reg,
4516 SET_SRC (PATTERN (seq)))
4517 && NONJUMP_INSN_P (NEXT_INSN (seq))
4518 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4519 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4520 && memory_address_addr_space_p
4521 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4522 MEM_ADDR_SPACE (x)))
4524 rtx src1 = SET_SRC (PATTERN (seq));
4525 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4527 /* Replace the placeholder in SRC2 with SRC1. If we can
4528 find where in SRC2 it was placed, that can become our
4529 split point and we can replace this address with SRC2.
4530 Just try two obvious places. */
4532 src2 = replace_rtx (src2, reg, src1);
4533 split = 0;
4534 if (XEXP (src2, 0) == src1)
4535 split = &XEXP (src2, 0);
4536 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4537 && XEXP (XEXP (src2, 0), 0) == src1)
4538 split = &XEXP (XEXP (src2, 0), 0);
4540 if (split)
4542 SUBST (XEXP (x, 0), src2);
4543 return split;
4547 /* If that didn't work, perhaps the first operand is complex and
4548 needs to be computed separately, so make a split point there.
4549 This will occur on machines that just support REG + CONST
4550 and have a constant moved through some previous computation. */
4552 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4553 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4554 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4555 return &XEXP (XEXP (x, 0), 0);
4558 /* If we have a PLUS whose first operand is complex, try computing it
4559 separately by making a split there. */
4560 if (GET_CODE (XEXP (x, 0)) == PLUS
4561 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4562 MEM_ADDR_SPACE (x))
4563 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4564 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4565 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4566 return &XEXP (XEXP (x, 0), 0);
4567 break;
4569 case SET:
4570 #ifdef HAVE_cc0
4571 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4572 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4573 we need to put the operand into a register. So split at that
4574 point. */
4576 if (SET_DEST (x) == cc0_rtx
4577 && GET_CODE (SET_SRC (x)) != COMPARE
4578 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4579 && !OBJECT_P (SET_SRC (x))
4580 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4581 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4582 return &SET_SRC (x);
4583 #endif
4585 /* See if we can split SET_SRC as it stands. */
4586 split = find_split_point (&SET_SRC (x), insn, true);
4587 if (split && split != &SET_SRC (x))
4588 return split;
4590 /* See if we can split SET_DEST as it stands. */
4591 split = find_split_point (&SET_DEST (x), insn, false);
4592 if (split && split != &SET_DEST (x))
4593 return split;
4595 /* See if this is a bitfield assignment with everything constant. If
4596 so, this is an IOR of an AND, so split it into that. */
4597 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4598 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4599 <= HOST_BITS_PER_WIDE_INT)
4600 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4601 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4602 && CONST_INT_P (SET_SRC (x))
4603 && ((INTVAL (XEXP (SET_DEST (x), 1))
4604 + INTVAL (XEXP (SET_DEST (x), 2)))
4605 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4606 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4608 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4609 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4610 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4611 rtx dest = XEXP (SET_DEST (x), 0);
4612 enum machine_mode mode = GET_MODE (dest);
4613 unsigned HOST_WIDE_INT mask
4614 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4615 rtx or_mask;
4617 if (BITS_BIG_ENDIAN)
4618 pos = GET_MODE_BITSIZE (mode) - len - pos;
4620 or_mask = gen_int_mode (src << pos, mode);
4621 if (src == mask)
4622 SUBST (SET_SRC (x),
4623 simplify_gen_binary (IOR, mode, dest, or_mask));
4624 else
4626 rtx negmask = gen_int_mode (~(mask << pos), mode);
4627 SUBST (SET_SRC (x),
4628 simplify_gen_binary (IOR, mode,
4629 simplify_gen_binary (AND, mode,
4630 dest, negmask),
4631 or_mask));
4634 SUBST (SET_DEST (x), dest);
4636 split = find_split_point (&SET_SRC (x), insn, true);
4637 if (split && split != &SET_SRC (x))
4638 return split;
4641 /* Otherwise, see if this is an operation that we can split into two.
4642 If so, try to split that. */
4643 code = GET_CODE (SET_SRC (x));
4645 switch (code)
4647 case AND:
4648 /* If we are AND'ing with a large constant that is only a single
4649 bit and the result is only being used in a context where we
4650 need to know if it is zero or nonzero, replace it with a bit
4651 extraction. This will avoid the large constant, which might
4652 have taken more than one insn to make. If the constant were
4653 not a valid argument to the AND but took only one insn to make,
4654 this is no worse, but if it took more than one insn, it will
4655 be better. */
4657 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4658 && REG_P (XEXP (SET_SRC (x), 0))
4659 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4660 && REG_P (SET_DEST (x))
4661 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4662 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4663 && XEXP (*split, 0) == SET_DEST (x)
4664 && XEXP (*split, 1) == const0_rtx)
4666 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4667 XEXP (SET_SRC (x), 0),
4668 pos, NULL_RTX, 1, 1, 0, 0);
4669 if (extraction != 0)
4671 SUBST (SET_SRC (x), extraction);
4672 return find_split_point (loc, insn, false);
4675 break;
4677 case NE:
4678 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4679 is known to be on, this can be converted into a NEG of a shift. */
4680 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4681 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4682 && 1 <= (pos = exact_log2
4683 (nonzero_bits (XEXP (SET_SRC (x), 0),
4684 GET_MODE (XEXP (SET_SRC (x), 0))))))
4686 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4688 SUBST (SET_SRC (x),
4689 gen_rtx_NEG (mode,
4690 gen_rtx_LSHIFTRT (mode,
4691 XEXP (SET_SRC (x), 0),
4692 GEN_INT (pos))));
4694 split = find_split_point (&SET_SRC (x), insn, true);
4695 if (split && split != &SET_SRC (x))
4696 return split;
4698 break;
4700 case SIGN_EXTEND:
4701 inner = XEXP (SET_SRC (x), 0);
4703 /* We can't optimize if either mode is a partial integer
4704 mode as we don't know how many bits are significant
4705 in those modes. */
4706 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4707 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4708 break;
4710 pos = 0;
4711 len = GET_MODE_BITSIZE (GET_MODE (inner));
4712 unsignedp = 0;
4713 break;
4715 case SIGN_EXTRACT:
4716 case ZERO_EXTRACT:
4717 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4718 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4720 inner = XEXP (SET_SRC (x), 0);
4721 len = INTVAL (XEXP (SET_SRC (x), 1));
4722 pos = INTVAL (XEXP (SET_SRC (x), 2));
4724 if (BITS_BIG_ENDIAN)
4725 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4726 unsignedp = (code == ZERO_EXTRACT);
4728 break;
4730 default:
4731 break;
4734 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4736 enum machine_mode mode = GET_MODE (SET_SRC (x));
4738 /* For unsigned, we have a choice of a shift followed by an
4739 AND or two shifts. Use two shifts for field sizes where the
4740 constant might be too large. We assume here that we can
4741 always at least get 8-bit constants in an AND insn, which is
4742 true for every current RISC. */
4744 if (unsignedp && len <= 8)
4746 SUBST (SET_SRC (x),
4747 gen_rtx_AND (mode,
4748 gen_rtx_LSHIFTRT
4749 (mode, gen_lowpart (mode, inner),
4750 GEN_INT (pos)),
4751 GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4752 - 1)));
4754 split = find_split_point (&SET_SRC (x), insn, true);
4755 if (split && split != &SET_SRC (x))
4756 return split;
4758 else
4760 SUBST (SET_SRC (x),
4761 gen_rtx_fmt_ee
4762 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4763 gen_rtx_ASHIFT (mode,
4764 gen_lowpart (mode, inner),
4765 GEN_INT (GET_MODE_BITSIZE (mode)
4766 - len - pos)),
4767 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4769 split = find_split_point (&SET_SRC (x), insn, true);
4770 if (split && split != &SET_SRC (x))
4771 return split;
4775 /* See if this is a simple operation with a constant as the second
4776 operand. It might be that this constant is out of range and hence
4777 could be used as a split point. */
4778 if (BINARY_P (SET_SRC (x))
4779 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4780 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4781 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4782 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4783 return &XEXP (SET_SRC (x), 1);
4785 /* Finally, see if this is a simple operation with its first operand
4786 not in a register. The operation might require this operand in a
4787 register, so return it as a split point. We can always do this
4788 because if the first operand were another operation, we would have
4789 already found it as a split point. */
4790 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4791 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4792 return &XEXP (SET_SRC (x), 0);
4794 return 0;
4796 case AND:
4797 case IOR:
4798 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4799 it is better to write this as (not (ior A B)) so we can split it.
4800 Similarly for IOR. */
4801 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4803 SUBST (*loc,
4804 gen_rtx_NOT (GET_MODE (x),
4805 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4806 GET_MODE (x),
4807 XEXP (XEXP (x, 0), 0),
4808 XEXP (XEXP (x, 1), 0))));
4809 return find_split_point (loc, insn, set_src);
4812 /* Many RISC machines have a large set of logical insns. If the
4813 second operand is a NOT, put it first so we will try to split the
4814 other operand first. */
4815 if (GET_CODE (XEXP (x, 1)) == NOT)
4817 rtx tem = XEXP (x, 0);
4818 SUBST (XEXP (x, 0), XEXP (x, 1));
4819 SUBST (XEXP (x, 1), tem);
4821 break;
4823 case PLUS:
4824 case MINUS:
4825 /* Canonicalization can produce (minus A (mult B C)), where C is a
4826 constant. It may be better to try splitting (plus (mult B -C) A)
4827 instead if this isn't a multiply by a power of two. */
4828 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4829 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4830 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4832 enum machine_mode mode = GET_MODE (x);
4833 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4834 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4835 SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4836 XEXP (XEXP (x, 1), 0),
4837 GEN_INT (other_int)),
4838 XEXP (x, 0)));
4839 return find_split_point (loc, insn, set_src);
4842 /* Split at a multiply-accumulate instruction. However if this is
4843 the SET_SRC, we likely do not have such an instruction and it's
4844 worthless to try this split. */
4845 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4846 return loc;
4848 default:
4849 break;
4852 /* Otherwise, select our actions depending on our rtx class. */
4853 switch (GET_RTX_CLASS (code))
4855 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4856 case RTX_TERNARY:
4857 split = find_split_point (&XEXP (x, 2), insn, false);
4858 if (split)
4859 return split;
4860 /* ... fall through ... */
4861 case RTX_BIN_ARITH:
4862 case RTX_COMM_ARITH:
4863 case RTX_COMPARE:
4864 case RTX_COMM_COMPARE:
4865 split = find_split_point (&XEXP (x, 1), insn, false);
4866 if (split)
4867 return split;
4868 /* ... fall through ... */
4869 case RTX_UNARY:
4870 /* Some machines have (and (shift ...) ...) insns. If X is not
4871 an AND, but XEXP (X, 0) is, use it as our split point. */
4872 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4873 return &XEXP (x, 0);
4875 split = find_split_point (&XEXP (x, 0), insn, false);
4876 if (split)
4877 return split;
4878 return loc;
4880 default:
4881 /* Otherwise, we don't have a split point. */
4882 return 0;
4886 /* Throughout X, replace FROM with TO, and return the result.
4887 The result is TO if X is FROM;
4888 otherwise the result is X, but its contents may have been modified.
4889 If they were modified, a record was made in undobuf so that
4890 undo_all will (among other things) return X to its original state.
4892 If the number of changes necessary is too much to record to undo,
4893 the excess changes are not made, so the result is invalid.
4894 The changes already made can still be undone.
4895 undobuf.num_undo is incremented for such changes, so by testing that
4896 the caller can tell whether the result is valid.
4898 `n_occurrences' is incremented each time FROM is replaced.
4900 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4902 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4903 by copying if `n_occurrences' is nonzero. */
4905 static rtx
4906 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4908 enum rtx_code code = GET_CODE (x);
4909 enum machine_mode op0_mode = VOIDmode;
4910 const char *fmt;
4911 int len, i;
4912 rtx new_rtx;
4914 /* Two expressions are equal if they are identical copies of a shared
4915 RTX or if they are both registers with the same register number
4916 and mode. */
4918 #define COMBINE_RTX_EQUAL_P(X,Y) \
4919 ((X) == (Y) \
4920 || (REG_P (X) && REG_P (Y) \
4921 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4923 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4925 n_occurrences++;
4926 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4929 /* If X and FROM are the same register but different modes, they
4930 will not have been seen as equal above. However, the log links code
4931 will make a LOG_LINKS entry for that case. If we do nothing, we
4932 will try to rerecognize our original insn and, when it succeeds,
4933 we will delete the feeding insn, which is incorrect.
4935 So force this insn not to match in this (rare) case. */
4936 if (! in_dest && code == REG && REG_P (from)
4937 && reg_overlap_mentioned_p (x, from))
4938 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4940 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4941 of which may contain things that can be combined. */
4942 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4943 return x;
4945 /* It is possible to have a subexpression appear twice in the insn.
4946 Suppose that FROM is a register that appears within TO.
4947 Then, after that subexpression has been scanned once by `subst',
4948 the second time it is scanned, TO may be found. If we were
4949 to scan TO here, we would find FROM within it and create a
4950 self-referent rtl structure which is completely wrong. */
4951 if (COMBINE_RTX_EQUAL_P (x, to))
4952 return to;
4954 /* Parallel asm_operands need special attention because all of the
4955 inputs are shared across the arms. Furthermore, unsharing the
4956 rtl results in recognition failures. Failure to handle this case
4957 specially can result in circular rtl.
4959 Solve this by doing a normal pass across the first entry of the
4960 parallel, and only processing the SET_DESTs of the subsequent
4961 entries. Ug. */
4963 if (code == PARALLEL
4964 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4965 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4967 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4969 /* If this substitution failed, this whole thing fails. */
4970 if (GET_CODE (new_rtx) == CLOBBER
4971 && XEXP (new_rtx, 0) == const0_rtx)
4972 return new_rtx;
4974 SUBST (XVECEXP (x, 0, 0), new_rtx);
4976 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4978 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4980 if (!REG_P (dest)
4981 && GET_CODE (dest) != CC0
4982 && GET_CODE (dest) != PC)
4984 new_rtx = subst (dest, from, to, 0, unique_copy);
4986 /* If this substitution failed, this whole thing fails. */
4987 if (GET_CODE (new_rtx) == CLOBBER
4988 && XEXP (new_rtx, 0) == const0_rtx)
4989 return new_rtx;
4991 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4995 else
4997 len = GET_RTX_LENGTH (code);
4998 fmt = GET_RTX_FORMAT (code);
5000 /* We don't need to process a SET_DEST that is a register, CC0,
5001 or PC, so set up to skip this common case. All other cases
5002 where we want to suppress replacing something inside a
5003 SET_SRC are handled via the IN_DEST operand. */
5004 if (code == SET
5005 && (REG_P (SET_DEST (x))
5006 || GET_CODE (SET_DEST (x)) == CC0
5007 || GET_CODE (SET_DEST (x)) == PC))
5008 fmt = "ie";
5010 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5011 constant. */
5012 if (fmt[0] == 'e')
5013 op0_mode = GET_MODE (XEXP (x, 0));
5015 for (i = 0; i < len; i++)
5017 if (fmt[i] == 'E')
5019 int j;
5020 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5022 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5024 new_rtx = (unique_copy && n_occurrences
5025 ? copy_rtx (to) : to);
5026 n_occurrences++;
5028 else
5030 new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
5031 unique_copy);
5033 /* If this substitution failed, this whole thing
5034 fails. */
5035 if (GET_CODE (new_rtx) == CLOBBER
5036 && XEXP (new_rtx, 0) == const0_rtx)
5037 return new_rtx;
5040 SUBST (XVECEXP (x, i, j), new_rtx);
5043 else if (fmt[i] == 'e')
5045 /* If this is a register being set, ignore it. */
5046 new_rtx = XEXP (x, i);
5047 if (in_dest
5048 && i == 0
5049 && (((code == SUBREG || code == ZERO_EXTRACT)
5050 && REG_P (new_rtx))
5051 || code == STRICT_LOW_PART))
5054 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5056 /* In general, don't install a subreg involving two
5057 modes not tieable. It can worsen register
5058 allocation, and can even make invalid reload
5059 insns, since the reg inside may need to be copied
5060 from in the outside mode, and that may be invalid
5061 if it is an fp reg copied in integer mode.
5063 We allow two exceptions to this: It is valid if
5064 it is inside another SUBREG and the mode of that
5065 SUBREG and the mode of the inside of TO is
5066 tieable and it is valid if X is a SET that copies
5067 FROM to CC0. */
5069 if (GET_CODE (to) == SUBREG
5070 && ! MODES_TIEABLE_P (GET_MODE (to),
5071 GET_MODE (SUBREG_REG (to)))
5072 && ! (code == SUBREG
5073 && MODES_TIEABLE_P (GET_MODE (x),
5074 GET_MODE (SUBREG_REG (to))))
5075 #ifdef HAVE_cc0
5076 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5077 #endif
5079 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5081 #ifdef CANNOT_CHANGE_MODE_CLASS
5082 if (code == SUBREG
5083 && REG_P (to)
5084 && REGNO (to) < FIRST_PSEUDO_REGISTER
5085 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5086 GET_MODE (to),
5087 GET_MODE (x)))
5088 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5089 #endif
5091 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5092 n_occurrences++;
5094 else
5095 /* If we are in a SET_DEST, suppress most cases unless we
5096 have gone inside a MEM, in which case we want to
5097 simplify the address. We assume here that things that
5098 are actually part of the destination have their inner
5099 parts in the first expression. This is true for SUBREG,
5100 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5101 things aside from REG and MEM that should appear in a
5102 SET_DEST. */
5103 new_rtx = subst (XEXP (x, i), from, to,
5104 (((in_dest
5105 && (code == SUBREG || code == STRICT_LOW_PART
5106 || code == ZERO_EXTRACT))
5107 || code == SET)
5108 && i == 0), unique_copy);
5110 /* If we found that we will have to reject this combination,
5111 indicate that by returning the CLOBBER ourselves, rather than
5112 an expression containing it. This will speed things up as
5113 well as prevent accidents where two CLOBBERs are considered
5114 to be equal, thus producing an incorrect simplification. */
5116 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5117 return new_rtx;
5119 if (GET_CODE (x) == SUBREG
5120 && (CONST_INT_P (new_rtx)
5121 || GET_CODE (new_rtx) == CONST_DOUBLE))
5123 enum machine_mode mode = GET_MODE (x);
5125 x = simplify_subreg (GET_MODE (x), new_rtx,
5126 GET_MODE (SUBREG_REG (x)),
5127 SUBREG_BYTE (x));
5128 if (! x)
5129 x = gen_rtx_CLOBBER (mode, const0_rtx);
5131 else if (CONST_INT_P (new_rtx)
5132 && GET_CODE (x) == ZERO_EXTEND)
5134 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5135 new_rtx, GET_MODE (XEXP (x, 0)));
5136 gcc_assert (x);
5138 else
5139 SUBST (XEXP (x, i), new_rtx);
5144 /* Check if we are loading something from the constant pool via float
5145 extension; in this case we would undo compress_float_constant
5146 optimization and degenerate constant load to an immediate value. */
5147 if (GET_CODE (x) == FLOAT_EXTEND
5148 && MEM_P (XEXP (x, 0))
5149 && MEM_READONLY_P (XEXP (x, 0)))
5151 rtx tmp = avoid_constant_pool_reference (x);
5152 if (x != tmp)
5153 return x;
5156 /* Try to simplify X. If the simplification changed the code, it is likely
5157 that further simplification will help, so loop, but limit the number
5158 of repetitions that will be performed. */
5160 for (i = 0; i < 4; i++)
5162 /* If X is sufficiently simple, don't bother trying to do anything
5163 with it. */
5164 if (code != CONST_INT && code != REG && code != CLOBBER)
5165 x = combine_simplify_rtx (x, op0_mode, in_dest);
5167 if (GET_CODE (x) == code)
5168 break;
5170 code = GET_CODE (x);
5172 /* We no longer know the original mode of operand 0 since we
5173 have changed the form of X) */
5174 op0_mode = VOIDmode;
5177 return x;
5180 /* Simplify X, a piece of RTL. We just operate on the expression at the
5181 outer level; call `subst' to simplify recursively. Return the new
5182 expression.
5184 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5185 if we are inside a SET_DEST. */
5187 static rtx
5188 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
5190 enum rtx_code code = GET_CODE (x);
5191 enum machine_mode mode = GET_MODE (x);
5192 rtx temp;
5193 int i;
5195 /* If this is a commutative operation, put a constant last and a complex
5196 expression first. We don't need to do this for comparisons here. */
5197 if (COMMUTATIVE_ARITH_P (x)
5198 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5200 temp = XEXP (x, 0);
5201 SUBST (XEXP (x, 0), XEXP (x, 1));
5202 SUBST (XEXP (x, 1), temp);
5205 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5206 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5207 things. Check for cases where both arms are testing the same
5208 condition.
5210 Don't do anything if all operands are very simple. */
5212 if ((BINARY_P (x)
5213 && ((!OBJECT_P (XEXP (x, 0))
5214 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5215 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5216 || (!OBJECT_P (XEXP (x, 1))
5217 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5218 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5219 || (UNARY_P (x)
5220 && (!OBJECT_P (XEXP (x, 0))
5221 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5222 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5224 rtx cond, true_rtx, false_rtx;
5226 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5227 if (cond != 0
5228 /* If everything is a comparison, what we have is highly unlikely
5229 to be simpler, so don't use it. */
5230 && ! (COMPARISON_P (x)
5231 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5233 rtx cop1 = const0_rtx;
5234 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5236 if (cond_code == NE && COMPARISON_P (cond))
5237 return x;
5239 /* Simplify the alternative arms; this may collapse the true and
5240 false arms to store-flag values. Be careful to use copy_rtx
5241 here since true_rtx or false_rtx might share RTL with x as a
5242 result of the if_then_else_cond call above. */
5243 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
5244 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
5246 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5247 is unlikely to be simpler. */
5248 if (general_operand (true_rtx, VOIDmode)
5249 && general_operand (false_rtx, VOIDmode))
5251 enum rtx_code reversed;
5253 /* Restarting if we generate a store-flag expression will cause
5254 us to loop. Just drop through in this case. */
5256 /* If the result values are STORE_FLAG_VALUE and zero, we can
5257 just make the comparison operation. */
5258 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5259 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5260 cond, cop1);
5261 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5262 && ((reversed = reversed_comparison_code_parts
5263 (cond_code, cond, cop1, NULL))
5264 != UNKNOWN))
5265 x = simplify_gen_relational (reversed, mode, VOIDmode,
5266 cond, cop1);
5268 /* Likewise, we can make the negate of a comparison operation
5269 if the result values are - STORE_FLAG_VALUE and zero. */
5270 else if (CONST_INT_P (true_rtx)
5271 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5272 && false_rtx == const0_rtx)
5273 x = simplify_gen_unary (NEG, mode,
5274 simplify_gen_relational (cond_code,
5275 mode, VOIDmode,
5276 cond, cop1),
5277 mode);
5278 else if (CONST_INT_P (false_rtx)
5279 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5280 && true_rtx == const0_rtx
5281 && ((reversed = reversed_comparison_code_parts
5282 (cond_code, cond, cop1, NULL))
5283 != UNKNOWN))
5284 x = simplify_gen_unary (NEG, mode,
5285 simplify_gen_relational (reversed,
5286 mode, VOIDmode,
5287 cond, cop1),
5288 mode);
5289 else
5290 return gen_rtx_IF_THEN_ELSE (mode,
5291 simplify_gen_relational (cond_code,
5292 mode,
5293 VOIDmode,
5294 cond,
5295 cop1),
5296 true_rtx, false_rtx);
5298 code = GET_CODE (x);
5299 op0_mode = VOIDmode;
5304 /* Try to fold this expression in case we have constants that weren't
5305 present before. */
5306 temp = 0;
5307 switch (GET_RTX_CLASS (code))
5309 case RTX_UNARY:
5310 if (op0_mode == VOIDmode)
5311 op0_mode = GET_MODE (XEXP (x, 0));
5312 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5313 break;
5314 case RTX_COMPARE:
5315 case RTX_COMM_COMPARE:
5317 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5318 if (cmp_mode == VOIDmode)
5320 cmp_mode = GET_MODE (XEXP (x, 1));
5321 if (cmp_mode == VOIDmode)
5322 cmp_mode = op0_mode;
5324 temp = simplify_relational_operation (code, mode, cmp_mode,
5325 XEXP (x, 0), XEXP (x, 1));
5327 break;
5328 case RTX_COMM_ARITH:
5329 case RTX_BIN_ARITH:
5330 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5331 break;
5332 case RTX_BITFIELD_OPS:
5333 case RTX_TERNARY:
5334 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5335 XEXP (x, 1), XEXP (x, 2));
5336 break;
5337 default:
5338 break;
5341 if (temp)
5343 x = temp;
5344 code = GET_CODE (temp);
5345 op0_mode = VOIDmode;
5346 mode = GET_MODE (temp);
5349 /* First see if we can apply the inverse distributive law. */
5350 if (code == PLUS || code == MINUS
5351 || code == AND || code == IOR || code == XOR)
5353 x = apply_distributive_law (x);
5354 code = GET_CODE (x);
5355 op0_mode = VOIDmode;
5358 /* If CODE is an associative operation not otherwise handled, see if we
5359 can associate some operands. This can win if they are constants or
5360 if they are logically related (i.e. (a & b) & a). */
5361 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5362 || code == AND || code == IOR || code == XOR
5363 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5364 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5365 || (flag_associative_math && FLOAT_MODE_P (mode))))
5367 if (GET_CODE (XEXP (x, 0)) == code)
5369 rtx other = XEXP (XEXP (x, 0), 0);
5370 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5371 rtx inner_op1 = XEXP (x, 1);
5372 rtx inner;
5374 /* Make sure we pass the constant operand if any as the second
5375 one if this is a commutative operation. */
5376 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5378 rtx tem = inner_op0;
5379 inner_op0 = inner_op1;
5380 inner_op1 = tem;
5382 inner = simplify_binary_operation (code == MINUS ? PLUS
5383 : code == DIV ? MULT
5384 : code,
5385 mode, inner_op0, inner_op1);
5387 /* For commutative operations, try the other pair if that one
5388 didn't simplify. */
5389 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5391 other = XEXP (XEXP (x, 0), 1);
5392 inner = simplify_binary_operation (code, mode,
5393 XEXP (XEXP (x, 0), 0),
5394 XEXP (x, 1));
5397 if (inner)
5398 return simplify_gen_binary (code, mode, other, inner);
5402 /* A little bit of algebraic simplification here. */
5403 switch (code)
5405 case MEM:
5406 /* Ensure that our address has any ASHIFTs converted to MULT in case
5407 address-recognizing predicates are called later. */
5408 temp = make_compound_operation (XEXP (x, 0), MEM);
5409 SUBST (XEXP (x, 0), temp);
5410 break;
5412 case SUBREG:
5413 if (op0_mode == VOIDmode)
5414 op0_mode = GET_MODE (SUBREG_REG (x));
5416 /* See if this can be moved to simplify_subreg. */
5417 if (CONSTANT_P (SUBREG_REG (x))
5418 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5419 /* Don't call gen_lowpart if the inner mode
5420 is VOIDmode and we cannot simplify it, as SUBREG without
5421 inner mode is invalid. */
5422 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5423 || gen_lowpart_common (mode, SUBREG_REG (x))))
5424 return gen_lowpart (mode, SUBREG_REG (x));
5426 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5427 break;
5429 rtx temp;
5430 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5431 SUBREG_BYTE (x));
5432 if (temp)
5433 return temp;
5436 /* Don't change the mode of the MEM if that would change the meaning
5437 of the address. */
5438 if (MEM_P (SUBREG_REG (x))
5439 && (MEM_VOLATILE_P (SUBREG_REG (x))
5440 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5441 return gen_rtx_CLOBBER (mode, const0_rtx);
5443 /* Note that we cannot do any narrowing for non-constants since
5444 we might have been counting on using the fact that some bits were
5445 zero. We now do this in the SET. */
5447 break;
5449 case NEG:
5450 temp = expand_compound_operation (XEXP (x, 0));
5452 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5453 replaced by (lshiftrt X C). This will convert
5454 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5456 if (GET_CODE (temp) == ASHIFTRT
5457 && CONST_INT_P (XEXP (temp, 1))
5458 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5459 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5460 INTVAL (XEXP (temp, 1)));
5462 /* If X has only a single bit that might be nonzero, say, bit I, convert
5463 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5464 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5465 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5466 or a SUBREG of one since we'd be making the expression more
5467 complex if it was just a register. */
5469 if (!REG_P (temp)
5470 && ! (GET_CODE (temp) == SUBREG
5471 && REG_P (SUBREG_REG (temp)))
5472 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5474 rtx temp1 = simplify_shift_const
5475 (NULL_RTX, ASHIFTRT, mode,
5476 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5477 GET_MODE_BITSIZE (mode) - 1 - i),
5478 GET_MODE_BITSIZE (mode) - 1 - i);
5480 /* If all we did was surround TEMP with the two shifts, we
5481 haven't improved anything, so don't use it. Otherwise,
5482 we are better off with TEMP1. */
5483 if (GET_CODE (temp1) != ASHIFTRT
5484 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5485 || XEXP (XEXP (temp1, 0), 0) != temp)
5486 return temp1;
5488 break;
5490 case TRUNCATE:
5491 /* We can't handle truncation to a partial integer mode here
5492 because we don't know the real bitsize of the partial
5493 integer mode. */
5494 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5495 break;
5497 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5498 SUBST (XEXP (x, 0),
5499 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5500 GET_MODE_MASK (mode), 0));
5502 /* We can truncate a constant value and return it. */
5503 if (CONST_INT_P (XEXP (x, 0)))
5504 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5506 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5507 whose value is a comparison can be replaced with a subreg if
5508 STORE_FLAG_VALUE permits. */
5509 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5510 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5511 && (temp = get_last_value (XEXP (x, 0)))
5512 && COMPARISON_P (temp))
5513 return gen_lowpart (mode, XEXP (x, 0));
5514 break;
5516 case CONST:
5517 /* (const (const X)) can become (const X). Do it this way rather than
5518 returning the inner CONST since CONST can be shared with a
5519 REG_EQUAL note. */
5520 if (GET_CODE (XEXP (x, 0)) == CONST)
5521 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5522 break;
5524 #ifdef HAVE_lo_sum
5525 case LO_SUM:
5526 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5527 can add in an offset. find_split_point will split this address up
5528 again if it doesn't match. */
5529 if (GET_CODE (XEXP (x, 0)) == HIGH
5530 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5531 return XEXP (x, 1);
5532 break;
5533 #endif
5535 case PLUS:
5536 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5537 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5538 bit-field and can be replaced by either a sign_extend or a
5539 sign_extract. The `and' may be a zero_extend and the two
5540 <c>, -<c> constants may be reversed. */
5541 if (GET_CODE (XEXP (x, 0)) == XOR
5542 && CONST_INT_P (XEXP (x, 1))
5543 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5544 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5545 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5546 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5547 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5548 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5549 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5550 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5551 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5552 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5553 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5554 == (unsigned int) i + 1))))
5555 return simplify_shift_const
5556 (NULL_RTX, ASHIFTRT, mode,
5557 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5558 XEXP (XEXP (XEXP (x, 0), 0), 0),
5559 GET_MODE_BITSIZE (mode) - (i + 1)),
5560 GET_MODE_BITSIZE (mode) - (i + 1));
5562 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5563 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5564 the bitsize of the mode - 1. This allows simplification of
5565 "a = (b & 8) == 0;" */
5566 if (XEXP (x, 1) == constm1_rtx
5567 && !REG_P (XEXP (x, 0))
5568 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5569 && REG_P (SUBREG_REG (XEXP (x, 0))))
5570 && nonzero_bits (XEXP (x, 0), mode) == 1)
5571 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5572 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5573 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5574 GET_MODE_BITSIZE (mode) - 1),
5575 GET_MODE_BITSIZE (mode) - 1);
5577 /* If we are adding two things that have no bits in common, convert
5578 the addition into an IOR. This will often be further simplified,
5579 for example in cases like ((a & 1) + (a & 2)), which can
5580 become a & 3. */
5582 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5583 && (nonzero_bits (XEXP (x, 0), mode)
5584 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5586 /* Try to simplify the expression further. */
5587 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5588 temp = combine_simplify_rtx (tor, mode, in_dest);
5590 /* If we could, great. If not, do not go ahead with the IOR
5591 replacement, since PLUS appears in many special purpose
5592 address arithmetic instructions. */
5593 if (GET_CODE (temp) != CLOBBER && temp != tor)
5594 return temp;
5596 break;
5598 case MINUS:
5599 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5600 (and <foo> (const_int pow2-1)) */
5601 if (GET_CODE (XEXP (x, 1)) == AND
5602 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5603 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5604 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5605 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5606 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5607 break;
5609 case MULT:
5610 /* If we have (mult (plus A B) C), apply the distributive law and then
5611 the inverse distributive law to see if things simplify. This
5612 occurs mostly in addresses, often when unrolling loops. */
5614 if (GET_CODE (XEXP (x, 0)) == PLUS)
5616 rtx result = distribute_and_simplify_rtx (x, 0);
5617 if (result)
5618 return result;
5621 /* Try simplify a*(b/c) as (a*b)/c. */
5622 if (FLOAT_MODE_P (mode) && flag_associative_math
5623 && GET_CODE (XEXP (x, 0)) == DIV)
5625 rtx tem = simplify_binary_operation (MULT, mode,
5626 XEXP (XEXP (x, 0), 0),
5627 XEXP (x, 1));
5628 if (tem)
5629 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5631 break;
5633 case UDIV:
5634 /* If this is a divide by a power of two, treat it as a shift if
5635 its first operand is a shift. */
5636 if (CONST_INT_P (XEXP (x, 1))
5637 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5638 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5639 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5640 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5641 || GET_CODE (XEXP (x, 0)) == ROTATE
5642 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5643 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5644 break;
5646 case EQ: case NE:
5647 case GT: case GTU: case GE: case GEU:
5648 case LT: case LTU: case LE: case LEU:
5649 case UNEQ: case LTGT:
5650 case UNGT: case UNGE:
5651 case UNLT: case UNLE:
5652 case UNORDERED: case ORDERED:
5653 /* If the first operand is a condition code, we can't do anything
5654 with it. */
5655 if (GET_CODE (XEXP (x, 0)) == COMPARE
5656 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5657 && ! CC0_P (XEXP (x, 0))))
5659 rtx op0 = XEXP (x, 0);
5660 rtx op1 = XEXP (x, 1);
5661 enum rtx_code new_code;
5663 if (GET_CODE (op0) == COMPARE)
5664 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5666 /* Simplify our comparison, if possible. */
5667 new_code = simplify_comparison (code, &op0, &op1);
5669 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5670 if only the low-order bit is possibly nonzero in X (such as when
5671 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5672 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5673 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5674 (plus X 1).
5676 Remove any ZERO_EXTRACT we made when thinking this was a
5677 comparison. It may now be simpler to use, e.g., an AND. If a
5678 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5679 the call to make_compound_operation in the SET case. */
5681 if (STORE_FLAG_VALUE == 1
5682 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5683 && op1 == const0_rtx
5684 && mode == GET_MODE (op0)
5685 && nonzero_bits (op0, mode) == 1)
5686 return gen_lowpart (mode,
5687 expand_compound_operation (op0));
5689 else if (STORE_FLAG_VALUE == 1
5690 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5691 && op1 == const0_rtx
5692 && mode == GET_MODE (op0)
5693 && (num_sign_bit_copies (op0, mode)
5694 == GET_MODE_BITSIZE (mode)))
5696 op0 = expand_compound_operation (op0);
5697 return simplify_gen_unary (NEG, mode,
5698 gen_lowpart (mode, op0),
5699 mode);
5702 else if (STORE_FLAG_VALUE == 1
5703 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5704 && op1 == const0_rtx
5705 && mode == GET_MODE (op0)
5706 && nonzero_bits (op0, mode) == 1)
5708 op0 = expand_compound_operation (op0);
5709 return simplify_gen_binary (XOR, mode,
5710 gen_lowpart (mode, op0),
5711 const1_rtx);
5714 else if (STORE_FLAG_VALUE == 1
5715 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5716 && op1 == const0_rtx
5717 && mode == GET_MODE (op0)
5718 && (num_sign_bit_copies (op0, mode)
5719 == GET_MODE_BITSIZE (mode)))
5721 op0 = expand_compound_operation (op0);
5722 return plus_constant (gen_lowpart (mode, op0), 1);
5725 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5726 those above. */
5727 if (STORE_FLAG_VALUE == -1
5728 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5729 && op1 == const0_rtx
5730 && (num_sign_bit_copies (op0, mode)
5731 == GET_MODE_BITSIZE (mode)))
5732 return gen_lowpart (mode,
5733 expand_compound_operation (op0));
5735 else if (STORE_FLAG_VALUE == -1
5736 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5737 && op1 == const0_rtx
5738 && mode == GET_MODE (op0)
5739 && nonzero_bits (op0, mode) == 1)
5741 op0 = expand_compound_operation (op0);
5742 return simplify_gen_unary (NEG, mode,
5743 gen_lowpart (mode, op0),
5744 mode);
5747 else if (STORE_FLAG_VALUE == -1
5748 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5749 && op1 == const0_rtx
5750 && mode == GET_MODE (op0)
5751 && (num_sign_bit_copies (op0, mode)
5752 == GET_MODE_BITSIZE (mode)))
5754 op0 = expand_compound_operation (op0);
5755 return simplify_gen_unary (NOT, mode,
5756 gen_lowpart (mode, op0),
5757 mode);
5760 /* If X is 0/1, (eq X 0) is X-1. */
5761 else if (STORE_FLAG_VALUE == -1
5762 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5763 && op1 == const0_rtx
5764 && mode == GET_MODE (op0)
5765 && nonzero_bits (op0, mode) == 1)
5767 op0 = expand_compound_operation (op0);
5768 return plus_constant (gen_lowpart (mode, op0), -1);
5771 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5772 one bit that might be nonzero, we can convert (ne x 0) to
5773 (ashift x c) where C puts the bit in the sign bit. Remove any
5774 AND with STORE_FLAG_VALUE when we are done, since we are only
5775 going to test the sign bit. */
5776 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5777 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5778 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5779 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5780 && op1 == const0_rtx
5781 && mode == GET_MODE (op0)
5782 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5784 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5785 expand_compound_operation (op0),
5786 GET_MODE_BITSIZE (mode) - 1 - i);
5787 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5788 return XEXP (x, 0);
5789 else
5790 return x;
5793 /* If the code changed, return a whole new comparison. */
5794 if (new_code != code)
5795 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5797 /* Otherwise, keep this operation, but maybe change its operands.
5798 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5799 SUBST (XEXP (x, 0), op0);
5800 SUBST (XEXP (x, 1), op1);
5802 break;
5804 case IF_THEN_ELSE:
5805 return simplify_if_then_else (x);
5807 case ZERO_EXTRACT:
5808 case SIGN_EXTRACT:
5809 case ZERO_EXTEND:
5810 case SIGN_EXTEND:
5811 /* If we are processing SET_DEST, we are done. */
5812 if (in_dest)
5813 return x;
5815 return expand_compound_operation (x);
5817 case SET:
5818 return simplify_set (x);
5820 case AND:
5821 case IOR:
5822 return simplify_logical (x);
5824 case ASHIFT:
5825 case LSHIFTRT:
5826 case ASHIFTRT:
5827 case ROTATE:
5828 case ROTATERT:
5829 /* If this is a shift by a constant amount, simplify it. */
5830 if (CONST_INT_P (XEXP (x, 1)))
5831 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5832 INTVAL (XEXP (x, 1)));
5834 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5835 SUBST (XEXP (x, 1),
5836 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5837 ((unsigned HOST_WIDE_INT) 1
5838 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5839 - 1,
5840 0));
5841 break;
5843 default:
5844 break;
5847 return x;
5850 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5852 static rtx
5853 simplify_if_then_else (rtx x)
5855 enum machine_mode mode = GET_MODE (x);
5856 rtx cond = XEXP (x, 0);
5857 rtx true_rtx = XEXP (x, 1);
5858 rtx false_rtx = XEXP (x, 2);
5859 enum rtx_code true_code = GET_CODE (cond);
5860 int comparison_p = COMPARISON_P (cond);
5861 rtx temp;
5862 int i;
5863 enum rtx_code false_code;
5864 rtx reversed;
5866 /* Simplify storing of the truth value. */
5867 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5868 return simplify_gen_relational (true_code, mode, VOIDmode,
5869 XEXP (cond, 0), XEXP (cond, 1));
5871 /* Also when the truth value has to be reversed. */
5872 if (comparison_p
5873 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5874 && (reversed = reversed_comparison (cond, mode)))
5875 return reversed;
5877 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5878 in it is being compared against certain values. Get the true and false
5879 comparisons and see if that says anything about the value of each arm. */
5881 if (comparison_p
5882 && ((false_code = reversed_comparison_code (cond, NULL))
5883 != UNKNOWN)
5884 && REG_P (XEXP (cond, 0)))
5886 HOST_WIDE_INT nzb;
5887 rtx from = XEXP (cond, 0);
5888 rtx true_val = XEXP (cond, 1);
5889 rtx false_val = true_val;
5890 int swapped = 0;
5892 /* If FALSE_CODE is EQ, swap the codes and arms. */
5894 if (false_code == EQ)
5896 swapped = 1, true_code = EQ, false_code = NE;
5897 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5900 /* If we are comparing against zero and the expression being tested has
5901 only a single bit that might be nonzero, that is its value when it is
5902 not equal to zero. Similarly if it is known to be -1 or 0. */
5904 if (true_code == EQ && true_val == const0_rtx
5905 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5907 false_code = EQ;
5908 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5910 else if (true_code == EQ && true_val == const0_rtx
5911 && (num_sign_bit_copies (from, GET_MODE (from))
5912 == GET_MODE_BITSIZE (GET_MODE (from))))
5914 false_code = EQ;
5915 false_val = constm1_rtx;
5918 /* Now simplify an arm if we know the value of the register in the
5919 branch and it is used in the arm. Be careful due to the potential
5920 of locally-shared RTL. */
5922 if (reg_mentioned_p (from, true_rtx))
5923 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5924 from, true_val),
5925 pc_rtx, pc_rtx, 0, 0);
5926 if (reg_mentioned_p (from, false_rtx))
5927 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5928 from, false_val),
5929 pc_rtx, pc_rtx, 0, 0);
5931 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5932 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5934 true_rtx = XEXP (x, 1);
5935 false_rtx = XEXP (x, 2);
5936 true_code = GET_CODE (cond);
5939 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5940 reversed, do so to avoid needing two sets of patterns for
5941 subtract-and-branch insns. Similarly if we have a constant in the true
5942 arm, the false arm is the same as the first operand of the comparison, or
5943 the false arm is more complicated than the true arm. */
5945 if (comparison_p
5946 && reversed_comparison_code (cond, NULL) != UNKNOWN
5947 && (true_rtx == pc_rtx
5948 || (CONSTANT_P (true_rtx)
5949 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5950 || true_rtx == const0_rtx
5951 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5952 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5953 && !OBJECT_P (false_rtx))
5954 || reg_mentioned_p (true_rtx, false_rtx)
5955 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5957 true_code = reversed_comparison_code (cond, NULL);
5958 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5959 SUBST (XEXP (x, 1), false_rtx);
5960 SUBST (XEXP (x, 2), true_rtx);
5962 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5963 cond = XEXP (x, 0);
5965 /* It is possible that the conditional has been simplified out. */
5966 true_code = GET_CODE (cond);
5967 comparison_p = COMPARISON_P (cond);
5970 /* If the two arms are identical, we don't need the comparison. */
5972 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5973 return true_rtx;
5975 /* Convert a == b ? b : a to "a". */
5976 if (true_code == EQ && ! side_effects_p (cond)
5977 && !HONOR_NANS (mode)
5978 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5979 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5980 return false_rtx;
5981 else if (true_code == NE && ! side_effects_p (cond)
5982 && !HONOR_NANS (mode)
5983 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5984 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5985 return true_rtx;
5987 /* Look for cases where we have (abs x) or (neg (abs X)). */
5989 if (GET_MODE_CLASS (mode) == MODE_INT
5990 && comparison_p
5991 && XEXP (cond, 1) == const0_rtx
5992 && GET_CODE (false_rtx) == NEG
5993 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5994 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5995 && ! side_effects_p (true_rtx))
5996 switch (true_code)
5998 case GT:
5999 case GE:
6000 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6001 case LT:
6002 case LE:
6003 return
6004 simplify_gen_unary (NEG, mode,
6005 simplify_gen_unary (ABS, mode, true_rtx, mode),
6006 mode);
6007 default:
6008 break;
6011 /* Look for MIN or MAX. */
6013 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6014 && comparison_p
6015 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6016 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6017 && ! side_effects_p (cond))
6018 switch (true_code)
6020 case GE:
6021 case GT:
6022 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6023 case LE:
6024 case LT:
6025 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6026 case GEU:
6027 case GTU:
6028 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6029 case LEU:
6030 case LTU:
6031 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6032 default:
6033 break;
6036 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6037 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6038 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6039 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6040 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6041 neither 1 or -1, but it isn't worth checking for. */
6043 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6044 && comparison_p
6045 && GET_MODE_CLASS (mode) == MODE_INT
6046 && ! side_effects_p (x))
6048 rtx t = make_compound_operation (true_rtx, SET);
6049 rtx f = make_compound_operation (false_rtx, SET);
6050 rtx cond_op0 = XEXP (cond, 0);
6051 rtx cond_op1 = XEXP (cond, 1);
6052 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6053 enum machine_mode m = mode;
6054 rtx z = 0, c1 = NULL_RTX;
6056 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6057 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6058 || GET_CODE (t) == ASHIFT
6059 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6060 && rtx_equal_p (XEXP (t, 0), f))
6061 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6063 /* If an identity-zero op is commutative, check whether there
6064 would be a match if we swapped the operands. */
6065 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6066 || GET_CODE (t) == XOR)
6067 && rtx_equal_p (XEXP (t, 1), f))
6068 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6069 else if (GET_CODE (t) == SIGN_EXTEND
6070 && (GET_CODE (XEXP (t, 0)) == PLUS
6071 || GET_CODE (XEXP (t, 0)) == MINUS
6072 || GET_CODE (XEXP (t, 0)) == IOR
6073 || GET_CODE (XEXP (t, 0)) == XOR
6074 || GET_CODE (XEXP (t, 0)) == ASHIFT
6075 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6076 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6077 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6078 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6079 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6080 && (num_sign_bit_copies (f, GET_MODE (f))
6081 > (unsigned int)
6082 (GET_MODE_BITSIZE (mode)
6083 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6085 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6086 extend_op = SIGN_EXTEND;
6087 m = GET_MODE (XEXP (t, 0));
6089 else if (GET_CODE (t) == SIGN_EXTEND
6090 && (GET_CODE (XEXP (t, 0)) == PLUS
6091 || GET_CODE (XEXP (t, 0)) == IOR
6092 || GET_CODE (XEXP (t, 0)) == XOR)
6093 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6094 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6095 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6096 && (num_sign_bit_copies (f, GET_MODE (f))
6097 > (unsigned int)
6098 (GET_MODE_BITSIZE (mode)
6099 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6101 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6102 extend_op = SIGN_EXTEND;
6103 m = GET_MODE (XEXP (t, 0));
6105 else if (GET_CODE (t) == ZERO_EXTEND
6106 && (GET_CODE (XEXP (t, 0)) == PLUS
6107 || GET_CODE (XEXP (t, 0)) == MINUS
6108 || GET_CODE (XEXP (t, 0)) == IOR
6109 || GET_CODE (XEXP (t, 0)) == XOR
6110 || GET_CODE (XEXP (t, 0)) == ASHIFT
6111 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6112 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6113 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6114 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6115 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6116 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6117 && ((nonzero_bits (f, GET_MODE (f))
6118 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6119 == 0))
6121 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6122 extend_op = ZERO_EXTEND;
6123 m = GET_MODE (XEXP (t, 0));
6125 else if (GET_CODE (t) == ZERO_EXTEND
6126 && (GET_CODE (XEXP (t, 0)) == PLUS
6127 || GET_CODE (XEXP (t, 0)) == IOR
6128 || GET_CODE (XEXP (t, 0)) == XOR)
6129 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6130 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6131 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6132 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6133 && ((nonzero_bits (f, GET_MODE (f))
6134 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6135 == 0))
6137 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6138 extend_op = ZERO_EXTEND;
6139 m = GET_MODE (XEXP (t, 0));
6142 if (z)
6144 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6145 cond_op0, cond_op1),
6146 pc_rtx, pc_rtx, 0, 0);
6147 temp = simplify_gen_binary (MULT, m, temp,
6148 simplify_gen_binary (MULT, m, c1,
6149 const_true_rtx));
6150 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
6151 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6153 if (extend_op != UNKNOWN)
6154 temp = simplify_gen_unary (extend_op, mode, temp, m);
6156 return temp;
6160 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6161 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6162 negation of a single bit, we can convert this operation to a shift. We
6163 can actually do this more generally, but it doesn't seem worth it. */
6165 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6166 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6167 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6168 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6169 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6170 == GET_MODE_BITSIZE (mode))
6171 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6172 return
6173 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6174 gen_lowpart (mode, XEXP (cond, 0)), i);
6176 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6177 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6178 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6179 && GET_MODE (XEXP (cond, 0)) == mode
6180 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6181 == nonzero_bits (XEXP (cond, 0), mode)
6182 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6183 return XEXP (cond, 0);
6185 return x;
6188 /* Simplify X, a SET expression. Return the new expression. */
6190 static rtx
6191 simplify_set (rtx x)
6193 rtx src = SET_SRC (x);
6194 rtx dest = SET_DEST (x);
6195 enum machine_mode mode
6196 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6197 rtx other_insn;
6198 rtx *cc_use;
6200 /* (set (pc) (return)) gets written as (return). */
6201 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
6202 return src;
6204 /* Now that we know for sure which bits of SRC we are using, see if we can
6205 simplify the expression for the object knowing that we only need the
6206 low-order bits. */
6208 if (GET_MODE_CLASS (mode) == MODE_INT
6209 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6211 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6212 SUBST (SET_SRC (x), src);
6215 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6216 the comparison result and try to simplify it unless we already have used
6217 undobuf.other_insn. */
6218 if ((GET_MODE_CLASS (mode) == MODE_CC
6219 || GET_CODE (src) == COMPARE
6220 || CC0_P (dest))
6221 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6222 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6223 && COMPARISON_P (*cc_use)
6224 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6226 enum rtx_code old_code = GET_CODE (*cc_use);
6227 enum rtx_code new_code;
6228 rtx op0, op1, tmp;
6229 int other_changed = 0;
6230 enum machine_mode compare_mode = GET_MODE (dest);
6232 if (GET_CODE (src) == COMPARE)
6233 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6234 else
6235 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6237 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6238 op0, op1);
6239 if (!tmp)
6240 new_code = old_code;
6241 else if (!CONSTANT_P (tmp))
6243 new_code = GET_CODE (tmp);
6244 op0 = XEXP (tmp, 0);
6245 op1 = XEXP (tmp, 1);
6247 else
6249 rtx pat = PATTERN (other_insn);
6250 undobuf.other_insn = other_insn;
6251 SUBST (*cc_use, tmp);
6253 /* Attempt to simplify CC user. */
6254 if (GET_CODE (pat) == SET)
6256 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6257 if (new_rtx != NULL_RTX)
6258 SUBST (SET_SRC (pat), new_rtx);
6261 /* Convert X into a no-op move. */
6262 SUBST (SET_DEST (x), pc_rtx);
6263 SUBST (SET_SRC (x), pc_rtx);
6264 return x;
6267 /* Simplify our comparison, if possible. */
6268 new_code = simplify_comparison (new_code, &op0, &op1);
6270 #ifdef SELECT_CC_MODE
6271 /* If this machine has CC modes other than CCmode, check to see if we
6272 need to use a different CC mode here. */
6273 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6274 compare_mode = GET_MODE (op0);
6275 else
6276 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6278 #ifndef HAVE_cc0
6279 /* If the mode changed, we have to change SET_DEST, the mode in the
6280 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6281 a hard register, just build new versions with the proper mode. If it
6282 is a pseudo, we lose unless it is only time we set the pseudo, in
6283 which case we can safely change its mode. */
6284 if (compare_mode != GET_MODE (dest))
6286 if (can_change_dest_mode (dest, 0, compare_mode))
6288 unsigned int regno = REGNO (dest);
6289 rtx new_dest;
6291 if (regno < FIRST_PSEUDO_REGISTER)
6292 new_dest = gen_rtx_REG (compare_mode, regno);
6293 else
6295 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6296 new_dest = regno_reg_rtx[regno];
6299 SUBST (SET_DEST (x), new_dest);
6300 SUBST (XEXP (*cc_use, 0), new_dest);
6301 other_changed = 1;
6303 dest = new_dest;
6306 #endif /* cc0 */
6307 #endif /* SELECT_CC_MODE */
6309 /* If the code changed, we have to build a new comparison in
6310 undobuf.other_insn. */
6311 if (new_code != old_code)
6313 int other_changed_previously = other_changed;
6314 unsigned HOST_WIDE_INT mask;
6315 rtx old_cc_use = *cc_use;
6317 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6318 dest, const0_rtx));
6319 other_changed = 1;
6321 /* If the only change we made was to change an EQ into an NE or
6322 vice versa, OP0 has only one bit that might be nonzero, and OP1
6323 is zero, check if changing the user of the condition code will
6324 produce a valid insn. If it won't, we can keep the original code
6325 in that insn by surrounding our operation with an XOR. */
6327 if (((old_code == NE && new_code == EQ)
6328 || (old_code == EQ && new_code == NE))
6329 && ! other_changed_previously && op1 == const0_rtx
6330 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
6331 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6333 rtx pat = PATTERN (other_insn), note = 0;
6335 if ((recog_for_combine (&pat, other_insn, &note) < 0
6336 && ! check_asm_operands (pat)))
6338 *cc_use = old_cc_use;
6339 other_changed = 0;
6341 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6342 op0, GEN_INT (mask));
6347 if (other_changed)
6348 undobuf.other_insn = other_insn;
6350 /* Otherwise, if we didn't previously have a COMPARE in the
6351 correct mode, we need one. */
6352 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6354 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6355 src = SET_SRC (x);
6357 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6359 SUBST (SET_SRC (x), op0);
6360 src = SET_SRC (x);
6362 /* Otherwise, update the COMPARE if needed. */
6363 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6365 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6366 src = SET_SRC (x);
6369 else
6371 /* Get SET_SRC in a form where we have placed back any
6372 compound expressions. Then do the checks below. */
6373 src = make_compound_operation (src, SET);
6374 SUBST (SET_SRC (x), src);
6377 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6378 and X being a REG or (subreg (reg)), we may be able to convert this to
6379 (set (subreg:m2 x) (op)).
6381 We can always do this if M1 is narrower than M2 because that means that
6382 we only care about the low bits of the result.
6384 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6385 perform a narrower operation than requested since the high-order bits will
6386 be undefined. On machine where it is defined, this transformation is safe
6387 as long as M1 and M2 have the same number of words. */
6389 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6390 && !OBJECT_P (SUBREG_REG (src))
6391 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6392 / UNITS_PER_WORD)
6393 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6394 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6395 #ifndef WORD_REGISTER_OPERATIONS
6396 && (GET_MODE_SIZE (GET_MODE (src))
6397 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6398 #endif
6399 #ifdef CANNOT_CHANGE_MODE_CLASS
6400 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6401 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6402 GET_MODE (SUBREG_REG (src)),
6403 GET_MODE (src)))
6404 #endif
6405 && (REG_P (dest)
6406 || (GET_CODE (dest) == SUBREG
6407 && REG_P (SUBREG_REG (dest)))))
6409 SUBST (SET_DEST (x),
6410 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6411 dest));
6412 SUBST (SET_SRC (x), SUBREG_REG (src));
6414 src = SET_SRC (x), dest = SET_DEST (x);
6417 #ifdef HAVE_cc0
6418 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6419 in SRC. */
6420 if (dest == cc0_rtx
6421 && GET_CODE (src) == SUBREG
6422 && subreg_lowpart_p (src)
6423 && (GET_MODE_BITSIZE (GET_MODE (src))
6424 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6426 rtx inner = SUBREG_REG (src);
6427 enum machine_mode inner_mode = GET_MODE (inner);
6429 /* Here we make sure that we don't have a sign bit on. */
6430 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6431 && (nonzero_bits (inner, inner_mode)
6432 < ((unsigned HOST_WIDE_INT) 1
6433 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6435 SUBST (SET_SRC (x), inner);
6436 src = SET_SRC (x);
6439 #endif
6441 #ifdef LOAD_EXTEND_OP
6442 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6443 would require a paradoxical subreg. Replace the subreg with a
6444 zero_extend to avoid the reload that would otherwise be required. */
6446 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6447 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6448 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6449 && SUBREG_BYTE (src) == 0
6450 && (GET_MODE_SIZE (GET_MODE (src))
6451 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6452 && MEM_P (SUBREG_REG (src)))
6454 SUBST (SET_SRC (x),
6455 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6456 GET_MODE (src), SUBREG_REG (src)));
6458 src = SET_SRC (x);
6460 #endif
6462 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6463 are comparing an item known to be 0 or -1 against 0, use a logical
6464 operation instead. Check for one of the arms being an IOR of the other
6465 arm with some value. We compute three terms to be IOR'ed together. In
6466 practice, at most two will be nonzero. Then we do the IOR's. */
6468 if (GET_CODE (dest) != PC
6469 && GET_CODE (src) == IF_THEN_ELSE
6470 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6471 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6472 && XEXP (XEXP (src, 0), 1) == const0_rtx
6473 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6474 #ifdef HAVE_conditional_move
6475 && ! can_conditionally_move_p (GET_MODE (src))
6476 #endif
6477 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6478 GET_MODE (XEXP (XEXP (src, 0), 0)))
6479 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6480 && ! side_effects_p (src))
6482 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6483 ? XEXP (src, 1) : XEXP (src, 2));
6484 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6485 ? XEXP (src, 2) : XEXP (src, 1));
6486 rtx term1 = const0_rtx, term2, term3;
6488 if (GET_CODE (true_rtx) == IOR
6489 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6490 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6491 else if (GET_CODE (true_rtx) == IOR
6492 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6493 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6494 else if (GET_CODE (false_rtx) == IOR
6495 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6496 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6497 else if (GET_CODE (false_rtx) == IOR
6498 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6499 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6501 term2 = simplify_gen_binary (AND, GET_MODE (src),
6502 XEXP (XEXP (src, 0), 0), true_rtx);
6503 term3 = simplify_gen_binary (AND, GET_MODE (src),
6504 simplify_gen_unary (NOT, GET_MODE (src),
6505 XEXP (XEXP (src, 0), 0),
6506 GET_MODE (src)),
6507 false_rtx);
6509 SUBST (SET_SRC (x),
6510 simplify_gen_binary (IOR, GET_MODE (src),
6511 simplify_gen_binary (IOR, GET_MODE (src),
6512 term1, term2),
6513 term3));
6515 src = SET_SRC (x);
6518 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6519 whole thing fail. */
6520 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6521 return src;
6522 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6523 return dest;
6524 else
6525 /* Convert this into a field assignment operation, if possible. */
6526 return make_field_assignment (x);
6529 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6530 result. */
6532 static rtx
6533 simplify_logical (rtx x)
6535 enum machine_mode mode = GET_MODE (x);
6536 rtx op0 = XEXP (x, 0);
6537 rtx op1 = XEXP (x, 1);
6539 switch (GET_CODE (x))
6541 case AND:
6542 /* We can call simplify_and_const_int only if we don't lose
6543 any (sign) bits when converting INTVAL (op1) to
6544 "unsigned HOST_WIDE_INT". */
6545 if (CONST_INT_P (op1)
6546 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6547 || INTVAL (op1) > 0))
6549 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6550 if (GET_CODE (x) != AND)
6551 return x;
6553 op0 = XEXP (x, 0);
6554 op1 = XEXP (x, 1);
6557 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6558 apply the distributive law and then the inverse distributive
6559 law to see if things simplify. */
6560 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6562 rtx result = distribute_and_simplify_rtx (x, 0);
6563 if (result)
6564 return result;
6566 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6568 rtx result = distribute_and_simplify_rtx (x, 1);
6569 if (result)
6570 return result;
6572 break;
6574 case IOR:
6575 /* If we have (ior (and A B) C), apply the distributive law and then
6576 the inverse distributive law to see if things simplify. */
6578 if (GET_CODE (op0) == AND)
6580 rtx result = distribute_and_simplify_rtx (x, 0);
6581 if (result)
6582 return result;
6585 if (GET_CODE (op1) == AND)
6587 rtx result = distribute_and_simplify_rtx (x, 1);
6588 if (result)
6589 return result;
6591 break;
6593 default:
6594 gcc_unreachable ();
6597 return x;
6600 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6601 operations" because they can be replaced with two more basic operations.
6602 ZERO_EXTEND is also considered "compound" because it can be replaced with
6603 an AND operation, which is simpler, though only one operation.
6605 The function expand_compound_operation is called with an rtx expression
6606 and will convert it to the appropriate shifts and AND operations,
6607 simplifying at each stage.
6609 The function make_compound_operation is called to convert an expression
6610 consisting of shifts and ANDs into the equivalent compound expression.
6611 It is the inverse of this function, loosely speaking. */
6613 static rtx
6614 expand_compound_operation (rtx x)
6616 unsigned HOST_WIDE_INT pos = 0, len;
6617 int unsignedp = 0;
6618 unsigned int modewidth;
6619 rtx tem;
6621 switch (GET_CODE (x))
6623 case ZERO_EXTEND:
6624 unsignedp = 1;
6625 case SIGN_EXTEND:
6626 /* We can't necessarily use a const_int for a multiword mode;
6627 it depends on implicitly extending the value.
6628 Since we don't know the right way to extend it,
6629 we can't tell whether the implicit way is right.
6631 Even for a mode that is no wider than a const_int,
6632 we can't win, because we need to sign extend one of its bits through
6633 the rest of it, and we don't know which bit. */
6634 if (CONST_INT_P (XEXP (x, 0)))
6635 return x;
6637 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6638 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6639 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6640 reloaded. If not for that, MEM's would very rarely be safe.
6642 Reject MODEs bigger than a word, because we might not be able
6643 to reference a two-register group starting with an arbitrary register
6644 (and currently gen_lowpart might crash for a SUBREG). */
6646 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6647 return x;
6649 /* Reject MODEs that aren't scalar integers because turning vector
6650 or complex modes into shifts causes problems. */
6652 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6653 return x;
6655 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6656 /* If the inner object has VOIDmode (the only way this can happen
6657 is if it is an ASM_OPERANDS), we can't do anything since we don't
6658 know how much masking to do. */
6659 if (len == 0)
6660 return x;
6662 break;
6664 case ZERO_EXTRACT:
6665 unsignedp = 1;
6667 /* ... fall through ... */
6669 case SIGN_EXTRACT:
6670 /* If the operand is a CLOBBER, just return it. */
6671 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6672 return XEXP (x, 0);
6674 if (!CONST_INT_P (XEXP (x, 1))
6675 || !CONST_INT_P (XEXP (x, 2))
6676 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6677 return x;
6679 /* Reject MODEs that aren't scalar integers because turning vector
6680 or complex modes into shifts causes problems. */
6682 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6683 return x;
6685 len = INTVAL (XEXP (x, 1));
6686 pos = INTVAL (XEXP (x, 2));
6688 /* This should stay within the object being extracted, fail otherwise. */
6689 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6690 return x;
6692 if (BITS_BIG_ENDIAN)
6693 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6695 break;
6697 default:
6698 return x;
6700 /* Convert sign extension to zero extension, if we know that the high
6701 bit is not set, as this is easier to optimize. It will be converted
6702 back to cheaper alternative in make_extraction. */
6703 if (GET_CODE (x) == SIGN_EXTEND
6704 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6705 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6706 & ~(((unsigned HOST_WIDE_INT)
6707 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6708 >> 1))
6709 == 0)))
6711 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6712 rtx temp2 = expand_compound_operation (temp);
6714 /* Make sure this is a profitable operation. */
6715 if (rtx_cost (x, SET, optimize_this_for_speed_p)
6716 > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6717 return temp2;
6718 else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6719 > rtx_cost (temp, SET, optimize_this_for_speed_p))
6720 return temp;
6721 else
6722 return x;
6725 /* We can optimize some special cases of ZERO_EXTEND. */
6726 if (GET_CODE (x) == ZERO_EXTEND)
6728 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6729 know that the last value didn't have any inappropriate bits
6730 set. */
6731 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6732 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6733 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6734 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6735 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6736 return XEXP (XEXP (x, 0), 0);
6738 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6739 if (GET_CODE (XEXP (x, 0)) == SUBREG
6740 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6741 && subreg_lowpart_p (XEXP (x, 0))
6742 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6743 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6744 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6745 return SUBREG_REG (XEXP (x, 0));
6747 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6748 is a comparison and STORE_FLAG_VALUE permits. This is like
6749 the first case, but it works even when GET_MODE (x) is larger
6750 than HOST_WIDE_INT. */
6751 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6752 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6753 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6754 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6755 <= HOST_BITS_PER_WIDE_INT)
6756 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6757 return XEXP (XEXP (x, 0), 0);
6759 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6760 if (GET_CODE (XEXP (x, 0)) == SUBREG
6761 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6762 && subreg_lowpart_p (XEXP (x, 0))
6763 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6764 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6765 <= HOST_BITS_PER_WIDE_INT)
6766 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6767 return SUBREG_REG (XEXP (x, 0));
6771 /* If we reach here, we want to return a pair of shifts. The inner
6772 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6773 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6774 logical depending on the value of UNSIGNEDP.
6776 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6777 converted into an AND of a shift.
6779 We must check for the case where the left shift would have a negative
6780 count. This can happen in a case like (x >> 31) & 255 on machines
6781 that can't shift by a constant. On those machines, we would first
6782 combine the shift with the AND to produce a variable-position
6783 extraction. Then the constant of 31 would be substituted in
6784 to produce such a position. */
6786 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6787 if (modewidth >= pos + len)
6789 enum machine_mode mode = GET_MODE (x);
6790 tem = gen_lowpart (mode, XEXP (x, 0));
6791 if (!tem || GET_CODE (tem) == CLOBBER)
6792 return x;
6793 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6794 tem, modewidth - pos - len);
6795 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6796 mode, tem, modewidth - len);
6798 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6799 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6800 simplify_shift_const (NULL_RTX, LSHIFTRT,
6801 GET_MODE (x),
6802 XEXP (x, 0), pos),
6803 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6804 else
6805 /* Any other cases we can't handle. */
6806 return x;
6808 /* If we couldn't do this for some reason, return the original
6809 expression. */
6810 if (GET_CODE (tem) == CLOBBER)
6811 return x;
6813 return tem;
6816 /* X is a SET which contains an assignment of one object into
6817 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6818 or certain SUBREGS). If possible, convert it into a series of
6819 logical operations.
6821 We half-heartedly support variable positions, but do not at all
6822 support variable lengths. */
6824 static const_rtx
6825 expand_field_assignment (const_rtx x)
6827 rtx inner;
6828 rtx pos; /* Always counts from low bit. */
6829 int len;
6830 rtx mask, cleared, masked;
6831 enum machine_mode compute_mode;
6833 /* Loop until we find something we can't simplify. */
6834 while (1)
6836 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6837 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6839 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6840 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6841 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6843 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6844 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6846 inner = XEXP (SET_DEST (x), 0);
6847 len = INTVAL (XEXP (SET_DEST (x), 1));
6848 pos = XEXP (SET_DEST (x), 2);
6850 /* A constant position should stay within the width of INNER. */
6851 if (CONST_INT_P (pos)
6852 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6853 break;
6855 if (BITS_BIG_ENDIAN)
6857 if (CONST_INT_P (pos))
6858 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6859 - INTVAL (pos));
6860 else if (GET_CODE (pos) == MINUS
6861 && CONST_INT_P (XEXP (pos, 1))
6862 && (INTVAL (XEXP (pos, 1))
6863 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6864 /* If position is ADJUST - X, new position is X. */
6865 pos = XEXP (pos, 0);
6866 else
6867 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6868 GEN_INT (GET_MODE_BITSIZE (
6869 GET_MODE (inner))
6870 - len),
6871 pos);
6875 /* A SUBREG between two modes that occupy the same numbers of words
6876 can be done by moving the SUBREG to the source. */
6877 else if (GET_CODE (SET_DEST (x)) == SUBREG
6878 /* We need SUBREGs to compute nonzero_bits properly. */
6879 && nonzero_sign_valid
6880 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6881 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6882 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6883 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6885 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6886 gen_lowpart
6887 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6888 SET_SRC (x)));
6889 continue;
6891 else
6892 break;
6894 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6895 inner = SUBREG_REG (inner);
6897 compute_mode = GET_MODE (inner);
6899 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6900 if (! SCALAR_INT_MODE_P (compute_mode))
6902 enum machine_mode imode;
6904 /* Don't do anything for vector or complex integral types. */
6905 if (! FLOAT_MODE_P (compute_mode))
6906 break;
6908 /* Try to find an integral mode to pun with. */
6909 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6910 if (imode == BLKmode)
6911 break;
6913 compute_mode = imode;
6914 inner = gen_lowpart (imode, inner);
6917 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6918 if (len >= HOST_BITS_PER_WIDE_INT)
6919 break;
6921 /* Now compute the equivalent expression. Make a copy of INNER
6922 for the SET_DEST in case it is a MEM into which we will substitute;
6923 we don't want shared RTL in that case. */
6924 mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
6925 cleared = simplify_gen_binary (AND, compute_mode,
6926 simplify_gen_unary (NOT, compute_mode,
6927 simplify_gen_binary (ASHIFT,
6928 compute_mode,
6929 mask, pos),
6930 compute_mode),
6931 inner);
6932 masked = simplify_gen_binary (ASHIFT, compute_mode,
6933 simplify_gen_binary (
6934 AND, compute_mode,
6935 gen_lowpart (compute_mode, SET_SRC (x)),
6936 mask),
6937 pos);
6939 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6940 simplify_gen_binary (IOR, compute_mode,
6941 cleared, masked));
6944 return x;
6947 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6948 it is an RTX that represents a variable starting position; otherwise,
6949 POS is the (constant) starting bit position (counted from the LSB).
6951 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6952 signed reference.
6954 IN_DEST is nonzero if this is a reference in the destination of a
6955 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6956 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6957 be used.
6959 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6960 ZERO_EXTRACT should be built even for bits starting at bit 0.
6962 MODE is the desired mode of the result (if IN_DEST == 0).
6964 The result is an RTX for the extraction or NULL_RTX if the target
6965 can't handle it. */
6967 static rtx
6968 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6969 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6970 int in_dest, int in_compare)
6972 /* This mode describes the size of the storage area
6973 to fetch the overall value from. Within that, we
6974 ignore the POS lowest bits, etc. */
6975 enum machine_mode is_mode = GET_MODE (inner);
6976 enum machine_mode inner_mode;
6977 enum machine_mode wanted_inner_mode;
6978 enum machine_mode wanted_inner_reg_mode = word_mode;
6979 enum machine_mode pos_mode = word_mode;
6980 enum machine_mode extraction_mode = word_mode;
6981 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6982 rtx new_rtx = 0;
6983 rtx orig_pos_rtx = pos_rtx;
6984 HOST_WIDE_INT orig_pos;
6986 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6988 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6989 consider just the QI as the memory to extract from.
6990 The subreg adds or removes high bits; its mode is
6991 irrelevant to the meaning of this extraction,
6992 since POS and LEN count from the lsb. */
6993 if (MEM_P (SUBREG_REG (inner)))
6994 is_mode = GET_MODE (SUBREG_REG (inner));
6995 inner = SUBREG_REG (inner);
6997 else if (GET_CODE (inner) == ASHIFT
6998 && CONST_INT_P (XEXP (inner, 1))
6999 && pos_rtx == 0 && pos == 0
7000 && len > UINTVAL (XEXP (inner, 1)))
7002 /* We're extracting the least significant bits of an rtx
7003 (ashift X (const_int C)), where LEN > C. Extract the
7004 least significant (LEN - C) bits of X, giving an rtx
7005 whose mode is MODE, then shift it left C times. */
7006 new_rtx = make_extraction (mode, XEXP (inner, 0),
7007 0, 0, len - INTVAL (XEXP (inner, 1)),
7008 unsignedp, in_dest, in_compare);
7009 if (new_rtx != 0)
7010 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7013 inner_mode = GET_MODE (inner);
7015 if (pos_rtx && CONST_INT_P (pos_rtx))
7016 pos = INTVAL (pos_rtx), pos_rtx = 0;
7018 /* See if this can be done without an extraction. We never can if the
7019 width of the field is not the same as that of some integer mode. For
7020 registers, we can only avoid the extraction if the position is at the
7021 low-order bit and this is either not in the destination or we have the
7022 appropriate STRICT_LOW_PART operation available.
7024 For MEM, we can avoid an extract if the field starts on an appropriate
7025 boundary and we can change the mode of the memory reference. */
7027 if (tmode != BLKmode
7028 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7029 && !MEM_P (inner)
7030 && (inner_mode == tmode
7031 || !REG_P (inner)
7032 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
7033 GET_MODE_BITSIZE (inner_mode))
7034 || reg_truncated_to_mode (tmode, inner))
7035 && (! in_dest
7036 || (REG_P (inner)
7037 && have_insn_for (STRICT_LOW_PART, tmode))))
7038 || (MEM_P (inner) && pos_rtx == 0
7039 && (pos
7040 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7041 : BITS_PER_UNIT)) == 0
7042 /* We can't do this if we are widening INNER_MODE (it
7043 may not be aligned, for one thing). */
7044 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
7045 && (inner_mode == tmode
7046 || (! mode_dependent_address_p (XEXP (inner, 0))
7047 && ! MEM_VOLATILE_P (inner))))))
7049 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7050 field. If the original and current mode are the same, we need not
7051 adjust the offset. Otherwise, we do if bytes big endian.
7053 If INNER is not a MEM, get a piece consisting of just the field
7054 of interest (in this case POS % BITS_PER_WORD must be 0). */
7056 if (MEM_P (inner))
7058 HOST_WIDE_INT offset;
7060 /* POS counts from lsb, but make OFFSET count in memory order. */
7061 if (BYTES_BIG_ENDIAN)
7062 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
7063 else
7064 offset = pos / BITS_PER_UNIT;
7066 new_rtx = adjust_address_nv (inner, tmode, offset);
7068 else if (REG_P (inner))
7070 if (tmode != inner_mode)
7072 /* We can't call gen_lowpart in a DEST since we
7073 always want a SUBREG (see below) and it would sometimes
7074 return a new hard register. */
7075 if (pos || in_dest)
7077 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7079 if (WORDS_BIG_ENDIAN
7080 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7081 final_word = ((GET_MODE_SIZE (inner_mode)
7082 - GET_MODE_SIZE (tmode))
7083 / UNITS_PER_WORD) - final_word;
7085 final_word *= UNITS_PER_WORD;
7086 if (BYTES_BIG_ENDIAN &&
7087 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7088 final_word += (GET_MODE_SIZE (inner_mode)
7089 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7091 /* Avoid creating invalid subregs, for example when
7092 simplifying (x>>32)&255. */
7093 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7094 return NULL_RTX;
7096 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7098 else
7099 new_rtx = gen_lowpart (tmode, inner);
7101 else
7102 new_rtx = inner;
7104 else
7105 new_rtx = force_to_mode (inner, tmode,
7106 len >= HOST_BITS_PER_WIDE_INT
7107 ? ~(unsigned HOST_WIDE_INT) 0
7108 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7111 /* If this extraction is going into the destination of a SET,
7112 make a STRICT_LOW_PART unless we made a MEM. */
7114 if (in_dest)
7115 return (MEM_P (new_rtx) ? new_rtx
7116 : (GET_CODE (new_rtx) != SUBREG
7117 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7118 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7120 if (mode == tmode)
7121 return new_rtx;
7123 if (CONST_INT_P (new_rtx)
7124 || GET_CODE (new_rtx) == CONST_DOUBLE)
7125 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7126 mode, new_rtx, tmode);
7128 /* If we know that no extraneous bits are set, and that the high
7129 bit is not set, convert the extraction to the cheaper of
7130 sign and zero extension, that are equivalent in these cases. */
7131 if (flag_expensive_optimizations
7132 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
7133 && ((nonzero_bits (new_rtx, tmode)
7134 & ~(((unsigned HOST_WIDE_INT)
7135 GET_MODE_MASK (tmode))
7136 >> 1))
7137 == 0)))
7139 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7140 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7142 /* Prefer ZERO_EXTENSION, since it gives more information to
7143 backends. */
7144 if (rtx_cost (temp, SET, optimize_this_for_speed_p)
7145 <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
7146 return temp;
7147 return temp1;
7150 /* Otherwise, sign- or zero-extend unless we already are in the
7151 proper mode. */
7153 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7154 mode, new_rtx));
7157 /* Unless this is a COMPARE or we have a funny memory reference,
7158 don't do anything with zero-extending field extracts starting at
7159 the low-order bit since they are simple AND operations. */
7160 if (pos_rtx == 0 && pos == 0 && ! in_dest
7161 && ! in_compare && unsignedp)
7162 return 0;
7164 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7165 if the position is not a constant and the length is not 1. In all
7166 other cases, we would only be going outside our object in cases when
7167 an original shift would have been undefined. */
7168 if (MEM_P (inner)
7169 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
7170 || (pos_rtx != 0 && len != 1)))
7171 return 0;
7173 /* Get the mode to use should INNER not be a MEM, the mode for the position,
7174 and the mode for the result. */
7175 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7177 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7178 pos_mode = mode_for_extraction (EP_insv, 2);
7179 extraction_mode = mode_for_extraction (EP_insv, 3);
7182 if (! in_dest && unsignedp
7183 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7185 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7186 pos_mode = mode_for_extraction (EP_extzv, 3);
7187 extraction_mode = mode_for_extraction (EP_extzv, 0);
7190 if (! in_dest && ! unsignedp
7191 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7193 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7194 pos_mode = mode_for_extraction (EP_extv, 3);
7195 extraction_mode = mode_for_extraction (EP_extv, 0);
7198 /* Never narrow an object, since that might not be safe. */
7200 if (mode != VOIDmode
7201 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7202 extraction_mode = mode;
7204 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7205 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7206 pos_mode = GET_MODE (pos_rtx);
7208 /* If this is not from memory, the desired mode is the preferred mode
7209 for an extraction pattern's first input operand, or word_mode if there
7210 is none. */
7211 if (!MEM_P (inner))
7212 wanted_inner_mode = wanted_inner_reg_mode;
7213 else
7215 /* Be careful not to go beyond the extracted object and maintain the
7216 natural alignment of the memory. */
7217 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7218 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7219 > GET_MODE_BITSIZE (wanted_inner_mode))
7221 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7222 gcc_assert (wanted_inner_mode != VOIDmode);
7225 /* If we have to change the mode of memory and cannot, the desired mode
7226 is EXTRACTION_MODE. */
7227 if (inner_mode != wanted_inner_mode
7228 && (mode_dependent_address_p (XEXP (inner, 0))
7229 || MEM_VOLATILE_P (inner)
7230 || pos_rtx))
7231 wanted_inner_mode = extraction_mode;
7234 orig_pos = pos;
7236 if (BITS_BIG_ENDIAN)
7238 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7239 BITS_BIG_ENDIAN style. If position is constant, compute new
7240 position. Otherwise, build subtraction.
7241 Note that POS is relative to the mode of the original argument.
7242 If it's a MEM we need to recompute POS relative to that.
7243 However, if we're extracting from (or inserting into) a register,
7244 we want to recompute POS relative to wanted_inner_mode. */
7245 int width = (MEM_P (inner)
7246 ? GET_MODE_BITSIZE (is_mode)
7247 : GET_MODE_BITSIZE (wanted_inner_mode));
7249 if (pos_rtx == 0)
7250 pos = width - len - pos;
7251 else
7252 pos_rtx
7253 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7254 /* POS may be less than 0 now, but we check for that below.
7255 Note that it can only be less than 0 if !MEM_P (inner). */
7258 /* If INNER has a wider mode, and this is a constant extraction, try to
7259 make it smaller and adjust the byte to point to the byte containing
7260 the value. */
7261 if (wanted_inner_mode != VOIDmode
7262 && inner_mode != wanted_inner_mode
7263 && ! pos_rtx
7264 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7265 && MEM_P (inner)
7266 && ! mode_dependent_address_p (XEXP (inner, 0))
7267 && ! MEM_VOLATILE_P (inner))
7269 int offset = 0;
7271 /* The computations below will be correct if the machine is big
7272 endian in both bits and bytes or little endian in bits and bytes.
7273 If it is mixed, we must adjust. */
7275 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7276 adjust OFFSET to compensate. */
7277 if (BYTES_BIG_ENDIAN
7278 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7279 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7281 /* We can now move to the desired byte. */
7282 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7283 * GET_MODE_SIZE (wanted_inner_mode);
7284 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7286 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7287 && is_mode != wanted_inner_mode)
7288 offset = (GET_MODE_SIZE (is_mode)
7289 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7291 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7294 /* If INNER is not memory, get it into the proper mode. If we are changing
7295 its mode, POS must be a constant and smaller than the size of the new
7296 mode. */
7297 else if (!MEM_P (inner))
7299 /* On the LHS, don't create paradoxical subregs implicitely truncating
7300 the register unless TRULY_NOOP_TRUNCATION. */
7301 if (in_dest
7302 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
7303 GET_MODE_BITSIZE (wanted_inner_mode)))
7304 return NULL_RTX;
7306 if (GET_MODE (inner) != wanted_inner_mode
7307 && (pos_rtx != 0
7308 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7309 return NULL_RTX;
7311 if (orig_pos < 0)
7312 return NULL_RTX;
7314 inner = force_to_mode (inner, wanted_inner_mode,
7315 pos_rtx
7316 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7317 ? ~(unsigned HOST_WIDE_INT) 0
7318 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7319 << orig_pos),
7323 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7324 have to zero extend. Otherwise, we can just use a SUBREG. */
7325 if (pos_rtx != 0
7326 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7328 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7330 /* If we know that no extraneous bits are set, and that the high
7331 bit is not set, convert extraction to cheaper one - either
7332 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7333 cases. */
7334 if (flag_expensive_optimizations
7335 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
7336 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7337 & ~(((unsigned HOST_WIDE_INT)
7338 GET_MODE_MASK (GET_MODE (pos_rtx)))
7339 >> 1))
7340 == 0)))
7342 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7344 /* Prefer ZERO_EXTENSION, since it gives more information to
7345 backends. */
7346 if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
7347 < rtx_cost (temp, SET, optimize_this_for_speed_p))
7348 temp = temp1;
7350 pos_rtx = temp;
7352 else if (pos_rtx != 0
7353 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7354 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7356 /* Make POS_RTX unless we already have it and it is correct. If we don't
7357 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7358 be a CONST_INT. */
7359 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7360 pos_rtx = orig_pos_rtx;
7362 else if (pos_rtx == 0)
7363 pos_rtx = GEN_INT (pos);
7365 /* Make the required operation. See if we can use existing rtx. */
7366 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7367 extraction_mode, inner, GEN_INT (len), pos_rtx);
7368 if (! in_dest)
7369 new_rtx = gen_lowpart (mode, new_rtx);
7371 return new_rtx;
7374 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7375 with any other operations in X. Return X without that shift if so. */
7377 static rtx
7378 extract_left_shift (rtx x, int count)
7380 enum rtx_code code = GET_CODE (x);
7381 enum machine_mode mode = GET_MODE (x);
7382 rtx tem;
7384 switch (code)
7386 case ASHIFT:
7387 /* This is the shift itself. If it is wide enough, we will return
7388 either the value being shifted if the shift count is equal to
7389 COUNT or a shift for the difference. */
7390 if (CONST_INT_P (XEXP (x, 1))
7391 && INTVAL (XEXP (x, 1)) >= count)
7392 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7393 INTVAL (XEXP (x, 1)) - count);
7394 break;
7396 case NEG: case NOT:
7397 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7398 return simplify_gen_unary (code, mode, tem, mode);
7400 break;
7402 case PLUS: case IOR: case XOR: case AND:
7403 /* If we can safely shift this constant and we find the inner shift,
7404 make a new operation. */
7405 if (CONST_INT_P (XEXP (x, 1))
7406 && (UINTVAL (XEXP (x, 1))
7407 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7408 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7409 return simplify_gen_binary (code, mode, tem,
7410 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7412 break;
7414 default:
7415 break;
7418 return 0;
7421 /* Look at the expression rooted at X. Look for expressions
7422 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7423 Form these expressions.
7425 Return the new rtx, usually just X.
7427 Also, for machines like the VAX that don't have logical shift insns,
7428 try to convert logical to arithmetic shift operations in cases where
7429 they are equivalent. This undoes the canonicalizations to logical
7430 shifts done elsewhere.
7432 We try, as much as possible, to re-use rtl expressions to save memory.
7434 IN_CODE says what kind of expression we are processing. Normally, it is
7435 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7436 being kludges), it is MEM. When processing the arguments of a comparison
7437 or a COMPARE against zero, it is COMPARE. */
7439 static rtx
7440 make_compound_operation (rtx x, enum rtx_code in_code)
7442 enum rtx_code code = GET_CODE (x);
7443 enum machine_mode mode = GET_MODE (x);
7444 int mode_width = GET_MODE_BITSIZE (mode);
7445 rtx rhs, lhs;
7446 enum rtx_code next_code;
7447 int i, j;
7448 rtx new_rtx = 0;
7449 rtx tem;
7450 const char *fmt;
7452 /* Select the code to be used in recursive calls. Once we are inside an
7453 address, we stay there. If we have a comparison, set to COMPARE,
7454 but once inside, go back to our default of SET. */
7456 next_code = (code == MEM ? MEM
7457 : ((code == PLUS || code == MINUS)
7458 && SCALAR_INT_MODE_P (mode)) ? MEM
7459 : ((code == COMPARE || COMPARISON_P (x))
7460 && XEXP (x, 1) == const0_rtx) ? COMPARE
7461 : in_code == COMPARE ? SET : in_code);
7463 /* Process depending on the code of this operation. If NEW is set
7464 nonzero, it will be returned. */
7466 switch (code)
7468 case ASHIFT:
7469 /* Convert shifts by constants into multiplications if inside
7470 an address. */
7471 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7472 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7473 && INTVAL (XEXP (x, 1)) >= 0)
7475 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7476 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7478 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7479 if (GET_CODE (new_rtx) == NEG)
7481 new_rtx = XEXP (new_rtx, 0);
7482 multval = -multval;
7484 multval = trunc_int_for_mode (multval, mode);
7485 new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7487 break;
7489 case PLUS:
7490 lhs = XEXP (x, 0);
7491 rhs = XEXP (x, 1);
7492 lhs = make_compound_operation (lhs, next_code);
7493 rhs = make_compound_operation (rhs, next_code);
7494 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7495 && SCALAR_INT_MODE_P (mode))
7497 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7498 XEXP (lhs, 1));
7499 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7501 else if (GET_CODE (lhs) == MULT
7502 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7504 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7505 simplify_gen_unary (NEG, mode,
7506 XEXP (lhs, 1),
7507 mode));
7508 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7510 else
7512 SUBST (XEXP (x, 0), lhs);
7513 SUBST (XEXP (x, 1), rhs);
7514 goto maybe_swap;
7516 x = gen_lowpart (mode, new_rtx);
7517 goto maybe_swap;
7519 case MINUS:
7520 lhs = XEXP (x, 0);
7521 rhs = XEXP (x, 1);
7522 lhs = make_compound_operation (lhs, next_code);
7523 rhs = make_compound_operation (rhs, next_code);
7524 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7525 && SCALAR_INT_MODE_P (mode))
7527 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7528 XEXP (rhs, 1));
7529 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7531 else if (GET_CODE (rhs) == MULT
7532 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7534 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7535 simplify_gen_unary (NEG, mode,
7536 XEXP (rhs, 1),
7537 mode));
7538 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7540 else
7542 SUBST (XEXP (x, 0), lhs);
7543 SUBST (XEXP (x, 1), rhs);
7544 return x;
7546 return gen_lowpart (mode, new_rtx);
7548 case AND:
7549 /* If the second operand is not a constant, we can't do anything
7550 with it. */
7551 if (!CONST_INT_P (XEXP (x, 1)))
7552 break;
7554 /* If the constant is a power of two minus one and the first operand
7555 is a logical right shift, make an extraction. */
7556 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7557 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7559 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7560 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7561 0, in_code == COMPARE);
7564 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7565 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7566 && subreg_lowpart_p (XEXP (x, 0))
7567 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7568 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7570 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7571 next_code);
7572 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7573 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7574 0, in_code == COMPARE);
7576 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7577 else if ((GET_CODE (XEXP (x, 0)) == XOR
7578 || GET_CODE (XEXP (x, 0)) == IOR)
7579 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7580 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7581 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7583 /* Apply the distributive law, and then try to make extractions. */
7584 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7585 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7586 XEXP (x, 1)),
7587 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7588 XEXP (x, 1)));
7589 new_rtx = make_compound_operation (new_rtx, in_code);
7592 /* If we are have (and (rotate X C) M) and C is larger than the number
7593 of bits in M, this is an extraction. */
7595 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7596 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7597 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7598 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7600 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7601 new_rtx = make_extraction (mode, new_rtx,
7602 (GET_MODE_BITSIZE (mode)
7603 - INTVAL (XEXP (XEXP (x, 0), 1))),
7604 NULL_RTX, i, 1, 0, in_code == COMPARE);
7607 /* On machines without logical shifts, if the operand of the AND is
7608 a logical shift and our mask turns off all the propagated sign
7609 bits, we can replace the logical shift with an arithmetic shift. */
7610 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7611 && !have_insn_for (LSHIFTRT, mode)
7612 && have_insn_for (ASHIFTRT, mode)
7613 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7614 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7615 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7616 && mode_width <= HOST_BITS_PER_WIDE_INT)
7618 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7620 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7621 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7622 SUBST (XEXP (x, 0),
7623 gen_rtx_ASHIFTRT (mode,
7624 make_compound_operation
7625 (XEXP (XEXP (x, 0), 0), next_code),
7626 XEXP (XEXP (x, 0), 1)));
7629 /* If the constant is one less than a power of two, this might be
7630 representable by an extraction even if no shift is present.
7631 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7632 we are in a COMPARE. */
7633 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7634 new_rtx = make_extraction (mode,
7635 make_compound_operation (XEXP (x, 0),
7636 next_code),
7637 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7639 /* If we are in a comparison and this is an AND with a power of two,
7640 convert this into the appropriate bit extract. */
7641 else if (in_code == COMPARE
7642 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7643 new_rtx = make_extraction (mode,
7644 make_compound_operation (XEXP (x, 0),
7645 next_code),
7646 i, NULL_RTX, 1, 1, 0, 1);
7648 break;
7650 case LSHIFTRT:
7651 /* If the sign bit is known to be zero, replace this with an
7652 arithmetic shift. */
7653 if (have_insn_for (ASHIFTRT, mode)
7654 && ! have_insn_for (LSHIFTRT, mode)
7655 && mode_width <= HOST_BITS_PER_WIDE_INT
7656 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7658 new_rtx = gen_rtx_ASHIFTRT (mode,
7659 make_compound_operation (XEXP (x, 0),
7660 next_code),
7661 XEXP (x, 1));
7662 break;
7665 /* ... fall through ... */
7667 case ASHIFTRT:
7668 lhs = XEXP (x, 0);
7669 rhs = XEXP (x, 1);
7671 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7672 this is a SIGN_EXTRACT. */
7673 if (CONST_INT_P (rhs)
7674 && GET_CODE (lhs) == ASHIFT
7675 && CONST_INT_P (XEXP (lhs, 1))
7676 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7677 && INTVAL (rhs) < mode_width)
7679 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7680 new_rtx = make_extraction (mode, new_rtx,
7681 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7682 NULL_RTX, mode_width - INTVAL (rhs),
7683 code == LSHIFTRT, 0, in_code == COMPARE);
7684 break;
7687 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7688 If so, try to merge the shifts into a SIGN_EXTEND. We could
7689 also do this for some cases of SIGN_EXTRACT, but it doesn't
7690 seem worth the effort; the case checked for occurs on Alpha. */
7692 if (!OBJECT_P (lhs)
7693 && ! (GET_CODE (lhs) == SUBREG
7694 && (OBJECT_P (SUBREG_REG (lhs))))
7695 && CONST_INT_P (rhs)
7696 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7697 && INTVAL (rhs) < mode_width
7698 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7699 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7700 0, NULL_RTX, mode_width - INTVAL (rhs),
7701 code == LSHIFTRT, 0, in_code == COMPARE);
7703 break;
7705 case SUBREG:
7706 /* Call ourselves recursively on the inner expression. If we are
7707 narrowing the object and it has a different RTL code from
7708 what it originally did, do this SUBREG as a force_to_mode. */
7710 rtx inner = SUBREG_REG (x), simplified;
7712 tem = make_compound_operation (inner, in_code);
7714 simplified
7715 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7716 if (simplified)
7717 tem = simplified;
7719 if (GET_CODE (tem) != GET_CODE (inner)
7720 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7721 && subreg_lowpart_p (x))
7723 rtx newer
7724 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7726 /* If we have something other than a SUBREG, we might have
7727 done an expansion, so rerun ourselves. */
7728 if (GET_CODE (newer) != SUBREG)
7729 newer = make_compound_operation (newer, in_code);
7731 /* force_to_mode can expand compounds. If it just re-expanded the
7732 compound, use gen_lowpart to convert to the desired mode. */
7733 if (rtx_equal_p (newer, x)
7734 /* Likewise if it re-expanded the compound only partially.
7735 This happens for SUBREG of ZERO_EXTRACT if they extract
7736 the same number of bits. */
7737 || (GET_CODE (newer) == SUBREG
7738 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7739 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7740 && GET_CODE (inner) == AND
7741 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7742 return gen_lowpart (GET_MODE (x), tem);
7744 return newer;
7747 if (simplified)
7748 return tem;
7750 break;
7752 default:
7753 break;
7756 if (new_rtx)
7758 x = gen_lowpart (mode, new_rtx);
7759 code = GET_CODE (x);
7762 /* Now recursively process each operand of this operation. */
7763 fmt = GET_RTX_FORMAT (code);
7764 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7765 if (fmt[i] == 'e')
7767 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7768 SUBST (XEXP (x, i), new_rtx);
7770 else if (fmt[i] == 'E')
7771 for (j = 0; j < XVECLEN (x, i); j++)
7773 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7774 SUBST (XVECEXP (x, i, j), new_rtx);
7777 maybe_swap:
7778 /* If this is a commutative operation, the changes to the operands
7779 may have made it noncanonical. */
7780 if (COMMUTATIVE_ARITH_P (x)
7781 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7783 tem = XEXP (x, 0);
7784 SUBST (XEXP (x, 0), XEXP (x, 1));
7785 SUBST (XEXP (x, 1), tem);
7788 return x;
7791 /* Given M see if it is a value that would select a field of bits
7792 within an item, but not the entire word. Return -1 if not.
7793 Otherwise, return the starting position of the field, where 0 is the
7794 low-order bit.
7796 *PLEN is set to the length of the field. */
7798 static int
7799 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7801 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7802 int pos = m ? ctz_hwi (m) : -1;
7803 int len = 0;
7805 if (pos >= 0)
7806 /* Now shift off the low-order zero bits and see if we have a
7807 power of two minus 1. */
7808 len = exact_log2 ((m >> pos) + 1);
7810 if (len <= 0)
7811 pos = -1;
7813 *plen = len;
7814 return pos;
7817 /* If X refers to a register that equals REG in value, replace these
7818 references with REG. */
7819 static rtx
7820 canon_reg_for_combine (rtx x, rtx reg)
7822 rtx op0, op1, op2;
7823 const char *fmt;
7824 int i;
7825 bool copied;
7827 enum rtx_code code = GET_CODE (x);
7828 switch (GET_RTX_CLASS (code))
7830 case RTX_UNARY:
7831 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7832 if (op0 != XEXP (x, 0))
7833 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7834 GET_MODE (reg));
7835 break;
7837 case RTX_BIN_ARITH:
7838 case RTX_COMM_ARITH:
7839 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7840 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7841 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7842 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7843 break;
7845 case RTX_COMPARE:
7846 case RTX_COMM_COMPARE:
7847 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7848 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7849 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7850 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7851 GET_MODE (op0), op0, op1);
7852 break;
7854 case RTX_TERNARY:
7855 case RTX_BITFIELD_OPS:
7856 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7857 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7858 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7859 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7860 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7861 GET_MODE (op0), op0, op1, op2);
7863 case RTX_OBJ:
7864 if (REG_P (x))
7866 if (rtx_equal_p (get_last_value (reg), x)
7867 || rtx_equal_p (reg, get_last_value (x)))
7868 return reg;
7869 else
7870 break;
7873 /* fall through */
7875 default:
7876 fmt = GET_RTX_FORMAT (code);
7877 copied = false;
7878 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7879 if (fmt[i] == 'e')
7881 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7882 if (op != XEXP (x, i))
7884 if (!copied)
7886 copied = true;
7887 x = copy_rtx (x);
7889 XEXP (x, i) = op;
7892 else if (fmt[i] == 'E')
7894 int j;
7895 for (j = 0; j < XVECLEN (x, i); j++)
7897 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7898 if (op != XVECEXP (x, i, j))
7900 if (!copied)
7902 copied = true;
7903 x = copy_rtx (x);
7905 XVECEXP (x, i, j) = op;
7910 break;
7913 return x;
7916 /* Return X converted to MODE. If the value is already truncated to
7917 MODE we can just return a subreg even though in the general case we
7918 would need an explicit truncation. */
7920 static rtx
7921 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7923 if (!CONST_INT_P (x)
7924 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7925 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7926 GET_MODE_BITSIZE (GET_MODE (x)))
7927 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7929 /* Bit-cast X into an integer mode. */
7930 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7931 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7932 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7933 x, GET_MODE (x));
7936 return gen_lowpart (mode, x);
7939 /* See if X can be simplified knowing that we will only refer to it in
7940 MODE and will only refer to those bits that are nonzero in MASK.
7941 If other bits are being computed or if masking operations are done
7942 that select a superset of the bits in MASK, they can sometimes be
7943 ignored.
7945 Return a possibly simplified expression, but always convert X to
7946 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7948 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7949 are all off in X. This is used when X will be complemented, by either
7950 NOT, NEG, or XOR. */
7952 static rtx
7953 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7954 int just_select)
7956 enum rtx_code code = GET_CODE (x);
7957 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7958 enum machine_mode op_mode;
7959 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7960 rtx op0, op1, temp;
7962 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7963 code below will do the wrong thing since the mode of such an
7964 expression is VOIDmode.
7966 Also do nothing if X is a CLOBBER; this can happen if X was
7967 the return value from a call to gen_lowpart. */
7968 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7969 return x;
7971 /* We want to perform the operation is its present mode unless we know
7972 that the operation is valid in MODE, in which case we do the operation
7973 in MODE. */
7974 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7975 && have_insn_for (code, mode))
7976 ? mode : GET_MODE (x));
7978 /* It is not valid to do a right-shift in a narrower mode
7979 than the one it came in with. */
7980 if ((code == LSHIFTRT || code == ASHIFTRT)
7981 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7982 op_mode = GET_MODE (x);
7984 /* Truncate MASK to fit OP_MODE. */
7985 if (op_mode)
7986 mask &= GET_MODE_MASK (op_mode);
7988 /* When we have an arithmetic operation, or a shift whose count we
7989 do not know, we need to assume that all bits up to the highest-order
7990 bit in MASK will be needed. This is how we form such a mask. */
7991 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7992 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7993 else
7994 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7995 - 1);
7997 /* Determine what bits of X are guaranteed to be (non)zero. */
7998 nonzero = nonzero_bits (x, mode);
8000 /* If none of the bits in X are needed, return a zero. */
8001 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8002 x = const0_rtx;
8004 /* If X is a CONST_INT, return a new one. Do this here since the
8005 test below will fail. */
8006 if (CONST_INT_P (x))
8008 if (SCALAR_INT_MODE_P (mode))
8009 return gen_int_mode (INTVAL (x) & mask, mode);
8010 else
8012 x = GEN_INT (INTVAL (x) & mask);
8013 return gen_lowpart_common (mode, x);
8017 /* If X is narrower than MODE and we want all the bits in X's mode, just
8018 get X in the proper mode. */
8019 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8020 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8021 return gen_lowpart (mode, x);
8023 /* We can ignore the effect of a SUBREG if it narrows the mode or
8024 if the constant masks to zero all the bits the mode doesn't have. */
8025 if (GET_CODE (x) == SUBREG
8026 && subreg_lowpart_p (x)
8027 && ((GET_MODE_SIZE (GET_MODE (x))
8028 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8029 || (0 == (mask
8030 & GET_MODE_MASK (GET_MODE (x))
8031 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8032 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8034 /* The arithmetic simplifications here only work for scalar integer modes. */
8035 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8036 return gen_lowpart_or_truncate (mode, x);
8038 switch (code)
8040 case CLOBBER:
8041 /* If X is a (clobber (const_int)), return it since we know we are
8042 generating something that won't match. */
8043 return x;
8045 case SIGN_EXTEND:
8046 case ZERO_EXTEND:
8047 case ZERO_EXTRACT:
8048 case SIGN_EXTRACT:
8049 x = expand_compound_operation (x);
8050 if (GET_CODE (x) != code)
8051 return force_to_mode (x, mode, mask, next_select);
8052 break;
8054 case TRUNCATE:
8055 /* Similarly for a truncate. */
8056 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8058 case AND:
8059 /* If this is an AND with a constant, convert it into an AND
8060 whose constant is the AND of that constant with MASK. If it
8061 remains an AND of MASK, delete it since it is redundant. */
8063 if (CONST_INT_P (XEXP (x, 1)))
8065 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8066 mask & INTVAL (XEXP (x, 1)));
8068 /* If X is still an AND, see if it is an AND with a mask that
8069 is just some low-order bits. If so, and it is MASK, we don't
8070 need it. */
8072 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8073 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8074 == mask))
8075 x = XEXP (x, 0);
8077 /* If it remains an AND, try making another AND with the bits
8078 in the mode mask that aren't in MASK turned on. If the
8079 constant in the AND is wide enough, this might make a
8080 cheaper constant. */
8082 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8083 && GET_MODE_MASK (GET_MODE (x)) != mask
8084 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
8086 unsigned HOST_WIDE_INT cval
8087 = UINTVAL (XEXP (x, 1))
8088 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8089 int width = GET_MODE_BITSIZE (GET_MODE (x));
8090 rtx y;
8092 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8093 number, sign extend it. */
8094 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8095 && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8096 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8098 y = simplify_gen_binary (AND, GET_MODE (x),
8099 XEXP (x, 0), GEN_INT (cval));
8100 if (rtx_cost (y, SET, optimize_this_for_speed_p)
8101 < rtx_cost (x, SET, optimize_this_for_speed_p))
8102 x = y;
8105 break;
8108 goto binop;
8110 case PLUS:
8111 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8112 low-order bits (as in an alignment operation) and FOO is already
8113 aligned to that boundary, mask C1 to that boundary as well.
8114 This may eliminate that PLUS and, later, the AND. */
8117 unsigned int width = GET_MODE_BITSIZE (mode);
8118 unsigned HOST_WIDE_INT smask = mask;
8120 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8121 number, sign extend it. */
8123 if (width < HOST_BITS_PER_WIDE_INT
8124 && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8125 smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8127 if (CONST_INT_P (XEXP (x, 1))
8128 && exact_log2 (- smask) >= 0
8129 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8130 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8131 return force_to_mode (plus_constant (XEXP (x, 0),
8132 (INTVAL (XEXP (x, 1)) & smask)),
8133 mode, smask, next_select);
8136 /* ... fall through ... */
8138 case MULT:
8139 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8140 most significant bit in MASK since carries from those bits will
8141 affect the bits we are interested in. */
8142 mask = fuller_mask;
8143 goto binop;
8145 case MINUS:
8146 /* If X is (minus C Y) where C's least set bit is larger than any bit
8147 in the mask, then we may replace with (neg Y). */
8148 if (CONST_INT_P (XEXP (x, 0))
8149 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8150 & -INTVAL (XEXP (x, 0))))
8151 > mask))
8153 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8154 GET_MODE (x));
8155 return force_to_mode (x, mode, mask, next_select);
8158 /* Similarly, if C contains every bit in the fuller_mask, then we may
8159 replace with (not Y). */
8160 if (CONST_INT_P (XEXP (x, 0))
8161 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8163 x = simplify_gen_unary (NOT, GET_MODE (x),
8164 XEXP (x, 1), GET_MODE (x));
8165 return force_to_mode (x, mode, mask, next_select);
8168 mask = fuller_mask;
8169 goto binop;
8171 case IOR:
8172 case XOR:
8173 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8174 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8175 operation which may be a bitfield extraction. Ensure that the
8176 constant we form is not wider than the mode of X. */
8178 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8179 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8180 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8181 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8182 && CONST_INT_P (XEXP (x, 1))
8183 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8184 + floor_log2 (INTVAL (XEXP (x, 1))))
8185 < GET_MODE_BITSIZE (GET_MODE (x)))
8186 && (UINTVAL (XEXP (x, 1))
8187 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8189 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8190 << INTVAL (XEXP (XEXP (x, 0), 1)));
8191 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8192 XEXP (XEXP (x, 0), 0), temp);
8193 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8194 XEXP (XEXP (x, 0), 1));
8195 return force_to_mode (x, mode, mask, next_select);
8198 binop:
8199 /* For most binary operations, just propagate into the operation and
8200 change the mode if we have an operation of that mode. */
8202 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8203 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8205 /* If we ended up truncating both operands, truncate the result of the
8206 operation instead. */
8207 if (GET_CODE (op0) == TRUNCATE
8208 && GET_CODE (op1) == TRUNCATE)
8210 op0 = XEXP (op0, 0);
8211 op1 = XEXP (op1, 0);
8214 op0 = gen_lowpart_or_truncate (op_mode, op0);
8215 op1 = gen_lowpart_or_truncate (op_mode, op1);
8217 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8218 x = simplify_gen_binary (code, op_mode, op0, op1);
8219 break;
8221 case ASHIFT:
8222 /* For left shifts, do the same, but just for the first operand.
8223 However, we cannot do anything with shifts where we cannot
8224 guarantee that the counts are smaller than the size of the mode
8225 because such a count will have a different meaning in a
8226 wider mode. */
8228 if (! (CONST_INT_P (XEXP (x, 1))
8229 && INTVAL (XEXP (x, 1)) >= 0
8230 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
8231 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8232 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8233 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
8234 break;
8236 /* If the shift count is a constant and we can do arithmetic in
8237 the mode of the shift, refine which bits we need. Otherwise, use the
8238 conservative form of the mask. */
8239 if (CONST_INT_P (XEXP (x, 1))
8240 && INTVAL (XEXP (x, 1)) >= 0
8241 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
8242 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8243 mask >>= INTVAL (XEXP (x, 1));
8244 else
8245 mask = fuller_mask;
8247 op0 = gen_lowpart_or_truncate (op_mode,
8248 force_to_mode (XEXP (x, 0), op_mode,
8249 mask, next_select));
8251 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8252 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8253 break;
8255 case LSHIFTRT:
8256 /* Here we can only do something if the shift count is a constant,
8257 this shift constant is valid for the host, and we can do arithmetic
8258 in OP_MODE. */
8260 if (CONST_INT_P (XEXP (x, 1))
8261 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8262 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8264 rtx inner = XEXP (x, 0);
8265 unsigned HOST_WIDE_INT inner_mask;
8267 /* Select the mask of the bits we need for the shift operand. */
8268 inner_mask = mask << INTVAL (XEXP (x, 1));
8270 /* We can only change the mode of the shift if we can do arithmetic
8271 in the mode of the shift and INNER_MASK is no wider than the
8272 width of X's mode. */
8273 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8274 op_mode = GET_MODE (x);
8276 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8278 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8279 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8282 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8283 shift and AND produces only copies of the sign bit (C2 is one less
8284 than a power of two), we can do this with just a shift. */
8286 if (GET_CODE (x) == LSHIFTRT
8287 && CONST_INT_P (XEXP (x, 1))
8288 /* The shift puts one of the sign bit copies in the least significant
8289 bit. */
8290 && ((INTVAL (XEXP (x, 1))
8291 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8292 >= GET_MODE_BITSIZE (GET_MODE (x)))
8293 && exact_log2 (mask + 1) >= 0
8294 /* Number of bits left after the shift must be more than the mask
8295 needs. */
8296 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8297 <= GET_MODE_BITSIZE (GET_MODE (x)))
8298 /* Must be more sign bit copies than the mask needs. */
8299 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8300 >= exact_log2 (mask + 1)))
8301 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8302 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
8303 - exact_log2 (mask + 1)));
8305 goto shiftrt;
8307 case ASHIFTRT:
8308 /* If we are just looking for the sign bit, we don't need this shift at
8309 all, even if it has a variable count. */
8310 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8311 && (mask == ((unsigned HOST_WIDE_INT) 1
8312 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8313 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8315 /* If this is a shift by a constant, get a mask that contains those bits
8316 that are not copies of the sign bit. We then have two cases: If
8317 MASK only includes those bits, this can be a logical shift, which may
8318 allow simplifications. If MASK is a single-bit field not within
8319 those bits, we are requesting a copy of the sign bit and hence can
8320 shift the sign bit to the appropriate location. */
8322 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8323 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8325 int i;
8327 /* If the considered data is wider than HOST_WIDE_INT, we can't
8328 represent a mask for all its bits in a single scalar.
8329 But we only care about the lower bits, so calculate these. */
8331 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8333 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8335 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8336 is the number of bits a full-width mask would have set.
8337 We need only shift if these are fewer than nonzero can
8338 hold. If not, we must keep all bits set in nonzero. */
8340 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8341 < HOST_BITS_PER_WIDE_INT)
8342 nonzero >>= INTVAL (XEXP (x, 1))
8343 + HOST_BITS_PER_WIDE_INT
8344 - GET_MODE_BITSIZE (GET_MODE (x)) ;
8346 else
8348 nonzero = GET_MODE_MASK (GET_MODE (x));
8349 nonzero >>= INTVAL (XEXP (x, 1));
8352 if ((mask & ~nonzero) == 0)
8354 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8355 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8356 if (GET_CODE (x) != ASHIFTRT)
8357 return force_to_mode (x, mode, mask, next_select);
8360 else if ((i = exact_log2 (mask)) >= 0)
8362 x = simplify_shift_const
8363 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8364 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
8366 if (GET_CODE (x) != ASHIFTRT)
8367 return force_to_mode (x, mode, mask, next_select);
8371 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8372 even if the shift count isn't a constant. */
8373 if (mask == 1)
8374 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8375 XEXP (x, 0), XEXP (x, 1));
8377 shiftrt:
8379 /* If this is a zero- or sign-extension operation that just affects bits
8380 we don't care about, remove it. Be sure the call above returned
8381 something that is still a shift. */
8383 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8384 && CONST_INT_P (XEXP (x, 1))
8385 && INTVAL (XEXP (x, 1)) >= 0
8386 && (INTVAL (XEXP (x, 1))
8387 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
8388 && GET_CODE (XEXP (x, 0)) == ASHIFT
8389 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8390 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8391 next_select);
8393 break;
8395 case ROTATE:
8396 case ROTATERT:
8397 /* If the shift count is constant and we can do computations
8398 in the mode of X, compute where the bits we care about are.
8399 Otherwise, we can't do anything. Don't change the mode of
8400 the shift or propagate MODE into the shift, though. */
8401 if (CONST_INT_P (XEXP (x, 1))
8402 && INTVAL (XEXP (x, 1)) >= 0)
8404 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8405 GET_MODE (x), GEN_INT (mask),
8406 XEXP (x, 1));
8407 if (temp && CONST_INT_P (temp))
8408 SUBST (XEXP (x, 0),
8409 force_to_mode (XEXP (x, 0), GET_MODE (x),
8410 INTVAL (temp), next_select));
8412 break;
8414 case NEG:
8415 /* If we just want the low-order bit, the NEG isn't needed since it
8416 won't change the low-order bit. */
8417 if (mask == 1)
8418 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8420 /* We need any bits less significant than the most significant bit in
8421 MASK since carries from those bits will affect the bits we are
8422 interested in. */
8423 mask = fuller_mask;
8424 goto unop;
8426 case NOT:
8427 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8428 same as the XOR case above. Ensure that the constant we form is not
8429 wider than the mode of X. */
8431 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8432 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8433 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8434 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8435 < GET_MODE_BITSIZE (GET_MODE (x)))
8436 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8438 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8439 GET_MODE (x));
8440 temp = simplify_gen_binary (XOR, GET_MODE (x),
8441 XEXP (XEXP (x, 0), 0), temp);
8442 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8443 temp, XEXP (XEXP (x, 0), 1));
8445 return force_to_mode (x, mode, mask, next_select);
8448 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8449 use the full mask inside the NOT. */
8450 mask = fuller_mask;
8452 unop:
8453 op0 = gen_lowpart_or_truncate (op_mode,
8454 force_to_mode (XEXP (x, 0), mode, mask,
8455 next_select));
8456 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8457 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8458 break;
8460 case NE:
8461 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8462 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8463 which is equal to STORE_FLAG_VALUE. */
8464 if ((mask & ~STORE_FLAG_VALUE) == 0
8465 && XEXP (x, 1) == const0_rtx
8466 && GET_MODE (XEXP (x, 0)) == mode
8467 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8468 && (nonzero_bits (XEXP (x, 0), mode)
8469 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8470 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8472 break;
8474 case IF_THEN_ELSE:
8475 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8476 written in a narrower mode. We play it safe and do not do so. */
8478 SUBST (XEXP (x, 1),
8479 gen_lowpart_or_truncate (GET_MODE (x),
8480 force_to_mode (XEXP (x, 1), mode,
8481 mask, next_select)));
8482 SUBST (XEXP (x, 2),
8483 gen_lowpart_or_truncate (GET_MODE (x),
8484 force_to_mode (XEXP (x, 2), mode,
8485 mask, next_select)));
8486 break;
8488 default:
8489 break;
8492 /* Ensure we return a value of the proper mode. */
8493 return gen_lowpart_or_truncate (mode, x);
8496 /* Return nonzero if X is an expression that has one of two values depending on
8497 whether some other value is zero or nonzero. In that case, we return the
8498 value that is being tested, *PTRUE is set to the value if the rtx being
8499 returned has a nonzero value, and *PFALSE is set to the other alternative.
8501 If we return zero, we set *PTRUE and *PFALSE to X. */
8503 static rtx
8504 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8506 enum machine_mode mode = GET_MODE (x);
8507 enum rtx_code code = GET_CODE (x);
8508 rtx cond0, cond1, true0, true1, false0, false1;
8509 unsigned HOST_WIDE_INT nz;
8511 /* If we are comparing a value against zero, we are done. */
8512 if ((code == NE || code == EQ)
8513 && XEXP (x, 1) == const0_rtx)
8515 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8516 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8517 return XEXP (x, 0);
8520 /* If this is a unary operation whose operand has one of two values, apply
8521 our opcode to compute those values. */
8522 else if (UNARY_P (x)
8523 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8525 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8526 *pfalse = simplify_gen_unary (code, mode, false0,
8527 GET_MODE (XEXP (x, 0)));
8528 return cond0;
8531 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8532 make can't possibly match and would suppress other optimizations. */
8533 else if (code == COMPARE)
8536 /* If this is a binary operation, see if either side has only one of two
8537 values. If either one does or if both do and they are conditional on
8538 the same value, compute the new true and false values. */
8539 else if (BINARY_P (x))
8541 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8542 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8544 if ((cond0 != 0 || cond1 != 0)
8545 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8547 /* If if_then_else_cond returned zero, then true/false are the
8548 same rtl. We must copy one of them to prevent invalid rtl
8549 sharing. */
8550 if (cond0 == 0)
8551 true0 = copy_rtx (true0);
8552 else if (cond1 == 0)
8553 true1 = copy_rtx (true1);
8555 if (COMPARISON_P (x))
8557 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8558 true0, true1);
8559 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8560 false0, false1);
8562 else
8564 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8565 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8568 return cond0 ? cond0 : cond1;
8571 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8572 operands is zero when the other is nonzero, and vice-versa,
8573 and STORE_FLAG_VALUE is 1 or -1. */
8575 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8576 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8577 || code == UMAX)
8578 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8580 rtx op0 = XEXP (XEXP (x, 0), 1);
8581 rtx op1 = XEXP (XEXP (x, 1), 1);
8583 cond0 = XEXP (XEXP (x, 0), 0);
8584 cond1 = XEXP (XEXP (x, 1), 0);
8586 if (COMPARISON_P (cond0)
8587 && COMPARISON_P (cond1)
8588 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8589 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8590 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8591 || ((swap_condition (GET_CODE (cond0))
8592 == reversed_comparison_code (cond1, NULL))
8593 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8594 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8595 && ! side_effects_p (x))
8597 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8598 *pfalse = simplify_gen_binary (MULT, mode,
8599 (code == MINUS
8600 ? simplify_gen_unary (NEG, mode,
8601 op1, mode)
8602 : op1),
8603 const_true_rtx);
8604 return cond0;
8608 /* Similarly for MULT, AND and UMIN, except that for these the result
8609 is always zero. */
8610 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8611 && (code == MULT || code == AND || code == UMIN)
8612 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8614 cond0 = XEXP (XEXP (x, 0), 0);
8615 cond1 = XEXP (XEXP (x, 1), 0);
8617 if (COMPARISON_P (cond0)
8618 && COMPARISON_P (cond1)
8619 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8620 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8621 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8622 || ((swap_condition (GET_CODE (cond0))
8623 == reversed_comparison_code (cond1, NULL))
8624 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8625 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8626 && ! side_effects_p (x))
8628 *ptrue = *pfalse = const0_rtx;
8629 return cond0;
8634 else if (code == IF_THEN_ELSE)
8636 /* If we have IF_THEN_ELSE already, extract the condition and
8637 canonicalize it if it is NE or EQ. */
8638 cond0 = XEXP (x, 0);
8639 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8640 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8641 return XEXP (cond0, 0);
8642 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8644 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8645 return XEXP (cond0, 0);
8647 else
8648 return cond0;
8651 /* If X is a SUBREG, we can narrow both the true and false values
8652 if the inner expression, if there is a condition. */
8653 else if (code == SUBREG
8654 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8655 &true0, &false0)))
8657 true0 = simplify_gen_subreg (mode, true0,
8658 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8659 false0 = simplify_gen_subreg (mode, false0,
8660 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8661 if (true0 && false0)
8663 *ptrue = true0;
8664 *pfalse = false0;
8665 return cond0;
8669 /* If X is a constant, this isn't special and will cause confusions
8670 if we treat it as such. Likewise if it is equivalent to a constant. */
8671 else if (CONSTANT_P (x)
8672 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8675 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8676 will be least confusing to the rest of the compiler. */
8677 else if (mode == BImode)
8679 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8680 return x;
8683 /* If X is known to be either 0 or -1, those are the true and
8684 false values when testing X. */
8685 else if (x == constm1_rtx || x == const0_rtx
8686 || (mode != VOIDmode
8687 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8689 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8690 return x;
8693 /* Likewise for 0 or a single bit. */
8694 else if (SCALAR_INT_MODE_P (mode)
8695 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8696 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8698 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8699 return x;
8702 /* Otherwise fail; show no condition with true and false values the same. */
8703 *ptrue = *pfalse = x;
8704 return 0;
8707 /* Return the value of expression X given the fact that condition COND
8708 is known to be true when applied to REG as its first operand and VAL
8709 as its second. X is known to not be shared and so can be modified in
8710 place.
8712 We only handle the simplest cases, and specifically those cases that
8713 arise with IF_THEN_ELSE expressions. */
8715 static rtx
8716 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8718 enum rtx_code code = GET_CODE (x);
8719 rtx temp;
8720 const char *fmt;
8721 int i, j;
8723 if (side_effects_p (x))
8724 return x;
8726 /* If either operand of the condition is a floating point value,
8727 then we have to avoid collapsing an EQ comparison. */
8728 if (cond == EQ
8729 && rtx_equal_p (x, reg)
8730 && ! FLOAT_MODE_P (GET_MODE (x))
8731 && ! FLOAT_MODE_P (GET_MODE (val)))
8732 return val;
8734 if (cond == UNEQ && rtx_equal_p (x, reg))
8735 return val;
8737 /* If X is (abs REG) and we know something about REG's relationship
8738 with zero, we may be able to simplify this. */
8740 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8741 switch (cond)
8743 case GE: case GT: case EQ:
8744 return XEXP (x, 0);
8745 case LT: case LE:
8746 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8747 XEXP (x, 0),
8748 GET_MODE (XEXP (x, 0)));
8749 default:
8750 break;
8753 /* The only other cases we handle are MIN, MAX, and comparisons if the
8754 operands are the same as REG and VAL. */
8756 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8758 if (rtx_equal_p (XEXP (x, 0), val))
8759 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8761 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8763 if (COMPARISON_P (x))
8765 if (comparison_dominates_p (cond, code))
8766 return const_true_rtx;
8768 code = reversed_comparison_code (x, NULL);
8769 if (code != UNKNOWN
8770 && comparison_dominates_p (cond, code))
8771 return const0_rtx;
8772 else
8773 return x;
8775 else if (code == SMAX || code == SMIN
8776 || code == UMIN || code == UMAX)
8778 int unsignedp = (code == UMIN || code == UMAX);
8780 /* Do not reverse the condition when it is NE or EQ.
8781 This is because we cannot conclude anything about
8782 the value of 'SMAX (x, y)' when x is not equal to y,
8783 but we can when x equals y. */
8784 if ((code == SMAX || code == UMAX)
8785 && ! (cond == EQ || cond == NE))
8786 cond = reverse_condition (cond);
8788 switch (cond)
8790 case GE: case GT:
8791 return unsignedp ? x : XEXP (x, 1);
8792 case LE: case LT:
8793 return unsignedp ? x : XEXP (x, 0);
8794 case GEU: case GTU:
8795 return unsignedp ? XEXP (x, 1) : x;
8796 case LEU: case LTU:
8797 return unsignedp ? XEXP (x, 0) : x;
8798 default:
8799 break;
8804 else if (code == SUBREG)
8806 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8807 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8809 if (SUBREG_REG (x) != r)
8811 /* We must simplify subreg here, before we lose track of the
8812 original inner_mode. */
8813 new_rtx = simplify_subreg (GET_MODE (x), r,
8814 inner_mode, SUBREG_BYTE (x));
8815 if (new_rtx)
8816 return new_rtx;
8817 else
8818 SUBST (SUBREG_REG (x), r);
8821 return x;
8823 /* We don't have to handle SIGN_EXTEND here, because even in the
8824 case of replacing something with a modeless CONST_INT, a
8825 CONST_INT is already (supposed to be) a valid sign extension for
8826 its narrower mode, which implies it's already properly
8827 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8828 story is different. */
8829 else if (code == ZERO_EXTEND)
8831 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8832 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8834 if (XEXP (x, 0) != r)
8836 /* We must simplify the zero_extend here, before we lose
8837 track of the original inner_mode. */
8838 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8839 r, inner_mode);
8840 if (new_rtx)
8841 return new_rtx;
8842 else
8843 SUBST (XEXP (x, 0), r);
8846 return x;
8849 fmt = GET_RTX_FORMAT (code);
8850 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8852 if (fmt[i] == 'e')
8853 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8854 else if (fmt[i] == 'E')
8855 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8856 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8857 cond, reg, val));
8860 return x;
8863 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8864 assignment as a field assignment. */
8866 static int
8867 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8869 if (x == y || rtx_equal_p (x, y))
8870 return 1;
8872 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8873 return 0;
8875 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8876 Note that all SUBREGs of MEM are paradoxical; otherwise they
8877 would have been rewritten. */
8878 if (MEM_P (x) && GET_CODE (y) == SUBREG
8879 && MEM_P (SUBREG_REG (y))
8880 && rtx_equal_p (SUBREG_REG (y),
8881 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8882 return 1;
8884 if (MEM_P (y) && GET_CODE (x) == SUBREG
8885 && MEM_P (SUBREG_REG (x))
8886 && rtx_equal_p (SUBREG_REG (x),
8887 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8888 return 1;
8890 /* We used to see if get_last_value of X and Y were the same but that's
8891 not correct. In one direction, we'll cause the assignment to have
8892 the wrong destination and in the case, we'll import a register into this
8893 insn that might have already have been dead. So fail if none of the
8894 above cases are true. */
8895 return 0;
8898 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8899 Return that assignment if so.
8901 We only handle the most common cases. */
8903 static rtx
8904 make_field_assignment (rtx x)
8906 rtx dest = SET_DEST (x);
8907 rtx src = SET_SRC (x);
8908 rtx assign;
8909 rtx rhs, lhs;
8910 HOST_WIDE_INT c1;
8911 HOST_WIDE_INT pos;
8912 unsigned HOST_WIDE_INT len;
8913 rtx other;
8914 enum machine_mode mode;
8916 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8917 a clear of a one-bit field. We will have changed it to
8918 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8919 for a SUBREG. */
8921 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8922 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8923 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8924 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8926 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8927 1, 1, 1, 0);
8928 if (assign != 0)
8929 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8930 return x;
8933 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8934 && subreg_lowpart_p (XEXP (src, 0))
8935 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8936 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8937 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8938 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8939 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8940 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8942 assign = make_extraction (VOIDmode, dest, 0,
8943 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8944 1, 1, 1, 0);
8945 if (assign != 0)
8946 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8947 return x;
8950 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8951 one-bit field. */
8952 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8953 && XEXP (XEXP (src, 0), 0) == const1_rtx
8954 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8956 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8957 1, 1, 1, 0);
8958 if (assign != 0)
8959 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8960 return x;
8963 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8964 SRC is an AND with all bits of that field set, then we can discard
8965 the AND. */
8966 if (GET_CODE (dest) == ZERO_EXTRACT
8967 && CONST_INT_P (XEXP (dest, 1))
8968 && GET_CODE (src) == AND
8969 && CONST_INT_P (XEXP (src, 1)))
8971 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8972 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8973 unsigned HOST_WIDE_INT ze_mask;
8975 if (width >= HOST_BITS_PER_WIDE_INT)
8976 ze_mask = -1;
8977 else
8978 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8980 /* Complete overlap. We can remove the source AND. */
8981 if ((and_mask & ze_mask) == ze_mask)
8982 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8984 /* Partial overlap. We can reduce the source AND. */
8985 if ((and_mask & ze_mask) != and_mask)
8987 mode = GET_MODE (src);
8988 src = gen_rtx_AND (mode, XEXP (src, 0),
8989 gen_int_mode (and_mask & ze_mask, mode));
8990 return gen_rtx_SET (VOIDmode, dest, src);
8994 /* The other case we handle is assignments into a constant-position
8995 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
8996 a mask that has all one bits except for a group of zero bits and
8997 OTHER is known to have zeros where C1 has ones, this is such an
8998 assignment. Compute the position and length from C1. Shift OTHER
8999 to the appropriate position, force it to the required mode, and
9000 make the extraction. Check for the AND in both operands. */
9002 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9003 return x;
9005 rhs = expand_compound_operation (XEXP (src, 0));
9006 lhs = expand_compound_operation (XEXP (src, 1));
9008 if (GET_CODE (rhs) == AND
9009 && CONST_INT_P (XEXP (rhs, 1))
9010 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9011 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9012 else if (GET_CODE (lhs) == AND
9013 && CONST_INT_P (XEXP (lhs, 1))
9014 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9015 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9016 else
9017 return x;
9019 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9020 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
9021 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9022 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9023 return x;
9025 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9026 if (assign == 0)
9027 return x;
9029 /* The mode to use for the source is the mode of the assignment, or of
9030 what is inside a possible STRICT_LOW_PART. */
9031 mode = (GET_CODE (assign) == STRICT_LOW_PART
9032 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9034 /* Shift OTHER right POS places and make it the source, restricting it
9035 to the proper length and mode. */
9037 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9038 GET_MODE (src),
9039 other, pos),
9040 dest);
9041 src = force_to_mode (src, mode,
9042 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
9043 ? ~(unsigned HOST_WIDE_INT) 0
9044 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9047 /* If SRC is masked by an AND that does not make a difference in
9048 the value being stored, strip it. */
9049 if (GET_CODE (assign) == ZERO_EXTRACT
9050 && CONST_INT_P (XEXP (assign, 1))
9051 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9052 && GET_CODE (src) == AND
9053 && CONST_INT_P (XEXP (src, 1))
9054 && UINTVAL (XEXP (src, 1))
9055 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9056 src = XEXP (src, 0);
9058 return gen_rtx_SET (VOIDmode, assign, src);
9061 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9062 if so. */
9064 static rtx
9065 apply_distributive_law (rtx x)
9067 enum rtx_code code = GET_CODE (x);
9068 enum rtx_code inner_code;
9069 rtx lhs, rhs, other;
9070 rtx tem;
9072 /* Distributivity is not true for floating point as it can change the
9073 value. So we don't do it unless -funsafe-math-optimizations. */
9074 if (FLOAT_MODE_P (GET_MODE (x))
9075 && ! flag_unsafe_math_optimizations)
9076 return x;
9078 /* The outer operation can only be one of the following: */
9079 if (code != IOR && code != AND && code != XOR
9080 && code != PLUS && code != MINUS)
9081 return x;
9083 lhs = XEXP (x, 0);
9084 rhs = XEXP (x, 1);
9086 /* If either operand is a primitive we can't do anything, so get out
9087 fast. */
9088 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9089 return x;
9091 lhs = expand_compound_operation (lhs);
9092 rhs = expand_compound_operation (rhs);
9093 inner_code = GET_CODE (lhs);
9094 if (inner_code != GET_CODE (rhs))
9095 return x;
9097 /* See if the inner and outer operations distribute. */
9098 switch (inner_code)
9100 case LSHIFTRT:
9101 case ASHIFTRT:
9102 case AND:
9103 case IOR:
9104 /* These all distribute except over PLUS. */
9105 if (code == PLUS || code == MINUS)
9106 return x;
9107 break;
9109 case MULT:
9110 if (code != PLUS && code != MINUS)
9111 return x;
9112 break;
9114 case ASHIFT:
9115 /* This is also a multiply, so it distributes over everything. */
9116 break;
9118 case SUBREG:
9119 /* Non-paradoxical SUBREGs distributes over all operations,
9120 provided the inner modes and byte offsets are the same, this
9121 is an extraction of a low-order part, we don't convert an fp
9122 operation to int or vice versa, this is not a vector mode,
9123 and we would not be converting a single-word operation into a
9124 multi-word operation. The latter test is not required, but
9125 it prevents generating unneeded multi-word operations. Some
9126 of the previous tests are redundant given the latter test,
9127 but are retained because they are required for correctness.
9129 We produce the result slightly differently in this case. */
9131 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
9132 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
9133 || ! subreg_lowpart_p (lhs)
9134 || (GET_MODE_CLASS (GET_MODE (lhs))
9135 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
9136 || (GET_MODE_SIZE (GET_MODE (lhs))
9137 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
9138 || VECTOR_MODE_P (GET_MODE (lhs))
9139 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
9140 /* Result might need to be truncated. Don't change mode if
9141 explicit truncation is needed. */
9142 || !TRULY_NOOP_TRUNCATION
9143 (GET_MODE_BITSIZE (GET_MODE (x)),
9144 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
9145 return x;
9147 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
9148 SUBREG_REG (lhs), SUBREG_REG (rhs));
9149 return gen_lowpart (GET_MODE (x), tem);
9151 default:
9152 return x;
9155 /* Set LHS and RHS to the inner operands (A and B in the example
9156 above) and set OTHER to the common operand (C in the example).
9157 There is only one way to do this unless the inner operation is
9158 commutative. */
9159 if (COMMUTATIVE_ARITH_P (lhs)
9160 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9161 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9162 else if (COMMUTATIVE_ARITH_P (lhs)
9163 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9164 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9165 else if (COMMUTATIVE_ARITH_P (lhs)
9166 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9167 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9168 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9169 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9170 else
9171 return x;
9173 /* Form the new inner operation, seeing if it simplifies first. */
9174 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9176 /* There is one exception to the general way of distributing:
9177 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9178 if (code == XOR && inner_code == IOR)
9180 inner_code = AND;
9181 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9184 /* We may be able to continuing distributing the result, so call
9185 ourselves recursively on the inner operation before forming the
9186 outer operation, which we return. */
9187 return simplify_gen_binary (inner_code, GET_MODE (x),
9188 apply_distributive_law (tem), other);
9191 /* See if X is of the form (* (+ A B) C), and if so convert to
9192 (+ (* A C) (* B C)) and try to simplify.
9194 Most of the time, this results in no change. However, if some of
9195 the operands are the same or inverses of each other, simplifications
9196 will result.
9198 For example, (and (ior A B) (not B)) can occur as the result of
9199 expanding a bit field assignment. When we apply the distributive
9200 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9201 which then simplifies to (and (A (not B))).
9203 Note that no checks happen on the validity of applying the inverse
9204 distributive law. This is pointless since we can do it in the
9205 few places where this routine is called.
9207 N is the index of the term that is decomposed (the arithmetic operation,
9208 i.e. (+ A B) in the first example above). !N is the index of the term that
9209 is distributed, i.e. of C in the first example above. */
9210 static rtx
9211 distribute_and_simplify_rtx (rtx x, int n)
9213 enum machine_mode mode;
9214 enum rtx_code outer_code, inner_code;
9215 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9217 /* Distributivity is not true for floating point as it can change the
9218 value. So we don't do it unless -funsafe-math-optimizations. */
9219 if (FLOAT_MODE_P (GET_MODE (x))
9220 && ! flag_unsafe_math_optimizations)
9221 return NULL_RTX;
9223 decomposed = XEXP (x, n);
9224 if (!ARITHMETIC_P (decomposed))
9225 return NULL_RTX;
9227 mode = GET_MODE (x);
9228 outer_code = GET_CODE (x);
9229 distributed = XEXP (x, !n);
9231 inner_code = GET_CODE (decomposed);
9232 inner_op0 = XEXP (decomposed, 0);
9233 inner_op1 = XEXP (decomposed, 1);
9235 /* Special case (and (xor B C) (not A)), which is equivalent to
9236 (xor (ior A B) (ior A C)) */
9237 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9239 distributed = XEXP (distributed, 0);
9240 outer_code = IOR;
9243 if (n == 0)
9245 /* Distribute the second term. */
9246 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9247 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9249 else
9251 /* Distribute the first term. */
9252 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9253 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9256 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9257 new_op0, new_op1));
9258 if (GET_CODE (tmp) != outer_code
9259 && rtx_cost (tmp, SET, optimize_this_for_speed_p)
9260 < rtx_cost (x, SET, optimize_this_for_speed_p))
9261 return tmp;
9263 return NULL_RTX;
9266 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9267 in MODE. Return an equivalent form, if different from (and VAROP
9268 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9270 static rtx
9271 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9272 unsigned HOST_WIDE_INT constop)
9274 unsigned HOST_WIDE_INT nonzero;
9275 unsigned HOST_WIDE_INT orig_constop;
9276 rtx orig_varop;
9277 int i;
9279 orig_varop = varop;
9280 orig_constop = constop;
9281 if (GET_CODE (varop) == CLOBBER)
9282 return NULL_RTX;
9284 /* Simplify VAROP knowing that we will be only looking at some of the
9285 bits in it.
9287 Note by passing in CONSTOP, we guarantee that the bits not set in
9288 CONSTOP are not significant and will never be examined. We must
9289 ensure that is the case by explicitly masking out those bits
9290 before returning. */
9291 varop = force_to_mode (varop, mode, constop, 0);
9293 /* If VAROP is a CLOBBER, we will fail so return it. */
9294 if (GET_CODE (varop) == CLOBBER)
9295 return varop;
9297 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9298 to VAROP and return the new constant. */
9299 if (CONST_INT_P (varop))
9300 return gen_int_mode (INTVAL (varop) & constop, mode);
9302 /* See what bits may be nonzero in VAROP. Unlike the general case of
9303 a call to nonzero_bits, here we don't care about bits outside
9304 MODE. */
9306 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9308 /* Turn off all bits in the constant that are known to already be zero.
9309 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9310 which is tested below. */
9312 constop &= nonzero;
9314 /* If we don't have any bits left, return zero. */
9315 if (constop == 0)
9316 return const0_rtx;
9318 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9319 a power of two, we can replace this with an ASHIFT. */
9320 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9321 && (i = exact_log2 (constop)) >= 0)
9322 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9324 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9325 or XOR, then try to apply the distributive law. This may eliminate
9326 operations if either branch can be simplified because of the AND.
9327 It may also make some cases more complex, but those cases probably
9328 won't match a pattern either with or without this. */
9330 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9331 return
9332 gen_lowpart
9333 (mode,
9334 apply_distributive_law
9335 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9336 simplify_and_const_int (NULL_RTX,
9337 GET_MODE (varop),
9338 XEXP (varop, 0),
9339 constop),
9340 simplify_and_const_int (NULL_RTX,
9341 GET_MODE (varop),
9342 XEXP (varop, 1),
9343 constop))));
9345 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9346 the AND and see if one of the operands simplifies to zero. If so, we
9347 may eliminate it. */
9349 if (GET_CODE (varop) == PLUS
9350 && exact_log2 (constop + 1) >= 0)
9352 rtx o0, o1;
9354 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9355 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9356 if (o0 == const0_rtx)
9357 return o1;
9358 if (o1 == const0_rtx)
9359 return o0;
9362 /* Make a SUBREG if necessary. If we can't make it, fail. */
9363 varop = gen_lowpart (mode, varop);
9364 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9365 return NULL_RTX;
9367 /* If we are only masking insignificant bits, return VAROP. */
9368 if (constop == nonzero)
9369 return varop;
9371 if (varop == orig_varop && constop == orig_constop)
9372 return NULL_RTX;
9374 /* Otherwise, return an AND. */
9375 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9379 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9380 in MODE.
9382 Return an equivalent form, if different from X. Otherwise, return X. If
9383 X is zero, we are to always construct the equivalent form. */
9385 static rtx
9386 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9387 unsigned HOST_WIDE_INT constop)
9389 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9390 if (tem)
9391 return tem;
9393 if (!x)
9394 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9395 gen_int_mode (constop, mode));
9396 if (GET_MODE (x) != mode)
9397 x = gen_lowpart (mode, x);
9398 return x;
9401 /* Given a REG, X, compute which bits in X can be nonzero.
9402 We don't care about bits outside of those defined in MODE.
9404 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9405 a shift, AND, or zero_extract, we can do better. */
9407 static rtx
9408 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9409 const_rtx known_x ATTRIBUTE_UNUSED,
9410 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9411 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9412 unsigned HOST_WIDE_INT *nonzero)
9414 rtx tem;
9415 reg_stat_type *rsp;
9417 /* If X is a register whose nonzero bits value is current, use it.
9418 Otherwise, if X is a register whose value we can find, use that
9419 value. Otherwise, use the previously-computed global nonzero bits
9420 for this register. */
9422 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9423 if (rsp->last_set_value != 0
9424 && (rsp->last_set_mode == mode
9425 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9426 && GET_MODE_CLASS (mode) == MODE_INT))
9427 && ((rsp->last_set_label >= label_tick_ebb_start
9428 && rsp->last_set_label < label_tick)
9429 || (rsp->last_set_label == label_tick
9430 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9431 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9432 && REG_N_SETS (REGNO (x)) == 1
9433 && !REGNO_REG_SET_P
9434 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9436 *nonzero &= rsp->last_set_nonzero_bits;
9437 return NULL;
9440 tem = get_last_value (x);
9442 if (tem)
9444 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9445 /* If X is narrower than MODE and TEM is a non-negative
9446 constant that would appear negative in the mode of X,
9447 sign-extend it for use in reg_nonzero_bits because some
9448 machines (maybe most) will actually do the sign-extension
9449 and this is the conservative approach.
9451 ??? For 2.5, try to tighten up the MD files in this regard
9452 instead of this kludge. */
9454 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9455 && CONST_INT_P (tem)
9456 && INTVAL (tem) > 0
9457 && 0 != (UINTVAL (tem)
9458 & ((unsigned HOST_WIDE_INT) 1
9459 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9460 tem = GEN_INT (UINTVAL (tem)
9461 | ((unsigned HOST_WIDE_INT) (-1)
9462 << GET_MODE_BITSIZE (GET_MODE (x))));
9463 #endif
9464 return tem;
9466 else if (nonzero_sign_valid && rsp->nonzero_bits)
9468 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9470 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9471 /* We don't know anything about the upper bits. */
9472 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9473 *nonzero &= mask;
9476 return NULL;
9479 /* Return the number of bits at the high-order end of X that are known to
9480 be equal to the sign bit. X will be used in mode MODE; if MODE is
9481 VOIDmode, X will be used in its own mode. The returned value will always
9482 be between 1 and the number of bits in MODE. */
9484 static rtx
9485 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9486 const_rtx known_x ATTRIBUTE_UNUSED,
9487 enum machine_mode known_mode
9488 ATTRIBUTE_UNUSED,
9489 unsigned int known_ret ATTRIBUTE_UNUSED,
9490 unsigned int *result)
9492 rtx tem;
9493 reg_stat_type *rsp;
9495 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9496 if (rsp->last_set_value != 0
9497 && rsp->last_set_mode == mode
9498 && ((rsp->last_set_label >= label_tick_ebb_start
9499 && rsp->last_set_label < label_tick)
9500 || (rsp->last_set_label == label_tick
9501 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9502 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9503 && REG_N_SETS (REGNO (x)) == 1
9504 && !REGNO_REG_SET_P
9505 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9507 *result = rsp->last_set_sign_bit_copies;
9508 return NULL;
9511 tem = get_last_value (x);
9512 if (tem != 0)
9513 return tem;
9515 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9516 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9517 *result = rsp->sign_bit_copies;
9519 return NULL;
9522 /* Return the number of "extended" bits there are in X, when interpreted
9523 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9524 unsigned quantities, this is the number of high-order zero bits.
9525 For signed quantities, this is the number of copies of the sign bit
9526 minus 1. In both case, this function returns the number of "spare"
9527 bits. For example, if two quantities for which this function returns
9528 at least 1 are added, the addition is known not to overflow.
9530 This function will always return 0 unless called during combine, which
9531 implies that it must be called from a define_split. */
9533 unsigned int
9534 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9536 if (nonzero_sign_valid == 0)
9537 return 0;
9539 return (unsignedp
9540 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9541 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9542 - floor_log2 (nonzero_bits (x, mode)))
9543 : 0)
9544 : num_sign_bit_copies (x, mode) - 1);
9547 /* This function is called from `simplify_shift_const' to merge two
9548 outer operations. Specifically, we have already found that we need
9549 to perform operation *POP0 with constant *PCONST0 at the outermost
9550 position. We would now like to also perform OP1 with constant CONST1
9551 (with *POP0 being done last).
9553 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9554 the resulting operation. *PCOMP_P is set to 1 if we would need to
9555 complement the innermost operand, otherwise it is unchanged.
9557 MODE is the mode in which the operation will be done. No bits outside
9558 the width of this mode matter. It is assumed that the width of this mode
9559 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9561 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9562 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9563 result is simply *PCONST0.
9565 If the resulting operation cannot be expressed as one operation, we
9566 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9568 static int
9569 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
9571 enum rtx_code op0 = *pop0;
9572 HOST_WIDE_INT const0 = *pconst0;
9574 const0 &= GET_MODE_MASK (mode);
9575 const1 &= GET_MODE_MASK (mode);
9577 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9578 if (op0 == AND)
9579 const1 &= const0;
9581 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9582 if OP0 is SET. */
9584 if (op1 == UNKNOWN || op0 == SET)
9585 return 1;
9587 else if (op0 == UNKNOWN)
9588 op0 = op1, const0 = const1;
9590 else if (op0 == op1)
9592 switch (op0)
9594 case AND:
9595 const0 &= const1;
9596 break;
9597 case IOR:
9598 const0 |= const1;
9599 break;
9600 case XOR:
9601 const0 ^= const1;
9602 break;
9603 case PLUS:
9604 const0 += const1;
9605 break;
9606 case NEG:
9607 op0 = UNKNOWN;
9608 break;
9609 default:
9610 break;
9614 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9615 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9616 return 0;
9618 /* If the two constants aren't the same, we can't do anything. The
9619 remaining six cases can all be done. */
9620 else if (const0 != const1)
9621 return 0;
9623 else
9624 switch (op0)
9626 case IOR:
9627 if (op1 == AND)
9628 /* (a & b) | b == b */
9629 op0 = SET;
9630 else /* op1 == XOR */
9631 /* (a ^ b) | b == a | b */
9633 break;
9635 case XOR:
9636 if (op1 == AND)
9637 /* (a & b) ^ b == (~a) & b */
9638 op0 = AND, *pcomp_p = 1;
9639 else /* op1 == IOR */
9640 /* (a | b) ^ b == a & ~b */
9641 op0 = AND, const0 = ~const0;
9642 break;
9644 case AND:
9645 if (op1 == IOR)
9646 /* (a | b) & b == b */
9647 op0 = SET;
9648 else /* op1 == XOR */
9649 /* (a ^ b) & b) == (~a) & b */
9650 *pcomp_p = 1;
9651 break;
9652 default:
9653 break;
9656 /* Check for NO-OP cases. */
9657 const0 &= GET_MODE_MASK (mode);
9658 if (const0 == 0
9659 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9660 op0 = UNKNOWN;
9661 else if (const0 == 0 && op0 == AND)
9662 op0 = SET;
9663 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9664 && op0 == AND)
9665 op0 = UNKNOWN;
9667 *pop0 = op0;
9669 /* ??? Slightly redundant with the above mask, but not entirely.
9670 Moving this above means we'd have to sign-extend the mode mask
9671 for the final test. */
9672 if (op0 != UNKNOWN && op0 != NEG)
9673 *pconst0 = trunc_int_for_mode (const0, mode);
9675 return 1;
9678 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9679 the shift in. The original shift operation CODE is performed on OP in
9680 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9681 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9682 result of the shift is subject to operation OUTER_CODE with operand
9683 OUTER_CONST. */
9685 static enum machine_mode
9686 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9687 enum machine_mode orig_mode, enum machine_mode mode,
9688 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9690 if (orig_mode == mode)
9691 return mode;
9692 gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9694 /* In general we can't perform in wider mode for right shift and rotate. */
9695 switch (code)
9697 case ASHIFTRT:
9698 /* We can still widen if the bits brought in from the left are identical
9699 to the sign bit of ORIG_MODE. */
9700 if (num_sign_bit_copies (op, mode)
9701 > (unsigned) (GET_MODE_BITSIZE (mode)
9702 - GET_MODE_BITSIZE (orig_mode)))
9703 return mode;
9704 return orig_mode;
9706 case LSHIFTRT:
9707 /* Similarly here but with zero bits. */
9708 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9709 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9710 return mode;
9712 /* We can also widen if the bits brought in will be masked off. This
9713 operation is performed in ORIG_MODE. */
9714 if (outer_code == AND)
9716 int care_bits = low_bitmask_len (orig_mode, outer_const);
9718 if (care_bits >= 0
9719 && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9720 return mode;
9722 /* fall through */
9724 case ROTATE:
9725 return orig_mode;
9727 case ROTATERT:
9728 gcc_unreachable ();
9730 default:
9731 return mode;
9735 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9736 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
9737 simplify it. Otherwise, return a simplified value.
9739 The shift is normally computed in the widest mode we find in VAROP, as
9740 long as it isn't a different number of words than RESULT_MODE. Exceptions
9741 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9743 static rtx
9744 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9745 rtx varop, int orig_count)
9747 enum rtx_code orig_code = code;
9748 rtx orig_varop = varop;
9749 int count;
9750 enum machine_mode mode = result_mode;
9751 enum machine_mode shift_mode, tmode;
9752 unsigned int mode_words
9753 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9754 /* We form (outer_op (code varop count) (outer_const)). */
9755 enum rtx_code outer_op = UNKNOWN;
9756 HOST_WIDE_INT outer_const = 0;
9757 int complement_p = 0;
9758 rtx new_rtx, x;
9760 /* Make sure and truncate the "natural" shift on the way in. We don't
9761 want to do this inside the loop as it makes it more difficult to
9762 combine shifts. */
9763 if (SHIFT_COUNT_TRUNCATED)
9764 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9766 /* If we were given an invalid count, don't do anything except exactly
9767 what was requested. */
9769 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9770 return NULL_RTX;
9772 count = orig_count;
9774 /* Unless one of the branches of the `if' in this loop does a `continue',
9775 we will `break' the loop after the `if'. */
9777 while (count != 0)
9779 /* If we have an operand of (clobber (const_int 0)), fail. */
9780 if (GET_CODE (varop) == CLOBBER)
9781 return NULL_RTX;
9783 /* Convert ROTATERT to ROTATE. */
9784 if (code == ROTATERT)
9786 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9787 code = ROTATE;
9788 if (VECTOR_MODE_P (result_mode))
9789 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9790 else
9791 count = bitsize - count;
9794 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9795 mode, outer_op, outer_const);
9797 /* Handle cases where the count is greater than the size of the mode
9798 minus 1. For ASHIFT, use the size minus one as the count (this can
9799 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9800 take the count modulo the size. For other shifts, the result is
9801 zero.
9803 Since these shifts are being produced by the compiler by combining
9804 multiple operations, each of which are defined, we know what the
9805 result is supposed to be. */
9807 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9809 if (code == ASHIFTRT)
9810 count = GET_MODE_BITSIZE (shift_mode) - 1;
9811 else if (code == ROTATE || code == ROTATERT)
9812 count %= GET_MODE_BITSIZE (shift_mode);
9813 else
9815 /* We can't simply return zero because there may be an
9816 outer op. */
9817 varop = const0_rtx;
9818 count = 0;
9819 break;
9823 /* If we discovered we had to complement VAROP, leave. Making a NOT
9824 here would cause an infinite loop. */
9825 if (complement_p)
9826 break;
9828 /* An arithmetic right shift of a quantity known to be -1 or 0
9829 is a no-op. */
9830 if (code == ASHIFTRT
9831 && (num_sign_bit_copies (varop, shift_mode)
9832 == GET_MODE_BITSIZE (shift_mode)))
9834 count = 0;
9835 break;
9838 /* If we are doing an arithmetic right shift and discarding all but
9839 the sign bit copies, this is equivalent to doing a shift by the
9840 bitsize minus one. Convert it into that shift because it will often
9841 allow other simplifications. */
9843 if (code == ASHIFTRT
9844 && (count + num_sign_bit_copies (varop, shift_mode)
9845 >= GET_MODE_BITSIZE (shift_mode)))
9846 count = GET_MODE_BITSIZE (shift_mode) - 1;
9848 /* We simplify the tests below and elsewhere by converting
9849 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9850 `make_compound_operation' will convert it to an ASHIFTRT for
9851 those machines (such as VAX) that don't have an LSHIFTRT. */
9852 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9853 && code == ASHIFTRT
9854 && ((nonzero_bits (varop, shift_mode)
9855 & ((unsigned HOST_WIDE_INT) 1
9856 << (GET_MODE_BITSIZE (shift_mode) - 1))) == 0))
9857 code = LSHIFTRT;
9859 if (((code == LSHIFTRT
9860 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9861 && !(nonzero_bits (varop, shift_mode) >> count))
9862 || (code == ASHIFT
9863 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9864 && !((nonzero_bits (varop, shift_mode) << count)
9865 & GET_MODE_MASK (shift_mode))))
9866 && !side_effects_p (varop))
9867 varop = const0_rtx;
9869 switch (GET_CODE (varop))
9871 case SIGN_EXTEND:
9872 case ZERO_EXTEND:
9873 case SIGN_EXTRACT:
9874 case ZERO_EXTRACT:
9875 new_rtx = expand_compound_operation (varop);
9876 if (new_rtx != varop)
9878 varop = new_rtx;
9879 continue;
9881 break;
9883 case MEM:
9884 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9885 minus the width of a smaller mode, we can do this with a
9886 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9887 if ((code == ASHIFTRT || code == LSHIFTRT)
9888 && ! mode_dependent_address_p (XEXP (varop, 0))
9889 && ! MEM_VOLATILE_P (varop)
9890 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9891 MODE_INT, 1)) != BLKmode)
9893 new_rtx = adjust_address_nv (varop, tmode,
9894 BYTES_BIG_ENDIAN ? 0
9895 : count / BITS_PER_UNIT);
9897 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9898 : ZERO_EXTEND, mode, new_rtx);
9899 count = 0;
9900 continue;
9902 break;
9904 case SUBREG:
9905 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9906 the same number of words as what we've seen so far. Then store
9907 the widest mode in MODE. */
9908 if (subreg_lowpart_p (varop)
9909 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9910 > GET_MODE_SIZE (GET_MODE (varop)))
9911 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9912 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9913 == mode_words
9914 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
9915 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
9917 varop = SUBREG_REG (varop);
9918 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9919 mode = GET_MODE (varop);
9920 continue;
9922 break;
9924 case MULT:
9925 /* Some machines use MULT instead of ASHIFT because MULT
9926 is cheaper. But it is still better on those machines to
9927 merge two shifts into one. */
9928 if (CONST_INT_P (XEXP (varop, 1))
9929 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9931 varop
9932 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9933 XEXP (varop, 0),
9934 GEN_INT (exact_log2 (
9935 UINTVAL (XEXP (varop, 1)))));
9936 continue;
9938 break;
9940 case UDIV:
9941 /* Similar, for when divides are cheaper. */
9942 if (CONST_INT_P (XEXP (varop, 1))
9943 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9945 varop
9946 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9947 XEXP (varop, 0),
9948 GEN_INT (exact_log2 (
9949 UINTVAL (XEXP (varop, 1)))));
9950 continue;
9952 break;
9954 case ASHIFTRT:
9955 /* If we are extracting just the sign bit of an arithmetic
9956 right shift, that shift is not needed. However, the sign
9957 bit of a wider mode may be different from what would be
9958 interpreted as the sign bit in a narrower mode, so, if
9959 the result is narrower, don't discard the shift. */
9960 if (code == LSHIFTRT
9961 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9962 && (GET_MODE_BITSIZE (result_mode)
9963 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9965 varop = XEXP (varop, 0);
9966 continue;
9969 /* ... fall through ... */
9971 case LSHIFTRT:
9972 case ASHIFT:
9973 case ROTATE:
9974 /* Here we have two nested shifts. The result is usually the
9975 AND of a new shift with a mask. We compute the result below. */
9976 if (CONST_INT_P (XEXP (varop, 1))
9977 && INTVAL (XEXP (varop, 1)) >= 0
9978 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9979 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9980 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9981 && !VECTOR_MODE_P (result_mode))
9983 enum rtx_code first_code = GET_CODE (varop);
9984 unsigned int first_count = INTVAL (XEXP (varop, 1));
9985 unsigned HOST_WIDE_INT mask;
9986 rtx mask_rtx;
9988 /* We have one common special case. We can't do any merging if
9989 the inner code is an ASHIFTRT of a smaller mode. However, if
9990 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9991 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9992 we can convert it to
9993 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9994 This simplifies certain SIGN_EXTEND operations. */
9995 if (code == ASHIFT && first_code == ASHIFTRT
9996 && count == (GET_MODE_BITSIZE (result_mode)
9997 - GET_MODE_BITSIZE (GET_MODE (varop))))
9999 /* C3 has the low-order C1 bits zero. */
10001 mask = GET_MODE_MASK (mode)
10002 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10004 varop = simplify_and_const_int (NULL_RTX, result_mode,
10005 XEXP (varop, 0), mask);
10006 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10007 varop, count);
10008 count = first_count;
10009 code = ASHIFTRT;
10010 continue;
10013 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10014 than C1 high-order bits equal to the sign bit, we can convert
10015 this to either an ASHIFT or an ASHIFTRT depending on the
10016 two counts.
10018 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10020 if (code == ASHIFTRT && first_code == ASHIFT
10021 && GET_MODE (varop) == shift_mode
10022 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10023 > first_count))
10025 varop = XEXP (varop, 0);
10026 count -= first_count;
10027 if (count < 0)
10029 count = -count;
10030 code = ASHIFT;
10033 continue;
10036 /* There are some cases we can't do. If CODE is ASHIFTRT,
10037 we can only do this if FIRST_CODE is also ASHIFTRT.
10039 We can't do the case when CODE is ROTATE and FIRST_CODE is
10040 ASHIFTRT.
10042 If the mode of this shift is not the mode of the outer shift,
10043 we can't do this if either shift is a right shift or ROTATE.
10045 Finally, we can't do any of these if the mode is too wide
10046 unless the codes are the same.
10048 Handle the case where the shift codes are the same
10049 first. */
10051 if (code == first_code)
10053 if (GET_MODE (varop) != result_mode
10054 && (code == ASHIFTRT || code == LSHIFTRT
10055 || code == ROTATE))
10056 break;
10058 count += first_count;
10059 varop = XEXP (varop, 0);
10060 continue;
10063 if (code == ASHIFTRT
10064 || (code == ROTATE && first_code == ASHIFTRT)
10065 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
10066 || (GET_MODE (varop) != result_mode
10067 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10068 || first_code == ROTATE
10069 || code == ROTATE)))
10070 break;
10072 /* To compute the mask to apply after the shift, shift the
10073 nonzero bits of the inner shift the same way the
10074 outer shift will. */
10076 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10078 mask_rtx
10079 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10080 GEN_INT (count));
10082 /* Give up if we can't compute an outer operation to use. */
10083 if (mask_rtx == 0
10084 || !CONST_INT_P (mask_rtx)
10085 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10086 INTVAL (mask_rtx),
10087 result_mode, &complement_p))
10088 break;
10090 /* If the shifts are in the same direction, we add the
10091 counts. Otherwise, we subtract them. */
10092 if ((code == ASHIFTRT || code == LSHIFTRT)
10093 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10094 count += first_count;
10095 else
10096 count -= first_count;
10098 /* If COUNT is positive, the new shift is usually CODE,
10099 except for the two exceptions below, in which case it is
10100 FIRST_CODE. If the count is negative, FIRST_CODE should
10101 always be used */
10102 if (count > 0
10103 && ((first_code == ROTATE && code == ASHIFT)
10104 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10105 code = first_code;
10106 else if (count < 0)
10107 code = first_code, count = -count;
10109 varop = XEXP (varop, 0);
10110 continue;
10113 /* If we have (A << B << C) for any shift, we can convert this to
10114 (A << C << B). This wins if A is a constant. Only try this if
10115 B is not a constant. */
10117 else if (GET_CODE (varop) == code
10118 && CONST_INT_P (XEXP (varop, 0))
10119 && !CONST_INT_P (XEXP (varop, 1)))
10121 rtx new_rtx = simplify_const_binary_operation (code, mode,
10122 XEXP (varop, 0),
10123 GEN_INT (count));
10124 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10125 count = 0;
10126 continue;
10128 break;
10130 case NOT:
10131 if (VECTOR_MODE_P (mode))
10132 break;
10134 /* Make this fit the case below. */
10135 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
10136 GEN_INT (GET_MODE_MASK (mode)));
10137 continue;
10139 case IOR:
10140 case AND:
10141 case XOR:
10142 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10143 with C the size of VAROP - 1 and the shift is logical if
10144 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10145 we have an (le X 0) operation. If we have an arithmetic shift
10146 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10147 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10149 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10150 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10151 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10152 && (code == LSHIFTRT || code == ASHIFTRT)
10153 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10154 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10156 count = 0;
10157 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10158 const0_rtx);
10160 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10161 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10163 continue;
10166 /* If we have (shift (logical)), move the logical to the outside
10167 to allow it to possibly combine with another logical and the
10168 shift to combine with another shift. This also canonicalizes to
10169 what a ZERO_EXTRACT looks like. Also, some machines have
10170 (and (shift)) insns. */
10172 if (CONST_INT_P (XEXP (varop, 1))
10173 /* We can't do this if we have (ashiftrt (xor)) and the
10174 constant has its sign bit set in shift_mode. */
10175 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10176 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10177 shift_mode))
10178 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10179 XEXP (varop, 1),
10180 GEN_INT (count))) != 0
10181 && CONST_INT_P (new_rtx)
10182 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10183 INTVAL (new_rtx), result_mode, &complement_p))
10185 varop = XEXP (varop, 0);
10186 continue;
10189 /* If we can't do that, try to simplify the shift in each arm of the
10190 logical expression, make a new logical expression, and apply
10191 the inverse distributive law. This also can't be done
10192 for some (ashiftrt (xor)). */
10193 if (CONST_INT_P (XEXP (varop, 1))
10194 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10195 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10196 shift_mode)))
10198 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10199 XEXP (varop, 0), count);
10200 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10201 XEXP (varop, 1), count);
10203 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10204 lhs, rhs);
10205 varop = apply_distributive_law (varop);
10207 count = 0;
10208 continue;
10210 break;
10212 case EQ:
10213 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10214 says that the sign bit can be tested, FOO has mode MODE, C is
10215 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
10216 that may be nonzero. */
10217 if (code == LSHIFTRT
10218 && XEXP (varop, 1) == const0_rtx
10219 && GET_MODE (XEXP (varop, 0)) == result_mode
10220 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10221 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10222 && STORE_FLAG_VALUE == -1
10223 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10224 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10225 &complement_p))
10227 varop = XEXP (varop, 0);
10228 count = 0;
10229 continue;
10231 break;
10233 case NEG:
10234 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10235 than the number of bits in the mode is equivalent to A. */
10236 if (code == LSHIFTRT
10237 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10238 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10240 varop = XEXP (varop, 0);
10241 count = 0;
10242 continue;
10245 /* NEG commutes with ASHIFT since it is multiplication. Move the
10246 NEG outside to allow shifts to combine. */
10247 if (code == ASHIFT
10248 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10249 &complement_p))
10251 varop = XEXP (varop, 0);
10252 continue;
10254 break;
10256 case PLUS:
10257 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10258 is one less than the number of bits in the mode is
10259 equivalent to (xor A 1). */
10260 if (code == LSHIFTRT
10261 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10262 && XEXP (varop, 1) == constm1_rtx
10263 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10264 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10265 &complement_p))
10267 count = 0;
10268 varop = XEXP (varop, 0);
10269 continue;
10272 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10273 that might be nonzero in BAR are those being shifted out and those
10274 bits are known zero in FOO, we can replace the PLUS with FOO.
10275 Similarly in the other operand order. This code occurs when
10276 we are computing the size of a variable-size array. */
10278 if ((code == ASHIFTRT || code == LSHIFTRT)
10279 && count < HOST_BITS_PER_WIDE_INT
10280 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10281 && (nonzero_bits (XEXP (varop, 1), result_mode)
10282 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10284 varop = XEXP (varop, 0);
10285 continue;
10287 else if ((code == ASHIFTRT || code == LSHIFTRT)
10288 && count < HOST_BITS_PER_WIDE_INT
10289 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10290 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10291 >> count)
10292 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10293 & nonzero_bits (XEXP (varop, 1),
10294 result_mode)))
10296 varop = XEXP (varop, 1);
10297 continue;
10300 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10301 if (code == ASHIFT
10302 && CONST_INT_P (XEXP (varop, 1))
10303 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10304 XEXP (varop, 1),
10305 GEN_INT (count))) != 0
10306 && CONST_INT_P (new_rtx)
10307 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10308 INTVAL (new_rtx), result_mode, &complement_p))
10310 varop = XEXP (varop, 0);
10311 continue;
10314 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10315 signbit', and attempt to change the PLUS to an XOR and move it to
10316 the outer operation as is done above in the AND/IOR/XOR case
10317 leg for shift(logical). See details in logical handling above
10318 for reasoning in doing so. */
10319 if (code == LSHIFTRT
10320 && CONST_INT_P (XEXP (varop, 1))
10321 && mode_signbit_p (result_mode, XEXP (varop, 1))
10322 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10323 XEXP (varop, 1),
10324 GEN_INT (count))) != 0
10325 && CONST_INT_P (new_rtx)
10326 && merge_outer_ops (&outer_op, &outer_const, XOR,
10327 INTVAL (new_rtx), result_mode, &complement_p))
10329 varop = XEXP (varop, 0);
10330 continue;
10333 break;
10335 case MINUS:
10336 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10337 with C the size of VAROP - 1 and the shift is logical if
10338 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10339 we have a (gt X 0) operation. If the shift is arithmetic with
10340 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10341 we have a (neg (gt X 0)) operation. */
10343 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10344 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10345 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10346 && (code == LSHIFTRT || code == ASHIFTRT)
10347 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10348 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10349 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10351 count = 0;
10352 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10353 const0_rtx);
10355 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10356 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10358 continue;
10360 break;
10362 case TRUNCATE:
10363 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10364 if the truncate does not affect the value. */
10365 if (code == LSHIFTRT
10366 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10367 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10368 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10369 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
10370 - GET_MODE_BITSIZE (GET_MODE (varop)))))
10372 rtx varop_inner = XEXP (varop, 0);
10374 varop_inner
10375 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10376 XEXP (varop_inner, 0),
10377 GEN_INT
10378 (count + INTVAL (XEXP (varop_inner, 1))));
10379 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10380 count = 0;
10381 continue;
10383 break;
10385 default:
10386 break;
10389 break;
10392 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10393 outer_op, outer_const);
10395 /* We have now finished analyzing the shift. The result should be
10396 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10397 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10398 to the result of the shift. OUTER_CONST is the relevant constant,
10399 but we must turn off all bits turned off in the shift. */
10401 if (outer_op == UNKNOWN
10402 && orig_code == code && orig_count == count
10403 && varop == orig_varop
10404 && shift_mode == GET_MODE (varop))
10405 return NULL_RTX;
10407 /* Make a SUBREG if necessary. If we can't make it, fail. */
10408 varop = gen_lowpart (shift_mode, varop);
10409 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10410 return NULL_RTX;
10412 /* If we have an outer operation and we just made a shift, it is
10413 possible that we could have simplified the shift were it not
10414 for the outer operation. So try to do the simplification
10415 recursively. */
10417 if (outer_op != UNKNOWN)
10418 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10419 else
10420 x = NULL_RTX;
10422 if (x == NULL_RTX)
10423 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10425 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10426 turn off all the bits that the shift would have turned off. */
10427 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10428 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10429 GET_MODE_MASK (result_mode) >> orig_count);
10431 /* Do the remainder of the processing in RESULT_MODE. */
10432 x = gen_lowpart_or_truncate (result_mode, x);
10434 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10435 operation. */
10436 if (complement_p)
10437 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10439 if (outer_op != UNKNOWN)
10441 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10442 && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10443 outer_const = trunc_int_for_mode (outer_const, result_mode);
10445 if (outer_op == AND)
10446 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10447 else if (outer_op == SET)
10449 /* This means that we have determined that the result is
10450 equivalent to a constant. This should be rare. */
10451 if (!side_effects_p (x))
10452 x = GEN_INT (outer_const);
10454 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10455 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10456 else
10457 x = simplify_gen_binary (outer_op, result_mode, x,
10458 GEN_INT (outer_const));
10461 return x;
10464 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10465 The result of the shift is RESULT_MODE. If we cannot simplify it,
10466 return X or, if it is NULL, synthesize the expression with
10467 simplify_gen_binary. Otherwise, return a simplified value.
10469 The shift is normally computed in the widest mode we find in VAROP, as
10470 long as it isn't a different number of words than RESULT_MODE. Exceptions
10471 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10473 static rtx
10474 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10475 rtx varop, int count)
10477 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10478 if (tem)
10479 return tem;
10481 if (!x)
10482 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10483 if (GET_MODE (x) != result_mode)
10484 x = gen_lowpart (result_mode, x);
10485 return x;
10489 /* Like recog, but we receive the address of a pointer to a new pattern.
10490 We try to match the rtx that the pointer points to.
10491 If that fails, we may try to modify or replace the pattern,
10492 storing the replacement into the same pointer object.
10494 Modifications include deletion or addition of CLOBBERs.
10496 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10497 the CLOBBERs are placed.
10499 The value is the final insn code from the pattern ultimately matched,
10500 or -1. */
10502 static int
10503 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10505 rtx pat = *pnewpat;
10506 int insn_code_number;
10507 int num_clobbers_to_add = 0;
10508 int i;
10509 rtx notes = 0;
10510 rtx old_notes, old_pat;
10512 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10513 we use to indicate that something didn't match. If we find such a
10514 thing, force rejection. */
10515 if (GET_CODE (pat) == PARALLEL)
10516 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10517 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10518 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10519 return -1;
10521 old_pat = PATTERN (insn);
10522 old_notes = REG_NOTES (insn);
10523 PATTERN (insn) = pat;
10524 REG_NOTES (insn) = 0;
10526 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10527 if (dump_file && (dump_flags & TDF_DETAILS))
10529 if (insn_code_number < 0)
10530 fputs ("Failed to match this instruction:\n", dump_file);
10531 else
10532 fputs ("Successfully matched this instruction:\n", dump_file);
10533 print_rtl_single (dump_file, pat);
10536 /* If it isn't, there is the possibility that we previously had an insn
10537 that clobbered some register as a side effect, but the combined
10538 insn doesn't need to do that. So try once more without the clobbers
10539 unless this represents an ASM insn. */
10541 if (insn_code_number < 0 && ! check_asm_operands (pat)
10542 && GET_CODE (pat) == PARALLEL)
10544 int pos;
10546 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10547 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10549 if (i != pos)
10550 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10551 pos++;
10554 SUBST_INT (XVECLEN (pat, 0), pos);
10556 if (pos == 1)
10557 pat = XVECEXP (pat, 0, 0);
10559 PATTERN (insn) = pat;
10560 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10561 if (dump_file && (dump_flags & TDF_DETAILS))
10563 if (insn_code_number < 0)
10564 fputs ("Failed to match this instruction:\n", dump_file);
10565 else
10566 fputs ("Successfully matched this instruction:\n", dump_file);
10567 print_rtl_single (dump_file, pat);
10570 PATTERN (insn) = old_pat;
10571 REG_NOTES (insn) = old_notes;
10573 /* Recognize all noop sets, these will be killed by followup pass. */
10574 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10575 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10577 /* If we had any clobbers to add, make a new pattern than contains
10578 them. Then check to make sure that all of them are dead. */
10579 if (num_clobbers_to_add)
10581 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10582 rtvec_alloc (GET_CODE (pat) == PARALLEL
10583 ? (XVECLEN (pat, 0)
10584 + num_clobbers_to_add)
10585 : num_clobbers_to_add + 1));
10587 if (GET_CODE (pat) == PARALLEL)
10588 for (i = 0; i < XVECLEN (pat, 0); i++)
10589 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10590 else
10591 XVECEXP (newpat, 0, 0) = pat;
10593 add_clobbers (newpat, insn_code_number);
10595 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10596 i < XVECLEN (newpat, 0); i++)
10598 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10599 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10600 return -1;
10601 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10603 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10604 notes = alloc_reg_note (REG_UNUSED,
10605 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10608 pat = newpat;
10611 *pnewpat = pat;
10612 *pnotes = notes;
10614 return insn_code_number;
10617 /* Like gen_lowpart_general but for use by combine. In combine it
10618 is not possible to create any new pseudoregs. However, it is
10619 safe to create invalid memory addresses, because combine will
10620 try to recognize them and all they will do is make the combine
10621 attempt fail.
10623 If for some reason this cannot do its job, an rtx
10624 (clobber (const_int 0)) is returned.
10625 An insn containing that will not be recognized. */
10627 static rtx
10628 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10630 enum machine_mode imode = GET_MODE (x);
10631 unsigned int osize = GET_MODE_SIZE (omode);
10632 unsigned int isize = GET_MODE_SIZE (imode);
10633 rtx result;
10635 if (omode == imode)
10636 return x;
10638 /* Return identity if this is a CONST or symbolic reference. */
10639 if (omode == Pmode
10640 && (GET_CODE (x) == CONST
10641 || GET_CODE (x) == SYMBOL_REF
10642 || GET_CODE (x) == LABEL_REF))
10643 return x;
10645 /* We can only support MODE being wider than a word if X is a
10646 constant integer or has a mode the same size. */
10647 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10648 && ! ((imode == VOIDmode
10649 && (CONST_INT_P (x)
10650 || GET_CODE (x) == CONST_DOUBLE))
10651 || isize == osize))
10652 goto fail;
10654 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10655 won't know what to do. So we will strip off the SUBREG here and
10656 process normally. */
10657 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10659 x = SUBREG_REG (x);
10661 /* For use in case we fall down into the address adjustments
10662 further below, we need to adjust the known mode and size of
10663 x; imode and isize, since we just adjusted x. */
10664 imode = GET_MODE (x);
10666 if (imode == omode)
10667 return x;
10669 isize = GET_MODE_SIZE (imode);
10672 result = gen_lowpart_common (omode, x);
10674 if (result)
10675 return result;
10677 if (MEM_P (x))
10679 int offset = 0;
10681 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10682 address. */
10683 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10684 goto fail;
10686 /* If we want to refer to something bigger than the original memref,
10687 generate a paradoxical subreg instead. That will force a reload
10688 of the original memref X. */
10689 if (isize < osize)
10690 return gen_rtx_SUBREG (omode, x, 0);
10692 if (WORDS_BIG_ENDIAN)
10693 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10695 /* Adjust the address so that the address-after-the-data is
10696 unchanged. */
10697 if (BYTES_BIG_ENDIAN)
10698 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10700 return adjust_address_nv (x, omode, offset);
10703 /* If X is a comparison operator, rewrite it in a new mode. This
10704 probably won't match, but may allow further simplifications. */
10705 else if (COMPARISON_P (x))
10706 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10708 /* If we couldn't simplify X any other way, just enclose it in a
10709 SUBREG. Normally, this SUBREG won't match, but some patterns may
10710 include an explicit SUBREG or we may simplify it further in combine. */
10711 else
10713 int offset = 0;
10714 rtx res;
10716 offset = subreg_lowpart_offset (omode, imode);
10717 if (imode == VOIDmode)
10719 imode = int_mode_for_mode (omode);
10720 x = gen_lowpart_common (imode, x);
10721 if (x == NULL)
10722 goto fail;
10724 res = simplify_gen_subreg (omode, x, imode, offset);
10725 if (res)
10726 return res;
10729 fail:
10730 return gen_rtx_CLOBBER (omode, const0_rtx);
10733 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10734 comparison code that will be tested.
10736 The result is a possibly different comparison code to use. *POP0 and
10737 *POP1 may be updated.
10739 It is possible that we might detect that a comparison is either always
10740 true or always false. However, we do not perform general constant
10741 folding in combine, so this knowledge isn't useful. Such tautologies
10742 should have been detected earlier. Hence we ignore all such cases. */
10744 static enum rtx_code
10745 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10747 rtx op0 = *pop0;
10748 rtx op1 = *pop1;
10749 rtx tem, tem1;
10750 int i;
10751 enum machine_mode mode, tmode;
10753 /* Try a few ways of applying the same transformation to both operands. */
10754 while (1)
10756 #ifndef WORD_REGISTER_OPERATIONS
10757 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10758 so check specially. */
10759 if (code != GTU && code != GEU && code != LTU && code != LEU
10760 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10761 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10762 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10763 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10764 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10765 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10766 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10767 && CONST_INT_P (XEXP (op0, 1))
10768 && XEXP (op0, 1) == XEXP (op1, 1)
10769 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10770 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10771 && (INTVAL (XEXP (op0, 1))
10772 == (GET_MODE_BITSIZE (GET_MODE (op0))
10773 - (GET_MODE_BITSIZE
10774 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10776 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10777 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10779 #endif
10781 /* If both operands are the same constant shift, see if we can ignore the
10782 shift. We can if the shift is a rotate or if the bits shifted out of
10783 this shift are known to be zero for both inputs and if the type of
10784 comparison is compatible with the shift. */
10785 if (GET_CODE (op0) == GET_CODE (op1)
10786 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10787 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10788 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10789 && (code != GT && code != LT && code != GE && code != LE))
10790 || (GET_CODE (op0) == ASHIFTRT
10791 && (code != GTU && code != LTU
10792 && code != GEU && code != LEU)))
10793 && CONST_INT_P (XEXP (op0, 1))
10794 && INTVAL (XEXP (op0, 1)) >= 0
10795 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10796 && XEXP (op0, 1) == XEXP (op1, 1))
10798 enum machine_mode mode = GET_MODE (op0);
10799 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10800 int shift_count = INTVAL (XEXP (op0, 1));
10802 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10803 mask &= (mask >> shift_count) << shift_count;
10804 else if (GET_CODE (op0) == ASHIFT)
10805 mask = (mask & (mask << shift_count)) >> shift_count;
10807 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10808 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10809 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10810 else
10811 break;
10814 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10815 SUBREGs are of the same mode, and, in both cases, the AND would
10816 be redundant if the comparison was done in the narrower mode,
10817 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10818 and the operand's possibly nonzero bits are 0xffffff01; in that case
10819 if we only care about QImode, we don't need the AND). This case
10820 occurs if the output mode of an scc insn is not SImode and
10821 STORE_FLAG_VALUE == 1 (e.g., the 386).
10823 Similarly, check for a case where the AND's are ZERO_EXTEND
10824 operations from some narrower mode even though a SUBREG is not
10825 present. */
10827 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10828 && CONST_INT_P (XEXP (op0, 1))
10829 && CONST_INT_P (XEXP (op1, 1)))
10831 rtx inner_op0 = XEXP (op0, 0);
10832 rtx inner_op1 = XEXP (op1, 0);
10833 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10834 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10835 int changed = 0;
10837 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10838 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10839 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10840 && (GET_MODE (SUBREG_REG (inner_op0))
10841 == GET_MODE (SUBREG_REG (inner_op1)))
10842 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10843 <= HOST_BITS_PER_WIDE_INT)
10844 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10845 GET_MODE (SUBREG_REG (inner_op0)))))
10846 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10847 GET_MODE (SUBREG_REG (inner_op1))))))
10849 op0 = SUBREG_REG (inner_op0);
10850 op1 = SUBREG_REG (inner_op1);
10852 /* The resulting comparison is always unsigned since we masked
10853 off the original sign bit. */
10854 code = unsigned_condition (code);
10856 changed = 1;
10859 else if (c0 == c1)
10860 for (tmode = GET_CLASS_NARROWEST_MODE
10861 (GET_MODE_CLASS (GET_MODE (op0)));
10862 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10863 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10865 op0 = gen_lowpart (tmode, inner_op0);
10866 op1 = gen_lowpart (tmode, inner_op1);
10867 code = unsigned_condition (code);
10868 changed = 1;
10869 break;
10872 if (! changed)
10873 break;
10876 /* If both operands are NOT, we can strip off the outer operation
10877 and adjust the comparison code for swapped operands; similarly for
10878 NEG, except that this must be an equality comparison. */
10879 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10880 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10881 && (code == EQ || code == NE)))
10882 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10884 else
10885 break;
10888 /* If the first operand is a constant, swap the operands and adjust the
10889 comparison code appropriately, but don't do this if the second operand
10890 is already a constant integer. */
10891 if (swap_commutative_operands_p (op0, op1))
10893 tem = op0, op0 = op1, op1 = tem;
10894 code = swap_condition (code);
10897 /* We now enter a loop during which we will try to simplify the comparison.
10898 For the most part, we only are concerned with comparisons with zero,
10899 but some things may really be comparisons with zero but not start
10900 out looking that way. */
10902 while (CONST_INT_P (op1))
10904 enum machine_mode mode = GET_MODE (op0);
10905 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10906 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10907 int equality_comparison_p;
10908 int sign_bit_comparison_p;
10909 int unsigned_comparison_p;
10910 HOST_WIDE_INT const_op;
10912 /* We only want to handle integral modes. This catches VOIDmode,
10913 CCmode, and the floating-point modes. An exception is that we
10914 can handle VOIDmode if OP0 is a COMPARE or a comparison
10915 operation. */
10917 if (GET_MODE_CLASS (mode) != MODE_INT
10918 && ! (mode == VOIDmode
10919 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10920 break;
10922 /* Get the constant we are comparing against and turn off all bits
10923 not on in our mode. */
10924 const_op = INTVAL (op1);
10925 if (mode != VOIDmode)
10926 const_op = trunc_int_for_mode (const_op, mode);
10927 op1 = GEN_INT (const_op);
10929 /* If we are comparing against a constant power of two and the value
10930 being compared can only have that single bit nonzero (e.g., it was
10931 `and'ed with that bit), we can replace this with a comparison
10932 with zero. */
10933 if (const_op
10934 && (code == EQ || code == NE || code == GE || code == GEU
10935 || code == LT || code == LTU)
10936 && mode_width <= HOST_BITS_PER_WIDE_INT
10937 && exact_log2 (const_op) >= 0
10938 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10940 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10941 op1 = const0_rtx, const_op = 0;
10944 /* Similarly, if we are comparing a value known to be either -1 or
10945 0 with -1, change it to the opposite comparison against zero. */
10947 if (const_op == -1
10948 && (code == EQ || code == NE || code == GT || code == LE
10949 || code == GEU || code == LTU)
10950 && num_sign_bit_copies (op0, mode) == mode_width)
10952 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10953 op1 = const0_rtx, const_op = 0;
10956 /* Do some canonicalizations based on the comparison code. We prefer
10957 comparisons against zero and then prefer equality comparisons.
10958 If we can reduce the size of a constant, we will do that too. */
10960 switch (code)
10962 case LT:
10963 /* < C is equivalent to <= (C - 1) */
10964 if (const_op > 0)
10966 const_op -= 1;
10967 op1 = GEN_INT (const_op);
10968 code = LE;
10969 /* ... fall through to LE case below. */
10971 else
10972 break;
10974 case LE:
10975 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10976 if (const_op < 0)
10978 const_op += 1;
10979 op1 = GEN_INT (const_op);
10980 code = LT;
10983 /* If we are doing a <= 0 comparison on a value known to have
10984 a zero sign bit, we can replace this with == 0. */
10985 else if (const_op == 0
10986 && mode_width <= HOST_BITS_PER_WIDE_INT
10987 && (nonzero_bits (op0, mode)
10988 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10989 == 0)
10990 code = EQ;
10991 break;
10993 case GE:
10994 /* >= C is equivalent to > (C - 1). */
10995 if (const_op > 0)
10997 const_op -= 1;
10998 op1 = GEN_INT (const_op);
10999 code = GT;
11000 /* ... fall through to GT below. */
11002 else
11003 break;
11005 case GT:
11006 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11007 if (const_op < 0)
11009 const_op += 1;
11010 op1 = GEN_INT (const_op);
11011 code = GE;
11014 /* If we are doing a > 0 comparison on a value known to have
11015 a zero sign bit, we can replace this with != 0. */
11016 else if (const_op == 0
11017 && mode_width <= HOST_BITS_PER_WIDE_INT
11018 && (nonzero_bits (op0, mode)
11019 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11020 == 0)
11021 code = NE;
11022 break;
11024 case LTU:
11025 /* < C is equivalent to <= (C - 1). */
11026 if (const_op > 0)
11028 const_op -= 1;
11029 op1 = GEN_INT (const_op);
11030 code = LEU;
11031 /* ... fall through ... */
11034 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11035 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11036 && (unsigned HOST_WIDE_INT) const_op
11037 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11039 const_op = 0, op1 = const0_rtx;
11040 code = GE;
11041 break;
11043 else
11044 break;
11046 case LEU:
11047 /* unsigned <= 0 is equivalent to == 0 */
11048 if (const_op == 0)
11049 code = EQ;
11051 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11052 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11053 && (unsigned HOST_WIDE_INT) const_op
11054 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11056 const_op = 0, op1 = const0_rtx;
11057 code = GE;
11059 break;
11061 case GEU:
11062 /* >= C is equivalent to > (C - 1). */
11063 if (const_op > 1)
11065 const_op -= 1;
11066 op1 = GEN_INT (const_op);
11067 code = GTU;
11068 /* ... fall through ... */
11071 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11072 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11073 && (unsigned HOST_WIDE_INT) const_op
11074 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11076 const_op = 0, op1 = const0_rtx;
11077 code = LT;
11078 break;
11080 else
11081 break;
11083 case GTU:
11084 /* unsigned > 0 is equivalent to != 0 */
11085 if (const_op == 0)
11086 code = NE;
11088 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11089 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11090 && (unsigned HOST_WIDE_INT) const_op
11091 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11093 const_op = 0, op1 = const0_rtx;
11094 code = LT;
11096 break;
11098 default:
11099 break;
11102 /* Compute some predicates to simplify code below. */
11104 equality_comparison_p = (code == EQ || code == NE);
11105 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11106 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11107 || code == GEU);
11109 /* If this is a sign bit comparison and we can do arithmetic in
11110 MODE, say that we will only be needing the sign bit of OP0. */
11111 if (sign_bit_comparison_p
11112 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11113 op0 = force_to_mode (op0, mode,
11114 (unsigned HOST_WIDE_INT) 1
11115 << (GET_MODE_BITSIZE (mode) - 1),
11118 /* Now try cases based on the opcode of OP0. If none of the cases
11119 does a "continue", we exit this loop immediately after the
11120 switch. */
11122 switch (GET_CODE (op0))
11124 case ZERO_EXTRACT:
11125 /* If we are extracting a single bit from a variable position in
11126 a constant that has only a single bit set and are comparing it
11127 with zero, we can convert this into an equality comparison
11128 between the position and the location of the single bit. */
11129 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11130 have already reduced the shift count modulo the word size. */
11131 if (!SHIFT_COUNT_TRUNCATED
11132 && CONST_INT_P (XEXP (op0, 0))
11133 && XEXP (op0, 1) == const1_rtx
11134 && equality_comparison_p && const_op == 0
11135 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11137 if (BITS_BIG_ENDIAN)
11139 enum machine_mode new_mode
11140 = mode_for_extraction (EP_extzv, 1);
11141 if (new_mode == MAX_MACHINE_MODE)
11142 i = BITS_PER_WORD - 1 - i;
11143 else
11145 mode = new_mode;
11146 i = (GET_MODE_BITSIZE (mode) - 1 - i);
11150 op0 = XEXP (op0, 2);
11151 op1 = GEN_INT (i);
11152 const_op = i;
11154 /* Result is nonzero iff shift count is equal to I. */
11155 code = reverse_condition (code);
11156 continue;
11159 /* ... fall through ... */
11161 case SIGN_EXTRACT:
11162 tem = expand_compound_operation (op0);
11163 if (tem != op0)
11165 op0 = tem;
11166 continue;
11168 break;
11170 case NOT:
11171 /* If testing for equality, we can take the NOT of the constant. */
11172 if (equality_comparison_p
11173 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11175 op0 = XEXP (op0, 0);
11176 op1 = tem;
11177 continue;
11180 /* If just looking at the sign bit, reverse the sense of the
11181 comparison. */
11182 if (sign_bit_comparison_p)
11184 op0 = XEXP (op0, 0);
11185 code = (code == GE ? LT : GE);
11186 continue;
11188 break;
11190 case NEG:
11191 /* If testing for equality, we can take the NEG of the constant. */
11192 if (equality_comparison_p
11193 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11195 op0 = XEXP (op0, 0);
11196 op1 = tem;
11197 continue;
11200 /* The remaining cases only apply to comparisons with zero. */
11201 if (const_op != 0)
11202 break;
11204 /* When X is ABS or is known positive,
11205 (neg X) is < 0 if and only if X != 0. */
11207 if (sign_bit_comparison_p
11208 && (GET_CODE (XEXP (op0, 0)) == ABS
11209 || (mode_width <= HOST_BITS_PER_WIDE_INT
11210 && (nonzero_bits (XEXP (op0, 0), mode)
11211 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11212 == 0)))
11214 op0 = XEXP (op0, 0);
11215 code = (code == LT ? NE : EQ);
11216 continue;
11219 /* If we have NEG of something whose two high-order bits are the
11220 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11221 if (num_sign_bit_copies (op0, mode) >= 2)
11223 op0 = XEXP (op0, 0);
11224 code = swap_condition (code);
11225 continue;
11227 break;
11229 case ROTATE:
11230 /* If we are testing equality and our count is a constant, we
11231 can perform the inverse operation on our RHS. */
11232 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11233 && (tem = simplify_binary_operation (ROTATERT, mode,
11234 op1, XEXP (op0, 1))) != 0)
11236 op0 = XEXP (op0, 0);
11237 op1 = tem;
11238 continue;
11241 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11242 a particular bit. Convert it to an AND of a constant of that
11243 bit. This will be converted into a ZERO_EXTRACT. */
11244 if (const_op == 0 && sign_bit_comparison_p
11245 && CONST_INT_P (XEXP (op0, 1))
11246 && mode_width <= HOST_BITS_PER_WIDE_INT)
11248 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11249 ((unsigned HOST_WIDE_INT) 1
11250 << (mode_width - 1
11251 - INTVAL (XEXP (op0, 1)))));
11252 code = (code == LT ? NE : EQ);
11253 continue;
11256 /* Fall through. */
11258 case ABS:
11259 /* ABS is ignorable inside an equality comparison with zero. */
11260 if (const_op == 0 && equality_comparison_p)
11262 op0 = XEXP (op0, 0);
11263 continue;
11265 break;
11267 case SIGN_EXTEND:
11268 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11269 (compare FOO CONST) if CONST fits in FOO's mode and we
11270 are either testing inequality or have an unsigned
11271 comparison with ZERO_EXTEND or a signed comparison with
11272 SIGN_EXTEND. But don't do it if we don't have a compare
11273 insn of the given mode, since we'd have to revert it
11274 later on, and then we wouldn't know whether to sign- or
11275 zero-extend. */
11276 mode = GET_MODE (XEXP (op0, 0));
11277 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11278 && ! unsigned_comparison_p
11279 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11280 && ((unsigned HOST_WIDE_INT) const_op
11281 < (((unsigned HOST_WIDE_INT) 1
11282 << (GET_MODE_BITSIZE (mode) - 1))))
11283 && have_insn_for (COMPARE, mode))
11285 op0 = XEXP (op0, 0);
11286 continue;
11288 break;
11290 case SUBREG:
11291 /* Check for the case where we are comparing A - C1 with C2, that is
11293 (subreg:MODE (plus (A) (-C1))) op (C2)
11295 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11296 comparison in the wider mode. One of the following two conditions
11297 must be true in order for this to be valid:
11299 1. The mode extension results in the same bit pattern being added
11300 on both sides and the comparison is equality or unsigned. As
11301 C2 has been truncated to fit in MODE, the pattern can only be
11302 all 0s or all 1s.
11304 2. The mode extension results in the sign bit being copied on
11305 each side.
11307 The difficulty here is that we have predicates for A but not for
11308 (A - C1) so we need to check that C1 is within proper bounds so
11309 as to perturbate A as little as possible. */
11311 if (mode_width <= HOST_BITS_PER_WIDE_INT
11312 && subreg_lowpart_p (op0)
11313 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
11314 && GET_CODE (SUBREG_REG (op0)) == PLUS
11315 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11317 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11318 rtx a = XEXP (SUBREG_REG (op0), 0);
11319 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11321 if ((c1 > 0
11322 && (unsigned HOST_WIDE_INT) c1
11323 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11324 && (equality_comparison_p || unsigned_comparison_p)
11325 /* (A - C1) zero-extends if it is positive and sign-extends
11326 if it is negative, C2 both zero- and sign-extends. */
11327 && ((0 == (nonzero_bits (a, inner_mode)
11328 & ~GET_MODE_MASK (mode))
11329 && const_op >= 0)
11330 /* (A - C1) sign-extends if it is positive and 1-extends
11331 if it is negative, C2 both sign- and 1-extends. */
11332 || (num_sign_bit_copies (a, inner_mode)
11333 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11334 - mode_width)
11335 && const_op < 0)))
11336 || ((unsigned HOST_WIDE_INT) c1
11337 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11338 /* (A - C1) always sign-extends, like C2. */
11339 && num_sign_bit_copies (a, inner_mode)
11340 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11341 - (mode_width - 1))))
11343 op0 = SUBREG_REG (op0);
11344 continue;
11348 /* If the inner mode is narrower and we are extracting the low part,
11349 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11350 if (subreg_lowpart_p (op0)
11351 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
11352 /* Fall through */ ;
11353 else
11354 break;
11356 /* ... fall through ... */
11358 case ZERO_EXTEND:
11359 mode = GET_MODE (XEXP (op0, 0));
11360 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11361 && (unsigned_comparison_p || equality_comparison_p)
11362 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11363 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
11364 && have_insn_for (COMPARE, mode))
11366 op0 = XEXP (op0, 0);
11367 continue;
11369 break;
11371 case PLUS:
11372 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11373 this for equality comparisons due to pathological cases involving
11374 overflows. */
11375 if (equality_comparison_p
11376 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11377 op1, XEXP (op0, 1))))
11379 op0 = XEXP (op0, 0);
11380 op1 = tem;
11381 continue;
11384 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11385 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11386 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11388 op0 = XEXP (XEXP (op0, 0), 0);
11389 code = (code == LT ? EQ : NE);
11390 continue;
11392 break;
11394 case MINUS:
11395 /* We used to optimize signed comparisons against zero, but that
11396 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11397 arrive here as equality comparisons, or (GEU, LTU) are
11398 optimized away. No need to special-case them. */
11400 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11401 (eq B (minus A C)), whichever simplifies. We can only do
11402 this for equality comparisons due to pathological cases involving
11403 overflows. */
11404 if (equality_comparison_p
11405 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11406 XEXP (op0, 1), op1)))
11408 op0 = XEXP (op0, 0);
11409 op1 = tem;
11410 continue;
11413 if (equality_comparison_p
11414 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11415 XEXP (op0, 0), op1)))
11417 op0 = XEXP (op0, 1);
11418 op1 = tem;
11419 continue;
11422 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11423 of bits in X minus 1, is one iff X > 0. */
11424 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11425 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11426 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11427 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11429 op0 = XEXP (op0, 1);
11430 code = (code == GE ? LE : GT);
11431 continue;
11433 break;
11435 case XOR:
11436 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11437 if C is zero or B is a constant. */
11438 if (equality_comparison_p
11439 && 0 != (tem = simplify_binary_operation (XOR, mode,
11440 XEXP (op0, 1), op1)))
11442 op0 = XEXP (op0, 0);
11443 op1 = tem;
11444 continue;
11446 break;
11448 case EQ: case NE:
11449 case UNEQ: case LTGT:
11450 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11451 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11452 case UNORDERED: case ORDERED:
11453 /* We can't do anything if OP0 is a condition code value, rather
11454 than an actual data value. */
11455 if (const_op != 0
11456 || CC0_P (XEXP (op0, 0))
11457 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11458 break;
11460 /* Get the two operands being compared. */
11461 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11462 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11463 else
11464 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11466 /* Check for the cases where we simply want the result of the
11467 earlier test or the opposite of that result. */
11468 if (code == NE || code == EQ
11469 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11470 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11471 && (STORE_FLAG_VALUE
11472 & (((unsigned HOST_WIDE_INT) 1
11473 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11474 && (code == LT || code == GE)))
11476 enum rtx_code new_code;
11477 if (code == LT || code == NE)
11478 new_code = GET_CODE (op0);
11479 else
11480 new_code = reversed_comparison_code (op0, NULL);
11482 if (new_code != UNKNOWN)
11484 code = new_code;
11485 op0 = tem;
11486 op1 = tem1;
11487 continue;
11490 break;
11492 case IOR:
11493 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11494 iff X <= 0. */
11495 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11496 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11497 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11499 op0 = XEXP (op0, 1);
11500 code = (code == GE ? GT : LE);
11501 continue;
11503 break;
11505 case AND:
11506 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11507 will be converted to a ZERO_EXTRACT later. */
11508 if (const_op == 0 && equality_comparison_p
11509 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11510 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11512 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11513 XEXP (XEXP (op0, 0), 1));
11514 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11515 continue;
11518 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11519 zero and X is a comparison and C1 and C2 describe only bits set
11520 in STORE_FLAG_VALUE, we can compare with X. */
11521 if (const_op == 0 && equality_comparison_p
11522 && mode_width <= HOST_BITS_PER_WIDE_INT
11523 && CONST_INT_P (XEXP (op0, 1))
11524 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11525 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11526 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11527 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11529 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11530 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11531 if ((~STORE_FLAG_VALUE & mask) == 0
11532 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11533 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11534 && COMPARISON_P (tem))))
11536 op0 = XEXP (XEXP (op0, 0), 0);
11537 continue;
11541 /* If we are doing an equality comparison of an AND of a bit equal
11542 to the sign bit, replace this with a LT or GE comparison of
11543 the underlying value. */
11544 if (equality_comparison_p
11545 && const_op == 0
11546 && CONST_INT_P (XEXP (op0, 1))
11547 && mode_width <= HOST_BITS_PER_WIDE_INT
11548 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11549 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11551 op0 = XEXP (op0, 0);
11552 code = (code == EQ ? GE : LT);
11553 continue;
11556 /* If this AND operation is really a ZERO_EXTEND from a narrower
11557 mode, the constant fits within that mode, and this is either an
11558 equality or unsigned comparison, try to do this comparison in
11559 the narrower mode.
11561 Note that in:
11563 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11564 -> (ne:DI (reg:SI 4) (const_int 0))
11566 unless TRULY_NOOP_TRUNCATION allows it or the register is
11567 known to hold a value of the required mode the
11568 transformation is invalid. */
11569 if ((equality_comparison_p || unsigned_comparison_p)
11570 && CONST_INT_P (XEXP (op0, 1))
11571 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11572 & GET_MODE_MASK (mode))
11573 + 1)) >= 0
11574 && const_op >> i == 0
11575 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11576 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11577 GET_MODE_BITSIZE (GET_MODE (op0)))
11578 || (REG_P (XEXP (op0, 0))
11579 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11581 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11582 continue;
11585 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11586 fits in both M1 and M2 and the SUBREG is either paradoxical
11587 or represents the low part, permute the SUBREG and the AND
11588 and try again. */
11589 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11591 unsigned HOST_WIDE_INT c1;
11592 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11593 /* Require an integral mode, to avoid creating something like
11594 (AND:SF ...). */
11595 if (SCALAR_INT_MODE_P (tmode)
11596 /* It is unsafe to commute the AND into the SUBREG if the
11597 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11598 not defined. As originally written the upper bits
11599 have a defined value due to the AND operation.
11600 However, if we commute the AND inside the SUBREG then
11601 they no longer have defined values and the meaning of
11602 the code has been changed. */
11603 && (0
11604 #ifdef WORD_REGISTER_OPERATIONS
11605 || (mode_width > GET_MODE_BITSIZE (tmode)
11606 && mode_width <= BITS_PER_WORD)
11607 #endif
11608 || (mode_width <= GET_MODE_BITSIZE (tmode)
11609 && subreg_lowpart_p (XEXP (op0, 0))))
11610 && CONST_INT_P (XEXP (op0, 1))
11611 && mode_width <= HOST_BITS_PER_WIDE_INT
11612 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11613 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11614 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11615 && c1 != mask
11616 && c1 != GET_MODE_MASK (tmode))
11618 op0 = simplify_gen_binary (AND, tmode,
11619 SUBREG_REG (XEXP (op0, 0)),
11620 gen_int_mode (c1, tmode));
11621 op0 = gen_lowpart (mode, op0);
11622 continue;
11626 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11627 if (const_op == 0 && equality_comparison_p
11628 && XEXP (op0, 1) == const1_rtx
11629 && GET_CODE (XEXP (op0, 0)) == NOT)
11631 op0 = simplify_and_const_int (NULL_RTX, mode,
11632 XEXP (XEXP (op0, 0), 0), 1);
11633 code = (code == NE ? EQ : NE);
11634 continue;
11637 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11638 (eq (and (lshiftrt X) 1) 0).
11639 Also handle the case where (not X) is expressed using xor. */
11640 if (const_op == 0 && equality_comparison_p
11641 && XEXP (op0, 1) == const1_rtx
11642 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11644 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11645 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11647 if (GET_CODE (shift_op) == NOT
11648 || (GET_CODE (shift_op) == XOR
11649 && CONST_INT_P (XEXP (shift_op, 1))
11650 && CONST_INT_P (shift_count)
11651 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11652 && (UINTVAL (XEXP (shift_op, 1))
11653 == (unsigned HOST_WIDE_INT) 1
11654 << INTVAL (shift_count))))
11657 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11658 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11659 code = (code == NE ? EQ : NE);
11660 continue;
11663 break;
11665 case ASHIFT:
11666 /* If we have (compare (ashift FOO N) (const_int C)) and
11667 the high order N bits of FOO (N+1 if an inequality comparison)
11668 are known to be zero, we can do this by comparing FOO with C
11669 shifted right N bits so long as the low-order N bits of C are
11670 zero. */
11671 if (CONST_INT_P (XEXP (op0, 1))
11672 && INTVAL (XEXP (op0, 1)) >= 0
11673 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11674 < HOST_BITS_PER_WIDE_INT)
11675 && (((unsigned HOST_WIDE_INT) const_op
11676 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11677 - 1)) == 0)
11678 && mode_width <= HOST_BITS_PER_WIDE_INT
11679 && (nonzero_bits (XEXP (op0, 0), mode)
11680 & ~(mask >> (INTVAL (XEXP (op0, 1))
11681 + ! equality_comparison_p))) == 0)
11683 /* We must perform a logical shift, not an arithmetic one,
11684 as we want the top N bits of C to be zero. */
11685 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11687 temp >>= INTVAL (XEXP (op0, 1));
11688 op1 = gen_int_mode (temp, mode);
11689 op0 = XEXP (op0, 0);
11690 continue;
11693 /* If we are doing a sign bit comparison, it means we are testing
11694 a particular bit. Convert it to the appropriate AND. */
11695 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11696 && mode_width <= HOST_BITS_PER_WIDE_INT)
11698 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11699 ((unsigned HOST_WIDE_INT) 1
11700 << (mode_width - 1
11701 - INTVAL (XEXP (op0, 1)))));
11702 code = (code == LT ? NE : EQ);
11703 continue;
11706 /* If this an equality comparison with zero and we are shifting
11707 the low bit to the sign bit, we can convert this to an AND of the
11708 low-order bit. */
11709 if (const_op == 0 && equality_comparison_p
11710 && CONST_INT_P (XEXP (op0, 1))
11711 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11713 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11714 continue;
11716 break;
11718 case ASHIFTRT:
11719 /* If this is an equality comparison with zero, we can do this
11720 as a logical shift, which might be much simpler. */
11721 if (equality_comparison_p && const_op == 0
11722 && CONST_INT_P (XEXP (op0, 1)))
11724 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11725 XEXP (op0, 0),
11726 INTVAL (XEXP (op0, 1)));
11727 continue;
11730 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11731 do the comparison in a narrower mode. */
11732 if (! unsigned_comparison_p
11733 && CONST_INT_P (XEXP (op0, 1))
11734 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11735 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11736 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11737 MODE_INT, 1)) != BLKmode
11738 && (((unsigned HOST_WIDE_INT) const_op
11739 + (GET_MODE_MASK (tmode) >> 1) + 1)
11740 <= GET_MODE_MASK (tmode)))
11742 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11743 continue;
11746 /* Likewise if OP0 is a PLUS of a sign extension with a
11747 constant, which is usually represented with the PLUS
11748 between the shifts. */
11749 if (! unsigned_comparison_p
11750 && CONST_INT_P (XEXP (op0, 1))
11751 && GET_CODE (XEXP (op0, 0)) == PLUS
11752 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11753 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11754 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11755 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11756 MODE_INT, 1)) != BLKmode
11757 && (((unsigned HOST_WIDE_INT) const_op
11758 + (GET_MODE_MASK (tmode) >> 1) + 1)
11759 <= GET_MODE_MASK (tmode)))
11761 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11762 rtx add_const = XEXP (XEXP (op0, 0), 1);
11763 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11764 add_const, XEXP (op0, 1));
11766 op0 = simplify_gen_binary (PLUS, tmode,
11767 gen_lowpart (tmode, inner),
11768 new_const);
11769 continue;
11772 /* ... fall through ... */
11773 case LSHIFTRT:
11774 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11775 the low order N bits of FOO are known to be zero, we can do this
11776 by comparing FOO with C shifted left N bits so long as no
11777 overflow occurs. Even if the low order N bits of FOO aren't known
11778 to be zero, if the comparison is >= or < we can use the same
11779 optimization and for > or <= by setting all the low
11780 order N bits in the comparison constant. */
11781 if (CONST_INT_P (XEXP (op0, 1))
11782 && INTVAL (XEXP (op0, 1)) > 0
11783 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11784 && mode_width <= HOST_BITS_PER_WIDE_INT
11785 && (((unsigned HOST_WIDE_INT) const_op
11786 + (GET_CODE (op0) != LSHIFTRT
11787 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11788 + 1)
11789 : 0))
11790 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11792 unsigned HOST_WIDE_INT low_bits
11793 = (nonzero_bits (XEXP (op0, 0), mode)
11794 & (((unsigned HOST_WIDE_INT) 1
11795 << INTVAL (XEXP (op0, 1))) - 1));
11796 if (low_bits == 0 || !equality_comparison_p)
11798 /* If the shift was logical, then we must make the condition
11799 unsigned. */
11800 if (GET_CODE (op0) == LSHIFTRT)
11801 code = unsigned_condition (code);
11803 const_op <<= INTVAL (XEXP (op0, 1));
11804 if (low_bits != 0
11805 && (code == GT || code == GTU
11806 || code == LE || code == LEU))
11807 const_op
11808 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11809 op1 = GEN_INT (const_op);
11810 op0 = XEXP (op0, 0);
11811 continue;
11815 /* If we are using this shift to extract just the sign bit, we
11816 can replace this with an LT or GE comparison. */
11817 if (const_op == 0
11818 && (equality_comparison_p || sign_bit_comparison_p)
11819 && CONST_INT_P (XEXP (op0, 1))
11820 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11822 op0 = XEXP (op0, 0);
11823 code = (code == NE || code == GT ? LT : GE);
11824 continue;
11826 break;
11828 default:
11829 break;
11832 break;
11835 /* Now make any compound operations involved in this comparison. Then,
11836 check for an outmost SUBREG on OP0 that is not doing anything or is
11837 paradoxical. The latter transformation must only be performed when
11838 it is known that the "extra" bits will be the same in op0 and op1 or
11839 that they don't matter. There are three cases to consider:
11841 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11842 care bits and we can assume they have any convenient value. So
11843 making the transformation is safe.
11845 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11846 In this case the upper bits of op0 are undefined. We should not make
11847 the simplification in that case as we do not know the contents of
11848 those bits.
11850 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11851 UNKNOWN. In that case we know those bits are zeros or ones. We must
11852 also be sure that they are the same as the upper bits of op1.
11854 We can never remove a SUBREG for a non-equality comparison because
11855 the sign bit is in a different place in the underlying object. */
11857 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11858 op1 = make_compound_operation (op1, SET);
11860 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11861 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11862 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11863 && (code == NE || code == EQ))
11865 if (GET_MODE_SIZE (GET_MODE (op0))
11866 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11868 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11869 implemented. */
11870 if (REG_P (SUBREG_REG (op0)))
11872 op0 = SUBREG_REG (op0);
11873 op1 = gen_lowpart (GET_MODE (op0), op1);
11876 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11877 <= HOST_BITS_PER_WIDE_INT)
11878 && (nonzero_bits (SUBREG_REG (op0),
11879 GET_MODE (SUBREG_REG (op0)))
11880 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11882 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11884 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11885 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11886 op0 = SUBREG_REG (op0), op1 = tem;
11890 /* We now do the opposite procedure: Some machines don't have compare
11891 insns in all modes. If OP0's mode is an integer mode smaller than a
11892 word and we can't do a compare in that mode, see if there is a larger
11893 mode for which we can do the compare. There are a number of cases in
11894 which we can use the wider mode. */
11896 mode = GET_MODE (op0);
11897 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11898 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11899 && ! have_insn_for (COMPARE, mode))
11900 for (tmode = GET_MODE_WIDER_MODE (mode);
11901 (tmode != VOIDmode
11902 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11903 tmode = GET_MODE_WIDER_MODE (tmode))
11904 if (have_insn_for (COMPARE, tmode))
11906 int zero_extended;
11908 /* If this is a test for negative, we can make an explicit
11909 test of the sign bit. Test this first so we can use
11910 a paradoxical subreg to extend OP0. */
11912 if (op1 == const0_rtx && (code == LT || code == GE)
11913 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11915 op0 = simplify_gen_binary (AND, tmode,
11916 gen_lowpart (tmode, op0),
11917 GEN_INT ((unsigned HOST_WIDE_INT) 1
11918 << (GET_MODE_BITSIZE (mode)
11919 - 1)));
11920 code = (code == LT) ? NE : EQ;
11921 break;
11924 /* If the only nonzero bits in OP0 and OP1 are those in the
11925 narrower mode and this is an equality or unsigned comparison,
11926 we can use the wider mode. Similarly for sign-extended
11927 values, in which case it is true for all comparisons. */
11928 zero_extended = ((code == EQ || code == NE
11929 || code == GEU || code == GTU
11930 || code == LEU || code == LTU)
11931 && (nonzero_bits (op0, tmode)
11932 & ~GET_MODE_MASK (mode)) == 0
11933 && ((CONST_INT_P (op1)
11934 || (nonzero_bits (op1, tmode)
11935 & ~GET_MODE_MASK (mode)) == 0)));
11937 if (zero_extended
11938 || ((num_sign_bit_copies (op0, tmode)
11939 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11940 - GET_MODE_BITSIZE (mode)))
11941 && (num_sign_bit_copies (op1, tmode)
11942 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11943 - GET_MODE_BITSIZE (mode)))))
11945 /* If OP0 is an AND and we don't have an AND in MODE either,
11946 make a new AND in the proper mode. */
11947 if (GET_CODE (op0) == AND
11948 && !have_insn_for (AND, mode))
11949 op0 = simplify_gen_binary (AND, tmode,
11950 gen_lowpart (tmode,
11951 XEXP (op0, 0)),
11952 gen_lowpart (tmode,
11953 XEXP (op0, 1)));
11954 else
11956 if (zero_extended)
11958 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11959 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11961 else
11963 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11964 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11966 break;
11971 #ifdef CANONICALIZE_COMPARISON
11972 /* If this machine only supports a subset of valid comparisons, see if we
11973 can convert an unsupported one into a supported one. */
11974 CANONICALIZE_COMPARISON (code, op0, op1);
11975 #endif
11977 *pop0 = op0;
11978 *pop1 = op1;
11980 return code;
11983 /* Utility function for record_value_for_reg. Count number of
11984 rtxs in X. */
11985 static int
11986 count_rtxs (rtx x)
11988 enum rtx_code code = GET_CODE (x);
11989 const char *fmt;
11990 int i, j, ret = 1;
11992 if (GET_RTX_CLASS (code) == '2'
11993 || GET_RTX_CLASS (code) == 'c')
11995 rtx x0 = XEXP (x, 0);
11996 rtx x1 = XEXP (x, 1);
11998 if (x0 == x1)
11999 return 1 + 2 * count_rtxs (x0);
12001 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
12002 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
12003 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12004 return 2 + 2 * count_rtxs (x0)
12005 + count_rtxs (x == XEXP (x1, 0)
12006 ? XEXP (x1, 1) : XEXP (x1, 0));
12008 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
12009 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
12010 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12011 return 2 + 2 * count_rtxs (x1)
12012 + count_rtxs (x == XEXP (x0, 0)
12013 ? XEXP (x0, 1) : XEXP (x0, 0));
12016 fmt = GET_RTX_FORMAT (code);
12017 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12018 if (fmt[i] == 'e')
12019 ret += count_rtxs (XEXP (x, i));
12020 else if (fmt[i] == 'E')
12021 for (j = 0; j < XVECLEN (x, i); j++)
12022 ret += count_rtxs (XVECEXP (x, i, j));
12024 return ret;
12027 /* Utility function for following routine. Called when X is part of a value
12028 being stored into last_set_value. Sets last_set_table_tick
12029 for each register mentioned. Similar to mention_regs in cse.c */
12031 static void
12032 update_table_tick (rtx x)
12034 enum rtx_code code = GET_CODE (x);
12035 const char *fmt = GET_RTX_FORMAT (code);
12036 int i, j;
12038 if (code == REG)
12040 unsigned int regno = REGNO (x);
12041 unsigned int endregno = END_REGNO (x);
12042 unsigned int r;
12044 for (r = regno; r < endregno; r++)
12046 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
12047 rsp->last_set_table_tick = label_tick;
12050 return;
12053 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12054 if (fmt[i] == 'e')
12056 /* Check for identical subexpressions. If x contains
12057 identical subexpression we only have to traverse one of
12058 them. */
12059 if (i == 0 && ARITHMETIC_P (x))
12061 /* Note that at this point x1 has already been
12062 processed. */
12063 rtx x0 = XEXP (x, 0);
12064 rtx x1 = XEXP (x, 1);
12066 /* If x0 and x1 are identical then there is no need to
12067 process x0. */
12068 if (x0 == x1)
12069 break;
12071 /* If x0 is identical to a subexpression of x1 then while
12072 processing x1, x0 has already been processed. Thus we
12073 are done with x. */
12074 if (ARITHMETIC_P (x1)
12075 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12076 break;
12078 /* If x1 is identical to a subexpression of x0 then we
12079 still have to process the rest of x0. */
12080 if (ARITHMETIC_P (x0)
12081 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12083 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12084 break;
12088 update_table_tick (XEXP (x, i));
12090 else if (fmt[i] == 'E')
12091 for (j = 0; j < XVECLEN (x, i); j++)
12092 update_table_tick (XVECEXP (x, i, j));
12095 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12096 are saying that the register is clobbered and we no longer know its
12097 value. If INSN is zero, don't update reg_stat[].last_set; this is
12098 only permitted with VALUE also zero and is used to invalidate the
12099 register. */
12101 static void
12102 record_value_for_reg (rtx reg, rtx insn, rtx value)
12104 unsigned int regno = REGNO (reg);
12105 unsigned int endregno = END_REGNO (reg);
12106 unsigned int i;
12107 reg_stat_type *rsp;
12109 /* If VALUE contains REG and we have a previous value for REG, substitute
12110 the previous value. */
12111 if (value && insn && reg_overlap_mentioned_p (reg, value))
12113 rtx tem;
12115 /* Set things up so get_last_value is allowed to see anything set up to
12116 our insn. */
12117 subst_low_luid = DF_INSN_LUID (insn);
12118 tem = get_last_value (reg);
12120 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12121 it isn't going to be useful and will take a lot of time to process,
12122 so just use the CLOBBER. */
12124 if (tem)
12126 if (ARITHMETIC_P (tem)
12127 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12128 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12129 tem = XEXP (tem, 0);
12130 else if (count_occurrences (value, reg, 1) >= 2)
12132 /* If there are two or more occurrences of REG in VALUE,
12133 prevent the value from growing too much. */
12134 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12135 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12138 value = replace_rtx (copy_rtx (value), reg, tem);
12142 /* For each register modified, show we don't know its value, that
12143 we don't know about its bitwise content, that its value has been
12144 updated, and that we don't know the location of the death of the
12145 register. */
12146 for (i = regno; i < endregno; i++)
12148 rsp = VEC_index (reg_stat_type, reg_stat, i);
12150 if (insn)
12151 rsp->last_set = insn;
12153 rsp->last_set_value = 0;
12154 rsp->last_set_mode = VOIDmode;
12155 rsp->last_set_nonzero_bits = 0;
12156 rsp->last_set_sign_bit_copies = 0;
12157 rsp->last_death = 0;
12158 rsp->truncated_to_mode = VOIDmode;
12161 /* Mark registers that are being referenced in this value. */
12162 if (value)
12163 update_table_tick (value);
12165 /* Now update the status of each register being set.
12166 If someone is using this register in this block, set this register
12167 to invalid since we will get confused between the two lives in this
12168 basic block. This makes using this register always invalid. In cse, we
12169 scan the table to invalidate all entries using this register, but this
12170 is too much work for us. */
12172 for (i = regno; i < endregno; i++)
12174 rsp = VEC_index (reg_stat_type, reg_stat, i);
12175 rsp->last_set_label = label_tick;
12176 if (!insn
12177 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12178 rsp->last_set_invalid = 1;
12179 else
12180 rsp->last_set_invalid = 0;
12183 /* The value being assigned might refer to X (like in "x++;"). In that
12184 case, we must replace it with (clobber (const_int 0)) to prevent
12185 infinite loops. */
12186 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12187 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12189 value = copy_rtx (value);
12190 if (!get_last_value_validate (&value, insn, label_tick, 1))
12191 value = 0;
12194 /* For the main register being modified, update the value, the mode, the
12195 nonzero bits, and the number of sign bit copies. */
12197 rsp->last_set_value = value;
12199 if (value)
12201 enum machine_mode mode = GET_MODE (reg);
12202 subst_low_luid = DF_INSN_LUID (insn);
12203 rsp->last_set_mode = mode;
12204 if (GET_MODE_CLASS (mode) == MODE_INT
12205 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12206 mode = nonzero_bits_mode;
12207 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12208 rsp->last_set_sign_bit_copies
12209 = num_sign_bit_copies (value, GET_MODE (reg));
12213 /* Called via note_stores from record_dead_and_set_regs to handle one
12214 SET or CLOBBER in an insn. DATA is the instruction in which the
12215 set is occurring. */
12217 static void
12218 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12220 rtx record_dead_insn = (rtx) data;
12222 if (GET_CODE (dest) == SUBREG)
12223 dest = SUBREG_REG (dest);
12225 if (!record_dead_insn)
12227 if (REG_P (dest))
12228 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12229 return;
12232 if (REG_P (dest))
12234 /* If we are setting the whole register, we know its value. Otherwise
12235 show that we don't know the value. We can handle SUBREG in
12236 some cases. */
12237 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12238 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12239 else if (GET_CODE (setter) == SET
12240 && GET_CODE (SET_DEST (setter)) == SUBREG
12241 && SUBREG_REG (SET_DEST (setter)) == dest
12242 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
12243 && subreg_lowpart_p (SET_DEST (setter)))
12244 record_value_for_reg (dest, record_dead_insn,
12245 gen_lowpart (GET_MODE (dest),
12246 SET_SRC (setter)));
12247 else
12248 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12250 else if (MEM_P (dest)
12251 /* Ignore pushes, they clobber nothing. */
12252 && ! push_operand (dest, GET_MODE (dest)))
12253 mem_last_set = DF_INSN_LUID (record_dead_insn);
12256 /* Update the records of when each REG was most recently set or killed
12257 for the things done by INSN. This is the last thing done in processing
12258 INSN in the combiner loop.
12260 We update reg_stat[], in particular fields last_set, last_set_value,
12261 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12262 last_death, and also the similar information mem_last_set (which insn
12263 most recently modified memory) and last_call_luid (which insn was the
12264 most recent subroutine call). */
12266 static void
12267 record_dead_and_set_regs (rtx insn)
12269 rtx link;
12270 unsigned int i;
12272 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12274 if (REG_NOTE_KIND (link) == REG_DEAD
12275 && REG_P (XEXP (link, 0)))
12277 unsigned int regno = REGNO (XEXP (link, 0));
12278 unsigned int endregno = END_REGNO (XEXP (link, 0));
12280 for (i = regno; i < endregno; i++)
12282 reg_stat_type *rsp;
12284 rsp = VEC_index (reg_stat_type, reg_stat, i);
12285 rsp->last_death = insn;
12288 else if (REG_NOTE_KIND (link) == REG_INC)
12289 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12292 if (CALL_P (insn))
12294 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12295 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12297 reg_stat_type *rsp;
12299 rsp = VEC_index (reg_stat_type, reg_stat, i);
12300 rsp->last_set_invalid = 1;
12301 rsp->last_set = insn;
12302 rsp->last_set_value = 0;
12303 rsp->last_set_mode = VOIDmode;
12304 rsp->last_set_nonzero_bits = 0;
12305 rsp->last_set_sign_bit_copies = 0;
12306 rsp->last_death = 0;
12307 rsp->truncated_to_mode = VOIDmode;
12310 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12312 /* We can't combine into a call pattern. Remember, though, that
12313 the return value register is set at this LUID. We could
12314 still replace a register with the return value from the
12315 wrong subroutine call! */
12316 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12318 else
12319 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12322 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12323 register present in the SUBREG, so for each such SUBREG go back and
12324 adjust nonzero and sign bit information of the registers that are
12325 known to have some zero/sign bits set.
12327 This is needed because when combine blows the SUBREGs away, the
12328 information on zero/sign bits is lost and further combines can be
12329 missed because of that. */
12331 static void
12332 record_promoted_value (rtx insn, rtx subreg)
12334 rtx links, set;
12335 unsigned int regno = REGNO (SUBREG_REG (subreg));
12336 enum machine_mode mode = GET_MODE (subreg);
12338 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
12339 return;
12341 for (links = LOG_LINKS (insn); links;)
12343 reg_stat_type *rsp;
12345 insn = XEXP (links, 0);
12346 set = single_set (insn);
12348 if (! set || !REG_P (SET_DEST (set))
12349 || REGNO (SET_DEST (set)) != regno
12350 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12352 links = XEXP (links, 1);
12353 continue;
12356 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12357 if (rsp->last_set == insn)
12359 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12360 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12363 if (REG_P (SET_SRC (set)))
12365 regno = REGNO (SET_SRC (set));
12366 links = LOG_LINKS (insn);
12368 else
12369 break;
12373 /* Check if X, a register, is known to contain a value already
12374 truncated to MODE. In this case we can use a subreg to refer to
12375 the truncated value even though in the generic case we would need
12376 an explicit truncation. */
12378 static bool
12379 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12381 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12382 enum machine_mode truncated = rsp->truncated_to_mode;
12384 if (truncated == 0
12385 || rsp->truncation_label < label_tick_ebb_start)
12386 return false;
12387 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12388 return true;
12389 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
12390 GET_MODE_BITSIZE (truncated)))
12391 return true;
12392 return false;
12395 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
12396 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
12397 might be able to turn a truncate into a subreg using this information.
12398 Return -1 if traversing *P is complete or 0 otherwise. */
12400 static int
12401 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12403 rtx x = *p;
12404 enum machine_mode truncated_mode;
12405 reg_stat_type *rsp;
12407 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12409 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12410 truncated_mode = GET_MODE (x);
12412 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12413 return -1;
12415 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
12416 GET_MODE_BITSIZE (original_mode)))
12417 return -1;
12419 x = SUBREG_REG (x);
12421 /* ??? For hard-regs we now record everything. We might be able to
12422 optimize this using last_set_mode. */
12423 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12424 truncated_mode = GET_MODE (x);
12425 else
12426 return 0;
12428 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12429 if (rsp->truncated_to_mode == 0
12430 || rsp->truncation_label < label_tick_ebb_start
12431 || (GET_MODE_SIZE (truncated_mode)
12432 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12434 rsp->truncated_to_mode = truncated_mode;
12435 rsp->truncation_label = label_tick;
12438 return -1;
12441 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12442 the modes they are used in. This can help truning TRUNCATEs into
12443 SUBREGs. */
12445 static void
12446 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12448 for_each_rtx (x, record_truncated_value, NULL);
12451 /* Scan X for promoted SUBREGs. For each one found,
12452 note what it implies to the registers used in it. */
12454 static void
12455 check_promoted_subreg (rtx insn, rtx x)
12457 if (GET_CODE (x) == SUBREG
12458 && SUBREG_PROMOTED_VAR_P (x)
12459 && REG_P (SUBREG_REG (x)))
12460 record_promoted_value (insn, x);
12461 else
12463 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12464 int i, j;
12466 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12467 switch (format[i])
12469 case 'e':
12470 check_promoted_subreg (insn, XEXP (x, i));
12471 break;
12472 case 'V':
12473 case 'E':
12474 if (XVEC (x, i) != 0)
12475 for (j = 0; j < XVECLEN (x, i); j++)
12476 check_promoted_subreg (insn, XVECEXP (x, i, j));
12477 break;
12482 /* Verify that all the registers and memory references mentioned in *LOC are
12483 still valid. *LOC was part of a value set in INSN when label_tick was
12484 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12485 the invalid references with (clobber (const_int 0)) and return 1. This
12486 replacement is useful because we often can get useful information about
12487 the form of a value (e.g., if it was produced by a shift that always
12488 produces -1 or 0) even though we don't know exactly what registers it
12489 was produced from. */
12491 static int
12492 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12494 rtx x = *loc;
12495 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12496 int len = GET_RTX_LENGTH (GET_CODE (x));
12497 int i, j;
12499 if (REG_P (x))
12501 unsigned int regno = REGNO (x);
12502 unsigned int endregno = END_REGNO (x);
12503 unsigned int j;
12505 for (j = regno; j < endregno; j++)
12507 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12508 if (rsp->last_set_invalid
12509 /* If this is a pseudo-register that was only set once and not
12510 live at the beginning of the function, it is always valid. */
12511 || (! (regno >= FIRST_PSEUDO_REGISTER
12512 && REG_N_SETS (regno) == 1
12513 && (!REGNO_REG_SET_P
12514 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12515 && rsp->last_set_label > tick))
12517 if (replace)
12518 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12519 return replace;
12523 return 1;
12525 /* If this is a memory reference, make sure that there were no stores after
12526 it that might have clobbered the value. We don't have alias info, so we
12527 assume any store invalidates it. Moreover, we only have local UIDs, so
12528 we also assume that there were stores in the intervening basic blocks. */
12529 else if (MEM_P (x) && !MEM_READONLY_P (x)
12530 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12532 if (replace)
12533 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12534 return replace;
12537 for (i = 0; i < len; i++)
12539 if (fmt[i] == 'e')
12541 /* Check for identical subexpressions. If x contains
12542 identical subexpression we only have to traverse one of
12543 them. */
12544 if (i == 1 && ARITHMETIC_P (x))
12546 /* Note that at this point x0 has already been checked
12547 and found valid. */
12548 rtx x0 = XEXP (x, 0);
12549 rtx x1 = XEXP (x, 1);
12551 /* If x0 and x1 are identical then x is also valid. */
12552 if (x0 == x1)
12553 return 1;
12555 /* If x1 is identical to a subexpression of x0 then
12556 while checking x0, x1 has already been checked. Thus
12557 it is valid and so as x. */
12558 if (ARITHMETIC_P (x0)
12559 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12560 return 1;
12562 /* If x0 is identical to a subexpression of x1 then x is
12563 valid iff the rest of x1 is valid. */
12564 if (ARITHMETIC_P (x1)
12565 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12566 return
12567 get_last_value_validate (&XEXP (x1,
12568 x0 == XEXP (x1, 0) ? 1 : 0),
12569 insn, tick, replace);
12572 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12573 replace) == 0)
12574 return 0;
12576 else if (fmt[i] == 'E')
12577 for (j = 0; j < XVECLEN (x, i); j++)
12578 if (get_last_value_validate (&XVECEXP (x, i, j),
12579 insn, tick, replace) == 0)
12580 return 0;
12583 /* If we haven't found a reason for it to be invalid, it is valid. */
12584 return 1;
12587 /* Get the last value assigned to X, if known. Some registers
12588 in the value may be replaced with (clobber (const_int 0)) if their value
12589 is known longer known reliably. */
12591 static rtx
12592 get_last_value (const_rtx x)
12594 unsigned int regno;
12595 rtx value;
12596 reg_stat_type *rsp;
12598 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12599 then convert it to the desired mode. If this is a paradoxical SUBREG,
12600 we cannot predict what values the "extra" bits might have. */
12601 if (GET_CODE (x) == SUBREG
12602 && subreg_lowpart_p (x)
12603 && (GET_MODE_SIZE (GET_MODE (x))
12604 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12605 && (value = get_last_value (SUBREG_REG (x))) != 0)
12606 return gen_lowpart (GET_MODE (x), value);
12608 if (!REG_P (x))
12609 return 0;
12611 regno = REGNO (x);
12612 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12613 value = rsp->last_set_value;
12615 /* If we don't have a value, or if it isn't for this basic block and
12616 it's either a hard register, set more than once, or it's a live
12617 at the beginning of the function, return 0.
12619 Because if it's not live at the beginning of the function then the reg
12620 is always set before being used (is never used without being set).
12621 And, if it's set only once, and it's always set before use, then all
12622 uses must have the same last value, even if it's not from this basic
12623 block. */
12625 if (value == 0
12626 || (rsp->last_set_label < label_tick_ebb_start
12627 && (regno < FIRST_PSEUDO_REGISTER
12628 || REG_N_SETS (regno) != 1
12629 || REGNO_REG_SET_P
12630 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12631 return 0;
12633 /* If the value was set in a later insn than the ones we are processing,
12634 we can't use it even if the register was only set once. */
12635 if (rsp->last_set_label == label_tick
12636 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12637 return 0;
12639 /* If the value has all its registers valid, return it. */
12640 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12641 return value;
12643 /* Otherwise, make a copy and replace any invalid register with
12644 (clobber (const_int 0)). If that fails for some reason, return 0. */
12646 value = copy_rtx (value);
12647 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12648 return value;
12650 return 0;
12653 /* Return nonzero if expression X refers to a REG or to memory
12654 that is set in an instruction more recent than FROM_LUID. */
12656 static int
12657 use_crosses_set_p (const_rtx x, int from_luid)
12659 const char *fmt;
12660 int i;
12661 enum rtx_code code = GET_CODE (x);
12663 if (code == REG)
12665 unsigned int regno = REGNO (x);
12666 unsigned endreg = END_REGNO (x);
12668 #ifdef PUSH_ROUNDING
12669 /* Don't allow uses of the stack pointer to be moved,
12670 because we don't know whether the move crosses a push insn. */
12671 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12672 return 1;
12673 #endif
12674 for (; regno < endreg; regno++)
12676 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12677 if (rsp->last_set
12678 && rsp->last_set_label == label_tick
12679 && DF_INSN_LUID (rsp->last_set) > from_luid)
12680 return 1;
12682 return 0;
12685 if (code == MEM && mem_last_set > from_luid)
12686 return 1;
12688 fmt = GET_RTX_FORMAT (code);
12690 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12692 if (fmt[i] == 'E')
12694 int j;
12695 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12696 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12697 return 1;
12699 else if (fmt[i] == 'e'
12700 && use_crosses_set_p (XEXP (x, i), from_luid))
12701 return 1;
12703 return 0;
12706 /* Define three variables used for communication between the following
12707 routines. */
12709 static unsigned int reg_dead_regno, reg_dead_endregno;
12710 static int reg_dead_flag;
12712 /* Function called via note_stores from reg_dead_at_p.
12714 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12715 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12717 static void
12718 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12720 unsigned int regno, endregno;
12722 if (!REG_P (dest))
12723 return;
12725 regno = REGNO (dest);
12726 endregno = END_REGNO (dest);
12727 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12728 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12731 /* Return nonzero if REG is known to be dead at INSN.
12733 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12734 referencing REG, it is dead. If we hit a SET referencing REG, it is
12735 live. Otherwise, see if it is live or dead at the start of the basic
12736 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12737 must be assumed to be always live. */
12739 static int
12740 reg_dead_at_p (rtx reg, rtx insn)
12742 basic_block block;
12743 unsigned int i;
12745 /* Set variables for reg_dead_at_p_1. */
12746 reg_dead_regno = REGNO (reg);
12747 reg_dead_endregno = END_REGNO (reg);
12749 reg_dead_flag = 0;
12751 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12752 we allow the machine description to decide whether use-and-clobber
12753 patterns are OK. */
12754 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12756 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12757 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12758 return 0;
12761 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12762 beginning of basic block. */
12763 block = BLOCK_FOR_INSN (insn);
12764 for (;;)
12766 if (INSN_P (insn))
12768 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12769 if (reg_dead_flag)
12770 return reg_dead_flag == 1 ? 1 : 0;
12772 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12773 return 1;
12776 if (insn == BB_HEAD (block))
12777 break;
12779 insn = PREV_INSN (insn);
12782 /* Look at live-in sets for the basic block that we were in. */
12783 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12784 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12785 return 0;
12787 return 1;
12790 /* Note hard registers in X that are used. */
12792 static void
12793 mark_used_regs_combine (rtx x)
12795 RTX_CODE code = GET_CODE (x);
12796 unsigned int regno;
12797 int i;
12799 switch (code)
12801 case LABEL_REF:
12802 case SYMBOL_REF:
12803 case CONST_INT:
12804 case CONST:
12805 case CONST_DOUBLE:
12806 case CONST_VECTOR:
12807 case PC:
12808 case ADDR_VEC:
12809 case ADDR_DIFF_VEC:
12810 case ASM_INPUT:
12811 #ifdef HAVE_cc0
12812 /* CC0 must die in the insn after it is set, so we don't need to take
12813 special note of it here. */
12814 case CC0:
12815 #endif
12816 return;
12818 case CLOBBER:
12819 /* If we are clobbering a MEM, mark any hard registers inside the
12820 address as used. */
12821 if (MEM_P (XEXP (x, 0)))
12822 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12823 return;
12825 case REG:
12826 regno = REGNO (x);
12827 /* A hard reg in a wide mode may really be multiple registers.
12828 If so, mark all of them just like the first. */
12829 if (regno < FIRST_PSEUDO_REGISTER)
12831 /* None of this applies to the stack, frame or arg pointers. */
12832 if (regno == STACK_POINTER_REGNUM
12833 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12834 || regno == HARD_FRAME_POINTER_REGNUM
12835 #endif
12836 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12837 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12838 #endif
12839 || regno == FRAME_POINTER_REGNUM)
12840 return;
12842 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12844 return;
12846 case SET:
12848 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12849 the address. */
12850 rtx testreg = SET_DEST (x);
12852 while (GET_CODE (testreg) == SUBREG
12853 || GET_CODE (testreg) == ZERO_EXTRACT
12854 || GET_CODE (testreg) == STRICT_LOW_PART)
12855 testreg = XEXP (testreg, 0);
12857 if (MEM_P (testreg))
12858 mark_used_regs_combine (XEXP (testreg, 0));
12860 mark_used_regs_combine (SET_SRC (x));
12862 return;
12864 default:
12865 break;
12868 /* Recursively scan the operands of this expression. */
12871 const char *fmt = GET_RTX_FORMAT (code);
12873 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12875 if (fmt[i] == 'e')
12876 mark_used_regs_combine (XEXP (x, i));
12877 else if (fmt[i] == 'E')
12879 int j;
12881 for (j = 0; j < XVECLEN (x, i); j++)
12882 mark_used_regs_combine (XVECEXP (x, i, j));
12888 /* Remove register number REGNO from the dead registers list of INSN.
12890 Return the note used to record the death, if there was one. */
12893 remove_death (unsigned int regno, rtx insn)
12895 rtx note = find_regno_note (insn, REG_DEAD, regno);
12897 if (note)
12898 remove_note (insn, note);
12900 return note;
12903 /* For each register (hardware or pseudo) used within expression X, if its
12904 death is in an instruction with luid between FROM_LUID (inclusive) and
12905 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12906 list headed by PNOTES.
12908 That said, don't move registers killed by maybe_kill_insn.
12910 This is done when X is being merged by combination into TO_INSN. These
12911 notes will then be distributed as needed. */
12913 static void
12914 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12915 rtx *pnotes)
12917 const char *fmt;
12918 int len, i;
12919 enum rtx_code code = GET_CODE (x);
12921 if (code == REG)
12923 unsigned int regno = REGNO (x);
12924 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12926 /* Don't move the register if it gets killed in between from and to. */
12927 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12928 && ! reg_referenced_p (x, maybe_kill_insn))
12929 return;
12931 if (where_dead
12932 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12933 && DF_INSN_LUID (where_dead) >= from_luid
12934 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12936 rtx note = remove_death (regno, where_dead);
12938 /* It is possible for the call above to return 0. This can occur
12939 when last_death points to I2 or I1 that we combined with.
12940 In that case make a new note.
12942 We must also check for the case where X is a hard register
12943 and NOTE is a death note for a range of hard registers
12944 including X. In that case, we must put REG_DEAD notes for
12945 the remaining registers in place of NOTE. */
12947 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12948 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12949 > GET_MODE_SIZE (GET_MODE (x))))
12951 unsigned int deadregno = REGNO (XEXP (note, 0));
12952 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12953 unsigned int ourend = END_HARD_REGNO (x);
12954 unsigned int i;
12956 for (i = deadregno; i < deadend; i++)
12957 if (i < regno || i >= ourend)
12958 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12961 /* If we didn't find any note, or if we found a REG_DEAD note that
12962 covers only part of the given reg, and we have a multi-reg hard
12963 register, then to be safe we must check for REG_DEAD notes
12964 for each register other than the first. They could have
12965 their own REG_DEAD notes lying around. */
12966 else if ((note == 0
12967 || (note != 0
12968 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12969 < GET_MODE_SIZE (GET_MODE (x)))))
12970 && regno < FIRST_PSEUDO_REGISTER
12971 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12973 unsigned int ourend = END_HARD_REGNO (x);
12974 unsigned int i, offset;
12975 rtx oldnotes = 0;
12977 if (note)
12978 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12979 else
12980 offset = 1;
12982 for (i = regno + offset; i < ourend; i++)
12983 move_deaths (regno_reg_rtx[i],
12984 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12987 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12989 XEXP (note, 1) = *pnotes;
12990 *pnotes = note;
12992 else
12993 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
12996 return;
12999 else if (GET_CODE (x) == SET)
13001 rtx dest = SET_DEST (x);
13003 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13005 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13006 that accesses one word of a multi-word item, some
13007 piece of everything register in the expression is used by
13008 this insn, so remove any old death. */
13009 /* ??? So why do we test for equality of the sizes? */
13011 if (GET_CODE (dest) == ZERO_EXTRACT
13012 || GET_CODE (dest) == STRICT_LOW_PART
13013 || (GET_CODE (dest) == SUBREG
13014 && (((GET_MODE_SIZE (GET_MODE (dest))
13015 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13016 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13017 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13019 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13020 return;
13023 /* If this is some other SUBREG, we know it replaces the entire
13024 value, so use that as the destination. */
13025 if (GET_CODE (dest) == SUBREG)
13026 dest = SUBREG_REG (dest);
13028 /* If this is a MEM, adjust deaths of anything used in the address.
13029 For a REG (the only other possibility), the entire value is
13030 being replaced so the old value is not used in this insn. */
13032 if (MEM_P (dest))
13033 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13034 to_insn, pnotes);
13035 return;
13038 else if (GET_CODE (x) == CLOBBER)
13039 return;
13041 len = GET_RTX_LENGTH (code);
13042 fmt = GET_RTX_FORMAT (code);
13044 for (i = 0; i < len; i++)
13046 if (fmt[i] == 'E')
13048 int j;
13049 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13050 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13051 to_insn, pnotes);
13053 else if (fmt[i] == 'e')
13054 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13058 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13059 pattern of an insn. X must be a REG. */
13061 static int
13062 reg_bitfield_target_p (rtx x, rtx body)
13064 int i;
13066 if (GET_CODE (body) == SET)
13068 rtx dest = SET_DEST (body);
13069 rtx target;
13070 unsigned int regno, tregno, endregno, endtregno;
13072 if (GET_CODE (dest) == ZERO_EXTRACT)
13073 target = XEXP (dest, 0);
13074 else if (GET_CODE (dest) == STRICT_LOW_PART)
13075 target = SUBREG_REG (XEXP (dest, 0));
13076 else
13077 return 0;
13079 if (GET_CODE (target) == SUBREG)
13080 target = SUBREG_REG (target);
13082 if (!REG_P (target))
13083 return 0;
13085 tregno = REGNO (target), regno = REGNO (x);
13086 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13087 return target == x;
13089 endtregno = end_hard_regno (GET_MODE (target), tregno);
13090 endregno = end_hard_regno (GET_MODE (x), regno);
13092 return endregno > tregno && regno < endtregno;
13095 else if (GET_CODE (body) == PARALLEL)
13096 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13097 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13098 return 1;
13100 return 0;
13103 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13104 as appropriate. I3 and I2 are the insns resulting from the combination
13105 insns including FROM (I2 may be zero).
13107 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13108 not need REG_DEAD notes because they are being substituted for. This
13109 saves searching in the most common cases.
13111 Each note in the list is either ignored or placed on some insns, depending
13112 on the type of note. */
13114 static void
13115 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13116 rtx elim_i1, rtx elim_i0)
13118 rtx note, next_note;
13119 rtx tem;
13121 for (note = notes; note; note = next_note)
13123 rtx place = 0, place2 = 0;
13125 next_note = XEXP (note, 1);
13126 switch (REG_NOTE_KIND (note))
13128 case REG_BR_PROB:
13129 case REG_BR_PRED:
13130 /* Doesn't matter much where we put this, as long as it's somewhere.
13131 It is preferable to keep these notes on branches, which is most
13132 likely to be i3. */
13133 place = i3;
13134 break;
13136 case REG_VALUE_PROFILE:
13137 /* Just get rid of this note, as it is unused later anyway. */
13138 break;
13140 case REG_NON_LOCAL_GOTO:
13141 if (JUMP_P (i3))
13142 place = i3;
13143 else
13145 gcc_assert (i2 && JUMP_P (i2));
13146 place = i2;
13148 break;
13150 case REG_EH_REGION:
13151 /* These notes must remain with the call or trapping instruction. */
13152 if (CALL_P (i3))
13153 place = i3;
13154 else if (i2 && CALL_P (i2))
13155 place = i2;
13156 else
13158 gcc_assert (cfun->can_throw_non_call_exceptions);
13159 if (may_trap_p (i3))
13160 place = i3;
13161 else if (i2 && may_trap_p (i2))
13162 place = i2;
13163 /* ??? Otherwise assume we've combined things such that we
13164 can now prove that the instructions can't trap. Drop the
13165 note in this case. */
13167 break;
13169 case REG_NORETURN:
13170 case REG_SETJMP:
13171 /* These notes must remain with the call. It should not be
13172 possible for both I2 and I3 to be a call. */
13173 if (CALL_P (i3))
13174 place = i3;
13175 else
13177 gcc_assert (i2 && CALL_P (i2));
13178 place = i2;
13180 break;
13182 case REG_UNUSED:
13183 /* Any clobbers for i3 may still exist, and so we must process
13184 REG_UNUSED notes from that insn.
13186 Any clobbers from i2 or i1 can only exist if they were added by
13187 recog_for_combine. In that case, recog_for_combine created the
13188 necessary REG_UNUSED notes. Trying to keep any original
13189 REG_UNUSED notes from these insns can cause incorrect output
13190 if it is for the same register as the original i3 dest.
13191 In that case, we will notice that the register is set in i3,
13192 and then add a REG_UNUSED note for the destination of i3, which
13193 is wrong. However, it is possible to have REG_UNUSED notes from
13194 i2 or i1 for register which were both used and clobbered, so
13195 we keep notes from i2 or i1 if they will turn into REG_DEAD
13196 notes. */
13198 /* If this register is set or clobbered in I3, put the note there
13199 unless there is one already. */
13200 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13202 if (from_insn != i3)
13203 break;
13205 if (! (REG_P (XEXP (note, 0))
13206 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13207 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13208 place = i3;
13210 /* Otherwise, if this register is used by I3, then this register
13211 now dies here, so we must put a REG_DEAD note here unless there
13212 is one already. */
13213 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13214 && ! (REG_P (XEXP (note, 0))
13215 ? find_regno_note (i3, REG_DEAD,
13216 REGNO (XEXP (note, 0)))
13217 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13219 PUT_REG_NOTE_KIND (note, REG_DEAD);
13220 place = i3;
13222 break;
13224 case REG_EQUAL:
13225 case REG_EQUIV:
13226 case REG_NOALIAS:
13227 /* These notes say something about results of an insn. We can
13228 only support them if they used to be on I3 in which case they
13229 remain on I3. Otherwise they are ignored.
13231 If the note refers to an expression that is not a constant, we
13232 must also ignore the note since we cannot tell whether the
13233 equivalence is still true. It might be possible to do
13234 slightly better than this (we only have a problem if I2DEST
13235 or I1DEST is present in the expression), but it doesn't
13236 seem worth the trouble. */
13238 if (from_insn == i3
13239 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13240 place = i3;
13241 break;
13243 case REG_INC:
13244 /* These notes say something about how a register is used. They must
13245 be present on any use of the register in I2 or I3. */
13246 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13247 place = i3;
13249 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13251 if (place)
13252 place2 = i2;
13253 else
13254 place = i2;
13256 break;
13258 case REG_LABEL_TARGET:
13259 case REG_LABEL_OPERAND:
13260 /* This can show up in several ways -- either directly in the
13261 pattern, or hidden off in the constant pool with (or without?)
13262 a REG_EQUAL note. */
13263 /* ??? Ignore the without-reg_equal-note problem for now. */
13264 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13265 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13266 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13267 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13268 place = i3;
13270 if (i2
13271 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13272 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13273 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13274 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13276 if (place)
13277 place2 = i2;
13278 else
13279 place = i2;
13282 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13283 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13284 there. */
13285 if (place && JUMP_P (place)
13286 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13287 && (JUMP_LABEL (place) == NULL
13288 || JUMP_LABEL (place) == XEXP (note, 0)))
13290 rtx label = JUMP_LABEL (place);
13292 if (!label)
13293 JUMP_LABEL (place) = XEXP (note, 0);
13294 else if (LABEL_P (label))
13295 LABEL_NUSES (label)--;
13298 if (place2 && JUMP_P (place2)
13299 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13300 && (JUMP_LABEL (place2) == NULL
13301 || JUMP_LABEL (place2) == XEXP (note, 0)))
13303 rtx label = JUMP_LABEL (place2);
13305 if (!label)
13306 JUMP_LABEL (place2) = XEXP (note, 0);
13307 else if (LABEL_P (label))
13308 LABEL_NUSES (label)--;
13309 place2 = 0;
13311 break;
13313 case REG_NONNEG:
13314 /* This note says something about the value of a register prior
13315 to the execution of an insn. It is too much trouble to see
13316 if the note is still correct in all situations. It is better
13317 to simply delete it. */
13318 break;
13320 case REG_DEAD:
13321 /* If we replaced the right hand side of FROM_INSN with a
13322 REG_EQUAL note, the original use of the dying register
13323 will not have been combined into I3 and I2. In such cases,
13324 FROM_INSN is guaranteed to be the first of the combined
13325 instructions, so we simply need to search back before
13326 FROM_INSN for the previous use or set of this register,
13327 then alter the notes there appropriately.
13329 If the register is used as an input in I3, it dies there.
13330 Similarly for I2, if it is nonzero and adjacent to I3.
13332 If the register is not used as an input in either I3 or I2
13333 and it is not one of the registers we were supposed to eliminate,
13334 there are two possibilities. We might have a non-adjacent I2
13335 or we might have somehow eliminated an additional register
13336 from a computation. For example, we might have had A & B where
13337 we discover that B will always be zero. In this case we will
13338 eliminate the reference to A.
13340 In both cases, we must search to see if we can find a previous
13341 use of A and put the death note there. */
13343 if (from_insn
13344 && from_insn == i2mod
13345 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13346 tem = from_insn;
13347 else
13349 if (from_insn
13350 && CALL_P (from_insn)
13351 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13352 place = from_insn;
13353 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13354 place = i3;
13355 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13356 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13357 place = i2;
13358 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13359 && !(i2mod
13360 && reg_overlap_mentioned_p (XEXP (note, 0),
13361 i2mod_old_rhs)))
13362 || rtx_equal_p (XEXP (note, 0), elim_i1)
13363 || rtx_equal_p (XEXP (note, 0), elim_i0))
13364 break;
13365 tem = i3;
13368 if (place == 0)
13370 basic_block bb = this_basic_block;
13372 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13374 if (!NONDEBUG_INSN_P (tem))
13376 if (tem == BB_HEAD (bb))
13377 break;
13378 continue;
13381 /* If the register is being set at TEM, see if that is all
13382 TEM is doing. If so, delete TEM. Otherwise, make this
13383 into a REG_UNUSED note instead. Don't delete sets to
13384 global register vars. */
13385 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13386 || !global_regs[REGNO (XEXP (note, 0))])
13387 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13389 rtx set = single_set (tem);
13390 rtx inner_dest = 0;
13391 #ifdef HAVE_cc0
13392 rtx cc0_setter = NULL_RTX;
13393 #endif
13395 if (set != 0)
13396 for (inner_dest = SET_DEST (set);
13397 (GET_CODE (inner_dest) == STRICT_LOW_PART
13398 || GET_CODE (inner_dest) == SUBREG
13399 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13400 inner_dest = XEXP (inner_dest, 0))
13403 /* Verify that it was the set, and not a clobber that
13404 modified the register.
13406 CC0 targets must be careful to maintain setter/user
13407 pairs. If we cannot delete the setter due to side
13408 effects, mark the user with an UNUSED note instead
13409 of deleting it. */
13411 if (set != 0 && ! side_effects_p (SET_SRC (set))
13412 && rtx_equal_p (XEXP (note, 0), inner_dest)
13413 #ifdef HAVE_cc0
13414 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13415 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13416 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13417 #endif
13420 /* Move the notes and links of TEM elsewhere.
13421 This might delete other dead insns recursively.
13422 First set the pattern to something that won't use
13423 any register. */
13424 rtx old_notes = REG_NOTES (tem);
13426 PATTERN (tem) = pc_rtx;
13427 REG_NOTES (tem) = NULL;
13429 distribute_notes (old_notes, tem, tem, NULL_RTX,
13430 NULL_RTX, NULL_RTX, NULL_RTX);
13431 distribute_links (LOG_LINKS (tem));
13433 SET_INSN_DELETED (tem);
13434 if (tem == i2)
13435 i2 = NULL_RTX;
13437 #ifdef HAVE_cc0
13438 /* Delete the setter too. */
13439 if (cc0_setter)
13441 PATTERN (cc0_setter) = pc_rtx;
13442 old_notes = REG_NOTES (cc0_setter);
13443 REG_NOTES (cc0_setter) = NULL;
13445 distribute_notes (old_notes, cc0_setter,
13446 cc0_setter, NULL_RTX,
13447 NULL_RTX, NULL_RTX, NULL_RTX);
13448 distribute_links (LOG_LINKS (cc0_setter));
13450 SET_INSN_DELETED (cc0_setter);
13451 if (cc0_setter == i2)
13452 i2 = NULL_RTX;
13454 #endif
13456 else
13458 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13460 /* If there isn't already a REG_UNUSED note, put one
13461 here. Do not place a REG_DEAD note, even if
13462 the register is also used here; that would not
13463 match the algorithm used in lifetime analysis
13464 and can cause the consistency check in the
13465 scheduler to fail. */
13466 if (! find_regno_note (tem, REG_UNUSED,
13467 REGNO (XEXP (note, 0))))
13468 place = tem;
13469 break;
13472 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13473 || (CALL_P (tem)
13474 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13476 place = tem;
13478 /* If we are doing a 3->2 combination, and we have a
13479 register which formerly died in i3 and was not used
13480 by i2, which now no longer dies in i3 and is used in
13481 i2 but does not die in i2, and place is between i2
13482 and i3, then we may need to move a link from place to
13483 i2. */
13484 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13485 && from_insn
13486 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13487 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13489 rtx links = LOG_LINKS (place);
13490 LOG_LINKS (place) = 0;
13491 distribute_links (links);
13493 break;
13496 if (tem == BB_HEAD (bb))
13497 break;
13502 /* If the register is set or already dead at PLACE, we needn't do
13503 anything with this note if it is still a REG_DEAD note.
13504 We check here if it is set at all, not if is it totally replaced,
13505 which is what `dead_or_set_p' checks, so also check for it being
13506 set partially. */
13508 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13510 unsigned int regno = REGNO (XEXP (note, 0));
13511 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13513 if (dead_or_set_p (place, XEXP (note, 0))
13514 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13516 /* Unless the register previously died in PLACE, clear
13517 last_death. [I no longer understand why this is
13518 being done.] */
13519 if (rsp->last_death != place)
13520 rsp->last_death = 0;
13521 place = 0;
13523 else
13524 rsp->last_death = place;
13526 /* If this is a death note for a hard reg that is occupying
13527 multiple registers, ensure that we are still using all
13528 parts of the object. If we find a piece of the object
13529 that is unused, we must arrange for an appropriate REG_DEAD
13530 note to be added for it. However, we can't just emit a USE
13531 and tag the note to it, since the register might actually
13532 be dead; so we recourse, and the recursive call then finds
13533 the previous insn that used this register. */
13535 if (place && regno < FIRST_PSEUDO_REGISTER
13536 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13538 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13539 int all_used = 1;
13540 unsigned int i;
13542 for (i = regno; i < endregno; i++)
13543 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13544 && ! find_regno_fusage (place, USE, i))
13545 || dead_or_set_regno_p (place, i))
13546 all_used = 0;
13548 if (! all_used)
13550 /* Put only REG_DEAD notes for pieces that are
13551 not already dead or set. */
13553 for (i = regno; i < endregno;
13554 i += hard_regno_nregs[i][reg_raw_mode[i]])
13556 rtx piece = regno_reg_rtx[i];
13557 basic_block bb = this_basic_block;
13559 if (! dead_or_set_p (place, piece)
13560 && ! reg_bitfield_target_p (piece,
13561 PATTERN (place)))
13563 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13564 NULL_RTX);
13566 distribute_notes (new_note, place, place,
13567 NULL_RTX, NULL_RTX, NULL_RTX,
13568 NULL_RTX);
13570 else if (! refers_to_regno_p (i, i + 1,
13571 PATTERN (place), 0)
13572 && ! find_regno_fusage (place, USE, i))
13573 for (tem = PREV_INSN (place); ;
13574 tem = PREV_INSN (tem))
13576 if (!NONDEBUG_INSN_P (tem))
13578 if (tem == BB_HEAD (bb))
13579 break;
13580 continue;
13582 if (dead_or_set_p (tem, piece)
13583 || reg_bitfield_target_p (piece,
13584 PATTERN (tem)))
13586 add_reg_note (tem, REG_UNUSED, piece);
13587 break;
13593 place = 0;
13597 break;
13599 default:
13600 /* Any other notes should not be present at this point in the
13601 compilation. */
13602 gcc_unreachable ();
13605 if (place)
13607 XEXP (note, 1) = REG_NOTES (place);
13608 REG_NOTES (place) = note;
13611 if (place2)
13612 add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13616 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13617 I3, I2, and I1 to new locations. This is also called to add a link
13618 pointing at I3 when I3's destination is changed. */
13620 static void
13621 distribute_links (rtx links)
13623 rtx link, next_link;
13625 for (link = links; link; link = next_link)
13627 rtx place = 0;
13628 rtx insn;
13629 rtx set, reg;
13631 next_link = XEXP (link, 1);
13633 /* If the insn that this link points to is a NOTE or isn't a single
13634 set, ignore it. In the latter case, it isn't clear what we
13635 can do other than ignore the link, since we can't tell which
13636 register it was for. Such links wouldn't be used by combine
13637 anyway.
13639 It is not possible for the destination of the target of the link to
13640 have been changed by combine. The only potential of this is if we
13641 replace I3, I2, and I1 by I3 and I2. But in that case the
13642 destination of I2 also remains unchanged. */
13644 if (NOTE_P (XEXP (link, 0))
13645 || (set = single_set (XEXP (link, 0))) == 0)
13646 continue;
13648 reg = SET_DEST (set);
13649 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13650 || GET_CODE (reg) == STRICT_LOW_PART)
13651 reg = XEXP (reg, 0);
13653 /* A LOG_LINK is defined as being placed on the first insn that uses
13654 a register and points to the insn that sets the register. Start
13655 searching at the next insn after the target of the link and stop
13656 when we reach a set of the register or the end of the basic block.
13658 Note that this correctly handles the link that used to point from
13659 I3 to I2. Also note that not much searching is typically done here
13660 since most links don't point very far away. */
13662 for (insn = NEXT_INSN (XEXP (link, 0));
13663 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13664 || BB_HEAD (this_basic_block->next_bb) != insn));
13665 insn = NEXT_INSN (insn))
13666 if (DEBUG_INSN_P (insn))
13667 continue;
13668 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13670 if (reg_referenced_p (reg, PATTERN (insn)))
13671 place = insn;
13672 break;
13674 else if (CALL_P (insn)
13675 && find_reg_fusage (insn, USE, reg))
13677 place = insn;
13678 break;
13680 else if (INSN_P (insn) && reg_set_p (reg, insn))
13681 break;
13683 /* If we found a place to put the link, place it there unless there
13684 is already a link to the same insn as LINK at that point. */
13686 if (place)
13688 rtx link2;
13690 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13691 if (XEXP (link2, 0) == XEXP (link, 0))
13692 break;
13694 if (link2 == 0)
13696 XEXP (link, 1) = LOG_LINKS (place);
13697 LOG_LINKS (place) = link;
13699 /* Set added_links_insn to the earliest insn we added a
13700 link to. */
13701 if (added_links_insn == 0
13702 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13703 added_links_insn = place;
13709 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13710 Check whether the expression pointer to by LOC is a register or
13711 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13712 Otherwise return zero. */
13714 static int
13715 unmentioned_reg_p_1 (rtx *loc, void *expr)
13717 rtx x = *loc;
13719 if (x != NULL_RTX
13720 && (REG_P (x) || MEM_P (x))
13721 && ! reg_mentioned_p (x, (rtx) expr))
13722 return 1;
13723 return 0;
13726 /* Check for any register or memory mentioned in EQUIV that is not
13727 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13728 of EXPR where some registers may have been replaced by constants. */
13730 static bool
13731 unmentioned_reg_p (rtx equiv, rtx expr)
13733 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13736 void
13737 dump_combine_stats (FILE *file)
13739 fprintf
13740 (file,
13741 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13742 combine_attempts, combine_merges, combine_extras, combine_successes);
13745 void
13746 dump_combine_total_stats (FILE *file)
13748 fprintf
13749 (file,
13750 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13751 total_attempts, total_merges, total_extras, total_successes);
13754 static bool
13755 gate_handle_combine (void)
13757 return (optimize > 0);
13760 /* Try combining insns through substitution. */
13761 static unsigned int
13762 rest_of_handle_combine (void)
13764 int rebuild_jump_labels_after_combine;
13766 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13767 df_note_add_problem ();
13768 df_analyze ();
13770 regstat_init_n_sets_and_refs ();
13772 rebuild_jump_labels_after_combine
13773 = combine_instructions (get_insns (), max_reg_num ());
13775 /* Combining insns may have turned an indirect jump into a
13776 direct jump. Rebuild the JUMP_LABEL fields of jumping
13777 instructions. */
13778 if (rebuild_jump_labels_after_combine)
13780 timevar_push (TV_JUMP);
13781 rebuild_jump_labels (get_insns ());
13782 cleanup_cfg (0);
13783 timevar_pop (TV_JUMP);
13786 regstat_free_n_sets_and_refs ();
13787 return 0;
13790 struct rtl_opt_pass pass_combine =
13793 RTL_PASS,
13794 "combine", /* name */
13795 gate_handle_combine, /* gate */
13796 rest_of_handle_combine, /* execute */
13797 NULL, /* sub */
13798 NULL, /* next */
13799 0, /* static_pass_number */
13800 TV_COMBINE, /* tv_id */
13801 PROP_cfglayout, /* properties_required */
13802 0, /* properties_provided */
13803 0, /* properties_destroyed */
13804 0, /* todo_flags_start */
13805 TODO_dump_func |
13806 TODO_df_finish | TODO_verify_rtl_sharing |
13807 TODO_ggc_collect, /* todo_flags_finish */