2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / combine.c
blobd9e0b4fcda5398b6f17b83a2f96b8c6ca66375e8
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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"
100 /* Number of attempts to combine instructions in this function. */
102 static int combine_attempts;
104 /* Number of attempts that got as far as substitution in this function. */
106 static int combine_merges;
108 /* Number of instructions combined with added SETs in this function. */
110 static int combine_extras;
112 /* Number of instructions combined in this function. */
114 static int combine_successes;
116 /* Totals over entire compilation. */
118 static int total_attempts, total_merges, total_extras, total_successes;
121 /* Vector mapping INSN_UIDs to cuids.
122 The cuids are like uids but increase monotonically always.
123 Combine always uses cuids so that it can compare them.
124 But actually renumbering the uids, which we used to do,
125 proves to be a bad idea because it makes it hard to compare
126 the dumps produced by earlier passes with those from later passes. */
128 static int *uid_cuid;
129 static int max_uid_cuid;
131 /* Get the cuid of an insn. */
133 #define INSN_CUID(INSN) \
134 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
136 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
137 BITS_PER_WORD would invoke undefined behavior. Work around it. */
139 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
140 (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
142 /* Maximum register number, which is the size of the tables below. */
144 static unsigned int combine_max_regno;
146 struct reg_stat {
147 /* Record last point of death of (hard or pseudo) register n. */
148 rtx last_death;
150 /* Record last point of modification of (hard or pseudo) register n. */
151 rtx last_set;
153 /* The next group of fields allows the recording of the last value assigned
154 to (hard or pseudo) register n. We use this information to see if an
155 operation being processed is redundant given a prior operation performed
156 on the register. For example, an `and' with a constant is redundant if
157 all the zero bits are already known to be turned off.
159 We use an approach similar to that used by cse, but change it in the
160 following ways:
162 (1) We do not want to reinitialize at each label.
163 (2) It is useful, but not critical, to know the actual value assigned
164 to a register. Often just its form is helpful.
166 Therefore, we maintain the following fields:
168 last_set_value the last value assigned
169 last_set_label records the value of label_tick when the
170 register was assigned
171 last_set_table_tick records the value of label_tick when a
172 value using the register is assigned
173 last_set_invalid set to nonzero when it is not valid
174 to use the value of this register in some
175 register's value
177 To understand the usage of these tables, it is important to understand
178 the distinction between the value in last_set_value being valid and
179 the register being validly contained in some other expression in the
180 table.
182 (The next two parameters are out of date).
184 reg_stat[i].last_set_value is valid if it is nonzero, and either
185 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
187 Register I may validly appear in any expression returned for the value
188 of another register if reg_n_sets[i] is 1. It may also appear in the
189 value for register J if reg_stat[j].last_set_invalid is zero, or
190 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
192 If an expression is found in the table containing a register which may
193 not validly appear in an expression, the register is replaced by
194 something that won't match, (clobber (const_int 0)). */
196 /* Record last value assigned to (hard or pseudo) register n. */
198 rtx last_set_value;
200 /* Record the value of label_tick when an expression involving register n
201 is placed in last_set_value. */
203 int last_set_table_tick;
205 /* Record the value of label_tick when the value for register n is placed in
206 last_set_value. */
208 int last_set_label;
210 /* These fields are maintained in parallel with last_set_value and are
211 used to store the mode in which the register was last set, the bits
212 that were known to be zero when it was last set, and the number of
213 sign bits copies it was known to have when it was last set. */
215 unsigned HOST_WIDE_INT last_set_nonzero_bits;
216 char last_set_sign_bit_copies;
217 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
219 /* Set nonzero if references to register n in expressions should not be
220 used. last_set_invalid is set nonzero when this register is being
221 assigned to and last_set_table_tick == label_tick. */
223 char last_set_invalid;
225 /* Some registers that are set more than once and used in more than one
226 basic block are nevertheless always set in similar ways. For example,
227 a QImode register may be loaded from memory in two places on a machine
228 where byte loads zero extend.
230 We record in the following fields if a register has some leading bits
231 that are always equal to the sign bit, and what we know about the
232 nonzero bits of a register, specifically which bits are known to be
233 zero.
235 If an entry is zero, it means that we don't know anything special. */
237 unsigned char sign_bit_copies;
239 unsigned HOST_WIDE_INT nonzero_bits;
242 static struct reg_stat *reg_stat;
244 /* Record the cuid of the last insn that invalidated memory
245 (anything that writes memory, and subroutine calls, but not pushes). */
247 static int mem_last_set;
249 /* Record the cuid of the last CALL_INSN
250 so we can tell whether a potential combination crosses any calls. */
252 static int last_call_cuid;
254 /* When `subst' is called, this is the insn that is being modified
255 (by combining in a previous insn). The PATTERN of this insn
256 is still the old pattern partially modified and it should not be
257 looked at, but this may be used to examine the successors of the insn
258 to judge whether a simplification is valid. */
260 static rtx subst_insn;
262 /* This is the lowest CUID that `subst' is currently dealing with.
263 get_last_value will not return a value if the register was set at or
264 after this CUID. If not for this mechanism, we could get confused if
265 I2 or I1 in try_combine were an insn that used the old value of a register
266 to obtain a new value. In that case, we might erroneously get the
267 new value of the register when we wanted the old one. */
269 static int subst_low_cuid;
271 /* This contains any hard registers that are used in newpat; reg_dead_at_p
272 must consider all these registers to be always live. */
274 static HARD_REG_SET newpat_used_regs;
276 /* This is an insn to which a LOG_LINKS entry has been added. If this
277 insn is the earlier than I2 or I3, combine should rescan starting at
278 that location. */
280 static rtx added_links_insn;
282 /* Basic block in which we are performing combines. */
283 static basic_block this_basic_block;
285 /* A bitmap indicating which blocks had registers go dead at entry.
286 After combine, we'll need to re-do global life analysis with
287 those blocks as starting points. */
288 static sbitmap refresh_blocks;
290 /* The following array records the insn_rtx_cost for every insn
291 in the instruction stream. */
293 static int *uid_insn_cost;
295 /* Length of the currently allocated uid_insn_cost array. */
297 static int last_insn_cost;
299 /* Incremented for each label. */
301 static int label_tick;
303 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
304 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
306 static enum machine_mode nonzero_bits_mode;
308 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
309 be safely used. It is zero while computing them and after combine has
310 completed. This former test prevents propagating values based on
311 previously set values, which can be incorrect if a variable is modified
312 in a loop. */
314 static int nonzero_sign_valid;
317 /* Record one modification to rtl structure
318 to be undone by storing old_contents into *where.
319 is_int is 1 if the contents are an int. */
321 struct undo
323 struct undo *next;
324 int is_int;
325 union {rtx r; int i;} old_contents;
326 union {rtx *r; int *i;} where;
329 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
330 num_undo says how many are currently recorded.
332 other_insn is nonzero if we have modified some other insn in the process
333 of working on subst_insn. It must be verified too. */
335 struct undobuf
337 struct undo *undos;
338 struct undo *frees;
339 rtx other_insn;
342 static struct undobuf undobuf;
344 /* Number of times the pseudo being substituted for
345 was found and replaced. */
347 static int n_occurrences;
349 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
350 enum machine_mode,
351 unsigned HOST_WIDE_INT,
352 unsigned HOST_WIDE_INT *);
353 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
354 enum machine_mode,
355 unsigned int, unsigned int *);
356 static void do_SUBST (rtx *, rtx);
357 static void do_SUBST_INT (int *, int);
358 static void init_reg_last (void);
359 static void setup_incoming_promotions (void);
360 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
361 static int cant_combine_insn_p (rtx);
362 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
363 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
364 static int contains_muldiv (rtx);
365 static rtx try_combine (rtx, rtx, rtx, int *);
366 static void undo_all (void);
367 static void undo_commit (void);
368 static rtx *find_split_point (rtx *, rtx);
369 static rtx subst (rtx, rtx, rtx, int, int);
370 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
371 static rtx simplify_if_then_else (rtx);
372 static rtx simplify_set (rtx);
373 static rtx simplify_logical (rtx);
374 static rtx expand_compound_operation (rtx);
375 static rtx expand_field_assignment (rtx);
376 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
377 rtx, unsigned HOST_WIDE_INT, int, int, int);
378 static rtx extract_left_shift (rtx, int);
379 static rtx make_compound_operation (rtx, enum rtx_code);
380 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
381 unsigned HOST_WIDE_INT *);
382 static rtx force_to_mode (rtx, enum machine_mode,
383 unsigned HOST_WIDE_INT, rtx, int);
384 static rtx if_then_else_cond (rtx, rtx *, rtx *);
385 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
386 static int rtx_equal_for_field_assignment_p (rtx, rtx);
387 static rtx make_field_assignment (rtx);
388 static rtx apply_distributive_law (rtx);
389 static rtx distribute_and_simplify_rtx (rtx, int);
390 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
391 unsigned HOST_WIDE_INT);
392 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
393 HOST_WIDE_INT, enum machine_mode, int *);
394 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
395 int);
396 static int recog_for_combine (rtx *, rtx, rtx *);
397 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
398 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
399 static void update_table_tick (rtx);
400 static void record_value_for_reg (rtx, rtx, rtx);
401 static void check_promoted_subreg (rtx, rtx);
402 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
403 static void record_dead_and_set_regs (rtx);
404 static int get_last_value_validate (rtx *, rtx, int, int);
405 static rtx get_last_value (rtx);
406 static int use_crosses_set_p (rtx, int);
407 static void reg_dead_at_p_1 (rtx, rtx, void *);
408 static int reg_dead_at_p (rtx, rtx);
409 static void move_deaths (rtx, rtx, int, rtx, rtx *);
410 static int reg_bitfield_target_p (rtx, rtx);
411 static void distribute_notes (rtx, rtx, rtx, rtx);
412 static void distribute_links (rtx);
413 static void mark_used_regs_combine (rtx);
414 static int insn_cuid (rtx);
415 static void record_promoted_value (rtx, rtx);
416 static int unmentioned_reg_p_1 (rtx *, void *);
417 static bool unmentioned_reg_p (rtx, rtx);
420 /* It is not safe to use ordinary gen_lowpart in combine.
421 See comments in gen_lowpart_for_combine. */
422 #undef RTL_HOOKS_GEN_LOWPART
423 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
425 /* Our implementation of gen_lowpart never emits a new pseudo. */
426 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
427 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
429 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
430 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
432 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
433 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
435 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
438 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
439 insn. The substitution can be undone by undo_all. If INTO is already
440 set to NEWVAL, do not record this change. Because computing NEWVAL might
441 also call SUBST, we have to compute it before we put anything into
442 the undo table. */
444 static void
445 do_SUBST (rtx *into, rtx newval)
447 struct undo *buf;
448 rtx oldval = *into;
450 if (oldval == newval)
451 return;
453 /* We'd like to catch as many invalid transformations here as
454 possible. Unfortunately, there are way too many mode changes
455 that are perfectly valid, so we'd waste too much effort for
456 little gain doing the checks here. Focus on catching invalid
457 transformations involving integer constants. */
458 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
459 && GET_CODE (newval) == CONST_INT)
461 /* Sanity check that we're replacing oldval with a CONST_INT
462 that is a valid sign-extension for the original mode. */
463 gcc_assert (INTVAL (newval)
464 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
466 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
467 CONST_INT is not valid, because after the replacement, the
468 original mode would be gone. Unfortunately, we can't tell
469 when do_SUBST is called to replace the operand thereof, so we
470 perform this test on oldval instead, checking whether an
471 invalid replacement took place before we got here. */
472 gcc_assert (!(GET_CODE (oldval) == SUBREG
473 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
474 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
475 && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
478 if (undobuf.frees)
479 buf = undobuf.frees, undobuf.frees = buf->next;
480 else
481 buf = xmalloc (sizeof (struct undo));
483 buf->is_int = 0;
484 buf->where.r = into;
485 buf->old_contents.r = oldval;
486 *into = newval;
488 buf->next = undobuf.undos, undobuf.undos = buf;
491 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
493 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
494 for the value of a HOST_WIDE_INT value (including CONST_INT) is
495 not safe. */
497 static void
498 do_SUBST_INT (int *into, int newval)
500 struct undo *buf;
501 int oldval = *into;
503 if (oldval == newval)
504 return;
506 if (undobuf.frees)
507 buf = undobuf.frees, undobuf.frees = buf->next;
508 else
509 buf = xmalloc (sizeof (struct undo));
511 buf->is_int = 1;
512 buf->where.i = into;
513 buf->old_contents.i = oldval;
514 *into = newval;
516 buf->next = undobuf.undos, undobuf.undos = buf;
519 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
521 /* Subroutine of try_combine. Determine whether the combine replacement
522 patterns NEWPAT and NEWI2PAT are cheaper according to insn_rtx_cost
523 that the original instruction sequence I1, I2 and I3. Note that I1
524 and/or NEWI2PAT may be NULL_RTX. This function returns false, if the
525 costs of all instructions can be estimated, and the replacements are
526 more expensive than the original sequence. */
528 static bool
529 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat)
531 int i1_cost, i2_cost, i3_cost;
532 int new_i2_cost, new_i3_cost;
533 int old_cost, new_cost;
535 /* Lookup the original insn_rtx_costs. */
536 i2_cost = INSN_UID (i2) <= last_insn_cost
537 ? uid_insn_cost[INSN_UID (i2)] : 0;
538 i3_cost = INSN_UID (i3) <= last_insn_cost
539 ? uid_insn_cost[INSN_UID (i3)] : 0;
541 if (i1)
543 i1_cost = INSN_UID (i1) <= last_insn_cost
544 ? uid_insn_cost[INSN_UID (i1)] : 0;
545 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
546 ? i1_cost + i2_cost + i3_cost : 0;
548 else
550 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
551 i1_cost = 0;
554 /* Calculate the replacement insn_rtx_costs. */
555 new_i3_cost = insn_rtx_cost (newpat);
556 if (newi2pat)
558 new_i2_cost = insn_rtx_cost (newi2pat);
559 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
560 ? new_i2_cost + new_i3_cost : 0;
562 else
564 new_cost = new_i3_cost;
565 new_i2_cost = 0;
568 if (undobuf.other_insn)
570 int old_other_cost, new_other_cost;
572 old_other_cost = (INSN_UID (undobuf.other_insn) <= last_insn_cost
573 ? uid_insn_cost[INSN_UID (undobuf.other_insn)] : 0);
574 new_other_cost = insn_rtx_cost (PATTERN (undobuf.other_insn));
575 if (old_other_cost > 0 && new_other_cost > 0)
577 old_cost += old_other_cost;
578 new_cost += new_other_cost;
580 else
581 old_cost = 0;
584 /* Disallow this recombination if both new_cost and old_cost are
585 greater than zero, and new_cost is greater than old cost. */
586 if (old_cost > 0
587 && new_cost > old_cost)
589 if (dump_file)
591 if (i1)
593 fprintf (dump_file,
594 "rejecting combination of insns %d, %d and %d\n",
595 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
596 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
597 i1_cost, i2_cost, i3_cost, old_cost);
599 else
601 fprintf (dump_file,
602 "rejecting combination of insns %d and %d\n",
603 INSN_UID (i2), INSN_UID (i3));
604 fprintf (dump_file, "original costs %d + %d = %d\n",
605 i2_cost, i3_cost, old_cost);
608 if (newi2pat)
610 fprintf (dump_file, "replacement costs %d + %d = %d\n",
611 new_i2_cost, new_i3_cost, new_cost);
613 else
614 fprintf (dump_file, "replacement cost %d\n", new_cost);
617 return false;
620 /* Update the uid_insn_cost array with the replacement costs. */
621 uid_insn_cost[INSN_UID (i2)] = new_i2_cost;
622 uid_insn_cost[INSN_UID (i3)] = new_i3_cost;
623 if (i1)
624 uid_insn_cost[INSN_UID (i1)] = 0;
626 return true;
629 /* Main entry point for combiner. F is the first insn of the function.
630 NREGS is the first unused pseudo-reg number.
632 Return nonzero if the combiner has turned an indirect jump
633 instruction into a direct jump. */
635 combine_instructions (rtx f, unsigned int nregs)
637 rtx insn, next;
638 #ifdef HAVE_cc0
639 rtx prev;
640 #endif
641 int i;
642 rtx links, nextlinks;
644 int new_direct_jump_p = 0;
646 combine_attempts = 0;
647 combine_merges = 0;
648 combine_extras = 0;
649 combine_successes = 0;
651 combine_max_regno = nregs;
653 rtl_hooks = combine_rtl_hooks;
655 reg_stat = xcalloc (nregs, sizeof (struct reg_stat));
657 init_recog_no_volatile ();
659 /* Compute maximum uid value so uid_cuid can be allocated. */
661 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
662 if (INSN_UID (insn) > i)
663 i = INSN_UID (insn);
665 uid_cuid = xmalloc ((i + 1) * sizeof (int));
666 max_uid_cuid = i;
668 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
670 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
671 problems when, for example, we have j <<= 1 in a loop. */
673 nonzero_sign_valid = 0;
675 /* Compute the mapping from uids to cuids.
676 Cuids are numbers assigned to insns, like uids,
677 except that cuids increase monotonically through the code.
679 Scan all SETs and see if we can deduce anything about what
680 bits are known to be zero for some registers and how many copies
681 of the sign bit are known to exist for those registers.
683 Also set any known values so that we can use it while searching
684 for what bits are known to be set. */
686 label_tick = 1;
688 setup_incoming_promotions ();
690 refresh_blocks = sbitmap_alloc (last_basic_block);
691 sbitmap_zero (refresh_blocks);
693 /* Allocate array of current insn_rtx_costs. */
694 uid_insn_cost = xcalloc (max_uid_cuid + 1, sizeof (int));
695 last_insn_cost = max_uid_cuid;
697 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
699 uid_cuid[INSN_UID (insn)] = ++i;
700 subst_low_cuid = i;
701 subst_insn = insn;
703 if (INSN_P (insn))
705 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
706 NULL);
707 record_dead_and_set_regs (insn);
709 #ifdef AUTO_INC_DEC
710 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
711 if (REG_NOTE_KIND (links) == REG_INC)
712 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
713 NULL);
714 #endif
716 /* Record the current insn_rtx_cost of this instruction. */
717 if (NONJUMP_INSN_P (insn))
718 uid_insn_cost[INSN_UID (insn)] = insn_rtx_cost (PATTERN (insn));
719 if (dump_file)
720 fprintf(dump_file, "insn_cost %d: %d\n",
721 INSN_UID (insn), uid_insn_cost[INSN_UID (insn)]);
724 if (LABEL_P (insn))
725 label_tick++;
728 nonzero_sign_valid = 1;
730 /* Now scan all the insns in forward order. */
732 label_tick = 1;
733 last_call_cuid = 0;
734 mem_last_set = 0;
735 init_reg_last ();
736 setup_incoming_promotions ();
738 FOR_EACH_BB (this_basic_block)
740 for (insn = BB_HEAD (this_basic_block);
741 insn != NEXT_INSN (BB_END (this_basic_block));
742 insn = next ? next : NEXT_INSN (insn))
744 next = 0;
746 if (LABEL_P (insn))
747 label_tick++;
749 else if (INSN_P (insn))
751 /* See if we know about function return values before this
752 insn based upon SUBREG flags. */
753 check_promoted_subreg (insn, PATTERN (insn));
755 /* Try this insn with each insn it links back to. */
757 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
758 if ((next = try_combine (insn, XEXP (links, 0),
759 NULL_RTX, &new_direct_jump_p)) != 0)
760 goto retry;
762 /* Try each sequence of three linked insns ending with this one. */
764 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
766 rtx link = XEXP (links, 0);
768 /* If the linked insn has been replaced by a note, then there
769 is no point in pursuing this chain any further. */
770 if (NOTE_P (link))
771 continue;
773 for (nextlinks = LOG_LINKS (link);
774 nextlinks;
775 nextlinks = XEXP (nextlinks, 1))
776 if ((next = try_combine (insn, link,
777 XEXP (nextlinks, 0),
778 &new_direct_jump_p)) != 0)
779 goto retry;
782 #ifdef HAVE_cc0
783 /* Try to combine a jump insn that uses CC0
784 with a preceding insn that sets CC0, and maybe with its
785 logical predecessor as well.
786 This is how we make decrement-and-branch insns.
787 We need this special code because data flow connections
788 via CC0 do not get entered in LOG_LINKS. */
790 if (JUMP_P (insn)
791 && (prev = prev_nonnote_insn (insn)) != 0
792 && NONJUMP_INSN_P (prev)
793 && sets_cc0_p (PATTERN (prev)))
795 if ((next = try_combine (insn, prev,
796 NULL_RTX, &new_direct_jump_p)) != 0)
797 goto retry;
799 for (nextlinks = LOG_LINKS (prev); nextlinks;
800 nextlinks = XEXP (nextlinks, 1))
801 if ((next = try_combine (insn, prev,
802 XEXP (nextlinks, 0),
803 &new_direct_jump_p)) != 0)
804 goto retry;
807 /* Do the same for an insn that explicitly references CC0. */
808 if (NONJUMP_INSN_P (insn)
809 && (prev = prev_nonnote_insn (insn)) != 0
810 && NONJUMP_INSN_P (prev)
811 && sets_cc0_p (PATTERN (prev))
812 && GET_CODE (PATTERN (insn)) == SET
813 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
815 if ((next = try_combine (insn, prev,
816 NULL_RTX, &new_direct_jump_p)) != 0)
817 goto retry;
819 for (nextlinks = LOG_LINKS (prev); nextlinks;
820 nextlinks = XEXP (nextlinks, 1))
821 if ((next = try_combine (insn, prev,
822 XEXP (nextlinks, 0),
823 &new_direct_jump_p)) != 0)
824 goto retry;
827 /* Finally, see if any of the insns that this insn links to
828 explicitly references CC0. If so, try this insn, that insn,
829 and its predecessor if it sets CC0. */
830 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
831 if (NONJUMP_INSN_P (XEXP (links, 0))
832 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
833 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
834 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
835 && NONJUMP_INSN_P (prev)
836 && sets_cc0_p (PATTERN (prev))
837 && (next = try_combine (insn, XEXP (links, 0),
838 prev, &new_direct_jump_p)) != 0)
839 goto retry;
840 #endif
842 /* Try combining an insn with two different insns whose results it
843 uses. */
844 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
845 for (nextlinks = XEXP (links, 1); nextlinks;
846 nextlinks = XEXP (nextlinks, 1))
847 if ((next = try_combine (insn, XEXP (links, 0),
848 XEXP (nextlinks, 0),
849 &new_direct_jump_p)) != 0)
850 goto retry;
852 /* Try this insn with each REG_EQUAL note it links back to. */
853 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
855 rtx set, note;
856 rtx temp = XEXP (links, 0);
857 if ((set = single_set (temp)) != 0
858 && (note = find_reg_equal_equiv_note (temp)) != 0
859 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
860 /* Avoid using a register that may already been marked
861 dead by an earlier instruction. */
862 && ! unmentioned_reg_p (XEXP (note, 0), SET_SRC (set)))
864 /* Temporarily replace the set's source with the
865 contents of the REG_EQUAL note. The insn will
866 be deleted or recognized by try_combine. */
867 rtx orig = SET_SRC (set);
868 SET_SRC (set) = XEXP (note, 0);
869 next = try_combine (insn, temp, NULL_RTX,
870 &new_direct_jump_p);
871 if (next)
872 goto retry;
873 SET_SRC (set) = orig;
877 if (!NOTE_P (insn))
878 record_dead_and_set_regs (insn);
880 retry:
885 clear_bb_flags ();
887 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
888 BASIC_BLOCK (i)->flags |= BB_DIRTY);
889 new_direct_jump_p |= purge_all_dead_edges ();
890 delete_noop_moves ();
892 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
893 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
894 | PROP_KILL_DEAD_CODE);
896 /* Clean up. */
897 sbitmap_free (refresh_blocks);
898 free (uid_insn_cost);
899 free (reg_stat);
900 free (uid_cuid);
903 struct undo *undo, *next;
904 for (undo = undobuf.frees; undo; undo = next)
906 next = undo->next;
907 free (undo);
909 undobuf.frees = 0;
912 total_attempts += combine_attempts;
913 total_merges += combine_merges;
914 total_extras += combine_extras;
915 total_successes += combine_successes;
917 nonzero_sign_valid = 0;
918 rtl_hooks = general_rtl_hooks;
920 /* Make recognizer allow volatile MEMs again. */
921 init_recog ();
923 return new_direct_jump_p;
926 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
928 static void
929 init_reg_last (void)
931 unsigned int i;
932 for (i = 0; i < combine_max_regno; i++)
933 memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
936 /* Set up any promoted values for incoming argument registers. */
938 static void
939 setup_incoming_promotions (void)
941 unsigned int regno;
942 rtx reg;
943 enum machine_mode mode;
944 int unsignedp;
945 rtx first = get_insns ();
947 if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
949 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
950 /* Check whether this register can hold an incoming pointer
951 argument. FUNCTION_ARG_REGNO_P tests outgoing register
952 numbers, so translate if necessary due to register windows. */
953 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
954 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
956 record_value_for_reg
957 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
958 : SIGN_EXTEND),
959 GET_MODE (reg),
960 gen_rtx_CLOBBER (mode, const0_rtx)));
965 /* Called via note_stores. If X is a pseudo that is narrower than
966 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
968 If we are setting only a portion of X and we can't figure out what
969 portion, assume all bits will be used since we don't know what will
970 be happening.
972 Similarly, set how many bits of X are known to be copies of the sign bit
973 at all locations in the function. This is the smallest number implied
974 by any set of X. */
976 static void
977 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
978 void *data ATTRIBUTE_UNUSED)
980 unsigned int num;
982 if (REG_P (x)
983 && REGNO (x) >= FIRST_PSEUDO_REGISTER
984 /* If this register is undefined at the start of the file, we can't
985 say what its contents were. */
986 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
987 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
989 if (set == 0 || GET_CODE (set) == CLOBBER)
991 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
992 reg_stat[REGNO (x)].sign_bit_copies = 1;
993 return;
996 /* If this is a complex assignment, see if we can convert it into a
997 simple assignment. */
998 set = expand_field_assignment (set);
1000 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1001 set what we know about X. */
1003 if (SET_DEST (set) == x
1004 || (GET_CODE (SET_DEST (set)) == SUBREG
1005 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1006 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1007 && SUBREG_REG (SET_DEST (set)) == x))
1009 rtx src = SET_SRC (set);
1011 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1012 /* If X is narrower than a word and SRC is a non-negative
1013 constant that would appear negative in the mode of X,
1014 sign-extend it for use in reg_stat[].nonzero_bits because some
1015 machines (maybe most) will actually do the sign-extension
1016 and this is the conservative approach.
1018 ??? For 2.5, try to tighten up the MD files in this regard
1019 instead of this kludge. */
1021 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1022 && GET_CODE (src) == CONST_INT
1023 && INTVAL (src) > 0
1024 && 0 != (INTVAL (src)
1025 & ((HOST_WIDE_INT) 1
1026 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1027 src = GEN_INT (INTVAL (src)
1028 | ((HOST_WIDE_INT) (-1)
1029 << GET_MODE_BITSIZE (GET_MODE (x))));
1030 #endif
1032 /* Don't call nonzero_bits if it cannot change anything. */
1033 if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1034 reg_stat[REGNO (x)].nonzero_bits
1035 |= nonzero_bits (src, nonzero_bits_mode);
1036 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1037 if (reg_stat[REGNO (x)].sign_bit_copies == 0
1038 || reg_stat[REGNO (x)].sign_bit_copies > num)
1039 reg_stat[REGNO (x)].sign_bit_copies = num;
1041 else
1043 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1044 reg_stat[REGNO (x)].sign_bit_copies = 1;
1049 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1050 insns that were previously combined into I3 or that will be combined
1051 into the merger of INSN and I3.
1053 Return 0 if the combination is not allowed for any reason.
1055 If the combination is allowed, *PDEST will be set to the single
1056 destination of INSN and *PSRC to the single source, and this function
1057 will return 1. */
1059 static int
1060 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1061 rtx *pdest, rtx *psrc)
1063 int i;
1064 rtx set = 0, src, dest;
1065 rtx p;
1066 #ifdef AUTO_INC_DEC
1067 rtx link;
1068 #endif
1069 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1070 && next_active_insn (succ) == i3)
1071 : next_active_insn (insn) == i3);
1073 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1074 or a PARALLEL consisting of such a SET and CLOBBERs.
1076 If INSN has CLOBBER parallel parts, ignore them for our processing.
1077 By definition, these happen during the execution of the insn. When it
1078 is merged with another insn, all bets are off. If they are, in fact,
1079 needed and aren't also supplied in I3, they may be added by
1080 recog_for_combine. Otherwise, it won't match.
1082 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1083 note.
1085 Get the source and destination of INSN. If more than one, can't
1086 combine. */
1088 if (GET_CODE (PATTERN (insn)) == SET)
1089 set = PATTERN (insn);
1090 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1091 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1093 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1095 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1096 rtx note;
1098 switch (GET_CODE (elt))
1100 /* This is important to combine floating point insns
1101 for the SH4 port. */
1102 case USE:
1103 /* Combining an isolated USE doesn't make sense.
1104 We depend here on combinable_i3pat to reject them. */
1105 /* The code below this loop only verifies that the inputs of
1106 the SET in INSN do not change. We call reg_set_between_p
1107 to verify that the REG in the USE does not change between
1108 I3 and INSN.
1109 If the USE in INSN was for a pseudo register, the matching
1110 insn pattern will likely match any register; combining this
1111 with any other USE would only be safe if we knew that the
1112 used registers have identical values, or if there was
1113 something to tell them apart, e.g. different modes. For
1114 now, we forgo such complicated tests and simply disallow
1115 combining of USES of pseudo registers with any other USE. */
1116 if (REG_P (XEXP (elt, 0))
1117 && GET_CODE (PATTERN (i3)) == PARALLEL)
1119 rtx i3pat = PATTERN (i3);
1120 int i = XVECLEN (i3pat, 0) - 1;
1121 unsigned int regno = REGNO (XEXP (elt, 0));
1125 rtx i3elt = XVECEXP (i3pat, 0, i);
1127 if (GET_CODE (i3elt) == USE
1128 && REG_P (XEXP (i3elt, 0))
1129 && (REGNO (XEXP (i3elt, 0)) == regno
1130 ? reg_set_between_p (XEXP (elt, 0),
1131 PREV_INSN (insn), i3)
1132 : regno >= FIRST_PSEUDO_REGISTER))
1133 return 0;
1135 while (--i >= 0);
1137 break;
1139 /* We can ignore CLOBBERs. */
1140 case CLOBBER:
1141 break;
1143 case SET:
1144 /* Ignore SETs whose result isn't used but not those that
1145 have side-effects. */
1146 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1147 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1148 || INTVAL (XEXP (note, 0)) <= 0)
1149 && ! side_effects_p (elt))
1150 break;
1152 /* If we have already found a SET, this is a second one and
1153 so we cannot combine with this insn. */
1154 if (set)
1155 return 0;
1157 set = elt;
1158 break;
1160 default:
1161 /* Anything else means we can't combine. */
1162 return 0;
1166 if (set == 0
1167 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1168 so don't do anything with it. */
1169 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1170 return 0;
1172 else
1173 return 0;
1175 if (set == 0)
1176 return 0;
1178 set = expand_field_assignment (set);
1179 src = SET_SRC (set), dest = SET_DEST (set);
1181 /* Don't eliminate a store in the stack pointer. */
1182 if (dest == stack_pointer_rtx
1183 /* Don't combine with an insn that sets a register to itself if it has
1184 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1185 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1186 /* Can't merge an ASM_OPERANDS. */
1187 || GET_CODE (src) == ASM_OPERANDS
1188 /* Can't merge a function call. */
1189 || GET_CODE (src) == CALL
1190 /* Don't eliminate a function call argument. */
1191 || (CALL_P (i3)
1192 && (find_reg_fusage (i3, USE, dest)
1193 || (REG_P (dest)
1194 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1195 && global_regs[REGNO (dest)])))
1196 /* Don't substitute into an incremented register. */
1197 || FIND_REG_INC_NOTE (i3, dest)
1198 || (succ && FIND_REG_INC_NOTE (succ, dest))
1199 /* Don't substitute into a non-local goto, this confuses CFG. */
1200 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1201 #if 0
1202 /* Don't combine the end of a libcall into anything. */
1203 /* ??? This gives worse code, and appears to be unnecessary, since no
1204 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1205 use REG_RETVAL notes for noconflict blocks, but other code here
1206 makes sure that those insns don't disappear. */
1207 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1208 #endif
1209 /* Make sure that DEST is not used after SUCC but before I3. */
1210 || (succ && ! all_adjacent
1211 && reg_used_between_p (dest, succ, i3))
1212 /* Make sure that the value that is to be substituted for the register
1213 does not use any registers whose values alter in between. However,
1214 If the insns are adjacent, a use can't cross a set even though we
1215 think it might (this can happen for a sequence of insns each setting
1216 the same destination; last_set of that register might point to
1217 a NOTE). If INSN has a REG_EQUIV note, the register is always
1218 equivalent to the memory so the substitution is valid even if there
1219 are intervening stores. Also, don't move a volatile asm or
1220 UNSPEC_VOLATILE across any other insns. */
1221 || (! all_adjacent
1222 && (((!MEM_P (src)
1223 || ! find_reg_note (insn, REG_EQUIV, src))
1224 && use_crosses_set_p (src, INSN_CUID (insn)))
1225 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1226 || GET_CODE (src) == UNSPEC_VOLATILE))
1227 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1228 better register allocation by not doing the combine. */
1229 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1230 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1231 /* Don't combine across a CALL_INSN, because that would possibly
1232 change whether the life span of some REGs crosses calls or not,
1233 and it is a pain to update that information.
1234 Exception: if source is a constant, moving it later can't hurt.
1235 Accept that special case, because it helps -fforce-addr a lot. */
1236 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1237 return 0;
1239 /* DEST must either be a REG or CC0. */
1240 if (REG_P (dest))
1242 /* If register alignment is being enforced for multi-word items in all
1243 cases except for parameters, it is possible to have a register copy
1244 insn referencing a hard register that is not allowed to contain the
1245 mode being copied and which would not be valid as an operand of most
1246 insns. Eliminate this problem by not combining with such an insn.
1248 Also, on some machines we don't want to extend the life of a hard
1249 register. */
1251 if (REG_P (src)
1252 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1253 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1254 /* Don't extend the life of a hard register unless it is
1255 user variable (if we have few registers) or it can't
1256 fit into the desired register (meaning something special
1257 is going on).
1258 Also avoid substituting a return register into I3, because
1259 reload can't handle a conflict with constraints of other
1260 inputs. */
1261 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1262 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1263 return 0;
1265 else if (GET_CODE (dest) != CC0)
1266 return 0;
1269 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1270 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1271 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1273 /* Don't substitute for a register intended as a clobberable
1274 operand. */
1275 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1276 if (rtx_equal_p (reg, dest))
1277 return 0;
1279 /* If the clobber represents an earlyclobber operand, we must not
1280 substitute an expression containing the clobbered register.
1281 As we do not analyze the constraint strings here, we have to
1282 make the conservative assumption. However, if the register is
1283 a fixed hard reg, the clobber cannot represent any operand;
1284 we leave it up to the machine description to either accept or
1285 reject use-and-clobber patterns. */
1286 if (!REG_P (reg)
1287 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1288 || !fixed_regs[REGNO (reg)])
1289 if (reg_overlap_mentioned_p (reg, src))
1290 return 0;
1293 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1294 or not), reject, unless nothing volatile comes between it and I3 */
1296 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1298 /* Make sure succ doesn't contain a volatile reference. */
1299 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1300 return 0;
1302 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1303 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1304 return 0;
1307 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1308 to be an explicit register variable, and was chosen for a reason. */
1310 if (GET_CODE (src) == ASM_OPERANDS
1311 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1312 return 0;
1314 /* If there are any volatile insns between INSN and I3, reject, because
1315 they might affect machine state. */
1317 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1318 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1319 return 0;
1321 /* If INSN contains an autoincrement or autodecrement, make sure that
1322 register is not used between there and I3, and not already used in
1323 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1324 Also insist that I3 not be a jump; if it were one
1325 and the incremented register were spilled, we would lose. */
1327 #ifdef AUTO_INC_DEC
1328 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1329 if (REG_NOTE_KIND (link) == REG_INC
1330 && (JUMP_P (i3)
1331 || reg_used_between_p (XEXP (link, 0), insn, i3)
1332 || (pred != NULL_RTX
1333 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1334 || (succ != NULL_RTX
1335 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1336 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1337 return 0;
1338 #endif
1340 #ifdef HAVE_cc0
1341 /* Don't combine an insn that follows a CC0-setting insn.
1342 An insn that uses CC0 must not be separated from the one that sets it.
1343 We do, however, allow I2 to follow a CC0-setting insn if that insn
1344 is passed as I1; in that case it will be deleted also.
1345 We also allow combining in this case if all the insns are adjacent
1346 because that would leave the two CC0 insns adjacent as well.
1347 It would be more logical to test whether CC0 occurs inside I1 or I2,
1348 but that would be much slower, and this ought to be equivalent. */
1350 p = prev_nonnote_insn (insn);
1351 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1352 && ! all_adjacent)
1353 return 0;
1354 #endif
1356 /* If we get here, we have passed all the tests and the combination is
1357 to be allowed. */
1359 *pdest = dest;
1360 *psrc = src;
1362 return 1;
1365 /* LOC is the location within I3 that contains its pattern or the component
1366 of a PARALLEL of the pattern. We validate that it is valid for combining.
1368 One problem is if I3 modifies its output, as opposed to replacing it
1369 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1370 so would produce an insn that is not equivalent to the original insns.
1372 Consider:
1374 (set (reg:DI 101) (reg:DI 100))
1375 (set (subreg:SI (reg:DI 101) 0) <foo>)
1377 This is NOT equivalent to:
1379 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1380 (set (reg:DI 101) (reg:DI 100))])
1382 Not only does this modify 100 (in which case it might still be valid
1383 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1385 We can also run into a problem if I2 sets a register that I1
1386 uses and I1 gets directly substituted into I3 (not via I2). In that
1387 case, we would be getting the wrong value of I2DEST into I3, so we
1388 must reject the combination. This case occurs when I2 and I1 both
1389 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1390 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1391 of a SET must prevent combination from occurring.
1393 Before doing the above check, we first try to expand a field assignment
1394 into a set of logical operations.
1396 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1397 we place a register that is both set and used within I3. If more than one
1398 such register is detected, we fail.
1400 Return 1 if the combination is valid, zero otherwise. */
1402 static int
1403 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1404 int i1_not_in_src, rtx *pi3dest_killed)
1406 rtx x = *loc;
1408 if (GET_CODE (x) == SET)
1410 rtx set = x ;
1411 rtx dest = SET_DEST (set);
1412 rtx src = SET_SRC (set);
1413 rtx inner_dest = dest;
1415 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1416 || GET_CODE (inner_dest) == SUBREG
1417 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1418 inner_dest = XEXP (inner_dest, 0);
1420 /* Check for the case where I3 modifies its output, as discussed
1421 above. We don't want to prevent pseudos from being combined
1422 into the address of a MEM, so only prevent the combination if
1423 i1 or i2 set the same MEM. */
1424 if ((inner_dest != dest &&
1425 (!MEM_P (inner_dest)
1426 || rtx_equal_p (i2dest, inner_dest)
1427 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1428 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1429 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1431 /* This is the same test done in can_combine_p except we can't test
1432 all_adjacent; we don't have to, since this instruction will stay
1433 in place, thus we are not considering increasing the lifetime of
1434 INNER_DEST.
1436 Also, if this insn sets a function argument, combining it with
1437 something that might need a spill could clobber a previous
1438 function argument; the all_adjacent test in can_combine_p also
1439 checks this; here, we do a more specific test for this case. */
1441 || (REG_P (inner_dest)
1442 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1443 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1444 GET_MODE (inner_dest))))
1445 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1446 return 0;
1448 /* If DEST is used in I3, it is being killed in this insn,
1449 so record that for later.
1450 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1451 STACK_POINTER_REGNUM, since these are always considered to be
1452 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1453 if (pi3dest_killed && REG_P (dest)
1454 && reg_referenced_p (dest, PATTERN (i3))
1455 && REGNO (dest) != FRAME_POINTER_REGNUM
1456 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1457 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1458 #endif
1459 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1460 && (REGNO (dest) != ARG_POINTER_REGNUM
1461 || ! fixed_regs [REGNO (dest)])
1462 #endif
1463 && REGNO (dest) != STACK_POINTER_REGNUM)
1465 if (*pi3dest_killed)
1466 return 0;
1468 *pi3dest_killed = dest;
1472 else if (GET_CODE (x) == PARALLEL)
1474 int i;
1476 for (i = 0; i < XVECLEN (x, 0); i++)
1477 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1478 i1_not_in_src, pi3dest_killed))
1479 return 0;
1482 return 1;
1485 /* Return 1 if X is an arithmetic expression that contains a multiplication
1486 and division. We don't count multiplications by powers of two here. */
1488 static int
1489 contains_muldiv (rtx x)
1491 switch (GET_CODE (x))
1493 case MOD: case DIV: case UMOD: case UDIV:
1494 return 1;
1496 case MULT:
1497 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1498 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1499 default:
1500 if (BINARY_P (x))
1501 return contains_muldiv (XEXP (x, 0))
1502 || contains_muldiv (XEXP (x, 1));
1504 if (UNARY_P (x))
1505 return contains_muldiv (XEXP (x, 0));
1507 return 0;
1511 /* Determine whether INSN can be used in a combination. Return nonzero if
1512 not. This is used in try_combine to detect early some cases where we
1513 can't perform combinations. */
1515 static int
1516 cant_combine_insn_p (rtx insn)
1518 rtx set;
1519 rtx src, dest;
1521 /* If this isn't really an insn, we can't do anything.
1522 This can occur when flow deletes an insn that it has merged into an
1523 auto-increment address. */
1524 if (! INSN_P (insn))
1525 return 1;
1527 /* Never combine loads and stores involving hard regs that are likely
1528 to be spilled. The register allocator can usually handle such
1529 reg-reg moves by tying. If we allow the combiner to make
1530 substitutions of likely-spilled regs, reload might die.
1531 As an exception, we allow combinations involving fixed regs; these are
1532 not available to the register allocator so there's no risk involved. */
1534 set = single_set (insn);
1535 if (! set)
1536 return 0;
1537 src = SET_SRC (set);
1538 dest = SET_DEST (set);
1539 if (GET_CODE (src) == SUBREG)
1540 src = SUBREG_REG (src);
1541 if (GET_CODE (dest) == SUBREG)
1542 dest = SUBREG_REG (dest);
1543 if (REG_P (src) && REG_P (dest)
1544 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1545 && ! fixed_regs[REGNO (src)]
1546 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1547 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1548 && ! fixed_regs[REGNO (dest)]
1549 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1550 return 1;
1552 return 0;
1555 /* Adjust INSN after we made a change to its destination.
1557 Changing the destination can invalidate notes that say something about
1558 the results of the insn and a LOG_LINK pointing to the insn. */
1560 static void
1561 adjust_for_new_dest (rtx insn)
1563 rtx *loc;
1565 /* For notes, be conservative and simply remove them. */
1566 loc = &REG_NOTES (insn);
1567 while (*loc)
1569 enum reg_note kind = REG_NOTE_KIND (*loc);
1570 if (kind == REG_EQUAL || kind == REG_EQUIV)
1571 *loc = XEXP (*loc, 1);
1572 else
1573 loc = &XEXP (*loc, 1);
1576 /* The new insn will have a destination that was previously the destination
1577 of an insn just above it. Call distribute_links to make a LOG_LINK from
1578 the next use of that destination. */
1579 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1582 /* Try to combine the insns I1 and I2 into I3.
1583 Here I1 and I2 appear earlier than I3.
1584 I1 can be zero; then we combine just I2 into I3.
1586 If we are combining three insns and the resulting insn is not recognized,
1587 try splitting it into two insns. If that happens, I2 and I3 are retained
1588 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1589 are pseudo-deleted.
1591 Return 0 if the combination does not work. Then nothing is changed.
1592 If we did the combination, return the insn at which combine should
1593 resume scanning.
1595 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1596 new direct jump instruction. */
1598 static rtx
1599 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1601 /* New patterns for I3 and I2, respectively. */
1602 rtx newpat, newi2pat = 0;
1603 rtvec newpat_vec_with_clobbers = 0;
1604 int substed_i2 = 0, substed_i1 = 0;
1605 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1606 int added_sets_1, added_sets_2;
1607 /* Total number of SETs to put into I3. */
1608 int total_sets;
1609 /* Nonzero if I2's body now appears in I3. */
1610 int i2_is_used;
1611 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1612 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1613 /* Contains I3 if the destination of I3 is used in its source, which means
1614 that the old life of I3 is being killed. If that usage is placed into
1615 I2 and not in I3, a REG_DEAD note must be made. */
1616 rtx i3dest_killed = 0;
1617 /* SET_DEST and SET_SRC of I2 and I1. */
1618 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1619 /* PATTERN (I2), or a copy of it in certain cases. */
1620 rtx i2pat;
1621 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1622 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1623 int i1_feeds_i3 = 0;
1624 /* Notes that must be added to REG_NOTES in I3 and I2. */
1625 rtx new_i3_notes, new_i2_notes;
1626 /* Notes that we substituted I3 into I2 instead of the normal case. */
1627 int i3_subst_into_i2 = 0;
1628 /* Notes that I1, I2 or I3 is a MULT operation. */
1629 int have_mult = 0;
1630 int swap_i2i3 = 0;
1632 int maxreg;
1633 rtx temp;
1634 rtx link;
1635 int i;
1637 /* Exit early if one of the insns involved can't be used for
1638 combinations. */
1639 if (cant_combine_insn_p (i3)
1640 || cant_combine_insn_p (i2)
1641 || (i1 && cant_combine_insn_p (i1))
1642 /* We also can't do anything if I3 has a
1643 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1644 libcall. */
1645 #if 0
1646 /* ??? This gives worse code, and appears to be unnecessary, since no
1647 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1648 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1649 #endif
1651 return 0;
1653 combine_attempts++;
1654 undobuf.other_insn = 0;
1656 /* Reset the hard register usage information. */
1657 CLEAR_HARD_REG_SET (newpat_used_regs);
1659 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1660 code below, set I1 to be the earlier of the two insns. */
1661 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1662 temp = i1, i1 = i2, i2 = temp;
1664 added_links_insn = 0;
1666 /* First check for one important special-case that the code below will
1667 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1668 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1669 we may be able to replace that destination with the destination of I3.
1670 This occurs in the common code where we compute both a quotient and
1671 remainder into a structure, in which case we want to do the computation
1672 directly into the structure to avoid register-register copies.
1674 Note that this case handles both multiple sets in I2 and also
1675 cases where I2 has a number of CLOBBER or PARALLELs.
1677 We make very conservative checks below and only try to handle the
1678 most common cases of this. For example, we only handle the case
1679 where I2 and I3 are adjacent to avoid making difficult register
1680 usage tests. */
1682 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
1683 && REG_P (SET_SRC (PATTERN (i3)))
1684 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1685 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1686 && GET_CODE (PATTERN (i2)) == PARALLEL
1687 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1688 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1689 below would need to check what is inside (and reg_overlap_mentioned_p
1690 doesn't support those codes anyway). Don't allow those destinations;
1691 the resulting insn isn't likely to be recognized anyway. */
1692 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1693 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1694 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1695 SET_DEST (PATTERN (i3)))
1696 && next_real_insn (i2) == i3)
1698 rtx p2 = PATTERN (i2);
1700 /* Make sure that the destination of I3,
1701 which we are going to substitute into one output of I2,
1702 is not used within another output of I2. We must avoid making this:
1703 (parallel [(set (mem (reg 69)) ...)
1704 (set (reg 69) ...)])
1705 which is not well-defined as to order of actions.
1706 (Besides, reload can't handle output reloads for this.)
1708 The problem can also happen if the dest of I3 is a memory ref,
1709 if another dest in I2 is an indirect memory ref. */
1710 for (i = 0; i < XVECLEN (p2, 0); i++)
1711 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1712 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1713 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1714 SET_DEST (XVECEXP (p2, 0, i))))
1715 break;
1717 if (i == XVECLEN (p2, 0))
1718 for (i = 0; i < XVECLEN (p2, 0); i++)
1719 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1720 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1721 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1723 combine_merges++;
1725 subst_insn = i3;
1726 subst_low_cuid = INSN_CUID (i2);
1728 added_sets_2 = added_sets_1 = 0;
1729 i2dest = SET_SRC (PATTERN (i3));
1731 /* Replace the dest in I2 with our dest and make the resulting
1732 insn the new pattern for I3. Then skip to where we
1733 validate the pattern. Everything was set up above. */
1734 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1735 SET_DEST (PATTERN (i3)));
1737 newpat = p2;
1738 i3_subst_into_i2 = 1;
1739 goto validate_replacement;
1743 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1744 one of those words to another constant, merge them by making a new
1745 constant. */
1746 if (i1 == 0
1747 && (temp = single_set (i2)) != 0
1748 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1749 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1750 && REG_P (SET_DEST (temp))
1751 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1752 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1753 && GET_CODE (PATTERN (i3)) == SET
1754 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1755 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1756 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1757 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1758 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1760 HOST_WIDE_INT lo, hi;
1762 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1763 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1764 else
1766 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1767 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1770 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1772 /* We don't handle the case of the target word being wider
1773 than a host wide int. */
1774 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD);
1776 lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1777 lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1778 & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1780 else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1781 hi = INTVAL (SET_SRC (PATTERN (i3)));
1782 else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1784 int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1785 >> (HOST_BITS_PER_WIDE_INT - 1));
1787 lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1788 (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1789 lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1790 (INTVAL (SET_SRC (PATTERN (i3)))));
1791 if (hi == sign)
1792 hi = lo < 0 ? -1 : 0;
1794 else
1795 /* We don't handle the case of the higher word not fitting
1796 entirely in either hi or lo. */
1797 gcc_unreachable ();
1799 combine_merges++;
1800 subst_insn = i3;
1801 subst_low_cuid = INSN_CUID (i2);
1802 added_sets_2 = added_sets_1 = 0;
1803 i2dest = SET_DEST (temp);
1805 SUBST (SET_SRC (temp),
1806 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1808 newpat = PATTERN (i2);
1809 goto validate_replacement;
1812 #ifndef HAVE_cc0
1813 /* If we have no I1 and I2 looks like:
1814 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1815 (set Y OP)])
1816 make up a dummy I1 that is
1817 (set Y OP)
1818 and change I2 to be
1819 (set (reg:CC X) (compare:CC Y (const_int 0)))
1821 (We can ignore any trailing CLOBBERs.)
1823 This undoes a previous combination and allows us to match a branch-and-
1824 decrement insn. */
1826 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1827 && XVECLEN (PATTERN (i2), 0) >= 2
1828 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1829 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1830 == MODE_CC)
1831 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1832 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1833 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1834 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
1835 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1836 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1838 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1839 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1840 break;
1842 if (i == 1)
1844 /* We make I1 with the same INSN_UID as I2. This gives it
1845 the same INSN_CUID for value tracking. Our fake I1 will
1846 never appear in the insn stream so giving it the same INSN_UID
1847 as I2 will not cause a problem. */
1849 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1850 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1851 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1852 NULL_RTX);
1854 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1855 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1856 SET_DEST (PATTERN (i1)));
1859 #endif
1861 /* Verify that I2 and I1 are valid for combining. */
1862 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1863 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1865 undo_all ();
1866 return 0;
1869 /* Record whether I2DEST is used in I2SRC and similarly for the other
1870 cases. Knowing this will help in register status updating below. */
1871 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1872 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1873 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1875 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1876 in I2SRC. */
1877 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1879 /* Ensure that I3's pattern can be the destination of combines. */
1880 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1881 i1 && i2dest_in_i1src && i1_feeds_i3,
1882 &i3dest_killed))
1884 undo_all ();
1885 return 0;
1888 /* See if any of the insns is a MULT operation. Unless one is, we will
1889 reject a combination that is, since it must be slower. Be conservative
1890 here. */
1891 if (GET_CODE (i2src) == MULT
1892 || (i1 != 0 && GET_CODE (i1src) == MULT)
1893 || (GET_CODE (PATTERN (i3)) == SET
1894 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1895 have_mult = 1;
1897 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1898 We used to do this EXCEPT in one case: I3 has a post-inc in an
1899 output operand. However, that exception can give rise to insns like
1900 mov r3,(r3)+
1901 which is a famous insn on the PDP-11 where the value of r3 used as the
1902 source was model-dependent. Avoid this sort of thing. */
1904 #if 0
1905 if (!(GET_CODE (PATTERN (i3)) == SET
1906 && REG_P (SET_SRC (PATTERN (i3)))
1907 && MEM_P (SET_DEST (PATTERN (i3)))
1908 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1909 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1910 /* It's not the exception. */
1911 #endif
1912 #ifdef AUTO_INC_DEC
1913 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1914 if (REG_NOTE_KIND (link) == REG_INC
1915 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1916 || (i1 != 0
1917 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1919 undo_all ();
1920 return 0;
1922 #endif
1924 /* See if the SETs in I1 or I2 need to be kept around in the merged
1925 instruction: whenever the value set there is still needed past I3.
1926 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1928 For the SET in I1, we have two cases: If I1 and I2 independently
1929 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1930 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1931 in I1 needs to be kept around unless I1DEST dies or is set in either
1932 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1933 I1DEST. If so, we know I1 feeds into I2. */
1935 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1937 added_sets_1
1938 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1939 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1941 /* If the set in I2 needs to be kept around, we must make a copy of
1942 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1943 PATTERN (I2), we are only substituting for the original I1DEST, not into
1944 an already-substituted copy. This also prevents making self-referential
1945 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1946 I2DEST. */
1948 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1949 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1950 : PATTERN (i2));
1952 if (added_sets_2)
1953 i2pat = copy_rtx (i2pat);
1955 combine_merges++;
1957 /* Substitute in the latest insn for the regs set by the earlier ones. */
1959 maxreg = max_reg_num ();
1961 subst_insn = i3;
1963 /* It is possible that the source of I2 or I1 may be performing an
1964 unneeded operation, such as a ZERO_EXTEND of something that is known
1965 to have the high part zero. Handle that case by letting subst look at
1966 the innermost one of them.
1968 Another way to do this would be to have a function that tries to
1969 simplify a single insn instead of merging two or more insns. We don't
1970 do this because of the potential of infinite loops and because
1971 of the potential extra memory required. However, doing it the way
1972 we are is a bit of a kludge and doesn't catch all cases.
1974 But only do this if -fexpensive-optimizations since it slows things down
1975 and doesn't usually win. */
1977 if (flag_expensive_optimizations)
1979 /* Pass pc_rtx so no substitutions are done, just simplifications. */
1980 if (i1)
1982 subst_low_cuid = INSN_CUID (i1);
1983 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1985 else
1987 subst_low_cuid = INSN_CUID (i2);
1988 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1992 #ifndef HAVE_cc0
1993 /* Many machines that don't use CC0 have insns that can both perform an
1994 arithmetic operation and set the condition code. These operations will
1995 be represented as a PARALLEL with the first element of the vector
1996 being a COMPARE of an arithmetic operation with the constant zero.
1997 The second element of the vector will set some pseudo to the result
1998 of the same arithmetic operation. If we simplify the COMPARE, we won't
1999 match such a pattern and so will generate an extra insn. Here we test
2000 for this case, where both the comparison and the operation result are
2001 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2002 I2SRC. Later we will make the PARALLEL that contains I2. */
2004 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2005 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2006 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2007 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2009 #ifdef SELECT_CC_MODE
2010 rtx *cc_use;
2011 enum machine_mode compare_mode;
2012 #endif
2014 newpat = PATTERN (i3);
2015 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2017 i2_is_used = 1;
2019 #ifdef SELECT_CC_MODE
2020 /* See if a COMPARE with the operand we substituted in should be done
2021 with the mode that is currently being used. If not, do the same
2022 processing we do in `subst' for a SET; namely, if the destination
2023 is used only once, try to replace it with a register of the proper
2024 mode and also replace the COMPARE. */
2025 if (undobuf.other_insn == 0
2026 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2027 &undobuf.other_insn))
2028 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2029 i2src, const0_rtx))
2030 != GET_MODE (SET_DEST (newpat))))
2032 unsigned int regno = REGNO (SET_DEST (newpat));
2033 rtx new_dest = gen_rtx_REG (compare_mode, regno);
2035 if (regno < FIRST_PSEUDO_REGISTER
2036 || (REG_N_SETS (regno) == 1 && ! added_sets_2
2037 && ! REG_USERVAR_P (SET_DEST (newpat))))
2039 if (regno >= FIRST_PSEUDO_REGISTER)
2040 SUBST (regno_reg_rtx[regno], new_dest);
2042 SUBST (SET_DEST (newpat), new_dest);
2043 SUBST (XEXP (*cc_use, 0), new_dest);
2044 SUBST (SET_SRC (newpat),
2045 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2047 else
2048 undobuf.other_insn = 0;
2050 #endif
2052 else
2053 #endif
2055 n_occurrences = 0; /* `subst' counts here */
2057 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2058 need to make a unique copy of I2SRC each time we substitute it
2059 to avoid self-referential rtl. */
2061 subst_low_cuid = INSN_CUID (i2);
2062 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2063 ! i1_feeds_i3 && i1dest_in_i1src);
2064 substed_i2 = 1;
2066 /* Record whether i2's body now appears within i3's body. */
2067 i2_is_used = n_occurrences;
2070 /* If we already got a failure, don't try to do more. Otherwise,
2071 try to substitute in I1 if we have it. */
2073 if (i1 && GET_CODE (newpat) != CLOBBER)
2075 /* Before we can do this substitution, we must redo the test done
2076 above (see detailed comments there) that ensures that I1DEST
2077 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2079 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2080 0, (rtx*) 0))
2082 undo_all ();
2083 return 0;
2086 n_occurrences = 0;
2087 subst_low_cuid = INSN_CUID (i1);
2088 newpat = subst (newpat, i1dest, i1src, 0, 0);
2089 substed_i1 = 1;
2092 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2093 to count all the ways that I2SRC and I1SRC can be used. */
2094 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2095 && i2_is_used + added_sets_2 > 1)
2096 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2097 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2098 > 1))
2099 /* Fail if we tried to make a new register. */
2100 || max_reg_num () != maxreg
2101 /* Fail if we couldn't do something and have a CLOBBER. */
2102 || GET_CODE (newpat) == CLOBBER
2103 /* Fail if this new pattern is a MULT and we didn't have one before
2104 at the outer level. */
2105 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2106 && ! have_mult))
2108 undo_all ();
2109 return 0;
2112 /* If the actions of the earlier insns must be kept
2113 in addition to substituting them into the latest one,
2114 we must make a new PARALLEL for the latest insn
2115 to hold additional the SETs. */
2117 if (added_sets_1 || added_sets_2)
2119 combine_extras++;
2121 if (GET_CODE (newpat) == PARALLEL)
2123 rtvec old = XVEC (newpat, 0);
2124 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2125 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2126 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2127 sizeof (old->elem[0]) * old->num_elem);
2129 else
2131 rtx old = newpat;
2132 total_sets = 1 + added_sets_1 + added_sets_2;
2133 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2134 XVECEXP (newpat, 0, 0) = old;
2137 if (added_sets_1)
2138 XVECEXP (newpat, 0, --total_sets)
2139 = (GET_CODE (PATTERN (i1)) == PARALLEL
2140 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2142 if (added_sets_2)
2144 /* If there is no I1, use I2's body as is. We used to also not do
2145 the subst call below if I2 was substituted into I3,
2146 but that could lose a simplification. */
2147 if (i1 == 0)
2148 XVECEXP (newpat, 0, --total_sets) = i2pat;
2149 else
2150 /* See comment where i2pat is assigned. */
2151 XVECEXP (newpat, 0, --total_sets)
2152 = subst (i2pat, i1dest, i1src, 0, 0);
2156 /* We come here when we are replacing a destination in I2 with the
2157 destination of I3. */
2158 validate_replacement:
2160 /* Note which hard regs this insn has as inputs. */
2161 mark_used_regs_combine (newpat);
2163 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2164 consider splitting this pattern, we might need these clobbers. */
2165 if (i1 && GET_CODE (newpat) == PARALLEL
2166 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2168 int len = XVECLEN (newpat, 0);
2170 newpat_vec_with_clobbers = rtvec_alloc (len);
2171 for (i = 0; i < len; i++)
2172 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2175 /* Is the result of combination a valid instruction? */
2176 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2178 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2179 the second SET's destination is a register that is unused and isn't
2180 marked as an instruction that might trap in an EH region. In that case,
2181 we just need the first SET. This can occur when simplifying a divmod
2182 insn. We *must* test for this case here because the code below that
2183 splits two independent SETs doesn't handle this case correctly when it
2184 updates the register status.
2186 It's pointless doing this if we originally had two sets, one from
2187 i3, and one from i2. Combining then splitting the parallel results
2188 in the original i2 again plus an invalid insn (which we delete).
2189 The net effect is only to move instructions around, which makes
2190 debug info less accurate.
2192 Also check the case where the first SET's destination is unused.
2193 That would not cause incorrect code, but does cause an unneeded
2194 insn to remain. */
2196 if (insn_code_number < 0
2197 && !(added_sets_2 && i1 == 0)
2198 && GET_CODE (newpat) == PARALLEL
2199 && XVECLEN (newpat, 0) == 2
2200 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2201 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2202 && asm_noperands (newpat) < 0)
2204 rtx set0 = XVECEXP (newpat, 0, 0);
2205 rtx set1 = XVECEXP (newpat, 0, 1);
2206 rtx note;
2208 if (((REG_P (SET_DEST (set1))
2209 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2210 || (GET_CODE (SET_DEST (set1)) == SUBREG
2211 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2212 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2213 || INTVAL (XEXP (note, 0)) <= 0)
2214 && ! side_effects_p (SET_SRC (set1)))
2216 newpat = set0;
2217 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2220 else if (((REG_P (SET_DEST (set0))
2221 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2222 || (GET_CODE (SET_DEST (set0)) == SUBREG
2223 && find_reg_note (i3, REG_UNUSED,
2224 SUBREG_REG (SET_DEST (set0)))))
2225 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2226 || INTVAL (XEXP (note, 0)) <= 0)
2227 && ! side_effects_p (SET_SRC (set0)))
2229 newpat = set1;
2230 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2232 if (insn_code_number >= 0)
2234 /* If we will be able to accept this, we have made a
2235 change to the destination of I3. This requires us to
2236 do a few adjustments. */
2238 PATTERN (i3) = newpat;
2239 adjust_for_new_dest (i3);
2244 /* If we were combining three insns and the result is a simple SET
2245 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2246 insns. There are two ways to do this. It can be split using a
2247 machine-specific method (like when you have an addition of a large
2248 constant) or by combine in the function find_split_point. */
2250 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2251 && asm_noperands (newpat) < 0)
2253 rtx m_split, *split;
2254 rtx ni2dest = i2dest;
2256 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2257 use I2DEST as a scratch register will help. In the latter case,
2258 convert I2DEST to the mode of the source of NEWPAT if we can. */
2260 m_split = split_insns (newpat, i3);
2262 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2263 inputs of NEWPAT. */
2265 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2266 possible to try that as a scratch reg. This would require adding
2267 more code to make it work though. */
2269 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2271 /* If I2DEST is a hard register or the only use of a pseudo,
2272 we can change its mode. */
2273 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2274 && GET_MODE (SET_DEST (newpat)) != VOIDmode
2275 && REG_P (i2dest)
2276 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2277 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2278 && ! REG_USERVAR_P (i2dest))))
2279 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2280 REGNO (i2dest));
2282 m_split = split_insns (gen_rtx_PARALLEL
2283 (VOIDmode,
2284 gen_rtvec (2, newpat,
2285 gen_rtx_CLOBBER (VOIDmode,
2286 ni2dest))),
2287 i3);
2288 /* If the split with the mode-changed register didn't work, try
2289 the original register. */
2290 if (! m_split && ni2dest != i2dest)
2292 ni2dest = i2dest;
2293 m_split = split_insns (gen_rtx_PARALLEL
2294 (VOIDmode,
2295 gen_rtvec (2, newpat,
2296 gen_rtx_CLOBBER (VOIDmode,
2297 i2dest))),
2298 i3);
2302 /* If recog_for_combine has discarded clobbers, try to use them
2303 again for the split. */
2304 if (m_split == 0 && newpat_vec_with_clobbers)
2305 m_split
2306 = split_insns (gen_rtx_PARALLEL (VOIDmode,
2307 newpat_vec_with_clobbers), i3);
2309 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2311 m_split = PATTERN (m_split);
2312 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2313 if (insn_code_number >= 0)
2314 newpat = m_split;
2316 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2317 && (next_real_insn (i2) == i3
2318 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2320 rtx i2set, i3set;
2321 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2322 newi2pat = PATTERN (m_split);
2324 i3set = single_set (NEXT_INSN (m_split));
2325 i2set = single_set (m_split);
2327 /* In case we changed the mode of I2DEST, replace it in the
2328 pseudo-register table here. We can't do it above in case this
2329 code doesn't get executed and we do a split the other way. */
2331 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2332 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2334 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2336 /* If I2 or I3 has multiple SETs, we won't know how to track
2337 register status, so don't use these insns. If I2's destination
2338 is used between I2 and I3, we also can't use these insns. */
2340 if (i2_code_number >= 0 && i2set && i3set
2341 && (next_real_insn (i2) == i3
2342 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2343 insn_code_number = recog_for_combine (&newi3pat, i3,
2344 &new_i3_notes);
2345 if (insn_code_number >= 0)
2346 newpat = newi3pat;
2348 /* It is possible that both insns now set the destination of I3.
2349 If so, we must show an extra use of it. */
2351 if (insn_code_number >= 0)
2353 rtx new_i3_dest = SET_DEST (i3set);
2354 rtx new_i2_dest = SET_DEST (i2set);
2356 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2357 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2358 || GET_CODE (new_i3_dest) == SUBREG)
2359 new_i3_dest = XEXP (new_i3_dest, 0);
2361 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2362 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2363 || GET_CODE (new_i2_dest) == SUBREG)
2364 new_i2_dest = XEXP (new_i2_dest, 0);
2366 if (REG_P (new_i3_dest)
2367 && REG_P (new_i2_dest)
2368 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2369 REG_N_SETS (REGNO (new_i2_dest))++;
2373 /* If we can split it and use I2DEST, go ahead and see if that
2374 helps things be recognized. Verify that none of the registers
2375 are set between I2 and I3. */
2376 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2377 #ifdef HAVE_cc0
2378 && REG_P (i2dest)
2379 #endif
2380 /* We need I2DEST in the proper mode. If it is a hard register
2381 or the only use of a pseudo, we can change its mode.
2382 Make sure we don't change a hard register to have a mode that
2383 isn't valid for it, or change the number of registers. */
2384 && (GET_MODE (*split) == GET_MODE (i2dest)
2385 || GET_MODE (*split) == VOIDmode
2386 || (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2387 && HARD_REGNO_MODE_OK (REGNO (i2dest), GET_MODE (*split))
2388 && (HARD_REGNO_NREGS (REGNO (i2dest), GET_MODE (i2dest))
2389 == HARD_REGNO_NREGS (REGNO (i2dest), GET_MODE (*split))))
2390 || (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER
2391 && REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2392 && ! REG_USERVAR_P (i2dest)))
2393 && (next_real_insn (i2) == i3
2394 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2395 /* We can't overwrite I2DEST if its value is still used by
2396 NEWPAT. */
2397 && ! reg_referenced_p (i2dest, newpat))
2399 rtx newdest = i2dest;
2400 enum rtx_code split_code = GET_CODE (*split);
2401 enum machine_mode split_mode = GET_MODE (*split);
2403 /* Get NEWDEST as a register in the proper mode. We have already
2404 validated that we can do this. */
2405 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2407 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2409 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2410 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2413 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2414 an ASHIFT. This can occur if it was inside a PLUS and hence
2415 appeared to be a memory address. This is a kludge. */
2416 if (split_code == MULT
2417 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2418 && INTVAL (XEXP (*split, 1)) > 0
2419 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2421 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2422 XEXP (*split, 0), GEN_INT (i)));
2423 /* Update split_code because we may not have a multiply
2424 anymore. */
2425 split_code = GET_CODE (*split);
2428 #ifdef INSN_SCHEDULING
2429 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2430 be written as a ZERO_EXTEND. */
2431 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
2433 #ifdef LOAD_EXTEND_OP
2434 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2435 what it really is. */
2436 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2437 == SIGN_EXTEND)
2438 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2439 SUBREG_REG (*split)));
2440 else
2441 #endif
2442 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2443 SUBREG_REG (*split)));
2445 #endif
2447 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2448 SUBST (*split, newdest);
2449 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2451 /* recog_for_combine might have added CLOBBERs to newi2pat.
2452 Make sure NEWPAT does not depend on the clobbered regs. */
2453 if (GET_CODE (newi2pat) == PARALLEL)
2454 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
2455 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
2457 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
2458 if (reg_overlap_mentioned_p (reg, newpat))
2460 undo_all ();
2461 return 0;
2465 /* If the split point was a MULT and we didn't have one before,
2466 don't use one now. */
2467 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2468 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2472 /* Check for a case where we loaded from memory in a narrow mode and
2473 then sign extended it, but we need both registers. In that case,
2474 we have a PARALLEL with both loads from the same memory location.
2475 We can split this into a load from memory followed by a register-register
2476 copy. This saves at least one insn, more if register allocation can
2477 eliminate the copy.
2479 We cannot do this if the destination of the first assignment is a
2480 condition code register or cc0. We eliminate this case by making sure
2481 the SET_DEST and SET_SRC have the same mode.
2483 We cannot do this if the destination of the second assignment is
2484 a register that we have already assumed is zero-extended. Similarly
2485 for a SUBREG of such a register. */
2487 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2488 && GET_CODE (newpat) == PARALLEL
2489 && XVECLEN (newpat, 0) == 2
2490 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2491 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2492 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2493 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2494 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2495 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2496 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2497 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2498 INSN_CUID (i2))
2499 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2500 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2501 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2502 (REG_P (temp)
2503 && reg_stat[REGNO (temp)].nonzero_bits != 0
2504 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2505 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2506 && (reg_stat[REGNO (temp)].nonzero_bits
2507 != GET_MODE_MASK (word_mode))))
2508 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2509 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2510 (REG_P (temp)
2511 && reg_stat[REGNO (temp)].nonzero_bits != 0
2512 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2513 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2514 && (reg_stat[REGNO (temp)].nonzero_bits
2515 != GET_MODE_MASK (word_mode)))))
2516 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2517 SET_SRC (XVECEXP (newpat, 0, 1)))
2518 && ! find_reg_note (i3, REG_UNUSED,
2519 SET_DEST (XVECEXP (newpat, 0, 0))))
2521 rtx ni2dest;
2523 newi2pat = XVECEXP (newpat, 0, 0);
2524 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2525 newpat = XVECEXP (newpat, 0, 1);
2526 SUBST (SET_SRC (newpat),
2527 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2528 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2530 if (i2_code_number >= 0)
2531 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2533 if (insn_code_number >= 0)
2534 swap_i2i3 = 1;
2537 /* Similarly, check for a case where we have a PARALLEL of two independent
2538 SETs but we started with three insns. In this case, we can do the sets
2539 as two separate insns. This case occurs when some SET allows two
2540 other insns to combine, but the destination of that SET is still live. */
2542 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2543 && GET_CODE (newpat) == PARALLEL
2544 && XVECLEN (newpat, 0) == 2
2545 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2546 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2547 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2548 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2549 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2550 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2551 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2552 INSN_CUID (i2))
2553 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2554 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2555 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2556 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2557 XVECEXP (newpat, 0, 0))
2558 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2559 XVECEXP (newpat, 0, 1))
2560 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2561 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2563 /* Normally, it doesn't matter which of the two is done first,
2564 but it does if one references cc0. In that case, it has to
2565 be first. */
2566 #ifdef HAVE_cc0
2567 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2569 newi2pat = XVECEXP (newpat, 0, 0);
2570 newpat = XVECEXP (newpat, 0, 1);
2572 else
2573 #endif
2575 newi2pat = XVECEXP (newpat, 0, 1);
2576 newpat = XVECEXP (newpat, 0, 0);
2579 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2581 if (i2_code_number >= 0)
2582 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2585 /* If it still isn't recognized, fail and change things back the way they
2586 were. */
2587 if ((insn_code_number < 0
2588 /* Is the result a reasonable ASM_OPERANDS? */
2589 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2591 undo_all ();
2592 return 0;
2595 /* If we had to change another insn, make sure it is valid also. */
2596 if (undobuf.other_insn)
2598 rtx other_pat = PATTERN (undobuf.other_insn);
2599 rtx new_other_notes;
2600 rtx note, next;
2602 CLEAR_HARD_REG_SET (newpat_used_regs);
2604 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2605 &new_other_notes);
2607 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2609 undo_all ();
2610 return 0;
2613 PATTERN (undobuf.other_insn) = other_pat;
2615 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2616 are still valid. Then add any non-duplicate notes added by
2617 recog_for_combine. */
2618 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2620 next = XEXP (note, 1);
2622 if (REG_NOTE_KIND (note) == REG_UNUSED
2623 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2625 if (REG_P (XEXP (note, 0)))
2626 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2628 remove_note (undobuf.other_insn, note);
2632 for (note = new_other_notes; note; note = XEXP (note, 1))
2633 if (REG_P (XEXP (note, 0)))
2634 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2636 distribute_notes (new_other_notes, undobuf.other_insn,
2637 undobuf.other_insn, NULL_RTX);
2639 #ifdef HAVE_cc0
2640 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2641 they are adjacent to each other or not. */
2643 rtx p = prev_nonnote_insn (i3);
2644 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
2645 && sets_cc0_p (newi2pat))
2647 undo_all ();
2648 return 0;
2651 #endif
2653 /* Only allow this combination if insn_rtx_costs reports that the
2654 replacement instructions are cheaper than the originals. */
2655 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat))
2657 undo_all ();
2658 return 0;
2661 /* We now know that we can do this combination. Merge the insns and
2662 update the status of registers and LOG_LINKS. */
2664 if (swap_i2i3)
2666 rtx insn;
2667 rtx link;
2668 rtx ni2dest;
2670 /* I3 now uses what used to be its destination and which is now
2671 I2's destination. This requires us to do a few adjustments. */
2672 PATTERN (i3) = newpat;
2673 adjust_for_new_dest (i3);
2675 /* We need a LOG_LINK from I3 to I2. But we used to have one,
2676 so we still will.
2678 However, some later insn might be using I2's dest and have
2679 a LOG_LINK pointing at I3. We must remove this link.
2680 The simplest way to remove the link is to point it at I1,
2681 which we know will be a NOTE. */
2683 /* newi2pat is usually a SET here; however, recog_for_combine might
2684 have added some clobbers. */
2685 if (GET_CODE (newi2pat) == PARALLEL)
2686 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
2687 else
2688 ni2dest = SET_DEST (newi2pat);
2690 for (insn = NEXT_INSN (i3);
2691 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2692 || insn != BB_HEAD (this_basic_block->next_bb));
2693 insn = NEXT_INSN (insn))
2695 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2697 for (link = LOG_LINKS (insn); link;
2698 link = XEXP (link, 1))
2699 if (XEXP (link, 0) == i3)
2700 XEXP (link, 0) = i1;
2702 break;
2708 rtx i3notes, i2notes, i1notes = 0;
2709 rtx i3links, i2links, i1links = 0;
2710 rtx midnotes = 0;
2711 unsigned int regno;
2713 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2714 clear them. */
2715 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2716 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2717 if (i1)
2718 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2720 /* Ensure that we do not have something that should not be shared but
2721 occurs multiple times in the new insns. Check this by first
2722 resetting all the `used' flags and then copying anything is shared. */
2724 reset_used_flags (i3notes);
2725 reset_used_flags (i2notes);
2726 reset_used_flags (i1notes);
2727 reset_used_flags (newpat);
2728 reset_used_flags (newi2pat);
2729 if (undobuf.other_insn)
2730 reset_used_flags (PATTERN (undobuf.other_insn));
2732 i3notes = copy_rtx_if_shared (i3notes);
2733 i2notes = copy_rtx_if_shared (i2notes);
2734 i1notes = copy_rtx_if_shared (i1notes);
2735 newpat = copy_rtx_if_shared (newpat);
2736 newi2pat = copy_rtx_if_shared (newi2pat);
2737 if (undobuf.other_insn)
2738 reset_used_flags (PATTERN (undobuf.other_insn));
2740 INSN_CODE (i3) = insn_code_number;
2741 PATTERN (i3) = newpat;
2743 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
2745 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2747 reset_used_flags (call_usage);
2748 call_usage = copy_rtx (call_usage);
2750 if (substed_i2)
2751 replace_rtx (call_usage, i2dest, i2src);
2753 if (substed_i1)
2754 replace_rtx (call_usage, i1dest, i1src);
2756 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2759 if (undobuf.other_insn)
2760 INSN_CODE (undobuf.other_insn) = other_code_number;
2762 /* We had one special case above where I2 had more than one set and
2763 we replaced a destination of one of those sets with the destination
2764 of I3. In that case, we have to update LOG_LINKS of insns later
2765 in this basic block. Note that this (expensive) case is rare.
2767 Also, in this case, we must pretend that all REG_NOTEs for I2
2768 actually came from I3, so that REG_UNUSED notes from I2 will be
2769 properly handled. */
2771 if (i3_subst_into_i2)
2773 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2774 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2775 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
2776 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2777 && ! find_reg_note (i2, REG_UNUSED,
2778 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2779 for (temp = NEXT_INSN (i2);
2780 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2781 || BB_HEAD (this_basic_block) != temp);
2782 temp = NEXT_INSN (temp))
2783 if (temp != i3 && INSN_P (temp))
2784 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2785 if (XEXP (link, 0) == i2)
2786 XEXP (link, 0) = i3;
2788 if (i3notes)
2790 rtx link = i3notes;
2791 while (XEXP (link, 1))
2792 link = XEXP (link, 1);
2793 XEXP (link, 1) = i2notes;
2795 else
2796 i3notes = i2notes;
2797 i2notes = 0;
2800 LOG_LINKS (i3) = 0;
2801 REG_NOTES (i3) = 0;
2802 LOG_LINKS (i2) = 0;
2803 REG_NOTES (i2) = 0;
2805 if (newi2pat)
2807 INSN_CODE (i2) = i2_code_number;
2808 PATTERN (i2) = newi2pat;
2810 else
2811 SET_INSN_DELETED (i2);
2813 if (i1)
2815 LOG_LINKS (i1) = 0;
2816 REG_NOTES (i1) = 0;
2817 SET_INSN_DELETED (i1);
2820 /* Get death notes for everything that is now used in either I3 or
2821 I2 and used to die in a previous insn. If we built two new
2822 patterns, move from I1 to I2 then I2 to I3 so that we get the
2823 proper movement on registers that I2 modifies. */
2825 if (newi2pat)
2827 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2828 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2830 else
2831 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2832 i3, &midnotes);
2834 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2835 if (i3notes)
2836 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2837 if (i2notes)
2838 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2839 if (i1notes)
2840 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2841 if (midnotes)
2842 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2844 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2845 know these are REG_UNUSED and want them to go to the desired insn,
2846 so we always pass it as i3. We have not counted the notes in
2847 reg_n_deaths yet, so we need to do so now. */
2849 if (newi2pat && new_i2_notes)
2851 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2852 if (REG_P (XEXP (temp, 0)))
2853 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2855 distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2858 if (new_i3_notes)
2860 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2861 if (REG_P (XEXP (temp, 0)))
2862 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2864 distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2867 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2868 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2869 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2870 in that case, it might delete I2. Similarly for I2 and I1.
2871 Show an additional death due to the REG_DEAD note we make here. If
2872 we discard it in distribute_notes, we will decrement it again. */
2874 if (i3dest_killed)
2876 if (REG_P (i3dest_killed))
2877 REG_N_DEATHS (REGNO (i3dest_killed))++;
2879 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2880 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2881 NULL_RTX),
2882 NULL_RTX, i2, NULL_RTX);
2883 else
2884 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2885 NULL_RTX),
2886 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2889 if (i2dest_in_i2src)
2891 if (REG_P (i2dest))
2892 REG_N_DEATHS (REGNO (i2dest))++;
2894 if (newi2pat && reg_set_p (i2dest, newi2pat))
2895 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2896 NULL_RTX, i2, NULL_RTX);
2897 else
2898 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2899 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2902 if (i1dest_in_i1src)
2904 if (REG_P (i1dest))
2905 REG_N_DEATHS (REGNO (i1dest))++;
2907 if (newi2pat && reg_set_p (i1dest, newi2pat))
2908 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2909 NULL_RTX, i2, NULL_RTX);
2910 else
2911 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2912 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2915 distribute_links (i3links);
2916 distribute_links (i2links);
2917 distribute_links (i1links);
2919 if (REG_P (i2dest))
2921 rtx link;
2922 rtx i2_insn = 0, i2_val = 0, set;
2924 /* The insn that used to set this register doesn't exist, and
2925 this life of the register may not exist either. See if one of
2926 I3's links points to an insn that sets I2DEST. If it does,
2927 that is now the last known value for I2DEST. If we don't update
2928 this and I2 set the register to a value that depended on its old
2929 contents, we will get confused. If this insn is used, thing
2930 will be set correctly in combine_instructions. */
2932 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2933 if ((set = single_set (XEXP (link, 0))) != 0
2934 && rtx_equal_p (i2dest, SET_DEST (set)))
2935 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2937 record_value_for_reg (i2dest, i2_insn, i2_val);
2939 /* If the reg formerly set in I2 died only once and that was in I3,
2940 zero its use count so it won't make `reload' do any work. */
2941 if (! added_sets_2
2942 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2943 && ! i2dest_in_i2src)
2945 regno = REGNO (i2dest);
2946 REG_N_SETS (regno)--;
2950 if (i1 && REG_P (i1dest))
2952 rtx link;
2953 rtx i1_insn = 0, i1_val = 0, set;
2955 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2956 if ((set = single_set (XEXP (link, 0))) != 0
2957 && rtx_equal_p (i1dest, SET_DEST (set)))
2958 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2960 record_value_for_reg (i1dest, i1_insn, i1_val);
2962 regno = REGNO (i1dest);
2963 if (! added_sets_1 && ! i1dest_in_i1src)
2964 REG_N_SETS (regno)--;
2967 /* Update reg_stat[].nonzero_bits et al for any changes that may have
2968 been made to this insn. The order of
2969 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
2970 can affect nonzero_bits of newpat */
2971 if (newi2pat)
2972 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2973 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2975 /* Set new_direct_jump_p if a new return or simple jump instruction
2976 has been created.
2978 If I3 is now an unconditional jump, ensure that it has a
2979 BARRIER following it since it may have initially been a
2980 conditional jump. It may also be the last nonnote insn. */
2982 if (returnjump_p (i3) || any_uncondjump_p (i3))
2984 *new_direct_jump_p = 1;
2985 mark_jump_label (PATTERN (i3), i3, 0);
2987 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2988 || !BARRIER_P (temp))
2989 emit_barrier_after (i3);
2992 if (undobuf.other_insn != NULL_RTX
2993 && (returnjump_p (undobuf.other_insn)
2994 || any_uncondjump_p (undobuf.other_insn)))
2996 *new_direct_jump_p = 1;
2998 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2999 || !BARRIER_P (temp))
3000 emit_barrier_after (undobuf.other_insn);
3003 /* An NOOP jump does not need barrier, but it does need cleaning up
3004 of CFG. */
3005 if (GET_CODE (newpat) == SET
3006 && SET_SRC (newpat) == pc_rtx
3007 && SET_DEST (newpat) == pc_rtx)
3008 *new_direct_jump_p = 1;
3011 combine_successes++;
3012 undo_commit ();
3014 if (added_links_insn
3015 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
3016 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
3017 return added_links_insn;
3018 else
3019 return newi2pat ? i2 : i3;
3022 /* Undo all the modifications recorded in undobuf. */
3024 static void
3025 undo_all (void)
3027 struct undo *undo, *next;
3029 for (undo = undobuf.undos; undo; undo = next)
3031 next = undo->next;
3032 if (undo->is_int)
3033 *undo->where.i = undo->old_contents.i;
3034 else
3035 *undo->where.r = undo->old_contents.r;
3037 undo->next = undobuf.frees;
3038 undobuf.frees = undo;
3041 undobuf.undos = 0;
3044 /* We've committed to accepting the changes we made. Move all
3045 of the undos to the free list. */
3047 static void
3048 undo_commit (void)
3050 struct undo *undo, *next;
3052 for (undo = undobuf.undos; undo; undo = next)
3054 next = undo->next;
3055 undo->next = undobuf.frees;
3056 undobuf.frees = undo;
3058 undobuf.undos = 0;
3062 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3063 where we have an arithmetic expression and return that point. LOC will
3064 be inside INSN.
3066 try_combine will call this function to see if an insn can be split into
3067 two insns. */
3069 static rtx *
3070 find_split_point (rtx *loc, rtx insn)
3072 rtx x = *loc;
3073 enum rtx_code code = GET_CODE (x);
3074 rtx *split;
3075 unsigned HOST_WIDE_INT len = 0;
3076 HOST_WIDE_INT pos = 0;
3077 int unsignedp = 0;
3078 rtx inner = NULL_RTX;
3080 /* First special-case some codes. */
3081 switch (code)
3083 case SUBREG:
3084 #ifdef INSN_SCHEDULING
3085 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3086 point. */
3087 if (MEM_P (SUBREG_REG (x)))
3088 return loc;
3089 #endif
3090 return find_split_point (&SUBREG_REG (x), insn);
3092 case MEM:
3093 #ifdef HAVE_lo_sum
3094 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3095 using LO_SUM and HIGH. */
3096 if (GET_CODE (XEXP (x, 0)) == CONST
3097 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3099 SUBST (XEXP (x, 0),
3100 gen_rtx_LO_SUM (Pmode,
3101 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3102 XEXP (x, 0)));
3103 return &XEXP (XEXP (x, 0), 0);
3105 #endif
3107 /* If we have a PLUS whose second operand is a constant and the
3108 address is not valid, perhaps will can split it up using
3109 the machine-specific way to split large constants. We use
3110 the first pseudo-reg (one of the virtual regs) as a placeholder;
3111 it will not remain in the result. */
3112 if (GET_CODE (XEXP (x, 0)) == PLUS
3113 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3114 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3116 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3117 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3118 subst_insn);
3120 /* This should have produced two insns, each of which sets our
3121 placeholder. If the source of the second is a valid address,
3122 we can make put both sources together and make a split point
3123 in the middle. */
3125 if (seq
3126 && NEXT_INSN (seq) != NULL_RTX
3127 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3128 && NONJUMP_INSN_P (seq)
3129 && GET_CODE (PATTERN (seq)) == SET
3130 && SET_DEST (PATTERN (seq)) == reg
3131 && ! reg_mentioned_p (reg,
3132 SET_SRC (PATTERN (seq)))
3133 && NONJUMP_INSN_P (NEXT_INSN (seq))
3134 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3135 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3136 && memory_address_p (GET_MODE (x),
3137 SET_SRC (PATTERN (NEXT_INSN (seq)))))
3139 rtx src1 = SET_SRC (PATTERN (seq));
3140 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3142 /* Replace the placeholder in SRC2 with SRC1. If we can
3143 find where in SRC2 it was placed, that can become our
3144 split point and we can replace this address with SRC2.
3145 Just try two obvious places. */
3147 src2 = replace_rtx (src2, reg, src1);
3148 split = 0;
3149 if (XEXP (src2, 0) == src1)
3150 split = &XEXP (src2, 0);
3151 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3152 && XEXP (XEXP (src2, 0), 0) == src1)
3153 split = &XEXP (XEXP (src2, 0), 0);
3155 if (split)
3157 SUBST (XEXP (x, 0), src2);
3158 return split;
3162 /* If that didn't work, perhaps the first operand is complex and
3163 needs to be computed separately, so make a split point there.
3164 This will occur on machines that just support REG + CONST
3165 and have a constant moved through some previous computation. */
3167 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3168 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3169 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3170 return &XEXP (XEXP (x, 0), 0);
3172 break;
3174 case SET:
3175 #ifdef HAVE_cc0
3176 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3177 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3178 we need to put the operand into a register. So split at that
3179 point. */
3181 if (SET_DEST (x) == cc0_rtx
3182 && GET_CODE (SET_SRC (x)) != COMPARE
3183 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3184 && !OBJECT_P (SET_SRC (x))
3185 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3186 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3187 return &SET_SRC (x);
3188 #endif
3190 /* See if we can split SET_SRC as it stands. */
3191 split = find_split_point (&SET_SRC (x), insn);
3192 if (split && split != &SET_SRC (x))
3193 return split;
3195 /* See if we can split SET_DEST as it stands. */
3196 split = find_split_point (&SET_DEST (x), insn);
3197 if (split && split != &SET_DEST (x))
3198 return split;
3200 /* See if this is a bitfield assignment with everything constant. If
3201 so, this is an IOR of an AND, so split it into that. */
3202 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3203 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3204 <= HOST_BITS_PER_WIDE_INT)
3205 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3206 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3207 && GET_CODE (SET_SRC (x)) == CONST_INT
3208 && ((INTVAL (XEXP (SET_DEST (x), 1))
3209 + INTVAL (XEXP (SET_DEST (x), 2)))
3210 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3211 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3213 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3214 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3215 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3216 rtx dest = XEXP (SET_DEST (x), 0);
3217 enum machine_mode mode = GET_MODE (dest);
3218 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3220 if (BITS_BIG_ENDIAN)
3221 pos = GET_MODE_BITSIZE (mode) - len - pos;
3223 if (src == mask)
3224 SUBST (SET_SRC (x),
3225 simplify_gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3226 else
3228 rtx negmask = gen_int_mode (~(mask << pos), mode);
3229 SUBST (SET_SRC (x),
3230 simplify_gen_binary (IOR, mode,
3231 simplify_gen_binary (AND, mode,
3232 dest, negmask),
3233 GEN_INT (src << pos)));
3236 SUBST (SET_DEST (x), dest);
3238 split = find_split_point (&SET_SRC (x), insn);
3239 if (split && split != &SET_SRC (x))
3240 return split;
3243 /* Otherwise, see if this is an operation that we can split into two.
3244 If so, try to split that. */
3245 code = GET_CODE (SET_SRC (x));
3247 switch (code)
3249 case AND:
3250 /* If we are AND'ing with a large constant that is only a single
3251 bit and the result is only being used in a context where we
3252 need to know if it is zero or nonzero, replace it with a bit
3253 extraction. This will avoid the large constant, which might
3254 have taken more than one insn to make. If the constant were
3255 not a valid argument to the AND but took only one insn to make,
3256 this is no worse, but if it took more than one insn, it will
3257 be better. */
3259 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3260 && REG_P (XEXP (SET_SRC (x), 0))
3261 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3262 && REG_P (SET_DEST (x))
3263 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3264 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3265 && XEXP (*split, 0) == SET_DEST (x)
3266 && XEXP (*split, 1) == const0_rtx)
3268 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3269 XEXP (SET_SRC (x), 0),
3270 pos, NULL_RTX, 1, 1, 0, 0);
3271 if (extraction != 0)
3273 SUBST (SET_SRC (x), extraction);
3274 return find_split_point (loc, insn);
3277 break;
3279 case NE:
3280 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3281 is known to be on, this can be converted into a NEG of a shift. */
3282 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3283 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3284 && 1 <= (pos = exact_log2
3285 (nonzero_bits (XEXP (SET_SRC (x), 0),
3286 GET_MODE (XEXP (SET_SRC (x), 0))))))
3288 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3290 SUBST (SET_SRC (x),
3291 gen_rtx_NEG (mode,
3292 gen_rtx_LSHIFTRT (mode,
3293 XEXP (SET_SRC (x), 0),
3294 GEN_INT (pos))));
3296 split = find_split_point (&SET_SRC (x), insn);
3297 if (split && split != &SET_SRC (x))
3298 return split;
3300 break;
3302 case SIGN_EXTEND:
3303 inner = XEXP (SET_SRC (x), 0);
3305 /* We can't optimize if either mode is a partial integer
3306 mode as we don't know how many bits are significant
3307 in those modes. */
3308 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3309 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3310 break;
3312 pos = 0;
3313 len = GET_MODE_BITSIZE (GET_MODE (inner));
3314 unsignedp = 0;
3315 break;
3317 case SIGN_EXTRACT:
3318 case ZERO_EXTRACT:
3319 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3320 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3322 inner = XEXP (SET_SRC (x), 0);
3323 len = INTVAL (XEXP (SET_SRC (x), 1));
3324 pos = INTVAL (XEXP (SET_SRC (x), 2));
3326 if (BITS_BIG_ENDIAN)
3327 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3328 unsignedp = (code == ZERO_EXTRACT);
3330 break;
3332 default:
3333 break;
3336 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3338 enum machine_mode mode = GET_MODE (SET_SRC (x));
3340 /* For unsigned, we have a choice of a shift followed by an
3341 AND or two shifts. Use two shifts for field sizes where the
3342 constant might be too large. We assume here that we can
3343 always at least get 8-bit constants in an AND insn, which is
3344 true for every current RISC. */
3346 if (unsignedp && len <= 8)
3348 SUBST (SET_SRC (x),
3349 gen_rtx_AND (mode,
3350 gen_rtx_LSHIFTRT
3351 (mode, gen_lowpart (mode, inner),
3352 GEN_INT (pos)),
3353 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3355 split = find_split_point (&SET_SRC (x), insn);
3356 if (split && split != &SET_SRC (x))
3357 return split;
3359 else
3361 SUBST (SET_SRC (x),
3362 gen_rtx_fmt_ee
3363 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3364 gen_rtx_ASHIFT (mode,
3365 gen_lowpart (mode, inner),
3366 GEN_INT (GET_MODE_BITSIZE (mode)
3367 - len - pos)),
3368 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3370 split = find_split_point (&SET_SRC (x), insn);
3371 if (split && split != &SET_SRC (x))
3372 return split;
3376 /* See if this is a simple operation with a constant as the second
3377 operand. It might be that this constant is out of range and hence
3378 could be used as a split point. */
3379 if (BINARY_P (SET_SRC (x))
3380 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3381 && (OBJECT_P (XEXP (SET_SRC (x), 0))
3382 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3383 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3384 return &XEXP (SET_SRC (x), 1);
3386 /* Finally, see if this is a simple operation with its first operand
3387 not in a register. The operation might require this operand in a
3388 register, so return it as a split point. We can always do this
3389 because if the first operand were another operation, we would have
3390 already found it as a split point. */
3391 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3392 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3393 return &XEXP (SET_SRC (x), 0);
3395 return 0;
3397 case AND:
3398 case IOR:
3399 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3400 it is better to write this as (not (ior A B)) so we can split it.
3401 Similarly for IOR. */
3402 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3404 SUBST (*loc,
3405 gen_rtx_NOT (GET_MODE (x),
3406 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3407 GET_MODE (x),
3408 XEXP (XEXP (x, 0), 0),
3409 XEXP (XEXP (x, 1), 0))));
3410 return find_split_point (loc, insn);
3413 /* Many RISC machines have a large set of logical insns. If the
3414 second operand is a NOT, put it first so we will try to split the
3415 other operand first. */
3416 if (GET_CODE (XEXP (x, 1)) == NOT)
3418 rtx tem = XEXP (x, 0);
3419 SUBST (XEXP (x, 0), XEXP (x, 1));
3420 SUBST (XEXP (x, 1), tem);
3422 break;
3424 default:
3425 break;
3428 /* Otherwise, select our actions depending on our rtx class. */
3429 switch (GET_RTX_CLASS (code))
3431 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3432 case RTX_TERNARY:
3433 split = find_split_point (&XEXP (x, 2), insn);
3434 if (split)
3435 return split;
3436 /* ... fall through ... */
3437 case RTX_BIN_ARITH:
3438 case RTX_COMM_ARITH:
3439 case RTX_COMPARE:
3440 case RTX_COMM_COMPARE:
3441 split = find_split_point (&XEXP (x, 1), insn);
3442 if (split)
3443 return split;
3444 /* ... fall through ... */
3445 case RTX_UNARY:
3446 /* Some machines have (and (shift ...) ...) insns. If X is not
3447 an AND, but XEXP (X, 0) is, use it as our split point. */
3448 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3449 return &XEXP (x, 0);
3451 split = find_split_point (&XEXP (x, 0), insn);
3452 if (split)
3453 return split;
3454 return loc;
3456 default:
3457 /* Otherwise, we don't have a split point. */
3458 return 0;
3462 /* Throughout X, replace FROM with TO, and return the result.
3463 The result is TO if X is FROM;
3464 otherwise the result is X, but its contents may have been modified.
3465 If they were modified, a record was made in undobuf so that
3466 undo_all will (among other things) return X to its original state.
3468 If the number of changes necessary is too much to record to undo,
3469 the excess changes are not made, so the result is invalid.
3470 The changes already made can still be undone.
3471 undobuf.num_undo is incremented for such changes, so by testing that
3472 the caller can tell whether the result is valid.
3474 `n_occurrences' is incremented each time FROM is replaced.
3476 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3478 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3479 by copying if `n_occurrences' is nonzero. */
3481 static rtx
3482 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3484 enum rtx_code code = GET_CODE (x);
3485 enum machine_mode op0_mode = VOIDmode;
3486 const char *fmt;
3487 int len, i;
3488 rtx new;
3490 /* Two expressions are equal if they are identical copies of a shared
3491 RTX or if they are both registers with the same register number
3492 and mode. */
3494 #define COMBINE_RTX_EQUAL_P(X,Y) \
3495 ((X) == (Y) \
3496 || (REG_P (X) && REG_P (Y) \
3497 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3499 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3501 n_occurrences++;
3502 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3505 /* If X and FROM are the same register but different modes, they will
3506 not have been seen as equal above. However, flow.c will make a
3507 LOG_LINKS entry for that case. If we do nothing, we will try to
3508 rerecognize our original insn and, when it succeeds, we will
3509 delete the feeding insn, which is incorrect.
3511 So force this insn not to match in this (rare) case. */
3512 if (! in_dest && code == REG && REG_P (from)
3513 && REGNO (x) == REGNO (from))
3514 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3516 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3517 of which may contain things that can be combined. */
3518 if (code != MEM && code != LO_SUM && OBJECT_P (x))
3519 return x;
3521 /* It is possible to have a subexpression appear twice in the insn.
3522 Suppose that FROM is a register that appears within TO.
3523 Then, after that subexpression has been scanned once by `subst',
3524 the second time it is scanned, TO may be found. If we were
3525 to scan TO here, we would find FROM within it and create a
3526 self-referent rtl structure which is completely wrong. */
3527 if (COMBINE_RTX_EQUAL_P (x, to))
3528 return to;
3530 /* Parallel asm_operands need special attention because all of the
3531 inputs are shared across the arms. Furthermore, unsharing the
3532 rtl results in recognition failures. Failure to handle this case
3533 specially can result in circular rtl.
3535 Solve this by doing a normal pass across the first entry of the
3536 parallel, and only processing the SET_DESTs of the subsequent
3537 entries. Ug. */
3539 if (code == PARALLEL
3540 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3541 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3543 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3545 /* If this substitution failed, this whole thing fails. */
3546 if (GET_CODE (new) == CLOBBER
3547 && XEXP (new, 0) == const0_rtx)
3548 return new;
3550 SUBST (XVECEXP (x, 0, 0), new);
3552 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3554 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3556 if (!REG_P (dest)
3557 && GET_CODE (dest) != CC0
3558 && GET_CODE (dest) != PC)
3560 new = subst (dest, from, to, 0, unique_copy);
3562 /* If this substitution failed, this whole thing fails. */
3563 if (GET_CODE (new) == CLOBBER
3564 && XEXP (new, 0) == const0_rtx)
3565 return new;
3567 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3571 else
3573 len = GET_RTX_LENGTH (code);
3574 fmt = GET_RTX_FORMAT (code);
3576 /* We don't need to process a SET_DEST that is a register, CC0,
3577 or PC, so set up to skip this common case. All other cases
3578 where we want to suppress replacing something inside a
3579 SET_SRC are handled via the IN_DEST operand. */
3580 if (code == SET
3581 && (REG_P (SET_DEST (x))
3582 || GET_CODE (SET_DEST (x)) == CC0
3583 || GET_CODE (SET_DEST (x)) == PC))
3584 fmt = "ie";
3586 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3587 constant. */
3588 if (fmt[0] == 'e')
3589 op0_mode = GET_MODE (XEXP (x, 0));
3591 for (i = 0; i < len; i++)
3593 if (fmt[i] == 'E')
3595 int j;
3596 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3598 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3600 new = (unique_copy && n_occurrences
3601 ? copy_rtx (to) : to);
3602 n_occurrences++;
3604 else
3606 new = subst (XVECEXP (x, i, j), from, to, 0,
3607 unique_copy);
3609 /* If this substitution failed, this whole thing
3610 fails. */
3611 if (GET_CODE (new) == CLOBBER
3612 && XEXP (new, 0) == const0_rtx)
3613 return new;
3616 SUBST (XVECEXP (x, i, j), new);
3619 else if (fmt[i] == 'e')
3621 /* If this is a register being set, ignore it. */
3622 new = XEXP (x, i);
3623 if (in_dest
3624 && i == 0
3625 && (((code == SUBREG || code == ZERO_EXTRACT)
3626 && REG_P (new))
3627 || code == STRICT_LOW_PART))
3630 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3632 /* In general, don't install a subreg involving two
3633 modes not tieable. It can worsen register
3634 allocation, and can even make invalid reload
3635 insns, since the reg inside may need to be copied
3636 from in the outside mode, and that may be invalid
3637 if it is an fp reg copied in integer mode.
3639 We allow two exceptions to this: It is valid if
3640 it is inside another SUBREG and the mode of that
3641 SUBREG and the mode of the inside of TO is
3642 tieable and it is valid if X is a SET that copies
3643 FROM to CC0. */
3645 if (GET_CODE (to) == SUBREG
3646 && ! MODES_TIEABLE_P (GET_MODE (to),
3647 GET_MODE (SUBREG_REG (to)))
3648 && ! (code == SUBREG
3649 && MODES_TIEABLE_P (GET_MODE (x),
3650 GET_MODE (SUBREG_REG (to))))
3651 #ifdef HAVE_cc0
3652 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3653 #endif
3655 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3657 #ifdef CANNOT_CHANGE_MODE_CLASS
3658 if (code == SUBREG
3659 && REG_P (to)
3660 && REGNO (to) < FIRST_PSEUDO_REGISTER
3661 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3662 GET_MODE (to),
3663 GET_MODE (x)))
3664 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3665 #endif
3667 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3668 n_occurrences++;
3670 else
3671 /* If we are in a SET_DEST, suppress most cases unless we
3672 have gone inside a MEM, in which case we want to
3673 simplify the address. We assume here that things that
3674 are actually part of the destination have their inner
3675 parts in the first expression. This is true for SUBREG,
3676 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3677 things aside from REG and MEM that should appear in a
3678 SET_DEST. */
3679 new = subst (XEXP (x, i), from, to,
3680 (((in_dest
3681 && (code == SUBREG || code == STRICT_LOW_PART
3682 || code == ZERO_EXTRACT))
3683 || code == SET)
3684 && i == 0), unique_copy);
3686 /* If we found that we will have to reject this combination,
3687 indicate that by returning the CLOBBER ourselves, rather than
3688 an expression containing it. This will speed things up as
3689 well as prevent accidents where two CLOBBERs are considered
3690 to be equal, thus producing an incorrect simplification. */
3692 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3693 return new;
3695 if (GET_CODE (x) == SUBREG
3696 && (GET_CODE (new) == CONST_INT
3697 || GET_CODE (new) == CONST_DOUBLE))
3699 enum machine_mode mode = GET_MODE (x);
3701 x = simplify_subreg (GET_MODE (x), new,
3702 GET_MODE (SUBREG_REG (x)),
3703 SUBREG_BYTE (x));
3704 if (! x)
3705 x = gen_rtx_CLOBBER (mode, const0_rtx);
3707 else if (GET_CODE (new) == CONST_INT
3708 && GET_CODE (x) == ZERO_EXTEND)
3710 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3711 new, GET_MODE (XEXP (x, 0)));
3712 gcc_assert (x);
3714 else
3715 SUBST (XEXP (x, i), new);
3720 /* Try to simplify X. If the simplification changed the code, it is likely
3721 that further simplification will help, so loop, but limit the number
3722 of repetitions that will be performed. */
3724 for (i = 0; i < 4; i++)
3726 /* If X is sufficiently simple, don't bother trying to do anything
3727 with it. */
3728 if (code != CONST_INT && code != REG && code != CLOBBER)
3729 x = combine_simplify_rtx (x, op0_mode, in_dest);
3731 if (GET_CODE (x) == code)
3732 break;
3734 code = GET_CODE (x);
3736 /* We no longer know the original mode of operand 0 since we
3737 have changed the form of X) */
3738 op0_mode = VOIDmode;
3741 return x;
3744 /* Simplify X, a piece of RTL. We just operate on the expression at the
3745 outer level; call `subst' to simplify recursively. Return the new
3746 expression.
3748 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
3749 if we are inside a SET_DEST. */
3751 static rtx
3752 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3754 enum rtx_code code = GET_CODE (x);
3755 enum machine_mode mode = GET_MODE (x);
3756 rtx temp;
3757 rtx reversed;
3758 int i;
3760 /* If this is a commutative operation, put a constant last and a complex
3761 expression first. We don't need to do this for comparisons here. */
3762 if (COMMUTATIVE_ARITH_P (x)
3763 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3765 temp = XEXP (x, 0);
3766 SUBST (XEXP (x, 0), XEXP (x, 1));
3767 SUBST (XEXP (x, 1), temp);
3770 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3771 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3772 things. Check for cases where both arms are testing the same
3773 condition.
3775 Don't do anything if all operands are very simple. */
3777 if ((BINARY_P (x)
3778 && ((!OBJECT_P (XEXP (x, 0))
3779 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3780 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3781 || (!OBJECT_P (XEXP (x, 1))
3782 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3783 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3784 || (UNARY_P (x)
3785 && (!OBJECT_P (XEXP (x, 0))
3786 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3787 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3789 rtx cond, true_rtx, false_rtx;
3791 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3792 if (cond != 0
3793 /* If everything is a comparison, what we have is highly unlikely
3794 to be simpler, so don't use it. */
3795 && ! (COMPARISON_P (x)
3796 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
3798 rtx cop1 = const0_rtx;
3799 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3801 if (cond_code == NE && COMPARISON_P (cond))
3802 return x;
3804 /* Simplify the alternative arms; this may collapse the true and
3805 false arms to store-flag values. Be careful to use copy_rtx
3806 here since true_rtx or false_rtx might share RTL with x as a
3807 result of the if_then_else_cond call above. */
3808 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3809 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3811 /* If true_rtx and false_rtx are not general_operands, an if_then_else
3812 is unlikely to be simpler. */
3813 if (general_operand (true_rtx, VOIDmode)
3814 && general_operand (false_rtx, VOIDmode))
3816 enum rtx_code reversed;
3818 /* Restarting if we generate a store-flag expression will cause
3819 us to loop. Just drop through in this case. */
3821 /* If the result values are STORE_FLAG_VALUE and zero, we can
3822 just make the comparison operation. */
3823 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3824 x = simplify_gen_relational (cond_code, mode, VOIDmode,
3825 cond, cop1);
3826 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3827 && ((reversed = reversed_comparison_code_parts
3828 (cond_code, cond, cop1, NULL))
3829 != UNKNOWN))
3830 x = simplify_gen_relational (reversed, mode, VOIDmode,
3831 cond, cop1);
3833 /* Likewise, we can make the negate of a comparison operation
3834 if the result values are - STORE_FLAG_VALUE and zero. */
3835 else if (GET_CODE (true_rtx) == CONST_INT
3836 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3837 && false_rtx == const0_rtx)
3838 x = simplify_gen_unary (NEG, mode,
3839 simplify_gen_relational (cond_code,
3840 mode, VOIDmode,
3841 cond, cop1),
3842 mode);
3843 else if (GET_CODE (false_rtx) == CONST_INT
3844 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3845 && true_rtx == const0_rtx
3846 && ((reversed = reversed_comparison_code_parts
3847 (cond_code, cond, cop1, NULL))
3848 != UNKNOWN))
3849 x = simplify_gen_unary (NEG, mode,
3850 simplify_gen_relational (reversed,
3851 mode, VOIDmode,
3852 cond, cop1),
3853 mode);
3854 else
3855 return gen_rtx_IF_THEN_ELSE (mode,
3856 simplify_gen_relational (cond_code,
3857 mode,
3858 VOIDmode,
3859 cond,
3860 cop1),
3861 true_rtx, false_rtx);
3863 code = GET_CODE (x);
3864 op0_mode = VOIDmode;
3869 /* Try to fold this expression in case we have constants that weren't
3870 present before. */
3871 temp = 0;
3872 switch (GET_RTX_CLASS (code))
3874 case RTX_UNARY:
3875 if (op0_mode == VOIDmode)
3876 op0_mode = GET_MODE (XEXP (x, 0));
3877 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3878 break;
3879 case RTX_COMPARE:
3880 case RTX_COMM_COMPARE:
3882 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3883 if (cmp_mode == VOIDmode)
3885 cmp_mode = GET_MODE (XEXP (x, 1));
3886 if (cmp_mode == VOIDmode)
3887 cmp_mode = op0_mode;
3889 temp = simplify_relational_operation (code, mode, cmp_mode,
3890 XEXP (x, 0), XEXP (x, 1));
3892 break;
3893 case RTX_COMM_ARITH:
3894 case RTX_BIN_ARITH:
3895 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3896 break;
3897 case RTX_BITFIELD_OPS:
3898 case RTX_TERNARY:
3899 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3900 XEXP (x, 1), XEXP (x, 2));
3901 break;
3902 default:
3903 break;
3906 if (temp)
3908 x = temp;
3909 code = GET_CODE (temp);
3910 op0_mode = VOIDmode;
3911 mode = GET_MODE (temp);
3914 /* First see if we can apply the inverse distributive law. */
3915 if (code == PLUS || code == MINUS
3916 || code == AND || code == IOR || code == XOR)
3918 x = apply_distributive_law (x);
3919 code = GET_CODE (x);
3920 op0_mode = VOIDmode;
3923 /* If CODE is an associative operation not otherwise handled, see if we
3924 can associate some operands. This can win if they are constants or
3925 if they are logically related (i.e. (a & b) & a). */
3926 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3927 || code == AND || code == IOR || code == XOR
3928 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3929 && ((INTEGRAL_MODE_P (mode) && code != DIV)
3930 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3932 if (GET_CODE (XEXP (x, 0)) == code)
3934 rtx other = XEXP (XEXP (x, 0), 0);
3935 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3936 rtx inner_op1 = XEXP (x, 1);
3937 rtx inner;
3939 /* Make sure we pass the constant operand if any as the second
3940 one if this is a commutative operation. */
3941 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
3943 rtx tem = inner_op0;
3944 inner_op0 = inner_op1;
3945 inner_op1 = tem;
3947 inner = simplify_binary_operation (code == MINUS ? PLUS
3948 : code == DIV ? MULT
3949 : code,
3950 mode, inner_op0, inner_op1);
3952 /* For commutative operations, try the other pair if that one
3953 didn't simplify. */
3954 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
3956 other = XEXP (XEXP (x, 0), 1);
3957 inner = simplify_binary_operation (code, mode,
3958 XEXP (XEXP (x, 0), 0),
3959 XEXP (x, 1));
3962 if (inner)
3963 return simplify_gen_binary (code, mode, other, inner);
3967 /* A little bit of algebraic simplification here. */
3968 switch (code)
3970 case MEM:
3971 /* Ensure that our address has any ASHIFTs converted to MULT in case
3972 address-recognizing predicates are called later. */
3973 temp = make_compound_operation (XEXP (x, 0), MEM);
3974 SUBST (XEXP (x, 0), temp);
3975 break;
3977 case SUBREG:
3978 if (op0_mode == VOIDmode)
3979 op0_mode = GET_MODE (SUBREG_REG (x));
3981 /* See if this can be moved to simplify_subreg. */
3982 if (CONSTANT_P (SUBREG_REG (x))
3983 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3984 /* Don't call gen_lowpart if the inner mode
3985 is VOIDmode and we cannot simplify it, as SUBREG without
3986 inner mode is invalid. */
3987 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3988 || gen_lowpart_common (mode, SUBREG_REG (x))))
3989 return gen_lowpart (mode, SUBREG_REG (x));
3991 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3992 break;
3994 rtx temp;
3995 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3996 SUBREG_BYTE (x));
3997 if (temp)
3998 return temp;
4001 /* Don't change the mode of the MEM if that would change the meaning
4002 of the address. */
4003 if (MEM_P (SUBREG_REG (x))
4004 && (MEM_VOLATILE_P (SUBREG_REG (x))
4005 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4006 return gen_rtx_CLOBBER (mode, const0_rtx);
4008 /* Note that we cannot do any narrowing for non-constants since
4009 we might have been counting on using the fact that some bits were
4010 zero. We now do this in the SET. */
4012 break;
4014 case NOT:
4015 if (GET_CODE (XEXP (x, 0)) == SUBREG
4016 && subreg_lowpart_p (XEXP (x, 0))
4017 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
4018 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
4019 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
4020 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
4022 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
4024 x = gen_rtx_ROTATE (inner_mode,
4025 simplify_gen_unary (NOT, inner_mode, const1_rtx,
4026 inner_mode),
4027 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
4028 return gen_lowpart (mode, x);
4031 /* Apply De Morgan's laws to reduce number of patterns for machines
4032 with negating logical insns (and-not, nand, etc.). If result has
4033 only one NOT, put it first, since that is how the patterns are
4034 coded. */
4036 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
4038 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
4039 enum machine_mode op_mode;
4041 op_mode = GET_MODE (in1);
4042 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
4044 op_mode = GET_MODE (in2);
4045 if (op_mode == VOIDmode)
4046 op_mode = mode;
4047 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
4049 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
4051 rtx tem = in2;
4052 in2 = in1; in1 = tem;
4055 return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
4056 mode, in1, in2);
4058 break;
4060 case NEG:
4061 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
4062 if (GET_CODE (XEXP (x, 0)) == XOR
4063 && XEXP (XEXP (x, 0), 1) == const1_rtx
4064 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
4065 return simplify_gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4066 constm1_rtx);
4068 temp = expand_compound_operation (XEXP (x, 0));
4070 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4071 replaced by (lshiftrt X C). This will convert
4072 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4074 if (GET_CODE (temp) == ASHIFTRT
4075 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4076 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4077 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4078 INTVAL (XEXP (temp, 1)));
4080 /* If X has only a single bit that might be nonzero, say, bit I, convert
4081 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4082 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4083 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4084 or a SUBREG of one since we'd be making the expression more
4085 complex if it was just a register. */
4087 if (!REG_P (temp)
4088 && ! (GET_CODE (temp) == SUBREG
4089 && REG_P (SUBREG_REG (temp)))
4090 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4092 rtx temp1 = simplify_shift_const
4093 (NULL_RTX, ASHIFTRT, mode,
4094 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4095 GET_MODE_BITSIZE (mode) - 1 - i),
4096 GET_MODE_BITSIZE (mode) - 1 - i);
4098 /* If all we did was surround TEMP with the two shifts, we
4099 haven't improved anything, so don't use it. Otherwise,
4100 we are better off with TEMP1. */
4101 if (GET_CODE (temp1) != ASHIFTRT
4102 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4103 || XEXP (XEXP (temp1, 0), 0) != temp)
4104 return temp1;
4106 break;
4108 case TRUNCATE:
4109 /* We can't handle truncation to a partial integer mode here
4110 because we don't know the real bitsize of the partial
4111 integer mode. */
4112 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4113 break;
4115 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4116 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4117 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4118 SUBST (XEXP (x, 0),
4119 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4120 GET_MODE_MASK (mode), NULL_RTX, 0));
4122 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
4123 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4124 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4125 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4126 return XEXP (XEXP (x, 0), 0);
4128 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4129 (OP:SI foo:SI) if OP is NEG or ABS. */
4130 if ((GET_CODE (XEXP (x, 0)) == ABS
4131 || GET_CODE (XEXP (x, 0)) == NEG)
4132 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4133 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4134 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4135 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4136 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4138 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4139 (truncate:SI x). */
4140 if (GET_CODE (XEXP (x, 0)) == SUBREG
4141 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4142 && subreg_lowpart_p (XEXP (x, 0)))
4143 return SUBREG_REG (XEXP (x, 0));
4145 /* If we know that the value is already truncated, we can
4146 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4147 is nonzero for the corresponding modes. But don't do this
4148 for an (LSHIFTRT (MULT ...)) since this will cause problems
4149 with the umulXi3_highpart patterns. */
4150 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4151 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4152 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4153 >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4154 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4155 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4156 return gen_lowpart (mode, XEXP (x, 0));
4158 /* A truncate of a comparison can be replaced with a subreg if
4159 STORE_FLAG_VALUE permits. This is like the previous test,
4160 but it works even if the comparison is done in a mode larger
4161 than HOST_BITS_PER_WIDE_INT. */
4162 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4163 && COMPARISON_P (XEXP (x, 0))
4164 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4165 return gen_lowpart (mode, XEXP (x, 0));
4167 /* Similarly, a truncate of a register whose value is a
4168 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4169 permits. */
4170 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4171 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4172 && (temp = get_last_value (XEXP (x, 0)))
4173 && COMPARISON_P (temp))
4174 return gen_lowpart (mode, XEXP (x, 0));
4176 break;
4178 case FLOAT_TRUNCATE:
4179 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4180 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4181 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4182 return XEXP (XEXP (x, 0), 0);
4184 /* (float_truncate:SF (float_truncate:DF foo:XF))
4185 = (float_truncate:SF foo:XF).
4186 This may eliminate double rounding, so it is unsafe.
4188 (float_truncate:SF (float_extend:XF foo:DF))
4189 = (float_truncate:SF foo:DF).
4191 (float_truncate:DF (float_extend:XF foo:SF))
4192 = (float_extend:SF foo:DF). */
4193 if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4194 && flag_unsafe_math_optimizations)
4195 || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4196 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4197 0)))
4198 > GET_MODE_SIZE (mode)
4199 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4200 mode,
4201 XEXP (XEXP (x, 0), 0), mode);
4203 /* (float_truncate (float x)) is (float x) */
4204 if (GET_CODE (XEXP (x, 0)) == FLOAT
4205 && (flag_unsafe_math_optimizations
4206 || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4207 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4208 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4209 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4210 return simplify_gen_unary (FLOAT, mode,
4211 XEXP (XEXP (x, 0), 0),
4212 GET_MODE (XEXP (XEXP (x, 0), 0)));
4214 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4215 (OP:SF foo:SF) if OP is NEG or ABS. */
4216 if ((GET_CODE (XEXP (x, 0)) == ABS
4217 || GET_CODE (XEXP (x, 0)) == NEG)
4218 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4219 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4220 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4221 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4223 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4224 is (float_truncate:SF x). */
4225 if (GET_CODE (XEXP (x, 0)) == SUBREG
4226 && subreg_lowpart_p (XEXP (x, 0))
4227 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4228 return SUBREG_REG (XEXP (x, 0));
4229 break;
4230 case FLOAT_EXTEND:
4231 /* (float_extend (float_extend x)) is (float_extend x)
4233 (float_extend (float x)) is (float x) assuming that double
4234 rounding can't happen.
4236 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4237 || (GET_CODE (XEXP (x, 0)) == FLOAT
4238 && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4239 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4240 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4241 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4242 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4243 XEXP (XEXP (x, 0), 0),
4244 GET_MODE (XEXP (XEXP (x, 0), 0)));
4246 break;
4247 #ifdef HAVE_cc0
4248 case COMPARE:
4249 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4250 using cc0, in which case we want to leave it as a COMPARE
4251 so we can distinguish it from a register-register-copy. */
4252 if (XEXP (x, 1) == const0_rtx)
4253 return XEXP (x, 0);
4255 /* x - 0 is the same as x unless x's mode has signed zeros and
4256 allows rounding towards -infinity. Under those conditions,
4257 0 - 0 is -0. */
4258 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4259 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4260 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4261 return XEXP (x, 0);
4262 break;
4263 #endif
4265 case CONST:
4266 /* (const (const X)) can become (const X). Do it this way rather than
4267 returning the inner CONST since CONST can be shared with a
4268 REG_EQUAL note. */
4269 if (GET_CODE (XEXP (x, 0)) == CONST)
4270 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4271 break;
4273 #ifdef HAVE_lo_sum
4274 case LO_SUM:
4275 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4276 can add in an offset. find_split_point will split this address up
4277 again if it doesn't match. */
4278 if (GET_CODE (XEXP (x, 0)) == HIGH
4279 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4280 return XEXP (x, 1);
4281 break;
4282 #endif
4284 case PLUS:
4285 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4287 if (GET_CODE (XEXP (x, 0)) == MULT
4288 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4290 rtx in1, in2;
4292 in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4293 in2 = XEXP (XEXP (x, 0), 1);
4294 return simplify_gen_binary (MINUS, mode, XEXP (x, 1),
4295 simplify_gen_binary (MULT, mode,
4296 in1, in2));
4299 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4300 outermost. That's because that's the way indexed addresses are
4301 supposed to appear. This code used to check many more cases, but
4302 they are now checked elsewhere. */
4303 if (GET_CODE (XEXP (x, 0)) == PLUS
4304 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4305 return simplify_gen_binary (PLUS, mode,
4306 simplify_gen_binary (PLUS, mode,
4307 XEXP (XEXP (x, 0), 0),
4308 XEXP (x, 1)),
4309 XEXP (XEXP (x, 0), 1));
4311 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4312 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4313 bit-field and can be replaced by either a sign_extend or a
4314 sign_extract. The `and' may be a zero_extend and the two
4315 <c>, -<c> constants may be reversed. */
4316 if (GET_CODE (XEXP (x, 0)) == XOR
4317 && GET_CODE (XEXP (x, 1)) == CONST_INT
4318 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4319 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4320 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4321 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4322 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4323 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4324 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4325 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4326 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4327 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4328 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4329 == (unsigned int) i + 1))))
4330 return simplify_shift_const
4331 (NULL_RTX, ASHIFTRT, mode,
4332 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4333 XEXP (XEXP (XEXP (x, 0), 0), 0),
4334 GET_MODE_BITSIZE (mode) - (i + 1)),
4335 GET_MODE_BITSIZE (mode) - (i + 1));
4337 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4338 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4339 is 1. This produces better code than the alternative immediately
4340 below. */
4341 if (COMPARISON_P (XEXP (x, 0))
4342 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4343 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4344 && (reversed = reversed_comparison (XEXP (x, 0), mode)))
4345 return
4346 simplify_gen_unary (NEG, mode, reversed, mode);
4348 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4349 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4350 the bitsize of the mode - 1. This allows simplification of
4351 "a = (b & 8) == 0;" */
4352 if (XEXP (x, 1) == constm1_rtx
4353 && !REG_P (XEXP (x, 0))
4354 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4355 && REG_P (SUBREG_REG (XEXP (x, 0))))
4356 && nonzero_bits (XEXP (x, 0), mode) == 1)
4357 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4358 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4359 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4360 GET_MODE_BITSIZE (mode) - 1),
4361 GET_MODE_BITSIZE (mode) - 1);
4363 /* If we are adding two things that have no bits in common, convert
4364 the addition into an IOR. This will often be further simplified,
4365 for example in cases like ((a & 1) + (a & 2)), which can
4366 become a & 3. */
4368 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4369 && (nonzero_bits (XEXP (x, 0), mode)
4370 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4372 /* Try to simplify the expression further. */
4373 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4374 temp = combine_simplify_rtx (tor, mode, in_dest);
4376 /* If we could, great. If not, do not go ahead with the IOR
4377 replacement, since PLUS appears in many special purpose
4378 address arithmetic instructions. */
4379 if (GET_CODE (temp) != CLOBBER && temp != tor)
4380 return temp;
4382 break;
4384 case MINUS:
4385 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4386 by reversing the comparison code if valid. */
4387 if (STORE_FLAG_VALUE == 1
4388 && XEXP (x, 0) == const1_rtx
4389 && COMPARISON_P (XEXP (x, 1))
4390 && (reversed = reversed_comparison (XEXP (x, 1), mode)))
4391 return reversed;
4393 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4394 (and <foo> (const_int pow2-1)) */
4395 if (GET_CODE (XEXP (x, 1)) == AND
4396 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4397 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4398 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4399 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4400 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4402 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4404 if (GET_CODE (XEXP (x, 1)) == MULT
4405 && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4407 rtx in1, in2;
4409 in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4410 in2 = XEXP (XEXP (x, 1), 1);
4411 return simplify_gen_binary (PLUS, mode,
4412 simplify_gen_binary (MULT, mode,
4413 in1, in2),
4414 XEXP (x, 0));
4417 /* Canonicalize (minus (neg A) (mult B C)) to
4418 (minus (mult (neg B) C) A). */
4419 if (GET_CODE (XEXP (x, 1)) == MULT
4420 && GET_CODE (XEXP (x, 0)) == NEG)
4422 rtx in1, in2;
4424 in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4425 in2 = XEXP (XEXP (x, 1), 1);
4426 return simplify_gen_binary (MINUS, mode,
4427 simplify_gen_binary (MULT, mode,
4428 in1, in2),
4429 XEXP (XEXP (x, 0), 0));
4432 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4433 integers. */
4434 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4435 return simplify_gen_binary (MINUS, mode,
4436 simplify_gen_binary (MINUS, mode,
4437 XEXP (x, 0),
4438 XEXP (XEXP (x, 1), 0)),
4439 XEXP (XEXP (x, 1), 1));
4440 break;
4442 case MULT:
4443 /* If we have (mult (plus A B) C), apply the distributive law and then
4444 the inverse distributive law to see if things simplify. This
4445 occurs mostly in addresses, often when unrolling loops. */
4447 if (GET_CODE (XEXP (x, 0)) == PLUS)
4449 rtx result = distribute_and_simplify_rtx (x, 0);
4450 if (result)
4451 return result;
4454 /* Try simplify a*(b/c) as (a*b)/c. */
4455 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4456 && GET_CODE (XEXP (x, 0)) == DIV)
4458 rtx tem = simplify_binary_operation (MULT, mode,
4459 XEXP (XEXP (x, 0), 0),
4460 XEXP (x, 1));
4461 if (tem)
4462 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4464 break;
4466 case UDIV:
4467 /* If this is a divide by a power of two, treat it as a shift if
4468 its first operand is a shift. */
4469 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4470 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4471 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4472 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4473 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4474 || GET_CODE (XEXP (x, 0)) == ROTATE
4475 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4476 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4477 break;
4479 case EQ: case NE:
4480 case GT: case GTU: case GE: case GEU:
4481 case LT: case LTU: case LE: case LEU:
4482 case UNEQ: case LTGT:
4483 case UNGT: case UNGE:
4484 case UNLT: case UNLE:
4485 case UNORDERED: case ORDERED:
4486 /* If the first operand is a condition code, we can't do anything
4487 with it. */
4488 if (GET_CODE (XEXP (x, 0)) == COMPARE
4489 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4490 && ! CC0_P (XEXP (x, 0))))
4492 rtx op0 = XEXP (x, 0);
4493 rtx op1 = XEXP (x, 1);
4494 enum rtx_code new_code;
4496 if (GET_CODE (op0) == COMPARE)
4497 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4499 /* Simplify our comparison, if possible. */
4500 new_code = simplify_comparison (code, &op0, &op1);
4502 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4503 if only the low-order bit is possibly nonzero in X (such as when
4504 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4505 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4506 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4507 (plus X 1).
4509 Remove any ZERO_EXTRACT we made when thinking this was a
4510 comparison. It may now be simpler to use, e.g., an AND. If a
4511 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4512 the call to make_compound_operation in the SET case. */
4514 if (STORE_FLAG_VALUE == 1
4515 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4516 && op1 == const0_rtx
4517 && mode == GET_MODE (op0)
4518 && nonzero_bits (op0, mode) == 1)
4519 return gen_lowpart (mode,
4520 expand_compound_operation (op0));
4522 else if (STORE_FLAG_VALUE == 1
4523 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4524 && op1 == const0_rtx
4525 && mode == GET_MODE (op0)
4526 && (num_sign_bit_copies (op0, mode)
4527 == GET_MODE_BITSIZE (mode)))
4529 op0 = expand_compound_operation (op0);
4530 return simplify_gen_unary (NEG, mode,
4531 gen_lowpart (mode, op0),
4532 mode);
4535 else if (STORE_FLAG_VALUE == 1
4536 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4537 && op1 == const0_rtx
4538 && mode == GET_MODE (op0)
4539 && nonzero_bits (op0, mode) == 1)
4541 op0 = expand_compound_operation (op0);
4542 return simplify_gen_binary (XOR, mode,
4543 gen_lowpart (mode, op0),
4544 const1_rtx);
4547 else if (STORE_FLAG_VALUE == 1
4548 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4549 && op1 == const0_rtx
4550 && mode == GET_MODE (op0)
4551 && (num_sign_bit_copies (op0, mode)
4552 == GET_MODE_BITSIZE (mode)))
4554 op0 = expand_compound_operation (op0);
4555 return plus_constant (gen_lowpart (mode, op0), 1);
4558 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4559 those above. */
4560 if (STORE_FLAG_VALUE == -1
4561 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4562 && op1 == const0_rtx
4563 && (num_sign_bit_copies (op0, mode)
4564 == GET_MODE_BITSIZE (mode)))
4565 return gen_lowpart (mode,
4566 expand_compound_operation (op0));
4568 else if (STORE_FLAG_VALUE == -1
4569 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4570 && op1 == const0_rtx
4571 && mode == GET_MODE (op0)
4572 && nonzero_bits (op0, mode) == 1)
4574 op0 = expand_compound_operation (op0);
4575 return simplify_gen_unary (NEG, mode,
4576 gen_lowpart (mode, op0),
4577 mode);
4580 else if (STORE_FLAG_VALUE == -1
4581 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4582 && op1 == const0_rtx
4583 && mode == GET_MODE (op0)
4584 && (num_sign_bit_copies (op0, mode)
4585 == GET_MODE_BITSIZE (mode)))
4587 op0 = expand_compound_operation (op0);
4588 return simplify_gen_unary (NOT, mode,
4589 gen_lowpart (mode, op0),
4590 mode);
4593 /* If X is 0/1, (eq X 0) is X-1. */
4594 else if (STORE_FLAG_VALUE == -1
4595 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4596 && op1 == const0_rtx
4597 && mode == GET_MODE (op0)
4598 && nonzero_bits (op0, mode) == 1)
4600 op0 = expand_compound_operation (op0);
4601 return plus_constant (gen_lowpart (mode, op0), -1);
4604 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4605 one bit that might be nonzero, we can convert (ne x 0) to
4606 (ashift x c) where C puts the bit in the sign bit. Remove any
4607 AND with STORE_FLAG_VALUE when we are done, since we are only
4608 going to test the sign bit. */
4609 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4610 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4611 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4612 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4613 && op1 == const0_rtx
4614 && mode == GET_MODE (op0)
4615 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4617 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4618 expand_compound_operation (op0),
4619 GET_MODE_BITSIZE (mode) - 1 - i);
4620 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4621 return XEXP (x, 0);
4622 else
4623 return x;
4626 /* If the code changed, return a whole new comparison. */
4627 if (new_code != code)
4628 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4630 /* Otherwise, keep this operation, but maybe change its operands.
4631 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4632 SUBST (XEXP (x, 0), op0);
4633 SUBST (XEXP (x, 1), op1);
4635 break;
4637 case IF_THEN_ELSE:
4638 return simplify_if_then_else (x);
4640 case ZERO_EXTRACT:
4641 case SIGN_EXTRACT:
4642 case ZERO_EXTEND:
4643 case SIGN_EXTEND:
4644 /* If we are processing SET_DEST, we are done. */
4645 if (in_dest)
4646 return x;
4648 return expand_compound_operation (x);
4650 case SET:
4651 return simplify_set (x);
4653 case AND:
4654 case IOR:
4655 case XOR:
4656 return simplify_logical (x);
4658 case ABS:
4659 /* (abs (neg <foo>)) -> (abs <foo>) */
4660 if (GET_CODE (XEXP (x, 0)) == NEG)
4661 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4663 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4664 do nothing. */
4665 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4666 break;
4668 /* If operand is something known to be positive, ignore the ABS. */
4669 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4670 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4671 <= HOST_BITS_PER_WIDE_INT)
4672 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4673 & ((HOST_WIDE_INT) 1
4674 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4675 == 0)))
4676 return XEXP (x, 0);
4678 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4679 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4680 return gen_rtx_NEG (mode, XEXP (x, 0));
4682 break;
4684 case FFS:
4685 /* (ffs (*_extend <X>)) = (ffs <X>) */
4686 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4687 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4688 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4689 break;
4691 case POPCOUNT:
4692 case PARITY:
4693 /* (pop* (zero_extend <X>)) = (pop* <X>) */
4694 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4695 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4696 break;
4698 case FLOAT:
4699 /* (float (sign_extend <X>)) = (float <X>). */
4700 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4701 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4702 break;
4704 case ASHIFT:
4705 case LSHIFTRT:
4706 case ASHIFTRT:
4707 case ROTATE:
4708 case ROTATERT:
4709 /* If this is a shift by a constant amount, simplify it. */
4710 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4711 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4712 INTVAL (XEXP (x, 1)));
4714 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
4715 SUBST (XEXP (x, 1),
4716 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4717 ((HOST_WIDE_INT) 1
4718 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4719 - 1,
4720 NULL_RTX, 0));
4721 break;
4723 case VEC_SELECT:
4725 rtx op0 = XEXP (x, 0);
4726 rtx op1 = XEXP (x, 1);
4727 int len;
4729 gcc_assert (GET_CODE (op1) == PARALLEL);
4730 len = XVECLEN (op1, 0);
4731 if (len == 1
4732 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4733 && GET_CODE (op0) == VEC_CONCAT)
4735 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4737 /* Try to find the element in the VEC_CONCAT. */
4738 for (;;)
4740 if (GET_MODE (op0) == GET_MODE (x))
4741 return op0;
4742 if (GET_CODE (op0) == VEC_CONCAT)
4744 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4745 if (op0_size < offset)
4746 op0 = XEXP (op0, 0);
4747 else
4749 offset -= op0_size;
4750 op0 = XEXP (op0, 1);
4753 else
4754 break;
4759 break;
4761 default:
4762 break;
4765 return x;
4768 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4770 static rtx
4771 simplify_if_then_else (rtx x)
4773 enum machine_mode mode = GET_MODE (x);
4774 rtx cond = XEXP (x, 0);
4775 rtx true_rtx = XEXP (x, 1);
4776 rtx false_rtx = XEXP (x, 2);
4777 enum rtx_code true_code = GET_CODE (cond);
4778 int comparison_p = COMPARISON_P (cond);
4779 rtx temp;
4780 int i;
4781 enum rtx_code false_code;
4782 rtx reversed;
4784 /* Simplify storing of the truth value. */
4785 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4786 return simplify_gen_relational (true_code, mode, VOIDmode,
4787 XEXP (cond, 0), XEXP (cond, 1));
4789 /* Also when the truth value has to be reversed. */
4790 if (comparison_p
4791 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4792 && (reversed = reversed_comparison (cond, mode)))
4793 return reversed;
4795 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4796 in it is being compared against certain values. Get the true and false
4797 comparisons and see if that says anything about the value of each arm. */
4799 if (comparison_p
4800 && ((false_code = reversed_comparison_code (cond, NULL))
4801 != UNKNOWN)
4802 && REG_P (XEXP (cond, 0)))
4804 HOST_WIDE_INT nzb;
4805 rtx from = XEXP (cond, 0);
4806 rtx true_val = XEXP (cond, 1);
4807 rtx false_val = true_val;
4808 int swapped = 0;
4810 /* If FALSE_CODE is EQ, swap the codes and arms. */
4812 if (false_code == EQ)
4814 swapped = 1, true_code = EQ, false_code = NE;
4815 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4818 /* If we are comparing against zero and the expression being tested has
4819 only a single bit that might be nonzero, that is its value when it is
4820 not equal to zero. Similarly if it is known to be -1 or 0. */
4822 if (true_code == EQ && true_val == const0_rtx
4823 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4824 false_code = EQ, false_val = GEN_INT (nzb);
4825 else if (true_code == EQ && true_val == const0_rtx
4826 && (num_sign_bit_copies (from, GET_MODE (from))
4827 == GET_MODE_BITSIZE (GET_MODE (from))))
4828 false_code = EQ, false_val = constm1_rtx;
4830 /* Now simplify an arm if we know the value of the register in the
4831 branch and it is used in the arm. Be careful due to the potential
4832 of locally-shared RTL. */
4834 if (reg_mentioned_p (from, true_rtx))
4835 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4836 from, true_val),
4837 pc_rtx, pc_rtx, 0, 0);
4838 if (reg_mentioned_p (from, false_rtx))
4839 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4840 from, false_val),
4841 pc_rtx, pc_rtx, 0, 0);
4843 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4844 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4846 true_rtx = XEXP (x, 1);
4847 false_rtx = XEXP (x, 2);
4848 true_code = GET_CODE (cond);
4851 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4852 reversed, do so to avoid needing two sets of patterns for
4853 subtract-and-branch insns. Similarly if we have a constant in the true
4854 arm, the false arm is the same as the first operand of the comparison, or
4855 the false arm is more complicated than the true arm. */
4857 if (comparison_p
4858 && reversed_comparison_code (cond, NULL) != UNKNOWN
4859 && (true_rtx == pc_rtx
4860 || (CONSTANT_P (true_rtx)
4861 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4862 || true_rtx == const0_rtx
4863 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4864 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4865 && !OBJECT_P (false_rtx))
4866 || reg_mentioned_p (true_rtx, false_rtx)
4867 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4869 true_code = reversed_comparison_code (cond, NULL);
4870 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
4871 SUBST (XEXP (x, 1), false_rtx);
4872 SUBST (XEXP (x, 2), true_rtx);
4874 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4875 cond = XEXP (x, 0);
4877 /* It is possible that the conditional has been simplified out. */
4878 true_code = GET_CODE (cond);
4879 comparison_p = COMPARISON_P (cond);
4882 /* If the two arms are identical, we don't need the comparison. */
4884 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4885 return true_rtx;
4887 /* Convert a == b ? b : a to "a". */
4888 if (true_code == EQ && ! side_effects_p (cond)
4889 && !HONOR_NANS (mode)
4890 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4891 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4892 return false_rtx;
4893 else if (true_code == NE && ! side_effects_p (cond)
4894 && !HONOR_NANS (mode)
4895 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4896 && rtx_equal_p (XEXP (cond, 1), false_rtx))
4897 return true_rtx;
4899 /* Look for cases where we have (abs x) or (neg (abs X)). */
4901 if (GET_MODE_CLASS (mode) == MODE_INT
4902 && GET_CODE (false_rtx) == NEG
4903 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4904 && comparison_p
4905 && rtx_equal_p (true_rtx, XEXP (cond, 0))
4906 && ! side_effects_p (true_rtx))
4907 switch (true_code)
4909 case GT:
4910 case GE:
4911 return simplify_gen_unary (ABS, mode, true_rtx, mode);
4912 case LT:
4913 case LE:
4914 return
4915 simplify_gen_unary (NEG, mode,
4916 simplify_gen_unary (ABS, mode, true_rtx, mode),
4917 mode);
4918 default:
4919 break;
4922 /* Look for MIN or MAX. */
4924 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4925 && comparison_p
4926 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4927 && rtx_equal_p (XEXP (cond, 1), false_rtx)
4928 && ! side_effects_p (cond))
4929 switch (true_code)
4931 case GE:
4932 case GT:
4933 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
4934 case LE:
4935 case LT:
4936 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
4937 case GEU:
4938 case GTU:
4939 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
4940 case LEU:
4941 case LTU:
4942 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
4943 default:
4944 break;
4947 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4948 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4949 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4950 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4951 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4952 neither 1 or -1, but it isn't worth checking for. */
4954 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4955 && comparison_p
4956 && GET_MODE_CLASS (mode) == MODE_INT
4957 && ! side_effects_p (x))
4959 rtx t = make_compound_operation (true_rtx, SET);
4960 rtx f = make_compound_operation (false_rtx, SET);
4961 rtx cond_op0 = XEXP (cond, 0);
4962 rtx cond_op1 = XEXP (cond, 1);
4963 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
4964 enum machine_mode m = mode;
4965 rtx z = 0, c1 = NULL_RTX;
4967 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4968 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4969 || GET_CODE (t) == ASHIFT
4970 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4971 && rtx_equal_p (XEXP (t, 0), f))
4972 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4974 /* If an identity-zero op is commutative, check whether there
4975 would be a match if we swapped the operands. */
4976 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4977 || GET_CODE (t) == XOR)
4978 && rtx_equal_p (XEXP (t, 1), f))
4979 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4980 else if (GET_CODE (t) == SIGN_EXTEND
4981 && (GET_CODE (XEXP (t, 0)) == PLUS
4982 || GET_CODE (XEXP (t, 0)) == MINUS
4983 || GET_CODE (XEXP (t, 0)) == IOR
4984 || GET_CODE (XEXP (t, 0)) == XOR
4985 || GET_CODE (XEXP (t, 0)) == ASHIFT
4986 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4987 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4988 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4989 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4990 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4991 && (num_sign_bit_copies (f, GET_MODE (f))
4992 > (unsigned int)
4993 (GET_MODE_BITSIZE (mode)
4994 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4996 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4997 extend_op = SIGN_EXTEND;
4998 m = GET_MODE (XEXP (t, 0));
5000 else if (GET_CODE (t) == SIGN_EXTEND
5001 && (GET_CODE (XEXP (t, 0)) == PLUS
5002 || GET_CODE (XEXP (t, 0)) == IOR
5003 || GET_CODE (XEXP (t, 0)) == XOR)
5004 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5005 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5006 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5007 && (num_sign_bit_copies (f, GET_MODE (f))
5008 > (unsigned int)
5009 (GET_MODE_BITSIZE (mode)
5010 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5012 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5013 extend_op = SIGN_EXTEND;
5014 m = GET_MODE (XEXP (t, 0));
5016 else if (GET_CODE (t) == ZERO_EXTEND
5017 && (GET_CODE (XEXP (t, 0)) == PLUS
5018 || GET_CODE (XEXP (t, 0)) == MINUS
5019 || GET_CODE (XEXP (t, 0)) == IOR
5020 || GET_CODE (XEXP (t, 0)) == XOR
5021 || GET_CODE (XEXP (t, 0)) == ASHIFT
5022 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5023 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5024 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5025 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5026 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5027 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5028 && ((nonzero_bits (f, GET_MODE (f))
5029 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5030 == 0))
5032 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5033 extend_op = ZERO_EXTEND;
5034 m = GET_MODE (XEXP (t, 0));
5036 else if (GET_CODE (t) == ZERO_EXTEND
5037 && (GET_CODE (XEXP (t, 0)) == PLUS
5038 || GET_CODE (XEXP (t, 0)) == IOR
5039 || GET_CODE (XEXP (t, 0)) == XOR)
5040 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5041 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5042 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5043 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5044 && ((nonzero_bits (f, GET_MODE (f))
5045 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5046 == 0))
5048 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5049 extend_op = ZERO_EXTEND;
5050 m = GET_MODE (XEXP (t, 0));
5053 if (z)
5055 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5056 cond_op0, cond_op1),
5057 pc_rtx, pc_rtx, 0, 0);
5058 temp = simplify_gen_binary (MULT, m, temp,
5059 simplify_gen_binary (MULT, m, c1,
5060 const_true_rtx));
5061 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5062 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5064 if (extend_op != UNKNOWN)
5065 temp = simplify_gen_unary (extend_op, mode, temp, m);
5067 return temp;
5071 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5072 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5073 negation of a single bit, we can convert this operation to a shift. We
5074 can actually do this more generally, but it doesn't seem worth it. */
5076 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5077 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5078 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5079 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5080 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5081 == GET_MODE_BITSIZE (mode))
5082 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5083 return
5084 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5085 gen_lowpart (mode, XEXP (cond, 0)), i);
5087 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5088 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5089 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5090 && GET_MODE (XEXP (cond, 0)) == mode
5091 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5092 == nonzero_bits (XEXP (cond, 0), mode)
5093 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5094 return XEXP (cond, 0);
5096 return x;
5099 /* Simplify X, a SET expression. Return the new expression. */
5101 static rtx
5102 simplify_set (rtx x)
5104 rtx src = SET_SRC (x);
5105 rtx dest = SET_DEST (x);
5106 enum machine_mode mode
5107 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5108 rtx other_insn;
5109 rtx *cc_use;
5111 /* (set (pc) (return)) gets written as (return). */
5112 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5113 return src;
5115 /* Now that we know for sure which bits of SRC we are using, see if we can
5116 simplify the expression for the object knowing that we only need the
5117 low-order bits. */
5119 if (GET_MODE_CLASS (mode) == MODE_INT
5120 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5122 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5123 SUBST (SET_SRC (x), src);
5126 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5127 the comparison result and try to simplify it unless we already have used
5128 undobuf.other_insn. */
5129 if ((GET_MODE_CLASS (mode) == MODE_CC
5130 || GET_CODE (src) == COMPARE
5131 || CC0_P (dest))
5132 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5133 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5134 && COMPARISON_P (*cc_use)
5135 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5137 enum rtx_code old_code = GET_CODE (*cc_use);
5138 enum rtx_code new_code;
5139 rtx op0, op1, tmp;
5140 int other_changed = 0;
5141 enum machine_mode compare_mode = GET_MODE (dest);
5143 if (GET_CODE (src) == COMPARE)
5144 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5145 else
5146 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5148 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5149 op0, op1);
5150 if (!tmp)
5151 new_code = old_code;
5152 else if (!CONSTANT_P (tmp))
5154 new_code = GET_CODE (tmp);
5155 op0 = XEXP (tmp, 0);
5156 op1 = XEXP (tmp, 1);
5158 else
5160 rtx pat = PATTERN (other_insn);
5161 undobuf.other_insn = other_insn;
5162 SUBST (*cc_use, tmp);
5164 /* Attempt to simplify CC user. */
5165 if (GET_CODE (pat) == SET)
5167 rtx new = simplify_rtx (SET_SRC (pat));
5168 if (new != NULL_RTX)
5169 SUBST (SET_SRC (pat), new);
5172 /* Convert X into a no-op move. */
5173 SUBST (SET_DEST (x), pc_rtx);
5174 SUBST (SET_SRC (x), pc_rtx);
5175 return x;
5178 /* Simplify our comparison, if possible. */
5179 new_code = simplify_comparison (new_code, &op0, &op1);
5181 #ifdef SELECT_CC_MODE
5182 /* If this machine has CC modes other than CCmode, check to see if we
5183 need to use a different CC mode here. */
5184 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5185 compare_mode = GET_MODE (op0);
5186 else
5187 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5189 #ifndef HAVE_cc0
5190 /* If the mode changed, we have to change SET_DEST, the mode in the
5191 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5192 a hard register, just build new versions with the proper mode. If it
5193 is a pseudo, we lose unless it is only time we set the pseudo, in
5194 which case we can safely change its mode. */
5195 if (compare_mode != GET_MODE (dest))
5197 unsigned int regno = REGNO (dest);
5198 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5200 if (regno < FIRST_PSEUDO_REGISTER
5201 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5203 if (regno >= FIRST_PSEUDO_REGISTER)
5204 SUBST (regno_reg_rtx[regno], new_dest);
5206 SUBST (SET_DEST (x), new_dest);
5207 SUBST (XEXP (*cc_use, 0), new_dest);
5208 other_changed = 1;
5210 dest = new_dest;
5213 #endif /* cc0 */
5214 #endif /* SELECT_CC_MODE */
5216 /* If the code changed, we have to build a new comparison in
5217 undobuf.other_insn. */
5218 if (new_code != old_code)
5220 int other_changed_previously = other_changed;
5221 unsigned HOST_WIDE_INT mask;
5223 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5224 dest, const0_rtx));
5225 other_changed = 1;
5227 /* If the only change we made was to change an EQ into an NE or
5228 vice versa, OP0 has only one bit that might be nonzero, and OP1
5229 is zero, check if changing the user of the condition code will
5230 produce a valid insn. If it won't, we can keep the original code
5231 in that insn by surrounding our operation with an XOR. */
5233 if (((old_code == NE && new_code == EQ)
5234 || (old_code == EQ && new_code == NE))
5235 && ! other_changed_previously && op1 == const0_rtx
5236 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5237 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5239 rtx pat = PATTERN (other_insn), note = 0;
5241 if ((recog_for_combine (&pat, other_insn, &note) < 0
5242 && ! check_asm_operands (pat)))
5244 PUT_CODE (*cc_use, old_code);
5245 other_changed = 0;
5247 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5248 op0, GEN_INT (mask));
5253 if (other_changed)
5254 undobuf.other_insn = other_insn;
5256 #ifdef HAVE_cc0
5257 /* If we are now comparing against zero, change our source if
5258 needed. If we do not use cc0, we always have a COMPARE. */
5259 if (op1 == const0_rtx && dest == cc0_rtx)
5261 SUBST (SET_SRC (x), op0);
5262 src = op0;
5264 else
5265 #endif
5267 /* Otherwise, if we didn't previously have a COMPARE in the
5268 correct mode, we need one. */
5269 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5271 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5272 src = SET_SRC (x);
5274 else
5276 /* Otherwise, update the COMPARE if needed. */
5277 SUBST (XEXP (src, 0), op0);
5278 SUBST (XEXP (src, 1), op1);
5281 else
5283 /* Get SET_SRC in a form where we have placed back any
5284 compound expressions. Then do the checks below. */
5285 src = make_compound_operation (src, SET);
5286 SUBST (SET_SRC (x), src);
5289 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5290 and X being a REG or (subreg (reg)), we may be able to convert this to
5291 (set (subreg:m2 x) (op)).
5293 We can always do this if M1 is narrower than M2 because that means that
5294 we only care about the low bits of the result.
5296 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5297 perform a narrower operation than requested since the high-order bits will
5298 be undefined. On machine where it is defined, this transformation is safe
5299 as long as M1 and M2 have the same number of words. */
5301 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5302 && !OBJECT_P (SUBREG_REG (src))
5303 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5304 / UNITS_PER_WORD)
5305 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5306 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5307 #ifndef WORD_REGISTER_OPERATIONS
5308 && (GET_MODE_SIZE (GET_MODE (src))
5309 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5310 #endif
5311 #ifdef CANNOT_CHANGE_MODE_CLASS
5312 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5313 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5314 GET_MODE (SUBREG_REG (src)),
5315 GET_MODE (src)))
5316 #endif
5317 && (REG_P (dest)
5318 || (GET_CODE (dest) == SUBREG
5319 && REG_P (SUBREG_REG (dest)))))
5321 SUBST (SET_DEST (x),
5322 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5323 dest));
5324 SUBST (SET_SRC (x), SUBREG_REG (src));
5326 src = SET_SRC (x), dest = SET_DEST (x);
5329 #ifdef HAVE_cc0
5330 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5331 in SRC. */
5332 if (dest == cc0_rtx
5333 && GET_CODE (src) == SUBREG
5334 && subreg_lowpart_p (src)
5335 && (GET_MODE_BITSIZE (GET_MODE (src))
5336 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5338 rtx inner = SUBREG_REG (src);
5339 enum machine_mode inner_mode = GET_MODE (inner);
5341 /* Here we make sure that we don't have a sign bit on. */
5342 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5343 && (nonzero_bits (inner, inner_mode)
5344 < ((unsigned HOST_WIDE_INT) 1
5345 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5347 SUBST (SET_SRC (x), inner);
5348 src = SET_SRC (x);
5351 #endif
5353 #ifdef LOAD_EXTEND_OP
5354 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5355 would require a paradoxical subreg. Replace the subreg with a
5356 zero_extend to avoid the reload that would otherwise be required. */
5358 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5359 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5360 && SUBREG_BYTE (src) == 0
5361 && (GET_MODE_SIZE (GET_MODE (src))
5362 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5363 && MEM_P (SUBREG_REG (src)))
5365 SUBST (SET_SRC (x),
5366 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5367 GET_MODE (src), SUBREG_REG (src)));
5369 src = SET_SRC (x);
5371 #endif
5373 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5374 are comparing an item known to be 0 or -1 against 0, use a logical
5375 operation instead. Check for one of the arms being an IOR of the other
5376 arm with some value. We compute three terms to be IOR'ed together. In
5377 practice, at most two will be nonzero. Then we do the IOR's. */
5379 if (GET_CODE (dest) != PC
5380 && GET_CODE (src) == IF_THEN_ELSE
5381 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5382 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5383 && XEXP (XEXP (src, 0), 1) == const0_rtx
5384 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5385 #ifdef HAVE_conditional_move
5386 && ! can_conditionally_move_p (GET_MODE (src))
5387 #endif
5388 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5389 GET_MODE (XEXP (XEXP (src, 0), 0)))
5390 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5391 && ! side_effects_p (src))
5393 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5394 ? XEXP (src, 1) : XEXP (src, 2));
5395 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5396 ? XEXP (src, 2) : XEXP (src, 1));
5397 rtx term1 = const0_rtx, term2, term3;
5399 if (GET_CODE (true_rtx) == IOR
5400 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5401 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5402 else if (GET_CODE (true_rtx) == IOR
5403 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5404 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5405 else if (GET_CODE (false_rtx) == IOR
5406 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5407 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5408 else if (GET_CODE (false_rtx) == IOR
5409 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5410 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5412 term2 = simplify_gen_binary (AND, GET_MODE (src),
5413 XEXP (XEXP (src, 0), 0), true_rtx);
5414 term3 = simplify_gen_binary (AND, GET_MODE (src),
5415 simplify_gen_unary (NOT, GET_MODE (src),
5416 XEXP (XEXP (src, 0), 0),
5417 GET_MODE (src)),
5418 false_rtx);
5420 SUBST (SET_SRC (x),
5421 simplify_gen_binary (IOR, GET_MODE (src),
5422 simplify_gen_binary (IOR, GET_MODE (src),
5423 term1, term2),
5424 term3));
5426 src = SET_SRC (x);
5429 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5430 whole thing fail. */
5431 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5432 return src;
5433 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5434 return dest;
5435 else
5436 /* Convert this into a field assignment operation, if possible. */
5437 return make_field_assignment (x);
5440 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5441 result. */
5443 static rtx
5444 simplify_logical (rtx x)
5446 enum machine_mode mode = GET_MODE (x);
5447 rtx op0 = XEXP (x, 0);
5448 rtx op1 = XEXP (x, 1);
5449 rtx reversed;
5451 switch (GET_CODE (x))
5453 case AND:
5454 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5455 insn (and may simplify more). */
5456 if (GET_CODE (op0) == XOR
5457 && rtx_equal_p (XEXP (op0, 0), op1)
5458 && ! side_effects_p (op1))
5459 x = simplify_gen_binary (AND, mode,
5460 simplify_gen_unary (NOT, mode,
5461 XEXP (op0, 1), mode),
5462 op1);
5464 if (GET_CODE (op0) == XOR
5465 && rtx_equal_p (XEXP (op0, 1), op1)
5466 && ! side_effects_p (op1))
5467 x = simplify_gen_binary (AND, mode,
5468 simplify_gen_unary (NOT, mode,
5469 XEXP (op0, 0), mode),
5470 op1);
5472 /* Similarly for (~(A ^ B)) & A. */
5473 if (GET_CODE (op0) == NOT
5474 && GET_CODE (XEXP (op0, 0)) == XOR
5475 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5476 && ! side_effects_p (op1))
5477 x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5479 if (GET_CODE (op0) == NOT
5480 && GET_CODE (XEXP (op0, 0)) == XOR
5481 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5482 && ! side_effects_p (op1))
5483 x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5485 /* We can call simplify_and_const_int only if we don't lose
5486 any (sign) bits when converting INTVAL (op1) to
5487 "unsigned HOST_WIDE_INT". */
5488 if (GET_CODE (op1) == CONST_INT
5489 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5490 || INTVAL (op1) > 0))
5492 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5494 /* If we have (ior (and (X C1) C2)) and the next restart would be
5495 the last, simplify this by making C1 as small as possible
5496 and then exit. Only do this if C1 actually changes: for now
5497 this only saves memory but, should this transformation be
5498 moved to simplify-rtx.c, we'd risk unbounded recursion there. */
5499 if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5500 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5501 && GET_CODE (op1) == CONST_INT
5502 && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5503 return simplify_gen_binary (IOR, mode,
5504 simplify_gen_binary
5505 (AND, mode, XEXP (op0, 0),
5506 GEN_INT (INTVAL (XEXP (op0, 1))
5507 & ~INTVAL (op1))), op1);
5509 if (GET_CODE (x) != AND)
5510 return x;
5512 op0 = XEXP (x, 0);
5513 op1 = XEXP (x, 1);
5516 /* Convert (A | B) & A to A. */
5517 if (GET_CODE (op0) == IOR
5518 && (rtx_equal_p (XEXP (op0, 0), op1)
5519 || rtx_equal_p (XEXP (op0, 1), op1))
5520 && ! side_effects_p (XEXP (op0, 0))
5521 && ! side_effects_p (XEXP (op0, 1)))
5522 return op1;
5524 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5525 apply the distributive law and then the inverse distributive
5526 law to see if things simplify. */
5527 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5529 rtx result = distribute_and_simplify_rtx (x, 0);
5530 if (result)
5531 return result;
5533 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5535 rtx result = distribute_and_simplify_rtx (x, 1);
5536 if (result)
5537 return result;
5539 break;
5541 case IOR:
5542 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5543 if (GET_CODE (op1) == CONST_INT
5544 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5545 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5546 return op1;
5548 /* Convert (A & B) | A to A. */
5549 if (GET_CODE (op0) == AND
5550 && (rtx_equal_p (XEXP (op0, 0), op1)
5551 || rtx_equal_p (XEXP (op0, 1), op1))
5552 && ! side_effects_p (XEXP (op0, 0))
5553 && ! side_effects_p (XEXP (op0, 1)))
5554 return op1;
5556 /* If we have (ior (and A B) C), apply the distributive law and then
5557 the inverse distributive law to see if things simplify. */
5559 if (GET_CODE (op0) == AND)
5561 rtx result = distribute_and_simplify_rtx (x, 0);
5562 if (result)
5563 return result;
5566 if (GET_CODE (op1) == AND)
5568 rtx result = distribute_and_simplify_rtx (x, 1);
5569 if (result)
5570 return result;
5573 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5574 mode size to (rotate A CX). */
5576 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5577 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5578 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5579 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5580 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5581 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5582 == GET_MODE_BITSIZE (mode)))
5583 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5584 (GET_CODE (op0) == ASHIFT
5585 ? XEXP (op0, 1) : XEXP (op1, 1)));
5587 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5588 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5589 does not affect any of the bits in OP1, it can really be done
5590 as a PLUS and we can associate. We do this by seeing if OP1
5591 can be safely shifted left C bits. */
5592 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5593 && GET_CODE (XEXP (op0, 0)) == PLUS
5594 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5595 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5596 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5598 int count = INTVAL (XEXP (op0, 1));
5599 HOST_WIDE_INT mask = INTVAL (op1) << count;
5601 if (mask >> count == INTVAL (op1)
5602 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5604 SUBST (XEXP (XEXP (op0, 0), 1),
5605 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5606 return op0;
5609 break;
5611 case XOR:
5612 /* If we are XORing two things that have no bits in common,
5613 convert them into an IOR. This helps to detect rotation encoded
5614 using those methods and possibly other simplifications. */
5616 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5617 && (nonzero_bits (op0, mode)
5618 & nonzero_bits (op1, mode)) == 0)
5619 return (simplify_gen_binary (IOR, mode, op0, op1));
5621 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5622 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5623 (NOT y). */
5625 int num_negated = 0;
5627 if (GET_CODE (op0) == NOT)
5628 num_negated++, op0 = XEXP (op0, 0);
5629 if (GET_CODE (op1) == NOT)
5630 num_negated++, op1 = XEXP (op1, 0);
5632 if (num_negated == 2)
5634 SUBST (XEXP (x, 0), op0);
5635 SUBST (XEXP (x, 1), op1);
5637 else if (num_negated == 1)
5638 return
5639 simplify_gen_unary (NOT, mode,
5640 simplify_gen_binary (XOR, mode, op0, op1),
5641 mode);
5644 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5645 correspond to a machine insn or result in further simplifications
5646 if B is a constant. */
5648 if (GET_CODE (op0) == AND
5649 && rtx_equal_p (XEXP (op0, 1), op1)
5650 && ! side_effects_p (op1))
5651 return simplify_gen_binary (AND, mode,
5652 simplify_gen_unary (NOT, mode,
5653 XEXP (op0, 0), mode),
5654 op1);
5656 else if (GET_CODE (op0) == AND
5657 && rtx_equal_p (XEXP (op0, 0), op1)
5658 && ! side_effects_p (op1))
5659 return simplify_gen_binary (AND, mode,
5660 simplify_gen_unary (NOT, mode,
5661 XEXP (op0, 1), mode),
5662 op1);
5664 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5665 comparison if STORE_FLAG_VALUE is 1. */
5666 if (STORE_FLAG_VALUE == 1
5667 && op1 == const1_rtx
5668 && COMPARISON_P (op0)
5669 && (reversed = reversed_comparison (op0, mode)))
5670 return reversed;
5672 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5673 is (lt foo (const_int 0)), so we can perform the above
5674 simplification if STORE_FLAG_VALUE is 1. */
5676 if (STORE_FLAG_VALUE == 1
5677 && op1 == const1_rtx
5678 && GET_CODE (op0) == LSHIFTRT
5679 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5680 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5681 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5683 /* (xor (comparison foo bar) (const_int sign-bit))
5684 when STORE_FLAG_VALUE is the sign bit. */
5685 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5686 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5687 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5688 && op1 == const_true_rtx
5689 && COMPARISON_P (op0)
5690 && (reversed = reversed_comparison (op0, mode)))
5691 return reversed;
5693 break;
5695 default:
5696 gcc_unreachable ();
5699 return x;
5702 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5703 operations" because they can be replaced with two more basic operations.
5704 ZERO_EXTEND is also considered "compound" because it can be replaced with
5705 an AND operation, which is simpler, though only one operation.
5707 The function expand_compound_operation is called with an rtx expression
5708 and will convert it to the appropriate shifts and AND operations,
5709 simplifying at each stage.
5711 The function make_compound_operation is called to convert an expression
5712 consisting of shifts and ANDs into the equivalent compound expression.
5713 It is the inverse of this function, loosely speaking. */
5715 static rtx
5716 expand_compound_operation (rtx x)
5718 unsigned HOST_WIDE_INT pos = 0, len;
5719 int unsignedp = 0;
5720 unsigned int modewidth;
5721 rtx tem;
5723 switch (GET_CODE (x))
5725 case ZERO_EXTEND:
5726 unsignedp = 1;
5727 case SIGN_EXTEND:
5728 /* We can't necessarily use a const_int for a multiword mode;
5729 it depends on implicitly extending the value.
5730 Since we don't know the right way to extend it,
5731 we can't tell whether the implicit way is right.
5733 Even for a mode that is no wider than a const_int,
5734 we can't win, because we need to sign extend one of its bits through
5735 the rest of it, and we don't know which bit. */
5736 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5737 return x;
5739 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5740 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5741 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5742 reloaded. If not for that, MEM's would very rarely be safe.
5744 Reject MODEs bigger than a word, because we might not be able
5745 to reference a two-register group starting with an arbitrary register
5746 (and currently gen_lowpart might crash for a SUBREG). */
5748 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5749 return x;
5751 /* Reject MODEs that aren't scalar integers because turning vector
5752 or complex modes into shifts causes problems. */
5754 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5755 return x;
5757 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5758 /* If the inner object has VOIDmode (the only way this can happen
5759 is if it is an ASM_OPERANDS), we can't do anything since we don't
5760 know how much masking to do. */
5761 if (len == 0)
5762 return x;
5764 break;
5766 case ZERO_EXTRACT:
5767 unsignedp = 1;
5769 /* ... fall through ... */
5771 case SIGN_EXTRACT:
5772 /* If the operand is a CLOBBER, just return it. */
5773 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5774 return XEXP (x, 0);
5776 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5777 || GET_CODE (XEXP (x, 2)) != CONST_INT
5778 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5779 return x;
5781 /* Reject MODEs that aren't scalar integers because turning vector
5782 or complex modes into shifts causes problems. */
5784 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5785 return x;
5787 len = INTVAL (XEXP (x, 1));
5788 pos = INTVAL (XEXP (x, 2));
5790 /* If this goes outside the object being extracted, replace the object
5791 with a (use (mem ...)) construct that only combine understands
5792 and is used only for this purpose. */
5793 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5794 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5796 if (BITS_BIG_ENDIAN)
5797 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5799 break;
5801 default:
5802 return x;
5804 /* Convert sign extension to zero extension, if we know that the high
5805 bit is not set, as this is easier to optimize. It will be converted
5806 back to cheaper alternative in make_extraction. */
5807 if (GET_CODE (x) == SIGN_EXTEND
5808 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5809 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5810 & ~(((unsigned HOST_WIDE_INT)
5811 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5812 >> 1))
5813 == 0)))
5815 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5816 rtx temp2 = expand_compound_operation (temp);
5818 /* Make sure this is a profitable operation. */
5819 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5820 return temp2;
5821 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5822 return temp;
5823 else
5824 return x;
5827 /* We can optimize some special cases of ZERO_EXTEND. */
5828 if (GET_CODE (x) == ZERO_EXTEND)
5830 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5831 know that the last value didn't have any inappropriate bits
5832 set. */
5833 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5834 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5835 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5836 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5837 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5838 return XEXP (XEXP (x, 0), 0);
5840 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5841 if (GET_CODE (XEXP (x, 0)) == SUBREG
5842 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5843 && subreg_lowpart_p (XEXP (x, 0))
5844 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5845 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5846 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5847 return SUBREG_REG (XEXP (x, 0));
5849 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5850 is a comparison and STORE_FLAG_VALUE permits. This is like
5851 the first case, but it works even when GET_MODE (x) is larger
5852 than HOST_WIDE_INT. */
5853 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5854 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5855 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5856 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5857 <= HOST_BITS_PER_WIDE_INT)
5858 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5859 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5860 return XEXP (XEXP (x, 0), 0);
5862 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5863 if (GET_CODE (XEXP (x, 0)) == SUBREG
5864 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5865 && subreg_lowpart_p (XEXP (x, 0))
5866 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5867 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5868 <= HOST_BITS_PER_WIDE_INT)
5869 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5870 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5871 return SUBREG_REG (XEXP (x, 0));
5875 /* If we reach here, we want to return a pair of shifts. The inner
5876 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5877 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5878 logical depending on the value of UNSIGNEDP.
5880 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5881 converted into an AND of a shift.
5883 We must check for the case where the left shift would have a negative
5884 count. This can happen in a case like (x >> 31) & 255 on machines
5885 that can't shift by a constant. On those machines, we would first
5886 combine the shift with the AND to produce a variable-position
5887 extraction. Then the constant of 31 would be substituted in to produce
5888 a such a position. */
5890 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5891 if (modewidth + len >= pos)
5892 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5893 GET_MODE (x),
5894 simplify_shift_const (NULL_RTX, ASHIFT,
5895 GET_MODE (x),
5896 XEXP (x, 0),
5897 modewidth - pos - len),
5898 modewidth - len);
5900 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5901 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5902 simplify_shift_const (NULL_RTX, LSHIFTRT,
5903 GET_MODE (x),
5904 XEXP (x, 0), pos),
5905 ((HOST_WIDE_INT) 1 << len) - 1);
5906 else
5907 /* Any other cases we can't handle. */
5908 return x;
5910 /* If we couldn't do this for some reason, return the original
5911 expression. */
5912 if (GET_CODE (tem) == CLOBBER)
5913 return x;
5915 return tem;
5918 /* X is a SET which contains an assignment of one object into
5919 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5920 or certain SUBREGS). If possible, convert it into a series of
5921 logical operations.
5923 We half-heartedly support variable positions, but do not at all
5924 support variable lengths. */
5926 static rtx
5927 expand_field_assignment (rtx x)
5929 rtx inner;
5930 rtx pos; /* Always counts from low bit. */
5931 int len;
5932 rtx mask, cleared, masked;
5933 enum machine_mode compute_mode;
5935 /* Loop until we find something we can't simplify. */
5936 while (1)
5938 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5939 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5941 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5942 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5943 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5945 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5946 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5948 inner = XEXP (SET_DEST (x), 0);
5949 len = INTVAL (XEXP (SET_DEST (x), 1));
5950 pos = XEXP (SET_DEST (x), 2);
5952 /* If the position is constant and spans the width of INNER,
5953 surround INNER with a USE to indicate this. */
5954 if (GET_CODE (pos) == CONST_INT
5955 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5956 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5958 if (BITS_BIG_ENDIAN)
5960 if (GET_CODE (pos) == CONST_INT)
5961 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5962 - INTVAL (pos));
5963 else if (GET_CODE (pos) == MINUS
5964 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5965 && (INTVAL (XEXP (pos, 1))
5966 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5967 /* If position is ADJUST - X, new position is X. */
5968 pos = XEXP (pos, 0);
5969 else
5970 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
5971 GEN_INT (GET_MODE_BITSIZE (
5972 GET_MODE (inner))
5973 - len),
5974 pos);
5978 /* A SUBREG between two modes that occupy the same numbers of words
5979 can be done by moving the SUBREG to the source. */
5980 else if (GET_CODE (SET_DEST (x)) == SUBREG
5981 /* We need SUBREGs to compute nonzero_bits properly. */
5982 && nonzero_sign_valid
5983 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5984 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5985 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5986 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5988 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5989 gen_lowpart
5990 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5991 SET_SRC (x)));
5992 continue;
5994 else
5995 break;
5997 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5998 inner = SUBREG_REG (inner);
6000 compute_mode = GET_MODE (inner);
6002 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6003 if (! SCALAR_INT_MODE_P (compute_mode))
6005 enum machine_mode imode;
6007 /* Don't do anything for vector or complex integral types. */
6008 if (! FLOAT_MODE_P (compute_mode))
6009 break;
6011 /* Try to find an integral mode to pun with. */
6012 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6013 if (imode == BLKmode)
6014 break;
6016 compute_mode = imode;
6017 inner = gen_lowpart (imode, inner);
6020 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6021 if (len >= HOST_BITS_PER_WIDE_INT)
6022 break;
6024 /* Now compute the equivalent expression. Make a copy of INNER
6025 for the SET_DEST in case it is a MEM into which we will substitute;
6026 we don't want shared RTL in that case. */
6027 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6028 cleared = simplify_gen_binary (AND, compute_mode,
6029 simplify_gen_unary (NOT, compute_mode,
6030 simplify_gen_binary (ASHIFT,
6031 compute_mode,
6032 mask, pos),
6033 compute_mode),
6034 inner);
6035 masked = simplify_gen_binary (ASHIFT, compute_mode,
6036 simplify_gen_binary (
6037 AND, compute_mode,
6038 gen_lowpart (compute_mode, SET_SRC (x)),
6039 mask),
6040 pos);
6042 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6043 simplify_gen_binary (IOR, compute_mode,
6044 cleared, masked));
6047 return x;
6050 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6051 it is an RTX that represents a variable starting position; otherwise,
6052 POS is the (constant) starting bit position (counted from the LSB).
6054 INNER may be a USE. This will occur when we started with a bitfield
6055 that went outside the boundary of the object in memory, which is
6056 allowed on most machines. To isolate this case, we produce a USE
6057 whose mode is wide enough and surround the MEM with it. The only
6058 code that understands the USE is this routine. If it is not removed,
6059 it will cause the resulting insn not to match.
6061 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6062 signed reference.
6064 IN_DEST is nonzero if this is a reference in the destination of a
6065 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6066 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6067 be used.
6069 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6070 ZERO_EXTRACT should be built even for bits starting at bit 0.
6072 MODE is the desired mode of the result (if IN_DEST == 0).
6074 The result is an RTX for the extraction or NULL_RTX if the target
6075 can't handle it. */
6077 static rtx
6078 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6079 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6080 int in_dest, int in_compare)
6082 /* This mode describes the size of the storage area
6083 to fetch the overall value from. Within that, we
6084 ignore the POS lowest bits, etc. */
6085 enum machine_mode is_mode = GET_MODE (inner);
6086 enum machine_mode inner_mode;
6087 enum machine_mode wanted_inner_mode = byte_mode;
6088 enum machine_mode wanted_inner_reg_mode = word_mode;
6089 enum machine_mode pos_mode = word_mode;
6090 enum machine_mode extraction_mode = word_mode;
6091 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6092 int spans_byte = 0;
6093 rtx new = 0;
6094 rtx orig_pos_rtx = pos_rtx;
6095 HOST_WIDE_INT orig_pos;
6097 /* Get some information about INNER and get the innermost object. */
6098 if (GET_CODE (inner) == USE)
6099 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
6100 /* We don't need to adjust the position because we set up the USE
6101 to pretend that it was a full-word object. */
6102 spans_byte = 1, inner = XEXP (inner, 0);
6103 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6105 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6106 consider just the QI as the memory to extract from.
6107 The subreg adds or removes high bits; its mode is
6108 irrelevant to the meaning of this extraction,
6109 since POS and LEN count from the lsb. */
6110 if (MEM_P (SUBREG_REG (inner)))
6111 is_mode = GET_MODE (SUBREG_REG (inner));
6112 inner = SUBREG_REG (inner);
6114 else if (GET_CODE (inner) == ASHIFT
6115 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6116 && pos_rtx == 0 && pos == 0
6117 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6119 /* We're extracting the least significant bits of an rtx
6120 (ashift X (const_int C)), where LEN > C. Extract the
6121 least significant (LEN - C) bits of X, giving an rtx
6122 whose mode is MODE, then shift it left C times. */
6123 new = make_extraction (mode, XEXP (inner, 0),
6124 0, 0, len - INTVAL (XEXP (inner, 1)),
6125 unsignedp, in_dest, in_compare);
6126 if (new != 0)
6127 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6130 inner_mode = GET_MODE (inner);
6132 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6133 pos = INTVAL (pos_rtx), pos_rtx = 0;
6135 /* See if this can be done without an extraction. We never can if the
6136 width of the field is not the same as that of some integer mode. For
6137 registers, we can only avoid the extraction if the position is at the
6138 low-order bit and this is either not in the destination or we have the
6139 appropriate STRICT_LOW_PART operation available.
6141 For MEM, we can avoid an extract if the field starts on an appropriate
6142 boundary and we can change the mode of the memory reference. However,
6143 we cannot directly access the MEM if we have a USE and the underlying
6144 MEM is not TMODE. This combination means that MEM was being used in a
6145 context where bits outside its mode were being referenced; that is only
6146 valid in bit-field insns. */
6148 if (tmode != BLKmode
6149 && ! (spans_byte && inner_mode != tmode)
6150 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6151 && !MEM_P (inner)
6152 && (! in_dest
6153 || (REG_P (inner)
6154 && have_insn_for (STRICT_LOW_PART, tmode))))
6155 || (MEM_P (inner) && pos_rtx == 0
6156 && (pos
6157 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6158 : BITS_PER_UNIT)) == 0
6159 /* We can't do this if we are widening INNER_MODE (it
6160 may not be aligned, for one thing). */
6161 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6162 && (inner_mode == tmode
6163 || (! mode_dependent_address_p (XEXP (inner, 0))
6164 && ! MEM_VOLATILE_P (inner))))))
6166 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6167 field. If the original and current mode are the same, we need not
6168 adjust the offset. Otherwise, we do if bytes big endian.
6170 If INNER is not a MEM, get a piece consisting of just the field
6171 of interest (in this case POS % BITS_PER_WORD must be 0). */
6173 if (MEM_P (inner))
6175 HOST_WIDE_INT offset;
6177 /* POS counts from lsb, but make OFFSET count in memory order. */
6178 if (BYTES_BIG_ENDIAN)
6179 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6180 else
6181 offset = pos / BITS_PER_UNIT;
6183 new = adjust_address_nv (inner, tmode, offset);
6185 else if (REG_P (inner))
6187 if (tmode != inner_mode)
6189 /* We can't call gen_lowpart in a DEST since we
6190 always want a SUBREG (see below) and it would sometimes
6191 return a new hard register. */
6192 if (pos || in_dest)
6194 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6196 if (WORDS_BIG_ENDIAN
6197 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6198 final_word = ((GET_MODE_SIZE (inner_mode)
6199 - GET_MODE_SIZE (tmode))
6200 / UNITS_PER_WORD) - final_word;
6202 final_word *= UNITS_PER_WORD;
6203 if (BYTES_BIG_ENDIAN &&
6204 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6205 final_word += (GET_MODE_SIZE (inner_mode)
6206 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6208 /* Avoid creating invalid subregs, for example when
6209 simplifying (x>>32)&255. */
6210 if (final_word >= GET_MODE_SIZE (inner_mode))
6211 return NULL_RTX;
6213 new = gen_rtx_SUBREG (tmode, inner, final_word);
6215 else
6216 new = gen_lowpart (tmode, inner);
6218 else
6219 new = inner;
6221 else
6222 new = force_to_mode (inner, tmode,
6223 len >= HOST_BITS_PER_WIDE_INT
6224 ? ~(unsigned HOST_WIDE_INT) 0
6225 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6226 NULL_RTX, 0);
6228 /* If this extraction is going into the destination of a SET,
6229 make a STRICT_LOW_PART unless we made a MEM. */
6231 if (in_dest)
6232 return (MEM_P (new) ? new
6233 : (GET_CODE (new) != SUBREG
6234 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6235 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6237 if (mode == tmode)
6238 return new;
6240 if (GET_CODE (new) == CONST_INT)
6241 return gen_int_mode (INTVAL (new), mode);
6243 /* If we know that no extraneous bits are set, and that the high
6244 bit is not set, convert the extraction to the cheaper of
6245 sign and zero extension, that are equivalent in these cases. */
6246 if (flag_expensive_optimizations
6247 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6248 && ((nonzero_bits (new, tmode)
6249 & ~(((unsigned HOST_WIDE_INT)
6250 GET_MODE_MASK (tmode))
6251 >> 1))
6252 == 0)))
6254 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6255 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6257 /* Prefer ZERO_EXTENSION, since it gives more information to
6258 backends. */
6259 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6260 return temp;
6261 return temp1;
6264 /* Otherwise, sign- or zero-extend unless we already are in the
6265 proper mode. */
6267 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6268 mode, new));
6271 /* Unless this is a COMPARE or we have a funny memory reference,
6272 don't do anything with zero-extending field extracts starting at
6273 the low-order bit since they are simple AND operations. */
6274 if (pos_rtx == 0 && pos == 0 && ! in_dest
6275 && ! in_compare && ! spans_byte && unsignedp)
6276 return 0;
6278 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6279 we would be spanning bytes or if the position is not a constant and the
6280 length is not 1. In all other cases, we would only be going outside
6281 our object in cases when an original shift would have been
6282 undefined. */
6283 if (! spans_byte && MEM_P (inner)
6284 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6285 || (pos_rtx != 0 && len != 1)))
6286 return 0;
6288 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6289 and the mode for the result. */
6290 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6292 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6293 pos_mode = mode_for_extraction (EP_insv, 2);
6294 extraction_mode = mode_for_extraction (EP_insv, 3);
6297 if (! in_dest && unsignedp
6298 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6300 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6301 pos_mode = mode_for_extraction (EP_extzv, 3);
6302 extraction_mode = mode_for_extraction (EP_extzv, 0);
6305 if (! in_dest && ! unsignedp
6306 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6308 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6309 pos_mode = mode_for_extraction (EP_extv, 3);
6310 extraction_mode = mode_for_extraction (EP_extv, 0);
6313 /* Never narrow an object, since that might not be safe. */
6315 if (mode != VOIDmode
6316 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6317 extraction_mode = mode;
6319 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6320 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6321 pos_mode = GET_MODE (pos_rtx);
6323 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6324 if we have to change the mode of memory and cannot, the desired mode is
6325 EXTRACTION_MODE. */
6326 if (!MEM_P (inner))
6327 wanted_inner_mode = wanted_inner_reg_mode;
6328 else if (inner_mode != wanted_inner_mode
6329 && (mode_dependent_address_p (XEXP (inner, 0))
6330 || MEM_VOLATILE_P (inner)))
6331 wanted_inner_mode = extraction_mode;
6333 orig_pos = pos;
6335 if (BITS_BIG_ENDIAN)
6337 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6338 BITS_BIG_ENDIAN style. If position is constant, compute new
6339 position. Otherwise, build subtraction.
6340 Note that POS is relative to the mode of the original argument.
6341 If it's a MEM we need to recompute POS relative to that.
6342 However, if we're extracting from (or inserting into) a register,
6343 we want to recompute POS relative to wanted_inner_mode. */
6344 int width = (MEM_P (inner)
6345 ? GET_MODE_BITSIZE (is_mode)
6346 : GET_MODE_BITSIZE (wanted_inner_mode));
6348 if (pos_rtx == 0)
6349 pos = width - len - pos;
6350 else
6351 pos_rtx
6352 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6353 /* POS may be less than 0 now, but we check for that below.
6354 Note that it can only be less than 0 if !MEM_P (inner). */
6357 /* If INNER has a wider mode, make it smaller. If this is a constant
6358 extract, try to adjust the byte to point to the byte containing
6359 the value. */
6360 if (wanted_inner_mode != VOIDmode
6361 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6362 && ((MEM_P (inner)
6363 && (inner_mode == wanted_inner_mode
6364 || (! mode_dependent_address_p (XEXP (inner, 0))
6365 && ! MEM_VOLATILE_P (inner))))))
6367 int offset = 0;
6369 /* The computations below will be correct if the machine is big
6370 endian in both bits and bytes or little endian in bits and bytes.
6371 If it is mixed, we must adjust. */
6373 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6374 adjust OFFSET to compensate. */
6375 if (BYTES_BIG_ENDIAN
6376 && ! spans_byte
6377 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6378 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6380 /* If this is a constant position, we can move to the desired byte. */
6381 if (pos_rtx == 0)
6383 offset += pos / BITS_PER_UNIT;
6384 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6387 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6388 && ! spans_byte
6389 && is_mode != wanted_inner_mode)
6390 offset = (GET_MODE_SIZE (is_mode)
6391 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6393 if (offset != 0 || inner_mode != wanted_inner_mode)
6394 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6397 /* If INNER is not memory, we can always get it into the proper mode. If we
6398 are changing its mode, POS must be a constant and smaller than the size
6399 of the new mode. */
6400 else if (!MEM_P (inner))
6402 if (GET_MODE (inner) != wanted_inner_mode
6403 && (pos_rtx != 0
6404 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6405 return 0;
6407 inner = force_to_mode (inner, wanted_inner_mode,
6408 pos_rtx
6409 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6410 ? ~(unsigned HOST_WIDE_INT) 0
6411 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6412 << orig_pos),
6413 NULL_RTX, 0);
6416 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6417 have to zero extend. Otherwise, we can just use a SUBREG. */
6418 if (pos_rtx != 0
6419 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6421 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6423 /* If we know that no extraneous bits are set, and that the high
6424 bit is not set, convert extraction to cheaper one - either
6425 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6426 cases. */
6427 if (flag_expensive_optimizations
6428 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6429 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6430 & ~(((unsigned HOST_WIDE_INT)
6431 GET_MODE_MASK (GET_MODE (pos_rtx)))
6432 >> 1))
6433 == 0)))
6435 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6437 /* Prefer ZERO_EXTENSION, since it gives more information to
6438 backends. */
6439 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6440 temp = temp1;
6442 pos_rtx = temp;
6444 else if (pos_rtx != 0
6445 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6446 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6448 /* Make POS_RTX unless we already have it and it is correct. If we don't
6449 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6450 be a CONST_INT. */
6451 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6452 pos_rtx = orig_pos_rtx;
6454 else if (pos_rtx == 0)
6455 pos_rtx = GEN_INT (pos);
6457 /* Make the required operation. See if we can use existing rtx. */
6458 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6459 extraction_mode, inner, GEN_INT (len), pos_rtx);
6460 if (! in_dest)
6461 new = gen_lowpart (mode, new);
6463 return new;
6466 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6467 with any other operations in X. Return X without that shift if so. */
6469 static rtx
6470 extract_left_shift (rtx x, int count)
6472 enum rtx_code code = GET_CODE (x);
6473 enum machine_mode mode = GET_MODE (x);
6474 rtx tem;
6476 switch (code)
6478 case ASHIFT:
6479 /* This is the shift itself. If it is wide enough, we will return
6480 either the value being shifted if the shift count is equal to
6481 COUNT or a shift for the difference. */
6482 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6483 && INTVAL (XEXP (x, 1)) >= count)
6484 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6485 INTVAL (XEXP (x, 1)) - count);
6486 break;
6488 case NEG: case NOT:
6489 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6490 return simplify_gen_unary (code, mode, tem, mode);
6492 break;
6494 case PLUS: case IOR: case XOR: case AND:
6495 /* If we can safely shift this constant and we find the inner shift,
6496 make a new operation. */
6497 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6498 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6499 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6500 return simplify_gen_binary (code, mode, tem,
6501 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6503 break;
6505 default:
6506 break;
6509 return 0;
6512 /* Look at the expression rooted at X. Look for expressions
6513 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6514 Form these expressions.
6516 Return the new rtx, usually just X.
6518 Also, for machines like the VAX that don't have logical shift insns,
6519 try to convert logical to arithmetic shift operations in cases where
6520 they are equivalent. This undoes the canonicalizations to logical
6521 shifts done elsewhere.
6523 We try, as much as possible, to re-use rtl expressions to save memory.
6525 IN_CODE says what kind of expression we are processing. Normally, it is
6526 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6527 being kludges), it is MEM. When processing the arguments of a comparison
6528 or a COMPARE against zero, it is COMPARE. */
6530 static rtx
6531 make_compound_operation (rtx x, enum rtx_code in_code)
6533 enum rtx_code code = GET_CODE (x);
6534 enum machine_mode mode = GET_MODE (x);
6535 int mode_width = GET_MODE_BITSIZE (mode);
6536 rtx rhs, lhs;
6537 enum rtx_code next_code;
6538 int i;
6539 rtx new = 0;
6540 rtx tem;
6541 const char *fmt;
6543 /* Select the code to be used in recursive calls. Once we are inside an
6544 address, we stay there. If we have a comparison, set to COMPARE,
6545 but once inside, go back to our default of SET. */
6547 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6548 : ((code == COMPARE || COMPARISON_P (x))
6549 && XEXP (x, 1) == const0_rtx) ? COMPARE
6550 : in_code == COMPARE ? SET : in_code);
6552 /* Process depending on the code of this operation. If NEW is set
6553 nonzero, it will be returned. */
6555 switch (code)
6557 case ASHIFT:
6558 /* Convert shifts by constants into multiplications if inside
6559 an address. */
6560 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6561 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6562 && INTVAL (XEXP (x, 1)) >= 0)
6564 new = make_compound_operation (XEXP (x, 0), next_code);
6565 new = gen_rtx_MULT (mode, new,
6566 GEN_INT ((HOST_WIDE_INT) 1
6567 << INTVAL (XEXP (x, 1))));
6569 break;
6571 case AND:
6572 /* If the second operand is not a constant, we can't do anything
6573 with it. */
6574 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6575 break;
6577 /* If the constant is a power of two minus one and the first operand
6578 is a logical right shift, make an extraction. */
6579 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6580 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6582 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6583 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6584 0, in_code == COMPARE);
6587 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6588 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6589 && subreg_lowpart_p (XEXP (x, 0))
6590 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6591 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6593 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6594 next_code);
6595 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6596 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6597 0, in_code == COMPARE);
6599 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6600 else if ((GET_CODE (XEXP (x, 0)) == XOR
6601 || GET_CODE (XEXP (x, 0)) == IOR)
6602 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6603 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6604 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6606 /* Apply the distributive law, and then try to make extractions. */
6607 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6608 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6609 XEXP (x, 1)),
6610 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6611 XEXP (x, 1)));
6612 new = make_compound_operation (new, in_code);
6615 /* If we are have (and (rotate X C) M) and C is larger than the number
6616 of bits in M, this is an extraction. */
6618 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6619 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6620 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6621 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6623 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6624 new = make_extraction (mode, new,
6625 (GET_MODE_BITSIZE (mode)
6626 - INTVAL (XEXP (XEXP (x, 0), 1))),
6627 NULL_RTX, i, 1, 0, in_code == COMPARE);
6630 /* On machines without logical shifts, if the operand of the AND is
6631 a logical shift and our mask turns off all the propagated sign
6632 bits, we can replace the logical shift with an arithmetic shift. */
6633 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6634 && !have_insn_for (LSHIFTRT, mode)
6635 && have_insn_for (ASHIFTRT, mode)
6636 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6637 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6638 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6639 && mode_width <= HOST_BITS_PER_WIDE_INT)
6641 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6643 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6644 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6645 SUBST (XEXP (x, 0),
6646 gen_rtx_ASHIFTRT (mode,
6647 make_compound_operation
6648 (XEXP (XEXP (x, 0), 0), next_code),
6649 XEXP (XEXP (x, 0), 1)));
6652 /* If the constant is one less than a power of two, this might be
6653 representable by an extraction even if no shift is present.
6654 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6655 we are in a COMPARE. */
6656 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6657 new = make_extraction (mode,
6658 make_compound_operation (XEXP (x, 0),
6659 next_code),
6660 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6662 /* If we are in a comparison and this is an AND with a power of two,
6663 convert this into the appropriate bit extract. */
6664 else if (in_code == COMPARE
6665 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6666 new = make_extraction (mode,
6667 make_compound_operation (XEXP (x, 0),
6668 next_code),
6669 i, NULL_RTX, 1, 1, 0, 1);
6671 break;
6673 case LSHIFTRT:
6674 /* If the sign bit is known to be zero, replace this with an
6675 arithmetic shift. */
6676 if (have_insn_for (ASHIFTRT, mode)
6677 && ! have_insn_for (LSHIFTRT, mode)
6678 && mode_width <= HOST_BITS_PER_WIDE_INT
6679 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6681 new = gen_rtx_ASHIFTRT (mode,
6682 make_compound_operation (XEXP (x, 0),
6683 next_code),
6684 XEXP (x, 1));
6685 break;
6688 /* ... fall through ... */
6690 case ASHIFTRT:
6691 lhs = XEXP (x, 0);
6692 rhs = XEXP (x, 1);
6694 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6695 this is a SIGN_EXTRACT. */
6696 if (GET_CODE (rhs) == CONST_INT
6697 && GET_CODE (lhs) == ASHIFT
6698 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6699 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6701 new = make_compound_operation (XEXP (lhs, 0), next_code);
6702 new = make_extraction (mode, new,
6703 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6704 NULL_RTX, mode_width - INTVAL (rhs),
6705 code == LSHIFTRT, 0, in_code == COMPARE);
6706 break;
6709 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6710 If so, try to merge the shifts into a SIGN_EXTEND. We could
6711 also do this for some cases of SIGN_EXTRACT, but it doesn't
6712 seem worth the effort; the case checked for occurs on Alpha. */
6714 if (!OBJECT_P (lhs)
6715 && ! (GET_CODE (lhs) == SUBREG
6716 && (OBJECT_P (SUBREG_REG (lhs))))
6717 && GET_CODE (rhs) == CONST_INT
6718 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6719 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6720 new = make_extraction (mode, make_compound_operation (new, next_code),
6721 0, NULL_RTX, mode_width - INTVAL (rhs),
6722 code == LSHIFTRT, 0, in_code == COMPARE);
6724 break;
6726 case SUBREG:
6727 /* Call ourselves recursively on the inner expression. If we are
6728 narrowing the object and it has a different RTL code from
6729 what it originally did, do this SUBREG as a force_to_mode. */
6731 tem = make_compound_operation (SUBREG_REG (x), in_code);
6732 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6733 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6734 && subreg_lowpart_p (x))
6736 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6737 NULL_RTX, 0);
6739 /* If we have something other than a SUBREG, we might have
6740 done an expansion, so rerun ourselves. */
6741 if (GET_CODE (newer) != SUBREG)
6742 newer = make_compound_operation (newer, in_code);
6744 return newer;
6747 /* If this is a paradoxical subreg, and the new code is a sign or
6748 zero extension, omit the subreg and widen the extension. If it
6749 is a regular subreg, we can still get rid of the subreg by not
6750 widening so much, or in fact removing the extension entirely. */
6751 if ((GET_CODE (tem) == SIGN_EXTEND
6752 || GET_CODE (tem) == ZERO_EXTEND)
6753 && subreg_lowpart_p (x))
6755 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6756 || (GET_MODE_SIZE (mode) >
6757 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6759 if (! SCALAR_INT_MODE_P (mode))
6760 break;
6761 tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6763 else
6764 tem = gen_lowpart (mode, XEXP (tem, 0));
6765 return tem;
6767 break;
6769 default:
6770 break;
6773 if (new)
6775 x = gen_lowpart (mode, new);
6776 code = GET_CODE (x);
6779 /* Now recursively process each operand of this operation. */
6780 fmt = GET_RTX_FORMAT (code);
6781 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6782 if (fmt[i] == 'e')
6784 new = make_compound_operation (XEXP (x, i), next_code);
6785 SUBST (XEXP (x, i), new);
6788 return x;
6791 /* Given M see if it is a value that would select a field of bits
6792 within an item, but not the entire word. Return -1 if not.
6793 Otherwise, return the starting position of the field, where 0 is the
6794 low-order bit.
6796 *PLEN is set to the length of the field. */
6798 static int
6799 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6801 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6802 int pos = exact_log2 (m & -m);
6803 int len = 0;
6805 if (pos >= 0)
6806 /* Now shift off the low-order zero bits and see if we have a
6807 power of two minus 1. */
6808 len = exact_log2 ((m >> pos) + 1);
6810 if (len <= 0)
6811 pos = -1;
6813 *plen = len;
6814 return pos;
6817 /* See if X can be simplified knowing that we will only refer to it in
6818 MODE and will only refer to those bits that are nonzero in MASK.
6819 If other bits are being computed or if masking operations are done
6820 that select a superset of the bits in MASK, they can sometimes be
6821 ignored.
6823 Return a possibly simplified expression, but always convert X to
6824 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6826 Also, if REG is nonzero and X is a register equal in value to REG,
6827 replace X with REG.
6829 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6830 are all off in X. This is used when X will be complemented, by either
6831 NOT, NEG, or XOR. */
6833 static rtx
6834 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6835 rtx reg, int just_select)
6837 enum rtx_code code = GET_CODE (x);
6838 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6839 enum machine_mode op_mode;
6840 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6841 rtx op0, op1, temp;
6843 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6844 code below will do the wrong thing since the mode of such an
6845 expression is VOIDmode.
6847 Also do nothing if X is a CLOBBER; this can happen if X was
6848 the return value from a call to gen_lowpart. */
6849 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6850 return x;
6852 /* We want to perform the operation is its present mode unless we know
6853 that the operation is valid in MODE, in which case we do the operation
6854 in MODE. */
6855 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6856 && have_insn_for (code, mode))
6857 ? mode : GET_MODE (x));
6859 /* It is not valid to do a right-shift in a narrower mode
6860 than the one it came in with. */
6861 if ((code == LSHIFTRT || code == ASHIFTRT)
6862 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6863 op_mode = GET_MODE (x);
6865 /* Truncate MASK to fit OP_MODE. */
6866 if (op_mode)
6867 mask &= GET_MODE_MASK (op_mode);
6869 /* When we have an arithmetic operation, or a shift whose count we
6870 do not know, we need to assume that all bits up to the highest-order
6871 bit in MASK will be needed. This is how we form such a mask. */
6872 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6873 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6874 else
6875 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6876 - 1);
6878 /* Determine what bits of X are guaranteed to be (non)zero. */
6879 nonzero = nonzero_bits (x, mode);
6881 /* If none of the bits in X are needed, return a zero. */
6882 if (! just_select && (nonzero & mask) == 0)
6883 x = const0_rtx;
6885 /* If X is a CONST_INT, return a new one. Do this here since the
6886 test below will fail. */
6887 if (GET_CODE (x) == CONST_INT)
6889 if (SCALAR_INT_MODE_P (mode))
6890 return gen_int_mode (INTVAL (x) & mask, mode);
6891 else
6893 x = GEN_INT (INTVAL (x) & mask);
6894 return gen_lowpart_common (mode, x);
6898 /* If X is narrower than MODE and we want all the bits in X's mode, just
6899 get X in the proper mode. */
6900 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6901 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6902 return gen_lowpart (mode, x);
6904 switch (code)
6906 case CLOBBER:
6907 /* If X is a (clobber (const_int)), return it since we know we are
6908 generating something that won't match. */
6909 return x;
6911 case USE:
6912 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6913 spanned the boundary of the MEM. If we are now masking so it is
6914 within that boundary, we don't need the USE any more. */
6915 if (! BITS_BIG_ENDIAN
6916 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6917 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6918 break;
6920 case SIGN_EXTEND:
6921 case ZERO_EXTEND:
6922 case ZERO_EXTRACT:
6923 case SIGN_EXTRACT:
6924 x = expand_compound_operation (x);
6925 if (GET_CODE (x) != code)
6926 return force_to_mode (x, mode, mask, reg, next_select);
6927 break;
6929 case REG:
6930 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6931 || rtx_equal_p (reg, get_last_value (x))))
6932 x = reg;
6933 break;
6935 case SUBREG:
6936 if (subreg_lowpart_p (x)
6937 /* We can ignore the effect of this SUBREG if it narrows the mode or
6938 if the constant masks to zero all the bits the mode doesn't
6939 have. */
6940 && ((GET_MODE_SIZE (GET_MODE (x))
6941 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6942 || (0 == (mask
6943 & GET_MODE_MASK (GET_MODE (x))
6944 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6945 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6946 break;
6948 case AND:
6949 /* If this is an AND with a constant, convert it into an AND
6950 whose constant is the AND of that constant with MASK. If it
6951 remains an AND of MASK, delete it since it is redundant. */
6953 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6955 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6956 mask & INTVAL (XEXP (x, 1)));
6958 /* If X is still an AND, see if it is an AND with a mask that
6959 is just some low-order bits. If so, and it is MASK, we don't
6960 need it. */
6962 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6963 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6964 == mask))
6965 x = XEXP (x, 0);
6967 /* If it remains an AND, try making another AND with the bits
6968 in the mode mask that aren't in MASK turned on. If the
6969 constant in the AND is wide enough, this might make a
6970 cheaper constant. */
6972 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6973 && GET_MODE_MASK (GET_MODE (x)) != mask
6974 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6976 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6977 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6978 int width = GET_MODE_BITSIZE (GET_MODE (x));
6979 rtx y;
6981 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
6982 number, sign extend it. */
6983 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6984 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6985 cval |= (HOST_WIDE_INT) -1 << width;
6987 y = simplify_gen_binary (AND, GET_MODE (x),
6988 XEXP (x, 0), GEN_INT (cval));
6989 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6990 x = y;
6993 break;
6996 goto binop;
6998 case PLUS:
6999 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7000 low-order bits (as in an alignment operation) and FOO is already
7001 aligned to that boundary, mask C1 to that boundary as well.
7002 This may eliminate that PLUS and, later, the AND. */
7005 unsigned int width = GET_MODE_BITSIZE (mode);
7006 unsigned HOST_WIDE_INT smask = mask;
7008 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7009 number, sign extend it. */
7011 if (width < HOST_BITS_PER_WIDE_INT
7012 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7013 smask |= (HOST_WIDE_INT) -1 << width;
7015 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7016 && exact_log2 (- smask) >= 0
7017 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7018 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7019 return force_to_mode (plus_constant (XEXP (x, 0),
7020 (INTVAL (XEXP (x, 1)) & smask)),
7021 mode, smask, reg, next_select);
7024 /* ... fall through ... */
7026 case MULT:
7027 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7028 most significant bit in MASK since carries from those bits will
7029 affect the bits we are interested in. */
7030 mask = fuller_mask;
7031 goto binop;
7033 case MINUS:
7034 /* If X is (minus C Y) where C's least set bit is larger than any bit
7035 in the mask, then we may replace with (neg Y). */
7036 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7037 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7038 & -INTVAL (XEXP (x, 0))))
7039 > mask))
7041 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7042 GET_MODE (x));
7043 return force_to_mode (x, mode, mask, reg, next_select);
7046 /* Similarly, if C contains every bit in the fuller_mask, then we may
7047 replace with (not Y). */
7048 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7049 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7050 == INTVAL (XEXP (x, 0))))
7052 x = simplify_gen_unary (NOT, GET_MODE (x),
7053 XEXP (x, 1), GET_MODE (x));
7054 return force_to_mode (x, mode, mask, reg, next_select);
7057 mask = fuller_mask;
7058 goto binop;
7060 case IOR:
7061 case XOR:
7062 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7063 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7064 operation which may be a bitfield extraction. Ensure that the
7065 constant we form is not wider than the mode of X. */
7067 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7068 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7069 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7070 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7071 && GET_CODE (XEXP (x, 1)) == CONST_INT
7072 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7073 + floor_log2 (INTVAL (XEXP (x, 1))))
7074 < GET_MODE_BITSIZE (GET_MODE (x)))
7075 && (INTVAL (XEXP (x, 1))
7076 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7078 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7079 << INTVAL (XEXP (XEXP (x, 0), 1)));
7080 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7081 XEXP (XEXP (x, 0), 0), temp);
7082 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7083 XEXP (XEXP (x, 0), 1));
7084 return force_to_mode (x, mode, mask, reg, next_select);
7087 binop:
7088 /* For most binary operations, just propagate into the operation and
7089 change the mode if we have an operation of that mode. */
7091 op0 = gen_lowpart (op_mode,
7092 force_to_mode (XEXP (x, 0), mode, mask,
7093 reg, next_select));
7094 op1 = gen_lowpart (op_mode,
7095 force_to_mode (XEXP (x, 1), mode, mask,
7096 reg, next_select));
7098 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7099 x = simplify_gen_binary (code, op_mode, op0, op1);
7100 break;
7102 case ASHIFT:
7103 /* For left shifts, do the same, but just for the first operand.
7104 However, we cannot do anything with shifts where we cannot
7105 guarantee that the counts are smaller than the size of the mode
7106 because such a count will have a different meaning in a
7107 wider mode. */
7109 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7110 && INTVAL (XEXP (x, 1)) >= 0
7111 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7112 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7113 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7114 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7115 break;
7117 /* If the shift count is a constant and we can do arithmetic in
7118 the mode of the shift, refine which bits we need. Otherwise, use the
7119 conservative form of the mask. */
7120 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7121 && INTVAL (XEXP (x, 1)) >= 0
7122 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7123 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7124 mask >>= INTVAL (XEXP (x, 1));
7125 else
7126 mask = fuller_mask;
7128 op0 = gen_lowpart (op_mode,
7129 force_to_mode (XEXP (x, 0), op_mode,
7130 mask, reg, next_select));
7132 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7133 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7134 break;
7136 case LSHIFTRT:
7137 /* Here we can only do something if the shift count is a constant,
7138 this shift constant is valid for the host, and we can do arithmetic
7139 in OP_MODE. */
7141 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7142 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7143 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7145 rtx inner = XEXP (x, 0);
7146 unsigned HOST_WIDE_INT inner_mask;
7148 /* Select the mask of the bits we need for the shift operand. */
7149 inner_mask = mask << INTVAL (XEXP (x, 1));
7151 /* We can only change the mode of the shift if we can do arithmetic
7152 in the mode of the shift and INNER_MASK is no wider than the
7153 width of X's mode. */
7154 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7155 op_mode = GET_MODE (x);
7157 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7159 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7160 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7163 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7164 shift and AND produces only copies of the sign bit (C2 is one less
7165 than a power of two), we can do this with just a shift. */
7167 if (GET_CODE (x) == LSHIFTRT
7168 && GET_CODE (XEXP (x, 1)) == CONST_INT
7169 /* The shift puts one of the sign bit copies in the least significant
7170 bit. */
7171 && ((INTVAL (XEXP (x, 1))
7172 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7173 >= GET_MODE_BITSIZE (GET_MODE (x)))
7174 && exact_log2 (mask + 1) >= 0
7175 /* Number of bits left after the shift must be more than the mask
7176 needs. */
7177 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7178 <= GET_MODE_BITSIZE (GET_MODE (x)))
7179 /* Must be more sign bit copies than the mask needs. */
7180 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7181 >= exact_log2 (mask + 1)))
7182 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7183 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7184 - exact_log2 (mask + 1)));
7186 goto shiftrt;
7188 case ASHIFTRT:
7189 /* If we are just looking for the sign bit, we don't need this shift at
7190 all, even if it has a variable count. */
7191 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7192 && (mask == ((unsigned HOST_WIDE_INT) 1
7193 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7194 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7196 /* If this is a shift by a constant, get a mask that contains those bits
7197 that are not copies of the sign bit. We then have two cases: If
7198 MASK only includes those bits, this can be a logical shift, which may
7199 allow simplifications. If MASK is a single-bit field not within
7200 those bits, we are requesting a copy of the sign bit and hence can
7201 shift the sign bit to the appropriate location. */
7203 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7204 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7206 int i = -1;
7208 /* If the considered data is wider than HOST_WIDE_INT, we can't
7209 represent a mask for all its bits in a single scalar.
7210 But we only care about the lower bits, so calculate these. */
7212 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7214 nonzero = ~(HOST_WIDE_INT) 0;
7216 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7217 is the number of bits a full-width mask would have set.
7218 We need only shift if these are fewer than nonzero can
7219 hold. If not, we must keep all bits set in nonzero. */
7221 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7222 < HOST_BITS_PER_WIDE_INT)
7223 nonzero >>= INTVAL (XEXP (x, 1))
7224 + HOST_BITS_PER_WIDE_INT
7225 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7227 else
7229 nonzero = GET_MODE_MASK (GET_MODE (x));
7230 nonzero >>= INTVAL (XEXP (x, 1));
7233 if ((mask & ~nonzero) == 0
7234 || (i = exact_log2 (mask)) >= 0)
7236 x = simplify_shift_const
7237 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7238 i < 0 ? INTVAL (XEXP (x, 1))
7239 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7241 if (GET_CODE (x) != ASHIFTRT)
7242 return force_to_mode (x, mode, mask, reg, next_select);
7246 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7247 even if the shift count isn't a constant. */
7248 if (mask == 1)
7249 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7250 XEXP (x, 0), XEXP (x, 1));
7252 shiftrt:
7254 /* If this is a zero- or sign-extension operation that just affects bits
7255 we don't care about, remove it. Be sure the call above returned
7256 something that is still a shift. */
7258 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7259 && GET_CODE (XEXP (x, 1)) == CONST_INT
7260 && INTVAL (XEXP (x, 1)) >= 0
7261 && (INTVAL (XEXP (x, 1))
7262 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7263 && GET_CODE (XEXP (x, 0)) == ASHIFT
7264 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7265 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7266 reg, next_select);
7268 break;
7270 case ROTATE:
7271 case ROTATERT:
7272 /* If the shift count is constant and we can do computations
7273 in the mode of X, compute where the bits we care about are.
7274 Otherwise, we can't do anything. Don't change the mode of
7275 the shift or propagate MODE into the shift, though. */
7276 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7277 && INTVAL (XEXP (x, 1)) >= 0)
7279 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7280 GET_MODE (x), GEN_INT (mask),
7281 XEXP (x, 1));
7282 if (temp && GET_CODE (temp) == CONST_INT)
7283 SUBST (XEXP (x, 0),
7284 force_to_mode (XEXP (x, 0), GET_MODE (x),
7285 INTVAL (temp), reg, next_select));
7287 break;
7289 case NEG:
7290 /* If we just want the low-order bit, the NEG isn't needed since it
7291 won't change the low-order bit. */
7292 if (mask == 1)
7293 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7295 /* We need any bits less significant than the most significant bit in
7296 MASK since carries from those bits will affect the bits we are
7297 interested in. */
7298 mask = fuller_mask;
7299 goto unop;
7301 case NOT:
7302 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7303 same as the XOR case above. Ensure that the constant we form is not
7304 wider than the mode of X. */
7306 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7307 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7308 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7309 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7310 < GET_MODE_BITSIZE (GET_MODE (x)))
7311 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7313 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7314 GET_MODE (x));
7315 temp = simplify_gen_binary (XOR, GET_MODE (x),
7316 XEXP (XEXP (x, 0), 0), temp);
7317 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7318 temp, XEXP (XEXP (x, 0), 1));
7320 return force_to_mode (x, mode, mask, reg, next_select);
7323 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7324 use the full mask inside the NOT. */
7325 mask = fuller_mask;
7327 unop:
7328 op0 = gen_lowpart (op_mode,
7329 force_to_mode (XEXP (x, 0), mode, mask,
7330 reg, next_select));
7331 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7332 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7333 break;
7335 case NE:
7336 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7337 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7338 which is equal to STORE_FLAG_VALUE. */
7339 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7340 && GET_MODE (XEXP (x, 0)) == mode
7341 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7342 && (nonzero_bits (XEXP (x, 0), mode)
7343 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7344 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7346 break;
7348 case IF_THEN_ELSE:
7349 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7350 written in a narrower mode. We play it safe and do not do so. */
7352 SUBST (XEXP (x, 1),
7353 gen_lowpart (GET_MODE (x),
7354 force_to_mode (XEXP (x, 1), mode,
7355 mask, reg, next_select)));
7356 SUBST (XEXP (x, 2),
7357 gen_lowpart (GET_MODE (x),
7358 force_to_mode (XEXP (x, 2), mode,
7359 mask, reg, next_select)));
7360 break;
7362 default:
7363 break;
7366 /* Ensure we return a value of the proper mode. */
7367 return gen_lowpart (mode, x);
7370 /* Return nonzero if X is an expression that has one of two values depending on
7371 whether some other value is zero or nonzero. In that case, we return the
7372 value that is being tested, *PTRUE is set to the value if the rtx being
7373 returned has a nonzero value, and *PFALSE is set to the other alternative.
7375 If we return zero, we set *PTRUE and *PFALSE to X. */
7377 static rtx
7378 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7380 enum machine_mode mode = GET_MODE (x);
7381 enum rtx_code code = GET_CODE (x);
7382 rtx cond0, cond1, true0, true1, false0, false1;
7383 unsigned HOST_WIDE_INT nz;
7385 /* If we are comparing a value against zero, we are done. */
7386 if ((code == NE || code == EQ)
7387 && XEXP (x, 1) == const0_rtx)
7389 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7390 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7391 return XEXP (x, 0);
7394 /* If this is a unary operation whose operand has one of two values, apply
7395 our opcode to compute those values. */
7396 else if (UNARY_P (x)
7397 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7399 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7400 *pfalse = simplify_gen_unary (code, mode, false0,
7401 GET_MODE (XEXP (x, 0)));
7402 return cond0;
7405 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7406 make can't possibly match and would suppress other optimizations. */
7407 else if (code == COMPARE)
7410 /* If this is a binary operation, see if either side has only one of two
7411 values. If either one does or if both do and they are conditional on
7412 the same value, compute the new true and false values. */
7413 else if (BINARY_P (x))
7415 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7416 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7418 if ((cond0 != 0 || cond1 != 0)
7419 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7421 /* If if_then_else_cond returned zero, then true/false are the
7422 same rtl. We must copy one of them to prevent invalid rtl
7423 sharing. */
7424 if (cond0 == 0)
7425 true0 = copy_rtx (true0);
7426 else if (cond1 == 0)
7427 true1 = copy_rtx (true1);
7429 if (COMPARISON_P (x))
7431 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7432 true0, true1);
7433 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7434 false0, false1);
7436 else
7438 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7439 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7442 return cond0 ? cond0 : cond1;
7445 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7446 operands is zero when the other is nonzero, and vice-versa,
7447 and STORE_FLAG_VALUE is 1 or -1. */
7449 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7450 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7451 || code == UMAX)
7452 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7454 rtx op0 = XEXP (XEXP (x, 0), 1);
7455 rtx op1 = XEXP (XEXP (x, 1), 1);
7457 cond0 = XEXP (XEXP (x, 0), 0);
7458 cond1 = XEXP (XEXP (x, 1), 0);
7460 if (COMPARISON_P (cond0)
7461 && COMPARISON_P (cond1)
7462 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7463 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7464 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7465 || ((swap_condition (GET_CODE (cond0))
7466 == reversed_comparison_code (cond1, NULL))
7467 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7468 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7469 && ! side_effects_p (x))
7471 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7472 *pfalse = simplify_gen_binary (MULT, mode,
7473 (code == MINUS
7474 ? simplify_gen_unary (NEG, mode,
7475 op1, mode)
7476 : op1),
7477 const_true_rtx);
7478 return cond0;
7482 /* Similarly for MULT, AND and UMIN, except that for these the result
7483 is always zero. */
7484 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7485 && (code == MULT || code == AND || code == UMIN)
7486 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7488 cond0 = XEXP (XEXP (x, 0), 0);
7489 cond1 = XEXP (XEXP (x, 1), 0);
7491 if (COMPARISON_P (cond0)
7492 && COMPARISON_P (cond1)
7493 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7494 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7495 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7496 || ((swap_condition (GET_CODE (cond0))
7497 == reversed_comparison_code (cond1, NULL))
7498 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7499 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7500 && ! side_effects_p (x))
7502 *ptrue = *pfalse = const0_rtx;
7503 return cond0;
7508 else if (code == IF_THEN_ELSE)
7510 /* If we have IF_THEN_ELSE already, extract the condition and
7511 canonicalize it if it is NE or EQ. */
7512 cond0 = XEXP (x, 0);
7513 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7514 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7515 return XEXP (cond0, 0);
7516 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7518 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7519 return XEXP (cond0, 0);
7521 else
7522 return cond0;
7525 /* If X is a SUBREG, we can narrow both the true and false values
7526 if the inner expression, if there is a condition. */
7527 else if (code == SUBREG
7528 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7529 &true0, &false0)))
7531 true0 = simplify_gen_subreg (mode, true0,
7532 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7533 false0 = simplify_gen_subreg (mode, false0,
7534 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7535 if (true0 && false0)
7537 *ptrue = true0;
7538 *pfalse = false0;
7539 return cond0;
7543 /* If X is a constant, this isn't special and will cause confusions
7544 if we treat it as such. Likewise if it is equivalent to a constant. */
7545 else if (CONSTANT_P (x)
7546 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7549 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7550 will be least confusing to the rest of the compiler. */
7551 else if (mode == BImode)
7553 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7554 return x;
7557 /* If X is known to be either 0 or -1, those are the true and
7558 false values when testing X. */
7559 else if (x == constm1_rtx || x == const0_rtx
7560 || (mode != VOIDmode
7561 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7563 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7564 return x;
7567 /* Likewise for 0 or a single bit. */
7568 else if (SCALAR_INT_MODE_P (mode)
7569 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7570 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7572 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7573 return x;
7576 /* Otherwise fail; show no condition with true and false values the same. */
7577 *ptrue = *pfalse = x;
7578 return 0;
7581 /* Return the value of expression X given the fact that condition COND
7582 is known to be true when applied to REG as its first operand and VAL
7583 as its second. X is known to not be shared and so can be modified in
7584 place.
7586 We only handle the simplest cases, and specifically those cases that
7587 arise with IF_THEN_ELSE expressions. */
7589 static rtx
7590 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7592 enum rtx_code code = GET_CODE (x);
7593 rtx temp;
7594 const char *fmt;
7595 int i, j;
7597 if (side_effects_p (x))
7598 return x;
7600 /* If either operand of the condition is a floating point value,
7601 then we have to avoid collapsing an EQ comparison. */
7602 if (cond == EQ
7603 && rtx_equal_p (x, reg)
7604 && ! FLOAT_MODE_P (GET_MODE (x))
7605 && ! FLOAT_MODE_P (GET_MODE (val)))
7606 return val;
7608 if (cond == UNEQ && rtx_equal_p (x, reg))
7609 return val;
7611 /* If X is (abs REG) and we know something about REG's relationship
7612 with zero, we may be able to simplify this. */
7614 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7615 switch (cond)
7617 case GE: case GT: case EQ:
7618 return XEXP (x, 0);
7619 case LT: case LE:
7620 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7621 XEXP (x, 0),
7622 GET_MODE (XEXP (x, 0)));
7623 default:
7624 break;
7627 /* The only other cases we handle are MIN, MAX, and comparisons if the
7628 operands are the same as REG and VAL. */
7630 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7632 if (rtx_equal_p (XEXP (x, 0), val))
7633 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7635 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7637 if (COMPARISON_P (x))
7639 if (comparison_dominates_p (cond, code))
7640 return const_true_rtx;
7642 code = reversed_comparison_code (x, NULL);
7643 if (code != UNKNOWN
7644 && comparison_dominates_p (cond, code))
7645 return const0_rtx;
7646 else
7647 return x;
7649 else if (code == SMAX || code == SMIN
7650 || code == UMIN || code == UMAX)
7652 int unsignedp = (code == UMIN || code == UMAX);
7654 /* Do not reverse the condition when it is NE or EQ.
7655 This is because we cannot conclude anything about
7656 the value of 'SMAX (x, y)' when x is not equal to y,
7657 but we can when x equals y. */
7658 if ((code == SMAX || code == UMAX)
7659 && ! (cond == EQ || cond == NE))
7660 cond = reverse_condition (cond);
7662 switch (cond)
7664 case GE: case GT:
7665 return unsignedp ? x : XEXP (x, 1);
7666 case LE: case LT:
7667 return unsignedp ? x : XEXP (x, 0);
7668 case GEU: case GTU:
7669 return unsignedp ? XEXP (x, 1) : x;
7670 case LEU: case LTU:
7671 return unsignedp ? XEXP (x, 0) : x;
7672 default:
7673 break;
7678 else if (code == SUBREG)
7680 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7681 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7683 if (SUBREG_REG (x) != r)
7685 /* We must simplify subreg here, before we lose track of the
7686 original inner_mode. */
7687 new = simplify_subreg (GET_MODE (x), r,
7688 inner_mode, SUBREG_BYTE (x));
7689 if (new)
7690 return new;
7691 else
7692 SUBST (SUBREG_REG (x), r);
7695 return x;
7697 /* We don't have to handle SIGN_EXTEND here, because even in the
7698 case of replacing something with a modeless CONST_INT, a
7699 CONST_INT is already (supposed to be) a valid sign extension for
7700 its narrower mode, which implies it's already properly
7701 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7702 story is different. */
7703 else if (code == ZERO_EXTEND)
7705 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7706 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7708 if (XEXP (x, 0) != r)
7710 /* We must simplify the zero_extend here, before we lose
7711 track of the original inner_mode. */
7712 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7713 r, inner_mode);
7714 if (new)
7715 return new;
7716 else
7717 SUBST (XEXP (x, 0), r);
7720 return x;
7723 fmt = GET_RTX_FORMAT (code);
7724 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7726 if (fmt[i] == 'e')
7727 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7728 else if (fmt[i] == 'E')
7729 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7730 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7731 cond, reg, val));
7734 return x;
7737 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7738 assignment as a field assignment. */
7740 static int
7741 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7743 if (x == y || rtx_equal_p (x, y))
7744 return 1;
7746 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7747 return 0;
7749 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7750 Note that all SUBREGs of MEM are paradoxical; otherwise they
7751 would have been rewritten. */
7752 if (MEM_P (x) && GET_CODE (y) == SUBREG
7753 && MEM_P (SUBREG_REG (y))
7754 && rtx_equal_p (SUBREG_REG (y),
7755 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7756 return 1;
7758 if (MEM_P (y) && GET_CODE (x) == SUBREG
7759 && MEM_P (SUBREG_REG (x))
7760 && rtx_equal_p (SUBREG_REG (x),
7761 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7762 return 1;
7764 /* We used to see if get_last_value of X and Y were the same but that's
7765 not correct. In one direction, we'll cause the assignment to have
7766 the wrong destination and in the case, we'll import a register into this
7767 insn that might have already have been dead. So fail if none of the
7768 above cases are true. */
7769 return 0;
7772 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7773 Return that assignment if so.
7775 We only handle the most common cases. */
7777 static rtx
7778 make_field_assignment (rtx x)
7780 rtx dest = SET_DEST (x);
7781 rtx src = SET_SRC (x);
7782 rtx assign;
7783 rtx rhs, lhs;
7784 HOST_WIDE_INT c1;
7785 HOST_WIDE_INT pos;
7786 unsigned HOST_WIDE_INT len;
7787 rtx other;
7788 enum machine_mode mode;
7790 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7791 a clear of a one-bit field. We will have changed it to
7792 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7793 for a SUBREG. */
7795 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7796 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7797 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7798 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7800 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7801 1, 1, 1, 0);
7802 if (assign != 0)
7803 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7804 return x;
7807 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7808 && subreg_lowpart_p (XEXP (src, 0))
7809 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7810 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7811 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7812 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7813 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7814 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7816 assign = make_extraction (VOIDmode, dest, 0,
7817 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7818 1, 1, 1, 0);
7819 if (assign != 0)
7820 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7821 return x;
7824 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7825 one-bit field. */
7826 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7827 && XEXP (XEXP (src, 0), 0) == const1_rtx
7828 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7830 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7831 1, 1, 1, 0);
7832 if (assign != 0)
7833 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7834 return x;
7837 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
7838 SRC is an AND with all bits of that field set, then we can discard
7839 the AND. */
7840 if (GET_CODE (dest) == ZERO_EXTRACT
7841 && GET_CODE (XEXP (dest, 1)) == CONST_INT
7842 && GET_CODE (src) == AND
7843 && GET_CODE (XEXP (src, 1)) == CONST_INT)
7845 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
7846 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
7847 unsigned HOST_WIDE_INT ze_mask;
7849 if (width >= HOST_BITS_PER_WIDE_INT)
7850 ze_mask = -1;
7851 else
7852 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
7854 /* Complete overlap. We can remove the source AND. */
7855 if ((and_mask & ze_mask) == ze_mask)
7856 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
7858 /* Partial overlap. We can reduce the source AND. */
7859 if ((and_mask & ze_mask) != and_mask)
7861 mode = GET_MODE (src);
7862 src = gen_rtx_AND (mode, XEXP (src, 0),
7863 gen_int_mode (and_mask & ze_mask, mode));
7864 return gen_rtx_SET (VOIDmode, dest, src);
7868 /* The other case we handle is assignments into a constant-position
7869 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7870 a mask that has all one bits except for a group of zero bits and
7871 OTHER is known to have zeros where C1 has ones, this is such an
7872 assignment. Compute the position and length from C1. Shift OTHER
7873 to the appropriate position, force it to the required mode, and
7874 make the extraction. Check for the AND in both operands. */
7876 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7877 return x;
7879 rhs = expand_compound_operation (XEXP (src, 0));
7880 lhs = expand_compound_operation (XEXP (src, 1));
7882 if (GET_CODE (rhs) == AND
7883 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7884 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7885 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7886 else if (GET_CODE (lhs) == AND
7887 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7888 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7889 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7890 else
7891 return x;
7893 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7894 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7895 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7896 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7897 return x;
7899 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7900 if (assign == 0)
7901 return x;
7903 /* The mode to use for the source is the mode of the assignment, or of
7904 what is inside a possible STRICT_LOW_PART. */
7905 mode = (GET_CODE (assign) == STRICT_LOW_PART
7906 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7908 /* Shift OTHER right POS places and make it the source, restricting it
7909 to the proper length and mode. */
7911 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7912 GET_MODE (src), other, pos),
7913 mode,
7914 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7915 ? ~(unsigned HOST_WIDE_INT) 0
7916 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7917 dest, 0);
7919 /* If SRC is masked by an AND that does not make a difference in
7920 the value being stored, strip it. */
7921 if (GET_CODE (assign) == ZERO_EXTRACT
7922 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7923 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7924 && GET_CODE (src) == AND
7925 && GET_CODE (XEXP (src, 1)) == CONST_INT
7926 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7927 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7928 src = XEXP (src, 0);
7930 return gen_rtx_SET (VOIDmode, assign, src);
7933 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7934 if so. */
7936 static rtx
7937 apply_distributive_law (rtx x)
7939 enum rtx_code code = GET_CODE (x);
7940 enum rtx_code inner_code;
7941 rtx lhs, rhs, other;
7942 rtx tem;
7944 /* Distributivity is not true for floating point as it can change the
7945 value. So we don't do it unless -funsafe-math-optimizations. */
7946 if (FLOAT_MODE_P (GET_MODE (x))
7947 && ! flag_unsafe_math_optimizations)
7948 return x;
7950 /* The outer operation can only be one of the following: */
7951 if (code != IOR && code != AND && code != XOR
7952 && code != PLUS && code != MINUS)
7953 return x;
7955 lhs = XEXP (x, 0);
7956 rhs = XEXP (x, 1);
7958 /* If either operand is a primitive we can't do anything, so get out
7959 fast. */
7960 if (OBJECT_P (lhs) || OBJECT_P (rhs))
7961 return x;
7963 lhs = expand_compound_operation (lhs);
7964 rhs = expand_compound_operation (rhs);
7965 inner_code = GET_CODE (lhs);
7966 if (inner_code != GET_CODE (rhs))
7967 return x;
7969 /* See if the inner and outer operations distribute. */
7970 switch (inner_code)
7972 case LSHIFTRT:
7973 case ASHIFTRT:
7974 case AND:
7975 case IOR:
7976 /* These all distribute except over PLUS. */
7977 if (code == PLUS || code == MINUS)
7978 return x;
7979 break;
7981 case MULT:
7982 if (code != PLUS && code != MINUS)
7983 return x;
7984 break;
7986 case ASHIFT:
7987 /* This is also a multiply, so it distributes over everything. */
7988 break;
7990 case SUBREG:
7991 /* Non-paradoxical SUBREGs distributes over all operations, provided
7992 the inner modes and byte offsets are the same, this is an extraction
7993 of a low-order part, we don't convert an fp operation to int or
7994 vice versa, and we would not be converting a single-word
7995 operation into a multi-word operation. The latter test is not
7996 required, but it prevents generating unneeded multi-word operations.
7997 Some of the previous tests are redundant given the latter test, but
7998 are retained because they are required for correctness.
8000 We produce the result slightly differently in this case. */
8002 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8003 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8004 || ! subreg_lowpart_p (lhs)
8005 || (GET_MODE_CLASS (GET_MODE (lhs))
8006 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8007 || (GET_MODE_SIZE (GET_MODE (lhs))
8008 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8009 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
8010 return x;
8012 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8013 SUBREG_REG (lhs), SUBREG_REG (rhs));
8014 return gen_lowpart (GET_MODE (x), tem);
8016 default:
8017 return x;
8020 /* Set LHS and RHS to the inner operands (A and B in the example
8021 above) and set OTHER to the common operand (C in the example).
8022 There is only one way to do this unless the inner operation is
8023 commutative. */
8024 if (COMMUTATIVE_ARITH_P (lhs)
8025 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8026 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8027 else if (COMMUTATIVE_ARITH_P (lhs)
8028 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8029 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8030 else if (COMMUTATIVE_ARITH_P (lhs)
8031 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8032 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8033 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8034 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8035 else
8036 return x;
8038 /* Form the new inner operation, seeing if it simplifies first. */
8039 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8041 /* There is one exception to the general way of distributing:
8042 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8043 if (code == XOR && inner_code == IOR)
8045 inner_code = AND;
8046 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8049 /* We may be able to continuing distributing the result, so call
8050 ourselves recursively on the inner operation before forming the
8051 outer operation, which we return. */
8052 return simplify_gen_binary (inner_code, GET_MODE (x),
8053 apply_distributive_law (tem), other);
8056 /* See if X is of the form (* (+ A B) C), and if so convert to
8057 (+ (* A C) (* B C)) and try to simplify.
8059 Most of the time, this results in no change. However, if some of
8060 the operands are the same or inverses of each other, simplifications
8061 will result.
8063 For example, (and (ior A B) (not B)) can occur as the result of
8064 expanding a bit field assignment. When we apply the distributive
8065 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8066 which then simplifies to (and (A (not B))).
8068 Note that no checks happen on the validity of applying the inverse
8069 distributive law. This is pointless since we can do it in the
8070 few places where this routine is called.
8072 N is the index of the term that is decomposed (the arithmetic operation,
8073 i.e. (+ A B) in the first example above). !N is the index of the term that
8074 is distributed, i.e. of C in the first example above. */
8075 static rtx
8076 distribute_and_simplify_rtx (rtx x, int n)
8078 enum machine_mode mode;
8079 enum rtx_code outer_code, inner_code;
8080 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8082 decomposed = XEXP (x, n);
8083 if (!ARITHMETIC_P (decomposed))
8084 return NULL_RTX;
8086 mode = GET_MODE (x);
8087 outer_code = GET_CODE (x);
8088 distributed = XEXP (x, !n);
8090 inner_code = GET_CODE (decomposed);
8091 inner_op0 = XEXP (decomposed, 0);
8092 inner_op1 = XEXP (decomposed, 1);
8094 /* Special case (and (xor B C) (not A)), which is equivalent to
8095 (xor (ior A B) (ior A C)) */
8096 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8098 distributed = XEXP (distributed, 0);
8099 outer_code = IOR;
8102 if (n == 0)
8104 /* Distribute the second term. */
8105 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8106 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8108 else
8110 /* Distribute the first term. */
8111 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8112 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8115 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8116 new_op0, new_op1));
8117 if (GET_CODE (tmp) != outer_code
8118 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8119 return tmp;
8121 return NULL_RTX;
8124 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8125 in MODE.
8127 Return an equivalent form, if different from X. Otherwise, return X. If
8128 X is zero, we are to always construct the equivalent form. */
8130 static rtx
8131 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8132 unsigned HOST_WIDE_INT constop)
8134 unsigned HOST_WIDE_INT nonzero;
8135 int i;
8137 /* Simplify VAROP knowing that we will be only looking at some of the
8138 bits in it.
8140 Note by passing in CONSTOP, we guarantee that the bits not set in
8141 CONSTOP are not significant and will never be examined. We must
8142 ensure that is the case by explicitly masking out those bits
8143 before returning. */
8144 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
8146 /* If VAROP is a CLOBBER, we will fail so return it. */
8147 if (GET_CODE (varop) == CLOBBER)
8148 return varop;
8150 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8151 to VAROP and return the new constant. */
8152 if (GET_CODE (varop) == CONST_INT)
8153 return gen_int_mode (INTVAL (varop) & constop, mode);
8155 /* See what bits may be nonzero in VAROP. Unlike the general case of
8156 a call to nonzero_bits, here we don't care about bits outside
8157 MODE. */
8159 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8161 /* Turn off all bits in the constant that are known to already be zero.
8162 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8163 which is tested below. */
8165 constop &= nonzero;
8167 /* If we don't have any bits left, return zero. */
8168 if (constop == 0)
8169 return const0_rtx;
8171 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8172 a power of two, we can replace this with an ASHIFT. */
8173 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8174 && (i = exact_log2 (constop)) >= 0)
8175 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8177 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8178 or XOR, then try to apply the distributive law. This may eliminate
8179 operations if either branch can be simplified because of the AND.
8180 It may also make some cases more complex, but those cases probably
8181 won't match a pattern either with or without this. */
8183 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8184 return
8185 gen_lowpart
8186 (mode,
8187 apply_distributive_law
8188 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8189 simplify_and_const_int (NULL_RTX,
8190 GET_MODE (varop),
8191 XEXP (varop, 0),
8192 constop),
8193 simplify_and_const_int (NULL_RTX,
8194 GET_MODE (varop),
8195 XEXP (varop, 1),
8196 constop))));
8198 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8199 the AND and see if one of the operands simplifies to zero. If so, we
8200 may eliminate it. */
8202 if (GET_CODE (varop) == PLUS
8203 && exact_log2 (constop + 1) >= 0)
8205 rtx o0, o1;
8207 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8208 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8209 if (o0 == const0_rtx)
8210 return o1;
8211 if (o1 == const0_rtx)
8212 return o0;
8215 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
8216 if we already had one (just check for the simplest cases). */
8217 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8218 && GET_MODE (XEXP (x, 0)) == mode
8219 && SUBREG_REG (XEXP (x, 0)) == varop)
8220 varop = XEXP (x, 0);
8221 else
8222 varop = gen_lowpart (mode, varop);
8224 /* If we can't make the SUBREG, try to return what we were given. */
8225 if (GET_CODE (varop) == CLOBBER)
8226 return x ? x : varop;
8228 /* If we are only masking insignificant bits, return VAROP. */
8229 if (constop == nonzero)
8230 x = varop;
8231 else
8233 /* Otherwise, return an AND. */
8234 constop = trunc_int_for_mode (constop, mode);
8235 /* See how much, if any, of X we can use. */
8236 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8237 x = simplify_gen_binary (AND, mode, varop, GEN_INT (constop));
8239 else
8241 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8242 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8243 SUBST (XEXP (x, 1), GEN_INT (constop));
8245 SUBST (XEXP (x, 0), varop);
8249 return x;
8252 /* Given a REG, X, compute which bits in X can be nonzero.
8253 We don't care about bits outside of those defined in MODE.
8255 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8256 a shift, AND, or zero_extract, we can do better. */
8258 static rtx
8259 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8260 rtx known_x ATTRIBUTE_UNUSED,
8261 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8262 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8263 unsigned HOST_WIDE_INT *nonzero)
8265 rtx tem;
8267 /* If X is a register whose nonzero bits value is current, use it.
8268 Otherwise, if X is a register whose value we can find, use that
8269 value. Otherwise, use the previously-computed global nonzero bits
8270 for this register. */
8272 if (reg_stat[REGNO (x)].last_set_value != 0
8273 && (reg_stat[REGNO (x)].last_set_mode == mode
8274 || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8275 && GET_MODE_CLASS (mode) == MODE_INT))
8276 && (reg_stat[REGNO (x)].last_set_label == label_tick
8277 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8278 && REG_N_SETS (REGNO (x)) == 1
8279 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8280 REGNO (x))))
8281 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8283 *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8284 return NULL;
8287 tem = get_last_value (x);
8289 if (tem)
8291 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8292 /* If X is narrower than MODE and TEM is a non-negative
8293 constant that would appear negative in the mode of X,
8294 sign-extend it for use in reg_nonzero_bits because some
8295 machines (maybe most) will actually do the sign-extension
8296 and this is the conservative approach.
8298 ??? For 2.5, try to tighten up the MD files in this regard
8299 instead of this kludge. */
8301 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8302 && GET_CODE (tem) == CONST_INT
8303 && INTVAL (tem) > 0
8304 && 0 != (INTVAL (tem)
8305 & ((HOST_WIDE_INT) 1
8306 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8307 tem = GEN_INT (INTVAL (tem)
8308 | ((HOST_WIDE_INT) (-1)
8309 << GET_MODE_BITSIZE (GET_MODE (x))));
8310 #endif
8311 return tem;
8313 else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8315 unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8317 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8318 /* We don't know anything about the upper bits. */
8319 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8320 *nonzero &= mask;
8323 return NULL;
8326 /* Return the number of bits at the high-order end of X that are known to
8327 be equal to the sign bit. X will be used in mode MODE; if MODE is
8328 VOIDmode, X will be used in its own mode. The returned value will always
8329 be between 1 and the number of bits in MODE. */
8331 static rtx
8332 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8333 rtx known_x ATTRIBUTE_UNUSED,
8334 enum machine_mode known_mode
8335 ATTRIBUTE_UNUSED,
8336 unsigned int known_ret ATTRIBUTE_UNUSED,
8337 unsigned int *result)
8339 rtx tem;
8341 if (reg_stat[REGNO (x)].last_set_value != 0
8342 && reg_stat[REGNO (x)].last_set_mode == mode
8343 && (reg_stat[REGNO (x)].last_set_label == label_tick
8344 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8345 && REG_N_SETS (REGNO (x)) == 1
8346 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8347 REGNO (x))))
8348 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8350 *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8351 return NULL;
8354 tem = get_last_value (x);
8355 if (tem != 0)
8356 return tem;
8358 if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8359 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8360 *result = reg_stat[REGNO (x)].sign_bit_copies;
8362 return NULL;
8365 /* Return the number of "extended" bits there are in X, when interpreted
8366 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8367 unsigned quantities, this is the number of high-order zero bits.
8368 For signed quantities, this is the number of copies of the sign bit
8369 minus 1. In both case, this function returns the number of "spare"
8370 bits. For example, if two quantities for which this function returns
8371 at least 1 are added, the addition is known not to overflow.
8373 This function will always return 0 unless called during combine, which
8374 implies that it must be called from a define_split. */
8376 unsigned int
8377 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8379 if (nonzero_sign_valid == 0)
8380 return 0;
8382 return (unsignedp
8383 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8384 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8385 - floor_log2 (nonzero_bits (x, mode)))
8386 : 0)
8387 : num_sign_bit_copies (x, mode) - 1);
8390 /* This function is called from `simplify_shift_const' to merge two
8391 outer operations. Specifically, we have already found that we need
8392 to perform operation *POP0 with constant *PCONST0 at the outermost
8393 position. We would now like to also perform OP1 with constant CONST1
8394 (with *POP0 being done last).
8396 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8397 the resulting operation. *PCOMP_P is set to 1 if we would need to
8398 complement the innermost operand, otherwise it is unchanged.
8400 MODE is the mode in which the operation will be done. No bits outside
8401 the width of this mode matter. It is assumed that the width of this mode
8402 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8404 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8405 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8406 result is simply *PCONST0.
8408 If the resulting operation cannot be expressed as one operation, we
8409 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8411 static int
8412 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)
8414 enum rtx_code op0 = *pop0;
8415 HOST_WIDE_INT const0 = *pconst0;
8417 const0 &= GET_MODE_MASK (mode);
8418 const1 &= GET_MODE_MASK (mode);
8420 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8421 if (op0 == AND)
8422 const1 &= const0;
8424 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8425 if OP0 is SET. */
8427 if (op1 == UNKNOWN || op0 == SET)
8428 return 1;
8430 else if (op0 == UNKNOWN)
8431 op0 = op1, const0 = const1;
8433 else if (op0 == op1)
8435 switch (op0)
8437 case AND:
8438 const0 &= const1;
8439 break;
8440 case IOR:
8441 const0 |= const1;
8442 break;
8443 case XOR:
8444 const0 ^= const1;
8445 break;
8446 case PLUS:
8447 const0 += const1;
8448 break;
8449 case NEG:
8450 op0 = UNKNOWN;
8451 break;
8452 default:
8453 break;
8457 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8458 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8459 return 0;
8461 /* If the two constants aren't the same, we can't do anything. The
8462 remaining six cases can all be done. */
8463 else if (const0 != const1)
8464 return 0;
8466 else
8467 switch (op0)
8469 case IOR:
8470 if (op1 == AND)
8471 /* (a & b) | b == b */
8472 op0 = SET;
8473 else /* op1 == XOR */
8474 /* (a ^ b) | b == a | b */
8476 break;
8478 case XOR:
8479 if (op1 == AND)
8480 /* (a & b) ^ b == (~a) & b */
8481 op0 = AND, *pcomp_p = 1;
8482 else /* op1 == IOR */
8483 /* (a | b) ^ b == a & ~b */
8484 op0 = AND, const0 = ~const0;
8485 break;
8487 case AND:
8488 if (op1 == IOR)
8489 /* (a | b) & b == b */
8490 op0 = SET;
8491 else /* op1 == XOR */
8492 /* (a ^ b) & b) == (~a) & b */
8493 *pcomp_p = 1;
8494 break;
8495 default:
8496 break;
8499 /* Check for NO-OP cases. */
8500 const0 &= GET_MODE_MASK (mode);
8501 if (const0 == 0
8502 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8503 op0 = UNKNOWN;
8504 else if (const0 == 0 && op0 == AND)
8505 op0 = SET;
8506 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8507 && op0 == AND)
8508 op0 = UNKNOWN;
8510 /* ??? Slightly redundant with the above mask, but not entirely.
8511 Moving this above means we'd have to sign-extend the mode mask
8512 for the final test. */
8513 const0 = trunc_int_for_mode (const0, mode);
8515 *pop0 = op0;
8516 *pconst0 = const0;
8518 return 1;
8521 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8522 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
8523 that we started with.
8525 The shift is normally computed in the widest mode we find in VAROP, as
8526 long as it isn't a different number of words than RESULT_MODE. Exceptions
8527 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8529 static rtx
8530 simplify_shift_const (rtx x, enum rtx_code code,
8531 enum machine_mode result_mode, rtx varop,
8532 int orig_count)
8534 enum rtx_code orig_code = code;
8535 unsigned int count;
8536 int signed_count;
8537 enum machine_mode mode = result_mode;
8538 enum machine_mode shift_mode, tmode;
8539 unsigned int mode_words
8540 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8541 /* We form (outer_op (code varop count) (outer_const)). */
8542 enum rtx_code outer_op = UNKNOWN;
8543 HOST_WIDE_INT outer_const = 0;
8544 rtx const_rtx;
8545 int complement_p = 0;
8546 rtx new;
8548 /* Make sure and truncate the "natural" shift on the way in. We don't
8549 want to do this inside the loop as it makes it more difficult to
8550 combine shifts. */
8551 if (SHIFT_COUNT_TRUNCATED)
8552 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8554 /* If we were given an invalid count, don't do anything except exactly
8555 what was requested. */
8557 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8559 if (x)
8560 return x;
8562 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
8565 count = orig_count;
8567 /* Unless one of the branches of the `if' in this loop does a `continue',
8568 we will `break' the loop after the `if'. */
8570 while (count != 0)
8572 /* If we have an operand of (clobber (const_int 0)), just return that
8573 value. */
8574 if (GET_CODE (varop) == CLOBBER)
8575 return varop;
8577 /* If we discovered we had to complement VAROP, leave. Making a NOT
8578 here would cause an infinite loop. */
8579 if (complement_p)
8580 break;
8582 /* Convert ROTATERT to ROTATE. */
8583 if (code == ROTATERT)
8585 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8586 code = ROTATE;
8587 if (VECTOR_MODE_P (result_mode))
8588 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8589 else
8590 count = bitsize - count;
8593 /* We need to determine what mode we will do the shift in. If the
8594 shift is a right shift or a ROTATE, we must always do it in the mode
8595 it was originally done in. Otherwise, we can do it in MODE, the
8596 widest mode encountered. */
8597 shift_mode
8598 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8599 ? result_mode : mode);
8601 /* Handle cases where the count is greater than the size of the mode
8602 minus 1. For ASHIFT, use the size minus one as the count (this can
8603 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8604 take the count modulo the size. For other shifts, the result is
8605 zero.
8607 Since these shifts are being produced by the compiler by combining
8608 multiple operations, each of which are defined, we know what the
8609 result is supposed to be. */
8611 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
8613 if (code == ASHIFTRT)
8614 count = GET_MODE_BITSIZE (shift_mode) - 1;
8615 else if (code == ROTATE || code == ROTATERT)
8616 count %= GET_MODE_BITSIZE (shift_mode);
8617 else
8619 /* We can't simply return zero because there may be an
8620 outer op. */
8621 varop = const0_rtx;
8622 count = 0;
8623 break;
8627 /* An arithmetic right shift of a quantity known to be -1 or 0
8628 is a no-op. */
8629 if (code == ASHIFTRT
8630 && (num_sign_bit_copies (varop, shift_mode)
8631 == GET_MODE_BITSIZE (shift_mode)))
8633 count = 0;
8634 break;
8637 /* If we are doing an arithmetic right shift and discarding all but
8638 the sign bit copies, this is equivalent to doing a shift by the
8639 bitsize minus one. Convert it into that shift because it will often
8640 allow other simplifications. */
8642 if (code == ASHIFTRT
8643 && (count + num_sign_bit_copies (varop, shift_mode)
8644 >= GET_MODE_BITSIZE (shift_mode)))
8645 count = GET_MODE_BITSIZE (shift_mode) - 1;
8647 /* We simplify the tests below and elsewhere by converting
8648 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8649 `make_compound_operation' will convert it to an ASHIFTRT for
8650 those machines (such as VAX) that don't have an LSHIFTRT. */
8651 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8652 && code == ASHIFTRT
8653 && ((nonzero_bits (varop, shift_mode)
8654 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8655 == 0))
8656 code = LSHIFTRT;
8658 if (code == LSHIFTRT
8659 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8660 && !(nonzero_bits (varop, shift_mode) >> count))
8661 varop = const0_rtx;
8662 if (code == ASHIFT
8663 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8664 && !((nonzero_bits (varop, shift_mode) << count)
8665 & GET_MODE_MASK (shift_mode)))
8666 varop = const0_rtx;
8668 switch (GET_CODE (varop))
8670 case SIGN_EXTEND:
8671 case ZERO_EXTEND:
8672 case SIGN_EXTRACT:
8673 case ZERO_EXTRACT:
8674 new = expand_compound_operation (varop);
8675 if (new != varop)
8677 varop = new;
8678 continue;
8680 break;
8682 case MEM:
8683 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8684 minus the width of a smaller mode, we can do this with a
8685 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8686 if ((code == ASHIFTRT || code == LSHIFTRT)
8687 && ! mode_dependent_address_p (XEXP (varop, 0))
8688 && ! MEM_VOLATILE_P (varop)
8689 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8690 MODE_INT, 1)) != BLKmode)
8692 new = adjust_address_nv (varop, tmode,
8693 BYTES_BIG_ENDIAN ? 0
8694 : count / BITS_PER_UNIT);
8696 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8697 : ZERO_EXTEND, mode, new);
8698 count = 0;
8699 continue;
8701 break;
8703 case USE:
8704 /* Similar to the case above, except that we can only do this if
8705 the resulting mode is the same as that of the underlying
8706 MEM and adjust the address depending on the *bits* endianness
8707 because of the way that bit-field extract insns are defined. */
8708 if ((code == ASHIFTRT || code == LSHIFTRT)
8709 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8710 MODE_INT, 1)) != BLKmode
8711 && tmode == GET_MODE (XEXP (varop, 0)))
8713 if (BITS_BIG_ENDIAN)
8714 new = XEXP (varop, 0);
8715 else
8717 new = copy_rtx (XEXP (varop, 0));
8718 SUBST (XEXP (new, 0),
8719 plus_constant (XEXP (new, 0),
8720 count / BITS_PER_UNIT));
8723 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8724 : ZERO_EXTEND, mode, new);
8725 count = 0;
8726 continue;
8728 break;
8730 case SUBREG:
8731 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8732 the same number of words as what we've seen so far. Then store
8733 the widest mode in MODE. */
8734 if (subreg_lowpart_p (varop)
8735 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8736 > GET_MODE_SIZE (GET_MODE (varop)))
8737 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8738 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8739 == mode_words)
8741 varop = SUBREG_REG (varop);
8742 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8743 mode = GET_MODE (varop);
8744 continue;
8746 break;
8748 case MULT:
8749 /* Some machines use MULT instead of ASHIFT because MULT
8750 is cheaper. But it is still better on those machines to
8751 merge two shifts into one. */
8752 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8753 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8755 varop
8756 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
8757 XEXP (varop, 0),
8758 GEN_INT (exact_log2 (
8759 INTVAL (XEXP (varop, 1)))));
8760 continue;
8762 break;
8764 case UDIV:
8765 /* Similar, for when divides are cheaper. */
8766 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8767 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8769 varop
8770 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
8771 XEXP (varop, 0),
8772 GEN_INT (exact_log2 (
8773 INTVAL (XEXP (varop, 1)))));
8774 continue;
8776 break;
8778 case ASHIFTRT:
8779 /* If we are extracting just the sign bit of an arithmetic
8780 right shift, that shift is not needed. However, the sign
8781 bit of a wider mode may be different from what would be
8782 interpreted as the sign bit in a narrower mode, so, if
8783 the result is narrower, don't discard the shift. */
8784 if (code == LSHIFTRT
8785 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8786 && (GET_MODE_BITSIZE (result_mode)
8787 >= GET_MODE_BITSIZE (GET_MODE (varop))))
8789 varop = XEXP (varop, 0);
8790 continue;
8793 /* ... fall through ... */
8795 case LSHIFTRT:
8796 case ASHIFT:
8797 case ROTATE:
8798 /* Here we have two nested shifts. The result is usually the
8799 AND of a new shift with a mask. We compute the result below. */
8800 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8801 && INTVAL (XEXP (varop, 1)) >= 0
8802 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8803 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8804 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8806 enum rtx_code first_code = GET_CODE (varop);
8807 unsigned int first_count = INTVAL (XEXP (varop, 1));
8808 unsigned HOST_WIDE_INT mask;
8809 rtx mask_rtx;
8811 /* We have one common special case. We can't do any merging if
8812 the inner code is an ASHIFTRT of a smaller mode. However, if
8813 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8814 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8815 we can convert it to
8816 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8817 This simplifies certain SIGN_EXTEND operations. */
8818 if (code == ASHIFT && first_code == ASHIFTRT
8819 && count == (unsigned int)
8820 (GET_MODE_BITSIZE (result_mode)
8821 - GET_MODE_BITSIZE (GET_MODE (varop))))
8823 /* C3 has the low-order C1 bits zero. */
8825 mask = (GET_MODE_MASK (mode)
8826 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8828 varop = simplify_and_const_int (NULL_RTX, result_mode,
8829 XEXP (varop, 0), mask);
8830 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8831 varop, count);
8832 count = first_count;
8833 code = ASHIFTRT;
8834 continue;
8837 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8838 than C1 high-order bits equal to the sign bit, we can convert
8839 this to either an ASHIFT or an ASHIFTRT depending on the
8840 two counts.
8842 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8844 if (code == ASHIFTRT && first_code == ASHIFT
8845 && GET_MODE (varop) == shift_mode
8846 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8847 > first_count))
8849 varop = XEXP (varop, 0);
8851 signed_count = count - first_count;
8852 if (signed_count < 0)
8853 count = -signed_count, code = ASHIFT;
8854 else
8855 count = signed_count;
8857 continue;
8860 /* There are some cases we can't do. If CODE is ASHIFTRT,
8861 we can only do this if FIRST_CODE is also ASHIFTRT.
8863 We can't do the case when CODE is ROTATE and FIRST_CODE is
8864 ASHIFTRT.
8866 If the mode of this shift is not the mode of the outer shift,
8867 we can't do this if either shift is a right shift or ROTATE.
8869 Finally, we can't do any of these if the mode is too wide
8870 unless the codes are the same.
8872 Handle the case where the shift codes are the same
8873 first. */
8875 if (code == first_code)
8877 if (GET_MODE (varop) != result_mode
8878 && (code == ASHIFTRT || code == LSHIFTRT
8879 || code == ROTATE))
8880 break;
8882 count += first_count;
8883 varop = XEXP (varop, 0);
8884 continue;
8887 if (code == ASHIFTRT
8888 || (code == ROTATE && first_code == ASHIFTRT)
8889 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8890 || (GET_MODE (varop) != result_mode
8891 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8892 || first_code == ROTATE
8893 || code == ROTATE)))
8894 break;
8896 /* To compute the mask to apply after the shift, shift the
8897 nonzero bits of the inner shift the same way the
8898 outer shift will. */
8900 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8902 mask_rtx
8903 = simplify_binary_operation (code, result_mode, mask_rtx,
8904 GEN_INT (count));
8906 /* Give up if we can't compute an outer operation to use. */
8907 if (mask_rtx == 0
8908 || GET_CODE (mask_rtx) != CONST_INT
8909 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8910 INTVAL (mask_rtx),
8911 result_mode, &complement_p))
8912 break;
8914 /* If the shifts are in the same direction, we add the
8915 counts. Otherwise, we subtract them. */
8916 signed_count = count;
8917 if ((code == ASHIFTRT || code == LSHIFTRT)
8918 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8919 signed_count += first_count;
8920 else
8921 signed_count -= first_count;
8923 /* If COUNT is positive, the new shift is usually CODE,
8924 except for the two exceptions below, in which case it is
8925 FIRST_CODE. If the count is negative, FIRST_CODE should
8926 always be used */
8927 if (signed_count > 0
8928 && ((first_code == ROTATE && code == ASHIFT)
8929 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8930 code = first_code, count = signed_count;
8931 else if (signed_count < 0)
8932 code = first_code, count = -signed_count;
8933 else
8934 count = signed_count;
8936 varop = XEXP (varop, 0);
8937 continue;
8940 /* If we have (A << B << C) for any shift, we can convert this to
8941 (A << C << B). This wins if A is a constant. Only try this if
8942 B is not a constant. */
8944 else if (GET_CODE (varop) == code
8945 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8946 && 0 != (new
8947 = simplify_binary_operation (code, mode,
8948 XEXP (varop, 0),
8949 GEN_INT (count))))
8951 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
8952 count = 0;
8953 continue;
8955 break;
8957 case NOT:
8958 /* Make this fit the case below. */
8959 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
8960 GEN_INT (GET_MODE_MASK (mode)));
8961 continue;
8963 case IOR:
8964 case AND:
8965 case XOR:
8966 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8967 with C the size of VAROP - 1 and the shift is logical if
8968 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8969 we have an (le X 0) operation. If we have an arithmetic shift
8970 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8971 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8973 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8974 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8975 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8976 && (code == LSHIFTRT || code == ASHIFTRT)
8977 && count == (unsigned int)
8978 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8979 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8981 count = 0;
8982 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
8983 const0_rtx);
8985 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8986 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8988 continue;
8991 /* If we have (shift (logical)), move the logical to the outside
8992 to allow it to possibly combine with another logical and the
8993 shift to combine with another shift. This also canonicalizes to
8994 what a ZERO_EXTRACT looks like. Also, some machines have
8995 (and (shift)) insns. */
8997 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8998 /* We can't do this if we have (ashiftrt (xor)) and the
8999 constant has its sign bit set in shift_mode. */
9000 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9001 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9002 shift_mode))
9003 && (new = simplify_binary_operation (code, result_mode,
9004 XEXP (varop, 1),
9005 GEN_INT (count))) != 0
9006 && GET_CODE (new) == CONST_INT
9007 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9008 INTVAL (new), result_mode, &complement_p))
9010 varop = XEXP (varop, 0);
9011 continue;
9014 /* If we can't do that, try to simplify the shift in each arm of the
9015 logical expression, make a new logical expression, and apply
9016 the inverse distributive law. This also can't be done
9017 for some (ashiftrt (xor)). */
9018 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9019 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9020 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9021 shift_mode)))
9023 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9024 XEXP (varop, 0), count);
9025 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9026 XEXP (varop, 1), count);
9028 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9029 lhs, rhs);
9030 varop = apply_distributive_law (varop);
9032 count = 0;
9033 continue;
9035 break;
9037 case EQ:
9038 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9039 says that the sign bit can be tested, FOO has mode MODE, C is
9040 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9041 that may be nonzero. */
9042 if (code == LSHIFTRT
9043 && XEXP (varop, 1) == const0_rtx
9044 && GET_MODE (XEXP (varop, 0)) == result_mode
9045 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9046 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9047 && ((STORE_FLAG_VALUE
9048 & ((HOST_WIDE_INT) 1
9049 < (GET_MODE_BITSIZE (result_mode) - 1))))
9050 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9051 && merge_outer_ops (&outer_op, &outer_const, XOR,
9052 (HOST_WIDE_INT) 1, result_mode,
9053 &complement_p))
9055 varop = XEXP (varop, 0);
9056 count = 0;
9057 continue;
9059 break;
9061 case NEG:
9062 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9063 than the number of bits in the mode is equivalent to A. */
9064 if (code == LSHIFTRT
9065 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9066 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9068 varop = XEXP (varop, 0);
9069 count = 0;
9070 continue;
9073 /* NEG commutes with ASHIFT since it is multiplication. Move the
9074 NEG outside to allow shifts to combine. */
9075 if (code == ASHIFT
9076 && merge_outer_ops (&outer_op, &outer_const, NEG,
9077 (HOST_WIDE_INT) 0, result_mode,
9078 &complement_p))
9080 varop = XEXP (varop, 0);
9081 continue;
9083 break;
9085 case PLUS:
9086 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9087 is one less than the number of bits in the mode is
9088 equivalent to (xor A 1). */
9089 if (code == LSHIFTRT
9090 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9091 && XEXP (varop, 1) == constm1_rtx
9092 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9093 && merge_outer_ops (&outer_op, &outer_const, XOR,
9094 (HOST_WIDE_INT) 1, result_mode,
9095 &complement_p))
9097 count = 0;
9098 varop = XEXP (varop, 0);
9099 continue;
9102 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9103 that might be nonzero in BAR are those being shifted out and those
9104 bits are known zero in FOO, we can replace the PLUS with FOO.
9105 Similarly in the other operand order. This code occurs when
9106 we are computing the size of a variable-size array. */
9108 if ((code == ASHIFTRT || code == LSHIFTRT)
9109 && count < HOST_BITS_PER_WIDE_INT
9110 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9111 && (nonzero_bits (XEXP (varop, 1), result_mode)
9112 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9114 varop = XEXP (varop, 0);
9115 continue;
9117 else if ((code == ASHIFTRT || code == LSHIFTRT)
9118 && count < HOST_BITS_PER_WIDE_INT
9119 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9120 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9121 >> count)
9122 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9123 & nonzero_bits (XEXP (varop, 1),
9124 result_mode)))
9126 varop = XEXP (varop, 1);
9127 continue;
9130 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9131 if (code == ASHIFT
9132 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9133 && (new = simplify_binary_operation (ASHIFT, result_mode,
9134 XEXP (varop, 1),
9135 GEN_INT (count))) != 0
9136 && GET_CODE (new) == CONST_INT
9137 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9138 INTVAL (new), result_mode, &complement_p))
9140 varop = XEXP (varop, 0);
9141 continue;
9144 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9145 signbit', and attempt to change the PLUS to an XOR and move it to
9146 the outer operation as is done above in the AND/IOR/XOR case
9147 leg for shift(logical). See details in logical handling above
9148 for reasoning in doing so. */
9149 if (code == LSHIFTRT
9150 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9151 && mode_signbit_p (result_mode, XEXP (varop, 1))
9152 && (new = simplify_binary_operation (code, result_mode,
9153 XEXP (varop, 1),
9154 GEN_INT (count))) != 0
9155 && GET_CODE (new) == CONST_INT
9156 && merge_outer_ops (&outer_op, &outer_const, XOR,
9157 INTVAL (new), result_mode, &complement_p))
9159 varop = XEXP (varop, 0);
9160 continue;
9163 break;
9165 case MINUS:
9166 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9167 with C the size of VAROP - 1 and the shift is logical if
9168 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9169 we have a (gt X 0) operation. If the shift is arithmetic with
9170 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9171 we have a (neg (gt X 0)) operation. */
9173 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9174 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9175 && count == (unsigned int)
9176 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9177 && (code == LSHIFTRT || code == ASHIFTRT)
9178 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9179 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9180 == count
9181 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9183 count = 0;
9184 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9185 const0_rtx);
9187 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9188 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9190 continue;
9192 break;
9194 case TRUNCATE:
9195 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9196 if the truncate does not affect the value. */
9197 if (code == LSHIFTRT
9198 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9199 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9200 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9201 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9202 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9204 rtx varop_inner = XEXP (varop, 0);
9206 varop_inner
9207 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9208 XEXP (varop_inner, 0),
9209 GEN_INT
9210 (count + INTVAL (XEXP (varop_inner, 1))));
9211 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9212 count = 0;
9213 continue;
9215 break;
9217 default:
9218 break;
9221 break;
9224 /* We need to determine what mode to do the shift in. If the shift is
9225 a right shift or ROTATE, we must always do it in the mode it was
9226 originally done in. Otherwise, we can do it in MODE, the widest mode
9227 encountered. The code we care about is that of the shift that will
9228 actually be done, not the shift that was originally requested. */
9229 shift_mode
9230 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9231 ? result_mode : mode);
9233 /* We have now finished analyzing the shift. The result should be
9234 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9235 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9236 to the result of the shift. OUTER_CONST is the relevant constant,
9237 but we must turn off all bits turned off in the shift.
9239 If we were passed a value for X, see if we can use any pieces of
9240 it. If not, make new rtx. */
9242 if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9243 && GET_CODE (XEXP (x, 1)) == CONST_INT
9244 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9245 const_rtx = XEXP (x, 1);
9246 else
9247 const_rtx = GEN_INT (count);
9249 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9250 && GET_MODE (XEXP (x, 0)) == shift_mode
9251 && SUBREG_REG (XEXP (x, 0)) == varop)
9252 varop = XEXP (x, 0);
9253 else if (GET_MODE (varop) != shift_mode)
9254 varop = gen_lowpart (shift_mode, varop);
9256 /* If we can't make the SUBREG, try to return what we were given. */
9257 if (GET_CODE (varop) == CLOBBER)
9258 return x ? x : varop;
9260 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9261 if (new != 0)
9262 x = new;
9263 else
9264 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9266 /* If we have an outer operation and we just made a shift, it is
9267 possible that we could have simplified the shift were it not
9268 for the outer operation. So try to do the simplification
9269 recursively. */
9271 if (outer_op != UNKNOWN && GET_CODE (x) == code
9272 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9273 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9274 INTVAL (XEXP (x, 1)));
9276 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9277 turn off all the bits that the shift would have turned off. */
9278 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9279 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9280 GET_MODE_MASK (result_mode) >> orig_count);
9282 /* Do the remainder of the processing in RESULT_MODE. */
9283 x = gen_lowpart (result_mode, x);
9285 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9286 operation. */
9287 if (complement_p)
9288 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9290 if (outer_op != UNKNOWN)
9292 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9293 outer_const = trunc_int_for_mode (outer_const, result_mode);
9295 if (outer_op == AND)
9296 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9297 else if (outer_op == SET)
9298 /* This means that we have determined that the result is
9299 equivalent to a constant. This should be rare. */
9300 x = GEN_INT (outer_const);
9301 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9302 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9303 else
9304 x = simplify_gen_binary (outer_op, result_mode, x,
9305 GEN_INT (outer_const));
9308 return x;
9311 /* Like recog, but we receive the address of a pointer to a new pattern.
9312 We try to match the rtx that the pointer points to.
9313 If that fails, we may try to modify or replace the pattern,
9314 storing the replacement into the same pointer object.
9316 Modifications include deletion or addition of CLOBBERs.
9318 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9319 the CLOBBERs are placed.
9321 The value is the final insn code from the pattern ultimately matched,
9322 or -1. */
9324 static int
9325 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9327 rtx pat = *pnewpat;
9328 int insn_code_number;
9329 int num_clobbers_to_add = 0;
9330 int i;
9331 rtx notes = 0;
9332 rtx old_notes, old_pat;
9334 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9335 we use to indicate that something didn't match. If we find such a
9336 thing, force rejection. */
9337 if (GET_CODE (pat) == PARALLEL)
9338 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9339 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9340 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9341 return -1;
9343 old_pat = PATTERN (insn);
9344 old_notes = REG_NOTES (insn);
9345 PATTERN (insn) = pat;
9346 REG_NOTES (insn) = 0;
9348 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9350 /* If it isn't, there is the possibility that we previously had an insn
9351 that clobbered some register as a side effect, but the combined
9352 insn doesn't need to do that. So try once more without the clobbers
9353 unless this represents an ASM insn. */
9355 if (insn_code_number < 0 && ! check_asm_operands (pat)
9356 && GET_CODE (pat) == PARALLEL)
9358 int pos;
9360 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9361 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9363 if (i != pos)
9364 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9365 pos++;
9368 SUBST_INT (XVECLEN (pat, 0), pos);
9370 if (pos == 1)
9371 pat = XVECEXP (pat, 0, 0);
9373 PATTERN (insn) = pat;
9374 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9376 PATTERN (insn) = old_pat;
9377 REG_NOTES (insn) = old_notes;
9379 /* Recognize all noop sets, these will be killed by followup pass. */
9380 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9381 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9383 /* If we had any clobbers to add, make a new pattern than contains
9384 them. Then check to make sure that all of them are dead. */
9385 if (num_clobbers_to_add)
9387 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9388 rtvec_alloc (GET_CODE (pat) == PARALLEL
9389 ? (XVECLEN (pat, 0)
9390 + num_clobbers_to_add)
9391 : num_clobbers_to_add + 1));
9393 if (GET_CODE (pat) == PARALLEL)
9394 for (i = 0; i < XVECLEN (pat, 0); i++)
9395 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9396 else
9397 XVECEXP (newpat, 0, 0) = pat;
9399 add_clobbers (newpat, insn_code_number);
9401 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9402 i < XVECLEN (newpat, 0); i++)
9404 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9405 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9406 return -1;
9407 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9408 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9410 pat = newpat;
9413 *pnewpat = pat;
9414 *pnotes = notes;
9416 return insn_code_number;
9419 /* Like gen_lowpart_general but for use by combine. In combine it
9420 is not possible to create any new pseudoregs. However, it is
9421 safe to create invalid memory addresses, because combine will
9422 try to recognize them and all they will do is make the combine
9423 attempt fail.
9425 If for some reason this cannot do its job, an rtx
9426 (clobber (const_int 0)) is returned.
9427 An insn containing that will not be recognized. */
9429 static rtx
9430 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9432 enum machine_mode imode = GET_MODE (x);
9433 unsigned int osize = GET_MODE_SIZE (omode);
9434 unsigned int isize = GET_MODE_SIZE (imode);
9435 rtx result;
9437 if (omode == imode)
9438 return x;
9440 /* Return identity if this is a CONST or symbolic reference. */
9441 if (omode == Pmode
9442 && (GET_CODE (x) == CONST
9443 || GET_CODE (x) == SYMBOL_REF
9444 || GET_CODE (x) == LABEL_REF))
9445 return x;
9447 /* We can only support MODE being wider than a word if X is a
9448 constant integer or has a mode the same size. */
9449 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9450 && ! ((imode == VOIDmode
9451 && (GET_CODE (x) == CONST_INT
9452 || GET_CODE (x) == CONST_DOUBLE))
9453 || isize == osize))
9454 goto fail;
9456 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9457 won't know what to do. So we will strip off the SUBREG here and
9458 process normally. */
9459 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9461 x = SUBREG_REG (x);
9463 /* For use in case we fall down into the address adjustments
9464 further below, we need to adjust the known mode and size of
9465 x; imode and isize, since we just adjusted x. */
9466 imode = GET_MODE (x);
9468 if (imode == omode)
9469 return x;
9471 isize = GET_MODE_SIZE (imode);
9474 result = gen_lowpart_common (omode, x);
9476 #ifdef CANNOT_CHANGE_MODE_CLASS
9477 if (result != 0 && GET_CODE (result) == SUBREG)
9478 record_subregs_of_mode (result);
9479 #endif
9481 if (result)
9482 return result;
9484 if (MEM_P (x))
9486 int offset = 0;
9488 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9489 address. */
9490 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9491 goto fail;
9493 /* If we want to refer to something bigger than the original memref,
9494 generate a paradoxical subreg instead. That will force a reload
9495 of the original memref X. */
9496 if (isize < osize)
9497 return gen_rtx_SUBREG (omode, x, 0);
9499 if (WORDS_BIG_ENDIAN)
9500 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9502 /* Adjust the address so that the address-after-the-data is
9503 unchanged. */
9504 if (BYTES_BIG_ENDIAN)
9505 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9507 return adjust_address_nv (x, omode, offset);
9510 /* If X is a comparison operator, rewrite it in a new mode. This
9511 probably won't match, but may allow further simplifications. */
9512 else if (COMPARISON_P (x))
9513 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9515 /* If we couldn't simplify X any other way, just enclose it in a
9516 SUBREG. Normally, this SUBREG won't match, but some patterns may
9517 include an explicit SUBREG or we may simplify it further in combine. */
9518 else
9520 int offset = 0;
9521 rtx res;
9523 offset = subreg_lowpart_offset (omode, imode);
9524 if (imode == VOIDmode)
9526 imode = int_mode_for_mode (omode);
9527 x = gen_lowpart_common (imode, x);
9528 if (x == NULL)
9529 goto fail;
9531 res = simplify_gen_subreg (omode, x, imode, offset);
9532 if (res)
9533 return res;
9536 fail:
9537 return gen_rtx_CLOBBER (imode, const0_rtx);
9540 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9541 comparison code that will be tested.
9543 The result is a possibly different comparison code to use. *POP0 and
9544 *POP1 may be updated.
9546 It is possible that we might detect that a comparison is either always
9547 true or always false. However, we do not perform general constant
9548 folding in combine, so this knowledge isn't useful. Such tautologies
9549 should have been detected earlier. Hence we ignore all such cases. */
9551 static enum rtx_code
9552 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9554 rtx op0 = *pop0;
9555 rtx op1 = *pop1;
9556 rtx tem, tem1;
9557 int i;
9558 enum machine_mode mode, tmode;
9560 /* Try a few ways of applying the same transformation to both operands. */
9561 while (1)
9563 #ifndef WORD_REGISTER_OPERATIONS
9564 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9565 so check specially. */
9566 if (code != GTU && code != GEU && code != LTU && code != LEU
9567 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9568 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9569 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9570 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9571 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9572 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9573 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9574 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9575 && XEXP (op0, 1) == XEXP (op1, 1)
9576 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9577 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9578 && (INTVAL (XEXP (op0, 1))
9579 == (GET_MODE_BITSIZE (GET_MODE (op0))
9580 - (GET_MODE_BITSIZE
9581 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9583 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9584 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9586 #endif
9588 /* If both operands are the same constant shift, see if we can ignore the
9589 shift. We can if the shift is a rotate or if the bits shifted out of
9590 this shift are known to be zero for both inputs and if the type of
9591 comparison is compatible with the shift. */
9592 if (GET_CODE (op0) == GET_CODE (op1)
9593 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9594 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9595 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9596 && (code != GT && code != LT && code != GE && code != LE))
9597 || (GET_CODE (op0) == ASHIFTRT
9598 && (code != GTU && code != LTU
9599 && code != GEU && code != LEU)))
9600 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9601 && INTVAL (XEXP (op0, 1)) >= 0
9602 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9603 && XEXP (op0, 1) == XEXP (op1, 1))
9605 enum machine_mode mode = GET_MODE (op0);
9606 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9607 int shift_count = INTVAL (XEXP (op0, 1));
9609 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9610 mask &= (mask >> shift_count) << shift_count;
9611 else if (GET_CODE (op0) == ASHIFT)
9612 mask = (mask & (mask << shift_count)) >> shift_count;
9614 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9615 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9616 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9617 else
9618 break;
9621 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9622 SUBREGs are of the same mode, and, in both cases, the AND would
9623 be redundant if the comparison was done in the narrower mode,
9624 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9625 and the operand's possibly nonzero bits are 0xffffff01; in that case
9626 if we only care about QImode, we don't need the AND). This case
9627 occurs if the output mode of an scc insn is not SImode and
9628 STORE_FLAG_VALUE == 1 (e.g., the 386).
9630 Similarly, check for a case where the AND's are ZERO_EXTEND
9631 operations from some narrower mode even though a SUBREG is not
9632 present. */
9634 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9635 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9636 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9638 rtx inner_op0 = XEXP (op0, 0);
9639 rtx inner_op1 = XEXP (op1, 0);
9640 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9641 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9642 int changed = 0;
9644 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9645 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9646 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9647 && (GET_MODE (SUBREG_REG (inner_op0))
9648 == GET_MODE (SUBREG_REG (inner_op1)))
9649 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9650 <= HOST_BITS_PER_WIDE_INT)
9651 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9652 GET_MODE (SUBREG_REG (inner_op0)))))
9653 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9654 GET_MODE (SUBREG_REG (inner_op1))))))
9656 op0 = SUBREG_REG (inner_op0);
9657 op1 = SUBREG_REG (inner_op1);
9659 /* The resulting comparison is always unsigned since we masked
9660 off the original sign bit. */
9661 code = unsigned_condition (code);
9663 changed = 1;
9666 else if (c0 == c1)
9667 for (tmode = GET_CLASS_NARROWEST_MODE
9668 (GET_MODE_CLASS (GET_MODE (op0)));
9669 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9670 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9672 op0 = gen_lowpart (tmode, inner_op0);
9673 op1 = gen_lowpart (tmode, inner_op1);
9674 code = unsigned_condition (code);
9675 changed = 1;
9676 break;
9679 if (! changed)
9680 break;
9683 /* If both operands are NOT, we can strip off the outer operation
9684 and adjust the comparison code for swapped operands; similarly for
9685 NEG, except that this must be an equality comparison. */
9686 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9687 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9688 && (code == EQ || code == NE)))
9689 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9691 else
9692 break;
9695 /* If the first operand is a constant, swap the operands and adjust the
9696 comparison code appropriately, but don't do this if the second operand
9697 is already a constant integer. */
9698 if (swap_commutative_operands_p (op0, op1))
9700 tem = op0, op0 = op1, op1 = tem;
9701 code = swap_condition (code);
9704 /* We now enter a loop during which we will try to simplify the comparison.
9705 For the most part, we only are concerned with comparisons with zero,
9706 but some things may really be comparisons with zero but not start
9707 out looking that way. */
9709 while (GET_CODE (op1) == CONST_INT)
9711 enum machine_mode mode = GET_MODE (op0);
9712 unsigned int mode_width = GET_MODE_BITSIZE (mode);
9713 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9714 int equality_comparison_p;
9715 int sign_bit_comparison_p;
9716 int unsigned_comparison_p;
9717 HOST_WIDE_INT const_op;
9719 /* We only want to handle integral modes. This catches VOIDmode,
9720 CCmode, and the floating-point modes. An exception is that we
9721 can handle VOIDmode if OP0 is a COMPARE or a comparison
9722 operation. */
9724 if (GET_MODE_CLASS (mode) != MODE_INT
9725 && ! (mode == VOIDmode
9726 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9727 break;
9729 /* Get the constant we are comparing against and turn off all bits
9730 not on in our mode. */
9731 const_op = INTVAL (op1);
9732 if (mode != VOIDmode)
9733 const_op = trunc_int_for_mode (const_op, mode);
9734 op1 = GEN_INT (const_op);
9736 /* If we are comparing against a constant power of two and the value
9737 being compared can only have that single bit nonzero (e.g., it was
9738 `and'ed with that bit), we can replace this with a comparison
9739 with zero. */
9740 if (const_op
9741 && (code == EQ || code == NE || code == GE || code == GEU
9742 || code == LT || code == LTU)
9743 && mode_width <= HOST_BITS_PER_WIDE_INT
9744 && exact_log2 (const_op) >= 0
9745 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9747 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9748 op1 = const0_rtx, const_op = 0;
9751 /* Similarly, if we are comparing a value known to be either -1 or
9752 0 with -1, change it to the opposite comparison against zero. */
9754 if (const_op == -1
9755 && (code == EQ || code == NE || code == GT || code == LE
9756 || code == GEU || code == LTU)
9757 && num_sign_bit_copies (op0, mode) == mode_width)
9759 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9760 op1 = const0_rtx, const_op = 0;
9763 /* Do some canonicalizations based on the comparison code. We prefer
9764 comparisons against zero and then prefer equality comparisons.
9765 If we can reduce the size of a constant, we will do that too. */
9767 switch (code)
9769 case LT:
9770 /* < C is equivalent to <= (C - 1) */
9771 if (const_op > 0)
9773 const_op -= 1;
9774 op1 = GEN_INT (const_op);
9775 code = LE;
9776 /* ... fall through to LE case below. */
9778 else
9779 break;
9781 case LE:
9782 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9783 if (const_op < 0)
9785 const_op += 1;
9786 op1 = GEN_INT (const_op);
9787 code = LT;
9790 /* If we are doing a <= 0 comparison on a value known to have
9791 a zero sign bit, we can replace this with == 0. */
9792 else if (const_op == 0
9793 && mode_width <= HOST_BITS_PER_WIDE_INT
9794 && (nonzero_bits (op0, mode)
9795 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9796 code = EQ;
9797 break;
9799 case GE:
9800 /* >= C is equivalent to > (C - 1). */
9801 if (const_op > 0)
9803 const_op -= 1;
9804 op1 = GEN_INT (const_op);
9805 code = GT;
9806 /* ... fall through to GT below. */
9808 else
9809 break;
9811 case GT:
9812 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
9813 if (const_op < 0)
9815 const_op += 1;
9816 op1 = GEN_INT (const_op);
9817 code = GE;
9820 /* If we are doing a > 0 comparison on a value known to have
9821 a zero sign bit, we can replace this with != 0. */
9822 else if (const_op == 0
9823 && mode_width <= HOST_BITS_PER_WIDE_INT
9824 && (nonzero_bits (op0, mode)
9825 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9826 code = NE;
9827 break;
9829 case LTU:
9830 /* < C is equivalent to <= (C - 1). */
9831 if (const_op > 0)
9833 const_op -= 1;
9834 op1 = GEN_INT (const_op);
9835 code = LEU;
9836 /* ... fall through ... */
9839 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9840 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9841 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9843 const_op = 0, op1 = const0_rtx;
9844 code = GE;
9845 break;
9847 else
9848 break;
9850 case LEU:
9851 /* unsigned <= 0 is equivalent to == 0 */
9852 if (const_op == 0)
9853 code = EQ;
9855 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9856 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9857 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9859 const_op = 0, op1 = const0_rtx;
9860 code = GE;
9862 break;
9864 case GEU:
9865 /* >= C is equivalent to > (C - 1). */
9866 if (const_op > 1)
9868 const_op -= 1;
9869 op1 = GEN_INT (const_op);
9870 code = GTU;
9871 /* ... fall through ... */
9874 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9875 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9876 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9878 const_op = 0, op1 = const0_rtx;
9879 code = LT;
9880 break;
9882 else
9883 break;
9885 case GTU:
9886 /* unsigned > 0 is equivalent to != 0 */
9887 if (const_op == 0)
9888 code = NE;
9890 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9891 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9892 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9894 const_op = 0, op1 = const0_rtx;
9895 code = LT;
9897 break;
9899 default:
9900 break;
9903 /* Compute some predicates to simplify code below. */
9905 equality_comparison_p = (code == EQ || code == NE);
9906 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9907 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9908 || code == GEU);
9910 /* If this is a sign bit comparison and we can do arithmetic in
9911 MODE, say that we will only be needing the sign bit of OP0. */
9912 if (sign_bit_comparison_p
9913 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9914 op0 = force_to_mode (op0, mode,
9915 ((HOST_WIDE_INT) 1
9916 << (GET_MODE_BITSIZE (mode) - 1)),
9917 NULL_RTX, 0);
9919 /* Now try cases based on the opcode of OP0. If none of the cases
9920 does a "continue", we exit this loop immediately after the
9921 switch. */
9923 switch (GET_CODE (op0))
9925 case ZERO_EXTRACT:
9926 /* If we are extracting a single bit from a variable position in
9927 a constant that has only a single bit set and are comparing it
9928 with zero, we can convert this into an equality comparison
9929 between the position and the location of the single bit. */
9930 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
9931 have already reduced the shift count modulo the word size. */
9932 if (!SHIFT_COUNT_TRUNCATED
9933 && GET_CODE (XEXP (op0, 0)) == CONST_INT
9934 && XEXP (op0, 1) == const1_rtx
9935 && equality_comparison_p && const_op == 0
9936 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9938 if (BITS_BIG_ENDIAN)
9940 enum machine_mode new_mode
9941 = mode_for_extraction (EP_extzv, 1);
9942 if (new_mode == MAX_MACHINE_MODE)
9943 i = BITS_PER_WORD - 1 - i;
9944 else
9946 mode = new_mode;
9947 i = (GET_MODE_BITSIZE (mode) - 1 - i);
9951 op0 = XEXP (op0, 2);
9952 op1 = GEN_INT (i);
9953 const_op = i;
9955 /* Result is nonzero iff shift count is equal to I. */
9956 code = reverse_condition (code);
9957 continue;
9960 /* ... fall through ... */
9962 case SIGN_EXTRACT:
9963 tem = expand_compound_operation (op0);
9964 if (tem != op0)
9966 op0 = tem;
9967 continue;
9969 break;
9971 case NOT:
9972 /* If testing for equality, we can take the NOT of the constant. */
9973 if (equality_comparison_p
9974 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9976 op0 = XEXP (op0, 0);
9977 op1 = tem;
9978 continue;
9981 /* If just looking at the sign bit, reverse the sense of the
9982 comparison. */
9983 if (sign_bit_comparison_p)
9985 op0 = XEXP (op0, 0);
9986 code = (code == GE ? LT : GE);
9987 continue;
9989 break;
9991 case NEG:
9992 /* If testing for equality, we can take the NEG of the constant. */
9993 if (equality_comparison_p
9994 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9996 op0 = XEXP (op0, 0);
9997 op1 = tem;
9998 continue;
10001 /* The remaining cases only apply to comparisons with zero. */
10002 if (const_op != 0)
10003 break;
10005 /* When X is ABS or is known positive,
10006 (neg X) is < 0 if and only if X != 0. */
10008 if (sign_bit_comparison_p
10009 && (GET_CODE (XEXP (op0, 0)) == ABS
10010 || (mode_width <= HOST_BITS_PER_WIDE_INT
10011 && (nonzero_bits (XEXP (op0, 0), mode)
10012 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10014 op0 = XEXP (op0, 0);
10015 code = (code == LT ? NE : EQ);
10016 continue;
10019 /* If we have NEG of something whose two high-order bits are the
10020 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10021 if (num_sign_bit_copies (op0, mode) >= 2)
10023 op0 = XEXP (op0, 0);
10024 code = swap_condition (code);
10025 continue;
10027 break;
10029 case ROTATE:
10030 /* If we are testing equality and our count is a constant, we
10031 can perform the inverse operation on our RHS. */
10032 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10033 && (tem = simplify_binary_operation (ROTATERT, mode,
10034 op1, XEXP (op0, 1))) != 0)
10036 op0 = XEXP (op0, 0);
10037 op1 = tem;
10038 continue;
10041 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10042 a particular bit. Convert it to an AND of a constant of that
10043 bit. This will be converted into a ZERO_EXTRACT. */
10044 if (const_op == 0 && sign_bit_comparison_p
10045 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10046 && mode_width <= HOST_BITS_PER_WIDE_INT)
10048 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10049 ((HOST_WIDE_INT) 1
10050 << (mode_width - 1
10051 - INTVAL (XEXP (op0, 1)))));
10052 code = (code == LT ? NE : EQ);
10053 continue;
10056 /* Fall through. */
10058 case ABS:
10059 /* ABS is ignorable inside an equality comparison with zero. */
10060 if (const_op == 0 && equality_comparison_p)
10062 op0 = XEXP (op0, 0);
10063 continue;
10065 break;
10067 case SIGN_EXTEND:
10068 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10069 (compare FOO CONST) if CONST fits in FOO's mode and we
10070 are either testing inequality or have an unsigned
10071 comparison with ZERO_EXTEND or a signed comparison with
10072 SIGN_EXTEND. But don't do it if we don't have a compare
10073 insn of the given mode, since we'd have to revert it
10074 later on, and then we wouldn't know whether to sign- or
10075 zero-extend. */
10076 mode = GET_MODE (XEXP (op0, 0));
10077 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10078 && ! unsigned_comparison_p
10079 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10080 && ((unsigned HOST_WIDE_INT) const_op
10081 < (((unsigned HOST_WIDE_INT) 1
10082 << (GET_MODE_BITSIZE (mode) - 1))))
10083 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10085 op0 = XEXP (op0, 0);
10086 continue;
10088 break;
10090 case SUBREG:
10091 /* Check for the case where we are comparing A - C1 with C2, that is
10093 (subreg:MODE (plus (A) (-C1))) op (C2)
10095 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10096 comparison in the wider mode. One of the following two conditions
10097 must be true in order for this to be valid:
10099 1. The mode extension results in the same bit pattern being added
10100 on both sides and the comparison is equality or unsigned. As
10101 C2 has been truncated to fit in MODE, the pattern can only be
10102 all 0s or all 1s.
10104 2. The mode extension results in the sign bit being copied on
10105 each side.
10107 The difficulty here is that we have predicates for A but not for
10108 (A - C1) so we need to check that C1 is within proper bounds so
10109 as to perturbate A as little as possible. */
10111 if (mode_width <= HOST_BITS_PER_WIDE_INT
10112 && subreg_lowpart_p (op0)
10113 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10114 && GET_CODE (SUBREG_REG (op0)) == PLUS
10115 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10117 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10118 rtx a = XEXP (SUBREG_REG (op0), 0);
10119 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10121 if ((c1 > 0
10122 && (unsigned HOST_WIDE_INT) c1
10123 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10124 && (equality_comparison_p || unsigned_comparison_p)
10125 /* (A - C1) zero-extends if it is positive and sign-extends
10126 if it is negative, C2 both zero- and sign-extends. */
10127 && ((0 == (nonzero_bits (a, inner_mode)
10128 & ~GET_MODE_MASK (mode))
10129 && const_op >= 0)
10130 /* (A - C1) sign-extends if it is positive and 1-extends
10131 if it is negative, C2 both sign- and 1-extends. */
10132 || (num_sign_bit_copies (a, inner_mode)
10133 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10134 - mode_width)
10135 && const_op < 0)))
10136 || ((unsigned HOST_WIDE_INT) c1
10137 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10138 /* (A - C1) always sign-extends, like C2. */
10139 && num_sign_bit_copies (a, inner_mode)
10140 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10141 - mode_width - 1)))
10143 op0 = SUBREG_REG (op0);
10144 continue;
10148 /* If the inner mode is narrower and we are extracting the low part,
10149 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10150 if (subreg_lowpart_p (op0)
10151 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10152 /* Fall through */ ;
10153 else
10154 break;
10156 /* ... fall through ... */
10158 case ZERO_EXTEND:
10159 mode = GET_MODE (XEXP (op0, 0));
10160 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10161 && (unsigned_comparison_p || equality_comparison_p)
10162 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10163 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10164 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10166 op0 = XEXP (op0, 0);
10167 continue;
10169 break;
10171 case PLUS:
10172 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10173 this for equality comparisons due to pathological cases involving
10174 overflows. */
10175 if (equality_comparison_p
10176 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10177 op1, XEXP (op0, 1))))
10179 op0 = XEXP (op0, 0);
10180 op1 = tem;
10181 continue;
10184 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10185 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10186 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10188 op0 = XEXP (XEXP (op0, 0), 0);
10189 code = (code == LT ? EQ : NE);
10190 continue;
10192 break;
10194 case MINUS:
10195 /* We used to optimize signed comparisons against zero, but that
10196 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10197 arrive here as equality comparisons, or (GEU, LTU) are
10198 optimized away. No need to special-case them. */
10200 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10201 (eq B (minus A C)), whichever simplifies. We can only do
10202 this for equality comparisons due to pathological cases involving
10203 overflows. */
10204 if (equality_comparison_p
10205 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10206 XEXP (op0, 1), op1)))
10208 op0 = XEXP (op0, 0);
10209 op1 = tem;
10210 continue;
10213 if (equality_comparison_p
10214 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10215 XEXP (op0, 0), op1)))
10217 op0 = XEXP (op0, 1);
10218 op1 = tem;
10219 continue;
10222 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10223 of bits in X minus 1, is one iff X > 0. */
10224 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10225 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10226 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10227 == mode_width - 1
10228 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10230 op0 = XEXP (op0, 1);
10231 code = (code == GE ? LE : GT);
10232 continue;
10234 break;
10236 case XOR:
10237 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10238 if C is zero or B is a constant. */
10239 if (equality_comparison_p
10240 && 0 != (tem = simplify_binary_operation (XOR, mode,
10241 XEXP (op0, 1), op1)))
10243 op0 = XEXP (op0, 0);
10244 op1 = tem;
10245 continue;
10247 break;
10249 case EQ: case NE:
10250 case UNEQ: case LTGT:
10251 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10252 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10253 case UNORDERED: case ORDERED:
10254 /* We can't do anything if OP0 is a condition code value, rather
10255 than an actual data value. */
10256 if (const_op != 0
10257 || CC0_P (XEXP (op0, 0))
10258 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10259 break;
10261 /* Get the two operands being compared. */
10262 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10263 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10264 else
10265 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10267 /* Check for the cases where we simply want the result of the
10268 earlier test or the opposite of that result. */
10269 if (code == NE || code == EQ
10270 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10271 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10272 && (STORE_FLAG_VALUE
10273 & (((HOST_WIDE_INT) 1
10274 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10275 && (code == LT || code == GE)))
10277 enum rtx_code new_code;
10278 if (code == LT || code == NE)
10279 new_code = GET_CODE (op0);
10280 else
10281 new_code = reversed_comparison_code (op0, NULL);
10283 if (new_code != UNKNOWN)
10285 code = new_code;
10286 op0 = tem;
10287 op1 = tem1;
10288 continue;
10291 break;
10293 case IOR:
10294 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10295 iff X <= 0. */
10296 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10297 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10298 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10300 op0 = XEXP (op0, 1);
10301 code = (code == GE ? GT : LE);
10302 continue;
10304 break;
10306 case AND:
10307 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10308 will be converted to a ZERO_EXTRACT later. */
10309 if (const_op == 0 && equality_comparison_p
10310 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10311 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10313 op0 = simplify_and_const_int
10314 (op0, mode, gen_rtx_LSHIFTRT (mode,
10315 XEXP (op0, 1),
10316 XEXP (XEXP (op0, 0), 1)),
10317 (HOST_WIDE_INT) 1);
10318 continue;
10321 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10322 zero and X is a comparison and C1 and C2 describe only bits set
10323 in STORE_FLAG_VALUE, we can compare with X. */
10324 if (const_op == 0 && equality_comparison_p
10325 && mode_width <= HOST_BITS_PER_WIDE_INT
10326 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10327 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10328 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10329 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10330 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10332 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10333 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10334 if ((~STORE_FLAG_VALUE & mask) == 0
10335 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10336 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10337 && COMPARISON_P (tem))))
10339 op0 = XEXP (XEXP (op0, 0), 0);
10340 continue;
10344 /* If we are doing an equality comparison of an AND of a bit equal
10345 to the sign bit, replace this with a LT or GE comparison of
10346 the underlying value. */
10347 if (equality_comparison_p
10348 && const_op == 0
10349 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10350 && mode_width <= HOST_BITS_PER_WIDE_INT
10351 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10352 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10354 op0 = XEXP (op0, 0);
10355 code = (code == EQ ? GE : LT);
10356 continue;
10359 /* If this AND operation is really a ZERO_EXTEND from a narrower
10360 mode, the constant fits within that mode, and this is either an
10361 equality or unsigned comparison, try to do this comparison in
10362 the narrower mode. */
10363 if ((equality_comparison_p || unsigned_comparison_p)
10364 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10365 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10366 & GET_MODE_MASK (mode))
10367 + 1)) >= 0
10368 && const_op >> i == 0
10369 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10371 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10372 continue;
10375 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10376 fits in both M1 and M2 and the SUBREG is either paradoxical
10377 or represents the low part, permute the SUBREG and the AND
10378 and try again. */
10379 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10381 unsigned HOST_WIDE_INT c1;
10382 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10383 /* Require an integral mode, to avoid creating something like
10384 (AND:SF ...). */
10385 if (SCALAR_INT_MODE_P (tmode)
10386 /* It is unsafe to commute the AND into the SUBREG if the
10387 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10388 not defined. As originally written the upper bits
10389 have a defined value due to the AND operation.
10390 However, if we commute the AND inside the SUBREG then
10391 they no longer have defined values and the meaning of
10392 the code has been changed. */
10393 && (0
10394 #ifdef WORD_REGISTER_OPERATIONS
10395 || (mode_width > GET_MODE_BITSIZE (tmode)
10396 && mode_width <= BITS_PER_WORD)
10397 #endif
10398 || (mode_width <= GET_MODE_BITSIZE (tmode)
10399 && subreg_lowpart_p (XEXP (op0, 0))))
10400 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10401 && mode_width <= HOST_BITS_PER_WIDE_INT
10402 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10403 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10404 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10405 && c1 != mask
10406 && c1 != GET_MODE_MASK (tmode))
10408 op0 = simplify_gen_binary (AND, tmode,
10409 SUBREG_REG (XEXP (op0, 0)),
10410 gen_int_mode (c1, tmode));
10411 op0 = gen_lowpart (mode, op0);
10412 continue;
10416 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10417 if (const_op == 0 && equality_comparison_p
10418 && XEXP (op0, 1) == const1_rtx
10419 && GET_CODE (XEXP (op0, 0)) == NOT)
10421 op0 = simplify_and_const_int
10422 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10423 code = (code == NE ? EQ : NE);
10424 continue;
10427 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10428 (eq (and (lshiftrt X) 1) 0).
10429 Also handle the case where (not X) is expressed using xor. */
10430 if (const_op == 0 && equality_comparison_p
10431 && XEXP (op0, 1) == const1_rtx
10432 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10434 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10435 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10437 if (GET_CODE (shift_op) == NOT
10438 || (GET_CODE (shift_op) == XOR
10439 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10440 && GET_CODE (shift_count) == CONST_INT
10441 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10442 && (INTVAL (XEXP (shift_op, 1))
10443 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10445 op0 = simplify_and_const_int
10446 (NULL_RTX, mode,
10447 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10448 (HOST_WIDE_INT) 1);
10449 code = (code == NE ? EQ : NE);
10450 continue;
10453 break;
10455 case ASHIFT:
10456 /* If we have (compare (ashift FOO N) (const_int C)) and
10457 the high order N bits of FOO (N+1 if an inequality comparison)
10458 are known to be zero, we can do this by comparing FOO with C
10459 shifted right N bits so long as the low-order N bits of C are
10460 zero. */
10461 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10462 && INTVAL (XEXP (op0, 1)) >= 0
10463 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10464 < HOST_BITS_PER_WIDE_INT)
10465 && ((const_op
10466 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10467 && mode_width <= HOST_BITS_PER_WIDE_INT
10468 && (nonzero_bits (XEXP (op0, 0), mode)
10469 & ~(mask >> (INTVAL (XEXP (op0, 1))
10470 + ! equality_comparison_p))) == 0)
10472 /* We must perform a logical shift, not an arithmetic one,
10473 as we want the top N bits of C to be zero. */
10474 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10476 temp >>= INTVAL (XEXP (op0, 1));
10477 op1 = gen_int_mode (temp, mode);
10478 op0 = XEXP (op0, 0);
10479 continue;
10482 /* If we are doing a sign bit comparison, it means we are testing
10483 a particular bit. Convert it to the appropriate AND. */
10484 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10485 && mode_width <= HOST_BITS_PER_WIDE_INT)
10487 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10488 ((HOST_WIDE_INT) 1
10489 << (mode_width - 1
10490 - INTVAL (XEXP (op0, 1)))));
10491 code = (code == LT ? NE : EQ);
10492 continue;
10495 /* If this an equality comparison with zero and we are shifting
10496 the low bit to the sign bit, we can convert this to an AND of the
10497 low-order bit. */
10498 if (const_op == 0 && equality_comparison_p
10499 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10500 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10501 == mode_width - 1)
10503 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10504 (HOST_WIDE_INT) 1);
10505 continue;
10507 break;
10509 case ASHIFTRT:
10510 /* If this is an equality comparison with zero, we can do this
10511 as a logical shift, which might be much simpler. */
10512 if (equality_comparison_p && const_op == 0
10513 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10515 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10516 XEXP (op0, 0),
10517 INTVAL (XEXP (op0, 1)));
10518 continue;
10521 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10522 do the comparison in a narrower mode. */
10523 if (! unsigned_comparison_p
10524 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10525 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10526 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10527 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10528 MODE_INT, 1)) != BLKmode
10529 && (((unsigned HOST_WIDE_INT) const_op
10530 + (GET_MODE_MASK (tmode) >> 1) + 1)
10531 <= GET_MODE_MASK (tmode)))
10533 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10534 continue;
10537 /* Likewise if OP0 is a PLUS of a sign extension with a
10538 constant, which is usually represented with the PLUS
10539 between the shifts. */
10540 if (! unsigned_comparison_p
10541 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10542 && GET_CODE (XEXP (op0, 0)) == PLUS
10543 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10544 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10545 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10546 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10547 MODE_INT, 1)) != BLKmode
10548 && (((unsigned HOST_WIDE_INT) const_op
10549 + (GET_MODE_MASK (tmode) >> 1) + 1)
10550 <= GET_MODE_MASK (tmode)))
10552 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10553 rtx add_const = XEXP (XEXP (op0, 0), 1);
10554 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10555 add_const, XEXP (op0, 1));
10557 op0 = simplify_gen_binary (PLUS, tmode,
10558 gen_lowpart (tmode, inner),
10559 new_const);
10560 continue;
10563 /* ... fall through ... */
10564 case LSHIFTRT:
10565 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10566 the low order N bits of FOO are known to be zero, we can do this
10567 by comparing FOO with C shifted left N bits so long as no
10568 overflow occurs. */
10569 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10570 && INTVAL (XEXP (op0, 1)) >= 0
10571 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10572 && mode_width <= HOST_BITS_PER_WIDE_INT
10573 && (nonzero_bits (XEXP (op0, 0), mode)
10574 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10575 && (((unsigned HOST_WIDE_INT) const_op
10576 + (GET_CODE (op0) != LSHIFTRT
10577 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10578 + 1)
10579 : 0))
10580 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10582 /* If the shift was logical, then we must make the condition
10583 unsigned. */
10584 if (GET_CODE (op0) == LSHIFTRT)
10585 code = unsigned_condition (code);
10587 const_op <<= INTVAL (XEXP (op0, 1));
10588 op1 = GEN_INT (const_op);
10589 op0 = XEXP (op0, 0);
10590 continue;
10593 /* If we are using this shift to extract just the sign bit, we
10594 can replace this with an LT or GE comparison. */
10595 if (const_op == 0
10596 && (equality_comparison_p || sign_bit_comparison_p)
10597 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10598 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10599 == mode_width - 1)
10601 op0 = XEXP (op0, 0);
10602 code = (code == NE || code == GT ? LT : GE);
10603 continue;
10605 break;
10607 default:
10608 break;
10611 break;
10614 /* Now make any compound operations involved in this comparison. Then,
10615 check for an outmost SUBREG on OP0 that is not doing anything or is
10616 paradoxical. The latter transformation must only be performed when
10617 it is known that the "extra" bits will be the same in op0 and op1 or
10618 that they don't matter. There are three cases to consider:
10620 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10621 care bits and we can assume they have any convenient value. So
10622 making the transformation is safe.
10624 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10625 In this case the upper bits of op0 are undefined. We should not make
10626 the simplification in that case as we do not know the contents of
10627 those bits.
10629 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10630 UNKNOWN. In that case we know those bits are zeros or ones. We must
10631 also be sure that they are the same as the upper bits of op1.
10633 We can never remove a SUBREG for a non-equality comparison because
10634 the sign bit is in a different place in the underlying object. */
10636 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10637 op1 = make_compound_operation (op1, SET);
10639 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10640 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10641 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10642 && (code == NE || code == EQ))
10644 if (GET_MODE_SIZE (GET_MODE (op0))
10645 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10647 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
10648 implemented. */
10649 if (REG_P (SUBREG_REG (op0)))
10651 op0 = SUBREG_REG (op0);
10652 op1 = gen_lowpart (GET_MODE (op0), op1);
10655 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10656 <= HOST_BITS_PER_WIDE_INT)
10657 && (nonzero_bits (SUBREG_REG (op0),
10658 GET_MODE (SUBREG_REG (op0)))
10659 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10661 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10663 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10664 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10665 op0 = SUBREG_REG (op0), op1 = tem;
10669 /* We now do the opposite procedure: Some machines don't have compare
10670 insns in all modes. If OP0's mode is an integer mode smaller than a
10671 word and we can't do a compare in that mode, see if there is a larger
10672 mode for which we can do the compare. There are a number of cases in
10673 which we can use the wider mode. */
10675 mode = GET_MODE (op0);
10676 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10677 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10678 && ! have_insn_for (COMPARE, mode))
10679 for (tmode = GET_MODE_WIDER_MODE (mode);
10680 (tmode != VOIDmode
10681 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10682 tmode = GET_MODE_WIDER_MODE (tmode))
10683 if (have_insn_for (COMPARE, tmode))
10685 int zero_extended;
10687 /* If the only nonzero bits in OP0 and OP1 are those in the
10688 narrower mode and this is an equality or unsigned comparison,
10689 we can use the wider mode. Similarly for sign-extended
10690 values, in which case it is true for all comparisons. */
10691 zero_extended = ((code == EQ || code == NE
10692 || code == GEU || code == GTU
10693 || code == LEU || code == LTU)
10694 && (nonzero_bits (op0, tmode)
10695 & ~GET_MODE_MASK (mode)) == 0
10696 && ((GET_CODE (op1) == CONST_INT
10697 || (nonzero_bits (op1, tmode)
10698 & ~GET_MODE_MASK (mode)) == 0)));
10700 if (zero_extended
10701 || ((num_sign_bit_copies (op0, tmode)
10702 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10703 - GET_MODE_BITSIZE (mode)))
10704 && (num_sign_bit_copies (op1, tmode)
10705 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10706 - GET_MODE_BITSIZE (mode)))))
10708 /* If OP0 is an AND and we don't have an AND in MODE either,
10709 make a new AND in the proper mode. */
10710 if (GET_CODE (op0) == AND
10711 && !have_insn_for (AND, mode))
10712 op0 = simplify_gen_binary (AND, tmode,
10713 gen_lowpart (tmode,
10714 XEXP (op0, 0)),
10715 gen_lowpart (tmode,
10716 XEXP (op0, 1)));
10718 op0 = gen_lowpart (tmode, op0);
10719 if (zero_extended && GET_CODE (op1) == CONST_INT)
10720 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10721 op1 = gen_lowpart (tmode, op1);
10722 break;
10725 /* If this is a test for negative, we can make an explicit
10726 test of the sign bit. */
10728 if (op1 == const0_rtx && (code == LT || code == GE)
10729 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10731 op0 = simplify_gen_binary (AND, tmode,
10732 gen_lowpart (tmode, op0),
10733 GEN_INT ((HOST_WIDE_INT) 1
10734 << (GET_MODE_BITSIZE (mode)
10735 - 1)));
10736 code = (code == LT) ? NE : EQ;
10737 break;
10741 #ifdef CANONICALIZE_COMPARISON
10742 /* If this machine only supports a subset of valid comparisons, see if we
10743 can convert an unsupported one into a supported one. */
10744 CANONICALIZE_COMPARISON (code, op0, op1);
10745 #endif
10747 *pop0 = op0;
10748 *pop1 = op1;
10750 return code;
10753 /* Utility function for record_value_for_reg. Count number of
10754 rtxs in X. */
10755 static int
10756 count_rtxs (rtx x)
10758 enum rtx_code code = GET_CODE (x);
10759 const char *fmt;
10760 int i, ret = 1;
10762 if (GET_RTX_CLASS (code) == '2'
10763 || GET_RTX_CLASS (code) == 'c')
10765 rtx x0 = XEXP (x, 0);
10766 rtx x1 = XEXP (x, 1);
10768 if (x0 == x1)
10769 return 1 + 2 * count_rtxs (x0);
10771 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
10772 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
10773 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10774 return 2 + 2 * count_rtxs (x0)
10775 + count_rtxs (x == XEXP (x1, 0)
10776 ? XEXP (x1, 1) : XEXP (x1, 0));
10778 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
10779 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
10780 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10781 return 2 + 2 * count_rtxs (x1)
10782 + count_rtxs (x == XEXP (x0, 0)
10783 ? XEXP (x0, 1) : XEXP (x0, 0));
10786 fmt = GET_RTX_FORMAT (code);
10787 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10788 if (fmt[i] == 'e')
10789 ret += count_rtxs (XEXP (x, i));
10791 return ret;
10794 /* Utility function for following routine. Called when X is part of a value
10795 being stored into last_set_value. Sets last_set_table_tick
10796 for each register mentioned. Similar to mention_regs in cse.c */
10798 static void
10799 update_table_tick (rtx x)
10801 enum rtx_code code = GET_CODE (x);
10802 const char *fmt = GET_RTX_FORMAT (code);
10803 int i;
10805 if (code == REG)
10807 unsigned int regno = REGNO (x);
10808 unsigned int endregno
10809 = regno + (regno < FIRST_PSEUDO_REGISTER
10810 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10811 unsigned int r;
10813 for (r = regno; r < endregno; r++)
10814 reg_stat[r].last_set_table_tick = label_tick;
10816 return;
10819 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10820 /* Note that we can't have an "E" in values stored; see
10821 get_last_value_validate. */
10822 if (fmt[i] == 'e')
10824 /* Check for identical subexpressions. If x contains
10825 identical subexpression we only have to traverse one of
10826 them. */
10827 if (i == 0 && ARITHMETIC_P (x))
10829 /* Note that at this point x1 has already been
10830 processed. */
10831 rtx x0 = XEXP (x, 0);
10832 rtx x1 = XEXP (x, 1);
10834 /* If x0 and x1 are identical then there is no need to
10835 process x0. */
10836 if (x0 == x1)
10837 break;
10839 /* If x0 is identical to a subexpression of x1 then while
10840 processing x1, x0 has already been processed. Thus we
10841 are done with x. */
10842 if (ARITHMETIC_P (x1)
10843 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10844 break;
10846 /* If x1 is identical to a subexpression of x0 then we
10847 still have to process the rest of x0. */
10848 if (ARITHMETIC_P (x0)
10849 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10851 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10852 break;
10856 update_table_tick (XEXP (x, i));
10860 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10861 are saying that the register is clobbered and we no longer know its
10862 value. If INSN is zero, don't update reg_stat[].last_set; this is
10863 only permitted with VALUE also zero and is used to invalidate the
10864 register. */
10866 static void
10867 record_value_for_reg (rtx reg, rtx insn, rtx value)
10869 unsigned int regno = REGNO (reg);
10870 unsigned int endregno
10871 = regno + (regno < FIRST_PSEUDO_REGISTER
10872 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10873 unsigned int i;
10875 /* If VALUE contains REG and we have a previous value for REG, substitute
10876 the previous value. */
10877 if (value && insn && reg_overlap_mentioned_p (reg, value))
10879 rtx tem;
10881 /* Set things up so get_last_value is allowed to see anything set up to
10882 our insn. */
10883 subst_low_cuid = INSN_CUID (insn);
10884 tem = get_last_value (reg);
10886 /* If TEM is simply a binary operation with two CLOBBERs as operands,
10887 it isn't going to be useful and will take a lot of time to process,
10888 so just use the CLOBBER. */
10890 if (tem)
10892 if (ARITHMETIC_P (tem)
10893 && GET_CODE (XEXP (tem, 0)) == CLOBBER
10894 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10895 tem = XEXP (tem, 0);
10896 else if (count_occurrences (value, reg, 1) >= 2)
10898 /* If there are two or more occurrences of REG in VALUE,
10899 prevent the value from growing too much. */
10900 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
10901 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
10904 value = replace_rtx (copy_rtx (value), reg, tem);
10908 /* For each register modified, show we don't know its value, that
10909 we don't know about its bitwise content, that its value has been
10910 updated, and that we don't know the location of the death of the
10911 register. */
10912 for (i = regno; i < endregno; i++)
10914 if (insn)
10915 reg_stat[i].last_set = insn;
10917 reg_stat[i].last_set_value = 0;
10918 reg_stat[i].last_set_mode = 0;
10919 reg_stat[i].last_set_nonzero_bits = 0;
10920 reg_stat[i].last_set_sign_bit_copies = 0;
10921 reg_stat[i].last_death = 0;
10924 /* Mark registers that are being referenced in this value. */
10925 if (value)
10926 update_table_tick (value);
10928 /* Now update the status of each register being set.
10929 If someone is using this register in this block, set this register
10930 to invalid since we will get confused between the two lives in this
10931 basic block. This makes using this register always invalid. In cse, we
10932 scan the table to invalidate all entries using this register, but this
10933 is too much work for us. */
10935 for (i = regno; i < endregno; i++)
10937 reg_stat[i].last_set_label = label_tick;
10938 if (value && reg_stat[i].last_set_table_tick == label_tick)
10939 reg_stat[i].last_set_invalid = 1;
10940 else
10941 reg_stat[i].last_set_invalid = 0;
10944 /* The value being assigned might refer to X (like in "x++;"). In that
10945 case, we must replace it with (clobber (const_int 0)) to prevent
10946 infinite loops. */
10947 if (value && ! get_last_value_validate (&value, insn,
10948 reg_stat[regno].last_set_label, 0))
10950 value = copy_rtx (value);
10951 if (! get_last_value_validate (&value, insn,
10952 reg_stat[regno].last_set_label, 1))
10953 value = 0;
10956 /* For the main register being modified, update the value, the mode, the
10957 nonzero bits, and the number of sign bit copies. */
10959 reg_stat[regno].last_set_value = value;
10961 if (value)
10963 enum machine_mode mode = GET_MODE (reg);
10964 subst_low_cuid = INSN_CUID (insn);
10965 reg_stat[regno].last_set_mode = mode;
10966 if (GET_MODE_CLASS (mode) == MODE_INT
10967 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10968 mode = nonzero_bits_mode;
10969 reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
10970 reg_stat[regno].last_set_sign_bit_copies
10971 = num_sign_bit_copies (value, GET_MODE (reg));
10975 /* Called via note_stores from record_dead_and_set_regs to handle one
10976 SET or CLOBBER in an insn. DATA is the instruction in which the
10977 set is occurring. */
10979 static void
10980 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
10982 rtx record_dead_insn = (rtx) data;
10984 if (GET_CODE (dest) == SUBREG)
10985 dest = SUBREG_REG (dest);
10987 if (REG_P (dest))
10989 /* If we are setting the whole register, we know its value. Otherwise
10990 show that we don't know the value. We can handle SUBREG in
10991 some cases. */
10992 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10993 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10994 else if (GET_CODE (setter) == SET
10995 && GET_CODE (SET_DEST (setter)) == SUBREG
10996 && SUBREG_REG (SET_DEST (setter)) == dest
10997 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10998 && subreg_lowpart_p (SET_DEST (setter)))
10999 record_value_for_reg (dest, record_dead_insn,
11000 gen_lowpart (GET_MODE (dest),
11001 SET_SRC (setter)));
11002 else
11003 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11005 else if (MEM_P (dest)
11006 /* Ignore pushes, they clobber nothing. */
11007 && ! push_operand (dest, GET_MODE (dest)))
11008 mem_last_set = INSN_CUID (record_dead_insn);
11011 /* Update the records of when each REG was most recently set or killed
11012 for the things done by INSN. This is the last thing done in processing
11013 INSN in the combiner loop.
11015 We update reg_stat[], in particular fields last_set, last_set_value,
11016 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11017 last_death, and also the similar information mem_last_set (which insn
11018 most recently modified memory) and last_call_cuid (which insn was the
11019 most recent subroutine call). */
11021 static void
11022 record_dead_and_set_regs (rtx insn)
11024 rtx link;
11025 unsigned int i;
11027 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11029 if (REG_NOTE_KIND (link) == REG_DEAD
11030 && REG_P (XEXP (link, 0)))
11032 unsigned int regno = REGNO (XEXP (link, 0));
11033 unsigned int endregno
11034 = regno + (regno < FIRST_PSEUDO_REGISTER
11035 ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11036 : 1);
11038 for (i = regno; i < endregno; i++)
11039 reg_stat[i].last_death = insn;
11041 else if (REG_NOTE_KIND (link) == REG_INC)
11042 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11045 if (CALL_P (insn))
11047 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11048 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11050 reg_stat[i].last_set_value = 0;
11051 reg_stat[i].last_set_mode = 0;
11052 reg_stat[i].last_set_nonzero_bits = 0;
11053 reg_stat[i].last_set_sign_bit_copies = 0;
11054 reg_stat[i].last_death = 0;
11057 last_call_cuid = mem_last_set = INSN_CUID (insn);
11059 /* Don't bother recording what this insn does. It might set the
11060 return value register, but we can't combine into a call
11061 pattern anyway, so there's no point trying (and it may cause
11062 a crash, if e.g. we wind up asking for last_set_value of a
11063 SUBREG of the return value register). */
11064 return;
11067 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11070 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11071 register present in the SUBREG, so for each such SUBREG go back and
11072 adjust nonzero and sign bit information of the registers that are
11073 known to have some zero/sign bits set.
11075 This is needed because when combine blows the SUBREGs away, the
11076 information on zero/sign bits is lost and further combines can be
11077 missed because of that. */
11079 static void
11080 record_promoted_value (rtx insn, rtx subreg)
11082 rtx links, set;
11083 unsigned int regno = REGNO (SUBREG_REG (subreg));
11084 enum machine_mode mode = GET_MODE (subreg);
11086 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11087 return;
11089 for (links = LOG_LINKS (insn); links;)
11091 insn = XEXP (links, 0);
11092 set = single_set (insn);
11094 if (! set || !REG_P (SET_DEST (set))
11095 || REGNO (SET_DEST (set)) != regno
11096 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11098 links = XEXP (links, 1);
11099 continue;
11102 if (reg_stat[regno].last_set == insn)
11104 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11105 reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11108 if (REG_P (SET_SRC (set)))
11110 regno = REGNO (SET_SRC (set));
11111 links = LOG_LINKS (insn);
11113 else
11114 break;
11118 /* Scan X for promoted SUBREGs. For each one found,
11119 note what it implies to the registers used in it. */
11121 static void
11122 check_promoted_subreg (rtx insn, rtx x)
11124 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11125 && REG_P (SUBREG_REG (x)))
11126 record_promoted_value (insn, x);
11127 else
11129 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11130 int i, j;
11132 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11133 switch (format[i])
11135 case 'e':
11136 check_promoted_subreg (insn, XEXP (x, i));
11137 break;
11138 case 'V':
11139 case 'E':
11140 if (XVEC (x, i) != 0)
11141 for (j = 0; j < XVECLEN (x, i); j++)
11142 check_promoted_subreg (insn, XVECEXP (x, i, j));
11143 break;
11148 /* Utility routine for the following function. Verify that all the registers
11149 mentioned in *LOC are valid when *LOC was part of a value set when
11150 label_tick == TICK. Return 0 if some are not.
11152 If REPLACE is nonzero, replace the invalid reference with
11153 (clobber (const_int 0)) and return 1. This replacement is useful because
11154 we often can get useful information about the form of a value (e.g., if
11155 it was produced by a shift that always produces -1 or 0) even though
11156 we don't know exactly what registers it was produced from. */
11158 static int
11159 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11161 rtx x = *loc;
11162 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11163 int len = GET_RTX_LENGTH (GET_CODE (x));
11164 int i;
11166 if (REG_P (x))
11168 unsigned int regno = REGNO (x);
11169 unsigned int endregno
11170 = regno + (regno < FIRST_PSEUDO_REGISTER
11171 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11172 unsigned int j;
11174 for (j = regno; j < endregno; j++)
11175 if (reg_stat[j].last_set_invalid
11176 /* If this is a pseudo-register that was only set once and not
11177 live at the beginning of the function, it is always valid. */
11178 || (! (regno >= FIRST_PSEUDO_REGISTER
11179 && REG_N_SETS (regno) == 1
11180 && (! REGNO_REG_SET_P
11181 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11182 && reg_stat[j].last_set_label > tick))
11184 if (replace)
11185 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11186 return replace;
11189 return 1;
11191 /* If this is a memory reference, make sure that there were
11192 no stores after it that might have clobbered the value. We don't
11193 have alias info, so we assume any store invalidates it. */
11194 else if (MEM_P (x) && !MEM_READONLY_P (x)
11195 && INSN_CUID (insn) <= mem_last_set)
11197 if (replace)
11198 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11199 return replace;
11202 for (i = 0; i < len; i++)
11204 if (fmt[i] == 'e')
11206 /* Check for identical subexpressions. If x contains
11207 identical subexpression we only have to traverse one of
11208 them. */
11209 if (i == 1 && ARITHMETIC_P (x))
11211 /* Note that at this point x0 has already been checked
11212 and found valid. */
11213 rtx x0 = XEXP (x, 0);
11214 rtx x1 = XEXP (x, 1);
11216 /* If x0 and x1 are identical then x is also valid. */
11217 if (x0 == x1)
11218 return 1;
11220 /* If x1 is identical to a subexpression of x0 then
11221 while checking x0, x1 has already been checked. Thus
11222 it is valid and so as x. */
11223 if (ARITHMETIC_P (x0)
11224 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11225 return 1;
11227 /* If x0 is identical to a subexpression of x1 then x is
11228 valid iff the rest of x1 is valid. */
11229 if (ARITHMETIC_P (x1)
11230 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11231 return
11232 get_last_value_validate (&XEXP (x1,
11233 x0 == XEXP (x1, 0) ? 1 : 0),
11234 insn, tick, replace);
11237 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11238 replace) == 0)
11239 return 0;
11241 /* Don't bother with these. They shouldn't occur anyway. */
11242 else if (fmt[i] == 'E')
11243 return 0;
11246 /* If we haven't found a reason for it to be invalid, it is valid. */
11247 return 1;
11250 /* Get the last value assigned to X, if known. Some registers
11251 in the value may be replaced with (clobber (const_int 0)) if their value
11252 is known longer known reliably. */
11254 static rtx
11255 get_last_value (rtx x)
11257 unsigned int regno;
11258 rtx value;
11260 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11261 then convert it to the desired mode. If this is a paradoxical SUBREG,
11262 we cannot predict what values the "extra" bits might have. */
11263 if (GET_CODE (x) == SUBREG
11264 && subreg_lowpart_p (x)
11265 && (GET_MODE_SIZE (GET_MODE (x))
11266 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11267 && (value = get_last_value (SUBREG_REG (x))) != 0)
11268 return gen_lowpart (GET_MODE (x), value);
11270 if (!REG_P (x))
11271 return 0;
11273 regno = REGNO (x);
11274 value = reg_stat[regno].last_set_value;
11276 /* If we don't have a value, or if it isn't for this basic block and
11277 it's either a hard register, set more than once, or it's a live
11278 at the beginning of the function, return 0.
11280 Because if it's not live at the beginning of the function then the reg
11281 is always set before being used (is never used without being set).
11282 And, if it's set only once, and it's always set before use, then all
11283 uses must have the same last value, even if it's not from this basic
11284 block. */
11286 if (value == 0
11287 || (reg_stat[regno].last_set_label != label_tick
11288 && (regno < FIRST_PSEUDO_REGISTER
11289 || REG_N_SETS (regno) != 1
11290 || (REGNO_REG_SET_P
11291 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11292 return 0;
11294 /* If the value was set in a later insn than the ones we are processing,
11295 we can't use it even if the register was only set once. */
11296 if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11297 return 0;
11299 /* If the value has all its registers valid, return it. */
11300 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11301 reg_stat[regno].last_set_label, 0))
11302 return value;
11304 /* Otherwise, make a copy and replace any invalid register with
11305 (clobber (const_int 0)). If that fails for some reason, return 0. */
11307 value = copy_rtx (value);
11308 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11309 reg_stat[regno].last_set_label, 1))
11310 return value;
11312 return 0;
11315 /* Return nonzero if expression X refers to a REG or to memory
11316 that is set in an instruction more recent than FROM_CUID. */
11318 static int
11319 use_crosses_set_p (rtx x, int from_cuid)
11321 const char *fmt;
11322 int i;
11323 enum rtx_code code = GET_CODE (x);
11325 if (code == REG)
11327 unsigned int regno = REGNO (x);
11328 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11329 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11331 #ifdef PUSH_ROUNDING
11332 /* Don't allow uses of the stack pointer to be moved,
11333 because we don't know whether the move crosses a push insn. */
11334 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11335 return 1;
11336 #endif
11337 for (; regno < endreg; regno++)
11338 if (reg_stat[regno].last_set
11339 && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11340 return 1;
11341 return 0;
11344 if (code == MEM && mem_last_set > from_cuid)
11345 return 1;
11347 fmt = GET_RTX_FORMAT (code);
11349 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11351 if (fmt[i] == 'E')
11353 int j;
11354 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11355 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11356 return 1;
11358 else if (fmt[i] == 'e'
11359 && use_crosses_set_p (XEXP (x, i), from_cuid))
11360 return 1;
11362 return 0;
11365 /* Define three variables used for communication between the following
11366 routines. */
11368 static unsigned int reg_dead_regno, reg_dead_endregno;
11369 static int reg_dead_flag;
11371 /* Function called via note_stores from reg_dead_at_p.
11373 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11374 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11376 static void
11377 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11379 unsigned int regno, endregno;
11381 if (!REG_P (dest))
11382 return;
11384 regno = REGNO (dest);
11385 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11386 ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11388 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11389 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11392 /* Return nonzero if REG is known to be dead at INSN.
11394 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11395 referencing REG, it is dead. If we hit a SET referencing REG, it is
11396 live. Otherwise, see if it is live or dead at the start of the basic
11397 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11398 must be assumed to be always live. */
11400 static int
11401 reg_dead_at_p (rtx reg, rtx insn)
11403 basic_block block;
11404 unsigned int i;
11406 /* Set variables for reg_dead_at_p_1. */
11407 reg_dead_regno = REGNO (reg);
11408 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11409 ? hard_regno_nregs[reg_dead_regno]
11410 [GET_MODE (reg)]
11411 : 1);
11413 reg_dead_flag = 0;
11415 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11416 we allow the machine description to decide whether use-and-clobber
11417 patterns are OK. */
11418 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11420 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11421 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11422 return 0;
11425 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11426 beginning of function. */
11427 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11428 insn = prev_nonnote_insn (insn))
11430 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11431 if (reg_dead_flag)
11432 return reg_dead_flag == 1 ? 1 : 0;
11434 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11435 return 1;
11438 /* Get the basic block that we were in. */
11439 if (insn == 0)
11440 block = ENTRY_BLOCK_PTR->next_bb;
11441 else
11443 FOR_EACH_BB (block)
11444 if (insn == BB_HEAD (block))
11445 break;
11447 if (block == EXIT_BLOCK_PTR)
11448 return 0;
11451 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11452 if (REGNO_REG_SET_P (block->global_live_at_start, i))
11453 return 0;
11455 return 1;
11458 /* Note hard registers in X that are used. This code is similar to
11459 that in flow.c, but much simpler since we don't care about pseudos. */
11461 static void
11462 mark_used_regs_combine (rtx x)
11464 RTX_CODE code = GET_CODE (x);
11465 unsigned int regno;
11466 int i;
11468 switch (code)
11470 case LABEL_REF:
11471 case SYMBOL_REF:
11472 case CONST_INT:
11473 case CONST:
11474 case CONST_DOUBLE:
11475 case CONST_VECTOR:
11476 case PC:
11477 case ADDR_VEC:
11478 case ADDR_DIFF_VEC:
11479 case ASM_INPUT:
11480 #ifdef HAVE_cc0
11481 /* CC0 must die in the insn after it is set, so we don't need to take
11482 special note of it here. */
11483 case CC0:
11484 #endif
11485 return;
11487 case CLOBBER:
11488 /* If we are clobbering a MEM, mark any hard registers inside the
11489 address as used. */
11490 if (MEM_P (XEXP (x, 0)))
11491 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11492 return;
11494 case REG:
11495 regno = REGNO (x);
11496 /* A hard reg in a wide mode may really be multiple registers.
11497 If so, mark all of them just like the first. */
11498 if (regno < FIRST_PSEUDO_REGISTER)
11500 unsigned int endregno, r;
11502 /* None of this applies to the stack, frame or arg pointers. */
11503 if (regno == STACK_POINTER_REGNUM
11504 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11505 || regno == HARD_FRAME_POINTER_REGNUM
11506 #endif
11507 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11508 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11509 #endif
11510 || regno == FRAME_POINTER_REGNUM)
11511 return;
11513 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11514 for (r = regno; r < endregno; r++)
11515 SET_HARD_REG_BIT (newpat_used_regs, r);
11517 return;
11519 case SET:
11521 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11522 the address. */
11523 rtx testreg = SET_DEST (x);
11525 while (GET_CODE (testreg) == SUBREG
11526 || GET_CODE (testreg) == ZERO_EXTRACT
11527 || GET_CODE (testreg) == STRICT_LOW_PART)
11528 testreg = XEXP (testreg, 0);
11530 if (MEM_P (testreg))
11531 mark_used_regs_combine (XEXP (testreg, 0));
11533 mark_used_regs_combine (SET_SRC (x));
11535 return;
11537 default:
11538 break;
11541 /* Recursively scan the operands of this expression. */
11544 const char *fmt = GET_RTX_FORMAT (code);
11546 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11548 if (fmt[i] == 'e')
11549 mark_used_regs_combine (XEXP (x, i));
11550 else if (fmt[i] == 'E')
11552 int j;
11554 for (j = 0; j < XVECLEN (x, i); j++)
11555 mark_used_regs_combine (XVECEXP (x, i, j));
11561 /* Remove register number REGNO from the dead registers list of INSN.
11563 Return the note used to record the death, if there was one. */
11566 remove_death (unsigned int regno, rtx insn)
11568 rtx note = find_regno_note (insn, REG_DEAD, regno);
11570 if (note)
11572 REG_N_DEATHS (regno)--;
11573 remove_note (insn, note);
11576 return note;
11579 /* For each register (hardware or pseudo) used within expression X, if its
11580 death is in an instruction with cuid between FROM_CUID (inclusive) and
11581 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11582 list headed by PNOTES.
11584 That said, don't move registers killed by maybe_kill_insn.
11586 This is done when X is being merged by combination into TO_INSN. These
11587 notes will then be distributed as needed. */
11589 static void
11590 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11591 rtx *pnotes)
11593 const char *fmt;
11594 int len, i;
11595 enum rtx_code code = GET_CODE (x);
11597 if (code == REG)
11599 unsigned int regno = REGNO (x);
11600 rtx where_dead = reg_stat[regno].last_death;
11601 rtx before_dead, after_dead;
11603 /* Don't move the register if it gets killed in between from and to. */
11604 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11605 && ! reg_referenced_p (x, maybe_kill_insn))
11606 return;
11608 /* WHERE_DEAD could be a USE insn made by combine, so first we
11609 make sure that we have insns with valid INSN_CUID values. */
11610 before_dead = where_dead;
11611 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11612 before_dead = PREV_INSN (before_dead);
11614 after_dead = where_dead;
11615 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11616 after_dead = NEXT_INSN (after_dead);
11618 if (before_dead && after_dead
11619 && INSN_CUID (before_dead) >= from_cuid
11620 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11621 || (where_dead != after_dead
11622 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11624 rtx note = remove_death (regno, where_dead);
11626 /* It is possible for the call above to return 0. This can occur
11627 when last_death points to I2 or I1 that we combined with.
11628 In that case make a new note.
11630 We must also check for the case where X is a hard register
11631 and NOTE is a death note for a range of hard registers
11632 including X. In that case, we must put REG_DEAD notes for
11633 the remaining registers in place of NOTE. */
11635 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11636 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11637 > GET_MODE_SIZE (GET_MODE (x))))
11639 unsigned int deadregno = REGNO (XEXP (note, 0));
11640 unsigned int deadend
11641 = (deadregno + hard_regno_nregs[deadregno]
11642 [GET_MODE (XEXP (note, 0))]);
11643 unsigned int ourend
11644 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11645 unsigned int i;
11647 for (i = deadregno; i < deadend; i++)
11648 if (i < regno || i >= ourend)
11649 REG_NOTES (where_dead)
11650 = gen_rtx_EXPR_LIST (REG_DEAD,
11651 regno_reg_rtx[i],
11652 REG_NOTES (where_dead));
11655 /* If we didn't find any note, or if we found a REG_DEAD note that
11656 covers only part of the given reg, and we have a multi-reg hard
11657 register, then to be safe we must check for REG_DEAD notes
11658 for each register other than the first. They could have
11659 their own REG_DEAD notes lying around. */
11660 else if ((note == 0
11661 || (note != 0
11662 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11663 < GET_MODE_SIZE (GET_MODE (x)))))
11664 && regno < FIRST_PSEUDO_REGISTER
11665 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11667 unsigned int ourend
11668 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11669 unsigned int i, offset;
11670 rtx oldnotes = 0;
11672 if (note)
11673 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11674 else
11675 offset = 1;
11677 for (i = regno + offset; i < ourend; i++)
11678 move_deaths (regno_reg_rtx[i],
11679 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11682 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11684 XEXP (note, 1) = *pnotes;
11685 *pnotes = note;
11687 else
11688 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11690 REG_N_DEATHS (regno)++;
11693 return;
11696 else if (GET_CODE (x) == SET)
11698 rtx dest = SET_DEST (x);
11700 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11702 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11703 that accesses one word of a multi-word item, some
11704 piece of everything register in the expression is used by
11705 this insn, so remove any old death. */
11706 /* ??? So why do we test for equality of the sizes? */
11708 if (GET_CODE (dest) == ZERO_EXTRACT
11709 || GET_CODE (dest) == STRICT_LOW_PART
11710 || (GET_CODE (dest) == SUBREG
11711 && (((GET_MODE_SIZE (GET_MODE (dest))
11712 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11713 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11714 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11716 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11717 return;
11720 /* If this is some other SUBREG, we know it replaces the entire
11721 value, so use that as the destination. */
11722 if (GET_CODE (dest) == SUBREG)
11723 dest = SUBREG_REG (dest);
11725 /* If this is a MEM, adjust deaths of anything used in the address.
11726 For a REG (the only other possibility), the entire value is
11727 being replaced so the old value is not used in this insn. */
11729 if (MEM_P (dest))
11730 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11731 to_insn, pnotes);
11732 return;
11735 else if (GET_CODE (x) == CLOBBER)
11736 return;
11738 len = GET_RTX_LENGTH (code);
11739 fmt = GET_RTX_FORMAT (code);
11741 for (i = 0; i < len; i++)
11743 if (fmt[i] == 'E')
11745 int j;
11746 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11747 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11748 to_insn, pnotes);
11750 else if (fmt[i] == 'e')
11751 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11755 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11756 pattern of an insn. X must be a REG. */
11758 static int
11759 reg_bitfield_target_p (rtx x, rtx body)
11761 int i;
11763 if (GET_CODE (body) == SET)
11765 rtx dest = SET_DEST (body);
11766 rtx target;
11767 unsigned int regno, tregno, endregno, endtregno;
11769 if (GET_CODE (dest) == ZERO_EXTRACT)
11770 target = XEXP (dest, 0);
11771 else if (GET_CODE (dest) == STRICT_LOW_PART)
11772 target = SUBREG_REG (XEXP (dest, 0));
11773 else
11774 return 0;
11776 if (GET_CODE (target) == SUBREG)
11777 target = SUBREG_REG (target);
11779 if (!REG_P (target))
11780 return 0;
11782 tregno = REGNO (target), regno = REGNO (x);
11783 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11784 return target == x;
11786 endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11787 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11789 return endregno > tregno && regno < endtregno;
11792 else if (GET_CODE (body) == PARALLEL)
11793 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11794 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11795 return 1;
11797 return 0;
11800 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11801 as appropriate. I3 and I2 are the insns resulting from the combination
11802 insns including FROM (I2 may be zero).
11804 Each note in the list is either ignored or placed on some insns, depending
11805 on the type of note. */
11807 static void
11808 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
11810 rtx note, next_note;
11811 rtx tem;
11813 for (note = notes; note; note = next_note)
11815 rtx place = 0, place2 = 0;
11817 /* If this NOTE references a pseudo register, ensure it references
11818 the latest copy of that register. */
11819 if (XEXP (note, 0) && REG_P (XEXP (note, 0))
11820 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11821 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11823 next_note = XEXP (note, 1);
11824 switch (REG_NOTE_KIND (note))
11826 case REG_BR_PROB:
11827 case REG_BR_PRED:
11828 /* Doesn't matter much where we put this, as long as it's somewhere.
11829 It is preferable to keep these notes on branches, which is most
11830 likely to be i3. */
11831 place = i3;
11832 break;
11834 case REG_VALUE_PROFILE:
11835 /* Just get rid of this note, as it is unused later anyway. */
11836 break;
11838 case REG_NON_LOCAL_GOTO:
11839 if (JUMP_P (i3))
11840 place = i3;
11841 else
11843 gcc_assert (i2 && JUMP_P (i2));
11844 place = i2;
11846 break;
11848 case REG_EH_REGION:
11849 /* These notes must remain with the call or trapping instruction. */
11850 if (CALL_P (i3))
11851 place = i3;
11852 else if (i2 && CALL_P (i2))
11853 place = i2;
11854 else
11856 gcc_assert (flag_non_call_exceptions);
11857 if (may_trap_p (i3))
11858 place = i3;
11859 else if (i2 && may_trap_p (i2))
11860 place = i2;
11861 /* ??? Otherwise assume we've combined things such that we
11862 can now prove that the instructions can't trap. Drop the
11863 note in this case. */
11865 break;
11867 case REG_NORETURN:
11868 case REG_SETJMP:
11869 /* These notes must remain with the call. It should not be
11870 possible for both I2 and I3 to be a call. */
11871 if (CALL_P (i3))
11872 place = i3;
11873 else
11875 gcc_assert (i2 && CALL_P (i2));
11876 place = i2;
11878 break;
11880 case REG_UNUSED:
11881 /* Any clobbers for i3 may still exist, and so we must process
11882 REG_UNUSED notes from that insn.
11884 Any clobbers from i2 or i1 can only exist if they were added by
11885 recog_for_combine. In that case, recog_for_combine created the
11886 necessary REG_UNUSED notes. Trying to keep any original
11887 REG_UNUSED notes from these insns can cause incorrect output
11888 if it is for the same register as the original i3 dest.
11889 In that case, we will notice that the register is set in i3,
11890 and then add a REG_UNUSED note for the destination of i3, which
11891 is wrong. However, it is possible to have REG_UNUSED notes from
11892 i2 or i1 for register which were both used and clobbered, so
11893 we keep notes from i2 or i1 if they will turn into REG_DEAD
11894 notes. */
11896 /* If this register is set or clobbered in I3, put the note there
11897 unless there is one already. */
11898 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11900 if (from_insn != i3)
11901 break;
11903 if (! (REG_P (XEXP (note, 0))
11904 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11905 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11906 place = i3;
11908 /* Otherwise, if this register is used by I3, then this register
11909 now dies here, so we must put a REG_DEAD note here unless there
11910 is one already. */
11911 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11912 && ! (REG_P (XEXP (note, 0))
11913 ? find_regno_note (i3, REG_DEAD,
11914 REGNO (XEXP (note, 0)))
11915 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11917 PUT_REG_NOTE_KIND (note, REG_DEAD);
11918 place = i3;
11920 break;
11922 case REG_EQUAL:
11923 case REG_EQUIV:
11924 case REG_NOALIAS:
11925 /* These notes say something about results of an insn. We can
11926 only support them if they used to be on I3 in which case they
11927 remain on I3. Otherwise they are ignored.
11929 If the note refers to an expression that is not a constant, we
11930 must also ignore the note since we cannot tell whether the
11931 equivalence is still true. It might be possible to do
11932 slightly better than this (we only have a problem if I2DEST
11933 or I1DEST is present in the expression), but it doesn't
11934 seem worth the trouble. */
11936 if (from_insn == i3
11937 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11938 place = i3;
11939 break;
11941 case REG_INC:
11942 case REG_NO_CONFLICT:
11943 /* These notes say something about how a register is used. They must
11944 be present on any use of the register in I2 or I3. */
11945 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11946 place = i3;
11948 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11950 if (place)
11951 place2 = i2;
11952 else
11953 place = i2;
11955 break;
11957 case REG_LABEL:
11958 /* This can show up in several ways -- either directly in the
11959 pattern, or hidden off in the constant pool with (or without?)
11960 a REG_EQUAL note. */
11961 /* ??? Ignore the without-reg_equal-note problem for now. */
11962 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11963 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11964 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11965 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11966 place = i3;
11968 if (i2
11969 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11970 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11971 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11972 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11974 if (place)
11975 place2 = i2;
11976 else
11977 place = i2;
11980 /* Don't attach REG_LABEL note to a JUMP_INSN. Add
11981 a JUMP_LABEL instead or decrement LABEL_NUSES. */
11982 if (place && JUMP_P (place))
11984 rtx label = JUMP_LABEL (place);
11986 if (!label)
11987 JUMP_LABEL (place) = XEXP (note, 0);
11988 else
11990 gcc_assert (label == XEXP (note, 0));
11991 if (LABEL_P (label))
11992 LABEL_NUSES (label)--;
11994 place = 0;
11996 if (place2 && JUMP_P (place2))
11998 rtx label = JUMP_LABEL (place2);
12000 if (!label)
12001 JUMP_LABEL (place2) = XEXP (note, 0);
12002 else
12004 gcc_assert (label == XEXP (note, 0));
12005 if (LABEL_P (label))
12006 LABEL_NUSES (label)--;
12008 place2 = 0;
12010 break;
12012 case REG_NONNEG:
12013 /* This note says something about the value of a register prior
12014 to the execution of an insn. It is too much trouble to see
12015 if the note is still correct in all situations. It is better
12016 to simply delete it. */
12017 break;
12019 case REG_RETVAL:
12020 /* If the insn previously containing this note still exists,
12021 put it back where it was. Otherwise move it to the previous
12022 insn. Adjust the corresponding REG_LIBCALL note. */
12023 if (!NOTE_P (from_insn))
12024 place = from_insn;
12025 else
12027 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12028 place = prev_real_insn (from_insn);
12029 if (tem && place)
12030 XEXP (tem, 0) = place;
12031 /* If we're deleting the last remaining instruction of a
12032 libcall sequence, don't add the notes. */
12033 else if (XEXP (note, 0) == from_insn)
12034 tem = place = 0;
12035 /* Don't add the dangling REG_RETVAL note. */
12036 else if (! tem)
12037 place = 0;
12039 break;
12041 case REG_LIBCALL:
12042 /* This is handled similarly to REG_RETVAL. */
12043 if (!NOTE_P (from_insn))
12044 place = from_insn;
12045 else
12047 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12048 place = next_real_insn (from_insn);
12049 if (tem && place)
12050 XEXP (tem, 0) = place;
12051 /* If we're deleting the last remaining instruction of a
12052 libcall sequence, don't add the notes. */
12053 else if (XEXP (note, 0) == from_insn)
12054 tem = place = 0;
12055 /* Don't add the dangling REG_LIBCALL note. */
12056 else if (! tem)
12057 place = 0;
12059 break;
12061 case REG_DEAD:
12062 /* If the register is used as an input in I3, it dies there.
12063 Similarly for I2, if it is nonzero and adjacent to I3.
12065 If the register is not used as an input in either I3 or I2
12066 and it is not one of the registers we were supposed to eliminate,
12067 there are two possibilities. We might have a non-adjacent I2
12068 or we might have somehow eliminated an additional register
12069 from a computation. For example, we might have had A & B where
12070 we discover that B will always be zero. In this case we will
12071 eliminate the reference to A.
12073 In both cases, we must search to see if we can find a previous
12074 use of A and put the death note there. */
12076 if (from_insn
12077 && CALL_P (from_insn)
12078 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12079 place = from_insn;
12080 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12081 place = i3;
12082 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12083 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12084 place = i2;
12086 if (place == 0)
12088 basic_block bb = this_basic_block;
12090 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12092 if (! INSN_P (tem))
12094 if (tem == BB_HEAD (bb))
12095 break;
12096 continue;
12099 /* If the register is being set at TEM, see if that is all
12100 TEM is doing. If so, delete TEM. Otherwise, make this
12101 into a REG_UNUSED note instead. Don't delete sets to
12102 global register vars. */
12103 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12104 || !global_regs[REGNO (XEXP (note, 0))])
12105 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12107 rtx set = single_set (tem);
12108 rtx inner_dest = 0;
12109 #ifdef HAVE_cc0
12110 rtx cc0_setter = NULL_RTX;
12111 #endif
12113 if (set != 0)
12114 for (inner_dest = SET_DEST (set);
12115 (GET_CODE (inner_dest) == STRICT_LOW_PART
12116 || GET_CODE (inner_dest) == SUBREG
12117 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12118 inner_dest = XEXP (inner_dest, 0))
12121 /* Verify that it was the set, and not a clobber that
12122 modified the register.
12124 CC0 targets must be careful to maintain setter/user
12125 pairs. If we cannot delete the setter due to side
12126 effects, mark the user with an UNUSED note instead
12127 of deleting it. */
12129 if (set != 0 && ! side_effects_p (SET_SRC (set))
12130 && rtx_equal_p (XEXP (note, 0), inner_dest)
12131 #ifdef HAVE_cc0
12132 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12133 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12134 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12135 #endif
12138 /* Move the notes and links of TEM elsewhere.
12139 This might delete other dead insns recursively.
12140 First set the pattern to something that won't use
12141 any register. */
12142 rtx old_notes = REG_NOTES (tem);
12144 PATTERN (tem) = pc_rtx;
12145 REG_NOTES (tem) = NULL;
12147 distribute_notes (old_notes, tem, tem, NULL_RTX);
12148 distribute_links (LOG_LINKS (tem));
12150 SET_INSN_DELETED (tem);
12152 #ifdef HAVE_cc0
12153 /* Delete the setter too. */
12154 if (cc0_setter)
12156 PATTERN (cc0_setter) = pc_rtx;
12157 old_notes = REG_NOTES (cc0_setter);
12158 REG_NOTES (cc0_setter) = NULL;
12160 distribute_notes (old_notes, cc0_setter,
12161 cc0_setter, NULL_RTX);
12162 distribute_links (LOG_LINKS (cc0_setter));
12164 SET_INSN_DELETED (cc0_setter);
12166 #endif
12168 else
12170 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12172 /* If there isn't already a REG_UNUSED note, put one
12173 here. Do not place a REG_DEAD note, even if
12174 the register is also used here; that would not
12175 match the algorithm used in lifetime analysis
12176 and can cause the consistency check in the
12177 scheduler to fail. */
12178 if (! find_regno_note (tem, REG_UNUSED,
12179 REGNO (XEXP (note, 0))))
12180 place = tem;
12181 break;
12184 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12185 || (CALL_P (tem)
12186 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12188 place = tem;
12190 /* If we are doing a 3->2 combination, and we have a
12191 register which formerly died in i3 and was not used
12192 by i2, which now no longer dies in i3 and is used in
12193 i2 but does not die in i2, and place is between i2
12194 and i3, then we may need to move a link from place to
12195 i2. */
12196 if (i2 && INSN_UID (place) <= max_uid_cuid
12197 && INSN_CUID (place) > INSN_CUID (i2)
12198 && from_insn
12199 && INSN_CUID (from_insn) > INSN_CUID (i2)
12200 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12202 rtx links = LOG_LINKS (place);
12203 LOG_LINKS (place) = 0;
12204 distribute_links (links);
12206 break;
12209 if (tem == BB_HEAD (bb))
12210 break;
12213 /* We haven't found an insn for the death note and it
12214 is still a REG_DEAD note, but we have hit the beginning
12215 of the block. If the existing life info says the reg
12216 was dead, there's nothing left to do. Otherwise, we'll
12217 need to do a global life update after combine. */
12218 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12219 && REGNO_REG_SET_P (bb->global_live_at_start,
12220 REGNO (XEXP (note, 0))))
12221 SET_BIT (refresh_blocks, this_basic_block->index);
12224 /* If the register is set or already dead at PLACE, we needn't do
12225 anything with this note if it is still a REG_DEAD note.
12226 We check here if it is set at all, not if is it totally replaced,
12227 which is what `dead_or_set_p' checks, so also check for it being
12228 set partially. */
12230 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12232 unsigned int regno = REGNO (XEXP (note, 0));
12234 /* Similarly, if the instruction on which we want to place
12235 the note is a noop, we'll need do a global live update
12236 after we remove them in delete_noop_moves. */
12237 if (noop_move_p (place))
12238 SET_BIT (refresh_blocks, this_basic_block->index);
12240 if (dead_or_set_p (place, XEXP (note, 0))
12241 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12243 /* Unless the register previously died in PLACE, clear
12244 last_death. [I no longer understand why this is
12245 being done.] */
12246 if (reg_stat[regno].last_death != place)
12247 reg_stat[regno].last_death = 0;
12248 place = 0;
12250 else
12251 reg_stat[regno].last_death = place;
12253 /* If this is a death note for a hard reg that is occupying
12254 multiple registers, ensure that we are still using all
12255 parts of the object. If we find a piece of the object
12256 that is unused, we must arrange for an appropriate REG_DEAD
12257 note to be added for it. However, we can't just emit a USE
12258 and tag the note to it, since the register might actually
12259 be dead; so we recourse, and the recursive call then finds
12260 the previous insn that used this register. */
12262 if (place && regno < FIRST_PSEUDO_REGISTER
12263 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12265 unsigned int endregno
12266 = regno + hard_regno_nregs[regno]
12267 [GET_MODE (XEXP (note, 0))];
12268 int all_used = 1;
12269 unsigned int i;
12271 for (i = regno; i < endregno; i++)
12272 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12273 && ! find_regno_fusage (place, USE, i))
12274 || dead_or_set_regno_p (place, i))
12275 all_used = 0;
12277 if (! all_used)
12279 /* Put only REG_DEAD notes for pieces that are
12280 not already dead or set. */
12282 for (i = regno; i < endregno;
12283 i += hard_regno_nregs[i][reg_raw_mode[i]])
12285 rtx piece = regno_reg_rtx[i];
12286 basic_block bb = this_basic_block;
12288 if (! dead_or_set_p (place, piece)
12289 && ! reg_bitfield_target_p (piece,
12290 PATTERN (place)))
12292 rtx new_note
12293 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12295 distribute_notes (new_note, place, place,
12296 NULL_RTX);
12298 else if (! refers_to_regno_p (i, i + 1,
12299 PATTERN (place), 0)
12300 && ! find_regno_fusage (place, USE, i))
12301 for (tem = PREV_INSN (place); ;
12302 tem = PREV_INSN (tem))
12304 if (! INSN_P (tem))
12306 if (tem == BB_HEAD (bb))
12308 SET_BIT (refresh_blocks,
12309 this_basic_block->index);
12310 break;
12312 continue;
12314 if (dead_or_set_p (tem, piece)
12315 || reg_bitfield_target_p (piece,
12316 PATTERN (tem)))
12318 REG_NOTES (tem)
12319 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12320 REG_NOTES (tem));
12321 break;
12327 place = 0;
12331 break;
12333 default:
12334 /* Any other notes should not be present at this point in the
12335 compilation. */
12336 gcc_unreachable ();
12339 if (place)
12341 XEXP (note, 1) = REG_NOTES (place);
12342 REG_NOTES (place) = note;
12344 else if ((REG_NOTE_KIND (note) == REG_DEAD
12345 || REG_NOTE_KIND (note) == REG_UNUSED)
12346 && REG_P (XEXP (note, 0)))
12347 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12349 if (place2)
12351 if ((REG_NOTE_KIND (note) == REG_DEAD
12352 || REG_NOTE_KIND (note) == REG_UNUSED)
12353 && REG_P (XEXP (note, 0)))
12354 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12356 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12357 REG_NOTE_KIND (note),
12358 XEXP (note, 0),
12359 REG_NOTES (place2));
12364 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12365 I3, I2, and I1 to new locations. This is also called to add a link
12366 pointing at I3 when I3's destination is changed. */
12368 static void
12369 distribute_links (rtx links)
12371 rtx link, next_link;
12373 for (link = links; link; link = next_link)
12375 rtx place = 0;
12376 rtx insn;
12377 rtx set, reg;
12379 next_link = XEXP (link, 1);
12381 /* If the insn that this link points to is a NOTE or isn't a single
12382 set, ignore it. In the latter case, it isn't clear what we
12383 can do other than ignore the link, since we can't tell which
12384 register it was for. Such links wouldn't be used by combine
12385 anyway.
12387 It is not possible for the destination of the target of the link to
12388 have been changed by combine. The only potential of this is if we
12389 replace I3, I2, and I1 by I3 and I2. But in that case the
12390 destination of I2 also remains unchanged. */
12392 if (NOTE_P (XEXP (link, 0))
12393 || (set = single_set (XEXP (link, 0))) == 0)
12394 continue;
12396 reg = SET_DEST (set);
12397 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12398 || GET_CODE (reg) == STRICT_LOW_PART)
12399 reg = XEXP (reg, 0);
12401 /* A LOG_LINK is defined as being placed on the first insn that uses
12402 a register and points to the insn that sets the register. Start
12403 searching at the next insn after the target of the link and stop
12404 when we reach a set of the register or the end of the basic block.
12406 Note that this correctly handles the link that used to point from
12407 I3 to I2. Also note that not much searching is typically done here
12408 since most links don't point very far away. */
12410 for (insn = NEXT_INSN (XEXP (link, 0));
12411 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12412 || BB_HEAD (this_basic_block->next_bb) != insn));
12413 insn = NEXT_INSN (insn))
12414 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12416 if (reg_referenced_p (reg, PATTERN (insn)))
12417 place = insn;
12418 break;
12420 else if (CALL_P (insn)
12421 && find_reg_fusage (insn, USE, reg))
12423 place = insn;
12424 break;
12426 else if (INSN_P (insn) && reg_set_p (reg, insn))
12427 break;
12429 /* If we found a place to put the link, place it there unless there
12430 is already a link to the same insn as LINK at that point. */
12432 if (place)
12434 rtx link2;
12436 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12437 if (XEXP (link2, 0) == XEXP (link, 0))
12438 break;
12440 if (link2 == 0)
12442 XEXP (link, 1) = LOG_LINKS (place);
12443 LOG_LINKS (place) = link;
12445 /* Set added_links_insn to the earliest insn we added a
12446 link to. */
12447 if (added_links_insn == 0
12448 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12449 added_links_insn = place;
12455 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12456 Check whether the expression pointer to by LOC is a register or
12457 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12458 Otherwise return zero. */
12460 static int
12461 unmentioned_reg_p_1 (rtx *loc, void *expr)
12463 rtx x = *loc;
12465 if (x != NULL_RTX
12466 && (REG_P (x) || MEM_P (x))
12467 && ! reg_mentioned_p (x, (rtx) expr))
12468 return 1;
12469 return 0;
12472 /* Check for any register or memory mentioned in EQUIV that is not
12473 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12474 of EXPR where some registers may have been replaced by constants. */
12476 static bool
12477 unmentioned_reg_p (rtx equiv, rtx expr)
12479 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12482 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12484 static int
12485 insn_cuid (rtx insn)
12487 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12488 && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE)
12489 insn = NEXT_INSN (insn);
12491 gcc_assert (INSN_UID (insn) <= max_uid_cuid);
12493 return INSN_CUID (insn);
12496 void
12497 dump_combine_stats (FILE *file)
12499 fnotice
12500 (file,
12501 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12502 combine_attempts, combine_merges, combine_extras, combine_successes);
12505 void
12506 dump_combine_total_stats (FILE *file)
12508 fnotice
12509 (file,
12510 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12511 total_attempts, total_merges, total_extras, total_successes);