* check-init.c, decl.c, expr.c, gcj.texi, java-tree.h,
[official-gcc.git] / gcc / combine.c
blob57f490da76b94b68b031bd18c48a2304c99cb3ef
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
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 created by
53 flow.c aren't completely updated:
55 - reg_live_length is not updated
56 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
57 removed because there is no way to know which register it was
58 linking
60 To simplify substitution, we combine only when the earlier insn(s)
61 consist of only a single assignment. To simplify updating afterward,
62 we never combine when a subroutine call appears in the middle.
64 Since we do not represent assignments to CC0 explicitly except when that
65 is all an insn does, there is no LOG_LINKS entry in an insn that uses
66 the condition code for the insn that set the condition code.
67 Fortunately, these two insns must be consecutive.
68 Therefore, every JUMP_INSN is taken to have an implicit logical link
69 to the preceding insn. This is not quite right, since non-jumps can
70 also use the condition code; but in practice such insns would not
71 combine anyway. */
73 #include "config.h"
74 #include "system.h"
75 #include "coretypes.h"
76 #include "tm.h"
77 #include "rtl.h"
78 #include "tree.h"
79 #include "tm_p.h"
80 #include "flags.h"
81 #include "regs.h"
82 #include "hard-reg-set.h"
83 #include "basic-block.h"
84 #include "insn-config.h"
85 #include "function.h"
86 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
87 #include "expr.h"
88 #include "insn-attr.h"
89 #include "recog.h"
90 #include "real.h"
91 #include "toplev.h"
92 #include "target.h"
93 #include "optabs.h"
94 #include "insn-codes.h"
95 #include "rtlhooks-def.h"
96 /* Include output.h for dump_file. */
97 #include "output.h"
98 #include "params.h"
99 #include "timevar.h"
100 #include "tree-pass.h"
102 /* Number of attempts to combine instructions in this function. */
104 static int combine_attempts;
106 /* Number of attempts that got as far as substitution in this function. */
108 static int combine_merges;
110 /* Number of instructions combined with added SETs in this function. */
112 static int combine_extras;
114 /* Number of instructions combined in this function. */
116 static int combine_successes;
118 /* Totals over entire compilation. */
120 static int total_attempts, total_merges, total_extras, total_successes;
123 /* Vector mapping INSN_UIDs to cuids.
124 The cuids are like uids but increase monotonically always.
125 Combine always uses cuids so that it can compare them.
126 But actually renumbering the uids, which we used to do,
127 proves to be a bad idea because it makes it hard to compare
128 the dumps produced by earlier passes with those from later passes. */
130 static int *uid_cuid;
131 static int max_uid_cuid;
133 /* Get the cuid of an insn. */
135 #define INSN_CUID(INSN) \
136 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
138 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
139 BITS_PER_WORD would invoke undefined behavior. Work around it. */
141 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
142 (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
144 /* Maximum register number, which is the size of the tables below. */
146 static unsigned int combine_max_regno;
148 struct reg_stat {
149 /* Record last point of death of (hard or pseudo) register n. */
150 rtx last_death;
152 /* Record last point of modification of (hard or pseudo) register n. */
153 rtx last_set;
155 /* The next group of fields allows the recording of the last value assigned
156 to (hard or pseudo) register n. We use this information to see if an
157 operation being processed is redundant given a prior operation performed
158 on the register. For example, an `and' with a constant is redundant if
159 all the zero bits are already known to be turned off.
161 We use an approach similar to that used by cse, but change it in the
162 following ways:
164 (1) We do not want to reinitialize at each label.
165 (2) It is useful, but not critical, to know the actual value assigned
166 to a register. Often just its form is helpful.
168 Therefore, we maintain the following fields:
170 last_set_value the last value assigned
171 last_set_label records the value of label_tick when the
172 register was assigned
173 last_set_table_tick records the value of label_tick when a
174 value using the register is assigned
175 last_set_invalid set to nonzero when it is not valid
176 to use the value of this register in some
177 register's value
179 To understand the usage of these tables, it is important to understand
180 the distinction between the value in last_set_value being valid and
181 the register being validly contained in some other expression in the
182 table.
184 (The next two parameters are out of date).
186 reg_stat[i].last_set_value is valid if it is nonzero, and either
187 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
189 Register I may validly appear in any expression returned for the value
190 of another register if reg_n_sets[i] is 1. It may also appear in the
191 value for register J if reg_stat[j].last_set_invalid is zero, or
192 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
194 If an expression is found in the table containing a register which may
195 not validly appear in an expression, the register is replaced by
196 something that won't match, (clobber (const_int 0)). */
198 /* Record last value assigned to (hard or pseudo) register n. */
200 rtx last_set_value;
202 /* Record the value of label_tick when an expression involving register n
203 is placed in last_set_value. */
205 int last_set_table_tick;
207 /* Record the value of label_tick when the value for register n is placed in
208 last_set_value. */
210 int last_set_label;
212 /* These fields are maintained in parallel with last_set_value and are
213 used to store the mode in which the register was last set, the bits
214 that were known to be zero when it was last set, and the number of
215 sign bits copies it was known to have when it was last set. */
217 unsigned HOST_WIDE_INT last_set_nonzero_bits;
218 char last_set_sign_bit_copies;
219 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
221 /* Set nonzero if references to register n in expressions should not be
222 used. last_set_invalid is set nonzero when this register is being
223 assigned to and last_set_table_tick == label_tick. */
225 char last_set_invalid;
227 /* Some registers that are set more than once and used in more than one
228 basic block are nevertheless always set in similar ways. For example,
229 a QImode register may be loaded from memory in two places on a machine
230 where byte loads zero extend.
232 We record in the following fields if a register has some leading bits
233 that are always equal to the sign bit, and what we know about the
234 nonzero bits of a register, specifically which bits are known to be
235 zero.
237 If an entry is zero, it means that we don't know anything special. */
239 unsigned char sign_bit_copies;
241 unsigned HOST_WIDE_INT nonzero_bits;
244 static struct reg_stat *reg_stat;
246 /* Record the cuid of the last insn that invalidated memory
247 (anything that writes memory, and subroutine calls, but not pushes). */
249 static int mem_last_set;
251 /* Record the cuid of the last CALL_INSN
252 so we can tell whether a potential combination crosses any calls. */
254 static int last_call_cuid;
256 /* When `subst' is called, this is the insn that is being modified
257 (by combining in a previous insn). The PATTERN of this insn
258 is still the old pattern partially modified and it should not be
259 looked at, but this may be used to examine the successors of the insn
260 to judge whether a simplification is valid. */
262 static rtx subst_insn;
264 /* This is the lowest CUID that `subst' is currently dealing with.
265 get_last_value will not return a value if the register was set at or
266 after this CUID. If not for this mechanism, we could get confused if
267 I2 or I1 in try_combine were an insn that used the old value of a register
268 to obtain a new value. In that case, we might erroneously get the
269 new value of the register when we wanted the old one. */
271 static int subst_low_cuid;
273 /* This contains any hard registers that are used in newpat; reg_dead_at_p
274 must consider all these registers to be always live. */
276 static HARD_REG_SET newpat_used_regs;
278 /* This is an insn to which a LOG_LINKS entry has been added. If this
279 insn is the earlier than I2 or I3, combine should rescan starting at
280 that location. */
282 static rtx added_links_insn;
284 /* Basic block in which we are performing combines. */
285 static basic_block this_basic_block;
287 /* A bitmap indicating which blocks had registers go dead at entry.
288 After combine, we'll need to re-do global life analysis with
289 those blocks as starting points. */
290 static sbitmap refresh_blocks;
292 /* The following array records the insn_rtx_cost for every insn
293 in the instruction stream. */
295 static int *uid_insn_cost;
297 /* Length of the currently allocated uid_insn_cost array. */
299 static int last_insn_cost;
301 /* Incremented for each label. */
303 static int label_tick;
305 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
306 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
308 static enum machine_mode nonzero_bits_mode;
310 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
311 be safely used. It is zero while computing them and after combine has
312 completed. This former test prevents propagating values based on
313 previously set values, which can be incorrect if a variable is modified
314 in a loop. */
316 static int nonzero_sign_valid;
319 /* Record one modification to rtl structure
320 to be undone by storing old_contents into *where.
321 is_int is 1 if the contents are an int. */
323 struct undo
325 struct undo *next;
326 int is_int;
327 union {rtx r; int i;} old_contents;
328 union {rtx *r; int *i;} where;
331 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
332 num_undo says how many are currently recorded.
334 other_insn is nonzero if we have modified some other insn in the process
335 of working on subst_insn. It must be verified too. */
337 struct undobuf
339 struct undo *undos;
340 struct undo *frees;
341 rtx other_insn;
344 static struct undobuf undobuf;
346 /* Number of times the pseudo being substituted for
347 was found and replaced. */
349 static int n_occurrences;
351 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
352 enum machine_mode,
353 unsigned HOST_WIDE_INT,
354 unsigned HOST_WIDE_INT *);
355 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
356 enum machine_mode,
357 unsigned int, unsigned int *);
358 static void do_SUBST (rtx *, rtx);
359 static void do_SUBST_INT (int *, int);
360 static void init_reg_last (void);
361 static void setup_incoming_promotions (void);
362 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
363 static int cant_combine_insn_p (rtx);
364 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
365 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
366 static int contains_muldiv (rtx);
367 static rtx try_combine (rtx, rtx, rtx, int *);
368 static void undo_all (void);
369 static void undo_commit (void);
370 static rtx *find_split_point (rtx *, rtx);
371 static rtx subst (rtx, rtx, rtx, int, int);
372 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
373 static rtx simplify_if_then_else (rtx);
374 static rtx simplify_set (rtx);
375 static rtx simplify_logical (rtx);
376 static rtx expand_compound_operation (rtx);
377 static rtx expand_field_assignment (rtx);
378 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
379 rtx, unsigned HOST_WIDE_INT, int, int, int);
380 static rtx extract_left_shift (rtx, int);
381 static rtx make_compound_operation (rtx, enum rtx_code);
382 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
383 unsigned HOST_WIDE_INT *);
384 static rtx force_to_mode (rtx, enum machine_mode,
385 unsigned HOST_WIDE_INT, rtx, int);
386 static rtx if_then_else_cond (rtx, rtx *, rtx *);
387 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
388 static int rtx_equal_for_field_assignment_p (rtx, rtx);
389 static rtx make_field_assignment (rtx);
390 static rtx apply_distributive_law (rtx);
391 static rtx distribute_and_simplify_rtx (rtx, int);
392 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
393 unsigned HOST_WIDE_INT);
394 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
395 HOST_WIDE_INT, enum machine_mode, int *);
396 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
397 int);
398 static int recog_for_combine (rtx *, rtx, rtx *);
399 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
400 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
401 static void update_table_tick (rtx);
402 static void record_value_for_reg (rtx, rtx, rtx);
403 static void check_promoted_subreg (rtx, rtx);
404 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
405 static void record_dead_and_set_regs (rtx);
406 static int get_last_value_validate (rtx *, rtx, int, int);
407 static rtx get_last_value (rtx);
408 static int use_crosses_set_p (rtx, int);
409 static void reg_dead_at_p_1 (rtx, rtx, void *);
410 static int reg_dead_at_p (rtx, rtx);
411 static void move_deaths (rtx, rtx, int, rtx, rtx *);
412 static int reg_bitfield_target_p (rtx, rtx);
413 static void distribute_notes (rtx, rtx, rtx, rtx);
414 static void distribute_links (rtx);
415 static void mark_used_regs_combine (rtx);
416 static int insn_cuid (rtx);
417 static void record_promoted_value (rtx, rtx);
418 static int unmentioned_reg_p_1 (rtx *, void *);
419 static bool unmentioned_reg_p (rtx, rtx);
422 /* It is not safe to use ordinary gen_lowpart in combine.
423 See comments in gen_lowpart_for_combine. */
424 #undef RTL_HOOKS_GEN_LOWPART
425 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
427 /* Our implementation of gen_lowpart never emits a new pseudo. */
428 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
429 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
431 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
432 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
434 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
435 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
437 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
440 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
441 insn. The substitution can be undone by undo_all. If INTO is already
442 set to NEWVAL, do not record this change. Because computing NEWVAL might
443 also call SUBST, we have to compute it before we put anything into
444 the undo table. */
446 static void
447 do_SUBST (rtx *into, rtx newval)
449 struct undo *buf;
450 rtx oldval = *into;
452 if (oldval == newval)
453 return;
455 /* We'd like to catch as many invalid transformations here as
456 possible. Unfortunately, there are way too many mode changes
457 that are perfectly valid, so we'd waste too much effort for
458 little gain doing the checks here. Focus on catching invalid
459 transformations involving integer constants. */
460 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
461 && GET_CODE (newval) == CONST_INT)
463 /* Sanity check that we're replacing oldval with a CONST_INT
464 that is a valid sign-extension for the original mode. */
465 gcc_assert (INTVAL (newval)
466 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
468 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
469 CONST_INT is not valid, because after the replacement, the
470 original mode would be gone. Unfortunately, we can't tell
471 when do_SUBST is called to replace the operand thereof, so we
472 perform this test on oldval instead, checking whether an
473 invalid replacement took place before we got here. */
474 gcc_assert (!(GET_CODE (oldval) == SUBREG
475 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
476 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
477 && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
480 if (undobuf.frees)
481 buf = undobuf.frees, undobuf.frees = buf->next;
482 else
483 buf = xmalloc (sizeof (struct undo));
485 buf->is_int = 0;
486 buf->where.r = into;
487 buf->old_contents.r = oldval;
488 *into = newval;
490 buf->next = undobuf.undos, undobuf.undos = buf;
493 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
495 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
496 for the value of a HOST_WIDE_INT value (including CONST_INT) is
497 not safe. */
499 static void
500 do_SUBST_INT (int *into, int newval)
502 struct undo *buf;
503 int oldval = *into;
505 if (oldval == newval)
506 return;
508 if (undobuf.frees)
509 buf = undobuf.frees, undobuf.frees = buf->next;
510 else
511 buf = xmalloc (sizeof (struct undo));
513 buf->is_int = 1;
514 buf->where.i = into;
515 buf->old_contents.i = oldval;
516 *into = newval;
518 buf->next = undobuf.undos, undobuf.undos = buf;
521 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
523 /* Subroutine of try_combine. Determine whether the combine replacement
524 patterns NEWPAT and NEWI2PAT are cheaper according to insn_rtx_cost
525 that the original instruction sequence I1, I2 and I3. Note that I1
526 and/or NEWI2PAT may be NULL_RTX. This function returns false, if the
527 costs of all instructions can be estimated, and the replacements are
528 more expensive than the original sequence. */
530 static bool
531 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat)
533 int i1_cost, i2_cost, i3_cost;
534 int new_i2_cost, new_i3_cost;
535 int old_cost, new_cost;
537 /* Lookup the original insn_rtx_costs. */
538 i2_cost = INSN_UID (i2) <= last_insn_cost
539 ? uid_insn_cost[INSN_UID (i2)] : 0;
540 i3_cost = INSN_UID (i3) <= last_insn_cost
541 ? uid_insn_cost[INSN_UID (i3)] : 0;
543 if (i1)
545 i1_cost = INSN_UID (i1) <= last_insn_cost
546 ? uid_insn_cost[INSN_UID (i1)] : 0;
547 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
548 ? i1_cost + i2_cost + i3_cost : 0;
550 else
552 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
553 i1_cost = 0;
556 /* Calculate the replacement insn_rtx_costs. */
557 new_i3_cost = insn_rtx_cost (newpat);
558 if (newi2pat)
560 new_i2_cost = insn_rtx_cost (newi2pat);
561 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
562 ? new_i2_cost + new_i3_cost : 0;
564 else
566 new_cost = new_i3_cost;
567 new_i2_cost = 0;
570 if (undobuf.other_insn)
572 int old_other_cost, new_other_cost;
574 old_other_cost = (INSN_UID (undobuf.other_insn) <= last_insn_cost
575 ? uid_insn_cost[INSN_UID (undobuf.other_insn)] : 0);
576 new_other_cost = insn_rtx_cost (PATTERN (undobuf.other_insn));
577 if (old_other_cost > 0 && new_other_cost > 0)
579 old_cost += old_other_cost;
580 new_cost += new_other_cost;
582 else
583 old_cost = 0;
586 /* Disallow this recombination if both new_cost and old_cost are
587 greater than zero, and new_cost is greater than old cost. */
588 if (old_cost > 0
589 && new_cost > old_cost)
591 if (dump_file)
593 if (i1)
595 fprintf (dump_file,
596 "rejecting combination of insns %d, %d and %d\n",
597 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
598 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
599 i1_cost, i2_cost, i3_cost, old_cost);
601 else
603 fprintf (dump_file,
604 "rejecting combination of insns %d and %d\n",
605 INSN_UID (i2), INSN_UID (i3));
606 fprintf (dump_file, "original costs %d + %d = %d\n",
607 i2_cost, i3_cost, old_cost);
610 if (newi2pat)
612 fprintf (dump_file, "replacement costs %d + %d = %d\n",
613 new_i2_cost, new_i3_cost, new_cost);
615 else
616 fprintf (dump_file, "replacement cost %d\n", new_cost);
619 return false;
622 /* Update the uid_insn_cost array with the replacement costs. */
623 uid_insn_cost[INSN_UID (i2)] = new_i2_cost;
624 uid_insn_cost[INSN_UID (i3)] = new_i3_cost;
625 if (i1)
626 uid_insn_cost[INSN_UID (i1)] = 0;
628 return true;
631 /* Main entry point for combiner. F is the first insn of the function.
632 NREGS is the first unused pseudo-reg number.
634 Return nonzero if the combiner has turned an indirect jump
635 instruction into a direct jump. */
637 combine_instructions (rtx f, unsigned int nregs)
639 rtx insn, next;
640 #ifdef HAVE_cc0
641 rtx prev;
642 #endif
643 int i;
644 unsigned int j = 0;
645 rtx links, nextlinks;
646 sbitmap_iterator sbi;
648 int new_direct_jump_p = 0;
650 combine_attempts = 0;
651 combine_merges = 0;
652 combine_extras = 0;
653 combine_successes = 0;
655 combine_max_regno = nregs;
657 rtl_hooks = combine_rtl_hooks;
659 reg_stat = xcalloc (nregs, sizeof (struct reg_stat));
661 init_recog_no_volatile ();
663 /* Compute maximum uid value so uid_cuid can be allocated. */
665 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
666 if (INSN_UID (insn) > i)
667 i = INSN_UID (insn);
669 uid_cuid = xmalloc ((i + 1) * sizeof (int));
670 max_uid_cuid = i;
672 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
674 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
675 problems when, for example, we have j <<= 1 in a loop. */
677 nonzero_sign_valid = 0;
679 /* Compute the mapping from uids to cuids.
680 Cuids are numbers assigned to insns, like uids,
681 except that cuids increase monotonically through the code.
683 Scan all SETs and see if we can deduce anything about what
684 bits are known to be zero for some registers and how many copies
685 of the sign bit are known to exist for those registers.
687 Also set any known values so that we can use it while searching
688 for what bits are known to be set. */
690 label_tick = 1;
692 setup_incoming_promotions ();
694 refresh_blocks = sbitmap_alloc (last_basic_block);
695 sbitmap_zero (refresh_blocks);
697 /* Allocate array of current insn_rtx_costs. */
698 uid_insn_cost = xcalloc (max_uid_cuid + 1, sizeof (int));
699 last_insn_cost = max_uid_cuid;
701 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
703 uid_cuid[INSN_UID (insn)] = ++i;
704 subst_low_cuid = i;
705 subst_insn = insn;
707 if (INSN_P (insn))
709 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
710 NULL);
711 record_dead_and_set_regs (insn);
713 #ifdef AUTO_INC_DEC
714 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
715 if (REG_NOTE_KIND (links) == REG_INC)
716 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
717 NULL);
718 #endif
720 /* Record the current insn_rtx_cost of this instruction. */
721 if (NONJUMP_INSN_P (insn))
722 uid_insn_cost[INSN_UID (insn)] = insn_rtx_cost (PATTERN (insn));
723 if (dump_file)
724 fprintf(dump_file, "insn_cost %d: %d\n",
725 INSN_UID (insn), uid_insn_cost[INSN_UID (insn)]);
728 if (LABEL_P (insn))
729 label_tick++;
732 nonzero_sign_valid = 1;
734 /* Now scan all the insns in forward order. */
736 label_tick = 1;
737 last_call_cuid = 0;
738 mem_last_set = 0;
739 init_reg_last ();
740 setup_incoming_promotions ();
742 FOR_EACH_BB (this_basic_block)
744 for (insn = BB_HEAD (this_basic_block);
745 insn != NEXT_INSN (BB_END (this_basic_block));
746 insn = next ? next : NEXT_INSN (insn))
748 next = 0;
750 if (LABEL_P (insn))
751 label_tick++;
753 else if (INSN_P (insn))
755 /* See if we know about function return values before this
756 insn based upon SUBREG flags. */
757 check_promoted_subreg (insn, PATTERN (insn));
759 /* Try this insn with each insn it links back to. */
761 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
762 if ((next = try_combine (insn, XEXP (links, 0),
763 NULL_RTX, &new_direct_jump_p)) != 0)
764 goto retry;
766 /* Try each sequence of three linked insns ending with this one. */
768 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
770 rtx link = XEXP (links, 0);
772 /* If the linked insn has been replaced by a note, then there
773 is no point in pursuing this chain any further. */
774 if (NOTE_P (link))
775 continue;
777 for (nextlinks = LOG_LINKS (link);
778 nextlinks;
779 nextlinks = XEXP (nextlinks, 1))
780 if ((next = try_combine (insn, link,
781 XEXP (nextlinks, 0),
782 &new_direct_jump_p)) != 0)
783 goto retry;
786 #ifdef HAVE_cc0
787 /* Try to combine a jump insn that uses CC0
788 with a preceding insn that sets CC0, and maybe with its
789 logical predecessor as well.
790 This is how we make decrement-and-branch insns.
791 We need this special code because data flow connections
792 via CC0 do not get entered in LOG_LINKS. */
794 if (JUMP_P (insn)
795 && (prev = prev_nonnote_insn (insn)) != 0
796 && NONJUMP_INSN_P (prev)
797 && sets_cc0_p (PATTERN (prev)))
799 if ((next = try_combine (insn, prev,
800 NULL_RTX, &new_direct_jump_p)) != 0)
801 goto retry;
803 for (nextlinks = LOG_LINKS (prev); nextlinks;
804 nextlinks = XEXP (nextlinks, 1))
805 if ((next = try_combine (insn, prev,
806 XEXP (nextlinks, 0),
807 &new_direct_jump_p)) != 0)
808 goto retry;
811 /* Do the same for an insn that explicitly references CC0. */
812 if (NONJUMP_INSN_P (insn)
813 && (prev = prev_nonnote_insn (insn)) != 0
814 && NONJUMP_INSN_P (prev)
815 && sets_cc0_p (PATTERN (prev))
816 && GET_CODE (PATTERN (insn)) == SET
817 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
819 if ((next = try_combine (insn, prev,
820 NULL_RTX, &new_direct_jump_p)) != 0)
821 goto retry;
823 for (nextlinks = LOG_LINKS (prev); nextlinks;
824 nextlinks = XEXP (nextlinks, 1))
825 if ((next = try_combine (insn, prev,
826 XEXP (nextlinks, 0),
827 &new_direct_jump_p)) != 0)
828 goto retry;
831 /* Finally, see if any of the insns that this insn links to
832 explicitly references CC0. If so, try this insn, that insn,
833 and its predecessor if it sets CC0. */
834 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
835 if (NONJUMP_INSN_P (XEXP (links, 0))
836 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
837 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
838 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
839 && NONJUMP_INSN_P (prev)
840 && sets_cc0_p (PATTERN (prev))
841 && (next = try_combine (insn, XEXP (links, 0),
842 prev, &new_direct_jump_p)) != 0)
843 goto retry;
844 #endif
846 /* Try combining an insn with two different insns whose results it
847 uses. */
848 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
849 for (nextlinks = XEXP (links, 1); nextlinks;
850 nextlinks = XEXP (nextlinks, 1))
851 if ((next = try_combine (insn, XEXP (links, 0),
852 XEXP (nextlinks, 0),
853 &new_direct_jump_p)) != 0)
854 goto retry;
856 /* Try this insn with each REG_EQUAL note it links back to. */
857 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
859 rtx set, note;
860 rtx temp = XEXP (links, 0);
861 if ((set = single_set (temp)) != 0
862 && (note = find_reg_equal_equiv_note (temp)) != 0
863 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
864 /* Avoid using a register that may already been marked
865 dead by an earlier instruction. */
866 && ! unmentioned_reg_p (note, SET_SRC (set))
867 && (GET_MODE (note) == VOIDmode
868 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
869 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
871 /* Temporarily replace the set's source with the
872 contents of the REG_EQUAL note. The insn will
873 be deleted or recognized by try_combine. */
874 rtx orig = SET_SRC (set);
875 SET_SRC (set) = note;
876 next = try_combine (insn, temp, NULL_RTX,
877 &new_direct_jump_p);
878 if (next)
879 goto retry;
880 SET_SRC (set) = orig;
884 if (!NOTE_P (insn))
885 record_dead_and_set_regs (insn);
887 retry:
892 clear_bb_flags ();
894 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, j, sbi)
895 BASIC_BLOCK (j)->flags |= BB_DIRTY;
896 new_direct_jump_p |= purge_all_dead_edges ();
897 delete_noop_moves ();
899 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
900 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
901 | PROP_KILL_DEAD_CODE);
903 /* Clean up. */
904 sbitmap_free (refresh_blocks);
905 free (uid_insn_cost);
906 free (reg_stat);
907 free (uid_cuid);
910 struct undo *undo, *next;
911 for (undo = undobuf.frees; undo; undo = next)
913 next = undo->next;
914 free (undo);
916 undobuf.frees = 0;
919 total_attempts += combine_attempts;
920 total_merges += combine_merges;
921 total_extras += combine_extras;
922 total_successes += combine_successes;
924 nonzero_sign_valid = 0;
925 rtl_hooks = general_rtl_hooks;
927 /* Make recognizer allow volatile MEMs again. */
928 init_recog ();
930 return new_direct_jump_p;
933 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
935 static void
936 init_reg_last (void)
938 unsigned int i;
939 for (i = 0; i < combine_max_regno; i++)
940 memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
943 /* Set up any promoted values for incoming argument registers. */
945 static void
946 setup_incoming_promotions (void)
948 unsigned int regno;
949 rtx reg;
950 enum machine_mode mode;
951 int unsignedp;
952 rtx first = get_insns ();
954 if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
956 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
957 /* Check whether this register can hold an incoming pointer
958 argument. FUNCTION_ARG_REGNO_P tests outgoing register
959 numbers, so translate if necessary due to register windows. */
960 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
961 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
963 record_value_for_reg
964 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
965 : SIGN_EXTEND),
966 GET_MODE (reg),
967 gen_rtx_CLOBBER (mode, const0_rtx)));
972 /* Called via note_stores. If X is a pseudo that is narrower than
973 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
975 If we are setting only a portion of X and we can't figure out what
976 portion, assume all bits will be used since we don't know what will
977 be happening.
979 Similarly, set how many bits of X are known to be copies of the sign bit
980 at all locations in the function. This is the smallest number implied
981 by any set of X. */
983 static void
984 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
985 void *data ATTRIBUTE_UNUSED)
987 unsigned int num;
989 if (REG_P (x)
990 && REGNO (x) >= FIRST_PSEUDO_REGISTER
991 /* If this register is undefined at the start of the file, we can't
992 say what its contents were. */
993 && ! REGNO_REG_SET_P
994 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start, REGNO (x))
995 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
997 if (set == 0 || GET_CODE (set) == CLOBBER)
999 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1000 reg_stat[REGNO (x)].sign_bit_copies = 1;
1001 return;
1004 /* If this is a complex assignment, see if we can convert it into a
1005 simple assignment. */
1006 set = expand_field_assignment (set);
1008 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1009 set what we know about X. */
1011 if (SET_DEST (set) == x
1012 || (GET_CODE (SET_DEST (set)) == SUBREG
1013 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1014 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1015 && SUBREG_REG (SET_DEST (set)) == x))
1017 rtx src = SET_SRC (set);
1019 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1020 /* If X is narrower than a word and SRC is a non-negative
1021 constant that would appear negative in the mode of X,
1022 sign-extend it for use in reg_stat[].nonzero_bits because some
1023 machines (maybe most) will actually do the sign-extension
1024 and this is the conservative approach.
1026 ??? For 2.5, try to tighten up the MD files in this regard
1027 instead of this kludge. */
1029 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1030 && GET_CODE (src) == CONST_INT
1031 && INTVAL (src) > 0
1032 && 0 != (INTVAL (src)
1033 & ((HOST_WIDE_INT) 1
1034 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1035 src = GEN_INT (INTVAL (src)
1036 | ((HOST_WIDE_INT) (-1)
1037 << GET_MODE_BITSIZE (GET_MODE (x))));
1038 #endif
1040 /* Don't call nonzero_bits if it cannot change anything. */
1041 if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1042 reg_stat[REGNO (x)].nonzero_bits
1043 |= nonzero_bits (src, nonzero_bits_mode);
1044 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1045 if (reg_stat[REGNO (x)].sign_bit_copies == 0
1046 || reg_stat[REGNO (x)].sign_bit_copies > num)
1047 reg_stat[REGNO (x)].sign_bit_copies = num;
1049 else
1051 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1052 reg_stat[REGNO (x)].sign_bit_copies = 1;
1057 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1058 insns that were previously combined into I3 or that will be combined
1059 into the merger of INSN and I3.
1061 Return 0 if the combination is not allowed for any reason.
1063 If the combination is allowed, *PDEST will be set to the single
1064 destination of INSN and *PSRC to the single source, and this function
1065 will return 1. */
1067 static int
1068 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1069 rtx *pdest, rtx *psrc)
1071 int i;
1072 rtx set = 0, src, dest;
1073 rtx p;
1074 #ifdef AUTO_INC_DEC
1075 rtx link;
1076 #endif
1077 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1078 && next_active_insn (succ) == i3)
1079 : next_active_insn (insn) == i3);
1081 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1082 or a PARALLEL consisting of such a SET and CLOBBERs.
1084 If INSN has CLOBBER parallel parts, ignore them for our processing.
1085 By definition, these happen during the execution of the insn. When it
1086 is merged with another insn, all bets are off. If they are, in fact,
1087 needed and aren't also supplied in I3, they may be added by
1088 recog_for_combine. Otherwise, it won't match.
1090 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1091 note.
1093 Get the source and destination of INSN. If more than one, can't
1094 combine. */
1096 if (GET_CODE (PATTERN (insn)) == SET)
1097 set = PATTERN (insn);
1098 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1099 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1101 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1103 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1104 rtx note;
1106 switch (GET_CODE (elt))
1108 /* This is important to combine floating point insns
1109 for the SH4 port. */
1110 case USE:
1111 /* Combining an isolated USE doesn't make sense.
1112 We depend here on combinable_i3pat to reject them. */
1113 /* The code below this loop only verifies that the inputs of
1114 the SET in INSN do not change. We call reg_set_between_p
1115 to verify that the REG in the USE does not change between
1116 I3 and INSN.
1117 If the USE in INSN was for a pseudo register, the matching
1118 insn pattern will likely match any register; combining this
1119 with any other USE would only be safe if we knew that the
1120 used registers have identical values, or if there was
1121 something to tell them apart, e.g. different modes. For
1122 now, we forgo such complicated tests and simply disallow
1123 combining of USES of pseudo registers with any other USE. */
1124 if (REG_P (XEXP (elt, 0))
1125 && GET_CODE (PATTERN (i3)) == PARALLEL)
1127 rtx i3pat = PATTERN (i3);
1128 int i = XVECLEN (i3pat, 0) - 1;
1129 unsigned int regno = REGNO (XEXP (elt, 0));
1133 rtx i3elt = XVECEXP (i3pat, 0, i);
1135 if (GET_CODE (i3elt) == USE
1136 && REG_P (XEXP (i3elt, 0))
1137 && (REGNO (XEXP (i3elt, 0)) == regno
1138 ? reg_set_between_p (XEXP (elt, 0),
1139 PREV_INSN (insn), i3)
1140 : regno >= FIRST_PSEUDO_REGISTER))
1141 return 0;
1143 while (--i >= 0);
1145 break;
1147 /* We can ignore CLOBBERs. */
1148 case CLOBBER:
1149 break;
1151 case SET:
1152 /* Ignore SETs whose result isn't used but not those that
1153 have side-effects. */
1154 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1155 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1156 || INTVAL (XEXP (note, 0)) <= 0)
1157 && ! side_effects_p (elt))
1158 break;
1160 /* If we have already found a SET, this is a second one and
1161 so we cannot combine with this insn. */
1162 if (set)
1163 return 0;
1165 set = elt;
1166 break;
1168 default:
1169 /* Anything else means we can't combine. */
1170 return 0;
1174 if (set == 0
1175 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1176 so don't do anything with it. */
1177 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1178 return 0;
1180 else
1181 return 0;
1183 if (set == 0)
1184 return 0;
1186 set = expand_field_assignment (set);
1187 src = SET_SRC (set), dest = SET_DEST (set);
1189 /* Don't eliminate a store in the stack pointer. */
1190 if (dest == stack_pointer_rtx
1191 /* Don't combine with an insn that sets a register to itself if it has
1192 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1193 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1194 /* Can't merge an ASM_OPERANDS. */
1195 || GET_CODE (src) == ASM_OPERANDS
1196 /* Can't merge a function call. */
1197 || GET_CODE (src) == CALL
1198 /* Don't eliminate a function call argument. */
1199 || (CALL_P (i3)
1200 && (find_reg_fusage (i3, USE, dest)
1201 || (REG_P (dest)
1202 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1203 && global_regs[REGNO (dest)])))
1204 /* Don't substitute into an incremented register. */
1205 || FIND_REG_INC_NOTE (i3, dest)
1206 || (succ && FIND_REG_INC_NOTE (succ, dest))
1207 /* Don't substitute into a non-local goto, this confuses CFG. */
1208 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1209 #if 0
1210 /* Don't combine the end of a libcall into anything. */
1211 /* ??? This gives worse code, and appears to be unnecessary, since no
1212 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1213 use REG_RETVAL notes for noconflict blocks, but other code here
1214 makes sure that those insns don't disappear. */
1215 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1216 #endif
1217 /* Make sure that DEST is not used after SUCC but before I3. */
1218 || (succ && ! all_adjacent
1219 && reg_used_between_p (dest, succ, i3))
1220 /* Make sure that the value that is to be substituted for the register
1221 does not use any registers whose values alter in between. However,
1222 If the insns are adjacent, a use can't cross a set even though we
1223 think it might (this can happen for a sequence of insns each setting
1224 the same destination; last_set of that register might point to
1225 a NOTE). If INSN has a REG_EQUIV note, the register is always
1226 equivalent to the memory so the substitution is valid even if there
1227 are intervening stores. Also, don't move a volatile asm or
1228 UNSPEC_VOLATILE across any other insns. */
1229 || (! all_adjacent
1230 && (((!MEM_P (src)
1231 || ! find_reg_note (insn, REG_EQUIV, src))
1232 && use_crosses_set_p (src, INSN_CUID (insn)))
1233 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1234 || GET_CODE (src) == UNSPEC_VOLATILE))
1235 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1236 better register allocation by not doing the combine. */
1237 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1238 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1239 /* Don't combine across a CALL_INSN, because that would possibly
1240 change whether the life span of some REGs crosses calls or not,
1241 and it is a pain to update that information.
1242 Exception: if source is a constant, moving it later can't hurt.
1243 Accept that special case, because it helps -fforce-addr a lot. */
1244 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1245 return 0;
1247 /* DEST must either be a REG or CC0. */
1248 if (REG_P (dest))
1250 /* If register alignment is being enforced for multi-word items in all
1251 cases except for parameters, it is possible to have a register copy
1252 insn referencing a hard register that is not allowed to contain the
1253 mode being copied and which would not be valid as an operand of most
1254 insns. Eliminate this problem by not combining with such an insn.
1256 Also, on some machines we don't want to extend the life of a hard
1257 register. */
1259 if (REG_P (src)
1260 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1261 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1262 /* Don't extend the life of a hard register unless it is
1263 user variable (if we have few registers) or it can't
1264 fit into the desired register (meaning something special
1265 is going on).
1266 Also avoid substituting a return register into I3, because
1267 reload can't handle a conflict with constraints of other
1268 inputs. */
1269 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1270 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1271 return 0;
1273 else if (GET_CODE (dest) != CC0)
1274 return 0;
1277 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1278 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1279 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1281 /* Don't substitute for a register intended as a clobberable
1282 operand. */
1283 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1284 if (rtx_equal_p (reg, dest))
1285 return 0;
1287 /* If the clobber represents an earlyclobber operand, we must not
1288 substitute an expression containing the clobbered register.
1289 As we do not analyze the constraint strings here, we have to
1290 make the conservative assumption. However, if the register is
1291 a fixed hard reg, the clobber cannot represent any operand;
1292 we leave it up to the machine description to either accept or
1293 reject use-and-clobber patterns. */
1294 if (!REG_P (reg)
1295 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1296 || !fixed_regs[REGNO (reg)])
1297 if (reg_overlap_mentioned_p (reg, src))
1298 return 0;
1301 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1302 or not), reject, unless nothing volatile comes between it and I3 */
1304 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1306 /* Make sure succ doesn't contain a volatile reference. */
1307 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1308 return 0;
1310 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1311 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1312 return 0;
1315 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1316 to be an explicit register variable, and was chosen for a reason. */
1318 if (GET_CODE (src) == ASM_OPERANDS
1319 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1320 return 0;
1322 /* If there are any volatile insns between INSN and I3, reject, because
1323 they might affect machine state. */
1325 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1326 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1327 return 0;
1329 /* If INSN contains an autoincrement or autodecrement, make sure that
1330 register is not used between there and I3, and not already used in
1331 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1332 Also insist that I3 not be a jump; if it were one
1333 and the incremented register were spilled, we would lose. */
1335 #ifdef AUTO_INC_DEC
1336 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1337 if (REG_NOTE_KIND (link) == REG_INC
1338 && (JUMP_P (i3)
1339 || reg_used_between_p (XEXP (link, 0), insn, i3)
1340 || (pred != NULL_RTX
1341 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1342 || (succ != NULL_RTX
1343 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1344 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1345 return 0;
1346 #endif
1348 #ifdef HAVE_cc0
1349 /* Don't combine an insn that follows a CC0-setting insn.
1350 An insn that uses CC0 must not be separated from the one that sets it.
1351 We do, however, allow I2 to follow a CC0-setting insn if that insn
1352 is passed as I1; in that case it will be deleted also.
1353 We also allow combining in this case if all the insns are adjacent
1354 because that would leave the two CC0 insns adjacent as well.
1355 It would be more logical to test whether CC0 occurs inside I1 or I2,
1356 but that would be much slower, and this ought to be equivalent. */
1358 p = prev_nonnote_insn (insn);
1359 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1360 && ! all_adjacent)
1361 return 0;
1362 #endif
1364 /* If we get here, we have passed all the tests and the combination is
1365 to be allowed. */
1367 *pdest = dest;
1368 *psrc = src;
1370 return 1;
1373 /* LOC is the location within I3 that contains its pattern or the component
1374 of a PARALLEL of the pattern. We validate that it is valid for combining.
1376 One problem is if I3 modifies its output, as opposed to replacing it
1377 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1378 so would produce an insn that is not equivalent to the original insns.
1380 Consider:
1382 (set (reg:DI 101) (reg:DI 100))
1383 (set (subreg:SI (reg:DI 101) 0) <foo>)
1385 This is NOT equivalent to:
1387 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1388 (set (reg:DI 101) (reg:DI 100))])
1390 Not only does this modify 100 (in which case it might still be valid
1391 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1393 We can also run into a problem if I2 sets a register that I1
1394 uses and I1 gets directly substituted into I3 (not via I2). In that
1395 case, we would be getting the wrong value of I2DEST into I3, so we
1396 must reject the combination. This case occurs when I2 and I1 both
1397 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1398 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1399 of a SET must prevent combination from occurring.
1401 Before doing the above check, we first try to expand a field assignment
1402 into a set of logical operations.
1404 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1405 we place a register that is both set and used within I3. If more than one
1406 such register is detected, we fail.
1408 Return 1 if the combination is valid, zero otherwise. */
1410 static int
1411 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1412 int i1_not_in_src, rtx *pi3dest_killed)
1414 rtx x = *loc;
1416 if (GET_CODE (x) == SET)
1418 rtx set = x ;
1419 rtx dest = SET_DEST (set);
1420 rtx src = SET_SRC (set);
1421 rtx inner_dest = dest;
1423 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1424 || GET_CODE (inner_dest) == SUBREG
1425 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1426 inner_dest = XEXP (inner_dest, 0);
1428 /* Check for the case where I3 modifies its output, as discussed
1429 above. We don't want to prevent pseudos from being combined
1430 into the address of a MEM, so only prevent the combination if
1431 i1 or i2 set the same MEM. */
1432 if ((inner_dest != dest &&
1433 (!MEM_P (inner_dest)
1434 || rtx_equal_p (i2dest, inner_dest)
1435 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1436 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1437 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1439 /* This is the same test done in can_combine_p except we can't test
1440 all_adjacent; we don't have to, since this instruction will stay
1441 in place, thus we are not considering increasing the lifetime of
1442 INNER_DEST.
1444 Also, if this insn sets a function argument, combining it with
1445 something that might need a spill could clobber a previous
1446 function argument; the all_adjacent test in can_combine_p also
1447 checks this; here, we do a more specific test for this case. */
1449 || (REG_P (inner_dest)
1450 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1451 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1452 GET_MODE (inner_dest))))
1453 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1454 return 0;
1456 /* If DEST is used in I3, it is being killed in this insn,
1457 so record that for later.
1458 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1459 STACK_POINTER_REGNUM, since these are always considered to be
1460 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1461 if (pi3dest_killed && REG_P (dest)
1462 && reg_referenced_p (dest, PATTERN (i3))
1463 && REGNO (dest) != FRAME_POINTER_REGNUM
1464 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1465 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1466 #endif
1467 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1468 && (REGNO (dest) != ARG_POINTER_REGNUM
1469 || ! fixed_regs [REGNO (dest)])
1470 #endif
1471 && REGNO (dest) != STACK_POINTER_REGNUM)
1473 if (*pi3dest_killed)
1474 return 0;
1476 *pi3dest_killed = dest;
1480 else if (GET_CODE (x) == PARALLEL)
1482 int i;
1484 for (i = 0; i < XVECLEN (x, 0); i++)
1485 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1486 i1_not_in_src, pi3dest_killed))
1487 return 0;
1490 return 1;
1493 /* Return 1 if X is an arithmetic expression that contains a multiplication
1494 and division. We don't count multiplications by powers of two here. */
1496 static int
1497 contains_muldiv (rtx x)
1499 switch (GET_CODE (x))
1501 case MOD: case DIV: case UMOD: case UDIV:
1502 return 1;
1504 case MULT:
1505 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1506 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1507 default:
1508 if (BINARY_P (x))
1509 return contains_muldiv (XEXP (x, 0))
1510 || contains_muldiv (XEXP (x, 1));
1512 if (UNARY_P (x))
1513 return contains_muldiv (XEXP (x, 0));
1515 return 0;
1519 /* Determine whether INSN can be used in a combination. Return nonzero if
1520 not. This is used in try_combine to detect early some cases where we
1521 can't perform combinations. */
1523 static int
1524 cant_combine_insn_p (rtx insn)
1526 rtx set;
1527 rtx src, dest;
1529 /* If this isn't really an insn, we can't do anything.
1530 This can occur when flow deletes an insn that it has merged into an
1531 auto-increment address. */
1532 if (! INSN_P (insn))
1533 return 1;
1535 /* Never combine loads and stores involving hard regs that are likely
1536 to be spilled. The register allocator can usually handle such
1537 reg-reg moves by tying. If we allow the combiner to make
1538 substitutions of likely-spilled regs, reload might die.
1539 As an exception, we allow combinations involving fixed regs; these are
1540 not available to the register allocator so there's no risk involved. */
1542 set = single_set (insn);
1543 if (! set)
1544 return 0;
1545 src = SET_SRC (set);
1546 dest = SET_DEST (set);
1547 if (GET_CODE (src) == SUBREG)
1548 src = SUBREG_REG (src);
1549 if (GET_CODE (dest) == SUBREG)
1550 dest = SUBREG_REG (dest);
1551 if (REG_P (src) && REG_P (dest)
1552 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1553 && ! fixed_regs[REGNO (src)]
1554 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1555 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1556 && ! fixed_regs[REGNO (dest)]
1557 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1558 return 1;
1560 return 0;
1563 struct likely_spilled_retval_info
1565 unsigned regno, nregs;
1566 unsigned mask;
1569 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
1570 hard registers that are known to be written to / clobbered in full. */
1571 static void
1572 likely_spilled_retval_1 (rtx x, rtx set, void *data)
1574 struct likely_spilled_retval_info *info = data;
1575 unsigned regno, nregs;
1576 unsigned new_mask;
1578 if (!REG_P (XEXP (set, 0)))
1579 return;
1580 regno = REGNO (x);
1581 if (regno >= info->regno + info->nregs)
1582 return;
1583 nregs = hard_regno_nregs[regno][GET_MODE (x)];
1584 if (regno + nregs <= info->regno)
1585 return;
1586 new_mask = (2U << (nregs - 1)) - 1;
1587 if (regno < info->regno)
1588 new_mask >>= info->regno - regno;
1589 else
1590 new_mask <<= regno - info->regno;
1591 info->mask &= new_mask;
1594 /* Return nonzero iff part of the return value is live during INSN, and
1595 it is likely spilled. This can happen when more than one insn is needed
1596 to copy the return value, e.g. when we consider to combine into the
1597 second copy insn for a complex value. */
1599 static int
1600 likely_spilled_retval_p (rtx insn)
1602 rtx use = BB_END (this_basic_block);
1603 rtx reg, p;
1604 unsigned regno, nregs;
1605 /* We assume here that no machine mode needs more than
1606 32 hard registers when the value overlaps with a register
1607 for which FUNCTION_VALUE_REGNO_P is true. */
1608 unsigned mask;
1609 struct likely_spilled_retval_info info;
1611 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
1612 return 0;
1613 reg = XEXP (PATTERN (use), 0);
1614 if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
1615 return 0;
1616 regno = REGNO (reg);
1617 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
1618 if (nregs == 1)
1619 return 0;
1620 mask = (2U << (nregs - 1)) - 1;
1622 /* Disregard parts of the return value that are set later. */
1623 info.regno = regno;
1624 info.nregs = nregs;
1625 info.mask = mask;
1626 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
1627 note_stores (PATTERN (insn), likely_spilled_retval_1, &info);
1628 mask = info.mask;
1630 /* Check if any of the (probably) live return value registers is
1631 likely spilled. */
1632 nregs --;
1635 if ((mask & 1 << nregs)
1636 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
1637 return 1;
1638 } while (nregs--);
1639 return 0;
1642 /* Adjust INSN after we made a change to its destination.
1644 Changing the destination can invalidate notes that say something about
1645 the results of the insn and a LOG_LINK pointing to the insn. */
1647 static void
1648 adjust_for_new_dest (rtx insn)
1650 rtx *loc;
1652 /* For notes, be conservative and simply remove them. */
1653 loc = &REG_NOTES (insn);
1654 while (*loc)
1656 enum reg_note kind = REG_NOTE_KIND (*loc);
1657 if (kind == REG_EQUAL || kind == REG_EQUIV)
1658 *loc = XEXP (*loc, 1);
1659 else
1660 loc = &XEXP (*loc, 1);
1663 /* The new insn will have a destination that was previously the destination
1664 of an insn just above it. Call distribute_links to make a LOG_LINK from
1665 the next use of that destination. */
1666 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1669 /* Return TRUE if combine can reuse reg X in mode MODE.
1670 ADDED_SETS is nonzero if the original set is still required. */
1671 static bool
1672 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
1674 unsigned int regno;
1676 if (!REG_P(x))
1677 return false;
1679 regno = REGNO (x);
1680 /* Allow hard registers if the new mode is legal, and occupies no more
1681 registers than the old mode. */
1682 if (regno < FIRST_PSEUDO_REGISTER)
1683 return (HARD_REGNO_MODE_OK (regno, mode)
1684 && (hard_regno_nregs[regno][GET_MODE (x)]
1685 >= hard_regno_nregs[regno][mode]));
1687 /* Or a pseudo that is only used once. */
1688 return (REG_N_SETS (regno) == 1 && !added_sets
1689 && !REG_USERVAR_P (x));
1692 /* Try to combine the insns I1 and I2 into I3.
1693 Here I1 and I2 appear earlier than I3.
1694 I1 can be zero; then we combine just I2 into I3.
1696 If we are combining three insns and the resulting insn is not recognized,
1697 try splitting it into two insns. If that happens, I2 and I3 are retained
1698 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1699 are pseudo-deleted.
1701 Return 0 if the combination does not work. Then nothing is changed.
1702 If we did the combination, return the insn at which combine should
1703 resume scanning.
1705 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1706 new direct jump instruction. */
1708 static rtx
1709 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1711 /* New patterns for I3 and I2, respectively. */
1712 rtx newpat, newi2pat = 0;
1713 rtvec newpat_vec_with_clobbers = 0;
1714 int substed_i2 = 0, substed_i1 = 0;
1715 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1716 int added_sets_1, added_sets_2;
1717 /* Total number of SETs to put into I3. */
1718 int total_sets;
1719 /* Nonzero if I2's body now appears in I3. */
1720 int i2_is_used;
1721 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1722 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1723 /* Contains I3 if the destination of I3 is used in its source, which means
1724 that the old life of I3 is being killed. If that usage is placed into
1725 I2 and not in I3, a REG_DEAD note must be made. */
1726 rtx i3dest_killed = 0;
1727 /* SET_DEST and SET_SRC of I2 and I1. */
1728 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1729 /* PATTERN (I2), or a copy of it in certain cases. */
1730 rtx i2pat;
1731 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1732 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1733 int i1_feeds_i3 = 0;
1734 /* Notes that must be added to REG_NOTES in I3 and I2. */
1735 rtx new_i3_notes, new_i2_notes;
1736 /* Notes that we substituted I3 into I2 instead of the normal case. */
1737 int i3_subst_into_i2 = 0;
1738 /* Notes that I1, I2 or I3 is a MULT operation. */
1739 int have_mult = 0;
1740 int swap_i2i3 = 0;
1742 int maxreg;
1743 rtx temp;
1744 rtx link;
1745 int i;
1747 /* Exit early if one of the insns involved can't be used for
1748 combinations. */
1749 if (cant_combine_insn_p (i3)
1750 || cant_combine_insn_p (i2)
1751 || (i1 && cant_combine_insn_p (i1))
1752 || likely_spilled_retval_p (i3)
1753 /* We also can't do anything if I3 has a
1754 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1755 libcall. */
1756 #if 0
1757 /* ??? This gives worse code, and appears to be unnecessary, since no
1758 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1759 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1760 #endif
1762 return 0;
1764 combine_attempts++;
1765 undobuf.other_insn = 0;
1767 /* Reset the hard register usage information. */
1768 CLEAR_HARD_REG_SET (newpat_used_regs);
1770 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1771 code below, set I1 to be the earlier of the two insns. */
1772 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1773 temp = i1, i1 = i2, i2 = temp;
1775 added_links_insn = 0;
1777 /* First check for one important special-case that the code below will
1778 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1779 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1780 we may be able to replace that destination with the destination of I3.
1781 This occurs in the common code where we compute both a quotient and
1782 remainder into a structure, in which case we want to do the computation
1783 directly into the structure to avoid register-register copies.
1785 Note that this case handles both multiple sets in I2 and also
1786 cases where I2 has a number of CLOBBER or PARALLELs.
1788 We make very conservative checks below and only try to handle the
1789 most common cases of this. For example, we only handle the case
1790 where I2 and I3 are adjacent to avoid making difficult register
1791 usage tests. */
1793 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
1794 && REG_P (SET_SRC (PATTERN (i3)))
1795 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1796 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1797 && GET_CODE (PATTERN (i2)) == PARALLEL
1798 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1799 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1800 below would need to check what is inside (and reg_overlap_mentioned_p
1801 doesn't support those codes anyway). Don't allow those destinations;
1802 the resulting insn isn't likely to be recognized anyway. */
1803 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1804 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1805 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1806 SET_DEST (PATTERN (i3)))
1807 && next_real_insn (i2) == i3)
1809 rtx p2 = PATTERN (i2);
1811 /* Make sure that the destination of I3,
1812 which we are going to substitute into one output of I2,
1813 is not used within another output of I2. We must avoid making this:
1814 (parallel [(set (mem (reg 69)) ...)
1815 (set (reg 69) ...)])
1816 which is not well-defined as to order of actions.
1817 (Besides, reload can't handle output reloads for this.)
1819 The problem can also happen if the dest of I3 is a memory ref,
1820 if another dest in I2 is an indirect memory ref. */
1821 for (i = 0; i < XVECLEN (p2, 0); i++)
1822 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1823 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1824 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1825 SET_DEST (XVECEXP (p2, 0, i))))
1826 break;
1828 if (i == XVECLEN (p2, 0))
1829 for (i = 0; i < XVECLEN (p2, 0); i++)
1830 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1831 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1832 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1834 combine_merges++;
1836 subst_insn = i3;
1837 subst_low_cuid = INSN_CUID (i2);
1839 added_sets_2 = added_sets_1 = 0;
1840 i2dest = SET_SRC (PATTERN (i3));
1842 /* Replace the dest in I2 with our dest and make the resulting
1843 insn the new pattern for I3. Then skip to where we
1844 validate the pattern. Everything was set up above. */
1845 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1846 SET_DEST (PATTERN (i3)));
1848 newpat = p2;
1849 i3_subst_into_i2 = 1;
1850 goto validate_replacement;
1854 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1855 one of those words to another constant, merge them by making a new
1856 constant. */
1857 if (i1 == 0
1858 && (temp = single_set (i2)) != 0
1859 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1860 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1861 && REG_P (SET_DEST (temp))
1862 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1863 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1864 && GET_CODE (PATTERN (i3)) == SET
1865 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1866 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1867 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1868 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1869 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1871 HOST_WIDE_INT lo, hi;
1873 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1874 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1875 else
1877 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1878 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1881 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1883 /* We don't handle the case of the target word being wider
1884 than a host wide int. */
1885 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD);
1887 lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1888 lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1889 & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1891 else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1892 hi = INTVAL (SET_SRC (PATTERN (i3)));
1893 else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1895 int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1896 >> (HOST_BITS_PER_WIDE_INT - 1));
1898 lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1899 (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1900 lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1901 (INTVAL (SET_SRC (PATTERN (i3)))));
1902 if (hi == sign)
1903 hi = lo < 0 ? -1 : 0;
1905 else
1906 /* We don't handle the case of the higher word not fitting
1907 entirely in either hi or lo. */
1908 gcc_unreachable ();
1910 combine_merges++;
1911 subst_insn = i3;
1912 subst_low_cuid = INSN_CUID (i2);
1913 added_sets_2 = added_sets_1 = 0;
1914 i2dest = SET_DEST (temp);
1916 SUBST (SET_SRC (temp),
1917 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1919 newpat = PATTERN (i2);
1920 goto validate_replacement;
1923 #ifndef HAVE_cc0
1924 /* If we have no I1 and I2 looks like:
1925 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1926 (set Y OP)])
1927 make up a dummy I1 that is
1928 (set Y OP)
1929 and change I2 to be
1930 (set (reg:CC X) (compare:CC Y (const_int 0)))
1932 (We can ignore any trailing CLOBBERs.)
1934 This undoes a previous combination and allows us to match a branch-and-
1935 decrement insn. */
1937 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1938 && XVECLEN (PATTERN (i2), 0) >= 2
1939 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1940 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1941 == MODE_CC)
1942 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1943 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1944 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1945 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
1946 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1947 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1949 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1950 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1951 break;
1953 if (i == 1)
1955 /* We make I1 with the same INSN_UID as I2. This gives it
1956 the same INSN_CUID for value tracking. Our fake I1 will
1957 never appear in the insn stream so giving it the same INSN_UID
1958 as I2 will not cause a problem. */
1960 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1961 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1962 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1963 NULL_RTX);
1965 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1966 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1967 SET_DEST (PATTERN (i1)));
1970 #endif
1972 /* Verify that I2 and I1 are valid for combining. */
1973 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1974 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1976 undo_all ();
1977 return 0;
1980 /* Record whether I2DEST is used in I2SRC and similarly for the other
1981 cases. Knowing this will help in register status updating below. */
1982 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1983 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1984 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1986 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1987 in I2SRC. */
1988 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1990 /* Ensure that I3's pattern can be the destination of combines. */
1991 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1992 i1 && i2dest_in_i1src && i1_feeds_i3,
1993 &i3dest_killed))
1995 undo_all ();
1996 return 0;
1999 /* See if any of the insns is a MULT operation. Unless one is, we will
2000 reject a combination that is, since it must be slower. Be conservative
2001 here. */
2002 if (GET_CODE (i2src) == MULT
2003 || (i1 != 0 && GET_CODE (i1src) == MULT)
2004 || (GET_CODE (PATTERN (i3)) == SET
2005 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2006 have_mult = 1;
2008 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2009 We used to do this EXCEPT in one case: I3 has a post-inc in an
2010 output operand. However, that exception can give rise to insns like
2011 mov r3,(r3)+
2012 which is a famous insn on the PDP-11 where the value of r3 used as the
2013 source was model-dependent. Avoid this sort of thing. */
2015 #if 0
2016 if (!(GET_CODE (PATTERN (i3)) == SET
2017 && REG_P (SET_SRC (PATTERN (i3)))
2018 && MEM_P (SET_DEST (PATTERN (i3)))
2019 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2020 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2021 /* It's not the exception. */
2022 #endif
2023 #ifdef AUTO_INC_DEC
2024 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2025 if (REG_NOTE_KIND (link) == REG_INC
2026 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2027 || (i1 != 0
2028 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2030 undo_all ();
2031 return 0;
2033 #endif
2035 /* See if the SETs in I1 or I2 need to be kept around in the merged
2036 instruction: whenever the value set there is still needed past I3.
2037 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2039 For the SET in I1, we have two cases: If I1 and I2 independently
2040 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2041 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2042 in I1 needs to be kept around unless I1DEST dies or is set in either
2043 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
2044 I1DEST. If so, we know I1 feeds into I2. */
2046 added_sets_2 = ! dead_or_set_p (i3, i2dest);
2048 added_sets_1
2049 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2050 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2052 /* If the set in I2 needs to be kept around, we must make a copy of
2053 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2054 PATTERN (I2), we are only substituting for the original I1DEST, not into
2055 an already-substituted copy. This also prevents making self-referential
2056 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2057 I2DEST. */
2059 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
2060 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
2061 : PATTERN (i2));
2063 if (added_sets_2)
2064 i2pat = copy_rtx (i2pat);
2066 combine_merges++;
2068 /* Substitute in the latest insn for the regs set by the earlier ones. */
2070 maxreg = max_reg_num ();
2072 subst_insn = i3;
2074 /* It is possible that the source of I2 or I1 may be performing an
2075 unneeded operation, such as a ZERO_EXTEND of something that is known
2076 to have the high part zero. Handle that case by letting subst look at
2077 the innermost one of them.
2079 Another way to do this would be to have a function that tries to
2080 simplify a single insn instead of merging two or more insns. We don't
2081 do this because of the potential of infinite loops and because
2082 of the potential extra memory required. However, doing it the way
2083 we are is a bit of a kludge and doesn't catch all cases.
2085 But only do this if -fexpensive-optimizations since it slows things down
2086 and doesn't usually win. */
2088 if (flag_expensive_optimizations)
2090 /* Pass pc_rtx so no substitutions are done, just simplifications. */
2091 if (i1)
2093 subst_low_cuid = INSN_CUID (i1);
2094 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2096 else
2098 subst_low_cuid = INSN_CUID (i2);
2099 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2103 #ifndef HAVE_cc0
2104 /* Many machines that don't use CC0 have insns that can both perform an
2105 arithmetic operation and set the condition code. These operations will
2106 be represented as a PARALLEL with the first element of the vector
2107 being a COMPARE of an arithmetic operation with the constant zero.
2108 The second element of the vector will set some pseudo to the result
2109 of the same arithmetic operation. If we simplify the COMPARE, we won't
2110 match such a pattern and so will generate an extra insn. Here we test
2111 for this case, where both the comparison and the operation result are
2112 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2113 I2SRC. Later we will make the PARALLEL that contains I2. */
2115 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2116 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2117 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2118 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2120 #ifdef SELECT_CC_MODE
2121 rtx *cc_use;
2122 enum machine_mode compare_mode;
2123 #endif
2125 newpat = PATTERN (i3);
2126 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2128 i2_is_used = 1;
2130 #ifdef SELECT_CC_MODE
2131 /* See if a COMPARE with the operand we substituted in should be done
2132 with the mode that is currently being used. If not, do the same
2133 processing we do in `subst' for a SET; namely, if the destination
2134 is used only once, try to replace it with a register of the proper
2135 mode and also replace the COMPARE. */
2136 if (undobuf.other_insn == 0
2137 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2138 &undobuf.other_insn))
2139 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2140 i2src, const0_rtx))
2141 != GET_MODE (SET_DEST (newpat))))
2143 if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2144 compare_mode))
2146 unsigned int regno = REGNO (SET_DEST (newpat));
2147 rtx new_dest = gen_rtx_REG (compare_mode, regno);
2149 if (regno >= FIRST_PSEUDO_REGISTER)
2150 SUBST (regno_reg_rtx[regno], new_dest);
2152 SUBST (SET_DEST (newpat), new_dest);
2153 SUBST (XEXP (*cc_use, 0), new_dest);
2154 SUBST (SET_SRC (newpat),
2155 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2157 else
2158 undobuf.other_insn = 0;
2160 #endif
2162 else
2163 #endif
2165 n_occurrences = 0; /* `subst' counts here */
2167 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2168 need to make a unique copy of I2SRC each time we substitute it
2169 to avoid self-referential rtl. */
2171 subst_low_cuid = INSN_CUID (i2);
2172 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2173 ! i1_feeds_i3 && i1dest_in_i1src);
2174 substed_i2 = 1;
2176 /* Record whether i2's body now appears within i3's body. */
2177 i2_is_used = n_occurrences;
2180 /* If we already got a failure, don't try to do more. Otherwise,
2181 try to substitute in I1 if we have it. */
2183 if (i1 && GET_CODE (newpat) != CLOBBER)
2185 /* Before we can do this substitution, we must redo the test done
2186 above (see detailed comments there) that ensures that I1DEST
2187 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2189 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2190 0, (rtx*) 0))
2192 undo_all ();
2193 return 0;
2196 n_occurrences = 0;
2197 subst_low_cuid = INSN_CUID (i1);
2198 newpat = subst (newpat, i1dest, i1src, 0, 0);
2199 substed_i1 = 1;
2202 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2203 to count all the ways that I2SRC and I1SRC can be used. */
2204 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2205 && i2_is_used + added_sets_2 > 1)
2206 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2207 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2208 > 1))
2209 /* Fail if we tried to make a new register. */
2210 || max_reg_num () != maxreg
2211 /* Fail if we couldn't do something and have a CLOBBER. */
2212 || GET_CODE (newpat) == CLOBBER
2213 /* Fail if this new pattern is a MULT and we didn't have one before
2214 at the outer level. */
2215 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2216 && ! have_mult))
2218 undo_all ();
2219 return 0;
2222 /* If the actions of the earlier insns must be kept
2223 in addition to substituting them into the latest one,
2224 we must make a new PARALLEL for the latest insn
2225 to hold additional the SETs. */
2227 if (added_sets_1 || added_sets_2)
2229 combine_extras++;
2231 if (GET_CODE (newpat) == PARALLEL)
2233 rtvec old = XVEC (newpat, 0);
2234 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2235 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2236 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2237 sizeof (old->elem[0]) * old->num_elem);
2239 else
2241 rtx old = newpat;
2242 total_sets = 1 + added_sets_1 + added_sets_2;
2243 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2244 XVECEXP (newpat, 0, 0) = old;
2247 if (added_sets_1)
2248 XVECEXP (newpat, 0, --total_sets)
2249 = (GET_CODE (PATTERN (i1)) == PARALLEL
2250 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2252 if (added_sets_2)
2254 /* If there is no I1, use I2's body as is. We used to also not do
2255 the subst call below if I2 was substituted into I3,
2256 but that could lose a simplification. */
2257 if (i1 == 0)
2258 XVECEXP (newpat, 0, --total_sets) = i2pat;
2259 else
2260 /* See comment where i2pat is assigned. */
2261 XVECEXP (newpat, 0, --total_sets)
2262 = subst (i2pat, i1dest, i1src, 0, 0);
2266 /* We come here when we are replacing a destination in I2 with the
2267 destination of I3. */
2268 validate_replacement:
2270 /* Note which hard regs this insn has as inputs. */
2271 mark_used_regs_combine (newpat);
2273 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2274 consider splitting this pattern, we might need these clobbers. */
2275 if (i1 && GET_CODE (newpat) == PARALLEL
2276 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2278 int len = XVECLEN (newpat, 0);
2280 newpat_vec_with_clobbers = rtvec_alloc (len);
2281 for (i = 0; i < len; i++)
2282 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2285 /* Is the result of combination a valid instruction? */
2286 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2288 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2289 the second SET's destination is a register that is unused and isn't
2290 marked as an instruction that might trap in an EH region. In that case,
2291 we just need the first SET. This can occur when simplifying a divmod
2292 insn. We *must* test for this case here because the code below that
2293 splits two independent SETs doesn't handle this case correctly when it
2294 updates the register status.
2296 It's pointless doing this if we originally had two sets, one from
2297 i3, and one from i2. Combining then splitting the parallel results
2298 in the original i2 again plus an invalid insn (which we delete).
2299 The net effect is only to move instructions around, which makes
2300 debug info less accurate.
2302 Also check the case where the first SET's destination is unused.
2303 That would not cause incorrect code, but does cause an unneeded
2304 insn to remain. */
2306 if (insn_code_number < 0
2307 && !(added_sets_2 && i1 == 0)
2308 && GET_CODE (newpat) == PARALLEL
2309 && XVECLEN (newpat, 0) == 2
2310 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2311 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2312 && asm_noperands (newpat) < 0)
2314 rtx set0 = XVECEXP (newpat, 0, 0);
2315 rtx set1 = XVECEXP (newpat, 0, 1);
2316 rtx note;
2318 if (((REG_P (SET_DEST (set1))
2319 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2320 || (GET_CODE (SET_DEST (set1)) == SUBREG
2321 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2322 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2323 || INTVAL (XEXP (note, 0)) <= 0)
2324 && ! side_effects_p (SET_SRC (set1)))
2326 newpat = set0;
2327 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2330 else if (((REG_P (SET_DEST (set0))
2331 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2332 || (GET_CODE (SET_DEST (set0)) == SUBREG
2333 && find_reg_note (i3, REG_UNUSED,
2334 SUBREG_REG (SET_DEST (set0)))))
2335 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2336 || INTVAL (XEXP (note, 0)) <= 0)
2337 && ! side_effects_p (SET_SRC (set0)))
2339 newpat = set1;
2340 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2342 if (insn_code_number >= 0)
2344 /* If we will be able to accept this, we have made a
2345 change to the destination of I3. This requires us to
2346 do a few adjustments. */
2348 PATTERN (i3) = newpat;
2349 adjust_for_new_dest (i3);
2354 /* If we were combining three insns and the result is a simple SET
2355 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2356 insns. There are two ways to do this. It can be split using a
2357 machine-specific method (like when you have an addition of a large
2358 constant) or by combine in the function find_split_point. */
2360 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2361 && asm_noperands (newpat) < 0)
2363 rtx m_split, *split;
2364 rtx ni2dest = i2dest;
2366 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2367 use I2DEST as a scratch register will help. In the latter case,
2368 convert I2DEST to the mode of the source of NEWPAT if we can. */
2370 m_split = split_insns (newpat, i3);
2372 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2373 inputs of NEWPAT. */
2375 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2376 possible to try that as a scratch reg. This would require adding
2377 more code to make it work though. */
2379 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2381 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2382 /* If I2DEST is a hard register or the only use of a pseudo,
2383 we can change its mode. */
2384 if (new_mode != GET_MODE (i2dest)
2385 && new_mode != VOIDmode
2386 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2387 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2388 REGNO (i2dest));
2390 m_split = split_insns (gen_rtx_PARALLEL
2391 (VOIDmode,
2392 gen_rtvec (2, newpat,
2393 gen_rtx_CLOBBER (VOIDmode,
2394 ni2dest))),
2395 i3);
2396 /* If the split with the mode-changed register didn't work, try
2397 the original register. */
2398 if (! m_split && ni2dest != i2dest)
2400 ni2dest = i2dest;
2401 m_split = split_insns (gen_rtx_PARALLEL
2402 (VOIDmode,
2403 gen_rtvec (2, newpat,
2404 gen_rtx_CLOBBER (VOIDmode,
2405 i2dest))),
2406 i3);
2410 /* If recog_for_combine has discarded clobbers, try to use them
2411 again for the split. */
2412 if (m_split == 0 && newpat_vec_with_clobbers)
2413 m_split
2414 = split_insns (gen_rtx_PARALLEL (VOIDmode,
2415 newpat_vec_with_clobbers), i3);
2417 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2419 m_split = PATTERN (m_split);
2420 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2421 if (insn_code_number >= 0)
2422 newpat = m_split;
2424 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2425 && (next_real_insn (i2) == i3
2426 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2428 rtx i2set, i3set;
2429 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2430 newi2pat = PATTERN (m_split);
2432 i3set = single_set (NEXT_INSN (m_split));
2433 i2set = single_set (m_split);
2435 /* In case we changed the mode of I2DEST, replace it in the
2436 pseudo-register table here. We can't do it above in case this
2437 code doesn't get executed and we do a split the other way. */
2439 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2440 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2442 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2444 /* If I2 or I3 has multiple SETs, we won't know how to track
2445 register status, so don't use these insns. If I2's destination
2446 is used between I2 and I3, we also can't use these insns. */
2448 if (i2_code_number >= 0 && i2set && i3set
2449 && (next_real_insn (i2) == i3
2450 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2451 insn_code_number = recog_for_combine (&newi3pat, i3,
2452 &new_i3_notes);
2453 if (insn_code_number >= 0)
2454 newpat = newi3pat;
2456 /* It is possible that both insns now set the destination of I3.
2457 If so, we must show an extra use of it. */
2459 if (insn_code_number >= 0)
2461 rtx new_i3_dest = SET_DEST (i3set);
2462 rtx new_i2_dest = SET_DEST (i2set);
2464 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2465 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2466 || GET_CODE (new_i3_dest) == SUBREG)
2467 new_i3_dest = XEXP (new_i3_dest, 0);
2469 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2470 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2471 || GET_CODE (new_i2_dest) == SUBREG)
2472 new_i2_dest = XEXP (new_i2_dest, 0);
2474 if (REG_P (new_i3_dest)
2475 && REG_P (new_i2_dest)
2476 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2477 REG_N_SETS (REGNO (new_i2_dest))++;
2481 /* If we can split it and use I2DEST, go ahead and see if that
2482 helps things be recognized. Verify that none of the registers
2483 are set between I2 and I3. */
2484 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2485 #ifdef HAVE_cc0
2486 && REG_P (i2dest)
2487 #endif
2488 /* We need I2DEST in the proper mode. If it is a hard register
2489 or the only use of a pseudo, we can change its mode.
2490 Make sure we don't change a hard register to have a mode that
2491 isn't valid for it, or change the number of registers. */
2492 && (GET_MODE (*split) == GET_MODE (i2dest)
2493 || GET_MODE (*split) == VOIDmode
2494 || can_change_dest_mode (i2dest, added_sets_2,
2495 GET_MODE (*split)))
2496 && (next_real_insn (i2) == i3
2497 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2498 /* We can't overwrite I2DEST if its value is still used by
2499 NEWPAT. */
2500 && ! reg_referenced_p (i2dest, newpat))
2502 rtx newdest = i2dest;
2503 enum rtx_code split_code = GET_CODE (*split);
2504 enum machine_mode split_mode = GET_MODE (*split);
2506 /* Get NEWDEST as a register in the proper mode. We have already
2507 validated that we can do this. */
2508 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2510 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2512 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2513 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2516 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2517 an ASHIFT. This can occur if it was inside a PLUS and hence
2518 appeared to be a memory address. This is a kludge. */
2519 if (split_code == MULT
2520 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2521 && INTVAL (XEXP (*split, 1)) > 0
2522 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2524 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2525 XEXP (*split, 0), GEN_INT (i)));
2526 /* Update split_code because we may not have a multiply
2527 anymore. */
2528 split_code = GET_CODE (*split);
2531 #ifdef INSN_SCHEDULING
2532 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2533 be written as a ZERO_EXTEND. */
2534 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
2536 #ifdef LOAD_EXTEND_OP
2537 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2538 what it really is. */
2539 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2540 == SIGN_EXTEND)
2541 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2542 SUBREG_REG (*split)));
2543 else
2544 #endif
2545 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2546 SUBREG_REG (*split)));
2548 #endif
2550 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2551 SUBST (*split, newdest);
2552 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2554 /* recog_for_combine might have added CLOBBERs to newi2pat.
2555 Make sure NEWPAT does not depend on the clobbered regs. */
2556 if (GET_CODE (newi2pat) == PARALLEL)
2557 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
2558 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
2560 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
2561 if (reg_overlap_mentioned_p (reg, newpat))
2563 undo_all ();
2564 return 0;
2568 /* If the split point was a MULT and we didn't have one before,
2569 don't use one now. */
2570 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2571 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2575 /* Check for a case where we loaded from memory in a narrow mode and
2576 then sign extended it, but we need both registers. In that case,
2577 we have a PARALLEL with both loads from the same memory location.
2578 We can split this into a load from memory followed by a register-register
2579 copy. This saves at least one insn, more if register allocation can
2580 eliminate the copy.
2582 We cannot do this if the destination of the first assignment is a
2583 condition code register or cc0. We eliminate this case by making sure
2584 the SET_DEST and SET_SRC have the same mode.
2586 We cannot do this if the destination of the second assignment is
2587 a register that we have already assumed is zero-extended. Similarly
2588 for a SUBREG of such a register. */
2590 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2591 && GET_CODE (newpat) == PARALLEL
2592 && XVECLEN (newpat, 0) == 2
2593 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2594 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2595 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2596 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2597 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2598 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2599 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2600 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2601 INSN_CUID (i2))
2602 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2603 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2604 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2605 (REG_P (temp)
2606 && reg_stat[REGNO (temp)].nonzero_bits != 0
2607 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2608 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2609 && (reg_stat[REGNO (temp)].nonzero_bits
2610 != GET_MODE_MASK (word_mode))))
2611 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2612 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2613 (REG_P (temp)
2614 && reg_stat[REGNO (temp)].nonzero_bits != 0
2615 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2616 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2617 && (reg_stat[REGNO (temp)].nonzero_bits
2618 != GET_MODE_MASK (word_mode)))))
2619 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2620 SET_SRC (XVECEXP (newpat, 0, 1)))
2621 && ! find_reg_note (i3, REG_UNUSED,
2622 SET_DEST (XVECEXP (newpat, 0, 0))))
2624 rtx ni2dest;
2626 newi2pat = XVECEXP (newpat, 0, 0);
2627 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2628 newpat = XVECEXP (newpat, 0, 1);
2629 SUBST (SET_SRC (newpat),
2630 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2631 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2633 if (i2_code_number >= 0)
2634 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2636 if (insn_code_number >= 0)
2637 swap_i2i3 = 1;
2640 /* Similarly, check for a case where we have a PARALLEL of two independent
2641 SETs but we started with three insns. In this case, we can do the sets
2642 as two separate insns. This case occurs when some SET allows two
2643 other insns to combine, but the destination of that SET is still live. */
2645 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2646 && GET_CODE (newpat) == PARALLEL
2647 && XVECLEN (newpat, 0) == 2
2648 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2649 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2650 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2651 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2652 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2653 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2654 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2655 INSN_CUID (i2))
2656 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2657 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2658 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2659 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2660 XVECEXP (newpat, 0, 0))
2661 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2662 XVECEXP (newpat, 0, 1))
2663 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2664 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2666 /* Normally, it doesn't matter which of the two is done first,
2667 but it does if one references cc0. In that case, it has to
2668 be first. */
2669 #ifdef HAVE_cc0
2670 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2672 newi2pat = XVECEXP (newpat, 0, 0);
2673 newpat = XVECEXP (newpat, 0, 1);
2675 else
2676 #endif
2678 newi2pat = XVECEXP (newpat, 0, 1);
2679 newpat = XVECEXP (newpat, 0, 0);
2682 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2684 if (i2_code_number >= 0)
2685 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2688 /* If it still isn't recognized, fail and change things back the way they
2689 were. */
2690 if ((insn_code_number < 0
2691 /* Is the result a reasonable ASM_OPERANDS? */
2692 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2694 undo_all ();
2695 return 0;
2698 /* If we had to change another insn, make sure it is valid also. */
2699 if (undobuf.other_insn)
2701 rtx other_pat = PATTERN (undobuf.other_insn);
2702 rtx new_other_notes;
2703 rtx note, next;
2705 CLEAR_HARD_REG_SET (newpat_used_regs);
2707 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2708 &new_other_notes);
2710 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2712 undo_all ();
2713 return 0;
2716 PATTERN (undobuf.other_insn) = other_pat;
2718 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2719 are still valid. Then add any non-duplicate notes added by
2720 recog_for_combine. */
2721 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2723 next = XEXP (note, 1);
2725 if (REG_NOTE_KIND (note) == REG_UNUSED
2726 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2728 if (REG_P (XEXP (note, 0)))
2729 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2731 remove_note (undobuf.other_insn, note);
2735 for (note = new_other_notes; note; note = XEXP (note, 1))
2736 if (REG_P (XEXP (note, 0)))
2737 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2739 distribute_notes (new_other_notes, undobuf.other_insn,
2740 undobuf.other_insn, NULL_RTX);
2742 #ifdef HAVE_cc0
2743 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2744 they are adjacent to each other or not. */
2746 rtx p = prev_nonnote_insn (i3);
2747 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
2748 && sets_cc0_p (newi2pat))
2750 undo_all ();
2751 return 0;
2754 #endif
2756 /* Only allow this combination if insn_rtx_costs reports that the
2757 replacement instructions are cheaper than the originals. */
2758 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat))
2760 undo_all ();
2761 return 0;
2764 /* We now know that we can do this combination. Merge the insns and
2765 update the status of registers and LOG_LINKS. */
2767 if (swap_i2i3)
2769 rtx insn;
2770 rtx link;
2771 rtx ni2dest;
2773 /* I3 now uses what used to be its destination and which is now
2774 I2's destination. This requires us to do a few adjustments. */
2775 PATTERN (i3) = newpat;
2776 adjust_for_new_dest (i3);
2778 /* We need a LOG_LINK from I3 to I2. But we used to have one,
2779 so we still will.
2781 However, some later insn might be using I2's dest and have
2782 a LOG_LINK pointing at I3. We must remove this link.
2783 The simplest way to remove the link is to point it at I1,
2784 which we know will be a NOTE. */
2786 /* newi2pat is usually a SET here; however, recog_for_combine might
2787 have added some clobbers. */
2788 if (GET_CODE (newi2pat) == PARALLEL)
2789 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
2790 else
2791 ni2dest = SET_DEST (newi2pat);
2793 for (insn = NEXT_INSN (i3);
2794 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2795 || insn != BB_HEAD (this_basic_block->next_bb));
2796 insn = NEXT_INSN (insn))
2798 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2800 for (link = LOG_LINKS (insn); link;
2801 link = XEXP (link, 1))
2802 if (XEXP (link, 0) == i3)
2803 XEXP (link, 0) = i1;
2805 break;
2811 rtx i3notes, i2notes, i1notes = 0;
2812 rtx i3links, i2links, i1links = 0;
2813 rtx midnotes = 0;
2814 unsigned int regno;
2816 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2817 clear them. */
2818 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2819 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2820 if (i1)
2821 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2823 /* Ensure that we do not have something that should not be shared but
2824 occurs multiple times in the new insns. Check this by first
2825 resetting all the `used' flags and then copying anything is shared. */
2827 reset_used_flags (i3notes);
2828 reset_used_flags (i2notes);
2829 reset_used_flags (i1notes);
2830 reset_used_flags (newpat);
2831 reset_used_flags (newi2pat);
2832 if (undobuf.other_insn)
2833 reset_used_flags (PATTERN (undobuf.other_insn));
2835 i3notes = copy_rtx_if_shared (i3notes);
2836 i2notes = copy_rtx_if_shared (i2notes);
2837 i1notes = copy_rtx_if_shared (i1notes);
2838 newpat = copy_rtx_if_shared (newpat);
2839 newi2pat = copy_rtx_if_shared (newi2pat);
2840 if (undobuf.other_insn)
2841 reset_used_flags (PATTERN (undobuf.other_insn));
2843 INSN_CODE (i3) = insn_code_number;
2844 PATTERN (i3) = newpat;
2846 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
2848 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2850 reset_used_flags (call_usage);
2851 call_usage = copy_rtx (call_usage);
2853 if (substed_i2)
2854 replace_rtx (call_usage, i2dest, i2src);
2856 if (substed_i1)
2857 replace_rtx (call_usage, i1dest, i1src);
2859 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2862 if (undobuf.other_insn)
2863 INSN_CODE (undobuf.other_insn) = other_code_number;
2865 /* We had one special case above where I2 had more than one set and
2866 we replaced a destination of one of those sets with the destination
2867 of I3. In that case, we have to update LOG_LINKS of insns later
2868 in this basic block. Note that this (expensive) case is rare.
2870 Also, in this case, we must pretend that all REG_NOTEs for I2
2871 actually came from I3, so that REG_UNUSED notes from I2 will be
2872 properly handled. */
2874 if (i3_subst_into_i2)
2876 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2877 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2878 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
2879 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2880 && ! find_reg_note (i2, REG_UNUSED,
2881 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2882 for (temp = NEXT_INSN (i2);
2883 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2884 || BB_HEAD (this_basic_block) != temp);
2885 temp = NEXT_INSN (temp))
2886 if (temp != i3 && INSN_P (temp))
2887 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2888 if (XEXP (link, 0) == i2)
2889 XEXP (link, 0) = i3;
2891 if (i3notes)
2893 rtx link = i3notes;
2894 while (XEXP (link, 1))
2895 link = XEXP (link, 1);
2896 XEXP (link, 1) = i2notes;
2898 else
2899 i3notes = i2notes;
2900 i2notes = 0;
2903 LOG_LINKS (i3) = 0;
2904 REG_NOTES (i3) = 0;
2905 LOG_LINKS (i2) = 0;
2906 REG_NOTES (i2) = 0;
2908 if (newi2pat)
2910 INSN_CODE (i2) = i2_code_number;
2911 PATTERN (i2) = newi2pat;
2913 else
2914 SET_INSN_DELETED (i2);
2916 if (i1)
2918 LOG_LINKS (i1) = 0;
2919 REG_NOTES (i1) = 0;
2920 SET_INSN_DELETED (i1);
2923 /* Get death notes for everything that is now used in either I3 or
2924 I2 and used to die in a previous insn. If we built two new
2925 patterns, move from I1 to I2 then I2 to I3 so that we get the
2926 proper movement on registers that I2 modifies. */
2928 if (newi2pat)
2930 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2931 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2933 else
2934 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2935 i3, &midnotes);
2937 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2938 if (i3notes)
2939 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2940 if (i2notes)
2941 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2942 if (i1notes)
2943 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2944 if (midnotes)
2945 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2947 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2948 know these are REG_UNUSED and want them to go to the desired insn,
2949 so we always pass it as i3. We have not counted the notes in
2950 reg_n_deaths yet, so we need to do so now. */
2952 if (newi2pat && new_i2_notes)
2954 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2955 if (REG_P (XEXP (temp, 0)))
2956 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2958 distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2961 if (new_i3_notes)
2963 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2964 if (REG_P (XEXP (temp, 0)))
2965 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2967 distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2970 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2971 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2972 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2973 in that case, it might delete I2. Similarly for I2 and I1.
2974 Show an additional death due to the REG_DEAD note we make here. If
2975 we discard it in distribute_notes, we will decrement it again. */
2977 if (i3dest_killed)
2979 if (REG_P (i3dest_killed))
2980 REG_N_DEATHS (REGNO (i3dest_killed))++;
2982 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2983 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2984 NULL_RTX),
2985 NULL_RTX, i2, NULL_RTX);
2986 else
2987 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2988 NULL_RTX),
2989 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2992 if (i2dest_in_i2src)
2994 if (REG_P (i2dest))
2995 REG_N_DEATHS (REGNO (i2dest))++;
2997 if (newi2pat && reg_set_p (i2dest, newi2pat))
2998 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2999 NULL_RTX, i2, NULL_RTX);
3000 else
3001 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3002 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
3005 if (i1dest_in_i1src)
3007 if (REG_P (i1dest))
3008 REG_N_DEATHS (REGNO (i1dest))++;
3010 if (newi2pat && reg_set_p (i1dest, newi2pat))
3011 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3012 NULL_RTX, i2, NULL_RTX);
3013 else
3014 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3015 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
3018 distribute_links (i3links);
3019 distribute_links (i2links);
3020 distribute_links (i1links);
3022 if (REG_P (i2dest))
3024 rtx link;
3025 rtx i2_insn = 0, i2_val = 0, set;
3027 /* The insn that used to set this register doesn't exist, and
3028 this life of the register may not exist either. See if one of
3029 I3's links points to an insn that sets I2DEST. If it does,
3030 that is now the last known value for I2DEST. If we don't update
3031 this and I2 set the register to a value that depended on its old
3032 contents, we will get confused. If this insn is used, thing
3033 will be set correctly in combine_instructions. */
3035 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3036 if ((set = single_set (XEXP (link, 0))) != 0
3037 && rtx_equal_p (i2dest, SET_DEST (set)))
3038 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3040 record_value_for_reg (i2dest, i2_insn, i2_val);
3042 /* If the reg formerly set in I2 died only once and that was in I3,
3043 zero its use count so it won't make `reload' do any work. */
3044 if (! added_sets_2
3045 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3046 && ! i2dest_in_i2src)
3048 regno = REGNO (i2dest);
3049 REG_N_SETS (regno)--;
3053 if (i1 && REG_P (i1dest))
3055 rtx link;
3056 rtx i1_insn = 0, i1_val = 0, set;
3058 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3059 if ((set = single_set (XEXP (link, 0))) != 0
3060 && rtx_equal_p (i1dest, SET_DEST (set)))
3061 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3063 record_value_for_reg (i1dest, i1_insn, i1_val);
3065 regno = REGNO (i1dest);
3066 if (! added_sets_1 && ! i1dest_in_i1src)
3067 REG_N_SETS (regno)--;
3070 /* Update reg_stat[].nonzero_bits et al for any changes that may have
3071 been made to this insn. The order of
3072 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
3073 can affect nonzero_bits of newpat */
3074 if (newi2pat)
3075 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3076 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3078 /* Set new_direct_jump_p if a new return or simple jump instruction
3079 has been created.
3081 If I3 is now an unconditional jump, ensure that it has a
3082 BARRIER following it since it may have initially been a
3083 conditional jump. It may also be the last nonnote insn. */
3085 if (returnjump_p (i3) || any_uncondjump_p (i3))
3087 *new_direct_jump_p = 1;
3088 mark_jump_label (PATTERN (i3), i3, 0);
3090 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3091 || !BARRIER_P (temp))
3092 emit_barrier_after (i3);
3095 if (undobuf.other_insn != NULL_RTX
3096 && (returnjump_p (undobuf.other_insn)
3097 || any_uncondjump_p (undobuf.other_insn)))
3099 *new_direct_jump_p = 1;
3101 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3102 || !BARRIER_P (temp))
3103 emit_barrier_after (undobuf.other_insn);
3106 /* An NOOP jump does not need barrier, but it does need cleaning up
3107 of CFG. */
3108 if (GET_CODE (newpat) == SET
3109 && SET_SRC (newpat) == pc_rtx
3110 && SET_DEST (newpat) == pc_rtx)
3111 *new_direct_jump_p = 1;
3114 combine_successes++;
3115 undo_commit ();
3117 if (added_links_insn
3118 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
3119 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
3120 return added_links_insn;
3121 else
3122 return newi2pat ? i2 : i3;
3125 /* Undo all the modifications recorded in undobuf. */
3127 static void
3128 undo_all (void)
3130 struct undo *undo, *next;
3132 for (undo = undobuf.undos; undo; undo = next)
3134 next = undo->next;
3135 if (undo->is_int)
3136 *undo->where.i = undo->old_contents.i;
3137 else
3138 *undo->where.r = undo->old_contents.r;
3140 undo->next = undobuf.frees;
3141 undobuf.frees = undo;
3144 undobuf.undos = 0;
3147 /* We've committed to accepting the changes we made. Move all
3148 of the undos to the free list. */
3150 static void
3151 undo_commit (void)
3153 struct undo *undo, *next;
3155 for (undo = undobuf.undos; undo; undo = next)
3157 next = undo->next;
3158 undo->next = undobuf.frees;
3159 undobuf.frees = undo;
3161 undobuf.undos = 0;
3165 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3166 where we have an arithmetic expression and return that point. LOC will
3167 be inside INSN.
3169 try_combine will call this function to see if an insn can be split into
3170 two insns. */
3172 static rtx *
3173 find_split_point (rtx *loc, rtx insn)
3175 rtx x = *loc;
3176 enum rtx_code code = GET_CODE (x);
3177 rtx *split;
3178 unsigned HOST_WIDE_INT len = 0;
3179 HOST_WIDE_INT pos = 0;
3180 int unsignedp = 0;
3181 rtx inner = NULL_RTX;
3183 /* First special-case some codes. */
3184 switch (code)
3186 case SUBREG:
3187 #ifdef INSN_SCHEDULING
3188 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3189 point. */
3190 if (MEM_P (SUBREG_REG (x)))
3191 return loc;
3192 #endif
3193 return find_split_point (&SUBREG_REG (x), insn);
3195 case MEM:
3196 #ifdef HAVE_lo_sum
3197 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3198 using LO_SUM and HIGH. */
3199 if (GET_CODE (XEXP (x, 0)) == CONST
3200 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3202 SUBST (XEXP (x, 0),
3203 gen_rtx_LO_SUM (Pmode,
3204 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3205 XEXP (x, 0)));
3206 return &XEXP (XEXP (x, 0), 0);
3208 #endif
3210 /* If we have a PLUS whose second operand is a constant and the
3211 address is not valid, perhaps will can split it up using
3212 the machine-specific way to split large constants. We use
3213 the first pseudo-reg (one of the virtual regs) as a placeholder;
3214 it will not remain in the result. */
3215 if (GET_CODE (XEXP (x, 0)) == PLUS
3216 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3217 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3219 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3220 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3221 subst_insn);
3223 /* This should have produced two insns, each of which sets our
3224 placeholder. If the source of the second is a valid address,
3225 we can make put both sources together and make a split point
3226 in the middle. */
3228 if (seq
3229 && NEXT_INSN (seq) != NULL_RTX
3230 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3231 && NONJUMP_INSN_P (seq)
3232 && GET_CODE (PATTERN (seq)) == SET
3233 && SET_DEST (PATTERN (seq)) == reg
3234 && ! reg_mentioned_p (reg,
3235 SET_SRC (PATTERN (seq)))
3236 && NONJUMP_INSN_P (NEXT_INSN (seq))
3237 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3238 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3239 && memory_address_p (GET_MODE (x),
3240 SET_SRC (PATTERN (NEXT_INSN (seq)))))
3242 rtx src1 = SET_SRC (PATTERN (seq));
3243 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3245 /* Replace the placeholder in SRC2 with SRC1. If we can
3246 find where in SRC2 it was placed, that can become our
3247 split point and we can replace this address with SRC2.
3248 Just try two obvious places. */
3250 src2 = replace_rtx (src2, reg, src1);
3251 split = 0;
3252 if (XEXP (src2, 0) == src1)
3253 split = &XEXP (src2, 0);
3254 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3255 && XEXP (XEXP (src2, 0), 0) == src1)
3256 split = &XEXP (XEXP (src2, 0), 0);
3258 if (split)
3260 SUBST (XEXP (x, 0), src2);
3261 return split;
3265 /* If that didn't work, perhaps the first operand is complex and
3266 needs to be computed separately, so make a split point there.
3267 This will occur on machines that just support REG + CONST
3268 and have a constant moved through some previous computation. */
3270 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3271 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3272 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3273 return &XEXP (XEXP (x, 0), 0);
3275 break;
3277 case SET:
3278 #ifdef HAVE_cc0
3279 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3280 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3281 we need to put the operand into a register. So split at that
3282 point. */
3284 if (SET_DEST (x) == cc0_rtx
3285 && GET_CODE (SET_SRC (x)) != COMPARE
3286 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3287 && !OBJECT_P (SET_SRC (x))
3288 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3289 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3290 return &SET_SRC (x);
3291 #endif
3293 /* See if we can split SET_SRC as it stands. */
3294 split = find_split_point (&SET_SRC (x), insn);
3295 if (split && split != &SET_SRC (x))
3296 return split;
3298 /* See if we can split SET_DEST as it stands. */
3299 split = find_split_point (&SET_DEST (x), insn);
3300 if (split && split != &SET_DEST (x))
3301 return split;
3303 /* See if this is a bitfield assignment with everything constant. If
3304 so, this is an IOR of an AND, so split it into that. */
3305 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3306 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3307 <= HOST_BITS_PER_WIDE_INT)
3308 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3309 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3310 && GET_CODE (SET_SRC (x)) == CONST_INT
3311 && ((INTVAL (XEXP (SET_DEST (x), 1))
3312 + INTVAL (XEXP (SET_DEST (x), 2)))
3313 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3314 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3316 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3317 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3318 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3319 rtx dest = XEXP (SET_DEST (x), 0);
3320 enum machine_mode mode = GET_MODE (dest);
3321 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3323 if (BITS_BIG_ENDIAN)
3324 pos = GET_MODE_BITSIZE (mode) - len - pos;
3326 if (src == mask)
3327 SUBST (SET_SRC (x),
3328 simplify_gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3329 else
3331 rtx negmask = gen_int_mode (~(mask << pos), mode);
3332 SUBST (SET_SRC (x),
3333 simplify_gen_binary (IOR, mode,
3334 simplify_gen_binary (AND, mode,
3335 dest, negmask),
3336 GEN_INT (src << pos)));
3339 SUBST (SET_DEST (x), dest);
3341 split = find_split_point (&SET_SRC (x), insn);
3342 if (split && split != &SET_SRC (x))
3343 return split;
3346 /* Otherwise, see if this is an operation that we can split into two.
3347 If so, try to split that. */
3348 code = GET_CODE (SET_SRC (x));
3350 switch (code)
3352 case AND:
3353 /* If we are AND'ing with a large constant that is only a single
3354 bit and the result is only being used in a context where we
3355 need to know if it is zero or nonzero, replace it with a bit
3356 extraction. This will avoid the large constant, which might
3357 have taken more than one insn to make. If the constant were
3358 not a valid argument to the AND but took only one insn to make,
3359 this is no worse, but if it took more than one insn, it will
3360 be better. */
3362 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3363 && REG_P (XEXP (SET_SRC (x), 0))
3364 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3365 && REG_P (SET_DEST (x))
3366 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3367 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3368 && XEXP (*split, 0) == SET_DEST (x)
3369 && XEXP (*split, 1) == const0_rtx)
3371 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3372 XEXP (SET_SRC (x), 0),
3373 pos, NULL_RTX, 1, 1, 0, 0);
3374 if (extraction != 0)
3376 SUBST (SET_SRC (x), extraction);
3377 return find_split_point (loc, insn);
3380 break;
3382 case NE:
3383 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3384 is known to be on, this can be converted into a NEG of a shift. */
3385 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3386 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3387 && 1 <= (pos = exact_log2
3388 (nonzero_bits (XEXP (SET_SRC (x), 0),
3389 GET_MODE (XEXP (SET_SRC (x), 0))))))
3391 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3393 SUBST (SET_SRC (x),
3394 gen_rtx_NEG (mode,
3395 gen_rtx_LSHIFTRT (mode,
3396 XEXP (SET_SRC (x), 0),
3397 GEN_INT (pos))));
3399 split = find_split_point (&SET_SRC (x), insn);
3400 if (split && split != &SET_SRC (x))
3401 return split;
3403 break;
3405 case SIGN_EXTEND:
3406 inner = XEXP (SET_SRC (x), 0);
3408 /* We can't optimize if either mode is a partial integer
3409 mode as we don't know how many bits are significant
3410 in those modes. */
3411 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3412 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3413 break;
3415 pos = 0;
3416 len = GET_MODE_BITSIZE (GET_MODE (inner));
3417 unsignedp = 0;
3418 break;
3420 case SIGN_EXTRACT:
3421 case ZERO_EXTRACT:
3422 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3423 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3425 inner = XEXP (SET_SRC (x), 0);
3426 len = INTVAL (XEXP (SET_SRC (x), 1));
3427 pos = INTVAL (XEXP (SET_SRC (x), 2));
3429 if (BITS_BIG_ENDIAN)
3430 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3431 unsignedp = (code == ZERO_EXTRACT);
3433 break;
3435 default:
3436 break;
3439 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3441 enum machine_mode mode = GET_MODE (SET_SRC (x));
3443 /* For unsigned, we have a choice of a shift followed by an
3444 AND or two shifts. Use two shifts for field sizes where the
3445 constant might be too large. We assume here that we can
3446 always at least get 8-bit constants in an AND insn, which is
3447 true for every current RISC. */
3449 if (unsignedp && len <= 8)
3451 SUBST (SET_SRC (x),
3452 gen_rtx_AND (mode,
3453 gen_rtx_LSHIFTRT
3454 (mode, gen_lowpart (mode, inner),
3455 GEN_INT (pos)),
3456 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3458 split = find_split_point (&SET_SRC (x), insn);
3459 if (split && split != &SET_SRC (x))
3460 return split;
3462 else
3464 SUBST (SET_SRC (x),
3465 gen_rtx_fmt_ee
3466 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3467 gen_rtx_ASHIFT (mode,
3468 gen_lowpart (mode, inner),
3469 GEN_INT (GET_MODE_BITSIZE (mode)
3470 - len - pos)),
3471 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3473 split = find_split_point (&SET_SRC (x), insn);
3474 if (split && split != &SET_SRC (x))
3475 return split;
3479 /* See if this is a simple operation with a constant as the second
3480 operand. It might be that this constant is out of range and hence
3481 could be used as a split point. */
3482 if (BINARY_P (SET_SRC (x))
3483 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3484 && (OBJECT_P (XEXP (SET_SRC (x), 0))
3485 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3486 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3487 return &XEXP (SET_SRC (x), 1);
3489 /* Finally, see if this is a simple operation with its first operand
3490 not in a register. The operation might require this operand in a
3491 register, so return it as a split point. We can always do this
3492 because if the first operand were another operation, we would have
3493 already found it as a split point. */
3494 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3495 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3496 return &XEXP (SET_SRC (x), 0);
3498 return 0;
3500 case AND:
3501 case IOR:
3502 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3503 it is better to write this as (not (ior A B)) so we can split it.
3504 Similarly for IOR. */
3505 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3507 SUBST (*loc,
3508 gen_rtx_NOT (GET_MODE (x),
3509 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3510 GET_MODE (x),
3511 XEXP (XEXP (x, 0), 0),
3512 XEXP (XEXP (x, 1), 0))));
3513 return find_split_point (loc, insn);
3516 /* Many RISC machines have a large set of logical insns. If the
3517 second operand is a NOT, put it first so we will try to split the
3518 other operand first. */
3519 if (GET_CODE (XEXP (x, 1)) == NOT)
3521 rtx tem = XEXP (x, 0);
3522 SUBST (XEXP (x, 0), XEXP (x, 1));
3523 SUBST (XEXP (x, 1), tem);
3525 break;
3527 default:
3528 break;
3531 /* Otherwise, select our actions depending on our rtx class. */
3532 switch (GET_RTX_CLASS (code))
3534 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3535 case RTX_TERNARY:
3536 split = find_split_point (&XEXP (x, 2), insn);
3537 if (split)
3538 return split;
3539 /* ... fall through ... */
3540 case RTX_BIN_ARITH:
3541 case RTX_COMM_ARITH:
3542 case RTX_COMPARE:
3543 case RTX_COMM_COMPARE:
3544 split = find_split_point (&XEXP (x, 1), insn);
3545 if (split)
3546 return split;
3547 /* ... fall through ... */
3548 case RTX_UNARY:
3549 /* Some machines have (and (shift ...) ...) insns. If X is not
3550 an AND, but XEXP (X, 0) is, use it as our split point. */
3551 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3552 return &XEXP (x, 0);
3554 split = find_split_point (&XEXP (x, 0), insn);
3555 if (split)
3556 return split;
3557 return loc;
3559 default:
3560 /* Otherwise, we don't have a split point. */
3561 return 0;
3565 /* Throughout X, replace FROM with TO, and return the result.
3566 The result is TO if X is FROM;
3567 otherwise the result is X, but its contents may have been modified.
3568 If they were modified, a record was made in undobuf so that
3569 undo_all will (among other things) return X to its original state.
3571 If the number of changes necessary is too much to record to undo,
3572 the excess changes are not made, so the result is invalid.
3573 The changes already made can still be undone.
3574 undobuf.num_undo is incremented for such changes, so by testing that
3575 the caller can tell whether the result is valid.
3577 `n_occurrences' is incremented each time FROM is replaced.
3579 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3581 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3582 by copying if `n_occurrences' is nonzero. */
3584 static rtx
3585 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3587 enum rtx_code code = GET_CODE (x);
3588 enum machine_mode op0_mode = VOIDmode;
3589 const char *fmt;
3590 int len, i;
3591 rtx new;
3593 /* Two expressions are equal if they are identical copies of a shared
3594 RTX or if they are both registers with the same register number
3595 and mode. */
3597 #define COMBINE_RTX_EQUAL_P(X,Y) \
3598 ((X) == (Y) \
3599 || (REG_P (X) && REG_P (Y) \
3600 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3602 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3604 n_occurrences++;
3605 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3608 /* If X and FROM are the same register but different modes, they will
3609 not have been seen as equal above. However, flow.c will make a
3610 LOG_LINKS entry for that case. If we do nothing, we will try to
3611 rerecognize our original insn and, when it succeeds, we will
3612 delete the feeding insn, which is incorrect.
3614 So force this insn not to match in this (rare) case. */
3615 if (! in_dest && code == REG && REG_P (from)
3616 && REGNO (x) == REGNO (from))
3617 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3619 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3620 of which may contain things that can be combined. */
3621 if (code != MEM && code != LO_SUM && OBJECT_P (x))
3622 return x;
3624 /* It is possible to have a subexpression appear twice in the insn.
3625 Suppose that FROM is a register that appears within TO.
3626 Then, after that subexpression has been scanned once by `subst',
3627 the second time it is scanned, TO may be found. If we were
3628 to scan TO here, we would find FROM within it and create a
3629 self-referent rtl structure which is completely wrong. */
3630 if (COMBINE_RTX_EQUAL_P (x, to))
3631 return to;
3633 /* Parallel asm_operands need special attention because all of the
3634 inputs are shared across the arms. Furthermore, unsharing the
3635 rtl results in recognition failures. Failure to handle this case
3636 specially can result in circular rtl.
3638 Solve this by doing a normal pass across the first entry of the
3639 parallel, and only processing the SET_DESTs of the subsequent
3640 entries. Ug. */
3642 if (code == PARALLEL
3643 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3644 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3646 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3648 /* If this substitution failed, this whole thing fails. */
3649 if (GET_CODE (new) == CLOBBER
3650 && XEXP (new, 0) == const0_rtx)
3651 return new;
3653 SUBST (XVECEXP (x, 0, 0), new);
3655 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3657 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3659 if (!REG_P (dest)
3660 && GET_CODE (dest) != CC0
3661 && GET_CODE (dest) != PC)
3663 new = subst (dest, from, to, 0, unique_copy);
3665 /* If this substitution failed, this whole thing fails. */
3666 if (GET_CODE (new) == CLOBBER
3667 && XEXP (new, 0) == const0_rtx)
3668 return new;
3670 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3674 else
3676 len = GET_RTX_LENGTH (code);
3677 fmt = GET_RTX_FORMAT (code);
3679 /* We don't need to process a SET_DEST that is a register, CC0,
3680 or PC, so set up to skip this common case. All other cases
3681 where we want to suppress replacing something inside a
3682 SET_SRC are handled via the IN_DEST operand. */
3683 if (code == SET
3684 && (REG_P (SET_DEST (x))
3685 || GET_CODE (SET_DEST (x)) == CC0
3686 || GET_CODE (SET_DEST (x)) == PC))
3687 fmt = "ie";
3689 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3690 constant. */
3691 if (fmt[0] == 'e')
3692 op0_mode = GET_MODE (XEXP (x, 0));
3694 for (i = 0; i < len; i++)
3696 if (fmt[i] == 'E')
3698 int j;
3699 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3701 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3703 new = (unique_copy && n_occurrences
3704 ? copy_rtx (to) : to);
3705 n_occurrences++;
3707 else
3709 new = subst (XVECEXP (x, i, j), from, to, 0,
3710 unique_copy);
3712 /* If this substitution failed, this whole thing
3713 fails. */
3714 if (GET_CODE (new) == CLOBBER
3715 && XEXP (new, 0) == const0_rtx)
3716 return new;
3719 SUBST (XVECEXP (x, i, j), new);
3722 else if (fmt[i] == 'e')
3724 /* If this is a register being set, ignore it. */
3725 new = XEXP (x, i);
3726 if (in_dest
3727 && i == 0
3728 && (((code == SUBREG || code == ZERO_EXTRACT)
3729 && REG_P (new))
3730 || code == STRICT_LOW_PART))
3733 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3735 /* In general, don't install a subreg involving two
3736 modes not tieable. It can worsen register
3737 allocation, and can even make invalid reload
3738 insns, since the reg inside may need to be copied
3739 from in the outside mode, and that may be invalid
3740 if it is an fp reg copied in integer mode.
3742 We allow two exceptions to this: It is valid if
3743 it is inside another SUBREG and the mode of that
3744 SUBREG and the mode of the inside of TO is
3745 tieable and it is valid if X is a SET that copies
3746 FROM to CC0. */
3748 if (GET_CODE (to) == SUBREG
3749 && ! MODES_TIEABLE_P (GET_MODE (to),
3750 GET_MODE (SUBREG_REG (to)))
3751 && ! (code == SUBREG
3752 && MODES_TIEABLE_P (GET_MODE (x),
3753 GET_MODE (SUBREG_REG (to))))
3754 #ifdef HAVE_cc0
3755 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3756 #endif
3758 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3760 #ifdef CANNOT_CHANGE_MODE_CLASS
3761 if (code == SUBREG
3762 && REG_P (to)
3763 && REGNO (to) < FIRST_PSEUDO_REGISTER
3764 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3765 GET_MODE (to),
3766 GET_MODE (x)))
3767 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3768 #endif
3770 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3771 n_occurrences++;
3773 else
3774 /* If we are in a SET_DEST, suppress most cases unless we
3775 have gone inside a MEM, in which case we want to
3776 simplify the address. We assume here that things that
3777 are actually part of the destination have their inner
3778 parts in the first expression. This is true for SUBREG,
3779 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3780 things aside from REG and MEM that should appear in a
3781 SET_DEST. */
3782 new = subst (XEXP (x, i), from, to,
3783 (((in_dest
3784 && (code == SUBREG || code == STRICT_LOW_PART
3785 || code == ZERO_EXTRACT))
3786 || code == SET)
3787 && i == 0), unique_copy);
3789 /* If we found that we will have to reject this combination,
3790 indicate that by returning the CLOBBER ourselves, rather than
3791 an expression containing it. This will speed things up as
3792 well as prevent accidents where two CLOBBERs are considered
3793 to be equal, thus producing an incorrect simplification. */
3795 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3796 return new;
3798 if (GET_CODE (x) == SUBREG
3799 && (GET_CODE (new) == CONST_INT
3800 || GET_CODE (new) == CONST_DOUBLE))
3802 enum machine_mode mode = GET_MODE (x);
3804 x = simplify_subreg (GET_MODE (x), new,
3805 GET_MODE (SUBREG_REG (x)),
3806 SUBREG_BYTE (x));
3807 if (! x)
3808 x = gen_rtx_CLOBBER (mode, const0_rtx);
3810 else if (GET_CODE (new) == CONST_INT
3811 && GET_CODE (x) == ZERO_EXTEND)
3813 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3814 new, GET_MODE (XEXP (x, 0)));
3815 gcc_assert (x);
3817 else
3818 SUBST (XEXP (x, i), new);
3823 /* Try to simplify X. If the simplification changed the code, it is likely
3824 that further simplification will help, so loop, but limit the number
3825 of repetitions that will be performed. */
3827 for (i = 0; i < 4; i++)
3829 /* If X is sufficiently simple, don't bother trying to do anything
3830 with it. */
3831 if (code != CONST_INT && code != REG && code != CLOBBER)
3832 x = combine_simplify_rtx (x, op0_mode, in_dest);
3834 if (GET_CODE (x) == code)
3835 break;
3837 code = GET_CODE (x);
3839 /* We no longer know the original mode of operand 0 since we
3840 have changed the form of X) */
3841 op0_mode = VOIDmode;
3844 return x;
3847 /* Simplify X, a piece of RTL. We just operate on the expression at the
3848 outer level; call `subst' to simplify recursively. Return the new
3849 expression.
3851 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
3852 if we are inside a SET_DEST. */
3854 static rtx
3855 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3857 enum rtx_code code = GET_CODE (x);
3858 enum machine_mode mode = GET_MODE (x);
3859 rtx temp;
3860 rtx reversed;
3861 int i;
3863 /* If this is a commutative operation, put a constant last and a complex
3864 expression first. We don't need to do this for comparisons here. */
3865 if (COMMUTATIVE_ARITH_P (x)
3866 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3868 temp = XEXP (x, 0);
3869 SUBST (XEXP (x, 0), XEXP (x, 1));
3870 SUBST (XEXP (x, 1), temp);
3873 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3874 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3875 things. Check for cases where both arms are testing the same
3876 condition.
3878 Don't do anything if all operands are very simple. */
3880 if ((BINARY_P (x)
3881 && ((!OBJECT_P (XEXP (x, 0))
3882 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3883 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3884 || (!OBJECT_P (XEXP (x, 1))
3885 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3886 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3887 || (UNARY_P (x)
3888 && (!OBJECT_P (XEXP (x, 0))
3889 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3890 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3892 rtx cond, true_rtx, false_rtx;
3894 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3895 if (cond != 0
3896 /* If everything is a comparison, what we have is highly unlikely
3897 to be simpler, so don't use it. */
3898 && ! (COMPARISON_P (x)
3899 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
3901 rtx cop1 = const0_rtx;
3902 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3904 if (cond_code == NE && COMPARISON_P (cond))
3905 return x;
3907 /* Simplify the alternative arms; this may collapse the true and
3908 false arms to store-flag values. Be careful to use copy_rtx
3909 here since true_rtx or false_rtx might share RTL with x as a
3910 result of the if_then_else_cond call above. */
3911 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3912 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3914 /* If true_rtx and false_rtx are not general_operands, an if_then_else
3915 is unlikely to be simpler. */
3916 if (general_operand (true_rtx, VOIDmode)
3917 && general_operand (false_rtx, VOIDmode))
3919 enum rtx_code reversed;
3921 /* Restarting if we generate a store-flag expression will cause
3922 us to loop. Just drop through in this case. */
3924 /* If the result values are STORE_FLAG_VALUE and zero, we can
3925 just make the comparison operation. */
3926 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3927 x = simplify_gen_relational (cond_code, mode, VOIDmode,
3928 cond, cop1);
3929 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3930 && ((reversed = reversed_comparison_code_parts
3931 (cond_code, cond, cop1, NULL))
3932 != UNKNOWN))
3933 x = simplify_gen_relational (reversed, mode, VOIDmode,
3934 cond, cop1);
3936 /* Likewise, we can make the negate of a comparison operation
3937 if the result values are - STORE_FLAG_VALUE and zero. */
3938 else if (GET_CODE (true_rtx) == CONST_INT
3939 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3940 && false_rtx == const0_rtx)
3941 x = simplify_gen_unary (NEG, mode,
3942 simplify_gen_relational (cond_code,
3943 mode, VOIDmode,
3944 cond, cop1),
3945 mode);
3946 else if (GET_CODE (false_rtx) == CONST_INT
3947 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3948 && true_rtx == const0_rtx
3949 && ((reversed = reversed_comparison_code_parts
3950 (cond_code, cond, cop1, NULL))
3951 != UNKNOWN))
3952 x = simplify_gen_unary (NEG, mode,
3953 simplify_gen_relational (reversed,
3954 mode, VOIDmode,
3955 cond, cop1),
3956 mode);
3957 else
3958 return gen_rtx_IF_THEN_ELSE (mode,
3959 simplify_gen_relational (cond_code,
3960 mode,
3961 VOIDmode,
3962 cond,
3963 cop1),
3964 true_rtx, false_rtx);
3966 code = GET_CODE (x);
3967 op0_mode = VOIDmode;
3972 /* Try to fold this expression in case we have constants that weren't
3973 present before. */
3974 temp = 0;
3975 switch (GET_RTX_CLASS (code))
3977 case RTX_UNARY:
3978 if (op0_mode == VOIDmode)
3979 op0_mode = GET_MODE (XEXP (x, 0));
3980 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3981 break;
3982 case RTX_COMPARE:
3983 case RTX_COMM_COMPARE:
3985 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3986 if (cmp_mode == VOIDmode)
3988 cmp_mode = GET_MODE (XEXP (x, 1));
3989 if (cmp_mode == VOIDmode)
3990 cmp_mode = op0_mode;
3992 temp = simplify_relational_operation (code, mode, cmp_mode,
3993 XEXP (x, 0), XEXP (x, 1));
3995 break;
3996 case RTX_COMM_ARITH:
3997 case RTX_BIN_ARITH:
3998 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3999 break;
4000 case RTX_BITFIELD_OPS:
4001 case RTX_TERNARY:
4002 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4003 XEXP (x, 1), XEXP (x, 2));
4004 break;
4005 default:
4006 break;
4009 if (temp)
4011 x = temp;
4012 code = GET_CODE (temp);
4013 op0_mode = VOIDmode;
4014 mode = GET_MODE (temp);
4017 /* First see if we can apply the inverse distributive law. */
4018 if (code == PLUS || code == MINUS
4019 || code == AND || code == IOR || code == XOR)
4021 x = apply_distributive_law (x);
4022 code = GET_CODE (x);
4023 op0_mode = VOIDmode;
4026 /* If CODE is an associative operation not otherwise handled, see if we
4027 can associate some operands. This can win if they are constants or
4028 if they are logically related (i.e. (a & b) & a). */
4029 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4030 || code == AND || code == IOR || code == XOR
4031 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4032 && ((INTEGRAL_MODE_P (mode) && code != DIV)
4033 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
4035 if (GET_CODE (XEXP (x, 0)) == code)
4037 rtx other = XEXP (XEXP (x, 0), 0);
4038 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4039 rtx inner_op1 = XEXP (x, 1);
4040 rtx inner;
4042 /* Make sure we pass the constant operand if any as the second
4043 one if this is a commutative operation. */
4044 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4046 rtx tem = inner_op0;
4047 inner_op0 = inner_op1;
4048 inner_op1 = tem;
4050 inner = simplify_binary_operation (code == MINUS ? PLUS
4051 : code == DIV ? MULT
4052 : code,
4053 mode, inner_op0, inner_op1);
4055 /* For commutative operations, try the other pair if that one
4056 didn't simplify. */
4057 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4059 other = XEXP (XEXP (x, 0), 1);
4060 inner = simplify_binary_operation (code, mode,
4061 XEXP (XEXP (x, 0), 0),
4062 XEXP (x, 1));
4065 if (inner)
4066 return simplify_gen_binary (code, mode, other, inner);
4070 /* A little bit of algebraic simplification here. */
4071 switch (code)
4073 case MEM:
4074 /* Ensure that our address has any ASHIFTs converted to MULT in case
4075 address-recognizing predicates are called later. */
4076 temp = make_compound_operation (XEXP (x, 0), MEM);
4077 SUBST (XEXP (x, 0), temp);
4078 break;
4080 case SUBREG:
4081 if (op0_mode == VOIDmode)
4082 op0_mode = GET_MODE (SUBREG_REG (x));
4084 /* See if this can be moved to simplify_subreg. */
4085 if (CONSTANT_P (SUBREG_REG (x))
4086 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4087 /* Don't call gen_lowpart if the inner mode
4088 is VOIDmode and we cannot simplify it, as SUBREG without
4089 inner mode is invalid. */
4090 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4091 || gen_lowpart_common (mode, SUBREG_REG (x))))
4092 return gen_lowpart (mode, SUBREG_REG (x));
4094 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4095 break;
4097 rtx temp;
4098 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4099 SUBREG_BYTE (x));
4100 if (temp)
4101 return temp;
4104 /* Don't change the mode of the MEM if that would change the meaning
4105 of the address. */
4106 if (MEM_P (SUBREG_REG (x))
4107 && (MEM_VOLATILE_P (SUBREG_REG (x))
4108 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4109 return gen_rtx_CLOBBER (mode, const0_rtx);
4111 /* Note that we cannot do any narrowing for non-constants since
4112 we might have been counting on using the fact that some bits were
4113 zero. We now do this in the SET. */
4115 break;
4117 case NOT:
4118 if (GET_CODE (XEXP (x, 0)) == SUBREG
4119 && subreg_lowpart_p (XEXP (x, 0))
4120 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
4121 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
4122 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
4123 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
4125 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
4127 x = gen_rtx_ROTATE (inner_mode,
4128 simplify_gen_unary (NOT, inner_mode, const1_rtx,
4129 inner_mode),
4130 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
4131 return gen_lowpart (mode, x);
4134 /* Apply De Morgan's laws to reduce number of patterns for machines
4135 with negating logical insns (and-not, nand, etc.). If result has
4136 only one NOT, put it first, since that is how the patterns are
4137 coded. */
4139 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
4141 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
4142 enum machine_mode op_mode;
4144 op_mode = GET_MODE (in1);
4145 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
4147 op_mode = GET_MODE (in2);
4148 if (op_mode == VOIDmode)
4149 op_mode = mode;
4150 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
4152 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
4154 rtx tem = in2;
4155 in2 = in1; in1 = tem;
4158 return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
4159 mode, in1, in2);
4161 break;
4163 case NEG:
4164 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
4165 if (GET_CODE (XEXP (x, 0)) == XOR
4166 && XEXP (XEXP (x, 0), 1) == const1_rtx
4167 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
4168 return simplify_gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4169 constm1_rtx);
4171 temp = expand_compound_operation (XEXP (x, 0));
4173 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4174 replaced by (lshiftrt X C). This will convert
4175 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4177 if (GET_CODE (temp) == ASHIFTRT
4178 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4179 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4180 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4181 INTVAL (XEXP (temp, 1)));
4183 /* If X has only a single bit that might be nonzero, say, bit I, convert
4184 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4185 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4186 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4187 or a SUBREG of one since we'd be making the expression more
4188 complex if it was just a register. */
4190 if (!REG_P (temp)
4191 && ! (GET_CODE (temp) == SUBREG
4192 && REG_P (SUBREG_REG (temp)))
4193 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4195 rtx temp1 = simplify_shift_const
4196 (NULL_RTX, ASHIFTRT, mode,
4197 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4198 GET_MODE_BITSIZE (mode) - 1 - i),
4199 GET_MODE_BITSIZE (mode) - 1 - i);
4201 /* If all we did was surround TEMP with the two shifts, we
4202 haven't improved anything, so don't use it. Otherwise,
4203 we are better off with TEMP1. */
4204 if (GET_CODE (temp1) != ASHIFTRT
4205 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4206 || XEXP (XEXP (temp1, 0), 0) != temp)
4207 return temp1;
4209 break;
4211 case TRUNCATE:
4212 /* We can't handle truncation to a partial integer mode here
4213 because we don't know the real bitsize of the partial
4214 integer mode. */
4215 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4216 break;
4218 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4219 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4220 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4221 SUBST (XEXP (x, 0),
4222 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4223 GET_MODE_MASK (mode), NULL_RTX, 0));
4225 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
4226 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4227 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4228 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4229 return XEXP (XEXP (x, 0), 0);
4231 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4232 (OP:SI foo:SI) if OP is NEG or ABS. */
4233 if ((GET_CODE (XEXP (x, 0)) == ABS
4234 || GET_CODE (XEXP (x, 0)) == NEG)
4235 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4236 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4237 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4238 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4239 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4241 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4242 (truncate:SI x). */
4243 if (GET_CODE (XEXP (x, 0)) == SUBREG
4244 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4245 && subreg_lowpart_p (XEXP (x, 0)))
4246 return SUBREG_REG (XEXP (x, 0));
4248 /* If we know that the value is already truncated, we can
4249 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4250 is nonzero for the corresponding modes. But don't do this
4251 for an (LSHIFTRT (MULT ...)) since this will cause problems
4252 with the umulXi3_highpart patterns. */
4253 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4254 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4255 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4256 >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4257 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4258 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4259 return gen_lowpart (mode, XEXP (x, 0));
4261 /* A truncate of a comparison can be replaced with a subreg if
4262 STORE_FLAG_VALUE permits. This is like the previous test,
4263 but it works even if the comparison is done in a mode larger
4264 than HOST_BITS_PER_WIDE_INT. */
4265 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4266 && COMPARISON_P (XEXP (x, 0))
4267 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4268 return gen_lowpart (mode, XEXP (x, 0));
4270 /* Similarly, a truncate of a register whose value is a
4271 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4272 permits. */
4273 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4274 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4275 && (temp = get_last_value (XEXP (x, 0)))
4276 && COMPARISON_P (temp))
4277 return gen_lowpart (mode, XEXP (x, 0));
4279 break;
4281 case FLOAT_TRUNCATE:
4282 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4283 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4284 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4285 return XEXP (XEXP (x, 0), 0);
4287 /* (float_truncate:SF (float_truncate:DF foo:XF))
4288 = (float_truncate:SF foo:XF).
4289 This may eliminate double rounding, so it is unsafe.
4291 (float_truncate:SF (float_extend:XF foo:DF))
4292 = (float_truncate:SF foo:DF).
4294 (float_truncate:DF (float_extend:XF foo:SF))
4295 = (float_extend:SF foo:DF). */
4296 if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4297 && flag_unsafe_math_optimizations)
4298 || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4299 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4300 0)))
4301 > GET_MODE_SIZE (mode)
4302 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4303 mode,
4304 XEXP (XEXP (x, 0), 0), mode);
4306 /* (float_truncate (float x)) is (float x) */
4307 if (GET_CODE (XEXP (x, 0)) == FLOAT
4308 && (flag_unsafe_math_optimizations
4309 || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4310 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4311 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4312 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4313 return simplify_gen_unary (FLOAT, mode,
4314 XEXP (XEXP (x, 0), 0),
4315 GET_MODE (XEXP (XEXP (x, 0), 0)));
4317 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4318 (OP:SF foo:SF) if OP is NEG or ABS. */
4319 if ((GET_CODE (XEXP (x, 0)) == ABS
4320 || GET_CODE (XEXP (x, 0)) == NEG)
4321 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4322 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4323 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4324 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4326 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4327 is (float_truncate:SF x). */
4328 if (GET_CODE (XEXP (x, 0)) == SUBREG
4329 && subreg_lowpart_p (XEXP (x, 0))
4330 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4331 return SUBREG_REG (XEXP (x, 0));
4332 break;
4333 case FLOAT_EXTEND:
4334 /* (float_extend (float_extend x)) is (float_extend x)
4336 (float_extend (float x)) is (float x) assuming that double
4337 rounding can't happen.
4339 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4340 || (GET_CODE (XEXP (x, 0)) == FLOAT
4341 && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4342 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4343 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4344 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4345 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4346 XEXP (XEXP (x, 0), 0),
4347 GET_MODE (XEXP (XEXP (x, 0), 0)));
4349 break;
4350 #ifdef HAVE_cc0
4351 case COMPARE:
4352 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4353 using cc0, in which case we want to leave it as a COMPARE
4354 so we can distinguish it from a register-register-copy. */
4355 if (XEXP (x, 1) == const0_rtx)
4356 return XEXP (x, 0);
4358 /* x - 0 is the same as x unless x's mode has signed zeros and
4359 allows rounding towards -infinity. Under those conditions,
4360 0 - 0 is -0. */
4361 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4362 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4363 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4364 return XEXP (x, 0);
4365 break;
4366 #endif
4368 case CONST:
4369 /* (const (const X)) can become (const X). Do it this way rather than
4370 returning the inner CONST since CONST can be shared with a
4371 REG_EQUAL note. */
4372 if (GET_CODE (XEXP (x, 0)) == CONST)
4373 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4374 break;
4376 #ifdef HAVE_lo_sum
4377 case LO_SUM:
4378 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4379 can add in an offset. find_split_point will split this address up
4380 again if it doesn't match. */
4381 if (GET_CODE (XEXP (x, 0)) == HIGH
4382 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4383 return XEXP (x, 1);
4384 break;
4385 #endif
4387 case PLUS:
4388 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4390 if (GET_CODE (XEXP (x, 0)) == MULT
4391 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4393 rtx in1, in2;
4395 in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4396 in2 = XEXP (XEXP (x, 0), 1);
4397 return simplify_gen_binary (MINUS, mode, XEXP (x, 1),
4398 simplify_gen_binary (MULT, mode,
4399 in1, in2));
4402 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4403 outermost. That's because that's the way indexed addresses are
4404 supposed to appear. This code used to check many more cases, but
4405 they are now checked elsewhere. */
4406 if (GET_CODE (XEXP (x, 0)) == PLUS
4407 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4408 return simplify_gen_binary (PLUS, mode,
4409 simplify_gen_binary (PLUS, mode,
4410 XEXP (XEXP (x, 0), 0),
4411 XEXP (x, 1)),
4412 XEXP (XEXP (x, 0), 1));
4414 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4415 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4416 bit-field and can be replaced by either a sign_extend or a
4417 sign_extract. The `and' may be a zero_extend and the two
4418 <c>, -<c> constants may be reversed. */
4419 if (GET_CODE (XEXP (x, 0)) == XOR
4420 && GET_CODE (XEXP (x, 1)) == CONST_INT
4421 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4422 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4423 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4424 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4425 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4426 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4427 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4428 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4429 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4430 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4431 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4432 == (unsigned int) i + 1))))
4433 return simplify_shift_const
4434 (NULL_RTX, ASHIFTRT, mode,
4435 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4436 XEXP (XEXP (XEXP (x, 0), 0), 0),
4437 GET_MODE_BITSIZE (mode) - (i + 1)),
4438 GET_MODE_BITSIZE (mode) - (i + 1));
4440 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4441 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4442 is 1. This produces better code than the alternative immediately
4443 below. */
4444 if (COMPARISON_P (XEXP (x, 0))
4445 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4446 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4447 && (reversed = reversed_comparison (XEXP (x, 0), mode)))
4448 return
4449 simplify_gen_unary (NEG, mode, reversed, mode);
4451 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4452 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4453 the bitsize of the mode - 1. This allows simplification of
4454 "a = (b & 8) == 0;" */
4455 if (XEXP (x, 1) == constm1_rtx
4456 && !REG_P (XEXP (x, 0))
4457 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4458 && REG_P (SUBREG_REG (XEXP (x, 0))))
4459 && nonzero_bits (XEXP (x, 0), mode) == 1)
4460 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4461 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4462 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4463 GET_MODE_BITSIZE (mode) - 1),
4464 GET_MODE_BITSIZE (mode) - 1);
4466 /* If we are adding two things that have no bits in common, convert
4467 the addition into an IOR. This will often be further simplified,
4468 for example in cases like ((a & 1) + (a & 2)), which can
4469 become a & 3. */
4471 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4472 && (nonzero_bits (XEXP (x, 0), mode)
4473 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4475 /* Try to simplify the expression further. */
4476 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4477 temp = combine_simplify_rtx (tor, mode, in_dest);
4479 /* If we could, great. If not, do not go ahead with the IOR
4480 replacement, since PLUS appears in many special purpose
4481 address arithmetic instructions. */
4482 if (GET_CODE (temp) != CLOBBER && temp != tor)
4483 return temp;
4485 break;
4487 case MINUS:
4488 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4489 by reversing the comparison code if valid. */
4490 if (STORE_FLAG_VALUE == 1
4491 && XEXP (x, 0) == const1_rtx
4492 && COMPARISON_P (XEXP (x, 1))
4493 && (reversed = reversed_comparison (XEXP (x, 1), mode)))
4494 return reversed;
4496 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4497 (and <foo> (const_int pow2-1)) */
4498 if (GET_CODE (XEXP (x, 1)) == AND
4499 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4500 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4501 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4502 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4503 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4505 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4507 if (GET_CODE (XEXP (x, 1)) == MULT
4508 && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4510 rtx in1, in2;
4512 in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4513 in2 = XEXP (XEXP (x, 1), 1);
4514 return simplify_gen_binary (PLUS, mode,
4515 simplify_gen_binary (MULT, mode,
4516 in1, in2),
4517 XEXP (x, 0));
4520 /* Canonicalize (minus (neg A) (mult B C)) to
4521 (minus (mult (neg B) C) A). */
4522 if (GET_CODE (XEXP (x, 1)) == MULT
4523 && GET_CODE (XEXP (x, 0)) == NEG)
4525 rtx in1, in2;
4527 in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4528 in2 = XEXP (XEXP (x, 1), 1);
4529 return simplify_gen_binary (MINUS, mode,
4530 simplify_gen_binary (MULT, mode,
4531 in1, in2),
4532 XEXP (XEXP (x, 0), 0));
4535 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4536 integers. */
4537 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4538 return simplify_gen_binary (MINUS, mode,
4539 simplify_gen_binary (MINUS, mode,
4540 XEXP (x, 0),
4541 XEXP (XEXP (x, 1), 0)),
4542 XEXP (XEXP (x, 1), 1));
4543 break;
4545 case MULT:
4546 /* If we have (mult (plus A B) C), apply the distributive law and then
4547 the inverse distributive law to see if things simplify. This
4548 occurs mostly in addresses, often when unrolling loops. */
4550 if (GET_CODE (XEXP (x, 0)) == PLUS)
4552 rtx result = distribute_and_simplify_rtx (x, 0);
4553 if (result)
4554 return result;
4557 /* Try simplify a*(b/c) as (a*b)/c. */
4558 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4559 && GET_CODE (XEXP (x, 0)) == DIV)
4561 rtx tem = simplify_binary_operation (MULT, mode,
4562 XEXP (XEXP (x, 0), 0),
4563 XEXP (x, 1));
4564 if (tem)
4565 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4567 break;
4569 case UDIV:
4570 /* If this is a divide by a power of two, treat it as a shift if
4571 its first operand is a shift. */
4572 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4573 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4574 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4575 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4576 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4577 || GET_CODE (XEXP (x, 0)) == ROTATE
4578 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4579 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4580 break;
4582 case EQ: case NE:
4583 case GT: case GTU: case GE: case GEU:
4584 case LT: case LTU: case LE: case LEU:
4585 case UNEQ: case LTGT:
4586 case UNGT: case UNGE:
4587 case UNLT: case UNLE:
4588 case UNORDERED: case ORDERED:
4589 /* If the first operand is a condition code, we can't do anything
4590 with it. */
4591 if (GET_CODE (XEXP (x, 0)) == COMPARE
4592 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4593 && ! CC0_P (XEXP (x, 0))))
4595 rtx op0 = XEXP (x, 0);
4596 rtx op1 = XEXP (x, 1);
4597 enum rtx_code new_code;
4599 if (GET_CODE (op0) == COMPARE)
4600 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4602 /* Simplify our comparison, if possible. */
4603 new_code = simplify_comparison (code, &op0, &op1);
4605 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4606 if only the low-order bit is possibly nonzero in X (such as when
4607 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4608 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4609 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4610 (plus X 1).
4612 Remove any ZERO_EXTRACT we made when thinking this was a
4613 comparison. It may now be simpler to use, e.g., an AND. If a
4614 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4615 the call to make_compound_operation in the SET case. */
4617 if (STORE_FLAG_VALUE == 1
4618 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4619 && op1 == const0_rtx
4620 && mode == GET_MODE (op0)
4621 && nonzero_bits (op0, mode) == 1)
4622 return gen_lowpart (mode,
4623 expand_compound_operation (op0));
4625 else if (STORE_FLAG_VALUE == 1
4626 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4627 && op1 == const0_rtx
4628 && mode == GET_MODE (op0)
4629 && (num_sign_bit_copies (op0, mode)
4630 == GET_MODE_BITSIZE (mode)))
4632 op0 = expand_compound_operation (op0);
4633 return simplify_gen_unary (NEG, mode,
4634 gen_lowpart (mode, op0),
4635 mode);
4638 else if (STORE_FLAG_VALUE == 1
4639 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4640 && op1 == const0_rtx
4641 && mode == GET_MODE (op0)
4642 && nonzero_bits (op0, mode) == 1)
4644 op0 = expand_compound_operation (op0);
4645 return simplify_gen_binary (XOR, mode,
4646 gen_lowpart (mode, op0),
4647 const1_rtx);
4650 else if (STORE_FLAG_VALUE == 1
4651 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4652 && op1 == const0_rtx
4653 && mode == GET_MODE (op0)
4654 && (num_sign_bit_copies (op0, mode)
4655 == GET_MODE_BITSIZE (mode)))
4657 op0 = expand_compound_operation (op0);
4658 return plus_constant (gen_lowpart (mode, op0), 1);
4661 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4662 those above. */
4663 if (STORE_FLAG_VALUE == -1
4664 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4665 && op1 == const0_rtx
4666 && (num_sign_bit_copies (op0, mode)
4667 == GET_MODE_BITSIZE (mode)))
4668 return gen_lowpart (mode,
4669 expand_compound_operation (op0));
4671 else if (STORE_FLAG_VALUE == -1
4672 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4673 && op1 == const0_rtx
4674 && mode == GET_MODE (op0)
4675 && nonzero_bits (op0, mode) == 1)
4677 op0 = expand_compound_operation (op0);
4678 return simplify_gen_unary (NEG, mode,
4679 gen_lowpart (mode, op0),
4680 mode);
4683 else if (STORE_FLAG_VALUE == -1
4684 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4685 && op1 == const0_rtx
4686 && mode == GET_MODE (op0)
4687 && (num_sign_bit_copies (op0, mode)
4688 == GET_MODE_BITSIZE (mode)))
4690 op0 = expand_compound_operation (op0);
4691 return simplify_gen_unary (NOT, mode,
4692 gen_lowpart (mode, op0),
4693 mode);
4696 /* If X is 0/1, (eq X 0) is X-1. */
4697 else if (STORE_FLAG_VALUE == -1
4698 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4699 && op1 == const0_rtx
4700 && mode == GET_MODE (op0)
4701 && nonzero_bits (op0, mode) == 1)
4703 op0 = expand_compound_operation (op0);
4704 return plus_constant (gen_lowpart (mode, op0), -1);
4707 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4708 one bit that might be nonzero, we can convert (ne x 0) to
4709 (ashift x c) where C puts the bit in the sign bit. Remove any
4710 AND with STORE_FLAG_VALUE when we are done, since we are only
4711 going to test the sign bit. */
4712 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4713 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4714 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4715 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4716 && op1 == const0_rtx
4717 && mode == GET_MODE (op0)
4718 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4720 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4721 expand_compound_operation (op0),
4722 GET_MODE_BITSIZE (mode) - 1 - i);
4723 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4724 return XEXP (x, 0);
4725 else
4726 return x;
4729 /* If the code changed, return a whole new comparison. */
4730 if (new_code != code)
4731 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4733 /* Otherwise, keep this operation, but maybe change its operands.
4734 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4735 SUBST (XEXP (x, 0), op0);
4736 SUBST (XEXP (x, 1), op1);
4738 break;
4740 case IF_THEN_ELSE:
4741 return simplify_if_then_else (x);
4743 case ZERO_EXTRACT:
4744 case SIGN_EXTRACT:
4745 case ZERO_EXTEND:
4746 case SIGN_EXTEND:
4747 /* If we are processing SET_DEST, we are done. */
4748 if (in_dest)
4749 return x;
4751 return expand_compound_operation (x);
4753 case SET:
4754 return simplify_set (x);
4756 case AND:
4757 case IOR:
4758 case XOR:
4759 return simplify_logical (x);
4761 case ABS:
4762 /* (abs (neg <foo>)) -> (abs <foo>) */
4763 if (GET_CODE (XEXP (x, 0)) == NEG)
4764 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4766 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4767 do nothing. */
4768 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4769 break;
4771 /* If operand is something known to be positive, ignore the ABS. */
4772 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4773 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4774 <= HOST_BITS_PER_WIDE_INT)
4775 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4776 & ((HOST_WIDE_INT) 1
4777 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4778 == 0)))
4779 return XEXP (x, 0);
4781 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4782 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4783 return gen_rtx_NEG (mode, XEXP (x, 0));
4785 break;
4787 case FFS:
4788 /* (ffs (*_extend <X>)) = (ffs <X>) */
4789 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4790 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4791 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4792 break;
4794 case POPCOUNT:
4795 case PARITY:
4796 /* (pop* (zero_extend <X>)) = (pop* <X>) */
4797 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4798 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4799 break;
4801 case FLOAT:
4802 /* (float (sign_extend <X>)) = (float <X>). */
4803 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4804 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4805 break;
4807 case ASHIFT:
4808 case LSHIFTRT:
4809 case ASHIFTRT:
4810 case ROTATE:
4811 case ROTATERT:
4812 /* If this is a shift by a constant amount, simplify it. */
4813 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4814 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4815 INTVAL (XEXP (x, 1)));
4817 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
4818 SUBST (XEXP (x, 1),
4819 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4820 ((HOST_WIDE_INT) 1
4821 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4822 - 1,
4823 NULL_RTX, 0));
4824 break;
4826 case VEC_SELECT:
4828 rtx op0 = XEXP (x, 0);
4829 rtx op1 = XEXP (x, 1);
4830 int len;
4832 gcc_assert (GET_CODE (op1) == PARALLEL);
4833 len = XVECLEN (op1, 0);
4834 if (len == 1
4835 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4836 && GET_CODE (op0) == VEC_CONCAT)
4838 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4840 /* Try to find the element in the VEC_CONCAT. */
4841 for (;;)
4843 if (GET_MODE (op0) == GET_MODE (x))
4844 return op0;
4845 if (GET_CODE (op0) == VEC_CONCAT)
4847 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4848 if (offset < op0_size)
4849 op0 = XEXP (op0, 0);
4850 else
4852 offset -= op0_size;
4853 op0 = XEXP (op0, 1);
4856 else
4857 break;
4862 break;
4864 default:
4865 break;
4868 return x;
4871 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4873 static rtx
4874 simplify_if_then_else (rtx x)
4876 enum machine_mode mode = GET_MODE (x);
4877 rtx cond = XEXP (x, 0);
4878 rtx true_rtx = XEXP (x, 1);
4879 rtx false_rtx = XEXP (x, 2);
4880 enum rtx_code true_code = GET_CODE (cond);
4881 int comparison_p = COMPARISON_P (cond);
4882 rtx temp;
4883 int i;
4884 enum rtx_code false_code;
4885 rtx reversed;
4887 /* Simplify storing of the truth value. */
4888 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4889 return simplify_gen_relational (true_code, mode, VOIDmode,
4890 XEXP (cond, 0), XEXP (cond, 1));
4892 /* Also when the truth value has to be reversed. */
4893 if (comparison_p
4894 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4895 && (reversed = reversed_comparison (cond, mode)))
4896 return reversed;
4898 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4899 in it is being compared against certain values. Get the true and false
4900 comparisons and see if that says anything about the value of each arm. */
4902 if (comparison_p
4903 && ((false_code = reversed_comparison_code (cond, NULL))
4904 != UNKNOWN)
4905 && REG_P (XEXP (cond, 0)))
4907 HOST_WIDE_INT nzb;
4908 rtx from = XEXP (cond, 0);
4909 rtx true_val = XEXP (cond, 1);
4910 rtx false_val = true_val;
4911 int swapped = 0;
4913 /* If FALSE_CODE is EQ, swap the codes and arms. */
4915 if (false_code == EQ)
4917 swapped = 1, true_code = EQ, false_code = NE;
4918 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4921 /* If we are comparing against zero and the expression being tested has
4922 only a single bit that might be nonzero, that is its value when it is
4923 not equal to zero. Similarly if it is known to be -1 or 0. */
4925 if (true_code == EQ && true_val == const0_rtx
4926 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4927 false_code = EQ, false_val = GEN_INT (nzb);
4928 else if (true_code == EQ && true_val == const0_rtx
4929 && (num_sign_bit_copies (from, GET_MODE (from))
4930 == GET_MODE_BITSIZE (GET_MODE (from))))
4931 false_code = EQ, false_val = constm1_rtx;
4933 /* Now simplify an arm if we know the value of the register in the
4934 branch and it is used in the arm. Be careful due to the potential
4935 of locally-shared RTL. */
4937 if (reg_mentioned_p (from, true_rtx))
4938 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4939 from, true_val),
4940 pc_rtx, pc_rtx, 0, 0);
4941 if (reg_mentioned_p (from, false_rtx))
4942 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4943 from, false_val),
4944 pc_rtx, pc_rtx, 0, 0);
4946 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4947 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4949 true_rtx = XEXP (x, 1);
4950 false_rtx = XEXP (x, 2);
4951 true_code = GET_CODE (cond);
4954 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4955 reversed, do so to avoid needing two sets of patterns for
4956 subtract-and-branch insns. Similarly if we have a constant in the true
4957 arm, the false arm is the same as the first operand of the comparison, or
4958 the false arm is more complicated than the true arm. */
4960 if (comparison_p
4961 && reversed_comparison_code (cond, NULL) != UNKNOWN
4962 && (true_rtx == pc_rtx
4963 || (CONSTANT_P (true_rtx)
4964 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4965 || true_rtx == const0_rtx
4966 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4967 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4968 && !OBJECT_P (false_rtx))
4969 || reg_mentioned_p (true_rtx, false_rtx)
4970 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4972 true_code = reversed_comparison_code (cond, NULL);
4973 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
4974 SUBST (XEXP (x, 1), false_rtx);
4975 SUBST (XEXP (x, 2), true_rtx);
4977 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4978 cond = XEXP (x, 0);
4980 /* It is possible that the conditional has been simplified out. */
4981 true_code = GET_CODE (cond);
4982 comparison_p = COMPARISON_P (cond);
4985 /* If the two arms are identical, we don't need the comparison. */
4987 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4988 return true_rtx;
4990 /* Convert a == b ? b : a to "a". */
4991 if (true_code == EQ && ! side_effects_p (cond)
4992 && !HONOR_NANS (mode)
4993 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4994 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4995 return false_rtx;
4996 else if (true_code == NE && ! side_effects_p (cond)
4997 && !HONOR_NANS (mode)
4998 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4999 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5000 return true_rtx;
5002 /* Look for cases where we have (abs x) or (neg (abs X)). */
5004 if (GET_MODE_CLASS (mode) == MODE_INT
5005 && GET_CODE (false_rtx) == NEG
5006 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5007 && comparison_p
5008 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5009 && ! side_effects_p (true_rtx))
5010 switch (true_code)
5012 case GT:
5013 case GE:
5014 return simplify_gen_unary (ABS, mode, true_rtx, mode);
5015 case LT:
5016 case LE:
5017 return
5018 simplify_gen_unary (NEG, mode,
5019 simplify_gen_unary (ABS, mode, true_rtx, mode),
5020 mode);
5021 default:
5022 break;
5025 /* Look for MIN or MAX. */
5027 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5028 && comparison_p
5029 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5030 && rtx_equal_p (XEXP (cond, 1), false_rtx)
5031 && ! side_effects_p (cond))
5032 switch (true_code)
5034 case GE:
5035 case GT:
5036 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5037 case LE:
5038 case LT:
5039 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5040 case GEU:
5041 case GTU:
5042 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5043 case LEU:
5044 case LTU:
5045 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5046 default:
5047 break;
5050 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5051 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5052 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5053 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5054 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5055 neither 1 or -1, but it isn't worth checking for. */
5057 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5058 && comparison_p
5059 && GET_MODE_CLASS (mode) == MODE_INT
5060 && ! side_effects_p (x))
5062 rtx t = make_compound_operation (true_rtx, SET);
5063 rtx f = make_compound_operation (false_rtx, SET);
5064 rtx cond_op0 = XEXP (cond, 0);
5065 rtx cond_op1 = XEXP (cond, 1);
5066 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5067 enum machine_mode m = mode;
5068 rtx z = 0, c1 = NULL_RTX;
5070 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5071 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5072 || GET_CODE (t) == ASHIFT
5073 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5074 && rtx_equal_p (XEXP (t, 0), f))
5075 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5077 /* If an identity-zero op is commutative, check whether there
5078 would be a match if we swapped the operands. */
5079 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5080 || GET_CODE (t) == XOR)
5081 && rtx_equal_p (XEXP (t, 1), f))
5082 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5083 else if (GET_CODE (t) == SIGN_EXTEND
5084 && (GET_CODE (XEXP (t, 0)) == PLUS
5085 || GET_CODE (XEXP (t, 0)) == MINUS
5086 || GET_CODE (XEXP (t, 0)) == IOR
5087 || GET_CODE (XEXP (t, 0)) == XOR
5088 || GET_CODE (XEXP (t, 0)) == ASHIFT
5089 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5090 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5091 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5092 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5093 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5094 && (num_sign_bit_copies (f, GET_MODE (f))
5095 > (unsigned int)
5096 (GET_MODE_BITSIZE (mode)
5097 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5099 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5100 extend_op = SIGN_EXTEND;
5101 m = GET_MODE (XEXP (t, 0));
5103 else if (GET_CODE (t) == SIGN_EXTEND
5104 && (GET_CODE (XEXP (t, 0)) == PLUS
5105 || GET_CODE (XEXP (t, 0)) == IOR
5106 || GET_CODE (XEXP (t, 0)) == XOR)
5107 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5108 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5109 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5110 && (num_sign_bit_copies (f, GET_MODE (f))
5111 > (unsigned int)
5112 (GET_MODE_BITSIZE (mode)
5113 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5115 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5116 extend_op = SIGN_EXTEND;
5117 m = GET_MODE (XEXP (t, 0));
5119 else if (GET_CODE (t) == ZERO_EXTEND
5120 && (GET_CODE (XEXP (t, 0)) == PLUS
5121 || GET_CODE (XEXP (t, 0)) == MINUS
5122 || GET_CODE (XEXP (t, 0)) == IOR
5123 || GET_CODE (XEXP (t, 0)) == XOR
5124 || GET_CODE (XEXP (t, 0)) == ASHIFT
5125 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5126 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5127 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5128 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5129 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5130 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5131 && ((nonzero_bits (f, GET_MODE (f))
5132 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5133 == 0))
5135 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5136 extend_op = ZERO_EXTEND;
5137 m = GET_MODE (XEXP (t, 0));
5139 else if (GET_CODE (t) == ZERO_EXTEND
5140 && (GET_CODE (XEXP (t, 0)) == PLUS
5141 || GET_CODE (XEXP (t, 0)) == IOR
5142 || GET_CODE (XEXP (t, 0)) == XOR)
5143 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5144 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5145 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5146 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5147 && ((nonzero_bits (f, GET_MODE (f))
5148 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5149 == 0))
5151 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5152 extend_op = ZERO_EXTEND;
5153 m = GET_MODE (XEXP (t, 0));
5156 if (z)
5158 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5159 cond_op0, cond_op1),
5160 pc_rtx, pc_rtx, 0, 0);
5161 temp = simplify_gen_binary (MULT, m, temp,
5162 simplify_gen_binary (MULT, m, c1,
5163 const_true_rtx));
5164 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5165 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5167 if (extend_op != UNKNOWN)
5168 temp = simplify_gen_unary (extend_op, mode, temp, m);
5170 return temp;
5174 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5175 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5176 negation of a single bit, we can convert this operation to a shift. We
5177 can actually do this more generally, but it doesn't seem worth it. */
5179 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5180 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5181 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5182 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5183 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5184 == GET_MODE_BITSIZE (mode))
5185 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5186 return
5187 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5188 gen_lowpart (mode, XEXP (cond, 0)), i);
5190 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5191 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5192 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5193 && GET_MODE (XEXP (cond, 0)) == mode
5194 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5195 == nonzero_bits (XEXP (cond, 0), mode)
5196 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5197 return XEXP (cond, 0);
5199 return x;
5202 /* Simplify X, a SET expression. Return the new expression. */
5204 static rtx
5205 simplify_set (rtx x)
5207 rtx src = SET_SRC (x);
5208 rtx dest = SET_DEST (x);
5209 enum machine_mode mode
5210 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5211 rtx other_insn;
5212 rtx *cc_use;
5214 /* (set (pc) (return)) gets written as (return). */
5215 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5216 return src;
5218 /* Now that we know for sure which bits of SRC we are using, see if we can
5219 simplify the expression for the object knowing that we only need the
5220 low-order bits. */
5222 if (GET_MODE_CLASS (mode) == MODE_INT
5223 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5225 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5226 SUBST (SET_SRC (x), src);
5229 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5230 the comparison result and try to simplify it unless we already have used
5231 undobuf.other_insn. */
5232 if ((GET_MODE_CLASS (mode) == MODE_CC
5233 || GET_CODE (src) == COMPARE
5234 || CC0_P (dest))
5235 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5236 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5237 && COMPARISON_P (*cc_use)
5238 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5240 enum rtx_code old_code = GET_CODE (*cc_use);
5241 enum rtx_code new_code;
5242 rtx op0, op1, tmp;
5243 int other_changed = 0;
5244 enum machine_mode compare_mode = GET_MODE (dest);
5246 if (GET_CODE (src) == COMPARE)
5247 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5248 else
5249 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5251 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5252 op0, op1);
5253 if (!tmp)
5254 new_code = old_code;
5255 else if (!CONSTANT_P (tmp))
5257 new_code = GET_CODE (tmp);
5258 op0 = XEXP (tmp, 0);
5259 op1 = XEXP (tmp, 1);
5261 else
5263 rtx pat = PATTERN (other_insn);
5264 undobuf.other_insn = other_insn;
5265 SUBST (*cc_use, tmp);
5267 /* Attempt to simplify CC user. */
5268 if (GET_CODE (pat) == SET)
5270 rtx new = simplify_rtx (SET_SRC (pat));
5271 if (new != NULL_RTX)
5272 SUBST (SET_SRC (pat), new);
5275 /* Convert X into a no-op move. */
5276 SUBST (SET_DEST (x), pc_rtx);
5277 SUBST (SET_SRC (x), pc_rtx);
5278 return x;
5281 /* Simplify our comparison, if possible. */
5282 new_code = simplify_comparison (new_code, &op0, &op1);
5284 #ifdef SELECT_CC_MODE
5285 /* If this machine has CC modes other than CCmode, check to see if we
5286 need to use a different CC mode here. */
5287 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5288 compare_mode = GET_MODE (op0);
5289 else
5290 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5292 #ifndef HAVE_cc0
5293 /* If the mode changed, we have to change SET_DEST, the mode in the
5294 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5295 a hard register, just build new versions with the proper mode. If it
5296 is a pseudo, we lose unless it is only time we set the pseudo, in
5297 which case we can safely change its mode. */
5298 if (compare_mode != GET_MODE (dest))
5300 if (can_change_dest_mode (dest, 0, compare_mode))
5302 unsigned int regno = REGNO (dest);
5303 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5305 if (regno >= FIRST_PSEUDO_REGISTER)
5306 SUBST (regno_reg_rtx[regno], new_dest);
5308 SUBST (SET_DEST (x), new_dest);
5309 SUBST (XEXP (*cc_use, 0), new_dest);
5310 other_changed = 1;
5312 dest = new_dest;
5315 #endif /* cc0 */
5316 #endif /* SELECT_CC_MODE */
5318 /* If the code changed, we have to build a new comparison in
5319 undobuf.other_insn. */
5320 if (new_code != old_code)
5322 int other_changed_previously = other_changed;
5323 unsigned HOST_WIDE_INT mask;
5325 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5326 dest, const0_rtx));
5327 other_changed = 1;
5329 /* If the only change we made was to change an EQ into an NE or
5330 vice versa, OP0 has only one bit that might be nonzero, and OP1
5331 is zero, check if changing the user of the condition code will
5332 produce a valid insn. If it won't, we can keep the original code
5333 in that insn by surrounding our operation with an XOR. */
5335 if (((old_code == NE && new_code == EQ)
5336 || (old_code == EQ && new_code == NE))
5337 && ! other_changed_previously && op1 == const0_rtx
5338 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5339 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5341 rtx pat = PATTERN (other_insn), note = 0;
5343 if ((recog_for_combine (&pat, other_insn, &note) < 0
5344 && ! check_asm_operands (pat)))
5346 PUT_CODE (*cc_use, old_code);
5347 other_changed = 0;
5349 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5350 op0, GEN_INT (mask));
5355 if (other_changed)
5356 undobuf.other_insn = other_insn;
5358 #ifdef HAVE_cc0
5359 /* If we are now comparing against zero, change our source if
5360 needed. If we do not use cc0, we always have a COMPARE. */
5361 if (op1 == const0_rtx && dest == cc0_rtx)
5363 SUBST (SET_SRC (x), op0);
5364 src = op0;
5366 else
5367 #endif
5369 /* Otherwise, if we didn't previously have a COMPARE in the
5370 correct mode, we need one. */
5371 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5373 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5374 src = SET_SRC (x);
5376 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5378 SUBST(SET_SRC (x), op0);
5379 src = SET_SRC (x);
5381 else
5383 /* Otherwise, update the COMPARE if needed. */
5384 SUBST (XEXP (src, 0), op0);
5385 SUBST (XEXP (src, 1), op1);
5388 else
5390 /* Get SET_SRC in a form where we have placed back any
5391 compound expressions. Then do the checks below. */
5392 src = make_compound_operation (src, SET);
5393 SUBST (SET_SRC (x), src);
5396 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5397 and X being a REG or (subreg (reg)), we may be able to convert this to
5398 (set (subreg:m2 x) (op)).
5400 We can always do this if M1 is narrower than M2 because that means that
5401 we only care about the low bits of the result.
5403 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5404 perform a narrower operation than requested since the high-order bits will
5405 be undefined. On machine where it is defined, this transformation is safe
5406 as long as M1 and M2 have the same number of words. */
5408 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5409 && !OBJECT_P (SUBREG_REG (src))
5410 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5411 / UNITS_PER_WORD)
5412 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5413 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5414 #ifndef WORD_REGISTER_OPERATIONS
5415 && (GET_MODE_SIZE (GET_MODE (src))
5416 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5417 #endif
5418 #ifdef CANNOT_CHANGE_MODE_CLASS
5419 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5420 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5421 GET_MODE (SUBREG_REG (src)),
5422 GET_MODE (src)))
5423 #endif
5424 && (REG_P (dest)
5425 || (GET_CODE (dest) == SUBREG
5426 && REG_P (SUBREG_REG (dest)))))
5428 SUBST (SET_DEST (x),
5429 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5430 dest));
5431 SUBST (SET_SRC (x), SUBREG_REG (src));
5433 src = SET_SRC (x), dest = SET_DEST (x);
5436 #ifdef HAVE_cc0
5437 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5438 in SRC. */
5439 if (dest == cc0_rtx
5440 && GET_CODE (src) == SUBREG
5441 && subreg_lowpart_p (src)
5442 && (GET_MODE_BITSIZE (GET_MODE (src))
5443 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5445 rtx inner = SUBREG_REG (src);
5446 enum machine_mode inner_mode = GET_MODE (inner);
5448 /* Here we make sure that we don't have a sign bit on. */
5449 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5450 && (nonzero_bits (inner, inner_mode)
5451 < ((unsigned HOST_WIDE_INT) 1
5452 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5454 SUBST (SET_SRC (x), inner);
5455 src = SET_SRC (x);
5458 #endif
5460 #ifdef LOAD_EXTEND_OP
5461 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5462 would require a paradoxical subreg. Replace the subreg with a
5463 zero_extend to avoid the reload that would otherwise be required. */
5465 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5466 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5467 && SUBREG_BYTE (src) == 0
5468 && (GET_MODE_SIZE (GET_MODE (src))
5469 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5470 && MEM_P (SUBREG_REG (src)))
5472 SUBST (SET_SRC (x),
5473 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5474 GET_MODE (src), SUBREG_REG (src)));
5476 src = SET_SRC (x);
5478 #endif
5480 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5481 are comparing an item known to be 0 or -1 against 0, use a logical
5482 operation instead. Check for one of the arms being an IOR of the other
5483 arm with some value. We compute three terms to be IOR'ed together. In
5484 practice, at most two will be nonzero. Then we do the IOR's. */
5486 if (GET_CODE (dest) != PC
5487 && GET_CODE (src) == IF_THEN_ELSE
5488 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5489 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5490 && XEXP (XEXP (src, 0), 1) == const0_rtx
5491 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5492 #ifdef HAVE_conditional_move
5493 && ! can_conditionally_move_p (GET_MODE (src))
5494 #endif
5495 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5496 GET_MODE (XEXP (XEXP (src, 0), 0)))
5497 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5498 && ! side_effects_p (src))
5500 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5501 ? XEXP (src, 1) : XEXP (src, 2));
5502 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5503 ? XEXP (src, 2) : XEXP (src, 1));
5504 rtx term1 = const0_rtx, term2, term3;
5506 if (GET_CODE (true_rtx) == IOR
5507 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5508 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5509 else if (GET_CODE (true_rtx) == IOR
5510 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5511 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5512 else if (GET_CODE (false_rtx) == IOR
5513 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5514 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5515 else if (GET_CODE (false_rtx) == IOR
5516 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5517 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5519 term2 = simplify_gen_binary (AND, GET_MODE (src),
5520 XEXP (XEXP (src, 0), 0), true_rtx);
5521 term3 = simplify_gen_binary (AND, GET_MODE (src),
5522 simplify_gen_unary (NOT, GET_MODE (src),
5523 XEXP (XEXP (src, 0), 0),
5524 GET_MODE (src)),
5525 false_rtx);
5527 SUBST (SET_SRC (x),
5528 simplify_gen_binary (IOR, GET_MODE (src),
5529 simplify_gen_binary (IOR, GET_MODE (src),
5530 term1, term2),
5531 term3));
5533 src = SET_SRC (x);
5536 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5537 whole thing fail. */
5538 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5539 return src;
5540 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5541 return dest;
5542 else
5543 /* Convert this into a field assignment operation, if possible. */
5544 return make_field_assignment (x);
5547 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5548 result. */
5550 static rtx
5551 simplify_logical (rtx x)
5553 enum machine_mode mode = GET_MODE (x);
5554 rtx op0 = XEXP (x, 0);
5555 rtx op1 = XEXP (x, 1);
5556 rtx reversed;
5558 switch (GET_CODE (x))
5560 case AND:
5561 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5562 insn (and may simplify more). */
5563 if (GET_CODE (op0) == XOR
5564 && rtx_equal_p (XEXP (op0, 0), op1)
5565 && ! side_effects_p (op1))
5566 x = simplify_gen_binary (AND, mode,
5567 simplify_gen_unary (NOT, mode,
5568 XEXP (op0, 1), mode),
5569 op1);
5571 if (GET_CODE (op0) == XOR
5572 && rtx_equal_p (XEXP (op0, 1), op1)
5573 && ! side_effects_p (op1))
5574 x = simplify_gen_binary (AND, mode,
5575 simplify_gen_unary (NOT, mode,
5576 XEXP (op0, 0), mode),
5577 op1);
5579 /* Similarly for (~(A ^ B)) & A. */
5580 if (GET_CODE (op0) == NOT
5581 && GET_CODE (XEXP (op0, 0)) == XOR
5582 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5583 && ! side_effects_p (op1))
5584 x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5586 if (GET_CODE (op0) == NOT
5587 && GET_CODE (XEXP (op0, 0)) == XOR
5588 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5589 && ! side_effects_p (op1))
5590 x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5592 /* We can call simplify_and_const_int only if we don't lose
5593 any (sign) bits when converting INTVAL (op1) to
5594 "unsigned HOST_WIDE_INT". */
5595 if (GET_CODE (op1) == CONST_INT
5596 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5597 || INTVAL (op1) > 0))
5599 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5601 /* If we have (ior (and (X C1) C2)) and the next restart would be
5602 the last, simplify this by making C1 as small as possible
5603 and then exit. Only do this if C1 actually changes: for now
5604 this only saves memory but, should this transformation be
5605 moved to simplify-rtx.c, we'd risk unbounded recursion there. */
5606 if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5607 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5608 && GET_CODE (op1) == CONST_INT
5609 && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5610 return simplify_gen_binary (IOR, mode,
5611 simplify_gen_binary
5612 (AND, mode, XEXP (op0, 0),
5613 GEN_INT (INTVAL (XEXP (op0, 1))
5614 & ~INTVAL (op1))), op1);
5616 if (GET_CODE (x) != AND)
5617 return x;
5619 op0 = XEXP (x, 0);
5620 op1 = XEXP (x, 1);
5623 /* Convert (A | B) & A to A. */
5624 if (GET_CODE (op0) == IOR
5625 && (rtx_equal_p (XEXP (op0, 0), op1)
5626 || rtx_equal_p (XEXP (op0, 1), op1))
5627 && ! side_effects_p (XEXP (op0, 0))
5628 && ! side_effects_p (XEXP (op0, 1)))
5629 return op1;
5631 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5632 apply the distributive law and then the inverse distributive
5633 law to see if things simplify. */
5634 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5636 rtx result = distribute_and_simplify_rtx (x, 0);
5637 if (result)
5638 return result;
5640 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5642 rtx result = distribute_and_simplify_rtx (x, 1);
5643 if (result)
5644 return result;
5646 break;
5648 case IOR:
5649 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5650 if (GET_CODE (op1) == CONST_INT
5651 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5652 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5653 return op1;
5655 /* Convert (A & B) | A to A. */
5656 if (GET_CODE (op0) == AND
5657 && (rtx_equal_p (XEXP (op0, 0), op1)
5658 || rtx_equal_p (XEXP (op0, 1), op1))
5659 && ! side_effects_p (XEXP (op0, 0))
5660 && ! side_effects_p (XEXP (op0, 1)))
5661 return op1;
5663 /* If we have (ior (and A B) C), apply the distributive law and then
5664 the inverse distributive law to see if things simplify. */
5666 if (GET_CODE (op0) == AND)
5668 rtx result = distribute_and_simplify_rtx (x, 0);
5669 if (result)
5670 return result;
5673 if (GET_CODE (op1) == AND)
5675 rtx result = distribute_and_simplify_rtx (x, 1);
5676 if (result)
5677 return result;
5680 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5681 mode size to (rotate A CX). */
5683 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5684 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5685 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5686 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5687 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5688 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5689 == GET_MODE_BITSIZE (mode)))
5690 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5691 (GET_CODE (op0) == ASHIFT
5692 ? XEXP (op0, 1) : XEXP (op1, 1)));
5694 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5695 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5696 does not affect any of the bits in OP1, it can really be done
5697 as a PLUS and we can associate. We do this by seeing if OP1
5698 can be safely shifted left C bits. */
5699 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5700 && GET_CODE (XEXP (op0, 0)) == PLUS
5701 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5702 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5703 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5705 int count = INTVAL (XEXP (op0, 1));
5706 HOST_WIDE_INT mask = INTVAL (op1) << count;
5708 if (mask >> count == INTVAL (op1)
5709 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5711 SUBST (XEXP (XEXP (op0, 0), 1),
5712 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5713 return op0;
5716 break;
5718 case XOR:
5719 /* If we are XORing two things that have no bits in common,
5720 convert them into an IOR. This helps to detect rotation encoded
5721 using those methods and possibly other simplifications. */
5723 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5724 && (nonzero_bits (op0, mode)
5725 & nonzero_bits (op1, mode)) == 0)
5726 return (simplify_gen_binary (IOR, mode, op0, op1));
5728 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5729 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5730 (NOT y). */
5732 int num_negated = 0;
5734 if (GET_CODE (op0) == NOT)
5735 num_negated++, op0 = XEXP (op0, 0);
5736 if (GET_CODE (op1) == NOT)
5737 num_negated++, op1 = XEXP (op1, 0);
5739 if (num_negated == 2)
5741 SUBST (XEXP (x, 0), op0);
5742 SUBST (XEXP (x, 1), op1);
5744 else if (num_negated == 1)
5745 return
5746 simplify_gen_unary (NOT, mode,
5747 simplify_gen_binary (XOR, mode, op0, op1),
5748 mode);
5751 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5752 correspond to a machine insn or result in further simplifications
5753 if B is a constant. */
5755 if (GET_CODE (op0) == AND
5756 && rtx_equal_p (XEXP (op0, 1), op1)
5757 && ! side_effects_p (op1))
5758 return simplify_gen_binary (AND, mode,
5759 simplify_gen_unary (NOT, mode,
5760 XEXP (op0, 0), mode),
5761 op1);
5763 else if (GET_CODE (op0) == AND
5764 && rtx_equal_p (XEXP (op0, 0), op1)
5765 && ! side_effects_p (op1))
5766 return simplify_gen_binary (AND, mode,
5767 simplify_gen_unary (NOT, mode,
5768 XEXP (op0, 1), mode),
5769 op1);
5771 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5772 comparison if STORE_FLAG_VALUE is 1. */
5773 if (STORE_FLAG_VALUE == 1
5774 && op1 == const1_rtx
5775 && COMPARISON_P (op0)
5776 && (reversed = reversed_comparison (op0, mode)))
5777 return reversed;
5779 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5780 is (lt foo (const_int 0)), so we can perform the above
5781 simplification if STORE_FLAG_VALUE is 1. */
5783 if (STORE_FLAG_VALUE == 1
5784 && op1 == const1_rtx
5785 && GET_CODE (op0) == LSHIFTRT
5786 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5787 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5788 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5790 /* (xor (comparison foo bar) (const_int sign-bit))
5791 when STORE_FLAG_VALUE is the sign bit. */
5792 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5793 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5794 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5795 && op1 == const_true_rtx
5796 && COMPARISON_P (op0)
5797 && (reversed = reversed_comparison (op0, mode)))
5798 return reversed;
5800 break;
5802 default:
5803 gcc_unreachable ();
5806 return x;
5809 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5810 operations" because they can be replaced with two more basic operations.
5811 ZERO_EXTEND is also considered "compound" because it can be replaced with
5812 an AND operation, which is simpler, though only one operation.
5814 The function expand_compound_operation is called with an rtx expression
5815 and will convert it to the appropriate shifts and AND operations,
5816 simplifying at each stage.
5818 The function make_compound_operation is called to convert an expression
5819 consisting of shifts and ANDs into the equivalent compound expression.
5820 It is the inverse of this function, loosely speaking. */
5822 static rtx
5823 expand_compound_operation (rtx x)
5825 unsigned HOST_WIDE_INT pos = 0, len;
5826 int unsignedp = 0;
5827 unsigned int modewidth;
5828 rtx tem;
5830 switch (GET_CODE (x))
5832 case ZERO_EXTEND:
5833 unsignedp = 1;
5834 case SIGN_EXTEND:
5835 /* We can't necessarily use a const_int for a multiword mode;
5836 it depends on implicitly extending the value.
5837 Since we don't know the right way to extend it,
5838 we can't tell whether the implicit way is right.
5840 Even for a mode that is no wider than a const_int,
5841 we can't win, because we need to sign extend one of its bits through
5842 the rest of it, and we don't know which bit. */
5843 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5844 return x;
5846 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5847 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5848 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5849 reloaded. If not for that, MEM's would very rarely be safe.
5851 Reject MODEs bigger than a word, because we might not be able
5852 to reference a two-register group starting with an arbitrary register
5853 (and currently gen_lowpart might crash for a SUBREG). */
5855 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5856 return x;
5858 /* Reject MODEs that aren't scalar integers because turning vector
5859 or complex modes into shifts causes problems. */
5861 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5862 return x;
5864 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5865 /* If the inner object has VOIDmode (the only way this can happen
5866 is if it is an ASM_OPERANDS), we can't do anything since we don't
5867 know how much masking to do. */
5868 if (len == 0)
5869 return x;
5871 break;
5873 case ZERO_EXTRACT:
5874 unsignedp = 1;
5876 /* ... fall through ... */
5878 case SIGN_EXTRACT:
5879 /* If the operand is a CLOBBER, just return it. */
5880 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5881 return XEXP (x, 0);
5883 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5884 || GET_CODE (XEXP (x, 2)) != CONST_INT
5885 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5886 return x;
5888 /* Reject MODEs that aren't scalar integers because turning vector
5889 or complex modes into shifts causes problems. */
5891 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5892 return x;
5894 len = INTVAL (XEXP (x, 1));
5895 pos = INTVAL (XEXP (x, 2));
5897 /* If this goes outside the object being extracted, replace the object
5898 with a (use (mem ...)) construct that only combine understands
5899 and is used only for this purpose. */
5900 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5901 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5903 if (BITS_BIG_ENDIAN)
5904 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5906 break;
5908 default:
5909 return x;
5911 /* Convert sign extension to zero extension, if we know that the high
5912 bit is not set, as this is easier to optimize. It will be converted
5913 back to cheaper alternative in make_extraction. */
5914 if (GET_CODE (x) == SIGN_EXTEND
5915 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5916 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5917 & ~(((unsigned HOST_WIDE_INT)
5918 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5919 >> 1))
5920 == 0)))
5922 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5923 rtx temp2 = expand_compound_operation (temp);
5925 /* Make sure this is a profitable operation. */
5926 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5927 return temp2;
5928 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5929 return temp;
5930 else
5931 return x;
5934 /* We can optimize some special cases of ZERO_EXTEND. */
5935 if (GET_CODE (x) == ZERO_EXTEND)
5937 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5938 know that the last value didn't have any inappropriate bits
5939 set. */
5940 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5941 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5942 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5943 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5944 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5945 return XEXP (XEXP (x, 0), 0);
5947 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5948 if (GET_CODE (XEXP (x, 0)) == SUBREG
5949 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5950 && subreg_lowpart_p (XEXP (x, 0))
5951 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5952 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5953 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5954 return SUBREG_REG (XEXP (x, 0));
5956 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5957 is a comparison and STORE_FLAG_VALUE permits. This is like
5958 the first case, but it works even when GET_MODE (x) is larger
5959 than HOST_WIDE_INT. */
5960 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5961 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5962 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5963 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5964 <= HOST_BITS_PER_WIDE_INT)
5965 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5966 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5967 return XEXP (XEXP (x, 0), 0);
5969 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5970 if (GET_CODE (XEXP (x, 0)) == SUBREG
5971 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5972 && subreg_lowpart_p (XEXP (x, 0))
5973 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5974 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5975 <= HOST_BITS_PER_WIDE_INT)
5976 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5977 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5978 return SUBREG_REG (XEXP (x, 0));
5982 /* If we reach here, we want to return a pair of shifts. The inner
5983 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5984 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5985 logical depending on the value of UNSIGNEDP.
5987 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5988 converted into an AND of a shift.
5990 We must check for the case where the left shift would have a negative
5991 count. This can happen in a case like (x >> 31) & 255 on machines
5992 that can't shift by a constant. On those machines, we would first
5993 combine the shift with the AND to produce a variable-position
5994 extraction. Then the constant of 31 would be substituted in to produce
5995 a such a position. */
5997 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5998 if (modewidth + len >= pos)
5999 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6000 GET_MODE (x),
6001 simplify_shift_const (NULL_RTX, ASHIFT,
6002 GET_MODE (x),
6003 XEXP (x, 0),
6004 modewidth - pos - len),
6005 modewidth - len);
6007 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6008 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6009 simplify_shift_const (NULL_RTX, LSHIFTRT,
6010 GET_MODE (x),
6011 XEXP (x, 0), pos),
6012 ((HOST_WIDE_INT) 1 << len) - 1);
6013 else
6014 /* Any other cases we can't handle. */
6015 return x;
6017 /* If we couldn't do this for some reason, return the original
6018 expression. */
6019 if (GET_CODE (tem) == CLOBBER)
6020 return x;
6022 return tem;
6025 /* X is a SET which contains an assignment of one object into
6026 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6027 or certain SUBREGS). If possible, convert it into a series of
6028 logical operations.
6030 We half-heartedly support variable positions, but do not at all
6031 support variable lengths. */
6033 static rtx
6034 expand_field_assignment (rtx x)
6036 rtx inner;
6037 rtx pos; /* Always counts from low bit. */
6038 int len;
6039 rtx mask, cleared, masked;
6040 enum machine_mode compute_mode;
6042 /* Loop until we find something we can't simplify. */
6043 while (1)
6045 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6046 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6048 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6049 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6050 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6052 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6053 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6055 inner = XEXP (SET_DEST (x), 0);
6056 len = INTVAL (XEXP (SET_DEST (x), 1));
6057 pos = XEXP (SET_DEST (x), 2);
6059 /* If the position is constant and spans the width of INNER,
6060 surround INNER with a USE to indicate this. */
6061 if (GET_CODE (pos) == CONST_INT
6062 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6063 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
6065 if (BITS_BIG_ENDIAN)
6067 if (GET_CODE (pos) == CONST_INT)
6068 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6069 - INTVAL (pos));
6070 else if (GET_CODE (pos) == MINUS
6071 && GET_CODE (XEXP (pos, 1)) == CONST_INT
6072 && (INTVAL (XEXP (pos, 1))
6073 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6074 /* If position is ADJUST - X, new position is X. */
6075 pos = XEXP (pos, 0);
6076 else
6077 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6078 GEN_INT (GET_MODE_BITSIZE (
6079 GET_MODE (inner))
6080 - len),
6081 pos);
6085 /* A SUBREG between two modes that occupy the same numbers of words
6086 can be done by moving the SUBREG to the source. */
6087 else if (GET_CODE (SET_DEST (x)) == SUBREG
6088 /* We need SUBREGs to compute nonzero_bits properly. */
6089 && nonzero_sign_valid
6090 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6091 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6092 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6093 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6095 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6096 gen_lowpart
6097 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6098 SET_SRC (x)));
6099 continue;
6101 else
6102 break;
6104 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6105 inner = SUBREG_REG (inner);
6107 compute_mode = GET_MODE (inner);
6109 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6110 if (! SCALAR_INT_MODE_P (compute_mode))
6112 enum machine_mode imode;
6114 /* Don't do anything for vector or complex integral types. */
6115 if (! FLOAT_MODE_P (compute_mode))
6116 break;
6118 /* Try to find an integral mode to pun with. */
6119 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6120 if (imode == BLKmode)
6121 break;
6123 compute_mode = imode;
6124 inner = gen_lowpart (imode, inner);
6127 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6128 if (len >= HOST_BITS_PER_WIDE_INT)
6129 break;
6131 /* Now compute the equivalent expression. Make a copy of INNER
6132 for the SET_DEST in case it is a MEM into which we will substitute;
6133 we don't want shared RTL in that case. */
6134 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6135 cleared = simplify_gen_binary (AND, compute_mode,
6136 simplify_gen_unary (NOT, compute_mode,
6137 simplify_gen_binary (ASHIFT,
6138 compute_mode,
6139 mask, pos),
6140 compute_mode),
6141 inner);
6142 masked = simplify_gen_binary (ASHIFT, compute_mode,
6143 simplify_gen_binary (
6144 AND, compute_mode,
6145 gen_lowpart (compute_mode, SET_SRC (x)),
6146 mask),
6147 pos);
6149 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6150 simplify_gen_binary (IOR, compute_mode,
6151 cleared, masked));
6154 return x;
6157 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6158 it is an RTX that represents a variable starting position; otherwise,
6159 POS is the (constant) starting bit position (counted from the LSB).
6161 INNER may be a USE. This will occur when we started with a bitfield
6162 that went outside the boundary of the object in memory, which is
6163 allowed on most machines. To isolate this case, we produce a USE
6164 whose mode is wide enough and surround the MEM with it. The only
6165 code that understands the USE is this routine. If it is not removed,
6166 it will cause the resulting insn not to match.
6168 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6169 signed reference.
6171 IN_DEST is nonzero if this is a reference in the destination of a
6172 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6173 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6174 be used.
6176 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6177 ZERO_EXTRACT should be built even for bits starting at bit 0.
6179 MODE is the desired mode of the result (if IN_DEST == 0).
6181 The result is an RTX for the extraction or NULL_RTX if the target
6182 can't handle it. */
6184 static rtx
6185 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6186 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6187 int in_dest, int in_compare)
6189 /* This mode describes the size of the storage area
6190 to fetch the overall value from. Within that, we
6191 ignore the POS lowest bits, etc. */
6192 enum machine_mode is_mode = GET_MODE (inner);
6193 enum machine_mode inner_mode;
6194 enum machine_mode wanted_inner_mode = byte_mode;
6195 enum machine_mode wanted_inner_reg_mode = word_mode;
6196 enum machine_mode pos_mode = word_mode;
6197 enum machine_mode extraction_mode = word_mode;
6198 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6199 int spans_byte = 0;
6200 rtx new = 0;
6201 rtx orig_pos_rtx = pos_rtx;
6202 HOST_WIDE_INT orig_pos;
6204 /* Get some information about INNER and get the innermost object. */
6205 if (GET_CODE (inner) == USE)
6206 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
6207 /* We don't need to adjust the position because we set up the USE
6208 to pretend that it was a full-word object. */
6209 spans_byte = 1, inner = XEXP (inner, 0);
6210 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6212 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6213 consider just the QI as the memory to extract from.
6214 The subreg adds or removes high bits; its mode is
6215 irrelevant to the meaning of this extraction,
6216 since POS and LEN count from the lsb. */
6217 if (MEM_P (SUBREG_REG (inner)))
6218 is_mode = GET_MODE (SUBREG_REG (inner));
6219 inner = SUBREG_REG (inner);
6221 else if (GET_CODE (inner) == ASHIFT
6222 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6223 && pos_rtx == 0 && pos == 0
6224 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6226 /* We're extracting the least significant bits of an rtx
6227 (ashift X (const_int C)), where LEN > C. Extract the
6228 least significant (LEN - C) bits of X, giving an rtx
6229 whose mode is MODE, then shift it left C times. */
6230 new = make_extraction (mode, XEXP (inner, 0),
6231 0, 0, len - INTVAL (XEXP (inner, 1)),
6232 unsignedp, in_dest, in_compare);
6233 if (new != 0)
6234 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6237 inner_mode = GET_MODE (inner);
6239 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6240 pos = INTVAL (pos_rtx), pos_rtx = 0;
6242 /* See if this can be done without an extraction. We never can if the
6243 width of the field is not the same as that of some integer mode. For
6244 registers, we can only avoid the extraction if the position is at the
6245 low-order bit and this is either not in the destination or we have the
6246 appropriate STRICT_LOW_PART operation available.
6248 For MEM, we can avoid an extract if the field starts on an appropriate
6249 boundary and we can change the mode of the memory reference. However,
6250 we cannot directly access the MEM if we have a USE and the underlying
6251 MEM is not TMODE. This combination means that MEM was being used in a
6252 context where bits outside its mode were being referenced; that is only
6253 valid in bit-field insns. */
6255 if (tmode != BLKmode
6256 && ! (spans_byte && inner_mode != tmode)
6257 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6258 && !MEM_P (inner)
6259 && (! in_dest
6260 || (REG_P (inner)
6261 && have_insn_for (STRICT_LOW_PART, tmode))))
6262 || (MEM_P (inner) && pos_rtx == 0
6263 && (pos
6264 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6265 : BITS_PER_UNIT)) == 0
6266 /* We can't do this if we are widening INNER_MODE (it
6267 may not be aligned, for one thing). */
6268 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6269 && (inner_mode == tmode
6270 || (! mode_dependent_address_p (XEXP (inner, 0))
6271 && ! MEM_VOLATILE_P (inner))))))
6273 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6274 field. If the original and current mode are the same, we need not
6275 adjust the offset. Otherwise, we do if bytes big endian.
6277 If INNER is not a MEM, get a piece consisting of just the field
6278 of interest (in this case POS % BITS_PER_WORD must be 0). */
6280 if (MEM_P (inner))
6282 HOST_WIDE_INT offset;
6284 /* POS counts from lsb, but make OFFSET count in memory order. */
6285 if (BYTES_BIG_ENDIAN)
6286 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6287 else
6288 offset = pos / BITS_PER_UNIT;
6290 new = adjust_address_nv (inner, tmode, offset);
6292 else if (REG_P (inner))
6294 if (tmode != inner_mode)
6296 /* We can't call gen_lowpart in a DEST since we
6297 always want a SUBREG (see below) and it would sometimes
6298 return a new hard register. */
6299 if (pos || in_dest)
6301 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6303 if (WORDS_BIG_ENDIAN
6304 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6305 final_word = ((GET_MODE_SIZE (inner_mode)
6306 - GET_MODE_SIZE (tmode))
6307 / UNITS_PER_WORD) - final_word;
6309 final_word *= UNITS_PER_WORD;
6310 if (BYTES_BIG_ENDIAN &&
6311 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6312 final_word += (GET_MODE_SIZE (inner_mode)
6313 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6315 /* Avoid creating invalid subregs, for example when
6316 simplifying (x>>32)&255. */
6317 if (final_word >= GET_MODE_SIZE (inner_mode))
6318 return NULL_RTX;
6320 new = gen_rtx_SUBREG (tmode, inner, final_word);
6322 else
6323 new = gen_lowpart (tmode, inner);
6325 else
6326 new = inner;
6328 else
6329 new = force_to_mode (inner, tmode,
6330 len >= HOST_BITS_PER_WIDE_INT
6331 ? ~(unsigned HOST_WIDE_INT) 0
6332 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6333 NULL_RTX, 0);
6335 /* If this extraction is going into the destination of a SET,
6336 make a STRICT_LOW_PART unless we made a MEM. */
6338 if (in_dest)
6339 return (MEM_P (new) ? new
6340 : (GET_CODE (new) != SUBREG
6341 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6342 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6344 if (mode == tmode)
6345 return new;
6347 if (GET_CODE (new) == CONST_INT)
6348 return gen_int_mode (INTVAL (new), mode);
6350 /* If we know that no extraneous bits are set, and that the high
6351 bit is not set, convert the extraction to the cheaper of
6352 sign and zero extension, that are equivalent in these cases. */
6353 if (flag_expensive_optimizations
6354 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6355 && ((nonzero_bits (new, tmode)
6356 & ~(((unsigned HOST_WIDE_INT)
6357 GET_MODE_MASK (tmode))
6358 >> 1))
6359 == 0)))
6361 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6362 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6364 /* Prefer ZERO_EXTENSION, since it gives more information to
6365 backends. */
6366 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6367 return temp;
6368 return temp1;
6371 /* Otherwise, sign- or zero-extend unless we already are in the
6372 proper mode. */
6374 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6375 mode, new));
6378 /* Unless this is a COMPARE or we have a funny memory reference,
6379 don't do anything with zero-extending field extracts starting at
6380 the low-order bit since they are simple AND operations. */
6381 if (pos_rtx == 0 && pos == 0 && ! in_dest
6382 && ! in_compare && ! spans_byte && unsignedp)
6383 return 0;
6385 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6386 we would be spanning bytes or if the position is not a constant and the
6387 length is not 1. In all other cases, we would only be going outside
6388 our object in cases when an original shift would have been
6389 undefined. */
6390 if (! spans_byte && MEM_P (inner)
6391 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6392 || (pos_rtx != 0 && len != 1)))
6393 return 0;
6395 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6396 and the mode for the result. */
6397 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6399 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6400 pos_mode = mode_for_extraction (EP_insv, 2);
6401 extraction_mode = mode_for_extraction (EP_insv, 3);
6404 if (! in_dest && unsignedp
6405 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6407 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6408 pos_mode = mode_for_extraction (EP_extzv, 3);
6409 extraction_mode = mode_for_extraction (EP_extzv, 0);
6412 if (! in_dest && ! unsignedp
6413 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6415 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6416 pos_mode = mode_for_extraction (EP_extv, 3);
6417 extraction_mode = mode_for_extraction (EP_extv, 0);
6420 /* Never narrow an object, since that might not be safe. */
6422 if (mode != VOIDmode
6423 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6424 extraction_mode = mode;
6426 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6427 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6428 pos_mode = GET_MODE (pos_rtx);
6430 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6431 if we have to change the mode of memory and cannot, the desired mode is
6432 EXTRACTION_MODE. */
6433 if (!MEM_P (inner))
6434 wanted_inner_mode = wanted_inner_reg_mode;
6435 else if (inner_mode != wanted_inner_mode
6436 && (mode_dependent_address_p (XEXP (inner, 0))
6437 || MEM_VOLATILE_P (inner)))
6438 wanted_inner_mode = extraction_mode;
6440 orig_pos = pos;
6442 if (BITS_BIG_ENDIAN)
6444 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6445 BITS_BIG_ENDIAN style. If position is constant, compute new
6446 position. Otherwise, build subtraction.
6447 Note that POS is relative to the mode of the original argument.
6448 If it's a MEM we need to recompute POS relative to that.
6449 However, if we're extracting from (or inserting into) a register,
6450 we want to recompute POS relative to wanted_inner_mode. */
6451 int width = (MEM_P (inner)
6452 ? GET_MODE_BITSIZE (is_mode)
6453 : GET_MODE_BITSIZE (wanted_inner_mode));
6455 if (pos_rtx == 0)
6456 pos = width - len - pos;
6457 else
6458 pos_rtx
6459 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6460 /* POS may be less than 0 now, but we check for that below.
6461 Note that it can only be less than 0 if !MEM_P (inner). */
6464 /* If INNER has a wider mode, make it smaller. If this is a constant
6465 extract, try to adjust the byte to point to the byte containing
6466 the value. */
6467 if (wanted_inner_mode != VOIDmode
6468 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6469 && ((MEM_P (inner)
6470 && (inner_mode == wanted_inner_mode
6471 || (! mode_dependent_address_p (XEXP (inner, 0))
6472 && ! MEM_VOLATILE_P (inner))))))
6474 int offset = 0;
6476 /* The computations below will be correct if the machine is big
6477 endian in both bits and bytes or little endian in bits and bytes.
6478 If it is mixed, we must adjust. */
6480 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6481 adjust OFFSET to compensate. */
6482 if (BYTES_BIG_ENDIAN
6483 && ! spans_byte
6484 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6485 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6487 /* If this is a constant position, we can move to the desired byte.
6488 Be careful not to go beyond the original object. */
6489 if (pos_rtx == 0)
6491 enum machine_mode bfmode = smallest_mode_for_size (len, MODE_INT);
6492 offset += pos / GET_MODE_BITSIZE (bfmode);
6493 pos %= GET_MODE_BITSIZE (bfmode);
6496 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6497 && ! spans_byte
6498 && is_mode != wanted_inner_mode)
6499 offset = (GET_MODE_SIZE (is_mode)
6500 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6502 if (offset != 0 || inner_mode != wanted_inner_mode)
6503 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6506 /* If INNER is not memory, we can always get it into the proper mode. If we
6507 are changing its mode, POS must be a constant and smaller than the size
6508 of the new mode. */
6509 else if (!MEM_P (inner))
6511 if (GET_MODE (inner) != wanted_inner_mode
6512 && (pos_rtx != 0
6513 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6514 return 0;
6516 inner = force_to_mode (inner, wanted_inner_mode,
6517 pos_rtx
6518 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6519 ? ~(unsigned HOST_WIDE_INT) 0
6520 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6521 << orig_pos),
6522 NULL_RTX, 0);
6525 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6526 have to zero extend. Otherwise, we can just use a SUBREG. */
6527 if (pos_rtx != 0
6528 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6530 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6532 /* If we know that no extraneous bits are set, and that the high
6533 bit is not set, convert extraction to cheaper one - either
6534 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6535 cases. */
6536 if (flag_expensive_optimizations
6537 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6538 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6539 & ~(((unsigned HOST_WIDE_INT)
6540 GET_MODE_MASK (GET_MODE (pos_rtx)))
6541 >> 1))
6542 == 0)))
6544 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6546 /* Prefer ZERO_EXTENSION, since it gives more information to
6547 backends. */
6548 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6549 temp = temp1;
6551 pos_rtx = temp;
6553 else if (pos_rtx != 0
6554 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6555 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6557 /* Make POS_RTX unless we already have it and it is correct. If we don't
6558 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6559 be a CONST_INT. */
6560 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6561 pos_rtx = orig_pos_rtx;
6563 else if (pos_rtx == 0)
6564 pos_rtx = GEN_INT (pos);
6566 /* Make the required operation. See if we can use existing rtx. */
6567 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6568 extraction_mode, inner, GEN_INT (len), pos_rtx);
6569 if (! in_dest)
6570 new = gen_lowpart (mode, new);
6572 return new;
6575 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6576 with any other operations in X. Return X without that shift if so. */
6578 static rtx
6579 extract_left_shift (rtx x, int count)
6581 enum rtx_code code = GET_CODE (x);
6582 enum machine_mode mode = GET_MODE (x);
6583 rtx tem;
6585 switch (code)
6587 case ASHIFT:
6588 /* This is the shift itself. If it is wide enough, we will return
6589 either the value being shifted if the shift count is equal to
6590 COUNT or a shift for the difference. */
6591 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6592 && INTVAL (XEXP (x, 1)) >= count)
6593 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6594 INTVAL (XEXP (x, 1)) - count);
6595 break;
6597 case NEG: case NOT:
6598 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6599 return simplify_gen_unary (code, mode, tem, mode);
6601 break;
6603 case PLUS: case IOR: case XOR: case AND:
6604 /* If we can safely shift this constant and we find the inner shift,
6605 make a new operation. */
6606 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6607 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6608 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6609 return simplify_gen_binary (code, mode, tem,
6610 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6612 break;
6614 default:
6615 break;
6618 return 0;
6621 /* Look at the expression rooted at X. Look for expressions
6622 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6623 Form these expressions.
6625 Return the new rtx, usually just X.
6627 Also, for machines like the VAX that don't have logical shift insns,
6628 try to convert logical to arithmetic shift operations in cases where
6629 they are equivalent. This undoes the canonicalizations to logical
6630 shifts done elsewhere.
6632 We try, as much as possible, to re-use rtl expressions to save memory.
6634 IN_CODE says what kind of expression we are processing. Normally, it is
6635 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6636 being kludges), it is MEM. When processing the arguments of a comparison
6637 or a COMPARE against zero, it is COMPARE. */
6639 static rtx
6640 make_compound_operation (rtx x, enum rtx_code in_code)
6642 enum rtx_code code = GET_CODE (x);
6643 enum machine_mode mode = GET_MODE (x);
6644 int mode_width = GET_MODE_BITSIZE (mode);
6645 rtx rhs, lhs;
6646 enum rtx_code next_code;
6647 int i;
6648 rtx new = 0;
6649 rtx tem;
6650 const char *fmt;
6652 /* Select the code to be used in recursive calls. Once we are inside an
6653 address, we stay there. If we have a comparison, set to COMPARE,
6654 but once inside, go back to our default of SET. */
6656 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6657 : ((code == COMPARE || COMPARISON_P (x))
6658 && XEXP (x, 1) == const0_rtx) ? COMPARE
6659 : in_code == COMPARE ? SET : in_code);
6661 /* Process depending on the code of this operation. If NEW is set
6662 nonzero, it will be returned. */
6664 switch (code)
6666 case ASHIFT:
6667 /* Convert shifts by constants into multiplications if inside
6668 an address. */
6669 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6670 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6671 && INTVAL (XEXP (x, 1)) >= 0)
6673 new = make_compound_operation (XEXP (x, 0), next_code);
6674 new = gen_rtx_MULT (mode, new,
6675 GEN_INT ((HOST_WIDE_INT) 1
6676 << INTVAL (XEXP (x, 1))));
6678 break;
6680 case AND:
6681 /* If the second operand is not a constant, we can't do anything
6682 with it. */
6683 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6684 break;
6686 /* If the constant is a power of two minus one and the first operand
6687 is a logical right shift, make an extraction. */
6688 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6689 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6691 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6692 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6693 0, in_code == COMPARE);
6696 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6697 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6698 && subreg_lowpart_p (XEXP (x, 0))
6699 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6700 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6702 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6703 next_code);
6704 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6705 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6706 0, in_code == COMPARE);
6708 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6709 else if ((GET_CODE (XEXP (x, 0)) == XOR
6710 || GET_CODE (XEXP (x, 0)) == IOR)
6711 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6712 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6713 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6715 /* Apply the distributive law, and then try to make extractions. */
6716 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6717 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6718 XEXP (x, 1)),
6719 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6720 XEXP (x, 1)));
6721 new = make_compound_operation (new, in_code);
6724 /* If we are have (and (rotate X C) M) and C is larger than the number
6725 of bits in M, this is an extraction. */
6727 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6728 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6729 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6730 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6732 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6733 new = make_extraction (mode, new,
6734 (GET_MODE_BITSIZE (mode)
6735 - INTVAL (XEXP (XEXP (x, 0), 1))),
6736 NULL_RTX, i, 1, 0, in_code == COMPARE);
6739 /* On machines without logical shifts, if the operand of the AND is
6740 a logical shift and our mask turns off all the propagated sign
6741 bits, we can replace the logical shift with an arithmetic shift. */
6742 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6743 && !have_insn_for (LSHIFTRT, mode)
6744 && have_insn_for (ASHIFTRT, mode)
6745 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6746 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6747 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6748 && mode_width <= HOST_BITS_PER_WIDE_INT)
6750 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6752 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6753 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6754 SUBST (XEXP (x, 0),
6755 gen_rtx_ASHIFTRT (mode,
6756 make_compound_operation
6757 (XEXP (XEXP (x, 0), 0), next_code),
6758 XEXP (XEXP (x, 0), 1)));
6761 /* If the constant is one less than a power of two, this might be
6762 representable by an extraction even if no shift is present.
6763 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6764 we are in a COMPARE. */
6765 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6766 new = make_extraction (mode,
6767 make_compound_operation (XEXP (x, 0),
6768 next_code),
6769 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6771 /* If we are in a comparison and this is an AND with a power of two,
6772 convert this into the appropriate bit extract. */
6773 else if (in_code == COMPARE
6774 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6775 new = make_extraction (mode,
6776 make_compound_operation (XEXP (x, 0),
6777 next_code),
6778 i, NULL_RTX, 1, 1, 0, 1);
6780 break;
6782 case LSHIFTRT:
6783 /* If the sign bit is known to be zero, replace this with an
6784 arithmetic shift. */
6785 if (have_insn_for (ASHIFTRT, mode)
6786 && ! have_insn_for (LSHIFTRT, mode)
6787 && mode_width <= HOST_BITS_PER_WIDE_INT
6788 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6790 new = gen_rtx_ASHIFTRT (mode,
6791 make_compound_operation (XEXP (x, 0),
6792 next_code),
6793 XEXP (x, 1));
6794 break;
6797 /* ... fall through ... */
6799 case ASHIFTRT:
6800 lhs = XEXP (x, 0);
6801 rhs = XEXP (x, 1);
6803 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6804 this is a SIGN_EXTRACT. */
6805 if (GET_CODE (rhs) == CONST_INT
6806 && GET_CODE (lhs) == ASHIFT
6807 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6808 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6810 new = make_compound_operation (XEXP (lhs, 0), next_code);
6811 new = make_extraction (mode, new,
6812 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6813 NULL_RTX, mode_width - INTVAL (rhs),
6814 code == LSHIFTRT, 0, in_code == COMPARE);
6815 break;
6818 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6819 If so, try to merge the shifts into a SIGN_EXTEND. We could
6820 also do this for some cases of SIGN_EXTRACT, but it doesn't
6821 seem worth the effort; the case checked for occurs on Alpha. */
6823 if (!OBJECT_P (lhs)
6824 && ! (GET_CODE (lhs) == SUBREG
6825 && (OBJECT_P (SUBREG_REG (lhs))))
6826 && GET_CODE (rhs) == CONST_INT
6827 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6828 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6829 new = make_extraction (mode, make_compound_operation (new, next_code),
6830 0, NULL_RTX, mode_width - INTVAL (rhs),
6831 code == LSHIFTRT, 0, in_code == COMPARE);
6833 break;
6835 case SUBREG:
6836 /* Call ourselves recursively on the inner expression. If we are
6837 narrowing the object and it has a different RTL code from
6838 what it originally did, do this SUBREG as a force_to_mode. */
6840 tem = make_compound_operation (SUBREG_REG (x), in_code);
6843 rtx simplified;
6844 simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6845 SUBREG_BYTE (x));
6847 if (simplified)
6848 tem = simplified;
6850 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6851 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6852 && subreg_lowpart_p (x))
6854 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6855 NULL_RTX, 0);
6857 /* If we have something other than a SUBREG, we might have
6858 done an expansion, so rerun ourselves. */
6859 if (GET_CODE (newer) != SUBREG)
6860 newer = make_compound_operation (newer, in_code);
6862 return newer;
6865 if (simplified)
6866 return tem;
6868 break;
6870 default:
6871 break;
6874 if (new)
6876 x = gen_lowpart (mode, new);
6877 code = GET_CODE (x);
6880 /* Now recursively process each operand of this operation. */
6881 fmt = GET_RTX_FORMAT (code);
6882 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6883 if (fmt[i] == 'e')
6885 new = make_compound_operation (XEXP (x, i), next_code);
6886 SUBST (XEXP (x, i), new);
6889 return x;
6892 /* Given M see if it is a value that would select a field of bits
6893 within an item, but not the entire word. Return -1 if not.
6894 Otherwise, return the starting position of the field, where 0 is the
6895 low-order bit.
6897 *PLEN is set to the length of the field. */
6899 static int
6900 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6902 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6903 int pos = exact_log2 (m & -m);
6904 int len = 0;
6906 if (pos >= 0)
6907 /* Now shift off the low-order zero bits and see if we have a
6908 power of two minus 1. */
6909 len = exact_log2 ((m >> pos) + 1);
6911 if (len <= 0)
6912 pos = -1;
6914 *plen = len;
6915 return pos;
6918 /* See if X can be simplified knowing that we will only refer to it in
6919 MODE and will only refer to those bits that are nonzero in MASK.
6920 If other bits are being computed or if masking operations are done
6921 that select a superset of the bits in MASK, they can sometimes be
6922 ignored.
6924 Return a possibly simplified expression, but always convert X to
6925 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6927 Also, if REG is nonzero and X is a register equal in value to REG,
6928 replace X with REG.
6930 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6931 are all off in X. This is used when X will be complemented, by either
6932 NOT, NEG, or XOR. */
6934 static rtx
6935 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6936 rtx reg, int just_select)
6938 enum rtx_code code = GET_CODE (x);
6939 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6940 enum machine_mode op_mode;
6941 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6942 rtx op0, op1, temp;
6944 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6945 code below will do the wrong thing since the mode of such an
6946 expression is VOIDmode.
6948 Also do nothing if X is a CLOBBER; this can happen if X was
6949 the return value from a call to gen_lowpart. */
6950 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6951 return x;
6953 /* We want to perform the operation is its present mode unless we know
6954 that the operation is valid in MODE, in which case we do the operation
6955 in MODE. */
6956 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6957 && have_insn_for (code, mode))
6958 ? mode : GET_MODE (x));
6960 /* It is not valid to do a right-shift in a narrower mode
6961 than the one it came in with. */
6962 if ((code == LSHIFTRT || code == ASHIFTRT)
6963 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6964 op_mode = GET_MODE (x);
6966 /* Truncate MASK to fit OP_MODE. */
6967 if (op_mode)
6968 mask &= GET_MODE_MASK (op_mode);
6970 /* When we have an arithmetic operation, or a shift whose count we
6971 do not know, we need to assume that all bits up to the highest-order
6972 bit in MASK will be needed. This is how we form such a mask. */
6973 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6974 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6975 else
6976 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6977 - 1);
6979 /* Determine what bits of X are guaranteed to be (non)zero. */
6980 nonzero = nonzero_bits (x, mode);
6982 /* If none of the bits in X are needed, return a zero. */
6983 if (! just_select && (nonzero & mask) == 0)
6984 x = const0_rtx;
6986 /* If X is a CONST_INT, return a new one. Do this here since the
6987 test below will fail. */
6988 if (GET_CODE (x) == CONST_INT)
6990 if (SCALAR_INT_MODE_P (mode))
6991 return gen_int_mode (INTVAL (x) & mask, mode);
6992 else
6994 x = GEN_INT (INTVAL (x) & mask);
6995 return gen_lowpart_common (mode, x);
6999 /* If X is narrower than MODE and we want all the bits in X's mode, just
7000 get X in the proper mode. */
7001 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7002 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7003 return gen_lowpart (mode, x);
7005 switch (code)
7007 case CLOBBER:
7008 /* If X is a (clobber (const_int)), return it since we know we are
7009 generating something that won't match. */
7010 return x;
7012 case USE:
7013 /* X is a (use (mem ..)) that was made from a bit-field extraction that
7014 spanned the boundary of the MEM. If we are now masking so it is
7015 within that boundary, we don't need the USE any more. */
7016 if (! BITS_BIG_ENDIAN
7017 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7018 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7019 break;
7021 case SIGN_EXTEND:
7022 case ZERO_EXTEND:
7023 case ZERO_EXTRACT:
7024 case SIGN_EXTRACT:
7025 x = expand_compound_operation (x);
7026 if (GET_CODE (x) != code)
7027 return force_to_mode (x, mode, mask, reg, next_select);
7028 break;
7030 case REG:
7031 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
7032 || rtx_equal_p (reg, get_last_value (x))))
7033 x = reg;
7034 break;
7036 case SUBREG:
7037 if (subreg_lowpart_p (x)
7038 /* We can ignore the effect of this SUBREG if it narrows the mode or
7039 if the constant masks to zero all the bits the mode doesn't
7040 have. */
7041 && ((GET_MODE_SIZE (GET_MODE (x))
7042 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7043 || (0 == (mask
7044 & GET_MODE_MASK (GET_MODE (x))
7045 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7046 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
7047 break;
7049 case AND:
7050 /* If this is an AND with a constant, convert it into an AND
7051 whose constant is the AND of that constant with MASK. If it
7052 remains an AND of MASK, delete it since it is redundant. */
7054 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7056 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7057 mask & INTVAL (XEXP (x, 1)));
7059 /* If X is still an AND, see if it is an AND with a mask that
7060 is just some low-order bits. If so, and it is MASK, we don't
7061 need it. */
7063 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7064 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7065 == mask))
7066 x = XEXP (x, 0);
7068 /* If it remains an AND, try making another AND with the bits
7069 in the mode mask that aren't in MASK turned on. If the
7070 constant in the AND is wide enough, this might make a
7071 cheaper constant. */
7073 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7074 && GET_MODE_MASK (GET_MODE (x)) != mask
7075 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7077 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7078 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7079 int width = GET_MODE_BITSIZE (GET_MODE (x));
7080 rtx y;
7082 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7083 number, sign extend it. */
7084 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7085 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7086 cval |= (HOST_WIDE_INT) -1 << width;
7088 y = simplify_gen_binary (AND, GET_MODE (x),
7089 XEXP (x, 0), GEN_INT (cval));
7090 if (rtx_cost (y, SET) < rtx_cost (x, SET))
7091 x = y;
7094 break;
7097 goto binop;
7099 case PLUS:
7100 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7101 low-order bits (as in an alignment operation) and FOO is already
7102 aligned to that boundary, mask C1 to that boundary as well.
7103 This may eliminate that PLUS and, later, the AND. */
7106 unsigned int width = GET_MODE_BITSIZE (mode);
7107 unsigned HOST_WIDE_INT smask = mask;
7109 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7110 number, sign extend it. */
7112 if (width < HOST_BITS_PER_WIDE_INT
7113 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7114 smask |= (HOST_WIDE_INT) -1 << width;
7116 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7117 && exact_log2 (- smask) >= 0
7118 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7119 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7120 return force_to_mode (plus_constant (XEXP (x, 0),
7121 (INTVAL (XEXP (x, 1)) & smask)),
7122 mode, smask, reg, next_select);
7125 /* ... fall through ... */
7127 case MULT:
7128 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7129 most significant bit in MASK since carries from those bits will
7130 affect the bits we are interested in. */
7131 mask = fuller_mask;
7132 goto binop;
7134 case MINUS:
7135 /* If X is (minus C Y) where C's least set bit is larger than any bit
7136 in the mask, then we may replace with (neg Y). */
7137 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7138 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7139 & -INTVAL (XEXP (x, 0))))
7140 > mask))
7142 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7143 GET_MODE (x));
7144 return force_to_mode (x, mode, mask, reg, next_select);
7147 /* Similarly, if C contains every bit in the fuller_mask, then we may
7148 replace with (not Y). */
7149 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7150 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7151 == INTVAL (XEXP (x, 0))))
7153 x = simplify_gen_unary (NOT, GET_MODE (x),
7154 XEXP (x, 1), GET_MODE (x));
7155 return force_to_mode (x, mode, mask, reg, next_select);
7158 mask = fuller_mask;
7159 goto binop;
7161 case IOR:
7162 case XOR:
7163 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7164 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7165 operation which may be a bitfield extraction. Ensure that the
7166 constant we form is not wider than the mode of X. */
7168 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7169 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7170 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7171 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7172 && GET_CODE (XEXP (x, 1)) == CONST_INT
7173 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7174 + floor_log2 (INTVAL (XEXP (x, 1))))
7175 < GET_MODE_BITSIZE (GET_MODE (x)))
7176 && (INTVAL (XEXP (x, 1))
7177 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7179 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7180 << INTVAL (XEXP (XEXP (x, 0), 1)));
7181 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7182 XEXP (XEXP (x, 0), 0), temp);
7183 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7184 XEXP (XEXP (x, 0), 1));
7185 return force_to_mode (x, mode, mask, reg, next_select);
7188 binop:
7189 /* For most binary operations, just propagate into the operation and
7190 change the mode if we have an operation of that mode. */
7192 op0 = gen_lowpart (op_mode,
7193 force_to_mode (XEXP (x, 0), mode, mask,
7194 reg, next_select));
7195 op1 = gen_lowpart (op_mode,
7196 force_to_mode (XEXP (x, 1), mode, mask,
7197 reg, next_select));
7199 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7200 x = simplify_gen_binary (code, op_mode, op0, op1);
7201 break;
7203 case ASHIFT:
7204 /* For left shifts, do the same, but just for the first operand.
7205 However, we cannot do anything with shifts where we cannot
7206 guarantee that the counts are smaller than the size of the mode
7207 because such a count will have a different meaning in a
7208 wider mode. */
7210 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7211 && INTVAL (XEXP (x, 1)) >= 0
7212 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7213 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7214 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7215 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7216 break;
7218 /* If the shift count is a constant and we can do arithmetic in
7219 the mode of the shift, refine which bits we need. Otherwise, use the
7220 conservative form of the mask. */
7221 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7222 && INTVAL (XEXP (x, 1)) >= 0
7223 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7224 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7225 mask >>= INTVAL (XEXP (x, 1));
7226 else
7227 mask = fuller_mask;
7229 op0 = gen_lowpart (op_mode,
7230 force_to_mode (XEXP (x, 0), op_mode,
7231 mask, reg, next_select));
7233 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7234 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7235 break;
7237 case LSHIFTRT:
7238 /* Here we can only do something if the shift count is a constant,
7239 this shift constant is valid for the host, and we can do arithmetic
7240 in OP_MODE. */
7242 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7243 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7244 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7246 rtx inner = XEXP (x, 0);
7247 unsigned HOST_WIDE_INT inner_mask;
7249 /* Select the mask of the bits we need for the shift operand. */
7250 inner_mask = mask << INTVAL (XEXP (x, 1));
7252 /* We can only change the mode of the shift if we can do arithmetic
7253 in the mode of the shift and INNER_MASK is no wider than the
7254 width of X's mode. */
7255 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7256 op_mode = GET_MODE (x);
7258 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7260 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7261 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7264 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7265 shift and AND produces only copies of the sign bit (C2 is one less
7266 than a power of two), we can do this with just a shift. */
7268 if (GET_CODE (x) == LSHIFTRT
7269 && GET_CODE (XEXP (x, 1)) == CONST_INT
7270 /* The shift puts one of the sign bit copies in the least significant
7271 bit. */
7272 && ((INTVAL (XEXP (x, 1))
7273 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7274 >= GET_MODE_BITSIZE (GET_MODE (x)))
7275 && exact_log2 (mask + 1) >= 0
7276 /* Number of bits left after the shift must be more than the mask
7277 needs. */
7278 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7279 <= GET_MODE_BITSIZE (GET_MODE (x)))
7280 /* Must be more sign bit copies than the mask needs. */
7281 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7282 >= exact_log2 (mask + 1)))
7283 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7284 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7285 - exact_log2 (mask + 1)));
7287 goto shiftrt;
7289 case ASHIFTRT:
7290 /* If we are just looking for the sign bit, we don't need this shift at
7291 all, even if it has a variable count. */
7292 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7293 && (mask == ((unsigned HOST_WIDE_INT) 1
7294 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7295 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7297 /* If this is a shift by a constant, get a mask that contains those bits
7298 that are not copies of the sign bit. We then have two cases: If
7299 MASK only includes those bits, this can be a logical shift, which may
7300 allow simplifications. If MASK is a single-bit field not within
7301 those bits, we are requesting a copy of the sign bit and hence can
7302 shift the sign bit to the appropriate location. */
7304 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7305 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7307 int i = -1;
7309 /* If the considered data is wider than HOST_WIDE_INT, we can't
7310 represent a mask for all its bits in a single scalar.
7311 But we only care about the lower bits, so calculate these. */
7313 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7315 nonzero = ~(HOST_WIDE_INT) 0;
7317 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7318 is the number of bits a full-width mask would have set.
7319 We need only shift if these are fewer than nonzero can
7320 hold. If not, we must keep all bits set in nonzero. */
7322 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7323 < HOST_BITS_PER_WIDE_INT)
7324 nonzero >>= INTVAL (XEXP (x, 1))
7325 + HOST_BITS_PER_WIDE_INT
7326 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7328 else
7330 nonzero = GET_MODE_MASK (GET_MODE (x));
7331 nonzero >>= INTVAL (XEXP (x, 1));
7334 if ((mask & ~nonzero) == 0
7335 || (i = exact_log2 (mask)) >= 0)
7337 x = simplify_shift_const
7338 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7339 i < 0 ? INTVAL (XEXP (x, 1))
7340 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7342 if (GET_CODE (x) != ASHIFTRT)
7343 return force_to_mode (x, mode, mask, reg, next_select);
7347 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7348 even if the shift count isn't a constant. */
7349 if (mask == 1)
7350 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7351 XEXP (x, 0), XEXP (x, 1));
7353 shiftrt:
7355 /* If this is a zero- or sign-extension operation that just affects bits
7356 we don't care about, remove it. Be sure the call above returned
7357 something that is still a shift. */
7359 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7360 && GET_CODE (XEXP (x, 1)) == CONST_INT
7361 && INTVAL (XEXP (x, 1)) >= 0
7362 && (INTVAL (XEXP (x, 1))
7363 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7364 && GET_CODE (XEXP (x, 0)) == ASHIFT
7365 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7366 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7367 reg, next_select);
7369 break;
7371 case ROTATE:
7372 case ROTATERT:
7373 /* If the shift count is constant and we can do computations
7374 in the mode of X, compute where the bits we care about are.
7375 Otherwise, we can't do anything. Don't change the mode of
7376 the shift or propagate MODE into the shift, though. */
7377 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7378 && INTVAL (XEXP (x, 1)) >= 0)
7380 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7381 GET_MODE (x), GEN_INT (mask),
7382 XEXP (x, 1));
7383 if (temp && GET_CODE (temp) == CONST_INT)
7384 SUBST (XEXP (x, 0),
7385 force_to_mode (XEXP (x, 0), GET_MODE (x),
7386 INTVAL (temp), reg, next_select));
7388 break;
7390 case NEG:
7391 /* If we just want the low-order bit, the NEG isn't needed since it
7392 won't change the low-order bit. */
7393 if (mask == 1)
7394 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7396 /* We need any bits less significant than the most significant bit in
7397 MASK since carries from those bits will affect the bits we are
7398 interested in. */
7399 mask = fuller_mask;
7400 goto unop;
7402 case NOT:
7403 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7404 same as the XOR case above. Ensure that the constant we form is not
7405 wider than the mode of X. */
7407 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7408 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7409 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7410 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7411 < GET_MODE_BITSIZE (GET_MODE (x)))
7412 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7414 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7415 GET_MODE (x));
7416 temp = simplify_gen_binary (XOR, GET_MODE (x),
7417 XEXP (XEXP (x, 0), 0), temp);
7418 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7419 temp, XEXP (XEXP (x, 0), 1));
7421 return force_to_mode (x, mode, mask, reg, next_select);
7424 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7425 use the full mask inside the NOT. */
7426 mask = fuller_mask;
7428 unop:
7429 op0 = gen_lowpart (op_mode,
7430 force_to_mode (XEXP (x, 0), mode, mask,
7431 reg, next_select));
7432 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7433 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7434 break;
7436 case NE:
7437 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7438 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7439 which is equal to STORE_FLAG_VALUE. */
7440 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7441 && GET_MODE (XEXP (x, 0)) == mode
7442 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7443 && (nonzero_bits (XEXP (x, 0), mode)
7444 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7445 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7447 break;
7449 case IF_THEN_ELSE:
7450 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7451 written in a narrower mode. We play it safe and do not do so. */
7453 SUBST (XEXP (x, 1),
7454 gen_lowpart (GET_MODE (x),
7455 force_to_mode (XEXP (x, 1), mode,
7456 mask, reg, next_select)));
7457 SUBST (XEXP (x, 2),
7458 gen_lowpart (GET_MODE (x),
7459 force_to_mode (XEXP (x, 2), mode,
7460 mask, reg, next_select)));
7461 break;
7463 default:
7464 break;
7467 /* Ensure we return a value of the proper mode. */
7468 return gen_lowpart (mode, x);
7471 /* Return nonzero if X is an expression that has one of two values depending on
7472 whether some other value is zero or nonzero. In that case, we return the
7473 value that is being tested, *PTRUE is set to the value if the rtx being
7474 returned has a nonzero value, and *PFALSE is set to the other alternative.
7476 If we return zero, we set *PTRUE and *PFALSE to X. */
7478 static rtx
7479 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7481 enum machine_mode mode = GET_MODE (x);
7482 enum rtx_code code = GET_CODE (x);
7483 rtx cond0, cond1, true0, true1, false0, false1;
7484 unsigned HOST_WIDE_INT nz;
7486 /* If we are comparing a value against zero, we are done. */
7487 if ((code == NE || code == EQ)
7488 && XEXP (x, 1) == const0_rtx)
7490 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7491 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7492 return XEXP (x, 0);
7495 /* If this is a unary operation whose operand has one of two values, apply
7496 our opcode to compute those values. */
7497 else if (UNARY_P (x)
7498 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7500 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7501 *pfalse = simplify_gen_unary (code, mode, false0,
7502 GET_MODE (XEXP (x, 0)));
7503 return cond0;
7506 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7507 make can't possibly match and would suppress other optimizations. */
7508 else if (code == COMPARE)
7511 /* If this is a binary operation, see if either side has only one of two
7512 values. If either one does or if both do and they are conditional on
7513 the same value, compute the new true and false values. */
7514 else if (BINARY_P (x))
7516 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7517 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7519 if ((cond0 != 0 || cond1 != 0)
7520 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7522 /* If if_then_else_cond returned zero, then true/false are the
7523 same rtl. We must copy one of them to prevent invalid rtl
7524 sharing. */
7525 if (cond0 == 0)
7526 true0 = copy_rtx (true0);
7527 else if (cond1 == 0)
7528 true1 = copy_rtx (true1);
7530 if (COMPARISON_P (x))
7532 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7533 true0, true1);
7534 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7535 false0, false1);
7537 else
7539 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7540 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7543 return cond0 ? cond0 : cond1;
7546 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7547 operands is zero when the other is nonzero, and vice-versa,
7548 and STORE_FLAG_VALUE is 1 or -1. */
7550 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7551 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7552 || code == UMAX)
7553 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7555 rtx op0 = XEXP (XEXP (x, 0), 1);
7556 rtx op1 = XEXP (XEXP (x, 1), 1);
7558 cond0 = XEXP (XEXP (x, 0), 0);
7559 cond1 = XEXP (XEXP (x, 1), 0);
7561 if (COMPARISON_P (cond0)
7562 && COMPARISON_P (cond1)
7563 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7564 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7565 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7566 || ((swap_condition (GET_CODE (cond0))
7567 == reversed_comparison_code (cond1, NULL))
7568 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7569 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7570 && ! side_effects_p (x))
7572 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7573 *pfalse = simplify_gen_binary (MULT, mode,
7574 (code == MINUS
7575 ? simplify_gen_unary (NEG, mode,
7576 op1, mode)
7577 : op1),
7578 const_true_rtx);
7579 return cond0;
7583 /* Similarly for MULT, AND and UMIN, except that for these the result
7584 is always zero. */
7585 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7586 && (code == MULT || code == AND || code == UMIN)
7587 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7589 cond0 = XEXP (XEXP (x, 0), 0);
7590 cond1 = XEXP (XEXP (x, 1), 0);
7592 if (COMPARISON_P (cond0)
7593 && COMPARISON_P (cond1)
7594 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7595 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7596 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7597 || ((swap_condition (GET_CODE (cond0))
7598 == reversed_comparison_code (cond1, NULL))
7599 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7600 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7601 && ! side_effects_p (x))
7603 *ptrue = *pfalse = const0_rtx;
7604 return cond0;
7609 else if (code == IF_THEN_ELSE)
7611 /* If we have IF_THEN_ELSE already, extract the condition and
7612 canonicalize it if it is NE or EQ. */
7613 cond0 = XEXP (x, 0);
7614 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7615 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7616 return XEXP (cond0, 0);
7617 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7619 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7620 return XEXP (cond0, 0);
7622 else
7623 return cond0;
7626 /* If X is a SUBREG, we can narrow both the true and false values
7627 if the inner expression, if there is a condition. */
7628 else if (code == SUBREG
7629 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7630 &true0, &false0)))
7632 true0 = simplify_gen_subreg (mode, true0,
7633 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7634 false0 = simplify_gen_subreg (mode, false0,
7635 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7636 if (true0 && false0)
7638 *ptrue = true0;
7639 *pfalse = false0;
7640 return cond0;
7644 /* If X is a constant, this isn't special and will cause confusions
7645 if we treat it as such. Likewise if it is equivalent to a constant. */
7646 else if (CONSTANT_P (x)
7647 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7650 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7651 will be least confusing to the rest of the compiler. */
7652 else if (mode == BImode)
7654 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7655 return x;
7658 /* If X is known to be either 0 or -1, those are the true and
7659 false values when testing X. */
7660 else if (x == constm1_rtx || x == const0_rtx
7661 || (mode != VOIDmode
7662 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7664 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7665 return x;
7668 /* Likewise for 0 or a single bit. */
7669 else if (SCALAR_INT_MODE_P (mode)
7670 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7671 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7673 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7674 return x;
7677 /* Otherwise fail; show no condition with true and false values the same. */
7678 *ptrue = *pfalse = x;
7679 return 0;
7682 /* Return the value of expression X given the fact that condition COND
7683 is known to be true when applied to REG as its first operand and VAL
7684 as its second. X is known to not be shared and so can be modified in
7685 place.
7687 We only handle the simplest cases, and specifically those cases that
7688 arise with IF_THEN_ELSE expressions. */
7690 static rtx
7691 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7693 enum rtx_code code = GET_CODE (x);
7694 rtx temp;
7695 const char *fmt;
7696 int i, j;
7698 if (side_effects_p (x))
7699 return x;
7701 /* If either operand of the condition is a floating point value,
7702 then we have to avoid collapsing an EQ comparison. */
7703 if (cond == EQ
7704 && rtx_equal_p (x, reg)
7705 && ! FLOAT_MODE_P (GET_MODE (x))
7706 && ! FLOAT_MODE_P (GET_MODE (val)))
7707 return val;
7709 if (cond == UNEQ && rtx_equal_p (x, reg))
7710 return val;
7712 /* If X is (abs REG) and we know something about REG's relationship
7713 with zero, we may be able to simplify this. */
7715 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7716 switch (cond)
7718 case GE: case GT: case EQ:
7719 return XEXP (x, 0);
7720 case LT: case LE:
7721 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7722 XEXP (x, 0),
7723 GET_MODE (XEXP (x, 0)));
7724 default:
7725 break;
7728 /* The only other cases we handle are MIN, MAX, and comparisons if the
7729 operands are the same as REG and VAL. */
7731 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7733 if (rtx_equal_p (XEXP (x, 0), val))
7734 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7736 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7738 if (COMPARISON_P (x))
7740 if (comparison_dominates_p (cond, code))
7741 return const_true_rtx;
7743 code = reversed_comparison_code (x, NULL);
7744 if (code != UNKNOWN
7745 && comparison_dominates_p (cond, code))
7746 return const0_rtx;
7747 else
7748 return x;
7750 else if (code == SMAX || code == SMIN
7751 || code == UMIN || code == UMAX)
7753 int unsignedp = (code == UMIN || code == UMAX);
7755 /* Do not reverse the condition when it is NE or EQ.
7756 This is because we cannot conclude anything about
7757 the value of 'SMAX (x, y)' when x is not equal to y,
7758 but we can when x equals y. */
7759 if ((code == SMAX || code == UMAX)
7760 && ! (cond == EQ || cond == NE))
7761 cond = reverse_condition (cond);
7763 switch (cond)
7765 case GE: case GT:
7766 return unsignedp ? x : XEXP (x, 1);
7767 case LE: case LT:
7768 return unsignedp ? x : XEXP (x, 0);
7769 case GEU: case GTU:
7770 return unsignedp ? XEXP (x, 1) : x;
7771 case LEU: case LTU:
7772 return unsignedp ? XEXP (x, 0) : x;
7773 default:
7774 break;
7779 else if (code == SUBREG)
7781 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7782 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7784 if (SUBREG_REG (x) != r)
7786 /* We must simplify subreg here, before we lose track of the
7787 original inner_mode. */
7788 new = simplify_subreg (GET_MODE (x), r,
7789 inner_mode, SUBREG_BYTE (x));
7790 if (new)
7791 return new;
7792 else
7793 SUBST (SUBREG_REG (x), r);
7796 return x;
7798 /* We don't have to handle SIGN_EXTEND here, because even in the
7799 case of replacing something with a modeless CONST_INT, a
7800 CONST_INT is already (supposed to be) a valid sign extension for
7801 its narrower mode, which implies it's already properly
7802 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7803 story is different. */
7804 else if (code == ZERO_EXTEND)
7806 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7807 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7809 if (XEXP (x, 0) != r)
7811 /* We must simplify the zero_extend here, before we lose
7812 track of the original inner_mode. */
7813 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7814 r, inner_mode);
7815 if (new)
7816 return new;
7817 else
7818 SUBST (XEXP (x, 0), r);
7821 return x;
7824 fmt = GET_RTX_FORMAT (code);
7825 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7827 if (fmt[i] == 'e')
7828 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7829 else if (fmt[i] == 'E')
7830 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7831 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7832 cond, reg, val));
7835 return x;
7838 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7839 assignment as a field assignment. */
7841 static int
7842 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7844 if (x == y || rtx_equal_p (x, y))
7845 return 1;
7847 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7848 return 0;
7850 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7851 Note that all SUBREGs of MEM are paradoxical; otherwise they
7852 would have been rewritten. */
7853 if (MEM_P (x) && GET_CODE (y) == SUBREG
7854 && MEM_P (SUBREG_REG (y))
7855 && rtx_equal_p (SUBREG_REG (y),
7856 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7857 return 1;
7859 if (MEM_P (y) && GET_CODE (x) == SUBREG
7860 && MEM_P (SUBREG_REG (x))
7861 && rtx_equal_p (SUBREG_REG (x),
7862 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7863 return 1;
7865 /* We used to see if get_last_value of X and Y were the same but that's
7866 not correct. In one direction, we'll cause the assignment to have
7867 the wrong destination and in the case, we'll import a register into this
7868 insn that might have already have been dead. So fail if none of the
7869 above cases are true. */
7870 return 0;
7873 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7874 Return that assignment if so.
7876 We only handle the most common cases. */
7878 static rtx
7879 make_field_assignment (rtx x)
7881 rtx dest = SET_DEST (x);
7882 rtx src = SET_SRC (x);
7883 rtx assign;
7884 rtx rhs, lhs;
7885 HOST_WIDE_INT c1;
7886 HOST_WIDE_INT pos;
7887 unsigned HOST_WIDE_INT len;
7888 rtx other;
7889 enum machine_mode mode;
7891 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7892 a clear of a one-bit field. We will have changed it to
7893 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7894 for a SUBREG. */
7896 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7897 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7898 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7899 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7901 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7902 1, 1, 1, 0);
7903 if (assign != 0)
7904 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7905 return x;
7908 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7909 && subreg_lowpart_p (XEXP (src, 0))
7910 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7911 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7912 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7913 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7914 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7915 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7917 assign = make_extraction (VOIDmode, dest, 0,
7918 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7919 1, 1, 1, 0);
7920 if (assign != 0)
7921 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7922 return x;
7925 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7926 one-bit field. */
7927 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7928 && XEXP (XEXP (src, 0), 0) == const1_rtx
7929 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7931 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7932 1, 1, 1, 0);
7933 if (assign != 0)
7934 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7935 return x;
7938 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
7939 SRC is an AND with all bits of that field set, then we can discard
7940 the AND. */
7941 if (GET_CODE (dest) == ZERO_EXTRACT
7942 && GET_CODE (XEXP (dest, 1)) == CONST_INT
7943 && GET_CODE (src) == AND
7944 && GET_CODE (XEXP (src, 1)) == CONST_INT)
7946 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
7947 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
7948 unsigned HOST_WIDE_INT ze_mask;
7950 if (width >= HOST_BITS_PER_WIDE_INT)
7951 ze_mask = -1;
7952 else
7953 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
7955 /* Complete overlap. We can remove the source AND. */
7956 if ((and_mask & ze_mask) == ze_mask)
7957 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
7959 /* Partial overlap. We can reduce the source AND. */
7960 if ((and_mask & ze_mask) != and_mask)
7962 mode = GET_MODE (src);
7963 src = gen_rtx_AND (mode, XEXP (src, 0),
7964 gen_int_mode (and_mask & ze_mask, mode));
7965 return gen_rtx_SET (VOIDmode, dest, src);
7969 /* The other case we handle is assignments into a constant-position
7970 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7971 a mask that has all one bits except for a group of zero bits and
7972 OTHER is known to have zeros where C1 has ones, this is such an
7973 assignment. Compute the position and length from C1. Shift OTHER
7974 to the appropriate position, force it to the required mode, and
7975 make the extraction. Check for the AND in both operands. */
7977 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7978 return x;
7980 rhs = expand_compound_operation (XEXP (src, 0));
7981 lhs = expand_compound_operation (XEXP (src, 1));
7983 if (GET_CODE (rhs) == AND
7984 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7985 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7986 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7987 else if (GET_CODE (lhs) == AND
7988 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7989 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7990 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7991 else
7992 return x;
7994 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7995 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7996 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7997 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7998 return x;
8000 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8001 if (assign == 0)
8002 return x;
8004 /* The mode to use for the source is the mode of the assignment, or of
8005 what is inside a possible STRICT_LOW_PART. */
8006 mode = (GET_CODE (assign) == STRICT_LOW_PART
8007 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8009 /* Shift OTHER right POS places and make it the source, restricting it
8010 to the proper length and mode. */
8012 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
8013 GET_MODE (src), other, pos),
8014 mode,
8015 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8016 ? ~(unsigned HOST_WIDE_INT) 0
8017 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8018 dest, 0);
8020 /* If SRC is masked by an AND that does not make a difference in
8021 the value being stored, strip it. */
8022 if (GET_CODE (assign) == ZERO_EXTRACT
8023 && GET_CODE (XEXP (assign, 1)) == CONST_INT
8024 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8025 && GET_CODE (src) == AND
8026 && GET_CODE (XEXP (src, 1)) == CONST_INT
8027 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8028 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8029 src = XEXP (src, 0);
8031 return gen_rtx_SET (VOIDmode, assign, src);
8034 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8035 if so. */
8037 static rtx
8038 apply_distributive_law (rtx x)
8040 enum rtx_code code = GET_CODE (x);
8041 enum rtx_code inner_code;
8042 rtx lhs, rhs, other;
8043 rtx tem;
8045 /* Distributivity is not true for floating point as it can change the
8046 value. So we don't do it unless -funsafe-math-optimizations. */
8047 if (FLOAT_MODE_P (GET_MODE (x))
8048 && ! flag_unsafe_math_optimizations)
8049 return x;
8051 /* The outer operation can only be one of the following: */
8052 if (code != IOR && code != AND && code != XOR
8053 && code != PLUS && code != MINUS)
8054 return x;
8056 lhs = XEXP (x, 0);
8057 rhs = XEXP (x, 1);
8059 /* If either operand is a primitive we can't do anything, so get out
8060 fast. */
8061 if (OBJECT_P (lhs) || OBJECT_P (rhs))
8062 return x;
8064 lhs = expand_compound_operation (lhs);
8065 rhs = expand_compound_operation (rhs);
8066 inner_code = GET_CODE (lhs);
8067 if (inner_code != GET_CODE (rhs))
8068 return x;
8070 /* See if the inner and outer operations distribute. */
8071 switch (inner_code)
8073 case LSHIFTRT:
8074 case ASHIFTRT:
8075 case AND:
8076 case IOR:
8077 /* These all distribute except over PLUS. */
8078 if (code == PLUS || code == MINUS)
8079 return x;
8080 break;
8082 case MULT:
8083 if (code != PLUS && code != MINUS)
8084 return x;
8085 break;
8087 case ASHIFT:
8088 /* This is also a multiply, so it distributes over everything. */
8089 break;
8091 case SUBREG:
8092 /* Non-paradoxical SUBREGs distributes over all operations, provided
8093 the inner modes and byte offsets are the same, this is an extraction
8094 of a low-order part, we don't convert an fp operation to int or
8095 vice versa, and we would not be converting a single-word
8096 operation into a multi-word operation. The latter test is not
8097 required, but it prevents generating unneeded multi-word operations.
8098 Some of the previous tests are redundant given the latter test, but
8099 are retained because they are required for correctness.
8101 We produce the result slightly differently in this case. */
8103 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8104 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8105 || ! subreg_lowpart_p (lhs)
8106 || (GET_MODE_CLASS (GET_MODE (lhs))
8107 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8108 || (GET_MODE_SIZE (GET_MODE (lhs))
8109 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8110 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
8111 return x;
8113 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8114 SUBREG_REG (lhs), SUBREG_REG (rhs));
8115 return gen_lowpart (GET_MODE (x), tem);
8117 default:
8118 return x;
8121 /* Set LHS and RHS to the inner operands (A and B in the example
8122 above) and set OTHER to the common operand (C in the example).
8123 There is only one way to do this unless the inner operation is
8124 commutative. */
8125 if (COMMUTATIVE_ARITH_P (lhs)
8126 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8127 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8128 else if (COMMUTATIVE_ARITH_P (lhs)
8129 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8130 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8131 else if (COMMUTATIVE_ARITH_P (lhs)
8132 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8133 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8134 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8135 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8136 else
8137 return x;
8139 /* Form the new inner operation, seeing if it simplifies first. */
8140 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8142 /* There is one exception to the general way of distributing:
8143 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8144 if (code == XOR && inner_code == IOR)
8146 inner_code = AND;
8147 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8150 /* We may be able to continuing distributing the result, so call
8151 ourselves recursively on the inner operation before forming the
8152 outer operation, which we return. */
8153 return simplify_gen_binary (inner_code, GET_MODE (x),
8154 apply_distributive_law (tem), other);
8157 /* See if X is of the form (* (+ A B) C), and if so convert to
8158 (+ (* A C) (* B C)) and try to simplify.
8160 Most of the time, this results in no change. However, if some of
8161 the operands are the same or inverses of each other, simplifications
8162 will result.
8164 For example, (and (ior A B) (not B)) can occur as the result of
8165 expanding a bit field assignment. When we apply the distributive
8166 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8167 which then simplifies to (and (A (not B))).
8169 Note that no checks happen on the validity of applying the inverse
8170 distributive law. This is pointless since we can do it in the
8171 few places where this routine is called.
8173 N is the index of the term that is decomposed (the arithmetic operation,
8174 i.e. (+ A B) in the first example above). !N is the index of the term that
8175 is distributed, i.e. of C in the first example above. */
8176 static rtx
8177 distribute_and_simplify_rtx (rtx x, int n)
8179 enum machine_mode mode;
8180 enum rtx_code outer_code, inner_code;
8181 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8183 decomposed = XEXP (x, n);
8184 if (!ARITHMETIC_P (decomposed))
8185 return NULL_RTX;
8187 mode = GET_MODE (x);
8188 outer_code = GET_CODE (x);
8189 distributed = XEXP (x, !n);
8191 inner_code = GET_CODE (decomposed);
8192 inner_op0 = XEXP (decomposed, 0);
8193 inner_op1 = XEXP (decomposed, 1);
8195 /* Special case (and (xor B C) (not A)), which is equivalent to
8196 (xor (ior A B) (ior A C)) */
8197 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8199 distributed = XEXP (distributed, 0);
8200 outer_code = IOR;
8203 if (n == 0)
8205 /* Distribute the second term. */
8206 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8207 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8209 else
8211 /* Distribute the first term. */
8212 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8213 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8216 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8217 new_op0, new_op1));
8218 if (GET_CODE (tmp) != outer_code
8219 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8220 return tmp;
8222 return NULL_RTX;
8225 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8226 in MODE.
8228 Return an equivalent form, if different from X. Otherwise, return X. If
8229 X is zero, we are to always construct the equivalent form. */
8231 static rtx
8232 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8233 unsigned HOST_WIDE_INT constop)
8235 unsigned HOST_WIDE_INT nonzero;
8236 int i;
8238 /* Simplify VAROP knowing that we will be only looking at some of the
8239 bits in it.
8241 Note by passing in CONSTOP, we guarantee that the bits not set in
8242 CONSTOP are not significant and will never be examined. We must
8243 ensure that is the case by explicitly masking out those bits
8244 before returning. */
8245 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
8247 /* If VAROP is a CLOBBER, we will fail so return it. */
8248 if (GET_CODE (varop) == CLOBBER)
8249 return varop;
8251 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8252 to VAROP and return the new constant. */
8253 if (GET_CODE (varop) == CONST_INT)
8254 return gen_int_mode (INTVAL (varop) & constop, mode);
8256 /* See what bits may be nonzero in VAROP. Unlike the general case of
8257 a call to nonzero_bits, here we don't care about bits outside
8258 MODE. */
8260 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8262 /* Turn off all bits in the constant that are known to already be zero.
8263 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8264 which is tested below. */
8266 constop &= nonzero;
8268 /* If we don't have any bits left, return zero. */
8269 if (constop == 0)
8270 return const0_rtx;
8272 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8273 a power of two, we can replace this with an ASHIFT. */
8274 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8275 && (i = exact_log2 (constop)) >= 0)
8276 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8278 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8279 or XOR, then try to apply the distributive law. This may eliminate
8280 operations if either branch can be simplified because of the AND.
8281 It may also make some cases more complex, but those cases probably
8282 won't match a pattern either with or without this. */
8284 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8285 return
8286 gen_lowpart
8287 (mode,
8288 apply_distributive_law
8289 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8290 simplify_and_const_int (NULL_RTX,
8291 GET_MODE (varop),
8292 XEXP (varop, 0),
8293 constop),
8294 simplify_and_const_int (NULL_RTX,
8295 GET_MODE (varop),
8296 XEXP (varop, 1),
8297 constop))));
8299 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8300 the AND and see if one of the operands simplifies to zero. If so, we
8301 may eliminate it. */
8303 if (GET_CODE (varop) == PLUS
8304 && exact_log2 (constop + 1) >= 0)
8306 rtx o0, o1;
8308 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8309 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8310 if (o0 == const0_rtx)
8311 return o1;
8312 if (o1 == const0_rtx)
8313 return o0;
8316 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
8317 if we already had one (just check for the simplest cases). */
8318 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8319 && GET_MODE (XEXP (x, 0)) == mode
8320 && SUBREG_REG (XEXP (x, 0)) == varop)
8321 varop = XEXP (x, 0);
8322 else
8323 varop = gen_lowpart (mode, varop);
8325 /* If we can't make the SUBREG, try to return what we were given. */
8326 if (GET_CODE (varop) == CLOBBER)
8327 return x ? x : varop;
8329 /* If we are only masking insignificant bits, return VAROP. */
8330 if (constop == nonzero)
8331 x = varop;
8332 else
8334 /* Otherwise, return an AND. */
8335 constop = trunc_int_for_mode (constop, mode);
8336 /* See how much, if any, of X we can use. */
8337 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8338 x = simplify_gen_binary (AND, mode, varop, GEN_INT (constop));
8340 else
8342 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8343 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8344 SUBST (XEXP (x, 1), GEN_INT (constop));
8346 SUBST (XEXP (x, 0), varop);
8350 return x;
8353 /* Given a REG, X, compute which bits in X can be nonzero.
8354 We don't care about bits outside of those defined in MODE.
8356 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8357 a shift, AND, or zero_extract, we can do better. */
8359 static rtx
8360 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8361 rtx known_x ATTRIBUTE_UNUSED,
8362 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8363 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8364 unsigned HOST_WIDE_INT *nonzero)
8366 rtx tem;
8368 /* If X is a register whose nonzero bits value is current, use it.
8369 Otherwise, if X is a register whose value we can find, use that
8370 value. Otherwise, use the previously-computed global nonzero bits
8371 for this register. */
8373 if (reg_stat[REGNO (x)].last_set_value != 0
8374 && (reg_stat[REGNO (x)].last_set_mode == mode
8375 || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8376 && GET_MODE_CLASS (mode) == MODE_INT))
8377 && (reg_stat[REGNO (x)].last_set_label == label_tick
8378 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8379 && REG_N_SETS (REGNO (x)) == 1
8380 && ! REGNO_REG_SET_P
8381 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8382 REGNO (x))))
8383 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8385 *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8386 return NULL;
8389 tem = get_last_value (x);
8391 if (tem)
8393 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8394 /* If X is narrower than MODE and TEM is a non-negative
8395 constant that would appear negative in the mode of X,
8396 sign-extend it for use in reg_nonzero_bits because some
8397 machines (maybe most) will actually do the sign-extension
8398 and this is the conservative approach.
8400 ??? For 2.5, try to tighten up the MD files in this regard
8401 instead of this kludge. */
8403 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8404 && GET_CODE (tem) == CONST_INT
8405 && INTVAL (tem) > 0
8406 && 0 != (INTVAL (tem)
8407 & ((HOST_WIDE_INT) 1
8408 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8409 tem = GEN_INT (INTVAL (tem)
8410 | ((HOST_WIDE_INT) (-1)
8411 << GET_MODE_BITSIZE (GET_MODE (x))));
8412 #endif
8413 return tem;
8415 else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8417 unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8419 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8420 /* We don't know anything about the upper bits. */
8421 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8422 *nonzero &= mask;
8425 return NULL;
8428 /* Return the number of bits at the high-order end of X that are known to
8429 be equal to the sign bit. X will be used in mode MODE; if MODE is
8430 VOIDmode, X will be used in its own mode. The returned value will always
8431 be between 1 and the number of bits in MODE. */
8433 static rtx
8434 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8435 rtx known_x ATTRIBUTE_UNUSED,
8436 enum machine_mode known_mode
8437 ATTRIBUTE_UNUSED,
8438 unsigned int known_ret ATTRIBUTE_UNUSED,
8439 unsigned int *result)
8441 rtx tem;
8443 if (reg_stat[REGNO (x)].last_set_value != 0
8444 && reg_stat[REGNO (x)].last_set_mode == mode
8445 && (reg_stat[REGNO (x)].last_set_label == label_tick
8446 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8447 && REG_N_SETS (REGNO (x)) == 1
8448 && ! REGNO_REG_SET_P
8449 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8450 REGNO (x))))
8451 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8453 *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8454 return NULL;
8457 tem = get_last_value (x);
8458 if (tem != 0)
8459 return tem;
8461 if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8462 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8463 *result = reg_stat[REGNO (x)].sign_bit_copies;
8465 return NULL;
8468 /* Return the number of "extended" bits there are in X, when interpreted
8469 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8470 unsigned quantities, this is the number of high-order zero bits.
8471 For signed quantities, this is the number of copies of the sign bit
8472 minus 1. In both case, this function returns the number of "spare"
8473 bits. For example, if two quantities for which this function returns
8474 at least 1 are added, the addition is known not to overflow.
8476 This function will always return 0 unless called during combine, which
8477 implies that it must be called from a define_split. */
8479 unsigned int
8480 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8482 if (nonzero_sign_valid == 0)
8483 return 0;
8485 return (unsignedp
8486 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8487 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8488 - floor_log2 (nonzero_bits (x, mode)))
8489 : 0)
8490 : num_sign_bit_copies (x, mode) - 1);
8493 /* This function is called from `simplify_shift_const' to merge two
8494 outer operations. Specifically, we have already found that we need
8495 to perform operation *POP0 with constant *PCONST0 at the outermost
8496 position. We would now like to also perform OP1 with constant CONST1
8497 (with *POP0 being done last).
8499 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8500 the resulting operation. *PCOMP_P is set to 1 if we would need to
8501 complement the innermost operand, otherwise it is unchanged.
8503 MODE is the mode in which the operation will be done. No bits outside
8504 the width of this mode matter. It is assumed that the width of this mode
8505 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8507 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8508 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8509 result is simply *PCONST0.
8511 If the resulting operation cannot be expressed as one operation, we
8512 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8514 static int
8515 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)
8517 enum rtx_code op0 = *pop0;
8518 HOST_WIDE_INT const0 = *pconst0;
8520 const0 &= GET_MODE_MASK (mode);
8521 const1 &= GET_MODE_MASK (mode);
8523 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8524 if (op0 == AND)
8525 const1 &= const0;
8527 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8528 if OP0 is SET. */
8530 if (op1 == UNKNOWN || op0 == SET)
8531 return 1;
8533 else if (op0 == UNKNOWN)
8534 op0 = op1, const0 = const1;
8536 else if (op0 == op1)
8538 switch (op0)
8540 case AND:
8541 const0 &= const1;
8542 break;
8543 case IOR:
8544 const0 |= const1;
8545 break;
8546 case XOR:
8547 const0 ^= const1;
8548 break;
8549 case PLUS:
8550 const0 += const1;
8551 break;
8552 case NEG:
8553 op0 = UNKNOWN;
8554 break;
8555 default:
8556 break;
8560 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8561 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8562 return 0;
8564 /* If the two constants aren't the same, we can't do anything. The
8565 remaining six cases can all be done. */
8566 else if (const0 != const1)
8567 return 0;
8569 else
8570 switch (op0)
8572 case IOR:
8573 if (op1 == AND)
8574 /* (a & b) | b == b */
8575 op0 = SET;
8576 else /* op1 == XOR */
8577 /* (a ^ b) | b == a | b */
8579 break;
8581 case XOR:
8582 if (op1 == AND)
8583 /* (a & b) ^ b == (~a) & b */
8584 op0 = AND, *pcomp_p = 1;
8585 else /* op1 == IOR */
8586 /* (a | b) ^ b == a & ~b */
8587 op0 = AND, const0 = ~const0;
8588 break;
8590 case AND:
8591 if (op1 == IOR)
8592 /* (a | b) & b == b */
8593 op0 = SET;
8594 else /* op1 == XOR */
8595 /* (a ^ b) & b) == (~a) & b */
8596 *pcomp_p = 1;
8597 break;
8598 default:
8599 break;
8602 /* Check for NO-OP cases. */
8603 const0 &= GET_MODE_MASK (mode);
8604 if (const0 == 0
8605 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8606 op0 = UNKNOWN;
8607 else if (const0 == 0 && op0 == AND)
8608 op0 = SET;
8609 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8610 && op0 == AND)
8611 op0 = UNKNOWN;
8613 /* ??? Slightly redundant with the above mask, but not entirely.
8614 Moving this above means we'd have to sign-extend the mode mask
8615 for the final test. */
8616 const0 = trunc_int_for_mode (const0, mode);
8618 *pop0 = op0;
8619 *pconst0 = const0;
8621 return 1;
8624 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8625 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
8626 that we started with.
8628 The shift is normally computed in the widest mode we find in VAROP, as
8629 long as it isn't a different number of words than RESULT_MODE. Exceptions
8630 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8632 static rtx
8633 simplify_shift_const (rtx x, enum rtx_code code,
8634 enum machine_mode result_mode, rtx varop,
8635 int orig_count)
8637 enum rtx_code orig_code = code;
8638 unsigned int count;
8639 int signed_count;
8640 enum machine_mode mode = result_mode;
8641 enum machine_mode shift_mode, tmode;
8642 unsigned int mode_words
8643 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8644 /* We form (outer_op (code varop count) (outer_const)). */
8645 enum rtx_code outer_op = UNKNOWN;
8646 HOST_WIDE_INT outer_const = 0;
8647 rtx const_rtx;
8648 int complement_p = 0;
8649 rtx new;
8651 /* Make sure and truncate the "natural" shift on the way in. We don't
8652 want to do this inside the loop as it makes it more difficult to
8653 combine shifts. */
8654 if (SHIFT_COUNT_TRUNCATED)
8655 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8657 /* If we were given an invalid count, don't do anything except exactly
8658 what was requested. */
8660 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8662 if (x)
8663 return x;
8665 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
8668 count = orig_count;
8670 /* Unless one of the branches of the `if' in this loop does a `continue',
8671 we will `break' the loop after the `if'. */
8673 while (count != 0)
8675 /* If we have an operand of (clobber (const_int 0)), just return that
8676 value. */
8677 if (GET_CODE (varop) == CLOBBER)
8678 return varop;
8680 /* If we discovered we had to complement VAROP, leave. Making a NOT
8681 here would cause an infinite loop. */
8682 if (complement_p)
8683 break;
8685 /* Convert ROTATERT to ROTATE. */
8686 if (code == ROTATERT)
8688 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8689 code = ROTATE;
8690 if (VECTOR_MODE_P (result_mode))
8691 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8692 else
8693 count = bitsize - count;
8696 /* We need to determine what mode we will do the shift in. If the
8697 shift is a right shift or a ROTATE, we must always do it in the mode
8698 it was originally done in. Otherwise, we can do it in MODE, the
8699 widest mode encountered. */
8700 shift_mode
8701 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8702 ? result_mode : mode);
8704 /* Handle cases where the count is greater than the size of the mode
8705 minus 1. For ASHIFT, use the size minus one as the count (this can
8706 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8707 take the count modulo the size. For other shifts, the result is
8708 zero.
8710 Since these shifts are being produced by the compiler by combining
8711 multiple operations, each of which are defined, we know what the
8712 result is supposed to be. */
8714 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
8716 if (code == ASHIFTRT)
8717 count = GET_MODE_BITSIZE (shift_mode) - 1;
8718 else if (code == ROTATE || code == ROTATERT)
8719 count %= GET_MODE_BITSIZE (shift_mode);
8720 else
8722 /* We can't simply return zero because there may be an
8723 outer op. */
8724 varop = const0_rtx;
8725 count = 0;
8726 break;
8730 /* An arithmetic right shift of a quantity known to be -1 or 0
8731 is a no-op. */
8732 if (code == ASHIFTRT
8733 && (num_sign_bit_copies (varop, shift_mode)
8734 == GET_MODE_BITSIZE (shift_mode)))
8736 count = 0;
8737 break;
8740 /* If we are doing an arithmetic right shift and discarding all but
8741 the sign bit copies, this is equivalent to doing a shift by the
8742 bitsize minus one. Convert it into that shift because it will often
8743 allow other simplifications. */
8745 if (code == ASHIFTRT
8746 && (count + num_sign_bit_copies (varop, shift_mode)
8747 >= GET_MODE_BITSIZE (shift_mode)))
8748 count = GET_MODE_BITSIZE (shift_mode) - 1;
8750 /* We simplify the tests below and elsewhere by converting
8751 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8752 `make_compound_operation' will convert it to an ASHIFTRT for
8753 those machines (such as VAX) that don't have an LSHIFTRT. */
8754 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8755 && code == ASHIFTRT
8756 && ((nonzero_bits (varop, shift_mode)
8757 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8758 == 0))
8759 code = LSHIFTRT;
8761 if (code == LSHIFTRT
8762 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8763 && !(nonzero_bits (varop, shift_mode) >> count))
8764 varop = const0_rtx;
8765 if (code == ASHIFT
8766 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8767 && !((nonzero_bits (varop, shift_mode) << count)
8768 & GET_MODE_MASK (shift_mode)))
8769 varop = const0_rtx;
8771 switch (GET_CODE (varop))
8773 case SIGN_EXTEND:
8774 case ZERO_EXTEND:
8775 case SIGN_EXTRACT:
8776 case ZERO_EXTRACT:
8777 new = expand_compound_operation (varop);
8778 if (new != varop)
8780 varop = new;
8781 continue;
8783 break;
8785 case MEM:
8786 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8787 minus the width of a smaller mode, we can do this with a
8788 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8789 if ((code == ASHIFTRT || code == LSHIFTRT)
8790 && ! mode_dependent_address_p (XEXP (varop, 0))
8791 && ! MEM_VOLATILE_P (varop)
8792 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8793 MODE_INT, 1)) != BLKmode)
8795 new = adjust_address_nv (varop, tmode,
8796 BYTES_BIG_ENDIAN ? 0
8797 : count / BITS_PER_UNIT);
8799 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8800 : ZERO_EXTEND, mode, new);
8801 count = 0;
8802 continue;
8804 break;
8806 case USE:
8807 /* Similar to the case above, except that we can only do this if
8808 the resulting mode is the same as that of the underlying
8809 MEM and adjust the address depending on the *bits* endianness
8810 because of the way that bit-field extract insns are defined. */
8811 if ((code == ASHIFTRT || code == LSHIFTRT)
8812 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8813 MODE_INT, 1)) != BLKmode
8814 && tmode == GET_MODE (XEXP (varop, 0)))
8816 if (BITS_BIG_ENDIAN)
8817 new = XEXP (varop, 0);
8818 else
8820 new = copy_rtx (XEXP (varop, 0));
8821 SUBST (XEXP (new, 0),
8822 plus_constant (XEXP (new, 0),
8823 count / BITS_PER_UNIT));
8826 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8827 : ZERO_EXTEND, mode, new);
8828 count = 0;
8829 continue;
8831 break;
8833 case SUBREG:
8834 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8835 the same number of words as what we've seen so far. Then store
8836 the widest mode in MODE. */
8837 if (subreg_lowpart_p (varop)
8838 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8839 > GET_MODE_SIZE (GET_MODE (varop)))
8840 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8841 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8842 == mode_words)
8844 varop = SUBREG_REG (varop);
8845 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8846 mode = GET_MODE (varop);
8847 continue;
8849 break;
8851 case MULT:
8852 /* Some machines use MULT instead of ASHIFT because MULT
8853 is cheaper. But it is still better on those machines to
8854 merge two shifts into one. */
8855 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8856 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8858 varop
8859 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
8860 XEXP (varop, 0),
8861 GEN_INT (exact_log2 (
8862 INTVAL (XEXP (varop, 1)))));
8863 continue;
8865 break;
8867 case UDIV:
8868 /* Similar, for when divides are cheaper. */
8869 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8870 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8872 varop
8873 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
8874 XEXP (varop, 0),
8875 GEN_INT (exact_log2 (
8876 INTVAL (XEXP (varop, 1)))));
8877 continue;
8879 break;
8881 case ASHIFTRT:
8882 /* If we are extracting just the sign bit of an arithmetic
8883 right shift, that shift is not needed. However, the sign
8884 bit of a wider mode may be different from what would be
8885 interpreted as the sign bit in a narrower mode, so, if
8886 the result is narrower, don't discard the shift. */
8887 if (code == LSHIFTRT
8888 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8889 && (GET_MODE_BITSIZE (result_mode)
8890 >= GET_MODE_BITSIZE (GET_MODE (varop))))
8892 varop = XEXP (varop, 0);
8893 continue;
8896 /* ... fall through ... */
8898 case LSHIFTRT:
8899 case ASHIFT:
8900 case ROTATE:
8901 /* Here we have two nested shifts. The result is usually the
8902 AND of a new shift with a mask. We compute the result below. */
8903 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8904 && INTVAL (XEXP (varop, 1)) >= 0
8905 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8906 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8907 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8909 enum rtx_code first_code = GET_CODE (varop);
8910 unsigned int first_count = INTVAL (XEXP (varop, 1));
8911 unsigned HOST_WIDE_INT mask;
8912 rtx mask_rtx;
8914 /* We have one common special case. We can't do any merging if
8915 the inner code is an ASHIFTRT of a smaller mode. However, if
8916 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8917 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8918 we can convert it to
8919 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8920 This simplifies certain SIGN_EXTEND operations. */
8921 if (code == ASHIFT && first_code == ASHIFTRT
8922 && count == (unsigned int)
8923 (GET_MODE_BITSIZE (result_mode)
8924 - GET_MODE_BITSIZE (GET_MODE (varop))))
8926 /* C3 has the low-order C1 bits zero. */
8928 mask = (GET_MODE_MASK (mode)
8929 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8931 varop = simplify_and_const_int (NULL_RTX, result_mode,
8932 XEXP (varop, 0), mask);
8933 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8934 varop, count);
8935 count = first_count;
8936 code = ASHIFTRT;
8937 continue;
8940 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8941 than C1 high-order bits equal to the sign bit, we can convert
8942 this to either an ASHIFT or an ASHIFTRT depending on the
8943 two counts.
8945 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8947 if (code == ASHIFTRT && first_code == ASHIFT
8948 && GET_MODE (varop) == shift_mode
8949 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8950 > first_count))
8952 varop = XEXP (varop, 0);
8954 signed_count = count - first_count;
8955 if (signed_count < 0)
8956 count = -signed_count, code = ASHIFT;
8957 else
8958 count = signed_count;
8960 continue;
8963 /* There are some cases we can't do. If CODE is ASHIFTRT,
8964 we can only do this if FIRST_CODE is also ASHIFTRT.
8966 We can't do the case when CODE is ROTATE and FIRST_CODE is
8967 ASHIFTRT.
8969 If the mode of this shift is not the mode of the outer shift,
8970 we can't do this if either shift is a right shift or ROTATE.
8972 Finally, we can't do any of these if the mode is too wide
8973 unless the codes are the same.
8975 Handle the case where the shift codes are the same
8976 first. */
8978 if (code == first_code)
8980 if (GET_MODE (varop) != result_mode
8981 && (code == ASHIFTRT || code == LSHIFTRT
8982 || code == ROTATE))
8983 break;
8985 count += first_count;
8986 varop = XEXP (varop, 0);
8987 continue;
8990 if (code == ASHIFTRT
8991 || (code == ROTATE && first_code == ASHIFTRT)
8992 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8993 || (GET_MODE (varop) != result_mode
8994 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8995 || first_code == ROTATE
8996 || code == ROTATE)))
8997 break;
8999 /* To compute the mask to apply after the shift, shift the
9000 nonzero bits of the inner shift the same way the
9001 outer shift will. */
9003 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9005 mask_rtx
9006 = simplify_binary_operation (code, result_mode, mask_rtx,
9007 GEN_INT (count));
9009 /* Give up if we can't compute an outer operation to use. */
9010 if (mask_rtx == 0
9011 || GET_CODE (mask_rtx) != CONST_INT
9012 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9013 INTVAL (mask_rtx),
9014 result_mode, &complement_p))
9015 break;
9017 /* If the shifts are in the same direction, we add the
9018 counts. Otherwise, we subtract them. */
9019 signed_count = count;
9020 if ((code == ASHIFTRT || code == LSHIFTRT)
9021 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9022 signed_count += first_count;
9023 else
9024 signed_count -= first_count;
9026 /* If COUNT is positive, the new shift is usually CODE,
9027 except for the two exceptions below, in which case it is
9028 FIRST_CODE. If the count is negative, FIRST_CODE should
9029 always be used */
9030 if (signed_count > 0
9031 && ((first_code == ROTATE && code == ASHIFT)
9032 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9033 code = first_code, count = signed_count;
9034 else if (signed_count < 0)
9035 code = first_code, count = -signed_count;
9036 else
9037 count = signed_count;
9039 varop = XEXP (varop, 0);
9040 continue;
9043 /* If we have (A << B << C) for any shift, we can convert this to
9044 (A << C << B). This wins if A is a constant. Only try this if
9045 B is not a constant. */
9047 else if (GET_CODE (varop) == code
9048 && GET_CODE (XEXP (varop, 1)) != CONST_INT
9049 && 0 != (new
9050 = simplify_binary_operation (code, mode,
9051 XEXP (varop, 0),
9052 GEN_INT (count))))
9054 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9055 count = 0;
9056 continue;
9058 break;
9060 case NOT:
9061 /* Make this fit the case below. */
9062 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9063 GEN_INT (GET_MODE_MASK (mode)));
9064 continue;
9066 case IOR:
9067 case AND:
9068 case XOR:
9069 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9070 with C the size of VAROP - 1 and the shift is logical if
9071 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9072 we have an (le X 0) operation. If we have an arithmetic shift
9073 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9074 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9076 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9077 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9078 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9079 && (code == LSHIFTRT || code == ASHIFTRT)
9080 && count == (unsigned int)
9081 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9082 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9084 count = 0;
9085 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9086 const0_rtx);
9088 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9089 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9091 continue;
9094 /* If we have (shift (logical)), move the logical to the outside
9095 to allow it to possibly combine with another logical and the
9096 shift to combine with another shift. This also canonicalizes to
9097 what a ZERO_EXTRACT looks like. Also, some machines have
9098 (and (shift)) insns. */
9100 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9101 /* We can't do this if we have (ashiftrt (xor)) and the
9102 constant has its sign bit set in shift_mode. */
9103 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9104 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9105 shift_mode))
9106 && (new = simplify_binary_operation (code, result_mode,
9107 XEXP (varop, 1),
9108 GEN_INT (count))) != 0
9109 && GET_CODE (new) == CONST_INT
9110 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9111 INTVAL (new), result_mode, &complement_p))
9113 varop = XEXP (varop, 0);
9114 continue;
9117 /* If we can't do that, try to simplify the shift in each arm of the
9118 logical expression, make a new logical expression, and apply
9119 the inverse distributive law. This also can't be done
9120 for some (ashiftrt (xor)). */
9121 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9122 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9123 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9124 shift_mode)))
9126 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9127 XEXP (varop, 0), count);
9128 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9129 XEXP (varop, 1), count);
9131 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9132 lhs, rhs);
9133 varop = apply_distributive_law (varop);
9135 count = 0;
9136 continue;
9138 break;
9140 case EQ:
9141 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9142 says that the sign bit can be tested, FOO has mode MODE, C is
9143 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9144 that may be nonzero. */
9145 if (code == LSHIFTRT
9146 && XEXP (varop, 1) == const0_rtx
9147 && GET_MODE (XEXP (varop, 0)) == result_mode
9148 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9149 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9150 && ((STORE_FLAG_VALUE
9151 & ((HOST_WIDE_INT) 1
9152 < (GET_MODE_BITSIZE (result_mode) - 1))))
9153 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9154 && merge_outer_ops (&outer_op, &outer_const, XOR,
9155 (HOST_WIDE_INT) 1, result_mode,
9156 &complement_p))
9158 varop = XEXP (varop, 0);
9159 count = 0;
9160 continue;
9162 break;
9164 case NEG:
9165 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9166 than the number of bits in the mode is equivalent to A. */
9167 if (code == LSHIFTRT
9168 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9169 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9171 varop = XEXP (varop, 0);
9172 count = 0;
9173 continue;
9176 /* NEG commutes with ASHIFT since it is multiplication. Move the
9177 NEG outside to allow shifts to combine. */
9178 if (code == ASHIFT
9179 && merge_outer_ops (&outer_op, &outer_const, NEG,
9180 (HOST_WIDE_INT) 0, result_mode,
9181 &complement_p))
9183 varop = XEXP (varop, 0);
9184 continue;
9186 break;
9188 case PLUS:
9189 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9190 is one less than the number of bits in the mode is
9191 equivalent to (xor A 1). */
9192 if (code == LSHIFTRT
9193 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9194 && XEXP (varop, 1) == constm1_rtx
9195 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9196 && merge_outer_ops (&outer_op, &outer_const, XOR,
9197 (HOST_WIDE_INT) 1, result_mode,
9198 &complement_p))
9200 count = 0;
9201 varop = XEXP (varop, 0);
9202 continue;
9205 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9206 that might be nonzero in BAR are those being shifted out and those
9207 bits are known zero in FOO, we can replace the PLUS with FOO.
9208 Similarly in the other operand order. This code occurs when
9209 we are computing the size of a variable-size array. */
9211 if ((code == ASHIFTRT || code == LSHIFTRT)
9212 && count < HOST_BITS_PER_WIDE_INT
9213 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9214 && (nonzero_bits (XEXP (varop, 1), result_mode)
9215 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9217 varop = XEXP (varop, 0);
9218 continue;
9220 else if ((code == ASHIFTRT || code == LSHIFTRT)
9221 && count < HOST_BITS_PER_WIDE_INT
9222 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9223 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9224 >> count)
9225 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9226 & nonzero_bits (XEXP (varop, 1),
9227 result_mode)))
9229 varop = XEXP (varop, 1);
9230 continue;
9233 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9234 if (code == ASHIFT
9235 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9236 && (new = simplify_binary_operation (ASHIFT, result_mode,
9237 XEXP (varop, 1),
9238 GEN_INT (count))) != 0
9239 && GET_CODE (new) == CONST_INT
9240 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9241 INTVAL (new), result_mode, &complement_p))
9243 varop = XEXP (varop, 0);
9244 continue;
9247 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9248 signbit', and attempt to change the PLUS to an XOR and move it to
9249 the outer operation as is done above in the AND/IOR/XOR case
9250 leg for shift(logical). See details in logical handling above
9251 for reasoning in doing so. */
9252 if (code == LSHIFTRT
9253 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9254 && mode_signbit_p (result_mode, XEXP (varop, 1))
9255 && (new = simplify_binary_operation (code, result_mode,
9256 XEXP (varop, 1),
9257 GEN_INT (count))) != 0
9258 && GET_CODE (new) == CONST_INT
9259 && merge_outer_ops (&outer_op, &outer_const, XOR,
9260 INTVAL (new), result_mode, &complement_p))
9262 varop = XEXP (varop, 0);
9263 continue;
9266 break;
9268 case MINUS:
9269 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9270 with C the size of VAROP - 1 and the shift is logical if
9271 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9272 we have a (gt X 0) operation. If the shift is arithmetic with
9273 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9274 we have a (neg (gt X 0)) operation. */
9276 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9277 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9278 && count == (unsigned int)
9279 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9280 && (code == LSHIFTRT || code == ASHIFTRT)
9281 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9282 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9283 == count
9284 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9286 count = 0;
9287 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9288 const0_rtx);
9290 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9291 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9293 continue;
9295 break;
9297 case TRUNCATE:
9298 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9299 if the truncate does not affect the value. */
9300 if (code == LSHIFTRT
9301 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9302 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9303 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9304 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9305 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9307 rtx varop_inner = XEXP (varop, 0);
9309 varop_inner
9310 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9311 XEXP (varop_inner, 0),
9312 GEN_INT
9313 (count + INTVAL (XEXP (varop_inner, 1))));
9314 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9315 count = 0;
9316 continue;
9318 break;
9320 default:
9321 break;
9324 break;
9327 /* We need to determine what mode to do the shift in. If the shift is
9328 a right shift or ROTATE, we must always do it in the mode it was
9329 originally done in. Otherwise, we can do it in MODE, the widest mode
9330 encountered. The code we care about is that of the shift that will
9331 actually be done, not the shift that was originally requested. */
9332 shift_mode
9333 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9334 ? result_mode : mode);
9336 /* We have now finished analyzing the shift. The result should be
9337 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9338 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9339 to the result of the shift. OUTER_CONST is the relevant constant,
9340 but we must turn off all bits turned off in the shift.
9342 If we were passed a value for X, see if we can use any pieces of
9343 it. If not, make new rtx. */
9345 if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9346 && GET_CODE (XEXP (x, 1)) == CONST_INT
9347 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9348 const_rtx = XEXP (x, 1);
9349 else
9350 const_rtx = GEN_INT (count);
9352 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9353 && GET_MODE (XEXP (x, 0)) == shift_mode
9354 && SUBREG_REG (XEXP (x, 0)) == varop)
9355 varop = XEXP (x, 0);
9356 else if (GET_MODE (varop) != shift_mode)
9357 varop = gen_lowpart (shift_mode, varop);
9359 /* If we can't make the SUBREG, try to return what we were given. */
9360 if (GET_CODE (varop) == CLOBBER)
9361 return x ? x : varop;
9363 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9364 if (new != 0)
9365 x = new;
9366 else
9367 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9369 /* If we have an outer operation and we just made a shift, it is
9370 possible that we could have simplified the shift were it not
9371 for the outer operation. So try to do the simplification
9372 recursively. */
9374 if (outer_op != UNKNOWN && GET_CODE (x) == code
9375 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9376 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9377 INTVAL (XEXP (x, 1)));
9379 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9380 turn off all the bits that the shift would have turned off. */
9381 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9382 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9383 GET_MODE_MASK (result_mode) >> orig_count);
9385 /* Do the remainder of the processing in RESULT_MODE. */
9386 x = gen_lowpart (result_mode, x);
9388 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9389 operation. */
9390 if (complement_p)
9391 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9393 if (outer_op != UNKNOWN)
9395 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9396 outer_const = trunc_int_for_mode (outer_const, result_mode);
9398 if (outer_op == AND)
9399 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9400 else if (outer_op == SET)
9401 /* This means that we have determined that the result is
9402 equivalent to a constant. This should be rare. */
9403 x = GEN_INT (outer_const);
9404 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9405 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9406 else
9407 x = simplify_gen_binary (outer_op, result_mode, x,
9408 GEN_INT (outer_const));
9411 return x;
9414 /* Like recog, but we receive the address of a pointer to a new pattern.
9415 We try to match the rtx that the pointer points to.
9416 If that fails, we may try to modify or replace the pattern,
9417 storing the replacement into the same pointer object.
9419 Modifications include deletion or addition of CLOBBERs.
9421 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9422 the CLOBBERs are placed.
9424 The value is the final insn code from the pattern ultimately matched,
9425 or -1. */
9427 static int
9428 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9430 rtx pat = *pnewpat;
9431 int insn_code_number;
9432 int num_clobbers_to_add = 0;
9433 int i;
9434 rtx notes = 0;
9435 rtx old_notes, old_pat;
9437 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9438 we use to indicate that something didn't match. If we find such a
9439 thing, force rejection. */
9440 if (GET_CODE (pat) == PARALLEL)
9441 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9442 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9443 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9444 return -1;
9446 old_pat = PATTERN (insn);
9447 old_notes = REG_NOTES (insn);
9448 PATTERN (insn) = pat;
9449 REG_NOTES (insn) = 0;
9451 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9453 /* If it isn't, there is the possibility that we previously had an insn
9454 that clobbered some register as a side effect, but the combined
9455 insn doesn't need to do that. So try once more without the clobbers
9456 unless this represents an ASM insn. */
9458 if (insn_code_number < 0 && ! check_asm_operands (pat)
9459 && GET_CODE (pat) == PARALLEL)
9461 int pos;
9463 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9464 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9466 if (i != pos)
9467 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9468 pos++;
9471 SUBST_INT (XVECLEN (pat, 0), pos);
9473 if (pos == 1)
9474 pat = XVECEXP (pat, 0, 0);
9476 PATTERN (insn) = pat;
9477 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9479 PATTERN (insn) = old_pat;
9480 REG_NOTES (insn) = old_notes;
9482 /* Recognize all noop sets, these will be killed by followup pass. */
9483 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9484 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9486 /* If we had any clobbers to add, make a new pattern than contains
9487 them. Then check to make sure that all of them are dead. */
9488 if (num_clobbers_to_add)
9490 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9491 rtvec_alloc (GET_CODE (pat) == PARALLEL
9492 ? (XVECLEN (pat, 0)
9493 + num_clobbers_to_add)
9494 : num_clobbers_to_add + 1));
9496 if (GET_CODE (pat) == PARALLEL)
9497 for (i = 0; i < XVECLEN (pat, 0); i++)
9498 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9499 else
9500 XVECEXP (newpat, 0, 0) = pat;
9502 add_clobbers (newpat, insn_code_number);
9504 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9505 i < XVECLEN (newpat, 0); i++)
9507 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9508 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9509 return -1;
9510 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9511 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9513 pat = newpat;
9516 *pnewpat = pat;
9517 *pnotes = notes;
9519 return insn_code_number;
9522 /* Like gen_lowpart_general but for use by combine. In combine it
9523 is not possible to create any new pseudoregs. However, it is
9524 safe to create invalid memory addresses, because combine will
9525 try to recognize them and all they will do is make the combine
9526 attempt fail.
9528 If for some reason this cannot do its job, an rtx
9529 (clobber (const_int 0)) is returned.
9530 An insn containing that will not be recognized. */
9532 static rtx
9533 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9535 enum machine_mode imode = GET_MODE (x);
9536 unsigned int osize = GET_MODE_SIZE (omode);
9537 unsigned int isize = GET_MODE_SIZE (imode);
9538 rtx result;
9540 if (omode == imode)
9541 return x;
9543 /* Return identity if this is a CONST or symbolic reference. */
9544 if (omode == Pmode
9545 && (GET_CODE (x) == CONST
9546 || GET_CODE (x) == SYMBOL_REF
9547 || GET_CODE (x) == LABEL_REF))
9548 return x;
9550 /* We can only support MODE being wider than a word if X is a
9551 constant integer or has a mode the same size. */
9552 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9553 && ! ((imode == VOIDmode
9554 && (GET_CODE (x) == CONST_INT
9555 || GET_CODE (x) == CONST_DOUBLE))
9556 || isize == osize))
9557 goto fail;
9559 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9560 won't know what to do. So we will strip off the SUBREG here and
9561 process normally. */
9562 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9564 x = SUBREG_REG (x);
9566 /* For use in case we fall down into the address adjustments
9567 further below, we need to adjust the known mode and size of
9568 x; imode and isize, since we just adjusted x. */
9569 imode = GET_MODE (x);
9571 if (imode == omode)
9572 return x;
9574 isize = GET_MODE_SIZE (imode);
9577 result = gen_lowpart_common (omode, x);
9579 #ifdef CANNOT_CHANGE_MODE_CLASS
9580 if (result != 0 && GET_CODE (result) == SUBREG)
9581 record_subregs_of_mode (result);
9582 #endif
9584 if (result)
9585 return result;
9587 if (MEM_P (x))
9589 int offset = 0;
9591 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9592 address. */
9593 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9594 goto fail;
9596 /* If we want to refer to something bigger than the original memref,
9597 generate a paradoxical subreg instead. That will force a reload
9598 of the original memref X. */
9599 if (isize < osize)
9600 return gen_rtx_SUBREG (omode, x, 0);
9602 if (WORDS_BIG_ENDIAN)
9603 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9605 /* Adjust the address so that the address-after-the-data is
9606 unchanged. */
9607 if (BYTES_BIG_ENDIAN)
9608 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9610 return adjust_address_nv (x, omode, offset);
9613 /* If X is a comparison operator, rewrite it in a new mode. This
9614 probably won't match, but may allow further simplifications. */
9615 else if (COMPARISON_P (x))
9616 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9618 /* If we couldn't simplify X any other way, just enclose it in a
9619 SUBREG. Normally, this SUBREG won't match, but some patterns may
9620 include an explicit SUBREG or we may simplify it further in combine. */
9621 else
9623 int offset = 0;
9624 rtx res;
9626 offset = subreg_lowpart_offset (omode, imode);
9627 if (imode == VOIDmode)
9629 imode = int_mode_for_mode (omode);
9630 x = gen_lowpart_common (imode, x);
9631 if (x == NULL)
9632 goto fail;
9634 res = simplify_gen_subreg (omode, x, imode, offset);
9635 if (res)
9636 return res;
9639 fail:
9640 return gen_rtx_CLOBBER (imode, const0_rtx);
9643 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9644 comparison code that will be tested.
9646 The result is a possibly different comparison code to use. *POP0 and
9647 *POP1 may be updated.
9649 It is possible that we might detect that a comparison is either always
9650 true or always false. However, we do not perform general constant
9651 folding in combine, so this knowledge isn't useful. Such tautologies
9652 should have been detected earlier. Hence we ignore all such cases. */
9654 static enum rtx_code
9655 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9657 rtx op0 = *pop0;
9658 rtx op1 = *pop1;
9659 rtx tem, tem1;
9660 int i;
9661 enum machine_mode mode, tmode;
9663 /* Try a few ways of applying the same transformation to both operands. */
9664 while (1)
9666 #ifndef WORD_REGISTER_OPERATIONS
9667 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9668 so check specially. */
9669 if (code != GTU && code != GEU && code != LTU && code != LEU
9670 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9671 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9672 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9673 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9674 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9675 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9676 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9677 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9678 && XEXP (op0, 1) == XEXP (op1, 1)
9679 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9680 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9681 && (INTVAL (XEXP (op0, 1))
9682 == (GET_MODE_BITSIZE (GET_MODE (op0))
9683 - (GET_MODE_BITSIZE
9684 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9686 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9687 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9689 #endif
9691 /* If both operands are the same constant shift, see if we can ignore the
9692 shift. We can if the shift is a rotate or if the bits shifted out of
9693 this shift are known to be zero for both inputs and if the type of
9694 comparison is compatible with the shift. */
9695 if (GET_CODE (op0) == GET_CODE (op1)
9696 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9697 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9698 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9699 && (code != GT && code != LT && code != GE && code != LE))
9700 || (GET_CODE (op0) == ASHIFTRT
9701 && (code != GTU && code != LTU
9702 && code != GEU && code != LEU)))
9703 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9704 && INTVAL (XEXP (op0, 1)) >= 0
9705 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9706 && XEXP (op0, 1) == XEXP (op1, 1))
9708 enum machine_mode mode = GET_MODE (op0);
9709 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9710 int shift_count = INTVAL (XEXP (op0, 1));
9712 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9713 mask &= (mask >> shift_count) << shift_count;
9714 else if (GET_CODE (op0) == ASHIFT)
9715 mask = (mask & (mask << shift_count)) >> shift_count;
9717 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9718 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9719 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9720 else
9721 break;
9724 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9725 SUBREGs are of the same mode, and, in both cases, the AND would
9726 be redundant if the comparison was done in the narrower mode,
9727 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9728 and the operand's possibly nonzero bits are 0xffffff01; in that case
9729 if we only care about QImode, we don't need the AND). This case
9730 occurs if the output mode of an scc insn is not SImode and
9731 STORE_FLAG_VALUE == 1 (e.g., the 386).
9733 Similarly, check for a case where the AND's are ZERO_EXTEND
9734 operations from some narrower mode even though a SUBREG is not
9735 present. */
9737 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9738 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9739 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9741 rtx inner_op0 = XEXP (op0, 0);
9742 rtx inner_op1 = XEXP (op1, 0);
9743 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9744 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9745 int changed = 0;
9747 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9748 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9749 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9750 && (GET_MODE (SUBREG_REG (inner_op0))
9751 == GET_MODE (SUBREG_REG (inner_op1)))
9752 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9753 <= HOST_BITS_PER_WIDE_INT)
9754 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9755 GET_MODE (SUBREG_REG (inner_op0)))))
9756 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9757 GET_MODE (SUBREG_REG (inner_op1))))))
9759 op0 = SUBREG_REG (inner_op0);
9760 op1 = SUBREG_REG (inner_op1);
9762 /* The resulting comparison is always unsigned since we masked
9763 off the original sign bit. */
9764 code = unsigned_condition (code);
9766 changed = 1;
9769 else if (c0 == c1)
9770 for (tmode = GET_CLASS_NARROWEST_MODE
9771 (GET_MODE_CLASS (GET_MODE (op0)));
9772 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9773 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9775 op0 = gen_lowpart (tmode, inner_op0);
9776 op1 = gen_lowpart (tmode, inner_op1);
9777 code = unsigned_condition (code);
9778 changed = 1;
9779 break;
9782 if (! changed)
9783 break;
9786 /* If both operands are NOT, we can strip off the outer operation
9787 and adjust the comparison code for swapped operands; similarly for
9788 NEG, except that this must be an equality comparison. */
9789 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9790 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9791 && (code == EQ || code == NE)))
9792 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9794 else
9795 break;
9798 /* If the first operand is a constant, swap the operands and adjust the
9799 comparison code appropriately, but don't do this if the second operand
9800 is already a constant integer. */
9801 if (swap_commutative_operands_p (op0, op1))
9803 tem = op0, op0 = op1, op1 = tem;
9804 code = swap_condition (code);
9807 /* We now enter a loop during which we will try to simplify the comparison.
9808 For the most part, we only are concerned with comparisons with zero,
9809 but some things may really be comparisons with zero but not start
9810 out looking that way. */
9812 while (GET_CODE (op1) == CONST_INT)
9814 enum machine_mode mode = GET_MODE (op0);
9815 unsigned int mode_width = GET_MODE_BITSIZE (mode);
9816 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9817 int equality_comparison_p;
9818 int sign_bit_comparison_p;
9819 int unsigned_comparison_p;
9820 HOST_WIDE_INT const_op;
9822 /* We only want to handle integral modes. This catches VOIDmode,
9823 CCmode, and the floating-point modes. An exception is that we
9824 can handle VOIDmode if OP0 is a COMPARE or a comparison
9825 operation. */
9827 if (GET_MODE_CLASS (mode) != MODE_INT
9828 && ! (mode == VOIDmode
9829 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9830 break;
9832 /* Get the constant we are comparing against and turn off all bits
9833 not on in our mode. */
9834 const_op = INTVAL (op1);
9835 if (mode != VOIDmode)
9836 const_op = trunc_int_for_mode (const_op, mode);
9837 op1 = GEN_INT (const_op);
9839 /* If we are comparing against a constant power of two and the value
9840 being compared can only have that single bit nonzero (e.g., it was
9841 `and'ed with that bit), we can replace this with a comparison
9842 with zero. */
9843 if (const_op
9844 && (code == EQ || code == NE || code == GE || code == GEU
9845 || code == LT || code == LTU)
9846 && mode_width <= HOST_BITS_PER_WIDE_INT
9847 && exact_log2 (const_op) >= 0
9848 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9850 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9851 op1 = const0_rtx, const_op = 0;
9854 /* Similarly, if we are comparing a value known to be either -1 or
9855 0 with -1, change it to the opposite comparison against zero. */
9857 if (const_op == -1
9858 && (code == EQ || code == NE || code == GT || code == LE
9859 || code == GEU || code == LTU)
9860 && num_sign_bit_copies (op0, mode) == mode_width)
9862 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9863 op1 = const0_rtx, const_op = 0;
9866 /* Do some canonicalizations based on the comparison code. We prefer
9867 comparisons against zero and then prefer equality comparisons.
9868 If we can reduce the size of a constant, we will do that too. */
9870 switch (code)
9872 case LT:
9873 /* < C is equivalent to <= (C - 1) */
9874 if (const_op > 0)
9876 const_op -= 1;
9877 op1 = GEN_INT (const_op);
9878 code = LE;
9879 /* ... fall through to LE case below. */
9881 else
9882 break;
9884 case LE:
9885 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9886 if (const_op < 0)
9888 const_op += 1;
9889 op1 = GEN_INT (const_op);
9890 code = LT;
9893 /* If we are doing a <= 0 comparison on a value known to have
9894 a zero sign bit, we can replace this with == 0. */
9895 else if (const_op == 0
9896 && mode_width <= HOST_BITS_PER_WIDE_INT
9897 && (nonzero_bits (op0, mode)
9898 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9899 code = EQ;
9900 break;
9902 case GE:
9903 /* >= C is equivalent to > (C - 1). */
9904 if (const_op > 0)
9906 const_op -= 1;
9907 op1 = GEN_INT (const_op);
9908 code = GT;
9909 /* ... fall through to GT below. */
9911 else
9912 break;
9914 case GT:
9915 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
9916 if (const_op < 0)
9918 const_op += 1;
9919 op1 = GEN_INT (const_op);
9920 code = GE;
9923 /* If we are doing a > 0 comparison on a value known to have
9924 a zero sign bit, we can replace this with != 0. */
9925 else if (const_op == 0
9926 && mode_width <= HOST_BITS_PER_WIDE_INT
9927 && (nonzero_bits (op0, mode)
9928 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9929 code = NE;
9930 break;
9932 case LTU:
9933 /* < C is equivalent to <= (C - 1). */
9934 if (const_op > 0)
9936 const_op -= 1;
9937 op1 = GEN_INT (const_op);
9938 code = LEU;
9939 /* ... fall through ... */
9942 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9943 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9944 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9946 const_op = 0, op1 = const0_rtx;
9947 code = GE;
9948 break;
9950 else
9951 break;
9953 case LEU:
9954 /* unsigned <= 0 is equivalent to == 0 */
9955 if (const_op == 0)
9956 code = EQ;
9958 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9959 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9960 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9962 const_op = 0, op1 = const0_rtx;
9963 code = GE;
9965 break;
9967 case GEU:
9968 /* >= C is equivalent to > (C - 1). */
9969 if (const_op > 1)
9971 const_op -= 1;
9972 op1 = GEN_INT (const_op);
9973 code = GTU;
9974 /* ... fall through ... */
9977 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9978 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9979 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9981 const_op = 0, op1 = const0_rtx;
9982 code = LT;
9983 break;
9985 else
9986 break;
9988 case GTU:
9989 /* unsigned > 0 is equivalent to != 0 */
9990 if (const_op == 0)
9991 code = NE;
9993 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9994 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9995 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9997 const_op = 0, op1 = const0_rtx;
9998 code = LT;
10000 break;
10002 default:
10003 break;
10006 /* Compute some predicates to simplify code below. */
10008 equality_comparison_p = (code == EQ || code == NE);
10009 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10010 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10011 || code == GEU);
10013 /* If this is a sign bit comparison and we can do arithmetic in
10014 MODE, say that we will only be needing the sign bit of OP0. */
10015 if (sign_bit_comparison_p
10016 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10017 op0 = force_to_mode (op0, mode,
10018 ((HOST_WIDE_INT) 1
10019 << (GET_MODE_BITSIZE (mode) - 1)),
10020 NULL_RTX, 0);
10022 /* Now try cases based on the opcode of OP0. If none of the cases
10023 does a "continue", we exit this loop immediately after the
10024 switch. */
10026 switch (GET_CODE (op0))
10028 case ZERO_EXTRACT:
10029 /* If we are extracting a single bit from a variable position in
10030 a constant that has only a single bit set and are comparing it
10031 with zero, we can convert this into an equality comparison
10032 between the position and the location of the single bit. */
10033 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10034 have already reduced the shift count modulo the word size. */
10035 if (!SHIFT_COUNT_TRUNCATED
10036 && GET_CODE (XEXP (op0, 0)) == CONST_INT
10037 && XEXP (op0, 1) == const1_rtx
10038 && equality_comparison_p && const_op == 0
10039 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10041 if (BITS_BIG_ENDIAN)
10043 enum machine_mode new_mode
10044 = mode_for_extraction (EP_extzv, 1);
10045 if (new_mode == MAX_MACHINE_MODE)
10046 i = BITS_PER_WORD - 1 - i;
10047 else
10049 mode = new_mode;
10050 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10054 op0 = XEXP (op0, 2);
10055 op1 = GEN_INT (i);
10056 const_op = i;
10058 /* Result is nonzero iff shift count is equal to I. */
10059 code = reverse_condition (code);
10060 continue;
10063 /* ... fall through ... */
10065 case SIGN_EXTRACT:
10066 tem = expand_compound_operation (op0);
10067 if (tem != op0)
10069 op0 = tem;
10070 continue;
10072 break;
10074 case NOT:
10075 /* If testing for equality, we can take the NOT of the constant. */
10076 if (equality_comparison_p
10077 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10079 op0 = XEXP (op0, 0);
10080 op1 = tem;
10081 continue;
10084 /* If just looking at the sign bit, reverse the sense of the
10085 comparison. */
10086 if (sign_bit_comparison_p)
10088 op0 = XEXP (op0, 0);
10089 code = (code == GE ? LT : GE);
10090 continue;
10092 break;
10094 case NEG:
10095 /* If testing for equality, we can take the NEG of the constant. */
10096 if (equality_comparison_p
10097 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10099 op0 = XEXP (op0, 0);
10100 op1 = tem;
10101 continue;
10104 /* The remaining cases only apply to comparisons with zero. */
10105 if (const_op != 0)
10106 break;
10108 /* When X is ABS or is known positive,
10109 (neg X) is < 0 if and only if X != 0. */
10111 if (sign_bit_comparison_p
10112 && (GET_CODE (XEXP (op0, 0)) == ABS
10113 || (mode_width <= HOST_BITS_PER_WIDE_INT
10114 && (nonzero_bits (XEXP (op0, 0), mode)
10115 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10117 op0 = XEXP (op0, 0);
10118 code = (code == LT ? NE : EQ);
10119 continue;
10122 /* If we have NEG of something whose two high-order bits are the
10123 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10124 if (num_sign_bit_copies (op0, mode) >= 2)
10126 op0 = XEXP (op0, 0);
10127 code = swap_condition (code);
10128 continue;
10130 break;
10132 case ROTATE:
10133 /* If we are testing equality and our count is a constant, we
10134 can perform the inverse operation on our RHS. */
10135 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10136 && (tem = simplify_binary_operation (ROTATERT, mode,
10137 op1, XEXP (op0, 1))) != 0)
10139 op0 = XEXP (op0, 0);
10140 op1 = tem;
10141 continue;
10144 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10145 a particular bit. Convert it to an AND of a constant of that
10146 bit. This will be converted into a ZERO_EXTRACT. */
10147 if (const_op == 0 && sign_bit_comparison_p
10148 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10149 && mode_width <= HOST_BITS_PER_WIDE_INT)
10151 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10152 ((HOST_WIDE_INT) 1
10153 << (mode_width - 1
10154 - INTVAL (XEXP (op0, 1)))));
10155 code = (code == LT ? NE : EQ);
10156 continue;
10159 /* Fall through. */
10161 case ABS:
10162 /* ABS is ignorable inside an equality comparison with zero. */
10163 if (const_op == 0 && equality_comparison_p)
10165 op0 = XEXP (op0, 0);
10166 continue;
10168 break;
10170 case SIGN_EXTEND:
10171 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10172 (compare FOO CONST) if CONST fits in FOO's mode and we
10173 are either testing inequality or have an unsigned
10174 comparison with ZERO_EXTEND or a signed comparison with
10175 SIGN_EXTEND. But don't do it if we don't have a compare
10176 insn of the given mode, since we'd have to revert it
10177 later on, and then we wouldn't know whether to sign- or
10178 zero-extend. */
10179 mode = GET_MODE (XEXP (op0, 0));
10180 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10181 && ! unsigned_comparison_p
10182 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10183 && ((unsigned HOST_WIDE_INT) const_op
10184 < (((unsigned HOST_WIDE_INT) 1
10185 << (GET_MODE_BITSIZE (mode) - 1))))
10186 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10188 op0 = XEXP (op0, 0);
10189 continue;
10191 break;
10193 case SUBREG:
10194 /* Check for the case where we are comparing A - C1 with C2, that is
10196 (subreg:MODE (plus (A) (-C1))) op (C2)
10198 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10199 comparison in the wider mode. One of the following two conditions
10200 must be true in order for this to be valid:
10202 1. The mode extension results in the same bit pattern being added
10203 on both sides and the comparison is equality or unsigned. As
10204 C2 has been truncated to fit in MODE, the pattern can only be
10205 all 0s or all 1s.
10207 2. The mode extension results in the sign bit being copied on
10208 each side.
10210 The difficulty here is that we have predicates for A but not for
10211 (A - C1) so we need to check that C1 is within proper bounds so
10212 as to perturbate A as little as possible. */
10214 if (mode_width <= HOST_BITS_PER_WIDE_INT
10215 && subreg_lowpart_p (op0)
10216 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10217 && GET_CODE (SUBREG_REG (op0)) == PLUS
10218 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10220 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10221 rtx a = XEXP (SUBREG_REG (op0), 0);
10222 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10224 if ((c1 > 0
10225 && (unsigned HOST_WIDE_INT) c1
10226 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10227 && (equality_comparison_p || unsigned_comparison_p)
10228 /* (A - C1) zero-extends if it is positive and sign-extends
10229 if it is negative, C2 both zero- and sign-extends. */
10230 && ((0 == (nonzero_bits (a, inner_mode)
10231 & ~GET_MODE_MASK (mode))
10232 && const_op >= 0)
10233 /* (A - C1) sign-extends if it is positive and 1-extends
10234 if it is negative, C2 both sign- and 1-extends. */
10235 || (num_sign_bit_copies (a, inner_mode)
10236 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10237 - mode_width)
10238 && const_op < 0)))
10239 || ((unsigned HOST_WIDE_INT) c1
10240 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10241 /* (A - C1) always sign-extends, like C2. */
10242 && num_sign_bit_copies (a, inner_mode)
10243 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10244 - (mode_width - 1))))
10246 op0 = SUBREG_REG (op0);
10247 continue;
10251 /* If the inner mode is narrower and we are extracting the low part,
10252 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10253 if (subreg_lowpart_p (op0)
10254 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10255 /* Fall through */ ;
10256 else
10257 break;
10259 /* ... fall through ... */
10261 case ZERO_EXTEND:
10262 mode = GET_MODE (XEXP (op0, 0));
10263 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10264 && (unsigned_comparison_p || equality_comparison_p)
10265 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10266 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10267 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10269 op0 = XEXP (op0, 0);
10270 continue;
10272 break;
10274 case PLUS:
10275 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10276 this for equality comparisons due to pathological cases involving
10277 overflows. */
10278 if (equality_comparison_p
10279 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10280 op1, XEXP (op0, 1))))
10282 op0 = XEXP (op0, 0);
10283 op1 = tem;
10284 continue;
10287 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10288 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10289 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10291 op0 = XEXP (XEXP (op0, 0), 0);
10292 code = (code == LT ? EQ : NE);
10293 continue;
10295 break;
10297 case MINUS:
10298 /* We used to optimize signed comparisons against zero, but that
10299 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10300 arrive here as equality comparisons, or (GEU, LTU) are
10301 optimized away. No need to special-case them. */
10303 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10304 (eq B (minus A C)), whichever simplifies. We can only do
10305 this for equality comparisons due to pathological cases involving
10306 overflows. */
10307 if (equality_comparison_p
10308 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10309 XEXP (op0, 1), op1)))
10311 op0 = XEXP (op0, 0);
10312 op1 = tem;
10313 continue;
10316 if (equality_comparison_p
10317 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10318 XEXP (op0, 0), op1)))
10320 op0 = XEXP (op0, 1);
10321 op1 = tem;
10322 continue;
10325 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10326 of bits in X minus 1, is one iff X > 0. */
10327 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10328 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10329 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10330 == mode_width - 1
10331 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10333 op0 = XEXP (op0, 1);
10334 code = (code == GE ? LE : GT);
10335 continue;
10337 break;
10339 case XOR:
10340 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10341 if C is zero or B is a constant. */
10342 if (equality_comparison_p
10343 && 0 != (tem = simplify_binary_operation (XOR, mode,
10344 XEXP (op0, 1), op1)))
10346 op0 = XEXP (op0, 0);
10347 op1 = tem;
10348 continue;
10350 break;
10352 case EQ: case NE:
10353 case UNEQ: case LTGT:
10354 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10355 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10356 case UNORDERED: case ORDERED:
10357 /* We can't do anything if OP0 is a condition code value, rather
10358 than an actual data value. */
10359 if (const_op != 0
10360 || CC0_P (XEXP (op0, 0))
10361 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10362 break;
10364 /* Get the two operands being compared. */
10365 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10366 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10367 else
10368 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10370 /* Check for the cases where we simply want the result of the
10371 earlier test or the opposite of that result. */
10372 if (code == NE || code == EQ
10373 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10374 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10375 && (STORE_FLAG_VALUE
10376 & (((HOST_WIDE_INT) 1
10377 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10378 && (code == LT || code == GE)))
10380 enum rtx_code new_code;
10381 if (code == LT || code == NE)
10382 new_code = GET_CODE (op0);
10383 else
10384 new_code = reversed_comparison_code (op0, NULL);
10386 if (new_code != UNKNOWN)
10388 code = new_code;
10389 op0 = tem;
10390 op1 = tem1;
10391 continue;
10394 break;
10396 case IOR:
10397 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10398 iff X <= 0. */
10399 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10400 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10401 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10403 op0 = XEXP (op0, 1);
10404 code = (code == GE ? GT : LE);
10405 continue;
10407 break;
10409 case AND:
10410 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10411 will be converted to a ZERO_EXTRACT later. */
10412 if (const_op == 0 && equality_comparison_p
10413 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10414 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10416 op0 = simplify_and_const_int
10417 (op0, mode, gen_rtx_LSHIFTRT (mode,
10418 XEXP (op0, 1),
10419 XEXP (XEXP (op0, 0), 1)),
10420 (HOST_WIDE_INT) 1);
10421 continue;
10424 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10425 zero and X is a comparison and C1 and C2 describe only bits set
10426 in STORE_FLAG_VALUE, we can compare with X. */
10427 if (const_op == 0 && equality_comparison_p
10428 && mode_width <= HOST_BITS_PER_WIDE_INT
10429 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10430 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10431 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10432 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10433 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10435 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10436 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10437 if ((~STORE_FLAG_VALUE & mask) == 0
10438 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10439 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10440 && COMPARISON_P (tem))))
10442 op0 = XEXP (XEXP (op0, 0), 0);
10443 continue;
10447 /* If we are doing an equality comparison of an AND of a bit equal
10448 to the sign bit, replace this with a LT or GE comparison of
10449 the underlying value. */
10450 if (equality_comparison_p
10451 && const_op == 0
10452 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10453 && mode_width <= HOST_BITS_PER_WIDE_INT
10454 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10455 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10457 op0 = XEXP (op0, 0);
10458 code = (code == EQ ? GE : LT);
10459 continue;
10462 /* If this AND operation is really a ZERO_EXTEND from a narrower
10463 mode, the constant fits within that mode, and this is either an
10464 equality or unsigned comparison, try to do this comparison in
10465 the narrower mode. */
10466 if ((equality_comparison_p || unsigned_comparison_p)
10467 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10468 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10469 & GET_MODE_MASK (mode))
10470 + 1)) >= 0
10471 && const_op >> i == 0
10472 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10474 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10475 continue;
10478 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10479 fits in both M1 and M2 and the SUBREG is either paradoxical
10480 or represents the low part, permute the SUBREG and the AND
10481 and try again. */
10482 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10484 unsigned HOST_WIDE_INT c1;
10485 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10486 /* Require an integral mode, to avoid creating something like
10487 (AND:SF ...). */
10488 if (SCALAR_INT_MODE_P (tmode)
10489 /* It is unsafe to commute the AND into the SUBREG if the
10490 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10491 not defined. As originally written the upper bits
10492 have a defined value due to the AND operation.
10493 However, if we commute the AND inside the SUBREG then
10494 they no longer have defined values and the meaning of
10495 the code has been changed. */
10496 && (0
10497 #ifdef WORD_REGISTER_OPERATIONS
10498 || (mode_width > GET_MODE_BITSIZE (tmode)
10499 && mode_width <= BITS_PER_WORD)
10500 #endif
10501 || (mode_width <= GET_MODE_BITSIZE (tmode)
10502 && subreg_lowpart_p (XEXP (op0, 0))))
10503 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10504 && mode_width <= HOST_BITS_PER_WIDE_INT
10505 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10506 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10507 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10508 && c1 != mask
10509 && c1 != GET_MODE_MASK (tmode))
10511 op0 = simplify_gen_binary (AND, tmode,
10512 SUBREG_REG (XEXP (op0, 0)),
10513 gen_int_mode (c1, tmode));
10514 op0 = gen_lowpart (mode, op0);
10515 continue;
10519 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10520 if (const_op == 0 && equality_comparison_p
10521 && XEXP (op0, 1) == const1_rtx
10522 && GET_CODE (XEXP (op0, 0)) == NOT)
10524 op0 = simplify_and_const_int
10525 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10526 code = (code == NE ? EQ : NE);
10527 continue;
10530 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10531 (eq (and (lshiftrt X) 1) 0).
10532 Also handle the case where (not X) is expressed using xor. */
10533 if (const_op == 0 && equality_comparison_p
10534 && XEXP (op0, 1) == const1_rtx
10535 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10537 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10538 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10540 if (GET_CODE (shift_op) == NOT
10541 || (GET_CODE (shift_op) == XOR
10542 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10543 && GET_CODE (shift_count) == CONST_INT
10544 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10545 && (INTVAL (XEXP (shift_op, 1))
10546 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10548 op0 = simplify_and_const_int
10549 (NULL_RTX, mode,
10550 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10551 (HOST_WIDE_INT) 1);
10552 code = (code == NE ? EQ : NE);
10553 continue;
10556 break;
10558 case ASHIFT:
10559 /* If we have (compare (ashift FOO N) (const_int C)) and
10560 the high order N bits of FOO (N+1 if an inequality comparison)
10561 are known to be zero, we can do this by comparing FOO with C
10562 shifted right N bits so long as the low-order N bits of C are
10563 zero. */
10564 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10565 && INTVAL (XEXP (op0, 1)) >= 0
10566 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10567 < HOST_BITS_PER_WIDE_INT)
10568 && ((const_op
10569 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10570 && mode_width <= HOST_BITS_PER_WIDE_INT
10571 && (nonzero_bits (XEXP (op0, 0), mode)
10572 & ~(mask >> (INTVAL (XEXP (op0, 1))
10573 + ! equality_comparison_p))) == 0)
10575 /* We must perform a logical shift, not an arithmetic one,
10576 as we want the top N bits of C to be zero. */
10577 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10579 temp >>= INTVAL (XEXP (op0, 1));
10580 op1 = gen_int_mode (temp, mode);
10581 op0 = XEXP (op0, 0);
10582 continue;
10585 /* If we are doing a sign bit comparison, it means we are testing
10586 a particular bit. Convert it to the appropriate AND. */
10587 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10588 && mode_width <= HOST_BITS_PER_WIDE_INT)
10590 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10591 ((HOST_WIDE_INT) 1
10592 << (mode_width - 1
10593 - INTVAL (XEXP (op0, 1)))));
10594 code = (code == LT ? NE : EQ);
10595 continue;
10598 /* If this an equality comparison with zero and we are shifting
10599 the low bit to the sign bit, we can convert this to an AND of the
10600 low-order bit. */
10601 if (const_op == 0 && equality_comparison_p
10602 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10603 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10604 == mode_width - 1)
10606 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10607 (HOST_WIDE_INT) 1);
10608 continue;
10610 break;
10612 case ASHIFTRT:
10613 /* If this is an equality comparison with zero, we can do this
10614 as a logical shift, which might be much simpler. */
10615 if (equality_comparison_p && const_op == 0
10616 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10618 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10619 XEXP (op0, 0),
10620 INTVAL (XEXP (op0, 1)));
10621 continue;
10624 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10625 do the comparison in a narrower mode. */
10626 if (! unsigned_comparison_p
10627 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10628 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10629 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10630 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10631 MODE_INT, 1)) != BLKmode
10632 && (((unsigned HOST_WIDE_INT) const_op
10633 + (GET_MODE_MASK (tmode) >> 1) + 1)
10634 <= GET_MODE_MASK (tmode)))
10636 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10637 continue;
10640 /* Likewise if OP0 is a PLUS of a sign extension with a
10641 constant, which is usually represented with the PLUS
10642 between the shifts. */
10643 if (! unsigned_comparison_p
10644 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10645 && GET_CODE (XEXP (op0, 0)) == PLUS
10646 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10647 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10648 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10649 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10650 MODE_INT, 1)) != BLKmode
10651 && (((unsigned HOST_WIDE_INT) const_op
10652 + (GET_MODE_MASK (tmode) >> 1) + 1)
10653 <= GET_MODE_MASK (tmode)))
10655 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10656 rtx add_const = XEXP (XEXP (op0, 0), 1);
10657 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10658 add_const, XEXP (op0, 1));
10660 op0 = simplify_gen_binary (PLUS, tmode,
10661 gen_lowpart (tmode, inner),
10662 new_const);
10663 continue;
10666 /* ... fall through ... */
10667 case LSHIFTRT:
10668 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10669 the low order N bits of FOO are known to be zero, we can do this
10670 by comparing FOO with C shifted left N bits so long as no
10671 overflow occurs. */
10672 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10673 && INTVAL (XEXP (op0, 1)) >= 0
10674 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10675 && mode_width <= HOST_BITS_PER_WIDE_INT
10676 && (nonzero_bits (XEXP (op0, 0), mode)
10677 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10678 && (((unsigned HOST_WIDE_INT) const_op
10679 + (GET_CODE (op0) != LSHIFTRT
10680 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10681 + 1)
10682 : 0))
10683 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10685 /* If the shift was logical, then we must make the condition
10686 unsigned. */
10687 if (GET_CODE (op0) == LSHIFTRT)
10688 code = unsigned_condition (code);
10690 const_op <<= INTVAL (XEXP (op0, 1));
10691 op1 = GEN_INT (const_op);
10692 op0 = XEXP (op0, 0);
10693 continue;
10696 /* If we are using this shift to extract just the sign bit, we
10697 can replace this with an LT or GE comparison. */
10698 if (const_op == 0
10699 && (equality_comparison_p || sign_bit_comparison_p)
10700 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10701 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10702 == mode_width - 1)
10704 op0 = XEXP (op0, 0);
10705 code = (code == NE || code == GT ? LT : GE);
10706 continue;
10708 break;
10710 default:
10711 break;
10714 break;
10717 /* Now make any compound operations involved in this comparison. Then,
10718 check for an outmost SUBREG on OP0 that is not doing anything or is
10719 paradoxical. The latter transformation must only be performed when
10720 it is known that the "extra" bits will be the same in op0 and op1 or
10721 that they don't matter. There are three cases to consider:
10723 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10724 care bits and we can assume they have any convenient value. So
10725 making the transformation is safe.
10727 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10728 In this case the upper bits of op0 are undefined. We should not make
10729 the simplification in that case as we do not know the contents of
10730 those bits.
10732 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10733 UNKNOWN. In that case we know those bits are zeros or ones. We must
10734 also be sure that they are the same as the upper bits of op1.
10736 We can never remove a SUBREG for a non-equality comparison because
10737 the sign bit is in a different place in the underlying object. */
10739 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10740 op1 = make_compound_operation (op1, SET);
10742 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10743 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10744 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10745 && (code == NE || code == EQ))
10747 if (GET_MODE_SIZE (GET_MODE (op0))
10748 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10750 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
10751 implemented. */
10752 if (REG_P (SUBREG_REG (op0)))
10754 op0 = SUBREG_REG (op0);
10755 op1 = gen_lowpart (GET_MODE (op0), op1);
10758 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10759 <= HOST_BITS_PER_WIDE_INT)
10760 && (nonzero_bits (SUBREG_REG (op0),
10761 GET_MODE (SUBREG_REG (op0)))
10762 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10764 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10766 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10767 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10768 op0 = SUBREG_REG (op0), op1 = tem;
10772 /* We now do the opposite procedure: Some machines don't have compare
10773 insns in all modes. If OP0's mode is an integer mode smaller than a
10774 word and we can't do a compare in that mode, see if there is a larger
10775 mode for which we can do the compare. There are a number of cases in
10776 which we can use the wider mode. */
10778 mode = GET_MODE (op0);
10779 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10780 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10781 && ! have_insn_for (COMPARE, mode))
10782 for (tmode = GET_MODE_WIDER_MODE (mode);
10783 (tmode != VOIDmode
10784 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10785 tmode = GET_MODE_WIDER_MODE (tmode))
10786 if (have_insn_for (COMPARE, tmode))
10788 int zero_extended;
10790 /* If the only nonzero bits in OP0 and OP1 are those in the
10791 narrower mode and this is an equality or unsigned comparison,
10792 we can use the wider mode. Similarly for sign-extended
10793 values, in which case it is true for all comparisons. */
10794 zero_extended = ((code == EQ || code == NE
10795 || code == GEU || code == GTU
10796 || code == LEU || code == LTU)
10797 && (nonzero_bits (op0, tmode)
10798 & ~GET_MODE_MASK (mode)) == 0
10799 && ((GET_CODE (op1) == CONST_INT
10800 || (nonzero_bits (op1, tmode)
10801 & ~GET_MODE_MASK (mode)) == 0)));
10803 if (zero_extended
10804 || ((num_sign_bit_copies (op0, tmode)
10805 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10806 - GET_MODE_BITSIZE (mode)))
10807 && (num_sign_bit_copies (op1, tmode)
10808 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10809 - GET_MODE_BITSIZE (mode)))))
10811 /* If OP0 is an AND and we don't have an AND in MODE either,
10812 make a new AND in the proper mode. */
10813 if (GET_CODE (op0) == AND
10814 && !have_insn_for (AND, mode))
10815 op0 = simplify_gen_binary (AND, tmode,
10816 gen_lowpart (tmode,
10817 XEXP (op0, 0)),
10818 gen_lowpart (tmode,
10819 XEXP (op0, 1)));
10821 op0 = gen_lowpart (tmode, op0);
10822 if (zero_extended && GET_CODE (op1) == CONST_INT)
10823 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10824 op1 = gen_lowpart (tmode, op1);
10825 break;
10828 /* If this is a test for negative, we can make an explicit
10829 test of the sign bit. */
10831 if (op1 == const0_rtx && (code == LT || code == GE)
10832 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10834 op0 = simplify_gen_binary (AND, tmode,
10835 gen_lowpart (tmode, op0),
10836 GEN_INT ((HOST_WIDE_INT) 1
10837 << (GET_MODE_BITSIZE (mode)
10838 - 1)));
10839 code = (code == LT) ? NE : EQ;
10840 break;
10844 #ifdef CANONICALIZE_COMPARISON
10845 /* If this machine only supports a subset of valid comparisons, see if we
10846 can convert an unsupported one into a supported one. */
10847 CANONICALIZE_COMPARISON (code, op0, op1);
10848 #endif
10850 *pop0 = op0;
10851 *pop1 = op1;
10853 return code;
10856 /* Utility function for record_value_for_reg. Count number of
10857 rtxs in X. */
10858 static int
10859 count_rtxs (rtx x)
10861 enum rtx_code code = GET_CODE (x);
10862 const char *fmt;
10863 int i, ret = 1;
10865 if (GET_RTX_CLASS (code) == '2'
10866 || GET_RTX_CLASS (code) == 'c')
10868 rtx x0 = XEXP (x, 0);
10869 rtx x1 = XEXP (x, 1);
10871 if (x0 == x1)
10872 return 1 + 2 * count_rtxs (x0);
10874 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
10875 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
10876 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10877 return 2 + 2 * count_rtxs (x0)
10878 + count_rtxs (x == XEXP (x1, 0)
10879 ? XEXP (x1, 1) : XEXP (x1, 0));
10881 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
10882 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
10883 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10884 return 2 + 2 * count_rtxs (x1)
10885 + count_rtxs (x == XEXP (x0, 0)
10886 ? XEXP (x0, 1) : XEXP (x0, 0));
10889 fmt = GET_RTX_FORMAT (code);
10890 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10891 if (fmt[i] == 'e')
10892 ret += count_rtxs (XEXP (x, i));
10894 return ret;
10897 /* Utility function for following routine. Called when X is part of a value
10898 being stored into last_set_value. Sets last_set_table_tick
10899 for each register mentioned. Similar to mention_regs in cse.c */
10901 static void
10902 update_table_tick (rtx x)
10904 enum rtx_code code = GET_CODE (x);
10905 const char *fmt = GET_RTX_FORMAT (code);
10906 int i;
10908 if (code == REG)
10910 unsigned int regno = REGNO (x);
10911 unsigned int endregno
10912 = regno + (regno < FIRST_PSEUDO_REGISTER
10913 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10914 unsigned int r;
10916 for (r = regno; r < endregno; r++)
10917 reg_stat[r].last_set_table_tick = label_tick;
10919 return;
10922 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10923 /* Note that we can't have an "E" in values stored; see
10924 get_last_value_validate. */
10925 if (fmt[i] == 'e')
10927 /* Check for identical subexpressions. If x contains
10928 identical subexpression we only have to traverse one of
10929 them. */
10930 if (i == 0 && ARITHMETIC_P (x))
10932 /* Note that at this point x1 has already been
10933 processed. */
10934 rtx x0 = XEXP (x, 0);
10935 rtx x1 = XEXP (x, 1);
10937 /* If x0 and x1 are identical then there is no need to
10938 process x0. */
10939 if (x0 == x1)
10940 break;
10942 /* If x0 is identical to a subexpression of x1 then while
10943 processing x1, x0 has already been processed. Thus we
10944 are done with x. */
10945 if (ARITHMETIC_P (x1)
10946 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10947 break;
10949 /* If x1 is identical to a subexpression of x0 then we
10950 still have to process the rest of x0. */
10951 if (ARITHMETIC_P (x0)
10952 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10954 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10955 break;
10959 update_table_tick (XEXP (x, i));
10963 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10964 are saying that the register is clobbered and we no longer know its
10965 value. If INSN is zero, don't update reg_stat[].last_set; this is
10966 only permitted with VALUE also zero and is used to invalidate the
10967 register. */
10969 static void
10970 record_value_for_reg (rtx reg, rtx insn, rtx value)
10972 unsigned int regno = REGNO (reg);
10973 unsigned int endregno
10974 = regno + (regno < FIRST_PSEUDO_REGISTER
10975 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10976 unsigned int i;
10978 /* If VALUE contains REG and we have a previous value for REG, substitute
10979 the previous value. */
10980 if (value && insn && reg_overlap_mentioned_p (reg, value))
10982 rtx tem;
10984 /* Set things up so get_last_value is allowed to see anything set up to
10985 our insn. */
10986 subst_low_cuid = INSN_CUID (insn);
10987 tem = get_last_value (reg);
10989 /* If TEM is simply a binary operation with two CLOBBERs as operands,
10990 it isn't going to be useful and will take a lot of time to process,
10991 so just use the CLOBBER. */
10993 if (tem)
10995 if (ARITHMETIC_P (tem)
10996 && GET_CODE (XEXP (tem, 0)) == CLOBBER
10997 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10998 tem = XEXP (tem, 0);
10999 else if (count_occurrences (value, reg, 1) >= 2)
11001 /* If there are two or more occurrences of REG in VALUE,
11002 prevent the value from growing too much. */
11003 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11004 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11007 value = replace_rtx (copy_rtx (value), reg, tem);
11011 /* For each register modified, show we don't know its value, that
11012 we don't know about its bitwise content, that its value has been
11013 updated, and that we don't know the location of the death of the
11014 register. */
11015 for (i = regno; i < endregno; i++)
11017 if (insn)
11018 reg_stat[i].last_set = insn;
11020 reg_stat[i].last_set_value = 0;
11021 reg_stat[i].last_set_mode = 0;
11022 reg_stat[i].last_set_nonzero_bits = 0;
11023 reg_stat[i].last_set_sign_bit_copies = 0;
11024 reg_stat[i].last_death = 0;
11027 /* Mark registers that are being referenced in this value. */
11028 if (value)
11029 update_table_tick (value);
11031 /* Now update the status of each register being set.
11032 If someone is using this register in this block, set this register
11033 to invalid since we will get confused between the two lives in this
11034 basic block. This makes using this register always invalid. In cse, we
11035 scan the table to invalidate all entries using this register, but this
11036 is too much work for us. */
11038 for (i = regno; i < endregno; i++)
11040 reg_stat[i].last_set_label = label_tick;
11041 if (value && reg_stat[i].last_set_table_tick == label_tick)
11042 reg_stat[i].last_set_invalid = 1;
11043 else
11044 reg_stat[i].last_set_invalid = 0;
11047 /* The value being assigned might refer to X (like in "x++;"). In that
11048 case, we must replace it with (clobber (const_int 0)) to prevent
11049 infinite loops. */
11050 if (value && ! get_last_value_validate (&value, insn,
11051 reg_stat[regno].last_set_label, 0))
11053 value = copy_rtx (value);
11054 if (! get_last_value_validate (&value, insn,
11055 reg_stat[regno].last_set_label, 1))
11056 value = 0;
11059 /* For the main register being modified, update the value, the mode, the
11060 nonzero bits, and the number of sign bit copies. */
11062 reg_stat[regno].last_set_value = value;
11064 if (value)
11066 enum machine_mode mode = GET_MODE (reg);
11067 subst_low_cuid = INSN_CUID (insn);
11068 reg_stat[regno].last_set_mode = mode;
11069 if (GET_MODE_CLASS (mode) == MODE_INT
11070 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11071 mode = nonzero_bits_mode;
11072 reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
11073 reg_stat[regno].last_set_sign_bit_copies
11074 = num_sign_bit_copies (value, GET_MODE (reg));
11078 /* Called via note_stores from record_dead_and_set_regs to handle one
11079 SET or CLOBBER in an insn. DATA is the instruction in which the
11080 set is occurring. */
11082 static void
11083 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11085 rtx record_dead_insn = (rtx) data;
11087 if (GET_CODE (dest) == SUBREG)
11088 dest = SUBREG_REG (dest);
11090 if (REG_P (dest))
11092 /* If we are setting the whole register, we know its value. Otherwise
11093 show that we don't know the value. We can handle SUBREG in
11094 some cases. */
11095 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11096 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11097 else if (GET_CODE (setter) == SET
11098 && GET_CODE (SET_DEST (setter)) == SUBREG
11099 && SUBREG_REG (SET_DEST (setter)) == dest
11100 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11101 && subreg_lowpart_p (SET_DEST (setter)))
11102 record_value_for_reg (dest, record_dead_insn,
11103 gen_lowpart (GET_MODE (dest),
11104 SET_SRC (setter)));
11105 else
11106 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11108 else if (MEM_P (dest)
11109 /* Ignore pushes, they clobber nothing. */
11110 && ! push_operand (dest, GET_MODE (dest)))
11111 mem_last_set = INSN_CUID (record_dead_insn);
11114 /* Update the records of when each REG was most recently set or killed
11115 for the things done by INSN. This is the last thing done in processing
11116 INSN in the combiner loop.
11118 We update reg_stat[], in particular fields last_set, last_set_value,
11119 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11120 last_death, and also the similar information mem_last_set (which insn
11121 most recently modified memory) and last_call_cuid (which insn was the
11122 most recent subroutine call). */
11124 static void
11125 record_dead_and_set_regs (rtx insn)
11127 rtx link;
11128 unsigned int i;
11130 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11132 if (REG_NOTE_KIND (link) == REG_DEAD
11133 && REG_P (XEXP (link, 0)))
11135 unsigned int regno = REGNO (XEXP (link, 0));
11136 unsigned int endregno
11137 = regno + (regno < FIRST_PSEUDO_REGISTER
11138 ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11139 : 1);
11141 for (i = regno; i < endregno; i++)
11142 reg_stat[i].last_death = insn;
11144 else if (REG_NOTE_KIND (link) == REG_INC)
11145 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11148 if (CALL_P (insn))
11150 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11151 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11153 reg_stat[i].last_set_value = 0;
11154 reg_stat[i].last_set_mode = 0;
11155 reg_stat[i].last_set_nonzero_bits = 0;
11156 reg_stat[i].last_set_sign_bit_copies = 0;
11157 reg_stat[i].last_death = 0;
11160 last_call_cuid = mem_last_set = INSN_CUID (insn);
11162 /* Don't bother recording what this insn does. It might set the
11163 return value register, but we can't combine into a call
11164 pattern anyway, so there's no point trying (and it may cause
11165 a crash, if e.g. we wind up asking for last_set_value of a
11166 SUBREG of the return value register). */
11167 return;
11170 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11173 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11174 register present in the SUBREG, so for each such SUBREG go back and
11175 adjust nonzero and sign bit information of the registers that are
11176 known to have some zero/sign bits set.
11178 This is needed because when combine blows the SUBREGs away, the
11179 information on zero/sign bits is lost and further combines can be
11180 missed because of that. */
11182 static void
11183 record_promoted_value (rtx insn, rtx subreg)
11185 rtx links, set;
11186 unsigned int regno = REGNO (SUBREG_REG (subreg));
11187 enum machine_mode mode = GET_MODE (subreg);
11189 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11190 return;
11192 for (links = LOG_LINKS (insn); links;)
11194 insn = XEXP (links, 0);
11195 set = single_set (insn);
11197 if (! set || !REG_P (SET_DEST (set))
11198 || REGNO (SET_DEST (set)) != regno
11199 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11201 links = XEXP (links, 1);
11202 continue;
11205 if (reg_stat[regno].last_set == insn)
11207 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11208 reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11211 if (REG_P (SET_SRC (set)))
11213 regno = REGNO (SET_SRC (set));
11214 links = LOG_LINKS (insn);
11216 else
11217 break;
11221 /* Scan X for promoted SUBREGs. For each one found,
11222 note what it implies to the registers used in it. */
11224 static void
11225 check_promoted_subreg (rtx insn, rtx x)
11227 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11228 && REG_P (SUBREG_REG (x)))
11229 record_promoted_value (insn, x);
11230 else
11232 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11233 int i, j;
11235 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11236 switch (format[i])
11238 case 'e':
11239 check_promoted_subreg (insn, XEXP (x, i));
11240 break;
11241 case 'V':
11242 case 'E':
11243 if (XVEC (x, i) != 0)
11244 for (j = 0; j < XVECLEN (x, i); j++)
11245 check_promoted_subreg (insn, XVECEXP (x, i, j));
11246 break;
11251 /* Utility routine for the following function. Verify that all the registers
11252 mentioned in *LOC are valid when *LOC was part of a value set when
11253 label_tick == TICK. Return 0 if some are not.
11255 If REPLACE is nonzero, replace the invalid reference with
11256 (clobber (const_int 0)) and return 1. This replacement is useful because
11257 we often can get useful information about the form of a value (e.g., if
11258 it was produced by a shift that always produces -1 or 0) even though
11259 we don't know exactly what registers it was produced from. */
11261 static int
11262 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11264 rtx x = *loc;
11265 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11266 int len = GET_RTX_LENGTH (GET_CODE (x));
11267 int i;
11269 if (REG_P (x))
11271 unsigned int regno = REGNO (x);
11272 unsigned int endregno
11273 = regno + (regno < FIRST_PSEUDO_REGISTER
11274 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11275 unsigned int j;
11277 for (j = regno; j < endregno; j++)
11278 if (reg_stat[j].last_set_invalid
11279 /* If this is a pseudo-register that was only set once and not
11280 live at the beginning of the function, it is always valid. */
11281 || (! (regno >= FIRST_PSEUDO_REGISTER
11282 && REG_N_SETS (regno) == 1
11283 && (! REGNO_REG_SET_P
11284 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
11285 regno)))
11286 && reg_stat[j].last_set_label > tick))
11288 if (replace)
11289 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11290 return replace;
11293 return 1;
11295 /* If this is a memory reference, make sure that there were
11296 no stores after it that might have clobbered the value. We don't
11297 have alias info, so we assume any store invalidates it. */
11298 else if (MEM_P (x) && !MEM_READONLY_P (x)
11299 && INSN_CUID (insn) <= mem_last_set)
11301 if (replace)
11302 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11303 return replace;
11306 for (i = 0; i < len; i++)
11308 if (fmt[i] == 'e')
11310 /* Check for identical subexpressions. If x contains
11311 identical subexpression we only have to traverse one of
11312 them. */
11313 if (i == 1 && ARITHMETIC_P (x))
11315 /* Note that at this point x0 has already been checked
11316 and found valid. */
11317 rtx x0 = XEXP (x, 0);
11318 rtx x1 = XEXP (x, 1);
11320 /* If x0 and x1 are identical then x is also valid. */
11321 if (x0 == x1)
11322 return 1;
11324 /* If x1 is identical to a subexpression of x0 then
11325 while checking x0, x1 has already been checked. Thus
11326 it is valid and so as x. */
11327 if (ARITHMETIC_P (x0)
11328 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11329 return 1;
11331 /* If x0 is identical to a subexpression of x1 then x is
11332 valid iff the rest of x1 is valid. */
11333 if (ARITHMETIC_P (x1)
11334 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11335 return
11336 get_last_value_validate (&XEXP (x1,
11337 x0 == XEXP (x1, 0) ? 1 : 0),
11338 insn, tick, replace);
11341 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11342 replace) == 0)
11343 return 0;
11345 /* Don't bother with these. They shouldn't occur anyway. */
11346 else if (fmt[i] == 'E')
11347 return 0;
11350 /* If we haven't found a reason for it to be invalid, it is valid. */
11351 return 1;
11354 /* Get the last value assigned to X, if known. Some registers
11355 in the value may be replaced with (clobber (const_int 0)) if their value
11356 is known longer known reliably. */
11358 static rtx
11359 get_last_value (rtx x)
11361 unsigned int regno;
11362 rtx value;
11364 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11365 then convert it to the desired mode. If this is a paradoxical SUBREG,
11366 we cannot predict what values the "extra" bits might have. */
11367 if (GET_CODE (x) == SUBREG
11368 && subreg_lowpart_p (x)
11369 && (GET_MODE_SIZE (GET_MODE (x))
11370 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11371 && (value = get_last_value (SUBREG_REG (x))) != 0)
11372 return gen_lowpart (GET_MODE (x), value);
11374 if (!REG_P (x))
11375 return 0;
11377 regno = REGNO (x);
11378 value = reg_stat[regno].last_set_value;
11380 /* If we don't have a value, or if it isn't for this basic block and
11381 it's either a hard register, set more than once, or it's a live
11382 at the beginning of the function, return 0.
11384 Because if it's not live at the beginning of the function then the reg
11385 is always set before being used (is never used without being set).
11386 And, if it's set only once, and it's always set before use, then all
11387 uses must have the same last value, even if it's not from this basic
11388 block. */
11390 if (value == 0
11391 || (reg_stat[regno].last_set_label != label_tick
11392 && (regno < FIRST_PSEUDO_REGISTER
11393 || REG_N_SETS (regno) != 1
11394 || (REGNO_REG_SET_P
11395 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
11396 regno)))))
11397 return 0;
11399 /* If the value was set in a later insn than the ones we are processing,
11400 we can't use it even if the register was only set once. */
11401 if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11402 return 0;
11404 /* If the value has all its registers valid, return it. */
11405 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11406 reg_stat[regno].last_set_label, 0))
11407 return value;
11409 /* Otherwise, make a copy and replace any invalid register with
11410 (clobber (const_int 0)). If that fails for some reason, return 0. */
11412 value = copy_rtx (value);
11413 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11414 reg_stat[regno].last_set_label, 1))
11415 return value;
11417 return 0;
11420 /* Return nonzero if expression X refers to a REG or to memory
11421 that is set in an instruction more recent than FROM_CUID. */
11423 static int
11424 use_crosses_set_p (rtx x, int from_cuid)
11426 const char *fmt;
11427 int i;
11428 enum rtx_code code = GET_CODE (x);
11430 if (code == REG)
11432 unsigned int regno = REGNO (x);
11433 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11434 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11436 #ifdef PUSH_ROUNDING
11437 /* Don't allow uses of the stack pointer to be moved,
11438 because we don't know whether the move crosses a push insn. */
11439 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11440 return 1;
11441 #endif
11442 for (; regno < endreg; regno++)
11443 if (reg_stat[regno].last_set
11444 && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11445 return 1;
11446 return 0;
11449 if (code == MEM && mem_last_set > from_cuid)
11450 return 1;
11452 fmt = GET_RTX_FORMAT (code);
11454 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11456 if (fmt[i] == 'E')
11458 int j;
11459 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11460 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11461 return 1;
11463 else if (fmt[i] == 'e'
11464 && use_crosses_set_p (XEXP (x, i), from_cuid))
11465 return 1;
11467 return 0;
11470 /* Define three variables used for communication between the following
11471 routines. */
11473 static unsigned int reg_dead_regno, reg_dead_endregno;
11474 static int reg_dead_flag;
11476 /* Function called via note_stores from reg_dead_at_p.
11478 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11479 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11481 static void
11482 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11484 unsigned int regno, endregno;
11486 if (!REG_P (dest))
11487 return;
11489 regno = REGNO (dest);
11490 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11491 ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11493 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11494 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11497 /* Return nonzero if REG is known to be dead at INSN.
11499 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11500 referencing REG, it is dead. If we hit a SET referencing REG, it is
11501 live. Otherwise, see if it is live or dead at the start of the basic
11502 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11503 must be assumed to be always live. */
11505 static int
11506 reg_dead_at_p (rtx reg, rtx insn)
11508 basic_block block;
11509 unsigned int i;
11511 /* Set variables for reg_dead_at_p_1. */
11512 reg_dead_regno = REGNO (reg);
11513 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11514 ? hard_regno_nregs[reg_dead_regno]
11515 [GET_MODE (reg)]
11516 : 1);
11518 reg_dead_flag = 0;
11520 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11521 we allow the machine description to decide whether use-and-clobber
11522 patterns are OK. */
11523 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11525 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11526 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11527 return 0;
11530 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11531 beginning of function. */
11532 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11533 insn = prev_nonnote_insn (insn))
11535 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11536 if (reg_dead_flag)
11537 return reg_dead_flag == 1 ? 1 : 0;
11539 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11540 return 1;
11543 /* Get the basic block that we were in. */
11544 if (insn == 0)
11545 block = ENTRY_BLOCK_PTR->next_bb;
11546 else
11548 FOR_EACH_BB (block)
11549 if (insn == BB_HEAD (block))
11550 break;
11552 if (block == EXIT_BLOCK_PTR)
11553 return 0;
11556 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11557 if (REGNO_REG_SET_P (block->il.rtl->global_live_at_start, i))
11558 return 0;
11560 return 1;
11563 /* Note hard registers in X that are used. This code is similar to
11564 that in flow.c, but much simpler since we don't care about pseudos. */
11566 static void
11567 mark_used_regs_combine (rtx x)
11569 RTX_CODE code = GET_CODE (x);
11570 unsigned int regno;
11571 int i;
11573 switch (code)
11575 case LABEL_REF:
11576 case SYMBOL_REF:
11577 case CONST_INT:
11578 case CONST:
11579 case CONST_DOUBLE:
11580 case CONST_VECTOR:
11581 case PC:
11582 case ADDR_VEC:
11583 case ADDR_DIFF_VEC:
11584 case ASM_INPUT:
11585 #ifdef HAVE_cc0
11586 /* CC0 must die in the insn after it is set, so we don't need to take
11587 special note of it here. */
11588 case CC0:
11589 #endif
11590 return;
11592 case CLOBBER:
11593 /* If we are clobbering a MEM, mark any hard registers inside the
11594 address as used. */
11595 if (MEM_P (XEXP (x, 0)))
11596 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11597 return;
11599 case REG:
11600 regno = REGNO (x);
11601 /* A hard reg in a wide mode may really be multiple registers.
11602 If so, mark all of them just like the first. */
11603 if (regno < FIRST_PSEUDO_REGISTER)
11605 unsigned int endregno, r;
11607 /* None of this applies to the stack, frame or arg pointers. */
11608 if (regno == STACK_POINTER_REGNUM
11609 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11610 || regno == HARD_FRAME_POINTER_REGNUM
11611 #endif
11612 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11613 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11614 #endif
11615 || regno == FRAME_POINTER_REGNUM)
11616 return;
11618 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11619 for (r = regno; r < endregno; r++)
11620 SET_HARD_REG_BIT (newpat_used_regs, r);
11622 return;
11624 case SET:
11626 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11627 the address. */
11628 rtx testreg = SET_DEST (x);
11630 while (GET_CODE (testreg) == SUBREG
11631 || GET_CODE (testreg) == ZERO_EXTRACT
11632 || GET_CODE (testreg) == STRICT_LOW_PART)
11633 testreg = XEXP (testreg, 0);
11635 if (MEM_P (testreg))
11636 mark_used_regs_combine (XEXP (testreg, 0));
11638 mark_used_regs_combine (SET_SRC (x));
11640 return;
11642 default:
11643 break;
11646 /* Recursively scan the operands of this expression. */
11649 const char *fmt = GET_RTX_FORMAT (code);
11651 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11653 if (fmt[i] == 'e')
11654 mark_used_regs_combine (XEXP (x, i));
11655 else if (fmt[i] == 'E')
11657 int j;
11659 for (j = 0; j < XVECLEN (x, i); j++)
11660 mark_used_regs_combine (XVECEXP (x, i, j));
11666 /* Remove register number REGNO from the dead registers list of INSN.
11668 Return the note used to record the death, if there was one. */
11671 remove_death (unsigned int regno, rtx insn)
11673 rtx note = find_regno_note (insn, REG_DEAD, regno);
11675 if (note)
11677 REG_N_DEATHS (regno)--;
11678 remove_note (insn, note);
11681 return note;
11684 /* For each register (hardware or pseudo) used within expression X, if its
11685 death is in an instruction with cuid between FROM_CUID (inclusive) and
11686 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11687 list headed by PNOTES.
11689 That said, don't move registers killed by maybe_kill_insn.
11691 This is done when X is being merged by combination into TO_INSN. These
11692 notes will then be distributed as needed. */
11694 static void
11695 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11696 rtx *pnotes)
11698 const char *fmt;
11699 int len, i;
11700 enum rtx_code code = GET_CODE (x);
11702 if (code == REG)
11704 unsigned int regno = REGNO (x);
11705 rtx where_dead = reg_stat[regno].last_death;
11706 rtx before_dead, after_dead;
11708 /* Don't move the register if it gets killed in between from and to. */
11709 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11710 && ! reg_referenced_p (x, maybe_kill_insn))
11711 return;
11713 /* WHERE_DEAD could be a USE insn made by combine, so first we
11714 make sure that we have insns with valid INSN_CUID values. */
11715 before_dead = where_dead;
11716 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11717 before_dead = PREV_INSN (before_dead);
11719 after_dead = where_dead;
11720 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11721 after_dead = NEXT_INSN (after_dead);
11723 if (before_dead && after_dead
11724 && INSN_CUID (before_dead) >= from_cuid
11725 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11726 || (where_dead != after_dead
11727 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11729 rtx note = remove_death (regno, where_dead);
11731 /* It is possible for the call above to return 0. This can occur
11732 when last_death points to I2 or I1 that we combined with.
11733 In that case make a new note.
11735 We must also check for the case where X is a hard register
11736 and NOTE is a death note for a range of hard registers
11737 including X. In that case, we must put REG_DEAD notes for
11738 the remaining registers in place of NOTE. */
11740 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11741 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11742 > GET_MODE_SIZE (GET_MODE (x))))
11744 unsigned int deadregno = REGNO (XEXP (note, 0));
11745 unsigned int deadend
11746 = (deadregno + hard_regno_nregs[deadregno]
11747 [GET_MODE (XEXP (note, 0))]);
11748 unsigned int ourend
11749 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11750 unsigned int i;
11752 for (i = deadregno; i < deadend; i++)
11753 if (i < regno || i >= ourend)
11754 REG_NOTES (where_dead)
11755 = gen_rtx_EXPR_LIST (REG_DEAD,
11756 regno_reg_rtx[i],
11757 REG_NOTES (where_dead));
11760 /* If we didn't find any note, or if we found a REG_DEAD note that
11761 covers only part of the given reg, and we have a multi-reg hard
11762 register, then to be safe we must check for REG_DEAD notes
11763 for each register other than the first. They could have
11764 their own REG_DEAD notes lying around. */
11765 else if ((note == 0
11766 || (note != 0
11767 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11768 < GET_MODE_SIZE (GET_MODE (x)))))
11769 && regno < FIRST_PSEUDO_REGISTER
11770 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11772 unsigned int ourend
11773 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11774 unsigned int i, offset;
11775 rtx oldnotes = 0;
11777 if (note)
11778 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11779 else
11780 offset = 1;
11782 for (i = regno + offset; i < ourend; i++)
11783 move_deaths (regno_reg_rtx[i],
11784 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11787 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11789 XEXP (note, 1) = *pnotes;
11790 *pnotes = note;
11792 else
11793 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11795 REG_N_DEATHS (regno)++;
11798 return;
11801 else if (GET_CODE (x) == SET)
11803 rtx dest = SET_DEST (x);
11805 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11807 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11808 that accesses one word of a multi-word item, some
11809 piece of everything register in the expression is used by
11810 this insn, so remove any old death. */
11811 /* ??? So why do we test for equality of the sizes? */
11813 if (GET_CODE (dest) == ZERO_EXTRACT
11814 || GET_CODE (dest) == STRICT_LOW_PART
11815 || (GET_CODE (dest) == SUBREG
11816 && (((GET_MODE_SIZE (GET_MODE (dest))
11817 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11818 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11819 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11821 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11822 return;
11825 /* If this is some other SUBREG, we know it replaces the entire
11826 value, so use that as the destination. */
11827 if (GET_CODE (dest) == SUBREG)
11828 dest = SUBREG_REG (dest);
11830 /* If this is a MEM, adjust deaths of anything used in the address.
11831 For a REG (the only other possibility), the entire value is
11832 being replaced so the old value is not used in this insn. */
11834 if (MEM_P (dest))
11835 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11836 to_insn, pnotes);
11837 return;
11840 else if (GET_CODE (x) == CLOBBER)
11841 return;
11843 len = GET_RTX_LENGTH (code);
11844 fmt = GET_RTX_FORMAT (code);
11846 for (i = 0; i < len; i++)
11848 if (fmt[i] == 'E')
11850 int j;
11851 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11852 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11853 to_insn, pnotes);
11855 else if (fmt[i] == 'e')
11856 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11860 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11861 pattern of an insn. X must be a REG. */
11863 static int
11864 reg_bitfield_target_p (rtx x, rtx body)
11866 int i;
11868 if (GET_CODE (body) == SET)
11870 rtx dest = SET_DEST (body);
11871 rtx target;
11872 unsigned int regno, tregno, endregno, endtregno;
11874 if (GET_CODE (dest) == ZERO_EXTRACT)
11875 target = XEXP (dest, 0);
11876 else if (GET_CODE (dest) == STRICT_LOW_PART)
11877 target = SUBREG_REG (XEXP (dest, 0));
11878 else
11879 return 0;
11881 if (GET_CODE (target) == SUBREG)
11882 target = SUBREG_REG (target);
11884 if (!REG_P (target))
11885 return 0;
11887 tregno = REGNO (target), regno = REGNO (x);
11888 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11889 return target == x;
11891 endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11892 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11894 return endregno > tregno && regno < endtregno;
11897 else if (GET_CODE (body) == PARALLEL)
11898 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11899 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11900 return 1;
11902 return 0;
11905 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11906 as appropriate. I3 and I2 are the insns resulting from the combination
11907 insns including FROM (I2 may be zero).
11909 Each note in the list is either ignored or placed on some insns, depending
11910 on the type of note. */
11912 static void
11913 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
11915 rtx note, next_note;
11916 rtx tem;
11918 for (note = notes; note; note = next_note)
11920 rtx place = 0, place2 = 0;
11922 /* If this NOTE references a pseudo register, ensure it references
11923 the latest copy of that register. */
11924 if (XEXP (note, 0) && REG_P (XEXP (note, 0))
11925 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11926 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11928 next_note = XEXP (note, 1);
11929 switch (REG_NOTE_KIND (note))
11931 case REG_BR_PROB:
11932 case REG_BR_PRED:
11933 /* Doesn't matter much where we put this, as long as it's somewhere.
11934 It is preferable to keep these notes on branches, which is most
11935 likely to be i3. */
11936 place = i3;
11937 break;
11939 case REG_VALUE_PROFILE:
11940 /* Just get rid of this note, as it is unused later anyway. */
11941 break;
11943 case REG_NON_LOCAL_GOTO:
11944 if (JUMP_P (i3))
11945 place = i3;
11946 else
11948 gcc_assert (i2 && JUMP_P (i2));
11949 place = i2;
11951 break;
11953 case REG_EH_REGION:
11954 /* These notes must remain with the call or trapping instruction. */
11955 if (CALL_P (i3))
11956 place = i3;
11957 else if (i2 && CALL_P (i2))
11958 place = i2;
11959 else
11961 gcc_assert (flag_non_call_exceptions);
11962 if (may_trap_p (i3))
11963 place = i3;
11964 else if (i2 && may_trap_p (i2))
11965 place = i2;
11966 /* ??? Otherwise assume we've combined things such that we
11967 can now prove that the instructions can't trap. Drop the
11968 note in this case. */
11970 break;
11972 case REG_NORETURN:
11973 case REG_SETJMP:
11974 /* These notes must remain with the call. It should not be
11975 possible for both I2 and I3 to be a call. */
11976 if (CALL_P (i3))
11977 place = i3;
11978 else
11980 gcc_assert (i2 && CALL_P (i2));
11981 place = i2;
11983 break;
11985 case REG_UNUSED:
11986 /* Any clobbers for i3 may still exist, and so we must process
11987 REG_UNUSED notes from that insn.
11989 Any clobbers from i2 or i1 can only exist if they were added by
11990 recog_for_combine. In that case, recog_for_combine created the
11991 necessary REG_UNUSED notes. Trying to keep any original
11992 REG_UNUSED notes from these insns can cause incorrect output
11993 if it is for the same register as the original i3 dest.
11994 In that case, we will notice that the register is set in i3,
11995 and then add a REG_UNUSED note for the destination of i3, which
11996 is wrong. However, it is possible to have REG_UNUSED notes from
11997 i2 or i1 for register which were both used and clobbered, so
11998 we keep notes from i2 or i1 if they will turn into REG_DEAD
11999 notes. */
12001 /* If this register is set or clobbered in I3, put the note there
12002 unless there is one already. */
12003 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12005 if (from_insn != i3)
12006 break;
12008 if (! (REG_P (XEXP (note, 0))
12009 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12010 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12011 place = i3;
12013 /* Otherwise, if this register is used by I3, then this register
12014 now dies here, so we must put a REG_DEAD note here unless there
12015 is one already. */
12016 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12017 && ! (REG_P (XEXP (note, 0))
12018 ? find_regno_note (i3, REG_DEAD,
12019 REGNO (XEXP (note, 0)))
12020 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12022 PUT_REG_NOTE_KIND (note, REG_DEAD);
12023 place = i3;
12025 break;
12027 case REG_EQUAL:
12028 case REG_EQUIV:
12029 case REG_NOALIAS:
12030 /* These notes say something about results of an insn. We can
12031 only support them if they used to be on I3 in which case they
12032 remain on I3. Otherwise they are ignored.
12034 If the note refers to an expression that is not a constant, we
12035 must also ignore the note since we cannot tell whether the
12036 equivalence is still true. It might be possible to do
12037 slightly better than this (we only have a problem if I2DEST
12038 or I1DEST is present in the expression), but it doesn't
12039 seem worth the trouble. */
12041 if (from_insn == i3
12042 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12043 place = i3;
12044 break;
12046 case REG_INC:
12047 case REG_NO_CONFLICT:
12048 /* These notes say something about how a register is used. They must
12049 be present on any use of the register in I2 or I3. */
12050 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12051 place = i3;
12053 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12055 if (place)
12056 place2 = i2;
12057 else
12058 place = i2;
12060 break;
12062 case REG_LABEL:
12063 /* This can show up in several ways -- either directly in the
12064 pattern, or hidden off in the constant pool with (or without?)
12065 a REG_EQUAL note. */
12066 /* ??? Ignore the without-reg_equal-note problem for now. */
12067 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12068 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12069 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12070 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12071 place = i3;
12073 if (i2
12074 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12075 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12076 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12077 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12079 if (place)
12080 place2 = i2;
12081 else
12082 place = i2;
12085 /* Don't attach REG_LABEL note to a JUMP_INSN. Add
12086 a JUMP_LABEL instead or decrement LABEL_NUSES. */
12087 if (place && JUMP_P (place))
12089 rtx label = JUMP_LABEL (place);
12091 if (!label)
12092 JUMP_LABEL (place) = XEXP (note, 0);
12093 else
12095 gcc_assert (label == XEXP (note, 0));
12096 if (LABEL_P (label))
12097 LABEL_NUSES (label)--;
12099 place = 0;
12101 if (place2 && JUMP_P (place2))
12103 rtx label = JUMP_LABEL (place2);
12105 if (!label)
12106 JUMP_LABEL (place2) = XEXP (note, 0);
12107 else
12109 gcc_assert (label == XEXP (note, 0));
12110 if (LABEL_P (label))
12111 LABEL_NUSES (label)--;
12113 place2 = 0;
12115 break;
12117 case REG_NONNEG:
12118 /* This note says something about the value of a register prior
12119 to the execution of an insn. It is too much trouble to see
12120 if the note is still correct in all situations. It is better
12121 to simply delete it. */
12122 break;
12124 case REG_RETVAL:
12125 /* If the insn previously containing this note still exists,
12126 put it back where it was. Otherwise move it to the previous
12127 insn. Adjust the corresponding REG_LIBCALL note. */
12128 if (!NOTE_P (from_insn))
12129 place = from_insn;
12130 else
12132 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12133 place = prev_real_insn (from_insn);
12134 if (tem && place)
12135 XEXP (tem, 0) = place;
12136 /* If we're deleting the last remaining instruction of a
12137 libcall sequence, don't add the notes. */
12138 else if (XEXP (note, 0) == from_insn)
12139 tem = place = 0;
12140 /* Don't add the dangling REG_RETVAL note. */
12141 else if (! tem)
12142 place = 0;
12144 break;
12146 case REG_LIBCALL:
12147 /* This is handled similarly to REG_RETVAL. */
12148 if (!NOTE_P (from_insn))
12149 place = from_insn;
12150 else
12152 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12153 place = next_real_insn (from_insn);
12154 if (tem && place)
12155 XEXP (tem, 0) = place;
12156 /* If we're deleting the last remaining instruction of a
12157 libcall sequence, don't add the notes. */
12158 else if (XEXP (note, 0) == from_insn)
12159 tem = place = 0;
12160 /* Don't add the dangling REG_LIBCALL note. */
12161 else if (! tem)
12162 place = 0;
12164 break;
12166 case REG_DEAD:
12167 /* If the register is used as an input in I3, it dies there.
12168 Similarly for I2, if it is nonzero and adjacent to I3.
12170 If the register is not used as an input in either I3 or I2
12171 and it is not one of the registers we were supposed to eliminate,
12172 there are two possibilities. We might have a non-adjacent I2
12173 or we might have somehow eliminated an additional register
12174 from a computation. For example, we might have had A & B where
12175 we discover that B will always be zero. In this case we will
12176 eliminate the reference to A.
12178 In both cases, we must search to see if we can find a previous
12179 use of A and put the death note there. */
12181 if (from_insn
12182 && CALL_P (from_insn)
12183 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12184 place = from_insn;
12185 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12186 place = i3;
12187 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12188 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12189 place = i2;
12191 if (place == 0)
12193 basic_block bb = this_basic_block;
12195 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12197 if (! INSN_P (tem))
12199 if (tem == BB_HEAD (bb))
12200 break;
12201 continue;
12204 /* If the register is being set at TEM, see if that is all
12205 TEM is doing. If so, delete TEM. Otherwise, make this
12206 into a REG_UNUSED note instead. Don't delete sets to
12207 global register vars. */
12208 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12209 || !global_regs[REGNO (XEXP (note, 0))])
12210 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12212 rtx set = single_set (tem);
12213 rtx inner_dest = 0;
12214 #ifdef HAVE_cc0
12215 rtx cc0_setter = NULL_RTX;
12216 #endif
12218 if (set != 0)
12219 for (inner_dest = SET_DEST (set);
12220 (GET_CODE (inner_dest) == STRICT_LOW_PART
12221 || GET_CODE (inner_dest) == SUBREG
12222 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12223 inner_dest = XEXP (inner_dest, 0))
12226 /* Verify that it was the set, and not a clobber that
12227 modified the register.
12229 CC0 targets must be careful to maintain setter/user
12230 pairs. If we cannot delete the setter due to side
12231 effects, mark the user with an UNUSED note instead
12232 of deleting it. */
12234 if (set != 0 && ! side_effects_p (SET_SRC (set))
12235 && rtx_equal_p (XEXP (note, 0), inner_dest)
12236 #ifdef HAVE_cc0
12237 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12238 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12239 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12240 #endif
12243 /* Move the notes and links of TEM elsewhere.
12244 This might delete other dead insns recursively.
12245 First set the pattern to something that won't use
12246 any register. */
12247 rtx old_notes = REG_NOTES (tem);
12249 PATTERN (tem) = pc_rtx;
12250 REG_NOTES (tem) = NULL;
12252 distribute_notes (old_notes, tem, tem, NULL_RTX);
12253 distribute_links (LOG_LINKS (tem));
12255 SET_INSN_DELETED (tem);
12257 #ifdef HAVE_cc0
12258 /* Delete the setter too. */
12259 if (cc0_setter)
12261 PATTERN (cc0_setter) = pc_rtx;
12262 old_notes = REG_NOTES (cc0_setter);
12263 REG_NOTES (cc0_setter) = NULL;
12265 distribute_notes (old_notes, cc0_setter,
12266 cc0_setter, NULL_RTX);
12267 distribute_links (LOG_LINKS (cc0_setter));
12269 SET_INSN_DELETED (cc0_setter);
12271 #endif
12273 else
12275 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12277 /* If there isn't already a REG_UNUSED note, put one
12278 here. Do not place a REG_DEAD note, even if
12279 the register is also used here; that would not
12280 match the algorithm used in lifetime analysis
12281 and can cause the consistency check in the
12282 scheduler to fail. */
12283 if (! find_regno_note (tem, REG_UNUSED,
12284 REGNO (XEXP (note, 0))))
12285 place = tem;
12286 break;
12289 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12290 || (CALL_P (tem)
12291 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12293 place = tem;
12295 /* If we are doing a 3->2 combination, and we have a
12296 register which formerly died in i3 and was not used
12297 by i2, which now no longer dies in i3 and is used in
12298 i2 but does not die in i2, and place is between i2
12299 and i3, then we may need to move a link from place to
12300 i2. */
12301 if (i2 && INSN_UID (place) <= max_uid_cuid
12302 && INSN_CUID (place) > INSN_CUID (i2)
12303 && from_insn
12304 && INSN_CUID (from_insn) > INSN_CUID (i2)
12305 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12307 rtx links = LOG_LINKS (place);
12308 LOG_LINKS (place) = 0;
12309 distribute_links (links);
12311 break;
12314 if (tem == BB_HEAD (bb))
12315 break;
12318 /* We haven't found an insn for the death note and it
12319 is still a REG_DEAD note, but we have hit the beginning
12320 of the block. If the existing life info says the reg
12321 was dead, there's nothing left to do. Otherwise, we'll
12322 need to do a global life update after combine. */
12323 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12324 && REGNO_REG_SET_P (bb->il.rtl->global_live_at_start,
12325 REGNO (XEXP (note, 0))))
12326 SET_BIT (refresh_blocks, this_basic_block->index);
12329 /* If the register is set or already dead at PLACE, we needn't do
12330 anything with this note if it is still a REG_DEAD note.
12331 We check here if it is set at all, not if is it totally replaced,
12332 which is what `dead_or_set_p' checks, so also check for it being
12333 set partially. */
12335 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12337 unsigned int regno = REGNO (XEXP (note, 0));
12339 /* Similarly, if the instruction on which we want to place
12340 the note is a noop, we'll need do a global live update
12341 after we remove them in delete_noop_moves. */
12342 if (noop_move_p (place))
12343 SET_BIT (refresh_blocks, this_basic_block->index);
12345 if (dead_or_set_p (place, XEXP (note, 0))
12346 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12348 /* Unless the register previously died in PLACE, clear
12349 last_death. [I no longer understand why this is
12350 being done.] */
12351 if (reg_stat[regno].last_death != place)
12352 reg_stat[regno].last_death = 0;
12353 place = 0;
12355 else
12356 reg_stat[regno].last_death = place;
12358 /* If this is a death note for a hard reg that is occupying
12359 multiple registers, ensure that we are still using all
12360 parts of the object. If we find a piece of the object
12361 that is unused, we must arrange for an appropriate REG_DEAD
12362 note to be added for it. However, we can't just emit a USE
12363 and tag the note to it, since the register might actually
12364 be dead; so we recourse, and the recursive call then finds
12365 the previous insn that used this register. */
12367 if (place && regno < FIRST_PSEUDO_REGISTER
12368 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12370 unsigned int endregno
12371 = regno + hard_regno_nregs[regno]
12372 [GET_MODE (XEXP (note, 0))];
12373 int all_used = 1;
12374 unsigned int i;
12376 for (i = regno; i < endregno; i++)
12377 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12378 && ! find_regno_fusage (place, USE, i))
12379 || dead_or_set_regno_p (place, i))
12380 all_used = 0;
12382 if (! all_used)
12384 /* Put only REG_DEAD notes for pieces that are
12385 not already dead or set. */
12387 for (i = regno; i < endregno;
12388 i += hard_regno_nregs[i][reg_raw_mode[i]])
12390 rtx piece = regno_reg_rtx[i];
12391 basic_block bb = this_basic_block;
12393 if (! dead_or_set_p (place, piece)
12394 && ! reg_bitfield_target_p (piece,
12395 PATTERN (place)))
12397 rtx new_note
12398 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12400 distribute_notes (new_note, place, place,
12401 NULL_RTX);
12403 else if (! refers_to_regno_p (i, i + 1,
12404 PATTERN (place), 0)
12405 && ! find_regno_fusage (place, USE, i))
12406 for (tem = PREV_INSN (place); ;
12407 tem = PREV_INSN (tem))
12409 if (! INSN_P (tem))
12411 if (tem == BB_HEAD (bb))
12413 SET_BIT (refresh_blocks,
12414 this_basic_block->index);
12415 break;
12417 continue;
12419 if (dead_or_set_p (tem, piece)
12420 || reg_bitfield_target_p (piece,
12421 PATTERN (tem)))
12423 REG_NOTES (tem)
12424 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12425 REG_NOTES (tem));
12426 break;
12432 place = 0;
12436 break;
12438 default:
12439 /* Any other notes should not be present at this point in the
12440 compilation. */
12441 gcc_unreachable ();
12444 if (place)
12446 XEXP (note, 1) = REG_NOTES (place);
12447 REG_NOTES (place) = note;
12449 else if ((REG_NOTE_KIND (note) == REG_DEAD
12450 || REG_NOTE_KIND (note) == REG_UNUSED)
12451 && REG_P (XEXP (note, 0)))
12452 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12454 if (place2)
12456 if ((REG_NOTE_KIND (note) == REG_DEAD
12457 || REG_NOTE_KIND (note) == REG_UNUSED)
12458 && REG_P (XEXP (note, 0)))
12459 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12461 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12462 REG_NOTE_KIND (note),
12463 XEXP (note, 0),
12464 REG_NOTES (place2));
12469 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12470 I3, I2, and I1 to new locations. This is also called to add a link
12471 pointing at I3 when I3's destination is changed. */
12473 static void
12474 distribute_links (rtx links)
12476 rtx link, next_link;
12478 for (link = links; link; link = next_link)
12480 rtx place = 0;
12481 rtx insn;
12482 rtx set, reg;
12484 next_link = XEXP (link, 1);
12486 /* If the insn that this link points to is a NOTE or isn't a single
12487 set, ignore it. In the latter case, it isn't clear what we
12488 can do other than ignore the link, since we can't tell which
12489 register it was for. Such links wouldn't be used by combine
12490 anyway.
12492 It is not possible for the destination of the target of the link to
12493 have been changed by combine. The only potential of this is if we
12494 replace I3, I2, and I1 by I3 and I2. But in that case the
12495 destination of I2 also remains unchanged. */
12497 if (NOTE_P (XEXP (link, 0))
12498 || (set = single_set (XEXP (link, 0))) == 0)
12499 continue;
12501 reg = SET_DEST (set);
12502 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12503 || GET_CODE (reg) == STRICT_LOW_PART)
12504 reg = XEXP (reg, 0);
12506 /* A LOG_LINK is defined as being placed on the first insn that uses
12507 a register and points to the insn that sets the register. Start
12508 searching at the next insn after the target of the link and stop
12509 when we reach a set of the register or the end of the basic block.
12511 Note that this correctly handles the link that used to point from
12512 I3 to I2. Also note that not much searching is typically done here
12513 since most links don't point very far away. */
12515 for (insn = NEXT_INSN (XEXP (link, 0));
12516 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12517 || BB_HEAD (this_basic_block->next_bb) != insn));
12518 insn = NEXT_INSN (insn))
12519 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12521 if (reg_referenced_p (reg, PATTERN (insn)))
12522 place = insn;
12523 break;
12525 else if (CALL_P (insn)
12526 && find_reg_fusage (insn, USE, reg))
12528 place = insn;
12529 break;
12531 else if (INSN_P (insn) && reg_set_p (reg, insn))
12532 break;
12534 /* If we found a place to put the link, place it there unless there
12535 is already a link to the same insn as LINK at that point. */
12537 if (place)
12539 rtx link2;
12541 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12542 if (XEXP (link2, 0) == XEXP (link, 0))
12543 break;
12545 if (link2 == 0)
12547 XEXP (link, 1) = LOG_LINKS (place);
12548 LOG_LINKS (place) = link;
12550 /* Set added_links_insn to the earliest insn we added a
12551 link to. */
12552 if (added_links_insn == 0
12553 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12554 added_links_insn = place;
12560 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12561 Check whether the expression pointer to by LOC is a register or
12562 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12563 Otherwise return zero. */
12565 static int
12566 unmentioned_reg_p_1 (rtx *loc, void *expr)
12568 rtx x = *loc;
12570 if (x != NULL_RTX
12571 && (REG_P (x) || MEM_P (x))
12572 && ! reg_mentioned_p (x, (rtx) expr))
12573 return 1;
12574 return 0;
12577 /* Check for any register or memory mentioned in EQUIV that is not
12578 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12579 of EXPR where some registers may have been replaced by constants. */
12581 static bool
12582 unmentioned_reg_p (rtx equiv, rtx expr)
12584 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12587 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12589 static int
12590 insn_cuid (rtx insn)
12592 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12593 && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE)
12594 insn = NEXT_INSN (insn);
12596 gcc_assert (INSN_UID (insn) <= max_uid_cuid);
12598 return INSN_CUID (insn);
12601 void
12602 dump_combine_stats (FILE *file)
12604 fprintf
12605 (file,
12606 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12607 combine_attempts, combine_merges, combine_extras, combine_successes);
12610 void
12611 dump_combine_total_stats (FILE *file)
12613 fprintf
12614 (file,
12615 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12616 total_attempts, total_merges, total_extras, total_successes);
12620 static bool
12621 gate_handle_combine (void)
12623 return (optimize > 0);
12626 /* Try combining insns through substitution. */
12627 static void
12628 rest_of_handle_combine (void)
12630 int rebuild_jump_labels_after_combine
12631 = combine_instructions (get_insns (), max_reg_num ());
12633 /* Combining insns may have turned an indirect jump into a
12634 direct jump. Rebuild the JUMP_LABEL fields of jumping
12635 instructions. */
12636 if (rebuild_jump_labels_after_combine)
12638 timevar_push (TV_JUMP);
12639 rebuild_jump_labels (get_insns ());
12640 timevar_pop (TV_JUMP);
12642 delete_dead_jumptables ();
12643 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
12647 struct tree_opt_pass pass_combine =
12649 "combine", /* name */
12650 gate_handle_combine, /* gate */
12651 rest_of_handle_combine, /* execute */
12652 NULL, /* sub */
12653 NULL, /* next */
12654 0, /* static_pass_number */
12655 TV_COMBINE, /* tv_id */
12656 0, /* properties_required */
12657 0, /* properties_provided */
12658 0, /* properties_destroyed */
12659 0, /* todo_flags_start */
12660 TODO_dump_func |
12661 TODO_ggc_collect, /* todo_flags_finish */
12662 'c' /* letter */