Daily bump.
[official-gcc.git] / gcc / combine.c
blob3ee53e6ccd9637b915cd5a7511b75f1ff91b3e0c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "diagnostic-core.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file. */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105 #include "df.h"
106 #include "cgraph.h"
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 if (undobuf.other_insn != NULL_RTX
4382 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4383 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4384 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4386 *new_direct_jump_p = 1;
4387 update_cfg_for_uncondjump (undobuf.other_insn);
4390 combine_successes++;
4391 undo_commit ();
4393 if (added_links_insn
4394 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4395 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4396 return added_links_insn;
4397 else
4398 return newi2pat ? i2 : i3;
4401 /* Undo all the modifications recorded in undobuf. */
4403 static void
4404 undo_all (void)
4406 struct undo *undo, *next;
4408 for (undo = undobuf.undos; undo; undo = next)
4410 next = undo->next;
4411 switch (undo->kind)
4413 case UNDO_RTX:
4414 *undo->where.r = undo->old_contents.r;
4415 break;
4416 case UNDO_INT:
4417 *undo->where.i = undo->old_contents.i;
4418 break;
4419 case UNDO_MODE:
4420 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4421 break;
4422 default:
4423 gcc_unreachable ();
4426 undo->next = undobuf.frees;
4427 undobuf.frees = undo;
4430 undobuf.undos = 0;
4433 /* We've committed to accepting the changes we made. Move all
4434 of the undos to the free list. */
4436 static void
4437 undo_commit (void)
4439 struct undo *undo, *next;
4441 for (undo = undobuf.undos; undo; undo = next)
4443 next = undo->next;
4444 undo->next = undobuf.frees;
4445 undobuf.frees = undo;
4447 undobuf.undos = 0;
4450 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4451 where we have an arithmetic expression and return that point. LOC will
4452 be inside INSN.
4454 try_combine will call this function to see if an insn can be split into
4455 two insns. */
4457 static rtx *
4458 find_split_point (rtx *loc, rtx insn, bool set_src)
4460 rtx x = *loc;
4461 enum rtx_code code = GET_CODE (x);
4462 rtx *split;
4463 unsigned HOST_WIDE_INT len = 0;
4464 HOST_WIDE_INT pos = 0;
4465 int unsignedp = 0;
4466 rtx inner = NULL_RTX;
4468 /* First special-case some codes. */
4469 switch (code)
4471 case SUBREG:
4472 #ifdef INSN_SCHEDULING
4473 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4474 point. */
4475 if (MEM_P (SUBREG_REG (x)))
4476 return loc;
4477 #endif
4478 return find_split_point (&SUBREG_REG (x), insn, false);
4480 case MEM:
4481 #ifdef HAVE_lo_sum
4482 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4483 using LO_SUM and HIGH. */
4484 if (GET_CODE (XEXP (x, 0)) == CONST
4485 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4487 enum machine_mode address_mode
4488 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4490 SUBST (XEXP (x, 0),
4491 gen_rtx_LO_SUM (address_mode,
4492 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4493 XEXP (x, 0)));
4494 return &XEXP (XEXP (x, 0), 0);
4496 #endif
4498 /* If we have a PLUS whose second operand is a constant and the
4499 address is not valid, perhaps will can split it up using
4500 the machine-specific way to split large constants. We use
4501 the first pseudo-reg (one of the virtual regs) as a placeholder;
4502 it will not remain in the result. */
4503 if (GET_CODE (XEXP (x, 0)) == PLUS
4504 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4505 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4506 MEM_ADDR_SPACE (x)))
4508 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4509 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4510 XEXP (x, 0)),
4511 subst_insn);
4513 /* This should have produced two insns, each of which sets our
4514 placeholder. If the source of the second is a valid address,
4515 we can make put both sources together and make a split point
4516 in the middle. */
4518 if (seq
4519 && NEXT_INSN (seq) != NULL_RTX
4520 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4521 && NONJUMP_INSN_P (seq)
4522 && GET_CODE (PATTERN (seq)) == SET
4523 && SET_DEST (PATTERN (seq)) == reg
4524 && ! reg_mentioned_p (reg,
4525 SET_SRC (PATTERN (seq)))
4526 && NONJUMP_INSN_P (NEXT_INSN (seq))
4527 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4528 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4529 && memory_address_addr_space_p
4530 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4531 MEM_ADDR_SPACE (x)))
4533 rtx src1 = SET_SRC (PATTERN (seq));
4534 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4536 /* Replace the placeholder in SRC2 with SRC1. If we can
4537 find where in SRC2 it was placed, that can become our
4538 split point and we can replace this address with SRC2.
4539 Just try two obvious places. */
4541 src2 = replace_rtx (src2, reg, src1);
4542 split = 0;
4543 if (XEXP (src2, 0) == src1)
4544 split = &XEXP (src2, 0);
4545 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4546 && XEXP (XEXP (src2, 0), 0) == src1)
4547 split = &XEXP (XEXP (src2, 0), 0);
4549 if (split)
4551 SUBST (XEXP (x, 0), src2);
4552 return split;
4556 /* If that didn't work, perhaps the first operand is complex and
4557 needs to be computed separately, so make a split point there.
4558 This will occur on machines that just support REG + CONST
4559 and have a constant moved through some previous computation. */
4561 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4562 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4563 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4564 return &XEXP (XEXP (x, 0), 0);
4567 /* If we have a PLUS whose first operand is complex, try computing it
4568 separately by making a split there. */
4569 if (GET_CODE (XEXP (x, 0)) == PLUS
4570 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4571 MEM_ADDR_SPACE (x))
4572 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4573 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4574 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4575 return &XEXP (XEXP (x, 0), 0);
4576 break;
4578 case SET:
4579 #ifdef HAVE_cc0
4580 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4581 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4582 we need to put the operand into a register. So split at that
4583 point. */
4585 if (SET_DEST (x) == cc0_rtx
4586 && GET_CODE (SET_SRC (x)) != COMPARE
4587 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4588 && !OBJECT_P (SET_SRC (x))
4589 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4590 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4591 return &SET_SRC (x);
4592 #endif
4594 /* See if we can split SET_SRC as it stands. */
4595 split = find_split_point (&SET_SRC (x), insn, true);
4596 if (split && split != &SET_SRC (x))
4597 return split;
4599 /* See if we can split SET_DEST as it stands. */
4600 split = find_split_point (&SET_DEST (x), insn, false);
4601 if (split && split != &SET_DEST (x))
4602 return split;
4604 /* See if this is a bitfield assignment with everything constant. If
4605 so, this is an IOR of an AND, so split it into that. */
4606 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4607 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4608 <= HOST_BITS_PER_WIDE_INT)
4609 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4610 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4611 && CONST_INT_P (SET_SRC (x))
4612 && ((INTVAL (XEXP (SET_DEST (x), 1))
4613 + INTVAL (XEXP (SET_DEST (x), 2)))
4614 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4615 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4617 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4618 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4619 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4620 rtx dest = XEXP (SET_DEST (x), 0);
4621 enum machine_mode mode = GET_MODE (dest);
4622 unsigned HOST_WIDE_INT mask
4623 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4624 rtx or_mask;
4626 if (BITS_BIG_ENDIAN)
4627 pos = GET_MODE_BITSIZE (mode) - len - pos;
4629 or_mask = gen_int_mode (src << pos, mode);
4630 if (src == mask)
4631 SUBST (SET_SRC (x),
4632 simplify_gen_binary (IOR, mode, dest, or_mask));
4633 else
4635 rtx negmask = gen_int_mode (~(mask << pos), mode);
4636 SUBST (SET_SRC (x),
4637 simplify_gen_binary (IOR, mode,
4638 simplify_gen_binary (AND, mode,
4639 dest, negmask),
4640 or_mask));
4643 SUBST (SET_DEST (x), dest);
4645 split = find_split_point (&SET_SRC (x), insn, true);
4646 if (split && split != &SET_SRC (x))
4647 return split;
4650 /* Otherwise, see if this is an operation that we can split into two.
4651 If so, try to split that. */
4652 code = GET_CODE (SET_SRC (x));
4654 switch (code)
4656 case AND:
4657 /* If we are AND'ing with a large constant that is only a single
4658 bit and the result is only being used in a context where we
4659 need to know if it is zero or nonzero, replace it with a bit
4660 extraction. This will avoid the large constant, which might
4661 have taken more than one insn to make. If the constant were
4662 not a valid argument to the AND but took only one insn to make,
4663 this is no worse, but if it took more than one insn, it will
4664 be better. */
4666 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4667 && REG_P (XEXP (SET_SRC (x), 0))
4668 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4669 && REG_P (SET_DEST (x))
4670 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4671 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4672 && XEXP (*split, 0) == SET_DEST (x)
4673 && XEXP (*split, 1) == const0_rtx)
4675 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4676 XEXP (SET_SRC (x), 0),
4677 pos, NULL_RTX, 1, 1, 0, 0);
4678 if (extraction != 0)
4680 SUBST (SET_SRC (x), extraction);
4681 return find_split_point (loc, insn, false);
4684 break;
4686 case NE:
4687 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4688 is known to be on, this can be converted into a NEG of a shift. */
4689 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4690 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4691 && 1 <= (pos = exact_log2
4692 (nonzero_bits (XEXP (SET_SRC (x), 0),
4693 GET_MODE (XEXP (SET_SRC (x), 0))))))
4695 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4697 SUBST (SET_SRC (x),
4698 gen_rtx_NEG (mode,
4699 gen_rtx_LSHIFTRT (mode,
4700 XEXP (SET_SRC (x), 0),
4701 GEN_INT (pos))));
4703 split = find_split_point (&SET_SRC (x), insn, true);
4704 if (split && split != &SET_SRC (x))
4705 return split;
4707 break;
4709 case SIGN_EXTEND:
4710 inner = XEXP (SET_SRC (x), 0);
4712 /* We can't optimize if either mode is a partial integer
4713 mode as we don't know how many bits are significant
4714 in those modes. */
4715 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4716 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4717 break;
4719 pos = 0;
4720 len = GET_MODE_BITSIZE (GET_MODE (inner));
4721 unsignedp = 0;
4722 break;
4724 case SIGN_EXTRACT:
4725 case ZERO_EXTRACT:
4726 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4727 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4729 inner = XEXP (SET_SRC (x), 0);
4730 len = INTVAL (XEXP (SET_SRC (x), 1));
4731 pos = INTVAL (XEXP (SET_SRC (x), 2));
4733 if (BITS_BIG_ENDIAN)
4734 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4735 unsignedp = (code == ZERO_EXTRACT);
4737 break;
4739 default:
4740 break;
4743 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4745 enum machine_mode mode = GET_MODE (SET_SRC (x));
4747 /* For unsigned, we have a choice of a shift followed by an
4748 AND or two shifts. Use two shifts for field sizes where the
4749 constant might be too large. We assume here that we can
4750 always at least get 8-bit constants in an AND insn, which is
4751 true for every current RISC. */
4753 if (unsignedp && len <= 8)
4755 SUBST (SET_SRC (x),
4756 gen_rtx_AND (mode,
4757 gen_rtx_LSHIFTRT
4758 (mode, gen_lowpart (mode, inner),
4759 GEN_INT (pos)),
4760 GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4761 - 1)));
4763 split = find_split_point (&SET_SRC (x), insn, true);
4764 if (split && split != &SET_SRC (x))
4765 return split;
4767 else
4769 SUBST (SET_SRC (x),
4770 gen_rtx_fmt_ee
4771 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4772 gen_rtx_ASHIFT (mode,
4773 gen_lowpart (mode, inner),
4774 GEN_INT (GET_MODE_BITSIZE (mode)
4775 - len - pos)),
4776 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4778 split = find_split_point (&SET_SRC (x), insn, true);
4779 if (split && split != &SET_SRC (x))
4780 return split;
4784 /* See if this is a simple operation with a constant as the second
4785 operand. It might be that this constant is out of range and hence
4786 could be used as a split point. */
4787 if (BINARY_P (SET_SRC (x))
4788 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4789 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4790 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4791 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4792 return &XEXP (SET_SRC (x), 1);
4794 /* Finally, see if this is a simple operation with its first operand
4795 not in a register. The operation might require this operand in a
4796 register, so return it as a split point. We can always do this
4797 because if the first operand were another operation, we would have
4798 already found it as a split point. */
4799 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4800 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4801 return &XEXP (SET_SRC (x), 0);
4803 return 0;
4805 case AND:
4806 case IOR:
4807 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4808 it is better to write this as (not (ior A B)) so we can split it.
4809 Similarly for IOR. */
4810 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4812 SUBST (*loc,
4813 gen_rtx_NOT (GET_MODE (x),
4814 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4815 GET_MODE (x),
4816 XEXP (XEXP (x, 0), 0),
4817 XEXP (XEXP (x, 1), 0))));
4818 return find_split_point (loc, insn, set_src);
4821 /* Many RISC machines have a large set of logical insns. If the
4822 second operand is a NOT, put it first so we will try to split the
4823 other operand first. */
4824 if (GET_CODE (XEXP (x, 1)) == NOT)
4826 rtx tem = XEXP (x, 0);
4827 SUBST (XEXP (x, 0), XEXP (x, 1));
4828 SUBST (XEXP (x, 1), tem);
4830 break;
4832 case PLUS:
4833 case MINUS:
4834 /* Canonicalization can produce (minus A (mult B C)), where C is a
4835 constant. It may be better to try splitting (plus (mult B -C) A)
4836 instead if this isn't a multiply by a power of two. */
4837 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4838 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4839 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4841 enum machine_mode mode = GET_MODE (x);
4842 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4843 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4844 SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4845 XEXP (XEXP (x, 1), 0),
4846 GEN_INT (other_int)),
4847 XEXP (x, 0)));
4848 return find_split_point (loc, insn, set_src);
4851 /* Split at a multiply-accumulate instruction. However if this is
4852 the SET_SRC, we likely do not have such an instruction and it's
4853 worthless to try this split. */
4854 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4855 return loc;
4857 default:
4858 break;
4861 /* Otherwise, select our actions depending on our rtx class. */
4862 switch (GET_RTX_CLASS (code))
4864 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4865 case RTX_TERNARY:
4866 split = find_split_point (&XEXP (x, 2), insn, false);
4867 if (split)
4868 return split;
4869 /* ... fall through ... */
4870 case RTX_BIN_ARITH:
4871 case RTX_COMM_ARITH:
4872 case RTX_COMPARE:
4873 case RTX_COMM_COMPARE:
4874 split = find_split_point (&XEXP (x, 1), insn, false);
4875 if (split)
4876 return split;
4877 /* ... fall through ... */
4878 case RTX_UNARY:
4879 /* Some machines have (and (shift ...) ...) insns. If X is not
4880 an AND, but XEXP (X, 0) is, use it as our split point. */
4881 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4882 return &XEXP (x, 0);
4884 split = find_split_point (&XEXP (x, 0), insn, false);
4885 if (split)
4886 return split;
4887 return loc;
4889 default:
4890 /* Otherwise, we don't have a split point. */
4891 return 0;
4895 /* Throughout X, replace FROM with TO, and return the result.
4896 The result is TO if X is FROM;
4897 otherwise the result is X, but its contents may have been modified.
4898 If they were modified, a record was made in undobuf so that
4899 undo_all will (among other things) return X to its original state.
4901 If the number of changes necessary is too much to record to undo,
4902 the excess changes are not made, so the result is invalid.
4903 The changes already made can still be undone.
4904 undobuf.num_undo is incremented for such changes, so by testing that
4905 the caller can tell whether the result is valid.
4907 `n_occurrences' is incremented each time FROM is replaced.
4909 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4911 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4912 by copying if `n_occurrences' is nonzero. */
4914 static rtx
4915 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4917 enum rtx_code code = GET_CODE (x);
4918 enum machine_mode op0_mode = VOIDmode;
4919 const char *fmt;
4920 int len, i;
4921 rtx new_rtx;
4923 /* Two expressions are equal if they are identical copies of a shared
4924 RTX or if they are both registers with the same register number
4925 and mode. */
4927 #define COMBINE_RTX_EQUAL_P(X,Y) \
4928 ((X) == (Y) \
4929 || (REG_P (X) && REG_P (Y) \
4930 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4932 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4934 n_occurrences++;
4935 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4938 /* If X and FROM are the same register but different modes, they
4939 will not have been seen as equal above. However, the log links code
4940 will make a LOG_LINKS entry for that case. If we do nothing, we
4941 will try to rerecognize our original insn and, when it succeeds,
4942 we will delete the feeding insn, which is incorrect.
4944 So force this insn not to match in this (rare) case. */
4945 if (! in_dest && code == REG && REG_P (from)
4946 && reg_overlap_mentioned_p (x, from))
4947 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4949 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4950 of which may contain things that can be combined. */
4951 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4952 return x;
4954 /* It is possible to have a subexpression appear twice in the insn.
4955 Suppose that FROM is a register that appears within TO.
4956 Then, after that subexpression has been scanned once by `subst',
4957 the second time it is scanned, TO may be found. If we were
4958 to scan TO here, we would find FROM within it and create a
4959 self-referent rtl structure which is completely wrong. */
4960 if (COMBINE_RTX_EQUAL_P (x, to))
4961 return to;
4963 /* Parallel asm_operands need special attention because all of the
4964 inputs are shared across the arms. Furthermore, unsharing the
4965 rtl results in recognition failures. Failure to handle this case
4966 specially can result in circular rtl.
4968 Solve this by doing a normal pass across the first entry of the
4969 parallel, and only processing the SET_DESTs of the subsequent
4970 entries. Ug. */
4972 if (code == PARALLEL
4973 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4974 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4976 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4978 /* If this substitution failed, this whole thing fails. */
4979 if (GET_CODE (new_rtx) == CLOBBER
4980 && XEXP (new_rtx, 0) == const0_rtx)
4981 return new_rtx;
4983 SUBST (XVECEXP (x, 0, 0), new_rtx);
4985 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4987 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4989 if (!REG_P (dest)
4990 && GET_CODE (dest) != CC0
4991 && GET_CODE (dest) != PC)
4993 new_rtx = subst (dest, from, to, 0, unique_copy);
4995 /* If this substitution failed, this whole thing fails. */
4996 if (GET_CODE (new_rtx) == CLOBBER
4997 && XEXP (new_rtx, 0) == const0_rtx)
4998 return new_rtx;
5000 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5004 else
5006 len = GET_RTX_LENGTH (code);
5007 fmt = GET_RTX_FORMAT (code);
5009 /* We don't need to process a SET_DEST that is a register, CC0,
5010 or PC, so set up to skip this common case. All other cases
5011 where we want to suppress replacing something inside a
5012 SET_SRC are handled via the IN_DEST operand. */
5013 if (code == SET
5014 && (REG_P (SET_DEST (x))
5015 || GET_CODE (SET_DEST (x)) == CC0
5016 || GET_CODE (SET_DEST (x)) == PC))
5017 fmt = "ie";
5019 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5020 constant. */
5021 if (fmt[0] == 'e')
5022 op0_mode = GET_MODE (XEXP (x, 0));
5024 for (i = 0; i < len; i++)
5026 if (fmt[i] == 'E')
5028 int j;
5029 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5031 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5033 new_rtx = (unique_copy && n_occurrences
5034 ? copy_rtx (to) : to);
5035 n_occurrences++;
5037 else
5039 new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
5040 unique_copy);
5042 /* If this substitution failed, this whole thing
5043 fails. */
5044 if (GET_CODE (new_rtx) == CLOBBER
5045 && XEXP (new_rtx, 0) == const0_rtx)
5046 return new_rtx;
5049 SUBST (XVECEXP (x, i, j), new_rtx);
5052 else if (fmt[i] == 'e')
5054 /* If this is a register being set, ignore it. */
5055 new_rtx = XEXP (x, i);
5056 if (in_dest
5057 && i == 0
5058 && (((code == SUBREG || code == ZERO_EXTRACT)
5059 && REG_P (new_rtx))
5060 || code == STRICT_LOW_PART))
5063 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5065 /* In general, don't install a subreg involving two
5066 modes not tieable. It can worsen register
5067 allocation, and can even make invalid reload
5068 insns, since the reg inside may need to be copied
5069 from in the outside mode, and that may be invalid
5070 if it is an fp reg copied in integer mode.
5072 We allow two exceptions to this: It is valid if
5073 it is inside another SUBREG and the mode of that
5074 SUBREG and the mode of the inside of TO is
5075 tieable and it is valid if X is a SET that copies
5076 FROM to CC0. */
5078 if (GET_CODE (to) == SUBREG
5079 && ! MODES_TIEABLE_P (GET_MODE (to),
5080 GET_MODE (SUBREG_REG (to)))
5081 && ! (code == SUBREG
5082 && MODES_TIEABLE_P (GET_MODE (x),
5083 GET_MODE (SUBREG_REG (to))))
5084 #ifdef HAVE_cc0
5085 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5086 #endif
5088 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5090 #ifdef CANNOT_CHANGE_MODE_CLASS
5091 if (code == SUBREG
5092 && REG_P (to)
5093 && REGNO (to) < FIRST_PSEUDO_REGISTER
5094 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5095 GET_MODE (to),
5096 GET_MODE (x)))
5097 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5098 #endif
5100 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5101 n_occurrences++;
5103 else
5104 /* If we are in a SET_DEST, suppress most cases unless we
5105 have gone inside a MEM, in which case we want to
5106 simplify the address. We assume here that things that
5107 are actually part of the destination have their inner
5108 parts in the first expression. This is true for SUBREG,
5109 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5110 things aside from REG and MEM that should appear in a
5111 SET_DEST. */
5112 new_rtx = subst (XEXP (x, i), from, to,
5113 (((in_dest
5114 && (code == SUBREG || code == STRICT_LOW_PART
5115 || code == ZERO_EXTRACT))
5116 || code == SET)
5117 && i == 0), unique_copy);
5119 /* If we found that we will have to reject this combination,
5120 indicate that by returning the CLOBBER ourselves, rather than
5121 an expression containing it. This will speed things up as
5122 well as prevent accidents where two CLOBBERs are considered
5123 to be equal, thus producing an incorrect simplification. */
5125 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5126 return new_rtx;
5128 if (GET_CODE (x) == SUBREG
5129 && (CONST_INT_P (new_rtx)
5130 || GET_CODE (new_rtx) == CONST_DOUBLE))
5132 enum machine_mode mode = GET_MODE (x);
5134 x = simplify_subreg (GET_MODE (x), new_rtx,
5135 GET_MODE (SUBREG_REG (x)),
5136 SUBREG_BYTE (x));
5137 if (! x)
5138 x = gen_rtx_CLOBBER (mode, const0_rtx);
5140 else if (CONST_INT_P (new_rtx)
5141 && GET_CODE (x) == ZERO_EXTEND)
5143 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5144 new_rtx, GET_MODE (XEXP (x, 0)));
5145 gcc_assert (x);
5147 else
5148 SUBST (XEXP (x, i), new_rtx);
5153 /* Check if we are loading something from the constant pool via float
5154 extension; in this case we would undo compress_float_constant
5155 optimization and degenerate constant load to an immediate value. */
5156 if (GET_CODE (x) == FLOAT_EXTEND
5157 && MEM_P (XEXP (x, 0))
5158 && MEM_READONLY_P (XEXP (x, 0)))
5160 rtx tmp = avoid_constant_pool_reference (x);
5161 if (x != tmp)
5162 return x;
5165 /* Try to simplify X. If the simplification changed the code, it is likely
5166 that further simplification will help, so loop, but limit the number
5167 of repetitions that will be performed. */
5169 for (i = 0; i < 4; i++)
5171 /* If X is sufficiently simple, don't bother trying to do anything
5172 with it. */
5173 if (code != CONST_INT && code != REG && code != CLOBBER)
5174 x = combine_simplify_rtx (x, op0_mode, in_dest);
5176 if (GET_CODE (x) == code)
5177 break;
5179 code = GET_CODE (x);
5181 /* We no longer know the original mode of operand 0 since we
5182 have changed the form of X) */
5183 op0_mode = VOIDmode;
5186 return x;
5189 /* Simplify X, a piece of RTL. We just operate on the expression at the
5190 outer level; call `subst' to simplify recursively. Return the new
5191 expression.
5193 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5194 if we are inside a SET_DEST. */
5196 static rtx
5197 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
5199 enum rtx_code code = GET_CODE (x);
5200 enum machine_mode mode = GET_MODE (x);
5201 rtx temp;
5202 int i;
5204 /* If this is a commutative operation, put a constant last and a complex
5205 expression first. We don't need to do this for comparisons here. */
5206 if (COMMUTATIVE_ARITH_P (x)
5207 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5209 temp = XEXP (x, 0);
5210 SUBST (XEXP (x, 0), XEXP (x, 1));
5211 SUBST (XEXP (x, 1), temp);
5214 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5215 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5216 things. Check for cases where both arms are testing the same
5217 condition.
5219 Don't do anything if all operands are very simple. */
5221 if ((BINARY_P (x)
5222 && ((!OBJECT_P (XEXP (x, 0))
5223 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5224 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5225 || (!OBJECT_P (XEXP (x, 1))
5226 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5227 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5228 || (UNARY_P (x)
5229 && (!OBJECT_P (XEXP (x, 0))
5230 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5231 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5233 rtx cond, true_rtx, false_rtx;
5235 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5236 if (cond != 0
5237 /* If everything is a comparison, what we have is highly unlikely
5238 to be simpler, so don't use it. */
5239 && ! (COMPARISON_P (x)
5240 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5242 rtx cop1 = const0_rtx;
5243 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5245 if (cond_code == NE && COMPARISON_P (cond))
5246 return x;
5248 /* Simplify the alternative arms; this may collapse the true and
5249 false arms to store-flag values. Be careful to use copy_rtx
5250 here since true_rtx or false_rtx might share RTL with x as a
5251 result of the if_then_else_cond call above. */
5252 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
5253 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
5255 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5256 is unlikely to be simpler. */
5257 if (general_operand (true_rtx, VOIDmode)
5258 && general_operand (false_rtx, VOIDmode))
5260 enum rtx_code reversed;
5262 /* Restarting if we generate a store-flag expression will cause
5263 us to loop. Just drop through in this case. */
5265 /* If the result values are STORE_FLAG_VALUE and zero, we can
5266 just make the comparison operation. */
5267 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5268 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5269 cond, cop1);
5270 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5271 && ((reversed = reversed_comparison_code_parts
5272 (cond_code, cond, cop1, NULL))
5273 != UNKNOWN))
5274 x = simplify_gen_relational (reversed, mode, VOIDmode,
5275 cond, cop1);
5277 /* Likewise, we can make the negate of a comparison operation
5278 if the result values are - STORE_FLAG_VALUE and zero. */
5279 else if (CONST_INT_P (true_rtx)
5280 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5281 && false_rtx == const0_rtx)
5282 x = simplify_gen_unary (NEG, mode,
5283 simplify_gen_relational (cond_code,
5284 mode, VOIDmode,
5285 cond, cop1),
5286 mode);
5287 else if (CONST_INT_P (false_rtx)
5288 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5289 && true_rtx == const0_rtx
5290 && ((reversed = reversed_comparison_code_parts
5291 (cond_code, cond, cop1, NULL))
5292 != UNKNOWN))
5293 x = simplify_gen_unary (NEG, mode,
5294 simplify_gen_relational (reversed,
5295 mode, VOIDmode,
5296 cond, cop1),
5297 mode);
5298 else
5299 return gen_rtx_IF_THEN_ELSE (mode,
5300 simplify_gen_relational (cond_code,
5301 mode,
5302 VOIDmode,
5303 cond,
5304 cop1),
5305 true_rtx, false_rtx);
5307 code = GET_CODE (x);
5308 op0_mode = VOIDmode;
5313 /* Try to fold this expression in case we have constants that weren't
5314 present before. */
5315 temp = 0;
5316 switch (GET_RTX_CLASS (code))
5318 case RTX_UNARY:
5319 if (op0_mode == VOIDmode)
5320 op0_mode = GET_MODE (XEXP (x, 0));
5321 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5322 break;
5323 case RTX_COMPARE:
5324 case RTX_COMM_COMPARE:
5326 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5327 if (cmp_mode == VOIDmode)
5329 cmp_mode = GET_MODE (XEXP (x, 1));
5330 if (cmp_mode == VOIDmode)
5331 cmp_mode = op0_mode;
5333 temp = simplify_relational_operation (code, mode, cmp_mode,
5334 XEXP (x, 0), XEXP (x, 1));
5336 break;
5337 case RTX_COMM_ARITH:
5338 case RTX_BIN_ARITH:
5339 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5340 break;
5341 case RTX_BITFIELD_OPS:
5342 case RTX_TERNARY:
5343 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5344 XEXP (x, 1), XEXP (x, 2));
5345 break;
5346 default:
5347 break;
5350 if (temp)
5352 x = temp;
5353 code = GET_CODE (temp);
5354 op0_mode = VOIDmode;
5355 mode = GET_MODE (temp);
5358 /* First see if we can apply the inverse distributive law. */
5359 if (code == PLUS || code == MINUS
5360 || code == AND || code == IOR || code == XOR)
5362 x = apply_distributive_law (x);
5363 code = GET_CODE (x);
5364 op0_mode = VOIDmode;
5367 /* If CODE is an associative operation not otherwise handled, see if we
5368 can associate some operands. This can win if they are constants or
5369 if they are logically related (i.e. (a & b) & a). */
5370 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5371 || code == AND || code == IOR || code == XOR
5372 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5373 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5374 || (flag_associative_math && FLOAT_MODE_P (mode))))
5376 if (GET_CODE (XEXP (x, 0)) == code)
5378 rtx other = XEXP (XEXP (x, 0), 0);
5379 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5380 rtx inner_op1 = XEXP (x, 1);
5381 rtx inner;
5383 /* Make sure we pass the constant operand if any as the second
5384 one if this is a commutative operation. */
5385 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5387 rtx tem = inner_op0;
5388 inner_op0 = inner_op1;
5389 inner_op1 = tem;
5391 inner = simplify_binary_operation (code == MINUS ? PLUS
5392 : code == DIV ? MULT
5393 : code,
5394 mode, inner_op0, inner_op1);
5396 /* For commutative operations, try the other pair if that one
5397 didn't simplify. */
5398 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5400 other = XEXP (XEXP (x, 0), 1);
5401 inner = simplify_binary_operation (code, mode,
5402 XEXP (XEXP (x, 0), 0),
5403 XEXP (x, 1));
5406 if (inner)
5407 return simplify_gen_binary (code, mode, other, inner);
5411 /* A little bit of algebraic simplification here. */
5412 switch (code)
5414 case MEM:
5415 /* Ensure that our address has any ASHIFTs converted to MULT in case
5416 address-recognizing predicates are called later. */
5417 temp = make_compound_operation (XEXP (x, 0), MEM);
5418 SUBST (XEXP (x, 0), temp);
5419 break;
5421 case SUBREG:
5422 if (op0_mode == VOIDmode)
5423 op0_mode = GET_MODE (SUBREG_REG (x));
5425 /* See if this can be moved to simplify_subreg. */
5426 if (CONSTANT_P (SUBREG_REG (x))
5427 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5428 /* Don't call gen_lowpart if the inner mode
5429 is VOIDmode and we cannot simplify it, as SUBREG without
5430 inner mode is invalid. */
5431 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5432 || gen_lowpart_common (mode, SUBREG_REG (x))))
5433 return gen_lowpart (mode, SUBREG_REG (x));
5435 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5436 break;
5438 rtx temp;
5439 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5440 SUBREG_BYTE (x));
5441 if (temp)
5442 return temp;
5445 /* Don't change the mode of the MEM if that would change the meaning
5446 of the address. */
5447 if (MEM_P (SUBREG_REG (x))
5448 && (MEM_VOLATILE_P (SUBREG_REG (x))
5449 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5450 return gen_rtx_CLOBBER (mode, const0_rtx);
5452 /* Note that we cannot do any narrowing for non-constants since
5453 we might have been counting on using the fact that some bits were
5454 zero. We now do this in the SET. */
5456 break;
5458 case NEG:
5459 temp = expand_compound_operation (XEXP (x, 0));
5461 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5462 replaced by (lshiftrt X C). This will convert
5463 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5465 if (GET_CODE (temp) == ASHIFTRT
5466 && CONST_INT_P (XEXP (temp, 1))
5467 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5468 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5469 INTVAL (XEXP (temp, 1)));
5471 /* If X has only a single bit that might be nonzero, say, bit I, convert
5472 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5473 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5474 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5475 or a SUBREG of one since we'd be making the expression more
5476 complex if it was just a register. */
5478 if (!REG_P (temp)
5479 && ! (GET_CODE (temp) == SUBREG
5480 && REG_P (SUBREG_REG (temp)))
5481 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5483 rtx temp1 = simplify_shift_const
5484 (NULL_RTX, ASHIFTRT, mode,
5485 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5486 GET_MODE_BITSIZE (mode) - 1 - i),
5487 GET_MODE_BITSIZE (mode) - 1 - i);
5489 /* If all we did was surround TEMP with the two shifts, we
5490 haven't improved anything, so don't use it. Otherwise,
5491 we are better off with TEMP1. */
5492 if (GET_CODE (temp1) != ASHIFTRT
5493 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5494 || XEXP (XEXP (temp1, 0), 0) != temp)
5495 return temp1;
5497 break;
5499 case TRUNCATE:
5500 /* We can't handle truncation to a partial integer mode here
5501 because we don't know the real bitsize of the partial
5502 integer mode. */
5503 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5504 break;
5506 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5507 SUBST (XEXP (x, 0),
5508 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5509 GET_MODE_MASK (mode), 0));
5511 /* We can truncate a constant value and return it. */
5512 if (CONST_INT_P (XEXP (x, 0)))
5513 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5515 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5516 whose value is a comparison can be replaced with a subreg if
5517 STORE_FLAG_VALUE permits. */
5518 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5519 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5520 && (temp = get_last_value (XEXP (x, 0)))
5521 && COMPARISON_P (temp))
5522 return gen_lowpart (mode, XEXP (x, 0));
5523 break;
5525 case CONST:
5526 /* (const (const X)) can become (const X). Do it this way rather than
5527 returning the inner CONST since CONST can be shared with a
5528 REG_EQUAL note. */
5529 if (GET_CODE (XEXP (x, 0)) == CONST)
5530 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5531 break;
5533 #ifdef HAVE_lo_sum
5534 case LO_SUM:
5535 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5536 can add in an offset. find_split_point will split this address up
5537 again if it doesn't match. */
5538 if (GET_CODE (XEXP (x, 0)) == HIGH
5539 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5540 return XEXP (x, 1);
5541 break;
5542 #endif
5544 case PLUS:
5545 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5546 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5547 bit-field and can be replaced by either a sign_extend or a
5548 sign_extract. The `and' may be a zero_extend and the two
5549 <c>, -<c> constants may be reversed. */
5550 if (GET_CODE (XEXP (x, 0)) == XOR
5551 && CONST_INT_P (XEXP (x, 1))
5552 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5553 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5554 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5555 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5556 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5557 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5558 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5559 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5560 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5561 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5562 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5563 == (unsigned int) i + 1))))
5564 return simplify_shift_const
5565 (NULL_RTX, ASHIFTRT, mode,
5566 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5567 XEXP (XEXP (XEXP (x, 0), 0), 0),
5568 GET_MODE_BITSIZE (mode) - (i + 1)),
5569 GET_MODE_BITSIZE (mode) - (i + 1));
5571 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5572 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5573 the bitsize of the mode - 1. This allows simplification of
5574 "a = (b & 8) == 0;" */
5575 if (XEXP (x, 1) == constm1_rtx
5576 && !REG_P (XEXP (x, 0))
5577 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5578 && REG_P (SUBREG_REG (XEXP (x, 0))))
5579 && nonzero_bits (XEXP (x, 0), mode) == 1)
5580 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5581 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5582 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5583 GET_MODE_BITSIZE (mode) - 1),
5584 GET_MODE_BITSIZE (mode) - 1);
5586 /* If we are adding two things that have no bits in common, convert
5587 the addition into an IOR. This will often be further simplified,
5588 for example in cases like ((a & 1) + (a & 2)), which can
5589 become a & 3. */
5591 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5592 && (nonzero_bits (XEXP (x, 0), mode)
5593 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5595 /* Try to simplify the expression further. */
5596 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5597 temp = combine_simplify_rtx (tor, mode, in_dest);
5599 /* If we could, great. If not, do not go ahead with the IOR
5600 replacement, since PLUS appears in many special purpose
5601 address arithmetic instructions. */
5602 if (GET_CODE (temp) != CLOBBER && temp != tor)
5603 return temp;
5605 break;
5607 case MINUS:
5608 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5609 (and <foo> (const_int pow2-1)) */
5610 if (GET_CODE (XEXP (x, 1)) == AND
5611 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5612 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5613 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5614 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5615 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5616 break;
5618 case MULT:
5619 /* If we have (mult (plus A B) C), apply the distributive law and then
5620 the inverse distributive law to see if things simplify. This
5621 occurs mostly in addresses, often when unrolling loops. */
5623 if (GET_CODE (XEXP (x, 0)) == PLUS)
5625 rtx result = distribute_and_simplify_rtx (x, 0);
5626 if (result)
5627 return result;
5630 /* Try simplify a*(b/c) as (a*b)/c. */
5631 if (FLOAT_MODE_P (mode) && flag_associative_math
5632 && GET_CODE (XEXP (x, 0)) == DIV)
5634 rtx tem = simplify_binary_operation (MULT, mode,
5635 XEXP (XEXP (x, 0), 0),
5636 XEXP (x, 1));
5637 if (tem)
5638 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5640 break;
5642 case UDIV:
5643 /* If this is a divide by a power of two, treat it as a shift if
5644 its first operand is a shift. */
5645 if (CONST_INT_P (XEXP (x, 1))
5646 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5647 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5648 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5649 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5650 || GET_CODE (XEXP (x, 0)) == ROTATE
5651 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5652 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5653 break;
5655 case EQ: case NE:
5656 case GT: case GTU: case GE: case GEU:
5657 case LT: case LTU: case LE: case LEU:
5658 case UNEQ: case LTGT:
5659 case UNGT: case UNGE:
5660 case UNLT: case UNLE:
5661 case UNORDERED: case ORDERED:
5662 /* If the first operand is a condition code, we can't do anything
5663 with it. */
5664 if (GET_CODE (XEXP (x, 0)) == COMPARE
5665 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5666 && ! CC0_P (XEXP (x, 0))))
5668 rtx op0 = XEXP (x, 0);
5669 rtx op1 = XEXP (x, 1);
5670 enum rtx_code new_code;
5672 if (GET_CODE (op0) == COMPARE)
5673 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5675 /* Simplify our comparison, if possible. */
5676 new_code = simplify_comparison (code, &op0, &op1);
5678 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5679 if only the low-order bit is possibly nonzero in X (such as when
5680 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5681 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5682 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5683 (plus X 1).
5685 Remove any ZERO_EXTRACT we made when thinking this was a
5686 comparison. It may now be simpler to use, e.g., an AND. If a
5687 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5688 the call to make_compound_operation in the SET case. */
5690 if (STORE_FLAG_VALUE == 1
5691 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5692 && op1 == const0_rtx
5693 && mode == GET_MODE (op0)
5694 && nonzero_bits (op0, mode) == 1)
5695 return gen_lowpart (mode,
5696 expand_compound_operation (op0));
5698 else if (STORE_FLAG_VALUE == 1
5699 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5700 && op1 == const0_rtx
5701 && mode == GET_MODE (op0)
5702 && (num_sign_bit_copies (op0, mode)
5703 == GET_MODE_BITSIZE (mode)))
5705 op0 = expand_compound_operation (op0);
5706 return simplify_gen_unary (NEG, mode,
5707 gen_lowpart (mode, op0),
5708 mode);
5711 else if (STORE_FLAG_VALUE == 1
5712 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5713 && op1 == const0_rtx
5714 && mode == GET_MODE (op0)
5715 && nonzero_bits (op0, mode) == 1)
5717 op0 = expand_compound_operation (op0);
5718 return simplify_gen_binary (XOR, mode,
5719 gen_lowpart (mode, op0),
5720 const1_rtx);
5723 else if (STORE_FLAG_VALUE == 1
5724 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5725 && op1 == const0_rtx
5726 && mode == GET_MODE (op0)
5727 && (num_sign_bit_copies (op0, mode)
5728 == GET_MODE_BITSIZE (mode)))
5730 op0 = expand_compound_operation (op0);
5731 return plus_constant (gen_lowpart (mode, op0), 1);
5734 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5735 those above. */
5736 if (STORE_FLAG_VALUE == -1
5737 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5738 && op1 == const0_rtx
5739 && (num_sign_bit_copies (op0, mode)
5740 == GET_MODE_BITSIZE (mode)))
5741 return gen_lowpart (mode,
5742 expand_compound_operation (op0));
5744 else if (STORE_FLAG_VALUE == -1
5745 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5746 && op1 == const0_rtx
5747 && mode == GET_MODE (op0)
5748 && nonzero_bits (op0, mode) == 1)
5750 op0 = expand_compound_operation (op0);
5751 return simplify_gen_unary (NEG, mode,
5752 gen_lowpart (mode, op0),
5753 mode);
5756 else if (STORE_FLAG_VALUE == -1
5757 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5758 && op1 == const0_rtx
5759 && mode == GET_MODE (op0)
5760 && (num_sign_bit_copies (op0, mode)
5761 == GET_MODE_BITSIZE (mode)))
5763 op0 = expand_compound_operation (op0);
5764 return simplify_gen_unary (NOT, mode,
5765 gen_lowpart (mode, op0),
5766 mode);
5769 /* If X is 0/1, (eq X 0) is X-1. */
5770 else if (STORE_FLAG_VALUE == -1
5771 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5772 && op1 == const0_rtx
5773 && mode == GET_MODE (op0)
5774 && nonzero_bits (op0, mode) == 1)
5776 op0 = expand_compound_operation (op0);
5777 return plus_constant (gen_lowpart (mode, op0), -1);
5780 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5781 one bit that might be nonzero, we can convert (ne x 0) to
5782 (ashift x c) where C puts the bit in the sign bit. Remove any
5783 AND with STORE_FLAG_VALUE when we are done, since we are only
5784 going to test the sign bit. */
5785 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5786 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5787 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5788 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5789 && op1 == const0_rtx
5790 && mode == GET_MODE (op0)
5791 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5793 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5794 expand_compound_operation (op0),
5795 GET_MODE_BITSIZE (mode) - 1 - i);
5796 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5797 return XEXP (x, 0);
5798 else
5799 return x;
5802 /* If the code changed, return a whole new comparison. */
5803 if (new_code != code)
5804 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5806 /* Otherwise, keep this operation, but maybe change its operands.
5807 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5808 SUBST (XEXP (x, 0), op0);
5809 SUBST (XEXP (x, 1), op1);
5811 break;
5813 case IF_THEN_ELSE:
5814 return simplify_if_then_else (x);
5816 case ZERO_EXTRACT:
5817 case SIGN_EXTRACT:
5818 case ZERO_EXTEND:
5819 case SIGN_EXTEND:
5820 /* If we are processing SET_DEST, we are done. */
5821 if (in_dest)
5822 return x;
5824 return expand_compound_operation (x);
5826 case SET:
5827 return simplify_set (x);
5829 case AND:
5830 case IOR:
5831 return simplify_logical (x);
5833 case ASHIFT:
5834 case LSHIFTRT:
5835 case ASHIFTRT:
5836 case ROTATE:
5837 case ROTATERT:
5838 /* If this is a shift by a constant amount, simplify it. */
5839 if (CONST_INT_P (XEXP (x, 1)))
5840 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5841 INTVAL (XEXP (x, 1)));
5843 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5844 SUBST (XEXP (x, 1),
5845 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5846 ((unsigned HOST_WIDE_INT) 1
5847 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5848 - 1,
5849 0));
5850 break;
5852 default:
5853 break;
5856 return x;
5859 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5861 static rtx
5862 simplify_if_then_else (rtx x)
5864 enum machine_mode mode = GET_MODE (x);
5865 rtx cond = XEXP (x, 0);
5866 rtx true_rtx = XEXP (x, 1);
5867 rtx false_rtx = XEXP (x, 2);
5868 enum rtx_code true_code = GET_CODE (cond);
5869 int comparison_p = COMPARISON_P (cond);
5870 rtx temp;
5871 int i;
5872 enum rtx_code false_code;
5873 rtx reversed;
5875 /* Simplify storing of the truth value. */
5876 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5877 return simplify_gen_relational (true_code, mode, VOIDmode,
5878 XEXP (cond, 0), XEXP (cond, 1));
5880 /* Also when the truth value has to be reversed. */
5881 if (comparison_p
5882 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5883 && (reversed = reversed_comparison (cond, mode)))
5884 return reversed;
5886 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5887 in it is being compared against certain values. Get the true and false
5888 comparisons and see if that says anything about the value of each arm. */
5890 if (comparison_p
5891 && ((false_code = reversed_comparison_code (cond, NULL))
5892 != UNKNOWN)
5893 && REG_P (XEXP (cond, 0)))
5895 HOST_WIDE_INT nzb;
5896 rtx from = XEXP (cond, 0);
5897 rtx true_val = XEXP (cond, 1);
5898 rtx false_val = true_val;
5899 int swapped = 0;
5901 /* If FALSE_CODE is EQ, swap the codes and arms. */
5903 if (false_code == EQ)
5905 swapped = 1, true_code = EQ, false_code = NE;
5906 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5909 /* If we are comparing against zero and the expression being tested has
5910 only a single bit that might be nonzero, that is its value when it is
5911 not equal to zero. Similarly if it is known to be -1 or 0. */
5913 if (true_code == EQ && true_val == const0_rtx
5914 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5916 false_code = EQ;
5917 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5919 else if (true_code == EQ && true_val == const0_rtx
5920 && (num_sign_bit_copies (from, GET_MODE (from))
5921 == GET_MODE_BITSIZE (GET_MODE (from))))
5923 false_code = EQ;
5924 false_val = constm1_rtx;
5927 /* Now simplify an arm if we know the value of the register in the
5928 branch and it is used in the arm. Be careful due to the potential
5929 of locally-shared RTL. */
5931 if (reg_mentioned_p (from, true_rtx))
5932 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5933 from, true_val),
5934 pc_rtx, pc_rtx, 0, 0);
5935 if (reg_mentioned_p (from, false_rtx))
5936 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5937 from, false_val),
5938 pc_rtx, pc_rtx, 0, 0);
5940 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5941 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5943 true_rtx = XEXP (x, 1);
5944 false_rtx = XEXP (x, 2);
5945 true_code = GET_CODE (cond);
5948 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5949 reversed, do so to avoid needing two sets of patterns for
5950 subtract-and-branch insns. Similarly if we have a constant in the true
5951 arm, the false arm is the same as the first operand of the comparison, or
5952 the false arm is more complicated than the true arm. */
5954 if (comparison_p
5955 && reversed_comparison_code (cond, NULL) != UNKNOWN
5956 && (true_rtx == pc_rtx
5957 || (CONSTANT_P (true_rtx)
5958 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5959 || true_rtx == const0_rtx
5960 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5961 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5962 && !OBJECT_P (false_rtx))
5963 || reg_mentioned_p (true_rtx, false_rtx)
5964 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5966 true_code = reversed_comparison_code (cond, NULL);
5967 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5968 SUBST (XEXP (x, 1), false_rtx);
5969 SUBST (XEXP (x, 2), true_rtx);
5971 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5972 cond = XEXP (x, 0);
5974 /* It is possible that the conditional has been simplified out. */
5975 true_code = GET_CODE (cond);
5976 comparison_p = COMPARISON_P (cond);
5979 /* If the two arms are identical, we don't need the comparison. */
5981 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5982 return true_rtx;
5984 /* Convert a == b ? b : a to "a". */
5985 if (true_code == EQ && ! side_effects_p (cond)
5986 && !HONOR_NANS (mode)
5987 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5988 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5989 return false_rtx;
5990 else if (true_code == NE && ! side_effects_p (cond)
5991 && !HONOR_NANS (mode)
5992 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5993 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5994 return true_rtx;
5996 /* Look for cases where we have (abs x) or (neg (abs X)). */
5998 if (GET_MODE_CLASS (mode) == MODE_INT
5999 && comparison_p
6000 && XEXP (cond, 1) == const0_rtx
6001 && GET_CODE (false_rtx) == NEG
6002 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6003 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6004 && ! side_effects_p (true_rtx))
6005 switch (true_code)
6007 case GT:
6008 case GE:
6009 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6010 case LT:
6011 case LE:
6012 return
6013 simplify_gen_unary (NEG, mode,
6014 simplify_gen_unary (ABS, mode, true_rtx, mode),
6015 mode);
6016 default:
6017 break;
6020 /* Look for MIN or MAX. */
6022 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6023 && comparison_p
6024 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6025 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6026 && ! side_effects_p (cond))
6027 switch (true_code)
6029 case GE:
6030 case GT:
6031 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6032 case LE:
6033 case LT:
6034 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6035 case GEU:
6036 case GTU:
6037 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6038 case LEU:
6039 case LTU:
6040 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6041 default:
6042 break;
6045 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6046 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6047 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6048 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6049 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6050 neither 1 or -1, but it isn't worth checking for. */
6052 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6053 && comparison_p
6054 && GET_MODE_CLASS (mode) == MODE_INT
6055 && ! side_effects_p (x))
6057 rtx t = make_compound_operation (true_rtx, SET);
6058 rtx f = make_compound_operation (false_rtx, SET);
6059 rtx cond_op0 = XEXP (cond, 0);
6060 rtx cond_op1 = XEXP (cond, 1);
6061 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6062 enum machine_mode m = mode;
6063 rtx z = 0, c1 = NULL_RTX;
6065 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6066 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6067 || GET_CODE (t) == ASHIFT
6068 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6069 && rtx_equal_p (XEXP (t, 0), f))
6070 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6072 /* If an identity-zero op is commutative, check whether there
6073 would be a match if we swapped the operands. */
6074 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6075 || GET_CODE (t) == XOR)
6076 && rtx_equal_p (XEXP (t, 1), f))
6077 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6078 else if (GET_CODE (t) == SIGN_EXTEND
6079 && (GET_CODE (XEXP (t, 0)) == PLUS
6080 || GET_CODE (XEXP (t, 0)) == MINUS
6081 || GET_CODE (XEXP (t, 0)) == IOR
6082 || GET_CODE (XEXP (t, 0)) == XOR
6083 || GET_CODE (XEXP (t, 0)) == ASHIFT
6084 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6085 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6086 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6087 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6088 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6089 && (num_sign_bit_copies (f, GET_MODE (f))
6090 > (unsigned int)
6091 (GET_MODE_BITSIZE (mode)
6092 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6094 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6095 extend_op = SIGN_EXTEND;
6096 m = GET_MODE (XEXP (t, 0));
6098 else if (GET_CODE (t) == SIGN_EXTEND
6099 && (GET_CODE (XEXP (t, 0)) == PLUS
6100 || GET_CODE (XEXP (t, 0)) == IOR
6101 || GET_CODE (XEXP (t, 0)) == XOR)
6102 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6103 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6104 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6105 && (num_sign_bit_copies (f, GET_MODE (f))
6106 > (unsigned int)
6107 (GET_MODE_BITSIZE (mode)
6108 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6110 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6111 extend_op = SIGN_EXTEND;
6112 m = GET_MODE (XEXP (t, 0));
6114 else if (GET_CODE (t) == ZERO_EXTEND
6115 && (GET_CODE (XEXP (t, 0)) == PLUS
6116 || GET_CODE (XEXP (t, 0)) == MINUS
6117 || GET_CODE (XEXP (t, 0)) == IOR
6118 || GET_CODE (XEXP (t, 0)) == XOR
6119 || GET_CODE (XEXP (t, 0)) == ASHIFT
6120 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6121 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6122 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6123 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6124 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6125 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6126 && ((nonzero_bits (f, GET_MODE (f))
6127 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6128 == 0))
6130 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6131 extend_op = ZERO_EXTEND;
6132 m = GET_MODE (XEXP (t, 0));
6134 else if (GET_CODE (t) == ZERO_EXTEND
6135 && (GET_CODE (XEXP (t, 0)) == PLUS
6136 || GET_CODE (XEXP (t, 0)) == IOR
6137 || GET_CODE (XEXP (t, 0)) == XOR)
6138 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6139 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6140 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6141 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6142 && ((nonzero_bits (f, GET_MODE (f))
6143 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6144 == 0))
6146 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6147 extend_op = ZERO_EXTEND;
6148 m = GET_MODE (XEXP (t, 0));
6151 if (z)
6153 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6154 cond_op0, cond_op1),
6155 pc_rtx, pc_rtx, 0, 0);
6156 temp = simplify_gen_binary (MULT, m, temp,
6157 simplify_gen_binary (MULT, m, c1,
6158 const_true_rtx));
6159 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
6160 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6162 if (extend_op != UNKNOWN)
6163 temp = simplify_gen_unary (extend_op, mode, temp, m);
6165 return temp;
6169 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6170 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6171 negation of a single bit, we can convert this operation to a shift. We
6172 can actually do this more generally, but it doesn't seem worth it. */
6174 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6175 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6176 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6177 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6178 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6179 == GET_MODE_BITSIZE (mode))
6180 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6181 return
6182 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6183 gen_lowpart (mode, XEXP (cond, 0)), i);
6185 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6186 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6187 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6188 && GET_MODE (XEXP (cond, 0)) == mode
6189 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6190 == nonzero_bits (XEXP (cond, 0), mode)
6191 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6192 return XEXP (cond, 0);
6194 return x;
6197 /* Simplify X, a SET expression. Return the new expression. */
6199 static rtx
6200 simplify_set (rtx x)
6202 rtx src = SET_SRC (x);
6203 rtx dest = SET_DEST (x);
6204 enum machine_mode mode
6205 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6206 rtx other_insn;
6207 rtx *cc_use;
6209 /* (set (pc) (return)) gets written as (return). */
6210 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
6211 return src;
6213 /* Now that we know for sure which bits of SRC we are using, see if we can
6214 simplify the expression for the object knowing that we only need the
6215 low-order bits. */
6217 if (GET_MODE_CLASS (mode) == MODE_INT
6218 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6220 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6221 SUBST (SET_SRC (x), src);
6224 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6225 the comparison result and try to simplify it unless we already have used
6226 undobuf.other_insn. */
6227 if ((GET_MODE_CLASS (mode) == MODE_CC
6228 || GET_CODE (src) == COMPARE
6229 || CC0_P (dest))
6230 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6231 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6232 && COMPARISON_P (*cc_use)
6233 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6235 enum rtx_code old_code = GET_CODE (*cc_use);
6236 enum rtx_code new_code;
6237 rtx op0, op1, tmp;
6238 int other_changed = 0;
6239 enum machine_mode compare_mode = GET_MODE (dest);
6241 if (GET_CODE (src) == COMPARE)
6242 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6243 else
6244 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6246 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6247 op0, op1);
6248 if (!tmp)
6249 new_code = old_code;
6250 else if (!CONSTANT_P (tmp))
6252 new_code = GET_CODE (tmp);
6253 op0 = XEXP (tmp, 0);
6254 op1 = XEXP (tmp, 1);
6256 else
6258 rtx pat = PATTERN (other_insn);
6259 undobuf.other_insn = other_insn;
6260 SUBST (*cc_use, tmp);
6262 /* Attempt to simplify CC user. */
6263 if (GET_CODE (pat) == SET)
6265 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6266 if (new_rtx != NULL_RTX)
6267 SUBST (SET_SRC (pat), new_rtx);
6270 /* Convert X into a no-op move. */
6271 SUBST (SET_DEST (x), pc_rtx);
6272 SUBST (SET_SRC (x), pc_rtx);
6273 return x;
6276 /* Simplify our comparison, if possible. */
6277 new_code = simplify_comparison (new_code, &op0, &op1);
6279 #ifdef SELECT_CC_MODE
6280 /* If this machine has CC modes other than CCmode, check to see if we
6281 need to use a different CC mode here. */
6282 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6283 compare_mode = GET_MODE (op0);
6284 else
6285 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6287 #ifndef HAVE_cc0
6288 /* If the mode changed, we have to change SET_DEST, the mode in the
6289 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6290 a hard register, just build new versions with the proper mode. If it
6291 is a pseudo, we lose unless it is only time we set the pseudo, in
6292 which case we can safely change its mode. */
6293 if (compare_mode != GET_MODE (dest))
6295 if (can_change_dest_mode (dest, 0, compare_mode))
6297 unsigned int regno = REGNO (dest);
6298 rtx new_dest;
6300 if (regno < FIRST_PSEUDO_REGISTER)
6301 new_dest = gen_rtx_REG (compare_mode, regno);
6302 else
6304 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6305 new_dest = regno_reg_rtx[regno];
6308 SUBST (SET_DEST (x), new_dest);
6309 SUBST (XEXP (*cc_use, 0), new_dest);
6310 other_changed = 1;
6312 dest = new_dest;
6315 #endif /* cc0 */
6316 #endif /* SELECT_CC_MODE */
6318 /* If the code changed, we have to build a new comparison in
6319 undobuf.other_insn. */
6320 if (new_code != old_code)
6322 int other_changed_previously = other_changed;
6323 unsigned HOST_WIDE_INT mask;
6324 rtx old_cc_use = *cc_use;
6326 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6327 dest, const0_rtx));
6328 other_changed = 1;
6330 /* If the only change we made was to change an EQ into an NE or
6331 vice versa, OP0 has only one bit that might be nonzero, and OP1
6332 is zero, check if changing the user of the condition code will
6333 produce a valid insn. If it won't, we can keep the original code
6334 in that insn by surrounding our operation with an XOR. */
6336 if (((old_code == NE && new_code == EQ)
6337 || (old_code == EQ && new_code == NE))
6338 && ! other_changed_previously && op1 == const0_rtx
6339 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
6340 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6342 rtx pat = PATTERN (other_insn), note = 0;
6344 if ((recog_for_combine (&pat, other_insn, &note) < 0
6345 && ! check_asm_operands (pat)))
6347 *cc_use = old_cc_use;
6348 other_changed = 0;
6350 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6351 op0, GEN_INT (mask));
6356 if (other_changed)
6357 undobuf.other_insn = other_insn;
6359 /* Otherwise, if we didn't previously have a COMPARE in the
6360 correct mode, we need one. */
6361 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6363 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6364 src = SET_SRC (x);
6366 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6368 SUBST (SET_SRC (x), op0);
6369 src = SET_SRC (x);
6371 /* Otherwise, update the COMPARE if needed. */
6372 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6374 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6375 src = SET_SRC (x);
6378 else
6380 /* Get SET_SRC in a form where we have placed back any
6381 compound expressions. Then do the checks below. */
6382 src = make_compound_operation (src, SET);
6383 SUBST (SET_SRC (x), src);
6386 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6387 and X being a REG or (subreg (reg)), we may be able to convert this to
6388 (set (subreg:m2 x) (op)).
6390 We can always do this if M1 is narrower than M2 because that means that
6391 we only care about the low bits of the result.
6393 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6394 perform a narrower operation than requested since the high-order bits will
6395 be undefined. On machine where it is defined, this transformation is safe
6396 as long as M1 and M2 have the same number of words. */
6398 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6399 && !OBJECT_P (SUBREG_REG (src))
6400 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6401 / UNITS_PER_WORD)
6402 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6403 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6404 #ifndef WORD_REGISTER_OPERATIONS
6405 && (GET_MODE_SIZE (GET_MODE (src))
6406 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6407 #endif
6408 #ifdef CANNOT_CHANGE_MODE_CLASS
6409 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6410 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6411 GET_MODE (SUBREG_REG (src)),
6412 GET_MODE (src)))
6413 #endif
6414 && (REG_P (dest)
6415 || (GET_CODE (dest) == SUBREG
6416 && REG_P (SUBREG_REG (dest)))))
6418 SUBST (SET_DEST (x),
6419 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6420 dest));
6421 SUBST (SET_SRC (x), SUBREG_REG (src));
6423 src = SET_SRC (x), dest = SET_DEST (x);
6426 #ifdef HAVE_cc0
6427 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6428 in SRC. */
6429 if (dest == cc0_rtx
6430 && GET_CODE (src) == SUBREG
6431 && subreg_lowpart_p (src)
6432 && (GET_MODE_BITSIZE (GET_MODE (src))
6433 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6435 rtx inner = SUBREG_REG (src);
6436 enum machine_mode inner_mode = GET_MODE (inner);
6438 /* Here we make sure that we don't have a sign bit on. */
6439 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6440 && (nonzero_bits (inner, inner_mode)
6441 < ((unsigned HOST_WIDE_INT) 1
6442 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6444 SUBST (SET_SRC (x), inner);
6445 src = SET_SRC (x);
6448 #endif
6450 #ifdef LOAD_EXTEND_OP
6451 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6452 would require a paradoxical subreg. Replace the subreg with a
6453 zero_extend to avoid the reload that would otherwise be required. */
6455 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6456 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6457 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6458 && SUBREG_BYTE (src) == 0
6459 && (GET_MODE_SIZE (GET_MODE (src))
6460 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6461 && MEM_P (SUBREG_REG (src)))
6463 SUBST (SET_SRC (x),
6464 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6465 GET_MODE (src), SUBREG_REG (src)));
6467 src = SET_SRC (x);
6469 #endif
6471 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6472 are comparing an item known to be 0 or -1 against 0, use a logical
6473 operation instead. Check for one of the arms being an IOR of the other
6474 arm with some value. We compute three terms to be IOR'ed together. In
6475 practice, at most two will be nonzero. Then we do the IOR's. */
6477 if (GET_CODE (dest) != PC
6478 && GET_CODE (src) == IF_THEN_ELSE
6479 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6480 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6481 && XEXP (XEXP (src, 0), 1) == const0_rtx
6482 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6483 #ifdef HAVE_conditional_move
6484 && ! can_conditionally_move_p (GET_MODE (src))
6485 #endif
6486 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6487 GET_MODE (XEXP (XEXP (src, 0), 0)))
6488 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6489 && ! side_effects_p (src))
6491 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6492 ? XEXP (src, 1) : XEXP (src, 2));
6493 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6494 ? XEXP (src, 2) : XEXP (src, 1));
6495 rtx term1 = const0_rtx, term2, term3;
6497 if (GET_CODE (true_rtx) == IOR
6498 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6499 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6500 else if (GET_CODE (true_rtx) == IOR
6501 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6502 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6503 else if (GET_CODE (false_rtx) == IOR
6504 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6505 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6506 else if (GET_CODE (false_rtx) == IOR
6507 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6508 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6510 term2 = simplify_gen_binary (AND, GET_MODE (src),
6511 XEXP (XEXP (src, 0), 0), true_rtx);
6512 term3 = simplify_gen_binary (AND, GET_MODE (src),
6513 simplify_gen_unary (NOT, GET_MODE (src),
6514 XEXP (XEXP (src, 0), 0),
6515 GET_MODE (src)),
6516 false_rtx);
6518 SUBST (SET_SRC (x),
6519 simplify_gen_binary (IOR, GET_MODE (src),
6520 simplify_gen_binary (IOR, GET_MODE (src),
6521 term1, term2),
6522 term3));
6524 src = SET_SRC (x);
6527 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6528 whole thing fail. */
6529 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6530 return src;
6531 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6532 return dest;
6533 else
6534 /* Convert this into a field assignment operation, if possible. */
6535 return make_field_assignment (x);
6538 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6539 result. */
6541 static rtx
6542 simplify_logical (rtx x)
6544 enum machine_mode mode = GET_MODE (x);
6545 rtx op0 = XEXP (x, 0);
6546 rtx op1 = XEXP (x, 1);
6548 switch (GET_CODE (x))
6550 case AND:
6551 /* We can call simplify_and_const_int only if we don't lose
6552 any (sign) bits when converting INTVAL (op1) to
6553 "unsigned HOST_WIDE_INT". */
6554 if (CONST_INT_P (op1)
6555 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6556 || INTVAL (op1) > 0))
6558 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6559 if (GET_CODE (x) != AND)
6560 return x;
6562 op0 = XEXP (x, 0);
6563 op1 = XEXP (x, 1);
6566 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6567 apply the distributive law and then the inverse distributive
6568 law to see if things simplify. */
6569 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6571 rtx result = distribute_and_simplify_rtx (x, 0);
6572 if (result)
6573 return result;
6575 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6577 rtx result = distribute_and_simplify_rtx (x, 1);
6578 if (result)
6579 return result;
6581 break;
6583 case IOR:
6584 /* If we have (ior (and A B) C), apply the distributive law and then
6585 the inverse distributive law to see if things simplify. */
6587 if (GET_CODE (op0) == AND)
6589 rtx result = distribute_and_simplify_rtx (x, 0);
6590 if (result)
6591 return result;
6594 if (GET_CODE (op1) == AND)
6596 rtx result = distribute_and_simplify_rtx (x, 1);
6597 if (result)
6598 return result;
6600 break;
6602 default:
6603 gcc_unreachable ();
6606 return x;
6609 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6610 operations" because they can be replaced with two more basic operations.
6611 ZERO_EXTEND is also considered "compound" because it can be replaced with
6612 an AND operation, which is simpler, though only one operation.
6614 The function expand_compound_operation is called with an rtx expression
6615 and will convert it to the appropriate shifts and AND operations,
6616 simplifying at each stage.
6618 The function make_compound_operation is called to convert an expression
6619 consisting of shifts and ANDs into the equivalent compound expression.
6620 It is the inverse of this function, loosely speaking. */
6622 static rtx
6623 expand_compound_operation (rtx x)
6625 unsigned HOST_WIDE_INT pos = 0, len;
6626 int unsignedp = 0;
6627 unsigned int modewidth;
6628 rtx tem;
6630 switch (GET_CODE (x))
6632 case ZERO_EXTEND:
6633 unsignedp = 1;
6634 case SIGN_EXTEND:
6635 /* We can't necessarily use a const_int for a multiword mode;
6636 it depends on implicitly extending the value.
6637 Since we don't know the right way to extend it,
6638 we can't tell whether the implicit way is right.
6640 Even for a mode that is no wider than a const_int,
6641 we can't win, because we need to sign extend one of its bits through
6642 the rest of it, and we don't know which bit. */
6643 if (CONST_INT_P (XEXP (x, 0)))
6644 return x;
6646 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6647 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6648 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6649 reloaded. If not for that, MEM's would very rarely be safe.
6651 Reject MODEs bigger than a word, because we might not be able
6652 to reference a two-register group starting with an arbitrary register
6653 (and currently gen_lowpart might crash for a SUBREG). */
6655 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6656 return x;
6658 /* Reject MODEs that aren't scalar integers because turning vector
6659 or complex modes into shifts causes problems. */
6661 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6662 return x;
6664 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6665 /* If the inner object has VOIDmode (the only way this can happen
6666 is if it is an ASM_OPERANDS), we can't do anything since we don't
6667 know how much masking to do. */
6668 if (len == 0)
6669 return x;
6671 break;
6673 case ZERO_EXTRACT:
6674 unsignedp = 1;
6676 /* ... fall through ... */
6678 case SIGN_EXTRACT:
6679 /* If the operand is a CLOBBER, just return it. */
6680 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6681 return XEXP (x, 0);
6683 if (!CONST_INT_P (XEXP (x, 1))
6684 || !CONST_INT_P (XEXP (x, 2))
6685 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6686 return x;
6688 /* Reject MODEs that aren't scalar integers because turning vector
6689 or complex modes into shifts causes problems. */
6691 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6692 return x;
6694 len = INTVAL (XEXP (x, 1));
6695 pos = INTVAL (XEXP (x, 2));
6697 /* This should stay within the object being extracted, fail otherwise. */
6698 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6699 return x;
6701 if (BITS_BIG_ENDIAN)
6702 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6704 break;
6706 default:
6707 return x;
6709 /* Convert sign extension to zero extension, if we know that the high
6710 bit is not set, as this is easier to optimize. It will be converted
6711 back to cheaper alternative in make_extraction. */
6712 if (GET_CODE (x) == SIGN_EXTEND
6713 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6714 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6715 & ~(((unsigned HOST_WIDE_INT)
6716 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6717 >> 1))
6718 == 0)))
6720 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6721 rtx temp2 = expand_compound_operation (temp);
6723 /* Make sure this is a profitable operation. */
6724 if (rtx_cost (x, SET, optimize_this_for_speed_p)
6725 > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6726 return temp2;
6727 else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6728 > rtx_cost (temp, SET, optimize_this_for_speed_p))
6729 return temp;
6730 else
6731 return x;
6734 /* We can optimize some special cases of ZERO_EXTEND. */
6735 if (GET_CODE (x) == ZERO_EXTEND)
6737 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6738 know that the last value didn't have any inappropriate bits
6739 set. */
6740 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6741 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6742 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6743 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6744 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6745 return XEXP (XEXP (x, 0), 0);
6747 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6748 if (GET_CODE (XEXP (x, 0)) == SUBREG
6749 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6750 && subreg_lowpart_p (XEXP (x, 0))
6751 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6752 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6753 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6754 return SUBREG_REG (XEXP (x, 0));
6756 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6757 is a comparison and STORE_FLAG_VALUE permits. This is like
6758 the first case, but it works even when GET_MODE (x) is larger
6759 than HOST_WIDE_INT. */
6760 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6761 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6762 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6763 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6764 <= HOST_BITS_PER_WIDE_INT)
6765 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6766 return XEXP (XEXP (x, 0), 0);
6768 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6769 if (GET_CODE (XEXP (x, 0)) == SUBREG
6770 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6771 && subreg_lowpart_p (XEXP (x, 0))
6772 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6773 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6774 <= HOST_BITS_PER_WIDE_INT)
6775 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6776 return SUBREG_REG (XEXP (x, 0));
6780 /* If we reach here, we want to return a pair of shifts. The inner
6781 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6782 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6783 logical depending on the value of UNSIGNEDP.
6785 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6786 converted into an AND of a shift.
6788 We must check for the case where the left shift would have a negative
6789 count. This can happen in a case like (x >> 31) & 255 on machines
6790 that can't shift by a constant. On those machines, we would first
6791 combine the shift with the AND to produce a variable-position
6792 extraction. Then the constant of 31 would be substituted in
6793 to produce such a position. */
6795 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6796 if (modewidth >= pos + len)
6798 enum machine_mode mode = GET_MODE (x);
6799 tem = gen_lowpart (mode, XEXP (x, 0));
6800 if (!tem || GET_CODE (tem) == CLOBBER)
6801 return x;
6802 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6803 tem, modewidth - pos - len);
6804 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6805 mode, tem, modewidth - len);
6807 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6808 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6809 simplify_shift_const (NULL_RTX, LSHIFTRT,
6810 GET_MODE (x),
6811 XEXP (x, 0), pos),
6812 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6813 else
6814 /* Any other cases we can't handle. */
6815 return x;
6817 /* If we couldn't do this for some reason, return the original
6818 expression. */
6819 if (GET_CODE (tem) == CLOBBER)
6820 return x;
6822 return tem;
6825 /* X is a SET which contains an assignment of one object into
6826 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6827 or certain SUBREGS). If possible, convert it into a series of
6828 logical operations.
6830 We half-heartedly support variable positions, but do not at all
6831 support variable lengths. */
6833 static const_rtx
6834 expand_field_assignment (const_rtx x)
6836 rtx inner;
6837 rtx pos; /* Always counts from low bit. */
6838 int len;
6839 rtx mask, cleared, masked;
6840 enum machine_mode compute_mode;
6842 /* Loop until we find something we can't simplify. */
6843 while (1)
6845 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6846 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6848 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6849 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6850 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6852 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6853 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6855 inner = XEXP (SET_DEST (x), 0);
6856 len = INTVAL (XEXP (SET_DEST (x), 1));
6857 pos = XEXP (SET_DEST (x), 2);
6859 /* A constant position should stay within the width of INNER. */
6860 if (CONST_INT_P (pos)
6861 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6862 break;
6864 if (BITS_BIG_ENDIAN)
6866 if (CONST_INT_P (pos))
6867 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6868 - INTVAL (pos));
6869 else if (GET_CODE (pos) == MINUS
6870 && CONST_INT_P (XEXP (pos, 1))
6871 && (INTVAL (XEXP (pos, 1))
6872 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6873 /* If position is ADJUST - X, new position is X. */
6874 pos = XEXP (pos, 0);
6875 else
6876 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6877 GEN_INT (GET_MODE_BITSIZE (
6878 GET_MODE (inner))
6879 - len),
6880 pos);
6884 /* A SUBREG between two modes that occupy the same numbers of words
6885 can be done by moving the SUBREG to the source. */
6886 else if (GET_CODE (SET_DEST (x)) == SUBREG
6887 /* We need SUBREGs to compute nonzero_bits properly. */
6888 && nonzero_sign_valid
6889 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6890 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6891 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6892 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6894 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6895 gen_lowpart
6896 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6897 SET_SRC (x)));
6898 continue;
6900 else
6901 break;
6903 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6904 inner = SUBREG_REG (inner);
6906 compute_mode = GET_MODE (inner);
6908 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6909 if (! SCALAR_INT_MODE_P (compute_mode))
6911 enum machine_mode imode;
6913 /* Don't do anything for vector or complex integral types. */
6914 if (! FLOAT_MODE_P (compute_mode))
6915 break;
6917 /* Try to find an integral mode to pun with. */
6918 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6919 if (imode == BLKmode)
6920 break;
6922 compute_mode = imode;
6923 inner = gen_lowpart (imode, inner);
6926 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6927 if (len >= HOST_BITS_PER_WIDE_INT)
6928 break;
6930 /* Now compute the equivalent expression. Make a copy of INNER
6931 for the SET_DEST in case it is a MEM into which we will substitute;
6932 we don't want shared RTL in that case. */
6933 mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
6934 cleared = simplify_gen_binary (AND, compute_mode,
6935 simplify_gen_unary (NOT, compute_mode,
6936 simplify_gen_binary (ASHIFT,
6937 compute_mode,
6938 mask, pos),
6939 compute_mode),
6940 inner);
6941 masked = simplify_gen_binary (ASHIFT, compute_mode,
6942 simplify_gen_binary (
6943 AND, compute_mode,
6944 gen_lowpart (compute_mode, SET_SRC (x)),
6945 mask),
6946 pos);
6948 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6949 simplify_gen_binary (IOR, compute_mode,
6950 cleared, masked));
6953 return x;
6956 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6957 it is an RTX that represents a variable starting position; otherwise,
6958 POS is the (constant) starting bit position (counted from the LSB).
6960 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6961 signed reference.
6963 IN_DEST is nonzero if this is a reference in the destination of a
6964 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6965 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6966 be used.
6968 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6969 ZERO_EXTRACT should be built even for bits starting at bit 0.
6971 MODE is the desired mode of the result (if IN_DEST == 0).
6973 The result is an RTX for the extraction or NULL_RTX if the target
6974 can't handle it. */
6976 static rtx
6977 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6978 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6979 int in_dest, int in_compare)
6981 /* This mode describes the size of the storage area
6982 to fetch the overall value from. Within that, we
6983 ignore the POS lowest bits, etc. */
6984 enum machine_mode is_mode = GET_MODE (inner);
6985 enum machine_mode inner_mode;
6986 enum machine_mode wanted_inner_mode;
6987 enum machine_mode wanted_inner_reg_mode = word_mode;
6988 enum machine_mode pos_mode = word_mode;
6989 enum machine_mode extraction_mode = word_mode;
6990 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6991 rtx new_rtx = 0;
6992 rtx orig_pos_rtx = pos_rtx;
6993 HOST_WIDE_INT orig_pos;
6995 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6997 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6998 consider just the QI as the memory to extract from.
6999 The subreg adds or removes high bits; its mode is
7000 irrelevant to the meaning of this extraction,
7001 since POS and LEN count from the lsb. */
7002 if (MEM_P (SUBREG_REG (inner)))
7003 is_mode = GET_MODE (SUBREG_REG (inner));
7004 inner = SUBREG_REG (inner);
7006 else if (GET_CODE (inner) == ASHIFT
7007 && CONST_INT_P (XEXP (inner, 1))
7008 && pos_rtx == 0 && pos == 0
7009 && len > UINTVAL (XEXP (inner, 1)))
7011 /* We're extracting the least significant bits of an rtx
7012 (ashift X (const_int C)), where LEN > C. Extract the
7013 least significant (LEN - C) bits of X, giving an rtx
7014 whose mode is MODE, then shift it left C times. */
7015 new_rtx = make_extraction (mode, XEXP (inner, 0),
7016 0, 0, len - INTVAL (XEXP (inner, 1)),
7017 unsignedp, in_dest, in_compare);
7018 if (new_rtx != 0)
7019 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7022 inner_mode = GET_MODE (inner);
7024 if (pos_rtx && CONST_INT_P (pos_rtx))
7025 pos = INTVAL (pos_rtx), pos_rtx = 0;
7027 /* See if this can be done without an extraction. We never can if the
7028 width of the field is not the same as that of some integer mode. For
7029 registers, we can only avoid the extraction if the position is at the
7030 low-order bit and this is either not in the destination or we have the
7031 appropriate STRICT_LOW_PART operation available.
7033 For MEM, we can avoid an extract if the field starts on an appropriate
7034 boundary and we can change the mode of the memory reference. */
7036 if (tmode != BLKmode
7037 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7038 && !MEM_P (inner)
7039 && (inner_mode == tmode
7040 || !REG_P (inner)
7041 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
7042 GET_MODE_BITSIZE (inner_mode))
7043 || reg_truncated_to_mode (tmode, inner))
7044 && (! in_dest
7045 || (REG_P (inner)
7046 && have_insn_for (STRICT_LOW_PART, tmode))))
7047 || (MEM_P (inner) && pos_rtx == 0
7048 && (pos
7049 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7050 : BITS_PER_UNIT)) == 0
7051 /* We can't do this if we are widening INNER_MODE (it
7052 may not be aligned, for one thing). */
7053 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
7054 && (inner_mode == tmode
7055 || (! mode_dependent_address_p (XEXP (inner, 0))
7056 && ! MEM_VOLATILE_P (inner))))))
7058 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7059 field. If the original and current mode are the same, we need not
7060 adjust the offset. Otherwise, we do if bytes big endian.
7062 If INNER is not a MEM, get a piece consisting of just the field
7063 of interest (in this case POS % BITS_PER_WORD must be 0). */
7065 if (MEM_P (inner))
7067 HOST_WIDE_INT offset;
7069 /* POS counts from lsb, but make OFFSET count in memory order. */
7070 if (BYTES_BIG_ENDIAN)
7071 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
7072 else
7073 offset = pos / BITS_PER_UNIT;
7075 new_rtx = adjust_address_nv (inner, tmode, offset);
7077 else if (REG_P (inner))
7079 if (tmode != inner_mode)
7081 /* We can't call gen_lowpart in a DEST since we
7082 always want a SUBREG (see below) and it would sometimes
7083 return a new hard register. */
7084 if (pos || in_dest)
7086 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7088 if (WORDS_BIG_ENDIAN
7089 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7090 final_word = ((GET_MODE_SIZE (inner_mode)
7091 - GET_MODE_SIZE (tmode))
7092 / UNITS_PER_WORD) - final_word;
7094 final_word *= UNITS_PER_WORD;
7095 if (BYTES_BIG_ENDIAN &&
7096 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7097 final_word += (GET_MODE_SIZE (inner_mode)
7098 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7100 /* Avoid creating invalid subregs, for example when
7101 simplifying (x>>32)&255. */
7102 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7103 return NULL_RTX;
7105 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7107 else
7108 new_rtx = gen_lowpart (tmode, inner);
7110 else
7111 new_rtx = inner;
7113 else
7114 new_rtx = force_to_mode (inner, tmode,
7115 len >= HOST_BITS_PER_WIDE_INT
7116 ? ~(unsigned HOST_WIDE_INT) 0
7117 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7120 /* If this extraction is going into the destination of a SET,
7121 make a STRICT_LOW_PART unless we made a MEM. */
7123 if (in_dest)
7124 return (MEM_P (new_rtx) ? new_rtx
7125 : (GET_CODE (new_rtx) != SUBREG
7126 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7127 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7129 if (mode == tmode)
7130 return new_rtx;
7132 if (CONST_INT_P (new_rtx)
7133 || GET_CODE (new_rtx) == CONST_DOUBLE)
7134 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7135 mode, new_rtx, tmode);
7137 /* If we know that no extraneous bits are set, and that the high
7138 bit is not set, convert the extraction to the cheaper of
7139 sign and zero extension, that are equivalent in these cases. */
7140 if (flag_expensive_optimizations
7141 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
7142 && ((nonzero_bits (new_rtx, tmode)
7143 & ~(((unsigned HOST_WIDE_INT)
7144 GET_MODE_MASK (tmode))
7145 >> 1))
7146 == 0)))
7148 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7149 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7151 /* Prefer ZERO_EXTENSION, since it gives more information to
7152 backends. */
7153 if (rtx_cost (temp, SET, optimize_this_for_speed_p)
7154 <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
7155 return temp;
7156 return temp1;
7159 /* Otherwise, sign- or zero-extend unless we already are in the
7160 proper mode. */
7162 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7163 mode, new_rtx));
7166 /* Unless this is a COMPARE or we have a funny memory reference,
7167 don't do anything with zero-extending field extracts starting at
7168 the low-order bit since they are simple AND operations. */
7169 if (pos_rtx == 0 && pos == 0 && ! in_dest
7170 && ! in_compare && unsignedp)
7171 return 0;
7173 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7174 if the position is not a constant and the length is not 1. In all
7175 other cases, we would only be going outside our object in cases when
7176 an original shift would have been undefined. */
7177 if (MEM_P (inner)
7178 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
7179 || (pos_rtx != 0 && len != 1)))
7180 return 0;
7182 /* Get the mode to use should INNER not be a MEM, the mode for the position,
7183 and the mode for the result. */
7184 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7186 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7187 pos_mode = mode_for_extraction (EP_insv, 2);
7188 extraction_mode = mode_for_extraction (EP_insv, 3);
7191 if (! in_dest && unsignedp
7192 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7194 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7195 pos_mode = mode_for_extraction (EP_extzv, 3);
7196 extraction_mode = mode_for_extraction (EP_extzv, 0);
7199 if (! in_dest && ! unsignedp
7200 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7202 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7203 pos_mode = mode_for_extraction (EP_extv, 3);
7204 extraction_mode = mode_for_extraction (EP_extv, 0);
7207 /* Never narrow an object, since that might not be safe. */
7209 if (mode != VOIDmode
7210 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7211 extraction_mode = mode;
7213 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7214 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7215 pos_mode = GET_MODE (pos_rtx);
7217 /* If this is not from memory, the desired mode is the preferred mode
7218 for an extraction pattern's first input operand, or word_mode if there
7219 is none. */
7220 if (!MEM_P (inner))
7221 wanted_inner_mode = wanted_inner_reg_mode;
7222 else
7224 /* Be careful not to go beyond the extracted object and maintain the
7225 natural alignment of the memory. */
7226 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7227 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7228 > GET_MODE_BITSIZE (wanted_inner_mode))
7230 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7231 gcc_assert (wanted_inner_mode != VOIDmode);
7234 /* If we have to change the mode of memory and cannot, the desired mode
7235 is EXTRACTION_MODE. */
7236 if (inner_mode != wanted_inner_mode
7237 && (mode_dependent_address_p (XEXP (inner, 0))
7238 || MEM_VOLATILE_P (inner)
7239 || pos_rtx))
7240 wanted_inner_mode = extraction_mode;
7243 orig_pos = pos;
7245 if (BITS_BIG_ENDIAN)
7247 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7248 BITS_BIG_ENDIAN style. If position is constant, compute new
7249 position. Otherwise, build subtraction.
7250 Note that POS is relative to the mode of the original argument.
7251 If it's a MEM we need to recompute POS relative to that.
7252 However, if we're extracting from (or inserting into) a register,
7253 we want to recompute POS relative to wanted_inner_mode. */
7254 int width = (MEM_P (inner)
7255 ? GET_MODE_BITSIZE (is_mode)
7256 : GET_MODE_BITSIZE (wanted_inner_mode));
7258 if (pos_rtx == 0)
7259 pos = width - len - pos;
7260 else
7261 pos_rtx
7262 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7263 /* POS may be less than 0 now, but we check for that below.
7264 Note that it can only be less than 0 if !MEM_P (inner). */
7267 /* If INNER has a wider mode, and this is a constant extraction, try to
7268 make it smaller and adjust the byte to point to the byte containing
7269 the value. */
7270 if (wanted_inner_mode != VOIDmode
7271 && inner_mode != wanted_inner_mode
7272 && ! pos_rtx
7273 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7274 && MEM_P (inner)
7275 && ! mode_dependent_address_p (XEXP (inner, 0))
7276 && ! MEM_VOLATILE_P (inner))
7278 int offset = 0;
7280 /* The computations below will be correct if the machine is big
7281 endian in both bits and bytes or little endian in bits and bytes.
7282 If it is mixed, we must adjust. */
7284 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7285 adjust OFFSET to compensate. */
7286 if (BYTES_BIG_ENDIAN
7287 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7288 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7290 /* We can now move to the desired byte. */
7291 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7292 * GET_MODE_SIZE (wanted_inner_mode);
7293 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7295 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7296 && is_mode != wanted_inner_mode)
7297 offset = (GET_MODE_SIZE (is_mode)
7298 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7300 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7303 /* If INNER is not memory, get it into the proper mode. If we are changing
7304 its mode, POS must be a constant and smaller than the size of the new
7305 mode. */
7306 else if (!MEM_P (inner))
7308 /* On the LHS, don't create paradoxical subregs implicitely truncating
7309 the register unless TRULY_NOOP_TRUNCATION. */
7310 if (in_dest
7311 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
7312 GET_MODE_BITSIZE (wanted_inner_mode)))
7313 return NULL_RTX;
7315 if (GET_MODE (inner) != wanted_inner_mode
7316 && (pos_rtx != 0
7317 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7318 return NULL_RTX;
7320 if (orig_pos < 0)
7321 return NULL_RTX;
7323 inner = force_to_mode (inner, wanted_inner_mode,
7324 pos_rtx
7325 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7326 ? ~(unsigned HOST_WIDE_INT) 0
7327 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7328 << orig_pos),
7332 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7333 have to zero extend. Otherwise, we can just use a SUBREG. */
7334 if (pos_rtx != 0
7335 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7337 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7339 /* If we know that no extraneous bits are set, and that the high
7340 bit is not set, convert extraction to cheaper one - either
7341 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7342 cases. */
7343 if (flag_expensive_optimizations
7344 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
7345 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7346 & ~(((unsigned HOST_WIDE_INT)
7347 GET_MODE_MASK (GET_MODE (pos_rtx)))
7348 >> 1))
7349 == 0)))
7351 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7353 /* Prefer ZERO_EXTENSION, since it gives more information to
7354 backends. */
7355 if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
7356 < rtx_cost (temp, SET, optimize_this_for_speed_p))
7357 temp = temp1;
7359 pos_rtx = temp;
7361 else if (pos_rtx != 0
7362 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7363 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7365 /* Make POS_RTX unless we already have it and it is correct. If we don't
7366 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7367 be a CONST_INT. */
7368 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7369 pos_rtx = orig_pos_rtx;
7371 else if (pos_rtx == 0)
7372 pos_rtx = GEN_INT (pos);
7374 /* Make the required operation. See if we can use existing rtx. */
7375 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7376 extraction_mode, inner, GEN_INT (len), pos_rtx);
7377 if (! in_dest)
7378 new_rtx = gen_lowpart (mode, new_rtx);
7380 return new_rtx;
7383 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7384 with any other operations in X. Return X without that shift if so. */
7386 static rtx
7387 extract_left_shift (rtx x, int count)
7389 enum rtx_code code = GET_CODE (x);
7390 enum machine_mode mode = GET_MODE (x);
7391 rtx tem;
7393 switch (code)
7395 case ASHIFT:
7396 /* This is the shift itself. If it is wide enough, we will return
7397 either the value being shifted if the shift count is equal to
7398 COUNT or a shift for the difference. */
7399 if (CONST_INT_P (XEXP (x, 1))
7400 && INTVAL (XEXP (x, 1)) >= count)
7401 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7402 INTVAL (XEXP (x, 1)) - count);
7403 break;
7405 case NEG: case NOT:
7406 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7407 return simplify_gen_unary (code, mode, tem, mode);
7409 break;
7411 case PLUS: case IOR: case XOR: case AND:
7412 /* If we can safely shift this constant and we find the inner shift,
7413 make a new operation. */
7414 if (CONST_INT_P (XEXP (x, 1))
7415 && (UINTVAL (XEXP (x, 1))
7416 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7417 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7418 return simplify_gen_binary (code, mode, tem,
7419 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7421 break;
7423 default:
7424 break;
7427 return 0;
7430 /* Look at the expression rooted at X. Look for expressions
7431 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7432 Form these expressions.
7434 Return the new rtx, usually just X.
7436 Also, for machines like the VAX that don't have logical shift insns,
7437 try to convert logical to arithmetic shift operations in cases where
7438 they are equivalent. This undoes the canonicalizations to logical
7439 shifts done elsewhere.
7441 We try, as much as possible, to re-use rtl expressions to save memory.
7443 IN_CODE says what kind of expression we are processing. Normally, it is
7444 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7445 being kludges), it is MEM. When processing the arguments of a comparison
7446 or a COMPARE against zero, it is COMPARE. */
7448 static rtx
7449 make_compound_operation (rtx x, enum rtx_code in_code)
7451 enum rtx_code code = GET_CODE (x);
7452 enum machine_mode mode = GET_MODE (x);
7453 int mode_width = GET_MODE_BITSIZE (mode);
7454 rtx rhs, lhs;
7455 enum rtx_code next_code;
7456 int i, j;
7457 rtx new_rtx = 0;
7458 rtx tem;
7459 const char *fmt;
7461 /* Select the code to be used in recursive calls. Once we are inside an
7462 address, we stay there. If we have a comparison, set to COMPARE,
7463 but once inside, go back to our default of SET. */
7465 next_code = (code == MEM ? MEM
7466 : ((code == PLUS || code == MINUS)
7467 && SCALAR_INT_MODE_P (mode)) ? MEM
7468 : ((code == COMPARE || COMPARISON_P (x))
7469 && XEXP (x, 1) == const0_rtx) ? COMPARE
7470 : in_code == COMPARE ? SET : in_code);
7472 /* Process depending on the code of this operation. If NEW is set
7473 nonzero, it will be returned. */
7475 switch (code)
7477 case ASHIFT:
7478 /* Convert shifts by constants into multiplications if inside
7479 an address. */
7480 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7481 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7482 && INTVAL (XEXP (x, 1)) >= 0)
7484 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7485 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7487 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7488 if (GET_CODE (new_rtx) == NEG)
7490 new_rtx = XEXP (new_rtx, 0);
7491 multval = -multval;
7493 multval = trunc_int_for_mode (multval, mode);
7494 new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7496 break;
7498 case PLUS:
7499 lhs = XEXP (x, 0);
7500 rhs = XEXP (x, 1);
7501 lhs = make_compound_operation (lhs, next_code);
7502 rhs = make_compound_operation (rhs, next_code);
7503 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7504 && SCALAR_INT_MODE_P (mode))
7506 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7507 XEXP (lhs, 1));
7508 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7510 else if (GET_CODE (lhs) == MULT
7511 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7513 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7514 simplify_gen_unary (NEG, mode,
7515 XEXP (lhs, 1),
7516 mode));
7517 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7519 else
7521 SUBST (XEXP (x, 0), lhs);
7522 SUBST (XEXP (x, 1), rhs);
7523 goto maybe_swap;
7525 x = gen_lowpart (mode, new_rtx);
7526 goto maybe_swap;
7528 case MINUS:
7529 lhs = XEXP (x, 0);
7530 rhs = XEXP (x, 1);
7531 lhs = make_compound_operation (lhs, next_code);
7532 rhs = make_compound_operation (rhs, next_code);
7533 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7534 && SCALAR_INT_MODE_P (mode))
7536 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7537 XEXP (rhs, 1));
7538 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7540 else if (GET_CODE (rhs) == MULT
7541 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7543 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7544 simplify_gen_unary (NEG, mode,
7545 XEXP (rhs, 1),
7546 mode));
7547 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7549 else
7551 SUBST (XEXP (x, 0), lhs);
7552 SUBST (XEXP (x, 1), rhs);
7553 return x;
7555 return gen_lowpart (mode, new_rtx);
7557 case AND:
7558 /* If the second operand is not a constant, we can't do anything
7559 with it. */
7560 if (!CONST_INT_P (XEXP (x, 1)))
7561 break;
7563 /* If the constant is a power of two minus one and the first operand
7564 is a logical right shift, make an extraction. */
7565 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7566 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7568 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7569 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7570 0, in_code == COMPARE);
7573 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7574 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7575 && subreg_lowpart_p (XEXP (x, 0))
7576 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7577 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7579 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7580 next_code);
7581 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7582 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7583 0, in_code == COMPARE);
7585 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7586 else if ((GET_CODE (XEXP (x, 0)) == XOR
7587 || GET_CODE (XEXP (x, 0)) == IOR)
7588 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7589 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7590 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7592 /* Apply the distributive law, and then try to make extractions. */
7593 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7594 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7595 XEXP (x, 1)),
7596 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7597 XEXP (x, 1)));
7598 new_rtx = make_compound_operation (new_rtx, in_code);
7601 /* If we are have (and (rotate X C) M) and C is larger than the number
7602 of bits in M, this is an extraction. */
7604 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7605 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7606 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7607 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7609 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7610 new_rtx = make_extraction (mode, new_rtx,
7611 (GET_MODE_BITSIZE (mode)
7612 - INTVAL (XEXP (XEXP (x, 0), 1))),
7613 NULL_RTX, i, 1, 0, in_code == COMPARE);
7616 /* On machines without logical shifts, if the operand of the AND is
7617 a logical shift and our mask turns off all the propagated sign
7618 bits, we can replace the logical shift with an arithmetic shift. */
7619 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7620 && !have_insn_for (LSHIFTRT, mode)
7621 && have_insn_for (ASHIFTRT, mode)
7622 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7623 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7624 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7625 && mode_width <= HOST_BITS_PER_WIDE_INT)
7627 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7629 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7630 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7631 SUBST (XEXP (x, 0),
7632 gen_rtx_ASHIFTRT (mode,
7633 make_compound_operation
7634 (XEXP (XEXP (x, 0), 0), next_code),
7635 XEXP (XEXP (x, 0), 1)));
7638 /* If the constant is one less than a power of two, this might be
7639 representable by an extraction even if no shift is present.
7640 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7641 we are in a COMPARE. */
7642 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7643 new_rtx = make_extraction (mode,
7644 make_compound_operation (XEXP (x, 0),
7645 next_code),
7646 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7648 /* If we are in a comparison and this is an AND with a power of two,
7649 convert this into the appropriate bit extract. */
7650 else if (in_code == COMPARE
7651 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7652 new_rtx = make_extraction (mode,
7653 make_compound_operation (XEXP (x, 0),
7654 next_code),
7655 i, NULL_RTX, 1, 1, 0, 1);
7657 break;
7659 case LSHIFTRT:
7660 /* If the sign bit is known to be zero, replace this with an
7661 arithmetic shift. */
7662 if (have_insn_for (ASHIFTRT, mode)
7663 && ! have_insn_for (LSHIFTRT, mode)
7664 && mode_width <= HOST_BITS_PER_WIDE_INT
7665 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7667 new_rtx = gen_rtx_ASHIFTRT (mode,
7668 make_compound_operation (XEXP (x, 0),
7669 next_code),
7670 XEXP (x, 1));
7671 break;
7674 /* ... fall through ... */
7676 case ASHIFTRT:
7677 lhs = XEXP (x, 0);
7678 rhs = XEXP (x, 1);
7680 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7681 this is a SIGN_EXTRACT. */
7682 if (CONST_INT_P (rhs)
7683 && GET_CODE (lhs) == ASHIFT
7684 && CONST_INT_P (XEXP (lhs, 1))
7685 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7686 && INTVAL (rhs) < mode_width)
7688 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7689 new_rtx = make_extraction (mode, new_rtx,
7690 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7691 NULL_RTX, mode_width - INTVAL (rhs),
7692 code == LSHIFTRT, 0, in_code == COMPARE);
7693 break;
7696 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7697 If so, try to merge the shifts into a SIGN_EXTEND. We could
7698 also do this for some cases of SIGN_EXTRACT, but it doesn't
7699 seem worth the effort; the case checked for occurs on Alpha. */
7701 if (!OBJECT_P (lhs)
7702 && ! (GET_CODE (lhs) == SUBREG
7703 && (OBJECT_P (SUBREG_REG (lhs))))
7704 && CONST_INT_P (rhs)
7705 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7706 && INTVAL (rhs) < mode_width
7707 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7708 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7709 0, NULL_RTX, mode_width - INTVAL (rhs),
7710 code == LSHIFTRT, 0, in_code == COMPARE);
7712 break;
7714 case SUBREG:
7715 /* Call ourselves recursively on the inner expression. If we are
7716 narrowing the object and it has a different RTL code from
7717 what it originally did, do this SUBREG as a force_to_mode. */
7719 rtx inner = SUBREG_REG (x), simplified;
7721 tem = make_compound_operation (inner, in_code);
7723 simplified
7724 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7725 if (simplified)
7726 tem = simplified;
7728 if (GET_CODE (tem) != GET_CODE (inner)
7729 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7730 && subreg_lowpart_p (x))
7732 rtx newer
7733 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7735 /* If we have something other than a SUBREG, we might have
7736 done an expansion, so rerun ourselves. */
7737 if (GET_CODE (newer) != SUBREG)
7738 newer = make_compound_operation (newer, in_code);
7740 /* force_to_mode can expand compounds. If it just re-expanded the
7741 compound, use gen_lowpart to convert to the desired mode. */
7742 if (rtx_equal_p (newer, x)
7743 /* Likewise if it re-expanded the compound only partially.
7744 This happens for SUBREG of ZERO_EXTRACT if they extract
7745 the same number of bits. */
7746 || (GET_CODE (newer) == SUBREG
7747 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7748 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7749 && GET_CODE (inner) == AND
7750 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7751 return gen_lowpart (GET_MODE (x), tem);
7753 return newer;
7756 if (simplified)
7757 return tem;
7759 break;
7761 default:
7762 break;
7765 if (new_rtx)
7767 x = gen_lowpart (mode, new_rtx);
7768 code = GET_CODE (x);
7771 /* Now recursively process each operand of this operation. */
7772 fmt = GET_RTX_FORMAT (code);
7773 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7774 if (fmt[i] == 'e')
7776 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7777 SUBST (XEXP (x, i), new_rtx);
7779 else if (fmt[i] == 'E')
7780 for (j = 0; j < XVECLEN (x, i); j++)
7782 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7783 SUBST (XVECEXP (x, i, j), new_rtx);
7786 maybe_swap:
7787 /* If this is a commutative operation, the changes to the operands
7788 may have made it noncanonical. */
7789 if (COMMUTATIVE_ARITH_P (x)
7790 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7792 tem = XEXP (x, 0);
7793 SUBST (XEXP (x, 0), XEXP (x, 1));
7794 SUBST (XEXP (x, 1), tem);
7797 return x;
7800 /* Given M see if it is a value that would select a field of bits
7801 within an item, but not the entire word. Return -1 if not.
7802 Otherwise, return the starting position of the field, where 0 is the
7803 low-order bit.
7805 *PLEN is set to the length of the field. */
7807 static int
7808 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7810 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7811 int pos = m ? ctz_hwi (m) : -1;
7812 int len = 0;
7814 if (pos >= 0)
7815 /* Now shift off the low-order zero bits and see if we have a
7816 power of two minus 1. */
7817 len = exact_log2 ((m >> pos) + 1);
7819 if (len <= 0)
7820 pos = -1;
7822 *plen = len;
7823 return pos;
7826 /* If X refers to a register that equals REG in value, replace these
7827 references with REG. */
7828 static rtx
7829 canon_reg_for_combine (rtx x, rtx reg)
7831 rtx op0, op1, op2;
7832 const char *fmt;
7833 int i;
7834 bool copied;
7836 enum rtx_code code = GET_CODE (x);
7837 switch (GET_RTX_CLASS (code))
7839 case RTX_UNARY:
7840 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7841 if (op0 != XEXP (x, 0))
7842 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7843 GET_MODE (reg));
7844 break;
7846 case RTX_BIN_ARITH:
7847 case RTX_COMM_ARITH:
7848 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7849 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7850 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7851 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7852 break;
7854 case RTX_COMPARE:
7855 case RTX_COMM_COMPARE:
7856 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7857 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7858 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7859 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7860 GET_MODE (op0), op0, op1);
7861 break;
7863 case RTX_TERNARY:
7864 case RTX_BITFIELD_OPS:
7865 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7866 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7867 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7868 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7869 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7870 GET_MODE (op0), op0, op1, op2);
7872 case RTX_OBJ:
7873 if (REG_P (x))
7875 if (rtx_equal_p (get_last_value (reg), x)
7876 || rtx_equal_p (reg, get_last_value (x)))
7877 return reg;
7878 else
7879 break;
7882 /* fall through */
7884 default:
7885 fmt = GET_RTX_FORMAT (code);
7886 copied = false;
7887 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7888 if (fmt[i] == 'e')
7890 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7891 if (op != XEXP (x, i))
7893 if (!copied)
7895 copied = true;
7896 x = copy_rtx (x);
7898 XEXP (x, i) = op;
7901 else if (fmt[i] == 'E')
7903 int j;
7904 for (j = 0; j < XVECLEN (x, i); j++)
7906 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7907 if (op != XVECEXP (x, i, j))
7909 if (!copied)
7911 copied = true;
7912 x = copy_rtx (x);
7914 XVECEXP (x, i, j) = op;
7919 break;
7922 return x;
7925 /* Return X converted to MODE. If the value is already truncated to
7926 MODE we can just return a subreg even though in the general case we
7927 would need an explicit truncation. */
7929 static rtx
7930 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7932 if (!CONST_INT_P (x)
7933 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7934 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7935 GET_MODE_BITSIZE (GET_MODE (x)))
7936 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7938 /* Bit-cast X into an integer mode. */
7939 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7940 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7941 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7942 x, GET_MODE (x));
7945 return gen_lowpart (mode, x);
7948 /* See if X can be simplified knowing that we will only refer to it in
7949 MODE and will only refer to those bits that are nonzero in MASK.
7950 If other bits are being computed or if masking operations are done
7951 that select a superset of the bits in MASK, they can sometimes be
7952 ignored.
7954 Return a possibly simplified expression, but always convert X to
7955 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7957 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7958 are all off in X. This is used when X will be complemented, by either
7959 NOT, NEG, or XOR. */
7961 static rtx
7962 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7963 int just_select)
7965 enum rtx_code code = GET_CODE (x);
7966 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7967 enum machine_mode op_mode;
7968 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7969 rtx op0, op1, temp;
7971 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7972 code below will do the wrong thing since the mode of such an
7973 expression is VOIDmode.
7975 Also do nothing if X is a CLOBBER; this can happen if X was
7976 the return value from a call to gen_lowpart. */
7977 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7978 return x;
7980 /* We want to perform the operation is its present mode unless we know
7981 that the operation is valid in MODE, in which case we do the operation
7982 in MODE. */
7983 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7984 && have_insn_for (code, mode))
7985 ? mode : GET_MODE (x));
7987 /* It is not valid to do a right-shift in a narrower mode
7988 than the one it came in with. */
7989 if ((code == LSHIFTRT || code == ASHIFTRT)
7990 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7991 op_mode = GET_MODE (x);
7993 /* Truncate MASK to fit OP_MODE. */
7994 if (op_mode)
7995 mask &= GET_MODE_MASK (op_mode);
7997 /* When we have an arithmetic operation, or a shift whose count we
7998 do not know, we need to assume that all bits up to the highest-order
7999 bit in MASK will be needed. This is how we form such a mask. */
8000 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8001 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8002 else
8003 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8004 - 1);
8006 /* Determine what bits of X are guaranteed to be (non)zero. */
8007 nonzero = nonzero_bits (x, mode);
8009 /* If none of the bits in X are needed, return a zero. */
8010 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8011 x = const0_rtx;
8013 /* If X is a CONST_INT, return a new one. Do this here since the
8014 test below will fail. */
8015 if (CONST_INT_P (x))
8017 if (SCALAR_INT_MODE_P (mode))
8018 return gen_int_mode (INTVAL (x) & mask, mode);
8019 else
8021 x = GEN_INT (INTVAL (x) & mask);
8022 return gen_lowpart_common (mode, x);
8026 /* If X is narrower than MODE and we want all the bits in X's mode, just
8027 get X in the proper mode. */
8028 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8029 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8030 return gen_lowpart (mode, x);
8032 /* We can ignore the effect of a SUBREG if it narrows the mode or
8033 if the constant masks to zero all the bits the mode doesn't have. */
8034 if (GET_CODE (x) == SUBREG
8035 && subreg_lowpart_p (x)
8036 && ((GET_MODE_SIZE (GET_MODE (x))
8037 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8038 || (0 == (mask
8039 & GET_MODE_MASK (GET_MODE (x))
8040 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8041 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8043 /* The arithmetic simplifications here only work for scalar integer modes. */
8044 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8045 return gen_lowpart_or_truncate (mode, x);
8047 switch (code)
8049 case CLOBBER:
8050 /* If X is a (clobber (const_int)), return it since we know we are
8051 generating something that won't match. */
8052 return x;
8054 case SIGN_EXTEND:
8055 case ZERO_EXTEND:
8056 case ZERO_EXTRACT:
8057 case SIGN_EXTRACT:
8058 x = expand_compound_operation (x);
8059 if (GET_CODE (x) != code)
8060 return force_to_mode (x, mode, mask, next_select);
8061 break;
8063 case TRUNCATE:
8064 /* Similarly for a truncate. */
8065 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8067 case AND:
8068 /* If this is an AND with a constant, convert it into an AND
8069 whose constant is the AND of that constant with MASK. If it
8070 remains an AND of MASK, delete it since it is redundant. */
8072 if (CONST_INT_P (XEXP (x, 1)))
8074 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8075 mask & INTVAL (XEXP (x, 1)));
8077 /* If X is still an AND, see if it is an AND with a mask that
8078 is just some low-order bits. If so, and it is MASK, we don't
8079 need it. */
8081 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8082 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8083 == mask))
8084 x = XEXP (x, 0);
8086 /* If it remains an AND, try making another AND with the bits
8087 in the mode mask that aren't in MASK turned on. If the
8088 constant in the AND is wide enough, this might make a
8089 cheaper constant. */
8091 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8092 && GET_MODE_MASK (GET_MODE (x)) != mask
8093 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
8095 unsigned HOST_WIDE_INT cval
8096 = UINTVAL (XEXP (x, 1))
8097 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8098 int width = GET_MODE_BITSIZE (GET_MODE (x));
8099 rtx y;
8101 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8102 number, sign extend it. */
8103 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8104 && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8105 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8107 y = simplify_gen_binary (AND, GET_MODE (x),
8108 XEXP (x, 0), GEN_INT (cval));
8109 if (rtx_cost (y, SET, optimize_this_for_speed_p)
8110 < rtx_cost (x, SET, optimize_this_for_speed_p))
8111 x = y;
8114 break;
8117 goto binop;
8119 case PLUS:
8120 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8121 low-order bits (as in an alignment operation) and FOO is already
8122 aligned to that boundary, mask C1 to that boundary as well.
8123 This may eliminate that PLUS and, later, the AND. */
8126 unsigned int width = GET_MODE_BITSIZE (mode);
8127 unsigned HOST_WIDE_INT smask = mask;
8129 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8130 number, sign extend it. */
8132 if (width < HOST_BITS_PER_WIDE_INT
8133 && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8134 smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8136 if (CONST_INT_P (XEXP (x, 1))
8137 && exact_log2 (- smask) >= 0
8138 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8139 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8140 return force_to_mode (plus_constant (XEXP (x, 0),
8141 (INTVAL (XEXP (x, 1)) & smask)),
8142 mode, smask, next_select);
8145 /* ... fall through ... */
8147 case MULT:
8148 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8149 most significant bit in MASK since carries from those bits will
8150 affect the bits we are interested in. */
8151 mask = fuller_mask;
8152 goto binop;
8154 case MINUS:
8155 /* If X is (minus C Y) where C's least set bit is larger than any bit
8156 in the mask, then we may replace with (neg Y). */
8157 if (CONST_INT_P (XEXP (x, 0))
8158 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8159 & -INTVAL (XEXP (x, 0))))
8160 > mask))
8162 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8163 GET_MODE (x));
8164 return force_to_mode (x, mode, mask, next_select);
8167 /* Similarly, if C contains every bit in the fuller_mask, then we may
8168 replace with (not Y). */
8169 if (CONST_INT_P (XEXP (x, 0))
8170 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8172 x = simplify_gen_unary (NOT, GET_MODE (x),
8173 XEXP (x, 1), GET_MODE (x));
8174 return force_to_mode (x, mode, mask, next_select);
8177 mask = fuller_mask;
8178 goto binop;
8180 case IOR:
8181 case XOR:
8182 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8183 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8184 operation which may be a bitfield extraction. Ensure that the
8185 constant we form is not wider than the mode of X. */
8187 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8188 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8189 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8190 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8191 && CONST_INT_P (XEXP (x, 1))
8192 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8193 + floor_log2 (INTVAL (XEXP (x, 1))))
8194 < GET_MODE_BITSIZE (GET_MODE (x)))
8195 && (UINTVAL (XEXP (x, 1))
8196 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8198 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8199 << INTVAL (XEXP (XEXP (x, 0), 1)));
8200 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8201 XEXP (XEXP (x, 0), 0), temp);
8202 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8203 XEXP (XEXP (x, 0), 1));
8204 return force_to_mode (x, mode, mask, next_select);
8207 binop:
8208 /* For most binary operations, just propagate into the operation and
8209 change the mode if we have an operation of that mode. */
8211 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8212 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8214 /* If we ended up truncating both operands, truncate the result of the
8215 operation instead. */
8216 if (GET_CODE (op0) == TRUNCATE
8217 && GET_CODE (op1) == TRUNCATE)
8219 op0 = XEXP (op0, 0);
8220 op1 = XEXP (op1, 0);
8223 op0 = gen_lowpart_or_truncate (op_mode, op0);
8224 op1 = gen_lowpart_or_truncate (op_mode, op1);
8226 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8227 x = simplify_gen_binary (code, op_mode, op0, op1);
8228 break;
8230 case ASHIFT:
8231 /* For left shifts, do the same, but just for the first operand.
8232 However, we cannot do anything with shifts where we cannot
8233 guarantee that the counts are smaller than the size of the mode
8234 because such a count will have a different meaning in a
8235 wider mode. */
8237 if (! (CONST_INT_P (XEXP (x, 1))
8238 && INTVAL (XEXP (x, 1)) >= 0
8239 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
8240 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8241 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8242 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
8243 break;
8245 /* If the shift count is a constant and we can do arithmetic in
8246 the mode of the shift, refine which bits we need. Otherwise, use the
8247 conservative form of the mask. */
8248 if (CONST_INT_P (XEXP (x, 1))
8249 && INTVAL (XEXP (x, 1)) >= 0
8250 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
8251 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8252 mask >>= INTVAL (XEXP (x, 1));
8253 else
8254 mask = fuller_mask;
8256 op0 = gen_lowpart_or_truncate (op_mode,
8257 force_to_mode (XEXP (x, 0), op_mode,
8258 mask, next_select));
8260 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8261 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8262 break;
8264 case LSHIFTRT:
8265 /* Here we can only do something if the shift count is a constant,
8266 this shift constant is valid for the host, and we can do arithmetic
8267 in OP_MODE. */
8269 if (CONST_INT_P (XEXP (x, 1))
8270 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8271 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8273 rtx inner = XEXP (x, 0);
8274 unsigned HOST_WIDE_INT inner_mask;
8276 /* Select the mask of the bits we need for the shift operand. */
8277 inner_mask = mask << INTVAL (XEXP (x, 1));
8279 /* We can only change the mode of the shift if we can do arithmetic
8280 in the mode of the shift and INNER_MASK is no wider than the
8281 width of X's mode. */
8282 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8283 op_mode = GET_MODE (x);
8285 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8287 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8288 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8291 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8292 shift and AND produces only copies of the sign bit (C2 is one less
8293 than a power of two), we can do this with just a shift. */
8295 if (GET_CODE (x) == LSHIFTRT
8296 && CONST_INT_P (XEXP (x, 1))
8297 /* The shift puts one of the sign bit copies in the least significant
8298 bit. */
8299 && ((INTVAL (XEXP (x, 1))
8300 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8301 >= GET_MODE_BITSIZE (GET_MODE (x)))
8302 && exact_log2 (mask + 1) >= 0
8303 /* Number of bits left after the shift must be more than the mask
8304 needs. */
8305 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8306 <= GET_MODE_BITSIZE (GET_MODE (x)))
8307 /* Must be more sign bit copies than the mask needs. */
8308 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8309 >= exact_log2 (mask + 1)))
8310 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8311 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
8312 - exact_log2 (mask + 1)));
8314 goto shiftrt;
8316 case ASHIFTRT:
8317 /* If we are just looking for the sign bit, we don't need this shift at
8318 all, even if it has a variable count. */
8319 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8320 && (mask == ((unsigned HOST_WIDE_INT) 1
8321 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8322 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8324 /* If this is a shift by a constant, get a mask that contains those bits
8325 that are not copies of the sign bit. We then have two cases: If
8326 MASK only includes those bits, this can be a logical shift, which may
8327 allow simplifications. If MASK is a single-bit field not within
8328 those bits, we are requesting a copy of the sign bit and hence can
8329 shift the sign bit to the appropriate location. */
8331 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8332 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8334 int i;
8336 /* If the considered data is wider than HOST_WIDE_INT, we can't
8337 represent a mask for all its bits in a single scalar.
8338 But we only care about the lower bits, so calculate these. */
8340 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8342 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8344 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8345 is the number of bits a full-width mask would have set.
8346 We need only shift if these are fewer than nonzero can
8347 hold. If not, we must keep all bits set in nonzero. */
8349 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8350 < HOST_BITS_PER_WIDE_INT)
8351 nonzero >>= INTVAL (XEXP (x, 1))
8352 + HOST_BITS_PER_WIDE_INT
8353 - GET_MODE_BITSIZE (GET_MODE (x)) ;
8355 else
8357 nonzero = GET_MODE_MASK (GET_MODE (x));
8358 nonzero >>= INTVAL (XEXP (x, 1));
8361 if ((mask & ~nonzero) == 0)
8363 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8364 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8365 if (GET_CODE (x) != ASHIFTRT)
8366 return force_to_mode (x, mode, mask, next_select);
8369 else if ((i = exact_log2 (mask)) >= 0)
8371 x = simplify_shift_const
8372 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8373 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
8375 if (GET_CODE (x) != ASHIFTRT)
8376 return force_to_mode (x, mode, mask, next_select);
8380 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8381 even if the shift count isn't a constant. */
8382 if (mask == 1)
8383 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8384 XEXP (x, 0), XEXP (x, 1));
8386 shiftrt:
8388 /* If this is a zero- or sign-extension operation that just affects bits
8389 we don't care about, remove it. Be sure the call above returned
8390 something that is still a shift. */
8392 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8393 && CONST_INT_P (XEXP (x, 1))
8394 && INTVAL (XEXP (x, 1)) >= 0
8395 && (INTVAL (XEXP (x, 1))
8396 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
8397 && GET_CODE (XEXP (x, 0)) == ASHIFT
8398 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8399 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8400 next_select);
8402 break;
8404 case ROTATE:
8405 case ROTATERT:
8406 /* If the shift count is constant and we can do computations
8407 in the mode of X, compute where the bits we care about are.
8408 Otherwise, we can't do anything. Don't change the mode of
8409 the shift or propagate MODE into the shift, though. */
8410 if (CONST_INT_P (XEXP (x, 1))
8411 && INTVAL (XEXP (x, 1)) >= 0)
8413 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8414 GET_MODE (x), GEN_INT (mask),
8415 XEXP (x, 1));
8416 if (temp && CONST_INT_P (temp))
8417 SUBST (XEXP (x, 0),
8418 force_to_mode (XEXP (x, 0), GET_MODE (x),
8419 INTVAL (temp), next_select));
8421 break;
8423 case NEG:
8424 /* If we just want the low-order bit, the NEG isn't needed since it
8425 won't change the low-order bit. */
8426 if (mask == 1)
8427 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8429 /* We need any bits less significant than the most significant bit in
8430 MASK since carries from those bits will affect the bits we are
8431 interested in. */
8432 mask = fuller_mask;
8433 goto unop;
8435 case NOT:
8436 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8437 same as the XOR case above. Ensure that the constant we form is not
8438 wider than the mode of X. */
8440 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8441 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8442 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8443 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8444 < GET_MODE_BITSIZE (GET_MODE (x)))
8445 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8447 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8448 GET_MODE (x));
8449 temp = simplify_gen_binary (XOR, GET_MODE (x),
8450 XEXP (XEXP (x, 0), 0), temp);
8451 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8452 temp, XEXP (XEXP (x, 0), 1));
8454 return force_to_mode (x, mode, mask, next_select);
8457 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8458 use the full mask inside the NOT. */
8459 mask = fuller_mask;
8461 unop:
8462 op0 = gen_lowpart_or_truncate (op_mode,
8463 force_to_mode (XEXP (x, 0), mode, mask,
8464 next_select));
8465 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8466 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8467 break;
8469 case NE:
8470 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8471 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8472 which is equal to STORE_FLAG_VALUE. */
8473 if ((mask & ~STORE_FLAG_VALUE) == 0
8474 && XEXP (x, 1) == const0_rtx
8475 && GET_MODE (XEXP (x, 0)) == mode
8476 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8477 && (nonzero_bits (XEXP (x, 0), mode)
8478 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8479 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8481 break;
8483 case IF_THEN_ELSE:
8484 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8485 written in a narrower mode. We play it safe and do not do so. */
8487 SUBST (XEXP (x, 1),
8488 gen_lowpart_or_truncate (GET_MODE (x),
8489 force_to_mode (XEXP (x, 1), mode,
8490 mask, next_select)));
8491 SUBST (XEXP (x, 2),
8492 gen_lowpart_or_truncate (GET_MODE (x),
8493 force_to_mode (XEXP (x, 2), mode,
8494 mask, next_select)));
8495 break;
8497 default:
8498 break;
8501 /* Ensure we return a value of the proper mode. */
8502 return gen_lowpart_or_truncate (mode, x);
8505 /* Return nonzero if X is an expression that has one of two values depending on
8506 whether some other value is zero or nonzero. In that case, we return the
8507 value that is being tested, *PTRUE is set to the value if the rtx being
8508 returned has a nonzero value, and *PFALSE is set to the other alternative.
8510 If we return zero, we set *PTRUE and *PFALSE to X. */
8512 static rtx
8513 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8515 enum machine_mode mode = GET_MODE (x);
8516 enum rtx_code code = GET_CODE (x);
8517 rtx cond0, cond1, true0, true1, false0, false1;
8518 unsigned HOST_WIDE_INT nz;
8520 /* If we are comparing a value against zero, we are done. */
8521 if ((code == NE || code == EQ)
8522 && XEXP (x, 1) == const0_rtx)
8524 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8525 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8526 return XEXP (x, 0);
8529 /* If this is a unary operation whose operand has one of two values, apply
8530 our opcode to compute those values. */
8531 else if (UNARY_P (x)
8532 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8534 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8535 *pfalse = simplify_gen_unary (code, mode, false0,
8536 GET_MODE (XEXP (x, 0)));
8537 return cond0;
8540 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8541 make can't possibly match and would suppress other optimizations. */
8542 else if (code == COMPARE)
8545 /* If this is a binary operation, see if either side has only one of two
8546 values. If either one does or if both do and they are conditional on
8547 the same value, compute the new true and false values. */
8548 else if (BINARY_P (x))
8550 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8551 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8553 if ((cond0 != 0 || cond1 != 0)
8554 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8556 /* If if_then_else_cond returned zero, then true/false are the
8557 same rtl. We must copy one of them to prevent invalid rtl
8558 sharing. */
8559 if (cond0 == 0)
8560 true0 = copy_rtx (true0);
8561 else if (cond1 == 0)
8562 true1 = copy_rtx (true1);
8564 if (COMPARISON_P (x))
8566 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8567 true0, true1);
8568 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8569 false0, false1);
8571 else
8573 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8574 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8577 return cond0 ? cond0 : cond1;
8580 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8581 operands is zero when the other is nonzero, and vice-versa,
8582 and STORE_FLAG_VALUE is 1 or -1. */
8584 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8585 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8586 || code == UMAX)
8587 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8589 rtx op0 = XEXP (XEXP (x, 0), 1);
8590 rtx op1 = XEXP (XEXP (x, 1), 1);
8592 cond0 = XEXP (XEXP (x, 0), 0);
8593 cond1 = XEXP (XEXP (x, 1), 0);
8595 if (COMPARISON_P (cond0)
8596 && COMPARISON_P (cond1)
8597 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8598 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8599 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8600 || ((swap_condition (GET_CODE (cond0))
8601 == reversed_comparison_code (cond1, NULL))
8602 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8603 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8604 && ! side_effects_p (x))
8606 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8607 *pfalse = simplify_gen_binary (MULT, mode,
8608 (code == MINUS
8609 ? simplify_gen_unary (NEG, mode,
8610 op1, mode)
8611 : op1),
8612 const_true_rtx);
8613 return cond0;
8617 /* Similarly for MULT, AND and UMIN, except that for these the result
8618 is always zero. */
8619 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8620 && (code == MULT || code == AND || code == UMIN)
8621 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8623 cond0 = XEXP (XEXP (x, 0), 0);
8624 cond1 = XEXP (XEXP (x, 1), 0);
8626 if (COMPARISON_P (cond0)
8627 && COMPARISON_P (cond1)
8628 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8629 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8630 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8631 || ((swap_condition (GET_CODE (cond0))
8632 == reversed_comparison_code (cond1, NULL))
8633 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8634 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8635 && ! side_effects_p (x))
8637 *ptrue = *pfalse = const0_rtx;
8638 return cond0;
8643 else if (code == IF_THEN_ELSE)
8645 /* If we have IF_THEN_ELSE already, extract the condition and
8646 canonicalize it if it is NE or EQ. */
8647 cond0 = XEXP (x, 0);
8648 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8649 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8650 return XEXP (cond0, 0);
8651 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8653 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8654 return XEXP (cond0, 0);
8656 else
8657 return cond0;
8660 /* If X is a SUBREG, we can narrow both the true and false values
8661 if the inner expression, if there is a condition. */
8662 else if (code == SUBREG
8663 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8664 &true0, &false0)))
8666 true0 = simplify_gen_subreg (mode, true0,
8667 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8668 false0 = simplify_gen_subreg (mode, false0,
8669 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8670 if (true0 && false0)
8672 *ptrue = true0;
8673 *pfalse = false0;
8674 return cond0;
8678 /* If X is a constant, this isn't special and will cause confusions
8679 if we treat it as such. Likewise if it is equivalent to a constant. */
8680 else if (CONSTANT_P (x)
8681 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8684 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8685 will be least confusing to the rest of the compiler. */
8686 else if (mode == BImode)
8688 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8689 return x;
8692 /* If X is known to be either 0 or -1, those are the true and
8693 false values when testing X. */
8694 else if (x == constm1_rtx || x == const0_rtx
8695 || (mode != VOIDmode
8696 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8698 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8699 return x;
8702 /* Likewise for 0 or a single bit. */
8703 else if (SCALAR_INT_MODE_P (mode)
8704 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8705 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8707 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8708 return x;
8711 /* Otherwise fail; show no condition with true and false values the same. */
8712 *ptrue = *pfalse = x;
8713 return 0;
8716 /* Return the value of expression X given the fact that condition COND
8717 is known to be true when applied to REG as its first operand and VAL
8718 as its second. X is known to not be shared and so can be modified in
8719 place.
8721 We only handle the simplest cases, and specifically those cases that
8722 arise with IF_THEN_ELSE expressions. */
8724 static rtx
8725 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8727 enum rtx_code code = GET_CODE (x);
8728 rtx temp;
8729 const char *fmt;
8730 int i, j;
8732 if (side_effects_p (x))
8733 return x;
8735 /* If either operand of the condition is a floating point value,
8736 then we have to avoid collapsing an EQ comparison. */
8737 if (cond == EQ
8738 && rtx_equal_p (x, reg)
8739 && ! FLOAT_MODE_P (GET_MODE (x))
8740 && ! FLOAT_MODE_P (GET_MODE (val)))
8741 return val;
8743 if (cond == UNEQ && rtx_equal_p (x, reg))
8744 return val;
8746 /* If X is (abs REG) and we know something about REG's relationship
8747 with zero, we may be able to simplify this. */
8749 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8750 switch (cond)
8752 case GE: case GT: case EQ:
8753 return XEXP (x, 0);
8754 case LT: case LE:
8755 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8756 XEXP (x, 0),
8757 GET_MODE (XEXP (x, 0)));
8758 default:
8759 break;
8762 /* The only other cases we handle are MIN, MAX, and comparisons if the
8763 operands are the same as REG and VAL. */
8765 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8767 if (rtx_equal_p (XEXP (x, 0), val))
8768 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8770 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8772 if (COMPARISON_P (x))
8774 if (comparison_dominates_p (cond, code))
8775 return const_true_rtx;
8777 code = reversed_comparison_code (x, NULL);
8778 if (code != UNKNOWN
8779 && comparison_dominates_p (cond, code))
8780 return const0_rtx;
8781 else
8782 return x;
8784 else if (code == SMAX || code == SMIN
8785 || code == UMIN || code == UMAX)
8787 int unsignedp = (code == UMIN || code == UMAX);
8789 /* Do not reverse the condition when it is NE or EQ.
8790 This is because we cannot conclude anything about
8791 the value of 'SMAX (x, y)' when x is not equal to y,
8792 but we can when x equals y. */
8793 if ((code == SMAX || code == UMAX)
8794 && ! (cond == EQ || cond == NE))
8795 cond = reverse_condition (cond);
8797 switch (cond)
8799 case GE: case GT:
8800 return unsignedp ? x : XEXP (x, 1);
8801 case LE: case LT:
8802 return unsignedp ? x : XEXP (x, 0);
8803 case GEU: case GTU:
8804 return unsignedp ? XEXP (x, 1) : x;
8805 case LEU: case LTU:
8806 return unsignedp ? XEXP (x, 0) : x;
8807 default:
8808 break;
8813 else if (code == SUBREG)
8815 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8816 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8818 if (SUBREG_REG (x) != r)
8820 /* We must simplify subreg here, before we lose track of the
8821 original inner_mode. */
8822 new_rtx = simplify_subreg (GET_MODE (x), r,
8823 inner_mode, SUBREG_BYTE (x));
8824 if (new_rtx)
8825 return new_rtx;
8826 else
8827 SUBST (SUBREG_REG (x), r);
8830 return x;
8832 /* We don't have to handle SIGN_EXTEND here, because even in the
8833 case of replacing something with a modeless CONST_INT, a
8834 CONST_INT is already (supposed to be) a valid sign extension for
8835 its narrower mode, which implies it's already properly
8836 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8837 story is different. */
8838 else if (code == ZERO_EXTEND)
8840 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8841 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8843 if (XEXP (x, 0) != r)
8845 /* We must simplify the zero_extend here, before we lose
8846 track of the original inner_mode. */
8847 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8848 r, inner_mode);
8849 if (new_rtx)
8850 return new_rtx;
8851 else
8852 SUBST (XEXP (x, 0), r);
8855 return x;
8858 fmt = GET_RTX_FORMAT (code);
8859 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8861 if (fmt[i] == 'e')
8862 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8863 else if (fmt[i] == 'E')
8864 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8865 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8866 cond, reg, val));
8869 return x;
8872 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8873 assignment as a field assignment. */
8875 static int
8876 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8878 if (x == y || rtx_equal_p (x, y))
8879 return 1;
8881 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8882 return 0;
8884 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8885 Note that all SUBREGs of MEM are paradoxical; otherwise they
8886 would have been rewritten. */
8887 if (MEM_P (x) && GET_CODE (y) == SUBREG
8888 && MEM_P (SUBREG_REG (y))
8889 && rtx_equal_p (SUBREG_REG (y),
8890 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8891 return 1;
8893 if (MEM_P (y) && GET_CODE (x) == SUBREG
8894 && MEM_P (SUBREG_REG (x))
8895 && rtx_equal_p (SUBREG_REG (x),
8896 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8897 return 1;
8899 /* We used to see if get_last_value of X and Y were the same but that's
8900 not correct. In one direction, we'll cause the assignment to have
8901 the wrong destination and in the case, we'll import a register into this
8902 insn that might have already have been dead. So fail if none of the
8903 above cases are true. */
8904 return 0;
8907 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8908 Return that assignment if so.
8910 We only handle the most common cases. */
8912 static rtx
8913 make_field_assignment (rtx x)
8915 rtx dest = SET_DEST (x);
8916 rtx src = SET_SRC (x);
8917 rtx assign;
8918 rtx rhs, lhs;
8919 HOST_WIDE_INT c1;
8920 HOST_WIDE_INT pos;
8921 unsigned HOST_WIDE_INT len;
8922 rtx other;
8923 enum machine_mode mode;
8925 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8926 a clear of a one-bit field. We will have changed it to
8927 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8928 for a SUBREG. */
8930 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8931 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8932 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8933 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8935 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8936 1, 1, 1, 0);
8937 if (assign != 0)
8938 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8939 return x;
8942 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8943 && subreg_lowpart_p (XEXP (src, 0))
8944 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8945 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8946 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8947 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8948 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8949 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8951 assign = make_extraction (VOIDmode, dest, 0,
8952 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8953 1, 1, 1, 0);
8954 if (assign != 0)
8955 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8956 return x;
8959 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8960 one-bit field. */
8961 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8962 && XEXP (XEXP (src, 0), 0) == const1_rtx
8963 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8965 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8966 1, 1, 1, 0);
8967 if (assign != 0)
8968 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8969 return x;
8972 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8973 SRC is an AND with all bits of that field set, then we can discard
8974 the AND. */
8975 if (GET_CODE (dest) == ZERO_EXTRACT
8976 && CONST_INT_P (XEXP (dest, 1))
8977 && GET_CODE (src) == AND
8978 && CONST_INT_P (XEXP (src, 1)))
8980 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8981 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8982 unsigned HOST_WIDE_INT ze_mask;
8984 if (width >= HOST_BITS_PER_WIDE_INT)
8985 ze_mask = -1;
8986 else
8987 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8989 /* Complete overlap. We can remove the source AND. */
8990 if ((and_mask & ze_mask) == ze_mask)
8991 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8993 /* Partial overlap. We can reduce the source AND. */
8994 if ((and_mask & ze_mask) != and_mask)
8996 mode = GET_MODE (src);
8997 src = gen_rtx_AND (mode, XEXP (src, 0),
8998 gen_int_mode (and_mask & ze_mask, mode));
8999 return gen_rtx_SET (VOIDmode, dest, src);
9003 /* The other case we handle is assignments into a constant-position
9004 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9005 a mask that has all one bits except for a group of zero bits and
9006 OTHER is known to have zeros where C1 has ones, this is such an
9007 assignment. Compute the position and length from C1. Shift OTHER
9008 to the appropriate position, force it to the required mode, and
9009 make the extraction. Check for the AND in both operands. */
9011 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9012 return x;
9014 rhs = expand_compound_operation (XEXP (src, 0));
9015 lhs = expand_compound_operation (XEXP (src, 1));
9017 if (GET_CODE (rhs) == AND
9018 && CONST_INT_P (XEXP (rhs, 1))
9019 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9020 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9021 else if (GET_CODE (lhs) == AND
9022 && CONST_INT_P (XEXP (lhs, 1))
9023 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9024 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9025 else
9026 return x;
9028 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9029 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
9030 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9031 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9032 return x;
9034 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9035 if (assign == 0)
9036 return x;
9038 /* The mode to use for the source is the mode of the assignment, or of
9039 what is inside a possible STRICT_LOW_PART. */
9040 mode = (GET_CODE (assign) == STRICT_LOW_PART
9041 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9043 /* Shift OTHER right POS places and make it the source, restricting it
9044 to the proper length and mode. */
9046 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9047 GET_MODE (src),
9048 other, pos),
9049 dest);
9050 src = force_to_mode (src, mode,
9051 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
9052 ? ~(unsigned HOST_WIDE_INT) 0
9053 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9056 /* If SRC is masked by an AND that does not make a difference in
9057 the value being stored, strip it. */
9058 if (GET_CODE (assign) == ZERO_EXTRACT
9059 && CONST_INT_P (XEXP (assign, 1))
9060 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9061 && GET_CODE (src) == AND
9062 && CONST_INT_P (XEXP (src, 1))
9063 && UINTVAL (XEXP (src, 1))
9064 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9065 src = XEXP (src, 0);
9067 return gen_rtx_SET (VOIDmode, assign, src);
9070 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9071 if so. */
9073 static rtx
9074 apply_distributive_law (rtx x)
9076 enum rtx_code code = GET_CODE (x);
9077 enum rtx_code inner_code;
9078 rtx lhs, rhs, other;
9079 rtx tem;
9081 /* Distributivity is not true for floating point as it can change the
9082 value. So we don't do it unless -funsafe-math-optimizations. */
9083 if (FLOAT_MODE_P (GET_MODE (x))
9084 && ! flag_unsafe_math_optimizations)
9085 return x;
9087 /* The outer operation can only be one of the following: */
9088 if (code != IOR && code != AND && code != XOR
9089 && code != PLUS && code != MINUS)
9090 return x;
9092 lhs = XEXP (x, 0);
9093 rhs = XEXP (x, 1);
9095 /* If either operand is a primitive we can't do anything, so get out
9096 fast. */
9097 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9098 return x;
9100 lhs = expand_compound_operation (lhs);
9101 rhs = expand_compound_operation (rhs);
9102 inner_code = GET_CODE (lhs);
9103 if (inner_code != GET_CODE (rhs))
9104 return x;
9106 /* See if the inner and outer operations distribute. */
9107 switch (inner_code)
9109 case LSHIFTRT:
9110 case ASHIFTRT:
9111 case AND:
9112 case IOR:
9113 /* These all distribute except over PLUS. */
9114 if (code == PLUS || code == MINUS)
9115 return x;
9116 break;
9118 case MULT:
9119 if (code != PLUS && code != MINUS)
9120 return x;
9121 break;
9123 case ASHIFT:
9124 /* This is also a multiply, so it distributes over everything. */
9125 break;
9127 case SUBREG:
9128 /* Non-paradoxical SUBREGs distributes over all operations,
9129 provided the inner modes and byte offsets are the same, this
9130 is an extraction of a low-order part, we don't convert an fp
9131 operation to int or vice versa, this is not a vector mode,
9132 and we would not be converting a single-word operation into a
9133 multi-word operation. The latter test is not required, but
9134 it prevents generating unneeded multi-word operations. Some
9135 of the previous tests are redundant given the latter test,
9136 but are retained because they are required for correctness.
9138 We produce the result slightly differently in this case. */
9140 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
9141 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
9142 || ! subreg_lowpart_p (lhs)
9143 || (GET_MODE_CLASS (GET_MODE (lhs))
9144 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
9145 || (GET_MODE_SIZE (GET_MODE (lhs))
9146 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
9147 || VECTOR_MODE_P (GET_MODE (lhs))
9148 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
9149 /* Result might need to be truncated. Don't change mode if
9150 explicit truncation is needed. */
9151 || !TRULY_NOOP_TRUNCATION
9152 (GET_MODE_BITSIZE (GET_MODE (x)),
9153 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
9154 return x;
9156 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
9157 SUBREG_REG (lhs), SUBREG_REG (rhs));
9158 return gen_lowpart (GET_MODE (x), tem);
9160 default:
9161 return x;
9164 /* Set LHS and RHS to the inner operands (A and B in the example
9165 above) and set OTHER to the common operand (C in the example).
9166 There is only one way to do this unless the inner operation is
9167 commutative. */
9168 if (COMMUTATIVE_ARITH_P (lhs)
9169 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9170 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9171 else if (COMMUTATIVE_ARITH_P (lhs)
9172 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9173 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9174 else if (COMMUTATIVE_ARITH_P (lhs)
9175 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9176 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9177 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9178 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9179 else
9180 return x;
9182 /* Form the new inner operation, seeing if it simplifies first. */
9183 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9185 /* There is one exception to the general way of distributing:
9186 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9187 if (code == XOR && inner_code == IOR)
9189 inner_code = AND;
9190 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9193 /* We may be able to continuing distributing the result, so call
9194 ourselves recursively on the inner operation before forming the
9195 outer operation, which we return. */
9196 return simplify_gen_binary (inner_code, GET_MODE (x),
9197 apply_distributive_law (tem), other);
9200 /* See if X is of the form (* (+ A B) C), and if so convert to
9201 (+ (* A C) (* B C)) and try to simplify.
9203 Most of the time, this results in no change. However, if some of
9204 the operands are the same or inverses of each other, simplifications
9205 will result.
9207 For example, (and (ior A B) (not B)) can occur as the result of
9208 expanding a bit field assignment. When we apply the distributive
9209 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9210 which then simplifies to (and (A (not B))).
9212 Note that no checks happen on the validity of applying the inverse
9213 distributive law. This is pointless since we can do it in the
9214 few places where this routine is called.
9216 N is the index of the term that is decomposed (the arithmetic operation,
9217 i.e. (+ A B) in the first example above). !N is the index of the term that
9218 is distributed, i.e. of C in the first example above. */
9219 static rtx
9220 distribute_and_simplify_rtx (rtx x, int n)
9222 enum machine_mode mode;
9223 enum rtx_code outer_code, inner_code;
9224 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9226 /* Distributivity is not true for floating point as it can change the
9227 value. So we don't do it unless -funsafe-math-optimizations. */
9228 if (FLOAT_MODE_P (GET_MODE (x))
9229 && ! flag_unsafe_math_optimizations)
9230 return NULL_RTX;
9232 decomposed = XEXP (x, n);
9233 if (!ARITHMETIC_P (decomposed))
9234 return NULL_RTX;
9236 mode = GET_MODE (x);
9237 outer_code = GET_CODE (x);
9238 distributed = XEXP (x, !n);
9240 inner_code = GET_CODE (decomposed);
9241 inner_op0 = XEXP (decomposed, 0);
9242 inner_op1 = XEXP (decomposed, 1);
9244 /* Special case (and (xor B C) (not A)), which is equivalent to
9245 (xor (ior A B) (ior A C)) */
9246 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9248 distributed = XEXP (distributed, 0);
9249 outer_code = IOR;
9252 if (n == 0)
9254 /* Distribute the second term. */
9255 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9256 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9258 else
9260 /* Distribute the first term. */
9261 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9262 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9265 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9266 new_op0, new_op1));
9267 if (GET_CODE (tmp) != outer_code
9268 && rtx_cost (tmp, SET, optimize_this_for_speed_p)
9269 < rtx_cost (x, SET, optimize_this_for_speed_p))
9270 return tmp;
9272 return NULL_RTX;
9275 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9276 in MODE. Return an equivalent form, if different from (and VAROP
9277 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9279 static rtx
9280 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9281 unsigned HOST_WIDE_INT constop)
9283 unsigned HOST_WIDE_INT nonzero;
9284 unsigned HOST_WIDE_INT orig_constop;
9285 rtx orig_varop;
9286 int i;
9288 orig_varop = varop;
9289 orig_constop = constop;
9290 if (GET_CODE (varop) == CLOBBER)
9291 return NULL_RTX;
9293 /* Simplify VAROP knowing that we will be only looking at some of the
9294 bits in it.
9296 Note by passing in CONSTOP, we guarantee that the bits not set in
9297 CONSTOP are not significant and will never be examined. We must
9298 ensure that is the case by explicitly masking out those bits
9299 before returning. */
9300 varop = force_to_mode (varop, mode, constop, 0);
9302 /* If VAROP is a CLOBBER, we will fail so return it. */
9303 if (GET_CODE (varop) == CLOBBER)
9304 return varop;
9306 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9307 to VAROP and return the new constant. */
9308 if (CONST_INT_P (varop))
9309 return gen_int_mode (INTVAL (varop) & constop, mode);
9311 /* See what bits may be nonzero in VAROP. Unlike the general case of
9312 a call to nonzero_bits, here we don't care about bits outside
9313 MODE. */
9315 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9317 /* Turn off all bits in the constant that are known to already be zero.
9318 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9319 which is tested below. */
9321 constop &= nonzero;
9323 /* If we don't have any bits left, return zero. */
9324 if (constop == 0)
9325 return const0_rtx;
9327 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9328 a power of two, we can replace this with an ASHIFT. */
9329 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9330 && (i = exact_log2 (constop)) >= 0)
9331 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9333 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9334 or XOR, then try to apply the distributive law. This may eliminate
9335 operations if either branch can be simplified because of the AND.
9336 It may also make some cases more complex, but those cases probably
9337 won't match a pattern either with or without this. */
9339 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9340 return
9341 gen_lowpart
9342 (mode,
9343 apply_distributive_law
9344 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9345 simplify_and_const_int (NULL_RTX,
9346 GET_MODE (varop),
9347 XEXP (varop, 0),
9348 constop),
9349 simplify_and_const_int (NULL_RTX,
9350 GET_MODE (varop),
9351 XEXP (varop, 1),
9352 constop))));
9354 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9355 the AND and see if one of the operands simplifies to zero. If so, we
9356 may eliminate it. */
9358 if (GET_CODE (varop) == PLUS
9359 && exact_log2 (constop + 1) >= 0)
9361 rtx o0, o1;
9363 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9364 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9365 if (o0 == const0_rtx)
9366 return o1;
9367 if (o1 == const0_rtx)
9368 return o0;
9371 /* Make a SUBREG if necessary. If we can't make it, fail. */
9372 varop = gen_lowpart (mode, varop);
9373 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9374 return NULL_RTX;
9376 /* If we are only masking insignificant bits, return VAROP. */
9377 if (constop == nonzero)
9378 return varop;
9380 if (varop == orig_varop && constop == orig_constop)
9381 return NULL_RTX;
9383 /* Otherwise, return an AND. */
9384 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9388 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9389 in MODE.
9391 Return an equivalent form, if different from X. Otherwise, return X. If
9392 X is zero, we are to always construct the equivalent form. */
9394 static rtx
9395 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9396 unsigned HOST_WIDE_INT constop)
9398 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9399 if (tem)
9400 return tem;
9402 if (!x)
9403 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9404 gen_int_mode (constop, mode));
9405 if (GET_MODE (x) != mode)
9406 x = gen_lowpart (mode, x);
9407 return x;
9410 /* Given a REG, X, compute which bits in X can be nonzero.
9411 We don't care about bits outside of those defined in MODE.
9413 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9414 a shift, AND, or zero_extract, we can do better. */
9416 static rtx
9417 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9418 const_rtx known_x ATTRIBUTE_UNUSED,
9419 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9420 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9421 unsigned HOST_WIDE_INT *nonzero)
9423 rtx tem;
9424 reg_stat_type *rsp;
9426 /* If X is a register whose nonzero bits value is current, use it.
9427 Otherwise, if X is a register whose value we can find, use that
9428 value. Otherwise, use the previously-computed global nonzero bits
9429 for this register. */
9431 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9432 if (rsp->last_set_value != 0
9433 && (rsp->last_set_mode == mode
9434 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9435 && GET_MODE_CLASS (mode) == MODE_INT))
9436 && ((rsp->last_set_label >= label_tick_ebb_start
9437 && rsp->last_set_label < label_tick)
9438 || (rsp->last_set_label == label_tick
9439 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9440 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9441 && REG_N_SETS (REGNO (x)) == 1
9442 && !REGNO_REG_SET_P
9443 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9445 *nonzero &= rsp->last_set_nonzero_bits;
9446 return NULL;
9449 tem = get_last_value (x);
9451 if (tem)
9453 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9454 /* If X is narrower than MODE and TEM is a non-negative
9455 constant that would appear negative in the mode of X,
9456 sign-extend it for use in reg_nonzero_bits because some
9457 machines (maybe most) will actually do the sign-extension
9458 and this is the conservative approach.
9460 ??? For 2.5, try to tighten up the MD files in this regard
9461 instead of this kludge. */
9463 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9464 && CONST_INT_P (tem)
9465 && INTVAL (tem) > 0
9466 && 0 != (UINTVAL (tem)
9467 & ((unsigned HOST_WIDE_INT) 1
9468 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9469 tem = GEN_INT (UINTVAL (tem)
9470 | ((unsigned HOST_WIDE_INT) (-1)
9471 << GET_MODE_BITSIZE (GET_MODE (x))));
9472 #endif
9473 return tem;
9475 else if (nonzero_sign_valid && rsp->nonzero_bits)
9477 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9479 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9480 /* We don't know anything about the upper bits. */
9481 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9482 *nonzero &= mask;
9485 return NULL;
9488 /* Return the number of bits at the high-order end of X that are known to
9489 be equal to the sign bit. X will be used in mode MODE; if MODE is
9490 VOIDmode, X will be used in its own mode. The returned value will always
9491 be between 1 and the number of bits in MODE. */
9493 static rtx
9494 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9495 const_rtx known_x ATTRIBUTE_UNUSED,
9496 enum machine_mode known_mode
9497 ATTRIBUTE_UNUSED,
9498 unsigned int known_ret ATTRIBUTE_UNUSED,
9499 unsigned int *result)
9501 rtx tem;
9502 reg_stat_type *rsp;
9504 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9505 if (rsp->last_set_value != 0
9506 && rsp->last_set_mode == mode
9507 && ((rsp->last_set_label >= label_tick_ebb_start
9508 && rsp->last_set_label < label_tick)
9509 || (rsp->last_set_label == label_tick
9510 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9511 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9512 && REG_N_SETS (REGNO (x)) == 1
9513 && !REGNO_REG_SET_P
9514 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9516 *result = rsp->last_set_sign_bit_copies;
9517 return NULL;
9520 tem = get_last_value (x);
9521 if (tem != 0)
9522 return tem;
9524 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9525 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9526 *result = rsp->sign_bit_copies;
9528 return NULL;
9531 /* Return the number of "extended" bits there are in X, when interpreted
9532 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9533 unsigned quantities, this is the number of high-order zero bits.
9534 For signed quantities, this is the number of copies of the sign bit
9535 minus 1. In both case, this function returns the number of "spare"
9536 bits. For example, if two quantities for which this function returns
9537 at least 1 are added, the addition is known not to overflow.
9539 This function will always return 0 unless called during combine, which
9540 implies that it must be called from a define_split. */
9542 unsigned int
9543 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9545 if (nonzero_sign_valid == 0)
9546 return 0;
9548 return (unsignedp
9549 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9550 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9551 - floor_log2 (nonzero_bits (x, mode)))
9552 : 0)
9553 : num_sign_bit_copies (x, mode) - 1);
9556 /* This function is called from `simplify_shift_const' to merge two
9557 outer operations. Specifically, we have already found that we need
9558 to perform operation *POP0 with constant *PCONST0 at the outermost
9559 position. We would now like to also perform OP1 with constant CONST1
9560 (with *POP0 being done last).
9562 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9563 the resulting operation. *PCOMP_P is set to 1 if we would need to
9564 complement the innermost operand, otherwise it is unchanged.
9566 MODE is the mode in which the operation will be done. No bits outside
9567 the width of this mode matter. It is assumed that the width of this mode
9568 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9570 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9571 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9572 result is simply *PCONST0.
9574 If the resulting operation cannot be expressed as one operation, we
9575 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9577 static int
9578 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)
9580 enum rtx_code op0 = *pop0;
9581 HOST_WIDE_INT const0 = *pconst0;
9583 const0 &= GET_MODE_MASK (mode);
9584 const1 &= GET_MODE_MASK (mode);
9586 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9587 if (op0 == AND)
9588 const1 &= const0;
9590 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9591 if OP0 is SET. */
9593 if (op1 == UNKNOWN || op0 == SET)
9594 return 1;
9596 else if (op0 == UNKNOWN)
9597 op0 = op1, const0 = const1;
9599 else if (op0 == op1)
9601 switch (op0)
9603 case AND:
9604 const0 &= const1;
9605 break;
9606 case IOR:
9607 const0 |= const1;
9608 break;
9609 case XOR:
9610 const0 ^= const1;
9611 break;
9612 case PLUS:
9613 const0 += const1;
9614 break;
9615 case NEG:
9616 op0 = UNKNOWN;
9617 break;
9618 default:
9619 break;
9623 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9624 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9625 return 0;
9627 /* If the two constants aren't the same, we can't do anything. The
9628 remaining six cases can all be done. */
9629 else if (const0 != const1)
9630 return 0;
9632 else
9633 switch (op0)
9635 case IOR:
9636 if (op1 == AND)
9637 /* (a & b) | b == b */
9638 op0 = SET;
9639 else /* op1 == XOR */
9640 /* (a ^ b) | b == a | b */
9642 break;
9644 case XOR:
9645 if (op1 == AND)
9646 /* (a & b) ^ b == (~a) & b */
9647 op0 = AND, *pcomp_p = 1;
9648 else /* op1 == IOR */
9649 /* (a | b) ^ b == a & ~b */
9650 op0 = AND, const0 = ~const0;
9651 break;
9653 case AND:
9654 if (op1 == IOR)
9655 /* (a | b) & b == b */
9656 op0 = SET;
9657 else /* op1 == XOR */
9658 /* (a ^ b) & b) == (~a) & b */
9659 *pcomp_p = 1;
9660 break;
9661 default:
9662 break;
9665 /* Check for NO-OP cases. */
9666 const0 &= GET_MODE_MASK (mode);
9667 if (const0 == 0
9668 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9669 op0 = UNKNOWN;
9670 else if (const0 == 0 && op0 == AND)
9671 op0 = SET;
9672 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9673 && op0 == AND)
9674 op0 = UNKNOWN;
9676 *pop0 = op0;
9678 /* ??? Slightly redundant with the above mask, but not entirely.
9679 Moving this above means we'd have to sign-extend the mode mask
9680 for the final test. */
9681 if (op0 != UNKNOWN && op0 != NEG)
9682 *pconst0 = trunc_int_for_mode (const0, mode);
9684 return 1;
9687 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9688 the shift in. The original shift operation CODE is performed on OP in
9689 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9690 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9691 result of the shift is subject to operation OUTER_CODE with operand
9692 OUTER_CONST. */
9694 static enum machine_mode
9695 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9696 enum machine_mode orig_mode, enum machine_mode mode,
9697 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9699 if (orig_mode == mode)
9700 return mode;
9701 gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9703 /* In general we can't perform in wider mode for right shift and rotate. */
9704 switch (code)
9706 case ASHIFTRT:
9707 /* We can still widen if the bits brought in from the left are identical
9708 to the sign bit of ORIG_MODE. */
9709 if (num_sign_bit_copies (op, mode)
9710 > (unsigned) (GET_MODE_BITSIZE (mode)
9711 - GET_MODE_BITSIZE (orig_mode)))
9712 return mode;
9713 return orig_mode;
9715 case LSHIFTRT:
9716 /* Similarly here but with zero bits. */
9717 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9718 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9719 return mode;
9721 /* We can also widen if the bits brought in will be masked off. This
9722 operation is performed in ORIG_MODE. */
9723 if (outer_code == AND)
9725 int care_bits = low_bitmask_len (orig_mode, outer_const);
9727 if (care_bits >= 0
9728 && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9729 return mode;
9731 /* fall through */
9733 case ROTATE:
9734 return orig_mode;
9736 case ROTATERT:
9737 gcc_unreachable ();
9739 default:
9740 return mode;
9744 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9745 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
9746 simplify it. Otherwise, return a simplified value.
9748 The shift is normally computed in the widest mode we find in VAROP, as
9749 long as it isn't a different number of words than RESULT_MODE. Exceptions
9750 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9752 static rtx
9753 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9754 rtx varop, int orig_count)
9756 enum rtx_code orig_code = code;
9757 rtx orig_varop = varop;
9758 int count;
9759 enum machine_mode mode = result_mode;
9760 enum machine_mode shift_mode, tmode;
9761 unsigned int mode_words
9762 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9763 /* We form (outer_op (code varop count) (outer_const)). */
9764 enum rtx_code outer_op = UNKNOWN;
9765 HOST_WIDE_INT outer_const = 0;
9766 int complement_p = 0;
9767 rtx new_rtx, x;
9769 /* Make sure and truncate the "natural" shift on the way in. We don't
9770 want to do this inside the loop as it makes it more difficult to
9771 combine shifts. */
9772 if (SHIFT_COUNT_TRUNCATED)
9773 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9775 /* If we were given an invalid count, don't do anything except exactly
9776 what was requested. */
9778 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9779 return NULL_RTX;
9781 count = orig_count;
9783 /* Unless one of the branches of the `if' in this loop does a `continue',
9784 we will `break' the loop after the `if'. */
9786 while (count != 0)
9788 /* If we have an operand of (clobber (const_int 0)), fail. */
9789 if (GET_CODE (varop) == CLOBBER)
9790 return NULL_RTX;
9792 /* Convert ROTATERT to ROTATE. */
9793 if (code == ROTATERT)
9795 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9796 code = ROTATE;
9797 if (VECTOR_MODE_P (result_mode))
9798 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9799 else
9800 count = bitsize - count;
9803 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9804 mode, outer_op, outer_const);
9806 /* Handle cases where the count is greater than the size of the mode
9807 minus 1. For ASHIFT, use the size minus one as the count (this can
9808 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9809 take the count modulo the size. For other shifts, the result is
9810 zero.
9812 Since these shifts are being produced by the compiler by combining
9813 multiple operations, each of which are defined, we know what the
9814 result is supposed to be. */
9816 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9818 if (code == ASHIFTRT)
9819 count = GET_MODE_BITSIZE (shift_mode) - 1;
9820 else if (code == ROTATE || code == ROTATERT)
9821 count %= GET_MODE_BITSIZE (shift_mode);
9822 else
9824 /* We can't simply return zero because there may be an
9825 outer op. */
9826 varop = const0_rtx;
9827 count = 0;
9828 break;
9832 /* If we discovered we had to complement VAROP, leave. Making a NOT
9833 here would cause an infinite loop. */
9834 if (complement_p)
9835 break;
9837 /* An arithmetic right shift of a quantity known to be -1 or 0
9838 is a no-op. */
9839 if (code == ASHIFTRT
9840 && (num_sign_bit_copies (varop, shift_mode)
9841 == GET_MODE_BITSIZE (shift_mode)))
9843 count = 0;
9844 break;
9847 /* If we are doing an arithmetic right shift and discarding all but
9848 the sign bit copies, this is equivalent to doing a shift by the
9849 bitsize minus one. Convert it into that shift because it will often
9850 allow other simplifications. */
9852 if (code == ASHIFTRT
9853 && (count + num_sign_bit_copies (varop, shift_mode)
9854 >= GET_MODE_BITSIZE (shift_mode)))
9855 count = GET_MODE_BITSIZE (shift_mode) - 1;
9857 /* We simplify the tests below and elsewhere by converting
9858 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9859 `make_compound_operation' will convert it to an ASHIFTRT for
9860 those machines (such as VAX) that don't have an LSHIFTRT. */
9861 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9862 && code == ASHIFTRT
9863 && ((nonzero_bits (varop, shift_mode)
9864 & ((unsigned HOST_WIDE_INT) 1
9865 << (GET_MODE_BITSIZE (shift_mode) - 1))) == 0))
9866 code = LSHIFTRT;
9868 if (((code == LSHIFTRT
9869 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9870 && !(nonzero_bits (varop, shift_mode) >> count))
9871 || (code == ASHIFT
9872 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9873 && !((nonzero_bits (varop, shift_mode) << count)
9874 & GET_MODE_MASK (shift_mode))))
9875 && !side_effects_p (varop))
9876 varop = const0_rtx;
9878 switch (GET_CODE (varop))
9880 case SIGN_EXTEND:
9881 case ZERO_EXTEND:
9882 case SIGN_EXTRACT:
9883 case ZERO_EXTRACT:
9884 new_rtx = expand_compound_operation (varop);
9885 if (new_rtx != varop)
9887 varop = new_rtx;
9888 continue;
9890 break;
9892 case MEM:
9893 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9894 minus the width of a smaller mode, we can do this with a
9895 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9896 if ((code == ASHIFTRT || code == LSHIFTRT)
9897 && ! mode_dependent_address_p (XEXP (varop, 0))
9898 && ! MEM_VOLATILE_P (varop)
9899 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9900 MODE_INT, 1)) != BLKmode)
9902 new_rtx = adjust_address_nv (varop, tmode,
9903 BYTES_BIG_ENDIAN ? 0
9904 : count / BITS_PER_UNIT);
9906 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9907 : ZERO_EXTEND, mode, new_rtx);
9908 count = 0;
9909 continue;
9911 break;
9913 case SUBREG:
9914 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9915 the same number of words as what we've seen so far. Then store
9916 the widest mode in MODE. */
9917 if (subreg_lowpart_p (varop)
9918 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9919 > GET_MODE_SIZE (GET_MODE (varop)))
9920 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9921 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9922 == mode_words
9923 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
9924 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
9926 varop = SUBREG_REG (varop);
9927 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9928 mode = GET_MODE (varop);
9929 continue;
9931 break;
9933 case MULT:
9934 /* Some machines use MULT instead of ASHIFT because MULT
9935 is cheaper. But it is still better on those machines to
9936 merge two shifts into one. */
9937 if (CONST_INT_P (XEXP (varop, 1))
9938 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9940 varop
9941 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9942 XEXP (varop, 0),
9943 GEN_INT (exact_log2 (
9944 UINTVAL (XEXP (varop, 1)))));
9945 continue;
9947 break;
9949 case UDIV:
9950 /* Similar, for when divides are cheaper. */
9951 if (CONST_INT_P (XEXP (varop, 1))
9952 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9954 varop
9955 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9956 XEXP (varop, 0),
9957 GEN_INT (exact_log2 (
9958 UINTVAL (XEXP (varop, 1)))));
9959 continue;
9961 break;
9963 case ASHIFTRT:
9964 /* If we are extracting just the sign bit of an arithmetic
9965 right shift, that shift is not needed. However, the sign
9966 bit of a wider mode may be different from what would be
9967 interpreted as the sign bit in a narrower mode, so, if
9968 the result is narrower, don't discard the shift. */
9969 if (code == LSHIFTRT
9970 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9971 && (GET_MODE_BITSIZE (result_mode)
9972 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9974 varop = XEXP (varop, 0);
9975 continue;
9978 /* ... fall through ... */
9980 case LSHIFTRT:
9981 case ASHIFT:
9982 case ROTATE:
9983 /* Here we have two nested shifts. The result is usually the
9984 AND of a new shift with a mask. We compute the result below. */
9985 if (CONST_INT_P (XEXP (varop, 1))
9986 && INTVAL (XEXP (varop, 1)) >= 0
9987 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9988 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9989 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9990 && !VECTOR_MODE_P (result_mode))
9992 enum rtx_code first_code = GET_CODE (varop);
9993 unsigned int first_count = INTVAL (XEXP (varop, 1));
9994 unsigned HOST_WIDE_INT mask;
9995 rtx mask_rtx;
9997 /* We have one common special case. We can't do any merging if
9998 the inner code is an ASHIFTRT of a smaller mode. However, if
9999 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10000 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10001 we can convert it to
10002 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
10003 This simplifies certain SIGN_EXTEND operations. */
10004 if (code == ASHIFT && first_code == ASHIFTRT
10005 && count == (GET_MODE_BITSIZE (result_mode)
10006 - GET_MODE_BITSIZE (GET_MODE (varop))))
10008 /* C3 has the low-order C1 bits zero. */
10010 mask = GET_MODE_MASK (mode)
10011 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10013 varop = simplify_and_const_int (NULL_RTX, result_mode,
10014 XEXP (varop, 0), mask);
10015 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10016 varop, count);
10017 count = first_count;
10018 code = ASHIFTRT;
10019 continue;
10022 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10023 than C1 high-order bits equal to the sign bit, we can convert
10024 this to either an ASHIFT or an ASHIFTRT depending on the
10025 two counts.
10027 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10029 if (code == ASHIFTRT && first_code == ASHIFT
10030 && GET_MODE (varop) == shift_mode
10031 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10032 > first_count))
10034 varop = XEXP (varop, 0);
10035 count -= first_count;
10036 if (count < 0)
10038 count = -count;
10039 code = ASHIFT;
10042 continue;
10045 /* There are some cases we can't do. If CODE is ASHIFTRT,
10046 we can only do this if FIRST_CODE is also ASHIFTRT.
10048 We can't do the case when CODE is ROTATE and FIRST_CODE is
10049 ASHIFTRT.
10051 If the mode of this shift is not the mode of the outer shift,
10052 we can't do this if either shift is a right shift or ROTATE.
10054 Finally, we can't do any of these if the mode is too wide
10055 unless the codes are the same.
10057 Handle the case where the shift codes are the same
10058 first. */
10060 if (code == first_code)
10062 if (GET_MODE (varop) != result_mode
10063 && (code == ASHIFTRT || code == LSHIFTRT
10064 || code == ROTATE))
10065 break;
10067 count += first_count;
10068 varop = XEXP (varop, 0);
10069 continue;
10072 if (code == ASHIFTRT
10073 || (code == ROTATE && first_code == ASHIFTRT)
10074 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
10075 || (GET_MODE (varop) != result_mode
10076 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10077 || first_code == ROTATE
10078 || code == ROTATE)))
10079 break;
10081 /* To compute the mask to apply after the shift, shift the
10082 nonzero bits of the inner shift the same way the
10083 outer shift will. */
10085 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10087 mask_rtx
10088 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10089 GEN_INT (count));
10091 /* Give up if we can't compute an outer operation to use. */
10092 if (mask_rtx == 0
10093 || !CONST_INT_P (mask_rtx)
10094 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10095 INTVAL (mask_rtx),
10096 result_mode, &complement_p))
10097 break;
10099 /* If the shifts are in the same direction, we add the
10100 counts. Otherwise, we subtract them. */
10101 if ((code == ASHIFTRT || code == LSHIFTRT)
10102 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10103 count += first_count;
10104 else
10105 count -= first_count;
10107 /* If COUNT is positive, the new shift is usually CODE,
10108 except for the two exceptions below, in which case it is
10109 FIRST_CODE. If the count is negative, FIRST_CODE should
10110 always be used */
10111 if (count > 0
10112 && ((first_code == ROTATE && code == ASHIFT)
10113 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10114 code = first_code;
10115 else if (count < 0)
10116 code = first_code, count = -count;
10118 varop = XEXP (varop, 0);
10119 continue;
10122 /* If we have (A << B << C) for any shift, we can convert this to
10123 (A << C << B). This wins if A is a constant. Only try this if
10124 B is not a constant. */
10126 else if (GET_CODE (varop) == code
10127 && CONST_INT_P (XEXP (varop, 0))
10128 && !CONST_INT_P (XEXP (varop, 1)))
10130 rtx new_rtx = simplify_const_binary_operation (code, mode,
10131 XEXP (varop, 0),
10132 GEN_INT (count));
10133 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10134 count = 0;
10135 continue;
10137 break;
10139 case NOT:
10140 if (VECTOR_MODE_P (mode))
10141 break;
10143 /* Make this fit the case below. */
10144 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
10145 GEN_INT (GET_MODE_MASK (mode)));
10146 continue;
10148 case IOR:
10149 case AND:
10150 case XOR:
10151 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10152 with C the size of VAROP - 1 and the shift is logical if
10153 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10154 we have an (le X 0) operation. If we have an arithmetic shift
10155 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10156 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10158 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10159 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10160 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10161 && (code == LSHIFTRT || code == ASHIFTRT)
10162 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10163 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10165 count = 0;
10166 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10167 const0_rtx);
10169 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10170 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10172 continue;
10175 /* If we have (shift (logical)), move the logical to the outside
10176 to allow it to possibly combine with another logical and the
10177 shift to combine with another shift. This also canonicalizes to
10178 what a ZERO_EXTRACT looks like. Also, some machines have
10179 (and (shift)) insns. */
10181 if (CONST_INT_P (XEXP (varop, 1))
10182 /* We can't do this if we have (ashiftrt (xor)) and the
10183 constant has its sign bit set in shift_mode. */
10184 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10185 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10186 shift_mode))
10187 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10188 XEXP (varop, 1),
10189 GEN_INT (count))) != 0
10190 && CONST_INT_P (new_rtx)
10191 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10192 INTVAL (new_rtx), result_mode, &complement_p))
10194 varop = XEXP (varop, 0);
10195 continue;
10198 /* If we can't do that, try to simplify the shift in each arm of the
10199 logical expression, make a new logical expression, and apply
10200 the inverse distributive law. This also can't be done
10201 for some (ashiftrt (xor)). */
10202 if (CONST_INT_P (XEXP (varop, 1))
10203 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10204 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10205 shift_mode)))
10207 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10208 XEXP (varop, 0), count);
10209 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10210 XEXP (varop, 1), count);
10212 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10213 lhs, rhs);
10214 varop = apply_distributive_law (varop);
10216 count = 0;
10217 continue;
10219 break;
10221 case EQ:
10222 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10223 says that the sign bit can be tested, FOO has mode MODE, C is
10224 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
10225 that may be nonzero. */
10226 if (code == LSHIFTRT
10227 && XEXP (varop, 1) == const0_rtx
10228 && GET_MODE (XEXP (varop, 0)) == result_mode
10229 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10230 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10231 && STORE_FLAG_VALUE == -1
10232 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10233 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10234 &complement_p))
10236 varop = XEXP (varop, 0);
10237 count = 0;
10238 continue;
10240 break;
10242 case NEG:
10243 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10244 than the number of bits in the mode is equivalent to A. */
10245 if (code == LSHIFTRT
10246 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10247 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10249 varop = XEXP (varop, 0);
10250 count = 0;
10251 continue;
10254 /* NEG commutes with ASHIFT since it is multiplication. Move the
10255 NEG outside to allow shifts to combine. */
10256 if (code == ASHIFT
10257 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10258 &complement_p))
10260 varop = XEXP (varop, 0);
10261 continue;
10263 break;
10265 case PLUS:
10266 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10267 is one less than the number of bits in the mode is
10268 equivalent to (xor A 1). */
10269 if (code == LSHIFTRT
10270 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10271 && XEXP (varop, 1) == constm1_rtx
10272 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10273 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10274 &complement_p))
10276 count = 0;
10277 varop = XEXP (varop, 0);
10278 continue;
10281 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10282 that might be nonzero in BAR are those being shifted out and those
10283 bits are known zero in FOO, we can replace the PLUS with FOO.
10284 Similarly in the other operand order. This code occurs when
10285 we are computing the size of a variable-size array. */
10287 if ((code == ASHIFTRT || code == LSHIFTRT)
10288 && count < HOST_BITS_PER_WIDE_INT
10289 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10290 && (nonzero_bits (XEXP (varop, 1), result_mode)
10291 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10293 varop = XEXP (varop, 0);
10294 continue;
10296 else if ((code == ASHIFTRT || code == LSHIFTRT)
10297 && count < HOST_BITS_PER_WIDE_INT
10298 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10299 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10300 >> count)
10301 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10302 & nonzero_bits (XEXP (varop, 1),
10303 result_mode)))
10305 varop = XEXP (varop, 1);
10306 continue;
10309 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10310 if (code == ASHIFT
10311 && CONST_INT_P (XEXP (varop, 1))
10312 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10313 XEXP (varop, 1),
10314 GEN_INT (count))) != 0
10315 && CONST_INT_P (new_rtx)
10316 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10317 INTVAL (new_rtx), result_mode, &complement_p))
10319 varop = XEXP (varop, 0);
10320 continue;
10323 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10324 signbit', and attempt to change the PLUS to an XOR and move it to
10325 the outer operation as is done above in the AND/IOR/XOR case
10326 leg for shift(logical). See details in logical handling above
10327 for reasoning in doing so. */
10328 if (code == LSHIFTRT
10329 && CONST_INT_P (XEXP (varop, 1))
10330 && mode_signbit_p (result_mode, XEXP (varop, 1))
10331 && (new_rtx = simplify_const_binary_operation (code, result_mode,
10332 XEXP (varop, 1),
10333 GEN_INT (count))) != 0
10334 && CONST_INT_P (new_rtx)
10335 && merge_outer_ops (&outer_op, &outer_const, XOR,
10336 INTVAL (new_rtx), result_mode, &complement_p))
10338 varop = XEXP (varop, 0);
10339 continue;
10342 break;
10344 case MINUS:
10345 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10346 with C the size of VAROP - 1 and the shift is logical if
10347 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10348 we have a (gt X 0) operation. If the shift is arithmetic with
10349 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10350 we have a (neg (gt X 0)) operation. */
10352 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10353 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10354 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10355 && (code == LSHIFTRT || code == ASHIFTRT)
10356 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10357 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10358 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10360 count = 0;
10361 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10362 const0_rtx);
10364 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10365 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10367 continue;
10369 break;
10371 case TRUNCATE:
10372 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10373 if the truncate does not affect the value. */
10374 if (code == LSHIFTRT
10375 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10376 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10377 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10378 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
10379 - GET_MODE_BITSIZE (GET_MODE (varop)))))
10381 rtx varop_inner = XEXP (varop, 0);
10383 varop_inner
10384 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10385 XEXP (varop_inner, 0),
10386 GEN_INT
10387 (count + INTVAL (XEXP (varop_inner, 1))));
10388 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10389 count = 0;
10390 continue;
10392 break;
10394 default:
10395 break;
10398 break;
10401 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10402 outer_op, outer_const);
10404 /* We have now finished analyzing the shift. The result should be
10405 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10406 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10407 to the result of the shift. OUTER_CONST is the relevant constant,
10408 but we must turn off all bits turned off in the shift. */
10410 if (outer_op == UNKNOWN
10411 && orig_code == code && orig_count == count
10412 && varop == orig_varop
10413 && shift_mode == GET_MODE (varop))
10414 return NULL_RTX;
10416 /* Make a SUBREG if necessary. If we can't make it, fail. */
10417 varop = gen_lowpart (shift_mode, varop);
10418 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10419 return NULL_RTX;
10421 /* If we have an outer operation and we just made a shift, it is
10422 possible that we could have simplified the shift were it not
10423 for the outer operation. So try to do the simplification
10424 recursively. */
10426 if (outer_op != UNKNOWN)
10427 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10428 else
10429 x = NULL_RTX;
10431 if (x == NULL_RTX)
10432 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10434 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10435 turn off all the bits that the shift would have turned off. */
10436 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10437 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10438 GET_MODE_MASK (result_mode) >> orig_count);
10440 /* Do the remainder of the processing in RESULT_MODE. */
10441 x = gen_lowpart_or_truncate (result_mode, x);
10443 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10444 operation. */
10445 if (complement_p)
10446 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10448 if (outer_op != UNKNOWN)
10450 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10451 && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10452 outer_const = trunc_int_for_mode (outer_const, result_mode);
10454 if (outer_op == AND)
10455 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10456 else if (outer_op == SET)
10458 /* This means that we have determined that the result is
10459 equivalent to a constant. This should be rare. */
10460 if (!side_effects_p (x))
10461 x = GEN_INT (outer_const);
10463 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10464 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10465 else
10466 x = simplify_gen_binary (outer_op, result_mode, x,
10467 GEN_INT (outer_const));
10470 return x;
10473 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10474 The result of the shift is RESULT_MODE. If we cannot simplify it,
10475 return X or, if it is NULL, synthesize the expression with
10476 simplify_gen_binary. Otherwise, return a simplified value.
10478 The shift is normally computed in the widest mode we find in VAROP, as
10479 long as it isn't a different number of words than RESULT_MODE. Exceptions
10480 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10482 static rtx
10483 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10484 rtx varop, int count)
10486 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10487 if (tem)
10488 return tem;
10490 if (!x)
10491 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10492 if (GET_MODE (x) != result_mode)
10493 x = gen_lowpart (result_mode, x);
10494 return x;
10498 /* Like recog, but we receive the address of a pointer to a new pattern.
10499 We try to match the rtx that the pointer points to.
10500 If that fails, we may try to modify or replace the pattern,
10501 storing the replacement into the same pointer object.
10503 Modifications include deletion or addition of CLOBBERs.
10505 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10506 the CLOBBERs are placed.
10508 The value is the final insn code from the pattern ultimately matched,
10509 or -1. */
10511 static int
10512 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10514 rtx pat = *pnewpat;
10515 int insn_code_number;
10516 int num_clobbers_to_add = 0;
10517 int i;
10518 rtx notes = 0;
10519 rtx old_notes, old_pat;
10521 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10522 we use to indicate that something didn't match. If we find such a
10523 thing, force rejection. */
10524 if (GET_CODE (pat) == PARALLEL)
10525 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10526 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10527 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10528 return -1;
10530 old_pat = PATTERN (insn);
10531 old_notes = REG_NOTES (insn);
10532 PATTERN (insn) = pat;
10533 REG_NOTES (insn) = 0;
10535 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10536 if (dump_file && (dump_flags & TDF_DETAILS))
10538 if (insn_code_number < 0)
10539 fputs ("Failed to match this instruction:\n", dump_file);
10540 else
10541 fputs ("Successfully matched this instruction:\n", dump_file);
10542 print_rtl_single (dump_file, pat);
10545 /* If it isn't, there is the possibility that we previously had an insn
10546 that clobbered some register as a side effect, but the combined
10547 insn doesn't need to do that. So try once more without the clobbers
10548 unless this represents an ASM insn. */
10550 if (insn_code_number < 0 && ! check_asm_operands (pat)
10551 && GET_CODE (pat) == PARALLEL)
10553 int pos;
10555 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10556 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10558 if (i != pos)
10559 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10560 pos++;
10563 SUBST_INT (XVECLEN (pat, 0), pos);
10565 if (pos == 1)
10566 pat = XVECEXP (pat, 0, 0);
10568 PATTERN (insn) = pat;
10569 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10570 if (dump_file && (dump_flags & TDF_DETAILS))
10572 if (insn_code_number < 0)
10573 fputs ("Failed to match this instruction:\n", dump_file);
10574 else
10575 fputs ("Successfully matched this instruction:\n", dump_file);
10576 print_rtl_single (dump_file, pat);
10579 PATTERN (insn) = old_pat;
10580 REG_NOTES (insn) = old_notes;
10582 /* Recognize all noop sets, these will be killed by followup pass. */
10583 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10584 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10586 /* If we had any clobbers to add, make a new pattern than contains
10587 them. Then check to make sure that all of them are dead. */
10588 if (num_clobbers_to_add)
10590 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10591 rtvec_alloc (GET_CODE (pat) == PARALLEL
10592 ? (XVECLEN (pat, 0)
10593 + num_clobbers_to_add)
10594 : num_clobbers_to_add + 1));
10596 if (GET_CODE (pat) == PARALLEL)
10597 for (i = 0; i < XVECLEN (pat, 0); i++)
10598 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10599 else
10600 XVECEXP (newpat, 0, 0) = pat;
10602 add_clobbers (newpat, insn_code_number);
10604 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10605 i < XVECLEN (newpat, 0); i++)
10607 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10608 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10609 return -1;
10610 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10612 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10613 notes = alloc_reg_note (REG_UNUSED,
10614 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10617 pat = newpat;
10620 *pnewpat = pat;
10621 *pnotes = notes;
10623 return insn_code_number;
10626 /* Like gen_lowpart_general but for use by combine. In combine it
10627 is not possible to create any new pseudoregs. However, it is
10628 safe to create invalid memory addresses, because combine will
10629 try to recognize them and all they will do is make the combine
10630 attempt fail.
10632 If for some reason this cannot do its job, an rtx
10633 (clobber (const_int 0)) is returned.
10634 An insn containing that will not be recognized. */
10636 static rtx
10637 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10639 enum machine_mode imode = GET_MODE (x);
10640 unsigned int osize = GET_MODE_SIZE (omode);
10641 unsigned int isize = GET_MODE_SIZE (imode);
10642 rtx result;
10644 if (omode == imode)
10645 return x;
10647 /* Return identity if this is a CONST or symbolic reference. */
10648 if (omode == Pmode
10649 && (GET_CODE (x) == CONST
10650 || GET_CODE (x) == SYMBOL_REF
10651 || GET_CODE (x) == LABEL_REF))
10652 return x;
10654 /* We can only support MODE being wider than a word if X is a
10655 constant integer or has a mode the same size. */
10656 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10657 && ! ((imode == VOIDmode
10658 && (CONST_INT_P (x)
10659 || GET_CODE (x) == CONST_DOUBLE))
10660 || isize == osize))
10661 goto fail;
10663 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10664 won't know what to do. So we will strip off the SUBREG here and
10665 process normally. */
10666 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10668 x = SUBREG_REG (x);
10670 /* For use in case we fall down into the address adjustments
10671 further below, we need to adjust the known mode and size of
10672 x; imode and isize, since we just adjusted x. */
10673 imode = GET_MODE (x);
10675 if (imode == omode)
10676 return x;
10678 isize = GET_MODE_SIZE (imode);
10681 result = gen_lowpart_common (omode, x);
10683 if (result)
10684 return result;
10686 if (MEM_P (x))
10688 int offset = 0;
10690 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10691 address. */
10692 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10693 goto fail;
10695 /* If we want to refer to something bigger than the original memref,
10696 generate a paradoxical subreg instead. That will force a reload
10697 of the original memref X. */
10698 if (isize < osize)
10699 return gen_rtx_SUBREG (omode, x, 0);
10701 if (WORDS_BIG_ENDIAN)
10702 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10704 /* Adjust the address so that the address-after-the-data is
10705 unchanged. */
10706 if (BYTES_BIG_ENDIAN)
10707 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10709 return adjust_address_nv (x, omode, offset);
10712 /* If X is a comparison operator, rewrite it in a new mode. This
10713 probably won't match, but may allow further simplifications. */
10714 else if (COMPARISON_P (x))
10715 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10717 /* If we couldn't simplify X any other way, just enclose it in a
10718 SUBREG. Normally, this SUBREG won't match, but some patterns may
10719 include an explicit SUBREG or we may simplify it further in combine. */
10720 else
10722 int offset = 0;
10723 rtx res;
10725 offset = subreg_lowpart_offset (omode, imode);
10726 if (imode == VOIDmode)
10728 imode = int_mode_for_mode (omode);
10729 x = gen_lowpart_common (imode, x);
10730 if (x == NULL)
10731 goto fail;
10733 res = simplify_gen_subreg (omode, x, imode, offset);
10734 if (res)
10735 return res;
10738 fail:
10739 return gen_rtx_CLOBBER (omode, const0_rtx);
10742 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10743 comparison code that will be tested.
10745 The result is a possibly different comparison code to use. *POP0 and
10746 *POP1 may be updated.
10748 It is possible that we might detect that a comparison is either always
10749 true or always false. However, we do not perform general constant
10750 folding in combine, so this knowledge isn't useful. Such tautologies
10751 should have been detected earlier. Hence we ignore all such cases. */
10753 static enum rtx_code
10754 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10756 rtx op0 = *pop0;
10757 rtx op1 = *pop1;
10758 rtx tem, tem1;
10759 int i;
10760 enum machine_mode mode, tmode;
10762 /* Try a few ways of applying the same transformation to both operands. */
10763 while (1)
10765 #ifndef WORD_REGISTER_OPERATIONS
10766 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10767 so check specially. */
10768 if (code != GTU && code != GEU && code != LTU && code != LEU
10769 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10770 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10771 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10772 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10773 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10774 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10775 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10776 && CONST_INT_P (XEXP (op0, 1))
10777 && XEXP (op0, 1) == XEXP (op1, 1)
10778 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10779 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10780 && (INTVAL (XEXP (op0, 1))
10781 == (GET_MODE_BITSIZE (GET_MODE (op0))
10782 - (GET_MODE_BITSIZE
10783 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10785 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10786 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10788 #endif
10790 /* If both operands are the same constant shift, see if we can ignore the
10791 shift. We can if the shift is a rotate or if the bits shifted out of
10792 this shift are known to be zero for both inputs and if the type of
10793 comparison is compatible with the shift. */
10794 if (GET_CODE (op0) == GET_CODE (op1)
10795 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10796 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10797 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10798 && (code != GT && code != LT && code != GE && code != LE))
10799 || (GET_CODE (op0) == ASHIFTRT
10800 && (code != GTU && code != LTU
10801 && code != GEU && code != LEU)))
10802 && CONST_INT_P (XEXP (op0, 1))
10803 && INTVAL (XEXP (op0, 1)) >= 0
10804 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10805 && XEXP (op0, 1) == XEXP (op1, 1))
10807 enum machine_mode mode = GET_MODE (op0);
10808 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10809 int shift_count = INTVAL (XEXP (op0, 1));
10811 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10812 mask &= (mask >> shift_count) << shift_count;
10813 else if (GET_CODE (op0) == ASHIFT)
10814 mask = (mask & (mask << shift_count)) >> shift_count;
10816 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10817 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10818 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10819 else
10820 break;
10823 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10824 SUBREGs are of the same mode, and, in both cases, the AND would
10825 be redundant if the comparison was done in the narrower mode,
10826 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10827 and the operand's possibly nonzero bits are 0xffffff01; in that case
10828 if we only care about QImode, we don't need the AND). This case
10829 occurs if the output mode of an scc insn is not SImode and
10830 STORE_FLAG_VALUE == 1 (e.g., the 386).
10832 Similarly, check for a case where the AND's are ZERO_EXTEND
10833 operations from some narrower mode even though a SUBREG is not
10834 present. */
10836 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10837 && CONST_INT_P (XEXP (op0, 1))
10838 && CONST_INT_P (XEXP (op1, 1)))
10840 rtx inner_op0 = XEXP (op0, 0);
10841 rtx inner_op1 = XEXP (op1, 0);
10842 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10843 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10844 int changed = 0;
10846 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10847 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10848 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10849 && (GET_MODE (SUBREG_REG (inner_op0))
10850 == GET_MODE (SUBREG_REG (inner_op1)))
10851 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10852 <= HOST_BITS_PER_WIDE_INT)
10853 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10854 GET_MODE (SUBREG_REG (inner_op0)))))
10855 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10856 GET_MODE (SUBREG_REG (inner_op1))))))
10858 op0 = SUBREG_REG (inner_op0);
10859 op1 = SUBREG_REG (inner_op1);
10861 /* The resulting comparison is always unsigned since we masked
10862 off the original sign bit. */
10863 code = unsigned_condition (code);
10865 changed = 1;
10868 else if (c0 == c1)
10869 for (tmode = GET_CLASS_NARROWEST_MODE
10870 (GET_MODE_CLASS (GET_MODE (op0)));
10871 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10872 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10874 op0 = gen_lowpart (tmode, inner_op0);
10875 op1 = gen_lowpart (tmode, inner_op1);
10876 code = unsigned_condition (code);
10877 changed = 1;
10878 break;
10881 if (! changed)
10882 break;
10885 /* If both operands are NOT, we can strip off the outer operation
10886 and adjust the comparison code for swapped operands; similarly for
10887 NEG, except that this must be an equality comparison. */
10888 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10889 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10890 && (code == EQ || code == NE)))
10891 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10893 else
10894 break;
10897 /* If the first operand is a constant, swap the operands and adjust the
10898 comparison code appropriately, but don't do this if the second operand
10899 is already a constant integer. */
10900 if (swap_commutative_operands_p (op0, op1))
10902 tem = op0, op0 = op1, op1 = tem;
10903 code = swap_condition (code);
10906 /* We now enter a loop during which we will try to simplify the comparison.
10907 For the most part, we only are concerned with comparisons with zero,
10908 but some things may really be comparisons with zero but not start
10909 out looking that way. */
10911 while (CONST_INT_P (op1))
10913 enum machine_mode mode = GET_MODE (op0);
10914 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10915 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10916 int equality_comparison_p;
10917 int sign_bit_comparison_p;
10918 int unsigned_comparison_p;
10919 HOST_WIDE_INT const_op;
10921 /* We only want to handle integral modes. This catches VOIDmode,
10922 CCmode, and the floating-point modes. An exception is that we
10923 can handle VOIDmode if OP0 is a COMPARE or a comparison
10924 operation. */
10926 if (GET_MODE_CLASS (mode) != MODE_INT
10927 && ! (mode == VOIDmode
10928 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10929 break;
10931 /* Get the constant we are comparing against and turn off all bits
10932 not on in our mode. */
10933 const_op = INTVAL (op1);
10934 if (mode != VOIDmode)
10935 const_op = trunc_int_for_mode (const_op, mode);
10936 op1 = GEN_INT (const_op);
10938 /* If we are comparing against a constant power of two and the value
10939 being compared can only have that single bit nonzero (e.g., it was
10940 `and'ed with that bit), we can replace this with a comparison
10941 with zero. */
10942 if (const_op
10943 && (code == EQ || code == NE || code == GE || code == GEU
10944 || code == LT || code == LTU)
10945 && mode_width <= HOST_BITS_PER_WIDE_INT
10946 && exact_log2 (const_op) >= 0
10947 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10949 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10950 op1 = const0_rtx, const_op = 0;
10953 /* Similarly, if we are comparing a value known to be either -1 or
10954 0 with -1, change it to the opposite comparison against zero. */
10956 if (const_op == -1
10957 && (code == EQ || code == NE || code == GT || code == LE
10958 || code == GEU || code == LTU)
10959 && num_sign_bit_copies (op0, mode) == mode_width)
10961 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10962 op1 = const0_rtx, const_op = 0;
10965 /* Do some canonicalizations based on the comparison code. We prefer
10966 comparisons against zero and then prefer equality comparisons.
10967 If we can reduce the size of a constant, we will do that too. */
10969 switch (code)
10971 case LT:
10972 /* < C is equivalent to <= (C - 1) */
10973 if (const_op > 0)
10975 const_op -= 1;
10976 op1 = GEN_INT (const_op);
10977 code = LE;
10978 /* ... fall through to LE case below. */
10980 else
10981 break;
10983 case LE:
10984 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10985 if (const_op < 0)
10987 const_op += 1;
10988 op1 = GEN_INT (const_op);
10989 code = LT;
10992 /* If we are doing a <= 0 comparison on a value known to have
10993 a zero sign bit, we can replace this with == 0. */
10994 else if (const_op == 0
10995 && mode_width <= HOST_BITS_PER_WIDE_INT
10996 && (nonzero_bits (op0, mode)
10997 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10998 == 0)
10999 code = EQ;
11000 break;
11002 case GE:
11003 /* >= C is equivalent to > (C - 1). */
11004 if (const_op > 0)
11006 const_op -= 1;
11007 op1 = GEN_INT (const_op);
11008 code = GT;
11009 /* ... fall through to GT below. */
11011 else
11012 break;
11014 case GT:
11015 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11016 if (const_op < 0)
11018 const_op += 1;
11019 op1 = GEN_INT (const_op);
11020 code = GE;
11023 /* If we are doing a > 0 comparison on a value known to have
11024 a zero sign bit, we can replace this with != 0. */
11025 else if (const_op == 0
11026 && mode_width <= HOST_BITS_PER_WIDE_INT
11027 && (nonzero_bits (op0, mode)
11028 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11029 == 0)
11030 code = NE;
11031 break;
11033 case LTU:
11034 /* < C is equivalent to <= (C - 1). */
11035 if (const_op > 0)
11037 const_op -= 1;
11038 op1 = GEN_INT (const_op);
11039 code = LEU;
11040 /* ... fall through ... */
11043 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11044 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11045 && (unsigned HOST_WIDE_INT) const_op
11046 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11048 const_op = 0, op1 = const0_rtx;
11049 code = GE;
11050 break;
11052 else
11053 break;
11055 case LEU:
11056 /* unsigned <= 0 is equivalent to == 0 */
11057 if (const_op == 0)
11058 code = EQ;
11060 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11061 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11062 && (unsigned HOST_WIDE_INT) const_op
11063 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11065 const_op = 0, op1 = const0_rtx;
11066 code = GE;
11068 break;
11070 case GEU:
11071 /* >= C is equivalent to > (C - 1). */
11072 if (const_op > 1)
11074 const_op -= 1;
11075 op1 = GEN_INT (const_op);
11076 code = GTU;
11077 /* ... fall through ... */
11080 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11081 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11082 && (unsigned HOST_WIDE_INT) const_op
11083 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11085 const_op = 0, op1 = const0_rtx;
11086 code = LT;
11087 break;
11089 else
11090 break;
11092 case GTU:
11093 /* unsigned > 0 is equivalent to != 0 */
11094 if (const_op == 0)
11095 code = NE;
11097 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11098 else if (mode_width <= HOST_BITS_PER_WIDE_INT
11099 && (unsigned HOST_WIDE_INT) const_op
11100 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11102 const_op = 0, op1 = const0_rtx;
11103 code = LT;
11105 break;
11107 default:
11108 break;
11111 /* Compute some predicates to simplify code below. */
11113 equality_comparison_p = (code == EQ || code == NE);
11114 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11115 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11116 || code == GEU);
11118 /* If this is a sign bit comparison and we can do arithmetic in
11119 MODE, say that we will only be needing the sign bit of OP0. */
11120 if (sign_bit_comparison_p
11121 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11122 op0 = force_to_mode (op0, mode,
11123 (unsigned HOST_WIDE_INT) 1
11124 << (GET_MODE_BITSIZE (mode) - 1),
11127 /* Now try cases based on the opcode of OP0. If none of the cases
11128 does a "continue", we exit this loop immediately after the
11129 switch. */
11131 switch (GET_CODE (op0))
11133 case ZERO_EXTRACT:
11134 /* If we are extracting a single bit from a variable position in
11135 a constant that has only a single bit set and are comparing it
11136 with zero, we can convert this into an equality comparison
11137 between the position and the location of the single bit. */
11138 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11139 have already reduced the shift count modulo the word size. */
11140 if (!SHIFT_COUNT_TRUNCATED
11141 && CONST_INT_P (XEXP (op0, 0))
11142 && XEXP (op0, 1) == const1_rtx
11143 && equality_comparison_p && const_op == 0
11144 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11146 if (BITS_BIG_ENDIAN)
11148 enum machine_mode new_mode
11149 = mode_for_extraction (EP_extzv, 1);
11150 if (new_mode == MAX_MACHINE_MODE)
11151 i = BITS_PER_WORD - 1 - i;
11152 else
11154 mode = new_mode;
11155 i = (GET_MODE_BITSIZE (mode) - 1 - i);
11159 op0 = XEXP (op0, 2);
11160 op1 = GEN_INT (i);
11161 const_op = i;
11163 /* Result is nonzero iff shift count is equal to I. */
11164 code = reverse_condition (code);
11165 continue;
11168 /* ... fall through ... */
11170 case SIGN_EXTRACT:
11171 tem = expand_compound_operation (op0);
11172 if (tem != op0)
11174 op0 = tem;
11175 continue;
11177 break;
11179 case NOT:
11180 /* If testing for equality, we can take the NOT of the constant. */
11181 if (equality_comparison_p
11182 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11184 op0 = XEXP (op0, 0);
11185 op1 = tem;
11186 continue;
11189 /* If just looking at the sign bit, reverse the sense of the
11190 comparison. */
11191 if (sign_bit_comparison_p)
11193 op0 = XEXP (op0, 0);
11194 code = (code == GE ? LT : GE);
11195 continue;
11197 break;
11199 case NEG:
11200 /* If testing for equality, we can take the NEG of the constant. */
11201 if (equality_comparison_p
11202 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11204 op0 = XEXP (op0, 0);
11205 op1 = tem;
11206 continue;
11209 /* The remaining cases only apply to comparisons with zero. */
11210 if (const_op != 0)
11211 break;
11213 /* When X is ABS or is known positive,
11214 (neg X) is < 0 if and only if X != 0. */
11216 if (sign_bit_comparison_p
11217 && (GET_CODE (XEXP (op0, 0)) == ABS
11218 || (mode_width <= HOST_BITS_PER_WIDE_INT
11219 && (nonzero_bits (XEXP (op0, 0), mode)
11220 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11221 == 0)))
11223 op0 = XEXP (op0, 0);
11224 code = (code == LT ? NE : EQ);
11225 continue;
11228 /* If we have NEG of something whose two high-order bits are the
11229 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11230 if (num_sign_bit_copies (op0, mode) >= 2)
11232 op0 = XEXP (op0, 0);
11233 code = swap_condition (code);
11234 continue;
11236 break;
11238 case ROTATE:
11239 /* If we are testing equality and our count is a constant, we
11240 can perform the inverse operation on our RHS. */
11241 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11242 && (tem = simplify_binary_operation (ROTATERT, mode,
11243 op1, XEXP (op0, 1))) != 0)
11245 op0 = XEXP (op0, 0);
11246 op1 = tem;
11247 continue;
11250 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11251 a particular bit. Convert it to an AND of a constant of that
11252 bit. This will be converted into a ZERO_EXTRACT. */
11253 if (const_op == 0 && sign_bit_comparison_p
11254 && CONST_INT_P (XEXP (op0, 1))
11255 && mode_width <= HOST_BITS_PER_WIDE_INT)
11257 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11258 ((unsigned HOST_WIDE_INT) 1
11259 << (mode_width - 1
11260 - INTVAL (XEXP (op0, 1)))));
11261 code = (code == LT ? NE : EQ);
11262 continue;
11265 /* Fall through. */
11267 case ABS:
11268 /* ABS is ignorable inside an equality comparison with zero. */
11269 if (const_op == 0 && equality_comparison_p)
11271 op0 = XEXP (op0, 0);
11272 continue;
11274 break;
11276 case SIGN_EXTEND:
11277 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11278 (compare FOO CONST) if CONST fits in FOO's mode and we
11279 are either testing inequality or have an unsigned
11280 comparison with ZERO_EXTEND or a signed comparison with
11281 SIGN_EXTEND. But don't do it if we don't have a compare
11282 insn of the given mode, since we'd have to revert it
11283 later on, and then we wouldn't know whether to sign- or
11284 zero-extend. */
11285 mode = GET_MODE (XEXP (op0, 0));
11286 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11287 && ! unsigned_comparison_p
11288 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11289 && ((unsigned HOST_WIDE_INT) const_op
11290 < (((unsigned HOST_WIDE_INT) 1
11291 << (GET_MODE_BITSIZE (mode) - 1))))
11292 && have_insn_for (COMPARE, mode))
11294 op0 = XEXP (op0, 0);
11295 continue;
11297 break;
11299 case SUBREG:
11300 /* Check for the case where we are comparing A - C1 with C2, that is
11302 (subreg:MODE (plus (A) (-C1))) op (C2)
11304 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11305 comparison in the wider mode. One of the following two conditions
11306 must be true in order for this to be valid:
11308 1. The mode extension results in the same bit pattern being added
11309 on both sides and the comparison is equality or unsigned. As
11310 C2 has been truncated to fit in MODE, the pattern can only be
11311 all 0s or all 1s.
11313 2. The mode extension results in the sign bit being copied on
11314 each side.
11316 The difficulty here is that we have predicates for A but not for
11317 (A - C1) so we need to check that C1 is within proper bounds so
11318 as to perturbate A as little as possible. */
11320 if (mode_width <= HOST_BITS_PER_WIDE_INT
11321 && subreg_lowpart_p (op0)
11322 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
11323 && GET_CODE (SUBREG_REG (op0)) == PLUS
11324 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11326 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11327 rtx a = XEXP (SUBREG_REG (op0), 0);
11328 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11330 if ((c1 > 0
11331 && (unsigned HOST_WIDE_INT) c1
11332 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11333 && (equality_comparison_p || unsigned_comparison_p)
11334 /* (A - C1) zero-extends if it is positive and sign-extends
11335 if it is negative, C2 both zero- and sign-extends. */
11336 && ((0 == (nonzero_bits (a, inner_mode)
11337 & ~GET_MODE_MASK (mode))
11338 && const_op >= 0)
11339 /* (A - C1) sign-extends if it is positive and 1-extends
11340 if it is negative, C2 both sign- and 1-extends. */
11341 || (num_sign_bit_copies (a, inner_mode)
11342 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11343 - mode_width)
11344 && const_op < 0)))
11345 || ((unsigned HOST_WIDE_INT) c1
11346 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11347 /* (A - C1) always sign-extends, like C2. */
11348 && num_sign_bit_copies (a, inner_mode)
11349 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11350 - (mode_width - 1))))
11352 op0 = SUBREG_REG (op0);
11353 continue;
11357 /* If the inner mode is narrower and we are extracting the low part,
11358 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11359 if (subreg_lowpart_p (op0)
11360 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
11361 /* Fall through */ ;
11362 else
11363 break;
11365 /* ... fall through ... */
11367 case ZERO_EXTEND:
11368 mode = GET_MODE (XEXP (op0, 0));
11369 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11370 && (unsigned_comparison_p || equality_comparison_p)
11371 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11372 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
11373 && have_insn_for (COMPARE, mode))
11375 op0 = XEXP (op0, 0);
11376 continue;
11378 break;
11380 case PLUS:
11381 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11382 this for equality comparisons due to pathological cases involving
11383 overflows. */
11384 if (equality_comparison_p
11385 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11386 op1, XEXP (op0, 1))))
11388 op0 = XEXP (op0, 0);
11389 op1 = tem;
11390 continue;
11393 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11394 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11395 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11397 op0 = XEXP (XEXP (op0, 0), 0);
11398 code = (code == LT ? EQ : NE);
11399 continue;
11401 break;
11403 case MINUS:
11404 /* We used to optimize signed comparisons against zero, but that
11405 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11406 arrive here as equality comparisons, or (GEU, LTU) are
11407 optimized away. No need to special-case them. */
11409 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11410 (eq B (minus A C)), whichever simplifies. We can only do
11411 this for equality comparisons due to pathological cases involving
11412 overflows. */
11413 if (equality_comparison_p
11414 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11415 XEXP (op0, 1), op1)))
11417 op0 = XEXP (op0, 0);
11418 op1 = tem;
11419 continue;
11422 if (equality_comparison_p
11423 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11424 XEXP (op0, 0), op1)))
11426 op0 = XEXP (op0, 1);
11427 op1 = tem;
11428 continue;
11431 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11432 of bits in X minus 1, is one iff X > 0. */
11433 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11434 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11435 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11436 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11438 op0 = XEXP (op0, 1);
11439 code = (code == GE ? LE : GT);
11440 continue;
11442 break;
11444 case XOR:
11445 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11446 if C is zero or B is a constant. */
11447 if (equality_comparison_p
11448 && 0 != (tem = simplify_binary_operation (XOR, mode,
11449 XEXP (op0, 1), op1)))
11451 op0 = XEXP (op0, 0);
11452 op1 = tem;
11453 continue;
11455 break;
11457 case EQ: case NE:
11458 case UNEQ: case LTGT:
11459 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11460 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11461 case UNORDERED: case ORDERED:
11462 /* We can't do anything if OP0 is a condition code value, rather
11463 than an actual data value. */
11464 if (const_op != 0
11465 || CC0_P (XEXP (op0, 0))
11466 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11467 break;
11469 /* Get the two operands being compared. */
11470 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11471 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11472 else
11473 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11475 /* Check for the cases where we simply want the result of the
11476 earlier test or the opposite of that result. */
11477 if (code == NE || code == EQ
11478 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11479 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11480 && (STORE_FLAG_VALUE
11481 & (((unsigned HOST_WIDE_INT) 1
11482 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11483 && (code == LT || code == GE)))
11485 enum rtx_code new_code;
11486 if (code == LT || code == NE)
11487 new_code = GET_CODE (op0);
11488 else
11489 new_code = reversed_comparison_code (op0, NULL);
11491 if (new_code != UNKNOWN)
11493 code = new_code;
11494 op0 = tem;
11495 op1 = tem1;
11496 continue;
11499 break;
11501 case IOR:
11502 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11503 iff X <= 0. */
11504 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11505 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11506 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11508 op0 = XEXP (op0, 1);
11509 code = (code == GE ? GT : LE);
11510 continue;
11512 break;
11514 case AND:
11515 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11516 will be converted to a ZERO_EXTRACT later. */
11517 if (const_op == 0 && equality_comparison_p
11518 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11519 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11521 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11522 XEXP (XEXP (op0, 0), 1));
11523 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11524 continue;
11527 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11528 zero and X is a comparison and C1 and C2 describe only bits set
11529 in STORE_FLAG_VALUE, we can compare with X. */
11530 if (const_op == 0 && equality_comparison_p
11531 && mode_width <= HOST_BITS_PER_WIDE_INT
11532 && CONST_INT_P (XEXP (op0, 1))
11533 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11534 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11535 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11536 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11538 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11539 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11540 if ((~STORE_FLAG_VALUE & mask) == 0
11541 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11542 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11543 && COMPARISON_P (tem))))
11545 op0 = XEXP (XEXP (op0, 0), 0);
11546 continue;
11550 /* If we are doing an equality comparison of an AND of a bit equal
11551 to the sign bit, replace this with a LT or GE comparison of
11552 the underlying value. */
11553 if (equality_comparison_p
11554 && const_op == 0
11555 && CONST_INT_P (XEXP (op0, 1))
11556 && mode_width <= HOST_BITS_PER_WIDE_INT
11557 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11558 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11560 op0 = XEXP (op0, 0);
11561 code = (code == EQ ? GE : LT);
11562 continue;
11565 /* If this AND operation is really a ZERO_EXTEND from a narrower
11566 mode, the constant fits within that mode, and this is either an
11567 equality or unsigned comparison, try to do this comparison in
11568 the narrower mode.
11570 Note that in:
11572 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11573 -> (ne:DI (reg:SI 4) (const_int 0))
11575 unless TRULY_NOOP_TRUNCATION allows it or the register is
11576 known to hold a value of the required mode the
11577 transformation is invalid. */
11578 if ((equality_comparison_p || unsigned_comparison_p)
11579 && CONST_INT_P (XEXP (op0, 1))
11580 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11581 & GET_MODE_MASK (mode))
11582 + 1)) >= 0
11583 && const_op >> i == 0
11584 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11585 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11586 GET_MODE_BITSIZE (GET_MODE (op0)))
11587 || (REG_P (XEXP (op0, 0))
11588 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11590 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11591 continue;
11594 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11595 fits in both M1 and M2 and the SUBREG is either paradoxical
11596 or represents the low part, permute the SUBREG and the AND
11597 and try again. */
11598 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11600 unsigned HOST_WIDE_INT c1;
11601 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11602 /* Require an integral mode, to avoid creating something like
11603 (AND:SF ...). */
11604 if (SCALAR_INT_MODE_P (tmode)
11605 /* It is unsafe to commute the AND into the SUBREG if the
11606 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11607 not defined. As originally written the upper bits
11608 have a defined value due to the AND operation.
11609 However, if we commute the AND inside the SUBREG then
11610 they no longer have defined values and the meaning of
11611 the code has been changed. */
11612 && (0
11613 #ifdef WORD_REGISTER_OPERATIONS
11614 || (mode_width > GET_MODE_BITSIZE (tmode)
11615 && mode_width <= BITS_PER_WORD)
11616 #endif
11617 || (mode_width <= GET_MODE_BITSIZE (tmode)
11618 && subreg_lowpart_p (XEXP (op0, 0))))
11619 && CONST_INT_P (XEXP (op0, 1))
11620 && mode_width <= HOST_BITS_PER_WIDE_INT
11621 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11622 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11623 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11624 && c1 != mask
11625 && c1 != GET_MODE_MASK (tmode))
11627 op0 = simplify_gen_binary (AND, tmode,
11628 SUBREG_REG (XEXP (op0, 0)),
11629 gen_int_mode (c1, tmode));
11630 op0 = gen_lowpart (mode, op0);
11631 continue;
11635 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11636 if (const_op == 0 && equality_comparison_p
11637 && XEXP (op0, 1) == const1_rtx
11638 && GET_CODE (XEXP (op0, 0)) == NOT)
11640 op0 = simplify_and_const_int (NULL_RTX, mode,
11641 XEXP (XEXP (op0, 0), 0), 1);
11642 code = (code == NE ? EQ : NE);
11643 continue;
11646 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11647 (eq (and (lshiftrt X) 1) 0).
11648 Also handle the case where (not X) is expressed using xor. */
11649 if (const_op == 0 && equality_comparison_p
11650 && XEXP (op0, 1) == const1_rtx
11651 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11653 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11654 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11656 if (GET_CODE (shift_op) == NOT
11657 || (GET_CODE (shift_op) == XOR
11658 && CONST_INT_P (XEXP (shift_op, 1))
11659 && CONST_INT_P (shift_count)
11660 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11661 && (UINTVAL (XEXP (shift_op, 1))
11662 == (unsigned HOST_WIDE_INT) 1
11663 << INTVAL (shift_count))))
11666 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11667 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11668 code = (code == NE ? EQ : NE);
11669 continue;
11672 break;
11674 case ASHIFT:
11675 /* If we have (compare (ashift FOO N) (const_int C)) and
11676 the high order N bits of FOO (N+1 if an inequality comparison)
11677 are known to be zero, we can do this by comparing FOO with C
11678 shifted right N bits so long as the low-order N bits of C are
11679 zero. */
11680 if (CONST_INT_P (XEXP (op0, 1))
11681 && INTVAL (XEXP (op0, 1)) >= 0
11682 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11683 < HOST_BITS_PER_WIDE_INT)
11684 && (((unsigned HOST_WIDE_INT) const_op
11685 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11686 - 1)) == 0)
11687 && mode_width <= HOST_BITS_PER_WIDE_INT
11688 && (nonzero_bits (XEXP (op0, 0), mode)
11689 & ~(mask >> (INTVAL (XEXP (op0, 1))
11690 + ! equality_comparison_p))) == 0)
11692 /* We must perform a logical shift, not an arithmetic one,
11693 as we want the top N bits of C to be zero. */
11694 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11696 temp >>= INTVAL (XEXP (op0, 1));
11697 op1 = gen_int_mode (temp, mode);
11698 op0 = XEXP (op0, 0);
11699 continue;
11702 /* If we are doing a sign bit comparison, it means we are testing
11703 a particular bit. Convert it to the appropriate AND. */
11704 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11705 && mode_width <= HOST_BITS_PER_WIDE_INT)
11707 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11708 ((unsigned HOST_WIDE_INT) 1
11709 << (mode_width - 1
11710 - INTVAL (XEXP (op0, 1)))));
11711 code = (code == LT ? NE : EQ);
11712 continue;
11715 /* If this an equality comparison with zero and we are shifting
11716 the low bit to the sign bit, we can convert this to an AND of the
11717 low-order bit. */
11718 if (const_op == 0 && equality_comparison_p
11719 && CONST_INT_P (XEXP (op0, 1))
11720 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11722 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11723 continue;
11725 break;
11727 case ASHIFTRT:
11728 /* If this is an equality comparison with zero, we can do this
11729 as a logical shift, which might be much simpler. */
11730 if (equality_comparison_p && const_op == 0
11731 && CONST_INT_P (XEXP (op0, 1)))
11733 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11734 XEXP (op0, 0),
11735 INTVAL (XEXP (op0, 1)));
11736 continue;
11739 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11740 do the comparison in a narrower mode. */
11741 if (! unsigned_comparison_p
11742 && CONST_INT_P (XEXP (op0, 1))
11743 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11744 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11745 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11746 MODE_INT, 1)) != BLKmode
11747 && (((unsigned HOST_WIDE_INT) const_op
11748 + (GET_MODE_MASK (tmode) >> 1) + 1)
11749 <= GET_MODE_MASK (tmode)))
11751 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11752 continue;
11755 /* Likewise if OP0 is a PLUS of a sign extension with a
11756 constant, which is usually represented with the PLUS
11757 between the shifts. */
11758 if (! unsigned_comparison_p
11759 && CONST_INT_P (XEXP (op0, 1))
11760 && GET_CODE (XEXP (op0, 0)) == PLUS
11761 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11762 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11763 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11764 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11765 MODE_INT, 1)) != BLKmode
11766 && (((unsigned HOST_WIDE_INT) const_op
11767 + (GET_MODE_MASK (tmode) >> 1) + 1)
11768 <= GET_MODE_MASK (tmode)))
11770 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11771 rtx add_const = XEXP (XEXP (op0, 0), 1);
11772 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11773 add_const, XEXP (op0, 1));
11775 op0 = simplify_gen_binary (PLUS, tmode,
11776 gen_lowpart (tmode, inner),
11777 new_const);
11778 continue;
11781 /* ... fall through ... */
11782 case LSHIFTRT:
11783 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11784 the low order N bits of FOO are known to be zero, we can do this
11785 by comparing FOO with C shifted left N bits so long as no
11786 overflow occurs. Even if the low order N bits of FOO aren't known
11787 to be zero, if the comparison is >= or < we can use the same
11788 optimization and for > or <= by setting all the low
11789 order N bits in the comparison constant. */
11790 if (CONST_INT_P (XEXP (op0, 1))
11791 && INTVAL (XEXP (op0, 1)) > 0
11792 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11793 && mode_width <= HOST_BITS_PER_WIDE_INT
11794 && (((unsigned HOST_WIDE_INT) const_op
11795 + (GET_CODE (op0) != LSHIFTRT
11796 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11797 + 1)
11798 : 0))
11799 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11801 unsigned HOST_WIDE_INT low_bits
11802 = (nonzero_bits (XEXP (op0, 0), mode)
11803 & (((unsigned HOST_WIDE_INT) 1
11804 << INTVAL (XEXP (op0, 1))) - 1));
11805 if (low_bits == 0 || !equality_comparison_p)
11807 /* If the shift was logical, then we must make the condition
11808 unsigned. */
11809 if (GET_CODE (op0) == LSHIFTRT)
11810 code = unsigned_condition (code);
11812 const_op <<= INTVAL (XEXP (op0, 1));
11813 if (low_bits != 0
11814 && (code == GT || code == GTU
11815 || code == LE || code == LEU))
11816 const_op
11817 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11818 op1 = GEN_INT (const_op);
11819 op0 = XEXP (op0, 0);
11820 continue;
11824 /* If we are using this shift to extract just the sign bit, we
11825 can replace this with an LT or GE comparison. */
11826 if (const_op == 0
11827 && (equality_comparison_p || sign_bit_comparison_p)
11828 && CONST_INT_P (XEXP (op0, 1))
11829 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11831 op0 = XEXP (op0, 0);
11832 code = (code == NE || code == GT ? LT : GE);
11833 continue;
11835 break;
11837 default:
11838 break;
11841 break;
11844 /* Now make any compound operations involved in this comparison. Then,
11845 check for an outmost SUBREG on OP0 that is not doing anything or is
11846 paradoxical. The latter transformation must only be performed when
11847 it is known that the "extra" bits will be the same in op0 and op1 or
11848 that they don't matter. There are three cases to consider:
11850 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11851 care bits and we can assume they have any convenient value. So
11852 making the transformation is safe.
11854 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11855 In this case the upper bits of op0 are undefined. We should not make
11856 the simplification in that case as we do not know the contents of
11857 those bits.
11859 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11860 UNKNOWN. In that case we know those bits are zeros or ones. We must
11861 also be sure that they are the same as the upper bits of op1.
11863 We can never remove a SUBREG for a non-equality comparison because
11864 the sign bit is in a different place in the underlying object. */
11866 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11867 op1 = make_compound_operation (op1, SET);
11869 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11870 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11871 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11872 && (code == NE || code == EQ))
11874 if (GET_MODE_SIZE (GET_MODE (op0))
11875 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11877 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11878 implemented. */
11879 if (REG_P (SUBREG_REG (op0)))
11881 op0 = SUBREG_REG (op0);
11882 op1 = gen_lowpart (GET_MODE (op0), op1);
11885 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11886 <= HOST_BITS_PER_WIDE_INT)
11887 && (nonzero_bits (SUBREG_REG (op0),
11888 GET_MODE (SUBREG_REG (op0)))
11889 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11891 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11893 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11894 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11895 op0 = SUBREG_REG (op0), op1 = tem;
11899 /* We now do the opposite procedure: Some machines don't have compare
11900 insns in all modes. If OP0's mode is an integer mode smaller than a
11901 word and we can't do a compare in that mode, see if there is a larger
11902 mode for which we can do the compare. There are a number of cases in
11903 which we can use the wider mode. */
11905 mode = GET_MODE (op0);
11906 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11907 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11908 && ! have_insn_for (COMPARE, mode))
11909 for (tmode = GET_MODE_WIDER_MODE (mode);
11910 (tmode != VOIDmode
11911 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11912 tmode = GET_MODE_WIDER_MODE (tmode))
11913 if (have_insn_for (COMPARE, tmode))
11915 int zero_extended;
11917 /* If this is a test for negative, we can make an explicit
11918 test of the sign bit. Test this first so we can use
11919 a paradoxical subreg to extend OP0. */
11921 if (op1 == const0_rtx && (code == LT || code == GE)
11922 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11924 op0 = simplify_gen_binary (AND, tmode,
11925 gen_lowpart (tmode, op0),
11926 GEN_INT ((unsigned HOST_WIDE_INT) 1
11927 << (GET_MODE_BITSIZE (mode)
11928 - 1)));
11929 code = (code == LT) ? NE : EQ;
11930 break;
11933 /* If the only nonzero bits in OP0 and OP1 are those in the
11934 narrower mode and this is an equality or unsigned comparison,
11935 we can use the wider mode. Similarly for sign-extended
11936 values, in which case it is true for all comparisons. */
11937 zero_extended = ((code == EQ || code == NE
11938 || code == GEU || code == GTU
11939 || code == LEU || code == LTU)
11940 && (nonzero_bits (op0, tmode)
11941 & ~GET_MODE_MASK (mode)) == 0
11942 && ((CONST_INT_P (op1)
11943 || (nonzero_bits (op1, tmode)
11944 & ~GET_MODE_MASK (mode)) == 0)));
11946 if (zero_extended
11947 || ((num_sign_bit_copies (op0, tmode)
11948 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11949 - GET_MODE_BITSIZE (mode)))
11950 && (num_sign_bit_copies (op1, tmode)
11951 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11952 - GET_MODE_BITSIZE (mode)))))
11954 /* If OP0 is an AND and we don't have an AND in MODE either,
11955 make a new AND in the proper mode. */
11956 if (GET_CODE (op0) == AND
11957 && !have_insn_for (AND, mode))
11958 op0 = simplify_gen_binary (AND, tmode,
11959 gen_lowpart (tmode,
11960 XEXP (op0, 0)),
11961 gen_lowpart (tmode,
11962 XEXP (op0, 1)));
11963 else
11965 if (zero_extended)
11967 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11968 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11970 else
11972 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11973 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11975 break;
11980 #ifdef CANONICALIZE_COMPARISON
11981 /* If this machine only supports a subset of valid comparisons, see if we
11982 can convert an unsupported one into a supported one. */
11983 CANONICALIZE_COMPARISON (code, op0, op1);
11984 #endif
11986 *pop0 = op0;
11987 *pop1 = op1;
11989 return code;
11992 /* Utility function for record_value_for_reg. Count number of
11993 rtxs in X. */
11994 static int
11995 count_rtxs (rtx x)
11997 enum rtx_code code = GET_CODE (x);
11998 const char *fmt;
11999 int i, j, ret = 1;
12001 if (GET_RTX_CLASS (code) == '2'
12002 || GET_RTX_CLASS (code) == 'c')
12004 rtx x0 = XEXP (x, 0);
12005 rtx x1 = XEXP (x, 1);
12007 if (x0 == x1)
12008 return 1 + 2 * count_rtxs (x0);
12010 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
12011 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
12012 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12013 return 2 + 2 * count_rtxs (x0)
12014 + count_rtxs (x == XEXP (x1, 0)
12015 ? XEXP (x1, 1) : XEXP (x1, 0));
12017 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
12018 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
12019 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12020 return 2 + 2 * count_rtxs (x1)
12021 + count_rtxs (x == XEXP (x0, 0)
12022 ? XEXP (x0, 1) : XEXP (x0, 0));
12025 fmt = GET_RTX_FORMAT (code);
12026 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12027 if (fmt[i] == 'e')
12028 ret += count_rtxs (XEXP (x, i));
12029 else if (fmt[i] == 'E')
12030 for (j = 0; j < XVECLEN (x, i); j++)
12031 ret += count_rtxs (XVECEXP (x, i, j));
12033 return ret;
12036 /* Utility function for following routine. Called when X is part of a value
12037 being stored into last_set_value. Sets last_set_table_tick
12038 for each register mentioned. Similar to mention_regs in cse.c */
12040 static void
12041 update_table_tick (rtx x)
12043 enum rtx_code code = GET_CODE (x);
12044 const char *fmt = GET_RTX_FORMAT (code);
12045 int i, j;
12047 if (code == REG)
12049 unsigned int regno = REGNO (x);
12050 unsigned int endregno = END_REGNO (x);
12051 unsigned int r;
12053 for (r = regno; r < endregno; r++)
12055 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
12056 rsp->last_set_table_tick = label_tick;
12059 return;
12062 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12063 if (fmt[i] == 'e')
12065 /* Check for identical subexpressions. If x contains
12066 identical subexpression we only have to traverse one of
12067 them. */
12068 if (i == 0 && ARITHMETIC_P (x))
12070 /* Note that at this point x1 has already been
12071 processed. */
12072 rtx x0 = XEXP (x, 0);
12073 rtx x1 = XEXP (x, 1);
12075 /* If x0 and x1 are identical then there is no need to
12076 process x0. */
12077 if (x0 == x1)
12078 break;
12080 /* If x0 is identical to a subexpression of x1 then while
12081 processing x1, x0 has already been processed. Thus we
12082 are done with x. */
12083 if (ARITHMETIC_P (x1)
12084 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12085 break;
12087 /* If x1 is identical to a subexpression of x0 then we
12088 still have to process the rest of x0. */
12089 if (ARITHMETIC_P (x0)
12090 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12092 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12093 break;
12097 update_table_tick (XEXP (x, i));
12099 else if (fmt[i] == 'E')
12100 for (j = 0; j < XVECLEN (x, i); j++)
12101 update_table_tick (XVECEXP (x, i, j));
12104 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12105 are saying that the register is clobbered and we no longer know its
12106 value. If INSN is zero, don't update reg_stat[].last_set; this is
12107 only permitted with VALUE also zero and is used to invalidate the
12108 register. */
12110 static void
12111 record_value_for_reg (rtx reg, rtx insn, rtx value)
12113 unsigned int regno = REGNO (reg);
12114 unsigned int endregno = END_REGNO (reg);
12115 unsigned int i;
12116 reg_stat_type *rsp;
12118 /* If VALUE contains REG and we have a previous value for REG, substitute
12119 the previous value. */
12120 if (value && insn && reg_overlap_mentioned_p (reg, value))
12122 rtx tem;
12124 /* Set things up so get_last_value is allowed to see anything set up to
12125 our insn. */
12126 subst_low_luid = DF_INSN_LUID (insn);
12127 tem = get_last_value (reg);
12129 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12130 it isn't going to be useful and will take a lot of time to process,
12131 so just use the CLOBBER. */
12133 if (tem)
12135 if (ARITHMETIC_P (tem)
12136 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12137 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12138 tem = XEXP (tem, 0);
12139 else if (count_occurrences (value, reg, 1) >= 2)
12141 /* If there are two or more occurrences of REG in VALUE,
12142 prevent the value from growing too much. */
12143 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12144 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12147 value = replace_rtx (copy_rtx (value), reg, tem);
12151 /* For each register modified, show we don't know its value, that
12152 we don't know about its bitwise content, that its value has been
12153 updated, and that we don't know the location of the death of the
12154 register. */
12155 for (i = regno; i < endregno; i++)
12157 rsp = VEC_index (reg_stat_type, reg_stat, i);
12159 if (insn)
12160 rsp->last_set = insn;
12162 rsp->last_set_value = 0;
12163 rsp->last_set_mode = VOIDmode;
12164 rsp->last_set_nonzero_bits = 0;
12165 rsp->last_set_sign_bit_copies = 0;
12166 rsp->last_death = 0;
12167 rsp->truncated_to_mode = VOIDmode;
12170 /* Mark registers that are being referenced in this value. */
12171 if (value)
12172 update_table_tick (value);
12174 /* Now update the status of each register being set.
12175 If someone is using this register in this block, set this register
12176 to invalid since we will get confused between the two lives in this
12177 basic block. This makes using this register always invalid. In cse, we
12178 scan the table to invalidate all entries using this register, but this
12179 is too much work for us. */
12181 for (i = regno; i < endregno; i++)
12183 rsp = VEC_index (reg_stat_type, reg_stat, i);
12184 rsp->last_set_label = label_tick;
12185 if (!insn
12186 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12187 rsp->last_set_invalid = 1;
12188 else
12189 rsp->last_set_invalid = 0;
12192 /* The value being assigned might refer to X (like in "x++;"). In that
12193 case, we must replace it with (clobber (const_int 0)) to prevent
12194 infinite loops. */
12195 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12196 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12198 value = copy_rtx (value);
12199 if (!get_last_value_validate (&value, insn, label_tick, 1))
12200 value = 0;
12203 /* For the main register being modified, update the value, the mode, the
12204 nonzero bits, and the number of sign bit copies. */
12206 rsp->last_set_value = value;
12208 if (value)
12210 enum machine_mode mode = GET_MODE (reg);
12211 subst_low_luid = DF_INSN_LUID (insn);
12212 rsp->last_set_mode = mode;
12213 if (GET_MODE_CLASS (mode) == MODE_INT
12214 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12215 mode = nonzero_bits_mode;
12216 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12217 rsp->last_set_sign_bit_copies
12218 = num_sign_bit_copies (value, GET_MODE (reg));
12222 /* Called via note_stores from record_dead_and_set_regs to handle one
12223 SET or CLOBBER in an insn. DATA is the instruction in which the
12224 set is occurring. */
12226 static void
12227 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12229 rtx record_dead_insn = (rtx) data;
12231 if (GET_CODE (dest) == SUBREG)
12232 dest = SUBREG_REG (dest);
12234 if (!record_dead_insn)
12236 if (REG_P (dest))
12237 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12238 return;
12241 if (REG_P (dest))
12243 /* If we are setting the whole register, we know its value. Otherwise
12244 show that we don't know the value. We can handle SUBREG in
12245 some cases. */
12246 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12247 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12248 else if (GET_CODE (setter) == SET
12249 && GET_CODE (SET_DEST (setter)) == SUBREG
12250 && SUBREG_REG (SET_DEST (setter)) == dest
12251 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
12252 && subreg_lowpart_p (SET_DEST (setter)))
12253 record_value_for_reg (dest, record_dead_insn,
12254 gen_lowpart (GET_MODE (dest),
12255 SET_SRC (setter)));
12256 else
12257 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12259 else if (MEM_P (dest)
12260 /* Ignore pushes, they clobber nothing. */
12261 && ! push_operand (dest, GET_MODE (dest)))
12262 mem_last_set = DF_INSN_LUID (record_dead_insn);
12265 /* Update the records of when each REG was most recently set or killed
12266 for the things done by INSN. This is the last thing done in processing
12267 INSN in the combiner loop.
12269 We update reg_stat[], in particular fields last_set, last_set_value,
12270 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12271 last_death, and also the similar information mem_last_set (which insn
12272 most recently modified memory) and last_call_luid (which insn was the
12273 most recent subroutine call). */
12275 static void
12276 record_dead_and_set_regs (rtx insn)
12278 rtx link;
12279 unsigned int i;
12281 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12283 if (REG_NOTE_KIND (link) == REG_DEAD
12284 && REG_P (XEXP (link, 0)))
12286 unsigned int regno = REGNO (XEXP (link, 0));
12287 unsigned int endregno = END_REGNO (XEXP (link, 0));
12289 for (i = regno; i < endregno; i++)
12291 reg_stat_type *rsp;
12293 rsp = VEC_index (reg_stat_type, reg_stat, i);
12294 rsp->last_death = insn;
12297 else if (REG_NOTE_KIND (link) == REG_INC)
12298 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12301 if (CALL_P (insn))
12303 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12304 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12306 reg_stat_type *rsp;
12308 rsp = VEC_index (reg_stat_type, reg_stat, i);
12309 rsp->last_set_invalid = 1;
12310 rsp->last_set = insn;
12311 rsp->last_set_value = 0;
12312 rsp->last_set_mode = VOIDmode;
12313 rsp->last_set_nonzero_bits = 0;
12314 rsp->last_set_sign_bit_copies = 0;
12315 rsp->last_death = 0;
12316 rsp->truncated_to_mode = VOIDmode;
12319 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12321 /* We can't combine into a call pattern. Remember, though, that
12322 the return value register is set at this LUID. We could
12323 still replace a register with the return value from the
12324 wrong subroutine call! */
12325 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12327 else
12328 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12331 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12332 register present in the SUBREG, so for each such SUBREG go back and
12333 adjust nonzero and sign bit information of the registers that are
12334 known to have some zero/sign bits set.
12336 This is needed because when combine blows the SUBREGs away, the
12337 information on zero/sign bits is lost and further combines can be
12338 missed because of that. */
12340 static void
12341 record_promoted_value (rtx insn, rtx subreg)
12343 rtx links, set;
12344 unsigned int regno = REGNO (SUBREG_REG (subreg));
12345 enum machine_mode mode = GET_MODE (subreg);
12347 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
12348 return;
12350 for (links = LOG_LINKS (insn); links;)
12352 reg_stat_type *rsp;
12354 insn = XEXP (links, 0);
12355 set = single_set (insn);
12357 if (! set || !REG_P (SET_DEST (set))
12358 || REGNO (SET_DEST (set)) != regno
12359 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12361 links = XEXP (links, 1);
12362 continue;
12365 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12366 if (rsp->last_set == insn)
12368 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12369 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12372 if (REG_P (SET_SRC (set)))
12374 regno = REGNO (SET_SRC (set));
12375 links = LOG_LINKS (insn);
12377 else
12378 break;
12382 /* Check if X, a register, is known to contain a value already
12383 truncated to MODE. In this case we can use a subreg to refer to
12384 the truncated value even though in the generic case we would need
12385 an explicit truncation. */
12387 static bool
12388 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12390 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12391 enum machine_mode truncated = rsp->truncated_to_mode;
12393 if (truncated == 0
12394 || rsp->truncation_label < label_tick_ebb_start)
12395 return false;
12396 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12397 return true;
12398 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
12399 GET_MODE_BITSIZE (truncated)))
12400 return true;
12401 return false;
12404 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
12405 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
12406 might be able to turn a truncate into a subreg using this information.
12407 Return -1 if traversing *P is complete or 0 otherwise. */
12409 static int
12410 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12412 rtx x = *p;
12413 enum machine_mode truncated_mode;
12414 reg_stat_type *rsp;
12416 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12418 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12419 truncated_mode = GET_MODE (x);
12421 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12422 return -1;
12424 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
12425 GET_MODE_BITSIZE (original_mode)))
12426 return -1;
12428 x = SUBREG_REG (x);
12430 /* ??? For hard-regs we now record everything. We might be able to
12431 optimize this using last_set_mode. */
12432 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12433 truncated_mode = GET_MODE (x);
12434 else
12435 return 0;
12437 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12438 if (rsp->truncated_to_mode == 0
12439 || rsp->truncation_label < label_tick_ebb_start
12440 || (GET_MODE_SIZE (truncated_mode)
12441 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12443 rsp->truncated_to_mode = truncated_mode;
12444 rsp->truncation_label = label_tick;
12447 return -1;
12450 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12451 the modes they are used in. This can help truning TRUNCATEs into
12452 SUBREGs. */
12454 static void
12455 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12457 for_each_rtx (x, record_truncated_value, NULL);
12460 /* Scan X for promoted SUBREGs. For each one found,
12461 note what it implies to the registers used in it. */
12463 static void
12464 check_promoted_subreg (rtx insn, rtx x)
12466 if (GET_CODE (x) == SUBREG
12467 && SUBREG_PROMOTED_VAR_P (x)
12468 && REG_P (SUBREG_REG (x)))
12469 record_promoted_value (insn, x);
12470 else
12472 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12473 int i, j;
12475 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12476 switch (format[i])
12478 case 'e':
12479 check_promoted_subreg (insn, XEXP (x, i));
12480 break;
12481 case 'V':
12482 case 'E':
12483 if (XVEC (x, i) != 0)
12484 for (j = 0; j < XVECLEN (x, i); j++)
12485 check_promoted_subreg (insn, XVECEXP (x, i, j));
12486 break;
12491 /* Verify that all the registers and memory references mentioned in *LOC are
12492 still valid. *LOC was part of a value set in INSN when label_tick was
12493 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12494 the invalid references with (clobber (const_int 0)) and return 1. This
12495 replacement is useful because we often can get useful information about
12496 the form of a value (e.g., if it was produced by a shift that always
12497 produces -1 or 0) even though we don't know exactly what registers it
12498 was produced from. */
12500 static int
12501 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12503 rtx x = *loc;
12504 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12505 int len = GET_RTX_LENGTH (GET_CODE (x));
12506 int i, j;
12508 if (REG_P (x))
12510 unsigned int regno = REGNO (x);
12511 unsigned int endregno = END_REGNO (x);
12512 unsigned int j;
12514 for (j = regno; j < endregno; j++)
12516 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12517 if (rsp->last_set_invalid
12518 /* If this is a pseudo-register that was only set once and not
12519 live at the beginning of the function, it is always valid. */
12520 || (! (regno >= FIRST_PSEUDO_REGISTER
12521 && REG_N_SETS (regno) == 1
12522 && (!REGNO_REG_SET_P
12523 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12524 && rsp->last_set_label > tick))
12526 if (replace)
12527 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12528 return replace;
12532 return 1;
12534 /* If this is a memory reference, make sure that there were no stores after
12535 it that might have clobbered the value. We don't have alias info, so we
12536 assume any store invalidates it. Moreover, we only have local UIDs, so
12537 we also assume that there were stores in the intervening basic blocks. */
12538 else if (MEM_P (x) && !MEM_READONLY_P (x)
12539 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12541 if (replace)
12542 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12543 return replace;
12546 for (i = 0; i < len; i++)
12548 if (fmt[i] == 'e')
12550 /* Check for identical subexpressions. If x contains
12551 identical subexpression we only have to traverse one of
12552 them. */
12553 if (i == 1 && ARITHMETIC_P (x))
12555 /* Note that at this point x0 has already been checked
12556 and found valid. */
12557 rtx x0 = XEXP (x, 0);
12558 rtx x1 = XEXP (x, 1);
12560 /* If x0 and x1 are identical then x is also valid. */
12561 if (x0 == x1)
12562 return 1;
12564 /* If x1 is identical to a subexpression of x0 then
12565 while checking x0, x1 has already been checked. Thus
12566 it is valid and so as x. */
12567 if (ARITHMETIC_P (x0)
12568 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12569 return 1;
12571 /* If x0 is identical to a subexpression of x1 then x is
12572 valid iff the rest of x1 is valid. */
12573 if (ARITHMETIC_P (x1)
12574 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12575 return
12576 get_last_value_validate (&XEXP (x1,
12577 x0 == XEXP (x1, 0) ? 1 : 0),
12578 insn, tick, replace);
12581 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12582 replace) == 0)
12583 return 0;
12585 else if (fmt[i] == 'E')
12586 for (j = 0; j < XVECLEN (x, i); j++)
12587 if (get_last_value_validate (&XVECEXP (x, i, j),
12588 insn, tick, replace) == 0)
12589 return 0;
12592 /* If we haven't found a reason for it to be invalid, it is valid. */
12593 return 1;
12596 /* Get the last value assigned to X, if known. Some registers
12597 in the value may be replaced with (clobber (const_int 0)) if their value
12598 is known longer known reliably. */
12600 static rtx
12601 get_last_value (const_rtx x)
12603 unsigned int regno;
12604 rtx value;
12605 reg_stat_type *rsp;
12607 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12608 then convert it to the desired mode. If this is a paradoxical SUBREG,
12609 we cannot predict what values the "extra" bits might have. */
12610 if (GET_CODE (x) == SUBREG
12611 && subreg_lowpart_p (x)
12612 && (GET_MODE_SIZE (GET_MODE (x))
12613 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12614 && (value = get_last_value (SUBREG_REG (x))) != 0)
12615 return gen_lowpart (GET_MODE (x), value);
12617 if (!REG_P (x))
12618 return 0;
12620 regno = REGNO (x);
12621 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12622 value = rsp->last_set_value;
12624 /* If we don't have a value, or if it isn't for this basic block and
12625 it's either a hard register, set more than once, or it's a live
12626 at the beginning of the function, return 0.
12628 Because if it's not live at the beginning of the function then the reg
12629 is always set before being used (is never used without being set).
12630 And, if it's set only once, and it's always set before use, then all
12631 uses must have the same last value, even if it's not from this basic
12632 block. */
12634 if (value == 0
12635 || (rsp->last_set_label < label_tick_ebb_start
12636 && (regno < FIRST_PSEUDO_REGISTER
12637 || REG_N_SETS (regno) != 1
12638 || REGNO_REG_SET_P
12639 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12640 return 0;
12642 /* If the value was set in a later insn than the ones we are processing,
12643 we can't use it even if the register was only set once. */
12644 if (rsp->last_set_label == label_tick
12645 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12646 return 0;
12648 /* If the value has all its registers valid, return it. */
12649 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12650 return value;
12652 /* Otherwise, make a copy and replace any invalid register with
12653 (clobber (const_int 0)). If that fails for some reason, return 0. */
12655 value = copy_rtx (value);
12656 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12657 return value;
12659 return 0;
12662 /* Return nonzero if expression X refers to a REG or to memory
12663 that is set in an instruction more recent than FROM_LUID. */
12665 static int
12666 use_crosses_set_p (const_rtx x, int from_luid)
12668 const char *fmt;
12669 int i;
12670 enum rtx_code code = GET_CODE (x);
12672 if (code == REG)
12674 unsigned int regno = REGNO (x);
12675 unsigned endreg = END_REGNO (x);
12677 #ifdef PUSH_ROUNDING
12678 /* Don't allow uses of the stack pointer to be moved,
12679 because we don't know whether the move crosses a push insn. */
12680 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12681 return 1;
12682 #endif
12683 for (; regno < endreg; regno++)
12685 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12686 if (rsp->last_set
12687 && rsp->last_set_label == label_tick
12688 && DF_INSN_LUID (rsp->last_set) > from_luid)
12689 return 1;
12691 return 0;
12694 if (code == MEM && mem_last_set > from_luid)
12695 return 1;
12697 fmt = GET_RTX_FORMAT (code);
12699 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12701 if (fmt[i] == 'E')
12703 int j;
12704 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12705 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12706 return 1;
12708 else if (fmt[i] == 'e'
12709 && use_crosses_set_p (XEXP (x, i), from_luid))
12710 return 1;
12712 return 0;
12715 /* Define three variables used for communication between the following
12716 routines. */
12718 static unsigned int reg_dead_regno, reg_dead_endregno;
12719 static int reg_dead_flag;
12721 /* Function called via note_stores from reg_dead_at_p.
12723 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12724 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12726 static void
12727 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12729 unsigned int regno, endregno;
12731 if (!REG_P (dest))
12732 return;
12734 regno = REGNO (dest);
12735 endregno = END_REGNO (dest);
12736 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12737 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12740 /* Return nonzero if REG is known to be dead at INSN.
12742 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12743 referencing REG, it is dead. If we hit a SET referencing REG, it is
12744 live. Otherwise, see if it is live or dead at the start of the basic
12745 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12746 must be assumed to be always live. */
12748 static int
12749 reg_dead_at_p (rtx reg, rtx insn)
12751 basic_block block;
12752 unsigned int i;
12754 /* Set variables for reg_dead_at_p_1. */
12755 reg_dead_regno = REGNO (reg);
12756 reg_dead_endregno = END_REGNO (reg);
12758 reg_dead_flag = 0;
12760 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12761 we allow the machine description to decide whether use-and-clobber
12762 patterns are OK. */
12763 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12765 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12766 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12767 return 0;
12770 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12771 beginning of basic block. */
12772 block = BLOCK_FOR_INSN (insn);
12773 for (;;)
12775 if (INSN_P (insn))
12777 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12778 if (reg_dead_flag)
12779 return reg_dead_flag == 1 ? 1 : 0;
12781 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12782 return 1;
12785 if (insn == BB_HEAD (block))
12786 break;
12788 insn = PREV_INSN (insn);
12791 /* Look at live-in sets for the basic block that we were in. */
12792 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12793 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12794 return 0;
12796 return 1;
12799 /* Note hard registers in X that are used. */
12801 static void
12802 mark_used_regs_combine (rtx x)
12804 RTX_CODE code = GET_CODE (x);
12805 unsigned int regno;
12806 int i;
12808 switch (code)
12810 case LABEL_REF:
12811 case SYMBOL_REF:
12812 case CONST_INT:
12813 case CONST:
12814 case CONST_DOUBLE:
12815 case CONST_VECTOR:
12816 case PC:
12817 case ADDR_VEC:
12818 case ADDR_DIFF_VEC:
12819 case ASM_INPUT:
12820 #ifdef HAVE_cc0
12821 /* CC0 must die in the insn after it is set, so we don't need to take
12822 special note of it here. */
12823 case CC0:
12824 #endif
12825 return;
12827 case CLOBBER:
12828 /* If we are clobbering a MEM, mark any hard registers inside the
12829 address as used. */
12830 if (MEM_P (XEXP (x, 0)))
12831 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12832 return;
12834 case REG:
12835 regno = REGNO (x);
12836 /* A hard reg in a wide mode may really be multiple registers.
12837 If so, mark all of them just like the first. */
12838 if (regno < FIRST_PSEUDO_REGISTER)
12840 /* None of this applies to the stack, frame or arg pointers. */
12841 if (regno == STACK_POINTER_REGNUM
12842 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12843 || regno == HARD_FRAME_POINTER_REGNUM
12844 #endif
12845 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12846 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12847 #endif
12848 || regno == FRAME_POINTER_REGNUM)
12849 return;
12851 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12853 return;
12855 case SET:
12857 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12858 the address. */
12859 rtx testreg = SET_DEST (x);
12861 while (GET_CODE (testreg) == SUBREG
12862 || GET_CODE (testreg) == ZERO_EXTRACT
12863 || GET_CODE (testreg) == STRICT_LOW_PART)
12864 testreg = XEXP (testreg, 0);
12866 if (MEM_P (testreg))
12867 mark_used_regs_combine (XEXP (testreg, 0));
12869 mark_used_regs_combine (SET_SRC (x));
12871 return;
12873 default:
12874 break;
12877 /* Recursively scan the operands of this expression. */
12880 const char *fmt = GET_RTX_FORMAT (code);
12882 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12884 if (fmt[i] == 'e')
12885 mark_used_regs_combine (XEXP (x, i));
12886 else if (fmt[i] == 'E')
12888 int j;
12890 for (j = 0; j < XVECLEN (x, i); j++)
12891 mark_used_regs_combine (XVECEXP (x, i, j));
12897 /* Remove register number REGNO from the dead registers list of INSN.
12899 Return the note used to record the death, if there was one. */
12902 remove_death (unsigned int regno, rtx insn)
12904 rtx note = find_regno_note (insn, REG_DEAD, regno);
12906 if (note)
12907 remove_note (insn, note);
12909 return note;
12912 /* For each register (hardware or pseudo) used within expression X, if its
12913 death is in an instruction with luid between FROM_LUID (inclusive) and
12914 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12915 list headed by PNOTES.
12917 That said, don't move registers killed by maybe_kill_insn.
12919 This is done when X is being merged by combination into TO_INSN. These
12920 notes will then be distributed as needed. */
12922 static void
12923 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12924 rtx *pnotes)
12926 const char *fmt;
12927 int len, i;
12928 enum rtx_code code = GET_CODE (x);
12930 if (code == REG)
12932 unsigned int regno = REGNO (x);
12933 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12935 /* Don't move the register if it gets killed in between from and to. */
12936 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12937 && ! reg_referenced_p (x, maybe_kill_insn))
12938 return;
12940 if (where_dead
12941 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12942 && DF_INSN_LUID (where_dead) >= from_luid
12943 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12945 rtx note = remove_death (regno, where_dead);
12947 /* It is possible for the call above to return 0. This can occur
12948 when last_death points to I2 or I1 that we combined with.
12949 In that case make a new note.
12951 We must also check for the case where X is a hard register
12952 and NOTE is a death note for a range of hard registers
12953 including X. In that case, we must put REG_DEAD notes for
12954 the remaining registers in place of NOTE. */
12956 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12957 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12958 > GET_MODE_SIZE (GET_MODE (x))))
12960 unsigned int deadregno = REGNO (XEXP (note, 0));
12961 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12962 unsigned int ourend = END_HARD_REGNO (x);
12963 unsigned int i;
12965 for (i = deadregno; i < deadend; i++)
12966 if (i < regno || i >= ourend)
12967 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12970 /* If we didn't find any note, or if we found a REG_DEAD note that
12971 covers only part of the given reg, and we have a multi-reg hard
12972 register, then to be safe we must check for REG_DEAD notes
12973 for each register other than the first. They could have
12974 their own REG_DEAD notes lying around. */
12975 else if ((note == 0
12976 || (note != 0
12977 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12978 < GET_MODE_SIZE (GET_MODE (x)))))
12979 && regno < FIRST_PSEUDO_REGISTER
12980 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12982 unsigned int ourend = END_HARD_REGNO (x);
12983 unsigned int i, offset;
12984 rtx oldnotes = 0;
12986 if (note)
12987 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12988 else
12989 offset = 1;
12991 for (i = regno + offset; i < ourend; i++)
12992 move_deaths (regno_reg_rtx[i],
12993 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12996 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12998 XEXP (note, 1) = *pnotes;
12999 *pnotes = note;
13001 else
13002 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13005 return;
13008 else if (GET_CODE (x) == SET)
13010 rtx dest = SET_DEST (x);
13012 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13014 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13015 that accesses one word of a multi-word item, some
13016 piece of everything register in the expression is used by
13017 this insn, so remove any old death. */
13018 /* ??? So why do we test for equality of the sizes? */
13020 if (GET_CODE (dest) == ZERO_EXTRACT
13021 || GET_CODE (dest) == STRICT_LOW_PART
13022 || (GET_CODE (dest) == SUBREG
13023 && (((GET_MODE_SIZE (GET_MODE (dest))
13024 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13025 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13026 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13028 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13029 return;
13032 /* If this is some other SUBREG, we know it replaces the entire
13033 value, so use that as the destination. */
13034 if (GET_CODE (dest) == SUBREG)
13035 dest = SUBREG_REG (dest);
13037 /* If this is a MEM, adjust deaths of anything used in the address.
13038 For a REG (the only other possibility), the entire value is
13039 being replaced so the old value is not used in this insn. */
13041 if (MEM_P (dest))
13042 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13043 to_insn, pnotes);
13044 return;
13047 else if (GET_CODE (x) == CLOBBER)
13048 return;
13050 len = GET_RTX_LENGTH (code);
13051 fmt = GET_RTX_FORMAT (code);
13053 for (i = 0; i < len; i++)
13055 if (fmt[i] == 'E')
13057 int j;
13058 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13059 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13060 to_insn, pnotes);
13062 else if (fmt[i] == 'e')
13063 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13067 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13068 pattern of an insn. X must be a REG. */
13070 static int
13071 reg_bitfield_target_p (rtx x, rtx body)
13073 int i;
13075 if (GET_CODE (body) == SET)
13077 rtx dest = SET_DEST (body);
13078 rtx target;
13079 unsigned int regno, tregno, endregno, endtregno;
13081 if (GET_CODE (dest) == ZERO_EXTRACT)
13082 target = XEXP (dest, 0);
13083 else if (GET_CODE (dest) == STRICT_LOW_PART)
13084 target = SUBREG_REG (XEXP (dest, 0));
13085 else
13086 return 0;
13088 if (GET_CODE (target) == SUBREG)
13089 target = SUBREG_REG (target);
13091 if (!REG_P (target))
13092 return 0;
13094 tregno = REGNO (target), regno = REGNO (x);
13095 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13096 return target == x;
13098 endtregno = end_hard_regno (GET_MODE (target), tregno);
13099 endregno = end_hard_regno (GET_MODE (x), regno);
13101 return endregno > tregno && regno < endtregno;
13104 else if (GET_CODE (body) == PARALLEL)
13105 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13106 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13107 return 1;
13109 return 0;
13112 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13113 as appropriate. I3 and I2 are the insns resulting from the combination
13114 insns including FROM (I2 may be zero).
13116 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13117 not need REG_DEAD notes because they are being substituted for. This
13118 saves searching in the most common cases.
13120 Each note in the list is either ignored or placed on some insns, depending
13121 on the type of note. */
13123 static void
13124 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13125 rtx elim_i1, rtx elim_i0)
13127 rtx note, next_note;
13128 rtx tem;
13130 for (note = notes; note; note = next_note)
13132 rtx place = 0, place2 = 0;
13134 next_note = XEXP (note, 1);
13135 switch (REG_NOTE_KIND (note))
13137 case REG_BR_PROB:
13138 case REG_BR_PRED:
13139 /* Doesn't matter much where we put this, as long as it's somewhere.
13140 It is preferable to keep these notes on branches, which is most
13141 likely to be i3. */
13142 place = i3;
13143 break;
13145 case REG_VALUE_PROFILE:
13146 /* Just get rid of this note, as it is unused later anyway. */
13147 break;
13149 case REG_NON_LOCAL_GOTO:
13150 if (JUMP_P (i3))
13151 place = i3;
13152 else
13154 gcc_assert (i2 && JUMP_P (i2));
13155 place = i2;
13157 break;
13159 case REG_EH_REGION:
13160 /* These notes must remain with the call or trapping instruction. */
13161 if (CALL_P (i3))
13162 place = i3;
13163 else if (i2 && CALL_P (i2))
13164 place = i2;
13165 else
13167 gcc_assert (cfun->can_throw_non_call_exceptions);
13168 if (may_trap_p (i3))
13169 place = i3;
13170 else if (i2 && may_trap_p (i2))
13171 place = i2;
13172 /* ??? Otherwise assume we've combined things such that we
13173 can now prove that the instructions can't trap. Drop the
13174 note in this case. */
13176 break;
13178 case REG_NORETURN:
13179 case REG_SETJMP:
13180 /* These notes must remain with the call. It should not be
13181 possible for both I2 and I3 to be a call. */
13182 if (CALL_P (i3))
13183 place = i3;
13184 else
13186 gcc_assert (i2 && CALL_P (i2));
13187 place = i2;
13189 break;
13191 case REG_UNUSED:
13192 /* Any clobbers for i3 may still exist, and so we must process
13193 REG_UNUSED notes from that insn.
13195 Any clobbers from i2 or i1 can only exist if they were added by
13196 recog_for_combine. In that case, recog_for_combine created the
13197 necessary REG_UNUSED notes. Trying to keep any original
13198 REG_UNUSED notes from these insns can cause incorrect output
13199 if it is for the same register as the original i3 dest.
13200 In that case, we will notice that the register is set in i3,
13201 and then add a REG_UNUSED note for the destination of i3, which
13202 is wrong. However, it is possible to have REG_UNUSED notes from
13203 i2 or i1 for register which were both used and clobbered, so
13204 we keep notes from i2 or i1 if they will turn into REG_DEAD
13205 notes. */
13207 /* If this register is set or clobbered in I3, put the note there
13208 unless there is one already. */
13209 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13211 if (from_insn != i3)
13212 break;
13214 if (! (REG_P (XEXP (note, 0))
13215 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13216 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13217 place = i3;
13219 /* Otherwise, if this register is used by I3, then this register
13220 now dies here, so we must put a REG_DEAD note here unless there
13221 is one already. */
13222 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13223 && ! (REG_P (XEXP (note, 0))
13224 ? find_regno_note (i3, REG_DEAD,
13225 REGNO (XEXP (note, 0)))
13226 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13228 PUT_REG_NOTE_KIND (note, REG_DEAD);
13229 place = i3;
13231 break;
13233 case REG_EQUAL:
13234 case REG_EQUIV:
13235 case REG_NOALIAS:
13236 /* These notes say something about results of an insn. We can
13237 only support them if they used to be on I3 in which case they
13238 remain on I3. Otherwise they are ignored.
13240 If the note refers to an expression that is not a constant, we
13241 must also ignore the note since we cannot tell whether the
13242 equivalence is still true. It might be possible to do
13243 slightly better than this (we only have a problem if I2DEST
13244 or I1DEST is present in the expression), but it doesn't
13245 seem worth the trouble. */
13247 if (from_insn == i3
13248 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13249 place = i3;
13250 break;
13252 case REG_INC:
13253 /* These notes say something about how a register is used. They must
13254 be present on any use of the register in I2 or I3. */
13255 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13256 place = i3;
13258 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13260 if (place)
13261 place2 = i2;
13262 else
13263 place = i2;
13265 break;
13267 case REG_LABEL_TARGET:
13268 case REG_LABEL_OPERAND:
13269 /* This can show up in several ways -- either directly in the
13270 pattern, or hidden off in the constant pool with (or without?)
13271 a REG_EQUAL note. */
13272 /* ??? Ignore the without-reg_equal-note problem for now. */
13273 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13274 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13275 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13276 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13277 place = i3;
13279 if (i2
13280 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13281 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13282 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13283 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13285 if (place)
13286 place2 = i2;
13287 else
13288 place = i2;
13291 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13292 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13293 there. */
13294 if (place && JUMP_P (place)
13295 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13296 && (JUMP_LABEL (place) == NULL
13297 || JUMP_LABEL (place) == XEXP (note, 0)))
13299 rtx label = JUMP_LABEL (place);
13301 if (!label)
13302 JUMP_LABEL (place) = XEXP (note, 0);
13303 else if (LABEL_P (label))
13304 LABEL_NUSES (label)--;
13307 if (place2 && JUMP_P (place2)
13308 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13309 && (JUMP_LABEL (place2) == NULL
13310 || JUMP_LABEL (place2) == XEXP (note, 0)))
13312 rtx label = JUMP_LABEL (place2);
13314 if (!label)
13315 JUMP_LABEL (place2) = XEXP (note, 0);
13316 else if (LABEL_P (label))
13317 LABEL_NUSES (label)--;
13318 place2 = 0;
13320 break;
13322 case REG_NONNEG:
13323 /* This note says something about the value of a register prior
13324 to the execution of an insn. It is too much trouble to see
13325 if the note is still correct in all situations. It is better
13326 to simply delete it. */
13327 break;
13329 case REG_DEAD:
13330 /* If we replaced the right hand side of FROM_INSN with a
13331 REG_EQUAL note, the original use of the dying register
13332 will not have been combined into I3 and I2. In such cases,
13333 FROM_INSN is guaranteed to be the first of the combined
13334 instructions, so we simply need to search back before
13335 FROM_INSN for the previous use or set of this register,
13336 then alter the notes there appropriately.
13338 If the register is used as an input in I3, it dies there.
13339 Similarly for I2, if it is nonzero and adjacent to I3.
13341 If the register is not used as an input in either I3 or I2
13342 and it is not one of the registers we were supposed to eliminate,
13343 there are two possibilities. We might have a non-adjacent I2
13344 or we might have somehow eliminated an additional register
13345 from a computation. For example, we might have had A & B where
13346 we discover that B will always be zero. In this case we will
13347 eliminate the reference to A.
13349 In both cases, we must search to see if we can find a previous
13350 use of A and put the death note there. */
13352 if (from_insn
13353 && from_insn == i2mod
13354 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13355 tem = from_insn;
13356 else
13358 if (from_insn
13359 && CALL_P (from_insn)
13360 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13361 place = from_insn;
13362 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13363 place = i3;
13364 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13365 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13366 place = i2;
13367 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13368 && !(i2mod
13369 && reg_overlap_mentioned_p (XEXP (note, 0),
13370 i2mod_old_rhs)))
13371 || rtx_equal_p (XEXP (note, 0), elim_i1)
13372 || rtx_equal_p (XEXP (note, 0), elim_i0))
13373 break;
13374 tem = i3;
13377 if (place == 0)
13379 basic_block bb = this_basic_block;
13381 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13383 if (!NONDEBUG_INSN_P (tem))
13385 if (tem == BB_HEAD (bb))
13386 break;
13387 continue;
13390 /* If the register is being set at TEM, see if that is all
13391 TEM is doing. If so, delete TEM. Otherwise, make this
13392 into a REG_UNUSED note instead. Don't delete sets to
13393 global register vars. */
13394 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13395 || !global_regs[REGNO (XEXP (note, 0))])
13396 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13398 rtx set = single_set (tem);
13399 rtx inner_dest = 0;
13400 #ifdef HAVE_cc0
13401 rtx cc0_setter = NULL_RTX;
13402 #endif
13404 if (set != 0)
13405 for (inner_dest = SET_DEST (set);
13406 (GET_CODE (inner_dest) == STRICT_LOW_PART
13407 || GET_CODE (inner_dest) == SUBREG
13408 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13409 inner_dest = XEXP (inner_dest, 0))
13412 /* Verify that it was the set, and not a clobber that
13413 modified the register.
13415 CC0 targets must be careful to maintain setter/user
13416 pairs. If we cannot delete the setter due to side
13417 effects, mark the user with an UNUSED note instead
13418 of deleting it. */
13420 if (set != 0 && ! side_effects_p (SET_SRC (set))
13421 && rtx_equal_p (XEXP (note, 0), inner_dest)
13422 #ifdef HAVE_cc0
13423 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13424 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13425 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13426 #endif
13429 /* Move the notes and links of TEM elsewhere.
13430 This might delete other dead insns recursively.
13431 First set the pattern to something that won't use
13432 any register. */
13433 rtx old_notes = REG_NOTES (tem);
13435 PATTERN (tem) = pc_rtx;
13436 REG_NOTES (tem) = NULL;
13438 distribute_notes (old_notes, tem, tem, NULL_RTX,
13439 NULL_RTX, NULL_RTX, NULL_RTX);
13440 distribute_links (LOG_LINKS (tem));
13442 SET_INSN_DELETED (tem);
13443 if (tem == i2)
13444 i2 = NULL_RTX;
13446 #ifdef HAVE_cc0
13447 /* Delete the setter too. */
13448 if (cc0_setter)
13450 PATTERN (cc0_setter) = pc_rtx;
13451 old_notes = REG_NOTES (cc0_setter);
13452 REG_NOTES (cc0_setter) = NULL;
13454 distribute_notes (old_notes, cc0_setter,
13455 cc0_setter, NULL_RTX,
13456 NULL_RTX, NULL_RTX, NULL_RTX);
13457 distribute_links (LOG_LINKS (cc0_setter));
13459 SET_INSN_DELETED (cc0_setter);
13460 if (cc0_setter == i2)
13461 i2 = NULL_RTX;
13463 #endif
13465 else
13467 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13469 /* If there isn't already a REG_UNUSED note, put one
13470 here. Do not place a REG_DEAD note, even if
13471 the register is also used here; that would not
13472 match the algorithm used in lifetime analysis
13473 and can cause the consistency check in the
13474 scheduler to fail. */
13475 if (! find_regno_note (tem, REG_UNUSED,
13476 REGNO (XEXP (note, 0))))
13477 place = tem;
13478 break;
13481 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13482 || (CALL_P (tem)
13483 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13485 place = tem;
13487 /* If we are doing a 3->2 combination, and we have a
13488 register which formerly died in i3 and was not used
13489 by i2, which now no longer dies in i3 and is used in
13490 i2 but does not die in i2, and place is between i2
13491 and i3, then we may need to move a link from place to
13492 i2. */
13493 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13494 && from_insn
13495 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13496 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13498 rtx links = LOG_LINKS (place);
13499 LOG_LINKS (place) = 0;
13500 distribute_links (links);
13502 break;
13505 if (tem == BB_HEAD (bb))
13506 break;
13511 /* If the register is set or already dead at PLACE, we needn't do
13512 anything with this note if it is still a REG_DEAD note.
13513 We check here if it is set at all, not if is it totally replaced,
13514 which is what `dead_or_set_p' checks, so also check for it being
13515 set partially. */
13517 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13519 unsigned int regno = REGNO (XEXP (note, 0));
13520 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13522 if (dead_or_set_p (place, XEXP (note, 0))
13523 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13525 /* Unless the register previously died in PLACE, clear
13526 last_death. [I no longer understand why this is
13527 being done.] */
13528 if (rsp->last_death != place)
13529 rsp->last_death = 0;
13530 place = 0;
13532 else
13533 rsp->last_death = place;
13535 /* If this is a death note for a hard reg that is occupying
13536 multiple registers, ensure that we are still using all
13537 parts of the object. If we find a piece of the object
13538 that is unused, we must arrange for an appropriate REG_DEAD
13539 note to be added for it. However, we can't just emit a USE
13540 and tag the note to it, since the register might actually
13541 be dead; so we recourse, and the recursive call then finds
13542 the previous insn that used this register. */
13544 if (place && regno < FIRST_PSEUDO_REGISTER
13545 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13547 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13548 int all_used = 1;
13549 unsigned int i;
13551 for (i = regno; i < endregno; i++)
13552 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13553 && ! find_regno_fusage (place, USE, i))
13554 || dead_or_set_regno_p (place, i))
13555 all_used = 0;
13557 if (! all_used)
13559 /* Put only REG_DEAD notes for pieces that are
13560 not already dead or set. */
13562 for (i = regno; i < endregno;
13563 i += hard_regno_nregs[i][reg_raw_mode[i]])
13565 rtx piece = regno_reg_rtx[i];
13566 basic_block bb = this_basic_block;
13568 if (! dead_or_set_p (place, piece)
13569 && ! reg_bitfield_target_p (piece,
13570 PATTERN (place)))
13572 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13573 NULL_RTX);
13575 distribute_notes (new_note, place, place,
13576 NULL_RTX, NULL_RTX, NULL_RTX,
13577 NULL_RTX);
13579 else if (! refers_to_regno_p (i, i + 1,
13580 PATTERN (place), 0)
13581 && ! find_regno_fusage (place, USE, i))
13582 for (tem = PREV_INSN (place); ;
13583 tem = PREV_INSN (tem))
13585 if (!NONDEBUG_INSN_P (tem))
13587 if (tem == BB_HEAD (bb))
13588 break;
13589 continue;
13591 if (dead_or_set_p (tem, piece)
13592 || reg_bitfield_target_p (piece,
13593 PATTERN (tem)))
13595 add_reg_note (tem, REG_UNUSED, piece);
13596 break;
13602 place = 0;
13606 break;
13608 default:
13609 /* Any other notes should not be present at this point in the
13610 compilation. */
13611 gcc_unreachable ();
13614 if (place)
13616 XEXP (note, 1) = REG_NOTES (place);
13617 REG_NOTES (place) = note;
13620 if (place2)
13621 add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13625 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13626 I3, I2, and I1 to new locations. This is also called to add a link
13627 pointing at I3 when I3's destination is changed. */
13629 static void
13630 distribute_links (rtx links)
13632 rtx link, next_link;
13634 for (link = links; link; link = next_link)
13636 rtx place = 0;
13637 rtx insn;
13638 rtx set, reg;
13640 next_link = XEXP (link, 1);
13642 /* If the insn that this link points to is a NOTE or isn't a single
13643 set, ignore it. In the latter case, it isn't clear what we
13644 can do other than ignore the link, since we can't tell which
13645 register it was for. Such links wouldn't be used by combine
13646 anyway.
13648 It is not possible for the destination of the target of the link to
13649 have been changed by combine. The only potential of this is if we
13650 replace I3, I2, and I1 by I3 and I2. But in that case the
13651 destination of I2 also remains unchanged. */
13653 if (NOTE_P (XEXP (link, 0))
13654 || (set = single_set (XEXP (link, 0))) == 0)
13655 continue;
13657 reg = SET_DEST (set);
13658 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13659 || GET_CODE (reg) == STRICT_LOW_PART)
13660 reg = XEXP (reg, 0);
13662 /* A LOG_LINK is defined as being placed on the first insn that uses
13663 a register and points to the insn that sets the register. Start
13664 searching at the next insn after the target of the link and stop
13665 when we reach a set of the register or the end of the basic block.
13667 Note that this correctly handles the link that used to point from
13668 I3 to I2. Also note that not much searching is typically done here
13669 since most links don't point very far away. */
13671 for (insn = NEXT_INSN (XEXP (link, 0));
13672 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13673 || BB_HEAD (this_basic_block->next_bb) != insn));
13674 insn = NEXT_INSN (insn))
13675 if (DEBUG_INSN_P (insn))
13676 continue;
13677 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13679 if (reg_referenced_p (reg, PATTERN (insn)))
13680 place = insn;
13681 break;
13683 else if (CALL_P (insn)
13684 && find_reg_fusage (insn, USE, reg))
13686 place = insn;
13687 break;
13689 else if (INSN_P (insn) && reg_set_p (reg, insn))
13690 break;
13692 /* If we found a place to put the link, place it there unless there
13693 is already a link to the same insn as LINK at that point. */
13695 if (place)
13697 rtx link2;
13699 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13700 if (XEXP (link2, 0) == XEXP (link, 0))
13701 break;
13703 if (link2 == 0)
13705 XEXP (link, 1) = LOG_LINKS (place);
13706 LOG_LINKS (place) = link;
13708 /* Set added_links_insn to the earliest insn we added a
13709 link to. */
13710 if (added_links_insn == 0
13711 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13712 added_links_insn = place;
13718 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13719 Check whether the expression pointer to by LOC is a register or
13720 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13721 Otherwise return zero. */
13723 static int
13724 unmentioned_reg_p_1 (rtx *loc, void *expr)
13726 rtx x = *loc;
13728 if (x != NULL_RTX
13729 && (REG_P (x) || MEM_P (x))
13730 && ! reg_mentioned_p (x, (rtx) expr))
13731 return 1;
13732 return 0;
13735 /* Check for any register or memory mentioned in EQUIV that is not
13736 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13737 of EXPR where some registers may have been replaced by constants. */
13739 static bool
13740 unmentioned_reg_p (rtx equiv, rtx expr)
13742 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13745 void
13746 dump_combine_stats (FILE *file)
13748 fprintf
13749 (file,
13750 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13751 combine_attempts, combine_merges, combine_extras, combine_successes);
13754 void
13755 dump_combine_total_stats (FILE *file)
13757 fprintf
13758 (file,
13759 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13760 total_attempts, total_merges, total_extras, total_successes);
13763 static bool
13764 gate_handle_combine (void)
13766 return (optimize > 0);
13769 /* Try combining insns through substitution. */
13770 static unsigned int
13771 rest_of_handle_combine (void)
13773 int rebuild_jump_labels_after_combine;
13775 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13776 df_note_add_problem ();
13777 df_analyze ();
13779 regstat_init_n_sets_and_refs ();
13781 rebuild_jump_labels_after_combine
13782 = combine_instructions (get_insns (), max_reg_num ());
13784 /* Combining insns may have turned an indirect jump into a
13785 direct jump. Rebuild the JUMP_LABEL fields of jumping
13786 instructions. */
13787 if (rebuild_jump_labels_after_combine)
13789 timevar_push (TV_JUMP);
13790 rebuild_jump_labels (get_insns ());
13791 cleanup_cfg (0);
13792 timevar_pop (TV_JUMP);
13795 regstat_free_n_sets_and_refs ();
13796 return 0;
13799 struct rtl_opt_pass pass_combine =
13802 RTL_PASS,
13803 "combine", /* name */
13804 gate_handle_combine, /* gate */
13805 rest_of_handle_combine, /* execute */
13806 NULL, /* sub */
13807 NULL, /* next */
13808 0, /* static_pass_number */
13809 TV_COMBINE, /* tv_id */
13810 PROP_cfglayout, /* properties_required */
13811 0, /* properties_provided */
13812 0, /* properties_destroyed */
13813 0, /* todo_flags_start */
13814 TODO_dump_func |
13815 TODO_df_finish | TODO_verify_rtl_sharing |
13816 TODO_ggc_collect, /* todo_flags_finish */